Skip to content

Commit

Permalink
[SCCP] Avoid modifying AdditionalUsers while iterating over it
Browse files Browse the repository at this point in the history
When run under valgrind, or with a malloc that poisons freed memory,
this can lead to segfaults or other problems.

To avoid modifying the AdditionalUsers DenseMap while still iterating,
save the instructions to be notified in a separate SmallPtrSet, and use
this to later call OperandChangedState on each instruction.

Fixes PR49582.

Reviewed By: fhahn

Differential Revision: https://reviews.llvm.org/D98602
  • Loading branch information
DimitryAndric committed Apr 2, 2021
1 parent 55978f9 commit 6abb92f
Show file tree
Hide file tree
Showing 2 changed files with 860 additions and 1 deletion.
7 changes: 6 additions & 1 deletion llvm/lib/Transforms/Scalar/SCCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,14 @@ class SCCPSolver : public InstVisitor<SCCPSolver> {

auto Iter = AdditionalUsers.find(I);
if (Iter != AdditionalUsers.end()) {
// Copy additional users before notifying them of changes, because new
// users may be added, potentially invalidating the iterator.
SmallVector<Instruction *, 2> ToNotify;
for (User *U : Iter->second)
if (auto *UI = dyn_cast<Instruction>(U))
OperandChangedState(UI);
ToNotify.push_back(UI);
for (Instruction *UI : ToNotify)
OperandChangedState(UI);
}
}
void handleCallOverdefined(CallBase &CB);
Expand Down
Loading

0 comments on commit 6abb92f

Please sign in to comment.