Skip to content

Commit 0503add

Browse files
committed
[CodeGen] Don't resolve the stack protector frame accesses until PEI
Currently, stack protector loads and stores are resolved during LocalStackSlotAllocation (if the pass needs to run). When this is the case, the base register assigned to the frame access is going to be one of the vregs created during LocalStackSlotAllocation. This means that we are keeping a pointer to the stack protector slot, and we're using this pointer to load and store to it. In case register pressure goes up, we may end up spilling this pointer to the stack, which can be a security concern. Instead, leave it to PEI to resolve the frame accesses. In order to do that, we make all stack protector accesses go through frame index operands, then PEI will resolve this using an offset from sp/fp/bp. Differential Revision: https://reviews.llvm.org/D64759 llvm-svn: 367068
1 parent 9d045a5 commit 0503add

File tree

5 files changed

+46
-26
lines changed

5 files changed

+46
-26
lines changed

llvm/lib/CodeGen/LocalStackSlotAllocation.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@ bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) {
351351
assert(MFI.isObjectPreAllocated(FrameIdx) &&
352352
"Only pre-allocated locals expected!");
353353

354+
// We need to keep the references to the stack protector slot through frame
355+
// index operands so that it gets resolved by PEI rather than this pass.
356+
// This avoids accesses to the stack protector though virtual base
357+
// registers, and forces PEI to address it using fp/sp/bp.
358+
if (MFI.hasStackProtectorIndex() &&
359+
FrameIdx == MFI.getStackProtectorIndex())
360+
continue;
361+
354362
LLVM_DEBUG(dbgs() << "Considering: " << MI);
355363

356364
unsigned idx = 0;

llvm/test/CodeGen/AArch64/stack-guard-reassign.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
; Verify that the offset assigned to the stack protector is at the top of the
44
; frame, covering the locals.
55
; CHECK-LABEL: fn:
6-
; CHECK: sub x8, x29, #24
7-
; CHECK-NEXT: adrp x9, __stack_chk_guard
8-
; CHECK-NEXT: ldr x9, [x9, :lo12:__stack_chk_guard]
9-
; CHECK-NEXT: str x9, [x8]
6+
; CHECK: adrp x8, __stack_chk_guard
7+
; CHECK-NEXT: ldr x8, [x8, :lo12:__stack_chk_guard]
8+
; CHECK-NEXT: stur x8, [x29, #-24]

llvm/test/CodeGen/ARM/stack-guard-reassign.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
; CHECK-LABEL: fn:
66
; CHECK: sub sp, sp, #32
77
; CHECK-NEXT: sub sp, sp, #65536
8+
; CHECK-NEXT: ldr r1, .LCPI0_0
9+
; CHECK-NEXT: ldr r2, [r1]
810
; CHECK-NEXT: add lr, sp, #65536
9-
; CHECK-NEXT: add r1, lr, #28
10-
; CHECK-NEXT: ldr r2, .LCPI0_0
11-
; CHECK-NEXT: ldr r3, [r2]
12-
; CHECK-NEXT: str r3, [r1]
11+
; CHECK-NEXT: str r2, [lr, #28]
1312
; CHECK: .LCPI0_0:
1413
; CHECK-NEXT: .long __stack_chk_guard

llvm/test/CodeGen/PowerPC/stack-guard-reassign.ll

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
; CHECK-NEXT: ori 0, 0, 65488
1010
; CHECK-NEXT: stwux 1, 1, 0
1111
; CHECK-NEXT: subf 0, 0, 1
12-
; CHECK-NEXT: lis 4, 1
13-
; CHECK-NEXT: ori 4, 4, 44
14-
; CHECK-NEXT: add 4, 1, 4
15-
; CHECK-NEXT: lis 5, __stack_chk_guard@ha
16-
; CHECK-NEXT: lwz 6, __stack_chk_guard@l(5)
17-
; CHECK-NEXT: stw 6, 0(4)
12+
; CHECK-NEXT: lis 4, __stack_chk_guard@ha
13+
; CHECK-NEXT: lwz 5, __stack_chk_guard@l(4)
14+
; CHECK-NEXT: lis 6, 1
15+
; CHECK-NEXT: ori 6, 6, 44
16+
; CHECK-NEXT: stwx 5, 1, 6

llvm/test/CodeGen/Thumb/stack_guard_remat.ll

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,40 @@
22
; RUN: llc < %s -mtriple=thumb-apple-darwin -relocation-model=static -no-integrated-as | FileCheck %s -check-prefix=NO-PIC -check-prefix=STATIC
33
; RUN: llc < %s -mtriple=thumb-apple-darwin -relocation-model=dynamic-no-pic -no-integrated-as | FileCheck %s -check-prefix=NO-PIC -check-prefix=DYNAMIC-NO-PIC
44

5-
;PIC: foo2
6-
;PIC: ldr [[R0:r[0-9]+]], [[LABEL0:LCPI[0-9_]+]]
7-
;PIC: [[LABEL1:LPC[0-9_]+]]:
8-
;PIC: add [[R0]], pc
9-
;PIC: ldr [[R1:r[0-9]+]], {{\[}}[[R0]]{{\]}}
10-
;PIC: ldr [[R1:r[0-9]+]], {{\[}}[[R1]]{{\]}}
11-
12-
;PIC: [[LABEL0]]:
5+
;PIC: foo2
6+
;PIC: ldr [[SAVED_GUARD:r[0-9]+]], [[GUARD_STACK_OFFSET:LCPI[0-9_]+]]
7+
;PIC-NEXT: add [[SAVED_GUARD]], sp
8+
;PIC-NEXT: ldr [[SAVED_GUARD]], {{\[}}[[SAVED_GUARD]]{{\]}}
9+
;PIC-NEXT: ldr [[ORIGINAL_GUARD:r[0-9]+]], [[ORIGINAL_GUARD_LABEL:LCPI[0-9_]+]]
10+
;PIC-NEXT: [[LABEL1:LPC[0-9_]+]]:
11+
;PIC-NEXT: add [[ORIGINAL_GUARD]], pc
12+
;PIC-NEXT: ldr [[ORIGINAL_GUARD]], {{\[}}[[ORIGINAL_GUARD]]{{\]}}
13+
;PIC-NEXT: ldr [[ORIGINAL_GUARD]], {{\[}}[[ORIGINAL_GUARD]]{{\]}}
14+
;PIC-NEXT: subs {{r[0-9]+}}, [[ORIGINAL_GUARD]], [[SAVED_GUARD]]
15+
16+
;PIC: [[GUARD_STACK_OFFSET]]:
17+
;PIC-NEXT: .long 1028
18+
;PIC: [[ORIGINAL_GUARD_LABEL]]:
1319
;PIC-NEXT: .long L___stack_chk_guard$non_lazy_ptr-([[LABEL1]]+4)
1420

1521
;NO-PIC: foo2
16-
;NO-PIC: ldr [[R0:r[0-9]+]], [[LABEL0:LCPI[0-9_]+]]
22+
;NO-PIC: ldr [[SAVED_GUARD:r[0-9]+]], [[GUARD_STACK_OFFSET:LCPI[0-9_]+]]
23+
;NO-PIC-NEXT: add [[SAVED_GUARD]], sp
24+
;NO-PIC-NEXT: ldr [[SAVED_GUARD]], {{\[}}[[SAVED_GUARD]]{{\]}}
25+
;NO-PIC-NEXT: ldr [[ORIGINAL_GUARD:r[0-9]+]], [[ORIGINAL_GUARD_LABEL:LCPI[0-9_]+]]
1726
;NO-PIC-NOT: LPC
18-
;NO-PIC: ldr {{r[0-9]+}}, {{\[}}[[R0]]{{\]}}
27+
;NO-PIC-NEXT: ldr [[ORIGINAL_GUARD]], {{\[}}[[ORIGINAL_GUARD]]{{\]}}
28+
;DYNAMIC-NO-PIC-NEXT: ldr [[ORIGINAL_GUARD]], {{\[}}[[ORIGINAL_GUARD]]{{\]}}
29+
;NO-PIC-NEXT: subs {{r[0-9]+}}, [[ORIGINAL_GUARD]], [[SAVED_GUARD]]
1930

20-
;STATIC: [[LABEL0]]:
31+
;STATIC: [[GUARD_STACK_OFFSET]]:
32+
;STATIC-NEXT: .long 1028
33+
;STATIC: [[ORIGINAL_GUARD_LABEL]]:
2134
;STATIC-NEXT: .long ___stack_chk_guard
2235

23-
;DYNAMIC-NO-PIC: [[LABEL0]]:
36+
;DYNAMIC-NO-PIC: [[GUARD_STACK_OFFSET]]:
37+
;DYNAMIC-NO-PIC-NEXT: .long 1028
38+
;DYNAMIC-NO-PIC: [[ORIGINAL_GUARD_LABEL]]:
2439
;DYNAMIC-NO-PIC-NEXT: .long L___stack_chk_guard$non_lazy_ptr
2540

2641
; Function Attrs: nounwind ssp

0 commit comments

Comments
 (0)