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