Skip to content

Commit dc35787

Browse files
openeuler-ci-botgitee-org
authored andcommitted
!140 回退 'Pull Request !68 : [backport] Support stack clash protection in ve…
From: @cf-zhao Reviewed-by: @liyunfei33 Signed-off-by: @liyunfei33
2 parents 956edd0 + 0206359 commit dc35787

39 files changed

+265
-3362
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,15 +1067,6 @@ void CodeGenModule::Release() {
10671067
"sign-return-address-with-bkey", 1);
10681068
}
10691069

1070-
if (CodeGenOpts.StackClashProtector)
1071-
getModule().addModuleFlag(
1072-
llvm::Module::Override, "probe-stack",
1073-
llvm::MDString::get(TheModule.getContext(), "inline-asm"));
1074-
1075-
if (CodeGenOpts.StackProbeSize && CodeGenOpts.StackProbeSize != 4096)
1076-
getModule().addModuleFlag(llvm::Module::Min, "stack-probe-size",
1077-
CodeGenOpts.StackProbeSize);
1078-
10791070
if (!CodeGenOpts.MemoryProfileOutput.empty()) {
10801071
llvm::LLVMContext &Ctx = TheModule.getContext();
10811072
getModule().addModuleFlag(
@@ -2270,10 +2261,6 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
22702261
if (CodeGenOpts.StackClashProtector)
22712262
B.addAttribute("probe-stack", "inline-asm");
22722263

2273-
if (CodeGenOpts.StackProbeSize && CodeGenOpts.StackProbeSize != 4096)
2274-
B.addAttribute("stack-probe-size",
2275-
std::to_string(CodeGenOpts.StackProbeSize));
2276-
22772264
if (!hasUnwindExceptions(LangOpts))
22782265
B.addAttribute(llvm::Attribute::NoUnwind);
22792266

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3479,7 +3479,7 @@ static void RenderSCPOptions(const ToolChain &TC, const ArgList &Args,
34793479
return;
34803480

34813481
if (!EffectiveTriple.isX86() && !EffectiveTriple.isSystemZ() &&
3482-
!EffectiveTriple.isPPC64() && !EffectiveTriple.isAArch64())
3482+
!EffectiveTriple.isPPC64())
34833483
return;
34843484

34853485
Args.addOptInFlag(CmdArgs, options::OPT_fstack_clash_protection,
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Check the correct function attributes are generated
2-
// RUN: %clang_cc1 -triple x86_64-linux -O0 -S -emit-llvm -o- %s -fstack-clash-protection -mstack-probe-size=8192 | FileCheck %s
3-
// RUN: %clang_cc1 -triple s390x-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection -mstack-probe-size=8192 | FileCheck %s
4-
// RUN: %clang_cc1 -triple powerpc64le-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection -mstack-probe-size=8192 | FileCheck %s
5-
// RUN: %clang_cc1 -triple powerpc64-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection -mstack-probe-size=8192 | FileCheck %s
6-
// RUN: %clang_cc1 -triple aarch64-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection -mstack-probe-size=8192 | FileCheck %s
2+
// RUN: %clang_cc1 -triple x86_64-linux -O0 -S -emit-llvm -o- %s -fstack-clash-protection | FileCheck %s
3+
// RUN: %clang_cc1 -triple s390x-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection | FileCheck %s
4+
// RUN: %clang_cc1 -triple powerpc64le-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection | FileCheck %s
5+
// RUN: %clang_cc1 -triple powerpc64-linux-gnu -O0 -S -emit-llvm -o- %s -fstack-clash-protection | FileCheck %s
76

87
// CHECK: define{{.*}} void @large_stack() #[[A:.*]] {
98
void large_stack(void) {
@@ -12,18 +11,15 @@ void large_stack(void) {
1211
stack[i] = i;
1312
}
1413

15-
// CHECK: define{{.*}} void @vla({{.*}}) #[[A]] {
14+
// CHECK: define{{.*}} void @vla({{.*}}) #[[A:.*]] {
1615
void vla(int n) {
1716
volatile int vla[n];
1817
__builtin_memset(&vla[0], 0, 1);
1918
}
2019

21-
// CHECK: define{{.*}} void @builtin_alloca({{.*}}) #[[A]] {
20+
// CHECK: define{{.*}} void @builtin_alloca({{.*}}) #[[A:.*]] {
2221
void builtin_alloca(int n) {
2322
volatile void *mem = __builtin_alloca(n);
2423
}
2524

26-
// CHECK: attributes #[[A]] = {{.*}}"probe-stack"="inline-asm" {{.*}}"stack-probe-size"="8192"
27-
28-
// CHECK: !{i32 4, !"probe-stack", !"inline-asm"}
29-
// CHECK: !{i32 8, !"stack-probe-size", i32 8192}
25+
// CHECK: attributes #[[A]] = {{.*}} "probe-stack"="inline-asm"

llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,7 @@ class LegalizerHelper {
400400
LegalizeResult lowerUnmergeValues(MachineInstr &MI);
401401
LegalizeResult lowerExtractInsertVectorElt(MachineInstr &MI);
402402
LegalizeResult lowerShuffleVector(MachineInstr &MI);
403-
Register getDynStackAllocTargetPtr(Register SPReg, Register AllocSize,
404-
Align Alignment, LLT PtrTy);
405403
LegalizeResult lowerDynStackAlloc(MachineInstr &MI);
406-
LegalizeResult lowerStackSave(MachineInstr &MI);
407-
LegalizeResult lowerStackRestore(MachineInstr &MI);
408404
LegalizeResult lowerExtract(MachineInstr &MI);
409405
LegalizeResult lowerInsert(MachineInstr &MI);
410406
LegalizeResult lowerSADDO_SSUBO(MachineInstr &MI);

llvm/include/llvm/Support/TargetOpcodes.def

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,6 @@ HANDLE_TARGET_OPCODE(G_JUMP_TABLE)
763763
/// Generic dynamic stack allocation.
764764
HANDLE_TARGET_OPCODE(G_DYN_STACKALLOC)
765765

766-
/// Generic stack pointer save.
767-
HANDLE_TARGET_OPCODE(G_STACKSAVE)
768-
769-
/// Generic stack pointer restore.
770-
HANDLE_TARGET_OPCODE(G_STACKRESTORE)
771-
772766
/// Strict floating point instructions.
773767
HANDLE_TARGET_OPCODE(G_STRICT_FADD)
774768
HANDLE_TARGET_OPCODE(G_STRICT_FSUB)

llvm/include/llvm/Target/GenericOpcodes.td

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,6 @@ def G_DYN_STACKALLOC : GenericInstruction {
225225
let hasSideEffects = true;
226226
}
227227

228-
def G_STACKSAVE : GenericInstruction {
229-
let OutOperandList = (outs ptype0:$dst);
230-
let InOperandList = (ins);
231-
let hasSideEffects = true;
232-
}
233-
234-
def G_STACKRESTORE : GenericInstruction {
235-
let OutOperandList = (outs);
236-
let InOperandList = (ins ptype0:$src);
237-
let hasSideEffects = true;
238-
}
239-
240228
def G_FREEZE : GenericInstruction {
241229
let OutOperandList = (outs type0:$dst);
242230
let InOperandList = (ins type0:$src);

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,12 +2229,31 @@ bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
22292229
return true;
22302230
}
22312231
case Intrinsic::stacksave: {
2232-
MIRBuilder.buildInstr(TargetOpcode::G_STACKSAVE, {getOrCreateVReg(CI)}, {});
2232+
// Save the stack pointer to the location provided by the intrinsic.
2233+
Register Reg = getOrCreateVReg(CI);
2234+
Register StackPtr = MF->getSubtarget()
2235+
.getTargetLowering()
2236+
->getStackPointerRegisterToSaveRestore();
2237+
2238+
// If the target doesn't specify a stack pointer, then fall back.
2239+
if (!StackPtr)
2240+
return false;
2241+
2242+
MIRBuilder.buildCopy(Reg, StackPtr);
22332243
return true;
22342244
}
22352245
case Intrinsic::stackrestore: {
2236-
MIRBuilder.buildInstr(TargetOpcode::G_STACKRESTORE, {},
2237-
{getOrCreateVReg(*CI.getArgOperand(0))});
2246+
// Restore the stack pointer from the location provided by the intrinsic.
2247+
Register Reg = getOrCreateVReg(*CI.getArgOperand(0));
2248+
Register StackPtr = MF->getSubtarget()
2249+
.getTargetLowering()
2250+
->getStackPointerRegisterToSaveRestore();
2251+
2252+
// If the target doesn't specify a stack pointer, then fall back.
2253+
if (!StackPtr)
2254+
return false;
2255+
2256+
MIRBuilder.buildCopy(StackPtr, Reg);
22382257
return true;
22392258
}
22402259
case Intrinsic::cttz:

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3503,10 +3503,6 @@ LegalizerHelper::lower(MachineInstr &MI, unsigned TypeIdx, LLT LowerHintTy) {
35033503
return lowerShuffleVector(MI);
35043504
case G_DYN_STACKALLOC:
35053505
return lowerDynStackAlloc(MI);
3506-
case G_STACKSAVE:
3507-
return lowerStackSave(MI);
3508-
case G_STACKRESTORE:
3509-
return lowerStackRestore(MI);
35103506
case G_EXTRACT:
35113507
return lowerExtract(MI);
35123508
case G_INSERT:
@@ -6777,12 +6773,21 @@ LegalizerHelper::lowerShuffleVector(MachineInstr &MI) {
67776773
return Legalized;
67786774
}
67796775

6780-
Register LegalizerHelper::getDynStackAllocTargetPtr(Register SPReg,
6781-
Register AllocSize,
6782-
Align Alignment,
6783-
LLT PtrTy) {
6776+
LegalizerHelper::LegalizeResult
6777+
LegalizerHelper::lowerDynStackAlloc(MachineInstr &MI) {
6778+
const auto &MF = *MI.getMF();
6779+
const auto &TFI = *MF.getSubtarget().getFrameLowering();
6780+
if (TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp)
6781+
return UnableToLegalize;
6782+
6783+
Register Dst = MI.getOperand(0).getReg();
6784+
Register AllocSize = MI.getOperand(1).getReg();
6785+
Align Alignment = assumeAligned(MI.getOperand(2).getImm());
6786+
6787+
LLT PtrTy = MRI.getType(Dst);
67846788
LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits());
67856789

6790+
Register SPReg = TLI.getStackPointerRegisterToSaveRestore();
67866791
auto SPTmp = MIRBuilder.buildCopy(PtrTy, SPReg);
67876792
SPTmp = MIRBuilder.buildCast(IntPtrTy, SPTmp);
67886793

@@ -6797,54 +6802,14 @@ Register LegalizerHelper::getDynStackAllocTargetPtr(Register SPReg,
67976802
Alloc = MIRBuilder.buildAnd(IntPtrTy, Alloc, AlignCst);
67986803
}
67996804

6800-
return MIRBuilder.buildCast(PtrTy, Alloc).getReg(0);
6801-
}
6802-
6803-
LegalizerHelper::LegalizeResult
6804-
LegalizerHelper::lowerDynStackAlloc(MachineInstr &MI) {
6805-
const auto &MF = *MI.getMF();
6806-
const auto &TFI = *MF.getSubtarget().getFrameLowering();
6807-
if (TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp)
6808-
return UnableToLegalize;
6809-
6810-
Register Dst = MI.getOperand(0).getReg();
6811-
Register AllocSize = MI.getOperand(1).getReg();
6812-
Align Alignment = assumeAligned(MI.getOperand(2).getImm());
6813-
6814-
LLT PtrTy = MRI.getType(Dst);
6815-
Register SPReg = TLI.getStackPointerRegisterToSaveRestore();
6816-
Register SPTmp =
6817-
getDynStackAllocTargetPtr(SPReg, AllocSize, Alignment, PtrTy);
6818-
6805+
SPTmp = MIRBuilder.buildCast(PtrTy, Alloc);
68196806
MIRBuilder.buildCopy(SPReg, SPTmp);
68206807
MIRBuilder.buildCopy(Dst, SPTmp);
68216808

68226809
MI.eraseFromParent();
68236810
return Legalized;
68246811
}
68256812

6826-
LegalizerHelper::LegalizeResult
6827-
LegalizerHelper::lowerStackSave(MachineInstr &MI) {
6828-
Register StackPtr = TLI.getStackPointerRegisterToSaveRestore();
6829-
if (!StackPtr)
6830-
return UnableToLegalize;
6831-
6832-
MIRBuilder.buildCopy(MI.getOperand(0), StackPtr);
6833-
MI.eraseFromParent();
6834-
return Legalized;
6835-
}
6836-
6837-
LegalizerHelper::LegalizeResult
6838-
LegalizerHelper::lowerStackRestore(MachineInstr &MI) {
6839-
Register StackPtr = TLI.getStackPointerRegisterToSaveRestore();
6840-
if (!StackPtr)
6841-
return UnableToLegalize;
6842-
6843-
MIRBuilder.buildCopy(StackPtr, MI.getOperand(0));
6844-
MI.eraseFromParent();
6845-
return Legalized;
6846-
}
6847-
68486813
LegalizerHelper::LegalizeResult
68496814
LegalizerHelper::lowerExtract(MachineInstr &MI) {
68506815
auto [DstReg, DstTy, SrcReg, SrcTy] = MI.getFirst2RegLLTs();

0 commit comments

Comments
 (0)