blob: 4aae52630efce81958a11dd35519f8f8f286ae82 [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
Elliott Hughesed398002017-06-21 14:41:24 -07004 Copyright (C) 2006-2017 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))
florianea71ffb2015-08-05 14:38:57 +0000150 DRD_(trace_msg)("[%u] 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
bart4005d472013-01-27 10:56:04 +0000155 if (p) {
bartd45d9952009-05-31 18:53:54 +0000156 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
bartbedfd232009-03-26 19:07:15 +0000157 VG_(maybe_record_error)(VG_(get_running_tid)(),
158 CondErr,
159 VG_(get_IP)(VG_(get_running_tid)()),
160 "initialized twice",
161 &cei);
162 }
bart72b751c2008-03-01 13:44:24 +0000163
bart4005d472013-01-27 10:56:04 +0000164 cond_get_or_allocate(cond);
sewardjaf44c822007-11-25 14:01:38 +0000165}
166
bart28230a32008-02-29 17:27:03 +0000167/** Called after pthread_cond_destroy(). */
bartd36fa802012-12-24 10:22:14 +0000168void DRD_(cond_post_destroy)(const Addr cond, const Bool destroy_succeeded)
sewardjaf44c822007-11-25 14:01:38 +0000169{
bartbedfd232009-03-26 19:07:15 +0000170 struct cond_info* p;
bart72b751c2008-03-01 13:44:24 +0000171
bartbedfd232009-03-26 19:07:15 +0000172 if (DRD_(s_trace_cond))
florianea71ffb2015-08-05 14:38:57 +0000173 DRD_(trace_msg)("[%u] cond_destroy cond 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000174 DRD_(thread_get_running_tid)(), cond);
sewardjaf44c822007-11-25 14:01:38 +0000175
bartbedfd232009-03-26 19:07:15 +0000176 p = DRD_(cond_get)(cond);
177 if (p == 0)
178 {
bartd45d9952009-05-31 18:53:54 +0000179 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
bartbedfd232009-03-26 19:07:15 +0000180 VG_(maybe_record_error)(VG_(get_running_tid)(),
181 CondErr,
182 VG_(get_IP)(VG_(get_running_tid)()),
183 "not a condition variable",
184 &cei);
185 return;
186 }
sewardjaf44c822007-11-25 14:01:38 +0000187
bartbedfd232009-03-26 19:07:15 +0000188 if (p->waiter_count != 0)
189 {
bartd45d9952009-05-31 18:53:54 +0000190 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
bartbedfd232009-03-26 19:07:15 +0000191 VG_(maybe_record_error)(VG_(get_running_tid)(),
192 CondErr,
193 VG_(get_IP)(VG_(get_running_tid)()),
194 "destruction of condition variable being waited"
195 " upon",
196 &cei);
197 }
bart72b751c2008-03-01 13:44:24 +0000198
bartd36fa802012-12-24 10:22:14 +0000199 if (destroy_succeeded)
200 DRD_(clientobj_remove)(p->a1, ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +0000201}
202
bart62cc2322010-03-07 10:54:21 +0000203/**
204 * Called before pthread_cond_wait(). Note: before this function is called,
205 * mutex_unlock() has already been called from drd_clientreq.c.
bart08e6d6a2008-06-28 16:28:49 +0000206 */
bart62cc2322010-03-07 10:54:21 +0000207void DRD_(cond_pre_wait)(const Addr cond, const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000208{
bartbedfd232009-03-26 19:07:15 +0000209 struct cond_info* p;
210 struct mutex_info* q;
sewardjaf44c822007-11-25 14:01:38 +0000211
bartbedfd232009-03-26 19:07:15 +0000212 if (DRD_(s_trace_cond))
florianea71ffb2015-08-05 14:38:57 +0000213 DRD_(trace_msg)("[%u] cond_pre_wait cond 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000214 DRD_(thread_get_running_tid)(), cond);
bart3b1ee452008-02-29 19:28:15 +0000215
bartd45d9952009-05-31 18:53:54 +0000216 p = cond_get_or_allocate(cond);
bart62cc2322010-03-07 10:54:21 +0000217 if (!p)
218 {
219 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
220 VG_(maybe_record_error)(VG_(get_running_tid)(),
221 CondErr,
222 VG_(get_IP)(VG_(get_running_tid)()),
223 "not a condition variable",
224 &cei);
225 return;
226 }
bart3b1ee452008-02-29 19:28:15 +0000227
bartbedfd232009-03-26 19:07:15 +0000228 if (p->waiter_count == 0)
229 {
230 p->mutex = mutex;
231 }
232 else if (p->mutex != mutex)
233 {
234 CondWaitErrInfo cwei
bartd45d9952009-05-31 18:53:54 +0000235 = { .tid = DRD_(thread_get_running_tid)(),
236 .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex };
bartbedfd232009-03-26 19:07:15 +0000237 VG_(maybe_record_error)(VG_(get_running_tid)(),
238 CondWaitErr,
239 VG_(get_IP)(VG_(get_running_tid)()),
240 "Inconsistent association of condition variable"
241 " and mutex",
242 &cwei);
243 }
244 tl_assert(p->mutex);
245 q = DRD_(mutex_get)(p->mutex);
246 if (q
247 && q->owner == DRD_(thread_get_running_tid)() && q->recursion_count > 0)
248 {
249 const ThreadId vg_tid = VG_(get_running_tid)();
bartd45d9952009-05-31 18:53:54 +0000250 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
251 q->a1, q->recursion_count, q->owner };
bartbedfd232009-03-26 19:07:15 +0000252 VG_(maybe_record_error)(vg_tid,
253 MutexErr,
254 VG_(get_IP)(vg_tid),
255 "Mutex locked recursively",
256 &MEI);
257 }
258 else if (q == 0)
259 {
260 DRD_(not_a_mutex)(p->mutex);
261 }
bart08e6d6a2008-06-28 16:28:49 +0000262
bart62cc2322010-03-07 10:54:21 +0000263 ++p->waiter_count;
sewardjaf44c822007-11-25 14:01:38 +0000264}
265
bart62cc2322010-03-07 10:54:21 +0000266/**
267 * Called after pthread_cond_wait().
268 */
269void DRD_(cond_post_wait)(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000270{
bartbedfd232009-03-26 19:07:15 +0000271 struct cond_info* p;
sewardjaf44c822007-11-25 14:01:38 +0000272
bartbedfd232009-03-26 19:07:15 +0000273 if (DRD_(s_trace_cond))
florianea71ffb2015-08-05 14:38:57 +0000274 DRD_(trace_msg)("[%u] cond_post_wait cond 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000275 DRD_(thread_get_running_tid)(), cond);
bart3b1ee452008-02-29 19:28:15 +0000276
bartbedfd232009-03-26 19:07:15 +0000277 p = DRD_(cond_get)(cond);
bart62cc2322010-03-07 10:54:21 +0000278 if (!p)
bartbedfd232009-03-26 19:07:15 +0000279 {
bart7a2cc3c2011-04-30 07:27:41 +0000280 CondDestrErrInfo cde = {
281 DRD_(thread_get_running_tid)(), cond, 0, DRD_INVALID_THREADID
282 };
283 VG_(maybe_record_error)(VG_(get_running_tid)(),
284 CondDestrErr,
285 VG_(get_IP)(VG_(get_running_tid)()),
286 "condition variable has been destroyed while"
287 " being waited upon",
288 &cde);
bart62cc2322010-03-07 10:54:21 +0000289 return;
bartbedfd232009-03-26 19:07:15 +0000290 }
bart62cc2322010-03-07 10:54:21 +0000291
292 if (p->waiter_count > 0)
293 {
294 --p->waiter_count;
295 if (p->waiter_count == 0)
296 {
297 p->mutex = 0;
298 }
299 }
sewardjaf44c822007-11-25 14:01:38 +0000300}
301
bart62cc2322010-03-07 10:54:21 +0000302static void cond_signal(const DrdThreadId tid, struct cond_info* const cond_p)
sewardjaf44c822007-11-25 14:01:38 +0000303{
bartbedfd232009-03-26 19:07:15 +0000304 const ThreadId vg_tid = VG_(get_running_tid)();
305 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
bart3b1ee452008-02-29 19:28:15 +0000306
bart62cc2322010-03-07 10:54:21 +0000307 tl_assert(cond_p);
308
309 if (cond_p->waiter_count > 0)
bartbedfd232009-03-26 19:07:15 +0000310 {
311 if (DRD_(s_report_signal_unlocked)
bart62cc2322010-03-07 10:54:21 +0000312 && ! DRD_(mutex_is_locked_by)(cond_p->mutex, drd_tid))
bartbedfd232009-03-26 19:07:15 +0000313 {
bart62cc2322010-03-07 10:54:21 +0000314 /*
315 * A signal is sent while the associated mutex has not been locked.
316 * This can indicate but is not necessarily a race condition.
317 */
318 CondRaceErrInfo cei = { .tid = DRD_(thread_get_running_tid)(),
319 .cond = cond_p->a1,
320 .mutex = cond_p->mutex,
321 };
322 VG_(maybe_record_error)(vg_tid,
323 CondRaceErr,
324 VG_(get_IP)(vg_tid),
325 "CondErr",
326 &cei);
bartbedfd232009-03-26 19:07:15 +0000327 }
328 }
329 else
330 {
bart62cc2322010-03-07 10:54:21 +0000331 /*
332 * No other thread is waiting for the signal, hence the signal will
333 * be lost. This is normal in a POSIX threads application.
334 */
bartbedfd232009-03-26 19:07:15 +0000335 }
sewardjaf44c822007-11-25 14:01:38 +0000336}
337
bart62cc2322010-03-07 10:54:21 +0000338static void not_initialized(Addr const cond)
339{
340 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
341 VG_(maybe_record_error)(VG_(get_running_tid)(),
342 CondErr,
343 VG_(get_IP)(VG_(get_running_tid)()),
344 "condition variable has not been initialized",
345 &cei);
346}
347
bart3b1ee452008-02-29 19:28:15 +0000348/** Called before pthread_cond_signal(). */
bartdc1ef032009-02-15 14:18:02 +0000349void DRD_(cond_pre_signal)(Addr const cond)
bart3b1ee452008-02-29 19:28:15 +0000350{
bart62cc2322010-03-07 10:54:21 +0000351 struct cond_info* p;
352
353 p = DRD_(cond_get)(cond);
bartbedfd232009-03-26 19:07:15 +0000354 if (DRD_(s_trace_cond))
florianea71ffb2015-08-05 14:38:57 +0000355 DRD_(trace_msg)("[%u] cond_signal cond 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000356 DRD_(thread_get_running_tid)(), cond);
bart3b1ee452008-02-29 19:28:15 +0000357
bart850f1992010-05-29 18:43:21 +0000358 tl_assert(DRD_(pthread_cond_initializer));
359 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
360 DRD_(pthread_cond_initializer_size)) != 0)
bart62cc2322010-03-07 10:54:21 +0000361 {
362 not_initialized(cond);
363 return;
364 }
365
bart850f1992010-05-29 18:43:21 +0000366 if (!p)
367 p = cond_get_or_allocate(cond);
368
bart62cc2322010-03-07 10:54:21 +0000369 cond_signal(DRD_(thread_get_running_tid)(), p);
bart3b1ee452008-02-29 19:28:15 +0000370}
371
bart28230a32008-02-29 17:27:03 +0000372/** Called before pthread_cond_broadcast(). */
bartdc1ef032009-02-15 14:18:02 +0000373void DRD_(cond_pre_broadcast)(Addr const cond)
sewardjaf44c822007-11-25 14:01:38 +0000374{
bart62cc2322010-03-07 10:54:21 +0000375 struct cond_info* p;
376
bartbedfd232009-03-26 19:07:15 +0000377 if (DRD_(s_trace_cond))
florianea71ffb2015-08-05 14:38:57 +0000378 DRD_(trace_msg)("[%u] cond_broadcast cond 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000379 DRD_(thread_get_running_tid)(), cond);
bart3b1ee452008-02-29 19:28:15 +0000380
bart62cc2322010-03-07 10:54:21 +0000381 p = DRD_(cond_get)(cond);
bart850f1992010-05-29 18:43:21 +0000382 tl_assert(DRD_(pthread_cond_initializer));
383 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
384 DRD_(pthread_cond_initializer_size)) != 0)
bart62cc2322010-03-07 10:54:21 +0000385 {
386 not_initialized(cond);
387 return;
388 }
389
bart850f1992010-05-29 18:43:21 +0000390 if (!p)
391 p = cond_get_or_allocate(cond);
392
bart62cc2322010-03-07 10:54:21 +0000393 cond_signal(DRD_(thread_get_running_tid)(), p);
sewardjaf44c822007-11-25 14:01:38 +0000394}