]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/drd/drd_cond.c
update
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / drd / drd_cond.c
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_cond.h"
28 #include "drd_error.h"
29 #include "drd_mutex.h"
30 #include "pub_tool_errormgr.h"    /* VG_(maybe_record_error)() */
31 #include "pub_tool_libcassert.h"  /* tl_assert()               */
32 #include "pub_tool_libcbase.h"    /* VG_(memcmp)()             */
33 #include "pub_tool_libcprint.h"   /* VG_(printf)()             */
34 #include "pub_tool_machine.h"     /* VG_(get_IP)()             */
35 #include "pub_tool_threadstate.h" /* VG_(get_running_tid)()    */
36
37
38 /* Local functions. */
39
40 static void DRD_(cond_cleanup)(struct cond_info* p);
41
42
43 /* Local variables. */
44
45 static Bool DRD_(s_report_signal_unlocked) = True;
46 static Bool DRD_(s_trace_cond);
47
48
49 /* Function definitions. */
50
51 void DRD_(cond_set_report_signal_unlocked)(const Bool r)
52 {
53    DRD_(s_report_signal_unlocked) = r;
54 }
55
56 void DRD_(cond_set_trace)(const Bool trace_cond)
57 {
58    DRD_(s_trace_cond) = trace_cond;
59 }
60
61 static
62 void DRD_(cond_initialize)(struct cond_info* const p, const Addr cond)
63 {
64    tl_assert(cond != 0);
65    tl_assert(p->a1   == cond);
66    tl_assert(p->type == ClientCondvar);
67
68    p->cleanup       = (void(*)(DrdClientobj*))(DRD_(cond_cleanup));
69    p->delete_thread = 0;
70    p->waiter_count  = 0;
71    p->mutex         = 0;
72 }
73
74 /**
75  * Free the memory that was allocated by cond_initialize(). Called by
76  * DRD_(clientobj_remove)().
77  */
78 static void DRD_(cond_cleanup)(struct cond_info* p)
79 {
80    tl_assert(p);
81    if (p->mutex)
82    {
83       struct mutex_info* q;
84       q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex);
85       {
86          CondDestrErrInfo cde = {
87             DRD_(thread_get_running_tid)(),
88             p->a1,
89             q ? q->a1 : 0,
90             q ? q->owner : DRD_INVALID_THREADID
91          };
92          VG_(maybe_record_error)(VG_(get_running_tid)(),
93                                  CondDestrErr,
94                                  VG_(get_IP)(VG_(get_running_tid)()),
95                                  "Destroying condition variable that is being"
96                                  " waited upon",
97                                  &cde);
98       }
99    }
100 }
101
102 /**
103  * Report that the synchronization object at address 'addr' is of the
104  * wrong type.
105  */
106 static void wrong_type(const Addr addr)
107 {
108    GenericErrInfo gei = {
109       .tid  = DRD_(thread_get_running_tid)(),
110       .addr = addr,
111    };
112    VG_(maybe_record_error)(VG_(get_running_tid)(),
113                            GenericErr,
114                            VG_(get_IP)(VG_(get_running_tid)()),
115                            "wrong type of synchronization object",
116                            &gei);
117 }
118
119 static struct cond_info* cond_get_or_allocate(const Addr cond)
120 {
121    struct cond_info *p;
122
123    tl_assert(offsetof(DrdClientobj, cond) == 0);
124    p = &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
125    if (p)
126       return p;
127
128    if (DRD_(clientobj_present)(cond, cond + 1))
129    {
130       wrong_type(cond);
131       return 0;
132    }
133
134    p = &(DRD_(clientobj_add)(cond, ClientCondvar)->cond);
135    DRD_(cond_initialize)(p, cond);
136    return p;
137 }
138
139 struct cond_info* DRD_(cond_get)(const Addr cond)
140 {
141    tl_assert(offsetof(DrdClientobj, cond) == 0);
142    return &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
143 }
144
145 /** Called before pthread_cond_init(). */
146 void DRD_(cond_pre_init)(const Addr cond)
147 {
148    struct cond_info* p;
149
150    if (DRD_(s_trace_cond))
151    {
152       VG_(message)(Vg_UserMsg,
153                    "[%d] cond_init       cond 0x%lx\n",
154                    DRD_(thread_get_running_tid)(),
155                    cond);
156    }
157
158    p = DRD_(cond_get)(cond);
159
160    if (p)
161    {
162       CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
163       VG_(maybe_record_error)(VG_(get_running_tid)(),
164                               CondErr,
165                               VG_(get_IP)(VG_(get_running_tid)()),
166                               "initialized twice",
167                               &cei);
168    }
169
170    p = cond_get_or_allocate(cond);
171 }
172
173 /** Called after pthread_cond_destroy(). */
174 void DRD_(cond_post_destroy)(const Addr cond)
175 {
176    struct cond_info* p;
177
178    if (DRD_(s_trace_cond))
179    {
180       VG_(message)(Vg_UserMsg,
181                    "[%d] cond_destroy    cond 0x%lx\n",
182                    DRD_(thread_get_running_tid)(),
183                    cond);
184    }
185
186    p = DRD_(cond_get)(cond);
187    if (p == 0)
188    {
189       CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
190       VG_(maybe_record_error)(VG_(get_running_tid)(),
191                               CondErr,
192                               VG_(get_IP)(VG_(get_running_tid)()),
193                               "not a condition variable",
194                               &cei);
195       return;
196    }
197
198    if (p->waiter_count != 0)
199    {
200       CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
201       VG_(maybe_record_error)(VG_(get_running_tid)(),
202                               CondErr,
203                               VG_(get_IP)(VG_(get_running_tid)()),
204                               "destruction of condition variable being waited"
205                               " upon",
206                               &cei);
207    }
208
209    DRD_(clientobj_remove)(p->a1, ClientCondvar);
210 }
211
212 /**
213  * Called before pthread_cond_wait(). Note: before this function is called,
214  * mutex_unlock() has already been called from drd_clientreq.c.
215  */
216 void DRD_(cond_pre_wait)(const Addr cond, const Addr mutex)
217 {
218    struct cond_info* p;
219    struct mutex_info* q;
220
221    if (DRD_(s_trace_cond))
222    {
223       VG_(message)(Vg_UserMsg,
224                    "[%d] cond_pre_wait   cond 0x%lx\n",
225                    DRD_(thread_get_running_tid)(),
226                    cond);
227    }
228
229    p = cond_get_or_allocate(cond);
230    if (!p)
231    {
232       CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
233       VG_(maybe_record_error)(VG_(get_running_tid)(),
234                               CondErr,
235                               VG_(get_IP)(VG_(get_running_tid)()),
236                               "not a condition variable",
237                               &cei);
238       return;
239    }
240
241    if (p->waiter_count == 0)
242    {
243       p->mutex = mutex;
244    }
245    else if (p->mutex != mutex)
246    {
247       CondWaitErrInfo cwei
248          = { .tid = DRD_(thread_get_running_tid)(),
249              .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex };
250       VG_(maybe_record_error)(VG_(get_running_tid)(),
251                               CondWaitErr,
252                               VG_(get_IP)(VG_(get_running_tid)()),
253                               "Inconsistent association of condition variable"
254                               " and mutex",
255                               &cwei);
256    }
257    tl_assert(p->mutex);
258    q = DRD_(mutex_get)(p->mutex);
259    if (q
260        && q->owner == DRD_(thread_get_running_tid)() && q->recursion_count > 0)
261    {
262       const ThreadId vg_tid = VG_(get_running_tid)();
263       MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
264                            q->a1, q->recursion_count, q->owner };
265       VG_(maybe_record_error)(vg_tid,
266                               MutexErr,
267                               VG_(get_IP)(vg_tid),
268                               "Mutex locked recursively",
269                               &MEI);
270    }
271    else if (q == 0)
272    {
273       DRD_(not_a_mutex)(p->mutex);
274    }
275
276    ++p->waiter_count;
277 }
278
279 /**
280  * Called after pthread_cond_wait().
281  */
282 void DRD_(cond_post_wait)(const Addr cond)
283 {
284    struct cond_info* p;
285
286    if (DRD_(s_trace_cond))
287    {
288       VG_(message)(Vg_UserMsg,
289                    "[%d] cond_post_wait  cond 0x%lx\n",
290                    DRD_(thread_get_running_tid)(),
291                    cond);
292    }
293
294    p = DRD_(cond_get)(cond);
295    if (!p)
296    {
297       CondDestrErrInfo cde = {
298          DRD_(thread_get_running_tid)(), cond, 0, DRD_INVALID_THREADID
299       };
300       VG_(maybe_record_error)(VG_(get_running_tid)(),
301                               CondDestrErr,
302                               VG_(get_IP)(VG_(get_running_tid)()),
303                               "condition variable has been destroyed while"
304                               " being waited upon",
305                               &cde);
306       return;
307    }
308
309    if (p->waiter_count > 0)
310    {
311       --p->waiter_count;
312       if (p->waiter_count == 0)
313       {
314          p->mutex = 0;
315       }
316    }
317 }
318
319 static void cond_signal(const DrdThreadId tid, struct cond_info* const cond_p)
320 {
321    const ThreadId vg_tid = VG_(get_running_tid)();
322    const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
323
324    tl_assert(cond_p);
325
326    if (cond_p->waiter_count > 0)
327    {
328       if (DRD_(s_report_signal_unlocked)
329           && ! DRD_(mutex_is_locked_by)(cond_p->mutex, drd_tid))
330       {
331          /*
332           * A signal is sent while the associated mutex has not been locked.
333           * This can indicate but is not necessarily a race condition.
334           */
335          CondRaceErrInfo cei = { .tid = DRD_(thread_get_running_tid)(),
336                                  .cond  = cond_p->a1,
337                                  .mutex = cond_p->mutex,
338          };
339          VG_(maybe_record_error)(vg_tid,
340                                  CondRaceErr,
341                                  VG_(get_IP)(vg_tid),
342                                  "CondErr",
343                                  &cei);
344       }
345    }
346    else
347    {
348       /*
349        * No other thread is waiting for the signal, hence the signal will
350        * be lost. This is normal in a POSIX threads application.
351        */
352    }
353 }
354
355 static void not_initialized(Addr const cond)
356 {
357    CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
358    VG_(maybe_record_error)(VG_(get_running_tid)(),
359                            CondErr,
360                            VG_(get_IP)(VG_(get_running_tid)()),
361                            "condition variable has not been initialized",
362                            &cei);
363 }
364
365 /** Called before pthread_cond_signal(). */
366 void DRD_(cond_pre_signal)(Addr const cond)
367 {
368    struct cond_info* p;
369
370    p = DRD_(cond_get)(cond);
371    if (DRD_(s_trace_cond))
372    {
373       VG_(message)(Vg_UserMsg,
374                    "[%d] cond_signal     cond 0x%lx\n",
375                    DRD_(thread_get_running_tid)(),
376                    cond);
377    }
378
379    tl_assert(DRD_(pthread_cond_initializer));
380    if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
381                          DRD_(pthread_cond_initializer_size)) != 0)
382    {
383       not_initialized(cond);
384       return;
385    }
386
387    if (!p)
388       p = cond_get_or_allocate(cond);
389
390    cond_signal(DRD_(thread_get_running_tid)(), p);
391 }
392
393 /** Called before pthread_cond_broadcast(). */
394 void DRD_(cond_pre_broadcast)(Addr const cond)
395 {
396    struct cond_info* p;
397
398    if (DRD_(s_trace_cond))
399    {
400       VG_(message)(Vg_UserMsg,
401                    "[%d] cond_broadcast  cond 0x%lx\n",
402                    DRD_(thread_get_running_tid)(),
403                    cond);
404    }
405
406    p = DRD_(cond_get)(cond);
407    tl_assert(DRD_(pthread_cond_initializer));
408    if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
409                          DRD_(pthread_cond_initializer_size)) != 0)
410    {
411       not_initialized(cond);
412       return;
413    }
414
415    if (!p)
416       p = cond_get_or_allocate(cond);
417
418    cond_signal(DRD_(thread_get_running_tid)(), p);
419 }