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