blob: 304f36531a839171e4d5310d1ea1d5309ed06834 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
4 Copyright (C) 2006-2007 Bart Van Assche
5 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
50// Local variables.
51
52static Bool s_trace_mutex;
53static ULong s_mutex_lock_count;
54struct mutex_info s_mutex[256];
55
56
57// Function definitions.
58
59void mutex_set_trace(const Bool trace_mutex)
60{
61 tl_assert(!! trace_mutex == trace_mutex);
62 s_trace_mutex = trace_mutex;
63}
64
65static
66void mutex_initialize(struct mutex_info* const p,
67 const Addr mutex,
sewardj721ad7b2007-11-30 08:30:29 +000068 const SizeT size,
69 const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000070{
71 tl_assert(mutex != 0);
72 tl_assert(size > 0);
sewardj721ad7b2007-11-30 08:30:29 +000073 tl_assert(mutex_type == mutex_type_mutex
74 || mutex_type == mutex_type_spinlock);
sewardjaf44c822007-11-25 14:01:38 +000075
76 p->mutex = mutex;
77 p->size = size;
sewardj721ad7b2007-11-30 08:30:29 +000078 p->mutex_type = mutex_type;
sewardjaf44c822007-11-25 14:01:38 +000079 p->recursion_count = 0;
80 p->owner = DRD_INVALID_THREADID;
81 vc_init(&p->vc, 0, 0);
82}
83
84static
sewardj721ad7b2007-11-30 08:30:29 +000085struct mutex_info*
86mutex_get_or_allocate(const Addr mutex,
87 const SizeT size,
88 const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000089{
90 int i;
sewardj721ad7b2007-11-30 08:30:29 +000091
92 tl_assert(mutex_type == mutex_type_mutex
93 || mutex_type == mutex_type_spinlock);
94
sewardjaf44c822007-11-25 14:01:38 +000095 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
sewardj721ad7b2007-11-30 08:30:29 +000096 {
sewardjaf44c822007-11-25 14:01:38 +000097 if (s_mutex[i].mutex == mutex)
sewardj721ad7b2007-11-30 08:30:29 +000098 {
99 tl_assert(s_mutex[i].mutex_type == mutex_type);
100 tl_assert(s_mutex[i].size == size);
sewardjaf44c822007-11-25 14:01:38 +0000101 return &s_mutex[i];
sewardj721ad7b2007-11-30 08:30:29 +0000102 }
103 }
sewardjaf44c822007-11-25 14:01:38 +0000104 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
105 {
106 if (s_mutex[i].mutex == 0)
107 {
sewardj721ad7b2007-11-30 08:30:29 +0000108 mutex_initialize(&s_mutex[i], mutex, size, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000109 drd_start_suppression(mutex, mutex + size,
110 mutex_get_typename(&s_mutex[i]));
111 return &s_mutex[i];
112 }
113 }
114 tl_assert(0);
115 return 0;
116}
117
sewardj721ad7b2007-11-30 08:30:29 +0000118struct mutex_info*
119mutex_init(const Addr mutex, const SizeT size, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000120{
121 struct mutex_info* mutex_p;
122
123 tl_assert(mutex_get(mutex) == 0);
sewardj721ad7b2007-11-30 08:30:29 +0000124 tl_assert(mutex_type == mutex_type_mutex
125 || mutex_type == mutex_type_spinlock);
126 mutex_p = mutex_get_or_allocate(mutex, size, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000127
128 if (s_trace_mutex)
129 {
130 const ThreadId vg_tid = VG_(get_running_tid)();
131 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
132 VG_(message)(Vg_DebugMsg,
133 "drd_post_mutex_init tid = %d/%d, %s 0x%lx",
134 vg_tid, drd_tid,
135 mutex_get_typename(mutex_p),
136 mutex);
137 }
138
139 return mutex_p;
140}
141
142void mutex_destroy(struct mutex_info* const p)
143{
144 if (s_trace_mutex)
145 {
146 const ThreadId vg_tid = VG_(get_running_tid)();
147 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
148 VG_(message)(Vg_DebugMsg,
149 "drd_pre_mutex_destroy tid = %d/%d, %s 0x%lx",
150 vg_tid, drd_tid,
151 mutex_get_typename(p),
152 p->mutex);
153 }
154
155 drd_finish_suppression(p->mutex, p->mutex + p->size);
156
157 vc_cleanup(&p->vc);
158 p->mutex = 0;
159}
160
161struct mutex_info* mutex_get(const Addr mutex)
162{
163 int i;
164 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
165 if (s_mutex[i].mutex == mutex)
166 return &s_mutex[i];
167 return 0;
168}
169
170/**
171 * Update mutex_info state when locking the pthread_mutex_t mutex.
172 * Note: this function must be called after pthread_mutex_lock() has been
173 * called, or a race condition is triggered !
174 */
sewardj721ad7b2007-11-30 08:30:29 +0000175int mutex_lock(const Addr mutex, const SizeT size, MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000176{
177 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)());
sewardj721ad7b2007-11-30 08:30:29 +0000178 struct mutex_info* const p = mutex_get_or_allocate(mutex, size, mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000179 const DrdThreadId last_owner = p->owner;
180
181 if (s_trace_mutex)
182 {
183 const ThreadId tid = DrdThreadIdToVgThreadId(drd_tid);
184 VG_(message)(Vg_DebugMsg,
185 "drd_post_mutex_lock tid = %d/%d, %s 0x%lx rc %d owner %d",
186 tid,
187 drd_tid,
188 mutex_get_typename(p),
189 mutex,
190 p ? p->recursion_count : 0,
191 p ? p->owner : VG_INVALID_THREADID);
192 }
193
sewardj721ad7b2007-11-30 08:30:29 +0000194 tl_assert(mutex_type == mutex_type_mutex
195 || mutex_type == mutex_type_spinlock);
196 tl_assert(p->mutex_type == mutex_type);
197 tl_assert(p->size == size);
198
199 if (p->recursion_count >= 1 && mutex_type == mutex_type_spinlock)
sewardjaf44c822007-11-25 14:01:38 +0000200 {
201 // TO DO: tell the user in a more friendly way that it is not allowed to
202 // lock spinlocks recursively.
203 tl_assert(0);
204 }
205
206 if (p->recursion_count == 0)
207 {
208 p->owner = drd_tid;
209 s_mutex_lock_count++;
210 }
211 else if (p->owner != drd_tid)
212 {
213 VG_(message)(Vg_DebugMsg,
214 "The impossible happened: mutex 0x%lx is locked"
215 " simultaneously by two threads (recursion count %d,"
216 " owners %d and %d) !",
217 p->mutex, p->recursion_count, p->owner, drd_tid);
218 tl_assert(0);
219 }
220 p->recursion_count++;
221
222 if (p->recursion_count == 1)
223 {
224 if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID)
225 thread_combine_vc2(drd_tid, mutex_get_last_vc(mutex));
226 thread_new_segment(drd_tid);
227 }
228
229 return p->recursion_count;
230}
231
232/**
233 * Update mutex_info state when unlocking the pthread_mutex_t mutex.
234 * Note: this function must be called before pthread_mutex_unlock() is called,
235 * or a race condition is triggered !
236 * @param mutex Pointer to pthread_mutex_t data structure in the client space.
237 * @param tid ThreadId of the thread calling pthread_mutex_unlock().
238 * @param vc Pointer to the current vector clock of thread tid.
239 */
sewardj721ad7b2007-11-30 08:30:29 +0000240int mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000241{
242 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)());
243 const ThreadId vg_tid = DrdThreadIdToVgThreadId(drd_tid);
244 const VectorClock* const vc = thread_get_vc(drd_tid);
245 struct mutex_info* const p = mutex_get(mutex);
246
247 if (s_trace_mutex)
248 {
249 VG_(message)(Vg_DebugMsg,
250 "drd_pre_mutex_unlock tid = %d/%d, %s 0x%lx rc %d",
251 vg_tid, drd_tid,
252 mutex_get_typename(p),
253 mutex,
254 p->recursion_count,
255 p->owner);
256 }
257
258 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000259 tl_assert(p->mutex_type == mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000260 tl_assert(p->owner != DRD_INVALID_THREADID);
sewardj721ad7b2007-11-30 08:30:29 +0000261 tl_assert(mutex_type == mutex_type_mutex
262 || mutex_type == mutex_type_spinlock);
263
sewardjaf44c822007-11-25 14:01:38 +0000264 if (p->owner != drd_tid)
265 {
266 MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner };
267 VG_(maybe_record_error)(vg_tid,
268 MutexErr,
269 VG_(get_IP)(vg_tid),
270 "Mutex not unlocked by owner thread",
271 &MEI);
272 }
273 p->recursion_count--;
274 tl_assert(p->recursion_count >= 0);
275 if (p->recursion_count == 0)
276 {
277 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
278 /* current vector clock of the thread such that it is available when */
279 /* this mutex is locked again. */
280 vc_copy(&p->vc, vc);
281
282 thread_new_segment(drd_tid);
283 }
284 return p->recursion_count;
285}
286
287const char* mutex_get_typename(struct mutex_info* const p)
288{
289 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000290
291 switch (p->mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000292 {
sewardj721ad7b2007-11-30 08:30:29 +0000293 case mutex_type_mutex:
sewardjaf44c822007-11-25 14:01:38 +0000294 return "mutex";
sewardj721ad7b2007-11-30 08:30:29 +0000295 case mutex_type_spinlock:
sewardjaf44c822007-11-25 14:01:38 +0000296 return "spinlock";
297 default:
298 tl_assert(0);
299 }
300 return "?";
301}
302
303Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid)
304{
305 struct mutex_info* const p = mutex_get(mutex);
306 tl_assert(p);
307 if (p)
308 {
309 return (p->recursion_count > 0 && p->owner == tid);
310 }
311 return False;
312}
313
314const VectorClock* mutex_get_last_vc(const Addr mutex)
315{
316 struct mutex_info* const p = mutex_get(mutex);
317 return p ? &p->vc : 0;
318}
319
320int mutex_get_recursion_count(const Addr mutex)
321{
322 struct mutex_info* const p = mutex_get(mutex);
323 tl_assert(p);
324 return p->recursion_count;
325}
326
327/**
328 * Call this function when thread threadid stops to exist, such that the
329 * "last owner" field can be cleared if it still refers to that thread.
330 * TO DO: print an error message if a thread exits while it still has some
331 * mutexes locked.
332 */
333void mutex_thread_delete(const DrdThreadId threadid)
334{
335 int i;
336 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
337 {
338 struct mutex_info* const p = &s_mutex[i];
339 if (p->mutex && p->owner == threadid)
340 {
341 p->owner = VG_INVALID_THREADID;
342 }
343 }
344}
345
346void mutex_stop_using_mem(const Addr a1, const Addr a2)
347{
348 unsigned i;
349 for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++)
350 {
351 if (a1 <= s_mutex[i].mutex && s_mutex[i].mutex < a2)
352 {
353 tl_assert(s_mutex[i].mutex + s_mutex[i].size <= a2);
354 mutex_destroy(&s_mutex[i]);
355 }
356 }
357}
358
359ULong get_mutex_lock_count(void)
360{
361 return s_mutex_lock_count;
362}