blob: c5b04a4b77bcec17ca0fff4472eb2dc5d6a5f612 [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
bart28230a32008-02-29 17:27:03 +000073/** Free the memory that was allocated by cond_initialize(). Called by
bart72b751c2008-03-01 13:44:24 +000074 * clientobj_remove().
bart28230a32008-02-29 17:27:03 +000075 */
76static void cond_cleanup(struct cond_info* p)
77{
78 tl_assert(p);
79 if (p->mutex)
80 {
81 struct mutex_info* q;
bart72b751c2008-03-01 13:44:24 +000082 q = &clientobj_get(p->mutex, ClientMutex)->mutex;
bart3b1ee452008-02-29 19:28:15 +000083 tl_assert(q);
84 {
85 CondDestrErrInfo cde = { p->a1, q->a1, q->owner };
86 VG_(maybe_record_error)(VG_(get_running_tid)(),
87 CondDestrErr,
88 VG_(get_IP)(VG_(get_running_tid)()),
89 "Destroying condition variable that is being"
90 " waited upon",
91 &cde);
92 }
bart28230a32008-02-29 17:27:03 +000093 }
94}
95
bart0268dfa2008-03-11 20:10:21 +000096static struct cond_info* cond_get_or_allocate(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +000097{
bart28230a32008-02-29 17:27:03 +000098 struct cond_info *p;
99
100 tl_assert(offsetof(DrdClientobj, cond) == 0);
bart72b751c2008-03-01 13:44:24 +0000101 p = &clientobj_get(cond, ClientCondvar)->cond;
bart28230a32008-02-29 17:27:03 +0000102 if (p == 0)
sewardj721ad7b2007-11-30 08:30:29 +0000103 {
bart0268dfa2008-03-11 20:10:21 +0000104 p = &clientobj_add(cond, ClientCondvar)->cond;
105 cond_initialize(p, cond);
sewardj721ad7b2007-11-30 08:30:29 +0000106 }
bart28230a32008-02-29 17:27:03 +0000107 return p;
sewardjaf44c822007-11-25 14:01:38 +0000108}
109
bart72b751c2008-03-01 13:44:24 +0000110static struct cond_info* cond_get(const Addr cond)
bart28230a32008-02-29 17:27:03 +0000111{
112 tl_assert(offsetof(DrdClientobj, cond) == 0);
bart72b751c2008-03-01 13:44:24 +0000113 return &clientobj_get(cond, ClientCondvar)->cond;
bart28230a32008-02-29 17:27:03 +0000114}
115
116/** Called before pthread_cond_init(). */
bart0268dfa2008-03-11 20:10:21 +0000117void cond_pre_init(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000118{
bart72b751c2008-03-01 13:44:24 +0000119 struct cond_info* p;
120
sewardjaf44c822007-11-25 14:01:38 +0000121 if (s_trace_cond)
122 {
bart3b1ee452008-02-29 19:28:15 +0000123 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000124 "[%d/%d] cond_init cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000125 VG_(get_running_tid)(),
126 thread_get_running_tid(),
127 cond);
sewardjaf44c822007-11-25 14:01:38 +0000128 }
bart72b751c2008-03-01 13:44:24 +0000129
bart72b751c2008-03-01 13:44:24 +0000130 p = cond_get(cond);
131
132 if (p)
133 {
134 CondErrInfo cei = { .cond = cond };
135 VG_(maybe_record_error)(VG_(get_running_tid)(),
136 CondErr,
137 VG_(get_IP)(VG_(get_running_tid)()),
138 "initialized twice",
139 &cei);
140 }
141
bart0268dfa2008-03-11 20:10:21 +0000142 p = cond_get_or_allocate(cond);
sewardjaf44c822007-11-25 14:01:38 +0000143}
144
bart28230a32008-02-29 17:27:03 +0000145/** Called after pthread_cond_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000146void cond_post_destroy(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000147{
bart72b751c2008-03-01 13:44:24 +0000148 struct cond_info* p;
149
sewardjaf44c822007-11-25 14:01:38 +0000150 if (s_trace_cond)
151 {
bart3b1ee452008-02-29 19:28:15 +0000152 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000153 "[%d/%d] cond_destroy cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000154 VG_(get_running_tid)(),
155 thread_get_running_tid(),
bart72b751c2008-03-01 13:44:24 +0000156 cond);
sewardjaf44c822007-11-25 14:01:38 +0000157 }
158
bart72b751c2008-03-01 13:44:24 +0000159 p = cond_get(cond);
160 if (p == 0)
161 {
162 CondErrInfo cei = { .cond = cond };
163 VG_(maybe_record_error)(VG_(get_running_tid)(),
164 CondErr,
165 VG_(get_IP)(VG_(get_running_tid)()),
166 "not a condition variable",
167 &cei);
168 return;
169 }
sewardjaf44c822007-11-25 14:01:38 +0000170
bart72b751c2008-03-01 13:44:24 +0000171 if (p->waiter_count != 0)
172 {
173 CondErrInfo cei = { .cond = cond };
174 VG_(maybe_record_error)(VG_(get_running_tid)(),
175 CondErr,
176 VG_(get_IP)(VG_(get_running_tid)()),
177 "destruction of condition variable being waited"
178 " upon",
179 &cei);
180 }
181
182 clientobj_remove(p->a1, ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +0000183}
184
bart08e6d6a2008-06-28 16:28:49 +0000185/** Called before pthread_cond_wait(). Note: before this function is called,
186 * mutex_unlock() has already been called from drd_clientreq.c.
187 */
bart0268dfa2008-03-11 20:10:21 +0000188int cond_pre_wait(const Addr cond, const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000189{
190 struct cond_info* p;
bart08e6d6a2008-06-28 16:28:49 +0000191 struct mutex_info* q;
sewardjaf44c822007-11-25 14:01:38 +0000192
bart3b1ee452008-02-29 19:28:15 +0000193 if (s_trace_cond)
194 {
195 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000196 "[%d/%d] cond_pre_wait cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000197 VG_(get_running_tid)(),
198 thread_get_running_tid(),
199 cond);
200 }
201
bart0268dfa2008-03-11 20:10:21 +0000202 p = cond_get_or_allocate(cond);
bart3b1ee452008-02-29 19:28:15 +0000203 tl_assert(p);
204
sewardjaf44c822007-11-25 14:01:38 +0000205 if (p->waiter_count == 0)
206 {
207 p->mutex = mutex;
208 }
bart3bb1cec2008-06-28 16:01:43 +0000209 else if (p->mutex != mutex)
sewardjaf44c822007-11-25 14:01:38 +0000210 {
bart3bb1cec2008-06-28 16:01:43 +0000211 CondWaitErrInfo cwei
212 = { .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex };
213 VG_(maybe_record_error)(VG_(get_running_tid)(),
214 CondWaitErr,
215 VG_(get_IP)(VG_(get_running_tid)()),
216 "Inconsistent association of condition variable"
217 " and mutex",
218 &cwei);
sewardjaf44c822007-11-25 14:01:38 +0000219 }
bart08e6d6a2008-06-28 16:28:49 +0000220 tl_assert(p->mutex);
221 q = mutex_get(p->mutex);
222 if (q && q->recursion_count > 0)
223 {
224 const ThreadId vg_tid = VG_(get_running_tid)();
225 MutexErrInfo MEI = { q->a1, q->recursion_count, q->owner };
226 VG_(maybe_record_error)(vg_tid,
227 MutexErr,
228 VG_(get_IP)(vg_tid),
229 "Mutex locked recursively",
230 &MEI);
231 }
232 else if (q == 0)
233 {
234 not_a_mutex(p->mutex);
235 }
236
sewardjaf44c822007-11-25 14:01:38 +0000237 return ++p->waiter_count;
238}
239
bart28230a32008-02-29 17:27:03 +0000240/** Called after pthread_cond_wait(). */
sewardjaf44c822007-11-25 14:01:38 +0000241int cond_post_wait(const Addr cond)
242{
243 struct cond_info* p;
244
bart3b1ee452008-02-29 19:28:15 +0000245 if (s_trace_cond)
246 {
247 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000248 "[%d/%d] cond_post_wait cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000249 VG_(get_running_tid)(),
250 thread_get_running_tid(),
251 cond);
252 }
253
sewardjaf44c822007-11-25 14:01:38 +0000254 p = cond_get(cond);
bart00344642008-03-01 15:27:41 +0000255 // To do: print a proper error message if the assert below fails.
sewardjaf44c822007-11-25 14:01:38 +0000256 tl_assert(p);
bart00344642008-03-01 15:27:41 +0000257 // To do: print a proper error message if the assert below fails.
sewardjaf44c822007-11-25 14:01:38 +0000258 tl_assert(p->waiter_count > 0);
259 tl_assert(p->mutex);
260 if (--p->waiter_count == 0)
261 {
262 p->mutex = 0;
263 }
264 return p->waiter_count;
265}
266
bart3b1ee452008-02-29 19:28:15 +0000267static void cond_signal(Addr const cond)
sewardjaf44c822007-11-25 14:01:38 +0000268{
269 const ThreadId vg_tid = VG_(get_running_tid)();
270 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
271 struct cond_info* const cond_p = cond_get(cond);
bart3b1ee452008-02-29 19:28:15 +0000272
sewardjaf44c822007-11-25 14:01:38 +0000273 if (cond_p && cond_p->waiter_count > 0)
274 {
bart46b5fce2008-06-28 13:01:30 +0000275 if (s_drd_report_signal_unlocked
276 && ! mutex_is_locked_by(cond_p->mutex, drd_tid))
sewardjaf44c822007-11-25 14:01:38 +0000277 {
bart11608002008-03-13 17:47:01 +0000278 /* A signal is sent while the associated mutex has not been locked. */
279 /* This can indicate but is not necessarily a race condition. */
sewardjaf44c822007-11-25 14:01:38 +0000280 CondRaceErrInfo cei;
281 cei.cond = cond;
282 cei.mutex = cond_p->mutex;
283 VG_(maybe_record_error)(vg_tid,
284 CondRaceErr,
285 VG_(get_IP)(vg_tid),
286 "CondErr",
287 &cei);
288 }
289 }
290 else
291 {
292 /* No other thread is waiting for the signal, hence the signal will be */
293 /* lost. This is normal in a POSIX threads application. */
294 }
295}
296
bart3b1ee452008-02-29 19:28:15 +0000297/** Called before pthread_cond_signal(). */
298void cond_pre_signal(Addr const cond)
299{
300 if (s_trace_cond)
301 {
302 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000303 "[%d/%d] cond_signal cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000304 VG_(get_running_tid)(),
305 thread_get_running_tid(),
306 cond);
307 }
308
309 cond_signal(cond);
310}
311
bart28230a32008-02-29 17:27:03 +0000312/** Called before pthread_cond_broadcast(). */
sewardjaf44c822007-11-25 14:01:38 +0000313void cond_pre_broadcast(Addr const cond)
314{
bart3b1ee452008-02-29 19:28:15 +0000315 if (s_trace_cond)
316 {
317 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000318 "[%d/%d] cond_broadcast cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000319 VG_(get_running_tid)(),
320 thread_get_running_tid(),
321 cond);
322 }
323
324 cond_signal(cond);
sewardjaf44c822007-11-25 14:01:38 +0000325}
326
bart28230a32008-02-29 17:27:03 +0000327/** Called after pthread_cond_destroy(). */
sewardj85642922008-01-14 11:54:56 +0000328void cond_thread_delete(const DrdThreadId tid)
329{ }