[clang] 6168739 - [clang][bytecode] Reject memcpy sizes with element size remainder (#119209)

via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 9 07:57:53 PST 2024


Author: Timm Baeder
Date: 2024-12-09T16:57:49+01:00
New Revision: 6168739f00616f34354e0b34852398aeced98f53

URL: https://github.com/llvm/llvm-project/commit/6168739f00616f34354e0b34852398aeced98f53
DIFF: https://github.com/llvm/llvm-project/commit/6168739f00616f34354e0b34852398aeced98f53.diff

LOG: [clang][bytecode] Reject memcpy sizes with element size remainder (#119209)

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/InterpBuiltin.cpp
    clang/test/AST/ByteCode/builtin-functions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 9f2e64b3faeff2..a0de193ec32a2f 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1860,6 +1860,21 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
     return false;
   }
 
+  QualType ElemType;
+  if (SrcPtr.getFieldDesc()->isArray())
+    ElemType = SrcPtr.getFieldDesc()->getElemQualType();
+  else
+    ElemType = SrcPtr.getType();
+
+  unsigned ElemSize =
+      S.getASTContext().getTypeSizeInChars(ElemType).getQuantity();
+  if (Size.urem(ElemSize) != 0) {
+    S.FFDiag(S.Current->getSource(OpPC),
+             diag::note_constexpr_memcpy_unsupported)
+        << Move << /*IsWchar=*/false << 0 << ElemType << Size << ElemSize;
+    return false;
+  }
+
   // As a last resort, reject dummy pointers.
   if (DestPtr.isDummy() || SrcPtr.isDummy())
     return false;

diff  --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index bd453d6d342d3f..a67b6b665f3790 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1197,4 +1197,14 @@ namespace BuiltinMemcpy {
     return b;
   }
   static_assert(simpleMove() == 12);
+
+  constexpr int memcpyTypeRem() { // ref-error {{never produces a constant expression}}
+    int a = 12;
+    int b = 0;
+    __builtin_memmove(&b, &a, 1); // both-note {{'memmove' not supported: size to copy (1) is not a multiple of size of element type 'int'}} \
+                                  // ref-note {{not supported}}
+    return b;
+  }
+  static_assert(memcpyTypeRem() == 12); // both-error {{not an integral constant expression}} \
+                                        // both-note {{in call to}}
 }


        


More information about the cfe-commits mailing list