From 236e389be4bb7f65e78bd378143b67f401f05338 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Fri, 9 Aug 2013 21:26:18 +0000 Subject: [stackprotector] Simplify SP Pass so that we emit different fail basic blocks for each fail condition. This patch decouples the stack protector pass so that we can support stack protector implementations that do not use the IR level generated stack protector fail basic block. No codesize increase is caused by this change since the MI level tail merge pass properly merges together the fail condition blocks (see the updated test). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188105 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StackProtector.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'lib/CodeGen/StackProtector.cpp') diff --git a/lib/CodeGen/StackProtector.cpp b/lib/CodeGen/StackProtector.cpp index 4c56380..f8c9826 100644 --- a/lib/CodeGen/StackProtector.cpp +++ b/lib/CodeGen/StackProtector.cpp @@ -312,8 +312,7 @@ static void CreatePrologue(Function *F, Module *M, ReturnInst *RI, /// - 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. + bool HasPrologue = false; AllocaInst *AI = 0; // Place on stack that stores the stack guard. Value *StackGuardVar = 0; // The stack guard variable. @@ -322,10 +321,9 @@ bool StackProtector::InsertStackProtectors() { ReturnInst *RI = dyn_cast(BB->getTerminator()); if (!RI) continue; - if (!FailBB) { + if (!HasPrologue) { + HasPrologue = true; CreatePrologue(F, M, RI, TLI, Trip, AI, StackGuardVar); - // Create the basic block to jump to when the guard check fails. - FailBB = CreateFailBB(); } // For each block with a return instruction, convert this: @@ -350,12 +348,16 @@ bool StackProtector::InsertStackProtectors() { // call void @__stack_chk_fail() // unreachable + // Create the fail basic block. + BasicBlock *FailBB = CreateFailBB(); + // Split the basic block before the return instruction. BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return"); + // Update the dominator tree if we need to. if (DT && DT->isReachableFromEntry(BB)) { DT->addNewBlock(NewBB, BB); - FailBBDom = FailBBDom ? DT->findNearestCommonDominator(FailBBDom, BB) :BB; + DT->addNewBlock(FailBB, BB); } // Remove default branch instruction to the new BB. @@ -374,10 +376,8 @@ bool StackProtector::InsertStackProtectors() { // Return if we didn't modify any basic blocks. I.e., there are no return // statements in the function. - if (!FailBB) return false; - - if (DT && FailBBDom) - DT->addNewBlock(FailBB, FailBBDom); + if (!HasPrologue) + return false; return true; } -- cgit v1.1 From 3480d1b84e0bdea91c08dcd931fe86b562971f3d Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Tue, 20 Aug 2013 08:36:53 +0000 Subject: [stackprotector] Added support for emitting the llvm intrinsic stack protector check. rdar://13935163 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188766 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StackProtector.cpp | 199 +++++++++++++++++++++++++++++++---------- 1 file changed, 150 insertions(+), 49 deletions(-) (limited to 'lib/CodeGen/StackProtector.cpp') diff --git a/lib/CodeGen/StackProtector.cpp b/lib/CodeGen/StackProtector.cpp index f8c9826..cca5e61 100644 --- a/lib/CodeGen/StackProtector.cpp +++ b/lib/CodeGen/StackProtector.cpp @@ -15,11 +15,13 @@ //===----------------------------------------------------------------------===// #define DEBUG_TYPE "stack-protector" +#include "llvm/CodeGen/Analysis.h" #include "llvm/CodeGen/Passes.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/Statistic.h" #include "llvm/ADT/Triple.h" #include "llvm/Analysis/Dominators.h" +#include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/Attributes.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DataLayout.h" @@ -28,6 +30,7 @@ #include "llvm/IR/GlobalValue.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Instructions.h" +#include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/Module.h" #include "llvm/Pass.h" @@ -40,6 +43,10 @@ STATISTIC(NumFunProtected, "Number of functions protected"); STATISTIC(NumAddrTaken, "Number of local variables that have their address" " taken."); +static cl::opt +EnableSelectionDAGSP("enable-selectiondag-sp", cl::init(true), + cl::Hidden); + namespace { class StackProtector : public FunctionPass { const TargetMachine *TM; @@ -265,6 +272,62 @@ bool StackProtector::RequiresStackProtector() { return false; } +static bool InstructionWillNotHaveChain(const Instruction *I) { + return !I->mayHaveSideEffects() && !I->mayReadFromMemory() && + isSafeToSpeculativelyExecute(I); +} + +/// Identify if RI has a previous instruction in the "Tail Position" and return +/// it. Otherwise return 0. +/// +/// This is based off of the code in llvm::isInTailCallPosition +static CallInst *FindPotentialTailCall(BasicBlock *BB, ReturnInst *RI, + const TargetLoweringBase *TLI) { + // Establish a reasonable upper bound on the maximum amount of instructions we + // will look through to find a tail call. + unsigned SearchCounter = 0; + const unsigned MaxSearch = 4; + bool NoInterposingChain = true; + + for (BasicBlock::reverse_iterator I = llvm::next(BB->rbegin()), E = BB->rend(); + I != E && SearchCounter < MaxSearch; ++I) { + Instruction *Inst = &*I; + + // Skip over debug intrinsics and do not allow them to affect our MaxSearch + // counter. + if (isa(Inst)) + continue; + + // If we find a call and the following conditions are satisifed, then we + // have found a tail call that satisfies at least the target independent + // requirements of a tail call: + // + // 1. The call site has the tail marker. + // + // 2. The call site either will not cause the creation of a chain or if a + // chain is necessary there are no instructions in between the callsite and + // the call which would create an interposing chain. + // + // 3. The return type of the function does not impede tail call + // optimization. + if (CallInst *CI = dyn_cast(Inst)) { + if (CI->isTailCall() && + (InstructionWillNotHaveChain(CI) || NoInterposingChain) && + returnTypeIsEligibleForTailCall(BB->getParent(), CI, RI, *TLI)) + return CI; + } + + // If we did not find a call see if we have an instruction that may create + // an interposing chain. + NoInterposingChain = NoInterposingChain && InstructionWillNotHaveChain(Inst); + + // Increment max search. + SearchCounter++; + } + + return 0; +} + /// Insert code into the entry block that stores the __stack_chk_guard /// variable onto the stack: /// @@ -273,9 +336,12 @@ bool StackProtector::RequiresStackProtector() { /// StackGuard = load __stack_chk_guard /// call void @llvm.stackprotect.create(StackGuard, StackGuardSlot) /// -static void CreatePrologue(Function *F, Module *M, ReturnInst *RI, +/// Returns true if the platform/triple supports the stackprotectorcreate pseudo +/// node. +static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI, const TargetLoweringBase *TLI, const Triple &Trip, AllocaInst *&AI, Value *&StackGuardVar) { + bool SupportsSelectionDAGSP = false; PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext()); unsigned AddressSpace, Offset; if (TLI->getStackCookieLocation(AddressSpace, Offset)) { @@ -290,7 +356,8 @@ static void CreatePrologue(Function *F, Module *M, ReturnInst *RI, cast(StackGuardVar) ->setVisibility(GlobalValue::HiddenVisibility); } else { - StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy); + SupportsSelectionDAGSP = true; + StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy); } BasicBlock &Entry = F->getEntryBlock(); @@ -303,6 +370,8 @@ static void CreatePrologue(Function *F, Module *M, ReturnInst *RI, CallInst:: Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), Args, "", InsPt); + + return SupportsSelectionDAGSP; } /// InsertStackProtectors - Insert code into the prologue and epilogue of the @@ -313,6 +382,7 @@ static void CreatePrologue(Function *F, Module *M, ReturnInst *RI, /// value. It calls __stack_chk_fail if they differ. bool StackProtector::InsertStackProtectors() { bool HasPrologue = false; + bool SupportsSelectionDAGSP = false; AllocaInst *AI = 0; // Place on stack that stores the stack guard. Value *StackGuardVar = 0; // The stack guard variable. @@ -323,55 +393,86 @@ bool StackProtector::InsertStackProtectors() { if (!HasPrologue) { HasPrologue = true; - CreatePrologue(F, M, RI, TLI, Trip, AI, StackGuardVar); - } - - // For each block with a return instruction, convert this: - // - // return: - // ... - // ret ... - // - // into this: - // - // return: - // ... - // %1 = load __stack_chk_guard - // %2 = load StackGuardSlot - // %3 = cmp i1 %1, %2 - // br i1 %3, label %SP_return, label %CallStackCheckFailBlk - // - // SP_return: - // ret ... - // - // CallStackCheckFailBlk: - // call void @__stack_chk_fail() - // unreachable - - // Create the fail basic block. - BasicBlock *FailBB = CreateFailBB(); - - // Split the basic block before the return instruction. - BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return"); + SupportsSelectionDAGSP = CreatePrologue(F, M, RI, TLI, Trip, AI, + StackGuardVar); + } + + // TODO: Put in check here if platform supports the stack protector check + // intrinsic. + if (EnableSelectionDAGSP && !TM->Options.EnableFastISel && + SupportsSelectionDAGSP) { + // Since we have a potential tail call, insert the special stack check + // intrinsic. + Instruction *InsertionPt = 0; + if (CallInst *CI = FindPotentialTailCall(BB, RI, TLI)) { + InsertionPt = CI; + } else { + InsertionPt = RI; + // At this point we know that BB has a return statement so it *DOES* + // have a terminator. + assert(InsertionPt != 0 && "BB must have a terminator instruction at " + "this point."); + } - // Update the dominator tree if we need to. - if (DT && DT->isReachableFromEntry(BB)) { - DT->addNewBlock(NewBB, BB); - DT->addNewBlock(FailBB, BB); + Function *Intrinsic = + Intrinsic::getDeclaration(M, Intrinsic::stackprotectorcheck); + Value *Args[] = { StackGuardVar }; + CallInst::Create(Intrinsic, Args, "", InsertionPt); + + } else { + // If we do not have a potential tail call or our platform does not + // support lowering the stack protector check pseudo node, perform the IR + // level stack check. + + // For each block with a return instruction, convert this: + // + // return: + // ... + // ret ... + // + // into this: + // + // return: + // ... + // %1 = load __stack_chk_guard + // %2 = load StackGuardSlot + // %3 = cmp i1 %1, %2 + // br i1 %3, label %SP_return, label %CallStackCheckFailBlk + // + // SP_return: + // ret ... + // + // CallStackCheckFailBlk: + // call void @__stack_chk_fail() + // unreachable + + // Create the FailBB. We duplicate the BB every time since the MI tail + // merge pass will merge together all of the various BB into one including + // fail BB generated by the stack protector pseudo instruction. + BasicBlock *FailBB = CreateFailBB(); + + // Split the basic block before the return instruction. + BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return"); + + // Update the dominator tree if we need to. + if (DT && DT->isReachableFromEntry(BB)) { + DT->addNewBlock(NewBB, BB); + DT->addNewBlock(FailBB, BB); + } + + // Remove default branch instruction to the new BB. + BB->getTerminator()->eraseFromParent(); + + // Move the newly created basic block to the point right after the old + // basic block so that it's in the "fall through" position. + NewBB->moveAfter(BB); + + // Generate the stack protector instructions in the old basic block. + 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); } - - // Remove default branch instruction to the new BB. - BB->getTerminator()->eraseFromParent(); - - // Move the newly created basic block to the point right after the old basic - // block so that it's in the "fall through" position. - NewBB->moveAfter(BB); - - // Generate the stack protector instructions in the old basic block. - 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); } // Return if we didn't modify any basic blocks. I.e., there are no return -- cgit v1.1 From 47d6e07a9be631c582d47d8187a9073619d1c158 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Tue, 20 Aug 2013 08:46:13 +0000 Subject: [stackprotector] Removed stale TODO. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188768 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StackProtector.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'lib/CodeGen/StackProtector.cpp') diff --git a/lib/CodeGen/StackProtector.cpp b/lib/CodeGen/StackProtector.cpp index cca5e61..3e6f44c 100644 --- a/lib/CodeGen/StackProtector.cpp +++ b/lib/CodeGen/StackProtector.cpp @@ -397,8 +397,6 @@ bool StackProtector::InsertStackProtectors() { StackGuardVar); } - // TODO: Put in check here if platform supports the stack protector check - // intrinsic. if (EnableSelectionDAGSP && !TM->Options.EnableFastISel && SupportsSelectionDAGSP) { // Since we have a potential tail call, insert the special stack check @@ -420,10 +418,9 @@ bool StackProtector::InsertStackProtectors() { CallInst::Create(Intrinsic, Args, "", InsertionPt); } else { - // If we do not have a potential tail call or our platform does not - // support lowering the stack protector check pseudo node, perform the IR - // level stack check. - + // If we do not support SelectionDAG based tail calls, generate IR level + // tail calls. + // // For each block with a return instruction, convert this: // // return: -- cgit v1.1 From c02dbeb429f3a11f396c3915b638a9a525c97c62 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Tue, 20 Aug 2013 08:46:16 +0000 Subject: Removed trailing whitespace. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188769 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StackProtector.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'lib/CodeGen/StackProtector.cpp') diff --git a/lib/CodeGen/StackProtector.cpp b/lib/CodeGen/StackProtector.cpp index 3e6f44c..b45459a 100644 --- a/lib/CodeGen/StackProtector.cpp +++ b/lib/CodeGen/StackProtector.cpp @@ -66,7 +66,7 @@ namespace { unsigned SSPBufferSize; /// VisitedPHIs - The set of PHI nodes visited when determining - /// if a variable's reference has been taken. This set + /// if a variable's reference has been taken. This set /// is maintained to ensure we don't visit the same PHI node multiple /// times. SmallPtrSet VisitedPHIs; @@ -245,7 +245,7 @@ bool StackProtector::RequiresStackProtector() { // of size. if (Strong) return true; - + if (const ConstantInt *CI = dyn_cast(AI->getArraySize())) { if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) @@ -262,7 +262,7 @@ bool StackProtector::RequiresStackProtector() { return true; if (Strong && HasAddressTaken(AI)) { - ++NumAddrTaken; + ++NumAddrTaken; return true; } } @@ -347,7 +347,7 @@ static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI, if (TLI->getStackCookieLocation(AddressSpace, Offset)) { Constant *OffsetVal = ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset); - + StackGuardVar = ConstantExpr::getIntToPtr(OffsetVal, PointerType::get(PtrTy, AddressSpace)); @@ -357,15 +357,15 @@ static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI, ->setVisibility(GlobalValue::HiddenVisibility); } else { SupportsSelectionDAGSP = true; - StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy); + 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), @@ -384,7 +384,7 @@ bool StackProtector::InsertStackProtectors() { bool HasPrologue = false; bool SupportsSelectionDAGSP = false; AllocaInst *AI = 0; // Place on stack that stores the stack guard. - Value *StackGuardVar = 0; // The stack guard variable. + Value *StackGuardVar = 0; // The stack guard variable. for (Function::iterator I = F->begin(), E = F->end(); I != E; ) { BasicBlock *BB = I++; @@ -394,8 +394,8 @@ bool StackProtector::InsertStackProtectors() { if (!HasPrologue) { HasPrologue = true; SupportsSelectionDAGSP = CreatePrologue(F, M, RI, TLI, Trip, AI, - StackGuardVar); - } + StackGuardVar); + } if (EnableSelectionDAGSP && !TM->Options.EnableFastISel && SupportsSelectionDAGSP) { @@ -404,7 +404,7 @@ bool StackProtector::InsertStackProtectors() { Instruction *InsertionPt = 0; if (CallInst *CI = FindPotentialTailCall(BB, RI, TLI)) { InsertionPt = CI; - } else { + } else { InsertionPt = RI; // At this point we know that BB has a return statement so it *DOES* // have a terminator. @@ -445,25 +445,25 @@ bool StackProtector::InsertStackProtectors() { // Create the FailBB. We duplicate the BB every time since the MI tail // merge pass will merge together all of the various BB into one including - // fail BB generated by the stack protector pseudo instruction. + // fail BB generated by the stack protector pseudo instruction. BasicBlock *FailBB = CreateFailBB(); - + // Split the basic block before the return instruction. BasicBlock *NewBB = BB->splitBasicBlock(RI, "SP_return"); - + // Update the dominator tree if we need to. if (DT && DT->isReachableFromEntry(BB)) { DT->addNewBlock(NewBB, BB); DT->addNewBlock(FailBB, BB); } - + // Remove default branch instruction to the new BB. BB->getTerminator()->eraseFromParent(); - + // Move the newly created basic block to the point right after the old // basic block so that it's in the "fall through" position. NewBB->moveAfter(BB); - + // Generate the stack protector instructions in the old basic block. LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB); LoadInst *LI2 = new LoadInst(AI, "", true, BB); -- cgit v1.1 From b99272a521ecffe8d021306713bd51fafc85721e Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Tue, 20 Aug 2013 08:56:23 +0000 Subject: [stackprotector] Added significantly longer comment to FindPotentialTailCall to make clear its relationship to llvm::isInTailCallPosition. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188770 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StackProtector.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'lib/CodeGen/StackProtector.cpp') diff --git a/lib/CodeGen/StackProtector.cpp b/lib/CodeGen/StackProtector.cpp index b45459a..2cd2219 100644 --- a/lib/CodeGen/StackProtector.cpp +++ b/lib/CodeGen/StackProtector.cpp @@ -280,7 +280,12 @@ static bool InstructionWillNotHaveChain(const Instruction *I) { /// Identify if RI has a previous instruction in the "Tail Position" and return /// it. Otherwise return 0. /// -/// This is based off of the code in llvm::isInTailCallPosition +/// This is based off of the code in llvm::isInTailCallPosition. The difference +/// is that it inverts the first part of llvm::isInTailCallPosition since +/// isInTailCallPosition is checking if a call is in a tail call position, and +/// we are searching for an unknown tail call that might be in the tail call +/// position. Once we find the call though, the code uses the same refactored +/// code, returnTypeIsEligibleForTailCall. static CallInst *FindPotentialTailCall(BasicBlock *BB, ReturnInst *RI, const TargetLoweringBase *TLI) { // Establish a reasonable upper bound on the maximum amount of instructions we -- cgit v1.1 From d4f478899e6229648f94c4aa70256986cdc6ee18 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Tue, 20 Aug 2013 08:56:26 +0000 Subject: [stackprotector] Small Bit of computation hoisting. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188771 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StackProtector.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/CodeGen/StackProtector.cpp') diff --git a/lib/CodeGen/StackProtector.cpp b/lib/CodeGen/StackProtector.cpp index 2cd2219..86ca53d 100644 --- a/lib/CodeGen/StackProtector.cpp +++ b/lib/CodeGen/StackProtector.cpp @@ -387,7 +387,8 @@ static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI, /// value. It calls __stack_chk_fail if they differ. bool StackProtector::InsertStackProtectors() { bool HasPrologue = false; - bool SupportsSelectionDAGSP = false; + bool SupportsSelectionDAGSP = + EnableSelectionDAGSP && !TM->Options.EnableFastISel; AllocaInst *AI = 0; // Place on stack that stores the stack guard. Value *StackGuardVar = 0; // The stack guard variable. @@ -398,12 +399,11 @@ bool StackProtector::InsertStackProtectors() { if (!HasPrologue) { HasPrologue = true; - SupportsSelectionDAGSP = CreatePrologue(F, M, RI, TLI, Trip, AI, - StackGuardVar); + SupportsSelectionDAGSP &= CreatePrologue(F, M, RI, TLI, Trip, AI, + StackGuardVar); } - if (EnableSelectionDAGSP && !TM->Options.EnableFastISel && - SupportsSelectionDAGSP) { + if (SupportsSelectionDAGSP) { // Since we have a potential tail call, insert the special stack check // intrinsic. Instruction *InsertionPt = 0; -- cgit v1.1 From ade3075030ddd6db370649993a6da5e21e73daab Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Tue, 20 Aug 2013 08:56:28 +0000 Subject: [stackprotector] Small cleanup. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188772 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StackProtector.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/CodeGen/StackProtector.cpp') diff --git a/lib/CodeGen/StackProtector.cpp b/lib/CodeGen/StackProtector.cpp index 86ca53d..45675c9 100644 --- a/lib/CodeGen/StackProtector.cpp +++ b/lib/CodeGen/StackProtector.cpp @@ -395,7 +395,8 @@ bool StackProtector::InsertStackProtectors() { for (Function::iterator I = F->begin(), E = F->end(); I != E; ) { BasicBlock *BB = I++; ReturnInst *RI = dyn_cast(BB->getTerminator()); - if (!RI) continue; + if (!RI) + continue; if (!HasPrologue) { HasPrologue = true; -- cgit v1.1 From 54cf1413aca342753ab846d915c6a55d9c087bc6 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Mon, 9 Sep 2013 17:38:01 +0000 Subject: [stackprotector] Modernize code with IRBuilder git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190317 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StackProtector.cpp | 49 +++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) (limited to 'lib/CodeGen/StackProtector.cpp') diff --git a/lib/CodeGen/StackProtector.cpp b/lib/CodeGen/StackProtector.cpp index 45675c9..f65b617 100644 --- a/lib/CodeGen/StackProtector.cpp +++ b/lib/CodeGen/StackProtector.cpp @@ -29,6 +29,7 @@ #include "llvm/IR/Function.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/IRBuilder.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/Intrinsics.h" @@ -134,7 +135,7 @@ bool StackProtector::runOnFunction(Function &Fn) { Fn.getAttributes().getAttribute(AttributeSet::FunctionIndex, "stack-protector-buffer-size"); if (Attr.isStringAttribute()) - SSPBufferSize = atoi(Attr.getValueAsString().data()); + Attr.getValueAsString().getAsInteger(10, SSPBufferSize); ++NumFunProtected; return InsertStackProtectors(); @@ -365,16 +366,11 @@ static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI, 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, "", InsPt); + IRBuilder<> B(&F->getEntryBlock().front()); + AI = B.CreateAlloca(PtrTy, 0, "StackGuardSlot"); + LoadInst *LI = B.CreateLoad(StackGuardVar, "StackGuard"); + B.CreateCall2(Intrinsic::getDeclaration(M, Intrinsic::stackprotector), LI, + AI); return SupportsSelectionDAGSP; } @@ -420,8 +416,7 @@ bool StackProtector::InsertStackProtectors() { Function *Intrinsic = Intrinsic::getDeclaration(M, Intrinsic::stackprotectorcheck); - Value *Args[] = { StackGuardVar }; - CallInst::Create(Intrinsic, Args, "", InsertionPt); + CallInst::Create(Intrinsic, StackGuardVar, "", InsertionPt); } else { // If we do not support SelectionDAG based tail calls, generate IR level @@ -471,10 +466,11 @@ bool StackProtector::InsertStackProtectors() { NewBB->moveAfter(BB); // Generate the stack protector instructions in the old basic block. - 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); + IRBuilder<> B(BB); + LoadInst *LI1 = B.CreateLoad(StackGuardVar); + LoadInst *LI2 = B.CreateLoad(AI); + Value *Cmp = B.CreateICmpEQ(LI1, LI2); + B.CreateCondBr(Cmp, NewBB, FailBB); } } @@ -491,29 +487,18 @@ bool StackProtector::InsertStackProtectors() { BasicBlock *StackProtector::CreateFailBB() { LLVMContext &Context = F->getContext(); BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F); + IRBuilder<> B(FailBB); if (Trip.getOS() == llvm::Triple::OpenBSD) { Constant *StackChkFail = M->getOrInsertFunction( "__stack_smash_handler", Type::getVoidTy(Context), Type::getInt8PtrTy(Context), NULL); - Constant *NameStr = ConstantDataArray::getString(Context, F->getName()); - Constant *FuncName = - new GlobalVariable(*M, NameStr->getType(), true, - GlobalVariable::PrivateLinkage, NameStr, "SSH"); - - SmallVector IdxList; - IdxList.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0)); - IdxList.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0)); - - SmallVector Args; - Args.push_back(ConstantExpr::getGetElementPtr(FuncName, IdxList)); - - CallInst::Create(StackChkFail, Args, "", FailBB); + B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH")); } else { Constant *StackChkFail = M->getOrInsertFunction( "__stack_chk_fail", Type::getVoidTy(Context), NULL); - CallInst::Create(StackChkFail, "", FailBB); + B.CreateCall(StackChkFail); } - new UnreachableInst(Context, FailBB); + B.CreateUnreachable(); return FailBB; } -- cgit v1.1 From 18ebd48960afe9a4e694dac3ba0ee1002044d297 Mon Sep 17 00:00:00 2001 From: Josh Magee Date: Fri, 27 Sep 2013 21:58:43 +0000 Subject: [stackprotector] Refactor the StackProtector pass from a single .cpp file into StackProtector.h and StackProtector.cpp. No functionality change. Future patches will add analysis which will be used in other passes (PEI, StackSlot). The end goal is to support ssp-strong stack layout rules. WIP. Differential Revision: http://llvm-reviews.chandlerc.com/D1521 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@191570 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StackProtector.cpp | 71 +----------------------------------------- 1 file changed, 1 insertion(+), 70 deletions(-) (limited to 'lib/CodeGen/StackProtector.cpp') diff --git a/lib/CodeGen/StackProtector.cpp b/lib/CodeGen/StackProtector.cpp index f65b617..cb22082 100644 --- a/lib/CodeGen/StackProtector.cpp +++ b/lib/CodeGen/StackProtector.cpp @@ -15,11 +15,11 @@ //===----------------------------------------------------------------------===// #define DEBUG_TYPE "stack-protector" +#include "llvm/CodeGen/StackProtector.h" #include "llvm/CodeGen/Analysis.h" #include "llvm/CodeGen/Passes.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/Statistic.h" -#include "llvm/ADT/Triple.h" #include "llvm/Analysis/Dominators.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/Attributes.h" @@ -34,9 +34,7 @@ #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/Module.h" -#include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" -#include "llvm/Target/TargetLowering.h" #include using namespace llvm; @@ -48,73 +46,6 @@ static cl::opt EnableSelectionDAGSP("enable-selectiondag-sp", cl::init(true), cl::Hidden); -namespace { - class StackProtector : public FunctionPass { - const TargetMachine *TM; - - /// TLI - Keep a pointer of a TargetLowering to consult for determining - /// target type sizes. - const TargetLoweringBase *TLI; - const Triple Trip; - - Function *F; - Module *M; - - DominatorTree *DT; - - /// \brief The minimum size of buffers that will receive stack smashing - /// protection when -fstack-protection is used. - unsigned SSPBufferSize; - - /// VisitedPHIs - The set of PHI nodes visited when determining - /// if a variable's reference has been taken. This set - /// is maintained to ensure we don't visit the same PHI node multiple - /// times. - SmallPtrSet VisitedPHIs; - - /// 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 InsertStackProtectors(); - - /// CreateFailBB - Create a basic block to jump to when the stack protector - /// check fails. - BasicBlock *CreateFailBB(); - - /// ContainsProtectableArray - Check whether the type either is an array or - /// contains an array of sufficient size so that we need stack protectors - /// for it. - bool ContainsProtectableArray(Type *Ty, bool Strong = false, - bool InStruct = false) const; - - /// \brief Check whether a stack allocation has its address taken. - bool HasAddressTaken(const Instruction *AI); - - /// RequiresStackProtector - Check whether or not this function needs a - /// stack protector based upon the stack protector level. - bool RequiresStackProtector(); - public: - static char ID; // Pass identification, replacement for typeid. - StackProtector() : FunctionPass(ID), TM(0), TLI(0), SSPBufferSize(0) { - initializeStackProtectorPass(*PassRegistry::getPassRegistry()); - } - StackProtector(const TargetMachine *TM) - : FunctionPass(ID), TM(TM), TLI(0), Trip(TM->getTargetTriple()), - SSPBufferSize(8) { - initializeStackProtectorPass(*PassRegistry::getPassRegistry()); - } - - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addPreserved(); - } - - virtual bool runOnFunction(Function &Fn); - }; -} // end anonymous namespace - char StackProtector::ID = 0; INITIALIZE_PASS(StackProtector, "stack-protector", "Insert stack protectors", false, false) -- cgit v1.1 From 4598b40ce62dceb5ff96bbb7caeebd1ca57ae3fe Mon Sep 17 00:00:00 2001 From: Josh Magee Date: Tue, 29 Oct 2013 21:16:16 +0000 Subject: [stackprotector] Update the StackProtector pass to perform datalayout analysis. This modifies the pass to classify every SSP-triggering AllocaInst according to an SSPLayoutKind (LargeArray, SmallArray, AddrOf). This analysis is collected by the pass and made available for use, but no other pass uses it yet. The next patch will make use of this analysis in PEI and StackSlot passes. The end goal is to support ssp-strong stack layout rules. WIP. Differential Revision: http://llvm-reviews.chandlerc.com/D1789 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@193653 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StackProtector.cpp | 87 +++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 27 deletions(-) (limited to 'lib/CodeGen/StackProtector.cpp') diff --git a/lib/CodeGen/StackProtector.cpp b/lib/CodeGen/StackProtector.cpp index cb22082..d2d87b2 100644 --- a/lib/CodeGen/StackProtector.cpp +++ b/lib/CodeGen/StackProtector.cpp @@ -48,12 +48,17 @@ EnableSelectionDAGSP("enable-selectiondag-sp", cl::init(true), char StackProtector::ID = 0; INITIALIZE_PASS(StackProtector, "stack-protector", - "Insert stack protectors", false, false) + "Insert stack protectors", false, true) FunctionPass *llvm::createStackProtectorPass(const TargetMachine *TM) { return new StackProtector(TM); } +StackProtector::SSPLayoutKind StackProtector::getSSPLayout(const AllocaInst *AI) + const { + return AI ? Layout.lookup(AI) : SSPLK_None; +} + bool StackProtector::runOnFunction(Function &Fn) { F = &Fn; M = F->getParent(); @@ -72,39 +77,51 @@ bool StackProtector::runOnFunction(Function &Fn) { return InsertStackProtectors(); } -/// ContainsProtectableArray - Check whether the type either is an array or -/// contains a char array of sufficient size so that we need stack protectors -/// for it. -bool StackProtector::ContainsProtectableArray(Type *Ty, bool Strong, - bool InStruct) const { +/// \param [out] IsLarge is set to true if a protectable array is found and +/// it is "large" ( >= ssp-buffer-size). In the case of a structure with +/// multiple arrays, this gets set if any of them is large. +bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge, + bool Strong, bool InStruct) + const { if (!Ty) return false; if (ArrayType *AT = dyn_cast(Ty)) { - // In strong mode any array, regardless of type and size, triggers a - // protector - if (Strong) - return true; if (!AT->getElementType()->isIntegerTy(8)) { // If we're on a non-Darwin platform or we're inside of a structure, don't // add stack protectors unless the array is a character array. - if (InStruct || !Trip.isOSDarwin()) - return false; + // However, in strong mode any array, regardless of type and size, + // triggers a protector. + if (!Strong && (InStruct || !Trip.isOSDarwin())) + return false; } // If an array has more than SSPBufferSize bytes of allocated space, then we // emit stack protectors. - if (SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT)) + if (SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT)) { + IsLarge = true; + return true; + } + + if (Strong) + // Require a protector for all arrays in strong mode return true; } const StructType *ST = dyn_cast(Ty); if (!ST) return false; + bool NeedsProtector = false; for (StructType::element_iterator I = ST->element_begin(), E = ST->element_end(); I != E; ++I) - if (ContainsProtectableArray(*I, Strong, true)) - return true; + if (ContainsProtectableArray(*I, IsLarge, Strong, true)) { + // If the element is a protectable array and is large (>= SSPBufferSize) + // then we are done. If the protectable array is not large, then + // keep looking in case a subsequent element is a large array. + if (IsLarge) + return true; + NeedsProtector = true; + } - return false; + return NeedsProtector; } bool StackProtector::HasAddressTaken(const Instruction *AI) { @@ -156,11 +173,13 @@ bool StackProtector::HasAddressTaken(const Instruction *AI) { /// address taken. bool StackProtector::RequiresStackProtector() { bool Strong = false; + bool NeedsProtector = false; if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, - Attribute::StackProtectReq)) - return true; - else if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, - Attribute::StackProtectStrong)) + Attribute::StackProtectReq)) { + NeedsProtector = true; + Strong = true; // Use the same heuristic as strong to determine SSPLayout + } else if (F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, + Attribute::StackProtectStrong)) Strong = true; else if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, Attribute::StackProtect)) @@ -180,28 +199,42 @@ bool StackProtector::RequiresStackProtector() { if (const ConstantInt *CI = dyn_cast(AI->getArraySize())) { - if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) + if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) { // A call to alloca with size >= SSPBufferSize requires // stack protectors. - return true; + Layout.insert(std::make_pair(AI, SSPLK_LargeArray)); + NeedsProtector = true; + } else if (Strong) { + // Require protectors for all alloca calls in strong mode. + Layout.insert(std::make_pair(AI, SSPLK_SmallArray)); + NeedsProtector = true; + } } else { // A call to alloca with a variable size requires protectors. - return true; + Layout.insert(std::make_pair(AI, SSPLK_LargeArray)); + NeedsProtector = true; } + continue; } - if (ContainsProtectableArray(AI->getAllocatedType(), Strong)) - return true; + bool IsLarge = false; + if (ContainsProtectableArray(AI->getAllocatedType(), IsLarge, Strong)) { + Layout.insert(std::make_pair(AI, IsLarge ? SSPLK_LargeArray + : SSPLK_SmallArray)); + NeedsProtector = true; + continue; + } if (Strong && HasAddressTaken(AI)) { ++NumAddrTaken; - return true; + Layout.insert(std::make_pair(AI, SSPLK_AddrOf)); + NeedsProtector = true; } } } } - return false; + return NeedsProtector; } static bool InstructionWillNotHaveChain(const Instruction *I) { -- cgit v1.1 From 62406fdc6f199e4e7df60830be45de4da97b34c7 Mon Sep 17 00:00:00 2001 From: Josh Magee Date: Wed, 30 Oct 2013 02:25:14 +0000 Subject: Reformat code with clang-format. Differential Revision: http://llvm-reviews.chandlerc.com/D2057 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@193672 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/StackProtector.cpp | 77 ++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 37 deletions(-) (limited to 'lib/CodeGen/StackProtector.cpp') diff --git a/lib/CodeGen/StackProtector.cpp b/lib/CodeGen/StackProtector.cpp index d2d87b2..9020449 100644 --- a/lib/CodeGen/StackProtector.cpp +++ b/lib/CodeGen/StackProtector.cpp @@ -42,20 +42,19 @@ STATISTIC(NumFunProtected, "Number of functions protected"); STATISTIC(NumAddrTaken, "Number of local variables that have their address" " taken."); -static cl::opt -EnableSelectionDAGSP("enable-selectiondag-sp", cl::init(true), - cl::Hidden); +static cl::opt EnableSelectionDAGSP("enable-selectiondag-sp", + cl::init(true), cl::Hidden); char StackProtector::ID = 0; -INITIALIZE_PASS(StackProtector, "stack-protector", - "Insert stack protectors", false, true) +INITIALIZE_PASS(StackProtector, "stack-protector", "Insert stack protectors", + false, true) FunctionPass *llvm::createStackProtectorPass(const TargetMachine *TM) { return new StackProtector(TM); } -StackProtector::SSPLayoutKind StackProtector::getSSPLayout(const AllocaInst *AI) - const { +StackProtector::SSPLayoutKind +StackProtector::getSSPLayout(const AllocaInst *AI) const { return AI ? Layout.lookup(AI) : SSPLK_None; } @@ -65,11 +64,11 @@ bool StackProtector::runOnFunction(Function &Fn) { DT = getAnalysisIfAvailable(); TLI = TM->getTargetLowering(); - if (!RequiresStackProtector()) return false; + if (!RequiresStackProtector()) + return false; - Attribute Attr = - Fn.getAttributes().getAttribute(AttributeSet::FunctionIndex, - "stack-protector-buffer-size"); + Attribute Attr = Fn.getAttributes().getAttribute( + AttributeSet::FunctionIndex, "stack-protector-buffer-size"); if (Attr.isStringAttribute()) Attr.getValueAsString().getAsInteger(10, SSPBufferSize); @@ -81,9 +80,10 @@ bool StackProtector::runOnFunction(Function &Fn) { /// it is "large" ( >= ssp-buffer-size). In the case of a structure with /// multiple arrays, this gets set if any of them is large. bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge, - bool Strong, bool InStruct) - const { - if (!Ty) return false; + bool Strong, + bool InStruct) const { + if (!Ty) + return false; if (ArrayType *AT = dyn_cast(Ty)) { if (!AT->getElementType()->isIntegerTy(8)) { // If we're on a non-Darwin platform or we're inside of a structure, don't @@ -99,7 +99,7 @@ bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge, if (SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT)) { IsLarge = true; return true; - } + } if (Strong) // Require a protector for all arrays in strong mode @@ -107,11 +107,13 @@ bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge, } const StructType *ST = dyn_cast(Ty); - if (!ST) return false; + if (!ST) + return false; bool NeedsProtector = false; for (StructType::element_iterator I = ST->element_begin(), - E = ST->element_end(); I != E; ++I) + E = ST->element_end(); + I != E; ++I) if (ContainsProtectableArray(*I, IsLarge, Strong, true)) { // If the element is a protectable array and is large (>= SSPBufferSize) // then we are done. If the protectable array is not large, then @@ -126,7 +128,7 @@ bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge, bool StackProtector::HasAddressTaken(const Instruction *AI) { for (Value::const_use_iterator UI = AI->use_begin(), UE = AI->use_end(); - UI != UE; ++UI) { + UI != UE; ++UI) { const User *U = *UI; if (const StoreInst *SI = dyn_cast(U)) { if (AI == SI->getValueOperand()) @@ -188,8 +190,8 @@ bool StackProtector::RequiresStackProtector() { 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) { + for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; + ++II) { if (AllocaInst *AI = dyn_cast(II)) { if (AI->isArrayAllocation()) { // SSP-Strong: Enable protectors for any call to alloca, regardless @@ -198,7 +200,7 @@ bool StackProtector::RequiresStackProtector() { return true; if (const ConstantInt *CI = - dyn_cast(AI->getArraySize())) { + dyn_cast(AI->getArraySize())) { if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) { // A call to alloca with size >= SSPBufferSize requires // stack protectors. @@ -239,7 +241,7 @@ bool StackProtector::RequiresStackProtector() { static bool InstructionWillNotHaveChain(const Instruction *I) { return !I->mayHaveSideEffects() && !I->mayReadFromMemory() && - isSafeToSpeculativelyExecute(I); + isSafeToSpeculativelyExecute(I); } /// Identify if RI has a previous instruction in the "Tail Position" and return @@ -259,7 +261,8 @@ static CallInst *FindPotentialTailCall(BasicBlock *BB, ReturnInst *RI, const unsigned MaxSearch = 4; bool NoInterposingChain = true; - for (BasicBlock::reverse_iterator I = llvm::next(BB->rbegin()), E = BB->rend(); + for (BasicBlock::reverse_iterator I = llvm::next(BB->rbegin()), + E = BB->rend(); I != E && SearchCounter < MaxSearch; ++I) { Instruction *Inst = &*I; @@ -289,7 +292,8 @@ static CallInst *FindPotentialTailCall(BasicBlock *BB, ReturnInst *RI, // If we did not find a call see if we have an instruction that may create // an interposing chain. - NoInterposingChain = NoInterposingChain && InstructionWillNotHaveChain(Inst); + NoInterposingChain = + NoInterposingChain && InstructionWillNotHaveChain(Inst); // Increment max search. SearchCounter++; @@ -316,15 +320,14 @@ static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI, unsigned AddressSpace, Offset; if (TLI->getStackCookieLocation(AddressSpace, Offset)) { Constant *OffsetVal = - ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset); + ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset); - StackGuardVar = ConstantExpr::getIntToPtr(OffsetVal, - PointerType::get(PtrTy, - AddressSpace)); + StackGuardVar = ConstantExpr::getIntToPtr( + OffsetVal, PointerType::get(PtrTy, AddressSpace)); } else if (Trip.getOS() == llvm::Triple::OpenBSD) { StackGuardVar = M->getOrInsertGlobal("__guard_local", PtrTy); cast(StackGuardVar) - ->setVisibility(GlobalValue::HiddenVisibility); + ->setVisibility(GlobalValue::HiddenVisibility); } else { SupportsSelectionDAGSP = true; StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy); @@ -348,11 +351,11 @@ static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI, bool StackProtector::InsertStackProtectors() { bool HasPrologue = false; bool SupportsSelectionDAGSP = - EnableSelectionDAGSP && !TM->Options.EnableFastISel; - AllocaInst *AI = 0; // Place on stack that stores the stack guard. - Value *StackGuardVar = 0; // The stack guard variable. + EnableSelectionDAGSP && !TM->Options.EnableFastISel; + 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; ) { + for (Function::iterator I = F->begin(), E = F->end(); I != E;) { BasicBlock *BB = I++; ReturnInst *RI = dyn_cast(BB->getTerminator()); if (!RI) @@ -360,8 +363,8 @@ bool StackProtector::InsertStackProtectors() { if (!HasPrologue) { HasPrologue = true; - SupportsSelectionDAGSP &= CreatePrologue(F, M, RI, TLI, Trip, AI, - StackGuardVar); + SupportsSelectionDAGSP &= + CreatePrologue(F, M, RI, TLI, Trip, AI, StackGuardVar); } if (SupportsSelectionDAGSP) { @@ -375,11 +378,11 @@ bool StackProtector::InsertStackProtectors() { // At this point we know that BB has a return statement so it *DOES* // have a terminator. assert(InsertionPt != 0 && "BB must have a terminator instruction at " - "this point."); + "this point."); } Function *Intrinsic = - Intrinsic::getDeclaration(M, Intrinsic::stackprotectorcheck); + Intrinsic::getDeclaration(M, Intrinsic::stackprotectorcheck); CallInst::Create(Intrinsic, StackGuardVar, "", InsertionPt); } else { -- cgit v1.1