blob: 1125c1b174209852e72a1d778f3385543ac79a34 [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)()
34#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
35
36
bart28230a32008-02-29 17:27:03 +000037// Local functions.
sewardj85642922008-01-14 11:54:56 +000038
bart28230a32008-02-29 17:27:03 +000039static void semaphore_cleanup(struct semaphore_info* p);
sewardj85642922008-01-14 11:54:56 +000040
41
42// Local variables.
43
44static Bool s_trace_semaphore;
bart6bbefaf2008-04-19 15:16:45 +000045static ULong s_semaphore_segment_creation_count;
sewardj85642922008-01-14 11:54:56 +000046
47
48// Function definitions.
49
50void semaphore_set_trace(const Bool trace_semaphore)
51{
52 s_trace_semaphore = trace_semaphore;
53}
54
55static
56void semaphore_initialize(struct semaphore_info* const p,
bart0268dfa2008-03-11 20:10:21 +000057 const Addr semaphore, const UWord value)
sewardj85642922008-01-14 11:54:56 +000058{
59 tl_assert(semaphore != 0);
bart28230a32008-02-29 17:27:03 +000060 tl_assert(p->a1 == semaphore);
bart28230a32008-02-29 17:27:03 +000061 tl_assert(p->type == ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +000062
bart28230a32008-02-29 17:27:03 +000063 p->cleanup = (void(*)(DrdClientobj*))semaphore_cleanup;
sewardj85642922008-01-14 11:54:56 +000064 p->value = value;
bart28230a32008-02-29 17:27:03 +000065 p->waiters = 0;
sewardj85642922008-01-14 11:54:56 +000066 p->last_sem_post_tid = DRD_INVALID_THREADID;
barta2b6e1b2008-03-17 18:32:39 +000067 p->last_sem_post_segment = 0;
sewardj85642922008-01-14 11:54:56 +000068}
69
bart28230a32008-02-29 17:27:03 +000070/** Free the memory that was allocated by semaphore_initialize(). Called by
bart72b751c2008-03-01 13:44:24 +000071 * clientobj_remove().
bart28230a32008-02-29 17:27:03 +000072 */
73static void semaphore_cleanup(struct semaphore_info* p)
74{
75 if (p->waiters > 0)
76 {
bart3b1ee452008-02-29 19:28:15 +000077 SemaphoreErrInfo sei = { p->a1 };
78 VG_(maybe_record_error)(VG_(get_running_tid)(),
79 SemaphoreErr,
80 VG_(get_IP)(VG_(get_running_tid)()),
81 "Destruction of semaphore that is being waited"
82 " upon",
83 &sei);
bart28230a32008-02-29 17:27:03 +000084 }
barta2b6e1b2008-03-17 18:32:39 +000085 sg_put(p->last_sem_post_segment);
bart28230a32008-02-29 17:27:03 +000086}
87
sewardj85642922008-01-14 11:54:56 +000088static
89struct semaphore_info*
bart0268dfa2008-03-11 20:10:21 +000090semaphore_get_or_allocate(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +000091{
bart28230a32008-02-29 17:27:03 +000092 struct semaphore_info *p;
sewardj85642922008-01-14 11:54:56 +000093
bart28230a32008-02-29 17:27:03 +000094 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart72b751c2008-03-01 13:44:24 +000095 p = &clientobj_get(semaphore, ClientSemaphore)->semaphore;
bart28230a32008-02-29 17:27:03 +000096 if (p == 0)
sewardj85642922008-01-14 11:54:56 +000097 {
bart28230a32008-02-29 17:27:03 +000098 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart0268dfa2008-03-11 20:10:21 +000099 p = &clientobj_add(semaphore, ClientSemaphore)->semaphore;
100 semaphore_initialize(p, semaphore, 0);
sewardj85642922008-01-14 11:54:56 +0000101 }
bart28230a32008-02-29 17:27:03 +0000102 return p;
sewardj85642922008-01-14 11:54:56 +0000103}
104
bart72b751c2008-03-01 13:44:24 +0000105static struct semaphore_info* semaphore_get(const Addr semaphore)
bart28230a32008-02-29 17:27:03 +0000106{
107 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart72b751c2008-03-01 13:44:24 +0000108 return &clientobj_get(semaphore, ClientSemaphore)->semaphore;
bart28230a32008-02-29 17:27:03 +0000109}
110
111/** Called before sem_init(). */
bart0268dfa2008-03-11 20:10:21 +0000112struct semaphore_info* semaphore_init(const Addr semaphore,
sewardj85642922008-01-14 11:54:56 +0000113 const Word pshared, const UWord value)
114{
115 struct semaphore_info* p;
116
bart3b1ee452008-02-29 19:28:15 +0000117 if (s_trace_semaphore)
118 {
119 VG_(message)(Vg_UserMsg,
bartda9436b2008-12-14 08:56:49 +0000120 "[%d/%d] semaphore_init 0x%lx value %d",
bart3b1ee452008-02-29 19:28:15 +0000121 VG_(get_running_tid)(),
122 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000123 semaphore,
124 value);
bart3b1ee452008-02-29 19:28:15 +0000125 }
bartd9e39ec2008-06-28 15:03:26 +0000126 p = semaphore_get(semaphore);
127 if (p)
bart0268dfa2008-03-11 20:10:21 +0000128 {
bartd9e39ec2008-06-28 15:03:26 +0000129 const ThreadId vg_tid = VG_(get_running_tid)();
130 SemaphoreErrInfo SEI = { semaphore };
131 VG_(maybe_record_error)(vg_tid,
132 SemaphoreErr,
133 VG_(get_IP)(vg_tid),
134 "Semaphore reinitialization",
135 &SEI);
bart0268dfa2008-03-11 20:10:21 +0000136 }
bartd9e39ec2008-06-28 15:03:26 +0000137 else
138 {
139 p = semaphore_get_or_allocate(semaphore);
140 }
141 tl_assert(p);
sewardj85642922008-01-14 11:54:56 +0000142 p->value = value;
143 return p;
144}
145
bart28230a32008-02-29 17:27:03 +0000146/** Called after sem_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000147void semaphore_destroy(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000148{
bart72b751c2008-03-01 13:44:24 +0000149 struct semaphore_info* p;
bart3b1ee452008-02-29 19:28:15 +0000150
bartda9436b2008-12-14 08:56:49 +0000151 p = semaphore_get(semaphore);
152
bart3b1ee452008-02-29 19:28:15 +0000153 if (s_trace_semaphore)
154 {
155 VG_(message)(Vg_UserMsg,
bartda9436b2008-12-14 08:56:49 +0000156 "[%d/%d] semaphore_destroy 0x%lx value %d",
bart3b1ee452008-02-29 19:28:15 +0000157 VG_(get_running_tid)(),
158 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000159 semaphore,
160 p ? p->value : 0);
bart3b1ee452008-02-29 19:28:15 +0000161 }
162
bart72b751c2008-03-01 13:44:24 +0000163 if (p == 0)
164 {
165 GenericErrInfo GEI;
166 VG_(maybe_record_error)(VG_(get_running_tid)(),
167 GenericErr,
168 VG_(get_IP)(VG_(get_running_tid)()),
169 "Not a semaphore",
170 &GEI);
171 return;
172 }
173
174 clientobj_remove(semaphore, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000175}
176
bart28230a32008-02-29 17:27:03 +0000177/** Called before sem_wait(). */
bart0268dfa2008-03-11 20:10:21 +0000178void semaphore_pre_wait(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000179{
180 struct semaphore_info* p;
181
bart0268dfa2008-03-11 20:10:21 +0000182 p = semaphore_get_or_allocate(semaphore);
bart28230a32008-02-29 17:27:03 +0000183 if (s_trace_semaphore)
184 {
bart3b1ee452008-02-29 19:28:15 +0000185 VG_(message)(Vg_UserMsg,
bartda9436b2008-12-14 08:56:49 +0000186 "[%d/%d] semaphore_pre_wait 0x%lx value %d",
bart3b1ee452008-02-29 19:28:15 +0000187 VG_(get_running_tid)(),
188 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000189 semaphore,
190 p->value);
bart28230a32008-02-29 17:27:03 +0000191 }
192 tl_assert(p);
bart74a5f212008-05-11 06:43:07 +0000193 tl_assert((int)p->waiters >= 0);
bart28230a32008-02-29 17:27:03 +0000194 p->waiters++;
195 tl_assert(p->waiters > 0);
196}
197
198/** Called after sem_wait() finished.
199 * @note Do not rely on the value of 'waited' -- some glibc versions do
200 * not set it correctly.
201 */
202void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
203 const Bool waited)
204{
205 struct semaphore_info* p;
206
207 p = semaphore_get(semaphore);
208 if (s_trace_semaphore)
209 {
bart3b1ee452008-02-29 19:28:15 +0000210 VG_(message)(Vg_UserMsg,
bartda9436b2008-12-14 08:56:49 +0000211 "[%d/%d] semaphore_post_wait 0x%lx value %d",
bart3b1ee452008-02-29 19:28:15 +0000212 VG_(get_running_tid)(),
213 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000214 semaphore,
215 p ? p->value - 1 : 0);
bart28230a32008-02-29 17:27:03 +0000216 }
217 tl_assert(p->waiters > 0);
218 p->waiters--;
bart74a5f212008-05-11 06:43:07 +0000219 tl_assert((int)p->waiters >= 0);
220 tl_assert((int)p->value >= 0);
bart28230a32008-02-29 17:27:03 +0000221 if (p->value == 0)
222 {
bart3b1ee452008-02-29 19:28:15 +0000223 SemaphoreErrInfo sei = { semaphore };
224 VG_(maybe_record_error)(VG_(get_running_tid)(),
225 SemaphoreErr,
226 VG_(get_IP)(VG_(get_running_tid)()),
227 "Invalid semaphore",
228 &sei);
bart28230a32008-02-29 17:27:03 +0000229 return;
230 }
sewardj85642922008-01-14 11:54:56 +0000231 p->value--;
bart74a5f212008-05-11 06:43:07 +0000232 tl_assert((int)p->value >= 0);
barta2b6e1b2008-03-17 18:32:39 +0000233 if (p->last_sem_post_tid != tid
234 && p->last_sem_post_tid != DRD_INVALID_THREADID)
235 {
236 tl_assert(p->last_sem_post_segment);
237 thread_combine_vc2(tid, &p->last_sem_post_segment->vc);
238 }
sewardj85642922008-01-14 11:54:56 +0000239 thread_new_segment(tid);
bart6bbefaf2008-04-19 15:16:45 +0000240 s_semaphore_segment_creation_count++;
sewardj85642922008-01-14 11:54:56 +0000241}
242
243/** Called before sem_post(). */
bart0268dfa2008-03-11 20:10:21 +0000244void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000245{
246 struct semaphore_info* p;
247
bartda9436b2008-12-14 08:56:49 +0000248 p = semaphore_get_or_allocate(semaphore);
249 p->value++;
250
bart3b1ee452008-02-29 19:28:15 +0000251 if (s_trace_semaphore)
252 {
253 VG_(message)(Vg_UserMsg,
bartda9436b2008-12-14 08:56:49 +0000254 "[%d/%d] semaphore_post 0x%lx value %d",
bart3b1ee452008-02-29 19:28:15 +0000255 VG_(get_running_tid)(),
256 thread_get_running_tid(),
bartda9436b2008-12-14 08:56:49 +0000257 semaphore,
258 p->value);
bart3b1ee452008-02-29 19:28:15 +0000259 }
bartda9436b2008-12-14 08:56:49 +0000260
sewardj85642922008-01-14 11:54:56 +0000261 if (p->value == 1)
262 {
263 p->last_sem_post_tid = tid;
sewardjc0be9252008-02-11 11:00:51 +0000264 thread_new_segment(tid);
barta2b6e1b2008-03-17 18:32:39 +0000265 thread_get_latest_segment(&p->last_sem_post_segment, tid);
bart6bbefaf2008-04-19 15:16:45 +0000266 s_semaphore_segment_creation_count++;
sewardj85642922008-01-14 11:54:56 +0000267 }
268}
269
270/** Called after sem_post() finished successfully. */
271void semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000272 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000273{
bart25896d92008-02-17 09:21:05 +0000274 /* Note: it is hard to implement the sem_post() wrapper correctly in */
275 /* case sem_post() returns an error code. This is because handling this */
276 /* case correctly requires restoring the vector clock associated with */
277 /* the semaphore to its original value here. In order to do that without */
278 /* introducing a race condition, extra locking has to be added around */
279 /* each semaphore call. Such extra locking would have to be added in */
280 /* drd_intercepts.c. However, it is hard to implement synchronization */
281 /* in drd_intercepts.c in a portable way without calling already */
282 /* redirected functions. */
sewardj85642922008-01-14 11:54:56 +0000283}
284
285void semaphore_thread_delete(const DrdThreadId threadid)
286{ }
bart6bbefaf2008-04-19 15:16:45 +0000287
288ULong get_semaphore_segment_creation_count(void)
289{
290 return s_semaphore_segment_creation_count;
291}