blob: 4bedf8f0c8bd4f1693c4a87d73d7db026044f2b0 [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"
30#include "priv_drd_clientreq.h"
31#include "pub_tool_errormgr.h" // VG_(maybe_record_error)()
32#include "pub_tool_libcassert.h" // tl_assert()
33#include "pub_tool_libcprint.h" // VG_(printf)()
34#include "pub_tool_machine.h" // VG_(get_IP)()
35#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
51void semaphore_set_trace(const Bool trace_semaphore)
52{
53 s_trace_semaphore = trace_semaphore;
54}
55
56static
57void semaphore_initialize(struct semaphore_info* const p,
bart0268dfa2008-03-11 20:10:21 +000058 const Addr semaphore, const UWord value)
sewardj85642922008-01-14 11:54:56 +000059{
60 tl_assert(semaphore != 0);
bart28230a32008-02-29 17:27:03 +000061 tl_assert(p->a1 == semaphore);
bart28230a32008-02-29 17:27:03 +000062 tl_assert(p->type == ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +000063
bart28230a32008-02-29 17:27:03 +000064 p->cleanup = (void(*)(DrdClientobj*))semaphore_cleanup;
sewardj85642922008-01-14 11:54:56 +000065 p->value = value;
bart28230a32008-02-29 17:27:03 +000066 p->waiters = 0;
sewardj85642922008-01-14 11:54:56 +000067 p->last_sem_post_tid = DRD_INVALID_THREADID;
barta2b6e1b2008-03-17 18:32:39 +000068 p->last_sem_post_segment = 0;
sewardj85642922008-01-14 11:54:56 +000069}
70
bart28230a32008-02-29 17:27:03 +000071/** Free the memory that was allocated by semaphore_initialize(). Called by
bart72b751c2008-03-01 13:44:24 +000072 * clientobj_remove().
bart28230a32008-02-29 17:27:03 +000073 */
74static void semaphore_cleanup(struct semaphore_info* p)
75{
76 if (p->waiters > 0)
77 {
bart3b1ee452008-02-29 19:28:15 +000078 SemaphoreErrInfo sei = { p->a1 };
79 VG_(maybe_record_error)(VG_(get_running_tid)(),
80 SemaphoreErr,
81 VG_(get_IP)(VG_(get_running_tid)()),
82 "Destruction of semaphore that is being waited"
83 " upon",
84 &sei);
bart28230a32008-02-29 17:27:03 +000085 }
barta2b6e1b2008-03-17 18:32:39 +000086 sg_put(p->last_sem_post_segment);
bart28230a32008-02-29 17:27:03 +000087}
88
sewardj85642922008-01-14 11:54:56 +000089static
90struct semaphore_info*
bart0268dfa2008-03-11 20:10:21 +000091semaphore_get_or_allocate(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +000092{
bart28230a32008-02-29 17:27:03 +000093 struct semaphore_info *p;
sewardj85642922008-01-14 11:54:56 +000094
bart28230a32008-02-29 17:27:03 +000095 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart72b751c2008-03-01 13:44:24 +000096 p = &clientobj_get(semaphore, ClientSemaphore)->semaphore;
bart28230a32008-02-29 17:27:03 +000097 if (p == 0)
sewardj85642922008-01-14 11:54:56 +000098 {
bart28230a32008-02-29 17:27:03 +000099 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart0268dfa2008-03-11 20:10:21 +0000100 p = &clientobj_add(semaphore, ClientSemaphore)->semaphore;
101 semaphore_initialize(p, semaphore, 0);
sewardj85642922008-01-14 11:54:56 +0000102 }
bart28230a32008-02-29 17:27:03 +0000103 return p;
sewardj85642922008-01-14 11:54:56 +0000104}
105
bart72b751c2008-03-01 13:44:24 +0000106static struct semaphore_info* semaphore_get(const Addr semaphore)
bart28230a32008-02-29 17:27:03 +0000107{
108 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart72b751c2008-03-01 13:44:24 +0000109 return &clientobj_get(semaphore, ClientSemaphore)->semaphore;
bart28230a32008-02-29 17:27:03 +0000110}
111
112/** Called before sem_init(). */
bart0268dfa2008-03-11 20:10:21 +0000113struct semaphore_info* semaphore_init(const Addr semaphore,
sewardj85642922008-01-14 11:54:56 +0000114 const Word pshared, const UWord value)
115{
116 struct semaphore_info* p;
117
bart3b1ee452008-02-29 19:28:15 +0000118 if (s_trace_semaphore)
119 {
120 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000121 "[%d/%d] semaphore_init 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000122 VG_(get_running_tid)(),
123 thread_get_running_tid(),
124 semaphore);
125 }
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
151 if (s_trace_semaphore)
152 {
153 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000154 "[%d/%d] semaphore_destroy 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000155 VG_(get_running_tid)(),
156 thread_get_running_tid(),
bart72b751c2008-03-01 13:44:24 +0000157 semaphore);
bart3b1ee452008-02-29 19:28:15 +0000158 }
159
bart72b751c2008-03-01 13:44:24 +0000160 p = semaphore_get(semaphore);
161
162 if (p == 0)
163 {
164 GenericErrInfo GEI;
165 VG_(maybe_record_error)(VG_(get_running_tid)(),
166 GenericErr,
167 VG_(get_IP)(VG_(get_running_tid)()),
168 "Not a semaphore",
169 &GEI);
170 return;
171 }
172
173 clientobj_remove(semaphore, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000174}
175
bart28230a32008-02-29 17:27:03 +0000176/** Called before sem_wait(). */
bart0268dfa2008-03-11 20:10:21 +0000177void semaphore_pre_wait(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000178{
179 struct semaphore_info* p;
180
bart0268dfa2008-03-11 20:10:21 +0000181 p = semaphore_get_or_allocate(semaphore);
bart28230a32008-02-29 17:27:03 +0000182 if (s_trace_semaphore)
183 {
bart3b1ee452008-02-29 19:28:15 +0000184 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000185 "[%d/%d] semaphore_pre_wait 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000186 VG_(get_running_tid)(),
187 thread_get_running_tid(),
188 semaphore);
bart28230a32008-02-29 17:27:03 +0000189 }
190 tl_assert(p);
bart74a5f212008-05-11 06:43:07 +0000191 tl_assert((int)p->waiters >= 0);
bart28230a32008-02-29 17:27:03 +0000192 p->waiters++;
193 tl_assert(p->waiters > 0);
194}
195
196/** Called after sem_wait() finished.
197 * @note Do not rely on the value of 'waited' -- some glibc versions do
198 * not set it correctly.
199 */
200void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
201 const Bool waited)
202{
203 struct semaphore_info* p;
204
205 p = semaphore_get(semaphore);
206 if (s_trace_semaphore)
207 {
bart3b1ee452008-02-29 19:28:15 +0000208 VG_(message)(Vg_UserMsg,
209 "[%d/%d] semaphore_post_wait 0x%lx",
210 VG_(get_running_tid)(),
211 thread_get_running_tid(),
212 semaphore);
bart28230a32008-02-29 17:27:03 +0000213 }
214 tl_assert(p->waiters > 0);
215 p->waiters--;
bart74a5f212008-05-11 06:43:07 +0000216 tl_assert((int)p->waiters >= 0);
217 tl_assert((int)p->value >= 0);
bart28230a32008-02-29 17:27:03 +0000218 if (p->value == 0)
219 {
bart3b1ee452008-02-29 19:28:15 +0000220 SemaphoreErrInfo sei = { semaphore };
221 VG_(maybe_record_error)(VG_(get_running_tid)(),
222 SemaphoreErr,
223 VG_(get_IP)(VG_(get_running_tid)()),
224 "Invalid semaphore",
225 &sei);
bart28230a32008-02-29 17:27:03 +0000226 return;
227 }
sewardj85642922008-01-14 11:54:56 +0000228 p->value--;
bart74a5f212008-05-11 06:43:07 +0000229 tl_assert((int)p->value >= 0);
barta2b6e1b2008-03-17 18:32:39 +0000230 if (p->last_sem_post_tid != tid
231 && p->last_sem_post_tid != DRD_INVALID_THREADID)
232 {
233 tl_assert(p->last_sem_post_segment);
234 thread_combine_vc2(tid, &p->last_sem_post_segment->vc);
235 }
sewardj85642922008-01-14 11:54:56 +0000236 thread_new_segment(tid);
bart6bbefaf2008-04-19 15:16:45 +0000237 s_semaphore_segment_creation_count++;
sewardj85642922008-01-14 11:54:56 +0000238}
239
240/** Called before sem_post(). */
bart0268dfa2008-03-11 20:10:21 +0000241void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000242{
243 struct semaphore_info* p;
244
bart3b1ee452008-02-29 19:28:15 +0000245 if (s_trace_semaphore)
246 {
247 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000248 "[%d/%d] semaphore_post 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000249 VG_(get_running_tid)(),
250 thread_get_running_tid(),
251 semaphore);
252 }
bart0268dfa2008-03-11 20:10:21 +0000253 p = semaphore_get_or_allocate(semaphore);
sewardj85642922008-01-14 11:54:56 +0000254 p->value++;
255 if (p->value == 1)
256 {
257 p->last_sem_post_tid = tid;
sewardjc0be9252008-02-11 11:00:51 +0000258 thread_new_segment(tid);
barta2b6e1b2008-03-17 18:32:39 +0000259 thread_get_latest_segment(&p->last_sem_post_segment, tid);
bart6bbefaf2008-04-19 15:16:45 +0000260 s_semaphore_segment_creation_count++;
sewardj85642922008-01-14 11:54:56 +0000261 }
262}
263
264/** Called after sem_post() finished successfully. */
265void semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000266 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000267{
bart25896d92008-02-17 09:21:05 +0000268 /* Note: it is hard to implement the sem_post() wrapper correctly in */
269 /* case sem_post() returns an error code. This is because handling this */
270 /* case correctly requires restoring the vector clock associated with */
271 /* the semaphore to its original value here. In order to do that without */
272 /* introducing a race condition, extra locking has to be added around */
273 /* each semaphore call. Such extra locking would have to be added in */
274 /* drd_intercepts.c. However, it is hard to implement synchronization */
275 /* in drd_intercepts.c in a portable way without calling already */
276 /* redirected functions. */
sewardj85642922008-01-14 11:54:56 +0000277}
278
279void semaphore_thread_delete(const DrdThreadId threadid)
280{ }
bart6bbefaf2008-04-19 15:16:45 +0000281
282ULong get_semaphore_segment_creation_count(void)
283{
284 return s_semaphore_segment_creation_count;
285}