[clang] bc37018 - [clang][bytecode] Fail on reads from constexpr-unknown pointers (#164996)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 27 01:38:28 PDT 2025


Author: Timm Baeder
Date: 2025-10-27T09:38:23+01:00
New Revision: bc37018a0bcd67d5fd8bfade51ecfd709498c45a

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

LOG: [clang][bytecode] Fail on reads from constexpr-unknown pointers (#164996)

If they aren't const.

Fixes https://github.com/llvm/llvm-project/issues/164985

Added: 
    clang/test/AST/ByteCode/codegen-cxx20.cpp

Modified: 
    clang/lib/AST/ByteCode/Interp.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 169a9a2c7b811..910868b27f48e 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -832,6 +832,8 @@ bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
     return false;
   if (!CheckVolatile(S, OpPC, Ptr, AK))
     return false;
+  if (!Ptr.isConst() && !S.inConstantContext() && isConstexprUnknown(Ptr))
+    return false;
   return true;
 }
 

diff  --git a/clang/test/AST/ByteCode/codegen-cxx20.cpp b/clang/test/AST/ByteCode/codegen-cxx20.cpp
new file mode 100644
index 0000000000000..c1ef629da1e88
--- /dev/null
+++ b/clang/test/AST/ByteCode/codegen-cxx20.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s -fcxx-exceptions                                         | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s -fcxx-exceptions -fexperimental-new-constant-interpreter | FileCheck %s
+
+
+/// The read from a used to succeed, causing the entire if statement to vanish.
+extern void e();
+int somefunc() {
+  auto foo = [a = false]() mutable {
+    if (a)
+      e();
+  };
+  foo();
+}
+
+// CHECK: call void @_Z1ev()


        


More information about the cfe-commits mailing list