[CodeGenOptions] make StackProtectorGuardOffset signed

GCC supports negative values for -mstack-protector-guard-offset=, this
should be a signed value. Pre-req to D100919.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D101325
This commit is contained in:
Nick Desaulniers 2021-04-27 09:58:42 -07:00
parent 4cf942adab
commit ea8416bf4d
8 changed files with 22 additions and 12 deletions

View File

@ -371,7 +371,7 @@ ENUM_CODEGENOPT(DefaultTLSModel, TLSModel, 2, GeneralDynamicTLSModel)
VALUE_CODEGENOPT(TLSSize, 8, 0)
/// The default stack protector guard offset to use.
VALUE_CODEGENOPT(StackProtectorGuardOffset, 32, (unsigned)-1)
VALUE_CODEGENOPT(StackProtectorGuardOffset, 32, INT_MAX)
/// Number of path components to strip when emitting checks. (0 == full
/// filename)

View File

@ -3345,7 +3345,7 @@ def mstack_protector_guard_EQ : Joined<["-"], "mstack-protector-guard=">, Group<
MarshallingInfoString<CodeGenOpts<"StackProtectorGuard">>;
def mstack_protector_guard_offset_EQ : Joined<["-"], "mstack-protector-guard-offset=">, Group<m_Group>, Flags<[CC1Option]>,
HelpText<"Use the given offset for addressing the stack-protector guard">,
MarshallingInfoInt<CodeGenOpts<"StackProtectorGuardOffset">, "(unsigned)-1">;
MarshallingInfoInt<CodeGenOpts<"StackProtectorGuardOffset">, "INT_MAX">;
def mstack_protector_guard_reg_EQ : Joined<["-"], "mstack-protector-guard-reg=">, Group<m_Group>, Flags<[CC1Option]>,
HelpText<"Use the given reg for addressing the stack-protector guard">,
MarshallingInfoString<CodeGenOpts<"StackProtectorGuardReg">, [{"none"}]>;

View File

