blob: acb7ee620e703b829acc9b20810808bed2ce8aea [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 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
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"
30#include "drd_suppression.h"
bart46b5fce2008-06-28 13:01:30 +000031#include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
32#include "pub_tool_libcassert.h" /* tl_assert() */
33#include "pub_tool_libcprint.h" /* VG_(printf)() */
34#include "pub_tool_machine.h" /* VG_(get_IP)() */
35#include "pub_tool_options.h" /* VG_(clo_backtrace_size) */
36#include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
sewardjaf44c822007-11-25 14:01:38 +000037
38
bart46b5fce2008-06-28 13:01:30 +000039/* Local functions. */
bart28230a32008-02-29 17:27:03 +000040
41static void cond_cleanup(struct cond_info* p);
42
43
bart46b5fce2008-06-28 13:01:30 +000044/* Local variables. */
bart28230a32008-02-29 17:27:03 +000045
bart764dea22009-02-15 13:16:52 +000046static Bool s_drd_report_signal_unlocked = True;
sewardjaf44c822007-11-25 14:01:38 +000047static Bool s_trace_cond;
48
49
bart46b5fce2008-06-28 13:01:30 +000050/* Function definitions. */
bart28230a32008-02-29 17:27:03 +000051
bart764dea22009-02-15 13:16:52 +000052void cond_set_report_signal_unlocked(const Bool r)
53{
54 s_drd_report_signal_unlocked = r;
55}
56
sewardjaf44c822007-11-25 14:01:38 +000057void cond_set_trace(const Bool trace_cond)
58{
59 s_trace_cond = trace_cond;
60}
61
62static
bart0268dfa2008-03-11 20:10:21 +000063void cond_initialize(struct cond_info* const p, const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +000064{
65 tl_assert(cond != 0);
bart28230a32008-02-29 17:27:03 +000066 tl_assert(p->a1 == cond);
bart28230a32008-02-29 17:27:03 +000067 tl_assert(p->type == ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +000068
bart28230a32008-02-29 17:27:03 +000069 p->cleanup = (void(*)(DrdClientobj*))cond_cleanup;
sewardjaf44c822007-11-25 14:01:38 +000070 p->waiter_count = 0;
71 p->mutex = 0;
72}
73
bart195e41f2009-02-15 11:34:57 +000074/**
75 * Free the memory that was allocated by cond_initialize(). Called by
76 * DRD_(clientobj_remove)().
bart28230a32008-02-29 17:27:03 +000077 */
78static void cond_cleanup(struct cond_info* p)
79{
80 tl_assert(p);
81 if (p->mutex)
82 {
83 struct mutex_info* q;
bart195e41f2009-02-15 11:34:57 +000084 q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex);
bart3b1ee452008-02-29 19:28:15 +000085 tl_assert(q);
86 {
87 CondDestrErrInfo cde = { p->a1, q->a1, q->owner };
88 VG_(maybe_record_error)(VG_(get_running_tid)(),
89 CondDestrErr,
90 VG_(get_IP)(VG_(get_running_tid)()),
91 "Destroying condition variable that is being"
92 " waited upon",
93 &cde);
94 }
bart28230a32008-02-29 17:27:03 +000095 }
96}
97
bart0268dfa2008-03-11 20:10:21 +000098static struct cond_info* cond_get_or_allocate(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +000099{
bart28230a32008-02-29 17:27:03 +0000100 struct cond_info *p;
101
102 tl_assert(offsetof(DrdClientobj, cond) == 0);
bart195e41f2009-02-15 11:34:57 +0000103 p = &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
bart28230a32008-02-29 17:27:03 +0000104 if (p == 0)
sewardj721ad7b2007-11-30 08:30:29 +0000105 {
bart195e41f2009-02-15 11:34:57 +0000106 p = &(DRD_(clientobj_add)(cond, ClientCondvar)->cond);
bart0268dfa2008-03-11 20:10:21 +0000107 cond_initialize(p, cond);
sewardj721ad7b2007-11-30 08:30:29 +0000108 }
bart28230a32008-02-29 17:27:03 +0000109 return p;
sewardjaf44c822007-11-25 14:01:38 +0000110}
111
bart72b751c2008-03-01 13:44:24 +0000112static struct cond_info* cond_get(const Addr cond)
bart28230a32008-02-29 17:27:03 +0000113{
114 tl_assert(offsetof(DrdClientobj, cond) == 0);
bart195e41f2009-02-15 11:34:57 +0000115 return &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
bart28230a32008-02-29 17:27:03 +0000116}
117
118/** Called before pthread_cond_init(). */
bart0268dfa2008-03-11 20:10:21 +0000119void cond_pre_init(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000120{
bart72b751c2008-03-01 13:44:24 +0000121 struct cond_info* p;
122
sewardjaf44c822007-11-25 14:01:38 +0000123 if (s_trace_cond)
124 {
bart3b1ee452008-02-29 19:28:15 +0000125 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000126 "[%d/%d] cond_init cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000127 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000128 DRD_(thread_get_running_tid)(),
bart3b1ee452008-02-29 19:28:15 +0000129 cond);
sewardjaf44c822007-11-25 14:01:38 +0000130 }
bart72b751c2008-03-01 13:44:24 +0000131
bart72b751c2008-03-01 13:44:24 +0000132 p = cond_get(cond);
133
134 if (p)
135 {
136 CondErrInfo cei = { .cond = cond };
137 VG_(maybe_record_error)(VG_(get_running_tid)(),
138 CondErr,
139 VG_(get_IP)(VG_(get_running_tid)()),
140 "initialized twice",
141 &cei);
142 }
143
bart0268dfa2008-03-11 20:10:21 +0000144 p = cond_get_or_allocate(cond);
sewardjaf44c822007-11-25 14:01:38 +0000145}
146
bart28230a32008-02-29 17:27:03 +0000147/** Called after pthread_cond_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000148void cond_post_destroy(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000149{
bart72b751c2008-03-01 13:44:24 +0000150 struct cond_info* p;
151
sewardjaf44c822007-11-25 14:01:38 +0000152 if (s_trace_cond)
153 {
bart3b1ee452008-02-29 19:28:15 +0000154 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000155 "[%d/%d] cond_destroy cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000156 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000157 DRD_(thread_get_running_tid)(),
bart72b751c2008-03-01 13:44:24 +0000158 cond);
sewardjaf44c822007-11-25 14:01:38 +0000159 }
160
bart72b751c2008-03-01 13:44:24 +0000161 p = cond_get(cond);
162 if (p == 0)
163 {
164 CondErrInfo cei = { .cond = cond };
165 VG_(maybe_record_error)(VG_(get_running_tid)(),
166 CondErr,
167 VG_(get_IP)(VG_(get_running_tid)()),
168 "not a condition variable",
169 &cei);
170 return;
171 }
sewardjaf44c822007-11-25 14:01:38 +0000172
bart72b751c2008-03-01 13:44:24 +0000173 if (p->waiter_count != 0)
174 {
175 CondErrInfo cei = { .cond = cond };
176 VG_(maybe_record_error)(VG_(get_running_tid)(),
177 CondErr,
178 VG_(get_IP)(VG_(get_running_tid)()),
179 "destruction of condition variable being waited"
180 " upon",
181 &cei);
182 }
183
bart195e41f2009-02-15 11:34:57 +0000184 DRD_(clientobj_remove)(p->a1, ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +0000185}
186
bart08e6d6a2008-06-28 16:28:49 +0000187/** Called before pthread_cond_wait(). Note: before this function is called,
188 * mutex_unlock() has already been called from drd_clientreq.c.
189 */
bart0268dfa2008-03-11 20:10:21 +0000190int cond_pre_wait(const Addr cond, const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000191{
192 struct cond_info* p;
bart08e6d6a2008-06-28 16:28:49 +0000193 struct mutex_info* q;
sewardjaf44c822007-11-25 14:01:38 +0000194
bart3b1ee452008-02-29 19:28:15 +0000195 if (s_trace_cond)
196 {
197 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000198 "[%d/%d] cond_pre_wait cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000199 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000200 DRD_(thread_get_running_tid)(),
bart3b1ee452008-02-29 19:28:15 +0000201 cond);
202 }
203
bart0268dfa2008-03-11 20:10:21 +0000204 p = cond_get_or_allocate(cond);
bart3b1ee452008-02-29 19:28:15 +0000205 tl_assert(p);
206
sewardjaf44c822007-11-25 14:01:38 +0000207 if (p->waiter_count == 0)
208 {
209 p->mutex = mutex;
210 }
bart3bb1cec2008-06-28 16:01:43 +0000211 else if (p->mutex != mutex)
sewardjaf44c822007-11-25 14:01:38 +0000212 {
bart3bb1cec2008-06-28 16:01:43 +0000213 CondWaitErrInfo cwei
214 = { .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex };
215 VG_(maybe_record_error)(VG_(get_running_tid)(),
216 CondWaitErr,
217 VG_(get_IP)(VG_(get_running_tid)()),
218 "Inconsistent association of condition variable"
219 " and mutex",
220 &cwei);
sewardjaf44c822007-11-25 14:01:38 +0000221 }
bart08e6d6a2008-06-28 16:28:49 +0000222 tl_assert(p->mutex);
223 q = mutex_get(p->mutex);
bart62a784c2009-02-15 13:11:14 +0000224 if (q && q->owner == DRD_(thread_get_running_tid)() && q->recursion_count > 0)
bart08e6d6a2008-06-28 16:28:49 +0000225 {
226 const ThreadId vg_tid = VG_(get_running_tid)();
227 MutexErrInfo MEI = { q->a1, q->recursion_count, q->owner };
228 VG_(maybe_record_error)(vg_tid,
229 MutexErr,
230 VG_(get_IP)(vg_tid),
231 "Mutex locked recursively",
232 &MEI);
233 }
234 else if (q == 0)
235 {
236 not_a_mutex(p->mutex);
237 }
238
sewardjaf44c822007-11-25 14:01:38 +0000239 return ++p->waiter_count;
240}
241
bart28230a32008-02-29 17:27:03 +0000242/** Called after pthread_cond_wait(). */
sewardjaf44c822007-11-25 14:01:38 +0000243int cond_post_wait(const Addr cond)
244{
245 struct cond_info* p;
246
bart3b1ee452008-02-29 19:28:15 +0000247 if (s_trace_cond)
248 {
249 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000250 "[%d/%d] cond_post_wait cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000251 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000252 DRD_(thread_get_running_tid)(),
bart3b1ee452008-02-29 19:28:15 +0000253 cond);
254 }
255
sewardjaf44c822007-11-25 14:01:38 +0000256 p = cond_get(cond);
barta9c55082008-06-28 16:55:35 +0000257 if (p)
sewardjaf44c822007-11-25 14:01:38 +0000258 {
barta9c55082008-06-28 16:55:35 +0000259 if (p->waiter_count > 0)
260 {
261 --p->waiter_count;
262 if (p->waiter_count == 0)
263 {
264 p->mutex = 0;
265 }
266 }
267 return p->waiter_count;
sewardjaf44c822007-11-25 14:01:38 +0000268 }
barta9c55082008-06-28 16:55:35 +0000269 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000270}
271
bart3b1ee452008-02-29 19:28:15 +0000272static void cond_signal(Addr const cond)
sewardjaf44c822007-11-25 14:01:38 +0000273{
274 const ThreadId vg_tid = VG_(get_running_tid)();
bart62a784c2009-02-15 13:11:14 +0000275 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
sewardjaf44c822007-11-25 14:01:38 +0000276 struct cond_info* const cond_p = cond_get(cond);
bart3b1ee452008-02-29 19:28:15 +0000277
sewardjaf44c822007-11-25 14:01:38 +0000278 if (cond_p && cond_p->waiter_count > 0)
279 {
bart46b5fce2008-06-28 13:01:30 +0000280 if (s_drd_report_signal_unlocked
281 && ! mutex_is_locked_by(cond_p->mutex, drd_tid))
sewardjaf44c822007-11-25 14:01:38 +0000282 {
bart11608002008-03-13 17:47:01 +0000283 /* A signal is sent while the associated mutex has not been locked. */
284 /* This can indicate but is not necessarily a race condition. */
sewardjaf44c822007-11-25 14:01:38 +0000285 CondRaceErrInfo cei;
286 cei.cond = cond;
287 cei.mutex = cond_p->mutex;
288 VG_(maybe_record_error)(vg_tid,
289 CondRaceErr,
290 VG_(get_IP)(vg_tid),
291 "CondErr",
292 &cei);
293 }
294 }
295 else
296 {
297 /* No other thread is waiting for the signal, hence the signal will be */
298 /* lost. This is normal in a POSIX threads application. */
299 }
300}
301
bart3b1ee452008-02-29 19:28:15 +0000302/** Called before pthread_cond_signal(). */
303void cond_pre_signal(Addr const cond)
304{
305 if (s_trace_cond)
306 {
307 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000308 "[%d/%d] cond_signal cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000309 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000310 DRD_(thread_get_running_tid)(),
bart3b1ee452008-02-29 19:28:15 +0000311 cond);
312 }
313
314 cond_signal(cond);
315}
316
bart28230a32008-02-29 17:27:03 +0000317/** Called before pthread_cond_broadcast(). */
sewardjaf44c822007-11-25 14:01:38 +0000318void cond_pre_broadcast(Addr const cond)
319{
bart3b1ee452008-02-29 19:28:15 +0000320 if (s_trace_cond)
321 {
322 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000323 "[%d/%d] cond_broadcast cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000324 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000325 DRD_(thread_get_running_tid)(),
bart3b1ee452008-02-29 19:28:15 +0000326 cond);
327 }
328
329 cond_signal(cond);
sewardjaf44c822007-11-25 14:01:38 +0000330}
331
bart28230a32008-02-29 17:27:03 +0000332/** Called after pthread_cond_destroy(). */
sewardj85642922008-01-14 11:54:56 +0000333void cond_thread_delete(const DrdThreadId tid)
334{ }