blob: d9c818f68b739806524e8a819359a4fa004affd9 [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;
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,
120 "[%d/%d] semaphore_init 0x%lx",
121 VG_(get_running_tid)(),
122 thread_get_running_tid(),
123 semaphore);
124 }
bart0268dfa2008-03-11 20:10:21 +0000125 if (semaphore_get(semaphore))
126 {
127 // To do: print an error message that a semaphore is being reinitialized.
128 }
129 p = semaphore_get_or_allocate(semaphore);
sewardj85642922008-01-14 11:54:56 +0000130 p->value = value;
131 return p;
132}
133
bart28230a32008-02-29 17:27:03 +0000134/** Called after sem_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000135void semaphore_destroy(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000136{
bart72b751c2008-03-01 13:44:24 +0000137 struct semaphore_info* p;
bart3b1ee452008-02-29 19:28:15 +0000138
139 if (s_trace_semaphore)
140 {
141 VG_(message)(Vg_UserMsg,
142 "[%d/%d] semaphore_destroy 0x%lx",
143 VG_(get_running_tid)(),
144 thread_get_running_tid(),
bart72b751c2008-03-01 13:44:24 +0000145 semaphore);
bart3b1ee452008-02-29 19:28:15 +0000146 }
147
bart72b751c2008-03-01 13:44:24 +0000148 p = semaphore_get(semaphore);
149
150 if (p == 0)
151 {
152 GenericErrInfo GEI;
153 VG_(maybe_record_error)(VG_(get_running_tid)(),
154 GenericErr,
155 VG_(get_IP)(VG_(get_running_tid)()),
156 "Not a semaphore",
157 &GEI);
158 return;
159 }
160
161 clientobj_remove(semaphore, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000162}
163
bart28230a32008-02-29 17:27:03 +0000164/** Called before sem_wait(). */
bart0268dfa2008-03-11 20:10:21 +0000165void semaphore_pre_wait(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000166{
167 struct semaphore_info* p;
168
bart0268dfa2008-03-11 20:10:21 +0000169 p = semaphore_get_or_allocate(semaphore);
bart28230a32008-02-29 17:27:03 +0000170 if (s_trace_semaphore)
171 {
bart3b1ee452008-02-29 19:28:15 +0000172 VG_(message)(Vg_UserMsg,
173 "[%d/%d] semaphore_pre_wait 0x%lx",
174 VG_(get_running_tid)(),
175 thread_get_running_tid(),
176 semaphore);
bart28230a32008-02-29 17:27:03 +0000177 }
178 tl_assert(p);
179 tl_assert(p->waiters >= 0);
180 p->waiters++;
181 tl_assert(p->waiters > 0);
182}
183
184/** Called after sem_wait() finished.
185 * @note Do not rely on the value of 'waited' -- some glibc versions do
186 * not set it correctly.
187 */
188void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
189 const Bool waited)
190{
191 struct semaphore_info* p;
192
193 p = semaphore_get(semaphore);
194 if (s_trace_semaphore)
195 {
bart3b1ee452008-02-29 19:28:15 +0000196 VG_(message)(Vg_UserMsg,
197 "[%d/%d] semaphore_post_wait 0x%lx",
198 VG_(get_running_tid)(),
199 thread_get_running_tid(),
200 semaphore);
bart28230a32008-02-29 17:27:03 +0000201 }
202 tl_assert(p->waiters > 0);
203 p->waiters--;
204 tl_assert(p->waiters >= 0);
sewardj85642922008-01-14 11:54:56 +0000205 tl_assert(p->value >= 0);
bart28230a32008-02-29 17:27:03 +0000206 if (p->value == 0)
207 {
bart3b1ee452008-02-29 19:28:15 +0000208 SemaphoreErrInfo sei = { semaphore };
209 VG_(maybe_record_error)(VG_(get_running_tid)(),
210 SemaphoreErr,
211 VG_(get_IP)(VG_(get_running_tid)()),
212 "Invalid semaphore",
213 &sei);
bart28230a32008-02-29 17:27:03 +0000214 return;
215 }
sewardj85642922008-01-14 11:54:56 +0000216 p->value--;
217 tl_assert(p->value >= 0);
barta2b6e1b2008-03-17 18:32:39 +0000218 if (p->last_sem_post_tid != tid
219 && p->last_sem_post_tid != DRD_INVALID_THREADID)
220 {
221 tl_assert(p->last_sem_post_segment);
222 thread_combine_vc2(tid, &p->last_sem_post_segment->vc);
223 }
sewardj85642922008-01-14 11:54:56 +0000224 thread_new_segment(tid);
225}
226
227/** Called before sem_post(). */
bart0268dfa2008-03-11 20:10:21 +0000228void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000229{
230 struct semaphore_info* p;
231
bart3b1ee452008-02-29 19:28:15 +0000232 if (s_trace_semaphore)
233 {
234 VG_(message)(Vg_UserMsg,
235 "[%d/%d] semaphore_post 0x%lx",
236 VG_(get_running_tid)(),
237 thread_get_running_tid(),
238 semaphore);
239 }
bart0268dfa2008-03-11 20:10:21 +0000240 p = semaphore_get_or_allocate(semaphore);
sewardj85642922008-01-14 11:54:56 +0000241 p->value++;
242 if (p->value == 1)
243 {
244 p->last_sem_post_tid = tid;
sewardjc0be9252008-02-11 11:00:51 +0000245 thread_new_segment(tid);
barta2b6e1b2008-03-17 18:32:39 +0000246 thread_get_latest_segment(&p->last_sem_post_segment, tid);
sewardj85642922008-01-14 11:54:56 +0000247 }
248}
249
250/** Called after sem_post() finished successfully. */
251void semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000252 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000253{
bart25896d92008-02-17 09:21:05 +0000254 /* Note: it is hard to implement the sem_post() wrapper correctly in */
255 /* case sem_post() returns an error code. This is because handling this */
256 /* case correctly requires restoring the vector clock associated with */
257 /* the semaphore to its original value here. In order to do that without */
258 /* introducing a race condition, extra locking has to be added around */
259 /* each semaphore call. Such extra locking would have to be added in */
260 /* drd_intercepts.c. However, it is hard to implement synchronization */
261 /* in drd_intercepts.c in a portable way without calling already */
262 /* redirected functions. */
sewardj85642922008-01-14 11:54:56 +0000263}
264
265void semaphore_thread_delete(const DrdThreadId threadid)
266{ }