Implement depth_first and inverse_depth_first range factory functions.
[oota-llvm.git] / lib / Transforms / Instrumentation / AddressSanitizer.cpp
1 //===-- AddressSanitizer.cpp - memory error detector ------------*- C++ -*-===//
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 file is a part of AddressSanitizer, an address sanity checker.
11 // Details of the algorithm:
12 //  http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm
13 //
14 //===----------------------------------------------------------------------===//
15
16 #define DEBUG_TYPE "asan"
17
18 #include "llvm/Transforms/Instrumentation.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/DepthFirstIterator.h"
22 #include "llvm/ADT/SmallSet.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/ADT/Triple.h"
28 #include "llvm/IR/CallSite.h"
29 #include "llvm/IR/DIBuilder.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/IRBuilder.h"
33 #include "llvm/IR/InlineAsm.h"
34 #include "llvm/IR/InstVisitor.h"
35 #include "llvm/IR/IntrinsicInst.h"
36 #include "llvm/IR/LLVMContext.h"
37 #include "llvm/IR/MDBuilder.h"
38 #include "llvm/IR/Module.h"
39 #include "llvm/IR/Type.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/DataTypes.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/Endian.h"
44 #include "llvm/Support/system_error.h"
45 #include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
46 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
47 #include "llvm/Transforms/Utils/Cloning.h"
48 #include "llvm/Transforms/Utils/Local.h"
49 #include "llvm/Transforms/Utils/ModuleUtils.h"
50 #include "llvm/Transforms/Utils/SpecialCaseList.h"
51 #include <algorithm>
52 #include <string>
53
54 using namespace llvm;
55
56 static const uint64_t kDefaultShadowScale = 3;
57 static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
58 static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
59 static const uint64_t kSmallX86_64ShadowOffset = 0x7FFF8000;  // < 2G.
60 static const uint64_t kPPC64_ShadowOffset64 = 1ULL << 41;
61 static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa8000;
62 static const uint64_t kFreeBSD_ShadowOffset32 = 1ULL << 30;
63 static const uint64_t kFreeBSD_ShadowOffset64 = 1ULL << 46;
64
65 static const size_t kMinStackMallocSize = 1 << 6;  // 64B
66 static const size_t kMaxStackMallocSize = 1 << 16;  // 64K
67 static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
68 static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
69
70 static const char *const kAsanModuleCtorName = "asan.module_ctor";
71 static const char *const kAsanModuleDtorName = "asan.module_dtor";
72 static const int         kAsanCtorAndCtorPriority = 1;
73 static const char *const kAsanReportErrorTemplate = "__asan_report_";
74 static const char *const kAsanReportLoadN = "__asan_report_load_n";
75 static const char *const kAsanReportStoreN = "__asan_report_store_n";
76 static const char *const kAsanRegisterGlobalsName = "__asan_register_globals";
77 static const char *const kAsanUnregisterGlobalsName =
78     "__asan_unregister_globals";
79 static const char *const kAsanPoisonGlobalsName = "__asan_before_dynamic_init";
80 static const char *const kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init";
81 static const char *const kAsanInitName = "__asan_init_v3";
82 static const char *const kAsanCovName = "__sanitizer_cov";
83 static const char *const kAsanPtrCmp = "__sanitizer_ptr_cmp";
84 static const char *const kAsanPtrSub = "__sanitizer_ptr_sub";
85 static const char *const kAsanHandleNoReturnName = "__asan_handle_no_return";
86 static const int         kMaxAsanStackMallocSizeClass = 10;
87 static const char *const kAsanStackMallocNameTemplate = "__asan_stack_malloc_";
88 static const char *const kAsanStackFreeNameTemplate = "__asan_stack_free_";
89 static const char *const kAsanGenPrefix = "__asan_gen_";
90 static const char *const kAsanPoisonStackMemoryName =
91     "__asan_poison_stack_memory";
92 static const char *const kAsanUnpoisonStackMemoryName =
93     "__asan_unpoison_stack_memory";
94
95 static const char *const kAsanOptionDetectUAR =
96     "__asan_option_detect_stack_use_after_return";
97
98 #ifndef NDEBUG
99 static const int kAsanStackAfterReturnMagic = 0xf5;
100 #endif
101
102 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
103 static const size_t kNumberOfAccessSizes = 5;
104
105 // Command-line flags.
106
107 // This flag may need to be replaced with -f[no-]asan-reads.
108 static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
109        cl::desc("instrument read instructions"), cl::Hidden, cl::init(true));
110 static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes",
111        cl::desc("instrument write instructions"), cl::Hidden, cl::init(true));
112 static cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics",
113        cl::desc("instrument atomic instructions (rmw, cmpxchg)"),
114        cl::Hidden, cl::init(true));
115 static cl::opt<bool> ClAlwaysSlowPath("asan-always-slow-path",
116        cl::desc("use instrumentation with slow path for all accesses"),
117        cl::Hidden, cl::init(false));
118 // This flag limits the number of instructions to be instrumented
119 // in any given BB. Normally, this should be set to unlimited (INT_MAX),
120 // but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
121 // set it to 10000.
122 static cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb",
123        cl::init(10000),
124        cl::desc("maximal number of instructions to instrument in any given BB"),
125        cl::Hidden);
126 // This flag may need to be replaced with -f[no]asan-stack.
127 static cl::opt<bool> ClStack("asan-stack",
128        cl::desc("Handle stack memory"), cl::Hidden, cl::init(true));
129 // This flag may need to be replaced with -f[no]asan-use-after-return.
130 static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
131        cl::desc("Check return-after-free"), cl::Hidden, cl::init(false));
132 // This flag may need to be replaced with -f[no]asan-globals.
133 static cl::opt<bool> ClGlobals("asan-globals",
134        cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
135 static cl::opt<int> ClCoverage("asan-coverage",
136        cl::desc("ASan coverage. 0: none, 1: entry block, 2: all blocks"),
137        cl::Hidden, cl::init(false));
138 static cl::opt<bool> ClInitializers("asan-initialization-order",
139        cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(false));
140 static cl::opt<bool> ClMemIntrin("asan-memintrin",
141        cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
142 static cl::opt<bool> ClInvalidPointerPairs("asan-detect-invalid-pointer-pair",
143        cl::desc("Instrument <, <=, >, >=, - with pointer operands"),
144        cl::Hidden, cl::init(false));
145 static cl::opt<unsigned> ClRealignStack("asan-realign-stack",
146        cl::desc("Realign stack to the value of this flag (power of two)"),
147        cl::Hidden, cl::init(32));
148 static cl::opt<std::string> ClBlacklistFile("asan-blacklist",
149        cl::desc("File containing the list of objects to ignore "
150                 "during instrumentation"), cl::Hidden);
151
152 // This is an experimental feature that will allow to choose between
153 // instrumented and non-instrumented code at link-time.
154 // If this option is on, just before instrumenting a function we create its
155 // clone; if the function is not changed by asan the clone is deleted.
156 // If we end up with a clone, we put the instrumented function into a section
157 // called "ASAN" and the uninstrumented function into a section called "NOASAN".
158 //
159 // This is still a prototype, we need to figure out a way to keep two copies of
160 // a function so that the linker can easily choose one of them.
161 static cl::opt<bool> ClKeepUninstrumented("asan-keep-uninstrumented-functions",
162        cl::desc("Keep uninstrumented copies of functions"),
163        cl::Hidden, cl::init(false));
164
165 // These flags allow to change the shadow mapping.
166 // The shadow mapping looks like
167 //    Shadow = (Mem >> scale) + (1 << offset_log)
168 static cl::opt<int> ClMappingScale("asan-mapping-scale",
169        cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0));
170
171 // Optimization flags. Not user visible, used mostly for testing
172 // and benchmarking the tool.
173 static cl::opt<bool> ClOpt("asan-opt",
174        cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true));
175 static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp",
176        cl::desc("Instrument the same temp just once"), cl::Hidden,
177        cl::init(true));
178 static cl::opt<bool> ClOptGlobals("asan-opt-globals",
179        cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
180
181 static cl::opt<bool> ClCheckLifetime("asan-check-lifetime",
182        cl::desc("Use llvm.lifetime intrinsics to insert extra checks"),
183        cl::Hidden, cl::init(false));
184
185 // Debug flags.
186 static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
187                             cl::init(0));
188 static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
189                                  cl::Hidden, cl::init(0));
190 static cl::opt<std::string> ClDebugFunc("asan-debug-func",
191                                         cl::Hidden, cl::desc("Debug func"));
192 static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
193                                cl::Hidden, cl::init(-1));
194 static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
195                                cl::Hidden, cl::init(-1));
196
197 STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
198 STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
199 STATISTIC(NumOptimizedAccessesToGlobalArray,
200           "Number of optimized accesses to global arrays");
201 STATISTIC(NumOptimizedAccessesToGlobalVar,
202           "Number of optimized accesses to global vars");
203
204 namespace {
205 /// A set of dynamically initialized globals extracted from metadata.
206 class SetOfDynamicallyInitializedGlobals {
207  public:
208   void Init(Module& M) {
209     // Clang generates metadata identifying all dynamically initialized globals.
210     NamedMDNode *DynamicGlobals =
211         M.getNamedMetadata("llvm.asan.dynamically_initialized_globals");
212     if (!DynamicGlobals)
213       return;
214     for (int i = 0, n = DynamicGlobals->getNumOperands(); i < n; ++i) {
215       MDNode *MDN = DynamicGlobals->getOperand(i);
216       assert(MDN->getNumOperands() == 1);
217       Value *VG = MDN->getOperand(0);
218       // The optimizer may optimize away a global entirely, in which case we
219       // cannot instrument access to it.
220       if (!VG)
221         continue;
222       DynInitGlobals.insert(cast<GlobalVariable>(VG));
223     }
224   }
225   bool Contains(GlobalVariable *G) { return DynInitGlobals.count(G) != 0; }
226  private:
227   SmallSet<GlobalValue*, 32> DynInitGlobals;
228 };
229
230 /// This struct defines the shadow mapping using the rule:
231 ///   shadow = (mem >> Scale) ADD-or-OR Offset.
232 struct ShadowMapping {
233   int Scale;
234   uint64_t Offset;
235   bool OrShadowOffset;
236 };
237
238 static ShadowMapping getShadowMapping(const Module &M, int LongSize) {
239   llvm::Triple TargetTriple(M.getTargetTriple());
240   bool IsAndroid = TargetTriple.getEnvironment() == llvm::Triple::Android;
241   // bool IsMacOSX = TargetTriple.getOS() == llvm::Triple::MacOSX;
242   bool IsFreeBSD = TargetTriple.getOS() == llvm::Triple::FreeBSD;
243   bool IsLinux = TargetTriple.getOS() == llvm::Triple::Linux;
244   bool IsPPC64 = TargetTriple.getArch() == llvm::Triple::ppc64 ||
245                  TargetTriple.getArch() == llvm::Triple::ppc64le;
246   bool IsX86_64 = TargetTriple.getArch() == llvm::Triple::x86_64;
247   bool IsMIPS32 = TargetTriple.getArch() == llvm::Triple::mips ||
248                   TargetTriple.getArch() == llvm::Triple::mipsel;
249
250   ShadowMapping Mapping;
251
252   if (LongSize == 32) {
253     if (IsAndroid)
254       Mapping.Offset = 0;
255     else if (IsMIPS32)
256       Mapping.Offset = kMIPS32_ShadowOffset32;
257     else if (IsFreeBSD)
258       Mapping.Offset = kFreeBSD_ShadowOffset32;
259     else
260       Mapping.Offset = kDefaultShadowOffset32;
261   } else {  // LongSize == 64
262     if (IsPPC64)
263       Mapping.Offset = kPPC64_ShadowOffset64;
264     else if (IsFreeBSD)
265       Mapping.Offset = kFreeBSD_ShadowOffset64;
266     else if (IsLinux && IsX86_64)
267       Mapping.Offset = kSmallX86_64ShadowOffset;
268     else
269       Mapping.Offset = kDefaultShadowOffset64;
270   }
271
272   Mapping.Scale = kDefaultShadowScale;
273   if (ClMappingScale) {
274     Mapping.Scale = ClMappingScale;
275   }
276
277   // OR-ing shadow offset if more efficient (at least on x86) if the offset
278   // is a power of two, but on ppc64 we have to use add since the shadow
279   // offset is not necessary 1/8-th of the address space.
280   Mapping.OrShadowOffset = !IsPPC64 && !(Mapping.Offset & (Mapping.Offset - 1));
281
282   return Mapping;
283 }
284
285 static size_t RedzoneSizeForScale(int MappingScale) {
286   // Redzone used for stack and globals is at least 32 bytes.
287   // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
288   return std::max(32U, 1U << MappingScale);
289 }
290
291 /// AddressSanitizer: instrument the code in module to find memory bugs.
292 struct AddressSanitizer : public FunctionPass {
293   AddressSanitizer(bool CheckInitOrder = true,
294                    bool CheckUseAfterReturn = false,
295                    bool CheckLifetime = false,
296                    StringRef BlacklistFile = StringRef())
297       : FunctionPass(ID),
298         CheckInitOrder(CheckInitOrder || ClInitializers),
299         CheckUseAfterReturn(CheckUseAfterReturn || ClUseAfterReturn),
300         CheckLifetime(CheckLifetime || ClCheckLifetime),
301         BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
302                                             : BlacklistFile) {}
303   const char *getPassName() const override {
304     return "AddressSanitizerFunctionPass";
305   }
306   void instrumentMop(Instruction *I);
307   void instrumentPointerComparisonOrSubtraction(Instruction *I);
308   void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore,
309                          Value *Addr, uint32_t TypeSize, bool IsWrite,
310                          Value *SizeArgument);
311   Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
312                            Value *ShadowValue, uint32_t TypeSize);
313   Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr,
314                                  bool IsWrite, size_t AccessSizeIndex,
315                                  Value *SizeArgument);
316   bool instrumentMemIntrinsic(MemIntrinsic *MI);
317   void instrumentMemIntrinsicParam(Instruction *OrigIns, Value *Addr,
318                                    Value *Size,
319                                    Instruction *InsertBefore, bool IsWrite);
320   Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
321   bool runOnFunction(Function &F) override;
322   bool maybeInsertAsanInitAtFunctionEntry(Function &F);
323   bool doInitialization(Module &M) override;
324   static char ID;  // Pass identification, replacement for typeid
325
326  private:
327   void initializeCallbacks(Module &M);
328
329   bool LooksLikeCodeInBug11395(Instruction *I);
330   bool GlobalIsLinkerInitialized(GlobalVariable *G);
331   bool InjectCoverage(Function &F, const ArrayRef<BasicBlock*> AllBlocks);
332   void InjectCoverageAtBlock(Function &F, BasicBlock &BB);
333
334   bool CheckInitOrder;
335   bool CheckUseAfterReturn;
336   bool CheckLifetime;
337   SmallString<64> BlacklistFile;
338
339   LLVMContext *C;
340   const DataLayout *DL;
341   int LongSize;
342   Type *IntptrTy;
343   ShadowMapping Mapping;
344   Function *AsanCtorFunction;
345   Function *AsanInitFunction;
346   Function *AsanHandleNoReturnFunc;
347   Function *AsanCovFunction;
348   Function *AsanPtrCmpFunction, *AsanPtrSubFunction;
349   std::unique_ptr<SpecialCaseList> BL;
350   // This array is indexed by AccessIsWrite and log2(AccessSize).
351   Function *AsanErrorCallback[2][kNumberOfAccessSizes];
352   // This array is indexed by AccessIsWrite.
353   Function *AsanErrorCallbackSized[2];
354   InlineAsm *EmptyAsm;
355   SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals;
356
357   friend struct FunctionStackPoisoner;
358 };
359
360 class AddressSanitizerModule : public ModulePass {
361  public:
362   AddressSanitizerModule(bool CheckInitOrder = true,
363                          StringRef BlacklistFile = StringRef())
364       : ModulePass(ID),
365         CheckInitOrder(CheckInitOrder || ClInitializers),
366         BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
367                                             : BlacklistFile) {}
368   bool runOnModule(Module &M) override;
369   static char ID;  // Pass identification, replacement for typeid
370   const char *getPassName() const override {
371     return "AddressSanitizerModule";
372   }
373
374  private:
375   void initializeCallbacks(Module &M);
376
377   bool ShouldInstrumentGlobal(GlobalVariable *G);
378   void createInitializerPoisonCalls(Module &M, GlobalValue *ModuleName);
379   size_t MinRedzoneSizeForGlobal() const {
380     return RedzoneSizeForScale(Mapping.Scale);
381   }
382
383   bool CheckInitOrder;
384   SmallString<64> BlacklistFile;
385
386   std::unique_ptr<SpecialCaseList> BL;
387   SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals;
388   Type *IntptrTy;
389   LLVMContext *C;
390   const DataLayout *DL;
391   ShadowMapping Mapping;
392   Function *AsanPoisonGlobals;
393   Function *AsanUnpoisonGlobals;
394   Function *AsanRegisterGlobals;
395   Function *AsanUnregisterGlobals;
396 };
397
398 // Stack poisoning does not play well with exception handling.
399 // When an exception is thrown, we essentially bypass the code
400 // that unpoisones the stack. This is why the run-time library has
401 // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
402 // stack in the interceptor. This however does not work inside the
403 // actual function which catches the exception. Most likely because the
404 // compiler hoists the load of the shadow value somewhere too high.
405 // This causes asan to report a non-existing bug on 453.povray.
406 // It sounds like an LLVM bug.
407 struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
408   Function &F;
409   AddressSanitizer &ASan;
410   DIBuilder DIB;
411   LLVMContext *C;
412   Type *IntptrTy;
413   Type *IntptrPtrTy;
414   ShadowMapping Mapping;
415
416   SmallVector<AllocaInst*, 16> AllocaVec;
417   SmallVector<Instruction*, 8> RetVec;
418   unsigned StackAlignment;
419
420   Function *AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
421            *AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1];
422   Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc;
423
424   // Stores a place and arguments of poisoning/unpoisoning call for alloca.
425   struct AllocaPoisonCall {
426     IntrinsicInst *InsBefore;
427     AllocaInst *AI;
428     uint64_t Size;
429     bool DoPoison;
430   };
431   SmallVector<AllocaPoisonCall, 8> AllocaPoisonCallVec;
432
433   // Maps Value to an AllocaInst from which the Value is originated.
434   typedef DenseMap<Value*, AllocaInst*> AllocaForValueMapTy;
435   AllocaForValueMapTy AllocaForValue;
436
437   FunctionStackPoisoner(Function &F, AddressSanitizer &ASan)
438       : F(F), ASan(ASan), DIB(*F.getParent()), C(ASan.C),
439         IntptrTy(ASan.IntptrTy), IntptrPtrTy(PointerType::get(IntptrTy, 0)),
440         Mapping(ASan.Mapping),
441         StackAlignment(1 << Mapping.Scale) {}
442
443   bool runOnFunction() {
444     if (!ClStack) return false;
445     // Collect alloca, ret, lifetime instructions etc.
446     for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
447       visit(*BB);
448
449     if (AllocaVec.empty()) return false;
450
451     initializeCallbacks(*F.getParent());
452
453     poisonStack();
454
455     if (ClDebugStack) {
456       DEBUG(dbgs() << F);
457     }
458     return true;
459   }
460
461   // Finds all static Alloca instructions and puts
462   // poisoned red zones around all of them.
463   // Then unpoison everything back before the function returns.
464   void poisonStack();
465
466   // ----------------------- Visitors.
467   /// \brief Collect all Ret instructions.
468   void visitReturnInst(ReturnInst &RI) {
469     RetVec.push_back(&RI);
470   }
471
472   /// \brief Collect Alloca instructions we want (and can) handle.
473   void visitAllocaInst(AllocaInst &AI) {
474     if (!isInterestingAlloca(AI)) return;
475
476     StackAlignment = std::max(StackAlignment, AI.getAlignment());
477     AllocaVec.push_back(&AI);
478   }
479
480   /// \brief Collect lifetime intrinsic calls to check for use-after-scope
481   /// errors.
482   void visitIntrinsicInst(IntrinsicInst &II) {
483     if (!ASan.CheckLifetime) return;
484     Intrinsic::ID ID = II.getIntrinsicID();
485     if (ID != Intrinsic::lifetime_start &&
486         ID != Intrinsic::lifetime_end)
487       return;
488     // Found lifetime intrinsic, add ASan instrumentation if necessary.
489     ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0));
490     // If size argument is undefined, don't do anything.
491     if (Size->isMinusOne()) return;
492     // Check that size doesn't saturate uint64_t and can
493     // be stored in IntptrTy.
494     const uint64_t SizeValue = Size->getValue().getLimitedValue();
495     if (SizeValue == ~0ULL ||
496         !ConstantInt::isValueValidForType(IntptrTy, SizeValue))
497       return;
498     // Find alloca instruction that corresponds to llvm.lifetime argument.
499     AllocaInst *AI = findAllocaForValue(II.getArgOperand(1));
500     if (!AI) return;
501     bool DoPoison = (ID == Intrinsic::lifetime_end);
502     AllocaPoisonCall APC = {&II, AI, SizeValue, DoPoison};
503     AllocaPoisonCallVec.push_back(APC);
504   }
505
506   // ---------------------- Helpers.
507   void initializeCallbacks(Module &M);
508
509   // Check if we want (and can) handle this alloca.
510   bool isInterestingAlloca(AllocaInst &AI) const {
511     return (!AI.isArrayAllocation() && AI.isStaticAlloca() &&
512             AI.getAllocatedType()->isSized() &&
513             // alloca() may be called with 0 size, ignore it.
514             getAllocaSizeInBytes(&AI) > 0);
515   }
516
517   uint64_t getAllocaSizeInBytes(AllocaInst *AI) const {
518     Type *Ty = AI->getAllocatedType();
519     uint64_t SizeInBytes = ASan.DL->getTypeAllocSize(Ty);
520     return SizeInBytes;
521   }
522   /// Finds alloca where the value comes from.
523   AllocaInst *findAllocaForValue(Value *V);
524   void poisonRedZones(const ArrayRef<uint8_t> ShadowBytes, IRBuilder<> &IRB,
525                       Value *ShadowBase, bool DoPoison);
526   void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> &IRB, bool DoPoison);
527
528   void SetShadowToStackAfterReturnInlined(IRBuilder<> &IRB, Value *ShadowBase,
529                                           int Size);
530 };
531
532 }  // namespace
533
534 char AddressSanitizer::ID = 0;
535 INITIALIZE_PASS(AddressSanitizer, "asan",
536     "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
537     false, false)
538 FunctionPass *llvm::createAddressSanitizerFunctionPass(
539     bool CheckInitOrder, bool CheckUseAfterReturn, bool CheckLifetime,
540     StringRef BlacklistFile) {
541   return new AddressSanitizer(CheckInitOrder, CheckUseAfterReturn,
542                               CheckLifetime, BlacklistFile);
543 }
544
545 char AddressSanitizerModule::ID = 0;
546 INITIALIZE_PASS(AddressSanitizerModule, "asan-module",
547     "AddressSanitizer: detects use-after-free and out-of-bounds bugs."
548     "ModulePass", false, false)
549 ModulePass *llvm::createAddressSanitizerModulePass(
550     bool CheckInitOrder, StringRef BlacklistFile) {
551   return new AddressSanitizerModule(CheckInitOrder, BlacklistFile);
552 }
553
554 static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
555   size_t Res = countTrailingZeros(TypeSize / 8);
556   assert(Res < kNumberOfAccessSizes);
557   return Res;
558 }
559
560 // \brief Create a constant for Str so that we can pass it to the run-time lib.
561 static GlobalVariable *createPrivateGlobalForString(
562     Module &M, StringRef Str, bool AllowMerging) {
563   Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
564   // We use private linkage for module-local strings. If they can be merged
565   // with another one, we set the unnamed_addr attribute.
566   GlobalVariable *GV =
567       new GlobalVariable(M, StrConst->getType(), true,
568                          GlobalValue::PrivateLinkage, StrConst, kAsanGenPrefix);
569   if (AllowMerging)
570     GV->setUnnamedAddr(true);
571   GV->setAlignment(1);  // Strings may not be merged w/o setting align 1.
572   return GV;
573 }
574
575 static bool GlobalWasGeneratedByAsan(GlobalVariable *G) {
576   return G->getName().find(kAsanGenPrefix) == 0;
577 }
578
579 Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
580   // Shadow >> scale
581   Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
582   if (Mapping.Offset == 0)
583     return Shadow;
584   // (Shadow >> scale) | offset
585   if (Mapping.OrShadowOffset)
586     return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset));
587   else
588     return IRB.CreateAdd(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset));
589 }
590
591 void AddressSanitizer::instrumentMemIntrinsicParam(
592     Instruction *OrigIns,
593     Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) {
594   IRBuilder<> IRB(InsertBefore);
595   if (Size->getType() != IntptrTy)
596     Size = IRB.CreateIntCast(Size, IntptrTy, false);
597   // Check the first byte.
598   instrumentAddress(OrigIns, InsertBefore, Addr, 8, IsWrite, Size);
599   // Check the last byte.
600   IRB.SetInsertPoint(InsertBefore);
601   Value *SizeMinusOne = IRB.CreateSub(Size, ConstantInt::get(IntptrTy, 1));
602   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
603   Value *AddrLast = IRB.CreateAdd(AddrLong, SizeMinusOne);
604   instrumentAddress(OrigIns, InsertBefore, AddrLast, 8, IsWrite, Size);
605 }
606
607 // Instrument memset/memmove/memcpy
608 bool AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
609   Value *Dst = MI->getDest();
610   MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI);
611   Value *Src = MemTran ? MemTran->getSource() : 0;
612   Value *Length = MI->getLength();
613
614   Constant *ConstLength = dyn_cast<Constant>(Length);
615   Instruction *InsertBefore = MI;
616   if (ConstLength) {
617     if (ConstLength->isNullValue()) return false;
618   } else {
619     // The size is not a constant so it could be zero -- check at run-time.
620     IRBuilder<> IRB(InsertBefore);
621
622     Value *Cmp = IRB.CreateICmpNE(Length,
623                                   Constant::getNullValue(Length->getType()));
624     InsertBefore = SplitBlockAndInsertIfThen(Cmp, InsertBefore, false);
625   }
626
627   instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true);
628   if (Src)
629     instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false);
630   return true;
631 }
632
633 // If I is an interesting memory access, return the PointerOperand
634 // and set IsWrite. Otherwise return NULL.
635 static Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite) {
636   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
637     if (!ClInstrumentReads) return NULL;
638     *IsWrite = false;
639     return LI->getPointerOperand();
640   }
641   if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
642     if (!ClInstrumentWrites) return NULL;
643     *IsWrite = true;
644     return SI->getPointerOperand();
645   }
646   if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
647     if (!ClInstrumentAtomics) return NULL;
648     *IsWrite = true;
649     return RMW->getPointerOperand();
650   }
651   if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
652     if (!ClInstrumentAtomics) return NULL;
653     *IsWrite = true;
654     return XCHG->getPointerOperand();
655   }
656   return NULL;
657 }
658
659 static bool isPointerOperand(Value *V) {
660   return V->getType()->isPointerTy() || isa<PtrToIntInst>(V);
661 }
662
663 // This is a rough heuristic; it may cause both false positives and
664 // false negatives. The proper implementation requires cooperation with
665 // the frontend.
666 static bool isInterestingPointerComparisonOrSubtraction(Instruction *I) {
667   if (ICmpInst *Cmp = dyn_cast<ICmpInst>(I)) {
668     if (!Cmp->isRelational())
669       return false;
670   } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
671     if (BO->getOpcode() != Instruction::Sub)
672       return false;
673   } else {
674     return false;
675   }
676   if (!isPointerOperand(I->getOperand(0)) ||
677       !isPointerOperand(I->getOperand(1)))
678       return false;
679   return true;
680 }
681
682 bool AddressSanitizer::GlobalIsLinkerInitialized(GlobalVariable *G) {
683   // If a global variable does not have dynamic initialization we don't
684   // have to instrument it.  However, if a global does not have initializer
685   // at all, we assume it has dynamic initializer (in other TU).
686   return G->hasInitializer() && !DynamicallyInitializedGlobals.Contains(G);
687 }
688
689 void
690 AddressSanitizer::instrumentPointerComparisonOrSubtraction(Instruction *I) {
691   IRBuilder<> IRB(I);
692   Function *F = isa<ICmpInst>(I) ? AsanPtrCmpFunction : AsanPtrSubFunction;
693   Value *Param[2] = {I->getOperand(0), I->getOperand(1)};
694   for (int i = 0; i < 2; i++) {
695     if (Param[i]->getType()->isPointerTy())
696       Param[i] = IRB.CreatePointerCast(Param[i], IntptrTy);
697   }
698   IRB.CreateCall2(F, Param[0], Param[1]);
699 }
700
701 void AddressSanitizer::instrumentMop(Instruction *I) {
702   bool IsWrite = false;
703   Value *Addr = isInterestingMemoryAccess(I, &IsWrite);
704   assert(Addr);
705   if (ClOpt && ClOptGlobals) {
706     if (GlobalVariable *G = dyn_cast<GlobalVariable>(Addr)) {
707       // If initialization order checking is disabled, a simple access to a
708       // dynamically initialized global is always valid.
709       if (!CheckInitOrder || GlobalIsLinkerInitialized(G)) {
710         NumOptimizedAccessesToGlobalVar++;
711         return;
712       }
713     }
714     ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr);
715     if (CE && CE->isGEPWithNoNotionalOverIndexing()) {
716       if (GlobalVariable *G = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
717         if (CE->getOperand(1)->isNullValue() && GlobalIsLinkerInitialized(G)) {
718           NumOptimizedAccessesToGlobalArray++;
719           return;
720         }
721       }
722     }
723   }
724
725   Type *OrigPtrTy = Addr->getType();
726   Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
727
728   assert(OrigTy->isSized());
729   uint32_t TypeSize = DL->getTypeStoreSizeInBits(OrigTy);
730
731   assert((TypeSize % 8) == 0);
732
733   if (IsWrite)
734     NumInstrumentedWrites++;
735   else
736     NumInstrumentedReads++;
737
738   // Instrument a 1-, 2-, 4-, 8-, or 16- byte access with one check.
739   if (TypeSize == 8  || TypeSize == 16 ||
740       TypeSize == 32 || TypeSize == 64 || TypeSize == 128)
741     return instrumentAddress(I, I, Addr, TypeSize, IsWrite, 0);
742   // Instrument unusual size (but still multiple of 8).
743   // We can not do it with a single check, so we do 1-byte check for the first
744   // and the last bytes. We call __asan_report_*_n(addr, real_size) to be able
745   // to report the actual access size.
746   IRBuilder<> IRB(I);
747   Value *LastByte =  IRB.CreateIntToPtr(
748       IRB.CreateAdd(IRB.CreatePointerCast(Addr, IntptrTy),
749                     ConstantInt::get(IntptrTy, TypeSize / 8 - 1)),
750       OrigPtrTy);
751   Value *Size = ConstantInt::get(IntptrTy, TypeSize / 8);
752   instrumentAddress(I, I, Addr, 8, IsWrite, Size);
753   instrumentAddress(I, I, LastByte, 8, IsWrite, Size);
754 }
755
756 // Validate the result of Module::getOrInsertFunction called for an interface
757 // function of AddressSanitizer. If the instrumented module defines a function
758 // with the same name, their prototypes must match, otherwise
759 // getOrInsertFunction returns a bitcast.
760 static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
761   if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast);
762   FuncOrBitcast->dump();
763   report_fatal_error("trying to redefine an AddressSanitizer "
764                      "interface function");
765 }
766
767 Instruction *AddressSanitizer::generateCrashCode(
768     Instruction *InsertBefore, Value *Addr,
769     bool IsWrite, size_t AccessSizeIndex, Value *SizeArgument) {
770   IRBuilder<> IRB(InsertBefore);
771   CallInst *Call = SizeArgument
772     ? IRB.CreateCall2(AsanErrorCallbackSized[IsWrite], Addr, SizeArgument)
773     : IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex], Addr);
774
775   // We don't do Call->setDoesNotReturn() because the BB already has
776   // UnreachableInst at the end.
777   // This EmptyAsm is required to avoid callback merge.
778   IRB.CreateCall(EmptyAsm);
779   return Call;
780 }
781
782 Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
783                                             Value *ShadowValue,
784                                             uint32_t TypeSize) {
785   size_t Granularity = 1 << Mapping.Scale;
786   // Addr & (Granularity - 1)
787   Value *LastAccessedByte = IRB.CreateAnd(
788       AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
789   // (Addr & (Granularity - 1)) + size - 1
790   if (TypeSize / 8 > 1)
791     LastAccessedByte = IRB.CreateAdd(
792         LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
793   // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
794   LastAccessedByte = IRB.CreateIntCast(
795       LastAccessedByte, ShadowValue->getType(), false);
796   // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
797   return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
798 }
799
800 void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
801                                          Instruction *InsertBefore,
802                                          Value *Addr, uint32_t TypeSize,
803                                          bool IsWrite, Value *SizeArgument) {
804   IRBuilder<> IRB(InsertBefore);
805   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
806
807   Type *ShadowTy  = IntegerType::get(
808       *C, std::max(8U, TypeSize >> Mapping.Scale));
809   Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
810   Value *ShadowPtr = memToShadow(AddrLong, IRB);
811   Value *CmpVal = Constant::getNullValue(ShadowTy);
812   Value *ShadowValue = IRB.CreateLoad(
813       IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
814
815   Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
816   size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
817   size_t Granularity = 1 << Mapping.Scale;
818   TerminatorInst *CrashTerm = 0;
819
820   if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) {
821     TerminatorInst *CheckTerm =
822         SplitBlockAndInsertIfThen(Cmp, InsertBefore, false);
823     assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional());
824     BasicBlock *NextBB = CheckTerm->getSuccessor(0);
825     IRB.SetInsertPoint(CheckTerm);
826     Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
827     BasicBlock *CrashBlock =
828         BasicBlock::Create(*C, "", NextBB->getParent(), NextBB);
829     CrashTerm = new UnreachableInst(*C, CrashBlock);
830     BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
831     ReplaceInstWithInst(CheckTerm, NewTerm);
832   } else {
833     CrashTerm = SplitBlockAndInsertIfThen(Cmp, InsertBefore, true);
834   }
835
836   Instruction *Crash = generateCrashCode(
837       CrashTerm, AddrLong, IsWrite, AccessSizeIndex, SizeArgument);
838   Crash->setDebugLoc(OrigIns->getDebugLoc());
839 }
840
841 void AddressSanitizerModule::createInitializerPoisonCalls(
842     Module &M, GlobalValue *ModuleName) {
843   // We do all of our poisoning and unpoisoning within _GLOBAL__I_a.
844   Function *GlobalInit = M.getFunction("_GLOBAL__I_a");
845   // If that function is not present, this TU contains no globals, or they have
846   // all been optimized away
847   if (!GlobalInit)
848     return;
849
850   // Set up the arguments to our poison/unpoison functions.
851   IRBuilder<> IRB(GlobalInit->begin()->getFirstInsertionPt());
852
853   // Add a call to poison all external globals before the given function starts.
854   Value *ModuleNameAddr = ConstantExpr::getPointerCast(ModuleName, IntptrTy);
855   IRB.CreateCall(AsanPoisonGlobals, ModuleNameAddr);
856
857   // Add calls to unpoison all globals before each return instruction.
858   for (Function::iterator I = GlobalInit->begin(), E = GlobalInit->end();
859       I != E; ++I) {
860     if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) {
861       CallInst::Create(AsanUnpoisonGlobals, "", RI);
862     }
863   }
864 }
865
866 bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) {
867   Type *Ty = cast<PointerType>(G->getType())->getElementType();
868   DEBUG(dbgs() << "GLOBAL: " << *G << "\n");
869
870   if (BL->isIn(*G)) return false;
871   if (!Ty->isSized()) return false;
872   if (!G->hasInitializer()) return false;
873   if (GlobalWasGeneratedByAsan(G)) return false;  // Our own global.
874   // Touch only those globals that will not be defined in other modules.
875   // Don't handle ODR type linkages since other modules may be built w/o asan.
876   if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
877       G->getLinkage() != GlobalVariable::PrivateLinkage &&
878       G->getLinkage() != GlobalVariable::InternalLinkage)
879     return false;
880   // Two problems with thread-locals:
881   //   - The address of the main thread's copy can't be computed at link-time.
882   //   - Need to poison all copies, not just the main thread's one.
883   if (G->isThreadLocal())
884     return false;
885   // For now, just ignore this Global if the alignment is large.
886   if (G->getAlignment() > MinRedzoneSizeForGlobal()) return false;
887
888   // Ignore all the globals with the names starting with "\01L_OBJC_".
889   // Many of those are put into the .cstring section. The linker compresses
890   // that section by removing the spare \0s after the string terminator, so
891   // our redzones get broken.
892   if ((G->getName().find("\01L_OBJC_") == 0) ||
893       (G->getName().find("\01l_OBJC_") == 0)) {
894     DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G << "\n");
895     return false;
896   }
897
898   if (G->hasSection()) {
899     StringRef Section(G->getSection());
900     // Ignore the globals from the __OBJC section. The ObjC runtime assumes
901     // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
902     // them.
903     if ((Section.find("__OBJC,") == 0) ||
904         (Section.find("__DATA, __objc_") == 0)) {
905       DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G << "\n");
906       return false;
907     }
908     // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
909     // Constant CFString instances are compiled in the following way:
910     //  -- the string buffer is emitted into
911     //     __TEXT,__cstring,cstring_literals
912     //  -- the constant NSConstantString structure referencing that buffer
913     //     is placed into __DATA,__cfstring
914     // Therefore there's no point in placing redzones into __DATA,__cfstring.
915     // Moreover, it causes the linker to crash on OS X 10.7
916     if (Section.find("__DATA,__cfstring") == 0) {
917       DEBUG(dbgs() << "Ignoring CFString: " << *G << "\n");
918       return false;
919     }
920     // The linker merges the contents of cstring_literals and removes the
921     // trailing zeroes.
922     if (Section.find("__TEXT,__cstring,cstring_literals") == 0) {
923       DEBUG(dbgs() << "Ignoring a cstring literal: " << *G << "\n");
924       return false;
925     }
926     // Globals from llvm.metadata aren't emitted, do not instrument them.
927     if (Section == "llvm.metadata") return false;
928   }
929
930   return true;
931 }
932
933 void AddressSanitizerModule::initializeCallbacks(Module &M) {
934   IRBuilder<> IRB(*C);
935   // Declare our poisoning and unpoisoning functions.
936   AsanPoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
937       kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, NULL));
938   AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
939   AsanUnpoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
940       kAsanUnpoisonGlobalsName, IRB.getVoidTy(), NULL));
941   AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
942   // Declare functions that register/unregister globals.
943   AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
944       kAsanRegisterGlobalsName, IRB.getVoidTy(),
945       IntptrTy, IntptrTy, NULL));
946   AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
947   AsanUnregisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
948       kAsanUnregisterGlobalsName,
949       IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
950   AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
951 }
952
953 // This function replaces all global variables with new variables that have
954 // trailing redzones. It also creates a function that poisons
955 // redzones and inserts this function into llvm.global_ctors.
956 bool AddressSanitizerModule::runOnModule(Module &M) {
957   if (!ClGlobals) return false;
958
959   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
960   if (!DLP)
961     return false;
962   DL = &DLP->getDataLayout();
963
964   BL.reset(SpecialCaseList::createOrDie(BlacklistFile));
965   if (BL->isIn(M)) return false;
966   C = &(M.getContext());
967   int LongSize = DL->getPointerSizeInBits();
968   IntptrTy = Type::getIntNTy(*C, LongSize);
969   Mapping = getShadowMapping(M, LongSize);
970   initializeCallbacks(M);
971   DynamicallyInitializedGlobals.Init(M);
972
973   SmallVector<GlobalVariable *, 16> GlobalsToChange;
974
975   for (Module::GlobalListType::iterator G = M.global_begin(),
976        E = M.global_end(); G != E; ++G) {
977     if (ShouldInstrumentGlobal(G))
978       GlobalsToChange.push_back(G);
979   }
980
981   size_t n = GlobalsToChange.size();
982   if (n == 0) return false;
983
984   // A global is described by a structure
985   //   size_t beg;
986   //   size_t size;
987   //   size_t size_with_redzone;
988   //   const char *name;
989   //   const char *module_name;
990   //   size_t has_dynamic_init;
991   // We initialize an array of such structures and pass it to a run-time call.
992   StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy,
993                                                IntptrTy, IntptrTy,
994                                                IntptrTy, IntptrTy, NULL);
995   SmallVector<Constant *, 16> Initializers(n);
996
997   Function *CtorFunc = M.getFunction(kAsanModuleCtorName);
998   assert(CtorFunc);
999   IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator());
1000
1001   bool HasDynamicallyInitializedGlobals = false;
1002
1003   // We shouldn't merge same module names, as this string serves as unique
1004   // module ID in runtime.
1005   GlobalVariable *ModuleName = createPrivateGlobalForString(
1006       M, M.getModuleIdentifier(), /*AllowMerging*/false);
1007
1008   for (size_t i = 0; i < n; i++) {
1009     static const uint64_t kMaxGlobalRedzone = 1 << 18;
1010     GlobalVariable *G = GlobalsToChange[i];
1011     PointerType *PtrTy = cast<PointerType>(G->getType());
1012     Type *Ty = PtrTy->getElementType();
1013     uint64_t SizeInBytes = DL->getTypeAllocSize(Ty);
1014     uint64_t MinRZ = MinRedzoneSizeForGlobal();
1015     // MinRZ <= RZ <= kMaxGlobalRedzone
1016     // and trying to make RZ to be ~ 1/4 of SizeInBytes.
1017     uint64_t RZ = std::max(MinRZ,
1018                          std::min(kMaxGlobalRedzone,
1019                                   (SizeInBytes / MinRZ / 4) * MinRZ));
1020     uint64_t RightRedzoneSize = RZ;
1021     // Round up to MinRZ
1022     if (SizeInBytes % MinRZ)
1023       RightRedzoneSize += MinRZ - (SizeInBytes % MinRZ);
1024     assert(((RightRedzoneSize + SizeInBytes) % MinRZ) == 0);
1025     Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
1026     // Determine whether this global should be poisoned in initialization.
1027     bool GlobalHasDynamicInitializer =
1028         DynamicallyInitializedGlobals.Contains(G);
1029     // Don't check initialization order if this global is blacklisted.
1030     GlobalHasDynamicInitializer &= !BL->isIn(*G, "init");
1031
1032     StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
1033     Constant *NewInitializer = ConstantStruct::get(
1034         NewTy, G->getInitializer(),
1035         Constant::getNullValue(RightRedZoneTy), NULL);
1036
1037     GlobalVariable *Name =
1038         createPrivateGlobalForString(M, G->getName(), /*AllowMerging*/true);
1039
1040     // Create a new global variable with enough space for a redzone.
1041     GlobalValue::LinkageTypes Linkage = G->getLinkage();
1042     if (G->isConstant() && Linkage == GlobalValue::PrivateLinkage)
1043       Linkage = GlobalValue::InternalLinkage;
1044     GlobalVariable *NewGlobal = new GlobalVariable(
1045         M, NewTy, G->isConstant(), Linkage,
1046         NewInitializer, "", G, G->getThreadLocalMode());
1047     NewGlobal->copyAttributesFrom(G);
1048     NewGlobal->setAlignment(MinRZ);
1049
1050     Value *Indices2[2];
1051     Indices2[0] = IRB.getInt32(0);
1052     Indices2[1] = IRB.getInt32(0);
1053
1054     G->replaceAllUsesWith(
1055         ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true));
1056     NewGlobal->takeName(G);
1057     G->eraseFromParent();
1058
1059     Initializers[i] = ConstantStruct::get(
1060         GlobalStructTy,
1061         ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
1062         ConstantInt::get(IntptrTy, SizeInBytes),
1063         ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
1064         ConstantExpr::getPointerCast(Name, IntptrTy),
1065         ConstantExpr::getPointerCast(ModuleName, IntptrTy),
1066         ConstantInt::get(IntptrTy, GlobalHasDynamicInitializer),
1067         NULL);
1068
1069     // Populate the first and last globals declared in this TU.
1070     if (CheckInitOrder && GlobalHasDynamicInitializer)
1071       HasDynamicallyInitializedGlobals = true;
1072
1073     DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
1074   }
1075
1076   ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
1077   GlobalVariable *AllGlobals = new GlobalVariable(
1078       M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage,
1079       ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
1080
1081   // Create calls for poisoning before initializers run and unpoisoning after.
1082   if (CheckInitOrder && HasDynamicallyInitializedGlobals)
1083     createInitializerPoisonCalls(M, ModuleName);
1084   IRB.CreateCall2(AsanRegisterGlobals,
1085                   IRB.CreatePointerCast(AllGlobals, IntptrTy),
1086                   ConstantInt::get(IntptrTy, n));
1087
1088   // We also need to unregister globals at the end, e.g. when a shared library
1089   // gets closed.
1090   Function *AsanDtorFunction = Function::Create(
1091       FunctionType::get(Type::getVoidTy(*C), false),
1092       GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
1093   BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
1094   IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
1095   IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
1096                        IRB.CreatePointerCast(AllGlobals, IntptrTy),
1097                        ConstantInt::get(IntptrTy, n));
1098   appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority);
1099
1100   DEBUG(dbgs() << M);
1101   return true;
1102 }
1103
1104 void AddressSanitizer::initializeCallbacks(Module &M) {
1105   IRBuilder<> IRB(*C);
1106   // Create __asan_report* callbacks.
1107   for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
1108     for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
1109          AccessSizeIndex++) {
1110       // IsWrite and TypeSize are encoded in the function name.
1111       std::string FunctionName = std::string(kAsanReportErrorTemplate) +
1112           (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex);
1113       // If we are merging crash callbacks, they have two parameters.
1114       AsanErrorCallback[AccessIsWrite][AccessSizeIndex] =
1115           checkInterfaceFunction(M.getOrInsertFunction(
1116               FunctionName, IRB.getVoidTy(), IntptrTy, NULL));
1117     }
1118   }
1119   AsanErrorCallbackSized[0] = checkInterfaceFunction(M.getOrInsertFunction(
1120               kAsanReportLoadN, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1121   AsanErrorCallbackSized[1] = checkInterfaceFunction(M.getOrInsertFunction(
1122               kAsanReportStoreN, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1123
1124   AsanHandleNoReturnFunc = checkInterfaceFunction(M.getOrInsertFunction(
1125       kAsanHandleNoReturnName, IRB.getVoidTy(), NULL));
1126   AsanCovFunction = checkInterfaceFunction(M.getOrInsertFunction(
1127       kAsanCovName, IRB.getVoidTy(), NULL));
1128   AsanPtrCmpFunction = checkInterfaceFunction(M.getOrInsertFunction(
1129       kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1130   AsanPtrSubFunction = checkInterfaceFunction(M.getOrInsertFunction(
1131       kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1132   // We insert an empty inline asm after __asan_report* to avoid callback merge.
1133   EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
1134                             StringRef(""), StringRef(""),
1135                             /*hasSideEffects=*/true);
1136 }
1137
1138 // virtual
1139 bool AddressSanitizer::doInitialization(Module &M) {
1140   // Initialize the private fields. No one has accessed them before.
1141   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
1142   if (!DLP)
1143     return false;
1144   DL = &DLP->getDataLayout();
1145
1146   BL.reset(SpecialCaseList::createOrDie(BlacklistFile));
1147   DynamicallyInitializedGlobals.Init(M);
1148
1149   C = &(M.getContext());
1150   LongSize = DL->getPointerSizeInBits();
1151   IntptrTy = Type::getIntNTy(*C, LongSize);
1152
1153   AsanCtorFunction = Function::Create(
1154       FunctionType::get(Type::getVoidTy(*C), false),
1155       GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
1156   BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
1157   // call __asan_init in the module ctor.
1158   IRBuilder<> IRB(ReturnInst::Create(*C, AsanCtorBB));
1159   AsanInitFunction = checkInterfaceFunction(
1160       M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
1161   AsanInitFunction->setLinkage(Function::ExternalLinkage);
1162   IRB.CreateCall(AsanInitFunction);
1163
1164   Mapping = getShadowMapping(M, LongSize);
1165
1166   appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
1167   return true;
1168 }
1169
1170 bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
1171   // For each NSObject descendant having a +load method, this method is invoked
1172   // by the ObjC runtime before any of the static constructors is called.
1173   // Therefore we need to instrument such methods with a call to __asan_init
1174   // at the beginning in order to initialize our runtime before any access to
1175   // the shadow memory.
1176   // We cannot just ignore these methods, because they may call other
1177   // instrumented functions.
1178   if (F.getName().find(" load]") != std::string::npos) {
1179     IRBuilder<> IRB(F.begin()->begin());
1180     IRB.CreateCall(AsanInitFunction);
1181     return true;
1182   }
1183   return false;
1184 }
1185
1186 void AddressSanitizer::InjectCoverageAtBlock(Function &F, BasicBlock &BB) {
1187   BasicBlock::iterator IP = BB.getFirstInsertionPt(), BE = BB.end();
1188   // Skip static allocas at the top of the entry block so they don't become
1189   // dynamic when we split the block.  If we used our optimized stack layout,
1190   // then there will only be one alloca and it will come first.
1191   for (; IP != BE; ++IP) {
1192     AllocaInst *AI = dyn_cast<AllocaInst>(IP);
1193     if (!AI || !AI->isStaticAlloca())
1194       break;
1195   }
1196
1197   IRBuilder<> IRB(IP);
1198   Type *Int8Ty = IRB.getInt8Ty();
1199   GlobalVariable *Guard = new GlobalVariable(
1200       *F.getParent(), Int8Ty, false, GlobalValue::PrivateLinkage,
1201       Constant::getNullValue(Int8Ty), "__asan_gen_cov_" + F.getName());
1202   LoadInst *Load = IRB.CreateLoad(Guard);
1203   Load->setAtomic(Monotonic);
1204   Load->setAlignment(1);
1205   Value *Cmp = IRB.CreateICmpEQ(Constant::getNullValue(Int8Ty), Load);
1206   Instruction *Ins = SplitBlockAndInsertIfThen(
1207       Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
1208   IRB.SetInsertPoint(Ins);
1209   // We pass &F to __sanitizer_cov. We could avoid this and rely on
1210   // GET_CALLER_PC, but having the PC of the first instruction is just nice.
1211   Instruction *Call = IRB.CreateCall(AsanCovFunction);
1212   Call->setDebugLoc(IP->getDebugLoc());
1213   StoreInst *Store = IRB.CreateStore(ConstantInt::get(Int8Ty, 1), Guard);
1214   Store->setAtomic(Monotonic);
1215   Store->setAlignment(1);
1216 }
1217
1218 // Poor man's coverage that works with ASan.
1219 // We create a Guard boolean variable with the same linkage
1220 // as the function and inject this code into the entry block (-asan-coverage=1)
1221 // or all blocks (-asan-coverage=2):
1222 // if (*Guard) {
1223 //    __sanitizer_cov(&F);
1224 //    *Guard = 1;
1225 // }
1226 // The accesses to Guard are atomic. The rest of the logic is
1227 // in __sanitizer_cov (it's fine to call it more than once).
1228 //
1229 // This coverage implementation provides very limited data:
1230 // it only tells if a given function (block) was ever executed.
1231 // No counters, no per-edge data.
1232 // But for many use cases this is what we need and the added slowdown
1233 // is negligible. This simple implementation will probably be obsoleted
1234 // by the upcoming Clang-based coverage implementation.
1235 // By having it here and now we hope to
1236 //  a) get the functionality to users earlier and
1237 //  b) collect usage statistics to help improve Clang coverage design.
1238 bool AddressSanitizer::InjectCoverage(Function &F,
1239                                       const ArrayRef<BasicBlock *> AllBlocks) {
1240   if (!ClCoverage) return false;
1241
1242   if (ClCoverage == 1) {
1243     InjectCoverageAtBlock(F, F.getEntryBlock());
1244   } else {
1245     for (size_t i = 0, n = AllBlocks.size(); i < n; i++)
1246       InjectCoverageAtBlock(F, *AllBlocks[i]);
1247   }
1248   return true;
1249 }
1250
1251 bool AddressSanitizer::runOnFunction(Function &F) {
1252   if (BL->isIn(F)) return false;
1253   if (&F == AsanCtorFunction) return false;
1254   if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false;
1255   DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
1256   initializeCallbacks(*F.getParent());
1257
1258   // If needed, insert __asan_init before checking for SanitizeAddress attr.
1259   maybeInsertAsanInitAtFunctionEntry(F);
1260
1261   if (!F.hasFnAttribute(Attribute::SanitizeAddress))
1262     return false;
1263
1264   if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
1265     return false;
1266
1267   // We want to instrument every address only once per basic block (unless there
1268   // are calls between uses).
1269   SmallSet<Value*, 16> TempsToInstrument;
1270   SmallVector<Instruction*, 16> ToInstrument;
1271   SmallVector<Instruction*, 8> NoReturnCalls;
1272   SmallVector<BasicBlock*, 16> AllBlocks;
1273   SmallVector<Instruction*, 16> PointerComparisonsOrSubtracts;
1274   int NumAllocas = 0;
1275   bool IsWrite;
1276
1277   // Fill the set of memory operations to instrument.
1278   for (Function::iterator FI = F.begin(), FE = F.end();
1279        FI != FE; ++FI) {
1280     AllBlocks.push_back(FI);
1281     TempsToInstrument.clear();
1282     int NumInsnsPerBB = 0;
1283     for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
1284          BI != BE; ++BI) {
1285       if (LooksLikeCodeInBug11395(BI)) return false;
1286       if (Value *Addr = isInterestingMemoryAccess(BI, &IsWrite)) {
1287         if (ClOpt && ClOptSameTemp) {
1288           if (!TempsToInstrument.insert(Addr))
1289             continue;  // We've seen this temp in the current BB.
1290         }
1291       } else if (ClInvalidPointerPairs &&
1292                  isInterestingPointerComparisonOrSubtraction(BI)) {
1293         PointerComparisonsOrSubtracts.push_back(BI);
1294         continue;
1295       } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) {
1296         // ok, take it.
1297       } else {
1298         if (isa<AllocaInst>(BI))
1299           NumAllocas++;
1300         CallSite CS(BI);
1301         if (CS) {
1302           // A call inside BB.
1303           TempsToInstrument.clear();
1304           if (CS.doesNotReturn())
1305             NoReturnCalls.push_back(CS.getInstruction());
1306         }
1307         continue;
1308       }
1309       ToInstrument.push_back(BI);
1310       NumInsnsPerBB++;
1311       if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB)
1312         break;
1313     }
1314   }
1315
1316   Function *UninstrumentedDuplicate = 0;
1317   bool LikelyToInstrument =
1318       !NoReturnCalls.empty() || !ToInstrument.empty() || (NumAllocas > 0);
1319   if (ClKeepUninstrumented && LikelyToInstrument) {
1320     ValueToValueMapTy VMap;
1321     UninstrumentedDuplicate = CloneFunction(&F, VMap, false);
1322     UninstrumentedDuplicate->removeFnAttr(Attribute::SanitizeAddress);
1323     UninstrumentedDuplicate->setName("NOASAN_" + F.getName());
1324     F.getParent()->getFunctionList().push_back(UninstrumentedDuplicate);
1325   }
1326
1327   // Instrument.
1328   int NumInstrumented = 0;
1329   for (size_t i = 0, n = ToInstrument.size(); i != n; i++) {
1330     Instruction *Inst = ToInstrument[i];
1331     if (ClDebugMin < 0 || ClDebugMax < 0 ||
1332         (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
1333       if (isInterestingMemoryAccess(Inst, &IsWrite))
1334         instrumentMop(Inst);
1335       else
1336         instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
1337     }
1338     NumInstrumented++;
1339   }
1340
1341   FunctionStackPoisoner FSP(F, *this);
1342   bool ChangedStack = FSP.runOnFunction();
1343
1344   // We must unpoison the stack before every NoReturn call (throw, _exit, etc).
1345   // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37
1346   for (size_t i = 0, n = NoReturnCalls.size(); i != n; i++) {
1347     Instruction *CI = NoReturnCalls[i];
1348     IRBuilder<> IRB(CI);
1349     IRB.CreateCall(AsanHandleNoReturnFunc);
1350   }
1351
1352   for (size_t i = 0, n = PointerComparisonsOrSubtracts.size(); i != n; i++) {
1353     instrumentPointerComparisonOrSubtraction(PointerComparisonsOrSubtracts[i]);
1354     NumInstrumented++;
1355   }
1356
1357   bool res = NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
1358
1359   if (InjectCoverage(F, AllBlocks))
1360     res = true;
1361
1362   DEBUG(dbgs() << "ASAN done instrumenting: " << res << " " << F << "\n");
1363
1364   if (ClKeepUninstrumented) {
1365     if (!res) {
1366       // No instrumentation is done, no need for the duplicate.
1367       if (UninstrumentedDuplicate)
1368         UninstrumentedDuplicate->eraseFromParent();
1369     } else {
1370       // The function was instrumented. We must have the duplicate.
1371       assert(UninstrumentedDuplicate);
1372       UninstrumentedDuplicate->setSection("NOASAN");
1373       assert(!F.hasSection());
1374       F.setSection("ASAN");
1375     }
1376   }
1377
1378   return res;
1379 }
1380
1381 // Workaround for bug 11395: we don't want to instrument stack in functions
1382 // with large assembly blobs (32-bit only), otherwise reg alloc may crash.
1383 // FIXME: remove once the bug 11395 is fixed.
1384 bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
1385   if (LongSize != 32) return false;
1386   CallInst *CI = dyn_cast<CallInst>(I);
1387   if (!CI || !CI->isInlineAsm()) return false;
1388   if (CI->getNumArgOperands() <= 5) return false;
1389   // We have inline assembly with quite a few arguments.
1390   return true;
1391 }
1392
1393 void FunctionStackPoisoner::initializeCallbacks(Module &M) {
1394   IRBuilder<> IRB(*C);
1395   for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) {
1396     std::string Suffix = itostr(i);
1397     AsanStackMallocFunc[i] = checkInterfaceFunction(
1398         M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy,
1399                               IntptrTy, IntptrTy, NULL));
1400     AsanStackFreeFunc[i] = checkInterfaceFunction(M.getOrInsertFunction(
1401         kAsanStackFreeNameTemplate + Suffix, IRB.getVoidTy(), IntptrTy,
1402         IntptrTy, IntptrTy, NULL));
1403   }
1404   AsanPoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction(
1405       kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1406   AsanUnpoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction(
1407       kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1408 }
1409
1410 void
1411 FunctionStackPoisoner::poisonRedZones(const ArrayRef<uint8_t> ShadowBytes,
1412                                       IRBuilder<> &IRB, Value *ShadowBase,
1413                                       bool DoPoison) {
1414   size_t n = ShadowBytes.size();
1415   size_t i = 0;
1416   // We need to (un)poison n bytes of stack shadow. Poison as many as we can
1417   // using 64-bit stores (if we are on 64-bit arch), then poison the rest
1418   // with 32-bit stores, then with 16-byte stores, then with 8-byte stores.
1419   for (size_t LargeStoreSizeInBytes = ASan.LongSize / 8;
1420        LargeStoreSizeInBytes != 0; LargeStoreSizeInBytes /= 2) {
1421     for (; i + LargeStoreSizeInBytes - 1 < n; i += LargeStoreSizeInBytes) {
1422       uint64_t Val = 0;
1423       for (size_t j = 0; j < LargeStoreSizeInBytes; j++) {
1424         if (ASan.DL->isLittleEndian())
1425           Val |= (uint64_t)ShadowBytes[i + j] << (8 * j);
1426         else
1427           Val = (Val << 8) | ShadowBytes[i + j];
1428       }
1429       if (!Val) continue;
1430       Value *Ptr = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
1431       Type *StoreTy = Type::getIntNTy(*C, LargeStoreSizeInBytes * 8);
1432       Value *Poison = ConstantInt::get(StoreTy, DoPoison ? Val : 0);
1433       IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, StoreTy->getPointerTo()));
1434     }
1435   }
1436 }
1437
1438 // Fake stack allocator (asan_fake_stack.h) has 11 size classes
1439 // for every power of 2 from kMinStackMallocSize to kMaxAsanStackMallocSizeClass
1440 static int StackMallocSizeClass(uint64_t LocalStackSize) {
1441   assert(LocalStackSize <= kMaxStackMallocSize);
1442   uint64_t MaxSize = kMinStackMallocSize;
1443   for (int i = 0; ; i++, MaxSize *= 2)
1444     if (LocalStackSize <= MaxSize)
1445       return i;
1446   llvm_unreachable("impossible LocalStackSize");
1447 }
1448
1449 // Set Size bytes starting from ShadowBase to kAsanStackAfterReturnMagic.
1450 // We can not use MemSet intrinsic because it may end up calling the actual
1451 // memset. Size is a multiple of 8.
1452 // Currently this generates 8-byte stores on x86_64; it may be better to
1453 // generate wider stores.
1454 void FunctionStackPoisoner::SetShadowToStackAfterReturnInlined(
1455     IRBuilder<> &IRB, Value *ShadowBase, int Size) {
1456   assert(!(Size % 8));
1457   assert(kAsanStackAfterReturnMagic == 0xf5);
1458   for (int i = 0; i < Size; i += 8) {
1459     Value *p = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
1460     IRB.CreateStore(ConstantInt::get(IRB.getInt64Ty(), 0xf5f5f5f5f5f5f5f5ULL),
1461                     IRB.CreateIntToPtr(p, IRB.getInt64Ty()->getPointerTo()));
1462   }
1463 }
1464
1465 void FunctionStackPoisoner::poisonStack() {
1466   int StackMallocIdx = -1;
1467
1468   assert(AllocaVec.size() > 0);
1469   Instruction *InsBefore = AllocaVec[0];
1470   IRBuilder<> IRB(InsBefore);
1471
1472   SmallVector<ASanStackVariableDescription, 16> SVD;
1473   SVD.reserve(AllocaVec.size());
1474   for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
1475     AllocaInst *AI = AllocaVec[i];
1476     ASanStackVariableDescription D = { AI->getName().data(),
1477                                    getAllocaSizeInBytes(AI),
1478                                    AI->getAlignment(), AI, 0};
1479     SVD.push_back(D);
1480   }
1481   // Minimal header size (left redzone) is 4 pointers,
1482   // i.e. 32 bytes on 64-bit platforms and 16 bytes in 32-bit platforms.
1483   size_t MinHeaderSize = ASan.LongSize / 2;
1484   ASanStackFrameLayout L;
1485   ComputeASanStackFrameLayout(SVD, 1UL << Mapping.Scale, MinHeaderSize, &L);
1486   DEBUG(dbgs() << L.DescriptionString << " --- " << L.FrameSize << "\n");
1487   uint64_t LocalStackSize = L.FrameSize;
1488   bool DoStackMalloc =
1489       ASan.CheckUseAfterReturn && LocalStackSize <= kMaxStackMallocSize;
1490
1491   Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
1492   AllocaInst *MyAlloca =
1493       new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
1494   assert((ClRealignStack & (ClRealignStack - 1)) == 0);
1495   size_t FrameAlignment = std::max(L.FrameAlignment, (size_t)ClRealignStack);
1496   MyAlloca->setAlignment(FrameAlignment);
1497   assert(MyAlloca->isStaticAlloca());
1498   Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
1499   Value *LocalStackBase = OrigStackBase;
1500
1501   if (DoStackMalloc) {
1502     // LocalStackBase = OrigStackBase
1503     // if (__asan_option_detect_stack_use_after_return)
1504     //   LocalStackBase = __asan_stack_malloc_N(LocalStackBase, OrigStackBase);
1505     StackMallocIdx = StackMallocSizeClass(LocalStackSize);
1506     assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass);
1507     Constant *OptionDetectUAR = F.getParent()->getOrInsertGlobal(
1508         kAsanOptionDetectUAR, IRB.getInt32Ty());
1509     Value *Cmp = IRB.CreateICmpNE(IRB.CreateLoad(OptionDetectUAR),
1510                                   Constant::getNullValue(IRB.getInt32Ty()));
1511     Instruction *Term = SplitBlockAndInsertIfThen(Cmp, InsBefore, false);
1512     BasicBlock *CmpBlock = cast<Instruction>(Cmp)->getParent();
1513     IRBuilder<> IRBIf(Term);
1514     LocalStackBase = IRBIf.CreateCall2(
1515         AsanStackMallocFunc[StackMallocIdx],
1516         ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
1517     BasicBlock *SetBlock = cast<Instruction>(LocalStackBase)->getParent();
1518     IRB.SetInsertPoint(InsBefore);
1519     PHINode *Phi = IRB.CreatePHI(IntptrTy, 2);
1520     Phi->addIncoming(OrigStackBase, CmpBlock);
1521     Phi->addIncoming(LocalStackBase, SetBlock);
1522     LocalStackBase = Phi;
1523   }
1524
1525   // Insert poison calls for lifetime intrinsics for alloca.
1526   bool HavePoisonedAllocas = false;
1527   for (size_t i = 0, n = AllocaPoisonCallVec.size(); i < n; i++) {
1528     const AllocaPoisonCall &APC = AllocaPoisonCallVec[i];
1529     assert(APC.InsBefore);
1530     assert(APC.AI);
1531     IRBuilder<> IRB(APC.InsBefore);
1532     poisonAlloca(APC.AI, APC.Size, IRB, APC.DoPoison);
1533     HavePoisonedAllocas |= APC.DoPoison;
1534   }
1535
1536   // Replace Alloca instructions with base+offset.
1537   for (size_t i = 0, n = SVD.size(); i < n; i++) {
1538     AllocaInst *AI = SVD[i].AI;
1539     Value *NewAllocaPtr = IRB.CreateIntToPtr(
1540         IRB.CreateAdd(LocalStackBase,
1541                       ConstantInt::get(IntptrTy, SVD[i].Offset)),
1542         AI->getType());
1543     replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB);
1544     AI->replaceAllUsesWith(NewAllocaPtr);
1545   }
1546
1547   // The left-most redzone has enough space for at least 4 pointers.
1548   // Write the Magic value to redzone[0].
1549   Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
1550   IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
1551                   BasePlus0);
1552   // Write the frame description constant to redzone[1].
1553   Value *BasePlus1 = IRB.CreateIntToPtr(
1554     IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, ASan.LongSize/8)),
1555     IntptrPtrTy);
1556   GlobalVariable *StackDescriptionGlobal =
1557       createPrivateGlobalForString(*F.getParent(), L.DescriptionString,
1558                                    /*AllowMerging*/true);
1559   Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal,
1560                                              IntptrTy);
1561   IRB.CreateStore(Description, BasePlus1);
1562   // Write the PC to redzone[2].
1563   Value *BasePlus2 = IRB.CreateIntToPtr(
1564     IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy,
1565                                                    2 * ASan.LongSize/8)),
1566     IntptrPtrTy);
1567   IRB.CreateStore(IRB.CreatePointerCast(&F, IntptrTy), BasePlus2);
1568
1569   // Poison the stack redzones at the entry.
1570   Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB);
1571   poisonRedZones(L.ShadowBytes, IRB, ShadowBase, true);
1572
1573   // (Un)poison the stack before all ret instructions.
1574   for (size_t i = 0, n = RetVec.size(); i < n; i++) {
1575     Instruction *Ret = RetVec[i];
1576     IRBuilder<> IRBRet(Ret);
1577     // Mark the current frame as retired.
1578     IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
1579                        BasePlus0);
1580     if (DoStackMalloc) {
1581       assert(StackMallocIdx >= 0);
1582       // if LocalStackBase != OrigStackBase:
1583       //     // In use-after-return mode, poison the whole stack frame.
1584       //     if StackMallocIdx <= 4
1585       //         // For small sizes inline the whole thing:
1586       //         memset(ShadowBase, kAsanStackAfterReturnMagic, ShadowSize);
1587       //         **SavedFlagPtr(LocalStackBase) = 0
1588       //     else
1589       //         __asan_stack_free_N(LocalStackBase, OrigStackBase)
1590       // else
1591       //     <This is not a fake stack; unpoison the redzones>
1592       Value *Cmp = IRBRet.CreateICmpNE(LocalStackBase, OrigStackBase);
1593       TerminatorInst *ThenTerm, *ElseTerm;
1594       SplitBlockAndInsertIfThenElse(Cmp, Ret, &ThenTerm, &ElseTerm);
1595
1596       IRBuilder<> IRBPoison(ThenTerm);
1597       if (StackMallocIdx <= 4) {
1598         int ClassSize = kMinStackMallocSize << StackMallocIdx;
1599         SetShadowToStackAfterReturnInlined(IRBPoison, ShadowBase,
1600                                            ClassSize >> Mapping.Scale);
1601         Value *SavedFlagPtrPtr = IRBPoison.CreateAdd(
1602             LocalStackBase,
1603             ConstantInt::get(IntptrTy, ClassSize - ASan.LongSize / 8));
1604         Value *SavedFlagPtr = IRBPoison.CreateLoad(
1605             IRBPoison.CreateIntToPtr(SavedFlagPtrPtr, IntptrPtrTy));
1606         IRBPoison.CreateStore(
1607             Constant::getNullValue(IRBPoison.getInt8Ty()),
1608             IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getInt8PtrTy()));
1609       } else {
1610         // For larger frames call __asan_stack_free_*.
1611         IRBPoison.CreateCall3(AsanStackFreeFunc[StackMallocIdx], LocalStackBase,
1612                               ConstantInt::get(IntptrTy, LocalStackSize),
1613                               OrigStackBase);
1614       }
1615
1616       IRBuilder<> IRBElse(ElseTerm);
1617       poisonRedZones(L.ShadowBytes, IRBElse, ShadowBase, false);
1618     } else if (HavePoisonedAllocas) {
1619       // If we poisoned some allocas in llvm.lifetime analysis,
1620       // unpoison whole stack frame now.
1621       assert(LocalStackBase == OrigStackBase);
1622       poisonAlloca(LocalStackBase, LocalStackSize, IRBRet, false);
1623     } else {
1624       poisonRedZones(L.ShadowBytes, IRBRet, ShadowBase, false);
1625     }
1626   }
1627
1628   // We are done. Remove the old unused alloca instructions.
1629   for (size_t i = 0, n = AllocaVec.size(); i < n; i++)
1630     AllocaVec[i]->eraseFromParent();
1631 }
1632
1633 void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size,
1634                                          IRBuilder<> &IRB, bool DoPoison) {
1635   // For now just insert the call to ASan runtime.
1636   Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy);
1637   Value *SizeArg = ConstantInt::get(IntptrTy, Size);
1638   IRB.CreateCall2(DoPoison ? AsanPoisonStackMemoryFunc
1639                            : AsanUnpoisonStackMemoryFunc,
1640                   AddrArg, SizeArg);
1641 }
1642
1643 // Handling llvm.lifetime intrinsics for a given %alloca:
1644 // (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca.
1645 // (2) if %size is constant, poison memory for llvm.lifetime.end (to detect
1646 //     invalid accesses) and unpoison it for llvm.lifetime.start (the memory
1647 //     could be poisoned by previous llvm.lifetime.end instruction, as the
1648 //     variable may go in and out of scope several times, e.g. in loops).
1649 // (3) if we poisoned at least one %alloca in a function,
1650 //     unpoison the whole stack frame at function exit.
1651
1652 AllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) {
1653   if (AllocaInst *AI = dyn_cast<AllocaInst>(V))
1654     // We're intested only in allocas we can handle.
1655     return isInterestingAlloca(*AI) ? AI : 0;
1656   // See if we've already calculated (or started to calculate) alloca for a
1657   // given value.
1658   AllocaForValueMapTy::iterator I = AllocaForValue.find(V);
1659   if (I != AllocaForValue.end())
1660     return I->second;
1661   // Store 0 while we're calculating alloca for value V to avoid
1662   // infinite recursion if the value references itself.
1663   AllocaForValue[V] = 0;
1664   AllocaInst *Res = 0;
1665   if (CastInst *CI = dyn_cast<CastInst>(V))
1666     Res = findAllocaForValue(CI->getOperand(0));
1667   else if (PHINode *PN = dyn_cast<PHINode>(V)) {
1668     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1669       Value *IncValue = PN->getIncomingValue(i);
1670       // Allow self-referencing phi-nodes.
1671       if (IncValue == PN) continue;
1672       AllocaInst *IncValueAI = findAllocaForValue(IncValue);
1673       // AI for incoming values should exist and should all be equal.
1674       if (IncValueAI == 0 || (Res != 0 && IncValueAI != Res))
1675         return 0;
1676       Res = IncValueAI;
1677     }
1678   }
1679   if (Res != 0)
1680     AllocaForValue[V] = Res;
1681   return Res;
1682 }