[llvm] 056733d - [SafeStack] Use proper API to get stack guard

Pengxuan Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun May 30 00:53:54 PDT 2021


Author: Pengxuan Zheng
Date: 2021-05-30T00:52:48-07:00
New Revision: 056733d0195b28fb1c5a7952b2adc10013edf19c

URL: https://github.com/llvm/llvm-project/commit/056733d0195b28fb1c5a7952b2adc10013edf19c
DIFF: https://github.com/llvm/llvm-project/commit/056733d0195b28fb1c5a7952b2adc10013edf19c.diff

LOG: [SafeStack] Use proper API to get stack guard

Using the proper API automatically sets `__stack_chk_guard` to `dso_local` if
`Reloc::Static`. This wasn't strictly necessary until recently when dso_local was
no longer implied by `TargetMachine::shouldAssumeDSOLocal` for
`__stack_chk_guard`. By using the proper API, we can avoid generating unnecessary
GOT relocations.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D102646

Added: 
    

Modified: 
    llvm/lib/CodeGen/SafeStack.cpp
    llvm/test/Transforms/SafeStack/X86/abi_ssp.ll
    llvm/test/Transforms/SafeStack/X86/ssp.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SafeStack.cpp b/llvm/lib/CodeGen/SafeStack.cpp
index 368061740c4b1..e097843d5e0e5 100644
--- a/llvm/lib/CodeGen/SafeStack.cpp
+++ b/llvm/lib/CodeGen/SafeStack.cpp
@@ -373,9 +373,13 @@ bool SafeStack::IsSafeStackAlloca(const Value *AllocaPtr, uint64_t AllocaSize) {
 
 Value *SafeStack::getStackGuard(IRBuilder<> &IRB, Function &F) {
   Value *StackGuardVar = TL.getIRStackGuard(IRB);
-  if (!StackGuardVar)
-    StackGuardVar =
-        F.getParent()->getOrInsertGlobal("__stack_chk_guard", StackPtrTy);
+  Module *M = F.getParent();
+
+  if (!StackGuardVar) {
+    TL.insertSSPDeclarations(*M);
+    return IRB.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackguard));
+  }
+
   return IRB.CreateLoad(StackPtrTy, StackGuardVar, "StackGuard");
 }
 

diff  --git a/llvm/test/Transforms/SafeStack/X86/abi_ssp.ll b/llvm/test/Transforms/SafeStack/X86/abi_ssp.ll
index b489e07a88683..a4b193e208df9 100644
--- a/llvm/test/Transforms/SafeStack/X86/abi_ssp.ll
+++ b/llvm/test/Transforms/SafeStack/X86/abi_ssp.ll
@@ -13,7 +13,7 @@ entry:
 ; TLS32: %[[StackGuard:.*]] = load i8*, i8* addrspace(256)* inttoptr (i32 20 to i8* addrspace(256)*)
 ; TLS64: %[[StackGuard:.*]] = load i8*, i8* addrspace(257)* inttoptr (i32 40 to i8* addrspace(257)*)
 ; FUCHSIA64: %[[StackGuard:.*]] = load i8*, i8* addrspace(257)* inttoptr (i32 16 to i8* addrspace(257)*)
-; GLOBAL32: %[[StackGuard:.*]] = load i8*, i8** @__stack_chk_guard
+; GLOBAL32: %[[StackGuard:.*]] = call i8* @llvm.stackguard()
 ; COMMON:   store i8* %[[StackGuard]], i8** %[[StackGuardSlot:.*]]
   %a = alloca i8, align 1
   call void @Capture(i8* %a)

diff  --git a/llvm/test/Transforms/SafeStack/X86/ssp.ll b/llvm/test/Transforms/SafeStack/X86/ssp.ll
index 0e28878c5477a..e4286dbd2cb1b 100644
--- a/llvm/test/Transforms/SafeStack/X86/ssp.ll
+++ b/llvm/test/Transforms/SafeStack/X86/ssp.ll
@@ -8,7 +8,7 @@ entry:
 
 ; CHECK: %[[A:.*]] = getelementptr i8, i8* %[[USP]], i32 -8
 ; CHECK: %[[StackGuardSlot:.*]] = bitcast i8* %[[A]] to i8**
-; CHECK: %[[StackGuard:.*]] = load i8*, i8** @__stack_chk_guard
+; CHECK: %[[StackGuard:.*]] = call i8* @llvm.stackguard()
 ; CHECK: store i8* %[[StackGuard]], i8** %[[StackGuardSlot]]
   %a = alloca i8, align 1
 


        


More information about the llvm-commits mailing list