[stackprotector] Refactored ssp prologue creation code into its own helper function.

No functionality change.

rdar://13935163

llvm-svn: 186868
This commit is contained in:
Michael Gottesman 2013-07-22 20:44:11 +00:00
parent 14dd2a656e
commit a6188f9fcd
1 changed files with 41 additions and 35 deletions

View File

@ -265,32 +265,17 @@ bool StackProtector::RequiresStackProtector() {
return false;
}
/// InsertStackProtectors - Insert code into the prologue and epilogue of the
/// function.
/// Insert code into the entry block that stores the __stack_chk_guard
/// variable onto the stack:
///
/// - The prologue code loads and stores the stack guard onto the stack.
/// - The epilogue checks the value stored in the prologue against the original
/// value. It calls __stack_chk_fail if they differ.
bool StackProtector::InsertStackProtectors() {
BasicBlock *FailBB = 0; // The basic block to jump to if check fails.
BasicBlock *FailBBDom = 0; // FailBB's dominator.
AllocaInst *AI = 0; // Place on stack that stores the stack guard.
Value *StackGuardVar = 0; // The stack guard variable.
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) {
// Insert code into the entry block that stores the __stack_chk_guard
// variable onto the stack:
//
// entry:
// StackGuardSlot = alloca i8*
// StackGuard = load __stack_chk_guard
// call void @llvm.stackprotect.create(StackGuard, StackGuardSlot)
//
/// entry:
/// StackGuardSlot = alloca i8*
/// StackGuard = load __stack_chk_guard
/// call void @llvm.stackprotect.create(StackGuard, StackGuardSlot)
///
static void CreatePrologue(Function *F, Module *M, ReturnInst *RI,
const TargetLoweringBase *TLI, const Triple &Trip,
AllocaInst *&AI, Value *&StackGuardVar) {
PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext());
unsigned AddressSpace, Offset;
if (TLI->getStackCookieLocation(AddressSpace, Offset)) {
@ -298,7 +283,8 @@ bool StackProtector::InsertStackProtectors() {
ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset);
StackGuardVar = ConstantExpr::getIntToPtr(OffsetVal,
PointerType::get(PtrTy, AddressSpace));
PointerType::get(PtrTy,
AddressSpace));
} else if (Trip.getOS() == llvm::Triple::OpenBSD) {
StackGuardVar = M->getOrInsertGlobal("__guard_local", PtrTy);
cast<GlobalValue>(StackGuardVar)
@ -317,7 +303,27 @@ bool StackProtector::InsertStackProtectors() {
CallInst::
Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector),
Args, "", InsPt);
}
/// InsertStackProtectors - Insert code into the prologue and epilogue of the
/// function.
///
/// - The prologue code loads and stores the stack guard onto the stack.
/// - The epilogue checks the value stored in the prologue against the original
/// value. It calls __stack_chk_fail if they differ.
bool StackProtector::InsertStackProtectors() {
BasicBlock *FailBB = 0; // The basic block to jump to if check fails.
BasicBlock *FailBBDom = 0; // FailBB's dominator.
AllocaInst *AI = 0; // Place on stack that stores the stack guard.
Value *StackGuardVar = 0; // The stack guard variable.
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) {
CreatePrologue(F, M, RI, TLI, Trip, AI, StackGuardVar);
// Create the basic block to jump to when the guard check fails.
FailBB = CreateFailBB();
}