@ -3124,7 +3124,7 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
if (!EffectiveTriple.isX86())
D.Diag(diag::err_drv_unsupported_opt_for_target)
<< A->getAsString(Args) << TripleStr;
unsigned Offset;
int Offset;
if (Value.getAsInteger(10, Offset)) {
D.Diag(diag::err_drv_invalid_value) << A->getOption().getName() << Value;
return;

View File

@ -107,7 +107,7 @@ bool getXCOFFTracebackTable();
std::string getBBSections();
std::string getStackProtectorGuard();
unsigned getStackProtectorGuardOffset();
int getStackProtectorGuardOffset();
std::string getStackProtectorGuardReg();
unsigned getTLSSize();

View File

@ -332,7 +332,7 @@ namespace llvm {
unsigned XRayOmitFunctionIndex : 1;
/// Stack protector guard offset to use.
unsigned StackProtectorGuardOffset = -1U;
int StackProtectorGuardOffset = INT_MAX;
/// Stack protector guard mode to use, e.g. tls, global.
StackProtectorGuards StackProtectorGuard =

View File

@ -80,7 +80,7 @@ CGOPT(bool, IgnoreXCOFFVisibility)
CGOPT(bool, XCOFFTracebackTable)
CGOPT(std::string, BBSections)
CGOPT(std::string, StackProtectorGuard)
CGOPT(unsigned, StackProtectorGuardOffset)
CGOPT(int, StackProtectorGuardOffset)
CGOPT(std::string, StackProtectorGuardReg)
CGOPT(unsigned, TLSSize)
CGOPT(bool, EmulatedTLS)
@ -375,9 +375,9 @@ codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
cl::init("none"));
CGBINDOPT(StackProtectorGuardReg);
static cl::opt<unsigned> StackProtectorGuardOffset(
static cl::opt<int> StackProtectorGuardOffset(
"stack-protector-guard-offset", cl::desc("Stack protector guard offset"),
cl::init((unsigned)-1));
cl::init(INT_MAX));
CGBINDOPT(StackProtectorGuardOffset);
static cl::opt<unsigned> TLSSize(

View File

@ -2484,7 +2484,7 @@ static bool hasStackGuardSlotTLS(const Triple &TargetTriple) {
}
static Constant* SegmentOffset(IRBuilder<> &IRB,
unsigned Offset, unsigned AddressSpace) {
int Offset, unsigned AddressSpace) {
return ConstantExpr::getIntToPtr(
ConstantInt::get(Type::getInt32Ty(IRB.getContext()), Offset),
Type::getInt8PtrTy(IRB.getContext())->getPointerTo(AddressSpace));
@ -2501,11 +2501,11 @@ Value *X86TargetLowering::getIRStackGuard(IRBuilder<> &IRB) const {
} else {
unsigned AddressSpace = getAddressSpace();
// Specially, some users may customize the base reg and offset.
unsigned Offset = getTargetMachine().Options.StackProtectorGuardOffset;
int Offset = getTargetMachine().Options.StackProtectorGuardOffset;
// If we don't set -stack-protector-guard-offset value:
// %fs:0x28, unless we're using a Kernel code model, in which case
// it's %gs:0x28. gs:0x14 on i386.
if (Offset == (unsigned)-1)
if (Offset == INT_MAX)
Offset = (Subtarget.is64Bit()) ? 0x28 : 0x14;
const auto &GuardReg = getTargetMachine().Options.StackProtectorGuardReg;
@ -2576,7 +2576,7 @@ Value *X86TargetLowering::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
if (Subtarget.isTargetAndroid()) {
// %fs:0x48, unless we're using a Kernel code model, in which case it's %gs:
// %gs:0x24 on i386
unsigned Offset = (Subtarget.is64Bit()) ? 0x48 : 0x24;
int Offset = (Subtarget.is64Bit()) ? 0x48 : 0x24;
return SegmentOffset(IRB, Offset, getAddressSpace());
}

View File

@ -4,6 +4,7 @@
; RUN: llc -mtriple=x86_64-pc-linux-gnu -stack-protector-guard-reg=fs -o - < %s | FileCheck --check-prefix=CHECK-TLS-FS-40 %s
; RUN: llc -mtriple=x86_64-pc-linux-gnu -stack-protector-guard-reg=gs -o - < %s | FileCheck --check-prefix=CHECK-GS %s
; RUN: llc -mtriple=x86_64-pc-linux-gnu -stack-protector-guard-offset=20 -o - < %s | FileCheck --check-prefix=CHECK-OFFSET %s
; RUN: llc -mtriple=x86_64-pc-linux-gnu -stack-protector-guard-offset=-20 -o - < %s | FileCheck --check-prefix=CHECK-NEGATIVE-OFFSET %s
; CHECK-TLS-FS-40: movq %fs:40, %rax
; CHECK-TLS-FS-40: movq %fs:40, %rax
@ -29,6 +30,15 @@
; CHECK-OFFSET-NEXT: .cfi_def_cfa_offset 32
; CHECK-OFFSET-NEXT: callq __stack_chk_fail
; CHECK-NEGATIVE-OFFSET: movl $4294967276, %eax # imm = 0xFFFFFFEC
; CHECK-NEGATIVE-OFFSET: movq %fs:(%rax), %rcx
; CHECK-NEGATIVE-OFFSET: movq %fs:(%rax), %rax
; CHECK-NEGATIVE-OFFSET-NEXT: cmpq 16(%rsp), %rax
; CHECK-NEGATIVE-OFFSET-NEXT: jne .LBB0_2
; CHECK-NEGATIVE-OFFSET: .LBB0_2:
; CHECK-NEGATIVE-OFFSET-NEXT: .cfi_def_cfa_offset 32
; CHECK-NEGATIVE-OFFSET-NEXT: callq __stack_chk_fail
; CHECK-GLOBAL: movq __stack_chk_guard(%rip), %rax
; CHECK-GLOBAL: movq __stack_chk_guard(%rip), %rax
; CHECK-GLOBAL-NEXT: cmpq 16(%rsp), %rax