1 //===- MemorySanitizer.cpp - detector of uninitialized reads --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// This file is a part of MemorySanitizer, a detector of uninitialized
12 /// reads.
13 ///
14 /// The algorithm of the tool is similar to Memcheck
15 /// (http://goo.gl/QKbem). We associate a few shadow bits with every
16 /// byte of the application memory, poison the shadow of the malloc-ed
17 /// or alloca-ed memory, load the shadow bits on every memory read,
18 /// propagate the shadow bits through some of the arithmetic
19 /// instruction (including MOV), store the shadow bits on every memory
20 /// write, report a bug on some other instructions (e.g. JMP) if the
21 /// associated shadow is poisoned.
22 ///
23 /// But there are differences too. The first and the major one:
24 /// compiler instrumentation instead of binary instrumentation. This
25 /// gives us much better register allocation, possible compiler
26 /// optimizations and a fast start-up. But this brings the major issue
27 /// as well: msan needs to see all program events, including system
28 /// calls and reads/writes in system libraries, so we either need to
29 /// compile *everything* with msan or use a binary translation
30 /// component (e.g. DynamoRIO) to instrument pre-built libraries.
31 /// Another difference from Memcheck is that we use 8 shadow bits per
32 /// byte of application memory and use a direct shadow mapping. This
33 /// greatly simplifies the instrumentation code and avoids races on
34 /// shadow updates (Memcheck is single-threaded so races are not a
35 /// concern there. Memcheck uses 2 shadow bits per byte with a slow
36 /// path storage that uses 8 bits per byte).
37 ///
38 /// The default value of shadow is 0, which means "clean" (not poisoned).
39 ///
40 /// Every module initializer should call __msan_init to ensure that the
41 /// shadow memory is ready. On error, __msan_warning is called. Since
42 /// parameters and return values may be passed via registers, we have a
43 /// specialized thread-local shadow for return values
44 /// (__msan_retval_tls) and parameters (__msan_param_tls).
45 ///
46 ///                           Origin tracking.
47 ///
48 /// MemorySanitizer can track origins (allocation points) of all uninitialized
49 /// values. This behavior is controlled with a flag (msan-track-origins) and is
50 /// disabled by default.
51 ///
52 /// Origins are 4-byte values created and interpreted by the runtime library.
53 /// They are stored in a second shadow mapping, one 4-byte value for 4 bytes
54 /// of application memory. Propagation of origins is basically a bunch of
55 /// "select" instructions that pick the origin of a dirty argument, if an
56 /// instruction has one.
57 ///
58 /// Every 4 aligned, consecutive bytes of application memory have one origin
59 /// value associated with them. If these bytes contain uninitialized data
60 /// coming from 2 different allocations, the last store wins. Because of this,
61 /// MemorySanitizer reports can show unrelated origins, but this is unlikely in
62 /// practice.
63 ///
64 /// Origins are meaningless for fully initialized values, so MemorySanitizer
65 /// avoids storing origin to memory when a fully initialized value is stored.
66 /// This way it avoids needless overwritting origin of the 4-byte region on
67 /// a short (i.e. 1 byte) clean store, and it is also good for performance.
68 ///
69 ///                            Atomic handling.
70 ///
71 /// Ideally, every atomic store of application value should update the
72 /// corresponding shadow location in an atomic way. Unfortunately, atomic store
73 /// of two disjoint locations can not be done without severe slowdown.
74 ///
75 /// Therefore, we implement an approximation that may err on the safe side.
76 /// In this implementation, every atomically accessed location in the program
77 /// may only change from (partially) uninitialized to fully initialized, but
78 /// not the other way around. We load the shadow _after_ the application load,
79 /// and we store the shadow _before_ the app store. Also, we always store clean
80 /// shadow (if the application store is atomic). This way, if the store-load
81 /// pair constitutes a happens-before arc, shadow store and load are correctly
82 /// ordered such that the load will get either the value that was stored, or
83 /// some later value (which is always clean).
84 ///
85 /// This does not work very well with Compare-And-Swap (CAS) and
86 /// Read-Modify-Write (RMW) operations. To follow the above logic, CAS and RMW
87 /// must store the new shadow before the app operation, and load the shadow
88 /// after the app operation. Computers don't work this way. Current
89 /// implementation ignores the load aspect of CAS/RMW, always returning a clean
90 /// value. It implements the store part as a simple atomic store by storing a
91 /// clean shadow.
92 //
93 //===----------------------------------------------------------------------===//
94 
95 #include "llvm/ADT/APInt.h"
96 #include "llvm/ADT/ArrayRef.h"
97 #include "llvm/ADT/DepthFirstIterator.h"
98 #include "llvm/ADT/SmallString.h"
99 #include "llvm/ADT/SmallVector.h"
100 #include "llvm/ADT/StringExtras.h"
101 #include "llvm/ADT/StringRef.h"
102 #include "llvm/ADT/Triple.h"
103 #include "llvm/Analysis/TargetLibraryInfo.h"
104 #include "llvm/IR/Argument.h"
105 #include "llvm/IR/Attributes.h"
106 #include "llvm/IR/BasicBlock.h"
107 #include "llvm/IR/CallSite.h"
108 #include "llvm/IR/CallingConv.h"
109 #include "llvm/IR/Constant.h"
110 #include "llvm/IR/Constants.h"
111 #include "llvm/IR/DataLayout.h"
112 #include "llvm/IR/DerivedTypes.h"
113 #include "llvm/IR/Function.h"
114 #include "llvm/IR/GlobalValue.h"
115 #include "llvm/IR/GlobalVariable.h"
116 #include "llvm/IR/IRBuilder.h"
117 #include "llvm/IR/InlineAsm.h"
118 #include "llvm/IR/InstVisitor.h"
119 #include "llvm/IR/InstrTypes.h"
120 #include "llvm/IR/Instruction.h"
121 #include "llvm/IR/Instructions.h"
122 #include "llvm/IR/IntrinsicInst.h"
123 #include "llvm/IR/Intrinsics.h"
124 #include "llvm/IR/LLVMContext.h"
125 #include "llvm/IR/MDBuilder.h"
126 #include "llvm/IR/Module.h"
127 #include "llvm/IR/Type.h"
128 #include "llvm/IR/Value.h"
129 #include "llvm/IR/ValueMap.h"
130 #include "llvm/Pass.h"
131 #include "llvm/Support/AtomicOrdering.h"
132 #include "llvm/Support/Casting.h"
133 #include "llvm/Support/CommandLine.h"
134 #include "llvm/Support/Compiler.h"
135 #include "llvm/Support/Debug.h"
136 #include "llvm/Support/ErrorHandling.h"
137 #include "llvm/Support/MathExtras.h"
138 #include "llvm/Support/raw_ostream.h"
139 #include "llvm/Transforms/Instrumentation.h"
140 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
141 #include "llvm/Transforms/Utils/Local.h"
142 #include "llvm/Transforms/Utils/ModuleUtils.h"
143 #include <algorithm>
144 #include <cassert>
145 #include <cstddef>
146 #include <cstdint>
147 #include <memory>
148 #include <string>
149 #include <tuple>
150 
151 using namespace llvm;
152 
153 #define DEBUG_TYPE "msan"
154 
155 static const unsigned kOriginSize = 4;
156 static const unsigned kMinOriginAlignment = 4;
157 static const unsigned kShadowTLSAlignment = 8;
158 
159 // These constants must be kept in sync with the ones in msan.h.
160 static const unsigned kParamTLSSize = 800;
161 static const unsigned kRetvalTLSSize = 800;
162 
163 // Accesses sizes are powers of two: 1, 2, 4, 8.
164 static const size_t kNumberOfAccessSizes = 4;
165 
166 /// \brief Track origins of uninitialized values.
167 ///
168 /// Adds a section to MemorySanitizer report that points to the allocation
169 /// (stack or heap) the uninitialized bits came from originally.
170 static cl::opt<int> ClTrackOrigins("msan-track-origins",
171        cl::desc("Track origins (allocation sites) of poisoned memory"),
172        cl::Hidden, cl::init(0));
173 
174 static cl::opt<bool> ClKeepGoing("msan-keep-going",
175        cl::desc("keep going after reporting a UMR"),
176        cl::Hidden, cl::init(false));
177 
178 static cl::opt<bool> ClPoisonStack("msan-poison-stack",
179        cl::desc("poison uninitialized stack variables"),
180        cl::Hidden, cl::init(true));
181 
182 static cl::opt<bool> ClPoisonStackWithCall("msan-poison-stack-with-call",
183        cl::desc("poison uninitialized stack variables with a call"),
184        cl::Hidden, cl::init(false));
185 
186 static cl::opt<int> ClPoisonStackPattern("msan-poison-stack-pattern",
187        cl::desc("poison uninitialized stack variables with the given pattern"),
188        cl::Hidden, cl::init(0xff));
189 
190 static cl::opt<bool> ClPoisonUndef("msan-poison-undef",
191        cl::desc("poison undef temps"),
192        cl::Hidden, cl::init(true));
193 
194 static cl::opt<bool> ClHandleICmp("msan-handle-icmp",
195        cl::desc("propagate shadow through ICmpEQ and ICmpNE"),
196        cl::Hidden, cl::init(true));
197 
198 static cl::opt<bool> ClHandleICmpExact("msan-handle-icmp-exact",
199        cl::desc("exact handling of relational integer ICmp"),
200        cl::Hidden, cl::init(false));
201 
202 // This flag controls whether we check the shadow of the address
203 // operand of load or store. Such bugs are very rare, since load from
204 // a garbage address typically results in SEGV, but still happen
205 // (e.g. only lower bits of address are garbage, or the access happens
206 // early at program startup where malloc-ed memory is more likely to
207 // be zeroed. As of 2012-08-28 this flag adds 20% slowdown.
208 static cl::opt<bool> ClCheckAccessAddress("msan-check-access-address",
209        cl::desc("report accesses through a pointer which has poisoned shadow"),
210        cl::Hidden, cl::init(true));
211 
212 static cl::opt<bool> ClDumpStrictInstructions("msan-dump-strict-instructions",
213        cl::desc("print out instructions with default strict semantics"),
214        cl::Hidden, cl::init(false));
215 
216 static cl::opt<int> ClInstrumentationWithCallThreshold(
217     "msan-instrumentation-with-call-threshold",
218     cl::desc(
219         "If the function being instrumented requires more than "
220         "this number of checks and origin stores, use callbacks instead of "
221         "inline checks (-1 means never use callbacks)."),
222     cl::Hidden, cl::init(3500));
223 
224 // This is an experiment to enable handling of cases where shadow is a non-zero
225 // compile-time constant. For some unexplainable reason they were silently
226 // ignored in the instrumentation.
227 static cl::opt<bool> ClCheckConstantShadow("msan-check-constant-shadow",
228        cl::desc("Insert checks for constant shadow values"),
229        cl::Hidden, cl::init(false));
230 
231 // This is off by default because of a bug in gold:
232 // https://sourceware.org/bugzilla/show_bug.cgi?id=19002
233 static cl::opt<bool> ClWithComdat("msan-with-comdat",
234        cl::desc("Place MSan constructors in comdat sections"),
235        cl::Hidden, cl::init(false));
236 
237 static const char *const kMsanModuleCtorName = "msan.module_ctor";
238 static const char *const kMsanInitName = "__msan_init";
239 
240 namespace {
241 
242 // Memory map parameters used in application-to-shadow address calculation.
243 // Offset = (Addr & ~AndMask) ^ XorMask
244 // Shadow = ShadowBase + Offset
245 // Origin = OriginBase + Offset
246 struct MemoryMapParams {
247   uint64_t AndMask;
248   uint64_t XorMask;
249   uint64_t ShadowBase;
250   uint64_t OriginBase;
251 };
252 
253 struct PlatformMemoryMapParams {
254   const MemoryMapParams *bits32;
255   const MemoryMapParams *bits64;
256 };
257 
258 } // end anonymous namespace
259 
260 // i386 Linux
261 static const MemoryMapParams Linux_I386_MemoryMapParams = {
262   0x000080000000,  // AndMask
263   0,               // XorMask (not used)
264   0,               // ShadowBase (not used)
265   0x000040000000,  // OriginBase
266 };
267 
268 // x86_64 Linux
269 static const MemoryMapParams Linux_X86_64_MemoryMapParams = {
270 #ifdef MSAN_LINUX_X86_64_OLD_MAPPING
271   0x400000000000,  // AndMask
272   0,               // XorMask (not used)
273   0,               // ShadowBase (not used)
274   0x200000000000,  // OriginBase
275 #else
276   0,               // AndMask (not used)
277   0x500000000000,  // XorMask
278   0,               // ShadowBase (not used)
279   0x100000000000,  // OriginBase
280 #endif
281 };
282 
283 // mips64 Linux
284 static const MemoryMapParams Linux_MIPS64_MemoryMapParams = {
285   0,               // AndMask (not used)
286   0x008000000000,  // XorMask
287   0,               // ShadowBase (not used)
288   0x002000000000,  // OriginBase
289 };
290 
291 // ppc64 Linux
292 static const MemoryMapParams Linux_PowerPC64_MemoryMapParams = {
293   0xE00000000000,  // AndMask
294   0x100000000000,  // XorMask
295   0x080000000000,  // ShadowBase
296   0x1C0000000000,  // OriginBase
297 };
298 
299 // aarch64 Linux
300 static const MemoryMapParams Linux_AArch64_MemoryMapParams = {
301   0,               // AndMask (not used)
302   0x06000000000,   // XorMask
303   0,               // ShadowBase (not used)
304   0x01000000000,   // OriginBase
305 };
306 
307 // i386 FreeBSD
308 static const MemoryMapParams FreeBSD_I386_MemoryMapParams = {
309   0x000180000000,  // AndMask
310   0x000040000000,  // XorMask
311   0x000020000000,  // ShadowBase
312   0x000700000000,  // OriginBase
313 };
314 
315 // x86_64 FreeBSD
316 static const MemoryMapParams FreeBSD_X86_64_MemoryMapParams = {
317   0xc00000000000,  // AndMask
318   0x200000000000,  // XorMask
319   0x100000000000,  // ShadowBase
320   0x380000000000,  // OriginBase
321 };
322 
323 static const PlatformMemoryMapParams Linux_X86_MemoryMapParams = {
324   &Linux_I386_MemoryMapParams,
325   &Linux_X86_64_MemoryMapParams,
326 };
327 
328 static const PlatformMemoryMapParams Linux_MIPS_MemoryMapParams = {
329   nullptr,
330   &Linux_MIPS64_MemoryMapParams,
331 };
332 
333 static const PlatformMemoryMapParams Linux_PowerPC_MemoryMapParams = {
334   nullptr,
335   &Linux_PowerPC64_MemoryMapParams,
336 };
337 
338 static const PlatformMemoryMapParams Linux_ARM_MemoryMapParams = {
339   nullptr,
340   &Linux_AArch64_MemoryMapParams,
341 };
342 
343 static const PlatformMemoryMapParams FreeBSD_X86_MemoryMapParams = {
344   &FreeBSD_I386_MemoryMapParams,
345   &FreeBSD_X86_64_MemoryMapParams,
346 };
347 
348 namespace {
349 
350 /// \brief An instrumentation pass implementing detection of uninitialized
351 /// reads.
352 ///
353 /// MemorySanitizer: instrument the code in module to find
354 /// uninitialized reads.
355 class MemorySanitizer : public FunctionPass {
356 public:
357   // Pass identification, replacement for typeid.
358   static char ID;
359 
360   MemorySanitizer(int TrackOrigins = 0, bool Recover = false)
361       : FunctionPass(ID),
362         TrackOrigins(std::max(TrackOrigins, (int)ClTrackOrigins)),
363         Recover(Recover || ClKeepGoing) {}
364 
365   StringRef getPassName() const override { return "MemorySanitizer"; }
366 
367   void getAnalysisUsage(AnalysisUsage &AU) const override {
368     AU.addRequired<TargetLibraryInfoWrapperPass>();
369   }
370 
371   bool runOnFunction(Function &F) override;
372   bool doInitialization(Module &M) override;
373 
374 private:
375   friend struct MemorySanitizerVisitor;
376   friend struct VarArgAMD64Helper;
377   friend struct VarArgMIPS64Helper;
378   friend struct VarArgAArch64Helper;
379   friend struct VarArgPowerPC64Helper;
380 
381   void initializeCallbacks(Module &M);
382 
383   /// \brief Track origins (allocation points) of uninitialized values.
384   int TrackOrigins;
385   bool Recover;
386 
387   LLVMContext *C;
388   Type *IntptrTy;
389   Type *OriginTy;
390 
391   /// \brief Thread-local shadow storage for function parameters.
392   GlobalVariable *ParamTLS;
393 
394   /// \brief Thread-local origin storage for function parameters.
395   GlobalVariable *ParamOriginTLS;
396 
397   /// \brief Thread-local shadow storage for function return value.
398   GlobalVariable *RetvalTLS;
399 
400   /// \brief Thread-local origin storage for function return value.
401   GlobalVariable *RetvalOriginTLS;
402 
403   /// \brief Thread-local shadow storage for in-register va_arg function
404   /// parameters (x86_64-specific).
405   GlobalVariable *VAArgTLS;
406 
407   /// \brief Thread-local shadow storage for va_arg overflow area
408   /// (x86_64-specific).
409   GlobalVariable *VAArgOverflowSizeTLS;
410 
411   /// \brief Thread-local space used to pass origin value to the UMR reporting
412   /// function.
413   GlobalVariable *OriginTLS;
414 
415   /// \brief The run-time callback to print a warning.
416   Value *WarningFn = nullptr;
417 
418   // These arrays are indexed by log2(AccessSize).
419   Value *MaybeWarningFn[kNumberOfAccessSizes];
420   Value *MaybeStoreOriginFn[kNumberOfAccessSizes];
421 
422   /// \brief Run-time helper that generates a new origin value for a stack
423   /// allocation.
424   Value *MsanSetAllocaOrigin4Fn;
425 
426   /// \brief Run-time helper that poisons stack on function entry.
427   Value *MsanPoisonStackFn;
428 
429   /// \brief Run-time helper that records a store (or any event) of an
430   /// uninitialized value and returns an updated origin id encoding this info.
431   Value *MsanChainOriginFn;
432 
433   /// \brief MSan runtime replacements for memmove, memcpy and memset.
434   Value *MemmoveFn, *MemcpyFn, *MemsetFn;
435 
436   /// \brief Memory map parameters used in application-to-shadow calculation.
437   const MemoryMapParams *MapParams;
438 
439   MDNode *ColdCallWeights;
440 
441   /// \brief Branch weights for origin store.
442   MDNode *OriginStoreWeights;
443 
444   /// \brief An empty volatile inline asm that prevents callback merge.
445   InlineAsm *EmptyAsm;
446 
447   Function *MsanCtorFunction;
448 };
449 
450 } // end anonymous namespace
451 
452 char MemorySanitizer::ID = 0;
453 
454 INITIALIZE_PASS_BEGIN(
455     MemorySanitizer, "msan",
456     "MemorySanitizer: detects uninitialized reads.", false, false)
457 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
458 INITIALIZE_PASS_END(
459     MemorySanitizer, "msan",
460     "MemorySanitizer: detects uninitialized reads.", false, false)
461 
462 FunctionPass *llvm::createMemorySanitizerPass(int TrackOrigins, bool Recover) {
463   return new MemorySanitizer(TrackOrigins, Recover);
464 }
465 
466 /// \brief Create a non-const global initialized with the given string.
467 ///
468 /// Creates a writable global for Str so that we can pass it to the
469 /// run-time lib. Runtime uses first 4 bytes of the string to store the
470 /// frame ID, so the string needs to be mutable.
471 static GlobalVariable *createPrivateNonConstGlobalForString(Module &M,
472                                                             StringRef Str) {
473   Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
474   return new GlobalVariable(M, StrConst->getType(), /*isConstant=*/false,
475                             GlobalValue::PrivateLinkage, StrConst, "");
476 }
477 
478 /// \brief Insert extern declaration of runtime-provided functions and globals.
479 void MemorySanitizer::initializeCallbacks(Module &M) {
480   // Only do this once.
481   if (WarningFn)
482     return;
483 
484   IRBuilder<> IRB(*C);
485   // Create the callback.
486   // FIXME: this function should have "Cold" calling conv,
487   // which is not yet implemented.
488   StringRef WarningFnName = Recover ? "__msan_warning"
489                                     : "__msan_warning_noreturn";
490   WarningFn = M.getOrInsertFunction(WarningFnName, IRB.getVoidTy());
491 
492   for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
493        AccessSizeIndex++) {
494     unsigned AccessSize = 1 << AccessSizeIndex;
495     std::string FunctionName = "__msan_maybe_warning_" + itostr(AccessSize);
496     MaybeWarningFn[AccessSizeIndex] = M.getOrInsertFunction(
497         FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8),
498         IRB.getInt32Ty());
499 
500     FunctionName = "__msan_maybe_store_origin_" + itostr(AccessSize);
501     MaybeStoreOriginFn[AccessSizeIndex] = M.getOrInsertFunction(
502         FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8),
503         IRB.getInt8PtrTy(), IRB.getInt32Ty());
504   }
505 
506   MsanSetAllocaOrigin4Fn = M.getOrInsertFunction(
507     "__msan_set_alloca_origin4", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy,
508     IRB.getInt8PtrTy(), IntptrTy);
509   MsanPoisonStackFn =
510       M.getOrInsertFunction("__msan_poison_stack", IRB.getVoidTy(),
511                             IRB.getInt8PtrTy(), IntptrTy);
512   MsanChainOriginFn = M.getOrInsertFunction(
513     "__msan_chain_origin", IRB.getInt32Ty(), IRB.getInt32Ty());
514   MemmoveFn = M.getOrInsertFunction(
515     "__msan_memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
516     IRB.getInt8PtrTy(), IntptrTy);
517   MemcpyFn = M.getOrInsertFunction(
518     "__msan_memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
519     IntptrTy);
520   MemsetFn = M.getOrInsertFunction(
521     "__msan_memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(),
522     IntptrTy);
523 
524   // Create globals.
525   RetvalTLS = new GlobalVariable(
526     M, ArrayType::get(IRB.getInt64Ty(), kRetvalTLSSize / 8), false,
527     GlobalVariable::ExternalLinkage, nullptr, "__msan_retval_tls", nullptr,
528     GlobalVariable::InitialExecTLSModel);
529   RetvalOriginTLS = new GlobalVariable(
530     M, OriginTy, false, GlobalVariable::ExternalLinkage, nullptr,
531     "__msan_retval_origin_tls", nullptr, GlobalVariable::InitialExecTLSModel);
532 
533   ParamTLS = new GlobalVariable(
534     M, ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), false,
535     GlobalVariable::ExternalLinkage, nullptr, "__msan_param_tls", nullptr,
536     GlobalVariable::InitialExecTLSModel);
537   ParamOriginTLS = new GlobalVariable(
538     M, ArrayType::get(OriginTy, kParamTLSSize / 4), false,
539     GlobalVariable::ExternalLinkage, nullptr, "__msan_param_origin_tls",
540     nullptr, GlobalVariable::InitialExecTLSModel);
541 
542   VAArgTLS = new GlobalVariable(
543     M, ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), false,
544     GlobalVariable::ExternalLinkage, nullptr, "__msan_va_arg_tls", nullptr,
545     GlobalVariable::InitialExecTLSModel);
546   VAArgOverflowSizeTLS = new GlobalVariable(
547     M, IRB.getInt64Ty(), false, GlobalVariable::ExternalLinkage, nullptr,
548     "__msan_va_arg_overflow_size_tls", nullptr,
549     GlobalVariable::InitialExecTLSModel);
550   OriginTLS = new GlobalVariable(
551     M, IRB.getInt32Ty(), false, GlobalVariable::ExternalLinkage, nullptr,
552     "__msan_origin_tls", nullptr, GlobalVariable::InitialExecTLSModel);
553 
554   // We insert an empty inline asm after __msan_report* to avoid callback merge.
555   EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
556                             StringRef(""), StringRef(""),
557                             /*hasSideEffects=*/true);
558 }
559 
560 /// \brief Module-level initialization.
561 ///
562 /// inserts a call to __msan_init to the module's constructor list.
563 bool MemorySanitizer::doInitialization(Module &M) {
564   auto &DL = M.getDataLayout();
565 
566   Triple TargetTriple(M.getTargetTriple());
567   switch (TargetTriple.getOS()) {
568     case Triple::FreeBSD:
569       switch (TargetTriple.getArch()) {
570         case Triple::x86_64:
571           MapParams = FreeBSD_X86_MemoryMapParams.bits64;
572           break;
573         case Triple::x86:
574           MapParams = FreeBSD_X86_MemoryMapParams.bits32;
575           break;
576         default:
577           report_fatal_error("unsupported architecture");
578       }
579       break;
580     case Triple::Linux:
581       switch (TargetTriple.getArch()) {
582         case Triple::x86_64:
583           MapParams = Linux_X86_MemoryMapParams.bits64;
584           break;
585         case Triple::x86:
586           MapParams = Linux_X86_MemoryMapParams.bits32;
587           break;
588         case Triple::mips64:
589         case Triple::mips64el:
590           MapParams = Linux_MIPS_MemoryMapParams.bits64;
591           break;
592         case Triple::ppc64:
593         case Triple::ppc64le:
594           MapParams = Linux_PowerPC_MemoryMapParams.bits64;
595           break;
596         case Triple::aarch64:
597         case Triple::aarch64_be:
598           MapParams = Linux_ARM_MemoryMapParams.bits64;
599           break;
600         default:
601           report_fatal_error("unsupported architecture");
602       }
603       break;
604     default:
605       report_fatal_error("unsupported operating system");
606   }
607 
608   C = &(M.getContext());
609   IRBuilder<> IRB(*C);
610   IntptrTy = IRB.getIntPtrTy(DL);
611   OriginTy = IRB.getInt32Ty();
612 
613   ColdCallWeights = MDBuilder(*C).createBranchWeights(1, 1000);
614   OriginStoreWeights = MDBuilder(*C).createBranchWeights(1, 1000);
615 
616   std::tie(MsanCtorFunction, std::ignore) =
617       createSanitizerCtorAndInitFunctions(M, kMsanModuleCtorName, kMsanInitName,
618                                           /*InitArgTypes=*/{},
619                                           /*InitArgs=*/{});
620   if (ClWithComdat) {
621     Comdat *MsanCtorComdat = M.getOrInsertComdat(kMsanModuleCtorName);
622     MsanCtorFunction->setComdat(MsanCtorComdat);
623     appendToGlobalCtors(M, MsanCtorFunction, 0, MsanCtorFunction);
624   } else {
625     appendToGlobalCtors(M, MsanCtorFunction, 0);
626   }
627 
628 
629   if (TrackOrigins)
630     new GlobalVariable(M, IRB.getInt32Ty(), true, GlobalValue::WeakODRLinkage,
631                        IRB.getInt32(TrackOrigins), "__msan_track_origins");
632 
633   if (Recover)
634     new GlobalVariable(M, IRB.getInt32Ty(), true, GlobalValue::WeakODRLinkage,
635                        IRB.getInt32(Recover), "__msan_keep_going");
636 
637   return true;
638 }
639 
640 namespace {
641 
642 /// \brief A helper class that handles instrumentation of VarArg
643 /// functions on a particular platform.
644 ///
645 /// Implementations are expected to insert the instrumentation
646 /// necessary to propagate argument shadow through VarArg function
647 /// calls. Visit* methods are called during an InstVisitor pass over
648 /// the function, and should avoid creating new basic blocks. A new
649 /// instance of this class is created for each instrumented function.
650 struct VarArgHelper {
651   virtual ~VarArgHelper() = default;
652 
653   /// \brief Visit a CallSite.
654   virtual void visitCallSite(CallSite &CS, IRBuilder<> &IRB) = 0;
655 
656   /// \brief Visit a va_start call.
657   virtual void visitVAStartInst(VAStartInst &I) = 0;
658 
659   /// \brief Visit a va_copy call.
660   virtual void visitVACopyInst(VACopyInst &I) = 0;
661 
662   /// \brief Finalize function instrumentation.
663   ///
664   /// This method is called after visiting all interesting (see above)
665   /// instructions in a function.
666   virtual void finalizeInstrumentation() = 0;
667 };
668 
669 struct MemorySanitizerVisitor;
670 
671 } // end anonymous namespace
672 
673 static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
674                                         MemorySanitizerVisitor &Visitor);
675 
676 static unsigned TypeSizeToSizeIndex(unsigned TypeSize) {
677   if (TypeSize <= 8) return 0;
678   return Log2_32_Ceil((TypeSize + 7) / 8);
679 }
680 
681 namespace {
682 
683 /// This class does all the work for a given function. Store and Load
684 /// instructions store and load corresponding shadow and origin
685 /// values. Most instructions propagate shadow from arguments to their
686 /// return values. Certain instructions (most importantly, BranchInst)
687 /// test their argument shadow and print reports (with a runtime call) if it's
688 /// non-zero.
689 struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
690   Function &F;
691   MemorySanitizer &MS;
692   SmallVector<PHINode *, 16> ShadowPHINodes, OriginPHINodes;
693   ValueMap<Value*, Value*> ShadowMap, OriginMap;
694   std::unique_ptr<VarArgHelper> VAHelper;
695   const TargetLibraryInfo *TLI;
696 
697   // The following flags disable parts of MSan instrumentation based on
698   // blacklist contents and command-line options.
699   bool InsertChecks;
700   bool PropagateShadow;
701   bool PoisonStack;
702   bool PoisonUndef;
703   bool CheckReturnValue;
704 
705   struct ShadowOriginAndInsertPoint {
706     Value *Shadow;
707     Value *Origin;
708     Instruction *OrigIns;
709 
710     ShadowOriginAndInsertPoint(Value *S, Value *O, Instruction *I)
711       : Shadow(S), Origin(O), OrigIns(I) {}
712   };
713   SmallVector<ShadowOriginAndInsertPoint, 16> InstrumentationList;
714   SmallVector<StoreInst *, 16> StoreList;
715 
716   MemorySanitizerVisitor(Function &F, MemorySanitizer &MS)
717       : F(F), MS(MS), VAHelper(CreateVarArgHelper(F, MS, *this)) {
718     bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeMemory);
719     InsertChecks = SanitizeFunction;
720     PropagateShadow = SanitizeFunction;
721     PoisonStack = SanitizeFunction && ClPoisonStack;
722     PoisonUndef = SanitizeFunction && ClPoisonUndef;
723     // FIXME: Consider using SpecialCaseList to specify a list of functions that
724     // must always return fully initialized values. For now, we hardcode "main".
725     CheckReturnValue = SanitizeFunction && (F.getName() == "main");
726     TLI = &MS.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
727 
728     DEBUG(if (!InsertChecks)
729           dbgs() << "MemorySanitizer is not inserting checks into '"
730                  << F.getName() << "'\n");
731   }
732 
733   Value *updateOrigin(Value *V, IRBuilder<> &IRB) {
734     if (MS.TrackOrigins <= 1) return V;
735     return IRB.CreateCall(MS.MsanChainOriginFn, V);
736   }
737 
738   Value *originToIntptr(IRBuilder<> &IRB, Value *Origin) {
739     const DataLayout &DL = F.getParent()->getDataLayout();
740     unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
741     if (IntptrSize == kOriginSize) return Origin;
742     assert(IntptrSize == kOriginSize * 2);
743     Origin = IRB.CreateIntCast(Origin, MS.IntptrTy, /* isSigned */ false);
744     return IRB.CreateOr(Origin, IRB.CreateShl(Origin, kOriginSize * 8));
745   }
746 
747   /// \brief Fill memory range with the given origin value.
748   void paintOrigin(IRBuilder<> &IRB, Value *Origin, Value *OriginPtr,
749                    unsigned Size, unsigned Alignment) {
750     const DataLayout &DL = F.getParent()->getDataLayout();
751     unsigned IntptrAlignment = DL.getABITypeAlignment(MS.IntptrTy);
752     unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
753     assert(IntptrAlignment >= kMinOriginAlignment);
754     assert(IntptrSize >= kOriginSize);
755 
756     unsigned Ofs = 0;
757     unsigned CurrentAlignment = Alignment;
758     if (Alignment >= IntptrAlignment && IntptrSize > kOriginSize) {
759       Value *IntptrOrigin = originToIntptr(IRB, Origin);
760       Value *IntptrOriginPtr =
761           IRB.CreatePointerCast(OriginPtr, PointerType::get(MS.IntptrTy, 0));
762       for (unsigned i = 0; i < Size / IntptrSize; ++i) {
763         Value *Ptr = i ? IRB.CreateConstGEP1_32(MS.IntptrTy, IntptrOriginPtr, i)
764                        : IntptrOriginPtr;
765         IRB.CreateAlignedStore(IntptrOrigin, Ptr, CurrentAlignment);
766         Ofs += IntptrSize / kOriginSize;
767         CurrentAlignment = IntptrAlignment;
768       }
769     }
770 
771     for (unsigned i = Ofs; i < (Size + kOriginSize - 1) / kOriginSize; ++i) {
772       Value *GEP =
773           i ? IRB.CreateConstGEP1_32(nullptr, OriginPtr, i) : OriginPtr;
774       IRB.CreateAlignedStore(Origin, GEP, CurrentAlignment);
775       CurrentAlignment = kMinOriginAlignment;
776     }
777   }
778 
779   void storeOrigin(IRBuilder<> &IRB, Value *Addr, Value *Shadow, Value *Origin,
780                    unsigned Alignment, bool AsCall) {
781     const DataLayout &DL = F.getParent()->getDataLayout();
782     unsigned OriginAlignment = std::max(kMinOriginAlignment, Alignment);
783     unsigned StoreSize = DL.getTypeStoreSize(Shadow->getType());
784     if (Shadow->getType()->isAggregateType()) {
785       paintOrigin(IRB, updateOrigin(Origin, IRB),
786                   getOriginPtr(Addr, IRB, Alignment), StoreSize,
787                   OriginAlignment);
788     } else {
789       Value *ConvertedShadow = convertToShadowTyNoVec(Shadow, IRB);
790       Constant *ConstantShadow = dyn_cast_or_null<Constant>(ConvertedShadow);
791       if (ConstantShadow) {
792         if (ClCheckConstantShadow && !ConstantShadow->isZeroValue())
793           paintOrigin(IRB, updateOrigin(Origin, IRB),
794                       getOriginPtr(Addr, IRB, Alignment), StoreSize,
795                       OriginAlignment);
796         return;
797       }
798 
799       unsigned TypeSizeInBits =
800           DL.getTypeSizeInBits(ConvertedShadow->getType());
801       unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
802       if (AsCall && SizeIndex < kNumberOfAccessSizes) {
803         Value *Fn = MS.MaybeStoreOriginFn[SizeIndex];
804         Value *ConvertedShadow2 = IRB.CreateZExt(
805             ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
806         IRB.CreateCall(Fn, {ConvertedShadow2,
807                             IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
808                             Origin});
809       } else {
810         Value *Cmp = IRB.CreateICmpNE(
811             ConvertedShadow, getCleanShadow(ConvertedShadow), "_mscmp");
812         Instruction *CheckTerm = SplitBlockAndInsertIfThen(
813             Cmp, &*IRB.GetInsertPoint(), false, MS.OriginStoreWeights);
814         IRBuilder<> IRBNew(CheckTerm);
815         paintOrigin(IRBNew, updateOrigin(Origin, IRBNew),
816                     getOriginPtr(Addr, IRBNew, Alignment), StoreSize,
817                     OriginAlignment);
818       }
819     }
820   }
821 
822   void materializeStores(bool InstrumentWithCalls) {
823     for (StoreInst *SI : StoreList) {
824       IRBuilder<> IRB(SI);
825       Value *Val = SI->getValueOperand();
826       Value *Addr = SI->getPointerOperand();
827       Value *Shadow = SI->isAtomic() ? getCleanShadow(Val) : getShadow(Val);
828       Value *ShadowPtr = getShadowPtr(Addr, Shadow->getType(), IRB);
829 
830       StoreInst *NewSI =
831           IRB.CreateAlignedStore(Shadow, ShadowPtr, SI->getAlignment());
832       DEBUG(dbgs() << "  STORE: " << *NewSI << "\n");
833 
834       if (ClCheckAccessAddress)
835         insertShadowCheck(Addr, NewSI);
836 
837       if (SI->isAtomic())
838         SI->setOrdering(addReleaseOrdering(SI->getOrdering()));
839 
840       if (MS.TrackOrigins && !SI->isAtomic())
841         storeOrigin(IRB, Addr, Shadow, getOrigin(Val), SI->getAlignment(),
842                     InstrumentWithCalls);
843     }
844   }
845 
846   void materializeOneCheck(Instruction *OrigIns, Value *Shadow, Value *Origin,
847                            bool AsCall) {
848     IRBuilder<> IRB(OrigIns);
849     DEBUG(dbgs() << "  SHAD0 : " << *Shadow << "\n");
850     Value *ConvertedShadow = convertToShadowTyNoVec(Shadow, IRB);
851     DEBUG(dbgs() << "  SHAD1 : " << *ConvertedShadow << "\n");
852 
853     Constant *ConstantShadow = dyn_cast_or_null<Constant>(ConvertedShadow);
854     if (ConstantShadow) {
855       if (ClCheckConstantShadow && !ConstantShadow->isZeroValue()) {
856         if (MS.TrackOrigins) {
857           IRB.CreateStore(Origin ? (Value *)Origin : (Value *)IRB.getInt32(0),
858                           MS.OriginTLS);
859         }
860         IRB.CreateCall(MS.WarningFn, {});
861         IRB.CreateCall(MS.EmptyAsm, {});
862         // FIXME: Insert UnreachableInst if !MS.Recover?
863         // This may invalidate some of the following checks and needs to be done
864         // at the very end.
865       }
866       return;
867     }
868 
869     const DataLayout &DL = OrigIns->getModule()->getDataLayout();
870 
871     unsigned TypeSizeInBits = DL.getTypeSizeInBits(ConvertedShadow->getType());
872     unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
873     if (AsCall && SizeIndex < kNumberOfAccessSizes) {
874       Value *Fn = MS.MaybeWarningFn[SizeIndex];
875       Value *ConvertedShadow2 =
876           IRB.CreateZExt(ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
877       IRB.CreateCall(Fn, {ConvertedShadow2, MS.TrackOrigins && Origin
878                                                 ? Origin
879                                                 : (Value *)IRB.getInt32(0)});
880     } else {
881       Value *Cmp = IRB.CreateICmpNE(ConvertedShadow,
882                                     getCleanShadow(ConvertedShadow), "_mscmp");
883       Instruction *CheckTerm = SplitBlockAndInsertIfThen(
884           Cmp, OrigIns,
885           /* Unreachable */ !MS.Recover, MS.ColdCallWeights);
886 
887       IRB.SetInsertPoint(CheckTerm);
888       if (MS.TrackOrigins) {
889         IRB.CreateStore(Origin ? (Value *)Origin : (Value *)IRB.getInt32(0),
890                         MS.OriginTLS);
891       }
892       IRB.CreateCall(MS.WarningFn, {});
893       IRB.CreateCall(MS.EmptyAsm, {});
894       DEBUG(dbgs() << "  CHECK: " << *Cmp << "\n");
895     }
896   }
897 
898   void materializeChecks(bool InstrumentWithCalls) {
899     for (const auto &ShadowData : InstrumentationList) {
900       Instruction *OrigIns = ShadowData.OrigIns;
901       Value *Shadow = ShadowData.Shadow;
902       Value *Origin = ShadowData.Origin;
903       materializeOneCheck(OrigIns, Shadow, Origin, InstrumentWithCalls);
904     }
905     DEBUG(dbgs() << "DONE:\n" << F);
906   }
907 
908   /// \brief Add MemorySanitizer instrumentation to a function.
909   bool runOnFunction() {
910     MS.initializeCallbacks(*F.getParent());
911 
912     // In the presence of unreachable blocks, we may see Phi nodes with
913     // incoming nodes from such blocks. Since InstVisitor skips unreachable
914     // blocks, such nodes will not have any shadow value associated with them.
915     // It's easier to remove unreachable blocks than deal with missing shadow.
916     removeUnreachableBlocks(F);
917 
918     // Iterate all BBs in depth-first order and create shadow instructions
919     // for all instructions (where applicable).
920     // For PHI nodes we create dummy shadow PHIs which will be finalized later.
921     for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
922       visit(*BB);
923 
924     // Finalize PHI nodes.
925     for (PHINode *PN : ShadowPHINodes) {
926       PHINode *PNS = cast<PHINode>(getShadow(PN));
927       PHINode *PNO = MS.TrackOrigins ? cast<PHINode>(getOrigin(PN)) : nullptr;
928       size_t NumValues = PN->getNumIncomingValues();
929       for (size_t v = 0; v < NumValues; v++) {
930         PNS->addIncoming(getShadow(PN, v), PN->getIncomingBlock(v));
931         if (PNO) PNO->addIncoming(getOrigin(PN, v), PN->getIncomingBlock(v));
932       }
933     }
934 
935     VAHelper->finalizeInstrumentation();
936 
937     bool InstrumentWithCalls = ClInstrumentationWithCallThreshold >= 0 &&
938                                InstrumentationList.size() + StoreList.size() >
939                                    (unsigned)ClInstrumentationWithCallThreshold;
940 
941     // Delayed instrumentation of StoreInst.
942     // This may add new checks to be inserted later.
943     materializeStores(InstrumentWithCalls);
944 
945     // Insert shadow value checks.
946     materializeChecks(InstrumentWithCalls);
947 
948     return true;
949   }
950 
951   /// \brief Compute the shadow type that corresponds to a given Value.
952   Type *getShadowTy(Value *V) {
953     return getShadowTy(V->getType());
954   }
955 
956   /// \brief Compute the shadow type that corresponds to a given Type.
957   Type *getShadowTy(Type *OrigTy) {
958     if (!OrigTy->isSized()) {
959       return nullptr;
960     }
961     // For integer type, shadow is the same as the original type.
962     // This may return weird-sized types like i1.
963     if (IntegerType *IT = dyn_cast<IntegerType>(OrigTy))
964       return IT;
965     const DataLayout &DL = F.getParent()->getDataLayout();
966     if (VectorType *VT = dyn_cast<VectorType>(OrigTy)) {
967       uint32_t EltSize = DL.getTypeSizeInBits(VT->getElementType());
968       return VectorType::get(IntegerType::get(*MS.C, EltSize),
969                              VT->getNumElements());
970     }
971     if (ArrayType *AT = dyn_cast<ArrayType>(OrigTy)) {
972       return ArrayType::get(getShadowTy(AT->getElementType()),
973                             AT->getNumElements());
974     }
975     if (StructType *ST = dyn_cast<StructType>(OrigTy)) {
976       SmallVector<Type*, 4> Elements;
977       for (unsigned i = 0, n = ST->getNumElements(); i < n; i++)
978         Elements.push_back(getShadowTy(ST->getElementType(i)));
979       StructType *Res = StructType::get(*MS.C, Elements, ST->isPacked());
980       DEBUG(dbgs() << "getShadowTy: " << *ST << " ===> " << *Res << "\n");
981       return Res;
982     }
983     uint32_t TypeSize = DL.getTypeSizeInBits(OrigTy);
984     return IntegerType::get(*MS.C, TypeSize);
985   }
986 
987   /// \brief Flatten a vector type.
988   Type *getShadowTyNoVec(Type *ty) {
989     if (VectorType *vt = dyn_cast<VectorType>(ty))
990       return IntegerType::get(*MS.C, vt->getBitWidth());
991     return ty;
992   }
993 
994   /// \brief Convert a shadow value to it's flattened variant.
995   Value *convertToShadowTyNoVec(Value *V, IRBuilder<> &IRB) {
996     Type *Ty = V->getType();
997     Type *NoVecTy = getShadowTyNoVec(Ty);
998     if (Ty == NoVecTy) return V;
999     return IRB.CreateBitCast(V, NoVecTy);
1000   }
1001 
1002   /// \brief Compute the integer shadow offset that corresponds to a given
1003   /// application address.
1004   ///
1005   /// Offset = (Addr & ~AndMask) ^ XorMask
1006   Value *getShadowPtrOffset(Value *Addr, IRBuilder<> &IRB) {
1007     Value *OffsetLong = IRB.CreatePointerCast(Addr, MS.IntptrTy);
1008 
1009     uint64_t AndMask = MS.MapParams->AndMask;
1010     if (AndMask)
1011       OffsetLong =
1012           IRB.CreateAnd(OffsetLong, ConstantInt::get(MS.IntptrTy, ~AndMask));
1013 
1014     uint64_t XorMask = MS.MapParams->XorMask;
1015     if (XorMask)
1016       OffsetLong =
1017           IRB.CreateXor(OffsetLong, ConstantInt::get(MS.IntptrTy, XorMask));
1018     return OffsetLong;
1019   }
1020 
1021   /// \brief Compute the shadow address that corresponds to a given application
1022   /// address.
1023   ///
1024   /// Shadow = ShadowBase + Offset
1025   Value *getShadowPtr(Value *Addr, Type *ShadowTy,
1026                       IRBuilder<> &IRB) {
1027     Value *ShadowLong = getShadowPtrOffset(Addr, IRB);
1028     uint64_t ShadowBase = MS.MapParams->ShadowBase;
1029     if (ShadowBase != 0)
1030       ShadowLong =
1031         IRB.CreateAdd(ShadowLong,
1032                       ConstantInt::get(MS.IntptrTy, ShadowBase));
1033     return IRB.CreateIntToPtr(ShadowLong, PointerType::get(ShadowTy, 0));
1034   }
1035 
1036   /// \brief Compute the origin address that corresponds to a given application
1037   /// address.
1038   ///
1039   /// OriginAddr = (OriginBase + Offset) & ~3ULL
1040   Value *getOriginPtr(Value *Addr, IRBuilder<> &IRB, unsigned Alignment) {
1041     Value *OriginLong = getShadowPtrOffset(Addr, IRB);
1042     uint64_t OriginBase = MS.MapParams->OriginBase;
1043     if (OriginBase != 0)
1044       OriginLong =
1045         IRB.CreateAdd(OriginLong,
1046                       ConstantInt::get(MS.IntptrTy, OriginBase));
1047     if (Alignment < kMinOriginAlignment) {
1048       uint64_t Mask = kMinOriginAlignment - 1;
1049       OriginLong = IRB.CreateAnd(OriginLong,
1050                                  ConstantInt::get(MS.IntptrTy, ~Mask));
1051     }
1052     return IRB.CreateIntToPtr(OriginLong,
1053                               PointerType::get(IRB.getInt32Ty(), 0));
1054   }
1055 
1056   /// \brief Compute the shadow address for a given function argument.
1057   ///
1058   /// Shadow = ParamTLS+ArgOffset.
1059   Value *getShadowPtrForArgument(Value *A, IRBuilder<> &IRB,
1060                                  int ArgOffset) {
1061     Value *Base = IRB.CreatePointerCast(MS.ParamTLS, MS.IntptrTy);
1062     Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
1063     return IRB.CreateIntToPtr(Base, PointerType::get(getShadowTy(A), 0),
1064                               "_msarg");
1065   }
1066 
1067   /// \brief Compute the origin address for a given function argument.
1068   Value *getOriginPtrForArgument(Value *A, IRBuilder<> &IRB,
1069                                  int ArgOffset) {
1070     if (!MS.TrackOrigins) return nullptr;
1071     Value *Base = IRB.CreatePointerCast(MS.ParamOriginTLS, MS.IntptrTy);
1072     Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
1073     return IRB.CreateIntToPtr(Base, PointerType::get(MS.OriginTy, 0),
1074                               "_msarg_o");
1075   }
1076 
1077   /// \brief Compute the shadow address for a retval.
1078   Value *getShadowPtrForRetval(Value *A, IRBuilder<> &IRB) {
1079     return IRB.CreatePointerCast(MS.RetvalTLS,
1080                                  PointerType::get(getShadowTy(A), 0),
1081                                  "_msret");
1082   }
1083 
1084   /// \brief Compute the origin address for a retval.
1085   Value *getOriginPtrForRetval(IRBuilder<> &IRB) {
1086     // We keep a single origin for the entire retval. Might be too optimistic.
1087     return MS.RetvalOriginTLS;
1088   }
1089 
1090   /// \brief Set SV to be the shadow value for V.
1091   void setShadow(Value *V, Value *SV) {
1092     assert(!ShadowMap.count(V) && "Values may only have one shadow");
1093     ShadowMap[V] = PropagateShadow ? SV : getCleanShadow(V);
1094   }
1095 
1096   /// \brief Set Origin to be the origin value for V.
1097   void setOrigin(Value *V, Value *Origin) {
1098     if (!MS.TrackOrigins) return;
1099     assert(!OriginMap.count(V) && "Values may only have one origin");
1100     DEBUG(dbgs() << "ORIGIN: " << *V << "  ==> " << *Origin << "\n");
1101     OriginMap[V] = Origin;
1102   }
1103 
1104   Constant *getCleanShadow(Type *OrigTy) {
1105     Type *ShadowTy = getShadowTy(OrigTy);
1106     if (!ShadowTy)
1107       return nullptr;
1108     return Constant::getNullValue(ShadowTy);
1109   }
1110 
1111   /// \brief Create a clean shadow value for a given value.
1112   ///
1113   /// Clean shadow (all zeroes) means all bits of the value are defined
1114   /// (initialized).
1115   Constant *getCleanShadow(Value *V) {
1116     return getCleanShadow(V->getType());
1117   }
1118 
1119   /// \brief Create a dirty shadow of a given shadow type.
1120   Constant *getPoisonedShadow(Type *ShadowTy) {
1121     assert(ShadowTy);
1122     if (isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy))
1123       return Constant::getAllOnesValue(ShadowTy);
1124     if (ArrayType *AT = dyn_cast<ArrayType>(ShadowTy)) {
1125       SmallVector<Constant *, 4> Vals(AT->getNumElements(),
1126                                       getPoisonedShadow(AT->getElementType()));
1127       return ConstantArray::get(AT, Vals);
1128     }
1129     if (StructType *ST = dyn_cast<StructType>(ShadowTy)) {
1130       SmallVector<Constant *, 4> Vals;
1131       for (unsigned i = 0, n = ST->getNumElements(); i < n; i++)
1132         Vals.push_back(getPoisonedShadow(ST->getElementType(i)));
1133       return ConstantStruct::get(ST, Vals);
1134     }
1135     llvm_unreachable("Unexpected shadow type");
1136   }
1137 
1138   /// \brief Create a dirty shadow for a given value.
1139   Constant *getPoisonedShadow(Value *V) {
1140     Type *ShadowTy = getShadowTy(V);
1141     if (!ShadowTy)
1142       return nullptr;
1143     return getPoisonedShadow(ShadowTy);
1144   }
1145 
1146   /// \brief Create a clean (zero) origin.
1147   Value *getCleanOrigin() {
1148     return Constant::getNullValue(MS.OriginTy);
1149   }
1150 
1151   /// \brief Get the shadow value for a given Value.
1152   ///
1153   /// This function either returns the value set earlier with setShadow,
1154   /// or extracts if from ParamTLS (for function arguments).
1155   Value *getShadow(Value *V) {
1156     if (!PropagateShadow) return getCleanShadow(V);
1157     if (Instruction *I = dyn_cast<Instruction>(V)) {
1158       if (I->getMetadata("nosanitize"))
1159         return getCleanShadow(V);
1160       // For instructions the shadow is already stored in the map.
1161       Value *Shadow = ShadowMap[V];
1162       if (!Shadow) {
1163         DEBUG(dbgs() << "No shadow: " << *V << "\n" << *(I->getParent()));
1164         (void)I;
1165         assert(Shadow && "No shadow for a value");
1166       }
1167       return Shadow;
1168     }
1169     if (UndefValue *U = dyn_cast<UndefValue>(V)) {
1170       Value *AllOnes = PoisonUndef ? getPoisonedShadow(V) : getCleanShadow(V);
1171       DEBUG(dbgs() << "Undef: " << *U << " ==> " << *AllOnes << "\n");
1172       (void)U;
1173       return AllOnes;
1174     }
1175     if (Argument *A = dyn_cast<Argument>(V)) {
1176       // For arguments we compute the shadow on demand and store it in the map.
1177       Value **ShadowPtr = &ShadowMap[V];
1178       if (*ShadowPtr)
1179         return *ShadowPtr;
1180       Function *F = A->getParent();
1181       IRBuilder<> EntryIRB(F->getEntryBlock().getFirstNonPHI());
1182       unsigned ArgOffset = 0;
1183       const DataLayout &DL = F->getParent()->getDataLayout();
1184       for (auto &FArg : F->args()) {
1185         if (!FArg.getType()->isSized()) {
1186           DEBUG(dbgs() << "Arg is not sized\n");
1187           continue;
1188         }
1189         unsigned Size =
1190             FArg.hasByValAttr()
1191                 ? DL.getTypeAllocSize(FArg.getType()->getPointerElementType())
1192                 : DL.getTypeAllocSize(FArg.getType());
1193         if (A == &FArg) {
1194           bool Overflow = ArgOffset + Size > kParamTLSSize;
1195           Value *Base = getShadowPtrForArgument(&FArg, EntryIRB, ArgOffset);
1196           if (FArg.hasByValAttr()) {
1197             // ByVal pointer itself has clean shadow. We copy the actual
1198             // argument shadow to the underlying memory.
1199             // Figure out maximal valid memcpy alignment.
1200             unsigned ArgAlign = FArg.getParamAlignment();
1201             if (ArgAlign == 0) {
1202               Type *EltType = A->getType()->getPointerElementType();
1203               ArgAlign = DL.getABITypeAlignment(EltType);
1204             }
1205             if (Overflow) {
1206               // ParamTLS overflow.
1207               EntryIRB.CreateMemSet(
1208                   getShadowPtr(V, EntryIRB.getInt8Ty(), EntryIRB),
1209                   Constant::getNullValue(EntryIRB.getInt8Ty()), Size, ArgAlign);
1210             } else {
1211               unsigned CopyAlign = std::min(ArgAlign, kShadowTLSAlignment);
1212               Value *Cpy = EntryIRB.CreateMemCpy(
1213                   getShadowPtr(V, EntryIRB.getInt8Ty(), EntryIRB), Base, Size,
1214                   CopyAlign);
1215               DEBUG(dbgs() << "  ByValCpy: " << *Cpy << "\n");
1216               (void)Cpy;
1217             }
1218             *ShadowPtr = getCleanShadow(V);
1219           } else {
1220             if (Overflow) {
1221               // ParamTLS overflow.
1222               *ShadowPtr = getCleanShadow(V);
1223             } else {
1224               *ShadowPtr =
1225                   EntryIRB.CreateAlignedLoad(Base, kShadowTLSAlignment);
1226             }
1227           }
1228           DEBUG(dbgs() << "  ARG:    "  << FArg << " ==> " <<
1229                 **ShadowPtr << "\n");
1230           if (MS.TrackOrigins && !Overflow) {
1231             Value *OriginPtr =
1232                 getOriginPtrForArgument(&FArg, EntryIRB, ArgOffset);
1233             setOrigin(A, EntryIRB.CreateLoad(OriginPtr));
1234           } else {
1235             setOrigin(A, getCleanOrigin());
1236           }
1237         }
1238         ArgOffset += alignTo(Size, kShadowTLSAlignment);
1239       }
1240       assert(*ShadowPtr && "Could not find shadow for an argument");
1241       return *ShadowPtr;
1242     }
1243     // For everything else the shadow is zero.
1244     return getCleanShadow(V);
1245   }
1246 
1247   /// \brief Get the shadow for i-th argument of the instruction I.
1248   Value *getShadow(Instruction *I, int i) {
1249     return getShadow(I->getOperand(i));
1250   }
1251 
1252   /// \brief Get the origin for a value.
1253   Value *getOrigin(Value *V) {
1254     if (!MS.TrackOrigins) return nullptr;
1255     if (!PropagateShadow) return getCleanOrigin();
1256     if (isa<Constant>(V)) return getCleanOrigin();
1257     assert((isa<Instruction>(V) || isa<Argument>(V)) &&
1258            "Unexpected value type in getOrigin()");
1259     if (Instruction *I = dyn_cast<Instruction>(V)) {
1260       if (I->getMetadata("nosanitize"))
1261         return getCleanOrigin();
1262     }
1263     Value *Origin = OriginMap[V];
1264     assert(Origin && "Missing origin");
1265     return Origin;
1266   }
1267 
1268   /// \brief Get the origin for i-th argument of the instruction I.
1269   Value *getOrigin(Instruction *I, int i) {
1270     return getOrigin(I->getOperand(i));
1271   }
1272 
1273   /// \brief Remember the place where a shadow check should be inserted.
1274   ///
1275   /// This location will be later instrumented with a check that will print a
1276   /// UMR warning in runtime if the shadow value is not 0.
1277   void insertShadowCheck(Value *Shadow, Value *Origin, Instruction *OrigIns) {
1278     assert(Shadow);
1279     if (!InsertChecks) return;
1280 #ifndef NDEBUG
1281     Type *ShadowTy = Shadow->getType();
1282     assert((isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy)) &&
1283            "Can only insert checks for integer and vector shadow types");
1284 #endif
1285     InstrumentationList.push_back(
1286         ShadowOriginAndInsertPoint(Shadow, Origin, OrigIns));
1287   }
1288 
1289   /// \brief Remember the place where a shadow check should be inserted.
1290   ///
1291   /// This location will be later instrumented with a check that will print a
1292   /// UMR warning in runtime if the value is not fully defined.
1293   void insertShadowCheck(Value *Val, Instruction *OrigIns) {
1294     assert(Val);
1295     Value *Shadow, *Origin;
1296     if (ClCheckConstantShadow) {
1297       Shadow = getShadow(Val);
1298       if (!Shadow) return;
1299       Origin = getOrigin(Val);
1300     } else {
1301       Shadow = dyn_cast_or_null<Instruction>(getShadow(Val));
1302       if (!Shadow) return;
1303       Origin = dyn_cast_or_null<Instruction>(getOrigin(Val));
1304     }
1305     insertShadowCheck(Shadow, Origin, OrigIns);
1306   }
1307 
1308   AtomicOrdering addReleaseOrdering(AtomicOrdering a) {
1309     switch (a) {
1310       case AtomicOrdering::NotAtomic:
1311         return AtomicOrdering::NotAtomic;
1312       case AtomicOrdering::Unordered:
1313       case AtomicOrdering::Monotonic:
1314       case AtomicOrdering::Release:
1315         return AtomicOrdering::Release;
1316       case AtomicOrdering::Acquire:
1317       case AtomicOrdering::AcquireRelease:
1318         return AtomicOrdering::AcquireRelease;
1319       case AtomicOrdering::SequentiallyConsistent:
1320         return AtomicOrdering::SequentiallyConsistent;
1321     }
1322     llvm_unreachable("Unknown ordering");
1323   }
1324 
1325   AtomicOrdering addAcquireOrdering(AtomicOrdering a) {
1326     switch (a) {
1327       case AtomicOrdering::NotAtomic:
1328         return AtomicOrdering::NotAtomic;
1329       case AtomicOrdering::Unordered:
1330       case AtomicOrdering::Monotonic:
1331       case AtomicOrdering::Acquire:
1332         return AtomicOrdering::Acquire;
1333       case AtomicOrdering::Release:
1334       case AtomicOrdering::AcquireRelease:
1335         return AtomicOrdering::AcquireRelease;
1336       case AtomicOrdering::SequentiallyConsistent:
1337         return AtomicOrdering::SequentiallyConsistent;
1338     }
1339     llvm_unreachable("Unknown ordering");
1340   }
1341 
1342   // ------------------- Visitors.
1343   using InstVisitor<MemorySanitizerVisitor>::visit;
1344   void visit(Instruction &I) {
1345     if (!I.getMetadata("nosanitize"))
1346       InstVisitor<MemorySanitizerVisitor>::visit(I);
1347   }
1348 
1349   /// \brief Instrument LoadInst
1350   ///
1351   /// Loads the corresponding shadow and (optionally) origin.
1352   /// Optionally, checks that the load address is fully defined.
1353   void visitLoadInst(LoadInst &I) {
1354     assert(I.getType()->isSized() && "Load type must have size");
1355     assert(!I.getMetadata("nosanitize"));
1356     IRBuilder<> IRB(I.getNextNode());
1357     Type *ShadowTy = getShadowTy(&I);
1358     Value *Addr = I.getPointerOperand();
1359     if (PropagateShadow) {
1360       Value *ShadowPtr = getShadowPtr(Addr, ShadowTy, IRB);
1361       setShadow(&I,
1362                 IRB.CreateAlignedLoad(ShadowPtr, I.getAlignment(), "_msld"));
1363     } else {
1364       setShadow(&I, getCleanShadow(&I));
1365     }
1366 
1367     if (ClCheckAccessAddress)
1368       insertShadowCheck(I.getPointerOperand(), &I);
1369 
1370     if (I.isAtomic())
1371       I.setOrdering(addAcquireOrdering(I.getOrdering()));
1372 
1373     if (MS.TrackOrigins) {
1374       if (PropagateShadow) {
1375         unsigned Alignment = I.getAlignment();
1376         unsigned OriginAlignment = std::max(kMinOriginAlignment, Alignment);
1377         setOrigin(&I, IRB.CreateAlignedLoad(getOriginPtr(Addr, IRB, Alignment),
1378                                             OriginAlignment));
1379       } else {
1380         setOrigin(&I, getCleanOrigin());
1381       }
1382     }
1383   }
1384 
1385   /// \brief Instrument StoreInst
1386   ///
1387   /// Stores the corresponding shadow and (optionally) origin.
1388   /// Optionally, checks that the store address is fully defined.
1389   void visitStoreInst(StoreInst &I) {
1390     StoreList.push_back(&I);
1391   }
1392 
1393   void handleCASOrRMW(Instruction &I) {
1394     assert(isa<AtomicRMWInst>(I) || isa<AtomicCmpXchgInst>(I));
1395 
1396     IRBuilder<> IRB(&I);
1397     Value *Addr = I.getOperand(0);
1398     Value *ShadowPtr = getShadowPtr(Addr, I.getType(), IRB);
1399 
1400     if (ClCheckAccessAddress)
1401       insertShadowCheck(Addr, &I);
1402 
1403     // Only test the conditional argument of cmpxchg instruction.
1404     // The other argument can potentially be uninitialized, but we can not
1405     // detect this situation reliably without possible false positives.
1406     if (isa<AtomicCmpXchgInst>(I))
1407       insertShadowCheck(I.getOperand(1), &I);
1408 
1409     IRB.CreateStore(getCleanShadow(&I), ShadowPtr);
1410 
1411     setShadow(&I, getCleanShadow(&I));
1412     setOrigin(&I, getCleanOrigin());
1413   }
1414 
1415   void visitAtomicRMWInst(AtomicRMWInst &I) {
1416     handleCASOrRMW(I);
1417     I.setOrdering(addReleaseOrdering(I.getOrdering()));
1418   }
1419 
1420   void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
1421     handleCASOrRMW(I);
1422     I.setSuccessOrdering(addReleaseOrdering(I.getSuccessOrdering()));
1423   }
1424 
1425   // Vector manipulation.
1426   void visitExtractElementInst(ExtractElementInst &I) {
1427     insertShadowCheck(I.getOperand(1), &I);
1428     IRBuilder<> IRB(&I);
1429     setShadow(&I, IRB.CreateExtractElement(getShadow(&I, 0), I.getOperand(1),
1430               "_msprop"));
1431     setOrigin(&I, getOrigin(&I, 0));
1432   }
1433 
1434   void visitInsertElementInst(InsertElementInst &I) {
1435     insertShadowCheck(I.getOperand(2), &I);
1436     IRBuilder<> IRB(&I);
1437     setShadow(&I, IRB.CreateInsertElement(getShadow(&I, 0), getShadow(&I, 1),
1438               I.getOperand(2), "_msprop"));
1439     setOriginForNaryOp(I);
1440   }
1441 
1442   void visitShuffleVectorInst(ShuffleVectorInst &I) {
1443     insertShadowCheck(I.getOperand(2), &I);
1444     IRBuilder<> IRB(&I);
1445     setShadow(&I, IRB.CreateShuffleVector(getShadow(&I, 0), getShadow(&I, 1),
1446               I.getOperand(2), "_msprop"));
1447     setOriginForNaryOp(I);
1448   }
1449 
1450   // Casts.
1451   void visitSExtInst(SExtInst &I) {
1452     IRBuilder<> IRB(&I);
1453     setShadow(&I, IRB.CreateSExt(getShadow(&I, 0), I.getType(), "_msprop"));
1454     setOrigin(&I, getOrigin(&I, 0));
1455   }
1456 
1457   void visitZExtInst(ZExtInst &I) {
1458     IRBuilder<> IRB(&I);
1459     setShadow(&I, IRB.CreateZExt(getShadow(&I, 0), I.getType(), "_msprop"));
1460     setOrigin(&I, getOrigin(&I, 0));
1461   }
1462 
1463   void visitTruncInst(TruncInst &I) {
1464     IRBuilder<> IRB(&I);
1465     setShadow(&I, IRB.CreateTrunc(getShadow(&I, 0), I.getType(), "_msprop"));
1466     setOrigin(&I, getOrigin(&I, 0));
1467   }
1468 
1469   void visitBitCastInst(BitCastInst &I) {
1470     // Special case: if this is the bitcast (there is exactly 1 allowed) between
1471     // a musttail call and a ret, don't instrument. New instructions are not
1472     // allowed after a musttail call.
1473     if (auto *CI = dyn_cast<CallInst>(I.getOperand(0)))
1474       if (CI->isMustTailCall())
1475         return;
1476     IRBuilder<> IRB(&I);
1477     setShadow(&I, IRB.CreateBitCast(getShadow(&I, 0), getShadowTy(&I)));
1478     setOrigin(&I, getOrigin(&I, 0));
1479   }
1480 
1481   void visitPtrToIntInst(PtrToIntInst &I) {
1482     IRBuilder<> IRB(&I);
1483     setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false,
1484              "_msprop_ptrtoint"));
1485     setOrigin(&I, getOrigin(&I, 0));
1486   }
1487 
1488   void visitIntToPtrInst(IntToPtrInst &I) {
1489     IRBuilder<> IRB(&I);
1490     setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false,
1491              "_msprop_inttoptr"));
1492     setOrigin(&I, getOrigin(&I, 0));
1493   }
1494 
1495   void visitFPToSIInst(CastInst& I) { handleShadowOr(I); }
1496   void visitFPToUIInst(CastInst& I) { handleShadowOr(I); }
1497   void visitSIToFPInst(CastInst& I) { handleShadowOr(I); }
1498   void visitUIToFPInst(CastInst& I) { handleShadowOr(I); }
1499   void visitFPExtInst(CastInst& I) { handleShadowOr(I); }
1500   void visitFPTruncInst(CastInst& I) { handleShadowOr(I); }
1501 
1502   /// \brief Propagate shadow for bitwise AND.
1503   ///
1504   /// This code is exact, i.e. if, for example, a bit in the left argument
1505   /// is defined and 0, then neither the value not definedness of the
1506   /// corresponding bit in B don't affect the resulting shadow.
1507   void visitAnd(BinaryOperator &I) {
1508     IRBuilder<> IRB(&I);
1509     //  "And" of 0 and a poisoned value results in unpoisoned value.
1510     //  1&1 => 1;     0&1 => 0;     p&1 => p;
1511     //  1&0 => 0;     0&0 => 0;     p&0 => 0;
1512     //  1&p => p;     0&p => 0;     p&p => p;
1513     //  S = (S1 & S2) | (V1 & S2) | (S1 & V2)
1514     Value *S1 = getShadow(&I, 0);
1515     Value *S2 = getShadow(&I, 1);
1516     Value *V1 = I.getOperand(0);
1517     Value *V2 = I.getOperand(1);
1518     if (V1->getType() != S1->getType()) {
1519       V1 = IRB.CreateIntCast(V1, S1->getType(), false);
1520       V2 = IRB.CreateIntCast(V2, S2->getType(), false);
1521     }
1522     Value *S1S2 = IRB.CreateAnd(S1, S2);
1523     Value *V1S2 = IRB.CreateAnd(V1, S2);
1524     Value *S1V2 = IRB.CreateAnd(S1, V2);
1525     setShadow(&I, IRB.CreateOr(S1S2, IRB.CreateOr(V1S2, S1V2)));
1526     setOriginForNaryOp(I);
1527   }
1528 
1529   void visitOr(BinaryOperator &I) {
1530     IRBuilder<> IRB(&I);
1531     //  "Or" of 1 and a poisoned value results in unpoisoned value.
1532     //  1|1 => 1;     0|1 => 1;     p|1 => 1;
1533     //  1|0 => 1;     0|0 => 0;     p|0 => p;
1534     //  1|p => 1;     0|p => p;     p|p => p;
1535     //  S = (S1 & S2) | (~V1 & S2) | (S1 & ~V2)
1536     Value *S1 = getShadow(&I, 0);
1537     Value *S2 = getShadow(&I, 1);
1538     Value *V1 = IRB.CreateNot(I.getOperand(0));
1539     Value *V2 = IRB.CreateNot(I.getOperand(1));
1540     if (V1->getType() != S1->getType()) {
1541       V1 = IRB.CreateIntCast(V1, S1->getType(), false);
1542       V2 = IRB.CreateIntCast(V2, S2->getType(), false);
1543     }
1544     Value *S1S2 = IRB.CreateAnd(S1, S2);
1545     Value *V1S2 = IRB.CreateAnd(V1, S2);
1546     Value *S1V2 = IRB.CreateAnd(S1, V2);
1547     setShadow(&I, IRB.CreateOr(S1S2, IRB.CreateOr(V1S2, S1V2)));
1548     setOriginForNaryOp(I);
1549   }
1550 
1551   /// \brief Default propagation of shadow and/or origin.
1552   ///
1553   /// This class implements the general case of shadow propagation, used in all
1554   /// cases where we don't know and/or don't care about what the operation
1555   /// actually does. It converts all input shadow values to a common type
1556   /// (extending or truncating as necessary), and bitwise OR's them.
1557   ///
1558   /// This is much cheaper than inserting checks (i.e. requiring inputs to be
1559   /// fully initialized), and less prone to false positives.
1560   ///
1561   /// This class also implements the general case of origin propagation. For a
1562   /// Nary operation, result origin is set to the origin of an argument that is
1563   /// not entirely initialized. If there is more than one such arguments, the
1564   /// rightmost of them is picked. It does not matter which one is picked if all
1565   /// arguments are initialized.
1566   template <bool CombineShadow>
1567   class Combiner {
1568     Value *Shadow = nullptr;
1569     Value *Origin = nullptr;
1570     IRBuilder<> &IRB;
1571     MemorySanitizerVisitor *MSV;
1572 
1573   public:
1574     Combiner(MemorySanitizerVisitor *MSV, IRBuilder<> &IRB)
1575         : IRB(IRB), MSV(MSV) {}
1576 
1577     /// \brief Add a pair of shadow and origin values to the mix.
1578     Combiner &Add(Value *OpShadow, Value *OpOrigin) {
1579       if (CombineShadow) {
1580         assert(OpShadow);
1581         if (!Shadow)
1582           Shadow = OpShadow;
1583         else {
1584           OpShadow = MSV->CreateShadowCast(IRB, OpShadow, Shadow->getType());
1585           Shadow = IRB.CreateOr(Shadow, OpShadow, "_msprop");
1586         }
1587       }
1588 
1589       if (MSV->MS.TrackOrigins) {
1590         assert(OpOrigin);
1591         if (!Origin) {
1592           Origin = OpOrigin;
1593         } else {
1594           Constant *ConstOrigin = dyn_cast<Constant>(OpOrigin);
1595           // No point in adding something that might result in 0 origin value.
1596           if (!ConstOrigin || !ConstOrigin->isNullValue()) {
1597             Value *FlatShadow = MSV->convertToShadowTyNoVec(OpShadow, IRB);
1598             Value *Cond =
1599                 IRB.CreateICmpNE(FlatShadow, MSV->getCleanShadow(FlatShadow));
1600             Origin = IRB.CreateSelect(Cond, OpOrigin, Origin);
1601           }
1602         }
1603       }
1604       return *this;
1605     }
1606 
1607     /// \brief Add an application value to the mix.
1608     Combiner &Add(Value *V) {
1609       Value *OpShadow = MSV->getShadow(V);
1610       Value *OpOrigin = MSV->MS.TrackOrigins ? MSV->getOrigin(V) : nullptr;
1611       return Add(OpShadow, OpOrigin);
1612     }
1613 
1614     /// \brief Set the current combined values as the given instruction's shadow
1615     /// and origin.
1616     void Done(Instruction *I) {
1617       if (CombineShadow) {
1618         assert(Shadow);
1619         Shadow = MSV->CreateShadowCast(IRB, Shadow, MSV->getShadowTy(I));
1620         MSV->setShadow(I, Shadow);
1621       }
1622       if (MSV->MS.TrackOrigins) {
1623         assert(Origin);
1624         MSV->setOrigin(I, Origin);
1625       }
1626     }
1627   };
1628 
1629   using ShadowAndOriginCombiner = Combiner<true>;
1630   using OriginCombiner = Combiner<false>;
1631 
1632   /// \brief Propagate origin for arbitrary operation.
1633   void setOriginForNaryOp(Instruction &I) {
1634     if (!MS.TrackOrigins) return;
1635     IRBuilder<> IRB(&I);
1636     OriginCombiner OC(this, IRB);
1637     for (Instruction::op_iterator OI = I.op_begin(); OI != I.op_end(); ++OI)
1638       OC.Add(OI->get());
1639     OC.Done(&I);
1640   }
1641 
1642   size_t VectorOrPrimitiveTypeSizeInBits(Type *Ty) {
1643     assert(!(Ty->isVectorTy() && Ty->getScalarType()->isPointerTy()) &&
1644            "Vector of pointers is not a valid shadow type");
1645     return Ty->isVectorTy() ?
1646       Ty->getVectorNumElements() * Ty->getScalarSizeInBits() :
1647       Ty->getPrimitiveSizeInBits();
1648   }
1649 
1650   /// \brief Cast between two shadow types, extending or truncating as
1651   /// necessary.
1652   Value *CreateShadowCast(IRBuilder<> &IRB, Value *V, Type *dstTy,
1653                           bool Signed = false) {
1654     Type *srcTy = V->getType();
1655     size_t srcSizeInBits = VectorOrPrimitiveTypeSizeInBits(srcTy);
1656     size_t dstSizeInBits = VectorOrPrimitiveTypeSizeInBits(dstTy);
1657     if (srcSizeInBits > 1 && dstSizeInBits == 1)
1658       return IRB.CreateICmpNE(V, getCleanShadow(V));
1659 
1660     if (dstTy->isIntegerTy() && srcTy->isIntegerTy())
1661       return IRB.CreateIntCast(V, dstTy, Signed);
1662     if (dstTy->isVectorTy() && srcTy->isVectorTy() &&
1663         dstTy->getVectorNumElements() == srcTy->getVectorNumElements())
1664       return IRB.CreateIntCast(V, dstTy, Signed);
1665     Value *V1 = IRB.CreateBitCast(V, Type::getIntNTy(*MS.C, srcSizeInBits));
1666     Value *V2 =
1667       IRB.CreateIntCast(V1, Type::getIntNTy(*MS.C, dstSizeInBits), Signed);
1668     return IRB.CreateBitCast(V2, dstTy);
1669     // TODO: handle struct types.
1670   }
1671 
1672   /// \brief Cast an application value to the type of its own shadow.
1673   Value *CreateAppToShadowCast(IRBuilder<> &IRB, Value *V) {
1674     Type *ShadowTy = getShadowTy(V);
1675     if (V->getType() == ShadowTy)
1676       return V;
1677     if (V->getType()->isPtrOrPtrVectorTy())
1678       return IRB.CreatePtrToInt(V, ShadowTy);
1679     else
1680       return IRB.CreateBitCast(V, ShadowTy);
1681   }
1682 
1683   /// \brief Propagate shadow for arbitrary operation.
1684   void handleShadowOr(Instruction &I) {
1685     IRBuilder<> IRB(&I);
1686     ShadowAndOriginCombiner SC(this, IRB);
1687     for (Instruction::op_iterator OI = I.op_begin(); OI != I.op_end(); ++OI)
1688       SC.Add(OI->get());
1689     SC.Done(&I);
1690   }
1691 
1692   // \brief Handle multiplication by constant.
1693   //
1694   // Handle a special case of multiplication by constant that may have one or
1695   // more zeros in the lower bits. This makes corresponding number of lower bits
1696   // of the result zero as well. We model it by shifting the other operand
1697   // shadow left by the required number of bits. Effectively, we transform
1698   // (X * (A * 2**B)) to ((X << B) * A) and instrument (X << B) as (Sx << B).
1699   // We use multiplication by 2**N instead of shift to cover the case of
1700   // multiplication by 0, which may occur in some elements of a vector operand.
1701   void handleMulByConstant(BinaryOperator &I, Constant *ConstArg,
1702                            Value *OtherArg) {
1703     Constant *ShadowMul;
1704     Type *Ty = ConstArg->getType();
1705     if (Ty->isVectorTy()) {
1706       unsigned NumElements = Ty->getVectorNumElements();
1707       Type *EltTy = Ty->getSequentialElementType();
1708       SmallVector<Constant *, 16> Elements;
1709       for (unsigned Idx = 0; Idx < NumElements; ++Idx) {
1710         if (ConstantInt *Elt =
1711                 dyn_cast<ConstantInt>(ConstArg->getAggregateElement(Idx))) {
1712           const APInt &V = Elt->getValue();
1713           APInt V2 = APInt(V.getBitWidth(), 1) << V.countTrailingZeros();
1714           Elements.push_back(ConstantInt::get(EltTy, V2));
1715         } else {
1716           Elements.push_back(ConstantInt::get(EltTy, 1));
1717         }
1718       }
1719       ShadowMul = ConstantVector::get(Elements);
1720     } else {
1721       if (ConstantInt *Elt = dyn_cast<ConstantInt>(ConstArg)) {
1722         const APInt &V = Elt->getValue();
1723         APInt V2 = APInt(V.getBitWidth(), 1) << V.countTrailingZeros();
1724         ShadowMul = ConstantInt::get(Ty, V2);
1725       } else {
1726         ShadowMul = ConstantInt::get(Ty, 1);
1727       }
1728     }
1729 
1730     IRBuilder<> IRB(&I);
1731     setShadow(&I,
1732               IRB.CreateMul(getShadow(OtherArg), ShadowMul, "msprop_mul_cst"));
1733     setOrigin(&I, getOrigin(OtherArg));
1734   }
1735 
1736   void visitMul(BinaryOperator &I) {
1737     Constant *constOp0 = dyn_cast<Constant>(I.getOperand(0));
1738     Constant *constOp1 = dyn_cast<Constant>(I.getOperand(1));
1739     if (constOp0 && !constOp1)
1740       handleMulByConstant(I, constOp0, I.getOperand(1));
1741     else if (constOp1 && !constOp0)
1742       handleMulByConstant(I, constOp1, I.getOperand(0));
1743     else
1744       handleShadowOr(I);
1745   }
1746 
1747   void visitFAdd(BinaryOperator &I) { handleShadowOr(I); }
1748   void visitFSub(BinaryOperator &I) { handleShadowOr(I); }
1749   void visitFMul(BinaryOperator &I) { handleShadowOr(I); }
1750   void visitAdd(BinaryOperator &I) { handleShadowOr(I); }
1751   void visitSub(BinaryOperator &I) { handleShadowOr(I); }
1752   void visitXor(BinaryOperator &I) { handleShadowOr(I); }
1753 
1754   void handleDiv(Instruction &I) {
1755     IRBuilder<> IRB(&I);
1756     // Strict on the second argument.
1757     insertShadowCheck(I.getOperand(1), &I);
1758     setShadow(&I, getShadow(&I, 0));
1759     setOrigin(&I, getOrigin(&I, 0));
1760   }
1761 
1762   void visitUDiv(BinaryOperator &I) { handleDiv(I); }
1763   void visitSDiv(BinaryOperator &I) { handleDiv(I); }
1764   void visitFDiv(BinaryOperator &I) { handleDiv(I); }
1765   void visitURem(BinaryOperator &I) { handleDiv(I); }
1766   void visitSRem(BinaryOperator &I) { handleDiv(I); }
1767   void visitFRem(BinaryOperator &I) { handleDiv(I); }
1768 
1769   /// \brief Instrument == and != comparisons.
1770   ///
1771   /// Sometimes the comparison result is known even if some of the bits of the
1772   /// arguments are not.
1773   void handleEqualityComparison(ICmpInst &I) {
1774     IRBuilder<> IRB(&I);
1775     Value *A = I.getOperand(0);
1776     Value *B = I.getOperand(1);
1777     Value *Sa = getShadow(A);
1778     Value *Sb = getShadow(B);
1779 
1780     // Get rid of pointers and vectors of pointers.
1781     // For ints (and vectors of ints), types of A and Sa match,
1782     // and this is a no-op.
1783     A = IRB.CreatePointerCast(A, Sa->getType());
1784     B = IRB.CreatePointerCast(B, Sb->getType());
1785 
1786     // A == B  <==>  (C = A^B) == 0
1787     // A != B  <==>  (C = A^B) != 0
1788     // Sc = Sa | Sb
1789     Value *C = IRB.CreateXor(A, B);
1790     Value *Sc = IRB.CreateOr(Sa, Sb);
1791     // Now dealing with i = (C == 0) comparison (or C != 0, does not matter now)
1792     // Result is defined if one of the following is true
1793     // * there is a defined 1 bit in C
1794     // * C is fully defined
1795     // Si = !(C & ~Sc) && Sc
1796     Value *Zero = Constant::getNullValue(Sc->getType());
1797     Value *MinusOne = Constant::getAllOnesValue(Sc->getType());
1798     Value *Si =
1799       IRB.CreateAnd(IRB.CreateICmpNE(Sc, Zero),
1800                     IRB.CreateICmpEQ(
1801                       IRB.CreateAnd(IRB.CreateXor(Sc, MinusOne), C), Zero));
1802     Si->setName("_msprop_icmp");
1803     setShadow(&I, Si);
1804     setOriginForNaryOp(I);
1805   }
1806 
1807   /// \brief Build the lowest possible value of V, taking into account V's
1808   ///        uninitialized bits.
1809   Value *getLowestPossibleValue(IRBuilder<> &IRB, Value *A, Value *Sa,
1810                                 bool isSigned) {
1811     if (isSigned) {
1812       // Split shadow into sign bit and other bits.
1813       Value *SaOtherBits = IRB.CreateLShr(IRB.CreateShl(Sa, 1), 1);
1814       Value *SaSignBit = IRB.CreateXor(Sa, SaOtherBits);
1815       // Maximise the undefined shadow bit, minimize other undefined bits.
1816       return
1817         IRB.CreateOr(IRB.CreateAnd(A, IRB.CreateNot(SaOtherBits)), SaSignBit);
1818     } else {
1819       // Minimize undefined bits.
1820       return IRB.CreateAnd(A, IRB.CreateNot(Sa));
1821     }
1822   }
1823 
1824   /// \brief Build the highest possible value of V, taking into account V's
1825   ///        uninitialized bits.
1826   Value *getHighestPossibleValue(IRBuilder<> &IRB, Value *A, Value *Sa,
1827                                 bool isSigned) {
1828     if (isSigned) {
1829       // Split shadow into sign bit and other bits.
1830       Value *SaOtherBits = IRB.CreateLShr(IRB.CreateShl(Sa, 1), 1);
1831       Value *SaSignBit = IRB.CreateXor(Sa, SaOtherBits);
1832       // Minimise the undefined shadow bit, maximise other undefined bits.
1833       return
1834         IRB.CreateOr(IRB.CreateAnd(A, IRB.CreateNot(SaSignBit)), SaOtherBits);
1835     } else {
1836       // Maximize undefined bits.
1837       return IRB.CreateOr(A, Sa);
1838     }
1839   }
1840 
1841   /// \brief Instrument relational comparisons.
1842   ///
1843   /// This function does exact shadow propagation for all relational
1844   /// comparisons of integers, pointers and vectors of those.
1845   /// FIXME: output seems suboptimal when one of the operands is a constant
1846   void handleRelationalComparisonExact(ICmpInst &I) {
1847     IRBuilder<> IRB(&I);
1848     Value *A = I.getOperand(0);
1849     Value *B = I.getOperand(1);
1850     Value *Sa = getShadow(A);
1851     Value *Sb = getShadow(B);
1852 
1853     // Get rid of pointers and vectors of pointers.
1854     // For ints (and vectors of ints), types of A and Sa match,
1855     // and this is a no-op.
1856     A = IRB.CreatePointerCast(A, Sa->getType());
1857     B = IRB.CreatePointerCast(B, Sb->getType());
1858 
1859     // Let [a0, a1] be the interval of possible values of A, taking into account
1860     // its undefined bits. Let [b0, b1] be the interval of possible values of B.
1861     // Then (A cmp B) is defined iff (a0 cmp b1) == (a1 cmp b0).
1862     bool IsSigned = I.isSigned();
1863     Value *S1 = IRB.CreateICmp(I.getPredicate(),
1864                                getLowestPossibleValue(IRB, A, Sa, IsSigned),
1865                                getHighestPossibleValue(IRB, B, Sb, IsSigned));
1866     Value *S2 = IRB.CreateICmp(I.getPredicate(),
1867                                getHighestPossibleValue(IRB, A, Sa, IsSigned),
1868                                getLowestPossibleValue(IRB, B, Sb, IsSigned));
1869     Value *Si = IRB.CreateXor(S1, S2);
1870     setShadow(&I, Si);
1871     setOriginForNaryOp(I);
1872   }
1873 
1874   /// \brief Instrument signed relational comparisons.
1875   ///
1876   /// Handle sign bit tests: x<0, x>=0, x<=-1, x>-1 by propagating the highest
1877   /// bit of the shadow. Everything else is delegated to handleShadowOr().
1878   void handleSignedRelationalComparison(ICmpInst &I) {
1879     Constant *constOp;
1880     Value *op = nullptr;
1881     CmpInst::Predicate pre;
1882     if ((constOp = dyn_cast<Constant>(I.getOperand(1)))) {
1883       op = I.getOperand(0);
1884       pre = I.getPredicate();
1885     } else if ((constOp = dyn_cast<Constant>(I.getOperand(0)))) {
1886       op = I.getOperand(1);
1887       pre = I.getSwappedPredicate();
1888     } else {
1889       handleShadowOr(I);
1890       return;
1891     }
1892 
1893     if ((constOp->isNullValue() &&
1894          (pre == CmpInst::ICMP_SLT || pre == CmpInst::ICMP_SGE)) ||
1895         (constOp->isAllOnesValue() &&
1896          (pre == CmpInst::ICMP_SGT || pre == CmpInst::ICMP_SLE))) {
1897       IRBuilder<> IRB(&I);
1898       Value *Shadow = IRB.CreateICmpSLT(getShadow(op), getCleanShadow(op),
1899                                         "_msprop_icmp_s");
1900       setShadow(&I, Shadow);
1901       setOrigin(&I, getOrigin(op));
1902     } else {
1903       handleShadowOr(I);
1904     }
1905   }
1906 
1907   void visitICmpInst(ICmpInst &I) {
1908     if (!ClHandleICmp) {
1909       handleShadowOr(I);
1910       return;
1911     }
1912     if (I.isEquality()) {
1913       handleEqualityComparison(I);
1914       return;
1915     }
1916 
1917     assert(I.isRelational());
1918     if (ClHandleICmpExact) {
1919       handleRelationalComparisonExact(I);
1920       return;
1921     }
1922     if (I.isSigned()) {
1923       handleSignedRelationalComparison(I);
1924       return;
1925     }
1926 
1927     assert(I.isUnsigned());
1928     if ((isa<Constant>(I.getOperand(0)) || isa<Constant>(I.getOperand(1)))) {
1929       handleRelationalComparisonExact(I);
1930       return;
1931     }
1932 
1933     handleShadowOr(I);
1934   }
1935 
1936   void visitFCmpInst(FCmpInst &I) {
1937     handleShadowOr(I);
1938   }
1939 
1940   void handleShift(BinaryOperator &I) {
1941     IRBuilder<> IRB(&I);
1942     // If any of the S2 bits are poisoned, the whole thing is poisoned.
1943     // Otherwise perform the same shift on S1.
1944     Value *S1 = getShadow(&I, 0);
1945     Value *S2 = getShadow(&I, 1);
1946     Value *S2Conv = IRB.CreateSExt(IRB.CreateICmpNE(S2, getCleanShadow(S2)),
1947                                    S2->getType());
1948     Value *V2 = I.getOperand(1);
1949     Value *Shift = IRB.CreateBinOp(I.getOpcode(), S1, V2);
1950     setShadow(&I, IRB.CreateOr(Shift, S2Conv));
1951     setOriginForNaryOp(I);
1952   }
1953 
1954   void visitShl(BinaryOperator &I) { handleShift(I); }
1955   void visitAShr(BinaryOperator &I) { handleShift(I); }
1956   void visitLShr(BinaryOperator &I) { handleShift(I); }
1957 
1958   /// \brief Instrument llvm.memmove
1959   ///
1960   /// At this point we don't know if llvm.memmove will be inlined or not.
1961   /// If we don't instrument it and it gets inlined,
1962   /// our interceptor will not kick in and we will lose the memmove.
1963   /// If we instrument the call here, but it does not get inlined,
1964   /// we will memove the shadow twice: which is bad in case
1965   /// of overlapping regions. So, we simply lower the intrinsic to a call.
1966   ///
1967   /// Similar situation exists for memcpy and memset.
1968   void visitMemMoveInst(MemMoveInst &I) {
1969     IRBuilder<> IRB(&I);
1970     IRB.CreateCall(
1971         MS.MemmoveFn,
1972         {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
1973          IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()),
1974          IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
1975     I.eraseFromParent();
1976   }
1977 
1978   // Similar to memmove: avoid copying shadow twice.
1979   // This is somewhat unfortunate as it may slowdown small constant memcpys.
1980   // FIXME: consider doing manual inline for small constant sizes and proper
1981   // alignment.
1982   void visitMemCpyInst(MemCpyInst &I) {
1983     IRBuilder<> IRB(&I);
1984     IRB.CreateCall(
1985         MS.MemcpyFn,
1986         {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
1987          IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()),
1988          IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
1989     I.eraseFromParent();
1990   }
1991 
1992   // Same as memcpy.
1993   void visitMemSetInst(MemSetInst &I) {
1994     IRBuilder<> IRB(&I);
1995     IRB.CreateCall(
1996         MS.MemsetFn,
1997         {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
1998          IRB.CreateIntCast(I.getArgOperand(1), IRB.getInt32Ty(), false),
1999          IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
2000     I.eraseFromParent();
2001   }
2002 
2003   void visitVAStartInst(VAStartInst &I) {
2004     VAHelper->visitVAStartInst(I);
2005   }
2006 
2007   void visitVACopyInst(VACopyInst &I) {
2008     VAHelper->visitVACopyInst(I);
2009   }
2010 
2011   /// \brief Handle vector store-like intrinsics.
2012   ///
2013   /// Instrument intrinsics that look like a simple SIMD store: writes memory,
2014   /// has 1 pointer argument and 1 vector argument, returns void.
2015   bool handleVectorStoreIntrinsic(IntrinsicInst &I) {
2016     IRBuilder<> IRB(&I);
2017     Value* Addr = I.getArgOperand(0);
2018     Value *Shadow = getShadow(&I, 1);
2019     Value *ShadowPtr = getShadowPtr(Addr, Shadow->getType(), IRB);
2020 
2021     // We don't know the pointer alignment (could be unaligned SSE store!).
2022     // Have to assume to worst case.
2023     IRB.CreateAlignedStore(Shadow, ShadowPtr, 1);
2024 
2025     if (ClCheckAccessAddress)
2026       insertShadowCheck(Addr, &I);
2027 
2028     // FIXME: factor out common code from materializeStores
2029     if (MS.TrackOrigins)
2030       IRB.CreateStore(getOrigin(&I, 1), getOriginPtr(Addr, IRB, 1));
2031     return true;
2032   }
2033 
2034   /// \brief Handle vector load-like intrinsics.
2035   ///
2036   /// Instrument intrinsics that look like a simple SIMD load: reads memory,
2037   /// has 1 pointer argument, returns a vector.
2038   bool handleVectorLoadIntrinsic(IntrinsicInst &I) {
2039     IRBuilder<> IRB(&I);
2040     Value *Addr = I.getArgOperand(0);
2041 
2042     Type *ShadowTy = getShadowTy(&I);
2043     if (PropagateShadow) {
2044       Value *ShadowPtr = getShadowPtr(Addr, ShadowTy, IRB);
2045       // We don't know the pointer alignment (could be unaligned SSE load!).
2046       // Have to assume to worst case.
2047       setShadow(&I, IRB.CreateAlignedLoad(ShadowPtr, 1, "_msld"));
2048     } else {
2049       setShadow(&I, getCleanShadow(&I));
2050     }
2051 
2052     if (ClCheckAccessAddress)
2053       insertShadowCheck(Addr, &I);
2054 
2055     if (MS.TrackOrigins) {
2056       if (PropagateShadow)
2057         setOrigin(&I, IRB.CreateLoad(getOriginPtr(Addr, IRB, 1)));
2058       else
2059         setOrigin(&I, getCleanOrigin());
2060     }
2061     return true;
2062   }
2063 
2064   /// \brief Handle (SIMD arithmetic)-like intrinsics.
2065   ///
2066   /// Instrument intrinsics with any number of arguments of the same type,
2067   /// equal to the return type. The type should be simple (no aggregates or
2068   /// pointers; vectors are fine).
2069   /// Caller guarantees that this intrinsic does not access memory.
2070   bool maybeHandleSimpleNomemIntrinsic(IntrinsicInst &I) {
2071     Type *RetTy = I.getType();
2072     if (!(RetTy->isIntOrIntVectorTy() ||
2073           RetTy->isFPOrFPVectorTy() ||
2074           RetTy->isX86_MMXTy()))
2075       return false;
2076 
2077     unsigned NumArgOperands = I.getNumArgOperands();
2078 
2079     for (unsigned i = 0; i < NumArgOperands; ++i) {
2080       Type *Ty = I.getArgOperand(i)->getType();
2081       if (Ty != RetTy)
2082         return false;
2083     }
2084 
2085     IRBuilder<> IRB(&I);
2086     ShadowAndOriginCombiner SC(this, IRB);
2087     for (unsigned i = 0; i < NumArgOperands; ++i)
2088       SC.Add(I.getArgOperand(i));
2089     SC.Done(&I);
2090 
2091     return true;
2092   }
2093 
2094   /// \brief Heuristically instrument unknown intrinsics.
2095   ///
2096   /// The main purpose of this code is to do something reasonable with all
2097   /// random intrinsics we might encounter, most importantly - SIMD intrinsics.
2098   /// We recognize several classes of intrinsics by their argument types and
2099   /// ModRefBehaviour and apply special intrumentation when we are reasonably
2100   /// sure that we know what the intrinsic does.
2101   ///
2102   /// We special-case intrinsics where this approach fails. See llvm.bswap
2103   /// handling as an example of that.
2104   bool handleUnknownIntrinsic(IntrinsicInst &I) {
2105     unsigned NumArgOperands = I.getNumArgOperands();
2106     if (NumArgOperands == 0)
2107       return false;
2108 
2109     if (NumArgOperands == 2 &&
2110         I.getArgOperand(0)->getType()->isPointerTy() &&
2111         I.getArgOperand(1)->getType()->isVectorTy() &&
2112         I.getType()->isVoidTy() &&
2113         !I.onlyReadsMemory()) {
2114       // This looks like a vector store.
2115       return handleVectorStoreIntrinsic(I);
2116     }
2117 
2118     if (NumArgOperands == 1 &&
2119         I.getArgOperand(0)->getType()->isPointerTy() &&
2120         I.getType()->isVectorTy() &&
2121         I.onlyReadsMemory()) {
2122       // This looks like a vector load.
2123       return handleVectorLoadIntrinsic(I);
2124     }
2125 
2126     if (I.doesNotAccessMemory())
2127       if (maybeHandleSimpleNomemIntrinsic(I))
2128         return true;
2129 
2130     // FIXME: detect and handle SSE maskstore/maskload
2131     return false;
2132   }
2133 
2134   void handleBswap(IntrinsicInst &I) {
2135     IRBuilder<> IRB(&I);
2136     Value *Op = I.getArgOperand(0);
2137     Type *OpType = Op->getType();
2138     Function *BswapFunc = Intrinsic::getDeclaration(
2139       F.getParent(), Intrinsic::bswap, makeArrayRef(&OpType, 1));
2140     setShadow(&I, IRB.CreateCall(BswapFunc, getShadow(Op)));
2141     setOrigin(&I, getOrigin(Op));
2142   }
2143 
2144   // \brief Instrument vector convert instrinsic.
2145   //
2146   // This function instruments intrinsics like cvtsi2ss:
2147   // %Out = int_xxx_cvtyyy(%ConvertOp)
2148   // or
2149   // %Out = int_xxx_cvtyyy(%CopyOp, %ConvertOp)
2150   // Intrinsic converts \p NumUsedElements elements of \p ConvertOp to the same
2151   // number \p Out elements, and (if has 2 arguments) copies the rest of the
2152   // elements from \p CopyOp.
2153   // In most cases conversion involves floating-point value which may trigger a
2154   // hardware exception when not fully initialized. For this reason we require
2155   // \p ConvertOp[0:NumUsedElements] to be fully initialized and trap otherwise.
2156   // We copy the shadow of \p CopyOp[NumUsedElements:] to \p
2157   // Out[NumUsedElements:]. This means that intrinsics without \p CopyOp always
2158   // return a fully initialized value.
2159   void handleVectorConvertIntrinsic(IntrinsicInst &I, int NumUsedElements) {
2160     IRBuilder<> IRB(&I);
2161     Value *CopyOp, *ConvertOp;
2162 
2163     switch (I.getNumArgOperands()) {
2164     case 3:
2165       assert(isa<ConstantInt>(I.getArgOperand(2)) && "Invalid rounding mode");
2166       LLVM_FALLTHROUGH;
2167     case 2:
2168       CopyOp = I.getArgOperand(0);
2169       ConvertOp = I.getArgOperand(1);
2170       break;
2171     case 1:
2172       ConvertOp = I.getArgOperand(0);
2173       CopyOp = nullptr;
2174       break;
2175     default:
2176       llvm_unreachable("Cvt intrinsic with unsupported number of arguments.");
2177     }
2178 
2179     // The first *NumUsedElements* elements of ConvertOp are converted to the
2180     // same number of output elements. The rest of the output is copied from
2181     // CopyOp, or (if not available) filled with zeroes.
2182     // Combine shadow for elements of ConvertOp that are used in this operation,
2183     // and insert a check.
2184     // FIXME: consider propagating shadow of ConvertOp, at least in the case of
2185     // int->any conversion.
2186     Value *ConvertShadow = getShadow(ConvertOp);
2187     Value *AggShadow = nullptr;
2188     if (ConvertOp->getType()->isVectorTy()) {
2189       AggShadow = IRB.CreateExtractElement(
2190           ConvertShadow, ConstantInt::get(IRB.getInt32Ty(), 0));
2191       for (int i = 1; i < NumUsedElements; ++i) {
2192         Value *MoreShadow = IRB.CreateExtractElement(
2193             ConvertShadow, ConstantInt::get(IRB.getInt32Ty(), i));
2194         AggShadow = IRB.CreateOr(AggShadow, MoreShadow);
2195       }
2196     } else {
2197       AggShadow = ConvertShadow;
2198     }
2199     assert(AggShadow->getType()->isIntegerTy());
2200     insertShadowCheck(AggShadow, getOrigin(ConvertOp), &I);
2201 
2202     // Build result shadow by zero-filling parts of CopyOp shadow that come from
2203     // ConvertOp.
2204     if (CopyOp) {
2205       assert(CopyOp->getType() == I.getType());
2206       assert(CopyOp->getType()->isVectorTy());
2207       Value *ResultShadow = getShadow(CopyOp);
2208       Type *EltTy = ResultShadow->getType()->getVectorElementType();
2209       for (int i = 0; i < NumUsedElements; ++i) {
2210         ResultShadow = IRB.CreateInsertElement(
2211             ResultShadow, ConstantInt::getNullValue(EltTy),
2212             ConstantInt::get(IRB.getInt32Ty(), i));
2213       }
2214       setShadow(&I, ResultShadow);
2215       setOrigin(&I, getOrigin(CopyOp));
2216     } else {
2217       setShadow(&I, getCleanShadow(&I));
2218       setOrigin(&I, getCleanOrigin());
2219     }
2220   }
2221 
2222   // Given a scalar or vector, extract lower 64 bits (or less), and return all
2223   // zeroes if it is zero, and all ones otherwise.
2224   Value *Lower64ShadowExtend(IRBuilder<> &IRB, Value *S, Type *T) {
2225     if (S->getType()->isVectorTy())
2226       S = CreateShadowCast(IRB, S, IRB.getInt64Ty(), /* Signed */ true);
2227     assert(S->getType()->getPrimitiveSizeInBits() <= 64);
2228     Value *S2 = IRB.CreateICmpNE(S, getCleanShadow(S));
2229     return CreateShadowCast(IRB, S2, T, /* Signed */ true);
2230   }
2231 
2232   // Given a vector, extract its first element, and return all
2233   // zeroes if it is zero, and all ones otherwise.
2234   Value *LowerElementShadowExtend(IRBuilder<> &IRB, Value *S, Type *T) {
2235     Value *S1 = IRB.CreateExtractElement(S, (uint64_t)0);
2236     Value *S2 = IRB.CreateICmpNE(S1, getCleanShadow(S1));
2237     return CreateShadowCast(IRB, S2, T, /* Signed */ true);
2238   }
2239 
2240   Value *VariableShadowExtend(IRBuilder<> &IRB, Value *S) {
2241     Type *T = S->getType();
2242     assert(T->isVectorTy());
2243     Value *S2 = IRB.CreateICmpNE(S, getCleanShadow(S));
2244     return IRB.CreateSExt(S2, T);
2245   }
2246 
2247   // \brief Instrument vector shift instrinsic.
2248   //
2249   // This function instruments intrinsics like int_x86_avx2_psll_w.
2250   // Intrinsic shifts %In by %ShiftSize bits.
2251   // %ShiftSize may be a vector. In that case the lower 64 bits determine shift
2252   // size, and the rest is ignored. Behavior is defined even if shift size is
2253   // greater than register (or field) width.
2254   void handleVectorShiftIntrinsic(IntrinsicInst &I, bool Variable) {
2255     assert(I.getNumArgOperands() == 2);
2256     IRBuilder<> IRB(&I);
2257     // If any of the S2 bits are poisoned, the whole thing is poisoned.
2258     // Otherwise perform the same shift on S1.
2259     Value *S1 = getShadow(&I, 0);
2260     Value *S2 = getShadow(&I, 1);
2261     Value *S2Conv = Variable ? VariableShadowExtend(IRB, S2)
2262                              : Lower64ShadowExtend(IRB, S2, getShadowTy(&I));
2263     Value *V1 = I.getOperand(0);
2264     Value *V2 = I.getOperand(1);
2265     Value *Shift = IRB.CreateCall(I.getCalledValue(),
2266                                   {IRB.CreateBitCast(S1, V1->getType()), V2});
2267     Shift = IRB.CreateBitCast(Shift, getShadowTy(&I));
2268     setShadow(&I, IRB.CreateOr(Shift, S2Conv));
2269     setOriginForNaryOp(I);
2270   }
2271 
2272   // \brief Get an X86_MMX-sized vector type.
2273   Type *getMMXVectorTy(unsigned EltSizeInBits) {
2274     const unsigned X86_MMXSizeInBits = 64;
2275     return VectorType::get(IntegerType::get(*MS.C, EltSizeInBits),
2276                            X86_MMXSizeInBits / EltSizeInBits);
2277   }
2278 
2279   // \brief Returns a signed counterpart for an (un)signed-saturate-and-pack
2280   // intrinsic.
2281   Intrinsic::ID getSignedPackIntrinsic(Intrinsic::ID id) {
2282     switch (id) {
2283       case Intrinsic::x86_sse2_packsswb_128:
2284       case Intrinsic::x86_sse2_packuswb_128:
2285         return Intrinsic::x86_sse2_packsswb_128;
2286 
2287       case Intrinsic::x86_sse2_packssdw_128:
2288       case Intrinsic::x86_sse41_packusdw:
2289         return Intrinsic::x86_sse2_packssdw_128;
2290 
2291       case Intrinsic::x86_avx2_packsswb:
2292       case Intrinsic::x86_avx2_packuswb:
2293         return Intrinsic::x86_avx2_packsswb;
2294 
2295       case Intrinsic::x86_avx2_packssdw:
2296       case Intrinsic::x86_avx2_packusdw:
2297         return Intrinsic::x86_avx2_packssdw;
2298 
2299       case Intrinsic::x86_mmx_packsswb:
2300       case Intrinsic::x86_mmx_packuswb:
2301         return Intrinsic::x86_mmx_packsswb;
2302 
2303       case Intrinsic::x86_mmx_packssdw:
2304         return Intrinsic::x86_mmx_packssdw;
2305       default:
2306         llvm_unreachable("unexpected intrinsic id");
2307     }
2308   }
2309 
2310   // \brief Instrument vector pack instrinsic.
2311   //
2312   // This function instruments intrinsics like x86_mmx_packsswb, that
2313   // packs elements of 2 input vectors into half as many bits with saturation.
2314   // Shadow is propagated with the signed variant of the same intrinsic applied
2315   // to sext(Sa != zeroinitializer), sext(Sb != zeroinitializer).
2316   // EltSizeInBits is used only for x86mmx arguments.
2317   void handleVectorPackIntrinsic(IntrinsicInst &I, unsigned EltSizeInBits = 0) {
2318     assert(I.getNumArgOperands() == 2);
2319     bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
2320     IRBuilder<> IRB(&I);
2321     Value *S1 = getShadow(&I, 0);
2322     Value *S2 = getShadow(&I, 1);
2323     assert(isX86_MMX || S1->getType()->isVectorTy());
2324 
2325     // SExt and ICmpNE below must apply to individual elements of input vectors.
2326     // In case of x86mmx arguments, cast them to appropriate vector types and
2327     // back.
2328     Type *T = isX86_MMX ? getMMXVectorTy(EltSizeInBits) : S1->getType();
2329     if (isX86_MMX) {
2330       S1 = IRB.CreateBitCast(S1, T);
2331       S2 = IRB.CreateBitCast(S2, T);
2332     }
2333     Value *S1_ext = IRB.CreateSExt(
2334         IRB.CreateICmpNE(S1, Constant::getNullValue(T)), T);
2335     Value *S2_ext = IRB.CreateSExt(
2336         IRB.CreateICmpNE(S2, Constant::getNullValue(T)), T);
2337     if (isX86_MMX) {
2338       Type *X86_MMXTy = Type::getX86_MMXTy(*MS.C);
2339       S1_ext = IRB.CreateBitCast(S1_ext, X86_MMXTy);
2340       S2_ext = IRB.CreateBitCast(S2_ext, X86_MMXTy);
2341     }
2342 
2343     Function *ShadowFn = Intrinsic::getDeclaration(
2344         F.getParent(), getSignedPackIntrinsic(I.getIntrinsicID()));
2345 
2346     Value *S =
2347         IRB.CreateCall(ShadowFn, {S1_ext, S2_ext}, "_msprop_vector_pack");
2348     if (isX86_MMX) S = IRB.CreateBitCast(S, getShadowTy(&I));
2349     setShadow(&I, S);
2350     setOriginForNaryOp(I);
2351   }
2352 
2353   // \brief Instrument sum-of-absolute-differencies intrinsic.
2354   void handleVectorSadIntrinsic(IntrinsicInst &I) {
2355     const unsigned SignificantBitsPerResultElement = 16;
2356     bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
2357     Type *ResTy = isX86_MMX ? IntegerType::get(*MS.C, 64) : I.getType();
2358     unsigned ZeroBitsPerResultElement =
2359         ResTy->getScalarSizeInBits() - SignificantBitsPerResultElement;
2360 
2361     IRBuilder<> IRB(&I);
2362     Value *S = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2363     S = IRB.CreateBitCast(S, ResTy);
2364     S = IRB.CreateSExt(IRB.CreateICmpNE(S, Constant::getNullValue(ResTy)),
2365                        ResTy);
2366     S = IRB.CreateLShr(S, ZeroBitsPerResultElement);
2367     S = IRB.CreateBitCast(S, getShadowTy(&I));
2368     setShadow(&I, S);
2369     setOriginForNaryOp(I);
2370   }
2371 
2372   // \brief Instrument multiply-add intrinsic.
2373   void handleVectorPmaddIntrinsic(IntrinsicInst &I,
2374                                   unsigned EltSizeInBits = 0) {
2375     bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
2376     Type *ResTy = isX86_MMX ? getMMXVectorTy(EltSizeInBits * 2) : I.getType();
2377     IRBuilder<> IRB(&I);
2378     Value *S = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2379     S = IRB.CreateBitCast(S, ResTy);
2380     S = IRB.CreateSExt(IRB.CreateICmpNE(S, Constant::getNullValue(ResTy)),
2381                        ResTy);
2382     S = IRB.CreateBitCast(S, getShadowTy(&I));
2383     setShadow(&I, S);
2384     setOriginForNaryOp(I);
2385   }
2386 
2387   // \brief Instrument compare-packed intrinsic.
2388   // Basically, an or followed by sext(icmp ne 0) to end up with all-zeros or
2389   // all-ones shadow.
2390   void handleVectorComparePackedIntrinsic(IntrinsicInst &I) {
2391     IRBuilder<> IRB(&I);
2392     Type *ResTy = getShadowTy(&I);
2393     Value *S0 = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2394     Value *S = IRB.CreateSExt(
2395         IRB.CreateICmpNE(S0, Constant::getNullValue(ResTy)), ResTy);
2396     setShadow(&I, S);
2397     setOriginForNaryOp(I);
2398   }
2399 
2400   // \brief Instrument compare-scalar intrinsic.
2401   // This handles both cmp* intrinsics which return the result in the first
2402   // element of a vector, and comi* which return the result as i32.
2403   void handleVectorCompareScalarIntrinsic(IntrinsicInst &I) {
2404     IRBuilder<> IRB(&I);
2405     Value *S0 = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1));
2406     Value *S = LowerElementShadowExtend(IRB, S0, getShadowTy(&I));
2407     setShadow(&I, S);
2408     setOriginForNaryOp(I);
2409   }
2410 
2411   void handleStmxcsr(IntrinsicInst &I) {
2412     IRBuilder<> IRB(&I);
2413     Value* Addr = I.getArgOperand(0);
2414     Type *Ty = IRB.getInt32Ty();
2415     Value *ShadowPtr = getShadowPtr(Addr, Ty, IRB);
2416 
2417     IRB.CreateStore(getCleanShadow(Ty),
2418                     IRB.CreatePointerCast(ShadowPtr, Ty->getPointerTo()));
2419 
2420     if (ClCheckAccessAddress)
2421       insertShadowCheck(Addr, &I);
2422   }
2423 
2424   void handleLdmxcsr(IntrinsicInst &I) {
2425     if (!InsertChecks) return;
2426 
2427     IRBuilder<> IRB(&I);
2428     Value *Addr = I.getArgOperand(0);
2429     Type *Ty = IRB.getInt32Ty();
2430     unsigned Alignment = 1;
2431 
2432     if (ClCheckAccessAddress)
2433       insertShadowCheck(Addr, &I);
2434 
2435     Value *Shadow = IRB.CreateAlignedLoad(getShadowPtr(Addr, Ty, IRB),
2436                                           Alignment, "_ldmxcsr");
2437     Value *Origin = MS.TrackOrigins
2438                         ? IRB.CreateLoad(getOriginPtr(Addr, IRB, Alignment))
2439                         : getCleanOrigin();
2440     insertShadowCheck(Shadow, Origin, &I);
2441   }
2442 
2443   void visitIntrinsicInst(IntrinsicInst &I) {
2444     switch (I.getIntrinsicID()) {
2445     case Intrinsic::bswap:
2446       handleBswap(I);
2447       break;
2448     case Intrinsic::x86_sse_stmxcsr:
2449       handleStmxcsr(I);
2450       break;
2451     case Intrinsic::x86_sse_ldmxcsr:
2452       handleLdmxcsr(I);
2453       break;
2454     case Intrinsic::x86_avx512_vcvtsd2usi64:
2455     case Intrinsic::x86_avx512_vcvtsd2usi32:
2456     case Intrinsic::x86_avx512_vcvtss2usi64:
2457     case Intrinsic::x86_avx512_vcvtss2usi32:
2458     case Intrinsic::x86_avx512_cvttss2usi64:
2459     case Intrinsic::x86_avx512_cvttss2usi:
2460     case Intrinsic::x86_avx512_cvttsd2usi64:
2461     case Intrinsic::x86_avx512_cvttsd2usi:
2462     case Intrinsic::x86_avx512_cvtusi2sd:
2463     case Intrinsic::x86_avx512_cvtusi2ss:
2464     case Intrinsic::x86_avx512_cvtusi642sd:
2465     case Intrinsic::x86_avx512_cvtusi642ss:
2466     case Intrinsic::x86_sse2_cvtsd2si64:
2467     case Intrinsic::x86_sse2_cvtsd2si:
2468     case Intrinsic::x86_sse2_cvtsd2ss:
2469     case Intrinsic::x86_sse2_cvtsi2sd:
2470     case Intrinsic::x86_sse2_cvtsi642sd:
2471     case Intrinsic::x86_sse2_cvtss2sd:
2472     case Intrinsic::x86_sse2_cvttsd2si64:
2473     case Intrinsic::x86_sse2_cvttsd2si:
2474     case Intrinsic::x86_sse_cvtsi2ss:
2475     case Intrinsic::x86_sse_cvtsi642ss:
2476     case Intrinsic::x86_sse_cvtss2si64:
2477     case Intrinsic::x86_sse_cvtss2si:
2478     case Intrinsic::x86_sse_cvttss2si64:
2479     case Intrinsic::x86_sse_cvttss2si:
2480       handleVectorConvertIntrinsic(I, 1);
2481       break;
2482     case Intrinsic::x86_sse_cvtps2pi:
2483     case Intrinsic::x86_sse_cvttps2pi:
2484       handleVectorConvertIntrinsic(I, 2);
2485       break;
2486 
2487     case Intrinsic::x86_avx512_psll_w_512:
2488     case Intrinsic::x86_avx512_psll_d_512:
2489     case Intrinsic::x86_avx512_psll_q_512:
2490     case Intrinsic::x86_avx512_pslli_w_512:
2491     case Intrinsic::x86_avx512_pslli_d_512:
2492     case Intrinsic::x86_avx512_pslli_q_512:
2493     case Intrinsic::x86_avx512_psrl_w_512:
2494     case Intrinsic::x86_avx512_psrl_d_512:
2495     case Intrinsic::x86_avx512_psrl_q_512:
2496     case Intrinsic::x86_avx512_psra_w_512:
2497     case Intrinsic::x86_avx512_psra_d_512:
2498     case Intrinsic::x86_avx512_psra_q_512:
2499     case Intrinsic::x86_avx512_psrli_w_512:
2500     case Intrinsic::x86_avx512_psrli_d_512:
2501     case Intrinsic::x86_avx512_psrli_q_512:
2502     case Intrinsic::x86_avx512_psrai_w_512:
2503     case Intrinsic::x86_avx512_psrai_d_512:
2504     case Intrinsic::x86_avx512_psrai_q_512:
2505     case Intrinsic::x86_avx512_psra_q_256:
2506     case Intrinsic::x86_avx512_psra_q_128:
2507     case Intrinsic::x86_avx512_psrai_q_256:
2508     case Intrinsic::x86_avx512_psrai_q_128:
2509     case Intrinsic::x86_avx2_psll_w:
2510     case Intrinsic::x86_avx2_psll_d:
2511     case Intrinsic::x86_avx2_psll_q:
2512     case Intrinsic::x86_avx2_pslli_w:
2513     case Intrinsic::x86_avx2_pslli_d:
2514     case Intrinsic::x86_avx2_pslli_q:
2515     case Intrinsic::x86_avx2_psrl_w:
2516     case Intrinsic::x86_avx2_psrl_d:
2517     case Intrinsic::x86_avx2_psrl_q:
2518     case Intrinsic::x86_avx2_psra_w:
2519     case Intrinsic::x86_avx2_psra_d:
2520     case Intrinsic::x86_avx2_psrli_w:
2521     case Intrinsic::x86_avx2_psrli_d:
2522     case Intrinsic::x86_avx2_psrli_q:
2523     case Intrinsic::x86_avx2_psrai_w:
2524     case Intrinsic::x86_avx2_psrai_d:
2525     case Intrinsic::x86_sse2_psll_w:
2526     case Intrinsic::x86_sse2_psll_d:
2527     case Intrinsic::x86_sse2_psll_q:
2528     case Intrinsic::x86_sse2_pslli_w:
2529     case Intrinsic::x86_sse2_pslli_d:
2530     case Intrinsic::x86_sse2_pslli_q:
2531     case Intrinsic::x86_sse2_psrl_w:
2532     case Intrinsic::x86_sse2_psrl_d:
2533     case Intrinsic::x86_sse2_psrl_q:
2534     case Intrinsic::x86_sse2_psra_w:
2535     case Intrinsic::x86_sse2_psra_d:
2536     case Intrinsic::x86_sse2_psrli_w:
2537     case Intrinsic::x86_sse2_psrli_d:
2538     case Intrinsic::x86_sse2_psrli_q:
2539     case Intrinsic::x86_sse2_psrai_w:
2540     case Intrinsic::x86_sse2_psrai_d:
2541     case Intrinsic::x86_mmx_psll_w:
2542     case Intrinsic::x86_mmx_psll_d:
2543     case Intrinsic::x86_mmx_psll_q:
2544     case Intrinsic::x86_mmx_pslli_w:
2545     case Intrinsic::x86_mmx_pslli_d:
2546     case Intrinsic::x86_mmx_pslli_q:
2547     case Intrinsic::x86_mmx_psrl_w:
2548     case Intrinsic::x86_mmx_psrl_d:
2549     case Intrinsic::x86_mmx_psrl_q:
2550     case Intrinsic::x86_mmx_psra_w:
2551     case Intrinsic::x86_mmx_psra_d:
2552     case Intrinsic::x86_mmx_psrli_w:
2553     case Intrinsic::x86_mmx_psrli_d:
2554     case Intrinsic::x86_mmx_psrli_q:
2555     case Intrinsic::x86_mmx_psrai_w:
2556     case Intrinsic::x86_mmx_psrai_d:
2557       handleVectorShiftIntrinsic(I, /* Variable */ false);
2558       break;
2559     case Intrinsic::x86_avx2_psllv_d:
2560     case Intrinsic::x86_avx2_psllv_d_256:
2561     case Intrinsic::x86_avx512_psllv_d_512:
2562     case Intrinsic::x86_avx2_psllv_q:
2563     case Intrinsic::x86_avx2_psllv_q_256:
2564     case Intrinsic::x86_avx512_psllv_q_512:
2565     case Intrinsic::x86_avx2_psrlv_d:
2566     case Intrinsic::x86_avx2_psrlv_d_256:
2567     case Intrinsic::x86_avx512_psrlv_d_512:
2568     case Intrinsic::x86_avx2_psrlv_q:
2569     case Intrinsic::x86_avx2_psrlv_q_256:
2570     case Intrinsic::x86_avx512_psrlv_q_512:
2571     case Intrinsic::x86_avx2_psrav_d:
2572     case Intrinsic::x86_avx2_psrav_d_256:
2573     case Intrinsic::x86_avx512_psrav_d_512:
2574     case Intrinsic::x86_avx512_psrav_q_128:
2575     case Intrinsic::x86_avx512_psrav_q_256:
2576     case Intrinsic::x86_avx512_psrav_q_512:
2577       handleVectorShiftIntrinsic(I, /* Variable */ true);
2578       break;
2579 
2580     case Intrinsic::x86_sse2_packsswb_128:
2581     case Intrinsic::x86_sse2_packssdw_128:
2582     case Intrinsic::x86_sse2_packuswb_128:
2583     case Intrinsic::x86_sse41_packusdw:
2584     case Intrinsic::x86_avx2_packsswb:
2585     case Intrinsic::x86_avx2_packssdw:
2586     case Intrinsic::x86_avx2_packuswb:
2587     case Intrinsic::x86_avx2_packusdw:
2588       handleVectorPackIntrinsic(I);
2589       break;
2590 
2591     case Intrinsic::x86_mmx_packsswb:
2592     case Intrinsic::x86_mmx_packuswb:
2593       handleVectorPackIntrinsic(I, 16);
2594       break;
2595 
2596     case Intrinsic::x86_mmx_packssdw:
2597       handleVectorPackIntrinsic(I, 32);
2598       break;
2599 
2600     case Intrinsic::x86_mmx_psad_bw:
2601     case Intrinsic::x86_sse2_psad_bw:
2602     case Intrinsic::x86_avx2_psad_bw:
2603       handleVectorSadIntrinsic(I);
2604       break;
2605 
2606     case Intrinsic::x86_sse2_pmadd_wd:
2607     case Intrinsic::x86_avx2_pmadd_wd:
2608     case Intrinsic::x86_ssse3_pmadd_ub_sw_128:
2609     case Intrinsic::x86_avx2_pmadd_ub_sw:
2610       handleVectorPmaddIntrinsic(I);
2611       break;
2612 
2613     case Intrinsic::x86_ssse3_pmadd_ub_sw:
2614       handleVectorPmaddIntrinsic(I, 8);
2615       break;
2616 
2617     case Intrinsic::x86_mmx_pmadd_wd:
2618       handleVectorPmaddIntrinsic(I, 16);
2619       break;
2620 
2621     case Intrinsic::x86_sse_cmp_ss:
2622     case Intrinsic::x86_sse2_cmp_sd:
2623     case Intrinsic::x86_sse_comieq_ss:
2624     case Intrinsic::x86_sse_comilt_ss:
2625     case Intrinsic::x86_sse_comile_ss:
2626     case Intrinsic::x86_sse_comigt_ss:
2627     case Intrinsic::x86_sse_comige_ss:
2628     case Intrinsic::x86_sse_comineq_ss:
2629     case Intrinsic::x86_sse_ucomieq_ss:
2630     case Intrinsic::x86_sse_ucomilt_ss:
2631     case Intrinsic::x86_sse_ucomile_ss:
2632     case Intrinsic::x86_sse_ucomigt_ss:
2633     case Intrinsic::x86_sse_ucomige_ss:
2634     case Intrinsic::x86_sse_ucomineq_ss:
2635     case Intrinsic::x86_sse2_comieq_sd:
2636     case Intrinsic::x86_sse2_comilt_sd:
2637     case Intrinsic::x86_sse2_comile_sd:
2638     case Intrinsic::x86_sse2_comigt_sd:
2639     case Intrinsic::x86_sse2_comige_sd:
2640     case Intrinsic::x86_sse2_comineq_sd:
2641     case Intrinsic::x86_sse2_ucomieq_sd:
2642     case Intrinsic::x86_sse2_ucomilt_sd:
2643     case Intrinsic::x86_sse2_ucomile_sd:
2644     case Intrinsic::x86_sse2_ucomigt_sd:
2645     case Intrinsic::x86_sse2_ucomige_sd:
2646     case Intrinsic::x86_sse2_ucomineq_sd:
2647       handleVectorCompareScalarIntrinsic(I);
2648       break;
2649 
2650     case Intrinsic::x86_sse_cmp_ps:
2651     case Intrinsic::x86_sse2_cmp_pd:
2652       // FIXME: For x86_avx_cmp_pd_256 and x86_avx_cmp_ps_256 this function
2653       // generates reasonably looking IR that fails in the backend with "Do not
2654       // know how to split the result of this operator!".
2655       handleVectorComparePackedIntrinsic(I);
2656       break;
2657 
2658     default:
2659       if (!handleUnknownIntrinsic(I))
2660         visitInstruction(I);
2661       break;
2662     }
2663   }
2664 
2665   void visitCallSite(CallSite CS) {
2666     Instruction &I = *CS.getInstruction();
2667     assert(!I.getMetadata("nosanitize"));
2668     assert((CS.isCall() || CS.isInvoke()) && "Unknown type of CallSite");
2669     if (CS.isCall()) {
2670       CallInst *Call = cast<CallInst>(&I);
2671 
2672       // For inline asm, do the usual thing: check argument shadow and mark all
2673       // outputs as clean. Note that any side effects of the inline asm that are
2674       // not immediately visible in its constraints are not handled.
2675       if (Call->isInlineAsm()) {
2676         visitInstruction(I);
2677         return;
2678       }
2679 
2680       assert(!isa<IntrinsicInst>(&I) && "intrinsics are handled elsewhere");
2681 
2682       // We are going to insert code that relies on the fact that the callee
2683       // will become a non-readonly function after it is instrumented by us. To
2684       // prevent this code from being optimized out, mark that function
2685       // non-readonly in advance.
2686       if (Function *Func = Call->getCalledFunction()) {
2687         // Clear out readonly/readnone attributes.
2688         AttrBuilder B;
2689         B.addAttribute(Attribute::ReadOnly)
2690           .addAttribute(Attribute::ReadNone);
2691         Func->removeAttributes(AttributeList::FunctionIndex, B);
2692       }
2693 
2694       maybeMarkSanitizerLibraryCallNoBuiltin(Call, TLI);
2695     }
2696     IRBuilder<> IRB(&I);
2697 
2698     unsigned ArgOffset = 0;
2699     DEBUG(dbgs() << "  CallSite: " << I << "\n");
2700     for (CallSite::arg_iterator ArgIt = CS.arg_begin(), End = CS.arg_end();
2701          ArgIt != End; ++ArgIt) {
2702       Value *A = *ArgIt;
2703       unsigned i = ArgIt - CS.arg_begin();
2704       if (!A->getType()->isSized()) {
2705         DEBUG(dbgs() << "Arg " << i << " is not sized: " << I << "\n");
2706         continue;
2707       }
2708       unsigned Size = 0;
2709       Value *Store = nullptr;
2710       // Compute the Shadow for arg even if it is ByVal, because
2711       // in that case getShadow() will copy the actual arg shadow to
2712       // __msan_param_tls.
2713       Value *ArgShadow = getShadow(A);
2714       Value *ArgShadowBase = getShadowPtrForArgument(A, IRB, ArgOffset);
2715       DEBUG(dbgs() << "  Arg#" << i << ": " << *A <<
2716             " Shadow: " << *ArgShadow << "\n");
2717       bool ArgIsInitialized = false;
2718       const DataLayout &DL = F.getParent()->getDataLayout();
2719       if (CS.paramHasAttr(i, Attribute::ByVal)) {
2720         assert(A->getType()->isPointerTy() &&
2721                "ByVal argument is not a pointer!");
2722         Size = DL.getTypeAllocSize(A->getType()->getPointerElementType());
2723         if (ArgOffset + Size > kParamTLSSize) break;
2724         unsigned ParamAlignment = CS.getParamAlignment(i);
2725         unsigned Alignment = std::min(ParamAlignment, kShadowTLSAlignment);
2726         Store = IRB.CreateMemCpy(ArgShadowBase,
2727                                  getShadowPtr(A, Type::getInt8Ty(*MS.C), IRB),
2728                                  Size, Alignment);
2729       } else {
2730         Size = DL.getTypeAllocSize(A->getType());
2731         if (ArgOffset + Size > kParamTLSSize) break;
2732         Store = IRB.CreateAlignedStore(ArgShadow, ArgShadowBase,
2733                                        kShadowTLSAlignment);
2734         Constant *Cst = dyn_cast<Constant>(ArgShadow);
2735         if (Cst && Cst->isNullValue()) ArgIsInitialized = true;
2736       }
2737       if (MS.TrackOrigins && !ArgIsInitialized)
2738         IRB.CreateStore(getOrigin(A),
2739                         getOriginPtrForArgument(A, IRB, ArgOffset));
2740       (void)Store;
2741       assert(Size != 0 && Store != nullptr);
2742       DEBUG(dbgs() << "  Param:" << *Store << "\n");
2743       ArgOffset += alignTo(Size, 8);
2744     }
2745     DEBUG(dbgs() << "  done with call args\n");
2746 
2747     FunctionType *FT =
2748       cast<FunctionType>(CS.getCalledValue()->getType()->getContainedType(0));
2749     if (FT->isVarArg()) {
2750       VAHelper->visitCallSite(CS, IRB);
2751     }
2752 
2753     // Now, get the shadow for the RetVal.
2754     if (!I.getType()->isSized()) return;
2755     // Don't emit the epilogue for musttail call returns.
2756     if (CS.isCall() && cast<CallInst>(&I)->isMustTailCall()) return;
2757     IRBuilder<> IRBBefore(&I);
2758     // Until we have full dynamic coverage, make sure the retval shadow is 0.
2759     Value *Base = getShadowPtrForRetval(&I, IRBBefore);
2760     IRBBefore.CreateAlignedStore(getCleanShadow(&I), Base, kShadowTLSAlignment);
2761     BasicBlock::iterator NextInsn;
2762     if (CS.isCall()) {
2763       NextInsn = ++I.getIterator();
2764       assert(NextInsn != I.getParent()->end());
2765     } else {
2766       BasicBlock *NormalDest = cast<InvokeInst>(&I)->getNormalDest();
2767       if (!NormalDest->getSinglePredecessor()) {
2768         // FIXME: this case is tricky, so we are just conservative here.
2769         // Perhaps we need to split the edge between this BB and NormalDest,
2770         // but a naive attempt to use SplitEdge leads to a crash.
2771         setShadow(&I, getCleanShadow(&I));
2772         setOrigin(&I, getCleanOrigin());
2773         return;
2774       }
2775       NextInsn = NormalDest->getFirstInsertionPt();
2776       assert(NextInsn != NormalDest->end() &&
2777              "Could not find insertion point for retval shadow load");
2778     }
2779     IRBuilder<> IRBAfter(&*NextInsn);
2780     Value *RetvalShadow =
2781       IRBAfter.CreateAlignedLoad(getShadowPtrForRetval(&I, IRBAfter),
2782                                  kShadowTLSAlignment, "_msret");
2783     setShadow(&I, RetvalShadow);
2784     if (MS.TrackOrigins)
2785       setOrigin(&I, IRBAfter.CreateLoad(getOriginPtrForRetval(IRBAfter)));
2786   }
2787 
2788   bool isAMustTailRetVal(Value *RetVal) {
2789     if (auto *I = dyn_cast<BitCastInst>(RetVal)) {
2790       RetVal = I->getOperand(0);
2791     }
2792     if (auto *I = dyn_cast<CallInst>(RetVal)) {
2793       return I->isMustTailCall();
2794     }
2795     return false;
2796   }
2797 
2798   void visitReturnInst(ReturnInst &I) {
2799     IRBuilder<> IRB(&I);
2800     Value *RetVal = I.getReturnValue();
2801     if (!RetVal) return;
2802     // Don't emit the epilogue for musttail call returns.
2803     if (isAMustTailRetVal(RetVal)) return;
2804     Value *ShadowPtr = getShadowPtrForRetval(RetVal, IRB);
2805     if (CheckReturnValue) {
2806       insertShadowCheck(RetVal, &I);
2807       Value *Shadow = getCleanShadow(RetVal);
2808       IRB.CreateAlignedStore(Shadow, ShadowPtr, kShadowTLSAlignment);
2809     } else {
2810       Value *Shadow = getShadow(RetVal);
2811       IRB.CreateAlignedStore(Shadow, ShadowPtr, kShadowTLSAlignment);
2812       if (MS.TrackOrigins)
2813         IRB.CreateStore(getOrigin(RetVal), getOriginPtrForRetval(IRB));
2814     }
2815   }
2816 
2817   void visitPHINode(PHINode &I) {
2818     IRBuilder<> IRB(&I);
2819     if (!PropagateShadow) {
2820       setShadow(&I, getCleanShadow(&I));
2821       setOrigin(&I, getCleanOrigin());
2822       return;
2823     }
2824 
2825     ShadowPHINodes.push_back(&I);
2826     setShadow(&I, IRB.CreatePHI(getShadowTy(&I), I.getNumIncomingValues(),
2827                                 "_msphi_s"));
2828     if (MS.TrackOrigins)
2829       setOrigin(&I, IRB.CreatePHI(MS.OriginTy, I.getNumIncomingValues(),
2830                                   "_msphi_o"));
2831   }
2832 
2833   void visitAllocaInst(AllocaInst &I) {
2834     setShadow(&I, getCleanShadow(&I));
2835     setOrigin(&I, getCleanOrigin());
2836     IRBuilder<> IRB(I.getNextNode());
2837     const DataLayout &DL = F.getParent()->getDataLayout();
2838     uint64_t TypeSize = DL.getTypeAllocSize(I.getAllocatedType());
2839     Value *Len = ConstantInt::get(MS.IntptrTy, TypeSize);
2840     if (I.isArrayAllocation())
2841       Len = IRB.CreateMul(Len, I.getArraySize());
2842     if (PoisonStack && ClPoisonStackWithCall) {
2843       IRB.CreateCall(MS.MsanPoisonStackFn,
2844                      {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len});
2845     } else {
2846       Value *ShadowBase = getShadowPtr(&I, Type::getInt8PtrTy(*MS.C), IRB);
2847       Value *PoisonValue = IRB.getInt8(PoisonStack ? ClPoisonStackPattern : 0);
2848       IRB.CreateMemSet(ShadowBase, PoisonValue, Len, I.getAlignment());
2849     }
2850 
2851     if (PoisonStack && MS.TrackOrigins) {
2852       SmallString<2048> StackDescriptionStorage;
2853       raw_svector_ostream StackDescription(StackDescriptionStorage);
2854       // We create a string with a description of the stack allocation and
2855       // pass it into __msan_set_alloca_origin.
2856       // It will be printed by the run-time if stack-originated UMR is found.
2857       // The first 4 bytes of the string are set to '----' and will be replaced
2858       // by __msan_va_arg_overflow_size_tls at the first call.
2859       StackDescription << "----" << I.getName() << "@" << F.getName();
2860       Value *Descr =
2861           createPrivateNonConstGlobalForString(*F.getParent(),
2862                                                StackDescription.str());
2863 
2864       IRB.CreateCall(MS.MsanSetAllocaOrigin4Fn,
2865                      {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len,
2866                       IRB.CreatePointerCast(Descr, IRB.getInt8PtrTy()),
2867                       IRB.CreatePointerCast(&F, MS.IntptrTy)});
2868     }
2869   }
2870 
2871   void visitSelectInst(SelectInst& I) {
2872     IRBuilder<> IRB(&I);
2873     // a = select b, c, d
2874     Value *B = I.getCondition();
2875     Value *C = I.getTrueValue();
2876     Value *D = I.getFalseValue();
2877     Value *Sb = getShadow(B);
2878     Value *Sc = getShadow(C);
2879     Value *Sd = getShadow(D);
2880 
2881     // Result shadow if condition shadow is 0.
2882     Value *Sa0 = IRB.CreateSelect(B, Sc, Sd);
2883     Value *Sa1;
2884     if (I.getType()->isAggregateType()) {
2885       // To avoid "sign extending" i1 to an arbitrary aggregate type, we just do
2886       // an extra "select". This results in much more compact IR.
2887       // Sa = select Sb, poisoned, (select b, Sc, Sd)
2888       Sa1 = getPoisonedShadow(getShadowTy(I.getType()));
2889     } else {
2890       // Sa = select Sb, [ (c^d) | Sc | Sd ], [ b ? Sc : Sd ]
2891       // If Sb (condition is poisoned), look for bits in c and d that are equal
2892       // and both unpoisoned.
2893       // If !Sb (condition is unpoisoned), simply pick one of Sc and Sd.
2894 
2895       // Cast arguments to shadow-compatible type.
2896       C = CreateAppToShadowCast(IRB, C);
2897       D = CreateAppToShadowCast(IRB, D);
2898 
2899       // Result shadow if condition shadow is 1.
2900       Sa1 = IRB.CreateOr(IRB.CreateXor(C, D), IRB.CreateOr(Sc, Sd));
2901     }
2902     Value *Sa = IRB.CreateSelect(Sb, Sa1, Sa0, "_msprop_select");
2903     setShadow(&I, Sa);
2904     if (MS.TrackOrigins) {
2905       // Origins are always i32, so any vector conditions must be flattened.
2906       // FIXME: consider tracking vector origins for app vectors?
2907       if (B->getType()->isVectorTy()) {
2908         Type *FlatTy = getShadowTyNoVec(B->getType());
2909         B = IRB.CreateICmpNE(IRB.CreateBitCast(B, FlatTy),
2910                                 ConstantInt::getNullValue(FlatTy));
2911         Sb = IRB.CreateICmpNE(IRB.CreateBitCast(Sb, FlatTy),
2912                                       ConstantInt::getNullValue(FlatTy));
2913       }
2914       // a = select b, c, d
2915       // Oa = Sb ? Ob : (b ? Oc : Od)
2916       setOrigin(
2917           &I, IRB.CreateSelect(Sb, getOrigin(I.getCondition()),
2918                                IRB.CreateSelect(B, getOrigin(I.getTrueValue()),
2919                                                 getOrigin(I.getFalseValue()))));
2920     }
2921   }
2922 
2923   void visitLandingPadInst(LandingPadInst &I) {
2924     // Do nothing.
2925     // See https://github.com/google/sanitizers/issues/504
2926     setShadow(&I, getCleanShadow(&I));
2927     setOrigin(&I, getCleanOrigin());
2928   }
2929 
2930   void visitCatchSwitchInst(CatchSwitchInst &I) {
2931     setShadow(&I, getCleanShadow(&I));
2932     setOrigin(&I, getCleanOrigin());
2933   }
2934 
2935   void visitFuncletPadInst(FuncletPadInst &I) {
2936     setShadow(&I, getCleanShadow(&I));
2937     setOrigin(&I, getCleanOrigin());
2938   }
2939 
2940   void visitGetElementPtrInst(GetElementPtrInst &I) {
2941     handleShadowOr(I);
2942   }
2943 
2944   void visitExtractValueInst(ExtractValueInst &I) {
2945     IRBuilder<> IRB(&I);
2946     Value *Agg = I.getAggregateOperand();
2947     DEBUG(dbgs() << "ExtractValue:  " << I << "\n");
2948     Value *AggShadow = getShadow(Agg);
2949     DEBUG(dbgs() << "   AggShadow:  " << *AggShadow << "\n");
2950     Value *ResShadow = IRB.CreateExtractValue(AggShadow, I.getIndices());
2951     DEBUG(dbgs() << "   ResShadow:  " << *ResShadow << "\n");
2952     setShadow(&I, ResShadow);
2953     setOriginForNaryOp(I);
2954   }
2955 
2956   void visitInsertValueInst(InsertValueInst &I) {
2957     IRBuilder<> IRB(&I);
2958     DEBUG(dbgs() << "InsertValue:  " << I << "\n");
2959     Value *AggShadow = getShadow(I.getAggregateOperand());
2960     Value *InsShadow = getShadow(I.getInsertedValueOperand());
2961     DEBUG(dbgs() << "   AggShadow:  " << *AggShadow << "\n");
2962     DEBUG(dbgs() << "   InsShadow:  " << *InsShadow << "\n");
2963     Value *Res = IRB.CreateInsertValue(AggShadow, InsShadow, I.getIndices());
2964     DEBUG(dbgs() << "   Res:        " << *Res << "\n");
2965     setShadow(&I, Res);
2966     setOriginForNaryOp(I);
2967   }
2968 
2969   void dumpInst(Instruction &I) {
2970     if (CallInst *CI = dyn_cast<CallInst>(&I)) {
2971       errs() << "ZZZ call " << CI->getCalledFunction()->getName() << "\n";
2972     } else {
2973       errs() << "ZZZ " << I.getOpcodeName() << "\n";
2974     }
2975     errs() << "QQQ " << I << "\n";
2976   }
2977 
2978   void visitResumeInst(ResumeInst &I) {
2979     DEBUG(dbgs() << "Resume: " << I << "\n");
2980     // Nothing to do here.
2981   }
2982 
2983   void visitCleanupReturnInst(CleanupReturnInst &CRI) {
2984     DEBUG(dbgs() << "CleanupReturn: " << CRI << "\n");
2985     // Nothing to do here.
2986   }
2987 
2988   void visitCatchReturnInst(CatchReturnInst &CRI) {
2989     DEBUG(dbgs() << "CatchReturn: " << CRI << "\n");
2990     // Nothing to do here.
2991   }
2992 
2993   void visitInstruction(Instruction &I) {
2994     // Everything else: stop propagating and check for poisoned shadow.
2995     if (ClDumpStrictInstructions)
2996       dumpInst(I);
2997     DEBUG(dbgs() << "DEFAULT: " << I << "\n");
2998     for (size_t i = 0, n = I.getNumOperands(); i < n; i++) {
2999       Value *Operand = I.getOperand(i);
3000       if (Operand->getType()->isSized())
3001         insertShadowCheck(Operand, &I);
3002     }
3003     setShadow(&I, getCleanShadow(&I));
3004     setOrigin(&I, getCleanOrigin());
3005   }
3006 };
3007 
3008 /// \brief AMD64-specific implementation of VarArgHelper.
3009 struct VarArgAMD64Helper : public VarArgHelper {
3010   // An unfortunate workaround for asymmetric lowering of va_arg stuff.
3011   // See a comment in visitCallSite for more details.
3012   static const unsigned AMD64GpEndOffset = 48;  // AMD64 ABI Draft 0.99.6 p3.5.7
3013   static const unsigned AMD64FpEndOffset = 176;
3014 
3015   Function &F;
3016   MemorySanitizer &MS;
3017   MemorySanitizerVisitor &MSV;
3018   Value *VAArgTLSCopy = nullptr;
3019   Value *VAArgOverflowSize = nullptr;
3020 
3021   SmallVector<CallInst*, 16> VAStartInstrumentationList;
3022 
3023   enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory };
3024 
3025   VarArgAMD64Helper(Function &F, MemorySanitizer &MS,
3026                     MemorySanitizerVisitor &MSV) : F(F), MS(MS), MSV(MSV) {}
3027 
3028   ArgKind classifyArgument(Value* arg) {
3029     // A very rough approximation of X86_64 argument classification rules.
3030     Type *T = arg->getType();
3031     if (T->isFPOrFPVectorTy() || T->isX86_MMXTy())
3032       return AK_FloatingPoint;
3033     if (T->isIntegerTy() && T->getPrimitiveSizeInBits() <= 64)
3034       return AK_GeneralPurpose;
3035     if (T->isPointerTy())
3036       return AK_GeneralPurpose;
3037     return AK_Memory;
3038   }
3039 
3040   // For VarArg functions, store the argument shadow in an ABI-specific format
3041   // that corresponds to va_list layout.
3042   // We do this because Clang lowers va_arg in the frontend, and this pass
3043   // only sees the low level code that deals with va_list internals.
3044   // A much easier alternative (provided that Clang emits va_arg instructions)
3045   // would have been to associate each live instance of va_list with a copy of
3046   // MSanParamTLS, and extract shadow on va_arg() call in the argument list
3047   // order.
3048   void visitCallSite(CallSite &CS, IRBuilder<> &IRB) override {
3049     unsigned GpOffset = 0;
3050     unsigned FpOffset = AMD64GpEndOffset;
3051     unsigned OverflowOffset = AMD64FpEndOffset;
3052     const DataLayout &DL = F.getParent()->getDataLayout();
3053     for (CallSite::arg_iterator ArgIt = CS.arg_begin(), End = CS.arg_end();
3054          ArgIt != End; ++ArgIt) {
3055       Value *A = *ArgIt;
3056       unsigned ArgNo = CS.getArgumentNo(ArgIt);
3057       bool IsFixed = ArgNo < CS.getFunctionType()->getNumParams();
3058       bool IsByVal = CS.paramHasAttr(ArgNo, Attribute::ByVal);
3059       if (IsByVal) {
3060         // ByVal arguments always go to the overflow area.
3061         // Fixed arguments passed through the overflow area will be stepped
3062         // over by va_start, so don't count them towards the offset.
3063         if (IsFixed)
3064           continue;
3065         assert(A->getType()->isPointerTy());
3066         Type *RealTy = A->getType()->getPointerElementType();
3067         uint64_t ArgSize = DL.getTypeAllocSize(RealTy);
3068         Value *Base = getShadowPtrForVAArgument(RealTy, IRB, OverflowOffset);
3069         OverflowOffset += alignTo(ArgSize, 8);
3070         IRB.CreateMemCpy(Base, MSV.getShadowPtr(A, IRB.getInt8Ty(), IRB),
3071                          ArgSize, kShadowTLSAlignment);
3072       } else {
3073         ArgKind AK = classifyArgument(A);
3074         if (AK == AK_GeneralPurpose && GpOffset >= AMD64GpEndOffset)
3075           AK = AK_Memory;
3076         if (AK == AK_FloatingPoint && FpOffset >= AMD64FpEndOffset)
3077           AK = AK_Memory;
3078         Value *Base;
3079         switch (AK) {
3080           case AK_GeneralPurpose:
3081             Base = getShadowPtrForVAArgument(A->getType(), IRB, GpOffset);
3082             GpOffset += 8;
3083             break;
3084           case AK_FloatingPoint:
3085             Base = getShadowPtrForVAArgument(A->getType(), IRB, FpOffset);
3086             FpOffset += 16;
3087             break;
3088           case AK_Memory:
3089             if (IsFixed)
3090               continue;
3091             uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
3092             Base = getShadowPtrForVAArgument(A->getType(), IRB, OverflowOffset);
3093             OverflowOffset += alignTo(ArgSize, 8);
3094         }
3095         // Take fixed arguments into account for GpOffset and FpOffset,
3096         // but don't actually store shadows for them.
3097         if (IsFixed)
3098           continue;
3099         IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
3100       }
3101     }
3102     Constant *OverflowSize =
3103       ConstantInt::get(IRB.getInt64Ty(), OverflowOffset - AMD64FpEndOffset);
3104     IRB.CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
3105   }
3106 
3107   /// \brief Compute the shadow address for a given va_arg.
3108   Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
3109                                    int ArgOffset) {
3110     Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
3111     Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
3112     return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
3113                               "_msarg");
3114   }
3115 
3116   void visitVAStartInst(VAStartInst &I) override {
3117     if (F.getCallingConv() == CallingConv::Win64)
3118       return;
3119     IRBuilder<> IRB(&I);
3120     VAStartInstrumentationList.push_back(&I);
3121     Value *VAListTag = I.getArgOperand(0);
3122     Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
3123 
3124     // Unpoison the whole __va_list_tag.
3125     // FIXME: magic ABI constants.
3126     IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
3127                      /* size */24, /* alignment */8, false);
3128   }
3129 
3130   void visitVACopyInst(VACopyInst &I) override {
3131     if (F.getCallingConv() == CallingConv::Win64)
3132       return;
3133     IRBuilder<> IRB(&I);
3134     Value *VAListTag = I.getArgOperand(0);
3135     Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
3136 
3137     // Unpoison the whole __va_list_tag.
3138     // FIXME: magic ABI constants.
3139     IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
3140                      /* size */24, /* alignment */8, false);
3141   }
3142 
3143   void finalizeInstrumentation() override {
3144     assert(!VAArgOverflowSize && !VAArgTLSCopy &&
3145            "finalizeInstrumentation called twice");
3146     if (!VAStartInstrumentationList.empty()) {
3147       // If there is a va_start in this function, make a backup copy of
3148       // va_arg_tls somewhere in the function entry block.
3149       IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
3150       VAArgOverflowSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS);
3151       Value *CopySize =
3152         IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, AMD64FpEndOffset),
3153                       VAArgOverflowSize);
3154       VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
3155       IRB.CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8);
3156     }
3157 
3158     // Instrument va_start.
3159     // Copy va_list shadow from the backup copy of the TLS contents.
3160     for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
3161       CallInst *OrigInst = VAStartInstrumentationList[i];
3162       IRBuilder<> IRB(OrigInst->getNextNode());
3163       Value *VAListTag = OrigInst->getArgOperand(0);
3164 
3165       Value *RegSaveAreaPtrPtr =
3166         IRB.CreateIntToPtr(
3167           IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
3168                         ConstantInt::get(MS.IntptrTy, 16)),
3169           Type::getInt64PtrTy(*MS.C));
3170       Value *RegSaveAreaPtr = IRB.CreateLoad(RegSaveAreaPtrPtr);
3171       Value *RegSaveAreaShadowPtr =
3172         MSV.getShadowPtr(RegSaveAreaPtr, IRB.getInt8Ty(), IRB);
3173       IRB.CreateMemCpy(RegSaveAreaShadowPtr, VAArgTLSCopy,
3174                        AMD64FpEndOffset, 16);
3175 
3176       Value *OverflowArgAreaPtrPtr =
3177         IRB.CreateIntToPtr(
3178           IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
3179                         ConstantInt::get(MS.IntptrTy, 8)),
3180           Type::getInt64PtrTy(*MS.C));
3181       Value *OverflowArgAreaPtr = IRB.CreateLoad(OverflowArgAreaPtrPtr);
3182       Value *OverflowArgAreaShadowPtr =
3183         MSV.getShadowPtr(OverflowArgAreaPtr, IRB.getInt8Ty(), IRB);
3184       Value *SrcPtr = IRB.CreateConstGEP1_32(IRB.getInt8Ty(), VAArgTLSCopy,
3185                                              AMD64FpEndOffset);
3186       IRB.CreateMemCpy(OverflowArgAreaShadowPtr, SrcPtr, VAArgOverflowSize, 16);
3187     }
3188   }
3189 };
3190 
3191 /// \brief MIPS64-specific implementation of VarArgHelper.
3192 struct VarArgMIPS64Helper : public VarArgHelper {
3193   Function &F;
3194   MemorySanitizer &MS;
3195   MemorySanitizerVisitor &MSV;
3196   Value *VAArgTLSCopy = nullptr;
3197   Value *VAArgSize = nullptr;
3198 
3199   SmallVector<CallInst*, 16> VAStartInstrumentationList;
3200 
3201   VarArgMIPS64Helper(Function &F, MemorySanitizer &MS,
3202                     MemorySanitizerVisitor &MSV) : F(F), MS(MS), MSV(MSV) {}
3203 
3204   void visitCallSite(CallSite &CS, IRBuilder<> &IRB) override {
3205     unsigned VAArgOffset = 0;
3206     const DataLayout &DL = F.getParent()->getDataLayout();
3207     for (CallSite::arg_iterator ArgIt = CS.arg_begin() +
3208          CS.getFunctionType()->getNumParams(), End = CS.arg_end();
3209          ArgIt != End; ++ArgIt) {
3210       Triple TargetTriple(F.getParent()->getTargetTriple());
3211       Value *A = *ArgIt;
3212       Value *Base;
3213       uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
3214       if (TargetTriple.getArch() == Triple::mips64) {
3215         // Adjusting the shadow for argument with size < 8 to match the placement
3216         // of bits in big endian system
3217         if (ArgSize < 8)
3218           VAArgOffset += (8 - ArgSize);
3219       }
3220       Base = getShadowPtrForVAArgument(A->getType(), IRB, VAArgOffset);
3221       VAArgOffset += ArgSize;
3222       VAArgOffset = alignTo(VAArgOffset, 8);
3223       IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
3224     }
3225 
3226     Constant *TotalVAArgSize = ConstantInt::get(IRB.getInt64Ty(), VAArgOffset);
3227     // Here using VAArgOverflowSizeTLS as VAArgSizeTLS to avoid creation of
3228     // a new class member i.e. it is the total size of all VarArgs.
3229     IRB.CreateStore(TotalVAArgSize, MS.VAArgOverflowSizeTLS);
3230   }
3231 
3232   /// \brief Compute the shadow address for a given va_arg.
3233   Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
3234                                    int ArgOffset) {
3235     Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
3236     Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
3237     return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
3238                               "_msarg");
3239   }
3240 
3241   void visitVAStartInst(VAStartInst &I) override {
3242     IRBuilder<> IRB(&I);
3243     VAStartInstrumentationList.push_back(&I);
3244     Value *VAListTag = I.getArgOperand(0);
3245     Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
3246     IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
3247                      /* size */8, /* alignment */8, false);
3248   }
3249 
3250   void visitVACopyInst(VACopyInst &I) override {
3251     IRBuilder<> IRB(&I);
3252     Value *VAListTag = I.getArgOperand(0);
3253     Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
3254     // Unpoison the whole __va_list_tag.
3255     // FIXME: magic ABI constants.
3256     IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
3257                      /* size */8, /* alignment */8, false);
3258   }
3259 
3260   void finalizeInstrumentation() override {
3261     assert(!VAArgSize && !VAArgTLSCopy &&
3262            "finalizeInstrumentation called twice");
3263     IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
3264     VAArgSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS);
3265     Value *CopySize = IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, 0),
3266                                     VAArgSize);
3267 
3268     if (!VAStartInstrumentationList.empty()) {
3269       // If there is a va_start in this function, make a backup copy of
3270       // va_arg_tls somewhere in the function entry block.
3271       VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
3272       IRB.CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8);
3273     }
3274 
3275     // Instrument va_start.
3276     // Copy va_list shadow from the backup copy of the TLS contents.
3277     for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
3278       CallInst *OrigInst = VAStartInstrumentationList[i];
3279       IRBuilder<> IRB(OrigInst->getNextNode());
3280       Value *VAListTag = OrigInst->getArgOperand(0);
3281       Value *RegSaveAreaPtrPtr =
3282         IRB.CreateIntToPtr(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
3283                         Type::getInt64PtrTy(*MS.C));
3284       Value *RegSaveAreaPtr = IRB.CreateLoad(RegSaveAreaPtrPtr);
3285       Value *RegSaveAreaShadowPtr =
3286       MSV.getShadowPtr(RegSaveAreaPtr, IRB.getInt8Ty(), IRB);
3287       IRB.CreateMemCpy(RegSaveAreaShadowPtr, VAArgTLSCopy, CopySize, 8);
3288     }
3289   }
3290 };
3291 
3292 /// \brief AArch64-specific implementation of VarArgHelper.
3293 struct VarArgAArch64Helper : public VarArgHelper {
3294   static const unsigned kAArch64GrArgSize = 64;
3295   static const unsigned kAArch64VrArgSize = 128;
3296 
3297   static const unsigned AArch64GrBegOffset = 0;
3298   static const unsigned AArch64GrEndOffset = kAArch64GrArgSize;
3299   // Make VR space aligned to 16 bytes.
3300   static const unsigned AArch64VrBegOffset = AArch64GrEndOffset;
3301   static const unsigned AArch64VrEndOffset = AArch64VrBegOffset
3302                                              + kAArch64VrArgSize;
3303   static const unsigned AArch64VAEndOffset = AArch64VrEndOffset;
3304 
3305   Function &F;
3306   MemorySanitizer &MS;
3307   MemorySanitizerVisitor &MSV;
3308   Value *VAArgTLSCopy = nullptr;
3309   Value *VAArgOverflowSize = nullptr;
3310 
3311   SmallVector<CallInst*, 16> VAStartInstrumentationList;
3312 
3313   enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory };
3314 
3315   VarArgAArch64Helper(Function &F, MemorySanitizer &MS,
3316                     MemorySanitizerVisitor &MSV) : F(F), MS(MS), MSV(MSV) {}
3317 
3318   ArgKind classifyArgument(Value* arg) {
3319     Type *T = arg->getType();
3320     if (T->isFPOrFPVectorTy())
3321       return AK_FloatingPoint;
3322     if ((T->isIntegerTy() && T->getPrimitiveSizeInBits() <= 64)
3323         || (T->isPointerTy()))
3324       return AK_GeneralPurpose;
3325     return AK_Memory;
3326   }
3327 
3328   // The instrumentation stores the argument shadow in a non ABI-specific
3329   // format because it does not know which argument is named (since Clang,
3330   // like x86_64 case, lowers the va_args in the frontend and this pass only
3331   // sees the low level code that deals with va_list internals).
3332   // The first seven GR registers are saved in the first 56 bytes of the
3333   // va_arg tls arra, followers by the first 8 FP/SIMD registers, and then
3334   // the remaining arguments.
3335   // Using constant offset within the va_arg TLS array allows fast copy
3336   // in the finalize instrumentation.
3337   void visitCallSite(CallSite &CS, IRBuilder<> &IRB) override {
3338     unsigned GrOffset = AArch64GrBegOffset;
3339     unsigned VrOffset = AArch64VrBegOffset;
3340     unsigned OverflowOffset = AArch64VAEndOffset;
3341 
3342     const DataLayout &DL = F.getParent()->getDataLayout();
3343     for (CallSite::arg_iterator ArgIt = CS.arg_begin(), End = CS.arg_end();
3344          ArgIt != End; ++ArgIt) {
3345       Value *A = *ArgIt;
3346       unsigned ArgNo = CS.getArgumentNo(ArgIt);
3347       bool IsFixed = ArgNo < CS.getFunctionType()->getNumParams();
3348       ArgKind AK = classifyArgument(A);
3349       if (AK == AK_GeneralPurpose && GrOffset >= AArch64GrEndOffset)
3350         AK = AK_Memory;
3351       if (AK == AK_FloatingPoint && VrOffset >= AArch64VrEndOffset)
3352         AK = AK_Memory;
3353       Value *Base;
3354       switch (AK) {
3355         case AK_GeneralPurpose:
3356           Base = getShadowPtrForVAArgument(A->getType(), IRB, GrOffset);
3357           GrOffset += 8;
3358           break;
3359         case AK_FloatingPoint:
3360           Base = getShadowPtrForVAArgument(A->getType(), IRB, VrOffset);
3361           VrOffset += 16;
3362           break;
3363         case AK_Memory:
3364           // Don't count fixed arguments in the overflow area - va_start will
3365           // skip right over them.
3366           if (IsFixed)
3367             continue;
3368           uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
3369           Base = getShadowPtrForVAArgument(A->getType(), IRB, OverflowOffset);
3370           OverflowOffset += alignTo(ArgSize, 8);
3371           break;
3372       }
3373       // Count Gp/Vr fixed arguments to their respective offsets, but don't
3374       // bother to actually store a shadow.
3375       if (IsFixed)
3376         continue;
3377       IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
3378     }
3379     Constant *OverflowSize =
3380       ConstantInt::get(IRB.getInt64Ty(), OverflowOffset - AArch64VAEndOffset);
3381     IRB.CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
3382   }
3383 
3384   /// Compute the shadow address for a given va_arg.
3385   Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
3386                                    int ArgOffset) {
3387     Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
3388     Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
3389     return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
3390                               "_msarg");
3391   }
3392 
3393   void visitVAStartInst(VAStartInst &I) override {
3394     IRBuilder<> IRB(&I);
3395     VAStartInstrumentationList.push_back(&I);
3396     Value *VAListTag = I.getArgOperand(0);
3397     Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
3398     // Unpoison the whole __va_list_tag.
3399     // FIXME: magic ABI constants (size of va_list).
3400     IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
3401                      /* size */32, /* alignment */8, false);
3402   }
3403 
3404   void visitVACopyInst(VACopyInst &I) override {
3405     IRBuilder<> IRB(&I);
3406     Value *VAListTag = I.getArgOperand(0);
3407     Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
3408     // Unpoison the whole __va_list_tag.
3409     // FIXME: magic ABI constants (size of va_list).
3410     IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
3411                      /* size */32, /* alignment */8, false);
3412   }
3413 
3414   // Retrieve a va_list field of 'void*' size.
3415   Value* getVAField64(IRBuilder<> &IRB, Value *VAListTag, int offset) {
3416     Value *SaveAreaPtrPtr =
3417       IRB.CreateIntToPtr(
3418         IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
3419                       ConstantInt::get(MS.IntptrTy, offset)),
3420         Type::getInt64PtrTy(*MS.C));
3421     return IRB.CreateLoad(SaveAreaPtrPtr);
3422   }
3423 
3424   // Retrieve a va_list field of 'int' size.
3425   Value* getVAField32(IRBuilder<> &IRB, Value *VAListTag, int offset) {
3426     Value *SaveAreaPtr =
3427       IRB.CreateIntToPtr(
3428         IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
3429                       ConstantInt::get(MS.IntptrTy, offset)),
3430         Type::getInt32PtrTy(*MS.C));
3431     Value *SaveArea32 = IRB.CreateLoad(SaveAreaPtr);
3432     return IRB.CreateSExt(SaveArea32, MS.IntptrTy);
3433   }
3434 
3435   void finalizeInstrumentation() override {
3436     assert(!VAArgOverflowSize && !VAArgTLSCopy &&
3437            "finalizeInstrumentation called twice");
3438     if (!VAStartInstrumentationList.empty()) {
3439       // If there is a va_start in this function, make a backup copy of
3440       // va_arg_tls somewhere in the function entry block.
3441       IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
3442       VAArgOverflowSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS);
3443       Value *CopySize =
3444         IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, AArch64VAEndOffset),
3445                       VAArgOverflowSize);
3446       VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
3447       IRB.CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8);
3448     }
3449 
3450     Value *GrArgSize = ConstantInt::get(MS.IntptrTy, kAArch64GrArgSize);
3451     Value *VrArgSize = ConstantInt::get(MS.IntptrTy, kAArch64VrArgSize);
3452 
3453     // Instrument va_start, copy va_list shadow from the backup copy of
3454     // the TLS contents.
3455     for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
3456       CallInst *OrigInst = VAStartInstrumentationList[i];
3457       IRBuilder<> IRB(OrigInst->getNextNode());
3458 
3459       Value *VAListTag = OrigInst->getArgOperand(0);
3460 
3461       // The variadic ABI for AArch64 creates two areas to save the incoming
3462       // argument registers (one for 64-bit general register xn-x7 and another
3463       // for 128-bit FP/SIMD vn-v7).
3464       // We need then to propagate the shadow arguments on both regions
3465       // 'va::__gr_top + va::__gr_offs' and 'va::__vr_top + va::__vr_offs'.
3466       // The remaning arguments are saved on shadow for 'va::stack'.
3467       // One caveat is it requires only to propagate the non-named arguments,
3468       // however on the call site instrumentation 'all' the arguments are
3469       // saved. So to copy the shadow values from the va_arg TLS array
3470       // we need to adjust the offset for both GR and VR fields based on
3471       // the __{gr,vr}_offs value (since they are stores based on incoming
3472       // named arguments).
3473 
3474       // Read the stack pointer from the va_list.
3475       Value *StackSaveAreaPtr = getVAField64(IRB, VAListTag, 0);
3476 
3477       // Read both the __gr_top and __gr_off and add them up.
3478       Value *GrTopSaveAreaPtr = getVAField64(IRB, VAListTag, 8);
3479       Value *GrOffSaveArea = getVAField32(IRB, VAListTag, 24);
3480 
3481       Value *GrRegSaveAreaPtr = IRB.CreateAdd(GrTopSaveAreaPtr, GrOffSaveArea);
3482 
3483       // Read both the __vr_top and __vr_off and add them up.
3484       Value *VrTopSaveAreaPtr = getVAField64(IRB, VAListTag, 16);
3485       Value *VrOffSaveArea = getVAField32(IRB, VAListTag, 28);
3486 
3487       Value *VrRegSaveAreaPtr = IRB.CreateAdd(VrTopSaveAreaPtr, VrOffSaveArea);
3488 
3489       // It does not know how many named arguments is being used and, on the
3490       // callsite all the arguments were saved.  Since __gr_off is defined as
3491       // '0 - ((8 - named_gr) * 8)', the idea is to just propagate the variadic
3492       // argument by ignoring the bytes of shadow from named arguments.
3493       Value *GrRegSaveAreaShadowPtrOff =
3494         IRB.CreateAdd(GrArgSize, GrOffSaveArea);
3495 
3496       Value *GrRegSaveAreaShadowPtr =
3497         MSV.getShadowPtr(GrRegSaveAreaPtr, IRB.getInt8Ty(), IRB);
3498 
3499       Value *GrSrcPtr = IRB.CreateInBoundsGEP(IRB.getInt8Ty(), VAArgTLSCopy,
3500                                               GrRegSaveAreaShadowPtrOff);
3501       Value *GrCopySize = IRB.CreateSub(GrArgSize, GrRegSaveAreaShadowPtrOff);
3502 
3503       IRB.CreateMemCpy(GrRegSaveAreaShadowPtr, GrSrcPtr, GrCopySize, 8);
3504 
3505       // Again, but for FP/SIMD values.
3506       Value *VrRegSaveAreaShadowPtrOff =
3507           IRB.CreateAdd(VrArgSize, VrOffSaveArea);
3508 
3509       Value *VrRegSaveAreaShadowPtr =
3510         MSV.getShadowPtr(VrRegSaveAreaPtr, IRB.getInt8Ty(), IRB);
3511 
3512       Value *VrSrcPtr = IRB.CreateInBoundsGEP(
3513         IRB.getInt8Ty(),
3514         IRB.CreateInBoundsGEP(IRB.getInt8Ty(), VAArgTLSCopy,
3515                               IRB.getInt32(AArch64VrBegOffset)),
3516         VrRegSaveAreaShadowPtrOff);
3517       Value *VrCopySize = IRB.CreateSub(VrArgSize, VrRegSaveAreaShadowPtrOff);
3518 
3519       IRB.CreateMemCpy(VrRegSaveAreaShadowPtr, VrSrcPtr, VrCopySize, 8);
3520 
3521       // And finally for remaining arguments.
3522       Value *StackSaveAreaShadowPtr =
3523         MSV.getShadowPtr(StackSaveAreaPtr, IRB.getInt8Ty(), IRB);
3524 
3525       Value *StackSrcPtr =
3526         IRB.CreateInBoundsGEP(IRB.getInt8Ty(), VAArgTLSCopy,
3527                               IRB.getInt32(AArch64VAEndOffset));
3528 
3529       IRB.CreateMemCpy(StackSaveAreaShadowPtr, StackSrcPtr,
3530                        VAArgOverflowSize, 16);
3531     }
3532   }
3533 };
3534 
3535 /// \brief PowerPC64-specific implementation of VarArgHelper.
3536 struct VarArgPowerPC64Helper : public VarArgHelper {
3537   Function &F;
3538   MemorySanitizer &MS;
3539   MemorySanitizerVisitor &MSV;
3540   Value *VAArgTLSCopy = nullptr;
3541   Value *VAArgSize = nullptr;
3542 
3543   SmallVector<CallInst*, 16> VAStartInstrumentationList;
3544 
3545   VarArgPowerPC64Helper(Function &F, MemorySanitizer &MS,
3546                     MemorySanitizerVisitor &MSV) : F(F), MS(MS), MSV(MSV) {}
3547 
3548   void visitCallSite(CallSite &CS, IRBuilder<> &IRB) override {
3549     // For PowerPC, we need to deal with alignment of stack arguments -
3550     // they are mostly aligned to 8 bytes, but vectors and i128 arrays
3551     // are aligned to 16 bytes, byvals can be aligned to 8 or 16 bytes,
3552     // and QPX vectors are aligned to 32 bytes.  For that reason, we
3553     // compute current offset from stack pointer (which is always properly
3554     // aligned), and offset for the first vararg, then subtract them.
3555     unsigned VAArgBase;
3556     Triple TargetTriple(F.getParent()->getTargetTriple());
3557     // Parameter save area starts at 48 bytes from frame pointer for ABIv1,
3558     // and 32 bytes for ABIv2.  This is usually determined by target
3559     // endianness, but in theory could be overriden by function attribute.
3560     // For simplicity, we ignore it here (it'd only matter for QPX vectors).
3561     if (TargetTriple.getArch() == Triple::ppc64)
3562       VAArgBase = 48;
3563     else
3564       VAArgBase = 32;
3565     unsigned VAArgOffset = VAArgBase;
3566     const DataLayout &DL = F.getParent()->getDataLayout();
3567     for (CallSite::arg_iterator ArgIt = CS.arg_begin(), End = CS.arg_end();
3568          ArgIt != End; ++ArgIt) {
3569       Value *A = *ArgIt;
3570       unsigned ArgNo = CS.getArgumentNo(ArgIt);
3571       bool IsFixed = ArgNo < CS.getFunctionType()->getNumParams();
3572       bool IsByVal = CS.paramHasAttr(ArgNo, Attribute::ByVal);
3573       if (IsByVal) {
3574         assert(A->getType()->isPointerTy());
3575         Type *RealTy = A->getType()->getPointerElementType();
3576         uint64_t ArgSize = DL.getTypeAllocSize(RealTy);
3577         uint64_t ArgAlign = CS.getParamAlignment(ArgNo);
3578         if (ArgAlign < 8)
3579           ArgAlign = 8;
3580         VAArgOffset = alignTo(VAArgOffset, ArgAlign);
3581         if (!IsFixed) {
3582           Value *Base = getShadowPtrForVAArgument(RealTy, IRB,
3583                                                   VAArgOffset - VAArgBase);
3584           IRB.CreateMemCpy(Base, MSV.getShadowPtr(A, IRB.getInt8Ty(), IRB),
3585                            ArgSize, kShadowTLSAlignment);
3586         }
3587         VAArgOffset += alignTo(ArgSize, 8);
3588       } else {
3589         Value *Base;
3590         uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
3591         uint64_t ArgAlign = 8;
3592         if (A->getType()->isArrayTy()) {
3593           // Arrays are aligned to element size, except for long double
3594           // arrays, which are aligned to 8 bytes.
3595           Type *ElementTy = A->getType()->getArrayElementType();
3596           if (!ElementTy->isPPC_FP128Ty())
3597             ArgAlign = DL.getTypeAllocSize(ElementTy);
3598         } else if (A->getType()->isVectorTy()) {
3599           // Vectors are naturally aligned.
3600           ArgAlign = DL.getTypeAllocSize(A->getType());
3601         }
3602         if (ArgAlign < 8)
3603           ArgAlign = 8;
3604         VAArgOffset = alignTo(VAArgOffset, ArgAlign);
3605         if (DL.isBigEndian()) {
3606           // Adjusting the shadow for argument with size < 8 to match the placement
3607           // of bits in big endian system
3608           if (ArgSize < 8)
3609             VAArgOffset += (8 - ArgSize);
3610         }
3611         if (!IsFixed) {
3612           Base = getShadowPtrForVAArgument(A->getType(), IRB,
3613                                            VAArgOffset - VAArgBase);
3614           IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
3615         }
3616         VAArgOffset += ArgSize;
3617         VAArgOffset = alignTo(VAArgOffset, 8);
3618       }
3619       if (IsFixed)
3620         VAArgBase = VAArgOffset;
3621     }
3622 
3623     Constant *TotalVAArgSize = ConstantInt::get(IRB.getInt64Ty(),
3624                                                 VAArgOffset - VAArgBase);
3625     // Here using VAArgOverflowSizeTLS as VAArgSizeTLS to avoid creation of
3626     // a new class member i.e. it is the total size of all VarArgs.
3627     IRB.CreateStore(TotalVAArgSize, MS.VAArgOverflowSizeTLS);
3628   }
3629 
3630   /// \brief Compute the shadow address for a given va_arg.
3631   Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
3632                                    int ArgOffset) {
3633     Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
3634     Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
3635     return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
3636                               "_msarg");
3637   }
3638 
3639   void visitVAStartInst(VAStartInst &I) override {
3640     IRBuilder<> IRB(&I);
3641     VAStartInstrumentationList.push_back(&I);
3642     Value *VAListTag = I.getArgOperand(0);
3643     Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
3644     IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
3645                      /* size */8, /* alignment */8, false);
3646   }
3647 
3648   void visitVACopyInst(VACopyInst &I) override {
3649     IRBuilder<> IRB(&I);
3650     Value *VAListTag = I.getArgOperand(0);
3651     Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
3652     // Unpoison the whole __va_list_tag.
3653     // FIXME: magic ABI constants.
3654     IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
3655                      /* size */8, /* alignment */8, false);
3656   }
3657 
3658   void finalizeInstrumentation() override {
3659     assert(!VAArgSize && !VAArgTLSCopy &&
3660            "finalizeInstrumentation called twice");
3661     IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
3662     VAArgSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS);
3663     Value *CopySize = IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, 0),
3664                                     VAArgSize);
3665 
3666     if (!VAStartInstrumentationList.empty()) {
3667       // If there is a va_start in this function, make a backup copy of
3668       // va_arg_tls somewhere in the function entry block.
3669       VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
3670       IRB.CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8);
3671     }
3672 
3673     // Instrument va_start.
3674     // Copy va_list shadow from the backup copy of the TLS contents.
3675     for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
3676       CallInst *OrigInst = VAStartInstrumentationList[i];
3677       IRBuilder<> IRB(OrigInst->getNextNode());
3678       Value *VAListTag = OrigInst->getArgOperand(0);
3679       Value *RegSaveAreaPtrPtr =
3680         IRB.CreateIntToPtr(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
3681                         Type::getInt64PtrTy(*MS.C));
3682       Value *RegSaveAreaPtr = IRB.CreateLoad(RegSaveAreaPtrPtr);
3683       Value *RegSaveAreaShadowPtr =
3684       MSV.getShadowPtr(RegSaveAreaPtr, IRB.getInt8Ty(), IRB);
3685       IRB.CreateMemCpy(RegSaveAreaShadowPtr, VAArgTLSCopy, CopySize, 8);
3686     }
3687   }
3688 };
3689 
3690 /// \brief A no-op implementation of VarArgHelper.
3691 struct VarArgNoOpHelper : public VarArgHelper {
3692   VarArgNoOpHelper(Function &F, MemorySanitizer &MS,
3693                    MemorySanitizerVisitor &MSV) {}
3694 
3695   void visitCallSite(CallSite &CS, IRBuilder<> &IRB) override {}
3696 
3697   void visitVAStartInst(VAStartInst &I) override {}
3698 
3699   void visitVACopyInst(VACopyInst &I) override {}
3700 
3701   void finalizeInstrumentation() override {}
3702 };
3703 
3704 } // end anonymous namespace
3705 
3706 static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
3707                                         MemorySanitizerVisitor &Visitor) {
3708   // VarArg handling is only implemented on AMD64. False positives are possible
3709   // on other platforms.
3710   Triple TargetTriple(Func.getParent()->getTargetTriple());
3711   if (TargetTriple.getArch() == Triple::x86_64)
3712     return new VarArgAMD64Helper(Func, Msan, Visitor);
3713   else if (TargetTriple.getArch() == Triple::mips64 ||
3714            TargetTriple.getArch() == Triple::mips64el)
3715     return new VarArgMIPS64Helper(Func, Msan, Visitor);
3716   else if (TargetTriple.getArch() == Triple::aarch64)
3717     return new VarArgAArch64Helper(Func, Msan, Visitor);
3718   else if (TargetTriple.getArch() == Triple::ppc64 ||
3719            TargetTriple.getArch() == Triple::ppc64le)
3720     return new VarArgPowerPC64Helper(Func, Msan, Visitor);
3721   else
3722     return new VarArgNoOpHelper(Func, Msan, Visitor);
3723 }
3724 
3725 bool MemorySanitizer::runOnFunction(Function &F) {
3726   if (&F == MsanCtorFunction)
3727     return false;
3728   MemorySanitizerVisitor Visitor(F, *this);
3729 
3730   // Clear out readonly/readnone attributes.
3731   AttrBuilder B;
3732   B.addAttribute(Attribute::ReadOnly)
3733     .addAttribute(Attribute::ReadNone);
3734   F.removeAttributes(AttributeList::FunctionIndex, B);
3735 
3736   return Visitor.runOnFunction();
3737 }
3738