Skip to content

Commit 414995a

Browse files
committed
Add the eor instruction in the failure BB
1 parent 2763964 commit 414995a

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
@@ -5828,7 +5828,7 @@ class LLVM_ABI TargetLowering : public TargetLoweringBase {
58285828
virtual bool useLoadStackGuardNode(const Module &M) const { return false; }
58295829

58305830
virtual SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val,
5831-
const SDLoc &DL) const {
5831+
const SDLoc &DL, bool FailureBB) const {
58325832
llvm_unreachable("not implemented for this target");
58335833
}
58345834

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
@@ -7429,7 +7429,7 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
74297429
MachineMemOperand::MOVolatile);
74307430
}
74317431
if (TLI.useStackGuardXorFP())
7432-
Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7432+
Res = TLI.emitStackGuardXorFP(DAG, Res, sdl, false);
74337433
DAG.setRoot(Chain);
74347434
setValue(&I, Res);
74357435
return;

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

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

29245+
bool AArch64TargetLowering::useStackGuardXorFP() const {
29246+
// Currently only MSVC CRTs XOR the frame pointer into the stack guard value.
29247+
return Subtarget->getTargetTriple().isOSMSVCRT() &&
29248+
!Subtarget->isTargetMachO() &&
29249+
!getTargetMachine().Options.EnableGlobalISel;
29250+
}
29251+
29252+
SDValue AArch64TargetLowering::emitStackGuardXorFP(SelectionDAG &DAG,
29253+
SDValue Val, const SDLoc &DL,
29254+
bool FailureBB) const {
29255+
if (FailureBB) {
29256+
return DAG.getNode(
29257+
ISD::XOR, DL, Val.getValueType(), Val,
29258+
DAG.getCopyFromReg(DAG.getEntryNode(), DL,
29259+
getStackPointerRegisterToSaveRestore(), MVT::i64));
29260+
}
29261+
return Val;
29262+
}
29263+
2924529264
unsigned AArch64TargetLowering::combineRepeatedFPDivisors() const {
2924629265
// Combine multiple FDIVs with the same divisor into multiple FMULs by the
2924729266
// 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
@@ -363,6 +363,9 @@ class AArch64TargetLowering : public TargetLowering {
363363
shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const override;
364364

365365
bool useLoadStackGuardNode(const Module &M) const override;
366+
bool useStackGuardXorFP() const override;
367+
SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val, const SDLoc &DL,
368+
bool FailureBB) const override;
366369
TargetLoweringBase::LegalizeTypeAction
367370
getPreferredVectorAction(MVT VT) const override;
368371

llvm/lib/Target/X86/X86ISelLowering.cpp

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

27462746
SDValue X86TargetLowering::emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val,
2747-
const SDLoc &DL) const {
2747+
const SDLoc &DL,
2748+
bool FailureBB) const {
27482749
EVT PtrTy = getPointerTy(DAG.getDataLayout());
27492750
unsigned XorOp = Subtarget.is64Bit() ? X86::XOR64_FP : X86::XOR32_FP;
27502751
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)