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