llvm.org GIT mirror llvm / 62406fd
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 Josh Magee 6 years ago
2 changed file(s) with 49 addition(s) and 45 deletion(s). Raw diff Collapse all Expand all
4444 };
4545
4646 /// A mapping of AllocaInsts to their required SSP layout.
47 typedef ValueMap*, SSPLayoutKind> SSPLayoutMap;
47 typedef ValueMap *, SSPLayoutKind> SSPLayoutMap;
4848
4949 private:
5050 const TargetMachine *TM;
7272 /// if a variable's reference has been taken. This set
7373 /// is maintained to ensure we don't visit the same PHI node multiple
7474 /// times.
75 SmallPtrSet*, 16> VisitedPHIs;
75 SmallPtrSet *, 16> VisitedPHIs;
7676
7777 /// InsertStackProtectors - Insert code into the prologue and epilogue of
7878 /// the function.
101101 /// RequiresStackProtector - Check whether or not this function needs a
102102 /// stack protector based upon the stack protector level.
103103 bool RequiresStackProtector();
104
104105 public:
105 static char ID; // Pass identification, replacement for typeid.
106 static char ID; // Pass identification, replacement for typeid.
106107 StackProtector() : FunctionPass(ID), TM(0), TLI(0), SSPBufferSize(0) {
107108 initializeStackProtectorPass(*PassRegistry::getPassRegistry());
108109 }
109110 StackProtector(const TargetMachine *TM)
110 : FunctionPass(ID), TM(TM), TLI(0), Trip(TM->getTargetTriple()),
111 SSPBufferSize(8) {
111 : FunctionPass(ID), TM(TM), TLI(0), Trip(TM->getTargetTriple()),
112 SSPBufferSize(8) {
112113 initializeStackProtectorPass(*PassRegistry::getPassRegistry());
113114 }
114115
4141 STATISTIC(NumAddrTaken, "Number of local variables that have their address"
4242 " taken.");
4343
44 static cl::opt
45 EnableSelectionDAGSP("enable-selectiondag-sp", cl::init(true),
46 cl::Hidden);
44 static cl::opt EnableSelectionDAGSP("enable-selectiondag-sp",
45 cl::init(true), cl::Hidden);
4746
4847 char StackProtector::ID = 0;
49 INITIALIZE_PASS(StackProtector, "stack-protector",
50 "Insert stack protectors", false, true)
48 INITIALIZE_PASS(StackProtector, "stack-protector", "Insert stack protectors",
49 false, true)
5150
5251 FunctionPass *llvm::createStackProtectorPass(const TargetMachine *TM) {
5352 return new StackProtector(TM);
5453 }
5554
56 StackProtector::SSPLayoutKind StackProtector::getSSPLayout(const AllocaInst *AI)
57 const {
55 StackProtector::SSPLayoutKind
56 StackProtector::getSSPLayout(const AllocaInst *AI) const {
5857 return AI ? Layout.lookup(AI) : SSPLK_None;
5958 }
6059
6463 DT = getAnalysisIfAvailable();
6564 TLI = TM->getTargetLowering();
6665
67 if (!RequiresStackProtector()) return false;
68
69 Attribute Attr =
70 Fn.getAttributes().getAttribute(AttributeSet::FunctionIndex,
71 "stack-protector-buffer-size");
66 if (!RequiresStackProtector())
67 return false;
68
69 Attribute Attr = Fn.getAttributes().getAttribute(
70 AttributeSet::FunctionIndex, "stack-protector-buffer-size");
7271 if (Attr.isStringAttribute())
7372 Attr.getValueAsString().getAsInteger(10, SSPBufferSize);
7473
8079 /// it is "large" ( >= ssp-buffer-size). In the case of a structure with
8180 /// multiple arrays, this gets set if any of them is large.
8281 bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge,
83 bool Strong, bool InStruct)
84 const {
85 if (!Ty) return false;
82 bool Strong,
83 bool InStruct) const {
84 if (!Ty)
85 return false;
8686 if (ArrayType *AT = dyn_cast(Ty)) {
8787 if (!AT->getElementType()->isIntegerTy(8)) {
8888 // If we're on a non-Darwin platform or we're inside of a structure, don't
9898 if (SSPBufferSize <= TLI->getDataLayout()->getTypeAllocSize(AT)) {
9999 IsLarge = true;
100100 return true;
101 }
101 }
102102
103103 if (Strong)
104104 // Require a protector for all arrays in strong mode
106106 }
107107
108108 const StructType *ST = dyn_cast(Ty);
109 if (!ST) return false;
109 if (!ST)
110 return false;
110111
111112 bool NeedsProtector = false;
112113 for (StructType::element_iterator I = ST->element_begin(),
113 E = ST->element_end(); I != E; ++I)
114 E = ST->element_end();
115 I != E; ++I)
114116 if (ContainsProtectableArray(*I, IsLarge, Strong, true)) {
115117 // If the element is a protectable array and is large (>= SSPBufferSize)
116118 // then we are done. If the protectable array is not large, then
125127
126128 bool StackProtector::HasAddressTaken(const Instruction *AI) {
127129 for (Value::const_use_iterator UI = AI->use_begin(), UE = AI->use_end();
128 UI != UE; ++UI) {
130 UI != UE; ++UI) {
129131 const User *U = *UI;
130132 if (const StoreInst *SI = dyn_cast(U)) {
131133 if (AI == SI->getValueOperand())
187189 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
188190 BasicBlock *BB = I;
189191
190 for (BasicBlock::iterator
191 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
192 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;
193 ++II) {
192194 if (AllocaInst *AI = dyn_cast(II)) {
193195 if (AI->isArrayAllocation()) {
194196 // SSP-Strong: Enable protectors for any call to alloca, regardless
197199 return true;
198200
199201 if (const ConstantInt *CI =
200 dyn_cast(AI->getArraySize())) {
202 dyn_cast(AI->getArraySize())) {
201203 if (CI->getLimitedValue(SSPBufferSize) >= SSPBufferSize) {
202204 // A call to alloca with size >= SSPBufferSize requires
203205 // stack protectors.
238240
239241 static bool InstructionWillNotHaveChain(const Instruction *I) {
240242 return !I->mayHaveSideEffects() && !I->mayReadFromMemory() &&
241 isSafeToSpeculativelyExecute(I);
243 isSafeToSpeculativelyExecute(I);
242244 }
243245
244246 /// Identify if RI has a previous instruction in the "Tail Position" and return
258260 const unsigned MaxSearch = 4;
259261 bool NoInterposingChain = true;
260262
261 for (BasicBlock::reverse_iterator I = llvm::next(BB->rbegin()), E = BB->rend();
263 for (BasicBlock::reverse_iterator I = llvm::next(BB->rbegin()),
264 E = BB->rend();
262265 I != E && SearchCounter < MaxSearch; ++I) {
263266 Instruction *Inst = &*I;
264267
288291
289292 // If we did not find a call see if we have an instruction that may create
290293 // an interposing chain.
291 NoInterposingChain = NoInterposingChain && InstructionWillNotHaveChain(Inst);
294 NoInterposingChain =
295 NoInterposingChain && InstructionWillNotHaveChain(Inst);
292296
293297 // Increment max search.
294298 SearchCounter++;
315319 unsigned AddressSpace, Offset;
316320 if (TLI->getStackCookieLocation(AddressSpace, Offset)) {
317321 Constant *OffsetVal =
318 ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset);
319
320 StackGuardVar = ConstantExpr::getIntToPtr(OffsetVal,
321 PointerType::get(PtrTy,
322 AddressSpace));
322 ConstantInt::get(Type::getInt32Ty(RI->getContext()), Offset);
323
324 StackGuardVar = ConstantExpr::getIntToPtr(
325 OffsetVal, PointerType::get(PtrTy, AddressSpace));
323326 } else if (Trip.getOS() == llvm::Triple::OpenBSD) {
324327 StackGuardVar = M->getOrInsertGlobal("__guard_local", PtrTy);
325328 cast(StackGuardVar)
326 ->setVisibility(GlobalValue::HiddenVisibility);
329 ->setVisibility(GlobalValue::HiddenVisibility);
327330 } else {
328331 SupportsSelectionDAGSP = true;
329332 StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
347350 bool StackProtector::InsertStackProtectors() {
348351 bool HasPrologue = false;
349352 bool SupportsSelectionDAGSP =
350 EnableSelectionDAGSP && !TM->Options.EnableFastISel;
351 AllocaInst *AI = 0; // Place on stack that stores the stack guard.
352 Value *StackGuardVar = 0; // The stack guard variable.
353
354 for (Function::iterator I = F->begin(), E = F->end(); I != E; ) {
353 EnableSelectionDAGSP && !TM->Options.EnableFastISel;
354 AllocaInst *AI = 0; // Place on stack that stores the stack guard.
355 Value *StackGuardVar = 0; // The stack guard variable.
356
357 for (Function::iterator I = F->begin(), E = F->end(); I != E;) {
355358 BasicBlock *BB = I++;
356359 ReturnInst *RI = dyn_cast(BB->getTerminator());
357360 if (!RI)
359362
360363 if (!HasPrologue) {
361364 HasPrologue = true;
362 SupportsSelectionDAGSP &= CreatePrologue(F, M, RI, TLI, Trip, AI,
363 StackGuardVar);
365 SupportsSelectionDAGSP &=
366 CreatePrologue(F, M, RI, TLI, Trip, AI, StackGuardVar);
364367 }
365368
366369 if (SupportsSelectionDAGSP) {
374377 // At this point we know that BB has a return statement so it *DOES*
375378 // have a terminator.
376379 assert(InsertionPt != 0 && "BB must have a terminator instruction at "
377 "this point.");
380 "this point.");
378381 }
379382
380383 Function *Intrinsic =
381 Intrinsic::getDeclaration(M, Intrinsic::stackprotectorcheck);
384 Intrinsic::getDeclaration(M, Intrinsic::stackprotectorcheck);
382385 CallInst::Create(Intrinsic, StackGuardVar, "", InsertionPt);
383386
384387 } else {