[PATCH] D154795: [clang][Interp] Check pointers for live-ness when returning them
Timm Bäder via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 26 00:52:30 PDT 2023
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGc7251385c85d: [clang][Interp] Check pointers for live-ness when returning them (authored by tbaeder).
Changed prior to commit:
https://reviews.llvm.org/D154795?vs=538442&id=544244#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D154795/new/
https://reviews.llvm.org/D154795
Files:
clang/lib/AST/Interp/Interp.h
clang/test/AST/Interp/functions.cpp
Index: clang/test/AST/Interp/functions.cpp
===================================================================
--- clang/test/AST/Interp/functions.cpp
+++ clang/test/AST/Interp/functions.cpp
@@ -265,3 +265,29 @@
g(0);
}
}
+
+namespace ReturnLocalPtr {
+ constexpr int *p() {
+ int a = 12;
+ return &a; // ref-warning {{address of stack memory}} \
+ // expected-warning {{address of stack memory}}
+ }
+
+ /// GCC rejects the expression below, just like the new interpreter. The current interpreter
+ /// however accepts it and only warns about the function above returning an address to stack
+ /// memory. If we change the condition to 'p() != nullptr', it even succeeds.
+ static_assert(p() == nullptr, ""); // ref-error {{static assertion failed}} \
+ // expected-error {{not an integral constant expression}}
+
+ /// FIXME: The current interpreter emits diagnostics in the reference case below, but the
+ /// new one does not.
+ constexpr const int &p2() {
+ int a = 12; // ref-note {{declared here}}
+ return a; // ref-warning {{reference to stack memory associated with local variable}} \
+ // expected-warning {{reference to stack memory associated with local variable}}
+ }
+
+ static_assert(p2() == 12, ""); // ref-error {{not an integral constant expression}} \
+ // ref-note {{read of variable whose lifetime has ended}} \
+ // expected-error {{not an integral constant expression}}
+}
Index: clang/lib/AST/Interp/Interp.h
===================================================================
--- clang/lib/AST/Interp/Interp.h
+++ clang/lib/AST/Interp/Interp.h
@@ -181,6 +181,17 @@
bool Ret(InterpState &S, CodePtr &PC, APValue &Result) {
const T &Ret = S.Stk.pop<T>();
+ // Make sure returned pointers are live. We might be trying to return a
+ // pointer or reference to a local variable.
+ // Just return false, since a diagnostic has already been emitted in Sema.
+ if constexpr (std::is_same_v<T, Pointer>) {
+ // FIXME: We could be calling isLive() here, but the emitted diagnostics
+ // seem a little weird, at least if the returned expression is of
+ // pointer type.
+ if (!Ret.isLive())
+ return false;
+ }
+
assert(S.Current->getFrameOffset() == S.Stk.size() && "Invalid frame");
if (!S.checkingPotentialConstantExpression() || S.Current->Caller)
S.Current->popArgs();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154795.544244.patch
Type: text/x-patch
Size: 2487 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230726/ec433eb4/attachment.bin>
More information about the cfe-commits
mailing list