Skip to content

Commit

Permalink
[clang][Interp] Clear pointers pointing to dead blocks
Browse files Browse the repository at this point in the history
before free()ing the dead blocks. Otherwise, we might end up with
dangling Pointers to those dead blocks.
  • Loading branch information
tbaederr committed Jul 13, 2024
1 parent 80e61e3 commit b22adf0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion clang/lib/AST/Interp/InterpState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ InterpState::~InterpState() {
}
}

void InterpState::cleanup() {}
void InterpState::cleanup() {
// As a last resort, make sure all pointers still pointing to a dead block
// don't point to it anymore.
for (DeadBlock *DB = DeadBlocks; DB; DB = DB->Next) {
for (Pointer *P = DB->B.Pointers; P; P = P->Next) {
P->PointeeStorage.BS.Pointee = nullptr;
}
}
}

Frame *InterpState::getCurrentFrame() {
if (Current && Current->Caller)
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/Interp/Pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ class Pointer {
friend class Block;
friend class DeadBlock;
friend class MemberPointer;
friend class InterpState;
friend struct InitMap;

Pointer(Block *Pointee, unsigned Base, uint64_t Offset);
Expand Down
13 changes: 13 additions & 0 deletions clang/test/AST/Interp/lifetimes.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s
// RUN: %clang_cc1 -verify=ref,both %s

/// FIXME: Slight difference in diagnostic output here.

struct Foo {
int a;
};
Expand All @@ -20,3 +22,14 @@ static_assert(dead1() == 1, ""); // both-error {{not an integral constant expres
// both-note {{in call to}}


struct S {
int &&r; // both-note {{reference member declared here}}
int t;
constexpr S() : r(0), t(r) {} // both-error {{reference member 'r' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}} \
// ref-note {{read of object outside its lifetime is not allowed in a constant expression}} \
// expected-note {{temporary created here}} \
// expected-note {{read of temporary whose lifetime has ended}}
};
constexpr int k1 = S().t; // both-error {{must be initialized by a constant expression}} \
// ref-note {{in call to}} \
// expected-note {{in call to}}

0 comments on commit b22adf0

Please sign in to comment.