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

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 24 08:11:11 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

If they aren't const.

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

---
Full diff: https://github.com/llvm/llvm-project/pull/164996.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Interp.cpp (+2) 
- (added) clang/test/AST/ByteCode/codegen-cxx20.cpp (+15) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index a72282caf5e73..5d89f32d6bdc2 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()

``````````

</details>


https://github.com/llvm/llvm-project/pull/164996


More information about the cfe-commits mailing list