Skip to content

[Thumb,ELF] Fix access to dso_preemptable __stack_chk_guard with static relocation model #78950

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
4 changes: 2 additions & 2 deletions llvm/lib/Target/ARM/Thumb1InstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ void Thumb1InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
void Thumb1InstrInfo::expandLoadStackGuard(
MachineBasicBlock::iterator MI) const {
MachineFunction &MF = *MI->getParent()->getParent();
const TargetMachine &TM = MF.getTarget();
const ARMSubtarget &ST = MF.getSubtarget<ARMSubtarget>();
const auto *GV = cast<GlobalValue>((*MI->memoperands_begin())->getValue());

assert(MF.getFunction().getParent()->getStackProtectorGuard() != "tls" &&
"TLS stack protector not supported for Thumb1 targets");

unsigned Instr;
if (TM.isPositionIndependent())
if (!GV->isDSOLocal())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does Thumb1 need similar guards for whether the target is elf?

Copy link
Member Author

@MaskRay MaskRay Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the answer is no.

Based on my archaeology to the Thumb1InstrInfo::expandLoadStackGuard function, the changes haven't been related to Thumb1-only microarchitectures (before v6T2/v7) for non-ELF object file formats.

https://reviews.llvm.org/D75453 says that Windows AArch32 requires at least v7.

Instr = ARM::tLDRLIT_ga_pcrel;
else if (ST.genExecuteOnly() && ST.hasV8MBaselineOps())
Instr = ARM::t2MOVi32imm;
Expand Down
6 changes: 2 additions & 4 deletions llvm/lib/Target/ARM/Thumb2InstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,8 @@ void Thumb2InstrInfo::expandLoadStackGuard(
return;
}

const GlobalValue *GV =
cast<GlobalValue>((*MI->memoperands_begin())->getValue());

if (MF.getSubtarget<ARMSubtarget>().isGVInGOT(GV))
const auto *GV = cast<GlobalValue>((*MI->memoperands_begin())->getValue());
if (MF.getSubtarget<ARMSubtarget>().isTargetELF() && !GV->isDSOLocal())
expandLoadStackGuardBase(MI, ARM::t2LDRLIT_ga_pcrel, ARM::t2LDRi12);
else if (MF.getTarget().isPositionIndependent())
expandLoadStackGuardBase(MI, ARM::t2MOV_ga_pcrel, ARM::t2LDRi12);
Expand Down
30 changes: 24 additions & 6 deletions llvm/test/CodeGen/ARM/stack-guard-elf.ll
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ define i32 @test1() #0 {
; THUMB1-NEXT: .pad #16
; THUMB1-NEXT: sub sp, #16
; THUMB1-NEXT: ldr r0, .LCPI0_0
; THUMB1-NEXT: .LPC0_0:
; THUMB1-NEXT: add r0, pc
; THUMB1-NEXT: ldr r0, [r0]
; THUMB1-NEXT: ldr r0, [r0]
; THUMB1-NEXT: add r1, sp, #904
Expand All @@ -67,7 +69,9 @@ define i32 @test1() #0 {
; THUMB1-NEXT: bl foo
; THUMB1-NEXT: add r0, sp, #904
; THUMB1-NEXT: ldr r0, [r0, #124]
; THUMB1-NEXT: ldr r1, .LCPI0_0
; THUMB1-NEXT: ldr r1, .LCPI0_1
; THUMB1-NEXT: .LPC0_1:
; THUMB1-NEXT: add r1, pc
; THUMB1-NEXT: ldr r1, [r1]
; THUMB1-NEXT: ldr r1, [r1]
; THUMB1-NEXT: cmp r1, r0
Expand All @@ -83,7 +87,11 @@ define i32 @test1() #0 {
; THUMB1-NEXT: .p2align 2
; THUMB1-NEXT: @ %bb.3:
; THUMB1-NEXT: .LCPI0_0:
; THUMB1-NEXT: .long __stack_chk_guard
; THUMB1-NEXT: .Ltmp0:
; THUMB1-NEXT: .long __stack_chk_guard(GOT_PREL)-((.LPC0_0+4)-.Ltmp0)
; THUMB1-NEXT: .LCPI0_1:
; THUMB1-NEXT: .Ltmp1:
; THUMB1-NEXT: .long __stack_chk_guard(GOT_PREL)-((.LPC0_1+4)-.Ltmp1)
;
; THUMB1-PIC-LABEL: test1:
; THUMB1-PIC: @ %bb.0:
Expand Down Expand Up @@ -136,16 +144,18 @@ define i32 @test1() #0 {
; THUMB2-NEXT: push {r7, lr}
; THUMB2-NEXT: .pad #1032
; THUMB2-NEXT: sub.w sp, sp, #1032
; THUMB2-NEXT: movw r0, :lower16:__stack_chk_guard
; THUMB2-NEXT: movt r0, :upper16:__stack_chk_guard
; THUMB2-NEXT: ldr r0, .LCPI0_0
; THUMB2-NEXT: .LPC0_0:
; THUMB2-NEXT: add r0, pc
; THUMB2-NEXT: ldr r0, [r0]
; THUMB2-NEXT: ldr r0, [r0]
; THUMB2-NEXT: str.w r0, [sp, #1028]
; THUMB2-NEXT: add r0, sp, #4
; THUMB2-NEXT: bl foo
; THUMB2-NEXT: movw r1, :lower16:__stack_chk_guard
; THUMB2-NEXT: ldr.w r0, [sp, #1028]
; THUMB2-NEXT: movt r1, :upper16:__stack_chk_guard
; THUMB2-NEXT: ldr r1, .LCPI0_1
; THUMB2-NEXT: .LPC0_1:
; THUMB2-NEXT: add r1, pc
; THUMB2-NEXT: ldr r1, [r1]
; THUMB2-NEXT: ldr r1, [r1]
; THUMB2-NEXT: cmp r1, r0
Expand All @@ -155,6 +165,14 @@ define i32 @test1() #0 {
; THUMB2-NEXT: popeq {r7, pc}
; THUMB2-NEXT: .LBB0_1:
; THUMB2-NEXT: bl __stack_chk_fail
; THUMB2-NEXT: .p2align 2
; THUMB2-NEXT: @ %bb.2:
; THUMB2-NEXT: .LCPI0_0:
; THUMB2-NEXT: .Ltmp0:
; THUMB2-NEXT: .long __stack_chk_guard(GOT_PREL)-((.LPC0_0+4)-.Ltmp0)
; THUMB2-NEXT: .LCPI0_1:
; THUMB2-NEXT: .Ltmp1:
; THUMB2-NEXT: .long __stack_chk_guard(GOT_PREL)-((.LPC0_1+4)-.Ltmp1)
;
; THUMB2-PIC-LABEL: test1:
; THUMB2-PIC: @ %bb.0:
Expand Down