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