Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1 | //===-- MemorySanitizer.cpp - detector of uninitialized reads -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// \file |
| 10 | /// This file is a part of MemorySanitizer, a detector of uninitialized |
| 11 | /// reads. |
| 12 | /// |
| 13 | /// Status: early prototype. |
| 14 | /// |
| 15 | /// The algorithm of the tool is similar to Memcheck |
| 16 | /// (http://goo.gl/QKbem). We associate a few shadow bits with every |
| 17 | /// byte of the application memory, poison the shadow of the malloc-ed |
| 18 | /// or alloca-ed memory, load the shadow bits on every memory read, |
| 19 | /// propagate the shadow bits through some of the arithmetic |
| 20 | /// instruction (including MOV), store the shadow bits on every memory |
| 21 | /// write, report a bug on some other instructions (e.g. JMP) if the |
| 22 | /// associated shadow is poisoned. |
| 23 | /// |
| 24 | /// But there are differences too. The first and the major one: |
| 25 | /// compiler instrumentation instead of binary instrumentation. This |
| 26 | /// gives us much better register allocation, possible compiler |
| 27 | /// optimizations and a fast start-up. But this brings the major issue |
| 28 | /// as well: msan needs to see all program events, including system |
| 29 | /// calls and reads/writes in system libraries, so we either need to |
| 30 | /// compile *everything* with msan or use a binary translation |
| 31 | /// component (e.g. DynamoRIO) to instrument pre-built libraries. |
| 32 | /// Another difference from Memcheck is that we use 8 shadow bits per |
| 33 | /// byte of application memory and use a direct shadow mapping. This |
| 34 | /// greatly simplifies the instrumentation code and avoids races on |
| 35 | /// shadow updates (Memcheck is single-threaded so races are not a |
| 36 | /// concern there. Memcheck uses 2 shadow bits per byte with a slow |
| 37 | /// path storage that uses 8 bits per byte). |
| 38 | /// |
| 39 | /// The default value of shadow is 0, which means "clean" (not poisoned). |
| 40 | /// |
| 41 | /// Every module initializer should call __msan_init to ensure that the |
| 42 | /// shadow memory is ready. On error, __msan_warning is called. Since |
| 43 | /// parameters and return values may be passed via registers, we have a |
| 44 | /// specialized thread-local shadow for return values |
| 45 | /// (__msan_retval_tls) and parameters (__msan_param_tls). |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
| 48 | #define DEBUG_TYPE "msan" |
| 49 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 50 | #include "llvm/Transforms/Instrumentation.h" |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 51 | #include "BlackList.h" |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/DepthFirstIterator.h" |
| 53 | #include "llvm/ADT/SmallString.h" |
| 54 | #include "llvm/ADT/SmallVector.h" |
| 55 | #include "llvm/ADT/ValueMap.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 56 | #include "llvm/DataLayout.h" |
| 57 | #include "llvm/Function.h" |
| 58 | #include "llvm/IRBuilder.h" |
| 59 | #include "llvm/InlineAsm.h" |
| 60 | #include "llvm/InstVisitor.h" |
| 61 | #include "llvm/IntrinsicInst.h" |
| 62 | #include "llvm/LLVMContext.h" |
| 63 | #include "llvm/MDBuilder.h" |
| 64 | #include "llvm/Module.h" |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 65 | #include "llvm/Support/CommandLine.h" |
| 66 | #include "llvm/Support/Compiler.h" |
| 67 | #include "llvm/Support/Debug.h" |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 68 | #include "llvm/Support/raw_ostream.h" |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 69 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 70 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 71 | #include "llvm/Type.h" |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 72 | |
| 73 | using namespace llvm; |
| 74 | |
| 75 | static const uint64_t kShadowMask32 = 1ULL << 31; |
| 76 | static const uint64_t kShadowMask64 = 1ULL << 46; |
| 77 | static const uint64_t kOriginOffset32 = 1ULL << 30; |
| 78 | static const uint64_t kOriginOffset64 = 1ULL << 45; |
| 79 | |
| 80 | // This is an important flag that makes the reports much more |
| 81 | // informative at the cost of greater slowdown. Not fully implemented |
| 82 | // yet. |
| 83 | // FIXME: this should be a top-level clang flag, e.g. |
| 84 | // -fmemory-sanitizer-full. |
| 85 | static cl::opt<bool> ClTrackOrigins("msan-track-origins", |
| 86 | cl::desc("Track origins (allocation sites) of poisoned memory"), |
| 87 | cl::Hidden, cl::init(false)); |
| 88 | static cl::opt<bool> ClKeepGoing("msan-keep-going", |
| 89 | cl::desc("keep going after reporting a UMR"), |
| 90 | cl::Hidden, cl::init(false)); |
| 91 | static cl::opt<bool> ClPoisonStack("msan-poison-stack", |
| 92 | cl::desc("poison uninitialized stack variables"), |
| 93 | cl::Hidden, cl::init(true)); |
| 94 | static cl::opt<bool> ClPoisonStackWithCall("msan-poison-stack-with-call", |
| 95 | cl::desc("poison uninitialized stack variables with a call"), |
| 96 | cl::Hidden, cl::init(false)); |
| 97 | static cl::opt<int> ClPoisonStackPattern("msan-poison-stack-pattern", |
| 98 | cl::desc("poison uninitialized stack variables with the given patter"), |
| 99 | cl::Hidden, cl::init(0xff)); |
| 100 | |
| 101 | static cl::opt<bool> ClHandleICmp("msan-handle-icmp", |
| 102 | cl::desc("propagate shadow through ICmpEQ and ICmpNE"), |
| 103 | cl::Hidden, cl::init(true)); |
| 104 | |
| 105 | // This flag controls whether we check the shadow of the address |
| 106 | // operand of load or store. Such bugs are very rare, since load from |
| 107 | // a garbage address typically results in SEGV, but still happen |
| 108 | // (e.g. only lower bits of address are garbage, or the access happens |
| 109 | // early at program startup where malloc-ed memory is more likely to |
| 110 | // be zeroed. As of 2012-08-28 this flag adds 20% slowdown. |
| 111 | static cl::opt<bool> ClCheckAccessAddress("msan-check-access-address", |
| 112 | cl::desc("report accesses through a pointer which has poisoned shadow"), |
| 113 | cl::Hidden, cl::init(true)); |
| 114 | |
| 115 | static cl::opt<bool> ClDumpStrictInstructions("msan-dump-strict-instructions", |
| 116 | cl::desc("print out instructions with default strict semantics"), |
| 117 | cl::Hidden, cl::init(false)); |
| 118 | |
| 119 | static cl::opt<std::string> ClBlackListFile("msan-blacklist", |
| 120 | cl::desc("File containing the list of functions where MemorySanitizer " |
| 121 | "should not report bugs"), cl::Hidden); |
| 122 | |
| 123 | namespace { |
| 124 | |
| 125 | /// \brief An instrumentation pass implementing detection of uninitialized |
| 126 | /// reads. |
| 127 | /// |
| 128 | /// MemorySanitizer: instrument the code in module to find |
| 129 | /// uninitialized reads. |
| 130 | class MemorySanitizer : public FunctionPass { |
| 131 | public: |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 132 | MemorySanitizer() : FunctionPass(ID), TD(0), WarningFn(0) { } |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 133 | const char *getPassName() const { return "MemorySanitizer"; } |
| 134 | bool runOnFunction(Function &F); |
| 135 | bool doInitialization(Module &M); |
| 136 | static char ID; // Pass identification, replacement for typeid. |
| 137 | |
| 138 | private: |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 139 | void initializeCallbacks(Module &M); |
| 140 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 141 | DataLayout *TD; |
| 142 | LLVMContext *C; |
| 143 | Type *IntptrTy; |
| 144 | Type *OriginTy; |
| 145 | /// \brief Thread-local shadow storage for function parameters. |
| 146 | GlobalVariable *ParamTLS; |
| 147 | /// \brief Thread-local origin storage for function parameters. |
| 148 | GlobalVariable *ParamOriginTLS; |
| 149 | /// \brief Thread-local shadow storage for function return value. |
| 150 | GlobalVariable *RetvalTLS; |
| 151 | /// \brief Thread-local origin storage for function return value. |
| 152 | GlobalVariable *RetvalOriginTLS; |
| 153 | /// \brief Thread-local shadow storage for in-register va_arg function |
| 154 | /// parameters (x86_64-specific). |
| 155 | GlobalVariable *VAArgTLS; |
| 156 | /// \brief Thread-local shadow storage for va_arg overflow area |
| 157 | /// (x86_64-specific). |
| 158 | GlobalVariable *VAArgOverflowSizeTLS; |
| 159 | /// \brief Thread-local space used to pass origin value to the UMR reporting |
| 160 | /// function. |
| 161 | GlobalVariable *OriginTLS; |
| 162 | |
| 163 | /// \brief The run-time callback to print a warning. |
| 164 | Value *WarningFn; |
| 165 | /// \brief Run-time helper that copies origin info for a memory range. |
| 166 | Value *MsanCopyOriginFn; |
| 167 | /// \brief Run-time helper that generates a new origin value for a stack |
| 168 | /// allocation. |
| 169 | Value *MsanSetAllocaOriginFn; |
| 170 | /// \brief Run-time helper that poisons stack on function entry. |
| 171 | Value *MsanPoisonStackFn; |
Evgeniy Stepanov | 62b5db9 | 2012-11-29 12:49:04 +0000 | [diff] [blame] | 172 | /// \brief MSan runtime replacements for memmove, memcpy and memset. |
| 173 | Value *MemmoveFn, *MemcpyFn, *MemsetFn; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 174 | |
| 175 | /// \brief Address mask used in application-to-shadow address calculation. |
| 176 | /// ShadowAddr is computed as ApplicationAddr & ~ShadowMask. |
| 177 | uint64_t ShadowMask; |
| 178 | /// \brief Offset of the origin shadow from the "normal" shadow. |
| 179 | /// OriginAddr is computed as (ShadowAddr + OriginOffset) & ~3ULL |
| 180 | uint64_t OriginOffset; |
| 181 | /// \brief Branch weights for error reporting. |
| 182 | MDNode *ColdCallWeights; |
| 183 | /// \brief The blacklist. |
| 184 | OwningPtr<BlackList> BL; |
Evgeniy Stepanov | 1d2da65 | 2012-11-29 12:30:18 +0000 | [diff] [blame] | 185 | /// \brief An empty volatile inline asm that prevents callback merge. |
| 186 | InlineAsm *EmptyAsm; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 187 | |
Evgeniy Stepanov | da0072b | 2012-11-29 13:12:03 +0000 | [diff] [blame] | 188 | friend struct MemorySanitizerVisitor; |
| 189 | friend struct VarArgAMD64Helper; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 190 | }; |
| 191 | } // namespace |
| 192 | |
| 193 | char MemorySanitizer::ID = 0; |
| 194 | INITIALIZE_PASS(MemorySanitizer, "msan", |
| 195 | "MemorySanitizer: detects uninitialized reads.", |
| 196 | false, false) |
| 197 | |
| 198 | FunctionPass *llvm::createMemorySanitizerPass() { |
| 199 | return new MemorySanitizer(); |
| 200 | } |
| 201 | |
| 202 | /// \brief Create a non-const global initialized with the given string. |
| 203 | /// |
| 204 | /// Creates a writable global for Str so that we can pass it to the |
| 205 | /// run-time lib. Runtime uses first 4 bytes of the string to store the |
| 206 | /// frame ID, so the string needs to be mutable. |
| 207 | static GlobalVariable *createPrivateNonConstGlobalForString(Module &M, |
| 208 | StringRef Str) { |
| 209 | Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str); |
| 210 | return new GlobalVariable(M, StrConst->getType(), /*isConstant=*/false, |
| 211 | GlobalValue::PrivateLinkage, StrConst, ""); |
| 212 | } |
| 213 | |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 214 | |
| 215 | /// \brief Insert extern declaration of runtime-provided functions and globals. |
| 216 | void MemorySanitizer::initializeCallbacks(Module &M) { |
| 217 | // Only do this once. |
| 218 | if (WarningFn) |
| 219 | return; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 220 | |
| 221 | IRBuilder<> IRB(*C); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 222 | // Create the callback. |
| 223 | // FIXME: this function should have "Cold" calling conv, |
| 224 | // which is not yet implemented. |
| 225 | StringRef WarningFnName = ClKeepGoing ? "__msan_warning" |
| 226 | : "__msan_warning_noreturn"; |
| 227 | WarningFn = M.getOrInsertFunction(WarningFnName, IRB.getVoidTy(), NULL); |
| 228 | |
| 229 | MsanCopyOriginFn = M.getOrInsertFunction( |
| 230 | "__msan_copy_origin", IRB.getVoidTy(), IRB.getInt8PtrTy(), |
| 231 | IRB.getInt8PtrTy(), IntptrTy, NULL); |
| 232 | MsanSetAllocaOriginFn = M.getOrInsertFunction( |
| 233 | "__msan_set_alloca_origin", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy, |
| 234 | IRB.getInt8PtrTy(), NULL); |
| 235 | MsanPoisonStackFn = M.getOrInsertFunction( |
| 236 | "__msan_poison_stack", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy, NULL); |
| 237 | MemmoveFn = M.getOrInsertFunction( |
Evgeniy Stepanov | 62b5db9 | 2012-11-29 12:49:04 +0000 | [diff] [blame] | 238 | "__msan_memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), |
| 239 | IntptrTy, NULL); |
| 240 | MemcpyFn = M.getOrInsertFunction( |
| 241 | "__msan_memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), |
| 242 | IntptrTy, NULL); |
| 243 | MemsetFn = M.getOrInsertFunction( |
| 244 | "__msan_memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(), |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 245 | IntptrTy, NULL); |
| 246 | |
| 247 | // Create globals. |
| 248 | RetvalTLS = new GlobalVariable( |
| 249 | M, ArrayType::get(IRB.getInt64Ty(), 8), false, |
| 250 | GlobalVariable::ExternalLinkage, 0, "__msan_retval_tls", 0, |
| 251 | GlobalVariable::GeneralDynamicTLSModel); |
| 252 | RetvalOriginTLS = new GlobalVariable( |
| 253 | M, OriginTy, false, GlobalVariable::ExternalLinkage, 0, |
| 254 | "__msan_retval_origin_tls", 0, GlobalVariable::GeneralDynamicTLSModel); |
| 255 | |
| 256 | ParamTLS = new GlobalVariable( |
| 257 | M, ArrayType::get(IRB.getInt64Ty(), 1000), false, |
| 258 | GlobalVariable::ExternalLinkage, 0, "__msan_param_tls", 0, |
| 259 | GlobalVariable::GeneralDynamicTLSModel); |
| 260 | ParamOriginTLS = new GlobalVariable( |
| 261 | M, ArrayType::get(OriginTy, 1000), false, GlobalVariable::ExternalLinkage, |
| 262 | 0, "__msan_param_origin_tls", 0, GlobalVariable::GeneralDynamicTLSModel); |
| 263 | |
| 264 | VAArgTLS = new GlobalVariable( |
| 265 | M, ArrayType::get(IRB.getInt64Ty(), 1000), false, |
| 266 | GlobalVariable::ExternalLinkage, 0, "__msan_va_arg_tls", 0, |
| 267 | GlobalVariable::GeneralDynamicTLSModel); |
| 268 | VAArgOverflowSizeTLS = new GlobalVariable( |
| 269 | M, IRB.getInt64Ty(), false, GlobalVariable::ExternalLinkage, 0, |
| 270 | "__msan_va_arg_overflow_size_tls", 0, |
| 271 | GlobalVariable::GeneralDynamicTLSModel); |
| 272 | OriginTLS = new GlobalVariable( |
| 273 | M, IRB.getInt32Ty(), false, GlobalVariable::ExternalLinkage, 0, |
| 274 | "__msan_origin_tls", 0, GlobalVariable::GeneralDynamicTLSModel); |
Evgeniy Stepanov | 1d2da65 | 2012-11-29 12:30:18 +0000 | [diff] [blame] | 275 | |
| 276 | // We insert an empty inline asm after __msan_report* to avoid callback merge. |
| 277 | EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), |
| 278 | StringRef(""), StringRef(""), |
| 279 | /*hasSideEffects=*/true); |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /// \brief Module-level initialization. |
| 283 | /// |
| 284 | /// inserts a call to __msan_init to the module's constructor list. |
| 285 | bool MemorySanitizer::doInitialization(Module &M) { |
| 286 | TD = getAnalysisIfAvailable<DataLayout>(); |
| 287 | if (!TD) |
| 288 | return false; |
| 289 | BL.reset(new BlackList(ClBlackListFile)); |
| 290 | C = &(M.getContext()); |
| 291 | unsigned PtrSize = TD->getPointerSizeInBits(/* AddressSpace */0); |
| 292 | switch (PtrSize) { |
| 293 | case 64: |
| 294 | ShadowMask = kShadowMask64; |
| 295 | OriginOffset = kOriginOffset64; |
| 296 | break; |
| 297 | case 32: |
| 298 | ShadowMask = kShadowMask32; |
| 299 | OriginOffset = kOriginOffset32; |
| 300 | break; |
| 301 | default: |
| 302 | report_fatal_error("unsupported pointer size"); |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | IRBuilder<> IRB(*C); |
| 307 | IntptrTy = IRB.getIntPtrTy(TD); |
| 308 | OriginTy = IRB.getInt32Ty(); |
| 309 | |
| 310 | ColdCallWeights = MDBuilder(*C).createBranchWeights(1, 1000); |
| 311 | |
| 312 | // Insert a call to __msan_init/__msan_track_origins into the module's CTORs. |
| 313 | appendToGlobalCtors(M, cast<Function>(M.getOrInsertFunction( |
| 314 | "__msan_init", IRB.getVoidTy(), NULL)), 0); |
| 315 | |
| 316 | new GlobalVariable(M, IRB.getInt32Ty(), true, GlobalValue::WeakODRLinkage, |
| 317 | IRB.getInt32(ClTrackOrigins), "__msan_track_origins"); |
| 318 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 319 | return true; |
| 320 | } |
| 321 | |
| 322 | namespace { |
| 323 | |
| 324 | /// \brief A helper class that handles instrumentation of VarArg |
| 325 | /// functions on a particular platform. |
| 326 | /// |
| 327 | /// Implementations are expected to insert the instrumentation |
| 328 | /// necessary to propagate argument shadow through VarArg function |
| 329 | /// calls. Visit* methods are called during an InstVisitor pass over |
| 330 | /// the function, and should avoid creating new basic blocks. A new |
| 331 | /// instance of this class is created for each instrumented function. |
| 332 | struct VarArgHelper { |
| 333 | /// \brief Visit a CallSite. |
| 334 | virtual void visitCallSite(CallSite &CS, IRBuilder<> &IRB) = 0; |
| 335 | |
| 336 | /// \brief Visit a va_start call. |
| 337 | virtual void visitVAStartInst(VAStartInst &I) = 0; |
| 338 | |
| 339 | /// \brief Visit a va_copy call. |
| 340 | virtual void visitVACopyInst(VACopyInst &I) = 0; |
| 341 | |
| 342 | /// \brief Finalize function instrumentation. |
| 343 | /// |
| 344 | /// This method is called after visiting all interesting (see above) |
| 345 | /// instructions in a function. |
| 346 | virtual void finalizeInstrumentation() = 0; |
Evgeniy Stepanov | da0072b | 2012-11-29 13:12:03 +0000 | [diff] [blame] | 347 | |
| 348 | virtual ~VarArgHelper() {} |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | struct MemorySanitizerVisitor; |
| 352 | |
| 353 | VarArgHelper* |
| 354 | CreateVarArgHelper(Function &Func, MemorySanitizer &Msan, |
| 355 | MemorySanitizerVisitor &Visitor); |
| 356 | |
| 357 | /// This class does all the work for a given function. Store and Load |
| 358 | /// instructions store and load corresponding shadow and origin |
| 359 | /// values. Most instructions propagate shadow from arguments to their |
| 360 | /// return values. Certain instructions (most importantly, BranchInst) |
| 361 | /// test their argument shadow and print reports (with a runtime call) if it's |
| 362 | /// non-zero. |
| 363 | struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { |
| 364 | Function &F; |
| 365 | MemorySanitizer &MS; |
| 366 | SmallVector<PHINode *, 16> ShadowPHINodes, OriginPHINodes; |
| 367 | ValueMap<Value*, Value*> ShadowMap, OriginMap; |
| 368 | bool InsertChecks; |
| 369 | OwningPtr<VarArgHelper> VAHelper; |
| 370 | |
| 371 | // An unfortunate workaround for asymmetric lowering of va_arg stuff. |
| 372 | // See a comment in visitCallSite for more details. |
| 373 | static const unsigned AMD64GpEndOffset = 48; // AMD64 ABI Draft 0.99.6 p3.5.7 |
| 374 | static const unsigned AMD64FpEndOffset = 176; |
| 375 | |
| 376 | struct ShadowOriginAndInsertPoint { |
| 377 | Instruction *Shadow; |
| 378 | Instruction *Origin; |
| 379 | Instruction *OrigIns; |
| 380 | ShadowOriginAndInsertPoint(Instruction *S, Instruction *O, Instruction *I) |
| 381 | : Shadow(S), Origin(O), OrigIns(I) { } |
| 382 | ShadowOriginAndInsertPoint() : Shadow(0), Origin(0), OrigIns(0) { } |
| 383 | }; |
| 384 | SmallVector<ShadowOriginAndInsertPoint, 16> InstrumentationList; |
| 385 | |
| 386 | MemorySanitizerVisitor(Function &F, MemorySanitizer &MS) |
| 387 | : F(F), MS(MS), VAHelper(CreateVarArgHelper(F, MS, *this)) { |
| 388 | InsertChecks = !MS.BL->isIn(F); |
| 389 | DEBUG(if (!InsertChecks) |
| 390 | dbgs() << "MemorySanitizer is not inserting checks into '" |
| 391 | << F.getName() << "'\n"); |
| 392 | } |
| 393 | |
| 394 | void materializeChecks() { |
| 395 | for (size_t i = 0, n = InstrumentationList.size(); i < n; i++) { |
| 396 | Instruction *Shadow = InstrumentationList[i].Shadow; |
| 397 | Instruction *OrigIns = InstrumentationList[i].OrigIns; |
| 398 | IRBuilder<> IRB(OrigIns); |
| 399 | DEBUG(dbgs() << " SHAD0 : " << *Shadow << "\n"); |
| 400 | Value *ConvertedShadow = convertToShadowTyNoVec(Shadow, IRB); |
| 401 | DEBUG(dbgs() << " SHAD1 : " << *ConvertedShadow << "\n"); |
| 402 | Value *Cmp = IRB.CreateICmpNE(ConvertedShadow, |
| 403 | getCleanShadow(ConvertedShadow), "_mscmp"); |
| 404 | Instruction *CheckTerm = |
| 405 | SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), |
| 406 | /* Unreachable */ !ClKeepGoing, |
| 407 | MS.ColdCallWeights); |
| 408 | |
| 409 | IRB.SetInsertPoint(CheckTerm); |
| 410 | if (ClTrackOrigins) { |
| 411 | Instruction *Origin = InstrumentationList[i].Origin; |
| 412 | IRB.CreateStore(Origin ? (Value*)Origin : (Value*)IRB.getInt32(0), |
| 413 | MS.OriginTLS); |
| 414 | } |
| 415 | CallInst *Call = IRB.CreateCall(MS.WarningFn); |
| 416 | Call->setDebugLoc(OrigIns->getDebugLoc()); |
Evgeniy Stepanov | 1d2da65 | 2012-11-29 12:30:18 +0000 | [diff] [blame] | 417 | IRB.CreateCall(MS.EmptyAsm); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 418 | DEBUG(dbgs() << " CHECK: " << *Cmp << "\n"); |
| 419 | } |
| 420 | DEBUG(dbgs() << "DONE:\n" << F); |
| 421 | } |
| 422 | |
| 423 | /// \brief Add MemorySanitizer instrumentation to a function. |
| 424 | bool runOnFunction() { |
Evgeniy Stepanov | 94b257d | 2012-12-05 13:14:33 +0000 | [diff] [blame] | 425 | MS.initializeCallbacks(*F.getParent()); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 426 | if (!MS.TD) return false; |
| 427 | // Iterate all BBs in depth-first order and create shadow instructions |
| 428 | // for all instructions (where applicable). |
| 429 | // For PHI nodes we create dummy shadow PHIs which will be finalized later. |
| 430 | for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()), |
| 431 | DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) { |
| 432 | BasicBlock *BB = *DI; |
| 433 | visit(*BB); |
| 434 | } |
| 435 | |
| 436 | // Finalize PHI nodes. |
| 437 | for (size_t i = 0, n = ShadowPHINodes.size(); i < n; i++) { |
| 438 | PHINode *PN = ShadowPHINodes[i]; |
| 439 | PHINode *PNS = cast<PHINode>(getShadow(PN)); |
| 440 | PHINode *PNO = ClTrackOrigins ? cast<PHINode>(getOrigin(PN)) : 0; |
| 441 | size_t NumValues = PN->getNumIncomingValues(); |
| 442 | for (size_t v = 0; v < NumValues; v++) { |
| 443 | PNS->addIncoming(getShadow(PN, v), PN->getIncomingBlock(v)); |
| 444 | if (PNO) |
| 445 | PNO->addIncoming(getOrigin(PN, v), PN->getIncomingBlock(v)); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | VAHelper->finalizeInstrumentation(); |
| 450 | |
| 451 | materializeChecks(); |
| 452 | |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | /// \brief Compute the shadow type that corresponds to a given Value. |
| 457 | Type *getShadowTy(Value *V) { |
| 458 | return getShadowTy(V->getType()); |
| 459 | } |
| 460 | |
| 461 | /// \brief Compute the shadow type that corresponds to a given Type. |
| 462 | Type *getShadowTy(Type *OrigTy) { |
| 463 | if (!OrigTy->isSized()) { |
| 464 | return 0; |
| 465 | } |
| 466 | // For integer type, shadow is the same as the original type. |
| 467 | // This may return weird-sized types like i1. |
| 468 | if (IntegerType *IT = dyn_cast<IntegerType>(OrigTy)) |
| 469 | return IT; |
| 470 | if (VectorType *VT = dyn_cast<VectorType>(OrigTy)) |
| 471 | return VectorType::getInteger(VT); |
| 472 | if (StructType *ST = dyn_cast<StructType>(OrigTy)) { |
| 473 | SmallVector<Type*, 4> Elements; |
| 474 | for (unsigned i = 0, n = ST->getNumElements(); i < n; i++) |
| 475 | Elements.push_back(getShadowTy(ST->getElementType(i))); |
| 476 | StructType *Res = StructType::get(*MS.C, Elements, ST->isPacked()); |
| 477 | DEBUG(dbgs() << "getShadowTy: " << *ST << " ===> " << *Res << "\n"); |
| 478 | return Res; |
| 479 | } |
| 480 | uint32_t TypeSize = MS.TD->getTypeStoreSizeInBits(OrigTy); |
| 481 | return IntegerType::get(*MS.C, TypeSize); |
| 482 | } |
| 483 | |
| 484 | /// \brief Flatten a vector type. |
| 485 | Type *getShadowTyNoVec(Type *ty) { |
| 486 | if (VectorType *vt = dyn_cast<VectorType>(ty)) |
| 487 | return IntegerType::get(*MS.C, vt->getBitWidth()); |
| 488 | return ty; |
| 489 | } |
| 490 | |
| 491 | /// \brief Convert a shadow value to it's flattened variant. |
| 492 | Value *convertToShadowTyNoVec(Value *V, IRBuilder<> &IRB) { |
| 493 | Type *Ty = V->getType(); |
| 494 | Type *NoVecTy = getShadowTyNoVec(Ty); |
| 495 | if (Ty == NoVecTy) return V; |
| 496 | return IRB.CreateBitCast(V, NoVecTy); |
| 497 | } |
| 498 | |
| 499 | /// \brief Compute the shadow address that corresponds to a given application |
| 500 | /// address. |
| 501 | /// |
| 502 | /// Shadow = Addr & ~ShadowMask. |
| 503 | Value *getShadowPtr(Value *Addr, Type *ShadowTy, |
| 504 | IRBuilder<> &IRB) { |
| 505 | Value *ShadowLong = |
| 506 | IRB.CreateAnd(IRB.CreatePointerCast(Addr, MS.IntptrTy), |
| 507 | ConstantInt::get(MS.IntptrTy, ~MS.ShadowMask)); |
| 508 | return IRB.CreateIntToPtr(ShadowLong, PointerType::get(ShadowTy, 0)); |
| 509 | } |
| 510 | |
| 511 | /// \brief Compute the origin address that corresponds to a given application |
| 512 | /// address. |
| 513 | /// |
| 514 | /// OriginAddr = (ShadowAddr + OriginOffset) & ~3ULL |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 515 | Value *getOriginPtr(Value *Addr, IRBuilder<> &IRB) { |
| 516 | Value *ShadowLong = |
| 517 | IRB.CreateAnd(IRB.CreatePointerCast(Addr, MS.IntptrTy), |
Evgeniy Stepanov | 62ba611 | 2012-11-29 13:43:05 +0000 | [diff] [blame] | 518 | ConstantInt::get(MS.IntptrTy, ~MS.ShadowMask)); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 519 | Value *Add = |
| 520 | IRB.CreateAdd(ShadowLong, |
| 521 | ConstantInt::get(MS.IntptrTy, MS.OriginOffset)); |
Evgeniy Stepanov | 62ba611 | 2012-11-29 13:43:05 +0000 | [diff] [blame] | 522 | Value *SecondAnd = |
| 523 | IRB.CreateAnd(Add, ConstantInt::get(MS.IntptrTy, ~3ULL)); |
| 524 | return IRB.CreateIntToPtr(SecondAnd, PointerType::get(IRB.getInt32Ty(), 0)); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /// \brief Compute the shadow address for a given function argument. |
| 528 | /// |
| 529 | /// Shadow = ParamTLS+ArgOffset. |
| 530 | Value *getShadowPtrForArgument(Value *A, IRBuilder<> &IRB, |
| 531 | int ArgOffset) { |
| 532 | Value *Base = IRB.CreatePointerCast(MS.ParamTLS, MS.IntptrTy); |
| 533 | Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset)); |
| 534 | return IRB.CreateIntToPtr(Base, PointerType::get(getShadowTy(A), 0), |
| 535 | "_msarg"); |
| 536 | } |
| 537 | |
| 538 | /// \brief Compute the origin address for a given function argument. |
| 539 | Value *getOriginPtrForArgument(Value *A, IRBuilder<> &IRB, |
| 540 | int ArgOffset) { |
| 541 | if (!ClTrackOrigins) return 0; |
| 542 | Value *Base = IRB.CreatePointerCast(MS.ParamOriginTLS, MS.IntptrTy); |
| 543 | Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset)); |
| 544 | return IRB.CreateIntToPtr(Base, PointerType::get(MS.OriginTy, 0), |
| 545 | "_msarg_o"); |
| 546 | } |
| 547 | |
| 548 | /// \brief Compute the shadow address for a retval. |
| 549 | Value *getShadowPtrForRetval(Value *A, IRBuilder<> &IRB) { |
| 550 | Value *Base = IRB.CreatePointerCast(MS.RetvalTLS, MS.IntptrTy); |
| 551 | return IRB.CreateIntToPtr(Base, PointerType::get(getShadowTy(A), 0), |
| 552 | "_msret"); |
| 553 | } |
| 554 | |
| 555 | /// \brief Compute the origin address for a retval. |
| 556 | Value *getOriginPtrForRetval(IRBuilder<> &IRB) { |
| 557 | // We keep a single origin for the entire retval. Might be too optimistic. |
| 558 | return MS.RetvalOriginTLS; |
| 559 | } |
| 560 | |
| 561 | /// \brief Set SV to be the shadow value for V. |
| 562 | void setShadow(Value *V, Value *SV) { |
| 563 | assert(!ShadowMap.count(V) && "Values may only have one shadow"); |
| 564 | ShadowMap[V] = SV; |
| 565 | } |
| 566 | |
| 567 | /// \brief Set Origin to be the origin value for V. |
| 568 | void setOrigin(Value *V, Value *Origin) { |
| 569 | if (!ClTrackOrigins) return; |
| 570 | assert(!OriginMap.count(V) && "Values may only have one origin"); |
| 571 | DEBUG(dbgs() << "ORIGIN: " << *V << " ==> " << *Origin << "\n"); |
| 572 | OriginMap[V] = Origin; |
| 573 | } |
| 574 | |
| 575 | /// \brief Create a clean shadow value for a given value. |
| 576 | /// |
| 577 | /// Clean shadow (all zeroes) means all bits of the value are defined |
| 578 | /// (initialized). |
| 579 | Value *getCleanShadow(Value *V) { |
| 580 | Type *ShadowTy = getShadowTy(V); |
| 581 | if (!ShadowTy) |
| 582 | return 0; |
| 583 | return Constant::getNullValue(ShadowTy); |
| 584 | } |
| 585 | |
| 586 | /// \brief Create a dirty shadow of a given shadow type. |
| 587 | Constant *getPoisonedShadow(Type *ShadowTy) { |
| 588 | assert(ShadowTy); |
| 589 | if (isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy)) |
| 590 | return Constant::getAllOnesValue(ShadowTy); |
| 591 | StructType *ST = cast<StructType>(ShadowTy); |
| 592 | SmallVector<Constant *, 4> Vals; |
| 593 | for (unsigned i = 0, n = ST->getNumElements(); i < n; i++) |
| 594 | Vals.push_back(getPoisonedShadow(ST->getElementType(i))); |
| 595 | return ConstantStruct::get(ST, Vals); |
| 596 | } |
| 597 | |
| 598 | /// \brief Create a clean (zero) origin. |
| 599 | Value *getCleanOrigin() { |
| 600 | return Constant::getNullValue(MS.OriginTy); |
| 601 | } |
| 602 | |
| 603 | /// \brief Get the shadow value for a given Value. |
| 604 | /// |
| 605 | /// This function either returns the value set earlier with setShadow, |
| 606 | /// or extracts if from ParamTLS (for function arguments). |
| 607 | Value *getShadow(Value *V) { |
| 608 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 609 | // For instructions the shadow is already stored in the map. |
| 610 | Value *Shadow = ShadowMap[V]; |
| 611 | if (!Shadow) { |
| 612 | DEBUG(dbgs() << "No shadow: " << *V << "\n" << *(I->getParent())); |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 613 | (void)I; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 614 | assert(Shadow && "No shadow for a value"); |
| 615 | } |
| 616 | return Shadow; |
| 617 | } |
| 618 | if (UndefValue *U = dyn_cast<UndefValue>(V)) { |
| 619 | Value *AllOnes = getPoisonedShadow(getShadowTy(V)); |
| 620 | DEBUG(dbgs() << "Undef: " << *U << " ==> " << *AllOnes << "\n"); |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 621 | (void)U; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 622 | return AllOnes; |
| 623 | } |
| 624 | if (Argument *A = dyn_cast<Argument>(V)) { |
| 625 | // For arguments we compute the shadow on demand and store it in the map. |
| 626 | Value **ShadowPtr = &ShadowMap[V]; |
| 627 | if (*ShadowPtr) |
| 628 | return *ShadowPtr; |
| 629 | Function *F = A->getParent(); |
| 630 | IRBuilder<> EntryIRB(F->getEntryBlock().getFirstNonPHI()); |
| 631 | unsigned ArgOffset = 0; |
| 632 | for (Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 633 | AI != AE; ++AI) { |
| 634 | if (!AI->getType()->isSized()) { |
| 635 | DEBUG(dbgs() << "Arg is not sized\n"); |
| 636 | continue; |
| 637 | } |
| 638 | unsigned Size = AI->hasByValAttr() |
| 639 | ? MS.TD->getTypeAllocSize(AI->getType()->getPointerElementType()) |
| 640 | : MS.TD->getTypeAllocSize(AI->getType()); |
| 641 | if (A == AI) { |
| 642 | Value *Base = getShadowPtrForArgument(AI, EntryIRB, ArgOffset); |
| 643 | if (AI->hasByValAttr()) { |
| 644 | // ByVal pointer itself has clean shadow. We copy the actual |
| 645 | // argument shadow to the underlying memory. |
| 646 | Value *Cpy = EntryIRB.CreateMemCpy( |
| 647 | getShadowPtr(V, EntryIRB.getInt8Ty(), EntryIRB), |
| 648 | Base, Size, AI->getParamAlignment()); |
| 649 | DEBUG(dbgs() << " ByValCpy: " << *Cpy << "\n"); |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 650 | (void)Cpy; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 651 | *ShadowPtr = getCleanShadow(V); |
| 652 | } else { |
| 653 | *ShadowPtr = EntryIRB.CreateLoad(Base); |
| 654 | } |
| 655 | DEBUG(dbgs() << " ARG: " << *AI << " ==> " << |
| 656 | **ShadowPtr << "\n"); |
| 657 | if (ClTrackOrigins) { |
| 658 | Value* OriginPtr = getOriginPtrForArgument(AI, EntryIRB, ArgOffset); |
| 659 | setOrigin(A, EntryIRB.CreateLoad(OriginPtr)); |
| 660 | } |
| 661 | } |
| 662 | ArgOffset += DataLayout::RoundUpAlignment(Size, 8); |
| 663 | } |
| 664 | assert(*ShadowPtr && "Could not find shadow for an argument"); |
| 665 | return *ShadowPtr; |
| 666 | } |
| 667 | // For everything else the shadow is zero. |
| 668 | return getCleanShadow(V); |
| 669 | } |
| 670 | |
| 671 | /// \brief Get the shadow for i-th argument of the instruction I. |
| 672 | Value *getShadow(Instruction *I, int i) { |
| 673 | return getShadow(I->getOperand(i)); |
| 674 | } |
| 675 | |
| 676 | /// \brief Get the origin for a value. |
| 677 | Value *getOrigin(Value *V) { |
| 678 | if (!ClTrackOrigins) return 0; |
| 679 | if (isa<Instruction>(V) || isa<Argument>(V)) { |
| 680 | Value *Origin = OriginMap[V]; |
| 681 | if (!Origin) { |
| 682 | DEBUG(dbgs() << "NO ORIGIN: " << *V << "\n"); |
| 683 | Origin = getCleanOrigin(); |
| 684 | } |
| 685 | return Origin; |
| 686 | } |
| 687 | return getCleanOrigin(); |
| 688 | } |
| 689 | |
| 690 | /// \brief Get the origin for i-th argument of the instruction I. |
| 691 | Value *getOrigin(Instruction *I, int i) { |
| 692 | return getOrigin(I->getOperand(i)); |
| 693 | } |
| 694 | |
| 695 | /// \brief Remember the place where a shadow check should be inserted. |
| 696 | /// |
| 697 | /// This location will be later instrumented with a check that will print a |
| 698 | /// UMR warning in runtime if the value is not fully defined. |
| 699 | void insertCheck(Value *Val, Instruction *OrigIns) { |
| 700 | assert(Val); |
| 701 | if (!InsertChecks) return; |
| 702 | Instruction *Shadow = dyn_cast_or_null<Instruction>(getShadow(Val)); |
| 703 | if (!Shadow) return; |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 704 | #ifndef NDEBUG |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 705 | Type *ShadowTy = Shadow->getType(); |
| 706 | assert((isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy)) && |
| 707 | "Can only insert checks for integer and vector shadow types"); |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 708 | #endif |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 709 | Instruction *Origin = dyn_cast_or_null<Instruction>(getOrigin(Val)); |
| 710 | InstrumentationList.push_back( |
| 711 | ShadowOriginAndInsertPoint(Shadow, Origin, OrigIns)); |
| 712 | } |
| 713 | |
| 714 | //------------------- Visitors. |
| 715 | |
| 716 | /// \brief Instrument LoadInst |
| 717 | /// |
| 718 | /// Loads the corresponding shadow and (optionally) origin. |
| 719 | /// Optionally, checks that the load address is fully defined. |
| 720 | void visitLoadInst(LoadInst &I) { |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 721 | assert(I.getType()->isSized() && "Load type must have size"); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 722 | IRBuilder<> IRB(&I); |
| 723 | Type *ShadowTy = getShadowTy(&I); |
| 724 | Value *Addr = I.getPointerOperand(); |
| 725 | Value *ShadowPtr = getShadowPtr(Addr, ShadowTy, IRB); |
Evgeniy Stepanov | eeb8b7c | 2012-11-29 14:05:53 +0000 | [diff] [blame] | 726 | setShadow(&I, IRB.CreateAlignedLoad(ShadowPtr, I.getAlignment(), "_msld")); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 727 | |
| 728 | if (ClCheckAccessAddress) |
| 729 | insertCheck(I.getPointerOperand(), &I); |
| 730 | |
| 731 | if (ClTrackOrigins) |
Evgeniy Stepanov | eeb8b7c | 2012-11-29 14:05:53 +0000 | [diff] [blame] | 732 | setOrigin(&I, IRB.CreateAlignedLoad(getOriginPtr(Addr, IRB), I.getAlignment())); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /// \brief Instrument StoreInst |
| 736 | /// |
| 737 | /// Stores the corresponding shadow and (optionally) origin. |
| 738 | /// Optionally, checks that the store address is fully defined. |
| 739 | /// Volatile stores check that the value being stored is fully defined. |
| 740 | void visitStoreInst(StoreInst &I) { |
| 741 | IRBuilder<> IRB(&I); |
| 742 | Value *Val = I.getValueOperand(); |
| 743 | Value *Addr = I.getPointerOperand(); |
| 744 | Value *Shadow = getShadow(Val); |
| 745 | Value *ShadowPtr = getShadowPtr(Addr, Shadow->getType(), IRB); |
| 746 | |
Evgeniy Stepanov | eeb8b7c | 2012-11-29 14:05:53 +0000 | [diff] [blame] | 747 | StoreInst *NewSI = IRB.CreateAlignedStore(Shadow, ShadowPtr, I.getAlignment()); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 748 | DEBUG(dbgs() << " STORE: " << *NewSI << "\n"); |
Matt Beaumont-Gay | c76536f | 2012-11-29 18:15:49 +0000 | [diff] [blame] | 749 | (void)NewSI; |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 750 | // If the store is volatile, add a check. |
| 751 | if (I.isVolatile()) |
| 752 | insertCheck(Val, &I); |
| 753 | if (ClCheckAccessAddress) |
| 754 | insertCheck(Addr, &I); |
| 755 | |
| 756 | if (ClTrackOrigins) |
Evgeniy Stepanov | eeb8b7c | 2012-11-29 14:05:53 +0000 | [diff] [blame] | 757 | IRB.CreateAlignedStore(getOrigin(Val), getOriginPtr(Addr, IRB), I.getAlignment()); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Evgeniy Stepanov | 30484fc | 2012-11-29 15:22:06 +0000 | [diff] [blame] | 760 | // Vector manipulation. |
| 761 | void visitExtractElementInst(ExtractElementInst &I) { |
| 762 | insertCheck(I.getOperand(1), &I); |
| 763 | IRBuilder<> IRB(&I); |
| 764 | setShadow(&I, IRB.CreateExtractElement(getShadow(&I, 0), I.getOperand(1), |
| 765 | "_msprop")); |
| 766 | setOrigin(&I, getOrigin(&I, 0)); |
| 767 | } |
| 768 | |
| 769 | void visitInsertElementInst(InsertElementInst &I) { |
| 770 | insertCheck(I.getOperand(2), &I); |
| 771 | IRBuilder<> IRB(&I); |
| 772 | setShadow(&I, IRB.CreateInsertElement(getShadow(&I, 0), getShadow(&I, 1), |
| 773 | I.getOperand(2), "_msprop")); |
| 774 | setOriginForNaryOp(I); |
| 775 | } |
| 776 | |
| 777 | void visitShuffleVectorInst(ShuffleVectorInst &I) { |
| 778 | insertCheck(I.getOperand(2), &I); |
| 779 | IRBuilder<> IRB(&I); |
| 780 | setShadow(&I, IRB.CreateShuffleVector(getShadow(&I, 0), getShadow(&I, 1), |
| 781 | I.getOperand(2), "_msprop")); |
| 782 | setOriginForNaryOp(I); |
| 783 | } |
| 784 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 785 | // Casts. |
| 786 | void visitSExtInst(SExtInst &I) { |
| 787 | IRBuilder<> IRB(&I); |
| 788 | setShadow(&I, IRB.CreateSExt(getShadow(&I, 0), I.getType(), "_msprop")); |
| 789 | setOrigin(&I, getOrigin(&I, 0)); |
| 790 | } |
| 791 | |
| 792 | void visitZExtInst(ZExtInst &I) { |
| 793 | IRBuilder<> IRB(&I); |
| 794 | setShadow(&I, IRB.CreateZExt(getShadow(&I, 0), I.getType(), "_msprop")); |
| 795 | setOrigin(&I, getOrigin(&I, 0)); |
| 796 | } |
| 797 | |
| 798 | void visitTruncInst(TruncInst &I) { |
| 799 | IRBuilder<> IRB(&I); |
| 800 | setShadow(&I, IRB.CreateTrunc(getShadow(&I, 0), I.getType(), "_msprop")); |
| 801 | setOrigin(&I, getOrigin(&I, 0)); |
| 802 | } |
| 803 | |
| 804 | void visitBitCastInst(BitCastInst &I) { |
| 805 | IRBuilder<> IRB(&I); |
| 806 | setShadow(&I, IRB.CreateBitCast(getShadow(&I, 0), getShadowTy(&I))); |
| 807 | setOrigin(&I, getOrigin(&I, 0)); |
| 808 | } |
| 809 | |
| 810 | void visitPtrToIntInst(PtrToIntInst &I) { |
| 811 | IRBuilder<> IRB(&I); |
| 812 | setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false, |
| 813 | "_msprop_ptrtoint")); |
| 814 | setOrigin(&I, getOrigin(&I, 0)); |
| 815 | } |
| 816 | |
| 817 | void visitIntToPtrInst(IntToPtrInst &I) { |
| 818 | IRBuilder<> IRB(&I); |
| 819 | setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false, |
| 820 | "_msprop_inttoptr")); |
| 821 | setOrigin(&I, getOrigin(&I, 0)); |
| 822 | } |
| 823 | |
| 824 | void visitFPToSIInst(CastInst& I) { handleShadowOr(I); } |
| 825 | void visitFPToUIInst(CastInst& I) { handleShadowOr(I); } |
| 826 | void visitSIToFPInst(CastInst& I) { handleShadowOr(I); } |
| 827 | void visitUIToFPInst(CastInst& I) { handleShadowOr(I); } |
| 828 | void visitFPExtInst(CastInst& I) { handleShadowOr(I); } |
| 829 | void visitFPTruncInst(CastInst& I) { handleShadowOr(I); } |
| 830 | |
| 831 | /// \brief Propagate shadow for bitwise AND. |
| 832 | /// |
| 833 | /// This code is exact, i.e. if, for example, a bit in the left argument |
| 834 | /// is defined and 0, then neither the value not definedness of the |
| 835 | /// corresponding bit in B don't affect the resulting shadow. |
| 836 | void visitAnd(BinaryOperator &I) { |
| 837 | IRBuilder<> IRB(&I); |
| 838 | // "And" of 0 and a poisoned value results in unpoisoned value. |
| 839 | // 1&1 => 1; 0&1 => 0; p&1 => p; |
| 840 | // 1&0 => 0; 0&0 => 0; p&0 => 0; |
| 841 | // 1&p => p; 0&p => 0; p&p => p; |
| 842 | // S = (S1 & S2) | (V1 & S2) | (S1 & V2) |
| 843 | Value *S1 = getShadow(&I, 0); |
| 844 | Value *S2 = getShadow(&I, 1); |
| 845 | Value *V1 = I.getOperand(0); |
| 846 | Value *V2 = I.getOperand(1); |
| 847 | if (V1->getType() != S1->getType()) { |
| 848 | V1 = IRB.CreateIntCast(V1, S1->getType(), false); |
| 849 | V2 = IRB.CreateIntCast(V2, S2->getType(), false); |
| 850 | } |
| 851 | Value *S1S2 = IRB.CreateAnd(S1, S2); |
| 852 | Value *V1S2 = IRB.CreateAnd(V1, S2); |
| 853 | Value *S1V2 = IRB.CreateAnd(S1, V2); |
| 854 | setShadow(&I, IRB.CreateOr(S1S2, IRB.CreateOr(V1S2, S1V2))); |
| 855 | setOriginForNaryOp(I); |
| 856 | } |
| 857 | |
| 858 | void visitOr(BinaryOperator &I) { |
| 859 | IRBuilder<> IRB(&I); |
| 860 | // "Or" of 1 and a poisoned value results in unpoisoned value. |
| 861 | // 1|1 => 1; 0|1 => 1; p|1 => 1; |
| 862 | // 1|0 => 1; 0|0 => 0; p|0 => p; |
| 863 | // 1|p => 1; 0|p => p; p|p => p; |
| 864 | // S = (S1 & S2) | (~V1 & S2) | (S1 & ~V2) |
| 865 | Value *S1 = getShadow(&I, 0); |
| 866 | Value *S2 = getShadow(&I, 1); |
| 867 | Value *V1 = IRB.CreateNot(I.getOperand(0)); |
| 868 | Value *V2 = IRB.CreateNot(I.getOperand(1)); |
| 869 | if (V1->getType() != S1->getType()) { |
| 870 | V1 = IRB.CreateIntCast(V1, S1->getType(), false); |
| 871 | V2 = IRB.CreateIntCast(V2, S2->getType(), false); |
| 872 | } |
| 873 | Value *S1S2 = IRB.CreateAnd(S1, S2); |
| 874 | Value *V1S2 = IRB.CreateAnd(V1, S2); |
| 875 | Value *S1V2 = IRB.CreateAnd(S1, V2); |
| 876 | setShadow(&I, IRB.CreateOr(S1S2, IRB.CreateOr(V1S2, S1V2))); |
| 877 | setOriginForNaryOp(I); |
| 878 | } |
| 879 | |
| 880 | /// \brief Propagate origin for an instruction. |
| 881 | /// |
| 882 | /// This is a general case of origin propagation. For an Nary operation, |
| 883 | /// is set to the origin of an argument that is not entirely initialized. |
Evgeniy Stepanov | f433cec | 2012-11-29 14:44:00 +0000 | [diff] [blame] | 884 | /// If there is more than one such arguments, the rightmost of them is picked. |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 885 | /// It does not matter which one is picked if all arguments are initialized. |
| 886 | void setOriginForNaryOp(Instruction &I) { |
| 887 | if (!ClTrackOrigins) return; |
| 888 | IRBuilder<> IRB(&I); |
| 889 | Value *Origin = getOrigin(&I, 0); |
| 890 | for (unsigned Op = 1, n = I.getNumOperands(); Op < n; ++Op) { |
Evgeniy Stepanov | f433cec | 2012-11-29 14:44:00 +0000 | [diff] [blame] | 891 | Value *S = convertToShadowTyNoVec(getShadow(&I, Op), IRB); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 892 | Origin = IRB.CreateSelect(IRB.CreateICmpNE(S, getCleanShadow(S)), |
Evgeniy Stepanov | f433cec | 2012-11-29 14:44:00 +0000 | [diff] [blame] | 893 | getOrigin(&I, Op), Origin); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 894 | } |
| 895 | setOrigin(&I, Origin); |
| 896 | } |
| 897 | |
| 898 | /// \brief Propagate shadow for a binary operation. |
| 899 | /// |
| 900 | /// Shadow = Shadow0 | Shadow1, all 3 must have the same type. |
| 901 | /// Bitwise OR is selected as an operation that will never lose even a bit of |
| 902 | /// poison. |
| 903 | void handleShadowOrBinary(Instruction &I) { |
| 904 | IRBuilder<> IRB(&I); |
| 905 | Value *Shadow0 = getShadow(&I, 0); |
| 906 | Value *Shadow1 = getShadow(&I, 1); |
| 907 | setShadow(&I, IRB.CreateOr(Shadow0, Shadow1, "_msprop")); |
| 908 | setOriginForNaryOp(I); |
| 909 | } |
| 910 | |
| 911 | /// \brief Propagate shadow for arbitrary operation. |
| 912 | /// |
| 913 | /// This is a general case of shadow propagation, used in all cases where we |
| 914 | /// don't know and/or care about what the operation actually does. |
| 915 | /// It converts all input shadow values to a common type (extending or |
| 916 | /// truncating as necessary), and bitwise OR's them. |
| 917 | /// |
| 918 | /// This is much cheaper than inserting checks (i.e. requiring inputs to be |
| 919 | /// fully initialized), and less prone to false positives. |
| 920 | // FIXME: is the casting actually correct? |
| 921 | // FIXME: merge this with handleShadowOrBinary. |
| 922 | void handleShadowOr(Instruction &I) { |
| 923 | IRBuilder<> IRB(&I); |
| 924 | Value *Shadow = getShadow(&I, 0); |
| 925 | for (unsigned Op = 1, n = I.getNumOperands(); Op < n; ++Op) |
| 926 | Shadow = IRB.CreateOr( |
| 927 | Shadow, IRB.CreateIntCast(getShadow(&I, Op), Shadow->getType(), false), |
| 928 | "_msprop"); |
| 929 | Shadow = IRB.CreateIntCast(Shadow, getShadowTy(&I), false); |
| 930 | setShadow(&I, Shadow); |
| 931 | setOriginForNaryOp(I); |
| 932 | } |
| 933 | |
| 934 | void visitFAdd(BinaryOperator &I) { handleShadowOrBinary(I); } |
| 935 | void visitFSub(BinaryOperator &I) { handleShadowOrBinary(I); } |
| 936 | void visitFMul(BinaryOperator &I) { handleShadowOrBinary(I); } |
| 937 | void visitAdd(BinaryOperator &I) { handleShadowOrBinary(I); } |
| 938 | void visitSub(BinaryOperator &I) { handleShadowOrBinary(I); } |
| 939 | void visitXor(BinaryOperator &I) { handleShadowOrBinary(I); } |
| 940 | void visitMul(BinaryOperator &I) { handleShadowOrBinary(I); } |
| 941 | |
| 942 | void handleDiv(Instruction &I) { |
| 943 | IRBuilder<> IRB(&I); |
| 944 | // Strict on the second argument. |
| 945 | insertCheck(I.getOperand(1), &I); |
| 946 | setShadow(&I, getShadow(&I, 0)); |
| 947 | setOrigin(&I, getOrigin(&I, 0)); |
| 948 | } |
| 949 | |
| 950 | void visitUDiv(BinaryOperator &I) { handleDiv(I); } |
| 951 | void visitSDiv(BinaryOperator &I) { handleDiv(I); } |
| 952 | void visitFDiv(BinaryOperator &I) { handleDiv(I); } |
| 953 | void visitURem(BinaryOperator &I) { handleDiv(I); } |
| 954 | void visitSRem(BinaryOperator &I) { handleDiv(I); } |
| 955 | void visitFRem(BinaryOperator &I) { handleDiv(I); } |
| 956 | |
| 957 | /// \brief Instrument == and != comparisons. |
| 958 | /// |
| 959 | /// Sometimes the comparison result is known even if some of the bits of the |
| 960 | /// arguments are not. |
| 961 | void handleEqualityComparison(ICmpInst &I) { |
| 962 | IRBuilder<> IRB(&I); |
| 963 | Value *A = I.getOperand(0); |
| 964 | Value *B = I.getOperand(1); |
| 965 | Value *Sa = getShadow(A); |
| 966 | Value *Sb = getShadow(B); |
| 967 | if (A->getType()->isPointerTy()) |
| 968 | A = IRB.CreatePointerCast(A, MS.IntptrTy); |
| 969 | if (B->getType()->isPointerTy()) |
| 970 | B = IRB.CreatePointerCast(B, MS.IntptrTy); |
| 971 | // A == B <==> (C = A^B) == 0 |
| 972 | // A != B <==> (C = A^B) != 0 |
| 973 | // Sc = Sa | Sb |
| 974 | Value *C = IRB.CreateXor(A, B); |
| 975 | Value *Sc = IRB.CreateOr(Sa, Sb); |
| 976 | // Now dealing with i = (C == 0) comparison (or C != 0, does not matter now) |
| 977 | // Result is defined if one of the following is true |
| 978 | // * there is a defined 1 bit in C |
| 979 | // * C is fully defined |
| 980 | // Si = !(C & ~Sc) && Sc |
| 981 | Value *Zero = Constant::getNullValue(Sc->getType()); |
| 982 | Value *MinusOne = Constant::getAllOnesValue(Sc->getType()); |
| 983 | Value *Si = |
| 984 | IRB.CreateAnd(IRB.CreateICmpNE(Sc, Zero), |
| 985 | IRB.CreateICmpEQ( |
| 986 | IRB.CreateAnd(IRB.CreateXor(Sc, MinusOne), C), Zero)); |
| 987 | Si->setName("_msprop_icmp"); |
| 988 | setShadow(&I, Si); |
| 989 | setOriginForNaryOp(I); |
| 990 | } |
| 991 | |
Evgeniy Stepanov | 857d9d2 | 2012-11-29 14:25:47 +0000 | [diff] [blame] | 992 | /// \brief Instrument signed relational comparisons. |
| 993 | /// |
| 994 | /// Handle (x<0) and (x>=0) comparisons (essentially, sign bit tests) by |
| 995 | /// propagating the highest bit of the shadow. Everything else is delegated |
| 996 | /// to handleShadowOr(). |
| 997 | void handleSignedRelationalComparison(ICmpInst &I) { |
| 998 | Constant *constOp0 = dyn_cast<Constant>(I.getOperand(0)); |
| 999 | Constant *constOp1 = dyn_cast<Constant>(I.getOperand(1)); |
| 1000 | Value* op = NULL; |
| 1001 | CmpInst::Predicate pre = I.getPredicate(); |
| 1002 | if (constOp0 && constOp0->isNullValue() && |
| 1003 | (pre == CmpInst::ICMP_SGT || pre == CmpInst::ICMP_SLE)) { |
| 1004 | op = I.getOperand(1); |
| 1005 | } else if (constOp1 && constOp1->isNullValue() && |
| 1006 | (pre == CmpInst::ICMP_SLT || pre == CmpInst::ICMP_SGE)) { |
| 1007 | op = I.getOperand(0); |
| 1008 | } |
| 1009 | if (op) { |
| 1010 | IRBuilder<> IRB(&I); |
| 1011 | Value* Shadow = |
| 1012 | IRB.CreateICmpSLT(getShadow(op), getCleanShadow(op), "_msprop_icmpslt"); |
| 1013 | setShadow(&I, Shadow); |
| 1014 | setOrigin(&I, getOrigin(op)); |
| 1015 | } else { |
| 1016 | handleShadowOr(I); |
| 1017 | } |
| 1018 | } |
| 1019 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1020 | void visitICmpInst(ICmpInst &I) { |
| 1021 | if (ClHandleICmp && I.isEquality()) |
| 1022 | handleEqualityComparison(I); |
Evgeniy Stepanov | 857d9d2 | 2012-11-29 14:25:47 +0000 | [diff] [blame] | 1023 | else if (ClHandleICmp && I.isSigned() && I.isRelational()) |
| 1024 | handleSignedRelationalComparison(I); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1025 | else |
| 1026 | handleShadowOr(I); |
| 1027 | } |
| 1028 | |
| 1029 | void visitFCmpInst(FCmpInst &I) { |
| 1030 | handleShadowOr(I); |
| 1031 | } |
| 1032 | |
| 1033 | void handleShift(BinaryOperator &I) { |
| 1034 | IRBuilder<> IRB(&I); |
| 1035 | // If any of the S2 bits are poisoned, the whole thing is poisoned. |
| 1036 | // Otherwise perform the same shift on S1. |
| 1037 | Value *S1 = getShadow(&I, 0); |
| 1038 | Value *S2 = getShadow(&I, 1); |
| 1039 | Value *S2Conv = IRB.CreateSExt(IRB.CreateICmpNE(S2, getCleanShadow(S2)), |
| 1040 | S2->getType()); |
| 1041 | Value *V2 = I.getOperand(1); |
| 1042 | Value *Shift = IRB.CreateBinOp(I.getOpcode(), S1, V2); |
| 1043 | setShadow(&I, IRB.CreateOr(Shift, S2Conv)); |
| 1044 | setOriginForNaryOp(I); |
| 1045 | } |
| 1046 | |
| 1047 | void visitShl(BinaryOperator &I) { handleShift(I); } |
| 1048 | void visitAShr(BinaryOperator &I) { handleShift(I); } |
| 1049 | void visitLShr(BinaryOperator &I) { handleShift(I); } |
| 1050 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1051 | /// \brief Instrument llvm.memmove |
| 1052 | /// |
| 1053 | /// At this point we don't know if llvm.memmove will be inlined or not. |
| 1054 | /// If we don't instrument it and it gets inlined, |
| 1055 | /// our interceptor will not kick in and we will lose the memmove. |
| 1056 | /// If we instrument the call here, but it does not get inlined, |
| 1057 | /// we will memove the shadow twice: which is bad in case |
| 1058 | /// of overlapping regions. So, we simply lower the intrinsic to a call. |
| 1059 | /// |
Evgeniy Stepanov | 62b5db9 | 2012-11-29 12:49:04 +0000 | [diff] [blame] | 1060 | /// Similar situation exists for memcpy and memset. |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1061 | void visitMemMoveInst(MemMoveInst &I) { |
| 1062 | IRBuilder<> IRB(&I); |
| 1063 | IRB.CreateCall3( |
| 1064 | MS.MemmoveFn, |
| 1065 | IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()), |
| 1066 | IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()), |
| 1067 | IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)); |
| 1068 | I.eraseFromParent(); |
| 1069 | } |
| 1070 | |
Evgeniy Stepanov | 62b5db9 | 2012-11-29 12:49:04 +0000 | [diff] [blame] | 1071 | // Similar to memmove: avoid copying shadow twice. |
| 1072 | // This is somewhat unfortunate as it may slowdown small constant memcpys. |
| 1073 | // FIXME: consider doing manual inline for small constant sizes and proper |
| 1074 | // alignment. |
| 1075 | void visitMemCpyInst(MemCpyInst &I) { |
| 1076 | IRBuilder<> IRB(&I); |
| 1077 | IRB.CreateCall3( |
| 1078 | MS.MemcpyFn, |
| 1079 | IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()), |
| 1080 | IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()), |
| 1081 | IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)); |
| 1082 | I.eraseFromParent(); |
| 1083 | } |
| 1084 | |
| 1085 | // Same as memcpy. |
| 1086 | void visitMemSetInst(MemSetInst &I) { |
| 1087 | IRBuilder<> IRB(&I); |
| 1088 | IRB.CreateCall3( |
| 1089 | MS.MemsetFn, |
| 1090 | IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()), |
| 1091 | IRB.CreateIntCast(I.getArgOperand(1), IRB.getInt32Ty(), false), |
| 1092 | IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)); |
| 1093 | I.eraseFromParent(); |
| 1094 | } |
| 1095 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1096 | void visitVAStartInst(VAStartInst &I) { |
| 1097 | VAHelper->visitVAStartInst(I); |
| 1098 | } |
| 1099 | |
| 1100 | void visitVACopyInst(VACopyInst &I) { |
| 1101 | VAHelper->visitVACopyInst(I); |
| 1102 | } |
| 1103 | |
Evgeniy Stepanov | 8b51bab | 2012-12-05 14:39:55 +0000 | [diff] [blame^] | 1104 | void handleBswap(IntrinsicInst &I) { |
| 1105 | IRBuilder<> IRB(&I); |
| 1106 | Value *Op = I.getArgOperand(0); |
| 1107 | Type *OpType = Op->getType(); |
| 1108 | Function *BswapFunc = Intrinsic::getDeclaration( |
| 1109 | F.getParent(), Intrinsic::bswap, ArrayRef<Type*>(&OpType, 1)); |
| 1110 | setShadow(&I, IRB.CreateCall(BswapFunc, getShadow(Op))); |
| 1111 | setOrigin(&I, getOrigin(Op)); |
| 1112 | } |
| 1113 | |
| 1114 | void visitIntrinsicInst(IntrinsicInst &I) { |
| 1115 | switch (I.getIntrinsicID()) { |
| 1116 | case llvm::Intrinsic::bswap: |
| 1117 | handleBswap(I); break; |
| 1118 | default: |
| 1119 | visitInstruction(I); break; |
| 1120 | } |
| 1121 | } |
| 1122 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1123 | void visitCallSite(CallSite CS) { |
| 1124 | Instruction &I = *CS.getInstruction(); |
| 1125 | assert((CS.isCall() || CS.isInvoke()) && "Unknown type of CallSite"); |
| 1126 | if (CS.isCall()) { |
Evgeniy Stepanov | 7ad7e83 | 2012-11-29 14:32:03 +0000 | [diff] [blame] | 1127 | CallInst *Call = cast<CallInst>(&I); |
| 1128 | |
| 1129 | // For inline asm, do the usual thing: check argument shadow and mark all |
| 1130 | // outputs as clean. Note that any side effects of the inline asm that are |
| 1131 | // not immediately visible in its constraints are not handled. |
| 1132 | if (Call->isInlineAsm()) { |
| 1133 | visitInstruction(I); |
| 1134 | return; |
| 1135 | } |
| 1136 | |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1137 | // Allow only tail calls with the same types, otherwise |
| 1138 | // we may have a false positive: shadow for a non-void RetVal |
| 1139 | // will get propagated to a void RetVal. |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1140 | if (Call->isTailCall() && Call->getType() != Call->getParent()->getType()) |
| 1141 | Call->setTailCall(false); |
Evgeniy Stepanov | 8b51bab | 2012-12-05 14:39:55 +0000 | [diff] [blame^] | 1142 | |
| 1143 | assert(!isa<IntrinsicInst>(&I) && "intrinsics are handled elsewhere"); |
Evgeniy Stepanov | d4bd7b7 | 2012-11-29 09:57:20 +0000 | [diff] [blame] | 1144 | } |
| 1145 | IRBuilder<> IRB(&I); |
| 1146 | unsigned ArgOffset = 0; |
| 1147 | DEBUG(dbgs() << " CallSite: " << I << "\n"); |
| 1148 | for (CallSite::arg_iterator ArgIt = CS.arg_begin(), End = CS.arg_end(); |
| 1149 | ArgIt != End; ++ArgIt) { |
| 1150 | Value *A = *ArgIt; |
| 1151 | unsigned i = ArgIt - CS.arg_begin(); |
| 1152 | if (!A->getType()->isSized()) { |
| 1153 | DEBUG(dbgs() << "Arg " << i << " is not sized: " << I << "\n"); |
| 1154 | continue; |
| 1155 | } |
| 1156 | unsigned Size = 0; |
| 1157 | Value *Store = 0; |
| 1158 | // Compute the Shadow for arg even if it is ByVal, because |
| 1159 | // in that case getShadow() will copy the actual arg shadow to |
| 1160 | // __msan_param_tls. |
| 1161 | Value *ArgShadow = getShadow(A); |
| 1162 | Value *ArgShadowBase = getShadowPtrForArgument(A, IRB, ArgOffset); |
| 1163 | DEBUG(dbgs() << " Arg#" << i << ": " << *A << |
| 1164 | " Shadow: " << *ArgShadow << "\n"); |
| 1165 | if (CS.paramHasAttr(i + 1, Attributes::ByVal)) { |
| 1166 | assert(A->getType()->isPointerTy() && |
| 1167 | "ByVal argument is not a pointer!"); |
| 1168 | Size = MS.TD->getTypeAllocSize(A->getType()->getPointerElementType()); |
| 1169 | unsigned Alignment = CS.getParamAlignment(i + 1); |
| 1170 | Store = IRB.CreateMemCpy(ArgShadowBase, |
| 1171 | getShadowPtr(A, Type::getInt8Ty(*MS.C), IRB), |
| 1172 | Size, Alignment); |
| 1173 | } else { |
| 1174 | Size = MS.TD->getTypeAllocSize(A->getType()); |
| 1175 | Store = IRB.CreateStore(ArgShadow, ArgShadowBase); |
| 1176 | } |
| 1177 | if (ClTrackOrigins) |
| 1178 | IRB.CreateStore(getOrigin(A), |
| 1179 | getOriginPtrForArgument(A, IRB, ArgOffset)); |
| 1180 | assert(Size != 0 && Store != 0); |
| 1181 | DEBUG(dbgs() << " Param:" << *Store << "\n"); |
| 1182 | ArgOffset += DataLayout::RoundUpAlignment(Size, 8); |
| 1183 | } |
| 1184 | DEBUG(dbgs() << " done with call args\n"); |
| 1185 | |
| 1186 | FunctionType *FT = |
| 1187 | cast<FunctionType>(CS.getCalledValue()->getType()-> getContainedType(0)); |
| 1188 | if (FT->isVarArg()) { |
| 1189 | VAHelper->visitCallSite(CS, IRB); |
| 1190 | } |
| 1191 | |
| 1192 | // Now, get the shadow for the RetVal. |
| 1193 | if (!I.getType()->isSized()) return; |
| 1194 | IRBuilder<> IRBBefore(&I); |
| 1195 | // Untill we have full dynamic coverage, make sure the retval shadow is 0. |
| 1196 | Value *Base = getShadowPtrForRetval(&I, IRBBefore); |
| 1197 | IRBBefore.CreateStore(getCleanShadow(&I), Base); |
| 1198 | Instruction *NextInsn = 0; |
| 1199 | if (CS.isCall()) { |
| 1200 | NextInsn = I.getNextNode(); |
| 1201 | } else { |
| 1202 | BasicBlock *NormalDest = cast<InvokeInst>(&I)->getNormalDest(); |
| 1203 | if (!NormalDest->getSinglePredecessor()) { |
| 1204 | // FIXME: this case is tricky, so we are just conservative here. |
| 1205 | // Perhaps we need to split the edge between this BB and NormalDest, |
| 1206 | // but a naive attempt to use SplitEdge leads to a crash. |
| 1207 | setShadow(&I, getCleanShadow(&I)); |
| 1208 | setOrigin(&I, getCleanOrigin()); |
| 1209 | return; |
| 1210 | } |
| 1211 | NextInsn = NormalDest->getFirstInsertionPt(); |
| 1212 | assert(NextInsn && |
| 1213 | "Could not find insertion point for retval shadow load"); |
| 1214 | } |
| 1215 | IRBuilder<> IRBAfter(NextInsn); |
| 1216 | setShadow(&I, IRBAfter.CreateLoad(getShadowPtrForRetval(&I, IRBAfter), |
| 1217 | "_msret")); |
| 1218 | if (ClTrackOrigins) |
| 1219 | setOrigin(&I, IRBAfter.CreateLoad(getOriginPtrForRetval(IRBAfter))); |
| 1220 | } |
| 1221 | |
| 1222 | void visitReturnInst(ReturnInst &I) { |
| 1223 | IRBuilder<> IRB(&I); |
| 1224 | if (Value *RetVal = I.getReturnValue()) { |
| 1225 | // Set the shadow for the RetVal. |
| 1226 | Value *Shadow = getShadow(RetVal); |
| 1227 | Value *ShadowPtr = getShadowPtrForRetval(RetVal, IRB); |
| 1228 | DEBUG(dbgs() << "Return: " << *Shadow << "\n" << *ShadowPtr << "\n"); |
| 1229 | IRB.CreateStore(Shadow, ShadowPtr); |
| 1230 | if (ClTrackOrigins) |
| 1231 | IRB.CreateStore(getOrigin(RetVal), getOriginPtrForRetval(IRB)); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | void visitPHINode(PHINode &I) { |
| 1236 | IRBuilder<> IRB(&I); |
| 1237 | ShadowPHINodes.push_back(&I); |
| 1238 | setShadow(&I, IRB.CreatePHI(getShadowTy(&I), I.getNumIncomingValues(), |
| 1239 | "_msphi_s")); |
| 1240 | if (ClTrackOrigins) |
| 1241 | setOrigin(&I, IRB.CreatePHI(MS.OriginTy, I.getNumIncomingValues(), |
| 1242 | "_msphi_o")); |
| 1243 | } |
| 1244 | |
| 1245 | void visitAllocaInst(AllocaInst &I) { |
| 1246 | setShadow(&I, getCleanShadow(&I)); |
| 1247 | if (!ClPoisonStack) return; |
| 1248 | IRBuilder<> IRB(I.getNextNode()); |
| 1249 | uint64_t Size = MS.TD->getTypeAllocSize(I.getAllocatedType()); |
| 1250 | if (ClPoisonStackWithCall) { |
| 1251 | IRB.CreateCall2(MS.MsanPoisonStackFn, |
| 1252 | IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), |
| 1253 | ConstantInt::get(MS.IntptrTy, Size)); |
| 1254 | } else { |
| 1255 | Value *ShadowBase = getShadowPtr(&I, Type::getInt8PtrTy(*MS.C), IRB); |
| 1256 | IRB.CreateMemSet(ShadowBase, IRB.getInt8(ClPoisonStackPattern), |
| 1257 | Size, I.getAlignment()); |
| 1258 | } |
| 1259 | |
| 1260 | if (ClTrackOrigins) { |
| 1261 | setOrigin(&I, getCleanOrigin()); |
| 1262 | SmallString<2048> StackDescriptionStorage; |
| 1263 | raw_svector_ostream StackDescription(StackDescriptionStorage); |
| 1264 | // We create a string with a description of the stack allocation and |
| 1265 | // pass it into __msan_set_alloca_origin. |
| 1266 | // It will be printed by the run-time if stack-originated UMR is found. |
| 1267 | // The first 4 bytes of the string are set to '----' and will be replaced |
| 1268 | // by __msan_va_arg_overflow_size_tls at the first call. |
| 1269 | StackDescription << "----" << I.getName() << "@" << F.getName(); |
| 1270 | Value *Descr = |
| 1271 | createPrivateNonConstGlobalForString(*F.getParent(), |
| 1272 | StackDescription.str()); |
| 1273 | IRB.CreateCall3(MS.MsanSetAllocaOriginFn, |
| 1274 | IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), |
| 1275 | ConstantInt::get(MS.IntptrTy, Size), |
| 1276 | IRB.CreatePointerCast(Descr, IRB.getInt8PtrTy())); |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | void visitSelectInst(SelectInst& I) { |
| 1281 | IRBuilder<> IRB(&I); |
| 1282 | setShadow(&I, IRB.CreateSelect(I.getCondition(), |
| 1283 | getShadow(I.getTrueValue()), getShadow(I.getFalseValue()), |
| 1284 | "_msprop")); |
| 1285 | if (ClTrackOrigins) |
| 1286 | setOrigin(&I, IRB.CreateSelect(I.getCondition(), |
| 1287 | getOrigin(I.getTrueValue()), getOrigin(I.getFalseValue()))); |
| 1288 | } |
| 1289 | |
| 1290 | void visitLandingPadInst(LandingPadInst &I) { |
| 1291 | // Do nothing. |
| 1292 | // See http://code.google.com/p/memory-sanitizer/issues/detail?id=1 |
| 1293 | setShadow(&I, getCleanShadow(&I)); |
| 1294 | setOrigin(&I, getCleanOrigin()); |
| 1295 | } |
| 1296 | |
| 1297 | void visitGetElementPtrInst(GetElementPtrInst &I) { |
| 1298 | handleShadowOr(I); |
| 1299 | } |
| 1300 | |
| 1301 | void visitExtractValueInst(ExtractValueInst &I) { |
| 1302 | IRBuilder<> IRB(&I); |
| 1303 | Value *Agg = I.getAggregateOperand(); |
| 1304 | DEBUG(dbgs() << "ExtractValue: " << I << "\n"); |
| 1305 | Value *AggShadow = getShadow(Agg); |
| 1306 | DEBUG(dbgs() << " AggShadow: " << *AggShadow << "\n"); |
| 1307 | Value *ResShadow = IRB.CreateExtractValue(AggShadow, I.getIndices()); |
| 1308 | DEBUG(dbgs() << " ResShadow: " << *ResShadow << "\n"); |
| 1309 | setShadow(&I, ResShadow); |
| 1310 | setOrigin(&I, getCleanOrigin()); |
| 1311 | } |
| 1312 | |
| 1313 | void visitInsertValueInst(InsertValueInst &I) { |
| 1314 | IRBuilder<> IRB(&I); |
| 1315 | DEBUG(dbgs() << "InsertValue: " << I << "\n"); |
| 1316 | Value *AggShadow = getShadow(I.getAggregateOperand()); |
| 1317 | Value *InsShadow = getShadow(I.getInsertedValueOperand()); |
| 1318 | DEBUG(dbgs() << " AggShadow: " << *AggShadow << "\n"); |
| 1319 | DEBUG(dbgs() << " InsShadow: " << *InsShadow << "\n"); |
| 1320 | Value *Res = IRB.CreateInsertValue(AggShadow, InsShadow, I.getIndices()); |
| 1321 | DEBUG(dbgs() << " Res: " << *Res << "\n"); |
| 1322 | setShadow(&I, Res); |
| 1323 | setOrigin(&I, getCleanOrigin()); |
| 1324 | } |
| 1325 | |
| 1326 | void dumpInst(Instruction &I) { |
| 1327 | if (CallInst *CI = dyn_cast<CallInst>(&I)) { |
| 1328 | errs() << "ZZZ call " << CI->getCalledFunction()->getName() << "\n"; |
| 1329 | } else { |
| 1330 | errs() << "ZZZ " << I.getOpcodeName() << "\n"; |
| 1331 | } |
| 1332 | errs() << "QQQ " << I << "\n"; |
| 1333 | } |
| 1334 | |
| 1335 | void visitResumeInst(ResumeInst &I) { |
| 1336 | DEBUG(dbgs() << "Resume: " << I << "\n"); |
| 1337 | // Nothing to do here. |
| 1338 | } |
| 1339 | |
| 1340 | void visitInstruction(Instruction &I) { |
| 1341 | // Everything else: stop propagating and check for poisoned shadow. |
| 1342 | if (ClDumpStrictInstructions) |
| 1343 | dumpInst(I); |
| 1344 | DEBUG(dbgs() << "DEFAULT: " << I << "\n"); |
| 1345 | for (size_t i = 0, n = I.getNumOperands(); i < n; i++) |
| 1346 | insertCheck(I.getOperand(i), &I); |
| 1347 | setShadow(&I, getCleanShadow(&I)); |
| 1348 | setOrigin(&I, getCleanOrigin()); |
| 1349 | } |
| 1350 | }; |
| 1351 | |
| 1352 | /// \brief AMD64-specific implementation of VarArgHelper. |
| 1353 | struct VarArgAMD64Helper : public VarArgHelper { |
| 1354 | // An unfortunate workaround for asymmetric lowering of va_arg stuff. |
| 1355 | // See a comment in visitCallSite for more details. |
| 1356 | static const unsigned AMD64GpEndOffset = 48; // AMD64 ABI Draft 0.99.6 p3.5.7 |
| 1357 | static const unsigned AMD64FpEndOffset = 176; |
| 1358 | |
| 1359 | Function &F; |
| 1360 | MemorySanitizer &MS; |
| 1361 | MemorySanitizerVisitor &MSV; |
| 1362 | Value *VAArgTLSCopy; |
| 1363 | Value *VAArgOverflowSize; |
| 1364 | |
| 1365 | SmallVector<CallInst*, 16> VAStartInstrumentationList; |
| 1366 | |
| 1367 | VarArgAMD64Helper(Function &F, MemorySanitizer &MS, |
| 1368 | MemorySanitizerVisitor &MSV) |
| 1369 | : F(F), MS(MS), MSV(MSV), VAArgTLSCopy(0), VAArgOverflowSize(0) { } |
| 1370 | |
| 1371 | enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory }; |
| 1372 | |
| 1373 | ArgKind classifyArgument(Value* arg) { |
| 1374 | // A very rough approximation of X86_64 argument classification rules. |
| 1375 | Type *T = arg->getType(); |
| 1376 | if (T->isFPOrFPVectorTy() || T->isX86_MMXTy()) |
| 1377 | return AK_FloatingPoint; |
| 1378 | if (T->isIntegerTy() && T->getPrimitiveSizeInBits() <= 64) |
| 1379 | return AK_GeneralPurpose; |
| 1380 | if (T->isPointerTy()) |
| 1381 | return AK_GeneralPurpose; |
| 1382 | return AK_Memory; |
| 1383 | } |
| 1384 | |
| 1385 | // For VarArg functions, store the argument shadow in an ABI-specific format |
| 1386 | // that corresponds to va_list layout. |
| 1387 | // We do this because Clang lowers va_arg in the frontend, and this pass |
| 1388 | // only sees the low level code that deals with va_list internals. |
| 1389 | // A much easier alternative (provided that Clang emits va_arg instructions) |
| 1390 | // would have been to associate each live instance of va_list with a copy of |
| 1391 | // MSanParamTLS, and extract shadow on va_arg() call in the argument list |
| 1392 | // order. |
| 1393 | void visitCallSite(CallSite &CS, IRBuilder<> &IRB) { |
| 1394 | unsigned GpOffset = 0; |
| 1395 | unsigned FpOffset = AMD64GpEndOffset; |
| 1396 | unsigned OverflowOffset = AMD64FpEndOffset; |
| 1397 | for (CallSite::arg_iterator ArgIt = CS.arg_begin(), End = CS.arg_end(); |
| 1398 | ArgIt != End; ++ArgIt) { |
| 1399 | Value *A = *ArgIt; |
| 1400 | ArgKind AK = classifyArgument(A); |
| 1401 | if (AK == AK_GeneralPurpose && GpOffset >= AMD64GpEndOffset) |
| 1402 | AK = AK_Memory; |
| 1403 | if (AK == AK_FloatingPoint && FpOffset >= AMD64FpEndOffset) |
| 1404 | AK = AK_Memory; |
| 1405 | Value *Base; |
| 1406 | switch (AK) { |
| 1407 | case AK_GeneralPurpose: |
| 1408 | Base = getShadowPtrForVAArgument(A, IRB, GpOffset); |
| 1409 | GpOffset += 8; |
| 1410 | break; |
| 1411 | case AK_FloatingPoint: |
| 1412 | Base = getShadowPtrForVAArgument(A, IRB, FpOffset); |
| 1413 | FpOffset += 16; |
| 1414 | break; |
| 1415 | case AK_Memory: |
| 1416 | uint64_t ArgSize = MS.TD->getTypeAllocSize(A->getType()); |
| 1417 | Base = getShadowPtrForVAArgument(A, IRB, OverflowOffset); |
| 1418 | OverflowOffset += DataLayout::RoundUpAlignment(ArgSize, 8); |
| 1419 | } |
| 1420 | IRB.CreateStore(MSV.getShadow(A), Base); |
| 1421 | } |
| 1422 | Constant *OverflowSize = |
| 1423 | ConstantInt::get(IRB.getInt64Ty(), OverflowOffset - AMD64FpEndOffset); |
| 1424 | IRB.CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS); |
| 1425 | } |
| 1426 | |
| 1427 | /// \brief Compute the shadow address for a given va_arg. |
| 1428 | Value *getShadowPtrForVAArgument(Value *A, IRBuilder<> &IRB, |
| 1429 | int ArgOffset) { |
| 1430 | Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy); |
| 1431 | Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset)); |
| 1432 | return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(A), 0), |
| 1433 | "_msarg"); |
| 1434 | } |
| 1435 | |
| 1436 | void visitVAStartInst(VAStartInst &I) { |
| 1437 | IRBuilder<> IRB(&I); |
| 1438 | VAStartInstrumentationList.push_back(&I); |
| 1439 | Value *VAListTag = I.getArgOperand(0); |
| 1440 | Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB); |
| 1441 | |
| 1442 | // Unpoison the whole __va_list_tag. |
| 1443 | // FIXME: magic ABI constants. |
| 1444 | IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()), |
| 1445 | /* size */24, /* alignment */16, false); |
| 1446 | } |
| 1447 | |
| 1448 | void visitVACopyInst(VACopyInst &I) { |
| 1449 | IRBuilder<> IRB(&I); |
| 1450 | Value *VAListTag = I.getArgOperand(0); |
| 1451 | Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB); |
| 1452 | |
| 1453 | // Unpoison the whole __va_list_tag. |
| 1454 | // FIXME: magic ABI constants. |
| 1455 | IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()), |
| 1456 | /* size */ 24, /* alignment */ 16, false); |
| 1457 | } |
| 1458 | |
| 1459 | void finalizeInstrumentation() { |
| 1460 | assert(!VAArgOverflowSize && !VAArgTLSCopy && |
| 1461 | "finalizeInstrumentation called twice"); |
| 1462 | if (!VAStartInstrumentationList.empty()) { |
| 1463 | // If there is a va_start in this function, make a backup copy of |
| 1464 | // va_arg_tls somewhere in the function entry block. |
| 1465 | IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI()); |
| 1466 | VAArgOverflowSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS); |
| 1467 | Value *CopySize = |
| 1468 | IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, AMD64FpEndOffset), |
| 1469 | VAArgOverflowSize); |
| 1470 | VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize); |
| 1471 | IRB.CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8); |
| 1472 | } |
| 1473 | |
| 1474 | // Instrument va_start. |
| 1475 | // Copy va_list shadow from the backup copy of the TLS contents. |
| 1476 | for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) { |
| 1477 | CallInst *OrigInst = VAStartInstrumentationList[i]; |
| 1478 | IRBuilder<> IRB(OrigInst->getNextNode()); |
| 1479 | Value *VAListTag = OrigInst->getArgOperand(0); |
| 1480 | |
| 1481 | Value *RegSaveAreaPtrPtr = |
| 1482 | IRB.CreateIntToPtr( |
| 1483 | IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy), |
| 1484 | ConstantInt::get(MS.IntptrTy, 16)), |
| 1485 | Type::getInt64PtrTy(*MS.C)); |
| 1486 | Value *RegSaveAreaPtr = IRB.CreateLoad(RegSaveAreaPtrPtr); |
| 1487 | Value *RegSaveAreaShadowPtr = |
| 1488 | MSV.getShadowPtr(RegSaveAreaPtr, IRB.getInt8Ty(), IRB); |
| 1489 | IRB.CreateMemCpy(RegSaveAreaShadowPtr, VAArgTLSCopy, |
| 1490 | AMD64FpEndOffset, 16); |
| 1491 | |
| 1492 | Value *OverflowArgAreaPtrPtr = |
| 1493 | IRB.CreateIntToPtr( |
| 1494 | IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy), |
| 1495 | ConstantInt::get(MS.IntptrTy, 8)), |
| 1496 | Type::getInt64PtrTy(*MS.C)); |
| 1497 | Value *OverflowArgAreaPtr = IRB.CreateLoad(OverflowArgAreaPtrPtr); |
| 1498 | Value *OverflowArgAreaShadowPtr = |
| 1499 | MSV.getShadowPtr(OverflowArgAreaPtr, IRB.getInt8Ty(), IRB); |
| 1500 | Value *SrcPtr = |
| 1501 | getShadowPtrForVAArgument(VAArgTLSCopy, IRB, AMD64FpEndOffset); |
| 1502 | IRB.CreateMemCpy(OverflowArgAreaShadowPtr, SrcPtr, VAArgOverflowSize, 16); |
| 1503 | } |
| 1504 | } |
| 1505 | }; |
| 1506 | |
| 1507 | VarArgHelper* CreateVarArgHelper(Function &Func, MemorySanitizer &Msan, |
| 1508 | MemorySanitizerVisitor &Visitor) { |
| 1509 | return new VarArgAMD64Helper(Func, Msan, Visitor); |
| 1510 | } |
| 1511 | |
| 1512 | } // namespace |
| 1513 | |
| 1514 | bool MemorySanitizer::runOnFunction(Function &F) { |
| 1515 | MemorySanitizerVisitor Visitor(F, *this); |
| 1516 | |
| 1517 | // Clear out readonly/readnone attributes. |
| 1518 | AttrBuilder B; |
| 1519 | B.addAttribute(Attributes::ReadOnly) |
| 1520 | .addAttribute(Attributes::ReadNone); |
| 1521 | F.removeAttribute(AttrListPtr::FunctionIndex, |
| 1522 | Attributes::get(F.getContext(), B)); |
| 1523 | |
| 1524 | return Visitor.runOnFunction(); |
| 1525 | } |