Skip to content

Commit 403e0e8

Browse files
authored
[clang][Interp] Fix crash during InterpStack printing (#68246)
`InterpStack` is using an `std::vector<>` to track the `ItemTypes`. As a result, the new types are inserted to the back of the `std::vector<>`, however `dump()` was reading the types from the front (the bottom of the stack) and printing the value on the top of the stack. This lead to a crash if the type on the bottom had a different type from the type on the top. E.g.: ``` Items: 2. Size: 40 0/8: 0 1/40: 0x5590cddc0460 {16, 16, 32} ``` The same method also miscalculated the offsets during printing the stack, which was a source of incorrect stack dumps and future crashes. This patch changes the order of iteration of the types and fixes the offset calculation. As for testing the change, the issue is that it needs to be done as a unittest, however from `clang/unittests` we don't have access to `clang/lib`, where `Interp` resides. Although the previous implementation didn't have unittests either, so I'm not sure if we actually care that much or not.
1 parent 2dd52b4 commit 403e0e8

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

clang/lib/AST/Interp/InterpStack.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,25 @@ void InterpStack::shrink(size_t Size) {
8686

8787
void InterpStack::dump() const {
8888
#ifndef NDEBUG
89-
llvm::errs() << "Items: " << ItemTypes.size() << ". Size: " << size() << "\n";
89+
llvm::errs() << "Items: " << ItemTypes.size() << ". Size: " << size() << '\n';
9090
if (ItemTypes.empty())
9191
return;
9292

9393
size_t Index = 0;
94-
size_t Offset = align(primSize(ItemTypes[0]));
95-
for (PrimType Ty : ItemTypes) {
96-
llvm::errs() << Index << "/" << Offset << ": ";
97-
TYPE_SWITCH(Ty, {
94+
size_t Offset = 0;
95+
96+
// The type of the item on the top of the stack is inserted to the back
97+
// of the vector, so the iteration has to happen backwards.
98+
for (auto TyIt = ItemTypes.rbegin(); TyIt != ItemTypes.rend(); ++TyIt) {
99+
Offset += align(primSize(*TyIt));
100+
101+
llvm::errs() << Index << '/' << Offset << ": ";
102+
TYPE_SWITCH(*TyIt, {
98103
const T &V = peek<T>(Offset);
99104
llvm::errs() << V;
100105
});
101-
llvm::errs() << "\n";
102-
Offset += align(primSize(Ty));
106+
llvm::errs() << '\n';
107+
103108
++Index;
104109
}
105110
#endif

0 commit comments

Comments
 (0)