blob: 417f2a1649b6517c3d0f70e9eedd86aa920cc9cd [file] [log] [blame]
Kostya Serebryanye2a0e412012-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
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Transforms/Instrumentation.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/ADT/SmallSet.h"
24#include "llvm/ADT/SmallString.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/StringExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/IRBuilder.h"
Kostya Serebryany463aa812013-03-28 11:21:13 +000031#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Intrinsics.h"
33#include "llvm/IR/LLVMContext.h"
34#include "llvm/IR/Metadata.h"
35#include "llvm/IR/Module.h"
36#include "llvm/IR/Type.h"
Kostya Serebryanyabad0022012-03-14 23:33:24 +000037#include "llvm/Support/CommandLine.h"
Kostya Serebryanye2a0e412012-02-13 22:50:51 +000038#include "llvm/Support/Debug.h"
Kostya Serebryanye2a0e412012-02-13 22:50:51 +000039#include "llvm/Support/MathExtras.h"
Kostya Serebryany6f8a7762012-03-26 17:35:03 +000040#include "llvm/Support/raw_ostream.h"
Kostya Serebryanya1259772012-04-27 07:31:53 +000041#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Kostya Serebryanye2a0e412012-02-13 22:50:51 +000042#include "llvm/Transforms/Utils/ModuleUtils.h"
Kostya Serebryanye2a0e412012-02-13 22:50:51 +000043
44using namespace llvm;
45
Chandler Carruth964daaa2014-04-22 02:55:47 +000046#define DEBUG_TYPE "tsan"
47
Kostya Serebryanyd23b18f2012-10-04 05:28:50 +000048static cl::opt<bool> ClInstrumentMemoryAccesses(
49 "tsan-instrument-memory-accesses", cl::init(true),
50 cl::desc("Instrument memory accesses"), cl::Hidden);
51static cl::opt<bool> ClInstrumentFuncEntryExit(
52 "tsan-instrument-func-entry-exit", cl::init(true),
53 cl::desc("Instrument function entry and exit"), cl::Hidden);
54static cl::opt<bool> ClInstrumentAtomics(
55 "tsan-instrument-atomics", cl::init(true),
56 cl::desc("Instrument atomics"), cl::Hidden);
Kostya Serebryany463aa812013-03-28 11:21:13 +000057static cl::opt<bool> ClInstrumentMemIntrinsics(
58 "tsan-instrument-memintrinsics", cl::init(true),
59 cl::desc("Instrument memintrinsics (memset/memcpy/memmove)"), cl::Hidden);
Kostya Serebryanyabad0022012-03-14 23:33:24 +000060
Kostya Serebryany5a4b7a22012-04-23 08:44:59 +000061STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
62STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
Alexey Samsonovf54e3aa2012-08-30 13:47:13 +000063STATISTIC(NumOmittedReadsBeforeWrite,
Kostya Serebryany5a4b7a22012-04-23 08:44:59 +000064 "Number of reads ignored due to following writes");
65STATISTIC(NumAccessesWithBadSize, "Number of accesses with bad size");
66STATISTIC(NumInstrumentedVtableWrites, "Number of vtable ptr writes");
Dmitry Vyukov55e63ef2013-03-22 08:51:22 +000067STATISTIC(NumInstrumentedVtableReads, "Number of vtable ptr reads");
Kostya Serebryany5a4b7a22012-04-23 08:44:59 +000068STATISTIC(NumOmittedReadsFromConstantGlobals,
69 "Number of reads from constant globals");
70STATISTIC(NumOmittedReadsFromVtable, "Number of vtable reads");
Kostya Serebryanybf2de802012-04-10 18:18:56 +000071
Kostya Serebryanye2a0e412012-02-13 22:50:51 +000072namespace {
Kostya Serebryanybf2de802012-04-10 18:18:56 +000073
Kostya Serebryanye2a0e412012-02-13 22:50:51 +000074/// ThreadSanitizer: instrument the code in module to find races.
75struct ThreadSanitizer : public FunctionPass {
Alexey Samsonov6d8bab82014-06-02 18:08:27 +000076 ThreadSanitizer() : FunctionPass(ID), DL(nullptr) {}
Craig Topper3e4c6972014-03-05 09:10:37 +000077 const char *getPassName() const override;
78 bool runOnFunction(Function &F) override;
79 bool doInitialization(Module &M) override;
Kostya Serebryanye2a0e412012-02-13 22:50:51 +000080 static char ID; // Pass identification, replacement for typeid.
81
82 private:
Kostya Serebryany4b929da2012-11-29 09:54:21 +000083 void initializeCallbacks(Module &M);
Kostya Serebryanya1259772012-04-27 07:31:53 +000084 bool instrumentLoadOrStore(Instruction *I);
85 bool instrumentAtomic(Instruction *I);
Kostya Serebryany463aa812013-03-28 11:21:13 +000086 bool instrumentMemIntrinsic(Instruction *I);
Kostya Serebryanyae7188d2012-05-02 13:12:19 +000087 void chooseInstructionsToInstrument(SmallVectorImpl<Instruction*> &Local,
88 SmallVectorImpl<Instruction*> &All);
Kostya Serebryany5ba61ac2012-04-10 22:29:17 +000089 bool addrPointsToConstantData(Value *Addr);
Kostya Serebryanya1259772012-04-27 07:31:53 +000090 int getMemoryAccessFuncIndex(Value *Addr);
Kostya Serebryanybf2de802012-04-10 18:18:56 +000091
Rafael Espindolaaeff8a92014-02-24 23:12:18 +000092 const DataLayout *DL;
Kostya Serebryany463aa812013-03-28 11:21:13 +000093 Type *IntptrTy;
Kostya Serebryanya1259772012-04-27 07:31:53 +000094 IntegerType *OrdTy;
Kostya Serebryanye2a0e412012-02-13 22:50:51 +000095 // Callbacks to run-time library are computed in doInitialization.
Kostya Serebryanya1259772012-04-27 07:31:53 +000096 Function *TsanFuncEntry;
97 Function *TsanFuncExit;
Kostya Serebryanye2a0e412012-02-13 22:50:51 +000098 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
Kostya Serebryanya8531ee2012-02-14 00:52:07 +000099 static const size_t kNumberOfAccessSizes = 5;
Kostya Serebryanya1259772012-04-27 07:31:53 +0000100 Function *TsanRead[kNumberOfAccessSizes];
101 Function *TsanWrite[kNumberOfAccessSizes];
102 Function *TsanAtomicLoad[kNumberOfAccessSizes];
103 Function *TsanAtomicStore[kNumberOfAccessSizes];
Dmitry Vyukov92b9e1d2012-11-09 12:55:36 +0000104 Function *TsanAtomicRMW[AtomicRMWInst::LAST_BINOP + 1][kNumberOfAccessSizes];
105 Function *TsanAtomicCAS[kNumberOfAccessSizes];
106 Function *TsanAtomicThreadFence;
107 Function *TsanAtomicSignalFence;
Kostya Serebryanya1259772012-04-27 07:31:53 +0000108 Function *TsanVptrUpdate;
Dmitry Vyukov55e63ef2013-03-22 08:51:22 +0000109 Function *TsanVptrLoad;
Kostya Serebryany463aa812013-03-28 11:21:13 +0000110 Function *MemmoveFn, *MemcpyFn, *MemsetFn;
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000111};
112} // namespace
113
114char ThreadSanitizer::ID = 0;
115INITIALIZE_PASS(ThreadSanitizer, "tsan",
116 "ThreadSanitizer: detects data races.",
117 false, false)
118
Kostya Serebryanya1259772012-04-27 07:31:53 +0000119const char *ThreadSanitizer::getPassName() const {
120 return "ThreadSanitizer";
121}
122
Alexey Samsonov6d8bab82014-06-02 18:08:27 +0000123FunctionPass *llvm::createThreadSanitizerPass() {
124 return new ThreadSanitizer();
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000125}
126
Kostya Serebryanya1259772012-04-27 07:31:53 +0000127static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
128 if (Function *F = dyn_cast<Function>(FuncOrBitcast))
129 return F;
130 FuncOrBitcast->dump();
131 report_fatal_error("ThreadSanitizer interface function redefined");
132}
133
Kostya Serebryany4b929da2012-11-29 09:54:21 +0000134void ThreadSanitizer::initializeCallbacks(Module &M) {
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000135 IRBuilder<> IRB(M.getContext());
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000136 // Initialize the callbacks.
Kostya Serebryanya1259772012-04-27 07:31:53 +0000137 TsanFuncEntry = checkInterfaceFunction(M.getOrInsertFunction(
138 "__tsan_func_entry", IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
139 TsanFuncExit = checkInterfaceFunction(M.getOrInsertFunction(
140 "__tsan_func_exit", IRB.getVoidTy(), NULL));
141 OrdTy = IRB.getInt32Ty();
Kostya Serebryanya8531ee2012-02-14 00:52:07 +0000142 for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
Kostya Serebryanya1259772012-04-27 07:31:53 +0000143 const size_t ByteSize = 1 << i;
144 const size_t BitSize = ByteSize * 8;
145 SmallString<32> ReadName("__tsan_read" + itostr(ByteSize));
146 TsanRead[i] = checkInterfaceFunction(M.getOrInsertFunction(
147 ReadName, IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
148
149 SmallString<32> WriteName("__tsan_write" + itostr(ByteSize));
150 TsanWrite[i] = checkInterfaceFunction(M.getOrInsertFunction(
151 WriteName, IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
152
153 Type *Ty = Type::getIntNTy(M.getContext(), BitSize);
154 Type *PtrTy = Ty->getPointerTo();
155 SmallString<32> AtomicLoadName("__tsan_atomic" + itostr(BitSize) +
156 "_load");
157 TsanAtomicLoad[i] = checkInterfaceFunction(M.getOrInsertFunction(
158 AtomicLoadName, Ty, PtrTy, OrdTy, NULL));
159
160 SmallString<32> AtomicStoreName("__tsan_atomic" + itostr(BitSize) +
161 "_store");
162 TsanAtomicStore[i] = checkInterfaceFunction(M.getOrInsertFunction(
163 AtomicStoreName, IRB.getVoidTy(), PtrTy, Ty, OrdTy,
164 NULL));
Dmitry Vyukov92b9e1d2012-11-09 12:55:36 +0000165
166 for (int op = AtomicRMWInst::FIRST_BINOP;
167 op <= AtomicRMWInst::LAST_BINOP; ++op) {
Craig Topperf40110f2014-04-25 05:29:35 +0000168 TsanAtomicRMW[op][i] = nullptr;
169 const char *NamePart = nullptr;
Dmitry Vyukov92b9e1d2012-11-09 12:55:36 +0000170 if (op == AtomicRMWInst::Xchg)
171 NamePart = "_exchange";
172 else if (op == AtomicRMWInst::Add)
173 NamePart = "_fetch_add";
174 else if (op == AtomicRMWInst::Sub)
175 NamePart = "_fetch_sub";
176 else if (op == AtomicRMWInst::And)
177 NamePart = "_fetch_and";
178 else if (op == AtomicRMWInst::Or)
179 NamePart = "_fetch_or";
180 else if (op == AtomicRMWInst::Xor)
181 NamePart = "_fetch_xor";
Dmitry Vyukova878e742012-11-27 08:09:25 +0000182 else if (op == AtomicRMWInst::Nand)
183 NamePart = "_fetch_nand";
Dmitry Vyukov92b9e1d2012-11-09 12:55:36 +0000184 else
185 continue;
186 SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart);
187 TsanAtomicRMW[op][i] = checkInterfaceFunction(M.getOrInsertFunction(
188 RMWName, Ty, PtrTy, Ty, OrdTy, NULL));
189 }
190
191 SmallString<32> AtomicCASName("__tsan_atomic" + itostr(BitSize) +
192 "_compare_exchange_val");
193 TsanAtomicCAS[i] = checkInterfaceFunction(M.getOrInsertFunction(
Dmitry Vyukov12b5cb92012-11-26 11:36:19 +0000194 AtomicCASName, Ty, PtrTy, Ty, Ty, OrdTy, OrdTy, NULL));
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000195 }
Kostya Serebryanya1259772012-04-27 07:31:53 +0000196 TsanVptrUpdate = checkInterfaceFunction(M.getOrInsertFunction(
197 "__tsan_vptr_update", IRB.getVoidTy(), IRB.getInt8PtrTy(),
198 IRB.getInt8PtrTy(), NULL));
Dmitry Vyukov55e63ef2013-03-22 08:51:22 +0000199 TsanVptrLoad = checkInterfaceFunction(M.getOrInsertFunction(
200 "__tsan_vptr_read", IRB.getVoidTy(), IRB.getInt8PtrTy(), NULL));
Dmitry Vyukov92b9e1d2012-11-09 12:55:36 +0000201 TsanAtomicThreadFence = checkInterfaceFunction(M.getOrInsertFunction(
202 "__tsan_atomic_thread_fence", IRB.getVoidTy(), OrdTy, NULL));
203 TsanAtomicSignalFence = checkInterfaceFunction(M.getOrInsertFunction(
204 "__tsan_atomic_signal_fence", IRB.getVoidTy(), OrdTy, NULL));
Kostya Serebryany463aa812013-03-28 11:21:13 +0000205
206 MemmoveFn = checkInterfaceFunction(M.getOrInsertFunction(
207 "memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
208 IRB.getInt8PtrTy(), IntptrTy, NULL));
209 MemcpyFn = checkInterfaceFunction(M.getOrInsertFunction(
210 "memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
211 IntptrTy, NULL));
212 MemsetFn = checkInterfaceFunction(M.getOrInsertFunction(
213 "memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(),
214 IntptrTy, NULL));
Kostya Serebryany4b929da2012-11-29 09:54:21 +0000215}
216
217bool ThreadSanitizer::doInitialization(Module &M) {
Rafael Espindola93512512014-02-25 17:30:31 +0000218 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
219 if (!DLP)
Evgeniy Stepanov119cb2e2014-04-23 12:51:32 +0000220 report_fatal_error("data layout missing");
Rafael Espindola93512512014-02-25 17:30:31 +0000221 DL = &DLP->getDataLayout();
Kostya Serebryany4b929da2012-11-29 09:54:21 +0000222
223 // Always insert a call to __tsan_init into the module's CTORs.
224 IRBuilder<> IRB(M.getContext());
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000225 IntptrTy = IRB.getIntPtrTy(DL);
Kostya Serebryany4b929da2012-11-29 09:54:21 +0000226 Value *TsanInit = M.getOrInsertFunction("__tsan_init",
227 IRB.getVoidTy(), NULL);
228 appendToGlobalCtors(M, cast<Function>(TsanInit), 0);
229
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000230 return true;
231}
232
Kostya Serebryany5ba61ac2012-04-10 22:29:17 +0000233static bool isVtableAccess(Instruction *I) {
Manman Rend8c68b12013-09-06 22:47:05 +0000234 if (MDNode *Tag = I->getMetadata(LLVMContext::MD_tbaa))
235 return Tag->isTBAAVtableAccess();
Kostya Serebryany5ba61ac2012-04-10 22:29:17 +0000236 return false;
237}
238
239bool ThreadSanitizer::addrPointsToConstantData(Value *Addr) {
240 // If this is a GEP, just analyze its pointer operand.
241 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Addr))
242 Addr = GEP->getPointerOperand();
243
244 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
245 if (GV->isConstant()) {
246 // Reads from constant globals can not race with any writes.
Kostya Serebryany5a4b7a22012-04-23 08:44:59 +0000247 NumOmittedReadsFromConstantGlobals++;
Kostya Serebryany5ba61ac2012-04-10 22:29:17 +0000248 return true;
249 }
Alexey Samsonovf54e3aa2012-08-30 13:47:13 +0000250 } else if (LoadInst *L = dyn_cast<LoadInst>(Addr)) {
Kostya Serebryany5ba61ac2012-04-10 22:29:17 +0000251 if (isVtableAccess(L)) {
252 // Reads from a vtable pointer can not race with any writes.
Kostya Serebryany5a4b7a22012-04-23 08:44:59 +0000253 NumOmittedReadsFromVtable++;
Kostya Serebryany5ba61ac2012-04-10 22:29:17 +0000254 return true;
255 }
256 }
257 return false;
258}
259
Kostya Serebryanybf2de802012-04-10 18:18:56 +0000260// Instrumenting some of the accesses may be proven redundant.
261// Currently handled:
262// - read-before-write (within same BB, no calls between)
263//
264// We do not handle some of the patterns that should not survive
265// after the classic compiler optimizations.
266// E.g. two reads from the same temp should be eliminated by CSE,
267// two writes should be eliminated by DSE, etc.
268//
269// 'Local' is a vector of insns within the same BB (no calls between).
270// 'All' is a vector of insns that will be instrumented.
Kostya Serebryanyae7188d2012-05-02 13:12:19 +0000271void ThreadSanitizer::chooseInstructionsToInstrument(
Kostya Serebryanybf2de802012-04-10 18:18:56 +0000272 SmallVectorImpl<Instruction*> &Local,
273 SmallVectorImpl<Instruction*> &All) {
274 SmallSet<Value*, 8> WriteTargets;
275 // Iterate from the end.
276 for (SmallVectorImpl<Instruction*>::reverse_iterator It = Local.rbegin(),
277 E = Local.rend(); It != E; ++It) {
278 Instruction *I = *It;
279 if (StoreInst *Store = dyn_cast<StoreInst>(I)) {
280 WriteTargets.insert(Store->getPointerOperand());
281 } else {
282 LoadInst *Load = cast<LoadInst>(I);
Kostya Serebryany5ba61ac2012-04-10 22:29:17 +0000283 Value *Addr = Load->getPointerOperand();
284 if (WriteTargets.count(Addr)) {
Kostya Serebryanybf2de802012-04-10 18:18:56 +0000285 // We will write to this temp, so no reason to analyze the read.
Kostya Serebryany5a4b7a22012-04-23 08:44:59 +0000286 NumOmittedReadsBeforeWrite++;
Kostya Serebryanybf2de802012-04-10 18:18:56 +0000287 continue;
288 }
Kostya Serebryany5ba61ac2012-04-10 22:29:17 +0000289 if (addrPointsToConstantData(Addr)) {
290 // Addr points to some constant data -- it can not race with any writes.
291 continue;
292 }
Kostya Serebryanybf2de802012-04-10 18:18:56 +0000293 }
294 All.push_back(I);
295 }
296 Local.clear();
297}
298
Kostya Serebryanya1259772012-04-27 07:31:53 +0000299static bool isAtomic(Instruction *I) {
300 if (LoadInst *LI = dyn_cast<LoadInst>(I))
301 return LI->isAtomic() && LI->getSynchScope() == CrossThread;
Kostya Serebryanyae7188d2012-05-02 13:12:19 +0000302 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Kostya Serebryanya1259772012-04-27 07:31:53 +0000303 return SI->isAtomic() && SI->getSynchScope() == CrossThread;
Kostya Serebryanyae7188d2012-05-02 13:12:19 +0000304 if (isa<AtomicRMWInst>(I))
Kostya Serebryanya1259772012-04-27 07:31:53 +0000305 return true;
Kostya Serebryanyae7188d2012-05-02 13:12:19 +0000306 if (isa<AtomicCmpXchgInst>(I))
Kostya Serebryanya1259772012-04-27 07:31:53 +0000307 return true;
Dmitry Vyukov92b9e1d2012-11-09 12:55:36 +0000308 if (isa<FenceInst>(I))
309 return true;
Kostya Serebryanya1259772012-04-27 07:31:53 +0000310 return false;
311}
312
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000313bool ThreadSanitizer::runOnFunction(Function &F) {
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000314 if (!DL) return false;
Kostya Serebryany4b929da2012-11-29 09:54:21 +0000315 initializeCallbacks(*F.getParent());
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000316 SmallVector<Instruction*, 8> RetVec;
Kostya Serebryanybf2de802012-04-10 18:18:56 +0000317 SmallVector<Instruction*, 8> AllLoadsAndStores;
318 SmallVector<Instruction*, 8> LocalLoadsAndStores;
Kostya Serebryanya1259772012-04-27 07:31:53 +0000319 SmallVector<Instruction*, 8> AtomicAccesses;
Kostya Serebryany463aa812013-03-28 11:21:13 +0000320 SmallVector<Instruction*, 8> MemIntrinCalls;
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000321 bool Res = false;
322 bool HasCalls = false;
Alexey Samsonov6d8bab82014-06-02 18:08:27 +0000323 bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeThread);
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000324
325 // Traverse all instructions, collect loads/stores/returns, check for calls.
Alexey Samsonova02e6642014-05-29 18:40:48 +0000326 for (auto &BB : F) {
327 for (auto &Inst : BB) {
328 if (isAtomic(&Inst))
329 AtomicAccesses.push_back(&Inst);
330 else if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
331 LocalLoadsAndStores.push_back(&Inst);
332 else if (isa<ReturnInst>(Inst))
333 RetVec.push_back(&Inst);
334 else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
335 if (isa<MemIntrinsic>(Inst))
336 MemIntrinCalls.push_back(&Inst);
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000337 HasCalls = true;
Kostya Serebryanyae7188d2012-05-02 13:12:19 +0000338 chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
Kostya Serebryanybf2de802012-04-10 18:18:56 +0000339 }
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000340 }
Kostya Serebryanyae7188d2012-05-02 13:12:19 +0000341 chooseInstructionsToInstrument(LocalLoadsAndStores, AllLoadsAndStores);
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000342 }
343
344 // We have collected all loads and stores.
345 // FIXME: many of these accesses do not need to be checked for races
346 // (e.g. variables that do not escape, etc).
347
Alexey Samsonovd3828b82014-05-31 00:11:37 +0000348 // Instrument memory accesses only if we want to report bugs in the function.
349 if (ClInstrumentMemoryAccesses && SanitizeFunction)
Alexey Samsonova02e6642014-05-29 18:40:48 +0000350 for (auto Inst : AllLoadsAndStores) {
351 Res |= instrumentLoadOrStore(Inst);
Kostya Serebryanyd23b18f2012-10-04 05:28:50 +0000352 }
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000353
Alexey Samsonovd3828b82014-05-31 00:11:37 +0000354 // Instrument atomic memory accesses in any case (they can be used to
355 // implement synchronization).
Kostya Serebryanyd23b18f2012-10-04 05:28:50 +0000356 if (ClInstrumentAtomics)
Alexey Samsonova02e6642014-05-29 18:40:48 +0000357 for (auto Inst : AtomicAccesses) {
358 Res |= instrumentAtomic(Inst);
Kostya Serebryanyd23b18f2012-10-04 05:28:50 +0000359 }
Kostya Serebryanya1259772012-04-27 07:31:53 +0000360
Alexey Samsonovd3828b82014-05-31 00:11:37 +0000361 if (ClInstrumentMemIntrinsics && SanitizeFunction)
Alexey Samsonova02e6642014-05-29 18:40:48 +0000362 for (auto Inst : MemIntrinCalls) {
363 Res |= instrumentMemIntrinsic(Inst);
Kostya Serebryany463aa812013-03-28 11:21:13 +0000364 }
365
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000366 // Instrument function entry/exit points if there were instrumented accesses.
Kostya Serebryanyd23b18f2012-10-04 05:28:50 +0000367 if ((Res || HasCalls) && ClInstrumentFuncEntryExit) {
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000368 IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
369 Value *ReturnAddress = IRB.CreateCall(
370 Intrinsic::getDeclaration(F.getParent(), Intrinsic::returnaddress),
371 IRB.getInt32(0));
372 IRB.CreateCall(TsanFuncEntry, ReturnAddress);
Alexey Samsonova02e6642014-05-29 18:40:48 +0000373 for (auto RetInst : RetVec) {
374 IRBuilder<> IRBRet(RetInst);
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000375 IRBRet.CreateCall(TsanFuncExit);
376 }
Kostya Serebryany6f8a7762012-03-26 17:35:03 +0000377 Res = true;
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000378 }
379 return Res;
380}
381
382bool ThreadSanitizer::instrumentLoadOrStore(Instruction *I) {
383 IRBuilder<> IRB(I);
384 bool IsWrite = isa<StoreInst>(*I);
385 Value *Addr = IsWrite
386 ? cast<StoreInst>(I)->getPointerOperand()
387 : cast<LoadInst>(I)->getPointerOperand();
Kostya Serebryanya1259772012-04-27 07:31:53 +0000388 int Idx = getMemoryAccessFuncIndex(Addr);
389 if (Idx < 0)
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000390 return false;
Kostya Serebryany6f8a7762012-03-26 17:35:03 +0000391 if (IsWrite && isVtableAccess(I)) {
Kostya Serebryanye36ae682012-07-05 09:07:31 +0000392 DEBUG(dbgs() << " VPTR : " << *I << "\n");
Kostya Serebryany6f8a7762012-03-26 17:35:03 +0000393 Value *StoredValue = cast<StoreInst>(I)->getValueOperand();
Kostya Serebryany08b9cf52013-12-02 08:07:15 +0000394 // StoredValue may be a vector type if we are storing several vptrs at once.
395 // In this case, just take the first element of the vector since this is
396 // enough to find vptr races.
397 if (isa<VectorType>(StoredValue->getType()))
398 StoredValue = IRB.CreateExtractElement(
399 StoredValue, ConstantInt::get(IRB.getInt32Ty(), 0));
Kostya Serebryany2460c3f2013-12-05 15:03:02 +0000400 if (StoredValue->getType()->isIntegerTy())
401 StoredValue = IRB.CreateIntToPtr(StoredValue, IRB.getInt8PtrTy());
Kostya Serebryanye36ae682012-07-05 09:07:31 +0000402 // Call TsanVptrUpdate.
Kostya Serebryany6f8a7762012-03-26 17:35:03 +0000403 IRB.CreateCall2(TsanVptrUpdate,
404 IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
Kostya Serebryany2460c3f2013-12-05 15:03:02 +0000405 IRB.CreatePointerCast(StoredValue, IRB.getInt8PtrTy()));
Kostya Serebryany5a4b7a22012-04-23 08:44:59 +0000406 NumInstrumentedVtableWrites++;
Kostya Serebryany6f8a7762012-03-26 17:35:03 +0000407 return true;
408 }
Dmitry Vyukov55e63ef2013-03-22 08:51:22 +0000409 if (!IsWrite && isVtableAccess(I)) {
410 IRB.CreateCall(TsanVptrLoad,
411 IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
412 NumInstrumentedVtableReads++;
413 return true;
414 }
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000415 Value *OnAccessFunc = IsWrite ? TsanWrite[Idx] : TsanRead[Idx];
416 IRB.CreateCall(OnAccessFunc, IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
Kostya Serebryany5a4b7a22012-04-23 08:44:59 +0000417 if (IsWrite) NumInstrumentedWrites++;
418 else NumInstrumentedReads++;
Kostya Serebryanye2a0e412012-02-13 22:50:51 +0000419 return true;
420}
Kostya Serebryanya1259772012-04-27 07:31:53 +0000421
422static ConstantInt *createOrdering(IRBuilder<> *IRB, AtomicOrdering ord) {
423 uint32_t v = 0;
424 switch (ord) {
425 case NotAtomic: assert(false);
426 case Unordered: // Fall-through.
Dmitry Vyukov0044e382012-11-09 14:12:16 +0000427 case Monotonic: v = 0; break;
Dmitry Vyukov93e67b62012-11-26 14:55:26 +0000428 // case Consume: v = 1; break; // Not specified yet.
Dmitry Vyukov0044e382012-11-09 14:12:16 +0000429 case Acquire: v = 2; break;
430 case Release: v = 3; break;
431 case AcquireRelease: v = 4; break;
432 case SequentiallyConsistent: v = 5; break;
Kostya Serebryanya1259772012-04-27 07:31:53 +0000433 }
Dmitry Vyukov0044e382012-11-09 14:12:16 +0000434 return IRB->getInt32(v);
Kostya Serebryanya1259772012-04-27 07:31:53 +0000435}
436
Kostya Serebryany463aa812013-03-28 11:21:13 +0000437// If a memset intrinsic gets inlined by the code gen, we will miss races on it.
438// So, we either need to ensure the intrinsic is not inlined, or instrument it.
439// We do not instrument memset/memmove/memcpy intrinsics (too complicated),
440// instead we simply replace them with regular function calls, which are then
441// intercepted by the run-time.
442// Since tsan is running after everyone else, the calls should not be
443// replaced back with intrinsics. If that becomes wrong at some point,
444// we will need to call e.g. __tsan_memset to avoid the intrinsics.
445bool ThreadSanitizer::instrumentMemIntrinsic(Instruction *I) {
446 IRBuilder<> IRB(I);
447 if (MemSetInst *M = dyn_cast<MemSetInst>(I)) {
448 IRB.CreateCall3(MemsetFn,
449 IRB.CreatePointerCast(M->getArgOperand(0), IRB.getInt8PtrTy()),
450 IRB.CreateIntCast(M->getArgOperand(1), IRB.getInt32Ty(), false),
451 IRB.CreateIntCast(M->getArgOperand(2), IntptrTy, false));
452 I->eraseFromParent();
453 } else if (MemTransferInst *M = dyn_cast<MemTransferInst>(I)) {
454 IRB.CreateCall3(isa<MemCpyInst>(M) ? MemcpyFn : MemmoveFn,
455 IRB.CreatePointerCast(M->getArgOperand(0), IRB.getInt8PtrTy()),
456 IRB.CreatePointerCast(M->getArgOperand(1), IRB.getInt8PtrTy()),
457 IRB.CreateIntCast(M->getArgOperand(2), IntptrTy, false));
458 I->eraseFromParent();
459 }
460 return false;
461}
462
Dmitry Vyukov12b5cb92012-11-26 11:36:19 +0000463// Both llvm and ThreadSanitizer atomic operations are based on C++11/C1x
Alp Tokercb402912014-01-24 17:20:08 +0000464// standards. For background see C++11 standard. A slightly older, publicly
Dmitry Vyukov12b5cb92012-11-26 11:36:19 +0000465// available draft of the standard (not entirely up-to-date, but close enough
466// for casual browsing) is available here:
Matt Beaumont-Gay000a3952012-11-26 16:27:22 +0000467// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
Dmitry Vyukov12b5cb92012-11-26 11:36:19 +0000468// The following page contains more background information:
469// http://www.hpl.hp.com/personal/Hans_Boehm/c++mm/
470
Kostya Serebryanya1259772012-04-27 07:31:53 +0000471bool ThreadSanitizer::instrumentAtomic(Instruction *I) {
472 IRBuilder<> IRB(I);
473 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
474 Value *Addr = LI->getPointerOperand();
475 int Idx = getMemoryAccessFuncIndex(Addr);
476 if (Idx < 0)
477 return false;
478 const size_t ByteSize = 1 << Idx;
479 const size_t BitSize = ByteSize * 8;
480 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
Micah Villmow51e72462012-10-24 17:25:11 +0000481 Type *PtrTy = Ty->getPointerTo();
Kostya Serebryanya1259772012-04-27 07:31:53 +0000482 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
483 createOrdering(&IRB, LI->getOrdering())};
Craig Toppere1d12942014-08-27 05:25:25 +0000484 CallInst *C = CallInst::Create(TsanAtomicLoad[Idx], Args);
Kostya Serebryanya1259772012-04-27 07:31:53 +0000485 ReplaceInstWithInst(I, C);
486
487 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
488 Value *Addr = SI->getPointerOperand();
489 int Idx = getMemoryAccessFuncIndex(Addr);
490 if (Idx < 0)
491 return false;
492 const size_t ByteSize = 1 << Idx;
493 const size_t BitSize = ByteSize * 8;
494 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
Micah Villmow51e72462012-10-24 17:25:11 +0000495 Type *PtrTy = Ty->getPointerTo();
Kostya Serebryanya1259772012-04-27 07:31:53 +0000496 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
497 IRB.CreateIntCast(SI->getValueOperand(), Ty, false),
498 createOrdering(&IRB, SI->getOrdering())};
Craig Toppere1d12942014-08-27 05:25:25 +0000499 CallInst *C = CallInst::Create(TsanAtomicStore[Idx], Args);
Kostya Serebryanya1259772012-04-27 07:31:53 +0000500 ReplaceInstWithInst(I, C);
Dmitry Vyukov92b9e1d2012-11-09 12:55:36 +0000501 } else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I)) {
502 Value *Addr = RMWI->getPointerOperand();
503 int Idx = getMemoryAccessFuncIndex(Addr);
504 if (Idx < 0)
505 return false;
506 Function *F = TsanAtomicRMW[RMWI->getOperation()][Idx];
Craig Topperf40110f2014-04-25 05:29:35 +0000507 if (!F)
Dmitry Vyukov92b9e1d2012-11-09 12:55:36 +0000508 return false;
509 const size_t ByteSize = 1 << Idx;
510 const size_t BitSize = ByteSize * 8;
511 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
512 Type *PtrTy = Ty->getPointerTo();
513 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
514 IRB.CreateIntCast(RMWI->getValOperand(), Ty, false),
515 createOrdering(&IRB, RMWI->getOrdering())};
Craig Toppere1d12942014-08-27 05:25:25 +0000516 CallInst *C = CallInst::Create(F, Args);
Dmitry Vyukov92b9e1d2012-11-09 12:55:36 +0000517 ReplaceInstWithInst(I, C);
518 } else if (AtomicCmpXchgInst *CASI = dyn_cast<AtomicCmpXchgInst>(I)) {
519 Value *Addr = CASI->getPointerOperand();
520 int Idx = getMemoryAccessFuncIndex(Addr);
521 if (Idx < 0)
522 return false;
523 const size_t ByteSize = 1 << Idx;
524 const size_t BitSize = ByteSize * 8;
525 Type *Ty = Type::getIntNTy(IRB.getContext(), BitSize);
526 Type *PtrTy = Ty->getPointerTo();
527 Value *Args[] = {IRB.CreatePointerCast(Addr, PtrTy),
528 IRB.CreateIntCast(CASI->getCompareOperand(), Ty, false),
529 IRB.CreateIntCast(CASI->getNewValOperand(), Ty, false),
Tim Northovere94a5182014-03-11 10:48:52 +0000530 createOrdering(&IRB, CASI->getSuccessOrdering()),
531 createOrdering(&IRB, CASI->getFailureOrdering())};
Tim Northover420a2162014-06-13 14:24:07 +0000532 CallInst *C = IRB.CreateCall(TsanAtomicCAS[Idx], Args);
533 Value *Success = IRB.CreateICmpEQ(C, CASI->getCompareOperand());
534
535 Value *Res = IRB.CreateInsertValue(UndefValue::get(CASI->getType()), C, 0);
536 Res = IRB.CreateInsertValue(Res, Success, 1);
537
538 I->replaceAllUsesWith(Res);
539 I->eraseFromParent();
Dmitry Vyukov92b9e1d2012-11-09 12:55:36 +0000540 } else if (FenceInst *FI = dyn_cast<FenceInst>(I)) {
541 Value *Args[] = {createOrdering(&IRB, FI->getOrdering())};
542 Function *F = FI->getSynchScope() == SingleThread ?
543 TsanAtomicSignalFence : TsanAtomicThreadFence;
Craig Toppere1d12942014-08-27 05:25:25 +0000544 CallInst *C = CallInst::Create(F, Args);
Dmitry Vyukov92b9e1d2012-11-09 12:55:36 +0000545 ReplaceInstWithInst(I, C);
Kostya Serebryanya1259772012-04-27 07:31:53 +0000546 }
547 return true;
548}
549
550int ThreadSanitizer::getMemoryAccessFuncIndex(Value *Addr) {
551 Type *OrigPtrTy = Addr->getType();
552 Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
553 assert(OrigTy->isSized());
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000554 uint32_t TypeSize = DL->getTypeStoreSizeInBits(OrigTy);
Kostya Serebryanya1259772012-04-27 07:31:53 +0000555 if (TypeSize != 8 && TypeSize != 16 &&
556 TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
557 NumAccessesWithBadSize++;
558 // Ignore all unusual sizes.
559 return -1;
560 }
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000561 size_t Idx = countTrailingZeros(TypeSize / 8);
Kostya Serebryanya1259772012-04-27 07:31:53 +0000562 assert(Idx < kNumberOfAccessSizes);
563 return Idx;
564}