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