StackProtector.cpp [plain text]
#define DEBUG_TYPE "stack-protector"
#include "llvm/CodeGen/Passes.h"
#include "llvm/Attributes.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/Intrinsics.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetLowering.h"
using namespace llvm;
static cl::opt<unsigned>
SSPBufferSize("stack-protector-buffer-size", cl::init(8),
cl::desc("Lower bound for a buffer to be considered for "
"stack protection"));
namespace {
class StackProtector : public FunctionPass {
const TargetLowering *TLI;
Function *F;
Module *M;
bool InsertStackProtectors();
BasicBlock *CreateFailBB();
bool RequiresStackProtector() const;
public:
static char ID; StackProtector() : FunctionPass(&ID), TLI(0) {}
StackProtector(const TargetLowering *tli)
: FunctionPass(&ID), TLI(tli) {}
virtual bool runOnFunction(Function &Fn);
};
}
char StackProtector::ID = 0;
static RegisterPass<StackProtector>
X("stack-protector", "Insert stack protectors");
FunctionPass *llvm::createStackProtectorPass(const TargetLowering *tli) {
return new StackProtector(tli);
}
bool StackProtector::runOnFunction(Function &Fn) {
F = &Fn;
M = F->getParent();
if (!RequiresStackProtector()) return false;
return InsertStackProtectors();
}
bool StackProtector::RequiresStackProtector() const {
if (F->hasFnAttr(Attribute::StackProtectReq))
return true;
if (!F->hasFnAttr(Attribute::StackProtect))
return false;
const TargetData *TD = TLI->getTargetData();
for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
BasicBlock *BB = I;
for (BasicBlock::iterator
II = BB->begin(), IE = BB->end(); II != IE; ++II)
if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
if (AI->isArrayAllocation())
return true;
if (const ArrayType *AT = dyn_cast<ArrayType>(AI->getAllocatedType())) {
if (!AT->getElementType()->isIntegerTy(8))
continue;
if (SSPBufferSize <= TD->getTypeAllocSize(AT))
return true;
}
}
}
return false;
}
bool StackProtector::InsertStackProtectors() {
BasicBlock *FailBB = 0; AllocaInst *AI = 0; Constant *StackGuardVar = 0;
for (Function::iterator I = F->begin(), E = F->end(); I != E; ) {
BasicBlock *BB = I++;
ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
if (!RI) continue;
if (!FailBB) {
PointerType *PtrTy = PointerType::getUnqual(
Type::getInt8Ty(RI->getContext()));
StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
BasicBlock &Entry = F->getEntryBlock();
Instruction *InsPt = &Entry.front();
AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt);
LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt);
Value *Args[] = { LI, AI };
CallInst::
Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector),
&Args[0], array_endof(Args), "", InsPt);
FailBB = CreateFailBB();
}
BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return");
BB->getTerminator()->eraseFromParent();
NewBB->moveAfter(BB);
LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB);
LoadInst *LI2 = new LoadInst(AI, "", true, BB);
ICmpInst *Cmp = new ICmpInst(*BB, CmpInst::ICMP_EQ, LI1, LI2, "");
BranchInst::Create(NewBB, FailBB, Cmp, BB);
}
if (!FailBB) return false;
return true;
}
BasicBlock *StackProtector::CreateFailBB() {
BasicBlock *FailBB = BasicBlock::Create(F->getContext(),
"CallStackCheckFailBlk", F);
Constant *StackChkFail =
M->getOrInsertFunction("__stack_chk_fail",
Type::getVoidTy(F->getContext()), NULL);
CallInst::Create(StackChkFail, "", FailBB);
new UnreachableInst(F->getContext(), FailBB);
return FailBB;
}