Skip to content

[RISCV] Use the thread local stack protector for Android targets #87672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21022,6 +21022,12 @@ Value *RISCVTargetLowering::getIRStackGuard(IRBuilderBase &IRB) const {
if (Subtarget.isTargetFuchsia())
return useTpOffset(IRB, -0x10);

// Android provides a fixed TLS slot for the stack cookie. See the definition
// of TLS_SLOT_STACK_GUARD in
// https://android.googlesource.com/platform/bionic/+/main/libc/platform/bionic/tls_defines.h
if (Subtarget.isTargetAndroid())
return useTpOffset(IRB, -0x18);

return TargetLowering::getIRStackGuard(IRB);
}

Expand Down
17 changes: 7 additions & 10 deletions llvm/test/CodeGen/RISCV/stack-protector-target.ll
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,18 @@ define void @func() sspreq nounwind {
; ANDROID-RISCV64: # %bb.0:
; ANDROID-RISCV64-NEXT: addi sp, sp, -32
; ANDROID-RISCV64-NEXT: sd ra, 24(sp) # 8-byte Folded Spill
; ANDROID-RISCV64-NEXT: sd s0, 16(sp) # 8-byte Folded Spill
; ANDROID-RISCV64-NEXT: lui s0, %hi(__stack_chk_guard)
; ANDROID-RISCV64-NEXT: ld a0, %lo(__stack_chk_guard)(s0)
; ANDROID-RISCV64-NEXT: sd a0, 8(sp)
; ANDROID-RISCV64-NEXT: addi a0, sp, 4
; ANDROID-RISCV64-NEXT: ld a0, -24(tp)
; ANDROID-RISCV64-NEXT: sd a0, 16(sp)
; ANDROID-RISCV64-NEXT: addi a0, sp, 12
; ANDROID-RISCV64-NEXT: call capture
; ANDROID-RISCV64-NEXT: ld a0, %lo(__stack_chk_guard)(s0)
; ANDROID-RISCV64-NEXT: ld a1, 8(sp)
; ANDROID-RISCV64-NEXT: ld a0, -24(tp)
; ANDROID-RISCV64-NEXT: ld a1, 16(sp)
; ANDROID-RISCV64-NEXT: bne a0, a1, .LBB0_2
; ANDROID-RISCV64-NEXT: # %bb.1:
; ANDROID-RISCV64-NEXT: # %bb.1: # %SP_return
; ANDROID-RISCV64-NEXT: ld ra, 24(sp) # 8-byte Folded Reload
; ANDROID-RISCV64-NEXT: ld s0, 16(sp) # 8-byte Folded Reload
; ANDROID-RISCV64-NEXT: addi sp, sp, 32
; ANDROID-RISCV64-NEXT: ret
; ANDROID-RISCV64-NEXT: .LBB0_2:
; ANDROID-RISCV64-NEXT: .LBB0_2: # %CallStackCheckFailBlk
; ANDROID-RISCV64-NEXT: call __stack_chk_fail
%1 = alloca i32, align 4
call void @capture(ptr %1)
Expand Down
Loading