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