blob: 83da627389a2312982d0f73f178d1fc4473e629f [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/* Global variables. */
45
46Bool s_drd_report_signal_unlocked = True;
47
48
49/* Local variables. */
bart28230a32008-02-29 17:27:03 +000050
sewardjaf44c822007-11-25 14:01:38 +000051static Bool s_trace_cond;
52
53
bart46b5fce2008-06-28 13:01:30 +000054/* Function definitions. */
bart28230a32008-02-29 17:27:03 +000055
sewardjaf44c822007-11-25 14:01:38 +000056void cond_set_trace(const Bool trace_cond)
57{
58 s_trace_cond = trace_cond;
59}
60
61static
bart0268dfa2008-03-11 20:10:21 +000062void cond_initialize(struct cond_info* const p, const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +000063{
64 tl_assert(cond != 0);
bart28230a32008-02-29 17:27:03 +000065 tl_assert(p->a1 == cond);
bart28230a32008-02-29 17:27:03 +000066 tl_assert(p->type == ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +000067
bart28230a32008-02-29 17:27:03 +000068 p->cleanup = (void(*)(DrdClientobj*))cond_cleanup;
sewardjaf44c822007-11-25 14:01:38 +000069 p->waiter_count = 0;
70 p->mutex = 0;
71}
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 */
77static void cond_cleanup(struct cond_info* p)
78{
79 tl_assert(p);
80 if (p->mutex)
81 {
82 struct mutex_info* q;
bart195e41f2009-02-15 11:34:57 +000083 q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex);
bart3b1ee452008-02-29 19:28:15 +000084 tl_assert(q);
85 {
86 CondDestrErrInfo cde = { p->a1, q->a1, q->owner };
87 VG_(maybe_record_error)(VG_(get_running_tid)(),
88 CondDestrErr,
89 VG_(get_IP)(VG_(get_running_tid)()),
90 "Destroying condition variable that is being"
91 " waited upon",
92 &cde);
93 }
bart28230a32008-02-29 17:27:03 +000094 }
95}
96
bart0268dfa2008-03-11 20:10:21 +000097static struct cond_info* cond_get_or_allocate(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +000098{
bart28230a32008-02-29 17:27:03 +000099 struct cond_info *p;
100
101 tl_assert(offsetof(DrdClientobj, cond) == 0);
bart195e41f2009-02-15 11:34:57 +0000102 p = &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
bart28230a32008-02-29 17:27:03 +0000103 if (p == 0)
sewardj721ad7b2007-11-30 08:30:29 +0000104 {
bart195e41f2009-02-15 11:34:57 +0000105 p = &(DRD_(clientobj_add)(cond, ClientCondvar)->cond);
bart0268dfa2008-03-11 20:10:21 +0000106 cond_initialize(p, cond);
sewardj721ad7b2007-11-30 08:30:29 +0000107 }
bart28230a32008-02-29 17:27:03 +0000108 return p;
sewardjaf44c822007-11-25 14:01:38 +0000109}
110
bart72b751c2008-03-01 13:44:24 +0000111static struct cond_info* cond_get(const Addr cond)
bart28230a32008-02-29 17:27:03 +0000112{
113 tl_assert(offsetof(DrdClientobj, cond) == 0);
bart195e41f2009-02-15 11:34:57 +0000114 return &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
bart28230a32008-02-29 17:27:03 +0000115}
116
117/** Called before pthread_cond_init(). */
bart0268dfa2008-03-11 20:10:21 +0000118void cond_pre_init(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000119{
bart72b751c2008-03-01 13:44:24 +0000120 struct cond_info* p;
121
sewardjaf44c822007-11-25 14:01:38 +0000122 if (s_trace_cond)
123 {
bart3b1ee452008-02-29 19:28:15 +0000124 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000125 "[%d/%d] cond_init cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000126 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000127 DRD_(thread_get_running_tid)(),
bart3b1ee452008-02-29 19:28:15 +0000128 cond);
sewardjaf44c822007-11-25 14:01:38 +0000129 }
bart72b751c2008-03-01 13:44:24 +0000130
bart72b751c2008-03-01 13:44:24 +0000131 p = cond_get(cond);
132
133 if (p)
134 {
135 CondErrInfo cei = { .cond = cond };
136 VG_(maybe_record_error)(VG_(get_running_tid)(),
137 CondErr,
138 VG_(get_IP)(VG_(get_running_tid)()),
139 "initialized twice",
140 &cei);
141 }
142
bart0268dfa2008-03-11 20:10:21 +0000143 p = cond_get_or_allocate(cond);
sewardjaf44c822007-11-25 14:01:38 +0000144}
145
bart28230a32008-02-29 17:27:03 +0000146/** Called after pthread_cond_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000147void cond_post_destroy(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000148{
bart72b751c2008-03-01 13:44:24 +0000149 struct cond_info* p;
150
sewardjaf44c822007-11-25 14:01:38 +0000151 if (s_trace_cond)
152 {
bart3b1ee452008-02-29 19:28:15 +0000153 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000154 "[%d/%d] cond_destroy cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000155 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000156 DRD_(thread_get_running_tid)(),
bart72b751c2008-03-01 13:44:24 +0000157 cond);
sewardjaf44c822007-11-25 14:01:38 +0000158 }
159
bart72b751c2008-03-01 13:44:24 +0000160 p = cond_get(cond);
161 if (p == 0)
162 {
163 CondErrInfo cei = { .cond = cond };
164 VG_(maybe_record_error)(VG_(get_running_tid)(),
165 CondErr,
166 VG_(get_IP)(VG_(get_running_tid)()),
167 "not a condition variable",
168 &cei);
169 return;
170 }
sewardjaf44c822007-11-25 14:01:38 +0000171
bart72b751c2008-03-01 13:44:24 +0000172 if (p->waiter_count != 0)
173 {
174 CondErrInfo cei = { .cond = cond };
175 VG_(maybe_record_error)(VG_(get_running_tid)(),
176 CondErr,
177 VG_(get_IP)(VG_(get_running_tid)()),
178 "destruction of condition variable being waited"
179 " upon",
180 &cei);
181 }
182
bart195e41f2009-02-15 11:34:57 +0000183 DRD_(clientobj_remove)(p->a1, ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +0000184}
185
bart08e6d6a2008-06-28 16:28:49 +0000186/** Called before pthread_cond_wait(). Note: before this function is called,
187 * mutex_unlock() has already been called from drd_clientreq.c.
188 */
bart0268dfa2008-03-11 20:10:21 +0000189int cond_pre_wait(const Addr cond, const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000190{
191 struct cond_info* p;
bart08e6d6a2008-06-28 16:28:49 +0000192 struct mutex_info* q;
sewardjaf44c822007-11-25 14:01:38 +0000193
bart3b1ee452008-02-29 19:28:15 +0000194 if (s_trace_cond)
195 {
196 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000197 "[%d/%d] cond_pre_wait cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000198 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000199 DRD_(thread_get_running_tid)(),
bart3b1ee452008-02-29 19:28:15 +0000200 cond);
201 }
202
bart0268dfa2008-03-11 20:10:21 +0000203 p = cond_get_or_allocate(cond);
bart3b1ee452008-02-29 19:28:15 +0000204 tl_assert(p);
205
sewardjaf44c822007-11-25 14:01:38 +0000206 if (p->waiter_count == 0)
207 {
208 p->mutex = mutex;
209 }
bart3bb1cec2008-06-28 16:01:43 +0000210 else if (p->mutex != mutex)
sewardjaf44c822007-11-25 14:01:38 +0000211 {
bart3bb1cec2008-06-28 16:01:43 +0000212 CondWaitErrInfo cwei
213 = { .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex };
214 VG_(maybe_record_error)(VG_(get_running_tid)(),
215 CondWaitErr,
216 VG_(get_IP)(VG_(get_running_tid)()),
217 "Inconsistent association of condition variable"
218 " and mutex",
219 &cwei);
sewardjaf44c822007-11-25 14:01:38 +0000220 }
bart08e6d6a2008-06-28 16:28:49 +0000221 tl_assert(p->mutex);
222 q = mutex_get(p->mutex);
bart62a784c2009-02-15 13:11:14 +0000223 if (q && q->owner == DRD_(thread_get_running_tid)() && q->recursion_count > 0)
bart08e6d6a2008-06-28 16:28:49 +0000224 {
225 const ThreadId vg_tid = VG_(get_running_tid)();
226 MutexErrInfo MEI = { q->a1, q->recursion_count, q->owner };
227 VG_(maybe_record_error)(vg_tid,
228 MutexErr,
229 VG_(get_IP)(vg_tid),
230 "Mutex locked recursively",
231 &MEI);
232 }
233 else if (q == 0)
234 {
235 not_a_mutex(p->mutex);
236 }
237
sewardjaf44c822007-11-25 14:01:38 +0000238 return ++p->waiter_count;
239}
240
bart28230a32008-02-29 17:27:03 +0000241/** Called after pthread_cond_wait(). */
sewardjaf44c822007-11-25 14:01:38 +0000242int cond_post_wait(const Addr cond)
243{
244 struct cond_info* p;
245
bart3b1ee452008-02-29 19:28:15 +0000246 if (s_trace_cond)
247 {
248 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000249 "[%d/%d] cond_post_wait cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000250 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000251 DRD_(thread_get_running_tid)(),
bart3b1ee452008-02-29 19:28:15 +0000252 cond);
253 }
254
sewardjaf44c822007-11-25 14:01:38 +0000255 p = cond_get(cond);
barta9c55082008-06-28 16:55:35 +0000256 if (p)
sewardjaf44c822007-11-25 14:01:38 +0000257 {
barta9c55082008-06-28 16:55:35 +0000258 if (p->waiter_count > 0)
259 {
260 --p->waiter_count;
261 if (p->waiter_count == 0)
262 {
263 p->mutex = 0;
264 }
265 }
266 return p->waiter_count;
sewardjaf44c822007-11-25 14:01:38 +0000267 }
barta9c55082008-06-28 16:55:35 +0000268 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000269}
270
bart3b1ee452008-02-29 19:28:15 +0000271static void cond_signal(Addr const cond)
sewardjaf44c822007-11-25 14:01:38 +0000272{
273 const ThreadId vg_tid = VG_(get_running_tid)();
bart62a784c2009-02-15 13:11:14 +0000274 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
sewardjaf44c822007-11-25 14:01:38 +0000275 struct cond_info* const cond_p = cond_get(cond);
bart3b1ee452008-02-29 19:28:15 +0000276
sewardjaf44c822007-11-25 14:01:38 +0000277 if (cond_p && cond_p->waiter_count > 0)
278 {
bart46b5fce2008-06-28 13:01:30 +0000279 if (s_drd_report_signal_unlocked
280 && ! mutex_is_locked_by(cond_p->mutex, drd_tid))
sewardjaf44c822007-11-25 14:01:38 +0000281 {
bart11608002008-03-13 17:47:01 +0000282 /* A signal is sent while the associated mutex has not been locked. */
283 /* This can indicate but is not necessarily a race condition. */
sewardjaf44c822007-11-25 14:01:38 +0000284 CondRaceErrInfo cei;
285 cei.cond = cond;
286 cei.mutex = cond_p->mutex;
287 VG_(maybe_record_error)(vg_tid,
288 CondRaceErr,
289 VG_(get_IP)(vg_tid),
290 "CondErr",
291 &cei);
292 }
293 }
294 else
295 {
296 /* No other thread is waiting for the signal, hence the signal will be */
297 /* lost. This is normal in a POSIX threads application. */
298 }
299}
300
bart3b1ee452008-02-29 19:28:15 +0000301/** Called before pthread_cond_signal(). */
302void cond_pre_signal(Addr const cond)
303{
304 if (s_trace_cond)
305 {
306 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000307 "[%d/%d] cond_signal cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000308 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000309 DRD_(thread_get_running_tid)(),
bart3b1ee452008-02-29 19:28:15 +0000310 cond);
311 }
312
313 cond_signal(cond);
314}
315
bart28230a32008-02-29 17:27:03 +0000316/** Called before pthread_cond_broadcast(). */
sewardjaf44c822007-11-25 14:01:38 +0000317void cond_pre_broadcast(Addr const cond)
318{
bart3b1ee452008-02-29 19:28:15 +0000319 if (s_trace_cond)
320 {
321 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000322 "[%d/%d] cond_broadcast cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000323 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000324 DRD_(thread_get_running_tid)(),
bart3b1ee452008-02-29 19:28:15 +0000325 cond);
326 }
327
328 cond_signal(cond);
sewardjaf44c822007-11-25 14:01:38 +0000329}
330
bart28230a32008-02-29 17:27:03 +0000331/** Called after pthread_cond_destroy(). */
sewardj85642922008-01-14 11:54:56 +0000332void cond_thread_delete(const DrdThreadId tid)
333{ }