blob: 48b6f912415a740b5d7bb16488adb94264a26782 [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,
57 const Addr semaphore,
58 const SizeT size,
59 const UWord value)
60{
61 tl_assert(semaphore != 0);
62 tl_assert(size > 0);
bart28230a32008-02-29 17:27:03 +000063 tl_assert(p->a1 == semaphore);
64 tl_assert(p->a2 - p->a1 == size);
65 tl_assert(p->type == ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +000066
bart28230a32008-02-29 17:27:03 +000067 p->cleanup = (void(*)(DrdClientobj*))semaphore_cleanup;
sewardj85642922008-01-14 11:54:56 +000068 p->value = value;
bart28230a32008-02-29 17:27:03 +000069 p->waiters = 0;
sewardj85642922008-01-14 11:54:56 +000070 p->last_sem_post_tid = DRD_INVALID_THREADID;
71 vc_init(&p->vc, 0, 0);
72}
73
bart28230a32008-02-29 17:27:03 +000074/** Free the memory that was allocated by semaphore_initialize(). Called by
bart72b751c2008-03-01 13:44:24 +000075 * clientobj_remove().
bart28230a32008-02-29 17:27:03 +000076 */
77static void semaphore_cleanup(struct semaphore_info* p)
78{
79 if (p->waiters > 0)
80 {
bart3b1ee452008-02-29 19:28:15 +000081 SemaphoreErrInfo sei = { p->a1 };
82 VG_(maybe_record_error)(VG_(get_running_tid)(),
83 SemaphoreErr,
84 VG_(get_IP)(VG_(get_running_tid)()),
85 "Destruction of semaphore that is being waited"
86 " upon",
87 &sei);
bart28230a32008-02-29 17:27:03 +000088 }
89 vc_cleanup(&p->vc);
90}
91
sewardj85642922008-01-14 11:54:56 +000092static
93struct semaphore_info*
94semaphore_get_or_allocate(const Addr semaphore, const SizeT size)
95{
bart28230a32008-02-29 17:27:03 +000096 struct semaphore_info *p;
sewardj85642922008-01-14 11:54:56 +000097
bart28230a32008-02-29 17:27:03 +000098 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart72b751c2008-03-01 13:44:24 +000099 p = &clientobj_get(semaphore, ClientSemaphore)->semaphore;
bart28230a32008-02-29 17:27:03 +0000100 if (p == 0)
sewardj85642922008-01-14 11:54:56 +0000101 {
bart28230a32008-02-29 17:27:03 +0000102 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart72b751c2008-03-01 13:44:24 +0000103 p = &clientobj_add(semaphore, semaphore + size,
bart28230a32008-02-29 17:27:03 +0000104 ClientSemaphore)->semaphore;
105 semaphore_initialize(p, semaphore, size, 0);
sewardj85642922008-01-14 11:54:56 +0000106 }
bart28230a32008-02-29 17:27:03 +0000107 return p;
sewardj85642922008-01-14 11:54:56 +0000108}
109
bart72b751c2008-03-01 13:44:24 +0000110static struct semaphore_info* semaphore_get(const Addr semaphore)
bart28230a32008-02-29 17:27:03 +0000111{
112 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
bart72b751c2008-03-01 13:44:24 +0000113 return &clientobj_get(semaphore, ClientSemaphore)->semaphore;
bart28230a32008-02-29 17:27:03 +0000114}
115
116/** Called before sem_init(). */
sewardj85642922008-01-14 11:54:56 +0000117struct semaphore_info* semaphore_init(const Addr semaphore, const SizeT size,
118 const Word pshared, const UWord value)
119{
120 struct semaphore_info* p;
121
bart3b1ee452008-02-29 19:28:15 +0000122 if (s_trace_semaphore)
123 {
124 VG_(message)(Vg_UserMsg,
125 "[%d/%d] semaphore_init 0x%lx",
126 VG_(get_running_tid)(),
127 thread_get_running_tid(),
128 semaphore);
129 }
sewardj85642922008-01-14 11:54:56 +0000130 tl_assert(semaphore_get(semaphore) == 0);
131 p = semaphore_get_or_allocate(semaphore, size);
132 p->value = value;
133 return p;
134}
135
bart28230a32008-02-29 17:27:03 +0000136/** Called after sem_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000137void semaphore_destroy(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000138{
bart72b751c2008-03-01 13:44:24 +0000139 struct semaphore_info* p;
bart3b1ee452008-02-29 19:28:15 +0000140
141 if (s_trace_semaphore)
142 {
143 VG_(message)(Vg_UserMsg,
144 "[%d/%d] semaphore_destroy 0x%lx",
145 VG_(get_running_tid)(),
146 thread_get_running_tid(),
bart72b751c2008-03-01 13:44:24 +0000147 semaphore);
bart3b1ee452008-02-29 19:28:15 +0000148 }
149
bart72b751c2008-03-01 13:44:24 +0000150 p = semaphore_get(semaphore);
151
152 if (p == 0)
153 {
154 GenericErrInfo GEI;
155 VG_(maybe_record_error)(VG_(get_running_tid)(),
156 GenericErr,
157 VG_(get_IP)(VG_(get_running_tid)()),
158 "Not a semaphore",
159 &GEI);
160 return;
161 }
162
163 clientobj_remove(semaphore, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000164}
165
bart28230a32008-02-29 17:27:03 +0000166/** Called before sem_wait(). */
167void semaphore_pre_wait(const Addr semaphore, const SizeT size)
sewardj85642922008-01-14 11:54:56 +0000168{
169 struct semaphore_info* p;
170
171 p = semaphore_get_or_allocate(semaphore, size);
bart28230a32008-02-29 17:27:03 +0000172 if (s_trace_semaphore)
173 {
bart3b1ee452008-02-29 19:28:15 +0000174 VG_(message)(Vg_UserMsg,
175 "[%d/%d] semaphore_pre_wait 0x%lx",
176 VG_(get_running_tid)(),
177 thread_get_running_tid(),
178 semaphore);
bart28230a32008-02-29 17:27:03 +0000179 }
180 tl_assert(p);
181 tl_assert(p->waiters >= 0);
182 p->waiters++;
183 tl_assert(p->waiters > 0);
184}
185
186/** Called after sem_wait() finished.
187 * @note Do not rely on the value of 'waited' -- some glibc versions do
188 * not set it correctly.
189 */
190void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
191 const Bool waited)
192{
193 struct semaphore_info* p;
194
195 p = semaphore_get(semaphore);
196 if (s_trace_semaphore)
197 {
bart3b1ee452008-02-29 19:28:15 +0000198 VG_(message)(Vg_UserMsg,
199 "[%d/%d] semaphore_post_wait 0x%lx",
200 VG_(get_running_tid)(),
201 thread_get_running_tid(),
202 semaphore);
bart28230a32008-02-29 17:27:03 +0000203 }
204 tl_assert(p->waiters > 0);
205 p->waiters--;
206 tl_assert(p->waiters >= 0);
sewardj85642922008-01-14 11:54:56 +0000207 tl_assert(p->value >= 0);
bart28230a32008-02-29 17:27:03 +0000208 if (p->value == 0)
209 {
bart3b1ee452008-02-29 19:28:15 +0000210 SemaphoreErrInfo sei = { semaphore };
211 VG_(maybe_record_error)(VG_(get_running_tid)(),
212 SemaphoreErr,
213 VG_(get_IP)(VG_(get_running_tid)()),
214 "Invalid semaphore",
215 &sei);
bart28230a32008-02-29 17:27:03 +0000216 return;
217 }
sewardj85642922008-01-14 11:54:56 +0000218 p->value--;
219 tl_assert(p->value >= 0);
220 if (p->last_sem_post_tid != tid)
221 thread_combine_vc2(tid, &p->vc);
222 thread_new_segment(tid);
223}
224
225/** Called before sem_post(). */
226void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore,
227 const SizeT size)
228{
229 struct semaphore_info* p;
230
bart3b1ee452008-02-29 19:28:15 +0000231 if (s_trace_semaphore)
232 {
233 VG_(message)(Vg_UserMsg,
234 "[%d/%d] semaphore_post 0x%lx",
235 VG_(get_running_tid)(),
236 thread_get_running_tid(),
237 semaphore);
238 }
sewardj85642922008-01-14 11:54:56 +0000239 p = semaphore_get_or_allocate(semaphore, size);
240 p->value++;
241 if (p->value == 1)
242 {
243 p->last_sem_post_tid = tid;
sewardjc0be9252008-02-11 11:00:51 +0000244 thread_new_segment(tid);
bart9cdaf1e2008-02-24 18:29:43 +0000245 vc_assign(&p->vc, thread_get_vc(tid));
sewardj85642922008-01-14 11:54:56 +0000246 }
247}
248
249/** Called after sem_post() finished successfully. */
250void semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
sewardje3b57aa2008-01-18 07:42:01 +0000251 const SizeT size, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000252{
bart25896d92008-02-17 09:21:05 +0000253 /* Note: it is hard to implement the sem_post() wrapper correctly in */
254 /* case sem_post() returns an error code. This is because handling this */
255 /* case correctly requires restoring the vector clock associated with */
256 /* the semaphore to its original value here. In order to do that without */
257 /* introducing a race condition, extra locking has to be added around */
258 /* each semaphore call. Such extra locking would have to be added in */
259 /* drd_intercepts.c. However, it is hard to implement synchronization */
260 /* in drd_intercepts.c in a portable way without calling already */
261 /* redirected functions. */
sewardj85642922008-01-14 11:54:56 +0000262}
263
264void semaphore_thread_delete(const DrdThreadId threadid)
265{ }