1 //===-- ThreadSanitizer.cpp - race detector -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer, a race detector.
12 // The tool is under development, for the details about previous versions see
13 // http://code.google.com/p/data-race-test
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 //===----------------------------------------------------------------------===//
22 #define DEBUG_TYPE "tsan"
24 #include "FunctionBlackList.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/Intrinsics.h"
30 #include "llvm/Function.h"
31 #include "llvm/LLVMContext.h"
32 #include "llvm/Metadata.h"
33 #include "llvm/Module.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/IRBuilder.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Target/TargetData.h"
40 #include "llvm/Transforms/Instrumentation.h"
41 #include "llvm/Transforms/Utils/ModuleUtils.h"
42 #include "llvm/Type.h"
46 static cl::opt<std::string> ClBlackListFile("tsan-blacklist",
47 cl::desc("Blacklist file"), cl::Hidden);
49 static cl::opt<bool> ClPrintStats("tsan-print-stats",
50 cl::desc("Print ThreadSanitizer instrumentation stats"), cl::Hidden);
54 // Stats counters for ThreadSanitizer instrumentation.
55 struct ThreadSanitizerStats {
56 size_t NumInstrumentedReads;
57 size_t NumInstrumentedWrites;
58 size_t NumOmittedReadsBeforeWrite;
59 size_t NumAccessesWithBadSize;
60 size_t NumInstrumentedVtableWrites;
63 /// ThreadSanitizer: instrument the code in module to find races.
64 struct ThreadSanitizer : public FunctionPass {
66 bool runOnFunction(Function &F);
67 bool doInitialization(Module &M);
68 bool doFinalization(Module &M);
69 bool instrumentLoadOrStore(Instruction *I);
70 static char ID; // Pass identification, replacement for typeid.
73 void choseInstructionsToInstrument(SmallVectorImpl<Instruction*> &Local,
74 SmallVectorImpl<Instruction*> &All);
77 OwningPtr<FunctionBlackList> BL;
78 // Callbacks to run-time library are computed in doInitialization.
81 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
82 static const size_t kNumberOfAccessSizes = 5;
83 Value *TsanRead[kNumberOfAccessSizes];
84 Value *TsanWrite[kNumberOfAccessSizes];
85 Value *TsanVptrUpdate;
87 // Stats are modified w/o synchronization.
88 ThreadSanitizerStats stats;
92 char ThreadSanitizer::ID = 0;
93 INITIALIZE_PASS(ThreadSanitizer, "tsan",
94 "ThreadSanitizer: detects data races.",
97 ThreadSanitizer::ThreadSanitizer()
102 FunctionPass *llvm::createThreadSanitizerPass() {
103 return new ThreadSanitizer();
106 bool ThreadSanitizer::doInitialization(Module &M) {
107 TD = getAnalysisIfAvailable<TargetData>();
110 BL.reset(new FunctionBlackList(ClBlackListFile));
111 memset(&stats, 0, sizeof(stats));
113 // Always insert a call to __tsan_init into the module's CTORs.
114 IRBuilder<> IRB(M.getContext());
115 Value *TsanInit = M.getOrInsertFunction("__tsan_init",
116 IRB.getVoidTy(), NULL);
117 appendToGlobalCtors(M, cast<Function>(TsanInit), 0);
119 // Initialize the callbacks.
120 TsanFuncEntry = M.getOrInsertFunction("__tsan_func_entry", IRB.getVoidTy(),
121 IRB.getInt8PtrTy(), NULL);
122 TsanFuncExit = M.getOrInsertFunction("__tsan_func_exit", IRB.getVoidTy(),
124 for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
125 SmallString<32> ReadName("__tsan_read");
126 ReadName += itostr(1 << i);
127 TsanRead[i] = M.getOrInsertFunction(ReadName, IRB.getVoidTy(),
128 IRB.getInt8PtrTy(), NULL);
129 SmallString<32> WriteName("__tsan_write");
130 WriteName += itostr(1 << i);
131 TsanWrite[i] = M.getOrInsertFunction(WriteName, IRB.getVoidTy(),
132 IRB.getInt8PtrTy(), NULL);
134 TsanVptrUpdate = M.getOrInsertFunction("__tsan_vptr_update", IRB.getVoidTy(),
135 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
140 bool ThreadSanitizer::doFinalization(Module &M) {
142 errs() << "ThreadSanitizerStats " << M.getModuleIdentifier()
143 << ": wr " << stats.NumInstrumentedWrites
144 << "; rd " << stats.NumInstrumentedReads
145 << "; vt " << stats.NumInstrumentedVtableWrites
146 << "; bs " << stats.NumAccessesWithBadSize
147 << "; rbw " << stats.NumOmittedReadsBeforeWrite
153 // Instrumenting some of the accesses may be proven redundant.
154 // Currently handled:
155 // - read-before-write (within same BB, no calls between)
157 // We do not handle some of the patterns that should not survive
158 // after the classic compiler optimizations.
159 // E.g. two reads from the same temp should be eliminated by CSE,
160 // two writes should be eliminated by DSE, etc.
162 // 'Local' is a vector of insns within the same BB (no calls between).
163 // 'All' is a vector of insns that will be instrumented.
164 void ThreadSanitizer::choseInstructionsToInstrument(
165 SmallVectorImpl<Instruction*> &Local,
166 SmallVectorImpl<Instruction*> &All) {
167 SmallSet<Value*, 8> WriteTargets;
168 // Iterate from the end.
169 for (SmallVectorImpl<Instruction*>::reverse_iterator It = Local.rbegin(),
170 E = Local.rend(); It != E; ++It) {
171 Instruction *I = *It;
172 if (StoreInst *Store = dyn_cast<StoreInst>(I)) {
173 WriteTargets.insert(Store->getPointerOperand());
175 LoadInst *Load = cast<LoadInst>(I);
176 if (WriteTargets.count(Load->getPointerOperand())) {
177 // We will write to this temp, so no reason to analyze the read.
178 stats.NumOmittedReadsBeforeWrite++;
187 bool ThreadSanitizer::runOnFunction(Function &F) {
188 if (!TD) return false;
189 if (BL->isIn(F)) return false;
190 SmallVector<Instruction*, 8> RetVec;
191 SmallVector<Instruction*, 8> AllLoadsAndStores;
192 SmallVector<Instruction*, 8> LocalLoadsAndStores;
194 bool HasCalls = false;
196 // Traverse all instructions, collect loads/stores/returns, check for calls.
197 for (Function::iterator FI = F.begin(), FE = F.end();
199 BasicBlock &BB = *FI;
200 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
202 if (isa<LoadInst>(BI) || isa<StoreInst>(BI))
203 LocalLoadsAndStores.push_back(BI);
204 else if (isa<ReturnInst>(BI))
205 RetVec.push_back(BI);
206 else if (isa<CallInst>(BI) || isa<InvokeInst>(BI)) {
208 choseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
211 choseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
214 // We have collected all loads and stores.
215 // FIXME: many of these accesses do not need to be checked for races
216 // (e.g. variables that do not escape, etc).
218 // Instrument memory accesses.
219 for (size_t i = 0, n = AllLoadsAndStores.size(); i < n; ++i) {
220 Res |= instrumentLoadOrStore(AllLoadsAndStores[i]);
223 // Instrument function entry/exit points if there were instrumented accesses.
224 if (Res || HasCalls) {
225 IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
226 Value *ReturnAddress = IRB.CreateCall(
227 Intrinsic::getDeclaration(F.getParent(), Intrinsic::returnaddress),
229 IRB.CreateCall(TsanFuncEntry, ReturnAddress);
230 for (size_t i = 0, n = RetVec.size(); i < n; ++i) {
231 IRBuilder<> IRBRet(RetVec[i]);
232 IRBRet.CreateCall(TsanFuncExit);
239 static bool isVtableAccess(Instruction *I) {
240 if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa)) {
241 if (Tag->getNumOperands() < 1) return false;
242 if (MDString *Tag1 = dyn_cast<MDString>(Tag->getOperand(0))) {
243 if (Tag1->getString() == "vtable pointer") return true;
249 bool ThreadSanitizer::instrumentLoadOrStore(Instruction *I) {
251 bool IsWrite = isa<StoreInst>(*I);
252 Value *Addr = IsWrite
253 ? cast<StoreInst>(I)->getPointerOperand()
254 : cast<LoadInst>(I)->getPointerOperand();
255 Type *OrigPtrTy = Addr->getType();
256 Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
257 assert(OrigTy->isSized());
258 uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
259 if (TypeSize != 8 && TypeSize != 16 &&
260 TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
261 stats.NumAccessesWithBadSize++;
262 // Ignore all unusual sizes.
265 if (IsWrite && isVtableAccess(I)) {
266 Value *StoredValue = cast<StoreInst>(I)->getValueOperand();
267 IRB.CreateCall2(TsanVptrUpdate,
268 IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
269 IRB.CreatePointerCast(StoredValue, IRB.getInt8PtrTy()));
270 stats.NumInstrumentedVtableWrites++;
273 size_t Idx = CountTrailingZeros_32(TypeSize / 8);
274 assert(Idx < kNumberOfAccessSizes);
275 Value *OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
276 IRB.CreateCall(OnAccessFunc, IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
277 if (IsWrite) stats.NumInstrumentedWrites++;
278 else stats.NumInstrumentedReads++;