| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 1 | //===-- StackProtector.cpp - Stack Protector Insertion --------------------===// | 
 | 2 | // | 
 | 3 | //                     The LLVM Compiler Infrastructure | 
 | 4 | // | 
 | 5 | // This file is distributed under the University of Illinois Open Source | 
 | 6 | // License. See LICENSE.TXT for details. | 
 | 7 | // | 
 | 8 | //===----------------------------------------------------------------------===// | 
 | 9 | // | 
| Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 10 | // This pass inserts stack protectors into functions which need them. A variable | 
 | 11 | // with a random value in it is stored onto the stack before the local variables | 
 | 12 | // are allocated. Upon exiting the block, the stored value is checked. If it's | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 13 | // changed, then there was some sort of violation and the program aborts. | 
 | 14 | // | 
 | 15 | //===----------------------------------------------------------------------===// | 
 | 16 |  | 
 | 17 | #define DEBUG_TYPE "stack-protector" | 
 | 18 | #include "llvm/CodeGen/Passes.h" | 
| Bill Wendling | e9e6bdf | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 19 | #include "llvm/Attributes.h" | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" | 
 | 21 | #include "llvm/DerivedTypes.h" | 
 | 22 | #include "llvm/Function.h" | 
 | 23 | #include "llvm/Instructions.h" | 
| Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 24 | #include "llvm/Intrinsics.h" | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 25 | #include "llvm/Module.h" | 
 | 26 | #include "llvm/Pass.h" | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" | 
| Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" | 
 | 29 | #include "llvm/Target/TargetLowering.h" | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 30 | using namespace llvm; | 
 | 31 |  | 
| Bill Wendling | f0eaa9a | 2008-11-05 00:46:15 +0000 | [diff] [blame] | 32 | // SSPBufferSize - The lower bound for a buffer to be considered for stack | 
 | 33 | // smashing protection. | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 34 | static cl::opt<unsigned> | 
| Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 35 | SSPBufferSize("stack-protector-buffer-size", cl::init(8), | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 36 |               cl::desc("Lower bound for a buffer to be considered for " | 
 | 37 |                        "stack protection")); | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 38 |  | 
 | 39 | namespace { | 
| Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 40 |   class StackProtector : public FunctionPass { | 
| Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 41 |     /// TLI - Keep a pointer of a TargetLowering to consult for determining | 
 | 42 |     /// target type sizes. | 
 | 43 |     const TargetLowering *TLI; | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 44 |  | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 45 |     Function *F; | 
 | 46 |     Module *M; | 
 | 47 |  | 
| Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 48 |     /// InsertStackProtectors - Insert code into the prologue and epilogue of | 
 | 49 |     /// the function. | 
 | 50 |     /// | 
 | 51 |     ///  - The prologue code loads and stores the stack guard onto the stack. | 
 | 52 |     ///  - The epilogue checks the value stored in the prologue against the | 
 | 53 |     ///    original value. It calls __stack_chk_fail if they differ. | 
 | 54 |     bool InsertStackProtectors(); | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 55 |  | 
 | 56 |     /// CreateFailBB - Create a basic block to jump to when the stack protector | 
 | 57 |     /// check fails. | 
| Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 58 |     BasicBlock *CreateFailBB(); | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 59 |  | 
 | 60 |     /// RequiresStackProtector - Check whether or not this function needs a | 
 | 61 |     /// stack protector based upon the stack protector level. | 
| Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 62 |     bool RequiresStackProtector() const; | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 63 |   public: | 
 | 64 |     static char ID;             // Pass identification, replacement for typeid. | 
| Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 65 |     StackProtector() : FunctionPass(ID), TLI(0) { | 
 | 66 |       initializeStackProtectorPass(*PassRegistry::getPassRegistry()); | 
 | 67 |     } | 
| Bill Wendling | e9e6bdf | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 68 |     StackProtector(const TargetLowering *tli) | 
| Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 69 |       : FunctionPass(ID), TLI(tli) { | 
 | 70 |         initializeStackProtectorPass(*PassRegistry::getPassRegistry()); | 
 | 71 |       } | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 72 |  | 
 | 73 |     virtual bool runOnFunction(Function &Fn); | 
 | 74 |   }; | 
 | 75 | } // end anonymous namespace | 
 | 76 |  | 
 | 77 | char StackProtector::ID = 0; | 
| Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 78 | INITIALIZE_PASS(StackProtector, "stack-protector", | 
| Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 79 |                 "Insert stack protectors", false, false) | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 80 |  | 
| Bill Wendling | e9e6bdf | 2008-11-13 01:02:14 +0000 | [diff] [blame] | 81 | FunctionPass *llvm::createStackProtectorPass(const TargetLowering *tli) { | 
 | 82 |   return new StackProtector(tli); | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 83 | } | 
 | 84 |  | 
 | 85 | bool StackProtector::runOnFunction(Function &Fn) { | 
 | 86 |   F = &Fn; | 
 | 87 |   M = F->getParent(); | 
 | 88 |  | 
 | 89 |   if (!RequiresStackProtector()) return false; | 
 | 90 |    | 
| Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 91 |   return InsertStackProtectors(); | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 92 | } | 
 | 93 |  | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 94 | /// RequiresStackProtector - Check whether or not this function needs a stack | 
 | 95 | /// protector based upon the stack protector level. The heuristic we use is to | 
 | 96 | /// add a guard variable to functions that call alloca, and functions with | 
 | 97 | /// buffers larger than SSPBufferSize bytes. | 
 | 98 | bool StackProtector::RequiresStackProtector() const { | 
 | 99 |   if (F->hasFnAttr(Attribute::StackProtectReq)) | 
 | 100 |     return true; | 
 | 101 |  | 
 | 102 |   if (!F->hasFnAttr(Attribute::StackProtect)) | 
 | 103 |     return false; | 
 | 104 |  | 
 | 105 |   const TargetData *TD = TLI->getTargetData(); | 
 | 106 |  | 
 | 107 |   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { | 
 | 108 |     BasicBlock *BB = I; | 
 | 109 |  | 
 | 110 |     for (BasicBlock::iterator | 
 | 111 |            II = BB->begin(), IE = BB->end(); II != IE; ++II) | 
 | 112 |       if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) { | 
 | 113 |         if (AI->isArrayAllocation()) | 
 | 114 |           // This is a call to alloca with a variable size. Emit stack | 
 | 115 |           // protectors. | 
 | 116 |           return true; | 
 | 117 |  | 
| Bill Wendling | dfd85c1 | 2009-10-23 00:01:05 +0000 | [diff] [blame] | 118 |         if (const ArrayType *AT = dyn_cast<ArrayType>(AI->getAllocatedType())) { | 
 | 119 |           // We apparently only care about character arrays. | 
| Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 120 |           if (!AT->getElementType()->isIntegerTy(8)) | 
| Bill Wendling | dfd85c1 | 2009-10-23 00:01:05 +0000 | [diff] [blame] | 121 |             continue; | 
 | 122 |  | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 123 |           // If an array has more than SSPBufferSize bytes of allocated space, | 
 | 124 |           // then we emit stack protectors. | 
| Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 125 |           if (SSPBufferSize <= TD->getTypeAllocSize(AT)) | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 126 |             return true; | 
| Bill Wendling | dfd85c1 | 2009-10-23 00:01:05 +0000 | [diff] [blame] | 127 |         } | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 128 |       } | 
 | 129 |   } | 
 | 130 |  | 
 | 131 |   return false; | 
 | 132 | } | 
 | 133 |  | 
| Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 134 | /// InsertStackProtectors - Insert code into the prologue and epilogue of the | 
 | 135 | /// function. | 
 | 136 | /// | 
 | 137 | ///  - The prologue code loads and stores the stack guard onto the stack. | 
 | 138 | ///  - The epilogue checks the value stored in the prologue against the original | 
 | 139 | ///    value. It calls __stack_chk_fail if they differ. | 
 | 140 | bool StackProtector::InsertStackProtectors() { | 
| Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 141 |   BasicBlock *FailBB = 0;       // The basic block to jump to if check fails. | 
 | 142 |   AllocaInst *AI = 0;           // Place on stack that stores the stack guard. | 
| Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 143 |   Value *StackGuardVar = 0;  // The stack guard variable. | 
| Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 144 |  | 
| Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 145 |   for (Function::iterator I = F->begin(), E = F->end(); I != E; ) { | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 146 |     BasicBlock *BB = I++; | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 147 |  | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 148 |     ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()); | 
 | 149 |     if (!RI) continue; | 
| Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 150 |  | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 151 |     if (!FailBB) { | 
 | 152 |       // Insert code into the entry block that stores the __stack_chk_guard | 
 | 153 |       // variable onto the stack: | 
 | 154 |       // | 
 | 155 |       //   entry: | 
 | 156 |       //     StackGuardSlot = alloca i8* | 
 | 157 |       //     StackGuard = load __stack_chk_guard | 
 | 158 |       //     call void @llvm.stackprotect.create(StackGuard, StackGuardSlot) | 
 | 159 |       //  | 
| Chris Lattner | f8bd392 | 2010-07-06 15:59:27 +0000 | [diff] [blame] | 160 |       const PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext()); | 
| Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 161 |       unsigned AddressSpace, Offset; | 
 | 162 |       if (TLI->getStackCookieLocation(AddressSpace, Offset)) { | 
| Chris Lattner | f8bd392 | 2010-07-06 15:59:27 +0000 | [diff] [blame] | 163 |         Constant *OffsetVal = | 
 | 164 |           ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset); | 
 | 165 |          | 
 | 166 |         StackGuardVar = ConstantExpr::getIntToPtr(OffsetVal, | 
 | 167 |                                       PointerType::get(PtrTy, AddressSpace)); | 
| Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 168 |       } else { | 
| Chris Lattner | f8bd392 | 2010-07-06 15:59:27 +0000 | [diff] [blame] | 169 |         StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);  | 
| Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 170 |       } | 
| Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 171 |  | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 172 |       BasicBlock &Entry = F->getEntryBlock(); | 
 | 173 |       Instruction *InsPt = &Entry.front(); | 
| Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 174 |  | 
| Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 175 |       AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt); | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 176 |       LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt); | 
| Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 177 |  | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 178 |       Value *Args[] = { LI, AI }; | 
 | 179 |       CallInst:: | 
| Bill Wendling | 5734450 | 2008-11-18 11:01:33 +0000 | [diff] [blame] | 180 |         Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 181 |                &Args[0], array_endof(Args), "", InsPt); | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 182 |  | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 183 |       // Create the basic block to jump to when the guard check fails. | 
 | 184 |       FailBB = CreateFailBB(); | 
| Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 185 |     } | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 186 |  | 
 | 187 |     // For each block with a return instruction, convert this: | 
 | 188 |     // | 
 | 189 |     //   return: | 
 | 190 |     //     ... | 
 | 191 |     //     ret ... | 
 | 192 |     // | 
 | 193 |     // into this: | 
 | 194 |     // | 
 | 195 |     //   return: | 
 | 196 |     //     ... | 
 | 197 |     //     %1 = load __stack_chk_guard | 
| Bill Wendling | 733bbc5 | 2008-11-18 07:30:57 +0000 | [diff] [blame] | 198 |     //     %2 = load StackGuardSlot | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 199 |     //     %3 = cmp i1 %1, %2 | 
 | 200 |     //     br i1 %3, label %SP_return, label %CallStackCheckFailBlk | 
 | 201 |     // | 
 | 202 |     //   SP_return: | 
 | 203 |     //     ret ... | 
 | 204 |     // | 
 | 205 |     //   CallStackCheckFailBlk: | 
 | 206 |     //     call void @__stack_chk_fail() | 
 | 207 |     //     unreachable | 
 | 208 |  | 
 | 209 |     // Split the basic block before the return instruction. | 
 | 210 |     BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return"); | 
 | 211 |  | 
| Bill Wendling | 5601699 | 2009-03-06 01:41:15 +0000 | [diff] [blame] | 212 |     // Remove default branch instruction to the new BB. | 
 | 213 |     BB->getTerminator()->eraseFromParent(); | 
 | 214 |  | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 215 |     // Move the newly created basic block to the point right after the old basic | 
 | 216 |     // block so that it's in the "fall through" position. | 
 | 217 |     NewBB->moveAfter(BB); | 
 | 218 |  | 
 | 219 |     // Generate the stack protector instructions in the old basic block. | 
| Bill Wendling | 733bbc5 | 2008-11-18 07:30:57 +0000 | [diff] [blame] | 220 |     LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB); | 
 | 221 |     LoadInst *LI2 = new LoadInst(AI, "", true, BB); | 
| Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 222 |     ICmpInst *Cmp = new ICmpInst(*BB, CmpInst::ICMP_EQ, LI1, LI2, ""); | 
| Bill Wendling | c3348a7 | 2008-11-18 05:32:11 +0000 | [diff] [blame] | 223 |     BranchInst::Create(NewBB, FailBB, Cmp, BB); | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 224 |   } | 
| Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 225 |  | 
| Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 226 |   // Return if we didn't modify any basic blocks. I.e., there are no return | 
 | 227 |   // statements in the function. | 
 | 228 |   if (!FailBB) return false; | 
 | 229 |  | 
| Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 230 |   return true; | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 231 | } | 
 | 232 |  | 
 | 233 | /// CreateFailBB - Create a basic block to jump to when the stack protector | 
 | 234 | /// check fails. | 
| Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 235 | BasicBlock *StackProtector::CreateFailBB() { | 
| Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 236 |   BasicBlock *FailBB = BasicBlock::Create(F->getContext(), | 
 | 237 |                                           "CallStackCheckFailBlk", F); | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 238 |   Constant *StackChkFail = | 
| Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 239 |     M->getOrInsertFunction("__stack_chk_fail", | 
 | 240 |                            Type::getVoidTy(F->getContext()), NULL); | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 241 |   CallInst::Create(StackChkFail, "", FailBB); | 
| Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 242 |   new UnreachableInst(F->getContext(), FailBB); | 
| Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 243 |   return FailBB; | 
| Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 244 | } |