ThreadSanitizer.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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/DenseMap.h"
  22. #include "llvm/ADT/Optional.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/Instructions.h"
  34. #include "llvm/IR/IntrinsicInst.h"
  35. #include "llvm/IR/Intrinsics.h"
  36. #include "llvm/IR/LLVMContext.h"
  37. #include "llvm/IR/Metadata.h"
  38. #include "llvm/IR/Module.h"
  39. #include "llvm/IR/Type.h"
  40. #include "llvm/InitializePasses.h"
  41. #include "llvm/ProfileData/InstrProf.h"
  42. #include "llvm/Support/CommandLine.h"
  43. #include "llvm/Support/Debug.h"
  44. #include "llvm/Support/MathExtras.h"
  45. #include "llvm/Support/raw_ostream.h"
  46. #include "llvm/Transforms/Instrumentation.h"
  47. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  48. #include "llvm/Transforms/Utils/EscapeEnumerator.h"
  49. #include "llvm/Transforms/Utils/Local.h"
  50. #include "llvm/Transforms/Utils/ModuleUtils.h"
  51. using namespace llvm;
  52. #define DEBUG_TYPE "tsan"
  53. static cl::opt<bool> ClInstrumentMemoryAccesses(
  54. "tsan-instrument-memory-accesses", cl::init(true),
  55. cl::desc("Instrument memory accesses"), cl::Hidden);
  56. static cl::opt<bool>
  57. ClInstrumentFuncEntryExit("tsan-instrument-func-entry-exit", cl::init(true),
  58. cl::desc("Instrument function entry and exit"),
  59. cl::Hidden);
  60. static cl::opt<bool> ClHandleCxxExceptions(
  61. "tsan-handle-cxx-exceptions", cl::init(true),
  62. cl::desc("Handle C++ exceptions (insert cleanup blocks for unwinding)"),
  63. cl::Hidden);
  64. static cl::opt<bool> ClInstrumentAtomics("tsan-instrument-atomics",
  65. cl::init(true),
  66. cl::desc("Instrument atomics"),
  67. cl::Hidden);
  68. static cl::opt<bool> ClInstrumentMemIntrinsics(
  69. "tsan-instrument-memintrinsics", cl::init(true),
  70. cl::desc("Instrument memintrinsics (memset/memcpy/memmove)"), cl::Hidden);
  71. static cl::opt<bool> ClDistinguishVolatile(
  72. "tsan-distinguish-volatile", cl::init(false),
  73. cl::desc("Emit special instrumentation for accesses to volatiles"),
  74. cl::Hidden);
  75. static cl::opt<bool> ClInstrumentReadBeforeWrite(
  76. "tsan-instrument-read-before-write", cl::init(false),
  77. cl::desc("Do not eliminate read instrumentation for read-before-writes"),
  78. cl::Hidden);
  79. static cl::opt<bool> ClCompoundReadBeforeWrite(
  80. "tsan-compound-read-before-write", cl::init(false),
  81. cl::desc("Emit special compound instrumentation for reads-before-writes"),
  82. cl::Hidden);
  83. STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
  84. STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
  85. STATISTIC(NumOmittedReadsBeforeWrite,
  86. "Number of reads ignored due to following writes");
  87. STATISTIC(NumAccessesWithBadSize, "Number of accesses with bad size");
  88. STATISTIC(NumInstrumentedVtableWrites, "Number of vtable ptr writes");
  89. STATISTIC(NumInstrumentedVtableReads, "Number of vtable ptr reads");
  90. STATISTIC(NumOmittedReadsFromConstantGlobals,
  91. "Number of reads from constant globals");
  92. STATISTIC(NumOmittedReadsFromVtable, "Number of vtable reads");
  93. STATISTIC(NumOmittedNonCaptured, "Number of accesses ignored due to capturing");
  94. const char kTsanModuleCtorName[] = "tsan.module_ctor";
  95. const char kTsanInitName[] = "__tsan_init";
  96. namespace {
  97. /// ThreadSanitizer: instrument the code in module to find races.
  98. ///
  99. /// Instantiating ThreadSanitizer inserts the tsan runtime library API function
  100. /// declarations into the module if they don't exist already. Instantiating
  101. /// ensures the __tsan_init function is in the list of global constructors for
  102. /// the module.
  103. struct ThreadSanitizer {
  104. ThreadSanitizer() {
  105. // Check options and warn user.
  106. if (ClInstrumentReadBeforeWrite && ClCompoundReadBeforeWrite) {
  107. errs()
  108. << "warning: Option -tsan-compound-read-before-write has no effect "
  109. "when -tsan-instrument-read-before-write is set.\n";
  110. }
  111. }
  112. bool sanitizeFunction(Function &F, const TargetLibraryInfo &TLI);
  113. private:
  114. // Internal Instruction wrapper that contains more information about the
  115. // Instruction from prior analysis.
  116. struct InstructionInfo {
  117. // Instrumentation emitted for this instruction is for a compounded set of
  118. // read and write operations in the same basic block.
  119. static constexpr unsigned kCompoundRW = (1U << 0);
  120. explicit InstructionInfo(Instruction *Inst) : Inst(Inst) {}
  121. Instruction *Inst;
  122. unsigned Flags = 0;
  123. };
  124. void initialize(Module &M);
  125. bool instrumentLoadOrStore(const InstructionInfo &II, const DataLayout &DL);
  126. bool instrumentAtomic(Instruction *I, const DataLayout &DL);
  127. bool instrumentMemIntrinsic(Instruction *I);
  128. void chooseInstructionsToInstrument(SmallVectorImpl<Instruction *> &Local,
  129. SmallVectorImpl<InstructionInfo> &All,
  130. const DataLayout &DL);
  131. bool addrPointsToConstantData(Value *Addr);
  132. int getMemoryAccessFuncIndex(Type *OrigTy, Value *Addr, const DataLayout &DL);
  133. void InsertRuntimeIgnores(Function &F);
  134. Type *IntptrTy;
  135. FunctionCallee TsanFuncEntry;
  136. FunctionCallee TsanFuncExit;
  137. FunctionCallee TsanIgnoreBegin;
  138. FunctionCallee TsanIgnoreEnd;
  139. // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
  140. static const size_t kNumberOfAccessSizes = 5;
  141. FunctionCallee TsanRead[kNumberOfAccessSizes];
  142. FunctionCallee TsanWrite[kNumberOfAccessSizes];
  143. FunctionCallee TsanUnalignedRead[kNumberOfAccessSizes];
  144. FunctionCallee TsanUnalignedWrite[kNumberOfAccessSizes];
  145. FunctionCallee TsanVolatileRead[kNumberOfAccessSizes];
  146. FunctionCallee TsanVolatileWrite[kNumberOfAccessSizes];
  147. FunctionCallee TsanUnalignedVolatileRead[kNumberOfAccessSizes];
  148. FunctionCallee TsanUnalignedVolatileWrite[kNumberOfAccessSizes];
  149. FunctionCallee TsanCompoundRW[kNumberOfAccessSizes];
  150. FunctionCallee TsanUnalignedCompoundRW[kNumberOfAccessSizes];
  151. FunctionCallee TsanAtomicLoad[kNumberOfAccessSizes];
  152. FunctionCallee TsanAtomicStore[kNumberOfAccessSizes];
  153. FunctionCallee TsanAtomicRMW[AtomicRMWInst::LAST_BINOP + 1]
  154. [kNumberOfAccessSizes];
  155. FunctionCallee TsanAtomicCAS[kNumberOfAccessSizes];
  156. FunctionCallee TsanAtomicThreadFence;
  157. FunctionCallee TsanAtomicSignalFence;
  158. FunctionCallee TsanVptrUpdate;
  159. FunctionCallee TsanVptrLoad;
  160. FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
  161. };
  162. struct ThreadSanitizerLegacyPass : FunctionPass {
  163. ThreadSanitizerLegacyPass() : FunctionPass(ID) {
  164. initializeThreadSanitizerLegacyPassPass(*PassRegistry::getPassRegistry());
  165. }
  166. StringRef getPassName() const override;
  167. void getAnalysisUsage(AnalysisUsage &AU) const override;
  168. bool runOnFunction(Function &F) override;
  169. bool doInitialization(Module &M) override;
  170. static char ID; // Pass identification, replacement for typeid.
  171. private:
  172. Optional<ThreadSanitizer> TSan;
  173. };
  174. void insertModuleCtor(Module &M) {
  175. getOrCreateSanitizerCtorAndInitFunctions(
  176. M, kTsanModuleCtorName, kTsanInitName, /*InitArgTypes=*/{},
  177. /*InitArgs=*/{},
  178. // This callback is invoked when the functions are created the first
  179. // time. Hook them into the global ctors list in that case:
  180. [&](Function *Ctor, FunctionCallee) { appendToGlobalCtors(M, Ctor, 0); });
  181. }
  182. } // namespace
  183. PreservedAnalyses ThreadSanitizerPass::run(Function &F,
  184. FunctionAnalysisManager &FAM) {
  185. ThreadSanitizer TSan;
  186. if (TSan.sanitizeFunction(F, FAM.getResult<TargetLibraryAnalysis>(F)))
  187. return PreservedAnalyses::none();
  188. return PreservedAnalyses::all();
  189. }
  190. PreservedAnalyses ModuleThreadSanitizerPass::run(Module &M,
  191. ModuleAnalysisManager &MAM) {
  192. insertModuleCtor(M);
  193. return PreservedAnalyses::none();
  194. }
  195. char ThreadSanitizerLegacyPass::ID = 0;
  196. INITIALIZE_PASS_BEGIN(ThreadSanitizerLegacyPass, "tsan",
  197. "ThreadSanitizer: detects data races.", false, false)
  198. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  199. INITIALIZE_PASS_END(ThreadSanitizerLegacyPass, "tsan",
  200. "ThreadSanitizer: detects data races.", false, false)
  201. StringRef ThreadSanitizerLegacyPass::getPassName() const {
  202. return "ThreadSanitizerLegacyPass";
  203. }
  204. void ThreadSanitizerLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
  205. AU.addRequired<TargetLibraryInfoWrapperPass>();
  206. }
  207. bool ThreadSanitizerLegacyPass::doInitialization(Module &M) {
  208. insertModuleCtor(M);
  209. TSan.emplace();
  210. return true;
  211. }
  212. bool ThreadSanitizerLegacyPass::runOnFunction(Function &F) {
  213. auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  214. TSan->sanitizeFunction(F, TLI);
  215. return true;
  216. }
  217. FunctionPass *llvm::createThreadSanitizerLegacyPassPass() {
  218. return new ThreadSanitizerLegacyPass();
  219. }
  220. void ThreadSanitizer::initialize(Module &M) {
  221. const DataLayout &DL = M.getDataLayout();
  222. IntptrTy = DL.getIntPtrType(M.getContext());
  223. IRBuilder<> IRB(M.getContext());
  224. AttributeList Attr;
  225. Attr = Attr.addFnAttribute(M.getContext(), Attribute::NoUnwind);
  226. // Initialize the callbacks.
  227. TsanFuncEntry = M.getOrInsertFunction("__tsan_func_entry", Attr,
  228. IRB.getVoidTy(), IRB.getInt8PtrTy());
  229. TsanFuncExit =
  230. M.getOrInsertFunction("__tsan_func_exit", Attr, IRB.getVoidTy());
  231. TsanIgnoreBegin = M.getOrInsertFunction("__tsan_ignore_thread_begin", Attr,
  232. IRB.getVoidTy());
  233. TsanIgnoreEnd =
  234. M.getOrInsertFunction("__tsan_ignore_thread_end", Attr, IRB.getVoidTy());
  235. IntegerType *OrdTy = IRB.getInt32Ty();
  236. for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
  237. const unsigned ByteSize = 1U << i;
  238. const unsigned BitSize = ByteSize * 8;
  239. std::string ByteSizeStr = utostr(ByteSize);
  240. std::string BitSizeStr = utostr(BitSize);
  241. SmallString<32> ReadName("__tsan_read" + ByteSizeStr);
  242. TsanRead[i] = M.getOrInsertFunction(ReadName, Attr, IRB.getVoidTy(),
  243. IRB.getInt8PtrTy());
  244. SmallString<32> WriteName("__tsan_write" + ByteSizeStr);
  245. TsanWrite[i] = M.getOrInsertFunction(WriteName, Attr, IRB.getVoidTy(),
  246. IRB.getInt8PtrTy());
  247. SmallString<64> UnalignedReadName("__tsan_unaligned_read" + ByteSizeStr);
  248. TsanUnalignedRead[i] = M.getOrInsertFunction(
  249. UnalignedReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
  250. SmallString<64> UnalignedWriteName("__tsan_unaligned_write" + ByteSizeStr);
  251. TsanUnalignedWrite[i] = M.getOrInsertFunction(
  252. UnalignedWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
  253. SmallString<64> VolatileReadName("__tsan_volatile_read" + ByteSizeStr);
  254. TsanVolatileRead[i] = M.getOrInsertFunction(
  255. VolatileReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
  256. SmallString<64> VolatileWriteName("__tsan_volatile_write" + ByteSizeStr);
  257. TsanVolatileWrite[i] = M.getOrInsertFunction(
  258. VolatileWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
  259. SmallString<64> UnalignedVolatileReadName("__tsan_unaligned_volatile_read" +
  260. ByteSizeStr);
  261. TsanUnalignedVolatileRead[i] = M.getOrInsertFunction(
  262. UnalignedVolatileReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
  263. SmallString<64> UnalignedVolatileWriteName(
  264. "__tsan_unaligned_volatile_write" + ByteSizeStr);
  265. TsanUnalignedVolatileWrite[i] = M.getOrInsertFunction(
  266. UnalignedVolatileWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
  267. SmallString<64> CompoundRWName("__tsan_read_write" + ByteSizeStr);
  268. TsanCompoundRW[i] = M.getOrInsertFunction(
  269. CompoundRWName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
  270. SmallString<64> UnalignedCompoundRWName("__tsan_unaligned_read_write" +
  271. ByteSizeStr);
  272. TsanUnalignedCompoundRW[i] = M.getOrInsertFunction(
  273. UnalignedCompoundRWName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy());
  274. Type *Ty = Type::getIntNTy(M.getContext(), BitSize);
  275. Type *PtrTy = Ty->getPointerTo();
  276. SmallString<32> AtomicLoadName("__tsan_atomic" + BitSizeStr + "_load");
  277. {
  278. AttributeList AL = Attr;
  279. AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt);
  280. TsanAtomicLoad[i] =
  281. M.getOrInsertFunction(AtomicLoadName, AL, Ty, PtrTy, OrdTy);
  282. }
  283. SmallString<32> AtomicStoreName("__tsan_atomic" + BitSizeStr + "_store");
  284. {
  285. AttributeList AL = Attr;
  286. AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt);
  287. AL = AL.addParamAttribute(M.getContext(), 2, Attribute::ZExt);
  288. TsanAtomicStore[i] = M.getOrInsertFunction(
  289. AtomicStoreName, AL, IRB.getVoidTy(), PtrTy, Ty, OrdTy);
  290. }
  291. for (unsigned Op = AtomicRMWInst::FIRST_BINOP;
  292. Op <= AtomicRMWInst::LAST_BINOP; ++Op) {
  293. TsanAtomicRMW[Op][i] = nullptr;
  294. const char *NamePart = nullptr;
  295. if (Op == AtomicRMWInst::Xchg)
  296. NamePart = "_exchange";
  297. else if (Op == AtomicRMWInst::Add)
  298. NamePart = "_fetch_add";
  299. else if (Op == AtomicRMWInst::Sub)
  300. NamePart = "_fetch_sub";
  301. else if (Op == AtomicRMWInst::And)
  302. NamePart = "_fetch_and";
  303. else if (Op == AtomicRMWInst::Or)
  304. NamePart = "_fetch_or";
  305. else if (Op == AtomicRMWInst::Xor)
  306. NamePart = "_fetch_xor";
  307. else if (Op == AtomicRMWInst::Nand)
  308. NamePart = "_fetch_nand";
  309. else
  310. continue;
  311. SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart);
  312. {
  313. AttributeList AL = Attr;
  314. AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt);
  315. AL = AL.addParamAttribute(M.getContext(), 2, Attribute::ZExt);
  316. TsanAtomicRMW[Op][i] =
  317. M.getOrInsertFunction(RMWName, AL, Ty, PtrTy, Ty, OrdTy);
  318. }
  319. }
  320. SmallString<32> AtomicCASName("__tsan_atomic" + BitSizeStr +
  321. "_compare_exchange_val");
  322. {
  323. AttributeList AL = Attr;
  324. AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt);
  325. AL = AL.addParamAttribute(M.getContext(), 2, Attribute::ZExt);
  326. AL = AL.addParamAttribute(M.getContext(), 3, Attribute::ZExt);
  327. AL = AL.addParamAttribute(M.getContext(), 4, Attribute::ZExt);
  328. TsanAtomicCAS[i] = M.getOrInsertFunction(AtomicCASName, AL, Ty, PtrTy, Ty,
  329. Ty, OrdTy, OrdTy);
  330. }
  331. }
  332. TsanVptrUpdate =
  333. M.getOrInsertFunction("__tsan_vptr_update", Attr, IRB.getVoidTy(),
  334. IRB.getInt8PtrTy(), IRB.getInt8PtrTy());
  335. TsanVptrLoad = M.getOrInsertFunction("__tsan_vptr_read", Attr,
  336. IRB.getVoidTy(), IRB.getInt8PtrTy());
  337. {
  338. AttributeList AL = Attr;
  339. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  340. TsanAtomicThreadFence = M.getOrInsertFunction("__tsan_atomic_thread_fence",
  341. AL, IRB.getVoidTy(), OrdTy);
  342. }
  343. {
  344. AttributeList AL = Attr;
  345. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  346. TsanAtomicSignalFence = M.getOrInsertFunction("__tsan_atomic_signal_fence",
  347. AL, IRB.getVoidTy(), OrdTy);
  348. }
  349. MemmoveFn =
  350. M.getOrInsertFunction("memmove", Attr, IRB.getInt8PtrTy(),
  351. IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
  352. MemcpyFn =
  353. M.getOrInsertFunction("memcpy", Attr, IRB.getInt8PtrTy(),
  354. IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
  355. MemsetFn =
  356. M.getOrInsertFunction("memset", Attr, IRB.getInt8PtrTy(),
  357. IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy);
  358. }
  359. static bool isVtableAccess(Instruction *I) {
  360. if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa))
  361. return Tag->isTBAAVtableAccess();
  362. return false;
  363. }
  364. // Do not instrument known races/"benign races" that come from compiler
  365. // instrumentatin. The user has no way of suppressing them.
  366. static bool shouldInstrumentReadWriteFromAddress(const Module *M, Value *Addr) {
  367. // Peel off GEPs and BitCasts.
  368. Addr = Addr->stripInBoundsOffsets();
  369. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
  370. if (GV->hasSection()) {
  371. StringRef SectionName = GV->getSection();
  372. // Check if the global is in the PGO counters section.
  373. auto OF = Triple(M->getTargetTriple()).getObjectFormat();
  374. if (SectionName.endswith(
  375. getInstrProfSectionName(IPSK_cnts, OF, /*AddSegmentInfo=*/false)))
  376. return false;
  377. }
  378. // Check if the global is private gcov data.
  379. if (GV->getName().startswith("__llvm_gcov") ||
  380. GV->getName().startswith("__llvm_gcda"))
  381. return false;
  382. }
  383. // Do not instrument acesses from different address spaces; we cannot deal
  384. // with them.
  385. if (Addr) {
  386. Type *PtrTy = cast<PointerType>(Addr->getType()->getScalarType());
  387. if (PtrTy->getPointerAddressSpace() != 0)
  388. return false;
  389. }
  390. return true;
  391. }
  392. bool ThreadSanitizer::addrPointsToConstantData(Value *Addr) {
  393. // If this is a GEP, just analyze its pointer operand.
  394. if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr))
  395. Addr = GEP->getPointerOperand();
  396. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
  397. if (GV->isConstant()) {
  398. // Reads from constant globals can not race with any writes.
  399. NumOmittedReadsFromConstantGlobals++;
  400. return true;
  401. }
  402. } else if (LoadInst *L = dyn_cast<LoadInst>(Addr)) {
  403. if (isVtableAccess(L)) {
  404. // Reads from a vtable pointer can not race with any writes.
  405. NumOmittedReadsFromVtable++;
  406. return true;
  407. }
  408. }
  409. return false;
  410. }
  411. // Instrumenting some of the accesses may be proven redundant.
  412. // Currently handled:
  413. // - read-before-write (within same BB, no calls between)
  414. // - not captured variables
  415. //
  416. // We do not handle some of the patterns that should not survive
  417. // after the classic compiler optimizations.
  418. // E.g. two reads from the same temp should be eliminated by CSE,
  419. // two writes should be eliminated by DSE, etc.
  420. //
  421. // 'Local' is a vector of insns within the same BB (no calls between).
  422. // 'All' is a vector of insns that will be instrumented.
  423. void ThreadSanitizer::chooseInstructionsToInstrument(
  424. SmallVectorImpl<Instruction *> &Local,
  425. SmallVectorImpl<InstructionInfo> &All, const DataLayout &DL) {
  426. DenseMap<Value *, size_t> WriteTargets; // Map of addresses to index in All
  427. // Iterate from the end.
  428. for (Instruction *I : reverse(Local)) {
  429. const bool IsWrite = isa<StoreInst>(*I);
  430. Value *Addr = IsWrite ? cast<StoreInst>(I)->getPointerOperand()
  431. : cast<LoadInst>(I)->getPointerOperand();
  432. if (!shouldInstrumentReadWriteFromAddress(I->getModule(), Addr))
  433. continue;
  434. if (!IsWrite) {
  435. const auto WriteEntry = WriteTargets.find(Addr);
  436. if (!ClInstrumentReadBeforeWrite && WriteEntry != WriteTargets.end()) {
  437. auto &WI = All[WriteEntry->second];
  438. // If we distinguish volatile accesses and if either the read or write
  439. // is volatile, do not omit any instrumentation.
  440. const bool AnyVolatile =
  441. ClDistinguishVolatile && (cast<LoadInst>(I)->isVolatile() ||
  442. cast<StoreInst>(WI.Inst)->isVolatile());
  443. if (!AnyVolatile) {
  444. // We will write to this temp, so no reason to analyze the read.
  445. // Mark the write instruction as compound.
  446. WI.Flags |= InstructionInfo::kCompoundRW;
  447. NumOmittedReadsBeforeWrite++;
  448. continue;
  449. }
  450. }
  451. if (addrPointsToConstantData(Addr)) {
  452. // Addr points to some constant data -- it can not race with any writes.
  453. continue;
  454. }
  455. }
  456. if (isa<AllocaInst>(getUnderlyingObject(Addr)) &&
  457. !PointerMayBeCaptured(Addr, true, true)) {
  458. // The variable is addressable but not captured, so it cannot be
  459. // referenced from a different thread and participate in a data race
  460. // (see llvm/Analysis/CaptureTracking.h for details).
  461. NumOmittedNonCaptured++;
  462. continue;
  463. }
  464. // Instrument this instruction.
  465. All.emplace_back(I);
  466. if (IsWrite) {
  467. // For read-before-write and compound instrumentation we only need one
  468. // write target, and we can override any previous entry if it exists.
  469. WriteTargets[Addr] = All.size() - 1;
  470. }
  471. }
  472. Local.clear();
  473. }
  474. static bool isAtomic(Instruction *I) {
  475. // TODO: Ask TTI whether synchronization scope is between threads.
  476. if (LoadInst *LI = dyn_cast<LoadInst>(I))
  477. return LI->isAtomic() && LI->getSyncScopeID() != SyncScope::SingleThread;
  478. if (StoreInst *SI = dyn_cast<StoreInst>(I))
  479. return SI->isAtomic() && SI->getSyncScopeID() != SyncScope::SingleThread;
  480. if (isa<AtomicRMWInst>(I))
  481. return true;
  482. if (isa<AtomicCmpXchgInst>(I))
  483. return true;
  484. if (isa<FenceInst>(I))
  485. return true;
  486. return false;
  487. }
  488. void ThreadSanitizer::InsertRuntimeIgnores(Function &F) {
  489. IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
  490. IRB.CreateCall(TsanIgnoreBegin);
  491. EscapeEnumerator EE(F, "tsan_ignore_cleanup", ClHandleCxxExceptions);
  492. while (IRBuilder<> *AtExit = EE.Next()) {
  493. AtExit->CreateCall(TsanIgnoreEnd);
  494. }
  495. }
  496. bool ThreadSanitizer::sanitizeFunction(Function &F,
  497. const TargetLibraryInfo &TLI) {
  498. // This is required to prevent instrumenting call to __tsan_init from within
  499. // the module constructor.
  500. if (F.getName() == kTsanModuleCtorName)
  501. return false;
  502. // Naked functions can not have prologue/epilogue
  503. // (__tsan_func_entry/__tsan_func_exit) generated, so don't instrument them at
  504. // all.
  505. if (F.hasFnAttribute(Attribute::Naked))
  506. return false;
  507. // __attribute__(disable_sanitizer_instrumentation) prevents all kinds of
  508. // instrumentation.
  509. if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
  510. return false;
  511. initialize(*F.getParent());
  512. SmallVector<InstructionInfo, 8> AllLoadsAndStores;
  513. SmallVector<Instruction*, 8> LocalLoadsAndStores;
  514. SmallVector<Instruction*, 8> AtomicAccesses;
  515. SmallVector<Instruction*, 8> MemIntrinCalls;
  516. bool Res = false;
  517. bool HasCalls = false;
  518. bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeThread);
  519. const DataLayout &DL = F.getParent()->getDataLayout();
  520. // Traverse all instructions, collect loads/stores/returns, check for calls.
  521. for (auto &BB : F) {
  522. for (auto &Inst : BB) {
  523. if (isAtomic(&Inst))
  524. AtomicAccesses.push_back(&Inst);
  525. else if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
  526. LocalLoadsAndStores.push_back(&Inst);
  527. else if ((isa<CallInst>(Inst) && !isa<DbgInfoIntrinsic>(Inst)) ||
  528. isa<InvokeInst>(Inst)) {
  529. if (CallInst *CI = dyn_cast<CallInst>(&Inst))
  530. maybeMarkSanitizerLibraryCallNoBuiltin(CI, &TLI);
  531. if (isa<MemIntrinsic>(Inst))
  532. MemIntrinCalls.push_back(&Inst);
  533. HasCalls = true;
  534. chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores,
  535. DL);
  536. }
  537. }
  538. chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores, DL);
  539. }
  540. // We have collected all loads and stores.
  541. // FIXME: many of these accesses do not need to be checked for races
  542. // (e.g. variables that do not escape, etc).
  543. // Instrument memory accesses only if we want to report bugs in the function.
  544. if (ClInstrumentMemoryAccesses && SanitizeFunction)
  545. for (const auto &II : AllLoadsAndStores) {
  546. Res |= instrumentLoadOrStore(II, DL);
  547. }
  548. // Instrument atomic memory accesses in any case (they can be used to
  549. // implement synchronization).
  550. if (ClInstrumentAtomics)
  551. for (auto Inst : AtomicAccesses) {
  552. Res |= instrumentAtomic(Inst, DL);
  553. }
  554. if (ClInstrumentMemIntrinsics && SanitizeFunction)
  555. for (auto Inst : MemIntrinCalls) {
  556. Res |= instrumentMemIntrinsic(Inst);
  557. }
  558. if (F.hasFnAttribute("sanitize_thread_no_checking_at_run_time")) {
  559. assert(!F.hasFnAttribute(Attribute::SanitizeThread));
  560. if (HasCalls)
  561. InsertRuntimeIgnores(F);
  562. }
  563. // Instrument function entry/exit points if there were instrumented accesses.
  564. if ((Res || HasCalls) && ClInstrumentFuncEntryExit) {
  565. IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
  566. Value *ReturnAddress = IRB.CreateCall(
  567. Intrinsic::getDeclaration(F.getParent(), Intrinsic::returnaddress),
  568. IRB.getInt32(0));
  569. IRB.CreateCall(TsanFuncEntry, ReturnAddress);
  570. EscapeEnumerator EE(F, "tsan_cleanup", ClHandleCxxExceptions);
  571. while (IRBuilder<> *AtExit = EE.Next()) {
  572. AtExit->CreateCall(TsanFuncExit, {});
  573. }
  574. Res = true;
  575. }
  576. return Res;
  577. }
  578. bool ThreadSanitizer::instrumentLoadOrStore(const InstructionInfo &II,
  579. const DataLayout &DL) {
  580. IRBuilder<> IRB(II.Inst);
  581. const bool IsWrite = isa<StoreInst>(*II.Inst);
  582. Value *Addr = IsWrite ? cast<StoreInst>(II.Inst)->getPointerOperand()
  583. : cast<LoadInst>(II.Inst)->getPointerOperand();
  584. Type *OrigTy = getLoadStoreType(II.Inst);
  585. // swifterror memory addresses are mem2reg promoted by instruction selection.
  586. // As such they cannot have regular uses like an instrumentation function and
  587. // it makes no sense to track them as memory.
  588. if (Addr->isSwiftError())
  589. return false;
  590. int Idx = getMemoryAccessFuncIndex(OrigTy, Addr, DL);
  591. if (Idx < 0)
  592. return false;
  593. if (IsWrite && isVtableAccess(II.Inst)) {
  594. LLVM_DEBUG(dbgs() << " VPTR : " << *II.Inst << "\n");
  595. Value *StoredValue = cast<StoreInst>(II.Inst)->getValueOperand();
  596. // StoredValue may be a vector type if we are storing several vptrs at once.
  597. // In this case, just take the first element of the vector since this is
  598. // enough to find vptr races.
  599. if (isa<VectorType>(StoredValue->getType()))
  600. StoredValue = IRB.CreateExtractElement(
  601. StoredValue, ConstantInt::get(IRB.getInt32Ty(), 0));
  602. if (StoredValue->getType()->isIntegerTy())
  603. StoredValue = IRB.CreateIntToPtr(StoredValue, IRB.getInt8PtrTy());
  604. // Call TsanVptrUpdate.
  605. IRB.CreateCall(TsanVptrUpdate,
  606. {IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
  607. IRB.CreatePointerCast(StoredValue, IRB.getInt8PtrTy())});
  608. NumInstrumentedVtableWrites++;
  609. return true;
  610. }
  611. if (!IsWrite && isVtableAccess(II.Inst)) {
  612. IRB.CreateCall(TsanVptrLoad,
  613. IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
  614. NumInstrumentedVtableReads++;
  615. return true;
  616. }
  617. const unsigned Alignment = IsWrite ? cast<StoreInst>(II.Inst)->getAlignment()
  618. : cast<LoadInst>(II.Inst)->getAlignment();
  619. const bool IsCompoundRW =
  620. ClCompoundReadBeforeWrite && (II.Flags & InstructionInfo::kCompoundRW);
  621. const bool IsVolatile = ClDistinguishVolatile &&
  622. (IsWrite ? cast<StoreInst>(II.Inst)->isVolatile()
  623. : cast<LoadInst>(II.Inst)->isVolatile());
  624. assert((!IsVolatile || !IsCompoundRW) && "Compound volatile invalid!");
  625. const uint32_t TypeSize = DL.getTypeStoreSizeInBits(OrigTy);
  626. FunctionCallee OnAccessFunc = nullptr;
  627. if (Alignment == 0 || Alignment >= 8 || (Alignment % (TypeSize / 8)) == 0) {
  628. if (IsCompoundRW)
  629. OnAccessFunc = TsanCompoundRW[Idx];
  630. else if (IsVolatile)
  631. OnAccessFunc = IsWrite ? TsanVolatileWrite[Idx] : TsanVolatileRead[Idx];
  632. else
  633. OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
  634. } else {
  635. if (IsCompoundRW)
  636. OnAccessFunc = TsanUnalignedCompoundRW[Idx];
  637. else if (IsVolatile)
  638. OnAccessFunc = IsWrite ? TsanUnalignedVolatileWrite[Idx]
  639. : TsanUnalignedVolatileRead[Idx];
  640. else
  641. OnAccessFunc = IsWrite ? TsanUnalignedWrite[Idx] : TsanUnalignedRead[Idx];
  642. }
  643. IRB.CreateCall(OnAccessFunc, IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
  644. if (IsCompoundRW || IsWrite)
  645. NumInstrumentedWrites++;
  646. if (IsCompoundRW || !IsWrite)
  647. NumInstrumentedReads++;
  648. return true;
  649. }
  650. static ConstantInt *createOrdering(IRBuilder<> *IRB, AtomicOrdering ord) {
  651. uint32_t v = 0;
  652. switch (ord) {
  653. case AtomicOrdering::NotAtomic:
  654. llvm_unreachable("unexpected atomic ordering!");
  655. case AtomicOrdering::Unordered: LLVM_FALLTHROUGH;
  656. case AtomicOrdering::Monotonic: v = 0; break;
  657. // Not specified yet:
  658. // case AtomicOrdering::Consume: v = 1; break;
  659. case AtomicOrdering::Acquire: v = 2; break;
  660. case AtomicOrdering::Release: v = 3; break;
  661. case AtomicOrdering::AcquireRelease: v = 4; break;
  662. case AtomicOrdering::SequentiallyConsistent: v = 5; break;
  663. }
  664. return IRB->getInt32(v);
  665. }
  666. // If a memset intrinsic gets inlined by the code gen, we will miss races on it.
  667. // So, we either need to ensure the intrinsic is not inlined, or instrument it.
  668. // We do not instrument memset/memmove/memcpy intrinsics (too complicated),
  669. // instead we simply replace them with regular function calls, which are then
  670. // intercepted by the run-time.
  671. // Since tsan is running after everyone else, the calls should not be
  672. // replaced back with intrinsics. If that becomes wrong at some point,
  673. // we will need to call e.g. __tsan_memset to avoid the intrinsics.
  674. bool ThreadSanitizer::instrumentMemIntrinsic(Instruction *I) {
  675. IRBuilder<> IRB(I);
  676. if (MemSetInst *M = dyn_cast<MemSetInst>(I)) {
  677. IRB.CreateCall(
  678. MemsetFn,
  679. {IRB.CreatePointerCast(M->getArgOperand(0), IRB.getInt8PtrTy()),
  680. IRB.CreateIntCast(M->getArgOperand(1), IRB.getInt32Ty(), false),
  681. IRB.CreateIntCast(M->getArgOperand(2), IntptrTy, false)});
  682. I->eraseFromParent();
  683. } else if (MemTransferInst *M = dyn_cast<MemTransferInst>(I)) {
  684. IRB.CreateCall(
  685. isa<MemCpyInst>(M) ? MemcpyFn : MemmoveFn,
  686. {IRB.CreatePointerCast(M->getArgOperand(0), IRB.getInt8PtrTy()),
  687. IRB.CreatePointerCast(M->getArgOperand(1), IRB.getInt8PtrTy()),
  688. IRB.CreateIntCast(M->getArgOperand(2), IntptrTy, false)});
  689. I->eraseFromParent();
  690. }
  691. return false;
  692. }
  693. // Both llvm and ThreadSanitizer atomic operations are based on C++11/C1x
  694. // standards. For background see C++11 standard. A slightly older, publicly
  695. // available draft of the standard (not entirely up-to-date, but close enough
  696. // for casual browsing) is available here:
  697. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
  698. // The following page contains more background information:
  699. // http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/
  700. bool ThreadSanitizer::instrumentAtomic(Instruction *I, const DataLayout &DL) {
  701. IRBuilder<> IRB(I);
  702. if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
  703. Value *Addr = LI->getPointerOperand();
  704. Type *OrigTy = LI->getType();
  705. int Idx = getMemoryAccessFuncIndex(OrigTy, Addr, DL);
  706. if (Idx < 0)
  707. return false;
  708. const unsigned ByteSize = 1U << Idx;
  709. const unsigned BitSize = ByteSize * 8;
  710. Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
  711. Type *PtrTy = Ty->getPointerTo();
  712. Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
  713. createOrdering(&IRB, LI->getOrdering())};
  714. Value *C = IRB.CreateCall(TsanAtomicLoad[Idx], Args);
  715. Value *Cast = IRB.CreateBitOrPointerCast(C, OrigTy);
  716. I->replaceAllUsesWith(Cast);
  717. } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
  718. Value *Addr = SI->getPointerOperand();
  719. int Idx =
  720. getMemoryAccessFuncIndex(SI->getValueOperand()->getType(), Addr, DL);
  721. if (Idx < 0)
  722. return false;
  723. const unsigned ByteSize = 1U << Idx;
  724. const unsigned BitSize = ByteSize * 8;
  725. Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
  726. Type *PtrTy = Ty->getPointerTo();
  727. Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
  728. IRB.CreateBitOrPointerCast(SI->getValueOperand(), Ty),
  729. createOrdering(&IRB, SI->getOrdering())};
  730. CallInst *C = CallInst::Create(TsanAtomicStore[Idx], Args);
  731. ReplaceInstWithInst(I, C);
  732. } else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I)) {
  733. Value *Addr = RMWI->getPointerOperand();
  734. int Idx =
  735. getMemoryAccessFuncIndex(RMWI->getValOperand()->getType(), Addr, DL);
  736. if (Idx < 0)
  737. return false;
  738. FunctionCallee F = TsanAtomicRMW[RMWI->getOperation()][Idx];
  739. if (!F)
  740. return false;
  741. const unsigned ByteSize = 1U << Idx;
  742. const unsigned BitSize = ByteSize * 8;
  743. Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
  744. Type *PtrTy = Ty->getPointerTo();
  745. Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
  746. IRB.CreateIntCast(RMWI->getValOperand(), Ty, false),
  747. createOrdering(&IRB, RMWI->getOrdering())};
  748. CallInst *C = CallInst::Create(F, Args);
  749. ReplaceInstWithInst(I, C);
  750. } else if (AtomicCmpXchgInst *CASI = dyn_cast<AtomicCmpXchgInst>(I)) {
  751. Value *Addr = CASI->getPointerOperand();
  752. Type *OrigOldValTy = CASI->getNewValOperand()->getType();
  753. int Idx = getMemoryAccessFuncIndex(OrigOldValTy, Addr, DL);
  754. if (Idx < 0)
  755. return false;
  756. const unsigned ByteSize = 1U << Idx;
  757. const unsigned BitSize = ByteSize * 8;
  758. Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
  759. Type *PtrTy = Ty->getPointerTo();
  760. Value *CmpOperand =
  761. IRB.CreateBitOrPointerCast(CASI->getCompareOperand(), Ty);
  762. Value *NewOperand =
  763. IRB.CreateBitOrPointerCast(CASI->getNewValOperand(), Ty);
  764. Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
  765. CmpOperand,
  766. NewOperand,
  767. createOrdering(&IRB, CASI->getSuccessOrdering()),
  768. createOrdering(&IRB, CASI->getFailureOrdering())};
  769. CallInst *C = IRB.CreateCall(TsanAtomicCAS[Idx], Args);
  770. Value *Success = IRB.CreateICmpEQ(C, CmpOperand);
  771. Value *OldVal = C;
  772. if (Ty != OrigOldValTy) {
  773. // The value is a pointer, so we need to cast the return value.
  774. OldVal = IRB.CreateIntToPtr(C, OrigOldValTy);
  775. }
  776. Value *Res =
  777. IRB.CreateInsertValue(UndefValue::get(CASI->getType()), OldVal, 0);
  778. Res = IRB.CreateInsertValue(Res, Success, 1);
  779. I->replaceAllUsesWith(Res);
  780. I->eraseFromParent();
  781. } else if (FenceInst *FI = dyn_cast<FenceInst>(I)) {
  782. Value *Args[] = {createOrdering(&IRB, FI->getOrdering())};
  783. FunctionCallee F = FI->getSyncScopeID() == SyncScope::SingleThread
  784. ? TsanAtomicSignalFence
  785. : TsanAtomicThreadFence;
  786. CallInst *C = CallInst::Create(F, Args);
  787. ReplaceInstWithInst(I, C);
  788. }
  789. return true;
  790. }
  791. int ThreadSanitizer::getMemoryAccessFuncIndex(Type *OrigTy, Value *Addr,
  792. const DataLayout &DL) {
  793. assert(OrigTy->isSized());
  794. assert(
  795. cast<PointerType>(Addr->getType())->isOpaqueOrPointeeTypeMatches(OrigTy));
  796. uint32_t TypeSize = DL.getTypeStoreSizeInBits(OrigTy);
  797. if (TypeSize != 8 && TypeSize != 16 &&
  798. TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
  799. NumAccessesWithBadSize++;
  800. // Ignore all unusual sizes.
  801. return -1;
  802. }
  803. size_t Idx = countTrailingZeros(TypeSize / 8);
  804. assert(Idx < kNumberOfAccessSizes);
  805. return Idx;
  806. }