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" |
| 26 | #include "llvm/ADT/APInt.h" |
| 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 | /// Level - The level of stack protection. |
| 42 | SSP::StackProtectorLevel Level; |
| 43 | |
| 44 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 45 | /// target type sizes. |
| 46 | const TargetLowering *TLI; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 47 | |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 48 | Function *F; |
| 49 | Module *M; |
| 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. |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 68 | StackProtector() : FunctionPass(&ID), Level(SSP::OFF), TLI(0) {} |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 69 | StackProtector(SSP::StackProtectorLevel lvl, const TargetLowering *tli) |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 70 | : FunctionPass(&ID), Level(lvl), TLI(tli) {} |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 71 | |
| 72 | virtual bool runOnFunction(Function &Fn); |
| 73 | }; |
| 74 | } // end anonymous namespace |
| 75 | |
| 76 | char StackProtector::ID = 0; |
| 77 | static RegisterPass<StackProtector> |
| 78 | X("stack-protector", "Insert stack protectors"); |
| 79 | |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 80 | FunctionPass *llvm::createStackProtectorPass(SSP::StackProtectorLevel lvl, |
| 81 | const TargetLowering *tli) { |
| 82 | return new StackProtector(lvl, 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 | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 94 | /// InsertStackProtectors - Insert code into the prologue and epilogue of the |
| 95 | /// function. |
| 96 | /// |
| 97 | /// - The prologue code loads and stores the stack guard onto the stack. |
| 98 | /// - The epilogue checks the value stored in the prologue against the original |
| 99 | /// value. It calls __stack_chk_fail if they differ. |
| 100 | bool StackProtector::InsertStackProtectors() { |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 101 | std::vector<BasicBlock*> ReturnBBs; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 102 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 103 | for (Function::iterator I = F->begin(); I != F->end(); ++I) |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 104 | if (isa<ReturnInst>(I->getTerminator())) |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 105 | ReturnBBs.push_back(I); |
| 106 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 107 | // If this function doesn't return, don't bother with stack protectors. |
| 108 | if (ReturnBBs.empty()) return false; |
| 109 | |
| 110 | // Insert code into the entry block that stores the __stack_chk_guard variable |
| 111 | // onto the stack. |
| 112 | BasicBlock &Entry = F->getEntryBlock(); |
| 113 | Instruction *InsertPt = &Entry.front(); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame^] | 114 | |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 115 | const PointerType *GuardTy = PointerType::getUnqual(Type::Int8Ty); |
| 116 | |
| 117 | // The global variable for the stack guard. |
| 118 | Constant *StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", GuardTy); |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 119 | LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsertPt); |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame^] | 120 | CallInst:: |
| 121 | Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_prologue), |
| 122 | LI, "", InsertPt); |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 123 | |
| 124 | // Create the basic block to jump to when the guard check fails. |
| 125 | BasicBlock *FailBB = CreateFailBB(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 126 | |
| 127 | // Loop through the basic blocks that have return instructions. Convert this: |
| 128 | // |
| 129 | // return: |
| 130 | // ... |
| 131 | // ret ... |
| 132 | // |
| 133 | // into this: |
| 134 | // |
| 135 | // return: |
| 136 | // ... |
| 137 | // %1 = load __stack_chk_guard |
| 138 | // %2 = load <stored stack guard> |
| 139 | // %3 = cmp i1 %1, %2 |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame^] | 140 | // br i1 %3, label %SP_return, label %CallStackCheckFailBlk |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 141 | // |
Bill Wendling | b7c2c12 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 142 | // SP_return: |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 143 | // ret ... |
| 144 | // |
| 145 | // CallStackCheckFailBlk: |
| 146 | // call void @__stack_chk_fail() |
| 147 | // unreachable |
| 148 | // |
| 149 | for (std::vector<BasicBlock*>::iterator |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 150 | I = ReturnBBs.begin(), E = ReturnBBs.end(); I != E; ++I) { |
| 151 | BasicBlock *BB = *I; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 152 | ReturnInst *RI = cast<ReturnInst>(BB->getTerminator()); |
| 153 | Function::iterator InsPt = BB; ++InsPt; // Insertion point for new BB. |
| 154 | |
Bill Wendling | b7c2c12 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 155 | // Split the basic block before the return instruction. |
| 156 | BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return"); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 157 | |
Bill Wendling | b7c2c12 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 158 | // Move the newly created basic block to the point right after the old basic |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 159 | // block so that it's in the "fall through" position. |
Bill Wendling | b7c2c12 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 160 | NewBB->removeFromParent(); |
| 161 | F->getBasicBlockList().insert(InsPt, NewBB); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 162 | |
Bill Wendling | b7c2c12 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 163 | // Generate the stack protector instructions in the old basic block. |
Bill Wendling | b2a4298 | 2008-11-06 02:29:10 +0000 | [diff] [blame^] | 164 | LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB); |
| 165 | CallInst *CI = CallInst:: |
| 166 | Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_epilogue), |
| 167 | "", BB); |
| 168 | ICmpInst *Cmp = new ICmpInst(CmpInst::ICMP_EQ, CI, LI1, "", BB); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 169 | BranchInst::Create(NewBB, FailBB, Cmp, BB); |
| 170 | } |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 171 | |
| 172 | return true; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /// CreateFailBB - Create a basic block to jump to when the stack protector |
| 176 | /// check fails. |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 177 | BasicBlock *StackProtector::CreateFailBB() { |
| 178 | BasicBlock *FailBB = BasicBlock::Create("CallStackCheckFailBlk", F); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 179 | Constant *StackChkFail = |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 180 | M->getOrInsertFunction("__stack_chk_fail", Type::VoidTy, NULL); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 181 | CallInst::Create(StackChkFail, "", FailBB); |
| 182 | new UnreachableInst(FailBB); |
Bill Wendling | 613f774 | 2008-11-05 00:00:21 +0000 | [diff] [blame] | 183 | return FailBB; |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /// RequiresStackProtector - Check whether or not this function needs a stack |
| 187 | /// protector based upon the stack protector level. |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 188 | bool StackProtector::RequiresStackProtector() const { |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 189 | switch (Level) { |
| 190 | default: return false; |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 191 | case SSP::ALL: return true; |
| 192 | case SSP::SOME: { |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 193 | // If the size of the local variables allocated on the stack is greater than |
| 194 | // SSPBufferSize, then we require a stack protector. |
| 195 | uint64_t StackSize = 0; |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 196 | const TargetData *TD = TLI->getTargetData(); |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 197 | |
| 198 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 199 | BasicBlock *BB = I; |
| 200 | |
| 201 | for (BasicBlock::iterator |
| 202 | II = BB->begin(), IE = BB->end(); II != IE; ++II) |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 203 | if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) { |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 204 | if (ConstantInt *CI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
Bill Wendling | fa75dc6 | 2008-11-05 00:54:27 +0000 | [diff] [blame] | 205 | const Type *Ty = AI->getAllocatedType(); |
| 206 | uint64_t TySize = TD->getABITypeSize(Ty); |
| 207 | StackSize += TySize * CI->getZExtValue(); // Total allocated size. |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 208 | |
Bill Wendling | 80a320d | 2008-11-04 21:53:09 +0000 | [diff] [blame] | 209 | if (SSPBufferSize <= StackSize) |
| 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | } |
Bill Wendling | 2b58ce5 | 2008-11-04 02:10:20 +0000 | [diff] [blame] | 214 | |
| 215 | return false; |
| 216 | } |
| 217 | } |
| 218 | } |