blob: d7a107221391cd0f2f12dd645e8493fedebe6b78 [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
bart5357fcb2008-02-27 15:46:00 +000052static Bool mutex_is_locked(struct mutex_info* const p);
sewardj347eeba2008-01-21 14:19:07 +000053static void mutex_destroy(struct mutex_info* const p);
54
55
sewardjaf44c822007-11-25 14:01:38 +000056// Local variables.
57
58static Bool s_trace_mutex;
59static ULong s_mutex_lock_count;
60struct mutex_info s_mutex[256];
61
62
63// Function definitions.
64
65void mutex_set_trace(const Bool trace_mutex)
66{
67 tl_assert(!! trace_mutex == trace_mutex);
68 s_trace_mutex = trace_mutex;
69}
70
71static
72void mutex_initialize(struct mutex_info* const p,
73 const Addr mutex,
sewardj721ad7b2007-11-30 08:30:29 +000074 const SizeT size,
75 const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000076{
77 tl_assert(mutex != 0);
78 tl_assert(size > 0);
bart5357fcb2008-02-27 15:46:00 +000079#if 0
sewardj721ad7b2007-11-30 08:30:29 +000080 tl_assert(mutex_type == mutex_type_mutex
81 || mutex_type == mutex_type_spinlock);
bart5357fcb2008-02-27 15:46:00 +000082#endif
sewardjaf44c822007-11-25 14:01:38 +000083
84 p->mutex = mutex;
85 p->size = size;
sewardj721ad7b2007-11-30 08:30:29 +000086 p->mutex_type = mutex_type;
sewardjaf44c822007-11-25 14:01:38 +000087 p->recursion_count = 0;
88 p->owner = DRD_INVALID_THREADID;
89 vc_init(&p->vc, 0, 0);
90}
91
92static
sewardj721ad7b2007-11-30 08:30:29 +000093struct mutex_info*
94mutex_get_or_allocate(const Addr mutex,
95 const SizeT size,
96 const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000097{
98 int i;
sewardj721ad7b2007-11-30 08:30:29 +000099
bart5357fcb2008-02-27 15:46:00 +0000100#if 0
sewardj721ad7b2007-11-30 08:30:29 +0000101 tl_assert(mutex_type == mutex_type_mutex
102 || mutex_type == mutex_type_spinlock);
bart5357fcb2008-02-27 15:46:00 +0000103#endif
sewardj721ad7b2007-11-30 08:30:29 +0000104
sewardjaf44c822007-11-25 14:01:38 +0000105 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
sewardj721ad7b2007-11-30 08:30:29 +0000106 {
sewardjaf44c822007-11-25 14:01:38 +0000107 if (s_mutex[i].mutex == mutex)
sewardj721ad7b2007-11-30 08:30:29 +0000108 {
bart5357fcb2008-02-27 15:46:00 +0000109 if (s_mutex[i].mutex_type != mutex_type)
110 {
111 VG_(message)(Vg_DebugMsg, "??? mutex %p: type changed from %d into %d",
112 s_mutex[i].mutex, s_mutex[i].mutex_type, mutex_type);
113 }
sewardj721ad7b2007-11-30 08:30:29 +0000114 tl_assert(s_mutex[i].mutex_type == mutex_type);
115 tl_assert(s_mutex[i].size == size);
sewardjaf44c822007-11-25 14:01:38 +0000116 return &s_mutex[i];
sewardj721ad7b2007-11-30 08:30:29 +0000117 }
118 }
sewardjaf44c822007-11-25 14:01:38 +0000119 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
120 {
121 if (s_mutex[i].mutex == 0)
122 {
bartab7a6442008-02-25 19:46:14 +0000123 if (drd_is_any_suppressed(mutex, mutex + size))
124 {
barte883bc82008-02-26 19:13:04 +0000125 GenericErrInfo GEI;
bartab7a6442008-02-25 19:46:14 +0000126 VG_(maybe_record_error)(VG_(get_running_tid)(),
barte883bc82008-02-26 19:13:04 +0000127 GenericErr,
bartab7a6442008-02-25 19:46:14 +0000128 VG_(get_IP)(VG_(get_running_tid)()),
129 "Not a mutex",
barte883bc82008-02-26 19:13:04 +0000130 &GEI);
bartab7a6442008-02-25 19:46:14 +0000131 return 0;
132 }
sewardj721ad7b2007-11-30 08:30:29 +0000133 mutex_initialize(&s_mutex[i], mutex, size, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000134 drd_start_suppression(mutex, mutex + size,
135 mutex_get_typename(&s_mutex[i]));
136 return &s_mutex[i];
137 }
138 }
139 tl_assert(0);
140 return 0;
141}
142
sewardj721ad7b2007-11-30 08:30:29 +0000143struct mutex_info*
144mutex_init(const Addr mutex, const SizeT size, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000145{
146 struct mutex_info* mutex_p;
147
sewardjaf44c822007-11-25 14:01:38 +0000148 if (s_trace_mutex)
149 {
150 const ThreadId vg_tid = VG_(get_running_tid)();
151 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
152 VG_(message)(Vg_DebugMsg,
153 "drd_post_mutex_init tid = %d/%d, %s 0x%lx",
154 vg_tid, drd_tid,
sewardj347eeba2008-01-21 14:19:07 +0000155 mutex_type_name(mutex_type),
sewardjaf44c822007-11-25 14:01:38 +0000156 mutex);
157 }
158
bart5357fcb2008-02-27 15:46:00 +0000159#if 0
sewardj347eeba2008-01-21 14:19:07 +0000160 tl_assert(mutex_type == mutex_type_mutex
161 || mutex_type == mutex_type_spinlock);
bart5357fcb2008-02-27 15:46:00 +0000162#endif
163
sewardj347eeba2008-01-21 14:19:07 +0000164 mutex_p = mutex_get(mutex);
165 if (mutex_p)
166 {
167 const ThreadId vg_tid = VG_(get_running_tid)();
168 MutexErrInfo MEI
169 = { mutex_p->mutex, mutex_p->recursion_count, mutex_p->owner };
170 VG_(maybe_record_error)(vg_tid,
171 MutexErr,
172 VG_(get_IP)(vg_tid),
173 "Mutex reinitialization",
174 &MEI);
175 mutex_destroy(mutex_p);
176 }
177 mutex_p = mutex_get_or_allocate(mutex, size, mutex_type);
178
sewardjaf44c822007-11-25 14:01:38 +0000179 return mutex_p;
180}
181
sewardj347eeba2008-01-21 14:19:07 +0000182static void mutex_destroy(struct mutex_info* const p)
sewardjaf44c822007-11-25 14:01:38 +0000183{
184 if (s_trace_mutex)
185 {
186 const ThreadId vg_tid = VG_(get_running_tid)();
187 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
188 VG_(message)(Vg_DebugMsg,
189 "drd_pre_mutex_destroy tid = %d/%d, %s 0x%lx",
190 vg_tid, drd_tid,
191 mutex_get_typename(p),
192 p->mutex);
193 }
194
bart5357fcb2008-02-27 15:46:00 +0000195 if (mutex_is_locked(p))
196 {
197 MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner };
198 VG_(maybe_record_error)(VG_(get_running_tid)(),
199 MutexErr,
200 VG_(get_IP)(VG_(get_running_tid)()),
201 "Destroying locked mutex",
202 &MEI);
203 }
204
sewardjaf44c822007-11-25 14:01:38 +0000205 drd_finish_suppression(p->mutex, p->mutex + p->size);
206
207 vc_cleanup(&p->vc);
208 p->mutex = 0;
209}
210
sewardj347eeba2008-01-21 14:19:07 +0000211void mutex_pre_destroy(struct mutex_info* const p)
212{
213 return mutex_destroy(p);
214}
215
216void mutex_post_destroy(const Addr mutex)
217{
218 struct mutex_info* p;
219
220 p = mutex_get(mutex);
221 tl_assert(p);
222 if (p)
223 {
224 if (mutex_get_recursion_count(mutex) > 0)
225 {
226 const ThreadId vg_tid = VG_(get_running_tid)();
227 MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner };
228 VG_(maybe_record_error)(vg_tid,
229 MutexErr,
230 VG_(get_IP)(vg_tid),
231 "Destroying locked mutex",
232 &MEI);
233 }
234 mutex_pre_destroy(p);
235 }
236}
237
sewardjaf44c822007-11-25 14:01:38 +0000238struct mutex_info* mutex_get(const Addr mutex)
239{
240 int i;
241 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
242 if (s_mutex[i].mutex == mutex)
243 return &s_mutex[i];
244 return 0;
245}
246
247/**
248 * Update mutex_info state when locking the pthread_mutex_t mutex.
249 * Note: this function must be called after pthread_mutex_lock() has been
250 * called, or a race condition is triggered !
251 */
sewardj721ad7b2007-11-30 08:30:29 +0000252int mutex_lock(const Addr mutex, const SizeT size, MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000253{
254 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)());
sewardj721ad7b2007-11-30 08:30:29 +0000255 struct mutex_info* const p = mutex_get_or_allocate(mutex, size, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000256
257 if (s_trace_mutex)
258 {
259 const ThreadId tid = DrdThreadIdToVgThreadId(drd_tid);
260 VG_(message)(Vg_DebugMsg,
261 "drd_post_mutex_lock tid = %d/%d, %s 0x%lx rc %d owner %d",
262 tid,
263 drd_tid,
264 mutex_get_typename(p),
265 mutex,
266 p ? p->recursion_count : 0,
267 p ? p->owner : VG_INVALID_THREADID);
268 }
269
bartab7a6442008-02-25 19:46:14 +0000270 if (p == 0)
271 {
barte883bc82008-02-26 19:13:04 +0000272 GenericErrInfo GEI;
bartab7a6442008-02-25 19:46:14 +0000273 VG_(maybe_record_error)(VG_(get_running_tid)(),
barte883bc82008-02-26 19:13:04 +0000274 GenericErr,
bartab7a6442008-02-25 19:46:14 +0000275 VG_(get_IP)(VG_(get_running_tid)()),
276 "Not a mutex",
barte883bc82008-02-26 19:13:04 +0000277 &GEI);
bartab7a6442008-02-25 19:46:14 +0000278 return 0;
279 }
280
bart5357fcb2008-02-27 15:46:00 +0000281#if 0
sewardj721ad7b2007-11-30 08:30:29 +0000282 tl_assert(mutex_type == mutex_type_mutex
283 || mutex_type == mutex_type_spinlock);
bart5357fcb2008-02-27 15:46:00 +0000284#endif
285
sewardj721ad7b2007-11-30 08:30:29 +0000286 tl_assert(p->mutex_type == mutex_type);
287 tl_assert(p->size == size);
288
289 if (p->recursion_count >= 1 && mutex_type == mutex_type_spinlock)
sewardjaf44c822007-11-25 14:01:38 +0000290 {
291 // TO DO: tell the user in a more friendly way that it is not allowed to
292 // lock spinlocks recursively.
293 tl_assert(0);
294 }
295
296 if (p->recursion_count == 0)
297 {
298 p->owner = drd_tid;
299 s_mutex_lock_count++;
300 }
301 else if (p->owner != drd_tid)
302 {
303 VG_(message)(Vg_DebugMsg,
304 "The impossible happened: mutex 0x%lx is locked"
305 " simultaneously by two threads (recursion count %d,"
306 " owners %d and %d) !",
307 p->mutex, p->recursion_count, p->owner, drd_tid);
sewardj347eeba2008-01-21 14:19:07 +0000308 p->owner = drd_tid;
sewardjaf44c822007-11-25 14:01:38 +0000309 }
310 p->recursion_count++;
311
312 if (p->recursion_count == 1)
313 {
bartab7a6442008-02-25 19:46:14 +0000314 const DrdThreadId last_owner = p->owner;
315
sewardjaf44c822007-11-25 14:01:38 +0000316 if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID)
317 thread_combine_vc2(drd_tid, mutex_get_last_vc(mutex));
318 thread_new_segment(drd_tid);
319 }
320
321 return p->recursion_count;
322}
323
324/**
325 * Update mutex_info state when unlocking the pthread_mutex_t mutex.
326 * Note: this function must be called before pthread_mutex_unlock() is called,
327 * or a race condition is triggered !
328 * @param mutex Pointer to pthread_mutex_t data structure in the client space.
329 * @param tid ThreadId of the thread calling pthread_mutex_unlock().
330 * @param vc Pointer to the current vector clock of thread tid.
331 */
sewardj721ad7b2007-11-30 08:30:29 +0000332int mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000333{
334 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)());
335 const ThreadId vg_tid = DrdThreadIdToVgThreadId(drd_tid);
336 const VectorClock* const vc = thread_get_vc(drd_tid);
337 struct mutex_info* const p = mutex_get(mutex);
338
339 if (s_trace_mutex)
340 {
341 VG_(message)(Vg_DebugMsg,
342 "drd_pre_mutex_unlock tid = %d/%d, %s 0x%lx rc %d",
343 vg_tid, drd_tid,
344 mutex_get_typename(p),
345 mutex,
346 p->recursion_count,
347 p->owner);
348 }
349
bart5357fcb2008-02-27 15:46:00 +0000350 if (p == 0)
bartab7a6442008-02-25 19:46:14 +0000351 {
barte883bc82008-02-26 19:13:04 +0000352 GenericErrInfo GEI;
bartab7a6442008-02-25 19:46:14 +0000353 VG_(maybe_record_error)(vg_tid,
barte883bc82008-02-26 19:13:04 +0000354 GenericErr,
bartab7a6442008-02-25 19:46:14 +0000355 VG_(get_IP)(vg_tid),
356 "Not a mutex",
barte883bc82008-02-26 19:13:04 +0000357 &GEI);
bartab7a6442008-02-25 19:46:14 +0000358 return 0;
359 }
360
bart5357fcb2008-02-27 15:46:00 +0000361 if (p->owner == DRD_INVALID_THREADID)
362 {
363 MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner };
364 VG_(maybe_record_error)(vg_tid,
365 MutexErr,
366 VG_(get_IP)(vg_tid),
367 "Mutex not locked",
368 &MEI);
369 return 0;
370 }
371
sewardjaf44c822007-11-25 14:01:38 +0000372 tl_assert(p);
bart5357fcb2008-02-27 15:46:00 +0000373 if (p->mutex_type != mutex_type)
374 {
375 VG_(message)(Vg_DebugMsg, "??? mutex %p: type changed from %d into %d",
376 p->mutex, p->mutex_type, mutex_type);
377 }
sewardj721ad7b2007-11-30 08:30:29 +0000378 tl_assert(p->mutex_type == mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000379 tl_assert(p->owner != DRD_INVALID_THREADID);
bart5357fcb2008-02-27 15:46:00 +0000380#if 0
sewardj721ad7b2007-11-30 08:30:29 +0000381 tl_assert(mutex_type == mutex_type_mutex
382 || mutex_type == mutex_type_spinlock);
bart5357fcb2008-02-27 15:46:00 +0000383#endif
sewardj721ad7b2007-11-30 08:30:29 +0000384
sewardjaf44c822007-11-25 14:01:38 +0000385 if (p->owner != drd_tid)
386 {
387 MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner };
388 VG_(maybe_record_error)(vg_tid,
389 MutexErr,
390 VG_(get_IP)(vg_tid),
391 "Mutex not unlocked by owner thread",
392 &MEI);
393 }
394 p->recursion_count--;
sewardj347eeba2008-01-21 14:19:07 +0000395 if (p->recursion_count < 0)
396 {
397 MutexErrInfo MEI
398 = { p->mutex, p->recursion_count, p->owner };
399 VG_(maybe_record_error)(vg_tid,
400 MutexErr,
401 VG_(get_IP)(vg_tid),
402 "Attempt to unlock a mutex that is not locked",
403 &MEI);
404 p->recursion_count = 0;
405 }
406
sewardjaf44c822007-11-25 14:01:38 +0000407 if (p->recursion_count == 0)
408 {
409 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
410 /* current vector clock of the thread such that it is available when */
411 /* this mutex is locked again. */
bart301c3112008-02-24 18:22:37 +0000412 vc_assign(&p->vc, vc);
sewardjaf44c822007-11-25 14:01:38 +0000413
414 thread_new_segment(drd_tid);
415 }
416 return p->recursion_count;
417}
418
419const char* mutex_get_typename(struct mutex_info* const p)
420{
421 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000422
sewardj347eeba2008-01-21 14:19:07 +0000423 return mutex_type_name(p->mutex_type);
424}
425
426const char* mutex_type_name(const MutexT mt)
427{
428 switch (mt)
sewardjaf44c822007-11-25 14:01:38 +0000429 {
bart5357fcb2008-02-27 15:46:00 +0000430 case mutex_type_recursive_mutex:
431 return "recursive mutex";
432 case mutex_type_errorcheck_mutex:
433 return "error checking mutex";
434 case mutex_type_default_mutex:
sewardjaf44c822007-11-25 14:01:38 +0000435 return "mutex";
sewardj721ad7b2007-11-30 08:30:29 +0000436 case mutex_type_spinlock:
sewardjaf44c822007-11-25 14:01:38 +0000437 return "spinlock";
438 default:
439 tl_assert(0);
440 }
441 return "?";
442}
443
bart5357fcb2008-02-27 15:46:00 +0000444/** Return true if the specified mutex is locked by any thread. */
445static Bool mutex_is_locked(struct mutex_info* const p)
446{
447 tl_assert(p);
448 return (p->recursion_count > 0);
449}
450
sewardjaf44c822007-11-25 14:01:38 +0000451Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid)
452{
453 struct mutex_info* const p = mutex_get(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000454 if (p)
455 {
456 return (p->recursion_count > 0 && p->owner == tid);
457 }
458 return False;
459}
460
461const VectorClock* mutex_get_last_vc(const Addr mutex)
462{
463 struct mutex_info* const p = mutex_get(mutex);
464 return p ? &p->vc : 0;
465}
466
467int mutex_get_recursion_count(const Addr mutex)
468{
469 struct mutex_info* const p = mutex_get(mutex);
470 tl_assert(p);
471 return p->recursion_count;
472}
473
474/**
bart301c3112008-02-24 18:22:37 +0000475 * Call this function when thread tid stops to exist, such that the
sewardjaf44c822007-11-25 14:01:38 +0000476 * "last owner" field can be cleared if it still refers to that thread.
sewardjaf44c822007-11-25 14:01:38 +0000477 */
bart301c3112008-02-24 18:22:37 +0000478void mutex_thread_delete(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000479{
480 int i;
481 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
482 {
483 struct mutex_info* const p = &s_mutex[i];
bart5357fcb2008-02-27 15:46:00 +0000484 if (p->mutex && p->owner == tid && p->recursion_count > 0)
sewardjaf44c822007-11-25 14:01:38 +0000485 {
bart5357fcb2008-02-27 15:46:00 +0000486 MutexErrInfo MEI
487 = { p->mutex, p->recursion_count, p->owner };
488 VG_(maybe_record_error)(VG_(get_running_tid)(),
489 MutexErr,
490 VG_(get_IP)(VG_(get_running_tid)()),
491 "Mutex still locked at thread exit",
492 &MEI);
sewardjaf44c822007-11-25 14:01:38 +0000493 p->owner = VG_INVALID_THREADID;
494 }
495 }
496}
497
498void mutex_stop_using_mem(const Addr a1, const Addr a2)
499{
500 unsigned i;
501 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
502 {
503 if (a1 <= s_mutex[i].mutex && s_mutex[i].mutex < a2)
504 {
505 tl_assert(s_mutex[i].mutex + s_mutex[i].size <= a2);
506 mutex_destroy(&s_mutex[i]);
507 }
508 }
509}
510
511ULong get_mutex_lock_count(void)
512{
513 return s_mutex_lock_count;
514}
sewardj347eeba2008-01-21 14:19:07 +0000515
516
517/*
518 * Local variables:
519 * c-basic-offset: 3
520 * End:
521 */