diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -371,7 +371,7 @@ 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) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3345,7 +3345,7 @@ MarshallingInfoString>; def mstack_protector_guard_offset_EQ : Joined<["-"], "mstack-protector-guard-offset=">, Group, Flags<[CC1Option]>, HelpText<"Use the given offset for addressing the stack-protector guard">, - MarshallingInfoInt, "(unsigned)-1">; + MarshallingInfoInt, "INT_MAX">; def mstack_protector_guard_reg_EQ : Joined<["-"], "mstack-protector-guard-reg=">, Group, Flags<[CC1Option]>, HelpText<"Use the given reg for addressing the stack-protector guard">, MarshallingInfoString, [{"none"}]>; diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -3117,7 +3117,7 @@ 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; diff --git a/llvm/include/llvm/CodeGen/CommandFlags.h b/llvm/include/llvm/CodeGen/CommandFlags.h --- a/llvm/include/llvm/CodeGen/CommandFlags.h +++ b/llvm/include/llvm/CodeGen/CommandFlags.h @@ -107,7 +107,7 @@ std::string getBBSections(); std::string getStackProtectorGuard(); -unsigned getStackProtectorGuardOffset(); +int getStackProtectorGuardOffset(); std::string getStackProtectorGuardReg(); unsigned getTLSSize(); diff --git a/llvm/include/llvm/Target/TargetOptions.h b/llvm/include/llvm/Target/TargetOptions.h --- a/llvm/include/llvm/Target/TargetOptions.h +++ b/llvm/include/llvm/Target/TargetOptions.h @@ -332,7 +332,7 @@ 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 = diff --git a/llvm/lib/CodeGen/CommandFlags.cpp b/llvm/lib/CodeGen/CommandFlags.cpp --- a/llvm/lib/CodeGen/CommandFlags.cpp +++ b/llvm/lib/CodeGen/CommandFlags.cpp @@ -80,7 +80,7 @@ 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 @@ cl::init("none")); CGBINDOPT(StackProtectorGuardReg); - static cl::opt StackProtectorGuardOffset( + static cl::opt StackProtectorGuardOffset( "stack-protector-guard-offset", cl::desc("Stack protector guard offset"), - cl::init((unsigned)-1)); + cl::init(INT_MAX)); CGBINDOPT(StackProtectorGuardOffset); static cl::opt TLSSize( diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -2484,7 +2484,7 @@ } 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 @@ } 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 @@ 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()); } diff --git a/llvm/test/CodeGen/X86/stack-protector-3.ll b/llvm/test/CodeGen/X86/stack-protector-3.ll --- a/llvm/test/CodeGen/X86/stack-protector-3.ll +++ b/llvm/test/CodeGen/X86/stack-protector-3.ll @@ -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