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