Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 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 | /// |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 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). |
Evgeniy Stepanov | d8be0c5 | 2012-12-26 10:59:00 +0000 | [diff] [blame] | 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 |
Alexey Samsonov | 3efc87e | 2012-12-28 09:30:44 +0000 | [diff] [blame] | 61 | /// practice. |
Evgeniy Stepanov | d8be0c5 | 2012-12-26 10:59:00 +0000 | [diff] [blame] | 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. |
Evgeniy Stepanov | 5522a70 | 2013-09-24 11:20:27 +0000 | [diff] [blame] | 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 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 92 | //===----------------------------------------------------------------------===// |
| 93 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 94 | #include "llvm/Transforms/Instrumentation.h" |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 95 | #include "llvm/ADT/DepthFirstIterator.h" |
| 96 | #include "llvm/ADT/SmallString.h" |
| 97 | #include "llvm/ADT/SmallVector.h" |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 98 | #include "llvm/ADT/StringExtras.h" |
Evgeniy Stepanov | ebd7f8e | 2013-05-21 12:27:47 +0000 | [diff] [blame] | 99 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 100 | #include "llvm/IR/DataLayout.h" |
| 101 | #include "llvm/IR/Function.h" |
| 102 | #include "llvm/IR/IRBuilder.h" |
| 103 | #include "llvm/IR/InlineAsm.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 104 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 105 | #include "llvm/IR/IntrinsicInst.h" |
| 106 | #include "llvm/IR/LLVMContext.h" |
| 107 | #include "llvm/IR/MDBuilder.h" |
| 108 | #include "llvm/IR/Module.h" |
| 109 | #include "llvm/IR/Type.h" |
Chandler Carruth | a4ea269 | 2014-03-04 11:26:31 +0000 | [diff] [blame] | 110 | #include "llvm/IR/ValueMap.h" |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 111 | #include "llvm/Support/CommandLine.h" |
| 112 | #include "llvm/Support/Compiler.h" |
| 113 | #include "llvm/Support/Debug.h" |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 114 | #include "llvm/Support/raw_ostream.h" |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 115 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Evgeniy Stepanov | 4fbc0d08 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 116 | #include "llvm/Transforms/Utils/Local.h" |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 117 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
| 118 | |
| 119 | using namespace llvm; |
| 120 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 121 | #define DEBUG_TYPE "msan" |
| 122 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 123 | static const uint64_t kShadowMask32 = 1ULL << 31; |
| 124 | static const uint64_t kShadowMask64 = 1ULL << 46; |
| 125 | static const uint64_t kOriginOffset32 = 1ULL << 30; |
| 126 | static const uint64_t kOriginOffset64 = 1ULL << 45; |
Evgeniy Stepanov | 5eb5bf8 | 2012-12-26 11:55:09 +0000 | [diff] [blame] | 127 | static const unsigned kMinOriginAlignment = 4; |
| 128 | static const unsigned kShadowTLSAlignment = 8; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 129 | |
Evgeniy Stepanov | 35eb265 | 2014-10-22 00:12:40 +0000 | [diff] [blame^] | 130 | // These constants must be kept in sync with the ones in msan.h. |
| 131 | static const unsigned kParamTLSSize = 800; |
| 132 | static const unsigned kRetvalTLSSize = 800; |
| 133 | |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 134 | // Accesses sizes are powers of two: 1, 2, 4, 8. |
| 135 | static const size_t kNumberOfAccessSizes = 4; |
| 136 | |
Evgeniy Stepanov | d8be0c5 | 2012-12-26 10:59:00 +0000 | [diff] [blame] | 137 | /// \brief Track origins of uninitialized values. |
Alexey Samsonov | 3efc87e | 2012-12-28 09:30:44 +0000 | [diff] [blame] | 138 | /// |
Evgeniy Stepanov | d8be0c5 | 2012-12-26 10:59:00 +0000 | [diff] [blame] | 139 | /// Adds a section to MemorySanitizer report that points to the allocation |
| 140 | /// (stack or heap) the uninitialized bits came from originally. |
Evgeniy Stepanov | 302964e | 2014-03-18 13:30:56 +0000 | [diff] [blame] | 141 | static cl::opt<int> ClTrackOrigins("msan-track-origins", |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 142 | cl::desc("Track origins (allocation sites) of poisoned memory"), |
Evgeniy Stepanov | 302964e | 2014-03-18 13:30:56 +0000 | [diff] [blame] | 143 | cl::Hidden, cl::init(0)); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 144 | static cl::opt<bool> ClKeepGoing("msan-keep-going", |
| 145 | cl::desc("keep going after reporting a UMR"), |
| 146 | cl::Hidden, cl::init(false)); |
| 147 | static cl::opt<bool> ClPoisonStack("msan-poison-stack", |
| 148 | cl::desc("poison uninitialized stack variables"), |
| 149 | cl::Hidden, cl::init(true)); |
| 150 | static cl::opt<bool> ClPoisonStackWithCall("msan-poison-stack-with-call", |
| 151 | cl::desc("poison uninitialized stack variables with a call"), |
| 152 | cl::Hidden, cl::init(false)); |
| 153 | static cl::opt<int> ClPoisonStackPattern("msan-poison-stack-pattern", |
| 154 | cl::desc("poison uninitialized stack variables with the given patter"), |
| 155 | cl::Hidden, cl::init(0xff)); |
Evgeniy Stepanov | a9a962c | 2013-03-21 09:38:26 +0000 | [diff] [blame] | 156 | static cl::opt<bool> ClPoisonUndef("msan-poison-undef", |
| 157 | cl::desc("poison undef temps"), |
| 158 | cl::Hidden, cl::init(true)); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 159 | |
| 160 | static cl::opt<bool> ClHandleICmp("msan-handle-icmp", |
| 161 | cl::desc("propagate shadow through ICmpEQ and ICmpNE"), |
| 162 | cl::Hidden, cl::init(true)); |
| 163 | |
Evgeniy Stepanov | fac8403 | 2013-01-25 15:31:10 +0000 | [diff] [blame] | 164 | static cl::opt<bool> ClHandleICmpExact("msan-handle-icmp-exact", |
| 165 | cl::desc("exact handling of relational integer ICmp"), |
Evgeniy Stepanov | 6f85ef3 | 2013-01-28 11:42:28 +0000 | [diff] [blame] | 166 | cl::Hidden, cl::init(false)); |
Evgeniy Stepanov | fac8403 | 2013-01-25 15:31:10 +0000 | [diff] [blame] | 167 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 168 | // This flag controls whether we check the shadow of the address |
| 169 | // operand of load or store. Such bugs are very rare, since load from |
| 170 | // a garbage address typically results in SEGV, but still happen |
| 171 | // (e.g. only lower bits of address are garbage, or the access happens |
| 172 | // early at program startup where malloc-ed memory is more likely to |
| 173 | // be zeroed. As of 2012-08-28 this flag adds 20% slowdown. |
| 174 | static cl::opt<bool> ClCheckAccessAddress("msan-check-access-address", |
| 175 | cl::desc("report accesses through a pointer which has poisoned shadow"), |
| 176 | cl::Hidden, cl::init(true)); |
| 177 | |
| 178 | static cl::opt<bool> ClDumpStrictInstructions("msan-dump-strict-instructions", |
| 179 | cl::desc("print out instructions with default strict semantics"), |
| 180 | cl::Hidden, cl::init(false)); |
| 181 | |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 182 | static cl::opt<int> ClInstrumentationWithCallThreshold( |
| 183 | "msan-instrumentation-with-call-threshold", |
| 184 | cl::desc( |
| 185 | "If the function being instrumented requires more than " |
| 186 | "this number of checks and origin stores, use callbacks instead of " |
| 187 | "inline checks (-1 means never use callbacks)."), |
Evgeniy Stepanov | 3939f54 | 2014-04-21 15:04:05 +0000 | [diff] [blame] | 188 | cl::Hidden, cl::init(3500)); |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 189 | |
Evgeniy Stepanov | 37b8645 | 2013-09-19 15:22:35 +0000 | [diff] [blame] | 190 | // Experimental. Wraps all indirect calls in the instrumented code with |
| 191 | // a call to the given function. This is needed to assist the dynamic |
| 192 | // helper tool (MSanDR) to regain control on transition between instrumented and |
| 193 | // non-instrumented code. |
| 194 | static cl::opt<std::string> ClWrapIndirectCalls("msan-wrap-indirect-calls", |
| 195 | cl::desc("Wrap indirect calls with a given function"), |
| 196 | cl::Hidden); |
| 197 | |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 198 | static cl::opt<bool> ClWrapIndirectCallsFast("msan-wrap-indirect-calls-fast", |
| 199 | cl::desc("Do not wrap indirect calls with target in the same module"), |
| 200 | cl::Hidden, cl::init(true)); |
| 201 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 202 | namespace { |
| 203 | |
| 204 | /// \brief An instrumentation pass implementing detection of uninitialized |
| 205 | /// reads. |
| 206 | /// |
| 207 | /// MemorySanitizer: instrument the code in module to find |
| 208 | /// uninitialized reads. |
| 209 | class MemorySanitizer : public FunctionPass { |
Evgeniy Stepanov | 9b72e99 | 2012-12-14 13:48:31 +0000 | [diff] [blame] | 210 | public: |
Alexey Samsonov | 6d8bab8 | 2014-06-02 18:08:27 +0000 | [diff] [blame] | 211 | MemorySanitizer(int TrackOrigins = 0) |
Evgeniy Stepanov | 37b8645 | 2013-09-19 15:22:35 +0000 | [diff] [blame] | 212 | : FunctionPass(ID), |
Evgeniy Stepanov | 302964e | 2014-03-18 13:30:56 +0000 | [diff] [blame] | 213 | TrackOrigins(std::max(TrackOrigins, (int)ClTrackOrigins)), |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 214 | DL(nullptr), |
| 215 | WarningFn(nullptr), |
Evgeniy Stepanov | 37b8645 | 2013-09-19 15:22:35 +0000 | [diff] [blame] | 216 | WrapIndirectCalls(!ClWrapIndirectCalls.empty()) {} |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 217 | const char *getPassName() const override { return "MemorySanitizer"; } |
| 218 | bool runOnFunction(Function &F) override; |
| 219 | bool doInitialization(Module &M) override; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 220 | static char ID; // Pass identification, replacement for typeid. |
| 221 | |
Evgeniy Stepanov | 9b72e99 | 2012-12-14 13:48:31 +0000 | [diff] [blame] | 222 | private: |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 223 | void initializeCallbacks(Module &M); |
| 224 | |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 225 | /// \brief Track origins (allocation points) of uninitialized values. |
Evgeniy Stepanov | 302964e | 2014-03-18 13:30:56 +0000 | [diff] [blame] | 226 | int TrackOrigins; |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 227 | |
Rafael Espindola | aeff8a9 | 2014-02-24 23:12:18 +0000 | [diff] [blame] | 228 | const DataLayout *DL; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 229 | LLVMContext *C; |
| 230 | Type *IntptrTy; |
| 231 | Type *OriginTy; |
| 232 | /// \brief Thread-local shadow storage for function parameters. |
| 233 | GlobalVariable *ParamTLS; |
| 234 | /// \brief Thread-local origin storage for function parameters. |
| 235 | GlobalVariable *ParamOriginTLS; |
| 236 | /// \brief Thread-local shadow storage for function return value. |
| 237 | GlobalVariable *RetvalTLS; |
| 238 | /// \brief Thread-local origin storage for function return value. |
| 239 | GlobalVariable *RetvalOriginTLS; |
| 240 | /// \brief Thread-local shadow storage for in-register va_arg function |
| 241 | /// parameters (x86_64-specific). |
| 242 | GlobalVariable *VAArgTLS; |
| 243 | /// \brief Thread-local shadow storage for va_arg overflow area |
| 244 | /// (x86_64-specific). |
| 245 | GlobalVariable *VAArgOverflowSizeTLS; |
| 246 | /// \brief Thread-local space used to pass origin value to the UMR reporting |
| 247 | /// function. |
| 248 | GlobalVariable *OriginTLS; |
| 249 | |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 250 | GlobalVariable *MsandrModuleStart; |
| 251 | GlobalVariable *MsandrModuleEnd; |
| 252 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 253 | /// \brief The run-time callback to print a warning. |
| 254 | Value *WarningFn; |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 255 | // These arrays are indexed by log2(AccessSize). |
| 256 | Value *MaybeWarningFn[kNumberOfAccessSizes]; |
| 257 | Value *MaybeStoreOriginFn[kNumberOfAccessSizes]; |
| 258 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 259 | /// \brief Run-time helper that generates a new origin value for a stack |
| 260 | /// allocation. |
Evgeniy Stepanov | 0435ecd | 2013-09-13 12:54:49 +0000 | [diff] [blame] | 261 | Value *MsanSetAllocaOrigin4Fn; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 262 | /// \brief Run-time helper that poisons stack on function entry. |
| 263 | Value *MsanPoisonStackFn; |
Evgeniy Stepanov | 302964e | 2014-03-18 13:30:56 +0000 | [diff] [blame] | 264 | /// \brief Run-time helper that records a store (or any event) of an |
| 265 | /// uninitialized value and returns an updated origin id encoding this info. |
| 266 | Value *MsanChainOriginFn; |
Evgeniy Stepanov | 62b5db9 | 2012-11-29 12:49:04 +0000 | [diff] [blame] | 267 | /// \brief MSan runtime replacements for memmove, memcpy and memset. |
| 268 | Value *MemmoveFn, *MemcpyFn, *MemsetFn; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 269 | |
| 270 | /// \brief Address mask used in application-to-shadow address calculation. |
| 271 | /// ShadowAddr is computed as ApplicationAddr & ~ShadowMask. |
| 272 | uint64_t ShadowMask; |
| 273 | /// \brief Offset of the origin shadow from the "normal" shadow. |
| 274 | /// OriginAddr is computed as (ShadowAddr + OriginOffset) & ~3ULL |
| 275 | uint64_t OriginOffset; |
| 276 | /// \brief Branch weights for error reporting. |
| 277 | MDNode *ColdCallWeights; |
Evgeniy Stepanov | 4f220d9 | 2012-12-06 11:41:03 +0000 | [diff] [blame] | 278 | /// \brief Branch weights for origin store. |
| 279 | MDNode *OriginStoreWeights; |
Evgeniy Stepanov | 1d2da65 | 2012-11-29 12:30:18 +0000 | [diff] [blame] | 280 | /// \brief An empty volatile inline asm that prevents callback merge. |
| 281 | InlineAsm *EmptyAsm; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 282 | |
Evgeniy Stepanov | 37b8645 | 2013-09-19 15:22:35 +0000 | [diff] [blame] | 283 | bool WrapIndirectCalls; |
| 284 | /// \brief Run-time wrapper for indirect calls. |
| 285 | Value *IndirectCallWrapperFn; |
| 286 | // Argument and return type of IndirectCallWrapperFn: void (*f)(void). |
| 287 | Type *AnyFunctionPtrTy; |
| 288 | |
Evgeniy Stepanov | da0072b | 2012-11-29 13:12:03 +0000 | [diff] [blame] | 289 | friend struct MemorySanitizerVisitor; |
| 290 | friend struct VarArgAMD64Helper; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 291 | }; |
| 292 | } // namespace |
| 293 | |
| 294 | char MemorySanitizer::ID = 0; |
| 295 | INITIALIZE_PASS(MemorySanitizer, "msan", |
| 296 | "MemorySanitizer: detects uninitialized reads.", |
| 297 | false, false) |
| 298 | |
Alexey Samsonov | 6d8bab8 | 2014-06-02 18:08:27 +0000 | [diff] [blame] | 299 | FunctionPass *llvm::createMemorySanitizerPass(int TrackOrigins) { |
| 300 | return new MemorySanitizer(TrackOrigins); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /// \brief Create a non-const global initialized with the given string. |
| 304 | /// |
| 305 | /// Creates a writable global for Str so that we can pass it to the |
| 306 | /// run-time lib. Runtime uses first 4 bytes of the string to store the |
| 307 | /// frame ID, so the string needs to be mutable. |
| 308 | static GlobalVariable *createPrivateNonConstGlobalForString(Module &M, |
| 309 | StringRef Str) { |
| 310 | Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str); |
| 311 | return new GlobalVariable(M, StrConst->getType(), /*isConstant=*/false, |
| 312 | GlobalValue::PrivateLinkage, StrConst, ""); |
| 313 | } |
| 314 | |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 315 | |
| 316 | /// \brief Insert extern declaration of runtime-provided functions and globals. |
| 317 | void MemorySanitizer::initializeCallbacks(Module &M) { |
| 318 | // Only do this once. |
| 319 | if (WarningFn) |
| 320 | return; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 321 | |
| 322 | IRBuilder<> IRB(*C); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 323 | // Create the callback. |
| 324 | // FIXME: this function should have "Cold" calling conv, |
| 325 | // which is not yet implemented. |
| 326 | StringRef WarningFnName = ClKeepGoing ? "__msan_warning" |
| 327 | : "__msan_warning_noreturn"; |
| 328 | WarningFn = M.getOrInsertFunction(WarningFnName, IRB.getVoidTy(), NULL); |
| 329 | |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 330 | for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes; |
| 331 | AccessSizeIndex++) { |
| 332 | unsigned AccessSize = 1 << AccessSizeIndex; |
| 333 | std::string FunctionName = "__msan_maybe_warning_" + itostr(AccessSize); |
| 334 | MaybeWarningFn[AccessSizeIndex] = M.getOrInsertFunction( |
| 335 | FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8), |
| 336 | IRB.getInt32Ty(), NULL); |
| 337 | |
| 338 | FunctionName = "__msan_maybe_store_origin_" + itostr(AccessSize); |
| 339 | MaybeStoreOriginFn[AccessSizeIndex] = M.getOrInsertFunction( |
| 340 | FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8), |
| 341 | IRB.getInt8PtrTy(), IRB.getInt32Ty(), NULL); |
| 342 | } |
| 343 | |
Evgeniy Stepanov | 0435ecd | 2013-09-13 12:54:49 +0000 | [diff] [blame] | 344 | MsanSetAllocaOrigin4Fn = M.getOrInsertFunction( |
| 345 | "__msan_set_alloca_origin4", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy, |
| 346 | IRB.getInt8PtrTy(), IntptrTy, NULL); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 347 | MsanPoisonStackFn = M.getOrInsertFunction( |
| 348 | "__msan_poison_stack", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy, NULL); |
Evgeniy Stepanov | 302964e | 2014-03-18 13:30:56 +0000 | [diff] [blame] | 349 | MsanChainOriginFn = M.getOrInsertFunction( |
| 350 | "__msan_chain_origin", IRB.getInt32Ty(), IRB.getInt32Ty(), NULL); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 351 | MemmoveFn = M.getOrInsertFunction( |
Evgeniy Stepanov | 9b72e99 | 2012-12-14 13:48:31 +0000 | [diff] [blame] | 352 | "__msan_memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), |
| 353 | IRB.getInt8PtrTy(), IntptrTy, NULL); |
Evgeniy Stepanov | 62b5db9 | 2012-11-29 12:49:04 +0000 | [diff] [blame] | 354 | MemcpyFn = M.getOrInsertFunction( |
| 355 | "__msan_memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), |
| 356 | IntptrTy, NULL); |
| 357 | MemsetFn = M.getOrInsertFunction( |
| 358 | "__msan_memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(), |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 359 | IntptrTy, NULL); |
| 360 | |
| 361 | // Create globals. |
| 362 | RetvalTLS = new GlobalVariable( |
Evgeniy Stepanov | 35eb265 | 2014-10-22 00:12:40 +0000 | [diff] [blame^] | 363 | M, ArrayType::get(IRB.getInt64Ty(), kRetvalTLSSize / 8), false, |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 364 | GlobalVariable::ExternalLinkage, nullptr, "__msan_retval_tls", nullptr, |
Evgeniy Stepanov | 1e76432 | 2013-05-16 09:14:05 +0000 | [diff] [blame] | 365 | GlobalVariable::InitialExecTLSModel); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 366 | RetvalOriginTLS = new GlobalVariable( |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 367 | M, OriginTy, false, GlobalVariable::ExternalLinkage, nullptr, |
| 368 | "__msan_retval_origin_tls", nullptr, GlobalVariable::InitialExecTLSModel); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 369 | |
| 370 | ParamTLS = new GlobalVariable( |
Evgeniy Stepanov | 35eb265 | 2014-10-22 00:12:40 +0000 | [diff] [blame^] | 371 | M, ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), false, |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 372 | GlobalVariable::ExternalLinkage, nullptr, "__msan_param_tls", nullptr, |
Evgeniy Stepanov | 1e76432 | 2013-05-16 09:14:05 +0000 | [diff] [blame] | 373 | GlobalVariable::InitialExecTLSModel); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 374 | ParamOriginTLS = new GlobalVariable( |
Evgeniy Stepanov | 35eb265 | 2014-10-22 00:12:40 +0000 | [diff] [blame^] | 375 | M, ArrayType::get(OriginTy, kParamTLSSize / 4), false, |
| 376 | GlobalVariable::ExternalLinkage, nullptr, "__msan_param_origin_tls", |
| 377 | nullptr, GlobalVariable::InitialExecTLSModel); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 378 | |
| 379 | VAArgTLS = new GlobalVariable( |
Evgeniy Stepanov | 35eb265 | 2014-10-22 00:12:40 +0000 | [diff] [blame^] | 380 | M, ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), false, |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 381 | GlobalVariable::ExternalLinkage, nullptr, "__msan_va_arg_tls", nullptr, |
Evgeniy Stepanov | 1e76432 | 2013-05-16 09:14:05 +0000 | [diff] [blame] | 382 | GlobalVariable::InitialExecTLSModel); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 383 | VAArgOverflowSizeTLS = new GlobalVariable( |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 384 | M, IRB.getInt64Ty(), false, GlobalVariable::ExternalLinkage, nullptr, |
| 385 | "__msan_va_arg_overflow_size_tls", nullptr, |
Evgeniy Stepanov | 1e76432 | 2013-05-16 09:14:05 +0000 | [diff] [blame] | 386 | GlobalVariable::InitialExecTLSModel); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 387 | OriginTLS = new GlobalVariable( |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 388 | M, IRB.getInt32Ty(), false, GlobalVariable::ExternalLinkage, nullptr, |
| 389 | "__msan_origin_tls", nullptr, GlobalVariable::InitialExecTLSModel); |
Evgeniy Stepanov | 1d2da65 | 2012-11-29 12:30:18 +0000 | [diff] [blame] | 390 | |
| 391 | // We insert an empty inline asm after __msan_report* to avoid callback merge. |
| 392 | EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), |
| 393 | StringRef(""), StringRef(""), |
| 394 | /*hasSideEffects=*/true); |
Evgeniy Stepanov | 37b8645 | 2013-09-19 15:22:35 +0000 | [diff] [blame] | 395 | |
| 396 | if (WrapIndirectCalls) { |
| 397 | AnyFunctionPtrTy = |
| 398 | PointerType::getUnqual(FunctionType::get(IRB.getVoidTy(), false)); |
| 399 | IndirectCallWrapperFn = M.getOrInsertFunction( |
| 400 | ClWrapIndirectCalls, AnyFunctionPtrTy, AnyFunctionPtrTy, NULL); |
| 401 | } |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 402 | |
Evgeniy Stepanov | c14fc42 | 2014-05-07 14:10:51 +0000 | [diff] [blame] | 403 | if (WrapIndirectCalls && ClWrapIndirectCallsFast) { |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 404 | MsandrModuleStart = new GlobalVariable( |
| 405 | M, IRB.getInt32Ty(), false, GlobalValue::ExternalLinkage, |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 406 | nullptr, "__executable_start"); |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 407 | MsandrModuleStart->setVisibility(GlobalVariable::HiddenVisibility); |
| 408 | MsandrModuleEnd = new GlobalVariable( |
| 409 | M, IRB.getInt32Ty(), false, GlobalValue::ExternalLinkage, |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 410 | nullptr, "_end"); |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 411 | MsandrModuleEnd->setVisibility(GlobalVariable::HiddenVisibility); |
| 412 | } |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /// \brief Module-level initialization. |
| 416 | /// |
| 417 | /// inserts a call to __msan_init to the module's constructor list. |
| 418 | bool MemorySanitizer::doInitialization(Module &M) { |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 419 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
| 420 | if (!DLP) |
Evgeniy Stepanov | 119cb2e | 2014-04-23 12:51:32 +0000 | [diff] [blame] | 421 | report_fatal_error("data layout missing"); |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 422 | DL = &DLP->getDataLayout(); |
| 423 | |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 424 | C = &(M.getContext()); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 425 | unsigned PtrSize = DL->getPointerSizeInBits(/* AddressSpace */0); |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 426 | switch (PtrSize) { |
| 427 | case 64: |
| 428 | ShadowMask = kShadowMask64; |
| 429 | OriginOffset = kOriginOffset64; |
| 430 | break; |
| 431 | case 32: |
| 432 | ShadowMask = kShadowMask32; |
| 433 | OriginOffset = kOriginOffset32; |
| 434 | break; |
| 435 | default: |
| 436 | report_fatal_error("unsupported pointer size"); |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | IRBuilder<> IRB(*C); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 441 | IntptrTy = IRB.getIntPtrTy(DL); |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 442 | OriginTy = IRB.getInt32Ty(); |
| 443 | |
| 444 | ColdCallWeights = MDBuilder(*C).createBranchWeights(1, 1000); |
Evgeniy Stepanov | 4f220d9 | 2012-12-06 11:41:03 +0000 | [diff] [blame] | 445 | OriginStoreWeights = MDBuilder(*C).createBranchWeights(1, 1000); |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 446 | |
| 447 | // Insert a call to __msan_init/__msan_track_origins into the module's CTORs. |
| 448 | appendToGlobalCtors(M, cast<Function>(M.getOrInsertFunction( |
| 449 | "__msan_init", IRB.getVoidTy(), NULL)), 0); |
| 450 | |
Evgeniy Stepanov | 888385e | 2013-05-31 12:04:29 +0000 | [diff] [blame] | 451 | if (TrackOrigins) |
| 452 | new GlobalVariable(M, IRB.getInt32Ty(), true, GlobalValue::WeakODRLinkage, |
| 453 | IRB.getInt32(TrackOrigins), "__msan_track_origins"); |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 454 | |
Evgeniy Stepanov | 888385e | 2013-05-31 12:04:29 +0000 | [diff] [blame] | 455 | if (ClKeepGoing) |
| 456 | new GlobalVariable(M, IRB.getInt32Ty(), true, GlobalValue::WeakODRLinkage, |
| 457 | IRB.getInt32(ClKeepGoing), "__msan_keep_going"); |
Evgeniy Stepanov | dcf6bcb | 2013-01-22 13:26:53 +0000 | [diff] [blame] | 458 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 459 | return true; |
| 460 | } |
| 461 | |
| 462 | namespace { |
| 463 | |
| 464 | /// \brief A helper class that handles instrumentation of VarArg |
| 465 | /// functions on a particular platform. |
| 466 | /// |
| 467 | /// Implementations are expected to insert the instrumentation |
| 468 | /// necessary to propagate argument shadow through VarArg function |
| 469 | /// calls. Visit* methods are called during an InstVisitor pass over |
| 470 | /// the function, and should avoid creating new basic blocks. A new |
| 471 | /// instance of this class is created for each instrumented function. |
| 472 | struct VarArgHelper { |
| 473 | /// \brief Visit a CallSite. |
| 474 | virtual void visitCallSite(CallSite &CS, IRBuilder<> &IRB) = 0; |
| 475 | |
| 476 | /// \brief Visit a va_start call. |
| 477 | virtual void visitVAStartInst(VAStartInst &I) = 0; |
| 478 | |
| 479 | /// \brief Visit a va_copy call. |
| 480 | virtual void visitVACopyInst(VACopyInst &I) = 0; |
| 481 | |
| 482 | /// \brief Finalize function instrumentation. |
| 483 | /// |
| 484 | /// This method is called after visiting all interesting (see above) |
| 485 | /// instructions in a function. |
| 486 | virtual void finalizeInstrumentation() = 0; |
Evgeniy Stepanov | da0072b | 2012-11-29 13:12:03 +0000 | [diff] [blame] | 487 | |
| 488 | virtual ~VarArgHelper() {} |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | struct MemorySanitizerVisitor; |
| 492 | |
| 493 | VarArgHelper* |
| 494 | CreateVarArgHelper(Function &Func, MemorySanitizer &Msan, |
| 495 | MemorySanitizerVisitor &Visitor); |
| 496 | |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 497 | unsigned TypeSizeToSizeIndex(unsigned TypeSize) { |
| 498 | if (TypeSize <= 8) return 0; |
| 499 | return Log2_32_Ceil(TypeSize / 8); |
| 500 | } |
| 501 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 502 | /// This class does all the work for a given function. Store and Load |
| 503 | /// instructions store and load corresponding shadow and origin |
| 504 | /// values. Most instructions propagate shadow from arguments to their |
| 505 | /// return values. Certain instructions (most importantly, BranchInst) |
| 506 | /// test their argument shadow and print reports (with a runtime call) if it's |
| 507 | /// non-zero. |
| 508 | struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { |
| 509 | Function &F; |
| 510 | MemorySanitizer &MS; |
| 511 | SmallVector<PHINode *, 16> ShadowPHINodes, OriginPHINodes; |
| 512 | ValueMap<Value*, Value*> ShadowMap, OriginMap; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 513 | std::unique_ptr<VarArgHelper> VAHelper; |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 514 | |
| 515 | // The following flags disable parts of MSan instrumentation based on |
| 516 | // blacklist contents and command-line options. |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 517 | bool InsertChecks; |
Evgeniy Stepanov | 174242c | 2014-07-03 11:56:30 +0000 | [diff] [blame] | 518 | bool PropagateShadow; |
Evgeniy Stepanov | dc6d7eb | 2013-07-03 14:39:14 +0000 | [diff] [blame] | 519 | bool PoisonStack; |
| 520 | bool PoisonUndef; |
Evgeniy Stepanov | 604293f | 2013-09-16 13:24:32 +0000 | [diff] [blame] | 521 | bool CheckReturnValue; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 522 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 523 | struct ShadowOriginAndInsertPoint { |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 524 | Value *Shadow; |
| 525 | Value *Origin; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 526 | Instruction *OrigIns; |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 527 | ShadowOriginAndInsertPoint(Value *S, Value *O, Instruction *I) |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 528 | : Shadow(S), Origin(O), OrigIns(I) { } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 529 | }; |
| 530 | SmallVector<ShadowOriginAndInsertPoint, 16> InstrumentationList; |
Evgeniy Stepanov | 4f220d9 | 2012-12-06 11:41:03 +0000 | [diff] [blame] | 531 | SmallVector<Instruction*, 16> StoreList; |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 532 | SmallVector<CallSite, 16> IndirectCallList; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 533 | |
| 534 | MemorySanitizerVisitor(Function &F, MemorySanitizer &MS) |
Evgeniy Stepanov | 00062b4 | 2013-02-28 11:25:14 +0000 | [diff] [blame] | 535 | : F(F), MS(MS), VAHelper(CreateVarArgHelper(F, MS, *this)) { |
Alexey Samsonov | 6d8bab8 | 2014-06-02 18:08:27 +0000 | [diff] [blame] | 536 | bool SanitizeFunction = F.getAttributes().hasAttribute( |
| 537 | AttributeSet::FunctionIndex, Attribute::SanitizeMemory); |
Evgeniy Stepanov | dc6d7eb | 2013-07-03 14:39:14 +0000 | [diff] [blame] | 538 | InsertChecks = SanitizeFunction; |
Evgeniy Stepanov | 174242c | 2014-07-03 11:56:30 +0000 | [diff] [blame] | 539 | PropagateShadow = SanitizeFunction; |
Evgeniy Stepanov | dc6d7eb | 2013-07-03 14:39:14 +0000 | [diff] [blame] | 540 | PoisonStack = SanitizeFunction && ClPoisonStack; |
| 541 | PoisonUndef = SanitizeFunction && ClPoisonUndef; |
Evgeniy Stepanov | 604293f | 2013-09-16 13:24:32 +0000 | [diff] [blame] | 542 | // FIXME: Consider using SpecialCaseList to specify a list of functions that |
| 543 | // must always return fully initialized values. For now, we hardcode "main". |
| 544 | CheckReturnValue = SanitizeFunction && (F.getName() == "main"); |
Evgeniy Stepanov | 00062b4 | 2013-02-28 11:25:14 +0000 | [diff] [blame] | 545 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 546 | DEBUG(if (!InsertChecks) |
Evgeniy Stepanov | 00062b4 | 2013-02-28 11:25:14 +0000 | [diff] [blame] | 547 | dbgs() << "MemorySanitizer is not inserting checks into '" |
| 548 | << F.getName() << "'\n"); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Evgeniy Stepanov | 302964e | 2014-03-18 13:30:56 +0000 | [diff] [blame] | 551 | Value *updateOrigin(Value *V, IRBuilder<> &IRB) { |
| 552 | if (MS.TrackOrigins <= 1) return V; |
| 553 | return IRB.CreateCall(MS.MsanChainOriginFn, V); |
| 554 | } |
| 555 | |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 556 | void storeOrigin(IRBuilder<> &IRB, Value *Addr, Value *Shadow, Value *Origin, |
| 557 | unsigned Alignment, bool AsCall) { |
| 558 | if (isa<StructType>(Shadow->getType())) { |
| 559 | IRB.CreateAlignedStore(updateOrigin(Origin, IRB), getOriginPtr(Addr, IRB), |
| 560 | Alignment); |
| 561 | } else { |
| 562 | Value *ConvertedShadow = convertToShadowTyNoVec(Shadow, IRB); |
| 563 | // TODO(eugenis): handle non-zero constant shadow by inserting an |
| 564 | // unconditional check (can not simply fail compilation as this could |
| 565 | // be in the dead code). |
| 566 | if (isa<Constant>(ConvertedShadow)) return; |
| 567 | unsigned TypeSizeInBits = |
| 568 | MS.DL->getTypeSizeInBits(ConvertedShadow->getType()); |
| 569 | unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits); |
| 570 | if (AsCall && SizeIndex < kNumberOfAccessSizes) { |
| 571 | Value *Fn = MS.MaybeStoreOriginFn[SizeIndex]; |
| 572 | Value *ConvertedShadow2 = IRB.CreateZExt( |
| 573 | ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex))); |
| 574 | IRB.CreateCall3(Fn, ConvertedShadow2, |
| 575 | IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()), |
Evgeniy Stepanov | b163f02 | 2014-06-25 14:41:57 +0000 | [diff] [blame] | 576 | Origin); |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 577 | } else { |
| 578 | Value *Cmp = IRB.CreateICmpNE( |
| 579 | ConvertedShadow, getCleanShadow(ConvertedShadow), "_mscmp"); |
| 580 | Instruction *CheckTerm = SplitBlockAndInsertIfThen( |
| 581 | Cmp, IRB.GetInsertPoint(), false, MS.OriginStoreWeights); |
| 582 | IRBuilder<> IRBNew(CheckTerm); |
| 583 | IRBNew.CreateAlignedStore(updateOrigin(Origin, IRBNew), |
| 584 | getOriginPtr(Addr, IRBNew), Alignment); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | void materializeStores(bool InstrumentWithCalls) { |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 590 | for (auto Inst : StoreList) { |
| 591 | StoreInst &SI = *dyn_cast<StoreInst>(Inst); |
Evgeniy Stepanov | 4f220d9 | 2012-12-06 11:41:03 +0000 | [diff] [blame] | 592 | |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 593 | IRBuilder<> IRB(&SI); |
| 594 | Value *Val = SI.getValueOperand(); |
| 595 | Value *Addr = SI.getPointerOperand(); |
| 596 | Value *Shadow = SI.isAtomic() ? getCleanShadow(Val) : getShadow(Val); |
Evgeniy Stepanov | 4f220d9 | 2012-12-06 11:41:03 +0000 | [diff] [blame] | 597 | Value *ShadowPtr = getShadowPtr(Addr, Shadow->getType(), IRB); |
| 598 | |
Evgeniy Stepanov | 9b72e99 | 2012-12-14 13:48:31 +0000 | [diff] [blame] | 599 | StoreInst *NewSI = |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 600 | IRB.CreateAlignedStore(Shadow, ShadowPtr, SI.getAlignment()); |
Evgeniy Stepanov | 4f220d9 | 2012-12-06 11:41:03 +0000 | [diff] [blame] | 601 | DEBUG(dbgs() << " STORE: " << *NewSI << "\n"); |
NAKAMURA Takumi | e0b1b46 | 2012-12-06 13:38:00 +0000 | [diff] [blame] | 602 | (void)NewSI; |
Evgeniy Stepanov | c441559 | 2013-01-22 12:30:52 +0000 | [diff] [blame] | 603 | |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 604 | if (ClCheckAccessAddress) insertShadowCheck(Addr, &SI); |
Evgeniy Stepanov | 4f220d9 | 2012-12-06 11:41:03 +0000 | [diff] [blame] | 605 | |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 606 | if (SI.isAtomic()) SI.setOrdering(addReleaseOrdering(SI.getOrdering())); |
Evgeniy Stepanov | 5522a70 | 2013-09-24 11:20:27 +0000 | [diff] [blame] | 607 | |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 608 | if (MS.TrackOrigins) { |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 609 | unsigned Alignment = std::max(kMinOriginAlignment, SI.getAlignment()); |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 610 | storeOrigin(IRB, Addr, Shadow, getOrigin(Val), Alignment, |
| 611 | InstrumentWithCalls); |
Evgeniy Stepanov | 4f220d9 | 2012-12-06 11:41:03 +0000 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 616 | void materializeOneCheck(Instruction *OrigIns, Value *Shadow, Value *Origin, |
| 617 | bool AsCall) { |
| 618 | IRBuilder<> IRB(OrigIns); |
| 619 | DEBUG(dbgs() << " SHAD0 : " << *Shadow << "\n"); |
| 620 | Value *ConvertedShadow = convertToShadowTyNoVec(Shadow, IRB); |
| 621 | DEBUG(dbgs() << " SHAD1 : " << *ConvertedShadow << "\n"); |
| 622 | // See the comment in materializeStores(). |
| 623 | if (isa<Constant>(ConvertedShadow)) return; |
| 624 | unsigned TypeSizeInBits = |
| 625 | MS.DL->getTypeSizeInBits(ConvertedShadow->getType()); |
| 626 | unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits); |
| 627 | if (AsCall && SizeIndex < kNumberOfAccessSizes) { |
| 628 | Value *Fn = MS.MaybeWarningFn[SizeIndex]; |
| 629 | Value *ConvertedShadow2 = |
| 630 | IRB.CreateZExt(ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex))); |
| 631 | IRB.CreateCall2(Fn, ConvertedShadow2, MS.TrackOrigins && Origin |
| 632 | ? Origin |
| 633 | : (Value *)IRB.getInt32(0)); |
| 634 | } else { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 635 | Value *Cmp = IRB.CreateICmpNE(ConvertedShadow, |
| 636 | getCleanShadow(ConvertedShadow), "_mscmp"); |
Evgeniy Stepanov | a9164e9 | 2013-12-19 13:29:56 +0000 | [diff] [blame] | 637 | Instruction *CheckTerm = SplitBlockAndInsertIfThen( |
| 638 | Cmp, OrigIns, |
| 639 | /* Unreachable */ !ClKeepGoing, MS.ColdCallWeights); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 640 | |
| 641 | IRB.SetInsertPoint(CheckTerm); |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 642 | if (MS.TrackOrigins) { |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 643 | IRB.CreateStore(Origin ? (Value *)Origin : (Value *)IRB.getInt32(0), |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 644 | MS.OriginTLS); |
| 645 | } |
Evgeniy Stepanov | 2275a01 | 2014-03-19 12:56:38 +0000 | [diff] [blame] | 646 | IRB.CreateCall(MS.WarningFn); |
Evgeniy Stepanov | 1d2da65 | 2012-11-29 12:30:18 +0000 | [diff] [blame] | 647 | IRB.CreateCall(MS.EmptyAsm); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 648 | DEBUG(dbgs() << " CHECK: " << *Cmp << "\n"); |
| 649 | } |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | void materializeChecks(bool InstrumentWithCalls) { |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 653 | for (const auto &ShadowData : InstrumentationList) { |
| 654 | Instruction *OrigIns = ShadowData.OrigIns; |
| 655 | Value *Shadow = ShadowData.Shadow; |
| 656 | Value *Origin = ShadowData.Origin; |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 657 | materializeOneCheck(OrigIns, Shadow, Origin, InstrumentWithCalls); |
| 658 | } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 659 | DEBUG(dbgs() << "DONE:\n" << F); |
| 660 | } |
| 661 | |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 662 | void materializeIndirectCalls() { |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 663 | for (auto &CS : IndirectCallList) { |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 664 | Instruction *I = CS.getInstruction(); |
| 665 | BasicBlock *B = I->getParent(); |
| 666 | IRBuilder<> IRB(I); |
| 667 | Value *Fn0 = CS.getCalledValue(); |
| 668 | Value *Fn = IRB.CreateBitCast(Fn0, MS.AnyFunctionPtrTy); |
| 669 | |
| 670 | if (ClWrapIndirectCallsFast) { |
| 671 | // Check that call target is inside this module limits. |
| 672 | Value *Start = |
| 673 | IRB.CreateBitCast(MS.MsandrModuleStart, MS.AnyFunctionPtrTy); |
| 674 | Value *End = IRB.CreateBitCast(MS.MsandrModuleEnd, MS.AnyFunctionPtrTy); |
| 675 | |
| 676 | Value *NotInThisModule = IRB.CreateOr(IRB.CreateICmpULT(Fn, Start), |
| 677 | IRB.CreateICmpUGE(Fn, End)); |
| 678 | |
| 679 | PHINode *NewFnPhi = |
| 680 | IRB.CreatePHI(Fn0->getType(), 2, "msandr.indirect_target"); |
| 681 | |
| 682 | Instruction *CheckTerm = SplitBlockAndInsertIfThen( |
Evgeniy Stepanov | a9164e9 | 2013-12-19 13:29:56 +0000 | [diff] [blame] | 683 | NotInThisModule, NewFnPhi, |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 684 | /* Unreachable */ false, MS.ColdCallWeights); |
| 685 | |
| 686 | IRB.SetInsertPoint(CheckTerm); |
| 687 | // Slow path: call wrapper function to possibly transform the call |
| 688 | // target. |
| 689 | Value *NewFn = IRB.CreateBitCast( |
| 690 | IRB.CreateCall(MS.IndirectCallWrapperFn, Fn), Fn0->getType()); |
| 691 | |
| 692 | NewFnPhi->addIncoming(Fn0, B); |
| 693 | NewFnPhi->addIncoming(NewFn, dyn_cast<Instruction>(NewFn)->getParent()); |
| 694 | CS.setCalledFunction(NewFnPhi); |
| 695 | } else { |
| 696 | Value *NewFn = IRB.CreateBitCast( |
| 697 | IRB.CreateCall(MS.IndirectCallWrapperFn, Fn), Fn0->getType()); |
| 698 | CS.setCalledFunction(NewFn); |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 703 | /// \brief Add MemorySanitizer instrumentation to a function. |
| 704 | bool runOnFunction() { |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 705 | MS.initializeCallbacks(*F.getParent()); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 706 | if (!MS.DL) return false; |
Evgeniy Stepanov | 4fbc0d08 | 2012-12-21 11:18:49 +0000 | [diff] [blame] | 707 | |
| 708 | // In the presence of unreachable blocks, we may see Phi nodes with |
| 709 | // incoming nodes from such blocks. Since InstVisitor skips unreachable |
| 710 | // blocks, such nodes will not have any shadow value associated with them. |
| 711 | // It's easier to remove unreachable blocks than deal with missing shadow. |
| 712 | removeUnreachableBlocks(F); |
| 713 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 714 | // Iterate all BBs in depth-first order and create shadow instructions |
| 715 | // for all instructions (where applicable). |
| 716 | // For PHI nodes we create dummy shadow PHIs which will be finalized later. |
David Blaikie | ceec2bd | 2014-04-11 01:50:01 +0000 | [diff] [blame] | 717 | for (BasicBlock *BB : depth_first(&F.getEntryBlock())) |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 718 | visit(*BB); |
David Blaikie | ceec2bd | 2014-04-11 01:50:01 +0000 | [diff] [blame] | 719 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 720 | |
| 721 | // Finalize PHI nodes. |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 722 | for (PHINode *PN : ShadowPHINodes) { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 723 | PHINode *PNS = cast<PHINode>(getShadow(PN)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 724 | PHINode *PNO = MS.TrackOrigins ? cast<PHINode>(getOrigin(PN)) : nullptr; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 725 | size_t NumValues = PN->getNumIncomingValues(); |
| 726 | for (size_t v = 0; v < NumValues; v++) { |
| 727 | PNS->addIncoming(getShadow(PN, v), PN->getIncomingBlock(v)); |
Evgeniy Stepanov | 174242c | 2014-07-03 11:56:30 +0000 | [diff] [blame] | 728 | if (PNO) PNO->addIncoming(getOrigin(PN, v), PN->getIncomingBlock(v)); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
| 732 | VAHelper->finalizeInstrumentation(); |
| 733 | |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 734 | bool InstrumentWithCalls = ClInstrumentationWithCallThreshold >= 0 && |
| 735 | InstrumentationList.size() + StoreList.size() > |
| 736 | (unsigned)ClInstrumentationWithCallThreshold; |
| 737 | |
Evgeniy Stepanov | 4f220d9 | 2012-12-06 11:41:03 +0000 | [diff] [blame] | 738 | // Delayed instrumentation of StoreInst. |
Evgeniy Stepanov | 47ac9ba | 2012-12-06 11:58:59 +0000 | [diff] [blame] | 739 | // This may add new checks to be inserted later. |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 740 | materializeStores(InstrumentWithCalls); |
Evgeniy Stepanov | 4f220d9 | 2012-12-06 11:41:03 +0000 | [diff] [blame] | 741 | |
| 742 | // Insert shadow value checks. |
Evgeniy Stepanov | 65120ec | 2014-04-18 12:17:20 +0000 | [diff] [blame] | 743 | materializeChecks(InstrumentWithCalls); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 744 | |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 745 | // Wrap indirect calls. |
| 746 | materializeIndirectCalls(); |
| 747 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 748 | return true; |
| 749 | } |
| 750 | |
| 751 | /// \brief Compute the shadow type that corresponds to a given Value. |
| 752 | Type *getShadowTy(Value *V) { |
| 753 | return getShadowTy(V->getType()); |
| 754 | } |
| 755 | |
| 756 | /// \brief Compute the shadow type that corresponds to a given Type. |
| 757 | Type *getShadowTy(Type *OrigTy) { |
| 758 | if (!OrigTy->isSized()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 759 | return nullptr; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 760 | } |
| 761 | // For integer type, shadow is the same as the original type. |
| 762 | // This may return weird-sized types like i1. |
| 763 | if (IntegerType *IT = dyn_cast<IntegerType>(OrigTy)) |
| 764 | return IT; |
Evgeniy Stepanov | f19c086 | 2012-12-25 16:04:38 +0000 | [diff] [blame] | 765 | if (VectorType *VT = dyn_cast<VectorType>(OrigTy)) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 766 | uint32_t EltSize = MS.DL->getTypeSizeInBits(VT->getElementType()); |
Evgeniy Stepanov | f19c086 | 2012-12-25 16:04:38 +0000 | [diff] [blame] | 767 | return VectorType::get(IntegerType::get(*MS.C, EltSize), |
| 768 | VT->getNumElements()); |
| 769 | } |
Evgeniy Stepanov | 5997feb | 2014-07-31 11:02:27 +0000 | [diff] [blame] | 770 | if (ArrayType *AT = dyn_cast<ArrayType>(OrigTy)) { |
| 771 | return ArrayType::get(getShadowTy(AT->getElementType()), |
| 772 | AT->getNumElements()); |
| 773 | } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 774 | if (StructType *ST = dyn_cast<StructType>(OrigTy)) { |
| 775 | SmallVector<Type*, 4> Elements; |
| 776 | for (unsigned i = 0, n = ST->getNumElements(); i < n; i++) |
| 777 | Elements.push_back(getShadowTy(ST->getElementType(i))); |
| 778 | StructType *Res = StructType::get(*MS.C, Elements, ST->isPacked()); |
| 779 | DEBUG(dbgs() << "getShadowTy: " << *ST << " ===> " << *Res << "\n"); |
| 780 | return Res; |
| 781 | } |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 782 | uint32_t TypeSize = MS.DL->getTypeSizeInBits(OrigTy); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 783 | return IntegerType::get(*MS.C, TypeSize); |
| 784 | } |
| 785 | |
| 786 | /// \brief Flatten a vector type. |
| 787 | Type *getShadowTyNoVec(Type *ty) { |
| 788 | if (VectorType *vt = dyn_cast<VectorType>(ty)) |
| 789 | return IntegerType::get(*MS.C, vt->getBitWidth()); |
| 790 | return ty; |
| 791 | } |
| 792 | |
| 793 | /// \brief Convert a shadow value to it's flattened variant. |
| 794 | Value *convertToShadowTyNoVec(Value *V, IRBuilder<> &IRB) { |
| 795 | Type *Ty = V->getType(); |
| 796 | Type *NoVecTy = getShadowTyNoVec(Ty); |
| 797 | if (Ty == NoVecTy) return V; |
| 798 | return IRB.CreateBitCast(V, NoVecTy); |
| 799 | } |
| 800 | |
| 801 | /// \brief Compute the shadow address that corresponds to a given application |
| 802 | /// address. |
| 803 | /// |
| 804 | /// Shadow = Addr & ~ShadowMask. |
| 805 | Value *getShadowPtr(Value *Addr, Type *ShadowTy, |
| 806 | IRBuilder<> &IRB) { |
| 807 | Value *ShadowLong = |
| 808 | IRB.CreateAnd(IRB.CreatePointerCast(Addr, MS.IntptrTy), |
| 809 | ConstantInt::get(MS.IntptrTy, ~MS.ShadowMask)); |
| 810 | return IRB.CreateIntToPtr(ShadowLong, PointerType::get(ShadowTy, 0)); |
| 811 | } |
| 812 | |
| 813 | /// \brief Compute the origin address that corresponds to a given application |
| 814 | /// address. |
| 815 | /// |
| 816 | /// OriginAddr = (ShadowAddr + OriginOffset) & ~3ULL |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 817 | Value *getOriginPtr(Value *Addr, IRBuilder<> &IRB) { |
| 818 | Value *ShadowLong = |
| 819 | IRB.CreateAnd(IRB.CreatePointerCast(Addr, MS.IntptrTy), |
Evgeniy Stepanov | 62ba611 | 2012-11-29 13:43:05 +0000 | [diff] [blame] | 820 | ConstantInt::get(MS.IntptrTy, ~MS.ShadowMask)); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 821 | Value *Add = |
| 822 | IRB.CreateAdd(ShadowLong, |
| 823 | ConstantInt::get(MS.IntptrTy, MS.OriginOffset)); |
Evgeniy Stepanov | 62ba611 | 2012-11-29 13:43:05 +0000 | [diff] [blame] | 824 | Value *SecondAnd = |
| 825 | IRB.CreateAnd(Add, ConstantInt::get(MS.IntptrTy, ~3ULL)); |
| 826 | return IRB.CreateIntToPtr(SecondAnd, PointerType::get(IRB.getInt32Ty(), 0)); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /// \brief Compute the shadow address for a given function argument. |
| 830 | /// |
| 831 | /// Shadow = ParamTLS+ArgOffset. |
| 832 | Value *getShadowPtrForArgument(Value *A, IRBuilder<> &IRB, |
| 833 | int ArgOffset) { |
| 834 | Value *Base = IRB.CreatePointerCast(MS.ParamTLS, MS.IntptrTy); |
| 835 | Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset)); |
| 836 | return IRB.CreateIntToPtr(Base, PointerType::get(getShadowTy(A), 0), |
| 837 | "_msarg"); |
| 838 | } |
| 839 | |
| 840 | /// \brief Compute the origin address for a given function argument. |
| 841 | Value *getOriginPtrForArgument(Value *A, IRBuilder<> &IRB, |
| 842 | int ArgOffset) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 843 | if (!MS.TrackOrigins) return nullptr; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 844 | Value *Base = IRB.CreatePointerCast(MS.ParamOriginTLS, MS.IntptrTy); |
| 845 | Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset)); |
| 846 | return IRB.CreateIntToPtr(Base, PointerType::get(MS.OriginTy, 0), |
| 847 | "_msarg_o"); |
| 848 | } |
| 849 | |
| 850 | /// \brief Compute the shadow address for a retval. |
| 851 | Value *getShadowPtrForRetval(Value *A, IRBuilder<> &IRB) { |
| 852 | Value *Base = IRB.CreatePointerCast(MS.RetvalTLS, MS.IntptrTy); |
| 853 | return IRB.CreateIntToPtr(Base, PointerType::get(getShadowTy(A), 0), |
| 854 | "_msret"); |
| 855 | } |
| 856 | |
| 857 | /// \brief Compute the origin address for a retval. |
| 858 | Value *getOriginPtrForRetval(IRBuilder<> &IRB) { |
| 859 | // We keep a single origin for the entire retval. Might be too optimistic. |
| 860 | return MS.RetvalOriginTLS; |
| 861 | } |
| 862 | |
| 863 | /// \brief Set SV to be the shadow value for V. |
| 864 | void setShadow(Value *V, Value *SV) { |
| 865 | assert(!ShadowMap.count(V) && "Values may only have one shadow"); |
Evgeniy Stepanov | 174242c | 2014-07-03 11:56:30 +0000 | [diff] [blame] | 866 | ShadowMap[V] = PropagateShadow ? SV : getCleanShadow(V); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /// \brief Set Origin to be the origin value for V. |
| 870 | void setOrigin(Value *V, Value *Origin) { |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 871 | if (!MS.TrackOrigins) return; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 872 | assert(!OriginMap.count(V) && "Values may only have one origin"); |
| 873 | DEBUG(dbgs() << "ORIGIN: " << *V << " ==> " << *Origin << "\n"); |
| 874 | OriginMap[V] = Origin; |
| 875 | } |
| 876 | |
| 877 | /// \brief Create a clean shadow value for a given value. |
| 878 | /// |
| 879 | /// Clean shadow (all zeroes) means all bits of the value are defined |
| 880 | /// (initialized). |
Evgeniy Stepanov | a9a962c | 2013-03-21 09:38:26 +0000 | [diff] [blame] | 881 | Constant *getCleanShadow(Value *V) { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 882 | Type *ShadowTy = getShadowTy(V); |
| 883 | if (!ShadowTy) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 884 | return nullptr; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 885 | return Constant::getNullValue(ShadowTy); |
| 886 | } |
| 887 | |
| 888 | /// \brief Create a dirty shadow of a given shadow type. |
| 889 | Constant *getPoisonedShadow(Type *ShadowTy) { |
| 890 | assert(ShadowTy); |
| 891 | if (isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy)) |
| 892 | return Constant::getAllOnesValue(ShadowTy); |
Evgeniy Stepanov | 5997feb | 2014-07-31 11:02:27 +0000 | [diff] [blame] | 893 | if (ArrayType *AT = dyn_cast<ArrayType>(ShadowTy)) { |
| 894 | SmallVector<Constant *, 4> Vals(AT->getNumElements(), |
| 895 | getPoisonedShadow(AT->getElementType())); |
| 896 | return ConstantArray::get(AT, Vals); |
| 897 | } |
| 898 | if (StructType *ST = dyn_cast<StructType>(ShadowTy)) { |
| 899 | SmallVector<Constant *, 4> Vals; |
| 900 | for (unsigned i = 0, n = ST->getNumElements(); i < n; i++) |
| 901 | Vals.push_back(getPoisonedShadow(ST->getElementType(i))); |
| 902 | return ConstantStruct::get(ST, Vals); |
| 903 | } |
| 904 | llvm_unreachable("Unexpected shadow type"); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Evgeniy Stepanov | a9a962c | 2013-03-21 09:38:26 +0000 | [diff] [blame] | 907 | /// \brief Create a dirty shadow for a given value. |
| 908 | Constant *getPoisonedShadow(Value *V) { |
| 909 | Type *ShadowTy = getShadowTy(V); |
| 910 | if (!ShadowTy) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 911 | return nullptr; |
Evgeniy Stepanov | a9a962c | 2013-03-21 09:38:26 +0000 | [diff] [blame] | 912 | return getPoisonedShadow(ShadowTy); |
| 913 | } |
| 914 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 915 | /// \brief Create a clean (zero) origin. |
| 916 | Value *getCleanOrigin() { |
| 917 | return Constant::getNullValue(MS.OriginTy); |
| 918 | } |
| 919 | |
| 920 | /// \brief Get the shadow value for a given Value. |
| 921 | /// |
| 922 | /// This function either returns the value set earlier with setShadow, |
| 923 | /// or extracts if from ParamTLS (for function arguments). |
| 924 | Value *getShadow(Value *V) { |
Evgeniy Stepanov | 174242c | 2014-07-03 11:56:30 +0000 | [diff] [blame] | 925 | if (!PropagateShadow) return getCleanShadow(V); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 926 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 927 | // For instructions the shadow is already stored in the map. |
| 928 | Value *Shadow = ShadowMap[V]; |
| 929 | if (!Shadow) { |
| 930 | DEBUG(dbgs() << "No shadow: " << *V << "\n" << *(I->getParent())); |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 931 | (void)I; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 932 | assert(Shadow && "No shadow for a value"); |
| 933 | } |
| 934 | return Shadow; |
| 935 | } |
| 936 | if (UndefValue *U = dyn_cast<UndefValue>(V)) { |
Evgeniy Stepanov | dc6d7eb | 2013-07-03 14:39:14 +0000 | [diff] [blame] | 937 | Value *AllOnes = PoisonUndef ? getPoisonedShadow(V) : getCleanShadow(V); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 938 | DEBUG(dbgs() << "Undef: " << *U << " ==> " << *AllOnes << "\n"); |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 939 | (void)U; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 940 | return AllOnes; |
| 941 | } |
| 942 | if (Argument *A = dyn_cast<Argument>(V)) { |
| 943 | // For arguments we compute the shadow on demand and store it in the map. |
| 944 | Value **ShadowPtr = &ShadowMap[V]; |
| 945 | if (*ShadowPtr) |
| 946 | return *ShadowPtr; |
| 947 | Function *F = A->getParent(); |
| 948 | IRBuilder<> EntryIRB(F->getEntryBlock().getFirstNonPHI()); |
| 949 | unsigned ArgOffset = 0; |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 950 | for (auto &FArg : F->args()) { |
| 951 | if (!FArg.getType()->isSized()) { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 952 | DEBUG(dbgs() << "Arg is not sized\n"); |
| 953 | continue; |
| 954 | } |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 955 | unsigned Size = FArg.hasByValAttr() |
| 956 | ? MS.DL->getTypeAllocSize(FArg.getType()->getPointerElementType()) |
| 957 | : MS.DL->getTypeAllocSize(FArg.getType()); |
| 958 | if (A == &FArg) { |
Evgeniy Stepanov | 35eb265 | 2014-10-22 00:12:40 +0000 | [diff] [blame^] | 959 | bool Overflow = ArgOffset + Size > kParamTLSSize; |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 960 | Value *Base = getShadowPtrForArgument(&FArg, EntryIRB, ArgOffset); |
| 961 | if (FArg.hasByValAttr()) { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 962 | // ByVal pointer itself has clean shadow. We copy the actual |
| 963 | // argument shadow to the underlying memory. |
Evgeniy Stepanov | fca0123 | 2013-05-28 13:07:43 +0000 | [diff] [blame] | 964 | // Figure out maximal valid memcpy alignment. |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 965 | unsigned ArgAlign = FArg.getParamAlignment(); |
Evgeniy Stepanov | fca0123 | 2013-05-28 13:07:43 +0000 | [diff] [blame] | 966 | if (ArgAlign == 0) { |
| 967 | Type *EltType = A->getType()->getPointerElementType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 968 | ArgAlign = MS.DL->getABITypeAlignment(EltType); |
Evgeniy Stepanov | fca0123 | 2013-05-28 13:07:43 +0000 | [diff] [blame] | 969 | } |
Evgeniy Stepanov | 35eb265 | 2014-10-22 00:12:40 +0000 | [diff] [blame^] | 970 | if (Overflow) { |
| 971 | // ParamTLS overflow. |
| 972 | EntryIRB.CreateMemSet( |
| 973 | getShadowPtr(V, EntryIRB.getInt8Ty(), EntryIRB), |
| 974 | Constant::getNullValue(EntryIRB.getInt8Ty()), Size, ArgAlign); |
| 975 | } else { |
| 976 | unsigned CopyAlign = std::min(ArgAlign, kShadowTLSAlignment); |
| 977 | Value *Cpy = EntryIRB.CreateMemCpy( |
| 978 | getShadowPtr(V, EntryIRB.getInt8Ty(), EntryIRB), Base, Size, |
| 979 | CopyAlign); |
| 980 | DEBUG(dbgs() << " ByValCpy: " << *Cpy << "\n"); |
| 981 | (void)Cpy; |
| 982 | } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 983 | *ShadowPtr = getCleanShadow(V); |
| 984 | } else { |
Evgeniy Stepanov | 35eb265 | 2014-10-22 00:12:40 +0000 | [diff] [blame^] | 985 | if (Overflow) { |
| 986 | // ParamTLS overflow. |
| 987 | *ShadowPtr = getCleanShadow(V); |
| 988 | } else { |
| 989 | *ShadowPtr = |
| 990 | EntryIRB.CreateAlignedLoad(Base, kShadowTLSAlignment); |
| 991 | } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 992 | } |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 993 | DEBUG(dbgs() << " ARG: " << FArg << " ==> " << |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 994 | **ShadowPtr << "\n"); |
Evgeniy Stepanov | 35eb265 | 2014-10-22 00:12:40 +0000 | [diff] [blame^] | 995 | if (MS.TrackOrigins && !Overflow) { |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 996 | Value *OriginPtr = |
| 997 | getOriginPtrForArgument(&FArg, EntryIRB, ArgOffset); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 998 | setOrigin(A, EntryIRB.CreateLoad(OriginPtr)); |
| 999 | } |
| 1000 | } |
David Majnemer | f3cadce | 2014-10-20 06:13:33 +0000 | [diff] [blame] | 1001 | ArgOffset += RoundUpToAlignment(Size, kShadowTLSAlignment); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1002 | } |
| 1003 | assert(*ShadowPtr && "Could not find shadow for an argument"); |
| 1004 | return *ShadowPtr; |
| 1005 | } |
| 1006 | // For everything else the shadow is zero. |
| 1007 | return getCleanShadow(V); |
| 1008 | } |
| 1009 | |
| 1010 | /// \brief Get the shadow for i-th argument of the instruction I. |
| 1011 | Value *getShadow(Instruction *I, int i) { |
| 1012 | return getShadow(I->getOperand(i)); |
| 1013 | } |
| 1014 | |
| 1015 | /// \brief Get the origin for a value. |
| 1016 | Value *getOrigin(Value *V) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1017 | if (!MS.TrackOrigins) return nullptr; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1018 | if (isa<Instruction>(V) || isa<Argument>(V)) { |
| 1019 | Value *Origin = OriginMap[V]; |
| 1020 | if (!Origin) { |
| 1021 | DEBUG(dbgs() << "NO ORIGIN: " << *V << "\n"); |
| 1022 | Origin = getCleanOrigin(); |
| 1023 | } |
| 1024 | return Origin; |
| 1025 | } |
| 1026 | return getCleanOrigin(); |
| 1027 | } |
| 1028 | |
| 1029 | /// \brief Get the origin for i-th argument of the instruction I. |
| 1030 | Value *getOrigin(Instruction *I, int i) { |
| 1031 | return getOrigin(I->getOperand(i)); |
| 1032 | } |
| 1033 | |
| 1034 | /// \brief Remember the place where a shadow check should be inserted. |
| 1035 | /// |
| 1036 | /// This location will be later instrumented with a check that will print a |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1037 | /// UMR warning in runtime if the shadow value is not 0. |
| 1038 | void insertShadowCheck(Value *Shadow, Value *Origin, Instruction *OrigIns) { |
| 1039 | assert(Shadow); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1040 | if (!InsertChecks) return; |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 1041 | #ifndef NDEBUG |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1042 | Type *ShadowTy = Shadow->getType(); |
| 1043 | assert((isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy)) && |
| 1044 | "Can only insert checks for integer and vector shadow types"); |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 1045 | #endif |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1046 | InstrumentationList.push_back( |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1047 | ShadowOriginAndInsertPoint(Shadow, Origin, OrigIns)); |
| 1048 | } |
| 1049 | |
| 1050 | /// \brief Remember the place where a shadow check should be inserted. |
| 1051 | /// |
| 1052 | /// This location will be later instrumented with a check that will print a |
| 1053 | /// UMR warning in runtime if the value is not fully defined. |
| 1054 | void insertShadowCheck(Value *Val, Instruction *OrigIns) { |
| 1055 | assert(Val); |
| 1056 | Instruction *Shadow = dyn_cast_or_null<Instruction>(getShadow(Val)); |
| 1057 | if (!Shadow) return; |
| 1058 | Instruction *Origin = dyn_cast_or_null<Instruction>(getOrigin(Val)); |
| 1059 | insertShadowCheck(Shadow, Origin, OrigIns); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Evgeniy Stepanov | 5522a70 | 2013-09-24 11:20:27 +0000 | [diff] [blame] | 1062 | AtomicOrdering addReleaseOrdering(AtomicOrdering a) { |
| 1063 | switch (a) { |
| 1064 | case NotAtomic: |
| 1065 | return NotAtomic; |
| 1066 | case Unordered: |
| 1067 | case Monotonic: |
| 1068 | case Release: |
| 1069 | return Release; |
| 1070 | case Acquire: |
| 1071 | case AcquireRelease: |
| 1072 | return AcquireRelease; |
| 1073 | case SequentiallyConsistent: |
| 1074 | return SequentiallyConsistent; |
| 1075 | } |
Evgeniy Stepanov | 32be034 | 2013-09-25 08:56:00 +0000 | [diff] [blame] | 1076 | llvm_unreachable("Unknown ordering"); |
Evgeniy Stepanov | 5522a70 | 2013-09-24 11:20:27 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | AtomicOrdering addAcquireOrdering(AtomicOrdering a) { |
| 1080 | switch (a) { |
| 1081 | case NotAtomic: |
| 1082 | return NotAtomic; |
| 1083 | case Unordered: |
| 1084 | case Monotonic: |
| 1085 | case Acquire: |
| 1086 | return Acquire; |
| 1087 | case Release: |
| 1088 | case AcquireRelease: |
| 1089 | return AcquireRelease; |
| 1090 | case SequentiallyConsistent: |
| 1091 | return SequentiallyConsistent; |
| 1092 | } |
Evgeniy Stepanov | 32be034 | 2013-09-25 08:56:00 +0000 | [diff] [blame] | 1093 | llvm_unreachable("Unknown ordering"); |
Evgeniy Stepanov | 5522a70 | 2013-09-24 11:20:27 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Evgeniy Stepanov | 9b72e99 | 2012-12-14 13:48:31 +0000 | [diff] [blame] | 1096 | // ------------------- Visitors. |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1097 | |
| 1098 | /// \brief Instrument LoadInst |
| 1099 | /// |
| 1100 | /// Loads the corresponding shadow and (optionally) origin. |
| 1101 | /// Optionally, checks that the load address is fully defined. |
| 1102 | void visitLoadInst(LoadInst &I) { |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 1103 | assert(I.getType()->isSized() && "Load type must have size"); |
Evgeniy Stepanov | 5522a70 | 2013-09-24 11:20:27 +0000 | [diff] [blame] | 1104 | IRBuilder<> IRB(I.getNextNode()); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1105 | Type *ShadowTy = getShadowTy(&I); |
| 1106 | Value *Addr = I.getPointerOperand(); |
Evgeniy Stepanov | 174242c | 2014-07-03 11:56:30 +0000 | [diff] [blame] | 1107 | if (PropagateShadow) { |
Evgeniy Stepanov | 00062b4 | 2013-02-28 11:25:14 +0000 | [diff] [blame] | 1108 | Value *ShadowPtr = getShadowPtr(Addr, ShadowTy, IRB); |
| 1109 | setShadow(&I, |
| 1110 | IRB.CreateAlignedLoad(ShadowPtr, I.getAlignment(), "_msld")); |
| 1111 | } else { |
| 1112 | setShadow(&I, getCleanShadow(&I)); |
| 1113 | } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1114 | |
| 1115 | if (ClCheckAccessAddress) |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1116 | insertShadowCheck(I.getPointerOperand(), &I); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1117 | |
Evgeniy Stepanov | 5522a70 | 2013-09-24 11:20:27 +0000 | [diff] [blame] | 1118 | if (I.isAtomic()) |
| 1119 | I.setOrdering(addAcquireOrdering(I.getOrdering())); |
| 1120 | |
Evgeniy Stepanov | 5eb5bf8 | 2012-12-26 11:55:09 +0000 | [diff] [blame] | 1121 | if (MS.TrackOrigins) { |
Evgeniy Stepanov | 174242c | 2014-07-03 11:56:30 +0000 | [diff] [blame] | 1122 | if (PropagateShadow) { |
Evgeniy Stepanov | 00062b4 | 2013-02-28 11:25:14 +0000 | [diff] [blame] | 1123 | unsigned Alignment = std::max(kMinOriginAlignment, I.getAlignment()); |
| 1124 | setOrigin(&I, |
| 1125 | IRB.CreateAlignedLoad(getOriginPtr(Addr, IRB), Alignment)); |
| 1126 | } else { |
| 1127 | setOrigin(&I, getCleanOrigin()); |
| 1128 | } |
Evgeniy Stepanov | 5eb5bf8 | 2012-12-26 11:55:09 +0000 | [diff] [blame] | 1129 | } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | /// \brief Instrument StoreInst |
| 1133 | /// |
| 1134 | /// Stores the corresponding shadow and (optionally) origin. |
| 1135 | /// Optionally, checks that the store address is fully defined. |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1136 | void visitStoreInst(StoreInst &I) { |
Evgeniy Stepanov | 4f220d9 | 2012-12-06 11:41:03 +0000 | [diff] [blame] | 1137 | StoreList.push_back(&I); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Evgeniy Stepanov | 5522a70 | 2013-09-24 11:20:27 +0000 | [diff] [blame] | 1140 | void handleCASOrRMW(Instruction &I) { |
| 1141 | assert(isa<AtomicRMWInst>(I) || isa<AtomicCmpXchgInst>(I)); |
| 1142 | |
| 1143 | IRBuilder<> IRB(&I); |
| 1144 | Value *Addr = I.getOperand(0); |
| 1145 | Value *ShadowPtr = getShadowPtr(Addr, I.getType(), IRB); |
| 1146 | |
| 1147 | if (ClCheckAccessAddress) |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1148 | insertShadowCheck(Addr, &I); |
Evgeniy Stepanov | 5522a70 | 2013-09-24 11:20:27 +0000 | [diff] [blame] | 1149 | |
| 1150 | // Only test the conditional argument of cmpxchg instruction. |
| 1151 | // The other argument can potentially be uninitialized, but we can not |
| 1152 | // detect this situation reliably without possible false positives. |
| 1153 | if (isa<AtomicCmpXchgInst>(I)) |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1154 | insertShadowCheck(I.getOperand(1), &I); |
Evgeniy Stepanov | 5522a70 | 2013-09-24 11:20:27 +0000 | [diff] [blame] | 1155 | |
| 1156 | IRB.CreateStore(getCleanShadow(&I), ShadowPtr); |
| 1157 | |
| 1158 | setShadow(&I, getCleanShadow(&I)); |
| 1159 | } |
| 1160 | |
| 1161 | void visitAtomicRMWInst(AtomicRMWInst &I) { |
| 1162 | handleCASOrRMW(I); |
| 1163 | I.setOrdering(addReleaseOrdering(I.getOrdering())); |
| 1164 | } |
| 1165 | |
| 1166 | void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) { |
| 1167 | handleCASOrRMW(I); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1168 | I.setSuccessOrdering(addReleaseOrdering(I.getSuccessOrdering())); |
Evgeniy Stepanov | 5522a70 | 2013-09-24 11:20:27 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Evgeniy Stepanov | 30484fc | 2012-11-29 15:22:06 +0000 | [diff] [blame] | 1171 | // Vector manipulation. |
| 1172 | void visitExtractElementInst(ExtractElementInst &I) { |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1173 | insertShadowCheck(I.getOperand(1), &I); |
Evgeniy Stepanov | 30484fc | 2012-11-29 15:22:06 +0000 | [diff] [blame] | 1174 | IRBuilder<> IRB(&I); |
| 1175 | setShadow(&I, IRB.CreateExtractElement(getShadow(&I, 0), I.getOperand(1), |
| 1176 | "_msprop")); |
| 1177 | setOrigin(&I, getOrigin(&I, 0)); |
| 1178 | } |
| 1179 | |
| 1180 | void visitInsertElementInst(InsertElementInst &I) { |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1181 | insertShadowCheck(I.getOperand(2), &I); |
Evgeniy Stepanov | 30484fc | 2012-11-29 15:22:06 +0000 | [diff] [blame] | 1182 | IRBuilder<> IRB(&I); |
| 1183 | setShadow(&I, IRB.CreateInsertElement(getShadow(&I, 0), getShadow(&I, 1), |
| 1184 | I.getOperand(2), "_msprop")); |
| 1185 | setOriginForNaryOp(I); |
| 1186 | } |
| 1187 | |
| 1188 | void visitShuffleVectorInst(ShuffleVectorInst &I) { |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1189 | insertShadowCheck(I.getOperand(2), &I); |
Evgeniy Stepanov | 30484fc | 2012-11-29 15:22:06 +0000 | [diff] [blame] | 1190 | IRBuilder<> IRB(&I); |
| 1191 | setShadow(&I, IRB.CreateShuffleVector(getShadow(&I, 0), getShadow(&I, 1), |
| 1192 | I.getOperand(2), "_msprop")); |
| 1193 | setOriginForNaryOp(I); |
| 1194 | } |
| 1195 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1196 | // Casts. |
| 1197 | void visitSExtInst(SExtInst &I) { |
| 1198 | IRBuilder<> IRB(&I); |
| 1199 | setShadow(&I, IRB.CreateSExt(getShadow(&I, 0), I.getType(), "_msprop")); |
| 1200 | setOrigin(&I, getOrigin(&I, 0)); |
| 1201 | } |
| 1202 | |
| 1203 | void visitZExtInst(ZExtInst &I) { |
| 1204 | IRBuilder<> IRB(&I); |
| 1205 | setShadow(&I, IRB.CreateZExt(getShadow(&I, 0), I.getType(), "_msprop")); |
| 1206 | setOrigin(&I, getOrigin(&I, 0)); |
| 1207 | } |
| 1208 | |
| 1209 | void visitTruncInst(TruncInst &I) { |
| 1210 | IRBuilder<> IRB(&I); |
| 1211 | setShadow(&I, IRB.CreateTrunc(getShadow(&I, 0), I.getType(), "_msprop")); |
| 1212 | setOrigin(&I, getOrigin(&I, 0)); |
| 1213 | } |
| 1214 | |
| 1215 | void visitBitCastInst(BitCastInst &I) { |
| 1216 | IRBuilder<> IRB(&I); |
| 1217 | setShadow(&I, IRB.CreateBitCast(getShadow(&I, 0), getShadowTy(&I))); |
| 1218 | setOrigin(&I, getOrigin(&I, 0)); |
| 1219 | } |
| 1220 | |
| 1221 | void visitPtrToIntInst(PtrToIntInst &I) { |
| 1222 | IRBuilder<> IRB(&I); |
| 1223 | setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false, |
| 1224 | "_msprop_ptrtoint")); |
| 1225 | setOrigin(&I, getOrigin(&I, 0)); |
| 1226 | } |
| 1227 | |
| 1228 | void visitIntToPtrInst(IntToPtrInst &I) { |
| 1229 | IRBuilder<> IRB(&I); |
| 1230 | setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false, |
| 1231 | "_msprop_inttoptr")); |
| 1232 | setOrigin(&I, getOrigin(&I, 0)); |
| 1233 | } |
| 1234 | |
| 1235 | void visitFPToSIInst(CastInst& I) { handleShadowOr(I); } |
| 1236 | void visitFPToUIInst(CastInst& I) { handleShadowOr(I); } |
| 1237 | void visitSIToFPInst(CastInst& I) { handleShadowOr(I); } |
| 1238 | void visitUIToFPInst(CastInst& I) { handleShadowOr(I); } |
| 1239 | void visitFPExtInst(CastInst& I) { handleShadowOr(I); } |
| 1240 | void visitFPTruncInst(CastInst& I) { handleShadowOr(I); } |
| 1241 | |
| 1242 | /// \brief Propagate shadow for bitwise AND. |
| 1243 | /// |
| 1244 | /// This code is exact, i.e. if, for example, a bit in the left argument |
| 1245 | /// is defined and 0, then neither the value not definedness of the |
| 1246 | /// corresponding bit in B don't affect the resulting shadow. |
| 1247 | void visitAnd(BinaryOperator &I) { |
| 1248 | IRBuilder<> IRB(&I); |
| 1249 | // "And" of 0 and a poisoned value results in unpoisoned value. |
| 1250 | // 1&1 => 1; 0&1 => 0; p&1 => p; |
| 1251 | // 1&0 => 0; 0&0 => 0; p&0 => 0; |
| 1252 | // 1&p => p; 0&p => 0; p&p => p; |
| 1253 | // S = (S1 & S2) | (V1 & S2) | (S1 & V2) |
| 1254 | Value *S1 = getShadow(&I, 0); |
| 1255 | Value *S2 = getShadow(&I, 1); |
| 1256 | Value *V1 = I.getOperand(0); |
| 1257 | Value *V2 = I.getOperand(1); |
| 1258 | if (V1->getType() != S1->getType()) { |
| 1259 | V1 = IRB.CreateIntCast(V1, S1->getType(), false); |
| 1260 | V2 = IRB.CreateIntCast(V2, S2->getType(), false); |
| 1261 | } |
| 1262 | Value *S1S2 = IRB.CreateAnd(S1, S2); |
| 1263 | Value *V1S2 = IRB.CreateAnd(V1, S2); |
| 1264 | Value *S1V2 = IRB.CreateAnd(S1, V2); |
| 1265 | setShadow(&I, IRB.CreateOr(S1S2, IRB.CreateOr(V1S2, S1V2))); |
| 1266 | setOriginForNaryOp(I); |
| 1267 | } |
| 1268 | |
| 1269 | void visitOr(BinaryOperator &I) { |
| 1270 | IRBuilder<> IRB(&I); |
| 1271 | // "Or" of 1 and a poisoned value results in unpoisoned value. |
| 1272 | // 1|1 => 1; 0|1 => 1; p|1 => 1; |
| 1273 | // 1|0 => 1; 0|0 => 0; p|0 => p; |
| 1274 | // 1|p => 1; 0|p => p; p|p => p; |
| 1275 | // S = (S1 & S2) | (~V1 & S2) | (S1 & ~V2) |
| 1276 | Value *S1 = getShadow(&I, 0); |
| 1277 | Value *S2 = getShadow(&I, 1); |
| 1278 | Value *V1 = IRB.CreateNot(I.getOperand(0)); |
| 1279 | Value *V2 = IRB.CreateNot(I.getOperand(1)); |
| 1280 | if (V1->getType() != S1->getType()) { |
| 1281 | V1 = IRB.CreateIntCast(V1, S1->getType(), false); |
| 1282 | V2 = IRB.CreateIntCast(V2, S2->getType(), false); |
| 1283 | } |
| 1284 | Value *S1S2 = IRB.CreateAnd(S1, S2); |
| 1285 | Value *V1S2 = IRB.CreateAnd(V1, S2); |
| 1286 | Value *S1V2 = IRB.CreateAnd(S1, V2); |
| 1287 | setShadow(&I, IRB.CreateOr(S1S2, IRB.CreateOr(V1S2, S1V2))); |
| 1288 | setOriginForNaryOp(I); |
| 1289 | } |
| 1290 | |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1291 | /// \brief Default propagation of shadow and/or origin. |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1292 | /// |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1293 | /// This class implements the general case of shadow propagation, used in all |
| 1294 | /// cases where we don't know and/or don't care about what the operation |
| 1295 | /// actually does. It converts all input shadow values to a common type |
| 1296 | /// (extending or truncating as necessary), and bitwise OR's them. |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1297 | /// |
| 1298 | /// This is much cheaper than inserting checks (i.e. requiring inputs to be |
| 1299 | /// fully initialized), and less prone to false positives. |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1300 | /// |
| 1301 | /// This class also implements the general case of origin propagation. For a |
| 1302 | /// Nary operation, result origin is set to the origin of an argument that is |
| 1303 | /// not entirely initialized. If there is more than one such arguments, the |
| 1304 | /// rightmost of them is picked. It does not matter which one is picked if all |
| 1305 | /// arguments are initialized. |
| 1306 | template <bool CombineShadow> |
| 1307 | class Combiner { |
| 1308 | Value *Shadow; |
| 1309 | Value *Origin; |
| 1310 | IRBuilder<> &IRB; |
| 1311 | MemorySanitizerVisitor *MSV; |
Evgeniy Stepanov | 9b72e99 | 2012-12-14 13:48:31 +0000 | [diff] [blame] | 1312 | |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1313 | public: |
| 1314 | Combiner(MemorySanitizerVisitor *MSV, IRBuilder<> &IRB) : |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1315 | Shadow(nullptr), Origin(nullptr), IRB(IRB), MSV(MSV) {} |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1316 | |
| 1317 | /// \brief Add a pair of shadow and origin values to the mix. |
| 1318 | Combiner &Add(Value *OpShadow, Value *OpOrigin) { |
| 1319 | if (CombineShadow) { |
| 1320 | assert(OpShadow); |
| 1321 | if (!Shadow) |
| 1322 | Shadow = OpShadow; |
| 1323 | else { |
| 1324 | OpShadow = MSV->CreateShadowCast(IRB, OpShadow, Shadow->getType()); |
| 1325 | Shadow = IRB.CreateOr(Shadow, OpShadow, "_msprop"); |
| 1326 | } |
| 1327 | } |
| 1328 | |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 1329 | if (MSV->MS.TrackOrigins) { |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1330 | assert(OpOrigin); |
| 1331 | if (!Origin) { |
| 1332 | Origin = OpOrigin; |
| 1333 | } else { |
Evgeniy Stepanov | 70d1b0a | 2014-06-09 14:29:34 +0000 | [diff] [blame] | 1334 | Constant *ConstOrigin = dyn_cast<Constant>(OpOrigin); |
| 1335 | // No point in adding something that might result in 0 origin value. |
| 1336 | if (!ConstOrigin || !ConstOrigin->isNullValue()) { |
| 1337 | Value *FlatShadow = MSV->convertToShadowTyNoVec(OpShadow, IRB); |
| 1338 | Value *Cond = |
| 1339 | IRB.CreateICmpNE(FlatShadow, MSV->getCleanShadow(FlatShadow)); |
| 1340 | Origin = IRB.CreateSelect(Cond, OpOrigin, Origin); |
| 1341 | } |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | return *this; |
| 1345 | } |
| 1346 | |
| 1347 | /// \brief Add an application value to the mix. |
| 1348 | Combiner &Add(Value *V) { |
| 1349 | Value *OpShadow = MSV->getShadow(V); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1350 | Value *OpOrigin = MSV->MS.TrackOrigins ? MSV->getOrigin(V) : nullptr; |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1351 | return Add(OpShadow, OpOrigin); |
| 1352 | } |
| 1353 | |
| 1354 | /// \brief Set the current combined values as the given instruction's shadow |
| 1355 | /// and origin. |
| 1356 | void Done(Instruction *I) { |
| 1357 | if (CombineShadow) { |
| 1358 | assert(Shadow); |
| 1359 | Shadow = MSV->CreateShadowCast(IRB, Shadow, MSV->getShadowTy(I)); |
| 1360 | MSV->setShadow(I, Shadow); |
| 1361 | } |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 1362 | if (MSV->MS.TrackOrigins) { |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1363 | assert(Origin); |
| 1364 | MSV->setOrigin(I, Origin); |
| 1365 | } |
| 1366 | } |
| 1367 | }; |
| 1368 | |
| 1369 | typedef Combiner<true> ShadowAndOriginCombiner; |
| 1370 | typedef Combiner<false> OriginCombiner; |
| 1371 | |
| 1372 | /// \brief Propagate origin for arbitrary operation. |
| 1373 | void setOriginForNaryOp(Instruction &I) { |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 1374 | if (!MS.TrackOrigins) return; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1375 | IRBuilder<> IRB(&I); |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1376 | OriginCombiner OC(this, IRB); |
| 1377 | for (Instruction::op_iterator OI = I.op_begin(); OI != I.op_end(); ++OI) |
| 1378 | OC.Add(OI->get()); |
| 1379 | OC.Done(&I); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1382 | size_t VectorOrPrimitiveTypeSizeInBits(Type *Ty) { |
Evgeniy Stepanov | f19c086 | 2012-12-25 16:04:38 +0000 | [diff] [blame] | 1383 | assert(!(Ty->isVectorTy() && Ty->getScalarType()->isPointerTy()) && |
| 1384 | "Vector of pointers is not a valid shadow type"); |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1385 | return Ty->isVectorTy() ? |
| 1386 | Ty->getVectorNumElements() * Ty->getScalarSizeInBits() : |
| 1387 | Ty->getPrimitiveSizeInBits(); |
| 1388 | } |
| 1389 | |
| 1390 | /// \brief Cast between two shadow types, extending or truncating as |
| 1391 | /// necessary. |
Evgeniy Stepanov | 21a9c93 | 2013-10-17 10:53:50 +0000 | [diff] [blame] | 1392 | Value *CreateShadowCast(IRBuilder<> &IRB, Value *V, Type *dstTy, |
| 1393 | bool Signed = false) { |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1394 | Type *srcTy = V->getType(); |
| 1395 | if (dstTy->isIntegerTy() && srcTy->isIntegerTy()) |
Evgeniy Stepanov | 21a9c93 | 2013-10-17 10:53:50 +0000 | [diff] [blame] | 1396 | return IRB.CreateIntCast(V, dstTy, Signed); |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1397 | if (dstTy->isVectorTy() && srcTy->isVectorTy() && |
| 1398 | dstTy->getVectorNumElements() == srcTy->getVectorNumElements()) |
Evgeniy Stepanov | 21a9c93 | 2013-10-17 10:53:50 +0000 | [diff] [blame] | 1399 | return IRB.CreateIntCast(V, dstTy, Signed); |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1400 | size_t srcSizeInBits = VectorOrPrimitiveTypeSizeInBits(srcTy); |
| 1401 | size_t dstSizeInBits = VectorOrPrimitiveTypeSizeInBits(dstTy); |
| 1402 | Value *V1 = IRB.CreateBitCast(V, Type::getIntNTy(*MS.C, srcSizeInBits)); |
| 1403 | Value *V2 = |
Evgeniy Stepanov | 21a9c93 | 2013-10-17 10:53:50 +0000 | [diff] [blame] | 1404 | IRB.CreateIntCast(V1, Type::getIntNTy(*MS.C, dstSizeInBits), Signed); |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1405 | return IRB.CreateBitCast(V2, dstTy); |
| 1406 | // TODO: handle struct types. |
| 1407 | } |
| 1408 | |
Evgeniy Stepanov | fc742ac | 2014-03-25 13:08:34 +0000 | [diff] [blame] | 1409 | /// \brief Cast an application value to the type of its own shadow. |
| 1410 | Value *CreateAppToShadowCast(IRBuilder<> &IRB, Value *V) { |
| 1411 | Type *ShadowTy = getShadowTy(V); |
| 1412 | if (V->getType() == ShadowTy) |
| 1413 | return V; |
| 1414 | if (V->getType()->isPtrOrPtrVectorTy()) |
| 1415 | return IRB.CreatePtrToInt(V, ShadowTy); |
| 1416 | else |
| 1417 | return IRB.CreateBitCast(V, ShadowTy); |
| 1418 | } |
| 1419 | |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1420 | /// \brief Propagate shadow for arbitrary operation. |
| 1421 | void handleShadowOr(Instruction &I) { |
| 1422 | IRBuilder<> IRB(&I); |
| 1423 | ShadowAndOriginCombiner SC(this, IRB); |
| 1424 | for (Instruction::op_iterator OI = I.op_begin(); OI != I.op_end(); ++OI) |
| 1425 | SC.Add(OI->get()); |
| 1426 | SC.Done(&I); |
| 1427 | } |
| 1428 | |
Evgeniy Stepanov | df187fe | 2014-06-17 09:23:12 +0000 | [diff] [blame] | 1429 | // \brief Handle multiplication by constant. |
| 1430 | // |
| 1431 | // Handle a special case of multiplication by constant that may have one or |
| 1432 | // more zeros in the lower bits. This makes corresponding number of lower bits |
| 1433 | // of the result zero as well. We model it by shifting the other operand |
| 1434 | // shadow left by the required number of bits. Effectively, we transform |
| 1435 | // (X * (A * 2**B)) to ((X << B) * A) and instrument (X << B) as (Sx << B). |
| 1436 | // We use multiplication by 2**N instead of shift to cover the case of |
| 1437 | // multiplication by 0, which may occur in some elements of a vector operand. |
| 1438 | void handleMulByConstant(BinaryOperator &I, Constant *ConstArg, |
| 1439 | Value *OtherArg) { |
| 1440 | Constant *ShadowMul; |
| 1441 | Type *Ty = ConstArg->getType(); |
| 1442 | if (Ty->isVectorTy()) { |
| 1443 | unsigned NumElements = Ty->getVectorNumElements(); |
| 1444 | Type *EltTy = Ty->getSequentialElementType(); |
| 1445 | SmallVector<Constant *, 16> Elements; |
| 1446 | for (unsigned Idx = 0; Idx < NumElements; ++Idx) { |
| 1447 | ConstantInt *Elt = |
| 1448 | dyn_cast<ConstantInt>(ConstArg->getAggregateElement(Idx)); |
| 1449 | APInt V = Elt->getValue(); |
| 1450 | APInt V2 = APInt(V.getBitWidth(), 1) << V.countTrailingZeros(); |
| 1451 | Elements.push_back(ConstantInt::get(EltTy, V2)); |
| 1452 | } |
| 1453 | ShadowMul = ConstantVector::get(Elements); |
| 1454 | } else { |
| 1455 | ConstantInt *Elt = dyn_cast<ConstantInt>(ConstArg); |
| 1456 | APInt V = Elt->getValue(); |
| 1457 | APInt V2 = APInt(V.getBitWidth(), 1) << V.countTrailingZeros(); |
| 1458 | ShadowMul = ConstantInt::get(Elt->getType(), V2); |
| 1459 | } |
| 1460 | |
| 1461 | IRBuilder<> IRB(&I); |
| 1462 | setShadow(&I, |
| 1463 | IRB.CreateMul(getShadow(OtherArg), ShadowMul, "msprop_mul_cst")); |
| 1464 | setOrigin(&I, getOrigin(OtherArg)); |
| 1465 | } |
| 1466 | |
| 1467 | void visitMul(BinaryOperator &I) { |
| 1468 | Constant *constOp0 = dyn_cast<Constant>(I.getOperand(0)); |
| 1469 | Constant *constOp1 = dyn_cast<Constant>(I.getOperand(1)); |
| 1470 | if (constOp0 && !constOp1) |
| 1471 | handleMulByConstant(I, constOp0, I.getOperand(1)); |
| 1472 | else if (constOp1 && !constOp0) |
| 1473 | handleMulByConstant(I, constOp1, I.getOperand(0)); |
| 1474 | else |
| 1475 | handleShadowOr(I); |
| 1476 | } |
| 1477 | |
Evgeniy Stepanov | f18e3af | 2012-12-14 12:54:18 +0000 | [diff] [blame] | 1478 | void visitFAdd(BinaryOperator &I) { handleShadowOr(I); } |
| 1479 | void visitFSub(BinaryOperator &I) { handleShadowOr(I); } |
| 1480 | void visitFMul(BinaryOperator &I) { handleShadowOr(I); } |
| 1481 | void visitAdd(BinaryOperator &I) { handleShadowOr(I); } |
| 1482 | void visitSub(BinaryOperator &I) { handleShadowOr(I); } |
| 1483 | void visitXor(BinaryOperator &I) { handleShadowOr(I); } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1484 | |
| 1485 | void handleDiv(Instruction &I) { |
| 1486 | IRBuilder<> IRB(&I); |
| 1487 | // Strict on the second argument. |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1488 | insertShadowCheck(I.getOperand(1), &I); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1489 | setShadow(&I, getShadow(&I, 0)); |
| 1490 | setOrigin(&I, getOrigin(&I, 0)); |
| 1491 | } |
| 1492 | |
| 1493 | void visitUDiv(BinaryOperator &I) { handleDiv(I); } |
| 1494 | void visitSDiv(BinaryOperator &I) { handleDiv(I); } |
| 1495 | void visitFDiv(BinaryOperator &I) { handleDiv(I); } |
| 1496 | void visitURem(BinaryOperator &I) { handleDiv(I); } |
| 1497 | void visitSRem(BinaryOperator &I) { handleDiv(I); } |
| 1498 | void visitFRem(BinaryOperator &I) { handleDiv(I); } |
| 1499 | |
| 1500 | /// \brief Instrument == and != comparisons. |
| 1501 | /// |
| 1502 | /// Sometimes the comparison result is known even if some of the bits of the |
| 1503 | /// arguments are not. |
| 1504 | void handleEqualityComparison(ICmpInst &I) { |
| 1505 | IRBuilder<> IRB(&I); |
| 1506 | Value *A = I.getOperand(0); |
| 1507 | Value *B = I.getOperand(1); |
| 1508 | Value *Sa = getShadow(A); |
| 1509 | Value *Sb = getShadow(B); |
Evgeniy Stepanov | d14e47b | 2013-01-15 16:44:52 +0000 | [diff] [blame] | 1510 | |
| 1511 | // Get rid of pointers and vectors of pointers. |
| 1512 | // For ints (and vectors of ints), types of A and Sa match, |
| 1513 | // and this is a no-op. |
| 1514 | A = IRB.CreatePointerCast(A, Sa->getType()); |
| 1515 | B = IRB.CreatePointerCast(B, Sb->getType()); |
| 1516 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1517 | // A == B <==> (C = A^B) == 0 |
| 1518 | // A != B <==> (C = A^B) != 0 |
| 1519 | // Sc = Sa | Sb |
| 1520 | Value *C = IRB.CreateXor(A, B); |
| 1521 | Value *Sc = IRB.CreateOr(Sa, Sb); |
| 1522 | // Now dealing with i = (C == 0) comparison (or C != 0, does not matter now) |
| 1523 | // Result is defined if one of the following is true |
| 1524 | // * there is a defined 1 bit in C |
| 1525 | // * C is fully defined |
| 1526 | // Si = !(C & ~Sc) && Sc |
| 1527 | Value *Zero = Constant::getNullValue(Sc->getType()); |
| 1528 | Value *MinusOne = Constant::getAllOnesValue(Sc->getType()); |
| 1529 | Value *Si = |
| 1530 | IRB.CreateAnd(IRB.CreateICmpNE(Sc, Zero), |
| 1531 | IRB.CreateICmpEQ( |
| 1532 | IRB.CreateAnd(IRB.CreateXor(Sc, MinusOne), C), Zero)); |
| 1533 | Si->setName("_msprop_icmp"); |
| 1534 | setShadow(&I, Si); |
| 1535 | setOriginForNaryOp(I); |
| 1536 | } |
| 1537 | |
Evgeniy Stepanov | fac8403 | 2013-01-25 15:31:10 +0000 | [diff] [blame] | 1538 | /// \brief Build the lowest possible value of V, taking into account V's |
| 1539 | /// uninitialized bits. |
| 1540 | Value *getLowestPossibleValue(IRBuilder<> &IRB, Value *A, Value *Sa, |
| 1541 | bool isSigned) { |
| 1542 | if (isSigned) { |
| 1543 | // Split shadow into sign bit and other bits. |
| 1544 | Value *SaOtherBits = IRB.CreateLShr(IRB.CreateShl(Sa, 1), 1); |
| 1545 | Value *SaSignBit = IRB.CreateXor(Sa, SaOtherBits); |
| 1546 | // Maximise the undefined shadow bit, minimize other undefined bits. |
| 1547 | return |
| 1548 | IRB.CreateOr(IRB.CreateAnd(A, IRB.CreateNot(SaOtherBits)), SaSignBit); |
| 1549 | } else { |
| 1550 | // Minimize undefined bits. |
| 1551 | return IRB.CreateAnd(A, IRB.CreateNot(Sa)); |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | /// \brief Build the highest possible value of V, taking into account V's |
| 1556 | /// uninitialized bits. |
| 1557 | Value *getHighestPossibleValue(IRBuilder<> &IRB, Value *A, Value *Sa, |
| 1558 | bool isSigned) { |
| 1559 | if (isSigned) { |
| 1560 | // Split shadow into sign bit and other bits. |
| 1561 | Value *SaOtherBits = IRB.CreateLShr(IRB.CreateShl(Sa, 1), 1); |
| 1562 | Value *SaSignBit = IRB.CreateXor(Sa, SaOtherBits); |
| 1563 | // Minimise the undefined shadow bit, maximise other undefined bits. |
| 1564 | return |
| 1565 | IRB.CreateOr(IRB.CreateAnd(A, IRB.CreateNot(SaSignBit)), SaOtherBits); |
| 1566 | } else { |
| 1567 | // Maximize undefined bits. |
| 1568 | return IRB.CreateOr(A, Sa); |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | /// \brief Instrument relational comparisons. |
| 1573 | /// |
| 1574 | /// This function does exact shadow propagation for all relational |
| 1575 | /// comparisons of integers, pointers and vectors of those. |
| 1576 | /// FIXME: output seems suboptimal when one of the operands is a constant |
| 1577 | void handleRelationalComparisonExact(ICmpInst &I) { |
| 1578 | IRBuilder<> IRB(&I); |
| 1579 | Value *A = I.getOperand(0); |
| 1580 | Value *B = I.getOperand(1); |
| 1581 | Value *Sa = getShadow(A); |
| 1582 | Value *Sb = getShadow(B); |
| 1583 | |
| 1584 | // Get rid of pointers and vectors of pointers. |
| 1585 | // For ints (and vectors of ints), types of A and Sa match, |
| 1586 | // and this is a no-op. |
| 1587 | A = IRB.CreatePointerCast(A, Sa->getType()); |
| 1588 | B = IRB.CreatePointerCast(B, Sb->getType()); |
| 1589 | |
Evgeniy Stepanov | 2cb0fa1 | 2013-01-25 15:35:29 +0000 | [diff] [blame] | 1590 | // Let [a0, a1] be the interval of possible values of A, taking into account |
| 1591 | // its undefined bits. Let [b0, b1] be the interval of possible values of B. |
| 1592 | // Then (A cmp B) is defined iff (a0 cmp b1) == (a1 cmp b0). |
Evgeniy Stepanov | fac8403 | 2013-01-25 15:31:10 +0000 | [diff] [blame] | 1593 | bool IsSigned = I.isSigned(); |
| 1594 | Value *S1 = IRB.CreateICmp(I.getPredicate(), |
| 1595 | getLowestPossibleValue(IRB, A, Sa, IsSigned), |
| 1596 | getHighestPossibleValue(IRB, B, Sb, IsSigned)); |
| 1597 | Value *S2 = IRB.CreateICmp(I.getPredicate(), |
| 1598 | getHighestPossibleValue(IRB, A, Sa, IsSigned), |
| 1599 | getLowestPossibleValue(IRB, B, Sb, IsSigned)); |
| 1600 | Value *Si = IRB.CreateXor(S1, S2); |
| 1601 | setShadow(&I, Si); |
| 1602 | setOriginForNaryOp(I); |
| 1603 | } |
| 1604 | |
Evgeniy Stepanov | 857d9d2 | 2012-11-29 14:25:47 +0000 | [diff] [blame] | 1605 | /// \brief Instrument signed relational comparisons. |
| 1606 | /// |
| 1607 | /// Handle (x<0) and (x>=0) comparisons (essentially, sign bit tests) by |
| 1608 | /// propagating the highest bit of the shadow. Everything else is delegated |
| 1609 | /// to handleShadowOr(). |
| 1610 | void handleSignedRelationalComparison(ICmpInst &I) { |
| 1611 | Constant *constOp0 = dyn_cast<Constant>(I.getOperand(0)); |
| 1612 | Constant *constOp1 = dyn_cast<Constant>(I.getOperand(1)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1613 | Value* op = nullptr; |
Evgeniy Stepanov | 857d9d2 | 2012-11-29 14:25:47 +0000 | [diff] [blame] | 1614 | CmpInst::Predicate pre = I.getPredicate(); |
| 1615 | if (constOp0 && constOp0->isNullValue() && |
| 1616 | (pre == CmpInst::ICMP_SGT || pre == CmpInst::ICMP_SLE)) { |
| 1617 | op = I.getOperand(1); |
| 1618 | } else if (constOp1 && constOp1->isNullValue() && |
| 1619 | (pre == CmpInst::ICMP_SLT || pre == CmpInst::ICMP_SGE)) { |
| 1620 | op = I.getOperand(0); |
| 1621 | } |
| 1622 | if (op) { |
| 1623 | IRBuilder<> IRB(&I); |
| 1624 | Value* Shadow = |
| 1625 | IRB.CreateICmpSLT(getShadow(op), getCleanShadow(op), "_msprop_icmpslt"); |
| 1626 | setShadow(&I, Shadow); |
| 1627 | setOrigin(&I, getOrigin(op)); |
| 1628 | } else { |
| 1629 | handleShadowOr(I); |
| 1630 | } |
| 1631 | } |
| 1632 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1633 | void visitICmpInst(ICmpInst &I) { |
Evgeniy Stepanov | 6f85ef3 | 2013-01-28 11:42:28 +0000 | [diff] [blame] | 1634 | if (!ClHandleICmp) { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1635 | handleShadowOr(I); |
Evgeniy Stepanov | 6f85ef3 | 2013-01-28 11:42:28 +0000 | [diff] [blame] | 1636 | return; |
| 1637 | } |
| 1638 | if (I.isEquality()) { |
| 1639 | handleEqualityComparison(I); |
| 1640 | return; |
| 1641 | } |
| 1642 | |
| 1643 | assert(I.isRelational()); |
| 1644 | if (ClHandleICmpExact) { |
| 1645 | handleRelationalComparisonExact(I); |
| 1646 | return; |
| 1647 | } |
| 1648 | if (I.isSigned()) { |
| 1649 | handleSignedRelationalComparison(I); |
| 1650 | return; |
| 1651 | } |
| 1652 | |
| 1653 | assert(I.isUnsigned()); |
| 1654 | if ((isa<Constant>(I.getOperand(0)) || isa<Constant>(I.getOperand(1)))) { |
| 1655 | handleRelationalComparisonExact(I); |
| 1656 | return; |
| 1657 | } |
| 1658 | |
| 1659 | handleShadowOr(I); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | void visitFCmpInst(FCmpInst &I) { |
| 1663 | handleShadowOr(I); |
| 1664 | } |
| 1665 | |
| 1666 | void handleShift(BinaryOperator &I) { |
| 1667 | IRBuilder<> IRB(&I); |
| 1668 | // If any of the S2 bits are poisoned, the whole thing is poisoned. |
| 1669 | // Otherwise perform the same shift on S1. |
| 1670 | Value *S1 = getShadow(&I, 0); |
| 1671 | Value *S2 = getShadow(&I, 1); |
| 1672 | Value *S2Conv = IRB.CreateSExt(IRB.CreateICmpNE(S2, getCleanShadow(S2)), |
| 1673 | S2->getType()); |
| 1674 | Value *V2 = I.getOperand(1); |
| 1675 | Value *Shift = IRB.CreateBinOp(I.getOpcode(), S1, V2); |
| 1676 | setShadow(&I, IRB.CreateOr(Shift, S2Conv)); |
| 1677 | setOriginForNaryOp(I); |
| 1678 | } |
| 1679 | |
| 1680 | void visitShl(BinaryOperator &I) { handleShift(I); } |
| 1681 | void visitAShr(BinaryOperator &I) { handleShift(I); } |
| 1682 | void visitLShr(BinaryOperator &I) { handleShift(I); } |
| 1683 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1684 | /// \brief Instrument llvm.memmove |
| 1685 | /// |
| 1686 | /// At this point we don't know if llvm.memmove will be inlined or not. |
| 1687 | /// If we don't instrument it and it gets inlined, |
| 1688 | /// our interceptor will not kick in and we will lose the memmove. |
| 1689 | /// If we instrument the call here, but it does not get inlined, |
| 1690 | /// we will memove the shadow twice: which is bad in case |
| 1691 | /// of overlapping regions. So, we simply lower the intrinsic to a call. |
| 1692 | /// |
Evgeniy Stepanov | 62b5db9 | 2012-11-29 12:49:04 +0000 | [diff] [blame] | 1693 | /// Similar situation exists for memcpy and memset. |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1694 | void visitMemMoveInst(MemMoveInst &I) { |
| 1695 | IRBuilder<> IRB(&I); |
| 1696 | IRB.CreateCall3( |
| 1697 | MS.MemmoveFn, |
| 1698 | IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()), |
| 1699 | IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()), |
| 1700 | IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)); |
| 1701 | I.eraseFromParent(); |
| 1702 | } |
| 1703 | |
Evgeniy Stepanov | 62b5db9 | 2012-11-29 12:49:04 +0000 | [diff] [blame] | 1704 | // Similar to memmove: avoid copying shadow twice. |
| 1705 | // This is somewhat unfortunate as it may slowdown small constant memcpys. |
| 1706 | // FIXME: consider doing manual inline for small constant sizes and proper |
| 1707 | // alignment. |
| 1708 | void visitMemCpyInst(MemCpyInst &I) { |
| 1709 | IRBuilder<> IRB(&I); |
| 1710 | IRB.CreateCall3( |
| 1711 | MS.MemcpyFn, |
| 1712 | IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()), |
| 1713 | IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()), |
| 1714 | IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)); |
| 1715 | I.eraseFromParent(); |
| 1716 | } |
| 1717 | |
| 1718 | // Same as memcpy. |
| 1719 | void visitMemSetInst(MemSetInst &I) { |
| 1720 | IRBuilder<> IRB(&I); |
| 1721 | IRB.CreateCall3( |
| 1722 | MS.MemsetFn, |
| 1723 | IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()), |
| 1724 | IRB.CreateIntCast(I.getArgOperand(1), IRB.getInt32Ty(), false), |
| 1725 | IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)); |
| 1726 | I.eraseFromParent(); |
| 1727 | } |
| 1728 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1729 | void visitVAStartInst(VAStartInst &I) { |
| 1730 | VAHelper->visitVAStartInst(I); |
| 1731 | } |
| 1732 | |
| 1733 | void visitVACopyInst(VACopyInst &I) { |
| 1734 | VAHelper->visitVACopyInst(I); |
| 1735 | } |
| 1736 | |
Evgeniy Stepanov | d7571cd | 2012-12-19 11:22:04 +0000 | [diff] [blame] | 1737 | enum IntrinsicKind { |
| 1738 | IK_DoesNotAccessMemory, |
| 1739 | IK_OnlyReadsMemory, |
| 1740 | IK_WritesMemory |
| 1741 | }; |
| 1742 | |
| 1743 | static IntrinsicKind getIntrinsicKind(Intrinsic::ID iid) { |
| 1744 | const int DoesNotAccessMemory = IK_DoesNotAccessMemory; |
| 1745 | const int OnlyReadsArgumentPointees = IK_OnlyReadsMemory; |
| 1746 | const int OnlyReadsMemory = IK_OnlyReadsMemory; |
| 1747 | const int OnlyAccessesArgumentPointees = IK_WritesMemory; |
| 1748 | const int UnknownModRefBehavior = IK_WritesMemory; |
| 1749 | #define GET_INTRINSIC_MODREF_BEHAVIOR |
| 1750 | #define ModRefBehavior IntrinsicKind |
Chandler Carruth | db25c6c | 2013-01-02 12:09:16 +0000 | [diff] [blame] | 1751 | #include "llvm/IR/Intrinsics.gen" |
Evgeniy Stepanov | d7571cd | 2012-12-19 11:22:04 +0000 | [diff] [blame] | 1752 | #undef ModRefBehavior |
| 1753 | #undef GET_INTRINSIC_MODREF_BEHAVIOR |
| 1754 | } |
| 1755 | |
| 1756 | /// \brief Handle vector store-like intrinsics. |
| 1757 | /// |
| 1758 | /// Instrument intrinsics that look like a simple SIMD store: writes memory, |
| 1759 | /// has 1 pointer argument and 1 vector argument, returns void. |
| 1760 | bool handleVectorStoreIntrinsic(IntrinsicInst &I) { |
| 1761 | IRBuilder<> IRB(&I); |
| 1762 | Value* Addr = I.getArgOperand(0); |
| 1763 | Value *Shadow = getShadow(&I, 1); |
| 1764 | Value *ShadowPtr = getShadowPtr(Addr, Shadow->getType(), IRB); |
| 1765 | |
| 1766 | // We don't know the pointer alignment (could be unaligned SSE store!). |
| 1767 | // Have to assume to worst case. |
| 1768 | IRB.CreateAlignedStore(Shadow, ShadowPtr, 1); |
| 1769 | |
| 1770 | if (ClCheckAccessAddress) |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1771 | insertShadowCheck(Addr, &I); |
Evgeniy Stepanov | d7571cd | 2012-12-19 11:22:04 +0000 | [diff] [blame] | 1772 | |
| 1773 | // FIXME: use ClStoreCleanOrigin |
| 1774 | // FIXME: factor out common code from materializeStores |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 1775 | if (MS.TrackOrigins) |
Evgeniy Stepanov | d7571cd | 2012-12-19 11:22:04 +0000 | [diff] [blame] | 1776 | IRB.CreateStore(getOrigin(&I, 1), getOriginPtr(Addr, IRB)); |
| 1777 | return true; |
| 1778 | } |
| 1779 | |
| 1780 | /// \brief Handle vector load-like intrinsics. |
| 1781 | /// |
| 1782 | /// Instrument intrinsics that look like a simple SIMD load: reads memory, |
| 1783 | /// has 1 pointer argument, returns a vector. |
| 1784 | bool handleVectorLoadIntrinsic(IntrinsicInst &I) { |
| 1785 | IRBuilder<> IRB(&I); |
| 1786 | Value *Addr = I.getArgOperand(0); |
| 1787 | |
| 1788 | Type *ShadowTy = getShadowTy(&I); |
Evgeniy Stepanov | 174242c | 2014-07-03 11:56:30 +0000 | [diff] [blame] | 1789 | if (PropagateShadow) { |
Evgeniy Stepanov | 00062b4 | 2013-02-28 11:25:14 +0000 | [diff] [blame] | 1790 | Value *ShadowPtr = getShadowPtr(Addr, ShadowTy, IRB); |
| 1791 | // We don't know the pointer alignment (could be unaligned SSE load!). |
| 1792 | // Have to assume to worst case. |
| 1793 | setShadow(&I, IRB.CreateAlignedLoad(ShadowPtr, 1, "_msld")); |
| 1794 | } else { |
| 1795 | setShadow(&I, getCleanShadow(&I)); |
| 1796 | } |
| 1797 | |
Evgeniy Stepanov | d7571cd | 2012-12-19 11:22:04 +0000 | [diff] [blame] | 1798 | if (ClCheckAccessAddress) |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1799 | insertShadowCheck(Addr, &I); |
Evgeniy Stepanov | d7571cd | 2012-12-19 11:22:04 +0000 | [diff] [blame] | 1800 | |
Evgeniy Stepanov | 00062b4 | 2013-02-28 11:25:14 +0000 | [diff] [blame] | 1801 | if (MS.TrackOrigins) { |
Evgeniy Stepanov | 174242c | 2014-07-03 11:56:30 +0000 | [diff] [blame] | 1802 | if (PropagateShadow) |
Evgeniy Stepanov | 00062b4 | 2013-02-28 11:25:14 +0000 | [diff] [blame] | 1803 | setOrigin(&I, IRB.CreateLoad(getOriginPtr(Addr, IRB))); |
| 1804 | else |
| 1805 | setOrigin(&I, getCleanOrigin()); |
| 1806 | } |
Evgeniy Stepanov | d7571cd | 2012-12-19 11:22:04 +0000 | [diff] [blame] | 1807 | return true; |
| 1808 | } |
| 1809 | |
| 1810 | /// \brief Handle (SIMD arithmetic)-like intrinsics. |
| 1811 | /// |
| 1812 | /// Instrument intrinsics with any number of arguments of the same type, |
| 1813 | /// equal to the return type. The type should be simple (no aggregates or |
| 1814 | /// pointers; vectors are fine). |
| 1815 | /// Caller guarantees that this intrinsic does not access memory. |
| 1816 | bool maybeHandleSimpleNomemIntrinsic(IntrinsicInst &I) { |
| 1817 | Type *RetTy = I.getType(); |
| 1818 | if (!(RetTy->isIntOrIntVectorTy() || |
| 1819 | RetTy->isFPOrFPVectorTy() || |
| 1820 | RetTy->isX86_MMXTy())) |
| 1821 | return false; |
| 1822 | |
| 1823 | unsigned NumArgOperands = I.getNumArgOperands(); |
| 1824 | |
| 1825 | for (unsigned i = 0; i < NumArgOperands; ++i) { |
| 1826 | Type *Ty = I.getArgOperand(i)->getType(); |
| 1827 | if (Ty != RetTy) |
| 1828 | return false; |
| 1829 | } |
| 1830 | |
| 1831 | IRBuilder<> IRB(&I); |
| 1832 | ShadowAndOriginCombiner SC(this, IRB); |
| 1833 | for (unsigned i = 0; i < NumArgOperands; ++i) |
| 1834 | SC.Add(I.getArgOperand(i)); |
| 1835 | SC.Done(&I); |
| 1836 | |
| 1837 | return true; |
| 1838 | } |
| 1839 | |
| 1840 | /// \brief Heuristically instrument unknown intrinsics. |
| 1841 | /// |
| 1842 | /// The main purpose of this code is to do something reasonable with all |
| 1843 | /// random intrinsics we might encounter, most importantly - SIMD intrinsics. |
| 1844 | /// We recognize several classes of intrinsics by their argument types and |
| 1845 | /// ModRefBehaviour and apply special intrumentation when we are reasonably |
| 1846 | /// sure that we know what the intrinsic does. |
| 1847 | /// |
| 1848 | /// We special-case intrinsics where this approach fails. See llvm.bswap |
| 1849 | /// handling as an example of that. |
| 1850 | bool handleUnknownIntrinsic(IntrinsicInst &I) { |
| 1851 | unsigned NumArgOperands = I.getNumArgOperands(); |
| 1852 | if (NumArgOperands == 0) |
| 1853 | return false; |
| 1854 | |
| 1855 | Intrinsic::ID iid = I.getIntrinsicID(); |
| 1856 | IntrinsicKind IK = getIntrinsicKind(iid); |
| 1857 | bool OnlyReadsMemory = IK == IK_OnlyReadsMemory; |
| 1858 | bool WritesMemory = IK == IK_WritesMemory; |
| 1859 | assert(!(OnlyReadsMemory && WritesMemory)); |
| 1860 | |
| 1861 | if (NumArgOperands == 2 && |
| 1862 | I.getArgOperand(0)->getType()->isPointerTy() && |
| 1863 | I.getArgOperand(1)->getType()->isVectorTy() && |
| 1864 | I.getType()->isVoidTy() && |
| 1865 | WritesMemory) { |
| 1866 | // This looks like a vector store. |
| 1867 | return handleVectorStoreIntrinsic(I); |
| 1868 | } |
| 1869 | |
| 1870 | if (NumArgOperands == 1 && |
| 1871 | I.getArgOperand(0)->getType()->isPointerTy() && |
| 1872 | I.getType()->isVectorTy() && |
| 1873 | OnlyReadsMemory) { |
| 1874 | // This looks like a vector load. |
| 1875 | return handleVectorLoadIntrinsic(I); |
| 1876 | } |
| 1877 | |
| 1878 | if (!OnlyReadsMemory && !WritesMemory) |
| 1879 | if (maybeHandleSimpleNomemIntrinsic(I)) |
| 1880 | return true; |
| 1881 | |
| 1882 | // FIXME: detect and handle SSE maskstore/maskload |
| 1883 | return false; |
| 1884 | } |
| 1885 | |
Evgeniy Stepanov | 8b51bab | 2012-12-05 14:39:55 +0000 | [diff] [blame] | 1886 | void handleBswap(IntrinsicInst &I) { |
| 1887 | IRBuilder<> IRB(&I); |
| 1888 | Value *Op = I.getArgOperand(0); |
| 1889 | Type *OpType = Op->getType(); |
| 1890 | Function *BswapFunc = Intrinsic::getDeclaration( |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 1891 | F.getParent(), Intrinsic::bswap, makeArrayRef(&OpType, 1)); |
Evgeniy Stepanov | 8b51bab | 2012-12-05 14:39:55 +0000 | [diff] [blame] | 1892 | setShadow(&I, IRB.CreateCall(BswapFunc, getShadow(Op))); |
| 1893 | setOrigin(&I, getOrigin(Op)); |
| 1894 | } |
| 1895 | |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1896 | // \brief Instrument vector convert instrinsic. |
| 1897 | // |
| 1898 | // This function instruments intrinsics like cvtsi2ss: |
| 1899 | // %Out = int_xxx_cvtyyy(%ConvertOp) |
| 1900 | // or |
| 1901 | // %Out = int_xxx_cvtyyy(%CopyOp, %ConvertOp) |
| 1902 | // Intrinsic converts \p NumUsedElements elements of \p ConvertOp to the same |
| 1903 | // number \p Out elements, and (if has 2 arguments) copies the rest of the |
| 1904 | // elements from \p CopyOp. |
| 1905 | // In most cases conversion involves floating-point value which may trigger a |
| 1906 | // hardware exception when not fully initialized. For this reason we require |
| 1907 | // \p ConvertOp[0:NumUsedElements] to be fully initialized and trap otherwise. |
| 1908 | // We copy the shadow of \p CopyOp[NumUsedElements:] to \p |
| 1909 | // Out[NumUsedElements:]. This means that intrinsics without \p CopyOp always |
| 1910 | // return a fully initialized value. |
| 1911 | void handleVectorConvertIntrinsic(IntrinsicInst &I, int NumUsedElements) { |
| 1912 | IRBuilder<> IRB(&I); |
| 1913 | Value *CopyOp, *ConvertOp; |
| 1914 | |
| 1915 | switch (I.getNumArgOperands()) { |
| 1916 | case 2: |
| 1917 | CopyOp = I.getArgOperand(0); |
| 1918 | ConvertOp = I.getArgOperand(1); |
| 1919 | break; |
| 1920 | case 1: |
| 1921 | ConvertOp = I.getArgOperand(0); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1922 | CopyOp = nullptr; |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1923 | break; |
| 1924 | default: |
| 1925 | llvm_unreachable("Cvt intrinsic with unsupported number of arguments."); |
| 1926 | } |
| 1927 | |
| 1928 | // The first *NumUsedElements* elements of ConvertOp are converted to the |
| 1929 | // same number of output elements. The rest of the output is copied from |
| 1930 | // CopyOp, or (if not available) filled with zeroes. |
| 1931 | // Combine shadow for elements of ConvertOp that are used in this operation, |
| 1932 | // and insert a check. |
| 1933 | // FIXME: consider propagating shadow of ConvertOp, at least in the case of |
| 1934 | // int->any conversion. |
| 1935 | Value *ConvertShadow = getShadow(ConvertOp); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1936 | Value *AggShadow = nullptr; |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 1937 | if (ConvertOp->getType()->isVectorTy()) { |
| 1938 | AggShadow = IRB.CreateExtractElement( |
| 1939 | ConvertShadow, ConstantInt::get(IRB.getInt32Ty(), 0)); |
| 1940 | for (int i = 1; i < NumUsedElements; ++i) { |
| 1941 | Value *MoreShadow = IRB.CreateExtractElement( |
| 1942 | ConvertShadow, ConstantInt::get(IRB.getInt32Ty(), i)); |
| 1943 | AggShadow = IRB.CreateOr(AggShadow, MoreShadow); |
| 1944 | } |
| 1945 | } else { |
| 1946 | AggShadow = ConvertShadow; |
| 1947 | } |
| 1948 | assert(AggShadow->getType()->isIntegerTy()); |
| 1949 | insertShadowCheck(AggShadow, getOrigin(ConvertOp), &I); |
| 1950 | |
| 1951 | // Build result shadow by zero-filling parts of CopyOp shadow that come from |
| 1952 | // ConvertOp. |
| 1953 | if (CopyOp) { |
| 1954 | assert(CopyOp->getType() == I.getType()); |
| 1955 | assert(CopyOp->getType()->isVectorTy()); |
| 1956 | Value *ResultShadow = getShadow(CopyOp); |
| 1957 | Type *EltTy = ResultShadow->getType()->getVectorElementType(); |
| 1958 | for (int i = 0; i < NumUsedElements; ++i) { |
| 1959 | ResultShadow = IRB.CreateInsertElement( |
| 1960 | ResultShadow, ConstantInt::getNullValue(EltTy), |
| 1961 | ConstantInt::get(IRB.getInt32Ty(), i)); |
| 1962 | } |
| 1963 | setShadow(&I, ResultShadow); |
| 1964 | setOrigin(&I, getOrigin(CopyOp)); |
| 1965 | } else { |
| 1966 | setShadow(&I, getCleanShadow(&I)); |
| 1967 | } |
| 1968 | } |
| 1969 | |
Evgeniy Stepanov | 77be532 | 2014-03-03 13:47:42 +0000 | [diff] [blame] | 1970 | // Given a scalar or vector, extract lower 64 bits (or less), and return all |
| 1971 | // zeroes if it is zero, and all ones otherwise. |
| 1972 | Value *Lower64ShadowExtend(IRBuilder<> &IRB, Value *S, Type *T) { |
| 1973 | if (S->getType()->isVectorTy()) |
| 1974 | S = CreateShadowCast(IRB, S, IRB.getInt64Ty(), /* Signed */ true); |
| 1975 | assert(S->getType()->getPrimitiveSizeInBits() <= 64); |
| 1976 | Value *S2 = IRB.CreateICmpNE(S, getCleanShadow(S)); |
| 1977 | return CreateShadowCast(IRB, S2, T, /* Signed */ true); |
| 1978 | } |
| 1979 | |
| 1980 | Value *VariableShadowExtend(IRBuilder<> &IRB, Value *S) { |
| 1981 | Type *T = S->getType(); |
| 1982 | assert(T->isVectorTy()); |
| 1983 | Value *S2 = IRB.CreateICmpNE(S, getCleanShadow(S)); |
| 1984 | return IRB.CreateSExt(S2, T); |
| 1985 | } |
| 1986 | |
| 1987 | // \brief Instrument vector shift instrinsic. |
| 1988 | // |
| 1989 | // This function instruments intrinsics like int_x86_avx2_psll_w. |
| 1990 | // Intrinsic shifts %In by %ShiftSize bits. |
| 1991 | // %ShiftSize may be a vector. In that case the lower 64 bits determine shift |
| 1992 | // size, and the rest is ignored. Behavior is defined even if shift size is |
| 1993 | // greater than register (or field) width. |
| 1994 | void handleVectorShiftIntrinsic(IntrinsicInst &I, bool Variable) { |
| 1995 | assert(I.getNumArgOperands() == 2); |
| 1996 | IRBuilder<> IRB(&I); |
| 1997 | // If any of the S2 bits are poisoned, the whole thing is poisoned. |
| 1998 | // Otherwise perform the same shift on S1. |
| 1999 | Value *S1 = getShadow(&I, 0); |
| 2000 | Value *S2 = getShadow(&I, 1); |
| 2001 | Value *S2Conv = Variable ? VariableShadowExtend(IRB, S2) |
| 2002 | : Lower64ShadowExtend(IRB, S2, getShadowTy(&I)); |
| 2003 | Value *V1 = I.getOperand(0); |
| 2004 | Value *V2 = I.getOperand(1); |
| 2005 | Value *Shift = IRB.CreateCall2(I.getCalledValue(), |
| 2006 | IRB.CreateBitCast(S1, V1->getType()), V2); |
| 2007 | Shift = IRB.CreateBitCast(Shift, getShadowTy(&I)); |
| 2008 | setShadow(&I, IRB.CreateOr(Shift, S2Conv)); |
| 2009 | setOriginForNaryOp(I); |
| 2010 | } |
| 2011 | |
Evgeniy Stepanov | f7c29a9 | 2014-06-09 08:40:16 +0000 | [diff] [blame] | 2012 | // \brief Get an X86_MMX-sized vector type. |
| 2013 | Type *getMMXVectorTy(unsigned EltSizeInBits) { |
| 2014 | const unsigned X86_MMXSizeInBits = 64; |
| 2015 | return VectorType::get(IntegerType::get(*MS.C, EltSizeInBits), |
| 2016 | X86_MMXSizeInBits / EltSizeInBits); |
| 2017 | } |
| 2018 | |
| 2019 | // \brief Returns a signed counterpart for an (un)signed-saturate-and-pack |
| 2020 | // intrinsic. |
| 2021 | Intrinsic::ID getSignedPackIntrinsic(Intrinsic::ID id) { |
| 2022 | switch (id) { |
| 2023 | case llvm::Intrinsic::x86_sse2_packsswb_128: |
| 2024 | case llvm::Intrinsic::x86_sse2_packuswb_128: |
| 2025 | return llvm::Intrinsic::x86_sse2_packsswb_128; |
| 2026 | |
| 2027 | case llvm::Intrinsic::x86_sse2_packssdw_128: |
| 2028 | case llvm::Intrinsic::x86_sse41_packusdw: |
| 2029 | return llvm::Intrinsic::x86_sse2_packssdw_128; |
| 2030 | |
| 2031 | case llvm::Intrinsic::x86_avx2_packsswb: |
| 2032 | case llvm::Intrinsic::x86_avx2_packuswb: |
| 2033 | return llvm::Intrinsic::x86_avx2_packsswb; |
| 2034 | |
| 2035 | case llvm::Intrinsic::x86_avx2_packssdw: |
| 2036 | case llvm::Intrinsic::x86_avx2_packusdw: |
| 2037 | return llvm::Intrinsic::x86_avx2_packssdw; |
| 2038 | |
| 2039 | case llvm::Intrinsic::x86_mmx_packsswb: |
| 2040 | case llvm::Intrinsic::x86_mmx_packuswb: |
| 2041 | return llvm::Intrinsic::x86_mmx_packsswb; |
| 2042 | |
| 2043 | case llvm::Intrinsic::x86_mmx_packssdw: |
| 2044 | return llvm::Intrinsic::x86_mmx_packssdw; |
| 2045 | default: |
| 2046 | llvm_unreachable("unexpected intrinsic id"); |
| 2047 | } |
| 2048 | } |
| 2049 | |
Evgeniy Stepanov | 5d97293 | 2014-06-17 11:26:00 +0000 | [diff] [blame] | 2050 | // \brief Instrument vector pack instrinsic. |
Evgeniy Stepanov | d425a2b | 2014-06-02 12:31:44 +0000 | [diff] [blame] | 2051 | // |
| 2052 | // This function instruments intrinsics like x86_mmx_packsswb, that |
Evgeniy Stepanov | 5d97293 | 2014-06-17 11:26:00 +0000 | [diff] [blame] | 2053 | // packs elements of 2 input vectors into half as many bits with saturation. |
Evgeniy Stepanov | f7c29a9 | 2014-06-09 08:40:16 +0000 | [diff] [blame] | 2054 | // Shadow is propagated with the signed variant of the same intrinsic applied |
| 2055 | // to sext(Sa != zeroinitializer), sext(Sb != zeroinitializer). |
| 2056 | // EltSizeInBits is used only for x86mmx arguments. |
| 2057 | void handleVectorPackIntrinsic(IntrinsicInst &I, unsigned EltSizeInBits = 0) { |
Evgeniy Stepanov | d425a2b | 2014-06-02 12:31:44 +0000 | [diff] [blame] | 2058 | assert(I.getNumArgOperands() == 2); |
Evgeniy Stepanov | f7c29a9 | 2014-06-09 08:40:16 +0000 | [diff] [blame] | 2059 | bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy(); |
Evgeniy Stepanov | d425a2b | 2014-06-02 12:31:44 +0000 | [diff] [blame] | 2060 | IRBuilder<> IRB(&I); |
| 2061 | Value *S1 = getShadow(&I, 0); |
| 2062 | Value *S2 = getShadow(&I, 1); |
Evgeniy Stepanov | f7c29a9 | 2014-06-09 08:40:16 +0000 | [diff] [blame] | 2063 | assert(isX86_MMX || S1->getType()->isVectorTy()); |
| 2064 | |
| 2065 | // SExt and ICmpNE below must apply to individual elements of input vectors. |
| 2066 | // In case of x86mmx arguments, cast them to appropriate vector types and |
| 2067 | // back. |
| 2068 | Type *T = isX86_MMX ? getMMXVectorTy(EltSizeInBits) : S1->getType(); |
| 2069 | if (isX86_MMX) { |
| 2070 | S1 = IRB.CreateBitCast(S1, T); |
| 2071 | S2 = IRB.CreateBitCast(S2, T); |
| 2072 | } |
Evgeniy Stepanov | d425a2b | 2014-06-02 12:31:44 +0000 | [diff] [blame] | 2073 | Value *S1_ext = IRB.CreateSExt( |
| 2074 | IRB.CreateICmpNE(S1, llvm::Constant::getNullValue(T)), T); |
| 2075 | Value *S2_ext = IRB.CreateSExt( |
| 2076 | IRB.CreateICmpNE(S2, llvm::Constant::getNullValue(T)), T); |
Evgeniy Stepanov | f7c29a9 | 2014-06-09 08:40:16 +0000 | [diff] [blame] | 2077 | if (isX86_MMX) { |
| 2078 | Type *X86_MMXTy = Type::getX86_MMXTy(*MS.C); |
| 2079 | S1_ext = IRB.CreateBitCast(S1_ext, X86_MMXTy); |
| 2080 | S2_ext = IRB.CreateBitCast(S2_ext, X86_MMXTy); |
| 2081 | } |
| 2082 | |
| 2083 | Function *ShadowFn = Intrinsic::getDeclaration( |
| 2084 | F.getParent(), getSignedPackIntrinsic(I.getIntrinsicID())); |
| 2085 | |
| 2086 | Value *S = IRB.CreateCall2(ShadowFn, S1_ext, S2_ext, "_msprop_vector_pack"); |
| 2087 | if (isX86_MMX) S = IRB.CreateBitCast(S, getShadowTy(&I)); |
Evgeniy Stepanov | d425a2b | 2014-06-02 12:31:44 +0000 | [diff] [blame] | 2088 | setShadow(&I, S); |
| 2089 | setOriginForNaryOp(I); |
| 2090 | } |
| 2091 | |
Evgeniy Stepanov | 4ea1647 | 2014-06-18 12:02:29 +0000 | [diff] [blame] | 2092 | // \brief Instrument sum-of-absolute-differencies intrinsic. |
| 2093 | void handleVectorSadIntrinsic(IntrinsicInst &I) { |
| 2094 | const unsigned SignificantBitsPerResultElement = 16; |
| 2095 | bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy(); |
| 2096 | Type *ResTy = isX86_MMX ? IntegerType::get(*MS.C, 64) : I.getType(); |
| 2097 | unsigned ZeroBitsPerResultElement = |
| 2098 | ResTy->getScalarSizeInBits() - SignificantBitsPerResultElement; |
| 2099 | |
| 2100 | IRBuilder<> IRB(&I); |
| 2101 | Value *S = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1)); |
| 2102 | S = IRB.CreateBitCast(S, ResTy); |
| 2103 | S = IRB.CreateSExt(IRB.CreateICmpNE(S, Constant::getNullValue(ResTy)), |
| 2104 | ResTy); |
| 2105 | S = IRB.CreateLShr(S, ZeroBitsPerResultElement); |
| 2106 | S = IRB.CreateBitCast(S, getShadowTy(&I)); |
| 2107 | setShadow(&I, S); |
| 2108 | setOriginForNaryOp(I); |
| 2109 | } |
| 2110 | |
| 2111 | // \brief Instrument multiply-add intrinsic. |
| 2112 | void handleVectorPmaddIntrinsic(IntrinsicInst &I, |
| 2113 | unsigned EltSizeInBits = 0) { |
| 2114 | bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy(); |
| 2115 | Type *ResTy = isX86_MMX ? getMMXVectorTy(EltSizeInBits * 2) : I.getType(); |
| 2116 | IRBuilder<> IRB(&I); |
| 2117 | Value *S = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1)); |
| 2118 | S = IRB.CreateBitCast(S, ResTy); |
| 2119 | S = IRB.CreateSExt(IRB.CreateICmpNE(S, Constant::getNullValue(ResTy)), |
| 2120 | ResTy); |
| 2121 | S = IRB.CreateBitCast(S, getShadowTy(&I)); |
| 2122 | setShadow(&I, S); |
| 2123 | setOriginForNaryOp(I); |
| 2124 | } |
| 2125 | |
Evgeniy Stepanov | 8b51bab | 2012-12-05 14:39:55 +0000 | [diff] [blame] | 2126 | void visitIntrinsicInst(IntrinsicInst &I) { |
| 2127 | switch (I.getIntrinsicID()) { |
| 2128 | case llvm::Intrinsic::bswap: |
Evgeniy Stepanov | 9b72e99 | 2012-12-14 13:48:31 +0000 | [diff] [blame] | 2129 | handleBswap(I); |
| 2130 | break; |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 2131 | case llvm::Intrinsic::x86_avx512_cvtsd2usi64: |
| 2132 | case llvm::Intrinsic::x86_avx512_cvtsd2usi: |
| 2133 | case llvm::Intrinsic::x86_avx512_cvtss2usi64: |
| 2134 | case llvm::Intrinsic::x86_avx512_cvtss2usi: |
| 2135 | case llvm::Intrinsic::x86_avx512_cvttss2usi64: |
| 2136 | case llvm::Intrinsic::x86_avx512_cvttss2usi: |
| 2137 | case llvm::Intrinsic::x86_avx512_cvttsd2usi64: |
| 2138 | case llvm::Intrinsic::x86_avx512_cvttsd2usi: |
| 2139 | case llvm::Intrinsic::x86_avx512_cvtusi2sd: |
| 2140 | case llvm::Intrinsic::x86_avx512_cvtusi2ss: |
| 2141 | case llvm::Intrinsic::x86_avx512_cvtusi642sd: |
| 2142 | case llvm::Intrinsic::x86_avx512_cvtusi642ss: |
| 2143 | case llvm::Intrinsic::x86_sse2_cvtsd2si64: |
| 2144 | case llvm::Intrinsic::x86_sse2_cvtsd2si: |
| 2145 | case llvm::Intrinsic::x86_sse2_cvtsd2ss: |
| 2146 | case llvm::Intrinsic::x86_sse2_cvtsi2sd: |
| 2147 | case llvm::Intrinsic::x86_sse2_cvtsi642sd: |
| 2148 | case llvm::Intrinsic::x86_sse2_cvtss2sd: |
| 2149 | case llvm::Intrinsic::x86_sse2_cvttsd2si64: |
| 2150 | case llvm::Intrinsic::x86_sse2_cvttsd2si: |
| 2151 | case llvm::Intrinsic::x86_sse_cvtsi2ss: |
| 2152 | case llvm::Intrinsic::x86_sse_cvtsi642ss: |
| 2153 | case llvm::Intrinsic::x86_sse_cvtss2si64: |
| 2154 | case llvm::Intrinsic::x86_sse_cvtss2si: |
| 2155 | case llvm::Intrinsic::x86_sse_cvttss2si64: |
| 2156 | case llvm::Intrinsic::x86_sse_cvttss2si: |
| 2157 | handleVectorConvertIntrinsic(I, 1); |
| 2158 | break; |
| 2159 | case llvm::Intrinsic::x86_sse2_cvtdq2pd: |
| 2160 | case llvm::Intrinsic::x86_sse2_cvtps2pd: |
| 2161 | case llvm::Intrinsic::x86_sse_cvtps2pi: |
| 2162 | case llvm::Intrinsic::x86_sse_cvttps2pi: |
| 2163 | handleVectorConvertIntrinsic(I, 2); |
| 2164 | break; |
Evgeniy Stepanov | 77be532 | 2014-03-03 13:47:42 +0000 | [diff] [blame] | 2165 | case llvm::Intrinsic::x86_avx512_psll_dq: |
| 2166 | case llvm::Intrinsic::x86_avx512_psrl_dq: |
| 2167 | case llvm::Intrinsic::x86_avx2_psll_w: |
| 2168 | case llvm::Intrinsic::x86_avx2_psll_d: |
| 2169 | case llvm::Intrinsic::x86_avx2_psll_q: |
| 2170 | case llvm::Intrinsic::x86_avx2_pslli_w: |
| 2171 | case llvm::Intrinsic::x86_avx2_pslli_d: |
| 2172 | case llvm::Intrinsic::x86_avx2_pslli_q: |
| 2173 | case llvm::Intrinsic::x86_avx2_psll_dq: |
| 2174 | case llvm::Intrinsic::x86_avx2_psrl_w: |
| 2175 | case llvm::Intrinsic::x86_avx2_psrl_d: |
| 2176 | case llvm::Intrinsic::x86_avx2_psrl_q: |
| 2177 | case llvm::Intrinsic::x86_avx2_psra_w: |
| 2178 | case llvm::Intrinsic::x86_avx2_psra_d: |
| 2179 | case llvm::Intrinsic::x86_avx2_psrli_w: |
| 2180 | case llvm::Intrinsic::x86_avx2_psrli_d: |
| 2181 | case llvm::Intrinsic::x86_avx2_psrli_q: |
| 2182 | case llvm::Intrinsic::x86_avx2_psrai_w: |
| 2183 | case llvm::Intrinsic::x86_avx2_psrai_d: |
| 2184 | case llvm::Intrinsic::x86_avx2_psrl_dq: |
| 2185 | case llvm::Intrinsic::x86_sse2_psll_w: |
| 2186 | case llvm::Intrinsic::x86_sse2_psll_d: |
| 2187 | case llvm::Intrinsic::x86_sse2_psll_q: |
| 2188 | case llvm::Intrinsic::x86_sse2_pslli_w: |
| 2189 | case llvm::Intrinsic::x86_sse2_pslli_d: |
| 2190 | case llvm::Intrinsic::x86_sse2_pslli_q: |
| 2191 | case llvm::Intrinsic::x86_sse2_psll_dq: |
| 2192 | case llvm::Intrinsic::x86_sse2_psrl_w: |
| 2193 | case llvm::Intrinsic::x86_sse2_psrl_d: |
| 2194 | case llvm::Intrinsic::x86_sse2_psrl_q: |
| 2195 | case llvm::Intrinsic::x86_sse2_psra_w: |
| 2196 | case llvm::Intrinsic::x86_sse2_psra_d: |
| 2197 | case llvm::Intrinsic::x86_sse2_psrli_w: |
| 2198 | case llvm::Intrinsic::x86_sse2_psrli_d: |
| 2199 | case llvm::Intrinsic::x86_sse2_psrli_q: |
| 2200 | case llvm::Intrinsic::x86_sse2_psrai_w: |
| 2201 | case llvm::Intrinsic::x86_sse2_psrai_d: |
| 2202 | case llvm::Intrinsic::x86_sse2_psrl_dq: |
| 2203 | case llvm::Intrinsic::x86_mmx_psll_w: |
| 2204 | case llvm::Intrinsic::x86_mmx_psll_d: |
| 2205 | case llvm::Intrinsic::x86_mmx_psll_q: |
| 2206 | case llvm::Intrinsic::x86_mmx_pslli_w: |
| 2207 | case llvm::Intrinsic::x86_mmx_pslli_d: |
| 2208 | case llvm::Intrinsic::x86_mmx_pslli_q: |
| 2209 | case llvm::Intrinsic::x86_mmx_psrl_w: |
| 2210 | case llvm::Intrinsic::x86_mmx_psrl_d: |
| 2211 | case llvm::Intrinsic::x86_mmx_psrl_q: |
| 2212 | case llvm::Intrinsic::x86_mmx_psra_w: |
| 2213 | case llvm::Intrinsic::x86_mmx_psra_d: |
| 2214 | case llvm::Intrinsic::x86_mmx_psrli_w: |
| 2215 | case llvm::Intrinsic::x86_mmx_psrli_d: |
| 2216 | case llvm::Intrinsic::x86_mmx_psrli_q: |
| 2217 | case llvm::Intrinsic::x86_mmx_psrai_w: |
| 2218 | case llvm::Intrinsic::x86_mmx_psrai_d: |
| 2219 | handleVectorShiftIntrinsic(I, /* Variable */ false); |
| 2220 | break; |
| 2221 | case llvm::Intrinsic::x86_avx2_psllv_d: |
| 2222 | case llvm::Intrinsic::x86_avx2_psllv_d_256: |
| 2223 | case llvm::Intrinsic::x86_avx2_psllv_q: |
| 2224 | case llvm::Intrinsic::x86_avx2_psllv_q_256: |
| 2225 | case llvm::Intrinsic::x86_avx2_psrlv_d: |
| 2226 | case llvm::Intrinsic::x86_avx2_psrlv_d_256: |
| 2227 | case llvm::Intrinsic::x86_avx2_psrlv_q: |
| 2228 | case llvm::Intrinsic::x86_avx2_psrlv_q_256: |
| 2229 | case llvm::Intrinsic::x86_avx2_psrav_d: |
| 2230 | case llvm::Intrinsic::x86_avx2_psrav_d_256: |
| 2231 | handleVectorShiftIntrinsic(I, /* Variable */ true); |
| 2232 | break; |
| 2233 | |
| 2234 | // Byte shifts are not implemented. |
| 2235 | // case llvm::Intrinsic::x86_avx512_psll_dq_bs: |
| 2236 | // case llvm::Intrinsic::x86_avx512_psrl_dq_bs: |
| 2237 | // case llvm::Intrinsic::x86_avx2_psll_dq_bs: |
| 2238 | // case llvm::Intrinsic::x86_avx2_psrl_dq_bs: |
| 2239 | // case llvm::Intrinsic::x86_sse2_psll_dq_bs: |
| 2240 | // case llvm::Intrinsic::x86_sse2_psrl_dq_bs: |
| 2241 | |
Evgeniy Stepanov | d425a2b | 2014-06-02 12:31:44 +0000 | [diff] [blame] | 2242 | case llvm::Intrinsic::x86_sse2_packsswb_128: |
| 2243 | case llvm::Intrinsic::x86_sse2_packssdw_128: |
| 2244 | case llvm::Intrinsic::x86_sse2_packuswb_128: |
| 2245 | case llvm::Intrinsic::x86_sse41_packusdw: |
| 2246 | case llvm::Intrinsic::x86_avx2_packsswb: |
| 2247 | case llvm::Intrinsic::x86_avx2_packssdw: |
| 2248 | case llvm::Intrinsic::x86_avx2_packuswb: |
| 2249 | case llvm::Intrinsic::x86_avx2_packusdw: |
Evgeniy Stepanov | d425a2b | 2014-06-02 12:31:44 +0000 | [diff] [blame] | 2250 | handleVectorPackIntrinsic(I); |
| 2251 | break; |
| 2252 | |
Evgeniy Stepanov | f7c29a9 | 2014-06-09 08:40:16 +0000 | [diff] [blame] | 2253 | case llvm::Intrinsic::x86_mmx_packsswb: |
| 2254 | case llvm::Intrinsic::x86_mmx_packuswb: |
| 2255 | handleVectorPackIntrinsic(I, 16); |
| 2256 | break; |
| 2257 | |
| 2258 | case llvm::Intrinsic::x86_mmx_packssdw: |
| 2259 | handleVectorPackIntrinsic(I, 32); |
| 2260 | break; |
| 2261 | |
Evgeniy Stepanov | 4ea1647 | 2014-06-18 12:02:29 +0000 | [diff] [blame] | 2262 | case llvm::Intrinsic::x86_mmx_psad_bw: |
| 2263 | case llvm::Intrinsic::x86_sse2_psad_bw: |
| 2264 | case llvm::Intrinsic::x86_avx2_psad_bw: |
| 2265 | handleVectorSadIntrinsic(I); |
| 2266 | break; |
| 2267 | |
| 2268 | case llvm::Intrinsic::x86_sse2_pmadd_wd: |
| 2269 | case llvm::Intrinsic::x86_avx2_pmadd_wd: |
| 2270 | case llvm::Intrinsic::x86_ssse3_pmadd_ub_sw_128: |
| 2271 | case llvm::Intrinsic::x86_avx2_pmadd_ub_sw: |
| 2272 | handleVectorPmaddIntrinsic(I); |
| 2273 | break; |
| 2274 | |
| 2275 | case llvm::Intrinsic::x86_ssse3_pmadd_ub_sw: |
| 2276 | handleVectorPmaddIntrinsic(I, 8); |
| 2277 | break; |
| 2278 | |
| 2279 | case llvm::Intrinsic::x86_mmx_pmadd_wd: |
| 2280 | handleVectorPmaddIntrinsic(I, 16); |
| 2281 | break; |
| 2282 | |
Evgeniy Stepanov | 8b51bab | 2012-12-05 14:39:55 +0000 | [diff] [blame] | 2283 | default: |
Evgeniy Stepanov | d7571cd | 2012-12-19 11:22:04 +0000 | [diff] [blame] | 2284 | if (!handleUnknownIntrinsic(I)) |
| 2285 | visitInstruction(I); |
Evgeniy Stepanov | 88b8dce | 2012-12-17 16:30:05 +0000 | [diff] [blame] | 2286 | break; |
Evgeniy Stepanov | 8b51bab | 2012-12-05 14:39:55 +0000 | [diff] [blame] | 2287 | } |
| 2288 | } |
| 2289 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2290 | void visitCallSite(CallSite CS) { |
| 2291 | Instruction &I = *CS.getInstruction(); |
| 2292 | assert((CS.isCall() || CS.isInvoke()) && "Unknown type of CallSite"); |
| 2293 | if (CS.isCall()) { |
Evgeniy Stepanov | 7ad7e83 | 2012-11-29 14:32:03 +0000 | [diff] [blame] | 2294 | CallInst *Call = cast<CallInst>(&I); |
| 2295 | |
| 2296 | // For inline asm, do the usual thing: check argument shadow and mark all |
| 2297 | // outputs as clean. Note that any side effects of the inline asm that are |
| 2298 | // not immediately visible in its constraints are not handled. |
| 2299 | if (Call->isInlineAsm()) { |
| 2300 | visitInstruction(I); |
| 2301 | return; |
| 2302 | } |
| 2303 | |
Evgeniy Stepanov | 8b51bab | 2012-12-05 14:39:55 +0000 | [diff] [blame] | 2304 | assert(!isa<IntrinsicInst>(&I) && "intrinsics are handled elsewhere"); |
Evgeniy Stepanov | 383b61e | 2012-12-07 09:08:32 +0000 | [diff] [blame] | 2305 | |
| 2306 | // We are going to insert code that relies on the fact that the callee |
| 2307 | // will become a non-readonly function after it is instrumented by us. To |
| 2308 | // prevent this code from being optimized out, mark that function |
| 2309 | // non-readonly in advance. |
| 2310 | if (Function *Func = Call->getCalledFunction()) { |
| 2311 | // Clear out readonly/readnone attributes. |
| 2312 | AttrBuilder B; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 2313 | B.addAttribute(Attribute::ReadOnly) |
| 2314 | .addAttribute(Attribute::ReadNone); |
Bill Wendling | 430fa9b | 2013-01-23 00:45:55 +0000 | [diff] [blame] | 2315 | Func->removeAttributes(AttributeSet::FunctionIndex, |
| 2316 | AttributeSet::get(Func->getContext(), |
| 2317 | AttributeSet::FunctionIndex, |
| 2318 | B)); |
Evgeniy Stepanov | 383b61e | 2012-12-07 09:08:32 +0000 | [diff] [blame] | 2319 | } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2320 | } |
| 2321 | IRBuilder<> IRB(&I); |
Evgeniy Stepanov | 37b8645 | 2013-09-19 15:22:35 +0000 | [diff] [blame] | 2322 | |
| 2323 | if (MS.WrapIndirectCalls && !CS.getCalledFunction()) |
Evgeniy Stepanov | 585813e | 2013-11-14 12:29:04 +0000 | [diff] [blame] | 2324 | IndirectCallList.push_back(CS); |
Evgeniy Stepanov | 37b8645 | 2013-09-19 15:22:35 +0000 | [diff] [blame] | 2325 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2326 | unsigned ArgOffset = 0; |
| 2327 | DEBUG(dbgs() << " CallSite: " << I << "\n"); |
| 2328 | for (CallSite::arg_iterator ArgIt = CS.arg_begin(), End = CS.arg_end(); |
| 2329 | ArgIt != End; ++ArgIt) { |
| 2330 | Value *A = *ArgIt; |
| 2331 | unsigned i = ArgIt - CS.arg_begin(); |
| 2332 | if (!A->getType()->isSized()) { |
| 2333 | DEBUG(dbgs() << "Arg " << i << " is not sized: " << I << "\n"); |
| 2334 | continue; |
| 2335 | } |
| 2336 | unsigned Size = 0; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2337 | Value *Store = nullptr; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2338 | // Compute the Shadow for arg even if it is ByVal, because |
| 2339 | // in that case getShadow() will copy the actual arg shadow to |
| 2340 | // __msan_param_tls. |
| 2341 | Value *ArgShadow = getShadow(A); |
| 2342 | Value *ArgShadowBase = getShadowPtrForArgument(A, IRB, ArgOffset); |
| 2343 | DEBUG(dbgs() << " Arg#" << i << ": " << *A << |
| 2344 | " Shadow: " << *ArgShadow << "\n"); |
Evgeniy Stepanov | c8227aa | 2014-07-17 09:10:37 +0000 | [diff] [blame] | 2345 | bool ArgIsInitialized = false; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 2346 | if (CS.paramHasAttr(i + 1, Attribute::ByVal)) { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2347 | assert(A->getType()->isPointerTy() && |
| 2348 | "ByVal argument is not a pointer!"); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2349 | Size = MS.DL->getTypeAllocSize(A->getType()->getPointerElementType()); |
Evgeniy Stepanov | 35eb265 | 2014-10-22 00:12:40 +0000 | [diff] [blame^] | 2350 | if (ArgOffset + Size > kParamTLSSize) break; |
Evgeniy Stepanov | e08633e | 2014-10-17 23:29:44 +0000 | [diff] [blame] | 2351 | unsigned ParamAlignment = CS.getParamAlignment(i + 1); |
| 2352 | unsigned Alignment = std::min(ParamAlignment, kShadowTLSAlignment); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2353 | Store = IRB.CreateMemCpy(ArgShadowBase, |
| 2354 | getShadowPtr(A, Type::getInt8Ty(*MS.C), IRB), |
| 2355 | Size, Alignment); |
| 2356 | } else { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2357 | Size = MS.DL->getTypeAllocSize(A->getType()); |
Evgeniy Stepanov | 35eb265 | 2014-10-22 00:12:40 +0000 | [diff] [blame^] | 2358 | if (ArgOffset + Size > kParamTLSSize) break; |
Evgeniy Stepanov | d2bd319 | 2012-12-11 12:34:09 +0000 | [diff] [blame] | 2359 | Store = IRB.CreateAlignedStore(ArgShadow, ArgShadowBase, |
| 2360 | kShadowTLSAlignment); |
Evgeniy Stepanov | c8227aa | 2014-07-17 09:10:37 +0000 | [diff] [blame] | 2361 | Constant *Cst = dyn_cast<Constant>(ArgShadow); |
| 2362 | if (Cst && Cst->isNullValue()) ArgIsInitialized = true; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2363 | } |
Evgeniy Stepanov | c8227aa | 2014-07-17 09:10:37 +0000 | [diff] [blame] | 2364 | if (MS.TrackOrigins && !ArgIsInitialized) |
Evgeniy Stepanov | 49175b2 | 2012-12-14 13:43:11 +0000 | [diff] [blame] | 2365 | IRB.CreateStore(getOrigin(A), |
| 2366 | getOriginPtrForArgument(A, IRB, ArgOffset)); |
Edwin Vane | 82f80d4 | 2013-01-29 17:42:24 +0000 | [diff] [blame] | 2367 | (void)Store; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 2368 | assert(Size != 0 && Store != nullptr); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2369 | DEBUG(dbgs() << " Param:" << *Store << "\n"); |
David Majnemer | f3cadce | 2014-10-20 06:13:33 +0000 | [diff] [blame] | 2370 | ArgOffset += RoundUpToAlignment(Size, 8); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2371 | } |
| 2372 | DEBUG(dbgs() << " done with call args\n"); |
| 2373 | |
| 2374 | FunctionType *FT = |
Evgeniy Stepanov | 37b8645 | 2013-09-19 15:22:35 +0000 | [diff] [blame] | 2375 | cast<FunctionType>(CS.getCalledValue()->getType()->getContainedType(0)); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2376 | if (FT->isVarArg()) { |
| 2377 | VAHelper->visitCallSite(CS, IRB); |
| 2378 | } |
| 2379 | |
| 2380 | // Now, get the shadow for the RetVal. |
| 2381 | if (!I.getType()->isSized()) return; |
| 2382 | IRBuilder<> IRBBefore(&I); |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 2383 | // Until we have full dynamic coverage, make sure the retval shadow is 0. |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2384 | Value *Base = getShadowPtrForRetval(&I, IRBBefore); |
Evgeniy Stepanov | d2bd319 | 2012-12-11 12:34:09 +0000 | [diff] [blame] | 2385 | IRBBefore.CreateAlignedStore(getCleanShadow(&I), Base, kShadowTLSAlignment); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2386 | Instruction *NextInsn = nullptr; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2387 | if (CS.isCall()) { |
| 2388 | NextInsn = I.getNextNode(); |
| 2389 | } else { |
| 2390 | BasicBlock *NormalDest = cast<InvokeInst>(&I)->getNormalDest(); |
| 2391 | if (!NormalDest->getSinglePredecessor()) { |
| 2392 | // FIXME: this case is tricky, so we are just conservative here. |
| 2393 | // Perhaps we need to split the edge between this BB and NormalDest, |
| 2394 | // but a naive attempt to use SplitEdge leads to a crash. |
| 2395 | setShadow(&I, getCleanShadow(&I)); |
| 2396 | setOrigin(&I, getCleanOrigin()); |
| 2397 | return; |
| 2398 | } |
| 2399 | NextInsn = NormalDest->getFirstInsertionPt(); |
| 2400 | assert(NextInsn && |
| 2401 | "Could not find insertion point for retval shadow load"); |
| 2402 | } |
| 2403 | IRBuilder<> IRBAfter(NextInsn); |
Evgeniy Stepanov | d2bd319 | 2012-12-11 12:34:09 +0000 | [diff] [blame] | 2404 | Value *RetvalShadow = |
| 2405 | IRBAfter.CreateAlignedLoad(getShadowPtrForRetval(&I, IRBAfter), |
| 2406 | kShadowTLSAlignment, "_msret"); |
| 2407 | setShadow(&I, RetvalShadow); |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 2408 | if (MS.TrackOrigins) |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2409 | setOrigin(&I, IRBAfter.CreateLoad(getOriginPtrForRetval(IRBAfter))); |
| 2410 | } |
| 2411 | |
| 2412 | void visitReturnInst(ReturnInst &I) { |
| 2413 | IRBuilder<> IRB(&I); |
Evgeniy Stepanov | 604293f | 2013-09-16 13:24:32 +0000 | [diff] [blame] | 2414 | Value *RetVal = I.getReturnValue(); |
| 2415 | if (!RetVal) return; |
| 2416 | Value *ShadowPtr = getShadowPtrForRetval(RetVal, IRB); |
| 2417 | if (CheckReturnValue) { |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 2418 | insertShadowCheck(RetVal, &I); |
Evgeniy Stepanov | 604293f | 2013-09-16 13:24:32 +0000 | [diff] [blame] | 2419 | Value *Shadow = getCleanShadow(RetVal); |
Evgeniy Stepanov | d2bd319 | 2012-12-11 12:34:09 +0000 | [diff] [blame] | 2420 | IRB.CreateAlignedStore(Shadow, ShadowPtr, kShadowTLSAlignment); |
Evgeniy Stepanov | 604293f | 2013-09-16 13:24:32 +0000 | [diff] [blame] | 2421 | } else { |
| 2422 | Value *Shadow = getShadow(RetVal); |
| 2423 | IRB.CreateAlignedStore(Shadow, ShadowPtr, kShadowTLSAlignment); |
| 2424 | // FIXME: make it conditional if ClStoreCleanOrigin==0 |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 2425 | if (MS.TrackOrigins) |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2426 | IRB.CreateStore(getOrigin(RetVal), getOriginPtrForRetval(IRB)); |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | void visitPHINode(PHINode &I) { |
| 2431 | IRBuilder<> IRB(&I); |
Evgeniy Stepanov | d948a5f | 2014-07-07 13:28:31 +0000 | [diff] [blame] | 2432 | if (!PropagateShadow) { |
| 2433 | setShadow(&I, getCleanShadow(&I)); |
| 2434 | return; |
| 2435 | } |
| 2436 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2437 | ShadowPHINodes.push_back(&I); |
| 2438 | setShadow(&I, IRB.CreatePHI(getShadowTy(&I), I.getNumIncomingValues(), |
| 2439 | "_msphi_s")); |
Evgeniy Stepanov | abeae5c | 2012-12-19 13:55:51 +0000 | [diff] [blame] | 2440 | if (MS.TrackOrigins) |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2441 | setOrigin(&I, IRB.CreatePHI(MS.OriginTy, I.getNumIncomingValues(), |
| 2442 | "_msphi_o")); |
| 2443 | } |
| 2444 | |
| 2445 | void visitAllocaInst(AllocaInst &I) { |
| 2446 | setShadow(&I, getCleanShadow(&I)); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2447 | IRBuilder<> IRB(I.getNextNode()); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2448 | uint64_t Size = MS.DL->getTypeAllocSize(I.getAllocatedType()); |
Evgeniy Stepanov | dc6d7eb | 2013-07-03 14:39:14 +0000 | [diff] [blame] | 2449 | if (PoisonStack && ClPoisonStackWithCall) { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2450 | IRB.CreateCall2(MS.MsanPoisonStackFn, |
| 2451 | IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), |
| 2452 | ConstantInt::get(MS.IntptrTy, Size)); |
| 2453 | } else { |
| 2454 | Value *ShadowBase = getShadowPtr(&I, Type::getInt8PtrTy(*MS.C), IRB); |
Evgeniy Stepanov | dc6d7eb | 2013-07-03 14:39:14 +0000 | [diff] [blame] | 2455 | Value *PoisonValue = IRB.getInt8(PoisonStack ? ClPoisonStackPattern : 0); |
| 2456 | IRB.CreateMemSet(ShadowBase, PoisonValue, Size, I.getAlignment()); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Evgeniy Stepanov | dc6d7eb | 2013-07-03 14:39:14 +0000 | [diff] [blame] | 2459 | if (PoisonStack && MS.TrackOrigins) { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2460 | setOrigin(&I, getCleanOrigin()); |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 2461 | SmallString<2048> StackDescriptionStorage; |
| 2462 | raw_svector_ostream StackDescription(StackDescriptionStorage); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2463 | // We create a string with a description of the stack allocation and |
| 2464 | // pass it into __msan_set_alloca_origin. |
| 2465 | // It will be printed by the run-time if stack-originated UMR is found. |
| 2466 | // The first 4 bytes of the string are set to '----' and will be replaced |
| 2467 | // by __msan_va_arg_overflow_size_tls at the first call. |
| 2468 | StackDescription << "----" << I.getName() << "@" << F.getName(); |
| 2469 | Value *Descr = |
| 2470 | createPrivateNonConstGlobalForString(*F.getParent(), |
| 2471 | StackDescription.str()); |
Evgeniy Stepanov | 0435ecd | 2013-09-13 12:54:49 +0000 | [diff] [blame] | 2472 | |
| 2473 | IRB.CreateCall4(MS.MsanSetAllocaOrigin4Fn, |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2474 | IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), |
| 2475 | ConstantInt::get(MS.IntptrTy, Size), |
Evgeniy Stepanov | 0435ecd | 2013-09-13 12:54:49 +0000 | [diff] [blame] | 2476 | IRB.CreatePointerCast(Descr, IRB.getInt8PtrTy()), |
| 2477 | IRB.CreatePointerCast(&F, MS.IntptrTy)); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | void visitSelectInst(SelectInst& I) { |
| 2482 | IRBuilder<> IRB(&I); |
Evgeniy Stepanov | 566f591 | 2013-09-03 10:04:11 +0000 | [diff] [blame] | 2483 | // a = select b, c, d |
Evgeniy Stepanov | fc742ac | 2014-03-25 13:08:34 +0000 | [diff] [blame] | 2484 | Value *B = I.getCondition(); |
| 2485 | Value *C = I.getTrueValue(); |
| 2486 | Value *D = I.getFalseValue(); |
| 2487 | Value *Sb = getShadow(B); |
| 2488 | Value *Sc = getShadow(C); |
| 2489 | Value *Sd = getShadow(D); |
| 2490 | |
| 2491 | // Result shadow if condition shadow is 0. |
| 2492 | Value *Sa0 = IRB.CreateSelect(B, Sc, Sd); |
| 2493 | Value *Sa1; |
Evgeniy Stepanov | e95d37c | 2013-09-03 13:05:29 +0000 | [diff] [blame] | 2494 | if (I.getType()->isAggregateType()) { |
| 2495 | // To avoid "sign extending" i1 to an arbitrary aggregate type, we just do |
| 2496 | // an extra "select". This results in much more compact IR. |
| 2497 | // Sa = select Sb, poisoned, (select b, Sc, Sd) |
Evgeniy Stepanov | fc742ac | 2014-03-25 13:08:34 +0000 | [diff] [blame] | 2498 | Sa1 = getPoisonedShadow(getShadowTy(I.getType())); |
Evgeniy Stepanov | e95d37c | 2013-09-03 13:05:29 +0000 | [diff] [blame] | 2499 | } else { |
Evgeniy Stepanov | fc742ac | 2014-03-25 13:08:34 +0000 | [diff] [blame] | 2500 | // Sa = select Sb, [ (c^d) | Sc | Sd ], [ b ? Sc : Sd ] |
| 2501 | // If Sb (condition is poisoned), look for bits in c and d that are equal |
| 2502 | // and both unpoisoned. |
| 2503 | // If !Sb (condition is unpoisoned), simply pick one of Sc and Sd. |
| 2504 | |
| 2505 | // Cast arguments to shadow-compatible type. |
| 2506 | C = CreateAppToShadowCast(IRB, C); |
| 2507 | D = CreateAppToShadowCast(IRB, D); |
| 2508 | |
| 2509 | // Result shadow if condition shadow is 1. |
| 2510 | Sa1 = IRB.CreateOr(IRB.CreateXor(C, D), IRB.CreateOr(Sc, Sd)); |
Evgeniy Stepanov | e95d37c | 2013-09-03 13:05:29 +0000 | [diff] [blame] | 2511 | } |
Evgeniy Stepanov | fc742ac | 2014-03-25 13:08:34 +0000 | [diff] [blame] | 2512 | Value *Sa = IRB.CreateSelect(Sb, Sa1, Sa0, "_msprop_select"); |
| 2513 | setShadow(&I, Sa); |
Evgeniy Stepanov | ec83712 | 2012-12-25 14:56:21 +0000 | [diff] [blame] | 2514 | if (MS.TrackOrigins) { |
| 2515 | // Origins are always i32, so any vector conditions must be flattened. |
| 2516 | // FIXME: consider tracking vector origins for app vectors? |
Evgeniy Stepanov | fc742ac | 2014-03-25 13:08:34 +0000 | [diff] [blame] | 2517 | if (B->getType()->isVectorTy()) { |
| 2518 | Type *FlatTy = getShadowTyNoVec(B->getType()); |
| 2519 | B = IRB.CreateICmpNE(IRB.CreateBitCast(B, FlatTy), |
Evgeniy Stepanov | cb5bdff | 2013-11-21 12:00:24 +0000 | [diff] [blame] | 2520 | ConstantInt::getNullValue(FlatTy)); |
Evgeniy Stepanov | fc742ac | 2014-03-25 13:08:34 +0000 | [diff] [blame] | 2521 | Sb = IRB.CreateICmpNE(IRB.CreateBitCast(Sb, FlatTy), |
Evgeniy Stepanov | cb5bdff | 2013-11-21 12:00:24 +0000 | [diff] [blame] | 2522 | ConstantInt::getNullValue(FlatTy)); |
Evgeniy Stepanov | ec83712 | 2012-12-25 14:56:21 +0000 | [diff] [blame] | 2523 | } |
Evgeniy Stepanov | cb5bdff | 2013-11-21 12:00:24 +0000 | [diff] [blame] | 2524 | // a = select b, c, d |
| 2525 | // Oa = Sb ? Ob : (b ? Oc : Od) |
| 2526 | setOrigin(&I, IRB.CreateSelect( |
Evgeniy Stepanov | fc742ac | 2014-03-25 13:08:34 +0000 | [diff] [blame] | 2527 | Sb, getOrigin(I.getCondition()), |
| 2528 | IRB.CreateSelect(B, getOrigin(C), getOrigin(D)))); |
Evgeniy Stepanov | ec83712 | 2012-12-25 14:56:21 +0000 | [diff] [blame] | 2529 | } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | void visitLandingPadInst(LandingPadInst &I) { |
| 2533 | // Do nothing. |
| 2534 | // See http://code.google.com/p/memory-sanitizer/issues/detail?id=1 |
| 2535 | setShadow(&I, getCleanShadow(&I)); |
| 2536 | setOrigin(&I, getCleanOrigin()); |
| 2537 | } |
| 2538 | |
| 2539 | void visitGetElementPtrInst(GetElementPtrInst &I) { |
| 2540 | handleShadowOr(I); |
| 2541 | } |
| 2542 | |
| 2543 | void visitExtractValueInst(ExtractValueInst &I) { |
| 2544 | IRBuilder<> IRB(&I); |
| 2545 | Value *Agg = I.getAggregateOperand(); |
| 2546 | DEBUG(dbgs() << "ExtractValue: " << I << "\n"); |
| 2547 | Value *AggShadow = getShadow(Agg); |
| 2548 | DEBUG(dbgs() << " AggShadow: " << *AggShadow << "\n"); |
| 2549 | Value *ResShadow = IRB.CreateExtractValue(AggShadow, I.getIndices()); |
| 2550 | DEBUG(dbgs() << " ResShadow: " << *ResShadow << "\n"); |
| 2551 | setShadow(&I, ResShadow); |
Evgeniy Stepanov | 560e0893 | 2013-11-11 13:37:10 +0000 | [diff] [blame] | 2552 | setOriginForNaryOp(I); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | void visitInsertValueInst(InsertValueInst &I) { |
| 2556 | IRBuilder<> IRB(&I); |
| 2557 | DEBUG(dbgs() << "InsertValue: " << I << "\n"); |
| 2558 | Value *AggShadow = getShadow(I.getAggregateOperand()); |
| 2559 | Value *InsShadow = getShadow(I.getInsertedValueOperand()); |
| 2560 | DEBUG(dbgs() << " AggShadow: " << *AggShadow << "\n"); |
| 2561 | DEBUG(dbgs() << " InsShadow: " << *InsShadow << "\n"); |
| 2562 | Value *Res = IRB.CreateInsertValue(AggShadow, InsShadow, I.getIndices()); |
| 2563 | DEBUG(dbgs() << " Res: " << *Res << "\n"); |
| 2564 | setShadow(&I, Res); |
Evgeniy Stepanov | 560e0893 | 2013-11-11 13:37:10 +0000 | [diff] [blame] | 2565 | setOriginForNaryOp(I); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | void dumpInst(Instruction &I) { |
| 2569 | if (CallInst *CI = dyn_cast<CallInst>(&I)) { |
| 2570 | errs() << "ZZZ call " << CI->getCalledFunction()->getName() << "\n"; |
| 2571 | } else { |
| 2572 | errs() << "ZZZ " << I.getOpcodeName() << "\n"; |
| 2573 | } |
| 2574 | errs() << "QQQ " << I << "\n"; |
| 2575 | } |
| 2576 | |
| 2577 | void visitResumeInst(ResumeInst &I) { |
| 2578 | DEBUG(dbgs() << "Resume: " << I << "\n"); |
| 2579 | // Nothing to do here. |
| 2580 | } |
| 2581 | |
| 2582 | void visitInstruction(Instruction &I) { |
| 2583 | // Everything else: stop propagating and check for poisoned shadow. |
| 2584 | if (ClDumpStrictInstructions) |
| 2585 | dumpInst(I); |
| 2586 | DEBUG(dbgs() << "DEFAULT: " << I << "\n"); |
| 2587 | for (size_t i = 0, n = I.getNumOperands(); i < n; i++) |
Evgeniy Stepanov | be83d8f | 2013-10-14 15:16:25 +0000 | [diff] [blame] | 2588 | insertShadowCheck(I.getOperand(i), &I); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2589 | setShadow(&I, getCleanShadow(&I)); |
| 2590 | setOrigin(&I, getCleanOrigin()); |
| 2591 | } |
| 2592 | }; |
| 2593 | |
| 2594 | /// \brief AMD64-specific implementation of VarArgHelper. |
| 2595 | struct VarArgAMD64Helper : public VarArgHelper { |
| 2596 | // An unfortunate workaround for asymmetric lowering of va_arg stuff. |
| 2597 | // See a comment in visitCallSite for more details. |
Evgeniy Stepanov | 9b72e99 | 2012-12-14 13:48:31 +0000 | [diff] [blame] | 2598 | static const unsigned AMD64GpEndOffset = 48; // AMD64 ABI Draft 0.99.6 p3.5.7 |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2599 | static const unsigned AMD64FpEndOffset = 176; |
| 2600 | |
| 2601 | Function &F; |
| 2602 | MemorySanitizer &MS; |
| 2603 | MemorySanitizerVisitor &MSV; |
| 2604 | Value *VAArgTLSCopy; |
| 2605 | Value *VAArgOverflowSize; |
| 2606 | |
| 2607 | SmallVector<CallInst*, 16> VAStartInstrumentationList; |
| 2608 | |
| 2609 | VarArgAMD64Helper(Function &F, MemorySanitizer &MS, |
| 2610 | MemorySanitizerVisitor &MSV) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2611 | : F(F), MS(MS), MSV(MSV), VAArgTLSCopy(nullptr), |
| 2612 | VAArgOverflowSize(nullptr) {} |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2613 | |
| 2614 | enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory }; |
| 2615 | |
| 2616 | ArgKind classifyArgument(Value* arg) { |
| 2617 | // A very rough approximation of X86_64 argument classification rules. |
| 2618 | Type *T = arg->getType(); |
| 2619 | if (T->isFPOrFPVectorTy() || T->isX86_MMXTy()) |
| 2620 | return AK_FloatingPoint; |
| 2621 | if (T->isIntegerTy() && T->getPrimitiveSizeInBits() <= 64) |
| 2622 | return AK_GeneralPurpose; |
| 2623 | if (T->isPointerTy()) |
| 2624 | return AK_GeneralPurpose; |
| 2625 | return AK_Memory; |
| 2626 | } |
| 2627 | |
| 2628 | // For VarArg functions, store the argument shadow in an ABI-specific format |
| 2629 | // that corresponds to va_list layout. |
| 2630 | // We do this because Clang lowers va_arg in the frontend, and this pass |
| 2631 | // only sees the low level code that deals with va_list internals. |
| 2632 | // A much easier alternative (provided that Clang emits va_arg instructions) |
| 2633 | // would have been to associate each live instance of va_list with a copy of |
| 2634 | // MSanParamTLS, and extract shadow on va_arg() call in the argument list |
| 2635 | // order. |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 2636 | void visitCallSite(CallSite &CS, IRBuilder<> &IRB) override { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2637 | unsigned GpOffset = 0; |
| 2638 | unsigned FpOffset = AMD64GpEndOffset; |
| 2639 | unsigned OverflowOffset = AMD64FpEndOffset; |
| 2640 | for (CallSite::arg_iterator ArgIt = CS.arg_begin(), End = CS.arg_end(); |
| 2641 | ArgIt != End; ++ArgIt) { |
| 2642 | Value *A = *ArgIt; |
Evgeniy Stepanov | 7ab838e | 2014-03-13 13:17:11 +0000 | [diff] [blame] | 2643 | unsigned ArgNo = CS.getArgumentNo(ArgIt); |
| 2644 | bool IsByVal = CS.paramHasAttr(ArgNo + 1, Attribute::ByVal); |
| 2645 | if (IsByVal) { |
| 2646 | // ByVal arguments always go to the overflow area. |
| 2647 | assert(A->getType()->isPointerTy()); |
| 2648 | Type *RealTy = A->getType()->getPointerElementType(); |
| 2649 | uint64_t ArgSize = MS.DL->getTypeAllocSize(RealTy); |
| 2650 | Value *Base = getShadowPtrForVAArgument(RealTy, IRB, OverflowOffset); |
David Majnemer | f3cadce | 2014-10-20 06:13:33 +0000 | [diff] [blame] | 2651 | OverflowOffset += RoundUpToAlignment(ArgSize, 8); |
Evgeniy Stepanov | 7ab838e | 2014-03-13 13:17:11 +0000 | [diff] [blame] | 2652 | IRB.CreateMemCpy(Base, MSV.getShadowPtr(A, IRB.getInt8Ty(), IRB), |
| 2653 | ArgSize, kShadowTLSAlignment); |
| 2654 | } else { |
| 2655 | ArgKind AK = classifyArgument(A); |
| 2656 | if (AK == AK_GeneralPurpose && GpOffset >= AMD64GpEndOffset) |
| 2657 | AK = AK_Memory; |
| 2658 | if (AK == AK_FloatingPoint && FpOffset >= AMD64FpEndOffset) |
| 2659 | AK = AK_Memory; |
| 2660 | Value *Base; |
| 2661 | switch (AK) { |
| 2662 | case AK_GeneralPurpose: |
| 2663 | Base = getShadowPtrForVAArgument(A->getType(), IRB, GpOffset); |
| 2664 | GpOffset += 8; |
| 2665 | break; |
| 2666 | case AK_FloatingPoint: |
| 2667 | Base = getShadowPtrForVAArgument(A->getType(), IRB, FpOffset); |
| 2668 | FpOffset += 16; |
| 2669 | break; |
| 2670 | case AK_Memory: |
| 2671 | uint64_t ArgSize = MS.DL->getTypeAllocSize(A->getType()); |
| 2672 | Base = getShadowPtrForVAArgument(A->getType(), IRB, OverflowOffset); |
David Majnemer | f3cadce | 2014-10-20 06:13:33 +0000 | [diff] [blame] | 2673 | OverflowOffset += RoundUpToAlignment(ArgSize, 8); |
Evgeniy Stepanov | 7ab838e | 2014-03-13 13:17:11 +0000 | [diff] [blame] | 2674 | } |
| 2675 | IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2676 | } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2677 | } |
| 2678 | Constant *OverflowSize = |
| 2679 | ConstantInt::get(IRB.getInt64Ty(), OverflowOffset - AMD64FpEndOffset); |
| 2680 | IRB.CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS); |
| 2681 | } |
| 2682 | |
| 2683 | /// \brief Compute the shadow address for a given va_arg. |
Evgeniy Stepanov | 7ab838e | 2014-03-13 13:17:11 +0000 | [diff] [blame] | 2684 | Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB, |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2685 | int ArgOffset) { |
| 2686 | Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy); |
| 2687 | Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset)); |
Evgeniy Stepanov | 7ab838e | 2014-03-13 13:17:11 +0000 | [diff] [blame] | 2688 | return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0), |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2689 | "_msarg"); |
| 2690 | } |
| 2691 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 2692 | void visitVAStartInst(VAStartInst &I) override { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2693 | IRBuilder<> IRB(&I); |
| 2694 | VAStartInstrumentationList.push_back(&I); |
| 2695 | Value *VAListTag = I.getArgOperand(0); |
| 2696 | Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB); |
| 2697 | |
| 2698 | // Unpoison the whole __va_list_tag. |
| 2699 | // FIXME: magic ABI constants. |
| 2700 | IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()), |
Peter Collingbourne | f7d65c4 | 2013-01-10 22:36:33 +0000 | [diff] [blame] | 2701 | /* size */24, /* alignment */8, false); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 2704 | void visitVACopyInst(VACopyInst &I) override { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2705 | IRBuilder<> IRB(&I); |
| 2706 | Value *VAListTag = I.getArgOperand(0); |
| 2707 | Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB); |
| 2708 | |
| 2709 | // Unpoison the whole __va_list_tag. |
| 2710 | // FIXME: magic ABI constants. |
| 2711 | IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()), |
Peter Collingbourne | f7d65c4 | 2013-01-10 22:36:33 +0000 | [diff] [blame] | 2712 | /* size */24, /* alignment */8, false); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 2715 | void finalizeInstrumentation() override { |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2716 | assert(!VAArgOverflowSize && !VAArgTLSCopy && |
| 2717 | "finalizeInstrumentation called twice"); |
| 2718 | if (!VAStartInstrumentationList.empty()) { |
| 2719 | // If there is a va_start in this function, make a backup copy of |
| 2720 | // va_arg_tls somewhere in the function entry block. |
| 2721 | IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI()); |
| 2722 | VAArgOverflowSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS); |
| 2723 | Value *CopySize = |
| 2724 | IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, AMD64FpEndOffset), |
| 2725 | VAArgOverflowSize); |
| 2726 | VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize); |
| 2727 | IRB.CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8); |
| 2728 | } |
| 2729 | |
| 2730 | // Instrument va_start. |
| 2731 | // Copy va_list shadow from the backup copy of the TLS contents. |
| 2732 | for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) { |
| 2733 | CallInst *OrigInst = VAStartInstrumentationList[i]; |
| 2734 | IRBuilder<> IRB(OrigInst->getNextNode()); |
| 2735 | Value *VAListTag = OrigInst->getArgOperand(0); |
| 2736 | |
| 2737 | Value *RegSaveAreaPtrPtr = |
| 2738 | IRB.CreateIntToPtr( |
| 2739 | IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy), |
| 2740 | ConstantInt::get(MS.IntptrTy, 16)), |
| 2741 | Type::getInt64PtrTy(*MS.C)); |
| 2742 | Value *RegSaveAreaPtr = IRB.CreateLoad(RegSaveAreaPtrPtr); |
| 2743 | Value *RegSaveAreaShadowPtr = |
| 2744 | MSV.getShadowPtr(RegSaveAreaPtr, IRB.getInt8Ty(), IRB); |
| 2745 | IRB.CreateMemCpy(RegSaveAreaShadowPtr, VAArgTLSCopy, |
| 2746 | AMD64FpEndOffset, 16); |
| 2747 | |
| 2748 | Value *OverflowArgAreaPtrPtr = |
| 2749 | IRB.CreateIntToPtr( |
| 2750 | IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy), |
| 2751 | ConstantInt::get(MS.IntptrTy, 8)), |
| 2752 | Type::getInt64PtrTy(*MS.C)); |
| 2753 | Value *OverflowArgAreaPtr = IRB.CreateLoad(OverflowArgAreaPtrPtr); |
| 2754 | Value *OverflowArgAreaShadowPtr = |
| 2755 | MSV.getShadowPtr(OverflowArgAreaPtr, IRB.getInt8Ty(), IRB); |
Evgeniy Stepanov | d42863c | 2013-08-23 12:11:00 +0000 | [diff] [blame] | 2756 | Value *SrcPtr = IRB.CreateConstGEP1_32(VAArgTLSCopy, AMD64FpEndOffset); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2757 | IRB.CreateMemCpy(OverflowArgAreaShadowPtr, SrcPtr, VAArgOverflowSize, 16); |
| 2758 | } |
| 2759 | } |
| 2760 | }; |
| 2761 | |
Evgeniy Stepanov | ebd7f8e | 2013-05-21 12:27:47 +0000 | [diff] [blame] | 2762 | /// \brief A no-op implementation of VarArgHelper. |
| 2763 | struct VarArgNoOpHelper : public VarArgHelper { |
| 2764 | VarArgNoOpHelper(Function &F, MemorySanitizer &MS, |
| 2765 | MemorySanitizerVisitor &MSV) {} |
| 2766 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 2767 | void visitCallSite(CallSite &CS, IRBuilder<> &IRB) override {} |
Evgeniy Stepanov | ebd7f8e | 2013-05-21 12:27:47 +0000 | [diff] [blame] | 2768 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 2769 | void visitVAStartInst(VAStartInst &I) override {} |
Evgeniy Stepanov | ebd7f8e | 2013-05-21 12:27:47 +0000 | [diff] [blame] | 2770 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 2771 | void visitVACopyInst(VACopyInst &I) override {} |
Evgeniy Stepanov | ebd7f8e | 2013-05-21 12:27:47 +0000 | [diff] [blame] | 2772 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 2773 | void finalizeInstrumentation() override {} |
Evgeniy Stepanov | ebd7f8e | 2013-05-21 12:27:47 +0000 | [diff] [blame] | 2774 | }; |
| 2775 | |
| 2776 | VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan, |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2777 | MemorySanitizerVisitor &Visitor) { |
Evgeniy Stepanov | ebd7f8e | 2013-05-21 12:27:47 +0000 | [diff] [blame] | 2778 | // VarArg handling is only implemented on AMD64. False positives are possible |
| 2779 | // on other platforms. |
| 2780 | llvm::Triple TargetTriple(Func.getParent()->getTargetTriple()); |
| 2781 | if (TargetTriple.getArch() == llvm::Triple::x86_64) |
| 2782 | return new VarArgAMD64Helper(Func, Msan, Visitor); |
| 2783 | else |
| 2784 | return new VarArgNoOpHelper(Func, Msan, Visitor); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | } // namespace |
| 2788 | |
| 2789 | bool MemorySanitizer::runOnFunction(Function &F) { |
| 2790 | MemorySanitizerVisitor Visitor(F, *this); |
| 2791 | |
| 2792 | // Clear out readonly/readnone attributes. |
| 2793 | AttrBuilder B; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 2794 | B.addAttribute(Attribute::ReadOnly) |
| 2795 | .addAttribute(Attribute::ReadNone); |
Bill Wendling | 430fa9b | 2013-01-23 00:45:55 +0000 | [diff] [blame] | 2796 | F.removeAttributes(AttributeSet::FunctionIndex, |
| 2797 | AttributeSet::get(F.getContext(), |
| 2798 | AttributeSet::FunctionIndex, B)); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 2799 | |
| 2800 | return Visitor.runOnFunction(); |
| 2801 | } |