blob: cdfaedf47c63d96a6cc8500388310c8deaa2bbe3 [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
Kostya Serebryanyb5b86d22012-08-24 16:44:47 +000024#include "BlackList.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000025#include "llvm/Function.h"
26#include "llvm/IRBuilder.h"
27#include "llvm/Intrinsics.h"
28#include "llvm/LLVMContext.h"
29#include "llvm/Metadata.h"
30#include "llvm/Module.h"
31#include "llvm/Type.h"
Kostya Serebryany2076af02012-04-10 18:18:56 +000032#include "llvm/ADT/SmallSet.h"
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000033#include "llvm/ADT/SmallString.h"
34#include "llvm/ADT/SmallVector.h"
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +000035#include "llvm/ADT/Statistic.h"
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000036#include "llvm/ADT/StringExtras.h"
Kostya Serebryany6e590e32012-03-14 23:33:24 +000037#include "llvm/Support/CommandLine.h"
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000038#include "llvm/Support/Debug.h"
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000039#include "llvm/Support/MathExtras.h"
Kostya Serebryany52eb69922012-03-26 17:35:03 +000040#include "llvm/Support/raw_ostream.h"
Micah Villmow3574eca2012-10-08 16:38:25 +000041#include "llvm/DataLayout.h"
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000042#include "llvm/Transforms/Instrumentation.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"
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000045
46using namespace llvm;
47
Kostya Serebryany6e590e32012-03-14 23:33:24 +000048static cl::opt<std::string> ClBlackListFile("tsan-blacklist",
49 cl::desc("Blacklist file"), cl::Hidden);
Kostya Serebryany41d876c2012-10-04 05:28:50 +000050static cl::opt<bool> ClInstrumentMemoryAccesses(
51 "tsan-instrument-memory-accesses", cl::init(true),
52 cl::desc("Instrument memory accesses"), cl::Hidden);
53static cl::opt<bool> ClInstrumentFuncEntryExit(
54 "tsan-instrument-func-entry-exit", cl::init(true),
55 cl::desc("Instrument function entry and exit"), cl::Hidden);
56static cl::opt<bool> ClInstrumentAtomics(
57 "tsan-instrument-atomics", cl::init(true),
58 cl::desc("Instrument atomics"), cl::Hidden);
Kostya Serebryany6e590e32012-03-14 23:33:24 +000059
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +000060STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
61STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
Alexey Samsonov1dfe9b52012-08-30 13:47:13 +000062STATISTIC(NumOmittedReadsBeforeWrite,
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +000063 "Number of reads ignored due to following writes");
64STATISTIC(NumAccessesWithBadSize, "Number of accesses with bad size");
65STATISTIC(NumInstrumentedVtableWrites, "Number of vtable ptr writes");
66STATISTIC(NumOmittedReadsFromConstantGlobals,
67 "Number of reads from constant globals");
68STATISTIC(NumOmittedReadsFromVtable, "Number of vtable reads");
Kostya Serebryany2076af02012-04-10 18:18:56 +000069
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000070namespace {
Kostya Serebryany2076af02012-04-10 18:18:56 +000071
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000072/// ThreadSanitizer: instrument the code in module to find races.
73struct ThreadSanitizer : public FunctionPass {
74 ThreadSanitizer();
Kostya Serebryanye5079222012-04-27 07:31:53 +000075 const char *getPassName() const;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000076 bool runOnFunction(Function &F);
77 bool doInitialization(Module &M);
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000078 static char ID; // Pass identification, replacement for typeid.
79
80 private:
Kostya Serebryanye5079222012-04-27 07:31:53 +000081 bool instrumentLoadOrStore(Instruction *I);
82 bool instrumentAtomic(Instruction *I);
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +000083 void chooseInstructionsToInstrument(SmallVectorImpl<Instruction*> &Local,
84 SmallVectorImpl<Instruction*> &All);
Kostya Serebryanycff60c12012-04-10 22:29:17 +000085 bool addrPointsToConstantData(Value *Addr);
Kostya Serebryanye5079222012-04-27 07:31:53 +000086 int getMemoryAccessFuncIndex(Value *Addr);
Kostya Serebryany2076af02012-04-10 18:18:56 +000087
Micah Villmow3574eca2012-10-08 16:38:25 +000088 DataLayout *TD;
Kostya Serebryanyb5b86d22012-08-24 16:44:47 +000089 OwningPtr<BlackList> BL;
Kostya Serebryanye5079222012-04-27 07:31:53 +000090 IntegerType *OrdTy;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000091 // Callbacks to run-time library are computed in doInitialization.
Kostya Serebryanye5079222012-04-27 07:31:53 +000092 Function *TsanFuncEntry;
93 Function *TsanFuncExit;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +000094 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
Kostya Serebryany3eccaa62012-02-14 00:52:07 +000095 static const size_t kNumberOfAccessSizes = 5;
Kostya Serebryanye5079222012-04-27 07:31:53 +000096 Function *TsanRead[kNumberOfAccessSizes];
97 Function *TsanWrite[kNumberOfAccessSizes];
98 Function *TsanAtomicLoad[kNumberOfAccessSizes];
99 Function *TsanAtomicStore[kNumberOfAccessSizes];
Dmitry Vyukov9f8a90b2012-11-09 12:55:36 +0000100 Function *TsanAtomicRMW[AtomicRMWInst::LAST_BINOP + 1][kNumberOfAccessSizes];
101 Function *TsanAtomicCAS[kNumberOfAccessSizes];
102 Function *TsanAtomicThreadFence;
103 Function *TsanAtomicSignalFence;
Kostya Serebryanye5079222012-04-27 07:31:53 +0000104 Function *TsanVptrUpdate;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000105};
106} // namespace
107
108char ThreadSanitizer::ID = 0;
109INITIALIZE_PASS(ThreadSanitizer, "tsan",
110 "ThreadSanitizer: detects data races.",
111 false, false)
112
Kostya Serebryanye5079222012-04-27 07:31:53 +0000113const char *ThreadSanitizer::getPassName() const {
114 return "ThreadSanitizer";
115}
116
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000117ThreadSanitizer::ThreadSanitizer()
118 : FunctionPass(ID),
119 TD(NULL) {
120}
121
122FunctionPass *llvm::createThreadSanitizerPass() {
123 return new ThreadSanitizer();
124}
125
Kostya Serebryanye5079222012-04-27 07:31:53 +0000126static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
127 if (Function *F = dyn_cast<Function>(FuncOrBitcast))
128 return F;
129 FuncOrBitcast->dump();
130 report_fatal_error("ThreadSanitizer interface function redefined");
131}
132
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000133bool ThreadSanitizer::doInitialization(Module &M) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000134 TD = getAnalysisIfAvailable<DataLayout>();
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000135 if (!TD)
136 return false;
Kostya Serebryanyb5b86d22012-08-24 16:44:47 +0000137 BL.reset(new BlackList(ClBlackListFile));
Kostya Serebryany6e590e32012-03-14 23:33:24 +0000138
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000139 // Always insert a call to __tsan_init into the module's CTORs.
140 IRBuilder<> IRB(M.getContext());
141 Value *TsanInit = M.getOrInsertFunction("__tsan_init",
142 IRB.getVoidTy(), NULL);
143 appendToGlobalCtors(M, cast<Function>(TsanInit), 0);
144
145 // 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 Vyukov9f8a90b2012-11-09 12:55:36 +0000208 TsanAtomicThreadFence = checkInterfaceFunction(M.getOrInsertFunction(
209 "__tsan_atomic_thread_fence", IRB.getVoidTy(), OrdTy, NULL));
210 TsanAtomicSignalFence = checkInterfaceFunction(M.getOrInsertFunction(
211 "__tsan_atomic_signal_fence", IRB.getVoidTy(), OrdTy, NULL));
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000212 return true;
213}
214
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000215static bool isVtableAccess(Instruction *I) {
216 if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa)) {
217 if (Tag->getNumOperands() < 1) return false;
218 if (MDString *Tag1 = dyn_cast<MDString>(Tag->getOperand(0))) {
219 if (Tag1->getString() == "vtable pointer") return true;
220 }
221 }
222 return false;
223}
224
225bool ThreadSanitizer::addrPointsToConstantData(Value *Addr) {
226 // If this is a GEP, just analyze its pointer operand.
227 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr))
228 Addr = GEP->getPointerOperand();
229
230 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
231 if (GV->isConstant()) {
232 // Reads from constant globals can not race with any writes.
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +0000233 NumOmittedReadsFromConstantGlobals++;
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000234 return true;
235 }
Alexey Samsonov1dfe9b52012-08-30 13:47:13 +0000236 } else if (LoadInst *L = dyn_cast<LoadInst>(Addr)) {
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000237 if (isVtableAccess(L)) {
238 // Reads from a vtable pointer can not race with any writes.
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +0000239 NumOmittedReadsFromVtable++;
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000240 return true;
241 }
242 }
243 return false;
244}
245
Kostya Serebryany2076af02012-04-10 18:18:56 +0000246// Instrumenting some of the accesses may be proven redundant.
247// Currently handled:
248// - read-before-write (within same BB, no calls between)
249//
250// We do not handle some of the patterns that should not survive
251// after the classic compiler optimizations.
252// E.g. two reads from the same temp should be eliminated by CSE,
253// two writes should be eliminated by DSE, etc.
254//
255// 'Local' is a vector of insns within the same BB (no calls between).
256// 'All' is a vector of insns that will be instrumented.
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000257void ThreadSanitizer::chooseInstructionsToInstrument(
Kostya Serebryany2076af02012-04-10 18:18:56 +0000258 SmallVectorImpl<Instruction*> &Local,
259 SmallVectorImpl<Instruction*> &All) {
260 SmallSet<Value*, 8> WriteTargets;
261 // Iterate from the end.
262 for (SmallVectorImpl<Instruction*>::reverse_iterator It = Local.rbegin(),
263 E = Local.rend(); It != E; ++It) {
264 Instruction *I = *It;
265 if (StoreInst *Store = dyn_cast<StoreInst>(I)) {
266 WriteTargets.insert(Store->getPointerOperand());
267 } else {
268 LoadInst *Load = cast<LoadInst>(I);
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000269 Value *Addr = Load->getPointerOperand();
270 if (WriteTargets.count(Addr)) {
Kostya Serebryany2076af02012-04-10 18:18:56 +0000271 // We will write to this temp, so no reason to analyze the read.
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +0000272 NumOmittedReadsBeforeWrite++;
Kostya Serebryany2076af02012-04-10 18:18:56 +0000273 continue;
274 }
Kostya Serebryanycff60c12012-04-10 22:29:17 +0000275 if (addrPointsToConstantData(Addr)) {
276 // Addr points to some constant data -- it can not race with any writes.
277 continue;
278 }
Kostya Serebryany2076af02012-04-10 18:18:56 +0000279 }
280 All.push_back(I);
281 }
282 Local.clear();
283}
284
Kostya Serebryanye5079222012-04-27 07:31:53 +0000285static bool isAtomic(Instruction *I) {
286 if (LoadInst *LI = dyn_cast<LoadInst>(I))
287 return LI->isAtomic() && LI->getSynchScope() == CrossThread;
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000288 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Kostya Serebryanye5079222012-04-27 07:31:53 +0000289 return SI->isAtomic() && SI->getSynchScope() == CrossThread;
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000290 if (isa<AtomicRMWInst>(I))
Kostya Serebryanye5079222012-04-27 07:31:53 +0000291 return true;
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000292 if (isa<AtomicCmpXchgInst>(I))
Kostya Serebryanye5079222012-04-27 07:31:53 +0000293 return true;
Dmitry Vyukov9f8a90b2012-11-09 12:55:36 +0000294 if (isa<FenceInst>(I))
295 return true;
Kostya Serebryanye5079222012-04-27 07:31:53 +0000296 return false;
297}
298
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000299bool ThreadSanitizer::runOnFunction(Function &F) {
300 if (!TD) return false;
Kostya Serebryany6e590e32012-03-14 23:33:24 +0000301 if (BL->isIn(F)) return false;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000302 SmallVector<Instruction*, 8> RetVec;
Kostya Serebryany2076af02012-04-10 18:18:56 +0000303 SmallVector<Instruction*, 8> AllLoadsAndStores;
304 SmallVector<Instruction*, 8> LocalLoadsAndStores;
Kostya Serebryanye5079222012-04-27 07:31:53 +0000305 SmallVector<Instruction*, 8> AtomicAccesses;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000306 bool Res = false;
307 bool HasCalls = false;
308
309 // Traverse all instructions, collect loads/stores/returns, check for calls.
310 for (Function::iterator FI = F.begin(), FE = F.end();
311 FI != FE; ++FI) {
312 BasicBlock &BB = *FI;
313 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
314 BI != BE; ++BI) {
Kostya Serebryanye5079222012-04-27 07:31:53 +0000315 if (isAtomic(BI))
316 AtomicAccesses.push_back(BI);
317 else if (isa<LoadInst>(BI) || isa<StoreInst>(BI))
Kostya Serebryany2076af02012-04-10 18:18:56 +0000318 LocalLoadsAndStores.push_back(BI);
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000319 else if (isa<ReturnInst>(BI))
320 RetVec.push_back(BI);
Kostya Serebryany2076af02012-04-10 18:18:56 +0000321 else if (isa<CallInst>(BI) || isa<InvokeInst>(BI)) {
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000322 HasCalls = true;
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000323 chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
Kostya Serebryany2076af02012-04-10 18:18:56 +0000324 }
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000325 }
Kostya Serebryany37cb9ac2012-05-02 13:12:19 +0000326 chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000327 }
328
329 // We have collected all loads and stores.
330 // FIXME: many of these accesses do not need to be checked for races
331 // (e.g. variables that do not escape, etc).
332
333 // Instrument memory accesses.
Kostya Serebryany41d876c2012-10-04 05:28:50 +0000334 if (ClInstrumentMemoryAccesses)
335 for (size_t i = 0, n = AllLoadsAndStores.size(); i < n; ++i) {
336 Res |= instrumentLoadOrStore(AllLoadsAndStores[i]);
337 }
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000338
Kostya Serebryanye5079222012-04-27 07:31:53 +0000339 // Instrument atomic memory accesses.
Kostya Serebryany41d876c2012-10-04 05:28:50 +0000340 if (ClInstrumentAtomics)
341 for (size_t i = 0, n = AtomicAccesses.size(); i < n; ++i) {
342 Res |= instrumentAtomic(AtomicAccesses[i]);
343 }
Kostya Serebryanye5079222012-04-27 07:31:53 +0000344
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000345 // Instrument function entry/exit points if there were instrumented accesses.
Kostya Serebryany41d876c2012-10-04 05:28:50 +0000346 if ((Res || HasCalls) && ClInstrumentFuncEntryExit) {
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000347 IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
348 Value *ReturnAddress = IRB.CreateCall(
349 Intrinsic::getDeclaration(F.getParent(), Intrinsic::returnaddress),
350 IRB.getInt32(0));
351 IRB.CreateCall(TsanFuncEntry, ReturnAddress);
352 for (size_t i = 0, n = RetVec.size(); i < n; ++i) {
353 IRBuilder<> IRBRet(RetVec[i]);
354 IRBRet.CreateCall(TsanFuncExit);
355 }
Kostya Serebryany52eb69922012-03-26 17:35:03 +0000356 Res = true;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000357 }
358 return Res;
359}
360
361bool ThreadSanitizer::instrumentLoadOrStore(Instruction *I) {
362 IRBuilder<> IRB(I);
363 bool IsWrite = isa<StoreInst>(*I);
364 Value *Addr = IsWrite
365 ? cast<StoreInst>(I)->getPointerOperand()
366 : cast<LoadInst>(I)->getPointerOperand();
Kostya Serebryanye5079222012-04-27 07:31:53 +0000367 int Idx = getMemoryAccessFuncIndex(Addr);
368 if (Idx < 0)
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000369 return false;
Kostya Serebryany52eb69922012-03-26 17:35:03 +0000370 if (IsWrite && isVtableAccess(I)) {
Kostya Serebryany4a002ab2012-07-05 09:07:31 +0000371 DEBUG(dbgs() << " VPTR : " << *I << "\n");
Kostya Serebryany52eb69922012-03-26 17:35:03 +0000372 Value *StoredValue = cast<StoreInst>(I)->getValueOperand();
Kostya Serebryany4a002ab2012-07-05 09:07:31 +0000373 // StoredValue does not necessary have a pointer type.
374 if (isa<IntegerType>(StoredValue->getType()))
375 StoredValue = IRB.CreateIntToPtr(StoredValue, IRB.getInt8PtrTy());
376 // Call TsanVptrUpdate.
Kostya Serebryany52eb69922012-03-26 17:35:03 +0000377 IRB.CreateCall2(TsanVptrUpdate,
378 IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
379 IRB.CreatePointerCast(StoredValue, IRB.getInt8PtrTy()));
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +0000380 NumInstrumentedVtableWrites++;
Kostya Serebryany52eb69922012-03-26 17:35:03 +0000381 return true;
382 }
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000383 Value *OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
384 IRB.CreateCall(OnAccessFunc, IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
Kostya Serebryany2d5fdf82012-04-23 08:44:59 +0000385 if (IsWrite) NumInstrumentedWrites++;
386 else NumInstrumentedReads++;
Kostya Serebryany60ebb1942012-02-13 22:50:51 +0000387 return true;
388}
Kostya Serebryanye5079222012-04-27 07:31:53 +0000389
390static ConstantInt *createOrdering(IRBuilder<> *IRB, AtomicOrdering ord) {
391 uint32_t v = 0;
392 switch (ord) {
393 case NotAtomic: assert(false);
394 case Unordered: // Fall-through.
Dmitry Vyukovc2e9ca12012-11-09 14:12:16 +0000395 case Monotonic: v = 0; break;
Dmitry Vyukov9a33f9f2012-11-26 14:55:26 +0000396 // case Consume: v = 1; break; // Not specified yet.
Dmitry Vyukovc2e9ca12012-11-09 14:12:16 +0000397 case Acquire: v = 2; break;
398 case Release: v = 3; break;
399 case AcquireRelease: v = 4; break;
400 case SequentiallyConsistent: v = 5; break;
Kostya Serebryanye5079222012-04-27 07:31:53 +0000401 }
Dmitry Vyukovc2e9ca12012-11-09 14:12:16 +0000402 return IRB->getInt32(v);
Kostya Serebryanye5079222012-04-27 07:31:53 +0000403}
404
Dmitry Vyukov6702e532012-11-26 11:36:19 +0000405static ConstantInt *createFailOrdering(IRBuilder<> *IRB, AtomicOrdering ord) {
406 uint32_t v = 0;
407 switch (ord) {
408 case NotAtomic: assert(false);
409 case Unordered: // Fall-through.
410 case Monotonic: v = 0; break;
Dmitry Vyukov9a33f9f2012-11-26 14:55:26 +0000411 // case Consume: v = 1; break; // Not specified yet.
Dmitry Vyukov6702e532012-11-26 11:36:19 +0000412 case Acquire: v = 2; break;
413 case Release: v = 0; break;
414 case AcquireRelease: v = 2; break;
415 case SequentiallyConsistent: v = 5; break;
416 }
417 return IRB->getInt32(v);
418}
419
420// Both llvm and ThreadSanitizer atomic operations are based on C++11/C1x
421// standards. For background see C++11 standard. A slightly older, publically
422// available draft of the standard (not entirely up-to-date, but close enough
423// for casual browsing) is available here:
Matt Beaumont-Gay70af9092012-11-26 16:27:22 +0000424// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
Dmitry Vyukov6702e532012-11-26 11:36:19 +0000425// The following page contains more background information:
426// http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/
427
Kostya Serebryanye5079222012-04-27 07:31:53 +0000428bool ThreadSanitizer::instrumentAtomic(Instruction *I) {
429 IRBuilder<> IRB(I);
430 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
431 Value *Addr = LI->getPointerOperand();
432 int Idx = getMemoryAccessFuncIndex(Addr);
433 if (Idx < 0)
434 return false;
435 const size_t ByteSize = 1 << Idx;
436 const size_t BitSize = ByteSize * 8;
437 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
Micah Villmowb8bce922012-10-24 17:25:11 +0000438 Type *PtrTy = Ty->getPointerTo();
Kostya Serebryanye5079222012-04-27 07:31:53 +0000439 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
440 createOrdering(&IRB, LI->getOrdering())};
441 CallInst *C = CallInst::Create(TsanAtomicLoad[Idx],
442 ArrayRef<Value*>(Args));
443 ReplaceInstWithInst(I, C);
444
445 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
446 Value *Addr = SI->getPointerOperand();
447 int Idx = getMemoryAccessFuncIndex(Addr);
448 if (Idx < 0)
449 return false;
450 const size_t ByteSize = 1 << Idx;
451 const size_t BitSize = ByteSize * 8;
452 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
Micah Villmowb8bce922012-10-24 17:25:11 +0000453 Type *PtrTy = Ty->getPointerTo();
Kostya Serebryanye5079222012-04-27 07:31:53 +0000454 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
455 IRB.CreateIntCast(SI->getValueOperand(), Ty, false),
456 createOrdering(&IRB, SI->getOrdering())};
457 CallInst *C = CallInst::Create(TsanAtomicStore[Idx],
458 ArrayRef<Value*>(Args));
459 ReplaceInstWithInst(I, C);
Dmitry Vyukov9f8a90b2012-11-09 12:55:36 +0000460 } else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I)) {
461 Value *Addr = RMWI->getPointerOperand();
462 int Idx = getMemoryAccessFuncIndex(Addr);
463 if (Idx < 0)
464 return false;
465 Function *F = TsanAtomicRMW[RMWI->getOperation()][Idx];
466 if (F == NULL)
467 return false;
468 const size_t ByteSize = 1 << Idx;
469 const size_t BitSize = ByteSize * 8;
470 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
471 Type *PtrTy = Ty->getPointerTo();
472 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
473 IRB.CreateIntCast(RMWI->getValOperand(), Ty, false),
474 createOrdering(&IRB, RMWI->getOrdering())};
475 CallInst *C = CallInst::Create(F, ArrayRef<Value*>(Args));
476 ReplaceInstWithInst(I, C);
477 } else if (AtomicCmpXchgInst *CASI = dyn_cast<AtomicCmpXchgInst>(I)) {
478 Value *Addr = CASI->getPointerOperand();
479 int Idx = getMemoryAccessFuncIndex(Addr);
480 if (Idx < 0)
481 return false;
482 const size_t ByteSize = 1 << Idx;
483 const size_t BitSize = ByteSize * 8;
484 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
485 Type *PtrTy = Ty->getPointerTo();
486 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
487 IRB.CreateIntCast(CASI->getCompareOperand(), Ty, false),
488 IRB.CreateIntCast(CASI->getNewValOperand(), Ty, false),
Dmitry Vyukov6702e532012-11-26 11:36:19 +0000489 createOrdering(&IRB, CASI->getOrdering()),
490 createFailOrdering(&IRB, CASI->getOrdering())};
Dmitry Vyukov9f8a90b2012-11-09 12:55:36 +0000491 CallInst *C = CallInst::Create(TsanAtomicCAS[Idx], ArrayRef<Value*>(Args));
492 ReplaceInstWithInst(I, C);
493 } else if (FenceInst *FI = dyn_cast<FenceInst>(I)) {
494 Value *Args[] = {createOrdering(&IRB, FI->getOrdering())};
495 Function *F = FI->getSynchScope() == SingleThread ?
496 TsanAtomicSignalFence : TsanAtomicThreadFence;
497 CallInst *C = CallInst::Create(F, ArrayRef<Value*>(Args));
498 ReplaceInstWithInst(I, C);
Kostya Serebryanye5079222012-04-27 07:31:53 +0000499 }
500 return true;
501}
502
503int ThreadSanitizer::getMemoryAccessFuncIndex(Value *Addr) {
504 Type *OrigPtrTy = Addr->getType();
505 Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
506 assert(OrigTy->isSized());
507 uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
508 if (TypeSize != 8 && TypeSize != 16 &&
509 TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
510 NumAccessesWithBadSize++;
511 // Ignore all unusual sizes.
512 return -1;
513 }
514 size_t Idx = CountTrailingZeros_32(TypeSize / 8);
515 assert(Idx < kNumberOfAccessSizes);
516 return Idx;
517}