blob: 1637cfe47f33ac02a43161175aab0f8880577ef9 [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;
bartc8914e92008-12-24 09:45:41 +000097 p->waits_to_skip = 0;
bart94866cc2008-12-21 17:20:22 +000098 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{
154 struct semaphore_info* p;
bart94866cc2008-12-21 17:20:22 +0000155 Segment* sg;
sewardj85642922008-01-14 11:54:56 +0000156
bart3b1ee452008-02-29 19:28:15 +0000157 if (s_trace_semaphore)
158 {
159 VG_(message)(Vg_UserMsg,
bartafb42b72008-12-17 07:32:09 +0000160 "[%d/%d] semaphore_init 0x%lx value %u",
bart3b1ee452008-02-29 19:28:15 +0000161 VG_(get_running_tid)(),
162 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000163 semaphore,
164 value);
bart3b1ee452008-02-29 19:28:15 +0000165 }
bartd9e39ec2008-06-28 15:03:26 +0000166 p = semaphore_get(semaphore);
167 if (p)
bart0268dfa2008-03-11 20:10:21 +0000168 {
bartd9e39ec2008-06-28 15:03:26 +0000169 const ThreadId vg_tid = VG_(get_running_tid)();
170 SemaphoreErrInfo SEI = { semaphore };
171 VG_(maybe_record_error)(vg_tid,
172 SemaphoreErr,
173 VG_(get_IP)(vg_tid),
174 "Semaphore reinitialization",
175 &SEI);
bart94866cc2008-12-21 17:20:22 +0000176 // Remove all segments from the segment stack.
177 while ((sg = segment_pop(p)))
178 {
179 sg_put(sg);
180 }
bart0268dfa2008-03-11 20:10:21 +0000181 }
bartd9e39ec2008-06-28 15:03:26 +0000182 else
183 {
184 p = semaphore_get_or_allocate(semaphore);
185 }
186 tl_assert(p);
bartc8914e92008-12-24 09:45:41 +0000187 p->waits_to_skip = value;
bart94866cc2008-12-21 17:20:22 +0000188 p->value = value;
sewardj85642922008-01-14 11:54:56 +0000189 return p;
190}
191
bart28230a32008-02-29 17:27:03 +0000192/** Called after sem_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000193void semaphore_destroy(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000194{
bart72b751c2008-03-01 13:44:24 +0000195 struct semaphore_info* p;
bart3b1ee452008-02-29 19:28:15 +0000196
bartda9436b2008-12-14 08:56:49 +0000197 p = semaphore_get(semaphore);
198
bart3b1ee452008-02-29 19:28:15 +0000199 if (s_trace_semaphore)
200 {
201 VG_(message)(Vg_UserMsg,
bartafb42b72008-12-17 07:32:09 +0000202 "[%d/%d] semaphore_destroy 0x%lx value %u",
bart3b1ee452008-02-29 19:28:15 +0000203 VG_(get_running_tid)(),
204 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000205 semaphore,
206 p ? p->value : 0);
bart3b1ee452008-02-29 19:28:15 +0000207 }
208
bart72b751c2008-03-01 13:44:24 +0000209 if (p == 0)
210 {
211 GenericErrInfo GEI;
212 VG_(maybe_record_error)(VG_(get_running_tid)(),
213 GenericErr,
214 VG_(get_IP)(VG_(get_running_tid)()),
215 "Not a semaphore",
216 &GEI);
217 return;
218 }
219
220 clientobj_remove(semaphore, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000221}
222
bart28230a32008-02-29 17:27:03 +0000223/** Called before sem_wait(). */
bart0268dfa2008-03-11 20:10:21 +0000224void semaphore_pre_wait(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000225{
226 struct semaphore_info* p;
227
bart0268dfa2008-03-11 20:10:21 +0000228 p = semaphore_get_or_allocate(semaphore);
bart28230a32008-02-29 17:27:03 +0000229 tl_assert(p);
bart74a5f212008-05-11 06:43:07 +0000230 tl_assert((int)p->waiters >= 0);
bart28230a32008-02-29 17:27:03 +0000231 p->waiters++;
232 tl_assert(p->waiters > 0);
233}
234
235/** Called after sem_wait() finished.
236 * @note Do not rely on the value of 'waited' -- some glibc versions do
237 * not set it correctly.
238 */
239void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
240 const Bool waited)
241{
242 struct semaphore_info* p;
bart3e017fa2008-12-17 19:20:13 +0000243 Segment* sg;
bart28230a32008-02-29 17:27:03 +0000244
245 p = semaphore_get(semaphore);
246 if (s_trace_semaphore)
247 {
bart3b1ee452008-02-29 19:28:15 +0000248 VG_(message)(Vg_UserMsg,
bart3e017fa2008-12-17 19:20:13 +0000249 "[%d/%d] semaphore_wait 0x%lx value %u -> %u",
bart3b1ee452008-02-29 19:28:15 +0000250 VG_(get_running_tid)(),
251 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000252 semaphore,
bart3e017fa2008-12-17 19:20:13 +0000253 p ? p->value : 0,
bartda9436b2008-12-14 08:56:49 +0000254 p ? p->value - 1 : 0);
bart28230a32008-02-29 17:27:03 +0000255 }
bart3e017fa2008-12-17 19:20:13 +0000256 tl_assert(p);
bart28230a32008-02-29 17:27:03 +0000257 tl_assert(p->waiters > 0);
258 p->waiters--;
bart74a5f212008-05-11 06:43:07 +0000259 tl_assert((int)p->waiters >= 0);
260 tl_assert((int)p->value >= 0);
bart28230a32008-02-29 17:27:03 +0000261 if (p->value == 0)
262 {
bart3b1ee452008-02-29 19:28:15 +0000263 SemaphoreErrInfo sei = { semaphore };
264 VG_(maybe_record_error)(VG_(get_running_tid)(),
265 SemaphoreErr,
266 VG_(get_IP)(VG_(get_running_tid)()),
267 "Invalid semaphore",
268 &sei);
bart28230a32008-02-29 17:27:03 +0000269 return;
270 }
sewardj85642922008-01-14 11:54:56 +0000271 p->value--;
bart74a5f212008-05-11 06:43:07 +0000272 tl_assert((int)p->value >= 0);
bartc8914e92008-12-24 09:45:41 +0000273 if (p->waits_to_skip > 0)
274 p->waits_to_skip--;
bart94866cc2008-12-21 17:20:22 +0000275 else
barta2b6e1b2008-03-17 18:32:39 +0000276 {
bart94866cc2008-12-21 17:20:22 +0000277 sg = segment_pop(p);
278 tl_assert(sg);
279 if (sg)
bart3e017fa2008-12-17 19:20:13 +0000280 {
bart94866cc2008-12-21 17:20:22 +0000281 if (p->last_sem_post_tid != tid
282 && p->last_sem_post_tid != DRD_INVALID_THREADID)
283 {
284 thread_combine_vc2(tid, &sg->vc);
285 }
286 sg_put(sg);
287 thread_new_segment(tid);
288 s_semaphore_segment_creation_count++;
bart3e017fa2008-12-17 19:20:13 +0000289 }
barta2b6e1b2008-03-17 18:32:39 +0000290 }
sewardj85642922008-01-14 11:54:56 +0000291}
292
293/** Called before sem_post(). */
bart0268dfa2008-03-11 20:10:21 +0000294void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000295{
296 struct semaphore_info* p;
bart3e017fa2008-12-17 19:20:13 +0000297 Segment* sg;
sewardj85642922008-01-14 11:54:56 +0000298
bartda9436b2008-12-14 08:56:49 +0000299 p = semaphore_get_or_allocate(semaphore);
300 p->value++;
301
bart3b1ee452008-02-29 19:28:15 +0000302 if (s_trace_semaphore)
303 {
304 VG_(message)(Vg_UserMsg,
bart3e017fa2008-12-17 19:20:13 +0000305 "[%d/%d] semaphore_post 0x%lx value %u -> %u",
bart3b1ee452008-02-29 19:28:15 +0000306 VG_(get_running_tid)(),
307 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000308 semaphore,
bart3e017fa2008-12-17 19:20:13 +0000309 p->value - 1, p->value);
bart3b1ee452008-02-29 19:28:15 +0000310 }
bartda9436b2008-12-14 08:56:49 +0000311
bart3e017fa2008-12-17 19:20:13 +0000312 p->last_sem_post_tid = tid;
313 thread_new_segment(tid);
314 sg = 0;
315 thread_get_latest_segment(&sg, tid);
316 tl_assert(sg);
317 segment_push(p, sg);
318 s_semaphore_segment_creation_count++;
sewardj85642922008-01-14 11:54:56 +0000319}
320
321/** Called after sem_post() finished successfully. */
322void semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000323 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000324{
bart25896d92008-02-17 09:21:05 +0000325 /* Note: it is hard to implement the sem_post() wrapper correctly in */
326 /* case sem_post() returns an error code. This is because handling this */
327 /* case correctly requires restoring the vector clock associated with */
328 /* the semaphore to its original value here. In order to do that without */
329 /* introducing a race condition, extra locking has to be added around */
330 /* each semaphore call. Such extra locking would have to be added in */
331 /* drd_intercepts.c. However, it is hard to implement synchronization */
332 /* in drd_intercepts.c in a portable way without calling already */
333 /* redirected functions. */
sewardj85642922008-01-14 11:54:56 +0000334}
335
336void semaphore_thread_delete(const DrdThreadId threadid)
337{ }
bart6bbefaf2008-04-19 15:16:45 +0000338
339ULong get_semaphore_segment_creation_count(void)
340{
341 return s_semaphore_segment_creation_count;
342}