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