1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
|
/*
This file is part of drd, a thread error detector.
Copyright (C) 2006-2015 Bart Van Assche <bvanassche@acm.org>.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#include "drd_clientobj.h"
#include "drd_error.h"
#include "drd_rwlock.h"
#include "pub_tool_vki.h"
#include "pub_tool_errormgr.h" // VG_(maybe_record_error)()
#include "pub_tool_libcassert.h" // tl_assert()
#include "pub_tool_libcprint.h" // VG_(message)()
#include "pub_tool_libcproc.h" // VG_(read_millisecond_timer)()
#include "pub_tool_machine.h" // VG_(get_IP)()
#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
/* Local type definitions. */
struct rwlock_thread_info
{
UWord tid; // DrdThreadId.
UInt reader_nesting_count;
UInt writer_nesting_count;
// Segment of last unlock call by this thread that unlocked a writer lock.
Segment* latest_wrlocked_segment;
// Segment of last unlock call by this thread that unlocked a reader lock.
Segment* latest_rdlocked_segment;
};
/* Local functions. */
static void rwlock_cleanup(struct rwlock_info* p);
static void rwlock_delete_thread(struct rwlock_info* const p,
const DrdThreadId tid);
/* Local variables. */
static Bool DRD_(s_trace_rwlock);
static UInt DRD_(s_exclusive_threshold_ms);
static UInt DRD_(s_shared_threshold_ms);
static ULong DRD_(s_rwlock_segment_creation_count);
/* Function definitions. */
void DRD_(rwlock_set_trace)(const Bool trace_rwlock)
{
tl_assert(trace_rwlock == False || trace_rwlock == True);
DRD_(s_trace_rwlock) = trace_rwlock;
}
void DRD_(rwlock_set_exclusive_threshold)(const UInt exclusive_threshold_ms)
{
DRD_(s_exclusive_threshold_ms) = exclusive_threshold_ms;
}
void DRD_(rwlock_set_shared_threshold)(const UInt shared_threshold_ms)
{
DRD_(s_shared_threshold_ms) = shared_threshold_ms;
}
static Bool DRD_(rwlock_is_rdlocked)(struct rwlock_info* p)
{
struct rwlock_thread_info* q;
VG_(OSetGen_ResetIter)(p->thread_info);
for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; )
{
return q->reader_nesting_count > 0;
}
return False;
}
static Bool DRD_(rwlock_is_wrlocked)(struct rwlock_info* p)
{
struct rwlock_thread_info* q;
VG_(OSetGen_ResetIter)(p->thread_info);
for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; )
{
return q->writer_nesting_count > 0;
}
return False;
}
static Bool DRD_(rwlock_is_locked)(struct rwlock_info* p)
{
return DRD_(rwlock_is_rdlocked)(p) || DRD_(rwlock_is_wrlocked)(p);
}
static Bool DRD_(rwlock_is_rdlocked_by)(struct rwlock_info* p,
const DrdThreadId tid)
{
const UWord uword_tid = tid;
struct rwlock_thread_info* q;
q = VG_(OSetGen_Lookup)(p->thread_info, &uword_tid);
return q && q->reader_nesting_count > 0;
}
static Bool DRD_(rwlock_is_wrlocked_by)(struct rwlock_info* p,
const DrdThreadId tid)
{
const UWord uword_tid = tid;
struct rwlock_thread_info* q;
q = VG_(OSetGen_Lookup)(p->thread_info, &uword_tid);
return q && q->writer_nesting_count > 0;
}
static Bool DRD_(rwlock_is_locked_by)(struct rwlock_info* p,
const DrdThreadId tid)
{
return (DRD_(rwlock_is_rdlocked_by)(p, tid)
|| DRD_(rwlock_is_wrlocked_by)(p, tid));
}
/** Either look up or insert a node corresponding to DRD thread id 'tid'. */
static
struct rwlock_thread_info*
DRD_(lookup_or_insert_node)(OSet* oset, const UWord tid)
{
struct rwlock_thread_info* q;
q = VG_(OSetGen_Lookup)(oset, &tid);
if (q == 0)
{
q = VG_(OSetGen_AllocNode)(oset, sizeof(*q));
q->tid = tid;
q->reader_nesting_count = 0;
q->writer_nesting_count = 0;
q->latest_wrlocked_segment = 0;
q->latest_rdlocked_segment = 0;
VG_(OSetGen_Insert)(oset, q);
}
tl_assert(q);
return q;
}
/**
* Combine the vector clock corresponding to the last unlock operation of
* reader-writer lock p into the vector clock of thread 'tid'.
*/
static void DRD_(rwlock_combine_other_vc)(struct rwlock_info* const p,
const DrdThreadId tid,
const Bool readers_too)
{
struct rwlock_thread_info* q;
VectorClock old_vc;
DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(tid));
VG_(OSetGen_ResetIter)(p->thread_info);
for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; ) {
if (q->tid != tid) {
if (q->latest_wrlocked_segment)
DRD_(vc_combine)(DRD_(thread_get_vc)(tid),
&q->latest_wrlocked_segment->vc);
if (readers_too && q->latest_rdlocked_segment)
DRD_(vc_combine)(DRD_(thread_get_vc)(tid),
&q->latest_rdlocked_segment->vc);
}
}
DRD_(thread_update_conflict_set)(tid, &old_vc);
DRD_(vc_cleanup)(&old_vc);
}
/**
* Compare the type of the rwlock specified at initialization time with
* the type passed as an argument, and complain if these two types do not
* match.
*/
static Bool drd_rwlock_check_type(struct rwlock_info* const p,
const RwLockT rwlock_type)
{
tl_assert(p);
/* The code below has to be updated if additional rwlock types are added. */
tl_assert(rwlock_type == pthread_rwlock || rwlock_type == user_rwlock);
tl_assert(p->rwlock_type == pthread_rwlock || p->rwlock_type == user_rwlock);
if (p->rwlock_type == rwlock_type)
return True;
{
RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
VG_(maybe_record_error)
(VG_(get_running_tid)(),
RwlockErr,
VG_(get_IP)(VG_(get_running_tid)()),
rwlock_type == pthread_rwlock
? "Attempt to use a user-defined rwlock as a POSIX rwlock"
: "Attempt to use a POSIX rwlock as a user-defined rwlock",
&REI);
}
return False;
}
/** Initialize the rwlock_info data structure *p. */
static
void DRD_(rwlock_initialize)(struct rwlock_info* const p, const Addr rwlock,
const RwLockT rwlock_type)
{
tl_assert(rwlock != 0);
tl_assert(p->a1 == rwlock);
tl_assert(p->type == ClientRwlock);
p->cleanup = (void(*)(DrdClientobj*))rwlock_cleanup;
p->delete_thread
= (void(*)(DrdClientobj*, DrdThreadId))rwlock_delete_thread;
p->rwlock_type = rwlock_type;
p->thread_info = VG_(OSetGen_Create)(
0, 0, VG_(malloc), "drd.rwlock.ri.1", VG_(free));
p->acquiry_time_ms = 0;
p->acquired_at = 0;
}
/** Deallocate the memory that was allocated by rwlock_initialize(). */
static void rwlock_cleanup(struct rwlock_info* p)
{
struct rwlock_thread_info* q;
tl_assert(p);
if (DRD_(s_trace_rwlock))
DRD_(trace_msg)("[%u] rwlock_destroy 0x%lx",
DRD_(thread_get_running_tid)(), p->a1);
if (DRD_(rwlock_is_locked)(p))
{
RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
VG_(maybe_record_error)(VG_(get_running_tid)(),
RwlockErr,
VG_(get_IP)(VG_(get_running_tid)()),
"Destroying locked rwlock",
&REI);
}
VG_(OSetGen_ResetIter)(p->thread_info);
for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; )
{
DRD_(sg_put)(q->latest_wrlocked_segment);
DRD_(sg_put)(q->latest_rdlocked_segment);
}
VG_(OSetGen_Destroy)(p->thread_info);
}
static
struct rwlock_info*
DRD_(rwlock_get_or_allocate)(const Addr rwlock, const RwLockT rwlock_type)
{
struct rwlock_info* p;
tl_assert(offsetof(DrdClientobj, rwlock) == 0);
p = &(DRD_(clientobj_get)(rwlock, ClientRwlock)->rwlock);
if (p)
{
drd_rwlock_check_type(p, rwlock_type);
return p;
}
if (DRD_(clientobj_present)(rwlock, rwlock + 1))
{
GenericErrInfo GEI = {
.tid = DRD_(thread_get_running_tid)(),
.addr = rwlock,
};
VG_(maybe_record_error)(VG_(get_running_tid)(),
GenericErr,
VG_(get_IP)(VG_(get_running_tid)()),
"Not a reader-writer lock",
&GEI);
return 0;
}
p = &(DRD_(clientobj_add)(rwlock, ClientRwlock)->rwlock);
DRD_(rwlock_initialize)(p, rwlock, rwlock_type);
return p;
}
static struct rwlock_info* DRD_(rwlock_get)(const Addr rwlock)
{
tl_assert(offsetof(DrdClientobj, rwlock) == 0);
return &(DRD_(clientobj_get)(rwlock, ClientRwlock)->rwlock);
}
/** Called before pthread_rwlock_init(). */
struct rwlock_info* DRD_(rwlock_pre_init)(const Addr rwlock,
const RwLockT rwlock_type)
{
struct rwlock_info* p;
if (DRD_(s_trace_rwlock))
DRD_(trace_msg)("[%u] rwlock_init 0x%lx",
DRD_(thread_get_running_tid)(), rwlock);
p = DRD_(rwlock_get)(rwlock);
if (p)
drd_rwlock_check_type(p, rwlock_type);
if (p)
{
const ThreadId vg_tid = VG_(get_running_tid)();
RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
VG_(maybe_record_error)(vg_tid,
RwlockErr,
VG_(get_IP)(vg_tid),
"Reader-writer lock reinitialization",
&REI);
return p;
}
p = DRD_(rwlock_get_or_allocate)(rwlock, rwlock_type);
return p;
}
/** Called after pthread_rwlock_destroy(). */
void DRD_(rwlock_post_destroy)(const Addr rwlock, const RwLockT rwlock_type)
{
struct rwlock_info* p;
p = DRD_(rwlock_get)(rwlock);
if (p == 0)
{
GenericErrInfo GEI = {
.tid = DRD_(thread_get_running_tid)(),
.addr = rwlock,
};
VG_(maybe_record_error)(VG_(get_running_tid)(),
GenericErr,
VG_(get_IP)(VG_(get_running_tid)()),
"Not a reader-writer lock",
&GEI);
return;
}
drd_rwlock_check_type(p, rwlock_type);
DRD_(clientobj_remove)(rwlock, ClientRwlock);
}
/**
* Called before pthread_rwlock_rdlock() is invoked. If a data structure for
* the client-side object was not yet created, do this now. Also check whether
* an attempt is made to lock recursively a synchronization object that must
* not be locked recursively.
*/
void DRD_(rwlock_pre_rdlock)(const Addr rwlock, const RwLockT rwlock_type)
{
struct rwlock_info* p;
if (DRD_(s_trace_rwlock))
DRD_(trace_msg)("[%u] pre_rwlock_rdlock 0x%lx",
DRD_(thread_get_running_tid)(), rwlock);
p = DRD_(rwlock_get_or_allocate)(rwlock, rwlock_type);
tl_assert(p);
if (DRD_(rwlock_is_wrlocked_by)(p, DRD_(thread_get_running_tid)())) {
RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
VG_(maybe_record_error)(VG_(get_running_tid)(),
RwlockErr,
VG_(get_IP)(VG_(get_running_tid)()),
"Already locked for writing by calling thread",
&REI);
}
}
/**
* Update rwlock_info state when locking the pthread_rwlock_t mutex.
* Note: this function must be called after pthread_rwlock_rdlock() has been
* called, or a race condition is triggered !
*/
void DRD_(rwlock_post_rdlock)(const Addr rwlock, const RwLockT rwlock_type,
const Bool took_lock)
{
const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
struct rwlock_info* p;
struct rwlock_thread_info* q;
if (DRD_(s_trace_rwlock))
DRD_(trace_msg)("[%u] post_rwlock_rdlock 0x%lx", drd_tid, rwlock);
p = DRD_(rwlock_get)(rwlock);
if (! p || ! took_lock)
return;
tl_assert(! DRD_(rwlock_is_wrlocked)(p));
q = DRD_(lookup_or_insert_node)(p->thread_info, drd_tid);
if (++q->reader_nesting_count == 1)
{
DRD_(thread_new_segment)(drd_tid);
DRD_(s_rwlock_segment_creation_count)++;
DRD_(rwlock_combine_other_vc)(p, drd_tid, False);
p->acquiry_time_ms = VG_(read_millisecond_timer)();
p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
}
}
/**
* Called before pthread_rwlock_wrlock() is invoked. If a data structure for
* the client-side object was not yet created, do this now. Also check whether
* an attempt is made to lock recursively a synchronization object that must
* not be locked recursively.
*/
void DRD_(rwlock_pre_wrlock)(const Addr rwlock, const RwLockT rwlock_type)
{
struct rwlock_info* p;
p = DRD_(rwlock_get)(rwlock);
if (DRD_(s_trace_rwlock))
DRD_(trace_msg)("[%u] pre_rwlock_wrlock 0x%lx",
DRD_(thread_get_running_tid)(), rwlock);
if (p == 0)
p = DRD_(rwlock_get_or_allocate)(rwlock, rwlock_type);
tl_assert(p);
if (DRD_(rwlock_is_wrlocked_by)(p, DRD_(thread_get_running_tid)()))
{
RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
VG_(maybe_record_error)(VG_(get_running_tid)(),
RwlockErr,
VG_(get_IP)(VG_(get_running_tid)()),
"Recursive writer locking not allowed",
&REI);
}
}
/**
* Update rwlock_info state when locking the pthread_rwlock_t rwlock.
* Note: this function must be called after pthread_rwlock_wrlock() has
* finished, or a race condition is triggered !
*/
void DRD_(rwlock_post_wrlock)(const Addr rwlock, const RwLockT rwlock_type,
const Bool took_lock)
{
const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
struct rwlock_info* p;
struct rwlock_thread_info* q;
p = DRD_(rwlock_get)(rwlock);
if (DRD_(s_trace_rwlock))
DRD_(trace_msg)("[%u] post_rwlock_wrlock 0x%lx", drd_tid, rwlock);
if (! p || ! took_lock)
return;
q = DRD_(lookup_or_insert_node)(p->thread_info,
DRD_(thread_get_running_tid)());
tl_assert(q->writer_nesting_count == 0);
q->writer_nesting_count++;
tl_assert(q->writer_nesting_count == 1);
DRD_(thread_new_segment)(drd_tid);
DRD_(s_rwlock_segment_creation_count)++;
DRD_(rwlock_combine_other_vc)(p, drd_tid, True);
p->acquiry_time_ms = VG_(read_millisecond_timer)();
p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
}
/**
* Update rwlock_info state when unlocking the pthread_rwlock_t rwlock.
*
* @param rwlock Pointer to pthread_rwlock_t data structure in the client space.
*
* @return New value of the rwlock recursion count.
*
* @note This function must be called before pthread_rwlock_unlock() is called,
* or a race condition is triggered !
*/
void DRD_(rwlock_pre_unlock)(const Addr rwlock, const RwLockT rwlock_type)
{
const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
const ThreadId vg_tid = VG_(get_running_tid)();
struct rwlock_info* p;
struct rwlock_thread_info* q;
if (DRD_(s_trace_rwlock))
DRD_(trace_msg)("[%u] rwlock_unlock 0x%lx", drd_tid, rwlock);
p = DRD_(rwlock_get)(rwlock);
if (p == 0)
{
GenericErrInfo GEI = {
.tid = DRD_(thread_get_running_tid)(),
.addr = rwlock,
};
VG_(maybe_record_error)(VG_(get_running_tid)(),
GenericErr,
VG_(get_IP)(VG_(get_running_tid)()),
"Not a reader-writer lock",
&GEI);
return;
}
drd_rwlock_check_type(p, rwlock_type);
if (! DRD_(rwlock_is_locked_by)(p, drd_tid))
{
RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
VG_(maybe_record_error)(vg_tid,
RwlockErr,
VG_(get_IP)(vg_tid),
"Reader-writer lock not locked by calling thread",
&REI);
return;
}
q = DRD_(lookup_or_insert_node)(p->thread_info, drd_tid);
tl_assert(q);
if (q->reader_nesting_count > 0)
{
q->reader_nesting_count--;
if (q->reader_nesting_count == 0 && DRD_(s_shared_threshold_ms) > 0)
{
Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
if (held > DRD_(s_shared_threshold_ms))
{
HoldtimeErrInfo HEI
= { DRD_(thread_get_running_tid)(),
rwlock, p->acquired_at, held, DRD_(s_shared_threshold_ms) };
VG_(maybe_record_error)(vg_tid,
HoldtimeErr,
VG_(get_IP)(vg_tid),
"rwlock",
&HEI);
}
}
if (q->reader_nesting_count == 0 && q->writer_nesting_count == 0)
{
/*
* This pthread_rwlock_unlock() call really unlocks the rwlock. Save
* the current vector clock of the thread such that it is available
* when this rwlock is locked again.
*/
DRD_(thread_get_latest_segment)(&q->latest_rdlocked_segment, drd_tid);
DRD_(thread_new_segment)(drd_tid);
DRD_(s_rwlock_segment_creation_count)++;
}
}
else if (q->writer_nesting_count > 0)
{
q->writer_nesting_count--;
if (q->writer_nesting_count == 0 && DRD_(s_exclusive_threshold_ms) > 0)
{
Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
if (held > DRD_(s_exclusive_threshold_ms))
{
HoldtimeErrInfo HEI
= { DRD_(thread_get_running_tid)(),
rwlock, p->acquired_at, held,
DRD_(s_exclusive_threshold_ms) };
VG_(maybe_record_error)(vg_tid,
HoldtimeErr,
VG_(get_IP)(vg_tid),
"rwlock",
&HEI);
}
}
if (q->reader_nesting_count == 0 && q->writer_nesting_count == 0)
{
/*
* This pthread_rwlock_unlock() call really unlocks the rwlock. Save
* the current vector clock of the thread such that it is available
* when this rwlock is locked again.
*/
DRD_(thread_get_latest_segment)(&q->latest_wrlocked_segment, drd_tid);
DRD_(thread_new_segment)(drd_tid);
DRD_(s_rwlock_segment_creation_count)++;
}
}
else
{
tl_assert(False);
}
}
/** Called when thread tid stops to exist. */
static void rwlock_delete_thread(struct rwlock_info* const p,
const DrdThreadId tid)
{
struct rwlock_thread_info* q;
if (DRD_(rwlock_is_locked_by)(p, tid))
{
RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
VG_(maybe_record_error)(VG_(get_running_tid)(),
RwlockErr,
VG_(get_IP)(VG_(get_running_tid)()),
"Reader-writer lock still locked at thread exit",
&REI);
q = DRD_(lookup_or_insert_node)(p->thread_info, tid);
q->reader_nesting_count = 0;
q->writer_nesting_count = 0;
}
}
ULong DRD_(get_rwlock_segment_creation_count)(void)
{
return DRD_(s_rwlock_segment_creation_count);
}
|