Skip to content

Commit c61707a

Browse files
committed
Add the eor instruction in the failure BB
1 parent 23fbbbb commit c61707a

File tree

7 files changed

+36
-10
lines changed

7 files changed

+36
-10
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5798,7 +5798,7 @@ class LLVM_ABI TargetLowering : public TargetLoweringBase {
57985798
virtual bool useLoadStackGuardNode(const Module &M) const { return false; }
57995799

58005800
virtual SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val,
5801-
const SDLoc &DL) const {
5801+
const SDLoc &DL, bool FailureBB) const {
58025802
llvm_unreachable("not implemented for this target");
58035803
}
58045804

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3129,7 +3129,7 @@ void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD,
31293129
MachineMemOperand::MOVolatile);
31303130

31313131
if (TLI.useStackGuardXorFP())
3132-
GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3132+
GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl, false);
31333133

31343134
// If we're using function-based instrumentation, call the guard check
31353135
// function
@@ -3238,7 +3238,7 @@ void SelectionDAGBuilder::visitSPDescriptorFailure(
32383238
MachineMemOperand::MOVolatile);
32393239

32403240
if (TLI.useStackGuardXorFP())
3241-
GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3241+
GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl, true);
32423242

32433243
// The target provides a guard check function to validate the guard value.
32443244
// Generate a call to that function with the content of the guard slot as
@@ -7381,7 +7381,7 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
73817381
MachineMemOperand::MOVolatile);
73827382
}
73837383
if (TLI.useStackGuardXorFP())
7384-
Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7384+
Res = TLI.emitStackGuardXorFP(DAG, Res, sdl, false);
73857385
DAG.setRoot(Chain);
73867386
setValue(&I, Res);
73877387
return;

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29096,6 +29096,25 @@ bool AArch64TargetLowering::useLoadStackGuardNode(const Module &M) const {
2909629096
return true;
2909729097
}
2909829098

29099+
bool AArch64TargetLowering::useStackGuardXorFP() const {
29100+
// Currently only MSVC CRTs XOR the frame pointer into the stack guard value.
29101+
return Subtarget->getTargetTriple().isOSMSVCRT() &&
29102+
!Subtarget->isTargetMachO() &&
29103+
!getTargetMachine().Options.EnableGlobalISel;
29104+
}
29105+
29106+
SDValue AArch64TargetLowering::emitStackGuardXorFP(SelectionDAG &DAG,
29107+
SDValue Val, const SDLoc &DL,
29108+
bool FailureBB) const {
29109+
if (FailureBB) {
29110+
return DAG.getNode(
29111+
ISD::XOR, DL, Val.getValueType(), Val,
29112+
DAG.getCopyFromReg(DAG.getEntryNode(), DL,
29113+
getStackPointerRegisterToSaveRestore(), MVT::i64));
29114+
}
29115+
return Val;
29116+
}
29117+
2909929118
unsigned AArch64TargetLowering::combineRepeatedFPDivisors() const {
2910029119
// Combine multiple FDIVs with the same divisor into multiple FMULs by the
2910129120
// reciprocal if there are three or more FDIVs.

llvm/lib/Target/AArch64/AArch64ISelLowering.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ class AArch64TargetLowering : public TargetLowering {
358358
shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const override;
359359

360360
bool useLoadStackGuardNode(const Module &M) const override;
361+
bool useStackGuardXorFP() const override;
362+
SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val, const SDLoc &DL,
363+
bool FailureBB) const override;
361364
TargetLoweringBase::LegalizeTypeAction
362365
getPreferredVectorAction(MVT VT) const override;
363366

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2739,7 +2739,8 @@ bool X86TargetLowering::useStackGuardXorFP() const {
27392739
}
27402740

27412741
SDValue X86TargetLowering::emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val,
2742-
const SDLoc &DL) const {
2742+
const SDLoc &DL,
2743+
bool FailureBB) const {
27432744
EVT PtrTy = getPointerTy(DAG.getDataLayout());
27442745
unsigned XorOp = Subtarget.is64Bit() ? X86::XOR64_FP : X86::XOR32_FP;
27452746
MachineSDNode *Node = DAG.getMachineNode(XorOp, DL, PtrTy, Val);

llvm/lib/Target/X86/X86ISelLowering.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,9 +1592,8 @@ namespace llvm {
15921592
bool useLoadStackGuardNode(const Module &M) const override;
15931593
bool useStackGuardXorFP() const override;
15941594
void insertSSPDeclarations(Module &M) const override;
1595-
SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val,
1596-
const SDLoc &DL) const override;
1597-
1595+
SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val, const SDLoc &DL,
1596+
bool FailureBB) const override;
15981597

15991598
/// Return true if the target stores SafeStack pointer at a fixed offset in
16001599
/// some non-standard address space, and populates the address space and

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ declare void @_Z7CapturePi(ptr)
3434
; WINDOWS-AARCH64: eor x8, x8, sp
3535
; WINDOWS-AARCH64: str x8, [sp, #8]
3636
; WINDOWS-AARCH64: bl _Z7CapturePi
37-
; WINDOWS-AARCH64: ldr x0, [sp, #8]
37+
; WINDOWS-AARCH64: ldr x8, [sp, #8]
38+
; WINDOWS-AARCH64: mov x9, sp
39+
; WINDOWS-AARCH64: eor x0, x8, x9
3840
; WINDOWS-AARCH64: bl __security_check_cookie
3941

4042
; WINDOWS-ARM64EC: adrp x8, __security_cookie
4143
; WINDOWS-ARM64EC: ldr x8, [x8, :lo12:__security_cookie]
4244
; WINDOWS-ARM64EC: eor x8, x8, sp
4345
; WINDOWS-ARM64EC: str x8, [sp, #8]
4446
; WINDOWS-ARM64EC: bl "#_Z7CapturePi"
45-
; WINDOWS-ARM64EC: ldr x0, [sp, #8]
47+
; WINDOWS-ARM64EC: ldr x8, [sp, #8]
48+
; WINDOWS-ARM64EC: mov x9, sp
49+
; WINDOWS-ARM64EC: eor x0, x8, x9
4650
; WINDOWS-ARM64EC: bl "#__security_check_cookie_arm64ec"

0 commit comments

Comments
 (0)