StackProtector.cpp revision 777d2306b36816a53bc1ae1244c0dc7d998ae691
1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//===-- StackProtector.cpp - Stack Protector Insertion --------------------===//
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// License. See LICENSE.TXT for details.
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//===----------------------------------------------------------------------===//
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This pass inserts stack protectors into functions which need them. A variable
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// with a random value in it is stored onto the stack before the local variables
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// are allocated. Upon exiting the block, the stored value is checked. If it's
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// changed, then there was some sort of violation and the program aborts.
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===----------------------------------------------------------------------===//
163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define DEBUG_TYPE "stack-protector"
1846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "llvm/CodeGen/Passes.h"
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "llvm/Attributes.h"
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "llvm/Constants.h"
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "llvm/DerivedTypes.h"
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "llvm/Function.h"
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "llvm/Instructions.h"
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "llvm/Intrinsics.h"
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "llvm/Module.h"
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "llvm/Pass.h"
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "llvm/Support/CommandLine.h"
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "llvm/Target/TargetData.h"
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "llvm/Target/TargetLowering.h"
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)using namespace llvm;
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// SSPBufferSize - The lower bound for a buffer to be considered for stack
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// smashing protection.
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)static cl::opt<unsigned>
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)SSPBufferSize("stack-protector-buffer-size", cl::init(8),
3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              cl::desc("Lower bound for a buffer to be considered for "
3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                       "stack protection"));
3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace {
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  class VISIBILITY_HIDDEN StackProtector : public FunctionPass {
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    /// TLI - Keep a pointer of a TargetLowering to consult for determining
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    /// target type sizes.
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const TargetLowering *TLI;
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    Function *F;
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    Module *M;
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    /// InsertStackProtectors - Insert code into the prologue and epilogue of
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    /// the function.
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ///
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ///  - The prologue code loads and stores the stack guard onto the stack.
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ///  - The epilogue checks the value stored in the prologue against the
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ///    original value. It calls __stack_chk_fail if they differ.
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    bool InsertStackProtectors();
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    /// CreateFailBB - Create a basic block to jump to when the stack protector
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    /// check fails.
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    BasicBlock *CreateFailBB();
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    /// RequiresStackProtector - Check whether or not this function needs a
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    /// stack protector based upon the stack protector level.
6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    bool RequiresStackProtector() const;
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  public:
6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    static char ID;             // Pass identification, replacement for typeid.
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    StackProtector() : FunctionPass(&ID), TLI(0) {}
6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    StackProtector(const TargetLowering *tli)
6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      : FunctionPass(&ID), TLI(tli) {}
6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    virtual bool runOnFunction(Function &Fn);
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  };
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // end anonymous namespace
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)char StackProtector::ID = 0;
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)static RegisterPass<StackProtector>
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)X("stack-protector", "Insert stack protectors");
7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)FunctionPass *llvm::createStackProtectorPass(const TargetLowering *tli) {
7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return new StackProtector(tli);
7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool StackProtector::runOnFunction(Function &Fn) {
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  F = &Fn;
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  M = F->getParent();
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (!RequiresStackProtector()) return false;
863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return InsertStackProtectors();
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)/// RequiresStackProtector - Check whether or not this function needs a stack
9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)/// protector based upon the stack protector level. The heuristic we use is to
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)/// add a guard variable to functions that call alloca, and functions with
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// buffers larger than SSPBufferSize bytes.
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool StackProtector::RequiresStackProtector() const {
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (F->hasFnAttr(Attribute::StackProtectReq))
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return true;
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!F->hasFnAttr(Attribute::StackProtect))
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return false;
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const TargetData *TD = TLI->getTargetData();
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    BasicBlock *BB = I;
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for (BasicBlock::iterator
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           II = BB->begin(), IE = BB->end(); II != IE; ++II)
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (AI->isArrayAllocation())
11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          // This is a call to alloca with a variable size. Emit stack
11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          // protectors.
11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          return true;
11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        if (const ArrayType *AT = dyn_cast<ArrayType>(AI->getAllocatedType()))
11590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          // If an array has more than SSPBufferSize bytes of allocated space,
11690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          // then we emit stack protectors.
11790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)          if (SSPBufferSize <= TD->getTypeAllocSize(AT))
11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            return true;
11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      }
12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return false;
12390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)/// InsertStackProtectors - Insert code into the prologue and epilogue of the
12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)/// function.
12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)///
12890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)///  - The prologue code loads and stores the stack guard onto the stack.
12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)///  - The epilogue checks the value stored in the prologue against the original
13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)///    value. It calls __stack_chk_fail if they differ.
13190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool StackProtector::InsertStackProtectors() {
13290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  BasicBlock *FailBB = 0;       // The basic block to jump to if check fails.
13390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  AllocaInst *AI = 0;           // Place on stack that stores the stack guard.
13490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  Constant *StackGuardVar = 0;  // The stack guard variable.
13590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
13690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  for (Function::iterator I = F->begin(), E = F->end(); I != E; ) {
13790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    BasicBlock *BB = I++;
13890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
13990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
14090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!RI) continue;
14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
14290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    if (!FailBB) {
14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Insert code into the entry block that stores the __stack_chk_guard
14490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // variable onto the stack:
14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      //
14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      //   entry:
14790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      //     StackGuardSlot = alloca i8*
14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      //     StackGuard = load __stack_chk_guard
14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      //     call void @llvm.stackprotect.create(StackGuard, StackGuardSlot)
15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      //
15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      PointerType *PtrTy = PointerType::getUnqual(Type::Int8Ty);
15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      BasicBlock &Entry = F->getEntryBlock();
15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      Instruction *InsPt = &Entry.front();
15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
15790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt);
15890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt);
15990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      Value *Args[] = { LI, AI };
16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      CallInst::
16290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector),
16390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)               &Args[0], array_endof(Args), "", InsPt);
16490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
16590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      // Create the basic block to jump to when the guard check fails.
16690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      FailBB = CreateFailBB();
16790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    }
16890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
16990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // For each block with a return instruction, convert this:
17090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //
17190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //   return:
17290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //     ...
17390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //     ret ...
17490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    //
17590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // into this:
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //   return:
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //     ...
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //     %1 = load __stack_chk_guard
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //     %2 = load StackGuardSlot
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //     %3 = cmp i1 %1, %2
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //     br i1 %3, label %SP_return, label %CallStackCheckFailBlk
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //   SP_return:
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //     ret ...
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //   CallStackCheckFailBlk:
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //     call void @__stack_chk_fail()
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //     unreachable
190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
191116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Split the basic block before the return instruction.
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return");
193116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
194116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Remove default branch instruction to the new BB.
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    BB->getTerminator()->eraseFromParent();
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Move the newly created basic block to the point right after the old basic
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // block so that it's in the "fall through" position.
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    NewBB->moveAfter(BB);
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Generate the stack protector instructions in the old basic block.
202010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB);
203010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    LoadInst *LI2 = new LoadInst(AI, "", true, BB);
204010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    ICmpInst *Cmp = new ICmpInst(CmpInst::ICMP_EQ, LI1, LI2, "", BB);
205010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    BranchInst::Create(NewBB, FailBB, Cmp, BB);
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Return if we didn't modify any basic blocks. I.e., there are no return
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // statements in the function.
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!FailBB) return false;
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return true;
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// CreateFailBB - Create a basic block to jump to when the stack protector
2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// check fails.
2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)BasicBlock *StackProtector::CreateFailBB() {
2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  BasicBlock *FailBB = BasicBlock::Create("CallStackCheckFailBlk", F);
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Constant *StackChkFail =
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    M->getOrInsertFunction("__stack_chk_fail", Type::VoidTy, NULL);
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  CallInst::Create(StackChkFail, "", FailBB);
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  new UnreachableInst(FailBB);
2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return FailBB;
224010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
225010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)