[clang] 3f39c5d - [clang][bytecode] Reject memcpy dummy pointers after null check (#118460)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 3 02:51:52 PST 2024


Author: Timm Baeder
Date: 2024-12-03T11:51:49+01:00
New Revision: 3f39c5df08d4ca1e7f852908e9fb255db24538da

URL: https://github.com/llvm/llvm-project/commit/3f39c5df08d4ca1e7f852908e9fb255db24538da
DIFF: https://github.com/llvm/llvm-project/commit/3f39c5df08d4ca1e7f852908e9fb255db24538da.diff

LOG: [clang][bytecode] Reject memcpy dummy pointers after null check (#118460)

To match the diagnostic output of the current interpreter.

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 c5473322ecb280..b788656f9484fc 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1813,9 +1813,6 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
 
   bool Move = (ID == Builtin::BI__builtin_memmove || ID == Builtin::BImemmove);
 
-  if (DestPtr.isDummy() || SrcPtr.isDummy())
-    return false;
-
   // If the size is zero, we treat this as always being a valid no-op.
   if (Size.isZero()) {
     S.Stk.push<Pointer>(DestPtr);
@@ -1830,6 +1827,10 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
     return false;
   }
 
+  // As a last resort, reject dummy pointers.
+  if (DestPtr.isDummy() || SrcPtr.isDummy())
+    return false;
+
   if (!DoBitCastPtr(S, OpPC, SrcPtr, DestPtr))
     return false;
 

diff  --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 211ca6e164cbfb..b951c04dde5980 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1169,6 +1169,10 @@ namespace BuiltinMemcpy {
   static_assert(__builtin_memcpy(null_incomplete, null_incomplete, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
                                                                                       // both-note {{source of 'memcpy' is nullptr}}
 
+  wchar_t global;
+  constexpr wchar_t *null = 0;
+  static_assert(__builtin_memcpy(&global, null, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \
+                                                                   // both-note {{source of 'memcpy' is nullptr}}
 
   constexpr int simpleMove() {
     int a = 12;


        


More information about the cfe-commits mailing list