blob: 513dcd1b1cb01eb514c23dbacc3b46b00e14ca1b [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
75 * drd_clientobj_remove().
76 */
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);
99 p = &drd_clientobj_get(semaphore, ClientSemaphore)->semaphore;
100 if (p == 0)
sewardj85642922008-01-14 11:54:56 +0000101 {
bart28230a32008-02-29 17:27:03 +0000102 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
103 p = &drd_clientobj_add(semaphore, semaphore + size,
104 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
bart28230a32008-02-29 17:27:03 +0000110struct semaphore_info* semaphore_get(const Addr semaphore)
111{
112 tl_assert(offsetof(DrdClientobj, semaphore) == 0);
113 return &drd_clientobj_get(semaphore, ClientSemaphore)->semaphore;
114}
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(). */
sewardj85642922008-01-14 11:54:56 +0000137void semaphore_destroy(struct semaphore_info* const p)
138{
bart3b1ee452008-02-29 19:28:15 +0000139 tl_assert(p);
140
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(),
147 p->a1);
148 }
149
bart28230a32008-02-29 17:27:03 +0000150 drd_clientobj_remove(p->a1, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000151}
152
bart28230a32008-02-29 17:27:03 +0000153/** Called before sem_wait(). */
154void semaphore_pre_wait(const Addr semaphore, const SizeT size)
sewardj85642922008-01-14 11:54:56 +0000155{
156 struct semaphore_info* p;
157
158 p = semaphore_get_or_allocate(semaphore, size);
bart28230a32008-02-29 17:27:03 +0000159 if (s_trace_semaphore)
160 {
bart3b1ee452008-02-29 19:28:15 +0000161 VG_(message)(Vg_UserMsg,
162 "[%d/%d] semaphore_pre_wait 0x%lx",
163 VG_(get_running_tid)(),
164 thread_get_running_tid(),
165 semaphore);
bart28230a32008-02-29 17:27:03 +0000166 }
167 tl_assert(p);
168 tl_assert(p->waiters >= 0);
169 p->waiters++;
170 tl_assert(p->waiters > 0);
171}
172
173/** Called after sem_wait() finished.
174 * @note Do not rely on the value of 'waited' -- some glibc versions do
175 * not set it correctly.
176 */
177void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
178 const Bool waited)
179{
180 struct semaphore_info* p;
181
182 p = semaphore_get(semaphore);
183 if (s_trace_semaphore)
184 {
bart3b1ee452008-02-29 19:28:15 +0000185 VG_(message)(Vg_UserMsg,
186 "[%d/%d] semaphore_post_wait 0x%lx",
187 VG_(get_running_tid)(),
188 thread_get_running_tid(),
189 semaphore);
bart28230a32008-02-29 17:27:03 +0000190 }
191 tl_assert(p->waiters > 0);
192 p->waiters--;
193 tl_assert(p->waiters >= 0);
sewardj85642922008-01-14 11:54:56 +0000194 tl_assert(p->value >= 0);
bart28230a32008-02-29 17:27:03 +0000195 if (p->value == 0)
196 {
bart3b1ee452008-02-29 19:28:15 +0000197 SemaphoreErrInfo sei = { semaphore };
198 VG_(maybe_record_error)(VG_(get_running_tid)(),
199 SemaphoreErr,
200 VG_(get_IP)(VG_(get_running_tid)()),
201 "Invalid semaphore",
202 &sei);
bart28230a32008-02-29 17:27:03 +0000203 return;
204 }
sewardj85642922008-01-14 11:54:56 +0000205 p->value--;
206 tl_assert(p->value >= 0);
207 if (p->last_sem_post_tid != tid)
208 thread_combine_vc2(tid, &p->vc);
209 thread_new_segment(tid);
210}
211
212/** Called before sem_post(). */
213void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore,
214 const SizeT size)
215{
216 struct semaphore_info* p;
217
bart3b1ee452008-02-29 19:28:15 +0000218 if (s_trace_semaphore)
219 {
220 VG_(message)(Vg_UserMsg,
221 "[%d/%d] semaphore_post 0x%lx",
222 VG_(get_running_tid)(),
223 thread_get_running_tid(),
224 semaphore);
225 }
sewardj85642922008-01-14 11:54:56 +0000226 p = semaphore_get_or_allocate(semaphore, size);
227 p->value++;
228 if (p->value == 1)
229 {
230 p->last_sem_post_tid = tid;
sewardjc0be9252008-02-11 11:00:51 +0000231 thread_new_segment(tid);
bart9cdaf1e2008-02-24 18:29:43 +0000232 vc_assign(&p->vc, thread_get_vc(tid));
sewardj85642922008-01-14 11:54:56 +0000233 }
234}
235
236/** Called after sem_post() finished successfully. */
237void semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
sewardje3b57aa2008-01-18 07:42:01 +0000238 const SizeT size, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000239{
bart25896d92008-02-17 09:21:05 +0000240 /* Note: it is hard to implement the sem_post() wrapper correctly in */
241 /* case sem_post() returns an error code. This is because handling this */
242 /* case correctly requires restoring the vector clock associated with */
243 /* the semaphore to its original value here. In order to do that without */
244 /* introducing a race condition, extra locking has to be added around */
245 /* each semaphore call. Such extra locking would have to be added in */
246 /* drd_intercepts.c. However, it is hard to implement synchronization */
247 /* in drd_intercepts.c in a portable way without calling already */
248 /* redirected functions. */
sewardj85642922008-01-14 11:54:56 +0000249}
250
251void semaphore_thread_delete(const DrdThreadId threadid)
252{ }