Skip to content

Commit 7510f32

Browse files
committed
[MachineSink] Fix crash due to use-after-free in a MachineInstr* cache.
After the SinkAndFold optimization was enabled, we saw some crashes with GISel due to SinkAndFold erasing an MI while a reference was being held in a cache.
1 parent 531233d commit 7510f32

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

llvm/lib/CodeGen/MachineSink.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,8 @@ bool MachineSinking::PerformSinkAndFold(MachineInstr &MI,
540540
}
541541
LLVM_DEBUG(dbgs() << "yielding"; New->dump());
542542
SinkDst->eraseFromParent();
543+
// Clear the StoreInstrCache, since we may have invalidated it by erasing.
544+
StoreInstrCache.clear();
543545
}
544546

545547
// Collect operands that need to be cleaned up because the registers no longer
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 3
2+
; RUN: llc < %s -mtriple=aarch64 -global-isel | FileCheck %s
3+
4+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
5+
target triple = "arm64-apple-macosx13.5.0"
6+
7+
; Check we don't crash here with a use-after-free.
8+
9+
define i32 @nsis_BZ2_bzDecompress(ptr %pos.i, i1 %cmp661.not3117.i, i1 %exitcond.not.i) {
10+
; CHECK-LABEL: nsis_BZ2_bzDecompress:
11+
; CHECK: // %bb.0: // %entry
12+
; CHECK-NEXT: b .LBB0_2
13+
; CHECK-NEXT: .LBB0_1: // %while.end671.i
14+
; CHECK-NEXT: // in Loop: Header=BB0_2 Depth=1
15+
; CHECK-NEXT: strb w8, [x0]
16+
; CHECK-NEXT: tbnz w2, #0, .LBB0_4
17+
; CHECK-NEXT: .LBB0_2: // %for.body653.i
18+
; CHECK-NEXT: // =>This Inner Loop Header: Depth=1
19+
; CHECK-NEXT: ldrb w8, [x0]
20+
; CHECK-NEXT: tbnz w1, #0, .LBB0_1
21+
; CHECK-NEXT: // %bb.3: // %while.body663.i
22+
; CHECK-NEXT: // in Loop: Header=BB0_2 Depth=1
23+
; CHECK-NEXT: mov x9, xzr
24+
; CHECK-NEXT: ldrb w9, [x9]
25+
; CHECK-NEXT: strb wzr, [x0, x9]
26+
; CHECK-NEXT: b .LBB0_1
27+
; CHECK-NEXT: .LBB0_4: // %for.end677.i
28+
; CHECK-NEXT: mov w0, wzr
29+
; CHECK-NEXT: ret
30+
entry:
31+
br label %for.body653.i
32+
33+
for.body653.i: ; preds = %while.end671.i, %entry
34+
%0 = load i8, ptr null, align 1
35+
%idxprom657.i2 = zext i8 %0 to i64
36+
%1 = load i8, ptr %pos.i, align 1
37+
br i1 %cmp661.not3117.i, label %while.end671.i, label %while.body663.i
38+
39+
while.body663.i: ; preds = %for.body653.i
40+
%arrayidx669.i = getelementptr [6 x i8], ptr %pos.i, i64 0, i64 %idxprom657.i2
41+
store i8 0, ptr %arrayidx669.i, align 1
42+
br label %while.end671.i
43+
44+
while.end671.i: ; preds = %while.body663.i, %for.body653.i
45+
store i8 %1, ptr %pos.i, align 1
46+
br i1 %exitcond.not.i, label %for.end677.i, label %for.body653.i
47+
48+
for.end677.i: ; preds = %while.end671.i
49+
ret i32 0
50+
}
51+

0 commit comments

Comments
 (0)