Skip to content

Commit 6abb92f

Browse files
committed
[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
1 parent 55978f9 commit 6abb92f

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
@@ -533,9 +533,14 @@ class SCCPSolver : public InstVisitor<SCCPSolver> {
533533

534534
auto Iter = AdditionalUsers.find(I);
535535
if (Iter != AdditionalUsers.end()) {
536+
// Copy additional users before notifying them of changes, because new
537+
// users may be added, potentially invalidating the iterator.
538+
SmallVector<Instruction *, 2> ToNotify;
536539
for (User *U : Iter->second)
537540
if (auto *UI = dyn_cast<Instruction>(U))
538-
OperandChangedState(UI);
541+
ToNotify.push_back(UI);
542+
for (Instruction *UI : ToNotify)
543+
OperandChangedState(UI);
539544
}
540545
}
541546
void handleCallOverdefined(CallBase &CB);

0 commit comments

Comments
 (0)