ThreadSanitizer.cpp 28 KB

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