blob: 70d457e80e1b748ad4e9a7407f945ba6926cabcb [file] [log] [blame]
bart922304f2011-03-13 12:02:44 +00001/* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
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
bart922304f2011-03-13 12:02:44 +00005 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
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 {
bart7a2cc3c2011-04-30 07:27:41 +0000303 CondDestrErrInfo cde = {
304 DRD_(thread_get_running_tid)(), cond, 0, DRD_INVALID_THREADID
305 };
306 VG_(maybe_record_error)(VG_(get_running_tid)(),
307 CondDestrErr,
308 VG_(get_IP)(VG_(get_running_tid)()),
309 "condition variable has been destroyed while"
310 " being waited upon",
311 &cde);
bart62cc2322010-03-07 10:54:21 +0000312 return;
bartbedfd232009-03-26 19:07:15 +0000313 }
bart62cc2322010-03-07 10:54:21 +0000314
315 if (p->waiter_count > 0)
316 {
317 --p->waiter_count;
318 if (p->waiter_count == 0)
319 {
320 p->mutex = 0;
321 }
322 }
sewardjaf44c822007-11-25 14:01:38 +0000323}
324
bart62cc2322010-03-07 10:54:21 +0000325static void cond_signal(const DrdThreadId tid, struct cond_info* const cond_p)
sewardjaf44c822007-11-25 14:01:38 +0000326{
bartbedfd232009-03-26 19:07:15 +0000327 const ThreadId vg_tid = VG_(get_running_tid)();
328 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
bart3b1ee452008-02-29 19:28:15 +0000329
bart62cc2322010-03-07 10:54:21 +0000330 tl_assert(cond_p);
331
332 if (cond_p->waiter_count > 0)
bartbedfd232009-03-26 19:07:15 +0000333 {
334 if (DRD_(s_report_signal_unlocked)
bart62cc2322010-03-07 10:54:21 +0000335 && ! DRD_(mutex_is_locked_by)(cond_p->mutex, drd_tid))
bartbedfd232009-03-26 19:07:15 +0000336 {
bart62cc2322010-03-07 10:54:21 +0000337 /*
338 * A signal is sent while the associated mutex has not been locked.
339 * This can indicate but is not necessarily a race condition.
340 */
341 CondRaceErrInfo cei = { .tid = DRD_(thread_get_running_tid)(),
342 .cond = cond_p->a1,
343 .mutex = cond_p->mutex,
344 };
345 VG_(maybe_record_error)(vg_tid,
346 CondRaceErr,
347 VG_(get_IP)(vg_tid),
348 "CondErr",
349 &cei);
bartbedfd232009-03-26 19:07:15 +0000350 }
351 }
352 else
353 {
bart62cc2322010-03-07 10:54:21 +0000354 /*
355 * No other thread is waiting for the signal, hence the signal will
356 * be lost. This is normal in a POSIX threads application.
357 */
bartbedfd232009-03-26 19:07:15 +0000358 }
sewardjaf44c822007-11-25 14:01:38 +0000359}
360
bart62cc2322010-03-07 10:54:21 +0000361static void not_initialized(Addr const cond)
362{
363 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
364 VG_(maybe_record_error)(VG_(get_running_tid)(),
365 CondErr,
366 VG_(get_IP)(VG_(get_running_tid)()),
367 "condition variable has not been initialized",
368 &cei);
369}
370
bart3b1ee452008-02-29 19:28:15 +0000371/** Called before pthread_cond_signal(). */
bartdc1ef032009-02-15 14:18:02 +0000372void DRD_(cond_pre_signal)(Addr const cond)
bart3b1ee452008-02-29 19:28:15 +0000373{
bart62cc2322010-03-07 10:54:21 +0000374 struct cond_info* p;
375
376 p = DRD_(cond_get)(cond);
bartbedfd232009-03-26 19:07:15 +0000377 if (DRD_(s_trace_cond))
378 {
379 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000380 "[%d] cond_signal cond 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000381 DRD_(thread_get_running_tid)(),
382 cond);
383 }
bart3b1ee452008-02-29 19:28:15 +0000384
bart850f1992010-05-29 18:43:21 +0000385 tl_assert(DRD_(pthread_cond_initializer));
386 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
387 DRD_(pthread_cond_initializer_size)) != 0)
bart62cc2322010-03-07 10:54:21 +0000388 {
389 not_initialized(cond);
390 return;
391 }
392
bart850f1992010-05-29 18:43:21 +0000393 if (!p)
394 p = cond_get_or_allocate(cond);
395
bart62cc2322010-03-07 10:54:21 +0000396 cond_signal(DRD_(thread_get_running_tid)(), p);
bart3b1ee452008-02-29 19:28:15 +0000397}
398
bart28230a32008-02-29 17:27:03 +0000399/** Called before pthread_cond_broadcast(). */
bartdc1ef032009-02-15 14:18:02 +0000400void DRD_(cond_pre_broadcast)(Addr const cond)
sewardjaf44c822007-11-25 14:01:38 +0000401{
bart62cc2322010-03-07 10:54:21 +0000402 struct cond_info* p;
403
bartbedfd232009-03-26 19:07:15 +0000404 if (DRD_(s_trace_cond))
405 {
406 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000407 "[%d] cond_broadcast cond 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000408 DRD_(thread_get_running_tid)(),
409 cond);
410 }
bart3b1ee452008-02-29 19:28:15 +0000411
bart62cc2322010-03-07 10:54:21 +0000412 p = DRD_(cond_get)(cond);
bart850f1992010-05-29 18:43:21 +0000413 tl_assert(DRD_(pthread_cond_initializer));
414 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
415 DRD_(pthread_cond_initializer_size)) != 0)
bart62cc2322010-03-07 10:54:21 +0000416 {
417 not_initialized(cond);
418 return;
419 }
420
bart850f1992010-05-29 18:43:21 +0000421 if (!p)
422 p = cond_get_or_allocate(cond);
423
bart62cc2322010-03-07 10:54:21 +0000424 cond_signal(DRD_(thread_get_running_tid)(), p);
sewardjaf44c822007-11-25 14:01:38 +0000425}