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