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