ThreadSanitizer.cpp 29 KB

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