blob: 2cc59dc3ad70f1aea10be300551313d31f7a8f37 [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
90void semaphore_initialize(struct semaphore_info* const p,
bart0268dfa2008-03-11 20:10:21 +000091 const Addr semaphore, const UWord value)
sewardj85642922008-01-14 11:54:56 +000092{
93 tl_assert(semaphore != 0);
bart28230a32008-02-29 17:27:03 +000094 tl_assert(p->a1 == semaphore);
bart28230a32008-02-29 17:27:03 +000095 tl_assert(p->type == ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +000096
bart28230a32008-02-29 17:27:03 +000097 p->cleanup = (void(*)(DrdClientobj*))semaphore_cleanup;
sewardj85642922008-01-14 11:54:56 +000098 p->value = value;
bart28230a32008-02-29 17:27:03 +000099 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;
139 semaphore_initialize(p, semaphore, 0);
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;
155
bart3b1ee452008-02-29 19:28:15 +0000156 if (s_trace_semaphore)
157 {
158 VG_(message)(Vg_UserMsg,
bartafb42b72008-12-17 07:32:09 +0000159 "[%d/%d] semaphore_init 0x%lx value %u",
bart3b1ee452008-02-29 19:28:15 +0000160 VG_(get_running_tid)(),
161 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000162 semaphore,
163 value);
bart3b1ee452008-02-29 19:28:15 +0000164 }
bartd9e39ec2008-06-28 15:03:26 +0000165 p = semaphore_get(semaphore);
166 if (p)
bart0268dfa2008-03-11 20:10:21 +0000167 {
bartd9e39ec2008-06-28 15:03:26 +0000168 const ThreadId vg_tid = VG_(get_running_tid)();
169 SemaphoreErrInfo SEI = { semaphore };
170 VG_(maybe_record_error)(vg_tid,
171 SemaphoreErr,
172 VG_(get_IP)(vg_tid),
173 "Semaphore reinitialization",
174 &SEI);
bart0268dfa2008-03-11 20:10:21 +0000175 }
bartd9e39ec2008-06-28 15:03:26 +0000176 else
177 {
178 p = semaphore_get_or_allocate(semaphore);
179 }
180 tl_assert(p);
sewardj85642922008-01-14 11:54:56 +0000181 p->value = value;
182 return p;
183}
184
bart28230a32008-02-29 17:27:03 +0000185/** Called after sem_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000186void semaphore_destroy(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000187{
bart72b751c2008-03-01 13:44:24 +0000188 struct semaphore_info* p;
bart3b1ee452008-02-29 19:28:15 +0000189
bartda9436b2008-12-14 08:56:49 +0000190 p = semaphore_get(semaphore);
191
bart3b1ee452008-02-29 19:28:15 +0000192 if (s_trace_semaphore)
193 {
194 VG_(message)(Vg_UserMsg,
bartafb42b72008-12-17 07:32:09 +0000195 "[%d/%d] semaphore_destroy 0x%lx value %u",
bart3b1ee452008-02-29 19:28:15 +0000196 VG_(get_running_tid)(),
197 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000198 semaphore,
199 p ? p->value : 0);
bart3b1ee452008-02-29 19:28:15 +0000200 }
201
bart72b751c2008-03-01 13:44:24 +0000202 if (p == 0)
203 {
204 GenericErrInfo GEI;
205 VG_(maybe_record_error)(VG_(get_running_tid)(),
206 GenericErr,
207 VG_(get_IP)(VG_(get_running_tid)()),
208 "Not a semaphore",
209 &GEI);
210 return;
211 }
212
213 clientobj_remove(semaphore, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000214}
215
bart28230a32008-02-29 17:27:03 +0000216/** Called before sem_wait(). */
bart0268dfa2008-03-11 20:10:21 +0000217void semaphore_pre_wait(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000218{
219 struct semaphore_info* p;
220
bart0268dfa2008-03-11 20:10:21 +0000221 p = semaphore_get_or_allocate(semaphore);
bart28230a32008-02-29 17:27:03 +0000222 tl_assert(p);
bart74a5f212008-05-11 06:43:07 +0000223 tl_assert((int)p->waiters >= 0);
bart28230a32008-02-29 17:27:03 +0000224 p->waiters++;
225 tl_assert(p->waiters > 0);
226}
227
228/** Called after sem_wait() finished.
229 * @note Do not rely on the value of 'waited' -- some glibc versions do
230 * not set it correctly.
231 */
232void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
233 const Bool waited)
234{
235 struct semaphore_info* p;
bart3e017fa2008-12-17 19:20:13 +0000236 Segment* sg;
bart28230a32008-02-29 17:27:03 +0000237
238 p = semaphore_get(semaphore);
239 if (s_trace_semaphore)
240 {
bart3b1ee452008-02-29 19:28:15 +0000241 VG_(message)(Vg_UserMsg,
bart3e017fa2008-12-17 19:20:13 +0000242 "[%d/%d] semaphore_wait 0x%lx value %u -> %u",
bart3b1ee452008-02-29 19:28:15 +0000243 VG_(get_running_tid)(),
244 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000245 semaphore,
bart3e017fa2008-12-17 19:20:13 +0000246 p ? p->value : 0,
bartda9436b2008-12-14 08:56:49 +0000247 p ? p->value - 1 : 0);
bart28230a32008-02-29 17:27:03 +0000248 }
bart3e017fa2008-12-17 19:20:13 +0000249 tl_assert(p);
bart28230a32008-02-29 17:27:03 +0000250 tl_assert(p->waiters > 0);
251 p->waiters--;
bart74a5f212008-05-11 06:43:07 +0000252 tl_assert((int)p->waiters >= 0);
253 tl_assert((int)p->value >= 0);
bart28230a32008-02-29 17:27:03 +0000254 if (p->value == 0)
255 {
bart3b1ee452008-02-29 19:28:15 +0000256 SemaphoreErrInfo sei = { semaphore };
257 VG_(maybe_record_error)(VG_(get_running_tid)(),
258 SemaphoreErr,
259 VG_(get_IP)(VG_(get_running_tid)()),
260 "Invalid semaphore",
261 &sei);
bart28230a32008-02-29 17:27:03 +0000262 return;
263 }
sewardj85642922008-01-14 11:54:56 +0000264 p->value--;
bart74a5f212008-05-11 06:43:07 +0000265 tl_assert((int)p->value >= 0);
bart3e017fa2008-12-17 19:20:13 +0000266 sg = segment_pop(p);
267 if (sg)
barta2b6e1b2008-03-17 18:32:39 +0000268 {
bart3e017fa2008-12-17 19:20:13 +0000269 if (p->last_sem_post_tid != tid
270 && p->last_sem_post_tid != DRD_INVALID_THREADID)
271 {
272 thread_combine_vc2(tid, &sg->vc);
273 }
274 sg_put(sg);
275 thread_new_segment(tid);
276 s_semaphore_segment_creation_count++;
barta2b6e1b2008-03-17 18:32:39 +0000277 }
sewardj85642922008-01-14 11:54:56 +0000278}
279
280/** Called before sem_post(). */
bart0268dfa2008-03-11 20:10:21 +0000281void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000282{
283 struct semaphore_info* p;
bart3e017fa2008-12-17 19:20:13 +0000284 Segment* sg;
sewardj85642922008-01-14 11:54:56 +0000285
bartda9436b2008-12-14 08:56:49 +0000286 p = semaphore_get_or_allocate(semaphore);
287 p->value++;
288
bart3b1ee452008-02-29 19:28:15 +0000289 if (s_trace_semaphore)
290 {
291 VG_(message)(Vg_UserMsg,
bart3e017fa2008-12-17 19:20:13 +0000292 "[%d/%d] semaphore_post 0x%lx value %u -> %u",
bart3b1ee452008-02-29 19:28:15 +0000293 VG_(get_running_tid)(),
294 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000295 semaphore,
bart3e017fa2008-12-17 19:20:13 +0000296 p->value - 1, p->value);
bart3b1ee452008-02-29 19:28:15 +0000297 }
bartda9436b2008-12-14 08:56:49 +0000298
bart3e017fa2008-12-17 19:20:13 +0000299 p->last_sem_post_tid = tid;
300 thread_new_segment(tid);
301 sg = 0;
302 thread_get_latest_segment(&sg, tid);
303 tl_assert(sg);
304 segment_push(p, sg);
305 s_semaphore_segment_creation_count++;
sewardj85642922008-01-14 11:54:56 +0000306}
307
308/** Called after sem_post() finished successfully. */
309void semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000310 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000311{
bart25896d92008-02-17 09:21:05 +0000312 /* Note: it is hard to implement the sem_post() wrapper correctly in */
313 /* case sem_post() returns an error code. This is because handling this */
314 /* case correctly requires restoring the vector clock associated with */
315 /* the semaphore to its original value here. In order to do that without */
316 /* introducing a race condition, extra locking has to be added around */
317 /* each semaphore call. Such extra locking would have to be added in */
318 /* drd_intercepts.c. However, it is hard to implement synchronization */
319 /* in drd_intercepts.c in a portable way without calling already */
320 /* redirected functions. */
sewardj85642922008-01-14 11:54:56 +0000321}
322
323void semaphore_thread_delete(const DrdThreadId threadid)
324{ }
bart6bbefaf2008-04-19 15:16:45 +0000325
326ULong get_semaphore_segment_creation_count(void)
327{
328 return s_semaphore_segment_creation_count;
329}