Skip to content

Commit aa97726

Browse files
DimitryAndrictstellar
authored andcommitted
[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 6abb92f)
1 parent b89942c commit aa97726

File tree

2 files changed

+860
-1
lines changed

2 files changed

+860
-1
lines changed

llvm/lib/Transforms/Scalar/SCCP.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,14 @@ class SCCPSolver : public InstVisitor<SCCPSolver> {
542542

543543
auto Iter = AdditionalUsers.find(I);
544544
if (Iter != AdditionalUsers.end()) {
545+
// Copy additional users before notifying them of changes, because new
546+
// users may be added, potentially invalidating the iterator.
547+
SmallVector<Instruction *, 2> ToNotify;
545548
for (User *U : Iter->second)
546549
if (auto *UI = dyn_cast<Instruction>(U))
547-
OperandChangedState(UI);
550+
ToNotify.push_back(UI);
551+
for (Instruction *UI : ToNotify)
552+
OperandChangedState(UI);
548553
}
549554
}
550555
void handleCallOverdefined(CallBase &CB);

0 commit comments

Comments
 (0)