Skip to content
Snippets Groups Projects
Commit aa97726f authored by Dimitry Andric's avatar Dimitry Andric Committed by Tom Stellard
Browse files

[SCCP] Avoid modifying AdditionalUsers while iterating over it

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

(cherry picked from commit 6abb92f2)
parent b89942c3
No related branches found
No related tags found
No related merge requests found
...@@ -542,9 +542,14 @@ private: ...@@ -542,9 +542,14 @@ private:
auto Iter = AdditionalUsers.find(I); auto Iter = AdditionalUsers.find(I);
if (Iter != AdditionalUsers.end()) { 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) for (User *U : Iter->second)
if (auto *UI = dyn_cast<Instruction>(U)) if (auto *UI = dyn_cast<Instruction>(U))
OperandChangedState(UI); ToNotify.push_back(UI);
for (Instruction *UI : ToNotify)
OperandChangedState(UI);
} }
} }
void handleCallOverdefined(CallBase &CB); void handleCallOverdefined(CallBase &CB);
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment