[clang] [clang][bytecode] Fail on reads from constexpr-unknown pointers (PR #164996)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 24 08:10:28 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/164996
If they aren't const.
Fixes https://github.com/llvm/llvm-project/issues/164985
>From d551ffdafa589f3c50d8824b371990a97afd4b45 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 24 Oct 2025 17:08:56 +0200
Subject: [PATCH] [clang][bytecode] Fail on reads from constexpr-unknown
pointers
If they aren't const.
Fixes https://github.com/llvm/llvm-project/issues/164985
---
clang/lib/AST/ByteCode/Interp.cpp | 2 ++
clang/test/AST/ByteCode/codegen-cxx20.cpp | 15 +++++++++++++++
2 files changed, 17 insertions(+)
create mode 100644 clang/test/AST/ByteCode/codegen-cxx20.cpp
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()
More information about the cfe-commits
mailing list