Skip to content

Commit 36b3c43

Browse files
authored
[AArch64] PAUTH_PROLOGUE should not be duplicated with PAuthLR (#124775)
When using PAuthLR, the PAUTH_PROLOGUE expands into a sequence of instructions which takes the address of one of those instructions, and uses that address to compute the return address signature. If this is duplicated, there will be two different addresses used in calculating the signature, so the epilogue will only be correct for (at most) one of them. This change also restricts code generation when using v8.3-A return address signing, without PAuthLR. This isn't strictly needed, as duplicating the prologue there would be valid. We could fix this by having two copies of PAUTH_PROLOGUE, with and without isNotDuplicable, but I don't think it's worth adding the extra complexity to a security feature for that.
1 parent 5e43418 commit 36b3c43

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

llvm/lib/Target/AArch64/AArch64InstrInfo.td

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,12 @@ def : InstAlias<"xpaclri", (XPACLRI), 0>;
17731773

17741774
let Uses = [LR, SP], Defs = [LR] in {
17751775
// Insertion point of LR signing code.
1776-
def PAUTH_PROLOGUE : Pseudo<(outs), (ins), []>, Sched<[]>;
1776+
def PAUTH_PROLOGUE : Pseudo<(outs), (ins), []>, Sched<[]> {
1777+
// When using PAuthLR, the address of one of the instructions this expands
1778+
// into is used as an input to the signature calculation, so this must not be
1779+
// duplicated.
1780+
let isNotDuplicable = 1;
1781+
}
17771782
// Insertion point of LR authentication code.
17781783
// The RET terminator of the containing machine basic block may be replaced
17791784
// with a combined RETA(A|B) instruction when rewriting this Pseudo.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -mtriple aarch64-none-elf -run-pass=block-placement -O3 -o - %s | FileCheck %s
3+
4+
## Check that block-placement does not perform tail duplication on the
5+
## PAUTH_EPILOGUE instruction. If that happened, the two prologues would use
6+
## different addresses while calculating the return address signature, so the
7+
## epilogue could only be correct for (at most) one of them.
8+
9+
--- |
10+
define void @test() "frame-pointer"="non-leaf" {
11+
entry:
12+
ret void
13+
}
14+
15+
declare void @f()
16+
...
17+
---
18+
name: test
19+
body: |
20+
; CHECK-LABEL: name: test
21+
; CHECK: bb.0.entry:
22+
; CHECK-NEXT: successors: %bb.1(0x30000000), %bb.2(0x50000000)
23+
; CHECK-NEXT: liveins: $w0, $w1, $lr
24+
; CHECK-NEXT: {{ $}}
25+
; CHECK-NEXT: CBZW renamable $w0, %bb.1
26+
; CHECK-NEXT: {{ $}}
27+
; CHECK-NEXT: bb.2:
28+
; CHECK-NEXT: successors: %bb.3(0x80000000)
29+
; CHECK-NEXT: liveins: $w0, $w1, $lr
30+
; CHECK-NEXT: {{ $}}
31+
; CHECK-NEXT: B %bb.3
32+
; CHECK-NEXT: {{ $}}
33+
; CHECK-NEXT: bb.1:
34+
; CHECK-NEXT: successors: %bb.3(0x80000000)
35+
; CHECK-NEXT: liveins: $w1, $lr
36+
; CHECK-NEXT: {{ $}}
37+
; CHECK-NEXT: renamable $w8 = MOVZWi 1, 0
38+
; CHECK-NEXT: {{ $}}
39+
; CHECK-NEXT: bb.3:
40+
; CHECK-NEXT: successors: %bb.5(0x30000000), %bb.4(0x50000000)
41+
; CHECK-NEXT: liveins: $w1, $w8, $lr
42+
; CHECK-NEXT: {{ $}}
43+
; CHECK-NEXT: frame-setup PAUTH_PROLOGUE implicit-def $lr, implicit killed $lr, implicit $sp
44+
; CHECK-NEXT: CBZW killed renamable $w1, %bb.5
45+
; CHECK-NEXT: {{ $}}
46+
; CHECK-NEXT: bb.4:
47+
; CHECK-NEXT: successors: %bb.5(0x80000000)
48+
; CHECK-NEXT: {{ $}}
49+
; CHECK-NEXT: BL @f, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit-def $sp
50+
; CHECK-NEXT: {{ $}}
51+
; CHECK-NEXT: bb.5:
52+
; CHECK-NEXT: BL @f, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit-def $sp
53+
; CHECK-NEXT: frame-destroy PAUTH_EPILOGUE implicit-def $lr, implicit killed $lr, implicit $sp
54+
; CHECK-NEXT: TCRETURNdi @f, 0, csr_aarch64_aapcs, implicit $sp
55+
bb.0.entry:
56+
successors: %bb.1(0x30000000), %bb.2(0x50000000)
57+
liveins: $w0, $w1, $lr
58+
59+
CBNZW renamable $w0, %bb.2
60+
61+
bb.1:
62+
successors: %bb.3(0x80000000)
63+
liveins: $w1, $lr
64+
65+
renamable $w8 = MOVZWi 1, 0
66+
B %bb.3
67+
68+
bb.2:
69+
successors: %bb.3(0x80000000)
70+
liveins: $w0, $w1, $lr
71+
72+
bb.3:
73+
successors: %bb.5(0x30000000), %bb.4(0x50000000)
74+
liveins: $w1, $w8, $lr
75+
76+
frame-setup PAUTH_PROLOGUE implicit-def $lr, implicit killed $lr, implicit $sp
77+
CBZW killed renamable $w1, %bb.5
78+
79+
bb.4:
80+
successors: %bb.5(0x80000000)
81+
82+
BL @f, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit-def $sp
83+
84+
bb.5:
85+
BL @f, csr_aarch64_aapcs, implicit-def dead $lr, implicit $sp, implicit-def $sp
86+
frame-destroy PAUTH_EPILOGUE implicit-def $lr, implicit killed $lr, implicit $sp
87+
TCRETURNdi @f, 0, csr_aarch64_aapcs, implicit $sp
88+
...

0 commit comments

Comments
 (0)