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" |
| 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/DerivedTypes.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/Instructions.h" |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
| 25 | #include "llvm/Pass.h" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
| 28 | #include "llvm/Target/TargetLowering.h" |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Bill Wendling | f0eaa9a | 2008-11-05 00:46:15 +0000 | [diff] [blame] | 31 | // SSPBufferSize - The lower bound for a buffer to be considered for stack |
| 32 | // smashing protection. |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 33 | static cl::opt<unsigned> |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 34 | SSPBufferSize("stack-protector-buffer-size", cl::init(8), |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 35 | cl::desc("The lower bound for a buffer to be considered for " |
| 36 | "stack smashing protection.")); |
| 37 | |
| 38 | namespace { |
| 39 | class VISIBILITY_HIDDEN StackProtector : public FunctionPass { |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 40 | /// Level - The level of stack protection. |
| 41 | SSP::StackProtectorLevel Level; |
| 42 | |
| 43 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 44 | /// target type sizes. |
| 45 | const TargetLowering *TLI; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 46 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 47 | Function *F; |
| 48 | Module *M; |
| 49 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 50 | /// InsertStackProtectors - Insert code into the prologue and epilogue of |
| 51 | /// the function. |
| 52 | /// |
| 53 | /// - The prologue code loads and stores the stack guard onto the stack. |
| 54 | /// - The epilogue checks the value stored in the prologue against the |
| 55 | /// original value. It calls __stack_chk_fail if they differ. |
| 56 | bool InsertStackProtectors(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 57 | |
| 58 | /// CreateFailBB - Create a basic block to jump to when the stack protector |
| 59 | /// check fails. |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 60 | BasicBlock *CreateFailBB(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 61 | |
| 62 | /// RequiresStackProtector - Check whether or not this function needs a |
| 63 | /// stack protector based upon the stack protector level. |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 64 | bool RequiresStackProtector() const; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 65 | public: |
| 66 | static char ID; // Pass identification, replacement for typeid. |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 67 | StackProtector() : FunctionPass(&ID), Level(SSP::OFF), TLI(0) {} |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 68 | StackProtector(SSP::StackProtectorLevel lvl, const TargetLowering *tli) |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 69 | : FunctionPass(&ID), Level(lvl), TLI(tli) {} |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 70 | |
| 71 | virtual bool runOnFunction(Function &Fn); |
| 72 | }; |
| 73 | } // end anonymous namespace |
| 74 | |
| 75 | char StackProtector::ID = 0; |
| 76 | static RegisterPass<StackProtector> |
| 77 | X("stack-protector", "Insert stack protectors"); |
| 78 | |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 79 | FunctionPass *llvm::createStackProtectorPass(SSP::StackProtectorLevel lvl, |
| 80 | const TargetLowering *tli) { |
| 81 | return new StackProtector(lvl, tli); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | bool StackProtector::runOnFunction(Function &Fn) { |
| 85 | F = &Fn; |
| 86 | M = F->getParent(); |
| 87 | |
| 88 | if (!RequiresStackProtector()) return false; |
| 89 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 90 | return InsertStackProtectors(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 93 | /// InsertStackProtectors - Insert code into the prologue and epilogue of the |
| 94 | /// function. |
| 95 | /// |
| 96 | /// - The prologue code loads and stores the stack guard onto the stack. |
| 97 | /// - The epilogue checks the value stored in the prologue against the original |
| 98 | /// value. It calls __stack_chk_fail if they differ. |
| 99 | bool StackProtector::InsertStackProtectors() { |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 100 | // Loop through the basic blocks that have return instructions. Convert this: |
| 101 | // |
| 102 | // return: |
| 103 | // ... |
| 104 | // ret ... |
| 105 | // |
| 106 | // into this: |
| 107 | // |
| 108 | // return: |
| 109 | // ... |
| 110 | // %1 = load __stack_chk_guard |
| 111 | // %2 = load <stored stack guard> |
| 112 | // %3 = cmp i1 %1, %2 |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame] | 113 | // br i1 %3, label %SP_return, label %CallStackCheckFailBlk |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 114 | // |
Bill Wendling | b7c2c12 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 115 | // SP_return: |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 116 | // ret ... |
| 117 | // |
| 118 | // CallStackCheckFailBlk: |
| 119 | // call void @__stack_chk_fail() |
| 120 | // unreachable |
| 121 | // |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 122 | BasicBlock *FailBB = 0; // The basic block to jump to if check fails. |
| 123 | AllocaInst *AI = 0; // Place on stack that stores the stack guard. |
| 124 | Constant *StackGuardVar = 0; // The stack guard variable. |
| 125 | |
Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 126 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ) { |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 127 | BasicBlock *BB = I; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 128 | |
Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 129 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 130 | if (!FailBB) { |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 131 | // Insert code into the entry block that stores the __stack_chk_guard |
| 132 | // variable onto the stack. |
| 133 | PointerType *PtrTy = PointerType::getUnqual(Type::Int8Ty); |
| 134 | StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy); |
| 135 | |
| 136 | BasicBlock &Entry = F->getEntryBlock(); |
| 137 | Instruction *InsPt = &Entry.front(); |
| 138 | |
| 139 | AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt); |
| 140 | LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt); |
| 141 | |
| 142 | Value *Args[] = { LI, AI }; |
| 143 | CallInst:: |
| 144 | Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_create), |
| 145 | &Args[0], array_endof(Args), "", InsPt); |
Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 146 | |
| 147 | // Create the basic block to jump to when the guard check fails. |
| 148 | FailBB = CreateFailBB(); |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 149 | } |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 150 | |
Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 151 | ++I; // Skip to the next block so that we don't resplit the return block. |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 152 | |
| 153 | // Split the basic block before the return instruction. |
| 154 | BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return"); |
| 155 | |
Bill Wendling | 22511ed | 2008-11-10 23:38:59 +0000 | [diff] [blame] | 156 | // Move the newly created basic block to the point right after the old |
| 157 | // basic block so that it's in the "fall through" position. |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 158 | NewBB->removeFromParent(); |
Bill Wendling | 22511ed | 2008-11-10 23:38:59 +0000 | [diff] [blame] | 159 | F->getBasicBlockList().insert(I, NewBB); |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 160 | |
| 161 | // Generate the stack protector instructions in the old basic block. |
| 162 | LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB); |
| 163 | CallInst *CI = CallInst:: |
| 164 | Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_check), |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 165 | AI, "", BB); |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 166 | ICmpInst *Cmp = new ICmpInst(CmpInst::ICMP_EQ, CI, LI1, "", BB); |
| 167 | BranchInst::Create(NewBB, FailBB, Cmp, BB); |
Bill Wendling | 7205677 | 2008-11-10 21:13:10 +0000 | [diff] [blame] | 168 | } else { |
| 169 | ++I; |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 170 | } |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 171 | } |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 172 | |
Bill Wendling | 1fb615f | 2008-11-06 23:55:49 +0000 | [diff] [blame] | 173 | // Return if we didn't modify any basic blocks. I.e., there are no return |
| 174 | // statements in the function. |
| 175 | if (!FailBB) return false; |
| 176 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 177 | return true; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /// CreateFailBB - Create a basic block to jump to when the stack protector |
| 181 | /// check fails. |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 182 | BasicBlock *StackProtector::CreateFailBB() { |
| 183 | BasicBlock *FailBB = BasicBlock::Create("CallStackCheckFailBlk", F); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 184 | Constant *StackChkFail = |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 185 | M->getOrInsertFunction("__stack_chk_fail", Type::VoidTy, NULL); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 186 | CallInst::Create(StackChkFail, "", FailBB); |
| 187 | new UnreachableInst(FailBB); |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 188 | return FailBB; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /// RequiresStackProtector - Check whether or not this function needs a stack |
Bill Wendling | 89c5cc6 | 2008-11-06 02:38:58 +0000 | [diff] [blame] | 192 | /// protector based upon the stack protector level. The heuristic we use is to |
| 193 | /// add a guard variable to functions that call alloca, and functions with |
| 194 | /// buffers larger than 8 bytes. |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 195 | bool StackProtector::RequiresStackProtector() const { |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 196 | switch (Level) { |
| 197 | default: return false; |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 198 | case SSP::ALL: return true; |
| 199 | case SSP::SOME: { |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 200 | const TargetData *TD = TLI->getTargetData(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 201 | |
| 202 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 203 | BasicBlock *BB = I; |
| 204 | |
| 205 | for (BasicBlock::iterator |
| 206 | II = BB->begin(), IE = BB->end(); II != IE; ++II) |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 207 | if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) { |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 208 | if (AI->isArrayAllocation()) |
| 209 | // This is a call to alloca with a variable size. Emit stack |
| 210 | // protectors. |
| 211 | return true; |
Bill Wendling | 89c5cc6 | 2008-11-06 02:38:58 +0000 | [diff] [blame] | 212 | |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 213 | if (const ArrayType *AT = dyn_cast<ArrayType>(AI->getAllocatedType())) |
Bill Wendling | 703ccfe | 2008-11-06 22:18:44 +0000 | [diff] [blame] | 214 | // If an array has more than 8 bytes of allocated space, then we |
| 215 | // emit stack protectors. |
Bill Wendling | b7c6ebc | 2008-11-07 01:23:58 +0000 | [diff] [blame] | 216 | if (SSPBufferSize <= TD->getABITypeSize(AT)) |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 217 | return true; |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 220 | |
| 221 | return false; |
| 222 | } |
| 223 | } |
| 224 | } |