LLVM API Documentation
00001 //===-- StackProtector.cpp - Stack Protector Insertion --------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This pass inserts stack protectors into functions which need them. A variable 00011 // with a random value in it is stored onto the stack before the local variables 00012 // are allocated. Upon exiting the block, the stored value is checked. If it's 00013 // changed, then there was some sort of violation and the program aborts. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #define DEBUG_TYPE "stack-protector" 00018 #include "llvm/CodeGen/Passes.h" 00019 #include "llvm/Attributes.h" 00020 #include "llvm/Constants.h" 00021 #include "llvm/DerivedTypes.h" 00022 #include "llvm/Function.h" 00023 #include "llvm/Instructions.h" 00024 #include "llvm/Intrinsics.h" 00025 #include "llvm/Module.h" 00026 #include "llvm/Pass.h" 00027 #include "llvm/Support/CommandLine.h" 00028 #include "llvm/Target/TargetData.h" 00029 #include "llvm/Target/TargetLowering.h" 00030 using namespace llvm; 00031 00032 // SSPBufferSize - The lower bound for a buffer to be considered for stack 00033 // smashing protection. 00034 static cl::opt<unsigned> 00035 SSPBufferSize("stack-protector-buffer-size", cl::init(8), 00036 cl::desc("Lower bound for a buffer to be considered for " 00037 "stack protection")); 00038 00039 namespace { 00040 class VISIBILITY_HIDDEN StackProtector : public FunctionPass { 00041 /// TLI - Keep a pointer of a TargetLowering to consult for determining 00042 /// target type sizes. 00043 const TargetLowering *TLI; 00044 00045 Function *F; 00046 Module *M; 00047 00048 /// InsertStackProtectors - Insert code into the prologue and epilogue of 00049 /// the function. 00050 /// 00051 /// - The prologue code loads and stores the stack guard onto the stack. 00052 /// - The epilogue checks the value stored in the prologue against the 00053 /// original value. It calls __stack_chk_fail if they differ. 00054 bool InsertStackProtectors(); 00055 00056 /// CreateFailBB - Create a basic block to jump to when the stack protector 00057 /// check fails. 00058 BasicBlock *CreateFailBB(); 00059 00060 /// RequiresStackProtector - Check whether or not this function needs a 00061 /// stack protector based upon the stack protector level. 00062 bool RequiresStackProtector() const; 00063 public: 00064 static char ID; // Pass identification, replacement for typeid. 00065 StackProtector() : FunctionPass(&ID), TLI(0) {} 00066 StackProtector(const TargetLowering *tli) 00067 : FunctionPass(&ID), TLI(tli) {} 00068 00069 virtual bool runOnFunction(Function &Fn); 00070 }; 00071 } // end anonymous namespace 00072 00073 char StackProtector::ID = 0; 00074 static RegisterPass<StackProtector> 00075 X("stack-protector", "Insert stack protectors"); 00076 00077 FunctionPass *llvm::createStackProtectorPass(const TargetLowering *tli) { 00078 return new StackProtector(tli); 00079 } 00080 00081 bool StackProtector::runOnFunction(Function &Fn) { 00082 F = &Fn; 00083 M = F->getParent(); 00084 00085 if (!RequiresStackProtector()) return false; 00086 00087 return InsertStackProtectors(); 00088 } 00089 00090 /// RequiresStackProtector - Check whether or not this function needs a stack 00091 /// protector based upon the stack protector level. The heuristic we use is to 00092 /// add a guard variable to functions that call alloca, and functions with 00093 /// buffers larger than SSPBufferSize bytes. 00094 bool StackProtector::RequiresStackProtector() const { 00095 if (F->hasFnAttr(Attribute::StackProtectReq)) 00096 return true; 00097 00098 if (!F->hasFnAttr(Attribute::StackProtect)) 00099 return false; 00100 00101 const TargetData *TD = TLI->getTargetData(); 00102 00103 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { 00104 BasicBlock *BB = I; 00105 00106 for (BasicBlock::iterator 00107 II = BB->begin(), IE = BB->end(); II != IE; ++II) 00108 if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 00109 if (AI->isArrayAllocation()) 00110 // This is a call to alloca with a variable size. Emit stack 00111 // protectors. 00112 return true; 00113 00114 if (const ArrayType *AT = dyn_cast<ArrayType>(AI->getAllocatedType())) 00115 // If an array has more than SSPBufferSize bytes of allocated space, 00116 // then we emit stack protectors. 00117 if (SSPBufferSize <= TD->getTypeAllocSize(AT)) 00118 return true; 00119 } 00120 } 00121 00122 return false; 00123 } 00124 00125 /// InsertStackProtectors - Insert code into the prologue and epilogue of the 00126 /// function. 00127 /// 00128 /// - The prologue code loads and stores the stack guard onto the stack. 00129 /// - The epilogue checks the value stored in the prologue against the original 00130 /// value. It calls __stack_chk_fail if they differ. 00131 bool StackProtector::InsertStackProtectors() { 00132 BasicBlock *FailBB = 0; // The basic block to jump to if check fails. 00133 AllocaInst *AI = 0; // Place on stack that stores the stack guard. 00134 Constant *StackGuardVar = 0; // The stack guard variable. 00135 00136 for (Function::iterator I = F->begin(), E = F->end(); I != E; ) { 00137 BasicBlock *BB = I++; 00138 00139 ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()); 00140 if (!RI) continue; 00141 00142 if (!FailBB) { 00143 // Insert code into the entry block that stores the __stack_chk_guard 00144 // variable onto the stack: 00145 // 00146 // entry: 00147 // StackGuardSlot = alloca i8* 00148 // StackGuard = load __stack_chk_guard 00149 // call void @llvm.stackprotect.create(StackGuard, StackGuardSlot) 00150 // 00151 PointerType *PtrTy = PointerType::getUnqual( 00152 Type::getInt8Ty(RI->getContext())); 00153 StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy); 00154 00155 BasicBlock &Entry = F->getEntryBlock(); 00156 Instruction *InsPt = &Entry.front(); 00157 00158 AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt); 00159 LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt); 00160 00161 Value *Args[] = { LI, AI }; 00162 CallInst:: 00163 Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), 00164 &Args[0], array_endof(Args), "", InsPt); 00165 00166 // Create the basic block to jump to when the guard check fails. 00167 FailBB = CreateFailBB(); 00168 } 00169 00170 // For each block with a return instruction, convert this: 00171 // 00172 // return: 00173 // ... 00174 // ret ... 00175 // 00176 // into this: 00177 // 00178 // return: 00179 // ... 00180 // %1 = load __stack_chk_guard 00181 // %2 = load StackGuardSlot 00182 // %3 = cmp i1 %1, %2 00183 // br i1 %3, label %SP_return, label %CallStackCheckFailBlk 00184 // 00185 // SP_return: 00186 // ret ... 00187 // 00188 // CallStackCheckFailBlk: 00189 // call void @__stack_chk_fail() 00190 // unreachable 00191 00192 // Split the basic block before the return instruction. 00193 BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return"); 00194 00195 // Remove default branch instruction to the new BB. 00196 BB->getTerminator()->eraseFromParent(); 00197 00198 // Move the newly created basic block to the point right after the old basic 00199 // block so that it's in the "fall through" position. 00200 NewBB->moveAfter(BB); 00201 00202 // Generate the stack protector instructions in the old basic block. 00203 LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB); 00204 LoadInst *LI2 = new LoadInst(AI, "", true, BB); 00205 ICmpInst *Cmp = new ICmpInst(*BB, CmpInst::ICMP_EQ, LI1, LI2, ""); 00206 BranchInst::Create(NewBB, FailBB, Cmp, BB); 00207 } 00208 00209 // Return if we didn't modify any basic blocks. I.e., there are no return 00210 // statements in the function. 00211 if (!FailBB) return false; 00212 00213 return true; 00214 } 00215 00216 /// CreateFailBB - Create a basic block to jump to when the stack protector 00217 /// check fails. 00218 BasicBlock *StackProtector::CreateFailBB() { 00219 BasicBlock *FailBB = BasicBlock::Create(F->getContext(), 00220 "CallStackCheckFailBlk", F); 00221 Constant *StackChkFail = 00222 M->getOrInsertFunction("__stack_chk_fail", 00223 Type::getVoidTy(F->getContext()), NULL); 00224 CallInst::Create(StackChkFail, "", FailBB); 00225 new UnreachableInst(F->getContext(), FailBB); 00226 return FailBB; 00227 }