blob: 1f798b2975623a2bf4253a9e1c3c8c1aeba3d032 [file] [log] [blame]
Alexey Samsonov3b2f9f42012-06-04 13:55:19 +00001//===-- tsan_sync.cc ------------------------------------------------------===//
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00002//
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 (TSan), a race detector.
11//
12//===----------------------------------------------------------------------===//
Alexey Samsonov8bd90982012-06-07 09:50:16 +000013#include "sanitizer_common/sanitizer_placement_new.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000014#include "tsan_sync.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000015#include "tsan_rtl.h"
16#include "tsan_mman.h"
17
18namespace __tsan {
19
20SyncVar::SyncVar(uptr addr)
21 : mtx(MutexTypeSyncVar, StatMtxSyncVar)
22 , addr(addr)
23 , owner_tid(kInvalidTid)
Dmitry Vyukov3482ec32012-08-16 15:08:49 +000024 , last_lock()
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000025 , recursion()
26 , is_rw()
27 , is_recursive()
Dmitry Vyukov19ae9f32012-08-16 14:21:09 +000028 , is_broken()
29 , is_linker_init() {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000030}
31
32SyncTab::Part::Part()
33 : mtx(MutexTypeSyncTab, StatMtxSyncTab)
34 , val() {
35}
36
37SyncTab::SyncTab() {
38}
39
40SyncTab::~SyncTab() {
41 for (int i = 0; i < kPartCount; i++) {
42 while (tab_[i].val) {
43 SyncVar *tmp = tab_[i].val;
44 tab_[i].val = tmp->next;
45 DestroyAndFree(tmp);
46 }
47 }
48}
49
50SyncVar* SyncTab::GetAndLock(ThreadState *thr, uptr pc,
51 uptr addr, bool write_lock) {
Dmitry Vyukov1c0b3c62012-08-15 17:27:20 +000052#ifndef TSAN_GO
53 if (PrimaryAllocator::PointerIsMine((void*)addr)) {
54 MBlock *b = user_mblock(thr, (void*)addr);
55 Lock l(&b->mtx);
56 SyncVar *res = 0;
57 for (res = b->head; res; res = res->next) {
58 if (res->addr == addr)
59 break;
60 }
61 if (res == 0) {
62 StatInc(thr, StatSyncCreated);
63 void *mem = internal_alloc(MBlockSync, sizeof(SyncVar));
64 res = new(mem) SyncVar(addr);
65 res->creation_stack.ObtainCurrent(thr, pc);
66 res->next = b->head;
67 b->head = res;
68 }
69 if (write_lock)
70 res->mtx.Lock();
71 else
72 res->mtx.ReadLock();
73 return res;
74 }
75#endif
76
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000077 Part *p = &tab_[PartIdx(addr)];
78 {
79 ReadLock l(&p->mtx);
80 for (SyncVar *res = p->val; res; res = res->next) {
81 if (res->addr == addr) {
82 if (write_lock)
83 res->mtx.Lock();
84 else
85 res->mtx.ReadLock();
86 return res;
87 }
88 }
89 }
90 {
91 Lock l(&p->mtx);
92 SyncVar *res = p->val;
93 for (; res; res = res->next) {
94 if (res->addr == addr)
95 break;
96 }
97 if (res == 0) {
98 StatInc(thr, StatSyncCreated);
99 void *mem = internal_alloc(MBlockSync, sizeof(SyncVar));
100 res = new(mem) SyncVar(addr);
Dmitry Vyukov536551d2012-07-27 13:21:33 +0000101#ifndef TSAN_GO
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000102 res->creation_stack.ObtainCurrent(thr, pc);
Dmitry Vyukov536551d2012-07-27 13:21:33 +0000103#endif
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000104 res->next = p->val;
105 p->val = res;
106 }
107 if (write_lock)
108 res->mtx.Lock();
109 else
110 res->mtx.ReadLock();
111 return res;
112 }
113}
114
115SyncVar* SyncTab::GetAndRemove(ThreadState *thr, uptr pc, uptr addr) {
Dmitry Vyukov1c0b3c62012-08-15 17:27:20 +0000116#ifndef TSAN_GO
117 if (PrimaryAllocator::PointerIsMine((void*)addr)) {
118 MBlock *b = user_mblock(thr, (void*)addr);
119 SyncVar *res = 0;
120 {
121 Lock l(&b->mtx);
122 SyncVar **prev = &b->head;
123 res = *prev;
124 while (res) {
125 if (res->addr == addr) {
126 *prev = res->next;
127 break;
128 }
129 prev = &res->next;
130 res = *prev;
131 }
132 }
133 if (res) {
134 StatInc(thr, StatSyncDestroyed);
135 res->mtx.Lock();
136 res->mtx.Unlock();
137 }
138 return res;
139 }
140#endif
141
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000142 Part *p = &tab_[PartIdx(addr)];
143 SyncVar *res = 0;
144 {
145 Lock l(&p->mtx);
146 SyncVar **prev = &p->val;
147 res = *prev;
148 while (res) {
149 if (res->addr == addr) {
150 *prev = res->next;
151 break;
152 }
153 prev = &res->next;
154 res = *prev;
155 }
156 }
157 if (res) {
158 StatInc(thr, StatSyncDestroyed);
159 res->mtx.Lock();
160 res->mtx.Unlock();
161 }
162 return res;
163}
164
Dmitry Vyukov15710c92012-05-22 11:33:03 +0000165uptr SyncVar::GetMemoryConsumption() {
166 return sizeof(*this)
167 + clock.size() * sizeof(u64)
168 + read_clock.size() * sizeof(u64)
169 + creation_stack.Size() * sizeof(uptr);
170}
171
172uptr SyncTab::GetMemoryConsumption(uptr *nsync) {
173 uptr mem = 0;
174 for (int i = 0; i < kPartCount; i++) {
175 Part *p = &tab_[i];
176 Lock l(&p->mtx);
177 for (SyncVar *s = p->val; s; s = s->next) {
178 *nsync += 1;
179 mem += s->GetMemoryConsumption();
180 }
181 }
182 return mem;
183}
184
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000185int SyncTab::PartIdx(uptr addr) {
186 return (addr >> 3) % kPartCount;
187}
188
189StackTrace::StackTrace()
190 : n_()
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000191 , s_()
192 , c_() {
193}
194
195StackTrace::StackTrace(uptr *buf, uptr cnt)
196 : n_()
197 , s_(buf)
198 , c_(cnt) {
199 CHECK_NE(buf, 0);
200 CHECK_NE(cnt, 0);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000201}
202
203StackTrace::~StackTrace() {
204 Reset();
205}
206
207void StackTrace::Reset() {
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000208 if (s_ && !c_) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000209 CHECK_NE(n_, 0);
210 internal_free(s_);
211 s_ = 0;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000212 }
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000213 n_ = 0;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000214}
215
216void StackTrace::Init(const uptr *pcs, uptr cnt) {
217 Reset();
218 if (cnt == 0)
219 return;
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000220 if (c_) {
221 CHECK_NE(s_, 0);
222 CHECK_LE(cnt, c_);
223 } else {
224 s_ = (uptr*)internal_alloc(MBlockStackTrace, cnt * sizeof(s_[0]));
225 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000226 n_ = cnt;
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000227 internal_memcpy(s_, pcs, cnt * sizeof(s_[0]));
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000228}
229
230void StackTrace::ObtainCurrent(ThreadState *thr, uptr toppc) {
231 Reset();
Dmitry Vyukov5bfac972012-07-16 16:44:47 +0000232 n_ = thr->shadow_stack_pos - thr->shadow_stack;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000233 if (n_ + !!toppc == 0)
234 return;
Dmitry Vyukovde1fd1c2012-06-22 11:08:55 +0000235 if (c_) {
236 CHECK_NE(s_, 0);
237 CHECK_LE(n_ + !!toppc, c_);
238 } else {
239 s_ = (uptr*)internal_alloc(MBlockStackTrace,
240 (n_ + !!toppc) * sizeof(s_[0]));
241 }
Kostya Serebryany4ad375f2012-05-10 13:48:04 +0000242 for (uptr i = 0; i < n_; i++)
243 s_[i] = thr->shadow_stack[i];
244 if (toppc) {
245 s_[n_] = toppc;
246 n_++;
247 }
248}
249
250void StackTrace::CopyFrom(const StackTrace& other) {
251 Reset();
252 Init(other.Begin(), other.Size());
253}
254
255bool StackTrace::IsEmpty() const {
256 return n_ == 0;
257}
258
259uptr StackTrace::Size() const {
260 return n_;
261}
262
263uptr StackTrace::Get(uptr i) const {
264 CHECK_LT(i, n_);
265 return s_[i];
266}
267
268const uptr *StackTrace::Begin() const {
269 return s_;
270}
271
272} // namespace __tsan