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