drd_semaphore.c revision 9986c99e20f6f0acd5a3ce547a53a5df4f95f70f
1/* -*- mode: C; c-basic-offset: 3; -*- */ 2/* 3 This file is part of drd, a thread error detector. 4 5 Copyright (C) 2006-2009 Bart Van Assche <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 26#include "drd_clientobj.h" 27#include "drd_error.h" 28#include "drd_semaphore.h" 29#include "drd_suppression.h" 30#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_mallocfree.h" // VG_(malloc), VG_(free) 35#include "pub_tool_threadstate.h" // VG_(get_running_tid)() 36 37 38/* Local functions. */ 39 40static void semaphore_cleanup(struct semaphore_info* p); 41 42 43/* Local variables. */ 44 45static Bool s_trace_semaphore; 46static ULong s_semaphore_segment_creation_count; 47 48 49/* Function definitions. */ 50 51/** Push a segment at the end of the queue 'p->last_sem_post_seg'. */ 52static void DRD_(segment_push)(struct semaphore_info* p, Segment* sg) 53{ 54 Word n; 55 56 tl_assert(sg); 57 n = VG_(addToXA)(p->last_sem_post_seg, &sg); 58#if 0 59 VG_(message)(Vg_UserMsg, "0x%lx push: added at position %ld/%ld", 60 p->a1, n, VG_(sizeXA)(p->last_sem_post_seg)); 61#endif 62 tl_assert(*(Segment**)VG_(indexXA)(p->last_sem_post_seg, n) == sg); 63} 64 65/** Pop a segment from the beginning of the queue 'p->last_sem_post_seg'. */ 66static Segment* DRD_(segment_pop)(struct semaphore_info* p) 67{ 68 Word sz; 69 Segment* sg; 70 71 sz = VG_(sizeXA)(p->last_sem_post_seg); 72#if 0 73 VG_(message)(Vg_UserMsg, "0x%lx pop: removed from position %ld/%ld", 74 p->a1, sz - 1, sz); 75#endif 76 sg = 0; 77 if (sz > 0) 78 { 79 sg = *(Segment**)VG_(indexXA)(p->last_sem_post_seg, sz - 1); 80 tl_assert(sg); 81 VG_(dropTailXA)(p->last_sem_post_seg, 1); 82 } 83 return sg; 84} 85 86/** Enable or disable tracing of semaphore actions. */ 87void DRD_(semaphore_set_trace)(const Bool trace_semaphore) 88{ 89 s_trace_semaphore = trace_semaphore; 90} 91 92/** 93 * Initialize the memory 'p' points at as a semaphore_info structure for the 94 * client semaphore at client addres 'semaphore'. 95 */ 96static 97void DRD_(semaphore_initialize)(struct semaphore_info* const p, 98 const Addr semaphore) 99{ 100 tl_assert(semaphore != 0); 101 tl_assert(p->a1 == semaphore); 102 tl_assert(p->type == ClientSemaphore); 103 104 p->cleanup = (void(*)(DrdClientobj*))semaphore_cleanup; 105 p->delete_thread = 0; 106 p->waits_to_skip = 0; 107 p->value = 0; 108 p->waiters = 0; 109 p->last_sem_post_tid = DRD_INVALID_THREADID; 110 p->last_sem_post_seg = VG_(newXA)(VG_(malloc), "drd.sg-stack", 111 VG_(free), sizeof(Segment*)); 112} 113 114/** 115 * Free the memory that was allocated by semaphore_initialize(). Called by 116 * DRD_(clientobj_remove)(). 117 */ 118static void semaphore_cleanup(struct semaphore_info* p) 119{ 120 Segment* sg; 121 122 if (p->waiters > 0) 123 { 124 SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), p->a1 }; 125 VG_(maybe_record_error)(VG_(get_running_tid)(), 126 SemaphoreErr, 127 VG_(get_IP)(VG_(get_running_tid)()), 128 "Destruction of semaphore that is being waited" 129 " upon", 130 &sei); 131 } 132 while ((sg = DRD_(segment_pop)(p))) 133 DRD_(sg_put)(sg); 134 VG_(deleteXA)(p->last_sem_post_seg); 135} 136 137/** 138 * Return a pointer to the structure with information about the specified 139 * client semaphore. Allocate a new structure if such a structure did not 140 * yet exist. 141 */ 142static 143struct semaphore_info* 144DRD_(semaphore_get_or_allocate)(const Addr semaphore) 145{ 146 struct semaphore_info *p; 147 148 tl_assert(offsetof(DrdClientobj, semaphore) == 0); 149 p = &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore); 150 if (p == 0) 151 { 152 tl_assert(offsetof(DrdClientobj, semaphore) == 0); 153 p = &(DRD_(clientobj_add)(semaphore, ClientSemaphore)->semaphore); 154 DRD_(semaphore_initialize)(p, semaphore); 155 } 156 return p; 157} 158 159/** 160 * Return a pointer to the structure with information about the specified 161 * client semaphore, or null if no such structure was found. 162 */ 163static struct semaphore_info* semaphore_get(const Addr semaphore) 164{ 165 tl_assert(offsetof(DrdClientobj, semaphore) == 0); 166 return &(DRD_(clientobj_get)(semaphore, ClientSemaphore)->semaphore); 167} 168 169/** Called before sem_init(). */ 170struct semaphore_info* DRD_(semaphore_init)(const Addr semaphore, 171 const Word pshared, 172 const UInt value) 173{ 174 struct semaphore_info* p; 175 Segment* sg; 176 177 if (s_trace_semaphore) 178 { 179 VG_(message)(Vg_UserMsg, 180 "[%d] semaphore_init 0x%lx value %u\n", 181 DRD_(thread_get_running_tid)(), 182 semaphore, 183 value); 184 } 185 p = semaphore_get(semaphore); 186 if (p) 187 { 188 const ThreadId vg_tid = VG_(get_running_tid)(); 189 SemaphoreErrInfo SEI = { DRD_(thread_get_running_tid)(), semaphore }; 190 VG_(maybe_record_error)(vg_tid, 191 SemaphoreErr, 192 VG_(get_IP)(vg_tid), 193 "Semaphore reinitialization", 194 &SEI); 195 // Remove all segments from the segment stack. 196 while ((sg = DRD_(segment_pop)(p))) 197 { 198 DRD_(sg_put)(sg); 199 } 200 } 201 else 202 { 203 p = DRD_(semaphore_get_or_allocate)(semaphore); 204 } 205 tl_assert(p); 206 p->waits_to_skip = value; 207 p->value = value; 208 return p; 209} 210 211/** Called after sem_destroy(). */ 212void DRD_(semaphore_destroy)(const Addr semaphore) 213{ 214 struct semaphore_info* p; 215 216 p = semaphore_get(semaphore); 217 218 if (s_trace_semaphore) 219 { 220 VG_(message)(Vg_UserMsg, 221 "[%d] semaphore_destroy 0x%lx value %u\n", 222 DRD_(thread_get_running_tid)(), 223 semaphore, 224 p ? p->value : 0); 225 } 226 227 if (p == 0) 228 { 229 GenericErrInfo GEI = { DRD_(thread_get_running_tid)() }; 230 VG_(maybe_record_error)(VG_(get_running_tid)(), 231 GenericErr, 232 VG_(get_IP)(VG_(get_running_tid)()), 233 "Not a semaphore", 234 &GEI); 235 return; 236 } 237 238 DRD_(clientobj_remove)(semaphore, ClientSemaphore); 239} 240 241/** Called before sem_wait(). */ 242void DRD_(semaphore_pre_wait)(const Addr semaphore) 243{ 244 struct semaphore_info* p; 245 246 p = DRD_(semaphore_get_or_allocate)(semaphore); 247 tl_assert(p); 248 p->waiters++; 249 250 if ((Word)(p->waiters) <= 0) 251 { 252 SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore }; 253 VG_(maybe_record_error)(VG_(get_running_tid)(), 254 SemaphoreErr, 255 VG_(get_IP)(VG_(get_running_tid)()), 256 "Invalid semaphore", 257 &sei); 258 } 259} 260 261/** 262 * Called after sem_wait() finished. 263 * @note Do not rely on the value of 'waited' -- some glibc versions do 264 * not set it correctly. 265 */ 266void DRD_(semaphore_post_wait)(const DrdThreadId tid, const Addr semaphore, 267 const Bool waited) 268{ 269 struct semaphore_info* p; 270 Segment* sg; 271 272 p = semaphore_get(semaphore); 273 if (s_trace_semaphore) 274 { 275 VG_(message)(Vg_UserMsg, 276 "[%d] semaphore_wait 0x%lx value %u -> %u\n", 277 DRD_(thread_get_running_tid)(), 278 semaphore, 279 p ? p->value : 0, 280 p ? p->value - 1 : 0); 281 } 282 283 if (p) 284 { 285 p->waiters--; 286 p->value--; 287 } 288 289 /* 290 * Note: if another thread destroyed and reinitialized a semaphore while 291 * the current thread was waiting in sem_wait, p->waiters may have been 292 * set to zero by DRD_(semaphore_initialize)() after 293 * DRD_(semaphore_pre_wait)() has finished before 294 * DRD_(semaphore_post_wait)() has been called. 295 */ 296 if (p == NULL || (Int)(p->value) < 0 || (Word)(p->waiters) < 0) 297 { 298 SemaphoreErrInfo sei = { DRD_(thread_get_running_tid)(), semaphore }; 299 VG_(maybe_record_error)(VG_(get_running_tid)(), 300 SemaphoreErr, 301 VG_(get_IP)(VG_(get_running_tid)()), 302 "Invalid semaphore", 303 &sei); 304 return; 305 } 306 307 if (p->waits_to_skip > 0) 308 p->waits_to_skip--; 309 else 310 { 311 sg = DRD_(segment_pop)(p); 312 tl_assert(sg); 313 if (sg) 314 { 315 if (p->last_sem_post_tid != tid 316 && p->last_sem_post_tid != DRD_INVALID_THREADID) 317 { 318 DRD_(thread_new_segment_and_combine_vc)(tid, sg); 319 } 320 else 321 DRD_(thread_new_segment)(tid); 322 s_semaphore_segment_creation_count++; 323 DRD_(sg_put)(sg); 324 } 325 } 326} 327 328/** Called before sem_post(). */ 329void DRD_(semaphore_pre_post)(const DrdThreadId tid, const Addr semaphore) 330{ 331 struct semaphore_info* p; 332 Segment* sg; 333 334 p = DRD_(semaphore_get_or_allocate)(semaphore); 335 p->value++; 336 337 if (s_trace_semaphore) 338 { 339 VG_(message)(Vg_UserMsg, 340 "[%d] semaphore_post 0x%lx value %u -> %u\n", 341 DRD_(thread_get_running_tid)(), 342 semaphore, 343 p->value - 1, p->value); 344 } 345 346 p->last_sem_post_tid = tid; 347 sg = 0; 348 DRD_(thread_get_latest_segment)(&sg, tid); 349 tl_assert(sg); 350 DRD_(segment_push)(p, sg); 351 DRD_(thread_new_segment)(tid); 352 s_semaphore_segment_creation_count++; 353} 354 355/** Called after sem_post() finished. */ 356void DRD_(semaphore_post_post)(const DrdThreadId tid, const Addr semaphore, 357 const Bool succeeded) 358{ 359 /* 360 * Note: it is hard to implement the sem_post() wrapper correctly in 361 * case sem_post() returns an error code. This is because handling this 362 * case correctly requires restoring the vector clock associated with 363 * the semaphore to its original value here. In order to do that without 364 * introducing a race condition, extra locking has to be added around 365 * each semaphore call. Such extra locking would have to be added in 366 * drd_pthread_intercepts.c. However, it is hard to implement 367 * synchronization in drd_pthread_intercepts.c in a portable way without 368 * calling already redirected functions. 369 */ 370} 371 372ULong DRD_(get_semaphore_segment_creation_count)(void) 373{ 374 return s_semaphore_segment_creation_count; 375} 376