Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 1 | //===-- ThreadSanitizer.cpp - race detector -------------------------------===// |
| 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 | // |
| 10 | // This file is a part of ThreadSanitizer, a race detector. |
| 11 | // |
| 12 | // The tool is under development, for the details about previous versions see |
| 13 | // http://code.google.com/p/data-race-test |
| 14 | // |
| 15 | // The instrumentation phase is quite simple: |
| 16 | // - Insert calls to run-time library before every memory access. |
| 17 | // - Optimizations may apply to avoid instrumenting some of the accesses. |
| 18 | // - Insert calls at function entry/exit. |
| 19 | // The rest is handled by the run-time library. |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #define DEBUG_TYPE "tsan" |
| 23 | |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Instrumentation.h" |
Kostya Serebryany | b5b86d2 | 2012-08-24 16:44:47 +0000 | [diff] [blame] | 25 | #include "BlackList.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallSet.h" |
| 27 | #include "llvm/ADT/SmallString.h" |
| 28 | #include "llvm/ADT/SmallVector.h" |
| 29 | #include "llvm/ADT/Statistic.h" |
| 30 | #include "llvm/ADT/StringExtras.h" |
| 31 | #include "llvm/DataLayout.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 32 | #include "llvm/Function.h" |
| 33 | #include "llvm/IRBuilder.h" |
| 34 | #include "llvm/Intrinsics.h" |
| 35 | #include "llvm/LLVMContext.h" |
| 36 | #include "llvm/Metadata.h" |
| 37 | #include "llvm/Module.h" |
Kostya Serebryany | 6e590e3 | 2012-03-14 23:33:24 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MathExtras.h" |
Kostya Serebryany | 52eb6992 | 2012-03-26 17:35:03 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 43 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 44 | #include "llvm/Type.h" |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 45 | |
| 46 | using namespace llvm; |
| 47 | |
Alexey Samsonov | f045df1 | 2012-12-28 09:30:44 +0000 | [diff] [blame^] | 48 | static cl::opt<std::string> ClBlacklistFile("tsan-blacklist", |
Kostya Serebryany | 6e590e3 | 2012-03-14 23:33:24 +0000 | [diff] [blame] | 49 | cl::desc("Blacklist file"), cl::Hidden); |
Kostya Serebryany | 41d876c | 2012-10-04 05:28:50 +0000 | [diff] [blame] | 50 | static cl::opt<bool> ClInstrumentMemoryAccesses( |
| 51 | "tsan-instrument-memory-accesses", cl::init(true), |
| 52 | cl::desc("Instrument memory accesses"), cl::Hidden); |
| 53 | static cl::opt<bool> ClInstrumentFuncEntryExit( |
| 54 | "tsan-instrument-func-entry-exit", cl::init(true), |
| 55 | cl::desc("Instrument function entry and exit"), cl::Hidden); |
| 56 | static cl::opt<bool> ClInstrumentAtomics( |
| 57 | "tsan-instrument-atomics", cl::init(true), |
| 58 | cl::desc("Instrument atomics"), cl::Hidden); |
Kostya Serebryany | 6e590e3 | 2012-03-14 23:33:24 +0000 | [diff] [blame] | 59 | |
Kostya Serebryany | 2d5fdf8 | 2012-04-23 08:44:59 +0000 | [diff] [blame] | 60 | STATISTIC(NumInstrumentedReads, "Number of instrumented reads"); |
| 61 | STATISTIC(NumInstrumentedWrites, "Number of instrumented writes"); |
Alexey Samsonov | 1dfe9b5 | 2012-08-30 13:47:13 +0000 | [diff] [blame] | 62 | STATISTIC(NumOmittedReadsBeforeWrite, |
Kostya Serebryany | 2d5fdf8 | 2012-04-23 08:44:59 +0000 | [diff] [blame] | 63 | "Number of reads ignored due to following writes"); |
| 64 | STATISTIC(NumAccessesWithBadSize, "Number of accesses with bad size"); |
| 65 | STATISTIC(NumInstrumentedVtableWrites, "Number of vtable ptr writes"); |
| 66 | STATISTIC(NumOmittedReadsFromConstantGlobals, |
| 67 | "Number of reads from constant globals"); |
| 68 | STATISTIC(NumOmittedReadsFromVtable, "Number of vtable reads"); |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 69 | |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 70 | namespace { |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 71 | |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 72 | /// ThreadSanitizer: instrument the code in module to find races. |
| 73 | struct ThreadSanitizer : public FunctionPass { |
Alexey Samsonov | f045df1 | 2012-12-28 09:30:44 +0000 | [diff] [blame^] | 74 | ThreadSanitizer(StringRef BlacklistFile = StringRef()) |
| 75 | : FunctionPass(ID), |
| 76 | TD(0), |
| 77 | BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile |
| 78 | : BlacklistFile) { } |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 79 | const char *getPassName() const; |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 80 | bool runOnFunction(Function &F); |
| 81 | bool doInitialization(Module &M); |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 82 | static char ID; // Pass identification, replacement for typeid. |
| 83 | |
| 84 | private: |
Kostya Serebryany | 8b390ff | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 85 | void initializeCallbacks(Module &M); |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 86 | bool instrumentLoadOrStore(Instruction *I); |
| 87 | bool instrumentAtomic(Instruction *I); |
Kostya Serebryany | 37cb9ac | 2012-05-02 13:12:19 +0000 | [diff] [blame] | 88 | void chooseInstructionsToInstrument(SmallVectorImpl<Instruction*> &Local, |
| 89 | SmallVectorImpl<Instruction*> &All); |
Kostya Serebryany | cff60c1 | 2012-04-10 22:29:17 +0000 | [diff] [blame] | 90 | bool addrPointsToConstantData(Value *Addr); |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 91 | int getMemoryAccessFuncIndex(Value *Addr); |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 92 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 93 | DataLayout *TD; |
Alexey Samsonov | f045df1 | 2012-12-28 09:30:44 +0000 | [diff] [blame^] | 94 | SmallString<64> BlacklistFile; |
Kostya Serebryany | b5b86d2 | 2012-08-24 16:44:47 +0000 | [diff] [blame] | 95 | OwningPtr<BlackList> BL; |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 96 | IntegerType *OrdTy; |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 97 | // Callbacks to run-time library are computed in doInitialization. |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 98 | Function *TsanFuncEntry; |
| 99 | Function *TsanFuncExit; |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 100 | // Accesses sizes are powers of two: 1, 2, 4, 8, 16. |
Kostya Serebryany | 3eccaa6 | 2012-02-14 00:52:07 +0000 | [diff] [blame] | 101 | static const size_t kNumberOfAccessSizes = 5; |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 102 | Function *TsanRead[kNumberOfAccessSizes]; |
| 103 | Function *TsanWrite[kNumberOfAccessSizes]; |
| 104 | Function *TsanAtomicLoad[kNumberOfAccessSizes]; |
| 105 | Function *TsanAtomicStore[kNumberOfAccessSizes]; |
Dmitry Vyukov | 9f8a90b | 2012-11-09 12:55:36 +0000 | [diff] [blame] | 106 | Function *TsanAtomicRMW[AtomicRMWInst::LAST_BINOP + 1][kNumberOfAccessSizes]; |
| 107 | Function *TsanAtomicCAS[kNumberOfAccessSizes]; |
| 108 | Function *TsanAtomicThreadFence; |
| 109 | Function *TsanAtomicSignalFence; |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 110 | Function *TsanVptrUpdate; |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 111 | }; |
| 112 | } // namespace |
| 113 | |
| 114 | char ThreadSanitizer::ID = 0; |
| 115 | INITIALIZE_PASS(ThreadSanitizer, "tsan", |
| 116 | "ThreadSanitizer: detects data races.", |
| 117 | false, false) |
| 118 | |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 119 | const char *ThreadSanitizer::getPassName() const { |
| 120 | return "ThreadSanitizer"; |
| 121 | } |
| 122 | |
Alexey Samsonov | f045df1 | 2012-12-28 09:30:44 +0000 | [diff] [blame^] | 123 | FunctionPass *llvm::createThreadSanitizerPass(StringRef BlacklistFile) { |
| 124 | return new ThreadSanitizer(BlacklistFile); |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 127 | static Function *checkInterfaceFunction(Constant *FuncOrBitcast) { |
| 128 | if (Function *F = dyn_cast<Function>(FuncOrBitcast)) |
| 129 | return F; |
| 130 | FuncOrBitcast->dump(); |
| 131 | report_fatal_error("ThreadSanitizer interface function redefined"); |
| 132 | } |
| 133 | |
Kostya Serebryany | 8b390ff | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 134 | void ThreadSanitizer::initializeCallbacks(Module &M) { |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 135 | IRBuilder<> IRB(M.getContext()); |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 136 | // Initialize the callbacks. |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 137 | TsanFuncEntry = checkInterfaceFunction(M.getOrInsertFunction( |
| 138 | "__tsan_func_entry", IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL)); |
| 139 | TsanFuncExit = checkInterfaceFunction(M.getOrInsertFunction( |
| 140 | "__tsan_func_exit", IRB.getVoidTy(), NULL)); |
| 141 | OrdTy = IRB.getInt32Ty(); |
Kostya Serebryany | 3eccaa6 | 2012-02-14 00:52:07 +0000 | [diff] [blame] | 142 | for (size_t i = 0; i < kNumberOfAccessSizes; ++i) { |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 143 | const size_t ByteSize = 1 << i; |
| 144 | const size_t BitSize = ByteSize * 8; |
| 145 | SmallString<32> ReadName("__tsan_read" + itostr(ByteSize)); |
| 146 | TsanRead[i] = checkInterfaceFunction(M.getOrInsertFunction( |
| 147 | ReadName, IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL)); |
| 148 | |
| 149 | SmallString<32> WriteName("__tsan_write" + itostr(ByteSize)); |
| 150 | TsanWrite[i] = checkInterfaceFunction(M.getOrInsertFunction( |
| 151 | WriteName, IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL)); |
| 152 | |
| 153 | Type *Ty = Type::getIntNTy(M.getContext(), BitSize); |
| 154 | Type *PtrTy = Ty->getPointerTo(); |
| 155 | SmallString<32> AtomicLoadName("__tsan_atomic" + itostr(BitSize) + |
| 156 | "_load"); |
| 157 | TsanAtomicLoad[i] = checkInterfaceFunction(M.getOrInsertFunction( |
| 158 | AtomicLoadName, Ty, PtrTy, OrdTy, NULL)); |
| 159 | |
| 160 | SmallString<32> AtomicStoreName("__tsan_atomic" + itostr(BitSize) + |
| 161 | "_store"); |
| 162 | TsanAtomicStore[i] = checkInterfaceFunction(M.getOrInsertFunction( |
| 163 | AtomicStoreName, IRB.getVoidTy(), PtrTy, Ty, OrdTy, |
| 164 | NULL)); |
Dmitry Vyukov | 9f8a90b | 2012-11-09 12:55:36 +0000 | [diff] [blame] | 165 | |
| 166 | for (int op = AtomicRMWInst::FIRST_BINOP; |
| 167 | op <= AtomicRMWInst::LAST_BINOP; ++op) { |
| 168 | TsanAtomicRMW[op][i] = NULL; |
| 169 | const char *NamePart = NULL; |
| 170 | if (op == AtomicRMWInst::Xchg) |
| 171 | NamePart = "_exchange"; |
| 172 | else if (op == AtomicRMWInst::Add) |
| 173 | NamePart = "_fetch_add"; |
| 174 | else if (op == AtomicRMWInst::Sub) |
| 175 | NamePart = "_fetch_sub"; |
| 176 | else if (op == AtomicRMWInst::And) |
| 177 | NamePart = "_fetch_and"; |
| 178 | else if (op == AtomicRMWInst::Or) |
| 179 | NamePart = "_fetch_or"; |
| 180 | else if (op == AtomicRMWInst::Xor) |
| 181 | NamePart = "_fetch_xor"; |
Dmitry Vyukov | b10675e | 2012-11-27 08:09:25 +0000 | [diff] [blame] | 182 | else if (op == AtomicRMWInst::Nand) |
| 183 | NamePart = "_fetch_nand"; |
Dmitry Vyukov | 9f8a90b | 2012-11-09 12:55:36 +0000 | [diff] [blame] | 184 | else |
| 185 | continue; |
| 186 | SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart); |
| 187 | TsanAtomicRMW[op][i] = checkInterfaceFunction(M.getOrInsertFunction( |
| 188 | RMWName, Ty, PtrTy, Ty, OrdTy, NULL)); |
| 189 | } |
| 190 | |
| 191 | SmallString<32> AtomicCASName("__tsan_atomic" + itostr(BitSize) + |
| 192 | "_compare_exchange_val"); |
| 193 | TsanAtomicCAS[i] = checkInterfaceFunction(M.getOrInsertFunction( |
Dmitry Vyukov | 6702e53 | 2012-11-26 11:36:19 +0000 | [diff] [blame] | 194 | AtomicCASName, Ty, PtrTy, Ty, Ty, OrdTy, OrdTy, NULL)); |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 195 | } |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 196 | TsanVptrUpdate = checkInterfaceFunction(M.getOrInsertFunction( |
| 197 | "__tsan_vptr_update", IRB.getVoidTy(), IRB.getInt8PtrTy(), |
| 198 | IRB.getInt8PtrTy(), NULL)); |
Dmitry Vyukov | 9f8a90b | 2012-11-09 12:55:36 +0000 | [diff] [blame] | 199 | TsanAtomicThreadFence = checkInterfaceFunction(M.getOrInsertFunction( |
| 200 | "__tsan_atomic_thread_fence", IRB.getVoidTy(), OrdTy, NULL)); |
| 201 | TsanAtomicSignalFence = checkInterfaceFunction(M.getOrInsertFunction( |
| 202 | "__tsan_atomic_signal_fence", IRB.getVoidTy(), OrdTy, NULL)); |
Kostya Serebryany | 8b390ff | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | bool ThreadSanitizer::doInitialization(Module &M) { |
| 206 | TD = getAnalysisIfAvailable<DataLayout>(); |
| 207 | if (!TD) |
| 208 | return false; |
Alexey Samsonov | f045df1 | 2012-12-28 09:30:44 +0000 | [diff] [blame^] | 209 | BL.reset(new BlackList(BlacklistFile)); |
Kostya Serebryany | 8b390ff | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 210 | |
| 211 | // Always insert a call to __tsan_init into the module's CTORs. |
| 212 | IRBuilder<> IRB(M.getContext()); |
| 213 | Value *TsanInit = M.getOrInsertFunction("__tsan_init", |
| 214 | IRB.getVoidTy(), NULL); |
| 215 | appendToGlobalCtors(M, cast<Function>(TsanInit), 0); |
| 216 | |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
Kostya Serebryany | cff60c1 | 2012-04-10 22:29:17 +0000 | [diff] [blame] | 220 | static bool isVtableAccess(Instruction *I) { |
| 221 | if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa)) { |
| 222 | if (Tag->getNumOperands() < 1) return false; |
| 223 | if (MDString *Tag1 = dyn_cast<MDString>(Tag->getOperand(0))) { |
| 224 | if (Tag1->getString() == "vtable pointer") return true; |
| 225 | } |
| 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | bool ThreadSanitizer::addrPointsToConstantData(Value *Addr) { |
| 231 | // If this is a GEP, just analyze its pointer operand. |
| 232 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr)) |
| 233 | Addr = GEP->getPointerOperand(); |
| 234 | |
| 235 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) { |
| 236 | if (GV->isConstant()) { |
| 237 | // Reads from constant globals can not race with any writes. |
Kostya Serebryany | 2d5fdf8 | 2012-04-23 08:44:59 +0000 | [diff] [blame] | 238 | NumOmittedReadsFromConstantGlobals++; |
Kostya Serebryany | cff60c1 | 2012-04-10 22:29:17 +0000 | [diff] [blame] | 239 | return true; |
| 240 | } |
Alexey Samsonov | 1dfe9b5 | 2012-08-30 13:47:13 +0000 | [diff] [blame] | 241 | } else if (LoadInst *L = dyn_cast<LoadInst>(Addr)) { |
Kostya Serebryany | cff60c1 | 2012-04-10 22:29:17 +0000 | [diff] [blame] | 242 | if (isVtableAccess(L)) { |
| 243 | // Reads from a vtable pointer can not race with any writes. |
Kostya Serebryany | 2d5fdf8 | 2012-04-23 08:44:59 +0000 | [diff] [blame] | 244 | NumOmittedReadsFromVtable++; |
Kostya Serebryany | cff60c1 | 2012-04-10 22:29:17 +0000 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 251 | // Instrumenting some of the accesses may be proven redundant. |
| 252 | // Currently handled: |
| 253 | // - read-before-write (within same BB, no calls between) |
| 254 | // |
| 255 | // We do not handle some of the patterns that should not survive |
| 256 | // after the classic compiler optimizations. |
| 257 | // E.g. two reads from the same temp should be eliminated by CSE, |
| 258 | // two writes should be eliminated by DSE, etc. |
| 259 | // |
| 260 | // 'Local' is a vector of insns within the same BB (no calls between). |
| 261 | // 'All' is a vector of insns that will be instrumented. |
Kostya Serebryany | 37cb9ac | 2012-05-02 13:12:19 +0000 | [diff] [blame] | 262 | void ThreadSanitizer::chooseInstructionsToInstrument( |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 263 | SmallVectorImpl<Instruction*> &Local, |
| 264 | SmallVectorImpl<Instruction*> &All) { |
| 265 | SmallSet<Value*, 8> WriteTargets; |
| 266 | // Iterate from the end. |
| 267 | for (SmallVectorImpl<Instruction*>::reverse_iterator It = Local.rbegin(), |
| 268 | E = Local.rend(); It != E; ++It) { |
| 269 | Instruction *I = *It; |
| 270 | if (StoreInst *Store = dyn_cast<StoreInst>(I)) { |
| 271 | WriteTargets.insert(Store->getPointerOperand()); |
| 272 | } else { |
| 273 | LoadInst *Load = cast<LoadInst>(I); |
Kostya Serebryany | cff60c1 | 2012-04-10 22:29:17 +0000 | [diff] [blame] | 274 | Value *Addr = Load->getPointerOperand(); |
| 275 | if (WriteTargets.count(Addr)) { |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 276 | // We will write to this temp, so no reason to analyze the read. |
Kostya Serebryany | 2d5fdf8 | 2012-04-23 08:44:59 +0000 | [diff] [blame] | 277 | NumOmittedReadsBeforeWrite++; |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 278 | continue; |
| 279 | } |
Kostya Serebryany | cff60c1 | 2012-04-10 22:29:17 +0000 | [diff] [blame] | 280 | if (addrPointsToConstantData(Addr)) { |
| 281 | // Addr points to some constant data -- it can not race with any writes. |
| 282 | continue; |
| 283 | } |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 284 | } |
| 285 | All.push_back(I); |
| 286 | } |
| 287 | Local.clear(); |
| 288 | } |
| 289 | |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 290 | static bool isAtomic(Instruction *I) { |
| 291 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 292 | return LI->isAtomic() && LI->getSynchScope() == CrossThread; |
Kostya Serebryany | 37cb9ac | 2012-05-02 13:12:19 +0000 | [diff] [blame] | 293 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 294 | return SI->isAtomic() && SI->getSynchScope() == CrossThread; |
Kostya Serebryany | 37cb9ac | 2012-05-02 13:12:19 +0000 | [diff] [blame] | 295 | if (isa<AtomicRMWInst>(I)) |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 296 | return true; |
Kostya Serebryany | 37cb9ac | 2012-05-02 13:12:19 +0000 | [diff] [blame] | 297 | if (isa<AtomicCmpXchgInst>(I)) |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 298 | return true; |
Dmitry Vyukov | 9f8a90b | 2012-11-09 12:55:36 +0000 | [diff] [blame] | 299 | if (isa<FenceInst>(I)) |
| 300 | return true; |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 301 | return false; |
| 302 | } |
| 303 | |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 304 | bool ThreadSanitizer::runOnFunction(Function &F) { |
| 305 | if (!TD) return false; |
Kostya Serebryany | 6e590e3 | 2012-03-14 23:33:24 +0000 | [diff] [blame] | 306 | if (BL->isIn(F)) return false; |
Kostya Serebryany | 8b390ff | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 307 | initializeCallbacks(*F.getParent()); |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 308 | SmallVector<Instruction*, 8> RetVec; |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 309 | SmallVector<Instruction*, 8> AllLoadsAndStores; |
| 310 | SmallVector<Instruction*, 8> LocalLoadsAndStores; |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 311 | SmallVector<Instruction*, 8> AtomicAccesses; |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 312 | bool Res = false; |
| 313 | bool HasCalls = false; |
| 314 | |
| 315 | // Traverse all instructions, collect loads/stores/returns, check for calls. |
| 316 | for (Function::iterator FI = F.begin(), FE = F.end(); |
| 317 | FI != FE; ++FI) { |
| 318 | BasicBlock &BB = *FI; |
| 319 | for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); |
| 320 | BI != BE; ++BI) { |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 321 | if (isAtomic(BI)) |
| 322 | AtomicAccesses.push_back(BI); |
| 323 | else if (isa<LoadInst>(BI) || isa<StoreInst>(BI)) |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 324 | LocalLoadsAndStores.push_back(BI); |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 325 | else if (isa<ReturnInst>(BI)) |
| 326 | RetVec.push_back(BI); |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 327 | else if (isa<CallInst>(BI) || isa<InvokeInst>(BI)) { |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 328 | HasCalls = true; |
Kostya Serebryany | 37cb9ac | 2012-05-02 13:12:19 +0000 | [diff] [blame] | 329 | chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores); |
Kostya Serebryany | 2076af0 | 2012-04-10 18:18:56 +0000 | [diff] [blame] | 330 | } |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 331 | } |
Kostya Serebryany | 37cb9ac | 2012-05-02 13:12:19 +0000 | [diff] [blame] | 332 | chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores); |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // We have collected all loads and stores. |
| 336 | // FIXME: many of these accesses do not need to be checked for races |
| 337 | // (e.g. variables that do not escape, etc). |
| 338 | |
| 339 | // Instrument memory accesses. |
Kostya Serebryany | 41d876c | 2012-10-04 05:28:50 +0000 | [diff] [blame] | 340 | if (ClInstrumentMemoryAccesses) |
| 341 | for (size_t i = 0, n = AllLoadsAndStores.size(); i < n; ++i) { |
| 342 | Res |= instrumentLoadOrStore(AllLoadsAndStores[i]); |
| 343 | } |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 344 | |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 345 | // Instrument atomic memory accesses. |
Kostya Serebryany | 41d876c | 2012-10-04 05:28:50 +0000 | [diff] [blame] | 346 | if (ClInstrumentAtomics) |
| 347 | for (size_t i = 0, n = AtomicAccesses.size(); i < n; ++i) { |
| 348 | Res |= instrumentAtomic(AtomicAccesses[i]); |
| 349 | } |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 350 | |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 351 | // Instrument function entry/exit points if there were instrumented accesses. |
Kostya Serebryany | 41d876c | 2012-10-04 05:28:50 +0000 | [diff] [blame] | 352 | if ((Res || HasCalls) && ClInstrumentFuncEntryExit) { |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 353 | IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI()); |
| 354 | Value *ReturnAddress = IRB.CreateCall( |
| 355 | Intrinsic::getDeclaration(F.getParent(), Intrinsic::returnaddress), |
| 356 | IRB.getInt32(0)); |
| 357 | IRB.CreateCall(TsanFuncEntry, ReturnAddress); |
| 358 | for (size_t i = 0, n = RetVec.size(); i < n; ++i) { |
| 359 | IRBuilder<> IRBRet(RetVec[i]); |
| 360 | IRBRet.CreateCall(TsanFuncExit); |
| 361 | } |
Kostya Serebryany | 52eb6992 | 2012-03-26 17:35:03 +0000 | [diff] [blame] | 362 | Res = true; |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 363 | } |
| 364 | return Res; |
| 365 | } |
| 366 | |
| 367 | bool ThreadSanitizer::instrumentLoadOrStore(Instruction *I) { |
| 368 | IRBuilder<> IRB(I); |
| 369 | bool IsWrite = isa<StoreInst>(*I); |
| 370 | Value *Addr = IsWrite |
| 371 | ? cast<StoreInst>(I)->getPointerOperand() |
| 372 | : cast<LoadInst>(I)->getPointerOperand(); |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 373 | int Idx = getMemoryAccessFuncIndex(Addr); |
| 374 | if (Idx < 0) |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 375 | return false; |
Kostya Serebryany | 52eb6992 | 2012-03-26 17:35:03 +0000 | [diff] [blame] | 376 | if (IsWrite && isVtableAccess(I)) { |
Kostya Serebryany | 4a002ab | 2012-07-05 09:07:31 +0000 | [diff] [blame] | 377 | DEBUG(dbgs() << " VPTR : " << *I << "\n"); |
Kostya Serebryany | 52eb6992 | 2012-03-26 17:35:03 +0000 | [diff] [blame] | 378 | Value *StoredValue = cast<StoreInst>(I)->getValueOperand(); |
Kostya Serebryany | 4a002ab | 2012-07-05 09:07:31 +0000 | [diff] [blame] | 379 | // StoredValue does not necessary have a pointer type. |
| 380 | if (isa<IntegerType>(StoredValue->getType())) |
| 381 | StoredValue = IRB.CreateIntToPtr(StoredValue, IRB.getInt8PtrTy()); |
| 382 | // Call TsanVptrUpdate. |
Kostya Serebryany | 52eb6992 | 2012-03-26 17:35:03 +0000 | [diff] [blame] | 383 | IRB.CreateCall2(TsanVptrUpdate, |
| 384 | IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()), |
| 385 | IRB.CreatePointerCast(StoredValue, IRB.getInt8PtrTy())); |
Kostya Serebryany | 2d5fdf8 | 2012-04-23 08:44:59 +0000 | [diff] [blame] | 386 | NumInstrumentedVtableWrites++; |
Kostya Serebryany | 52eb6992 | 2012-03-26 17:35:03 +0000 | [diff] [blame] | 387 | return true; |
| 388 | } |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 389 | Value *OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx]; |
| 390 | IRB.CreateCall(OnAccessFunc, IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy())); |
Kostya Serebryany | 2d5fdf8 | 2012-04-23 08:44:59 +0000 | [diff] [blame] | 391 | if (IsWrite) NumInstrumentedWrites++; |
| 392 | else NumInstrumentedReads++; |
Kostya Serebryany | 60ebb194 | 2012-02-13 22:50:51 +0000 | [diff] [blame] | 393 | return true; |
| 394 | } |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 395 | |
| 396 | static ConstantInt *createOrdering(IRBuilder<> *IRB, AtomicOrdering ord) { |
| 397 | uint32_t v = 0; |
| 398 | switch (ord) { |
| 399 | case NotAtomic: assert(false); |
| 400 | case Unordered: // Fall-through. |
Dmitry Vyukov | c2e9ca1 | 2012-11-09 14:12:16 +0000 | [diff] [blame] | 401 | case Monotonic: v = 0; break; |
Dmitry Vyukov | 9a33f9f | 2012-11-26 14:55:26 +0000 | [diff] [blame] | 402 | // case Consume: v = 1; break; // Not specified yet. |
Dmitry Vyukov | c2e9ca1 | 2012-11-09 14:12:16 +0000 | [diff] [blame] | 403 | case Acquire: v = 2; break; |
| 404 | case Release: v = 3; break; |
| 405 | case AcquireRelease: v = 4; break; |
| 406 | case SequentiallyConsistent: v = 5; break; |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 407 | } |
Dmitry Vyukov | c2e9ca1 | 2012-11-09 14:12:16 +0000 | [diff] [blame] | 408 | return IRB->getInt32(v); |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Dmitry Vyukov | 6702e53 | 2012-11-26 11:36:19 +0000 | [diff] [blame] | 411 | static ConstantInt *createFailOrdering(IRBuilder<> *IRB, AtomicOrdering ord) { |
| 412 | uint32_t v = 0; |
| 413 | switch (ord) { |
| 414 | case NotAtomic: assert(false); |
| 415 | case Unordered: // Fall-through. |
| 416 | case Monotonic: v = 0; break; |
Dmitry Vyukov | 9a33f9f | 2012-11-26 14:55:26 +0000 | [diff] [blame] | 417 | // case Consume: v = 1; break; // Not specified yet. |
Dmitry Vyukov | 6702e53 | 2012-11-26 11:36:19 +0000 | [diff] [blame] | 418 | case Acquire: v = 2; break; |
| 419 | case Release: v = 0; break; |
| 420 | case AcquireRelease: v = 2; break; |
| 421 | case SequentiallyConsistent: v = 5; break; |
| 422 | } |
| 423 | return IRB->getInt32(v); |
| 424 | } |
| 425 | |
| 426 | // Both llvm and ThreadSanitizer atomic operations are based on C++11/C1x |
| 427 | // standards. For background see C++11 standard. A slightly older, publically |
| 428 | // available draft of the standard (not entirely up-to-date, but close enough |
| 429 | // for casual browsing) is available here: |
Matt Beaumont-Gay | 70af909 | 2012-11-26 16:27:22 +0000 | [diff] [blame] | 430 | // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf |
Dmitry Vyukov | 6702e53 | 2012-11-26 11:36:19 +0000 | [diff] [blame] | 431 | // The following page contains more background information: |
| 432 | // http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/ |
| 433 | |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 434 | bool ThreadSanitizer::instrumentAtomic(Instruction *I) { |
| 435 | IRBuilder<> IRB(I); |
| 436 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 437 | Value *Addr = LI->getPointerOperand(); |
| 438 | int Idx = getMemoryAccessFuncIndex(Addr); |
| 439 | if (Idx < 0) |
| 440 | return false; |
| 441 | const size_t ByteSize = 1 << Idx; |
| 442 | const size_t BitSize = ByteSize * 8; |
| 443 | Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize); |
Micah Villmow | b8bce92 | 2012-10-24 17:25:11 +0000 | [diff] [blame] | 444 | Type *PtrTy = Ty->getPointerTo(); |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 445 | Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy), |
| 446 | createOrdering(&IRB, LI->getOrdering())}; |
| 447 | CallInst *C = CallInst::Create(TsanAtomicLoad[Idx], |
| 448 | ArrayRef<Value*>(Args)); |
| 449 | ReplaceInstWithInst(I, C); |
| 450 | |
| 451 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 452 | Value *Addr = SI->getPointerOperand(); |
| 453 | int Idx = getMemoryAccessFuncIndex(Addr); |
| 454 | if (Idx < 0) |
| 455 | return false; |
| 456 | const size_t ByteSize = 1 << Idx; |
| 457 | const size_t BitSize = ByteSize * 8; |
| 458 | Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize); |
Micah Villmow | b8bce92 | 2012-10-24 17:25:11 +0000 | [diff] [blame] | 459 | Type *PtrTy = Ty->getPointerTo(); |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 460 | Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy), |
| 461 | IRB.CreateIntCast(SI->getValueOperand(), Ty, false), |
| 462 | createOrdering(&IRB, SI->getOrdering())}; |
| 463 | CallInst *C = CallInst::Create(TsanAtomicStore[Idx], |
| 464 | ArrayRef<Value*>(Args)); |
| 465 | ReplaceInstWithInst(I, C); |
Dmitry Vyukov | 9f8a90b | 2012-11-09 12:55:36 +0000 | [diff] [blame] | 466 | } else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I)) { |
| 467 | Value *Addr = RMWI->getPointerOperand(); |
| 468 | int Idx = getMemoryAccessFuncIndex(Addr); |
| 469 | if (Idx < 0) |
| 470 | return false; |
| 471 | Function *F = TsanAtomicRMW[RMWI->getOperation()][Idx]; |
| 472 | if (F == NULL) |
| 473 | return false; |
| 474 | const size_t ByteSize = 1 << Idx; |
| 475 | const size_t BitSize = ByteSize * 8; |
| 476 | Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize); |
| 477 | Type *PtrTy = Ty->getPointerTo(); |
| 478 | Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy), |
| 479 | IRB.CreateIntCast(RMWI->getValOperand(), Ty, false), |
| 480 | createOrdering(&IRB, RMWI->getOrdering())}; |
| 481 | CallInst *C = CallInst::Create(F, ArrayRef<Value*>(Args)); |
| 482 | ReplaceInstWithInst(I, C); |
| 483 | } else if (AtomicCmpXchgInst *CASI = dyn_cast<AtomicCmpXchgInst>(I)) { |
| 484 | Value *Addr = CASI->getPointerOperand(); |
| 485 | int Idx = getMemoryAccessFuncIndex(Addr); |
| 486 | if (Idx < 0) |
| 487 | return false; |
| 488 | const size_t ByteSize = 1 << Idx; |
| 489 | const size_t BitSize = ByteSize * 8; |
| 490 | Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize); |
| 491 | Type *PtrTy = Ty->getPointerTo(); |
| 492 | Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy), |
| 493 | IRB.CreateIntCast(CASI->getCompareOperand(), Ty, false), |
| 494 | IRB.CreateIntCast(CASI->getNewValOperand(), Ty, false), |
Dmitry Vyukov | 6702e53 | 2012-11-26 11:36:19 +0000 | [diff] [blame] | 495 | createOrdering(&IRB, CASI->getOrdering()), |
| 496 | createFailOrdering(&IRB, CASI->getOrdering())}; |
Dmitry Vyukov | 9f8a90b | 2012-11-09 12:55:36 +0000 | [diff] [blame] | 497 | CallInst *C = CallInst::Create(TsanAtomicCAS[Idx], ArrayRef<Value*>(Args)); |
| 498 | ReplaceInstWithInst(I, C); |
| 499 | } else if (FenceInst *FI = dyn_cast<FenceInst>(I)) { |
| 500 | Value *Args[] = {createOrdering(&IRB, FI->getOrdering())}; |
| 501 | Function *F = FI->getSynchScope() == SingleThread ? |
| 502 | TsanAtomicSignalFence : TsanAtomicThreadFence; |
| 503 | CallInst *C = CallInst::Create(F, ArrayRef<Value*>(Args)); |
| 504 | ReplaceInstWithInst(I, C); |
Kostya Serebryany | e507922 | 2012-04-27 07:31:53 +0000 | [diff] [blame] | 505 | } |
| 506 | return true; |
| 507 | } |
| 508 | |
| 509 | int ThreadSanitizer::getMemoryAccessFuncIndex(Value *Addr) { |
| 510 | Type *OrigPtrTy = Addr->getType(); |
| 511 | Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType(); |
| 512 | assert(OrigTy->isSized()); |
| 513 | uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy); |
| 514 | if (TypeSize != 8 && TypeSize != 16 && |
| 515 | TypeSize != 32 && TypeSize != 64 && TypeSize != 128) { |
| 516 | NumAccessesWithBadSize++; |
| 517 | // Ignore all unusual sizes. |
| 518 | return -1; |
| 519 | } |
| 520 | size_t Idx = CountTrailingZeros_32(TypeSize / 8); |
| 521 | assert(Idx < kNumberOfAccessSizes); |
| 522 | return Idx; |
| 523 | } |