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 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/DepthFirstIterator.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/Analysis/MemoryBuiltins.h"
26 #include "llvm/Analysis/TargetLibraryInfo.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/IR/CallSite.h"
29 #include "llvm/IR/DIBuilder.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/Dominators.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/IRBuilder.h"
34 #include "llvm/IR/InlineAsm.h"
35 #include "llvm/IR/InstVisitor.h"
36 #include "llvm/IR/IntrinsicInst.h"
37 #include "llvm/IR/LLVMContext.h"
38 #include "llvm/IR/MDBuilder.h"
39 #include "llvm/IR/Module.h"
40 #include "llvm/IR/Type.h"
41 #include "llvm/MC/MCSectionMachO.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/DataTypes.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/Endian.h"
46 #include "llvm/Support/SwapByteOrder.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include "llvm/Transforms/Instrumentation.h"
49 #include "llvm/Transforms/Scalar.h"
50 #include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
51 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
52 #include "llvm/Transforms/Utils/Cloning.h"
53 #include "llvm/Transforms/Utils/Local.h"
54 #include "llvm/Transforms/Utils/ModuleUtils.h"
55 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
56 #include <algorithm>
57 #include <string>
58 #include <system_error>
59 
60 using namespace llvm;
61 
62 #define DEBUG_TYPE "asan"
63 
64 static const uint64_t kDefaultShadowScale = 3;
65 static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
66 static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
67 static const uint64_t kIOSShadowOffset32 = 1ULL << 30;
68 static const uint64_t kIOSShadowOffset64 = 0x120200000;
69 static const uint64_t kIOSSimShadowOffset32 = 1ULL << 30;
70 static const uint64_t kIOSSimShadowOffset64 = kDefaultShadowOffset64;
71 static const uint64_t kSmallX86_64ShadowOffset = 0x7FFF8000;  // < 2G.
72 static const uint64_t kLinuxKasan_ShadowOffset64 = 0xdffffc0000000000;
73 static const uint64_t kPPC64_ShadowOffset64 = 1ULL << 41;
74 static const uint64_t kSystemZ_ShadowOffset64 = 1ULL << 52;
75 static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa0000;
76 static const uint64_t kMIPS64_ShadowOffset64 = 1ULL << 37;
77 static const uint64_t kAArch64_ShadowOffset64 = 1ULL << 36;
78 static const uint64_t kFreeBSD_ShadowOffset32 = 1ULL << 30;
79 static const uint64_t kFreeBSD_ShadowOffset64 = 1ULL << 46;
80 static const uint64_t kWindowsShadowOffset32 = 3ULL << 28;
81 
82 static const size_t kMinStackMallocSize = 1 << 6;   // 64B
83 static const size_t kMaxStackMallocSize = 1 << 16;  // 64K
84 static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
85 static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
86 
87 static const char *const kAsanModuleCtorName = "asan.module_ctor";
88 static const char *const kAsanModuleDtorName = "asan.module_dtor";
89 static const uint64_t kAsanCtorAndDtorPriority = 1;
90 static const char *const kAsanReportErrorTemplate = "__asan_report_";
91 static const char *const kAsanRegisterGlobalsName = "__asan_register_globals";
92 static const char *const kAsanUnregisterGlobalsName =
93     "__asan_unregister_globals";
94 static const char *const kAsanRegisterImageGlobalsName =
95   "__asan_register_image_globals";
96 static const char *const kAsanUnregisterImageGlobalsName =
97   "__asan_unregister_image_globals";
98 static const char *const kAsanPoisonGlobalsName = "__asan_before_dynamic_init";
99 static const char *const kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init";
100 static const char *const kAsanInitName = "__asan_init";
101 static const char *const kAsanVersionCheckName =
102     "__asan_version_mismatch_check_v8";
103 static const char *const kAsanPtrCmp = "__sanitizer_ptr_cmp";
104 static const char *const kAsanPtrSub = "__sanitizer_ptr_sub";
105 static const char *const kAsanHandleNoReturnName = "__asan_handle_no_return";
106 static const int kMaxAsanStackMallocSizeClass = 10;
107 static const char *const kAsanStackMallocNameTemplate = "__asan_stack_malloc_";
108 static const char *const kAsanStackFreeNameTemplate = "__asan_stack_free_";
109 static const char *const kAsanGenPrefix = "__asan_gen_";
110 static const char *const kODRGenPrefix = "__odr_asan_gen_";
111 static const char *const kSanCovGenPrefix = "__sancov_gen_";
112 static const char *const kAsanPoisonStackMemoryName =
113     "__asan_poison_stack_memory";
114 static const char *const kAsanUnpoisonStackMemoryName =
115     "__asan_unpoison_stack_memory";
116 static const char *const kAsanGlobalsRegisteredFlagName =
117     "__asan_globals_registered";
118 
119 static const char *const kAsanOptionDetectUAR =
120     "__asan_option_detect_stack_use_after_return";
121 
122 static const char *const kAsanAllocaPoison = "__asan_alloca_poison";
123 static const char *const kAsanAllocasUnpoison = "__asan_allocas_unpoison";
124 
125 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
126 static const size_t kNumberOfAccessSizes = 5;
127 
128 static const unsigned kAllocaRzSize = 32;
129 
130 // Command-line flags.
131 static cl::opt<bool> ClEnableKasan(
132     "asan-kernel", cl::desc("Enable KernelAddressSanitizer instrumentation"),
133     cl::Hidden, cl::init(false));
134 static cl::opt<bool> ClRecover(
135     "asan-recover",
136     cl::desc("Enable recovery mode (continue-after-error)."),
137     cl::Hidden, cl::init(false));
138 
139 // This flag may need to be replaced with -f[no-]asan-reads.
140 static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
141                                        cl::desc("instrument read instructions"),
142                                        cl::Hidden, cl::init(true));
143 static cl::opt<bool> ClInstrumentWrites(
144     "asan-instrument-writes", cl::desc("instrument write instructions"),
145     cl::Hidden, cl::init(true));
146 static cl::opt<bool> ClInstrumentAtomics(
147     "asan-instrument-atomics",
148     cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden,
149     cl::init(true));
150 static cl::opt<bool> ClAlwaysSlowPath(
151     "asan-always-slow-path",
152     cl::desc("use instrumentation with slow path for all accesses"), cl::Hidden,
153     cl::init(false));
154 // This flag limits the number of instructions to be instrumented
155 // in any given BB. Normally, this should be set to unlimited (INT_MAX),
156 // but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
157 // set it to 10000.
158 static cl::opt<int> ClMaxInsnsToInstrumentPerBB(
159     "asan-max-ins-per-bb", cl::init(10000),
160     cl::desc("maximal number of instructions to instrument in any given BB"),
161     cl::Hidden);
162 // This flag may need to be replaced with -f[no]asan-stack.
163 static cl::opt<bool> ClStack("asan-stack", cl::desc("Handle stack memory"),
164                              cl::Hidden, cl::init(true));
165 static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
166                                       cl::desc("Check stack-use-after-return"),
167                                       cl::Hidden, cl::init(true));
168 static cl::opt<bool> ClUseAfterScope("asan-use-after-scope",
169                                      cl::desc("Check stack-use-after-scope"),
170                                      cl::Hidden, cl::init(false));
171 // This flag may need to be replaced with -f[no]asan-globals.
172 static cl::opt<bool> ClGlobals("asan-globals",
173                                cl::desc("Handle global objects"), cl::Hidden,
174                                cl::init(true));
175 static cl::opt<bool> ClInitializers("asan-initialization-order",
176                                     cl::desc("Handle C++ initializer order"),
177                                     cl::Hidden, cl::init(true));
178 static cl::opt<bool> ClInvalidPointerPairs(
179     "asan-detect-invalid-pointer-pair",
180     cl::desc("Instrument <, <=, >, >=, - with pointer operands"), cl::Hidden,
181     cl::init(false));
182 static cl::opt<unsigned> ClRealignStack(
183     "asan-realign-stack",
184     cl::desc("Realign stack to the value of this flag (power of two)"),
185     cl::Hidden, cl::init(32));
186 static cl::opt<int> ClInstrumentationWithCallsThreshold(
187     "asan-instrumentation-with-call-threshold",
188     cl::desc(
189         "If the function being instrumented contains more than "
190         "this number of memory accesses, use callbacks instead of "
191         "inline checks (-1 means never use callbacks)."),
192     cl::Hidden, cl::init(7000));
193 static cl::opt<std::string> ClMemoryAccessCallbackPrefix(
194     "asan-memory-access-callback-prefix",
195     cl::desc("Prefix for memory access callbacks"), cl::Hidden,
196     cl::init("__asan_"));
197 static cl::opt<bool> ClInstrumentAllocas("asan-instrument-allocas",
198                                          cl::desc("instrument dynamic allocas"),
199                                          cl::Hidden, cl::init(true));
200 static cl::opt<bool> ClSkipPromotableAllocas(
201     "asan-skip-promotable-allocas",
202     cl::desc("Do not instrument promotable allocas"), cl::Hidden,
203     cl::init(true));
204 
205 // These flags allow to change the shadow mapping.
206 // The shadow mapping looks like
207 //    Shadow = (Mem >> scale) + offset
208 static cl::opt<int> ClMappingScale("asan-mapping-scale",
209                                    cl::desc("scale of asan shadow mapping"),
210                                    cl::Hidden, cl::init(0));
211 static cl::opt<unsigned long long> ClMappingOffset(
212     "asan-mapping-offset",
213     cl::desc("offset of asan shadow mapping [EXPERIMENTAL]"), cl::Hidden,
214     cl::init(0));
215 
216 // Optimization flags. Not user visible, used mostly for testing
217 // and benchmarking the tool.
218 static cl::opt<bool> ClOpt("asan-opt", cl::desc("Optimize instrumentation"),
219                            cl::Hidden, cl::init(true));
220 static cl::opt<bool> ClOptSameTemp(
221     "asan-opt-same-temp", cl::desc("Instrument the same temp just once"),
222     cl::Hidden, cl::init(true));
223 static cl::opt<bool> ClOptGlobals("asan-opt-globals",
224                                   cl::desc("Don't instrument scalar globals"),
225                                   cl::Hidden, cl::init(true));
226 static cl::opt<bool> ClOptStack(
227     "asan-opt-stack", cl::desc("Don't instrument scalar stack variables"),
228     cl::Hidden, cl::init(false));
229 
230 static cl::opt<bool> ClDynamicAllocaStack(
231     "asan-stack-dynamic-alloca",
232     cl::desc("Use dynamic alloca to represent stack variables"), cl::Hidden,
233     cl::init(true));
234 
235 static cl::opt<uint32_t> ClForceExperiment(
236     "asan-force-experiment",
237     cl::desc("Force optimization experiment (for testing)"), cl::Hidden,
238     cl::init(0));
239 
240 static cl::opt<bool>
241     ClUsePrivateAliasForGlobals("asan-use-private-alias",
242                                 cl::desc("Use private aliases for global"
243                                          " variables"),
244                                 cl::Hidden, cl::init(false));
245 
246 // Debug flags.
247 static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
248                             cl::init(0));
249 static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
250                                  cl::Hidden, cl::init(0));
251 static cl::opt<std::string> ClDebugFunc("asan-debug-func", cl::Hidden,
252                                         cl::desc("Debug func"));
253 static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
254                                cl::Hidden, cl::init(-1));
255 static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
256                                cl::Hidden, cl::init(-1));
257 
258 STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
259 STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
260 STATISTIC(NumOptimizedAccessesToGlobalVar,
261           "Number of optimized accesses to global vars");
262 STATISTIC(NumOptimizedAccessesToStackVar,
263           "Number of optimized accesses to stack vars");
264 
265 namespace {
266 /// Frontend-provided metadata for source location.
267 struct LocationMetadata {
268   StringRef Filename;
269   int LineNo;
270   int ColumnNo;
271 
272   LocationMetadata() : Filename(), LineNo(0), ColumnNo(0) {}
273 
274   bool empty() const { return Filename.empty(); }
275 
276   void parse(MDNode *MDN) {
277     assert(MDN->getNumOperands() == 3);
278     MDString *DIFilename = cast<MDString>(MDN->getOperand(0));
279     Filename = DIFilename->getString();
280     LineNo =
281         mdconst::extract<ConstantInt>(MDN->getOperand(1))->getLimitedValue();
282     ColumnNo =
283         mdconst::extract<ConstantInt>(MDN->getOperand(2))->getLimitedValue();
284   }
285 };
286 
287 /// Frontend-provided metadata for global variables.
288 class GlobalsMetadata {
289  public:
290   struct Entry {
291     Entry() : SourceLoc(), Name(), IsDynInit(false), IsBlacklisted(false) {}
292     LocationMetadata SourceLoc;
293     StringRef Name;
294     bool IsDynInit;
295     bool IsBlacklisted;
296   };
297 
298   GlobalsMetadata() : inited_(false) {}
299 
300   void reset() {
301     inited_ = false;
302     Entries.clear();
303   }
304 
305   void init(Module &M) {
306     assert(!inited_);
307     inited_ = true;
308     NamedMDNode *Globals = M.getNamedMetadata("llvm.asan.globals");
309     if (!Globals) return;
310     for (auto MDN : Globals->operands()) {
311       // Metadata node contains the global and the fields of "Entry".
312       assert(MDN->getNumOperands() == 5);
313       auto *GV = mdconst::extract_or_null<GlobalVariable>(MDN->getOperand(0));
314       // The optimizer may optimize away a global entirely.
315       if (!GV) continue;
316       // We can already have an entry for GV if it was merged with another
317       // global.
318       Entry &E = Entries[GV];
319       if (auto *Loc = cast_or_null<MDNode>(MDN->getOperand(1)))
320         E.SourceLoc.parse(Loc);
321       if (auto *Name = cast_or_null<MDString>(MDN->getOperand(2)))
322         E.Name = Name->getString();
323       ConstantInt *IsDynInit =
324           mdconst::extract<ConstantInt>(MDN->getOperand(3));
325       E.IsDynInit |= IsDynInit->isOne();
326       ConstantInt *IsBlacklisted =
327           mdconst::extract<ConstantInt>(MDN->getOperand(4));
328       E.IsBlacklisted |= IsBlacklisted->isOne();
329     }
330   }
331 
332   /// Returns metadata entry for a given global.
333   Entry get(GlobalVariable *G) const {
334     auto Pos = Entries.find(G);
335     return (Pos != Entries.end()) ? Pos->second : Entry();
336   }
337 
338  private:
339   bool inited_;
340   DenseMap<GlobalVariable *, Entry> Entries;
341 };
342 
343 /// This struct defines the shadow mapping using the rule:
344 ///   shadow = (mem >> Scale) ADD-or-OR Offset.
345 struct ShadowMapping {
346   int Scale;
347   uint64_t Offset;
348   bool OrShadowOffset;
349 };
350 
351 static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize,
352                                       bool IsKasan) {
353   bool IsAndroid = TargetTriple.isAndroid();
354   bool IsIOS = TargetTriple.isiOS() || TargetTriple.isWatchOS();
355   bool IsFreeBSD = TargetTriple.isOSFreeBSD();
356   bool IsLinux = TargetTriple.isOSLinux();
357   bool IsPPC64 = TargetTriple.getArch() == llvm::Triple::ppc64 ||
358                  TargetTriple.getArch() == llvm::Triple::ppc64le;
359   bool IsSystemZ = TargetTriple.getArch() == llvm::Triple::systemz;
360   bool IsX86 = TargetTriple.getArch() == llvm::Triple::x86;
361   bool IsX86_64 = TargetTriple.getArch() == llvm::Triple::x86_64;
362   bool IsMIPS32 = TargetTriple.getArch() == llvm::Triple::mips ||
363                   TargetTriple.getArch() == llvm::Triple::mipsel;
364   bool IsMIPS64 = TargetTriple.getArch() == llvm::Triple::mips64 ||
365                   TargetTriple.getArch() == llvm::Triple::mips64el;
366   bool IsAArch64 = TargetTriple.getArch() == llvm::Triple::aarch64;
367   bool IsWindows = TargetTriple.isOSWindows();
368 
369   ShadowMapping Mapping;
370 
371   if (LongSize == 32) {
372     // Android is always PIE, which means that the beginning of the address
373     // space is always available.
374     if (IsAndroid)
375       Mapping.Offset = 0;
376     else if (IsMIPS32)
377       Mapping.Offset = kMIPS32_ShadowOffset32;
378     else if (IsFreeBSD)
379       Mapping.Offset = kFreeBSD_ShadowOffset32;
380     else if (IsIOS)
381       // If we're targeting iOS and x86, the binary is built for iOS simulator.
382       Mapping.Offset = IsX86 ? kIOSSimShadowOffset32 : kIOSShadowOffset32;
383     else if (IsWindows)
384       Mapping.Offset = kWindowsShadowOffset32;
385     else
386       Mapping.Offset = kDefaultShadowOffset32;
387   } else {  // LongSize == 64
388     if (IsPPC64)
389       Mapping.Offset = kPPC64_ShadowOffset64;
390     else if (IsSystemZ)
391       Mapping.Offset = kSystemZ_ShadowOffset64;
392     else if (IsFreeBSD)
393       Mapping.Offset = kFreeBSD_ShadowOffset64;
394     else if (IsLinux && IsX86_64) {
395       if (IsKasan)
396         Mapping.Offset = kLinuxKasan_ShadowOffset64;
397       else
398         Mapping.Offset = kSmallX86_64ShadowOffset;
399     } else if (IsMIPS64)
400       Mapping.Offset = kMIPS64_ShadowOffset64;
401     else if (IsIOS)
402       // If we're targeting iOS and x86, the binary is built for iOS simulator.
403       Mapping.Offset = IsX86_64 ? kIOSSimShadowOffset64 : kIOSShadowOffset64;
404     else if (IsAArch64)
405       Mapping.Offset = kAArch64_ShadowOffset64;
406     else
407       Mapping.Offset = kDefaultShadowOffset64;
408   }
409 
410   Mapping.Scale = kDefaultShadowScale;
411   if (ClMappingScale.getNumOccurrences() > 0) {
412     Mapping.Scale = ClMappingScale;
413   }
414 
415   if (ClMappingOffset.getNumOccurrences() > 0) {
416     Mapping.Offset = ClMappingOffset;
417   }
418 
419   // OR-ing shadow offset if more efficient (at least on x86) if the offset
420   // is a power of two, but on ppc64 we have to use add since the shadow
421   // offset is not necessary 1/8-th of the address space.  On SystemZ,
422   // we could OR the constant in a single instruction, but it's more
423   // efficient to load it once and use indexed addressing.
424   Mapping.OrShadowOffset = !IsAArch64 && !IsPPC64 && !IsSystemZ
425                            && !(Mapping.Offset & (Mapping.Offset - 1));
426 
427   return Mapping;
428 }
429 
430 static size_t RedzoneSizeForScale(int MappingScale) {
431   // Redzone used for stack and globals is at least 32 bytes.
432   // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
433   return std::max(32U, 1U << MappingScale);
434 }
435 
436 /// AddressSanitizer: instrument the code in module to find memory bugs.
437 struct AddressSanitizer : public FunctionPass {
438   explicit AddressSanitizer(bool CompileKernel = false, bool Recover = false,
439                             bool UseAfterScope = false)
440       : FunctionPass(ID), CompileKernel(CompileKernel || ClEnableKasan),
441         Recover(Recover || ClRecover),
442         UseAfterScope(UseAfterScope || ClUseAfterScope) {
443     initializeAddressSanitizerPass(*PassRegistry::getPassRegistry());
444   }
445   const char *getPassName() const override {
446     return "AddressSanitizerFunctionPass";
447   }
448   void getAnalysisUsage(AnalysisUsage &AU) const override {
449     AU.addRequired<DominatorTreeWrapperPass>();
450     AU.addRequired<TargetLibraryInfoWrapperPass>();
451   }
452   uint64_t getAllocaSizeInBytes(AllocaInst *AI) const {
453     Type *Ty = AI->getAllocatedType();
454     uint64_t SizeInBytes =
455         AI->getModule()->getDataLayout().getTypeAllocSize(Ty);
456     return SizeInBytes;
457   }
458   /// Check if we want (and can) handle this alloca.
459   bool isInterestingAlloca(AllocaInst &AI);
460 
461   // Check if we have dynamic alloca.
462   bool isDynamicAlloca(AllocaInst &AI) const {
463     return AI.isArrayAllocation() || !AI.isStaticAlloca();
464   }
465 
466   /// If it is an interesting memory access, return the PointerOperand
467   /// and set IsWrite/Alignment. Otherwise return nullptr.
468   Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite,
469                                    uint64_t *TypeSize, unsigned *Alignment);
470   void instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis, Instruction *I,
471                      bool UseCalls, const DataLayout &DL);
472   void instrumentPointerComparisonOrSubtraction(Instruction *I);
473   void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore,
474                          Value *Addr, uint32_t TypeSize, bool IsWrite,
475                          Value *SizeArgument, bool UseCalls, uint32_t Exp);
476   void instrumentUnusualSizeOrAlignment(Instruction *I, Value *Addr,
477                                         uint32_t TypeSize, bool IsWrite,
478                                         Value *SizeArgument, bool UseCalls,
479                                         uint32_t Exp);
480   Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
481                            Value *ShadowValue, uint32_t TypeSize);
482   Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr,
483                                  bool IsWrite, size_t AccessSizeIndex,
484                                  Value *SizeArgument, uint32_t Exp);
485   void instrumentMemIntrinsic(MemIntrinsic *MI);
486   Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
487   bool runOnFunction(Function &F) override;
488   bool maybeInsertAsanInitAtFunctionEntry(Function &F);
489   void markEscapedLocalAllocas(Function &F);
490   bool doInitialization(Module &M) override;
491   bool doFinalization(Module &M) override;
492   static char ID;  // Pass identification, replacement for typeid
493 
494   DominatorTree &getDominatorTree() const { return *DT; }
495 
496  private:
497   void initializeCallbacks(Module &M);
498 
499   bool LooksLikeCodeInBug11395(Instruction *I);
500   bool GlobalIsLinkerInitialized(GlobalVariable *G);
501   bool isSafeAccess(ObjectSizeOffsetVisitor &ObjSizeVis, Value *Addr,
502                     uint64_t TypeSize) const;
503 
504   /// Helper to cleanup per-function state.
505   struct FunctionStateRAII {
506     AddressSanitizer *Pass;
507     FunctionStateRAII(AddressSanitizer *Pass) : Pass(Pass) {
508       assert(Pass->ProcessedAllocas.empty() &&
509              "last pass forgot to clear cache");
510     }
511     ~FunctionStateRAII() { Pass->ProcessedAllocas.clear(); }
512   };
513 
514   LLVMContext *C;
515   Triple TargetTriple;
516   int LongSize;
517   bool CompileKernel;
518   bool Recover;
519   bool UseAfterScope;
520   Type *IntptrTy;
521   ShadowMapping Mapping;
522   DominatorTree *DT;
523   Function *AsanCtorFunction = nullptr;
524   Function *AsanInitFunction = nullptr;
525   Function *AsanHandleNoReturnFunc;
526   Function *AsanPtrCmpFunction, *AsanPtrSubFunction;
527   // This array is indexed by AccessIsWrite, Experiment and log2(AccessSize).
528   Function *AsanErrorCallback[2][2][kNumberOfAccessSizes];
529   Function *AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes];
530   // This array is indexed by AccessIsWrite and Experiment.
531   Function *AsanErrorCallbackSized[2][2];
532   Function *AsanMemoryAccessCallbackSized[2][2];
533   Function *AsanMemmove, *AsanMemcpy, *AsanMemset;
534   InlineAsm *EmptyAsm;
535   GlobalsMetadata GlobalsMD;
536   DenseMap<AllocaInst *, bool> ProcessedAllocas;
537 
538   friend struct FunctionStackPoisoner;
539 };
540 
541 class AddressSanitizerModule : public ModulePass {
542  public:
543   explicit AddressSanitizerModule(bool CompileKernel = false,
544                                   bool Recover = false)
545       : ModulePass(ID), CompileKernel(CompileKernel || ClEnableKasan),
546         Recover(Recover || ClRecover) {}
547   bool runOnModule(Module &M) override;
548   static char ID;  // Pass identification, replacement for typeid
549   const char *getPassName() const override { return "AddressSanitizerModule"; }
550 
551  private:
552   void initializeCallbacks(Module &M);
553 
554   bool InstrumentGlobals(IRBuilder<> &IRB, Module &M);
555   bool ShouldInstrumentGlobal(GlobalVariable *G);
556   bool ShouldUseMachOGlobalsSection() const;
557   void poisonOneInitializer(Function &GlobalInit, GlobalValue *ModuleName);
558   void createInitializerPoisonCalls(Module &M, GlobalValue *ModuleName);
559   size_t MinRedzoneSizeForGlobal() const {
560     return RedzoneSizeForScale(Mapping.Scale);
561   }
562 
563   GlobalsMetadata GlobalsMD;
564   bool CompileKernel;
565   bool Recover;
566   Type *IntptrTy;
567   LLVMContext *C;
568   Triple TargetTriple;
569   ShadowMapping Mapping;
570   Function *AsanPoisonGlobals;
571   Function *AsanUnpoisonGlobals;
572   Function *AsanRegisterGlobals;
573   Function *AsanUnregisterGlobals;
574   Function *AsanRegisterImageGlobals;
575   Function *AsanUnregisterImageGlobals;
576 };
577 
578 // Stack poisoning does not play well with exception handling.
579 // When an exception is thrown, we essentially bypass the code
580 // that unpoisones the stack. This is why the run-time library has
581 // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
582 // stack in the interceptor. This however does not work inside the
583 // actual function which catches the exception. Most likely because the
584 // compiler hoists the load of the shadow value somewhere too high.
585 // This causes asan to report a non-existing bug on 453.povray.
586 // It sounds like an LLVM bug.
587 struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
588   Function &F;
589   AddressSanitizer &ASan;
590   DIBuilder DIB;
591   LLVMContext *C;
592   Type *IntptrTy;
593   Type *IntptrPtrTy;
594   ShadowMapping Mapping;
595 
596   SmallVector<AllocaInst *, 16> AllocaVec;
597   SmallSetVector<AllocaInst *, 16> NonInstrumentedStaticAllocaVec;
598   SmallVector<Instruction *, 8> RetVec;
599   unsigned StackAlignment;
600 
601   Function *AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
602       *AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1];
603   Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc;
604   Function *AsanAllocaPoisonFunc, *AsanAllocasUnpoisonFunc;
605 
606   // Stores a place and arguments of poisoning/unpoisoning call for alloca.
607   struct AllocaPoisonCall {
608     IntrinsicInst *InsBefore;
609     AllocaInst *AI;
610     uint64_t Size;
611     bool DoPoison;
612   };
613   SmallVector<AllocaPoisonCall, 8> AllocaPoisonCallVec;
614 
615   SmallVector<AllocaInst *, 1> DynamicAllocaVec;
616   SmallVector<IntrinsicInst *, 1> StackRestoreVec;
617   AllocaInst *DynamicAllocaLayout = nullptr;
618   IntrinsicInst *LocalEscapeCall = nullptr;
619 
620   // Maps Value to an AllocaInst from which the Value is originated.
621   typedef DenseMap<Value *, AllocaInst *> AllocaForValueMapTy;
622   AllocaForValueMapTy AllocaForValue;
623 
624   bool HasNonEmptyInlineAsm = false;
625   bool HasReturnsTwiceCall = false;
626   std::unique_ptr<CallInst> EmptyInlineAsm;
627 
628   FunctionStackPoisoner(Function &F, AddressSanitizer &ASan)
629       : F(F),
630         ASan(ASan),
631         DIB(*F.getParent(), /*AllowUnresolved*/ false),
632         C(ASan.C),
633         IntptrTy(ASan.IntptrTy),
634         IntptrPtrTy(PointerType::get(IntptrTy, 0)),
635         Mapping(ASan.Mapping),
636         StackAlignment(1 << Mapping.Scale),
637         EmptyInlineAsm(CallInst::Create(ASan.EmptyAsm)) {}
638 
639   bool runOnFunction() {
640     if (!ClStack) return false;
641     // Collect alloca, ret, lifetime instructions etc.
642     for (BasicBlock *BB : depth_first(&F.getEntryBlock())) visit(*BB);
643 
644     if (AllocaVec.empty() && DynamicAllocaVec.empty()) return false;
645 
646     initializeCallbacks(*F.getParent());
647 
648     poisonStack();
649 
650     if (ClDebugStack) {
651       DEBUG(dbgs() << F);
652     }
653     return true;
654   }
655 
656   // Finds all Alloca instructions and puts
657   // poisoned red zones around all of them.
658   // Then unpoison everything back before the function returns.
659   void poisonStack();
660 
661   void createDynamicAllocasInitStorage();
662 
663   // ----------------------- Visitors.
664   /// \brief Collect all Ret instructions.
665   void visitReturnInst(ReturnInst &RI) { RetVec.push_back(&RI); }
666 
667   void unpoisonDynamicAllocasBeforeInst(Instruction *InstBefore,
668                                         Value *SavedStack) {
669     IRBuilder<> IRB(InstBefore);
670     Value *DynamicAreaPtr = IRB.CreatePtrToInt(SavedStack, IntptrTy);
671     // When we insert _asan_allocas_unpoison before @llvm.stackrestore, we
672     // need to adjust extracted SP to compute the address of the most recent
673     // alloca. We have a special @llvm.get.dynamic.area.offset intrinsic for
674     // this purpose.
675     if (!isa<ReturnInst>(InstBefore)) {
676       Function *DynamicAreaOffsetFunc = Intrinsic::getDeclaration(
677           InstBefore->getModule(), Intrinsic::get_dynamic_area_offset,
678           {IntptrTy});
679 
680       Value *DynamicAreaOffset = IRB.CreateCall(DynamicAreaOffsetFunc, {});
681 
682       DynamicAreaPtr = IRB.CreateAdd(IRB.CreatePtrToInt(SavedStack, IntptrTy),
683                                      DynamicAreaOffset);
684     }
685 
686     IRB.CreateCall(AsanAllocasUnpoisonFunc,
687                    {IRB.CreateLoad(DynamicAllocaLayout), DynamicAreaPtr});
688   }
689 
690   // Unpoison dynamic allocas redzones.
691   void unpoisonDynamicAllocas() {
692     for (auto &Ret : RetVec)
693       unpoisonDynamicAllocasBeforeInst(Ret, DynamicAllocaLayout);
694 
695     for (auto &StackRestoreInst : StackRestoreVec)
696       unpoisonDynamicAllocasBeforeInst(StackRestoreInst,
697                                        StackRestoreInst->getOperand(0));
698   }
699 
700   // Deploy and poison redzones around dynamic alloca call. To do this, we
701   // should replace this call with another one with changed parameters and
702   // replace all its uses with new address, so
703   //   addr = alloca type, old_size, align
704   // is replaced by
705   //   new_size = (old_size + additional_size) * sizeof(type)
706   //   tmp = alloca i8, new_size, max(align, 32)
707   //   addr = tmp + 32 (first 32 bytes are for the left redzone).
708   // Additional_size is added to make new memory allocation contain not only
709   // requested memory, but also left, partial and right redzones.
710   void handleDynamicAllocaCall(AllocaInst *AI);
711 
712   /// \brief Collect Alloca instructions we want (and can) handle.
713   void visitAllocaInst(AllocaInst &AI) {
714     if (!ASan.isInterestingAlloca(AI)) {
715       if (AI.isStaticAlloca()) NonInstrumentedStaticAllocaVec.insert(&AI);
716       return;
717     }
718 
719     StackAlignment = std::max(StackAlignment, AI.getAlignment());
720     if (ASan.isDynamicAlloca(AI))
721       DynamicAllocaVec.push_back(&AI);
722     else
723       AllocaVec.push_back(&AI);
724   }
725 
726   /// \brief Collect lifetime intrinsic calls to check for use-after-scope
727   /// errors.
728   void visitIntrinsicInst(IntrinsicInst &II) {
729     Intrinsic::ID ID = II.getIntrinsicID();
730     if (ID == Intrinsic::stackrestore) StackRestoreVec.push_back(&II);
731     if (ID == Intrinsic::localescape) LocalEscapeCall = &II;
732     if (!ASan.UseAfterScope)
733       return;
734     if (ID != Intrinsic::lifetime_start && ID != Intrinsic::lifetime_end)
735       return;
736     // Found lifetime intrinsic, add ASan instrumentation if necessary.
737     ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0));
738     // If size argument is undefined, don't do anything.
739     if (Size->isMinusOne()) return;
740     // Check that size doesn't saturate uint64_t and can
741     // be stored in IntptrTy.
742     const uint64_t SizeValue = Size->getValue().getLimitedValue();
743     if (SizeValue == ~0ULL ||
744         !ConstantInt::isValueValidForType(IntptrTy, SizeValue))
745       return;
746     // Find alloca instruction that corresponds to llvm.lifetime argument.
747     AllocaInst *AI = findAllocaForValue(II.getArgOperand(1));
748     if (!AI) return;
749     bool DoPoison = (ID == Intrinsic::lifetime_end);
750     AllocaPoisonCall APC = {&II, AI, SizeValue, DoPoison};
751     AllocaPoisonCallVec.push_back(APC);
752   }
753 
754   void visitCallSite(CallSite CS) {
755     Instruction *I = CS.getInstruction();
756     if (CallInst *CI = dyn_cast<CallInst>(I)) {
757       HasNonEmptyInlineAsm |=
758           CI->isInlineAsm() && !CI->isIdenticalTo(EmptyInlineAsm.get());
759       HasReturnsTwiceCall |= CI->canReturnTwice();
760     }
761   }
762 
763   // ---------------------- Helpers.
764   void initializeCallbacks(Module &M);
765 
766   bool doesDominateAllExits(const Instruction *I) const {
767     for (auto Ret : RetVec) {
768       if (!ASan.getDominatorTree().dominates(I, Ret)) return false;
769     }
770     return true;
771   }
772 
773   /// Finds alloca where the value comes from.
774   AllocaInst *findAllocaForValue(Value *V);
775   void poisonRedZones(ArrayRef<uint8_t> ShadowBytes, IRBuilder<> &IRB,
776                       Value *ShadowBase, bool DoPoison);
777   void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> &IRB, bool DoPoison);
778 
779   void SetShadowToStackAfterReturnInlined(IRBuilder<> &IRB, Value *ShadowBase,
780                                           int Size);
781   Value *createAllocaForLayout(IRBuilder<> &IRB, const ASanStackFrameLayout &L,
782                                bool Dynamic);
783   PHINode *createPHI(IRBuilder<> &IRB, Value *Cond, Value *ValueIfTrue,
784                      Instruction *ThenTerm, Value *ValueIfFalse);
785 };
786 
787 } // anonymous namespace
788 
789 char AddressSanitizer::ID = 0;
790 INITIALIZE_PASS_BEGIN(
791     AddressSanitizer, "asan",
792     "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", false,
793     false)
794 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
795 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
796 INITIALIZE_PASS_END(
797     AddressSanitizer, "asan",
798     "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", false,
799     false)
800 FunctionPass *llvm::createAddressSanitizerFunctionPass(bool CompileKernel,
801                                                        bool Recover,
802                                                        bool UseAfterScope) {
803   assert(!CompileKernel || Recover);
804   return new AddressSanitizer(CompileKernel, Recover, UseAfterScope);
805 }
806 
807 char AddressSanitizerModule::ID = 0;
808 INITIALIZE_PASS(
809     AddressSanitizerModule, "asan-module",
810     "AddressSanitizer: detects use-after-free and out-of-bounds bugs."
811     "ModulePass",
812     false, false)
813 ModulePass *llvm::createAddressSanitizerModulePass(bool CompileKernel,
814                                                    bool Recover) {
815   assert(!CompileKernel || Recover);
816   return new AddressSanitizerModule(CompileKernel, Recover);
817 }
818 
819 static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
820   size_t Res = countTrailingZeros(TypeSize / 8);
821   assert(Res < kNumberOfAccessSizes);
822   return Res;
823 }
824 
825 // \brief Create a constant for Str so that we can pass it to the run-time lib.
826 static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
827                                                     bool AllowMerging) {
828   Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
829   // We use private linkage for module-local strings. If they can be merged
830   // with another one, we set the unnamed_addr attribute.
831   GlobalVariable *GV =
832       new GlobalVariable(M, StrConst->getType(), true,
833                          GlobalValue::PrivateLinkage, StrConst, kAsanGenPrefix);
834   if (AllowMerging) GV->setUnnamedAddr(true);
835   GV->setAlignment(1);  // Strings may not be merged w/o setting align 1.
836   return GV;
837 }
838 
839 /// \brief Create a global describing a source location.
840 static GlobalVariable *createPrivateGlobalForSourceLoc(Module &M,
841                                                        LocationMetadata MD) {
842   Constant *LocData[] = {
843       createPrivateGlobalForString(M, MD.Filename, true),
844       ConstantInt::get(Type::getInt32Ty(M.getContext()), MD.LineNo),
845       ConstantInt::get(Type::getInt32Ty(M.getContext()), MD.ColumnNo),
846   };
847   auto LocStruct = ConstantStruct::getAnon(LocData);
848   auto GV = new GlobalVariable(M, LocStruct->getType(), true,
849                                GlobalValue::PrivateLinkage, LocStruct,
850                                kAsanGenPrefix);
851   GV->setUnnamedAddr(true);
852   return GV;
853 }
854 
855 static bool GlobalWasGeneratedByAsan(GlobalVariable *G) {
856   return G->getName().startswith(kAsanGenPrefix) ||
857          G->getName().startswith(kSanCovGenPrefix) ||
858          G->getName().startswith(kODRGenPrefix);
859 }
860 
861 Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
862   // Shadow >> scale
863   Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
864   if (Mapping.Offset == 0) return Shadow;
865   // (Shadow >> scale) | offset
866   if (Mapping.OrShadowOffset)
867     return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset));
868   else
869     return IRB.CreateAdd(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset));
870 }
871 
872 // Instrument memset/memmove/memcpy
873 void AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
874   IRBuilder<> IRB(MI);
875   if (isa<MemTransferInst>(MI)) {
876     IRB.CreateCall(
877         isa<MemMoveInst>(MI) ? AsanMemmove : AsanMemcpy,
878         {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
879          IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()),
880          IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
881   } else if (isa<MemSetInst>(MI)) {
882     IRB.CreateCall(
883         AsanMemset,
884         {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
885          IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false),
886          IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
887   }
888   MI->eraseFromParent();
889 }
890 
891 /// Check if we want (and can) handle this alloca.
892 bool AddressSanitizer::isInterestingAlloca(AllocaInst &AI) {
893   auto PreviouslySeenAllocaInfo = ProcessedAllocas.find(&AI);
894 
895   if (PreviouslySeenAllocaInfo != ProcessedAllocas.end())
896     return PreviouslySeenAllocaInfo->getSecond();
897 
898   bool IsInteresting =
899       (AI.getAllocatedType()->isSized() &&
900        // alloca() may be called with 0 size, ignore it.
901        getAllocaSizeInBytes(&AI) > 0 &&
902        // We are only interested in allocas not promotable to registers.
903        // Promotable allocas are common under -O0.
904        (!ClSkipPromotableAllocas || !isAllocaPromotable(&AI)) &&
905        // inalloca allocas are not treated as static, and we don't want
906        // dynamic alloca instrumentation for them as well.
907        !AI.isUsedWithInAlloca());
908 
909   ProcessedAllocas[&AI] = IsInteresting;
910   return IsInteresting;
911 }
912 
913 /// If I is an interesting memory access, return the PointerOperand
914 /// and set IsWrite/Alignment. Otherwise return nullptr.
915 Value *AddressSanitizer::isInterestingMemoryAccess(Instruction *I,
916                                                    bool *IsWrite,
917                                                    uint64_t *TypeSize,
918                                                    unsigned *Alignment) {
919   // Skip memory accesses inserted by another instrumentation.
920   if (I->getMetadata("nosanitize")) return nullptr;
921 
922   Value *PtrOperand = nullptr;
923   const DataLayout &DL = I->getModule()->getDataLayout();
924   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
925     if (!ClInstrumentReads) return nullptr;
926     *IsWrite = false;
927     *TypeSize = DL.getTypeStoreSizeInBits(LI->getType());
928     *Alignment = LI->getAlignment();
929     PtrOperand = LI->getPointerOperand();
930   } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
931     if (!ClInstrumentWrites) return nullptr;
932     *IsWrite = true;
933     *TypeSize = DL.getTypeStoreSizeInBits(SI->getValueOperand()->getType());
934     *Alignment = SI->getAlignment();
935     PtrOperand = SI->getPointerOperand();
936   } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
937     if (!ClInstrumentAtomics) return nullptr;
938     *IsWrite = true;
939     *TypeSize = DL.getTypeStoreSizeInBits(RMW->getValOperand()->getType());
940     *Alignment = 0;
941     PtrOperand = RMW->getPointerOperand();
942   } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
943     if (!ClInstrumentAtomics) return nullptr;
944     *IsWrite = true;
945     *TypeSize = DL.getTypeStoreSizeInBits(XCHG->getCompareOperand()->getType());
946     *Alignment = 0;
947     PtrOperand = XCHG->getPointerOperand();
948   }
949 
950   // Treat memory accesses to promotable allocas as non-interesting since they
951   // will not cause memory violations. This greatly speeds up the instrumented
952   // executable at -O0.
953   if (ClSkipPromotableAllocas)
954     if (auto AI = dyn_cast_or_null<AllocaInst>(PtrOperand))
955       return isInterestingAlloca(*AI) ? AI : nullptr;
956 
957   return PtrOperand;
958 }
959 
960 static bool isPointerOperand(Value *V) {
961   return V->getType()->isPointerTy() || isa<PtrToIntInst>(V);
962 }
963 
964 // This is a rough heuristic; it may cause both false positives and
965 // false negatives. The proper implementation requires cooperation with
966 // the frontend.
967 static bool isInterestingPointerComparisonOrSubtraction(Instruction *I) {
968   if (ICmpInst *Cmp = dyn_cast<ICmpInst>(I)) {
969     if (!Cmp->isRelational()) return false;
970   } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
971     if (BO->getOpcode() != Instruction::Sub) return false;
972   } else {
973     return false;
974   }
975   return isPointerOperand(I->getOperand(0)) &&
976          isPointerOperand(I->getOperand(1));
977 }
978 
979 bool AddressSanitizer::GlobalIsLinkerInitialized(GlobalVariable *G) {
980   // If a global variable does not have dynamic initialization we don't
981   // have to instrument it.  However, if a global does not have initializer
982   // at all, we assume it has dynamic initializer (in other TU).
983   return G->hasInitializer() && !GlobalsMD.get(G).IsDynInit;
984 }
985 
986 void AddressSanitizer::instrumentPointerComparisonOrSubtraction(
987     Instruction *I) {
988   IRBuilder<> IRB(I);
989   Function *F = isa<ICmpInst>(I) ? AsanPtrCmpFunction : AsanPtrSubFunction;
990   Value *Param[2] = {I->getOperand(0), I->getOperand(1)};
991   for (int i = 0; i < 2; i++) {
992     if (Param[i]->getType()->isPointerTy())
993       Param[i] = IRB.CreatePointerCast(Param[i], IntptrTy);
994   }
995   IRB.CreateCall(F, Param);
996 }
997 
998 void AddressSanitizer::instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis,
999                                      Instruction *I, bool UseCalls,
1000                                      const DataLayout &DL) {
1001   bool IsWrite = false;
1002   unsigned Alignment = 0;
1003   uint64_t TypeSize = 0;
1004   Value *Addr = isInterestingMemoryAccess(I, &IsWrite, &TypeSize, &Alignment);
1005   assert(Addr);
1006 
1007   // Optimization experiments.
1008   // The experiments can be used to evaluate potential optimizations that remove
1009   // instrumentation (assess false negatives). Instead of completely removing
1010   // some instrumentation, you set Exp to a non-zero value (mask of optimization
1011   // experiments that want to remove instrumentation of this instruction).
1012   // If Exp is non-zero, this pass will emit special calls into runtime
1013   // (e.g. __asan_report_exp_load1 instead of __asan_report_load1). These calls
1014   // make runtime terminate the program in a special way (with a different
1015   // exit status). Then you run the new compiler on a buggy corpus, collect
1016   // the special terminations (ideally, you don't see them at all -- no false
1017   // negatives) and make the decision on the optimization.
1018   uint32_t Exp = ClForceExperiment;
1019 
1020   if (ClOpt && ClOptGlobals) {
1021     // If initialization order checking is disabled, a simple access to a
1022     // dynamically initialized global is always valid.
1023     GlobalVariable *G = dyn_cast<GlobalVariable>(GetUnderlyingObject(Addr, DL));
1024     if (G && (!ClInitializers || GlobalIsLinkerInitialized(G)) &&
1025         isSafeAccess(ObjSizeVis, Addr, TypeSize)) {
1026       NumOptimizedAccessesToGlobalVar++;
1027       return;
1028     }
1029   }
1030 
1031   if (ClOpt && ClOptStack) {
1032     // A direct inbounds access to a stack variable is always valid.
1033     if (isa<AllocaInst>(GetUnderlyingObject(Addr, DL)) &&
1034         isSafeAccess(ObjSizeVis, Addr, TypeSize)) {
1035       NumOptimizedAccessesToStackVar++;
1036       return;
1037     }
1038   }
1039 
1040   if (IsWrite)
1041     NumInstrumentedWrites++;
1042   else
1043     NumInstrumentedReads++;
1044 
1045   unsigned Granularity = 1 << Mapping.Scale;
1046   // Instrument a 1-, 2-, 4-, 8-, or 16- byte access with one check
1047   // if the data is properly aligned.
1048   if ((TypeSize == 8 || TypeSize == 16 || TypeSize == 32 || TypeSize == 64 ||
1049        TypeSize == 128) &&
1050       (Alignment >= Granularity || Alignment == 0 || Alignment >= TypeSize / 8))
1051     return instrumentAddress(I, I, Addr, TypeSize, IsWrite, nullptr, UseCalls,
1052                              Exp);
1053   instrumentUnusualSizeOrAlignment(I, Addr, TypeSize, IsWrite, nullptr,
1054                                    UseCalls, Exp);
1055 }
1056 
1057 Instruction *AddressSanitizer::generateCrashCode(Instruction *InsertBefore,
1058                                                  Value *Addr, bool IsWrite,
1059                                                  size_t AccessSizeIndex,
1060                                                  Value *SizeArgument,
1061                                                  uint32_t Exp) {
1062   IRBuilder<> IRB(InsertBefore);
1063   Value *ExpVal = Exp == 0 ? nullptr : ConstantInt::get(IRB.getInt32Ty(), Exp);
1064   CallInst *Call = nullptr;
1065   if (SizeArgument) {
1066     if (Exp == 0)
1067       Call = IRB.CreateCall(AsanErrorCallbackSized[IsWrite][0],
1068                             {Addr, SizeArgument});
1069     else
1070       Call = IRB.CreateCall(AsanErrorCallbackSized[IsWrite][1],
1071                             {Addr, SizeArgument, ExpVal});
1072   } else {
1073     if (Exp == 0)
1074       Call =
1075           IRB.CreateCall(AsanErrorCallback[IsWrite][0][AccessSizeIndex], Addr);
1076     else
1077       Call = IRB.CreateCall(AsanErrorCallback[IsWrite][1][AccessSizeIndex],
1078                             {Addr, ExpVal});
1079   }
1080 
1081   // We don't do Call->setDoesNotReturn() because the BB already has
1082   // UnreachableInst at the end.
1083   // This EmptyAsm is required to avoid callback merge.
1084   IRB.CreateCall(EmptyAsm, {});
1085   return Call;
1086 }
1087 
1088 Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
1089                                            Value *ShadowValue,
1090                                            uint32_t TypeSize) {
1091   size_t Granularity = static_cast<size_t>(1) << Mapping.Scale;
1092   // Addr & (Granularity - 1)
1093   Value *LastAccessedByte =
1094       IRB.CreateAnd(AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
1095   // (Addr & (Granularity - 1)) + size - 1
1096   if (TypeSize / 8 > 1)
1097     LastAccessedByte = IRB.CreateAdd(
1098         LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
1099   // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
1100   LastAccessedByte =
1101       IRB.CreateIntCast(LastAccessedByte, ShadowValue->getType(), false);
1102   // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
1103   return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
1104 }
1105 
1106 void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
1107                                          Instruction *InsertBefore, Value *Addr,
1108                                          uint32_t TypeSize, bool IsWrite,
1109                                          Value *SizeArgument, bool UseCalls,
1110                                          uint32_t Exp) {
1111   IRBuilder<> IRB(InsertBefore);
1112   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
1113   size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
1114 
1115   if (UseCalls) {
1116     if (Exp == 0)
1117       IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][0][AccessSizeIndex],
1118                      AddrLong);
1119     else
1120       IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][1][AccessSizeIndex],
1121                      {AddrLong, ConstantInt::get(IRB.getInt32Ty(), Exp)});
1122     return;
1123   }
1124 
1125   Type *ShadowTy =
1126       IntegerType::get(*C, std::max(8U, TypeSize >> Mapping.Scale));
1127   Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
1128   Value *ShadowPtr = memToShadow(AddrLong, IRB);
1129   Value *CmpVal = Constant::getNullValue(ShadowTy);
1130   Value *ShadowValue =
1131       IRB.CreateLoad(IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
1132 
1133   Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
1134   size_t Granularity = 1ULL << Mapping.Scale;
1135   TerminatorInst *CrashTerm = nullptr;
1136 
1137   if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) {
1138     // We use branch weights for the slow path check, to indicate that the slow
1139     // path is rarely taken. This seems to be the case for SPEC benchmarks.
1140     TerminatorInst *CheckTerm = SplitBlockAndInsertIfThen(
1141         Cmp, InsertBefore, false, MDBuilder(*C).createBranchWeights(1, 100000));
1142     assert(cast<BranchInst>(CheckTerm)->isUnconditional());
1143     BasicBlock *NextBB = CheckTerm->getSuccessor(0);
1144     IRB.SetInsertPoint(CheckTerm);
1145     Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
1146     if (Recover) {
1147       CrashTerm = SplitBlockAndInsertIfThen(Cmp2, CheckTerm, false);
1148     } else {
1149       BasicBlock *CrashBlock =
1150         BasicBlock::Create(*C, "", NextBB->getParent(), NextBB);
1151       CrashTerm = new UnreachableInst(*C, CrashBlock);
1152       BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
1153       ReplaceInstWithInst(CheckTerm, NewTerm);
1154     }
1155   } else {
1156     CrashTerm = SplitBlockAndInsertIfThen(Cmp, InsertBefore, !Recover);
1157   }
1158 
1159   Instruction *Crash = generateCrashCode(CrashTerm, AddrLong, IsWrite,
1160                                          AccessSizeIndex, SizeArgument, Exp);
1161   Crash->setDebugLoc(OrigIns->getDebugLoc());
1162 }
1163 
1164 // Instrument unusual size or unusual alignment.
1165 // We can not do it with a single check, so we do 1-byte check for the first
1166 // and the last bytes. We call __asan_report_*_n(addr, real_size) to be able
1167 // to report the actual access size.
1168 void AddressSanitizer::instrumentUnusualSizeOrAlignment(
1169     Instruction *I, Value *Addr, uint32_t TypeSize, bool IsWrite,
1170     Value *SizeArgument, bool UseCalls, uint32_t Exp) {
1171   IRBuilder<> IRB(I);
1172   Value *Size = ConstantInt::get(IntptrTy, TypeSize / 8);
1173   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
1174   if (UseCalls) {
1175     if (Exp == 0)
1176       IRB.CreateCall(AsanMemoryAccessCallbackSized[IsWrite][0],
1177                      {AddrLong, Size});
1178     else
1179       IRB.CreateCall(AsanMemoryAccessCallbackSized[IsWrite][1],
1180                      {AddrLong, Size, ConstantInt::get(IRB.getInt32Ty(), Exp)});
1181   } else {
1182     Value *LastByte = IRB.CreateIntToPtr(
1183         IRB.CreateAdd(AddrLong, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)),
1184         Addr->getType());
1185     instrumentAddress(I, I, Addr, 8, IsWrite, Size, false, Exp);
1186     instrumentAddress(I, I, LastByte, 8, IsWrite, Size, false, Exp);
1187   }
1188 }
1189 
1190 void AddressSanitizerModule::poisonOneInitializer(Function &GlobalInit,
1191                                                   GlobalValue *ModuleName) {
1192   // Set up the arguments to our poison/unpoison functions.
1193   IRBuilder<> IRB(&GlobalInit.front(),
1194                   GlobalInit.front().getFirstInsertionPt());
1195 
1196   // Add a call to poison all external globals before the given function starts.
1197   Value *ModuleNameAddr = ConstantExpr::getPointerCast(ModuleName, IntptrTy);
1198   IRB.CreateCall(AsanPoisonGlobals, ModuleNameAddr);
1199 
1200   // Add calls to unpoison all globals before each return instruction.
1201   for (auto &BB : GlobalInit.getBasicBlockList())
1202     if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
1203       CallInst::Create(AsanUnpoisonGlobals, "", RI);
1204 }
1205 
1206 void AddressSanitizerModule::createInitializerPoisonCalls(
1207     Module &M, GlobalValue *ModuleName) {
1208   GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
1209 
1210   ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
1211   for (Use &OP : CA->operands()) {
1212     if (isa<ConstantAggregateZero>(OP)) continue;
1213     ConstantStruct *CS = cast<ConstantStruct>(OP);
1214 
1215     // Must have a function or null ptr.
1216     if (Function *F = dyn_cast<Function>(CS->getOperand(1))) {
1217       if (F->getName() == kAsanModuleCtorName) continue;
1218       ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
1219       // Don't instrument CTORs that will run before asan.module_ctor.
1220       if (Priority->getLimitedValue() <= kAsanCtorAndDtorPriority) continue;
1221       poisonOneInitializer(*F, ModuleName);
1222     }
1223   }
1224 }
1225 
1226 bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) {
1227   Type *Ty = G->getValueType();
1228   DEBUG(dbgs() << "GLOBAL: " << *G << "\n");
1229 
1230   if (GlobalsMD.get(G).IsBlacklisted) return false;
1231   if (!Ty->isSized()) return false;
1232   if (!G->hasInitializer()) return false;
1233   if (GlobalWasGeneratedByAsan(G)) return false;  // Our own global.
1234   // Touch only those globals that will not be defined in other modules.
1235   // Don't handle ODR linkage types and COMDATs since other modules may be built
1236   // without ASan.
1237   if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
1238       G->getLinkage() != GlobalVariable::PrivateLinkage &&
1239       G->getLinkage() != GlobalVariable::InternalLinkage)
1240     return false;
1241   if (G->hasComdat()) return false;
1242   // Two problems with thread-locals:
1243   //   - The address of the main thread's copy can't be computed at link-time.
1244   //   - Need to poison all copies, not just the main thread's one.
1245   if (G->isThreadLocal()) return false;
1246   // For now, just ignore this Global if the alignment is large.
1247   if (G->getAlignment() > MinRedzoneSizeForGlobal()) return false;
1248 
1249   if (G->hasSection()) {
1250     StringRef Section = G->getSection();
1251 
1252     // Globals from llvm.metadata aren't emitted, do not instrument them.
1253     if (Section == "llvm.metadata") return false;
1254     // Do not instrument globals from special LLVM sections.
1255     if (Section.find("__llvm") != StringRef::npos || Section.find("__LLVM") != StringRef::npos) return false;
1256 
1257     // Do not instrument function pointers to initialization and termination
1258     // routines: dynamic linker will not properly handle redzones.
1259     if (Section.startswith(".preinit_array") ||
1260         Section.startswith(".init_array") ||
1261         Section.startswith(".fini_array")) {
1262       return false;
1263     }
1264 
1265     // Callbacks put into the CRT initializer/terminator sections
1266     // should not be instrumented.
1267     // See https://code.google.com/p/address-sanitizer/issues/detail?id=305
1268     // and http://msdn.microsoft.com/en-US/en-en/library/bb918180(v=vs.120).aspx
1269     if (Section.startswith(".CRT")) {
1270       DEBUG(dbgs() << "Ignoring a global initializer callback: " << *G << "\n");
1271       return false;
1272     }
1273 
1274     if (TargetTriple.isOSBinFormatMachO()) {
1275       StringRef ParsedSegment, ParsedSection;
1276       unsigned TAA = 0, StubSize = 0;
1277       bool TAAParsed;
1278       std::string ErrorCode = MCSectionMachO::ParseSectionSpecifier(
1279           Section, ParsedSegment, ParsedSection, TAA, TAAParsed, StubSize);
1280       assert(ErrorCode.empty() && "Invalid section specifier.");
1281 
1282       // Ignore the globals from the __OBJC section. The ObjC runtime assumes
1283       // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
1284       // them.
1285       if (ParsedSegment == "__OBJC" ||
1286           (ParsedSegment == "__DATA" && ParsedSection.startswith("__objc_"))) {
1287         DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G << "\n");
1288         return false;
1289       }
1290       // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
1291       // Constant CFString instances are compiled in the following way:
1292       //  -- the string buffer is emitted into
1293       //     __TEXT,__cstring,cstring_literals
1294       //  -- the constant NSConstantString structure referencing that buffer
1295       //     is placed into __DATA,__cfstring
1296       // Therefore there's no point in placing redzones into __DATA,__cfstring.
1297       // Moreover, it causes the linker to crash on OS X 10.7
1298       if (ParsedSegment == "__DATA" && ParsedSection == "__cfstring") {
1299         DEBUG(dbgs() << "Ignoring CFString: " << *G << "\n");
1300         return false;
1301       }
1302       // The linker merges the contents of cstring_literals and removes the
1303       // trailing zeroes.
1304       if (ParsedSegment == "__TEXT" && (TAA & MachO::S_CSTRING_LITERALS)) {
1305         DEBUG(dbgs() << "Ignoring a cstring literal: " << *G << "\n");
1306         return false;
1307       }
1308     }
1309   }
1310 
1311   return true;
1312 }
1313 
1314 // On Mach-O platforms, we emit global metadata in a separate section of the
1315 // binary in order to allow the linker to properly dead strip. This is only
1316 // supported on recent versions of ld64.
1317 bool AddressSanitizerModule::ShouldUseMachOGlobalsSection() const {
1318   if (!TargetTriple.isOSBinFormatMachO())
1319     return false;
1320 
1321   if (TargetTriple.isMacOSX() && !TargetTriple.isMacOSXVersionLT(10, 11))
1322     return true;
1323   if (TargetTriple.isiOS() /* or tvOS */ && !TargetTriple.isOSVersionLT(9))
1324     return true;
1325   if (TargetTriple.isWatchOS() && !TargetTriple.isOSVersionLT(2))
1326     return true;
1327 
1328   return false;
1329 }
1330 
1331 void AddressSanitizerModule::initializeCallbacks(Module &M) {
1332   IRBuilder<> IRB(*C);
1333 
1334   // Declare our poisoning and unpoisoning functions.
1335   AsanPoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1336       kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
1337   AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
1338   AsanUnpoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1339       kAsanUnpoisonGlobalsName, IRB.getVoidTy(), nullptr));
1340   AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
1341 
1342   // Declare functions that register/unregister globals.
1343   AsanRegisterGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1344       kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1345   AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
1346   AsanUnregisterGlobals = checkSanitizerInterfaceFunction(
1347       M.getOrInsertFunction(kAsanUnregisterGlobalsName, IRB.getVoidTy(),
1348                             IntptrTy, IntptrTy, nullptr));
1349   AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
1350 
1351   // Declare the functions that find globals in a shared object and then invoke
1352   // the (un)register function on them.
1353   AsanRegisterImageGlobals = checkSanitizerInterfaceFunction(
1354       M.getOrInsertFunction(kAsanRegisterImageGlobalsName,
1355       IRB.getVoidTy(), IntptrTy, nullptr));
1356   AsanRegisterImageGlobals->setLinkage(Function::ExternalLinkage);
1357 
1358   AsanUnregisterImageGlobals = checkSanitizerInterfaceFunction(
1359       M.getOrInsertFunction(kAsanUnregisterImageGlobalsName,
1360       IRB.getVoidTy(), IntptrTy, nullptr));
1361   AsanUnregisterImageGlobals->setLinkage(Function::ExternalLinkage);
1362 }
1363 
1364 // This function replaces all global variables with new variables that have
1365 // trailing redzones. It also creates a function that poisons
1366 // redzones and inserts this function into llvm.global_ctors.
1367 bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) {
1368   GlobalsMD.init(M);
1369 
1370   SmallVector<GlobalVariable *, 16> GlobalsToChange;
1371 
1372   for (auto &G : M.globals()) {
1373     if (ShouldInstrumentGlobal(&G)) GlobalsToChange.push_back(&G);
1374   }
1375 
1376   size_t n = GlobalsToChange.size();
1377   if (n == 0) return false;
1378 
1379   // A global is described by a structure
1380   //   size_t beg;
1381   //   size_t size;
1382   //   size_t size_with_redzone;
1383   //   const char *name;
1384   //   const char *module_name;
1385   //   size_t has_dynamic_init;
1386   //   void *source_location;
1387   //   size_t odr_indicator;
1388   // We initialize an array of such structures and pass it to a run-time call.
1389   StructType *GlobalStructTy =
1390       StructType::get(IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy,
1391                       IntptrTy, IntptrTy, IntptrTy, nullptr);
1392   SmallVector<Constant *, 16> Initializers(n);
1393 
1394   bool HasDynamicallyInitializedGlobals = false;
1395 
1396   // We shouldn't merge same module names, as this string serves as unique
1397   // module ID in runtime.
1398   GlobalVariable *ModuleName = createPrivateGlobalForString(
1399       M, M.getModuleIdentifier(), /*AllowMerging*/ false);
1400 
1401   auto &DL = M.getDataLayout();
1402   for (size_t i = 0; i < n; i++) {
1403     static const uint64_t kMaxGlobalRedzone = 1 << 18;
1404     GlobalVariable *G = GlobalsToChange[i];
1405 
1406     auto MD = GlobalsMD.get(G);
1407     StringRef NameForGlobal = G->getName();
1408     // Create string holding the global name (use global name from metadata
1409     // if it's available, otherwise just write the name of global variable).
1410     GlobalVariable *Name = createPrivateGlobalForString(
1411         M, MD.Name.empty() ? NameForGlobal : MD.Name,
1412         /*AllowMerging*/ true);
1413 
1414     Type *Ty = G->getValueType();
1415     uint64_t SizeInBytes = DL.getTypeAllocSize(Ty);
1416     uint64_t MinRZ = MinRedzoneSizeForGlobal();
1417     // MinRZ <= RZ <= kMaxGlobalRedzone
1418     // and trying to make RZ to be ~ 1/4 of SizeInBytes.
1419     uint64_t RZ = std::max(
1420         MinRZ, std::min(kMaxGlobalRedzone, (SizeInBytes / MinRZ / 4) * MinRZ));
1421     uint64_t RightRedzoneSize = RZ;
1422     // Round up to MinRZ
1423     if (SizeInBytes % MinRZ) RightRedzoneSize += MinRZ - (SizeInBytes % MinRZ);
1424     assert(((RightRedzoneSize + SizeInBytes) % MinRZ) == 0);
1425     Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
1426 
1427     StructType *NewTy = StructType::get(Ty, RightRedZoneTy, nullptr);
1428     Constant *NewInitializer =
1429         ConstantStruct::get(NewTy, G->getInitializer(),
1430                             Constant::getNullValue(RightRedZoneTy), nullptr);
1431 
1432     // Create a new global variable with enough space for a redzone.
1433     GlobalValue::LinkageTypes Linkage = G->getLinkage();
1434     if (G->isConstant() && Linkage == GlobalValue::PrivateLinkage)
1435       Linkage = GlobalValue::InternalLinkage;
1436     GlobalVariable *NewGlobal =
1437         new GlobalVariable(M, NewTy, G->isConstant(), Linkage, NewInitializer,
1438                            "", G, G->getThreadLocalMode());
1439     NewGlobal->copyAttributesFrom(G);
1440     NewGlobal->setAlignment(MinRZ);
1441 
1442     Value *Indices2[2];
1443     Indices2[0] = IRB.getInt32(0);
1444     Indices2[1] = IRB.getInt32(0);
1445 
1446     G->replaceAllUsesWith(
1447         ConstantExpr::getGetElementPtr(NewTy, NewGlobal, Indices2, true));
1448     NewGlobal->takeName(G);
1449     G->eraseFromParent();
1450 
1451     Constant *SourceLoc;
1452     if (!MD.SourceLoc.empty()) {
1453       auto SourceLocGlobal = createPrivateGlobalForSourceLoc(M, MD.SourceLoc);
1454       SourceLoc = ConstantExpr::getPointerCast(SourceLocGlobal, IntptrTy);
1455     } else {
1456       SourceLoc = ConstantInt::get(IntptrTy, 0);
1457     }
1458 
1459     Constant *ODRIndicator = ConstantExpr::getNullValue(IRB.getInt8PtrTy());
1460     GlobalValue *InstrumentedGlobal = NewGlobal;
1461 
1462     bool CanUsePrivateAliases = TargetTriple.isOSBinFormatELF();
1463     if (CanUsePrivateAliases && ClUsePrivateAliasForGlobals) {
1464       // Create local alias for NewGlobal to avoid crash on ODR between
1465       // instrumented and non-instrumented libraries.
1466       auto *GA = GlobalAlias::create(GlobalValue::InternalLinkage,
1467                                      NameForGlobal + M.getName(), NewGlobal);
1468 
1469       // With local aliases, we need to provide another externally visible
1470       // symbol __odr_asan_XXX to detect ODR violation.
1471       auto *ODRIndicatorSym =
1472           new GlobalVariable(M, IRB.getInt8Ty(), false, Linkage,
1473                              Constant::getNullValue(IRB.getInt8Ty()),
1474                              kODRGenPrefix + NameForGlobal, nullptr,
1475                              NewGlobal->getThreadLocalMode());
1476 
1477       // Set meaningful attributes for indicator symbol.
1478       ODRIndicatorSym->setVisibility(NewGlobal->getVisibility());
1479       ODRIndicatorSym->setDLLStorageClass(NewGlobal->getDLLStorageClass());
1480       ODRIndicatorSym->setAlignment(1);
1481       ODRIndicator = ODRIndicatorSym;
1482       InstrumentedGlobal = GA;
1483     }
1484 
1485     Initializers[i] = ConstantStruct::get(
1486         GlobalStructTy,
1487         ConstantExpr::getPointerCast(InstrumentedGlobal, IntptrTy),
1488         ConstantInt::get(IntptrTy, SizeInBytes),
1489         ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
1490         ConstantExpr::getPointerCast(Name, IntptrTy),
1491         ConstantExpr::getPointerCast(ModuleName, IntptrTy),
1492         ConstantInt::get(IntptrTy, MD.IsDynInit), SourceLoc,
1493         ConstantExpr::getPointerCast(ODRIndicator, IntptrTy), nullptr);
1494 
1495     if (ClInitializers && MD.IsDynInit) HasDynamicallyInitializedGlobals = true;
1496 
1497     DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
1498   }
1499 
1500 
1501   GlobalVariable *AllGlobals = nullptr;
1502   GlobalVariable *RegisteredFlag = nullptr;
1503 
1504   // On recent Mach-O platforms, we emit the global metadata in a way that
1505   // allows the linker to properly strip dead globals.
1506   if (ShouldUseMachOGlobalsSection()) {
1507     // RegisteredFlag serves two purposes. First, we can pass it to dladdr()
1508     // to look up the loaded image that contains it. Second, we can store in it
1509     // whether registration has already occurred, to prevent duplicate
1510     // registration.
1511     //
1512     // Common linkage allows us to coalesce needles defined in each object
1513     // file so that there's only one per shared library.
1514     RegisteredFlag = new GlobalVariable(
1515         M, IntptrTy, false, GlobalVariable::CommonLinkage,
1516         ConstantInt::get(IntptrTy, 0), kAsanGlobalsRegisteredFlagName);
1517 
1518     // We also emit a structure which binds the liveness of the global
1519     // variable to the metadata struct.
1520     StructType *LivenessTy = StructType::get(IntptrTy, IntptrTy, nullptr);
1521 
1522     for (size_t i = 0; i < n; i++) {
1523       GlobalVariable *Metadata = new GlobalVariable(
1524           M, GlobalStructTy, false, GlobalVariable::InternalLinkage,
1525           Initializers[i], "");
1526       Metadata->setSection("__DATA,__asan_globals,regular");
1527       Metadata->setAlignment(1); // don't leave padding in between
1528 
1529       auto LivenessBinder = ConstantStruct::get(LivenessTy,
1530           Initializers[i]->getAggregateElement(0u),
1531           ConstantExpr::getPointerCast(Metadata, IntptrTy),
1532           nullptr);
1533       GlobalVariable *Liveness = new GlobalVariable(
1534           M, LivenessTy, false, GlobalVariable::InternalLinkage,
1535           LivenessBinder, "");
1536       Liveness->setSection("__DATA,__asan_liveness,regular,live_support");
1537     }
1538   } else {
1539     // On all other platfoms, we just emit an array of global metadata
1540     // structures.
1541     ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
1542     AllGlobals = new GlobalVariable(
1543         M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage,
1544         ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
1545   }
1546 
1547   // Create calls for poisoning before initializers run and unpoisoning after.
1548   if (HasDynamicallyInitializedGlobals)
1549     createInitializerPoisonCalls(M, ModuleName);
1550 
1551   // Create a call to register the globals with the runtime.
1552   if (ShouldUseMachOGlobalsSection()) {
1553     IRB.CreateCall(AsanRegisterImageGlobals,
1554                    {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
1555   } else {
1556     IRB.CreateCall(AsanRegisterGlobals,
1557                    {IRB.CreatePointerCast(AllGlobals, IntptrTy),
1558                     ConstantInt::get(IntptrTy, n)});
1559   }
1560 
1561   // We also need to unregister globals at the end, e.g., when a shared library
1562   // gets closed.
1563   Function *AsanDtorFunction =
1564       Function::Create(FunctionType::get(Type::getVoidTy(*C), false),
1565                        GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
1566   BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
1567   IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
1568 
1569   if (ShouldUseMachOGlobalsSection()) {
1570     IRB_Dtor.CreateCall(AsanUnregisterImageGlobals,
1571                         {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
1572   } else {
1573     IRB_Dtor.CreateCall(AsanUnregisterGlobals,
1574                         {IRB.CreatePointerCast(AllGlobals, IntptrTy),
1575                          ConstantInt::get(IntptrTy, n)});
1576   }
1577 
1578   appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority);
1579 
1580   DEBUG(dbgs() << M);
1581   return true;
1582 }
1583 
1584 bool AddressSanitizerModule::runOnModule(Module &M) {
1585   C = &(M.getContext());
1586   int LongSize = M.getDataLayout().getPointerSizeInBits();
1587   IntptrTy = Type::getIntNTy(*C, LongSize);
1588   TargetTriple = Triple(M.getTargetTriple());
1589   Mapping = getShadowMapping(TargetTriple, LongSize, CompileKernel);
1590   initializeCallbacks(M);
1591 
1592   bool Changed = false;
1593 
1594   // TODO(glider): temporarily disabled globals instrumentation for KASan.
1595   if (ClGlobals && !CompileKernel) {
1596     Function *CtorFunc = M.getFunction(kAsanModuleCtorName);
1597     assert(CtorFunc);
1598     IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator());
1599     Changed |= InstrumentGlobals(IRB, M);
1600   }
1601 
1602   return Changed;
1603 }
1604 
1605 void AddressSanitizer::initializeCallbacks(Module &M) {
1606   IRBuilder<> IRB(*C);
1607   // Create __asan_report* callbacks.
1608   // IsWrite, TypeSize and Exp are encoded in the function name.
1609   for (int Exp = 0; Exp < 2; Exp++) {
1610     for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
1611       const std::string TypeStr = AccessIsWrite ? "store" : "load";
1612       const std::string ExpStr = Exp ? "exp_" : "";
1613       const std::string SuffixStr = CompileKernel ? "N" : "_n";
1614       const std::string EndingStr = Recover ? "_noabort" : "";
1615       Type *ExpType = Exp ? Type::getInt32Ty(*C) : nullptr;
1616       AsanErrorCallbackSized[AccessIsWrite][Exp] =
1617           checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1618               kAsanReportErrorTemplate + ExpStr + TypeStr + SuffixStr + EndingStr,
1619               IRB.getVoidTy(), IntptrTy, IntptrTy, ExpType, nullptr));
1620       AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] =
1621           checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1622               ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr,
1623               IRB.getVoidTy(), IntptrTy, IntptrTy, ExpType, nullptr));
1624       for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
1625            AccessSizeIndex++) {
1626         const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex);
1627         AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] =
1628             checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1629                 kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr,
1630                 IRB.getVoidTy(), IntptrTy, ExpType, nullptr));
1631         AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] =
1632             checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1633                 ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr,
1634                 IRB.getVoidTy(), IntptrTy, ExpType, nullptr));
1635       }
1636     }
1637   }
1638 
1639   const std::string MemIntrinCallbackPrefix =
1640       CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
1641   AsanMemmove = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1642       MemIntrinCallbackPrefix + "memmove", IRB.getInt8PtrTy(),
1643       IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr));
1644   AsanMemcpy = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1645       MemIntrinCallbackPrefix + "memcpy", IRB.getInt8PtrTy(),
1646       IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr));
1647   AsanMemset = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1648       MemIntrinCallbackPrefix + "memset", IRB.getInt8PtrTy(),
1649       IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy, nullptr));
1650 
1651   AsanHandleNoReturnFunc = checkSanitizerInterfaceFunction(
1652       M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy(), nullptr));
1653 
1654   AsanPtrCmpFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1655       kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1656   AsanPtrSubFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1657       kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1658   // We insert an empty inline asm after __asan_report* to avoid callback merge.
1659   EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
1660                             StringRef(""), StringRef(""),
1661                             /*hasSideEffects=*/true);
1662 }
1663 
1664 // virtual
1665 bool AddressSanitizer::doInitialization(Module &M) {
1666   // Initialize the private fields. No one has accessed them before.
1667 
1668   GlobalsMD.init(M);
1669 
1670   C = &(M.getContext());
1671   LongSize = M.getDataLayout().getPointerSizeInBits();
1672   IntptrTy = Type::getIntNTy(*C, LongSize);
1673   TargetTriple = Triple(M.getTargetTriple());
1674 
1675   if (!CompileKernel) {
1676     std::tie(AsanCtorFunction, AsanInitFunction) =
1677         createSanitizerCtorAndInitFunctions(
1678             M, kAsanModuleCtorName, kAsanInitName,
1679             /*InitArgTypes=*/{}, /*InitArgs=*/{}, kAsanVersionCheckName);
1680     appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority);
1681   }
1682   Mapping = getShadowMapping(TargetTriple, LongSize, CompileKernel);
1683   return true;
1684 }
1685 
1686 bool AddressSanitizer::doFinalization(Module &M) {
1687   GlobalsMD.reset();
1688   return false;
1689 }
1690 
1691 bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
1692   // For each NSObject descendant having a +load method, this method is invoked
1693   // by the ObjC runtime before any of the static constructors is called.
1694   // Therefore we need to instrument such methods with a call to __asan_init
1695   // at the beginning in order to initialize our runtime before any access to
1696   // the shadow memory.
1697   // We cannot just ignore these methods, because they may call other
1698   // instrumented functions.
1699   if (F.getName().find(" load]") != std::string::npos) {
1700     IRBuilder<> IRB(&F.front(), F.front().begin());
1701     IRB.CreateCall(AsanInitFunction, {});
1702     return true;
1703   }
1704   return false;
1705 }
1706 
1707 void AddressSanitizer::markEscapedLocalAllocas(Function &F) {
1708   // Find the one possible call to llvm.localescape and pre-mark allocas passed
1709   // to it as uninteresting. This assumes we haven't started processing allocas
1710   // yet. This check is done up front because iterating the use list in
1711   // isInterestingAlloca would be algorithmically slower.
1712   assert(ProcessedAllocas.empty() && "must process localescape before allocas");
1713 
1714   // Try to get the declaration of llvm.localescape. If it's not in the module,
1715   // we can exit early.
1716   if (!F.getParent()->getFunction("llvm.localescape")) return;
1717 
1718   // Look for a call to llvm.localescape call in the entry block. It can't be in
1719   // any other block.
1720   for (Instruction &I : F.getEntryBlock()) {
1721     IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
1722     if (II && II->getIntrinsicID() == Intrinsic::localescape) {
1723       // We found a call. Mark all the allocas passed in as uninteresting.
1724       for (Value *Arg : II->arg_operands()) {
1725         AllocaInst *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts());
1726         assert(AI && AI->isStaticAlloca() &&
1727                "non-static alloca arg to localescape");
1728         ProcessedAllocas[AI] = false;
1729       }
1730       break;
1731     }
1732   }
1733 }
1734 
1735 bool AddressSanitizer::runOnFunction(Function &F) {
1736   if (&F == AsanCtorFunction) return false;
1737   if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false;
1738   DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
1739   initializeCallbacks(*F.getParent());
1740 
1741   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1742 
1743   // If needed, insert __asan_init before checking for SanitizeAddress attr.
1744   maybeInsertAsanInitAtFunctionEntry(F);
1745 
1746   if (!F.hasFnAttribute(Attribute::SanitizeAddress)) return false;
1747 
1748   if (!ClDebugFunc.empty() && ClDebugFunc != F.getName()) return false;
1749 
1750   FunctionStateRAII CleanupObj(this);
1751 
1752   // We can't instrument allocas used with llvm.localescape. Only static allocas
1753   // can be passed to that intrinsic.
1754   markEscapedLocalAllocas(F);
1755 
1756   // We want to instrument every address only once per basic block (unless there
1757   // are calls between uses).
1758   SmallSet<Value *, 16> TempsToInstrument;
1759   SmallVector<Instruction *, 16> ToInstrument;
1760   SmallVector<Instruction *, 8> NoReturnCalls;
1761   SmallVector<BasicBlock *, 16> AllBlocks;
1762   SmallVector<Instruction *, 16> PointerComparisonsOrSubtracts;
1763   int NumAllocas = 0;
1764   bool IsWrite;
1765   unsigned Alignment;
1766   uint64_t TypeSize;
1767 
1768   // Fill the set of memory operations to instrument.
1769   for (auto &BB : F) {
1770     AllBlocks.push_back(&BB);
1771     TempsToInstrument.clear();
1772     int NumInsnsPerBB = 0;
1773     for (auto &Inst : BB) {
1774       if (LooksLikeCodeInBug11395(&Inst)) return false;
1775       if (Value *Addr = isInterestingMemoryAccess(&Inst, &IsWrite, &TypeSize,
1776                                                   &Alignment)) {
1777         if (ClOpt && ClOptSameTemp) {
1778           if (!TempsToInstrument.insert(Addr).second)
1779             continue;  // We've seen this temp in the current BB.
1780         }
1781       } else if (ClInvalidPointerPairs &&
1782                  isInterestingPointerComparisonOrSubtraction(&Inst)) {
1783         PointerComparisonsOrSubtracts.push_back(&Inst);
1784         continue;
1785       } else if (isa<MemIntrinsic>(Inst)) {
1786         // ok, take it.
1787       } else {
1788         if (isa<AllocaInst>(Inst)) NumAllocas++;
1789         CallSite CS(&Inst);
1790         if (CS) {
1791           // A call inside BB.
1792           TempsToInstrument.clear();
1793           if (CS.doesNotReturn()) NoReturnCalls.push_back(CS.getInstruction());
1794         }
1795         continue;
1796       }
1797       ToInstrument.push_back(&Inst);
1798       NumInsnsPerBB++;
1799       if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB) break;
1800     }
1801   }
1802 
1803   bool UseCalls =
1804       CompileKernel ||
1805       (ClInstrumentationWithCallsThreshold >= 0 &&
1806        ToInstrument.size() > (unsigned)ClInstrumentationWithCallsThreshold);
1807   const TargetLibraryInfo *TLI =
1808       &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1809   const DataLayout &DL = F.getParent()->getDataLayout();
1810   ObjectSizeOffsetVisitor ObjSizeVis(DL, TLI, F.getContext(),
1811                                      /*RoundToAlign=*/true);
1812 
1813   // Instrument.
1814   int NumInstrumented = 0;
1815   for (auto Inst : ToInstrument) {
1816     if (ClDebugMin < 0 || ClDebugMax < 0 ||
1817         (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
1818       if (isInterestingMemoryAccess(Inst, &IsWrite, &TypeSize, &Alignment))
1819         instrumentMop(ObjSizeVis, Inst, UseCalls,
1820                       F.getParent()->getDataLayout());
1821       else
1822         instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
1823     }
1824     NumInstrumented++;
1825   }
1826 
1827   FunctionStackPoisoner FSP(F, *this);
1828   bool ChangedStack = FSP.runOnFunction();
1829 
1830   // We must unpoison the stack before every NoReturn call (throw, _exit, etc).
1831   // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37
1832   for (auto CI : NoReturnCalls) {
1833     IRBuilder<> IRB(CI);
1834     IRB.CreateCall(AsanHandleNoReturnFunc, {});
1835   }
1836 
1837   for (auto Inst : PointerComparisonsOrSubtracts) {
1838     instrumentPointerComparisonOrSubtraction(Inst);
1839     NumInstrumented++;
1840   }
1841 
1842   bool res = NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
1843 
1844   DEBUG(dbgs() << "ASAN done instrumenting: " << res << " " << F << "\n");
1845 
1846   return res;
1847 }
1848 
1849 // Workaround for bug 11395: we don't want to instrument stack in functions
1850 // with large assembly blobs (32-bit only), otherwise reg alloc may crash.
1851 // FIXME: remove once the bug 11395 is fixed.
1852 bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
1853   if (LongSize != 32) return false;
1854   CallInst *CI = dyn_cast<CallInst>(I);
1855   if (!CI || !CI->isInlineAsm()) return false;
1856   if (CI->getNumArgOperands() <= 5) return false;
1857   // We have inline assembly with quite a few arguments.
1858   return true;
1859 }
1860 
1861 void FunctionStackPoisoner::initializeCallbacks(Module &M) {
1862   IRBuilder<> IRB(*C);
1863   for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) {
1864     std::string Suffix = itostr(i);
1865     AsanStackMallocFunc[i] = checkSanitizerInterfaceFunction(
1866         M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy,
1867                               IntptrTy, nullptr));
1868     AsanStackFreeFunc[i] = checkSanitizerInterfaceFunction(
1869         M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix,
1870                               IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1871   }
1872   AsanPoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
1873       M.getOrInsertFunction(kAsanPoisonStackMemoryName, IRB.getVoidTy(),
1874                             IntptrTy, IntptrTy, nullptr));
1875   AsanUnpoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
1876       M.getOrInsertFunction(kAsanUnpoisonStackMemoryName, IRB.getVoidTy(),
1877                             IntptrTy, IntptrTy, nullptr));
1878   AsanAllocaPoisonFunc = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1879       kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1880   AsanAllocasUnpoisonFunc =
1881       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1882           kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1883 }
1884 
1885 void FunctionStackPoisoner::poisonRedZones(ArrayRef<uint8_t> ShadowBytes,
1886                                            IRBuilder<> &IRB, Value *ShadowBase,
1887                                            bool DoPoison) {
1888   size_t n = ShadowBytes.size();
1889   size_t i = 0;
1890   // We need to (un)poison n bytes of stack shadow. Poison as many as we can
1891   // using 64-bit stores (if we are on 64-bit arch), then poison the rest
1892   // with 32-bit stores, then with 16-byte stores, then with 8-byte stores.
1893   for (size_t LargeStoreSizeInBytes = ASan.LongSize / 8;
1894        LargeStoreSizeInBytes != 0; LargeStoreSizeInBytes /= 2) {
1895     for (; i + LargeStoreSizeInBytes - 1 < n; i += LargeStoreSizeInBytes) {
1896       uint64_t Val = 0;
1897       for (size_t j = 0; j < LargeStoreSizeInBytes; j++) {
1898         if (F.getParent()->getDataLayout().isLittleEndian())
1899           Val |= (uint64_t)ShadowBytes[i + j] << (8 * j);
1900         else
1901           Val = (Val << 8) | ShadowBytes[i + j];
1902       }
1903       if (!Val) continue;
1904       Value *Ptr = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
1905       Type *StoreTy = Type::getIntNTy(*C, LargeStoreSizeInBytes * 8);
1906       Value *Poison = ConstantInt::get(StoreTy, DoPoison ? Val : 0);
1907       IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, StoreTy->getPointerTo()));
1908     }
1909   }
1910 }
1911 
1912 // Fake stack allocator (asan_fake_stack.h) has 11 size classes
1913 // for every power of 2 from kMinStackMallocSize to kMaxAsanStackMallocSizeClass
1914 static int StackMallocSizeClass(uint64_t LocalStackSize) {
1915   assert(LocalStackSize <= kMaxStackMallocSize);
1916   uint64_t MaxSize = kMinStackMallocSize;
1917   for (int i = 0;; i++, MaxSize *= 2)
1918     if (LocalStackSize <= MaxSize) return i;
1919   llvm_unreachable("impossible LocalStackSize");
1920 }
1921 
1922 // Set Size bytes starting from ShadowBase to kAsanStackAfterReturnMagic.
1923 // We can not use MemSet intrinsic because it may end up calling the actual
1924 // memset. Size is a multiple of 8.
1925 // Currently this generates 8-byte stores on x86_64; it may be better to
1926 // generate wider stores.
1927 void FunctionStackPoisoner::SetShadowToStackAfterReturnInlined(
1928     IRBuilder<> &IRB, Value *ShadowBase, int Size) {
1929   assert(!(Size % 8));
1930 
1931   // kAsanStackAfterReturnMagic is 0xf5.
1932   const uint64_t kAsanStackAfterReturnMagic64 = 0xf5f5f5f5f5f5f5f5ULL;
1933 
1934   for (int i = 0; i < Size; i += 8) {
1935     Value *p = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
1936     IRB.CreateStore(
1937         ConstantInt::get(IRB.getInt64Ty(), kAsanStackAfterReturnMagic64),
1938         IRB.CreateIntToPtr(p, IRB.getInt64Ty()->getPointerTo()));
1939   }
1940 }
1941 
1942 PHINode *FunctionStackPoisoner::createPHI(IRBuilder<> &IRB, Value *Cond,
1943                                           Value *ValueIfTrue,
1944                                           Instruction *ThenTerm,
1945                                           Value *ValueIfFalse) {
1946   PHINode *PHI = IRB.CreatePHI(IntptrTy, 2);
1947   BasicBlock *CondBlock = cast<Instruction>(Cond)->getParent();
1948   PHI->addIncoming(ValueIfFalse, CondBlock);
1949   BasicBlock *ThenBlock = ThenTerm->getParent();
1950   PHI->addIncoming(ValueIfTrue, ThenBlock);
1951   return PHI;
1952 }
1953 
1954 Value *FunctionStackPoisoner::createAllocaForLayout(
1955     IRBuilder<> &IRB, const ASanStackFrameLayout &L, bool Dynamic) {
1956   AllocaInst *Alloca;
1957   if (Dynamic) {
1958     Alloca = IRB.CreateAlloca(IRB.getInt8Ty(),
1959                               ConstantInt::get(IRB.getInt64Ty(), L.FrameSize),
1960                               "MyAlloca");
1961   } else {
1962     Alloca = IRB.CreateAlloca(ArrayType::get(IRB.getInt8Ty(), L.FrameSize),
1963                               nullptr, "MyAlloca");
1964     assert(Alloca->isStaticAlloca());
1965   }
1966   assert((ClRealignStack & (ClRealignStack - 1)) == 0);
1967   size_t FrameAlignment = std::max(L.FrameAlignment, (size_t)ClRealignStack);
1968   Alloca->setAlignment(FrameAlignment);
1969   return IRB.CreatePointerCast(Alloca, IntptrTy);
1970 }
1971 
1972 void FunctionStackPoisoner::createDynamicAllocasInitStorage() {
1973   BasicBlock &FirstBB = *F.begin();
1974   IRBuilder<> IRB(dyn_cast<Instruction>(FirstBB.begin()));
1975   DynamicAllocaLayout = IRB.CreateAlloca(IntptrTy, nullptr);
1976   IRB.CreateStore(Constant::getNullValue(IntptrTy), DynamicAllocaLayout);
1977   DynamicAllocaLayout->setAlignment(32);
1978 }
1979 
1980 void FunctionStackPoisoner::poisonStack() {
1981   assert(AllocaVec.size() > 0 || DynamicAllocaVec.size() > 0);
1982 
1983   // Insert poison calls for lifetime intrinsics for alloca.
1984   bool HavePoisonedAllocas = false;
1985   for (const auto &APC : AllocaPoisonCallVec) {
1986     assert(APC.InsBefore);
1987     assert(APC.AI);
1988     IRBuilder<> IRB(APC.InsBefore);
1989     poisonAlloca(APC.AI, APC.Size, IRB, APC.DoPoison);
1990     HavePoisonedAllocas |= APC.DoPoison;
1991   }
1992 
1993   if (ClInstrumentAllocas && DynamicAllocaVec.size() > 0) {
1994     // Handle dynamic allocas.
1995     createDynamicAllocasInitStorage();
1996     for (auto &AI : DynamicAllocaVec) handleDynamicAllocaCall(AI);
1997 
1998     unpoisonDynamicAllocas();
1999   }
2000 
2001   if (AllocaVec.empty()) return;
2002 
2003   int StackMallocIdx = -1;
2004   DebugLoc EntryDebugLocation;
2005   if (auto SP = F.getSubprogram())
2006     EntryDebugLocation = DebugLoc::get(SP->getScopeLine(), 0, SP);
2007 
2008   Instruction *InsBefore = AllocaVec[0];
2009   IRBuilder<> IRB(InsBefore);
2010   IRB.SetCurrentDebugLocation(EntryDebugLocation);
2011 
2012   // Make sure non-instrumented allocas stay in the entry block. Otherwise,
2013   // debug info is broken, because only entry-block allocas are treated as
2014   // regular stack slots.
2015   auto InsBeforeB = InsBefore->getParent();
2016   assert(InsBeforeB == &F.getEntryBlock());
2017   for (BasicBlock::iterator I(InsBefore); I != InsBeforeB->end(); ++I)
2018     if (auto *AI = dyn_cast<AllocaInst>(I))
2019       if (NonInstrumentedStaticAllocaVec.count(AI) > 0)
2020         AI->moveBefore(InsBefore);
2021 
2022   // If we have a call to llvm.localescape, keep it in the entry block.
2023   if (LocalEscapeCall) LocalEscapeCall->moveBefore(InsBefore);
2024 
2025   SmallVector<ASanStackVariableDescription, 16> SVD;
2026   SVD.reserve(AllocaVec.size());
2027   for (AllocaInst *AI : AllocaVec) {
2028     ASanStackVariableDescription D = {AI->getName().data(),
2029                                       ASan.getAllocaSizeInBytes(AI),
2030                                       AI->getAlignment(), AI, 0};
2031     SVD.push_back(D);
2032   }
2033   // Minimal header size (left redzone) is 4 pointers,
2034   // i.e. 32 bytes on 64-bit platforms and 16 bytes in 32-bit platforms.
2035   size_t MinHeaderSize = ASan.LongSize / 2;
2036   ASanStackFrameLayout L;
2037   ComputeASanStackFrameLayout(SVD, 1ULL << Mapping.Scale, MinHeaderSize, &L);
2038   DEBUG(dbgs() << L.DescriptionString << " --- " << L.FrameSize << "\n");
2039   uint64_t LocalStackSize = L.FrameSize;
2040   bool DoStackMalloc = ClUseAfterReturn && !ASan.CompileKernel &&
2041                        LocalStackSize <= kMaxStackMallocSize;
2042   bool DoDynamicAlloca = ClDynamicAllocaStack;
2043   // Don't do dynamic alloca or stack malloc if:
2044   // 1) There is inline asm: too often it makes assumptions on which registers
2045   //    are available.
2046   // 2) There is a returns_twice call (typically setjmp), which is
2047   //    optimization-hostile, and doesn't play well with introduced indirect
2048   //    register-relative calculation of local variable addresses.
2049   DoDynamicAlloca &= !HasNonEmptyInlineAsm && !HasReturnsTwiceCall;
2050   DoStackMalloc &= !HasNonEmptyInlineAsm && !HasReturnsTwiceCall;
2051 
2052   Value *StaticAlloca =
2053       DoDynamicAlloca ? nullptr : createAllocaForLayout(IRB, L, false);
2054 
2055   Value *FakeStack;
2056   Value *LocalStackBase;
2057 
2058   if (DoStackMalloc) {
2059     // void *FakeStack = __asan_option_detect_stack_use_after_return
2060     //     ? __asan_stack_malloc_N(LocalStackSize)
2061     //     : nullptr;
2062     // void *LocalStackBase = (FakeStack) ? FakeStack : alloca(LocalStackSize);
2063     Constant *OptionDetectUAR = F.getParent()->getOrInsertGlobal(
2064         kAsanOptionDetectUAR, IRB.getInt32Ty());
2065     Value *UARIsEnabled =
2066         IRB.CreateICmpNE(IRB.CreateLoad(OptionDetectUAR),
2067                          Constant::getNullValue(IRB.getInt32Ty()));
2068     Instruction *Term =
2069         SplitBlockAndInsertIfThen(UARIsEnabled, InsBefore, false);
2070     IRBuilder<> IRBIf(Term);
2071     IRBIf.SetCurrentDebugLocation(EntryDebugLocation);
2072     StackMallocIdx = StackMallocSizeClass(LocalStackSize);
2073     assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass);
2074     Value *FakeStackValue =
2075         IRBIf.CreateCall(AsanStackMallocFunc[StackMallocIdx],
2076                          ConstantInt::get(IntptrTy, LocalStackSize));
2077     IRB.SetInsertPoint(InsBefore);
2078     IRB.SetCurrentDebugLocation(EntryDebugLocation);
2079     FakeStack = createPHI(IRB, UARIsEnabled, FakeStackValue, Term,
2080                           ConstantInt::get(IntptrTy, 0));
2081 
2082     Value *NoFakeStack =
2083         IRB.CreateICmpEQ(FakeStack, Constant::getNullValue(IntptrTy));
2084     Term = SplitBlockAndInsertIfThen(NoFakeStack, InsBefore, false);
2085     IRBIf.SetInsertPoint(Term);
2086     IRBIf.SetCurrentDebugLocation(EntryDebugLocation);
2087     Value *AllocaValue =
2088         DoDynamicAlloca ? createAllocaForLayout(IRBIf, L, true) : StaticAlloca;
2089     IRB.SetInsertPoint(InsBefore);
2090     IRB.SetCurrentDebugLocation(EntryDebugLocation);
2091     LocalStackBase = createPHI(IRB, NoFakeStack, AllocaValue, Term, FakeStack);
2092   } else {
2093     // void *FakeStack = nullptr;
2094     // void *LocalStackBase = alloca(LocalStackSize);
2095     FakeStack = ConstantInt::get(IntptrTy, 0);
2096     LocalStackBase =
2097         DoDynamicAlloca ? createAllocaForLayout(IRB, L, true) : StaticAlloca;
2098   }
2099 
2100   // Replace Alloca instructions with base+offset.
2101   for (const auto &Desc : SVD) {
2102     AllocaInst *AI = Desc.AI;
2103     Value *NewAllocaPtr = IRB.CreateIntToPtr(
2104         IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Desc.Offset)),
2105         AI->getType());
2106     replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB, /*Deref=*/true);
2107     AI->replaceAllUsesWith(NewAllocaPtr);
2108   }
2109 
2110   // The left-most redzone has enough space for at least 4 pointers.
2111   // Write the Magic value to redzone[0].
2112   Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
2113   IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
2114                   BasePlus0);
2115   // Write the frame description constant to redzone[1].
2116   Value *BasePlus1 = IRB.CreateIntToPtr(
2117       IRB.CreateAdd(LocalStackBase,
2118                     ConstantInt::get(IntptrTy, ASan.LongSize / 8)),
2119       IntptrPtrTy);
2120   GlobalVariable *StackDescriptionGlobal =
2121       createPrivateGlobalForString(*F.getParent(), L.DescriptionString,
2122                                    /*AllowMerging*/ true);
2123   Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal, IntptrTy);
2124   IRB.CreateStore(Description, BasePlus1);
2125   // Write the PC to redzone[2].
2126   Value *BasePlus2 = IRB.CreateIntToPtr(
2127       IRB.CreateAdd(LocalStackBase,
2128                     ConstantInt::get(IntptrTy, 2 * ASan.LongSize / 8)),
2129       IntptrPtrTy);
2130   IRB.CreateStore(IRB.CreatePointerCast(&F, IntptrTy), BasePlus2);
2131 
2132   // Poison the stack redzones at the entry.
2133   Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB);
2134   poisonRedZones(L.ShadowBytes, IRB, ShadowBase, true);
2135 
2136   // (Un)poison the stack before all ret instructions.
2137   for (auto Ret : RetVec) {
2138     IRBuilder<> IRBRet(Ret);
2139     // Mark the current frame as retired.
2140     IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
2141                        BasePlus0);
2142     if (DoStackMalloc) {
2143       assert(StackMallocIdx >= 0);
2144       // if FakeStack != 0  // LocalStackBase == FakeStack
2145       //     // In use-after-return mode, poison the whole stack frame.
2146       //     if StackMallocIdx <= 4
2147       //         // For small sizes inline the whole thing:
2148       //         memset(ShadowBase, kAsanStackAfterReturnMagic, ShadowSize);
2149       //         **SavedFlagPtr(FakeStack) = 0
2150       //     else
2151       //         __asan_stack_free_N(FakeStack, LocalStackSize)
2152       // else
2153       //     <This is not a fake stack; unpoison the redzones>
2154       Value *Cmp =
2155           IRBRet.CreateICmpNE(FakeStack, Constant::getNullValue(IntptrTy));
2156       TerminatorInst *ThenTerm, *ElseTerm;
2157       SplitBlockAndInsertIfThenElse(Cmp, Ret, &ThenTerm, &ElseTerm);
2158 
2159       IRBuilder<> IRBPoison(ThenTerm);
2160       if (StackMallocIdx <= 4) {
2161         int ClassSize = kMinStackMallocSize << StackMallocIdx;
2162         SetShadowToStackAfterReturnInlined(IRBPoison, ShadowBase,
2163                                            ClassSize >> Mapping.Scale);
2164         Value *SavedFlagPtrPtr = IRBPoison.CreateAdd(
2165             FakeStack,
2166             ConstantInt::get(IntptrTy, ClassSize - ASan.LongSize / 8));
2167         Value *SavedFlagPtr = IRBPoison.CreateLoad(
2168             IRBPoison.CreateIntToPtr(SavedFlagPtrPtr, IntptrPtrTy));
2169         IRBPoison.CreateStore(
2170             Constant::getNullValue(IRBPoison.getInt8Ty()),
2171             IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getInt8PtrTy()));
2172       } else {
2173         // For larger frames call __asan_stack_free_*.
2174         IRBPoison.CreateCall(
2175             AsanStackFreeFunc[StackMallocIdx],
2176             {FakeStack, ConstantInt::get(IntptrTy, LocalStackSize)});
2177       }
2178 
2179       IRBuilder<> IRBElse(ElseTerm);
2180       poisonRedZones(L.ShadowBytes, IRBElse, ShadowBase, false);
2181     } else if (HavePoisonedAllocas) {
2182       // If we poisoned some allocas in llvm.lifetime analysis,
2183       // unpoison whole stack frame now.
2184       poisonAlloca(LocalStackBase, LocalStackSize, IRBRet, false);
2185     } else {
2186       poisonRedZones(L.ShadowBytes, IRBRet, ShadowBase, false);
2187     }
2188   }
2189 
2190   // We are done. Remove the old unused alloca instructions.
2191   for (auto AI : AllocaVec) AI->eraseFromParent();
2192 }
2193 
2194 void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size,
2195                                          IRBuilder<> &IRB, bool DoPoison) {
2196   // For now just insert the call to ASan runtime.
2197   Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy);
2198   Value *SizeArg = ConstantInt::get(IntptrTy, Size);
2199   IRB.CreateCall(
2200       DoPoison ? AsanPoisonStackMemoryFunc : AsanUnpoisonStackMemoryFunc,
2201       {AddrArg, SizeArg});
2202 }
2203 
2204 // Handling llvm.lifetime intrinsics for a given %alloca:
2205 // (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca.
2206 // (2) if %size is constant, poison memory for llvm.lifetime.end (to detect
2207 //     invalid accesses) and unpoison it for llvm.lifetime.start (the memory
2208 //     could be poisoned by previous llvm.lifetime.end instruction, as the
2209 //     variable may go in and out of scope several times, e.g. in loops).
2210 // (3) if we poisoned at least one %alloca in a function,
2211 //     unpoison the whole stack frame at function exit.
2212 
2213 AllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) {
2214   if (AllocaInst *AI = dyn_cast<AllocaInst>(V))
2215     // We're intested only in allocas we can handle.
2216     return ASan.isInterestingAlloca(*AI) ? AI : nullptr;
2217   // See if we've already calculated (or started to calculate) alloca for a
2218   // given value.
2219   AllocaForValueMapTy::iterator I = AllocaForValue.find(V);
2220   if (I != AllocaForValue.end()) return I->second;
2221   // Store 0 while we're calculating alloca for value V to avoid
2222   // infinite recursion if the value references itself.
2223   AllocaForValue[V] = nullptr;
2224   AllocaInst *Res = nullptr;
2225   if (CastInst *CI = dyn_cast<CastInst>(V))
2226     Res = findAllocaForValue(CI->getOperand(0));
2227   else if (PHINode *PN = dyn_cast<PHINode>(V)) {
2228     for (Value *IncValue : PN->incoming_values()) {
2229       // Allow self-referencing phi-nodes.
2230       if (IncValue == PN) continue;
2231       AllocaInst *IncValueAI = findAllocaForValue(IncValue);
2232       // AI for incoming values should exist and should all be equal.
2233       if (IncValueAI == nullptr || (Res != nullptr && IncValueAI != Res))
2234         return nullptr;
2235       Res = IncValueAI;
2236     }
2237   }
2238   if (Res) AllocaForValue[V] = Res;
2239   return Res;
2240 }
2241 
2242 void FunctionStackPoisoner::handleDynamicAllocaCall(AllocaInst *AI) {
2243   IRBuilder<> IRB(AI);
2244 
2245   const unsigned Align = std::max(kAllocaRzSize, AI->getAlignment());
2246   const uint64_t AllocaRedzoneMask = kAllocaRzSize - 1;
2247 
2248   Value *Zero = Constant::getNullValue(IntptrTy);
2249   Value *AllocaRzSize = ConstantInt::get(IntptrTy, kAllocaRzSize);
2250   Value *AllocaRzMask = ConstantInt::get(IntptrTy, AllocaRedzoneMask);
2251 
2252   // Since we need to extend alloca with additional memory to locate
2253   // redzones, and OldSize is number of allocated blocks with
2254   // ElementSize size, get allocated memory size in bytes by
2255   // OldSize * ElementSize.
2256   const unsigned ElementSize =
2257       F.getParent()->getDataLayout().getTypeAllocSize(AI->getAllocatedType());
2258   Value *OldSize =
2259       IRB.CreateMul(IRB.CreateIntCast(AI->getArraySize(), IntptrTy, false),
2260                     ConstantInt::get(IntptrTy, ElementSize));
2261 
2262   // PartialSize = OldSize % 32
2263   Value *PartialSize = IRB.CreateAnd(OldSize, AllocaRzMask);
2264 
2265   // Misalign = kAllocaRzSize - PartialSize;
2266   Value *Misalign = IRB.CreateSub(AllocaRzSize, PartialSize);
2267 
2268   // PartialPadding = Misalign != kAllocaRzSize ? Misalign : 0;
2269   Value *Cond = IRB.CreateICmpNE(Misalign, AllocaRzSize);
2270   Value *PartialPadding = IRB.CreateSelect(Cond, Misalign, Zero);
2271 
2272   // AdditionalChunkSize = Align + PartialPadding + kAllocaRzSize
2273   // Align is added to locate left redzone, PartialPadding for possible
2274   // partial redzone and kAllocaRzSize for right redzone respectively.
2275   Value *AdditionalChunkSize = IRB.CreateAdd(
2276       ConstantInt::get(IntptrTy, Align + kAllocaRzSize), PartialPadding);
2277 
2278   Value *NewSize = IRB.CreateAdd(OldSize, AdditionalChunkSize);
2279 
2280   // Insert new alloca with new NewSize and Align params.
2281   AllocaInst *NewAlloca = IRB.CreateAlloca(IRB.getInt8Ty(), NewSize);
2282   NewAlloca->setAlignment(Align);
2283 
2284   // NewAddress = Address + Align
2285   Value *NewAddress = IRB.CreateAdd(IRB.CreatePtrToInt(NewAlloca, IntptrTy),
2286                                     ConstantInt::get(IntptrTy, Align));
2287 
2288   // Insert __asan_alloca_poison call for new created alloca.
2289   IRB.CreateCall(AsanAllocaPoisonFunc, {NewAddress, OldSize});
2290 
2291   // Store the last alloca's address to DynamicAllocaLayout. We'll need this
2292   // for unpoisoning stuff.
2293   IRB.CreateStore(IRB.CreatePtrToInt(NewAlloca, IntptrTy), DynamicAllocaLayout);
2294 
2295   Value *NewAddressPtr = IRB.CreateIntToPtr(NewAddress, AI->getType());
2296 
2297   // Replace all uses of AddessReturnedByAlloca with NewAddressPtr.
2298   AI->replaceAllUsesWith(NewAddressPtr);
2299 
2300   // We are done. Erase old alloca from parent.
2301   AI->eraseFromParent();
2302 }
2303 
2304 // isSafeAccess returns true if Addr is always inbounds with respect to its
2305 // base object. For example, it is a field access or an array access with
2306 // constant inbounds index.
2307 bool AddressSanitizer::isSafeAccess(ObjectSizeOffsetVisitor &ObjSizeVis,
2308                                     Value *Addr, uint64_t TypeSize) const {
2309   SizeOffsetType SizeOffset = ObjSizeVis.compute(Addr);
2310   if (!ObjSizeVis.bothKnown(SizeOffset)) return false;
2311   uint64_t Size = SizeOffset.first.getZExtValue();
2312   int64_t Offset = SizeOffset.second.getSExtValue();
2313   // Three checks are required to ensure safety:
2314   // . Offset >= 0  (since the offset is given from the base ptr)
2315   // . Size >= Offset  (unsigned)
2316   // . Size - Offset >= NeededSize  (unsigned)
2317   return Offset >= 0 && Size >= uint64_t(Offset) &&
2318          Size - uint64_t(Offset) >= TypeSize / 8;
2319 }
2320