[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
Sun Jul 9 11:11:43 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, shafik, cor3ntin.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

  We might be trying to return a pointer or reference to a local variable,
  which doesn't work.


Repository:
  rG LLVM Github Monorepo

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
@@ -204,6 +204,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 expresion 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.538442.patch
Type: text/x-patch
Size: 2486 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230709/f79b3e2d/attachment.bin>


More information about the cfe-commits mailing list