Skip to content

Commit d95f7c9

Browse files
authored
[RISCV] Use the thread local stack protector for Android targets (llvm#87672)
Android supports per thread stack protectors that are individually managed and initialized, which can provide stronger protections than using the global stack protector cookie. This patch matches the convention for other architectures targeting Android platforms.
1 parent 2d511cd commit d95f7c9

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21052,6 +21052,12 @@ Value *RISCVTargetLowering::getIRStackGuard(IRBuilderBase &IRB) const {
2105221052
if (Subtarget.isTargetFuchsia())
2105321053
return useTpOffset(IRB, -0x10);
2105421054

21055+
// Android provides a fixed TLS slot for the stack cookie. See the definition
21056+
// of TLS_SLOT_STACK_GUARD in
21057+
// https://android.googlesource.com/platform/bionic/+/main/libc/platform/bionic/tls_defines.h
21058+
if (Subtarget.isTargetAndroid())
21059+
return useTpOffset(IRB, -0x18);
21060+
2105521061
return TargetLowering::getIRStackGuard(IRB);
2105621062
}
2105721063

llvm/test/CodeGen/RISCV/stack-protector-target.ll

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,18 @@ define void @func() sspreq nounwind {
5050
; ANDROID-RISCV64: # %bb.0:
5151
; ANDROID-RISCV64-NEXT: addi sp, sp, -32
5252
; ANDROID-RISCV64-NEXT: sd ra, 24(sp) # 8-byte Folded Spill
53-
; ANDROID-RISCV64-NEXT: sd s0, 16(sp) # 8-byte Folded Spill
54-
; ANDROID-RISCV64-NEXT: lui s0, %hi(__stack_chk_guard)
55-
; ANDROID-RISCV64-NEXT: ld a0, %lo(__stack_chk_guard)(s0)
56-
; ANDROID-RISCV64-NEXT: sd a0, 8(sp)
57-
; ANDROID-RISCV64-NEXT: addi a0, sp, 4
53+
; ANDROID-RISCV64-NEXT: ld a0, -24(tp)
54+
; ANDROID-RISCV64-NEXT: sd a0, 16(sp)
55+
; ANDROID-RISCV64-NEXT: addi a0, sp, 12
5856
; ANDROID-RISCV64-NEXT: call capture
59-
; ANDROID-RISCV64-NEXT: ld a0, %lo(__stack_chk_guard)(s0)
60-
; ANDROID-RISCV64-NEXT: ld a1, 8(sp)
57+
; ANDROID-RISCV64-NEXT: ld a0, -24(tp)
58+
; ANDROID-RISCV64-NEXT: ld a1, 16(sp)
6159
; ANDROID-RISCV64-NEXT: bne a0, a1, .LBB0_2
62-
; ANDROID-RISCV64-NEXT: # %bb.1:
60+
; ANDROID-RISCV64-NEXT: # %bb.1: # %SP_return
6361
; ANDROID-RISCV64-NEXT: ld ra, 24(sp) # 8-byte Folded Reload
64-
; ANDROID-RISCV64-NEXT: ld s0, 16(sp) # 8-byte Folded Reload
6562
; ANDROID-RISCV64-NEXT: addi sp, sp, 32
6663
; ANDROID-RISCV64-NEXT: ret
67-
; ANDROID-RISCV64-NEXT: .LBB0_2:
64+
; ANDROID-RISCV64-NEXT: .LBB0_2: # %CallStackCheckFailBlk
6865
; ANDROID-RISCV64-NEXT: call __stack_chk_fail
6966
%1 = alloca i32, align 4
7067
call void @capture(ptr %1)

0 commit comments

Comments
 (0)