LLVM 22.0.0git
AddressSanitizer.cpp
Go to the documentation of this file.
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 basic correctness
10// checker.
11// Details of the algorithm:
12// https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
13//
14// FIXME: This sanitizer does not yet handle scalable vectors
15//
16//===----------------------------------------------------------------------===//
17
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/Statistic.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/ADT/Twine.h"
36#include "llvm/IR/Argument.h"
37#include "llvm/IR/Attributes.h"
38#include "llvm/IR/BasicBlock.h"
39#include "llvm/IR/Comdat.h"
40#include "llvm/IR/Constant.h"
41#include "llvm/IR/Constants.h"
42#include "llvm/IR/DIBuilder.h"
43#include "llvm/IR/DataLayout.h"
45#include "llvm/IR/DebugLoc.h"
48#include "llvm/IR/Function.h"
49#include "llvm/IR/GlobalAlias.h"
50#include "llvm/IR/GlobalValue.h"
52#include "llvm/IR/IRBuilder.h"
53#include "llvm/IR/InlineAsm.h"
54#include "llvm/IR/InstVisitor.h"
55#include "llvm/IR/InstrTypes.h"
56#include "llvm/IR/Instruction.h"
59#include "llvm/IR/Intrinsics.h"
60#include "llvm/IR/LLVMContext.h"
61#include "llvm/IR/MDBuilder.h"
62#include "llvm/IR/Metadata.h"
63#include "llvm/IR/Module.h"
64#include "llvm/IR/Type.h"
65#include "llvm/IR/Use.h"
66#include "llvm/IR/Value.h"
70#include "llvm/Support/Debug.h"
83#include <algorithm>
84#include <cassert>
85#include <cstddef>
86#include <cstdint>
87#include <iomanip>
88#include <limits>
89#include <sstream>
90#include <string>
91#include <tuple>
92
93using namespace llvm;
94
95#define DEBUG_TYPE "asan"
96
98static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
99static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
101 std::numeric_limits<uint64_t>::max();
102static const uint64_t kSmallX86_64ShadowOffsetBase = 0x7FFFFFFF; // < 2G.
104static const uint64_t kLinuxKasan_ShadowOffset64 = 0xdffffc0000000000;
105static const uint64_t kPPC64_ShadowOffset64 = 1ULL << 44;
106static const uint64_t kSystemZ_ShadowOffset64 = 1ULL << 52;
107static const uint64_t kMIPS_ShadowOffsetN32 = 1ULL << 29;
108static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa0000;
109static const uint64_t kMIPS64_ShadowOffset64 = 1ULL << 37;
110static const uint64_t kAArch64_ShadowOffset64 = 1ULL << 36;
111static const uint64_t kLoongArch64_ShadowOffset64 = 1ULL << 46;
113static const uint64_t kFreeBSD_ShadowOffset32 = 1ULL << 30;
114static const uint64_t kFreeBSD_ShadowOffset64 = 1ULL << 46;
115static const uint64_t kFreeBSDAArch64_ShadowOffset64 = 1ULL << 47;
116static const uint64_t kFreeBSDKasan_ShadowOffset64 = 0xdffff7c000000000;
117static const uint64_t kNetBSD_ShadowOffset32 = 1ULL << 30;
118static const uint64_t kNetBSD_ShadowOffset64 = 1ULL << 46;
119static const uint64_t kNetBSDKasan_ShadowOffset64 = 0xdfff900000000000;
120static const uint64_t kPS_ShadowOffset64 = 1ULL << 40;
121static const uint64_t kWindowsShadowOffset32 = 3ULL << 28;
123
124// The shadow memory space is dynamically allocated.
126
127static const size_t kMinStackMallocSize = 1 << 6; // 64B
128static const size_t kMaxStackMallocSize = 1 << 16; // 64K
129static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
130static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
131
132const char kAsanModuleCtorName[] = "asan.module_ctor";
133const char kAsanModuleDtorName[] = "asan.module_dtor";
135// On Emscripten, the system needs more than one priorities for constructors.
137const char kAsanReportErrorTemplate[] = "__asan_report_";
138const char kAsanRegisterGlobalsName[] = "__asan_register_globals";
139const char kAsanUnregisterGlobalsName[] = "__asan_unregister_globals";
140const char kAsanRegisterImageGlobalsName[] = "__asan_register_image_globals";
142 "__asan_unregister_image_globals";
143const char kAsanRegisterElfGlobalsName[] = "__asan_register_elf_globals";
144const char kAsanUnregisterElfGlobalsName[] = "__asan_unregister_elf_globals";
145const char kAsanPoisonGlobalsName[] = "__asan_before_dynamic_init";
146const char kAsanUnpoisonGlobalsName[] = "__asan_after_dynamic_init";
147const char kAsanInitName[] = "__asan_init";
148const char kAsanVersionCheckNamePrefix[] = "__asan_version_mismatch_check_v";
149const char kAsanPtrCmp[] = "__sanitizer_ptr_cmp";
150const char kAsanPtrSub[] = "__sanitizer_ptr_sub";
151const char kAsanHandleNoReturnName[] = "__asan_handle_no_return";
152static const int kMaxAsanStackMallocSizeClass = 10;
153const char kAsanStackMallocNameTemplate[] = "__asan_stack_malloc_";
155 "__asan_stack_malloc_always_";
156const char kAsanStackFreeNameTemplate[] = "__asan_stack_free_";
157const char kAsanGenPrefix[] = "___asan_gen_";
158const char kODRGenPrefix[] = "__odr_asan_gen_";
159const char kSanCovGenPrefix[] = "__sancov_gen_";
160const char kAsanSetShadowPrefix[] = "__asan_set_shadow_";
161const char kAsanPoisonStackMemoryName[] = "__asan_poison_stack_memory";
162const 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.
166const char kAsanGlobalsRegisteredFlagName[] = "___asan_globals_registered";
167
169 "__asan_option_detect_stack_use_after_return";
170
172 "__asan_shadow_memory_dynamic_address";
173
174const char kAsanAllocaPoison[] = "__asan_alloca_poison";
175const char kAsanAllocasUnpoison[] = "__asan_allocas_unpoison";
176
177const char kAMDGPUAddressSharedName[] = "llvm.amdgcn.is.shared";
178const char kAMDGPUAddressPrivateName[] = "llvm.amdgcn.is.private";
179const char kAMDGPUBallotName[] = "llvm.amdgcn.ballot.i64";
180const char kAMDGPUUnreachableName[] = "llvm.amdgcn.unreachable";
181
182// Accesses sizes are powers of two: 1, 2, 4, 8, 16.
183static const size_t kNumberOfAccessSizes = 5;
184
185static const uint64_t kAllocaRzSize = 32;
186
187// ASanAccessInfo implementation constants.
188constexpr size_t kCompileKernelShift = 0;
189constexpr size_t kCompileKernelMask = 0x1;
190constexpr size_t kAccessSizeIndexShift = 1;
191constexpr size_t kAccessSizeIndexMask = 0xf;
192constexpr size_t kIsWriteShift = 5;
193constexpr size_t kIsWriteMask = 0x1;
194
195// Command-line flags.
196
198 "asan-kernel", cl::desc("Enable KernelAddressSanitizer instrumentation"),
199 cl::Hidden, cl::init(false));
200
202 "asan-recover",
203 cl::desc("Enable recovery mode (continue-after-error)."),
204 cl::Hidden, cl::init(false));
205
207 "asan-guard-against-version-mismatch",
208 cl::desc("Guard against compiler/runtime version mismatch."), cl::Hidden,
209 cl::init(true));
210
211// This flag may need to be replaced with -f[no-]asan-reads.
212static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
213 cl::desc("instrument read instructions"),
214 cl::Hidden, cl::init(true));
215
217 "asan-instrument-writes", cl::desc("instrument write instructions"),
218 cl::Hidden, cl::init(true));
219
220static cl::opt<bool>
221 ClUseStackSafety("asan-use-stack-safety", cl::Hidden, cl::init(true),
222 cl::Hidden, cl::desc("Use Stack Safety analysis results"),
224
226 "asan-instrument-atomics",
227 cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden,
228 cl::init(true));
229
230static cl::opt<bool>
231 ClInstrumentByval("asan-instrument-byval",
232 cl::desc("instrument byval call arguments"), cl::Hidden,
233 cl::init(true));
234
236 "asan-always-slow-path",
237 cl::desc("use instrumentation with slow path for all accesses"), cl::Hidden,
238 cl::init(false));
239
241 "asan-force-dynamic-shadow",
242 cl::desc("Load shadow address into a local variable for each function"),
243 cl::Hidden, cl::init(false));
244
245static cl::opt<bool>
246 ClWithIfunc("asan-with-ifunc",
247 cl::desc("Access dynamic shadow through an ifunc global on "
248 "platforms that support this"),
249 cl::Hidden, cl::init(true));
250
252 "asan-with-ifunc-suppress-remat",
253 cl::desc("Suppress rematerialization of dynamic shadow address by passing "
254 "it through inline asm in prologue."),
255 cl::Hidden, cl::init(true));
256
257// This flag limits the number of instructions to be instrumented
258// in any given BB. Normally, this should be set to unlimited (INT_MAX),
259// but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
260// set it to 10000.
262 "asan-max-ins-per-bb", cl::init(10000),
263 cl::desc("maximal number of instructions to instrument in any given BB"),
264 cl::Hidden);
265
266// This flag may need to be replaced with -f[no]asan-stack.
267static cl::opt<bool> ClStack("asan-stack", cl::desc("Handle stack memory"),
268 cl::Hidden, cl::init(true));
270 "asan-max-inline-poisoning-size",
271 cl::desc(
272 "Inline shadow poisoning for blocks up to the given size in bytes."),
273 cl::Hidden, cl::init(64));
274
276 "asan-use-after-return",
277 cl::desc("Sets the mode of detection for stack-use-after-return."),
280 "Never detect stack use after return."),
283 "Detect stack use after return if "
284 "binary flag 'ASAN_OPTIONS=detect_stack_use_after_return' is set."),
286 "Always detect stack use after return.")),
288
289static cl::opt<bool> ClRedzoneByvalArgs("asan-redzone-byval-args",
290 cl::desc("Create redzones for byval "
291 "arguments (extra copy "
292 "required)"), cl::Hidden,
293 cl::init(true));
294
295static cl::opt<bool> ClUseAfterScope("asan-use-after-scope",
296 cl::desc("Check stack-use-after-scope"),
297 cl::Hidden, cl::init(false));
298
299// This flag may need to be replaced with -f[no]asan-globals.
300static cl::opt<bool> ClGlobals("asan-globals",
301 cl::desc("Handle global objects"), cl::Hidden,
302 cl::init(true));
303
304static cl::opt<bool> ClInitializers("asan-initialization-order",
305 cl::desc("Handle C++ initializer order"),
306 cl::Hidden, cl::init(true));
307
309 "asan-detect-invalid-pointer-pair",
310 cl::desc("Instrument <, <=, >, >=, - with pointer operands"), cl::Hidden,
311 cl::init(false));
312
314 "asan-detect-invalid-pointer-cmp",
315 cl::desc("Instrument <, <=, >, >= with pointer operands"), cl::Hidden,
316 cl::init(false));
317
319 "asan-detect-invalid-pointer-sub",
320 cl::desc("Instrument - operations with pointer operands"), cl::Hidden,
321 cl::init(false));
322
324 "asan-realign-stack",
325 cl::desc("Realign stack to the value of this flag (power of two)"),
326 cl::Hidden, cl::init(32));
327
329 "asan-instrumentation-with-call-threshold",
330 cl::desc("If the function being instrumented contains more than "
331 "this number of memory accesses, use callbacks instead of "
332 "inline checks (-1 means never use callbacks)."),
333 cl::Hidden, cl::init(7000));
334
336 "asan-memory-access-callback-prefix",
337 cl::desc("Prefix for memory access callbacks"), cl::Hidden,
338 cl::init("__asan_"));
339
341 "asan-kernel-mem-intrinsic-prefix",
342 cl::desc("Use prefix for memory intrinsics in KASAN mode"), cl::Hidden,
343 cl::init(false));
344
345static cl::opt<bool>
346 ClInstrumentDynamicAllocas("asan-instrument-dynamic-allocas",
347 cl::desc("instrument dynamic allocas"),
348 cl::Hidden, cl::init(true));
349
351 "asan-skip-promotable-allocas",
352 cl::desc("Do not instrument promotable allocas"), cl::Hidden,
353 cl::init(true));
354
356 "asan-constructor-kind",
357 cl::desc("Sets the ASan constructor kind"),
358 cl::values(clEnumValN(AsanCtorKind::None, "none", "No constructors"),
360 "Use global constructors")),
362// These flags allow to change the shadow mapping.
363// The shadow mapping looks like
364// Shadow = (Mem >> scale) + offset
365
366static cl::opt<int> ClMappingScale("asan-mapping-scale",
367 cl::desc("scale of asan shadow mapping"),
368 cl::Hidden, cl::init(0));
369
371 ClMappingOffset("asan-mapping-offset",
372 cl::desc("offset of asan shadow mapping [EXPERIMENTAL]"),
373 cl::Hidden, cl::init(0));
374
375// Optimization flags. Not user visible, used mostly for testing
376// and benchmarking the tool.
377
378static cl::opt<bool> ClOpt("asan-opt", cl::desc("Optimize instrumentation"),
379 cl::Hidden, cl::init(true));
380
381static cl::opt<bool> ClOptimizeCallbacks("asan-optimize-callbacks",
382 cl::desc("Optimize callbacks"),
383 cl::Hidden, cl::init(false));
384
386 "asan-opt-same-temp", cl::desc("Instrument the same temp just once"),
387 cl::Hidden, cl::init(true));
388
389static cl::opt<bool> ClOptGlobals("asan-opt-globals",
390 cl::desc("Don't instrument scalar globals"),
391 cl::Hidden, cl::init(true));
392
394 "asan-opt-stack", cl::desc("Don't instrument scalar stack variables"),
395 cl::Hidden, cl::init(false));
396
398 "asan-stack-dynamic-alloca",
399 cl::desc("Use dynamic alloca to represent stack variables"), cl::Hidden,
400 cl::init(true));
401
403 "asan-force-experiment",
404 cl::desc("Force optimization experiment (for testing)"), cl::Hidden,
405 cl::init(0));
406
407static cl::opt<bool>
408 ClUsePrivateAlias("asan-use-private-alias",
409 cl::desc("Use private aliases for global variables"),
410 cl::Hidden, cl::init(true));
411
412static cl::opt<bool>
413 ClUseOdrIndicator("asan-use-odr-indicator",
414 cl::desc("Use odr indicators to improve ODR reporting"),
415 cl::Hidden, cl::init(true));
416
417static cl::opt<bool>
418 ClUseGlobalsGC("asan-globals-live-support",
419 cl::desc("Use linker features to support dead "
420 "code stripping of globals"),
421 cl::Hidden, cl::init(true));
422
423// This is on by default even though there is a bug in gold:
424// https://sourceware.org/bugzilla/show_bug.cgi?id=19002
425static cl::opt<bool>
426 ClWithComdat("asan-with-comdat",
427 cl::desc("Place ASan constructors in comdat sections"),
428 cl::Hidden, cl::init(true));
429
431 "asan-destructor-kind",
432 cl::desc("Sets the ASan destructor kind. The default is to use the value "
433 "provided to the pass constructor"),
434 cl::values(clEnumValN(AsanDtorKind::None, "none", "No destructors"),
436 "Use global destructors")),
438
439// Debug flags.
440
441static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
442 cl::init(0));
443
444static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
445 cl::Hidden, cl::init(0));
446
448 cl::desc("Debug func"));
449
450static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
451 cl::Hidden, cl::init(-1));
452
453static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug max inst"),
454 cl::Hidden, cl::init(-1));
455
456STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
457STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
458STATISTIC(NumOptimizedAccessesToGlobalVar,
459 "Number of optimized accesses to global vars");
460STATISTIC(NumOptimizedAccessesToStackVar,
461 "Number of optimized accesses to stack vars");
462
463namespace {
464
465/// This struct defines the shadow mapping using the rule:
466/// shadow = (mem >> Scale) ADD-or-OR Offset.
467/// If InGlobal is true, then
468/// extern char __asan_shadow[];
469/// shadow = (mem >> Scale) + &__asan_shadow
470struct ShadowMapping {
471 int Scale;
473 bool OrShadowOffset;
474 bool InGlobal;
475};
476
477} // end anonymous namespace
478
479static ShadowMapping getShadowMapping(const Triple &TargetTriple, int LongSize,
480 bool IsKasan) {
481 bool IsAndroid = TargetTriple.isAndroid();
482 bool IsIOS = TargetTriple.isiOS() || TargetTriple.isWatchOS() ||
483 TargetTriple.isDriverKit();
484 bool IsMacOS = TargetTriple.isMacOSX();
485 bool IsFreeBSD = TargetTriple.isOSFreeBSD();
486 bool IsNetBSD = TargetTriple.isOSNetBSD();
487 bool IsPS = TargetTriple.isPS();
488 bool IsLinux = TargetTriple.isOSLinux();
489 bool IsPPC64 = TargetTriple.getArch() == Triple::ppc64 ||
490 TargetTriple.getArch() == Triple::ppc64le;
491 bool IsSystemZ = TargetTriple.getArch() == Triple::systemz;
492 bool IsX86_64 = TargetTriple.getArch() == Triple::x86_64;
493 bool IsMIPSN32ABI = TargetTriple.isABIN32();
494 bool IsMIPS32 = TargetTriple.isMIPS32();
495 bool IsMIPS64 = TargetTriple.isMIPS64();
496 bool IsArmOrThumb = TargetTriple.isARM() || TargetTriple.isThumb();
497 bool IsAArch64 = TargetTriple.getArch() == Triple::aarch64 ||
498 TargetTriple.getArch() == Triple::aarch64_be;
499 bool IsLoongArch64 = TargetTriple.isLoongArch64();
500 bool IsRISCV64 = TargetTriple.getArch() == Triple::riscv64;
501 bool IsWindows = TargetTriple.isOSWindows();
502 bool IsFuchsia = TargetTriple.isOSFuchsia();
503 bool IsAMDGPU = TargetTriple.isAMDGPU();
504 bool IsHaiku = TargetTriple.isOSHaiku();
505 bool IsWasm = TargetTriple.isWasm();
506
507 ShadowMapping Mapping;
508
509 Mapping.Scale = kDefaultShadowScale;
510 if (ClMappingScale.getNumOccurrences() > 0) {
511 Mapping.Scale = ClMappingScale;
512 }
513
514 if (LongSize == 32) {
515 if (IsAndroid)
516 Mapping.Offset = kDynamicShadowSentinel;
517 else if (IsMIPSN32ABI)
518 Mapping.Offset = kMIPS_ShadowOffsetN32;
519 else if (IsMIPS32)
520 Mapping.Offset = kMIPS32_ShadowOffset32;
521 else if (IsFreeBSD)
522 Mapping.Offset = kFreeBSD_ShadowOffset32;
523 else if (IsNetBSD)
524 Mapping.Offset = kNetBSD_ShadowOffset32;
525 else if (IsIOS)
526 Mapping.Offset = kDynamicShadowSentinel;
527 else if (IsWindows)
528 Mapping.Offset = kWindowsShadowOffset32;
529 else if (IsWasm)
530 Mapping.Offset = kWebAssemblyShadowOffset;
531 else
532 Mapping.Offset = kDefaultShadowOffset32;
533 } else { // LongSize == 64
534 // Fuchsia is always PIE, which means that the beginning of the address
535 // space is always available.
536 if (IsFuchsia)
537 Mapping.Offset = 0;
538 else if (IsPPC64)
539 Mapping.Offset = kPPC64_ShadowOffset64;
540 else if (IsSystemZ)
541 Mapping.Offset = kSystemZ_ShadowOffset64;
542 else if (IsFreeBSD && IsAArch64)
543 Mapping.Offset = kFreeBSDAArch64_ShadowOffset64;
544 else if (IsFreeBSD && !IsMIPS64) {
545 if (IsKasan)
546 Mapping.Offset = kFreeBSDKasan_ShadowOffset64;
547 else
548 Mapping.Offset = kFreeBSD_ShadowOffset64;
549 } else if (IsNetBSD) {
550 if (IsKasan)
551 Mapping.Offset = kNetBSDKasan_ShadowOffset64;
552 else
553 Mapping.Offset = kNetBSD_ShadowOffset64;
554 } else if (IsPS)
555 Mapping.Offset = kPS_ShadowOffset64;
556 else if (IsLinux && IsX86_64) {
557 if (IsKasan)
558 Mapping.Offset = kLinuxKasan_ShadowOffset64;
559 else
560 Mapping.Offset = (kSmallX86_64ShadowOffsetBase &
561 (kSmallX86_64ShadowOffsetAlignMask << Mapping.Scale));
562 } else if (IsWindows && IsX86_64) {
563 Mapping.Offset = kWindowsShadowOffset64;
564 } else if (IsMIPS64)
565 Mapping.Offset = kMIPS64_ShadowOffset64;
566 else if (IsIOS)
567 Mapping.Offset = kDynamicShadowSentinel;
568 else if (IsMacOS && IsAArch64)
569 Mapping.Offset = kDynamicShadowSentinel;
570 else if (IsAArch64)
571 Mapping.Offset = kAArch64_ShadowOffset64;
572 else if (IsLoongArch64)
573 Mapping.Offset = kLoongArch64_ShadowOffset64;
574 else if (IsRISCV64)
575 Mapping.Offset = kRISCV64_ShadowOffset64;
576 else if (IsAMDGPU)
577 Mapping.Offset = (kSmallX86_64ShadowOffsetBase &
578 (kSmallX86_64ShadowOffsetAlignMask << Mapping.Scale));
579 else if (IsHaiku && IsX86_64)
580 Mapping.Offset = (kSmallX86_64ShadowOffsetBase &
581 (kSmallX86_64ShadowOffsetAlignMask << Mapping.Scale));
582 else
583 Mapping.Offset = kDefaultShadowOffset64;
584 }
585
587 Mapping.Offset = kDynamicShadowSentinel;
588 }
589
590 if (ClMappingOffset.getNumOccurrences() > 0) {
591 Mapping.Offset = ClMappingOffset;
592 }
593
594 // OR-ing shadow offset if more efficient (at least on x86) if the offset
595 // is a power of two, but on ppc64 and loongarch64 we have to use add since
596 // the shadow offset is not necessarily 1/8-th of the address space. On
597 // SystemZ, we could OR the constant in a single instruction, but it's more
598 // efficient to load it once and use indexed addressing.
599 Mapping.OrShadowOffset = !IsAArch64 && !IsPPC64 && !IsSystemZ && !IsPS &&
600 !IsRISCV64 && !IsLoongArch64 &&
601 !(Mapping.Offset & (Mapping.Offset - 1)) &&
602 Mapping.Offset != kDynamicShadowSentinel;
603 bool IsAndroidWithIfuncSupport =
604 IsAndroid && !TargetTriple.isAndroidVersionLT(21);
605 Mapping.InGlobal = ClWithIfunc && IsAndroidWithIfuncSupport && IsArmOrThumb;
606
607 return Mapping;
608}
609
610namespace llvm {
611void getAddressSanitizerParams(const Triple &TargetTriple, int LongSize,
612 bool IsKasan, uint64_t *ShadowBase,
613 int *MappingScale, bool *OrShadowOffset) {
614 auto Mapping = getShadowMapping(TargetTriple, LongSize, IsKasan);
615 *ShadowBase = Mapping.Offset;
616 *MappingScale = Mapping.Scale;
617 *OrShadowOffset = Mapping.OrShadowOffset;
618}
619
621 // Sanitizer checks read from shadow, which invalidates memory(argmem: *).
622 //
623 // This is not only true for sanitized functions, because AttrInfer can
624 // infer those attributes on libc functions, which is not true if those
625 // are instrumented (Android) or intercepted.
626 //
627 // We might want to model ASan shadow memory more opaquely to get rid of
628 // this problem altogether, by hiding the shadow memory write in an
629 // intrinsic, essentially like in the AArch64StackTagging pass. But that's
630 // for another day.
631
632 // The API is weird. `onlyReadsMemory` actually means "does not write", and
633 // `onlyWritesMemory` actually means "does not read". So we reconstruct
634 // "accesses memory" && "does not read" <=> "writes".
635 bool Changed = false;
636 if (!F.doesNotAccessMemory()) {
637 bool WritesMemory = !F.onlyReadsMemory();
638 bool ReadsMemory = !F.onlyWritesMemory();
639 if ((WritesMemory && !ReadsMemory) || F.onlyAccessesArgMemory()) {
640 F.removeFnAttr(Attribute::Memory);
641 Changed = true;
642 }
643 }
644 if (ReadsArgMem) {
645 for (Argument &A : F.args()) {
646 if (A.hasAttribute(Attribute::WriteOnly)) {
647 A.removeAttr(Attribute::WriteOnly);
648 Changed = true;
649 }
650 }
651 }
652 if (Changed) {
653 // nobuiltin makes sure later passes don't restore assumptions about
654 // the function.
655 F.addFnAttr(Attribute::NoBuiltin);
656 }
657}
658
664
672
673} // namespace llvm
674
675static uint64_t getRedzoneSizeForScale(int MappingScale) {
676 // Redzone used for stack and globals is at least 32 bytes.
677 // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
678 return std::max(32U, 1U << MappingScale);
679}
680
682 if (TargetTriple.isOSEmscripten()) {
684 } else {
686 }
687}
688
689static Twine genName(StringRef suffix) {
690 return Twine(kAsanGenPrefix) + suffix;
691}
692
693namespace {
694/// Helper RAII class to post-process inserted asan runtime calls during a
695/// pass on a single Function. Upon end of scope, detects and applies the
696/// required funclet OpBundle.
697class RuntimeCallInserter {
698 Function *OwnerFn = nullptr;
699 bool TrackInsertedCalls = false;
700 SmallVector<CallInst *> InsertedCalls;
701
702public:
703 RuntimeCallInserter(Function &Fn) : OwnerFn(&Fn) {
704 if (Fn.hasPersonalityFn()) {
705 auto Personality = classifyEHPersonality(Fn.getPersonalityFn());
706 if (isScopedEHPersonality(Personality))
707 TrackInsertedCalls = true;
708 }
709 }
710
711 ~RuntimeCallInserter() {
712 if (InsertedCalls.empty())
713 return;
714 assert(TrackInsertedCalls && "Calls were wrongly tracked");
715
716 DenseMap<BasicBlock *, ColorVector> BlockColors = colorEHFunclets(*OwnerFn);
717 for (CallInst *CI : InsertedCalls) {
718 BasicBlock *BB = CI->getParent();
719 assert(BB && "Instruction doesn't belong to a BasicBlock");
720 assert(BB->getParent() == OwnerFn &&
721 "Instruction doesn't belong to the expected Function!");
722
723 ColorVector &Colors = BlockColors[BB];
724 // funclet opbundles are only valid in monochromatic BBs.
725 // Note that unreachable BBs are seen as colorless by colorEHFunclets()
726 // and will be DCE'ed later.
727 if (Colors.empty())
728 continue;
729 if (Colors.size() != 1) {
730 OwnerFn->getContext().emitError(
731 "Instruction's BasicBlock is not monochromatic");
732 continue;
733 }
734
735 BasicBlock *Color = Colors.front();
736 BasicBlock::iterator EHPadIt = Color->getFirstNonPHIIt();
737
738 if (EHPadIt != Color->end() && EHPadIt->isEHPad()) {
739 // Replace CI with a clone with an added funclet OperandBundle
740 OperandBundleDef OB("funclet", &*EHPadIt);
742 OB, CI->getIterator());
743 NewCall->copyMetadata(*CI);
744 CI->replaceAllUsesWith(NewCall);
745 CI->eraseFromParent();
746 }
747 }
748 }
749
750 CallInst *createRuntimeCall(IRBuilder<> &IRB, FunctionCallee Callee,
751 ArrayRef<Value *> Args = {},
752 const Twine &Name = "") {
753 assert(IRB.GetInsertBlock()->getParent() == OwnerFn);
754
755 CallInst *Inst = IRB.CreateCall(Callee, Args, Name, nullptr);
756 if (TrackInsertedCalls)
757 InsertedCalls.push_back(Inst);
758 return Inst;
759 }
760};
761
762/// AddressSanitizer: instrument the code in module to find memory bugs.
763struct AddressSanitizer {
764 AddressSanitizer(Module &M, const StackSafetyGlobalInfo *SSGI,
765 int InstrumentationWithCallsThreshold,
766 uint32_t MaxInlinePoisoningSize, bool CompileKernel = false,
767 bool Recover = false, bool UseAfterScope = false,
768 AsanDetectStackUseAfterReturnMode UseAfterReturn =
769 AsanDetectStackUseAfterReturnMode::Runtime)
770 : M(M),
771 CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
772 : CompileKernel),
773 Recover(ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover),
774 UseAfterScope(UseAfterScope || ClUseAfterScope),
775 UseAfterReturn(ClUseAfterReturn.getNumOccurrences() ? ClUseAfterReturn
776 : UseAfterReturn),
777 SSGI(SSGI),
778 InstrumentationWithCallsThreshold(
779 ClInstrumentationWithCallsThreshold.getNumOccurrences() > 0
781 : InstrumentationWithCallsThreshold),
782 MaxInlinePoisoningSize(ClMaxInlinePoisoningSize.getNumOccurrences() > 0
784 : MaxInlinePoisoningSize) {
785 C = &(M.getContext());
786 DL = &M.getDataLayout();
787 LongSize = M.getDataLayout().getPointerSizeInBits();
788 IntptrTy = Type::getIntNTy(*C, LongSize);
789 PtrTy = PointerType::getUnqual(*C);
790 Int32Ty = Type::getInt32Ty(*C);
791 TargetTriple = M.getTargetTriple();
792
793 Mapping = getShadowMapping(TargetTriple, LongSize, this->CompileKernel);
794
795 assert(this->UseAfterReturn != AsanDetectStackUseAfterReturnMode::Invalid);
796 }
797
798 TypeSize getAllocaSizeInBytes(const AllocaInst &AI) const {
799 return *AI.getAllocationSize(AI.getDataLayout());
800 }
801
802 /// Check if we want (and can) handle this alloca.
803 bool isInterestingAlloca(const AllocaInst &AI);
804
805 bool ignoreAccess(Instruction *Inst, Value *Ptr);
807 Instruction *I, SmallVectorImpl<InterestingMemoryOperand> &Interesting,
808 const TargetTransformInfo *TTI);
809
810 void instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis,
811 InterestingMemoryOperand &O, bool UseCalls,
812 const DataLayout &DL, RuntimeCallInserter &RTCI);
813 void instrumentPointerComparisonOrSubtraction(Instruction *I,
814 RuntimeCallInserter &RTCI);
815 void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore,
816 Value *Addr, MaybeAlign Alignment,
817 uint32_t TypeStoreSize, bool IsWrite,
818 Value *SizeArgument, bool UseCalls, uint32_t Exp,
819 RuntimeCallInserter &RTCI);
820 Instruction *instrumentAMDGPUAddress(Instruction *OrigIns,
821 Instruction *InsertBefore, Value *Addr,
822 uint32_t TypeStoreSize, bool IsWrite,
823 Value *SizeArgument);
824 Instruction *genAMDGPUReportBlock(IRBuilder<> &IRB, Value *Cond,
825 bool Recover);
826 void instrumentUnusualSizeOrAlignment(Instruction *I,
827 Instruction *InsertBefore, Value *Addr,
828 TypeSize TypeStoreSize, bool IsWrite,
829 Value *SizeArgument, bool UseCalls,
830 uint32_t Exp,
831 RuntimeCallInserter &RTCI);
832 void instrumentMaskedLoadOrStore(AddressSanitizer *Pass, const DataLayout &DL,
833 Type *IntptrTy, Value *Mask, Value *EVL,
834 Value *Stride, Instruction *I, Value *Addr,
835 MaybeAlign Alignment, unsigned Granularity,
836 Type *OpType, bool IsWrite,
837 Value *SizeArgument, bool UseCalls,
838 uint32_t Exp, RuntimeCallInserter &RTCI);
839 Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
840 Value *ShadowValue, uint32_t TypeStoreSize);
841 Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr,
842 bool IsWrite, size_t AccessSizeIndex,
843 Value *SizeArgument, uint32_t Exp,
844 RuntimeCallInserter &RTCI);
845 void instrumentMemIntrinsic(MemIntrinsic *MI, RuntimeCallInserter &RTCI);
846 Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
847 bool suppressInstrumentationSiteForDebug(int &Instrumented);
848 bool instrumentFunction(Function &F, const TargetLibraryInfo *TLI,
849 const TargetTransformInfo *TTI);
850 bool maybeInsertAsanInitAtFunctionEntry(Function &F);
851 bool maybeInsertDynamicShadowAtFunctionEntry(Function &F);
852 void markEscapedLocalAllocas(Function &F);
853
854private:
855 friend struct FunctionStackPoisoner;
856
857 void initializeCallbacks(const TargetLibraryInfo *TLI);
858
859 bool LooksLikeCodeInBug11395(Instruction *I);
860 bool GlobalIsLinkerInitialized(GlobalVariable *G);
861 bool isSafeAccess(ObjectSizeOffsetVisitor &ObjSizeVis, Value *Addr,
862 TypeSize TypeStoreSize) const;
863
864 /// Helper to cleanup per-function state.
865 struct FunctionStateRAII {
866 AddressSanitizer *Pass;
867
868 FunctionStateRAII(AddressSanitizer *Pass) : Pass(Pass) {
869 assert(Pass->ProcessedAllocas.empty() &&
870 "last pass forgot to clear cache");
871 assert(!Pass->LocalDynamicShadow);
872 }
873
874 ~FunctionStateRAII() {
875 Pass->LocalDynamicShadow = nullptr;
876 Pass->ProcessedAllocas.clear();
877 }
878 };
879
880 Module &M;
881 LLVMContext *C;
882 const DataLayout *DL;
883 Triple TargetTriple;
884 int LongSize;
885 bool CompileKernel;
886 bool Recover;
887 bool UseAfterScope;
889 Type *IntptrTy;
890 Type *Int32Ty;
891 PointerType *PtrTy;
892 ShadowMapping Mapping;
893 FunctionCallee AsanHandleNoReturnFunc;
894 FunctionCallee AsanPtrCmpFunction, AsanPtrSubFunction;
895 Constant *AsanShadowGlobal;
896
897 // These arrays is indexed by AccessIsWrite, Experiment and log2(AccessSize).
898 FunctionCallee AsanErrorCallback[2][2][kNumberOfAccessSizes];
899 FunctionCallee AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes];
900
901 // These arrays is indexed by AccessIsWrite and Experiment.
902 FunctionCallee AsanErrorCallbackSized[2][2];
903 FunctionCallee AsanMemoryAccessCallbackSized[2][2];
904
905 FunctionCallee AsanMemmove, AsanMemcpy, AsanMemset;
906 Value *LocalDynamicShadow = nullptr;
907 const StackSafetyGlobalInfo *SSGI;
908 DenseMap<const AllocaInst *, bool> ProcessedAllocas;
909
910 FunctionCallee AMDGPUAddressShared;
911 FunctionCallee AMDGPUAddressPrivate;
912 int InstrumentationWithCallsThreshold;
913 uint32_t MaxInlinePoisoningSize;
914};
915
916class ModuleAddressSanitizer {
917public:
918 ModuleAddressSanitizer(Module &M, bool InsertVersionCheck,
919 bool CompileKernel = false, bool Recover = false,
920 bool UseGlobalsGC = true, bool UseOdrIndicator = true,
921 AsanDtorKind DestructorKind = AsanDtorKind::Global,
922 AsanCtorKind ConstructorKind = AsanCtorKind::Global)
923 : M(M),
924 CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
925 : CompileKernel),
926 InsertVersionCheck(ClInsertVersionCheck.getNumOccurrences() > 0
928 : InsertVersionCheck),
929 Recover(ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover),
930 UseGlobalsGC(UseGlobalsGC && ClUseGlobalsGC && !this->CompileKernel),
931 // Enable aliases as they should have no downside with ODR indicators.
932 UsePrivateAlias(ClUsePrivateAlias.getNumOccurrences() > 0
934 : UseOdrIndicator),
935 UseOdrIndicator(ClUseOdrIndicator.getNumOccurrences() > 0
937 : UseOdrIndicator),
938 // Not a typo: ClWithComdat is almost completely pointless without
939 // ClUseGlobalsGC (because then it only works on modules without
940 // globals, which are rare); it is a prerequisite for ClUseGlobalsGC;
941 // and both suffer from gold PR19002 for which UseGlobalsGC constructor
942 // argument is designed as workaround. Therefore, disable both
943 // ClWithComdat and ClUseGlobalsGC unless the frontend says it's ok to
944 // do globals-gc.
945 UseCtorComdat(UseGlobalsGC && ClWithComdat && !this->CompileKernel),
946 DestructorKind(DestructorKind),
947 ConstructorKind(ClConstructorKind.getNumOccurrences() > 0
949 : ConstructorKind) {
950 C = &(M.getContext());
951 int LongSize = M.getDataLayout().getPointerSizeInBits();
952 IntptrTy = Type::getIntNTy(*C, LongSize);
953 PtrTy = PointerType::getUnqual(*C);
954 TargetTriple = M.getTargetTriple();
955 Mapping = getShadowMapping(TargetTriple, LongSize, this->CompileKernel);
956
957 if (ClOverrideDestructorKind != AsanDtorKind::Invalid)
958 this->DestructorKind = ClOverrideDestructorKind;
959 assert(this->DestructorKind != AsanDtorKind::Invalid);
960 }
961
962 bool instrumentModule();
963
964private:
965 void initializeCallbacks();
966
967 void instrumentGlobals(IRBuilder<> &IRB, bool *CtorComdat);
968 void InstrumentGlobalsCOFF(IRBuilder<> &IRB,
969 ArrayRef<GlobalVariable *> ExtendedGlobals,
970 ArrayRef<Constant *> MetadataInitializers);
971 void instrumentGlobalsELF(IRBuilder<> &IRB,
972 ArrayRef<GlobalVariable *> ExtendedGlobals,
973 ArrayRef<Constant *> MetadataInitializers,
974 const std::string &UniqueModuleId);
975 void InstrumentGlobalsMachO(IRBuilder<> &IRB,
976 ArrayRef<GlobalVariable *> ExtendedGlobals,
977 ArrayRef<Constant *> MetadataInitializers);
978 void
979 InstrumentGlobalsWithMetadataArray(IRBuilder<> &IRB,
980 ArrayRef<GlobalVariable *> ExtendedGlobals,
981 ArrayRef<Constant *> MetadataInitializers);
982
983 GlobalVariable *CreateMetadataGlobal(Constant *Initializer,
984 StringRef OriginalName);
985 void SetComdatForGlobalMetadata(GlobalVariable *G, GlobalVariable *Metadata,
986 StringRef InternalSuffix);
987 Instruction *CreateAsanModuleDtor();
988
989 const GlobalVariable *getExcludedAliasedGlobal(const GlobalAlias &GA) const;
990 bool shouldInstrumentGlobal(GlobalVariable *G) const;
991 bool ShouldUseMachOGlobalsSection() const;
992 StringRef getGlobalMetadataSection() const;
993 void poisonOneInitializer(Function &GlobalInit);
994 void createInitializerPoisonCalls();
995 uint64_t getMinRedzoneSizeForGlobal() const {
996 return getRedzoneSizeForScale(Mapping.Scale);
997 }
998 uint64_t getRedzoneSizeForGlobal(uint64_t SizeInBytes) const;
999 int GetAsanVersion() const;
1000 GlobalVariable *getOrCreateModuleName();
1001
1002 Module &M;
1003 bool CompileKernel;
1004 bool InsertVersionCheck;
1005 bool Recover;
1006 bool UseGlobalsGC;
1007 bool UsePrivateAlias;
1008 bool UseOdrIndicator;
1009 bool UseCtorComdat;
1010 AsanDtorKind DestructorKind;
1011 AsanCtorKind ConstructorKind;
1012 Type *IntptrTy;
1013 PointerType *PtrTy;
1014 LLVMContext *C;
1015 Triple TargetTriple;
1016 ShadowMapping Mapping;
1017 FunctionCallee AsanPoisonGlobals;
1018 FunctionCallee AsanUnpoisonGlobals;
1019 FunctionCallee AsanRegisterGlobals;
1020 FunctionCallee AsanUnregisterGlobals;
1021 FunctionCallee AsanRegisterImageGlobals;
1022 FunctionCallee AsanUnregisterImageGlobals;
1023 FunctionCallee AsanRegisterElfGlobals;
1024 FunctionCallee AsanUnregisterElfGlobals;
1025
1026 Function *AsanCtorFunction = nullptr;
1027 Function *AsanDtorFunction = nullptr;
1028 GlobalVariable *ModuleName = nullptr;
1029};
1030
1031// Stack poisoning does not play well with exception handling.
1032// When an exception is thrown, we essentially bypass the code
1033// that unpoisones the stack. This is why the run-time library has
1034// to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
1035// stack in the interceptor. This however does not work inside the
1036// actual function which catches the exception. Most likely because the
1037// compiler hoists the load of the shadow value somewhere too high.
1038// This causes asan to report a non-existing bug on 453.povray.
1039// It sounds like an LLVM bug.
1040struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
1041 Function &F;
1042 AddressSanitizer &ASan;
1043 RuntimeCallInserter &RTCI;
1044 DIBuilder DIB;
1045 LLVMContext *C;
1046 Type *IntptrTy;
1047 Type *IntptrPtrTy;
1048 ShadowMapping Mapping;
1049
1051 SmallVector<AllocaInst *, 16> StaticAllocasToMoveUp;
1052 SmallVector<Instruction *, 8> RetVec;
1053
1054 FunctionCallee AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
1055 AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1];
1056 FunctionCallee AsanSetShadowFunc[0x100] = {};
1057 FunctionCallee AsanPoisonStackMemoryFunc, AsanUnpoisonStackMemoryFunc;
1058 FunctionCallee AsanAllocaPoisonFunc, AsanAllocasUnpoisonFunc;
1059
1060 // Stores a place and arguments of poisoning/unpoisoning call for alloca.
1061 struct AllocaPoisonCall {
1062 IntrinsicInst *InsBefore;
1063 AllocaInst *AI;
1064 uint64_t Size;
1065 bool DoPoison;
1066 };
1067 SmallVector<AllocaPoisonCall, 8> DynamicAllocaPoisonCallVec;
1068 SmallVector<AllocaPoisonCall, 8> StaticAllocaPoisonCallVec;
1069
1070 SmallVector<AllocaInst *, 1> DynamicAllocaVec;
1071 SmallVector<IntrinsicInst *, 1> StackRestoreVec;
1072 AllocaInst *DynamicAllocaLayout = nullptr;
1073 IntrinsicInst *LocalEscapeCall = nullptr;
1074
1075 bool HasInlineAsm = false;
1076 bool HasReturnsTwiceCall = false;
1077 bool PoisonStack;
1078
1079 FunctionStackPoisoner(Function &F, AddressSanitizer &ASan,
1080 RuntimeCallInserter &RTCI)
1081 : F(F), ASan(ASan), RTCI(RTCI),
1082 DIB(*F.getParent(), /*AllowUnresolved*/ false), C(ASan.C),
1083 IntptrTy(ASan.IntptrTy),
1084 IntptrPtrTy(PointerType::get(IntptrTy->getContext(), 0)),
1085 Mapping(ASan.Mapping),
1086 PoisonStack(ClStack && !F.getParent()->getTargetTriple().isAMDGPU()) {}
1087
1088 bool runOnFunction() {
1089 if (!PoisonStack)
1090 return false;
1091
1093 copyArgsPassedByValToAllocas();
1094
1095 // Collect alloca, ret, lifetime instructions etc.
1096 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) visit(*BB);
1097
1098 if (AllocaVec.empty() && DynamicAllocaVec.empty()) return false;
1099
1100 initializeCallbacks(*F.getParent());
1101
1102 processDynamicAllocas();
1103 processStaticAllocas();
1104
1105 if (ClDebugStack) {
1106 LLVM_DEBUG(dbgs() << F);
1107 }
1108 return true;
1109 }
1110
1111 // Arguments marked with the "byval" attribute are implicitly copied without
1112 // using an alloca instruction. To produce redzones for those arguments, we
1113 // copy them a second time into memory allocated with an alloca instruction.
1114 void copyArgsPassedByValToAllocas();
1115
1116 // Finds all Alloca instructions and puts
1117 // poisoned red zones around all of them.
1118 // Then unpoison everything back before the function returns.
1119 void processStaticAllocas();
1120 void processDynamicAllocas();
1121
1122 void createDynamicAllocasInitStorage();
1123
1124 // ----------------------- Visitors.
1125 /// Collect all Ret instructions, or the musttail call instruction if it
1126 /// precedes the return instruction.
1127 void visitReturnInst(ReturnInst &RI) {
1128 if (CallInst *CI = RI.getParent()->getTerminatingMustTailCall())
1129 RetVec.push_back(CI);
1130 else
1131 RetVec.push_back(&RI);
1132 }
1133
1134 /// Collect all Resume instructions.
1135 void visitResumeInst(ResumeInst &RI) { RetVec.push_back(&RI); }
1136
1137 /// Collect all CatchReturnInst instructions.
1138 void visitCleanupReturnInst(CleanupReturnInst &CRI) { RetVec.push_back(&CRI); }
1139
1140 void unpoisonDynamicAllocasBeforeInst(Instruction *InstBefore,
1141 Value *SavedStack) {
1142 IRBuilder<> IRB(InstBefore);
1143 Value *DynamicAreaPtr = IRB.CreatePtrToInt(SavedStack, IntptrTy);
1144 // When we insert _asan_allocas_unpoison before @llvm.stackrestore, we
1145 // need to adjust extracted SP to compute the address of the most recent
1146 // alloca. We have a special @llvm.get.dynamic.area.offset intrinsic for
1147 // this purpose.
1148 if (!isa<ReturnInst>(InstBefore)) {
1149 Value *DynamicAreaOffset = IRB.CreateIntrinsic(
1150 Intrinsic::get_dynamic_area_offset, {IntptrTy}, {});
1151
1152 DynamicAreaPtr = IRB.CreateAdd(IRB.CreatePtrToInt(SavedStack, IntptrTy),
1153 DynamicAreaOffset);
1154 }
1155
1156 RTCI.createRuntimeCall(
1157 IRB, AsanAllocasUnpoisonFunc,
1158 {IRB.CreateLoad(IntptrTy, DynamicAllocaLayout), DynamicAreaPtr});
1159 }
1160
1161 // Unpoison dynamic allocas redzones.
1162 void unpoisonDynamicAllocas() {
1163 for (Instruction *Ret : RetVec)
1164 unpoisonDynamicAllocasBeforeInst(Ret, DynamicAllocaLayout);
1165
1166 for (Instruction *StackRestoreInst : StackRestoreVec)
1167 unpoisonDynamicAllocasBeforeInst(StackRestoreInst,
1168 StackRestoreInst->getOperand(0));
1169 }
1170
1171 // Deploy and poison redzones around dynamic alloca call. To do this, we
1172 // should replace this call with another one with changed parameters and
1173 // replace all its uses with new address, so
1174 // addr = alloca type, old_size, align
1175 // is replaced by
1176 // new_size = (old_size + additional_size) * sizeof(type)
1177 // tmp = alloca i8, new_size, max(align, 32)
1178 // addr = tmp + 32 (first 32 bytes are for the left redzone).
1179 // Additional_size is added to make new memory allocation contain not only
1180 // requested memory, but also left, partial and right redzones.
1181 void handleDynamicAllocaCall(AllocaInst *AI);
1182
1183 /// Collect Alloca instructions we want (and can) handle.
1184 void visitAllocaInst(AllocaInst &AI) {
1185 // FIXME: Handle scalable vectors instead of ignoring them.
1186 const Type *AllocaType = AI.getAllocatedType();
1187 const auto *STy = dyn_cast<StructType>(AllocaType);
1188 if (!ASan.isInterestingAlloca(AI) || isa<ScalableVectorType>(AllocaType) ||
1189 (STy && STy->containsHomogeneousScalableVectorTypes())) {
1190 if (AI.isStaticAlloca()) {
1191 // Skip over allocas that are present *before* the first instrumented
1192 // alloca, we don't want to move those around.
1193 if (AllocaVec.empty())
1194 return;
1195
1196 StaticAllocasToMoveUp.push_back(&AI);
1197 }
1198 return;
1199 }
1200
1201 if (!AI.isStaticAlloca())
1202 DynamicAllocaVec.push_back(&AI);
1203 else
1204 AllocaVec.push_back(&AI);
1205 }
1206
1207 /// Collect lifetime intrinsic calls to check for use-after-scope
1208 /// errors.
1209 void visitIntrinsicInst(IntrinsicInst &II) {
1210 Intrinsic::ID ID = II.getIntrinsicID();
1211 if (ID == Intrinsic::stackrestore) StackRestoreVec.push_back(&II);
1212 if (ID == Intrinsic::localescape) LocalEscapeCall = &II;
1213 if (!ASan.UseAfterScope)
1214 return;
1215 if (!II.isLifetimeStartOrEnd())
1216 return;
1217 // Find alloca instruction that corresponds to llvm.lifetime argument.
1218 AllocaInst *AI = dyn_cast<AllocaInst>(II.getArgOperand(0));
1219 // We're interested only in allocas we can handle.
1220 if (!AI || !ASan.isInterestingAlloca(*AI))
1221 return;
1222
1223 std::optional<TypeSize> Size = AI->getAllocationSize(AI->getDataLayout());
1224 // Check that size is known and can be stored in IntptrTy.
1225 // TODO: Add support for scalable vectors if possible.
1226 if (!Size || Size->isScalable() ||
1228 return;
1229
1230 bool DoPoison = (ID == Intrinsic::lifetime_end);
1231 AllocaPoisonCall APC = {&II, AI, *Size, DoPoison};
1232 if (AI->isStaticAlloca())
1233 StaticAllocaPoisonCallVec.push_back(APC);
1235 DynamicAllocaPoisonCallVec.push_back(APC);
1236 }
1237
1238 void visitCallBase(CallBase &CB) {
1239 if (CallInst *CI = dyn_cast<CallInst>(&CB)) {
1240 HasInlineAsm |= CI->isInlineAsm() && &CB != ASan.LocalDynamicShadow;
1241 HasReturnsTwiceCall |= CI->canReturnTwice();
1242 }
1243 }
1244
1245 // ---------------------- Helpers.
1246 void initializeCallbacks(Module &M);
1247
1248 // Copies bytes from ShadowBytes into shadow memory for indexes where
1249 // ShadowMask is not zero. If ShadowMask[i] is zero, we assume that
1250 // ShadowBytes[i] is constantly zero and doesn't need to be overwritten.
1251 void copyToShadow(ArrayRef<uint8_t> ShadowMask, ArrayRef<uint8_t> ShadowBytes,
1252 IRBuilder<> &IRB, Value *ShadowBase);
1253 void copyToShadow(ArrayRef<uint8_t> ShadowMask, ArrayRef<uint8_t> ShadowBytes,
1254 size_t Begin, size_t End, IRBuilder<> &IRB,
1255 Value *ShadowBase);
1256 void copyToShadowInline(ArrayRef<uint8_t> ShadowMask,
1257 ArrayRef<uint8_t> ShadowBytes, size_t Begin,
1258 size_t End, IRBuilder<> &IRB, Value *ShadowBase);
1259
1260 void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> &IRB, bool DoPoison);
1261
1262 Value *createAllocaForLayout(IRBuilder<> &IRB, const ASanStackFrameLayout &L,
1263 bool Dynamic);
1264 PHINode *createPHI(IRBuilder<> &IRB, Value *Cond, Value *ValueIfTrue,
1265 Instruction *ThenTerm, Value *ValueIfFalse);
1266};
1267
1268} // end anonymous namespace
1269
1271 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
1273 OS, MapClassName2PassName);
1274 OS << '<';
1275 if (Options.CompileKernel)
1276 OS << "kernel;";
1277 if (Options.UseAfterScope)
1278 OS << "use-after-scope";
1279 OS << '>';
1280}
1281
1283 const AddressSanitizerOptions &Options, bool UseGlobalGC,
1284 bool UseOdrIndicator, AsanDtorKind DestructorKind,
1285 AsanCtorKind ConstructorKind)
1286 : Options(Options), UseGlobalGC(UseGlobalGC),
1287 UseOdrIndicator(UseOdrIndicator), DestructorKind(DestructorKind),
1288 ConstructorKind(ConstructorKind) {}
1289
1292 // Return early if nosanitize_address module flag is present for the module.
1293 // This implies that asan pass has already run before.
1294 if (checkIfAlreadyInstrumented(M, "nosanitize_address"))
1295 return PreservedAnalyses::all();
1296
1297 ModuleAddressSanitizer ModuleSanitizer(
1298 M, Options.InsertVersionCheck, Options.CompileKernel, Options.Recover,
1299 UseGlobalGC, UseOdrIndicator, DestructorKind, ConstructorKind);
1300 bool Modified = false;
1301 auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
1302 const StackSafetyGlobalInfo *const SSGI =
1303 ClUseStackSafety ? &MAM.getResult<StackSafetyGlobalAnalysis>(M) : nullptr;
1304 for (Function &F : M) {
1305 if (F.empty())
1306 continue;
1307 if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage)
1308 continue;
1309 if (!ClDebugFunc.empty() && ClDebugFunc == F.getName())
1310 continue;
1311 if (F.getName().starts_with("__asan_"))
1312 continue;
1313 if (F.isPresplitCoroutine())
1314 continue;
1315 AddressSanitizer FunctionSanitizer(
1316 M, SSGI, Options.InstrumentationWithCallsThreshold,
1317 Options.MaxInlinePoisoningSize, Options.CompileKernel, Options.Recover,
1318 Options.UseAfterScope, Options.UseAfterReturn);
1319 const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1320 const TargetTransformInfo &TTI = FAM.getResult<TargetIRAnalysis>(F);
1321 Modified |= FunctionSanitizer.instrumentFunction(F, &TLI, &TTI);
1322 }
1323 Modified |= ModuleSanitizer.instrumentModule();
1324 if (!Modified)
1325 return PreservedAnalyses::all();
1326
1328 // GlobalsAA is considered stateless and does not get invalidated unless
1329 // explicitly invalidated; PreservedAnalyses::none() is not enough. Sanitizers
1330 // make changes that require GlobalsAA to be invalidated.
1331 PA.abandon<GlobalsAA>();
1332 return PA;
1333}
1334
1336 size_t Res = llvm::countr_zero(TypeSize / 8);
1338 return Res;
1339}
1340
1341/// Check if \p G has been created by a trusted compiler pass.
1343 // Do not instrument @llvm.global_ctors, @llvm.used, etc.
1344 if (G->getName().starts_with("llvm.") ||
1345 // Do not instrument gcov counter arrays.
1346 G->getName().starts_with("__llvm_gcov_ctr") ||
1347 // Do not instrument rtti proxy symbols for function sanitizer.
1348 G->getName().starts_with("__llvm_rtti_proxy"))
1349 return true;
1350
1351 // Do not instrument asan globals.
1352 if (G->getName().starts_with(kAsanGenPrefix) ||
1353 G->getName().starts_with(kSanCovGenPrefix) ||
1354 G->getName().starts_with(kODRGenPrefix))
1355 return true;
1356
1357 return false;
1358}
1359
1361 Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType());
1362 unsigned int AddrSpace = PtrTy->getPointerAddressSpace();
1363 if (AddrSpace == 3 || AddrSpace == 5)
1364 return true;
1365 return false;
1366}
1367
1368Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
1369 // Shadow >> scale
1370 Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
1371 if (Mapping.Offset == 0) return Shadow;
1372 // (Shadow >> scale) | offset
1373 Value *ShadowBase;
1374 if (LocalDynamicShadow)
1375 ShadowBase = LocalDynamicShadow;
1376 else
1377 ShadowBase = ConstantInt::get(IntptrTy, Mapping.Offset);
1378 if (Mapping.OrShadowOffset)
1379 return IRB.CreateOr(Shadow, ShadowBase);
1380 else
1381 return IRB.CreateAdd(Shadow, ShadowBase);
1382}
1383
1384// Instrument memset/memmove/memcpy
1385void AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI,
1386 RuntimeCallInserter &RTCI) {
1388 if (isa<MemTransferInst>(MI)) {
1389 RTCI.createRuntimeCall(
1390 IRB, isa<MemMoveInst>(MI) ? AsanMemmove : AsanMemcpy,
1391 {IRB.CreateAddrSpaceCast(MI->getOperand(0), PtrTy),
1392 IRB.CreateAddrSpaceCast(MI->getOperand(1), PtrTy),
1393 IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
1394 } else if (isa<MemSetInst>(MI)) {
1395 RTCI.createRuntimeCall(
1396 IRB, AsanMemset,
1397 {IRB.CreateAddrSpaceCast(MI->getOperand(0), PtrTy),
1398 IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false),
1399 IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
1400 }
1401 MI->eraseFromParent();
1402}
1403
1404/// Check if we want (and can) handle this alloca.
1405bool AddressSanitizer::isInterestingAlloca(const AllocaInst &AI) {
1406 auto [It, Inserted] = ProcessedAllocas.try_emplace(&AI);
1407
1408 if (!Inserted)
1409 return It->getSecond();
1410
1411 bool IsInteresting =
1412 (AI.getAllocatedType()->isSized() &&
1413 // alloca() may be called with 0 size, ignore it.
1414 ((!AI.isStaticAlloca()) || !getAllocaSizeInBytes(AI).isZero()) &&
1415 // We are only interested in allocas not promotable to registers.
1416 // Promotable allocas are common under -O0.
1418 // inalloca allocas are not treated as static, and we don't want
1419 // dynamic alloca instrumentation for them as well.
1420 !AI.isUsedWithInAlloca() &&
1421 // swifterror allocas are register promoted by ISel
1422 !AI.isSwiftError() &&
1423 // safe allocas are not interesting
1424 !(SSGI && SSGI->isSafe(AI)));
1425
1426 It->second = IsInteresting;
1427 return IsInteresting;
1428}
1429
1430bool AddressSanitizer::ignoreAccess(Instruction *Inst, Value *Ptr) {
1431 // Instrument accesses from different address spaces only for AMDGPU.
1432 Type *PtrTy = cast<PointerType>(Ptr->getType()->getScalarType());
1433 if (PtrTy->getPointerAddressSpace() != 0 &&
1434 !(TargetTriple.isAMDGPU() && !isUnsupportedAMDGPUAddrspace(Ptr)))
1435 return true;
1436
1437 // Ignore swifterror addresses.
1438 // swifterror memory addresses are mem2reg promoted by instruction
1439 // selection. As such they cannot have regular uses like an instrumentation
1440 // function and it makes no sense to track them as memory.
1441 if (Ptr->isSwiftError())
1442 return true;
1443
1444 // Treat memory accesses to promotable allocas as non-interesting since they
1445 // will not cause memory violations. This greatly speeds up the instrumented
1446 // executable at -O0.
1447 if (auto AI = dyn_cast_or_null<AllocaInst>(Ptr))
1448 if (ClSkipPromotableAllocas && !isInterestingAlloca(*AI))
1449 return true;
1450
1451 if (SSGI != nullptr && SSGI->stackAccessIsSafe(*Inst) &&
1453 return true;
1454
1455 return false;
1456}
1457
1458void AddressSanitizer::getInterestingMemoryOperands(
1460 const TargetTransformInfo *TTI) {
1461 // Do not instrument the load fetching the dynamic shadow address.
1462 if (LocalDynamicShadow == I)
1463 return;
1464
1465 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1466 if (!ClInstrumentReads || ignoreAccess(I, LI->getPointerOperand()))
1467 return;
1468 Interesting.emplace_back(I, LI->getPointerOperandIndex(), false,
1469 LI->getType(), LI->getAlign());
1470 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1471 if (!ClInstrumentWrites || ignoreAccess(I, SI->getPointerOperand()))
1472 return;
1473 Interesting.emplace_back(I, SI->getPointerOperandIndex(), true,
1474 SI->getValueOperand()->getType(), SI->getAlign());
1475 } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
1476 if (!ClInstrumentAtomics || ignoreAccess(I, RMW->getPointerOperand()))
1477 return;
1478 Interesting.emplace_back(I, RMW->getPointerOperandIndex(), true,
1479 RMW->getValOperand()->getType(), std::nullopt);
1480 } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
1481 if (!ClInstrumentAtomics || ignoreAccess(I, XCHG->getPointerOperand()))
1482 return;
1483 Interesting.emplace_back(I, XCHG->getPointerOperandIndex(), true,
1484 XCHG->getCompareOperand()->getType(),
1485 std::nullopt);
1486 } else if (auto CI = dyn_cast<CallInst>(I)) {
1487 switch (CI->getIntrinsicID()) {
1488 case Intrinsic::masked_load:
1489 case Intrinsic::masked_store:
1490 case Intrinsic::masked_gather:
1491 case Intrinsic::masked_scatter: {
1492 bool IsWrite = CI->getType()->isVoidTy();
1493 // Masked store has an initial operand for the value.
1494 unsigned OpOffset = IsWrite ? 1 : 0;
1495 if (IsWrite ? !ClInstrumentWrites : !ClInstrumentReads)
1496 return;
1497
1498 auto BasePtr = CI->getOperand(OpOffset);
1499 if (ignoreAccess(I, BasePtr))
1500 return;
1501 Type *Ty = IsWrite ? CI->getArgOperand(0)->getType() : CI->getType();
1502 MaybeAlign Alignment = Align(1);
1503 // Otherwise no alignment guarantees. We probably got Undef.
1504 if (auto *Op = dyn_cast<ConstantInt>(CI->getOperand(1 + OpOffset)))
1505 Alignment = Op->getMaybeAlignValue();
1506 Value *Mask = CI->getOperand(2 + OpOffset);
1507 Interesting.emplace_back(I, OpOffset, IsWrite, Ty, Alignment, Mask);
1508 break;
1509 }
1510 case Intrinsic::masked_expandload:
1511 case Intrinsic::masked_compressstore: {
1512 bool IsWrite = CI->getIntrinsicID() == Intrinsic::masked_compressstore;
1513 unsigned OpOffset = IsWrite ? 1 : 0;
1514 if (IsWrite ? !ClInstrumentWrites : !ClInstrumentReads)
1515 return;
1516 auto BasePtr = CI->getOperand(OpOffset);
1517 if (ignoreAccess(I, BasePtr))
1518 return;
1519 MaybeAlign Alignment = BasePtr->getPointerAlignment(*DL);
1520 Type *Ty = IsWrite ? CI->getArgOperand(0)->getType() : CI->getType();
1521
1522 IRBuilder IB(I);
1523 Value *Mask = CI->getOperand(1 + OpOffset);
1524 // Use the popcount of Mask as the effective vector length.
1525 Type *ExtTy = VectorType::get(IntptrTy, cast<VectorType>(Ty));
1526 Value *ExtMask = IB.CreateZExt(Mask, ExtTy);
1527 Value *EVL = IB.CreateAddReduce(ExtMask);
1528 Value *TrueMask = ConstantInt::get(Mask->getType(), 1);
1529 Interesting.emplace_back(I, OpOffset, IsWrite, Ty, Alignment, TrueMask,
1530 EVL);
1531 break;
1532 }
1533 case Intrinsic::vp_load:
1534 case Intrinsic::vp_store:
1535 case Intrinsic::experimental_vp_strided_load:
1536 case Intrinsic::experimental_vp_strided_store: {
1537 auto *VPI = cast<VPIntrinsic>(CI);
1538 unsigned IID = CI->getIntrinsicID();
1539 bool IsWrite = CI->getType()->isVoidTy();
1540 if (IsWrite ? !ClInstrumentWrites : !ClInstrumentReads)
1541 return;
1542 unsigned PtrOpNo = *VPI->getMemoryPointerParamPos(IID);
1543 Type *Ty = IsWrite ? CI->getArgOperand(0)->getType() : CI->getType();
1544 MaybeAlign Alignment = VPI->getOperand(PtrOpNo)->getPointerAlignment(*DL);
1545 Value *Stride = nullptr;
1546 if (IID == Intrinsic::experimental_vp_strided_store ||
1547 IID == Intrinsic::experimental_vp_strided_load) {
1548 Stride = VPI->getOperand(PtrOpNo + 1);
1549 // Use the pointer alignment as the element alignment if the stride is a
1550 // mutiple of the pointer alignment. Otherwise, the element alignment
1551 // should be Align(1).
1552 unsigned PointerAlign = Alignment.valueOrOne().value();
1553 if (!isa<ConstantInt>(Stride) ||
1554 cast<ConstantInt>(Stride)->getZExtValue() % PointerAlign != 0)
1555 Alignment = Align(1);
1556 }
1557 Interesting.emplace_back(I, PtrOpNo, IsWrite, Ty, Alignment,
1558 VPI->getMaskParam(), VPI->getVectorLengthParam(),
1559 Stride);
1560 break;
1561 }
1562 case Intrinsic::vp_gather:
1563 case Intrinsic::vp_scatter: {
1564 auto *VPI = cast<VPIntrinsic>(CI);
1565 unsigned IID = CI->getIntrinsicID();
1566 bool IsWrite = IID == Intrinsic::vp_scatter;
1567 if (IsWrite ? !ClInstrumentWrites : !ClInstrumentReads)
1568 return;
1569 unsigned PtrOpNo = *VPI->getMemoryPointerParamPos(IID);
1570 Type *Ty = IsWrite ? CI->getArgOperand(0)->getType() : CI->getType();
1571 MaybeAlign Alignment = VPI->getPointerAlignment();
1572 Interesting.emplace_back(I, PtrOpNo, IsWrite, Ty, Alignment,
1573 VPI->getMaskParam(),
1574 VPI->getVectorLengthParam());
1575 break;
1576 }
1577 default:
1578 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
1579 MemIntrinsicInfo IntrInfo;
1580 if (TTI->getTgtMemIntrinsic(II, IntrInfo))
1581 Interesting = IntrInfo.InterestingOperands;
1582 return;
1583 }
1584 for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ArgNo++) {
1585 if (!ClInstrumentByval || !CI->isByValArgument(ArgNo) ||
1586 ignoreAccess(I, CI->getArgOperand(ArgNo)))
1587 continue;
1588 Type *Ty = CI->getParamByValType(ArgNo);
1589 Interesting.emplace_back(I, ArgNo, false, Ty, Align(1));
1590 }
1591 }
1592 }
1593}
1594
1595static bool isPointerOperand(Value *V) {
1596 return V->getType()->isPointerTy() || isa<PtrToIntInst>(V);
1597}
1598
1599// This is a rough heuristic; it may cause both false positives and
1600// false negatives. The proper implementation requires cooperation with
1601// the frontend.
1603 if (ICmpInst *Cmp = dyn_cast<ICmpInst>(I)) {
1604 if (!Cmp->isRelational())
1605 return false;
1606 } else {
1607 return false;
1608 }
1609 return isPointerOperand(I->getOperand(0)) &&
1610 isPointerOperand(I->getOperand(1));
1611}
1612
1613// This is a rough heuristic; it may cause both false positives and
1614// false negatives. The proper implementation requires cooperation with
1615// the frontend.
1618 if (BO->getOpcode() != Instruction::Sub)
1619 return false;
1620 } else {
1621 return false;
1622 }
1623 return isPointerOperand(I->getOperand(0)) &&
1624 isPointerOperand(I->getOperand(1));
1625}
1626
1627bool AddressSanitizer::GlobalIsLinkerInitialized(GlobalVariable *G) {
1628 // If a global variable does not have dynamic initialization we don't
1629 // have to instrument it. However, if a global does not have initializer
1630 // at all, we assume it has dynamic initializer (in other TU).
1631 if (!G->hasInitializer())
1632 return false;
1633
1634 if (G->hasSanitizerMetadata() && G->getSanitizerMetadata().IsDynInit)
1635 return false;
1636
1637 return true;
1638}
1639
1640void AddressSanitizer::instrumentPointerComparisonOrSubtraction(
1641 Instruction *I, RuntimeCallInserter &RTCI) {
1642 IRBuilder<> IRB(I);
1643 FunctionCallee F = isa<ICmpInst>(I) ? AsanPtrCmpFunction : AsanPtrSubFunction;
1644 Value *Param[2] = {I->getOperand(0), I->getOperand(1)};
1645 for (Value *&i : Param) {
1646 if (i->getType()->isPointerTy())
1647 i = IRB.CreatePointerCast(i, IntptrTy);
1648 }
1649 RTCI.createRuntimeCall(IRB, F, Param);
1650}
1651
1652static void doInstrumentAddress(AddressSanitizer *Pass, Instruction *I,
1653 Instruction *InsertBefore, Value *Addr,
1654 MaybeAlign Alignment, unsigned Granularity,
1655 TypeSize TypeStoreSize, bool IsWrite,
1656 Value *SizeArgument, bool UseCalls,
1657 uint32_t Exp, RuntimeCallInserter &RTCI) {
1658 // Instrument a 1-, 2-, 4-, 8-, or 16- byte access with one check
1659 // if the data is properly aligned.
1660 if (!TypeStoreSize.isScalable()) {
1661 const auto FixedSize = TypeStoreSize.getFixedValue();
1662 switch (FixedSize) {
1663 case 8:
1664 case 16:
1665 case 32:
1666 case 64:
1667 case 128:
1668 if (!Alignment || *Alignment >= Granularity ||
1669 *Alignment >= FixedSize / 8)
1670 return Pass->instrumentAddress(I, InsertBefore, Addr, Alignment,
1671 FixedSize, IsWrite, nullptr, UseCalls,
1672 Exp, RTCI);
1673 }
1674 }
1675 Pass->instrumentUnusualSizeOrAlignment(I, InsertBefore, Addr, TypeStoreSize,
1676 IsWrite, nullptr, UseCalls, Exp, RTCI);
1677}
1678
1679void AddressSanitizer::instrumentMaskedLoadOrStore(
1680 AddressSanitizer *Pass, const DataLayout &DL, Type *IntptrTy, Value *Mask,
1681 Value *EVL, Value *Stride, Instruction *I, Value *Addr,
1682 MaybeAlign Alignment, unsigned Granularity, Type *OpType, bool IsWrite,
1683 Value *SizeArgument, bool UseCalls, uint32_t Exp,
1684 RuntimeCallInserter &RTCI) {
1685 auto *VTy = cast<VectorType>(OpType);
1686 TypeSize ElemTypeSize = DL.getTypeStoreSizeInBits(VTy->getScalarType());
1687 auto Zero = ConstantInt::get(IntptrTy, 0);
1688
1689 IRBuilder IB(I);
1690 Instruction *LoopInsertBefore = I;
1691 if (EVL) {
1692 // The end argument of SplitBlockAndInsertForLane is assumed bigger
1693 // than zero, so we should check whether EVL is zero here.
1694 Type *EVLType = EVL->getType();
1695 Value *IsEVLZero = IB.CreateICmpNE(EVL, ConstantInt::get(EVLType, 0));
1696 LoopInsertBefore = SplitBlockAndInsertIfThen(IsEVLZero, I, false);
1697 IB.SetInsertPoint(LoopInsertBefore);
1698 // Cast EVL to IntptrTy.
1699 EVL = IB.CreateZExtOrTrunc(EVL, IntptrTy);
1700 // To avoid undefined behavior for extracting with out of range index, use
1701 // the minimum of evl and element count as trip count.
1702 Value *EC = IB.CreateElementCount(IntptrTy, VTy->getElementCount());
1703 EVL = IB.CreateBinaryIntrinsic(Intrinsic::umin, EVL, EC);
1704 } else {
1705 EVL = IB.CreateElementCount(IntptrTy, VTy->getElementCount());
1706 }
1707
1708 // Cast Stride to IntptrTy.
1709 if (Stride)
1710 Stride = IB.CreateZExtOrTrunc(Stride, IntptrTy);
1711
1712 SplitBlockAndInsertForEachLane(EVL, LoopInsertBefore->getIterator(),
1713 [&](IRBuilderBase &IRB, Value *Index) {
1714 Value *MaskElem = IRB.CreateExtractElement(Mask, Index);
1715 if (auto *MaskElemC = dyn_cast<ConstantInt>(MaskElem)) {
1716 if (MaskElemC->isZero())
1717 // No check
1718 return;
1719 // Unconditional check
1720 } else {
1721 // Conditional check
1722 Instruction *ThenTerm = SplitBlockAndInsertIfThen(
1723 MaskElem, &*IRB.GetInsertPoint(), false);
1724 IRB.SetInsertPoint(ThenTerm);
1725 }
1726
1727 Value *InstrumentedAddress;
1728 if (isa<VectorType>(Addr->getType())) {
1729 assert(
1730 cast<VectorType>(Addr->getType())->getElementType()->isPointerTy() &&
1731 "Expected vector of pointer.");
1732 InstrumentedAddress = IRB.CreateExtractElement(Addr, Index);
1733 } else if (Stride) {
1734 Index = IRB.CreateMul(Index, Stride);
1735 InstrumentedAddress = IRB.CreatePtrAdd(Addr, Index);
1736 } else {
1737 InstrumentedAddress = IRB.CreateGEP(VTy, Addr, {Zero, Index});
1738 }
1739 doInstrumentAddress(Pass, I, &*IRB.GetInsertPoint(), InstrumentedAddress,
1740 Alignment, Granularity, ElemTypeSize, IsWrite,
1741 SizeArgument, UseCalls, Exp, RTCI);
1742 });
1743}
1744
1745void AddressSanitizer::instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis,
1746 InterestingMemoryOperand &O, bool UseCalls,
1747 const DataLayout &DL,
1748 RuntimeCallInserter &RTCI) {
1749 Value *Addr = O.getPtr();
1750
1751 // Optimization experiments.
1752 // The experiments can be used to evaluate potential optimizations that remove
1753 // instrumentation (assess false negatives). Instead of completely removing
1754 // some instrumentation, you set Exp to a non-zero value (mask of optimization
1755 // experiments that want to remove instrumentation of this instruction).
1756 // If Exp is non-zero, this pass will emit special calls into runtime
1757 // (e.g. __asan_report_exp_load1 instead of __asan_report_load1). These calls
1758 // make runtime terminate the program in a special way (with a different
1759 // exit status). Then you run the new compiler on a buggy corpus, collect
1760 // the special terminations (ideally, you don't see them at all -- no false
1761 // negatives) and make the decision on the optimization.
1762 uint32_t Exp = ClForceExperiment;
1763
1764 if (ClOpt && ClOptGlobals) {
1765 // If initialization order checking is disabled, a simple access to a
1766 // dynamically initialized global is always valid.
1768 if (G && (!ClInitializers || GlobalIsLinkerInitialized(G)) &&
1769 isSafeAccess(ObjSizeVis, Addr, O.TypeStoreSize)) {
1770 NumOptimizedAccessesToGlobalVar++;
1771 return;
1772 }
1773 }
1774
1775 if (ClOpt && ClOptStack) {
1776 // A direct inbounds access to a stack variable is always valid.
1778 isSafeAccess(ObjSizeVis, Addr, O.TypeStoreSize)) {
1779 NumOptimizedAccessesToStackVar++;
1780 return;
1781 }
1782 }
1783
1784 if (O.IsWrite)
1785 NumInstrumentedWrites++;
1786 else
1787 NumInstrumentedReads++;
1788
1789 unsigned Granularity = 1 << Mapping.Scale;
1790 if (O.MaybeMask) {
1791 instrumentMaskedLoadOrStore(this, DL, IntptrTy, O.MaybeMask, O.MaybeEVL,
1792 O.MaybeStride, O.getInsn(), Addr, O.Alignment,
1793 Granularity, O.OpType, O.IsWrite, nullptr,
1794 UseCalls, Exp, RTCI);
1795 } else {
1796 doInstrumentAddress(this, O.getInsn(), O.getInsn(), Addr, O.Alignment,
1797 Granularity, O.TypeStoreSize, O.IsWrite, nullptr,
1798 UseCalls, Exp, RTCI);
1799 }
1800}
1801
1802Instruction *AddressSanitizer::generateCrashCode(Instruction *InsertBefore,
1803 Value *Addr, bool IsWrite,
1804 size_t AccessSizeIndex,
1805 Value *SizeArgument,
1806 uint32_t Exp,
1807 RuntimeCallInserter &RTCI) {
1808 InstrumentationIRBuilder IRB(InsertBefore);
1809 Value *ExpVal = Exp == 0 ? nullptr : ConstantInt::get(IRB.getInt32Ty(), Exp);
1810 CallInst *Call = nullptr;
1811 if (SizeArgument) {
1812 if (Exp == 0)
1813 Call = RTCI.createRuntimeCall(IRB, AsanErrorCallbackSized[IsWrite][0],
1814 {Addr, SizeArgument});
1815 else
1816 Call = RTCI.createRuntimeCall(IRB, AsanErrorCallbackSized[IsWrite][1],
1817 {Addr, SizeArgument, ExpVal});
1818 } else {
1819 if (Exp == 0)
1820 Call = RTCI.createRuntimeCall(
1821 IRB, AsanErrorCallback[IsWrite][0][AccessSizeIndex], Addr);
1822 else
1823 Call = RTCI.createRuntimeCall(
1824 IRB, AsanErrorCallback[IsWrite][1][AccessSizeIndex], {Addr, ExpVal});
1825 }
1826
1828 return Call;
1829}
1830
1831Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
1832 Value *ShadowValue,
1833 uint32_t TypeStoreSize) {
1834 size_t Granularity = static_cast<size_t>(1) << Mapping.Scale;
1835 // Addr & (Granularity - 1)
1836 Value *LastAccessedByte =
1837 IRB.CreateAnd(AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
1838 // (Addr & (Granularity - 1)) + size - 1
1839 if (TypeStoreSize / 8 > 1)
1840 LastAccessedByte = IRB.CreateAdd(
1841 LastAccessedByte, ConstantInt::get(IntptrTy, TypeStoreSize / 8 - 1));
1842 // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
1843 LastAccessedByte =
1844 IRB.CreateIntCast(LastAccessedByte, ShadowValue->getType(), false);
1845 // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
1846 return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
1847}
1848
1849Instruction *AddressSanitizer::instrumentAMDGPUAddress(
1850 Instruction *OrigIns, Instruction *InsertBefore, Value *Addr,
1851 uint32_t TypeStoreSize, bool IsWrite, Value *SizeArgument) {
1852 // Do not instrument unsupported addrspaces.
1854 return nullptr;
1855 Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType());
1856 // Follow host instrumentation for global and constant addresses.
1857 if (PtrTy->getPointerAddressSpace() != 0)
1858 return InsertBefore;
1859 // Instrument generic addresses in supported addressspaces.
1860 IRBuilder<> IRB(InsertBefore);
1861 Value *IsShared = IRB.CreateCall(AMDGPUAddressShared, {Addr});
1862 Value *IsPrivate = IRB.CreateCall(AMDGPUAddressPrivate, {Addr});
1863 Value *IsSharedOrPrivate = IRB.CreateOr(IsShared, IsPrivate);
1864 Value *Cmp = IRB.CreateNot(IsSharedOrPrivate);
1865 Value *AddrSpaceZeroLanding =
1866 SplitBlockAndInsertIfThen(Cmp, InsertBefore, false);
1867 InsertBefore = cast<Instruction>(AddrSpaceZeroLanding);
1868 return InsertBefore;
1869}
1870
1871Instruction *AddressSanitizer::genAMDGPUReportBlock(IRBuilder<> &IRB,
1872 Value *Cond, bool Recover) {
1873 Module &M = *IRB.GetInsertBlock()->getModule();
1874 Value *ReportCond = Cond;
1875 if (!Recover) {
1876 auto Ballot = M.getOrInsertFunction(kAMDGPUBallotName, IRB.getInt64Ty(),
1877 IRB.getInt1Ty());
1878 ReportCond = IRB.CreateIsNotNull(IRB.CreateCall(Ballot, {Cond}));
1879 }
1880
1881 auto *Trm =
1882 SplitBlockAndInsertIfThen(ReportCond, &*IRB.GetInsertPoint(), false,
1884 Trm->getParent()->setName("asan.report");
1885
1886 if (Recover)
1887 return Trm;
1888
1889 Trm = SplitBlockAndInsertIfThen(Cond, Trm, false);
1890 IRB.SetInsertPoint(Trm);
1891 return IRB.CreateCall(
1892 M.getOrInsertFunction(kAMDGPUUnreachableName, IRB.getVoidTy()), {});
1893}
1894
1895void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
1896 Instruction *InsertBefore, Value *Addr,
1897 MaybeAlign Alignment,
1898 uint32_t TypeStoreSize, bool IsWrite,
1899 Value *SizeArgument, bool UseCalls,
1900 uint32_t Exp,
1901 RuntimeCallInserter &RTCI) {
1902 if (TargetTriple.isAMDGPU()) {
1903 InsertBefore = instrumentAMDGPUAddress(OrigIns, InsertBefore, Addr,
1904 TypeStoreSize, IsWrite, SizeArgument);
1905 if (!InsertBefore)
1906 return;
1907 }
1908
1909 InstrumentationIRBuilder IRB(InsertBefore);
1910 size_t AccessSizeIndex = TypeStoreSizeToSizeIndex(TypeStoreSize);
1911
1912 if (UseCalls && ClOptimizeCallbacks) {
1913 const ASanAccessInfo AccessInfo(IsWrite, CompileKernel, AccessSizeIndex);
1914 IRB.CreateIntrinsic(Intrinsic::asan_check_memaccess, {},
1915 {IRB.CreatePointerCast(Addr, PtrTy),
1916 ConstantInt::get(Int32Ty, AccessInfo.Packed)});
1917 return;
1918 }
1919
1920 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
1921 if (UseCalls) {
1922 if (Exp == 0)
1923 RTCI.createRuntimeCall(
1924 IRB, AsanMemoryAccessCallback[IsWrite][0][AccessSizeIndex], AddrLong);
1925 else
1926 RTCI.createRuntimeCall(
1927 IRB, AsanMemoryAccessCallback[IsWrite][1][AccessSizeIndex],
1928 {AddrLong, ConstantInt::get(IRB.getInt32Ty(), Exp)});
1929 return;
1930 }
1931
1932 Type *ShadowTy =
1933 IntegerType::get(*C, std::max(8U, TypeStoreSize >> Mapping.Scale));
1934 Type *ShadowPtrTy = PointerType::get(*C, 0);
1935 Value *ShadowPtr = memToShadow(AddrLong, IRB);
1936 const uint64_t ShadowAlign =
1937 std::max<uint64_t>(Alignment.valueOrOne().value() >> Mapping.Scale, 1);
1938 Value *ShadowValue = IRB.CreateAlignedLoad(
1939 ShadowTy, IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy), Align(ShadowAlign));
1940
1941 Value *Cmp = IRB.CreateIsNotNull(ShadowValue);
1942 size_t Granularity = 1ULL << Mapping.Scale;
1943 Instruction *CrashTerm = nullptr;
1944
1945 bool GenSlowPath = (ClAlwaysSlowPath || (TypeStoreSize < 8 * Granularity));
1946
1947 if (TargetTriple.isAMDGCN()) {
1948 if (GenSlowPath) {
1949 auto *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeStoreSize);
1950 Cmp = IRB.CreateAnd(Cmp, Cmp2);
1951 }
1952 CrashTerm = genAMDGPUReportBlock(IRB, Cmp, Recover);
1953 } else if (GenSlowPath) {
1954 // We use branch weights for the slow path check, to indicate that the slow
1955 // path is rarely taken. This seems to be the case for SPEC benchmarks.
1957 Cmp, InsertBefore, false, MDBuilder(*C).createUnlikelyBranchWeights());
1958 assert(cast<BranchInst>(CheckTerm)->isUnconditional());
1959 BasicBlock *NextBB = CheckTerm->getSuccessor(0);
1960 IRB.SetInsertPoint(CheckTerm);
1961 Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeStoreSize);
1962 if (Recover) {
1963 CrashTerm = SplitBlockAndInsertIfThen(Cmp2, CheckTerm, false);
1964 } else {
1965 BasicBlock *CrashBlock =
1966 BasicBlock::Create(*C, "", NextBB->getParent(), NextBB);
1967 CrashTerm = new UnreachableInst(*C, CrashBlock);
1968 BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
1969 ReplaceInstWithInst(CheckTerm, NewTerm);
1970 }
1971 } else {
1972 CrashTerm = SplitBlockAndInsertIfThen(Cmp, InsertBefore, !Recover);
1973 }
1974
1975 Instruction *Crash = generateCrashCode(
1976 CrashTerm, AddrLong, IsWrite, AccessSizeIndex, SizeArgument, Exp, RTCI);
1977 if (OrigIns->getDebugLoc())
1978 Crash->setDebugLoc(OrigIns->getDebugLoc());
1979}
1980
1981// Instrument unusual size or unusual alignment.
1982// We can not do it with a single check, so we do 1-byte check for the first
1983// and the last bytes. We call __asan_report_*_n(addr, real_size) to be able
1984// to report the actual access size.
1985void AddressSanitizer::instrumentUnusualSizeOrAlignment(
1986 Instruction *I, Instruction *InsertBefore, Value *Addr,
1987 TypeSize TypeStoreSize, bool IsWrite, Value *SizeArgument, bool UseCalls,
1988 uint32_t Exp, RuntimeCallInserter &RTCI) {
1989 InstrumentationIRBuilder IRB(InsertBefore);
1990 Value *NumBits = IRB.CreateTypeSize(IntptrTy, TypeStoreSize);
1991 Value *Size = IRB.CreateLShr(NumBits, ConstantInt::get(IntptrTy, 3));
1992
1993 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
1994 if (UseCalls) {
1995 if (Exp == 0)
1996 RTCI.createRuntimeCall(IRB, AsanMemoryAccessCallbackSized[IsWrite][0],
1997 {AddrLong, Size});
1998 else
1999 RTCI.createRuntimeCall(
2000 IRB, AsanMemoryAccessCallbackSized[IsWrite][1],
2001 {AddrLong, Size, ConstantInt::get(IRB.getInt32Ty(), Exp)});
2002 } else {
2003 Value *SizeMinusOne = IRB.CreateSub(Size, ConstantInt::get(IntptrTy, 1));
2004 Value *LastByte = IRB.CreateIntToPtr(
2005 IRB.CreateAdd(AddrLong, SizeMinusOne),
2006 Addr->getType());
2007 instrumentAddress(I, InsertBefore, Addr, {}, 8, IsWrite, Size, false, Exp,
2008 RTCI);
2009 instrumentAddress(I, InsertBefore, LastByte, {}, 8, IsWrite, Size, false,
2010 Exp, RTCI);
2011 }
2012}
2013
2014void ModuleAddressSanitizer::poisonOneInitializer(Function &GlobalInit) {
2015 // Set up the arguments to our poison/unpoison functions.
2016 IRBuilder<> IRB(&GlobalInit.front(),
2017 GlobalInit.front().getFirstInsertionPt());
2018
2019 // Add a call to poison all external globals before the given function starts.
2020 Value *ModuleNameAddr =
2021 ConstantExpr::getPointerCast(getOrCreateModuleName(), IntptrTy);
2022 IRB.CreateCall(AsanPoisonGlobals, ModuleNameAddr);
2023
2024 // Add calls to unpoison all globals before each return instruction.
2025 for (auto &BB : GlobalInit)
2027 CallInst::Create(AsanUnpoisonGlobals, "", RI->getIterator());
2028}
2029
2030void ModuleAddressSanitizer::createInitializerPoisonCalls() {
2031 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
2032 if (!GV)
2033 return;
2034
2036 if (!CA)
2037 return;
2038
2039 for (Use &OP : CA->operands()) {
2040 if (isa<ConstantAggregateZero>(OP)) continue;
2042
2043 // Must have a function or null ptr.
2044 if (Function *F = dyn_cast<Function>(CS->getOperand(1))) {
2045 if (F->getName() == kAsanModuleCtorName) continue;
2046 auto *Priority = cast<ConstantInt>(CS->getOperand(0));
2047 // Don't instrument CTORs that will run before asan.module_ctor.
2048 if (Priority->getLimitedValue() <= GetCtorAndDtorPriority(TargetTriple))
2049 continue;
2050 poisonOneInitializer(*F);
2051 }
2052 }
2053}
2054
2055const GlobalVariable *
2056ModuleAddressSanitizer::getExcludedAliasedGlobal(const GlobalAlias &GA) const {
2057 // In case this function should be expanded to include rules that do not just
2058 // apply when CompileKernel is true, either guard all existing rules with an
2059 // 'if (CompileKernel) { ... }' or be absolutely sure that all these rules
2060 // should also apply to user space.
2061 assert(CompileKernel && "Only expecting to be called when compiling kernel");
2062
2063 const Constant *C = GA.getAliasee();
2064
2065 // When compiling the kernel, globals that are aliased by symbols prefixed
2066 // by "__" are special and cannot be padded with a redzone.
2067 if (GA.getName().starts_with("__"))
2068 return dyn_cast<GlobalVariable>(C->stripPointerCastsAndAliases());
2069
2070 return nullptr;
2071}
2072
2073bool ModuleAddressSanitizer::shouldInstrumentGlobal(GlobalVariable *G) const {
2074 Type *Ty = G->getValueType();
2075 LLVM_DEBUG(dbgs() << "GLOBAL: " << *G << "\n");
2076
2077 if (G->hasSanitizerMetadata() && G->getSanitizerMetadata().NoAddress)
2078 return false;
2079 if (!Ty->isSized()) return false;
2080 if (!G->hasInitializer()) return false;
2081 // Globals in address space 1 and 4 are supported for AMDGPU.
2082 if (G->getAddressSpace() &&
2083 !(TargetTriple.isAMDGPU() && !isUnsupportedAMDGPUAddrspace(G)))
2084 return false;
2085 if (GlobalWasGeneratedByCompiler(G)) return false; // Our own globals.
2086 // Two problems with thread-locals:
2087 // - The address of the main thread's copy can't be computed at link-time.
2088 // - Need to poison all copies, not just the main thread's one.
2089 if (G->isThreadLocal()) return false;
2090 // For now, just ignore this Global if the alignment is large.
2091 if (G->getAlign() && *G->getAlign() > getMinRedzoneSizeForGlobal()) return false;
2092
2093 // For non-COFF targets, only instrument globals known to be defined by this
2094 // TU.
2095 // FIXME: We can instrument comdat globals on ELF if we are using the
2096 // GC-friendly metadata scheme.
2097 if (!TargetTriple.isOSBinFormatCOFF()) {
2098 if (!G->hasExactDefinition() || G->hasComdat())
2099 return false;
2100 } else {
2101 // On COFF, don't instrument non-ODR linkages.
2102 if (G->isInterposable())
2103 return false;
2104 // If the global has AvailableExternally linkage, then it is not in this
2105 // module, which means it does not need to be instrumented.
2106 if (G->hasAvailableExternallyLinkage())
2107 return false;
2108 }
2109
2110 // If a comdat is present, it must have a selection kind that implies ODR
2111 // semantics: no duplicates, any, or exact match.
2112 if (Comdat *C = G->getComdat()) {
2113 switch (C->getSelectionKind()) {
2114 case Comdat::Any:
2115 case Comdat::ExactMatch:
2117 break;
2118 case Comdat::Largest:
2119 case Comdat::SameSize:
2120 return false;
2121 }
2122 }
2123
2124 if (G->hasSection()) {
2125 // The kernel uses explicit sections for mostly special global variables
2126 // that we should not instrument. E.g. the kernel may rely on their layout
2127 // without redzones, or remove them at link time ("discard.*"), etc.
2128 if (CompileKernel)
2129 return false;
2130
2131 StringRef Section = G->getSection();
2132
2133 // Globals from llvm.metadata aren't emitted, do not instrument them.
2134 if (Section == "llvm.metadata") return false;
2135 // Do not instrument globals from special LLVM sections.
2136 if (Section.contains("__llvm") || Section.contains("__LLVM"))
2137 return false;
2138
2139 // Do not instrument function pointers to initialization and termination
2140 // routines: dynamic linker will not properly handle redzones.
2141 if (Section.starts_with(".preinit_array") ||
2142 Section.starts_with(".init_array") ||
2143 Section.starts_with(".fini_array")) {
2144 return false;
2145 }
2146
2147 // Do not instrument user-defined sections (with names resembling
2148 // valid C identifiers)
2149 if (TargetTriple.isOSBinFormatELF()) {
2150 if (llvm::all_of(Section,
2151 [](char c) { return llvm::isAlnum(c) || c == '_'; }))
2152 return false;
2153 }
2154
2155 // On COFF, if the section name contains '$', it is highly likely that the
2156 // user is using section sorting to create an array of globals similar to
2157 // the way initialization callbacks are registered in .init_array and
2158 // .CRT$XCU. The ATL also registers things in .ATL$__[azm]. Adding redzones
2159 // to such globals is counterproductive, because the intent is that they
2160 // will form an array, and out-of-bounds accesses are expected.
2161 // See https://github.com/google/sanitizers/issues/305
2162 // and http://msdn.microsoft.com/en-US/en-en/library/bb918180(v=vs.120).aspx
2163 if (TargetTriple.isOSBinFormatCOFF() && Section.contains('$')) {
2164 LLVM_DEBUG(dbgs() << "Ignoring global in sorted section (contains '$'): "
2165 << *G << "\n");
2166 return false;
2167 }
2168
2169 if (TargetTriple.isOSBinFormatMachO()) {
2170 StringRef ParsedSegment, ParsedSection;
2171 unsigned TAA = 0, StubSize = 0;
2172 bool TAAParsed;
2174 Section, ParsedSegment, ParsedSection, TAA, TAAParsed, StubSize));
2175
2176 // Ignore the globals from the __OBJC section. The ObjC runtime assumes
2177 // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
2178 // them.
2179 if (ParsedSegment == "__OBJC" ||
2180 (ParsedSegment == "__DATA" && ParsedSection.starts_with("__objc_"))) {
2181 LLVM_DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G << "\n");
2182 return false;
2183 }
2184 // See https://github.com/google/sanitizers/issues/32
2185 // Constant CFString instances are compiled in the following way:
2186 // -- the string buffer is emitted into
2187 // __TEXT,__cstring,cstring_literals
2188 // -- the constant NSConstantString structure referencing that buffer
2189 // is placed into __DATA,__cfstring
2190 // Therefore there's no point in placing redzones into __DATA,__cfstring.
2191 // Moreover, it causes the linker to crash on OS X 10.7
2192 if (ParsedSegment == "__DATA" && ParsedSection == "__cfstring") {
2193 LLVM_DEBUG(dbgs() << "Ignoring CFString: " << *G << "\n");
2194 return false;
2195 }
2196 // The linker merges the contents of cstring_literals and removes the
2197 // trailing zeroes.
2198 if (ParsedSegment == "__TEXT" && (TAA & MachO::S_CSTRING_LITERALS)) {
2199 LLVM_DEBUG(dbgs() << "Ignoring a cstring literal: " << *G << "\n");
2200 return false;
2201 }
2202 }
2203 }
2204
2205 if (CompileKernel) {
2206 // Globals that prefixed by "__" are special and cannot be padded with a
2207 // redzone.
2208 if (G->getName().starts_with("__"))
2209 return false;
2210 }
2211
2212 return true;
2213}
2214
2215// On Mach-O platforms, we emit global metadata in a separate section of the
2216// binary in order to allow the linker to properly dead strip. This is only
2217// supported on recent versions of ld64.
2218bool ModuleAddressSanitizer::ShouldUseMachOGlobalsSection() const {
2219 if (!TargetTriple.isOSBinFormatMachO())
2220 return false;
2221
2222 if (TargetTriple.isMacOSX() && !TargetTriple.isMacOSXVersionLT(10, 11))
2223 return true;
2224 if (TargetTriple.isiOS() /* or tvOS */ && !TargetTriple.isOSVersionLT(9))
2225 return true;
2226 if (TargetTriple.isWatchOS() && !TargetTriple.isOSVersionLT(2))
2227 return true;
2228 if (TargetTriple.isDriverKit())
2229 return true;
2230 if (TargetTriple.isXROS())
2231 return true;
2232
2233 return false;
2234}
2235
2236StringRef ModuleAddressSanitizer::getGlobalMetadataSection() const {
2237 switch (TargetTriple.getObjectFormat()) {
2238 case Triple::COFF: return ".ASAN$GL";
2239 case Triple::ELF: return "asan_globals";
2240 case Triple::MachO: return "__DATA,__asan_globals,regular";
2241 case Triple::Wasm:
2242 case Triple::GOFF:
2243 case Triple::SPIRV:
2244 case Triple::XCOFF:
2247 "ModuleAddressSanitizer not implemented for object file format");
2249 break;
2250 }
2251 llvm_unreachable("unsupported object format");
2252}
2253
2254void ModuleAddressSanitizer::initializeCallbacks() {
2255 IRBuilder<> IRB(*C);
2256
2257 // Declare our poisoning and unpoisoning functions.
2258 AsanPoisonGlobals =
2259 M.getOrInsertFunction(kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy);
2260 AsanUnpoisonGlobals =
2261 M.getOrInsertFunction(kAsanUnpoisonGlobalsName, IRB.getVoidTy());
2262
2263 // Declare functions that register/unregister globals.
2264 AsanRegisterGlobals = M.getOrInsertFunction(
2265 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy);
2266 AsanUnregisterGlobals = M.getOrInsertFunction(
2267 kAsanUnregisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy);
2268
2269 // Declare the functions that find globals in a shared object and then invoke
2270 // the (un)register function on them.
2271 AsanRegisterImageGlobals = M.getOrInsertFunction(
2272 kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy);
2273 AsanUnregisterImageGlobals = M.getOrInsertFunction(
2275
2276 AsanRegisterElfGlobals =
2277 M.getOrInsertFunction(kAsanRegisterElfGlobalsName, IRB.getVoidTy(),
2278 IntptrTy, IntptrTy, IntptrTy);
2279 AsanUnregisterElfGlobals =
2280 M.getOrInsertFunction(kAsanUnregisterElfGlobalsName, IRB.getVoidTy(),
2281 IntptrTy, IntptrTy, IntptrTy);
2282}
2283
2284// Put the metadata and the instrumented global in the same group. This ensures
2285// that the metadata is discarded if the instrumented global is discarded.
2286void ModuleAddressSanitizer::SetComdatForGlobalMetadata(
2287 GlobalVariable *G, GlobalVariable *Metadata, StringRef InternalSuffix) {
2288 Module &M = *G->getParent();
2289 Comdat *C = G->getComdat();
2290 if (!C) {
2291 if (!G->hasName()) {
2292 // If G is unnamed, it must be internal. Give it an artificial name
2293 // so we can put it in a comdat.
2294 assert(G->hasLocalLinkage());
2295 G->setName(genName("anon_global"));
2296 }
2297
2298 if (!InternalSuffix.empty() && G->hasLocalLinkage()) {
2299 std::string Name = std::string(G->getName());
2300 Name += InternalSuffix;
2301 C = M.getOrInsertComdat(Name);
2302 } else {
2303 C = M.getOrInsertComdat(G->getName());
2304 }
2305
2306 // Make this IMAGE_COMDAT_SELECT_NODUPLICATES on COFF. Also upgrade private
2307 // linkage to internal linkage so that a symbol table entry is emitted. This
2308 // is necessary in order to create the comdat group.
2309 if (TargetTriple.isOSBinFormatCOFF()) {
2310 C->setSelectionKind(Comdat::NoDeduplicate);
2311 if (G->hasPrivateLinkage())
2312 G->setLinkage(GlobalValue::InternalLinkage);
2313 }
2314 G->setComdat(C);
2315 }
2316
2317 assert(G->hasComdat());
2318 Metadata->setComdat(G->getComdat());
2319}
2320
2321// Create a separate metadata global and put it in the appropriate ASan
2322// global registration section.
2324ModuleAddressSanitizer::CreateMetadataGlobal(Constant *Initializer,
2325 StringRef OriginalName) {
2326 auto Linkage = TargetTriple.isOSBinFormatMachO()
2330 M, Initializer->getType(), false, Linkage, Initializer,
2331 Twine("__asan_global_") + GlobalValue::dropLLVMManglingEscape(OriginalName));
2332 Metadata->setSection(getGlobalMetadataSection());
2333 // Place metadata in a large section for x86-64 ELF binaries to mitigate
2334 // relocation pressure.
2336 return Metadata;
2337}
2338
2339Instruction *ModuleAddressSanitizer::CreateAsanModuleDtor() {
2340 AsanDtorFunction = Function::createWithDefaultAttr(
2343 AsanDtorFunction->addFnAttr(Attribute::NoUnwind);
2344 // Ensure Dtor cannot be discarded, even if in a comdat.
2345 appendToUsed(M, {AsanDtorFunction});
2346 BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
2347
2348 return ReturnInst::Create(*C, AsanDtorBB);
2349}
2350
2351void ModuleAddressSanitizer::InstrumentGlobalsCOFF(
2352 IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
2353 ArrayRef<Constant *> MetadataInitializers) {
2354 assert(ExtendedGlobals.size() == MetadataInitializers.size());
2355 auto &DL = M.getDataLayout();
2356
2357 SmallVector<GlobalValue *, 16> MetadataGlobals(ExtendedGlobals.size());
2358 for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
2359 Constant *Initializer = MetadataInitializers[i];
2360 GlobalVariable *G = ExtendedGlobals[i];
2361 GlobalVariable *Metadata = CreateMetadataGlobal(Initializer, G->getName());
2362 MDNode *MD = MDNode::get(M.getContext(), ValueAsMetadata::get(G));
2363 Metadata->setMetadata(LLVMContext::MD_associated, MD);
2364 MetadataGlobals[i] = Metadata;
2365
2366 // The MSVC linker always inserts padding when linking incrementally. We
2367 // cope with that by aligning each struct to its size, which must be a power
2368 // of two.
2369 unsigned SizeOfGlobalStruct = DL.getTypeAllocSize(Initializer->getType());
2370 assert(isPowerOf2_32(SizeOfGlobalStruct) &&
2371 "global metadata will not be padded appropriately");
2372 Metadata->setAlignment(assumeAligned(SizeOfGlobalStruct));
2373
2374 SetComdatForGlobalMetadata(G, Metadata, "");
2375 }
2376
2377 // Update llvm.compiler.used, adding the new metadata globals. This is
2378 // needed so that during LTO these variables stay alive.
2379 if (!MetadataGlobals.empty())
2380 appendToCompilerUsed(M, MetadataGlobals);
2381}
2382
2383void ModuleAddressSanitizer::instrumentGlobalsELF(
2384 IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
2385 ArrayRef<Constant *> MetadataInitializers,
2386 const std::string &UniqueModuleId) {
2387 assert(ExtendedGlobals.size() == MetadataInitializers.size());
2388
2389 // Putting globals in a comdat changes the semantic and potentially cause
2390 // false negative odr violations at link time. If odr indicators are used, we
2391 // keep the comdat sections, as link time odr violations will be dectected on
2392 // the odr indicator symbols.
2393 bool UseComdatForGlobalsGC = UseOdrIndicator && !UniqueModuleId.empty();
2394
2395 SmallVector<GlobalValue *, 16> MetadataGlobals(ExtendedGlobals.size());
2396 for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
2397 GlobalVariable *G = ExtendedGlobals[i];
2399 CreateMetadataGlobal(MetadataInitializers[i], G->getName());
2400 MDNode *MD = MDNode::get(M.getContext(), ValueAsMetadata::get(G));
2401 Metadata->setMetadata(LLVMContext::MD_associated, MD);
2402 MetadataGlobals[i] = Metadata;
2403
2404 if (UseComdatForGlobalsGC)
2405 SetComdatForGlobalMetadata(G, Metadata, UniqueModuleId);
2406 }
2407
2408 // Update llvm.compiler.used, adding the new metadata globals. This is
2409 // needed so that during LTO these variables stay alive.
2410 if (!MetadataGlobals.empty())
2411 appendToCompilerUsed(M, MetadataGlobals);
2412
2413 // RegisteredFlag serves two purposes. First, we can pass it to dladdr()
2414 // to look up the loaded image that contains it. Second, we can store in it
2415 // whether registration has already occurred, to prevent duplicate
2416 // registration.
2417 //
2418 // Common linkage ensures that there is only one global per shared library.
2419 GlobalVariable *RegisteredFlag = new GlobalVariable(
2420 M, IntptrTy, false, GlobalVariable::CommonLinkage,
2421 ConstantInt::get(IntptrTy, 0), kAsanGlobalsRegisteredFlagName);
2423
2424 // Create start and stop symbols.
2425 GlobalVariable *StartELFMetadata = new GlobalVariable(
2426 M, IntptrTy, false, GlobalVariable::ExternalWeakLinkage, nullptr,
2427 "__start_" + getGlobalMetadataSection());
2429 GlobalVariable *StopELFMetadata = new GlobalVariable(
2430 M, IntptrTy, false, GlobalVariable::ExternalWeakLinkage, nullptr,
2431 "__stop_" + getGlobalMetadataSection());
2433
2434 // Create a call to register the globals with the runtime.
2435 if (ConstructorKind == AsanCtorKind::Global)
2436 IRB.CreateCall(AsanRegisterElfGlobals,
2437 {IRB.CreatePointerCast(RegisteredFlag, IntptrTy),
2438 IRB.CreatePointerCast(StartELFMetadata, IntptrTy),
2439 IRB.CreatePointerCast(StopELFMetadata, IntptrTy)});
2440
2441 // We also need to unregister globals at the end, e.g., when a shared library
2442 // gets closed.
2443 if (DestructorKind != AsanDtorKind::None && !MetadataGlobals.empty()) {
2444 IRBuilder<> IrbDtor(CreateAsanModuleDtor());
2445 IrbDtor.CreateCall(AsanUnregisterElfGlobals,
2446 {IRB.CreatePointerCast(RegisteredFlag, IntptrTy),
2447 IRB.CreatePointerCast(StartELFMetadata, IntptrTy),
2448 IRB.CreatePointerCast(StopELFMetadata, IntptrTy)});
2449 }
2450}
2451
2452void ModuleAddressSanitizer::InstrumentGlobalsMachO(
2453 IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
2454 ArrayRef<Constant *> MetadataInitializers) {
2455 assert(ExtendedGlobals.size() == MetadataInitializers.size());
2456
2457 // On recent Mach-O platforms, use a structure which binds the liveness of
2458 // the global variable to the metadata struct. Keep the list of "Liveness" GV
2459 // created to be added to llvm.compiler.used
2460 StructType *LivenessTy = StructType::get(IntptrTy, IntptrTy);
2461 SmallVector<GlobalValue *, 16> LivenessGlobals(ExtendedGlobals.size());
2462
2463 for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
2464 Constant *Initializer = MetadataInitializers[i];
2465 GlobalVariable *G = ExtendedGlobals[i];
2466 GlobalVariable *Metadata = CreateMetadataGlobal(Initializer, G->getName());
2467
2468 // On recent Mach-O platforms, we emit the global metadata in a way that
2469 // allows the linker to properly strip dead globals.
2470 auto LivenessBinder =
2471 ConstantStruct::get(LivenessTy, Initializer->getAggregateElement(0u),
2473 GlobalVariable *Liveness = new GlobalVariable(
2474 M, LivenessTy, false, GlobalVariable::InternalLinkage, LivenessBinder,
2475 Twine("__asan_binder_") + G->getName());
2476 Liveness->setSection("__DATA,__asan_liveness,regular,live_support");
2477 LivenessGlobals[i] = Liveness;
2478 }
2479
2480 // Update llvm.compiler.used, adding the new liveness globals. This is
2481 // needed so that during LTO these variables stay alive. The alternative
2482 // would be to have the linker handling the LTO symbols, but libLTO
2483 // current API does not expose access to the section for each symbol.
2484 if (!LivenessGlobals.empty())
2485 appendToCompilerUsed(M, LivenessGlobals);
2486
2487 // RegisteredFlag serves two purposes. First, we can pass it to dladdr()
2488 // to look up the loaded image that contains it. Second, we can store in it
2489 // whether registration has already occurred, to prevent duplicate
2490 // registration.
2491 //
2492 // common linkage ensures that there is only one global per shared library.
2493 GlobalVariable *RegisteredFlag = new GlobalVariable(
2494 M, IntptrTy, false, GlobalVariable::CommonLinkage,
2495 ConstantInt::get(IntptrTy, 0), kAsanGlobalsRegisteredFlagName);
2497
2498 if (ConstructorKind == AsanCtorKind::Global)
2499 IRB.CreateCall(AsanRegisterImageGlobals,
2500 {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
2501
2502 // We also need to unregister globals at the end, e.g., when a shared library
2503 // gets closed.
2504 if (DestructorKind != AsanDtorKind::None) {
2505 IRBuilder<> IrbDtor(CreateAsanModuleDtor());
2506 IrbDtor.CreateCall(AsanUnregisterImageGlobals,
2507 {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
2508 }
2509}
2510
2511void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray(
2512 IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
2513 ArrayRef<Constant *> MetadataInitializers) {
2514 assert(ExtendedGlobals.size() == MetadataInitializers.size());
2515 unsigned N = ExtendedGlobals.size();
2516 assert(N > 0);
2517
2518 // On platforms that don't have a custom metadata section, we emit an array
2519 // of global metadata structures.
2520 ArrayType *ArrayOfGlobalStructTy =
2521 ArrayType::get(MetadataInitializers[0]->getType(), N);
2522 auto AllGlobals = new GlobalVariable(
2523 M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage,
2524 ConstantArray::get(ArrayOfGlobalStructTy, MetadataInitializers), "");
2525 if (Mapping.Scale > 3)
2526 AllGlobals->setAlignment(Align(1ULL << Mapping.Scale));
2527
2528 if (ConstructorKind == AsanCtorKind::Global)
2529 IRB.CreateCall(AsanRegisterGlobals,
2530 {IRB.CreatePointerCast(AllGlobals, IntptrTy),
2531 ConstantInt::get(IntptrTy, N)});
2532
2533 // We also need to unregister globals at the end, e.g., when a shared library
2534 // gets closed.
2535 if (DestructorKind != AsanDtorKind::None) {
2536 IRBuilder<> IrbDtor(CreateAsanModuleDtor());
2537 IrbDtor.CreateCall(AsanUnregisterGlobals,
2538 {IRB.CreatePointerCast(AllGlobals, IntptrTy),
2539 ConstantInt::get(IntptrTy, N)});
2540 }
2541}
2542
2543// This function replaces all global variables with new variables that have
2544// trailing redzones. It also creates a function that poisons
2545// redzones and inserts this function into llvm.global_ctors.
2546// Sets *CtorComdat to true if the global registration code emitted into the
2547// asan constructor is comdat-compatible.
2548void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,
2549 bool *CtorComdat) {
2550 // Build set of globals that are aliased by some GA, where
2551 // getExcludedAliasedGlobal(GA) returns the relevant GlobalVariable.
2552 SmallPtrSet<const GlobalVariable *, 16> AliasedGlobalExclusions;
2553 if (CompileKernel) {
2554 for (auto &GA : M.aliases()) {
2555 if (const GlobalVariable *GV = getExcludedAliasedGlobal(GA))
2556 AliasedGlobalExclusions.insert(GV);
2557 }
2558 }
2559
2560 SmallVector<GlobalVariable *, 16> GlobalsToChange;
2561 for (auto &G : M.globals()) {
2562 if (!AliasedGlobalExclusions.count(&G) && shouldInstrumentGlobal(&G))
2563 GlobalsToChange.push_back(&G);
2564 }
2565
2566 size_t n = GlobalsToChange.size();
2567 auto &DL = M.getDataLayout();
2568
2569 // A global is described by a structure
2570 // size_t beg;
2571 // size_t size;
2572 // size_t size_with_redzone;
2573 // const char *name;
2574 // const char *module_name;
2575 // size_t has_dynamic_init;
2576 // size_t padding_for_windows_msvc_incremental_link;
2577 // size_t odr_indicator;
2578 // We initialize an array of such structures and pass it to a run-time call.
2579 StructType *GlobalStructTy =
2580 StructType::get(IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy,
2581 IntptrTy, IntptrTy, IntptrTy);
2583 SmallVector<Constant *, 16> Initializers(n);
2584
2585 for (size_t i = 0; i < n; i++) {
2586 GlobalVariable *G = GlobalsToChange[i];
2587
2589 if (G->hasSanitizerMetadata())
2590 MD = G->getSanitizerMetadata();
2591
2592 // The runtime library tries demangling symbol names in the descriptor but
2593 // functionality like __cxa_demangle may be unavailable (e.g.
2594 // -static-libstdc++). So we demangle the symbol names here.
2595 std::string NameForGlobal = G->getName().str();
2598 /*AllowMerging*/ true, genName("global"));
2599
2600 Type *Ty = G->getValueType();
2601 const uint64_t SizeInBytes = DL.getTypeAllocSize(Ty);
2602 const uint64_t RightRedzoneSize = getRedzoneSizeForGlobal(SizeInBytes);
2603 Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
2604
2605 StructType *NewTy = StructType::get(Ty, RightRedZoneTy);
2606 Constant *NewInitializer = ConstantStruct::get(
2607 NewTy, G->getInitializer(), Constant::getNullValue(RightRedZoneTy));
2608
2609 // Create a new global variable with enough space for a redzone.
2610 GlobalValue::LinkageTypes Linkage = G->getLinkage();
2611 if (G->isConstant() && Linkage == GlobalValue::PrivateLinkage)
2613 GlobalVariable *NewGlobal = new GlobalVariable(
2614 M, NewTy, G->isConstant(), Linkage, NewInitializer, "", G,
2615 G->getThreadLocalMode(), G->getAddressSpace());
2616 NewGlobal->copyAttributesFrom(G);
2617 NewGlobal->setComdat(G->getComdat());
2618 NewGlobal->setAlignment(Align(getMinRedzoneSizeForGlobal()));
2619 // Don't fold globals with redzones. ODR violation detector and redzone
2620 // poisoning implicitly creates a dependence on the global's address, so it
2621 // is no longer valid for it to be marked unnamed_addr.
2623
2624 // Move null-terminated C strings to "__asan_cstring" section on Darwin.
2625 if (TargetTriple.isOSBinFormatMachO() && !G->hasSection() &&
2626 G->isConstant()) {
2627 auto Seq = dyn_cast<ConstantDataSequential>(G->getInitializer());
2628 if (Seq && Seq->isCString())
2629 NewGlobal->setSection("__TEXT,__asan_cstring,regular");
2630 }
2631
2632 // Transfer the debug info and type metadata. The payload starts at offset
2633 // zero so we can copy the metadata over as is.
2634 NewGlobal->copyMetadata(G, 0);
2635
2636 Value *Indices2[2];
2637 Indices2[0] = IRB.getInt32(0);
2638 Indices2[1] = IRB.getInt32(0);
2639
2641 ConstantExpr::getGetElementPtr(NewTy, NewGlobal, Indices2, true));
2642 NewGlobal->takeName(G);
2643 G->eraseFromParent();
2644 NewGlobals[i] = NewGlobal;
2645
2646 Constant *ODRIndicator = ConstantPointerNull::get(PtrTy);
2647 GlobalValue *InstrumentedGlobal = NewGlobal;
2648
2649 bool CanUsePrivateAliases =
2650 TargetTriple.isOSBinFormatELF() || TargetTriple.isOSBinFormatMachO() ||
2651 TargetTriple.isOSBinFormatWasm();
2652 if (CanUsePrivateAliases && UsePrivateAlias) {
2653 // Create local alias for NewGlobal to avoid crash on ODR between
2654 // instrumented and non-instrumented libraries.
2655 InstrumentedGlobal =
2657 }
2658
2659 // ODR should not happen for local linkage.
2660 if (NewGlobal->hasLocalLinkage()) {
2661 ODRIndicator =
2662 ConstantExpr::getIntToPtr(ConstantInt::get(IntptrTy, -1), PtrTy);
2663 } else if (UseOdrIndicator) {
2664 // With local aliases, we need to provide another externally visible
2665 // symbol __odr_asan_XXX to detect ODR violation.
2666 auto *ODRIndicatorSym =
2667 new GlobalVariable(M, IRB.getInt8Ty(), false, Linkage,
2669 kODRGenPrefix + NameForGlobal, nullptr,
2670 NewGlobal->getThreadLocalMode());
2671
2672 // Set meaningful attributes for indicator symbol.
2673 ODRIndicatorSym->setVisibility(NewGlobal->getVisibility());
2674 ODRIndicatorSym->setDLLStorageClass(NewGlobal->getDLLStorageClass());
2675 ODRIndicatorSym->setAlignment(Align(1));
2676 ODRIndicator = ODRIndicatorSym;
2677 }
2678
2679 Constant *Initializer = ConstantStruct::get(
2680 GlobalStructTy,
2681 ConstantExpr::getPointerCast(InstrumentedGlobal, IntptrTy),
2682 ConstantInt::get(IntptrTy, SizeInBytes),
2683 ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
2684 ConstantExpr::getPointerCast(Name, IntptrTy),
2685 ConstantExpr::getPointerCast(getOrCreateModuleName(), IntptrTy),
2686 ConstantInt::get(IntptrTy, MD.IsDynInit),
2687 Constant::getNullValue(IntptrTy),
2688 ConstantExpr::getPointerCast(ODRIndicator, IntptrTy));
2689
2690 LLVM_DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
2691
2692 Initializers[i] = Initializer;
2693 }
2694
2695 // Add instrumented globals to llvm.compiler.used list to avoid LTO from
2696 // ConstantMerge'ing them.
2697 SmallVector<GlobalValue *, 16> GlobalsToAddToUsedList;
2698 for (size_t i = 0; i < n; i++) {
2699 GlobalVariable *G = NewGlobals[i];
2700 if (G->getName().empty()) continue;
2701 GlobalsToAddToUsedList.push_back(G);
2702 }
2703 appendToCompilerUsed(M, ArrayRef<GlobalValue *>(GlobalsToAddToUsedList));
2704
2705 if (UseGlobalsGC && TargetTriple.isOSBinFormatELF()) {
2706 // Use COMDAT and register globals even if n == 0 to ensure that (a) the
2707 // linkage unit will only have one module constructor, and (b) the register
2708 // function will be called. The module destructor is not created when n ==
2709 // 0.
2710 *CtorComdat = true;
2711 instrumentGlobalsELF(IRB, NewGlobals, Initializers, getUniqueModuleId(&M));
2712 } else if (n == 0) {
2713 // When UseGlobalsGC is false, COMDAT can still be used if n == 0, because
2714 // all compile units will have identical module constructor/destructor.
2715 *CtorComdat = TargetTriple.isOSBinFormatELF();
2716 } else {
2717 *CtorComdat = false;
2718 if (UseGlobalsGC && TargetTriple.isOSBinFormatCOFF()) {
2719 InstrumentGlobalsCOFF(IRB, NewGlobals, Initializers);
2720 } else if (UseGlobalsGC && ShouldUseMachOGlobalsSection()) {
2721 InstrumentGlobalsMachO(IRB, NewGlobals, Initializers);
2722 } else {
2723 InstrumentGlobalsWithMetadataArray(IRB, NewGlobals, Initializers);
2724 }
2725 }
2726
2727 // Create calls for poisoning before initializers run and unpoisoning after.
2728 if (ClInitializers)
2729 createInitializerPoisonCalls();
2730
2731 LLVM_DEBUG(dbgs() << M);
2732}
2733
2734uint64_t
2735ModuleAddressSanitizer::getRedzoneSizeForGlobal(uint64_t SizeInBytes) const {
2736 constexpr uint64_t kMaxRZ = 1 << 18;
2737 const uint64_t MinRZ = getMinRedzoneSizeForGlobal();
2738
2739 uint64_t RZ = 0;
2740 if (SizeInBytes <= MinRZ / 2) {
2741 // Reduce redzone size for small size objects, e.g. int, char[1]. MinRZ is
2742 // at least 32 bytes, optimize when SizeInBytes is less than or equal to
2743 // half of MinRZ.
2744 RZ = MinRZ - SizeInBytes;
2745 } else {
2746 // Calculate RZ, where MinRZ <= RZ <= MaxRZ, and RZ ~ 1/4 * SizeInBytes.
2747 RZ = std::clamp((SizeInBytes / MinRZ / 4) * MinRZ, MinRZ, kMaxRZ);
2748
2749 // Round up to multiple of MinRZ.
2750 if (SizeInBytes % MinRZ)
2751 RZ += MinRZ - (SizeInBytes % MinRZ);
2752 }
2753
2754 assert((RZ + SizeInBytes) % MinRZ == 0);
2755
2756 return RZ;
2757}
2758
2759int ModuleAddressSanitizer::GetAsanVersion() const {
2760 int LongSize = M.getDataLayout().getPointerSizeInBits();
2761 bool isAndroid = M.getTargetTriple().isAndroid();
2762 int Version = 8;
2763 // 32-bit Android is one version ahead because of the switch to dynamic
2764 // shadow.
2765 Version += (LongSize == 32 && isAndroid);
2766 return Version;
2767}
2768
2769GlobalVariable *ModuleAddressSanitizer::getOrCreateModuleName() {
2770 if (!ModuleName) {
2771 // We shouldn't merge same module names, as this string serves as unique
2772 // module ID in runtime.
2773 ModuleName =
2774 createPrivateGlobalForString(M, M.getModuleIdentifier(),
2775 /*AllowMerging*/ false, genName("module"));
2776 }
2777 return ModuleName;
2778}
2779
2780bool ModuleAddressSanitizer::instrumentModule() {
2781 initializeCallbacks();
2782
2783 for (Function &F : M)
2784 removeASanIncompatibleFnAttributes(F, /*ReadsArgMem=*/false);
2785
2786 // Create a module constructor. A destructor is created lazily because not all
2787 // platforms, and not all modules need it.
2788 if (ConstructorKind == AsanCtorKind::Global) {
2789 if (CompileKernel) {
2790 // The kernel always builds with its own runtime, and therefore does not
2791 // need the init and version check calls.
2792 AsanCtorFunction = createSanitizerCtor(M, kAsanModuleCtorName);
2793 } else {
2794 std::string AsanVersion = std::to_string(GetAsanVersion());
2795 std::string VersionCheckName =
2796 InsertVersionCheck ? (kAsanVersionCheckNamePrefix + AsanVersion) : "";
2797 std::tie(AsanCtorFunction, std::ignore) =
2799 M, kAsanModuleCtorName, kAsanInitName, /*InitArgTypes=*/{},
2800 /*InitArgs=*/{}, VersionCheckName);
2801 }
2802 }
2803
2804 bool CtorComdat = true;
2805 if (ClGlobals) {
2806 assert(AsanCtorFunction || ConstructorKind == AsanCtorKind::None);
2807 if (AsanCtorFunction) {
2808 IRBuilder<> IRB(AsanCtorFunction->getEntryBlock().getTerminator());
2809 instrumentGlobals(IRB, &CtorComdat);
2810 } else {
2811 IRBuilder<> IRB(*C);
2812 instrumentGlobals(IRB, &CtorComdat);
2813 }
2814 }
2815
2816 const uint64_t Priority = GetCtorAndDtorPriority(TargetTriple);
2817
2818 // Put the constructor and destructor in comdat if both
2819 // (1) global instrumentation is not TU-specific
2820 // (2) target is ELF.
2821 if (UseCtorComdat && TargetTriple.isOSBinFormatELF() && CtorComdat) {
2822 if (AsanCtorFunction) {
2823 AsanCtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleCtorName));
2824 appendToGlobalCtors(M, AsanCtorFunction, Priority, AsanCtorFunction);
2825 }
2826 if (AsanDtorFunction) {
2827 AsanDtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleDtorName));
2828 appendToGlobalDtors(M, AsanDtorFunction, Priority, AsanDtorFunction);
2829 }
2830 } else {
2831 if (AsanCtorFunction)
2832 appendToGlobalCtors(M, AsanCtorFunction, Priority);
2833 if (AsanDtorFunction)
2834 appendToGlobalDtors(M, AsanDtorFunction, Priority);
2835 }
2836
2837 return true;
2838}
2839
2840void AddressSanitizer::initializeCallbacks(const TargetLibraryInfo *TLI) {
2841 IRBuilder<> IRB(*C);
2842 // Create __asan_report* callbacks.
2843 // IsWrite, TypeSize and Exp are encoded in the function name.
2844 for (int Exp = 0; Exp < 2; Exp++) {
2845 for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
2846 const std::string TypeStr = AccessIsWrite ? "store" : "load";
2847 const std::string ExpStr = Exp ? "exp_" : "";
2848 const std::string EndingStr = Recover ? "_noabort" : "";
2849
2850 SmallVector<Type *, 3> Args2 = {IntptrTy, IntptrTy};
2851 SmallVector<Type *, 2> Args1{1, IntptrTy};
2852 AttributeList AL2;
2853 AttributeList AL1;
2854 if (Exp) {
2855 Type *ExpType = Type::getInt32Ty(*C);
2856 Args2.push_back(ExpType);
2857 Args1.push_back(ExpType);
2858 if (auto AK = TLI->getExtAttrForI32Param(false)) {
2859 AL2 = AL2.addParamAttribute(*C, 2, AK);
2860 AL1 = AL1.addParamAttribute(*C, 1, AK);
2861 }
2862 }
2863 AsanErrorCallbackSized[AccessIsWrite][Exp] = M.getOrInsertFunction(
2864 kAsanReportErrorTemplate + ExpStr + TypeStr + "_n" + EndingStr,
2865 FunctionType::get(IRB.getVoidTy(), Args2, false), AL2);
2866
2867 AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] = M.getOrInsertFunction(
2868 ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr,
2869 FunctionType::get(IRB.getVoidTy(), Args2, false), AL2);
2870
2871 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
2872 AccessSizeIndex++) {
2873 const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex);
2874 AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] =
2875 M.getOrInsertFunction(
2876 kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr,
2877 FunctionType::get(IRB.getVoidTy(), Args1, false), AL1);
2878
2879 AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] =
2880 M.getOrInsertFunction(
2881 ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr,
2882 FunctionType::get(IRB.getVoidTy(), Args1, false), AL1);
2883 }
2884 }
2885 }
2886
2887 const std::string MemIntrinCallbackPrefix =
2888 (CompileKernel && !ClKasanMemIntrinCallbackPrefix)
2889 ? std::string("")
2891 AsanMemmove = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memmove",
2892 PtrTy, PtrTy, PtrTy, IntptrTy);
2893 AsanMemcpy = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memcpy", PtrTy,
2894 PtrTy, PtrTy, IntptrTy);
2895 AsanMemset = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memset",
2896 TLI->getAttrList(C, {1}, /*Signed=*/false),
2897 PtrTy, PtrTy, IRB.getInt32Ty(), IntptrTy);
2898
2899 AsanHandleNoReturnFunc =
2900 M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy());
2901
2902 AsanPtrCmpFunction =
2903 M.getOrInsertFunction(kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy);
2904 AsanPtrSubFunction =
2905 M.getOrInsertFunction(kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy);
2906 if (Mapping.InGlobal)
2907 AsanShadowGlobal = M.getOrInsertGlobal("__asan_shadow",
2908 ArrayType::get(IRB.getInt8Ty(), 0));
2909
2910 AMDGPUAddressShared =
2911 M.getOrInsertFunction(kAMDGPUAddressSharedName, IRB.getInt1Ty(), PtrTy);
2912 AMDGPUAddressPrivate =
2913 M.getOrInsertFunction(kAMDGPUAddressPrivateName, IRB.getInt1Ty(), PtrTy);
2914}
2915
2916bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
2917 // For each NSObject descendant having a +load method, this method is invoked
2918 // by the ObjC runtime before any of the static constructors is called.
2919 // Therefore we need to instrument such methods with a call to __asan_init
2920 // at the beginning in order to initialize our runtime before any access to
2921 // the shadow memory.
2922 // We cannot just ignore these methods, because they may call other
2923 // instrumented functions.
2924 if (F.getName().contains(" load]")) {
2925 FunctionCallee AsanInitFunction =
2926 declareSanitizerInitFunction(*F.getParent(), kAsanInitName, {});
2927 IRBuilder<> IRB(&F.front(), F.front().begin());
2928 IRB.CreateCall(AsanInitFunction, {});
2929 return true;
2930 }
2931 return false;
2932}
2933
2934bool AddressSanitizer::maybeInsertDynamicShadowAtFunctionEntry(Function &F) {
2935 // Generate code only when dynamic addressing is needed.
2936 if (Mapping.Offset != kDynamicShadowSentinel)
2937 return false;
2938
2939 IRBuilder<> IRB(&F.front().front());
2940 if (Mapping.InGlobal) {
2942 // An empty inline asm with input reg == output reg.
2943 // An opaque pointer-to-int cast, basically.
2945 FunctionType::get(IntptrTy, {AsanShadowGlobal->getType()}, false),
2946 StringRef(""), StringRef("=r,0"),
2947 /*hasSideEffects=*/false);
2948 LocalDynamicShadow =
2949 IRB.CreateCall(Asm, {AsanShadowGlobal}, ".asan.shadow");
2950 } else {
2951 LocalDynamicShadow =
2952 IRB.CreatePointerCast(AsanShadowGlobal, IntptrTy, ".asan.shadow");
2953 }
2954 } else {
2955 Value *GlobalDynamicAddress = F.getParent()->getOrInsertGlobal(
2957 LocalDynamicShadow = IRB.CreateLoad(IntptrTy, GlobalDynamicAddress);
2958 }
2959 return true;
2960}
2961
2962void AddressSanitizer::markEscapedLocalAllocas(Function &F) {
2963 // Find the one possible call to llvm.localescape and pre-mark allocas passed
2964 // to it as uninteresting. This assumes we haven't started processing allocas
2965 // yet. This check is done up front because iterating the use list in
2966 // isInterestingAlloca would be algorithmically slower.
2967 assert(ProcessedAllocas.empty() && "must process localescape before allocas");
2968
2969 // Try to get the declaration of llvm.localescape. If it's not in the module,
2970 // we can exit early.
2971 if (!F.getParent()->getFunction("llvm.localescape")) return;
2972
2973 // Look for a call to llvm.localescape call in the entry block. It can't be in
2974 // any other block.
2975 for (Instruction &I : F.getEntryBlock()) {
2977 if (II && II->getIntrinsicID() == Intrinsic::localescape) {
2978 // We found a call. Mark all the allocas passed in as uninteresting.
2979 for (Value *Arg : II->args()) {
2980 AllocaInst *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts());
2981 assert(AI && AI->isStaticAlloca() &&
2982 "non-static alloca arg to localescape");
2983 ProcessedAllocas[AI] = false;
2984 }
2985 break;
2986 }
2987 }
2988}
2989
2990bool AddressSanitizer::suppressInstrumentationSiteForDebug(int &Instrumented) {
2991 bool ShouldInstrument =
2992 ClDebugMin < 0 || ClDebugMax < 0 ||
2993 (Instrumented >= ClDebugMin && Instrumented <= ClDebugMax);
2994 Instrumented++;
2995 return !ShouldInstrument;
2996}
2997
2998bool AddressSanitizer::instrumentFunction(Function &F,
2999 const TargetLibraryInfo *TLI,
3000 const TargetTransformInfo *TTI) {
3001 bool FunctionModified = false;
3002
3003 // Do not apply any instrumentation for naked functions.
3004 if (F.hasFnAttribute(Attribute::Naked))
3005 return FunctionModified;
3006
3007 // If needed, insert __asan_init before checking for SanitizeAddress attr.
3008 // This function needs to be called even if the function body is not
3009 // instrumented.
3010 if (maybeInsertAsanInitAtFunctionEntry(F))
3011 FunctionModified = true;
3012
3013 // Leave if the function doesn't need instrumentation.
3014 if (!F.hasFnAttribute(Attribute::SanitizeAddress)) return FunctionModified;
3015
3016 if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
3017 return FunctionModified;
3018
3019 LLVM_DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
3020
3021 initializeCallbacks(TLI);
3022
3023 FunctionStateRAII CleanupObj(this);
3024
3025 RuntimeCallInserter RTCI(F);
3026
3027 FunctionModified |= maybeInsertDynamicShadowAtFunctionEntry(F);
3028
3029 // We can't instrument allocas used with llvm.localescape. Only static allocas
3030 // can be passed to that intrinsic.
3031 markEscapedLocalAllocas(F);
3032
3033 // We want to instrument every address only once per basic block (unless there
3034 // are calls between uses).
3035 SmallPtrSet<Value *, 16> TempsToInstrument;
3036 SmallVector<InterestingMemoryOperand, 16> OperandsToInstrument;
3037 SmallVector<MemIntrinsic *, 16> IntrinToInstrument;
3038 SmallVector<Instruction *, 8> NoReturnCalls;
3040 SmallVector<Instruction *, 16> PointerComparisonsOrSubtracts;
3041
3042 // Fill the set of memory operations to instrument.
3043 for (auto &BB : F) {
3044 AllBlocks.push_back(&BB);
3045 TempsToInstrument.clear();
3046 int NumInsnsPerBB = 0;
3047 for (auto &Inst : BB) {
3048 if (LooksLikeCodeInBug11395(&Inst)) return false;
3049 // Skip instructions inserted by another instrumentation.
3050 if (Inst.hasMetadata(LLVMContext::MD_nosanitize))
3051 continue;
3052 SmallVector<InterestingMemoryOperand, 1> InterestingOperands;
3053 getInterestingMemoryOperands(&Inst, InterestingOperands, TTI);
3054
3055 if (!InterestingOperands.empty()) {
3056 for (auto &Operand : InterestingOperands) {
3057 if (ClOpt && ClOptSameTemp) {
3058 Value *Ptr = Operand.getPtr();
3059 // If we have a mask, skip instrumentation if we've already
3060 // instrumented the full object. But don't add to TempsToInstrument
3061 // because we might get another load/store with a different mask.
3062 if (Operand.MaybeMask) {
3063 if (TempsToInstrument.count(Ptr))
3064 continue; // We've seen this (whole) temp in the current BB.
3065 } else {
3066 if (!TempsToInstrument.insert(Ptr).second)
3067 continue; // We've seen this temp in the current BB.
3068 }
3069 }
3070 OperandsToInstrument.push_back(Operand);
3071 NumInsnsPerBB++;
3072 }
3073 } else if (((ClInvalidPointerPairs || ClInvalidPointerCmp) &&
3077 PointerComparisonsOrSubtracts.push_back(&Inst);
3078 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&Inst)) {
3079 // ok, take it.
3080 IntrinToInstrument.push_back(MI);
3081 NumInsnsPerBB++;
3082 } else {
3083 if (auto *CB = dyn_cast<CallBase>(&Inst)) {
3084 // A call inside BB.
3085 TempsToInstrument.clear();
3086 if (CB->doesNotReturn())
3087 NoReturnCalls.push_back(CB);
3088 }
3089 if (CallInst *CI = dyn_cast<CallInst>(&Inst))
3091 }
3092 if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB) break;
3093 }
3094 }
3095
3096 bool UseCalls = (InstrumentationWithCallsThreshold >= 0 &&
3097 OperandsToInstrument.size() + IntrinToInstrument.size() >
3098 (unsigned)InstrumentationWithCallsThreshold);
3099 const DataLayout &DL = F.getDataLayout();
3100 ObjectSizeOffsetVisitor ObjSizeVis(DL, TLI, F.getContext());
3101
3102 // Instrument.
3103 int NumInstrumented = 0;
3104 for (auto &Operand : OperandsToInstrument) {
3105 if (!suppressInstrumentationSiteForDebug(NumInstrumented))
3106 instrumentMop(ObjSizeVis, Operand, UseCalls,
3107 F.getDataLayout(), RTCI);
3108 FunctionModified = true;
3109 }
3110 for (auto *Inst : IntrinToInstrument) {
3111 if (!suppressInstrumentationSiteForDebug(NumInstrumented))
3112 instrumentMemIntrinsic(Inst, RTCI);
3113 FunctionModified = true;
3114 }
3115
3116 FunctionStackPoisoner FSP(F, *this, RTCI);
3117 bool ChangedStack = FSP.runOnFunction();
3118
3119 // We must unpoison the stack before NoReturn calls (throw, _exit, etc).
3120 // See e.g. https://github.com/google/sanitizers/issues/37
3121 for (auto *CI : NoReturnCalls) {
3122 IRBuilder<> IRB(CI);
3123 RTCI.createRuntimeCall(IRB, AsanHandleNoReturnFunc, {});
3124 }
3125
3126 for (auto *Inst : PointerComparisonsOrSubtracts) {
3127 instrumentPointerComparisonOrSubtraction(Inst, RTCI);
3128 FunctionModified = true;
3129 }
3130
3131 if (ChangedStack || !NoReturnCalls.empty())
3132 FunctionModified = true;
3133
3134 LLVM_DEBUG(dbgs() << "ASAN done instrumenting: " << FunctionModified << " "
3135 << F << "\n");
3136
3137 return FunctionModified;
3138}
3139
3140// Workaround for bug 11395: we don't want to instrument stack in functions
3141// with large assembly blobs (32-bit only), otherwise reg alloc may crash.
3142// FIXME: remove once the bug 11395 is fixed.
3143bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
3144 if (LongSize != 32) return false;
3146 if (!CI || !CI->isInlineAsm()) return false;
3147 if (CI->arg_size() <= 5)
3148 return false;
3149 // We have inline assembly with quite a few arguments.
3150 return true;
3151}
3152
3153void FunctionStackPoisoner::initializeCallbacks(Module &M) {
3154 IRBuilder<> IRB(*C);
3155 if (ASan.UseAfterReturn == AsanDetectStackUseAfterReturnMode::Always ||
3156 ASan.UseAfterReturn == AsanDetectStackUseAfterReturnMode::Runtime) {
3157 const char *MallocNameTemplate =
3158 ASan.UseAfterReturn == AsanDetectStackUseAfterReturnMode::Always
3161 for (int Index = 0; Index <= kMaxAsanStackMallocSizeClass; Index++) {
3162 std::string Suffix = itostr(Index);
3163 AsanStackMallocFunc[Index] = M.getOrInsertFunction(
3164 MallocNameTemplate + Suffix, IntptrTy, IntptrTy);
3165 AsanStackFreeFunc[Index] =
3166 M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix,
3167 IRB.getVoidTy(), IntptrTy, IntptrTy);
3168 }
3169 }
3170 if (ASan.UseAfterScope) {
3171 AsanPoisonStackMemoryFunc = M.getOrInsertFunction(
3172 kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy);
3173 AsanUnpoisonStackMemoryFunc = M.getOrInsertFunction(
3174 kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy);
3175 }
3176
3177 for (size_t Val : {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xf1, 0xf2,
3178 0xf3, 0xf5, 0xf8}) {
3179 std::ostringstream Name;
3181 Name << std::setw(2) << std::setfill('0') << std::hex << Val;
3182 AsanSetShadowFunc[Val] =
3183 M.getOrInsertFunction(Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy);
3184 }
3185
3186 AsanAllocaPoisonFunc = M.getOrInsertFunction(
3187 kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy);
3188 AsanAllocasUnpoisonFunc = M.getOrInsertFunction(
3189 kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy);
3190}
3191
3192void FunctionStackPoisoner::copyToShadowInline(ArrayRef<uint8_t> ShadowMask,
3193 ArrayRef<uint8_t> ShadowBytes,
3194 size_t Begin, size_t End,
3195 IRBuilder<> &IRB,
3196 Value *ShadowBase) {
3197 if (Begin >= End)
3198 return;
3199
3200 const size_t LargestStoreSizeInBytes =
3201 std::min<size_t>(sizeof(uint64_t), ASan.LongSize / 8);
3202
3203 const bool IsLittleEndian = F.getDataLayout().isLittleEndian();
3204
3205 // Poison given range in shadow using larges store size with out leading and
3206 // trailing zeros in ShadowMask. Zeros never change, so they need neither
3207 // poisoning nor up-poisoning. Still we don't mind if some of them get into a
3208 // middle of a store.
3209 for (size_t i = Begin; i < End;) {
3210 if (!ShadowMask[i]) {
3211 assert(!ShadowBytes[i]);
3212 ++i;
3213 continue;
3214 }
3215
3216 size_t StoreSizeInBytes = LargestStoreSizeInBytes;
3217 // Fit store size into the range.
3218 while (StoreSizeInBytes > End - i)
3219 StoreSizeInBytes /= 2;
3220
3221 // Minimize store size by trimming trailing zeros.
3222 for (size_t j = StoreSizeInBytes - 1; j && !ShadowMask[i + j]; --j) {
3223 while (j <= StoreSizeInBytes / 2)
3224 StoreSizeInBytes /= 2;
3225 }
3226
3227 uint64_t Val = 0;
3228 for (size_t j = 0; j < StoreSizeInBytes; j++) {
3229 if (IsLittleEndian)
3230 Val |= (uint64_t)ShadowBytes[i + j] << (8 * j);
3231 else
3232 Val = (Val << 8) | ShadowBytes[i + j];
3233 }
3234
3235 Value *Ptr = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
3236 Value *Poison = IRB.getIntN(StoreSizeInBytes * 8, Val);
3239 Align(1));
3240
3241 i += StoreSizeInBytes;
3242 }
3243}
3244
3245void FunctionStackPoisoner::copyToShadow(ArrayRef<uint8_t> ShadowMask,
3246 ArrayRef<uint8_t> ShadowBytes,
3247 IRBuilder<> &IRB, Value *ShadowBase) {
3248 copyToShadow(ShadowMask, ShadowBytes, 0, ShadowMask.size(), IRB, ShadowBase);
3249}
3250
3251void FunctionStackPoisoner::copyToShadow(ArrayRef<uint8_t> ShadowMask,
3252 ArrayRef<uint8_t> ShadowBytes,
3253 size_t Begin, size_t End,
3254 IRBuilder<> &IRB, Value *ShadowBase) {
3255 assert(ShadowMask.size() == ShadowBytes.size());
3256 size_t Done = Begin;
3257 for (size_t i = Begin, j = Begin + 1; i < End; i = j++) {
3258 if (!ShadowMask[i]) {
3259 assert(!ShadowBytes[i]);
3260 continue;
3261 }
3262 uint8_t Val = ShadowBytes[i];
3263 if (!AsanSetShadowFunc[Val])
3264 continue;
3265
3266 // Skip same values.
3267 for (; j < End && ShadowMask[j] && Val == ShadowBytes[j]; ++j) {
3268 }
3269
3270 if (j - i >= ASan.MaxInlinePoisoningSize) {
3271 copyToShadowInline(ShadowMask, ShadowBytes, Done, i, IRB, ShadowBase);
3272 RTCI.createRuntimeCall(
3273 IRB, AsanSetShadowFunc[Val],
3274 {IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i)),
3275 ConstantInt::get(IntptrTy, j - i)});
3276 Done = j;
3277 }
3278 }
3279
3280 copyToShadowInline(ShadowMask, ShadowBytes, Done, End, IRB, ShadowBase);
3281}
3282
3283// Fake stack allocator (asan_fake_stack.h) has 11 size classes
3284// for every power of 2 from kMinStackMallocSize to kMaxAsanStackMallocSizeClass
3285static int StackMallocSizeClass(uint64_t LocalStackSize) {
3286 assert(LocalStackSize <= kMaxStackMallocSize);
3287 uint64_t MaxSize = kMinStackMallocSize;
3288 for (int i = 0;; i++, MaxSize *= 2)
3289 if (LocalStackSize <= MaxSize) return i;
3290 llvm_unreachable("impossible LocalStackSize");
3291}
3292
3293void FunctionStackPoisoner::copyArgsPassedByValToAllocas() {
3294 Instruction *CopyInsertPoint = &F.front().front();
3295 if (CopyInsertPoint == ASan.LocalDynamicShadow) {
3296 // Insert after the dynamic shadow location is determined
3297 CopyInsertPoint = CopyInsertPoint->getNextNode();
3298 assert(CopyInsertPoint);
3299 }
3300 IRBuilder<> IRB(CopyInsertPoint);
3301 const DataLayout &DL = F.getDataLayout();
3302 for (Argument &Arg : F.args()) {
3303 if (Arg.hasByValAttr()) {
3304 Type *Ty = Arg.getParamByValType();
3305 const Align Alignment =
3306 DL.getValueOrABITypeAlignment(Arg.getParamAlign(), Ty);
3307
3308 AllocaInst *AI = IRB.CreateAlloca(
3309 Ty, nullptr,
3310 (Arg.hasName() ? Arg.getName() : "Arg" + Twine(Arg.getArgNo())) +
3311 ".byval");
3312 AI->setAlignment(Alignment);
3313 Arg.replaceAllUsesWith(AI);
3314
3315 uint64_t AllocSize = DL.getTypeAllocSize(Ty);
3316 IRB.CreateMemCpy(AI, Alignment, &Arg, Alignment, AllocSize);
3317 }
3318 }
3319}
3320
3321PHINode *FunctionStackPoisoner::createPHI(IRBuilder<> &IRB, Value *Cond,
3322 Value *ValueIfTrue,
3323 Instruction *ThenTerm,
3324 Value *ValueIfFalse) {
3325 PHINode *PHI = IRB.CreatePHI(IntptrTy, 2);
3326 BasicBlock *CondBlock = cast<Instruction>(Cond)->getParent();
3327 PHI->addIncoming(ValueIfFalse, CondBlock);
3328 BasicBlock *ThenBlock = ThenTerm->getParent();
3329 PHI->addIncoming(ValueIfTrue, ThenBlock);
3330 return PHI;
3331}
3332
3333Value *FunctionStackPoisoner::createAllocaForLayout(
3334 IRBuilder<> &IRB, const ASanStackFrameLayout &L, bool Dynamic) {
3335 AllocaInst *Alloca;
3336 if (Dynamic) {
3337 Alloca = IRB.CreateAlloca(IRB.getInt8Ty(),
3338 ConstantInt::get(IRB.getInt64Ty(), L.FrameSize),
3339 "MyAlloca");
3340 } else {
3341 Alloca = IRB.CreateAlloca(ArrayType::get(IRB.getInt8Ty(), L.FrameSize),
3342 nullptr, "MyAlloca");
3343 assert(Alloca->isStaticAlloca());
3344 }
3345 assert((ClRealignStack & (ClRealignStack - 1)) == 0);
3346 uint64_t FrameAlignment = std::max(L.FrameAlignment, uint64_t(ClRealignStack));
3347 Alloca->setAlignment(Align(FrameAlignment));
3348 return IRB.CreatePointerCast(Alloca, IntptrTy);
3349}
3350
3351void FunctionStackPoisoner::createDynamicAllocasInitStorage() {
3352 BasicBlock &FirstBB = *F.begin();
3353 IRBuilder<> IRB(dyn_cast<Instruction>(FirstBB.begin()));
3354 DynamicAllocaLayout = IRB.CreateAlloca(IntptrTy, nullptr);
3355 IRB.CreateStore(Constant::getNullValue(IntptrTy), DynamicAllocaLayout);
3356 DynamicAllocaLayout->setAlignment(Align(32));
3357}
3358
3359void FunctionStackPoisoner::processDynamicAllocas() {
3360 if (!ClInstrumentDynamicAllocas || DynamicAllocaVec.empty()) {
3361 assert(DynamicAllocaPoisonCallVec.empty());
3362 return;
3363 }
3364
3365 // Insert poison calls for lifetime intrinsics for dynamic allocas.
3366 for (const auto &APC : DynamicAllocaPoisonCallVec) {
3367 assert(APC.InsBefore);
3368 assert(APC.AI);
3369 assert(ASan.isInterestingAlloca(*APC.AI));
3370 assert(!APC.AI->isStaticAlloca());
3371
3372 IRBuilder<> IRB(APC.InsBefore);
3373 poisonAlloca(APC.AI, APC.Size, IRB, APC.DoPoison);
3374 // Dynamic allocas will be unpoisoned unconditionally below in
3375 // unpoisonDynamicAllocas.
3376 // Flag that we need unpoison static allocas.
3377 }
3378
3379 // Handle dynamic allocas.
3380 createDynamicAllocasInitStorage();
3381 for (auto &AI : DynamicAllocaVec)
3382 handleDynamicAllocaCall(AI);
3383 unpoisonDynamicAllocas();
3384}
3385
3386/// Collect instructions in the entry block after \p InsBefore which initialize
3387/// permanent storage for a function argument. These instructions must remain in
3388/// the entry block so that uninitialized values do not appear in backtraces. An
3389/// added benefit is that this conserves spill slots. This does not move stores
3390/// before instrumented / "interesting" allocas.
3392 AddressSanitizer &ASan, Instruction &InsBefore,
3393 SmallVectorImpl<Instruction *> &InitInsts) {
3394 Instruction *Start = InsBefore.getNextNode();
3395 for (Instruction *It = Start; It; It = It->getNextNode()) {
3396 // Argument initialization looks like:
3397 // 1) store <Argument>, <Alloca> OR
3398 // 2) <CastArgument> = cast <Argument> to ...
3399 // store <CastArgument> to <Alloca>
3400 // Do not consider any other kind of instruction.
3401 //
3402 // Note: This covers all known cases, but may not be exhaustive. An
3403 // alternative to pattern-matching stores is to DFS over all Argument uses:
3404 // this might be more general, but is probably much more complicated.
3405 if (isa<AllocaInst>(It) || isa<CastInst>(It))
3406 continue;
3407 if (auto *Store = dyn_cast<StoreInst>(It)) {
3408 // The store destination must be an alloca that isn't interesting for
3409 // ASan to instrument. These are moved up before InsBefore, and they're
3410 // not interesting because allocas for arguments can be mem2reg'd.
3411 auto *Alloca = dyn_cast<AllocaInst>(Store->getPointerOperand());
3412 if (!Alloca || ASan.isInterestingAlloca(*Alloca))
3413 continue;
3414
3415 Value *Val = Store->getValueOperand();
3416 bool IsDirectArgInit = isa<Argument>(Val);
3417 bool IsArgInitViaCast =
3418 isa<CastInst>(Val) &&
3419 isa<Argument>(cast<CastInst>(Val)->getOperand(0)) &&
3420 // Check that the cast appears directly before the store. Otherwise
3421 // moving the cast before InsBefore may break the IR.
3422 Val == It->getPrevNode();
3423 bool IsArgInit = IsDirectArgInit || IsArgInitViaCast;
3424 if (!IsArgInit)
3425 continue;
3426
3427 if (IsArgInitViaCast)
3428 InitInsts.push_back(cast<Instruction>(Val));
3429 InitInsts.push_back(Store);
3430 continue;
3431 }
3432
3433 // Do not reorder past unknown instructions: argument initialization should
3434 // only involve casts and stores.
3435 return;
3436 }
3437}
3438
3440 // Alloca could have been renamed for uniqueness. Its true name will have been
3441 // recorded as an annotation.
3442 if (AI->hasMetadata(LLVMContext::MD_annotation)) {
3443 MDTuple *AllocaAnnotations =
3444 cast<MDTuple>(AI->getMetadata(LLVMContext::MD_annotation));
3445 for (auto &Annotation : AllocaAnnotations->operands()) {
3446 if (!isa<MDTuple>(Annotation))
3447 continue;
3448 auto AnnotationTuple = cast<MDTuple>(Annotation);
3449 for (unsigned Index = 0; Index < AnnotationTuple->getNumOperands();
3450 Index++) {
3451 // All annotations are strings
3452 auto MetadataString =
3453 cast<MDString>(AnnotationTuple->getOperand(Index));
3454 if (MetadataString->getString() == "alloca_name_altered")
3455 return cast<MDString>(AnnotationTuple->getOperand(Index + 1))
3456 ->getString();
3457 }
3458 }
3459 }
3460 return AI->getName();
3461}
3462
3463void FunctionStackPoisoner::processStaticAllocas() {
3464 if (AllocaVec.empty()) {
3465 assert(StaticAllocaPoisonCallVec.empty());
3466 return;
3467 }
3468
3469 int StackMallocIdx = -1;
3470 DebugLoc EntryDebugLocation;
3471 if (auto SP = F.getSubprogram())
3472 EntryDebugLocation =
3473 DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP);
3474
3475 Instruction *InsBefore = AllocaVec[0];
3476 IRBuilder<> IRB(InsBefore);
3477
3478 // Make sure non-instrumented allocas stay in the entry block. Otherwise,
3479 // debug info is broken, because only entry-block allocas are treated as
3480 // regular stack slots.
3481 auto InsBeforeB = InsBefore->getParent();
3482 assert(InsBeforeB == &F.getEntryBlock());
3483 for (auto *AI : StaticAllocasToMoveUp)
3484 if (AI->getParent() == InsBeforeB)
3485 AI->moveBefore(InsBefore->getIterator());
3486
3487 // Move stores of arguments into entry-block allocas as well. This prevents
3488 // extra stack slots from being generated (to house the argument values until
3489 // they can be stored into the allocas). This also prevents uninitialized
3490 // values from being shown in backtraces.
3491 SmallVector<Instruction *, 8> ArgInitInsts;
3492 findStoresToUninstrumentedArgAllocas(ASan, *InsBefore, ArgInitInsts);
3493 for (Instruction *ArgInitInst : ArgInitInsts)
3494 ArgInitInst->moveBefore(InsBefore->getIterator());
3495
3496 // If we have a call to llvm.localescape, keep it in the entry block.
3497 if (LocalEscapeCall)
3498 LocalEscapeCall->moveBefore(InsBefore->getIterator());
3499
3501 SVD.reserve(AllocaVec.size());
3502 for (AllocaInst *AI : AllocaVec) {
3505 ASan.getAllocaSizeInBytes(*AI),
3506 0,
3507 AI->getAlign().value(),
3508 AI,
3509 0,
3510 0};
3511 SVD.push_back(D);
3512 }
3513
3514 // Minimal header size (left redzone) is 4 pointers,
3515 // i.e. 32 bytes on 64-bit platforms and 16 bytes in 32-bit platforms.
3516 uint64_t Granularity = 1ULL << Mapping.Scale;
3517 uint64_t MinHeaderSize = std::max((uint64_t)ASan.LongSize / 2, Granularity);
3518 const ASanStackFrameLayout &L =
3519 ComputeASanStackFrameLayout(SVD, Granularity, MinHeaderSize);
3520
3521 // Build AllocaToSVDMap for ASanStackVariableDescription lookup.
3523 for (auto &Desc : SVD)
3524 AllocaToSVDMap[Desc.AI] = &Desc;
3525
3526 // Update SVD with information from lifetime intrinsics.
3527 for (const auto &APC : StaticAllocaPoisonCallVec) {
3528 assert(APC.InsBefore);
3529 assert(APC.AI);
3530 assert(ASan.isInterestingAlloca(*APC.AI));
3531 assert(APC.AI->isStaticAlloca());
3532
3533 ASanStackVariableDescription &Desc = *AllocaToSVDMap[APC.AI];
3534 Desc.LifetimeSize = Desc.Size;
3535 if (const DILocation *FnLoc = EntryDebugLocation.get()) {
3536 if (const DILocation *LifetimeLoc = APC.InsBefore->getDebugLoc().get()) {
3537 if (LifetimeLoc->getFile() == FnLoc->getFile())
3538 if (unsigned Line = LifetimeLoc->getLine())
3539 Desc.Line = std::min(Desc.Line ? Desc.Line : Line, Line);
3540 }
3541 }
3542 }
3543
3544 auto DescriptionString = ComputeASanStackFrameDescription(SVD);
3545 LLVM_DEBUG(dbgs() << DescriptionString << " --- " << L.FrameSize << "\n");
3546 uint64_t LocalStackSize = L.FrameSize;
3547 bool DoStackMalloc =
3548 ASan.UseAfterReturn != AsanDetectStackUseAfterReturnMode::Never &&
3549 !ASan.CompileKernel && LocalStackSize <= kMaxStackMallocSize;
3550 bool DoDynamicAlloca = ClDynamicAllocaStack;
3551 // Don't do dynamic alloca or stack malloc if:
3552 // 1) There is inline asm: too often it makes assumptions on which registers
3553 // are available.
3554 // 2) There is a returns_twice call (typically setjmp), which is
3555 // optimization-hostile, and doesn't play well with introduced indirect
3556 // register-relative calculation of local variable addresses.
3557 DoDynamicAlloca &= !HasInlineAsm && !HasReturnsTwiceCall;
3558 DoStackMalloc &= !HasInlineAsm && !HasReturnsTwiceCall;
3559
3560 Value *StaticAlloca =
3561 DoDynamicAlloca ? nullptr : createAllocaForLayout(IRB, L, false);
3562
3563 Value *FakeStack;
3564 Value *LocalStackBase;
3565 Value *LocalStackBaseAlloca;
3566 uint8_t DIExprFlags = DIExpression::ApplyOffset;
3567
3568 if (DoStackMalloc) {
3569 LocalStackBaseAlloca =
3570 IRB.CreateAlloca(IntptrTy, nullptr, "asan_local_stack_base");
3571 if (ASan.UseAfterReturn == AsanDetectStackUseAfterReturnMode::Runtime) {
3572 // void *FakeStack = __asan_option_detect_stack_use_after_return
3573 // ? __asan_stack_malloc_N(LocalStackSize)
3574 // : nullptr;
3575 // void *LocalStackBase = (FakeStack) ? FakeStack :
3576 // alloca(LocalStackSize);
3577 Constant *OptionDetectUseAfterReturn = F.getParent()->getOrInsertGlobal(
3579 Value *UseAfterReturnIsEnabled = IRB.CreateICmpNE(
3580 IRB.CreateLoad(IRB.getInt32Ty(), OptionDetectUseAfterReturn),
3582 Instruction *Term =
3583 SplitBlockAndInsertIfThen(UseAfterReturnIsEnabled, InsBefore, false);
3584 IRBuilder<> IRBIf(Term);
3585 StackMallocIdx = StackMallocSizeClass(LocalStackSize);
3586 assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass);
3587 Value *FakeStackValue =
3588 RTCI.createRuntimeCall(IRBIf, AsanStackMallocFunc[StackMallocIdx],
3589 ConstantInt::get(IntptrTy, LocalStackSize));
3590 IRB.SetInsertPoint(InsBefore);
3591 FakeStack = createPHI(IRB, UseAfterReturnIsEnabled, FakeStackValue, Term,
3592 ConstantInt::get(IntptrTy, 0));
3593 } else {
3594 // assert(ASan.UseAfterReturn == AsanDetectStackUseAfterReturnMode:Always)
3595 // void *FakeStack = __asan_stack_malloc_N(LocalStackSize);
3596 // void *LocalStackBase = (FakeStack) ? FakeStack :
3597 // alloca(LocalStackSize);
3598 StackMallocIdx = StackMallocSizeClass(LocalStackSize);
3599 FakeStack =
3600 RTCI.createRuntimeCall(IRB, AsanStackMallocFunc[StackMallocIdx],
3601 ConstantInt::get(IntptrTy, LocalStackSize));
3602 }
3603 Value *NoFakeStack =
3604 IRB.CreateICmpEQ(FakeStack, Constant::getNullValue(IntptrTy));
3605 Instruction *Term =
3606 SplitBlockAndInsertIfThen(NoFakeStack, InsBefore, false);
3607 IRBuilder<> IRBIf(Term);
3608 Value *AllocaValue =
3609 DoDynamicAlloca ? createAllocaForLayout(IRBIf, L, true) : StaticAlloca;
3610
3611 IRB.SetInsertPoint(InsBefore);
3612 LocalStackBase = createPHI(IRB, NoFakeStack, AllocaValue, Term, FakeStack);
3613 IRB.CreateStore(LocalStackBase, LocalStackBaseAlloca);
3614 DIExprFlags |= DIExpression::DerefBefore;
3615 } else {
3616 // void *FakeStack = nullptr;
3617 // void *LocalStackBase = alloca(LocalStackSize);
3618 FakeStack = ConstantInt::get(IntptrTy, 0);
3619 LocalStackBase =
3620 DoDynamicAlloca ? createAllocaForLayout(IRB, L, true) : StaticAlloca;
3621 LocalStackBaseAlloca = LocalStackBase;
3622 }
3623
3624 // It shouldn't matter whether we pass an `alloca` or a `ptrtoint` as the
3625 // dbg.declare address opereand, but passing a `ptrtoint` seems to confuse
3626 // later passes and can result in dropped variable coverage in debug info.
3627 Value *LocalStackBaseAllocaPtr =
3628 isa<PtrToIntInst>(LocalStackBaseAlloca)
3629 ? cast<PtrToIntInst>(LocalStackBaseAlloca)->getPointerOperand()
3630 : LocalStackBaseAlloca;
3631 assert(isa<AllocaInst>(LocalStackBaseAllocaPtr) &&
3632 "Variable descriptions relative to ASan stack base will be dropped");
3633
3634 // Replace Alloca instructions with base+offset.
3635 SmallVector<Value *> NewAllocaPtrs;
3636 for (const auto &Desc : SVD) {
3637 AllocaInst *AI = Desc.AI;
3638 replaceDbgDeclare(AI, LocalStackBaseAllocaPtr, DIB, DIExprFlags,
3639 Desc.Offset);
3640 Value *NewAllocaPtr = IRB.CreateIntToPtr(
3641 IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Desc.Offset)),
3642 AI->getType());
3643 AI->replaceAllUsesWith(NewAllocaPtr);
3644 NewAllocaPtrs.push_back(NewAllocaPtr);
3645 }
3646
3647 // The left-most redzone has enough space for at least 4 pointers.
3648 // Write the Magic value to redzone[0].
3649 Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
3650 IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
3651 BasePlus0);
3652 // Write the frame description constant to redzone[1].
3653 Value *BasePlus1 = IRB.CreateIntToPtr(
3654 IRB.CreateAdd(LocalStackBase,
3655 ConstantInt::get(IntptrTy, ASan.LongSize / 8)),
3656 IntptrPtrTy);
3657 GlobalVariable *StackDescriptionGlobal =
3658 createPrivateGlobalForString(*F.getParent(), DescriptionString,
3659 /*AllowMerging*/ true, genName("stack"));
3660 Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal, IntptrTy);
3661 IRB.CreateStore(Description, BasePlus1);
3662 // Write the PC to redzone[2].
3663 Value *BasePlus2 = IRB.CreateIntToPtr(
3664 IRB.CreateAdd(LocalStackBase,
3665 ConstantInt::get(IntptrTy, 2 * ASan.LongSize / 8)),
3666 IntptrPtrTy);
3667 IRB.CreateStore(IRB.CreatePointerCast(&F, IntptrTy), BasePlus2);
3668
3669 const auto &ShadowAfterScope = GetShadowBytesAfterScope(SVD, L);
3670
3671 // Poison the stack red zones at the entry.
3672 Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB);
3673 // As mask we must use most poisoned case: red zones and after scope.
3674 // As bytes we can use either the same or just red zones only.
3675 copyToShadow(ShadowAfterScope, ShadowAfterScope, IRB, ShadowBase);
3676
3677 if (!StaticAllocaPoisonCallVec.empty()) {
3678 const auto &ShadowInScope = GetShadowBytes(SVD, L);
3679
3680 // Poison static allocas near lifetime intrinsics.
3681 for (const auto &APC : StaticAllocaPoisonCallVec) {
3682 const ASanStackVariableDescription &Desc = *AllocaToSVDMap[APC.AI];
3683 assert(Desc.Offset % L.Granularity == 0);
3684 size_t Begin = Desc.Offset / L.Granularity;
3685 size_t End = Begin + (APC.Size + L.Granularity - 1) / L.Granularity;
3686
3687 IRBuilder<> IRB(APC.InsBefore);
3688 copyToShadow(ShadowAfterScope,
3689 APC.DoPoison ? ShadowAfterScope : ShadowInScope, Begin, End,
3690 IRB, ShadowBase);
3691 }
3692 }
3693
3694 // Remove lifetime markers now that these are no longer allocas.
3695 for (Value *NewAllocaPtr : NewAllocaPtrs) {
3696 for (User *U : make_early_inc_range(NewAllocaPtr->users())) {
3697 auto *I = cast<Instruction>(U);
3698 if (I->isLifetimeStartOrEnd())
3699 I->eraseFromParent();
3700 }
3701 }
3702
3703 SmallVector<uint8_t, 64> ShadowClean(ShadowAfterScope.size(), 0);
3704 SmallVector<uint8_t, 64> ShadowAfterReturn;
3705
3706 // (Un)poison the stack before all ret instructions.
3707 for (Instruction *Ret : RetVec) {
3708 IRBuilder<> IRBRet(Ret);
3709 // Mark the current frame as retired.
3710 IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
3711 BasePlus0);
3712 if (DoStackMalloc) {
3713 assert(StackMallocIdx >= 0);
3714 // if FakeStack != 0 // LocalStackBase == FakeStack
3715 // // In use-after-return mode, poison the whole stack frame.
3716 // if StackMallocIdx <= 4
3717 // // For small sizes inline the whole thing:
3718 // memset(ShadowBase, kAsanStackAfterReturnMagic, ShadowSize);
3719 // **SavedFlagPtr(FakeStack) = 0
3720 // else
3721 // __asan_stack_free_N(FakeStack, LocalStackSize)
3722 // else
3723 // <This is not a fake stack; unpoison the redzones>
3724 Value *Cmp =
3725 IRBRet.CreateICmpNE(FakeStack, Constant::getNullValue(IntptrTy));
3726 Instruction *ThenTerm, *ElseTerm;
3727 SplitBlockAndInsertIfThenElse(Cmp, Ret, &ThenTerm, &ElseTerm);
3728
3729 IRBuilder<> IRBPoison(ThenTerm);
3730 if (ASan.MaxInlinePoisoningSize != 0 && StackMallocIdx <= 4) {
3731 int ClassSize = kMinStackMallocSize << StackMallocIdx;
3732 ShadowAfterReturn.resize(ClassSize / L.Granularity,
3734 copyToShadow(ShadowAfterReturn, ShadowAfterReturn, IRBPoison,
3735 ShadowBase);
3736 Value *SavedFlagPtrPtr = IRBPoison.CreateAdd(
3737 FakeStack,
3738 ConstantInt::get(IntptrTy, ClassSize - ASan.LongSize / 8));
3739 Value *SavedFlagPtr = IRBPoison.CreateLoad(
3740 IntptrTy, IRBPoison.CreateIntToPtr(SavedFlagPtrPtr, IntptrPtrTy));
3741 IRBPoison.CreateStore(
3742 Constant::getNullValue(IRBPoison.getInt8Ty()),
3743 IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getPtrTy()));
3744 } else {
3745 // For larger frames call __asan_stack_free_*.
3746 RTCI.createRuntimeCall(
3747 IRBPoison, AsanStackFreeFunc[StackMallocIdx],
3748 {FakeStack, ConstantInt::get(IntptrTy, LocalStackSize)});
3749 }
3750
3751 IRBuilder<> IRBElse(ElseTerm);
3752 copyToShadow(ShadowAfterScope, ShadowClean, IRBElse, ShadowBase);
3753 } else {
3754 copyToShadow(ShadowAfterScope, ShadowClean, IRBRet, ShadowBase);
3755 }
3756 }
3757
3758 // We are done. Remove the old unused alloca instructions.
3759 for (auto *AI : AllocaVec)
3760 AI->eraseFromParent();
3761}
3762
3763void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size,
3764 IRBuilder<> &IRB, bool DoPoison) {
3765 // For now just insert the call to ASan runtime.
3766 Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy);
3767 Value *SizeArg = ConstantInt::get(IntptrTy, Size);
3768 RTCI.createRuntimeCall(
3769 IRB, DoPoison ? AsanPoisonStackMemoryFunc : AsanUnpoisonStackMemoryFunc,
3770 {AddrArg, SizeArg});
3771}
3772
3773// Handling llvm.lifetime intrinsics for a given %alloca:
3774// (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca.
3775// (2) if %size is constant, poison memory for llvm.lifetime.end (to detect
3776// invalid accesses) and unpoison it for llvm.lifetime.start (the memory
3777// could be poisoned by previous llvm.lifetime.end instruction, as the
3778// variable may go in and out of scope several times, e.g. in loops).
3779// (3) if we poisoned at least one %alloca in a function,
3780// unpoison the whole stack frame at function exit.
3781void FunctionStackPoisoner::handleDynamicAllocaCall(AllocaInst *AI) {
3782 IRBuilder<> IRB(AI);
3783
3784 const Align Alignment = std::max(Align(kAllocaRzSize), AI->getAlign());
3785 const uint64_t AllocaRedzoneMask = kAllocaRzSize - 1;
3786
3787 Value *Zero = Constant::getNullValue(IntptrTy);
3788 Value *AllocaRzSize = ConstantInt::get(IntptrTy, kAllocaRzSize);
3789 Value *AllocaRzMask = ConstantInt::get(IntptrTy, AllocaRedzoneMask);
3790
3791 // Since we need to extend alloca with additional memory to locate
3792 // redzones, and OldSize is number of allocated blocks with
3793 // ElementSize size, get allocated memory size in bytes by
3794 // OldSize * ElementSize.
3795 const unsigned ElementSize =
3796 F.getDataLayout().getTypeAllocSize(AI->getAllocatedType());
3797 Value *OldSize =
3798 IRB.CreateMul(IRB.CreateIntCast(AI->getArraySize(), IntptrTy, false),
3799 ConstantInt::get(IntptrTy, ElementSize));
3800
3801 // PartialSize = OldSize % 32
3802 Value *PartialSize = IRB.CreateAnd(OldSize, AllocaRzMask);
3803
3804 // Misalign = kAllocaRzSize - PartialSize;
3805 Value *Misalign = IRB.CreateSub(AllocaRzSize, PartialSize);
3806
3807 // PartialPadding = Misalign != kAllocaRzSize ? Misalign : 0;
3808 Value *Cond = IRB.CreateICmpNE(Misalign, AllocaRzSize);
3809 Value *PartialPadding = IRB.CreateSelect(Cond, Misalign, Zero);
3810
3811 // AdditionalChunkSize = Alignment + PartialPadding + kAllocaRzSize
3812 // Alignment is added to locate left redzone, PartialPadding for possible
3813 // partial redzone and kAllocaRzSize for right redzone respectively.
3814 Value *AdditionalChunkSize = IRB.CreateAdd(
3815 ConstantInt::get(IntptrTy, Alignment.value() + kAllocaRzSize),
3816 PartialPadding);
3817
3818 Value *NewSize = IRB.CreateAdd(OldSize, AdditionalChunkSize);
3819
3820 // Insert new alloca with new NewSize and Alignment params.
3821 AllocaInst *NewAlloca = IRB.CreateAlloca(IRB.getInt8Ty(), NewSize);
3822 NewAlloca->setAlignment(Alignment);
3823
3824 // NewAddress = Address + Alignment
3825 Value *NewAddress =
3826 IRB.CreateAdd(IRB.CreatePtrToInt(NewAlloca, IntptrTy),
3827 ConstantInt::get(IntptrTy, Alignment.value()));
3828
3829 // Insert __asan_alloca_poison call for new created alloca.
3830 RTCI.createRuntimeCall(IRB, AsanAllocaPoisonFunc, {NewAddress, OldSize});
3831
3832 // Store the last alloca's address to DynamicAllocaLayout. We'll need this
3833 // for unpoisoning stuff.
3834 IRB.CreateStore(IRB.CreatePtrToInt(NewAlloca, IntptrTy), DynamicAllocaLayout);
3835
3836 Value *NewAddressPtr = IRB.CreateIntToPtr(NewAddress, AI->getType());
3837
3838 // Remove lifetime markers now that this is no longer an alloca.
3839 for (User *U : make_early_inc_range(AI->users())) {
3840 auto *I = cast<Instruction>(U);
3841 if (I->isLifetimeStartOrEnd())
3842 I->eraseFromParent();
3843 }
3844
3845 // Replace all uses of AddessReturnedByAlloca with NewAddressPtr.
3846 AI->replaceAllUsesWith(NewAddressPtr);
3847
3848 // We are done. Erase old alloca from parent.
3849 AI->eraseFromParent();
3850}
3851
3852// isSafeAccess returns true if Addr is always inbounds with respect to its
3853// base object. For example, it is a field access or an array access with
3854// constant inbounds index.
3855bool AddressSanitizer::isSafeAccess(ObjectSizeOffsetVisitor &ObjSizeVis,
3856 Value *Addr, TypeSize TypeStoreSize) const {
3857 if (TypeStoreSize.isScalable())
3858 // TODO: We can use vscale_range to convert a scalable value to an
3859 // upper bound on the access size.
3860 return false;
3861
3862 SizeOffsetAPInt SizeOffset = ObjSizeVis.compute(Addr);
3863 if (!SizeOffset.bothKnown())
3864 return false;
3865
3866 uint64_t Size = SizeOffset.Size.getZExtValue();
3867 int64_t Offset = SizeOffset.Offset.getSExtValue();
3868
3869 // Three checks are required to ensure safety:
3870 // . Offset >= 0 (since the offset is given from the base ptr)
3871 // . Size >= Offset (unsigned)
3872 // . Size - Offset >= NeededSize (unsigned)
3873 return Offset >= 0 && Size >= uint64_t(Offset) &&
3874 Size - uint64_t(Offset) >= TypeStoreSize / 8;
3875}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< bool > ClUseStackSafety("stack-tagging-use-stack-safety", cl::Hidden, cl::init(true), cl::desc("Use Stack Safety analysis results"))
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void findStoresToUninstrumentedArgAllocas(AddressSanitizer &ASan, Instruction &InsBefore, SmallVectorImpl< Instruction * > &InitInsts)
Collect instructions in the entry block after InsBefore which initialize permanent storage for a func...
static void doInstrumentAddress(AddressSanitizer *Pass, Instruction *I, Instruction *InsertBefore, Value *Addr, MaybeAlign Alignment, unsigned Granularity, TypeSize TypeStoreSize, bool IsWrite, Value *SizeArgument, bool UseCalls, uint32_t Exp, RuntimeCallInserter &RTCI)
static const uint64_t kDefaultShadowScale
const char kAMDGPUUnreachableName[]
constexpr size_t kAccessSizeIndexMask
static cl::opt< int > ClDebugMin("asan-debug-min", cl::desc("Debug min inst"), cl::Hidden, cl::init(-1))
static cl::opt< bool > ClUsePrivateAlias("asan-use-private-alias", cl::desc("Use private aliases for global variables"), cl::Hidden, cl::init(true))
static const uint64_t kPS_ShadowOffset64
static const uint64_t kFreeBSD_ShadowOffset32
constexpr size_t kIsWriteShift
static const uint64_t kSmallX86_64ShadowOffsetAlignMask
static bool isInterestingPointerSubtraction(Instruction *I)
const char kAMDGPUAddressSharedName[]
const char kAsanStackFreeNameTemplate[]
constexpr size_t kCompileKernelMask
static cl::opt< bool > ClForceDynamicShadow("asan-force-dynamic-shadow", cl::desc("Load shadow address into a local variable for each function"), cl::Hidden, cl::init(false))
const char kAsanOptionDetectUseAfterReturn[]
static cl::opt< std::string > ClMemoryAccessCallbackPrefix("asan-memory-access-callback-prefix", cl::desc("Prefix for memory access callbacks"), cl::Hidden, cl::init("__asan_"))
static const uint64_t kRISCV64_ShadowOffset64
static cl::opt< bool > ClInsertVersionCheck("asan-guard-against-version-mismatch", cl::desc("Guard against compiler/runtime version mismatch."), cl::Hidden, cl::init(true))
const char kAsanSetShadowPrefix[]
static cl::opt< AsanDtorKind > ClOverrideDestructorKind("asan-destructor-kind", cl::desc("Sets the ASan destructor kind. The default is to use the value " "provided to the pass constructor"), cl::values(clEnumValN(AsanDtorKind::None, "none", "No destructors"), clEnumValN(AsanDtorKind::Global, "global", "Use global destructors")), cl::init(AsanDtorKind::Invalid), cl::Hidden)
static Twine genName(StringRef suffix)
static cl::opt< bool > ClInstrumentWrites("asan-instrument-writes", cl::desc("instrument write instructions"), cl::Hidden, cl::init(true))
const char kAsanPtrCmp[]
static uint64_t GetCtorAndDtorPriority(Triple &TargetTriple)
const char kAsanStackMallocNameTemplate[]
static cl::opt< bool > ClInstrumentByval("asan-instrument-byval", cl::desc("instrument byval call arguments"), cl::Hidden, cl::init(true))
const char kAsanInitName[]
static cl::opt< bool > ClGlobals("asan-globals", cl::desc("Handle global objects"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClRedzoneByvalArgs("asan-redzone-byval-args", cl::desc("Create redzones for byval " "arguments (extra copy " "required)"), cl::Hidden, cl::init(true))
static const uint64_t kWindowsShadowOffset64
const char kAsanGenPrefix[]
constexpr size_t kIsWriteMask
static uint64_t getRedzoneSizeForScale(int MappingScale)
static const uint64_t kDefaultShadowOffset64
static cl::opt< bool > ClOptimizeCallbacks("asan-optimize-callbacks", cl::desc("Optimize callbacks"), cl::Hidden, cl::init(false))
const char kAsanUnregisterGlobalsName[]
static const uint64_t kAsanCtorAndDtorPriority
const char kAsanUnpoisonGlobalsName[]
static cl::opt< bool > ClWithIfuncSuppressRemat("asan-with-ifunc-suppress-remat", cl::desc("Suppress rematerialization of dynamic shadow address by passing " "it through inline asm in prologue."), cl::Hidden, cl::init(true))
static cl::opt< int > ClDebugStack("asan-debug-stack", cl::desc("debug stack"), cl::Hidden, cl::init(0))
const char kAsanUnregisterElfGlobalsName[]
static bool isUnsupportedAMDGPUAddrspace(Value *Addr)
const char kAsanRegisterImageGlobalsName[]
static const uint64_t kWebAssemblyShadowOffset
static cl::opt< bool > ClOpt("asan-opt", cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true))
static const uint64_t kAllocaRzSize
const char kODRGenPrefix[]
static const uint64_t kSystemZ_ShadowOffset64
static const uint64_t kDefaultShadowOffset32
const char kAsanShadowMemoryDynamicAddress[]
static cl::opt< bool > ClUseOdrIndicator("asan-use-odr-indicator", cl::desc("Use odr indicators to improve ODR reporting"), cl::Hidden, cl::init(true))
static bool GlobalWasGeneratedByCompiler(GlobalVariable *G)
Check if G has been created by a trusted compiler pass.
const char kAsanStackMallocAlwaysNameTemplate[]
static cl::opt< bool > ClInvalidPointerCmp("asan-detect-invalid-pointer-cmp", cl::desc("Instrument <, <=, >, >= with pointer operands"), cl::Hidden, cl::init(false))
static const uint64_t kAsanEmscriptenCtorAndDtorPriority
static cl::opt< int > ClInstrumentationWithCallsThreshold("asan-instrumentation-with-call-threshold", cl::desc("If the function being instrumented contains more than " "this number of memory accesses, use callbacks instead of " "inline checks (-1 means never use callbacks)."), cl::Hidden, cl::init(7000))
static cl::opt< int > ClDebugMax("asan-debug-max", cl::desc("Debug max inst"), cl::Hidden, cl::init(-1))
static cl::opt< bool > ClInvalidPointerSub("asan-detect-invalid-pointer-sub", cl::desc("Instrument - operations with pointer operands"), cl::Hidden, cl::init(false))
static const uint64_t kFreeBSD_ShadowOffset64
static cl::opt< uint32_t > ClForceExperiment("asan-force-experiment", cl::desc("Force optimization experiment (for testing)"), cl::Hidden, cl::init(0))
const char kSanCovGenPrefix[]
static const uint64_t kFreeBSDKasan_ShadowOffset64
const char kAsanModuleDtorName[]
static const uint64_t kDynamicShadowSentinel
static bool isInterestingPointerComparison(Instruction *I)
static cl::opt< bool > ClStack("asan-stack", cl::desc("Handle stack memory"), cl::Hidden, cl::init(true))
static const uint64_t kMIPS64_ShadowOffset64
static const uint64_t kLinuxKasan_ShadowOffset64
static int StackMallocSizeClass(uint64_t LocalStackSize)
static cl::opt< uint32_t > ClMaxInlinePoisoningSize("asan-max-inline-poisoning-size", cl::desc("Inline shadow poisoning for blocks up to the given size in bytes."), cl::Hidden, cl::init(64))
static cl::opt< bool > ClInstrumentAtomics("asan-instrument-atomics", cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClUseAfterScope("asan-use-after-scope", cl::desc("Check stack-use-after-scope"), cl::Hidden, cl::init(false))
constexpr size_t kAccessSizeIndexShift
static cl::opt< int > ClMappingScale("asan-mapping-scale", cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0))
const char kAsanPoisonStackMemoryName[]
static cl::opt< bool > ClEnableKasan("asan-kernel", cl::desc("Enable KernelAddressSanitizer instrumentation"), cl::Hidden, cl::init(false))
static cl::opt< std::string > ClDebugFunc("asan-debug-func", cl::Hidden, cl::desc("Debug func"))
static cl::opt< bool > ClUseGlobalsGC("asan-globals-live-support", cl::desc("Use linker features to support dead " "code stripping of globals"), cl::Hidden, cl::init(true))
static const size_t kNumberOfAccessSizes
const char kAsanUnpoisonStackMemoryName[]
static const uint64_t kLoongArch64_ShadowOffset64
const char kAsanRegisterGlobalsName[]
static cl::opt< bool > ClInstrumentDynamicAllocas("asan-instrument-dynamic-allocas", cl::desc("instrument dynamic allocas"), cl::Hidden, cl::init(true))
const char kAsanModuleCtorName[]
const char kAsanGlobalsRegisteredFlagName[]
static const size_t kMaxStackMallocSize
static cl::opt< bool > ClRecover("asan-recover", cl::desc("Enable recovery mode (continue-after-error)."), cl::Hidden, cl::init(false))
static cl::opt< bool > ClOptSameTemp("asan-opt-same-temp", cl::desc("Instrument the same temp just once"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClDynamicAllocaStack("asan-stack-dynamic-alloca", cl::desc("Use dynamic alloca to represent stack variables"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClOptStack("asan-opt-stack", cl::desc("Don't instrument scalar stack variables"), cl::Hidden, cl::init(false))
static const uint64_t kMIPS_ShadowOffsetN32
const char kAsanUnregisterImageGlobalsName[]
static cl::opt< AsanDetectStackUseAfterReturnMode > ClUseAfterReturn("asan-use-after-return", cl::desc("Sets the mode of detection for stack-use-after-return."), cl::values(clEnumValN(AsanDetectStackUseAfterReturnMode::Never, "never", "Never detect stack use after return."), clEnumValN(AsanDetectStackUseAfterReturnMode::Runtime, "runtime", "Detect stack use after return if " "binary flag 'ASAN_OPTIONS=detect_stack_use_after_return' is set."), clEnumValN(AsanDetectStackUseAfterReturnMode::Always, "always", "Always detect stack use after return.")), cl::Hidden, cl::init(AsanDetectStackUseAfterReturnMode::Runtime))
static cl::opt< bool > ClOptGlobals("asan-opt-globals", cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true))
static const uintptr_t kCurrentStackFrameMagic
static ShadowMapping getShadowMapping(const Triple &TargetTriple, int LongSize, bool IsKasan)
static const uint64_t kPPC64_ShadowOffset64
static cl::opt< AsanCtorKind > ClConstructorKind("asan-constructor-kind", cl::desc("Sets the ASan constructor kind"), cl::values(clEnumValN(AsanCtorKind::None, "none", "No constructors"), clEnumValN(AsanCtorKind::Global, "global", "Use global constructors")), cl::init(AsanCtorKind::Global), cl::Hidden)
static const int kMaxAsanStackMallocSizeClass
static const uint64_t kMIPS32_ShadowOffset32
static cl::opt< bool > ClAlwaysSlowPath("asan-always-slow-path", cl::desc("use instrumentation with slow path for all accesses"), cl::Hidden, cl::init(false))
static const uint64_t kNetBSD_ShadowOffset32
static const uint64_t kFreeBSDAArch64_ShadowOffset64
static const uint64_t kSmallX86_64ShadowOffsetBase
static cl::opt< bool > ClInitializers("asan-initialization-order", cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(true))
static const uint64_t kNetBSD_ShadowOffset64
const char kAsanPtrSub[]
static cl::opt< unsigned > ClRealignStack("asan-realign-stack", cl::desc("Realign stack to the value of this flag (power of two)"), cl::Hidden, cl::init(32))
static const uint64_t kWindowsShadowOffset32
static cl::opt< bool > ClInstrumentReads("asan-instrument-reads", cl::desc("instrument read instructions"), cl::Hidden, cl::init(true))
static size_t TypeStoreSizeToSizeIndex(uint32_t TypeSize)
const char kAsanAllocaPoison[]
constexpr size_t kCompileKernelShift
static cl::opt< bool > ClWithIfunc("asan-with-ifunc", cl::desc("Access dynamic shadow through an ifunc global on " "platforms that support this"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClKasanMemIntrinCallbackPrefix("asan-kernel-mem-intrinsic-prefix", cl::desc("Use prefix for memory intrinsics in KASAN mode"), cl::Hidden, cl::init(false))
const char kAsanVersionCheckNamePrefix[]
const char kAMDGPUAddressPrivateName[]
static const uint64_t kNetBSDKasan_ShadowOffset64
const char kAMDGPUBallotName[]
const char kAsanRegisterElfGlobalsName[]
static cl::opt< uint64_t > ClMappingOffset("asan-mapping-offset", cl::desc("offset of asan shadow mapping [EXPERIMENTAL]"), cl::Hidden, cl::init(0))
const char kAsanReportErrorTemplate[]
static cl::opt< bool > ClWithComdat("asan-with-comdat", cl::desc("Place ASan constructors in comdat sections"), cl::Hidden, cl::init(true))
static StringRef getAllocaName(AllocaInst *AI)
static cl::opt< bool > ClSkipPromotableAllocas("asan-skip-promotable-allocas", cl::desc("Do not instrument promotable allocas"), cl::Hidden, cl::init(true))
static cl::opt< int > ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb", cl::init(10000), cl::desc("maximal number of instructions to instrument in any given BB"), cl::Hidden)
static const uintptr_t kRetiredStackFrameMagic
static cl::opt< bool > ClUseStackSafety("asan-use-stack-safety", cl::Hidden, cl::init(true), cl::Hidden, cl::desc("Use Stack Safety analysis results"), cl::Optional)
const char kAsanPoisonGlobalsName[]
const char kAsanHandleNoReturnName[]
static const size_t kMinStackMallocSize
static cl::opt< int > ClDebug("asan-debug", cl::desc("debug"), cl::Hidden, cl::init(0))
const char kAsanAllocasUnpoison[]
static const uint64_t kAArch64_ShadowOffset64
static cl::opt< bool > ClInvalidPointerPairs("asan-detect-invalid-pointer-pair", cl::desc("Instrument <, <=, >, >=, - with pointer operands"), cl::Hidden, cl::init(false))
Function Alias Analysis false
This file contains the simple types necessary to represent the attributes associated with functions a...
static bool isPointerOperand(Value *I, User *U)
static const Function * getParent(const Value *V)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DXIL Finalize Linkage
dxil translate DXIL Translate Metadata
This file defines the DenseMap class.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
static bool runOnFunction(Function &F, bool PostInlining)
This is the interface for a simple mod/ref and alias analysis over globals.
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
This defines the Use class.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
#define G(x, y, z)
Definition MD5.cpp:56
Machine Check Debug Module
This file contains the declarations for metadata subclasses.
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
if(PassOpts->AAPipeline)
const SmallVectorImpl< MachineOperand > & Cond
void visit(MachineFunction &MF, MachineBasicBlock &Start, std::function< void(MachineBasicBlock *)> op)
#define OP(OPC)
Definition Instruction.h:46
Shrink Wrap Pass
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1540
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1562
LLVM_ABI AddressSanitizerPass(const AddressSanitizerOptions &Options, bool UseGlobalGC=true, bool UseOdrIndicator=true, AsanDtorKind DestructorKind=AsanDtorKind::Global, AsanCtorKind ConstructorKind=AsanCtorKind::Global)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
an instruction to allocate memory on the stack
bool isSwiftError() const
Return true if this alloca is used as a swifterror argument to a call.
LLVM_ABI bool isStaticAlloca() const
Return true if this alloca is in the entry block of the function and is a constant size.
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
PointerType * getType() const
Overload to return most specific pointer type.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
LLVM_ABI std::optional< TypeSize > getAllocationSize(const DataLayout &DL) const
Get allocation size in bytes.
void setAlignment(Align Align)
const Value * getArraySize() const
Get the number of elements allocated.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
Class to represent array types.
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
LLVM_ABI const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Conditional or Unconditional Branch instruction.
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
bool isInlineAsm() const
Check if this call is an inline asm statement.
void setCannotMerge()
static LLVM_ABI CallBase * addOperandBundle(CallBase *CB, uint32_t ID, OperandBundleDef OB, InsertPosition InsertPt=nullptr)
Create a clone of CB with operand bundle OB added.
bool doesNotReturn() const
Determine if the call cannot return.
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
@ Largest
The linker will choose the largest COMDAT.
Definition Comdat.h:39
@ SameSize
The data referenced by the COMDAT must be the same size.
Definition Comdat.h:41
@ Any
The linker may choose any COMDAT.
Definition Comdat.h:37
@ NoDeduplicate
No deduplication is performed.
Definition Comdat.h:40
@ ExactMatch
The data referenced by the COMDAT must be the same.
Definition Comdat.h:38
ConstantArray - Constant Array Declarations.
Definition Constants.h:433
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition Constants.h:1274
static LLVM_ABI bool isValueValidForType(Type *Ty, uint64_t V)
This static method returns true if the type Ty is big enough to represent the value V.
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
A debug info location.
Definition DebugLoc.h:124
LLVM_ABI DILocation * get() const
Get the underlying DILocation.
Definition DebugLoc.cpp:50
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
const BasicBlock & front() const
Definition Function.h:858
static Function * createWithDefaultAttr(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Creates a function with some attributes recorded in llvm.module.flags and the LLVMContext applied.
Definition Function.cpp:380
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition Function.h:903
const Constant * getAliasee() const
Definition GlobalAlias.h:87
static LLVM_ABI GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition Globals.cpp:597
LLVM_ABI void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
LLVM_ABI void setComdat(Comdat *C)
Definition Globals.cpp:214
LLVM_ABI void setSection(StringRef S)
Change the section for this global.
Definition Globals.cpp:275
VisibilityTypes getVisibility() const
void setUnnamedAddr(UnnamedAddr Val)
bool hasLocalLinkage() const
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
ThreadLocalMode getThreadLocalMode() const
@ HiddenVisibility
The GV is hidden.
Definition GlobalValue.h:69
void setVisibility(VisibilityTypes V)
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
@ CommonLinkage
Tentative definitions.
Definition GlobalValue.h:63
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition GlobalValue.h:54
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition GlobalValue.h:62
DLLStorageClassTypes getDLLStorageClass() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ABI void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition Globals.cpp:552
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalVariable.
Analysis pass providing a never-invalidated alias analysis result.
This instruction compares its operands according to the predicate given to the constructor.
Common base class shared among various IRBuilders.
Definition IRBuilder.h:114
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Definition IRBuilder.h:1830
IntegerType * getInt1Ty()
Fetch the type representing a single bit.
Definition IRBuilder.h:547
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Definition IRBuilder.h:1864
CallInst * CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src, MaybeAlign SrcAlign, uint64_t Size, bool isVolatile=false, const AAMDNodes &AAInfo=AAMDNodes())
Create and insert a memcpy between the specified pointers.
Definition IRBuilder.h:687
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2251
Value * CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2357
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
BasicBlock::iterator GetInsertPoint() const
Definition IRBuilder.h:202
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2199
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition IRBuilder.h:1513
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition IRBuilder.h:562
Value * CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition IRBuilder.h:2036
BasicBlock * GetInsertBlock() const
Definition IRBuilder.h:201
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
Definition IRBuilder.h:567
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2333
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition IRBuilder.h:1923
LLVM_ABI CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition IRBuilder.h:522
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition IRBuilder.h:2494
Value * CreateNot(Value *V, const Twine &Name="")
Definition IRBuilder.h:1805
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2329
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1420
ConstantInt * getIntN(unsigned N, uint64_t C)
Get a constant N-bit value, zero extended or truncated from a 64-bit value.
Definition IRBuilder.h:533
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition IRBuilder.h:1847
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:1551
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition IRBuilder.h:1860
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1403
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2194
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Definition IRBuilder.h:2651
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:2508
LLVM_ABI Value * CreateTypeSize(Type *Ty, TypeSize Size)
Create an expression which evaluates to the number of units in Size at runtime.
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition IRBuilder.h:2277
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition IRBuilder.h:207
Type * getVoidTy()
Fetch the type representing void.
Definition IRBuilder.h:600
StoreInst * CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, bool isVolatile=false)
Definition IRBuilder.h:1883
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
Definition IRBuilder.h:1573
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition IRBuilder.h:552
Value * CreateAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2209
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1437
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2780
static LLVM_ABI InlineAsm * get(FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack=false, AsmDialect asmDialect=AD_ATT, bool canThrow=false)
InlineAsm::get - Return the specified uniqued inline asm string.
Definition InlineAsm.cpp:43
Base class for instruction visitors.
Definition InstVisitor.h:78
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:319
A wrapper class for inspecting calls to intrinsic functions.
An instruction for reading from memory.
static Error ParseSectionSpecifier(StringRef Spec, StringRef &Segment, StringRef &Section, unsigned &TAA, bool &TAAParsed, unsigned &StubSize)
Parse the section specifier indicated by "Spec".
LLVM_ABI MDNode * createUnlikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards false destination.
Definition MDBuilder.cpp:48
Metadata node.
Definition Metadata.h:1077
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1439
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1561
Tuple of metadata.
Definition Metadata.h:1489
This is the common base class for memset/memcpy/memmove.
Root of the metadata hierarchy.
Definition Metadata.h:63
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Evaluate the size and offset of an object pointed to by a Value* statically.
LLVM_ABI SizeOffsetAPInt compute(Value *V)
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & abandon()
Mark an analysis as abandoned.
Definition Analysis.h:171
Return a value (possibly void), from a function.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This pass performs the global (interprocedural) stack safety analysis (new pass manager).
bool stackAccessIsSafe(const Instruction &I) const
bool isSafe(const AllocaInst &AI) const
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:261
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
Class to represent struct types.
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition Type.cpp:414
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
AttributeList getAttrList(LLVMContext *C, ArrayRef< unsigned > ArgNos, bool Signed, bool Ret=false, AttributeList AL=AttributeList()) const
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
EltTy front() const
unsigned size() const
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
bool isAndroidVersionLT(unsigned Major) const
Definition Triple.h:821
bool isThumb() const
Tests whether the target is Thumb (little and big endian).
Definition Triple.h:909
bool isDriverKit() const
Is this an Apple DriverKit triple.
Definition Triple.h:600
bool isOSNetBSD() const
Definition Triple.h:630
bool isAndroid() const
Tests whether the target is Android.
Definition Triple.h:819
bool isABIN32() const
Definition Triple.h:1134
bool isMIPS64() const
Tests whether the target is MIPS 64-bit (little and big endian).
Definition Triple.h:1030
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:411
bool isLoongArch64() const
Tests whether the target is 64-bit LoongArch.
Definition Triple.h:1019
bool isMIPS32() const
Tests whether the target is MIPS 32-bit (little and big endian).
Definition Triple.h:1025
bool isOSWindows() const
Tests whether the OS is Windows.
Definition Triple.h:679
@ UnknownObjectFormat
Definition Triple.h:318
bool isARM() const
Tests whether the target is ARM (little and big endian).
Definition Triple.h:914
bool isOSLinux() const
Tests whether the OS is Linux.
Definition Triple.h:728
bool isAMDGPU() const
Definition Triple.h:906
bool isMacOSX() const
Is this a Mac OS X triple.
Definition Triple.h:566
bool isOSFreeBSD() const
Definition Triple.h:638
bool isOSEmscripten() const
Tests whether the OS is Emscripten.
Definition Triple.h:748
bool isWatchOS() const
Is this an Apple watchOS triple.
Definition Triple.h:585
bool isiOS() const
Is this an iOS triple.
Definition Triple.h:575
bool isPS() const
Tests whether the target is the PS4 or PS5 platform.
Definition Triple.h:816
bool isWasm() const
Tests whether the target is wasm (32- and 64-bit).
Definition Triple.h:1118
bool isOSFuchsia() const
Definition Triple.h:642
bool isOSHaiku() const
Tests whether the OS is Haiku.
Definition Triple.h:669
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:297
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:281
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition Type.h:311
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_range operands()
Definition User.h:292
Value * getOperand(unsigned i) const
Definition User.h:232
static LLVM_ABI ValueAsMetadata * get(Value *V)
Definition Metadata.cpp:502
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:396
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:130
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:355
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
CallInst * Call
Changed
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void getInterestingMemoryOperands(Module &M, Instruction *I, SmallVectorImpl< InterestingMemoryOperand > &Interesting)
Get all the memory operands from the instruction that needs to be instrumented.
void instrumentAddress(Module &M, IRBuilder<> &IRB, Instruction *OrigIns, Instruction *InsertBefore, Value *Addr, Align Alignment, TypeSize TypeStoreSize, bool IsWrite, Value *SizeArgument, bool UseCalls, bool Recover, int AsanScale, int AsanOffset)
Instrument the memory operand Addr.
uint64_t getRedzoneSizeForGlobal(int AsanScale, uint64_t SizeInBytes)
Given SizeInBytes of the Value to be instrunmented, Returns the redzone size corresponding to it.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ S_CSTRING_LITERALS
S_CSTRING_LITERALS - Section with literal C strings.
Definition MachO.h:131
@ OB
OB - OneByte - Set if this instruction has a one byte opcode.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
uint64_t getAllocaSizeInBytes(const AllocaInst &AI)
Context & getContext() const
Definition BasicBlock.h:99
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI, Instruction *I)
Replace the instruction specified by BI with the instruction specified by I.
@ Offset
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1705
LLVM_ABI SmallVector< uint8_t, 64 > GetShadowBytesAfterScope(const SmallVectorImpl< ASanStackVariableDescription > &Vars, const ASanStackFrameLayout &Layout)
LLVM_ABI GlobalVariable * createPrivateGlobalForString(Module &M, StringRef Str, bool AllowMerging, Twine NamePrefix="")
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
@ Done
Definition Threading.h:60
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
Definition InstrProf.h:296
LLVM_ABI Function * createSanitizerCtor(Module &M, StringRef CtorName)
Creates sanitizer constructor function.
AsanDetectStackUseAfterReturnMode
Mode of ASan detect stack use after return.
@ Always
Always detect stack use after return.
@ Never
Never detect stack use after return.
@ Runtime
Detect stack use after return if not disabled runtime with (ASAN_OPTIONS=detect_stack_use_after_retur...
LLVM_ABI DenseMap< BasicBlock *, ColorVector > colorEHFunclets(Function &F)
If an EH funclet personality is in use (see isFuncletEHPersonality), this will recompute which blocks...
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
Op::Description Desc
LLVM_ABI bool isAllocaPromotable(const AllocaInst *AI)
Return true if this alloca is legal for promotion.
LLVM_ABI SmallString< 64 > ComputeASanStackFrameDescription(const SmallVectorImpl< ASanStackVariableDescription > &Vars)
LLVM_ABI SmallVector< uint8_t, 64 > GetShadowBytes(const SmallVectorImpl< ASanStackVariableDescription > &Vars, const ASanStackFrameLayout &Layout)
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:186
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:759
LLVM_ABI FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName, ArrayRef< Type * > InitArgTypes, bool Weak=false)
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:302
LLVM_ABI std::string getUniqueModuleId(Module *M)
Produce a unique identifier for this module by taking the MD5 sum of the names of the module's strong...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:288
LLVM_ABI std::pair< Function *, FunctionCallee > createSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type * > InitArgTypes, ArrayRef< Value * > InitArgs, StringRef VersionCheckName=StringRef(), bool Weak=false)
Creates sanitizer constructor function, and calls sanitizer's init function from it.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void SplitBlockAndInsertIfThenElse(Value *Cond, BasicBlock::iterator SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
AsanDtorKind
Types of ASan module destructors supported.
@ Invalid
Not a valid destructor Kind.
@ Global
Append to llvm.global_dtors.
@ None
Do not emit any destructors for ASan.
LLVM_ABI ASanStackFrameLayout ComputeASanStackFrameLayout(SmallVectorImpl< ASanStackVariableDescription > &Vars, uint64_t Granularity, uint64_t MinHeaderSize)
TargetTransformInfo TTI
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
OperandBundleDefT< Value * > OperandBundleDef
Definition AutoUpgrade.h:34
LLVM_ABI void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
static const int kAsanStackUseAfterReturnMagic
LLVM_ABI void setGlobalVariableLargeSection(const Triple &TargetTriple, GlobalVariable &GV)
void removeASanIncompatibleFnAttributes(Function &F, bool ReadsArgMem)
Remove memory attributes that are incompatible with the instrumentation added by AddressSanitizer and...
DWARFExpression::Operation Op
@ Dynamic
Denotes mode unknown at compile time.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
TinyPtrVector< BasicBlock * > ColorVector
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
Align assumeAligned(uint64_t Value)
Treats the value 0 as a 1, so Align is always at least 1.
Definition Alignment.h:111
iterator_range< df_iterator< T > > depth_first(const T &G)
LLVM_ABI Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
AsanCtorKind
Types of ASan module constructors supported.
LLVM_ABI void maybeMarkSanitizerLibraryCallNoBuiltin(CallInst *CI, const TargetLibraryInfo *TLI)
Given a CallInst, check if it calls a string function known to CodeGen, and mark it with NoBuiltin if...
Definition Local.cpp:3832
LLVM_ABI void appendToUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.used list.
LLVM_ABI void appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Same as appendToGlobalCtors(), but for global dtors.
LLVM_ABI bool checkIfAlreadyInstrumented(Module &M, StringRef Flag)
Check if module has flag attached, if not add the flag.
void getAddressSanitizerParams(const Triple &TargetTriple, int LongSize, bool IsKasan, uint64_t *ShadowBase, int *MappingScale, bool *OrShadowOffset)
DEMANGLE_ABI std::string demangle(std::string_view MangledName)
Attempt to demangle a string using different demangling schemes.
Definition Demangle.cpp:20
std::string itostr(int64_t X)
LLVM_ABI void SplitBlockAndInsertForEachLane(ElementCount EC, Type *IndexTy, BasicBlock::iterator InsertBefore, std::function< void(IRBuilderBase &, Value *)> Func)
Utility function for performing a given action on each lane of a vector with EC elements.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI bool replaceDbgDeclare(Value *Address, Value *NewAddress, DIBuilder &Builder, uint8_t DIExprFlags, int Offset)
Replaces dbg.declare record when the address it describes is replaced with a new value.
Definition Local.cpp:1942
#define N
const uint8_t AccessSizeIndex
LLVM_ABI ASanAccessInfo(int32_t Packed)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:85
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:117
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition Alignment.h:141
Information about a load/store intrinsic defined by the target.
SmallVector< InterestingMemoryOperand, 1 > InterestingOperands
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70
SizeOffsetAPInt - Used by ObjectSizeOffsetVisitor, which works with APInts.