[CodeGenOptions] make StackProtectorGuardOffset signed
authorNick Desaulniers <ndesaulniers@google.com>
Tue, 27 Apr 2021 16:58:42 +0000 (09:58 -0700)
committerNick Desaulniers <ndesaulniers@google.com>
Tue, 27 Apr 2021 17:12:58 +0000 (10:12 -0700)
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

clang/include/clang/Basic/CodeGenOptions.def
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Clang.cpp
llvm/include/llvm/CodeGen/CommandFlags.h
llvm/include/llvm/Target/TargetOptions.h
llvm/lib/CodeGen/CommandFlags.cpp
llvm/lib/Target/X86/X86ISelLowering.cpp
llvm/test/CodeGen/X86/stack-protector-3.ll

index d3ea7d2..68ebcc6 100644 (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)
index 8de2853..4c171b9 100644 (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"}]>;
index 42d2151..e90d474 100644 (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;
index 69ce85e..a127338 100644 (file)
@@ -107,7 +107,7 @@ bool getXCOFFTracebackTable();
 std::string getBBSections();
 
 std::string getStackProtectorGuard();
-unsigned getStackProtectorGuardOffset();
+int getStackProtectorGuardOffset();
 std::string getStackProtectorGuardReg();
 
 unsigned getTLSSize();
index 651dbe1..5dab7de 100644 (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 =
index 76d8719..4c6c595 100644 (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(
index a5fe2cd..1a0c4b5 100644 (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());
   }
 
index 96b1a08..5cae90e 100644 (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
 ; 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