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