blob: 6dd600858447a425d8d2073191fd4bd3d124c378 [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
bart28230a32008-02-29 17:27:03 +000038// Local functions.
sewardj85642922008-01-14 11:54:56 +000039
bart28230a32008-02-29 17:27:03 +000040static void semaphore_cleanup(struct semaphore_info* p);
sewardj85642922008-01-14 11:54:56 +000041
42
43// Local variables.
44
45static Bool s_trace_semaphore;
bart6bbefaf2008-04-19 15:16:45 +000046static ULong s_semaphore_segment_creation_count;
sewardj85642922008-01-14 11:54:56 +000047
48
49// Function definitions.
50
bart3e017fa2008-12-17 19:20:13 +000051static void segment_push(struct semaphore_info* p, Segment* sg)
52{
53 Word n;
54
55 tl_assert(sg);
56 n = VG_(addToXA)(p->last_sem_post_seg, &sg);
57#if 0
58 VG_(message)(Vg_UserMsg, "0x%lx push: added at position %ld/%ld",
59 p->a1, n, VG_(sizeXA)(p->last_sem_post_seg));
60#endif
61 tl_assert(*(Segment**)VG_(indexXA)(p->last_sem_post_seg, n) == sg);
62}
63
64static Segment* segment_pop(struct semaphore_info* p)
65{
66 Word sz;
67 Segment* sg;
68
69 sz = VG_(sizeXA)(p->last_sem_post_seg);
70#if 0
71 VG_(message)(Vg_UserMsg, "0x%lx pop: removed from position %ld/%ld",
72 p->a1, sz - 1, sz);
73#endif
74 sg = 0;
75 if (sz > 0)
76 {
77 sg = *(Segment**)VG_(indexXA)(p->last_sem_post_seg, sz - 1);
78 tl_assert(sg);
79 VG_(dropTailXA)(p->last_sem_post_seg, 1);
80 }
81 return sg;
82}
83
sewardj85642922008-01-14 11:54:56 +000084void semaphore_set_trace(const Bool trace_semaphore)
85{
86 s_trace_semaphore = trace_semaphore;
87}
88
89static
bart94866cc2008-12-21 17:20:22 +000090void semaphore_initialize(struct semaphore_info* const p, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +000091{
92 tl_assert(semaphore != 0);
bart28230a32008-02-29 17:27:03 +000093 tl_assert(p->a1 == semaphore);
bart28230a32008-02-29 17:27:03 +000094 tl_assert(p->type == ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +000095
bart94866cc2008-12-21 17:20:22 +000096 p->cleanup = (void(*)(DrdClientobj*))semaphore_cleanup;
97 p->initial_value = 0;
98 p->value = 0;
99 p->waiters = 0;
sewardj85642922008-01-14 11:54:56 +0000100 p->last_sem_post_tid = DRD_INVALID_THREADID;
bart3e017fa2008-12-17 19:20:13 +0000101 p->last_sem_post_seg = VG_(newXA)(VG_(malloc), "drd.sg-stack",
102 VG_(free), sizeof(Segment*));
sewardj85642922008-01-14 11:54:56 +0000103}
104
bart28230a32008-02-29 17:27:03 +0000105/** Free the memory that was allocated by semaphore_initialize(). Called by
bart72b751c2008-03-01 13:44:24 +0000106 * clientobj_remove().
bart28230a32008-02-29 17:27:03 +0000107 */
108static void semaphore_cleanup(struct semaphore_info* p)
109{
bart3e017fa2008-12-17 19:20:13 +0000110 Segment* sg;
111
bart28230a32008-02-29 17:27:03 +0000112 if (p->waiters > 0)
113 {
bart3b1ee452008-02-29 19:28:15 +0000114 SemaphoreErrInfo sei = { p->a1 };
115 VG_(maybe_record_error)(VG_(get_running_tid)(),
116 SemaphoreErr,
117 VG_(get_IP)(VG_(get_running_tid)()),
118 "Destruction of semaphore that is being waited"
119 " upon",
120 &sei);
bart28230a32008-02-29 17:27:03 +0000121 }
bart3e017fa2008-12-17 19:20:13 +0000122 while ((sg = segment_pop(p)))
123 sg_put(sg);
124 VG_(deleteXA)(p->last_sem_post_seg);
bart28230a32008-02-29 17:27:03 +0000125}
126
sewardj85642922008-01-14 11:54:56 +0000127static
128struct semaphore_info*
bart0268dfa2008-03-11 20:10:21 +0000129semaphore_get_or_allocate(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000130{
bart28230a32008-02-29 17:27:03 +0000131 struct semaphore_info *p;
sewardj85642922008-01-14 11:54:56 +0000132
bart28230a32008-02-29 17:27:03 +0000133 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart72b751c2008-03-01 13:44:24 +0000134 p = &clientobj_get(semaphore, ClientSemaphore)->semaphore;
bart28230a32008-02-29 17:27:03 +0000135 if (p == 0)
sewardj85642922008-01-14 11:54:56 +0000136 {
bart28230a32008-02-29 17:27:03 +0000137 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart0268dfa2008-03-11 20:10:21 +0000138 p = &clientobj_add(semaphore, ClientSemaphore)->semaphore;
bart94866cc2008-12-21 17:20:22 +0000139 semaphore_initialize(p, semaphore);
sewardj85642922008-01-14 11:54:56 +0000140 }
bart28230a32008-02-29 17:27:03 +0000141 return p;
sewardj85642922008-01-14 11:54:56 +0000142}
143
bart72b751c2008-03-01 13:44:24 +0000144static struct semaphore_info* semaphore_get(const Addr semaphore)
bart28230a32008-02-29 17:27:03 +0000145{
146 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart72b751c2008-03-01 13:44:24 +0000147 return &clientobj_get(semaphore, ClientSemaphore)->semaphore;
bart28230a32008-02-29 17:27:03 +0000148}
149
150/** Called before sem_init(). */
bart0268dfa2008-03-11 20:10:21 +0000151struct semaphore_info* semaphore_init(const Addr semaphore,
bartafb42b72008-12-17 07:32:09 +0000152 const Word pshared, const UInt value)
sewardj85642922008-01-14 11:54:56 +0000153{
sewardj59347ff2008-12-23 02:31:22 +0000154 /* unsigned n; */
sewardj85642922008-01-14 11:54:56 +0000155 struct semaphore_info* p;
bart94866cc2008-12-21 17:20:22 +0000156 Segment* sg;
sewardj59347ff2008-12-23 02:31:22 +0000157 /* const DrdThreadId drd_tid = thread_get_running_tid(); */
sewardj85642922008-01-14 11:54:56 +0000158
bart3b1ee452008-02-29 19:28:15 +0000159 if (s_trace_semaphore)
160 {
161 VG_(message)(Vg_UserMsg,
bartafb42b72008-12-17 07:32:09 +0000162 "[%d/%d] semaphore_init 0x%lx value %u",
bart3b1ee452008-02-29 19:28:15 +0000163 VG_(get_running_tid)(),
164 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000165 semaphore,
166 value);
bart3b1ee452008-02-29 19:28:15 +0000167 }
bartd9e39ec2008-06-28 15:03:26 +0000168 p = semaphore_get(semaphore);
169 if (p)
bart0268dfa2008-03-11 20:10:21 +0000170 {
bartd9e39ec2008-06-28 15:03:26 +0000171 const ThreadId vg_tid = VG_(get_running_tid)();
172 SemaphoreErrInfo SEI = { semaphore };
173 VG_(maybe_record_error)(vg_tid,
174 SemaphoreErr,
175 VG_(get_IP)(vg_tid),
176 "Semaphore reinitialization",
177 &SEI);
bart94866cc2008-12-21 17:20:22 +0000178 // Remove all segments from the segment stack.
179 while ((sg = segment_pop(p)))
180 {
181 sg_put(sg);
182 }
bart0268dfa2008-03-11 20:10:21 +0000183 }
bartd9e39ec2008-06-28 15:03:26 +0000184 else
185 {
186 p = semaphore_get_or_allocate(semaphore);
187 }
188 tl_assert(p);
bart94866cc2008-12-21 17:20:22 +0000189 p->initial_value = value;
190 p->value = value;
sewardj85642922008-01-14 11:54:56 +0000191 return p;
192}
193
bart28230a32008-02-29 17:27:03 +0000194/** Called after sem_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000195void semaphore_destroy(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000196{
bart72b751c2008-03-01 13:44:24 +0000197 struct semaphore_info* p;
bart3b1ee452008-02-29 19:28:15 +0000198
bartda9436b2008-12-14 08:56:49 +0000199 p = semaphore_get(semaphore);
200
bart3b1ee452008-02-29 19:28:15 +0000201 if (s_trace_semaphore)
202 {
203 VG_(message)(Vg_UserMsg,
bartafb42b72008-12-17 07:32:09 +0000204 "[%d/%d] semaphore_destroy 0x%lx value %u",
bart3b1ee452008-02-29 19:28:15 +0000205 VG_(get_running_tid)(),
206 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000207 semaphore,
208 p ? p->value : 0);
bart3b1ee452008-02-29 19:28:15 +0000209 }
210
bart72b751c2008-03-01 13:44:24 +0000211 if (p == 0)
212 {
213 GenericErrInfo GEI;
214 VG_(maybe_record_error)(VG_(get_running_tid)(),
215 GenericErr,
216 VG_(get_IP)(VG_(get_running_tid)()),
217 "Not a semaphore",
218 &GEI);
219 return;
220 }
221
222 clientobj_remove(semaphore, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000223}
224
bart28230a32008-02-29 17:27:03 +0000225/** Called before sem_wait(). */
bart0268dfa2008-03-11 20:10:21 +0000226void semaphore_pre_wait(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000227{
228 struct semaphore_info* p;
229
bart0268dfa2008-03-11 20:10:21 +0000230 p = semaphore_get_or_allocate(semaphore);
bart28230a32008-02-29 17:27:03 +0000231 tl_assert(p);
bart74a5f212008-05-11 06:43:07 +0000232 tl_assert((int)p->waiters >= 0);
bart28230a32008-02-29 17:27:03 +0000233 p->waiters++;
234 tl_assert(p->waiters > 0);
235}
236
237/** Called after sem_wait() finished.
238 * @note Do not rely on the value of 'waited' -- some glibc versions do
239 * not set it correctly.
240 */
241void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
242 const Bool waited)
243{
244 struct semaphore_info* p;
bart3e017fa2008-12-17 19:20:13 +0000245 Segment* sg;
bart28230a32008-02-29 17:27:03 +0000246
247 p = semaphore_get(semaphore);
248 if (s_trace_semaphore)
249 {
bart3b1ee452008-02-29 19:28:15 +0000250 VG_(message)(Vg_UserMsg,
bart3e017fa2008-12-17 19:20:13 +0000251 "[%d/%d] semaphore_wait 0x%lx value %u -> %u",
bart3b1ee452008-02-29 19:28:15 +0000252 VG_(get_running_tid)(),
253 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000254 semaphore,
bart3e017fa2008-12-17 19:20:13 +0000255 p ? p->value : 0,
bartda9436b2008-12-14 08:56:49 +0000256 p ? p->value - 1 : 0);
bart28230a32008-02-29 17:27:03 +0000257 }
bart3e017fa2008-12-17 19:20:13 +0000258 tl_assert(p);
bart28230a32008-02-29 17:27:03 +0000259 tl_assert(p->waiters > 0);
260 p->waiters--;
bart74a5f212008-05-11 06:43:07 +0000261 tl_assert((int)p->waiters >= 0);
262 tl_assert((int)p->value >= 0);
bart28230a32008-02-29 17:27:03 +0000263 if (p->value == 0)
264 {
bart3b1ee452008-02-29 19:28:15 +0000265 SemaphoreErrInfo sei = { semaphore };
266 VG_(maybe_record_error)(VG_(get_running_tid)(),
267 SemaphoreErr,
268 VG_(get_IP)(VG_(get_running_tid)()),
269 "Invalid semaphore",
270 &sei);
bart28230a32008-02-29 17:27:03 +0000271 return;
272 }
sewardj85642922008-01-14 11:54:56 +0000273 p->value--;
bart74a5f212008-05-11 06:43:07 +0000274 tl_assert((int)p->value >= 0);
bart94866cc2008-12-21 17:20:22 +0000275 if (p->initial_value > 0)
276 p->initial_value--;
277 else
barta2b6e1b2008-03-17 18:32:39 +0000278 {
bart94866cc2008-12-21 17:20:22 +0000279 sg = segment_pop(p);
280 tl_assert(sg);
281 if (sg)
bart3e017fa2008-12-17 19:20:13 +0000282 {
bart94866cc2008-12-21 17:20:22 +0000283 if (p->last_sem_post_tid != tid
284 && p->last_sem_post_tid != DRD_INVALID_THREADID)
285 {
286 thread_combine_vc2(tid, &sg->vc);
287 }
288 sg_put(sg);
289 thread_new_segment(tid);
290 s_semaphore_segment_creation_count++;
bart3e017fa2008-12-17 19:20:13 +0000291 }
barta2b6e1b2008-03-17 18:32:39 +0000292 }
sewardj85642922008-01-14 11:54:56 +0000293}
294
295/** Called before sem_post(). */
bart0268dfa2008-03-11 20:10:21 +0000296void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000297{
298 struct semaphore_info* p;
bart3e017fa2008-12-17 19:20:13 +0000299 Segment* sg;
sewardj85642922008-01-14 11:54:56 +0000300
bartda9436b2008-12-14 08:56:49 +0000301 p = semaphore_get_or_allocate(semaphore);
302 p->value++;
303
bart3b1ee452008-02-29 19:28:15 +0000304 if (s_trace_semaphore)
305 {
306 VG_(message)(Vg_UserMsg,
bart3e017fa2008-12-17 19:20:13 +0000307 "[%d/%d] semaphore_post 0x%lx value %u -> %u",
bart3b1ee452008-02-29 19:28:15 +0000308 VG_(get_running_tid)(),
309 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000310 semaphore,
bart3e017fa2008-12-17 19:20:13 +0000311 p->value - 1, p->value);
bart3b1ee452008-02-29 19:28:15 +0000312 }
bartda9436b2008-12-14 08:56:49 +0000313
bart3e017fa2008-12-17 19:20:13 +0000314 p->last_sem_post_tid = tid;
315 thread_new_segment(tid);
316 sg = 0;
317 thread_get_latest_segment(&sg, tid);
318 tl_assert(sg);
319 segment_push(p, sg);
320 s_semaphore_segment_creation_count++;
sewardj85642922008-01-14 11:54:56 +0000321}
322
323/** Called after sem_post() finished successfully. */
324void semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000325 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000326{
bart25896d92008-02-17 09:21:05 +0000327 /* Note: it is hard to implement the sem_post() wrapper correctly in */
328 /* case sem_post() returns an error code. This is because handling this */
329 /* case correctly requires restoring the vector clock associated with */
330 /* the semaphore to its original value here. In order to do that without */
331 /* introducing a race condition, extra locking has to be added around */
332 /* each semaphore call. Such extra locking would have to be added in */
333 /* drd_intercepts.c. However, it is hard to implement synchronization */
334 /* in drd_intercepts.c in a portable way without calling already */
335 /* redirected functions. */
sewardj85642922008-01-14 11:54:56 +0000336}
337
338void semaphore_thread_delete(const DrdThreadId threadid)
339{ }
bart6bbefaf2008-04-19 15:16:45 +0000340
341ULong get_semaphore_segment_creation_count(void)
342{
343 return s_semaphore_segment_creation_count;
344}