blob: 89fb746a5c472f76c496903946da2c32d4257b58 [file] [log] [blame]
Kostya Serebryany60ebb1942012-02-13 22:50:51 +00001//===-- ThreadSanitizer.cpp - race detector -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of ThreadSanitizer, a race detector.
11//
12// The tool is under development, for the details about previous versions see
13// http://code.google.com/p/data-race-test
14//
15// The instrumentation phase is quite simple:
16// - Insert calls to run-time library before every memory access.
17// - Optimizations may apply to avoid instrumenting some of the accesses.
18// - Insert calls at function entry/exit.
19// The rest is handled by the run-time library.
20//===----------------------------------------------------------------------===//
21
22#define DEBUG_TYPE "tsan"
23
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Transforms/Instrumentation.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/ADT/SmallSet.h"
26#include "llvm/ADT/SmallString.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/StringExtras.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/DataLayout.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/IRBuilder.h"
Kostya Serebryanyf4644812013-03-28 11:21:13 +000033#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000034#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"
Kostya Serebryany6e590e32012-03-14 23:33:24 +000039#include "llvm/Support/CommandLine.h"
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000040#include "llvm/Support/Debug.h"
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000041#include "llvm/Support/MathExtras.h"
Kostya Serebryany52eb69922012-03-26 17:35:03 +000042#include "llvm/Support/raw_ostream.h"
Kostya Serebryanye5079222012-04-27 07:31:53 +000043#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000044#include "llvm/Transforms/Utils/ModuleUtils.h"
Peter Collingbourne405515d2013-07-09 22:02:49 +000045#include "llvm/Transforms/Utils/SpecialCaseList.h"
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000046
47using namespace llvm;
48
Alexey Samsonovf045df12012-12-28 09:30:44 +000049static cl::opt<std::string> ClBlacklistFile("tsan-blacklist",
Kostya Serebryany6e590e32012-03-14 23:33:24 +000050 cl::desc("Blacklist file"), cl::Hidden);
Kostya Serebryany41d876c2012-10-04 05:28:50 +000051static cl::opt<bool> ClInstrumentMemoryAccesses(
52 "tsan-instrument-memory-accesses", cl::init(true),
53 cl::desc("Instrument memory accesses"), cl::Hidden);
54static cl::opt<bool> ClInstrumentFuncEntryExit(
55 "tsan-instrument-func-entry-exit", cl::init(true),
56 cl::desc("Instrument function entry and exit"), cl::Hidden);
57static cl::opt<bool> ClInstrumentAtomics(
58 "tsan-instrument-atomics", cl::init(true),
59 cl::desc("Instrument atomics"), cl::Hidden);
Kostya Serebryanyf4644812013-03-28 11:21:13 +000060static cl::opt<bool> ClInstrumentMemIntrinsics(
61 "tsan-instrument-memintrinsics", cl::init(true),
62 cl::desc("Instrument memintrinsics (memset/memcpy/memmove)"), cl::Hidden);
Kostya Serebryany6e590e32012-03-14 23:33:24 +000063
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +000064STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
65STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
Alexey Samsonov1dfe9b52012-08-30 13:47:13 +000066STATISTIC(NumOmittedReadsBeforeWrite,
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +000067 "Number of reads ignored due to following writes");
68STATISTIC(NumAccessesWithBadSize, "Number of accesses with bad size");
69STATISTIC(NumInstrumentedVtableWrites, "Number of vtable ptr writes");
Dmitry Vyukovab78ac12013-03-22 08:51:22 +000070STATISTIC(NumInstrumentedVtableReads, "Number of vtable ptr reads");
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +000071STATISTIC(NumOmittedReadsFromConstantGlobals,
72 "Number of reads from constant globals");
73STATISTIC(NumOmittedReadsFromVtable, "Number of vtable reads");
Kostya Serebryany2076af02012-04-10 18:18:56 +000074
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000075namespace {
Kostya Serebryany2076af02012-04-10 18:18:56 +000076
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000077/// ThreadSanitizer: instrument the code in module to find races.
78struct ThreadSanitizer : public FunctionPass {
Alexey Samsonovf045df12012-12-28 09:30:44 +000079 ThreadSanitizer(StringRef BlacklistFile = StringRef())
80 : FunctionPass(ID),
81 TD(0),
82 BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
83 : BlacklistFile) { }
Kostya Serebryanye5079222012-04-27 07:31:53 +000084 const char *getPassName() const;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000085 bool runOnFunction(Function &F);
86 bool doInitialization(Module &M);
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000087 static char ID; // Pass identification, replacement for typeid.
88
89 private:
Kostya Serebryany8b390ff2012-11-29 09:54:21 +000090 void initializeCallbacks(Module &M);
Kostya Serebryanye5079222012-04-27 07:31:53 +000091 bool instrumentLoadOrStore(Instruction *I);
92 bool instrumentAtomic(Instruction *I);
Kostya Serebryanyf4644812013-03-28 11:21:13 +000093 bool instrumentMemIntrinsic(Instruction *I);
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +000094 void chooseInstructionsToInstrument(SmallVectorImpl<Instruction*> &Local,
95 SmallVectorImpl<Instruction*> &All);
Kostya Serebryanycff60c12012-04-10 22:29:17 +000096 bool addrPointsToConstantData(Value *Addr);
Kostya Serebryanye5079222012-04-27 07:31:53 +000097 int getMemoryAccessFuncIndex(Value *Addr);
Kostya Serebryany2076af02012-04-10 18:18:56 +000098
Micah Villmow3574eca2012-10-08 16:38:25 +000099 DataLayout *TD;
Kostya Serebryanyf4644812013-03-28 11:21:13 +0000100 Type *IntptrTy;
Alexey Samsonovf045df12012-12-28 09:30:44 +0000101 SmallString<64> BlacklistFile;
Peter Collingbourne405515d2013-07-09 22:02:49 +0000102 OwningPtr<SpecialCaseList> BL;
Kostya Serebryanye5079222012-04-27 07:31:53 +0000103 IntegerType *OrdTy;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000104 // Callbacks to run-time library are computed in doInitialization.
Kostya Serebryanye5079222012-04-27 07:31:53 +0000105 Function *TsanFuncEntry;
106 Function *TsanFuncExit;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000107 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
Kostya Serebryany3eccaa62012-02-14 00:52:07 +0000108 static const size_t kNumberOfAccessSizes = 5;
Kostya Serebryanye5079222012-04-27 07:31:53 +0000109 Function *TsanRead[kNumberOfAccessSizes];
110 Function *TsanWrite[kNumberOfAccessSizes];
111 Function *TsanAtomicLoad[kNumberOfAccessSizes];
112 Function *TsanAtomicStore[kNumberOfAccessSizes];
Dmitry Vyukov9f8a90b2012-11-09 12:55:36 +0000113 Function *TsanAtomicRMW[AtomicRMWInst::LAST_BINOP + 1][kNumberOfAccessSizes];
114 Function *TsanAtomicCAS[kNumberOfAccessSizes];
115 Function *TsanAtomicThreadFence;
116 Function *TsanAtomicSignalFence;
Kostya Serebryanye5079222012-04-27 07:31:53 +0000117 Function *TsanVptrUpdate;
Dmitry Vyukovab78ac12013-03-22 08:51:22 +0000118 Function *TsanVptrLoad;
Kostya Serebryanyf4644812013-03-28 11:21:13 +0000119 Function *MemmoveFn, *MemcpyFn, *MemsetFn;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000120};
121} // namespace
122
123char ThreadSanitizer::ID = 0;
124INITIALIZE_PASS(ThreadSanitizer, "tsan",
125 "ThreadSanitizer: detects data races.",
126 false, false)
127
Kostya Serebryanye5079222012-04-27 07:31:53 +0000128const char *ThreadSanitizer::getPassName() const {
129 return "ThreadSanitizer";
130}
131
Alexey Samsonovf045df12012-12-28 09:30:44 +0000132FunctionPass *llvm::createThreadSanitizerPass(StringRef BlacklistFile) {
133 return new ThreadSanitizer(BlacklistFile);
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000134}
135
Kostya Serebryanye5079222012-04-27 07:31:53 +0000136static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
137 if (Function *F = dyn_cast<Function>(FuncOrBitcast))
138 return F;
139 FuncOrBitcast->dump();
140 report_fatal_error("ThreadSanitizer interface function redefined");
141}
142
Kostya Serebryany8b390ff2012-11-29 09:54:21 +0000143void ThreadSanitizer::initializeCallbacks(Module &M) {
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000144 IRBuilder<> IRB(M.getContext());
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000145 // Initialize the callbacks.
Kostya Serebryanye5079222012-04-27 07:31:53 +0000146 TsanFuncEntry = checkInterfaceFunction(M.getOrInsertFunction(
147 "__tsan_func_entry", IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
148 TsanFuncExit = checkInterfaceFunction(M.getOrInsertFunction(
149 "__tsan_func_exit", IRB.getVoidTy(), NULL));
150 OrdTy = IRB.getInt32Ty();
Kostya Serebryany3eccaa62012-02-14 00:52:07 +0000151 for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
Kostya Serebryanye5079222012-04-27 07:31:53 +0000152 const size_t ByteSize = 1 << i;
153 const size_t BitSize = ByteSize * 8;
154 SmallString<32> ReadName("__tsan_read" + itostr(ByteSize));
155 TsanRead[i] = checkInterfaceFunction(M.getOrInsertFunction(
156 ReadName, IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
157
158 SmallString<32> WriteName("__tsan_write" + itostr(ByteSize));
159 TsanWrite[i] = checkInterfaceFunction(M.getOrInsertFunction(
160 WriteName, IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
161
162 Type *Ty = Type::getIntNTy(M.getContext(), BitSize);
163 Type *PtrTy = Ty->getPointerTo();
164 SmallString<32> AtomicLoadName("__tsan_atomic" + itostr(BitSize) +
165 "_load");
166 TsanAtomicLoad[i] = checkInterfaceFunction(M.getOrInsertFunction(
167 AtomicLoadName, Ty, PtrTy, OrdTy, NULL));
168
169 SmallString<32> AtomicStoreName("__tsan_atomic" + itostr(BitSize) +
170 "_store");
171 TsanAtomicStore[i] = checkInterfaceFunction(M.getOrInsertFunction(
172 AtomicStoreName, IRB.getVoidTy(), PtrTy, Ty, OrdTy,
173 NULL));
Dmitry Vyukov9f8a90b2012-11-09 12:55:36 +0000174
175 for (int op = AtomicRMWInst::FIRST_BINOP;
176 op <= AtomicRMWInst::LAST_BINOP; ++op) {
177 TsanAtomicRMW[op][i] = NULL;
178 const char *NamePart = NULL;
179 if (op == AtomicRMWInst::Xchg)
180 NamePart = "_exchange";
181 else if (op == AtomicRMWInst::Add)
182 NamePart = "_fetch_add";
183 else if (op == AtomicRMWInst::Sub)
184 NamePart = "_fetch_sub";
185 else if (op == AtomicRMWInst::And)
186 NamePart = "_fetch_and";
187 else if (op == AtomicRMWInst::Or)
188 NamePart = "_fetch_or";
189 else if (op == AtomicRMWInst::Xor)
190 NamePart = "_fetch_xor";
Dmitry Vyukovb10675e2012-11-27 08:09:25 +0000191 else if (op == AtomicRMWInst::Nand)
192 NamePart = "_fetch_nand";
Dmitry Vyukov9f8a90b2012-11-09 12:55:36 +0000193 else
194 continue;
195 SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart);
196 TsanAtomicRMW[op][i] = checkInterfaceFunction(M.getOrInsertFunction(
197 RMWName, Ty, PtrTy, Ty, OrdTy, NULL));
198 }
199
200 SmallString<32> AtomicCASName("__tsan_atomic" + itostr(BitSize) +
201 "_compare_exchange_val");
202 TsanAtomicCAS[i] = checkInterfaceFunction(M.getOrInsertFunction(
Dmitry Vyukov6702e532012-11-26 11:36:19 +0000203 AtomicCASName, Ty, PtrTy, Ty, Ty, OrdTy, OrdTy, NULL));
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000204 }
Kostya Serebryanye5079222012-04-27 07:31:53 +0000205 TsanVptrUpdate = checkInterfaceFunction(M.getOrInsertFunction(
206 "__tsan_vptr_update", IRB.getVoidTy(), IRB.getInt8PtrTy(),
207 IRB.getInt8PtrTy(), NULL));
Dmitry Vyukovab78ac12013-03-22 08:51:22 +0000208 TsanVptrLoad = checkInterfaceFunction(M.getOrInsertFunction(
209 "__tsan_vptr_read", IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
Dmitry Vyukov9f8a90b2012-11-09 12:55:36 +0000210 TsanAtomicThreadFence = checkInterfaceFunction(M.getOrInsertFunction(
211 "__tsan_atomic_thread_fence", IRB.getVoidTy(), OrdTy, NULL));
212 TsanAtomicSignalFence = checkInterfaceFunction(M.getOrInsertFunction(
213 "__tsan_atomic_signal_fence", IRB.getVoidTy(), OrdTy, NULL));
Kostya Serebryanyf4644812013-03-28 11:21:13 +0000214
215 MemmoveFn = checkInterfaceFunction(M.getOrInsertFunction(
216 "memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
217 IRB.getInt8PtrTy(), IntptrTy, NULL));
218 MemcpyFn = checkInterfaceFunction(M.getOrInsertFunction(
219 "memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
220 IntptrTy, NULL));
221 MemsetFn = checkInterfaceFunction(M.getOrInsertFunction(
222 "memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(),
223 IntptrTy, NULL));
Kostya Serebryany8b390ff2012-11-29 09:54:21 +0000224}
225
226bool ThreadSanitizer::doInitialization(Module &M) {
227 TD = getAnalysisIfAvailable<DataLayout>();
228 if (!TD)
229 return false;
Alexey Samsonove39e1312013-08-12 11:46:09 +0000230 BL.reset(SpecialCaseList::createOrDie(BlacklistFile));
Kostya Serebryany8b390ff2012-11-29 09:54:21 +0000231
232 // Always insert a call to __tsan_init into the module's CTORs.
233 IRBuilder<> IRB(M.getContext());
Kostya Serebryanyf4644812013-03-28 11:21:13 +0000234 IntptrTy = IRB.getIntPtrTy(TD);
Kostya Serebryany8b390ff2012-11-29 09:54:21 +0000235 Value *TsanInit = M.getOrInsertFunction("__tsan_init",
236 IRB.getVoidTy(), NULL);
237 appendToGlobalCtors(M, cast<Function>(TsanInit), 0);
238
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000239 return true;
240}
241
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000242static bool isVtableAccess(Instruction *I) {
Manman Ren0b3d3922013-09-06 22:47:05 +0000243 if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa))
244 return Tag->isTBAAVtableAccess();
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000245 return false;
246}
247
248bool ThreadSanitizer::addrPointsToConstantData(Value *Addr) {
249 // If this is a GEP, just analyze its pointer operand.
250 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr))
251 Addr = GEP->getPointerOperand();
252
253 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
254 if (GV->isConstant()) {
255 // Reads from constant globals can not race with any writes.
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +0000256 NumOmittedReadsFromConstantGlobals++;
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000257 return true;
258 }
Alexey Samsonov1dfe9b52012-08-30 13:47:13 +0000259 } else if (LoadInst *L = dyn_cast<LoadInst>(Addr)) {
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000260 if (isVtableAccess(L)) {
261 // Reads from a vtable pointer can not race with any writes.
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +0000262 NumOmittedReadsFromVtable++;
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000263 return true;
264 }
265 }
266 return false;
267}
268
Kostya Serebryany2076af02012-04-10 18:18:56 +0000269// Instrumenting some of the accesses may be proven redundant.
270// Currently handled:
271// - read-before-write (within same BB, no calls between)
272//
273// We do not handle some of the patterns that should not survive
274// after the classic compiler optimizations.
275// E.g. two reads from the same temp should be eliminated by CSE,
276// two writes should be eliminated by DSE, etc.
277//
278// 'Local' is a vector of insns within the same BB (no calls between).
279// 'All' is a vector of insns that will be instrumented.
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000280void ThreadSanitizer::chooseInstructionsToInstrument(
Kostya Serebryany2076af02012-04-10 18:18:56 +0000281 SmallVectorImpl<Instruction*> &Local,
282 SmallVectorImpl<Instruction*> &All) {
283 SmallSet<Value*, 8> WriteTargets;
284 // Iterate from the end.
285 for (SmallVectorImpl<Instruction*>::reverse_iterator It = Local.rbegin(),
286 E = Local.rend(); It != E; ++It) {
287 Instruction *I = *It;
288 if (StoreInst *Store = dyn_cast<StoreInst>(I)) {
289 WriteTargets.insert(Store->getPointerOperand());
290 } else {
291 LoadInst *Load = cast<LoadInst>(I);
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000292 Value *Addr = Load->getPointerOperand();
293 if (WriteTargets.count(Addr)) {
Kostya Serebryany2076af02012-04-10 18:18:56 +0000294 // We will write to this temp, so no reason to analyze the read.
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +0000295 NumOmittedReadsBeforeWrite++;
Kostya Serebryany2076af02012-04-10 18:18:56 +0000296 continue;
297 }
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000298 if (addrPointsToConstantData(Addr)) {
299 // Addr points to some constant data -- it can not race with any writes.
300 continue;
301 }
Kostya Serebryany2076af02012-04-10 18:18:56 +0000302 }
303 All.push_back(I);
304 }
305 Local.clear();
306}
307
Kostya Serebryanye5079222012-04-27 07:31:53 +0000308static bool isAtomic(Instruction *I) {
309 if (LoadInst *LI = dyn_cast<LoadInst>(I))
310 return LI->isAtomic() && LI->getSynchScope() == CrossThread;
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000311 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Kostya Serebryanye5079222012-04-27 07:31:53 +0000312 return SI->isAtomic() && SI->getSynchScope() == CrossThread;
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000313 if (isa<AtomicRMWInst>(I))
Kostya Serebryanye5079222012-04-27 07:31:53 +0000314 return true;
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000315 if (isa<AtomicCmpXchgInst>(I))
Kostya Serebryanye5079222012-04-27 07:31:53 +0000316 return true;
Dmitry Vyukov9f8a90b2012-11-09 12:55:36 +0000317 if (isa<FenceInst>(I))
318 return true;
Kostya Serebryanye5079222012-04-27 07:31:53 +0000319 return false;
320}
321
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000322bool ThreadSanitizer::runOnFunction(Function &F) {
323 if (!TD) return false;
Kostya Serebryany6e590e32012-03-14 23:33:24 +0000324 if (BL->isIn(F)) return false;
Kostya Serebryany8b390ff2012-11-29 09:54:21 +0000325 initializeCallbacks(*F.getParent());
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000326 SmallVector<Instruction*, 8> RetVec;
Kostya Serebryany2076af02012-04-10 18:18:56 +0000327 SmallVector<Instruction*, 8> AllLoadsAndStores;
328 SmallVector<Instruction*, 8> LocalLoadsAndStores;
Kostya Serebryanye5079222012-04-27 07:31:53 +0000329 SmallVector<Instruction*, 8> AtomicAccesses;
Kostya Serebryanyf4644812013-03-28 11:21:13 +0000330 SmallVector<Instruction*, 8> MemIntrinCalls;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000331 bool Res = false;
332 bool HasCalls = false;
333
334 // Traverse all instructions, collect loads/stores/returns, check for calls.
335 for (Function::iterator FI = F.begin(), FE = F.end();
336 FI != FE; ++FI) {
337 BasicBlock &BB = *FI;
338 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
339 BI != BE; ++BI) {
Kostya Serebryanye5079222012-04-27 07:31:53 +0000340 if (isAtomic(BI))
341 AtomicAccesses.push_back(BI);
342 else if (isa<LoadInst>(BI) || isa<StoreInst>(BI))
Kostya Serebryany2076af02012-04-10 18:18:56 +0000343 LocalLoadsAndStores.push_back(BI);
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000344 else if (isa<ReturnInst>(BI))
345 RetVec.push_back(BI);
Kostya Serebryany2076af02012-04-10 18:18:56 +0000346 else if (isa<CallInst>(BI) || isa<InvokeInst>(BI)) {
Kostya Serebryanyf4644812013-03-28 11:21:13 +0000347 if (isa<MemIntrinsic>(BI))
348 MemIntrinCalls.push_back(BI);
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000349 HasCalls = true;
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000350 chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
Kostya Serebryany2076af02012-04-10 18:18:56 +0000351 }
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000352 }
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000353 chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000354 }
355
356 // We have collected all loads and stores.
357 // FIXME: many of these accesses do not need to be checked for races
358 // (e.g. variables that do not escape, etc).
359
360 // Instrument memory accesses.
Dmitry Vyukov4ef19992013-10-17 07:20:06 +0000361 if (ClInstrumentMemoryAccesses && F.hasFnAttribute(Attribute::SanitizeThread))
Kostya Serebryany41d876c2012-10-04 05:28:50 +0000362 for (size_t i = 0, n = AllLoadsAndStores.size(); i < n; ++i) {
363 Res |= instrumentLoadOrStore(AllLoadsAndStores[i]);
364 }
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000365
Kostya Serebryanye5079222012-04-27 07:31:53 +0000366 // Instrument atomic memory accesses.
Kostya Serebryany41d876c2012-10-04 05:28:50 +0000367 if (ClInstrumentAtomics)
368 for (size_t i = 0, n = AtomicAccesses.size(); i < n; ++i) {
369 Res |= instrumentAtomic(AtomicAccesses[i]);
370 }
Kostya Serebryanye5079222012-04-27 07:31:53 +0000371
Kostya Serebryanyf4644812013-03-28 11:21:13 +0000372 if (ClInstrumentMemIntrinsics)
373 for (size_t i = 0, n = MemIntrinCalls.size(); i < n; ++i) {
374 Res |= instrumentMemIntrinsic(MemIntrinCalls[i]);
375 }
376
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000377 // Instrument function entry/exit points if there were instrumented accesses.
Kostya Serebryany41d876c2012-10-04 05:28:50 +0000378 if ((Res || HasCalls) && ClInstrumentFuncEntryExit) {
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000379 IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
380 Value *ReturnAddress = IRB.CreateCall(
381 Intrinsic::getDeclaration(F.getParent(), Intrinsic::returnaddress),
382 IRB.getInt32(0));
383 IRB.CreateCall(TsanFuncEntry, ReturnAddress);
384 for (size_t i = 0, n = RetVec.size(); i < n; ++i) {
385 IRBuilder<> IRBRet(RetVec[i]);
386 IRBRet.CreateCall(TsanFuncExit);
387 }
Kostya Serebryany52eb69922012-03-26 17:35:03 +0000388 Res = true;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000389 }
390 return Res;
391}
392
393bool ThreadSanitizer::instrumentLoadOrStore(Instruction *I) {
394 IRBuilder<> IRB(I);
395 bool IsWrite = isa<StoreInst>(*I);
396 Value *Addr = IsWrite
397 ? cast<StoreInst>(I)->getPointerOperand()
398 : cast<LoadInst>(I)->getPointerOperand();
Kostya Serebryanye5079222012-04-27 07:31:53 +0000399 int Idx = getMemoryAccessFuncIndex(Addr);
400 if (Idx < 0)
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000401 return false;
Kostya Serebryany52eb69922012-03-26 17:35:03 +0000402 if (IsWrite && isVtableAccess(I)) {
Kostya Serebryany4a002ab2012-07-05 09:07:31 +0000403 DEBUG(dbgs() << " VPTR : " << *I << "\n");
Kostya Serebryany52eb69922012-03-26 17:35:03 +0000404 Value *StoredValue = cast<StoreInst>(I)->getValueOperand();
Kostya Serebryany4a002ab2012-07-05 09:07:31 +0000405 // StoredValue does not necessary have a pointer type.
406 if (isa<IntegerType>(StoredValue->getType()))
407 StoredValue = IRB.CreateIntToPtr(StoredValue, IRB.getInt8PtrTy());
408 // Call TsanVptrUpdate.
Kostya Serebryany52eb69922012-03-26 17:35:03 +0000409 IRB.CreateCall2(TsanVptrUpdate,
410 IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
411 IRB.CreatePointerCast(StoredValue, IRB.getInt8PtrTy()));
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +0000412 NumInstrumentedVtableWrites++;
Kostya Serebryany52eb69922012-03-26 17:35:03 +0000413 return true;
414 }
Dmitry Vyukovab78ac12013-03-22 08:51:22 +0000415 if (!IsWrite && isVtableAccess(I)) {
416 IRB.CreateCall(TsanVptrLoad,
417 IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
418 NumInstrumentedVtableReads++;
419 return true;
420 }
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000421 Value *OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
422 IRB.CreateCall(OnAccessFunc, IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +0000423 if (IsWrite) NumInstrumentedWrites++;
424 else NumInstrumentedReads++;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000425 return true;
426}
Kostya Serebryanye5079222012-04-27 07:31:53 +0000427
428static ConstantInt *createOrdering(IRBuilder<> *IRB, AtomicOrdering ord) {
429 uint32_t v = 0;
430 switch (ord) {
431 case NotAtomic: assert(false);
432 case Unordered: // Fall-through.
Dmitry Vyukovc2e9ca12012-11-09 14:12:16 +0000433 case Monotonic: v = 0; break;
Dmitry Vyukov9a33f9f2012-11-26 14:55:26 +0000434 // case Consume: v = 1; break; // Not specified yet.
Dmitry Vyukovc2e9ca12012-11-09 14:12:16 +0000435 case Acquire: v = 2; break;
436 case Release: v = 3; break;
437 case AcquireRelease: v = 4; break;
438 case SequentiallyConsistent: v = 5; break;
Kostya Serebryanye5079222012-04-27 07:31:53 +0000439 }
Dmitry Vyukovc2e9ca12012-11-09 14:12:16 +0000440 return IRB->getInt32(v);
Kostya Serebryanye5079222012-04-27 07:31:53 +0000441}
442
Dmitry Vyukov6702e532012-11-26 11:36:19 +0000443static ConstantInt *createFailOrdering(IRBuilder<> *IRB, AtomicOrdering ord) {
444 uint32_t v = 0;
445 switch (ord) {
446 case NotAtomic: assert(false);
447 case Unordered: // Fall-through.
448 case Monotonic: v = 0; break;
Dmitry Vyukov9a33f9f2012-11-26 14:55:26 +0000449 // case Consume: v = 1; break; // Not specified yet.
Dmitry Vyukov6702e532012-11-26 11:36:19 +0000450 case Acquire: v = 2; break;
451 case Release: v = 0; break;
452 case AcquireRelease: v = 2; break;
453 case SequentiallyConsistent: v = 5; break;
454 }
455 return IRB->getInt32(v);
456}
457
Kostya Serebryanyf4644812013-03-28 11:21:13 +0000458// If a memset intrinsic gets inlined by the code gen, we will miss races on it.
459// So, we either need to ensure the intrinsic is not inlined, or instrument it.
460// We do not instrument memset/memmove/memcpy intrinsics (too complicated),
461// instead we simply replace them with regular function calls, which are then
462// intercepted by the run-time.
463// Since tsan is running after everyone else, the calls should not be
464// replaced back with intrinsics. If that becomes wrong at some point,
465// we will need to call e.g. __tsan_memset to avoid the intrinsics.
466bool ThreadSanitizer::instrumentMemIntrinsic(Instruction *I) {
467 IRBuilder<> IRB(I);
468 if (MemSetInst *M = dyn_cast<MemSetInst>(I)) {
469 IRB.CreateCall3(MemsetFn,
470 IRB.CreatePointerCast(M->getArgOperand(0), IRB.getInt8PtrTy()),
471 IRB.CreateIntCast(M->getArgOperand(1), IRB.getInt32Ty(), false),
472 IRB.CreateIntCast(M->getArgOperand(2), IntptrTy, false));
473 I->eraseFromParent();
474 } else if (MemTransferInst *M = dyn_cast<MemTransferInst>(I)) {
475 IRB.CreateCall3(isa<MemCpyInst>(M) ? MemcpyFn : MemmoveFn,
476 IRB.CreatePointerCast(M->getArgOperand(0), IRB.getInt8PtrTy()),
477 IRB.CreatePointerCast(M->getArgOperand(1), IRB.getInt8PtrTy()),
478 IRB.CreateIntCast(M->getArgOperand(2), IntptrTy, false));
479 I->eraseFromParent();
480 }
481 return false;
482}
483
Dmitry Vyukov6702e532012-11-26 11:36:19 +0000484// Both llvm and ThreadSanitizer atomic operations are based on C++11/C1x
485// standards. For background see C++11 standard. A slightly older, publically
486// available draft of the standard (not entirely up-to-date, but close enough
487// for casual browsing) is available here:
Matt Beaumont-Gay70af9092012-11-26 16:27:22 +0000488// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
Dmitry Vyukov6702e532012-11-26 11:36:19 +0000489// The following page contains more background information:
490// http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/
491
Kostya Serebryanye5079222012-04-27 07:31:53 +0000492bool ThreadSanitizer::instrumentAtomic(Instruction *I) {
493 IRBuilder<> IRB(I);
494 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
495 Value *Addr = LI->getPointerOperand();
496 int Idx = getMemoryAccessFuncIndex(Addr);
497 if (Idx < 0)
498 return false;
499 const size_t ByteSize = 1 << Idx;
500 const size_t BitSize = ByteSize * 8;
501 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
Micah Villmowb8bce922012-10-24 17:25:11 +0000502 Type *PtrTy = Ty->getPointerTo();
Kostya Serebryanye5079222012-04-27 07:31:53 +0000503 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
504 createOrdering(&IRB, LI->getOrdering())};
505 CallInst *C = CallInst::Create(TsanAtomicLoad[Idx],
506 ArrayRef<Value*>(Args));
507 ReplaceInstWithInst(I, C);
508
509 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
510 Value *Addr = SI->getPointerOperand();
511 int Idx = getMemoryAccessFuncIndex(Addr);
512 if (Idx < 0)
513 return false;
514 const size_t ByteSize = 1 << Idx;
515 const size_t BitSize = ByteSize * 8;
516 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
Micah Villmowb8bce922012-10-24 17:25:11 +0000517 Type *PtrTy = Ty->getPointerTo();
Kostya Serebryanye5079222012-04-27 07:31:53 +0000518 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
519 IRB.CreateIntCast(SI->getValueOperand(), Ty, false),
520 createOrdering(&IRB, SI->getOrdering())};
521 CallInst *C = CallInst::Create(TsanAtomicStore[Idx],
522 ArrayRef<Value*>(Args));
523 ReplaceInstWithInst(I, C);
Dmitry Vyukov9f8a90b2012-11-09 12:55:36 +0000524 } else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I)) {
525 Value *Addr = RMWI->getPointerOperand();
526 int Idx = getMemoryAccessFuncIndex(Addr);
527 if (Idx < 0)
528 return false;
529 Function *F = TsanAtomicRMW[RMWI->getOperation()][Idx];
530 if (F == NULL)
531 return false;
532 const size_t ByteSize = 1 << Idx;
533 const size_t BitSize = ByteSize * 8;
534 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
535 Type *PtrTy = Ty->getPointerTo();
536 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
537 IRB.CreateIntCast(RMWI->getValOperand(), Ty, false),
538 createOrdering(&IRB, RMWI->getOrdering())};
539 CallInst *C = CallInst::Create(F, ArrayRef<Value*>(Args));
540 ReplaceInstWithInst(I, C);
541 } else if (AtomicCmpXchgInst *CASI = dyn_cast<AtomicCmpXchgInst>(I)) {
542 Value *Addr = CASI->getPointerOperand();
543 int Idx = getMemoryAccessFuncIndex(Addr);
544 if (Idx < 0)
545 return false;
546 const size_t ByteSize = 1 << Idx;
547 const size_t BitSize = ByteSize * 8;
548 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
549 Type *PtrTy = Ty->getPointerTo();
550 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
551 IRB.CreateIntCast(CASI->getCompareOperand(), Ty, false),
552 IRB.CreateIntCast(CASI->getNewValOperand(), Ty, false),
Dmitry Vyukov6702e532012-11-26 11:36:19 +0000553 createOrdering(&IRB, CASI->getOrdering()),
554 createFailOrdering(&IRB, CASI->getOrdering())};
Dmitry Vyukov9f8a90b2012-11-09 12:55:36 +0000555 CallInst *C = CallInst::Create(TsanAtomicCAS[Idx], ArrayRef<Value*>(Args));
556 ReplaceInstWithInst(I, C);
557 } else if (FenceInst *FI = dyn_cast<FenceInst>(I)) {
558 Value *Args[] = {createOrdering(&IRB, FI->getOrdering())};
559 Function *F = FI->getSynchScope() == SingleThread ?
560 TsanAtomicSignalFence : TsanAtomicThreadFence;
561 CallInst *C = CallInst::Create(F, ArrayRef<Value*>(Args));
562 ReplaceInstWithInst(I, C);
Kostya Serebryanye5079222012-04-27 07:31:53 +0000563 }
564 return true;
565}
566
567int ThreadSanitizer::getMemoryAccessFuncIndex(Value *Addr) {
568 Type *OrigPtrTy = Addr->getType();
569 Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
570 assert(OrigTy->isSized());
571 uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
572 if (TypeSize != 8 && TypeSize != 16 &&
573 TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
574 NumAccessesWithBadSize++;
575 // Ignore all unusual sizes.
576 return -1;
577 }
Michael J. Spencerc6af2432013-05-24 22:23:49 +0000578 size_t Idx = countTrailingZeros(TypeSize / 8);
Kostya Serebryanye5079222012-04-27 07:31:53 +0000579 assert(Idx < kNumberOfAccessSizes);
580 return Idx;
581}