[clang] 5a8c4b5 - [clang][Interp] Don't allow reading from non-const blocks when returning
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 29 12:45:43 PDT 2024
Author: Timm Bäder
Date: 2024-06-29T21:10:25+02:00
New Revision: 5a8c4b597beed38e392f221042d29f475a3d1626
URL: https://github.com/llvm/llvm-project/commit/5a8c4b597beed38e392f221042d29f475a3d1626
DIFF: https://github.com/llvm/llvm-project/commit/5a8c4b597beed38e392f221042d29f475a3d1626.diff
LOG: [clang][Interp] Don't allow reading from non-const blocks when returning
Added:
Modified:
clang/lib/AST/Interp/EvalEmitter.cpp
clang/test/AST/Interp/const-temporaries.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/EvalEmitter.cpp b/clang/lib/AST/Interp/EvalEmitter.cpp
index f4854adba9348..d17151416b44b 100644
--- a/clang/lib/AST/Interp/EvalEmitter.cpp
+++ b/clang/lib/AST/Interp/EvalEmitter.cpp
@@ -151,6 +151,11 @@ template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
// Implicitly convert lvalue to rvalue, if requested.
if (ConvertResultToRValue) {
+ // Never allow reading from a non-const pointer, unless the memory
+ // has been created in this evaluation.
+ if (!Ptr.isConst() && Ptr.block()->getEvalID() != Ctx.getEvalID())
+ return false;
+
if (std::optional<APValue> V = Ptr.toRValue(Ctx)) {
EvalResult.setValue(*V);
} else {
diff --git a/clang/test/AST/Interp/const-temporaries.cpp b/clang/test/AST/Interp/const-temporaries.cpp
index bbb95b3c3dff7..1dc16ef63a846 100644
--- a/clang/test/AST/Interp/const-temporaries.cpp
+++ b/clang/test/AST/Interp/const-temporaries.cpp
@@ -90,3 +90,12 @@ struct R { mutable long x; };
struct Z2 { const R &x, y; };
Z2 z2 = { R{1}, z2.x.x = 10 };
+// CHECK: __cxa_atexit({{.*}} @_ZN1BD1Ev, {{.*}} @b
+
+// CHECK: define
+// CHECK-NOT: @_ZGRN21ModifyStaticTemporary1cE_
+// CHECK: store {{.*}} @_ZGRN21ModifyStaticTemporary1cE_, {{.*}} @_ZN21ModifyStaticTemporary1cE
+// CHECK: add
+// CHECK: store
+// CHECK: load {{.*}} @_ZN21ModifyStaticTemporary1bE
+// CHECK: store {{.*}} @_ZN21ModifyStaticTemporary1cE
More information about the cfe-commits
mailing list