blob: d3d0d17696b0b9f23abfeb3a004dce21bca91893 [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,
bart4a975e12008-03-30 13:28:33 +0000120 "[%d/%d] semaphore_init 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000121 VG_(get_running_tid)(),
122 thread_get_running_tid(),
123 semaphore);
124 }
bartd9e39ec2008-06-28 15:03:26 +0000125 p = semaphore_get(semaphore);
126 if (p)
bart0268dfa2008-03-11 20:10:21 +0000127 {
bartd9e39ec2008-06-28 15:03:26 +0000128 const ThreadId vg_tid = VG_(get_running_tid)();
129 SemaphoreErrInfo SEI = { semaphore };
130 VG_(maybe_record_error)(vg_tid,
131 SemaphoreErr,
132 VG_(get_IP)(vg_tid),
133 "Semaphore reinitialization",
134 &SEI);
bart0268dfa2008-03-11 20:10:21 +0000135 }
bartd9e39ec2008-06-28 15:03:26 +0000136 else
137 {
138 p = semaphore_get_or_allocate(semaphore);
139 }
140 tl_assert(p);
sewardj85642922008-01-14 11:54:56 +0000141 p->value = value;
142 return p;
143}
144
bart28230a32008-02-29 17:27:03 +0000145/** Called after sem_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000146void semaphore_destroy(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000147{
bart72b751c2008-03-01 13:44:24 +0000148 struct semaphore_info* p;
bart3b1ee452008-02-29 19:28:15 +0000149
150 if (s_trace_semaphore)
151 {
152 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000153 "[%d/%d] semaphore_destroy 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000154 VG_(get_running_tid)(),
155 thread_get_running_tid(),
bart72b751c2008-03-01 13:44:24 +0000156 semaphore);
bart3b1ee452008-02-29 19:28:15 +0000157 }
158
bart72b751c2008-03-01 13:44:24 +0000159 p = semaphore_get(semaphore);
160
161 if (p == 0)
162 {
163 GenericErrInfo GEI;
164 VG_(maybe_record_error)(VG_(get_running_tid)(),
165 GenericErr,
166 VG_(get_IP)(VG_(get_running_tid)()),
167 "Not a semaphore",
168 &GEI);
169 return;
170 }
171
172 clientobj_remove(semaphore, ClientSemaphore);
sewardj85642922008-01-14 11:54:56 +0000173}
174
bart28230a32008-02-29 17:27:03 +0000175/** Called before sem_wait(). */
bart0268dfa2008-03-11 20:10:21 +0000176void semaphore_pre_wait(const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000177{
178 struct semaphore_info* p;
179
bart0268dfa2008-03-11 20:10:21 +0000180 p = semaphore_get_or_allocate(semaphore);
bart28230a32008-02-29 17:27:03 +0000181 if (s_trace_semaphore)
182 {
bart3b1ee452008-02-29 19:28:15 +0000183 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000184 "[%d/%d] semaphore_pre_wait 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000185 VG_(get_running_tid)(),
186 thread_get_running_tid(),
187 semaphore);
bart28230a32008-02-29 17:27:03 +0000188 }
189 tl_assert(p);
bart74a5f212008-05-11 06:43:07 +0000190 tl_assert((int)p->waiters >= 0);
bart28230a32008-02-29 17:27:03 +0000191 p->waiters++;
192 tl_assert(p->waiters > 0);
193}
194
195/** Called after sem_wait() finished.
196 * @note Do not rely on the value of 'waited' -- some glibc versions do
197 * not set it correctly.
198 */
199void semaphore_post_wait(const DrdThreadId tid, const Addr semaphore,
200 const Bool waited)
201{
202 struct semaphore_info* p;
203
204 p = semaphore_get(semaphore);
205 if (s_trace_semaphore)
206 {
bart3b1ee452008-02-29 19:28:15 +0000207 VG_(message)(Vg_UserMsg,
208 "[%d/%d] semaphore_post_wait 0x%lx",
209 VG_(get_running_tid)(),
210 thread_get_running_tid(),
211 semaphore);
bart28230a32008-02-29 17:27:03 +0000212 }
213 tl_assert(p->waiters > 0);
214 p->waiters--;
bart74a5f212008-05-11 06:43:07 +0000215 tl_assert((int)p->waiters >= 0);
216 tl_assert((int)p->value >= 0);
bart28230a32008-02-29 17:27:03 +0000217 if (p->value == 0)
218 {
bart3b1ee452008-02-29 19:28:15 +0000219 SemaphoreErrInfo sei = { semaphore };
220 VG_(maybe_record_error)(VG_(get_running_tid)(),
221 SemaphoreErr,
222 VG_(get_IP)(VG_(get_running_tid)()),
223 "Invalid semaphore",
224 &sei);
bart28230a32008-02-29 17:27:03 +0000225 return;
226 }
sewardj85642922008-01-14 11:54:56 +0000227 p->value--;
bart74a5f212008-05-11 06:43:07 +0000228 tl_assert((int)p->value >= 0);
barta2b6e1b2008-03-17 18:32:39 +0000229 if (p->last_sem_post_tid != tid
230 && p->last_sem_post_tid != DRD_INVALID_THREADID)
231 {
232 tl_assert(p->last_sem_post_segment);
233 thread_combine_vc2(tid, &p->last_sem_post_segment->vc);
234 }
sewardj85642922008-01-14 11:54:56 +0000235 thread_new_segment(tid);
bart6bbefaf2008-04-19 15:16:45 +0000236 s_semaphore_segment_creation_count++;
sewardj85642922008-01-14 11:54:56 +0000237}
238
239/** Called before sem_post(). */
bart0268dfa2008-03-11 20:10:21 +0000240void semaphore_pre_post(const DrdThreadId tid, const Addr semaphore)
sewardj85642922008-01-14 11:54:56 +0000241{
242 struct semaphore_info* p;
243
bart3b1ee452008-02-29 19:28:15 +0000244 if (s_trace_semaphore)
245 {
246 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000247 "[%d/%d] semaphore_post 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000248 VG_(get_running_tid)(),
249 thread_get_running_tid(),
250 semaphore);
251 }
bart0268dfa2008-03-11 20:10:21 +0000252 p = semaphore_get_or_allocate(semaphore);
sewardj85642922008-01-14 11:54:56 +0000253 p->value++;
254 if (p->value == 1)
255 {
256 p->last_sem_post_tid = tid;
sewardjc0be9252008-02-11 11:00:51 +0000257 thread_new_segment(tid);
barta2b6e1b2008-03-17 18:32:39 +0000258 thread_get_latest_segment(&p->last_sem_post_segment, tid);
bart6bbefaf2008-04-19 15:16:45 +0000259 s_semaphore_segment_creation_count++;
sewardj85642922008-01-14 11:54:56 +0000260 }
261}
262
263/** Called after sem_post() finished successfully. */
264void semaphore_post_post(const DrdThreadId tid, const Addr semaphore,
bart0268dfa2008-03-11 20:10:21 +0000265 const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000266{
bart25896d92008-02-17 09:21:05 +0000267 /* Note: it is hard to implement the sem_post() wrapper correctly in */
268 /* case sem_post() returns an error code. This is because handling this */
269 /* case correctly requires restoring the vector clock associated with */
270 /* the semaphore to its original value here. In order to do that without */
271 /* introducing a race condition, extra locking has to be added around */
272 /* each semaphore call. Such extra locking would have to be added in */
273 /* drd_intercepts.c. However, it is hard to implement synchronization */
274 /* in drd_intercepts.c in a portable way without calling already */
275 /* redirected functions. */
sewardj85642922008-01-14 11:54:56 +0000276}
277
278void semaphore_thread_delete(const DrdThreadId threadid)
279{ }
bart6bbefaf2008-04-19 15:16:45 +0000280
281ULong get_semaphore_segment_creation_count(void)
282{
283 return s_semaphore_segment_creation_count;
284}