1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 This file is part of drd, a thread error detector.
5 Copyright (C) 2006-2010 Bart Van Assche <bart.vanassche@gmail.com>.
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.
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.
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
22 The GNU General Public License is contained in the file COPYING.
26 #include "drd_clientobj.h"
27 #include "drd_error.h"
28 #include "drd_semaphore.h"
29 #include "drd_suppression.h"
30 #include "pub_tool_errormgr.h" // VG_(maybe_record_error)()
31 #include "pub_tool_libcassert.h" // tl_assert()
32 #include "pub_tool_libcprint.h" // VG_(printf)()
33 #include "pub_tool_machine.h" // VG_(get_IP)()
34 #include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free)
35 #include "pub_tool_threadstate.h" // VG_(get_running_tid)()
38 /* Local functions. */
40 static void semaphore_cleanup(struct semaphore_info* p);
43 /* Local variables. */
45 static Bool s_trace_semaphore;
46 static ULong s_semaphore_segment_creation_count;
49 /* Function definitions. */
51 /** Push a segment at the end of the queue 'p->last_sem_post_seg'. */
52 static void drd_segment_push(struct semaphore_info* p, Segment* sg)
57 n = VG_(addToXA)(p->last_sem_post_seg, &sg);
59 VG_(message)(Vg_UserMsg, "0x%lx push: added at position %ld/%ld",
60 p->a1, n, VG_(sizeXA)(p->last_sem_post_seg));
62 tl_assert(*(Segment**)VG_(indexXA)(p->last_sem_post_seg, n) == sg);
65 /** Pop a segment from the beginning of the queue 'p->last_sem_post_seg'. */
66 static Segment* drd_segment_pop(struct semaphore_info* p)
71 sz = VG_(sizeXA)(p->last_sem_post_seg);
73 VG_(message)(Vg_UserMsg, "0x%lx pop: removed from position %ld/%ld",
79 sg = *(Segment**)VG_(indexXA)(p->last_sem_post_seg, sz - 1);
81 VG_(dropTailXA)(p->last_sem_post_seg, 1);
86 /** Enable or disable tracing of semaphore actions. */
87 void DRD_(semaphore_set_trace)(const Bool trace_semaphore)
89 s_trace_semaphore = trace_semaphore;
93 * Initialize the memory 'p' points at as a semaphore_info structure for the
94 * client semaphore at client addres 'semaphore'.
97 void drd_semaphore_initialize(struct semaphore_info* const p,
100 tl_assert(semaphore != 0);
101 tl_assert(p->a1 == semaphore);
102 tl_assert(p->type == ClientSemaphore);
104 p->cleanup = (void(*)(DrdClientobj*))semaphore_cleanup;
105 p->delete_thread = 0;
106 p->waits_to_skip = 0;
109 p->last_sem_post_tid = DRD_INVALID_THREADID;
110 p->last_sem_post_seg = VG_(newXA)(VG_(malloc), "drd.sg-stack",
111 VG_(free), sizeof(Segment*));
115 * Free the memory that was allocated by semaphore_initialize(). Called by
116 * DRD_(clientobj_remove)().
118 static void semaphore_cleanup(struct semaphore_info* p)
124 SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), p->a1 };
125 VG_(maybe_record_error)(VG_(get_running_tid)(),
127 VG_(get_IP)(VG_(get_running_tid)()),
128 "Destruction of semaphore that is being waited"
132 while ((sg = drd_segment_pop(p)))
134 VG_(deleteXA)(p->last_sem_post_seg);
138 * Return a pointer to the structure with information about the specified
139 * client semaphore. Allocate a new structure if such a structure did not
143 struct semaphore_info*
144 drd_semaphore_get_or_allocate(const Addr semaphore)
146 struct semaphore_info *p;
148 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
149 p = &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore);
152 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
153 p = &(DRD_(clientobj_add)(semaphore, ClientSemaphore)->semaphore);
154 drd_semaphore_initialize(p, semaphore);
160 * Return a pointer to the structure with information about the specified
161 * client semaphore, or null if no such structure was found.
163 static struct semaphore_info* semaphore_get(const Addr semaphore)
165 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
166 return &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore);
169 /** Called before sem_init(). */
170 struct semaphore_info* DRD_(semaphore_init)(const Addr semaphore,
174 struct semaphore_info* p;
177 if (s_trace_semaphore)
179 VG_(message)(Vg_UserMsg,
180 "[%d] sem_init 0x%lx value %u\n",
181 DRD_(thread_get_running_tid)(),
185 p = semaphore_get(semaphore);
188 const ThreadId vg_tid = VG_(get_running_tid)();
189 SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore };
190 VG_(maybe_record_error)(vg_tid,
193 "Semaphore reinitialization",
195 // Remove all segments from the segment stack.
196 while ((sg = drd_segment_pop(p)))
203 #if defined(VGO_darwin)
204 const ThreadId vg_tid = VG_(get_running_tid)();
205 GenericErrInfo GEI = { DRD_(thread_get_running_tid)(), NULL };
206 VG_(maybe_record_error)(vg_tid,
209 "sem_init() is not yet supported on Darwin",
213 p = drd_semaphore_get_or_allocate(semaphore);
217 p->waits_to_skip = value;
222 /** Called after sem_destroy(). */
223 void DRD_(semaphore_destroy)(const Addr semaphore)
225 struct semaphore_info* p;
227 p = semaphore_get(semaphore);
229 if (s_trace_semaphore)
231 VG_(message)(Vg_UserMsg,
232 "[%d] sem_destroy 0x%lx value %u\n",
233 DRD_(thread_get_running_tid)(),
240 GenericErrInfo GEI = {
241 .tid = DRD_(thread_get_running_tid)(),
244 VG_(maybe_record_error)(VG_(get_running_tid)(),
246 VG_(get_IP)(VG_(get_running_tid)()),
252 DRD_(clientobj_remove)(semaphore, ClientSemaphore);
255 /** Called after sem_open(). */
256 struct semaphore_info* DRD_(semaphore_open)(const Addr semaphore,
257 const Char* name, const Word oflag,
258 const Word mode, const UInt value)
260 struct semaphore_info* p;
263 if (s_trace_semaphore)
265 VG_(message)(Vg_UserMsg,
266 "[%d] sem_open 0x%lx name %s"
267 " oflag %#lx mode %#lo value %u\n",
268 DRD_(thread_get_running_tid)(),
269 semaphore, name, oflag, mode, value);
272 /* Return if the sem_open() call failed. */
276 p = semaphore_get(semaphore);
279 const ThreadId vg_tid = VG_(get_running_tid)();
280 SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore };
281 VG_(maybe_record_error)(vg_tid,
284 "Semaphore reinitialization",
286 // Remove all segments from the segment stack.
287 while ((sg = drd_segment_pop(p)))
294 p = drd_semaphore_get_or_allocate(semaphore);
297 p->waits_to_skip = value;
302 /** Called before sem_close(). */
303 void DRD_(semaphore_close)(const Addr semaphore)
305 struct semaphore_info* p;
307 p = semaphore_get(semaphore);
309 if (s_trace_semaphore)
311 VG_(message)(Vg_UserMsg,
312 "[%d] sem_close 0x%lx value %u\n",
313 DRD_(thread_get_running_tid)(),
320 GenericErrInfo GEI = {
321 .tid = DRD_(thread_get_running_tid)(),
324 VG_(maybe_record_error)(VG_(get_running_tid)(),
326 VG_(get_IP)(VG_(get_running_tid)()),
332 DRD_(clientobj_remove)(semaphore, ClientSemaphore);
335 /** Called before sem_wait(). */
336 void DRD_(semaphore_pre_wait)(const Addr semaphore)
338 struct semaphore_info* p;
340 p = drd_semaphore_get_or_allocate(semaphore);
344 if ((Word)(p->waiters) <= 0)
346 SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore };
347 VG_(maybe_record_error)(VG_(get_running_tid)(),
349 VG_(get_IP)(VG_(get_running_tid)()),
356 * Called after sem_wait() finished.
357 * @note Do not rely on the value of 'waited' -- some glibc versions do
358 * not set it correctly.
360 void DRD_(semaphore_post_wait)(const DrdThreadId tid, const Addr semaphore,
363 struct semaphore_info* p;
366 p = semaphore_get(semaphore);
367 if (s_trace_semaphore)
369 VG_(message)(Vg_UserMsg,
370 "[%d] sem_wait 0x%lx value %u -> %u\n",
371 DRD_(thread_get_running_tid)(),
374 p ? p->value - 1 : 0);
384 * Note: if another thread destroyed and reinitialized a semaphore while
385 * the current thread was waiting in sem_wait, p->waiters may have been
386 * set to zero by drd_semaphore_initialize() after
387 * DRD_(semaphore_pre_wait)() has finished before
388 * DRD_(semaphore_post_wait)() has been called.
390 if (p == NULL || (Int)(p->value) < 0 || (Word)(p->waiters) < 0)
392 SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore };
393 VG_(maybe_record_error)(VG_(get_running_tid)(),
395 VG_(get_IP)(VG_(get_running_tid)()),
401 if (p->waits_to_skip > 0)
405 sg = drd_segment_pop(p);
409 if (p->last_sem_post_tid != tid
410 && p->last_sem_post_tid != DRD_INVALID_THREADID)
412 DRD_(thread_new_segment_and_combine_vc)(tid, sg);
415 DRD_(thread_new_segment)(tid);
416 s_semaphore_segment_creation_count++;
422 /** Called before sem_post(). */
423 void DRD_(semaphore_pre_post)(const DrdThreadId tid, const Addr semaphore)
425 struct semaphore_info* p;
428 p = drd_semaphore_get_or_allocate(semaphore);
431 if (s_trace_semaphore)
433 VG_(message)(Vg_UserMsg,
434 "[%d] sem_post 0x%lx value %u -> %u\n",
435 DRD_(thread_get_running_tid)(),
437 p->value - 1, p->value);
440 p->last_sem_post_tid = tid;
442 DRD_(thread_get_latest_segment)(&sg, tid);
444 drd_segment_push(p, sg);
445 DRD_(thread_new_segment)(tid);
446 s_semaphore_segment_creation_count++;
449 /** Called after sem_post() finished. */
450 void DRD_(semaphore_post_post)(const DrdThreadId tid, const Addr semaphore,
451 const Bool succeeded)
454 * Note: it is hard to implement the sem_post() wrapper correctly in
455 * case sem_post() returns an error code. This is because handling this
456 * case correctly requires restoring the vector clock associated with
457 * the semaphore to its original value here. In order to do that without
458 * introducing a race condition, extra locking has to be added around
459 * each semaphore call. Such extra locking would have to be added in
460 * drd_pthread_intercepts.c. However, it is hard to implement
461 * synchronization in drd_pthread_intercepts.c in a portable way without
462 * calling already redirected functions.
466 ULong DRD_(get_semaphore_segment_creation_count)(void)
468 return s_semaphore_segment_creation_count;