blob: bef880f825058c75e74237e0b978a0ddb15a9fbd [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
sewardjaf44c822007-11-25 14:01:38 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00004
sewardj9eecbbb2010-05-03 21:37:12 +00005 Copyright (C) 2006-2010 Bart Van Assche <bart.vanassche@gmail.com>.
sewardjaf44c822007-11-25 14:01:38 +00006
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
bart28230a32008-02-29 17:27:03 +000026#include "drd_clientobj.h"
sewardjaf44c822007-11-25 14:01:38 +000027#include "drd_cond.h"
28#include "drd_error.h"
29#include "drd_mutex.h"
bart46b5fce2008-06-28 13:01:30 +000030#include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
31#include "pub_tool_libcassert.h" /* tl_assert() */
bart850f1992010-05-29 18:43:21 +000032#include "pub_tool_libcbase.h" /* VG_(memcmp)() */
bart46b5fce2008-06-28 13:01:30 +000033#include "pub_tool_libcprint.h" /* VG_(printf)() */
34#include "pub_tool_machine.h" /* VG_(get_IP)() */
bart46b5fce2008-06-28 13:01:30 +000035#include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
sewardjaf44c822007-11-25 14:01:38 +000036
37
bart46b5fce2008-06-28 13:01:30 +000038/* Local functions. */
bart28230a32008-02-29 17:27:03 +000039
bartdc1ef032009-02-15 14:18:02 +000040static void DRD_(cond_cleanup)(struct cond_info* p);
bart28230a32008-02-29 17:27:03 +000041
42
bart46b5fce2008-06-28 13:01:30 +000043/* Local variables. */
bart28230a32008-02-29 17:27:03 +000044
bartdc1ef032009-02-15 14:18:02 +000045static Bool DRD_(s_report_signal_unlocked) = True;
46static Bool DRD_(s_trace_cond);
sewardjaf44c822007-11-25 14:01:38 +000047
48
bart850f1992010-05-29 18:43:21 +000049/* Global variables. */
50
51Addr DRD_(pthread_cond_initializer);
52int DRD_(pthread_cond_initializer_size);
53
54
bart46b5fce2008-06-28 13:01:30 +000055/* Function definitions. */
bart28230a32008-02-29 17:27:03 +000056
bartdc1ef032009-02-15 14:18:02 +000057void DRD_(cond_set_report_signal_unlocked)(const Bool r)
bart764dea22009-02-15 13:16:52 +000058{
bartbedfd232009-03-26 19:07:15 +000059 DRD_(s_report_signal_unlocked) = r;
bart764dea22009-02-15 13:16:52 +000060}
61
bartdc1ef032009-02-15 14:18:02 +000062void DRD_(cond_set_trace)(const Bool trace_cond)
sewardjaf44c822007-11-25 14:01:38 +000063{
bartbedfd232009-03-26 19:07:15 +000064 DRD_(s_trace_cond) = trace_cond;
sewardjaf44c822007-11-25 14:01:38 +000065}
66
67static
bartdc1ef032009-02-15 14:18:02 +000068void DRD_(cond_initialize)(struct cond_info* const p, const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +000069{
bartbedfd232009-03-26 19:07:15 +000070 tl_assert(cond != 0);
bart62cc2322010-03-07 10:54:21 +000071 tl_assert(p->a1 == cond);
72 tl_assert(p->type == ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +000073
bartbedfd232009-03-26 19:07:15 +000074 p->cleanup = (void(*)(DrdClientobj*))(DRD_(cond_cleanup));
75 p->delete_thread = 0;
76 p->waiter_count = 0;
77 p->mutex = 0;
sewardjaf44c822007-11-25 14:01:38 +000078}
79
bart195e41f2009-02-15 11:34:57 +000080/**
81 * Free the memory that was allocated by cond_initialize(). Called by
82 * DRD_(clientobj_remove)().
bart28230a32008-02-29 17:27:03 +000083 */
bartdc1ef032009-02-15 14:18:02 +000084static void DRD_(cond_cleanup)(struct cond_info* p)
bart28230a32008-02-29 17:27:03 +000085{
bartbedfd232009-03-26 19:07:15 +000086 tl_assert(p);
87 if (p->mutex)
88 {
89 struct mutex_info* q;
90 q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex);
bartbedfd232009-03-26 19:07:15 +000091 {
bart62cc2322010-03-07 10:54:21 +000092 CondDestrErrInfo cde = {
93 DRD_(thread_get_running_tid)(),
94 p->a1,
95 q ? q->a1 : 0,
96 q ? q->owner : DRD_INVALID_THREADID
97 };
bartbedfd232009-03-26 19:07:15 +000098 VG_(maybe_record_error)(VG_(get_running_tid)(),
99 CondDestrErr,
100 VG_(get_IP)(VG_(get_running_tid)()),
101 "Destroying condition variable that is being"
102 " waited upon",
103 &cde);
104 }
105 }
bart28230a32008-02-29 17:27:03 +0000106}
107
bart62cc2322010-03-07 10:54:21 +0000108/**
109 * Report that the synchronization object at address 'addr' is of the
110 * wrong type.
111 */
112static void wrong_type(const Addr addr)
113{
114 GenericErrInfo gei = {
115 .tid = DRD_(thread_get_running_tid)(),
116 .addr = addr,
117 };
118 VG_(maybe_record_error)(VG_(get_running_tid)(),
119 GenericErr,
120 VG_(get_IP)(VG_(get_running_tid)()),
121 "wrong type of synchronization object",
122 &gei);
123}
124
bartd45d9952009-05-31 18:53:54 +0000125static struct cond_info* cond_get_or_allocate(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000126{
bartbedfd232009-03-26 19:07:15 +0000127 struct cond_info *p;
bart28230a32008-02-29 17:27:03 +0000128
bartbedfd232009-03-26 19:07:15 +0000129 tl_assert(offsetof(DrdClientobj, cond) == 0);
130 p = &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
bart62cc2322010-03-07 10:54:21 +0000131 if (p)
132 return p;
133
134 if (DRD_(clientobj_present)(cond, cond + 1))
bartbedfd232009-03-26 19:07:15 +0000135 {
bart62cc2322010-03-07 10:54:21 +0000136 wrong_type(cond);
137 return 0;
bartbedfd232009-03-26 19:07:15 +0000138 }
bart62cc2322010-03-07 10:54:21 +0000139
140 p = &(DRD_(clientobj_add)(cond, ClientCondvar)->cond);
141 DRD_(cond_initialize)(p, cond);
bartbedfd232009-03-26 19:07:15 +0000142 return p;
sewardjaf44c822007-11-25 14:01:38 +0000143}
144
bartd45d9952009-05-31 18:53:54 +0000145struct cond_info* DRD_(cond_get)(const Addr cond)
bart28230a32008-02-29 17:27:03 +0000146{
bartbedfd232009-03-26 19:07:15 +0000147 tl_assert(offsetof(DrdClientobj, cond) == 0);
148 return &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
bart28230a32008-02-29 17:27:03 +0000149}
150
151/** Called before pthread_cond_init(). */
bartdc1ef032009-02-15 14:18:02 +0000152void DRD_(cond_pre_init)(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000153{
bartbedfd232009-03-26 19:07:15 +0000154 struct cond_info* p;
bart72b751c2008-03-01 13:44:24 +0000155
bartbedfd232009-03-26 19:07:15 +0000156 if (DRD_(s_trace_cond))
157 {
158 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000159 "[%d] cond_init cond 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000160 DRD_(thread_get_running_tid)(),
161 cond);
162 }
bart72b751c2008-03-01 13:44:24 +0000163
bartbedfd232009-03-26 19:07:15 +0000164 p = DRD_(cond_get)(cond);
bart72b751c2008-03-01 13:44:24 +0000165
bartbedfd232009-03-26 19:07:15 +0000166 if (p)
167 {
bartd45d9952009-05-31 18:53:54 +0000168 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
bartbedfd232009-03-26 19:07:15 +0000169 VG_(maybe_record_error)(VG_(get_running_tid)(),
170 CondErr,
171 VG_(get_IP)(VG_(get_running_tid)()),
172 "initialized twice",
173 &cei);
174 }
bart72b751c2008-03-01 13:44:24 +0000175
bartd45d9952009-05-31 18:53:54 +0000176 p = cond_get_or_allocate(cond);
sewardjaf44c822007-11-25 14:01:38 +0000177}
178
bart28230a32008-02-29 17:27:03 +0000179/** Called after pthread_cond_destroy(). */
bartdc1ef032009-02-15 14:18:02 +0000180void DRD_(cond_post_destroy)(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000181{
bartbedfd232009-03-26 19:07:15 +0000182 struct cond_info* p;
bart72b751c2008-03-01 13:44:24 +0000183
bartbedfd232009-03-26 19:07:15 +0000184 if (DRD_(s_trace_cond))
185 {
186 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000187 "[%d] cond_destroy cond 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000188 DRD_(thread_get_running_tid)(),
189 cond);
190 }
sewardjaf44c822007-11-25 14:01:38 +0000191
bartbedfd232009-03-26 19:07:15 +0000192 p = DRD_(cond_get)(cond);
193 if (p == 0)
194 {
bartd45d9952009-05-31 18:53:54 +0000195 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
bartbedfd232009-03-26 19:07:15 +0000196 VG_(maybe_record_error)(VG_(get_running_tid)(),
197 CondErr,
198 VG_(get_IP)(VG_(get_running_tid)()),
199 "not a condition variable",
200 &cei);
201 return;
202 }
sewardjaf44c822007-11-25 14:01:38 +0000203
bartbedfd232009-03-26 19:07:15 +0000204 if (p->waiter_count != 0)
205 {
bartd45d9952009-05-31 18:53:54 +0000206 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
bartbedfd232009-03-26 19:07:15 +0000207 VG_(maybe_record_error)(VG_(get_running_tid)(),
208 CondErr,
209 VG_(get_IP)(VG_(get_running_tid)()),
210 "destruction of condition variable being waited"
211 " upon",
212 &cei);
213 }
bart72b751c2008-03-01 13:44:24 +0000214
bartbedfd232009-03-26 19:07:15 +0000215 DRD_(clientobj_remove)(p->a1, ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +0000216}
217
bart62cc2322010-03-07 10:54:21 +0000218/**
219 * Called before pthread_cond_wait(). Note: before this function is called,
220 * mutex_unlock() has already been called from drd_clientreq.c.
bart08e6d6a2008-06-28 16:28:49 +0000221 */
bart62cc2322010-03-07 10:54:21 +0000222void DRD_(cond_pre_wait)(const Addr cond, const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000223{
bartbedfd232009-03-26 19:07:15 +0000224 struct cond_info* p;
225 struct mutex_info* q;
sewardjaf44c822007-11-25 14:01:38 +0000226
bartbedfd232009-03-26 19:07:15 +0000227 if (DRD_(s_trace_cond))
228 {
229 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000230 "[%d] cond_pre_wait cond 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000231 DRD_(thread_get_running_tid)(),
232 cond);
233 }
bart3b1ee452008-02-29 19:28:15 +0000234
bartd45d9952009-05-31 18:53:54 +0000235 p = cond_get_or_allocate(cond);
bart62cc2322010-03-07 10:54:21 +0000236 if (!p)
237 {
238 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
239 VG_(maybe_record_error)(VG_(get_running_tid)(),
240 CondErr,
241 VG_(get_IP)(VG_(get_running_tid)()),
242 "not a condition variable",
243 &cei);
244 return;
245 }
bart3b1ee452008-02-29 19:28:15 +0000246
bartbedfd232009-03-26 19:07:15 +0000247 if (p->waiter_count == 0)
248 {
249 p->mutex = mutex;
250 }
251 else if (p->mutex != mutex)
252 {
253 CondWaitErrInfo cwei
bartd45d9952009-05-31 18:53:54 +0000254 = { .tid = DRD_(thread_get_running_tid)(),
255 .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex };
bartbedfd232009-03-26 19:07:15 +0000256 VG_(maybe_record_error)(VG_(get_running_tid)(),
257 CondWaitErr,
258 VG_(get_IP)(VG_(get_running_tid)()),
259 "Inconsistent association of condition variable"
260 " and mutex",
261 &cwei);
262 }
263 tl_assert(p->mutex);
264 q = DRD_(mutex_get)(p->mutex);
265 if (q
266 && q->owner == DRD_(thread_get_running_tid)() && q->recursion_count > 0)
267 {
268 const ThreadId vg_tid = VG_(get_running_tid)();
bartd45d9952009-05-31 18:53:54 +0000269 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
270 q->a1, q->recursion_count, q->owner };
bartbedfd232009-03-26 19:07:15 +0000271 VG_(maybe_record_error)(vg_tid,
272 MutexErr,
273 VG_(get_IP)(vg_tid),
274 "Mutex locked recursively",
275 &MEI);
276 }
277 else if (q == 0)
278 {
279 DRD_(not_a_mutex)(p->mutex);
280 }
bart08e6d6a2008-06-28 16:28:49 +0000281
bart62cc2322010-03-07 10:54:21 +0000282 ++p->waiter_count;
sewardjaf44c822007-11-25 14:01:38 +0000283}
284
bart62cc2322010-03-07 10:54:21 +0000285/**
286 * Called after pthread_cond_wait().
287 */
288void DRD_(cond_post_wait)(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000289{
bartbedfd232009-03-26 19:07:15 +0000290 struct cond_info* p;
sewardjaf44c822007-11-25 14:01:38 +0000291
bartbedfd232009-03-26 19:07:15 +0000292 if (DRD_(s_trace_cond))
293 {
294 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000295 "[%d] cond_post_wait cond 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000296 DRD_(thread_get_running_tid)(),
297 cond);
298 }
bart3b1ee452008-02-29 19:28:15 +0000299
bartbedfd232009-03-26 19:07:15 +0000300 p = DRD_(cond_get)(cond);
bart62cc2322010-03-07 10:54:21 +0000301 if (!p)
bartbedfd232009-03-26 19:07:15 +0000302 {
bart62cc2322010-03-07 10:54:21 +0000303 struct mutex_info* q;
304 q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex);
barta9c55082008-06-28 16:55:35 +0000305 {
bart62cc2322010-03-07 10:54:21 +0000306 CondDestrErrInfo cde = {
307 DRD_(thread_get_running_tid)(),
308 p->a1,
309 q ? q->a1 : 0,
310 q ? q->owner : DRD_INVALID_THREADID
311 };
312 VG_(maybe_record_error)(VG_(get_running_tid)(),
313 CondDestrErr,
314 VG_(get_IP)(VG_(get_running_tid)()),
315 "condition variable has been destroyed while"
316 " being waited upon",
317 &cde);
barta9c55082008-06-28 16:55:35 +0000318 }
bart62cc2322010-03-07 10:54:21 +0000319 return;
bartbedfd232009-03-26 19:07:15 +0000320 }
bart62cc2322010-03-07 10:54:21 +0000321
322 if (p->waiter_count > 0)
323 {
324 --p->waiter_count;
325 if (p->waiter_count == 0)
326 {
327 p->mutex = 0;
328 }
329 }
sewardjaf44c822007-11-25 14:01:38 +0000330}
331
bart62cc2322010-03-07 10:54:21 +0000332static void cond_signal(const DrdThreadId tid, struct cond_info* const cond_p)
sewardjaf44c822007-11-25 14:01:38 +0000333{
bartbedfd232009-03-26 19:07:15 +0000334 const ThreadId vg_tid = VG_(get_running_tid)();
335 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
bart3b1ee452008-02-29 19:28:15 +0000336
bart62cc2322010-03-07 10:54:21 +0000337 tl_assert(cond_p);
338
339 if (cond_p->waiter_count > 0)
bartbedfd232009-03-26 19:07:15 +0000340 {
341 if (DRD_(s_report_signal_unlocked)
bart62cc2322010-03-07 10:54:21 +0000342 && ! DRD_(mutex_is_locked_by)(cond_p->mutex, drd_tid))
bartbedfd232009-03-26 19:07:15 +0000343 {
bart62cc2322010-03-07 10:54:21 +0000344 /*
345 * A signal is sent while the associated mutex has not been locked.
346 * This can indicate but is not necessarily a race condition.
347 */
348 CondRaceErrInfo cei = { .tid = DRD_(thread_get_running_tid)(),
349 .cond = cond_p->a1,
350 .mutex = cond_p->mutex,
351 };
352 VG_(maybe_record_error)(vg_tid,
353 CondRaceErr,
354 VG_(get_IP)(vg_tid),
355 "CondErr",
356 &cei);
bartbedfd232009-03-26 19:07:15 +0000357 }
358 }
359 else
360 {
bart62cc2322010-03-07 10:54:21 +0000361 /*
362 * No other thread is waiting for the signal, hence the signal will
363 * be lost. This is normal in a POSIX threads application.
364 */
bartbedfd232009-03-26 19:07:15 +0000365 }
sewardjaf44c822007-11-25 14:01:38 +0000366}
367
bart62cc2322010-03-07 10:54:21 +0000368static void not_initialized(Addr const cond)
369{
370 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
371 VG_(maybe_record_error)(VG_(get_running_tid)(),
372 CondErr,
373 VG_(get_IP)(VG_(get_running_tid)()),
374 "condition variable has not been initialized",
375 &cei);
376}
377
bart3b1ee452008-02-29 19:28:15 +0000378/** Called before pthread_cond_signal(). */
bartdc1ef032009-02-15 14:18:02 +0000379void DRD_(cond_pre_signal)(Addr const cond)
bart3b1ee452008-02-29 19:28:15 +0000380{
bart62cc2322010-03-07 10:54:21 +0000381 struct cond_info* p;
382
383 p = DRD_(cond_get)(cond);
bartbedfd232009-03-26 19:07:15 +0000384 if (DRD_(s_trace_cond))
385 {
386 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000387 "[%d] cond_signal cond 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000388 DRD_(thread_get_running_tid)(),
389 cond);
390 }
bart3b1ee452008-02-29 19:28:15 +0000391
bart850f1992010-05-29 18:43:21 +0000392 tl_assert(DRD_(pthread_cond_initializer));
393 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
394 DRD_(pthread_cond_initializer_size)) != 0)
bart62cc2322010-03-07 10:54:21 +0000395 {
396 not_initialized(cond);
397 return;
398 }
399
bart850f1992010-05-29 18:43:21 +0000400 if (!p)
401 p = cond_get_or_allocate(cond);
402
bart62cc2322010-03-07 10:54:21 +0000403 cond_signal(DRD_(thread_get_running_tid)(), p);
bart3b1ee452008-02-29 19:28:15 +0000404}
405
bart28230a32008-02-29 17:27:03 +0000406/** Called before pthread_cond_broadcast(). */
bartdc1ef032009-02-15 14:18:02 +0000407void DRD_(cond_pre_broadcast)(Addr const cond)
sewardjaf44c822007-11-25 14:01:38 +0000408{
bart62cc2322010-03-07 10:54:21 +0000409 struct cond_info* p;
410
bartbedfd232009-03-26 19:07:15 +0000411 if (DRD_(s_trace_cond))
412 {
413 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000414 "[%d] cond_broadcast cond 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000415 DRD_(thread_get_running_tid)(),
416 cond);
417 }
bart3b1ee452008-02-29 19:28:15 +0000418
bart62cc2322010-03-07 10:54:21 +0000419 p = DRD_(cond_get)(cond);
bart850f1992010-05-29 18:43:21 +0000420 tl_assert(DRD_(pthread_cond_initializer));
421 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
422 DRD_(pthread_cond_initializer_size)) != 0)
bart62cc2322010-03-07 10:54:21 +0000423 {
424 not_initialized(cond);
425 return;
426 }
427
bart850f1992010-05-29 18:43:21 +0000428 if (!p)
429 p = cond_get_or_allocate(cond);
430
bart62cc2322010-03-07 10:54:21 +0000431 cond_signal(DRD_(thread_get_running_tid)(), p);
sewardjaf44c822007-11-25 14:01:38 +0000432}