Remove the Function::getFnAttributes method in favor of using the AttributeSet
[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 "BlackList.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/DepthFirstIterator.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/ADT/Triple.h"
29 #include "llvm/DataLayout.h"
30 #include "llvm/DIBuilder.h"
31 #include "llvm/Function.h"
32 #include "llvm/IRBuilder.h"
33 #include "llvm/InlineAsm.h"
34 #include "llvm/InstVisitor.h"
35 #include "llvm/IntrinsicInst.h"
36 #include "llvm/LLVMContext.h"
37 #include "llvm/Module.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/DataTypes.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Support/system_error.h"
43 #include "llvm/Target/TargetMachine.h"
44 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
45 #include "llvm/Transforms/Utils/Local.h"
46 #include "llvm/Transforms/Utils/ModuleUtils.h"
47 #include "llvm/Type.h"
48 #include <algorithm>
49 #include <string>
50
51 using namespace llvm;
52
53 static const uint64_t kDefaultShadowScale = 3;
54 static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
55 static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
56 static const uint64_t kDefaultShadowOffsetAndroid = 0;
57
58 static const size_t kMaxStackMallocSize = 1 << 16;  // 64K
59 static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
60 static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
61
62 static const char *kAsanModuleCtorName = "asan.module_ctor";
63 static const char *kAsanModuleDtorName = "asan.module_dtor";
64 static const int   kAsanCtorAndCtorPriority = 1;
65 static const char *kAsanReportErrorTemplate = "__asan_report_";
66 static const char *kAsanRegisterGlobalsName = "__asan_register_globals";
67 static const char *kAsanUnregisterGlobalsName = "__asan_unregister_globals";
68 static const char *kAsanPoisonGlobalsName = "__asan_before_dynamic_init";
69 static const char *kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init";
70 static const char *kAsanInitName = "__asan_init";
71 static const char *kAsanHandleNoReturnName = "__asan_handle_no_return";
72 static const char *kAsanMappingOffsetName = "__asan_mapping_offset";
73 static const char *kAsanMappingScaleName = "__asan_mapping_scale";
74 static const char *kAsanStackMallocName = "__asan_stack_malloc";
75 static const char *kAsanStackFreeName = "__asan_stack_free";
76 static const char *kAsanGenPrefix = "__asan_gen_";
77 static const char *kAsanPoisonStackMemoryName = "__asan_poison_stack_memory";
78 static const char *kAsanUnpoisonStackMemoryName =
79     "__asan_unpoison_stack_memory";
80
81 static const int kAsanStackLeftRedzoneMagic = 0xf1;
82 static const int kAsanStackMidRedzoneMagic = 0xf2;
83 static const int kAsanStackRightRedzoneMagic = 0xf3;
84 static const int kAsanStackPartialRedzoneMagic = 0xf4;
85
86 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
87 static const size_t kNumberOfAccessSizes = 5;
88
89 // Command-line flags.
90
91 // This flag may need to be replaced with -f[no-]asan-reads.
92 static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
93        cl::desc("instrument read instructions"), cl::Hidden, cl::init(true));
94 static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes",
95        cl::desc("instrument write instructions"), cl::Hidden, cl::init(true));
96 static cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics",
97        cl::desc("instrument atomic instructions (rmw, cmpxchg)"),
98        cl::Hidden, cl::init(true));
99 static cl::opt<bool> ClAlwaysSlowPath("asan-always-slow-path",
100        cl::desc("use instrumentation with slow path for all accesses"),
101        cl::Hidden, cl::init(false));
102 // This flag limits the number of instructions to be instrumented
103 // in any given BB. Normally, this should be set to unlimited (INT_MAX),
104 // but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
105 // set it to 10000.
106 static cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb",
107        cl::init(10000),
108        cl::desc("maximal number of instructions to instrument in any given BB"),
109        cl::Hidden);
110 // This flag may need to be replaced with -f[no]asan-stack.
111 static cl::opt<bool> ClStack("asan-stack",
112        cl::desc("Handle stack memory"), cl::Hidden, cl::init(true));
113 // This flag may need to be replaced with -f[no]asan-use-after-return.
114 static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
115        cl::desc("Check return-after-free"), cl::Hidden, cl::init(false));
116 // This flag may need to be replaced with -f[no]asan-globals.
117 static cl::opt<bool> ClGlobals("asan-globals",
118        cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
119 static cl::opt<bool> ClInitializers("asan-initialization-order",
120        cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(false));
121 static cl::opt<bool> ClMemIntrin("asan-memintrin",
122        cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
123 static cl::opt<bool> ClRealignStack("asan-realign-stack",
124        cl::desc("Realign stack to 32"), cl::Hidden, cl::init(true));
125 static cl::opt<std::string> ClBlacklistFile("asan-blacklist",
126        cl::desc("File containing the list of objects to ignore "
127                 "during instrumentation"), cl::Hidden);
128
129 // These flags allow to change the shadow mapping.
130 // The shadow mapping looks like
131 //    Shadow = (Mem >> scale) + (1 << offset_log)
132 static cl::opt<int> ClMappingScale("asan-mapping-scale",
133        cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0));
134 static cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log",
135        cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1));
136
137 // Optimization flags. Not user visible, used mostly for testing
138 // and benchmarking the tool.
139 static cl::opt<bool> ClOpt("asan-opt",
140        cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true));
141 static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp",
142        cl::desc("Instrument the same temp just once"), cl::Hidden,
143        cl::init(true));
144 static cl::opt<bool> ClOptGlobals("asan-opt-globals",
145        cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
146
147 static cl::opt<bool> ClCheckLifetime("asan-check-lifetime",
148        cl::desc("Use llvm.lifetime intrinsics to insert extra checks"),
149        cl::Hidden, cl::init(false));
150
151 // Debug flags.
152 static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
153                             cl::init(0));
154 static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
155                                  cl::Hidden, cl::init(0));
156 static cl::opt<std::string> ClDebugFunc("asan-debug-func",
157                                         cl::Hidden, cl::desc("Debug func"));
158 static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
159                                cl::Hidden, cl::init(-1));
160 static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
161                                cl::Hidden, cl::init(-1));
162
163 namespace {
164 /// A set of dynamically initialized globals extracted from metadata.
165 class SetOfDynamicallyInitializedGlobals {
166  public:
167   void Init(Module& M) {
168     // Clang generates metadata identifying all dynamically initialized globals.
169     NamedMDNode *DynamicGlobals =
170         M.getNamedMetadata("llvm.asan.dynamically_initialized_globals");
171     if (!DynamicGlobals)
172       return;
173     for (int i = 0, n = DynamicGlobals->getNumOperands(); i < n; ++i) {
174       MDNode *MDN = DynamicGlobals->getOperand(i);
175       assert(MDN->getNumOperands() == 1);
176       Value *VG = MDN->getOperand(0);
177       // The optimizer may optimize away a global entirely, in which case we
178       // cannot instrument access to it.
179       if (!VG)
180         continue;
181       DynInitGlobals.insert(cast<GlobalVariable>(VG));
182     }
183   }
184   bool Contains(GlobalVariable *G) { return DynInitGlobals.count(G) != 0; }
185  private:
186   SmallSet<GlobalValue*, 32> DynInitGlobals;
187 };
188
189 static int MappingScale() {
190   return ClMappingScale ? ClMappingScale : kDefaultShadowScale;
191 }
192
193 static size_t RedzoneSize() {
194   // Redzone used for stack and globals is at least 32 bytes.
195   // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
196   return std::max(32U, 1U << MappingScale());
197 }
198
199 /// AddressSanitizer: instrument the code in module to find memory bugs.
200 struct AddressSanitizer : public FunctionPass {
201   AddressSanitizer(bool CheckInitOrder = false,
202                    bool CheckUseAfterReturn = false,
203                    bool CheckLifetime = false,
204                    StringRef BlacklistFile = StringRef())
205       : FunctionPass(ID),
206         CheckInitOrder(CheckInitOrder || ClInitializers),
207         CheckUseAfterReturn(CheckUseAfterReturn || ClUseAfterReturn),
208         CheckLifetime(CheckLifetime || ClCheckLifetime),
209         BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
210                                             : BlacklistFile) {}
211   virtual const char *getPassName() const {
212     return "AddressSanitizerFunctionPass";
213   }
214   void instrumentMop(Instruction *I);
215   void instrumentAddress(Instruction *OrigIns, IRBuilder<> &IRB,
216                          Value *Addr, uint32_t TypeSize, bool IsWrite);
217   Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
218                            Value *ShadowValue, uint32_t TypeSize);
219   Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr,
220                                  bool IsWrite, size_t AccessSizeIndex);
221   bool instrumentMemIntrinsic(MemIntrinsic *MI);
222   void instrumentMemIntrinsicParam(Instruction *OrigIns, Value *Addr,
223                                    Value *Size,
224                                    Instruction *InsertBefore, bool IsWrite);
225   Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
226   bool runOnFunction(Function &F);
227   void createInitializerPoisonCalls(Module &M,
228                                     Value *FirstAddr, Value *LastAddr);
229   bool maybeInsertAsanInitAtFunctionEntry(Function &F);
230   virtual bool doInitialization(Module &M);
231   static char ID;  // Pass identification, replacement for typeid
232
233  private:
234   void initializeCallbacks(Module &M);
235
236   bool ShouldInstrumentGlobal(GlobalVariable *G);
237   bool LooksLikeCodeInBug11395(Instruction *I);
238   void FindDynamicInitializers(Module &M);
239
240   bool CheckInitOrder;
241   bool CheckUseAfterReturn;
242   bool CheckLifetime;
243   LLVMContext *C;
244   DataLayout *TD;
245   uint64_t MappingOffset;
246   int LongSize;
247   Type *IntptrTy;
248   Function *AsanCtorFunction;
249   Function *AsanInitFunction;
250   Function *AsanHandleNoReturnFunc;
251   SmallString<64> BlacklistFile;
252   OwningPtr<BlackList> BL;
253   // This array is indexed by AccessIsWrite and log2(AccessSize).
254   Function *AsanErrorCallback[2][kNumberOfAccessSizes];
255   InlineAsm *EmptyAsm;
256   SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals;
257
258   friend struct FunctionStackPoisoner;
259 };
260
261 class AddressSanitizerModule : public ModulePass {
262  public:
263   AddressSanitizerModule(bool CheckInitOrder = false,
264                          StringRef BlacklistFile = StringRef())
265       : ModulePass(ID),
266         CheckInitOrder(CheckInitOrder || ClInitializers),
267         BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
268                                             : BlacklistFile) {}
269   bool runOnModule(Module &M);
270   static char ID;  // Pass identification, replacement for typeid
271   virtual const char *getPassName() const {
272     return "AddressSanitizerModule";
273   }
274
275  private:
276   void initializeCallbacks(Module &M);
277
278   bool ShouldInstrumentGlobal(GlobalVariable *G);
279   void createInitializerPoisonCalls(Module &M, Value *FirstAddr,
280                                     Value *LastAddr);
281
282   bool CheckInitOrder;
283   SmallString<64> BlacklistFile;
284   OwningPtr<BlackList> BL;
285   SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals;
286   Type *IntptrTy;
287   LLVMContext *C;
288   DataLayout *TD;
289   Function *AsanPoisonGlobals;
290   Function *AsanUnpoisonGlobals;
291   Function *AsanRegisterGlobals;
292   Function *AsanUnregisterGlobals;
293 };
294
295 // Stack poisoning does not play well with exception handling.
296 // When an exception is thrown, we essentially bypass the code
297 // that unpoisones the stack. This is why the run-time library has
298 // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
299 // stack in the interceptor. This however does not work inside the
300 // actual function which catches the exception. Most likely because the
301 // compiler hoists the load of the shadow value somewhere too high.
302 // This causes asan to report a non-existing bug on 453.povray.
303 // It sounds like an LLVM bug.
304 struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
305   Function &F;
306   AddressSanitizer &ASan;
307   DIBuilder DIB;
308   LLVMContext *C;
309   Type *IntptrTy;
310   Type *IntptrPtrTy;
311
312   SmallVector<AllocaInst*, 16> AllocaVec;
313   SmallVector<Instruction*, 8> RetVec;
314   uint64_t TotalStackSize;
315   unsigned StackAlignment;
316
317   Function *AsanStackMallocFunc, *AsanStackFreeFunc;
318   Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc;
319
320   // Stores a place and arguments of poisoning/unpoisoning call for alloca.
321   struct AllocaPoisonCall {
322     IntrinsicInst *InsBefore;
323     uint64_t Size;
324     bool DoPoison;
325   };
326   SmallVector<AllocaPoisonCall, 8> AllocaPoisonCallVec;
327
328   // Maps Value to an AllocaInst from which the Value is originated.
329   typedef DenseMap<Value*, AllocaInst*> AllocaForValueMapTy;
330   AllocaForValueMapTy AllocaForValue;
331
332   FunctionStackPoisoner(Function &F, AddressSanitizer &ASan)
333       : F(F), ASan(ASan), DIB(*F.getParent()), C(ASan.C),
334         IntptrTy(ASan.IntptrTy), IntptrPtrTy(PointerType::get(IntptrTy, 0)),
335         TotalStackSize(0), StackAlignment(1 << MappingScale()) {}
336
337   bool runOnFunction() {
338     if (!ClStack) return false;
339     // Collect alloca, ret, lifetime instructions etc.
340     for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
341          DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) {
342       BasicBlock *BB = *DI;
343       visit(*BB);
344     }
345     if (AllocaVec.empty()) return false;
346
347     initializeCallbacks(*F.getParent());
348
349     poisonStack();
350
351     if (ClDebugStack) {
352       DEBUG(dbgs() << F);
353     }
354     return true;
355   }
356
357   // Finds all static Alloca instructions and puts
358   // poisoned red zones around all of them.
359   // Then unpoison everything back before the function returns.
360   void poisonStack();
361
362   // ----------------------- Visitors.
363   /// \brief Collect all Ret instructions.
364   void visitReturnInst(ReturnInst &RI) {
365     RetVec.push_back(&RI);
366   }
367
368   /// \brief Collect Alloca instructions we want (and can) handle.
369   void visitAllocaInst(AllocaInst &AI) {
370     if (!isInterestingAlloca(AI)) return;
371
372     StackAlignment = std::max(StackAlignment, AI.getAlignment());
373     AllocaVec.push_back(&AI);
374     uint64_t AlignedSize =  getAlignedAllocaSize(&AI);
375     TotalStackSize += AlignedSize;
376   }
377
378   /// \brief Collect lifetime intrinsic calls to check for use-after-scope
379   /// errors.
380   void visitIntrinsicInst(IntrinsicInst &II) {
381     if (!ASan.CheckLifetime) return;
382     Intrinsic::ID ID = II.getIntrinsicID();
383     if (ID != Intrinsic::lifetime_start &&
384         ID != Intrinsic::lifetime_end)
385       return;
386     // Found lifetime intrinsic, add ASan instrumentation if necessary.
387     ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0));
388     // If size argument is undefined, don't do anything.
389     if (Size->isMinusOne()) return;
390     // Check that size doesn't saturate uint64_t and can
391     // be stored in IntptrTy.
392     const uint64_t SizeValue = Size->getValue().getLimitedValue();
393     if (SizeValue == ~0ULL ||
394         !ConstantInt::isValueValidForType(IntptrTy, SizeValue))
395       return;
396     // Find alloca instruction that corresponds to llvm.lifetime argument.
397     AllocaInst *AI = findAllocaForValue(II.getArgOperand(1));
398     if (!AI) return;
399     bool DoPoison = (ID == Intrinsic::lifetime_end);
400     AllocaPoisonCall APC = {&II, SizeValue, DoPoison};
401     AllocaPoisonCallVec.push_back(APC);
402   }
403
404   // ---------------------- Helpers.
405   void initializeCallbacks(Module &M);
406
407   // Check if we want (and can) handle this alloca.
408   bool isInterestingAlloca(AllocaInst &AI) {
409     return (!AI.isArrayAllocation() &&
410             AI.isStaticAlloca() &&
411             AI.getAllocatedType()->isSized());
412   }
413
414   uint64_t getAllocaSizeInBytes(AllocaInst *AI) {
415     Type *Ty = AI->getAllocatedType();
416     uint64_t SizeInBytes = ASan.TD->getTypeAllocSize(Ty);
417     return SizeInBytes;
418   }
419   uint64_t getAlignedSize(uint64_t SizeInBytes) {
420     size_t RZ = RedzoneSize();
421     return ((SizeInBytes + RZ - 1) / RZ) * RZ;
422   }
423   uint64_t getAlignedAllocaSize(AllocaInst *AI) {
424     uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
425     return getAlignedSize(SizeInBytes);
426   }
427   /// Finds alloca where the value comes from.
428   AllocaInst *findAllocaForValue(Value *V);
429   void poisonRedZones(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB,
430                       Value *ShadowBase, bool DoPoison);
431   void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> IRB, bool DoPoison);
432 };
433
434 }  // namespace
435
436 char AddressSanitizer::ID = 0;
437 INITIALIZE_PASS(AddressSanitizer, "asan",
438     "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
439     false, false)
440 FunctionPass *llvm::createAddressSanitizerFunctionPass(
441     bool CheckInitOrder, bool CheckUseAfterReturn, bool CheckLifetime,
442     StringRef BlacklistFile) {
443   return new AddressSanitizer(CheckInitOrder, CheckUseAfterReturn,
444                               CheckLifetime, BlacklistFile);
445 }
446
447 char AddressSanitizerModule::ID = 0;
448 INITIALIZE_PASS(AddressSanitizerModule, "asan-module",
449     "AddressSanitizer: detects use-after-free and out-of-bounds bugs."
450     "ModulePass", false, false)
451 ModulePass *llvm::createAddressSanitizerModulePass(
452     bool CheckInitOrder, StringRef BlacklistFile) {
453   return new AddressSanitizerModule(CheckInitOrder, BlacklistFile);
454 }
455
456 static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
457   size_t Res = CountTrailingZeros_32(TypeSize / 8);
458   assert(Res < kNumberOfAccessSizes);
459   return Res;
460 }
461
462 // Create a constant for Str so that we can pass it to the run-time lib.
463 static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) {
464   Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
465   return new GlobalVariable(M, StrConst->getType(), true,
466                             GlobalValue::PrivateLinkage, StrConst,
467                             kAsanGenPrefix);
468 }
469
470 static bool GlobalWasGeneratedByAsan(GlobalVariable *G) {
471   return G->getName().find(kAsanGenPrefix) == 0;
472 }
473
474 Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
475   // Shadow >> scale
476   Shadow = IRB.CreateLShr(Shadow, MappingScale());
477   if (MappingOffset == 0)
478     return Shadow;
479   // (Shadow >> scale) | offset
480   return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy,
481                                                MappingOffset));
482 }
483
484 void AddressSanitizer::instrumentMemIntrinsicParam(
485     Instruction *OrigIns,
486     Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) {
487   // Check the first byte.
488   {
489     IRBuilder<> IRB(InsertBefore);
490     instrumentAddress(OrigIns, IRB, Addr, 8, IsWrite);
491   }
492   // Check the last byte.
493   {
494     IRBuilder<> IRB(InsertBefore);
495     Value *SizeMinusOne = IRB.CreateSub(
496         Size, ConstantInt::get(Size->getType(), 1));
497     SizeMinusOne = IRB.CreateIntCast(SizeMinusOne, IntptrTy, false);
498     Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
499     Value *AddrPlusSizeMinisOne = IRB.CreateAdd(AddrLong, SizeMinusOne);
500     instrumentAddress(OrigIns, IRB, AddrPlusSizeMinisOne, 8, IsWrite);
501   }
502 }
503
504 // Instrument memset/memmove/memcpy
505 bool AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
506   Value *Dst = MI->getDest();
507   MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI);
508   Value *Src = MemTran ? MemTran->getSource() : 0;
509   Value *Length = MI->getLength();
510
511   Constant *ConstLength = dyn_cast<Constant>(Length);
512   Instruction *InsertBefore = MI;
513   if (ConstLength) {
514     if (ConstLength->isNullValue()) return false;
515   } else {
516     // The size is not a constant so it could be zero -- check at run-time.
517     IRBuilder<> IRB(InsertBefore);
518
519     Value *Cmp = IRB.CreateICmpNE(Length,
520                                   Constant::getNullValue(Length->getType()));
521     InsertBefore = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false);
522   }
523
524   instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true);
525   if (Src)
526     instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false);
527   return true;
528 }
529
530 // If I is an interesting memory access, return the PointerOperand
531 // and set IsWrite. Otherwise return NULL.
532 static Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite) {
533   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
534     if (!ClInstrumentReads) return NULL;
535     *IsWrite = false;
536     return LI->getPointerOperand();
537   }
538   if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
539     if (!ClInstrumentWrites) return NULL;
540     *IsWrite = true;
541     return SI->getPointerOperand();
542   }
543   if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
544     if (!ClInstrumentAtomics) return NULL;
545     *IsWrite = true;
546     return RMW->getPointerOperand();
547   }
548   if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
549     if (!ClInstrumentAtomics) return NULL;
550     *IsWrite = true;
551     return XCHG->getPointerOperand();
552   }
553   return NULL;
554 }
555
556 void AddressSanitizer::instrumentMop(Instruction *I) {
557   bool IsWrite = false;
558   Value *Addr = isInterestingMemoryAccess(I, &IsWrite);
559   assert(Addr);
560   if (ClOpt && ClOptGlobals) {
561     if (GlobalVariable *G = dyn_cast<GlobalVariable>(Addr)) {
562       // If initialization order checking is disabled, a simple access to a
563       // dynamically initialized global is always valid.
564       if (!CheckInitOrder)
565         return;
566       // If a global variable does not have dynamic initialization we don't
567       // have to instrument it.  However, if a global does not have initailizer
568       // at all, we assume it has dynamic initializer (in other TU).
569       if (G->hasInitializer() && !DynamicallyInitializedGlobals.Contains(G))
570         return;
571     }
572   }
573
574   Type *OrigPtrTy = Addr->getType();
575   Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
576
577   assert(OrigTy->isSized());
578   uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
579
580   if (TypeSize != 8  && TypeSize != 16 &&
581       TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
582     // Ignore all unusual sizes.
583     return;
584   }
585
586   IRBuilder<> IRB(I);
587   instrumentAddress(I, IRB, Addr, TypeSize, IsWrite);
588 }
589
590 // Validate the result of Module::getOrInsertFunction called for an interface
591 // function of AddressSanitizer. If the instrumented module defines a function
592 // with the same name, their prototypes must match, otherwise
593 // getOrInsertFunction returns a bitcast.
594 static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
595   if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast);
596   FuncOrBitcast->dump();
597   report_fatal_error("trying to redefine an AddressSanitizer "
598                      "interface function");
599 }
600
601 Instruction *AddressSanitizer::generateCrashCode(
602     Instruction *InsertBefore, Value *Addr,
603     bool IsWrite, size_t AccessSizeIndex) {
604   IRBuilder<> IRB(InsertBefore);
605   CallInst *Call = IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex],
606                                   Addr);
607   // We don't do Call->setDoesNotReturn() because the BB already has
608   // UnreachableInst at the end.
609   // This EmptyAsm is required to avoid callback merge.
610   IRB.CreateCall(EmptyAsm);
611   return Call;
612 }
613
614 Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
615                                             Value *ShadowValue,
616                                             uint32_t TypeSize) {
617   size_t Granularity = 1 << MappingScale();
618   // Addr & (Granularity - 1)
619   Value *LastAccessedByte = IRB.CreateAnd(
620       AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
621   // (Addr & (Granularity - 1)) + size - 1
622   if (TypeSize / 8 > 1)
623     LastAccessedByte = IRB.CreateAdd(
624         LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
625   // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
626   LastAccessedByte = IRB.CreateIntCast(
627       LastAccessedByte, ShadowValue->getType(), false);
628   // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
629   return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
630 }
631
632 void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
633                                          IRBuilder<> &IRB, Value *Addr,
634                                          uint32_t TypeSize, bool IsWrite) {
635   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
636
637   Type *ShadowTy  = IntegerType::get(
638       *C, std::max(8U, TypeSize >> MappingScale()));
639   Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
640   Value *ShadowPtr = memToShadow(AddrLong, IRB);
641   Value *CmpVal = Constant::getNullValue(ShadowTy);
642   Value *ShadowValue = IRB.CreateLoad(
643       IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
644
645   Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
646   size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
647   size_t Granularity = 1 << MappingScale();
648   TerminatorInst *CrashTerm = 0;
649
650   if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) {
651     TerminatorInst *CheckTerm =
652         SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false);
653     assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional());
654     BasicBlock *NextBB = CheckTerm->getSuccessor(0);
655     IRB.SetInsertPoint(CheckTerm);
656     Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
657     BasicBlock *CrashBlock =
658         BasicBlock::Create(*C, "", NextBB->getParent(), NextBB);
659     CrashTerm = new UnreachableInst(*C, CrashBlock);
660     BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
661     ReplaceInstWithInst(CheckTerm, NewTerm);
662   } else {
663     CrashTerm = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), true);
664   }
665
666   Instruction *Crash =
667       generateCrashCode(CrashTerm, AddrLong, IsWrite, AccessSizeIndex);
668   Crash->setDebugLoc(OrigIns->getDebugLoc());
669 }
670
671 void AddressSanitizerModule::createInitializerPoisonCalls(
672     Module &M, Value *FirstAddr, Value *LastAddr) {
673   // We do all of our poisoning and unpoisoning within _GLOBAL__I_a.
674   Function *GlobalInit = M.getFunction("_GLOBAL__I_a");
675   // If that function is not present, this TU contains no globals, or they have
676   // all been optimized away
677   if (!GlobalInit)
678     return;
679
680   // Set up the arguments to our poison/unpoison functions.
681   IRBuilder<> IRB(GlobalInit->begin()->getFirstInsertionPt());
682
683   // Add a call to poison all external globals before the given function starts.
684   IRB.CreateCall2(AsanPoisonGlobals, FirstAddr, LastAddr);
685
686   // Add calls to unpoison all globals before each return instruction.
687   for (Function::iterator I = GlobalInit->begin(), E = GlobalInit->end();
688       I != E; ++I) {
689     if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) {
690       CallInst::Create(AsanUnpoisonGlobals, "", RI);
691     }
692   }
693 }
694
695 bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) {
696   Type *Ty = cast<PointerType>(G->getType())->getElementType();
697   DEBUG(dbgs() << "GLOBAL: " << *G << "\n");
698
699   if (BL->isIn(*G)) return false;
700   if (!Ty->isSized()) return false;
701   if (!G->hasInitializer()) return false;
702   if (GlobalWasGeneratedByAsan(G)) return false;  // Our own global.
703   // Touch only those globals that will not be defined in other modules.
704   // Don't handle ODR type linkages since other modules may be built w/o asan.
705   if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
706       G->getLinkage() != GlobalVariable::PrivateLinkage &&
707       G->getLinkage() != GlobalVariable::InternalLinkage)
708     return false;
709   // Two problems with thread-locals:
710   //   - The address of the main thread's copy can't be computed at link-time.
711   //   - Need to poison all copies, not just the main thread's one.
712   if (G->isThreadLocal())
713     return false;
714   // For now, just ignore this Alloca if the alignment is large.
715   if (G->getAlignment() > RedzoneSize()) return false;
716
717   // Ignore all the globals with the names starting with "\01L_OBJC_".
718   // Many of those are put into the .cstring section. The linker compresses
719   // that section by removing the spare \0s after the string terminator, so
720   // our redzones get broken.
721   if ((G->getName().find("\01L_OBJC_") == 0) ||
722       (G->getName().find("\01l_OBJC_") == 0)) {
723     DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G);
724     return false;
725   }
726
727   if (G->hasSection()) {
728     StringRef Section(G->getSection());
729     // Ignore the globals from the __OBJC section. The ObjC runtime assumes
730     // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
731     // them.
732     if ((Section.find("__OBJC,") == 0) ||
733         (Section.find("__DATA, __objc_") == 0)) {
734       DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G);
735       return false;
736     }
737     // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
738     // Constant CFString instances are compiled in the following way:
739     //  -- the string buffer is emitted into
740     //     __TEXT,__cstring,cstring_literals
741     //  -- the constant NSConstantString structure referencing that buffer
742     //     is placed into __DATA,__cfstring
743     // Therefore there's no point in placing redzones into __DATA,__cfstring.
744     // Moreover, it causes the linker to crash on OS X 10.7
745     if (Section.find("__DATA,__cfstring") == 0) {
746       DEBUG(dbgs() << "Ignoring CFString: " << *G);
747       return false;
748     }
749   }
750
751   return true;
752 }
753
754 void AddressSanitizerModule::initializeCallbacks(Module &M) {
755   IRBuilder<> IRB(*C);
756   // Declare our poisoning and unpoisoning functions.
757   AsanPoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
758       kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
759   AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
760   AsanUnpoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
761       kAsanUnpoisonGlobalsName, IRB.getVoidTy(), NULL));
762   AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
763   // Declare functions that register/unregister globals.
764   AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
765       kAsanRegisterGlobalsName, IRB.getVoidTy(),
766       IntptrTy, IntptrTy, NULL));
767   AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
768   AsanUnregisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
769       kAsanUnregisterGlobalsName,
770       IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
771   AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
772 }
773
774 // This function replaces all global variables with new variables that have
775 // trailing redzones. It also creates a function that poisons
776 // redzones and inserts this function into llvm.global_ctors.
777 bool AddressSanitizerModule::runOnModule(Module &M) {
778   if (!ClGlobals) return false;
779   TD = getAnalysisIfAvailable<DataLayout>();
780   if (!TD)
781     return false;
782   BL.reset(new BlackList(BlacklistFile));
783   if (BL->isIn(M)) return false;
784   C = &(M.getContext());
785   IntptrTy = Type::getIntNTy(*C, TD->getPointerSizeInBits());
786   initializeCallbacks(M);
787   DynamicallyInitializedGlobals.Init(M);
788
789   SmallVector<GlobalVariable *, 16> GlobalsToChange;
790
791   for (Module::GlobalListType::iterator G = M.global_begin(),
792        E = M.global_end(); G != E; ++G) {
793     if (ShouldInstrumentGlobal(G))
794       GlobalsToChange.push_back(G);
795   }
796
797   size_t n = GlobalsToChange.size();
798   if (n == 0) return false;
799
800   // A global is described by a structure
801   //   size_t beg;
802   //   size_t size;
803   //   size_t size_with_redzone;
804   //   const char *name;
805   //   size_t has_dynamic_init;
806   // We initialize an array of such structures and pass it to a run-time call.
807   StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy,
808                                                IntptrTy, IntptrTy,
809                                                IntptrTy, NULL);
810   SmallVector<Constant *, 16> Initializers(n), DynamicInit;
811
812
813   Function *CtorFunc = M.getFunction(kAsanModuleCtorName);
814   assert(CtorFunc);
815   IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator());
816
817   // The addresses of the first and last dynamically initialized globals in
818   // this TU.  Used in initialization order checking.
819   Value *FirstDynamic = 0, *LastDynamic = 0;
820
821   for (size_t i = 0; i < n; i++) {
822     GlobalVariable *G = GlobalsToChange[i];
823     PointerType *PtrTy = cast<PointerType>(G->getType());
824     Type *Ty = PtrTy->getElementType();
825     uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
826     size_t RZ = RedzoneSize();
827     uint64_t RightRedzoneSize = RZ + (RZ - (SizeInBytes % RZ));
828     Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
829     // Determine whether this global should be poisoned in initialization.
830     bool GlobalHasDynamicInitializer =
831         DynamicallyInitializedGlobals.Contains(G);
832     // Don't check initialization order if this global is blacklisted.
833     GlobalHasDynamicInitializer &= !BL->isInInit(*G);
834
835     StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
836     Constant *NewInitializer = ConstantStruct::get(
837         NewTy, G->getInitializer(),
838         Constant::getNullValue(RightRedZoneTy), NULL);
839
840     SmallString<2048> DescriptionOfGlobal = G->getName();
841     DescriptionOfGlobal += " (";
842     DescriptionOfGlobal += M.getModuleIdentifier();
843     DescriptionOfGlobal += ")";
844     GlobalVariable *Name = createPrivateGlobalForString(M, DescriptionOfGlobal);
845
846     // Create a new global variable with enough space for a redzone.
847     GlobalVariable *NewGlobal = new GlobalVariable(
848         M, NewTy, G->isConstant(), G->getLinkage(),
849         NewInitializer, "", G, G->getThreadLocalMode());
850     NewGlobal->copyAttributesFrom(G);
851     NewGlobal->setAlignment(RZ);
852
853     Value *Indices2[2];
854     Indices2[0] = IRB.getInt32(0);
855     Indices2[1] = IRB.getInt32(0);
856
857     G->replaceAllUsesWith(
858         ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true));
859     NewGlobal->takeName(G);
860     G->eraseFromParent();
861
862     Initializers[i] = ConstantStruct::get(
863         GlobalStructTy,
864         ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
865         ConstantInt::get(IntptrTy, SizeInBytes),
866         ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
867         ConstantExpr::getPointerCast(Name, IntptrTy),
868         ConstantInt::get(IntptrTy, GlobalHasDynamicInitializer),
869         NULL);
870
871     // Populate the first and last globals declared in this TU.
872     if (CheckInitOrder && GlobalHasDynamicInitializer) {
873       LastDynamic = ConstantExpr::getPointerCast(NewGlobal, IntptrTy);
874       if (FirstDynamic == 0)
875         FirstDynamic = LastDynamic;
876     }
877
878     DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
879   }
880
881   ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
882   GlobalVariable *AllGlobals = new GlobalVariable(
883       M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage,
884       ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
885
886   // Create calls for poisoning before initializers run and unpoisoning after.
887   if (CheckInitOrder && FirstDynamic && LastDynamic)
888     createInitializerPoisonCalls(M, FirstDynamic, LastDynamic);
889   IRB.CreateCall2(AsanRegisterGlobals,
890                   IRB.CreatePointerCast(AllGlobals, IntptrTy),
891                   ConstantInt::get(IntptrTy, n));
892
893   // We also need to unregister globals at the end, e.g. when a shared library
894   // gets closed.
895   Function *AsanDtorFunction = Function::Create(
896       FunctionType::get(Type::getVoidTy(*C), false),
897       GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
898   BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
899   IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
900   IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
901                        IRB.CreatePointerCast(AllGlobals, IntptrTy),
902                        ConstantInt::get(IntptrTy, n));
903   appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority);
904
905   DEBUG(dbgs() << M);
906   return true;
907 }
908
909 void AddressSanitizer::initializeCallbacks(Module &M) {
910   IRBuilder<> IRB(*C);
911   // Create __asan_report* callbacks.
912   for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
913     for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
914          AccessSizeIndex++) {
915       // IsWrite and TypeSize are encoded in the function name.
916       std::string FunctionName = std::string(kAsanReportErrorTemplate) +
917           (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex);
918       // If we are merging crash callbacks, they have two parameters.
919       AsanErrorCallback[AccessIsWrite][AccessSizeIndex] =
920           checkInterfaceFunction(M.getOrInsertFunction(
921               FunctionName, IRB.getVoidTy(), IntptrTy, NULL));
922     }
923   }
924
925   AsanHandleNoReturnFunc = checkInterfaceFunction(M.getOrInsertFunction(
926       kAsanHandleNoReturnName, IRB.getVoidTy(), NULL));
927   // We insert an empty inline asm after __asan_report* to avoid callback merge.
928   EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
929                             StringRef(""), StringRef(""),
930                             /*hasSideEffects=*/true);
931 }
932
933 // virtual
934 bool AddressSanitizer::doInitialization(Module &M) {
935   // Initialize the private fields. No one has accessed them before.
936   TD = getAnalysisIfAvailable<DataLayout>();
937
938   if (!TD)
939     return false;
940   BL.reset(new BlackList(BlacklistFile));
941   DynamicallyInitializedGlobals.Init(M);
942
943   C = &(M.getContext());
944   LongSize = TD->getPointerSizeInBits();
945   IntptrTy = Type::getIntNTy(*C, LongSize);
946
947   AsanCtorFunction = Function::Create(
948       FunctionType::get(Type::getVoidTy(*C), false),
949       GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
950   BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
951   // call __asan_init in the module ctor.
952   IRBuilder<> IRB(ReturnInst::Create(*C, AsanCtorBB));
953   AsanInitFunction = checkInterfaceFunction(
954       M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
955   AsanInitFunction->setLinkage(Function::ExternalLinkage);
956   IRB.CreateCall(AsanInitFunction);
957
958   llvm::Triple targetTriple(M.getTargetTriple());
959   bool isAndroid = targetTriple.getEnvironment() == llvm::Triple::Android;
960
961   MappingOffset = isAndroid ? kDefaultShadowOffsetAndroid :
962     (LongSize == 32 ? kDefaultShadowOffset32 : kDefaultShadowOffset64);
963   if (ClMappingOffsetLog >= 0) {
964     if (ClMappingOffsetLog == 0) {
965       // special case
966       MappingOffset = 0;
967     } else {
968       MappingOffset = 1ULL << ClMappingOffsetLog;
969     }
970   }
971
972
973   if (ClMappingOffsetLog >= 0) {
974     // Tell the run-time the current values of mapping offset and scale.
975     GlobalValue *asan_mapping_offset =
976         new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
977                        ConstantInt::get(IntptrTy, MappingOffset),
978                        kAsanMappingOffsetName);
979     // Read the global, otherwise it may be optimized away.
980     IRB.CreateLoad(asan_mapping_offset, true);
981   }
982   if (ClMappingScale) {
983     GlobalValue *asan_mapping_scale =
984         new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
985                            ConstantInt::get(IntptrTy, MappingScale()),
986                            kAsanMappingScaleName);
987     // Read the global, otherwise it may be optimized away.
988     IRB.CreateLoad(asan_mapping_scale, true);
989   }
990
991   appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
992
993   return true;
994 }
995
996 bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
997   // For each NSObject descendant having a +load method, this method is invoked
998   // by the ObjC runtime before any of the static constructors is called.
999   // Therefore we need to instrument such methods with a call to __asan_init
1000   // at the beginning in order to initialize our runtime before any access to
1001   // the shadow memory.
1002   // We cannot just ignore these methods, because they may call other
1003   // instrumented functions.
1004   if (F.getName().find(" load]") != std::string::npos) {
1005     IRBuilder<> IRB(F.begin()->begin());
1006     IRB.CreateCall(AsanInitFunction);
1007     return true;
1008   }
1009   return false;
1010 }
1011
1012 bool AddressSanitizer::runOnFunction(Function &F) {
1013   if (BL->isIn(F)) return false;
1014   if (&F == AsanCtorFunction) return false;
1015   DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
1016   initializeCallbacks(*F.getParent());
1017
1018   // If needed, insert __asan_init before checking for AddressSafety attr.
1019   maybeInsertAsanInitAtFunctionEntry(F);
1020
1021   if (!F.getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1022                                       Attribute::AddressSafety))
1023     return false;
1024
1025   if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
1026     return false;
1027
1028   // We want to instrument every address only once per basic block (unless there
1029   // are calls between uses).
1030   SmallSet<Value*, 16> TempsToInstrument;
1031   SmallVector<Instruction*, 16> ToInstrument;
1032   SmallVector<Instruction*, 8> NoReturnCalls;
1033   bool IsWrite;
1034
1035   // Fill the set of memory operations to instrument.
1036   for (Function::iterator FI = F.begin(), FE = F.end();
1037        FI != FE; ++FI) {
1038     TempsToInstrument.clear();
1039     int NumInsnsPerBB = 0;
1040     for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
1041          BI != BE; ++BI) {
1042       if (LooksLikeCodeInBug11395(BI)) return false;
1043       if (Value *Addr = isInterestingMemoryAccess(BI, &IsWrite)) {
1044         if (ClOpt && ClOptSameTemp) {
1045           if (!TempsToInstrument.insert(Addr))
1046             continue;  // We've seen this temp in the current BB.
1047         }
1048       } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) {
1049         // ok, take it.
1050       } else {
1051         if (CallInst *CI = dyn_cast<CallInst>(BI)) {
1052           // A call inside BB.
1053           TempsToInstrument.clear();
1054           if (CI->doesNotReturn()) {
1055             NoReturnCalls.push_back(CI);
1056           }
1057         }
1058         continue;
1059       }
1060       ToInstrument.push_back(BI);
1061       NumInsnsPerBB++;
1062       if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB)
1063         break;
1064     }
1065   }
1066
1067   // Instrument.
1068   int NumInstrumented = 0;
1069   for (size_t i = 0, n = ToInstrument.size(); i != n; i++) {
1070     Instruction *Inst = ToInstrument[i];
1071     if (ClDebugMin < 0 || ClDebugMax < 0 ||
1072         (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
1073       if (isInterestingMemoryAccess(Inst, &IsWrite))
1074         instrumentMop(Inst);
1075       else
1076         instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
1077     }
1078     NumInstrumented++;
1079   }
1080
1081   FunctionStackPoisoner FSP(F, *this);
1082   bool ChangedStack = FSP.runOnFunction();
1083
1084   // We must unpoison the stack before every NoReturn call (throw, _exit, etc).
1085   // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37
1086   for (size_t i = 0, n = NoReturnCalls.size(); i != n; i++) {
1087     Instruction *CI = NoReturnCalls[i];
1088     IRBuilder<> IRB(CI);
1089     IRB.CreateCall(AsanHandleNoReturnFunc);
1090   }
1091   DEBUG(dbgs() << "ASAN done instrumenting:\n" << F << "\n");
1092
1093   return NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
1094 }
1095
1096 static uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) {
1097   if (ShadowRedzoneSize == 1) return PoisonByte;
1098   if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte;
1099   if (ShadowRedzoneSize == 4)
1100     return (PoisonByte << 24) + (PoisonByte << 16) +
1101         (PoisonByte << 8) + (PoisonByte);
1102   llvm_unreachable("ShadowRedzoneSize is either 1, 2 or 4");
1103 }
1104
1105 static void PoisonShadowPartialRightRedzone(uint8_t *Shadow,
1106                                             size_t Size,
1107                                             size_t RZSize,
1108                                             size_t ShadowGranularity,
1109                                             uint8_t Magic) {
1110   for (size_t i = 0; i < RZSize;
1111        i+= ShadowGranularity, Shadow++) {
1112     if (i + ShadowGranularity <= Size) {
1113       *Shadow = 0;  // fully addressable
1114     } else if (i >= Size) {
1115       *Shadow = Magic;  // unaddressable
1116     } else {
1117       *Shadow = Size - i;  // first Size-i bytes are addressable
1118     }
1119   }
1120 }
1121
1122 // Workaround for bug 11395: we don't want to instrument stack in functions
1123 // with large assembly blobs (32-bit only), otherwise reg alloc may crash.
1124 // FIXME: remove once the bug 11395 is fixed.
1125 bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
1126   if (LongSize != 32) return false;
1127   CallInst *CI = dyn_cast<CallInst>(I);
1128   if (!CI || !CI->isInlineAsm()) return false;
1129   if (CI->getNumArgOperands() <= 5) return false;
1130   // We have inline assembly with quite a few arguments.
1131   return true;
1132 }
1133
1134 void FunctionStackPoisoner::initializeCallbacks(Module &M) {
1135   IRBuilder<> IRB(*C);
1136   AsanStackMallocFunc = checkInterfaceFunction(M.getOrInsertFunction(
1137       kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL));
1138   AsanStackFreeFunc = checkInterfaceFunction(M.getOrInsertFunction(
1139       kAsanStackFreeName, IRB.getVoidTy(),
1140       IntptrTy, IntptrTy, IntptrTy, NULL));
1141   AsanPoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction(
1142       kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1143   AsanUnpoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction(
1144       kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1145 }
1146
1147 void FunctionStackPoisoner::poisonRedZones(
1148   const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB, Value *ShadowBase,
1149   bool DoPoison) {
1150   size_t ShadowRZSize = RedzoneSize() >> MappingScale();
1151   assert(ShadowRZSize >= 1 && ShadowRZSize <= 4);
1152   Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8);
1153   Type *RZPtrTy = PointerType::get(RZTy, 0);
1154
1155   Value *PoisonLeft  = ConstantInt::get(RZTy,
1156     ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize));
1157   Value *PoisonMid   = ConstantInt::get(RZTy,
1158     ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize));
1159   Value *PoisonRight = ConstantInt::get(RZTy,
1160     ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize));
1161
1162   // poison the first red zone.
1163   IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy));
1164
1165   // poison all other red zones.
1166   uint64_t Pos = RedzoneSize();
1167   for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
1168     AllocaInst *AI = AllocaVec[i];
1169     uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
1170     uint64_t AlignedSize = getAlignedAllocaSize(AI);
1171     assert(AlignedSize - SizeInBytes < RedzoneSize());
1172     Value *Ptr = NULL;
1173
1174     Pos += AlignedSize;
1175
1176     assert(ShadowBase->getType() == IntptrTy);
1177     if (SizeInBytes < AlignedSize) {
1178       // Poison the partial redzone at right
1179       Ptr = IRB.CreateAdd(
1180           ShadowBase, ConstantInt::get(IntptrTy,
1181                                        (Pos >> MappingScale()) - ShadowRZSize));
1182       size_t AddressableBytes = RedzoneSize() - (AlignedSize - SizeInBytes);
1183       uint32_t Poison = 0;
1184       if (DoPoison) {
1185         PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes,
1186                                         RedzoneSize(),
1187                                         1ULL << MappingScale(),
1188                                         kAsanStackPartialRedzoneMagic);
1189       }
1190       Value *PartialPoison = ConstantInt::get(RZTy, Poison);
1191       IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
1192     }
1193
1194     // Poison the full redzone at right.
1195     Ptr = IRB.CreateAdd(ShadowBase,
1196                         ConstantInt::get(IntptrTy, Pos >> MappingScale()));
1197     bool LastAlloca = (i == AllocaVec.size() - 1);
1198     Value *Poison = LastAlloca ? PoisonRight : PoisonMid;
1199     IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
1200
1201     Pos += RedzoneSize();
1202   }
1203 }
1204
1205 void FunctionStackPoisoner::poisonStack() {
1206   uint64_t LocalStackSize = TotalStackSize +
1207                             (AllocaVec.size() + 1) * RedzoneSize();
1208
1209   bool DoStackMalloc = ASan.CheckUseAfterReturn
1210       && LocalStackSize <= kMaxStackMallocSize;
1211
1212   assert(AllocaVec.size() > 0);
1213   Instruction *InsBefore = AllocaVec[0];
1214   IRBuilder<> IRB(InsBefore);
1215
1216
1217   Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
1218   AllocaInst *MyAlloca =
1219       new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
1220   if (ClRealignStack && StackAlignment < RedzoneSize())
1221     StackAlignment = RedzoneSize();
1222   MyAlloca->setAlignment(StackAlignment);
1223   assert(MyAlloca->isStaticAlloca());
1224   Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
1225   Value *LocalStackBase = OrigStackBase;
1226
1227   if (DoStackMalloc) {
1228     LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc,
1229         ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
1230   }
1231
1232   // This string will be parsed by the run-time (DescribeStackAddress).
1233   SmallString<2048> StackDescriptionStorage;
1234   raw_svector_ostream StackDescription(StackDescriptionStorage);
1235   StackDescription << F.getName() << " " << AllocaVec.size() << " ";
1236
1237   // Insert poison calls for lifetime intrinsics for alloca.
1238   bool HavePoisonedAllocas = false;
1239   for (size_t i = 0, n = AllocaPoisonCallVec.size(); i < n; i++) {
1240     const AllocaPoisonCall &APC = AllocaPoisonCallVec[i];
1241     IntrinsicInst *II = APC.InsBefore;
1242     AllocaInst *AI = findAllocaForValue(II->getArgOperand(1));
1243     assert(AI);
1244     IRBuilder<> IRB(II);
1245     poisonAlloca(AI, APC.Size, IRB, APC.DoPoison);
1246     HavePoisonedAllocas |= APC.DoPoison;
1247   }
1248
1249   uint64_t Pos = RedzoneSize();
1250   // Replace Alloca instructions with base+offset.
1251   for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
1252     AllocaInst *AI = AllocaVec[i];
1253     uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
1254     StringRef Name = AI->getName();
1255     StackDescription << Pos << " " << SizeInBytes << " "
1256                      << Name.size() << " " << Name << " ";
1257     uint64_t AlignedSize = getAlignedAllocaSize(AI);
1258     assert((AlignedSize % RedzoneSize()) == 0);
1259     Value *NewAllocaPtr = IRB.CreateIntToPtr(
1260             IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)),
1261             AI->getType());
1262     replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB);
1263     AI->replaceAllUsesWith(NewAllocaPtr);
1264     Pos += AlignedSize + RedzoneSize();
1265   }
1266   assert(Pos == LocalStackSize);
1267
1268   // Write the Magic value and the frame description constant to the redzone.
1269   Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
1270   IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
1271                   BasePlus0);
1272   Value *BasePlus1 = IRB.CreateAdd(LocalStackBase,
1273                                    ConstantInt::get(IntptrTy,
1274                                                     ASan.LongSize/8));
1275   BasePlus1 = IRB.CreateIntToPtr(BasePlus1, IntptrPtrTy);
1276   GlobalVariable *StackDescriptionGlobal =
1277       createPrivateGlobalForString(*F.getParent(), StackDescription.str());
1278   Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal,
1279                                              IntptrTy);
1280   IRB.CreateStore(Description, BasePlus1);
1281
1282   // Poison the stack redzones at the entry.
1283   Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB);
1284   poisonRedZones(AllocaVec, IRB, ShadowBase, true);
1285
1286   // Unpoison the stack before all ret instructions.
1287   for (size_t i = 0, n = RetVec.size(); i < n; i++) {
1288     Instruction *Ret = RetVec[i];
1289     IRBuilder<> IRBRet(Ret);
1290     // Mark the current frame as retired.
1291     IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
1292                        BasePlus0);
1293     // Unpoison the stack.
1294     poisonRedZones(AllocaVec, IRBRet, ShadowBase, false);
1295     if (DoStackMalloc) {
1296       // In use-after-return mode, mark the whole stack frame unaddressable.
1297       IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase,
1298                          ConstantInt::get(IntptrTy, LocalStackSize),
1299                          OrigStackBase);
1300     } else if (HavePoisonedAllocas) {
1301       // If we poisoned some allocas in llvm.lifetime analysis,
1302       // unpoison whole stack frame now.
1303       assert(LocalStackBase == OrigStackBase);
1304       poisonAlloca(LocalStackBase, LocalStackSize, IRBRet, false);
1305     }
1306   }
1307
1308   // We are done. Remove the old unused alloca instructions.
1309   for (size_t i = 0, n = AllocaVec.size(); i < n; i++)
1310     AllocaVec[i]->eraseFromParent();
1311 }
1312
1313 void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size,
1314                                          IRBuilder<> IRB, bool DoPoison) {
1315   // For now just insert the call to ASan runtime.
1316   Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy);
1317   Value *SizeArg = ConstantInt::get(IntptrTy, Size);
1318   IRB.CreateCall2(DoPoison ? AsanPoisonStackMemoryFunc
1319                            : AsanUnpoisonStackMemoryFunc,
1320                   AddrArg, SizeArg);
1321 }
1322
1323 // Handling llvm.lifetime intrinsics for a given %alloca:
1324 // (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca.
1325 // (2) if %size is constant, poison memory for llvm.lifetime.end (to detect
1326 //     invalid accesses) and unpoison it for llvm.lifetime.start (the memory
1327 //     could be poisoned by previous llvm.lifetime.end instruction, as the
1328 //     variable may go in and out of scope several times, e.g. in loops).
1329 // (3) if we poisoned at least one %alloca in a function,
1330 //     unpoison the whole stack frame at function exit.
1331
1332 AllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) {
1333   if (AllocaInst *AI = dyn_cast<AllocaInst>(V))
1334     // We're intested only in allocas we can handle.
1335     return isInterestingAlloca(*AI) ? AI : 0;
1336   // See if we've already calculated (or started to calculate) alloca for a
1337   // given value.
1338   AllocaForValueMapTy::iterator I = AllocaForValue.find(V);
1339   if (I != AllocaForValue.end())
1340     return I->second;
1341   // Store 0 while we're calculating alloca for value V to avoid
1342   // infinite recursion if the value references itself.
1343   AllocaForValue[V] = 0;
1344   AllocaInst *Res = 0;
1345   if (CastInst *CI = dyn_cast<CastInst>(V))
1346     Res = findAllocaForValue(CI->getOperand(0));
1347   else if (PHINode *PN = dyn_cast<PHINode>(V)) {
1348     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1349       Value *IncValue = PN->getIncomingValue(i);
1350       // Allow self-referencing phi-nodes.
1351       if (IncValue == PN) continue;
1352       AllocaInst *IncValueAI = findAllocaForValue(IncValue);
1353       // AI for incoming values should exist and should all be equal.
1354       if (IncValueAI == 0 || (Res != 0 && IncValueAI != Res))
1355         return 0;
1356       Res = IncValueAI;
1357     }
1358   }
1359   if (Res != 0)
1360     AllocaForValue[V] = Res;
1361   return Res;
1362 }