ThreadSanitizer.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. #include "llvm/Transforms/Instrumentation.h"
  22. #include "llvm/ADT/SmallSet.h"
  23. #include "llvm/ADT/SmallString.h"
  24. #include "llvm/ADT/SmallVector.h"
  25. #include "llvm/ADT/Statistic.h"
  26. #include "llvm/ADT/StringExtras.h"
  27. #include "llvm/Analysis/CaptureTracking.h"
  28. #include "llvm/Analysis/ValueTracking.h"
  29. #include "llvm/IR/DataLayout.h"
  30. #include "llvm/IR/Function.h"
  31. #include "llvm/IR/IRBuilder.h"
  32. #include "llvm/IR/IntrinsicInst.h"
  33. #include "llvm/IR/Intrinsics.h"
  34. #include "llvm/IR/LLVMContext.h"
  35. #include "llvm/IR/Metadata.h"
  36. #include "llvm/IR/Module.h"
  37. #include "llvm/IR/Type.h"
  38. #include "llvm/Support/CommandLine.h"
  39. #include "llvm/Support/Debug.h"
  40. #include "llvm/Support/MathExtras.h"
  41. #include "llvm/Support/raw_ostream.h"
  42. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  43. #include "llvm/Transforms/Utils/ModuleUtils.h"
  44. using namespace llvm;
  45. #define DEBUG_TYPE "tsan"
  46. static cl::opt<bool> ClInstrumentMemoryAccesses(
  47. "tsan-instrument-memory-accesses", cl::init(true),
  48. cl::desc("Instrument memory accesses"), cl::Hidden);
  49. static cl::opt<bool> ClInstrumentFuncEntryExit(
  50. "tsan-instrument-func-entry-exit", cl::init(true),
  51. cl::desc("Instrument function entry and exit"), cl::Hidden);
  52. static cl::opt<bool> ClInstrumentAtomics(
  53. "tsan-instrument-atomics", cl::init(true),
  54. cl::desc("Instrument atomics"), cl::Hidden);
  55. static cl::opt<bool> ClInstrumentMemIntrinsics(
  56. "tsan-instrument-memintrinsics", cl::init(true),
  57. cl::desc("Instrument memintrinsics (memset/memcpy/memmove)"), cl::Hidden);
  58. STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
  59. STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
  60. STATISTIC(NumOmittedReadsBeforeWrite,
  61. "Number of reads ignored due to following writes");
  62. STATISTIC(NumAccessesWithBadSize, "Number of accesses with bad size");
  63. STATISTIC(NumInstrumentedVtableWrites, "Number of vtable ptr writes");
  64. STATISTIC(NumInstrumentedVtableReads, "Number of vtable ptr reads");
  65. STATISTIC(NumOmittedReadsFromConstantGlobals,
  66. "Number of reads from constant globals");
  67. STATISTIC(NumOmittedReadsFromVtable, "Number of vtable reads");
  68. STATISTIC(NumOmittedNonCaptured, "Number of accesses ignored due to capturing");
  69. static const char *const kTsanModuleCtorName = "tsan.module_ctor";
  70. static const char *const kTsanInitName = "__tsan_init";
  71. namespace {
  72. /// ThreadSanitizer: instrument the code in module to find races.
  73. struct ThreadSanitizer : public FunctionPass {
  74. ThreadSanitizer() : FunctionPass(ID) {}
  75. const char *getPassName() const override;
  76. bool runOnFunction(Function &F) override;
  77. bool doInitialization(Module &M) override;
  78. static char ID; // Pass identification, replacement for typeid.
  79. private:
  80. void initializeCallbacks(Module &M);
  81. bool instrumentLoadOrStore(Instruction *I, const DataLayout &DL);
  82. bool instrumentAtomic(Instruction *I, const DataLayout &DL);
  83. bool instrumentMemIntrinsic(Instruction *I);
  84. void chooseInstructionsToInstrument(SmallVectorImpl<Instruction *> &Local,
  85. SmallVectorImpl<Instruction *> &All,
  86. const DataLayout &DL);
  87. bool addrPointsToConstantData(Value *Addr);
  88. int getMemoryAccessFuncIndex(Value *Addr, const DataLayout &DL);
  89. Type *IntptrTy;
  90. IntegerType *OrdTy;
  91. // Callbacks to run-time library are computed in doInitialization.
  92. Function *TsanFuncEntry;
  93. Function *TsanFuncExit;
  94. // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
  95. static const size_t kNumberOfAccessSizes = 5;
  96. Function *TsanRead[kNumberOfAccessSizes];
  97. Function *TsanWrite[kNumberOfAccessSizes];
  98. Function *TsanUnalignedRead[kNumberOfAccessSizes];
  99. Function *TsanUnalignedWrite[kNumberOfAccessSizes];
  100. Function *TsanAtomicLoad[kNumberOfAccessSizes];
  101. Function *TsanAtomicStore[kNumberOfAccessSizes];
  102. Function *TsanAtomicRMW[AtomicRMWInst::LAST_BINOP + 1][kNumberOfAccessSizes];
  103. Function *TsanAtomicCAS[kNumberOfAccessSizes];
  104. Function *TsanAtomicThreadFence;
  105. Function *TsanAtomicSignalFence;
  106. Function *TsanVptrUpdate;
  107. Function *TsanVptrLoad;
  108. Function *MemmoveFn, *MemcpyFn, *MemsetFn;
  109. Function *TsanCtorFunction;
  110. };
  111. } // namespace
  112. char ThreadSanitizer::ID = 0;
  113. INITIALIZE_PASS(ThreadSanitizer, "tsan",
  114. "ThreadSanitizer: detects data races.",
  115. false, false)
  116. const char *ThreadSanitizer::getPassName() const {
  117. return "ThreadSanitizer";
  118. }
  119. FunctionPass *llvm::createThreadSanitizerPass() {
  120. return new ThreadSanitizer();
  121. }
  122. void ThreadSanitizer::initializeCallbacks(Module &M) {
  123. IRBuilder<> IRB(M.getContext());
  124. // Initialize the callbacks.
  125. TsanFuncEntry = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  126. "__tsan_func_entry", IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
  127. TsanFuncExit = checkSanitizerInterfaceFunction(
  128. M.getOrInsertFunction("__tsan_func_exit", IRB.getVoidTy(), nullptr));
  129. OrdTy = IRB.getInt32Ty();
  130. for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
  131. const size_t ByteSize = 1 << i;
  132. const size_t BitSize = ByteSize * 8;
  133. SmallString<32> ReadName("__tsan_read" + itostr(ByteSize));
  134. TsanRead[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  135. ReadName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
  136. SmallString<32> WriteName("__tsan_write" + itostr(ByteSize));
  137. TsanWrite[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  138. WriteName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
  139. SmallString<64> UnalignedReadName("__tsan_unaligned_read" +
  140. itostr(ByteSize));
  141. TsanUnalignedRead[i] =
  142. checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  143. UnalignedReadName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
  144. SmallString<64> UnalignedWriteName("__tsan_unaligned_write" +
  145. itostr(ByteSize));
  146. TsanUnalignedWrite[i] =
  147. checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  148. UnalignedWriteName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
  149. Type *Ty = Type::getIntNTy(M.getContext(), BitSize);
  150. Type *PtrTy = Ty->getPointerTo();
  151. SmallString<32> AtomicLoadName("__tsan_atomic" + itostr(BitSize) +
  152. "_load");
  153. TsanAtomicLoad[i] = checkSanitizerInterfaceFunction(
  154. M.getOrInsertFunction(AtomicLoadName, Ty, PtrTy, OrdTy, nullptr));
  155. SmallString<32> AtomicStoreName("__tsan_atomic" + itostr(BitSize) +
  156. "_store");
  157. TsanAtomicStore[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  158. AtomicStoreName, IRB.getVoidTy(), PtrTy, Ty, OrdTy, nullptr));
  159. for (int op = AtomicRMWInst::FIRST_BINOP;
  160. op <= AtomicRMWInst::LAST_BINOP; ++op) {
  161. TsanAtomicRMW[op][i] = nullptr;
  162. const char *NamePart = nullptr;
  163. if (op == AtomicRMWInst::Xchg)
  164. NamePart = "_exchange";
  165. else if (op == AtomicRMWInst::Add)
  166. NamePart = "_fetch_add";
  167. else if (op == AtomicRMWInst::Sub)
  168. NamePart = "_fetch_sub";
  169. else if (op == AtomicRMWInst::And)
  170. NamePart = "_fetch_and";
  171. else if (op == AtomicRMWInst::Or)
  172. NamePart = "_fetch_or";
  173. else if (op == AtomicRMWInst::Xor)
  174. NamePart = "_fetch_xor";
  175. else if (op == AtomicRMWInst::Nand)
  176. NamePart = "_fetch_nand";
  177. else
  178. continue;
  179. SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart);
  180. TsanAtomicRMW[op][i] = checkSanitizerInterfaceFunction(
  181. M.getOrInsertFunction(RMWName, Ty, PtrTy, Ty, OrdTy, nullptr));
  182. }
  183. SmallString<32> AtomicCASName("__tsan_atomic" + itostr(BitSize) +
  184. "_compare_exchange_val");
  185. TsanAtomicCAS[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  186. AtomicCASName, Ty, PtrTy, Ty, Ty, OrdTy, OrdTy, nullptr));
  187. }
  188. TsanVptrUpdate = checkSanitizerInterfaceFunction(
  189. M.getOrInsertFunction("__tsan_vptr_update", IRB.getVoidTy(),
  190. IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), nullptr));
  191. TsanVptrLoad = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  192. "__tsan_vptr_read", IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
  193. TsanAtomicThreadFence = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  194. "__tsan_atomic_thread_fence", IRB.getVoidTy(), OrdTy, nullptr));
  195. TsanAtomicSignalFence = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
  196. "__tsan_atomic_signal_fence", IRB.getVoidTy(), OrdTy, nullptr));
  197. MemmoveFn = checkSanitizerInterfaceFunction(
  198. M.getOrInsertFunction("memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
  199. IRB.getInt8PtrTy(), IntptrTy, nullptr));
  200. MemcpyFn = checkSanitizerInterfaceFunction(
  201. M.getOrInsertFunction("memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
  202. IRB.getInt8PtrTy(), IntptrTy, nullptr));
  203. MemsetFn = checkSanitizerInterfaceFunction(
  204. M.getOrInsertFunction("memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
  205. IRB.getInt32Ty(), IntptrTy, nullptr));
  206. }
  207. bool ThreadSanitizer::doInitialization(Module &M) {
  208. const DataLayout &DL = M.getDataLayout();
  209. IntptrTy = DL.getIntPtrType(M.getContext());
  210. std::tie(TsanCtorFunction, std::ignore) = createSanitizerCtorAndInitFunctions(
  211. M, kTsanModuleCtorName, kTsanInitName, /*InitArgTypes=*/{},
  212. /*InitArgs=*/{});
  213. appendToGlobalCtors(M, TsanCtorFunction, 0);
  214. return true;
  215. }
  216. static bool isVtableAccess(Instruction *I) {
  217. if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa))
  218. return Tag->isTBAAVtableAccess();
  219. return false;
  220. }
  221. bool ThreadSanitizer::addrPointsToConstantData(Value *Addr) {
  222. // If this is a GEP, just analyze its pointer operand.
  223. if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr))
  224. Addr = GEP->getPointerOperand();
  225. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
  226. if (GV->isConstant()) {
  227. // Reads from constant globals can not race with any writes.
  228. NumOmittedReadsFromConstantGlobals++;
  229. return true;
  230. }
  231. } else if (LoadInst *L = dyn_cast<LoadInst>(Addr)) {
  232. if (isVtableAccess(L)) {
  233. // Reads from a vtable pointer can not race with any writes.
  234. NumOmittedReadsFromVtable++;
  235. return true;
  236. }
  237. }
  238. return false;
  239. }
  240. // Instrumenting some of the accesses may be proven redundant.
  241. // Currently handled:
  242. // - read-before-write (within same BB, no calls between)
  243. // - not captured variables
  244. //
  245. // We do not handle some of the patterns that should not survive
  246. // after the classic compiler optimizations.
  247. // E.g. two reads from the same temp should be eliminated by CSE,
  248. // two writes should be eliminated by DSE, etc.
  249. //
  250. // 'Local' is a vector of insns within the same BB (no calls between).
  251. // 'All' is a vector of insns that will be instrumented.
  252. void ThreadSanitizer::chooseInstructionsToInstrument(
  253. SmallVectorImpl<Instruction *> &Local, SmallVectorImpl<Instruction *> &All,
  254. const DataLayout &DL) {
  255. SmallSet<Value*, 8> WriteTargets;
  256. // Iterate from the end.
  257. for (SmallVectorImpl<Instruction*>::reverse_iterator It = Local.rbegin(),
  258. E = Local.rend(); It != E; ++It) {
  259. Instruction *I = *It;
  260. if (StoreInst *Store = dyn_cast<StoreInst>(I)) {
  261. WriteTargets.insert(Store->getPointerOperand());
  262. } else {
  263. LoadInst *Load = cast<LoadInst>(I);
  264. Value *Addr = Load->getPointerOperand();
  265. if (WriteTargets.count(Addr)) {
  266. // We will write to this temp, so no reason to analyze the read.
  267. NumOmittedReadsBeforeWrite++;
  268. continue;
  269. }
  270. if (addrPointsToConstantData(Addr)) {
  271. // Addr points to some constant data -- it can not race with any writes.
  272. continue;
  273. }
  274. }
  275. Value *Addr = isa<StoreInst>(*I)
  276. ? cast<StoreInst>(I)->getPointerOperand()
  277. : cast<LoadInst>(I)->getPointerOperand();
  278. if (isa<AllocaInst>(GetUnderlyingObject(Addr, DL)) &&
  279. !PointerMayBeCaptured(Addr, true, true)) {
  280. // The variable is addressable but not captured, so it cannot be
  281. // referenced from a different thread and participate in a data race
  282. // (see llvm/Analysis/CaptureTracking.h for details).
  283. NumOmittedNonCaptured++;
  284. continue;
  285. }
  286. All.push_back(I);
  287. }
  288. Local.clear();
  289. }
  290. static bool isAtomic(Instruction *I) {
  291. if (LoadInst *LI = dyn_cast<LoadInst>(I))
  292. return LI->isAtomic() && LI->getSynchScope() == CrossThread;
  293. if (StoreInst *SI = dyn_cast<StoreInst>(I))
  294. return SI->isAtomic() && SI->getSynchScope() == CrossThread;
  295. if (isa<AtomicRMWInst>(I))
  296. return true;
  297. if (isa<AtomicCmpXchgInst>(I))
  298. return true;
  299. if (isa<FenceInst>(I))
  300. return true;
  301. return false;
  302. }
  303. bool ThreadSanitizer::runOnFunction(Function &F) {
  304. // This is required to prevent instrumenting call to __tsan_init from within
  305. // the module constructor.
  306. if (&F == TsanCtorFunction)
  307. return false;
  308. initializeCallbacks(*F.getParent());
  309. SmallVector<Instruction*, 8> RetVec;
  310. SmallVector<Instruction*, 8> AllLoadsAndStores;
  311. SmallVector<Instruction*, 8> LocalLoadsAndStores;
  312. SmallVector<Instruction*, 8> AtomicAccesses;
  313. SmallVector<Instruction*, 8> MemIntrinCalls;
  314. bool Res = false;
  315. bool HasCalls = false;
  316. bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeThread);
  317. const DataLayout &DL = F.getParent()->getDataLayout();
  318. // Traverse all instructions, collect loads/stores/returns, check for calls.
  319. for (auto &BB : F) {
  320. for (auto &Inst : BB) {
  321. if (isAtomic(&Inst))
  322. AtomicAccesses.push_back(&Inst);
  323. else if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
  324. LocalLoadsAndStores.push_back(&Inst);
  325. else if (isa<ReturnInst>(Inst))
  326. RetVec.push_back(&Inst);
  327. else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
  328. if (isa<MemIntrinsic>(Inst))
  329. MemIntrinCalls.push_back(&Inst);
  330. HasCalls = true;
  331. chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores,
  332. DL);
  333. }
  334. }
  335. chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores, DL);
  336. }
  337. // We have collected all loads and stores.
  338. // FIXME: many of these accesses do not need to be checked for races
  339. // (e.g. variables that do not escape, etc).
  340. // Instrument memory accesses only if we want to report bugs in the function.
  341. if (ClInstrumentMemoryAccesses && SanitizeFunction)
  342. for (auto Inst : AllLoadsAndStores) {
  343. Res |= instrumentLoadOrStore(Inst, DL);
  344. }
  345. // Instrument atomic memory accesses in any case (they can be used to
  346. // implement synchronization).
  347. if (ClInstrumentAtomics)
  348. for (auto Inst : AtomicAccesses) {
  349. Res |= instrumentAtomic(Inst, DL);
  350. }
  351. if (ClInstrumentMemIntrinsics && SanitizeFunction)
  352. for (auto Inst : MemIntrinCalls) {
  353. Res |= instrumentMemIntrinsic(Inst);
  354. }
  355. // Instrument function entry/exit points if there were instrumented accesses.
  356. if ((Res || HasCalls) && ClInstrumentFuncEntryExit) {
  357. IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
  358. Value *ReturnAddress = IRB.CreateCall(
  359. Intrinsic::getDeclaration(F.getParent(), Intrinsic::returnaddress),
  360. IRB.getInt32(0));
  361. IRB.CreateCall(TsanFuncEntry, ReturnAddress);
  362. for (auto RetInst : RetVec) {
  363. IRBuilder<> IRBRet(RetInst);
  364. IRBRet.CreateCall(TsanFuncExit, {});
  365. }
  366. Res = true;
  367. }
  368. return Res;
  369. }
  370. bool ThreadSanitizer::instrumentLoadOrStore(Instruction *I,
  371. const DataLayout &DL) {
  372. IRBuilder<> IRB(I);
  373. bool IsWrite = isa<StoreInst>(*I);
  374. Value *Addr = IsWrite
  375. ? cast<StoreInst>(I)->getPointerOperand()
  376. : cast<LoadInst>(I)->getPointerOperand();
  377. int Idx = getMemoryAccessFuncIndex(Addr, DL);
  378. if (Idx < 0)
  379. return false;
  380. if (IsWrite && isVtableAccess(I)) {
  381. DEBUG(dbgs() << " VPTR : " << *I << "\n");
  382. Value *StoredValue = cast<StoreInst>(I)->getValueOperand();
  383. // StoredValue may be a vector type if we are storing several vptrs at once.
  384. // In this case, just take the first element of the vector since this is
  385. // enough to find vptr races.
  386. if (isa<VectorType>(StoredValue->getType()))
  387. StoredValue = IRB.CreateExtractElement(
  388. StoredValue, ConstantInt::get(IRB.getInt32Ty(), 0));
  389. if (StoredValue->getType()->isIntegerTy())
  390. StoredValue = IRB.CreateIntToPtr(StoredValue, IRB.getInt8PtrTy());
  391. // Call TsanVptrUpdate.
  392. IRB.CreateCall(TsanVptrUpdate,
  393. {IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
  394. IRB.CreatePointerCast(StoredValue, IRB.getInt8PtrTy())});
  395. NumInstrumentedVtableWrites++;
  396. return true;
  397. }
  398. if (!IsWrite && isVtableAccess(I)) {
  399. IRB.CreateCall(TsanVptrLoad,
  400. IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
  401. NumInstrumentedVtableReads++;
  402. return true;
  403. }
  404. const unsigned Alignment = IsWrite
  405. ? cast<StoreInst>(I)->getAlignment()
  406. : cast<LoadInst>(I)->getAlignment();
  407. Type *OrigTy = cast<PointerType>(Addr->getType())->getElementType();
  408. const uint32_t TypeSize = DL.getTypeStoreSizeInBits(OrigTy);
  409. Value *OnAccessFunc = nullptr;
  410. if (Alignment == 0 || Alignment >= 8 || (Alignment % (TypeSize / 8)) == 0)
  411. OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
  412. else
  413. OnAccessFunc = IsWrite ? TsanUnalignedWrite[Idx] : TsanUnalignedRead[Idx];
  414. IRB.CreateCall(OnAccessFunc, IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
  415. if (IsWrite) NumInstrumentedWrites++;
  416. else NumInstrumentedReads++;
  417. return true;
  418. }
  419. static ConstantInt *createOrdering(IRBuilder<> *IRB, AtomicOrdering ord) {
  420. uint32_t v = 0;
  421. switch (ord) {
  422. case NotAtomic: llvm_unreachable("unexpected atomic ordering!");
  423. case Unordered: // Fall-through.
  424. case Monotonic: v = 0; break;
  425. // case Consume: v = 1; break; // Not specified yet.
  426. case Acquire: v = 2; break;
  427. case Release: v = 3; break;
  428. case AcquireRelease: v = 4; break;
  429. case SequentiallyConsistent: v = 5; break;
  430. }
  431. return IRB->getInt32(v);
  432. }
  433. // If a memset intrinsic gets inlined by the code gen, we will miss races on it.
  434. // So, we either need to ensure the intrinsic is not inlined, or instrument it.
  435. // We do not instrument memset/memmove/memcpy intrinsics (too complicated),
  436. // instead we simply replace them with regular function calls, which are then
  437. // intercepted by the run-time.
  438. // Since tsan is running after everyone else, the calls should not be
  439. // replaced back with intrinsics. If that becomes wrong at some point,
  440. // we will need to call e.g. __tsan_memset to avoid the intrinsics.
  441. bool ThreadSanitizer::instrumentMemIntrinsic(Instruction *I) {
  442. IRBuilder<> IRB(I);
  443. if (MemSetInst *M = dyn_cast<MemSetInst>(I)) {
  444. IRB.CreateCall(
  445. MemsetFn,
  446. {IRB.CreatePointerCast(M->getArgOperand(0), IRB.getInt8PtrTy()),
  447. IRB.CreateIntCast(M->getArgOperand(1), IRB.getInt32Ty(), false),
  448. IRB.CreateIntCast(M->getArgOperand(2), IntptrTy, false)});
  449. I->eraseFromParent();
  450. } else if (MemTransferInst *M = dyn_cast<MemTransferInst>(I)) {
  451. IRB.CreateCall(
  452. isa<MemCpyInst>(M) ? MemcpyFn : MemmoveFn,
  453. {IRB.CreatePointerCast(M->getArgOperand(0), IRB.getInt8PtrTy()),
  454. IRB.CreatePointerCast(M->getArgOperand(1), IRB.getInt8PtrTy()),
  455. IRB.CreateIntCast(M->getArgOperand(2), IntptrTy, false)});
  456. I->eraseFromParent();
  457. }
  458. return false;
  459. }
  460. // Both llvm and ThreadSanitizer atomic operations are based on C++11/C1x
  461. // standards. For background see C++11 standard. A slightly older, publicly
  462. // available draft of the standard (not entirely up-to-date, but close enough
  463. // for casual browsing) is available here:
  464. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
  465. // The following page contains more background information:
  466. // http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/
  467. bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) {
  468. IRBuilder<> IRB(I);
  469. if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
  470. Value *Addr = LI->getPointerOperand();
  471. int Idx = getMemoryAccessFuncIndex(Addr, DL);
  472. if (Idx < 0)
  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. createOrdering(&IRB, LI->getOrdering())};
  480. CallInst *C = CallInst::Create(TsanAtomicLoad[Idx], Args);
  481. ReplaceInstWithInst(I, C);
  482. } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
  483. Value *Addr = SI->getPointerOperand();
  484. int Idx = getMemoryAccessFuncIndex(Addr, DL);
  485. if (Idx < 0)
  486. return false;
  487. const size_t ByteSize = 1 << Idx;
  488. const size_t BitSize = ByteSize * 8;
  489. Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
  490. Type *PtrTy = Ty->getPointerTo();
  491. Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
  492. IRB.CreateIntCast(SI->getValueOperand(), Ty, false),
  493. createOrdering(&IRB, SI->getOrdering())};
  494. CallInst *C = CallInst::Create(TsanAtomicStore[Idx], Args);
  495. ReplaceInstWithInst(I, C);
  496. } else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I)) {
  497. Value *Addr = RMWI->getPointerOperand();
  498. int Idx = getMemoryAccessFuncIndex(Addr, DL);
  499. if (Idx < 0)
  500. return false;
  501. Function *F = TsanAtomicRMW[RMWI->getOperation()][Idx];
  502. if (!F)
  503. return false;
  504. const size_t ByteSize = 1 << Idx;
  505. const size_t BitSize = ByteSize * 8;
  506. Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
  507. Type *PtrTy = Ty->getPointerTo();
  508. Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
  509. IRB.CreateIntCast(RMWI->getValOperand(), Ty, false),
  510. createOrdering(&IRB, RMWI->getOrdering())};
  511. CallInst *C = CallInst::Create(F, Args);
  512. ReplaceInstWithInst(I, C);
  513. } else if (AtomicCmpXchgInst *CASI = dyn_cast<AtomicCmpXchgInst>(I)) {
  514. Value *Addr = CASI->getPointerOperand();
  515. int Idx = getMemoryAccessFuncIndex(Addr, DL);
  516. if (Idx < 0)
  517. return false;
  518. const size_t ByteSize = 1 << Idx;
  519. const size_t BitSize = ByteSize * 8;
  520. Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
  521. Type *PtrTy = Ty->getPointerTo();
  522. Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
  523. IRB.CreateIntCast(CASI->getCompareOperand(), Ty, false),
  524. IRB.CreateIntCast(CASI->getNewValOperand(), Ty, false),
  525. createOrdering(&IRB, CASI->getSuccessOrdering()),
  526. createOrdering(&IRB, CASI->getFailureOrdering())};
  527. CallInst *C = IRB.CreateCall(TsanAtomicCAS[Idx], Args);
  528. Value *Success = IRB.CreateICmpEQ(C, CASI->getCompareOperand());
  529. Value *Res = IRB.CreateInsertValue(UndefValue::get(CASI->getType()), C, 0);
  530. Res = IRB.CreateInsertValue(Res, Success, 1);
  531. I->replaceAllUsesWith(Res);
  532. I->eraseFromParent();
  533. } else if (FenceInst *FI = dyn_cast<FenceInst>(I)) {
  534. Value *Args[] = {createOrdering(&IRB, FI->getOrdering())};
  535. Function *F = FI->getSynchScope() == SingleThread ?
  536. TsanAtomicSignalFence : TsanAtomicThreadFence;
  537. CallInst *C = CallInst::Create(F, Args);
  538. ReplaceInstWithInst(I, C);
  539. }
  540. return true;
  541. }
  542. int ThreadSanitizer::getMemoryAccessFuncIndex(Value *Addr,
  543. const DataLayout &DL) {
  544. Type *OrigPtrTy = Addr->getType();
  545. Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
  546. assert(OrigTy->isSized());
  547. uint32_t TypeSize = DL.getTypeStoreSizeInBits(OrigTy);
  548. if (TypeSize != 8 && TypeSize != 16 &&
  549. TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
  550. NumAccessesWithBadSize++;
  551. // Ignore all unusual sizes.
  552. return -1;
  553. }
  554. size_t Idx = countTrailingZeros(TypeSize / 8);
  555. assert(Idx < kNumberOfAccessSizes);
  556. return Idx;
  557. }