[llvm-commits] [llvm] r58824 - in /llvm/trunk: include/llvm/Intrinsics.td lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp lib/CodeGen/StackProtector.cpp

Bill Wendling isanbard at gmail.com
Thu Nov 6 17:24:01 PST 2008


Author: void
Date: Thu Nov  6 19:23:58 2008
New Revision: 58824

URL: http://llvm.org/viewvc/llvm-project?rev=58824&view=rev
Log:
- Modify the stack protector algorithm so that the stack slot is allocated in
  LLVM IR code and not in the selection DAG ISel. This is a cleaner solution.

- Fix the heuristic for determining if protectors are necessary. The previous
  one wasn't checking the proper type size.


Modified:
    llvm/trunk/include/llvm/Intrinsics.td
    llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp
    llvm/trunk/lib/CodeGen/StackProtector.cpp

Modified: llvm/trunk/include/llvm/Intrinsics.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Intrinsics.td?rev=58824&r1=58823&r2=58824&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Intrinsics.td (original)
+++ llvm/trunk/include/llvm/Intrinsics.td Thu Nov  6 19:23:58 2008
@@ -179,9 +179,10 @@
 // Stack Protector Intrinsics - The stackprotector_create writes the stack guard
 // to the correct place on the stack frame. The stackprotector_check reads back
 // the stack guard that the stackprotector_create stored.
-def int_stackprotector_create : Intrinsic<[llvm_void_ty, llvm_ptr_ty],
-                                          [IntrWriteMem]>;
-def int_stackprotector_check  : Intrinsic<[llvm_ptr_ty], [IntrReadMem]>;
+def int_stackprotector_create : Intrinsic<[llvm_void_ty, llvm_ptr_ty,
+                                           llvm_ptrptr_ty], [IntrWriteMem]>;
+def int_stackprotector_check  : Intrinsic<[llvm_ptr_ty, llvm_ptrptr_ty],
+                                          [IntrReadMem]>;
 
 //===------------------- Standard C Library Intrinsics --------------------===//
 //

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp?rev=58824&r1=58823&r2=58824&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp Thu Nov  6 19:23:58 2008
@@ -3801,14 +3801,10 @@
     MachineFrameInfo *MFI = MF.getFrameInfo();
     MVT PtrTy = TLI.getPointerTy();
 
-    // Retrieve the stack protector guard's value.
-    SDValue Src = getValue(I.getOperand(1));
+    SDValue Src = getValue(I.getOperand(1));   // The guard's value.
+    AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
 
-    // Create a slot on the stack for the stack protector. It should go first
-    // before local variables are allocated.
-    unsigned Align =
-      TLI.getTargetData()->getPrefTypeAlignment(PtrTy.getTypeForMVT());
-    int FI = MFI->CreateStackObject(PtrTy.getSizeInBits() / 8, Align);
+    int FI = FuncInfo.StaticAllocaMap[Slot];
     MFI->setStackProtectorIndex(FI);
 
     SDValue FIN = DAG.getFrameIndex(FI, PtrTy);

Modified: llvm/trunk/lib/CodeGen/StackProtector.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/StackProtector.cpp?rev=58824&r1=58823&r2=58824&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/StackProtector.cpp (original)
+++ llvm/trunk/lib/CodeGen/StackProtector.cpp Thu Nov  6 19:23:58 2008
@@ -97,9 +97,6 @@
 ///  - The epilogue checks the value stored in the prologue against the original
 ///    value. It calls __stack_chk_fail if they differ.
 bool StackProtector::InsertStackProtectors() {
-  Constant *StackGuardVar = 0;  // The global variable for the stack guard.
-  BasicBlock *FailBB = 0;       // The basic block to jump to if check fails.
-
   // Loop through the basic blocks that have return instructions. Convert this:
   //
   //   return:
@@ -122,18 +119,34 @@
   //     call void @__stack_chk_fail()
   //     unreachable
   //
+  BasicBlock *FailBB = 0;       // The basic block to jump to if check fails.
+  AllocaInst *AI = 0;           // Place on stack that stores the stack guard.
+  Constant *StackGuardVar = 0;  // The stack guard variable.
+
   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
     BasicBlock *BB = I;
 
     if (isa<ReturnInst>(BB->getTerminator())) {
-      // Create the basic block to jump to when the guard check fails.
-      if (!FailBB)
+      if (!FailBB) {
+        // Create the basic block to jump to when the guard check fails.
         FailBB = CreateFailBB();
 
-      if (!StackGuardVar)
-        StackGuardVar =
-          M->getOrInsertGlobal("__stack_chk_guard",
-                               PointerType::getUnqual(Type::Int8Ty));
+        // Insert code into the entry block that stores the __stack_chk_guard
+        // variable onto the stack.
+        PointerType *PtrTy = PointerType::getUnqual(Type::Int8Ty);
+        StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
+
+        BasicBlock &Entry = F->getEntryBlock();
+        Instruction *InsPt = &Entry.front();
+
+        AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt);
+        LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt);
+
+        Value *Args[] = { LI, AI };
+        CallInst::
+          Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_create),
+                 &Args[0], array_endof(Args), "", InsPt);
+      }
 
       ReturnInst *RI = cast<ReturnInst>(BB->getTerminator());
       Function::iterator InsPt = BB; ++InsPt; // Insertion point for new BB.
@@ -151,7 +164,7 @@
       LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB);
       CallInst *CI = CallInst::
         Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_check),
-               "", BB);
+               AI, "", BB);
       ICmpInst *Cmp = new ICmpInst(CmpInst::ICMP_EQ, CI, LI1, "", BB);
       BranchInst::Create(NewBB, FailBB, Cmp, BB);
     }
@@ -161,16 +174,6 @@
   // statements in the function.
   if (!FailBB) return false;
 
-  // Insert code into the entry block that stores the __stack_chk_guard variable
-  // onto the stack.
-  BasicBlock &Entry = F->getEntryBlock();
-  Instruction *InsertPt = &Entry.front();
-
-  LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsertPt);
-  CallInst::
-    Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_create),
-           LI, "", InsertPt);
-
   return true;
 }
 
@@ -202,21 +205,16 @@
       for (BasicBlock::iterator
              II = BB->begin(), IE = BB->end(); II != IE; ++II)
         if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
-          if (!AI->isArrayAllocation()) continue; // Only care about arrays.
-
-          if (ConstantInt *CI = dyn_cast<ConstantInt>(AI->getArraySize())) {
-            const Type *Ty = AI->getAllocatedType();
-            uint64_t TySize = TD->getABITypeSize(Ty);
+          if (AI->isArrayAllocation())
+            // This is a call to alloca with a variable size. Emit stack
+            // protectors.
+            return true;
 
+          if (const ArrayType *AT = dyn_cast<ArrayType>(AI->getAllocatedType()))
             // If an array has more than 8 bytes of allocated space, then we
             // emit stack protectors.
-            if (SSPBufferSize <= TySize * CI->getZExtValue())
+            if (SSPBufferSize <= TD->getABITypeSize(AT))
               return true;
-          } else {
-            // This is a call to alloca with a variable size. Default to adding
-            // stack protectors.
-            return true;
-          }
         }
     }
 





More information about the llvm-commits mailing list