syswrap openat2 for all linux arches
[valgrind.git] / drd / drd_cond.c
blob0e35eb140c67ed5dc9cfd76f7aca2a752318c0df
1 /*
2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2020 Bart Van Assche <bvanassche@acm.org>.
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.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
19 The GNU General Public License is contained in the file COPYING.
23 #include "drd_clientobj.h"
24 #include "drd_cond.h"
25 #include "drd_error.h"
26 #include "drd_mutex.h"
27 #include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
28 #include "pub_tool_libcassert.h" /* tl_assert() */
29 #include "pub_tool_libcbase.h" /* VG_(memcmp)() */
30 #include "pub_tool_libcprint.h" /* VG_(printf)() */
31 #include "pub_tool_machine.h" /* VG_(get_IP)() */
32 #include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
35 /* Local functions. */
37 static void DRD_(cond_cleanup)(struct cond_info* p);
40 /* Local variables. */
42 static Bool DRD_(s_report_signal_unlocked) = True;
43 static Bool DRD_(s_trace_cond);
46 /* Function definitions. */
48 void DRD_(cond_set_report_signal_unlocked)(const Bool r)
50 DRD_(s_report_signal_unlocked) = r;
53 void DRD_(cond_set_trace)(const Bool trace_cond)
55 DRD_(s_trace_cond) = trace_cond;
58 static
59 void DRD_(cond_initialize)(struct cond_info* const p, const Addr cond)
61 tl_assert(cond != 0);
62 tl_assert(p->a1 == cond);
63 tl_assert(p->type == ClientCondvar);
65 p->cleanup = (void(*)(DrdClientobj*))(DRD_(cond_cleanup));
66 p->delete_thread = 0;
67 p->waiter_count = 0;
68 p->mutex = 0;
71 /**
72 * Free the memory that was allocated by cond_initialize(). Called by
73 * DRD_(clientobj_remove)().
75 static void DRD_(cond_cleanup)(struct cond_info* p)
77 tl_assert(p);
78 if (p->mutex)
80 struct mutex_info* q;
81 q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex);
83 CondDestrErrInfo cde = {
84 DRD_(thread_get_running_tid)(),
85 p->a1,
86 q ? q->a1 : 0,
87 q ? q->owner : DRD_INVALID_THREADID
89 VG_(maybe_record_error)(VG_(get_running_tid)(),
90 CondDestrErr,
91 VG_(get_IP)(VG_(get_running_tid)()),
92 "Destroying condition variable that is being"
93 " waited upon",
94 &cde);
99 /**
100 * Report that the synchronization object at address 'addr' is of the
101 * wrong type.
103 static void wrong_type(const Addr addr)
105 GenericErrInfo gei = {
106 .tid = DRD_(thread_get_running_tid)(),
107 .addr = addr,
109 VG_(maybe_record_error)(VG_(get_running_tid)(),
110 GenericErr,
111 VG_(get_IP)(VG_(get_running_tid)()),
112 "wrong type of synchronization object",
113 &gei);
116 static struct cond_info* cond_get_or_allocate(const Addr cond)
118 struct cond_info *p;
120 tl_assert(offsetof(DrdClientobj, cond) == 0);
121 p = &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
122 if (p)
123 return p;
125 if (DRD_(clientobj_present)(cond, cond + 1))
127 wrong_type(cond);
128 return 0;
131 p = &(DRD_(clientobj_add)(cond, ClientCondvar)->cond);
132 DRD_(cond_initialize)(p, cond);
133 return p;
136 struct cond_info* DRD_(cond_get)(const Addr cond)
138 tl_assert(offsetof(DrdClientobj, cond) == 0);
139 return &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
142 /** Called before pthread_cond_init(). */
143 void DRD_(cond_pre_init)(const Addr cond)
145 struct cond_info* p;
147 if (DRD_(s_trace_cond))
148 DRD_(trace_msg)("[%u] cond_init cond 0x%lx",
149 DRD_(thread_get_running_tid)(), cond);
151 p = DRD_(cond_get)(cond);
153 if (p) {
154 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
155 VG_(maybe_record_error)(VG_(get_running_tid)(),
156 CondErr,
157 VG_(get_IP)(VG_(get_running_tid)()),
158 "initialized twice",
159 &cei);
162 cond_get_or_allocate(cond);
165 /** Called after pthread_cond_destroy(). */
166 void DRD_(cond_post_destroy)(const Addr cond, const Bool destroy_succeeded)
168 struct cond_info* p;
170 if (DRD_(s_trace_cond))
171 DRD_(trace_msg)("[%u] cond_destroy cond 0x%lx",
172 DRD_(thread_get_running_tid)(), cond);
174 p = DRD_(cond_get)(cond);
175 if (p == 0)
177 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
178 VG_(maybe_record_error)(VG_(get_running_tid)(),
179 CondErr,
180 VG_(get_IP)(VG_(get_running_tid)()),
181 "not a condition variable",
182 &cei);
183 return;
186 if (p->waiter_count != 0)
188 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
189 VG_(maybe_record_error)(VG_(get_running_tid)(),
190 CondErr,
191 VG_(get_IP)(VG_(get_running_tid)()),
192 "destruction of condition variable being waited"
193 " upon",
194 &cei);
197 if (destroy_succeeded)
198 DRD_(clientobj_remove)(p->a1, ClientCondvar);
202 * Called before pthread_cond_wait(). Note: before this function is called,
203 * mutex_unlock() has already been called from drd_clientreq.c.
205 void DRD_(cond_pre_wait)(const Addr cond, const Addr mutex)
207 struct cond_info* p;
208 struct mutex_info* q;
210 if (DRD_(s_trace_cond))
211 DRD_(trace_msg)("[%u] cond_pre_wait cond 0x%lx",
212 DRD_(thread_get_running_tid)(), cond);
214 p = cond_get_or_allocate(cond);
215 if (!p)
217 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
218 VG_(maybe_record_error)(VG_(get_running_tid)(),
219 CondErr,
220 VG_(get_IP)(VG_(get_running_tid)()),
221 "not a condition variable",
222 &cei);
223 return;
226 if (p->waiter_count == 0)
228 p->mutex = mutex;
230 else if (p->mutex != mutex)
232 CondWaitErrInfo cwei
233 = { .tid = DRD_(thread_get_running_tid)(),
234 .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex };
235 VG_(maybe_record_error)(VG_(get_running_tid)(),
236 CondWaitErr,
237 VG_(get_IP)(VG_(get_running_tid)()),
238 "Inconsistent association of condition variable"
239 " and mutex",
240 &cwei);
242 tl_assert(p->mutex);
243 q = DRD_(mutex_get)(p->mutex);
244 if (q
245 && q->owner == DRD_(thread_get_running_tid)() && q->recursion_count > 0)
247 const ThreadId vg_tid = VG_(get_running_tid)();
248 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
249 q->a1, q->recursion_count, q->owner };
250 VG_(maybe_record_error)(vg_tid,
251 MutexErr,
252 VG_(get_IP)(vg_tid),
253 "Mutex locked recursively",
254 &MEI);
256 else if (q == 0)
258 DRD_(not_a_mutex)(p->mutex);
261 ++p->waiter_count;
265 * Called after pthread_cond_wait().
267 void DRD_(cond_post_wait)(const Addr cond)
269 struct cond_info* p;
271 if (DRD_(s_trace_cond))
272 DRD_(trace_msg)("[%u] cond_post_wait cond 0x%lx",
273 DRD_(thread_get_running_tid)(), cond);
275 p = DRD_(cond_get)(cond);
276 if (!p)
278 CondDestrErrInfo cde = {
279 DRD_(thread_get_running_tid)(), cond, 0, DRD_INVALID_THREADID
281 VG_(maybe_record_error)(VG_(get_running_tid)(),
282 CondDestrErr,
283 VG_(get_IP)(VG_(get_running_tid)()),
284 "condition variable has been destroyed while"
285 " being waited upon",
286 &cde);
287 return;
290 if (p->waiter_count > 0)
292 --p->waiter_count;
293 if (p->waiter_count == 0)
295 p->mutex = 0;
300 static void cond_signal(const DrdThreadId tid, struct cond_info* const cond_p)
302 const ThreadId vg_tid = VG_(get_running_tid)();
303 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
305 tl_assert(cond_p);
307 if (cond_p->waiter_count > 0)
309 if (DRD_(s_report_signal_unlocked)
310 && ! DRD_(mutex_is_locked_by)(cond_p->mutex, drd_tid))
313 * A signal is sent while the associated mutex has not been locked.
314 * This can indicate but is not necessarily a race condition.
316 CondRaceErrInfo cei = { .tid = DRD_(thread_get_running_tid)(),
317 .cond = cond_p->a1,
318 .mutex = cond_p->mutex,
320 VG_(maybe_record_error)(vg_tid,
321 CondRaceErr,
322 VG_(get_IP)(vg_tid),
323 "CondErr",
324 &cei);
327 else
330 * No other thread is waiting for the signal, hence the signal will
331 * be lost. This is normal in a POSIX threads application.
336 static void not_initialized(Addr const cond)
338 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
339 VG_(maybe_record_error)(VG_(get_running_tid)(),
340 CondErr,
341 VG_(get_IP)(VG_(get_running_tid)()),
342 "condition variable has not been initialized",
343 &cei);
346 /** Called before pthread_cond_signal(). */
347 void DRD_(cond_pre_signal)(Addr const cond)
349 struct cond_info* p;
351 p = DRD_(cond_get)(cond);
352 if (DRD_(s_trace_cond))
353 DRD_(trace_msg)("[%u] cond_signal cond 0x%lx",
354 DRD_(thread_get_running_tid)(), cond);
356 tl_assert(DRD_(pthread_cond_initializer));
357 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
358 DRD_(pthread_cond_initializer_size)) != 0)
360 not_initialized(cond);
361 return;
364 if (!p)
365 p = cond_get_or_allocate(cond);
367 cond_signal(DRD_(thread_get_running_tid)(), p);
370 /** Called before pthread_cond_broadcast(). */
371 void DRD_(cond_pre_broadcast)(Addr const cond)
373 struct cond_info* p;
375 if (DRD_(s_trace_cond))
376 DRD_(trace_msg)("[%u] cond_broadcast cond 0x%lx",
377 DRD_(thread_get_running_tid)(), cond);
379 p = DRD_(cond_get)(cond);
380 tl_assert(DRD_(pthread_cond_initializer));
381 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
382 DRD_(pthread_cond_initializer_size)) != 0)
384 not_initialized(cond);
385 return;
388 if (!p)
389 p = cond_get_or_allocate(cond);
391 cond_signal(DRD_(thread_get_running_tid)(), p);