blob: 4fcf34e98470aa36d4ce56323690fca696f00fbe [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
26#include "drd_error.h"
27#include "drd_mutex.h"
28#include "drd_suppression.h"
sewardj721ad7b2007-11-30 08:30:29 +000029#include "priv_drd_clientreq.h"
sewardjaf44c822007-11-25 14:01:38 +000030#include "pub_tool_errormgr.h" // VG_(maybe_record_error)()
31#include "pub_tool_libcassert.h" // tl_assert()
32#include "pub_tool_libcprint.h" // VG_(printf)()
33#include "pub_tool_machine.h" // VG_(get_IP)()
34#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
35
36
37// Type definitions.
38
39struct mutex_info
40{
41 Addr mutex; // Pointer to client mutex.
42 SizeT size; // Size in bytes of client-side object.
sewardj721ad7b2007-11-30 08:30:29 +000043 MutexT mutex_type; // pthread_mutex_t or pthread_spinlock_t.
sewardjaf44c822007-11-25 14:01:38 +000044 int recursion_count; // 0 if free, >= 1 if locked.
45 DrdThreadId owner; // owner if locked, last owner if free.
46 VectorClock vc; // vector clock associated with last unlock.
47};
48
49
sewardj347eeba2008-01-21 14:19:07 +000050// Local functions.
51
52static void mutex_destroy(struct mutex_info* const p);
53
54
sewardjaf44c822007-11-25 14:01:38 +000055// Local variables.
56
57static Bool s_trace_mutex;
58static ULong s_mutex_lock_count;
59struct mutex_info s_mutex[256];
60
61
62// Function definitions.
63
64void mutex_set_trace(const Bool trace_mutex)
65{
66 tl_assert(!! trace_mutex == trace_mutex);
67 s_trace_mutex = trace_mutex;
68}
69
70static
71void mutex_initialize(struct mutex_info* const p,
72 const Addr mutex,
sewardj721ad7b2007-11-30 08:30:29 +000073 const SizeT size,
74 const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000075{
76 tl_assert(mutex != 0);
77 tl_assert(size > 0);
sewardj721ad7b2007-11-30 08:30:29 +000078 tl_assert(mutex_type == mutex_type_mutex
79 || mutex_type == mutex_type_spinlock);
sewardjaf44c822007-11-25 14:01:38 +000080
81 p->mutex = mutex;
82 p->size = size;
sewardj721ad7b2007-11-30 08:30:29 +000083 p->mutex_type = mutex_type;
sewardjaf44c822007-11-25 14:01:38 +000084 p->recursion_count = 0;
85 p->owner = DRD_INVALID_THREADID;
86 vc_init(&p->vc, 0, 0);
87}
88
89static
sewardj721ad7b2007-11-30 08:30:29 +000090struct mutex_info*
91mutex_get_or_allocate(const Addr mutex,
92 const SizeT size,
93 const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000094{
95 int i;
sewardj721ad7b2007-11-30 08:30:29 +000096
97 tl_assert(mutex_type == mutex_type_mutex
98 || mutex_type == mutex_type_spinlock);
99
sewardjaf44c822007-11-25 14:01:38 +0000100 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
sewardj721ad7b2007-11-30 08:30:29 +0000101 {
sewardjaf44c822007-11-25 14:01:38 +0000102 if (s_mutex[i].mutex == mutex)
sewardj721ad7b2007-11-30 08:30:29 +0000103 {
104 tl_assert(s_mutex[i].mutex_type == mutex_type);
105 tl_assert(s_mutex[i].size == size);
sewardjaf44c822007-11-25 14:01:38 +0000106 return &s_mutex[i];
sewardj721ad7b2007-11-30 08:30:29 +0000107 }
108 }
sewardjaf44c822007-11-25 14:01:38 +0000109 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
110 {
111 if (s_mutex[i].mutex == 0)
112 {
bartab7a6442008-02-25 19:46:14 +0000113 if (drd_is_any_suppressed(mutex, mutex + size))
114 {
barte883bc82008-02-26 19:13:04 +0000115 GenericErrInfo GEI;
bartab7a6442008-02-25 19:46:14 +0000116 VG_(maybe_record_error)(VG_(get_running_tid)(),
barte883bc82008-02-26 19:13:04 +0000117 GenericErr,
bartab7a6442008-02-25 19:46:14 +0000118 VG_(get_IP)(VG_(get_running_tid)()),
119 "Not a mutex",
barte883bc82008-02-26 19:13:04 +0000120 &GEI);
bartab7a6442008-02-25 19:46:14 +0000121 return 0;
122 }
sewardj721ad7b2007-11-30 08:30:29 +0000123 mutex_initialize(&s_mutex[i], mutex, size, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000124 drd_start_suppression(mutex, mutex + size,
125 mutex_get_typename(&s_mutex[i]));
126 return &s_mutex[i];
127 }
128 }
129 tl_assert(0);
130 return 0;
131}
132
sewardj721ad7b2007-11-30 08:30:29 +0000133struct mutex_info*
134mutex_init(const Addr mutex, const SizeT size, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000135{
136 struct mutex_info* mutex_p;
137
sewardjaf44c822007-11-25 14:01:38 +0000138 if (s_trace_mutex)
139 {
140 const ThreadId vg_tid = VG_(get_running_tid)();
141 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
142 VG_(message)(Vg_DebugMsg,
143 "drd_post_mutex_init tid = %d/%d, %s 0x%lx",
144 vg_tid, drd_tid,
sewardj347eeba2008-01-21 14:19:07 +0000145 mutex_type_name(mutex_type),
sewardjaf44c822007-11-25 14:01:38 +0000146 mutex);
147 }
148
sewardj347eeba2008-01-21 14:19:07 +0000149 tl_assert(mutex_type == mutex_type_mutex
150 || mutex_type == mutex_type_spinlock);
151 mutex_p = mutex_get(mutex);
152 if (mutex_p)
153 {
154 const ThreadId vg_tid = VG_(get_running_tid)();
155 MutexErrInfo MEI
156 = { mutex_p->mutex, mutex_p->recursion_count, mutex_p->owner };
157 VG_(maybe_record_error)(vg_tid,
158 MutexErr,
159 VG_(get_IP)(vg_tid),
160 "Mutex reinitialization",
161 &MEI);
162 mutex_destroy(mutex_p);
163 }
164 mutex_p = mutex_get_or_allocate(mutex, size, mutex_type);
165
sewardjaf44c822007-11-25 14:01:38 +0000166 return mutex_p;
167}
168
sewardj347eeba2008-01-21 14:19:07 +0000169static void mutex_destroy(struct mutex_info* const p)
sewardjaf44c822007-11-25 14:01:38 +0000170{
171 if (s_trace_mutex)
172 {
173 const ThreadId vg_tid = VG_(get_running_tid)();
174 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
175 VG_(message)(Vg_DebugMsg,
176 "drd_pre_mutex_destroy tid = %d/%d, %s 0x%lx",
177 vg_tid, drd_tid,
178 mutex_get_typename(p),
179 p->mutex);
180 }
181
182 drd_finish_suppression(p->mutex, p->mutex + p->size);
183
184 vc_cleanup(&p->vc);
185 p->mutex = 0;
186}
187
sewardj347eeba2008-01-21 14:19:07 +0000188void mutex_pre_destroy(struct mutex_info* const p)
189{
190 return mutex_destroy(p);
191}
192
193void mutex_post_destroy(const Addr mutex)
194{
195 struct mutex_info* p;
196
197 p = mutex_get(mutex);
198 tl_assert(p);
199 if (p)
200 {
201 if (mutex_get_recursion_count(mutex) > 0)
202 {
203 const ThreadId vg_tid = VG_(get_running_tid)();
204 MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner };
205 VG_(maybe_record_error)(vg_tid,
206 MutexErr,
207 VG_(get_IP)(vg_tid),
208 "Destroying locked mutex",
209 &MEI);
210 }
211 mutex_pre_destroy(p);
212 }
213}
214
sewardjaf44c822007-11-25 14:01:38 +0000215struct mutex_info* mutex_get(const Addr mutex)
216{
217 int i;
218 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
219 if (s_mutex[i].mutex == mutex)
220 return &s_mutex[i];
221 return 0;
222}
223
224/**
225 * Update mutex_info state when locking the pthread_mutex_t mutex.
226 * Note: this function must be called after pthread_mutex_lock() has been
227 * called, or a race condition is triggered !
228 */
sewardj721ad7b2007-11-30 08:30:29 +0000229int mutex_lock(const Addr mutex, const SizeT size, MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000230{
231 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)());
sewardj721ad7b2007-11-30 08:30:29 +0000232 struct mutex_info* const p = mutex_get_or_allocate(mutex, size, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000233
234 if (s_trace_mutex)
235 {
236 const ThreadId tid = DrdThreadIdToVgThreadId(drd_tid);
237 VG_(message)(Vg_DebugMsg,
238 "drd_post_mutex_lock tid = %d/%d, %s 0x%lx rc %d owner %d",
239 tid,
240 drd_tid,
241 mutex_get_typename(p),
242 mutex,
243 p ? p->recursion_count : 0,
244 p ? p->owner : VG_INVALID_THREADID);
245 }
246
bartab7a6442008-02-25 19:46:14 +0000247 if (p == 0)
248 {
barte883bc82008-02-26 19:13:04 +0000249 GenericErrInfo GEI;
bartab7a6442008-02-25 19:46:14 +0000250 VG_(maybe_record_error)(VG_(get_running_tid)(),
barte883bc82008-02-26 19:13:04 +0000251 GenericErr,
bartab7a6442008-02-25 19:46:14 +0000252 VG_(get_IP)(VG_(get_running_tid)()),
253 "Not a mutex",
barte883bc82008-02-26 19:13:04 +0000254 &GEI);
bartab7a6442008-02-25 19:46:14 +0000255 return 0;
256 }
257
sewardj721ad7b2007-11-30 08:30:29 +0000258 tl_assert(mutex_type == mutex_type_mutex
259 || mutex_type == mutex_type_spinlock);
260 tl_assert(p->mutex_type == mutex_type);
261 tl_assert(p->size == size);
262
263 if (p->recursion_count >= 1 && mutex_type == mutex_type_spinlock)
sewardjaf44c822007-11-25 14:01:38 +0000264 {
265 // TO DO: tell the user in a more friendly way that it is not allowed to
266 // lock spinlocks recursively.
267 tl_assert(0);
268 }
269
270 if (p->recursion_count == 0)
271 {
272 p->owner = drd_tid;
273 s_mutex_lock_count++;
274 }
275 else if (p->owner != drd_tid)
276 {
277 VG_(message)(Vg_DebugMsg,
278 "The impossible happened: mutex 0x%lx is locked"
279 " simultaneously by two threads (recursion count %d,"
280 " owners %d and %d) !",
281 p->mutex, p->recursion_count, p->owner, drd_tid);
sewardj347eeba2008-01-21 14:19:07 +0000282 p->owner = drd_tid;
sewardjaf44c822007-11-25 14:01:38 +0000283 }
284 p->recursion_count++;
285
286 if (p->recursion_count == 1)
287 {
bartab7a6442008-02-25 19:46:14 +0000288 const DrdThreadId last_owner = p->owner;
289
sewardjaf44c822007-11-25 14:01:38 +0000290 if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID)
291 thread_combine_vc2(drd_tid, mutex_get_last_vc(mutex));
292 thread_new_segment(drd_tid);
293 }
294
295 return p->recursion_count;
296}
297
298/**
299 * Update mutex_info state when unlocking the pthread_mutex_t mutex.
300 * Note: this function must be called before pthread_mutex_unlock() is called,
301 * or a race condition is triggered !
302 * @param mutex Pointer to pthread_mutex_t data structure in the client space.
303 * @param tid ThreadId of the thread calling pthread_mutex_unlock().
304 * @param vc Pointer to the current vector clock of thread tid.
305 */
sewardj721ad7b2007-11-30 08:30:29 +0000306int mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000307{
308 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)());
309 const ThreadId vg_tid = DrdThreadIdToVgThreadId(drd_tid);
310 const VectorClock* const vc = thread_get_vc(drd_tid);
311 struct mutex_info* const p = mutex_get(mutex);
312
313 if (s_trace_mutex)
314 {
315 VG_(message)(Vg_DebugMsg,
316 "drd_pre_mutex_unlock tid = %d/%d, %s 0x%lx rc %d",
317 vg_tid, drd_tid,
318 mutex_get_typename(p),
319 mutex,
320 p->recursion_count,
321 p->owner);
322 }
323
bartab7a6442008-02-25 19:46:14 +0000324 if (p == 0 || p->owner == DRD_INVALID_THREADID)
325 {
barte883bc82008-02-26 19:13:04 +0000326 GenericErrInfo GEI;
bartab7a6442008-02-25 19:46:14 +0000327 VG_(maybe_record_error)(vg_tid,
barte883bc82008-02-26 19:13:04 +0000328 GenericErr,
bartab7a6442008-02-25 19:46:14 +0000329 VG_(get_IP)(vg_tid),
330 "Not a mutex",
barte883bc82008-02-26 19:13:04 +0000331 &GEI);
bartab7a6442008-02-25 19:46:14 +0000332 return 0;
333 }
334
sewardjaf44c822007-11-25 14:01:38 +0000335 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000336 tl_assert(p->mutex_type == mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000337 tl_assert(p->owner != DRD_INVALID_THREADID);
sewardj721ad7b2007-11-30 08:30:29 +0000338 tl_assert(mutex_type == mutex_type_mutex
339 || mutex_type == mutex_type_spinlock);
340
sewardjaf44c822007-11-25 14:01:38 +0000341 if (p->owner != drd_tid)
342 {
343 MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner };
344 VG_(maybe_record_error)(vg_tid,
345 MutexErr,
346 VG_(get_IP)(vg_tid),
347 "Mutex not unlocked by owner thread",
348 &MEI);
349 }
350 p->recursion_count--;
sewardj347eeba2008-01-21 14:19:07 +0000351 if (p->recursion_count < 0)
352 {
353 MutexErrInfo MEI
354 = { p->mutex, p->recursion_count, p->owner };
355 VG_(maybe_record_error)(vg_tid,
356 MutexErr,
357 VG_(get_IP)(vg_tid),
358 "Attempt to unlock a mutex that is not locked",
359 &MEI);
360 p->recursion_count = 0;
361 }
362
sewardjaf44c822007-11-25 14:01:38 +0000363 if (p->recursion_count == 0)
364 {
365 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
366 /* current vector clock of the thread such that it is available when */
367 /* this mutex is locked again. */
bart301c3112008-02-24 18:22:37 +0000368 vc_assign(&p->vc, vc);
sewardjaf44c822007-11-25 14:01:38 +0000369
370 thread_new_segment(drd_tid);
371 }
372 return p->recursion_count;
373}
374
375const char* mutex_get_typename(struct mutex_info* const p)
376{
377 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000378
sewardj347eeba2008-01-21 14:19:07 +0000379 return mutex_type_name(p->mutex_type);
380}
381
382const char* mutex_type_name(const MutexT mt)
383{
384 switch (mt)
sewardjaf44c822007-11-25 14:01:38 +0000385 {
sewardj721ad7b2007-11-30 08:30:29 +0000386 case mutex_type_mutex:
sewardjaf44c822007-11-25 14:01:38 +0000387 return "mutex";
sewardj721ad7b2007-11-30 08:30:29 +0000388 case mutex_type_spinlock:
sewardjaf44c822007-11-25 14:01:38 +0000389 return "spinlock";
390 default:
391 tl_assert(0);
392 }
393 return "?";
394}
395
396Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid)
397{
398 struct mutex_info* const p = mutex_get(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000399 if (p)
400 {
401 return (p->recursion_count > 0 && p->owner == tid);
402 }
403 return False;
404}
405
406const VectorClock* mutex_get_last_vc(const Addr mutex)
407{
408 struct mutex_info* const p = mutex_get(mutex);
409 return p ? &p->vc : 0;
410}
411
412int mutex_get_recursion_count(const Addr mutex)
413{
414 struct mutex_info* const p = mutex_get(mutex);
415 tl_assert(p);
416 return p->recursion_count;
417}
418
419/**
bart301c3112008-02-24 18:22:37 +0000420 * Call this function when thread tid stops to exist, such that the
sewardjaf44c822007-11-25 14:01:38 +0000421 * "last owner" field can be cleared if it still refers to that thread.
422 * TO DO: print an error message if a thread exits while it still has some
423 * mutexes locked.
424 */
bart301c3112008-02-24 18:22:37 +0000425void mutex_thread_delete(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000426{
427 int i;
428 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
429 {
430 struct mutex_info* const p = &s_mutex[i];
bart301c3112008-02-24 18:22:37 +0000431 if (p->mutex && p->owner == tid)
sewardjaf44c822007-11-25 14:01:38 +0000432 {
433 p->owner = VG_INVALID_THREADID;
434 }
435 }
436}
437
438void mutex_stop_using_mem(const Addr a1, const Addr a2)
439{
440 unsigned i;
441 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
442 {
443 if (a1 <= s_mutex[i].mutex && s_mutex[i].mutex < a2)
444 {
445 tl_assert(s_mutex[i].mutex + s_mutex[i].size <= a2);
446 mutex_destroy(&s_mutex[i]);
447 }
448 }
449}
450
451ULong get_mutex_lock_count(void)
452{
453 return s_mutex_lock_count;
454}
sewardj347eeba2008-01-21 14:19:07 +0000455
456
457/*
458 * Local variables:
459 * c-basic-offset: 3
460 * End:
461 */