ThreadSanitizer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. #define DEBUG_TYPE "tsan"
  22. #include "FunctionBlackList.h"
  23. #include "llvm/Function.h"
  24. #include "llvm/IRBuilder.h"
  25. #include "llvm/Intrinsics.h"
  26. #include "llvm/LLVMContext.h"
  27. #include "llvm/Metadata.h"
  28. #include "llvm/Module.h"
  29. #include "llvm/Type.h"
  30. #include "llvm/ADT/SmallSet.h"
  31. #include "llvm/ADT/SmallString.h"
  32. #include "llvm/ADT/SmallVector.h"
  33. #include "llvm/ADT/Statistic.h"
  34. #include "llvm/ADT/StringExtras.h"
  35. #include "llvm/Support/CommandLine.h"
  36. #include "llvm/Support/Debug.h"
  37. #include "llvm/Support/MathExtras.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. #include "llvm/Target/TargetData.h"
  40. #include "llvm/Transforms/Instrumentation.h"
  41. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  42. #include "llvm/Transforms/Utils/ModuleUtils.h"
  43. using namespace llvm;
  44. static cl::opt<std::string> ClBlackListFile("tsan-blacklist",
  45. cl::desc("Blacklist file"), cl::Hidden);
  46. STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
  47. STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
  48. STATISTIC(NumOmittedReadsBeforeWrite,
  49. "Number of reads ignored due to following writes");
  50. STATISTIC(NumAccessesWithBadSize, "Number of accesses with bad size");
  51. STATISTIC(NumInstrumentedVtableWrites, "Number of vtable ptr writes");
  52. STATISTIC(NumOmittedReadsFromConstantGlobals,
  53. "Number of reads from constant globals");
  54. STATISTIC(NumOmittedReadsFromVtable, "Number of vtable reads");
  55. namespace {
  56. /// ThreadSanitizer: instrument the code in module to find races.
  57. struct ThreadSanitizer : public FunctionPass {
  58. ThreadSanitizer();
  59. const char *getPassName() const;
  60. bool runOnFunction(Function &F);
  61. bool doInitialization(Module &M);
  62. static char ID; // Pass identification, replacement for typeid.
  63. private:
  64. bool instrumentLoadOrStore(Instruction *I);
  65. bool instrumentAtomic(Instruction *I);
  66. void chooseInstructionsToInstrument(SmallVectorImpl<Instruction*> &Local,
  67. SmallVectorImpl<Instruction*> &All);
  68. bool addrPointsToConstantData(Value *Addr);
  69. int getMemoryAccessFuncIndex(Value *Addr);
  70. TargetData *TD;
  71. OwningPtr<FunctionBlackList> BL;
  72. IntegerType *OrdTy;
  73. // Callbacks to run-time library are computed in doInitialization.
  74. Function *TsanFuncEntry;
  75. Function *TsanFuncExit;
  76. // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
  77. static const size_t kNumberOfAccessSizes = 5;
  78. Function *TsanRead[kNumberOfAccessSizes];
  79. Function *TsanWrite[kNumberOfAccessSizes];
  80. Function *TsanAtomicLoad[kNumberOfAccessSizes];
  81. Function *TsanAtomicStore[kNumberOfAccessSizes];
  82. Function *TsanVptrUpdate;
  83. };
  84. } // namespace
  85. char ThreadSanitizer::ID = 0;
  86. INITIALIZE_PASS(ThreadSanitizer, "tsan",
  87. "ThreadSanitizer: detects data races.",
  88. false, false)
  89. const char *ThreadSanitizer::getPassName() const {
  90. return "ThreadSanitizer";
  91. }
  92. ThreadSanitizer::ThreadSanitizer()
  93. : FunctionPass(ID),
  94. TD(NULL) {
  95. }
  96. FunctionPass *llvm::createThreadSanitizerPass() {
  97. return new ThreadSanitizer();
  98. }
  99. static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
  100. if (Function *F = dyn_cast<Function>(FuncOrBitcast))
  101. return F;
  102. FuncOrBitcast->dump();
  103. report_fatal_error("ThreadSanitizer interface function redefined");
  104. }
  105. bool ThreadSanitizer::doInitialization(Module &M) {
  106. TD = getAnalysisIfAvailable<TargetData>();
  107. if (!TD)
  108. return false;
  109. BL.reset(new FunctionBlackList(ClBlackListFile));
  110. // Always insert a call to __tsan_init into the module's CTORs.
  111. IRBuilder<> IRB(M.getContext());
  112. Value *TsanInit = M.getOrInsertFunction("__tsan_init",
  113. IRB.getVoidTy(), NULL);
  114. appendToGlobalCtors(M, cast<Function>(TsanInit), 0);
  115. // Initialize the callbacks.
  116. TsanFuncEntry = checkInterfaceFunction(M.getOrInsertFunction(
  117. "__tsan_func_entry", IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
  118. TsanFuncExit = checkInterfaceFunction(M.getOrInsertFunction(
  119. "__tsan_func_exit", IRB.getVoidTy(), NULL));
  120. OrdTy = IRB.getInt32Ty();
  121. for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
  122. const size_t ByteSize = 1 << i;
  123. const size_t BitSize = ByteSize * 8;
  124. SmallString<32> ReadName("__tsan_read" + itostr(ByteSize));
  125. TsanRead[i] = checkInterfaceFunction(M.getOrInsertFunction(
  126. ReadName, IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
  127. SmallString<32> WriteName("__tsan_write" + itostr(ByteSize));
  128. TsanWrite[i] = checkInterfaceFunction(M.getOrInsertFunction(
  129. WriteName, IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
  130. Type *Ty = Type::getIntNTy(M.getContext(), BitSize);
  131. Type *PtrTy = Ty->getPointerTo();
  132. SmallString<32> AtomicLoadName("__tsan_atomic" + itostr(BitSize) +
  133. "_load");
  134. TsanAtomicLoad[i] = checkInterfaceFunction(M.getOrInsertFunction(
  135. AtomicLoadName, Ty, PtrTy, OrdTy, NULL));
  136. SmallString<32> AtomicStoreName("__tsan_atomic" + itostr(BitSize) +
  137. "_store");
  138. TsanAtomicStore[i] = checkInterfaceFunction(M.getOrInsertFunction(
  139. AtomicStoreName, IRB.getVoidTy(), PtrTy, Ty, OrdTy,
  140. NULL));
  141. }
  142. TsanVptrUpdate = checkInterfaceFunction(M.getOrInsertFunction(
  143. "__tsan_vptr_update", IRB.getVoidTy(), IRB.getInt8PtrTy(),
  144. IRB.getInt8PtrTy(), NULL));
  145. return true;
  146. }
  147. static bool isVtableAccess(Instruction *I) {
  148. if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa)) {
  149. if (Tag->getNumOperands() < 1) return false;
  150. if (MDString *Tag1 = dyn_cast<MDString>(Tag->getOperand(0))) {
  151. if (Tag1->getString() == "vtable pointer") return true;
  152. }
  153. }
  154. return false;
  155. }
  156. bool ThreadSanitizer::addrPointsToConstantData(Value *Addr) {
  157. // If this is a GEP, just analyze its pointer operand.
  158. if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr))
  159. Addr = GEP->getPointerOperand();
  160. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
  161. if (GV->isConstant()) {
  162. // Reads from constant globals can not race with any writes.
  163. NumOmittedReadsFromConstantGlobals++;
  164. return true;
  165. }
  166. } else if(LoadInst *L = dyn_cast<LoadInst>(Addr)) {
  167. if (isVtableAccess(L)) {
  168. // Reads from a vtable pointer can not race with any writes.
  169. NumOmittedReadsFromVtable++;
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. // Instrumenting some of the accesses may be proven redundant.
  176. // Currently handled:
  177. // - read-before-write (within same BB, no calls between)
  178. //
  179. // We do not handle some of the patterns that should not survive
  180. // after the classic compiler optimizations.
  181. // E.g. two reads from the same temp should be eliminated by CSE,
  182. // two writes should be eliminated by DSE, etc.
  183. //
  184. // 'Local' is a vector of insns within the same BB (no calls between).
  185. // 'All' is a vector of insns that will be instrumented.
  186. void ThreadSanitizer::chooseInstructionsToInstrument(
  187. SmallVectorImpl<Instruction*> &Local,
  188. SmallVectorImpl<Instruction*> &All) {
  189. SmallSet<Value*, 8> WriteTargets;
  190. // Iterate from the end.
  191. for (SmallVectorImpl<Instruction*>::reverse_iterator It = Local.rbegin(),
  192. E = Local.rend(); It != E; ++It) {
  193. Instruction *I = *It;
  194. if (StoreInst *Store = dyn_cast<StoreInst>(I)) {
  195. WriteTargets.insert(Store->getPointerOperand());
  196. } else {
  197. LoadInst *Load = cast<LoadInst>(I);
  198. Value *Addr = Load->getPointerOperand();
  199. if (WriteTargets.count(Addr)) {
  200. // We will write to this temp, so no reason to analyze the read.
  201. NumOmittedReadsBeforeWrite++;
  202. continue;
  203. }
  204. if (addrPointsToConstantData(Addr)) {
  205. // Addr points to some constant data -- it can not race with any writes.
  206. continue;
  207. }
  208. }
  209. All.push_back(I);
  210. }
  211. Local.clear();
  212. }
  213. static bool isAtomic(Instruction *I) {
  214. if (LoadInst *LI = dyn_cast<LoadInst>(I))
  215. return LI->isAtomic() && LI->getSynchScope() == CrossThread;
  216. if (StoreInst *SI = dyn_cast<StoreInst>(I))
  217. return SI->isAtomic() && SI->getSynchScope() == CrossThread;
  218. if (isa<AtomicRMWInst>(I))
  219. return true;
  220. if (isa<AtomicCmpXchgInst>(I))
  221. return true;
  222. if (FenceInst *FI = dyn_cast<FenceInst>(I))
  223. return FI->getSynchScope() == CrossThread;
  224. return false;
  225. }
  226. bool ThreadSanitizer::runOnFunction(Function &F) {
  227. if (!TD) return false;
  228. if (BL->isIn(F)) return false;
  229. SmallVector<Instruction*, 8> RetVec;
  230. SmallVector<Instruction*, 8> AllLoadsAndStores;
  231. SmallVector<Instruction*, 8> LocalLoadsAndStores;
  232. SmallVector<Instruction*, 8> AtomicAccesses;
  233. bool Res = false;
  234. bool HasCalls = false;
  235. // Traverse all instructions, collect loads/stores/returns, check for calls.
  236. for (Function::iterator FI = F.begin(), FE = F.end();
  237. FI != FE; ++FI) {
  238. BasicBlock &BB = *FI;
  239. for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
  240. BI != BE; ++BI) {
  241. if (isAtomic(BI))
  242. AtomicAccesses.push_back(BI);
  243. else if (isa<LoadInst>(BI) || isa<StoreInst>(BI))
  244. LocalLoadsAndStores.push_back(BI);
  245. else if (isa<ReturnInst>(BI))
  246. RetVec.push_back(BI);
  247. else if (isa<CallInst>(BI) || isa<InvokeInst>(BI)) {
  248. HasCalls = true;
  249. chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
  250. }
  251. }
  252. chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
  253. }
  254. // We have collected all loads and stores.
  255. // FIXME: many of these accesses do not need to be checked for races
  256. // (e.g. variables that do not escape, etc).
  257. // Instrument memory accesses.
  258. for (size_t i = 0, n = AllLoadsAndStores.size(); i < n; ++i) {
  259. Res |= instrumentLoadOrStore(AllLoadsAndStores[i]);
  260. }
  261. // Instrument atomic memory accesses.
  262. for (size_t i = 0, n = AtomicAccesses.size(); i < n; ++i) {
  263. Res |= instrumentAtomic(AtomicAccesses[i]);
  264. }
  265. // Instrument function entry/exit points if there were instrumented accesses.
  266. if (Res || HasCalls) {
  267. IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
  268. Value *ReturnAddress = IRB.CreateCall(
  269. Intrinsic::getDeclaration(F.getParent(), Intrinsic::returnaddress),
  270. IRB.getInt32(0));
  271. IRB.CreateCall(TsanFuncEntry, ReturnAddress);
  272. for (size_t i = 0, n = RetVec.size(); i < n; ++i) {
  273. IRBuilder<> IRBRet(RetVec[i]);
  274. IRBRet.CreateCall(TsanFuncExit);
  275. }
  276. Res = true;
  277. }
  278. return Res;
  279. }
  280. bool ThreadSanitizer::instrumentLoadOrStore(Instruction *I) {
  281. IRBuilder<> IRB(I);
  282. bool IsWrite = isa<StoreInst>(*I);
  283. Value *Addr = IsWrite
  284. ? cast<StoreInst>(I)->getPointerOperand()
  285. : cast<LoadInst>(I)->getPointerOperand();
  286. int Idx = getMemoryAccessFuncIndex(Addr);
  287. if (Idx < 0)
  288. return false;
  289. if (IsWrite && isVtableAccess(I)) {
  290. Value *StoredValue = cast<StoreInst>(I)->getValueOperand();
  291. IRB.CreateCall2(TsanVptrUpdate,
  292. IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
  293. IRB.CreatePointerCast(StoredValue, IRB.getInt8PtrTy()));
  294. NumInstrumentedVtableWrites++;
  295. return true;
  296. }
  297. Value *OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
  298. IRB.CreateCall(OnAccessFunc, IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
  299. if (IsWrite) NumInstrumentedWrites++;
  300. else NumInstrumentedReads++;
  301. return true;
  302. }
  303. static ConstantInt *createOrdering(IRBuilder<> *IRB, AtomicOrdering ord) {
  304. uint32_t v = 0;
  305. switch (ord) {
  306. case NotAtomic: assert(false);
  307. case Unordered: // Fall-through.
  308. case Monotonic: v = 1 << 0; break;
  309. // case Consume: v = 1 << 1; break; // Not specified yet.
  310. case Acquire: v = 1 << 2; break;
  311. case Release: v = 1 << 3; break;
  312. case AcquireRelease: v = 1 << 4; break;
  313. case SequentiallyConsistent: v = 1 << 5; break;
  314. }
  315. return IRB->getInt32(v);
  316. }
  317. bool ThreadSanitizer::instrumentAtomic(Instruction *I) {
  318. IRBuilder<> IRB(I);
  319. if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
  320. Value *Addr = LI->getPointerOperand();
  321. int Idx = getMemoryAccessFuncIndex(Addr);
  322. if (Idx < 0)
  323. return false;
  324. const size_t ByteSize = 1 << Idx;
  325. const size_t BitSize = ByteSize * 8;
  326. Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
  327. Type *PtrTy = Ty->getPointerTo();
  328. Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
  329. createOrdering(&IRB, LI->getOrdering())};
  330. CallInst *C = CallInst::Create(TsanAtomicLoad[Idx],
  331. ArrayRef<Value*>(Args));
  332. ReplaceInstWithInst(I, C);
  333. } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
  334. Value *Addr = SI->getPointerOperand();
  335. int Idx = getMemoryAccessFuncIndex(Addr);
  336. if (Idx < 0)
  337. return false;
  338. const size_t ByteSize = 1 << Idx;
  339. const size_t BitSize = ByteSize * 8;
  340. Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
  341. Type *PtrTy = Ty->getPointerTo();
  342. Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
  343. IRB.CreateIntCast(SI->getValueOperand(), Ty, false),
  344. createOrdering(&IRB, SI->getOrdering())};
  345. CallInst *C = CallInst::Create(TsanAtomicStore[Idx],
  346. ArrayRef<Value*>(Args));
  347. ReplaceInstWithInst(I, C);
  348. } else if (isa<AtomicRMWInst>(I)) {
  349. // FIXME: Not yet supported.
  350. } else if (isa<AtomicCmpXchgInst>(I)) {
  351. // FIXME: Not yet supported.
  352. } else if (isa<FenceInst>(I)) {
  353. // FIXME: Not yet supported.
  354. }
  355. return true;
  356. }
  357. int ThreadSanitizer::getMemoryAccessFuncIndex(Value *Addr) {
  358. Type *OrigPtrTy = Addr->getType();
  359. Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
  360. assert(OrigTy->isSized());
  361. uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
  362. if (TypeSize != 8 && TypeSize != 16 &&
  363. TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
  364. NumAccessesWithBadSize++;
  365. // Ignore all unusual sizes.
  366. return -1;
  367. }
  368. size_t Idx = CountTrailingZeros_32(TypeSize / 8);
  369. assert(Idx < kNumberOfAccessSizes);
  370. return Idx;
  371. }