[clang] [clang][bytecode] Check builtin_memcpy() for non-block pointers (PR #174184)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 2 00:11:53 PST 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/174184
This pretty hard to produce in C++ but easy in C.
Fixes #171609
>From cecb72441ee0e2886deb8f058d7a59f770430fb1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 2 Jan 2026 09:10:48 +0100
Subject: [PATCH] [clang][bytecode] Check builtin_memcpy() for non-block
pointers
This pretty hard to produce in C++ but easy in C.
Fixes #171609
---
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 15 +++++++++++++--
clang/test/AST/ByteCode/builtins.c | 1 +
2 files changed, 14 insertions(+), 2 deletions(-)
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); }
More information about the cfe-commits
mailing list