LLVM 19.0.0git
MemorySanitizer.cpp
Go to the documentation of this file.
1//===- MemorySanitizer.cpp - detector of uninitialized reads --------------===//
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/// \file
10/// This file is a part of MemorySanitizer, a detector of uninitialized
11/// reads.
12///
13/// The algorithm of the tool is similar to Memcheck
14/// (http://goo.gl/QKbem). We associate a few shadow bits with every
15/// byte of the application memory, poison the shadow of the malloc-ed
16/// or alloca-ed memory, load the shadow bits on every memory read,
17/// propagate the shadow bits through some of the arithmetic
18/// instruction (including MOV), store the shadow bits on every memory
19/// write, report a bug on some other instructions (e.g. JMP) if the
20/// associated shadow is poisoned.
21///
22/// But there are differences too. The first and the major one:
23/// compiler instrumentation instead of binary instrumentation. This
24/// gives us much better register allocation, possible compiler
25/// optimizations and a fast start-up. But this brings the major issue
26/// as well: msan needs to see all program events, including system
27/// calls and reads/writes in system libraries, so we either need to
28/// compile *everything* with msan or use a binary translation
29/// component (e.g. DynamoRIO) to instrument pre-built libraries.
30/// Another difference from Memcheck is that we use 8 shadow bits per
31/// byte of application memory and use a direct shadow mapping. This
32/// greatly simplifies the instrumentation code and avoids races on
33/// shadow updates (Memcheck is single-threaded so races are not a
34/// concern there. Memcheck uses 2 shadow bits per byte with a slow
35/// path storage that uses 8 bits per byte).
36///
37/// The default value of shadow is 0, which means "clean" (not poisoned).
38///
39/// Every module initializer should call __msan_init to ensure that the
40/// shadow memory is ready. On error, __msan_warning is called. Since
41/// parameters and return values may be passed via registers, we have a
42/// specialized thread-local shadow for return values
43/// (__msan_retval_tls) and parameters (__msan_param_tls).
44///
45/// Origin tracking.
46///
47/// MemorySanitizer can track origins (allocation points) of all uninitialized
48/// values. This behavior is controlled with a flag (msan-track-origins) and is
49/// disabled by default.
50///
51/// Origins are 4-byte values created and interpreted by the runtime library.
52/// They are stored in a second shadow mapping, one 4-byte value for 4 bytes
53/// of application memory. Propagation of origins is basically a bunch of
54/// "select" instructions that pick the origin of a dirty argument, if an
55/// instruction has one.
56///
57/// Every 4 aligned, consecutive bytes of application memory have one origin
58/// value associated with them. If these bytes contain uninitialized data
59/// coming from 2 different allocations, the last store wins. Because of this,
60/// MemorySanitizer reports can show unrelated origins, but this is unlikely in
61/// practice.
62///
63/// Origins are meaningless for fully initialized values, so MemorySanitizer
64/// avoids storing origin to memory when a fully initialized value is stored.
65/// This way it avoids needless overwriting origin of the 4-byte region on
66/// a short (i.e. 1 byte) clean store, and it is also good for performance.
67///
68/// Atomic handling.
69///
70/// Ideally, every atomic store of application value should update the
71/// corresponding shadow location in an atomic way. Unfortunately, atomic store
72/// of two disjoint locations can not be done without severe slowdown.
73///
74/// Therefore, we implement an approximation that may err on the safe side.
75/// In this implementation, every atomically accessed location in the program
76/// may only change from (partially) uninitialized to fully initialized, but
77/// not the other way around. We load the shadow _after_ the application load,
78/// and we store the shadow _before_ the app store. Also, we always store clean
79/// shadow (if the application store is atomic). This way, if the store-load
80/// pair constitutes a happens-before arc, shadow store and load are correctly
81/// ordered such that the load will get either the value that was stored, or
82/// some later value (which is always clean).
83///
84/// This does not work very well with Compare-And-Swap (CAS) and
85/// Read-Modify-Write (RMW) operations. To follow the above logic, CAS and RMW
86/// must store the new shadow before the app operation, and load the shadow
87/// after the app operation. Computers don't work this way. Current
88/// implementation ignores the load aspect of CAS/RMW, always returning a clean
89/// value. It implements the store part as a simple atomic store by storing a
90/// clean shadow.
91///
92/// Instrumenting inline assembly.
93///
94/// For inline assembly code LLVM has little idea about which memory locations
95/// become initialized depending on the arguments. It can be possible to figure
96/// out which arguments are meant to point to inputs and outputs, but the
97/// actual semantics can be only visible at runtime. In the Linux kernel it's
98/// also possible that the arguments only indicate the offset for a base taken
99/// from a segment register, so it's dangerous to treat any asm() arguments as
100/// pointers. We take a conservative approach generating calls to
101/// __msan_instrument_asm_store(ptr, size)
102/// , which defer the memory unpoisoning to the runtime library.
103/// The latter can perform more complex address checks to figure out whether
104/// it's safe to touch the shadow memory.
105/// Like with atomic operations, we call __msan_instrument_asm_store() before
106/// the assembly call, so that changes to the shadow memory will be seen by
107/// other threads together with main memory initialization.
108///
109/// KernelMemorySanitizer (KMSAN) implementation.
110///
111/// The major differences between KMSAN and MSan instrumentation are:
112/// - KMSAN always tracks the origins and implies msan-keep-going=true;
113/// - KMSAN allocates shadow and origin memory for each page separately, so
114/// there are no explicit accesses to shadow and origin in the
115/// instrumentation.
116/// Shadow and origin values for a particular X-byte memory location
117/// (X=1,2,4,8) are accessed through pointers obtained via the
118/// __msan_metadata_ptr_for_load_X(ptr)
119/// __msan_metadata_ptr_for_store_X(ptr)
120/// functions. The corresponding functions check that the X-byte accesses
121/// are possible and returns the pointers to shadow and origin memory.
122/// Arbitrary sized accesses are handled with:
123/// __msan_metadata_ptr_for_load_n(ptr, size)
124/// __msan_metadata_ptr_for_store_n(ptr, size);
125/// Note that the sanitizer code has to deal with how shadow/origin pairs
126/// returned by the these functions are represented in different ABIs. In
127/// the X86_64 ABI they are returned in RDX:RAX, and in the SystemZ ABI they
128/// are written to memory pointed to by a hidden parameter.
129/// - TLS variables are stored in a single per-task struct. A call to a
130/// function __msan_get_context_state() returning a pointer to that struct
131/// is inserted into every instrumented function before the entry block;
132/// - __msan_warning() takes a 32-bit origin parameter;
133/// - local variables are poisoned with __msan_poison_alloca() upon function
134/// entry and unpoisoned with __msan_unpoison_alloca() before leaving the
135/// function;
136/// - the pass doesn't declare any global variables or add global constructors
137/// to the translation unit.
138///
139/// Also, KMSAN currently ignores uninitialized memory passed into inline asm
140/// calls, making sure we're on the safe side wrt. possible false positives.
141///
142/// KernelMemorySanitizer only supports X86_64 and SystemZ at the moment.
143///
144//
145// FIXME: This sanitizer does not yet handle scalable vectors
146//
147//===----------------------------------------------------------------------===//
148
150#include "llvm/ADT/APInt.h"
151#include "llvm/ADT/ArrayRef.h"
152#include "llvm/ADT/DenseMap.h"
154#include "llvm/ADT/SetVector.h"
155#include "llvm/ADT/SmallPtrSet.h"
156#include "llvm/ADT/SmallVector.h"
158#include "llvm/ADT/StringRef.h"
162#include "llvm/IR/Argument.h"
164#include "llvm/IR/Attributes.h"
165#include "llvm/IR/BasicBlock.h"
166#include "llvm/IR/CallingConv.h"
167#include "llvm/IR/Constant.h"
168#include "llvm/IR/Constants.h"
169#include "llvm/IR/DataLayout.h"
170#include "llvm/IR/DerivedTypes.h"
171#include "llvm/IR/Function.h"
172#include "llvm/IR/GlobalValue.h"
174#include "llvm/IR/IRBuilder.h"
175#include "llvm/IR/InlineAsm.h"
176#include "llvm/IR/InstVisitor.h"
177#include "llvm/IR/InstrTypes.h"
178#include "llvm/IR/Instruction.h"
179#include "llvm/IR/Instructions.h"
181#include "llvm/IR/Intrinsics.h"
182#include "llvm/IR/IntrinsicsX86.h"
183#include "llvm/IR/MDBuilder.h"
184#include "llvm/IR/Module.h"
185#include "llvm/IR/Type.h"
186#include "llvm/IR/Value.h"
187#include "llvm/IR/ValueMap.h"
190#include "llvm/Support/Casting.h"
192#include "llvm/Support/Debug.h"
201#include <algorithm>
202#include <cassert>
203#include <cstddef>
204#include <cstdint>
205#include <memory>
206#include <string>
207#include <tuple>
208
209using namespace llvm;
210
211#define DEBUG_TYPE "msan"
212
213DEBUG_COUNTER(DebugInsertCheck, "msan-insert-check",
214 "Controls which checks to insert");
215
216DEBUG_COUNTER(DebugInstrumentInstruction, "msan-instrument-instruction",
217 "Controls which instruction to instrument");
218
219static const unsigned kOriginSize = 4;
222
223// These constants must be kept in sync with the ones in msan.h.
224static const unsigned kParamTLSSize = 800;
225static const unsigned kRetvalTLSSize = 800;
226
227// Accesses sizes are powers of two: 1, 2, 4, 8.
228static const size_t kNumberOfAccessSizes = 4;
229
230/// Track origins of uninitialized values.
231///
232/// Adds a section to MemorySanitizer report that points to the allocation
233/// (stack or heap) the uninitialized bits came from originally.
235 "msan-track-origins",
236 cl::desc("Track origins (allocation sites) of poisoned memory"), cl::Hidden,
237 cl::init(0));
238
239static cl::opt<bool> ClKeepGoing("msan-keep-going",
240 cl::desc("keep going after reporting a UMR"),
241 cl::Hidden, cl::init(false));
242
243static cl::opt<bool>
244 ClPoisonStack("msan-poison-stack",
245 cl::desc("poison uninitialized stack variables"), cl::Hidden,
246 cl::init(true));
247
249 "msan-poison-stack-with-call",
250 cl::desc("poison uninitialized stack variables with a call"), cl::Hidden,
251 cl::init(false));
252
254 "msan-poison-stack-pattern",
255 cl::desc("poison uninitialized stack variables with the given pattern"),
256 cl::Hidden, cl::init(0xff));
257
258static cl::opt<bool>
259 ClPrintStackNames("msan-print-stack-names",
260 cl::desc("Print name of local stack variable"),
261 cl::Hidden, cl::init(true));
262
263static cl::opt<bool> ClPoisonUndef("msan-poison-undef",
264 cl::desc("poison undef temps"), cl::Hidden,
265 cl::init(true));
266
267static cl::opt<bool>
268 ClHandleICmp("msan-handle-icmp",
269 cl::desc("propagate shadow through ICmpEQ and ICmpNE"),
270 cl::Hidden, cl::init(true));
271
272static cl::opt<bool>
273 ClHandleICmpExact("msan-handle-icmp-exact",
274 cl::desc("exact handling of relational integer ICmp"),
275 cl::Hidden, cl::init(false));
276
278 "msan-handle-lifetime-intrinsics",
279 cl::desc(
280 "when possible, poison scoped variables at the beginning of the scope "
281 "(slower, but more precise)"),
282 cl::Hidden, cl::init(true));
283
284// When compiling the Linux kernel, we sometimes see false positives related to
285// MSan being unable to understand that inline assembly calls may initialize
286// local variables.
287// This flag makes the compiler conservatively unpoison every memory location
288// passed into an assembly call. Note that this may cause false positives.
289// Because it's impossible to figure out the array sizes, we can only unpoison
290// the first sizeof(type) bytes for each type* pointer.
292 "msan-handle-asm-conservative",
293 cl::desc("conservative handling of inline assembly"), cl::Hidden,
294 cl::init(true));
295
296// This flag controls whether we check the shadow of the address
297// operand of load or store. Such bugs are very rare, since load from
298// a garbage address typically results in SEGV, but still happen
299// (e.g. only lower bits of address are garbage, or the access happens
300// early at program startup where malloc-ed memory is more likely to
301// be zeroed. As of 2012-08-28 this flag adds 20% slowdown.
303 "msan-check-access-address",
304 cl::desc("report accesses through a pointer which has poisoned shadow"),
305 cl::Hidden, cl::init(true));
306
308 "msan-eager-checks",
309 cl::desc("check arguments and return values at function call boundaries"),
310 cl::Hidden, cl::init(false));
311
313 "msan-dump-strict-instructions",
314 cl::desc("print out instructions with default strict semantics"),
315 cl::Hidden, cl::init(false));
316
318 "msan-instrumentation-with-call-threshold",
319 cl::desc(
320 "If the function being instrumented requires more than "
321 "this number of checks and origin stores, use callbacks instead of "
322 "inline checks (-1 means never use callbacks)."),
323 cl::Hidden, cl::init(3500));
324
325static cl::opt<bool>
326 ClEnableKmsan("msan-kernel",
327 cl::desc("Enable KernelMemorySanitizer instrumentation"),
328 cl::Hidden, cl::init(false));
329
330static cl::opt<bool>
331 ClDisableChecks("msan-disable-checks",
332 cl::desc("Apply no_sanitize to the whole file"), cl::Hidden,
333 cl::init(false));
334
335static cl::opt<bool>
336 ClCheckConstantShadow("msan-check-constant-shadow",
337 cl::desc("Insert checks for constant shadow values"),
338 cl::Hidden, cl::init(true));
339
340// This is off by default because of a bug in gold:
341// https://sourceware.org/bugzilla/show_bug.cgi?id=19002
342static cl::opt<bool>
343 ClWithComdat("msan-with-comdat",
344 cl::desc("Place MSan constructors in comdat sections"),
345 cl::Hidden, cl::init(false));
346
347// These options allow to specify custom memory map parameters
348// See MemoryMapParams for details.
349static cl::opt<uint64_t> ClAndMask("msan-and-mask",
350 cl::desc("Define custom MSan AndMask"),
351 cl::Hidden, cl::init(0));
352
353static cl::opt<uint64_t> ClXorMask("msan-xor-mask",
354 cl::desc("Define custom MSan XorMask"),
355 cl::Hidden, cl::init(0));
356
357static cl::opt<uint64_t> ClShadowBase("msan-shadow-base",
358 cl::desc("Define custom MSan ShadowBase"),
359 cl::Hidden, cl::init(0));
360
361static cl::opt<uint64_t> ClOriginBase("msan-origin-base",
362 cl::desc("Define custom MSan OriginBase"),
363 cl::Hidden, cl::init(0));
364
365static cl::opt<int>
366 ClDisambiguateWarning("msan-disambiguate-warning-threshold",
367 cl::desc("Define threshold for number of checks per "
368 "debug location to force origin update."),
369 cl::Hidden, cl::init(3));
370
371const char kMsanModuleCtorName[] = "msan.module_ctor";
372const char kMsanInitName[] = "__msan_init";
373
374namespace {
375
376// Memory map parameters used in application-to-shadow address calculation.
377// Offset = (Addr & ~AndMask) ^ XorMask
378// Shadow = ShadowBase + Offset
379// Origin = OriginBase + Offset
380struct MemoryMapParams {
381 uint64_t AndMask;
382 uint64_t XorMask;
383 uint64_t ShadowBase;
384 uint64_t OriginBase;
385};
386
387struct PlatformMemoryMapParams {
388 const MemoryMapParams *bits32;
389 const MemoryMapParams *bits64;
390};
391
392} // end anonymous namespace
393
394// i386 Linux
395static const MemoryMapParams Linux_I386_MemoryMapParams = {
396 0x000080000000, // AndMask
397 0, // XorMask (not used)
398 0, // ShadowBase (not used)
399 0x000040000000, // OriginBase
400};
401
402// x86_64 Linux
403static const MemoryMapParams Linux_X86_64_MemoryMapParams = {
404 0, // AndMask (not used)
405 0x500000000000, // XorMask
406 0, // ShadowBase (not used)
407 0x100000000000, // OriginBase
408};
409
410// mips64 Linux
411static const MemoryMapParams Linux_MIPS64_MemoryMapParams = {
412 0, // AndMask (not used)
413 0x008000000000, // XorMask
414 0, // ShadowBase (not used)
415 0x002000000000, // OriginBase
416};
417
418// ppc64 Linux
419static const MemoryMapParams Linux_PowerPC64_MemoryMapParams = {
420 0xE00000000000, // AndMask
421 0x100000000000, // XorMask
422 0x080000000000, // ShadowBase
423 0x1C0000000000, // OriginBase
424};
425
426// s390x Linux
427static const MemoryMapParams Linux_S390X_MemoryMapParams = {
428 0xC00000000000, // AndMask
429 0, // XorMask (not used)
430 0x080000000000, // ShadowBase
431 0x1C0000000000, // OriginBase
432};
433
434// aarch64 Linux
435static const MemoryMapParams Linux_AArch64_MemoryMapParams = {
436 0, // AndMask (not used)
437 0x0B00000000000, // XorMask
438 0, // ShadowBase (not used)
439 0x0200000000000, // OriginBase
440};
441
442// loongarch64 Linux
443static const MemoryMapParams Linux_LoongArch64_MemoryMapParams = {
444 0, // AndMask (not used)
445 0x500000000000, // XorMask
446 0, // ShadowBase (not used)
447 0x100000000000, // OriginBase
448};
449
450// aarch64 FreeBSD
451static const MemoryMapParams FreeBSD_AArch64_MemoryMapParams = {
452 0x1800000000000, // AndMask
453 0x0400000000000, // XorMask
454 0x0200000000000, // ShadowBase
455 0x0700000000000, // OriginBase
456};
457
458// i386 FreeBSD
459static const MemoryMapParams FreeBSD_I386_MemoryMapParams = {
460 0x000180000000, // AndMask
461 0x000040000000, // XorMask
462 0x000020000000, // ShadowBase
463 0x000700000000, // OriginBase
464};
465
466// x86_64 FreeBSD
467static const MemoryMapParams FreeBSD_X86_64_MemoryMapParams = {
468 0xc00000000000, // AndMask
469 0x200000000000, // XorMask
470 0x100000000000, // ShadowBase
471 0x380000000000, // OriginBase
472};
473
474// x86_64 NetBSD
475static const MemoryMapParams NetBSD_X86_64_MemoryMapParams = {
476 0, // AndMask
477 0x500000000000, // XorMask
478 0, // ShadowBase
479 0x100000000000, // OriginBase
480};
481
482static const PlatformMemoryMapParams Linux_X86_MemoryMapParams = {
485};
486
487static const PlatformMemoryMapParams Linux_MIPS_MemoryMapParams = {
488 nullptr,
490};
491
492static const PlatformMemoryMapParams Linux_PowerPC_MemoryMapParams = {
493 nullptr,
495};
496
497static const PlatformMemoryMapParams Linux_S390_MemoryMapParams = {
498 nullptr,
500};
501
502static const PlatformMemoryMapParams Linux_ARM_MemoryMapParams = {
503 nullptr,
505};
506
507static const PlatformMemoryMapParams Linux_LoongArch_MemoryMapParams = {
508 nullptr,
510};
511
512static const PlatformMemoryMapParams FreeBSD_ARM_MemoryMapParams = {
513 nullptr,
515};
516
517static const PlatformMemoryMapParams FreeBSD_X86_MemoryMapParams = {
520};
521
522static const PlatformMemoryMapParams NetBSD_X86_MemoryMapParams = {
523 nullptr,
525};
526
527namespace {
528
529/// Instrument functions of a module to detect uninitialized reads.
530///
531/// Instantiating MemorySanitizer inserts the msan runtime library API function
532/// declarations into the module if they don't exist already. Instantiating
533/// ensures the __msan_init function is in the list of global constructors for
534/// the module.
535class MemorySanitizer {
536public:
537 MemorySanitizer(Module &M, MemorySanitizerOptions Options)
538 : CompileKernel(Options.Kernel), TrackOrigins(Options.TrackOrigins),
539 Recover(Options.Recover), EagerChecks(Options.EagerChecks) {
540 initializeModule(M);
541 }
542
543 // MSan cannot be moved or copied because of MapParams.
544 MemorySanitizer(MemorySanitizer &&) = delete;
545 MemorySanitizer &operator=(MemorySanitizer &&) = delete;
546 MemorySanitizer(const MemorySanitizer &) = delete;
547 MemorySanitizer &operator=(const MemorySanitizer &) = delete;
548
549 bool sanitizeFunction(Function &F, TargetLibraryInfo &TLI);
550
551private:
552 friend struct MemorySanitizerVisitor;
553 friend struct VarArgHelperBase;
554 friend struct VarArgAMD64Helper;
555 friend struct VarArgMIPS64Helper;
556 friend struct VarArgAArch64Helper;
557 friend struct VarArgPowerPC64Helper;
558 friend struct VarArgSystemZHelper;
559
560 void initializeModule(Module &M);
561 void initializeCallbacks(Module &M, const TargetLibraryInfo &TLI);
562 void createKernelApi(Module &M, const TargetLibraryInfo &TLI);
563 void createUserspaceApi(Module &M, const TargetLibraryInfo &TLI);
564
565 template <typename... ArgsTy>
566 FunctionCallee getOrInsertMsanMetadataFunction(Module &M, StringRef Name,
567 ArgsTy... Args);
568
569 /// True if we're compiling the Linux kernel.
570 bool CompileKernel;
571 /// Track origins (allocation points) of uninitialized values.
572 int TrackOrigins;
573 bool Recover;
574 bool EagerChecks;
575
576 Triple TargetTriple;
577 LLVMContext *C;
578 Type *IntptrTy; ///< Integer type with the size of a ptr in default AS.
579 Type *OriginTy;
580 PointerType *PtrTy; ///< Integer type with the size of a ptr in default AS.
581
582 // XxxTLS variables represent the per-thread state in MSan and per-task state
583 // in KMSAN.
584 // For the userspace these point to thread-local globals. In the kernel land
585 // they point to the members of a per-task struct obtained via a call to
586 // __msan_get_context_state().
587
588 /// Thread-local shadow storage for function parameters.
589 Value *ParamTLS;
590
591 /// Thread-local origin storage for function parameters.
592 Value *ParamOriginTLS;
593
594 /// Thread-local shadow storage for function return value.
595 Value *RetvalTLS;
596
597 /// Thread-local origin storage for function return value.
598 Value *RetvalOriginTLS;
599
600 /// Thread-local shadow storage for in-register va_arg function.
601 Value *VAArgTLS;
602
603 /// Thread-local shadow storage for in-register va_arg function.
604 Value *VAArgOriginTLS;
605
606 /// Thread-local shadow storage for va_arg overflow area.
607 Value *VAArgOverflowSizeTLS;
608
609 /// Are the instrumentation callbacks set up?
610 bool CallbacksInitialized = false;
611
612 /// The run-time callback to print a warning.
613 FunctionCallee WarningFn;
614
615 // These arrays are indexed by log2(AccessSize).
616 FunctionCallee MaybeWarningFn[kNumberOfAccessSizes];
617 FunctionCallee MaybeStoreOriginFn[kNumberOfAccessSizes];
618
619 /// Run-time helper that generates a new origin value for a stack
620 /// allocation.
621 FunctionCallee MsanSetAllocaOriginWithDescriptionFn;
622 // No description version
623 FunctionCallee MsanSetAllocaOriginNoDescriptionFn;
624
625 /// Run-time helper that poisons stack on function entry.
626 FunctionCallee MsanPoisonStackFn;
627
628 /// Run-time helper that records a store (or any event) of an
629 /// uninitialized value and returns an updated origin id encoding this info.
630 FunctionCallee MsanChainOriginFn;
631
632 /// Run-time helper that paints an origin over a region.
633 FunctionCallee MsanSetOriginFn;
634
635 /// MSan runtime replacements for memmove, memcpy and memset.
636 FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
637
638 /// KMSAN callback for task-local function argument shadow.
639 StructType *MsanContextStateTy;
640 FunctionCallee MsanGetContextStateFn;
641
642 /// Functions for poisoning/unpoisoning local variables
643 FunctionCallee MsanPoisonAllocaFn, MsanUnpoisonAllocaFn;
644
645 /// Pair of shadow/origin pointers.
646 Type *MsanMetadata;
647
648 /// Each of the MsanMetadataPtrXxx functions returns a MsanMetadata.
649 FunctionCallee MsanMetadataPtrForLoadN, MsanMetadataPtrForStoreN;
650 FunctionCallee MsanMetadataPtrForLoad_1_8[4];
651 FunctionCallee MsanMetadataPtrForStore_1_8[4];
652 FunctionCallee MsanInstrumentAsmStoreFn;
653
654 /// Storage for return values of the MsanMetadataPtrXxx functions.
655 Value *MsanMetadataAlloca;
656
657 /// Helper to choose between different MsanMetadataPtrXxx().
658 FunctionCallee getKmsanShadowOriginAccessFn(bool isStore, int size);
659
660 /// Memory map parameters used in application-to-shadow calculation.
661 const MemoryMapParams *MapParams;
662
663 /// Custom memory map parameters used when -msan-shadow-base or
664 // -msan-origin-base is provided.
665 MemoryMapParams CustomMapParams;
666
667 MDNode *ColdCallWeights;
668
669 /// Branch weights for origin store.
670 MDNode *OriginStoreWeights;
671};
672
673void insertModuleCtor(Module &M) {
676 /*InitArgTypes=*/{},
677 /*InitArgs=*/{},
678 // This callback is invoked when the functions are created the first
679 // time. Hook them into the global ctors list in that case:
680 [&](Function *Ctor, FunctionCallee) {
681 if (!ClWithComdat) {
682 appendToGlobalCtors(M, Ctor, 0);
683 return;
684 }
685 Comdat *MsanCtorComdat = M.getOrInsertComdat(kMsanModuleCtorName);
686 Ctor->setComdat(MsanCtorComdat);
687 appendToGlobalCtors(M, Ctor, 0, Ctor);
688 });
689}
690
691template <class T> T getOptOrDefault(const cl::opt<T> &Opt, T Default) {
692 return (Opt.getNumOccurrences() > 0) ? Opt : Default;
693}
694
695} // end anonymous namespace
696
698 bool EagerChecks)
699 : Kernel(getOptOrDefault(ClEnableKmsan, K)),
700 TrackOrigins(getOptOrDefault(ClTrackOrigins, Kernel ? 2 : TO)),
701 Recover(getOptOrDefault(ClKeepGoing, Kernel || R)),
702 EagerChecks(getOptOrDefault(ClEagerChecks, EagerChecks)) {}
703
706 bool Modified = false;
707 if (!Options.Kernel) {
708 insertModuleCtor(M);
709 Modified = true;
710 }
711
712 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
713 for (Function &F : M) {
714 if (F.empty())
715 continue;
716 MemorySanitizer Msan(*F.getParent(), Options);
717 Modified |=
718 Msan.sanitizeFunction(F, FAM.getResult<TargetLibraryAnalysis>(F));
719 }
720
721 if (!Modified)
722 return PreservedAnalyses::all();
723
725 // GlobalsAA is considered stateless and does not get invalidated unless
726 // explicitly invalidated; PreservedAnalyses::none() is not enough. Sanitizers
727 // make changes that require GlobalsAA to be invalidated.
728 PA.abandon<GlobalsAA>();
729 return PA;
730}
731
733 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
735 OS, MapClassName2PassName);
736 OS << '<';
737 if (Options.Recover)
738 OS << "recover;";
739 if (Options.Kernel)
740 OS << "kernel;";
741 if (Options.EagerChecks)
742 OS << "eager-checks;";
743 OS << "track-origins=" << Options.TrackOrigins;
744 OS << '>';
745}
746
747/// Create a non-const global initialized with the given string.
748///
749/// Creates a writable global for Str so that we can pass it to the
750/// run-time lib. Runtime uses first 4 bytes of the string to store the
751/// frame ID, so the string needs to be mutable.
753 StringRef Str) {
754 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
755 return new GlobalVariable(M, StrConst->getType(), /*isConstant=*/true,
756 GlobalValue::PrivateLinkage, StrConst, "");
757}
758
759template <typename... ArgsTy>
761MemorySanitizer::getOrInsertMsanMetadataFunction(Module &M, StringRef Name,
762 ArgsTy... Args) {
763 if (TargetTriple.getArch() == Triple::systemz) {
764 // SystemZ ABI: shadow/origin pair is returned via a hidden parameter.
765 return M.getOrInsertFunction(Name, Type::getVoidTy(*C),
766 PointerType::get(MsanMetadata, 0),
767 std::forward<ArgsTy>(Args)...);
768 }
769
770 return M.getOrInsertFunction(Name, MsanMetadata,
771 std::forward<ArgsTy>(Args)...);
772}
773
774/// Create KMSAN API callbacks.
775void MemorySanitizer::createKernelApi(Module &M, const TargetLibraryInfo &TLI) {
776 IRBuilder<> IRB(*C);
777
778 // These will be initialized in insertKmsanPrologue().
779 RetvalTLS = nullptr;
780 RetvalOriginTLS = nullptr;
781 ParamTLS = nullptr;
782 ParamOriginTLS = nullptr;
783 VAArgTLS = nullptr;
784 VAArgOriginTLS = nullptr;
785 VAArgOverflowSizeTLS = nullptr;
786
787 WarningFn = M.getOrInsertFunction("__msan_warning",
788 TLI.getAttrList(C, {0}, /*Signed=*/false),
789 IRB.getVoidTy(), IRB.getInt32Ty());
790
791 // Requests the per-task context state (kmsan_context_state*) from the
792 // runtime library.
793 MsanContextStateTy = StructType::get(
794 ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8),
795 ArrayType::get(IRB.getInt64Ty(), kRetvalTLSSize / 8),
796 ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8),
797 ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), /* va_arg_origin */
798 IRB.getInt64Ty(), ArrayType::get(OriginTy, kParamTLSSize / 4), OriginTy,
799 OriginTy);
800 MsanGetContextStateFn = M.getOrInsertFunction(
801 "__msan_get_context_state", PointerType::get(MsanContextStateTy, 0));
802
803 MsanMetadata = StructType::get(PointerType::get(IRB.getInt8Ty(), 0),
804 PointerType::get(IRB.getInt32Ty(), 0));
805
806 for (int ind = 0, size = 1; ind < 4; ind++, size <<= 1) {
807 std::string name_load =
808 "__msan_metadata_ptr_for_load_" + std::to_string(size);
809 std::string name_store =
810 "__msan_metadata_ptr_for_store_" + std::to_string(size);
811 MsanMetadataPtrForLoad_1_8[ind] = getOrInsertMsanMetadataFunction(
812 M, name_load, PointerType::get(IRB.getInt8Ty(), 0));
813 MsanMetadataPtrForStore_1_8[ind] = getOrInsertMsanMetadataFunction(
814 M, name_store, PointerType::get(IRB.getInt8Ty(), 0));
815 }
816
817 MsanMetadataPtrForLoadN = getOrInsertMsanMetadataFunction(
818 M, "__msan_metadata_ptr_for_load_n", PointerType::get(IRB.getInt8Ty(), 0),
819 IRB.getInt64Ty());
820 MsanMetadataPtrForStoreN = getOrInsertMsanMetadataFunction(
821 M, "__msan_metadata_ptr_for_store_n",
822 PointerType::get(IRB.getInt8Ty(), 0), IRB.getInt64Ty());
823
824 // Functions for poisoning and unpoisoning memory.
825 MsanPoisonAllocaFn = M.getOrInsertFunction(
826 "__msan_poison_alloca", IRB.getVoidTy(), PtrTy, IntptrTy, PtrTy);
827 MsanUnpoisonAllocaFn = M.getOrInsertFunction(
828 "__msan_unpoison_alloca", IRB.getVoidTy(), PtrTy, IntptrTy);
829}
830
832 return M.getOrInsertGlobal(Name, Ty, [&] {
833 return new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage,
834 nullptr, Name, nullptr,
836 });
837}
838
839/// Insert declarations for userspace-specific functions and globals.
840void MemorySanitizer::createUserspaceApi(Module &M, const TargetLibraryInfo &TLI) {
841 IRBuilder<> IRB(*C);
842
843 // Create the callback.
844 // FIXME: this function should have "Cold" calling conv,
845 // which is not yet implemented.
846 if (TrackOrigins) {
847 StringRef WarningFnName = Recover ? "__msan_warning_with_origin"
848 : "__msan_warning_with_origin_noreturn";
849 WarningFn = M.getOrInsertFunction(WarningFnName,
850 TLI.getAttrList(C, {0}, /*Signed=*/false),
851 IRB.getVoidTy(), IRB.getInt32Ty());
852 } else {
853 StringRef WarningFnName =
854 Recover ? "__msan_warning" : "__msan_warning_noreturn";
855 WarningFn = M.getOrInsertFunction(WarningFnName, IRB.getVoidTy());
856 }
857
858 // Create the global TLS variables.
859 RetvalTLS =
860 getOrInsertGlobal(M, "__msan_retval_tls",
861 ArrayType::get(IRB.getInt64Ty(), kRetvalTLSSize / 8));
862
863 RetvalOriginTLS = getOrInsertGlobal(M, "__msan_retval_origin_tls", OriginTy);
864
865 ParamTLS =
866 getOrInsertGlobal(M, "__msan_param_tls",
867 ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8));
868
869 ParamOriginTLS =
870 getOrInsertGlobal(M, "__msan_param_origin_tls",
871 ArrayType::get(OriginTy, kParamTLSSize / 4));
872
873 VAArgTLS =
874 getOrInsertGlobal(M, "__msan_va_arg_tls",
875 ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8));
876
877 VAArgOriginTLS =
878 getOrInsertGlobal(M, "__msan_va_arg_origin_tls",
879 ArrayType::get(OriginTy, kParamTLSSize / 4));
880
881 VAArgOverflowSizeTLS =
882 getOrInsertGlobal(M, "__msan_va_arg_overflow_size_tls", IRB.getInt64Ty());
883
884 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
885 AccessSizeIndex++) {
886 unsigned AccessSize = 1 << AccessSizeIndex;
887 std::string FunctionName = "__msan_maybe_warning_" + itostr(AccessSize);
888 MaybeWarningFn[AccessSizeIndex] = M.getOrInsertFunction(
889 FunctionName, TLI.getAttrList(C, {0, 1}, /*Signed=*/false),
890 IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8), IRB.getInt32Ty());
891
892 FunctionName = "__msan_maybe_store_origin_" + itostr(AccessSize);
893 MaybeStoreOriginFn[AccessSizeIndex] = M.getOrInsertFunction(
894 FunctionName, TLI.getAttrList(C, {0, 2}, /*Signed=*/false),
895 IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8), PtrTy,
896 IRB.getInt32Ty());
897 }
898
899 MsanSetAllocaOriginWithDescriptionFn =
900 M.getOrInsertFunction("__msan_set_alloca_origin_with_descr",
901 IRB.getVoidTy(), PtrTy, IntptrTy, PtrTy, PtrTy);
902 MsanSetAllocaOriginNoDescriptionFn =
903 M.getOrInsertFunction("__msan_set_alloca_origin_no_descr",
904 IRB.getVoidTy(), PtrTy, IntptrTy, PtrTy);
905 MsanPoisonStackFn = M.getOrInsertFunction("__msan_poison_stack",
906 IRB.getVoidTy(), PtrTy, IntptrTy);
907}
908
909/// Insert extern declaration of runtime-provided functions and globals.
910void MemorySanitizer::initializeCallbacks(Module &M, const TargetLibraryInfo &TLI) {
911 // Only do this once.
912 if (CallbacksInitialized)
913 return;
914
915 IRBuilder<> IRB(*C);
916 // Initialize callbacks that are common for kernel and userspace
917 // instrumentation.
918 MsanChainOriginFn = M.getOrInsertFunction(
919 "__msan_chain_origin",
920 TLI.getAttrList(C, {0}, /*Signed=*/false, /*Ret=*/true), IRB.getInt32Ty(),
921 IRB.getInt32Ty());
922 MsanSetOriginFn = M.getOrInsertFunction(
923 "__msan_set_origin", TLI.getAttrList(C, {2}, /*Signed=*/false),
924 IRB.getVoidTy(), PtrTy, IntptrTy, IRB.getInt32Ty());
925 MemmoveFn =
926 M.getOrInsertFunction("__msan_memmove", PtrTy, PtrTy, PtrTy, IntptrTy);
927 MemcpyFn =
928 M.getOrInsertFunction("__msan_memcpy", PtrTy, PtrTy, PtrTy, IntptrTy);
929 MemsetFn = M.getOrInsertFunction("__msan_memset",
930 TLI.getAttrList(C, {1}, /*Signed=*/true),
931 PtrTy, PtrTy, IRB.getInt32Ty(), IntptrTy);
932
933 MsanInstrumentAsmStoreFn =
934 M.getOrInsertFunction("__msan_instrument_asm_store", IRB.getVoidTy(),
935 PointerType::get(IRB.getInt8Ty(), 0), IntptrTy);
936
937 if (CompileKernel) {
938 createKernelApi(M, TLI);
939 } else {
940 createUserspaceApi(M, TLI);
941 }
942 CallbacksInitialized = true;
943}
944
945FunctionCallee MemorySanitizer::getKmsanShadowOriginAccessFn(bool isStore,
946 int size) {
947 FunctionCallee *Fns =
948 isStore ? MsanMetadataPtrForStore_1_8 : MsanMetadataPtrForLoad_1_8;
949 switch (size) {
950 case 1:
951 return Fns[0];
952 case 2:
953 return Fns[1];
954 case 4:
955 return Fns[2];
956 case 8:
957 return Fns[3];
958 default:
959 return nullptr;
960 }
961}
962
963/// Module-level initialization.
964///
965/// inserts a call to __msan_init to the module's constructor list.
966void MemorySanitizer::initializeModule(Module &M) {
967 auto &DL = M.getDataLayout();
968
969 TargetTriple = Triple(M.getTargetTriple());
970
971 bool ShadowPassed = ClShadowBase.getNumOccurrences() > 0;
972 bool OriginPassed = ClOriginBase.getNumOccurrences() > 0;
973 // Check the overrides first
974 if (ShadowPassed || OriginPassed) {
975 CustomMapParams.AndMask = ClAndMask;
976 CustomMapParams.XorMask = ClXorMask;
977 CustomMapParams.ShadowBase = ClShadowBase;
978 CustomMapParams.OriginBase = ClOriginBase;
979 MapParams = &CustomMapParams;
980 } else {
981 switch (TargetTriple.getOS()) {
982 case Triple::FreeBSD:
983 switch (TargetTriple.getArch()) {
984 case Triple::aarch64:
985 MapParams = FreeBSD_ARM_MemoryMapParams.bits64;
986 break;
987 case Triple::x86_64:
988 MapParams = FreeBSD_X86_MemoryMapParams.bits64;
989 break;
990 case Triple::x86:
991 MapParams = FreeBSD_X86_MemoryMapParams.bits32;
992 break;
993 default:
994 report_fatal_error("unsupported architecture");
995 }
996 break;
997 case Triple::NetBSD:
998 switch (TargetTriple.getArch()) {
999 case Triple::x86_64:
1000 MapParams = NetBSD_X86_MemoryMapParams.bits64;
1001 break;
1002 default:
1003 report_fatal_error("unsupported architecture");
1004 }
1005 break;
1006 case Triple::Linux:
1007 switch (TargetTriple.getArch()) {
1008 case Triple::x86_64:
1009 MapParams = Linux_X86_MemoryMapParams.bits64;
1010 break;
1011 case Triple::x86:
1012 MapParams = Linux_X86_MemoryMapParams.bits32;
1013 break;
1014 case Triple::mips64:
1015 case Triple::mips64el:
1016 MapParams = Linux_MIPS_MemoryMapParams.bits64;
1017 break;
1018 case Triple::ppc64:
1019 case Triple::ppc64le:
1020 MapParams = Linux_PowerPC_MemoryMapParams.bits64;
1021 break;
1022 case Triple::systemz:
1023 MapParams = Linux_S390_MemoryMapParams.bits64;
1024 break;
1025 case Triple::aarch64:
1026 case Triple::aarch64_be:
1027 MapParams = Linux_ARM_MemoryMapParams.bits64;
1028 break;
1030 MapParams = Linux_LoongArch_MemoryMapParams.bits64;
1031 break;
1032 default:
1033 report_fatal_error("unsupported architecture");
1034 }
1035 break;
1036 default:
1037 report_fatal_error("unsupported operating system");
1038 }
1039 }
1040
1041 C = &(M.getContext());
1042 IRBuilder<> IRB(*C);
1043 IntptrTy = IRB.getIntPtrTy(DL);
1044 OriginTy = IRB.getInt32Ty();
1045 PtrTy = IRB.getPtrTy();
1046
1047 ColdCallWeights = MDBuilder(*C).createUnlikelyBranchWeights();
1048 OriginStoreWeights = MDBuilder(*C).createUnlikelyBranchWeights();
1049
1050 if (!CompileKernel) {
1051 if (TrackOrigins)
1052 M.getOrInsertGlobal("__msan_track_origins", IRB.getInt32Ty(), [&] {
1053 return new GlobalVariable(
1054 M, IRB.getInt32Ty(), true, GlobalValue::WeakODRLinkage,
1055 IRB.getInt32(TrackOrigins), "__msan_track_origins");
1056 });
1057
1058 if (Recover)
1059 M.getOrInsertGlobal("__msan_keep_going", IRB.getInt32Ty(), [&] {
1060 return new GlobalVariable(M, IRB.getInt32Ty(), true,
1061 GlobalValue::WeakODRLinkage,
1062 IRB.getInt32(Recover), "__msan_keep_going");
1063 });
1064 }
1065}
1066
1067namespace {
1068
1069/// A helper class that handles instrumentation of VarArg
1070/// functions on a particular platform.
1071///
1072/// Implementations are expected to insert the instrumentation
1073/// necessary to propagate argument shadow through VarArg function
1074/// calls. Visit* methods are called during an InstVisitor pass over
1075/// the function, and should avoid creating new basic blocks. A new
1076/// instance of this class is created for each instrumented function.
1077struct VarArgHelper {
1078 virtual ~VarArgHelper() = default;
1079
1080 /// Visit a CallBase.
1081 virtual void visitCallBase(CallBase &CB, IRBuilder<> &IRB) = 0;
1082
1083 /// Visit a va_start call.
1084 virtual void visitVAStartInst(VAStartInst &I) = 0;
1085
1086 /// Visit a va_copy call.
1087 virtual void visitVACopyInst(VACopyInst &I) = 0;
1088
1089 /// Finalize function instrumentation.
1090 ///
1091 /// This method is called after visiting all interesting (see above)
1092 /// instructions in a function.
1093 virtual void finalizeInstrumentation() = 0;
1094};
1095
1096struct MemorySanitizerVisitor;
1097
1098} // end anonymous namespace
1099
1100static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
1101 MemorySanitizerVisitor &Visitor);
1102
1103static unsigned TypeSizeToSizeIndex(TypeSize TS) {
1104 if (TS.isScalable())
1105 // Scalable types unconditionally take slowpaths.
1106 return kNumberOfAccessSizes;
1107 unsigned TypeSizeFixed = TS.getFixedValue();
1108 if (TypeSizeFixed <= 8)
1109 return 0;
1110 return Log2_32_Ceil((TypeSizeFixed + 7) / 8);
1111}
1112
1113namespace {
1114
1115/// Helper class to attach debug information of the given instruction onto new
1116/// instructions inserted after.
1117class NextNodeIRBuilder : public IRBuilder<> {
1118public:
1119 explicit NextNodeIRBuilder(Instruction *IP) : IRBuilder<>(IP->getNextNode()) {
1120 SetCurrentDebugLocation(IP->getDebugLoc());
1121 }
1122};
1123
1124/// This class does all the work for a given function. Store and Load
1125/// instructions store and load corresponding shadow and origin
1126/// values. Most instructions propagate shadow from arguments to their
1127/// return values. Certain instructions (most importantly, BranchInst)
1128/// test their argument shadow and print reports (with a runtime call) if it's
1129/// non-zero.
1130struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
1131 Function &F;
1132 MemorySanitizer &MS;
1133 SmallVector<PHINode *, 16> ShadowPHINodes, OriginPHINodes;
1134 ValueMap<Value *, Value *> ShadowMap, OriginMap;
1135 std::unique_ptr<VarArgHelper> VAHelper;
1136 const TargetLibraryInfo *TLI;
1137 Instruction *FnPrologueEnd;
1139
1140 // The following flags disable parts of MSan instrumentation based on
1141 // exclusion list contents and command-line options.
1142 bool InsertChecks;
1143 bool PropagateShadow;
1144 bool PoisonStack;
1145 bool PoisonUndef;
1146
1147 struct ShadowOriginAndInsertPoint {
1148 Value *Shadow;
1149 Value *Origin;
1150 Instruction *OrigIns;
1151
1152 ShadowOriginAndInsertPoint(Value *S, Value *O, Instruction *I)
1153 : Shadow(S), Origin(O), OrigIns(I) {}
1154 };
1156 DenseMap<const DILocation *, int> LazyWarningDebugLocationCount;
1157 bool InstrumentLifetimeStart = ClHandleLifetimeIntrinsics;
1161 int64_t SplittableBlocksCount = 0;
1162
1163 MemorySanitizerVisitor(Function &F, MemorySanitizer &MS,
1164 const TargetLibraryInfo &TLI)
1165 : F(F), MS(MS), VAHelper(CreateVarArgHelper(F, MS, *this)), TLI(&TLI) {
1166 bool SanitizeFunction =
1167 F.hasFnAttribute(Attribute::SanitizeMemory) && !ClDisableChecks;
1168 InsertChecks = SanitizeFunction;
1169 PropagateShadow = SanitizeFunction;
1170 PoisonStack = SanitizeFunction && ClPoisonStack;
1171 PoisonUndef = SanitizeFunction && ClPoisonUndef;
1172
1173 // In the presence of unreachable blocks, we may see Phi nodes with
1174 // incoming nodes from such blocks. Since InstVisitor skips unreachable
1175 // blocks, such nodes will not have any shadow value associated with them.
1176 // It's easier to remove unreachable blocks than deal with missing shadow.
1178
1179 MS.initializeCallbacks(*F.getParent(), TLI);
1180 FnPrologueEnd = IRBuilder<>(F.getEntryBlock().getFirstNonPHI())
1181 .CreateIntrinsic(Intrinsic::donothing, {}, {});
1182
1183 if (MS.CompileKernel) {
1184 IRBuilder<> IRB(FnPrologueEnd);
1185 insertKmsanPrologue(IRB);
1186 }
1187
1188 LLVM_DEBUG(if (!InsertChecks) dbgs()
1189 << "MemorySanitizer is not inserting checks into '"
1190 << F.getName() << "'\n");
1191 }
1192
1193 bool instrumentWithCalls(Value *V) {
1194 // Constants likely will be eliminated by follow-up passes.
1195 if (isa<Constant>(V))
1196 return false;
1197
1198 ++SplittableBlocksCount;
1200 SplittableBlocksCount > ClInstrumentationWithCallThreshold;
1201 }
1202
1203 bool isInPrologue(Instruction &I) {
1204 return I.getParent() == FnPrologueEnd->getParent() &&
1205 (&I == FnPrologueEnd || I.comesBefore(FnPrologueEnd));
1206 }
1207
1208 // Creates a new origin and records the stack trace. In general we can call
1209 // this function for any origin manipulation we like. However it will cost
1210 // runtime resources. So use this wisely only if it can provide additional
1211 // information helpful to a user.
1212 Value *updateOrigin(Value *V, IRBuilder<> &IRB) {
1213 if (MS.TrackOrigins <= 1)
1214 return V;
1215 return IRB.CreateCall(MS.MsanChainOriginFn, V);
1216 }
1217
1218 Value *originToIntptr(IRBuilder<> &IRB, Value *Origin) {
1219 const DataLayout &DL = F.getParent()->getDataLayout();
1220 unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
1221 if (IntptrSize == kOriginSize)
1222 return Origin;
1223 assert(IntptrSize == kOriginSize * 2);
1224 Origin = IRB.CreateIntCast(Origin, MS.IntptrTy, /* isSigned */ false);
1225 return IRB.CreateOr(Origin, IRB.CreateShl(Origin, kOriginSize * 8));
1226 }
1227
1228 /// Fill memory range with the given origin value.
1229 void paintOrigin(IRBuilder<> &IRB, Value *Origin, Value *OriginPtr,
1230 TypeSize TS, Align Alignment) {
1231 const DataLayout &DL = F.getParent()->getDataLayout();
1232 const Align IntptrAlignment = DL.getABITypeAlign(MS.IntptrTy);
1233 unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
1234 assert(IntptrAlignment >= kMinOriginAlignment);
1235 assert(IntptrSize >= kOriginSize);
1236
1237 // Note: The loop based formation works for fixed length vectors too,
1238 // however we prefer to unroll and specialize alignment below.
1239 if (TS.isScalable()) {
1240 Value *Size = IRB.CreateTypeSize(MS.IntptrTy, TS);
1241 Value *RoundUp =
1242 IRB.CreateAdd(Size, ConstantInt::get(MS.IntptrTy, kOriginSize - 1));
1243 Value *End =
1244 IRB.CreateUDiv(RoundUp, ConstantInt::get(MS.IntptrTy, kOriginSize));
1245 auto [InsertPt, Index] =
1247 IRB.SetInsertPoint(InsertPt);
1248
1249 Value *GEP = IRB.CreateGEP(MS.OriginTy, OriginPtr, Index);
1251 return;
1252 }
1253
1254 unsigned Size = TS.getFixedValue();
1255
1256 unsigned Ofs = 0;
1257 Align CurrentAlignment = Alignment;
1258 if (Alignment >= IntptrAlignment && IntptrSize > kOriginSize) {
1259 Value *IntptrOrigin = originToIntptr(IRB, Origin);
1260 Value *IntptrOriginPtr =
1261 IRB.CreatePointerCast(OriginPtr, PointerType::get(MS.IntptrTy, 0));
1262 for (unsigned i = 0; i < Size / IntptrSize; ++i) {
1263 Value *Ptr = i ? IRB.CreateConstGEP1_32(MS.IntptrTy, IntptrOriginPtr, i)
1264 : IntptrOriginPtr;
1265 IRB.CreateAlignedStore(IntptrOrigin, Ptr, CurrentAlignment);
1266 Ofs += IntptrSize / kOriginSize;
1267 CurrentAlignment = IntptrAlignment;
1268 }
1269 }
1270
1271 for (unsigned i = Ofs; i < (Size + kOriginSize - 1) / kOriginSize; ++i) {
1272 Value *GEP =
1273 i ? IRB.CreateConstGEP1_32(MS.OriginTy, OriginPtr, i) : OriginPtr;
1274 IRB.CreateAlignedStore(Origin, GEP, CurrentAlignment);
1275 CurrentAlignment = kMinOriginAlignment;
1276 }
1277 }
1278
1279 void storeOrigin(IRBuilder<> &IRB, Value *Addr, Value *Shadow, Value *Origin,
1280 Value *OriginPtr, Align Alignment) {
1281 const DataLayout &DL = F.getParent()->getDataLayout();
1282 const Align OriginAlignment = std::max(kMinOriginAlignment, Alignment);
1283 TypeSize StoreSize = DL.getTypeStoreSize(Shadow->getType());
1284 Value *ConvertedShadow = convertShadowToScalar(Shadow, IRB);
1285 if (auto *ConstantShadow = dyn_cast<Constant>(ConvertedShadow)) {
1286 if (!ClCheckConstantShadow || ConstantShadow->isZeroValue()) {
1287 // Origin is not needed: value is initialized or const shadow is
1288 // ignored.
1289 return;
1290 }
1291 if (llvm::isKnownNonZero(ConvertedShadow, DL)) {
1292 // Copy origin as the value is definitely uninitialized.
1293 paintOrigin(IRB, updateOrigin(Origin, IRB), OriginPtr, StoreSize,
1294 OriginAlignment);
1295 return;
1296 }
1297 // Fallback to runtime check, which still can be optimized out later.
1298 }
1299
1300 TypeSize TypeSizeInBits = DL.getTypeSizeInBits(ConvertedShadow->getType());
1301 unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
1302 if (instrumentWithCalls(ConvertedShadow) &&
1303 SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) {
1304 FunctionCallee Fn = MS.MaybeStoreOriginFn[SizeIndex];
1305 Value *ConvertedShadow2 =
1306 IRB.CreateZExt(ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
1307 CallBase *CB = IRB.CreateCall(Fn, {ConvertedShadow2, Addr, Origin});
1308 CB->addParamAttr(0, Attribute::ZExt);
1309 CB->addParamAttr(2, Attribute::ZExt);
1310 } else {
1311 Value *Cmp = convertToBool(ConvertedShadow, IRB, "_mscmp");
1313 Cmp, &*IRB.GetInsertPoint(), false, MS.OriginStoreWeights);
1314 IRBuilder<> IRBNew(CheckTerm);
1315 paintOrigin(IRBNew, updateOrigin(Origin, IRBNew), OriginPtr, StoreSize,
1316 OriginAlignment);
1317 }
1318 }
1319
1320 void materializeStores() {
1321 for (StoreInst *SI : StoreList) {
1322 IRBuilder<> IRB(SI);
1323 Value *Val = SI->getValueOperand();
1324 Value *Addr = SI->getPointerOperand();
1325 Value *Shadow = SI->isAtomic() ? getCleanShadow(Val) : getShadow(Val);
1326 Value *ShadowPtr, *OriginPtr;
1327 Type *ShadowTy = Shadow->getType();
1328 const Align Alignment = SI->getAlign();
1329 const Align OriginAlignment = std::max(kMinOriginAlignment, Alignment);
1330 std::tie(ShadowPtr, OriginPtr) =
1331 getShadowOriginPtr(Addr, IRB, ShadowTy, Alignment, /*isStore*/ true);
1332
1333 StoreInst *NewSI = IRB.CreateAlignedStore(Shadow, ShadowPtr, Alignment);
1334 LLVM_DEBUG(dbgs() << " STORE: " << *NewSI << "\n");
1335 (void)NewSI;
1336
1337 if (SI->isAtomic())
1338 SI->setOrdering(addReleaseOrdering(SI->getOrdering()));
1339
1340 if (MS.TrackOrigins && !SI->isAtomic())
1341 storeOrigin(IRB, Addr, Shadow, getOrigin(Val), OriginPtr,
1342 OriginAlignment);
1343 }
1344 }
1345
1346 // Returns true if Debug Location corresponds to multiple warnings.
1347 bool shouldDisambiguateWarningLocation(const DebugLoc &DebugLoc) {
1348 if (MS.TrackOrigins < 2)
1349 return false;
1350
1351 if (LazyWarningDebugLocationCount.empty())
1352 for (const auto &I : InstrumentationList)
1353 ++LazyWarningDebugLocationCount[I.OrigIns->getDebugLoc()];
1354
1355 return LazyWarningDebugLocationCount[DebugLoc] >= ClDisambiguateWarning;
1356 }
1357
1358 /// Helper function to insert a warning at IRB's current insert point.
1359 void insertWarningFn(IRBuilder<> &IRB, Value *Origin) {
1360 if (!Origin)
1361 Origin = (Value *)IRB.getInt32(0);
1362 assert(Origin->getType()->isIntegerTy());
1363
1364 if (shouldDisambiguateWarningLocation(IRB.getCurrentDebugLocation())) {
1365 // Try to create additional origin with debug info of the last origin
1366 // instruction. It may provide additional information to the user.
1367 if (Instruction *OI = dyn_cast_or_null<Instruction>(Origin)) {
1368 assert(MS.TrackOrigins);
1369 auto NewDebugLoc = OI->getDebugLoc();
1370 // Origin update with missing or the same debug location provides no
1371 // additional value.
1372 if (NewDebugLoc && NewDebugLoc != IRB.getCurrentDebugLocation()) {
1373 // Insert update just before the check, so we call runtime only just
1374 // before the report.
1375 IRBuilder<> IRBOrigin(&*IRB.GetInsertPoint());
1376 IRBOrigin.SetCurrentDebugLocation(NewDebugLoc);
1377 Origin = updateOrigin(Origin, IRBOrigin);
1378 }
1379 }
1380 }
1381
1382 if (MS.CompileKernel || MS.TrackOrigins)
1383 IRB.CreateCall(MS.WarningFn, Origin)->setCannotMerge();
1384 else
1385 IRB.CreateCall(MS.WarningFn)->setCannotMerge();
1386 // FIXME: Insert UnreachableInst if !MS.Recover?
1387 // This may invalidate some of the following checks and needs to be done
1388 // at the very end.
1389 }
1390
1391 void materializeOneCheck(IRBuilder<> &IRB, Value *ConvertedShadow,
1392 Value *Origin) {
1393 const DataLayout &DL = F.getParent()->getDataLayout();
1394 TypeSize TypeSizeInBits = DL.getTypeSizeInBits(ConvertedShadow->getType());
1395 unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
1396 if (instrumentWithCalls(ConvertedShadow) &&
1397 SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) {
1398 FunctionCallee Fn = MS.MaybeWarningFn[SizeIndex];
1399 Value *ConvertedShadow2 =
1400 IRB.CreateZExt(ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
1401 CallBase *CB = IRB.CreateCall(
1402 Fn, {ConvertedShadow2,
1403 MS.TrackOrigins && Origin ? Origin : (Value *)IRB.getInt32(0)});
1404 CB->addParamAttr(0, Attribute::ZExt);
1405 CB->addParamAttr(1, Attribute::ZExt);
1406 } else {
1407 Value *Cmp = convertToBool(ConvertedShadow, IRB, "_mscmp");
1409 Cmp, &*IRB.GetInsertPoint(),
1410 /* Unreachable */ !MS.Recover, MS.ColdCallWeights);
1411
1412 IRB.SetInsertPoint(CheckTerm);
1413 insertWarningFn(IRB, Origin);
1414 LLVM_DEBUG(dbgs() << " CHECK: " << *Cmp << "\n");
1415 }
1416 }
1417
1418 void materializeInstructionChecks(
1419 ArrayRef<ShadowOriginAndInsertPoint> InstructionChecks) {
1420 const DataLayout &DL = F.getParent()->getDataLayout();
1421 // Disable combining in some cases. TrackOrigins checks each shadow to pick
1422 // correct origin.
1423 bool Combine = !MS.TrackOrigins;
1424 Instruction *Instruction = InstructionChecks.front().OrigIns;
1425 Value *Shadow = nullptr;
1426 for (const auto &ShadowData : InstructionChecks) {
1427 assert(ShadowData.OrigIns == Instruction);
1429
1430 Value *ConvertedShadow = ShadowData.Shadow;
1431
1432 if (auto *ConstantShadow = dyn_cast<Constant>(ConvertedShadow)) {
1433 if (!ClCheckConstantShadow || ConstantShadow->isZeroValue()) {
1434 // Skip, value is initialized or const shadow is ignored.
1435 continue;
1436 }
1437 if (llvm::isKnownNonZero(ConvertedShadow, DL)) {
1438 // Report as the value is definitely uninitialized.
1439 insertWarningFn(IRB, ShadowData.Origin);
1440 if (!MS.Recover)
1441 return; // Always fail and stop here, not need to check the rest.
1442 // Skip entire instruction,
1443 continue;
1444 }
1445 // Fallback to runtime check, which still can be optimized out later.
1446 }
1447
1448 if (!Combine) {
1449 materializeOneCheck(IRB, ConvertedShadow, ShadowData.Origin);
1450 continue;
1451 }
1452
1453 if (!Shadow) {
1454 Shadow = ConvertedShadow;
1455 continue;
1456 }
1457
1458 Shadow = convertToBool(Shadow, IRB, "_mscmp");
1459 ConvertedShadow = convertToBool(ConvertedShadow, IRB, "_mscmp");
1460 Shadow = IRB.CreateOr(Shadow, ConvertedShadow, "_msor");
1461 }
1462
1463 if (Shadow) {
1464 assert(Combine);
1466 materializeOneCheck(IRB, Shadow, nullptr);
1467 }
1468 }
1469
1470 void materializeChecks() {
1471#ifndef NDEBUG
1472 // For assert below.
1474#endif
1475
1476 for (auto I = InstrumentationList.begin();
1477 I != InstrumentationList.end();) {
1478 auto OrigIns = I->OrigIns;
1479 // Checks are grouped by the original instruction. We call all
1480 // `insertShadowCheck` for an instruction at once.
1481 assert(Done.insert(OrigIns).second);
1482 auto J = std::find_if(I + 1, InstrumentationList.end(),
1483 [OrigIns](const ShadowOriginAndInsertPoint &R) {
1484 return OrigIns != R.OrigIns;
1485 });
1486 // Process all checks of instruction at once.
1487 materializeInstructionChecks(ArrayRef<ShadowOriginAndInsertPoint>(I, J));
1488 I = J;
1489 }
1490
1491 LLVM_DEBUG(dbgs() << "DONE:\n" << F);
1492 }
1493
1494 // Returns the last instruction in the new prologue
1495 void insertKmsanPrologue(IRBuilder<> &IRB) {
1496 Value *ContextState = IRB.CreateCall(MS.MsanGetContextStateFn, {});
1497 Constant *Zero = IRB.getInt32(0);
1498 MS.ParamTLS = IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
1499 {Zero, IRB.getInt32(0)}, "param_shadow");
1500 MS.RetvalTLS = IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
1501 {Zero, IRB.getInt32(1)}, "retval_shadow");
1502 MS.VAArgTLS = IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
1503 {Zero, IRB.getInt32(2)}, "va_arg_shadow");
1504 MS.VAArgOriginTLS = IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
1505 {Zero, IRB.getInt32(3)}, "va_arg_origin");
1506 MS.VAArgOverflowSizeTLS =
1507 IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
1508 {Zero, IRB.getInt32(4)}, "va_arg_overflow_size");
1509 MS.ParamOriginTLS = IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
1510 {Zero, IRB.getInt32(5)}, "param_origin");
1511 MS.RetvalOriginTLS =
1512 IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
1513 {Zero, IRB.getInt32(6)}, "retval_origin");
1514 if (MS.TargetTriple.getArch() == Triple::systemz)
1515 MS.MsanMetadataAlloca = IRB.CreateAlloca(MS.MsanMetadata, 0u);
1516 }
1517
1518 /// Add MemorySanitizer instrumentation to a function.
1519 bool runOnFunction() {
1520 // Iterate all BBs in depth-first order and create shadow instructions
1521 // for all instructions (where applicable).
1522 // For PHI nodes we create dummy shadow PHIs which will be finalized later.
1523 for (BasicBlock *BB : depth_first(FnPrologueEnd->getParent()))
1524 visit(*BB);
1525
1526 // `visit` above only collects instructions. Process them after iterating
1527 // CFG to avoid requirement on CFG transformations.
1528 for (Instruction *I : Instructions)
1530
1531 // Finalize PHI nodes.
1532 for (PHINode *PN : ShadowPHINodes) {
1533 PHINode *PNS = cast<PHINode>(getShadow(PN));
1534 PHINode *PNO = MS.TrackOrigins ? cast<PHINode>(getOrigin(PN)) : nullptr;
1535 size_t NumValues = PN->getNumIncomingValues();
1536 for (size_t v = 0; v < NumValues; v++) {
1537 PNS->addIncoming(getShadow(PN, v), PN->getIncomingBlock(v));
1538 if (PNO)
1539 PNO->addIncoming(getOrigin(PN, v), PN->getIncomingBlock(v));
1540 }
1541 }
1542
1543 VAHelper->finalizeInstrumentation();
1544
1545 // Poison llvm.lifetime.start intrinsics, if we haven't fallen back to
1546 // instrumenting only allocas.
1547 if (InstrumentLifetimeStart) {
1548 for (auto Item : LifetimeStartList) {
1549 instrumentAlloca(*Item.second, Item.first);
1550 AllocaSet.remove(Item.second);
1551 }
1552 }
1553 // Poison the allocas for which we didn't instrument the corresponding
1554 // lifetime intrinsics.
1555 for (AllocaInst *AI : AllocaSet)
1556 instrumentAlloca(*AI);
1557
1558 // Insert shadow value checks.
1559 materializeChecks();
1560
1561 // Delayed instrumentation of StoreInst.
1562 // This may not add new address checks.
1563 materializeStores();
1564
1565 return true;
1566 }
1567
1568 /// Compute the shadow type that corresponds to a given Value.
1569 Type *getShadowTy(Value *V) { return getShadowTy(V->getType()); }
1570
1571 /// Compute the shadow type that corresponds to a given Type.
1572 Type *getShadowTy(Type *OrigTy) {
1573 if (!OrigTy->isSized()) {
1574 return nullptr;
1575 }
1576 // For integer type, shadow is the same as the original type.
1577 // This may return weird-sized types like i1.
1578 if (IntegerType *IT = dyn_cast<IntegerType>(OrigTy))
1579 return IT;
1580 const DataLayout &DL = F.getParent()->getDataLayout();
1581 if (VectorType *VT = dyn_cast<VectorType>(OrigTy)) {
1582 uint32_t EltSize = DL.getTypeSizeInBits(VT->getElementType());
1583 return VectorType::get(IntegerType::get(*MS.C, EltSize),
1584 VT->getElementCount());
1585 }
1586 if (ArrayType *AT = dyn_cast<ArrayType>(OrigTy)) {
1587 return ArrayType::get(getShadowTy(AT->getElementType()),
1588 AT->getNumElements());
1589 }
1590 if (StructType *ST = dyn_cast<StructType>(OrigTy)) {
1592 for (unsigned i = 0, n = ST->getNumElements(); i < n; i++)
1593 Elements.push_back(getShadowTy(ST->getElementType(i)));
1594 StructType *Res = StructType::get(*MS.C, Elements, ST->isPacked());
1595 LLVM_DEBUG(dbgs() << "getShadowTy: " << *ST << " ===> " << *Res << "\n");
1596 return Res;
1597 }
1598 uint32_t TypeSize = DL.getTypeSizeInBits(OrigTy);
1599 return IntegerType::get(*MS.C, TypeSize);
1600 }
1601
1602 /// Extract combined shadow of struct elements as a bool
1603 Value *collapseStructShadow(StructType *Struct, Value *Shadow,
1604 IRBuilder<> &IRB) {
1605 Value *FalseVal = IRB.getIntN(/* width */ 1, /* value */ 0);
1606 Value *Aggregator = FalseVal;
1607
1608 for (unsigned Idx = 0; Idx < Struct->getNumElements(); Idx++) {
1609 // Combine by ORing together each element's bool shadow
1610 Value *ShadowItem = IRB.CreateExtractValue(Shadow, Idx);
1611 Value *ShadowBool = convertToBool(ShadowItem, IRB);
1612
1613 if (Aggregator != FalseVal)
1614 Aggregator = IRB.CreateOr(Aggregator, ShadowBool);
1615 else
1616 Aggregator = ShadowBool;
1617 }
1618
1619 return Aggregator;
1620 }
1621
1622 // Extract combined shadow of array elements
1623 Value *collapseArrayShadow(ArrayType *Array, Value *Shadow,
1624 IRBuilder<> &IRB) {
1625 if (!Array->getNumElements())
1626 return IRB.getIntN(/* width */ 1, /* value */ 0);
1627
1628 Value *FirstItem = IRB.CreateExtractValue(Shadow, 0);
1629 Value *Aggregator = convertShadowToScalar(FirstItem, IRB);
1630
1631 for (unsigned Idx = 1; Idx < Array->getNumElements(); Idx++) {
1632 Value *ShadowItem = IRB.CreateExtractValue(Shadow, Idx);
1633 Value *ShadowInner = convertShadowToScalar(ShadowItem, IRB);
1634 Aggregator = IRB.CreateOr(Aggregator, ShadowInner);
1635 }
1636 return Aggregator;
1637 }
1638
1639 /// Convert a shadow value to it's flattened variant. The resulting
1640 /// shadow may not necessarily have the same bit width as the input
1641 /// value, but it will always be comparable to zero.
1642 Value *convertShadowToScalar(Value *V, IRBuilder<> &IRB) {
1643 if (StructType *Struct = dyn_cast<StructType>(V->getType()))
1644 return collapseStructShadow(Struct, V, IRB);
1645 if (ArrayType *Array = dyn_cast<ArrayType>(V->getType()))
1646 return collapseArrayShadow(Array, V, IRB);
1647 if (isa<VectorType>(V->getType())) {
1648 if (isa<ScalableVectorType>(V->getType()))
1649 return convertShadowToScalar(IRB.CreateOrReduce(V), IRB);
1650 unsigned BitWidth =
1651 V->getType()->getPrimitiveSizeInBits().getFixedValue();
1652 return IRB.CreateBitCast(V, IntegerType::get(*MS.C, BitWidth));
1653 }
1654 return V;
1655 }
1656
1657 // Convert a scalar value to an i1 by comparing with 0
1658 Value *convertToBool(Value *V, IRBuilder<> &IRB, const Twine &name = "") {
1659 Type *VTy = V->getType();
1660 if (!VTy->isIntegerTy())
1661 return convertToBool(convertShadowToScalar(V, IRB), IRB, name);
1662 if (VTy->getIntegerBitWidth() == 1)
1663 // Just converting a bool to a bool, so do nothing.
1664 return V;
1665 return IRB.CreateICmpNE(V, ConstantInt::get(VTy, 0), name);
1666 }
1667
1668 Type *ptrToIntPtrType(Type *PtrTy) const {
1669 if (VectorType *VectTy = dyn_cast<VectorType>(PtrTy)) {
1670 return VectorType::get(ptrToIntPtrType(VectTy->getElementType()),
1671 VectTy->getElementCount());
1672 }
1673 assert(PtrTy->isIntOrPtrTy());
1674 return MS.IntptrTy;
1675 }
1676
1677 Type *getPtrToShadowPtrType(Type *IntPtrTy, Type *ShadowTy) const {
1678 if (VectorType *VectTy = dyn_cast<VectorType>(IntPtrTy)) {
1679 return VectorType::get(
1680 getPtrToShadowPtrType(VectTy->getElementType(), ShadowTy),
1681 VectTy->getElementCount());
1682 }
1683 assert(IntPtrTy == MS.IntptrTy);
1684 return PointerType::get(*MS.C, 0);
1685 }
1686
1687 Constant *constToIntPtr(Type *IntPtrTy, uint64_t C) const {
1688 if (VectorType *VectTy = dyn_cast<VectorType>(IntPtrTy)) {
1690 VectTy->getElementCount(), constToIntPtr(VectTy->getElementType(), C));
1691 }
1692 assert(IntPtrTy == MS.IntptrTy);
1693 return ConstantInt::get(MS.IntptrTy, C);
1694 }
1695
1696 /// Compute the integer shadow offset that corresponds to a given
1697 /// application address.
1698 ///
1699 /// Offset = (Addr & ~AndMask) ^ XorMask
1700 /// Addr can be a ptr or <N x ptr>. In both cases ShadowTy the shadow type of
1701 /// a single pointee.
1702 /// Returns <shadow_ptr, origin_ptr> or <<N x shadow_ptr>, <N x origin_ptr>>.
1703 Value *getShadowPtrOffset(Value *Addr, IRBuilder<> &IRB) {
1704 Type *IntptrTy = ptrToIntPtrType(Addr->getType());
1705 Value *OffsetLong = IRB.CreatePointerCast(Addr, IntptrTy);
1706
1707 if (uint64_t AndMask = MS.MapParams->AndMask)
1708 OffsetLong = IRB.CreateAnd(OffsetLong, constToIntPtr(IntptrTy, ~AndMask));
1709
1710 if (uint64_t XorMask = MS.MapParams->XorMask)
1711 OffsetLong = IRB.CreateXor(OffsetLong, constToIntPtr(IntptrTy, XorMask));
1712 return OffsetLong;
1713 }
1714
1715 /// Compute the shadow and origin addresses corresponding to a given
1716 /// application address.
1717 ///
1718 /// Shadow = ShadowBase + Offset
1719 /// Origin = (OriginBase + Offset) & ~3ULL
1720 /// Addr can be a ptr or <N x ptr>. In both cases ShadowTy the shadow type of
1721 /// a single pointee.
1722 /// Returns <shadow_ptr, origin_ptr> or <<N x shadow_ptr>, <N x origin_ptr>>.
1723 std::pair<Value *, Value *>
1724 getShadowOriginPtrUserspace(Value *Addr, IRBuilder<> &IRB, Type *ShadowTy,
1725 MaybeAlign Alignment) {
1726 VectorType *VectTy = dyn_cast<VectorType>(Addr->getType());
1727 if (!VectTy) {
1728 assert(Addr->getType()->isPointerTy());
1729 } else {
1730 assert(VectTy->getElementType()->isPointerTy());
1731 }
1732 Type *IntptrTy = ptrToIntPtrType(Addr->getType());
1733 Value *ShadowOffset = getShadowPtrOffset(Addr, IRB);
1734 Value *ShadowLong = ShadowOffset;
1735 if (uint64_t ShadowBase = MS.MapParams->ShadowBase) {
1736 ShadowLong =
1737 IRB.CreateAdd(ShadowLong, constToIntPtr(IntptrTy, ShadowBase));
1738 }
1739 Value *ShadowPtr = IRB.CreateIntToPtr(
1740 ShadowLong, getPtrToShadowPtrType(IntptrTy, ShadowTy));
1741
1742 Value *OriginPtr = nullptr;
1743 if (MS.TrackOrigins) {
1744 Value *OriginLong = ShadowOffset;
1745 uint64_t OriginBase = MS.MapParams->OriginBase;
1746 if (OriginBase != 0)
1747 OriginLong =
1748 IRB.CreateAdd(OriginLong, constToIntPtr(IntptrTy, OriginBase));
1749 if (!Alignment || *Alignment < kMinOriginAlignment) {
1751 OriginLong = IRB.CreateAnd(OriginLong, constToIntPtr(IntptrTy, ~Mask));
1752 }
1753 OriginPtr = IRB.CreateIntToPtr(
1754 OriginLong, getPtrToShadowPtrType(IntptrTy, MS.OriginTy));
1755 }
1756 return std::make_pair(ShadowPtr, OriginPtr);
1757 }
1758
1759 template <typename... ArgsTy>
1760 Value *createMetadataCall(IRBuilder<> &IRB, FunctionCallee Callee,
1761 ArgsTy... Args) {
1762 if (MS.TargetTriple.getArch() == Triple::systemz) {
1763 IRB.CreateCall(Callee,
1764 {MS.MsanMetadataAlloca, std::forward<ArgsTy>(Args)...});
1765 return IRB.CreateLoad(MS.MsanMetadata, MS.MsanMetadataAlloca);
1766 }
1767
1768 return IRB.CreateCall(Callee, {std::forward<ArgsTy>(Args)...});
1769 }
1770
1771 std::pair<Value *, Value *> getShadowOriginPtrKernelNoVec(Value *Addr,
1772 IRBuilder<> &IRB,
1773 Type *ShadowTy,
1774 bool isStore) {
1775 Value *ShadowOriginPtrs;
1776 const DataLayout &DL = F.getParent()->getDataLayout();
1777 TypeSize Size = DL.getTypeStoreSize(ShadowTy);
1778
1779 FunctionCallee Getter = MS.getKmsanShadowOriginAccessFn(isStore, Size);
1780 Value *AddrCast =
1781 IRB.CreatePointerCast(Addr, PointerType::get(IRB.getInt8Ty(), 0));
1782 if (Getter) {
1783 ShadowOriginPtrs = createMetadataCall(IRB, Getter, AddrCast);
1784 } else {
1785 Value *SizeVal = ConstantInt::get(MS.IntptrTy, Size);
1786 ShadowOriginPtrs = createMetadataCall(
1787 IRB,
1788 isStore ? MS.MsanMetadataPtrForStoreN : MS.MsanMetadataPtrForLoadN,
1789 AddrCast, SizeVal);
1790 }
1791 Value *ShadowPtr = IRB.CreateExtractValue(ShadowOriginPtrs, 0);
1792 ShadowPtr = IRB.CreatePointerCast(ShadowPtr, PointerType::get(ShadowTy, 0));
1793 Value *OriginPtr = IRB.CreateExtractValue(ShadowOriginPtrs, 1);
1794
1795 return std::make_pair(ShadowPtr, OriginPtr);
1796 }
1797
1798 /// Addr can be a ptr or <N x ptr>. In both cases ShadowTy the shadow type of
1799 /// a single pointee.
1800 /// Returns <shadow_ptr, origin_ptr> or <<N x shadow_ptr>, <N x origin_ptr>>.
1801 std::pair<Value *, Value *> getShadowOriginPtrKernel(Value *Addr,
1802 IRBuilder<> &IRB,
1803 Type *ShadowTy,
1804 bool isStore) {
1805 VectorType *VectTy = dyn_cast<VectorType>(Addr->getType());
1806 if (!VectTy) {
1807 assert(Addr->getType()->isPointerTy());
1808 return getShadowOriginPtrKernelNoVec(Addr, IRB, ShadowTy, isStore);
1809 }
1810
1811 // TODO: Support callbacs with vectors of addresses.
1812 unsigned NumElements = cast<FixedVectorType>(VectTy)->getNumElements();
1813 Value *ShadowPtrs = ConstantInt::getNullValue(
1814 FixedVectorType::get(IRB.getPtrTy(), NumElements));
1815 Value *OriginPtrs = nullptr;
1816 if (MS.TrackOrigins)
1817 OriginPtrs = ConstantInt::getNullValue(
1818 FixedVectorType::get(IRB.getPtrTy(), NumElements));
1819 for (unsigned i = 0; i < NumElements; ++i) {
1820 Value *OneAddr =
1821 IRB.CreateExtractElement(Addr, ConstantInt::get(IRB.getInt32Ty(), i));
1822 auto [ShadowPtr, OriginPtr] =
1823 getShadowOriginPtrKernelNoVec(OneAddr, IRB, ShadowTy, isStore);
1824
1825 ShadowPtrs = IRB.CreateInsertElement(
1826 ShadowPtrs, ShadowPtr, ConstantInt::get(IRB.getInt32Ty(), i));
1827 if (MS.TrackOrigins)
1828 OriginPtrs = IRB.CreateInsertElement(
1829 OriginPtrs, OriginPtr, ConstantInt::get(IRB.getInt32Ty(), i));
1830 }
1831 return {ShadowPtrs, OriginPtrs};
1832 }
1833
1834 std::pair<Value *, Value *> getShadowOriginPtr(Value *Addr, IRBuilder<> &IRB,
1835 Type *ShadowTy,
1836 MaybeAlign Alignment,
1837 bool isStore) {
1838 if (MS.CompileKernel)
1839 return getShadowOriginPtrKernel(Addr, IRB, ShadowTy, isStore);
1840 return getShadowOriginPtrUserspace(Addr, IRB, ShadowTy, Alignment);
1841 }
1842
1843 /// Compute the shadow address for a given function argument.
1844 ///
1845 /// Shadow = ParamTLS+ArgOffset.
1846 Value *getShadowPtrForArgument(IRBuilder<> &IRB, int ArgOffset) {
1847 Value *Base = IRB.CreatePointerCast(MS.ParamTLS, MS.IntptrTy);
1848 if (ArgOffset)
1849 Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
1850 return IRB.CreateIntToPtr(Base, IRB.getPtrTy(0), "_msarg");
1851 }
1852
1853 /// Compute the origin address for a given function argument.
1854 Value *getOriginPtrForArgument(IRBuilder<> &IRB, int ArgOffset) {
1855 if (!MS.TrackOrigins)
1856 return nullptr;
1857 Value *Base = IRB.CreatePointerCast(MS.ParamOriginTLS, MS.IntptrTy);
1858 if (ArgOffset)
1859 Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
1860 return IRB.CreateIntToPtr(Base, IRB.getPtrTy(0), "_msarg_o");
1861 }
1862
1863 /// Compute the shadow address for a retval.
1864 Value *getShadowPtrForRetval(IRBuilder<> &IRB) {
1865 return IRB.CreatePointerCast(MS.RetvalTLS, IRB.getPtrTy(0), "_msret");
1866 }
1867
1868 /// Compute the origin address for a retval.
1869 Value *getOriginPtrForRetval() {
1870 // We keep a single origin for the entire retval. Might be too optimistic.
1871 return MS.RetvalOriginTLS;
1872 }
1873
1874 /// Set SV to be the shadow value for V.
1875 void setShadow(Value *V, Value *SV) {
1876 assert(!ShadowMap.count(V) && "Values may only have one shadow");
1877 ShadowMap[V] = PropagateShadow ? SV : getCleanShadow(V);
1878 }
1879
1880 /// Set Origin to be the origin value for V.
1881 void setOrigin(Value *V, Value *Origin) {
1882 if (!MS.TrackOrigins)
1883 return;
1884 assert(!OriginMap.count(V) && "Values may only have one origin");
1885 LLVM_DEBUG(dbgs() << "ORIGIN: " << *V << " ==> " << *Origin << "\n");
1886 OriginMap[V] = Origin;
1887 }
1888
1889 Constant *getCleanShadow(Type *OrigTy) {
1890 Type *ShadowTy = getShadowTy(OrigTy);
1891 if (!ShadowTy)
1892 return nullptr;
1893 return Constant::getNullValue(ShadowTy);
1894 }
1895
1896 /// Create a clean shadow value for a given value.
1897 ///
1898 /// Clean shadow (all zeroes) means all bits of the value are defined
1899 /// (initialized).
1900 Constant *getCleanShadow(Value *V) { return getCleanShadow(V->getType()); }
1901
1902 /// Create a dirty shadow of a given shadow type.
1903 Constant *getPoisonedShadow(Type *ShadowTy) {
1904 assert(ShadowTy);
1905 if (isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy))
1906 return Constant::getAllOnesValue(ShadowTy);
1907 if (ArrayType *AT = dyn_cast<ArrayType>(ShadowTy)) {
1908 SmallVector<Constant *, 4> Vals(AT->getNumElements(),
1909 getPoisonedShadow(AT->getElementType()));
1910 return ConstantArray::get(AT, Vals);
1911 }
1912 if (StructType *ST = dyn_cast<StructType>(ShadowTy)) {
1914 for (unsigned i = 0, n = ST->getNumElements(); i < n; i++)
1915 Vals.push_back(getPoisonedShadow(ST->getElementType(i)));
1916 return ConstantStruct::get(ST, Vals);
1917 }
1918 llvm_unreachable("Unexpected shadow type");
1919 }
1920
1921 /// Create a dirty shadow for a given value.
1922 Constant *getPoisonedShadow(Value *V) {
1923 Type *ShadowTy = getShadowTy(V);
1924 if (!ShadowTy)
1925 return nullptr;
1926 return getPoisonedShadow(ShadowTy);
1927 }
1928
1929 /// Create a clean (zero) origin.
1930 Value *getCleanOrigin() { return Constant::getNullValue(MS.OriginTy); }
1931
1932 /// Get the shadow value for a given Value.
1933 ///
1934 /// This function either returns the value set earlier with setShadow,
1935 /// or extracts if from ParamTLS (for function arguments).
1936 Value *getShadow(Value *V) {
1937 if (Instruction *I = dyn_cast<Instruction>(V)) {
1938 if (!PropagateShadow || I->getMetadata(LLVMContext::MD_nosanitize))
1939 return getCleanShadow(V);
1940 // For instructions the shadow is already stored in the map.
1941 Value *Shadow = ShadowMap[V];
1942 if (!Shadow) {
1943 LLVM_DEBUG(dbgs() << "No shadow: " << *V << "\n" << *(I->getParent()));
1944 (void)I;
1945 assert(Shadow && "No shadow for a value");
1946 }
1947 return Shadow;
1948 }
1949 if (UndefValue *U = dyn_cast<UndefValue>(V)) {
1950 Value *AllOnes = (PropagateShadow && PoisonUndef) ? getPoisonedShadow(V)
1951 : getCleanShadow(V);
1952 LLVM_DEBUG(dbgs() << "Undef: " << *U << " ==> " << *AllOnes << "\n");
1953 (void)U;
1954 return AllOnes;
1955 }
1956 if (Argument *A = dyn_cast<Argument>(V)) {
1957 // For arguments we compute the shadow on demand and store it in the map.
1958 Value *&ShadowPtr = ShadowMap[V];
1959 if (ShadowPtr)
1960 return ShadowPtr;
1961 Function *F = A->getParent();
1962 IRBuilder<> EntryIRB(FnPrologueEnd);
1963 unsigned ArgOffset = 0;
1964 const DataLayout &DL = F->getParent()->getDataLayout();
1965 for (auto &FArg : F->args()) {
1966 if (!FArg.getType()->isSized() || FArg.getType()->isScalableTy()) {
1967 LLVM_DEBUG(dbgs() << (FArg.getType()->isScalableTy()
1968 ? "vscale not fully supported\n"
1969 : "Arg is not sized\n"));
1970 if (A == &FArg) {
1971 ShadowPtr = getCleanShadow(V);
1972 setOrigin(A, getCleanOrigin());
1973 break;
1974 }
1975 continue;
1976 }
1977
1978 unsigned Size = FArg.hasByValAttr()
1979 ? DL.getTypeAllocSize(FArg.getParamByValType())
1980 : DL.getTypeAllocSize(FArg.getType());
1981
1982 if (A == &FArg) {
1983 bool Overflow = ArgOffset + Size > kParamTLSSize;
1984 if (FArg.hasByValAttr()) {
1985 // ByVal pointer itself has clean shadow. We copy the actual
1986 // argument shadow to the underlying memory.
1987 // Figure out maximal valid memcpy alignment.
1988 const Align ArgAlign = DL.getValueOrABITypeAlignment(
1989 FArg.getParamAlign(), FArg.getParamByValType());
1990 Value *CpShadowPtr, *CpOriginPtr;
1991 std::tie(CpShadowPtr, CpOriginPtr) =
1992 getShadowOriginPtr(V, EntryIRB, EntryIRB.getInt8Ty(), ArgAlign,
1993 /*isStore*/ true);
1994 if (!PropagateShadow || Overflow) {
1995 // ParamTLS overflow.
1996 EntryIRB.CreateMemSet(
1997 CpShadowPtr, Constant::getNullValue(EntryIRB.getInt8Ty()),
1998 Size, ArgAlign);
1999 } else {
2000 Value *Base = getShadowPtrForArgument(EntryIRB, ArgOffset);
2001 const Align CopyAlign = std::min(ArgAlign, kShadowTLSAlignment);
2002 Value *Cpy = EntryIRB.CreateMemCpy(CpShadowPtr, CopyAlign, Base,
2003 CopyAlign, Size);
2004 LLVM_DEBUG(dbgs() << " ByValCpy: " << *Cpy << "\n");
2005 (void)Cpy;
2006
2007 if (MS.TrackOrigins) {
2008 Value *OriginPtr =
2009 getOriginPtrForArgument(EntryIRB, ArgOffset);
2010 // FIXME: OriginSize should be:
2011 // alignTo(V % kMinOriginAlignment + Size, kMinOriginAlignment)
2012 unsigned OriginSize = alignTo(Size, kMinOriginAlignment);
2013 EntryIRB.CreateMemCpy(
2014 CpOriginPtr,
2015 /* by getShadowOriginPtr */ kMinOriginAlignment, OriginPtr,
2016 /* by origin_tls[ArgOffset] */ kMinOriginAlignment,
2017 OriginSize);
2018 }
2019 }
2020 }
2021
2022 if (!PropagateShadow || Overflow || FArg.hasByValAttr() ||
2023 (MS.EagerChecks && FArg.hasAttribute(Attribute::NoUndef))) {
2024 ShadowPtr = getCleanShadow(V);
2025 setOrigin(A, getCleanOrigin());
2026 } else {
2027 // Shadow over TLS
2028 Value *Base = getShadowPtrForArgument(EntryIRB, ArgOffset);
2029 ShadowPtr = EntryIRB.CreateAlignedLoad(getShadowTy(&FArg), Base,
2031 if (MS.TrackOrigins) {
2032 Value *OriginPtr =
2033 getOriginPtrForArgument(EntryIRB, ArgOffset);
2034 setOrigin(A, EntryIRB.CreateLoad(MS.OriginTy, OriginPtr));
2035 }
2036 }
2038 << " ARG: " << FArg << " ==> " << *ShadowPtr << "\n");
2039 break;
2040 }
2041
2042 ArgOffset += alignTo(Size, kShadowTLSAlignment);
2043 }
2044 assert(ShadowPtr && "Could not find shadow for an argument");
2045 return ShadowPtr;
2046 }
2047 // For everything else the shadow is zero.
2048 return getCleanShadow(V);
2049 }
2050
2051 /// Get the shadow for i-th argument of the instruction I.
2052 Value *getShadow(Instruction *I, int i) {
2053 return getShadow(I->getOperand(i));
2054 }
2055
2056 /// Get the origin for a value.
2057 Value *getOrigin(Value *V) {
2058 if (!MS.TrackOrigins)
2059 return nullptr;
2060 if (!PropagateShadow || isa<Constant>(V) || isa<InlineAsm>(V))
2061 return getCleanOrigin();
2062 assert((isa<Instruction>(V) || isa<Argument>(V)) &&
2063 "Unexpected value type in getOrigin()");
2064 if (Instruction *I = dyn_cast<Instruction>(V)) {
2065 if (I->getMetadata(LLVMContext::MD_nosanitize))
2066 return getCleanOrigin();
2067 }
2068 Value *Origin = OriginMap[V];
2069 assert(Origin && "Missing origin");
2070 return Origin;
2071 }
2072
2073 /// Get the origin for i-th argument of the instruction I.
2074 Value *getOrigin(Instruction *I, int i) {
2075 return getOrigin(I->getOperand(i));
2076 }
2077
2078 /// Remember the place where a shadow check should be inserted.
2079 ///
2080 /// This location will be later instrumented with a check that will print a
2081 /// UMR warning in runtime if the shadow value is not 0.
2082 void insertShadowCheck(Value *Shadow, Value *Origin, Instruction *OrigIns) {
2083 assert(Shadow);
2084 if (!InsertChecks)
2085 return;
2086
2087 if (!DebugCounter::shouldExecute(DebugInsertCheck)) {
2088 LLVM_DEBUG(dbgs() << "Skipping check of " << *Shadow << " before "
2089 << *OrigIns << "\n");
2090 return;
2091 }
2092#ifndef NDEBUG
2093 Type *ShadowTy = Shadow->getType();
2094 assert((isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy) ||
2095 isa<StructType>(ShadowTy) || isa<ArrayType>(ShadowTy)) &&
2096 "Can only insert checks for integer, vector, and aggregate shadow "
2097 "types");
2098#endif
2099 InstrumentationList.push_back(
2100 ShadowOriginAndInsertPoint(Shadow, Origin, OrigIns));
2101 }
2102
2103 /// Remember the place where a shadow check should be inserted.
2104 ///
2105 /// This location will be later instrumented with a check that will print a
2106 /// UMR warning in runtime if the value is not fully defined.
2107 void insertShadowCheck(Value *Val, Instruction *OrigIns) {
2108 assert(Val);
2109 Value *Shadow, *Origin;
2111 Shadow = getShadow(Val);
2112 if (!Shadow)
2113 return;
2114 Origin = getOrigin(Val);
2115 } else {
2116 Shadow = dyn_cast_or_null<Instruction>(getShadow(Val));
2117 if (!Shadow)
2118 return;
2119 Origin = dyn_cast_or_null<Instruction>(getOrigin(Val));
2120 }
2121 insertShadowCheck(Shadow, Origin, OrigIns);
2122 }
2123
2125 switch (a) {
2126 case AtomicOrdering::NotAtomic:
2127 return AtomicOrdering::NotAtomic;
2128 case AtomicOrdering::Unordered:
2129 case AtomicOrdering::Monotonic:
2130 case AtomicOrdering::Release:
2131 return AtomicOrdering::Release;
2132 case AtomicOrdering::Acquire:
2133 case AtomicOrdering::AcquireRelease:
2134 return AtomicOrdering::AcquireRelease;
2135 case AtomicOrdering::SequentiallyConsistent:
2136 return AtomicOrdering::SequentiallyConsistent;
2137 }
2138 llvm_unreachable("Unknown ordering");
2139 }
2140
2141 Value *makeAddReleaseOrderingTable(IRBuilder<> &IRB) {
2142 constexpr int NumOrderings = (int)AtomicOrderingCABI::seq_cst + 1;
2143 uint32_t OrderingTable[NumOrderings] = {};
2144
2145 OrderingTable[(int)AtomicOrderingCABI::relaxed] =
2146 OrderingTable[(int)AtomicOrderingCABI::release] =
2147 (int)AtomicOrderingCABI::release;
2148 OrderingTable[(int)AtomicOrderingCABI::consume] =
2149 OrderingTable[(int)AtomicOrderingCABI::acquire] =
2150 OrderingTable[(int)AtomicOrderingCABI::acq_rel] =
2151 (int)AtomicOrderingCABI::acq_rel;
2152 OrderingTable[(int)AtomicOrderingCABI::seq_cst] =
2153 (int)AtomicOrderingCABI::seq_cst;
2154
2155 return ConstantDataVector::get(IRB.getContext(), OrderingTable);
2156 }
2157
2159 switch (a) {
2160 case AtomicOrdering::NotAtomic:
2161 return AtomicOrdering::NotAtomic;
2162 case AtomicOrdering::Unordered:
2163 case AtomicOrdering::Monotonic:
2164 case AtomicOrdering::Acquire:
2165 return AtomicOrdering::Acquire;
2166 case AtomicOrdering::Release:
2167 case AtomicOrdering::AcquireRelease:
2168 return AtomicOrdering::AcquireRelease;
2169 case AtomicOrdering::SequentiallyConsistent:
2170 return AtomicOrdering::SequentiallyConsistent;
2171 }
2172 llvm_unreachable("Unknown ordering");
2173 }
2174
2175 Value *makeAddAcquireOrderingTable(IRBuilder<> &IRB) {
2176 constexpr int NumOrderings = (int)AtomicOrderingCABI::seq_cst + 1;
2177 uint32_t OrderingTable[NumOrderings] = {};
2178
2179 OrderingTable[(int)AtomicOrderingCABI::relaxed] =
2180 OrderingTable[(int)AtomicOrderingCABI::acquire] =
2181 OrderingTable[(int)AtomicOrderingCABI::consume] =
2182 (int)AtomicOrderingCABI::acquire;
2183 OrderingTable[(int)AtomicOrderingCABI::release] =
2184 OrderingTable[(int)AtomicOrderingCABI::acq_rel] =
2185 (int)AtomicOrderingCABI::acq_rel;
2186 OrderingTable[(int)AtomicOrderingCABI::seq_cst] =
2187 (int)AtomicOrderingCABI::seq_cst;
2188
2189 return ConstantDataVector::get(IRB.getContext(), OrderingTable);
2190 }
2191
2192 // ------------------- Visitors.
2193 using InstVisitor<MemorySanitizerVisitor>::visit;
2194 void visit(Instruction &I) {
2195 if (I.getMetadata(LLVMContext::MD_nosanitize))
2196 return;
2197 // Don't want to visit if we're in the prologue
2198 if (isInPrologue(I))
2199 return;
2200 if (!DebugCounter::shouldExecute(DebugInstrumentInstruction)) {
2201 LLVM_DEBUG(dbgs() << "Skipping instruction: " << I << "\n");
2202 // We still need to set the shadow and origin to clean values.
2203 setShadow(&I, getCleanShadow(&I));
2204 setOrigin(&I, getCleanOrigin());
2205 return;
2206 }
2207
2208 Instructions.push_back(&I);
2209 }
2210
2211 /// Instrument LoadInst
2212 ///
2213 /// Loads the corresponding shadow and (optionally) origin.
2214 /// Optionally, checks that the load address is fully defined.
2215 void visitLoadInst(LoadInst &I) {
2216 assert(I.getType()->isSized() && "Load type must have size");
2217 assert(!I.getMetadata(LLVMContext::MD_nosanitize));
2218 NextNodeIRBuilder IRB(&I);
2219 Type *ShadowTy = getShadowTy(&I);
2220 Value *Addr = I.getPointerOperand();
2221 Value *ShadowPtr = nullptr, *OriginPtr = nullptr;
2222 const Align Alignment = I.getAlign();
2223 if (PropagateShadow) {
2224 std::tie(ShadowPtr, OriginPtr) =
2225 getShadowOriginPtr(Addr, IRB, ShadowTy, Alignment, /*isStore*/ false);
2226 setShadow(&I,
2227 IRB.CreateAlignedLoad(ShadowTy, ShadowPtr, Alignment, "_msld"));
2228 } else {
2229 setShadow(&I, getCleanShadow(&I));
2230 }
2231
2233 insertShadowCheck(I.getPointerOperand(), &I);
2234
2235 if (I.isAtomic())
2236 I.setOrdering(addAcquireOrdering(I.getOrdering()));
2237
2238 if (MS.TrackOrigins) {
2239 if (PropagateShadow) {
2240 const Align OriginAlignment = std::max(kMinOriginAlignment, Alignment);
2241 setOrigin(
2242 &I, IRB.CreateAlignedLoad(MS.OriginTy, OriginPtr, OriginAlignment));
2243 } else {
2244 setOrigin(&I, getCleanOrigin());
2245 }
2246 }
2247 }
2248
2249 /// Instrument StoreInst
2250 ///
2251 /// Stores the corresponding shadow and (optionally) origin.
2252 /// Optionally, checks that the store address is fully defined.
2253 void visitStoreInst(StoreInst &I) {
2254 StoreList.push_back(&I);
2256 insertShadowCheck(I.getPointerOperand(), &I);
2257 }
2258
2259 void handleCASOrRMW(Instruction &I) {
2260 assert(isa<AtomicRMWInst>(I) || isa<AtomicCmpXchgInst>(I));
2261
2262 IRBuilder<> IRB(&I);
2263 Value *Addr = I.getOperand(0);
2264 Value *Val = I.getOperand(1);
2265 Value *ShadowPtr = getShadowOriginPtr(Addr, IRB, getShadowTy(Val), Align(1),
2266 /*isStore*/ true)
2267 .first;
2268
2270 insertShadowCheck(Addr, &I);
2271
2272 // Only test the conditional argument of cmpxchg instruction.
2273 // The other argument can potentially be uninitialized, but we can not
2274 // detect this situation reliably without possible false positives.
2275 if (isa<AtomicCmpXchgInst>(I))
2276 insertShadowCheck(Val, &I);
2277
2278 IRB.CreateStore(getCleanShadow(Val), ShadowPtr);
2279
2280 setShadow(&I, getCleanShadow(&I));
2281 setOrigin(&I, getCleanOrigin());
2282 }
2283
2284 void visitAtomicRMWInst(AtomicRMWInst &I) {
2285 handleCASOrRMW(I);
2286 I.setOrdering(addReleaseOrdering(I.getOrdering()));
2287 }
2288
2289 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
2290 handleCASOrRMW(I);
2291 I.setSuccessOrdering(addReleaseOrdering(I.getSuccessOrdering()));
2292 }
2293
2294 // Vector manipulation.
2295 void visitExtractElementInst(ExtractElementInst &I) {
2296 insertShadowCheck(I.getOperand(1), &I);
2297 IRBuilder<> IRB(&I);
2298 setShadow(&I, IRB.CreateExtractElement(getShadow(&I, 0), I.getOperand(1),
2299 "_msprop"));
2300 setOrigin(&I, getOrigin(&I, 0));
2301 }
2302
2303 void visitInsertElementInst(InsertElementInst &I) {
2304 insertShadowCheck(I.getOperand(2), &I);
2305 IRBuilder<> IRB(&I);
2306 auto *Shadow0 = getShadow(&I, 0);
2307 auto *Shadow1 = getShadow(&I, 1);
2308 setShadow(&I, IRB.CreateInsertElement(Shadow0, Shadow1, I.getOperand(2),
2309 "_msprop"));
2310 setOriginForNaryOp(I);
2311 }
2312
2313 void visitShuffleVectorInst(ShuffleVectorInst &I) {
2314 IRBuilder<> IRB(&I);
2315 auto *Shadow0 = getShadow(&I, 0);
2316 auto *Shadow1 = getShadow(&I, 1);
2317 setShadow(&I, IRB.CreateShuffleVector(Shadow0, Shadow1, I.getShuffleMask(),
2318 "_msprop"));
2319 setOriginForNaryOp(I);
2320 }
2321
2322 // Casts.
2323 void visitSExtInst(SExtInst &I) {
2324 IRBuilder<> IRB(&I);
2325 setShadow(&I, IRB.CreateSExt(getShadow(&I, 0), I.getType(), "_msprop"));
2326 setOrigin(&I, getOrigin(&I, 0));
2327 }
2328
2329 void visitZExtInst(ZExtInst &I) {
2330 IRBuilder<> IRB(&I);
2331 setShadow(&I, IRB.CreateZExt(getShadow(&I, 0), I.getType(), "_msprop"));
2332 setOrigin(&I, getOrigin(&I, 0));
2333 }
2334
2335 void visitTruncInst(TruncInst &I) {
2336 IRBuilder<> IRB(&I);
2337 setShadow(&I, IRB.CreateTrunc(getShadow(&I, 0), I.getType(), "_msprop"));
2338 setOrigin(&I, getOrigin(&I, 0));
2339 }
2340
2341 void visitBitCastInst(BitCastInst &I) {
2342 // Special case: if this is the bitcast (there is exactly 1 allowed) between
2343 // a musttail call and a ret, don't instrument. New instructions are not
2344 // allowed after a musttail call.
2345 if (auto *CI = dyn_cast<CallInst>(I.getOperand(0)))
2346 if (CI->isMustTailCall())
2347 return;
2348 IRBuilder<> IRB(&I);
2349 setShadow(&I, IRB.CreateBitCast(getShadow(&I, 0), getShadowTy(&I)));
2350 setOrigin(&I, getOrigin(&I, 0));
2351 }
2352
2353 void visitPtrToIntInst(PtrToIntInst &I) {
2354 IRBuilder<> IRB(&I);
2355 setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false,
2356 "_msprop_ptrtoint"));
2357 setOrigin(&I, getOrigin(&I, 0));
2358 }
2359
2360 void visitIntToPtrInst(IntToPtrInst &I) {
2361 IRBuilder<> IRB(&I);
2362 setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false,
2363 "_msprop_inttoptr"));
2364 setOrigin(&I, getOrigin(&I, 0));
2365 }
2366
2367 void visitFPToSIInst(CastInst &I) { handleShadowOr(I); }
2368 void visitFPToUIInst(CastInst &I) { handleShadowOr(I); }
2369 void visitSIToFPInst(CastInst &I) { handleShadowOr(I); }
2370 void visitUIToFPInst(CastInst &I) { handleShadowOr(I); }
2371 void visitFPExtInst(CastInst &I) { handleShadowOr(I); }
2372 void visitFPTruncInst(CastInst &I) { handleShadowOr(I); }
2373
2374 /// Propagate shadow for bitwise AND.
2375 ///
2376 /// This code is exact, i.e. if, for example, a bit in the left argument
2377 /// is defined and 0, then neither the value not definedness of the
2378 /// corresponding bit in B don't affect the resulting shadow.
2379 void visitAnd(BinaryOperator &I) {
2380 IRBuilder<> IRB(&I);
2381 // "And" of 0 and a poisoned value results in unpoisoned value.
2382 // 1&1 => 1; 0&1 => 0; p&1 => p;
2383 // 1&0 => 0; 0&0 => 0; p&0 => 0;
2384 // 1&p => p; 0&p => 0; p&p => p;
2385 // S = (S1 & S2) | (V1 & S2) | (S1 & V2)
2386 Value *S1 = getShadow(&I, 0);
2387 Value *S2 = getShadow(&I, 1);
2388 Value *V1 = I.getOperand(0);
2389 Value *V2 = I.getOperand(1);
2390 if (V1->getType() != S1->getType()) {
2391 V1 = IRB.CreateIntCast(V1, S1->getType(), false);
2392 V2 = IRB.CreateIntCast(V2, S2->getType(), false);
2393 }
2394 Value *S1S2 = IRB.CreateAnd(S1, S2);
2395 Value *V1S2 = IRB.CreateAnd(V1, S2);
2396 Value *S1V2 = IRB.CreateAnd(S1, V2);
2397 setShadow(&I, IRB.CreateOr({S1S2, V1S2, S1V2}));
2398 setOriginForNaryOp(I);
2399 }
2400
2401 void visitOr(BinaryOperator &I) {
2402 IRBuilder<> IRB(&I);
2403 // "Or" of 1 and a poisoned value results in unpoisoned value.
2404 // 1|1 => 1; 0|1 => 1; p|1 => 1;
2405 // 1|0 => 1; 0|0 => 0; p|0 => p;
2406 // 1|p => 1; 0|p => p; p|p => p;
2407 // S = (S1 & S2) | (~V1 & S2) | (S1 & ~V2)
2408 Value *S1 = getShadow(&I, 0);
2409 Value *S2 = getShadow(&I, 1);
2410 Value *V1 = IRB.CreateNot(I.getOperand(0));
2411 Value *V2 = IRB.CreateNot(I.getOperand(1));
2412 if (V1->getType() != S1->getType()) {
2413 V1 = IRB.CreateIntCast(V1, S1->getType(), false);
2414 V2 = IRB.CreateIntCast(V2, S2->getType(), false);
2415 }
2416 Value *S1S2 = IRB.CreateAnd(S1, S2);
2417 Value *V1S2 = IRB.CreateAnd(V1, S2);
2418 Value *S1V2 = IRB.CreateAnd(S1, V2);
2419 setShadow(&I, IRB.CreateOr({S1S2, V1S2, S1V2}));
2420 setOriginForNaryOp(I);
2421 }
2422
2423 /// Default propagation of shadow and/or origin.
2424 ///
2425 /// This class implements the general case of shadow propagation, used in all
2426 /// cases where we don't know and/or don't care about what the operation
2427 /// actually does. It converts all input shadow values to a common type
2428 /// (extending or truncating as necessary), and bitwise OR's them.
2429 ///
2430 /// This is much cheaper than inserting checks (i.e. requiring inputs to be
2431 /// fully initialized), and less prone to false positives.
2432 ///
2433 /// This class also implements the general case of origin propagation. For a
2434 /// Nary operation, result origin is set to the origin of an argument that is
2435 /// not entirely initialized. If there is more than one such arguments, the
2436 /// rightmost of them is picked. It does not matter which one is picked if all
2437 /// arguments are initialized.
2438 template <bool CombineShadow> class Combiner {
2439 Value *Shadow = nullptr;
2440 Value *Origin = nullptr;
2441 IRBuilder<> &IRB;
2442 MemorySanitizerVisitor *MSV;
2443
2444 public:
2445 Combiner(MemorySanitizerVisitor *MSV, IRBuilder<> &IRB)
2446 : IRB(IRB), MSV(MSV) {}
2447
2448 /// Add a pair of shadow and origin values to the mix.
2449 Combiner &Add(Value *OpShadow, Value *OpOrigin) {
2450 if (CombineShadow) {
2451 assert(OpShadow);
2452 if (!Shadow)
2453 Shadow = OpShadow;
2454 else {
2455 OpShadow = MSV->CreateShadowCast(IRB, OpShadow, Shadow->getType());
2456 Shadow = IRB.CreateOr(Shadow, OpShadow, "_msprop");
2457 }
2458 }
2459
2460 if (MSV->MS.TrackOrigins) {
2461 assert(OpOrigin);
2462 if (!Origin) {
2463 Origin = OpOrigin;
2464 } else {
2465 Constant *ConstOrigin = dyn_cast<Constant>(OpOrigin);
2466 // No point in adding something that might result in 0 origin value.
2467 if (!ConstOrigin || !ConstOrigin->isNullValue()) {
2468 Value *Cond = MSV->convertToBool(OpShadow, IRB);
2469 Origin = IRB.CreateSelect(Cond, OpOrigin, Origin);
2470 }
2471 }
2472 }
2473 return *this;
2474 }
2475
2476 /// Add an application value to the mix.
2477 Combiner &Add(Value *V) {
2478 Value *OpShadow = MSV->getShadow(V);
2479 Value *OpOrigin = MSV->MS.TrackOrigins ? MSV->getOrigin(V) : nullptr;
2480 return Add(OpShadow, OpOrigin);
2481 }
2482
2483 /// Set the current combined values as the given instruction's shadow
2484 /// and origin.
2485 void Done(Instruction *I) {
2486 if (CombineShadow) {
2487 assert(Shadow);
2488 Shadow = MSV->CreateShadowCast(IRB, Shadow, MSV->getShadowTy(I));
2489 MSV->setShadow(I, Shadow);
2490 }
2491 if (MSV->MS.TrackOrigins) {
2492 assert(Origin);
2493 MSV->setOrigin(I, Origin);
2494 }
2495 }
2496 };
2497
2498 using ShadowAndOriginCombiner = Combiner<true>;
2499 using OriginCombiner = Combiner<false>;
2500
2501 /// Propagate origin for arbitrary operation.
2502 void setOriginForNaryOp(Instruction &I) {
2503 if (!MS.TrackOrigins)
2504 return;
2505 IRBuilder<> IRB(&I);
2506 OriginCombiner OC(this, IRB);
2507 for (Use &Op : I.operands())
2508 OC.Add(Op.get());
2509 OC.Done(&I);
2510 }
2511
2512 size_t VectorOrPrimitiveTypeSizeInBits(Type *Ty) {
2513 assert(!(Ty->isVectorTy() && Ty->getScalarType()->isPointerTy()) &&
2514 "Vector of pointers is not a valid shadow type");
2515 return Ty->isVectorTy() ? cast<FixedVectorType>(Ty)->getNumElements() *
2517 : Ty->getPrimitiveSizeInBits();
2518 }
2519
2520 /// Cast between two shadow types, extending or truncating as
2521 /// necessary.
2522 Value *CreateShadowCast(IRBuilder<> &IRB, Value *V, Type *dstTy,
2523 bool Signed = false) {
2524 Type *srcTy = V->getType();
2525 if (srcTy == dstTy)
2526 return V;
2527 size_t srcSizeInBits = VectorOrPrimitiveTypeSizeInBits(srcTy);
2528 size_t dstSizeInBits = VectorOrPrimitiveTypeSizeInBits(dstTy);
2529 if (srcSizeInBits > 1 && dstSizeInBits == 1)
2530 return IRB.CreateICmpNE(V, getCleanShadow(V));
2531
2532 if (dstTy->isIntegerTy() && srcTy->isIntegerTy())
2533 return IRB.CreateIntCast(V, dstTy, Signed);
2534 if (dstTy->isVectorTy() && srcTy->isVectorTy() &&
2535 cast<VectorType>(dstTy)->getElementCount() ==
2536 cast<VectorType>(srcTy)->getElementCount())
2537 return IRB.CreateIntCast(V, dstTy, Signed);
2538 Value *V1 = IRB.CreateBitCast(V, Type::getIntNTy(*MS.C, srcSizeInBits));
2539 Value *V2 =
2540 IRB.CreateIntCast(V1, Type::getIntNTy(*MS.C, dstSizeInBits), Signed);
2541 return IRB.CreateBitCast(V2, dstTy);
2542 // TODO: handle struct types.
2543 }
2544
2545 /// Cast an application value to the type of its own shadow.
2546 Value *CreateAppToShadowCast(IRBuilder<> &IRB, Value *V) {
2547 Type *ShadowTy = getShadowTy(V);
2548 if (V->getType() == ShadowTy)
2549 return V;
2550 if (V->getType()->isPtrOrPtrVectorTy())
2551 return IRB.CreatePtrToInt(V, ShadowTy);
2552 else
2553 return IRB.CreateBitCast(V, ShadowTy);
2554 }
2555
2556 /// Propagate shadow for arbitrary operation.
2557 void handleShadowOr(Instruction &I) {
2558 IRBuilder<> IRB(&I);
2559 ShadowAndOriginCombiner SC(this, IRB);
2560 for (Use &Op : I.operands())
2561 SC.Add(Op.get());
2562 SC.Done(&I);
2563 }
2564
2565 void visitFNeg(UnaryOperator &I) { handleShadowOr(I); }
2566
2567 // Handle multiplication by constant.
2568 //
2569 // Handle a special case of multiplication by constant that may have one or
2570 // more zeros in the lower bits. This makes corresponding number of lower bits
2571 // of the result zero as well. We model it by shifting the other operand
2572 // shadow left by the required number of bits. Effectively, we transform
2573 // (X * (A * 2**B)) to ((X << B) * A) and instrument (X << B) as (Sx << B).
2574 // We use multiplication by 2**N instead of shift to cover the case of
2575 // multiplication by 0, which may occur in some elements of a vector operand.
2576 void handleMulByConstant(BinaryOperator &I, Constant *ConstArg,
2577 Value *OtherArg) {
2578 Constant *ShadowMul;
2579 Type *Ty = ConstArg->getType();
2580 if (auto *VTy = dyn_cast<VectorType>(Ty)) {
2581 unsigned NumElements = cast<FixedVectorType>(VTy)->getNumElements();
2582 Type *EltTy = VTy->getElementType();
2584 for (unsigned Idx = 0; Idx < NumElements; ++Idx) {
2585 if (ConstantInt *Elt =
2586 dyn_cast<ConstantInt>(ConstArg->getAggregateElement(Idx))) {
2587 const APInt &V = Elt->getValue();
2588 APInt V2 = APInt(V.getBitWidth(), 1) << V.countr_zero();
2589 Elements.push_back(ConstantInt::get(EltTy, V2));
2590 } else {
2591 Elements.push_back(ConstantInt::get(EltTy, 1));
2592 }
2593 }
2594 ShadowMul = ConstantVector::get(Elements);
2595 } else {
2596 if (ConstantInt *Elt = dyn_cast<ConstantInt>(ConstArg)) {
2597 const APInt &V = Elt->getValue();
2598 APInt V2 = APInt(V.getBitWidth(), 1) << V.countr_zero();
2599 ShadowMul = ConstantInt::get(Ty, V2);
2600 } else {
2601 ShadowMul = ConstantInt::get(Ty, 1);
2602 }
2603 }
2604
2605 IRBuilder<> IRB(&I);
2606 setShadow(&I,
2607 IRB.CreateMul(getShadow(OtherArg), ShadowMul, "msprop_mul_cst"));
2608 setOrigin(&I, getOrigin(OtherArg));
2609 }
2610
2611 void visitMul(BinaryOperator &I) {
2612 Constant *constOp0 = dyn_cast<Constant>(I.getOperand(0));
2613 Constant *constOp1 = dyn_cast<Constant>(I.getOperand(1));
2614 if (constOp0 && !constOp1)
2615 handleMulByConstant(I, constOp0, I.getOperand(1));
2616 else if (constOp1 && !constOp0)
2617 handleMulByConstant(I, constOp1, I.getOperand(0));
2618 else
2619 handleShadowOr(I);
2620 }
2621
2622 void visitFAdd(BinaryOperator &I) { handleShadowOr(I); }
2623 void visitFSub(BinaryOperator &I) { handleShadowOr(I); }
2624 void visitFMul(BinaryOperator &I) { handleShadowOr(I); }
2625 void visitAdd(BinaryOperator &I) { handleShadowOr(I); }
2626 void visitSub(BinaryOperator &I) { handleShadowOr(I); }
2627 void visitXor(BinaryOperator &I) { handleShadowOr(I); }
2628
2629 void handleIntegerDiv(Instruction &I) {
2630 IRBuilder<> IRB(&I);
2631 // Strict on the second argument.
2632 insertShadowCheck(I.getOperand(1), &I);
2633 setShadow(&I, getShadow(&I, 0));
2634 setOrigin(&I, getOrigin(&I, 0));
2635 }
2636
2637 void visitUDiv(BinaryOperator &I) { handleIntegerDiv(I); }
2638 void visitSDiv(BinaryOperator &I) { handleIntegerDiv(I); }
2639 void visitURem(BinaryOperator &I) { handleIntegerDiv(I); }
2640 void visitSRem(BinaryOperator &I) { handleIntegerDiv(I); }
2641
2642 // Floating point division is side-effect free. We can not require that the
2643 // divisor is fully initialized and must propagate shadow. See PR37523.
2644 void visitFDiv(BinaryOperator &I) { handleShadowOr(I); }
2645 void visitFRem(BinaryOperator &I) { handleShadowOr(I); }
2646
2647 /// Instrument == and != comparisons.
2648 ///
2649 /// Sometimes the comparison result is known even if some of the bits of the
2650 /// arguments are not.
2651 void handleEqualityComparison(ICmpInst &I) {
2652 IRBuilder<> IRB(&I);
2653 Value *A = I.getOperand(0);
2654 Value *B = I.getOperand(1);
2655 Value *Sa = getShadow(A);
2656 Value *Sb = getShadow(B);
2657
2658 // Get rid of pointers and vectors of pointers.
2659 // For ints (and vectors of ints), types of A and Sa match,
2660 // and this is a no-op.
2661 A = IRB.CreatePointerCast(A, Sa->getType());
2662 B = IRB.CreatePointerCast(B, Sb->getType());
2663
2664 // A == B <==> (C = A^B) == 0
2665 // A != B <==> (C = A^B) != 0
2666 // Sc = Sa | Sb
2667 Value *C = IRB.CreateXor(A, B);
2668 Value *Sc = IRB.CreateOr(Sa, Sb);
2669 // Now dealing with i = (C == 0) comparison (or C != 0, does not matter now)
2670 // Result is defined if one of the following is true
2671 // * there is a defined 1 bit in C
2672 // * C is fully defined
2673 // Si = !(C & ~Sc) && Sc
2675 Value *MinusOne = Constant::getAllOnesValue(Sc->getType());
2676 Value *LHS = IRB.CreateICmpNE(Sc, Zero);
2677 Value *RHS =
2678 IRB.CreateICmpEQ(IRB.CreateAnd(IRB.CreateXor(Sc, MinusOne), C), Zero);
2679 Value *Si = IRB.CreateAnd(LHS, RHS);
2680 Si->setName("_msprop_icmp");
2681 setShadow(&I, Si);
2682 setOriginForNaryOp(I);
2683 }
2684
2685 /// Build the lowest possible value of V, taking into account V's
2686 /// uninitialized bits.
2687 Value *getLowestPossibleValue(IRBuilder<> &IRB, Value *A, Value *Sa,
2688 bool isSigned) {
2689 if (isSigned) {
2690 // Split shadow into sign bit and other bits.
2691 Value *SaOtherBits = IRB.CreateLShr(IRB.CreateShl(Sa, 1), 1);
2692 Value *SaSignBit = IRB.CreateXor(Sa, SaOtherBits);
2693 // Maximise the undefined shadow bit, minimize other undefined bits.
2694 return IRB.CreateOr(IRB.CreateAnd(A, IRB.CreateNot(SaOtherBits)),
2695 SaSignBit);
2696 } else {
2697 // Minimize undefined bits.
2698 return IRB.CreateAnd(A, IRB.CreateNot(Sa));
2699 }
2700 }
2701
2702 /// Build the highest possible value of V, taking into account V's
2703 /// uninitialized bits.
2704 Value *getHighestPossibleValue(IRBuilder<> &IRB, Value *A, Value *Sa,
2705 bool isSigned) {
2706 if (isSigned) {
2707 // Split shadow into sign bit and other bits.
2708 Value *SaOtherBits = IRB.CreateLShr(IRB.CreateShl(Sa, 1), 1);
2709 Value *SaSignBit = IRB.CreateXor(Sa, SaOtherBits);
2710 // Minimise the undefined shadow bit, maximise other undefined bits.
2711 return IRB.CreateOr(IRB.CreateAnd(A, IRB.CreateNot(SaSignBit)),
2712 SaOtherBits);
2713 } else {
2714 // Maximize undefined bits.
2715 return IRB.CreateOr(A, Sa);
2716 }
2717 }
2718
2719 /// Instrument relational comparisons.
2720 ///
2721 /// This function does exact shadow propagation for all relational
2722 /// comparisons of integers, pointers and vectors of those.
2723 /// FIXME: output seems suboptimal when one of the operands is a constant
2724 void handleRelationalComparisonExact(ICmpInst &I) {
2725 IRBuilder<> IRB(&I);
2726 Value *A = I.getOperand(0);
2727 Value *B = I.getOperand(1);
2728 Value *Sa = getShadow(A);
2729 Value *Sb = getShadow(B);
2730
2731 // Get rid of pointers and vectors of pointers.
2732 // For ints (and vectors of ints), types of A and Sa match,
2733 // and this is a no-op.
2734 A = IRB.CreatePointerCast(A, Sa->getType());
2735 B = IRB.CreatePointerCast(B, Sb->getType());
2736
2737 // Let [a0, a1] be the interval of possible values of A, taking into account
2738 // its undefined bits. Let [b0, b1] be the interval of possible values of B.
2739 // Then (A cmp B) is defined iff (a0 cmp b1) == (a1 cmp b0).
2740 bool IsSigned = I.isSigned();
2741 Value *S1 = IRB.CreateICmp(I.getPredicate(),
2742 getLowestPossibleValue(IRB, A, Sa, IsSigned),
2743 getHighestPossibleValue(IRB, B, Sb, IsSigned));
2744 Value *S2 = IRB.CreateICmp(I.getPredicate(),
2745 getHighestPossibleValue(IRB, A, Sa, IsSigned),
2746 getLowestPossibleValue(IRB, B, Sb, IsSigned));
2747 Value *Si = IRB.CreateXor(S1, S2);
2748 setShadow(&I, Si);
2749 setOriginForNaryOp(I);
2750 }
2751
2752 /// Instrument signed relational comparisons.
2753 ///
2754 /// Handle sign bit tests: x<0, x>=0, x<=-1, x>-1 by propagating the highest
2755 /// bit of the shadow. Everything else is delegated to handleShadowOr().
2756 void handleSignedRelationalComparison(ICmpInst &I) {
2757 Constant *constOp;
2758 Value *op = nullptr;
2760 if ((constOp = dyn_cast<Constant>(I.getOperand(1)))) {
2761 op = I.getOperand(0);
2762 pre = I.getPredicate();
2763 } else if ((constOp = dyn_cast<Constant>(I.getOperand(0)))) {
2764 op = I.getOperand(1);
2765 pre = I.getSwappedPredicate();
2766 } else {
2767 handleShadowOr(I);
2768 return;
2769 }
2770
2771 if ((constOp->isNullValue() &&
2772 (pre == CmpInst::ICMP_SLT || pre == CmpInst::ICMP_SGE)) ||
2773 (constOp->isAllOnesValue() &&
2774 (pre == CmpInst::ICMP_SGT || pre == CmpInst::ICMP_SLE))) {
2775 IRBuilder<> IRB(&I);
2776 Value *Shadow = IRB.CreateICmpSLT(getShadow(op), getCleanShadow(op),
2777 "_msprop_icmp_s");
2778 setShadow(&I, Shadow);
2779 setOrigin(&I, getOrigin(op));
2780 } else {
2781 handleShadowOr(I);
2782 }
2783 }
2784
2785 void visitICmpInst(ICmpInst &I) {
2786 if (!ClHandleICmp) {
2787 handleShadowOr(I);
2788 return;
2789 }
2790 if (I.isEquality()) {
2791 handleEqualityComparison(I);
2792 return;
2793 }
2794
2795 assert(I.isRelational());
2796 if (ClHandleICmpExact) {
2797 handleRelationalComparisonExact(I);
2798 return;
2799 }
2800 if (I.isSigned()) {
2801 handleSignedRelationalComparison(I);
2802 return;
2803 }
2804
2805 assert(I.isUnsigned());
2806 if ((isa<Constant>(I.getOperand(0)) || isa<Constant>(I.getOperand(1)))) {
2807 handleRelationalComparisonExact(I);
2808 return;
2809 }
2810
2811 handleShadowOr(I);
2812 }
2813
2814 void visitFCmpInst(FCmpInst &I) { handleShadowOr(I); }
2815
2816 void handleShift(BinaryOperator &I) {
2817 IRBuilder<> IRB(&I);
2818 // If any of the S2 bits are poisoned, the whole thing is poisoned.
2819 // Otherwise perform the same shift on S1.
2820 Value *S1 = getShadow(&I, 0);
2821 Value *S2 = getShadow(&I, 1);
2822 Value *S2Conv =
2823 IRB.CreateSExt(IRB.CreateICmpNE(S2, getCleanShadow(S2)), S2->getType());
2824 Value *V2 = I.getOperand(1);
2825 Value *Shift = IRB.CreateBinOp(I.getOpcode(), S1, V2);
2826 setShadow(&I, IRB.CreateOr(Shift, S2Conv));
2827 setOriginForNaryOp(I);
2828 }
2829
2830 void visitShl(BinaryOperator &I) { handleShift(I); }
2831 void visitAShr(BinaryOperator &I) { handleShift(I); }
2832 void visitLShr(BinaryOperator &I) { handleShift(I); }
2833
2834 void handleFunnelShift(IntrinsicInst &I) {
2835 IRBuilder<> IRB(&I);
2836 // If any of the S2 bits are poisoned, the whole thing is poisoned.
2837 // Otherwise perform the same shift on S0 and S1.
2838 Value *S0 = getShadow(&I, 0);
2839 Value *S1 = getShadow(&I, 1);
2840 Value *S2 = getShadow(&I, 2);
2841 Value *S2Conv =
2842 IRB.CreateSExt(IRB.CreateICmpNE(S2, getCleanShadow(S2)), S2->getType());
2843 Value *V2 = I.getOperand(2);
2845 I.getModule(), I.getIntrinsicID(), S2Conv->getType());
2846 Value *Shift = IRB.CreateCall(Intrin, {S0, S1, V2});
2847 setShadow(&I, IRB.CreateOr(Shift, S2Conv));
2848 setOriginForNaryOp(I);
2849 }
2850
2851 /// Instrument llvm.memmove
2852 ///
2853 /// At this point we don't know if llvm.memmove will be inlined or not.
2854 /// If we don't instrument it and it gets inlined,
2855 /// our interceptor will not kick in and we will lose the memmove.
2856 /// If we instrument the call here, but it does not get inlined,
2857 /// we will memove the shadow twice: which is bad in case
2858 /// of overlapping regions. So, we simply lower the intrinsic to a call.
2859 ///
2860 /// Similar situation exists for memcpy and memset.
2861 void visitMemMoveInst(MemMoveInst &I) {
2862 getShadow(I.getArgOperand(1)); // Ensure shadow initialized
2863 IRBuilder<> IRB(&I);
2864 IRB.CreateCall(MS.MemmoveFn,
2865 {I.getArgOperand(0), I.getArgOperand(1),
2866 IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
2867 I.eraseFromParent();
2868 }
2869
2870 /// Instrument memcpy
2871 ///
2872 /// Similar to memmove: avoid copying shadow twice. This is somewhat
2873 /// unfortunate as it may slowdown small constant memcpys.
2874 /// FIXME: consider doing manual inline for small constant sizes and proper
2875 /// alignment.
2876 ///
2877 /// Note: This also handles memcpy.inline, which promises no calls to external
2878 /// functions as an optimization. However, with instrumentation enabled this
2879 /// is difficult to promise; additionally, we know that the MSan runtime
2880 /// exists and provides __msan_memcpy(). Therefore, we assume that with
2881 /// instrumentation it's safe to turn memcpy.inline into a call to
2882 /// __msan_memcpy(). Should this be wrong, such as when implementing memcpy()
2883 /// itself, instrumentation should be disabled with the no_sanitize attribute.
2884 void visitMemCpyInst(MemCpyInst &I) {
2885 getShadow(I.getArgOperand(1)); // Ensure shadow initialized
2886 IRBuilder<> IRB(&I);
2887 IRB.CreateCall(MS.MemcpyFn,
2888 {I.getArgOperand(0), I.getArgOperand(1),
2889 IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
2890 I.eraseFromParent();
2891 }
2892
2893 // Same as memcpy.
2894 void visitMemSetInst(MemSetInst &I) {
2895 IRBuilder<> IRB(&I);
2896 IRB.CreateCall(
2897 MS.MemsetFn,
2898 {I.getArgOperand(0),
2899 IRB.CreateIntCast(I.getArgOperand(1), IRB.getInt32Ty(), false),
2900 IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
2901 I.eraseFromParent();
2902 }
2903
2904 void visitVAStartInst(VAStartInst &I) { VAHelper->visitVAStartInst(I); }
2905
2906 void visitVACopyInst(VACopyInst &I) { VAHelper->visitVACopyInst(I); }
2907
2908 /// Handle vector store-like intrinsics.
2909 ///
2910 /// Instrument intrinsics that look like a simple SIMD store: writes memory,
2911 /// has 1 pointer argument and 1 vector argument, returns void.
2912 bool handleVectorStoreIntrinsic(IntrinsicInst &I) {
2913 IRBuilder<> IRB(&I);
2914 Value *Addr = I.getArgOperand(0);
2915 Value *Shadow = getShadow(&I, 1);
2916 Value *ShadowPtr, *OriginPtr;
2917
2918 // We don't know the pointer alignment (could be unaligned SSE store!).
2919 // Have to assume to worst case.
2920 std::tie(ShadowPtr, OriginPtr) = getShadowOriginPtr(
2921 Addr, IRB, Shadow->getType(), Align(1), /*isStore*/ true);
2922 IRB.CreateAlignedStore(Shadow, ShadowPtr, Align(1));
2923
2925 insertShadowCheck(Addr, &I);
2926
2927 // FIXME: factor out common code from materializeStores
2928 if (MS.TrackOrigins)
2929 IRB.CreateStore(getOrigin(&I, 1), OriginPtr);
2930 return true;
2931 }
2932
2933 /// Handle vector load-like intrinsics.
2934 ///
2935 /// Instrument intrinsics that look like a simple SIMD load: reads memory,
2936 /// has 1 pointer argument, returns a vector.
2937 bool handleVectorLoadIntrinsic(IntrinsicInst &I) {
2938 IRBuilder<> IRB(&I);
2939 Value *Addr = I.getArgOperand(0);
2940
2941 Type *ShadowTy = getShadowTy(&I);
2942 Value *ShadowPtr = nullptr, *OriginPtr = nullptr;
2943 if (PropagateShadow) {
2944 // We don't know the pointer alignment (could be unaligned SSE load!).
2945 // Have to assume to worst case.
2946 const Align Alignment = Align(1);
2947 std::tie(ShadowPtr, OriginPtr) =
2948 getShadowOriginPtr(Addr, IRB, ShadowTy, Alignment, /*isStore*/ false);
2949 setShadow(&I,
2950 IRB.CreateAlignedLoad(ShadowTy, ShadowPtr, Alignment, "_msld"));
2951 } else {
2952 setShadow(&I, getCleanShadow(&I));
2953 }
2954
2956 insertShadowCheck(Addr, &I);
2957
2958 if (MS.TrackOrigins) {
2959 if (PropagateShadow)
2960 setOrigin(&I, IRB.CreateLoad(MS.OriginTy, OriginPtr));
2961 else
2962 setOrigin(&I, getCleanOrigin());
2963 }
2964 return true;
2965 }
2966
2967 /// Handle (SIMD arithmetic)-like intrinsics.
2968 ///
2969 /// Instrument intrinsics with any number of arguments of the same type,
2970 /// equal to the return type. The type should be simple (no aggregates or
2971 /// pointers; vectors are fine).
2972 /// Caller guarantees that this intrinsic does not access memory.
2973 bool maybeHandleSimpleNomemIntrinsic(IntrinsicInst &I) {
2974 Type *RetTy = I.getType();
2975 if (!(RetTy->isIntOrIntVectorTy() || RetTy->isFPOrFPVectorTy() ||
2976 RetTy->isX86_MMXTy()))
2977 return false;
2978
2979 unsigned NumArgOperands = I.arg_size();
2980 for (unsigned i = 0; i < NumArgOperands; ++i) {
2981 Type *Ty = I.getArgOperand(i)->getType();
2982 if (Ty != RetTy)
2983 return false;
2984 }
2985
2986 IRBuilder<> IRB(&I);
2987 ShadowAndOriginCombiner SC(this, IRB);
2988 for (unsigned i = 0; i < NumArgOperands; ++i)
2989 SC.Add(I.getArgOperand(i));
2990 SC.Done(&I);
2991
2992 return true;
2993 }
2994
2995 /// Heuristically instrument unknown intrinsics.
2996 ///
2997 /// The main purpose of this code is to do something reasonable with all
2998 /// random intrinsics we might encounter, most importantly - SIMD intrinsics.
2999 /// We recognize several classes of intrinsics by their argument types and
3000 /// ModRefBehaviour and apply special instrumentation when we are reasonably
3001 /// sure that we know what the intrinsic does.
3002 ///
3003 /// We special-case intrinsics where this approach fails. See llvm.bswap
3004 /// handling as an example of that.
3005 bool handleUnknownIntrinsic(IntrinsicInst &I) {
3006 unsigned NumArgOperands = I.arg_size();
3007 if (NumArgOperands == 0)
3008 return false;
3009
3010 if (NumArgOperands == 2 && I.getArgOperand(0)->getType()->isPointerTy() &&
3011 I.getArgOperand(1)->getType()->isVectorTy() &&
3012 I.getType()->isVoidTy() && !I.onlyReadsMemory()) {
3013 // This looks like a vector store.
3014 return handleVectorStoreIntrinsic(I);
3015 }
3016
3017 if (NumArgOperands == 1 && I.getArgOperand(0)->getType()->isPointerTy() &&
3018 I.getType()->isVectorTy() && I.onlyReadsMemory()) {
3019 // This looks like a vector load.
3020 return handleVectorLoadIntrinsic(I);
3021 }
3022
3023 if (I.doesNotAccessMemory())
3024 if (maybeHandleSimpleNomemIntrinsic(I))
3025 return true;
3026
3027 // FIXME: detect and handle SSE maskstore/maskload
3028 return false;
3029 }
3030
3031 void handleInvariantGroup(IntrinsicInst &I) {
3032 setShadow(&I, getShadow(&I, 0));
3033 setOrigin(&I, getOrigin(&I, 0));
3034 }
3035
3036 void handleLifetimeStart(IntrinsicInst &I) {
3037 if (!PoisonStack)
3038 return;
3039 AllocaInst *AI = llvm::findAllocaForValue(I.getArgOperand(1));
3040 if (!AI)
3041 InstrumentLifetimeStart = false;
3042 LifetimeStartList.push_back(std::make_pair(&I, AI));
3043 }
3044
3045 void handleBswap(IntrinsicInst &I) {
3046 IRBuilder<> IRB(&I);
3047 Value *Op = I.getArgOperand(0);
3048 Type *OpType = Op->getType();
3050 F.getParent(), Intrinsic::bswap, ArrayRef(&OpType, 1));
3051 setShadow(&I, IRB.CreateCall(BswapFunc, getShadow(Op)));
3052 setOrigin(&I, getOrigin(Op));
3053 }
3054
3055 void handleCountZeroes(IntrinsicInst &I) {
3056 IRBuilder<> IRB(&I);
3057 Value *Src = I.getArgOperand(0);
3058
3059 // Set the Output shadow based on input Shadow
3060 Value *BoolShadow = IRB.CreateIsNotNull(getShadow(Src), "_mscz_bs");
3061
3062 // If zero poison is requested, mix in with the shadow
3063 Constant *IsZeroPoison = cast<Constant>(I.getOperand(1));
3064 if (!IsZeroPoison->isZeroValue()) {
3065 Value *BoolZeroPoison = IRB.CreateIsNull(Src, "_mscz_bzp");
3066 BoolShadow = IRB.CreateOr(BoolShadow, BoolZeroPoison, "_mscz_bs");
3067 }
3068
3069 Value *OutputShadow =
3070 IRB.CreateSExt(BoolShadow, getShadowTy(Src), "_mscz_os");
3071
3072 setShadow(&I, OutputShadow);
3073 setOriginForNaryOp(I);
3074 }
3075
3076 // Instrument vector convert intrinsic.
3077 //
3078 // This function instruments intrinsics like cvtsi2ss:
3079 // %Out = int_xxx_cvtyyy(%ConvertOp)
3080 // or
3081 // %Out = int_xxx_cvtyyy(%CopyOp, %ConvertOp)
3082 // Intrinsic converts \p NumUsedElements elements of \p ConvertOp to the same
3083 // number \p Out elements, and (if has 2 arguments) copies the rest of the
3084 // elements from \p CopyOp.
3085 // In most cases conversion involves floating-point value which may trigger a
3086 // hardware exception when not fully initialized. For this reason we require
3087 // \p ConvertOp[0:NumUsedElements] to be fully initialized and trap otherwise.
3088 // We copy the shadow of \p CopyOp[NumUsedElements:] to \p
3089 // Out[NumUsedElements:]. This means that intrinsics without \p CopyOp always
3090 // return a fully initialized value.
3091 void handleVectorConvertIntrinsic(IntrinsicInst &I, int NumUsedElements,
3092 bool HasRoundingMode = false) {
3093 IRBuilder<> IRB(&I);
3094 Value *CopyOp, *ConvertOp;
3095
3096 assert((!HasRoundingMode ||
3097 isa<ConstantInt>(I.getArgOperand(I.arg_size() - 1))) &&
3098 "Invalid rounding mode");
3099
3100 switch (I.arg_size() - HasRoundingMode) {
3101 case 2:
3102 CopyOp = I.getArgOperand(0);
3103 ConvertOp = I.getArgOperand(1);
3104 break;
3105 case 1:
3106 ConvertOp = I.getArgOperand(0);
3107 CopyOp = nullptr;
3108 break;
3109 default:
3110 llvm_unreachable("Cvt intrinsic with unsupported number of arguments.");
3111 }
3112
3113 // The first *NumUsedElements* elements of ConvertOp are converted to the
3114 // same number of output elements. The rest of the output is copied from
3115 // CopyOp, or (if not available) filled with zeroes.
3116 // Combine shadow for elements of ConvertOp that are used in this operation,
3117 // and insert a check.
3118 // FIXME: consider propagating shadow of ConvertOp, at least in the case of
3119 // int->any conversion.
3120 Value *ConvertShadow = getShadow(ConvertOp);
3121 Value *AggShadow = nullptr;
3122 if (ConvertOp->getType()->isVectorTy()) {
3123 AggShadow = IRB.CreateExtractElement(
3124 ConvertShadow, ConstantInt::get(IRB.getInt32Ty(), 0));
3125 for (int i = 1; i < NumUsedElements; ++i) {
3126 Value *MoreShadow = IRB.CreateExtractElement(
3127 ConvertShadow, ConstantInt::get(IRB.getInt32Ty(), i));
3128 AggShadow = IRB.CreateOr(AggShadow, MoreShadow);
3129 }
3130 } else {
3131 AggShadow = ConvertShadow;
3132 }
3133 assert(AggShadow->getType()->isIntegerTy());
3134 insertShadowCheck(AggShadow, getOrigin(ConvertOp), &I);
3135
3136 // Build result shadow by zero-filling parts of CopyOp shadow that come from
3137 // ConvertOp.
3138 if (CopyOp) {
3139 assert(CopyOp->getType() == I.getType());
3140 assert(CopyOp->getType()->isVectorTy());
3141 Value *ResultShadow = getShadow(CopyOp);
3142 Type *EltTy = cast<VectorType>(ResultShadow->getType())->getElementType();
3143 for (int i = 0; i < NumUsedElements; ++i) {
3144 ResultShadow = IRB.CreateInsertElement(
3145 ResultShadow, ConstantInt::getNullValue(EltTy),
3146 ConstantInt::get(IRB.getInt32Ty(), i));
3147 }
3148 setShadow(&I, ResultShadow);
3149 setOrigin(&I, getOrigin(CopyOp));
3150 } else {
3151 setShadow(&I, getCleanShadow(&I));
3152 setOrigin(&I, getCleanOrigin());
3153 }
3154 }
3155
3156 // Given a scalar or vector, extract lower 64 bits (or less), and return all
3157 // zeroes if it is zero, and all ones otherwise.
3158 Value *Lower64ShadowExtend(IRBuilder<> &IRB, Value *S, Type *T) {
3159 if (S->getType()->isVectorTy())
3160 S = CreateShadowCast(IRB, S, IRB.getInt64Ty(), /* Signed */ true);
3161 assert(S->getType()->getPrimitiveSizeInBits() <= 64);
3162 Value *S2 = IRB.CreateICmpNE(S, getCleanShadow(S));
3163 return CreateShadowCast(IRB, S2, T, /* Signed */ true);
3164 }
3165
3166 // Given a vector, extract its first element, and return all
3167 // zeroes if it is zero, and all ones otherwise.
3168 Value *LowerElementShadowExtend(IRBuilder<> &IRB, Value *S, Type *T) {
3169 Value *S1 = IRB.CreateExtractElement(S, (uint64_t)0);
3170 Value *S2 = IRB.CreateICmpNE(S1, getCleanShadow(S1));
3171 return CreateShadowCast(IRB, S2, T, /* Signed */ true);
3172 }
3173
3174 Value *VariableShadowExtend(IRBuilder<> &IRB, Value *S) {
3175 Type *T = S->getType();
3176 assert(T->isVectorTy());
3177 Value *S2 = IRB.CreateICmpNE(S, getCleanShadow(S));
3178 return IRB.CreateSExt(S2, T);
3179 }
3180
3181 // Instrument vector shift intrinsic.
3182 //
3183 // This function instruments intrinsics like int_x86_avx2_psll_w.
3184 // Intrinsic shifts %In by %ShiftSize bits.
3185 // %ShiftSize may be a vector. In that case the lower 64 bits determine shift
3186 // size, and the rest is ignored. Behavior is defined even if shift size is
3187 // greater than register (or field) width.
3188 void handleVectorShiftIntrinsic(IntrinsicInst &I, bool Variable) {
3189 assert(I.arg_size() == 2);
3190 IRBuilder<> IRB(&I);
3191 // If any of the S2 bits are poisoned, the whole thing is poisoned.
3192 // Otherwise perform the same shift on S1.
3193 Value *S1 = getShadow(&I, 0);
3194 Value *S2 = getShadow(&I, 1);
3195 Value *S2Conv = Variable ? VariableShadowExtend(IRB, S2)
3196 : Lower64ShadowExtend(IRB, S2, getShadowTy(&I));
3197 Value *V1 = I.getOperand(0);
3198 Value *V2 = I.getOperand(1);
3199 Value *Shift = IRB.CreateCall(I.getFunctionType(), I.getCalledOperand(),
3200 {IRB.CreateBitCast(S1, V1->getType()), V2});
3201 Shift = IRB.CreateBitCast(Shift, getShadowTy(&I));
3202 setShadow(&I, IRB.CreateOr(Shift, S2Conv));
3203 setOriginForNaryOp(I);
3204 }
3205
3206 // Get an X86_MMX-sized vector type.
3207 Type *getMMXVectorTy(unsigned EltSizeInBits) {
3208 const unsigned X86_MMXSizeInBits = 64;
3209 assert(EltSizeInBits != 0 && (X86_MMXSizeInBits % EltSizeInBits) == 0 &&
3210 "Illegal MMX vector element size");
3211 return FixedVectorType::get(IntegerType::get(*MS.C, EltSizeInBits),
3212 X86_MMXSizeInBits / EltSizeInBits);
3213 }
3214
3215 // Returns a signed counterpart for an (un)signed-saturate-and-pack
3216 // intrinsic.
3217 Intrinsic::ID getSignedPackIntrinsic(Intrinsic::ID id) {
3218 switch (id) {
3219 case Intrinsic::x86_sse2_packsswb_128:
3220 case Intrinsic::x86_sse2_packuswb_128:
3221 return Intrinsic::x86_sse2_packsswb_128;
3222
3223 case Intrinsic::x86_sse2_packssdw_128:
3224 case Intrinsic::x86_sse41_packusdw:
3225 return Intrinsic::x86_sse2_packssdw_128;
3226
3227 case Intrinsic::x86_avx2_packsswb:
3228 case Intrinsic::x86_avx2_packuswb:
3229 return Intrinsic::x86_avx2_packsswb;
3230
3231 case Intrinsic::x86_avx2_packssdw:
3232 case Intrinsic::x86_avx2_packusdw:
3233 return Intrinsic::x86_avx2_packssdw;
3234
3235 case Intrinsic::x86_mmx_packsswb:
3236 case Intrinsic::x86_mmx_packuswb:
3237 return Intrinsic::x86_mmx_packsswb;
3238
3239 case Intrinsic::x86_mmx_packssdw:
3240 return Intrinsic::x86_mmx_packssdw;
3241 default:
3242 llvm_unreachable("unexpected intrinsic id");
3243 }
3244 }
3245
3246 // Instrument vector pack intrinsic.
3247 //
3248 // This function instruments intrinsics like x86_mmx_packsswb, that
3249 // packs elements of 2 input vectors into half as many bits with saturation.
3250 // Shadow is propagated with the signed variant of the same intrinsic applied
3251 // to sext(Sa != zeroinitializer), sext(Sb != zeroinitializer).
3252 // EltSizeInBits is used only for x86mmx arguments.
3253 void handleVectorPackIntrinsic(IntrinsicInst &I, unsigned EltSizeInBits = 0) {
3254 assert(I.arg_size() == 2);
3255 bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
3256 IRBuilder<> IRB(&I);
3257 Value *S1 = getShadow(&I, 0);
3258 Value *S2 = getShadow(&I, 1);
3259 assert(isX86_MMX || S1->getType()->isVectorTy());
3260
3261 // SExt and ICmpNE below must apply to individual elements of input vectors.
3262 // In case of x86mmx arguments, cast them to appropriate vector types and
3263 // back.
3264 Type *T = isX86_MMX ? getMMXVectorTy(EltSizeInBits) : S1->getType();
3265 if (isX86_MMX) {
3266 S1 = IRB.CreateBitCast(S1, T);
3267 S2 = IRB.CreateBitCast(S2, T);
3268 }
3269 Value *S1_ext =
3271 Value *S2_ext =
3273 if (isX86_MMX) {
3274 Type *X86_MMXTy = Type::getX86_MMXTy(*MS.C);
3275 S1_ext = IRB.CreateBitCast(S1_ext, X86_MMXTy);
3276 S2_ext = IRB.CreateBitCast(S2_ext, X86_MMXTy);
3277 }
3278
3280 F.getParent(), getSignedPackIntrinsic(I.getIntrinsicID()));
3281
3282 Value *S =
3283 IRB.CreateCall(ShadowFn, {S1_ext, S2_ext}, "_msprop_vector_pack");
3284 if (isX86_MMX)
3285 S = IRB.CreateBitCast(S, getShadowTy(&I));
3286 setShadow(&I, S);
3287 setOriginForNaryOp(I);
3288 }
3289
3290 // Instrument sum-of-absolute-differences intrinsic.
3291 void handleVectorSadIntrinsic(IntrinsicInst &I) {
3292 const unsigned SignificantBitsPerResultElement = 16;
3293 bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
3294 Type *ResTy = isX86_MMX ? IntegerType::get(*MS.C, 64) : I.getType();
3295 unsigned ZeroBitsPerResultElement =
3296 ResTy->getScalarSizeInBits() - SignificantBitsPerResultElement;
3297
3298 IRBuilder<> IRB(&I);
3299 auto *Shadow0 = getShadow(&I, 0);
3300 auto *Shadow1 = getShadow(&I, 1);
3301 Value *S = IRB.CreateOr(Shadow0, Shadow1);
3302 S = IRB.CreateBitCast(S, ResTy);
3303 S = IRB.CreateSExt(IRB.CreateICmpNE(S, Constant::getNullValue(ResTy)),
3304 ResTy);
3305 S = IRB.CreateLShr(S, ZeroBitsPerResultElement);
3306 S = IRB.CreateBitCast(S, getShadowTy(&I));
3307 setShadow(&I, S);
3308 setOriginForNaryOp(I);
3309 }
3310
3311 // Instrument multiply-add intrinsic.
3312 void handleVectorPmaddIntrinsic(IntrinsicInst &I,
3313 unsigned EltSizeInBits = 0) {
3314 bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
3315 Type *ResTy = isX86_MMX ? getMMXVectorTy(EltSizeInBits * 2) : I.getType();
3316 IRBuilder<> IRB(&I);
3317 auto *Shadow0 = getShadow(&I, 0);
3318 auto *Shadow1 = getShadow(&I, 1);
3319 Value *S = IRB.CreateOr(Shadow0, Shadow1);
3320 S = IRB.CreateBitCast(S, ResTy);
3321 S = IRB.CreateSExt(IRB.CreateICmpNE(S, Constant::getNullValue(ResTy)),
3322 ResTy);
3323 S = IRB.CreateBitCast(S, getShadowTy(&I));
3324 setShadow(&I, S);
3325 setOriginForNaryOp(I);
3326 }
3327
3328 // Instrument compare-packed intrinsic.
3329 // Basically, an or followed by sext(icmp ne 0) to end up with all-zeros or
3330 // all-ones shadow.
3331 void handleVectorComparePackedIntrinsic(IntrinsicInst &I) {
3332 IRBuilder<> IRB(&I);
3333 Type *ResTy = getShadowTy(&I);
3334 auto *Shadow0 = getShadow(&I, 0);
3335 auto *Shadow1 = getShadow(&I, 1);
3336 Value *S0 = IRB.CreateOr(Shadow0, Shadow1);
3337 Value *S = IRB.CreateSExt(
3338 IRB.CreateICmpNE(S0, Constant::getNullValue(ResTy)), ResTy);
3339 setShadow(&I, S);
3340 setOriginForNaryOp(I);
3341 }
3342
3343 // Instrument compare-scalar intrinsic.
3344 // This handles both cmp* intrinsics which return the result in the first
3345 // element of a vector, and comi* which return the result as i32.
3346 void handleVectorCompareScalarIntrinsic(IntrinsicInst &I) {
3347 IRBuilder<> IRB(&I);
3348 auto *Shadow0 = getShadow(&I, 0);
3349 auto *Shadow1 = getShadow(&I, 1);
3350 Value *S0 = IRB.CreateOr(Shadow0, Shadow1);
3351 Value *S = LowerElementShadowExtend(IRB, S0, getShadowTy(&I));
3352 setShadow(&I, S);
3353 setOriginForNaryOp(I);
3354 }
3355
3356 // Instrument generic vector reduction intrinsics
3357 // by ORing together all their fields.
3358 void handleVectorReduceIntrinsic(IntrinsicInst &I) {
3359 IRBuilder<> IRB(&I);
3360 Value *S = IRB.CreateOrReduce(getShadow(&I, 0));
3361 setShadow(&I, S);
3362 setOrigin(&I, getOrigin(&I, 0));
3363 }
3364
3365 // Instrument vector.reduce.or intrinsic.
3366 // Valid (non-poisoned) set bits in the operand pull low the
3367 // corresponding shadow bits.
3368 void handleVectorReduceOrIntrinsic(IntrinsicInst &I) {
3369 IRBuilder<> IRB(&I);
3370 Value *OperandShadow = getShadow(&I, 0);
3371 Value *OperandUnsetBits = IRB.CreateNot(I.getOperand(0));
3372 Value *OperandUnsetOrPoison = IRB.CreateOr(OperandUnsetBits, OperandShadow);
3373 // Bit N is clean if any field's bit N is 1 and unpoison
3374 Value *OutShadowMask = IRB.CreateAndReduce(OperandUnsetOrPoison);
3375 // Otherwise, it is clean if every field's bit N is unpoison
3376 Value *OrShadow = IRB.CreateOrReduce(OperandShadow);
3377 Value *S = IRB.CreateAnd(OutShadowMask, OrShadow);
3378
3379 setShadow(&I, S);
3380 setOrigin(&I, getOrigin(&I, 0));
3381 }
3382
3383 // Instrument vector.reduce.and intrinsic.
3384 // Valid (non-poisoned) unset bits in the operand pull down the
3385 // corresponding shadow bits.
3386 void handleVectorReduceAndIntrinsic(IntrinsicInst &I) {
3387 IRBuilder<> IRB(&I);
3388 Value *OperandShadow = getShadow(&I, 0);
3389 Value *OperandSetOrPoison = IRB.CreateOr(I.getOperand(0), OperandShadow);
3390 // Bit N is clean if any field's bit N is 0 and unpoison
3391 Value *OutShadowMask = IRB.CreateAndReduce(OperandSetOrPoison);
3392 // Otherwise, it is clean if every field's bit N is unpoison
3393 Value *OrShadow = IRB.CreateOrReduce(OperandShadow);
3394 Value *S = IRB.CreateAnd(OutShadowMask, OrShadow);
3395
3396 setShadow(&I, S);
3397 setOrigin(&I, getOrigin(&I, 0));
3398 }
3399
3400 void handleStmxcsr(IntrinsicInst &I) {
3401 IRBuilder<> IRB(&I);
3402 Value *Addr = I.getArgOperand(0);
3403 Type *Ty = IRB.getInt32Ty();
3404 Value *ShadowPtr =
3405 getShadowOriginPtr(Addr, IRB, Ty, Align(1), /*isStore*/ true).first;
3406
3407 IRB.CreateStore(getCleanShadow(Ty), ShadowPtr);
3408
3410 insertShadowCheck(Addr, &I);
3411 }
3412
3413 void handleLdmxcsr(IntrinsicInst &I) {
3414 if (!InsertChecks)
3415 return;
3416
3417 IRBuilder<> IRB(&I);
3418 Value *Addr = I.getArgOperand(0);
3419 Type *Ty = IRB.getInt32Ty();
3420 const Align Alignment = Align(1);
3421 Value *ShadowPtr, *OriginPtr;
3422 std::tie(ShadowPtr, OriginPtr) =
3423 getShadowOriginPtr(Addr, IRB, Ty, Alignment, /*isStore*/ false);
3424
3426 insertShadowCheck(Addr, &I);
3427
3428 Value *Shadow = IRB.CreateAlignedLoad(Ty, ShadowPtr, Alignment, "_ldmxcsr");
3429 Value *Origin = MS.TrackOrigins ? IRB.CreateLoad(MS.OriginTy, OriginPtr)
3430 : getCleanOrigin();
3431 insertShadowCheck(Shadow, Origin, &I);
3432 }
3433
3434 void handleMaskedExpandLoad(IntrinsicInst &I) {
3435 IRBuilder<> IRB(&I);
3436 Value *Ptr = I.getArgOperand(0);
3437 Value *Mask = I.getArgOperand(1);
3438 Value *PassThru = I.getArgOperand(2);
3439
3441 insertShadowCheck(Ptr, &I);
3442 insertShadowCheck(Mask, &I);
3443 }
3444
3445 if (!PropagateShadow) {
3446 setShadow(&I, getCleanShadow(&I));
3447 setOrigin(&I, getCleanOrigin());
3448 return;
3449 }
3450
3451 Type *ShadowTy = getShadowTy(&I);
3452 Type *ElementShadowTy = cast<VectorType>(ShadowTy)->getElementType();
3453 auto [ShadowPtr, OriginPtr] =
3454 getShadowOriginPtr(Ptr, IRB, ElementShadowTy, {}, /*isStore*/ false);
3455
3456 Value *Shadow = IRB.CreateMaskedExpandLoad(
3457 ShadowTy, ShadowPtr, Mask, getShadow(PassThru), "_msmaskedexpload");
3458
3459 setShadow(&I, Shadow);
3460
3461 // TODO: Store origins.
3462 setOrigin(&I, getCleanOrigin());
3463 }
3464
3465 void handleMaskedCompressStore(IntrinsicInst &I) {
3466 IRBuilder<> IRB(&I);
3467 Value *Values = I.getArgOperand(0);
3468 Value *Ptr = I.getArgOperand(1);
3469 Value *Mask = I.getArgOperand(2);
3470
3472 insertShadowCheck(Ptr, &I);
3473 insertShadowCheck(Mask, &I);
3474 }
3475
3476 Value *Shadow = getShadow(Values);
3477 Type *ElementShadowTy =
3478 getShadowTy(cast<VectorType>(Values->getType())->getElementType());
3479 auto [ShadowPtr, OriginPtrs] =
3480 getShadowOriginPtr(Ptr, IRB, ElementShadowTy, {}, /*isStore*/ true);
3481
3482 IRB.CreateMaskedCompressStore(Shadow, ShadowPtr, Mask);
3483
3484 // TODO: Store origins.
3485 }
3486
3487 void handleMaskedGather(IntrinsicInst &I) {
3488 IRBuilder<> IRB(&I);
3489 Value *Ptrs = I.getArgOperand(0);
3490 const Align Alignment(
3491 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
3492 Value *Mask = I.getArgOperand(2);
3493 Value *PassThru = I.getArgOperand(3);
3494
3495 Type *PtrsShadowTy = getShadowTy(Ptrs);
3497 insertShadowCheck(Mask, &I);
3498 Value *MaskedPtrShadow = IRB.CreateSelect(
3499 Mask, getShadow(Ptrs), Constant::getNullValue((PtrsShadowTy)),
3500 "_msmaskedptrs");
3501 insertShadowCheck(MaskedPtrShadow, getOrigin(Ptrs), &I);
3502 }
3503
3504 if (!PropagateShadow) {
3505 setShadow(&I, getCleanShadow(&I));
3506 setOrigin(&I, getCleanOrigin());
3507 return;
3508 }
3509
3510 Type *ShadowTy = getShadowTy(&I);
3511 Type *ElementShadowTy = cast<VectorType>(ShadowTy)->getElementType();
3512 auto [ShadowPtrs, OriginPtrs] = getShadowOriginPtr(
3513 Ptrs, IRB, ElementShadowTy, Alignment, /*isStore*/ false);
3514
3515 Value *Shadow =
3516 IRB.CreateMaskedGather(ShadowTy, ShadowPtrs, Alignment, Mask,
3517 getShadow(PassThru), "_msmaskedgather");
3518
3519 setShadow(&I, Shadow);
3520
3521 // TODO: Store origins.
3522 setOrigin(&I, getCleanOrigin());
3523 }
3524
3525 void handleMaskedScatter(IntrinsicInst &I) {
3526 IRBuilder<> IRB(&I);
3527 Value *Values = I.getArgOperand(0);
3528 Value *Ptrs = I.getArgOperand(1);
3529 const Align Alignment(
3530 cast<ConstantInt>(I.getArgOperand(2))->getZExtValue());
3531 Value *Mask = I.getArgOperand(3);
3532
3533 Type *PtrsShadowTy = getShadowTy(Ptrs);
3535 insertShadowCheck(Mask, &I);
3536 Value *MaskedPtrShadow = IRB.CreateSelect(
3537 Mask, getShadow(Ptrs), Constant::getNullValue((PtrsShadowTy)),
3538 "_msmaskedptrs");
3539 insertShadowCheck(MaskedPtrShadow, getOrigin(Ptrs), &I);
3540 }
3541
3542 Value *Shadow = getShadow(Values);
3543 Type *ElementShadowTy =
3544 getShadowTy(cast<VectorType>(Values->getType())->getElementType());
3545 auto [ShadowPtrs, OriginPtrs] = getShadowOriginPtr(
3546 Ptrs, IRB, ElementShadowTy, Alignment, /*isStore*/ true);
3547
3548 IRB.CreateMaskedScatter(Shadow, ShadowPtrs, Alignment, Mask);
3549
3550 // TODO: Store origin.
3551 }
3552
3553 void handleMaskedStore(IntrinsicInst &I) {
3554 IRBuilder<> IRB(&I);
3555 Value *V = I.getArgOperand(0);
3556 Value *Ptr = I.getArgOperand(1);
3557 const Align Alignment(
3558 cast<ConstantInt>(I.getArgOperand(2))->getZExtValue());
3559 Value *Mask = I.getArgOperand(3);
3560 Value *Shadow = getShadow(V);
3561
3563 insertShadowCheck(Ptr, &I);
3564 insertShadowCheck(Mask, &I);
3565 }
3566
3567 Value *ShadowPtr;
3568 Value *OriginPtr;
3569 std::tie(ShadowPtr, OriginPtr) = getShadowOriginPtr(
3570 Ptr, IRB, Shadow->getType(), Alignment, /*isStore*/ true);
3571
3572 IRB.CreateMaskedStore(Shadow, ShadowPtr, Alignment, Mask);
3573
3574 if (!MS.TrackOrigins)
3575 return;
3576
3577 auto &DL = F.getParent()->getDataLayout();
3578 paintOrigin(IRB, getOrigin(V), OriginPtr,
3579 DL.getTypeStoreSize(Shadow->getType()),
3580 std::max(Alignment, kMinOriginAlignment));
3581 }
3582
3583 void handleMaskedLoad(IntrinsicInst &I) {
3584 IRBuilder<> IRB(&I);
3585 Value *Ptr = I.getArgOperand(0);
3586 const Align Alignment(
3587 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
3588 Value *Mask = I.getArgOperand(2);
3589 Value *PassThru = I.getArgOperand(3);
3590
3592 insertShadowCheck(Ptr, &I);
3593 insertShadowCheck(Mask, &I);
3594 }
3595
3596 if (!PropagateShadow) {
3597 setShadow(&I, getCleanShadow(&I));
3598 setOrigin(&I, getCleanOrigin());
3599 return;
3600 }
3601
3602 Type *ShadowTy = getShadowTy(&I);
3603 Value *ShadowPtr, *OriginPtr;
3604 std::tie(ShadowPtr, OriginPtr) =
3605 getShadowOriginPtr(Ptr, IRB, ShadowTy, Alignment, /*isStore*/ false);
3606 setShadow(&I, IRB.CreateMaskedLoad(ShadowTy, ShadowPtr, Alignment, Mask,
3607 getShadow(PassThru), "_msmaskedld"));
3608
3609 if (!MS.TrackOrigins)
3610 return;
3611
3612 // Choose between PassThru's and the loaded value's origins.
3613 Value *MaskedPassThruShadow = IRB.CreateAnd(
3614 getShadow(PassThru), IRB.CreateSExt(IRB.CreateNeg(Mask), ShadowTy));
3615
3616 Value *NotNull = convertToBool(MaskedPassThruShadow, IRB, "_mscmp");
3617
3618 Value *PtrOrigin = IRB.CreateLoad(MS.OriginTy, OriginPtr);
3619 Value *Origin = IRB.CreateSelect(NotNull, getOrigin(PassThru), PtrOrigin);
3620
3621 setOrigin(&I, Origin);
3622 }
3623
3624 // Instrument BMI / BMI2 intrinsics.
3625 // All of these intrinsics are Z = I(X, Y)
3626 // where the types of all operands and the result match, and are either i32 or
3627 // i64. The following instrumentation happens to work for all of them:
3628 // Sz = I(Sx, Y) | (sext (Sy != 0))
3629 void handleBmiIntrinsic(IntrinsicInst &I) {
3630 IRBuilder<> IRB(&I);
3631 Type *ShadowTy = getShadowTy(&I);
3632
3633 // If any bit of the mask operand is poisoned, then the whole thing is.
3634 Value *SMask = getShadow(&I, 1);
3635 SMask = IRB.CreateSExt(IRB.CreateICmpNE(SMask, getCleanShadow(ShadowTy)),
3636 ShadowTy);
3637 // Apply the same intrinsic to the shadow of the first operand.
3638 Value *S = IRB.CreateCall(I.getCalledFunction(),
3639 {getShadow(&I, 0), I.getOperand(1)});
3640 S = IRB.CreateOr(SMask, S);
3641 setShadow(&I, S);
3642 setOriginForNaryOp(I);
3643 }
3644
3645 SmallVector<int, 8> getPclmulMask(unsigned Width, bool OddElements) {
3647 for (unsigned X = OddElements ? 1 : 0; X < Width; X += 2) {
3648 Mask.append(2, X);
3649 }
3650 return Mask;
3651 }
3652
3653 // Instrument pclmul intrinsics.
3654 // These intrinsics operate either on odd or on even elements of the input
3655 // vectors, depending on the constant in the 3rd argument, ignoring the rest.
3656 // Replace the unused elements with copies of the used ones, ex:
3657 // (0, 1, 2, 3) -> (0, 0, 2, 2) (even case)
3658 // or
3659 // (0, 1, 2, 3) -> (1, 1, 3, 3) (odd case)
3660 // and then apply the usual shadow combining logic.
3661 void handlePclmulIntrinsic(IntrinsicInst &I) {
3662 IRBuilder<> IRB(&I);
3663 unsigned Width =
3664 cast<FixedVectorType>(I.getArgOperand(0)->getType())->getNumElements();
3665 assert(isa<ConstantInt>(I.getArgOperand(2)) &&
3666 "pclmul 3rd operand must be a constant");
3667 unsigned Imm = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
3668 Value *Shuf0 = IRB.CreateShuffleVector(getShadow(&I, 0),
3669 getPclmulMask(Width, Imm & 0x01));
3670 Value *Shuf1 = IRB.CreateShuffleVector(getShadow(&I, 1),
3671 getPclmulMask(Width, Imm & 0x10));
3672 ShadowAndOriginCombiner SOC(this, IRB);
3673 SOC.Add(Shuf0, getOrigin(&I, 0));
3674 SOC.Add(Shuf1, getOrigin(&I, 1));
3675 SOC.Done(&I);
3676 }
3677
3678 // Instrument _mm_*_sd|ss intrinsics
3679 void handleUnarySdSsIntrinsic(IntrinsicInst &I) {
3680 IRBuilder<> IRB(&I);
3681 unsigned Width =
3682 cast<FixedVectorType>(I.getArgOperand(0)->getType())->getNumElements();
3683 Value *First = getShadow(&I, 0);
3684 Value *Second = getShadow(&I, 1);
3685 // First element of second operand, remaining elements of first operand
3687 Mask.push_back(Width);
3688 for (unsigned i = 1; i < Width; i++)
3689 Mask.push_back(i);
3690 Value *Shadow = IRB.CreateShuffleVector(First, Second, Mask);
3691
3692 setShadow(&I, Shadow);
3693 setOriginForNaryOp(I);
3694 }
3695
3696 void handleVtestIntrinsic(IntrinsicInst &I) {
3697 IRBuilder<> IRB(&I);
3698 Value *Shadow0 = getShadow(&I, 0);
3699 Value *Shadow1 = getShadow(&I, 1);
3700 Value *Or = IRB.CreateOr(Shadow0, Shadow1);
3701 Value *NZ = IRB.CreateICmpNE(Or, Constant::getNullValue(Or->getType()));
3702 Value *Scalar = convertShadowToScalar(NZ, IRB);
3703 Value *Shadow = IRB.CreateZExt(Scalar, getShadowTy(&I));
3704
3705 setShadow(&I, Shadow);
3706 setOriginForNaryOp(I);
3707 }
3708
3709 void handleBinarySdSsIntrinsic(IntrinsicInst &I) {
3710 IRBuilder<> IRB(&I);
3711 unsigned Width =
3712 cast<FixedVectorType>(I.getArgOperand(0)->getType())->getNumElements();
3713 Value *First = getShadow(&I, 0);
3714 Value *Second = getShadow(&I, 1);
3715 Value *OrShadow = IRB.CreateOr(First, Second);
3716 // First element of both OR'd together, remaining elements of first operand
3718 Mask.push_back(Width);
3719 for (unsigned i = 1; i < Width; i++)
3720 Mask.push_back(i);
3721 Value *Shadow = IRB.CreateShuffleVector(First, OrShadow, Mask);
3722
3723 setShadow(&I, Shadow);
3724 setOriginForNaryOp(I);
3725 }
3726
3727 // Instrument abs intrinsic.
3728 // handleUnknownIntrinsic can't handle it because of the last
3729 // is_int_min_poison argument which does not match the result type.
3730 void handleAbsIntrinsic(IntrinsicInst &I) {
3731 assert(I.getType()->isIntOrIntVectorTy());
3732 assert(I.getArgOperand(0)->getType() == I.getType());
3733
3734 // FIXME: Handle is_int_min_poison.
3735 IRBuilder<> IRB(&I);
3736 setShadow(&I, getShadow(&I, 0));
3737 setOrigin(&I, getOrigin(&I, 0));
3738 }
3739
3740 void handleIsFpClass(IntrinsicInst &I) {
3741 IRBuilder<> IRB(&I);
3742 Value *Shadow = getShadow(&I, 0);
3743 setShadow(&I, IRB.CreateICmpNE(Shadow, getCleanShadow(Shadow)));
3744 setOrigin(&I, getOrigin(&I, 0));
3745 }
3746
3747 void handleArithmeticWithOverflow(IntrinsicInst &I) {
3748 IRBuilder<> IRB(&I);
3749 Value *Shadow0 = getShadow(&I, 0);
3750 Value *Shadow1 = getShadow(&I, 1);
3751 Value *ShadowElt0 = IRB.CreateOr(Shadow0, Shadow1);
3752 Value *ShadowElt1 =
3753 IRB.CreateICmpNE(ShadowElt0, getCleanShadow(ShadowElt0));
3754
3755 Value *Shadow = PoisonValue::get(getShadowTy(&I));
3756 Shadow = IRB.CreateInsertValue(Shadow, ShadowElt0, 0);
3757 Shadow = IRB.CreateInsertValue(Shadow, ShadowElt1, 1);
3758
3759 setShadow(&I, Shadow);
3760 setOriginForNaryOp(I);
3761 }
3762
3763 void visitIntrinsicInst(IntrinsicInst &I) {
3764 switch (I.getIntrinsicID()) {
3765 case Intrinsic::uadd_with_overflow:
3766 case Intrinsic::sadd_with_overflow:
3767 case Intrinsic::usub_with_overflow:
3768 case Intrinsic::ssub_with_overflow:
3769 case Intrinsic::umul_with_overflow:
3770 case Intrinsic::smul_with_overflow:
3771 handleArithmeticWithOverflow(I);
3772 break;
3773 case Intrinsic::abs:
3774 handleAbsIntrinsic(I);
3775 break;
3776 case Intrinsic::is_fpclass:
3777 handleIsFpClass(I);
3778 break;
3779 case Intrinsic::lifetime_start:
3780 handleLifetimeStart(I);
3781 break;
3782 case Intrinsic::launder_invariant_group:
3783 case Intrinsic::strip_invariant_group:
3784 handleInvariantGroup(I);
3785 break;
3786 case Intrinsic::bswap:
3787 handleBswap(I);
3788 break;
3789 case Intrinsic::ctlz:
3790 case Intrinsic::cttz:
3791 handleCountZeroes(I);
3792 break;
3793 case Intrinsic::masked_compressstore:
3794 handleMaskedCompressStore(I);
3795 break;
3796 case Intrinsic::masked_expandload:
3797 handleMaskedExpandLoad(I);
3798 break;
3799 case Intrinsic::masked_gather:
3800 handleMaskedGather(I);
3801 break;
3802 case Intrinsic::masked_scatter:
3803 handleMaskedScatter(I);
3804 break;
3805 case Intrinsic::masked_store:
3806 handleMaskedStore(I);
3807 break;
3808 case Intrinsic::masked_load:
3809 handleMaskedLoad(I);
3810 break;
3811 case Intrinsic::vector_reduce_and:
3812 handleVectorReduceAndIntrinsic(I);
3813 break;
3814 case Intrinsic::vector_reduce_or:
3815 handleVectorReduceOrIntrinsic(I);
3816 break;
3817 case Intrinsic::vector_reduce_add:
3818 case Intrinsic::vector_reduce_xor:
3819 case Intrinsic::vector_reduce_mul:
3820 handleVectorReduceIntrinsic(I);
3821 break;
3822 case Intrinsic::x86_sse_stmxcsr:
3823 handleStmxcsr(I);
3824 break;
3825 case Intrinsic::x86_sse_ldmxcsr:
3826 handleLdmxcsr(I);
3827 break;
3828 case Intrinsic::x86_avx512_vcvtsd2usi64:
3829 case Intrinsic::x86_avx512_vcvtsd2usi32:
3830 case Intrinsic::x86_avx512_vcvtss2usi64:
3831 case Intrinsic::x86_avx512_vcvtss2usi32:
3832 case Intrinsic::x86_avx512_cvttss2usi64:
3833 case Intrinsic::x86_avx512_cvttss2usi:
3834 case Intrinsic::x86_avx512_cvttsd2usi64:
3835 case Intrinsic::x86_avx512_cvttsd2usi:
3836 case Intrinsic::x86_avx512_cvtusi2ss:
3837 case Intrinsic::x86_avx512_cvtusi642sd:
3838 case Intrinsic::x86_avx512_cvtusi642ss:
3839 handleVectorConvertIntrinsic(I, 1, true);
3840 break;
3841 case Intrinsic::x86_sse2_cvtsd2si64:
3842 case Intrinsic::x86_sse2_cvtsd2si:
3843 case Intrinsic::x86_sse2_cvtsd2ss:
3844 case Intrinsic::x86_sse2_cvttsd2si64:
3845 case Intrinsic::x86_sse2_cvttsd2si:
3846 case Intrinsic::x86_sse_cvtss2si64:
3847 case Intrinsic::x86_sse_cvtss2si:
3848 case Intrinsic::x86_sse_cvttss2si64:
3849 case Intrinsic::x86_sse_cvttss2si:
3850 handleVectorConvertIntrinsic(I, 1);
3851 break;
3852 case Intrinsic::x86_sse_cvtps2pi:
3853 case Intrinsic::x86_sse_cvttps2pi:
3854 handleVectorConvertIntrinsic(I, 2);
3855 break;
3856
3857 case Intrinsic::x86_avx512_psll_w_512:
3858 case Intrinsic::x86_avx512_psll_d_512:
3859 case Intrinsic::x86_avx512_psll_q_512:
3860 case Intrinsic::x86_avx512_pslli_w_512:
3861 case Intrinsic::x86_avx512_pslli_d_512:
3862 case Intrinsic::x86_avx512_pslli_q_512:
3863 case Intrinsic::x86_avx512_psrl_w_512:
3864 case Intrinsic::x86_avx512_psrl_d_512:
3865 case Intrinsic::x86_avx512_psrl_q_512:
3866 case Intrinsic::x86_avx512_psra_w_512:
3867 case Intrinsic::x86_avx512_psra_d_512:
3868 case Intrinsic::x86_avx512_psra_q_512:
3869 case Intrinsic::x86_avx512_psrli_w_512:
3870 case Intrinsic::x86_avx512_psrli_d_512:
3871 case Intrinsic::x86_avx512_psrli_q_512:
3872 case Intrinsic::x86_avx512_psrai_w_512:
3873 case Intrinsic::x86_avx512_psrai_d_512:
3874 case Intrinsic::x86_avx512_psrai_q_512:
3875 case Intrinsic::x86_avx512_psra_q_256:
3876 case Intrinsic::x86_avx512_psra_q_128:
3877 case Intrinsic::x86_avx512_psrai_q_256:
3878 case Intrinsic::x86_avx512_psrai_q_128:
3879 case Intrinsic::x86_avx2_psll_w:
3880 case Intrinsic::x86_avx2_psll_d:
3881 case Intrinsic::x86_avx2_psll_q:
3882 case Intrinsic::x86_avx2_pslli_w:
3883 case Intrinsic::x86_avx2_pslli_d:
3884 case Intrinsic::x86_avx2_pslli_q:
3885 case Intrinsic::x86_avx2_psrl_w:
3886 case Intrinsic::x86_avx2_psrl_d:
3887 case Intrinsic::x86_avx2_psrl_q:
3888 case Intrinsic::x86_avx2_psra_w:
3889 case Intrinsic::x86_avx2_psra_d:
3890 case Intrinsic::x86_avx2_psrli_w:
3891 case Intrinsic::x86_avx2_psrli_d:
3892 case Intrinsic::x86_avx2_psrli_q:
3893 case Intrinsic::x86_avx2_psrai_w:
3894 case Intrinsic::x86_avx2_psrai_d:
3895 case Intrinsic::x86_sse2_psll_w:
3896 case Intrinsic::x86_sse2_psll_d:
3897 case Intrinsic::x86_sse2_psll_q:
3898 case Intrinsic::x86_sse2_pslli_w:
3899 case Intrinsic::x86_sse2_pslli_d:
3900 case Intrinsic::x86_sse2_pslli_q:
3901 case Intrinsic::x86_sse2_psrl_w:
3902 case Intrinsic::x86_sse2_psrl_d:
3903 case Intrinsic::x86_sse2_psrl_q:
3904 case Intrinsic::x86_sse2_psra_w:
3905 case Intrinsic::x86_sse2_psra_d:
3906 case Intrinsic::x86_sse2_psrli_w:
3907 case Intrinsic::x86_sse2_psrli_d:
3908 case Intrinsic::x86_sse2_psrli_q:
3909 case Intrinsic::x86_sse2_psrai_w:
3910 case Intrinsic::x86_sse2_psrai_d:
3911 case Intrinsic::x86_mmx_psll_w:
3912 case Intrinsic::x86_mmx_psll_d:
3913 case Intrinsic::x86_mmx_psll_q:
3914 case Intrinsic::x86_mmx_pslli_w:
3915 case Intrinsic::x86_mmx_pslli_d:
3916 case Intrinsic::x86_mmx_pslli_q:
3917 case Intrinsic::x86_mmx_psrl_w:
3918 case Intrinsic::x86_mmx_psrl_d:
3919 case Intrinsic::x86_mmx_psrl_q:
3920 case Intrinsic::x86_mmx_psra_w:
3921 case Intrinsic::x86_mmx_psra_d:
3922 case Intrinsic::x86_mmx_psrli_w:
3923 case Intrinsic::x86_mmx_psrli_d:
3924 case Intrinsic::x86_mmx_psrli_q:
3925 case Intrinsic::x86_mmx_psrai_w:
3926 case Intrinsic::x86_mmx_psrai_d:
3927 handleVectorShiftIntrinsic(I, /* Variable */ false);
3928 break;
3929 case Intrinsic::x86_avx2_psllv_d:
3930 case Intrinsic::x86_avx2_psllv_d_256:
3931 case Intrinsic::x86_avx512_psllv_d_512:
3932 case Intrinsic::x86_avx2_psllv_q:
3933 case Intrinsic::x86_avx2_psllv_q_256:
3934 case Intrinsic::x86_avx512_psllv_q_512:
3935 case Intrinsic::x86_avx2_psrlv_d:
3936 case Intrinsic::x86_avx2_psrlv_d_256:
3937 case Intrinsic::x86_avx512_psrlv_d_512:
3938 case Intrinsic::x86_avx2_psrlv_q:
3939 case Intrinsic::x86_avx2_psrlv_q_256:
3940 case Intrinsic::x86_avx512_psrlv_q_512:
3941 case Intrinsic::x86_avx2_psrav_d:
3942 case Intrinsic::x86_avx2_psrav_d_256:
3943 case Intrinsic::x86_avx512_psrav_d_512:
3944 case Intrinsic::x86_avx512_psrav_q_128:
3945 case Intrinsic::x86_avx512_psrav_q_256:
3946 case Intrinsic::x86_avx512_psrav_q_512:
3947 handleVectorShiftIntrinsic(I, /* Variable */ true);
3948 break;
3949
3950 case Intrinsic::x86_sse2_packsswb_128:
3951 case Intrinsic::x86_sse2_packssdw_128:
3952 case Intrinsic::x86_sse2_packuswb_128:
3953 case Intrinsic::x86_sse41_packusdw:
3954 case Intrinsic::x86_avx2_packsswb:
3955 case Intrinsic::x86_avx2_packssdw:
3956 case Intrinsic::x86_avx2_packuswb:
3957 case Intrinsic::x86_avx2_packusdw:
3958 handleVectorPackIntrinsic(I);
3959 break;
3960
3961 case Intrinsic::x86_mmx_packsswb:
3962 case Intrinsic::x86_mmx_packuswb:
3963 handleVectorPackIntrinsic(I, 16);
3964 break;
3965
3966 case Intrinsic::x86_mmx_packssdw:
3967 handleVectorPackIntrinsic(I, 32);
3968 break;
3969
3970 case Intrinsic::x86_mmx_psad_bw:
3971 case Intrinsic::x86_sse2_psad_bw:
3972 case Intrinsic::x86_avx2_psad_bw:
3973 handleVectorSadIntrinsic(I);
3974 break;
3975
3976 case Intrinsic::x86_sse2_pmadd_wd:
3977 case Intrinsic::x86_avx2_pmadd_wd:
3978 case Intrinsic::x86_ssse3_pmadd_ub_sw_128:
3979 case Intrinsic::x86_avx2_pmadd_ub_sw:
3980 handleVectorPmaddIntrinsic(I);
3981 break;
3982
3983 case Intrinsic::x86_ssse3_pmadd_ub_sw:
3984 handleVectorPmaddIntrinsic(I, 8);
3985 break;
3986
3987 case Intrinsic::x86_mmx_pmadd_wd:
3988 handleVectorPmaddIntrinsic(I, 16);
3989 break;
3990
3991 case Intrinsic::x86_sse_cmp_ss:
3992 case Intrinsic::x86_sse2_cmp_sd:
3993 case Intrinsic::x86_sse_comieq_ss:
3994 case Intrinsic::x86_sse_comilt_ss:
3995 case Intrinsic::x86_sse_comile_ss:
3996 case Intrinsic::x86_sse_comigt_ss:
3997 case Intrinsic::x86_sse_comige_ss:
3998 case Intrinsic::x86_sse_comineq_ss:
3999 case Intrinsic::x86_sse_ucomieq_ss:
4000 case Intrinsic::x86_sse_ucomilt_ss:
4001 case Intrinsic::x86_sse_ucomile_ss:
4002 case Intrinsic::x86_sse_ucomigt_ss:
4003 case Intrinsic::x86_sse_ucomige_ss:
4004 case Intrinsic::x86_sse_ucomineq_ss:
4005 case Intrinsic::x86_sse2_comieq_sd:
4006 case Intrinsic::x86_sse2_comilt_sd:
4007 case Intrinsic::x86_sse2_comile_sd:
4008 case Intrinsic::x86_sse2_comigt_sd:
4009 case Intrinsic::x86_sse2_comige_sd:
4010 case Intrinsic::x86_sse2_comineq_sd:
4011 case Intrinsic::x86_sse2_ucomieq_sd:
4012 case Intrinsic::x86_sse2_ucomilt_sd:
4013 case Intrinsic::x86_sse2_ucomile_sd:
4014 case Intrinsic::x86_sse2_ucomigt_sd:
4015 case Intrinsic::x86_sse2_ucomige_sd:
4016 case Intrinsic::x86_sse2_ucomineq_sd:
4017 handleVectorCompareScalarIntrinsic(I);
4018 break;
4019
4020 case Intrinsic::x86_avx_cmp_pd_256:
4021 case Intrinsic::x86_avx_cmp_ps_256:
4022 case Intrinsic::x86_sse2_cmp_pd:
4023 case Intrinsic::x86_sse_cmp_ps:
4024 handleVectorComparePackedIntrinsic(I);
4025 break;
4026
4027 case Intrinsic::x86_bmi_bextr_32:
4028 case Intrinsic::x86_bmi_bextr_64:
4029 case Intrinsic::x86_bmi_bzhi_32:
4030 case Intrinsic::x86_bmi_bzhi_64:
4031 case Intrinsic::x86_bmi_pdep_32:
4032 case Intrinsic::x86_bmi_pdep_64:
4033 case Intrinsic::x86_bmi_pext_32:
4034 case Intrinsic::x86_bmi_pext_64:
4035 handleBmiIntrinsic(I);
4036 break;
4037
4038 case Intrinsic::x86_pclmulqdq:
4039 case Intrinsic::x86_pclmulqdq_256:
4040 case Intrinsic::x86_pclmulqdq_512:
4041 handlePclmulIntrinsic(I);
4042 break;
4043
4044 case Intrinsic::x86_sse41_round_sd:
4045 case Intrinsic::x86_sse41_round_ss:
4046 handleUnarySdSsIntrinsic(I);
4047 break;
4048 case Intrinsic::x86_sse2_max_sd:
4049 case Intrinsic::x86_sse_max_ss:
4050 case Intrinsic::x86_sse2_min_sd:
4051 case Intrinsic::x86_sse_min_ss:
4052 handleBinarySdSsIntrinsic(I);
4053 break;
4054
4055 case Intrinsic::x86_avx_vtestc_pd:
4056 case Intrinsic::x86_avx_vtestc_pd_256:
4057 case Intrinsic::x86_avx_vtestc_ps:
4058 case Intrinsic::x86_avx_vtestc_ps_256:
4059 case Intrinsic::x86_avx_vtestnzc_pd:
4060 case Intrinsic::x86_avx_vtestnzc_pd_256:
4061 case Intrinsic::x86_avx_vtestnzc_ps:
4062 case Intrinsic::x86_avx_vtestnzc_ps_256:
4063 case Intrinsic::x86_avx_vtestz_pd:
4064 case Intrinsic::x86_avx_vtestz_pd_256:
4065 case Intrinsic::x86_avx_vtestz_ps:
4066 case Intrinsic::x86_avx_vtestz_ps_256:
4067 case Intrinsic::x86_avx_ptestc_256:
4068 case Intrinsic::x86_avx_ptestnzc_256:
4069 case Intrinsic::x86_avx_ptestz_256:
4070 case Intrinsic::x86_sse41_ptestc:
4071 case Intrinsic::x86_sse41_ptestnzc:
4072 case Intrinsic::x86_sse41_ptestz:
4073 handleVtestIntrinsic(I);
4074 break;
4075
4076 case Intrinsic::fshl:
4077 case Intrinsic::fshr:
4078 handleFunnelShift(I);
4079 break;
4080
4081 case Intrinsic::is_constant:
4082 // The result of llvm.is.constant() is always defined.
4083 setShadow(&I, getCleanShadow(&I));
4084 setOrigin(&I, getCleanOrigin());
4085 break;
4086
4087 default:
4088 if (!handleUnknownIntrinsic(I))
4089 visitInstruction(I);
4090 break;
4091 }
4092 }
4093
4094 void visitLibAtomicLoad(CallBase &CB) {
4095 // Since we use getNextNode here, we can't have CB terminate the BB.
4096 assert(isa<CallInst>(CB));
4097
4098 IRBuilder<> IRB(&CB);
4099 Value *Size = CB.getArgOperand(0);
4100 Value *SrcPtr = CB.getArgOperand(1);
4101 Value *DstPtr = CB.getArgOperand(2);
4102 Value *Ordering = CB.getArgOperand(3);
4103 // Convert the call to have at least Acquire ordering to make sure
4104 // the shadow operations aren't reordered before it.
4105 Value *NewOrdering =
4106 IRB.CreateExtractElement(makeAddAcquireOrderingTable(IRB), Ordering);
4107 CB.setArgOperand(3, NewOrdering);
4108
4109 NextNodeIRBuilder NextIRB(&CB);
4110 Value *SrcShadowPtr, *SrcOriginPtr;
4111 std::tie(SrcShadowPtr, SrcOriginPtr) =
4112 getShadowOriginPtr(SrcPtr, NextIRB, NextIRB.getInt8Ty(), Align(1),
4113 /*isStore*/ false);
4114 Value *DstShadowPtr =
4115 getShadowOriginPtr(DstPtr, NextIRB, NextIRB.getInt8Ty(), Align(1),
4116 /*isStore*/ true)
4117 .first;
4118
4119 NextIRB.CreateMemCpy(DstShadowPtr, Align(1), SrcShadowPtr, Align(1), Size);
4120 if (MS.TrackOrigins) {
4121 Value *SrcOrigin = NextIRB.CreateAlignedLoad(MS.OriginTy, SrcOriginPtr,
4123 Value *NewOrigin = updateOrigin(SrcOrigin, NextIRB);
4124 NextIRB.CreateCall(MS.MsanSetOriginFn, {DstPtr, Size, NewOrigin});
4125 }
4126 }
4127
4128 void visitLibAtomicStore(CallBase &CB) {
4129 IRBuilder<> IRB(&CB);
4130 Value *Size = CB.getArgOperand(0);
4131 Value *DstPtr = CB.getArgOperand(2);
4132 Value *Ordering = CB.getArgOperand(3);
4133 // Convert the call to have at least Release ordering to make sure
4134 // the shadow operations aren't reordered after it.
4135 Value *NewOrdering =
4136 IRB.CreateExtractElement(makeAddReleaseOrderingTable(IRB), Ordering);
4137 CB.setArgOperand(3, NewOrdering);
4138
4139 Value *DstShadowPtr =
4140 getShadowOriginPtr(DstPtr, IRB, IRB.getInt8Ty(), Align(1),
4141 /*isStore*/ true)
4142 .first;
4143
4144 // Atomic store always paints clean shadow/origin. See file header.
4145 IRB.CreateMemSet(DstShadowPtr, getCleanShadow(IRB.getInt8Ty()), Size,
4146 Align(1));
4147 }
4148
4149 void visitCallBase(CallBase &CB) {
4150 assert(!CB.getMetadata(LLVMContext::MD_nosanitize));
4151 if (CB.isInlineAsm()) {
4152 // For inline asm (either a call to asm function, or callbr instruction),
4153 // do the usual thing: check argument shadow and mark all outputs as
4154 // clean. Note that any side effects of the inline asm that are not
4155 // immediately visible in its constraints are not handled.
4157 visitAsmInstruction(CB);
4158 else
4159 visitInstruction(CB);
4160 return;
4161 }
4162 LibFunc LF;
4163 if (TLI->getLibFunc(CB, LF)) {
4164 // libatomic.a functions need to have special handling because there isn't
4165 // a good way to intercept them or compile the library with
4166 // instrumentation.
4167 switch (LF) {
4168 case LibFunc_atomic_load:
4169 if (!isa<CallInst>(CB)) {
4170 llvm::errs() << "MSAN -- cannot instrument invoke of libatomic load."
4171 "Ignoring!\n";
4172 break;
4173 }
4174 visitLibAtomicLoad(CB);
4175 return;
4176 case LibFunc_atomic_store:
4177 visitLibAtomicStore(CB);
4178 return;
4179 default:
4180 break;
4181 }
4182 }
4183
4184 if (auto *Call = dyn_cast<CallInst>(&CB)) {
4185 assert(!isa<IntrinsicInst>(Call) && "intrinsics are handled elsewhere");
4186
4187 // We are going to insert code that relies on the fact that the callee
4188 // will become a non-readonly function after it is instrumented by us. To
4189 // prevent this code from being optimized out, mark that function
4190 // non-readonly in advance.
4191 // TODO: We can likely do better than dropping memory() completely here.
4193 B.addAttribute(Attribute::Memory).addAttribute(Attribute::Speculatable);
4194
4195 Call->removeFnAttrs(B);
4196 if (Function *Func = Call->getCalledFunction()) {
4197 Func->removeFnAttrs(B);
4198 }
4199
4201 }
4202 IRBuilder<> IRB(&CB);
4203 bool MayCheckCall = MS.EagerChecks;
4204 if (Function *Func = CB.getCalledFunction()) {
4205 // __sanitizer_unaligned_{load,store} functions may be called by users
4206 // and always expects shadows in the TLS. So don't check them.
4207 MayCheckCall &= !Func->getName().starts_with("__sanitizer_unaligned_");
4208 }
4209
4210 unsigned ArgOffset = 0;
4211 LLVM_DEBUG(dbgs() << " CallSite: " << CB << "\n");
4212 for (const auto &[i, A] : llvm::enumerate(CB.args())) {
4213 if (!A->getType()->isSized()) {
4214 LLVM_DEBUG(dbgs() << "Arg " << i << " is not sized: " << CB << "\n");
4215 continue;
4216 }
4217
4218 if (A->getType()->isScalableTy()) {
4219 LLVM_DEBUG(dbgs() << "Arg " << i << " is vscale: " << CB << "\n");
4220 // Handle as noundef, but don't reserve tls slots.
4221 insertShadowCheck(A, &CB);
4222 continue;
4223 }
4224
4225 unsigned Size = 0;
4226 const DataLayout &DL = F.getParent()->getDataLayout();
4227
4228 bool ByVal = CB.paramHasAttr(i, Attribute::ByVal);
4229 bool NoUndef = CB.paramHasAttr(i, Attribute::NoUndef);
4230 bool EagerCheck = MayCheckCall && !ByVal && NoUndef;
4231
4232 if (EagerCheck) {
4233 insertShadowCheck(A, &CB);
4234 Size = DL.getTypeAllocSize(A->getType());
4235 } else {
4236 Value *Store = nullptr;
4237 // Compute the Shadow for arg even if it is ByVal, because
4238 // in that case getShadow() will copy the actual arg shadow to
4239 // __msan_param_tls.
4240 Value *ArgShadow = getShadow(A);
4241 Value *ArgShadowBase = getShadowPtrForArgument(IRB, ArgOffset);
4242 LLVM_DEBUG(dbgs() << " Arg#" << i << ": " << *A
4243 << " Shadow: " << *ArgShadow << "\n");
4244 if (ByVal) {
4245 // ByVal requires some special handling as it's too big for a single
4246 // load
4247 assert(A->getType()->isPointerTy() &&
4248 "ByVal argument is not a pointer!");
4249 Size = DL.getTypeAllocSize(CB.getParamByValType(i));
4250 if (ArgOffset + Size > kParamTLSSize)
4251 break;
4252 const MaybeAlign ParamAlignment(CB.getParamAlign(i));
4253 MaybeAlign Alignment = std::nullopt;
4254 if (ParamAlignment)
4255 Alignment = std::min(*ParamAlignment, kShadowTLSAlignment);
4256 Value *AShadowPtr, *AOriginPtr;
4257 std::tie(AShadowPtr, AOriginPtr) =
4258 getShadowOriginPtr(A, IRB, IRB.getInt8Ty(), Alignment,
4259 /*isStore*/ false);
4260 if (!PropagateShadow) {
4261 Store = IRB.CreateMemSet(ArgShadowBase,
4263 Size, Alignment);
4264 } else {
4265 Store = IRB.CreateMemCpy(ArgShadowBase, Alignment, AShadowPtr,
4266 Alignment, Size);
4267 if (MS.TrackOrigins) {
4268 Value *ArgOriginBase = getOriginPtrForArgument(IRB, ArgOffset);
4269 // FIXME: OriginSize should be:
4270 // alignTo(A % kMinOriginAlignment + Size, kMinOriginAlignment)
4271 unsigned OriginSize = alignTo(Size, kMinOriginAlignment);
4272 IRB.CreateMemCpy(
4273 ArgOriginBase,
4274 /* by origin_tls[ArgOffset] */ kMinOriginAlignment,
4275 AOriginPtr,
4276 /* by getShadowOriginPtr */ kMinOriginAlignment, OriginSize);
4277 }
4278 }
4279 } else {
4280 // Any other parameters mean we need bit-grained tracking of uninit
4281 // data
4282 Size = DL.getTypeAllocSize(A->getType());
4283 if (ArgOffset + Size > kParamTLSSize)
4284 break;
4285 Store = IRB.CreateAlignedStore(ArgShadow, ArgShadowBase,
4287 Constant *Cst = dyn_cast<Constant>(ArgShadow);
4288 if (MS.TrackOrigins && !(Cst && Cst->isNullValue())) {
4289 IRB.CreateStore(getOrigin(A),
4290 getOriginPtrForArgument(IRB, ArgOffset));
4291 }
4292 }
4293 (void)Store;
4294 assert(Store != nullptr);
4295 LLVM_DEBUG(dbgs() << " Param:" << *Store << "\n");
4296 }
4297 assert(Size != 0);
4298 ArgOffset += alignTo(Size, kShadowTLSAlignment);
4299 }
4300 LLVM_DEBUG(dbgs() << " done with call args\n");
4301
4302 FunctionType *FT = CB.getFunctionType();
4303 if (FT->isVarArg()) {
4304 VAHelper->visitCallBase(CB, IRB);
4305 }
4306
4307 // Now, get the shadow for the RetVal.
4308 if (!CB.getType()->isSized())
4309 return;
4310 // Don't emit the epilogue for musttail call returns.
4311 if (isa<CallInst>(CB) && cast<CallInst>(CB).isMustTailCall())
4312 return;
4313
4314 if (MayCheckCall && CB.hasRetAttr(Attribute::NoUndef)) {
4315 setShadow(&CB, getCleanShadow(&CB));
4316 setOrigin(&CB, getCleanOrigin());
4317 return;
4318 }
4319
4320 IRBuilder<> IRBBefore(&CB);
4321 // Until we have full dynamic coverage, make sure the retval shadow is 0.
4322 Value *Base = getShadowPtrForRetval(IRBBefore);
4323 IRBBefore.CreateAlignedStore(getCleanShadow(&CB), Base,
4325 BasicBlock::iterator NextInsn;
4326 if (isa<CallInst>(CB)) {
4327 NextInsn = ++CB.getIterator();
4328 assert(NextInsn != CB.getParent()->end());
4329 } else {
4330 BasicBlock *NormalDest = cast<InvokeInst>(CB).getNormalDest();
4331 if (!NormalDest->getSinglePredecessor()) {
4332 // FIXME: this case is tricky, so we are just conservative here.
4333 // Perhaps we need to split the edge between this BB and NormalDest,
4334 // but a naive attempt to use SplitEdge leads to a crash.
4335 setShadow(&CB, getCleanShadow(&CB));
4336 setOrigin(&CB, getCleanOrigin());
4337 return;
4338 }
4339 // FIXME: NextInsn is likely in a basic block that has not been visited
4340 // yet. Anything inserted there will be instrumented by MSan later!
4341 NextInsn = NormalDest->getFirstInsertionPt();
4342 assert(NextInsn != NormalDest->end() &&
4343 "Could not find insertion point for retval shadow load");
4344 }
4345 IRBuilder<> IRBAfter(&*NextInsn);
4346 Value *RetvalShadow = IRBAfter.CreateAlignedLoad(
4347 getShadowTy(&CB), getShadowPtrForRetval(IRBAfter),
4348 kShadowTLSAlignment, "_msret");
4349 setShadow(&CB, RetvalShadow);
4350 if (MS.TrackOrigins)
4351 setOrigin(&CB, IRBAfter.CreateLoad(MS.OriginTy,
4352 getOriginPtrForRetval()));
4353 }
4354
4355 bool isAMustTailRetVal(Value *RetVal) {
4356 if (auto *I = dyn_cast<BitCastInst>(RetVal)) {
4357 RetVal = I->getOperand(0);
4358 }
4359 if (auto *I = dyn_cast<CallInst>(RetVal)) {
4360 return I->isMustTailCall();
4361 }
4362 return false;
4363 }
4364
4365 void visitReturnInst(ReturnInst &I) {
4366 IRBuilder<> IRB(&I);
4367 Value *RetVal = I.getReturnValue();
4368 if (!RetVal)
4369 return;
4370 // Don't emit the epilogue for musttail call returns.
4371 if (isAMustTailRetVal(RetVal))
4372 return;
4373 Value *ShadowPtr = getShadowPtrForRetval(IRB);
4374 bool HasNoUndef = F.hasRetAttribute(Attribute::NoUndef);
4375 bool StoreShadow = !(MS.EagerChecks && HasNoUndef);
4376 // FIXME: Consider using SpecialCaseList to specify a list of functions that
4377 // must always return fully initialized values. For now, we hardcode "main".
4378 bool EagerCheck = (MS.EagerChecks && HasNoUndef) || (F.getName() == "main");
4379
4380 Value *Shadow = getShadow(RetVal);
4381 bool StoreOrigin = true;
4382 if (EagerCheck) {
4383 insertShadowCheck(RetVal, &I);
4384 Shadow = getCleanShadow(RetVal);
4385 StoreOrigin = false;
4386 }
4387
4388 // The caller may still expect information passed over TLS if we pass our
4389 // check
4390 if (StoreShadow) {
4391 IRB.CreateAlignedStore(Shadow, ShadowPtr, kShadowTLSAlignment);
4392 if (MS.TrackOrigins && StoreOrigin)
4393 IRB.CreateStore(getOrigin(RetVal), getOriginPtrForRetval());
4394 }
4395 }
4396
4397 void visitPHINode(PHINode &I) {
4398 IRBuilder<> IRB(&I);
4399 if (!PropagateShadow) {
4400 setShadow(&I, getCleanShadow(&I));
4401 setOrigin(&I, getCleanOrigin());
4402 return;
4403 }
4404
4405 ShadowPHINodes.push_back(&I);
4406 setShadow(&I, IRB.CreatePHI(getShadowTy(&I), I.getNumIncomingValues(),
4407 "_msphi_s"));
4408 if (MS.TrackOrigins)
4409 setOrigin(
4410 &I, IRB.CreatePHI(MS.OriginTy, I.getNumIncomingValues(), "_msphi_o"));
4411 }
4412
4413 Value *getLocalVarIdptr(AllocaInst &I) {
4414 ConstantInt *IntConst =
4415 ConstantInt::get(Type::getInt32Ty((*F.getParent()).getContext()), 0);
4416 return new GlobalVariable(*F.getParent(), IntConst->getType(),
4417 /*isConstant=*/false, GlobalValue::PrivateLinkage,
4418 IntConst);
4419 }
4420
4421 Value *getLocalVarDescription(AllocaInst &I) {
4422 return createPrivateConstGlobalForString(*F.getParent(), I.getName());
4423 }
4424
4425 void poisonAllocaUserspace(AllocaInst &I, IRBuilder<> &IRB, Value *Len) {
4426 if (PoisonStack && ClPoisonStackWithCall) {
4427 IRB.CreateCall(MS.MsanPoisonStackFn, {&I, Len});
4428 } else {
4429 Value *ShadowBase, *OriginBase;
4430 std::tie(ShadowBase, OriginBase) = getShadowOriginPtr(
4431 &I, IRB, IRB.getInt8Ty(), Align(1), /*isStore*/ true);
4432
4433 Value *PoisonValue = IRB.getInt8(PoisonStack ? ClPoisonStackPattern : 0);
4434 IRB.CreateMemSet(ShadowBase, PoisonValue, Len, I.getAlign());
4435 }
4436
4437 if (PoisonStack && MS.TrackOrigins) {
4438 Value *Idptr = getLocalVarIdptr(I);
4439 if (ClPrintStackNames) {
4440 Value *Descr = getLocalVarDescription(I);
4441 IRB.CreateCall(MS.MsanSetAllocaOriginWithDescriptionFn,
4442 {&I, Len, Idptr, Descr});
4443 } else {
4444 IRB.CreateCall(MS.MsanSetAllocaOriginNoDescriptionFn, {&I, Len, Idptr});
4445 }
4446 }
4447 }
4448
4449 void poisonAllocaKmsan(AllocaInst &I, IRBuilder<> &IRB, Value *Len) {
4450 Value *Descr = getLocalVarDescription(I);
4451 if (PoisonStack) {
4452 IRB.CreateCall(MS.MsanPoisonAllocaFn, {&I, Len, Descr});
4453 } else {
4454 IRB.CreateCall(MS.MsanUnpoisonAllocaFn, {&I, Len});
4455 }
4456 }
4457
4458 void instrumentAlloca(AllocaInst &I, Instruction *InsPoint = nullptr) {
4459 if (!InsPoint)
4460 InsPoint = &I;
4461 NextNodeIRBuilder IRB(InsPoint);
4462 const DataLayout &DL = F.getParent()->getDataLayout();
4463 TypeSize TS = DL.getTypeAllocSize(I.getAllocatedType());
4464 Value *Len = IRB.CreateTypeSize(MS.IntptrTy, TS);
4465 if (I.isArrayAllocation())
4466 Len = IRB.CreateMul(Len,
4467 IRB.CreateZExtOrTrunc(I.getArraySize(), MS.IntptrTy));
4468
4469 if (MS.CompileKernel)
4470 poisonAllocaKmsan(I, IRB, Len);
4471 else
4472 poisonAllocaUserspace(I, IRB, Len);
4473 }
4474
4475 void visitAllocaInst(AllocaInst &I) {
4476 setShadow(&I, getCleanShadow(&I));
4477 setOrigin(&I, getCleanOrigin());
4478 // We'll get to this alloca later unless it's poisoned at the corresponding
4479 // llvm.lifetime.start.
4480 AllocaSet.insert(&I);
4481 }
4482
4483 void visitSelectInst(SelectInst &I) {
4484 IRBuilder<> IRB(&I);
4485 // a = select b, c, d
4486 Value *B = I.getCondition();
4487 Value *C = I.getTrueValue();
4488 Value *D = I.getFalseValue();
4489 Value *Sb = getShadow(B);
4490 Value *Sc = getShadow(C);
4491 Value *Sd = getShadow(D);
4492
4493 // Result shadow if condition shadow is 0.
4494 Value *Sa0 = IRB.CreateSelect(B, Sc, Sd);
4495 Value *Sa1;
4496 if (I.getType()->isAggregateType()) {
4497 // To avoid "sign extending" i1 to an arbitrary aggregate type, we just do
4498 // an extra "select". This results in much more compact IR.
4499 // Sa = select Sb, poisoned, (select b, Sc, Sd)
4500 Sa1 = getPoisonedShadow(getShadowTy(I.getType()));
4501 } else {
4502 // Sa = select Sb, [ (c^d) | Sc | Sd ], [ b ? Sc : Sd ]
4503 // If Sb (condition is poisoned), look for bits in c and d that are equal
4504 // and both unpoisoned.
4505 // If !Sb (condition is unpoisoned), simply pick one of Sc and Sd.
4506
4507 // Cast arguments to shadow-compatible type.
4508 C = CreateAppToShadowCast(IRB, C);
4509 D = CreateAppToShadowCast(IRB, D);
4510
4511 // Result shadow if condition shadow is 1.
4512 Sa1 = IRB.CreateOr({IRB.CreateXor(C, D), Sc, Sd});
4513 }
4514 Value *Sa = IRB.CreateSelect(Sb, Sa1, Sa0, "_msprop_select");
4515 setShadow(&I, Sa);
4516 if (MS.TrackOrigins) {
4517 // Origins are always i32, so any vector conditions must be flattened.
4518 // FIXME: consider tracking vector origins for app vectors?
4519 if (B->getType()->isVectorTy()) {
4520 B = convertToBool(B, IRB);
4521 Sb = convertToBool(Sb, IRB);
4522 }
4523 // a = select b, c, d
4524 // Oa = Sb ? Ob : (b ? Oc : Od)
4525 setOrigin(
4526 &I, IRB.CreateSelect(Sb, getOrigin(I.getCondition()),
4527 IRB.CreateSelect(B, getOrigin(I.getTrueValue()),
4528 getOrigin(I.getFalseValue()))));
4529 }
4530 }
4531
4532 void visitLandingPadInst(LandingPadInst &I) {
4533 // Do nothing.
4534 // See https://github.com/google/sanitizers/issues/504
4535 setShadow(&I, getCleanShadow(&I));
4536 setOrigin(&I, getCleanOrigin());
4537 }
4538
4539 void visitCatchSwitchInst(CatchSwitchInst &I) {
4540 setShadow(&I, getCleanShadow(&I));
4541 setOrigin(&I, getCleanOrigin());
4542 }
4543
4544 void visitFuncletPadInst(FuncletPadInst &I) {
4545 setShadow(&I, getCleanShadow(&I));
4546 setOrigin(&I, getCleanOrigin());
4547 }
4548
4549 void visitGetElementPtrInst(GetElementPtrInst &I) { handleShadowOr(I); }
4550
4551 void visitExtractValueInst(ExtractValueInst &I) {
4552 IRBuilder<> IRB(&I);
4553 Value *Agg = I.getAggregateOperand();
4554 LLVM_DEBUG(dbgs() << "ExtractValue: " << I << "\n");
4555 Value *AggShadow = getShadow(Agg);
4556 LLVM_DEBUG(dbgs() << " AggShadow: " << *AggShadow << "\n");
4557 Value *ResShadow = IRB.CreateExtractValue(AggShadow, I.getIndices());
4558 LLVM_DEBUG(dbgs() << " ResShadow: " << *ResShadow << "\n");
4559 setShadow(&I, ResShadow);
4560 setOriginForNaryOp(I);
4561 }
4562
4563 void visitInsertValueInst(InsertValueInst &I) {
4564 IRBuilder<> IRB(&I);
4565 LLVM_DEBUG(dbgs() << "InsertValue: " << I << "\n");
4566 Value *AggShadow = getShadow(I.getAggregateOperand());
4567 Value *InsShadow = getShadow(I.getInsertedValueOperand());
4568 LLVM_DEBUG(dbgs() << " AggShadow: " << *AggShadow << "\n");
4569 LLVM_DEBUG(dbgs() << " InsShadow: " << *InsShadow << "\n");
4570 Value *Res = IRB.CreateInsertValue(AggShadow, InsShadow, I.getIndices());
4571 LLVM_DEBUG(dbgs() << " Res: " << *Res << "\n");
4572 setShadow(&I, Res);
4573 setOriginForNaryOp(I);
4574 }
4575
4576 void dumpInst(Instruction &I) {
4577 if (CallInst *CI = dyn_cast<CallInst>(&I)) {
4578 errs() << "ZZZ call " << CI->getCalledFunction()->getName() << "\n";
4579 } else {
4580 errs() << "ZZZ " << I.getOpcodeName() << "\n";
4581 }
4582 errs() << "QQQ " << I << "\n";
4583 }
4584
4585 void visitResumeInst(ResumeInst &I) {
4586 LLVM_DEBUG(dbgs() << "Resume: " << I << "\n");
4587 // Nothing to do here.
4588 }
4589
4590 void visitCleanupReturnInst(CleanupReturnInst &CRI) {
4591 LLVM_DEBUG(dbgs() << "CleanupReturn: " << CRI << "\n");
4592 // Nothing to do here.
4593 }
4594
4595 void visitCatchReturnInst(CatchReturnInst &CRI) {
4596 LLVM_DEBUG(dbgs() << "CatchReturn: " << CRI << "\n");
4597 // Nothing to do here.
4598 }
4599
4600 void instrumentAsmArgument(Value *Operand, Type *ElemTy, Instruction &I,
4601 IRBuilder<> &IRB, const DataLayout &DL,
4602 bool isOutput) {
4603 // For each assembly argument, we check its value for being initialized.
4604 // If the argument is a pointer, we assume it points to a single element
4605 // of the corresponding type (or to a 8-byte word, if the type is unsized).
4606 // Each such pointer is instrumented with a call to the runtime library.
4607 Type *OpType = Operand->getType();
4608 // Check the operand value itself.
4609 insertShadowCheck(Operand, &I);
4610 if (!OpType->isPointerTy() || !isOutput) {
4611 assert(!isOutput);
4612 return;
4613 }
4614 if (!ElemTy->isSized())
4615 return;
4616 auto Size = DL.getTypeStoreSize(ElemTy);
4617 Value *SizeVal = IRB.CreateTypeSize(MS.IntptrTy, Size);
4618 if (MS.CompileKernel) {
4619 IRB.CreateCall(MS.MsanInstrumentAsmStoreFn, {Operand, SizeVal});
4620 } else {
4621 // ElemTy, derived from elementtype(), does not encode the alignment of
4622 // the pointer. Conservatively assume that the shadow memory is unaligned.
4623 // When Size is large, avoid StoreInst as it would expand to many
4624 // instructions.
4625 auto [ShadowPtr, _] =
4626 getShadowOriginPtrUserspace(Operand, IRB, IRB.getInt8Ty(), Align(1));
4627 if (Size <= 32)
4628 IRB.CreateAlignedStore(getCleanShadow(ElemTy), ShadowPtr, Align(1));
4629 else
4630 IRB.CreateMemSet(ShadowPtr, ConstantInt::getNullValue(IRB.getInt8Ty()),
4631 SizeVal, Align(1));
4632 }
4633 }
4634
4635 /// Get the number of output arguments returned by pointers.
4636 int getNumOutputArgs(InlineAsm *IA, CallBase *CB) {
4637 int NumRetOutputs = 0;
4638 int NumOutputs = 0;
4639 Type *RetTy = cast<Value>(CB)->getType();
4640 if (!RetTy->isVoidTy()) {
4641 // Register outputs are returned via the CallInst return value.
4642 auto *ST = dyn_cast<StructType>(RetTy);
4643 if (ST)
4644 NumRetOutputs = ST->getNumElements();
4645 else
4646 NumRetOutputs = 1;
4647 }
4648 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
4649 for (const InlineAsm::ConstraintInfo &Info : Constraints) {
4650 switch (Info.Type) {
4652 NumOutputs++;
4653 break;
4654 default:
4655 break;
4656 }
4657 }
4658 return NumOutputs - NumRetOutputs;
4659 }
4660
4661 void visitAsmInstruction(Instruction &I) {
4662 // Conservative inline assembly handling: check for poisoned shadow of
4663 // asm() arguments, then unpoison the result and all the memory locations
4664 // pointed to by those arguments.
4665 // An inline asm() statement in C++ contains lists of input and output
4666 // arguments used by the assembly code. These are mapped to operands of the
4667 // CallInst as follows:
4668 // - nR register outputs ("=r) are returned by value in a single structure
4669 // (SSA value of the CallInst);
4670 // - nO other outputs ("=m" and others) are returned by pointer as first
4671 // nO operands of the CallInst;
4672 // - nI inputs ("r", "m" and others) are passed to CallInst as the
4673 // remaining nI operands.
4674 // The total number of asm() arguments in the source is nR+nO+nI, and the
4675 // corresponding CallInst has nO+nI+1 operands (the last operand is the
4676 // function to be called).
4677 const DataLayout &DL = F.getParent()->getDataLayout();
4678 CallBase *CB = cast<CallBase>(&I);
4679 IRBuilder<> IRB(&I);
4680 InlineAsm *IA = cast<InlineAsm>(CB->getCalledOperand());
4681 int OutputArgs = getNumOutputArgs(IA, CB);
4682 // The last operand of a CallInst is the function itself.
4683 int NumOperands = CB->getNumOperands() - 1;
4684
4685 // Check input arguments. Doing so before unpoisoning output arguments, so
4686 // that we won't overwrite uninit values before checking them.
4687 for (int i = OutputArgs; i < NumOperands; i++) {
4688 Value *Operand = CB->getOperand(i);
4689 instrumentAsmArgument(Operand, CB->getParamElementType(i), I, IRB, DL,
4690 /*isOutput*/ false);
4691 }
4692 // Unpoison output arguments. This must happen before the actual InlineAsm
4693 // call, so that the shadow for memory published in the asm() statement
4694 // remains valid.
4695 for (int i = 0; i < OutputArgs; i++) {
4696 Value *Operand = CB->getOperand(i);
4697 instrumentAsmArgument(Operand, CB->getParamElementType(i), I, IRB, DL,
4698 /*isOutput*/ true);
4699 }
4700
4701 setShadow(&I, getCleanShadow(&I));
4702 setOrigin(&I, getCleanOrigin());
4703 }
4704
4705 void visitFreezeInst(FreezeInst &I) {
4706 // Freeze always returns a fully defined value.
4707 setShadow(&I, getCleanShadow(&I));
4708 setOrigin(&I, getCleanOrigin());
4709 }
4710
4711 void visitInstruction(Instruction &I) {
4712 // Everything else: stop propagating and check for poisoned shadow.
4714 dumpInst(I);
4715 LLVM_DEBUG(dbgs() << "DEFAULT: " << I << "\n");
4716 for (size_t i = 0, n = I.getNumOperands(); i < n; i++) {
4717 Value *Operand = I.getOperand(i);
4718 if (Operand->getType()->isSized())
4719 insertShadowCheck(Operand, &I);
4720 }
4721 setShadow(&I, getCleanShadow(&I));
4722 setOrigin(&I, getCleanOrigin());
4723 }
4724};
4725
4726struct VarArgHelperBase : public VarArgHelper {
4727 Function &F;
4728 MemorySanitizer &MS;
4729 MemorySanitizerVisitor &MSV;
4730 SmallVector<CallInst *, 16> VAStartInstrumentationList;
4731 const unsigned VAListTagSize;
4732
4733 VarArgHelperBase(Function &F, MemorySanitizer &MS,
4734 MemorySanitizerVisitor &MSV, unsigned VAListTagSize)
4735 : F(F), MS(MS), MSV(MSV), VAListTagSize(VAListTagSize) {}
4736
4737 Value *getShadowAddrForVAArgument(IRBuilder<> &IRB, unsigned ArgOffset) {
4738 Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
4739 return IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
4740 }
4741
4742 /// Compute the shadow address for a given va_arg.
4743 Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
4744 unsigned ArgOffset) {
4745 Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
4746 Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
4747 return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
4748 "_msarg_va_s");
4749 }
4750
4751 /// Compute the shadow address for a given va_arg.
4752 Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
4753 unsigned ArgOffset, unsigned ArgSize) {
4754 // Make sure we don't overflow __msan_va_arg_tls.
4755 if (ArgOffset + ArgSize > kParamTLSSize)
4756 return nullptr;
4757 return getShadowPtrForVAArgument(Ty, IRB, ArgOffset);
4758 }
4759
4760 /// Compute the origin address for a given va_arg.
4761 Value *getOriginPtrForVAArgument(IRBuilder<> &IRB, int ArgOffset) {
4762 Value *Base = IRB.CreatePointerCast(MS.VAArgOriginTLS, MS.IntptrTy);
4763 // getOriginPtrForVAArgument() is always called after
4764 // getShadowPtrForVAArgument(), so __msan_va_arg_origin_tls can never
4765 // overflow.
4766 Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
4767 return IRB.CreateIntToPtr(Base, PointerType::get(MS.OriginTy, 0),
4768 "_msarg_va_o");
4769 }
4770
4771 void CleanUnusedTLS(IRBuilder<> &IRB, Value *ShadowBase,
4772 unsigned BaseOffset) {
4773 // The tails of __msan_va_arg_tls is not large enough to fit full
4774 // value shadow, but it will be copied to backup anyway. Make it
4775 // clean.
4776 if (BaseOffset >= kParamTLSSize)
4777 return;
4778 Value *TailSize =
4779 ConstantInt::getSigned(IRB.getInt32Ty(), kParamTLSSize - BaseOffset);
4780 IRB.CreateMemSet(ShadowBase, ConstantInt::getNullValue(IRB.getInt8Ty()),
4781 TailSize, Align(8));
4782 }
4783
4784 void unpoisonVAListTagForInst(IntrinsicInst &I) {
4785 IRBuilder<> IRB(&I);
4786 Value *VAListTag = I.getArgOperand(0);
4787 const Align Alignment = Align(8);
4788 auto [ShadowPtr, OriginPtr] = MSV.getShadowOriginPtr(
4789 VAListTag, IRB, IRB.getInt8Ty(), Alignment, /*isStore*/ true);
4790 // Unpoison the whole __va_list_tag.
4791 IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
4792 VAListTagSize, Alignment, false);
4793 }
4794
4795 void visitVAStartInst(VAStartInst &I) override {
4796 if (F.getCallingConv() == CallingConv::Win64)
4797 return;
4798 VAStartInstrumentationList.push_back(&I);
4799 unpoisonVAListTagForInst(I);
4800 }
4801
4802 void visitVACopyInst(VACopyInst &I) override {
4803 if (F.getCallingConv() == CallingConv::Win64)
4804 return;
4805 unpoisonVAListTagForInst(I);
4806 }
4807};
4808
4809/// AMD64-specific implementation of VarArgHelper.
4810struct VarArgAMD64Helper : public VarArgHelperBase {
4811 // An unfortunate workaround for asymmetric lowering of va_arg stuff.
4812 // See a comment in visitCallBase for more details.
4813 static const unsigned AMD64GpEndOffset = 48; // AMD64 ABI Draft 0.99.6 p3.5.7
4814 static const unsigned AMD64FpEndOffsetSSE = 176;
4815 // If SSE is disabled, fp_offset in va_list is zero.
4816 static const unsigned AMD64FpEndOffsetNoSSE = AMD64GpEndOffset;
4817
4818 unsigned AMD64FpEndOffset;
4819 AllocaInst *VAArgTLSCopy = nullptr;
4820 AllocaInst *VAArgTLSOriginCopy = nullptr;
4821 Value *VAArgOverflowSize = nullptr;
4822
4823 enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory };
4824
4825 VarArgAMD64Helper(Function &F, MemorySanitizer &MS,
4826 MemorySanitizerVisitor &MSV)
4827 : VarArgHelperBase(F, MS, MSV, /*VAListTagSize=*/24) {
4828 AMD64FpEndOffset = AMD64FpEndOffsetSSE;
4829 for (const auto &Attr : F.getAttributes().getFnAttrs()) {
4830 if (Attr.isStringAttribute() &&
4831 (Attr.getKindAsString() == "target-features")) {
4832 if (Attr.getValueAsString().contains("-sse"))
4833 AMD64FpEndOffset = AMD64FpEndOffsetNoSSE;
4834 break;
4835 }
4836 }
4837 }
4838
4839 ArgKind classifyArgument(Value *arg) {
4840 // A very rough approximation of X86_64 argument classification rules.
4841 Type *T = arg->getType();
4842 if (T->isX86_FP80Ty())
4843 return AK_Memory;
4844 if (T->isFPOrFPVectorTy() || T->isX86_MMXTy())
4845 return AK_FloatingPoint;
4846 if (T->isIntegerTy() && T->getPrimitiveSizeInBits() <= 64)
4847 return AK_GeneralPurpose;
4848 if (T->isPointerTy())
4849 return AK_GeneralPurpose;
4850 return AK_Memory;
4851 }
4852
4853 // For VarArg functions, store the argument shadow in an ABI-specific format
4854 // that corresponds to va_list layout.
4855 // We do this because Clang lowers va_arg in the frontend, and this pass
4856 // only sees the low level code that deals with va_list internals.
4857 // A much easier alternative (provided that Clang emits va_arg instructions)
4858 // would have been to associate each live instance of va_list with a copy of
4859 // MSanParamTLS, and extract shadow on va_arg() call in the argument list
4860 // order.
4861 void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {
4862 unsigned GpOffset = 0;
4863 unsigned FpOffset = AMD64GpEndOffset;
4864 unsigned OverflowOffset = AMD64FpEndOffset;
4865 const DataLayout &DL = F.getParent()->getDataLayout();
4866
4867 for (const auto &[ArgNo, A] : llvm::enumerate(CB.args())) {
4868 bool IsFixed = ArgNo < CB.getFunctionType()->getNumParams();
4869 bool IsByVal = CB.paramHasAttr(ArgNo, Attribute::ByVal);
4870 if (IsByVal) {
4871 // ByVal arguments always go to the overflow area.
4872 // Fixed arguments passed through the overflow area will be stepped
4873 // over by va_start, so don't count them towards the offset.
4874 if (IsFixed)
4875 continue;
4876 assert(A->getType()->isPointerTy());
4877 Type *RealTy = CB.getParamByValType(ArgNo);
4878 uint64_t ArgSize = DL.getTypeAllocSize(RealTy);
4879 uint64_t AlignedSize = alignTo(ArgSize, 8);
4880 unsigned BaseOffset = OverflowOffset;
4881 Value *ShadowBase =
4882 getShadowPtrForVAArgument(RealTy, IRB, OverflowOffset);
4883 Value *OriginBase = nullptr;
4884 if (MS.TrackOrigins)
4885 OriginBase = getOriginPtrForVAArgument(IRB, OverflowOffset);
4886 OverflowOffset += AlignedSize;
4887
4888 if (OverflowOffset > kParamTLSSize) {
4889 CleanUnusedTLS(IRB, ShadowBase, BaseOffset);
4890 continue; // We have no space to copy shadow there.
4891 }
4892
4893 Value *ShadowPtr, *OriginPtr;
4894 std::tie(ShadowPtr, OriginPtr) =
4895 MSV.getShadowOriginPtr(A, IRB, IRB.getInt8Ty(), kShadowTLSAlignment,
4896 /*isStore*/ false);
4897 IRB.CreateMemCpy(ShadowBase, kShadowTLSAlignment, ShadowPtr,
4898 kShadowTLSAlignment, ArgSize);
4899 if (MS.TrackOrigins)
4900 IRB.CreateMemCpy(OriginBase, kShadowTLSAlignment, OriginPtr,
4901 kShadowTLSAlignment, ArgSize);
4902 } else {
4903 ArgKind AK = classifyArgument(A);
4904 if (AK == AK_GeneralPurpose && GpOffset >= AMD64GpEndOffset)
4905 AK = AK_Memory;
4906 if (AK == AK_FloatingPoint && FpOffset >= AMD64FpEndOffset)
4907 AK = AK_Memory;
4908 Value *ShadowBase, *OriginBase = nullptr;
4909 switch (AK) {
4910 case AK_GeneralPurpose:
4911 ShadowBase = getShadowPtrForVAArgument(A->getType(), IRB, GpOffset);
4912 if (MS.TrackOrigins)
4913 OriginBase = getOriginPtrForVAArgument(IRB, GpOffset);
4914 GpOffset += 8;
4915 assert(GpOffset <= kParamTLSSize);
4916 break;
4917 case AK_FloatingPoint:
4918 ShadowBase = getShadowPtrForVAArgument(A->getType(), IRB, FpOffset);
4919 if (MS.TrackOrigins)
4920 OriginBase = getOriginPtrForVAArgument(IRB, FpOffset);
4921 FpOffset += 16;
4922 assert(FpOffset <= kParamTLSSize);
4923 break;
4924 case AK_Memory:
4925 if (IsFixed)
4926 continue;
4927 uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
4928 uint64_t AlignedSize = alignTo(ArgSize, 8);
4929 unsigned BaseOffset = OverflowOffset;
4930 ShadowBase =
4931 getShadowPtrForVAArgument(A->getType(), IRB, OverflowOffset);
4932 if (MS.TrackOrigins) {
4933 OriginBase = getOriginPtrForVAArgument(IRB, OverflowOffset);
4934 }
4935 OverflowOffset += AlignedSize;
4936 if (OverflowOffset > kParamTLSSize) {
4937 // We have no space to copy shadow there.
4938 CleanUnusedTLS(IRB, ShadowBase, BaseOffset);
4939 continue;
4940 }
4941 }
4942 // Take fixed arguments into account for GpOffset and FpOffset,
4943 // but don't actually store shadows for them.
4944 // TODO(glider): don't call get*PtrForVAArgument() for them.
4945 if (IsFixed)
4946 continue;
4947 Value *Shadow = MSV.getShadow(A);
4948 IRB.CreateAlignedStore(Shadow, ShadowBase, kShadowTLSAlignment);
4949 if (MS.TrackOrigins) {
4950 Value *Origin = MSV.getOrigin(A);
4951 TypeSize StoreSize = DL.getTypeStoreSize(Shadow->getType());
4952 MSV.paintOrigin(IRB, Origin, OriginBase, StoreSize,
4954 }
4955 }
4956 }
4957 Constant *OverflowSize =
4958 ConstantInt::get(IRB.getInt64Ty(), OverflowOffset - AMD64FpEndOffset);
4959 IRB.CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
4960 }
4961
4962 void finalizeInstrumentation() override {
4963 assert(!VAArgOverflowSize && !VAArgTLSCopy &&
4964 "finalizeInstrumentation called twice");
4965 if (!VAStartInstrumentationList.empty()) {
4966 // If there is a va_start in this function, make a backup copy of
4967 // va_arg_tls somewhere in the function entry block.
4968 IRBuilder<> IRB(MSV.FnPrologueEnd);
4969 VAArgOverflowSize =
4970 IRB.CreateLoad(IRB.getInt64Ty(), MS.VAArgOverflowSizeTLS);
4971 Value *CopySize = IRB.CreateAdd(
4972 ConstantInt::get(MS.IntptrTy, AMD64FpEndOffset), VAArgOverflowSize);
4973 VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
4974 VAArgTLSCopy->setAlignment(kShadowTLSAlignment);
4975 IRB.CreateMemSet(VAArgTLSCopy, Constant::getNullValue(IRB.getInt8Ty()),
4976 CopySize, kShadowTLSAlignment, false);
4977
4978 Value *SrcSize = IRB.CreateBinaryIntrinsic(
4979 Intrinsic::umin, CopySize,
4980 ConstantInt::get(MS.IntptrTy, kParamTLSSize));
4981 IRB.CreateMemCpy(VAArgTLSCopy, kShadowTLSAlignment, MS.VAArgTLS,
4982 kShadowTLSAlignment, SrcSize);
4983 if (MS.TrackOrigins) {
4984 VAArgTLSOriginCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
4985 VAArgTLSOriginCopy->setAlignment(kShadowTLSAlignment);
4986 IRB.CreateMemCpy(VAArgTLSOriginCopy, kShadowTLSAlignment,
4987 MS.VAArgOriginTLS, kShadowTLSAlignment, SrcSize);
4988 }
4989 }
4990
4991 // Instrument va_start.
4992 // Copy va_list shadow from the backup copy of the TLS contents.
4993 for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
4994 CallInst *OrigInst = VAStartInstrumentationList[i];
4995 NextNodeIRBuilder IRB(OrigInst);
4996 Value *VAListTag = OrigInst->getArgOperand(0);
4997
4998 Type *RegSaveAreaPtrTy = PointerType::getUnqual(*MS.C); // i64*
4999 Value *RegSaveAreaPtrPtr = IRB.CreateIntToPtr(
5000 IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
5001 ConstantInt::get(MS.IntptrTy, 16)),
5002 PointerType::get(RegSaveAreaPtrTy, 0));
5003 Value *RegSaveAreaPtr =
5004 IRB.CreateLoad(RegSaveAreaPtrTy, RegSaveAreaPtrPtr);
5005 Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
5006 const Align Alignment = Align(16);
5007 std::tie(RegSaveAreaShadowPtr, RegSaveAreaOriginPtr) =
5008 MSV.getShadowOriginPtr(RegSaveAreaPtr, IRB, IRB.getInt8Ty(),
5009 Alignment, /*isStore*/ true);
5010 IRB.CreateMemCpy(RegSaveAreaShadowPtr, Alignment, VAArgTLSCopy, Alignment,
5011 AMD64FpEndOffset);
5012 if (MS.TrackOrigins)
5013 IRB.CreateMemCpy(RegSaveAreaOriginPtr, Alignment, VAArgTLSOriginCopy,
5014 Alignment, AMD64FpEndOffset);
5015 Type *OverflowArgAreaPtrTy = PointerType::getUnqual(*MS.C); // i64*
5016 Value *OverflowArgAreaPtrPtr = IRB.CreateIntToPtr(
5017 IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
5018 ConstantInt::get(MS.IntptrTy, 8)),
5019 PointerType::get(OverflowArgAreaPtrTy, 0));
5020 Value *OverflowArgAreaPtr =
5021 IRB.CreateLoad(OverflowArgAreaPtrTy, OverflowArgAreaPtrPtr);
5022 Value *OverflowArgAreaShadowPtr, *OverflowArgAreaOriginPtr;
5023 std::tie(OverflowArgAreaShadowPtr, OverflowArgAreaOriginPtr) =
5024 MSV.getShadowOriginPtr(OverflowArgAreaPtr, IRB, IRB.getInt8Ty(),
5025 Alignment, /*isStore*/ true);
5026 Value *SrcPtr = IRB.CreateConstGEP1_32(IRB.getInt8Ty(), VAArgTLSCopy,
5027 AMD64FpEndOffset);
5028 IRB.CreateMemCpy(OverflowArgAreaShadowPtr, Alignment, SrcPtr, Alignment,
5029 VAArgOverflowSize);
5030 if (MS.TrackOrigins) {
5031 SrcPtr = IRB.CreateConstGEP1_32(IRB.getInt8Ty(), VAArgTLSOriginCopy,
5032 AMD64FpEndOffset);
5033 IRB.CreateMemCpy(OverflowArgAreaOriginPtr, Alignment, SrcPtr, Alignment,
5034 VAArgOverflowSize);
5035 }
5036 }
5037 }
5038};
5039
5040/// MIPS64-specific implementation of VarArgHelper.
5041/// NOTE: This is also used for LoongArch64.
5042struct VarArgMIPS64Helper : public VarArgHelperBase {
5043 AllocaInst *VAArgTLSCopy = nullptr;
5044 Value *VAArgSize = nullptr;
5045
5046 VarArgMIPS64Helper(Function &F, MemorySanitizer &MS,
5047 MemorySanitizerVisitor &MSV)
5048 : VarArgHelperBase(F, MS, MSV, /*VAListTagSize=*/8) {}
5049
5050 void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {
5051 unsigned VAArgOffset = 0;
5052 const DataLayout &DL = F.getParent()->getDataLayout();
5053 for (Value *A :
5055 Triple TargetTriple(F.getParent()->getTargetTriple());
5056 Value *Base;
5057 uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
5058 if (TargetTriple.getArch() == Triple::mips64) {
5059 // Adjusting the shadow for argument with size < 8 to match the
5060 // placement of bits in big endian system
5061 if (ArgSize < 8)
5062 VAArgOffset += (8 - ArgSize);
5063 }
5064 Base = getShadowPtrForVAArgument(A->getType(), IRB, VAArgOffset, ArgSize);
5065 VAArgOffset += ArgSize;
5066 VAArgOffset = alignTo(VAArgOffset, 8);
5067 if (!Base)
5068 continue;
5069 IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
5070 }
5071
5072 Constant *TotalVAArgSize = ConstantInt::get(IRB.getInt64Ty(), VAArgOffset);
5073 // Here using VAArgOverflowSizeTLS as VAArgSizeTLS to avoid creation of
5074 // a new class member i.e. it is the total size of all VarArgs.
5075 IRB.CreateStore(TotalVAArgSize, MS.VAArgOverflowSizeTLS);
5076 }
5077
5078 void finalizeInstrumentation() override {
5079 assert(!VAArgSize && !VAArgTLSCopy &&
5080 "finalizeInstrumentation called twice");
5081 IRBuilder<> IRB(MSV.FnPrologueEnd);
5082 VAArgSize = IRB.CreateLoad(IRB.getInt64Ty(), MS.VAArgOverflowSizeTLS);
5083 Value *CopySize =
5084 IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, 0), VAArgSize);
5085
5086 if (!VAStartInstrumentationList.empty()) {
5087 // If there is a va_start in this function, make a backup copy of
5088 // va_arg_tls somewhere in the function entry block.
5089 VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
5090 VAArgTLSCopy->setAlignment(kShadowTLSAlignment);
5091 IRB.CreateMemSet(VAArgTLSCopy, Constant::getNullValue(IRB.getInt8Ty()),
5092 CopySize, kShadowTLSAlignment, false);
5093
5094 Value *SrcSize = IRB.CreateBinaryIntrinsic(
5095 Intrinsic::umin, CopySize,
5096 ConstantInt::get(MS.IntptrTy, kParamTLSSize));
5097 IRB.CreateMemCpy(VAArgTLSCopy, kShadowTLSAlignment, MS.VAArgTLS,
5098 kShadowTLSAlignment, SrcSize);
5099 }
5100
5101 // Instrument va_start.
5102 // Copy va_list shadow from the backup copy of the TLS contents.
5103 for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
5104 CallInst *OrigInst = VAStartInstrumentationList[i];
5105 NextNodeIRBuilder IRB(OrigInst);
5106 Value *VAListTag = OrigInst->getArgOperand(0);
5107 Type *RegSaveAreaPtrTy = PointerType::getUnqual(*MS.C); // i64*
5108 Value *RegSaveAreaPtrPtr =
5109 IRB.CreateIntToPtr(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
5110 PointerType::get(RegSaveAreaPtrTy, 0));
5111 Value *RegSaveAreaPtr =
5112 IRB.CreateLoad(RegSaveAreaPtrTy, RegSaveAreaPtrPtr);
5113 Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
5114 const Align Alignment = Align(8);
5115 std::tie(RegSaveAreaShadowPtr, RegSaveAreaOriginPtr) =
5116 MSV.getShadowOriginPtr(RegSaveAreaPtr, IRB, IRB.getInt8Ty(),
5117 Alignment, /*isStore*/ true);
5118 IRB.CreateMemCpy(RegSaveAreaShadowPtr, Alignment, VAArgTLSCopy, Alignment,
5119 CopySize);
5120 }
5121 }
5122};
5123
5124/// AArch64-specific implementation of VarArgHelper.
5125struct VarArgAArch64Helper : public VarArgHelperBase {
5126 static const unsigned kAArch64GrArgSize = 64;
5127 static const unsigned kAArch64VrArgSize = 128;
5128
5129 static const unsigned AArch64GrBegOffset = 0;
5130 static const unsigned AArch64GrEndOffset = kAArch64GrArgSize;
5131 // Make VR space aligned to 16 bytes.
5132 static const unsigned AArch64VrBegOffset = AArch64GrEndOffset;
5133 static const unsigned AArch64VrEndOffset =
5134 AArch64VrBegOffset + kAArch64VrArgSize;
5135 static const unsigned AArch64VAEndOffset = AArch64VrEndOffset;
5136
5137 AllocaInst *VAArgTLSCopy = nullptr;
5138 Value *VAArgOverflowSize = nullptr;
5139
5140 enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory };
5141
5142 VarArgAArch64Helper(Function &F, MemorySanitizer &MS,
5143 MemorySanitizerVisitor &MSV)
5144 : VarArgHelperBase(F, MS, MSV, /*VAListTagSize=*/32) {}
5145
5146 // A very rough approximation of aarch64 argument classification rules.
5147 std::pair<ArgKind, uint64_t> classifyArgument(Type *T) {
5148 if (T->isIntOrPtrTy() && T->getPrimitiveSizeInBits() <= 64)
5149 return {AK_GeneralPurpose, 1};
5150 if (T->isFloatingPointTy() && T->getPrimitiveSizeInBits() <= 128)
5151 return {AK_FloatingPoint, 1};
5152
5153 if (T->isArrayTy()) {
5154 auto R = classifyArgument(T->getArrayElementType());
5155 R.second *= T->getScalarType()->getArrayNumElements();
5156 return R;
5157 }
5158
5159 if (const FixedVectorType *FV = dyn_cast<FixedVectorType>(T)) {
5160 auto R = classifyArgument(FV->getScalarType());
5161 R.second *= FV->getNumElements();
5162 return R;
5163 }
5164
5165 LLVM_DEBUG(errs() << "Unknown vararg type: " << *T << "\n");
5166 return {AK_Memory, 0};
5167 }
5168
5169 // The instrumentation stores the argument shadow in a non ABI-specific
5170 // format because it does not know which argument is named (since Clang,
5171 // like x86_64 case, lowers the va_args in the frontend and this pass only
5172 // sees the low level code that deals with va_list internals).
5173 // The first seven GR registers are saved in the first 56 bytes of the
5174 // va_arg tls arra, followed by the first 8 FP/SIMD registers, and then
5175 // the remaining arguments.
5176 // Using constant offset within the va_arg TLS array allows fast copy
5177 // in the finalize instrumentation.
5178 void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {
5179 unsigned GrOffset = AArch64GrBegOffset;
5180 unsigned VrOffset = AArch64VrBegOffset;
5181 unsigned OverflowOffset = AArch64VAEndOffset;
5182
5183 const DataLayout &DL = F.getParent()->getDataLayout();
5184 for (const auto &[ArgNo, A] : llvm::enumerate(CB.args())) {
5185 bool IsFixed = ArgNo < CB.getFunctionType()->getNumParams();
5186 auto [AK, RegNum] = classifyArgument(A->getType());
5187 if (AK == AK_GeneralPurpose &&
5188 (GrOffset + RegNum * 8) > AArch64GrEndOffset)
5189 AK = AK_Memory;
5190 if (AK == AK_FloatingPoint &&
5191 (VrOffset + RegNum * 16) > AArch64VrEndOffset)
5192 AK = AK_Memory;
5193 Value *Base;
5194 switch (AK) {
5195 case AK_GeneralPurpose:
5196 Base = getShadowPtrForVAArgument(A->getType(), IRB, GrOffset);
5197 GrOffset += 8 * RegNum;
5198 break;
5199 case AK_FloatingPoint:
5200 Base = getShadowPtrForVAArgument(A->getType(), IRB, VrOffset);
5201 VrOffset += 16 * RegNum;
5202 break;
5203 case AK_Memory:
5204 // Don't count fixed arguments in the overflow area - va_start will
5205 // skip right over them.
5206 if (IsFixed)
5207 continue;
5208 uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
5209 uint64_t AlignedSize = alignTo(ArgSize, 8);
5210 unsigned BaseOffset = OverflowOffset;
5211 Base = getShadowPtrForVAArgument(A->getType(), IRB, BaseOffset);
5212 OverflowOffset += AlignedSize;
5213 if (OverflowOffset > kParamTLSSize) {
5214 // We have no space to copy shadow there.
5215 CleanUnusedTLS(IRB, Base, BaseOffset);
5216 continue;
5217 }
5218 break;
5219 }
5220 // Count Gp/Vr fixed arguments to their respective offsets, but don't
5221 // bother to actually store a shadow.
5222 if (IsFixed)
5223 continue;
5224 IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
5225 }
5226 Constant *OverflowSize =
5227 ConstantInt::get(IRB.getInt64Ty(), OverflowOffset - AArch64VAEndOffset);
5228 IRB.CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
5229 }
5230
5231 // Retrieve a va_list field of 'void*' size.
5232 Value *getVAField64(IRBuilder<> &IRB, Value *VAListTag, int offset) {
5233 Value *SaveAreaPtrPtr = IRB.CreateIntToPtr(
5234 IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
5235 ConstantInt::get(MS.IntptrTy, offset)),
5236 PointerType::get(*MS.C, 0));
5237 return IRB.CreateLoad(Type::getInt64Ty(*MS.C), SaveAreaPtrPtr);
5238 }
5239
5240 // Retrieve a va_list field of 'int' size.
5241 Value *getVAField32(IRBuilder<> &IRB, Value *VAListTag, int offset) {
5242 Value *SaveAreaPtr = IRB.CreateIntToPtr(
5243 IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
5244 ConstantInt::get(MS.IntptrTy, offset)),
5245 PointerType::get(*MS.C, 0));
5246 Value *SaveArea32 = IRB.CreateLoad(IRB.getInt32Ty(), SaveAreaPtr);
5247 return IRB.CreateSExt(SaveArea32, MS.IntptrTy);
5248 }
5249
5250 void finalizeInstrumentation() override {
5251 assert(!VAArgOverflowSize && !VAArgTLSCopy &&
5252 "finalizeInstrumentation called twice");
5253 if (!VAStartInstrumentationList.empty()) {
5254 // If there is a va_start in this function, make a backup copy of
5255 // va_arg_tls somewhere in the function entry block.
5256 IRBuilder<> IRB(MSV.FnPrologueEnd);
5257 VAArgOverflowSize =
5258 IRB.CreateLoad(IRB.getInt64Ty(), MS.VAArgOverflowSizeTLS);
5259 Value *CopySize = IRB.CreateAdd(
5260 ConstantInt::get(MS.IntptrTy, AArch64VAEndOffset), VAArgOverflowSize);
5261 VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
5262 VAArgTLSCopy->setAlignment(kShadowTLSAlignment);
5263 IRB.CreateMemSet(VAArgTLSCopy, Constant::getNullValue(IRB.getInt8Ty()),
5264 CopySize, kShadowTLSAlignment, false);
5265
5266 Value *SrcSize = IRB.CreateBinaryIntrinsic(
5267 Intrinsic::umin, CopySize,
5268 ConstantInt::get(MS.IntptrTy, kParamTLSSize));
5269 IRB.CreateMemCpy(VAArgTLSCopy, kShadowTLSAlignment, MS.VAArgTLS,
5270 kShadowTLSAlignment, SrcSize);
5271 }
5272
5273 Value *GrArgSize = ConstantInt::get(MS.IntptrTy, kAArch64GrArgSize);
5274 Value *VrArgSize = ConstantInt::get(MS.IntptrTy, kAArch64VrArgSize);
5275
5276 // Instrument va_start, copy va_list shadow from the backup copy of
5277 // the TLS contents.
5278 for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
5279 CallInst *OrigInst = VAStartInstrumentationList[i];
5280 NextNodeIRBuilder IRB(OrigInst);
5281
5282 Value *VAListTag = OrigInst->getArgOperand(0);
5283
5284 // The variadic ABI for AArch64 creates two areas to save the incoming
5285 // argument registers (one for 64-bit general register xn-x7 and another
5286 // for 128-bit FP/SIMD vn-v7).
5287 // We need then to propagate the shadow arguments on both regions
5288 // 'va::__gr_top + va::__gr_offs' and 'va::__vr_top + va::__vr_offs'.
5289 // The remaining arguments are saved on shadow for 'va::stack'.
5290 // One caveat is it requires only to propagate the non-named arguments,
5291 // however on the call site instrumentation 'all' the arguments are
5292 // saved. So to copy the shadow values from the va_arg TLS array
5293 // we need to adjust the offset for both GR and VR fields based on
5294 // the __{gr,vr}_offs value (since they are stores based on incoming
5295 // named arguments).
5296 Type *RegSaveAreaPtrTy = IRB.getPtrTy();
5297
5298 // Read the stack pointer from the va_list.
5299 Value *StackSaveAreaPtr =
5300 IRB.CreateIntToPtr(getVAField64(IRB, VAListTag, 0), RegSaveAreaPtrTy);
5301
5302 // Read both the __gr_top and __gr_off and add them up.
5303 Value *GrTopSaveAreaPtr = getVAField64(IRB, VAListTag, 8);
5304 Value *GrOffSaveArea = getVAField32(IRB, VAListTag, 24);
5305
5306 Value *GrRegSaveAreaPtr = IRB.CreateIntToPtr(
5307 IRB.CreateAdd(GrTopSaveAreaPtr, GrOffSaveArea), RegSaveAreaPtrTy);
5308
5309 // Read both the __vr_top and __vr_off and add them up.
5310 Value *VrTopSaveAreaPtr = getVAField64(IRB, VAListTag, 16);
5311 Value *VrOffSaveArea = getVAField32(IRB, VAListTag, 28);
5312
5313 Value *VrRegSaveAreaPtr = IRB.CreateIntToPtr(
5314 IRB.CreateAdd(VrTopSaveAreaPtr, VrOffSaveArea), RegSaveAreaPtrTy);
5315
5316 // It does not know how many named arguments is being used and, on the
5317 // callsite all the arguments were saved. Since __gr_off is defined as
5318 // '0 - ((8 - named_gr) * 8)', the idea is to just propagate the variadic
5319 // argument by ignoring the bytes of shadow from named arguments.
5320 Value *GrRegSaveAreaShadowPtrOff =
5321 IRB.CreateAdd(GrArgSize, GrOffSaveArea);
5322
5323 Value *GrRegSaveAreaShadowPtr =
5324 MSV.getShadowOriginPtr(GrRegSaveAreaPtr, IRB, IRB.getInt8Ty(),
5325 Align(8), /*isStore*/ true)
5326 .first;
5327
5328 Value *GrSrcPtr =
5329 IRB.CreateInBoundsPtrAdd(VAArgTLSCopy, GrRegSaveAreaShadowPtrOff);
5330 Value *GrCopySize = IRB.CreateSub(GrArgSize, GrRegSaveAreaShadowPtrOff);
5331
5332 IRB.CreateMemCpy(GrRegSaveAreaShadowPtr, Align(8), GrSrcPtr, Align(8),
5333 GrCopySize);
5334
5335 // Again, but for FP/SIMD values.
5336 Value *VrRegSaveAreaShadowPtrOff =
5337 IRB.CreateAdd(VrArgSize, VrOffSaveArea);
5338
5339 Value *VrRegSaveAreaShadowPtr =
5340 MSV.getShadowOriginPtr(VrRegSaveAreaPtr, IRB, IRB.getInt8Ty(),
5341 Align(8), /*isStore*/ true)
5342 .first;
5343
5344 Value *VrSrcPtr = IRB.CreateInBoundsPtrAdd(
5345 IRB.CreateInBoundsPtrAdd(VAArgTLSCopy,
5346 IRB.getInt32(AArch64VrBegOffset)),
5347 VrRegSaveAreaShadowPtrOff);
5348 Value *VrCopySize = IRB.CreateSub(VrArgSize, VrRegSaveAreaShadowPtrOff);
5349
5350 IRB.CreateMemCpy(VrRegSaveAreaShadowPtr, Align(8), VrSrcPtr, Align(8),
5351 VrCopySize);
5352
5353 // And finally for remaining arguments.
5354 Value *StackSaveAreaShadowPtr =
5355 MSV.getShadowOriginPtr(StackSaveAreaPtr, IRB, IRB.getInt8Ty(),
5356 Align(16), /*isStore*/ true)
5357 .first;
5358
5359 Value *StackSrcPtr = IRB.CreateInBoundsPtrAdd(
5360 VAArgTLSCopy, IRB.getInt32(AArch64VAEndOffset));
5361
5362 IRB.CreateMemCpy(StackSaveAreaShadowPtr, Align(16), StackSrcPtr,
5363 Align(16), VAArgOverflowSize);
5364 }
5365 }
5366};
5367
5368/// PowerPC64-specific implementation of VarArgHelper.
5369struct VarArgPowerPC64Helper : public VarArgHelperBase {
5370 AllocaInst *VAArgTLSCopy = nullptr;
5371 Value *VAArgSize = nullptr;
5372
5373 VarArgPowerPC64Helper(Function &F, MemorySanitizer &MS,
5374 MemorySanitizerVisitor &MSV)
5375 : VarArgHelperBase(F, MS, MSV, /*VAListTagSize=*/8) {}
5376
5377 void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {
5378 // For PowerPC, we need to deal with alignment of stack arguments -
5379 // they are mostly aligned to 8 bytes, but vectors and i128 arrays
5380 // are aligned to 16 bytes, byvals can be aligned to 8 or 16 bytes,
5381 // For that reason, we compute current offset from stack pointer (which is
5382 // always properly aligned), and offset for the first vararg, then subtract
5383 // them.
5384 unsigned VAArgBase;
5385 Triple TargetTriple(F.getParent()->getTargetTriple());
5386 // Parameter save area starts at 48 bytes from frame pointer for ABIv1,
5387 // and 32 bytes for ABIv2. This is usually determined by target
5388 // endianness, but in theory could be overridden by function attribute.
5389 if (TargetTriple.getArch() == Triple::ppc64)
5390 VAArgBase = 48;
5391 else
5392 VAArgBase = 32;
5393 unsigned VAArgOffset = VAArgBase;
5394 const DataLayout &DL = F.getParent()->getDataLayout();
5395 for (const auto &[ArgNo, A] : llvm::enumerate(CB.args())) {
5396 bool IsFixed = ArgNo < CB.getFunctionType()->getNumParams();
5397 bool IsByVal = CB.paramHasAttr(ArgNo, Attribute::ByVal);
5398 if (IsByVal) {
5399 assert(A->getType()->isPointerTy());
5400 Type *RealTy = CB.getParamByValType(ArgNo);
5401 uint64_t ArgSize = DL.getTypeAllocSize(RealTy);
5402 Align ArgAlign = CB.getParamAlign(ArgNo).value_or(Align(8));
5403 if (ArgAlign < 8)
5404 ArgAlign = Align(8);
5405 VAArgOffset = alignTo(VAArgOffset, ArgAlign);
5406 if (!IsFixed) {
5407 Value *Base = getShadowPtrForVAArgument(
5408 RealTy, IRB, VAArgOffset - VAArgBase, ArgSize);
5409 if (Base) {
5410 Value *AShadowPtr, *AOriginPtr;
5411 std::tie(AShadowPtr, AOriginPtr) =
5412 MSV.getShadowOriginPtr(A, IRB, IRB.getInt8Ty(),
5413 kShadowTLSAlignment, /*isStore*/ false);
5414
5415 IRB.CreateMemCpy(Base, kShadowTLSAlignment, AShadowPtr,
5416 kShadowTLSAlignment, ArgSize);
5417 }
5418 }
5419 VAArgOffset += alignTo(ArgSize, Align(8));
5420 } else {
5421 Value *Base;
5422 uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
5423 Align ArgAlign = Align(8);
5424 if (A->getType()->isArrayTy()) {
5425 // Arrays are aligned to element size, except for long double
5426 // arrays, which are aligned to 8 bytes.
5427 Type *ElementTy = A->getType()->getArrayElementType();
5428 if (!ElementTy->isPPC_FP128Ty())
5429 ArgAlign = Align(DL.getTypeAllocSize(ElementTy));
5430 } else if (A->getType()->isVectorTy()) {
5431 // Vectors are naturally aligned.
5432 ArgAlign = Align(ArgSize);
5433 }
5434 if (ArgAlign < 8)
5435 ArgAlign = Align(8);
5436 VAArgOffset = alignTo(VAArgOffset, ArgAlign);
5437 if (DL.isBigEndian()) {
5438 // Adjusting the shadow for argument with size < 8 to match the
5439 // placement of bits in big endian system
5440 if (ArgSize < 8)
5441 VAArgOffset += (8 - ArgSize);
5442 }
5443 if (!IsFixed) {
5444 Base = getShadowPtrForVAArgument(A->getType(), IRB,
5445 VAArgOffset - VAArgBase, ArgSize);
5446 if (Base)
5447 IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
5448 }
5449 VAArgOffset += ArgSize;
5450 VAArgOffset = alignTo(VAArgOffset, Align(8));
5451 }
5452 if (IsFixed)
5453 VAArgBase = VAArgOffset;
5454 }
5455
5456 Constant *TotalVAArgSize =
5457 ConstantInt::get(IRB.getInt64Ty(), VAArgOffset - VAArgBase);
5458 // Here using VAArgOverflowSizeTLS as VAArgSizeTLS to avoid creation of
5459 // a new class member i.e. it is the total size of all VarArgs.
5460 IRB.CreateStore(TotalVAArgSize, MS.VAArgOverflowSizeTLS);
5461 }
5462
5463 void finalizeInstrumentation() override {
5464 assert(!VAArgSize && !VAArgTLSCopy &&
5465 "finalizeInstrumentation called twice");
5466 IRBuilder<> IRB(MSV.FnPrologueEnd);
5467 VAArgSize = IRB.CreateLoad(IRB.getInt64Ty(), MS.VAArgOverflowSizeTLS);
5468 Value *CopySize =
5469 IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, 0), VAArgSize);
5470
5471 if (!VAStartInstrumentationList.empty()) {
5472 // If there is a va_start in this function, make a backup copy of
5473 // va_arg_tls somewhere in the function entry block.
5474
5475 VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
5476 VAArgTLSCopy->setAlignment(kShadowTLSAlignment);
5477 IRB.CreateMemSet(VAArgTLSCopy, Constant::getNullValue(IRB.getInt8Ty()),
5478 CopySize, kShadowTLSAlignment, false);
5479
5480 Value *SrcSize = IRB.CreateBinaryIntrinsic(
5481 Intrinsic::umin, CopySize,
5482 ConstantInt::get(MS.IntptrTy, kParamTLSSize));
5483 IRB.CreateMemCpy(VAArgTLSCopy, kShadowTLSAlignment, MS.VAArgTLS,
5484 kShadowTLSAlignment, SrcSize);
5485 }
5486
5487 // Instrument va_start.
5488 // Copy va_list shadow from the backup copy of the TLS contents.
5489 for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
5490 CallInst *OrigInst = VAStartInstrumentationList[i];
5491 NextNodeIRBuilder IRB(OrigInst);
5492 Value *VAListTag = OrigInst->getArgOperand(0);
5493 Type *RegSaveAreaPtrTy = PointerType::getUnqual(*MS.C); // i64*
5494 Value *RegSaveAreaPtrPtr =
5495 IRB.CreateIntToPtr(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
5496 PointerType::get(RegSaveAreaPtrTy, 0));
5497 Value *RegSaveAreaPtr =
5498 IRB.CreateLoad(RegSaveAreaPtrTy, RegSaveAreaPtrPtr);
5499 Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
5500 const Align Alignment = Align(8);
5501 std::tie(RegSaveAreaShadowPtr, RegSaveAreaOriginPtr) =
5502 MSV.getShadowOriginPtr(RegSaveAreaPtr, IRB, IRB.getInt8Ty(),
5503 Alignment, /*isStore*/ true);
5504 IRB.CreateMemCpy(RegSaveAreaShadowPtr, Alignment, VAArgTLSCopy, Alignment,
5505 CopySize);
5506 }
5507 }
5508};
5509
5510/// SystemZ-specific implementation of VarArgHelper.
5511struct VarArgSystemZHelper : public VarArgHelperBase {
5512 static const unsigned SystemZGpOffset = 16;
5513 static const unsigned SystemZGpEndOffset = 56;
5514 static const unsigned SystemZFpOffset = 128;
5515 static const unsigned SystemZFpEndOffset = 160;
5516 static const unsigned SystemZMaxVrArgs = 8;
5517 static const unsigned SystemZRegSaveAreaSize = 160;
5518 static const unsigned SystemZOverflowOffset = 160;
5519 static const unsigned SystemZVAListTagSize = 32;
5520 static const unsigned SystemZOverflowArgAreaPtrOffset = 16;
5521 static const unsigned SystemZRegSaveAreaPtrOffset = 24;
5522
5523 bool IsSoftFloatABI;
5524 AllocaInst *VAArgTLSCopy = nullptr;
5525 AllocaInst *VAArgTLSOriginCopy = nullptr;
5526 Value *VAArgOverflowSize = nullptr;
5527
5528 enum class ArgKind {
5529 GeneralPurpose,
5530 FloatingPoint,
5531 Vector,
5532 Memory,
5533 Indirect,
5534 };
5535
5536 enum class ShadowExtension { None, Zero, Sign };
5537
5538 VarArgSystemZHelper(Function &F, MemorySanitizer &MS,
5539 MemorySanitizerVisitor &MSV)
5540 : VarArgHelperBase(F, MS, MSV, SystemZVAListTagSize),
5541 IsSoftFloatABI(F.getFnAttribute("use-soft-float").getValueAsBool()) {}
5542
5543 ArgKind classifyArgument(Type *T) {
5544 // T is a SystemZABIInfo::classifyArgumentType() output, and there are
5545 // only a few possibilities of what it can be. In particular, enums, single
5546 // element structs and large types have already been taken care of.
5547
5548 // Some i128 and fp128 arguments are converted to pointers only in the
5549 // back end.
5550 if (T->isIntegerTy(128) || T->isFP128Ty())
5551 return ArgKind::Indirect;
5552 if (T->isFloatingPointTy())
5553 return IsSoftFloatABI ? ArgKind::GeneralPurpose : ArgKind::FloatingPoint;
5554 if (T->isIntegerTy() || T->isPointerTy())
5555 return ArgKind::GeneralPurpose;
5556 if (T->isVectorTy())
5557 return ArgKind::Vector;
5558 return ArgKind::Memory;
5559 }
5560
5561 ShadowExtension getShadowExtension(const CallBase &CB, unsigned ArgNo) {
5562 // ABI says: "One of the simple integer types no more than 64 bits wide.
5563 // ... If such an argument is shorter than 64 bits, replace it by a full
5564 // 64-bit integer representing the same number, using sign or zero
5565 // extension". Shadow for an integer argument has the same type as the
5566 // argument itself, so it can be sign or zero extended as well.
5567 bool ZExt = CB.paramHasAttr(ArgNo, Attribute::ZExt);
5568 bool SExt = CB.paramHasAttr(ArgNo, Attribute::SExt);
5569 if (ZExt) {
5570 assert(!SExt);
5571 return ShadowExtension::Zero;
5572 }
5573 if (SExt) {
5574 assert(!ZExt);
5575 return ShadowExtension::Sign;
5576 }
5577 return ShadowExtension::None;
5578 }
5579
5580 void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {
5581 unsigned GpOffset = SystemZGpOffset;
5582 unsigned FpOffset = SystemZFpOffset;
5583 unsigned VrIndex = 0;
5584 unsigned OverflowOffset = SystemZOverflowOffset;
5585 const DataLayout &DL = F.getParent()->getDataLayout();
5586 for (const auto &[ArgNo, A] : llvm::enumerate(CB.args())) {
5587 bool IsFixed = ArgNo < CB.getFunctionType()->getNumParams();
5588 // SystemZABIInfo does not produce ByVal parameters.
5589 assert(!CB.paramHasAttr(ArgNo, Attribute::ByVal));
5590 Type *T = A->getType();
5591 ArgKind AK = classifyArgument(T);
5592 if (AK == ArgKind::Indirect) {
5593 T = PointerType::get(T, 0);
5594 AK = ArgKind::GeneralPurpose;
5595 }
5596 if (AK == ArgKind::GeneralPurpose && GpOffset >= SystemZGpEndOffset)
5597 AK = ArgKind::Memory;
5598 if (AK == ArgKind::FloatingPoint && FpOffset >= SystemZFpEndOffset)
5599 AK = ArgKind::Memory;
5600 if (AK == ArgKind::Vector && (VrIndex >= SystemZMaxVrArgs || !IsFixed))
5601 AK = ArgKind::Memory;
5602 Value *ShadowBase = nullptr;
5603 Value *OriginBase = nullptr;
5604 ShadowExtension SE = ShadowExtension::None;
5605 switch (AK) {
5606 case ArgKind::GeneralPurpose: {
5607 // Always keep track of GpOffset, but store shadow only for varargs.
5608 uint64_t ArgSize = 8;
5609 if (GpOffset + ArgSize <= kParamTLSSize) {
5610 if (!IsFixed) {
5611 SE = getShadowExtension(CB, ArgNo);
5612 uint64_t GapSize = 0;
5613 if (SE == ShadowExtension::None) {
5614 uint64_t ArgAllocSize = DL.getTypeAllocSize(T);
5615 assert(ArgAllocSize <= ArgSize);
5616 GapSize = ArgSize - ArgAllocSize;
5617 }
5618 ShadowBase = getShadowAddrForVAArgument(IRB, GpOffset + GapSize);
5619 if (MS.TrackOrigins)
5620 OriginBase = getOriginPtrForVAArgument(IRB, GpOffset + GapSize);
5621 }
5622 GpOffset += ArgSize;
5623 } else {
5624 GpOffset = kParamTLSSize;
5625 }
5626 break;
5627 }
5628 case ArgKind::FloatingPoint: {
5629 // Always keep track of FpOffset, but store shadow only for varargs.
5630 uint64_t ArgSize = 8;
5631 if (FpOffset + ArgSize <= kParamTLSSize) {
5632 if (!IsFixed) {
5633 // PoP says: "A short floating-point datum requires only the
5634 // left-most 32 bit positions of a floating-point register".
5635 // Therefore, in contrast to AK_GeneralPurpose and AK_Memory,
5636 // don't extend shadow and don't mind the gap.
5637 ShadowBase = getShadowAddrForVAArgument(IRB, FpOffset);
5638 if (MS.TrackOrigins)
5639 OriginBase = getOriginPtrForVAArgument(IRB, FpOffset);
5640 }
5641 FpOffset += ArgSize;
5642 } else {
5643 FpOffset = kParamTLSSize;
5644 }
5645 break;
5646 }
5647 case ArgKind::Vector: {
5648 // Keep track of VrIndex. No need to store shadow, since vector varargs
5649 // go through AK_Memory.
5650 assert(IsFixed);
5651 VrIndex++;
5652 break;
5653 }
5654 case ArgKind::Memory: {
5655 // Keep track of OverflowOffset and store shadow only for varargs.
5656 // Ignore fixed args, since we need to copy only the vararg portion of
5657 // the overflow area shadow.
5658 if (!IsFixed) {
5659 uint64_t ArgAllocSize = DL.getTypeAllocSize(T);
5660 uint64_t ArgSize = alignTo(ArgAllocSize, 8);
5661 if (OverflowOffset + ArgSize <= kParamTLSSize) {
5662 SE = getShadowExtension(CB, ArgNo);
5663 uint64_t GapSize =
5664 SE == ShadowExtension::None ? ArgSize - ArgAllocSize : 0;
5665 ShadowBase =
5666 getShadowAddrForVAArgument(IRB, OverflowOffset + GapSize);
5667 if (MS.TrackOrigins)
5668 OriginBase =
5669 getOriginPtrForVAArgument(IRB, OverflowOffset + GapSize);
5670 OverflowOffset += ArgSize;
5671 } else {
5672 OverflowOffset = kParamTLSSize;
5673 }
5674 }
5675 break;
5676 }
5677 case ArgKind::Indirect:
5678 llvm_unreachable("Indirect must be converted to GeneralPurpose");
5679 }
5680 if (ShadowBase == nullptr)
5681 continue;
5682 Value *Shadow = MSV.getShadow(A);
5683 if (SE != ShadowExtension::None)
5684 Shadow = MSV.CreateShadowCast(IRB, Shadow, IRB.getInt64Ty(),
5685 /*Signed*/ SE == ShadowExtension::Sign);
5686 ShadowBase = IRB.CreateIntToPtr(
5687 ShadowBase, PointerType::get(Shadow->getType(), 0), "_msarg_va_s");
5688 IRB.CreateStore(Shadow, ShadowBase);
5689 if (MS.TrackOrigins) {
5690 Value *Origin = MSV.getOrigin(A);
5691 TypeSize StoreSize = DL.getTypeStoreSize(Shadow->getType());
5692 MSV.paintOrigin(IRB, Origin, OriginBase, StoreSize,
5694 }
5695 }
5696 Constant *OverflowSize = ConstantInt::get(
5697 IRB.getInt64Ty(), OverflowOffset - SystemZOverflowOffset);
5698 IRB.CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
5699 }
5700
5701 void copyRegSaveArea(IRBuilder<> &IRB, Value *VAListTag) {
5702 Type *RegSaveAreaPtrTy = PointerType::getUnqual(*MS.C); // i64*
5703 Value *RegSaveAreaPtrPtr = IRB.CreateIntToPtr(
5704 IRB.CreateAdd(
5705 IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
5706 ConstantInt::get(MS.IntptrTy, SystemZRegSaveAreaPtrOffset)),
5707 PointerType::get(RegSaveAreaPtrTy, 0));
5708 Value *RegSaveAreaPtr = IRB.CreateLoad(RegSaveAreaPtrTy, RegSaveAreaPtrPtr);
5709 Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
5710 const Align Alignment = Align(8);
5711 std::tie(RegSaveAreaShadowPtr, RegSaveAreaOriginPtr) =
5712 MSV.getShadowOriginPtr(RegSaveAreaPtr, IRB, IRB.getInt8Ty(), Alignment,
5713 /*isStore*/ true);
5714 // TODO(iii): copy only fragments filled by visitCallBase()
5715 // TODO(iii): support packed-stack && !use-soft-float
5716 // For use-soft-float functions, it is enough to copy just the GPRs.
5717 unsigned RegSaveAreaSize =
5718 IsSoftFloatABI ? SystemZGpEndOffset : SystemZRegSaveAreaSize;
5719 IRB.CreateMemCpy(RegSaveAreaShadowPtr, Alignment, VAArgTLSCopy, Alignment,
5720 RegSaveAreaSize);
5721 if (MS.TrackOrigins)
5722 IRB.CreateMemCpy(RegSaveAreaOriginPtr, Alignment, VAArgTLSOriginCopy,
5723 Alignment, RegSaveAreaSize);
5724 }
5725
5726 // FIXME: This implementation limits OverflowOffset to kParamTLSSize, so we
5727 // don't know real overflow size and can't clear shadow beyond kParamTLSSize.
5728 void copyOverflowArea(IRBuilder<> &IRB, Value *VAListTag) {
5729 Type *OverflowArgAreaPtrTy = PointerType::getUnqual(*MS.C); // i64*
5730 Value *OverflowArgAreaPtrPtr = IRB.CreateIntToPtr(
5731 IRB.CreateAdd(
5732 IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
5733 ConstantInt::get(MS.IntptrTy, SystemZOverflowArgAreaPtrOffset)),
5734 PointerType::get(OverflowArgAreaPtrTy, 0));
5735 Value *OverflowArgAreaPtr =
5736 IRB.CreateLoad(OverflowArgAreaPtrTy, OverflowArgAreaPtrPtr);
5737 Value *OverflowArgAreaShadowPtr, *OverflowArgAreaOriginPtr;
5738 const Align Alignment = Align(8);
5739 std::tie(OverflowArgAreaShadowPtr, OverflowArgAreaOriginPtr) =
5740 MSV.getShadowOriginPtr(OverflowArgAreaPtr, IRB, IRB.getInt8Ty(),
5741 Alignment, /*isStore*/ true);
5742 Value *SrcPtr = IRB.CreateConstGEP1_32(IRB.getInt8Ty(), VAArgTLSCopy,
5743 SystemZOverflowOffset);
5744 IRB.CreateMemCpy(OverflowArgAreaShadowPtr, Alignment, SrcPtr, Alignment,
5745 VAArgOverflowSize);
5746 if (MS.TrackOrigins) {
5747 SrcPtr = IRB.CreateConstGEP1_32(IRB.getInt8Ty(), VAArgTLSOriginCopy,
5748 SystemZOverflowOffset);
5749 IRB.CreateMemCpy(OverflowArgAreaOriginPtr, Alignment, SrcPtr, Alignment,
5750 VAArgOverflowSize);
5751 }
5752 }
5753
5754 void finalizeInstrumentation() override {
5755 assert(!VAArgOverflowSize && !VAArgTLSCopy &&
5756 "finalizeInstrumentation called twice");
5757 if (!VAStartInstrumentationList.empty()) {
5758 // If there is a va_start in this function, make a backup copy of
5759 // va_arg_tls somewhere in the function entry block.
5760 IRBuilder<> IRB(MSV.FnPrologueEnd);
5761 VAArgOverflowSize =
5762 IRB.CreateLoad(IRB.getInt64Ty(), MS.VAArgOverflowSizeTLS);
5763 Value *CopySize =
5764 IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, SystemZOverflowOffset),
5765 VAArgOverflowSize);
5766 VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
5767 VAArgTLSCopy->setAlignment(kShadowTLSAlignment);
5768 IRB.CreateMemSet(VAArgTLSCopy, Constant::getNullValue(IRB.getInt8Ty()),
5769 CopySize, kShadowTLSAlignment, false);
5770
5771 Value *SrcSize = IRB.CreateBinaryIntrinsic(
5772 Intrinsic::umin, CopySize,
5773 ConstantInt::get(MS.IntptrTy, kParamTLSSize));
5774 IRB.CreateMemCpy(VAArgTLSCopy, kShadowTLSAlignment, MS.VAArgTLS,
5775 kShadowTLSAlignment, SrcSize);
5776 if (MS.TrackOrigins) {
5777 VAArgTLSOriginCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
5778 VAArgTLSOriginCopy->setAlignment(kShadowTLSAlignment);
5779 IRB.CreateMemCpy(VAArgTLSOriginCopy, kShadowTLSAlignment,
5780 MS.VAArgOriginTLS, kShadowTLSAlignment, SrcSize);
5781 }
5782 }
5783
5784 // Instrument va_start.
5785 // Copy va_list shadow from the backup copy of the TLS contents.
5786 for (size_t VaStartNo = 0, VaStartNum = VAStartInstrumentationList.size();
5787 VaStartNo < VaStartNum; VaStartNo++) {
5788 CallInst *OrigInst = VAStartInstrumentationList[VaStartNo];
5789 NextNodeIRBuilder IRB(OrigInst);
5790 Value *VAListTag = OrigInst->getArgOperand(0);
5791 copyRegSaveArea(IRB, VAListTag);
5792 copyOverflowArea(IRB, VAListTag);
5793 }
5794 }
5795};
5796
5797// Loongarch64 is not a MIPS, but the current vargs calling convention matches
5798// the MIPS.
5799using VarArgLoongArch64Helper = VarArgMIPS64Helper;
5800
5801/// A no-op implementation of VarArgHelper.
5802struct VarArgNoOpHelper : public VarArgHelper {
5803 VarArgNoOpHelper(Function &F, MemorySanitizer &MS,
5804 MemorySanitizerVisitor &MSV) {}
5805
5806 void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {}
5807
5808 void visitVAStartInst(VAStartInst &I) override {}
5809
5810 void visitVACopyInst(VACopyInst &I) override {}
5811
5812 void finalizeInstrumentation() override {}
5813};
5814
5815} // end anonymous namespace
5816
5817static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
5818 MemorySanitizerVisitor &Visitor) {
5819 // VarArg handling is only implemented on AMD64. False positives are possible
5820 // on other platforms.
5821 Triple TargetTriple(Func.getParent()->getTargetTriple());
5822 if (TargetTriple.getArch() == Triple::x86_64)
5823 return new VarArgAMD64Helper(Func, Msan, Visitor);
5824 else if (TargetTriple.isMIPS64())
5825 return new VarArgMIPS64Helper(Func, Msan, Visitor);
5826 else if (TargetTriple.getArch() == Triple::aarch64)
5827 return new VarArgAArch64Helper(Func, Msan, Visitor);
5828 else if (TargetTriple.getArch() == Triple::ppc64 ||
5829 TargetTriple.getArch() == Triple::ppc64le)
5830 return new VarArgPowerPC64Helper(Func, Msan, Visitor);
5831 else if (TargetTriple.getArch() == Triple::systemz)
5832 return new VarArgSystemZHelper(Func, Msan, Visitor);
5833 else if (TargetTriple.isLoongArch64())
5834 return new VarArgLoongArch64Helper(Func, Msan, Visitor);
5835 else
5836 return new VarArgNoOpHelper(Func, Msan, Visitor);
5837}
5838
5839bool MemorySanitizer::sanitizeFunction(Function &F, TargetLibraryInfo &TLI) {
5840 if (!CompileKernel && F.getName() == kMsanModuleCtorName)
5841 return false;
5842
5843 if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
5844 return false;
5845
5846 MemorySanitizerVisitor Visitor(F, *this, TLI);
5847
5848 // Clear out memory attributes.
5850 B.addAttribute(Attribute::Memory).addAttribute(Attribute::Speculatable);
5851 F.removeFnAttrs(B);
5852
5853 return Visitor.runOnFunction();
5854}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const LLT S1
This file implements a class to represent arbitrary precision integral constant values and operations...
static bool isStore(int Opcode)
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
static const size_t kNumberOfAccessSizes
VarLocInsertPt getNextNode(const DbgRecord *DVR)
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static AtomicOrdering addReleaseOrdering(AtomicOrdering AO)
static AtomicOrdering addAcquireOrdering(AtomicOrdering AO)
static bool isAMustTailRetVal(Value *RetVal)
return RetTy
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
Definition: DebugCounter.h:182
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseMap class.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
@ Default
Definition: DwarfDebug.cpp:87
uint64_t Addr
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
static bool runOnFunction(Function &F, bool PostInlining)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool isSigned(unsigned int Opcode)
This is the interface for a simple mod/ref and alias analysis over globals.
static size_t TypeSizeToSizeIndex(uint32_t TypeSize)
#define op(i)
Hexagon Common GEP
#define _
Hexagon Vector Combine
static LVOptions Options
Definition: LVOptions.cpp:25
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static const MemoryMapParams Linux_LoongArch64_MemoryMapParams
static const PlatformMemoryMapParams Linux_S390_MemoryMapParams
static const Align kMinOriginAlignment
static const MemoryMapParams Linux_X86_64_MemoryMapParams
static cl::opt< uint64_t > ClShadowBase("msan-shadow-base", cl::desc("Define custom MSan ShadowBase"), cl::Hidden, cl::init(0))
static cl::opt< bool > ClDumpStrictInstructions("msan-dump-strict-instructions", cl::desc("print out instructions with default strict semantics"), cl::Hidden, cl::init(false))
static cl::opt< bool > ClHandleICmpExact("msan-handle-icmp-exact", cl::desc("exact handling of relational integer ICmp"), cl::Hidden, cl::init(false))
static const PlatformMemoryMapParams Linux_X86_MemoryMapParams
static cl::opt< uint64_t > ClOriginBase("msan-origin-base", cl::desc("Define custom MSan OriginBase"), cl::Hidden, cl::init(0))
static cl::opt< bool > ClCheckConstantShadow("msan-check-constant-shadow", cl::desc("Insert checks for constant shadow values"), cl::Hidden, cl::init(true))
static const PlatformMemoryMapParams Linux_LoongArch_MemoryMapParams
static const MemoryMapParams NetBSD_X86_64_MemoryMapParams
static const PlatformMemoryMapParams Linux_MIPS_MemoryMapParams
static const unsigned kOriginSize
static cl::opt< bool > ClWithComdat("msan-with-comdat", cl::desc("Place MSan constructors in comdat sections"), cl::Hidden, cl::init(false))
static cl::opt< int > ClTrackOrigins("msan-track-origins", cl::desc("Track origins (allocation sites) of poisoned memory"), cl::Hidden, cl::init(0))
Track origins of uninitialized values.
static cl::opt< int > ClInstrumentationWithCallThreshold("msan-instrumentation-with-call-threshold", cl::desc("If the function being instrumented requires more than " "this number of checks and origin stores, use callbacks instead of " "inline checks (-1 means never use callbacks)."), cl::Hidden, cl::init(3500))
static cl::opt< int > ClPoisonStackPattern("msan-poison-stack-pattern", cl::desc("poison uninitialized stack variables with the given pattern"), cl::Hidden, cl::init(0xff))
static const Align kShadowTLSAlignment
static const PlatformMemoryMapParams Linux_ARM_MemoryMapParams
static Constant * getOrInsertGlobal(Module &M, StringRef Name, Type *Ty)
static const MemoryMapParams Linux_S390X_MemoryMapParams
static cl::opt< bool > ClPoisonUndef("msan-poison-undef", cl::desc("poison undef temps"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClPoisonStack("msan-poison-stack", cl::desc("poison uninitialized stack variables"), cl::Hidden, cl::init(true))
static const MemoryMapParams Linux_I386_MemoryMapParams
const char kMsanInitName[]
static cl::opt< bool > ClPrintStackNames("msan-print-stack-names", cl::desc("Print name of local stack variable"), cl::Hidden, cl::init(true))
static const MemoryMapParams Linux_AArch64_MemoryMapParams
static cl::opt< uint64_t > ClAndMask("msan-and-mask", cl::desc("Define custom MSan AndMask"), cl::Hidden, cl::init(0))
static cl::opt< bool > ClHandleLifetimeIntrinsics("msan-handle-lifetime-intrinsics", cl::desc("when possible, poison scoped variables at the beginning of the scope " "(slower, but more precise)"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClKeepGoing("msan-keep-going", cl::desc("keep going after reporting a UMR"), cl::Hidden, cl::init(false))
static const MemoryMapParams FreeBSD_X86_64_MemoryMapParams
static GlobalVariable * createPrivateConstGlobalForString(Module &M, StringRef Str)
Create a non-const global initialized with the given string.
static const PlatformMemoryMapParams Linux_PowerPC_MemoryMapParams
static const size_t kNumberOfAccessSizes
static cl::opt< bool > ClEagerChecks("msan-eager-checks", cl::desc("check arguments and return values at function call boundaries"), cl::Hidden, cl::init(false))
static cl::opt< int > ClDisambiguateWarning("msan-disambiguate-warning-threshold", cl::desc("Define threshold for number of checks per " "debug location to force origin update."), cl::Hidden, cl::init(3))
static VarArgHelper * CreateVarArgHelper(Function &Func, MemorySanitizer &Msan, MemorySanitizerVisitor &Visitor)
static const MemoryMapParams Linux_MIPS64_MemoryMapParams
static const MemoryMapParams Linux_PowerPC64_MemoryMapParams
static cl::opt< uint64_t > ClXorMask("msan-xor-mask", cl::desc("Define custom MSan XorMask"), cl::Hidden, cl::init(0))
static cl::opt< bool > ClHandleAsmConservative("msan-handle-asm-conservative", cl::desc("conservative handling of inline assembly"), cl::Hidden, cl::init(true))
static const PlatformMemoryMapParams FreeBSD_X86_MemoryMapParams
static const PlatformMemoryMapParams FreeBSD_ARM_MemoryMapParams
static const unsigned kParamTLSSize
static cl::opt< bool > ClHandleICmp("msan-handle-icmp", cl::desc("propagate shadow through ICmpEQ and ICmpNE"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClEnableKmsan("msan-kernel", cl::desc("Enable KernelMemorySanitizer instrumentation"), cl::Hidden, cl::init(false))
static cl::opt< bool > ClPoisonStackWithCall("msan-poison-stack-with-call", cl::desc("poison uninitialized stack variables with a call"), cl::Hidden, cl::init(false))
static const PlatformMemoryMapParams NetBSD_X86_MemoryMapParams
static const unsigned kRetvalTLSSize
static const MemoryMapParams FreeBSD_AArch64_MemoryMapParams
const char kMsanModuleCtorName[]
static const MemoryMapParams FreeBSD_I386_MemoryMapParams
static cl::opt< bool > ClCheckAccessAddress("msan-check-access-address", cl::desc("report accesses through a pointer which has poisoned shadow"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClDisableChecks("msan-disable-checks", cl::desc("Apply no_sanitize to the whole file"), cl::Hidden, cl::init(false))
Module.h This file contains the declarations for the Module class.
FunctionAnalysisManager FAM
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Definition: SMEABIPass.cpp:49
raw_pwrite_stream & OS
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:40
@ Struct
@ None
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:76
an instruction to allocate memory on the stack
Definition: Instructions.h:59
void setAlignment(Align Align)
Definition: Instructions.h:136
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:473
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:168
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:647
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:539
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:748
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
iterator end()
Definition: BasicBlock.h:443
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:409
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:452
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:165
This class represents a no-op cast from one type to another.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1494
bool isInlineAsm() const
Check if this call is an inline asm statement.
Definition: InstrTypes.h:1809
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1742
bool hasRetAttr(Attribute::AttrKind Kind) const
Determine whether the return value has the given attribute.
Definition: InstrTypes.h:1950
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
void setCannotMerge()
Definition: InstrTypes.h:2286
MaybeAlign getParamAlign(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
Definition: InstrTypes.h:2109
Type * getParamByValType(unsigned ArgNo) const
Extract the byval type for a call or parameter.
Definition: InstrTypes.h:2118
Value * getCalledOperand() const
Definition: InstrTypes.h:1735
Type * getParamElementType(unsigned ArgNo) const
Extract the elementtype type for a parameter.
Definition: InstrTypes.h:2156
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1687
void setArgOperand(unsigned i, Value *v)
Definition: InstrTypes.h:1692
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1600
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1678
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Adds the attribute to the indicated argument.
Definition: InstrTypes.h:1871
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:601
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:993
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:1022
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:1023
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:1020
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:1021
Combiner implementation.
Definition: Combiner.h:34
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1291
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2881
static Constant * get(LLVMContext &Context, ArrayRef< uint8_t > Elts)
get() constructors - Return a constant with vector type with an element count and element type matchi...
Definition: Constants.cpp:2897
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition: Constants.h:123
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1356
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1449
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1398
This is an important base class in LLVM.
Definition: Constant.h:41
static Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:417
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
Definition: Constants.cpp:107
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:432
bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition: Constants.cpp:76
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
static bool shouldExecute(unsigned CounterName)
Definition: DebugCounter.h:72
A debug info location.
Definition: DebugLoc.h:33
bool empty() const
Definition: DenseMap.h:98
This instruction extracts a single (scalar) element from a VectorType value.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
Class to represent fixed width SIMD vectors.
Definition: DerivedTypes.h:539
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition: Type.cpp:692
This class represents a freeze function that returns random concrete value if an operand is either a ...
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:168
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:142
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:973
void setComdat(Comdat *C)
Definition: Globals.cpp:197
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:60
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
Analysis pass providing a never-invalidated alias analysis result.
This instruction compares its operands according to the predicate given to the constructor.
CallInst * CreateMaskedCompressStore(Value *Val, Value *Ptr, Value *Mask=nullptr)
Create a call to Masked Compress Store intrinsic.
Definition: IRBuilder.cpp:704
CallInst * CreateMaskedExpandLoad(Type *Ty, Value *Ptr, Value *Mask=nullptr, Value *PassThru=nullptr, const Twine &Name="")
Create a call to Masked Expand Load intrinsic.
Definition: IRBuilder.cpp:686
Value * CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with 2 operands which is mangled on the first type.
Definition: IRBuilder.cpp:921
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2472
Value * CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, const Twine &Name="")
Definition: IRBuilder.h:1881
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Definition: IRBuilder.h:1773
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2523
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2460
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Definition: IRBuilder.h:539
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Definition: IRBuilder.h:1807
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition: IRBuilder.h:2039
CallInst * CreateAndReduce(Value *Src)
Create a vector int AND reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:441
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2170
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2516
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition: IRBuilder.cpp:932
CallInst * CreateMaskedLoad(Type *Ty, Value *Ptr, Align Alignment, Value *Mask, Value *PassThru=nullptr, const Twine &Name="")
Create a call to Masked Load intrinsic.
Definition: IRBuilder.cpp:578
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, MaybeAlign Align, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memset to the specified pointer and the specified value.
Definition: IRBuilder.h:595
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1110
BasicBlock::iterator GetInsertPoint() const
Definition: IRBuilder.h:175
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2033
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2122
Value * CreateTypeSize(Type *DstType, TypeSize Size)
Create an expression which evaluates to the number of units in Size at runtime.
Definition: IRBuilder.cpp:104
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1437
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:526
ConstantInt * getInt8(uint8_t C)
Get a constant 8-bit value.
Definition: IRBuilder.h:476
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
Definition: IRBuilder.h:531
Value * CreateUDiv(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1378
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2245
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNSW=false)
Definition: IRBuilder.h:1721
CallInst * CreateOrReduce(Value *Src)
Create a vector int OR reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:445
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:486
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2397
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1749
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2241
DebugLoc getCurrentDebugLocation() const
Get location information used by debugging information.
Definition: IRBuilder.cpp:63
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1344
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2127
ConstantInt * getIntN(unsigned N, uint64_t C)
Get a constant N-bit value, zero extended or truncated from a 64-bit value.
Definition: IRBuilder.h:497
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition: IRBuilder.h:1790
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1416
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2021
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2494
LLVMContext & getContext() const
Definition: IRBuilder.h:176
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1475
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1803
CallInst * CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment, Value *Mask)
Create a call to Masked Store intrinsic.
Definition: IRBuilder.cpp:598
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1327
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2117
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Definition: IRBuilder.h:2549
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2007
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1497
PointerType * getPtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer.
Definition: IRBuilder.h:569
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1666
Value * CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2273
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:2196
Value * CreateIsNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg == 0.
Definition: IRBuilder.h:2544
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:180
StoreInst * CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, bool isVolatile=false)
Definition: IRBuilder.h:1826
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2412
Value * CreateInBoundsPtrAdd(Value *Ptr, Value *Offset, const Twine &Name="")
Definition: IRBuilder.h:1983
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1519
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", bool IsInBounds=false)
Definition: IRBuilder.h:1866
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2351
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:516
CallInst * CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src, MaybeAlign SrcAlign, uint64_t Size, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *TBAAStructTag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memcpy between the specified pointers.
Definition: IRBuilder.h:659
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1361
CallInst * CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment, Value *Mask=nullptr)
Create a call to Masked Scatter intrinsic.
Definition: IRBuilder.cpp:661
CallInst * CreateMaskedGather(Type *Ty, Value *Ptrs, Align Alignment, Value *Mask=nullptr, Value *PassThru=nullptr, const Twine &Name="")
Create a call to Masked Gather intrinsic.
Definition: IRBuilder.cpp:630
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2666
std::vector< ConstraintInfo > ConstraintInfoVector
Definition: InlineAsm.h:121
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:631
This instruction inserts a single (scalar) element into a VectorType value.
This instruction inserts a struct field of array element value into an aggregate value.
Base class for instruction visitors.
Definition: InstVisitor.h:78
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:87
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:454
const BasicBlock * getParent() const
Definition: Instruction.h:152
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:359
This class represents a cast from an integer to a pointer.
Class to represent integer types.
Definition: DerivedTypes.h:40
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:278
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:47
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
The landingpad instruction holds all of the information necessary to generate correct exception handl...
An instruction for reading from memory.
Definition: Instructions.h:184
MDNode * createUnlikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards false destination.
Definition: MDBuilder.cpp:47
Metadata node.
Definition: Metadata.h:1067
This class wraps the llvm.memcpy intrinsic.
This class wraps the llvm.memmove intrinsic.
This class wraps the llvm.memset and llvm.memset.inline intrinsics.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
In order to facilitate speculative execution, many instructions do not invoke immediate undefined beh...
Definition: Constants.h:1396
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1827
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
void abandon()
Mark an analysis as abandoned.
Definition: Analysis.h:162
This class represents a cast from a pointer to an integer.
Resume the propagation of an exception.
Return a value (possibly void), from a function.
This class represents a sign extension of integer types.
This class represents the LLVM 'select' instruction.
bool remove(const value_type &X)
Remove an item from the set vector.
Definition: SetVector.h:188
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
This instruction constructs a fixed permutation of two input vectors.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:370
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
An instruction for storing to memory.
Definition: Instructions.h:317
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Class to represent struct types.
Definition: DerivedTypes.h:216
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:373
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
AttributeList getAttrList(LLVMContext *C, ArrayRef< unsigned > ArgNos, bool Signed, bool Ret=false, AttributeList AL=AttributeList()) const
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isMIPS64() const
Tests whether the target is MIPS 64-bit (little and big endian).
Definition: Triple.h:938
@ aarch64_be
Definition: Triple.h:52
@ loongarch64
Definition: Triple.h:62
@ mips64el
Definition: Triple.h:67
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:372
bool isLoongArch64() const
Tests whether the target is 64-bit LoongArch.
Definition: Triple.h:927
This class represents a truncation of integer types.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:255
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition: Type.h:166
static Type * getX86_MMXTy(LLVMContext &C)
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
static Type * getVoidTy(LLVMContext &C)
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:302
static IntegerType * getInt8Ty(LLVMContext &C)
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition: Type.h:243
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:228
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:348
'undef' values are things that do not have specified contents.
Definition: Constants.h:1348
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
Value * getOperand(unsigned i) const
Definition: User.h:169
unsigned getNumOperands() const
Definition: User.h:191
This represents the llvm.va_copy intrinsic.
This represents the llvm.va_start intrinsic.
See the file comment.
Definition: ValueMap.h:84
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: ValueMap.h:151
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:377
This class represents zero extension of integer types.
int getNumOccurrences() const
Definition: CommandLine.h:406
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:199
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
Definition: ilist_node.h:109
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This class provides various memory handling functions that manipulate MemoryBlock instances.
Definition: Memory.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:121
@ Win64
The C convention as implemented on Windows/x86-64 and AArch64.
Definition: CallingConv.h:159
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1471
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
Function * Kernel
Summary of a kernel (=entry point for target offloading).
Definition: OpenMPOpt.h:21
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
@ FalseVal
Definition: TGLexer.h:59
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:329
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition: MathExtras.h:337
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1680
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are are tuples (A,...
Definition: STLExtras.h:2406
AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
@ Done
Definition: Threading.h:61
std::pair< Function *, FunctionCallee > getOrCreateSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type * > InitArgTypes, ArrayRef< Value * > InitArgs, function_ref< void(Function *, FunctionCallee)> FunctionsCreatedCallback, StringRef VersionCheckName=StringRef(), bool Weak=false)
Creates sanitizer constructor function lazily.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
@ Or
Bitwise or logical OR of integers.
@ Add
Sum of integers.
std::pair< Instruction *, Value * > SplitBlockAndInsertSimpleForLoop(Value *End, Instruction *SplitBefore)
Insert a for (int i = 0; i < End; i++) loop structure (with the exception that End is assumed > 0,...
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
Definition: ModuleUtils.cpp:73
iterator_range< df_iterator< T > > depth_first(const T &G)
Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
void maybeMarkSanitizerLibraryCallNoBuiltin(CallInst *CI, const TargetLibraryInfo *TLI)
Given a CallInst, check if it calls a string function known to CodeGen, and mark it with NoBuiltin if...
Definition: Local.cpp:4099
bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Remove all blocks that can not be reached from the function's entry.
Definition: Local.cpp:3198
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:74