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