Bug Summary

File:lib/CodeGen/SafeStack.cpp
Warning:line 574, column 7
Value stored to 'Size' is never read

Annotated Source Code

[?] Use j/k keys for keyboard navigation

1//===- SafeStack.cpp - Safe Stack Insertion -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass splits the stack into the safe stack (kept as-is for LLVM backend)
11// and the unsafe stack (explicitly allocated and managed through the runtime
12// support library).
13//
14// http://clang.llvm.org/docs/SafeStack.html
15//
16//===----------------------------------------------------------------------===//
17
18#include "SafeStackColoring.h"
19#include "SafeStackLayout.h"
20#include "llvm/ADT/APInt.h"
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/SmallPtrSet.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/Analysis/AssumptionCache.h"
26#include "llvm/Analysis/BranchProbabilityInfo.h"
27#include "llvm/Analysis/LoopInfo.h"
28#include "llvm/Analysis/ScalarEvolution.h"
29#include "llvm/Analysis/ScalarEvolutionExpressions.h"
30#include "llvm/Analysis/TargetLibraryInfo.h"
31#include "llvm/CodeGen/TargetLowering.h"
32#include "llvm/CodeGen/TargetPassConfig.h"
33#include "llvm/CodeGen/TargetSubtargetInfo.h"
34#include "llvm/IR/Argument.h"
35#include "llvm/IR/Attributes.h"
36#include "llvm/IR/CallSite.h"
37#include "llvm/IR/ConstantRange.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/DIBuilder.h"
40#include "llvm/IR/DataLayout.h"
41#include "llvm/IR/DerivedTypes.h"
42#include "llvm/IR/Dominators.h"
43#include "llvm/IR/Function.h"
44#include "llvm/IR/IRBuilder.h"
45#include "llvm/IR/InstIterator.h"
46#include "llvm/IR/Instruction.h"
47#include "llvm/IR/Instructions.h"
48#include "llvm/IR/IntrinsicInst.h"
49#include "llvm/IR/Intrinsics.h"
50#include "llvm/IR/MDBuilder.h"
51#include "llvm/IR/Module.h"
52#include "llvm/IR/Type.h"
53#include "llvm/IR/Use.h"
54#include "llvm/IR/User.h"
55#include "llvm/IR/Value.h"
56#include "llvm/Pass.h"
57#include "llvm/Support/Casting.h"
58#include "llvm/Support/Debug.h"
59#include "llvm/Support/ErrorHandling.h"
60#include "llvm/Support/MathExtras.h"
61#include "llvm/Support/raw_ostream.h"
62#include "llvm/Target/TargetMachine.h"
63#include "llvm/Transforms/Utils/BasicBlockUtils.h"
64#include "llvm/Transforms/Utils/Local.h"
65#include <algorithm>
66#include <cassert>
67#include <cstdint>
68#include <string>
69#include <utility>
70
71using namespace llvm;
72using namespace llvm::safestack;
73
74#define DEBUG_TYPE"safe-stack" "safe-stack"
75
76namespace llvm {
77
78STATISTIC(NumFunctions, "Total number of functions")static llvm::Statistic NumFunctions = {"safe-stack", "NumFunctions"
, "Total number of functions", {0}, false}
;
79STATISTIC(NumUnsafeStackFunctions, "Number of functions with unsafe stack")static llvm::Statistic NumUnsafeStackFunctions = {"safe-stack"
, "NumUnsafeStackFunctions", "Number of functions with unsafe stack"
, {0}, false}
;
80STATISTIC(NumUnsafeStackRestorePointsFunctions,static llvm::Statistic NumUnsafeStackRestorePointsFunctions =
{"safe-stack", "NumUnsafeStackRestorePointsFunctions", "Number of functions that use setjmp or exceptions"
, {0}, false}
81 "Number of functions that use setjmp or exceptions")static llvm::Statistic NumUnsafeStackRestorePointsFunctions =
{"safe-stack", "NumUnsafeStackRestorePointsFunctions", "Number of functions that use setjmp or exceptions"
, {0}, false}
;
82
83STATISTIC(NumAllocas, "Total number of allocas")static llvm::Statistic NumAllocas = {"safe-stack", "NumAllocas"
, "Total number of allocas", {0}, false}
;
84STATISTIC(NumUnsafeStaticAllocas, "Number of unsafe static allocas")static llvm::Statistic NumUnsafeStaticAllocas = {"safe-stack"
, "NumUnsafeStaticAllocas", "Number of unsafe static allocas"
, {0}, false}
;
85STATISTIC(NumUnsafeDynamicAllocas, "Number of unsafe dynamic allocas")static llvm::Statistic NumUnsafeDynamicAllocas = {"safe-stack"
, "NumUnsafeDynamicAllocas", "Number of unsafe dynamic allocas"
, {0}, false}
;
86STATISTIC(NumUnsafeByValArguments, "Number of unsafe byval arguments")static llvm::Statistic NumUnsafeByValArguments = {"safe-stack"
, "NumUnsafeByValArguments", "Number of unsafe byval arguments"
, {0}, false}
;
87STATISTIC(NumUnsafeStackRestorePoints, "Number of setjmps and landingpads")static llvm::Statistic NumUnsafeStackRestorePoints = {"safe-stack"
, "NumUnsafeStackRestorePoints", "Number of setjmps and landingpads"
, {0}, false}
;
88
89} // namespace llvm
90
91namespace {
92
93/// Rewrite an SCEV expression for a memory access address to an expression that
94/// represents offset from the given alloca.
95///
96/// The implementation simply replaces all mentions of the alloca with zero.
97class AllocaOffsetRewriter : public SCEVRewriteVisitor<AllocaOffsetRewriter> {
98 const Value *AllocaPtr;
99
100public:
101 AllocaOffsetRewriter(ScalarEvolution &SE, const Value *AllocaPtr)
102 : SCEVRewriteVisitor(SE), AllocaPtr(AllocaPtr) {}
103
104 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
105 if (Expr->getValue() == AllocaPtr)
106 return SE.getZero(Expr->getType());
107 return Expr;
108 }
109};
110
111/// The SafeStack pass splits the stack of each function into the safe
112/// stack, which is only accessed through memory safe dereferences (as
113/// determined statically), and the unsafe stack, which contains all
114/// local variables that are accessed in ways that we can't prove to
115/// be safe.
116class SafeStack {
117 Function &F;
118 const TargetLoweringBase &TL;
119 const DataLayout &DL;
120 ScalarEvolution &SE;
121
122 Type *StackPtrTy;
123 Type *IntPtrTy;
124 Type *Int32Ty;
125 Type *Int8Ty;
126
127 Value *UnsafeStackPtr = nullptr;
128
129 /// Unsafe stack alignment. Each stack frame must ensure that the stack is
130 /// aligned to this value. We need to re-align the unsafe stack if the
131 /// alignment of any object on the stack exceeds this value.
132 ///
133 /// 16 seems like a reasonable upper bound on the alignment of objects that we
134 /// might expect to appear on the stack on most common targets.
135 enum { StackAlignment = 16 };
136
137 /// \brief Return the value of the stack canary.
138 Value *getStackGuard(IRBuilder<> &IRB, Function &F);
139
140 /// \brief Load stack guard from the frame and check if it has changed.
141 void checkStackGuard(IRBuilder<> &IRB, Function &F, ReturnInst &RI,
142 AllocaInst *StackGuardSlot, Value *StackGuard);
143
144 /// \brief Find all static allocas, dynamic allocas, return instructions and
145 /// stack restore points (exception unwind blocks and setjmp calls) in the
146 /// given function and append them to the respective vectors.
147 void findInsts(Function &F, SmallVectorImpl<AllocaInst *> &StaticAllocas,
148 SmallVectorImpl<AllocaInst *> &DynamicAllocas,
149 SmallVectorImpl<Argument *> &ByValArguments,
150 SmallVectorImpl<ReturnInst *> &Returns,
151 SmallVectorImpl<Instruction *> &StackRestorePoints);
152
153 /// \brief Calculate the allocation size of a given alloca. Returns 0 if the
154 /// size can not be statically determined.
155 uint64_t getStaticAllocaAllocationSize(const AllocaInst* AI);
156
157 /// \brief Allocate space for all static allocas in \p StaticAllocas,
158 /// replace allocas with pointers into the unsafe stack and generate code to
159 /// restore the stack pointer before all return instructions in \p Returns.
160 ///
161 /// \returns A pointer to the top of the unsafe stack after all unsafe static
162 /// allocas are allocated.
163 Value *moveStaticAllocasToUnsafeStack(IRBuilder<> &IRB, Function &F,
164 ArrayRef<AllocaInst *> StaticAllocas,
165 ArrayRef<Argument *> ByValArguments,
166 ArrayRef<ReturnInst *> Returns,
167 Instruction *BasePointer,
168 AllocaInst *StackGuardSlot);
169
170 /// \brief Generate code to restore the stack after all stack restore points
171 /// in \p StackRestorePoints.
172 ///
173 /// \returns A local variable in which to maintain the dynamic top of the
174 /// unsafe stack if needed.
175 AllocaInst *
176 createStackRestorePoints(IRBuilder<> &IRB, Function &F,
177 ArrayRef<Instruction *> StackRestorePoints,
178 Value *StaticTop, bool NeedDynamicTop);
179
180 /// \brief Replace all allocas in \p DynamicAllocas with code to allocate
181 /// space dynamically on the unsafe stack and store the dynamic unsafe stack
182 /// top to \p DynamicTop if non-null.
183 void moveDynamicAllocasToUnsafeStack(Function &F, Value *UnsafeStackPtr,
184 AllocaInst *DynamicTop,
185 ArrayRef<AllocaInst *> DynamicAllocas);
186
187 bool IsSafeStackAlloca(const Value *AllocaPtr, uint64_t AllocaSize);
188
189 bool IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U,
190 const Value *AllocaPtr, uint64_t AllocaSize);
191 bool IsAccessSafe(Value *Addr, uint64_t Size, const Value *AllocaPtr,
192 uint64_t AllocaSize);
193
194public:
195 SafeStack(Function &F, const TargetLoweringBase &TL, const DataLayout &DL,
196 ScalarEvolution &SE)
197 : F(F), TL(TL), DL(DL), SE(SE),
198 StackPtrTy(Type::getInt8PtrTy(F.getContext())),
199 IntPtrTy(DL.getIntPtrType(F.getContext())),
200 Int32Ty(Type::getInt32Ty(F.getContext())),
201 Int8Ty(Type::getInt8Ty(F.getContext())) {}
202
203 // Run the transformation on the associated function.
204 // Returns whether the function was changed.
205 bool run();
206};
207
208uint64_t SafeStack::getStaticAllocaAllocationSize(const AllocaInst* AI) {
209 uint64_t Size = DL.getTypeAllocSize(AI->getAllocatedType());
210 if (AI->isArrayAllocation()) {
211 auto C = dyn_cast<ConstantInt>(AI->getArraySize());
212 if (!C)
213 return 0;
214 Size *= C->getZExtValue();
215 }
216 return Size;
217}
218
219bool SafeStack::IsAccessSafe(Value *Addr, uint64_t AccessSize,
220 const Value *AllocaPtr, uint64_t AllocaSize) {
221 AllocaOffsetRewriter Rewriter(SE, AllocaPtr);
222 const SCEV *Expr = Rewriter.visit(SE.getSCEV(Addr));
223
224 uint64_t BitWidth = SE.getTypeSizeInBits(Expr->getType());
225 ConstantRange AccessStartRange = SE.getUnsignedRange(Expr);
226 ConstantRange SizeRange =
227 ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, AccessSize));
228 ConstantRange AccessRange = AccessStartRange.add(SizeRange);
229 ConstantRange AllocaRange =
230 ConstantRange(APInt(BitWidth, 0), APInt(BitWidth, AllocaSize));
231 bool Safe = AllocaRange.contains(AccessRange);
232
233 DEBUG(dbgs() << "[SafeStack] "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] " << (isa
<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
<< *AllocaPtr << "\n" << " Access "
<< *Addr << "\n" << " SCEV " <<
*Expr << " U: " << SE.getUnsignedRange(Expr) <<
", S: " << SE.getSignedRange(Expr) << "\n" <<
" Range " << AccessRange << "\n" <<
" AllocaRange " << AllocaRange << "\n"
<< " " << (Safe ? "safe" : "unsafe") <<
"\n"; } } while (false)
234 << (isa<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] " << (isa
<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
<< *AllocaPtr << "\n" << " Access "
<< *Addr << "\n" << " SCEV " <<
*Expr << " U: " << SE.getUnsignedRange(Expr) <<
", S: " << SE.getSignedRange(Expr) << "\n" <<
" Range " << AccessRange << "\n" <<
" AllocaRange " << AllocaRange << "\n"
<< " " << (Safe ? "safe" : "unsafe") <<
"\n"; } } while (false)
235 << *AllocaPtr << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] " << (isa
<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
<< *AllocaPtr << "\n" << " Access "
<< *Addr << "\n" << " SCEV " <<
*Expr << " U: " << SE.getUnsignedRange(Expr) <<
", S: " << SE.getSignedRange(Expr) << "\n" <<
" Range " << AccessRange << "\n" <<
" AllocaRange " << AllocaRange << "\n"
<< " " << (Safe ? "safe" : "unsafe") <<
"\n"; } } while (false)
236 << " Access " << *Addr << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] " << (isa
<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
<< *AllocaPtr << "\n" << " Access "
<< *Addr << "\n" << " SCEV " <<
*Expr << " U: " << SE.getUnsignedRange(Expr) <<
", S: " << SE.getSignedRange(Expr) << "\n" <<
" Range " << AccessRange << "\n" <<
" AllocaRange " << AllocaRange << "\n"
<< " " << (Safe ? "safe" : "unsafe") <<
"\n"; } } while (false)
237 << " SCEV " << *Exprdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] " << (isa
<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
<< *AllocaPtr << "\n" << " Access "
<< *Addr << "\n" << " SCEV " <<
*Expr << " U: " << SE.getUnsignedRange(Expr) <<
", S: " << SE.getSignedRange(Expr) << "\n" <<
" Range " << AccessRange << "\n" <<
" AllocaRange " << AllocaRange << "\n"
<< " " << (Safe ? "safe" : "unsafe") <<
"\n"; } } while (false)
238 << " U: " << SE.getUnsignedRange(Expr)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] " << (isa
<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
<< *AllocaPtr << "\n" << " Access "
<< *Addr << "\n" << " SCEV " <<
*Expr << " U: " << SE.getUnsignedRange(Expr) <<
", S: " << SE.getSignedRange(Expr) << "\n" <<
" Range " << AccessRange << "\n" <<
" AllocaRange " << AllocaRange << "\n"
<< " " << (Safe ? "safe" : "unsafe") <<
"\n"; } } while (false)
239 << ", S: " << SE.getSignedRange(Expr) << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] " << (isa
<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
<< *AllocaPtr << "\n" << " Access "
<< *Addr << "\n" << " SCEV " <<
*Expr << " U: " << SE.getUnsignedRange(Expr) <<
", S: " << SE.getSignedRange(Expr) << "\n" <<
" Range " << AccessRange << "\n" <<
" AllocaRange " << AllocaRange << "\n"
<< " " << (Safe ? "safe" : "unsafe") <<
"\n"; } } while (false)
240 << " Range " << AccessRange << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] " << (isa
<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
<< *AllocaPtr << "\n" << " Access "
<< *Addr << "\n" << " SCEV " <<
*Expr << " U: " << SE.getUnsignedRange(Expr) <<
", S: " << SE.getSignedRange(Expr) << "\n" <<
" Range " << AccessRange << "\n" <<
" AllocaRange " << AllocaRange << "\n"
<< " " << (Safe ? "safe" : "unsafe") <<
"\n"; } } while (false)
241 << " AllocaRange " << AllocaRange << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] " << (isa
<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
<< *AllocaPtr << "\n" << " Access "
<< *Addr << "\n" << " SCEV " <<
*Expr << " U: " << SE.getUnsignedRange(Expr) <<
", S: " << SE.getSignedRange(Expr) << "\n" <<
" Range " << AccessRange << "\n" <<
" AllocaRange " << AllocaRange << "\n"
<< " " << (Safe ? "safe" : "unsafe") <<
"\n"; } } while (false)
242 << " " << (Safe ? "safe" : "unsafe") << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] " << (isa
<AllocaInst>(AllocaPtr) ? "Alloca " : "ByValArgument ")
<< *AllocaPtr << "\n" << " Access "
<< *Addr << "\n" << " SCEV " <<
*Expr << " U: " << SE.getUnsignedRange(Expr) <<
", S: " << SE.getSignedRange(Expr) << "\n" <<
" Range " << AccessRange << "\n" <<
" AllocaRange " << AllocaRange << "\n"
<< " " << (Safe ? "safe" : "unsafe") <<
"\n"; } } while (false)
;
243
244 return Safe;
245}
246
247bool SafeStack::IsMemIntrinsicSafe(const MemIntrinsic *MI, const Use &U,
248 const Value *AllocaPtr,
249 uint64_t AllocaSize) {
250 // All MemIntrinsics have destination address in Arg0 and size in Arg2.
251 if (MI->getRawDest() != U) return true;
252 const auto *Len = dyn_cast<ConstantInt>(MI->getLength());
253 // Non-constant size => unsafe. FIXME: try SCEV getRange.
254 if (!Len) return false;
255 return IsAccessSafe(U, Len->getZExtValue(), AllocaPtr, AllocaSize);
256}
257
258/// Check whether a given allocation must be put on the safe
259/// stack or not. The function analyzes all uses of AI and checks whether it is
260/// only accessed in a memory safe way (as decided statically).
261bool SafeStack::IsSafeStackAlloca(const Value *AllocaPtr, uint64_t AllocaSize) {
262 // Go through all uses of this alloca and check whether all accesses to the
263 // allocated object are statically known to be memory safe and, hence, the
264 // object can be placed on the safe stack.
265 SmallPtrSet<const Value *, 16> Visited;
266 SmallVector<const Value *, 8> WorkList;
267 WorkList.push_back(AllocaPtr);
268
269 // A DFS search through all uses of the alloca in bitcasts/PHI/GEPs/etc.
270 while (!WorkList.empty()) {
271 const Value *V = WorkList.pop_back_val();
272 for (const Use &UI : V->uses()) {
273 auto I = cast<const Instruction>(UI.getUser());
274 assert(V == UI.get())(static_cast <bool> (V == UI.get()) ? void (0) : __assert_fail
("V == UI.get()", "/build/llvm-toolchain-snapshot-6.0~svn321639/lib/CodeGen/SafeStack.cpp"
, 274, __extension__ __PRETTY_FUNCTION__))
;
275
276 switch (I->getOpcode()) {
277 case Instruction::Load:
278 if (!IsAccessSafe(UI, DL.getTypeStoreSize(I->getType()), AllocaPtr,
279 AllocaSize))
280 return false;
281 break;
282
283 case Instruction::VAArg:
284 // "va-arg" from a pointer is safe.
285 break;
286 case Instruction::Store:
287 if (V == I->getOperand(0)) {
288 // Stored the pointer - conservatively assume it may be unsafe.
289 DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AllocaPtrdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] Unsafe alloca: "
<< *AllocaPtr << "\n store of address: "
<< *I << "\n"; } } while (false)
290 << "\n store of address: " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] Unsafe alloca: "
<< *AllocaPtr << "\n store of address: "
<< *I << "\n"; } } while (false)
;
291 return false;
292 }
293
294 if (!IsAccessSafe(UI, DL.getTypeStoreSize(I->getOperand(0)->getType()),
295 AllocaPtr, AllocaSize))
296 return false;
297 break;
298
299 case Instruction::Ret:
300 // Information leak.
301 return false;
302
303 case Instruction::Call:
304 case Instruction::Invoke: {
305 ImmutableCallSite CS(I);
306
307 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
308 if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
309 II->getIntrinsicID() == Intrinsic::lifetime_end)
310 continue;
311 }
312
313 if (const MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) {
314 if (!IsMemIntrinsicSafe(MI, UI, AllocaPtr, AllocaSize)) {
315 DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AllocaPtrdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] Unsafe alloca: "
<< *AllocaPtr << "\n unsafe memintrinsic: "
<< *I << "\n"; } } while (false)
316 << "\n unsafe memintrinsic: " << *Ido { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] Unsafe alloca: "
<< *AllocaPtr << "\n unsafe memintrinsic: "
<< *I << "\n"; } } while (false)
317 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] Unsafe alloca: "
<< *AllocaPtr << "\n unsafe memintrinsic: "
<< *I << "\n"; } } while (false)
;
318 return false;
319 }
320 continue;
321 }
322
323 // LLVM 'nocapture' attribute is only set for arguments whose address
324 // is not stored, passed around, or used in any other non-trivial way.
325 // We assume that passing a pointer to an object as a 'nocapture
326 // readnone' argument is safe.
327 // FIXME: a more precise solution would require an interprocedural
328 // analysis here, which would look at all uses of an argument inside
329 // the function being called.
330 ImmutableCallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
331 for (ImmutableCallSite::arg_iterator A = B; A != E; ++A)
332 if (A->get() == V)
333 if (!(CS.doesNotCapture(A - B) && (CS.doesNotAccessMemory(A - B) ||
334 CS.doesNotAccessMemory()))) {
335 DEBUG(dbgs() << "[SafeStack] Unsafe alloca: " << *AllocaPtrdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] Unsafe alloca: "
<< *AllocaPtr << "\n unsafe call: " <<
*I << "\n"; } } while (false)
336 << "\n unsafe call: " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] Unsafe alloca: "
<< *AllocaPtr << "\n unsafe call: " <<
*I << "\n"; } } while (false)
;
337 return false;
338 }
339 continue;
340 }
341
342 default:
343 if (Visited.insert(I).second)
344 WorkList.push_back(cast<const Instruction>(I));
345 }
346 }
347 }
348
349 // All uses of the alloca are safe, we can place it on the safe stack.
350 return true;
351}
352
353Value *SafeStack::getStackGuard(IRBuilder<> &IRB, Function &F) {
354 Value *StackGuardVar = TL.getIRStackGuard(IRB);
355 if (!StackGuardVar)
356 StackGuardVar =
357 F.getParent()->getOrInsertGlobal("__stack_chk_guard", StackPtrTy);
358 return IRB.CreateLoad(StackGuardVar, "StackGuard");
359}
360
361void SafeStack::findInsts(Function &F,
362 SmallVectorImpl<AllocaInst *> &StaticAllocas,
363 SmallVectorImpl<AllocaInst *> &DynamicAllocas,
364 SmallVectorImpl<Argument *> &ByValArguments,
365 SmallVectorImpl<ReturnInst *> &Returns,
366 SmallVectorImpl<Instruction *> &StackRestorePoints) {
367 for (Instruction &I : instructions(&F)) {
368 if (auto AI = dyn_cast<AllocaInst>(&I)) {
369 ++NumAllocas;
370
371 uint64_t Size = getStaticAllocaAllocationSize(AI);
372 if (IsSafeStackAlloca(AI, Size))
373 continue;
374
375 if (AI->isStaticAlloca()) {
376 ++NumUnsafeStaticAllocas;
377 StaticAllocas.push_back(AI);
378 } else {
379 ++NumUnsafeDynamicAllocas;
380 DynamicAllocas.push_back(AI);
381 }
382 } else if (auto RI = dyn_cast<ReturnInst>(&I)) {
383 Returns.push_back(RI);
384 } else if (auto CI = dyn_cast<CallInst>(&I)) {
385 // setjmps require stack restore.
386 if (CI->getCalledFunction() && CI->canReturnTwice())
387 StackRestorePoints.push_back(CI);
388 } else if (auto LP = dyn_cast<LandingPadInst>(&I)) {
389 // Exception landing pads require stack restore.
390 StackRestorePoints.push_back(LP);
391 } else if (auto II = dyn_cast<IntrinsicInst>(&I)) {
392 if (II->getIntrinsicID() == Intrinsic::gcroot)
393 report_fatal_error(
394 "gcroot intrinsic not compatible with safestack attribute");
395 }
396 }
397 for (Argument &Arg : F.args()) {
398 if (!Arg.hasByValAttr())
399 continue;
400 uint64_t Size =
401 DL.getTypeStoreSize(Arg.getType()->getPointerElementType());
402 if (IsSafeStackAlloca(&Arg, Size))
403 continue;
404
405 ++NumUnsafeByValArguments;
406 ByValArguments.push_back(&Arg);
407 }
408}
409
410AllocaInst *
411SafeStack::createStackRestorePoints(IRBuilder<> &IRB, Function &F,
412 ArrayRef<Instruction *> StackRestorePoints,
413 Value *StaticTop, bool NeedDynamicTop) {
414 assert(StaticTop && "The stack top isn't set.")(static_cast <bool> (StaticTop && "The stack top isn't set."
) ? void (0) : __assert_fail ("StaticTop && \"The stack top isn't set.\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/lib/CodeGen/SafeStack.cpp"
, 414, __extension__ __PRETTY_FUNCTION__))
;
415
416 if (StackRestorePoints.empty())
417 return nullptr;
418
419 // We need the current value of the shadow stack pointer to restore
420 // after longjmp or exception catching.
421
422 // FIXME: On some platforms this could be handled by the longjmp/exception
423 // runtime itself.
424
425 AllocaInst *DynamicTop = nullptr;
426 if (NeedDynamicTop) {
427 // If we also have dynamic alloca's, the stack pointer value changes
428 // throughout the function. For now we store it in an alloca.
429 DynamicTop = IRB.CreateAlloca(StackPtrTy, /*ArraySize=*/nullptr,
430 "unsafe_stack_dynamic_ptr");
431 IRB.CreateStore(StaticTop, DynamicTop);
432 }
433
434 // Restore current stack pointer after longjmp/exception catch.
435 for (Instruction *I : StackRestorePoints) {
436 ++NumUnsafeStackRestorePoints;
437
438 IRB.SetInsertPoint(I->getNextNode());
439 Value *CurrentTop = DynamicTop ? IRB.CreateLoad(DynamicTop) : StaticTop;
440 IRB.CreateStore(CurrentTop, UnsafeStackPtr);
441 }
442
443 return DynamicTop;
444}
445
446void SafeStack::checkStackGuard(IRBuilder<> &IRB, Function &F, ReturnInst &RI,
447 AllocaInst *StackGuardSlot, Value *StackGuard) {
448 Value *V = IRB.CreateLoad(StackGuardSlot);
449 Value *Cmp = IRB.CreateICmpNE(StackGuard, V);
450
451 auto SuccessProb = BranchProbabilityInfo::getBranchProbStackProtector(true);
452 auto FailureProb = BranchProbabilityInfo::getBranchProbStackProtector(false);
453 MDNode *Weights = MDBuilder(F.getContext())
454 .createBranchWeights(SuccessProb.getNumerator(),
455 FailureProb.getNumerator());
456 Instruction *CheckTerm =
457 SplitBlockAndInsertIfThen(Cmp, &RI,
458 /* Unreachable */ true, Weights);
459 IRBuilder<> IRBFail(CheckTerm);
460 // FIXME: respect -fsanitize-trap / -ftrap-function here?
461 Constant *StackChkFail = F.getParent()->getOrInsertFunction(
462 "__stack_chk_fail", IRB.getVoidTy());
463 IRBFail.CreateCall(StackChkFail, {});
464}
465
466/// We explicitly compute and set the unsafe stack layout for all unsafe
467/// static alloca instructions. We save the unsafe "base pointer" in the
468/// prologue into a local variable and restore it in the epilogue.
469Value *SafeStack::moveStaticAllocasToUnsafeStack(
470 IRBuilder<> &IRB, Function &F, ArrayRef<AllocaInst *> StaticAllocas,
471 ArrayRef<Argument *> ByValArguments, ArrayRef<ReturnInst *> Returns,
472 Instruction *BasePointer, AllocaInst *StackGuardSlot) {
473 if (StaticAllocas.empty() && ByValArguments.empty())
474 return BasePointer;
475
476 DIBuilder DIB(*F.getParent());
477
478 StackColoring SSC(F, StaticAllocas);
479 SSC.run();
480 SSC.removeAllMarkers();
481
482 // Unsafe stack always grows down.
483 StackLayout SSL(StackAlignment);
484 if (StackGuardSlot) {
485 Type *Ty = StackGuardSlot->getAllocatedType();
486 unsigned Align =
487 std::max(DL.getPrefTypeAlignment(Ty), StackGuardSlot->getAlignment());
488 SSL.addObject(StackGuardSlot, getStaticAllocaAllocationSize(StackGuardSlot),
489 Align, SSC.getFullLiveRange());
490 }
491
492 for (Argument *Arg : ByValArguments) {
493 Type *Ty = Arg->getType()->getPointerElementType();
494 uint64_t Size = DL.getTypeStoreSize(Ty);
495 if (Size == 0)
496 Size = 1; // Don't create zero-sized stack objects.
497
498 // Ensure the object is properly aligned.
499 unsigned Align = std::max((unsigned)DL.getPrefTypeAlignment(Ty),
500 Arg->getParamAlignment());
501 SSL.addObject(Arg, Size, Align, SSC.getFullLiveRange());
502 }
503
504 for (AllocaInst *AI : StaticAllocas) {
505 Type *Ty = AI->getAllocatedType();
506 uint64_t Size = getStaticAllocaAllocationSize(AI);
507 if (Size == 0)
508 Size = 1; // Don't create zero-sized stack objects.
509
510 // Ensure the object is properly aligned.
511 unsigned Align =
512 std::max((unsigned)DL.getPrefTypeAlignment(Ty), AI->getAlignment());
513
514 SSL.addObject(AI, Size, Align, SSC.getLiveRange(AI));
515 }
516
517 SSL.computeLayout();
518 unsigned FrameAlignment = SSL.getFrameAlignment();
519
520 // FIXME: tell SSL that we start at a less-then-MaxAlignment aligned location
521 // (AlignmentSkew).
522 if (FrameAlignment > StackAlignment) {
523 // Re-align the base pointer according to the max requested alignment.
524 assert(isPowerOf2_32(FrameAlignment))(static_cast <bool> (isPowerOf2_32(FrameAlignment)) ? void
(0) : __assert_fail ("isPowerOf2_32(FrameAlignment)", "/build/llvm-toolchain-snapshot-6.0~svn321639/lib/CodeGen/SafeStack.cpp"
, 524, __extension__ __PRETTY_FUNCTION__))
;
525 IRB.SetInsertPoint(BasePointer->getNextNode());
526 BasePointer = cast<Instruction>(IRB.CreateIntToPtr(
527 IRB.CreateAnd(IRB.CreatePtrToInt(BasePointer, IntPtrTy),
528 ConstantInt::get(IntPtrTy, ~uint64_t(FrameAlignment - 1))),
529 StackPtrTy));
530 }
531
532 IRB.SetInsertPoint(BasePointer->getNextNode());
533
534 if (StackGuardSlot) {
535 unsigned Offset = SSL.getObjectOffset(StackGuardSlot);
536 Value *Off = IRB.CreateGEP(BasePointer, // BasePointer is i8*
537 ConstantInt::get(Int32Ty, -Offset));
538 Value *NewAI =
539 IRB.CreateBitCast(Off, StackGuardSlot->getType(), "StackGuardSlot");
540
541 // Replace alloc with the new location.
542 StackGuardSlot->replaceAllUsesWith(NewAI);
543 StackGuardSlot->eraseFromParent();
544 }
545
546 for (Argument *Arg : ByValArguments) {
547 unsigned Offset = SSL.getObjectOffset(Arg);
548 Type *Ty = Arg->getType()->getPointerElementType();
549
550 uint64_t Size = DL.getTypeStoreSize(Ty);
551 if (Size == 0)
552 Size = 1; // Don't create zero-sized stack objects.
553
554 Value *Off = IRB.CreateGEP(BasePointer, // BasePointer is i8*
555 ConstantInt::get(Int32Ty, -Offset));
556 Value *NewArg = IRB.CreateBitCast(Off, Arg->getType(),
557 Arg->getName() + ".unsafe-byval");
558
559 // Replace alloc with the new location.
560 replaceDbgDeclare(Arg, BasePointer, BasePointer->getNextNode(), DIB,
561 DIExpression::NoDeref, -Offset, DIExpression::NoDeref);
562 Arg->replaceAllUsesWith(NewArg);
563 IRB.SetInsertPoint(cast<Instruction>(NewArg)->getNextNode());
564 IRB.CreateMemCpy(Off, Arg, Size, Arg->getParamAlignment());
565 }
566
567 // Allocate space for every unsafe static AllocaInst on the unsafe stack.
568 for (AllocaInst *AI : StaticAllocas) {
569 IRB.SetInsertPoint(AI);
570 unsigned Offset = SSL.getObjectOffset(AI);
571
572 uint64_t Size = getStaticAllocaAllocationSize(AI);
573 if (Size == 0)
574 Size = 1; // Don't create zero-sized stack objects.
Value stored to 'Size' is never read
575
576 replaceDbgDeclareForAlloca(AI, BasePointer, DIB, DIExpression::NoDeref,
577 -Offset, DIExpression::NoDeref);
578 replaceDbgValueForAlloca(AI, BasePointer, DIB, -Offset);
579
580 // Replace uses of the alloca with the new location.
581 // Insert address calculation close to each use to work around PR27844.
582 std::string Name = std::string(AI->getName()) + ".unsafe";
583 while (!AI->use_empty()) {
584 Use &U = *AI->use_begin();
585 Instruction *User = cast<Instruction>(U.getUser());
586
587 Instruction *InsertBefore;
588 if (auto *PHI = dyn_cast<PHINode>(User))
589 InsertBefore = PHI->getIncomingBlock(U)->getTerminator();
590 else
591 InsertBefore = User;
592
593 IRBuilder<> IRBUser(InsertBefore);
594 Value *Off = IRBUser.CreateGEP(BasePointer, // BasePointer is i8*
595 ConstantInt::get(Int32Ty, -Offset));
596 Value *Replacement = IRBUser.CreateBitCast(Off, AI->getType(), Name);
597
598 if (auto *PHI = dyn_cast<PHINode>(User)) {
599 // PHI nodes may have multiple incoming edges from the same BB (why??),
600 // all must be updated at once with the same incoming value.
601 auto *BB = PHI->getIncomingBlock(U);
602 for (unsigned I = 0; I < PHI->getNumIncomingValues(); ++I)
603 if (PHI->getIncomingBlock(I) == BB)
604 PHI->setIncomingValue(I, Replacement);
605 } else {
606 U.set(Replacement);
607 }
608 }
609
610 AI->eraseFromParent();
611 }
612
613 // Re-align BasePointer so that our callees would see it aligned as
614 // expected.
615 // FIXME: no need to update BasePointer in leaf functions.
616 unsigned FrameSize = alignTo(SSL.getFrameSize(), StackAlignment);
617
618 // Update shadow stack pointer in the function epilogue.
619 IRB.SetInsertPoint(BasePointer->getNextNode());
620
621 Value *StaticTop =
622 IRB.CreateGEP(BasePointer, ConstantInt::get(Int32Ty, -FrameSize),
623 "unsafe_stack_static_top");
624 IRB.CreateStore(StaticTop, UnsafeStackPtr);
625 return StaticTop;
626}
627
628void SafeStack::moveDynamicAllocasToUnsafeStack(
629 Function &F, Value *UnsafeStackPtr, AllocaInst *DynamicTop,
630 ArrayRef<AllocaInst *> DynamicAllocas) {
631 DIBuilder DIB(*F.getParent());
632
633 for (AllocaInst *AI : DynamicAllocas) {
634 IRBuilder<> IRB(AI);
635
636 // Compute the new SP value (after AI).
637 Value *ArraySize = AI->getArraySize();
638 if (ArraySize->getType() != IntPtrTy)
639 ArraySize = IRB.CreateIntCast(ArraySize, IntPtrTy, false);
640
641 Type *Ty = AI->getAllocatedType();
642 uint64_t TySize = DL.getTypeAllocSize(Ty);
643 Value *Size = IRB.CreateMul(ArraySize, ConstantInt::get(IntPtrTy, TySize));
644
645 Value *SP = IRB.CreatePtrToInt(IRB.CreateLoad(UnsafeStackPtr), IntPtrTy);
646 SP = IRB.CreateSub(SP, Size);
647
648 // Align the SP value to satisfy the AllocaInst, type and stack alignments.
649 unsigned Align = std::max(
650 std::max((unsigned)DL.getPrefTypeAlignment(Ty), AI->getAlignment()),
651 (unsigned)StackAlignment);
652
653 assert(isPowerOf2_32(Align))(static_cast <bool> (isPowerOf2_32(Align)) ? void (0) :
__assert_fail ("isPowerOf2_32(Align)", "/build/llvm-toolchain-snapshot-6.0~svn321639/lib/CodeGen/SafeStack.cpp"
, 653, __extension__ __PRETTY_FUNCTION__))
;
654 Value *NewTop = IRB.CreateIntToPtr(
655 IRB.CreateAnd(SP, ConstantInt::get(IntPtrTy, ~uint64_t(Align - 1))),
656 StackPtrTy);
657
658 // Save the stack pointer.
659 IRB.CreateStore(NewTop, UnsafeStackPtr);
660 if (DynamicTop)
661 IRB.CreateStore(NewTop, DynamicTop);
662
663 Value *NewAI = IRB.CreatePointerCast(NewTop, AI->getType());
664 if (AI->hasName() && isa<Instruction>(NewAI))
665 NewAI->takeName(AI);
666
667 replaceDbgDeclareForAlloca(AI, NewAI, DIB, DIExpression::NoDeref, 0,
668 DIExpression::NoDeref);
669 AI->replaceAllUsesWith(NewAI);
670 AI->eraseFromParent();
671 }
672
673 if (!DynamicAllocas.empty()) {
674 // Now go through the instructions again, replacing stacksave/stackrestore.
675 for (inst_iterator It = inst_begin(&F), Ie = inst_end(&F); It != Ie;) {
676 Instruction *I = &*(It++);
677 auto II = dyn_cast<IntrinsicInst>(I);
678 if (!II)
679 continue;
680
681 if (II->getIntrinsicID() == Intrinsic::stacksave) {
682 IRBuilder<> IRB(II);
683 Instruction *LI = IRB.CreateLoad(UnsafeStackPtr);
684 LI->takeName(II);
685 II->replaceAllUsesWith(LI);
686 II->eraseFromParent();
687 } else if (II->getIntrinsicID() == Intrinsic::stackrestore) {
688 IRBuilder<> IRB(II);
689 Instruction *SI = IRB.CreateStore(II->getArgOperand(0), UnsafeStackPtr);
690 SI->takeName(II);
691 assert(II->use_empty())(static_cast <bool> (II->use_empty()) ? void (0) : __assert_fail
("II->use_empty()", "/build/llvm-toolchain-snapshot-6.0~svn321639/lib/CodeGen/SafeStack.cpp"
, 691, __extension__ __PRETTY_FUNCTION__))
;
692 II->eraseFromParent();
693 }
694 }
695 }
696}
697
698bool SafeStack::run() {
699 assert(F.hasFnAttribute(Attribute::SafeStack) &&(static_cast <bool> (F.hasFnAttribute(Attribute::SafeStack
) && "Can't run SafeStack on a function without the attribute"
) ? void (0) : __assert_fail ("F.hasFnAttribute(Attribute::SafeStack) && \"Can't run SafeStack on a function without the attribute\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/lib/CodeGen/SafeStack.cpp"
, 700, __extension__ __PRETTY_FUNCTION__))
700 "Can't run SafeStack on a function without the attribute")(static_cast <bool> (F.hasFnAttribute(Attribute::SafeStack
) && "Can't run SafeStack on a function without the attribute"
) ? void (0) : __assert_fail ("F.hasFnAttribute(Attribute::SafeStack) && \"Can't run SafeStack on a function without the attribute\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/lib/CodeGen/SafeStack.cpp"
, 700, __extension__ __PRETTY_FUNCTION__))
;
701 assert(!F.isDeclaration() && "Can't run SafeStack on a function declaration")(static_cast <bool> (!F.isDeclaration() && "Can't run SafeStack on a function declaration"
) ? void (0) : __assert_fail ("!F.isDeclaration() && \"Can't run SafeStack on a function declaration\""
, "/build/llvm-toolchain-snapshot-6.0~svn321639/lib/CodeGen/SafeStack.cpp"
, 701, __extension__ __PRETTY_FUNCTION__))
;
702
703 ++NumFunctions;
704
705 SmallVector<AllocaInst *, 16> StaticAllocas;
706 SmallVector<AllocaInst *, 4> DynamicAllocas;
707 SmallVector<Argument *, 4> ByValArguments;
708 SmallVector<ReturnInst *, 4> Returns;
709
710 // Collect all points where stack gets unwound and needs to be restored
711 // This is only necessary because the runtime (setjmp and unwind code) is
712 // not aware of the unsafe stack and won't unwind/restore it properly.
713 // To work around this problem without changing the runtime, we insert
714 // instrumentation to restore the unsafe stack pointer when necessary.
715 SmallVector<Instruction *, 4> StackRestorePoints;
716
717 // Find all static and dynamic alloca instructions that must be moved to the
718 // unsafe stack, all return instructions and stack restore points.
719 findInsts(F, StaticAllocas, DynamicAllocas, ByValArguments, Returns,
720 StackRestorePoints);
721
722 if (StaticAllocas.empty() && DynamicAllocas.empty() &&
723 ByValArguments.empty() && StackRestorePoints.empty())
724 return false; // Nothing to do in this function.
725
726 if (!StaticAllocas.empty() || !DynamicAllocas.empty() ||
727 !ByValArguments.empty())
728 ++NumUnsafeStackFunctions; // This function has the unsafe stack.
729
730 if (!StackRestorePoints.empty())
731 ++NumUnsafeStackRestorePointsFunctions;
732
733 IRBuilder<> IRB(&F.front(), F.begin()->getFirstInsertionPt());
734 UnsafeStackPtr = TL.getSafeStackPointerLocation(IRB);
735
736 // Load the current stack pointer (we'll also use it as a base pointer).
737 // FIXME: use a dedicated register for it ?
738 Instruction *BasePointer =
739 IRB.CreateLoad(UnsafeStackPtr, false, "unsafe_stack_ptr");
740 assert(BasePointer->getType() == StackPtrTy)(static_cast <bool> (BasePointer->getType() == StackPtrTy
) ? void (0) : __assert_fail ("BasePointer->getType() == StackPtrTy"
, "/build/llvm-toolchain-snapshot-6.0~svn321639/lib/CodeGen/SafeStack.cpp"
, 740, __extension__ __PRETTY_FUNCTION__))
;
741
742 AllocaInst *StackGuardSlot = nullptr;
743 // FIXME: implement weaker forms of stack protector.
744 if (F.hasFnAttribute(Attribute::StackProtect) ||
745 F.hasFnAttribute(Attribute::StackProtectStrong) ||
746 F.hasFnAttribute(Attribute::StackProtectReq)) {
747 Value *StackGuard = getStackGuard(IRB, F);
748 StackGuardSlot = IRB.CreateAlloca(StackPtrTy, nullptr);
749 IRB.CreateStore(StackGuard, StackGuardSlot);
750
751 for (ReturnInst *RI : Returns) {
752 IRBuilder<> IRBRet(RI);
753 checkStackGuard(IRBRet, F, *RI, StackGuardSlot, StackGuard);
754 }
755 }
756
757 // The top of the unsafe stack after all unsafe static allocas are
758 // allocated.
759 Value *StaticTop =
760 moveStaticAllocasToUnsafeStack(IRB, F, StaticAllocas, ByValArguments,
761 Returns, BasePointer, StackGuardSlot);
762
763 // Safe stack object that stores the current unsafe stack top. It is updated
764 // as unsafe dynamic (non-constant-sized) allocas are allocated and freed.
765 // This is only needed if we need to restore stack pointer after longjmp
766 // or exceptions, and we have dynamic allocations.
767 // FIXME: a better alternative might be to store the unsafe stack pointer
768 // before setjmp / invoke instructions.
769 AllocaInst *DynamicTop = createStackRestorePoints(
770 IRB, F, StackRestorePoints, StaticTop, !DynamicAllocas.empty());
771
772 // Handle dynamic allocas.
773 moveDynamicAllocasToUnsafeStack(F, UnsafeStackPtr, DynamicTop,
774 DynamicAllocas);
775
776 // Restore the unsafe stack pointer before each return.
777 for (ReturnInst *RI : Returns) {
778 IRB.SetInsertPoint(RI);
779 IRB.CreateStore(BasePointer, UnsafeStackPtr);
780 }
781
782 DEBUG(dbgs() << "[SafeStack] safestack applied\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] safestack applied\n"
; } } while (false)
;
783 return true;
784}
785
786class SafeStackLegacyPass : public FunctionPass {
787 const TargetMachine *TM = nullptr;
788
789public:
790 static char ID; // Pass identification, replacement for typeid..
791
792 SafeStackLegacyPass() : FunctionPass(ID) {
793 initializeSafeStackLegacyPassPass(*PassRegistry::getPassRegistry());
794 }
795
796 void getAnalysisUsage(AnalysisUsage &AU) const override {
797 AU.addRequired<TargetPassConfig>();
798 AU.addRequired<TargetLibraryInfoWrapperPass>();
799 AU.addRequired<AssumptionCacheTracker>();
800 }
801
802 bool runOnFunction(Function &F) override {
803 DEBUG(dbgs() << "[SafeStack] Function: " << F.getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] Function: " <<
F.getName() << "\n"; } } while (false)
;
804
805 if (!F.hasFnAttribute(Attribute::SafeStack)) {
806 DEBUG(dbgs() << "[SafeStack] safestack is not requested"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] safestack is not requested"
" for this function\n"; } } while (false)
807 " for this function\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] safestack is not requested"
" for this function\n"; } } while (false)
;
808 return false;
809 }
810
811 if (F.isDeclaration()) {
812 DEBUG(dbgs() << "[SafeStack] function definition"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] function definition"
" is not available\n"; } } while (false)
813 " is not available\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safe-stack")) { dbgs() << "[SafeStack] function definition"
" is not available\n"; } } while (false)
;
814 return false;
815 }
816
817 TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
818 auto *TL = TM->getSubtargetImpl(F)->getTargetLowering();
819 if (!TL)
820 report_fatal_error("TargetLowering instance is required");
821
822 auto *DL = &F.getParent()->getDataLayout();
823 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
824 auto &ACT = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
825
826 // Compute DT and LI only for functions that have the attribute.
827 // This is only useful because the legacy pass manager doesn't let us
828 // compute analyzes lazily.
829 // In the backend pipeline, nothing preserves DT before SafeStack, so we
830 // would otherwise always compute it wastefully, even if there is no
831 // function with the safestack attribute.
832 DominatorTree DT(F);
833 LoopInfo LI(DT);
834
835 ScalarEvolution SE(F, TLI, ACT, DT, LI);
836
837 return SafeStack(F, *TL, *DL, SE).run();
838 }
839};
840
841} // end anonymous namespace
842
843char SafeStackLegacyPass::ID = 0;
844
845INITIALIZE_PASS_BEGIN(SafeStackLegacyPass, DEBUG_TYPE,static void *initializeSafeStackLegacyPassPassOnce(PassRegistry
&Registry) {
846 "Safe Stack instrumentation pass", false, false)static void *initializeSafeStackLegacyPassPassOnce(PassRegistry
&Registry) {
847INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)initializeTargetPassConfigPass(Registry);
848INITIALIZE_PASS_END(SafeStackLegacyPass, DEBUG_TYPE,PassInfo *PI = new PassInfo( "Safe Stack instrumentation pass"
, "safe-stack", &SafeStackLegacyPass::ID, PassInfo::NormalCtor_t
(callDefaultCtor<SafeStackLegacyPass>), false, false); Registry
.registerPass(*PI, true); return PI; } static llvm::once_flag
InitializeSafeStackLegacyPassPassFlag; void llvm::initializeSafeStackLegacyPassPass
(PassRegistry &Registry) { llvm::call_once(InitializeSafeStackLegacyPassPassFlag
, initializeSafeStackLegacyPassPassOnce, std::ref(Registry));
}
849 "Safe Stack instrumentation pass", false, false)PassInfo *PI = new PassInfo( "Safe Stack instrumentation pass"
, "safe-stack", &SafeStackLegacyPass::ID, PassInfo::NormalCtor_t
(callDefaultCtor<SafeStackLegacyPass>), false, false); Registry
.registerPass(*PI, true); return PI; } static llvm::once_flag
InitializeSafeStackLegacyPassPassFlag; void llvm::initializeSafeStackLegacyPassPass
(PassRegistry &Registry) { llvm::call_once(InitializeSafeStackLegacyPassPassFlag
, initializeSafeStackLegacyPassPassOnce, std::ref(Registry));
}
850
851FunctionPass *llvm::createSafeStackPass() { return new SafeStackLegacyPass(); }