1 //===- AddressSanitizer.cpp - memory error detector -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Details of the algorithm:
11 //  https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/DepthFirstIterator.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Analysis/MemoryBuiltins.h"
27 #include "llvm/Analysis/TargetLibraryInfo.h"
28 #include "llvm/Analysis/ValueTracking.h"
29 #include "llvm/BinaryFormat/MachO.h"
30 #include "llvm/IR/Argument.h"
31 #include "llvm/IR/Attributes.h"
32 #include "llvm/IR/BasicBlock.h"
33 #include "llvm/IR/CallSite.h"
34 #include "llvm/IR/Comdat.h"
35 #include "llvm/IR/Constant.h"
36 #include "llvm/IR/Constants.h"
37 #include "llvm/IR/DIBuilder.h"
38 #include "llvm/IR/DataLayout.h"
39 #include "llvm/IR/DebugInfoMetadata.h"
40 #include "llvm/IR/DebugLoc.h"
41 #include "llvm/IR/DerivedTypes.h"
42 #include "llvm/IR/Dominators.h"
43 #include "llvm/IR/Function.h"
44 #include "llvm/IR/GlobalAlias.h"
45 #include "llvm/IR/GlobalValue.h"
46 #include "llvm/IR/GlobalVariable.h"
47 #include "llvm/IR/IRBuilder.h"
48 #include "llvm/IR/InlineAsm.h"
49 #include "llvm/IR/InstVisitor.h"
50 #include "llvm/IR/InstrTypes.h"
51 #include "llvm/IR/Instruction.h"
52 #include "llvm/IR/Instructions.h"
53 #include "llvm/IR/IntrinsicInst.h"
54 #include "llvm/IR/Intrinsics.h"
55 #include "llvm/IR/LLVMContext.h"
56 #include "llvm/IR/MDBuilder.h"
57 #include "llvm/IR/Metadata.h"
58 #include "llvm/IR/Module.h"
59 #include "llvm/IR/Type.h"
60 #include "llvm/IR/Use.h"
61 #include "llvm/IR/Value.h"
62 #include "llvm/MC/MCSectionMachO.h"
63 #include "llvm/Pass.h"
64 #include "llvm/Support/Casting.h"
65 #include "llvm/Support/CommandLine.h"
66 #include "llvm/Support/Debug.h"
67 #include "llvm/Support/ErrorHandling.h"
68 #include "llvm/Support/MathExtras.h"
69 #include "llvm/Support/ScopedPrinter.h"
70 #include "llvm/Support/raw_ostream.h"
71 #include "llvm/Transforms/Instrumentation.h"
72 #include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
73 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
74 #include "llvm/Transforms/Utils/Local.h"
75 #include "llvm/Transforms/Utils/ModuleUtils.h"
76 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
77 #include <algorithm>
78 #include <cassert>
79 #include <cstddef>
80 #include <cstdint>
81 #include <iomanip>
82 #include <limits>
83 #include <memory>
84 #include <sstream>
85 #include <string>
86 #include <tuple>
87 
88 using namespace llvm;
89 
90 #define DEBUG_TYPE "asan"
91 
92 static const uint64_t kDefaultShadowScale = 3;
93 static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
94 static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
95 static const uint64_t kDynamicShadowSentinel =
96     std::numeric_limits<uint64_t>::max();
97 static const uint64_t kSmallX86_64ShadowOffsetBase = 0x7FFFFFFF;  // < 2G.
98 static const uint64_t kSmallX86_64ShadowOffsetAlignMask = ~0xFFFULL;
99 static const uint64_t kLinuxKasan_ShadowOffset64 = 0xdffffc0000000000;
100 static const uint64_t kPPC64_ShadowOffset64 = 1ULL << 44;
101 static const uint64_t kSystemZ_ShadowOffset64 = 1ULL << 52;
102 static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa0000;
103 static const uint64_t kMIPS64_ShadowOffset64 = 1ULL << 37;
104 static const uint64_t kAArch64_ShadowOffset64 = 1ULL << 36;
105 static const uint64_t kFreeBSD_ShadowOffset32 = 1ULL << 30;
106 static const uint64_t kFreeBSD_ShadowOffset64 = 1ULL << 46;
107 static const uint64_t kNetBSD_ShadowOffset32 = 1ULL << 30;
108 static const uint64_t kNetBSD_ShadowOffset64 = 1ULL << 46;
109 static const uint64_t kNetBSDKasan_ShadowOffset64 = 0xdfff900000000000;
110 static const uint64_t kPS4CPU_ShadowOffset64 = 1ULL << 40;
111 static const uint64_t kWindowsShadowOffset32 = 3ULL << 28;
112 static const uint64_t kEmscriptenShadowOffset = 0;
113 
114 static const uint64_t kMyriadShadowScale = 5;
115 static const uint64_t kMyriadMemoryOffset32 = 0x80000000ULL;
116 static const uint64_t kMyriadMemorySize32 = 0x20000000ULL;
117 static const uint64_t kMyriadTagShift = 29;
118 static const uint64_t kMyriadDDRTag = 4;
119 static const uint64_t kMyriadCacheBitMask32 = 0x40000000ULL;
120 
121 // The shadow memory space is dynamically allocated.
122 static const uint64_t kWindowsShadowOffset64 = kDynamicShadowSentinel;
123 
124 static const size_t kMinStackMallocSize = 1 << 6;   // 64B
125 static const size_t kMaxStackMallocSize = 1 << 16;  // 64K
126 static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
127 static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
128 
129 static const char *const kAsanModuleCtorName = "asan.module_ctor";
130 static const char *const kAsanModuleDtorName = "asan.module_dtor";
131 static const uint64_t kAsanCtorAndDtorPriority = 1;
132 // On Emscripten, the system needs more than one priorities for constructors.
133 static const uint64_t kAsanEmscriptenCtorAndDtorPriority = 50;
134 static const char *const kAsanReportErrorTemplate = "__asan_report_";
135 static const char *const kAsanRegisterGlobalsName = "__asan_register_globals";
136 static const char *const kAsanUnregisterGlobalsName =
137     "__asan_unregister_globals";
138 static const char *const kAsanRegisterImageGlobalsName =
139   "__asan_register_image_globals";
140 static const char *const kAsanUnregisterImageGlobalsName =
141   "__asan_unregister_image_globals";
142 static const char *const kAsanRegisterElfGlobalsName =
143   "__asan_register_elf_globals";
144 static const char *const kAsanUnregisterElfGlobalsName =
145   "__asan_unregister_elf_globals";
146 static const char *const kAsanPoisonGlobalsName = "__asan_before_dynamic_init";
147 static const char *const kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init";
148 static const char *const kAsanInitName = "__asan_init";
149 static const char *const kAsanVersionCheckNamePrefix =
150     "__asan_version_mismatch_check_v";
151 static const char *const kAsanPtrCmp = "__sanitizer_ptr_cmp";
152 static const char *const kAsanPtrSub = "__sanitizer_ptr_sub";
153 static const char *const kAsanHandleNoReturnName = "__asan_handle_no_return";
154 static const int kMaxAsanStackMallocSizeClass = 10;
155 static const char *const kAsanStackMallocNameTemplate = "__asan_stack_malloc_";
156 static const char *const kAsanStackFreeNameTemplate = "__asan_stack_free_";
157 static const char *const kAsanGenPrefix = "___asan_gen_";
158 static const char *const kODRGenPrefix = "__odr_asan_gen_";
159 static const char *const kSanCovGenPrefix = "__sancov_gen_";
160 static const char *const kAsanSetShadowPrefix = "__asan_set_shadow_";
161 static const char *const kAsanPoisonStackMemoryName =
162     "__asan_poison_stack_memory";
163 static const char *const kAsanUnpoisonStackMemoryName =
164     "__asan_unpoison_stack_memory";
165 
166 // ASan version script has __asan_* wildcard. Triple underscore prevents a
167 // linker (gold) warning about attempting to export a local symbol.
168 static const char *const kAsanGlobalsRegisteredFlagName =
169     "___asan_globals_registered";
170 
171 static const char *const kAsanOptionDetectUseAfterReturn =
172     "__asan_option_detect_stack_use_after_return";
173 
174 static const char *const kAsanShadowMemoryDynamicAddress =
175     "__asan_shadow_memory_dynamic_address";
176 
177 static const char *const kAsanAllocaPoison = "__asan_alloca_poison";
178 static const char *const kAsanAllocasUnpoison = "__asan_allocas_unpoison";
179 
180 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
181 static const size_t kNumberOfAccessSizes = 5;
182 
183 static const unsigned kAllocaRzSize = 32;
184 
185 // Command-line flags.
186 
187 static cl::opt<bool> ClEnableKasan(
188     "asan-kernel", cl::desc("Enable KernelAddressSanitizer instrumentation"),
189     cl::Hidden, cl::init(false));
190 
191 static cl::opt<bool> ClRecover(
192     "asan-recover",
193     cl::desc("Enable recovery mode (continue-after-error)."),
194     cl::Hidden, cl::init(false));
195 
196 // This flag may need to be replaced with -f[no-]asan-reads.
197 static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
198                                        cl::desc("instrument read instructions"),
199                                        cl::Hidden, cl::init(true));
200 
201 static cl::opt<bool> ClInstrumentWrites(
202     "asan-instrument-writes", cl::desc("instrument write instructions"),
203     cl::Hidden, cl::init(true));
204 
205 static cl::opt<bool> ClInstrumentAtomics(
206     "asan-instrument-atomics",
207     cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden,
208     cl::init(true));
209 
210 static cl::opt<bool> ClAlwaysSlowPath(
211     "asan-always-slow-path",
212     cl::desc("use instrumentation with slow path for all accesses"), cl::Hidden,
213     cl::init(false));
214 
215 static cl::opt<bool> ClForceDynamicShadow(
216     "asan-force-dynamic-shadow",
217     cl::desc("Load shadow address into a local variable for each function"),
218     cl::Hidden, cl::init(false));
219 
220 static cl::opt<bool>
221     ClWithIfunc("asan-with-ifunc",
222                 cl::desc("Access dynamic shadow through an ifunc global on "
223                          "platforms that support this"),
224                 cl::Hidden, cl::init(true));
225 
226 static cl::opt<bool> ClWithIfuncSuppressRemat(
227     "asan-with-ifunc-suppress-remat",
228     cl::desc("Suppress rematerialization of dynamic shadow address by passing "
229              "it through inline asm in prologue."),
230     cl::Hidden, cl::init(true));
231 
232 // This flag limits the number of instructions to be instrumented
233 // in any given BB. Normally, this should be set to unlimited (INT_MAX),
234 // but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
235 // set it to 10000.
236 static cl::opt<int> ClMaxInsnsToInstrumentPerBB(
237     "asan-max-ins-per-bb", cl::init(10000),
238     cl::desc("maximal number of instructions to instrument in any given BB"),
239     cl::Hidden);
240 
241 // This flag may need to be replaced with -f[no]asan-stack.
242 static cl::opt<bool> ClStack("asan-stack", cl::desc("Handle stack memory"),
243                              cl::Hidden, cl::init(true));
244 static cl::opt<uint32_t> ClMaxInlinePoisoningSize(
245     "asan-max-inline-poisoning-size",
246     cl::desc(
247         "Inline shadow poisoning for blocks up to the given size in bytes."),
248     cl::Hidden, cl::init(64));
249 
250 static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
251                                       cl::desc("Check stack-use-after-return"),
252                                       cl::Hidden, cl::init(true));
253 
254 static cl::opt<bool> ClRedzoneByvalArgs("asan-redzone-byval-args",
255                                         cl::desc("Create redzones for byval "
256                                                  "arguments (extra copy "
257                                                  "required)"), cl::Hidden,
258                                         cl::init(true));
259 
260 static cl::opt<bool> ClUseAfterScope("asan-use-after-scope",
261                                      cl::desc("Check stack-use-after-scope"),
262                                      cl::Hidden, cl::init(false));
263 
264 // This flag may need to be replaced with -f[no]asan-globals.
265 static cl::opt<bool> ClGlobals("asan-globals",
266                                cl::desc("Handle global objects"), cl::Hidden,
267                                cl::init(true));
268 
269 static cl::opt<bool> ClInitializers("asan-initialization-order",
270                                     cl::desc("Handle C++ initializer order"),
271                                     cl::Hidden, cl::init(true));
272 
273 static cl::opt<bool> ClInvalidPointerPairs(
274     "asan-detect-invalid-pointer-pair",
275     cl::desc("Instrument <, <=, >, >=, - with pointer operands"), cl::Hidden,
276     cl::init(false));
277 
278 static cl::opt<bool> ClInvalidPointerCmp(
279     "asan-detect-invalid-pointer-cmp",
280     cl::desc("Instrument <, <=, >, >= with pointer operands"), cl::Hidden,
281     cl::init(false));
282 
283 static cl::opt<bool> ClInvalidPointerSub(
284     "asan-detect-invalid-pointer-sub",
285     cl::desc("Instrument - operations with pointer operands"), cl::Hidden,
286     cl::init(false));
287 
288 static cl::opt<unsigned> ClRealignStack(
289     "asan-realign-stack",
290     cl::desc("Realign stack to the value of this flag (power of two)"),
291     cl::Hidden, cl::init(32));
292 
293 static cl::opt<int> ClInstrumentationWithCallsThreshold(
294     "asan-instrumentation-with-call-threshold",
295     cl::desc(
296         "If the function being instrumented contains more than "
297         "this number of memory accesses, use callbacks instead of "
298         "inline checks (-1 means never use callbacks)."),
299     cl::Hidden, cl::init(7000));
300 
301 static cl::opt<std::string> ClMemoryAccessCallbackPrefix(
302     "asan-memory-access-callback-prefix",
303     cl::desc("Prefix for memory access callbacks"), cl::Hidden,
304     cl::init("__asan_"));
305 
306 static cl::opt<bool>
307     ClInstrumentDynamicAllocas("asan-instrument-dynamic-allocas",
308                                cl::desc("instrument dynamic allocas"),
309                                cl::Hidden, cl::init(true));
310 
311 static cl::opt<bool> ClSkipPromotableAllocas(
312     "asan-skip-promotable-allocas",
313     cl::desc("Do not instrument promotable allocas"), cl::Hidden,
314     cl::init(true));
315 
316 // These flags allow to change the shadow mapping.
317 // The shadow mapping looks like
318 //    Shadow = (Mem >> scale) + offset
319 
320 static cl::opt<int> ClMappingScale("asan-mapping-scale",
321                                    cl::desc("scale of asan shadow mapping"),
322                                    cl::Hidden, cl::init(0));
323 
324 static cl::opt<uint64_t>
325     ClMappingOffset("asan-mapping-offset",
326                     cl::desc("offset of asan shadow mapping [EXPERIMENTAL]"),
327                     cl::Hidden, cl::init(0));
328 
329 // Optimization flags. Not user visible, used mostly for testing
330 // and benchmarking the tool.
331 
332 static cl::opt<bool> ClOpt("asan-opt", cl::desc("Optimize instrumentation"),
333                            cl::Hidden, cl::init(true));
334 
335 static cl::opt<bool> ClOptSameTemp(
336     "asan-opt-same-temp", cl::desc("Instrument the same temp just once"),
337     cl::Hidden, cl::init(true));
338 
339 static cl::opt<bool> ClOptGlobals("asan-opt-globals",
340                                   cl::desc("Don't instrument scalar globals"),
341                                   cl::Hidden, cl::init(true));
342 
343 static cl::opt<bool> ClOptStack(
344     "asan-opt-stack", cl::desc("Don't instrument scalar stack variables"),
345     cl::Hidden, cl::init(false));
346 
347 static cl::opt<bool> ClDynamicAllocaStack(
348     "asan-stack-dynamic-alloca",
349     cl::desc("Use dynamic alloca to represent stack variables"), cl::Hidden,
350     cl::init(true));
351 
352 static cl::opt<uint32_t> ClForceExperiment(
353     "asan-force-experiment",
354     cl::desc("Force optimization experiment (for testing)"), cl::Hidden,
355     cl::init(0));
356 
357 static cl::opt<bool>
358     ClUsePrivateAlias("asan-use-private-alias",
359                       cl::desc("Use private aliases for global variables"),
360                       cl::Hidden, cl::init(false));
361 
362 static cl::opt<bool>
363     ClUseOdrIndicator("asan-use-odr-indicator",
364                       cl::desc("Use odr indicators to improve ODR reporting"),
365                       cl::Hidden, cl::init(false));
366 
367 static cl::opt<bool>
368     ClUseGlobalsGC("asan-globals-live-support",
369                    cl::desc("Use linker features to support dead "
370                             "code stripping of globals"),
371                    cl::Hidden, cl::init(true));
372 
373 // This is on by default even though there is a bug in gold:
374 // https://sourceware.org/bugzilla/show_bug.cgi?id=19002
375 static cl::opt<bool>
376     ClWithComdat("asan-with-comdat",
377                  cl::desc("Place ASan constructors in comdat sections"),
378                  cl::Hidden, cl::init(true));
379 
380 // Debug flags.
381 
382 static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
383                             cl::init(0));
384 
385 static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
386                                  cl::Hidden, cl::init(0));
387 
388 static cl::opt<std::string> ClDebugFunc("asan-debug-func", cl::Hidden,
389                                         cl::desc("Debug func"));
390 
391 static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
392                                cl::Hidden, cl::init(-1));
393 
394 static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug max inst"),
395                                cl::Hidden, cl::init(-1));
396 
397 STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
398 STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
399 STATISTIC(NumOptimizedAccessesToGlobalVar,
400           "Number of optimized accesses to global vars");
401 STATISTIC(NumOptimizedAccessesToStackVar,
402           "Number of optimized accesses to stack vars");
403 
404 namespace {
405 
406 /// This struct defines the shadow mapping using the rule:
407 ///   shadow = (mem >> Scale) ADD-or-OR Offset.
408 /// If InGlobal is true, then
409 ///   extern char __asan_shadow[];
410 ///   shadow = (mem >> Scale) + &__asan_shadow
411 struct ShadowMapping {
412   int Scale;
413   uint64_t Offset;
414   bool OrShadowOffset;
415   bool InGlobal;
416 };
417 
418 } // end anonymous namespace
419 
420 static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize,
421                                       bool IsKasan) {
422   bool IsAndroid = TargetTriple.isAndroid();
423   bool IsIOS = TargetTriple.isiOS() || TargetTriple.isWatchOS();
424   bool IsFreeBSD = TargetTriple.isOSFreeBSD();
425   bool IsNetBSD = TargetTriple.isOSNetBSD();
426   bool IsPS4CPU = TargetTriple.isPS4CPU();
427   bool IsLinux = TargetTriple.isOSLinux();
428   bool IsPPC64 = TargetTriple.getArch() == Triple::ppc64 ||
429                  TargetTriple.getArch() == Triple::ppc64le;
430   bool IsSystemZ = TargetTriple.getArch() == Triple::systemz;
431   bool IsX86_64 = TargetTriple.getArch() == Triple::x86_64;
432   bool IsMIPS32 = TargetTriple.isMIPS32();
433   bool IsMIPS64 = TargetTriple.isMIPS64();
434   bool IsArmOrThumb = TargetTriple.isARM() || TargetTriple.isThumb();
435   bool IsAArch64 = TargetTriple.getArch() == Triple::aarch64;
436   bool IsWindows = TargetTriple.isOSWindows();
437   bool IsFuchsia = TargetTriple.isOSFuchsia();
438   bool IsMyriad = TargetTriple.getVendor() == llvm::Triple::Myriad;
439   bool IsEmscripten = TargetTriple.isOSEmscripten();
440 
441   ShadowMapping Mapping;
442 
443   Mapping.Scale = IsMyriad ? kMyriadShadowScale : kDefaultShadowScale;
444   if (ClMappingScale.getNumOccurrences() > 0) {
445     Mapping.Scale = ClMappingScale;
446   }
447 
448   if (LongSize == 32) {
449     if (IsAndroid)
450       Mapping.Offset = kDynamicShadowSentinel;
451     else if (IsMIPS32)
452       Mapping.Offset = kMIPS32_ShadowOffset32;
453     else if (IsFreeBSD)
454       Mapping.Offset = kFreeBSD_ShadowOffset32;
455     else if (IsNetBSD)
456       Mapping.Offset = kNetBSD_ShadowOffset32;
457     else if (IsIOS)
458       Mapping.Offset = kDynamicShadowSentinel;
459     else if (IsWindows)
460       Mapping.Offset = kWindowsShadowOffset32;
461     else if (IsEmscripten)
462       Mapping.Offset = kEmscriptenShadowOffset;
463     else if (IsMyriad) {
464       uint64_t ShadowOffset = (kMyriadMemoryOffset32 + kMyriadMemorySize32 -
465                                (kMyriadMemorySize32 >> Mapping.Scale));
466       Mapping.Offset = ShadowOffset - (kMyriadMemoryOffset32 >> Mapping.Scale);
467     }
468     else
469       Mapping.Offset = kDefaultShadowOffset32;
470   } else {  // LongSize == 64
471     // Fuchsia is always PIE, which means that the beginning of the address
472     // space is always available.
473     if (IsFuchsia)
474       Mapping.Offset = 0;
475     else if (IsPPC64)
476       Mapping.Offset = kPPC64_ShadowOffset64;
477     else if (IsSystemZ)
478       Mapping.Offset = kSystemZ_ShadowOffset64;
479     else if (IsFreeBSD && !IsMIPS64)
480       Mapping.Offset = kFreeBSD_ShadowOffset64;
481     else if (IsNetBSD) {
482       if (IsKasan)
483         Mapping.Offset = kNetBSDKasan_ShadowOffset64;
484       else
485         Mapping.Offset = kNetBSD_ShadowOffset64;
486     } else if (IsPS4CPU)
487       Mapping.Offset = kPS4CPU_ShadowOffset64;
488     else if (IsLinux && IsX86_64) {
489       if (IsKasan)
490         Mapping.Offset = kLinuxKasan_ShadowOffset64;
491       else
492         Mapping.Offset = (kSmallX86_64ShadowOffsetBase &
493                           (kSmallX86_64ShadowOffsetAlignMask << Mapping.Scale));
494     } else if (IsWindows && IsX86_64) {
495       Mapping.Offset = kWindowsShadowOffset64;
496     } else if (IsMIPS64)
497       Mapping.Offset = kMIPS64_ShadowOffset64;
498     else if (IsIOS)
499       Mapping.Offset = kDynamicShadowSentinel;
500     else if (IsAArch64)
501       Mapping.Offset = kAArch64_ShadowOffset64;
502     else
503       Mapping.Offset = kDefaultShadowOffset64;
504   }
505 
506   if (ClForceDynamicShadow) {
507     Mapping.Offset = kDynamicShadowSentinel;
508   }
509 
510   if (ClMappingOffset.getNumOccurrences() > 0) {
511     Mapping.Offset = ClMappingOffset;
512   }
513 
514   // OR-ing shadow offset if more efficient (at least on x86) if the offset
515   // is a power of two, but on ppc64 we have to use add since the shadow
516   // offset is not necessary 1/8-th of the address space.  On SystemZ,
517   // we could OR the constant in a single instruction, but it's more
518   // efficient to load it once and use indexed addressing.
519   Mapping.OrShadowOffset = !IsAArch64 && !IsPPC64 && !IsSystemZ && !IsPS4CPU &&
520                            !(Mapping.Offset & (Mapping.Offset - 1)) &&
521                            Mapping.Offset != kDynamicShadowSentinel;
522   bool IsAndroidWithIfuncSupport =
523       IsAndroid && !TargetTriple.isAndroidVersionLT(21);
524   Mapping.InGlobal = ClWithIfunc && IsAndroidWithIfuncSupport && IsArmOrThumb;
525 
526   return Mapping;
527 }
528 
529 static size_t RedzoneSizeForScale(int MappingScale) {
530   // Redzone used for stack and globals is at least 32 bytes.
531   // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
532   return std::max(32U, 1U << MappingScale);
533 }
534 
535 static uint64_t GetCtorAndDtorPriority(Triple &TargetTriple) {
536   if (TargetTriple.isOSEmscripten()) {
537     return kAsanEmscriptenCtorAndDtorPriority;
538   } else {
539     return kAsanCtorAndDtorPriority;
540   }
541 }
542 
543 namespace {
544 
545 /// Module analysis for getting various metadata about the module.
546 class ASanGlobalsMetadataWrapperPass : public ModulePass {
547 public:
548   static char ID;
549 
550   ASanGlobalsMetadataWrapperPass() : ModulePass(ID) {
551     initializeASanGlobalsMetadataWrapperPassPass(
552         *PassRegistry::getPassRegistry());
553   }
554 
555   bool runOnModule(Module &M) override {
556     GlobalsMD = GlobalsMetadata(M);
557     return false;
558   }
559 
560   StringRef getPassName() const override {
561     return "ASanGlobalsMetadataWrapperPass";
562   }
563 
564   void getAnalysisUsage(AnalysisUsage &AU) const override {
565     AU.setPreservesAll();
566   }
567 
568   GlobalsMetadata &getGlobalsMD() { return GlobalsMD; }
569 
570 private:
571   GlobalsMetadata GlobalsMD;
572 };
573 
574 char ASanGlobalsMetadataWrapperPass::ID = 0;
575 
576 /// AddressSanitizer: instrument the code in module to find memory bugs.
577 struct AddressSanitizer {
578   AddressSanitizer(Module &M, GlobalsMetadata &GlobalsMD,
579                    bool CompileKernel = false, bool Recover = false,
580                    bool UseAfterScope = false)
581       : UseAfterScope(UseAfterScope || ClUseAfterScope), GlobalsMD(GlobalsMD) {
582     this->Recover = ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover;
583     this->CompileKernel =
584         ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan : CompileKernel;
585 
586     C = &(M.getContext());
587     LongSize = M.getDataLayout().getPointerSizeInBits();
588     IntptrTy = Type::getIntNTy(*C, LongSize);
589     TargetTriple = Triple(M.getTargetTriple());
590 
591     Mapping = getShadowMapping(TargetTriple, LongSize, this->CompileKernel);
592   }
593 
594   uint64_t getAllocaSizeInBytes(const AllocaInst &AI) const {
595     uint64_t ArraySize = 1;
596     if (AI.isArrayAllocation()) {
597       const ConstantInt *CI = dyn_cast<ConstantInt>(AI.getArraySize());
598       assert(CI && "non-constant array size");
599       ArraySize = CI->getZExtValue();
600     }
601     Type *Ty = AI.getAllocatedType();
602     uint64_t SizeInBytes =
603         AI.getModule()->getDataLayout().getTypeAllocSize(Ty);
604     return SizeInBytes * ArraySize;
605   }
606 
607   /// Check if we want (and can) handle this alloca.
608   bool isInterestingAlloca(const AllocaInst &AI);
609 
610   /// If it is an interesting memory access, return the PointerOperand
611   /// and set IsWrite/Alignment. Otherwise return nullptr.
612   /// MaybeMask is an output parameter for the mask Value, if we're looking at a
613   /// masked load/store.
614   Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite,
615                                    uint64_t *TypeSize, unsigned *Alignment,
616                                    Value **MaybeMask = nullptr);
617 
618   void instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis, Instruction *I,
619                      bool UseCalls, const DataLayout &DL);
620   void instrumentPointerComparisonOrSubtraction(Instruction *I);
621   void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore,
622                          Value *Addr, uint32_t TypeSize, bool IsWrite,
623                          Value *SizeArgument, bool UseCalls, uint32_t Exp);
624   void instrumentUnusualSizeOrAlignment(Instruction *I,
625                                         Instruction *InsertBefore, Value *Addr,
626                                         uint32_t TypeSize, bool IsWrite,
627                                         Value *SizeArgument, bool UseCalls,
628                                         uint32_t Exp);
629   Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
630                            Value *ShadowValue, uint32_t TypeSize);
631   Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr,
632                                  bool IsWrite, size_t AccessSizeIndex,
633                                  Value *SizeArgument, uint32_t Exp);
634   void instrumentMemIntrinsic(MemIntrinsic *MI);
635   Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
636   bool instrumentFunction(Function &F, const TargetLibraryInfo *TLI);
637   bool maybeInsertAsanInitAtFunctionEntry(Function &F);
638   void maybeInsertDynamicShadowAtFunctionEntry(Function &F);
639   void markEscapedLocalAllocas(Function &F);
640 
641 private:
642   friend struct FunctionStackPoisoner;
643 
644   void initializeCallbacks(Module &M);
645 
646   bool LooksLikeCodeInBug11395(Instruction *I);
647   bool GlobalIsLinkerInitialized(GlobalVariable *G);
648   bool isSafeAccess(ObjectSizeOffsetVisitor &ObjSizeVis, Value *Addr,
649                     uint64_t TypeSize) const;
650 
651   /// Helper to cleanup per-function state.
652   struct FunctionStateRAII {
653     AddressSanitizer *Pass;
654 
655     FunctionStateRAII(AddressSanitizer *Pass) : Pass(Pass) {
656       assert(Pass->ProcessedAllocas.empty() &&
657              "last pass forgot to clear cache");
658       assert(!Pass->LocalDynamicShadow);
659     }
660 
661     ~FunctionStateRAII() {
662       Pass->LocalDynamicShadow = nullptr;
663       Pass->ProcessedAllocas.clear();
664     }
665   };
666 
667   LLVMContext *C;
668   Triple TargetTriple;
669   int LongSize;
670   bool CompileKernel;
671   bool Recover;
672   bool UseAfterScope;
673   Type *IntptrTy;
674   ShadowMapping Mapping;
675   FunctionCallee AsanHandleNoReturnFunc;
676   FunctionCallee AsanPtrCmpFunction, AsanPtrSubFunction;
677   Constant *AsanShadowGlobal;
678 
679   // These arrays is indexed by AccessIsWrite, Experiment and log2(AccessSize).
680   FunctionCallee AsanErrorCallback[2][2][kNumberOfAccessSizes];
681   FunctionCallee AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes];
682 
683   // These arrays is indexed by AccessIsWrite and Experiment.
684   FunctionCallee AsanErrorCallbackSized[2][2];
685   FunctionCallee AsanMemoryAccessCallbackSized[2][2];
686 
687   FunctionCallee AsanMemmove, AsanMemcpy, AsanMemset;
688   InlineAsm *EmptyAsm;
689   Value *LocalDynamicShadow = nullptr;
690   GlobalsMetadata GlobalsMD;
691   DenseMap<const AllocaInst *, bool> ProcessedAllocas;
692 };
693 
694 class AddressSanitizerLegacyPass : public FunctionPass {
695 public:
696   static char ID;
697 
698   explicit AddressSanitizerLegacyPass(bool CompileKernel = false,
699                                       bool Recover = false,
700                                       bool UseAfterScope = false)
701       : FunctionPass(ID), CompileKernel(CompileKernel), Recover(Recover),
702         UseAfterScope(UseAfterScope) {
703     initializeAddressSanitizerLegacyPassPass(*PassRegistry::getPassRegistry());
704   }
705 
706   StringRef getPassName() const override {
707     return "AddressSanitizerFunctionPass";
708   }
709 
710   void getAnalysisUsage(AnalysisUsage &AU) const override {
711     AU.addRequired<ASanGlobalsMetadataWrapperPass>();
712     AU.addRequired<TargetLibraryInfoWrapperPass>();
713   }
714 
715   bool runOnFunction(Function &F) override {
716     GlobalsMetadata &GlobalsMD =
717         getAnalysis<ASanGlobalsMetadataWrapperPass>().getGlobalsMD();
718     const TargetLibraryInfo *TLI =
719         &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
720     AddressSanitizer ASan(*F.getParent(), GlobalsMD, CompileKernel, Recover,
721                           UseAfterScope);
722     return ASan.instrumentFunction(F, TLI);
723   }
724 
725 private:
726   bool CompileKernel;
727   bool Recover;
728   bool UseAfterScope;
729 };
730 
731 class ModuleAddressSanitizer {
732 public:
733   ModuleAddressSanitizer(Module &M, GlobalsMetadata &GlobalsMD,
734                          bool CompileKernel = false, bool Recover = false,
735                          bool UseGlobalsGC = true, bool UseOdrIndicator = false)
736       : GlobalsMD(GlobalsMD), UseGlobalsGC(UseGlobalsGC && ClUseGlobalsGC),
737         // Enable aliases as they should have no downside with ODR indicators.
738         UsePrivateAlias(UseOdrIndicator || ClUsePrivateAlias),
739         UseOdrIndicator(UseOdrIndicator || ClUseOdrIndicator),
740         // Not a typo: ClWithComdat is almost completely pointless without
741         // ClUseGlobalsGC (because then it only works on modules without
742         // globals, which are rare); it is a prerequisite for ClUseGlobalsGC;
743         // and both suffer from gold PR19002 for which UseGlobalsGC constructor
744         // argument is designed as workaround. Therefore, disable both
745         // ClWithComdat and ClUseGlobalsGC unless the frontend says it's ok to
746         // do globals-gc.
747         UseCtorComdat(UseGlobalsGC && ClWithComdat) {
748     this->Recover = ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover;
749     this->CompileKernel =
750         ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan : CompileKernel;
751 
752     C = &(M.getContext());
753     int LongSize = M.getDataLayout().getPointerSizeInBits();
754     IntptrTy = Type::getIntNTy(*C, LongSize);
755     TargetTriple = Triple(M.getTargetTriple());
756     Mapping = getShadowMapping(TargetTriple, LongSize, this->CompileKernel);
757   }
758 
759   bool instrumentModule(Module &);
760 
761 private:
762   void initializeCallbacks(Module &M);
763 
764   bool InstrumentGlobals(IRBuilder<> &IRB, Module &M, bool *CtorComdat);
765   void InstrumentGlobalsCOFF(IRBuilder<> &IRB, Module &M,
766                              ArrayRef<GlobalVariable *> ExtendedGlobals,
767                              ArrayRef<Constant *> MetadataInitializers);
768   void InstrumentGlobalsELF(IRBuilder<> &IRB, Module &M,
769                             ArrayRef<GlobalVariable *> ExtendedGlobals,
770                             ArrayRef<Constant *> MetadataInitializers,
771                             const std::string &UniqueModuleId);
772   void InstrumentGlobalsMachO(IRBuilder<> &IRB, Module &M,
773                               ArrayRef<GlobalVariable *> ExtendedGlobals,
774                               ArrayRef<Constant *> MetadataInitializers);
775   void
776   InstrumentGlobalsWithMetadataArray(IRBuilder<> &IRB, Module &M,
777                                      ArrayRef<GlobalVariable *> ExtendedGlobals,
778                                      ArrayRef<Constant *> MetadataInitializers);
779 
780   GlobalVariable *CreateMetadataGlobal(Module &M, Constant *Initializer,
781                                        StringRef OriginalName);
782   void SetComdatForGlobalMetadata(GlobalVariable *G, GlobalVariable *Metadata,
783                                   StringRef InternalSuffix);
784   IRBuilder<> CreateAsanModuleDtor(Module &M);
785 
786   bool ShouldInstrumentGlobal(GlobalVariable *G);
787   bool ShouldUseMachOGlobalsSection() const;
788   StringRef getGlobalMetadataSection() const;
789   void poisonOneInitializer(Function &GlobalInit, GlobalValue *ModuleName);
790   void createInitializerPoisonCalls(Module &M, GlobalValue *ModuleName);
791   size_t MinRedzoneSizeForGlobal() const {
792     return RedzoneSizeForScale(Mapping.Scale);
793   }
794   int GetAsanVersion(const Module &M) const;
795 
796   GlobalsMetadata GlobalsMD;
797   bool CompileKernel;
798   bool Recover;
799   bool UseGlobalsGC;
800   bool UsePrivateAlias;
801   bool UseOdrIndicator;
802   bool UseCtorComdat;
803   Type *IntptrTy;
804   LLVMContext *C;
805   Triple TargetTriple;
806   ShadowMapping Mapping;
807   FunctionCallee AsanPoisonGlobals;
808   FunctionCallee AsanUnpoisonGlobals;
809   FunctionCallee AsanRegisterGlobals;
810   FunctionCallee AsanUnregisterGlobals;
811   FunctionCallee AsanRegisterImageGlobals;
812   FunctionCallee AsanUnregisterImageGlobals;
813   FunctionCallee AsanRegisterElfGlobals;
814   FunctionCallee AsanUnregisterElfGlobals;
815 
816   Function *AsanCtorFunction = nullptr;
817   Function *AsanDtorFunction = nullptr;
818 };
819 
820 class ModuleAddressSanitizerLegacyPass : public ModulePass {
821 public:
822   static char ID;
823 
824   explicit ModuleAddressSanitizerLegacyPass(bool CompileKernel = false,
825                                             bool Recover = false,
826                                             bool UseGlobalGC = true,
827                                             bool UseOdrIndicator = false)
828       : ModulePass(ID), CompileKernel(CompileKernel), Recover(Recover),
829         UseGlobalGC(UseGlobalGC), UseOdrIndicator(UseOdrIndicator) {
830     initializeModuleAddressSanitizerLegacyPassPass(
831         *PassRegistry::getPassRegistry());
832   }
833 
834   StringRef getPassName() const override { return "ModuleAddressSanitizer"; }
835 
836   void getAnalysisUsage(AnalysisUsage &AU) const override {
837     AU.addRequired<ASanGlobalsMetadataWrapperPass>();
838   }
839 
840   bool runOnModule(Module &M) override {
841     GlobalsMetadata &GlobalsMD =
842         getAnalysis<ASanGlobalsMetadataWrapperPass>().getGlobalsMD();
843     ModuleAddressSanitizer ASanModule(M, GlobalsMD, CompileKernel, Recover,
844                                       UseGlobalGC, UseOdrIndicator);
845     return ASanModule.instrumentModule(M);
846   }
847 
848 private:
849   bool CompileKernel;
850   bool Recover;
851   bool UseGlobalGC;
852   bool UseOdrIndicator;
853 };
854 
855 // Stack poisoning does not play well with exception handling.
856 // When an exception is thrown, we essentially bypass the code
857 // that unpoisones the stack. This is why the run-time library has
858 // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
859 // stack in the interceptor. This however does not work inside the
860 // actual function which catches the exception. Most likely because the
861 // compiler hoists the load of the shadow value somewhere too high.
862 // This causes asan to report a non-existing bug on 453.povray.
863 // It sounds like an LLVM bug.
864 struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
865   Function &F;
866   AddressSanitizer &ASan;
867   DIBuilder DIB;
868   LLVMContext *C;
869   Type *IntptrTy;
870   Type *IntptrPtrTy;
871   ShadowMapping Mapping;
872 
873   SmallVector<AllocaInst *, 16> AllocaVec;
874   SmallVector<AllocaInst *, 16> StaticAllocasToMoveUp;
875   SmallVector<Instruction *, 8> RetVec;
876   unsigned StackAlignment;
877 
878   FunctionCallee AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
879       AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1];
880   FunctionCallee AsanSetShadowFunc[0x100] = {};
881   FunctionCallee AsanPoisonStackMemoryFunc, AsanUnpoisonStackMemoryFunc;
882   FunctionCallee AsanAllocaPoisonFunc, AsanAllocasUnpoisonFunc;
883 
884   // Stores a place and arguments of poisoning/unpoisoning call for alloca.
885   struct AllocaPoisonCall {
886     IntrinsicInst *InsBefore;
887     AllocaInst *AI;
888     uint64_t Size;
889     bool DoPoison;
890   };
891   SmallVector<AllocaPoisonCall, 8> DynamicAllocaPoisonCallVec;
892   SmallVector<AllocaPoisonCall, 8> StaticAllocaPoisonCallVec;
893   bool HasUntracedLifetimeIntrinsic = false;
894 
895   SmallVector<AllocaInst *, 1> DynamicAllocaVec;
896   SmallVector<IntrinsicInst *, 1> StackRestoreVec;
897   AllocaInst *DynamicAllocaLayout = nullptr;
898   IntrinsicInst *LocalEscapeCall = nullptr;
899 
900   // Maps Value to an AllocaInst from which the Value is originated.
901   using AllocaForValueMapTy = DenseMap<Value *, AllocaInst *>;
902   AllocaForValueMapTy AllocaForValue;
903 
904   bool HasNonEmptyInlineAsm = false;
905   bool HasReturnsTwiceCall = false;
906   std::unique_ptr<CallInst> EmptyInlineAsm;
907 
908   FunctionStackPoisoner(Function &F, AddressSanitizer &ASan)
909       : F(F), ASan(ASan), DIB(*F.getParent(), /*AllowUnresolved*/ false),
910         C(ASan.C), IntptrTy(ASan.IntptrTy),
911         IntptrPtrTy(PointerType::get(IntptrTy, 0)), Mapping(ASan.Mapping),
912         StackAlignment(1 << Mapping.Scale),
913         EmptyInlineAsm(CallInst::Create(ASan.EmptyAsm)) {}
914 
915   bool runOnFunction() {
916     if (!ClStack) return false;
917 
918     if (ClRedzoneByvalArgs)
919       copyArgsPassedByValToAllocas();
920 
921     // Collect alloca, ret, lifetime instructions etc.
922     for (BasicBlock *BB : depth_first(&F.getEntryBlock())) visit(*BB);
923 
924     if (AllocaVec.empty() && DynamicAllocaVec.empty()) return false;
925 
926     initializeCallbacks(*F.getParent());
927 
928     if (HasUntracedLifetimeIntrinsic) {
929       // If there are lifetime intrinsics which couldn't be traced back to an
930       // alloca, we may not know exactly when a variable enters scope, and
931       // therefore should "fail safe" by not poisoning them.
932       StaticAllocaPoisonCallVec.clear();
933       DynamicAllocaPoisonCallVec.clear();
934     }
935 
936     processDynamicAllocas();
937     processStaticAllocas();
938 
939     if (ClDebugStack) {
940       LLVM_DEBUG(dbgs() << F);
941     }
942     return true;
943   }
944 
945   // Arguments marked with the "byval" attribute are implicitly copied without
946   // using an alloca instruction.  To produce redzones for those arguments, we
947   // copy them a second time into memory allocated with an alloca instruction.
948   void copyArgsPassedByValToAllocas();
949 
950   // Finds all Alloca instructions and puts
951   // poisoned red zones around all of them.
952   // Then unpoison everything back before the function returns.
953   void processStaticAllocas();
954   void processDynamicAllocas();
955 
956   void createDynamicAllocasInitStorage();
957 
958   // ----------------------- Visitors.
959   /// Collect all Ret instructions.
960   void visitReturnInst(ReturnInst &RI) { RetVec.push_back(&RI); }
961 
962   /// Collect all Resume instructions.
963   void visitResumeInst(ResumeInst &RI) { RetVec.push_back(&RI); }
964 
965   /// Collect all CatchReturnInst instructions.
966   void visitCleanupReturnInst(CleanupReturnInst &CRI) { RetVec.push_back(&CRI); }
967 
968   void unpoisonDynamicAllocasBeforeInst(Instruction *InstBefore,
969                                         Value *SavedStack) {
970     IRBuilder<> IRB(InstBefore);
971     Value *DynamicAreaPtr = IRB.CreatePtrToInt(SavedStack, IntptrTy);
972     // When we insert _asan_allocas_unpoison before @llvm.stackrestore, we
973     // need to adjust extracted SP to compute the address of the most recent
974     // alloca. We have a special @llvm.get.dynamic.area.offset intrinsic for
975     // this purpose.
976     if (!isa<ReturnInst>(InstBefore)) {
977       Function *DynamicAreaOffsetFunc = Intrinsic::getDeclaration(
978           InstBefore->getModule(), Intrinsic::get_dynamic_area_offset,
979           {IntptrTy});
980 
981       Value *DynamicAreaOffset = IRB.CreateCall(DynamicAreaOffsetFunc, {});
982 
983       DynamicAreaPtr = IRB.CreateAdd(IRB.CreatePtrToInt(SavedStack, IntptrTy),
984                                      DynamicAreaOffset);
985     }
986 
987     IRB.CreateCall(
988         AsanAllocasUnpoisonFunc,
989         {IRB.CreateLoad(IntptrTy, DynamicAllocaLayout), DynamicAreaPtr});
990   }
991 
992   // Unpoison dynamic allocas redzones.
993   void unpoisonDynamicAllocas() {
994     for (auto &Ret : RetVec)
995       unpoisonDynamicAllocasBeforeInst(Ret, DynamicAllocaLayout);
996 
997     for (auto &StackRestoreInst : StackRestoreVec)
998       unpoisonDynamicAllocasBeforeInst(StackRestoreInst,
999                                        StackRestoreInst->getOperand(0));
1000   }
1001 
1002   // Deploy and poison redzones around dynamic alloca call. To do this, we
1003   // should replace this call with another one with changed parameters and
1004   // replace all its uses with new address, so
1005   //   addr = alloca type, old_size, align
1006   // is replaced by
1007   //   new_size = (old_size + additional_size) * sizeof(type)
1008   //   tmp = alloca i8, new_size, max(align, 32)
1009   //   addr = tmp + 32 (first 32 bytes are for the left redzone).
1010   // Additional_size is added to make new memory allocation contain not only
1011   // requested memory, but also left, partial and right redzones.
1012   void handleDynamicAllocaCall(AllocaInst *AI);
1013 
1014   /// Collect Alloca instructions we want (and can) handle.
1015   void visitAllocaInst(AllocaInst &AI) {
1016     if (!ASan.isInterestingAlloca(AI)) {
1017       if (AI.isStaticAlloca()) {
1018         // Skip over allocas that are present *before* the first instrumented
1019         // alloca, we don't want to move those around.
1020         if (AllocaVec.empty())
1021           return;
1022 
1023         StaticAllocasToMoveUp.push_back(&AI);
1024       }
1025       return;
1026     }
1027 
1028     StackAlignment = std::max(StackAlignment, AI.getAlignment());
1029     if (!AI.isStaticAlloca())
1030       DynamicAllocaVec.push_back(&AI);
1031     else
1032       AllocaVec.push_back(&AI);
1033   }
1034 
1035   /// Collect lifetime intrinsic calls to check for use-after-scope
1036   /// errors.
1037   void visitIntrinsicInst(IntrinsicInst &II) {
1038     Intrinsic::ID ID = II.getIntrinsicID();
1039     if (ID == Intrinsic::stackrestore) StackRestoreVec.push_back(&II);
1040     if (ID == Intrinsic::localescape) LocalEscapeCall = &II;
1041     if (!ASan.UseAfterScope)
1042       return;
1043     if (!II.isLifetimeStartOrEnd())
1044       return;
1045     // Found lifetime intrinsic, add ASan instrumentation if necessary.
1046     ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0));
1047     // If size argument is undefined, don't do anything.
1048     if (Size->isMinusOne()) return;
1049     // Check that size doesn't saturate uint64_t and can
1050     // be stored in IntptrTy.
1051     const uint64_t SizeValue = Size->getValue().getLimitedValue();
1052     if (SizeValue == ~0ULL ||
1053         !ConstantInt::isValueValidForType(IntptrTy, SizeValue))
1054       return;
1055     // Find alloca instruction that corresponds to llvm.lifetime argument.
1056     AllocaInst *AI =
1057         llvm::findAllocaForValue(II.getArgOperand(1), AllocaForValue);
1058     if (!AI) {
1059       HasUntracedLifetimeIntrinsic = true;
1060       return;
1061     }
1062     // We're interested only in allocas we can handle.
1063     if (!ASan.isInterestingAlloca(*AI))
1064       return;
1065     bool DoPoison = (ID == Intrinsic::lifetime_end);
1066     AllocaPoisonCall APC = {&II, AI, SizeValue, DoPoison};
1067     if (AI->isStaticAlloca())
1068       StaticAllocaPoisonCallVec.push_back(APC);
1069     else if (ClInstrumentDynamicAllocas)
1070       DynamicAllocaPoisonCallVec.push_back(APC);
1071   }
1072 
1073   void visitCallSite(CallSite CS) {
1074     Instruction *I = CS.getInstruction();
1075     if (CallInst *CI = dyn_cast<CallInst>(I)) {
1076       HasNonEmptyInlineAsm |= CI->isInlineAsm() &&
1077                               !CI->isIdenticalTo(EmptyInlineAsm.get()) &&
1078                               I != ASan.LocalDynamicShadow;
1079       HasReturnsTwiceCall |= CI->canReturnTwice();
1080     }
1081   }
1082 
1083   // ---------------------- Helpers.
1084   void initializeCallbacks(Module &M);
1085 
1086   // Copies bytes from ShadowBytes into shadow memory for indexes where
1087   // ShadowMask is not zero. If ShadowMask[i] is zero, we assume that
1088   // ShadowBytes[i] is constantly zero and doesn't need to be overwritten.
1089   void copyToShadow(ArrayRef<uint8_t> ShadowMask, ArrayRef<uint8_t> ShadowBytes,
1090                     IRBuilder<> &IRB, Value *ShadowBase);
1091   void copyToShadow(ArrayRef<uint8_t> ShadowMask, ArrayRef<uint8_t> ShadowBytes,
1092                     size_t Begin, size_t End, IRBuilder<> &IRB,
1093                     Value *ShadowBase);
1094   void copyToShadowInline(ArrayRef<uint8_t> ShadowMask,
1095                           ArrayRef<uint8_t> ShadowBytes, size_t Begin,
1096                           size_t End, IRBuilder<> &IRB, Value *ShadowBase);
1097 
1098   void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> &IRB, bool DoPoison);
1099 
1100   Value *createAllocaForLayout(IRBuilder<> &IRB, const ASanStackFrameLayout &L,
1101                                bool Dynamic);
1102   PHINode *createPHI(IRBuilder<> &IRB, Value *Cond, Value *ValueIfTrue,
1103                      Instruction *ThenTerm, Value *ValueIfFalse);
1104 };
1105 
1106 } // end anonymous namespace
1107 
1108 void LocationMetadata::parse(MDNode *MDN) {
1109   assert(MDN->getNumOperands() == 3);
1110   MDString *DIFilename = cast<MDString>(MDN->getOperand(0));
1111   Filename = DIFilename->getString();
1112   LineNo = mdconst::extract<ConstantInt>(MDN->getOperand(1))->getLimitedValue();
1113   ColumnNo =
1114       mdconst::extract<ConstantInt>(MDN->getOperand(2))->getLimitedValue();
1115 }
1116 
1117 // FIXME: It would be cleaner to instead attach relevant metadata to the globals
1118 // we want to sanitize instead and reading this metadata on each pass over a
1119 // function instead of reading module level metadata at first.
1120 GlobalsMetadata::GlobalsMetadata(Module &M) {
1121   NamedMDNode *Globals = M.getNamedMetadata("llvm.asan.globals");
1122   if (!Globals)
1123     return;
1124   for (auto MDN : Globals->operands()) {
1125     // Metadata node contains the global and the fields of "Entry".
1126     assert(MDN->getNumOperands() == 5);
1127     auto *V = mdconst::extract_or_null<Constant>(MDN->getOperand(0));
1128     // The optimizer may optimize away a global entirely.
1129     if (!V)
1130       continue;
1131     auto *StrippedV = V->stripPointerCasts();
1132     auto *GV = dyn_cast<GlobalVariable>(StrippedV);
1133     if (!GV)
1134       continue;
1135     // We can already have an entry for GV if it was merged with another
1136     // global.
1137     Entry &E = Entries[GV];
1138     if (auto *Loc = cast_or_null<MDNode>(MDN->getOperand(1)))
1139       E.SourceLoc.parse(Loc);
1140     if (auto *Name = cast_or_null<MDString>(MDN->getOperand(2)))
1141       E.Name = Name->getString();
1142     ConstantInt *IsDynInit = mdconst::extract<ConstantInt>(MDN->getOperand(3));
1143     E.IsDynInit |= IsDynInit->isOne();
1144     ConstantInt *IsBlacklisted =
1145         mdconst::extract<ConstantInt>(MDN->getOperand(4));
1146     E.IsBlacklisted |= IsBlacklisted->isOne();
1147   }
1148 }
1149 
1150 AnalysisKey ASanGlobalsMetadataAnalysis::Key;
1151 
1152 GlobalsMetadata ASanGlobalsMetadataAnalysis::run(Module &M,
1153                                                  ModuleAnalysisManager &AM) {
1154   return GlobalsMetadata(M);
1155 }
1156 
1157 AddressSanitizerPass::AddressSanitizerPass(bool CompileKernel, bool Recover,
1158                                            bool UseAfterScope)
1159     : CompileKernel(CompileKernel), Recover(Recover),
1160       UseAfterScope(UseAfterScope) {}
1161 
1162 PreservedAnalyses AddressSanitizerPass::run(Function &F,
1163                                             AnalysisManager<Function> &AM) {
1164   auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
1165   auto &MAM = MAMProxy.getManager();
1166   Module &M = *F.getParent();
1167   if (auto *R = MAM.getCachedResult<ASanGlobalsMetadataAnalysis>(M)) {
1168     const TargetLibraryInfo *TLI = &AM.getResult<TargetLibraryAnalysis>(F);
1169     AddressSanitizer Sanitizer(M, *R, CompileKernel, Recover, UseAfterScope);
1170     if (Sanitizer.instrumentFunction(F, TLI))
1171       return PreservedAnalyses::none();
1172     return PreservedAnalyses::all();
1173   }
1174 
1175   report_fatal_error(
1176       "The ASanGlobalsMetadataAnalysis is required to run before "
1177       "AddressSanitizer can run");
1178   return PreservedAnalyses::all();
1179 }
1180 
1181 ModuleAddressSanitizerPass::ModuleAddressSanitizerPass(bool CompileKernel,
1182                                                        bool Recover,
1183                                                        bool UseGlobalGC,
1184                                                        bool UseOdrIndicator)
1185     : CompileKernel(CompileKernel), Recover(Recover), UseGlobalGC(UseGlobalGC),
1186       UseOdrIndicator(UseOdrIndicator) {}
1187 
1188 PreservedAnalyses ModuleAddressSanitizerPass::run(Module &M,
1189                                                   AnalysisManager<Module> &AM) {
1190   GlobalsMetadata &GlobalsMD = AM.getResult<ASanGlobalsMetadataAnalysis>(M);
1191   ModuleAddressSanitizer Sanitizer(M, GlobalsMD, CompileKernel, Recover,
1192                                    UseGlobalGC, UseOdrIndicator);
1193   if (Sanitizer.instrumentModule(M))
1194     return PreservedAnalyses::none();
1195   return PreservedAnalyses::all();
1196 }
1197 
1198 INITIALIZE_PASS(ASanGlobalsMetadataWrapperPass, "asan-globals-md",
1199                 "Read metadata to mark which globals should be instrumented "
1200                 "when running ASan.",
1201                 false, true)
1202 
1203 char AddressSanitizerLegacyPass::ID = 0;
1204 
1205 INITIALIZE_PASS_BEGIN(
1206     AddressSanitizerLegacyPass, "asan",
1207     "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", false,
1208     false)
1209 INITIALIZE_PASS_DEPENDENCY(ASanGlobalsMetadataWrapperPass)
1210 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1211 INITIALIZE_PASS_END(
1212     AddressSanitizerLegacyPass, "asan",
1213     "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", false,
1214     false)
1215 
1216 FunctionPass *llvm::createAddressSanitizerFunctionPass(bool CompileKernel,
1217                                                        bool Recover,
1218                                                        bool UseAfterScope) {
1219   assert(!CompileKernel || Recover);
1220   return new AddressSanitizerLegacyPass(CompileKernel, Recover, UseAfterScope);
1221 }
1222 
1223 char ModuleAddressSanitizerLegacyPass::ID = 0;
1224 
1225 INITIALIZE_PASS(
1226     ModuleAddressSanitizerLegacyPass, "asan-module",
1227     "AddressSanitizer: detects use-after-free and out-of-bounds bugs."
1228     "ModulePass",
1229     false, false)
1230 
1231 ModulePass *llvm::createModuleAddressSanitizerLegacyPassPass(
1232     bool CompileKernel, bool Recover, bool UseGlobalsGC, bool UseOdrIndicator) {
1233   assert(!CompileKernel || Recover);
1234   return new ModuleAddressSanitizerLegacyPass(CompileKernel, Recover,
1235                                               UseGlobalsGC, UseOdrIndicator);
1236 }
1237 
1238 static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
1239   size_t Res = countTrailingZeros(TypeSize / 8);
1240   assert(Res < kNumberOfAccessSizes);
1241   return Res;
1242 }
1243 
1244 /// Create a global describing a source location.
1245 static GlobalVariable *createPrivateGlobalForSourceLoc(Module &M,
1246                                                        LocationMetadata MD) {
1247   Constant *LocData[] = {
1248       createPrivateGlobalForString(M, MD.Filename, true, kAsanGenPrefix),
1249       ConstantInt::get(Type::getInt32Ty(M.getContext()), MD.LineNo),
1250       ConstantInt::get(Type::getInt32Ty(M.getContext()), MD.ColumnNo),
1251   };
1252   auto LocStruct = ConstantStruct::getAnon(LocData);
1253   auto GV = new GlobalVariable(M, LocStruct->getType(), true,
1254                                GlobalValue::PrivateLinkage, LocStruct,
1255                                kAsanGenPrefix);
1256   GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1257   return GV;
1258 }
1259 
1260 /// Check if \p G has been created by a trusted compiler pass.
1261 static bool GlobalWasGeneratedByCompiler(GlobalVariable *G) {
1262   // Do not instrument @llvm.global_ctors, @llvm.used, etc.
1263   if (G->getName().startswith("llvm."))
1264     return true;
1265 
1266   // Do not instrument asan globals.
1267   if (G->getName().startswith(kAsanGenPrefix) ||
1268       G->getName().startswith(kSanCovGenPrefix) ||
1269       G->getName().startswith(kODRGenPrefix))
1270     return true;
1271 
1272   // Do not instrument gcov counter arrays.
1273   if (G->getName() == "__llvm_gcov_ctr")
1274     return true;
1275 
1276   return false;
1277 }
1278 
1279 Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
1280   // Shadow >> scale
1281   Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
1282   if (Mapping.Offset == 0) return Shadow;
1283   // (Shadow >> scale) | offset
1284   Value *ShadowBase;
1285   if (LocalDynamicShadow)
1286     ShadowBase = LocalDynamicShadow;
1287   else
1288     ShadowBase = ConstantInt::get(IntptrTy, Mapping.Offset);
1289   if (Mapping.OrShadowOffset)
1290     return IRB.CreateOr(Shadow, ShadowBase);
1291   else
1292     return IRB.CreateAdd(Shadow, ShadowBase);
1293 }
1294 
1295 // Instrument memset/memmove/memcpy
1296 void AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
1297   IRBuilder<> IRB(MI);
1298   if (isa<MemTransferInst>(MI)) {
1299     IRB.CreateCall(
1300         isa<MemMoveInst>(MI) ? AsanMemmove : AsanMemcpy,
1301         {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
1302          IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()),
1303          IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
1304   } else if (isa<MemSetInst>(MI)) {
1305     IRB.CreateCall(
1306         AsanMemset,
1307         {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
1308          IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false),
1309          IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
1310   }
1311   MI->eraseFromParent();
1312 }
1313 
1314 /// Check if we want (and can) handle this alloca.
1315 bool AddressSanitizer::isInterestingAlloca(const AllocaInst &AI) {
1316   auto PreviouslySeenAllocaInfo = ProcessedAllocas.find(&AI);
1317 
1318   if (PreviouslySeenAllocaInfo != ProcessedAllocas.end())
1319     return PreviouslySeenAllocaInfo->getSecond();
1320 
1321   bool IsInteresting =
1322       (AI.getAllocatedType()->isSized() &&
1323        // alloca() may be called with 0 size, ignore it.
1324        ((!AI.isStaticAlloca()) || getAllocaSizeInBytes(AI) > 0) &&
1325        // We are only interested in allocas not promotable to registers.
1326        // Promotable allocas are common under -O0.
1327        (!ClSkipPromotableAllocas || !isAllocaPromotable(&AI)) &&
1328        // inalloca allocas are not treated as static, and we don't want
1329        // dynamic alloca instrumentation for them as well.
1330        !AI.isUsedWithInAlloca() &&
1331        // swifterror allocas are register promoted by ISel
1332        !AI.isSwiftError());
1333 
1334   ProcessedAllocas[&AI] = IsInteresting;
1335   return IsInteresting;
1336 }
1337 
1338 Value *AddressSanitizer::isInterestingMemoryAccess(Instruction *I,
1339                                                    bool *IsWrite,
1340                                                    uint64_t *TypeSize,
1341                                                    unsigned *Alignment,
1342                                                    Value **MaybeMask) {
1343   // Skip memory accesses inserted by another instrumentation.
1344   if (I->getMetadata("nosanitize")) return nullptr;
1345 
1346   // Do not instrument the load fetching the dynamic shadow address.
1347   if (LocalDynamicShadow == I)
1348     return nullptr;
1349 
1350   Value *PtrOperand = nullptr;
1351   const DataLayout &DL = I->getModule()->getDataLayout();
1352   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1353     if (!ClInstrumentReads) return nullptr;
1354     *IsWrite = false;
1355     *TypeSize = DL.getTypeStoreSizeInBits(LI->getType());
1356     *Alignment = LI->getAlignment();
1357     PtrOperand = LI->getPointerOperand();
1358   } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1359     if (!ClInstrumentWrites) return nullptr;
1360     *IsWrite = true;
1361     *TypeSize = DL.getTypeStoreSizeInBits(SI->getValueOperand()->getType());
1362     *Alignment = SI->getAlignment();
1363     PtrOperand = SI->getPointerOperand();
1364   } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
1365     if (!ClInstrumentAtomics) return nullptr;
1366     *IsWrite = true;
1367     *TypeSize = DL.getTypeStoreSizeInBits(RMW->getValOperand()->getType());
1368     *Alignment = 0;
1369     PtrOperand = RMW->getPointerOperand();
1370   } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
1371     if (!ClInstrumentAtomics) return nullptr;
1372     *IsWrite = true;
1373     *TypeSize = DL.getTypeStoreSizeInBits(XCHG->getCompareOperand()->getType());
1374     *Alignment = 0;
1375     PtrOperand = XCHG->getPointerOperand();
1376   } else if (auto CI = dyn_cast<CallInst>(I)) {
1377     auto *F = dyn_cast<Function>(CI->getCalledValue());
1378     if (F && (F->getName().startswith("llvm.masked.load.") ||
1379               F->getName().startswith("llvm.masked.store."))) {
1380       unsigned OpOffset = 0;
1381       if (F->getName().startswith("llvm.masked.store.")) {
1382         if (!ClInstrumentWrites)
1383           return nullptr;
1384         // Masked store has an initial operand for the value.
1385         OpOffset = 1;
1386         *IsWrite = true;
1387       } else {
1388         if (!ClInstrumentReads)
1389           return nullptr;
1390         *IsWrite = false;
1391       }
1392 
1393       auto BasePtr = CI->getOperand(0 + OpOffset);
1394       auto Ty = cast<PointerType>(BasePtr->getType())->getElementType();
1395       *TypeSize = DL.getTypeStoreSizeInBits(Ty);
1396       if (auto AlignmentConstant =
1397               dyn_cast<ConstantInt>(CI->getOperand(1 + OpOffset)))
1398         *Alignment = (unsigned)AlignmentConstant->getZExtValue();
1399       else
1400         *Alignment = 1; // No alignment guarantees. We probably got Undef
1401       if (MaybeMask)
1402         *MaybeMask = CI->getOperand(2 + OpOffset);
1403       PtrOperand = BasePtr;
1404     }
1405   }
1406 
1407   if (PtrOperand) {
1408     // Do not instrument acesses from different address spaces; we cannot deal
1409     // with them.
1410     Type *PtrTy = cast<PointerType>(PtrOperand->getType()->getScalarType());
1411     if (PtrTy->getPointerAddressSpace() != 0)
1412       return nullptr;
1413 
1414     // Ignore swifterror addresses.
1415     // swifterror memory addresses are mem2reg promoted by instruction
1416     // selection. As such they cannot have regular uses like an instrumentation
1417     // function and it makes no sense to track them as memory.
1418     if (PtrOperand->isSwiftError())
1419       return nullptr;
1420   }
1421 
1422   // Treat memory accesses to promotable allocas as non-interesting since they
1423   // will not cause memory violations. This greatly speeds up the instrumented
1424   // executable at -O0.
1425   if (ClSkipPromotableAllocas)
1426     if (auto AI = dyn_cast_or_null<AllocaInst>(PtrOperand))
1427       return isInterestingAlloca(*AI) ? AI : nullptr;
1428 
1429   return PtrOperand;
1430 }
1431 
1432 static bool isPointerOperand(Value *V) {
1433   return V->getType()->isPointerTy() || isa<PtrToIntInst>(V);
1434 }
1435 
1436 // This is a rough heuristic; it may cause both false positives and
1437 // false negatives. The proper implementation requires cooperation with
1438 // the frontend.
1439 static bool isInterestingPointerComparison(Instruction *I) {
1440   if (ICmpInst *Cmp = dyn_cast<ICmpInst>(I)) {
1441     if (!Cmp->isRelational())
1442       return false;
1443   } else {
1444     return false;
1445   }
1446   return isPointerOperand(I->getOperand(0)) &&
1447          isPointerOperand(I->getOperand(1));
1448 }
1449 
1450 // This is a rough heuristic; it may cause both false positives and
1451 // false negatives. The proper implementation requires cooperation with
1452 // the frontend.
1453 static bool isInterestingPointerSubtraction(Instruction *I) {
1454   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
1455     if (BO->getOpcode() != Instruction::Sub)
1456       return false;
1457   } else {
1458     return false;
1459   }
1460   return isPointerOperand(I->getOperand(0)) &&
1461          isPointerOperand(I->getOperand(1));
1462 }
1463 
1464 bool AddressSanitizer::GlobalIsLinkerInitialized(GlobalVariable *G) {
1465   // If a global variable does not have dynamic initialization we don't
1466   // have to instrument it.  However, if a global does not have initializer
1467   // at all, we assume it has dynamic initializer (in other TU).
1468   //
1469   // FIXME: Metadata should be attched directly to the global directly instead
1470   // of being added to llvm.asan.globals.
1471   return G->hasInitializer() && !GlobalsMD.get(G).IsDynInit;
1472 }
1473 
1474 void AddressSanitizer::instrumentPointerComparisonOrSubtraction(
1475     Instruction *I) {
1476   IRBuilder<> IRB(I);
1477   FunctionCallee F = isa<ICmpInst>(I) ? AsanPtrCmpFunction : AsanPtrSubFunction;
1478   Value *Param[2] = {I->getOperand(0), I->getOperand(1)};
1479   for (Value *&i : Param) {
1480     if (i->getType()->isPointerTy())
1481       i = IRB.CreatePointerCast(i, IntptrTy);
1482   }
1483   IRB.CreateCall(F, Param);
1484 }
1485 
1486 static void doInstrumentAddress(AddressSanitizer *Pass, Instruction *I,
1487                                 Instruction *InsertBefore, Value *Addr,
1488                                 unsigned Alignment, unsigned Granularity,
1489                                 uint32_t TypeSize, bool IsWrite,
1490                                 Value *SizeArgument, bool UseCalls,
1491                                 uint32_t Exp) {
1492   // Instrument a 1-, 2-, 4-, 8-, or 16- byte access with one check
1493   // if the data is properly aligned.
1494   if ((TypeSize == 8 || TypeSize == 16 || TypeSize == 32 || TypeSize == 64 ||
1495        TypeSize == 128) &&
1496       (Alignment >= Granularity || Alignment == 0 || Alignment >= TypeSize / 8))
1497     return Pass->instrumentAddress(I, InsertBefore, Addr, TypeSize, IsWrite,
1498                                    nullptr, UseCalls, Exp);
1499   Pass->instrumentUnusualSizeOrAlignment(I, InsertBefore, Addr, TypeSize,
1500                                          IsWrite, nullptr, UseCalls, Exp);
1501 }
1502 
1503 static void instrumentMaskedLoadOrStore(AddressSanitizer *Pass,
1504                                         const DataLayout &DL, Type *IntptrTy,
1505                                         Value *Mask, Instruction *I,
1506                                         Value *Addr, unsigned Alignment,
1507                                         unsigned Granularity, uint32_t TypeSize,
1508                                         bool IsWrite, Value *SizeArgument,
1509                                         bool UseCalls, uint32_t Exp) {
1510   auto *VTy = cast<PointerType>(Addr->getType())->getElementType();
1511   uint64_t ElemTypeSize = DL.getTypeStoreSizeInBits(VTy->getScalarType());
1512   unsigned Num = VTy->getVectorNumElements();
1513   auto Zero = ConstantInt::get(IntptrTy, 0);
1514   for (unsigned Idx = 0; Idx < Num; ++Idx) {
1515     Value *InstrumentedAddress = nullptr;
1516     Instruction *InsertBefore = I;
1517     if (auto *Vector = dyn_cast<ConstantVector>(Mask)) {
1518       // dyn_cast as we might get UndefValue
1519       if (auto *Masked = dyn_cast<ConstantInt>(Vector->getOperand(Idx))) {
1520         if (Masked->isZero())
1521           // Mask is constant false, so no instrumentation needed.
1522           continue;
1523         // If we have a true or undef value, fall through to doInstrumentAddress
1524         // with InsertBefore == I
1525       }
1526     } else {
1527       IRBuilder<> IRB(I);
1528       Value *MaskElem = IRB.CreateExtractElement(Mask, Idx);
1529       Instruction *ThenTerm = SplitBlockAndInsertIfThen(MaskElem, I, false);
1530       InsertBefore = ThenTerm;
1531     }
1532 
1533     IRBuilder<> IRB(InsertBefore);
1534     InstrumentedAddress =
1535         IRB.CreateGEP(VTy, Addr, {Zero, ConstantInt::get(IntptrTy, Idx)});
1536     doInstrumentAddress(Pass, I, InsertBefore, InstrumentedAddress, Alignment,
1537                         Granularity, ElemTypeSize, IsWrite, SizeArgument,
1538                         UseCalls, Exp);
1539   }
1540 }
1541 
1542 void AddressSanitizer::instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis,
1543                                      Instruction *I, bool UseCalls,
1544                                      const DataLayout &DL) {
1545   bool IsWrite = false;
1546   unsigned Alignment = 0;
1547   uint64_t TypeSize = 0;
1548   Value *MaybeMask = nullptr;
1549   Value *Addr =
1550       isInterestingMemoryAccess(I, &IsWrite, &TypeSize, &Alignment, &MaybeMask);
1551   assert(Addr);
1552 
1553   // Optimization experiments.
1554   // The experiments can be used to evaluate potential optimizations that remove
1555   // instrumentation (assess false negatives). Instead of completely removing
1556   // some instrumentation, you set Exp to a non-zero value (mask of optimization
1557   // experiments that want to remove instrumentation of this instruction).
1558   // If Exp is non-zero, this pass will emit special calls into runtime
1559   // (e.g. __asan_report_exp_load1 instead of __asan_report_load1). These calls
1560   // make runtime terminate the program in a special way (with a different
1561   // exit status). Then you run the new compiler on a buggy corpus, collect
1562   // the special terminations (ideally, you don't see them at all -- no false
1563   // negatives) and make the decision on the optimization.
1564   uint32_t Exp = ClForceExperiment;
1565 
1566   if (ClOpt && ClOptGlobals) {
1567     // If initialization order checking is disabled, a simple access to a
1568     // dynamically initialized global is always valid.
1569     GlobalVariable *G = dyn_cast<GlobalVariable>(GetUnderlyingObject(Addr, DL));
1570     if (G && (!ClInitializers || GlobalIsLinkerInitialized(G)) &&
1571         isSafeAccess(ObjSizeVis, Addr, TypeSize)) {
1572       NumOptimizedAccessesToGlobalVar++;
1573       return;
1574     }
1575   }
1576 
1577   if (ClOpt && ClOptStack) {
1578     // A direct inbounds access to a stack variable is always valid.
1579     if (isa<AllocaInst>(GetUnderlyingObject(Addr, DL)) &&
1580         isSafeAccess(ObjSizeVis, Addr, TypeSize)) {
1581       NumOptimizedAccessesToStackVar++;
1582       return;
1583     }
1584   }
1585 
1586   if (IsWrite)
1587     NumInstrumentedWrites++;
1588   else
1589     NumInstrumentedReads++;
1590 
1591   unsigned Granularity = 1 << Mapping.Scale;
1592   if (MaybeMask) {
1593     instrumentMaskedLoadOrStore(this, DL, IntptrTy, MaybeMask, I, Addr,
1594                                 Alignment, Granularity, TypeSize, IsWrite,
1595                                 nullptr, UseCalls, Exp);
1596   } else {
1597     doInstrumentAddress(this, I, I, Addr, Alignment, Granularity, TypeSize,
1598                         IsWrite, nullptr, UseCalls, Exp);
1599   }
1600 }
1601 
1602 Instruction *AddressSanitizer::generateCrashCode(Instruction *InsertBefore,
1603                                                  Value *Addr, bool IsWrite,
1604                                                  size_t AccessSizeIndex,
1605                                                  Value *SizeArgument,
1606                                                  uint32_t Exp) {
1607   IRBuilder<> IRB(InsertBefore);
1608   Value *ExpVal = Exp == 0 ? nullptr : ConstantInt::get(IRB.getInt32Ty(), Exp);
1609   CallInst *Call = nullptr;
1610   if (SizeArgument) {
1611     if (Exp == 0)
1612       Call = IRB.CreateCall(AsanErrorCallbackSized[IsWrite][0],
1613                             {Addr, SizeArgument});
1614     else
1615       Call = IRB.CreateCall(AsanErrorCallbackSized[IsWrite][1],
1616                             {Addr, SizeArgument, ExpVal});
1617   } else {
1618     if (Exp == 0)
1619       Call =
1620           IRB.CreateCall(AsanErrorCallback[IsWrite][0][AccessSizeIndex], Addr);
1621     else
1622       Call = IRB.CreateCall(AsanErrorCallback[IsWrite][1][AccessSizeIndex],
1623                             {Addr, ExpVal});
1624   }
1625 
1626   // We don't do Call->setDoesNotReturn() because the BB already has
1627   // UnreachableInst at the end.
1628   // This EmptyAsm is required to avoid callback merge.
1629   IRB.CreateCall(EmptyAsm, {});
1630   return Call;
1631 }
1632 
1633 Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
1634                                            Value *ShadowValue,
1635                                            uint32_t TypeSize) {
1636   size_t Granularity = static_cast<size_t>(1) << Mapping.Scale;
1637   // Addr & (Granularity - 1)
1638   Value *LastAccessedByte =
1639       IRB.CreateAnd(AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
1640   // (Addr & (Granularity - 1)) + size - 1
1641   if (TypeSize / 8 > 1)
1642     LastAccessedByte = IRB.CreateAdd(
1643         LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
1644   // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
1645   LastAccessedByte =
1646       IRB.CreateIntCast(LastAccessedByte, ShadowValue->getType(), false);
1647   // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
1648   return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
1649 }
1650 
1651 void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
1652                                          Instruction *InsertBefore, Value *Addr,
1653                                          uint32_t TypeSize, bool IsWrite,
1654                                          Value *SizeArgument, bool UseCalls,
1655                                          uint32_t Exp) {
1656   bool IsMyriad = TargetTriple.getVendor() == llvm::Triple::Myriad;
1657 
1658   IRBuilder<> IRB(InsertBefore);
1659   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
1660   size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
1661 
1662   if (UseCalls) {
1663     if (Exp == 0)
1664       IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][0][AccessSizeIndex],
1665                      AddrLong);
1666     else
1667       IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][1][AccessSizeIndex],
1668                      {AddrLong, ConstantInt::get(IRB.getInt32Ty(), Exp)});
1669     return;
1670   }
1671 
1672   if (IsMyriad) {
1673     // Strip the cache bit and do range check.
1674     // AddrLong &= ~kMyriadCacheBitMask32
1675     AddrLong = IRB.CreateAnd(AddrLong, ~kMyriadCacheBitMask32);
1676     // Tag = AddrLong >> kMyriadTagShift
1677     Value *Tag = IRB.CreateLShr(AddrLong, kMyriadTagShift);
1678     // Tag == kMyriadDDRTag
1679     Value *TagCheck =
1680         IRB.CreateICmpEQ(Tag, ConstantInt::get(IntptrTy, kMyriadDDRTag));
1681 
1682     Instruction *TagCheckTerm =
1683         SplitBlockAndInsertIfThen(TagCheck, InsertBefore, false,
1684                                   MDBuilder(*C).createBranchWeights(1, 100000));
1685     assert(cast<BranchInst>(TagCheckTerm)->isUnconditional());
1686     IRB.SetInsertPoint(TagCheckTerm);
1687     InsertBefore = TagCheckTerm;
1688   }
1689 
1690   Type *ShadowTy =
1691       IntegerType::get(*C, std::max(8U, TypeSize >> Mapping.Scale));
1692   Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
1693   Value *ShadowPtr = memToShadow(AddrLong, IRB);
1694   Value *CmpVal = Constant::getNullValue(ShadowTy);
1695   Value *ShadowValue =
1696       IRB.CreateLoad(ShadowTy, IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
1697 
1698   Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
1699   size_t Granularity = 1ULL << Mapping.Scale;
1700   Instruction *CrashTerm = nullptr;
1701 
1702   if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) {
1703     // We use branch weights for the slow path check, to indicate that the slow
1704     // path is rarely taken. This seems to be the case for SPEC benchmarks.
1705     Instruction *CheckTerm = SplitBlockAndInsertIfThen(
1706         Cmp, InsertBefore, false, MDBuilder(*C).createBranchWeights(1, 100000));
1707     assert(cast<BranchInst>(CheckTerm)->isUnconditional());
1708     BasicBlock *NextBB = CheckTerm->getSuccessor(0);
1709     IRB.SetInsertPoint(CheckTerm);
1710     Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
1711     if (Recover) {
1712       CrashTerm = SplitBlockAndInsertIfThen(Cmp2, CheckTerm, false);
1713     } else {
1714       BasicBlock *CrashBlock =
1715         BasicBlock::Create(*C, "", NextBB->getParent(), NextBB);
1716       CrashTerm = new UnreachableInst(*C, CrashBlock);
1717       BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
1718       ReplaceInstWithInst(CheckTerm, NewTerm);
1719     }
1720   } else {
1721     CrashTerm = SplitBlockAndInsertIfThen(Cmp, InsertBefore, !Recover);
1722   }
1723 
1724   Instruction *Crash = generateCrashCode(CrashTerm, AddrLong, IsWrite,
1725                                          AccessSizeIndex, SizeArgument, Exp);
1726   Crash->setDebugLoc(OrigIns->getDebugLoc());
1727 }
1728 
1729 // Instrument unusual size or unusual alignment.
1730 // We can not do it with a single check, so we do 1-byte check for the first
1731 // and the last bytes. We call __asan_report_*_n(addr, real_size) to be able
1732 // to report the actual access size.
1733 void AddressSanitizer::instrumentUnusualSizeOrAlignment(
1734     Instruction *I, Instruction *InsertBefore, Value *Addr, uint32_t TypeSize,
1735     bool IsWrite, Value *SizeArgument, bool UseCalls, uint32_t Exp) {
1736   IRBuilder<> IRB(InsertBefore);
1737   Value *Size = ConstantInt::get(IntptrTy, TypeSize / 8);
1738   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
1739   if (UseCalls) {
1740     if (Exp == 0)
1741       IRB.CreateCall(AsanMemoryAccessCallbackSized[IsWrite][0],
1742                      {AddrLong, Size});
1743     else
1744       IRB.CreateCall(AsanMemoryAccessCallbackSized[IsWrite][1],
1745                      {AddrLong, Size, ConstantInt::get(IRB.getInt32Ty(), Exp)});
1746   } else {
1747     Value *LastByte = IRB.CreateIntToPtr(
1748         IRB.CreateAdd(AddrLong, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)),
1749         Addr->getType());
1750     instrumentAddress(I, InsertBefore, Addr, 8, IsWrite, Size, false, Exp);
1751     instrumentAddress(I, InsertBefore, LastByte, 8, IsWrite, Size, false, Exp);
1752   }
1753 }
1754 
1755 void ModuleAddressSanitizer::poisonOneInitializer(Function &GlobalInit,
1756                                                   GlobalValue *ModuleName) {
1757   // Set up the arguments to our poison/unpoison functions.
1758   IRBuilder<> IRB(&GlobalInit.front(),
1759                   GlobalInit.front().getFirstInsertionPt());
1760 
1761   // Add a call to poison all external globals before the given function starts.
1762   Value *ModuleNameAddr = ConstantExpr::getPointerCast(ModuleName, IntptrTy);
1763   IRB.CreateCall(AsanPoisonGlobals, ModuleNameAddr);
1764 
1765   // Add calls to unpoison all globals before each return instruction.
1766   for (auto &BB : GlobalInit.getBasicBlockList())
1767     if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
1768       CallInst::Create(AsanUnpoisonGlobals, "", RI);
1769 }
1770 
1771 void ModuleAddressSanitizer::createInitializerPoisonCalls(
1772     Module &M, GlobalValue *ModuleName) {
1773   GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
1774   if (!GV)
1775     return;
1776 
1777   ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
1778   if (!CA)
1779     return;
1780 
1781   for (Use &OP : CA->operands()) {
1782     if (isa<ConstantAggregateZero>(OP)) continue;
1783     ConstantStruct *CS = cast<ConstantStruct>(OP);
1784 
1785     // Must have a function or null ptr.
1786     if (Function *F = dyn_cast<Function>(CS->getOperand(1))) {
1787       if (F->getName() == kAsanModuleCtorName) continue;
1788       ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
1789       // Don't instrument CTORs that will run before asan.module_ctor.
1790       if (Priority->getLimitedValue() <= GetCtorAndDtorPriority(TargetTriple))
1791         continue;
1792       poisonOneInitializer(*F, ModuleName);
1793     }
1794   }
1795 }
1796 
1797 bool ModuleAddressSanitizer::ShouldInstrumentGlobal(GlobalVariable *G) {
1798   Type *Ty = G->getValueType();
1799   LLVM_DEBUG(dbgs() << "GLOBAL: " << *G << "\n");
1800 
1801   // FIXME: Metadata should be attched directly to the global directly instead
1802   // of being added to llvm.asan.globals.
1803   if (GlobalsMD.get(G).IsBlacklisted) return false;
1804   if (!Ty->isSized()) return false;
1805   if (!G->hasInitializer()) return false;
1806   if (GlobalWasGeneratedByCompiler(G)) return false; // Our own globals.
1807   // Two problems with thread-locals:
1808   //   - The address of the main thread's copy can't be computed at link-time.
1809   //   - Need to poison all copies, not just the main thread's one.
1810   if (G->isThreadLocal()) return false;
1811   // For now, just ignore this Global if the alignment is large.
1812   if (G->getAlignment() > MinRedzoneSizeForGlobal()) return false;
1813 
1814   // For non-COFF targets, only instrument globals known to be defined by this
1815   // TU.
1816   // FIXME: We can instrument comdat globals on ELF if we are using the
1817   // GC-friendly metadata scheme.
1818   if (!TargetTriple.isOSBinFormatCOFF()) {
1819     if (!G->hasExactDefinition() || G->hasComdat())
1820       return false;
1821   } else {
1822     // On COFF, don't instrument non-ODR linkages.
1823     if (G->isInterposable())
1824       return false;
1825   }
1826 
1827   // If a comdat is present, it must have a selection kind that implies ODR
1828   // semantics: no duplicates, any, or exact match.
1829   if (Comdat *C = G->getComdat()) {
1830     switch (C->getSelectionKind()) {
1831     case Comdat::Any:
1832     case Comdat::ExactMatch:
1833     case Comdat::NoDuplicates:
1834       break;
1835     case Comdat::Largest:
1836     case Comdat::SameSize:
1837       return false;
1838     }
1839   }
1840 
1841   if (G->hasSection()) {
1842     StringRef Section = G->getSection();
1843 
1844     // Globals from llvm.metadata aren't emitted, do not instrument them.
1845     if (Section == "llvm.metadata") return false;
1846     // Do not instrument globals from special LLVM sections.
1847     if (Section.find("__llvm") != StringRef::npos || Section.find("__LLVM") != StringRef::npos) return false;
1848 
1849     // Do not instrument function pointers to initialization and termination
1850     // routines: dynamic linker will not properly handle redzones.
1851     if (Section.startswith(".preinit_array") ||
1852         Section.startswith(".init_array") ||
1853         Section.startswith(".fini_array")) {
1854       return false;
1855     }
1856 
1857     // On COFF, if the section name contains '$', it is highly likely that the
1858     // user is using section sorting to create an array of globals similar to
1859     // the way initialization callbacks are registered in .init_array and
1860     // .CRT$XCU. The ATL also registers things in .ATL$__[azm]. Adding redzones
1861     // to such globals is counterproductive, because the intent is that they
1862     // will form an array, and out-of-bounds accesses are expected.
1863     // See https://github.com/google/sanitizers/issues/305
1864     // and http://msdn.microsoft.com/en-US/en-en/library/bb918180(v=vs.120).aspx
1865     if (TargetTriple.isOSBinFormatCOFF() && Section.contains('$')) {
1866       LLVM_DEBUG(dbgs() << "Ignoring global in sorted section (contains '$'): "
1867                         << *G << "\n");
1868       return false;
1869     }
1870 
1871     if (TargetTriple.isOSBinFormatMachO()) {
1872       StringRef ParsedSegment, ParsedSection;
1873       unsigned TAA = 0, StubSize = 0;
1874       bool TAAParsed;
1875       std::string ErrorCode = MCSectionMachO::ParseSectionSpecifier(
1876           Section, ParsedSegment, ParsedSection, TAA, TAAParsed, StubSize);
1877       assert(ErrorCode.empty() && "Invalid section specifier.");
1878 
1879       // Ignore the globals from the __OBJC section. The ObjC runtime assumes
1880       // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
1881       // them.
1882       if (ParsedSegment == "__OBJC" ||
1883           (ParsedSegment == "__DATA" && ParsedSection.startswith("__objc_"))) {
1884         LLVM_DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G << "\n");
1885         return false;
1886       }
1887       // See https://github.com/google/sanitizers/issues/32
1888       // Constant CFString instances are compiled in the following way:
1889       //  -- the string buffer is emitted into
1890       //     __TEXT,__cstring,cstring_literals
1891       //  -- the constant NSConstantString structure referencing that buffer
1892       //     is placed into __DATA,__cfstring
1893       // Therefore there's no point in placing redzones into __DATA,__cfstring.
1894       // Moreover, it causes the linker to crash on OS X 10.7
1895       if (ParsedSegment == "__DATA" && ParsedSection == "__cfstring") {
1896         LLVM_DEBUG(dbgs() << "Ignoring CFString: " << *G << "\n");
1897         return false;
1898       }
1899       // The linker merges the contents of cstring_literals and removes the
1900       // trailing zeroes.
1901       if (ParsedSegment == "__TEXT" && (TAA & MachO::S_CSTRING_LITERALS)) {
1902         LLVM_DEBUG(dbgs() << "Ignoring a cstring literal: " << *G << "\n");
1903         return false;
1904       }
1905     }
1906   }
1907 
1908   return true;
1909 }
1910 
1911 // On Mach-O platforms, we emit global metadata in a separate section of the
1912 // binary in order to allow the linker to properly dead strip. This is only
1913 // supported on recent versions of ld64.
1914 bool ModuleAddressSanitizer::ShouldUseMachOGlobalsSection() const {
1915   if (!TargetTriple.isOSBinFormatMachO())
1916     return false;
1917 
1918   if (TargetTriple.isMacOSX() && !TargetTriple.isMacOSXVersionLT(10, 11))
1919     return true;
1920   if (TargetTriple.isiOS() /* or tvOS */ && !TargetTriple.isOSVersionLT(9))
1921     return true;
1922   if (TargetTriple.isWatchOS() && !TargetTriple.isOSVersionLT(2))
1923     return true;
1924 
1925   return false;
1926 }
1927 
1928 StringRef ModuleAddressSanitizer::getGlobalMetadataSection() const {
1929   switch (TargetTriple.getObjectFormat()) {
1930   case Triple::COFF:  return ".ASAN$GL";
1931   case Triple::ELF:   return "asan_globals";
1932   case Triple::MachO: return "__DATA,__asan_globals,regular";
1933   case Triple::Wasm:
1934   case Triple::XCOFF:
1935     report_fatal_error(
1936         "ModuleAddressSanitizer not implemented for object file format.");
1937   case Triple::UnknownObjectFormat:
1938     break;
1939   }
1940   llvm_unreachable("unsupported object format");
1941 }
1942 
1943 void ModuleAddressSanitizer::initializeCallbacks(Module &M) {
1944   IRBuilder<> IRB(*C);
1945 
1946   // Declare our poisoning and unpoisoning functions.
1947   AsanPoisonGlobals =
1948       M.getOrInsertFunction(kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy);
1949   AsanUnpoisonGlobals =
1950       M.getOrInsertFunction(kAsanUnpoisonGlobalsName, IRB.getVoidTy());
1951 
1952   // Declare functions that register/unregister globals.
1953   AsanRegisterGlobals = M.getOrInsertFunction(
1954       kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy);
1955   AsanUnregisterGlobals = M.getOrInsertFunction(
1956       kAsanUnregisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy);
1957 
1958   // Declare the functions that find globals in a shared object and then invoke
1959   // the (un)register function on them.
1960   AsanRegisterImageGlobals = M.getOrInsertFunction(
1961       kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy);
1962   AsanUnregisterImageGlobals = M.getOrInsertFunction(
1963       kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy);
1964 
1965   AsanRegisterElfGlobals =
1966       M.getOrInsertFunction(kAsanRegisterElfGlobalsName, IRB.getVoidTy(),
1967                             IntptrTy, IntptrTy, IntptrTy);
1968   AsanUnregisterElfGlobals =
1969       M.getOrInsertFunction(kAsanUnregisterElfGlobalsName, IRB.getVoidTy(),
1970                             IntptrTy, IntptrTy, IntptrTy);
1971 }
1972 
1973 // Put the metadata and the instrumented global in the same group. This ensures
1974 // that the metadata is discarded if the instrumented global is discarded.
1975 void ModuleAddressSanitizer::SetComdatForGlobalMetadata(
1976     GlobalVariable *G, GlobalVariable *Metadata, StringRef InternalSuffix) {
1977   Module &M = *G->getParent();
1978   Comdat *C = G->getComdat();
1979   if (!C) {
1980     if (!G->hasName()) {
1981       // If G is unnamed, it must be internal. Give it an artificial name
1982       // so we can put it in a comdat.
1983       assert(G->hasLocalLinkage());
1984       G->setName(Twine(kAsanGenPrefix) + "_anon_global");
1985     }
1986 
1987     if (!InternalSuffix.empty() && G->hasLocalLinkage()) {
1988       std::string Name = G->getName();
1989       Name += InternalSuffix;
1990       C = M.getOrInsertComdat(Name);
1991     } else {
1992       C = M.getOrInsertComdat(G->getName());
1993     }
1994 
1995     // Make this IMAGE_COMDAT_SELECT_NODUPLICATES on COFF. Also upgrade private
1996     // linkage to internal linkage so that a symbol table entry is emitted. This
1997     // is necessary in order to create the comdat group.
1998     if (TargetTriple.isOSBinFormatCOFF()) {
1999       C->setSelectionKind(Comdat::NoDuplicates);
2000       if (G->hasPrivateLinkage())
2001         G->setLinkage(GlobalValue::InternalLinkage);
2002     }
2003     G->setComdat(C);
2004   }
2005 
2006   assert(G->hasComdat());
2007   Metadata->setComdat(G->getComdat());
2008 }
2009 
2010 // Create a separate metadata global and put it in the appropriate ASan
2011 // global registration section.
2012 GlobalVariable *
2013 ModuleAddressSanitizer::CreateMetadataGlobal(Module &M, Constant *Initializer,
2014                                              StringRef OriginalName) {
2015   auto Linkage = TargetTriple.isOSBinFormatMachO()
2016                      ? GlobalVariable::InternalLinkage
2017                      : GlobalVariable::PrivateLinkage;
2018   GlobalVariable *Metadata = new GlobalVariable(
2019       M, Initializer->getType(), false, Linkage, Initializer,
2020       Twine("__asan_global_") + GlobalValue::dropLLVMManglingEscape(OriginalName));
2021   Metadata->setSection(getGlobalMetadataSection());
2022   return Metadata;
2023 }
2024 
2025 IRBuilder<> ModuleAddressSanitizer::CreateAsanModuleDtor(Module &M) {
2026   AsanDtorFunction =
2027       Function::Create(FunctionType::get(Type::getVoidTy(*C), false),
2028                        GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
2029   BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
2030 
2031   return IRBuilder<>(ReturnInst::Create(*C, AsanDtorBB));
2032 }
2033 
2034 void ModuleAddressSanitizer::InstrumentGlobalsCOFF(
2035     IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2036     ArrayRef<Constant *> MetadataInitializers) {
2037   assert(ExtendedGlobals.size() == MetadataInitializers.size());
2038   auto &DL = M.getDataLayout();
2039 
2040   for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
2041     Constant *Initializer = MetadataInitializers[i];
2042     GlobalVariable *G = ExtendedGlobals[i];
2043     GlobalVariable *Metadata =
2044         CreateMetadataGlobal(M, Initializer, G->getName());
2045 
2046     // The MSVC linker always inserts padding when linking incrementally. We
2047     // cope with that by aligning each struct to its size, which must be a power
2048     // of two.
2049     unsigned SizeOfGlobalStruct = DL.getTypeAllocSize(Initializer->getType());
2050     assert(isPowerOf2_32(SizeOfGlobalStruct) &&
2051            "global metadata will not be padded appropriately");
2052     Metadata->setAlignment(SizeOfGlobalStruct);
2053 
2054     SetComdatForGlobalMetadata(G, Metadata, "");
2055   }
2056 }
2057 
2058 void ModuleAddressSanitizer::InstrumentGlobalsELF(
2059     IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2060     ArrayRef<Constant *> MetadataInitializers,
2061     const std::string &UniqueModuleId) {
2062   assert(ExtendedGlobals.size() == MetadataInitializers.size());
2063 
2064   SmallVector<GlobalValue *, 16> MetadataGlobals(ExtendedGlobals.size());
2065   for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
2066     GlobalVariable *G = ExtendedGlobals[i];
2067     GlobalVariable *Metadata =
2068         CreateMetadataGlobal(M, MetadataInitializers[i], G->getName());
2069     MDNode *MD = MDNode::get(M.getContext(), ValueAsMetadata::get(G));
2070     Metadata->setMetadata(LLVMContext::MD_associated, MD);
2071     MetadataGlobals[i] = Metadata;
2072 
2073     SetComdatForGlobalMetadata(G, Metadata, UniqueModuleId);
2074   }
2075 
2076   // Update llvm.compiler.used, adding the new metadata globals. This is
2077   // needed so that during LTO these variables stay alive.
2078   if (!MetadataGlobals.empty())
2079     appendToCompilerUsed(M, MetadataGlobals);
2080 
2081   // RegisteredFlag serves two purposes. First, we can pass it to dladdr()
2082   // to look up the loaded image that contains it. Second, we can store in it
2083   // whether registration has already occurred, to prevent duplicate
2084   // registration.
2085   //
2086   // Common linkage ensures that there is only one global per shared library.
2087   GlobalVariable *RegisteredFlag = new GlobalVariable(
2088       M, IntptrTy, false, GlobalVariable::CommonLinkage,
2089       ConstantInt::get(IntptrTy, 0), kAsanGlobalsRegisteredFlagName);
2090   RegisteredFlag->setVisibility(GlobalVariable::HiddenVisibility);
2091 
2092   // Create start and stop symbols.
2093   GlobalVariable *StartELFMetadata = new GlobalVariable(
2094       M, IntptrTy, false, GlobalVariable::ExternalWeakLinkage, nullptr,
2095       "__start_" + getGlobalMetadataSection());
2096   StartELFMetadata->setVisibility(GlobalVariable::HiddenVisibility);
2097   GlobalVariable *StopELFMetadata = new GlobalVariable(
2098       M, IntptrTy, false, GlobalVariable::ExternalWeakLinkage, nullptr,
2099       "__stop_" + getGlobalMetadataSection());
2100   StopELFMetadata->setVisibility(GlobalVariable::HiddenVisibility);
2101 
2102   // Create a call to register the globals with the runtime.
2103   IRB.CreateCall(AsanRegisterElfGlobals,
2104                  {IRB.CreatePointerCast(RegisteredFlag, IntptrTy),
2105                   IRB.CreatePointerCast(StartELFMetadata, IntptrTy),
2106                   IRB.CreatePointerCast(StopELFMetadata, IntptrTy)});
2107 
2108   // We also need to unregister globals at the end, e.g., when a shared library
2109   // gets closed.
2110   IRBuilder<> IRB_Dtor = CreateAsanModuleDtor(M);
2111   IRB_Dtor.CreateCall(AsanUnregisterElfGlobals,
2112                       {IRB.CreatePointerCast(RegisteredFlag, IntptrTy),
2113                        IRB.CreatePointerCast(StartELFMetadata, IntptrTy),
2114                        IRB.CreatePointerCast(StopELFMetadata, IntptrTy)});
2115 }
2116 
2117 void ModuleAddressSanitizer::InstrumentGlobalsMachO(
2118     IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2119     ArrayRef<Constant *> MetadataInitializers) {
2120   assert(ExtendedGlobals.size() == MetadataInitializers.size());
2121 
2122   // On recent Mach-O platforms, use a structure which binds the liveness of
2123   // the global variable to the metadata struct. Keep the list of "Liveness" GV
2124   // created to be added to llvm.compiler.used
2125   StructType *LivenessTy = StructType::get(IntptrTy, IntptrTy);
2126   SmallVector<GlobalValue *, 16> LivenessGlobals(ExtendedGlobals.size());
2127 
2128   for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
2129     Constant *Initializer = MetadataInitializers[i];
2130     GlobalVariable *G = ExtendedGlobals[i];
2131     GlobalVariable *Metadata =
2132         CreateMetadataGlobal(M, Initializer, G->getName());
2133 
2134     // On recent Mach-O platforms, we emit the global metadata in a way that
2135     // allows the linker to properly strip dead globals.
2136     auto LivenessBinder =
2137         ConstantStruct::get(LivenessTy, Initializer->getAggregateElement(0u),
2138                             ConstantExpr::getPointerCast(Metadata, IntptrTy));
2139     GlobalVariable *Liveness = new GlobalVariable(
2140         M, LivenessTy, false, GlobalVariable::InternalLinkage, LivenessBinder,
2141         Twine("__asan_binder_") + G->getName());
2142     Liveness->setSection("__DATA,__asan_liveness,regular,live_support");
2143     LivenessGlobals[i] = Liveness;
2144   }
2145 
2146   // Update llvm.compiler.used, adding the new liveness globals. This is
2147   // needed so that during LTO these variables stay alive. The alternative
2148   // would be to have the linker handling the LTO symbols, but libLTO
2149   // current API does not expose access to the section for each symbol.
2150   if (!LivenessGlobals.empty())
2151     appendToCompilerUsed(M, LivenessGlobals);
2152 
2153   // RegisteredFlag serves two purposes. First, we can pass it to dladdr()
2154   // to look up the loaded image that contains it. Second, we can store in it
2155   // whether registration has already occurred, to prevent duplicate
2156   // registration.
2157   //
2158   // common linkage ensures that there is only one global per shared library.
2159   GlobalVariable *RegisteredFlag = new GlobalVariable(
2160       M, IntptrTy, false, GlobalVariable::CommonLinkage,
2161       ConstantInt::get(IntptrTy, 0), kAsanGlobalsRegisteredFlagName);
2162   RegisteredFlag->setVisibility(GlobalVariable::HiddenVisibility);
2163 
2164   IRB.CreateCall(AsanRegisterImageGlobals,
2165                  {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
2166 
2167   // We also need to unregister globals at the end, e.g., when a shared library
2168   // gets closed.
2169   IRBuilder<> IRB_Dtor = CreateAsanModuleDtor(M);
2170   IRB_Dtor.CreateCall(AsanUnregisterImageGlobals,
2171                       {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
2172 }
2173 
2174 void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray(
2175     IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2176     ArrayRef<Constant *> MetadataInitializers) {
2177   assert(ExtendedGlobals.size() == MetadataInitializers.size());
2178   unsigned N = ExtendedGlobals.size();
2179   assert(N > 0);
2180 
2181   // On platforms that don't have a custom metadata section, we emit an array
2182   // of global metadata structures.
2183   ArrayType *ArrayOfGlobalStructTy =
2184       ArrayType::get(MetadataInitializers[0]->getType(), N);
2185   auto AllGlobals = new GlobalVariable(
2186       M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage,
2187       ConstantArray::get(ArrayOfGlobalStructTy, MetadataInitializers), "");
2188   if (Mapping.Scale > 3)
2189     AllGlobals->setAlignment(1ULL << Mapping.Scale);
2190 
2191   IRB.CreateCall(AsanRegisterGlobals,
2192                  {IRB.CreatePointerCast(AllGlobals, IntptrTy),
2193                   ConstantInt::get(IntptrTy, N)});
2194 
2195   // We also need to unregister globals at the end, e.g., when a shared library
2196   // gets closed.
2197   IRBuilder<> IRB_Dtor = CreateAsanModuleDtor(M);
2198   IRB_Dtor.CreateCall(AsanUnregisterGlobals,
2199                       {IRB.CreatePointerCast(AllGlobals, IntptrTy),
2200                        ConstantInt::get(IntptrTy, N)});
2201 }
2202 
2203 // This function replaces all global variables with new variables that have
2204 // trailing redzones. It also creates a function that poisons
2205 // redzones and inserts this function into llvm.global_ctors.
2206 // Sets *CtorComdat to true if the global registration code emitted into the
2207 // asan constructor is comdat-compatible.
2208 bool ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M,
2209                                                bool *CtorComdat) {
2210   *CtorComdat = false;
2211 
2212   SmallVector<GlobalVariable *, 16> GlobalsToChange;
2213 
2214   for (auto &G : M.globals()) {
2215     if (ShouldInstrumentGlobal(&G)) GlobalsToChange.push_back(&G);
2216   }
2217 
2218   size_t n = GlobalsToChange.size();
2219   if (n == 0) {
2220     *CtorComdat = true;
2221     return false;
2222   }
2223 
2224   auto &DL = M.getDataLayout();
2225 
2226   // A global is described by a structure
2227   //   size_t beg;
2228   //   size_t size;
2229   //   size_t size_with_redzone;
2230   //   const char *name;
2231   //   const char *module_name;
2232   //   size_t has_dynamic_init;
2233   //   void *source_location;
2234   //   size_t odr_indicator;
2235   // We initialize an array of such structures and pass it to a run-time call.
2236   StructType *GlobalStructTy =
2237       StructType::get(IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy,
2238                       IntptrTy, IntptrTy, IntptrTy);
2239   SmallVector<GlobalVariable *, 16> NewGlobals(n);
2240   SmallVector<Constant *, 16> Initializers(n);
2241 
2242   bool HasDynamicallyInitializedGlobals = false;
2243 
2244   // We shouldn't merge same module names, as this string serves as unique
2245   // module ID in runtime.
2246   GlobalVariable *ModuleName = createPrivateGlobalForString(
2247       M, M.getModuleIdentifier(), /*AllowMerging*/ false, kAsanGenPrefix);
2248 
2249   for (size_t i = 0; i < n; i++) {
2250     static const uint64_t kMaxGlobalRedzone = 1 << 18;
2251     GlobalVariable *G = GlobalsToChange[i];
2252 
2253     // FIXME: Metadata should be attched directly to the global directly instead
2254     // of being added to llvm.asan.globals.
2255     auto MD = GlobalsMD.get(G);
2256     StringRef NameForGlobal = G->getName();
2257     // Create string holding the global name (use global name from metadata
2258     // if it's available, otherwise just write the name of global variable).
2259     GlobalVariable *Name = createPrivateGlobalForString(
2260         M, MD.Name.empty() ? NameForGlobal : MD.Name,
2261         /*AllowMerging*/ true, kAsanGenPrefix);
2262 
2263     Type *Ty = G->getValueType();
2264     uint64_t SizeInBytes = DL.getTypeAllocSize(Ty);
2265     uint64_t MinRZ = MinRedzoneSizeForGlobal();
2266     // MinRZ <= RZ <= kMaxGlobalRedzone
2267     // and trying to make RZ to be ~ 1/4 of SizeInBytes.
2268     uint64_t RZ = std::max(
2269         MinRZ, std::min(kMaxGlobalRedzone, (SizeInBytes / MinRZ / 4) * MinRZ));
2270     uint64_t RightRedzoneSize = RZ;
2271     // Round up to MinRZ
2272     if (SizeInBytes % MinRZ) RightRedzoneSize += MinRZ - (SizeInBytes % MinRZ);
2273     assert(((RightRedzoneSize + SizeInBytes) % MinRZ) == 0);
2274     Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
2275 
2276     StructType *NewTy = StructType::get(Ty, RightRedZoneTy);
2277     Constant *NewInitializer = ConstantStruct::get(
2278         NewTy, G->getInitializer(), Constant::getNullValue(RightRedZoneTy));
2279 
2280     // Create a new global variable with enough space for a redzone.
2281     GlobalValue::LinkageTypes Linkage = G->getLinkage();
2282     if (G->isConstant() && Linkage == GlobalValue::PrivateLinkage)
2283       Linkage = GlobalValue::InternalLinkage;
2284     GlobalVariable *NewGlobal =
2285         new GlobalVariable(M, NewTy, G->isConstant(), Linkage, NewInitializer,
2286                            "", G, G->getThreadLocalMode());
2287     NewGlobal->copyAttributesFrom(G);
2288     NewGlobal->setComdat(G->getComdat());
2289     NewGlobal->setAlignment(MinRZ);
2290     // Don't fold globals with redzones. ODR violation detector and redzone
2291     // poisoning implicitly creates a dependence on the global's address, so it
2292     // is no longer valid for it to be marked unnamed_addr.
2293     NewGlobal->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
2294 
2295     // Move null-terminated C strings to "__asan_cstring" section on Darwin.
2296     if (TargetTriple.isOSBinFormatMachO() && !G->hasSection() &&
2297         G->isConstant()) {
2298       auto Seq = dyn_cast<ConstantDataSequential>(G->getInitializer());
2299       if (Seq && Seq->isCString())
2300         NewGlobal->setSection("__TEXT,__asan_cstring,regular");
2301     }
2302 
2303     // Transfer the debug info.  The payload starts at offset zero so we can
2304     // copy the debug info over as is.
2305     SmallVector<DIGlobalVariableExpression *, 1> GVs;
2306     G->getDebugInfo(GVs);
2307     for (auto *GV : GVs)
2308       NewGlobal->addDebugInfo(GV);
2309 
2310     Value *Indices2[2];
2311     Indices2[0] = IRB.getInt32(0);
2312     Indices2[1] = IRB.getInt32(0);
2313 
2314     G->replaceAllUsesWith(
2315         ConstantExpr::getGetElementPtr(NewTy, NewGlobal, Indices2, true));
2316     NewGlobal->takeName(G);
2317     G->eraseFromParent();
2318     NewGlobals[i] = NewGlobal;
2319 
2320     Constant *SourceLoc;
2321     if (!MD.SourceLoc.empty()) {
2322       auto SourceLocGlobal = createPrivateGlobalForSourceLoc(M, MD.SourceLoc);
2323       SourceLoc = ConstantExpr::getPointerCast(SourceLocGlobal, IntptrTy);
2324     } else {
2325       SourceLoc = ConstantInt::get(IntptrTy, 0);
2326     }
2327 
2328     Constant *ODRIndicator = ConstantExpr::getNullValue(IRB.getInt8PtrTy());
2329     GlobalValue *InstrumentedGlobal = NewGlobal;
2330 
2331     bool CanUsePrivateAliases =
2332         TargetTriple.isOSBinFormatELF() || TargetTriple.isOSBinFormatMachO() ||
2333         TargetTriple.isOSBinFormatWasm();
2334     if (CanUsePrivateAliases && UsePrivateAlias) {
2335       // Create local alias for NewGlobal to avoid crash on ODR between
2336       // instrumented and non-instrumented libraries.
2337       InstrumentedGlobal =
2338           GlobalAlias::create(GlobalValue::PrivateLinkage, "", NewGlobal);
2339     }
2340 
2341     // ODR should not happen for local linkage.
2342     if (NewGlobal->hasLocalLinkage()) {
2343       ODRIndicator = ConstantExpr::getIntToPtr(ConstantInt::get(IntptrTy, -1),
2344                                                IRB.getInt8PtrTy());
2345     } else if (UseOdrIndicator) {
2346       // With local aliases, we need to provide another externally visible
2347       // symbol __odr_asan_XXX to detect ODR violation.
2348       auto *ODRIndicatorSym =
2349           new GlobalVariable(M, IRB.getInt8Ty(), false, Linkage,
2350                              Constant::getNullValue(IRB.getInt8Ty()),
2351                              kODRGenPrefix + NameForGlobal, nullptr,
2352                              NewGlobal->getThreadLocalMode());
2353 
2354       // Set meaningful attributes for indicator symbol.
2355       ODRIndicatorSym->setVisibility(NewGlobal->getVisibility());
2356       ODRIndicatorSym->setDLLStorageClass(NewGlobal->getDLLStorageClass());
2357       ODRIndicatorSym->setAlignment(1);
2358       ODRIndicator = ODRIndicatorSym;
2359     }
2360 
2361     Constant *Initializer = ConstantStruct::get(
2362         GlobalStructTy,
2363         ConstantExpr::getPointerCast(InstrumentedGlobal, IntptrTy),
2364         ConstantInt::get(IntptrTy, SizeInBytes),
2365         ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
2366         ConstantExpr::getPointerCast(Name, IntptrTy),
2367         ConstantExpr::getPointerCast(ModuleName, IntptrTy),
2368         ConstantInt::get(IntptrTy, MD.IsDynInit), SourceLoc,
2369         ConstantExpr::getPointerCast(ODRIndicator, IntptrTy));
2370 
2371     if (ClInitializers && MD.IsDynInit) HasDynamicallyInitializedGlobals = true;
2372 
2373     LLVM_DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
2374 
2375     Initializers[i] = Initializer;
2376   }
2377 
2378   // Add instrumented globals to llvm.compiler.used list to avoid LTO from
2379   // ConstantMerge'ing them.
2380   SmallVector<GlobalValue *, 16> GlobalsToAddToUsedList;
2381   for (size_t i = 0; i < n; i++) {
2382     GlobalVariable *G = NewGlobals[i];
2383     if (G->getName().empty()) continue;
2384     GlobalsToAddToUsedList.push_back(G);
2385   }
2386   appendToCompilerUsed(M, ArrayRef<GlobalValue *>(GlobalsToAddToUsedList));
2387 
2388   std::string ELFUniqueModuleId =
2389       (UseGlobalsGC && TargetTriple.isOSBinFormatELF()) ? getUniqueModuleId(&M)
2390                                                         : "";
2391 
2392   if (!ELFUniqueModuleId.empty()) {
2393     InstrumentGlobalsELF(IRB, M, NewGlobals, Initializers, ELFUniqueModuleId);
2394     *CtorComdat = true;
2395   } else if (UseGlobalsGC && TargetTriple.isOSBinFormatCOFF()) {
2396     InstrumentGlobalsCOFF(IRB, M, NewGlobals, Initializers);
2397   } else if (UseGlobalsGC && ShouldUseMachOGlobalsSection()) {
2398     InstrumentGlobalsMachO(IRB, M, NewGlobals, Initializers);
2399   } else {
2400     InstrumentGlobalsWithMetadataArray(IRB, M, NewGlobals, Initializers);
2401   }
2402 
2403   // Create calls for poisoning before initializers run and unpoisoning after.
2404   if (HasDynamicallyInitializedGlobals)
2405     createInitializerPoisonCalls(M, ModuleName);
2406 
2407   LLVM_DEBUG(dbgs() << M);
2408   return true;
2409 }
2410 
2411 int ModuleAddressSanitizer::GetAsanVersion(const Module &M) const {
2412   int LongSize = M.getDataLayout().getPointerSizeInBits();
2413   bool isAndroid = Triple(M.getTargetTriple()).isAndroid();
2414   int Version = 8;
2415   // 32-bit Android is one version ahead because of the switch to dynamic
2416   // shadow.
2417   Version += (LongSize == 32 && isAndroid);
2418   return Version;
2419 }
2420 
2421 bool ModuleAddressSanitizer::instrumentModule(Module &M) {
2422   initializeCallbacks(M);
2423 
2424   if (CompileKernel)
2425     return false;
2426 
2427   // Create a module constructor. A destructor is created lazily because not all
2428   // platforms, and not all modules need it.
2429   std::string VersionCheckName =
2430       kAsanVersionCheckNamePrefix + std::to_string(GetAsanVersion(M));
2431   std::tie(AsanCtorFunction, std::ignore) = createSanitizerCtorAndInitFunctions(
2432       M, kAsanModuleCtorName, kAsanInitName, /*InitArgTypes=*/{},
2433       /*InitArgs=*/{}, VersionCheckName);
2434 
2435   bool CtorComdat = true;
2436   bool Changed = false;
2437   // TODO(glider): temporarily disabled globals instrumentation for KASan.
2438   if (ClGlobals) {
2439     IRBuilder<> IRB(AsanCtorFunction->getEntryBlock().getTerminator());
2440     Changed |= InstrumentGlobals(IRB, M, &CtorComdat);
2441   }
2442 
2443   const uint64_t Priority = GetCtorAndDtorPriority(TargetTriple);
2444 
2445   // Put the constructor and destructor in comdat if both
2446   // (1) global instrumentation is not TU-specific
2447   // (2) target is ELF.
2448   if (UseCtorComdat && TargetTriple.isOSBinFormatELF() && CtorComdat) {
2449     AsanCtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleCtorName));
2450     appendToGlobalCtors(M, AsanCtorFunction, Priority, AsanCtorFunction);
2451     if (AsanDtorFunction) {
2452       AsanDtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleDtorName));
2453       appendToGlobalDtors(M, AsanDtorFunction, Priority, AsanDtorFunction);
2454     }
2455   } else {
2456     appendToGlobalCtors(M, AsanCtorFunction, Priority);
2457     if (AsanDtorFunction)
2458       appendToGlobalDtors(M, AsanDtorFunction, Priority);
2459   }
2460 
2461   return Changed;
2462 }
2463 
2464 void AddressSanitizer::initializeCallbacks(Module &M) {
2465   IRBuilder<> IRB(*C);
2466   // Create __asan_report* callbacks.
2467   // IsWrite, TypeSize and Exp are encoded in the function name.
2468   for (int Exp = 0; Exp < 2; Exp++) {
2469     for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
2470       const std::string TypeStr = AccessIsWrite ? "store" : "load";
2471       const std::string ExpStr = Exp ? "exp_" : "";
2472       const std::string EndingStr = Recover ? "_noabort" : "";
2473 
2474       SmallVector<Type *, 3> Args2 = {IntptrTy, IntptrTy};
2475       SmallVector<Type *, 2> Args1{1, IntptrTy};
2476       if (Exp) {
2477         Type *ExpType = Type::getInt32Ty(*C);
2478         Args2.push_back(ExpType);
2479         Args1.push_back(ExpType);
2480       }
2481       AsanErrorCallbackSized[AccessIsWrite][Exp] = M.getOrInsertFunction(
2482           kAsanReportErrorTemplate + ExpStr + TypeStr + "_n" + EndingStr,
2483           FunctionType::get(IRB.getVoidTy(), Args2, false));
2484 
2485       AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] = M.getOrInsertFunction(
2486           ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr,
2487           FunctionType::get(IRB.getVoidTy(), Args2, false));
2488 
2489       for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
2490            AccessSizeIndex++) {
2491         const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex);
2492         AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] =
2493             M.getOrInsertFunction(
2494                 kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr,
2495                 FunctionType::get(IRB.getVoidTy(), Args1, false));
2496 
2497         AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] =
2498             M.getOrInsertFunction(
2499                 ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr,
2500                 FunctionType::get(IRB.getVoidTy(), Args1, false));
2501       }
2502     }
2503   }
2504 
2505   const std::string MemIntrinCallbackPrefix =
2506       CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
2507   AsanMemmove = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memmove",
2508                                       IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
2509                                       IRB.getInt8PtrTy(), IntptrTy);
2510   AsanMemcpy = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memcpy",
2511                                      IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
2512                                      IRB.getInt8PtrTy(), IntptrTy);
2513   AsanMemset = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memset",
2514                                      IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
2515                                      IRB.getInt32Ty(), IntptrTy);
2516 
2517   AsanHandleNoReturnFunc =
2518       M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy());
2519 
2520   AsanPtrCmpFunction =
2521       M.getOrInsertFunction(kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy);
2522   AsanPtrSubFunction =
2523       M.getOrInsertFunction(kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy);
2524   // We insert an empty inline asm after __asan_report* to avoid callback merge.
2525   EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
2526                             StringRef(""), StringRef(""),
2527                             /*hasSideEffects=*/true);
2528   if (Mapping.InGlobal)
2529     AsanShadowGlobal = M.getOrInsertGlobal("__asan_shadow",
2530                                            ArrayType::get(IRB.getInt8Ty(), 0));
2531 }
2532 
2533 bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
2534   // For each NSObject descendant having a +load method, this method is invoked
2535   // by the ObjC runtime before any of the static constructors is called.
2536   // Therefore we need to instrument such methods with a call to __asan_init
2537   // at the beginning in order to initialize our runtime before any access to
2538   // the shadow memory.
2539   // We cannot just ignore these methods, because they may call other
2540   // instrumented functions.
2541   if (F.getName().find(" load]") != std::string::npos) {
2542     FunctionCallee AsanInitFunction =
2543         declareSanitizerInitFunction(*F.getParent(), kAsanInitName, {});
2544     IRBuilder<> IRB(&F.front(), F.front().begin());
2545     IRB.CreateCall(AsanInitFunction, {});
2546     return true;
2547   }
2548   return false;
2549 }
2550 
2551 void AddressSanitizer::maybeInsertDynamicShadowAtFunctionEntry(Function &F) {
2552   // Generate code only when dynamic addressing is needed.
2553   if (Mapping.Offset != kDynamicShadowSentinel)
2554     return;
2555 
2556   IRBuilder<> IRB(&F.front().front());
2557   if (Mapping.InGlobal) {
2558     if (ClWithIfuncSuppressRemat) {
2559       // An empty inline asm with input reg == output reg.
2560       // An opaque pointer-to-int cast, basically.
2561       InlineAsm *Asm = InlineAsm::get(
2562           FunctionType::get(IntptrTy, {AsanShadowGlobal->getType()}, false),
2563           StringRef(""), StringRef("=r,0"),
2564           /*hasSideEffects=*/false);
2565       LocalDynamicShadow =
2566           IRB.CreateCall(Asm, {AsanShadowGlobal}, ".asan.shadow");
2567     } else {
2568       LocalDynamicShadow =
2569           IRB.CreatePointerCast(AsanShadowGlobal, IntptrTy, ".asan.shadow");
2570     }
2571   } else {
2572     Value *GlobalDynamicAddress = F.getParent()->getOrInsertGlobal(
2573         kAsanShadowMemoryDynamicAddress, IntptrTy);
2574     LocalDynamicShadow = IRB.CreateLoad(IntptrTy, GlobalDynamicAddress);
2575   }
2576 }
2577 
2578 void AddressSanitizer::markEscapedLocalAllocas(Function &F) {
2579   // Find the one possible call to llvm.localescape and pre-mark allocas passed
2580   // to it as uninteresting. This assumes we haven't started processing allocas
2581   // yet. This check is done up front because iterating the use list in
2582   // isInterestingAlloca would be algorithmically slower.
2583   assert(ProcessedAllocas.empty() && "must process localescape before allocas");
2584 
2585   // Try to get the declaration of llvm.localescape. If it's not in the module,
2586   // we can exit early.
2587   if (!F.getParent()->getFunction("llvm.localescape")) return;
2588 
2589   // Look for a call to llvm.localescape call in the entry block. It can't be in
2590   // any other block.
2591   for (Instruction &I : F.getEntryBlock()) {
2592     IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
2593     if (II && II->getIntrinsicID() == Intrinsic::localescape) {
2594       // We found a call. Mark all the allocas passed in as uninteresting.
2595       for (Value *Arg : II->arg_operands()) {
2596         AllocaInst *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts());
2597         assert(AI && AI->isStaticAlloca() &&
2598                "non-static alloca arg to localescape");
2599         ProcessedAllocas[AI] = false;
2600       }
2601       break;
2602     }
2603   }
2604 }
2605 
2606 bool AddressSanitizer::instrumentFunction(Function &F,
2607                                           const TargetLibraryInfo *TLI) {
2608   if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false;
2609   if (!ClDebugFunc.empty() && ClDebugFunc == F.getName()) return false;
2610   if (F.getName().startswith("__asan_")) return false;
2611 
2612   bool FunctionModified = false;
2613 
2614   // If needed, insert __asan_init before checking for SanitizeAddress attr.
2615   // This function needs to be called even if the function body is not
2616   // instrumented.
2617   if (maybeInsertAsanInitAtFunctionEntry(F))
2618     FunctionModified = true;
2619 
2620   // Leave if the function doesn't need instrumentation.
2621   if (!F.hasFnAttribute(Attribute::SanitizeAddress)) return FunctionModified;
2622 
2623   LLVM_DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
2624 
2625   initializeCallbacks(*F.getParent());
2626 
2627   FunctionStateRAII CleanupObj(this);
2628 
2629   maybeInsertDynamicShadowAtFunctionEntry(F);
2630 
2631   // We can't instrument allocas used with llvm.localescape. Only static allocas
2632   // can be passed to that intrinsic.
2633   markEscapedLocalAllocas(F);
2634 
2635   // We want to instrument every address only once per basic block (unless there
2636   // are calls between uses).
2637   SmallPtrSet<Value *, 16> TempsToInstrument;
2638   SmallVector<Instruction *, 16> ToInstrument;
2639   SmallVector<Instruction *, 8> NoReturnCalls;
2640   SmallVector<BasicBlock *, 16> AllBlocks;
2641   SmallVector<Instruction *, 16> PointerComparisonsOrSubtracts;
2642   int NumAllocas = 0;
2643   bool IsWrite;
2644   unsigned Alignment;
2645   uint64_t TypeSize;
2646 
2647   // Fill the set of memory operations to instrument.
2648   for (auto &BB : F) {
2649     AllBlocks.push_back(&BB);
2650     TempsToInstrument.clear();
2651     int NumInsnsPerBB = 0;
2652     for (auto &Inst : BB) {
2653       if (LooksLikeCodeInBug11395(&Inst)) return false;
2654       Value *MaybeMask = nullptr;
2655       if (Value *Addr = isInterestingMemoryAccess(&Inst, &IsWrite, &TypeSize,
2656                                                   &Alignment, &MaybeMask)) {
2657         if (ClOpt && ClOptSameTemp) {
2658           // If we have a mask, skip instrumentation if we've already
2659           // instrumented the full object. But don't add to TempsToInstrument
2660           // because we might get another load/store with a different mask.
2661           if (MaybeMask) {
2662             if (TempsToInstrument.count(Addr))
2663               continue; // We've seen this (whole) temp in the current BB.
2664           } else {
2665             if (!TempsToInstrument.insert(Addr).second)
2666               continue; // We've seen this temp in the current BB.
2667           }
2668         }
2669       } else if (((ClInvalidPointerPairs || ClInvalidPointerCmp) &&
2670                   isInterestingPointerComparison(&Inst)) ||
2671                  ((ClInvalidPointerPairs || ClInvalidPointerSub) &&
2672                   isInterestingPointerSubtraction(&Inst))) {
2673         PointerComparisonsOrSubtracts.push_back(&Inst);
2674         continue;
2675       } else if (isa<MemIntrinsic>(Inst)) {
2676         // ok, take it.
2677       } else {
2678         if (isa<AllocaInst>(Inst)) NumAllocas++;
2679         CallSite CS(&Inst);
2680         if (CS) {
2681           // A call inside BB.
2682           TempsToInstrument.clear();
2683           if (CS.doesNotReturn() && !CS->getMetadata("nosanitize"))
2684             NoReturnCalls.push_back(CS.getInstruction());
2685         }
2686         if (CallInst *CI = dyn_cast<CallInst>(&Inst))
2687           maybeMarkSanitizerLibraryCallNoBuiltin(CI, TLI);
2688         continue;
2689       }
2690       ToInstrument.push_back(&Inst);
2691       NumInsnsPerBB++;
2692       if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB) break;
2693     }
2694   }
2695 
2696   bool UseCalls =
2697       (ClInstrumentationWithCallsThreshold >= 0 &&
2698        ToInstrument.size() > (unsigned)ClInstrumentationWithCallsThreshold);
2699   const DataLayout &DL = F.getParent()->getDataLayout();
2700   ObjectSizeOpts ObjSizeOpts;
2701   ObjSizeOpts.RoundToAlign = true;
2702   ObjectSizeOffsetVisitor ObjSizeVis(DL, TLI, F.getContext(), ObjSizeOpts);
2703 
2704   // Instrument.
2705   int NumInstrumented = 0;
2706   for (auto Inst : ToInstrument) {
2707     if (ClDebugMin < 0 || ClDebugMax < 0 ||
2708         (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
2709       if (isInterestingMemoryAccess(Inst, &IsWrite, &TypeSize, &Alignment))
2710         instrumentMop(ObjSizeVis, Inst, UseCalls,
2711                       F.getParent()->getDataLayout());
2712       else
2713         instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
2714     }
2715     NumInstrumented++;
2716   }
2717 
2718   FunctionStackPoisoner FSP(F, *this);
2719   bool ChangedStack = FSP.runOnFunction();
2720 
2721   // We must unpoison the stack before NoReturn calls (throw, _exit, etc).
2722   // See e.g. https://github.com/google/sanitizers/issues/37
2723   for (auto CI : NoReturnCalls) {
2724     IRBuilder<> IRB(CI);
2725     IRB.CreateCall(AsanHandleNoReturnFunc, {});
2726   }
2727 
2728   for (auto Inst : PointerComparisonsOrSubtracts) {
2729     instrumentPointerComparisonOrSubtraction(Inst);
2730     NumInstrumented++;
2731   }
2732 
2733   if (NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty())
2734     FunctionModified = true;
2735 
2736   LLVM_DEBUG(dbgs() << "ASAN done instrumenting: " << FunctionModified << " "
2737                     << F << "\n");
2738 
2739   return FunctionModified;
2740 }
2741 
2742 // Workaround for bug 11395: we don't want to instrument stack in functions
2743 // with large assembly blobs (32-bit only), otherwise reg alloc may crash.
2744 // FIXME: remove once the bug 11395 is fixed.
2745 bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
2746   if (LongSize != 32) return false;
2747   CallInst *CI = dyn_cast<CallInst>(I);
2748   if (!CI || !CI->isInlineAsm()) return false;
2749   if (CI->getNumArgOperands() <= 5) return false;
2750   // We have inline assembly with quite a few arguments.
2751   return true;
2752 }
2753 
2754 void FunctionStackPoisoner::initializeCallbacks(Module &M) {
2755   IRBuilder<> IRB(*C);
2756   for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) {
2757     std::string Suffix = itostr(i);
2758     AsanStackMallocFunc[i] = M.getOrInsertFunction(
2759         kAsanStackMallocNameTemplate + Suffix, IntptrTy, IntptrTy);
2760     AsanStackFreeFunc[i] =
2761         M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix,
2762                               IRB.getVoidTy(), IntptrTy, IntptrTy);
2763   }
2764   if (ASan.UseAfterScope) {
2765     AsanPoisonStackMemoryFunc = M.getOrInsertFunction(
2766         kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy);
2767     AsanUnpoisonStackMemoryFunc = M.getOrInsertFunction(
2768         kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy);
2769   }
2770 
2771   for (size_t Val : {0x00, 0xf1, 0xf2, 0xf3, 0xf5, 0xf8}) {
2772     std::ostringstream Name;
2773     Name << kAsanSetShadowPrefix;
2774     Name << std::setw(2) << std::setfill('0') << std::hex << Val;
2775     AsanSetShadowFunc[Val] =
2776         M.getOrInsertFunction(Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy);
2777   }
2778 
2779   AsanAllocaPoisonFunc = M.getOrInsertFunction(
2780       kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy);
2781   AsanAllocasUnpoisonFunc = M.getOrInsertFunction(
2782       kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy);
2783 }
2784 
2785 void FunctionStackPoisoner::copyToShadowInline(ArrayRef<uint8_t> ShadowMask,
2786                                                ArrayRef<uint8_t> ShadowBytes,
2787                                                size_t Begin, size_t End,
2788                                                IRBuilder<> &IRB,
2789                                                Value *ShadowBase) {
2790   if (Begin >= End)
2791     return;
2792 
2793   const size_t LargestStoreSizeInBytes =
2794       std::min<size_t>(sizeof(uint64_t), ASan.LongSize / 8);
2795 
2796   const bool IsLittleEndian = F.getParent()->getDataLayout().isLittleEndian();
2797 
2798   // Poison given range in shadow using larges store size with out leading and
2799   // trailing zeros in ShadowMask. Zeros never change, so they need neither
2800   // poisoning nor up-poisoning. Still we don't mind if some of them get into a
2801   // middle of a store.
2802   for (size_t i = Begin; i < End;) {
2803     if (!ShadowMask[i]) {
2804       assert(!ShadowBytes[i]);
2805       ++i;
2806       continue;
2807     }
2808 
2809     size_t StoreSizeInBytes = LargestStoreSizeInBytes;
2810     // Fit store size into the range.
2811     while (StoreSizeInBytes > End - i)
2812       StoreSizeInBytes /= 2;
2813 
2814     // Minimize store size by trimming trailing zeros.
2815     for (size_t j = StoreSizeInBytes - 1; j && !ShadowMask[i + j]; --j) {
2816       while (j <= StoreSizeInBytes / 2)
2817         StoreSizeInBytes /= 2;
2818     }
2819 
2820     uint64_t Val = 0;
2821     for (size_t j = 0; j < StoreSizeInBytes; j++) {
2822       if (IsLittleEndian)
2823         Val |= (uint64_t)ShadowBytes[i + j] << (8 * j);
2824       else
2825         Val = (Val << 8) | ShadowBytes[i + j];
2826     }
2827 
2828     Value *Ptr = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
2829     Value *Poison = IRB.getIntN(StoreSizeInBytes * 8, Val);
2830     IRB.CreateAlignedStore(
2831         Poison, IRB.CreateIntToPtr(Ptr, Poison->getType()->getPointerTo()), 1);
2832 
2833     i += StoreSizeInBytes;
2834   }
2835 }
2836 
2837 void FunctionStackPoisoner::copyToShadow(ArrayRef<uint8_t> ShadowMask,
2838                                          ArrayRef<uint8_t> ShadowBytes,
2839                                          IRBuilder<> &IRB, Value *ShadowBase) {
2840   copyToShadow(ShadowMask, ShadowBytes, 0, ShadowMask.size(), IRB, ShadowBase);
2841 }
2842 
2843 void FunctionStackPoisoner::copyToShadow(ArrayRef<uint8_t> ShadowMask,
2844                                          ArrayRef<uint8_t> ShadowBytes,
2845                                          size_t Begin, size_t End,
2846                                          IRBuilder<> &IRB, Value *ShadowBase) {
2847   assert(ShadowMask.size() == ShadowBytes.size());
2848   size_t Done = Begin;
2849   for (size_t i = Begin, j = Begin + 1; i < End; i = j++) {
2850     if (!ShadowMask[i]) {
2851       assert(!ShadowBytes[i]);
2852       continue;
2853     }
2854     uint8_t Val = ShadowBytes[i];
2855     if (!AsanSetShadowFunc[Val])
2856       continue;
2857 
2858     // Skip same values.
2859     for (; j < End && ShadowMask[j] && Val == ShadowBytes[j]; ++j) {
2860     }
2861 
2862     if (j - i >= ClMaxInlinePoisoningSize) {
2863       copyToShadowInline(ShadowMask, ShadowBytes, Done, i, IRB, ShadowBase);
2864       IRB.CreateCall(AsanSetShadowFunc[Val],
2865                      {IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i)),
2866                       ConstantInt::get(IntptrTy, j - i)});
2867       Done = j;
2868     }
2869   }
2870 
2871   copyToShadowInline(ShadowMask, ShadowBytes, Done, End, IRB, ShadowBase);
2872 }
2873 
2874 // Fake stack allocator (asan_fake_stack.h) has 11 size classes
2875 // for every power of 2 from kMinStackMallocSize to kMaxAsanStackMallocSizeClass
2876 static int StackMallocSizeClass(uint64_t LocalStackSize) {
2877   assert(LocalStackSize <= kMaxStackMallocSize);
2878   uint64_t MaxSize = kMinStackMallocSize;
2879   for (int i = 0;; i++, MaxSize *= 2)
2880     if (LocalStackSize <= MaxSize) return i;
2881   llvm_unreachable("impossible LocalStackSize");
2882 }
2883 
2884 void FunctionStackPoisoner::copyArgsPassedByValToAllocas() {
2885   Instruction *CopyInsertPoint = &F.front().front();
2886   if (CopyInsertPoint == ASan.LocalDynamicShadow) {
2887     // Insert after the dynamic shadow location is determined
2888     CopyInsertPoint = CopyInsertPoint->getNextNode();
2889     assert(CopyInsertPoint);
2890   }
2891   IRBuilder<> IRB(CopyInsertPoint);
2892   const DataLayout &DL = F.getParent()->getDataLayout();
2893   for (Argument &Arg : F.args()) {
2894     if (Arg.hasByValAttr()) {
2895       Type *Ty = Arg.getType()->getPointerElementType();
2896       unsigned Align = Arg.getParamAlignment();
2897       if (Align == 0) Align = DL.getABITypeAlignment(Ty);
2898 
2899       AllocaInst *AI = IRB.CreateAlloca(
2900           Ty, nullptr,
2901           (Arg.hasName() ? Arg.getName() : "Arg" + Twine(Arg.getArgNo())) +
2902               ".byval");
2903       AI->setAlignment(Align);
2904       Arg.replaceAllUsesWith(AI);
2905 
2906       uint64_t AllocSize = DL.getTypeAllocSize(Ty);
2907       IRB.CreateMemCpy(AI, Align, &Arg, Align, AllocSize);
2908     }
2909   }
2910 }
2911 
2912 PHINode *FunctionStackPoisoner::createPHI(IRBuilder<> &IRB, Value *Cond,
2913                                           Value *ValueIfTrue,
2914                                           Instruction *ThenTerm,
2915                                           Value *ValueIfFalse) {
2916   PHINode *PHI = IRB.CreatePHI(IntptrTy, 2);
2917   BasicBlock *CondBlock = cast<Instruction>(Cond)->getParent();
2918   PHI->addIncoming(ValueIfFalse, CondBlock);
2919   BasicBlock *ThenBlock = ThenTerm->getParent();
2920   PHI->addIncoming(ValueIfTrue, ThenBlock);
2921   return PHI;
2922 }
2923 
2924 Value *FunctionStackPoisoner::createAllocaForLayout(
2925     IRBuilder<> &IRB, const ASanStackFrameLayout &L, bool Dynamic) {
2926   AllocaInst *Alloca;
2927   if (Dynamic) {
2928     Alloca = IRB.CreateAlloca(IRB.getInt8Ty(),
2929                               ConstantInt::get(IRB.getInt64Ty(), L.FrameSize),
2930                               "MyAlloca");
2931   } else {
2932     Alloca = IRB.CreateAlloca(ArrayType::get(IRB.getInt8Ty(), L.FrameSize),
2933                               nullptr, "MyAlloca");
2934     assert(Alloca->isStaticAlloca());
2935   }
2936   assert((ClRealignStack & (ClRealignStack - 1)) == 0);
2937   size_t FrameAlignment = std::max(L.FrameAlignment, (size_t)ClRealignStack);
2938   Alloca->setAlignment(FrameAlignment);
2939   return IRB.CreatePointerCast(Alloca, IntptrTy);
2940 }
2941 
2942 void FunctionStackPoisoner::createDynamicAllocasInitStorage() {
2943   BasicBlock &FirstBB = *F.begin();
2944   IRBuilder<> IRB(dyn_cast<Instruction>(FirstBB.begin()));
2945   DynamicAllocaLayout = IRB.CreateAlloca(IntptrTy, nullptr);
2946   IRB.CreateStore(Constant::getNullValue(IntptrTy), DynamicAllocaLayout);
2947   DynamicAllocaLayout->setAlignment(32);
2948 }
2949 
2950 void FunctionStackPoisoner::processDynamicAllocas() {
2951   if (!ClInstrumentDynamicAllocas || DynamicAllocaVec.empty()) {
2952     assert(DynamicAllocaPoisonCallVec.empty());
2953     return;
2954   }
2955 
2956   // Insert poison calls for lifetime intrinsics for dynamic allocas.
2957   for (const auto &APC : DynamicAllocaPoisonCallVec) {
2958     assert(APC.InsBefore);
2959     assert(APC.AI);
2960     assert(ASan.isInterestingAlloca(*APC.AI));
2961     assert(!APC.AI->isStaticAlloca());
2962 
2963     IRBuilder<> IRB(APC.InsBefore);
2964     poisonAlloca(APC.AI, APC.Size, IRB, APC.DoPoison);
2965     // Dynamic allocas will be unpoisoned unconditionally below in
2966     // unpoisonDynamicAllocas.
2967     // Flag that we need unpoison static allocas.
2968   }
2969 
2970   // Handle dynamic allocas.
2971   createDynamicAllocasInitStorage();
2972   for (auto &AI : DynamicAllocaVec)
2973     handleDynamicAllocaCall(AI);
2974   unpoisonDynamicAllocas();
2975 }
2976 
2977 void FunctionStackPoisoner::processStaticAllocas() {
2978   if (AllocaVec.empty()) {
2979     assert(StaticAllocaPoisonCallVec.empty());
2980     return;
2981   }
2982 
2983   int StackMallocIdx = -1;
2984   DebugLoc EntryDebugLocation;
2985   if (auto SP = F.getSubprogram())
2986     EntryDebugLocation = DebugLoc::get(SP->getScopeLine(), 0, SP);
2987 
2988   Instruction *InsBefore = AllocaVec[0];
2989   IRBuilder<> IRB(InsBefore);
2990   IRB.SetCurrentDebugLocation(EntryDebugLocation);
2991 
2992   // Make sure non-instrumented allocas stay in the entry block. Otherwise,
2993   // debug info is broken, because only entry-block allocas are treated as
2994   // regular stack slots.
2995   auto InsBeforeB = InsBefore->getParent();
2996   assert(InsBeforeB == &F.getEntryBlock());
2997   for (auto *AI : StaticAllocasToMoveUp)
2998     if (AI->getParent() == InsBeforeB)
2999       AI->moveBefore(InsBefore);
3000 
3001   // If we have a call to llvm.localescape, keep it in the entry block.
3002   if (LocalEscapeCall) LocalEscapeCall->moveBefore(InsBefore);
3003 
3004   SmallVector<ASanStackVariableDescription, 16> SVD;
3005   SVD.reserve(AllocaVec.size());
3006   for (AllocaInst *AI : AllocaVec) {
3007     ASanStackVariableDescription D = {AI->getName().data(),
3008                                       ASan.getAllocaSizeInBytes(*AI),
3009                                       0,
3010                                       AI->getAlignment(),
3011                                       AI,
3012                                       0,
3013                                       0};
3014     SVD.push_back(D);
3015   }
3016 
3017   // Minimal header size (left redzone) is 4 pointers,
3018   // i.e. 32 bytes on 64-bit platforms and 16 bytes in 32-bit platforms.
3019   size_t Granularity = 1ULL << Mapping.Scale;
3020   size_t MinHeaderSize = std::max((size_t)ASan.LongSize / 2, Granularity);
3021   const ASanStackFrameLayout &L =
3022       ComputeASanStackFrameLayout(SVD, Granularity, MinHeaderSize);
3023 
3024   // Build AllocaToSVDMap for ASanStackVariableDescription lookup.
3025   DenseMap<const AllocaInst *, ASanStackVariableDescription *> AllocaToSVDMap;
3026   for (auto &Desc : SVD)
3027     AllocaToSVDMap[Desc.AI] = &Desc;
3028 
3029   // Update SVD with information from lifetime intrinsics.
3030   for (const auto &APC : StaticAllocaPoisonCallVec) {
3031     assert(APC.InsBefore);
3032     assert(APC.AI);
3033     assert(ASan.isInterestingAlloca(*APC.AI));
3034     assert(APC.AI->isStaticAlloca());
3035 
3036     ASanStackVariableDescription &Desc = *AllocaToSVDMap[APC.AI];
3037     Desc.LifetimeSize = Desc.Size;
3038     if (const DILocation *FnLoc = EntryDebugLocation.get()) {
3039       if (const DILocation *LifetimeLoc = APC.InsBefore->getDebugLoc().get()) {
3040         if (LifetimeLoc->getFile() == FnLoc->getFile())
3041           if (unsigned Line = LifetimeLoc->getLine())
3042             Desc.Line = std::min(Desc.Line ? Desc.Line : Line, Line);
3043       }
3044     }
3045   }
3046 
3047   auto DescriptionString = ComputeASanStackFrameDescription(SVD);
3048   LLVM_DEBUG(dbgs() << DescriptionString << " --- " << L.FrameSize << "\n");
3049   uint64_t LocalStackSize = L.FrameSize;
3050   bool DoStackMalloc = ClUseAfterReturn && !ASan.CompileKernel &&
3051                        LocalStackSize <= kMaxStackMallocSize;
3052   bool DoDynamicAlloca = ClDynamicAllocaStack;
3053   // Don't do dynamic alloca or stack malloc if:
3054   // 1) There is inline asm: too often it makes assumptions on which registers
3055   //    are available.
3056   // 2) There is a returns_twice call (typically setjmp), which is
3057   //    optimization-hostile, and doesn't play well with introduced indirect
3058   //    register-relative calculation of local variable addresses.
3059   DoDynamicAlloca &= !HasNonEmptyInlineAsm && !HasReturnsTwiceCall;
3060   DoStackMalloc &= !HasNonEmptyInlineAsm && !HasReturnsTwiceCall;
3061 
3062   Value *StaticAlloca =
3063       DoDynamicAlloca ? nullptr : createAllocaForLayout(IRB, L, false);
3064 
3065   Value *FakeStack;
3066   Value *LocalStackBase;
3067   Value *LocalStackBaseAlloca;
3068   uint8_t DIExprFlags = DIExpression::ApplyOffset;
3069 
3070   if (DoStackMalloc) {
3071     LocalStackBaseAlloca =
3072         IRB.CreateAlloca(IntptrTy, nullptr, "asan_local_stack_base");
3073     // void *FakeStack = __asan_option_detect_stack_use_after_return
3074     //     ? __asan_stack_malloc_N(LocalStackSize)
3075     //     : nullptr;
3076     // void *LocalStackBase = (FakeStack) ? FakeStack : alloca(LocalStackSize);
3077     Constant *OptionDetectUseAfterReturn = F.getParent()->getOrInsertGlobal(
3078         kAsanOptionDetectUseAfterReturn, IRB.getInt32Ty());
3079     Value *UseAfterReturnIsEnabled = IRB.CreateICmpNE(
3080         IRB.CreateLoad(IRB.getInt32Ty(), OptionDetectUseAfterReturn),
3081         Constant::getNullValue(IRB.getInt32Ty()));
3082     Instruction *Term =
3083         SplitBlockAndInsertIfThen(UseAfterReturnIsEnabled, InsBefore, false);
3084     IRBuilder<> IRBIf(Term);
3085     IRBIf.SetCurrentDebugLocation(EntryDebugLocation);
3086     StackMallocIdx = StackMallocSizeClass(LocalStackSize);
3087     assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass);
3088     Value *FakeStackValue =
3089         IRBIf.CreateCall(AsanStackMallocFunc[StackMallocIdx],
3090                          ConstantInt::get(IntptrTy, LocalStackSize));
3091     IRB.SetInsertPoint(InsBefore);
3092     IRB.SetCurrentDebugLocation(EntryDebugLocation);
3093     FakeStack = createPHI(IRB, UseAfterReturnIsEnabled, FakeStackValue, Term,
3094                           ConstantInt::get(IntptrTy, 0));
3095 
3096     Value *NoFakeStack =
3097         IRB.CreateICmpEQ(FakeStack, Constant::getNullValue(IntptrTy));
3098     Term = SplitBlockAndInsertIfThen(NoFakeStack, InsBefore, false);
3099     IRBIf.SetInsertPoint(Term);
3100     IRBIf.SetCurrentDebugLocation(EntryDebugLocation);
3101     Value *AllocaValue =
3102         DoDynamicAlloca ? createAllocaForLayout(IRBIf, L, true) : StaticAlloca;
3103 
3104     IRB.SetInsertPoint(InsBefore);
3105     IRB.SetCurrentDebugLocation(EntryDebugLocation);
3106     LocalStackBase = createPHI(IRB, NoFakeStack, AllocaValue, Term, FakeStack);
3107     IRB.SetCurrentDebugLocation(EntryDebugLocation);
3108     IRB.CreateStore(LocalStackBase, LocalStackBaseAlloca);
3109     DIExprFlags |= DIExpression::DerefBefore;
3110   } else {
3111     // void *FakeStack = nullptr;
3112     // void *LocalStackBase = alloca(LocalStackSize);
3113     FakeStack = ConstantInt::get(IntptrTy, 0);
3114     LocalStackBase =
3115         DoDynamicAlloca ? createAllocaForLayout(IRB, L, true) : StaticAlloca;
3116     LocalStackBaseAlloca = LocalStackBase;
3117   }
3118 
3119   // Replace Alloca instructions with base+offset.
3120   for (const auto &Desc : SVD) {
3121     AllocaInst *AI = Desc.AI;
3122     replaceDbgDeclareForAlloca(AI, LocalStackBaseAlloca, DIB, DIExprFlags,
3123                                Desc.Offset);
3124     Value *NewAllocaPtr = IRB.CreateIntToPtr(
3125         IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Desc.Offset)),
3126         AI->getType());
3127     AI->replaceAllUsesWith(NewAllocaPtr);
3128   }
3129 
3130   // The left-most redzone has enough space for at least 4 pointers.
3131   // Write the Magic value to redzone[0].
3132   Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
3133   IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
3134                   BasePlus0);
3135   // Write the frame description constant to redzone[1].
3136   Value *BasePlus1 = IRB.CreateIntToPtr(
3137       IRB.CreateAdd(LocalStackBase,
3138                     ConstantInt::get(IntptrTy, ASan.LongSize / 8)),
3139       IntptrPtrTy);
3140   GlobalVariable *StackDescriptionGlobal =
3141       createPrivateGlobalForString(*F.getParent(), DescriptionString,
3142                                    /*AllowMerging*/ true, kAsanGenPrefix);
3143   Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal, IntptrTy);
3144   IRB.CreateStore(Description, BasePlus1);
3145   // Write the PC to redzone[2].
3146   Value *BasePlus2 = IRB.CreateIntToPtr(
3147       IRB.CreateAdd(LocalStackBase,
3148                     ConstantInt::get(IntptrTy, 2 * ASan.LongSize / 8)),
3149       IntptrPtrTy);
3150   IRB.CreateStore(IRB.CreatePointerCast(&F, IntptrTy), BasePlus2);
3151 
3152   const auto &ShadowAfterScope = GetShadowBytesAfterScope(SVD, L);
3153 
3154   // Poison the stack red zones at the entry.
3155   Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB);
3156   // As mask we must use most poisoned case: red zones and after scope.
3157   // As bytes we can use either the same or just red zones only.
3158   copyToShadow(ShadowAfterScope, ShadowAfterScope, IRB, ShadowBase);
3159 
3160   if (!StaticAllocaPoisonCallVec.empty()) {
3161     const auto &ShadowInScope = GetShadowBytes(SVD, L);
3162 
3163     // Poison static allocas near lifetime intrinsics.
3164     for (const auto &APC : StaticAllocaPoisonCallVec) {
3165       const ASanStackVariableDescription &Desc = *AllocaToSVDMap[APC.AI];
3166       assert(Desc.Offset % L.Granularity == 0);
3167       size_t Begin = Desc.Offset / L.Granularity;
3168       size_t End = Begin + (APC.Size + L.Granularity - 1) / L.Granularity;
3169 
3170       IRBuilder<> IRB(APC.InsBefore);
3171       copyToShadow(ShadowAfterScope,
3172                    APC.DoPoison ? ShadowAfterScope : ShadowInScope, Begin, End,
3173                    IRB, ShadowBase);
3174     }
3175   }
3176 
3177   SmallVector<uint8_t, 64> ShadowClean(ShadowAfterScope.size(), 0);
3178   SmallVector<uint8_t, 64> ShadowAfterReturn;
3179 
3180   // (Un)poison the stack before all ret instructions.
3181   for (auto Ret : RetVec) {
3182     IRBuilder<> IRBRet(Ret);
3183     // Mark the current frame as retired.
3184     IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
3185                        BasePlus0);
3186     if (DoStackMalloc) {
3187       assert(StackMallocIdx >= 0);
3188       // if FakeStack != 0  // LocalStackBase == FakeStack
3189       //     // In use-after-return mode, poison the whole stack frame.
3190       //     if StackMallocIdx <= 4
3191       //         // For small sizes inline the whole thing:
3192       //         memset(ShadowBase, kAsanStackAfterReturnMagic, ShadowSize);
3193       //         **SavedFlagPtr(FakeStack) = 0
3194       //     else
3195       //         __asan_stack_free_N(FakeStack, LocalStackSize)
3196       // else
3197       //     <This is not a fake stack; unpoison the redzones>
3198       Value *Cmp =
3199           IRBRet.CreateICmpNE(FakeStack, Constant::getNullValue(IntptrTy));
3200       Instruction *ThenTerm, *ElseTerm;
3201       SplitBlockAndInsertIfThenElse(Cmp, Ret, &ThenTerm, &ElseTerm);
3202 
3203       IRBuilder<> IRBPoison(ThenTerm);
3204       if (StackMallocIdx <= 4) {
3205         int ClassSize = kMinStackMallocSize << StackMallocIdx;
3206         ShadowAfterReturn.resize(ClassSize / L.Granularity,
3207                                  kAsanStackUseAfterReturnMagic);
3208         copyToShadow(ShadowAfterReturn, ShadowAfterReturn, IRBPoison,
3209                      ShadowBase);
3210         Value *SavedFlagPtrPtr = IRBPoison.CreateAdd(
3211             FakeStack,
3212             ConstantInt::get(IntptrTy, ClassSize - ASan.LongSize / 8));
3213         Value *SavedFlagPtr = IRBPoison.CreateLoad(
3214             IntptrTy, IRBPoison.CreateIntToPtr(SavedFlagPtrPtr, IntptrPtrTy));
3215         IRBPoison.CreateStore(
3216             Constant::getNullValue(IRBPoison.getInt8Ty()),
3217             IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getInt8PtrTy()));
3218       } else {
3219         // For larger frames call __asan_stack_free_*.
3220         IRBPoison.CreateCall(
3221             AsanStackFreeFunc[StackMallocIdx],
3222             {FakeStack, ConstantInt::get(IntptrTy, LocalStackSize)});
3223       }
3224 
3225       IRBuilder<> IRBElse(ElseTerm);
3226       copyToShadow(ShadowAfterScope, ShadowClean, IRBElse, ShadowBase);
3227     } else {
3228       copyToShadow(ShadowAfterScope, ShadowClean, IRBRet, ShadowBase);
3229     }
3230   }
3231 
3232   // We are done. Remove the old unused alloca instructions.
3233   for (auto AI : AllocaVec) AI->eraseFromParent();
3234 }
3235 
3236 void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size,
3237                                          IRBuilder<> &IRB, bool DoPoison) {
3238   // For now just insert the call to ASan runtime.
3239   Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy);
3240   Value *SizeArg = ConstantInt::get(IntptrTy, Size);
3241   IRB.CreateCall(
3242       DoPoison ? AsanPoisonStackMemoryFunc : AsanUnpoisonStackMemoryFunc,
3243       {AddrArg, SizeArg});
3244 }
3245 
3246 // Handling llvm.lifetime intrinsics for a given %alloca:
3247 // (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca.
3248 // (2) if %size is constant, poison memory for llvm.lifetime.end (to detect
3249 //     invalid accesses) and unpoison it for llvm.lifetime.start (the memory
3250 //     could be poisoned by previous llvm.lifetime.end instruction, as the
3251 //     variable may go in and out of scope several times, e.g. in loops).
3252 // (3) if we poisoned at least one %alloca in a function,
3253 //     unpoison the whole stack frame at function exit.
3254 void FunctionStackPoisoner::handleDynamicAllocaCall(AllocaInst *AI) {
3255   IRBuilder<> IRB(AI);
3256 
3257   const unsigned Align = std::max(kAllocaRzSize, AI->getAlignment());
3258   const uint64_t AllocaRedzoneMask = kAllocaRzSize - 1;
3259 
3260   Value *Zero = Constant::getNullValue(IntptrTy);
3261   Value *AllocaRzSize = ConstantInt::get(IntptrTy, kAllocaRzSize);
3262   Value *AllocaRzMask = ConstantInt::get(IntptrTy, AllocaRedzoneMask);
3263 
3264   // Since we need to extend alloca with additional memory to locate
3265   // redzones, and OldSize is number of allocated blocks with
3266   // ElementSize size, get allocated memory size in bytes by
3267   // OldSize * ElementSize.
3268   const unsigned ElementSize =
3269       F.getParent()->getDataLayout().getTypeAllocSize(AI->getAllocatedType());
3270   Value *OldSize =
3271       IRB.CreateMul(IRB.CreateIntCast(AI->getArraySize(), IntptrTy, false),
3272                     ConstantInt::get(IntptrTy, ElementSize));
3273 
3274   // PartialSize = OldSize % 32
3275   Value *PartialSize = IRB.CreateAnd(OldSize, AllocaRzMask);
3276 
3277   // Misalign = kAllocaRzSize - PartialSize;
3278   Value *Misalign = IRB.CreateSub(AllocaRzSize, PartialSize);
3279 
3280   // PartialPadding = Misalign != kAllocaRzSize ? Misalign : 0;
3281   Value *Cond = IRB.CreateICmpNE(Misalign, AllocaRzSize);
3282   Value *PartialPadding = IRB.CreateSelect(Cond, Misalign, Zero);
3283 
3284   // AdditionalChunkSize = Align + PartialPadding + kAllocaRzSize
3285   // Align is added to locate left redzone, PartialPadding for possible
3286   // partial redzone and kAllocaRzSize for right redzone respectively.
3287   Value *AdditionalChunkSize = IRB.CreateAdd(
3288       ConstantInt::get(IntptrTy, Align + kAllocaRzSize), PartialPadding);
3289 
3290   Value *NewSize = IRB.CreateAdd(OldSize, AdditionalChunkSize);
3291 
3292   // Insert new alloca with new NewSize and Align params.
3293   AllocaInst *NewAlloca = IRB.CreateAlloca(IRB.getInt8Ty(), NewSize);
3294   NewAlloca->setAlignment(Align);
3295 
3296   // NewAddress = Address + Align
3297   Value *NewAddress = IRB.CreateAdd(IRB.CreatePtrToInt(NewAlloca, IntptrTy),
3298                                     ConstantInt::get(IntptrTy, Align));
3299 
3300   // Insert __asan_alloca_poison call for new created alloca.
3301   IRB.CreateCall(AsanAllocaPoisonFunc, {NewAddress, OldSize});
3302 
3303   // Store the last alloca's address to DynamicAllocaLayout. We'll need this
3304   // for unpoisoning stuff.
3305   IRB.CreateStore(IRB.CreatePtrToInt(NewAlloca, IntptrTy), DynamicAllocaLayout);
3306 
3307   Value *NewAddressPtr = IRB.CreateIntToPtr(NewAddress, AI->getType());
3308 
3309   // Replace all uses of AddessReturnedByAlloca with NewAddressPtr.
3310   AI->replaceAllUsesWith(NewAddressPtr);
3311 
3312   // We are done. Erase old alloca from parent.
3313   AI->eraseFromParent();
3314 }
3315 
3316 // isSafeAccess returns true if Addr is always inbounds with respect to its
3317 // base object. For example, it is a field access or an array access with
3318 // constant inbounds index.
3319 bool AddressSanitizer::isSafeAccess(ObjectSizeOffsetVisitor &ObjSizeVis,
3320                                     Value *Addr, uint64_t TypeSize) const {
3321   SizeOffsetType SizeOffset = ObjSizeVis.compute(Addr);
3322   if (!ObjSizeVis.bothKnown(SizeOffset)) return false;
3323   uint64_t Size = SizeOffset.first.getZExtValue();
3324   int64_t Offset = SizeOffset.second.getSExtValue();
3325   // Three checks are required to ensure safety:
3326   // . Offset >= 0  (since the offset is given from the base ptr)
3327   // . Size >= Offset  (unsigned)
3328   // . Size - Offset >= NeededSize  (unsigned)
3329   return Offset >= 0 && Size >= uint64_t(Offset) &&
3330          Size - uint64_t(Offset) >= TypeSize / 8;
3331 }
3332