Skip to content

Commit a2be1b8

Browse files
authored
[msan] Don't modify CFG iterating it (#90691)
In rare cases `SplitBlockAndInsertSimpleForLoop` in `paintOrigin` crashes outsize iterators. Somehow existing `SplitBlockAndInsertIfThen` do not invalidate iterators.
1 parent 0f8d97c commit a2be1b8

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
11351135
std::unique_ptr<VarArgHelper> VAHelper;
11361136
const TargetLibraryInfo *TLI;
11371137
Instruction *FnPrologueEnd;
1138+
SmallVector<Instruction *, 16> Instructions;
11381139

11391140
// The following flags disable parts of MSan instrumentation based on
11401141
// exclusion list contents and command-line options.
@@ -1520,6 +1521,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
15201521
for (BasicBlock *BB : depth_first(FnPrologueEnd->getParent()))
15211522
visit(*BB);
15221523

1524+
// `visit` above only collects instructions. Process them after iterating
1525+
// CFG to avoid requirement on CFG transformations.
1526+
for (Instruction *I : Instructions)
1527+
InstVisitor<MemorySanitizerVisitor>::visit(*I);
1528+
15231529
// Finalize PHI nodes.
15241530
for (PHINode *PN : ShadowPHINodes) {
15251531
PHINode *PNS = cast<PHINode>(getShadow(PN));
@@ -2196,7 +2202,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
21962202
setOrigin(&I, getCleanOrigin());
21972203
return;
21982204
}
2199-
InstVisitor<MemorySanitizerVisitor>::visit(I);
2205+
2206+
Instructions.push_back(&I);
22002207
}
22012208

22022209
/// Instrument LoadInst

0 commit comments

Comments
 (0)