blob: 23d7c8e823fdbcb562c436e60f5612f3a606448d [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00003
sewardj03f8d3f2012-08-05 15:46:46 +00004 Copyright (C) 2006-2012 Bart Van Assche <bvanassche@acm.org>.
sewardjaf44c822007-11-25 14:01:38 +00005
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
20
21 The GNU General Public License is contained in the file COPYING.
22*/
23
24
bart28230a32008-02-29 17:27:03 +000025#include "drd_clientobj.h"
sewardjaf44c822007-11-25 14:01:38 +000026#include "drd_cond.h"
27#include "drd_error.h"
28#include "drd_mutex.h"
bart46b5fce2008-06-28 13:01:30 +000029#include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
30#include "pub_tool_libcassert.h" /* tl_assert() */
bart850f1992010-05-29 18:43:21 +000031#include "pub_tool_libcbase.h" /* VG_(memcmp)() */
bart46b5fce2008-06-28 13:01:30 +000032#include "pub_tool_libcprint.h" /* VG_(printf)() */
33#include "pub_tool_machine.h" /* VG_(get_IP)() */
bart46b5fce2008-06-28 13:01:30 +000034#include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
sewardjaf44c822007-11-25 14:01:38 +000035
36
bart46b5fce2008-06-28 13:01:30 +000037/* Local functions. */
bart28230a32008-02-29 17:27:03 +000038
bartdc1ef032009-02-15 14:18:02 +000039static void DRD_(cond_cleanup)(struct cond_info* p);
bart28230a32008-02-29 17:27:03 +000040
41
bart46b5fce2008-06-28 13:01:30 +000042/* Local variables. */
bart28230a32008-02-29 17:27:03 +000043
bartdc1ef032009-02-15 14:18:02 +000044static Bool DRD_(s_report_signal_unlocked) = True;
45static Bool DRD_(s_trace_cond);
sewardjaf44c822007-11-25 14:01:38 +000046
47
bart46b5fce2008-06-28 13:01:30 +000048/* Function definitions. */
bart28230a32008-02-29 17:27:03 +000049
bartdc1ef032009-02-15 14:18:02 +000050void DRD_(cond_set_report_signal_unlocked)(const Bool r)
bart764dea22009-02-15 13:16:52 +000051{
bartbedfd232009-03-26 19:07:15 +000052 DRD_(s_report_signal_unlocked) = r;
bart764dea22009-02-15 13:16:52 +000053}
54
bartdc1ef032009-02-15 14:18:02 +000055void DRD_(cond_set_trace)(const Bool trace_cond)
sewardjaf44c822007-11-25 14:01:38 +000056{
bartbedfd232009-03-26 19:07:15 +000057 DRD_(s_trace_cond) = trace_cond;
sewardjaf44c822007-11-25 14:01:38 +000058}
59
60static
bartdc1ef032009-02-15 14:18:02 +000061void DRD_(cond_initialize)(struct cond_info* const p, const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +000062{
bartbedfd232009-03-26 19:07:15 +000063 tl_assert(cond != 0);
bart62cc2322010-03-07 10:54:21 +000064 tl_assert(p->a1 == cond);
65 tl_assert(p->type == ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +000066
bartbedfd232009-03-26 19:07:15 +000067 p->cleanup = (void(*)(DrdClientobj*))(DRD_(cond_cleanup));
68 p->delete_thread = 0;
69 p->waiter_count = 0;
70 p->mutex = 0;
sewardjaf44c822007-11-25 14:01:38 +000071}
72
bart195e41f2009-02-15 11:34:57 +000073/**
74 * Free the memory that was allocated by cond_initialize(). Called by
75 * DRD_(clientobj_remove)().
bart28230a32008-02-29 17:27:03 +000076 */
bartdc1ef032009-02-15 14:18:02 +000077static void DRD_(cond_cleanup)(struct cond_info* p)
bart28230a32008-02-29 17:27:03 +000078{
bartbedfd232009-03-26 19:07:15 +000079 tl_assert(p);
80 if (p->mutex)
81 {
82 struct mutex_info* q;
83 q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex);
bartbedfd232009-03-26 19:07:15 +000084 {
bart62cc2322010-03-07 10:54:21 +000085 CondDestrErrInfo cde = {
86 DRD_(thread_get_running_tid)(),
87 p->a1,
88 q ? q->a1 : 0,
89 q ? q->owner : DRD_INVALID_THREADID
90 };
bartbedfd232009-03-26 19:07:15 +000091 VG_(maybe_record_error)(VG_(get_running_tid)(),
92 CondDestrErr,
93 VG_(get_IP)(VG_(get_running_tid)()),
94 "Destroying condition variable that is being"
95 " waited upon",
96 &cde);
97 }
98 }
bart28230a32008-02-29 17:27:03 +000099}
100
bart62cc2322010-03-07 10:54:21 +0000101/**
102 * Report that the synchronization object at address 'addr' is of the
103 * wrong type.
104 */
105static void wrong_type(const Addr addr)
106{
107 GenericErrInfo gei = {
108 .tid = DRD_(thread_get_running_tid)(),
109 .addr = addr,
110 };
111 VG_(maybe_record_error)(VG_(get_running_tid)(),
112 GenericErr,
113 VG_(get_IP)(VG_(get_running_tid)()),
114 "wrong type of synchronization object",
115 &gei);
116}
117
bartd45d9952009-05-31 18:53:54 +0000118static struct cond_info* cond_get_or_allocate(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000119{
bartbedfd232009-03-26 19:07:15 +0000120 struct cond_info *p;
bart28230a32008-02-29 17:27:03 +0000121
bartbedfd232009-03-26 19:07:15 +0000122 tl_assert(offsetof(DrdClientobj, cond) == 0);
123 p = &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
bart62cc2322010-03-07 10:54:21 +0000124 if (p)
125 return p;
126
127 if (DRD_(clientobj_present)(cond, cond + 1))
bartbedfd232009-03-26 19:07:15 +0000128 {
bart62cc2322010-03-07 10:54:21 +0000129 wrong_type(cond);
130 return 0;
bartbedfd232009-03-26 19:07:15 +0000131 }
bart62cc2322010-03-07 10:54:21 +0000132
133 p = &(DRD_(clientobj_add)(cond, ClientCondvar)->cond);
134 DRD_(cond_initialize)(p, cond);
bartbedfd232009-03-26 19:07:15 +0000135 return p;
sewardjaf44c822007-11-25 14:01:38 +0000136}
137
bartd45d9952009-05-31 18:53:54 +0000138struct cond_info* DRD_(cond_get)(const Addr cond)
bart28230a32008-02-29 17:27:03 +0000139{
bartbedfd232009-03-26 19:07:15 +0000140 tl_assert(offsetof(DrdClientobj, cond) == 0);
141 return &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
bart28230a32008-02-29 17:27:03 +0000142}
143
144/** Called before pthread_cond_init(). */
bartdc1ef032009-02-15 14:18:02 +0000145void DRD_(cond_pre_init)(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000146{
bartbedfd232009-03-26 19:07:15 +0000147 struct cond_info* p;
bart72b751c2008-03-01 13:44:24 +0000148
bartbedfd232009-03-26 19:07:15 +0000149 if (DRD_(s_trace_cond))
bartad994e82011-10-13 18:04:30 +0000150 DRD_(trace_msg)("[%d] cond_init cond 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000151 DRD_(thread_get_running_tid)(), cond);
bart72b751c2008-03-01 13:44:24 +0000152
bartbedfd232009-03-26 19:07:15 +0000153 p = DRD_(cond_get)(cond);
bart72b751c2008-03-01 13:44:24 +0000154
bartbedfd232009-03-26 19:07:15 +0000155 if (p)
156 {
bartd45d9952009-05-31 18:53:54 +0000157 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
bartbedfd232009-03-26 19:07:15 +0000158 VG_(maybe_record_error)(VG_(get_running_tid)(),
159 CondErr,
160 VG_(get_IP)(VG_(get_running_tid)()),
161 "initialized twice",
162 &cei);
163 }
bart72b751c2008-03-01 13:44:24 +0000164
bartd45d9952009-05-31 18:53:54 +0000165 p = cond_get_or_allocate(cond);
sewardjaf44c822007-11-25 14:01:38 +0000166}
167
bart28230a32008-02-29 17:27:03 +0000168/** Called after pthread_cond_destroy(). */
bartd36fa802012-12-24 10:22:14 +0000169void DRD_(cond_post_destroy)(const Addr cond, const Bool destroy_succeeded)
sewardjaf44c822007-11-25 14:01:38 +0000170{
bartbedfd232009-03-26 19:07:15 +0000171 struct cond_info* p;
bart72b751c2008-03-01 13:44:24 +0000172
bartbedfd232009-03-26 19:07:15 +0000173 if (DRD_(s_trace_cond))
bartad994e82011-10-13 18:04:30 +0000174 DRD_(trace_msg)("[%d] cond_destroy cond 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000175 DRD_(thread_get_running_tid)(), cond);
sewardjaf44c822007-11-25 14:01:38 +0000176
bartbedfd232009-03-26 19:07:15 +0000177 p = DRD_(cond_get)(cond);
178 if (p == 0)
179 {
bartd45d9952009-05-31 18:53:54 +0000180 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
bartbedfd232009-03-26 19:07:15 +0000181 VG_(maybe_record_error)(VG_(get_running_tid)(),
182 CondErr,
183 VG_(get_IP)(VG_(get_running_tid)()),
184 "not a condition variable",
185 &cei);
186 return;
187 }
sewardjaf44c822007-11-25 14:01:38 +0000188
bartbedfd232009-03-26 19:07:15 +0000189 if (p->waiter_count != 0)
190 {
bartd45d9952009-05-31 18:53:54 +0000191 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
bartbedfd232009-03-26 19:07:15 +0000192 VG_(maybe_record_error)(VG_(get_running_tid)(),
193 CondErr,
194 VG_(get_IP)(VG_(get_running_tid)()),
195 "destruction of condition variable being waited"
196 " upon",
197 &cei);
198 }
bart72b751c2008-03-01 13:44:24 +0000199
bartd36fa802012-12-24 10:22:14 +0000200 if (destroy_succeeded)
201 DRD_(clientobj_remove)(p->a1, ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +0000202}
203
bart62cc2322010-03-07 10:54:21 +0000204/**
205 * Called before pthread_cond_wait(). Note: before this function is called,
206 * mutex_unlock() has already been called from drd_clientreq.c.
bart08e6d6a2008-06-28 16:28:49 +0000207 */
bart62cc2322010-03-07 10:54:21 +0000208void DRD_(cond_pre_wait)(const Addr cond, const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000209{
bartbedfd232009-03-26 19:07:15 +0000210 struct cond_info* p;
211 struct mutex_info* q;
sewardjaf44c822007-11-25 14:01:38 +0000212
bartbedfd232009-03-26 19:07:15 +0000213 if (DRD_(s_trace_cond))
bartad994e82011-10-13 18:04:30 +0000214 DRD_(trace_msg)("[%d] cond_pre_wait cond 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000215 DRD_(thread_get_running_tid)(), cond);
bart3b1ee452008-02-29 19:28:15 +0000216
bartd45d9952009-05-31 18:53:54 +0000217 p = cond_get_or_allocate(cond);
bart62cc2322010-03-07 10:54:21 +0000218 if (!p)
219 {
220 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
221 VG_(maybe_record_error)(VG_(get_running_tid)(),
222 CondErr,
223 VG_(get_IP)(VG_(get_running_tid)()),
224 "not a condition variable",
225 &cei);
226 return;
227 }
bart3b1ee452008-02-29 19:28:15 +0000228
bartbedfd232009-03-26 19:07:15 +0000229 if (p->waiter_count == 0)
230 {
231 p->mutex = mutex;
232 }
233 else if (p->mutex != mutex)
234 {
235 CondWaitErrInfo cwei
bartd45d9952009-05-31 18:53:54 +0000236 = { .tid = DRD_(thread_get_running_tid)(),
237 .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex };
bartbedfd232009-03-26 19:07:15 +0000238 VG_(maybe_record_error)(VG_(get_running_tid)(),
239 CondWaitErr,
240 VG_(get_IP)(VG_(get_running_tid)()),
241 "Inconsistent association of condition variable"
242 " and mutex",
243 &cwei);
244 }
245 tl_assert(p->mutex);
246 q = DRD_(mutex_get)(p->mutex);
247 if (q
248 && q->owner == DRD_(thread_get_running_tid)() && q->recursion_count > 0)
249 {
250 const ThreadId vg_tid = VG_(get_running_tid)();
bartd45d9952009-05-31 18:53:54 +0000251 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
252 q->a1, q->recursion_count, q->owner };
bartbedfd232009-03-26 19:07:15 +0000253 VG_(maybe_record_error)(vg_tid,
254 MutexErr,
255 VG_(get_IP)(vg_tid),
256 "Mutex locked recursively",
257 &MEI);
258 }
259 else if (q == 0)
260 {
261 DRD_(not_a_mutex)(p->mutex);
262 }
bart08e6d6a2008-06-28 16:28:49 +0000263
bart62cc2322010-03-07 10:54:21 +0000264 ++p->waiter_count;
sewardjaf44c822007-11-25 14:01:38 +0000265}
266
bart62cc2322010-03-07 10:54:21 +0000267/**
268 * Called after pthread_cond_wait().
269 */
270void DRD_(cond_post_wait)(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000271{
bartbedfd232009-03-26 19:07:15 +0000272 struct cond_info* p;
sewardjaf44c822007-11-25 14:01:38 +0000273
bartbedfd232009-03-26 19:07:15 +0000274 if (DRD_(s_trace_cond))
bartad994e82011-10-13 18:04:30 +0000275 DRD_(trace_msg)("[%d] cond_post_wait cond 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000276 DRD_(thread_get_running_tid)(), cond);
bart3b1ee452008-02-29 19:28:15 +0000277
bartbedfd232009-03-26 19:07:15 +0000278 p = DRD_(cond_get)(cond);
bart62cc2322010-03-07 10:54:21 +0000279 if (!p)
bartbedfd232009-03-26 19:07:15 +0000280 {
bart7a2cc3c2011-04-30 07:27:41 +0000281 CondDestrErrInfo cde = {
282 DRD_(thread_get_running_tid)(), cond, 0, DRD_INVALID_THREADID
283 };
284 VG_(maybe_record_error)(VG_(get_running_tid)(),
285 CondDestrErr,
286 VG_(get_IP)(VG_(get_running_tid)()),
287 "condition variable has been destroyed while"
288 " being waited upon",
289 &cde);
bart62cc2322010-03-07 10:54:21 +0000290 return;
bartbedfd232009-03-26 19:07:15 +0000291 }
bart62cc2322010-03-07 10:54:21 +0000292
293 if (p->waiter_count > 0)
294 {
295 --p->waiter_count;
296 if (p->waiter_count == 0)
297 {
298 p->mutex = 0;
299 }
300 }
sewardjaf44c822007-11-25 14:01:38 +0000301}
302
bart62cc2322010-03-07 10:54:21 +0000303static void cond_signal(const DrdThreadId tid, struct cond_info* const cond_p)
sewardjaf44c822007-11-25 14:01:38 +0000304{
bartbedfd232009-03-26 19:07:15 +0000305 const ThreadId vg_tid = VG_(get_running_tid)();
306 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
bart3b1ee452008-02-29 19:28:15 +0000307
bart62cc2322010-03-07 10:54:21 +0000308 tl_assert(cond_p);
309
310 if (cond_p->waiter_count > 0)
bartbedfd232009-03-26 19:07:15 +0000311 {
312 if (DRD_(s_report_signal_unlocked)
bart62cc2322010-03-07 10:54:21 +0000313 && ! DRD_(mutex_is_locked_by)(cond_p->mutex, drd_tid))
bartbedfd232009-03-26 19:07:15 +0000314 {
bart62cc2322010-03-07 10:54:21 +0000315 /*
316 * A signal is sent while the associated mutex has not been locked.
317 * This can indicate but is not necessarily a race condition.
318 */
319 CondRaceErrInfo cei = { .tid = DRD_(thread_get_running_tid)(),
320 .cond = cond_p->a1,
321 .mutex = cond_p->mutex,
322 };
323 VG_(maybe_record_error)(vg_tid,
324 CondRaceErr,
325 VG_(get_IP)(vg_tid),
326 "CondErr",
327 &cei);
bartbedfd232009-03-26 19:07:15 +0000328 }
329 }
330 else
331 {
bart62cc2322010-03-07 10:54:21 +0000332 /*
333 * No other thread is waiting for the signal, hence the signal will
334 * be lost. This is normal in a POSIX threads application.
335 */
bartbedfd232009-03-26 19:07:15 +0000336 }
sewardjaf44c822007-11-25 14:01:38 +0000337}
338
bart62cc2322010-03-07 10:54:21 +0000339static void not_initialized(Addr const cond)
340{
341 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
342 VG_(maybe_record_error)(VG_(get_running_tid)(),
343 CondErr,
344 VG_(get_IP)(VG_(get_running_tid)()),
345 "condition variable has not been initialized",
346 &cei);
347}
348
bart3b1ee452008-02-29 19:28:15 +0000349/** Called before pthread_cond_signal(). */
bartdc1ef032009-02-15 14:18:02 +0000350void DRD_(cond_pre_signal)(Addr const cond)
bart3b1ee452008-02-29 19:28:15 +0000351{
bart62cc2322010-03-07 10:54:21 +0000352 struct cond_info* p;
353
354 p = DRD_(cond_get)(cond);
bartbedfd232009-03-26 19:07:15 +0000355 if (DRD_(s_trace_cond))
bartad994e82011-10-13 18:04:30 +0000356 DRD_(trace_msg)("[%d] cond_signal cond 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000357 DRD_(thread_get_running_tid)(), cond);
bart3b1ee452008-02-29 19:28:15 +0000358
bart850f1992010-05-29 18:43:21 +0000359 tl_assert(DRD_(pthread_cond_initializer));
360 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
361 DRD_(pthread_cond_initializer_size)) != 0)
bart62cc2322010-03-07 10:54:21 +0000362 {
363 not_initialized(cond);
364 return;
365 }
366
bart850f1992010-05-29 18:43:21 +0000367 if (!p)
368 p = cond_get_or_allocate(cond);
369
bart62cc2322010-03-07 10:54:21 +0000370 cond_signal(DRD_(thread_get_running_tid)(), p);
bart3b1ee452008-02-29 19:28:15 +0000371}
372
bart28230a32008-02-29 17:27:03 +0000373/** Called before pthread_cond_broadcast(). */
bartdc1ef032009-02-15 14:18:02 +0000374void DRD_(cond_pre_broadcast)(Addr const cond)
sewardjaf44c822007-11-25 14:01:38 +0000375{
bart62cc2322010-03-07 10:54:21 +0000376 struct cond_info* p;
377
bartbedfd232009-03-26 19:07:15 +0000378 if (DRD_(s_trace_cond))
bartad994e82011-10-13 18:04:30 +0000379 DRD_(trace_msg)("[%d] cond_broadcast cond 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000380 DRD_(thread_get_running_tid)(), cond);
bart3b1ee452008-02-29 19:28:15 +0000381
bart62cc2322010-03-07 10:54:21 +0000382 p = DRD_(cond_get)(cond);
bart850f1992010-05-29 18:43:21 +0000383 tl_assert(DRD_(pthread_cond_initializer));
384 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
385 DRD_(pthread_cond_initializer_size)) != 0)
bart62cc2322010-03-07 10:54:21 +0000386 {
387 not_initialized(cond);
388 return;
389 }
390
bart850f1992010-05-29 18:43:21 +0000391 if (!p)
392 p = cond_get_or_allocate(cond);
393
bart62cc2322010-03-07 10:54:21 +0000394 cond_signal(DRD_(thread_get_running_tid)(), p);
sewardjaf44c822007-11-25 14:01:38 +0000395}