[stackprotector] Modernize code with IRBuilder

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190317 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Benjamin Kramer 2013-09-09 17:38:01 +00:00
parent b57d99694b
commit 54cf1413ac

View File

@ -29,6 +29,7 @@
#include "llvm/IR/Function.h" #include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h" #include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h" #include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h" #include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h" #include "llvm/IR/Intrinsics.h"
@ -134,7 +135,7 @@ bool StackProtector::runOnFunction(Function &Fn) {
Fn.getAttributes().getAttribute(AttributeSet::FunctionIndex, Fn.getAttributes().getAttribute(AttributeSet::FunctionIndex,
"stack-protector-buffer-size"); "stack-protector-buffer-size");
if (Attr.isStringAttribute()) if (Attr.isStringAttribute())
SSPBufferSize = atoi(Attr.getValueAsString().data()); Attr.getValueAsString().getAsInteger(10, SSPBufferSize);
++NumFunProtected; ++NumFunProtected;
return InsertStackProtectors(); return InsertStackProtectors();
@ -365,16 +366,11 @@ static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI,
StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy); StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
} }
BasicBlock &Entry = F->getEntryBlock(); IRBuilder<> B(&F->getEntryBlock().front());
Instruction *InsPt = &Entry.front(); AI = B.CreateAlloca(PtrTy, 0, "StackGuardSlot");
LoadInst *LI = B.CreateLoad(StackGuardVar, "StackGuard");
AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt); B.CreateCall2(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), LI,
LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt); AI);
Value *Args[] = { LI, AI };
CallInst::
Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector),
Args, "", InsPt);
return SupportsSelectionDAGSP; return SupportsSelectionDAGSP;
} }
@ -420,8 +416,7 @@ bool StackProtector::InsertStackProtectors() {
Function *Intrinsic = Function *Intrinsic =
Intrinsic::getDeclaration(M, Intrinsic::stackprotectorcheck); Intrinsic::getDeclaration(M, Intrinsic::stackprotectorcheck);
Value *Args[] = { StackGuardVar }; CallInst::Create(Intrinsic, StackGuardVar, "", InsertionPt);
CallInst::Create(Intrinsic, Args, "", InsertionPt);
} else { } else {
// If we do not support SelectionDAG based tail calls, generate IR level // If we do not support SelectionDAG based tail calls, generate IR level
@ -471,10 +466,11 @@ bool StackProtector::InsertStackProtectors() {
NewBB->moveAfter(BB); NewBB->moveAfter(BB);
// Generate the stack protector instructions in the old basic block. // Generate the stack protector instructions in the old basic block.
LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB); IRBuilder<> B(BB);
LoadInst *LI2 = new LoadInst(AI, "", true, BB); LoadInst *LI1 = B.CreateLoad(StackGuardVar);
ICmpInst *Cmp = new ICmpInst(*BB, CmpInst::ICMP_EQ, LI1, LI2, ""); LoadInst *LI2 = B.CreateLoad(AI);
BranchInst::Create(NewBB, FailBB, Cmp, BB); Value *Cmp = B.CreateICmpEQ(LI1, LI2);
B.CreateCondBr(Cmp, NewBB, FailBB);
} }
} }
@ -491,29 +487,18 @@ bool StackProtector::InsertStackProtectors() {
BasicBlock *StackProtector::CreateFailBB() { BasicBlock *StackProtector::CreateFailBB() {
LLVMContext &Context = F->getContext(); LLVMContext &Context = F->getContext();
BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F); BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F);
IRBuilder<> B(FailBB);
if (Trip.getOS() == llvm::Triple::OpenBSD) { if (Trip.getOS() == llvm::Triple::OpenBSD) {
Constant *StackChkFail = M->getOrInsertFunction( Constant *StackChkFail = M->getOrInsertFunction(
"__stack_smash_handler", Type::getVoidTy(Context), "__stack_smash_handler", Type::getVoidTy(Context),
Type::getInt8PtrTy(Context), NULL); Type::getInt8PtrTy(Context), NULL);
Constant *NameStr = ConstantDataArray::getString(Context, F->getName()); B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
Constant *FuncName =
new GlobalVariable(*M, NameStr->getType(), true,
GlobalVariable::PrivateLinkage, NameStr, "SSH");
SmallVector<Constant *, 2> IdxList;
IdxList.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0));
IdxList.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0));
SmallVector<Value *, 1> Args;
Args.push_back(ConstantExpr::getGetElementPtr(FuncName, IdxList));
CallInst::Create(StackChkFail, Args, "", FailBB);
} else { } else {
Constant *StackChkFail = M->getOrInsertFunction( Constant *StackChkFail = M->getOrInsertFunction(
"__stack_chk_fail", Type::getVoidTy(Context), NULL); "__stack_chk_fail", Type::getVoidTy(Context), NULL);
CallInst::Create(StackChkFail, "", FailBB); B.CreateCall(StackChkFail);
} }
new UnreachableInst(Context, FailBB); B.CreateUnreachable();
return FailBB; return FailBB;
} }