blob: 03dfbde3187ac2bb020ad5d7b335c8b453b17bf8 [file] [log] [blame]
sewardj85642922008-01-14 11:54:56 +00001/*
2 This file is part of drd, a data race detector.
3
4 Copyright (C) 2006-2008 Bart Van Assche
5 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"
sewardj85642922008-01-14 11:54:56 +000027#include "drd_error.h"
28#include "drd_semaphore.h"
29#include "drd_suppression.h"
sewardj85642922008-01-14 11:54:56 +000030#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)()
bart3e017fa2008-12-17 19:20:13 +000034#include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free)
sewardj85642922008-01-14 11:54:56 +000035#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
36
37
bartdc1ef032009-02-15 14:18:02 +000038/* Local functions. */
sewardj85642922008-01-14 11:54:56 +000039
bartdc1ef032009-02-15 14:18:02 +000040static void DRD_(semaphore_cleanup)(struct semaphore_info* p);
sewardj85642922008-01-14 11:54:56 +000041
42
bartdc1ef032009-02-15 14:18:02 +000043/* Local variables. */
sewardj85642922008-01-14 11:54:56 +000044
bartdc1ef032009-02-15 14:18:02 +000045static Bool DRD_(s_trace_semaphore);
46static ULong DRD_(s_semaphore_segment_creation_count);
sewardj85642922008-01-14 11:54:56 +000047
48
bartdc1ef032009-02-15 14:18:02 +000049/* Function definitions. */
sewardj85642922008-01-14 11:54:56 +000050
bartdc1ef032009-02-15 14:18:02 +000051/** 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)
bart3e017fa2008-12-17 19:20:13 +000053{
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_UserMsg, "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
bartdc1ef032009-02-15 14:18:02 +000065/** Pop a segment from the beginning of the queue 'p->last_sem_post_seg'. */
66static Segment* DRD_(segment_pop)(struct semaphore_info* p)
bart3e017fa2008-12-17 19:20:13 +000067{
68 Word sz;
69 Segment* sg;
70
71 sz = VG_(sizeXA)(p->last_sem_post_seg);
72#if 0
73 VG_(message)(Vg_UserMsg, "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
bartdc1ef032009-02-15 14:18:02 +000086/** Enable or disable tracing of semaphore actions. */
87void DRD_(semaphore_set_trace)(const Bool trace_semaphore)
sewardj85642922008-01-14 11:54:56 +000088{
bartdc1ef032009-02-15 14:18:02 +000089 DRD_(s_trace_semaphore) = trace_semaphore;
sewardj85642922008-01-14 11:54:56 +000090}
91
bartdc1ef032009-02-15 14:18:02 +000092/**
93 * Initialize the memory 'p' points at as a semaphore_info structure for the
94 * client semaphore at client addres 'semaphore'.
95 */
sewardj85642922008-01-14 11:54:56 +000096static
bartdc1ef032009-02-15 14:18:02 +000097void DRD_(semaphore_initialize)(struct semaphore_info* const p,
98 const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +000099{
100 tl_assert(semaphore != 0);
bart28230a32008-02-29 17:27:03 +0000101 tl_assert(p->a1 == semaphore);
bart28230a32008-02-29 17:27:03 +0000102 tl_assert(p->type == ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000103
bartdc1ef032009-02-15 14:18:02 +0000104 p->cleanup = (void(*)(DrdClientobj*))(DRD_(semaphore_cleanup));
bartc8914e92008-12-24 09:45:41 +0000105 p->waits_to_skip = 0;
bart94866cc2008-12-21 17:20:22 +0000106 p->value = 0;
107 p->waiters = 0;
sewardj85642922008-01-14 11:54:56 +0000108 p->last_sem_post_tid = DRD_INVALID_THREADID;
bart3e017fa2008-12-17 19:20:13 +0000109 p->last_sem_post_seg = VG_(newXA)(VG_(malloc), "drd.sg-stack",
110 VG_(free), sizeof(Segment*));
sewardj85642922008-01-14 11:54:56 +0000111}
112
bart195e41f2009-02-15 11:34:57 +0000113/**
114 * Free the memory that was allocated by semaphore_initialize(). Called by
115 * DRD_(clientobj_remove)().
bart28230a32008-02-29 17:27:03 +0000116 */
bartdc1ef032009-02-15 14:18:02 +0000117static void DRD_(semaphore_cleanup)(struct semaphore_info* p)
bart28230a32008-02-29 17:27:03 +0000118{
bart3e017fa2008-12-17 19:20:13 +0000119 Segment* sg;
120
bart28230a32008-02-29 17:27:03 +0000121 if (p->waiters > 0)
122 {
bart3b1ee452008-02-29 19:28:15 +0000123 SemaphoreErrInfo sei = { p->a1 };
124 VG_(maybe_record_error)(VG_(get_running_tid)(),
125 SemaphoreErr,
126 VG_(get_IP)(VG_(get_running_tid)()),
127 "Destruction of semaphore that is being waited"
128 " upon",
129 &sei);
bart28230a32008-02-29 17:27:03 +0000130 }
bartdc1ef032009-02-15 14:18:02 +0000131 while ((sg = DRD_(segment_pop)(p)))
bart62ada3f2009-02-14 17:19:58 +0000132 DRD_(sg_put)(sg);
bart3e017fa2008-12-17 19:20:13 +0000133 VG_(deleteXA)(p->last_sem_post_seg);
bart28230a32008-02-29 17:27:03 +0000134}
135
bartdc1ef032009-02-15 14:18:02 +0000136/**
137 * Return a pointer to the structure with information about the specified
138 * client semaphore. Allocate a new structure if such a structure did not
139 * yet exist.
140 */
sewardj85642922008-01-14 11:54:56 +0000141static
142struct semaphore_info*
bartdc1ef032009-02-15 14:18:02 +0000143DRD_(semaphore_get_or_allocate)(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000144{
bart28230a32008-02-29 17:27:03 +0000145 struct semaphore_info *p;
sewardj85642922008-01-14 11:54:56 +0000146
bart28230a32008-02-29 17:27:03 +0000147 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart195e41f2009-02-15 11:34:57 +0000148 p = &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore);
bart28230a32008-02-29 17:27:03 +0000149 if (p == 0)
sewardj85642922008-01-14 11:54:56 +0000150 {
bart28230a32008-02-29 17:27:03 +0000151 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart195e41f2009-02-15 11:34:57 +0000152 p = &(DRD_(clientobj_add)(semaphore, ClientSemaphore)->semaphore);
bartdc1ef032009-02-15 14:18:02 +0000153 DRD_(semaphore_initialize)(p, semaphore);
sewardj85642922008-01-14 11:54:56 +0000154 }
bart28230a32008-02-29 17:27:03 +0000155 return p;
sewardj85642922008-01-14 11:54:56 +0000156}
157
bartdc1ef032009-02-15 14:18:02 +0000158/**
159 * Return a pointer to the structure with information about the specified
160 * client semaphore, or null if no such structure was found.
161 */
162static struct semaphore_info* DRD_(semaphore_get)(const Addr semaphore)
bart28230a32008-02-29 17:27:03 +0000163{
164 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart195e41f2009-02-15 11:34:57 +0000165 return &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore);
bart28230a32008-02-29 17:27:03 +0000166}
167
168/** Called before sem_init(). */
bartdc1ef032009-02-15 14:18:02 +0000169struct semaphore_info* DRD_(semaphore_init)(const Addr semaphore,
170 const Word pshared,
171 const UInt value)
sewardj85642922008-01-14 11:54:56 +0000172{
173 struct semaphore_info* p;
bart94866cc2008-12-21 17:20:22 +0000174 Segment* sg;
sewardj85642922008-01-14 11:54:56 +0000175
bartdc1ef032009-02-15 14:18:02 +0000176 if (DRD_(s_trace_semaphore))
bart3b1ee452008-02-29 19:28:15 +0000177 {
178 VG_(message)(Vg_UserMsg,
bartafb42b72008-12-17 07:32:09 +0000179 "[%d/%d] semaphore_init 0x%lx value %u",
bart3b1ee452008-02-29 19:28:15 +0000180 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000181 DRD_(thread_get_running_tid)(),
bartda9436b2008-12-14 08:56:49 +0000182 semaphore,
183 value);
bart3b1ee452008-02-29 19:28:15 +0000184 }
bartdc1ef032009-02-15 14:18:02 +0000185 p = DRD_(semaphore_get)(semaphore);
bartd9e39ec2008-06-28 15:03:26 +0000186 if (p)
bart0268dfa2008-03-11 20:10:21 +0000187 {
bartd9e39ec2008-06-28 15:03:26 +0000188 const ThreadId vg_tid = VG_(get_running_tid)();
189 SemaphoreErrInfo SEI = { semaphore };
190 VG_(maybe_record_error)(vg_tid,
191 SemaphoreErr,
192 VG_(get_IP)(vg_tid),
193 "Semaphore reinitialization",
194 &SEI);
bart94866cc2008-12-21 17:20:22 +0000195 // Remove all segments from the segment stack.
bartdc1ef032009-02-15 14:18:02 +0000196 while ((sg = DRD_(segment_pop)(p)))
bart94866cc2008-12-21 17:20:22 +0000197 {
bart62ada3f2009-02-14 17:19:58 +0000198 DRD_(sg_put)(sg);
bart94866cc2008-12-21 17:20:22 +0000199 }
bart0268dfa2008-03-11 20:10:21 +0000200 }
bartd9e39ec2008-06-28 15:03:26 +0000201 else
202 {
bartdc1ef032009-02-15 14:18:02 +0000203 p = DRD_(semaphore_get_or_allocate)(semaphore);
bartd9e39ec2008-06-28 15:03:26 +0000204 }
205 tl_assert(p);
bartc8914e92008-12-24 09:45:41 +0000206 p->waits_to_skip = value;
bart94866cc2008-12-21 17:20:22 +0000207 p->value = value;
sewardj85642922008-01-14 11:54:56 +0000208 return p;
209}
210
bart28230a32008-02-29 17:27:03 +0000211/** Called after sem_destroy(). */
bartdc1ef032009-02-15 14:18:02 +0000212void DRD_(semaphore_destroy)(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000213{
bart72b751c2008-03-01 13:44:24 +0000214 struct semaphore_info* p;
bart3b1ee452008-02-29 19:28:15 +0000215
bartdc1ef032009-02-15 14:18:02 +0000216 p = DRD_(semaphore_get)(semaphore);
bartda9436b2008-12-14 08:56:49 +0000217
bartdc1ef032009-02-15 14:18:02 +0000218 if (DRD_(s_trace_semaphore))
bart3b1ee452008-02-29 19:28:15 +0000219 {
220 VG_(message)(Vg_UserMsg,
bartafb42b72008-12-17 07:32:09 +0000221 "[%d/%d] semaphore_destroy 0x%lx value %u",
bart3b1ee452008-02-29 19:28:15 +0000222 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000223 DRD_(thread_get_running_tid)(),
bartda9436b2008-12-14 08:56:49 +0000224 semaphore,
225 p ? p->value : 0);
bart3b1ee452008-02-29 19:28:15 +0000226 }
227
bart72b751c2008-03-01 13:44:24 +0000228 if (p == 0)
229 {
230 GenericErrInfo GEI;
231 VG_(maybe_record_error)(VG_(get_running_tid)(),
232 GenericErr,
233 VG_(get_IP)(VG_(get_running_tid)()),
234 "Not a semaphore",
235 &GEI);
236 return;
237 }
238
bart195e41f2009-02-15 11:34:57 +0000239 DRD_(clientobj_remove)(semaphore, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000240}
241
bart28230a32008-02-29 17:27:03 +0000242/** Called before sem_wait(). */
bartdc1ef032009-02-15 14:18:02 +0000243void DRD_(semaphore_pre_wait)(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000244{
245 struct semaphore_info* p;
246
bartdc1ef032009-02-15 14:18:02 +0000247 p = DRD_(semaphore_get_or_allocate)(semaphore);
bart28230a32008-02-29 17:27:03 +0000248 tl_assert(p);
bart74a5f212008-05-11 06:43:07 +0000249 tl_assert((int)p->waiters >= 0);
bart28230a32008-02-29 17:27:03 +0000250 p->waiters++;
251 tl_assert(p->waiters > 0);
252}
253
bartdc1ef032009-02-15 14:18:02 +0000254/**
255 * Called after sem_wait() finished.
256 * @note Do not rely on the value of 'waited' -- some glibc versions do
257 * not set it correctly.
bart28230a32008-02-29 17:27:03 +0000258 */
bartdc1ef032009-02-15 14:18:02 +0000259void DRD_(semaphore_post_wait)(const DrdThreadId tid, const Addr semaphore,
260 const Bool waited)
bart28230a32008-02-29 17:27:03 +0000261{
262 struct semaphore_info* p;
bart3e017fa2008-12-17 19:20:13 +0000263 Segment* sg;
bart28230a32008-02-29 17:27:03 +0000264
bartdc1ef032009-02-15 14:18:02 +0000265 p = DRD_(semaphore_get)(semaphore);
266 if (DRD_(s_trace_semaphore))
bart28230a32008-02-29 17:27:03 +0000267 {
bart3b1ee452008-02-29 19:28:15 +0000268 VG_(message)(Vg_UserMsg,
bart3e017fa2008-12-17 19:20:13 +0000269 "[%d/%d] semaphore_wait 0x%lx value %u -> %u",
bart3b1ee452008-02-29 19:28:15 +0000270 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000271 DRD_(thread_get_running_tid)(),
bartda9436b2008-12-14 08:56:49 +0000272 semaphore,
bart3e017fa2008-12-17 19:20:13 +0000273 p ? p->value : 0,
bartda9436b2008-12-14 08:56:49 +0000274 p ? p->value - 1 : 0);
bart28230a32008-02-29 17:27:03 +0000275 }
bart3e017fa2008-12-17 19:20:13 +0000276 tl_assert(p);
bart28230a32008-02-29 17:27:03 +0000277 tl_assert(p->waiters > 0);
278 p->waiters--;
bart74a5f212008-05-11 06:43:07 +0000279 tl_assert((int)p->waiters >= 0);
280 tl_assert((int)p->value >= 0);
bart28230a32008-02-29 17:27:03 +0000281 if (p->value == 0)
282 {
bart3b1ee452008-02-29 19:28:15 +0000283 SemaphoreErrInfo sei = { semaphore };
284 VG_(maybe_record_error)(VG_(get_running_tid)(),
285 SemaphoreErr,
286 VG_(get_IP)(VG_(get_running_tid)()),
287 "Invalid semaphore",
288 &sei);
bart28230a32008-02-29 17:27:03 +0000289 return;
290 }
sewardj85642922008-01-14 11:54:56 +0000291 p->value--;
bart74a5f212008-05-11 06:43:07 +0000292 tl_assert((int)p->value >= 0);
bartc8914e92008-12-24 09:45:41 +0000293 if (p->waits_to_skip > 0)
294 p->waits_to_skip--;
bart94866cc2008-12-21 17:20:22 +0000295 else
barta2b6e1b2008-03-17 18:32:39 +0000296 {
bartdc1ef032009-02-15 14:18:02 +0000297 sg = DRD_(segment_pop)(p);
bart94866cc2008-12-21 17:20:22 +0000298 tl_assert(sg);
299 if (sg)
bart3e017fa2008-12-17 19:20:13 +0000300 {
bart94866cc2008-12-21 17:20:22 +0000301 if (p->last_sem_post_tid != tid
302 && p->last_sem_post_tid != DRD_INVALID_THREADID)
303 {
bart62a784c2009-02-15 13:11:14 +0000304 DRD_(thread_combine_vc2)(tid, &sg->vc);
bart94866cc2008-12-21 17:20:22 +0000305 }
bart62ada3f2009-02-14 17:19:58 +0000306 DRD_(sg_put)(sg);
bart62a784c2009-02-15 13:11:14 +0000307 DRD_(thread_new_segment)(tid);
bartdc1ef032009-02-15 14:18:02 +0000308 DRD_(s_semaphore_segment_creation_count)++;
bart3e017fa2008-12-17 19:20:13 +0000309 }
barta2b6e1b2008-03-17 18:32:39 +0000310 }
sewardj85642922008-01-14 11:54:56 +0000311}
312
313/** Called before sem_post(). */
bartdc1ef032009-02-15 14:18:02 +0000314void DRD_(semaphore_pre_post)(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000315{
316 struct semaphore_info* p;
bart3e017fa2008-12-17 19:20:13 +0000317 Segment* sg;
sewardj85642922008-01-14 11:54:56 +0000318
bartdc1ef032009-02-15 14:18:02 +0000319 p = DRD_(semaphore_get_or_allocate)(semaphore);
bartda9436b2008-12-14 08:56:49 +0000320 p->value++;
321
bartdc1ef032009-02-15 14:18:02 +0000322 if (DRD_(s_trace_semaphore))
bart3b1ee452008-02-29 19:28:15 +0000323 {
324 VG_(message)(Vg_UserMsg,
bart3e017fa2008-12-17 19:20:13 +0000325 "[%d/%d] semaphore_post 0x%lx value %u -> %u",
bart3b1ee452008-02-29 19:28:15 +0000326 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000327 DRD_(thread_get_running_tid)(),
bartda9436b2008-12-14 08:56:49 +0000328 semaphore,
bart3e017fa2008-12-17 19:20:13 +0000329 p->value - 1, p->value);
bart3b1ee452008-02-29 19:28:15 +0000330 }
bartda9436b2008-12-14 08:56:49 +0000331
bart3e017fa2008-12-17 19:20:13 +0000332 p->last_sem_post_tid = tid;
bart62a784c2009-02-15 13:11:14 +0000333 DRD_(thread_new_segment)(tid);
bart3e017fa2008-12-17 19:20:13 +0000334 sg = 0;
bart62a784c2009-02-15 13:11:14 +0000335 DRD_(thread_get_latest_segment)(&sg, tid);
bart3e017fa2008-12-17 19:20:13 +0000336 tl_assert(sg);
bartdc1ef032009-02-15 14:18:02 +0000337 DRD_(segment_push)(p, sg);
338 DRD_(s_semaphore_segment_creation_count)++;
sewardj85642922008-01-14 11:54:56 +0000339}
340
341/** Called after sem_post() finished successfully. */
bartdc1ef032009-02-15 14:18:02 +0000342void DRD_(semaphore_post_post)(const DrdThreadId tid, const Addr semaphore,
343 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000344{
bart25896d92008-02-17 09:21:05 +0000345 /* Note: it is hard to implement the sem_post() wrapper correctly in */
346 /* case sem_post() returns an error code. This is because handling this */
347 /* case correctly requires restoring the vector clock associated with */
348 /* the semaphore to its original value here. In order to do that without */
349 /* introducing a race condition, extra locking has to be added around */
350 /* each semaphore call. Such extra locking would have to be added in */
351 /* drd_intercepts.c. However, it is hard to implement synchronization */
352 /* in drd_intercepts.c in a portable way without calling already */
353 /* redirected functions. */
sewardj85642922008-01-14 11:54:56 +0000354}
355
bartdc1ef032009-02-15 14:18:02 +0000356void DRD_(semaphore_thread_delete)(const DrdThreadId threadid)
sewardj85642922008-01-14 11:54:56 +0000357{ }
bart6bbefaf2008-04-19 15:16:45 +0000358
bartdc1ef032009-02-15 14:18:02 +0000359ULong DRD_(get_semaphore_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000360{
bartdc1ef032009-02-15 14:18:02 +0000361 return DRD_(s_semaphore_segment_creation_count);
bart6bbefaf2008-04-19 15:16:45 +0000362}