drd_semaphore.c revision 352db6d8babe42e5432eb6a2b92e605d0204eed2
1/* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
2/*
3  This file is part of drd, a thread error detector.
4
5  Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
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
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)()
36
37
38/* Local functions. */
39
40static void semaphore_cleanup(struct semaphore_info* p);
41
42
43/* Local variables. */
44
45static Bool s_trace_semaphore;
46static ULong s_semaphore_segment_creation_count;
47
48
49/* Function definitions. */
50
51/** Push a segment at the end of the queue 'p->last_sem_post_seg'. */
52static void drd_segment_push(struct semaphore_info* p, Segment* sg)
53{
54   Word n;
55
56   tl_assert(sg);
57   n = VG_(addToXA)(p->last_sem_post_seg, &sg);
58#if 0
59   VG_(message)(Vg_DebugMsg, "0x%lx push: added at position %ld/%ld",
60                p->a1, n, VG_(sizeXA)(p->last_sem_post_seg));
61#endif
62   tl_assert(*(Segment**)VG_(indexXA)(p->last_sem_post_seg, n) == sg);
63}
64
65/** Pop a segment from the beginning of the queue 'p->last_sem_post_seg'. */
66static Segment* drd_segment_pop(struct semaphore_info* p)
67{
68   Word sz;
69   Segment* sg;
70
71   sz = VG_(sizeXA)(p->last_sem_post_seg);
72#if 0
73   VG_(message)(Vg_DebugMsg, "0x%lx pop:  removed from position %ld/%ld",
74                p->a1, sz - 1, sz);
75#endif
76   sg = 0;
77   if (sz > 0)
78   {
79      sg = *(Segment**)VG_(indexXA)(p->last_sem_post_seg, sz - 1);
80      tl_assert(sg);
81      VG_(dropTailXA)(p->last_sem_post_seg, 1);
82   }
83   return sg;
84}
85
86/** Enable or disable tracing of semaphore actions. */
87void DRD_(semaphore_set_trace)(const Bool trace_semaphore)
88{
89   s_trace_semaphore = trace_semaphore;
90}
91
92/**
93 * Initialize the memory 'p' points at as a semaphore_info structure for the
94 * client semaphore at client addres 'semaphore'.
95 */
96static
97void drd_semaphore_initialize(struct semaphore_info* const p,
98                              const Addr semaphore)
99{
100   tl_assert(semaphore != 0);
101   tl_assert(p->a1 == semaphore);
102   tl_assert(p->type == ClientSemaphore);
103
104   p->cleanup           = (void(*)(DrdClientobj*))semaphore_cleanup;
105   p->delete_thread     = 0;
106   p->waits_to_skip     = 0;
107   p->value             = 0;
108   p->waiters           = 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*));
112}
113
114/**
115 * Free the memory that was allocated by semaphore_initialize(). Called by
116 * DRD_(clientobj_remove)().
117 */
118static void semaphore_cleanup(struct semaphore_info* p)
119{
120   Segment* sg;
121
122   if (p->waiters > 0)
123   {
124      SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), p->a1 };
125      VG_(maybe_record_error)(VG_(get_running_tid)(),
126                              SemaphoreErr,
127                              VG_(get_IP)(VG_(get_running_tid)()),
128                              "Destruction of semaphore that is being waited"
129                              " upon",
130                              &sei);
131   }
132   while ((sg = drd_segment_pop(p)))
133      DRD_(sg_put)(sg);
134   VG_(deleteXA)(p->last_sem_post_seg);
135}
136
137/**
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
140 * yet exist.
141 */
142static
143struct semaphore_info*
144drd_semaphore_get_or_allocate(const Addr semaphore)
145{
146   struct semaphore_info *p;
147
148   tl_assert(offsetof(DrdClientobj, semaphore) == 0);
149   p = &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore);
150   if (p == 0)
151   {
152      tl_assert(offsetof(DrdClientobj, semaphore) == 0);
153      p = &(DRD_(clientobj_add)(semaphore, ClientSemaphore)->semaphore);
154      drd_semaphore_initialize(p, semaphore);
155   }
156   return p;
157}
158
159/**
160 * Return a pointer to the structure with information about the specified
161 * client semaphore, or null if no such structure was found.
162 */
163static struct semaphore_info* semaphore_get(const Addr semaphore)
164{
165   tl_assert(offsetof(DrdClientobj, semaphore) == 0);
166   return &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore);
167}
168
169/** Called before sem_init(). */
170struct semaphore_info* DRD_(semaphore_init)(const Addr semaphore,
171                                            const Word pshared,
172                                            const UInt value)
173{
174   struct semaphore_info* p;
175   Segment* sg;
176
177   if (s_trace_semaphore)
178      DRD_(trace_msg)("[%d] sem_init      0x%lx value %u\n",
179                      DRD_(thread_get_running_tid)(), semaphore, value);
180
181   p = semaphore_get(semaphore);
182   if (p)
183   {
184      const ThreadId vg_tid = VG_(get_running_tid)();
185      SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore };
186      VG_(maybe_record_error)(vg_tid,
187                              SemaphoreErr,
188                              VG_(get_IP)(vg_tid),
189                              "Semaphore reinitialization",
190                              &SEI);
191      // Remove all segments from the segment stack.
192      while ((sg = drd_segment_pop(p)))
193      {
194         DRD_(sg_put)(sg);
195      }
196   }
197   else
198   {
199#if defined(VGO_darwin)
200      const ThreadId vg_tid = VG_(get_running_tid)();
201      GenericErrInfo GEI = { DRD_(thread_get_running_tid)(), 0 };
202      VG_(maybe_record_error)(vg_tid,
203			      GenericErr,
204			      VG_(get_IP)(vg_tid),
205			      "sem_init() is not yet supported on Darwin",
206			      &GEI);
207      return NULL;
208#else
209      p = drd_semaphore_get_or_allocate(semaphore);
210#endif
211   }
212   tl_assert(p);
213   p->waits_to_skip = value;
214   p->value         = value;
215   return p;
216}
217
218/** Called after sem_destroy(). */
219void DRD_(semaphore_destroy)(const Addr semaphore)
220{
221   struct semaphore_info* p;
222
223   p = semaphore_get(semaphore);
224
225   if (s_trace_semaphore)
226      DRD_(trace_msg)("[%d] sem_destroy   0x%lx value %u\n",
227                      DRD_(thread_get_running_tid)(), semaphore,
228                      p ? p->value : 0);
229
230   if (p == 0)
231   {
232      GenericErrInfo GEI = {
233	 .tid  = DRD_(thread_get_running_tid)(),
234	 .addr = semaphore,
235      };
236      VG_(maybe_record_error)(VG_(get_running_tid)(),
237                              GenericErr,
238                              VG_(get_IP)(VG_(get_running_tid)()),
239                              "Not a semaphore",
240                              &GEI);
241      return;
242   }
243
244   DRD_(clientobj_remove)(semaphore, ClientSemaphore);
245}
246
247/** Called after sem_open(). */
248struct semaphore_info* DRD_(semaphore_open)(const Addr semaphore,
249                                            const Char* name, const Word oflag,
250                                            const Word mode, const UInt value)
251{
252   struct semaphore_info* p;
253   Segment* sg;
254
255   if (s_trace_semaphore)
256      DRD_(trace_msg)("[%d] sem_open      0x%lx name %s"
257                      " oflag %#lx mode %#lo value %u\n",
258                      DRD_(thread_get_running_tid)(),
259                      semaphore, name, oflag, mode, value);
260
261   /* Return if the sem_open() call failed. */
262   if (! semaphore)
263      return NULL;
264
265   p = semaphore_get(semaphore);
266   if (p)
267   {
268      const ThreadId vg_tid = VG_(get_running_tid)();
269      SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore };
270      VG_(maybe_record_error)(vg_tid,
271                              SemaphoreErr,
272                              VG_(get_IP)(vg_tid),
273                              "Semaphore reinitialization",
274                              &SEI);
275      // Remove all segments from the segment stack.
276      while ((sg = drd_segment_pop(p)))
277      {
278         DRD_(sg_put)(sg);
279      }
280   }
281   else
282   {
283      p = drd_semaphore_get_or_allocate(semaphore);
284   }
285   tl_assert(p);
286   p->waits_to_skip = value;
287   p->value         = value;
288   return p;
289}
290
291/** Called before sem_close(). */
292void DRD_(semaphore_close)(const Addr semaphore)
293{
294   struct semaphore_info* p;
295
296   p = semaphore_get(semaphore);
297
298   if (s_trace_semaphore)
299      DRD_(trace_msg)("[%d] sem_close     0x%lx value %u\n",
300                      DRD_(thread_get_running_tid)(), semaphore,
301                      p ? p->value : 0);
302
303   if (p == 0)
304   {
305      GenericErrInfo GEI = {
306	 .tid  = DRD_(thread_get_running_tid)(),
307	 .addr = semaphore,
308      };
309      VG_(maybe_record_error)(VG_(get_running_tid)(),
310                              GenericErr,
311                              VG_(get_IP)(VG_(get_running_tid)()),
312                              "Not a semaphore",
313                              &GEI);
314      return;
315   }
316
317   DRD_(clientobj_remove)(semaphore, ClientSemaphore);
318}
319
320/** Called before sem_wait(). */
321void DRD_(semaphore_pre_wait)(const Addr semaphore)
322{
323   struct semaphore_info* p;
324
325   tl_assert(semaphore < semaphore + 1);
326   p = drd_semaphore_get_or_allocate(semaphore);
327   tl_assert(p);
328   p->waiters++;
329
330   if ((Word)(p->waiters) <= 0)
331   {
332      SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore };
333      VG_(maybe_record_error)(VG_(get_running_tid)(),
334                              SemaphoreErr,
335                              VG_(get_IP)(VG_(get_running_tid)()),
336                              "Invalid semaphore",
337                              &sei);
338   }
339}
340
341/**
342 * Called after sem_wait() finished.
343 * @note Do not rely on the value of 'waited' -- some glibc versions do
344 *       not set it correctly.
345 */
346void DRD_(semaphore_post_wait)(const DrdThreadId tid, const Addr semaphore,
347                               const Bool waited)
348{
349   struct semaphore_info* p;
350   Segment* sg;
351
352   p = semaphore_get(semaphore);
353   if (s_trace_semaphore)
354      DRD_(trace_msg)("[%d] sem_wait      0x%lx value %u -> %u\n",
355                      DRD_(thread_get_running_tid)(), semaphore,
356                      p ? p->value : 0, p ? p->value - 1 : 0);
357
358   if (p)
359   {
360      p->waiters--;
361      p->value--;
362   }
363
364   /*
365    * Note: if another thread destroyed and reinitialized a semaphore while
366    * the current thread was waiting in sem_wait, p->waiters may have been
367    * set to zero by drd_semaphore_initialize() after
368    * DRD_(semaphore_pre_wait)() has finished before
369    * DRD_(semaphore_post_wait)() has been called.
370    */
371   if (p == NULL || (Int)(p->value) < 0 || (Word)(p->waiters) < 0)
372   {
373      SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore };
374      VG_(maybe_record_error)(VG_(get_running_tid)(),
375                              SemaphoreErr,
376                              VG_(get_IP)(VG_(get_running_tid)()),
377                              "Invalid semaphore",
378                              &sei);
379      return;
380   }
381
382   if (p->waits_to_skip > 0)
383      p->waits_to_skip--;
384   else
385   {
386      sg = drd_segment_pop(p);
387      tl_assert(sg);
388      if (p->last_sem_post_tid != tid
389          && p->last_sem_post_tid != DRD_INVALID_THREADID)
390      {
391         DRD_(thread_new_segment_and_combine_vc)(tid, sg);
392      }
393      else
394         DRD_(thread_new_segment)(tid);
395      s_semaphore_segment_creation_count++;
396      DRD_(sg_put)(sg);
397   }
398}
399
400/** Called before sem_post(). */
401void DRD_(semaphore_pre_post)(const DrdThreadId tid, const Addr semaphore)
402{
403   struct semaphore_info* p;
404   Segment* sg;
405
406   p = drd_semaphore_get_or_allocate(semaphore);
407   p->value++;
408
409   if (s_trace_semaphore)
410      DRD_(trace_msg)("[%d] sem_post      0x%lx value %u -> %u\n",
411                      DRD_(thread_get_running_tid)(),
412                      semaphore, p->value - 1, p->value);
413
414   p->last_sem_post_tid = tid;
415   sg = 0;
416   DRD_(thread_get_latest_segment)(&sg, tid);
417   tl_assert(sg);
418   drd_segment_push(p, sg);
419   DRD_(thread_new_segment)(tid);
420   s_semaphore_segment_creation_count++;
421}
422
423/** Called after sem_post() finished. */
424void DRD_(semaphore_post_post)(const DrdThreadId tid, const Addr semaphore,
425                               const Bool succeeded)
426{
427   /*
428    * Note: it is hard to implement the sem_post() wrapper correctly in
429    * case sem_post() returns an error code. This is because handling this
430    * case correctly requires restoring the vector clock associated with
431    * the semaphore to its original value here. In order to do that without
432    * introducing a race condition, extra locking has to be added around
433    * each semaphore call. Such extra locking would have to be added in
434    * drd_pthread_intercepts.c. However, it is hard to implement
435    * synchronization in drd_pthread_intercepts.c in a portable way without
436    * calling already redirected functions.
437    */
438}
439
440ULong DRD_(get_semaphore_segment_creation_count)(void)
441{
442   return s_semaphore_segment_creation_count;
443}
444