Author: Timm Baeder
Date: 2026-01-02T10:01:57+01:00
New Revision: ed6698e28ccb266d6a9079d39f6c042aab80379f

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

LOG: [clang][bytecode] Check builtin_memcpy() for non-block pointers (#174184)

This pretty hard to produce in C++ but easy in C.

Fixes #171609

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/InterpBuiltin.cpp
    clang/test/AST/ByteCode/builtins.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp 
b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 57d5f0ae6eed3..65101174247d1 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -64,6 +64,18 @@ static APSInt popToAPSInt(InterpState &S, QualType T) {
   return popToAPSInt(S.Stk, *S.getContext().classify(T));
 }
 
+/// Check for common reasons a pointer can't be read from, which
+/// are usually not diagnosed in a builtin function.
+static bool isReadable(const Pointer &P) {
+  if (P.isDummy())
+    return false;
+  if (!P.isBlockPointer())
+    return false;
+  if (!P.isLive())
+    return false;
+  return true;
+}
+
 /// Pushes \p Val on the stack as the type given by \p QT.
 static void pushInteger(InterpState &S, const APSInt &Val, QualType QT) {
   assert(QT->isSignedIntegerOrEnumerationType() ||
@@ -1794,8 +1806,7 @@ static bool interp__builtin_memcpy(InterpState &S, 
CodePtr OpPC,
     return false;
   }
 
-  // Can't read from dummy pointers.
-  if (DestPtr.isDummy() || SrcPtr.isDummy())
+  if (!isReadable(DestPtr) || !isReadable(SrcPtr))
     return false;
 
   if (DestPtr.getType()->isIncompleteType()) {

diff  --git a/clang/test/AST/ByteCode/builtins.c 
b/clang/test/AST/ByteCode/builtins.c
index a51260cd3431f..5be5455ab8813 100644
--- a/clang/test/AST/ByteCode/builtins.c
+++ b/clang/test/AST/ByteCode/builtins.c
@@ -17,3 +17,4 @@ int structStrlen(void) {
   return 1;
 }
 
+void f() { __builtin_memcpy(f, f, 1); }


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to