2 /*--------------------------------------------------------------------*/
3 /*--- Helgrind: a Valgrind tool for detecting errors ---*/
4 /*--- in threaded programs. hg_main.c ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Helgrind, a Valgrind tool for detecting errors
11 Copyright (C) 2007-2013 OpenWorks LLP
14 Copyright (C) 2007-2013 Apple, Inc.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation; either version 2 of the
19 License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31 The GNU General Public License is contained in the file COPYING.
33 Neither the names of the U.S. Department of Energy nor the
34 University of California nor the names of its contributors may be
35 used to endorse or promote products derived from this software
36 without prior written permission.
39 #include "pub_tool_basics.h"
40 #include "pub_tool_gdbserver.h"
41 #include "pub_tool_libcassert.h"
42 #include "pub_tool_libcbase.h"
43 #include "pub_tool_libcprint.h"
44 #include "pub_tool_threadstate.h"
45 #include "pub_tool_tooliface.h"
46 #include "pub_tool_hashtable.h"
47 #include "pub_tool_replacemalloc.h"
48 #include "pub_tool_machine.h"
49 #include "pub_tool_options.h"
50 #include "pub_tool_xarray.h"
51 #include "pub_tool_stacktrace.h"
52 #include "pub_tool_wordfm.h"
53 #include "pub_tool_debuginfo.h" // VG_(find_seginfo), VG_(seginfo_soname)
54 #include "pub_tool_redir.h" // sonames for the dynamic linkers
55 #include "pub_tool_vki.h" // VKI_PAGE_SIZE
56 #include "pub_tool_libcproc.h"
57 #include "pub_tool_aspacemgr.h" // VG_(am_is_valid_for_client)
58 #include "pub_tool_poolalloc.h"
59 #include "pub_tool_addrinfo.h"
61 #include "hg_basics.h"
62 #include "hg_wordset.h"
63 #include "hg_addrdescr.h"
64 #include "hg_lock_n_thread.h"
65 #include "hg_errors.h"
72 // FIXME: new_mem_w_tid ignores the supplied tid. (wtf?!)
74 // FIXME: when client destroys a lock or a CV, remove these
75 // from our mappings, so that the associated SO can be freed up
77 /*----------------------------------------------------------------*/
79 /*----------------------------------------------------------------*/
81 /* Note this needs to be compiled with -fno-strict-aliasing, since it
82 contains a whole bunch of calls to lookupFM etc which cast between
83 Word and pointer types. gcc rightly complains this breaks ANSI C
84 strict aliasing rules, at -O2. No complaints at -O, but -O2 gives
85 worthwhile performance benefits over -O.
88 // FIXME what is supposed to happen to locks in memory which
89 // is relocated as a result of client realloc?
91 // FIXME put referencing ThreadId into Thread and get
92 // rid of the slow reverse mapping function.
94 // FIXME accesses to NoAccess areas: change state to Excl?
96 // FIXME report errors for accesses of NoAccess memory?
98 // FIXME pth_cond_wait/timedwait wrappers. Even if these fail,
99 // the thread still holds the lock.
101 /* ------------ Debug/trace options ------------ */
103 // 0 for silent, 1 for some stuff, 2 for lots of stuff
104 #define SHOW_EVENTS 0
107 static void all__sanity_check ( const HChar
* who
); /* fwds */
109 #define HG_CLI__DEFAULT_MALLOC_REDZONE_SZB 16 /* let's say */
111 // 0 for none, 1 for dump at end of run
112 #define SHOW_DATA_STRUCTURES 0
115 /* ------------ Misc comments ------------ */
117 // FIXME: don't hardwire initial entries for root thread.
118 // Instead, let the pre_thread_ll_create handler do this.
121 /*----------------------------------------------------------------*/
122 /*--- Primary data structures ---*/
123 /*----------------------------------------------------------------*/
125 /* Admin linked list of Threads */
126 static Thread
* admin_threads
= NULL
;
127 Thread
* get_admin_threads ( void ) { return admin_threads
; }
129 /* Admin double linked list of Locks */
130 /* We need a double linked list to properly and efficiently
132 static Lock
* admin_locks
= NULL
;
134 /* Mapping table for core ThreadIds to Thread* */
135 static Thread
** map_threads
= NULL
; /* Array[VG_N_THREADS] of Thread* */
137 /* Mapping table for lock guest addresses to Lock* */
138 static WordFM
* map_locks
= NULL
; /* WordFM LockAddr Lock* */
140 /* The word-set universes for lock sets. */
141 static WordSetU
* univ_lsets
= NULL
; /* sets of Lock* */
142 static WordSetU
* univ_laog
= NULL
; /* sets of Lock*, for LAOG */
143 static Int next_gc_univ_laog
= 1;
144 /* univ_laog will be garbaged collected when the nr of element in univ_laog is
145 >= next_gc_univ_laog. */
147 /* Allow libhb to get at the universe of locksets stored
149 WordSetU
* HG_(get_univ_lsets
) ( void ) { return univ_lsets
; }
151 /* Allow libhb to get at the list of locks stored here. Ditto
153 Lock
* HG_(get_admin_locks
) ( void ) { return admin_locks
; }
156 /*----------------------------------------------------------------*/
157 /*--- Simple helpers for the data structures ---*/
158 /*----------------------------------------------------------------*/
160 static UWord stats__lockN_acquires
= 0;
161 static UWord stats__lockN_releases
= 0;
163 #if defined(VGO_solaris)
164 Bool
HG_(clo_ignore_thread_creation
) = True
;
166 Bool
HG_(clo_ignore_thread_creation
) = False
;
167 #endif /* VGO_solaris */
170 ThreadId
map_threads_maybe_reverse_lookup_SLOW ( Thread
* thr
); /*fwds*/
172 /* --------- Constructors --------- */
174 static Thread
* mk_Thread ( Thr
* hbthr
) {
176 Thread
* thread
= HG_(zalloc
)( "hg.mk_Thread.1", sizeof(Thread
) );
177 thread
->locksetA
= HG_(emptyWS
)( univ_lsets
);
178 thread
->locksetW
= HG_(emptyWS
)( univ_lsets
);
179 thread
->magic
= Thread_MAGIC
;
180 thread
->hbthr
= hbthr
;
181 thread
->coretid
= VG_INVALID_THREADID
;
182 thread
->created_at
= NULL
;
183 thread
->announced
= False
;
184 thread
->errmsg_index
= indx
++;
185 thread
->admin
= admin_threads
;
186 thread
->synchr_nesting
= 0;
187 thread
->pthread_create_nesting_level
= 0;
188 #if defined(VGO_solaris)
189 thread
->bind_guard_flag
= 0;
190 #endif /* VGO_solaris */
192 admin_threads
= thread
;
196 // Make a new lock which is unlocked (hence ownerless)
197 // and insert the new lock in admin_locks double linked list.
198 static Lock
* mk_LockN ( LockKind kind
, Addr guestaddr
) {
199 static ULong unique
= 0;
200 Lock
* lock
= HG_(zalloc
)( "hg.mk_Lock.1", sizeof(Lock
) );
201 /* begin: add to double linked list */
203 admin_locks
->admin_prev
= lock
;
204 lock
->admin_next
= admin_locks
;
205 lock
->admin_prev
= NULL
;
208 lock
->unique
= unique
++;
209 lock
->magic
= LockN_MAGIC
;
210 lock
->appeared_at
= NULL
;
211 lock
->acquired_at
= NULL
;
212 lock
->hbso
= libhb_so_alloc();
213 lock
->guestaddr
= guestaddr
;
217 tl_assert(HG_(is_sane_LockN
)(lock
));
221 /* Release storage for a Lock. Also release storage in .heldBy, if
222 any. Removes from admin_locks double linked list. */
223 static void del_LockN ( Lock
* lk
)
225 tl_assert(HG_(is_sane_LockN
)(lk
));
227 libhb_so_dealloc(lk
->hbso
);
229 VG_(deleteBag
)( lk
->heldBy
);
230 /* begin: del lock from double linked list */
231 if (lk
== admin_locks
) {
232 tl_assert(lk
->admin_prev
== NULL
);
234 lk
->admin_next
->admin_prev
= NULL
;
235 admin_locks
= lk
->admin_next
;
238 tl_assert(lk
->admin_prev
!= NULL
);
239 lk
->admin_prev
->admin_next
= lk
->admin_next
;
241 lk
->admin_next
->admin_prev
= lk
->admin_prev
;
244 VG_(memset
)(lk
, 0xAA, sizeof(*lk
));
248 /* Update 'lk' to reflect that 'thr' now has a write-acquisition of
249 it. This is done strictly: only combinations resulting from
250 correct program and libpthread behaviour are allowed. */
251 static void lockN_acquire_writer ( Lock
* lk
, Thread
* thr
)
253 tl_assert(HG_(is_sane_LockN
)(lk
));
254 tl_assert(HG_(is_sane_Thread
)(thr
));
256 stats__lockN_acquires
++;
258 /* EXPOSITION only */
259 /* We need to keep recording snapshots of where the lock was
260 acquired, so as to produce better lock-order error messages. */
261 if (lk
->acquired_at
== NULL
) {
263 tl_assert(lk
->heldBy
== NULL
);
264 tid
= map_threads_maybe_reverse_lookup_SLOW(thr
);
266 = VG_(record_ExeContext
)(tid
, 0/*first_ip_delta*/);
268 tl_assert(lk
->heldBy
!= NULL
);
270 /* end EXPOSITION only */
275 tl_assert(lk
->heldBy
== NULL
); /* can't w-lock recursively */
276 tl_assert(!lk
->heldW
);
278 lk
->heldBy
= VG_(newBag
)( HG_(zalloc
), "hg.lNaw.1", HG_(free
) );
279 VG_(addToBag
)( lk
->heldBy
, (UWord
)thr
);
282 if (lk
->heldBy
== NULL
)
284 /* 2nd and subsequent locking of a lock by its owner */
285 tl_assert(lk
->heldW
);
286 /* assert: lk is only held by one thread .. */
287 tl_assert(VG_(sizeUniqueBag(lk
->heldBy
)) == 1);
288 /* assert: .. and that thread is 'thr'. */
289 tl_assert(VG_(elemBag
)(lk
->heldBy
, (UWord
)thr
)
290 == VG_(sizeTotalBag
)(lk
->heldBy
));
291 VG_(addToBag
)(lk
->heldBy
, (UWord
)thr
);
294 tl_assert(lk
->heldBy
== NULL
&& !lk
->heldW
); /* must be unheld */
299 tl_assert(HG_(is_sane_LockN
)(lk
));
302 static void lockN_acquire_reader ( Lock
* lk
, Thread
* thr
)
304 tl_assert(HG_(is_sane_LockN
)(lk
));
305 tl_assert(HG_(is_sane_Thread
)(thr
));
306 /* can only add reader to a reader-writer lock. */
307 tl_assert(lk
->kind
== LK_rdwr
);
308 /* lk must be free or already r-held. */
309 tl_assert(lk
->heldBy
== NULL
310 || (lk
->heldBy
!= NULL
&& !lk
->heldW
));
312 stats__lockN_acquires
++;
314 /* EXPOSITION only */
315 /* We need to keep recording snapshots of where the lock was
316 acquired, so as to produce better lock-order error messages. */
317 if (lk
->acquired_at
== NULL
) {
319 tl_assert(lk
->heldBy
== NULL
);
320 tid
= map_threads_maybe_reverse_lookup_SLOW(thr
);
322 = VG_(record_ExeContext
)(tid
, 0/*first_ip_delta*/);
324 tl_assert(lk
->heldBy
!= NULL
);
326 /* end EXPOSITION only */
329 VG_(addToBag
)(lk
->heldBy
, (UWord
)thr
);
332 lk
->heldBy
= VG_(newBag
)( HG_(zalloc
), "hg.lNar.1", HG_(free
) );
333 VG_(addToBag
)( lk
->heldBy
, (UWord
)thr
);
335 tl_assert(!lk
->heldW
);
336 tl_assert(HG_(is_sane_LockN
)(lk
));
339 /* Update 'lk' to reflect a release of it by 'thr'. This is done
340 strictly: only combinations resulting from correct program and
341 libpthread behaviour are allowed. */
343 static void lockN_release ( Lock
* lk
, Thread
* thr
)
346 tl_assert(HG_(is_sane_LockN
)(lk
));
347 tl_assert(HG_(is_sane_Thread
)(thr
));
348 /* lock must be held by someone */
349 tl_assert(lk
->heldBy
);
350 stats__lockN_releases
++;
351 /* Remove it from the holder set */
352 b
= VG_(delFromBag
)(lk
->heldBy
, (UWord
)thr
);
353 /* thr must actually have been a holder of lk */
356 tl_assert(lk
->acquired_at
);
357 if (VG_(isEmptyBag
)(lk
->heldBy
)) {
358 VG_(deleteBag
)(lk
->heldBy
);
361 lk
->acquired_at
= NULL
;
363 tl_assert(HG_(is_sane_LockN
)(lk
));
366 static void remove_Lock_from_locksets_of_all_owning_Threads( Lock
* lk
)
370 tl_assert(!lk
->heldW
);
373 /* for each thread that holds this lock do ... */
374 VG_(initIterBag
)( lk
->heldBy
);
375 while (VG_(nextIterBag
)( lk
->heldBy
, (UWord
*)&thr
, NULL
)) {
376 tl_assert(HG_(is_sane_Thread
)(thr
));
377 tl_assert(HG_(elemWS
)( univ_lsets
,
378 thr
->locksetA
, (UWord
)lk
));
380 = HG_(delFromWS
)( univ_lsets
, thr
->locksetA
, (UWord
)lk
);
383 tl_assert(HG_(elemWS
)( univ_lsets
,
384 thr
->locksetW
, (UWord
)lk
));
386 = HG_(delFromWS
)( univ_lsets
, thr
->locksetW
, (UWord
)lk
);
389 VG_(doneIterBag
)( lk
->heldBy
);
393 /*----------------------------------------------------------------*/
394 /*--- Print out the primary data structures ---*/
395 /*----------------------------------------------------------------*/
397 #define PP_THREADS (1<<1)
398 #define PP_LOCKS (1<<2)
399 #define PP_ALL (PP_THREADS | PP_LOCKS)
402 static const Int sHOW_ADMIN
= 0;
404 static void space ( Int n
)
408 tl_assert(n
>= 0 && n
< 128);
411 for (i
= 0; i
< n
; i
++)
414 tl_assert(i
< 128+1);
415 VG_(printf
)("%s", spaces
);
418 static void pp_Thread ( Int d
, Thread
* t
)
420 space(d
+0); VG_(printf
)("Thread %p {\n", t
);
422 space(d
+3); VG_(printf
)("admin %p\n", t
->admin
);
423 space(d
+3); VG_(printf
)("magic 0x%x\n", (UInt
)t
->magic
);
425 space(d
+3); VG_(printf
)("locksetA %d\n", (Int
)t
->locksetA
);
426 space(d
+3); VG_(printf
)("locksetW %d\n", (Int
)t
->locksetW
);
427 space(d
+0); VG_(printf
)("}\n");
430 static void pp_admin_threads ( Int d
)
434 for (n
= 0, t
= admin_threads
; t
; n
++, t
= t
->admin
) {
437 space(d
); VG_(printf
)("admin_threads (%d records) {\n", n
);
438 for (i
= 0, t
= admin_threads
; t
; i
++, t
= t
->admin
) {
441 VG_(printf
)("admin_threads record %d of %d:\n", i
, n
);
445 space(d
); VG_(printf
)("}\n");
448 static void pp_map_threads ( Int d
)
451 space(d
); VG_(printf
)("map_threads ");
452 for (i
= 0; i
< VG_N_THREADS
; i
++) {
453 if (map_threads
[i
] != NULL
)
456 VG_(printf
)("(%d entries) {\n", n
);
457 for (i
= 0; i
< VG_N_THREADS
; i
++) {
458 if (map_threads
[i
] == NULL
)
461 VG_(printf
)("coretid %d -> Thread %p\n", i
, map_threads
[i
]);
463 space(d
); VG_(printf
)("}\n");
466 static const HChar
* show_LockKind ( LockKind lkk
) {
468 case LK_mbRec
: return "mbRec";
469 case LK_nonRec
: return "nonRec";
470 case LK_rdwr
: return "rdwr";
471 default: tl_assert(0);
475 /* Pretty Print lock lk.
476 if show_lock_addrdescr, describes the (guest) lock address.
477 (this description will be more complete with --read-var-info=yes).
478 if show_internal_data, shows also helgrind internal information.
479 d is the level at which output is indented. */
480 static void pp_Lock ( Int d
, Lock
* lk
,
481 Bool show_lock_addrdescr
,
482 Bool show_internal_data
)
485 if (show_internal_data
)
486 VG_(printf
)("Lock %p (ga %#lx) {\n", lk
, lk
->guestaddr
);
488 VG_(printf
)("Lock ga %#lx {\n", lk
->guestaddr
);
489 if (!show_lock_addrdescr
490 || !HG_(get_and_pp_addrdescr
) ((Addr
) lk
->guestaddr
))
494 space(d
+3); VG_(printf
)("admin_n %p\n", lk
->admin_next
);
495 space(d
+3); VG_(printf
)("admin_p %p\n", lk
->admin_prev
);
496 space(d
+3); VG_(printf
)("magic 0x%x\n", (UInt
)lk
->magic
);
498 if (show_internal_data
) {
499 space(d
+3); VG_(printf
)("unique %llu\n", lk
->unique
);
501 space(d
+3); VG_(printf
)("kind %s\n", show_LockKind(lk
->kind
));
502 if (show_internal_data
) {
503 space(d
+3); VG_(printf
)("heldW %s\n", lk
->heldW
? "yes" : "no");
505 if (show_internal_data
) {
506 space(d
+3); VG_(printf
)("heldBy %p", lk
->heldBy
);
512 VG_(initIterBag
)( lk
->heldBy
);
513 while (VG_(nextIterBag
)( lk
->heldBy
, (UWord
*)&thr
, &count
)) {
514 if (show_internal_data
)
515 VG_(printf
)("%lu:%p ", count
, thr
);
517 VG_(printf
)("%c%lu:thread #%d ",
518 lk
->heldW
? 'W' : 'R',
519 count
, thr
->errmsg_index
);
520 if (thr
->coretid
== VG_INVALID_THREADID
)
521 VG_(printf
)("tid (exited) ");
523 VG_(printf
)("tid %d ", thr
->coretid
);
527 VG_(doneIterBag
)( lk
->heldBy
);
530 space(d
+0); VG_(printf
)("}\n");
533 static void pp_admin_locks ( Int d
)
537 for (n
= 0, lk
= admin_locks
; lk
; n
++, lk
= lk
->admin_next
) {
540 space(d
); VG_(printf
)("admin_locks (%d records) {\n", n
);
541 for (i
= 0, lk
= admin_locks
; lk
; i
++, lk
= lk
->admin_next
) {
544 VG_(printf
)("admin_locks record %d of %d:\n", i
, n
);
547 False
/* show_lock_addrdescr */,
548 True
/* show_internal_data */);
550 space(d
); VG_(printf
)("}\n");
553 static void pp_map_locks ( Int d
)
557 space(d
); VG_(printf
)("map_locks (%d entries) {\n",
558 (Int
)VG_(sizeFM
)( map_locks
));
559 VG_(initIterFM
)( map_locks
);
560 while (VG_(nextIterFM
)( map_locks
, (UWord
*)&gla
,
563 VG_(printf
)("guest %p -> Lock %p\n", gla
, lk
);
565 VG_(doneIterFM
)( map_locks
);
566 space(d
); VG_(printf
)("}\n");
569 static void pp_everything ( Int flags
, const HChar
* caller
)
573 VG_(printf
)("All_Data_Structures (caller = \"%s\") {\n", caller
);
574 if (flags
& PP_THREADS
) {
576 pp_admin_threads(d
+3);
580 if (flags
& PP_LOCKS
) {
595 /*----------------------------------------------------------------*/
596 /*--- Initialise the primary data structures ---*/
597 /*----------------------------------------------------------------*/
599 static void initialise_data_structures ( Thr
* hbthr_root
)
604 /* Get everything initialised and zeroed. */
605 tl_assert(admin_threads
== NULL
);
606 tl_assert(admin_locks
== NULL
);
608 tl_assert(map_threads
== NULL
);
609 map_threads
= HG_(zalloc
)( "hg.ids.1", VG_N_THREADS
* sizeof(Thread
*) );
611 tl_assert(sizeof(Addr
) == sizeof(UWord
));
612 tl_assert(map_locks
== NULL
);
613 map_locks
= VG_(newFM
)( HG_(zalloc
), "hg.ids.2", HG_(free
),
614 NULL
/*unboxed Word cmp*/);
616 tl_assert(univ_lsets
== NULL
);
617 univ_lsets
= HG_(newWordSetU
)( HG_(zalloc
), "hg.ids.4", HG_(free
),
619 tl_assert(univ_lsets
!= NULL
);
620 /* Ensure that univ_lsets is non-empty, with lockset zero being the
621 empty lockset. hg_errors.c relies on the assumption that
622 lockset number zero in univ_lsets is always valid. */
623 wsid
= HG_(emptyWS
)(univ_lsets
);
624 tl_assert(wsid
== 0);
626 tl_assert(univ_laog
== NULL
);
627 if (HG_(clo_track_lockorders
)) {
628 univ_laog
= HG_(newWordSetU
)( HG_(zalloc
), "hg.ids.5 (univ_laog)",
629 HG_(free
), 24/*cacheSize*/ );
630 tl_assert(univ_laog
!= NULL
);
633 /* Set up entries for the root thread */
634 // FIXME: this assumes that the first real ThreadId is 1
636 /* a Thread for the new thread ... */
637 thr
= mk_Thread(hbthr_root
);
638 thr
->coretid
= 1; /* FIXME: hardwires an assumption about the
639 identity of the root thread. */
640 tl_assert( libhb_get_Thr_hgthread(hbthr_root
) == NULL
);
641 libhb_set_Thr_hgthread(hbthr_root
, thr
);
643 /* and bind it in the thread-map table. */
644 tl_assert(HG_(is_sane_ThreadId
)(thr
->coretid
));
645 tl_assert(thr
->coretid
!= VG_INVALID_THREADID
);
647 map_threads
[thr
->coretid
] = thr
;
649 tl_assert(VG_INVALID_THREADID
== 0);
651 all__sanity_check("initialise_data_structures");
655 /*----------------------------------------------------------------*/
656 /*--- map_threads :: array[core-ThreadId] of Thread* ---*/
657 /*----------------------------------------------------------------*/
659 /* Doesn't assert if the relevant map_threads entry is NULL. */
660 static Thread
* map_threads_maybe_lookup ( ThreadId coretid
)
663 tl_assert( HG_(is_sane_ThreadId
)(coretid
) );
664 thr
= map_threads
[coretid
];
668 /* Asserts if the relevant map_threads entry is NULL. */
669 static inline Thread
* map_threads_lookup ( ThreadId coretid
)
672 tl_assert( HG_(is_sane_ThreadId
)(coretid
) );
673 thr
= map_threads
[coretid
];
678 /* Do a reverse lookup. Does not assert if 'thr' is not found in
680 static ThreadId
map_threads_maybe_reverse_lookup_SLOW ( Thread
* thr
)
683 tl_assert(HG_(is_sane_Thread
)(thr
));
684 /* Check nobody used the invalid-threadid slot */
685 tl_assert(VG_INVALID_THREADID
>= 0 && VG_INVALID_THREADID
< VG_N_THREADS
);
686 tl_assert(map_threads
[VG_INVALID_THREADID
] == NULL
);
688 tl_assert(HG_(is_sane_ThreadId
)(tid
));
692 /* Do a reverse lookup. Warning: POTENTIALLY SLOW. Asserts if 'thr'
693 is not found in map_threads. */
694 static ThreadId
map_threads_reverse_lookup_SLOW ( Thread
* thr
)
696 ThreadId tid
= map_threads_maybe_reverse_lookup_SLOW( thr
);
697 tl_assert(tid
!= VG_INVALID_THREADID
);
698 tl_assert(map_threads
[tid
]);
699 tl_assert(map_threads
[tid
]->coretid
== tid
);
703 static void map_threads_delete ( ThreadId coretid
)
706 tl_assert(coretid
!= 0);
707 tl_assert( HG_(is_sane_ThreadId
)(coretid
) );
708 thr
= map_threads
[coretid
];
710 map_threads
[coretid
] = NULL
;
713 static void HG_(thread_enter_synchr
)(Thread
*thr
) {
714 tl_assert(thr
->synchr_nesting
>= 0);
715 #if defined(VGO_solaris)
716 thr
->synchr_nesting
+= 1;
717 #endif /* VGO_solaris */
720 static void HG_(thread_leave_synchr
)(Thread
*thr
) {
721 #if defined(VGO_solaris)
722 thr
->synchr_nesting
-= 1;
723 #endif /* VGO_solaris */
724 tl_assert(thr
->synchr_nesting
>= 0);
727 static void HG_(thread_enter_pthread_create
)(Thread
*thr
) {
728 tl_assert(thr
->pthread_create_nesting_level
>= 0);
729 thr
->pthread_create_nesting_level
+= 1;
732 static void HG_(thread_leave_pthread_create
)(Thread
*thr
) {
733 tl_assert(thr
->pthread_create_nesting_level
> 0);
734 thr
->pthread_create_nesting_level
-= 1;
737 static Int
HG_(get_pthread_create_nesting_level
)(ThreadId tid
) {
738 Thread
*thr
= map_threads_maybe_lookup(tid
);
739 return thr
->pthread_create_nesting_level
;
742 /*----------------------------------------------------------------*/
743 /*--- map_locks :: WordFM guest-Addr-of-lock Lock* ---*/
744 /*----------------------------------------------------------------*/
746 /* Make sure there is a lock table entry for the given (lock) guest
747 address. If not, create one of the stated 'kind' in unheld state.
748 In any case, return the address of the existing or new Lock. */
750 Lock
* map_locks_lookup_or_create ( LockKind lkk
, Addr ga
, ThreadId tid
)
753 Lock
* oldlock
= NULL
;
754 tl_assert(HG_(is_sane_ThreadId
)(tid
));
755 found
= VG_(lookupFM
)( map_locks
,
756 NULL
, (UWord
*)&oldlock
, (UWord
)ga
);
758 Lock
* lock
= mk_LockN(lkk
, ga
);
759 lock
->appeared_at
= VG_(record_ExeContext
)( tid
, 0 );
760 tl_assert(HG_(is_sane_LockN
)(lock
));
761 VG_(addToFM
)( map_locks
, (UWord
)ga
, (UWord
)lock
);
762 tl_assert(oldlock
== NULL
);
765 tl_assert(oldlock
!= NULL
);
766 tl_assert(HG_(is_sane_LockN
)(oldlock
));
767 tl_assert(oldlock
->guestaddr
== ga
);
772 static Lock
* map_locks_maybe_lookup ( Addr ga
)
776 found
= VG_(lookupFM
)( map_locks
, NULL
, (UWord
*)&lk
, (UWord
)ga
);
777 tl_assert(found
? lk
!= NULL
: lk
== NULL
);
781 static void map_locks_delete ( Addr ga
)
785 VG_(delFromFM
)( map_locks
,
786 (UWord
*)&ga2
, (UWord
*)&lk
, (UWord
)ga
);
787 /* delFromFM produces the val which is being deleted, if it is
788 found. So assert it is non-null; that in effect asserts that we
789 are deleting a (ga, Lock) pair which actually exists. */
790 tl_assert(lk
!= NULL
);
791 tl_assert(ga2
== ga
);
796 /*----------------------------------------------------------------*/
797 /*--- Sanity checking the data structures ---*/
798 /*----------------------------------------------------------------*/
800 static UWord stats__sanity_checks
= 0;
802 static void laog__sanity_check ( const HChar
* who
); /* fwds */
804 /* REQUIRED INVARIANTS:
806 Thread vs Segment/Lock/SecMaps
808 for each t in Threads {
810 // Thread.lockset: each element is really a valid Lock
812 // Thread.lockset: each Lock in set is actually held by that thread
813 for lk in Thread.lockset
816 // Thread.csegid is a valid SegmentID
817 // and the associated Segment has .thr == t
821 all thread Locksets are pairwise empty under intersection
822 (that is, no lock is claimed to be held by more than one thread)
823 -- this is guaranteed if all locks in locksets point back to their
826 Lock vs Thread/Segment/SecMaps
828 for each entry (gla, la) in map_locks
829 gla == la->guest_addr
831 for each lk in Locks {
834 lk->guest_addr does not have shadow state NoAccess
835 if lk == LockedBy(t), then t->lockset contains lk
836 if lk == UnlockedBy(segid) then segid is valid SegmentID
837 and can be mapped to a valid Segment(seg)
838 and seg->thr->lockset does not contain lk
839 if lk == UnlockedNew then (no lockset contains lk)
841 secmaps for lk has .mbHasLocks == True
845 Segment vs Thread/Lock/SecMaps
847 the Segment graph is a dag (no cycles)
848 all of the Segment graph must be reachable from the segids
849 mentioned in the Threads
851 for seg in Segments {
853 seg->thr is a sane Thread
857 SecMaps vs Segment/Thread/Lock
862 if any shadow word is ShR or ShM then .mbHasShared == True
864 for each Excl(segid) state
865 map_segments_lookup maps to a sane Segment(seg)
866 for each ShM/ShR(tsetid,lsetid) state
867 each lk in lset is a valid Lock
868 each thr in tset is a valid thread, which is non-dead
874 /* Return True iff 'thr' holds 'lk' in some mode. */
875 static Bool
thread_is_a_holder_of_Lock ( Thread
* thr
, Lock
* lk
)
878 return VG_(elemBag
)( lk
->heldBy
, (UWord
)thr
) > 0;
883 /* Sanity check Threads, as far as possible */
884 __attribute__((noinline
))
885 static void threads__sanity_check ( const HChar
* who
)
887 #define BAD(_str) do { how = (_str); goto bad; } while (0)
888 const HChar
* how
= "no error";
894 for (thr
= admin_threads
; thr
; thr
= thr
->admin
) {
895 if (!HG_(is_sane_Thread
)(thr
)) BAD("1");
898 // locks held in W mode are a subset of all locks held
899 if (!HG_(isSubsetOf
)( univ_lsets
, wsW
, wsA
)) BAD("7");
900 HG_(getPayloadWS
)( &ls_words
, &ls_size
, univ_lsets
, wsA
);
901 for (i
= 0; i
< ls_size
; i
++) {
902 lk
= (Lock
*)ls_words
[i
];
903 // Thread.lockset: each element is really a valid Lock
904 if (!HG_(is_sane_LockN
)(lk
)) BAD("2");
905 // Thread.lockset: each Lock in set is actually held by that
907 if (!thread_is_a_holder_of_Lock(thr
,lk
)) BAD("3");
912 VG_(printf
)("threads__sanity_check: who=\"%s\", bad=\"%s\"\n", who
, how
);
918 /* Sanity check Locks, as far as possible */
919 __attribute__((noinline
))
920 static void locks__sanity_check ( const HChar
* who
)
922 #define BAD(_str) do { how = (_str); goto bad; } while (0)
923 const HChar
* how
= "no error";
927 // # entries in admin_locks == # entries in map_locks
928 for (i
= 0, lk
= admin_locks
; lk
; i
++, lk
= lk
->admin_next
)
930 if (i
!= VG_(sizeFM
)(map_locks
)) BAD("1");
931 // for each entry (gla, lk) in map_locks
932 // gla == lk->guest_addr
933 VG_(initIterFM
)( map_locks
);
934 while (VG_(nextIterFM
)( map_locks
,
935 (UWord
*)&gla
, (UWord
*)&lk
)) {
936 if (lk
->guestaddr
!= gla
) BAD("2");
938 VG_(doneIterFM
)( map_locks
);
939 // scan through admin_locks ...
940 for (lk
= admin_locks
; lk
; lk
= lk
->admin_next
) {
941 // lock is sane. Quite comprehensive, also checks that
942 // referenced (holder) threads are sane.
943 if (!HG_(is_sane_LockN
)(lk
)) BAD("3");
944 // map_locks binds guest address back to this lock
945 if (lk
!= map_locks_maybe_lookup(lk
->guestaddr
)) BAD("4");
946 // look at all threads mentioned as holders of this lock. Ensure
947 // this lock is mentioned in their locksets.
951 VG_(initIterBag
)( lk
->heldBy
);
952 while (VG_(nextIterBag
)( lk
->heldBy
,
953 (UWord
*)&thr
, &count
)) {
954 // HG_(is_sane_LockN) above ensures these
955 tl_assert(count
>= 1);
956 tl_assert(HG_(is_sane_Thread
)(thr
));
957 if (!HG_(elemWS
)(univ_lsets
, thr
->locksetA
, (UWord
)lk
))
959 // also check the w-only lockset
961 && !HG_(elemWS
)(univ_lsets
, thr
->locksetW
, (UWord
)lk
))
964 && HG_(elemWS
)(univ_lsets
, thr
->locksetW
, (UWord
)lk
))
967 VG_(doneIterBag
)( lk
->heldBy
);
969 /* lock not held by anybody */
970 if (lk
->heldW
) BAD("9"); /* should be False if !heldBy */
971 // since lk is unheld, then (no lockset contains lk)
972 // hmm, this is really too expensive to check. Hmm.
978 VG_(printf
)("locks__sanity_check: who=\"%s\", bad=\"%s\"\n", who
, how
);
984 static void all_except_Locks__sanity_check ( const HChar
* who
) {
985 stats__sanity_checks
++;
986 if (0) VG_(printf
)("all_except_Locks__sanity_check(%s)\n", who
);
987 threads__sanity_check(who
);
988 if (HG_(clo_track_lockorders
))
989 laog__sanity_check(who
);
991 static void all__sanity_check ( const HChar
* who
) {
992 all_except_Locks__sanity_check(who
);
993 locks__sanity_check(who
);
997 /*----------------------------------------------------------------*/
998 /*--- Shadow value and address range handlers ---*/
999 /*----------------------------------------------------------------*/
1001 static void laog__pre_thread_acquires_lock ( Thread
*, Lock
* ); /* fwds */
1002 //static void laog__handle_lock_deletions ( WordSetID ); /* fwds */
1003 static inline Thread
* get_current_Thread ( void ); /* fwds */
1004 __attribute__((noinline
))
1005 static void laog__handle_one_lock_deletion ( Lock
* lk
); /* fwds */
1008 /* Block-copy states (needed for implementing realloc()). */
1009 /* FIXME this copies shadow memory; it doesn't apply the MSM to it.
1010 Is that a problem? (hence 'scopy' rather than 'ccopy') */
1011 static void shadow_mem_scopy_range ( Thread
* thr
,
1012 Addr src
, Addr dst
, SizeT len
)
1014 Thr
* hbthr
= thr
->hbthr
;
1016 libhb_copy_shadow_state( hbthr
, src
, dst
, len
);
1019 static void shadow_mem_cread_range ( Thread
* thr
, Addr a
, SizeT len
)
1021 Thr
* hbthr
= thr
->hbthr
;
1023 LIBHB_CREAD_N(hbthr
, a
, len
);
1026 static void shadow_mem_cwrite_range ( Thread
* thr
, Addr a
, SizeT len
) {
1027 Thr
* hbthr
= thr
->hbthr
;
1029 LIBHB_CWRITE_N(hbthr
, a
, len
);
1032 static void shadow_mem_make_New ( Thread
* thr
, Addr a
, SizeT len
)
1034 libhb_srange_new( thr
->hbthr
, a
, len
);
1037 static void shadow_mem_make_NoAccess_NoFX ( Thread
* thr
, Addr aIN
, SizeT len
)
1040 VG_(printf
)("make NoAccess_NoFX ( %#lx, %ld )\n", aIN
, len
);
1041 // has no effect (NoFX)
1042 libhb_srange_noaccess_NoFX( thr
->hbthr
, aIN
, len
);
1045 static void shadow_mem_make_NoAccess_AHAE ( Thread
* thr
, Addr aIN
, SizeT len
)
1048 VG_(printf
)("make NoAccess_AHAE ( %#lx, %ld )\n", aIN
, len
);
1049 // Actually Has An Effect (AHAE)
1050 libhb_srange_noaccess_AHAE( thr
->hbthr
, aIN
, len
);
1053 static void shadow_mem_make_Untracked ( Thread
* thr
, Addr aIN
, SizeT len
)
1056 VG_(printf
)("make Untracked ( %#lx, %ld )\n", aIN
, len
);
1057 libhb_srange_untrack( thr
->hbthr
, aIN
, len
);
1061 /*----------------------------------------------------------------*/
1062 /*--- Event handlers (evh__* functions) ---*/
1063 /*--- plus helpers (evhH__* functions) ---*/
1064 /*----------------------------------------------------------------*/
1066 /*--------- Event handler helpers (evhH__* functions) ---------*/
1068 /* Create a new segment for 'thr', making it depend (.prev) on its
1069 existing segment, bind together the SegmentID and Segment, and
1070 return both of them. Also update 'thr' so it references the new
1073 //zz void evhH__start_new_segment_for_thread ( /*OUT*/SegmentID* new_segidP,
1074 //zz /*OUT*/Segment** new_segP,
1077 //zz Segment* cur_seg;
1078 //zz tl_assert(new_segP);
1079 //zz tl_assert(new_segidP);
1080 //zz tl_assert(HG_(is_sane_Thread)(thr));
1081 //zz cur_seg = map_segments_lookup( thr->csegid );
1082 //zz tl_assert(cur_seg);
1083 //zz tl_assert(cur_seg->thr == thr); /* all sane segs should point back
1084 //zz at their owner thread. */
1085 //zz *new_segP = mk_Segment( thr, cur_seg, NULL/*other*/ );
1086 //zz *new_segidP = alloc_SegmentID();
1087 //zz map_segments_add( *new_segidP, *new_segP );
1088 //zz thr->csegid = *new_segidP;
1092 /* The lock at 'lock_ga' has acquired a writer. Make all necessary
1093 updates, and also do all possible error checks. */
1095 void evhH__post_thread_w_acquires_lock ( Thread
* thr
,
1096 LockKind lkk
, Addr lock_ga
)
1100 /* Basically what we need to do is call lockN_acquire_writer.
1101 However, that will barf if any 'invalid' lock states would
1102 result. Therefore check before calling. Side effect is that
1103 'HG_(is_sane_LockN)(lk)' is both a pre- and post-condition of this
1106 Because this routine is only called after successful lock
1107 acquisition, we should not be asked to move the lock into any
1108 invalid states. Requests to do so are bugs in libpthread, since
1109 that should have rejected any such requests. */
1111 tl_assert(HG_(is_sane_Thread
)(thr
));
1112 /* Try to find the lock. If we can't, then create a new one with
1114 lk
= map_locks_lookup_or_create(
1115 lkk
, lock_ga
, map_threads_reverse_lookup_SLOW(thr
) );
1116 tl_assert( HG_(is_sane_LockN
)(lk
) );
1118 /* check libhb level entities exist */
1119 tl_assert(thr
->hbthr
);
1120 tl_assert(lk
->hbso
);
1122 if (lk
->heldBy
== NULL
) {
1123 /* the lock isn't held. Simple. */
1124 tl_assert(!lk
->heldW
);
1125 lockN_acquire_writer( lk
, thr
);
1126 /* acquire a dependency from the lock's VCs */
1127 libhb_so_recv( thr
->hbthr
, lk
->hbso
, True
/*strong_recv*/ );
1131 /* So the lock is already held. If held as a r-lock then
1132 libpthread must be buggy. */
1133 tl_assert(lk
->heldBy
);
1135 HG_(record_error_Misc
)(
1136 thr
, "Bug in libpthread: write lock "
1137 "granted on rwlock which is currently rd-held");
1141 /* So the lock is held in w-mode. If it's held by some other
1142 thread, then libpthread must be buggy. */
1143 tl_assert(VG_(sizeUniqueBag
)(lk
->heldBy
) == 1); /* from precondition */
1145 if (thr
!= (Thread
*)VG_(anyElementOfBag
)(lk
->heldBy
)) {
1146 HG_(record_error_Misc
)(
1147 thr
, "Bug in libpthread: write lock "
1148 "granted on mutex/rwlock which is currently "
1149 "wr-held by a different thread");
1153 /* So the lock is already held in w-mode by 'thr'. That means this
1154 is an attempt to lock it recursively, which is only allowable
1155 for LK_mbRec kinded locks. Since this routine is called only
1156 once the lock has been acquired, this must also be a libpthread
1158 if (lk
->kind
!= LK_mbRec
) {
1159 HG_(record_error_Misc
)(
1160 thr
, "Bug in libpthread: recursive write lock "
1161 "granted on mutex/wrlock which does not "
1162 "support recursion");
1166 /* So we are recursively re-locking a lock we already w-hold. */
1167 lockN_acquire_writer( lk
, thr
);
1168 /* acquire a dependency from the lock's VC. Probably pointless,
1169 but also harmless. */
1170 libhb_so_recv( thr
->hbthr
, lk
->hbso
, True
/*strong_recv*/ );
1174 if (HG_(clo_track_lockorders
)) {
1175 /* check lock order acquisition graph, and update. This has to
1176 happen before the lock is added to the thread's locksetA/W. */
1177 laog__pre_thread_acquires_lock( thr
, lk
);
1179 /* update the thread's held-locks set */
1180 thr
->locksetA
= HG_(addToWS
)( univ_lsets
, thr
->locksetA
, (UWord
)lk
);
1181 thr
->locksetW
= HG_(addToWS
)( univ_lsets
, thr
->locksetW
, (UWord
)lk
);
1185 tl_assert(HG_(is_sane_LockN
)(lk
));
1189 /* The lock at 'lock_ga' has acquired a reader. Make all necessary
1190 updates, and also do all possible error checks. */
1192 void evhH__post_thread_r_acquires_lock ( Thread
* thr
,
1193 LockKind lkk
, Addr lock_ga
)
1197 /* Basically what we need to do is call lockN_acquire_reader.
1198 However, that will barf if any 'invalid' lock states would
1199 result. Therefore check before calling. Side effect is that
1200 'HG_(is_sane_LockN)(lk)' is both a pre- and post-condition of this
1203 Because this routine is only called after successful lock
1204 acquisition, we should not be asked to move the lock into any
1205 invalid states. Requests to do so are bugs in libpthread, since
1206 that should have rejected any such requests. */
1208 tl_assert(HG_(is_sane_Thread
)(thr
));
1209 /* Try to find the lock. If we can't, then create a new one with
1210 kind 'lkk'. Only a reader-writer lock can be read-locked,
1211 hence the first assertion. */
1212 tl_assert(lkk
== LK_rdwr
);
1213 lk
= map_locks_lookup_or_create(
1214 lkk
, lock_ga
, map_threads_reverse_lookup_SLOW(thr
) );
1215 tl_assert( HG_(is_sane_LockN
)(lk
) );
1217 /* check libhb level entities exist */
1218 tl_assert(thr
->hbthr
);
1219 tl_assert(lk
->hbso
);
1221 if (lk
->heldBy
== NULL
) {
1222 /* the lock isn't held. Simple. */
1223 tl_assert(!lk
->heldW
);
1224 lockN_acquire_reader( lk
, thr
);
1225 /* acquire a dependency from the lock's VC */
1226 libhb_so_recv( thr
->hbthr
, lk
->hbso
, False
/*!strong_recv*/ );
1230 /* So the lock is already held. If held as a w-lock then
1231 libpthread must be buggy. */
1232 tl_assert(lk
->heldBy
);
1234 HG_(record_error_Misc
)( thr
, "Bug in libpthread: read lock "
1235 "granted on rwlock which is "
1236 "currently wr-held");
1240 /* Easy enough. In short anybody can get a read-lock on a rwlock
1241 provided it is either unlocked or already in rd-held. */
1242 lockN_acquire_reader( lk
, thr
);
1243 /* acquire a dependency from the lock's VC. Probably pointless,
1244 but also harmless. */
1245 libhb_so_recv( thr
->hbthr
, lk
->hbso
, False
/*!strong_recv*/ );
1249 if (HG_(clo_track_lockorders
)) {
1250 /* check lock order acquisition graph, and update. This has to
1251 happen before the lock is added to the thread's locksetA/W. */
1252 laog__pre_thread_acquires_lock( thr
, lk
);
1254 /* update the thread's held-locks set */
1255 thr
->locksetA
= HG_(addToWS
)( univ_lsets
, thr
->locksetA
, (UWord
)lk
);
1256 /* but don't update thr->locksetW, since lk is only rd-held */
1260 tl_assert(HG_(is_sane_LockN
)(lk
));
1264 /* The lock at 'lock_ga' is just about to be unlocked. Make all
1265 necessary updates, and also do all possible error checks. */
1267 void evhH__pre_thread_releases_lock ( Thread
* thr
,
1268 Addr lock_ga
, Bool isRDWR
)
1274 /* This routine is called prior to a lock release, before
1275 libpthread has had a chance to validate the call. Hence we need
1276 to detect and reject any attempts to move the lock into an
1277 invalid state. Such attempts are bugs in the client.
1279 isRDWR is True if we know from the wrapper context that lock_ga
1280 should refer to a reader-writer lock, and is False if [ditto]
1281 lock_ga should refer to a standard mutex. */
1283 tl_assert(HG_(is_sane_Thread
)(thr
));
1284 lock
= map_locks_maybe_lookup( lock_ga
);
1287 /* We know nothing about a lock at 'lock_ga'. Nevertheless
1288 the client is trying to unlock it. So complain, then ignore
1290 HG_(record_error_UnlockBogus
)( thr
, lock_ga
);
1294 tl_assert(lock
->guestaddr
== lock_ga
);
1295 tl_assert(HG_(is_sane_LockN
)(lock
));
1297 if (isRDWR
&& lock
->kind
!= LK_rdwr
) {
1298 HG_(record_error_Misc
)( thr
, "pthread_rwlock_unlock with a "
1299 "pthread_mutex_t* argument " );
1301 if ((!isRDWR
) && lock
->kind
== LK_rdwr
) {
1302 HG_(record_error_Misc
)( thr
, "pthread_mutex_unlock with a "
1303 "pthread_rwlock_t* argument " );
1306 if (!lock
->heldBy
) {
1307 /* The lock is not held. This indicates a serious bug in the
1309 tl_assert(!lock
->heldW
);
1310 HG_(record_error_UnlockUnlocked
)( thr
, lock
);
1311 tl_assert(!HG_(elemWS
)( univ_lsets
, thr
->locksetA
, (UWord
)lock
));
1312 tl_assert(!HG_(elemWS
)( univ_lsets
, thr
->locksetW
, (UWord
)lock
));
1316 /* test just above dominates */
1317 tl_assert(lock
->heldBy
);
1318 was_heldW
= lock
->heldW
;
1320 /* The lock is held. Is this thread one of the holders? If not,
1321 report a bug in the client. */
1322 n
= VG_(elemBag
)( lock
->heldBy
, (UWord
)thr
);
1325 /* We are not a current holder of the lock. This is a bug in
1326 the guest, and (per POSIX pthread rules) the unlock
1327 attempt will fail. So just complain and do nothing
1329 Thread
* realOwner
= (Thread
*)VG_(anyElementOfBag
)( lock
->heldBy
);
1330 tl_assert(HG_(is_sane_Thread
)(realOwner
));
1331 tl_assert(realOwner
!= thr
);
1332 tl_assert(!HG_(elemWS
)( univ_lsets
, thr
->locksetA
, (UWord
)lock
));
1333 tl_assert(!HG_(elemWS
)( univ_lsets
, thr
->locksetW
, (UWord
)lock
));
1334 HG_(record_error_UnlockForeign
)( thr
, realOwner
, lock
);
1338 /* Ok, we hold the lock 'n' times. */
1341 lockN_release( lock
, thr
);
1347 tl_assert(lock
->heldBy
);
1348 tl_assert(n
== VG_(elemBag
)( lock
->heldBy
, (UWord
)thr
));
1349 /* We still hold the lock. So either it's a recursive lock
1350 or a rwlock which is currently r-held. */
1351 tl_assert(lock
->kind
== LK_mbRec
1352 || (lock
->kind
== LK_rdwr
&& !lock
->heldW
));
1353 tl_assert(HG_(elemWS
)( univ_lsets
, thr
->locksetA
, (UWord
)lock
));
1355 tl_assert(HG_(elemWS
)( univ_lsets
, thr
->locksetW
, (UWord
)lock
));
1357 tl_assert(!HG_(elemWS
)( univ_lsets
, thr
->locksetW
, (UWord
)lock
));
1359 /* n is zero. This means we don't hold the lock any more. But
1360 if it's a rwlock held in r-mode, someone else could still
1361 hold it. Just do whatever sanity checks we can. */
1362 if (lock
->kind
== LK_rdwr
&& lock
->heldBy
) {
1363 /* It's a rwlock. We no longer hold it but we used to;
1364 nevertheless it still appears to be held by someone else.
1365 The implication is that, prior to this release, it must
1366 have been shared by us and and whoever else is holding it;
1367 which in turn implies it must be r-held, since a lock
1368 can't be w-held by more than one thread. */
1369 /* The lock is now R-held by somebody else: */
1370 tl_assert(lock
->heldW
== False
);
1372 /* Normal case. It's either not a rwlock, or it's a rwlock
1373 that we used to hold in w-mode (which is pretty much the
1374 same thing as a non-rwlock.) Since this transaction is
1375 atomic (V does not allow multiple threads to run
1376 simultaneously), it must mean the lock is now not held by
1377 anybody. Hence assert for it. */
1378 /* The lock is now not held by anybody: */
1379 tl_assert(!lock
->heldBy
);
1380 tl_assert(lock
->heldW
== False
);
1382 //if (lock->heldBy) {
1383 // tl_assert(0 == VG_(elemBag)( lock->heldBy, (UWord)thr ));
1385 /* update this thread's lockset accordingly. */
1387 = HG_(delFromWS
)( univ_lsets
, thr
->locksetA
, (UWord
)lock
);
1389 = HG_(delFromWS
)( univ_lsets
, thr
->locksetW
, (UWord
)lock
);
1390 /* push our VC into the lock */
1391 tl_assert(thr
->hbthr
);
1392 tl_assert(lock
->hbso
);
1393 /* If the lock was previously W-held, then we want to do a
1394 strong send, and if previously R-held, then a weak send. */
1395 libhb_so_send( thr
->hbthr
, lock
->hbso
, was_heldW
);
1400 tl_assert(HG_(is_sane_LockN
)(lock
));
1404 /* ---------------------------------------------------------- */
1405 /* -------- Event handlers proper (evh__* functions) -------- */
1406 /* ---------------------------------------------------------- */
1408 /* What is the Thread* for the currently running thread? This is
1409 absolutely performance critical. We receive notifications from the
1410 core for client code starts/stops, and cache the looked-up result
1411 in 'current_Thread'. Hence, for the vast majority of requests,
1412 finding the current thread reduces to a read of a global variable,
1413 provided get_current_Thread_in_C_C is inlined.
1415 Outside of client code, current_Thread is NULL, and presumably
1416 any uses of it will cause a segfault. Hence:
1418 - for uses definitely within client code, use
1419 get_current_Thread_in_C_C.
1421 - for all other uses, use get_current_Thread.
1424 static Thread
*current_Thread
= NULL
,
1425 *current_Thread_prev
= NULL
;
1427 static void evh__start_client_code ( ThreadId tid
, ULong nDisp
) {
1428 if (0) VG_(printf
)("start %d %llu\n", (Int
)tid
, nDisp
);
1429 tl_assert(current_Thread
== NULL
);
1430 current_Thread
= map_threads_lookup( tid
);
1431 tl_assert(current_Thread
!= NULL
);
1432 if (current_Thread
!= current_Thread_prev
) {
1433 libhb_Thr_resumes( current_Thread
->hbthr
);
1434 current_Thread_prev
= current_Thread
;
1437 static void evh__stop_client_code ( ThreadId tid
, ULong nDisp
) {
1438 if (0) VG_(printf
)(" stop %d %llu\n", (Int
)tid
, nDisp
);
1439 tl_assert(current_Thread
!= NULL
);
1440 current_Thread
= NULL
;
1443 static inline Thread
* get_current_Thread_in_C_C ( void ) {
1444 return current_Thread
;
1446 static inline Thread
* get_current_Thread ( void ) {
1449 thr
= get_current_Thread_in_C_C();
1452 /* evidently not in client code. Do it the slow way. */
1453 coretid
= VG_(get_running_tid
)();
1454 /* FIXME: get rid of the following kludge. It exists because
1455 evh__new_mem is called during initialisation (as notification
1456 of initial memory layout) and VG_(get_running_tid)() returns
1457 VG_INVALID_THREADID at that point. */
1458 if (coretid
== VG_INVALID_THREADID
)
1459 coretid
= 1; /* KLUDGE */
1460 thr
= map_threads_lookup( coretid
);
1465 void evh__new_mem ( Addr a
, SizeT len
) {
1466 Thread
*thr
= get_current_Thread();
1467 if (SHOW_EVENTS
>= 2)
1468 VG_(printf
)("evh__new_mem(%p, %lu)\n", (void*)a
, len
);
1469 shadow_mem_make_New( thr
, a
, len
);
1470 if (len
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1471 all__sanity_check("evh__new_mem-post");
1472 if (UNLIKELY(thr
->pthread_create_nesting_level
> 0))
1473 shadow_mem_make_Untracked( thr
, a
, len
);
1477 void evh__new_mem_stack ( Addr a
, SizeT len
) {
1478 Thread
*thr
= get_current_Thread();
1479 if (SHOW_EVENTS
>= 2)
1480 VG_(printf
)("evh__new_mem_stack(%p, %lu)\n", (void*)a
, len
);
1481 shadow_mem_make_New( thr
, -VG_STACK_REDZONE_SZB
+ a
, len
);
1482 if (len
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1483 all__sanity_check("evh__new_mem_stack-post");
1484 if (UNLIKELY(thr
->pthread_create_nesting_level
> 0))
1485 shadow_mem_make_Untracked( thr
, a
, len
);
1489 void evh__new_mem_w_tid ( Addr a
, SizeT len
, ThreadId tid
) {
1490 Thread
*thr
= get_current_Thread();
1491 if (SHOW_EVENTS
>= 2)
1492 VG_(printf
)("evh__new_mem_w_tid(%p, %lu)\n", (void*)a
, len
);
1493 shadow_mem_make_New( thr
, a
, len
);
1494 if (len
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1495 all__sanity_check("evh__new_mem_w_tid-post");
1496 if (UNLIKELY(thr
->pthread_create_nesting_level
> 0))
1497 shadow_mem_make_Untracked( thr
, a
, len
);
1501 void evh__new_mem_w_perms ( Addr a
, SizeT len
,
1502 Bool rr
, Bool ww
, Bool xx
, ULong di_handle
) {
1503 Thread
*thr
= get_current_Thread();
1504 if (SHOW_EVENTS
>= 1)
1505 VG_(printf
)("evh__new_mem_w_perms(%p, %lu, %d,%d,%d)\n",
1506 (void*)a
, len
, (Int
)rr
, (Int
)ww
, (Int
)xx
);
1507 if (rr
|| ww
|| xx
) {
1508 shadow_mem_make_New( thr
, a
, len
);
1509 if (UNLIKELY(thr
->pthread_create_nesting_level
> 0))
1510 shadow_mem_make_Untracked( thr
, a
, len
);
1512 if (len
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1513 all__sanity_check("evh__new_mem_w_perms-post");
1517 void evh__set_perms ( Addr a
, SizeT len
,
1518 Bool rr
, Bool ww
, Bool xx
) {
1519 // This handles mprotect requests. If the memory is being put
1520 // into no-R no-W state, paint it as NoAccess, for the reasons
1521 // documented at evh__die_mem_munmap().
1522 if (SHOW_EVENTS
>= 1)
1523 VG_(printf
)("evh__set_perms(%p, %lu, r=%d w=%d x=%d)\n",
1524 (void*)a
, len
, (Int
)rr
, (Int
)ww
, (Int
)xx
);
1525 /* Hmm. What should we do here, that actually makes any sense?
1526 Let's say: if neither readable nor writable, then declare it
1527 NoAccess, else leave it alone. */
1529 shadow_mem_make_NoAccess_AHAE( get_current_Thread(), a
, len
);
1530 if (len
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1531 all__sanity_check("evh__set_perms-post");
1535 void evh__die_mem ( Addr a
, SizeT len
) {
1536 // Urr, libhb ignores this.
1537 if (SHOW_EVENTS
>= 2)
1538 VG_(printf
)("evh__die_mem(%p, %lu)\n", (void*)a
, len
);
1539 shadow_mem_make_NoAccess_NoFX( get_current_Thread(), a
, len
);
1540 if (len
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1541 all__sanity_check("evh__die_mem-post");
1545 void evh__die_mem_munmap ( Addr a
, SizeT len
) {
1546 // It's important that libhb doesn't ignore this. If, as is likely,
1547 // the client is subject to address space layout randomization,
1548 // then unmapped areas may never get remapped over, even in long
1549 // runs. If we just ignore them we wind up with large resource
1550 // (VTS) leaks in libhb. So force them to NoAccess, so that all
1551 // VTS references in the affected area are dropped. Marking memory
1552 // as NoAccess is expensive, but we assume that munmap is sufficiently
1553 // rare that the space gains of doing this are worth the costs.
1554 if (SHOW_EVENTS
>= 2)
1555 VG_(printf
)("evh__die_mem_munmap(%p, %lu)\n", (void*)a
, len
);
1556 shadow_mem_make_NoAccess_AHAE( get_current_Thread(), a
, len
);
1560 void evh__untrack_mem ( Addr a
, SizeT len
) {
1561 // Libhb doesn't ignore this.
1562 if (SHOW_EVENTS
>= 2)
1563 VG_(printf
)("evh__untrack_mem(%p, %lu)\n", (void*)a
, len
);
1564 shadow_mem_make_Untracked( get_current_Thread(), a
, len
);
1565 if (len
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1566 all__sanity_check("evh__untrack_mem-post");
1570 void evh__copy_mem ( Addr src
, Addr dst
, SizeT len
) {
1571 if (SHOW_EVENTS
>= 2)
1572 VG_(printf
)("evh__copy_mem(%p, %p, %lu)\n", (void*)src
, (void*)dst
, len
);
1573 Thread
*thr
= get_current_Thread();
1574 if (LIKELY(thr
->synchr_nesting
== 0))
1575 shadow_mem_scopy_range( thr
, src
, dst
, len
);
1576 if (len
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1577 all__sanity_check("evh__copy_mem-post");
1581 void evh__pre_thread_ll_create ( ThreadId parent
, ThreadId child
)
1583 if (SHOW_EVENTS
>= 1)
1584 VG_(printf
)("evh__pre_thread_ll_create(p=%d, c=%d)\n",
1585 (Int
)parent
, (Int
)child
);
1587 if (parent
!= VG_INVALID_THREADID
) {
1593 tl_assert(HG_(is_sane_ThreadId
)(parent
));
1594 tl_assert(HG_(is_sane_ThreadId
)(child
));
1595 tl_assert(parent
!= child
);
1597 thr_p
= map_threads_maybe_lookup( parent
);
1598 thr_c
= map_threads_maybe_lookup( child
);
1600 tl_assert(thr_p
!= NULL
);
1601 tl_assert(thr_c
== NULL
);
1603 hbthr_p
= thr_p
->hbthr
;
1604 tl_assert(hbthr_p
!= NULL
);
1605 tl_assert( libhb_get_Thr_hgthread(hbthr_p
) == thr_p
);
1607 hbthr_c
= libhb_create ( hbthr_p
);
1609 /* Create a new thread record for the child. */
1610 /* a Thread for the new thread ... */
1611 thr_c
= mk_Thread( hbthr_c
);
1612 tl_assert( libhb_get_Thr_hgthread(hbthr_c
) == NULL
);
1613 libhb_set_Thr_hgthread(hbthr_c
, thr_c
);
1615 /* and bind it in the thread-map table */
1616 map_threads
[child
] = thr_c
;
1617 tl_assert(thr_c
->coretid
== VG_INVALID_THREADID
);
1618 thr_c
->coretid
= child
;
1620 /* Record where the parent is so we can later refer to this in
1623 On x86/amd64-linux, this entails a nasty glibc specific hack.
1624 The stack snapshot is taken immediately after the parent has
1625 returned from its sys_clone call. Unfortunately there is no
1626 unwind info for the insn following "syscall" - reading the
1627 glibc sources confirms this. So we ask for a snapshot to be
1628 taken as if RIP was 3 bytes earlier, in a place where there
1629 is unwind info. Sigh.
1631 { Word first_ip_delta
= 0;
1632 # if defined(VGP_amd64_linux) || defined(VGP_x86_linux)
1633 first_ip_delta
= -3;
1634 # elif defined(VGP_arm64_linux) || defined(VGP_arm_linux)
1635 first_ip_delta
= -1;
1637 thr_c
->created_at
= VG_(record_ExeContext
)(parent
, first_ip_delta
);
1640 if (HG_(clo_ignore_thread_creation
)) {
1641 HG_(thread_enter_pthread_create
)(thr_c
);
1642 tl_assert(thr_c
->synchr_nesting
== 0);
1643 HG_(thread_enter_synchr
)(thr_c
);
1644 /* Counterpart in _VG_USERREQ__HG_SET_MY_PTHREAD_T. */
1648 if (HG_(clo_sanity_flags
) & SCE_THREADS
)
1649 all__sanity_check("evh__pre_thread_create-post");
1653 void evh__pre_thread_ll_exit ( ThreadId quit_tid
)
1657 if (SHOW_EVENTS
>= 1)
1658 VG_(printf
)("evh__pre_thread_ll_exit(thr=%d)\n",
1661 /* quit_tid has disappeared without joining to any other thread.
1662 Therefore there is no synchronisation event associated with its
1663 exit and so we have to pretty much treat it as if it was still
1664 alive but mysteriously making no progress. That is because, if
1665 we don't know when it really exited, then we can never say there
1666 is a point in time when we're sure the thread really has
1667 finished, and so we need to consider the possibility that it
1668 lingers indefinitely and continues to interact with other
1670 /* However, it might have rendezvous'd with a thread that called
1671 pthread_join with this one as arg, prior to this point (that's
1672 how NPTL works). In which case there has already been a prior
1673 sync event. So in any case, just let the thread exit. On NPTL,
1674 all thread exits go through here. */
1675 tl_assert(HG_(is_sane_ThreadId
)(quit_tid
));
1676 thr_q
= map_threads_maybe_lookup( quit_tid
);
1677 tl_assert(thr_q
!= NULL
);
1679 /* Complain if this thread holds any locks. */
1680 nHeld
= HG_(cardinalityWS
)( univ_lsets
, thr_q
->locksetA
);
1681 tl_assert(nHeld
>= 0);
1684 VG_(sprintf
)(buf
, "Exiting thread still holds %d lock%s",
1685 nHeld
, nHeld
> 1 ? "s" : "");
1686 HG_(record_error_Misc
)( thr_q
, buf
);
1689 /* Not much to do here:
1690 - tell libhb the thread is gone
1691 - clear the map_threads entry, in order that the Valgrind core
1693 /* Cleanup actions (next 5 lines) copied in evh__atfork_child; keep
1695 tl_assert(thr_q
->hbthr
);
1696 libhb_async_exit(thr_q
->hbthr
);
1697 tl_assert(thr_q
->coretid
== quit_tid
);
1698 thr_q
->coretid
= VG_INVALID_THREADID
;
1699 map_threads_delete( quit_tid
);
1701 if (HG_(clo_sanity_flags
) & SCE_THREADS
)
1702 all__sanity_check("evh__pre_thread_ll_exit-post");
1705 /* This is called immediately after fork, for the child only. 'tid'
1706 is the only surviving thread (as per POSIX rules on fork() in
1707 threaded programs), so we have to clean up map_threads to remove
1708 entries for any other threads. */
1710 void evh__atfork_child ( ThreadId tid
)
1714 /* Slot 0 should never be used. */
1715 thr
= map_threads_maybe_lookup( 0/*INVALID*/ );
1717 /* Clean up all other slots except 'tid'. */
1718 for (i
= 1; i
< VG_N_THREADS
; i
++) {
1721 thr
= map_threads_maybe_lookup(i
);
1724 /* Cleanup actions (next 5 lines) copied from end of
1725 evh__pre_thread_ll_exit; keep in sync. */
1726 tl_assert(thr
->hbthr
);
1727 libhb_async_exit(thr
->hbthr
);
1728 tl_assert(thr
->coretid
== i
);
1729 thr
->coretid
= VG_INVALID_THREADID
;
1730 map_threads_delete(i
);
1734 /* generate a dependence from the hbthr_q quitter to the hbthr_s stayer. */
1736 void generate_quitter_stayer_dependence (Thr
* hbthr_q
, Thr
* hbthr_s
)
1739 /* Allocate a temporary synchronisation object and use it to send
1740 an imaginary message from the quitter to the stayer, the purpose
1741 being to generate a dependence from the quitter to the
1743 so
= libhb_so_alloc();
1745 /* Send last arg of _so_send as False, since the sending thread
1746 doesn't actually exist any more, so we don't want _so_send to
1747 try taking stack snapshots of it. */
1748 libhb_so_send(hbthr_q
, so
, True
/*strong_send*//*?!? wrt comment above*/);
1749 libhb_so_recv(hbthr_s
, so
, True
/*strong_recv*/);
1750 libhb_so_dealloc(so
);
1752 /* Tell libhb that the quitter has been reaped. Note that we might
1753 have to be cleverer about this, to exclude 2nd and subsequent
1754 notifications for the same hbthr_q, in the case where the app is
1755 buggy (calls pthread_join twice or more on the same thread) AND
1756 where libpthread is also buggy and doesn't return ESRCH on
1757 subsequent calls. (If libpthread isn't thusly buggy, then the
1758 wrapper for pthread_join in hg_intercepts.c will stop us getting
1759 notified here multiple times for the same joinee.) See also
1760 comments in helgrind/tests/jointwice.c. */
1761 libhb_joinedwith_done(hbthr_q
);
1766 void evh__HG_PTHREAD_JOIN_POST ( ThreadId stay_tid
, Thread
* quit_thr
)
1773 if (SHOW_EVENTS
>= 1)
1774 VG_(printf
)("evh__post_thread_join(stayer=%d, quitter=%p)\n",
1775 (Int
)stay_tid
, quit_thr
);
1777 tl_assert(HG_(is_sane_ThreadId
)(stay_tid
));
1779 thr_s
= map_threads_maybe_lookup( stay_tid
);
1781 tl_assert(thr_s
!= NULL
);
1782 tl_assert(thr_q
!= NULL
);
1783 tl_assert(thr_s
!= thr_q
);
1785 hbthr_s
= thr_s
->hbthr
;
1786 hbthr_q
= thr_q
->hbthr
;
1787 tl_assert(hbthr_s
!= hbthr_q
);
1788 tl_assert( libhb_get_Thr_hgthread(hbthr_s
) == thr_s
);
1789 tl_assert( libhb_get_Thr_hgthread(hbthr_q
) == thr_q
);
1791 generate_quitter_stayer_dependence (hbthr_q
, hbthr_s
);
1793 /* evh__pre_thread_ll_exit issues an error message if the exiting
1794 thread holds any locks. No need to check here. */
1796 /* This holds because, at least when using NPTL as the thread
1797 library, we should be notified the low level thread exit before
1798 we hear of any join event on it. The low level exit
1799 notification feeds through into evh__pre_thread_ll_exit,
1800 which should clear the map_threads entry for it. Hence we
1801 expect there to be no map_threads entry at this point. */
1802 tl_assert( map_threads_maybe_reverse_lookup_SLOW(thr_q
)
1803 == VG_INVALID_THREADID
);
1805 if (HG_(clo_sanity_flags
) & SCE_THREADS
)
1806 all__sanity_check("evh__post_thread_join-post");
1810 void evh__pre_mem_read ( CorePart part
, ThreadId tid
, const HChar
* s
,
1811 Addr a
, SizeT size
) {
1812 if (SHOW_EVENTS
>= 2
1813 || (SHOW_EVENTS
>= 1 && size
!= 1))
1814 VG_(printf
)("evh__pre_mem_read(ctid=%d, \"%s\", %p, %lu)\n",
1815 (Int
)tid
, s
, (void*)a
, size
);
1816 Thread
*thr
= map_threads_lookup(tid
);
1817 if (LIKELY(thr
->synchr_nesting
== 0))
1818 shadow_mem_cread_range(thr
, a
, size
);
1819 if (size
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1820 all__sanity_check("evh__pre_mem_read-post");
1824 void evh__pre_mem_read_asciiz ( CorePart part
, ThreadId tid
,
1825 const HChar
* s
, Addr a
) {
1827 if (SHOW_EVENTS
>= 1)
1828 VG_(printf
)("evh__pre_mem_asciiz(ctid=%d, \"%s\", %p)\n",
1829 (Int
)tid
, s
, (void*)a
);
1830 // Don't segfault if the string starts in an obviously stupid
1831 // place. Actually we should check the whole string, not just
1832 // the start address, but that's too much trouble. At least
1833 // checking the first byte is better than nothing. See #255009.
1834 if (!VG_(am_is_valid_for_client
) (a
, 1, VKI_PROT_READ
))
1836 Thread
*thr
= map_threads_lookup(tid
);
1837 len
= VG_(strlen
)( (HChar
*) a
);
1838 if (LIKELY(thr
->synchr_nesting
== 0))
1839 shadow_mem_cread_range( thr
, a
, len
+1 );
1840 if (len
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1841 all__sanity_check("evh__pre_mem_read_asciiz-post");
1845 void evh__pre_mem_write ( CorePart part
, ThreadId tid
, const HChar
* s
,
1846 Addr a
, SizeT size
) {
1847 if (SHOW_EVENTS
>= 1)
1848 VG_(printf
)("evh__pre_mem_write(ctid=%d, \"%s\", %p, %lu)\n",
1849 (Int
)tid
, s
, (void*)a
, size
);
1850 Thread
*thr
= map_threads_lookup(tid
);
1851 if (LIKELY(thr
->synchr_nesting
== 0))
1852 shadow_mem_cwrite_range(thr
, a
, size
);
1853 if (size
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1854 all__sanity_check("evh__pre_mem_write-post");
1858 void evh__new_mem_heap ( Addr a
, SizeT len
, Bool is_inited
) {
1859 if (SHOW_EVENTS
>= 1)
1860 VG_(printf
)("evh__new_mem_heap(%p, %lu, inited=%d)\n",
1861 (void*)a
, len
, (Int
)is_inited
);
1862 // We ignore the initialisation state (is_inited); that's ok.
1863 shadow_mem_make_New(get_current_Thread(), a
, len
);
1864 if (len
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1865 all__sanity_check("evh__pre_mem_read-post");
1869 void evh__die_mem_heap ( Addr a
, SizeT len
) {
1871 if (SHOW_EVENTS
>= 1)
1872 VG_(printf
)("evh__die_mem_heap(%p, %lu)\n", (void*)a
, len
);
1873 thr
= get_current_Thread();
1875 if (HG_(clo_free_is_write
)) {
1876 /* Treat frees as if the memory was written immediately prior to
1877 the free. This shakes out more races, specifically, cases
1878 where memory is referenced by one thread, and freed by
1879 another, and there's no observable synchronisation event to
1880 guarantee that the reference happens before the free. */
1881 if (LIKELY(thr
->synchr_nesting
== 0))
1882 shadow_mem_cwrite_range(thr
, a
, len
);
1884 shadow_mem_make_NoAccess_AHAE( thr
, a
, len
);
1885 /* We used to call instead
1886 shadow_mem_make_NoAccess_NoFX( thr, a, len );
1887 A non-buggy application will not access anymore
1888 the freed memory, and so marking no access is in theory useless.
1889 Not marking freed memory would avoid the overhead for applications
1890 doing mostly malloc/free, as the freed memory should then be recycled
1891 very quickly after marking.
1892 We rather mark it noaccess for the following reasons:
1893 * accessibility bits then always correctly represents the memory
1894 status (e.g. for the client request VALGRIND_HG_GET_ABITS).
1895 * the overhead is reasonable (about 5 seconds per Gb in 1000 bytes
1896 blocks, on a ppc64le, for a unrealistic workload of an application
1897 doing only malloc/free).
1898 * marking no access allows to GC the SecMap, which might improve
1899 performance and/or memory usage.
1900 * we might detect more applications bugs when memory is marked
1902 If needed, we could support here an option --free-is-noaccess=yes|no
1903 to avoid marking freed memory as no access if some applications
1904 would need to avoid the marking noaccess overhead. */
1906 if (len
>= SCE_BIGRANGE_T
&& (HG_(clo_sanity_flags
) & SCE_BIGRANGE
))
1907 all__sanity_check("evh__pre_mem_read-post");
1910 /* --- Event handlers called from generated code --- */
1912 static VG_REGPARM(1)
1913 void evh__mem_help_cread_1(Addr a
) {
1914 Thread
* thr
= get_current_Thread_in_C_C();
1915 Thr
* hbthr
= thr
->hbthr
;
1916 if (LIKELY(thr
->synchr_nesting
== 0))
1917 LIBHB_CREAD_1(hbthr
, a
);
1920 static VG_REGPARM(1)
1921 void evh__mem_help_cread_2(Addr a
) {
1922 Thread
* thr
= get_current_Thread_in_C_C();
1923 Thr
* hbthr
= thr
->hbthr
;
1924 if (LIKELY(thr
->synchr_nesting
== 0))
1925 LIBHB_CREAD_2(hbthr
, a
);
1928 static VG_REGPARM(1)
1929 void evh__mem_help_cread_4(Addr a
) {
1930 Thread
* thr
= get_current_Thread_in_C_C();
1931 Thr
* hbthr
= thr
->hbthr
;
1932 if (LIKELY(thr
->synchr_nesting
== 0))
1933 LIBHB_CREAD_4(hbthr
, a
);
1936 static VG_REGPARM(1)
1937 void evh__mem_help_cread_8(Addr a
) {
1938 Thread
* thr
= get_current_Thread_in_C_C();
1939 Thr
* hbthr
= thr
->hbthr
;
1940 if (LIKELY(thr
->synchr_nesting
== 0))
1941 LIBHB_CREAD_8(hbthr
, a
);
1944 static VG_REGPARM(2)
1945 void evh__mem_help_cread_N(Addr a
, SizeT size
) {
1946 Thread
* thr
= get_current_Thread_in_C_C();
1947 Thr
* hbthr
= thr
->hbthr
;
1948 if (LIKELY(thr
->synchr_nesting
== 0))
1949 LIBHB_CREAD_N(hbthr
, a
, size
);
1952 static VG_REGPARM(1)
1953 void evh__mem_help_cwrite_1(Addr a
) {
1954 Thread
* thr
= get_current_Thread_in_C_C();
1955 Thr
* hbthr
= thr
->hbthr
;
1956 if (LIKELY(thr
->synchr_nesting
== 0))
1957 LIBHB_CWRITE_1(hbthr
, a
);
1960 static VG_REGPARM(1)
1961 void evh__mem_help_cwrite_2(Addr a
) {
1962 Thread
* thr
= get_current_Thread_in_C_C();
1963 Thr
* hbthr
= thr
->hbthr
;
1964 if (LIKELY(thr
->synchr_nesting
== 0))
1965 LIBHB_CWRITE_2(hbthr
, a
);
1968 static VG_REGPARM(1)
1969 void evh__mem_help_cwrite_4(Addr a
) {
1970 Thread
* thr
= get_current_Thread_in_C_C();
1971 Thr
* hbthr
= thr
->hbthr
;
1972 if (LIKELY(thr
->synchr_nesting
== 0))
1973 LIBHB_CWRITE_4(hbthr
, a
);
1976 static VG_REGPARM(1)
1977 void evh__mem_help_cwrite_8(Addr a
) {
1978 Thread
* thr
= get_current_Thread_in_C_C();
1979 Thr
* hbthr
= thr
->hbthr
;
1980 if (LIKELY(thr
->synchr_nesting
== 0))
1981 LIBHB_CWRITE_8(hbthr
, a
);
1984 static VG_REGPARM(2)
1985 void evh__mem_help_cwrite_N(Addr a
, SizeT size
) {
1986 Thread
* thr
= get_current_Thread_in_C_C();
1987 Thr
* hbthr
= thr
->hbthr
;
1988 if (LIKELY(thr
->synchr_nesting
== 0))
1989 LIBHB_CWRITE_N(hbthr
, a
, size
);
1993 /* ------------------------------------------------------- */
1994 /* -------------- events to do with mutexes -------------- */
1995 /* ------------------------------------------------------- */
1997 /* EXPOSITION only: by intercepting lock init events we can show the
1998 user where the lock was initialised, rather than only being able to
1999 show where it was first locked. Intercepting lock initialisations
2000 is not necessary for the basic operation of the race checker. */
2002 void evh__HG_PTHREAD_MUTEX_INIT_POST( ThreadId tid
,
2003 void* mutex
, Word mbRec
)
2005 if (SHOW_EVENTS
>= 1)
2006 VG_(printf
)("evh__hg_PTHREAD_MUTEX_INIT_POST(ctid=%d, mbRec=%ld, %p)\n",
2007 (Int
)tid
, mbRec
, (void*)mutex
);
2008 tl_assert(mbRec
== 0 || mbRec
== 1);
2009 map_locks_lookup_or_create( mbRec
? LK_mbRec
: LK_nonRec
,
2011 if (HG_(clo_sanity_flags
) & SCE_LOCKS
)
2012 all__sanity_check("evh__hg_PTHREAD_MUTEX_INIT_POST");
2016 void evh__HG_PTHREAD_MUTEX_DESTROY_PRE( ThreadId tid
, void* mutex
,
2017 Bool mutex_is_init
)
2021 if (SHOW_EVENTS
>= 1)
2022 VG_(printf
)("evh__hg_PTHREAD_MUTEX_DESTROY_PRE"
2023 "(ctid=%d, %p, isInit=%d)\n",
2024 (Int
)tid
, (void*)mutex
, (Int
)mutex_is_init
);
2026 thr
= map_threads_maybe_lookup( tid
);
2027 /* cannot fail - Thread* must already exist */
2028 tl_assert( HG_(is_sane_Thread
)(thr
) );
2030 lk
= map_locks_maybe_lookup( (Addr
)mutex
);
2032 if (lk
== NULL
&& mutex_is_init
) {
2033 /* We're destroying a mutex which we don't have any record of,
2034 and which appears to have the value PTHREAD_MUTEX_INITIALIZER.
2035 Assume it never got used, and so we don't need to do anything
2040 if (lk
== NULL
|| (lk
->kind
!= LK_nonRec
&& lk
->kind
!= LK_mbRec
)) {
2041 HG_(record_error_Misc
)(
2042 thr
, "pthread_mutex_destroy with invalid argument" );
2046 tl_assert( HG_(is_sane_LockN
)(lk
) );
2047 tl_assert( lk
->guestaddr
== (Addr
)mutex
);
2049 /* Basically act like we unlocked the lock */
2050 HG_(record_error_Misc
)(
2051 thr
, "pthread_mutex_destroy of a locked mutex" );
2052 /* remove lock from locksets of all owning threads */
2053 remove_Lock_from_locksets_of_all_owning_Threads( lk
);
2054 VG_(deleteBag
)( lk
->heldBy
);
2057 lk
->acquired_at
= NULL
;
2059 tl_assert( !lk
->heldBy
);
2060 tl_assert( HG_(is_sane_LockN
)(lk
) );
2062 if (HG_(clo_track_lockorders
))
2063 laog__handle_one_lock_deletion(lk
);
2064 map_locks_delete( lk
->guestaddr
);
2069 if (HG_(clo_sanity_flags
) & SCE_LOCKS
)
2070 all__sanity_check("evh__hg_PTHREAD_MUTEX_DESTROY_PRE");
2073 static void evh__HG_PTHREAD_MUTEX_LOCK_PRE ( ThreadId tid
,
2074 void* mutex
, Word isTryLock
)
2076 /* Just check the mutex is sane; nothing else to do. */
2077 // 'mutex' may be invalid - not checked by wrapper
2080 if (SHOW_EVENTS
>= 1)
2081 VG_(printf
)("evh__hg_PTHREAD_MUTEX_LOCK_PRE(ctid=%d, mutex=%p)\n",
2082 (Int
)tid
, (void*)mutex
);
2084 tl_assert(isTryLock
== 0 || isTryLock
== 1);
2085 thr
= map_threads_maybe_lookup( tid
);
2086 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2088 lk
= map_locks_maybe_lookup( (Addr
)mutex
);
2090 if (lk
&& (lk
->kind
== LK_rdwr
)) {
2091 HG_(record_error_Misc
)( thr
, "pthread_mutex_lock with a "
2092 "pthread_rwlock_t* argument " );
2097 && (lk
->kind
== LK_nonRec
|| lk
->kind
== LK_rdwr
)
2100 && VG_(elemBag
)( lk
->heldBy
, (UWord
)thr
) > 0 ) {
2101 /* uh, it's a non-recursive lock and we already w-hold it, and
2102 this is a real lock operation (not a speculative "tryLock"
2103 kind of thing). Duh. Deadlock coming up; but at least
2104 produce an error message. */
2105 const HChar
* errstr
= "Attempt to re-lock a "
2106 "non-recursive lock I already hold";
2107 const HChar
* auxstr
= "Lock was previously acquired";
2108 if (lk
->acquired_at
) {
2109 HG_(record_error_Misc_w_aux
)( thr
, errstr
, auxstr
, lk
->acquired_at
);
2111 HG_(record_error_Misc
)( thr
, errstr
);
2116 static void evh__HG_PTHREAD_MUTEX_LOCK_POST ( ThreadId tid
, void* mutex
)
2118 // only called if the real library call succeeded - so mutex is sane
2120 if (SHOW_EVENTS
>= 1)
2121 VG_(printf
)("evh__HG_PTHREAD_MUTEX_LOCK_POST(ctid=%d, mutex=%p)\n",
2122 (Int
)tid
, (void*)mutex
);
2124 thr
= map_threads_maybe_lookup( tid
);
2125 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2127 evhH__post_thread_w_acquires_lock(
2129 LK_mbRec
, /* if not known, create new lock with this LockKind */
2134 static void evh__HG_PTHREAD_MUTEX_UNLOCK_PRE ( ThreadId tid
, void* mutex
)
2136 // 'mutex' may be invalid - not checked by wrapper
2138 if (SHOW_EVENTS
>= 1)
2139 VG_(printf
)("evh__HG_PTHREAD_MUTEX_UNLOCK_PRE(ctid=%d, mutex=%p)\n",
2140 (Int
)tid
, (void*)mutex
);
2142 thr
= map_threads_maybe_lookup( tid
);
2143 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2145 evhH__pre_thread_releases_lock( thr
, (Addr
)mutex
, False
/*!isRDWR*/ );
2148 static void evh__HG_PTHREAD_MUTEX_UNLOCK_POST ( ThreadId tid
, void* mutex
)
2150 // only called if the real library call succeeded - so mutex is sane
2152 if (SHOW_EVENTS
>= 1)
2153 VG_(printf
)("evh__hg_PTHREAD_MUTEX_UNLOCK_POST(ctid=%d, mutex=%p)\n",
2154 (Int
)tid
, (void*)mutex
);
2155 thr
= map_threads_maybe_lookup( tid
);
2156 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2158 // anything we should do here?
2162 /* ------------------------------------------------------- */
2163 /* -------------- events to do with spinlocks ------------ */
2164 /* ------------------------------------------------------- */
2166 /* All a bit of a kludge. Pretend we're really dealing with ordinary
2167 pthread_mutex_t's instead, for the most part. */
2169 static void evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE( ThreadId tid
,
2174 /* In glibc's kludgey world, we're either initialising or unlocking
2175 it. Since this is the pre-routine, if it is locked, unlock it
2176 and take a dependence edge. Otherwise, do nothing. */
2178 if (SHOW_EVENTS
>= 1)
2179 VG_(printf
)("evh__hg_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE"
2180 "(ctid=%d, slock=%p)\n",
2181 (Int
)tid
, (void*)slock
);
2183 thr
= map_threads_maybe_lookup( tid
);
2184 /* cannot fail - Thread* must already exist */;
2185 tl_assert( HG_(is_sane_Thread
)(thr
) );
2187 lk
= map_locks_maybe_lookup( (Addr
)slock
);
2188 if (lk
&& lk
->heldBy
) {
2189 /* it's held. So do the normal pre-unlock actions, as copied
2190 from evh__HG_PTHREAD_MUTEX_UNLOCK_PRE. This stupidly
2191 duplicates the map_locks_maybe_lookup. */
2192 evhH__pre_thread_releases_lock( thr
, (Addr
)slock
,
2197 static void evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_POST( ThreadId tid
,
2201 /* More kludgery. If the lock has never been seen before, do
2202 actions as per evh__HG_PTHREAD_MUTEX_INIT_POST. Else do
2205 if (SHOW_EVENTS
>= 1)
2206 VG_(printf
)("evh__hg_PTHREAD_SPIN_INIT_OR_UNLOCK_POST"
2207 "(ctid=%d, slock=%p)\n",
2208 (Int
)tid
, (void*)slock
);
2210 lk
= map_locks_maybe_lookup( (Addr
)slock
);
2212 map_locks_lookup_or_create( LK_nonRec
, (Addr
)slock
, tid
);
2216 static void evh__HG_PTHREAD_SPIN_LOCK_PRE( ThreadId tid
,
2217 void* slock
, Word isTryLock
)
2219 evh__HG_PTHREAD_MUTEX_LOCK_PRE( tid
, slock
, isTryLock
);
2222 static void evh__HG_PTHREAD_SPIN_LOCK_POST( ThreadId tid
,
2225 evh__HG_PTHREAD_MUTEX_LOCK_POST( tid
, slock
);
2228 static void evh__HG_PTHREAD_SPIN_DESTROY_PRE( ThreadId tid
,
2231 evh__HG_PTHREAD_MUTEX_DESTROY_PRE( tid
, slock
, 0/*!isInit*/ );
2235 /* ----------------------------------------------------- */
2236 /* --------------- events to do with CVs --------------- */
2237 /* ----------------------------------------------------- */
2239 /* A mapping from CV to (the SO associated with it, plus some
2240 auxiliary data for error checking). When the CV is
2241 signalled/broadcasted upon, we do a 'send' into the SO, and when a
2242 wait on it completes, we do a 'recv' from the SO. This is believed
2243 to give the correct happens-before events arising from CV
2244 signallings/broadcasts.
2247 /* .so is the SO for this CV.
2248 .mx_ga is the associated mutex, when .nWaiters > 0
2250 POSIX says effectively that the first pthread_cond_{timed}wait call
2251 causes a dynamic binding between the CV and the mutex, and that
2252 lasts until such time as the waiter count falls to zero. Hence
2253 need to keep track of the number of waiters in order to do
2254 consistency tracking. */
2257 SO
* so
; /* libhb-allocated SO */
2258 void* mx_ga
; /* addr of associated mutex, if any */
2259 UWord nWaiters
; /* # threads waiting on the CV */
2264 /* pthread_cond_t* -> CVInfo* */
2265 static WordFM
* map_cond_to_CVInfo
= NULL
;
2267 static void map_cond_to_CVInfo_INIT ( void ) {
2268 if (UNLIKELY(map_cond_to_CVInfo
== NULL
)) {
2269 map_cond_to_CVInfo
= VG_(newFM
)( HG_(zalloc
),
2270 "hg.mctCI.1", HG_(free
), NULL
);
2274 static CVInfo
* map_cond_to_CVInfo_lookup_or_alloc ( void* cond
) {
2276 map_cond_to_CVInfo_INIT();
2277 if (VG_(lookupFM
)( map_cond_to_CVInfo
, &key
, &val
, (UWord
)cond
)) {
2278 tl_assert(key
== (UWord
)cond
);
2279 return (CVInfo
*)val
;
2281 SO
* so
= libhb_so_alloc();
2282 CVInfo
* cvi
= HG_(zalloc
)("hg.mctCloa.1", sizeof(CVInfo
));
2285 VG_(addToFM
)( map_cond_to_CVInfo
, (UWord
)cond
, (UWord
)cvi
);
2290 static CVInfo
* map_cond_to_CVInfo_lookup_NO_alloc ( void* cond
) {
2292 map_cond_to_CVInfo_INIT();
2293 if (VG_(lookupFM
)( map_cond_to_CVInfo
, &key
, &val
, (UWord
)cond
)) {
2294 tl_assert(key
== (UWord
)cond
);
2295 return (CVInfo
*)val
;
2301 static void map_cond_to_CVInfo_delete ( ThreadId tid
,
2302 void* cond
, Bool cond_is_init
) {
2306 thr
= map_threads_maybe_lookup( tid
);
2307 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2309 map_cond_to_CVInfo_INIT();
2310 if (VG_(lookupFM
)( map_cond_to_CVInfo
, &keyW
, &valW
, (UWord
)cond
)) {
2311 CVInfo
* cvi
= (CVInfo
*)valW
;
2312 tl_assert(keyW
== (UWord
)cond
);
2315 if (cvi
->nWaiters
> 0) {
2316 HG_(record_error_Misc
)(
2317 thr
, "pthread_cond_destroy:"
2318 " destruction of condition variable being waited upon");
2319 /* Destroying a cond var being waited upon outcome is EBUSY and
2320 variable is not destroyed. */
2323 if (!VG_(delFromFM
)( map_cond_to_CVInfo
, &keyW
, &valW
, (UWord
)cond
))
2324 tl_assert(0); // cond var found above, and not here ???
2325 libhb_so_dealloc(cvi
->so
);
2329 /* We have no record of this CV. So complain about it
2330 .. except, don't bother to complain if it has exactly the
2331 value PTHREAD_COND_INITIALIZER, since it might be that the CV
2332 was initialised like that but never used. */
2333 if (!cond_is_init
) {
2334 HG_(record_error_Misc
)(
2335 thr
, "pthread_cond_destroy: destruction of unknown cond var");
2340 static void evh__HG_PTHREAD_COND_SIGNAL_PRE ( ThreadId tid
, void* cond
)
2342 /* 'tid' has signalled on 'cond'. As per the comment above, bind
2343 cond to a SO if it is not already so bound, and 'send' on the
2344 SO. This is later used by other thread(s) which successfully
2345 exit from a pthread_cond_wait on the same cv; then they 'recv'
2346 from the SO, thereby acquiring a dependency on this signalling
2352 if (SHOW_EVENTS
>= 1)
2353 VG_(printf
)("evh__HG_PTHREAD_COND_SIGNAL_PRE(ctid=%d, cond=%p)\n",
2354 (Int
)tid
, (void*)cond
);
2356 thr
= map_threads_maybe_lookup( tid
);
2357 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2359 cvi
= map_cond_to_CVInfo_lookup_or_alloc( cond
);
2363 // error-if: mutex is bogus
2364 // error-if: mutex is not locked
2365 // Hmm. POSIX doesn't actually say that it's an error to call
2366 // pthread_cond_signal with the associated mutex being unlocked.
2367 // Although it does say that it should be "if consistent scheduling
2368 // is desired." For that reason, print "dubious" if the lock isn't
2369 // held by any thread. Skip the "dubious" if it is held by some
2370 // other thread; that sounds straight-out wrong.
2372 // Anybody who writes code that signals on a CV without holding
2373 // the associated MX needs to be shipped off to a lunatic asylum
2374 // ASAP, even though POSIX doesn't actually declare such behaviour
2375 // illegal -- it makes code extremely difficult to understand/
2376 // reason about. In particular it puts the signalling thread in
2377 // a situation where it is racing against the released waiter
2378 // as soon as the signalling is done, and so there needs to be
2379 // some auxiliary synchronisation mechanism in the program that
2380 // makes this safe -- or the race(s) need to be harmless, or
2381 // probably nonexistent.
2385 if (cvi
->mx_ga
!= 0) {
2386 lk
= map_locks_maybe_lookup( (Addr
)cvi
->mx_ga
);
2388 /* note: lk could be NULL. Be careful. */
2390 if (lk
->kind
== LK_rdwr
) {
2391 HG_(record_error_Misc
)(thr
,
2392 "pthread_cond_{signal,broadcast}: associated lock is a rwlock");
2394 if (lk
->heldBy
== NULL
) {
2395 HG_(record_error_Misc
)(thr
,
2396 "pthread_cond_{signal,broadcast}: dubious: "
2397 "associated lock is not held by any thread");
2399 if (lk
->heldBy
!= NULL
&& 0 == VG_(elemBag
)(lk
->heldBy
, (UWord
)thr
)) {
2400 HG_(record_error_Misc
)(thr
,
2401 "pthread_cond_{signal,broadcast}: "
2402 "associated lock is not held by calling thread");
2405 /* Couldn't even find the damn thing. */
2406 // But actually .. that's not necessarily an error. We don't
2407 // know the (CV,MX) binding until a pthread_cond_wait or bcast
2408 // shows us what it is, and if that may not have happened yet.
2409 // So just keep quiet in this circumstance.
2410 //HG_(record_error_Misc)( thr,
2411 // "pthread_cond_{signal,broadcast}: "
2412 // "no or invalid mutex associated with cond");
2416 libhb_so_send( thr
->hbthr
, cvi
->so
, True
/*strong_send*/ );
2419 /* returns True if it reckons 'mutex' is valid and held by this
2420 thread, else False */
2421 static Bool
evh__HG_PTHREAD_COND_WAIT_PRE ( ThreadId tid
,
2422 void* cond
, void* mutex
)
2426 Bool lk_valid
= True
;
2429 if (SHOW_EVENTS
>= 1)
2430 VG_(printf
)("evh__hg_PTHREAD_COND_WAIT_PRE"
2431 "(ctid=%d, cond=%p, mutex=%p)\n",
2432 (Int
)tid
, (void*)cond
, (void*)mutex
);
2434 thr
= map_threads_maybe_lookup( tid
);
2435 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2437 lk
= map_locks_maybe_lookup( (Addr
)mutex
);
2439 /* Check for stupid mutex arguments. There are various ways to be
2440 a bozo. Only complain once, though, even if more than one thing
2444 HG_(record_error_Misc
)(
2446 "pthread_cond_{timed}wait called with invalid mutex" );
2448 tl_assert( HG_(is_sane_LockN
)(lk
) );
2449 if (lk
->kind
== LK_rdwr
) {
2451 HG_(record_error_Misc
)(
2452 thr
, "pthread_cond_{timed}wait called with mutex "
2453 "of type pthread_rwlock_t*" );
2455 if (lk
->heldBy
== NULL
) {
2457 HG_(record_error_Misc
)(
2458 thr
, "pthread_cond_{timed}wait called with un-held mutex");
2460 if (lk
->heldBy
!= NULL
2461 && VG_(elemBag
)( lk
->heldBy
, (UWord
)thr
) == 0) {
2463 HG_(record_error_Misc
)(
2464 thr
, "pthread_cond_{timed}wait called with mutex "
2465 "held by a different thread" );
2469 // error-if: cond is also associated with a different mutex
2470 cvi
= map_cond_to_CVInfo_lookup_or_alloc(cond
);
2473 if (cvi
->nWaiters
== 0) {
2474 /* form initial (CV,MX) binding */
2477 else /* check existing (CV,MX) binding */
2478 if (cvi
->mx_ga
!= mutex
) {
2479 HG_(record_error_Misc
)(
2480 thr
, "pthread_cond_{timed}wait: cond is associated "
2481 "with a different mutex");
2488 static void evh__HG_PTHREAD_COND_WAIT_POST ( ThreadId tid
,
2489 void* cond
, void* mutex
,
2492 /* A pthread_cond_wait(cond, mutex) completed successfully. Find
2493 the SO for this cond, and 'recv' from it so as to acquire a
2494 dependency edge back to the signaller/broadcaster. */
2498 if (SHOW_EVENTS
>= 1)
2499 VG_(printf
)("evh__HG_PTHREAD_COND_WAIT_POST"
2500 "(ctid=%d, cond=%p, mutex=%p)\n, timeout=%d",
2501 (Int
)tid
, (void*)cond
, (void*)mutex
, (Int
)timeout
);
2503 thr
= map_threads_maybe_lookup( tid
);
2504 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2506 // error-if: cond is also associated with a different mutex
2508 cvi
= map_cond_to_CVInfo_lookup_NO_alloc( cond
);
2510 /* This could be either a bug in helgrind or the guest application
2511 that did an error (e.g. cond var was destroyed by another thread.
2512 Let's assume helgrind is perfect ...
2513 Note that this is similar to drd behaviour. */
2514 HG_(record_error_Misc
)(thr
, "condition variable has been destroyed while"
2515 " being waited upon");
2521 tl_assert(cvi
->nWaiters
> 0);
2523 if (!timeout
&& !libhb_so_everSent(cvi
->so
)) {
2524 /* Hmm. How can a wait on 'cond' succeed if nobody signalled
2525 it? If this happened it would surely be a bug in the threads
2526 library. Or one of those fabled "spurious wakeups". */
2527 HG_(record_error_Misc
)( thr
, "Bug in libpthread: pthread_cond_wait "
2529 " without prior pthread_cond_post");
2532 /* anyway, acquire a dependency on it. */
2533 libhb_so_recv( thr
->hbthr
, cvi
->so
, True
/*strong_recv*/ );
2538 static void evh__HG_PTHREAD_COND_INIT_POST ( ThreadId tid
,
2539 void* cond
, void* cond_attr
)
2543 if (SHOW_EVENTS
>= 1)
2544 VG_(printf
)("evh__HG_PTHREAD_COND_INIT_POST"
2545 "(ctid=%d, cond=%p, cond_attr=%p)\n",
2546 (Int
)tid
, (void*)cond
, (void*) cond_attr
);
2548 cvi
= map_cond_to_CVInfo_lookup_or_alloc( cond
);
2550 tl_assert (cvi
->so
);
2554 static void evh__HG_PTHREAD_COND_DESTROY_PRE ( ThreadId tid
,
2555 void* cond
, Bool cond_is_init
)
2557 /* Deal with destroy events. The only purpose is to free storage
2558 associated with the CV, so as to avoid any possible resource
2560 if (SHOW_EVENTS
>= 1)
2561 VG_(printf
)("evh__HG_PTHREAD_COND_DESTROY_PRE"
2562 "(ctid=%d, cond=%p, cond_is_init=%d)\n",
2563 (Int
)tid
, (void*)cond
, (Int
)cond_is_init
);
2565 map_cond_to_CVInfo_delete( tid
, cond
, cond_is_init
);
2569 /* ------------------------------------------------------- */
2570 /* -------------- events to do with rwlocks -------------- */
2571 /* ------------------------------------------------------- */
2573 /* EXPOSITION only */
2575 void evh__HG_PTHREAD_RWLOCK_INIT_POST( ThreadId tid
, void* rwl
)
2577 if (SHOW_EVENTS
>= 1)
2578 VG_(printf
)("evh__hg_PTHREAD_RWLOCK_INIT_POST(ctid=%d, %p)\n",
2579 (Int
)tid
, (void*)rwl
);
2580 map_locks_lookup_or_create( LK_rdwr
, (Addr
)rwl
, tid
);
2581 if (HG_(clo_sanity_flags
) & SCE_LOCKS
)
2582 all__sanity_check("evh__hg_PTHREAD_RWLOCK_INIT_POST");
2586 void evh__HG_PTHREAD_RWLOCK_DESTROY_PRE( ThreadId tid
, void* rwl
)
2590 if (SHOW_EVENTS
>= 1)
2591 VG_(printf
)("evh__hg_PTHREAD_RWLOCK_DESTROY_PRE(ctid=%d, %p)\n",
2592 (Int
)tid
, (void*)rwl
);
2594 thr
= map_threads_maybe_lookup( tid
);
2595 /* cannot fail - Thread* must already exist */
2596 tl_assert( HG_(is_sane_Thread
)(thr
) );
2598 lk
= map_locks_maybe_lookup( (Addr
)rwl
);
2600 if (lk
== NULL
|| lk
->kind
!= LK_rdwr
) {
2601 HG_(record_error_Misc
)(
2602 thr
, "pthread_rwlock_destroy with invalid argument" );
2606 tl_assert( HG_(is_sane_LockN
)(lk
) );
2607 tl_assert( lk
->guestaddr
== (Addr
)rwl
);
2609 /* Basically act like we unlocked the lock */
2610 HG_(record_error_Misc
)(
2611 thr
, "pthread_rwlock_destroy of a locked mutex" );
2612 /* remove lock from locksets of all owning threads */
2613 remove_Lock_from_locksets_of_all_owning_Threads( lk
);
2614 VG_(deleteBag
)( lk
->heldBy
);
2617 lk
->acquired_at
= NULL
;
2619 tl_assert( !lk
->heldBy
);
2620 tl_assert( HG_(is_sane_LockN
)(lk
) );
2622 if (HG_(clo_track_lockorders
))
2623 laog__handle_one_lock_deletion(lk
);
2624 map_locks_delete( lk
->guestaddr
);
2628 if (HG_(clo_sanity_flags
) & SCE_LOCKS
)
2629 all__sanity_check("evh__hg_PTHREAD_RWLOCK_DESTROY_PRE");
2633 void evh__HG_PTHREAD_RWLOCK_LOCK_PRE ( ThreadId tid
,
2635 Word isW
, Word isTryLock
)
2637 /* Just check the rwl is sane; nothing else to do. */
2638 // 'rwl' may be invalid - not checked by wrapper
2641 if (SHOW_EVENTS
>= 1)
2642 VG_(printf
)("evh__hg_PTHREAD_RWLOCK_LOCK_PRE(ctid=%d, isW=%d, %p)\n",
2643 (Int
)tid
, (Int
)isW
, (void*)rwl
);
2645 tl_assert(isW
== 0 || isW
== 1); /* assured us by wrapper */
2646 tl_assert(isTryLock
== 0 || isTryLock
== 1); /* assured us by wrapper */
2647 thr
= map_threads_maybe_lookup( tid
);
2648 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2650 lk
= map_locks_maybe_lookup( (Addr
)rwl
);
2652 && (lk
->kind
== LK_nonRec
|| lk
->kind
== LK_mbRec
) ) {
2653 /* Wrong kind of lock. Duh. */
2654 HG_(record_error_Misc
)(
2655 thr
, "pthread_rwlock_{rd,rw}lock with a "
2656 "pthread_mutex_t* argument " );
2661 void evh__HG_PTHREAD_RWLOCK_LOCK_POST ( ThreadId tid
, void* rwl
, Word isW
)
2663 // only called if the real library call succeeded - so mutex is sane
2665 if (SHOW_EVENTS
>= 1)
2666 VG_(printf
)("evh__hg_PTHREAD_RWLOCK_LOCK_POST(ctid=%d, isW=%d, %p)\n",
2667 (Int
)tid
, (Int
)isW
, (void*)rwl
);
2669 tl_assert(isW
== 0 || isW
== 1); /* assured us by wrapper */
2670 thr
= map_threads_maybe_lookup( tid
);
2671 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2673 (isW
? evhH__post_thread_w_acquires_lock
2674 : evhH__post_thread_r_acquires_lock
)(
2676 LK_rdwr
, /* if not known, create new lock with this LockKind */
2681 static void evh__HG_PTHREAD_RWLOCK_UNLOCK_PRE ( ThreadId tid
, void* rwl
)
2683 // 'rwl' may be invalid - not checked by wrapper
2685 if (SHOW_EVENTS
>= 1)
2686 VG_(printf
)("evh__HG_PTHREAD_RWLOCK_UNLOCK_PRE(ctid=%d, rwl=%p)\n",
2687 (Int
)tid
, (void*)rwl
);
2689 thr
= map_threads_maybe_lookup( tid
);
2690 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2692 evhH__pre_thread_releases_lock( thr
, (Addr
)rwl
, True
/*isRDWR*/ );
2695 static void evh__HG_PTHREAD_RWLOCK_UNLOCK_POST ( ThreadId tid
, void* rwl
)
2697 // only called if the real library call succeeded - so mutex is sane
2699 if (SHOW_EVENTS
>= 1)
2700 VG_(printf
)("evh__hg_PTHREAD_RWLOCK_UNLOCK_POST(ctid=%d, rwl=%p)\n",
2701 (Int
)tid
, (void*)rwl
);
2702 thr
= map_threads_maybe_lookup( tid
);
2703 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2705 // anything we should do here?
2709 /* ---------------------------------------------------------- */
2710 /* -------------- events to do with semaphores -------------- */
2711 /* ---------------------------------------------------------- */
2713 /* This is similar to but not identical to the handling for condition
2716 /* For each semaphore, we maintain a stack of SOs. When a 'post'
2717 operation is done on a semaphore (unlocking, essentially), a new SO
2718 is created for the posting thread, the posting thread does a strong
2719 send to it (which merely installs the posting thread's VC in the
2720 SO), and the SO is pushed on the semaphore's stack.
2722 Later, when a (probably different) thread completes 'wait' on the
2723 semaphore, we pop a SO off the semaphore's stack (which should be
2724 nonempty), and do a strong recv from it. This mechanism creates
2725 dependencies between posters and waiters of the semaphore.
2727 It may not be necessary to use a stack - perhaps a bag of SOs would
2728 do. But we do need to keep track of how many unused-up posts have
2729 happened for the semaphore.
2731 Imagine T1 and T2 both post once on a semaphore S, and T3 waits
2732 twice on S. T3 cannot complete its waits without both T1 and T2
2733 posting. The above mechanism will ensure that T3 acquires
2734 dependencies on both T1 and T2.
2736 When a semaphore is initialised with value N, we do as if we'd
2737 posted N times on the semaphore: basically create N SOs and do a
2738 strong send to all of then. This allows up to N waits on the
2739 semaphore to acquire a dependency on the initialisation point,
2740 which AFAICS is the correct behaviour.
2742 We don't emit an error for DESTROY_PRE on a semaphore we don't know
2746 /* sem_t* -> XArray* SO* */
2747 static WordFM
* map_sem_to_SO_stack
= NULL
;
2749 static void map_sem_to_SO_stack_INIT ( void ) {
2750 if (map_sem_to_SO_stack
== NULL
) {
2751 map_sem_to_SO_stack
= VG_(newFM
)( HG_(zalloc
), "hg.mstSs.1",
2756 static void push_SO_for_sem ( void* sem
, SO
* so
) {
2760 map_sem_to_SO_stack_INIT();
2761 if (VG_(lookupFM
)( map_sem_to_SO_stack
,
2762 &keyW
, (UWord
*)&xa
, (UWord
)sem
)) {
2763 tl_assert(keyW
== (UWord
)sem
);
2765 VG_(addToXA
)( xa
, &so
);
2767 xa
= VG_(newXA
)( HG_(zalloc
), "hg.pSfs.1", HG_(free
), sizeof(SO
*) );
2768 VG_(addToXA
)( xa
, &so
);
2769 VG_(addToFM
)( map_sem_to_SO_stack
, (UWord
)sem
, (UWord
)xa
);
2773 static SO
* mb_pop_SO_for_sem ( void* sem
) {
2777 map_sem_to_SO_stack_INIT();
2778 if (VG_(lookupFM
)( map_sem_to_SO_stack
,
2779 &keyW
, (UWord
*)&xa
, (UWord
)sem
)) {
2780 /* xa is the stack for this semaphore. */
2782 tl_assert(keyW
== (UWord
)sem
);
2783 sz
= VG_(sizeXA
)( xa
);
2786 return NULL
; /* odd, the stack is empty */
2787 so
= *(SO
**)VG_(indexXA
)( xa
, sz
-1 );
2789 VG_(dropTailXA
)( xa
, 1 );
2792 /* hmm, that's odd. No stack for this semaphore. */
2797 static void evh__HG_POSIX_SEM_DESTROY_PRE ( ThreadId tid
, void* sem
)
2802 if (SHOW_EVENTS
>= 1)
2803 VG_(printf
)("evh__HG_POSIX_SEM_DESTROY_PRE(ctid=%d, sem=%p)\n",
2804 (Int
)tid
, (void*)sem
);
2806 map_sem_to_SO_stack_INIT();
2808 /* Empty out the semaphore's SO stack. This way of doing it is
2809 stupid, but at least it's easy. */
2811 so
= mb_pop_SO_for_sem( sem
);
2813 libhb_so_dealloc(so
);
2816 if (VG_(delFromFM
)( map_sem_to_SO_stack
, &keyW
, &valW
, (UWord
)sem
)) {
2817 XArray
* xa
= (XArray
*)valW
;
2818 tl_assert(keyW
== (UWord
)sem
);
2820 tl_assert(VG_(sizeXA
)(xa
) == 0); /* preceding loop just emptied it */
2826 void evh__HG_POSIX_SEM_INIT_POST ( ThreadId tid
, void* sem
, UWord value
)
2831 if (SHOW_EVENTS
>= 1)
2832 VG_(printf
)("evh__HG_POSIX_SEM_INIT_POST(ctid=%d, sem=%p, value=%lu)\n",
2833 (Int
)tid
, (void*)sem
, value
);
2835 thr
= map_threads_maybe_lookup( tid
);
2836 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2838 /* Empty out the semaphore's SO stack. This way of doing it is
2839 stupid, but at least it's easy. */
2841 so
= mb_pop_SO_for_sem( sem
);
2843 libhb_so_dealloc(so
);
2846 /* If we don't do this check, the following while loop runs us out
2847 of memory for stupid initial values of 'value'. */
2848 if (value
> 10000) {
2849 HG_(record_error_Misc
)(
2850 thr
, "sem_init: initial value exceeds 10000; using 10000" );
2854 /* Now create 'valid' new SOs for the thread, do a strong send to
2855 each of them, and push them all on the stack. */
2856 for (; value
> 0; value
--) {
2857 Thr
* hbthr
= thr
->hbthr
;
2860 so
= libhb_so_alloc();
2861 libhb_so_send( hbthr
, so
, True
/*strong send*/ );
2862 push_SO_for_sem( sem
, so
);
2866 static void evh__HG_POSIX_SEM_POST_PRE ( ThreadId tid
, void* sem
)
2868 /* 'tid' has posted on 'sem'. Create a new SO, do a strong send to
2869 it (iow, write our VC into it, then tick ours), and push the SO
2870 on on a stack of SOs associated with 'sem'. This is later used
2871 by other thread(s) which successfully exit from a sem_wait on
2872 the same sem; by doing a strong recv from SOs popped of the
2873 stack, they acquire dependencies on the posting thread
2880 if (SHOW_EVENTS
>= 1)
2881 VG_(printf
)("evh__HG_POSIX_SEM_POST_PRE(ctid=%d, sem=%p)\n",
2882 (Int
)tid
, (void*)sem
);
2884 thr
= map_threads_maybe_lookup( tid
);
2885 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2887 // error-if: sem is bogus
2892 so
= libhb_so_alloc();
2893 libhb_so_send( hbthr
, so
, True
/*strong send*/ );
2894 push_SO_for_sem( sem
, so
);
2897 static void evh__HG_POSIX_SEM_WAIT_POST ( ThreadId tid
, void* sem
)
2899 /* A sem_wait(sem) completed successfully. Pop the posting-SO for
2900 the 'sem' from this semaphore's SO-stack, and do a strong recv
2901 from it. This creates a dependency back to one of the post-ers
2902 for the semaphore. */
2908 if (SHOW_EVENTS
>= 1)
2909 VG_(printf
)("evh__HG_POSIX_SEM_WAIT_POST(ctid=%d, sem=%p)\n",
2910 (Int
)tid
, (void*)sem
);
2912 thr
= map_threads_maybe_lookup( tid
);
2913 tl_assert(thr
); /* cannot fail - Thread* must already exist */
2915 // error-if: sem is bogus
2917 so
= mb_pop_SO_for_sem( sem
);
2923 libhb_so_recv( hbthr
, so
, True
/*strong recv*/ );
2924 libhb_so_dealloc(so
);
2926 /* Hmm. How can a wait on 'sem' succeed if nobody posted to it?
2927 If this happened it would surely be a bug in the threads
2929 HG_(record_error_Misc
)(
2930 thr
, "Bug in libpthread: sem_wait succeeded on"
2931 " semaphore without prior sem_post");
2936 /* -------------------------------------------------------- */
2937 /* -------------- events to do with barriers -------------- */
2938 /* -------------------------------------------------------- */
2942 Bool initted
; /* has it yet been initted by guest? */
2943 Bool resizable
; /* is resizing allowed? */
2944 UWord size
; /* declared size */
2945 XArray
* waiting
; /* XA of Thread*. # present is 0 .. .size */
2949 static Bar
* new_Bar ( void ) {
2950 Bar
* bar
= HG_(zalloc
)( "hg.nB.1 (new_Bar)", sizeof(Bar
) );
2951 /* all fields are zero */
2952 tl_assert(bar
->initted
== False
);
2956 static void delete_Bar ( Bar
* bar
) {
2959 VG_(deleteXA
)(bar
->waiting
);
2963 /* A mapping which stores auxiliary data for barriers. */
2965 /* pthread_barrier_t* -> Bar* */
2966 static WordFM
* map_barrier_to_Bar
= NULL
;
2968 static void map_barrier_to_Bar_INIT ( void ) {
2969 if (UNLIKELY(map_barrier_to_Bar
== NULL
)) {
2970 map_barrier_to_Bar
= VG_(newFM
)( HG_(zalloc
),
2971 "hg.mbtBI.1", HG_(free
), NULL
);
2975 static Bar
* map_barrier_to_Bar_lookup_or_alloc ( void* barrier
) {
2977 map_barrier_to_Bar_INIT();
2978 if (VG_(lookupFM
)( map_barrier_to_Bar
, &key
, &val
, (UWord
)barrier
)) {
2979 tl_assert(key
== (UWord
)barrier
);
2982 Bar
* bar
= new_Bar();
2983 VG_(addToFM
)( map_barrier_to_Bar
, (UWord
)barrier
, (UWord
)bar
);
2988 static void map_barrier_to_Bar_delete ( void* barrier
) {
2990 map_barrier_to_Bar_INIT();
2991 if (VG_(delFromFM
)( map_barrier_to_Bar
, &keyW
, &valW
, (UWord
)barrier
)) {
2992 Bar
* bar
= (Bar
*)valW
;
2993 tl_assert(keyW
== (UWord
)barrier
);
2999 static void evh__HG_PTHREAD_BARRIER_INIT_PRE ( ThreadId tid
,
3007 if (SHOW_EVENTS
>= 1)
3008 VG_(printf
)("evh__HG_PTHREAD_BARRIER_INIT_PRE"
3009 "(tid=%d, barrier=%p, count=%lu, resizable=%lu)\n",
3010 (Int
)tid
, (void*)barrier
, count
, resizable
);
3012 thr
= map_threads_maybe_lookup( tid
);
3013 tl_assert(thr
); /* cannot fail - Thread* must already exist */
3016 HG_(record_error_Misc
)(
3017 thr
, "pthread_barrier_init: 'count' argument is zero"
3021 if (resizable
!= 0 && resizable
!= 1) {
3022 HG_(record_error_Misc
)(
3023 thr
, "pthread_barrier_init: invalid 'resizable' argument"
3027 bar
= map_barrier_to_Bar_lookup_or_alloc(barrier
);
3031 HG_(record_error_Misc
)(
3032 thr
, "pthread_barrier_init: barrier is already initialised"
3036 if (bar
->waiting
&& VG_(sizeXA
)(bar
->waiting
) > 0) {
3037 tl_assert(bar
->initted
);
3038 HG_(record_error_Misc
)(
3039 thr
, "pthread_barrier_init: threads are waiting at barrier"
3041 VG_(dropTailXA
)(bar
->waiting
, VG_(sizeXA
)(bar
->waiting
));
3043 if (!bar
->waiting
) {
3044 bar
->waiting
= VG_(newXA
)( HG_(zalloc
), "hg.eHPBIP.1", HG_(free
),
3048 tl_assert(VG_(sizeXA
)(bar
->waiting
) == 0);
3049 bar
->initted
= True
;
3050 bar
->resizable
= resizable
== 1 ? True
: False
;
3055 static void evh__HG_PTHREAD_BARRIER_DESTROY_PRE ( ThreadId tid
,
3061 /* Deal with destroy events. The only purpose is to free storage
3062 associated with the barrier, so as to avoid any possible
3064 if (SHOW_EVENTS
>= 1)
3065 VG_(printf
)("evh__HG_PTHREAD_BARRIER_DESTROY_PRE"
3066 "(tid=%d, barrier=%p)\n",
3067 (Int
)tid
, (void*)barrier
);
3069 thr
= map_threads_maybe_lookup( tid
);
3070 tl_assert(thr
); /* cannot fail - Thread* must already exist */
3072 bar
= map_barrier_to_Bar_lookup_or_alloc(barrier
);
3075 if (!bar
->initted
) {
3076 HG_(record_error_Misc
)(
3077 thr
, "pthread_barrier_destroy: barrier was never initialised"
3081 if (bar
->initted
&& bar
->waiting
&& VG_(sizeXA
)(bar
->waiting
) > 0) {
3082 HG_(record_error_Misc
)(
3083 thr
, "pthread_barrier_destroy: threads are waiting at barrier"
3087 /* Maybe we shouldn't do this; just let it persist, so that when it
3088 is reinitialised we don't need to do any dynamic memory
3089 allocation? The downside is a potentially unlimited space leak,
3090 if the client creates (in turn) a large number of barriers all
3091 at different locations. Note that if we do later move to the
3092 don't-delete-it scheme, we need to mark the barrier as
3093 uninitialised again since otherwise a later _init call will
3094 elicit a duplicate-init error. */
3095 map_barrier_to_Bar_delete( barrier
);
3099 /* All the threads have arrived. Now do the Interesting Bit. Get a
3100 new synchronisation object and do a weak send to it from all the
3101 participating threads. This makes its vector clocks be the join of
3102 all the individual threads' vector clocks. Then do a strong
3103 receive from it back to all threads, so that their VCs are a copy
3104 of it (hence are all equal to the join of their original VCs.) */
3105 static void do_barrier_cross_sync_and_empty ( Bar
* bar
)
3107 /* XXX check bar->waiting has no duplicates */
3109 SO
* so
= libhb_so_alloc();
3111 tl_assert(bar
->waiting
);
3112 tl_assert(VG_(sizeXA
)(bar
->waiting
) == bar
->size
);
3114 /* compute the join ... */
3115 for (i
= 0; i
< bar
->size
; i
++) {
3116 Thread
* t
= *(Thread
**)VG_(indexXA
)(bar
->waiting
, i
);
3117 Thr
* hbthr
= t
->hbthr
;
3118 libhb_so_send( hbthr
, so
, False
/*weak send*/ );
3120 /* ... and distribute to all threads */
3121 for (i
= 0; i
< bar
->size
; i
++) {
3122 Thread
* t
= *(Thread
**)VG_(indexXA
)(bar
->waiting
, i
);
3123 Thr
* hbthr
= t
->hbthr
;
3124 libhb_so_recv( hbthr
, so
, True
/*strong recv*/ );
3127 /* finally, we must empty out the waiting vector */
3128 VG_(dropTailXA
)(bar
->waiting
, VG_(sizeXA
)(bar
->waiting
));
3130 /* and we don't need this any more. Perhaps a stack-allocated
3131 SO would be better? */
3132 libhb_so_dealloc(so
);
3136 static void evh__HG_PTHREAD_BARRIER_WAIT_PRE ( ThreadId tid
,
3139 /* This function gets called after a client thread calls
3140 pthread_barrier_wait but before it arrives at the real
3141 pthread_barrier_wait.
3143 Why is the following correct? It's a bit subtle.
3145 If this is not the last thread arriving at the barrier, we simply
3146 note its presence and return. Because valgrind (at least as of
3147 Nov 08) is single threaded, we are guaranteed safe from any race
3148 conditions when in this function -- no other client threads are
3151 If this is the last thread, then we are again the only running
3152 thread. All the other threads will have either arrived at the
3153 real pthread_barrier_wait or are on their way to it, but in any
3154 case are guaranteed not to be able to move past it, because this
3155 thread is currently in this function and so has not yet arrived
3156 at the real pthread_barrier_wait. That means that:
3158 1. While we are in this function, none of the other threads
3159 waiting at the barrier can move past it.
3161 2. When this function returns (and simulated execution resumes),
3162 this thread and all other waiting threads will be able to move
3163 past the real barrier.
3165 Because of this, it is now safe to update the vector clocks of
3166 all threads, to represent the fact that they all arrived at the
3167 barrier and have all moved on. There is no danger of any
3168 complications to do with some threads leaving the barrier and
3169 racing back round to the front, whilst others are still leaving
3170 (which is the primary source of complication in correct handling/
3171 implementation of barriers). That can't happen because we update
3172 here our data structures so as to indicate that the threads have
3173 passed the barrier, even though, as per (2) above, they are
3174 guaranteed not to pass the barrier until we return.
3176 This relies crucially on Valgrind being single threaded. If that
3177 changes, this will need to be reconsidered.
3183 if (SHOW_EVENTS
>= 1)
3184 VG_(printf
)("evh__HG_PTHREAD_BARRIER_WAIT_PRE"
3185 "(tid=%d, barrier=%p)\n",
3186 (Int
)tid
, (void*)barrier
);
3188 thr
= map_threads_maybe_lookup( tid
);
3189 tl_assert(thr
); /* cannot fail - Thread* must already exist */
3191 bar
= map_barrier_to_Bar_lookup_or_alloc(barrier
);
3194 if (!bar
->initted
) {
3195 HG_(record_error_Misc
)(
3196 thr
, "pthread_barrier_wait: barrier is uninitialised"
3198 return; /* client is broken .. avoid assertions below */
3201 /* guaranteed by _INIT_PRE above */
3202 tl_assert(bar
->size
> 0);
3203 tl_assert(bar
->waiting
);
3205 VG_(addToXA
)( bar
->waiting
, &thr
);
3207 /* guaranteed by this function */
3208 present
= VG_(sizeXA
)(bar
->waiting
);
3209 tl_assert(present
> 0 && present
<= bar
->size
);
3211 if (present
< bar
->size
)
3214 do_barrier_cross_sync_and_empty(bar
);
3218 static void evh__HG_PTHREAD_BARRIER_RESIZE_PRE ( ThreadId tid
,
3226 if (SHOW_EVENTS
>= 1)
3227 VG_(printf
)("evh__HG_PTHREAD_BARRIER_RESIZE_PRE"
3228 "(tid=%d, barrier=%p, newcount=%lu)\n",
3229 (Int
)tid
, (void*)barrier
, newcount
);
3231 thr
= map_threads_maybe_lookup( tid
);
3232 tl_assert(thr
); /* cannot fail - Thread* must already exist */
3234 bar
= map_barrier_to_Bar_lookup_or_alloc(barrier
);
3237 if (!bar
->initted
) {
3238 HG_(record_error_Misc
)(
3239 thr
, "pthread_barrier_resize: barrier is uninitialised"
3241 return; /* client is broken .. avoid assertions below */
3244 if (!bar
->resizable
) {
3245 HG_(record_error_Misc
)(
3246 thr
, "pthread_barrier_resize: barrier is may not be resized"
3248 return; /* client is broken .. avoid assertions below */
3251 if (newcount
== 0) {
3252 HG_(record_error_Misc
)(
3253 thr
, "pthread_barrier_resize: 'newcount' argument is zero"
3255 return; /* client is broken .. avoid assertions below */
3258 /* guaranteed by _INIT_PRE above */
3259 tl_assert(bar
->size
> 0);
3260 tl_assert(bar
->waiting
);
3261 /* Guaranteed by this fn */
3262 tl_assert(newcount
> 0);
3264 if (newcount
>= bar
->size
) {
3265 /* Increasing the capacity. There's no possibility of threads
3266 moving on from the barrier in this situation, so just note
3267 the fact and do nothing more. */
3268 bar
->size
= newcount
;
3270 /* Decreasing the capacity. If we decrease it to be equal or
3271 below the number of waiting threads, they will now move past
3272 the barrier, so need to mess with dep edges in the same way
3273 as if the barrier had filled up normally. */
3274 present
= VG_(sizeXA
)(bar
->waiting
);
3275 tl_assert(present
>= 0 && present
<= bar
->size
);
3276 if (newcount
<= present
) {
3277 bar
->size
= present
; /* keep the cross_sync call happy */
3278 do_barrier_cross_sync_and_empty(bar
);
3280 bar
->size
= newcount
;
3285 /* ----------------------------------------------------- */
3286 /* ----- events to do with user-specified HB edges ----- */
3287 /* ----------------------------------------------------- */
3289 /* A mapping from arbitrary UWord tag to the SO associated with it.
3290 The UWord tags are meaningless to us, interpreted only by the
3296 static WordFM
* map_usertag_to_SO
= NULL
;
3298 static void map_usertag_to_SO_INIT ( void ) {
3299 if (UNLIKELY(map_usertag_to_SO
== NULL
)) {
3300 map_usertag_to_SO
= VG_(newFM
)( HG_(zalloc
),
3301 "hg.mutS.1", HG_(free
), NULL
);
3305 static SO
* map_usertag_to_SO_lookup_or_alloc ( UWord usertag
) {
3307 map_usertag_to_SO_INIT();
3308 if (VG_(lookupFM
)( map_usertag_to_SO
, &key
, &val
, usertag
)) {
3309 tl_assert(key
== (UWord
)usertag
);
3312 SO
* so
= libhb_so_alloc();
3313 VG_(addToFM
)( map_usertag_to_SO
, usertag
, (UWord
)so
);
3318 static void map_usertag_to_SO_delete ( UWord usertag
) {
3320 map_usertag_to_SO_INIT();
3321 if (VG_(delFromFM
)( map_usertag_to_SO
, &keyW
, &valW
, usertag
)) {
3323 tl_assert(keyW
== usertag
);
3325 libhb_so_dealloc(so
);
3331 void evh__HG_USERSO_SEND_PRE ( ThreadId tid
, UWord usertag
)
3333 /* TID is just about to notionally sent a message on a notional
3334 abstract synchronisation object whose identity is given by
3335 USERTAG. Bind USERTAG to a real SO if it is not already so
3336 bound, and do a 'weak send' on the SO. This joins the vector
3337 clocks from this thread into any vector clocks already present
3338 in the SO. The resulting SO vector clocks are later used by
3339 other thread(s) which successfully 'receive' from the SO,
3340 thereby acquiring a dependency on all the events that have
3341 previously signalled on this SO. */
3345 if (SHOW_EVENTS
>= 1)
3346 VG_(printf
)("evh__HG_USERSO_SEND_PRE(ctid=%d, usertag=%#lx)\n",
3347 (Int
)tid
, usertag
);
3349 thr
= map_threads_maybe_lookup( tid
);
3350 tl_assert(thr
); /* cannot fail - Thread* must already exist */
3352 so
= map_usertag_to_SO_lookup_or_alloc( usertag
);
3355 libhb_so_send( thr
->hbthr
, so
, False
/*!strong_send*/ );
3359 void evh__HG_USERSO_RECV_POST ( ThreadId tid
, UWord usertag
)
3361 /* TID has just notionally received a message from a notional
3362 abstract synchronisation object whose identity is given by
3363 USERTAG. Bind USERTAG to a real SO if it is not already so
3364 bound. If the SO has at some point in the past been 'sent' on,
3365 to a 'strong receive' on it, thereby acquiring a dependency on
3370 if (SHOW_EVENTS
>= 1)
3371 VG_(printf
)("evh__HG_USERSO_RECV_POST(ctid=%d, usertag=%#lx)\n",
3372 (Int
)tid
, usertag
);
3374 thr
= map_threads_maybe_lookup( tid
);
3375 tl_assert(thr
); /* cannot fail - Thread* must already exist */
3377 so
= map_usertag_to_SO_lookup_or_alloc( usertag
);
3380 /* Acquire a dependency on it. If the SO has never so far been
3381 sent on, then libhb_so_recv will do nothing. So we're safe
3382 regardless of SO's history. */
3383 libhb_so_recv( thr
->hbthr
, so
, True
/*strong_recv*/ );
3387 void evh__HG_USERSO_FORGET_ALL ( ThreadId tid
, UWord usertag
)
3389 /* TID declares that any happens-before edges notionally stored in
3390 USERTAG can be deleted. If (as would normally be the case) a
3391 SO is associated with USERTAG, then the association is removed
3392 and all resources associated with SO are freed. Importantly,
3393 that frees up any VTSs stored in SO. */
3394 if (SHOW_EVENTS
>= 1)
3395 VG_(printf
)("evh__HG_USERSO_FORGET_ALL(ctid=%d, usertag=%#lx)\n",
3396 (Int
)tid
, usertag
);
3398 map_usertag_to_SO_delete( usertag
);
3402 #if defined(VGO_solaris)
3403 /* ----------------------------------------------------- */
3404 /* --- events to do with bind guard/clear intercepts --- */
3405 /* ----------------------------------------------------- */
3408 void evh__HG_RTLD_BIND_GUARD(ThreadId tid
, Int flags
)
3410 if (SHOW_EVENTS
>= 1)
3411 VG_(printf
)("evh__HG_RTLD_BIND_GUARD"
3412 "(tid=%d, flags=%d)\n",
3415 Thread
*thr
= map_threads_maybe_lookup(tid
);
3416 tl_assert(thr
!= NULL
);
3418 Int bindflag
= (flags
& VKI_THR_FLG_RTLD
);
3419 if ((bindflag
& thr
->bind_guard_flag
) == 0) {
3420 thr
->bind_guard_flag
|= bindflag
;
3421 HG_(thread_enter_synchr
)(thr
);
3422 /* Misuse pthread_create_nesting_level for ignoring mutex activity. */
3423 HG_(thread_enter_pthread_create
)(thr
);
3428 void evh__HG_RTLD_BIND_CLEAR(ThreadId tid
, Int flags
)
3430 if (SHOW_EVENTS
>= 1)
3431 VG_(printf
)("evh__HG_RTLD_BIND_CLEAR"
3432 "(tid=%d, flags=%d)\n",
3435 Thread
*thr
= map_threads_maybe_lookup(tid
);
3436 tl_assert(thr
!= NULL
);
3438 Int bindflag
= (flags
& VKI_THR_FLG_RTLD
);
3439 if ((thr
->bind_guard_flag
& bindflag
) != 0) {
3440 thr
->bind_guard_flag
&= ~bindflag
;
3441 HG_(thread_leave_synchr
)(thr
);
3442 HG_(thread_leave_pthread_create
)(thr
);
3445 #endif /* VGO_solaris */
3448 /*--------------------------------------------------------------*/
3449 /*--- Lock acquisition order monitoring ---*/
3450 /*--------------------------------------------------------------*/
3452 /* FIXME: here are some optimisations still to do in
3453 laog__pre_thread_acquires_lock.
3455 The graph is structured so that if L1 --*--> L2 then L1 must be
3458 The common case is that some thread T holds (eg) L1 L2 and L3 and
3459 is repeatedly acquiring and releasing Ln, and there is no ordering
3460 error in what it is doing. Hence it repeatly:
3462 (1) searches laog to see if Ln --*--> {L1,L2,L3}, which always
3463 produces the answer No (because there is no error).
3465 (2) adds edges {L1,L2,L3} --> Ln to laog, which are already present
3466 (because they already got added the first time T acquired Ln).
3468 Hence cache these two events:
3470 (1) Cache result of the query from last time. Invalidate the cache
3471 any time any edges are added to or deleted from laog.
3473 (2) Cache these add-edge requests and ignore them if said edges
3474 have already been added to laog. Invalidate the cache any time
3475 any edges are deleted from laog.
3480 WordSetID inns
; /* in univ_laog */
3481 WordSetID outs
; /* in univ_laog */
3485 /* lock order acquisition graph */
3486 static WordFM
* laog
= NULL
; /* WordFM Lock* LAOGLinks* */
3488 /* EXPOSITION ONLY: for each edge in 'laog', record the two places
3489 where that edge was created, so that we can show the user later if
3493 Addr src_ga
; /* Lock guest addresses for */
3494 Addr dst_ga
; /* src/dst of the edge */
3495 ExeContext
* src_ec
; /* And corresponding places where that */
3496 ExeContext
* dst_ec
; /* ordering was established */
3500 static Word
cmp_LAOGLinkExposition ( UWord llx1W
, UWord llx2W
) {
3501 /* Compare LAOGLinkExposition*s by (src_ga,dst_ga) field pair. */
3502 LAOGLinkExposition
* llx1
= (LAOGLinkExposition
*)llx1W
;
3503 LAOGLinkExposition
* llx2
= (LAOGLinkExposition
*)llx2W
;
3504 if (llx1
->src_ga
< llx2
->src_ga
) return -1;
3505 if (llx1
->src_ga
> llx2
->src_ga
) return 1;
3506 if (llx1
->dst_ga
< llx2
->dst_ga
) return -1;
3507 if (llx1
->dst_ga
> llx2
->dst_ga
) return 1;
3511 static WordFM
* laog_exposition
= NULL
; /* WordFM LAOGLinkExposition* NULL */
3512 /* end EXPOSITION ONLY */
3515 __attribute__((noinline
))
3516 static void laog__init ( void )
3519 tl_assert(!laog_exposition
);
3520 tl_assert(HG_(clo_track_lockorders
));
3522 laog
= VG_(newFM
)( HG_(zalloc
), "hg.laog__init.1",
3523 HG_(free
), NULL
/*unboxedcmp*/ );
3525 laog_exposition
= VG_(newFM
)( HG_(zalloc
), "hg.laog__init.2", HG_(free
),
3526 cmp_LAOGLinkExposition
);
3529 static void laog__show ( const HChar
* who
) {
3534 VG_(printf
)("laog (requested by %s) {\n", who
);
3535 VG_(initIterFM
)( laog
);
3538 while (VG_(nextIterFM
)( laog
, (UWord
*)&me
,
3542 VG_(printf
)(" node %p:\n", me
);
3543 HG_(getPayloadWS
)( &ws_words
, &ws_size
, univ_laog
, links
->inns
);
3544 for (i
= 0; i
< ws_size
; i
++)
3545 VG_(printf
)(" inn %#lx\n", ws_words
[i
] );
3546 HG_(getPayloadWS
)( &ws_words
, &ws_size
, univ_laog
, links
->outs
);
3547 for (i
= 0; i
< ws_size
; i
++)
3548 VG_(printf
)(" out %#lx\n", ws_words
[i
] );
3552 VG_(doneIterFM
)( laog
);
3556 static void univ_laog_do_GC ( void ) {
3560 Int prev_next_gc_univ_laog
= next_gc_univ_laog
;
3561 const UWord univ_laog_cardinality
= HG_(cardinalityWSU
)( univ_laog
);
3563 Bool
*univ_laog_seen
= HG_(zalloc
) ( "hg.gc_univ_laog.1",
3564 (Int
) univ_laog_cardinality
3566 // univ_laog_seen[*] set to 0 (False) by zalloc.
3568 VG_(initIterFM
)( laog
);
3570 while (VG_(nextIterFM
)( laog
, NULL
, (UWord
*)&links
)) {
3572 tl_assert(links
->inns
>= 0 && links
->inns
< univ_laog_cardinality
);
3573 univ_laog_seen
[links
->inns
] = True
;
3574 tl_assert(links
->outs
>= 0 && links
->outs
< univ_laog_cardinality
);
3575 univ_laog_seen
[links
->outs
] = True
;
3578 VG_(doneIterFM
)( laog
);
3580 for (i
= 0; i
< (Int
)univ_laog_cardinality
; i
++) {
3581 if (univ_laog_seen
[i
])
3584 HG_(dieWS
) ( univ_laog
, (WordSet
)i
);
3587 HG_(free
) (univ_laog_seen
);
3589 // We need to decide the value of the next_gc.
3590 // 3 solutions were looked at:
3591 // Sol 1: garbage collect at seen * 2
3592 // This solution was a lot slower, probably because we both do a lot of
3593 // garbage collection and do not keep long enough laog WV that will become
3594 // useful again very soon.
3595 // Sol 2: garbage collect at a percentage increase of the current cardinality
3596 // (with a min increase of 1)
3597 // Trials on a small test program with 1%, 5% and 10% increase was done.
3598 // 1% is slightly faster than 5%, which is slightly slower than 10%.
3599 // However, on a big application, this caused the memory to be exhausted,
3600 // as even a 1% increase of size at each gc becomes a lot, when many gc
3602 // Sol 3: always garbage collect at current cardinality + 1.
3603 // This solution was the fastest of the 3 solutions, and caused no memory
3604 // exhaustion in the big application.
3606 // With regards to cost introduced by gc: on the t2t perf test (doing only
3607 // lock/unlock operations), t2t 50 10 2 was about 25% faster than the
3608 // version with garbage collection. With t2t 50 20 2, my machine started
3609 // to page out, and so the garbage collected version was much faster.
3610 // On smaller lock sets (e.g. t2t 20 5 2, giving about 100 locks), the
3611 // difference performance is insignificant (~ 0.1 s).
3612 // Of course, it might be that real life programs are not well represented
3615 // If ever we want to have a more sophisticated control
3616 // (e.g. clo options to control the percentage increase or fixed increased),
3617 // we should do it here, eg.
3618 // next_gc_univ_laog = prev_next_gc_univ_laog + VG_(clo_laog_gc_fixed);
3619 // Currently, we just hard-code the solution 3 above.
3620 next_gc_univ_laog
= prev_next_gc_univ_laog
+ 1;
3625 "univ_laog_do_GC cardinality entered %d exit %d next gc at %d\n",
3626 (Int
)univ_laog_cardinality
, (Int
)seen
, next_gc_univ_laog
);
3630 __attribute__((noinline
))
3631 static void laog__add_edge ( Lock
* src
, Lock
* dst
) {
3634 Bool presentF
, presentR
;
3635 if (0) VG_(printf
)("laog__add_edge %p %p\n", src
, dst
);
3637 /* Take the opportunity to sanity check the graph. Record in
3638 presentF if there is already a src->dst mapping in this node's
3639 forwards links, and presentR if there is already a src->dst
3640 mapping in this node's backwards links. They should agree!
3641 Also, we need to know whether the edge was already present so as
3642 to decide whether or not to update the link details mapping. We
3643 can compute presentF and presentR essentially for free, so may
3644 as well do this always. */
3645 presentF
= presentR
= False
;
3647 /* Update the out edges for src */
3650 if (VG_(lookupFM
)( laog
, &keyW
, (UWord
*)&links
, (UWord
)src
)) {
3653 tl_assert(keyW
== (UWord
)src
);
3654 outs_new
= HG_(addToWS
)( univ_laog
, links
->outs
, (UWord
)dst
);
3655 presentF
= outs_new
== links
->outs
;
3656 links
->outs
= outs_new
;
3658 links
= HG_(zalloc
)("hg.lae.1", sizeof(LAOGLinks
));
3659 links
->inns
= HG_(emptyWS
)( univ_laog
);
3660 links
->outs
= HG_(singletonWS
)( univ_laog
, (UWord
)dst
);
3661 VG_(addToFM
)( laog
, (UWord
)src
, (UWord
)links
);
3663 /* Update the in edges for dst */
3666 if (VG_(lookupFM
)( laog
, &keyW
, (UWord
*)&links
, (UWord
)dst
)) {
3669 tl_assert(keyW
== (UWord
)dst
);
3670 inns_new
= HG_(addToWS
)( univ_laog
, links
->inns
, (UWord
)src
);
3671 presentR
= inns_new
== links
->inns
;
3672 links
->inns
= inns_new
;
3674 links
= HG_(zalloc
)("hg.lae.2", sizeof(LAOGLinks
));
3675 links
->inns
= HG_(singletonWS
)( univ_laog
, (UWord
)src
);
3676 links
->outs
= HG_(emptyWS
)( univ_laog
);
3677 VG_(addToFM
)( laog
, (UWord
)dst
, (UWord
)links
);
3680 tl_assert( (presentF
&& presentR
) || (!presentF
&& !presentR
) );
3682 if (!presentF
&& src
->acquired_at
&& dst
->acquired_at
) {
3683 LAOGLinkExposition expo
;
3684 /* If this edge is entering the graph, and we have acquired_at
3685 information for both src and dst, record those acquisition
3686 points. Hence, if there is later a violation of this
3687 ordering, we can show the user the two places in which the
3688 required src-dst ordering was previously established. */
3689 if (0) VG_(printf
)("acquire edge %#lx %#lx\n",
3690 src
->guestaddr
, dst
->guestaddr
);
3691 expo
.src_ga
= src
->guestaddr
;
3692 expo
.dst_ga
= dst
->guestaddr
;
3695 tl_assert(laog_exposition
);
3696 if (VG_(lookupFM
)( laog_exposition
, NULL
, NULL
, (UWord
)&expo
)) {
3697 /* we already have it; do nothing */
3699 LAOGLinkExposition
* expo2
= HG_(zalloc
)("hg.lae.3",
3700 sizeof(LAOGLinkExposition
));
3701 expo2
->src_ga
= src
->guestaddr
;
3702 expo2
->dst_ga
= dst
->guestaddr
;
3703 expo2
->src_ec
= src
->acquired_at
;
3704 expo2
->dst_ec
= dst
->acquired_at
;
3705 VG_(addToFM
)( laog_exposition
, (UWord
)expo2
, (UWord
)NULL
);
3709 if (HG_(cardinalityWSU
) (univ_laog
) >= next_gc_univ_laog
)
3713 __attribute__((noinline
))
3714 static void laog__del_edge ( Lock
* src
, Lock
* dst
) {
3717 if (0) VG_(printf
)("laog__del_edge enter %p %p\n", src
, dst
);
3718 /* Update the out edges for src */
3721 if (VG_(lookupFM
)( laog
, &keyW
, (UWord
*)&links
, (UWord
)src
)) {
3723 tl_assert(keyW
== (UWord
)src
);
3724 links
->outs
= HG_(delFromWS
)( univ_laog
, links
->outs
, (UWord
)dst
);
3726 /* Update the in edges for dst */
3729 if (VG_(lookupFM
)( laog
, &keyW
, (UWord
*)&links
, (UWord
)dst
)) {
3731 tl_assert(keyW
== (UWord
)dst
);
3732 links
->inns
= HG_(delFromWS
)( univ_laog
, links
->inns
, (UWord
)src
);
3735 /* Remove the exposition of src,dst (if present) */
3737 LAOGLinkExposition
*fm_expo
;
3739 LAOGLinkExposition expo
;
3740 expo
.src_ga
= src
->guestaddr
;
3741 expo
.dst_ga
= dst
->guestaddr
;
3745 if (VG_(delFromFM
) (laog_exposition
,
3746 (UWord
*)&fm_expo
, NULL
, (UWord
)&expo
)) {
3747 HG_(free
) (fm_expo
);
3751 /* deleting edges can increase nr of of WS so check for gc. */
3752 if (HG_(cardinalityWSU
) (univ_laog
) >= next_gc_univ_laog
)
3754 if (0) VG_(printf
)("laog__del_edge exit\n");
3757 __attribute__((noinline
))
3758 static WordSetID
/* in univ_laog */ laog__succs ( Lock
* lk
) {
3763 if (VG_(lookupFM
)( laog
, &keyW
, (UWord
*)&links
, (UWord
)lk
)) {
3765 tl_assert(keyW
== (UWord
)lk
);
3768 return HG_(emptyWS
)( univ_laog
);
3772 __attribute__((noinline
))
3773 static WordSetID
/* in univ_laog */ laog__preds ( Lock
* lk
) {
3778 if (VG_(lookupFM
)( laog
, &keyW
, (UWord
*)&links
, (UWord
)lk
)) {
3780 tl_assert(keyW
== (UWord
)lk
);
3783 return HG_(emptyWS
)( univ_laog
);
3787 __attribute__((noinline
))
3788 static void laog__sanity_check ( const HChar
* who
) {
3793 VG_(initIterFM
)( laog
);
3796 if (0) VG_(printf
)("laog sanity check\n");
3797 while (VG_(nextIterFM
)( laog
, (UWord
*)&me
,
3801 HG_(getPayloadWS
)( &ws_words
, &ws_size
, univ_laog
, links
->inns
);
3802 for (i
= 0; i
< ws_size
; i
++) {
3803 if ( ! HG_(elemWS
)( univ_laog
,
3804 laog__succs( (Lock
*)ws_words
[i
] ),
3808 HG_(getPayloadWS
)( &ws_words
, &ws_size
, univ_laog
, links
->outs
);
3809 for (i
= 0; i
< ws_size
; i
++) {
3810 if ( ! HG_(elemWS
)( univ_laog
,
3811 laog__preds( (Lock
*)ws_words
[i
] ),
3818 VG_(doneIterFM
)( laog
);
3822 VG_(printf
)("laog__sanity_check(%s) FAILED\n", who
);
3827 /* If there is a path in laog from 'src' to any of the elements in
3828 'dst', return an arbitrarily chosen element of 'dst' reachable from
3829 'src'. If no path exist from 'src' to any element in 'dst', return
3831 __attribute__((noinline
))
3833 Lock
* laog__do_dfs_from_to ( Lock
* src
, WordSetID dsts
/* univ_lsets */ )
3837 XArray
* stack
; /* of Lock* */
3838 WordFM
* visited
; /* Lock* -> void, iow, Set(Lock*) */
3841 UWord succs_size
, i
;
3843 //laog__sanity_check();
3845 /* If the destination set is empty, we can never get there from
3846 'src' :-), so don't bother to try */
3847 if (HG_(isEmptyWS
)( univ_lsets
, dsts
))
3851 stack
= VG_(newXA
)( HG_(zalloc
), "hg.lddft.1", HG_(free
), sizeof(Lock
*) );
3852 visited
= VG_(newFM
)( HG_(zalloc
), "hg.lddft.2", HG_(free
), NULL
/*unboxedcmp*/ );
3854 (void) VG_(addToXA
)( stack
, &src
);
3858 ssz
= VG_(sizeXA
)( stack
);
3860 if (ssz
== 0) { ret
= NULL
; break; }
3862 here
= *(Lock
**) VG_(indexXA
)( stack
, ssz
-1 );
3863 VG_(dropTailXA
)( stack
, 1 );
3865 if (HG_(elemWS
)( univ_lsets
, dsts
, (UWord
)here
)) { ret
= here
; break; }
3867 if (VG_(lookupFM
)( visited
, NULL
, NULL
, (UWord
)here
))
3870 VG_(addToFM
)( visited
, (UWord
)here
, 0 );
3872 succs
= laog__succs( here
);
3873 HG_(getPayloadWS
)( &succs_words
, &succs_size
, univ_laog
, succs
);
3874 for (i
= 0; i
< succs_size
; i
++)
3875 (void) VG_(addToXA
)( stack
, &succs_words
[i
] );
3878 VG_(deleteFM
)( visited
, NULL
, NULL
);
3879 VG_(deleteXA
)( stack
);
3884 /* Thread 'thr' is acquiring 'lk'. Check for inconsistent ordering
3885 between 'lk' and the locks already held by 'thr' and issue a
3886 complaint if so. Also, update the ordering graph appropriately.
3888 __attribute__((noinline
))
3889 static void laog__pre_thread_acquires_lock (
3890 Thread
* thr
, /* NB: BEFORE lock is added */
3898 /* It may be that 'thr' already holds 'lk' and is recursively
3899 relocking in. In this case we just ignore the call. */
3900 /* NB: univ_lsets really is correct here */
3901 if (HG_(elemWS
)( univ_lsets
, thr
->locksetA
, (UWord
)lk
))
3904 /* First, the check. Complain if there is any path in laog from lk
3905 to any of the locks already held by thr, since if any such path
3906 existed, it would mean that previously lk was acquired before
3907 (rather than after, as we are doing here) at least one of those
3910 other
= laog__do_dfs_from_to(lk
, thr
->locksetA
);
3912 LAOGLinkExposition key
, *found
;
3913 /* So we managed to find a path lk --*--> other in the graph,
3914 which implies that 'lk' should have been acquired before
3915 'other' but is in fact being acquired afterwards. We present
3916 the lk/other arguments to record_error_LockOrder in the order
3917 in which they should have been acquired. */
3918 /* Go look in the laog_exposition mapping, to find the allocation
3919 points for this edge, so we can show the user. */
3920 key
.src_ga
= lk
->guestaddr
;
3921 key
.dst_ga
= other
->guestaddr
;
3925 if (VG_(lookupFM
)( laog_exposition
,
3926 (UWord
*)&found
, NULL
, (UWord
)&key
)) {
3927 tl_assert(found
!= &key
);
3928 tl_assert(found
->src_ga
== key
.src_ga
);
3929 tl_assert(found
->dst_ga
== key
.dst_ga
);
3930 tl_assert(found
->src_ec
);
3931 tl_assert(found
->dst_ec
);
3932 HG_(record_error_LockOrder
)(
3934 found
->src_ec
, found
->dst_ec
, other
->acquired_at
);
3936 /* Hmm. This can't happen (can it?) */
3937 /* Yes, it can happen: see tests/tc14_laog_dinphils.
3938 Imagine we have 3 philosophers A B C, and the forks
3947 Let's have the following actions:
3955 Helgrind will report a lock order error when C takes fCA.
3956 Effectively, we have a deadlock if the following
3962 The error reported is:
3963 Observed (incorrect) order fBC followed by fCA
3964 but the stack traces that have established the required order
3967 This is because there is no pair (fCA, fBC) in laog exposition :
3968 the laog_exposition records all pairs of locks between a new lock
3969 taken by a thread and all the already taken locks.
3970 So, there is no laog_exposition (fCA, fBC) as no thread ever
3971 first locked fCA followed by fBC.
3973 In other words, when the deadlock cycle involves more than
3974 two locks, then helgrind does not report the sequence of
3975 operations that created the cycle.
3977 However, we can report the current stack trace (where
3978 lk is being taken), and the stack trace where other was acquired:
3979 Effectively, the variable 'other' contains a lock currently
3980 held by this thread, with its 'acquired_at'. */
3982 HG_(record_error_LockOrder
)(
3984 NULL
, NULL
, other
->acquired_at
);
3988 /* Second, add to laog the pairs
3989 (old, lk) | old <- locks already held by thr
3990 Since both old and lk are currently held by thr, their acquired_at
3991 fields must be non-NULL.
3993 tl_assert(lk
->acquired_at
);
3994 HG_(getPayloadWS
)( &ls_words
, &ls_size
, univ_lsets
, thr
->locksetA
);
3995 for (i
= 0; i
< ls_size
; i
++) {
3996 Lock
* old
= (Lock
*)ls_words
[i
];
3997 tl_assert(old
->acquired_at
);
3998 laog__add_edge( old
, lk
);
4001 /* Why "except_Locks" ? We're here because a lock is being
4002 acquired by a thread, and we're in an inconsistent state here.
4003 See the call points in evhH__post_thread_{r,w}_acquires_lock.
4004 When called in this inconsistent state, locks__sanity_check duly
4006 if (HG_(clo_sanity_flags
) & SCE_LAOG
)
4007 all_except_Locks__sanity_check("laog__pre_thread_acquires_lock-post");
4010 /* Allocates a duplicate of words. Caller must HG_(free) the result. */
4011 static UWord
* UWordV_dup(UWord
* words
, Word words_size
)
4015 if (words_size
== 0)
4018 UWord
*dup
= HG_(zalloc
) ("hg.dup.1", (SizeT
) words_size
* sizeof(UWord
));
4020 for (i
= 0; i
< words_size
; i
++)
4026 /* Delete from 'laog' any pair mentioning a lock in locksToDelete */
4028 __attribute__((noinline
))
4029 static void laog__handle_one_lock_deletion ( Lock
* lk
)
4031 WordSetID preds
, succs
;
4032 UWord preds_size
, succs_size
, i
, j
;
4033 UWord
*preds_words
, *succs_words
;
4035 preds
= laog__preds( lk
);
4036 succs
= laog__succs( lk
);
4038 // We need to duplicate the payload, as these can be garbage collected
4039 // during the del/add operations below.
4040 HG_(getPayloadWS
)( &preds_words
, &preds_size
, univ_laog
, preds
);
4041 preds_words
= UWordV_dup(preds_words
, preds_size
);
4043 HG_(getPayloadWS
)( &succs_words
, &succs_size
, univ_laog
, succs
);
4044 succs_words
= UWordV_dup(succs_words
, succs_size
);
4046 for (i
= 0; i
< preds_size
; i
++)
4047 laog__del_edge( (Lock
*)preds_words
[i
], lk
);
4049 for (j
= 0; j
< succs_size
; j
++)
4050 laog__del_edge( lk
, (Lock
*)succs_words
[j
] );
4052 for (i
= 0; i
< preds_size
; i
++) {
4053 for (j
= 0; j
< succs_size
; j
++) {
4054 if (preds_words
[i
] != succs_words
[j
]) {
4055 /* This can pass unlocked locks to laog__add_edge, since
4056 we're deleting stuff. So their acquired_at fields may
4058 laog__add_edge( (Lock
*)preds_words
[i
], (Lock
*)succs_words
[j
] );
4064 HG_(free
) (preds_words
);
4066 HG_(free
) (succs_words
);
4068 // Remove lk information from laog links FM
4073 if (VG_(delFromFM
) (laog
,
4074 (UWord
*)&linked_lk
, (UWord
*)&links
, (UWord
)lk
)) {
4075 tl_assert (linked_lk
== lk
);
4079 /* FIXME ??? What about removing lock lk data from EXPOSITION ??? */
4082 //__attribute__((noinline))
4083 //static void laog__handle_lock_deletions (
4084 // WordSetID /* in univ_laog */ locksToDelete
4091 // HG_(getPayloadWS)( &ws_words, &ws_size, univ_lsets, locksToDelete );
4092 // UWordV_dup call needed here ...
4093 // for (i = 0; i < ws_size; i++)
4094 // laog__handle_one_lock_deletion( (Lock*)ws_words[i] );
4096 // if (HG_(clo_sanity_flags) & SCE_LAOG)
4097 // all__sanity_check("laog__handle_lock_deletions-post");
4101 /*--------------------------------------------------------------*/
4102 /*--- Malloc/free replacements ---*/
4103 /*--------------------------------------------------------------*/
4107 void* next
; /* required by m_hashtable */
4108 Addr payload
; /* ptr to actual block */
4109 SizeT szB
; /* size requested */
4110 ExeContext
* where
; /* where it was allocated */
4111 Thread
* thr
; /* allocating thread */
4115 /* A hash table of MallocMetas, used to track malloc'd blocks
4117 static VgHashTable
*hg_mallocmeta_table
= NULL
;
4119 /* MallocMeta are small elements. We use a pool to avoid
4120 the overhead of malloc for each MallocMeta. */
4121 static PoolAlloc
*MallocMeta_poolalloc
= NULL
;
4123 static MallocMeta
* new_MallocMeta ( void ) {
4124 MallocMeta
* md
= VG_(allocEltPA
) (MallocMeta_poolalloc
);
4125 VG_(memset
)(md
, 0, sizeof(MallocMeta
));
4128 static void delete_MallocMeta ( MallocMeta
* md
) {
4129 VG_(freeEltPA
)(MallocMeta_poolalloc
, md
);
4133 /* Allocate a client block and set up the metadata for it. */
4136 void* handle_alloc ( ThreadId tid
,
4137 SizeT szB
, SizeT alignB
, Bool is_zeroed
)
4142 tl_assert( ((SSizeT
)szB
) >= 0 );
4143 p
= (Addr
)VG_(cli_malloc
)(alignB
, szB
);
4148 VG_(memset
)((void*)p
, 0, szB
);
4150 /* Note that map_threads_lookup must succeed (cannot assert), since
4151 memory can only be allocated by currently alive threads, hence
4152 they must have an entry in map_threads. */
4153 md
= new_MallocMeta();
4156 md
->where
= VG_(record_ExeContext
)( tid
, 0 );
4157 md
->thr
= map_threads_lookup( tid
);
4159 VG_(HT_add_node
)( hg_mallocmeta_table
, (VgHashNode
*)md
);
4161 /* Tell the lower level memory wranglers. */
4162 evh__new_mem_heap( p
, szB
, is_zeroed
);
4167 /* Re the checks for less-than-zero (also in hg_cli__realloc below):
4168 Cast to a signed type to catch any unexpectedly negative args.
4169 We're assuming here that the size asked for is not greater than
4170 2^31 bytes (for 32-bit platforms) or 2^63 bytes (for 64-bit
4172 static void* hg_cli__malloc ( ThreadId tid
, SizeT n
) {
4173 if (((SSizeT
)n
) < 0) return NULL
;
4174 return handle_alloc ( tid
, n
, VG_(clo_alignment
),
4175 /*is_zeroed*/False
);
4177 static void* hg_cli____builtin_new ( ThreadId tid
, SizeT n
) {
4178 if (((SSizeT
)n
) < 0) return NULL
;
4179 return handle_alloc ( tid
, n
, VG_(clo_alignment
),
4180 /*is_zeroed*/False
);
4182 static void* hg_cli____builtin_vec_new ( ThreadId tid
, SizeT n
) {
4183 if (((SSizeT
)n
) < 0) return NULL
;
4184 return handle_alloc ( tid
, n
, VG_(clo_alignment
),
4185 /*is_zeroed*/False
);
4187 static void* hg_cli__memalign ( ThreadId tid
, SizeT align
, SizeT n
) {
4188 if (((SSizeT
)n
) < 0) return NULL
;
4189 return handle_alloc ( tid
, n
, align
,
4190 /*is_zeroed*/False
);
4192 static void* hg_cli__calloc ( ThreadId tid
, SizeT nmemb
, SizeT size1
) {
4193 if ( ((SSizeT
)nmemb
) < 0 || ((SSizeT
)size1
) < 0 ) return NULL
;
4194 return handle_alloc ( tid
, nmemb
*size1
, VG_(clo_alignment
),
4195 /*is_zeroed*/True
);
4199 /* Free a client block, including getting rid of the relevant
4202 static void handle_free ( ThreadId tid
, void* p
)
4204 MallocMeta
*md
, *old_md
;
4207 /* First see if we can find the metadata for 'p'. */
4208 md
= (MallocMeta
*) VG_(HT_lookup
)( hg_mallocmeta_table
, (UWord
)p
);
4210 return; /* apparently freeing a bogus address. Oh well. */
4212 tl_assert(md
->payload
== (Addr
)p
);
4215 /* Nuke the metadata block */
4216 old_md
= (MallocMeta
*)
4217 VG_(HT_remove
)( hg_mallocmeta_table
, (UWord
)p
);
4218 tl_assert(old_md
); /* it must be present - we just found it */
4219 tl_assert(old_md
== md
);
4220 tl_assert(old_md
->payload
== (Addr
)p
);
4222 VG_(cli_free
)((void*)old_md
->payload
);
4223 delete_MallocMeta(old_md
);
4225 /* Tell the lower level memory wranglers. */
4226 evh__die_mem_heap( (Addr
)p
, szB
);
4229 static void hg_cli__free ( ThreadId tid
, void* p
) {
4230 handle_free(tid
, p
);
4232 static void hg_cli____builtin_delete ( ThreadId tid
, void* p
) {
4233 handle_free(tid
, p
);
4235 static void hg_cli____builtin_vec_delete ( ThreadId tid
, void* p
) {
4236 handle_free(tid
, p
);
4240 static void* hg_cli__realloc ( ThreadId tid
, void* payloadV
, SizeT new_size
)
4242 MallocMeta
*md
, *md_new
, *md_tmp
;
4245 Addr payload
= (Addr
)payloadV
;
4247 if (((SSizeT
)new_size
) < 0) return NULL
;
4249 md
= (MallocMeta
*) VG_(HT_lookup
)( hg_mallocmeta_table
, (UWord
)payload
);
4251 return NULL
; /* apparently realloc-ing a bogus address. Oh well. */
4253 tl_assert(md
->payload
== payload
);
4255 if (md
->szB
== new_size
) {
4256 /* size unchanged */
4257 md
->where
= VG_(record_ExeContext
)(tid
, 0);
4261 if (md
->szB
> new_size
) {
4262 /* new size is smaller */
4264 md
->where
= VG_(record_ExeContext
)(tid
, 0);
4265 evh__die_mem_heap( md
->payload
+ new_size
, md
->szB
- new_size
);
4270 /* new size is bigger */
4271 Addr p_new
= (Addr
)VG_(cli_malloc
)(VG_(clo_alignment
), new_size
);
4273 /* First half kept and copied, second half new */
4274 // FIXME: shouldn't we use a copier which implements the
4275 // memory state machine?
4276 evh__copy_mem( payload
, p_new
, md
->szB
);
4277 evh__new_mem_heap ( p_new
+ md
->szB
, new_size
- md
->szB
,
4279 /* FIXME: can anything funny happen here? specifically, if the
4280 old range contained a lock, then die_mem_heap will complain.
4281 Is that the correct behaviour? Not sure. */
4282 evh__die_mem_heap( payload
, md
->szB
);
4284 /* Copy from old to new */
4285 for (i
= 0; i
< md
->szB
; i
++)
4286 ((UChar
*)p_new
)[i
] = ((UChar
*)payload
)[i
];
4288 /* Because the metadata hash table is index by payload address,
4289 we have to get rid of the old hash table entry and make a new
4290 one. We can't just modify the existing metadata in place,
4291 because then it would (almost certainly) be in the wrong hash
4293 md_new
= new_MallocMeta();
4296 md_tmp
= VG_(HT_remove
)( hg_mallocmeta_table
, payload
);
4298 tl_assert(md_tmp
== md
);
4300 VG_(cli_free
)((void*)md
->payload
);
4301 delete_MallocMeta(md
);
4304 md_new
->where
= VG_(record_ExeContext
)( tid
, 0 );
4305 md_new
->szB
= new_size
;
4306 md_new
->payload
= p_new
;
4307 md_new
->thr
= map_threads_lookup( tid
);
4310 VG_(HT_add_node
)( hg_mallocmeta_table
, (VgHashNode
*)md_new
);
4312 return (void*)p_new
;
4316 static SizeT
hg_cli_malloc_usable_size ( ThreadId tid
, void* p
)
4318 MallocMeta
*md
= VG_(HT_lookup
)( hg_mallocmeta_table
, (UWord
)p
);
4320 // There may be slop, but pretend there isn't because only the asked-for
4321 // area will have been shadowed properly.
4322 return ( md
? md
->szB
: 0 );
4326 /* For error creation: map 'data_addr' to a malloc'd chunk, if any.
4327 Slow linear search. With a bit of hash table help if 'data_addr'
4328 is either the start of a block or up to 15 word-sized steps along
4329 from the start of a block. */
4331 static inline Bool
addr_is_in_MM_Chunk( MallocMeta
* mm
, Addr a
)
4333 /* Accept 'a' as within 'mm' if 'mm's size is zero and 'a' points
4335 if (UNLIKELY(mm
->szB
== 0 && a
== mm
->payload
))
4337 /* else normal interval rules apply */
4338 if (LIKELY(a
< mm
->payload
)) return False
;
4339 if (LIKELY(a
>= mm
->payload
+ mm
->szB
)) return False
;
4343 Bool
HG_(mm_find_containing_block
)( /*OUT*/ExeContext
** where
,
4345 /*OUT*/Addr
* payload
,
4351 const Int n_fast_check_words
= 16;
4353 /* First, do a few fast searches on the basis that data_addr might
4354 be exactly the start of a block or up to 15 words inside. This
4355 can happen commonly via the creq
4356 _VG_USERREQ__HG_CLEAN_MEMORY_HEAPBLOCK. */
4357 for (i
= 0; i
< n_fast_check_words
; i
++) {
4358 mm
= VG_(HT_lookup
)( hg_mallocmeta_table
,
4359 data_addr
- (UWord
)(UInt
)i
* sizeof(UWord
) );
4360 if (UNLIKELY(mm
&& addr_is_in_MM_Chunk(mm
, data_addr
)))
4364 /* Well, this totally sucks. But without using an interval tree or
4365 some such, it's hard to see how to do better. We have to check
4366 every block in the entire table. */
4367 VG_(HT_ResetIter
)(hg_mallocmeta_table
);
4368 while ( (mm
= VG_(HT_Next
)(hg_mallocmeta_table
)) ) {
4369 if (UNLIKELY(addr_is_in_MM_Chunk(mm
, data_addr
)))
4373 /* Not found. Bah. */
4379 tl_assert(addr_is_in_MM_Chunk(mm
, data_addr
));
4380 if (where
) *where
= mm
->where
;
4381 if (tnr
) *tnr
= mm
->thr
->errmsg_index
;
4382 if (payload
) *payload
= mm
->payload
;
4383 if (szB
) *szB
= mm
->szB
;
4388 /*--------------------------------------------------------------*/
4389 /*--- Instrumentation ---*/
4390 /*--------------------------------------------------------------*/
4392 #define unop(_op, _arg1) IRExpr_Unop((_op),(_arg1))
4393 #define binop(_op, _arg1, _arg2) IRExpr_Binop((_op),(_arg1),(_arg2))
4394 #define mkexpr(_tmp) IRExpr_RdTmp((_tmp))
4395 #define mkU32(_n) IRExpr_Const(IRConst_U32(_n))
4396 #define mkU64(_n) IRExpr_Const(IRConst_U64(_n))
4397 #define assign(_t, _e) IRStmt_WrTmp((_t), (_e))
4399 /* This takes and returns atoms, of course. Not full IRExprs. */
4400 static IRExpr
* mk_And1 ( IRSB
* sbOut
, IRExpr
* arg1
, IRExpr
* arg2
)
4402 tl_assert(arg1
&& arg2
);
4403 tl_assert(isIRAtom(arg1
));
4404 tl_assert(isIRAtom(arg2
));
4405 /* Generate 32to1(And32(1Uto32(arg1), 1Uto32(arg2))). Appalling
4407 IRTemp wide1
= newIRTemp(sbOut
->tyenv
, Ity_I32
);
4408 IRTemp wide2
= newIRTemp(sbOut
->tyenv
, Ity_I32
);
4409 IRTemp anded
= newIRTemp(sbOut
->tyenv
, Ity_I32
);
4410 IRTemp res
= newIRTemp(sbOut
->tyenv
, Ity_I1
);
4411 addStmtToIRSB(sbOut
, assign(wide1
, unop(Iop_1Uto32
, arg1
)));
4412 addStmtToIRSB(sbOut
, assign(wide2
, unop(Iop_1Uto32
, arg2
)));
4413 addStmtToIRSB(sbOut
, assign(anded
, binop(Iop_And32
, mkexpr(wide1
),
4415 addStmtToIRSB(sbOut
, assign(res
, unop(Iop_32to1
, mkexpr(anded
))));
4419 static void instrument_mem_access ( IRSB
* sbOut
,
4425 IRExpr
* guard
) /* NULL => True */
4427 IRType tyAddr
= Ity_INVALID
;
4428 const HChar
* hName
= NULL
;
4431 IRExpr
** argv
= NULL
;
4434 // THRESH is the size of the window above SP (well,
4435 // mostly above) that we assume implies a stack reference.
4436 const Int THRESH
= 4096 * 4; // somewhat arbitrary
4437 const Int rz_szB
= VG_STACK_REDZONE_SZB
;
4439 tl_assert(isIRAtom(addr
));
4440 tl_assert(hWordTy_szB
== 4 || hWordTy_szB
== 8);
4442 tyAddr
= typeOfIRExpr( sbOut
->tyenv
, addr
);
4443 tl_assert(tyAddr
== Ity_I32
|| tyAddr
== Ity_I64
);
4445 /* So the effective address is in 'addr' now. */
4446 regparms
= 1; // unless stated otherwise
4450 hName
= "evh__mem_help_cwrite_1";
4451 hAddr
= &evh__mem_help_cwrite_1
;
4452 argv
= mkIRExprVec_1( addr
);
4455 hName
= "evh__mem_help_cwrite_2";
4456 hAddr
= &evh__mem_help_cwrite_2
;
4457 argv
= mkIRExprVec_1( addr
);
4460 hName
= "evh__mem_help_cwrite_4";
4461 hAddr
= &evh__mem_help_cwrite_4
;
4462 argv
= mkIRExprVec_1( addr
);
4465 hName
= "evh__mem_help_cwrite_8";
4466 hAddr
= &evh__mem_help_cwrite_8
;
4467 argv
= mkIRExprVec_1( addr
);
4470 tl_assert(szB
> 8 && szB
<= 512); /* stay sane */
4472 hName
= "evh__mem_help_cwrite_N";
4473 hAddr
= &evh__mem_help_cwrite_N
;
4474 argv
= mkIRExprVec_2( addr
, mkIRExpr_HWord( szB
));
4480 hName
= "evh__mem_help_cread_1";
4481 hAddr
= &evh__mem_help_cread_1
;
4482 argv
= mkIRExprVec_1( addr
);
4485 hName
= "evh__mem_help_cread_2";
4486 hAddr
= &evh__mem_help_cread_2
;
4487 argv
= mkIRExprVec_1( addr
);
4490 hName
= "evh__mem_help_cread_4";
4491 hAddr
= &evh__mem_help_cread_4
;
4492 argv
= mkIRExprVec_1( addr
);
4495 hName
= "evh__mem_help_cread_8";
4496 hAddr
= &evh__mem_help_cread_8
;
4497 argv
= mkIRExprVec_1( addr
);
4500 tl_assert(szB
> 8 && szB
<= 512); /* stay sane */
4502 hName
= "evh__mem_help_cread_N";
4503 hAddr
= &evh__mem_help_cread_N
;
4504 argv
= mkIRExprVec_2( addr
, mkIRExpr_HWord( szB
));
4509 /* Create the helper. */
4513 di
= unsafeIRDirty_0_N( regparms
,
4514 hName
, VG_(fnptr_to_fnentry
)( hAddr
),
4517 if (! HG_(clo_check_stack_refs
)) {
4518 /* We're ignoring memory references which are (obviously) to the
4519 stack. In fact just skip stack refs that are within 4 pages
4520 of SP (SP - the redzone, really), as that's simple, easy, and
4521 filters out most stack references. */
4522 /* Generate the guard condition: "(addr - (SP - RZ)) >u N", for
4523 some arbitrary N. If that is true then addr is outside the
4524 range (SP - RZ .. SP + N - RZ). If N is smallish (a few
4525 pages) then we can say addr is within a few pages of SP and
4526 so can't possibly be a heap access, and so can be skipped.
4528 Note that the condition simplifies to
4529 (addr - SP + RZ) >u N
4530 which generates better code in x86/amd64 backends, but it does
4531 not unfortunately simplify to
4532 (addr - SP) >u (N - RZ)
4533 (would be beneficial because N - RZ is a constant) because
4534 wraparound arithmetic messes up the comparison. eg.
4536 but (20 - 15) >u (10 - 15) == 5 >u (MAXINT-5) == False.
4538 IRTemp sp
= newIRTemp(sbOut
->tyenv
, tyAddr
);
4539 addStmtToIRSB( sbOut
, assign(sp
, IRExpr_Get(goff_sp
, tyAddr
)));
4542 IRTemp addr_minus_sp
= newIRTemp(sbOut
->tyenv
, tyAddr
);
4545 assign(addr_minus_sp
,
4547 ? binop(Iop_Sub32
, addr
, mkexpr(sp
))
4548 : binop(Iop_Sub64
, addr
, mkexpr(sp
)))
4551 /* "addr - SP + RZ" */
4552 IRTemp diff
= newIRTemp(sbOut
->tyenv
, tyAddr
);
4557 ? binop(Iop_Add32
, mkexpr(addr_minus_sp
), mkU32(rz_szB
))
4558 : binop(Iop_Add64
, mkexpr(addr_minus_sp
), mkU64(rz_szB
)))
4561 /* guardA == "guard on the address" */
4562 IRTemp guardA
= newIRTemp(sbOut
->tyenv
, Ity_I1
);
4567 ? binop(Iop_CmpLT32U
, mkU32(THRESH
), mkexpr(diff
))
4568 : binop(Iop_CmpLT64U
, mkU64(THRESH
), mkexpr(diff
)))
4570 di
->guard
= mkexpr(guardA
);
4573 /* If there's a guard on the access itself (as supplied by the
4574 caller of this routine), we need to AND that in to any guard we
4575 might already have. */
4577 di
->guard
= mk_And1(sbOut
, di
->guard
, guard
);
4580 /* Add the helper. */
4581 addStmtToIRSB( sbOut
, IRStmt_Dirty(di
) );
4585 /* Figure out if GA is a guest code address in the dynamic linker, and
4586 if so return True. Otherwise (and in case of any doubt) return
4587 False. (sidedly safe w/ False as the safe value) */
4588 static Bool
is_in_dynamic_linker_shared_object( Addr ga
)
4591 const HChar
* soname
;
4592 if (0) return False
;
4594 dinfo
= VG_(find_DebugInfo
)( ga
);
4595 if (!dinfo
) return False
;
4597 soname
= VG_(DebugInfo_get_soname
)(dinfo
);
4599 if (0) VG_(printf
)("%s\n", soname
);
4601 # if defined(VGO_linux)
4602 if (VG_STREQ(soname
, VG_U_LD_LINUX_SO_3
)) return True
;
4603 if (VG_STREQ(soname
, VG_U_LD_LINUX_SO_2
)) return True
;
4604 if (VG_STREQ(soname
, VG_U_LD_LINUX_X86_64_SO_2
)) return True
;
4605 if (VG_STREQ(soname
, VG_U_LD64_SO_1
)) return True
;
4606 if (VG_STREQ(soname
, VG_U_LD64_SO_2
)) return True
;
4607 if (VG_STREQ(soname
, VG_U_LD_SO_1
)) return True
;
4608 if (VG_STREQ(soname
, VG_U_LD_LINUX_AARCH64_SO_1
)) return True
;
4609 if (VG_STREQ(soname
, VG_U_LD_LINUX_ARMHF_SO_3
)) return True
;
4610 # elif defined(VGO_darwin)
4611 if (VG_STREQ(soname
, VG_U_DYLD
)) return True
;
4612 # elif defined(VGO_solaris)
4613 if (VG_STREQ(soname
, VG_U_LD_SO_1
)) return True
;
4615 # error "Unsupported OS"
4621 IRSB
* hg_instrument ( VgCallbackClosure
* closure
,
4623 const VexGuestLayout
* layout
,
4624 const VexGuestExtents
* vge
,
4625 const VexArchInfo
* archinfo_host
,
4626 IRType gWordTy
, IRType hWordTy
)
4630 Addr cia
; /* address of current insn */
4632 Bool inLDSO
= False
;
4633 Addr inLDSOmask4K
= 1; /* mismatches on first check */
4635 const Int goff_sp
= layout
->offset_SP
;
4637 if (gWordTy
!= hWordTy
) {
4638 /* We don't currently support this case. */
4639 VG_(tool_panic
)("host/guest word size mismatch");
4642 if (VKI_PAGE_SIZE
< 4096 || VG_(log2
)(VKI_PAGE_SIZE
) == -1) {
4643 VG_(tool_panic
)("implausible or too-small VKI_PAGE_SIZE");
4647 bbOut
= emptyIRSB();
4648 bbOut
->tyenv
= deepCopyIRTypeEnv(bbIn
->tyenv
);
4649 bbOut
->next
= deepCopyIRExpr(bbIn
->next
);
4650 bbOut
->jumpkind
= bbIn
->jumpkind
;
4651 bbOut
->offsIP
= bbIn
->offsIP
;
4653 // Copy verbatim any IR preamble preceding the first IMark
4655 while (i
< bbIn
->stmts_used
&& bbIn
->stmts
[i
]->tag
!= Ist_IMark
) {
4656 addStmtToIRSB( bbOut
, bbIn
->stmts
[i
] );
4660 // Get the first statement, and initial cia from it
4661 tl_assert(bbIn
->stmts_used
> 0);
4662 tl_assert(i
< bbIn
->stmts_used
);
4663 st
= bbIn
->stmts
[i
];
4664 tl_assert(Ist_IMark
== st
->tag
);
4665 cia
= st
->Ist
.IMark
.addr
;
4668 for (/*use current i*/; i
< bbIn
->stmts_used
; i
++) {
4669 st
= bbIn
->stmts
[i
];
4671 tl_assert(isFlatIRStmt(st
));
4678 /* None of these can contain any memory references. */
4682 /* no mem refs, but note the insn address. */
4683 cia
= st
->Ist
.IMark
.addr
;
4684 /* Don't instrument the dynamic linker. It generates a
4685 lot of races which we just expensively suppress, so
4688 Avoid flooding is_in_dynamic_linker_shared_object with
4689 requests by only checking at transitions between 4K
4691 if ((cia
& ~(Addr
)0xFFF) != inLDSOmask4K
) {
4692 if (0) VG_(printf
)("NEW %#lx\n", cia
);
4693 inLDSOmask4K
= cia
& ~(Addr
)0xFFF;
4694 inLDSO
= is_in_dynamic_linker_shared_object(cia
);
4696 if (0) VG_(printf
)("old %#lx\n", cia
);
4701 switch (st
->Ist
.MBE
.event
) {
4703 case Imbe_CancelReservation
:
4704 break; /* not interesting */
4711 /* Atomic read-modify-write cycle. Just pretend it's a
4713 IRCAS
* cas
= st
->Ist
.CAS
.details
;
4714 Bool isDCAS
= cas
->oldHi
!= IRTemp_INVALID
;
4716 tl_assert(cas
->expdHi
);
4717 tl_assert(cas
->dataHi
);
4719 tl_assert(!cas
->expdHi
);
4720 tl_assert(!cas
->dataHi
);
4722 /* Just be boring about it. */
4724 instrument_mem_access(
4728 * sizeofIRType(typeOfIRExpr(bbIn
->tyenv
, cas
->dataLo
)),
4730 sizeofIRType(hWordTy
), goff_sp
,
4738 /* We pretend store-conditionals don't exist, viz, ignore
4739 them. Whereas load-linked's are treated the same as
4742 if (st
->Ist
.LLSC
.storedata
== NULL
) {
4744 dataTy
= typeOfIRTemp(bbIn
->tyenv
, st
->Ist
.LLSC
.result
);
4746 instrument_mem_access(
4749 sizeofIRType(dataTy
),
4751 sizeofIRType(hWordTy
), goff_sp
,
4764 instrument_mem_access(
4767 sizeofIRType(typeOfIRExpr(bbIn
->tyenv
, st
->Ist
.Store
.data
)),
4769 sizeofIRType(hWordTy
), goff_sp
,
4776 IRStoreG
* sg
= st
->Ist
.StoreG
.details
;
4777 IRExpr
* data
= sg
->data
;
4778 IRExpr
* addr
= sg
->addr
;
4779 IRType type
= typeOfIRExpr(bbIn
->tyenv
, data
);
4780 tl_assert(type
!= Ity_INVALID
);
4781 instrument_mem_access( bbOut
, addr
, sizeofIRType(type
),
4783 sizeofIRType(hWordTy
),
4784 goff_sp
, sg
->guard
);
4789 IRLoadG
* lg
= st
->Ist
.LoadG
.details
;
4790 IRType type
= Ity_INVALID
; /* loaded type */
4791 IRType typeWide
= Ity_INVALID
; /* after implicit widening */
4792 IRExpr
* addr
= lg
->addr
;
4793 typeOfIRLoadGOp(lg
->cvt
, &typeWide
, &type
);
4794 tl_assert(type
!= Ity_INVALID
);
4795 instrument_mem_access( bbOut
, addr
, sizeofIRType(type
),
4797 sizeofIRType(hWordTy
),
4798 goff_sp
, lg
->guard
);
4803 IRExpr
* data
= st
->Ist
.WrTmp
.data
;
4804 if (data
->tag
== Iex_Load
) {
4806 instrument_mem_access(
4808 data
->Iex
.Load
.addr
,
4809 sizeofIRType(data
->Iex
.Load
.ty
),
4811 sizeofIRType(hWordTy
), goff_sp
,
4821 IRDirty
* d
= st
->Ist
.Dirty
.details
;
4822 if (d
->mFx
!= Ifx_None
) {
4823 /* This dirty helper accesses memory. Collect the
4825 tl_assert(d
->mAddr
!= NULL
);
4826 tl_assert(d
->mSize
!= 0);
4827 dataSize
= d
->mSize
;
4828 if (d
->mFx
== Ifx_Read
|| d
->mFx
== Ifx_Modify
) {
4830 instrument_mem_access(
4831 bbOut
, d
->mAddr
, dataSize
, False
/*!isStore*/,
4832 sizeofIRType(hWordTy
), goff_sp
, NULL
/*no-guard*/
4836 if (d
->mFx
== Ifx_Write
|| d
->mFx
== Ifx_Modify
) {
4838 instrument_mem_access(
4839 bbOut
, d
->mAddr
, dataSize
, True
/*isStore*/,
4840 sizeofIRType(hWordTy
), goff_sp
, NULL
/*no-guard*/
4845 tl_assert(d
->mAddr
== NULL
);
4846 tl_assert(d
->mSize
== 0);
4856 } /* switch (st->tag) */
4858 addStmtToIRSB( bbOut
, st
);
4859 } /* iterate over bbIn->stmts */
4871 /*----------------------------------------------------------------*/
4872 /*--- Client requests ---*/
4873 /*----------------------------------------------------------------*/
4875 /* Sheesh. Yet another goddam finite map. */
4876 static WordFM
* map_pthread_t_to_Thread
= NULL
; /* pthread_t -> Thread* */
4878 static void map_pthread_t_to_Thread_INIT ( void ) {
4879 if (UNLIKELY(map_pthread_t_to_Thread
== NULL
)) {
4880 map_pthread_t_to_Thread
= VG_(newFM
)( HG_(zalloc
), "hg.mpttT.1",
4885 /* A list of Ada dependent tasks and their masters. Used for implementing
4886 the Ada task termination semantic as implemented by the
4887 gcc gnat Ada runtime. */
4890 void* dependent
; // Ada Task Control Block of the Dependent
4891 void* master
; // ATCB of the master
4892 Word master_level
; // level of dependency between master and dependent
4893 Thread
* hg_dependent
; // helgrind Thread* for dependent task.
4896 static XArray
* gnat_dmmls
; /* of GNAT_dmml */
4897 static void gnat_dmmls_INIT (void)
4899 if (UNLIKELY(gnat_dmmls
== NULL
)) {
4900 gnat_dmmls
= VG_(newXA
) (HG_(zalloc
), "hg.gnat_md.1",
4902 sizeof(GNAT_dmml
) );
4905 static void print_monitor_help ( void )
4910 "helgrind monitor commands:\n"
4911 " info locks [lock_addr] : show status of lock at addr lock_addr\n"
4912 " with no lock_addr, show status of all locks\n"
4913 " accesshistory <addr> [<len>] : show access history recorded\n"
4914 " for <len> (or 1) bytes at <addr>\n"
4918 /* return True if request recognised, False otherwise */
4919 static Bool
handle_gdb_monitor_command (ThreadId tid
, HChar
*req
)
4922 HChar s
[VG_(strlen(req
))]; /* copy for strtok_r */
4926 VG_(strcpy
) (s
, req
);
4928 wcmd
= VG_(strtok_r
) (s
, " ", &ssaveptr
);
4929 /* NB: if possible, avoid introducing a new command below which
4930 starts with the same first letter(s) as an already existing
4931 command. This ensures a shorter abbreviation for the user. */
4932 switch (VG_(keyword_id
)
4933 ("help info accesshistory",
4934 wcmd
, kwd_report_duplicated_matches
)) {
4935 case -2: /* multiple matches */
4937 case -1: /* not found */
4940 print_monitor_help();
4943 wcmd
= VG_(strtok_r
) (NULL
, " ", &ssaveptr
);
4944 switch (kwdid
= VG_(keyword_id
)
4946 wcmd
, kwd_report_all
)) {
4954 Bool lk_shown
= False
;
4955 Bool all_locks
= True
;
4959 wa
= VG_(strtok_r
) (NULL
, " ", &ssaveptr
);
4961 if (VG_(parse_Addr
) (&wa
, &lk_addr
) )
4964 VG_(gdb_printf
) ("missing or malformed address\n");
4967 for (i
= 0, lk
= admin_locks
; lk
; i
++, lk
= lk
->admin_next
) {
4968 if (all_locks
|| lk_addr
== lk
->guestaddr
) {
4970 True
/* show_lock_addrdescr */,
4971 False
/* show_internal_data */);
4976 VG_(gdb_printf
) ("no locks\n");
4977 if (!all_locks
&& !lk_shown
)
4978 VG_(gdb_printf
) ("lock with address %p not found\n",
4987 case 2: /* accesshistory */
4991 if (VG_(strtok_get_address_and_size
) (&address
, &szB
, &ssaveptr
)) {
4993 libhb_event_map_access_history (address
, szB
, HG_(print_access
));
4995 VG_(gdb_printf
) ("len must be >=1\n");
5007 Bool
hg_handle_client_request ( ThreadId tid
, UWord
* args
, UWord
* ret
)
5009 if (!VG_IS_TOOL_USERREQ('H','G',args
[0])
5010 && VG_USERREQ__GDB_MONITOR_COMMAND
!= args
[0])
5013 /* Anything that gets past the above check is one of ours, so we
5014 should be able to handle it. */
5016 /* default, meaningless return value, unless otherwise set */
5021 /* --- --- User-visible client requests --- --- */
5023 case VG_USERREQ__HG_CLEAN_MEMORY
:
5024 if (0) VG_(printf
)("VG_USERREQ__HG_CLEAN_MEMORY(%#lx,%ld)\n",
5026 /* Call die_mem to (expensively) tidy up properly, if there
5027 are any held locks etc in the area. Calling evh__die_mem
5028 and then evh__new_mem is a bit inefficient; probably just
5029 the latter would do. */
5030 if (args
[2] > 0) { /* length */
5031 evh__die_mem(args
[1], args
[2]);
5032 /* and then set it to New */
5033 evh__new_mem(args
[1], args
[2]);
5037 case _VG_USERREQ__HG_CLEAN_MEMORY_HEAPBLOCK
: {
5040 if (0) VG_(printf
)("VG_USERREQ__HG_CLEAN_MEMORY_HEAPBLOCK(%#lx)\n",
5042 if (HG_(mm_find_containing_block
)(NULL
, NULL
,
5043 &payload
, &pszB
, args
[1])) {
5045 evh__die_mem(payload
, pszB
);
5046 evh__new_mem(payload
, pszB
);
5055 case _VG_USERREQ__HG_ARANGE_MAKE_UNTRACKED
:
5056 if (0) VG_(printf
)("HG_ARANGE_MAKE_UNTRACKED(%#lx,%ld)\n",
5058 if (args
[2] > 0) { /* length */
5059 evh__untrack_mem(args
[1], args
[2]);
5063 case _VG_USERREQ__HG_ARANGE_MAKE_TRACKED
:
5064 if (0) VG_(printf
)("HG_ARANGE_MAKE_TRACKED(%#lx,%ld)\n",
5066 if (args
[2] > 0) { /* length */
5067 evh__new_mem(args
[1], args
[2]);
5071 case _VG_USERREQ__HG_GET_ABITS
:
5072 if (0) VG_(printf
)("HG_GET_ABITS(%#lx,%#lx,%ld)\n",
5073 args
[1], args
[2], args
[3]);
5074 UChar
*zzabit
= (UChar
*) args
[2];
5076 || VG_(am_is_valid_for_client
)((Addr
)zzabit
, (SizeT
)args
[3],
5077 VKI_PROT_READ
|VKI_PROT_WRITE
))
5078 *ret
= (UWord
) libhb_srange_get_abits ((Addr
) args
[1],
5085 /* --- --- Client requests for Helgrind's use only --- --- */
5087 /* Some thread is telling us its pthread_t value. Record the
5088 binding between that and the associated Thread*, so we can
5089 later find the Thread* again when notified of a join by the
5091 case _VG_USERREQ__HG_SET_MY_PTHREAD_T
: {
5092 Thread
* my_thr
= NULL
;
5094 VG_(printf
)("SET_MY_PTHREAD_T (tid %d): pthread_t = %p\n", (Int
)tid
,
5096 map_pthread_t_to_Thread_INIT();
5097 my_thr
= map_threads_maybe_lookup( tid
);
5098 /* This assertion should hold because the map_threads (tid to
5099 Thread*) binding should have been made at the point of
5100 low-level creation of this thread, which should have
5101 happened prior to us getting this client request for it.
5102 That's because this client request is sent from
5103 client-world from the 'thread_wrapper' function, which
5104 only runs once the thread has been low-level created. */
5105 tl_assert(my_thr
!= NULL
);
5106 /* So now we know that (pthread_t)args[1] is associated with
5107 (Thread*)my_thr. Note that down. */
5109 VG_(printf
)("XXXX: bind pthread_t %p to Thread* %p\n",
5110 (void*)args
[1], (void*)my_thr
);
5111 VG_(addToFM
)( map_pthread_t_to_Thread
, (UWord
)args
[1], (UWord
)my_thr
);
5113 if (my_thr
->coretid
!= 1) {
5114 /* FIXME: hardwires assumption about identity of the root thread. */
5115 if (HG_(clo_ignore_thread_creation
)) {
5116 HG_(thread_leave_pthread_create
)(my_thr
);
5117 HG_(thread_leave_synchr
)(my_thr
);
5118 tl_assert(my_thr
->synchr_nesting
== 0);
5124 case _VG_USERREQ__HG_PTH_API_ERROR
: {
5125 Thread
* my_thr
= NULL
;
5126 map_pthread_t_to_Thread_INIT();
5127 my_thr
= map_threads_maybe_lookup( tid
);
5128 tl_assert(my_thr
); /* See justification above in SET_MY_PTHREAD_T */
5129 HG_(record_error_PthAPIerror
)(
5130 my_thr
, (HChar
*)args
[1], (UWord
)args
[2], (HChar
*)args
[3] );
5134 /* This thread (tid) has completed a join with the quitting
5135 thread whose pthread_t is in args[1]. */
5136 case _VG_USERREQ__HG_PTHREAD_JOIN_POST
: {
5137 Thread
* thr_q
= NULL
; /* quitter Thread* */
5140 VG_(printf
)("NOTIFY_JOIN_COMPLETE (tid %d): quitter = %p\n", (Int
)tid
,
5142 map_pthread_t_to_Thread_INIT();
5143 found
= VG_(lookupFM
)( map_pthread_t_to_Thread
,
5144 NULL
, (UWord
*)&thr_q
, (UWord
)args
[1] );
5145 /* Can this fail? It would mean that our pthread_join
5146 wrapper observed a successful join on args[1] yet that
5147 thread never existed (or at least, it never lodged an
5148 entry in the mapping (via SET_MY_PTHREAD_T)). Which
5149 sounds like a bug in the threads library. */
5150 // FIXME: get rid of this assertion; handle properly
5154 VG_(printf
)(".................... quitter Thread* = %p\n",
5156 evh__HG_PTHREAD_JOIN_POST( tid
, thr_q
);
5161 /* This thread (tid) is informing us of its master. */
5162 case _VG_USERREQ__HG_GNAT_MASTER_HOOK
: {
5164 dmml
.dependent
= (void*)args
[1];
5165 dmml
.master
= (void*)args
[2];
5166 dmml
.master_level
= (Word
)args
[3];
5167 dmml
.hg_dependent
= map_threads_maybe_lookup( tid
);
5168 tl_assert(dmml
.hg_dependent
);
5171 VG_(printf
)("HG_GNAT_MASTER_HOOK (tid %d): "
5172 "dependent = %p master = %p master_level = %ld"
5173 " dependent Thread* = %p\n",
5174 (Int
)tid
, dmml
.dependent
, dmml
.master
, dmml
.master_level
,
5177 VG_(addToXA
) (gnat_dmmls
, &dmml
);
5181 /* This thread (tid) is informing us that it has completed a
5183 case _VG_USERREQ__HG_GNAT_MASTER_COMPLETED_HOOK
: {
5185 const Thread
*stayer
= map_threads_maybe_lookup( tid
);
5186 const void *master
= (void*)args
[1];
5187 const Word master_level
= (Word
) args
[2];
5191 VG_(printf
)("HG_GNAT_MASTER_COMPLETED_HOOK (tid %d): "
5192 "self_id = %p master_level = %ld Thread* = %p\n",
5193 (Int
)tid
, master
, master_level
, stayer
);
5196 /* Reverse loop on the array, simulating a pthread_join for
5197 the Dependent tasks of the completed master, and removing
5198 them from the array. */
5199 for (n
= VG_(sizeXA
) (gnat_dmmls
) - 1; n
>= 0; n
--) {
5200 GNAT_dmml
*dmml
= (GNAT_dmml
*) VG_(indexXA
)(gnat_dmmls
, n
);
5201 if (dmml
->master
== master
5202 && dmml
->master_level
== master_level
) {
5204 VG_(printf
)("quitter %p dependency to stayer %p\n",
5205 dmml
->hg_dependent
->hbthr
, stayer
->hbthr
);
5206 tl_assert(dmml
->hg_dependent
->hbthr
!= stayer
->hbthr
);
5207 generate_quitter_stayer_dependence (dmml
->hg_dependent
->hbthr
,
5209 VG_(removeIndexXA
) (gnat_dmmls
, n
);
5215 /* EXPOSITION only: by intercepting lock init events we can show
5216 the user where the lock was initialised, rather than only
5217 being able to show where it was first locked. Intercepting
5218 lock initialisations is not necessary for the basic operation
5219 of the race checker. */
5220 case _VG_USERREQ__HG_PTHREAD_MUTEX_INIT_POST
:
5221 evh__HG_PTHREAD_MUTEX_INIT_POST( tid
, (void*)args
[1], args
[2] );
5224 /* mutex=arg[1], mutex_is_init=arg[2] */
5225 case _VG_USERREQ__HG_PTHREAD_MUTEX_DESTROY_PRE
:
5226 evh__HG_PTHREAD_MUTEX_DESTROY_PRE( tid
, (void*)args
[1], args
[2] != 0 );
5229 case _VG_USERREQ__HG_PTHREAD_MUTEX_UNLOCK_PRE
: // pth_mx_t*
5230 HG_(thread_enter_synchr
)(map_threads_maybe_lookup(tid
));
5231 if (HG_(get_pthread_create_nesting_level
)(tid
) == 0)
5232 evh__HG_PTHREAD_MUTEX_UNLOCK_PRE( tid
, (void*)args
[1] );
5235 case _VG_USERREQ__HG_PTHREAD_MUTEX_UNLOCK_POST
: // pth_mx_t*
5236 if (HG_(get_pthread_create_nesting_level
)(tid
) == 0)
5237 evh__HG_PTHREAD_MUTEX_UNLOCK_POST( tid
, (void*)args
[1] );
5238 HG_(thread_leave_synchr
)(map_threads_maybe_lookup(tid
));
5241 case _VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_PRE
: // pth_mx_t*
5242 HG_(thread_enter_synchr
)(map_threads_maybe_lookup(tid
));
5243 if (HG_(get_pthread_create_nesting_level
)(tid
) == 0)
5244 evh__HG_PTHREAD_MUTEX_LOCK_PRE( tid
, (void*)args
[1], args
[2] );
5247 case _VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_POST
: // pth_mx_t*, long
5248 if ((args
[2] == True
) // lock actually taken
5249 && (HG_(get_pthread_create_nesting_level
)(tid
) == 0))
5250 evh__HG_PTHREAD_MUTEX_LOCK_POST( tid
, (void*)args
[1] );
5251 HG_(thread_leave_synchr
)(map_threads_maybe_lookup(tid
));
5254 /* This thread is about to do pthread_cond_signal on the
5255 pthread_cond_t* in arg[1]. Ditto pthread_cond_broadcast. */
5256 case _VG_USERREQ__HG_PTHREAD_COND_SIGNAL_PRE
:
5257 case _VG_USERREQ__HG_PTHREAD_COND_BROADCAST_PRE
:
5258 HG_(thread_enter_synchr
)(map_threads_maybe_lookup(tid
));
5259 evh__HG_PTHREAD_COND_SIGNAL_PRE( tid
, (void*)args
[1] );
5262 case _VG_USERREQ__HG_PTHREAD_COND_SIGNAL_POST
:
5263 case _VG_USERREQ__HG_PTHREAD_COND_BROADCAST_POST
:
5264 HG_(thread_leave_synchr
)(map_threads_maybe_lookup(tid
));
5267 /* Entry into pthread_cond_wait, cond=arg[1], mutex=arg[2].
5268 Returns a flag indicating whether or not the mutex is believed to be
5269 valid for this operation. */
5270 case _VG_USERREQ__HG_PTHREAD_COND_WAIT_PRE
: {
5271 HG_(thread_enter_synchr
)(map_threads_maybe_lookup(tid
));
5273 = evh__HG_PTHREAD_COND_WAIT_PRE( tid
, (void*)args
[1],
5275 *ret
= mutex_is_valid
? 1 : 0;
5279 /* Thread successfully completed pthread_cond_init:
5280 cond=arg[1], cond_attr=arg[2] */
5281 case _VG_USERREQ__HG_PTHREAD_COND_INIT_POST
:
5282 evh__HG_PTHREAD_COND_INIT_POST( tid
,
5283 (void*)args
[1], (void*)args
[2] );
5286 /* cond=arg[1], cond_is_init=arg[2] */
5287 case _VG_USERREQ__HG_PTHREAD_COND_DESTROY_PRE
:
5288 evh__HG_PTHREAD_COND_DESTROY_PRE( tid
, (void*)args
[1], args
[2] != 0 );
5291 /* Thread completed pthread_cond_wait, cond=arg[1],
5292 mutex=arg[2], timeout=arg[3], successful=arg[4] */
5293 case _VG_USERREQ__HG_PTHREAD_COND_WAIT_POST
:
5294 if (args
[4] == True
)
5295 evh__HG_PTHREAD_COND_WAIT_POST( tid
,
5296 (void*)args
[1], (void*)args
[2],
5298 HG_(thread_leave_synchr
)(map_threads_maybe_lookup(tid
));
5301 case _VG_USERREQ__HG_PTHREAD_RWLOCK_INIT_POST
:
5302 evh__HG_PTHREAD_RWLOCK_INIT_POST( tid
, (void*)args
[1] );
5305 case _VG_USERREQ__HG_PTHREAD_RWLOCK_DESTROY_PRE
:
5306 evh__HG_PTHREAD_RWLOCK_DESTROY_PRE( tid
, (void*)args
[1] );
5309 /* rwlock=arg[1], isW=arg[2], isTryLock=arg[3] */
5310 case _VG_USERREQ__HG_PTHREAD_RWLOCK_LOCK_PRE
:
5311 HG_(thread_enter_synchr
)(map_threads_maybe_lookup(tid
));
5312 if (HG_(get_pthread_create_nesting_level
)(tid
) == 0)
5313 evh__HG_PTHREAD_RWLOCK_LOCK_PRE( tid
, (void*)args
[1],
5317 /* rwlock=arg[1], isW=arg[2], tookLock=arg[3] */
5318 case _VG_USERREQ__HG_PTHREAD_RWLOCK_LOCK_POST
:
5319 if ((args
[3] == True
)
5320 && (HG_(get_pthread_create_nesting_level
)(tid
) == 0))
5321 evh__HG_PTHREAD_RWLOCK_LOCK_POST( tid
, (void*)args
[1], args
[2] );
5322 HG_(thread_leave_synchr
)(map_threads_maybe_lookup(tid
));
5325 case _VG_USERREQ__HG_PTHREAD_RWLOCK_UNLOCK_PRE
:
5326 HG_(thread_enter_synchr
)(map_threads_maybe_lookup(tid
));
5327 if (HG_(get_pthread_create_nesting_level
)(tid
) == 0)
5328 evh__HG_PTHREAD_RWLOCK_UNLOCK_PRE( tid
, (void*)args
[1] );
5331 case _VG_USERREQ__HG_PTHREAD_RWLOCK_UNLOCK_POST
:
5332 if (HG_(get_pthread_create_nesting_level
)(tid
) == 0)
5333 evh__HG_PTHREAD_RWLOCK_UNLOCK_POST( tid
, (void*)args
[1] );
5334 HG_(thread_leave_synchr
)(map_threads_maybe_lookup(tid
));
5337 case _VG_USERREQ__HG_POSIX_SEM_INIT_POST
: /* sem_t*, unsigned long */
5338 evh__HG_POSIX_SEM_INIT_POST( tid
, (void*)args
[1], args
[2] );
5341 case _VG_USERREQ__HG_POSIX_SEM_DESTROY_PRE
: /* sem_t* */
5342 evh__HG_POSIX_SEM_DESTROY_PRE( tid
, (void*)args
[1] );
5345 case _VG_USERREQ__HG_POSIX_SEM_POST_PRE
: /* sem_t* */
5346 HG_(thread_enter_synchr
)(map_threads_maybe_lookup(tid
));
5347 evh__HG_POSIX_SEM_POST_PRE( tid
, (void*)args
[1] );
5350 case _VG_USERREQ__HG_POSIX_SEM_POST_POST
: /* sem_t* */
5351 HG_(thread_leave_synchr
)(map_threads_maybe_lookup(tid
));
5354 case _VG_USERREQ__HG_POSIX_SEM_WAIT_PRE
: /* sem_t* */
5355 HG_(thread_enter_synchr
)(map_threads_maybe_lookup(tid
));
5358 case _VG_USERREQ__HG_POSIX_SEM_WAIT_POST
: /* sem_t*, long tookLock */
5359 if (args
[2] == True
)
5360 evh__HG_POSIX_SEM_WAIT_POST( tid
, (void*)args
[1] );
5361 HG_(thread_leave_synchr
)(map_threads_maybe_lookup(tid
));
5364 case _VG_USERREQ__HG_PTHREAD_BARRIER_INIT_PRE
:
5365 /* pth_bar_t*, ulong count, ulong resizable */
5366 evh__HG_PTHREAD_BARRIER_INIT_PRE( tid
, (void*)args
[1],
5370 case _VG_USERREQ__HG_PTHREAD_BARRIER_RESIZE_PRE
:
5371 /* pth_bar_t*, ulong newcount */
5372 evh__HG_PTHREAD_BARRIER_RESIZE_PRE ( tid
, (void*)args
[1],
5376 case _VG_USERREQ__HG_PTHREAD_BARRIER_WAIT_PRE
:
5378 evh__HG_PTHREAD_BARRIER_WAIT_PRE( tid
, (void*)args
[1] );
5381 case _VG_USERREQ__HG_PTHREAD_BARRIER_DESTROY_PRE
:
5383 evh__HG_PTHREAD_BARRIER_DESTROY_PRE( tid
, (void*)args
[1] );
5386 case _VG_USERREQ__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE
:
5387 /* pth_spinlock_t* */
5388 evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE( tid
, (void*)args
[1] );
5391 case _VG_USERREQ__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_POST
:
5392 /* pth_spinlock_t* */
5393 evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_POST( tid
, (void*)args
[1] );
5396 case _VG_USERREQ__HG_PTHREAD_SPIN_LOCK_PRE
:
5397 /* pth_spinlock_t*, Word */
5398 evh__HG_PTHREAD_SPIN_LOCK_PRE( tid
, (void*)args
[1], args
[2] );
5401 case _VG_USERREQ__HG_PTHREAD_SPIN_LOCK_POST
:
5402 /* pth_spinlock_t* */
5403 evh__HG_PTHREAD_SPIN_LOCK_POST( tid
, (void*)args
[1] );
5406 case _VG_USERREQ__HG_PTHREAD_SPIN_DESTROY_PRE
:
5407 /* pth_spinlock_t* */
5408 evh__HG_PTHREAD_SPIN_DESTROY_PRE( tid
, (void*)args
[1] );
5411 case _VG_USERREQ__HG_CLIENTREQ_UNIMP
: {
5413 HChar
* who
= (HChar
*)args
[1];
5415 Thread
* thr
= map_threads_maybe_lookup( tid
);
5416 tl_assert( thr
); /* I must be mapped */
5418 tl_assert( VG_(strlen
)(who
) <= 50 );
5419 VG_(sprintf
)(buf
, "Unimplemented client request macro \"%s\"", who
);
5420 /* record_error_Misc strdup's buf, so this is safe: */
5421 HG_(record_error_Misc
)( thr
, buf
);
5425 case _VG_USERREQ__HG_USERSO_SEND_PRE
:
5426 /* UWord arbitrary-SO-tag */
5427 evh__HG_USERSO_SEND_PRE( tid
, args
[1] );
5430 case _VG_USERREQ__HG_USERSO_RECV_POST
:
5431 /* UWord arbitrary-SO-tag */
5432 evh__HG_USERSO_RECV_POST( tid
, args
[1] );
5435 case _VG_USERREQ__HG_USERSO_FORGET_ALL
:
5436 /* UWord arbitrary-SO-tag */
5437 evh__HG_USERSO_FORGET_ALL( tid
, args
[1] );
5440 case VG_USERREQ__GDB_MONITOR_COMMAND
: {
5441 Bool handled
= handle_gdb_monitor_command (tid
, (HChar
*)args
[1]);
5449 case _VG_USERREQ__HG_PTHREAD_CREATE_BEGIN
: {
5450 Thread
*thr
= map_threads_maybe_lookup(tid
);
5451 if (HG_(clo_ignore_thread_creation
)) {
5452 HG_(thread_enter_pthread_create
)(thr
);
5453 HG_(thread_enter_synchr
)(thr
);
5458 case _VG_USERREQ__HG_PTHREAD_CREATE_END
: {
5459 Thread
*thr
= map_threads_maybe_lookup(tid
);
5460 if (HG_(clo_ignore_thread_creation
)) {
5461 HG_(thread_leave_pthread_create
)(thr
);
5462 HG_(thread_leave_synchr
)(thr
);
5467 case _VG_USERREQ__HG_PTHREAD_MUTEX_ACQUIRE_PRE
: // pth_mx_t*, long tryLock
5468 evh__HG_PTHREAD_MUTEX_LOCK_PRE( tid
, (void*)args
[1], args
[2] );
5471 case _VG_USERREQ__HG_PTHREAD_MUTEX_ACQUIRE_POST
: // pth_mx_t*
5472 evh__HG_PTHREAD_MUTEX_LOCK_POST( tid
, (void*)args
[1] );
5475 case _VG_USERREQ__HG_PTHREAD_RWLOCK_ACQUIRED
: // void*, long isW
5476 evh__HG_PTHREAD_RWLOCK_LOCK_POST( tid
, (void*)args
[1], args
[2] );
5479 case _VG_USERREQ__HG_PTHREAD_RWLOCK_RELEASED
: // void*
5480 evh__HG_PTHREAD_RWLOCK_UNLOCK_PRE( tid
, (void*)args
[1] );
5483 case _VG_USERREQ__HG_POSIX_SEM_RELEASED
: /* sem_t* */
5484 evh__HG_POSIX_SEM_POST_PRE( tid
, (void*)args
[1] );
5487 case _VG_USERREQ__HG_POSIX_SEM_ACQUIRED
: /* sem_t* */
5488 evh__HG_POSIX_SEM_WAIT_POST( tid
, (void*)args
[1] );
5491 #if defined(VGO_solaris)
5492 case _VG_USERREQ__HG_RTLD_BIND_GUARD
:
5493 evh__HG_RTLD_BIND_GUARD(tid
, args
[1]);
5496 case _VG_USERREQ__HG_RTLD_BIND_CLEAR
:
5497 evh__HG_RTLD_BIND_CLEAR(tid
, args
[1]);
5499 #endif /* VGO_solaris */
5502 /* Unhandled Helgrind client request! */
5503 tl_assert2(0, "unhandled Helgrind client request 0x%lx",
5511 /*----------------------------------------------------------------*/
5513 /*----------------------------------------------------------------*/
5515 static Bool
hg_process_cmd_line_option ( const HChar
* arg
)
5517 const HChar
* tmp_str
;
5519 if VG_BOOL_CLO(arg
, "--track-lockorders",
5520 HG_(clo_track_lockorders
)) {}
5521 else if VG_BOOL_CLO(arg
, "--cmp-race-err-addrs",
5522 HG_(clo_cmp_race_err_addrs
)) {}
5524 else if VG_XACT_CLO(arg
, "--history-level=none",
5525 HG_(clo_history_level
), 0);
5526 else if VG_XACT_CLO(arg
, "--history-level=approx",
5527 HG_(clo_history_level
), 1);
5528 else if VG_XACT_CLO(arg
, "--history-level=full",
5529 HG_(clo_history_level
), 2);
5531 else if VG_BINT_CLO(arg
, "--conflict-cache-size",
5532 HG_(clo_conflict_cache_size
), 10*1000, 150*1000*1000) {}
5534 /* "stuvwx" --> stuvwx (binary) */
5535 else if VG_STR_CLO(arg
, "--hg-sanity-flags", tmp_str
) {
5538 if (6 != VG_(strlen
)(tmp_str
)) {
5539 VG_(message
)(Vg_UserMsg
,
5540 "--hg-sanity-flags argument must have 6 digits\n");
5543 for (j
= 0; j
< 6; j
++) {
5544 if ('0' == tmp_str
[j
]) { /* do nothing */ }
5545 else if ('1' == tmp_str
[j
]) HG_(clo_sanity_flags
) |= (1 << (6-1-j
));
5547 VG_(message
)(Vg_UserMsg
, "--hg-sanity-flags argument can "
5548 "only contain 0s and 1s\n");
5552 if (0) VG_(printf
)("XXX sanity flags: 0x%lx\n", HG_(clo_sanity_flags
));
5555 else if VG_BOOL_CLO(arg
, "--free-is-write",
5556 HG_(clo_free_is_write
)) {}
5558 else if VG_XACT_CLO(arg
, "--vts-pruning=never",
5559 HG_(clo_vts_pruning
), 0);
5560 else if VG_XACT_CLO(arg
, "--vts-pruning=auto",
5561 HG_(clo_vts_pruning
), 1);
5562 else if VG_XACT_CLO(arg
, "--vts-pruning=always",
5563 HG_(clo_vts_pruning
), 2);
5565 else if VG_BOOL_CLO(arg
, "--check-stack-refs",
5566 HG_(clo_check_stack_refs
)) {}
5567 else if VG_BOOL_CLO(arg
, "--ignore-thread-creation",
5568 HG_(clo_ignore_thread_creation
)) {}
5571 return VG_(replacement_malloc_process_cmd_line_option
)(arg
);
5576 static void hg_print_usage ( void )
5579 " --free-is-write=no|yes treat heap frees as writes [no]\n"
5580 " --track-lockorders=no|yes show lock ordering errors? [yes]\n"
5581 " --history-level=none|approx|full [full]\n"
5582 " full: show both stack traces for a data race (can be very slow)\n"
5583 " approx: full trace for one thread, approx for the other (faster)\n"
5584 " none: only show trace for one thread in a race (fastest)\n"
5585 " --conflict-cache-size=N size of 'full' history cache [2000000]\n"
5586 " --check-stack-refs=no|yes race-check reads and writes on the\n"
5587 " main stack and thread stacks? [yes]\n"
5588 " --ignore-thread-creation=yes|no Ignore activities during thread\n"
5590 HG_(clo_ignore_thread_creation
) ? "yes" : "no"
5594 static void hg_print_debug_usage ( void )
5596 VG_(printf
)(" --cmp-race-err-addrs=no|yes are data addresses in "
5597 "race errors significant? [no]\n");
5598 VG_(printf
)(" --hg-sanity-flags=<XXXXXX> sanity check "
5599 " at events (X = 0|1) [000000]\n");
5600 VG_(printf
)(" --hg-sanity-flags values:\n");
5601 VG_(printf
)(" 010000 after changes to "
5602 "lock-order-acquisition-graph\n");
5603 VG_(printf
)(" 001000 at memory accesses (NB: not currently used)\n");
5604 VG_(printf
)(" 000100 at mem permission setting for "
5605 "ranges >= %d bytes\n", SCE_BIGRANGE_T
);
5606 VG_(printf
)(" 000010 at lock/unlock events\n");
5607 VG_(printf
)(" 000001 at thread create/join events\n");
5609 " --vts-pruning=never|auto|always [auto]\n"
5610 " never: is never done (may cause big space leaks in Helgrind)\n"
5611 " auto: done just often enough to keep space usage under control\n"
5612 " always: done after every VTS GC (mostly just a big time waster)\n"
5616 static void hg_print_stats (void)
5621 HG_(ppWSUstats
)( univ_lsets
, "univ_lsets" );
5622 if (HG_(clo_track_lockorders
)) {
5624 HG_(ppWSUstats
)( univ_laog
, "univ_laog" );
5628 //zz VG_(printf)("\n");
5629 //zz VG_(printf)(" hbefore: %'10lu queries\n", stats__hbefore_queries);
5630 //zz VG_(printf)(" hbefore: %'10lu cache 0 hits\n", stats__hbefore_cache0s);
5631 //zz VG_(printf)(" hbefore: %'10lu cache > 0 hits\n", stats__hbefore_cacheNs);
5632 //zz VG_(printf)(" hbefore: %'10lu graph searches\n", stats__hbefore_gsearches);
5633 //zz VG_(printf)(" hbefore: %'10lu of which slow\n",
5634 //zz stats__hbefore_gsearches - stats__hbefore_gsearchFs);
5635 //zz VG_(printf)(" hbefore: %'10lu stack high water mark\n",
5636 //zz stats__hbefore_stk_hwm);
5637 //zz VG_(printf)(" hbefore: %'10lu cache invals\n", stats__hbefore_invals);
5638 //zz VG_(printf)(" hbefore: %'10lu probes\n", stats__hbefore_probes);
5641 VG_(printf
)(" locksets: %'8d unique lock sets\n",
5642 (Int
)HG_(cardinalityWSU
)( univ_lsets
));
5643 if (HG_(clo_track_lockorders
)) {
5644 VG_(printf
)(" univ_laog: %'8d unique lock sets\n",
5645 (Int
)HG_(cardinalityWSU
)( univ_laog
));
5648 //VG_(printf)("L(ast)L(ock) map: %'8lu inserts (%d map size)\n",
5649 // stats__ga_LL_adds,
5650 // (Int)(ga_to_lastlock ? VG_(sizeFM)( ga_to_lastlock ) : 0) );
5652 VG_(printf
)(" LockN-to-P map: %'8llu queries (%llu map size)\n",
5653 HG_(stats__LockN_to_P_queries
),
5654 HG_(stats__LockN_to_P_get_map_size
)() );
5656 VG_(printf
)("client malloc-ed blocks: %'8d\n",
5657 VG_(HT_count_nodes
)(hg_mallocmeta_table
));
5659 VG_(printf
)("string table map: %'8llu queries (%llu map size)\n",
5660 HG_(stats__string_table_queries
),
5661 HG_(stats__string_table_get_map_size
)() );
5662 if (HG_(clo_track_lockorders
)) {
5663 VG_(printf
)(" LAOG: %'8d map size\n",
5664 (Int
)(laog
? VG_(sizeFM
)( laog
) : 0));
5665 VG_(printf
)(" LAOG exposition: %'8d map size\n",
5666 (Int
)(laog_exposition
? VG_(sizeFM
)( laog_exposition
) : 0));
5669 VG_(printf
)(" locks: %'8lu acquires, "
5671 stats__lockN_acquires
,
5672 stats__lockN_releases
5674 VG_(printf
)(" sanity checks: %'8lu\n", stats__sanity_checks
);
5677 libhb_shutdown(True
); // This in fact only print stats.
5680 static void hg_fini ( Int exitcode
)
5682 if (VG_(clo_verbosity
) == 1 && !VG_(clo_xml
)) {
5683 VG_(message
)(Vg_UserMsg
,
5684 "For counts of detected and suppressed errors, "
5685 "rerun with: -v\n");
5688 if (VG_(clo_verbosity
) == 1 && !VG_(clo_xml
)
5689 && HG_(clo_history_level
) >= 2) {
5691 "Use --history-level=approx or =none to gain increased speed, at\n" );
5693 "the cost of reduced accuracy of conflicting-access information\n");
5696 if (SHOW_DATA_STRUCTURES
)
5697 pp_everything( PP_ALL
, "SK_(fini)" );
5698 if (HG_(clo_sanity_flags
))
5699 all__sanity_check("SK_(fini)");
5705 /* FIXME: move these somewhere sane */
5708 void for_libhb__get_stacktrace ( Thr
* hbt
, Addr
* frames
, UWord nRequest
)
5714 thr
= libhb_get_Thr_hgthread( hbt
);
5716 tid
= map_threads_maybe_reverse_lookup_SLOW(thr
);
5717 nActual
= (UWord
)VG_(get_StackTrace
)( tid
, frames
, (UInt
)nRequest
,
5719 tl_assert(nActual
<= nRequest
);
5720 for (; nActual
< nRequest
; nActual
++)
5721 frames
[nActual
] = 0;
5725 ExeContext
* for_libhb__get_EC ( Thr
* hbt
)
5731 thr
= libhb_get_Thr_hgthread( hbt
);
5733 tid
= map_threads_maybe_reverse_lookup_SLOW(thr
);
5734 /* this will assert if tid is invalid */
5735 ec
= VG_(record_ExeContext
)( tid
, 0 );
5740 static void hg_post_clo_init ( void )
5744 /////////////////////////////////////////////
5745 hbthr_root
= libhb_init( for_libhb__get_stacktrace
,
5746 for_libhb__get_EC
);
5747 /////////////////////////////////////////////
5750 if (HG_(clo_track_lockorders
))
5753 initialise_data_structures(hbthr_root
);
5756 static void hg_info_location (Addr a
)
5758 (void) HG_(get_and_pp_addrdescr
) (a
);
5761 static void hg_pre_clo_init ( void )
5763 VG_(details_name
) ("Helgrind");
5764 VG_(details_version
) (NULL
);
5765 VG_(details_description
) ("a thread error detector");
5766 VG_(details_copyright_author
)(
5767 "Copyright (C) 2007-2013, and GNU GPL'd, by OpenWorks LLP et al.");
5768 VG_(details_bug_reports_to
) (VG_BUGS_TO
);
5769 VG_(details_avg_translation_sizeB
) ( 320 );
5771 VG_(basic_tool_funcs
) (hg_post_clo_init
,
5775 VG_(needs_core_errors
) ();
5776 VG_(needs_tool_errors
) (HG_(eq_Error
),
5777 HG_(before_pp_Error
),
5779 False
,/*show TIDs for errors*/
5781 HG_(recognised_suppression
),
5782 HG_(read_extra_suppression_info
),
5783 HG_(error_matches_suppression
),
5784 HG_(get_error_name
),
5785 HG_(get_extra_suppression_info
),
5786 HG_(print_extra_suppression_use
),
5787 HG_(update_extra_suppression_use
));
5789 VG_(needs_xml_output
) ();
5791 VG_(needs_command_line_options
)(hg_process_cmd_line_option
,
5793 hg_print_debug_usage
);
5794 VG_(needs_client_requests
) (hg_handle_client_request
);
5797 //VG_(needs_sanity_checks) (hg_cheap_sanity_check,
5798 // hg_expensive_sanity_check);
5800 VG_(needs_print_stats
) (hg_print_stats
);
5801 VG_(needs_info_location
) (hg_info_location
);
5803 VG_(needs_malloc_replacement
) (hg_cli__malloc
,
5804 hg_cli____builtin_new
,
5805 hg_cli____builtin_vec_new
,
5809 hg_cli____builtin_delete
,
5810 hg_cli____builtin_vec_delete
,
5812 hg_cli_malloc_usable_size
,
5813 HG_CLI__DEFAULT_MALLOC_REDZONE_SZB
);
5815 /* 21 Dec 08: disabled this; it mostly causes H to start more
5816 slowly and use significantly more memory, without very often
5817 providing useful results. The user can request to load this
5818 information manually with --read-var-info=yes. */
5819 if (0) VG_(needs_var_info
)(); /* optional */
5821 VG_(track_new_mem_startup
) ( evh__new_mem_w_perms
);
5822 VG_(track_new_mem_stack_signal
)( evh__new_mem_w_tid
);
5823 VG_(track_new_mem_brk
) ( evh__new_mem_w_tid
);
5824 VG_(track_new_mem_mmap
) ( evh__new_mem_w_perms
);
5825 VG_(track_new_mem_stack
) ( evh__new_mem_stack
);
5827 // FIXME: surely this isn't thread-aware
5828 VG_(track_copy_mem_remap
) ( evh__copy_mem
);
5830 VG_(track_change_mem_mprotect
) ( evh__set_perms
);
5832 VG_(track_die_mem_stack_signal
)( evh__die_mem
);
5833 VG_(track_die_mem_brk
) ( evh__die_mem_munmap
);
5834 VG_(track_die_mem_munmap
) ( evh__die_mem_munmap
);
5836 /* evh__die_mem calls at the end libhb_srange_noaccess_NoFX
5837 which has no effect. We do not use VG_(track_die_mem_stack),
5838 as this would be an expensive way to do nothing. */
5839 // VG_(track_die_mem_stack) ( evh__die_mem );
5841 // FIXME: what is this for?
5842 VG_(track_ban_mem_stack
) (NULL
);
5844 VG_(track_pre_mem_read
) ( evh__pre_mem_read
);
5845 VG_(track_pre_mem_read_asciiz
) ( evh__pre_mem_read_asciiz
);
5846 VG_(track_pre_mem_write
) ( evh__pre_mem_write
);
5847 VG_(track_post_mem_write
) (NULL
);
5851 VG_(track_pre_thread_ll_create
)( evh__pre_thread_ll_create
);
5852 VG_(track_pre_thread_ll_exit
) ( evh__pre_thread_ll_exit
);
5854 VG_(track_start_client_code
)( evh__start_client_code
);
5855 VG_(track_stop_client_code
)( evh__stop_client_code
);
5857 /* Ensure that requirements for "dodgy C-as-C++ style inheritance"
5858 as described in comments at the top of pub_tool_hashtable.h, are
5860 tl_assert( sizeof(void*) == sizeof(struct _MallocMeta
*) );
5861 tl_assert( sizeof(UWord
) == sizeof(Addr
) );
5863 = VG_(HT_construct
)( "hg_malloc_metadata_table" );
5865 MallocMeta_poolalloc
= VG_(newPA
) ( sizeof(MallocMeta
),
5868 "hg_malloc_metadata_pool",
5871 // add a callback to clean up on (threaded) fork.
5872 VG_(atfork
)(NULL
/*pre*/, NULL
/*parent*/, evh__atfork_child
/*child*/);
5875 VG_DETERMINE_INTERFACE_VERSION(hg_pre_clo_init
)
5877 /*--------------------------------------------------------------------*/
5878 /*--- end hg_main.c ---*/
5879 /*--------------------------------------------------------------------*/