StackProtector.cpp revision 5734450d40fe5546eb98c0d856d73c3321eaf5d2
1e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner//===-- StackProtector.cpp - Stack Protector Insertion --------------------===//
22cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//
32cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//                     The LLVM Compiler Infrastructure
42cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//
52cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// This file is distributed under the University of Illinois Open Source
62cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// License. See LICENSE.TXT for details.
72cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//
82cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//===----------------------------------------------------------------------===//
92cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//
102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// This pass inserts stack protectors into functions which need them. A variable
112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// with a random value in it is stored onto the stack before the local variables
122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// are allocated. Upon exiting the block, the stored value is checked. If it's
132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// changed, then there was some sort of violation and the program aborts.
142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor//
15e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor//===----------------------------------------------------------------------===//
161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define DEBUG_TYPE "stack-protector"
182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "llvm/CodeGen/Passes.h"
192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "llvm/Attributes.h"
200b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor#include "llvm/Constants.h"
212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "llvm/DerivedTypes.h"
22a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#include "llvm/Function.h"
2377f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl#include "llvm/Instructions.h"
247c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner#include "llvm/Intrinsics.h"
256a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor#include "llvm/Module.h"
267c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner#include "llvm/Pass.h"
2783d63c78810556d26b62ac4cbae2eda6cdd2570cSteve Naroff#include "llvm/Support/CommandLine.h"
2814f79002e58556798e86168c63e48d533287eda5Douglas Gregor#include "llvm/Target/TargetData.h"
293251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor#include "llvm/Target/TargetLowering.h"
3014f79002e58556798e86168c63e48d533287eda5Douglas Gregorusing namespace llvm;
31bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor
322bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor// SSPBufferSize - The lower bound for a buffer to be considered for stack
33ab41e63821dc60ad144d0684df8d79a9eef86b75Douglas Gregor// smashing protection.
3417fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregorstatic cl::opt<unsigned>
3517fc223395d51be582fc666bb6ea21bd1dff26dcDouglas GregorSSPBufferSize("stack-protector-buffer-size", cl::init(8),
362596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar              cl::desc("Lower bound for a buffer to be considered for "
372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                       "stack protection"));
3814f79002e58556798e86168c63e48d533287eda5Douglas Gregor
39b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregornamespace {
403c304bd9ec2b4611572d4cbae9e1727bbecb5dc9Chris Lattner  class VISIBILITY_HIDDEN StackProtector : public FunctionPass {
412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    /// TLI - Keep a pointer of a TargetLowering to consult for determining
422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    /// target type sizes.
43ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl    const TargetLowering *TLI;
44ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl
45ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl    Function *F;
46ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl    Module *M;
47ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl
48ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl    /// InsertStackProtectors - Insert code into the prologue and epilogue of
49ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl    /// the function.
50ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl    ///
51ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl    ///  - The prologue code loads and stores the stack guard onto the stack.
522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ///  - The epilogue checks the value stored in the prologue against the
532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ///    original value. It calls __stack_chk_fail if they differ.
542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    bool InsertStackProtectors();
5512b1c7615d4f9a2edc544be499f895f16ac100edChris Lattner
562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    /// CreateFailBB - Create a basic block to jump to when the stack protector
57bd21828179a61bd0954b082825cfb8a93345f602Benjamin Kramer    /// check fails.
582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    BasicBlock *CreateFailBB();
592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    /// RequiresStackProtector - Check whether or not this function needs a
612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    /// stack protector based upon the stack protector level.
622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    bool RequiresStackProtector() const;
632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  public:
642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    static char ID;             // Pass identification, replacement for typeid.
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    StackProtector() : FunctionPass(&ID), TLI(0) {}
664fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor    StackProtector(const TargetLowering *tli)
672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      : FunctionPass(&ID), TLI(tli) {}
682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    virtual bool runOnFunction(Function &Fn);
702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  };
712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor} // end anonymous namespace
722cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
732cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorchar StackProtector::ID = 0;
742cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorstatic RegisterPass<StackProtector>
752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorX("stack-protector", "Insert stack protectors");
762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorFunctionPass *llvm::createStackProtectorPass(const TargetLowering *tli) {
782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return new StackProtector(tli);
792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
802cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorbool StackProtector::runOnFunction(Function &Fn) {
822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  F = &Fn;
832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  M = F->getParent();
842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!RequiresStackProtector()) return false;
862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return InsertStackProtectors();
882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// RequiresStackProtector - Check whether or not this function needs a stack
912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// protector based upon the stack protector level. The heuristic we use is to
922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// add a guard variable to functions that call alloca, and functions with
931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// buffers larger than SSPBufferSize bytes.
942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorbool StackProtector::RequiresStackProtector() const {
952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (F->hasFnAttr(Attribute::StackProtectReq))
962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return true;
972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (!F->hasFnAttr(Attribute::StackProtect))
992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    return false;
1002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  const TargetData *TD = TLI->getTargetData();
1022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
1042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    BasicBlock *BB = I;
1052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    for (BasicBlock::iterator
1072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor           II = BB->begin(), IE = BB->end(); II != IE; ++II)
1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
1091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        if (AI->isArrayAllocation())
1102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          // This is a call to alloca with a variable size. Emit stack
1112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          // protectors.
1122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          return true;
1132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        if (const ArrayType *AT = dyn_cast<ArrayType>(AI->getAllocatedType()))
1152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          // If an array has more than SSPBufferSize bytes of allocated space,
1160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall          // then we emit stack protectors.
1172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          if (SSPBufferSize <= TD->getABITypeSize(AT))
1182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor            return true;
1192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      }
1202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
1212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return false;
1232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// InsertStackProtectors - Insert code into the prologue and epilogue of the
1262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// function.
1272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor///
1282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor///  - The prologue code loads and stores the stack guard onto the stack.
1292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor///  - The epilogue checks the value stored in the prologue against the original
1302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor///    value. It calls __stack_chk_fail if they differ.
1312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorbool StackProtector::InsertStackProtectors() {
1327e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  BasicBlock *FailBB = 0;       // The basic block to jump to if check fails.
1337e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  AllocaInst *AI = 0;           // Place on stack that stores the stack guard.
134c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor  Constant *StackGuardVar = 0;  // The stack guard variable.
1352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (Function::iterator I = F->begin(), E = F->end(); I != E; ) {
1372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    BasicBlock *BB = I++;
1382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
1402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    if (!RI) continue;
141788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner
1422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    if (!FailBB) {
1432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      // Insert code into the entry block that stores the __stack_chk_guard
1442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      // variable onto the stack:
1452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      //
1462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      //   entry:
1472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      //     StackGuardSlot = alloca i8*
1482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      //     StackGuard = load __stack_chk_guard
1492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      //     call void @llvm.stackprotect.create(StackGuard, StackGuardSlot)
1502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      //
1512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      PointerType *PtrTy = PointerType::getUnqual(Type::Int8Ty);
152264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
153264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
154425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola      BasicBlock &Entry = F->getEntryBlock();
155ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor      Instruction *InsPt = &Entry.front();
156264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
1572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      AI = new AllocaInst(PtrTy, "StackGuardSlot", InsPt);
1582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsPt);
1592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      Value *Args[] = { LI, AI };
1612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      CallInst::
1622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector),
1632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor               &Args[0], array_endof(Args), "", InsPt);
1642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      // Create the basic block to jump to when the guard check fails.
1662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      FailBB = CreateFailBB();
1672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    }
1682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // For each block with a return instruction, convert this:
1702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    //
171465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    //   return:
172465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    //     ...
173465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    //     ret ...
174465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    //
175465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    // into this:
1762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    //
1772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    //   return:
1782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    //     ...
179ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    //     %1 = load __stack_chk_guard
180ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    //     %2 = load StackGuardSlot
181ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    //     %3 = cmp i1 %1, %2
182ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    //     br i1 %3, label %SP_return, label %CallStackCheckFailBlk
183ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    //
1842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    //   SP_return:
1852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    //     ret ...
1869763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    //
1879763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    //   CallStackCheckFailBlk:
1882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    //     call void @__stack_chk_fail()
1892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    //     unreachable
1902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // Split the basic block before the return instruction.
192c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor    BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return");
1932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // Move the newly created basic block to the point right after the old basic
1952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // block so that it's in the "fall through" position.
1962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    NewBB->moveAfter(BB);
1972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    // Generate the stack protector instructions in the old basic block.
1992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB);
2002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    LoadInst *LI2 = new LoadInst(AI, "", true, BB);
201395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    ICmpInst *Cmp = new ICmpInst(CmpInst::ICMP_EQ, LI1, LI2, "", BB);
202395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    BranchInst::Create(NewBB, FailBB, Cmp, BB);
203395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  }
204395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
205395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  // Return if we didn't modify any basic blocks. I.e., there are no return
2062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // statements in the function.
207be191100e034b23a3e13053757a57b7f5068c24aArgyrios Kyrtzidis  if (!FailBB) return false;
2082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return true;
2102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// CreateFailBB - Create a basic block to jump to when the stack protector
2132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/// check fails.
2142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorBasicBlock *StackProtector::CreateFailBB() {
2152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  BasicBlock *FailBB = BasicBlock::Create("CallStackCheckFailBlk", F);
2162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Constant *StackChkFail =
2172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    M->getOrInsertFunction("__stack_chk_fail", Type::VoidTy, NULL);
2182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  CallInst::Create(StackChkFail, "", FailBB);
2192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  new UnreachableInst(FailBB);
2202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  return FailBB;
2212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor