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;
61 size_t NumOmittedReadsFromConstantGlobals;
62 size_t NumOmittedReadsFromVtable;
65 /// ThreadSanitizer: instrument the code in module to find races.
66 struct ThreadSanitizer : public FunctionPass {
68 bool runOnFunction(Function &F);
69 bool doInitialization(Module &M);
70 bool doFinalization(Module &M);
71 bool instrumentLoadOrStore(Instruction *I);
72 static char ID; // Pass identification, replacement for typeid.
75 void choseInstructionsToInstrument(SmallVectorImpl<Instruction*> &Local,
76 SmallVectorImpl<Instruction*> &All);
77 bool addrPointsToConstantData(Value *Addr);
80 OwningPtr<FunctionBlackList> BL;
81 // Callbacks to run-time library are computed in doInitialization.
84 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
85 static const size_t kNumberOfAccessSizes = 5;
86 Value *TsanRead[kNumberOfAccessSizes];
87 Value *TsanWrite[kNumberOfAccessSizes];
88 Value *TsanVptrUpdate;
90 // Stats are modified w/o synchronization.
91 ThreadSanitizerStats stats;
95 char ThreadSanitizer::ID = 0;
96 INITIALIZE_PASS(ThreadSanitizer, "tsan",
97 "ThreadSanitizer: detects data races.",
100 ThreadSanitizer::ThreadSanitizer()
105 FunctionPass *llvm::createThreadSanitizerPass() {
106 return new ThreadSanitizer();
109 bool ThreadSanitizer::doInitialization(Module &M) {
110 TD = getAnalysisIfAvailable<TargetData>();
113 BL.reset(new FunctionBlackList(ClBlackListFile));
114 memset(&stats, 0, sizeof(stats));
116 // Always insert a call to __tsan_init into the module's CTORs.
117 IRBuilder<> IRB(M.getContext());
118 Value *TsanInit = M.getOrInsertFunction("__tsan_init",
119 IRB.getVoidTy(), NULL);
120 appendToGlobalCtors(M, cast<Function>(TsanInit), 0);
122 // Initialize the callbacks.
123 TsanFuncEntry = M.getOrInsertFunction("__tsan_func_entry", IRB.getVoidTy(),
124 IRB.getInt8PtrTy(), NULL);
125 TsanFuncExit = M.getOrInsertFunction("__tsan_func_exit", IRB.getVoidTy(),
127 for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
128 SmallString<32> ReadName("__tsan_read");
129 ReadName += itostr(1 << i);
130 TsanRead[i] = M.getOrInsertFunction(ReadName, IRB.getVoidTy(),
131 IRB.getInt8PtrTy(), NULL);
132 SmallString<32> WriteName("__tsan_write");
133 WriteName += itostr(1 << i);
134 TsanWrite[i] = M.getOrInsertFunction(WriteName, IRB.getVoidTy(),
135 IRB.getInt8PtrTy(), NULL);
137 TsanVptrUpdate = M.getOrInsertFunction("__tsan_vptr_update", IRB.getVoidTy(),
138 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
143 bool ThreadSanitizer::doFinalization(Module &M) {
145 errs() << "ThreadSanitizerStats " << M.getModuleIdentifier()
146 << ": wr " << stats.NumInstrumentedWrites
147 << "; rd " << stats.NumInstrumentedReads
148 << "; vt " << stats.NumInstrumentedVtableWrites
149 << "; bs " << stats.NumAccessesWithBadSize
150 << "; rbw " << stats.NumOmittedReadsBeforeWrite
151 << "; rcg " << stats.NumOmittedReadsFromConstantGlobals
152 << "; rvt " << stats.NumOmittedReadsFromVtable
158 static bool isVtableAccess(Instruction *I) {
159 if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa)) {
160 if (Tag->getNumOperands() < 1) return false;
161 if (MDString *Tag1 = dyn_cast<MDString>(Tag->getOperand(0))) {
162 if (Tag1->getString() == "vtable pointer") return true;
168 bool ThreadSanitizer::addrPointsToConstantData(Value *Addr) {
169 // If this is a GEP, just analyze its pointer operand.
170 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr))
171 Addr = GEP->getPointerOperand();
173 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
174 if (GV->isConstant()) {
175 // Reads from constant globals can not race with any writes.
176 stats.NumOmittedReadsFromConstantGlobals++;
179 } else if(LoadInst *L = dyn_cast<LoadInst>(Addr)) {
180 if (isVtableAccess(L)) {
181 // Reads from a vtable pointer can not race with any writes.
182 stats.NumOmittedReadsFromVtable++;
189 // Instrumenting some of the accesses may be proven redundant.
190 // Currently handled:
191 // - read-before-write (within same BB, no calls between)
193 // We do not handle some of the patterns that should not survive
194 // after the classic compiler optimizations.
195 // E.g. two reads from the same temp should be eliminated by CSE,
196 // two writes should be eliminated by DSE, etc.
198 // 'Local' is a vector of insns within the same BB (no calls between).
199 // 'All' is a vector of insns that will be instrumented.
200 void ThreadSanitizer::choseInstructionsToInstrument(
201 SmallVectorImpl<Instruction*> &Local,
202 SmallVectorImpl<Instruction*> &All) {
203 SmallSet<Value*, 8> WriteTargets;
204 // Iterate from the end.
205 for (SmallVectorImpl<Instruction*>::reverse_iterator It = Local.rbegin(),
206 E = Local.rend(); It != E; ++It) {
207 Instruction *I = *It;
208 if (StoreInst *Store = dyn_cast<StoreInst>(I)) {
209 WriteTargets.insert(Store->getPointerOperand());
211 LoadInst *Load = cast<LoadInst>(I);
212 Value *Addr = Load->getPointerOperand();
213 if (WriteTargets.count(Addr)) {
214 // We will write to this temp, so no reason to analyze the read.
215 stats.NumOmittedReadsBeforeWrite++;
218 if (addrPointsToConstantData(Addr)) {
219 // Addr points to some constant data -- it can not race with any writes.
228 bool ThreadSanitizer::runOnFunction(Function &F) {
229 if (!TD) return false;
230 if (BL->isIn(F)) return false;
231 SmallVector<Instruction*, 8> RetVec;
232 SmallVector<Instruction*, 8> AllLoadsAndStores;
233 SmallVector<Instruction*, 8> LocalLoadsAndStores;
235 bool HasCalls = false;
237 // Traverse all instructions, collect loads/stores/returns, check for calls.
238 for (Function::iterator FI = F.begin(), FE = F.end();
240 BasicBlock &BB = *FI;
241 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
243 if (isa<LoadInst>(BI) || isa<StoreInst>(BI))
244 LocalLoadsAndStores.push_back(BI);
245 else if (isa<ReturnInst>(BI))
246 RetVec.push_back(BI);
247 else if (isa<CallInst>(BI) || isa<InvokeInst>(BI)) {
249 choseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
252 choseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
255 // We have collected all loads and stores.
256 // FIXME: many of these accesses do not need to be checked for races
257 // (e.g. variables that do not escape, etc).
259 // Instrument memory accesses.
260 for (size_t i = 0, n = AllLoadsAndStores.size(); i < n; ++i) {
261 Res |= instrumentLoadOrStore(AllLoadsAndStores[i]);
264 // Instrument function entry/exit points if there were instrumented accesses.
265 if (Res || HasCalls) {
266 IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
267 Value *ReturnAddress = IRB.CreateCall(
268 Intrinsic::getDeclaration(F.getParent(), Intrinsic::returnaddress),
270 IRB.CreateCall(TsanFuncEntry, ReturnAddress);
271 for (size_t i = 0, n = RetVec.size(); i < n; ++i) {
272 IRBuilder<> IRBRet(RetVec[i]);
273 IRBRet.CreateCall(TsanFuncExit);
280 bool ThreadSanitizer::instrumentLoadOrStore(Instruction *I) {
282 bool IsWrite = isa<StoreInst>(*I);
283 Value *Addr = IsWrite
284 ? cast<StoreInst>(I)->getPointerOperand()
285 : cast<LoadInst>(I)->getPointerOperand();
286 Type *OrigPtrTy = Addr->getType();
287 Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
288 assert(OrigTy->isSized());
289 uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
290 if (TypeSize != 8 && TypeSize != 16 &&
291 TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
292 stats.NumAccessesWithBadSize++;
293 // Ignore all unusual sizes.
296 if (IsWrite && isVtableAccess(I)) {
297 Value *StoredValue = cast<StoreInst>(I)->getValueOperand();
298 IRB.CreateCall2(TsanVptrUpdate,
299 IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
300 IRB.CreatePointerCast(StoredValue, IRB.getInt8PtrTy()));
301 stats.NumInstrumentedVtableWrites++;
304 size_t Idx = CountTrailingZeros_32(TypeSize / 8);
305 assert(Idx < kNumberOfAccessSizes);
306 Value *OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
307 IRB.CreateCall(OnAccessFunc, IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
308 if (IsWrite) stats.NumInstrumentedWrites++;
309 else stats.NumInstrumentedReads++;