sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Helgrind: a Valgrind tool for detecting errors ---*/ |
| 4 | /*--- in threaded programs. hg_main.c ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
| 8 | This file is part of Helgrind, a Valgrind tool for detecting errors |
| 9 | in threaded programs. |
| 10 | |
sewardj | 0f157dd | 2013-10-18 14:27:36 +0000 | [diff] [blame] | 11 | Copyright (C) 2007-2013 OpenWorks LLP |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 12 | info@open-works.co.uk |
| 13 | |
sewardj | 0f157dd | 2013-10-18 14:27:36 +0000 | [diff] [blame] | 14 | Copyright (C) 2007-2013 Apple, Inc. |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 15 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 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. |
| 20 | |
| 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. |
| 25 | |
| 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 |
| 29 | 02111-1307, USA. |
| 30 | |
| 31 | The GNU General Public License is contained in the file COPYING. |
| 32 | |
| 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. |
| 37 | */ |
| 38 | |
| 39 | #include "pub_tool_basics.h" |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 40 | #include "pub_tool_gdbserver.h" |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 41 | #include "pub_tool_libcassert.h" |
| 42 | #include "pub_tool_libcbase.h" |
| 43 | #include "pub_tool_libcprint.h" |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 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" |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 52 | #include "pub_tool_wordfm.h" |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 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 |
sewardj | 61bc2c5 | 2011-02-09 10:34:00 +0000 | [diff] [blame] | 56 | #include "pub_tool_libcproc.h" // VG_(atfork) |
sewardj | 234e558 | 2011-02-09 12:47:23 +0000 | [diff] [blame] | 57 | #include "pub_tool_aspacemgr.h" // VG_(am_is_valid_for_client) |
philippe | 5fbc976 | 2013-12-01 19:28:48 +0000 | [diff] [blame] | 58 | #include "pub_tool_poolalloc.h" |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 59 | #include "pub_tool_addrinfo.h" |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 60 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 61 | #include "hg_basics.h" |
| 62 | #include "hg_wordset.h" |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 63 | #include "hg_addrdescr.h" |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 64 | #include "hg_lock_n_thread.h" |
| 65 | #include "hg_errors.h" |
| 66 | |
| 67 | #include "libhb.h" |
| 68 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 69 | #include "helgrind.h" |
| 70 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 71 | |
| 72 | // FIXME: new_mem_w_tid ignores the supplied tid. (wtf?!) |
| 73 | |
| 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 |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 76 | |
| 77 | /*----------------------------------------------------------------*/ |
| 78 | /*--- ---*/ |
| 79 | /*----------------------------------------------------------------*/ |
| 80 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 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. |
sewardj | c17be79 | 2007-11-10 22:50:13 +0000 | [diff] [blame] | 86 | */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 87 | |
| 88 | // FIXME what is supposed to happen to locks in memory which |
| 89 | // is relocated as a result of client realloc? |
| 90 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 91 | // FIXME put referencing ThreadId into Thread and get |
| 92 | // rid of the slow reverse mapping function. |
| 93 | |
| 94 | // FIXME accesses to NoAccess areas: change state to Excl? |
| 95 | |
| 96 | // FIXME report errors for accesses of NoAccess memory? |
| 97 | |
| 98 | // FIXME pth_cond_wait/timedwait wrappers. Even if these fail, |
| 99 | // the thread still holds the lock. |
| 100 | |
| 101 | /* ------------ Debug/trace options ------------ */ |
| 102 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 103 | // 0 for silent, 1 for some stuff, 2 for lots of stuff |
| 104 | #define SHOW_EVENTS 0 |
| 105 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 106 | |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 107 | static void all__sanity_check ( const HChar* who ); /* fwds */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 108 | |
philippe | d99c26a | 2012-07-31 22:17:28 +0000 | [diff] [blame] | 109 | #define HG_CLI__DEFAULT_MALLOC_REDZONE_SZB 16 /* let's say */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 110 | |
| 111 | // 0 for none, 1 for dump at end of run |
| 112 | #define SHOW_DATA_STRUCTURES 0 |
| 113 | |
| 114 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 115 | /* ------------ Misc comments ------------ */ |
| 116 | |
| 117 | // FIXME: don't hardwire initial entries for root thread. |
| 118 | // Instead, let the pre_thread_ll_create handler do this. |
| 119 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 120 | |
| 121 | /*----------------------------------------------------------------*/ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 122 | /*--- Primary data structures ---*/ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 123 | /*----------------------------------------------------------------*/ |
| 124 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 125 | /* Admin linked list of Threads */ |
| 126 | static Thread* admin_threads = NULL; |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 127 | Thread* get_admin_threads ( void ) { return admin_threads; } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 128 | |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 129 | /* Admin double linked list of Locks */ |
| 130 | /* We need a double linked list to properly and efficiently |
| 131 | handle del_LockN. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 132 | static Lock* admin_locks = NULL; |
| 133 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 134 | /* Mapping table for core ThreadIds to Thread* */ |
| 135 | static Thread** map_threads = NULL; /* Array[VG_N_THREADS] of Thread* */ |
| 136 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 137 | /* Mapping table for lock guest addresses to Lock* */ |
| 138 | static WordFM* map_locks = NULL; /* WordFM LockAddr Lock* */ |
| 139 | |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 140 | /* The word-set universes for lock sets. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 141 | static WordSetU* univ_lsets = NULL; /* sets of Lock* */ |
| 142 | static WordSetU* univ_laog = NULL; /* sets of Lock*, for LAOG */ |
sewardj | 866c80c | 2011-10-22 19:29:51 +0000 | [diff] [blame] | 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. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 146 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 147 | /* Allow libhb to get at the universe of locksets stored |
| 148 | here. Sigh. */ |
| 149 | WordSetU* HG_(get_univ_lsets) ( void ) { return univ_lsets; } |
| 150 | |
| 151 | /* Allow libhb to get at the list of locks stored here. Ditto |
| 152 | sigh. */ |
| 153 | Lock* HG_(get_admin_locks) ( void ) { return admin_locks; } |
| 154 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 155 | |
| 156 | /*----------------------------------------------------------------*/ |
| 157 | /*--- Simple helpers for the data structures ---*/ |
| 158 | /*----------------------------------------------------------------*/ |
| 159 | |
| 160 | static UWord stats__lockN_acquires = 0; |
| 161 | static UWord stats__lockN_releases = 0; |
| 162 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 163 | static |
| 164 | ThreadId map_threads_maybe_reverse_lookup_SLOW ( Thread* thr ); /*fwds*/ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 165 | |
| 166 | /* --------- Constructors --------- */ |
| 167 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 168 | static Thread* mk_Thread ( Thr* hbthr ) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 169 | static Int indx = 1; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 170 | Thread* thread = HG_(zalloc)( "hg.mk_Thread.1", sizeof(Thread) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 171 | thread->locksetA = HG_(emptyWS)( univ_lsets ); |
| 172 | thread->locksetW = HG_(emptyWS)( univ_lsets ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 173 | thread->magic = Thread_MAGIC; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 174 | thread->hbthr = hbthr; |
| 175 | thread->coretid = VG_INVALID_THREADID; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 176 | thread->created_at = NULL; |
| 177 | thread->announced = False; |
| 178 | thread->errmsg_index = indx++; |
| 179 | thread->admin = admin_threads; |
| 180 | admin_threads = thread; |
| 181 | return thread; |
| 182 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 183 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 184 | // Make a new lock which is unlocked (hence ownerless) |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 185 | // and insert the new lock in admin_locks double linked list. |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 186 | static Lock* mk_LockN ( LockKind kind, Addr guestaddr ) { |
| 187 | static ULong unique = 0; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 188 | Lock* lock = HG_(zalloc)( "hg.mk_Lock.1", sizeof(Lock) ); |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 189 | /* begin: add to double linked list */ |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 190 | if (admin_locks) |
| 191 | admin_locks->admin_prev = lock; |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 192 | lock->admin_next = admin_locks; |
| 193 | lock->admin_prev = NULL; |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 194 | admin_locks = lock; |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 195 | /* end: add */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 196 | lock->unique = unique++; |
| 197 | lock->magic = LockN_MAGIC; |
| 198 | lock->appeared_at = NULL; |
| 199 | lock->acquired_at = NULL; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 200 | lock->hbso = libhb_so_alloc(); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 201 | lock->guestaddr = guestaddr; |
| 202 | lock->kind = kind; |
| 203 | lock->heldW = False; |
| 204 | lock->heldBy = NULL; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 205 | tl_assert(HG_(is_sane_LockN)(lock)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 206 | return lock; |
| 207 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 208 | |
| 209 | /* Release storage for a Lock. Also release storage in .heldBy, if |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 210 | any. Removes from admin_locks double linked list. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 211 | static void del_LockN ( Lock* lk ) |
| 212 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 213 | tl_assert(HG_(is_sane_LockN)(lk)); |
| 214 | tl_assert(lk->hbso); |
| 215 | libhb_so_dealloc(lk->hbso); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 216 | if (lk->heldBy) |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 217 | VG_(deleteBag)( lk->heldBy ); |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 218 | /* begin: del lock from double linked list */ |
| 219 | if (lk == admin_locks) { |
| 220 | tl_assert(lk->admin_prev == NULL); |
| 221 | if (lk->admin_next) |
| 222 | lk->admin_next->admin_prev = NULL; |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 223 | admin_locks = lk->admin_next; |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 224 | } |
| 225 | else { |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 226 | tl_assert(lk->admin_prev != NULL); |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 227 | lk->admin_prev->admin_next = lk->admin_next; |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 228 | if (lk->admin_next) |
| 229 | lk->admin_next->admin_prev = lk->admin_prev; |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 230 | } |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 231 | /* end: del */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 232 | VG_(memset)(lk, 0xAA, sizeof(*lk)); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 233 | HG_(free)(lk); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* Update 'lk' to reflect that 'thr' now has a write-acquisition of |
| 237 | it. This is done strictly: only combinations resulting from |
| 238 | correct program and libpthread behaviour are allowed. */ |
| 239 | static void lockN_acquire_writer ( Lock* lk, Thread* thr ) |
| 240 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 241 | tl_assert(HG_(is_sane_LockN)(lk)); |
| 242 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 243 | |
| 244 | stats__lockN_acquires++; |
| 245 | |
| 246 | /* EXPOSITION only */ |
| 247 | /* We need to keep recording snapshots of where the lock was |
| 248 | acquired, so as to produce better lock-order error messages. */ |
| 249 | if (lk->acquired_at == NULL) { |
| 250 | ThreadId tid; |
| 251 | tl_assert(lk->heldBy == NULL); |
| 252 | tid = map_threads_maybe_reverse_lookup_SLOW(thr); |
| 253 | lk->acquired_at |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 254 | = VG_(record_ExeContext)(tid, 0/*first_ip_delta*/); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 255 | } else { |
| 256 | tl_assert(lk->heldBy != NULL); |
| 257 | } |
| 258 | /* end EXPOSITION only */ |
| 259 | |
| 260 | switch (lk->kind) { |
| 261 | case LK_nonRec: |
| 262 | case_LK_nonRec: |
| 263 | tl_assert(lk->heldBy == NULL); /* can't w-lock recursively */ |
| 264 | tl_assert(!lk->heldW); |
| 265 | lk->heldW = True; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 266 | lk->heldBy = VG_(newBag)( HG_(zalloc), "hg.lNaw.1", HG_(free) ); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 267 | VG_(addToBag)( lk->heldBy, (UWord)thr ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 268 | break; |
| 269 | case LK_mbRec: |
| 270 | if (lk->heldBy == NULL) |
| 271 | goto case_LK_nonRec; |
| 272 | /* 2nd and subsequent locking of a lock by its owner */ |
| 273 | tl_assert(lk->heldW); |
| 274 | /* assert: lk is only held by one thread .. */ |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 275 | tl_assert(VG_(sizeUniqueBag(lk->heldBy)) == 1); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 276 | /* assert: .. and that thread is 'thr'. */ |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 277 | tl_assert(VG_(elemBag)(lk->heldBy, (UWord)thr) |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 278 | == VG_(sizeTotalBag)(lk->heldBy)); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 279 | VG_(addToBag)(lk->heldBy, (UWord)thr); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 280 | break; |
| 281 | case LK_rdwr: |
| 282 | tl_assert(lk->heldBy == NULL && !lk->heldW); /* must be unheld */ |
| 283 | goto case_LK_nonRec; |
| 284 | default: |
| 285 | tl_assert(0); |
| 286 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 287 | tl_assert(HG_(is_sane_LockN)(lk)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static void lockN_acquire_reader ( Lock* lk, Thread* thr ) |
| 291 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 292 | tl_assert(HG_(is_sane_LockN)(lk)); |
| 293 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 294 | /* can only add reader to a reader-writer lock. */ |
| 295 | tl_assert(lk->kind == LK_rdwr); |
| 296 | /* lk must be free or already r-held. */ |
| 297 | tl_assert(lk->heldBy == NULL |
| 298 | || (lk->heldBy != NULL && !lk->heldW)); |
| 299 | |
| 300 | stats__lockN_acquires++; |
| 301 | |
| 302 | /* EXPOSITION only */ |
| 303 | /* We need to keep recording snapshots of where the lock was |
| 304 | acquired, so as to produce better lock-order error messages. */ |
| 305 | if (lk->acquired_at == NULL) { |
| 306 | ThreadId tid; |
| 307 | tl_assert(lk->heldBy == NULL); |
| 308 | tid = map_threads_maybe_reverse_lookup_SLOW(thr); |
| 309 | lk->acquired_at |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 310 | = VG_(record_ExeContext)(tid, 0/*first_ip_delta*/); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 311 | } else { |
| 312 | tl_assert(lk->heldBy != NULL); |
| 313 | } |
| 314 | /* end EXPOSITION only */ |
| 315 | |
| 316 | if (lk->heldBy) { |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 317 | VG_(addToBag)(lk->heldBy, (UWord)thr); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 318 | } else { |
| 319 | lk->heldW = False; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 320 | lk->heldBy = VG_(newBag)( HG_(zalloc), "hg.lNar.1", HG_(free) ); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 321 | VG_(addToBag)( lk->heldBy, (UWord)thr ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 322 | } |
| 323 | tl_assert(!lk->heldW); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 324 | tl_assert(HG_(is_sane_LockN)(lk)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* Update 'lk' to reflect a release of it by 'thr'. This is done |
| 328 | strictly: only combinations resulting from correct program and |
| 329 | libpthread behaviour are allowed. */ |
| 330 | |
| 331 | static void lockN_release ( Lock* lk, Thread* thr ) |
| 332 | { |
| 333 | Bool b; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 334 | tl_assert(HG_(is_sane_LockN)(lk)); |
| 335 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 336 | /* lock must be held by someone */ |
| 337 | tl_assert(lk->heldBy); |
| 338 | stats__lockN_releases++; |
| 339 | /* Remove it from the holder set */ |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 340 | b = VG_(delFromBag)(lk->heldBy, (UWord)thr); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 341 | /* thr must actually have been a holder of lk */ |
| 342 | tl_assert(b); |
| 343 | /* normalise */ |
| 344 | tl_assert(lk->acquired_at); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 345 | if (VG_(isEmptyBag)(lk->heldBy)) { |
| 346 | VG_(deleteBag)(lk->heldBy); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 347 | lk->heldBy = NULL; |
| 348 | lk->heldW = False; |
| 349 | lk->acquired_at = NULL; |
| 350 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 351 | tl_assert(HG_(is_sane_LockN)(lk)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static void remove_Lock_from_locksets_of_all_owning_Threads( Lock* lk ) |
| 355 | { |
| 356 | Thread* thr; |
| 357 | if (!lk->heldBy) { |
| 358 | tl_assert(!lk->heldW); |
| 359 | return; |
| 360 | } |
| 361 | /* for each thread that holds this lock do ... */ |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 362 | VG_(initIterBag)( lk->heldBy ); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 363 | while (VG_(nextIterBag)( lk->heldBy, (UWord*)&thr, NULL )) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 364 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 365 | tl_assert(HG_(elemWS)( univ_lsets, |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 366 | thr->locksetA, (UWord)lk )); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 367 | thr->locksetA |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 368 | = HG_(delFromWS)( univ_lsets, thr->locksetA, (UWord)lk ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 369 | |
| 370 | if (lk->heldW) { |
| 371 | tl_assert(HG_(elemWS)( univ_lsets, |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 372 | thr->locksetW, (UWord)lk )); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 373 | thr->locksetW |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 374 | = HG_(delFromWS)( univ_lsets, thr->locksetW, (UWord)lk ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 377 | VG_(doneIterBag)( lk->heldBy ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 378 | } |
| 379 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 380 | |
| 381 | /*----------------------------------------------------------------*/ |
| 382 | /*--- Print out the primary data structures ---*/ |
| 383 | /*----------------------------------------------------------------*/ |
| 384 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 385 | #define PP_THREADS (1<<1) |
| 386 | #define PP_LOCKS (1<<2) |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 387 | #define PP_ALL (PP_THREADS | PP_LOCKS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 388 | |
| 389 | |
| 390 | static const Int sHOW_ADMIN = 0; |
| 391 | |
| 392 | static void space ( Int n ) |
| 393 | { |
| 394 | Int i; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 395 | HChar spaces[128+1]; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 396 | tl_assert(n >= 0 && n < 128); |
| 397 | if (n == 0) |
| 398 | return; |
| 399 | for (i = 0; i < n; i++) |
| 400 | spaces[i] = ' '; |
| 401 | spaces[i] = 0; |
| 402 | tl_assert(i < 128+1); |
| 403 | VG_(printf)("%s", spaces); |
| 404 | } |
| 405 | |
| 406 | static void pp_Thread ( Int d, Thread* t ) |
| 407 | { |
| 408 | space(d+0); VG_(printf)("Thread %p {\n", t); |
| 409 | if (sHOW_ADMIN) { |
| 410 | space(d+3); VG_(printf)("admin %p\n", t->admin); |
| 411 | space(d+3); VG_(printf)("magic 0x%x\n", (UInt)t->magic); |
| 412 | } |
| 413 | space(d+3); VG_(printf)("locksetA %d\n", (Int)t->locksetA); |
| 414 | space(d+3); VG_(printf)("locksetW %d\n", (Int)t->locksetW); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 415 | space(d+0); VG_(printf)("}\n"); |
| 416 | } |
| 417 | |
| 418 | static void pp_admin_threads ( Int d ) |
| 419 | { |
| 420 | Int i, n; |
| 421 | Thread* t; |
| 422 | for (n = 0, t = admin_threads; t; n++, t = t->admin) { |
| 423 | /* nothing */ |
| 424 | } |
| 425 | space(d); VG_(printf)("admin_threads (%d records) {\n", n); |
| 426 | for (i = 0, t = admin_threads; t; i++, t = t->admin) { |
| 427 | if (0) { |
| 428 | space(n); |
| 429 | VG_(printf)("admin_threads record %d of %d:\n", i, n); |
| 430 | } |
| 431 | pp_Thread(d+3, t); |
| 432 | } |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 433 | space(d); VG_(printf)("}\n"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static void pp_map_threads ( Int d ) |
| 437 | { |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 438 | Int i, n = 0; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 439 | space(d); VG_(printf)("map_threads "); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 440 | for (i = 0; i < VG_N_THREADS; i++) { |
| 441 | if (map_threads[i] != NULL) |
| 442 | n++; |
| 443 | } |
| 444 | VG_(printf)("(%d entries) {\n", n); |
| 445 | for (i = 0; i < VG_N_THREADS; i++) { |
| 446 | if (map_threads[i] == NULL) |
| 447 | continue; |
| 448 | space(d+3); |
| 449 | VG_(printf)("coretid %d -> Thread %p\n", i, map_threads[i]); |
| 450 | } |
| 451 | space(d); VG_(printf)("}\n"); |
| 452 | } |
| 453 | |
| 454 | static const HChar* show_LockKind ( LockKind lkk ) { |
| 455 | switch (lkk) { |
| 456 | case LK_mbRec: return "mbRec"; |
| 457 | case LK_nonRec: return "nonRec"; |
| 458 | case LK_rdwr: return "rdwr"; |
| 459 | default: tl_assert(0); |
| 460 | } |
| 461 | } |
| 462 | |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 463 | /* Pretty Print lock lk. |
| 464 | if show_lock_addrdescr, describes the (guest) lock address. |
| 465 | (this description will be more complete with --read-var-info=yes). |
| 466 | if show_internal_data, shows also helgrind internal information. |
| 467 | d is the level at which output is indented. */ |
| 468 | static void pp_Lock ( Int d, Lock* lk, |
| 469 | Bool show_lock_addrdescr, |
| 470 | Bool show_internal_data) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 471 | { |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 472 | space(d+0); |
| 473 | if (show_internal_data) |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 474 | VG_(printf)("Lock %p (ga %#lx) {\n", lk, lk->guestaddr); |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 475 | else |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 476 | VG_(printf)("Lock ga %#lx {\n", lk->guestaddr); |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 477 | if (!show_lock_addrdescr |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 478 | || !HG_(get_and_pp_addrdescr) ((Addr) lk->guestaddr)) |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 479 | VG_(printf)("\n"); |
| 480 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 481 | if (sHOW_ADMIN) { |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 482 | space(d+3); VG_(printf)("admin_n %p\n", lk->admin_next); |
| 483 | space(d+3); VG_(printf)("admin_p %p\n", lk->admin_prev); |
| 484 | space(d+3); VG_(printf)("magic 0x%x\n", (UInt)lk->magic); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 485 | } |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 486 | if (show_internal_data) { |
| 487 | space(d+3); VG_(printf)("unique %llu\n", lk->unique); |
| 488 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 489 | space(d+3); VG_(printf)("kind %s\n", show_LockKind(lk->kind)); |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 490 | if (show_internal_data) { |
| 491 | space(d+3); VG_(printf)("heldW %s\n", lk->heldW ? "yes" : "no"); |
| 492 | } |
| 493 | if (show_internal_data) { |
| 494 | space(d+3); VG_(printf)("heldBy %p", lk->heldBy); |
| 495 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 496 | if (lk->heldBy) { |
| 497 | Thread* thr; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 498 | UWord count; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 499 | VG_(printf)(" { "); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 500 | VG_(initIterBag)( lk->heldBy ); |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 501 | while (VG_(nextIterBag)( lk->heldBy, (UWord*)&thr, &count )) { |
| 502 | if (show_internal_data) |
| 503 | VG_(printf)("%lu:%p ", count, thr); |
| 504 | else { |
| 505 | VG_(printf)("%c%lu:thread #%d ", |
| 506 | lk->heldW ? 'W' : 'R', |
| 507 | count, thr->errmsg_index); |
| 508 | if (thr->coretid == VG_INVALID_THREADID) |
| 509 | VG_(printf)("tid (exited) "); |
| 510 | else |
| 511 | VG_(printf)("tid %d ", thr->coretid); |
| 512 | |
| 513 | } |
| 514 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 515 | VG_(doneIterBag)( lk->heldBy ); |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 516 | VG_(printf)("}\n"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 517 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 518 | space(d+0); VG_(printf)("}\n"); |
| 519 | } |
| 520 | |
| 521 | static void pp_admin_locks ( Int d ) |
| 522 | { |
| 523 | Int i, n; |
| 524 | Lock* lk; |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 525 | for (n = 0, lk = admin_locks; lk; n++, lk = lk->admin_next) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 526 | /* nothing */ |
| 527 | } |
| 528 | space(d); VG_(printf)("admin_locks (%d records) {\n", n); |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 529 | for (i = 0, lk = admin_locks; lk; i++, lk = lk->admin_next) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 530 | if (0) { |
| 531 | space(n); |
| 532 | VG_(printf)("admin_locks record %d of %d:\n", i, n); |
| 533 | } |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 534 | pp_Lock(d+3, lk, |
| 535 | False /* show_lock_addrdescr */, |
| 536 | True /* show_internal_data */); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 537 | } |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 538 | space(d); VG_(printf)("}\n"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 539 | } |
| 540 | |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 541 | static void pp_map_locks ( Int d) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 542 | { |
| 543 | void* gla; |
| 544 | Lock* lk; |
| 545 | space(d); VG_(printf)("map_locks (%d entries) {\n", |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 546 | (Int)VG_(sizeFM)( map_locks )); |
| 547 | VG_(initIterFM)( map_locks ); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 548 | while (VG_(nextIterFM)( map_locks, (UWord*)&gla, |
| 549 | (UWord*)&lk )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 550 | space(d+3); |
| 551 | VG_(printf)("guest %p -> Lock %p\n", gla, lk); |
| 552 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 553 | VG_(doneIterFM)( map_locks ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 554 | space(d); VG_(printf)("}\n"); |
| 555 | } |
| 556 | |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 557 | static void pp_everything ( Int flags, const HChar* caller ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 558 | { |
| 559 | Int d = 0; |
| 560 | VG_(printf)("\n"); |
| 561 | VG_(printf)("All_Data_Structures (caller = \"%s\") {\n", caller); |
| 562 | if (flags & PP_THREADS) { |
| 563 | VG_(printf)("\n"); |
| 564 | pp_admin_threads(d+3); |
| 565 | VG_(printf)("\n"); |
| 566 | pp_map_threads(d+3); |
| 567 | } |
| 568 | if (flags & PP_LOCKS) { |
| 569 | VG_(printf)("\n"); |
| 570 | pp_admin_locks(d+3); |
| 571 | VG_(printf)("\n"); |
| 572 | pp_map_locks(d+3); |
| 573 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 574 | |
| 575 | VG_(printf)("\n"); |
| 576 | VG_(printf)("}\n"); |
| 577 | VG_(printf)("\n"); |
| 578 | } |
| 579 | |
| 580 | #undef SHOW_ADMIN |
| 581 | |
| 582 | |
| 583 | /*----------------------------------------------------------------*/ |
| 584 | /*--- Initialise the primary data structures ---*/ |
| 585 | /*----------------------------------------------------------------*/ |
| 586 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 587 | static void initialise_data_structures ( Thr* hbthr_root ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 588 | { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 589 | Thread* thr; |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 590 | WordSetID wsid; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 591 | |
| 592 | /* Get everything initialised and zeroed. */ |
| 593 | tl_assert(admin_threads == NULL); |
| 594 | tl_assert(admin_locks == NULL); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 595 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 596 | tl_assert(map_threads == NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 597 | map_threads = HG_(zalloc)( "hg.ids.1", VG_N_THREADS * sizeof(Thread*) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 598 | tl_assert(map_threads != NULL); |
| 599 | |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 600 | tl_assert(sizeof(Addr) == sizeof(UWord)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 601 | tl_assert(map_locks == NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 602 | map_locks = VG_(newFM)( HG_(zalloc), "hg.ids.2", HG_(free), |
| 603 | NULL/*unboxed Word cmp*/); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 604 | tl_assert(map_locks != NULL); |
| 605 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 606 | tl_assert(univ_lsets == NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 607 | univ_lsets = HG_(newWordSetU)( HG_(zalloc), "hg.ids.4", HG_(free), |
| 608 | 8/*cacheSize*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 609 | tl_assert(univ_lsets != NULL); |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 610 | /* Ensure that univ_lsets is non-empty, with lockset zero being the |
| 611 | empty lockset. hg_errors.c relies on the assumption that |
| 612 | lockset number zero in univ_lsets is always valid. */ |
| 613 | wsid = HG_(emptyWS)(univ_lsets); |
| 614 | tl_assert(wsid == 0); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 615 | |
| 616 | tl_assert(univ_laog == NULL); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 617 | if (HG_(clo_track_lockorders)) { |
| 618 | univ_laog = HG_(newWordSetU)( HG_(zalloc), "hg.ids.5 (univ_laog)", |
| 619 | HG_(free), 24/*cacheSize*/ ); |
| 620 | tl_assert(univ_laog != NULL); |
| 621 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 622 | |
| 623 | /* Set up entries for the root thread */ |
| 624 | // FIXME: this assumes that the first real ThreadId is 1 |
| 625 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 626 | /* a Thread for the new thread ... */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 627 | thr = mk_Thread(hbthr_root); |
| 628 | thr->coretid = 1; /* FIXME: hardwires an assumption about the |
| 629 | identity of the root thread. */ |
sewardj | 6062664 | 2011-03-10 15:14:37 +0000 | [diff] [blame] | 630 | tl_assert( libhb_get_Thr_hgthread(hbthr_root) == NULL ); |
| 631 | libhb_set_Thr_hgthread(hbthr_root, thr); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 632 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 633 | /* and bind it in the thread-map table. */ |
| 634 | tl_assert(HG_(is_sane_ThreadId)(thr->coretid)); |
| 635 | tl_assert(thr->coretid != VG_INVALID_THREADID); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 636 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 637 | map_threads[thr->coretid] = thr; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 638 | |
| 639 | tl_assert(VG_INVALID_THREADID == 0); |
| 640 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 641 | all__sanity_check("initialise_data_structures"); |
| 642 | } |
| 643 | |
| 644 | |
| 645 | /*----------------------------------------------------------------*/ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 646 | /*--- map_threads :: array[core-ThreadId] of Thread* ---*/ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 647 | /*----------------------------------------------------------------*/ |
| 648 | |
| 649 | /* Doesn't assert if the relevant map_threads entry is NULL. */ |
| 650 | static Thread* map_threads_maybe_lookup ( ThreadId coretid ) |
| 651 | { |
| 652 | Thread* thr; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 653 | tl_assert( HG_(is_sane_ThreadId)(coretid) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 654 | thr = map_threads[coretid]; |
| 655 | return thr; |
| 656 | } |
| 657 | |
| 658 | /* Asserts if the relevant map_threads entry is NULL. */ |
| 659 | static inline Thread* map_threads_lookup ( ThreadId coretid ) |
| 660 | { |
| 661 | Thread* thr; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 662 | tl_assert( HG_(is_sane_ThreadId)(coretid) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 663 | thr = map_threads[coretid]; |
| 664 | tl_assert(thr); |
| 665 | return thr; |
| 666 | } |
| 667 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 668 | /* Do a reverse lookup. Does not assert if 'thr' is not found in |
| 669 | map_threads. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 670 | static ThreadId map_threads_maybe_reverse_lookup_SLOW ( Thread* thr ) |
| 671 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 672 | ThreadId tid; |
| 673 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 674 | /* Check nobody used the invalid-threadid slot */ |
| 675 | tl_assert(VG_INVALID_THREADID >= 0 && VG_INVALID_THREADID < VG_N_THREADS); |
| 676 | tl_assert(map_threads[VG_INVALID_THREADID] == NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 677 | tid = thr->coretid; |
| 678 | tl_assert(HG_(is_sane_ThreadId)(tid)); |
| 679 | return tid; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* Do a reverse lookup. Warning: POTENTIALLY SLOW. Asserts if 'thr' |
| 683 | is not found in map_threads. */ |
| 684 | static ThreadId map_threads_reverse_lookup_SLOW ( Thread* thr ) |
| 685 | { |
| 686 | ThreadId tid = map_threads_maybe_reverse_lookup_SLOW( thr ); |
| 687 | tl_assert(tid != VG_INVALID_THREADID); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 688 | tl_assert(map_threads[tid]); |
| 689 | tl_assert(map_threads[tid]->coretid == tid); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 690 | return tid; |
| 691 | } |
| 692 | |
| 693 | static void map_threads_delete ( ThreadId coretid ) |
| 694 | { |
| 695 | Thread* thr; |
| 696 | tl_assert(coretid != 0); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 697 | tl_assert( HG_(is_sane_ThreadId)(coretid) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 698 | thr = map_threads[coretid]; |
| 699 | tl_assert(thr); |
| 700 | map_threads[coretid] = NULL; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | /*----------------------------------------------------------------*/ |
| 705 | /*--- map_locks :: WordFM guest-Addr-of-lock Lock* ---*/ |
| 706 | /*----------------------------------------------------------------*/ |
| 707 | |
| 708 | /* Make sure there is a lock table entry for the given (lock) guest |
| 709 | address. If not, create one of the stated 'kind' in unheld state. |
| 710 | In any case, return the address of the existing or new Lock. */ |
| 711 | static |
| 712 | Lock* map_locks_lookup_or_create ( LockKind lkk, Addr ga, ThreadId tid ) |
| 713 | { |
| 714 | Bool found; |
| 715 | Lock* oldlock = NULL; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 716 | tl_assert(HG_(is_sane_ThreadId)(tid)); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 717 | found = VG_(lookupFM)( map_locks, |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 718 | NULL, (UWord*)&oldlock, (UWord)ga ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 719 | if (!found) { |
| 720 | Lock* lock = mk_LockN(lkk, ga); |
| 721 | lock->appeared_at = VG_(record_ExeContext)( tid, 0 ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 722 | tl_assert(HG_(is_sane_LockN)(lock)); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 723 | VG_(addToFM)( map_locks, (UWord)ga, (UWord)lock ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 724 | tl_assert(oldlock == NULL); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 725 | return lock; |
| 726 | } else { |
| 727 | tl_assert(oldlock != NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 728 | tl_assert(HG_(is_sane_LockN)(oldlock)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 729 | tl_assert(oldlock->guestaddr == ga); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 730 | return oldlock; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | static Lock* map_locks_maybe_lookup ( Addr ga ) |
| 735 | { |
| 736 | Bool found; |
| 737 | Lock* lk = NULL; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 738 | found = VG_(lookupFM)( map_locks, NULL, (UWord*)&lk, (UWord)ga ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 739 | tl_assert(found ? lk != NULL : lk == NULL); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 740 | return lk; |
| 741 | } |
| 742 | |
| 743 | static void map_locks_delete ( Addr ga ) |
| 744 | { |
| 745 | Addr ga2 = 0; |
| 746 | Lock* lk = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 747 | VG_(delFromFM)( map_locks, |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 748 | (UWord*)&ga2, (UWord*)&lk, (UWord)ga ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 749 | /* delFromFM produces the val which is being deleted, if it is |
| 750 | found. So assert it is non-null; that in effect asserts that we |
| 751 | are deleting a (ga, Lock) pair which actually exists. */ |
| 752 | tl_assert(lk != NULL); |
| 753 | tl_assert(ga2 == ga); |
| 754 | } |
| 755 | |
| 756 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 757 | |
| 758 | /*----------------------------------------------------------------*/ |
| 759 | /*--- Sanity checking the data structures ---*/ |
| 760 | /*----------------------------------------------------------------*/ |
| 761 | |
| 762 | static UWord stats__sanity_checks = 0; |
| 763 | |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 764 | static void laog__sanity_check ( const HChar* who ); /* fwds */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 765 | |
| 766 | /* REQUIRED INVARIANTS: |
| 767 | |
| 768 | Thread vs Segment/Lock/SecMaps |
| 769 | |
| 770 | for each t in Threads { |
| 771 | |
| 772 | // Thread.lockset: each element is really a valid Lock |
| 773 | |
| 774 | // Thread.lockset: each Lock in set is actually held by that thread |
| 775 | for lk in Thread.lockset |
| 776 | lk == LockedBy(t) |
| 777 | |
| 778 | // Thread.csegid is a valid SegmentID |
| 779 | // and the associated Segment has .thr == t |
| 780 | |
| 781 | } |
| 782 | |
| 783 | all thread Locksets are pairwise empty under intersection |
| 784 | (that is, no lock is claimed to be held by more than one thread) |
| 785 | -- this is guaranteed if all locks in locksets point back to their |
| 786 | owner threads |
| 787 | |
| 788 | Lock vs Thread/Segment/SecMaps |
| 789 | |
| 790 | for each entry (gla, la) in map_locks |
| 791 | gla == la->guest_addr |
| 792 | |
| 793 | for each lk in Locks { |
| 794 | |
| 795 | lk->tag is valid |
| 796 | lk->guest_addr does not have shadow state NoAccess |
| 797 | if lk == LockedBy(t), then t->lockset contains lk |
| 798 | if lk == UnlockedBy(segid) then segid is valid SegmentID |
| 799 | and can be mapped to a valid Segment(seg) |
| 800 | and seg->thr->lockset does not contain lk |
| 801 | if lk == UnlockedNew then (no lockset contains lk) |
| 802 | |
| 803 | secmaps for lk has .mbHasLocks == True |
| 804 | |
| 805 | } |
| 806 | |
| 807 | Segment vs Thread/Lock/SecMaps |
| 808 | |
| 809 | the Segment graph is a dag (no cycles) |
| 810 | all of the Segment graph must be reachable from the segids |
| 811 | mentioned in the Threads |
| 812 | |
| 813 | for seg in Segments { |
| 814 | |
| 815 | seg->thr is a sane Thread |
| 816 | |
| 817 | } |
| 818 | |
| 819 | SecMaps vs Segment/Thread/Lock |
| 820 | |
| 821 | for sm in SecMaps { |
| 822 | |
| 823 | sm properly aligned |
| 824 | if any shadow word is ShR or ShM then .mbHasShared == True |
| 825 | |
| 826 | for each Excl(segid) state |
| 827 | map_segments_lookup maps to a sane Segment(seg) |
| 828 | for each ShM/ShR(tsetid,lsetid) state |
| 829 | each lk in lset is a valid Lock |
| 830 | each thr in tset is a valid thread, which is non-dead |
| 831 | |
| 832 | } |
| 833 | */ |
| 834 | |
| 835 | |
| 836 | /* Return True iff 'thr' holds 'lk' in some mode. */ |
| 837 | static Bool thread_is_a_holder_of_Lock ( Thread* thr, Lock* lk ) |
| 838 | { |
| 839 | if (lk->heldBy) |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 840 | return VG_(elemBag)( lk->heldBy, (UWord)thr ) > 0; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 841 | else |
| 842 | return False; |
| 843 | } |
| 844 | |
| 845 | /* Sanity check Threads, as far as possible */ |
| 846 | __attribute__((noinline)) |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 847 | static void threads__sanity_check ( const HChar* who ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 848 | { |
| 849 | #define BAD(_str) do { how = (_str); goto bad; } while (0) |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 850 | const HChar* how = "no error"; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 851 | Thread* thr; |
| 852 | WordSetID wsA, wsW; |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 853 | UWord* ls_words; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 854 | UWord ls_size, i; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 855 | Lock* lk; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 856 | for (thr = admin_threads; thr; thr = thr->admin) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 857 | if (!HG_(is_sane_Thread)(thr)) BAD("1"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 858 | wsA = thr->locksetA; |
| 859 | wsW = thr->locksetW; |
| 860 | // locks held in W mode are a subset of all locks held |
| 861 | if (!HG_(isSubsetOf)( univ_lsets, wsW, wsA )) BAD("7"); |
| 862 | HG_(getPayloadWS)( &ls_words, &ls_size, univ_lsets, wsA ); |
| 863 | for (i = 0; i < ls_size; i++) { |
| 864 | lk = (Lock*)ls_words[i]; |
| 865 | // Thread.lockset: each element is really a valid Lock |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 866 | if (!HG_(is_sane_LockN)(lk)) BAD("2"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 867 | // Thread.lockset: each Lock in set is actually held by that |
| 868 | // thread |
| 869 | if (!thread_is_a_holder_of_Lock(thr,lk)) BAD("3"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | return; |
| 873 | bad: |
| 874 | VG_(printf)("threads__sanity_check: who=\"%s\", bad=\"%s\"\n", who, how); |
| 875 | tl_assert(0); |
| 876 | #undef BAD |
| 877 | } |
| 878 | |
| 879 | |
| 880 | /* Sanity check Locks, as far as possible */ |
| 881 | __attribute__((noinline)) |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 882 | static void locks__sanity_check ( const HChar* who ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 883 | { |
| 884 | #define BAD(_str) do { how = (_str); goto bad; } while (0) |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 885 | const HChar* how = "no error"; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 886 | Addr gla; |
| 887 | Lock* lk; |
| 888 | Int i; |
| 889 | // # entries in admin_locks == # entries in map_locks |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 890 | for (i = 0, lk = admin_locks; lk; i++, lk = lk->admin_next) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 891 | ; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 892 | if (i != VG_(sizeFM)(map_locks)) BAD("1"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 893 | // for each entry (gla, lk) in map_locks |
| 894 | // gla == lk->guest_addr |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 895 | VG_(initIterFM)( map_locks ); |
| 896 | while (VG_(nextIterFM)( map_locks, |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 897 | (UWord*)&gla, (UWord*)&lk )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 898 | if (lk->guestaddr != gla) BAD("2"); |
| 899 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 900 | VG_(doneIterFM)( map_locks ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 901 | // scan through admin_locks ... |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 902 | for (lk = admin_locks; lk; lk = lk->admin_next) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 903 | // lock is sane. Quite comprehensive, also checks that |
| 904 | // referenced (holder) threads are sane. |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 905 | if (!HG_(is_sane_LockN)(lk)) BAD("3"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 906 | // map_locks binds guest address back to this lock |
| 907 | if (lk != map_locks_maybe_lookup(lk->guestaddr)) BAD("4"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 908 | // look at all threads mentioned as holders of this lock. Ensure |
| 909 | // this lock is mentioned in their locksets. |
| 910 | if (lk->heldBy) { |
| 911 | Thread* thr; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 912 | UWord count; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 913 | VG_(initIterBag)( lk->heldBy ); |
| 914 | while (VG_(nextIterBag)( lk->heldBy, |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 915 | (UWord*)&thr, &count )) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 916 | // HG_(is_sane_LockN) above ensures these |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 917 | tl_assert(count >= 1); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 918 | tl_assert(HG_(is_sane_Thread)(thr)); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 919 | if (!HG_(elemWS)(univ_lsets, thr->locksetA, (UWord)lk)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 920 | BAD("6"); |
| 921 | // also check the w-only lockset |
| 922 | if (lk->heldW |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 923 | && !HG_(elemWS)(univ_lsets, thr->locksetW, (UWord)lk)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 924 | BAD("7"); |
| 925 | if ((!lk->heldW) |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 926 | && HG_(elemWS)(univ_lsets, thr->locksetW, (UWord)lk)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 927 | BAD("8"); |
| 928 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 929 | VG_(doneIterBag)( lk->heldBy ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 930 | } else { |
| 931 | /* lock not held by anybody */ |
| 932 | if (lk->heldW) BAD("9"); /* should be False if !heldBy */ |
| 933 | // since lk is unheld, then (no lockset contains lk) |
| 934 | // hmm, this is really too expensive to check. Hmm. |
| 935 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | return; |
| 939 | bad: |
| 940 | VG_(printf)("locks__sanity_check: who=\"%s\", bad=\"%s\"\n", who, how); |
| 941 | tl_assert(0); |
| 942 | #undef BAD |
| 943 | } |
| 944 | |
| 945 | |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 946 | static void all_except_Locks__sanity_check ( const HChar* who ) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 947 | stats__sanity_checks++; |
| 948 | if (0) VG_(printf)("all_except_Locks__sanity_check(%s)\n", who); |
| 949 | threads__sanity_check(who); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 950 | if (HG_(clo_track_lockorders)) |
| 951 | laog__sanity_check(who); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 952 | } |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 953 | static void all__sanity_check ( const HChar* who ) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 954 | all_except_Locks__sanity_check(who); |
| 955 | locks__sanity_check(who); |
| 956 | } |
| 957 | |
| 958 | |
| 959 | /*----------------------------------------------------------------*/ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 960 | /*--- Shadow value and address range handlers ---*/ |
| 961 | /*----------------------------------------------------------------*/ |
| 962 | |
| 963 | static void laog__pre_thread_acquires_lock ( Thread*, Lock* ); /* fwds */ |
sewardj | 1cbc12f | 2008-11-10 16:16:46 +0000 | [diff] [blame] | 964 | //static void laog__handle_lock_deletions ( WordSetID ); /* fwds */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 965 | static inline Thread* get_current_Thread ( void ); /* fwds */ |
sewardj | 1cbc12f | 2008-11-10 16:16:46 +0000 | [diff] [blame] | 966 | __attribute__((noinline)) |
| 967 | static void laog__handle_one_lock_deletion ( Lock* lk ); /* fwds */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 968 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 969 | |
| 970 | /* Block-copy states (needed for implementing realloc()). */ |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 971 | /* FIXME this copies shadow memory; it doesn't apply the MSM to it. |
| 972 | Is that a problem? (hence 'scopy' rather than 'ccopy') */ |
| 973 | static void shadow_mem_scopy_range ( Thread* thr, |
| 974 | Addr src, Addr dst, SizeT len ) |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 975 | { |
| 976 | Thr* hbthr = thr->hbthr; |
| 977 | tl_assert(hbthr); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 978 | libhb_copy_shadow_state( hbthr, src, dst, len ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 979 | } |
| 980 | |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 981 | static void shadow_mem_cread_range ( Thread* thr, Addr a, SizeT len ) |
| 982 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 983 | Thr* hbthr = thr->hbthr; |
| 984 | tl_assert(hbthr); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 985 | LIBHB_CREAD_N(hbthr, a, len); |
| 986 | } |
| 987 | |
| 988 | static void shadow_mem_cwrite_range ( Thread* thr, Addr a, SizeT len ) { |
| 989 | Thr* hbthr = thr->hbthr; |
| 990 | tl_assert(hbthr); |
| 991 | LIBHB_CWRITE_N(hbthr, a, len); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static void shadow_mem_make_New ( Thread* thr, Addr a, SizeT len ) |
| 995 | { |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 996 | libhb_srange_new( thr->hbthr, a, len ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 997 | } |
| 998 | |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 999 | static void shadow_mem_make_NoAccess_NoFX ( Thread* thr, Addr aIN, SizeT len ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1000 | { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1001 | if (0 && len > 500) |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1002 | VG_(printf)("make NoAccess_NoFX ( %#lx, %ld )\n", aIN, len ); |
| 1003 | // has no effect (NoFX) |
| 1004 | libhb_srange_noaccess_NoFX( thr->hbthr, aIN, len ); |
| 1005 | } |
| 1006 | |
| 1007 | static void shadow_mem_make_NoAccess_AHAE ( Thread* thr, Addr aIN, SizeT len ) |
| 1008 | { |
| 1009 | if (0 && len > 500) |
| 1010 | VG_(printf)("make NoAccess_AHAE ( %#lx, %ld )\n", aIN, len ); |
| 1011 | // Actually Has An Effect (AHAE) |
| 1012 | libhb_srange_noaccess_AHAE( thr->hbthr, aIN, len ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 1015 | static void shadow_mem_make_Untracked ( Thread* thr, Addr aIN, SizeT len ) |
| 1016 | { |
| 1017 | if (0 && len > 500) |
| 1018 | VG_(printf)("make Untracked ( %#lx, %ld )\n", aIN, len ); |
| 1019 | libhb_srange_untrack( thr->hbthr, aIN, len ); |
| 1020 | } |
| 1021 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1022 | |
| 1023 | /*----------------------------------------------------------------*/ |
| 1024 | /*--- Event handlers (evh__* functions) ---*/ |
| 1025 | /*--- plus helpers (evhH__* functions) ---*/ |
| 1026 | /*----------------------------------------------------------------*/ |
| 1027 | |
| 1028 | /*--------- Event handler helpers (evhH__* functions) ---------*/ |
| 1029 | |
| 1030 | /* Create a new segment for 'thr', making it depend (.prev) on its |
| 1031 | existing segment, bind together the SegmentID and Segment, and |
| 1032 | return both of them. Also update 'thr' so it references the new |
| 1033 | Segment. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1034 | //zz static |
| 1035 | //zz void evhH__start_new_segment_for_thread ( /*OUT*/SegmentID* new_segidP, |
| 1036 | //zz /*OUT*/Segment** new_segP, |
| 1037 | //zz Thread* thr ) |
| 1038 | //zz { |
| 1039 | //zz Segment* cur_seg; |
| 1040 | //zz tl_assert(new_segP); |
| 1041 | //zz tl_assert(new_segidP); |
| 1042 | //zz tl_assert(HG_(is_sane_Thread)(thr)); |
| 1043 | //zz cur_seg = map_segments_lookup( thr->csegid ); |
| 1044 | //zz tl_assert(cur_seg); |
| 1045 | //zz tl_assert(cur_seg->thr == thr); /* all sane segs should point back |
| 1046 | //zz at their owner thread. */ |
| 1047 | //zz *new_segP = mk_Segment( thr, cur_seg, NULL/*other*/ ); |
| 1048 | //zz *new_segidP = alloc_SegmentID(); |
| 1049 | //zz map_segments_add( *new_segidP, *new_segP ); |
| 1050 | //zz thr->csegid = *new_segidP; |
| 1051 | //zz } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1052 | |
| 1053 | |
| 1054 | /* The lock at 'lock_ga' has acquired a writer. Make all necessary |
| 1055 | updates, and also do all possible error checks. */ |
| 1056 | static |
| 1057 | void evhH__post_thread_w_acquires_lock ( Thread* thr, |
| 1058 | LockKind lkk, Addr lock_ga ) |
| 1059 | { |
| 1060 | Lock* lk; |
| 1061 | |
| 1062 | /* Basically what we need to do is call lockN_acquire_writer. |
| 1063 | However, that will barf if any 'invalid' lock states would |
| 1064 | result. Therefore check before calling. Side effect is that |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1065 | 'HG_(is_sane_LockN)(lk)' is both a pre- and post-condition of this |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1066 | routine. |
| 1067 | |
| 1068 | Because this routine is only called after successful lock |
| 1069 | acquisition, we should not be asked to move the lock into any |
| 1070 | invalid states. Requests to do so are bugs in libpthread, since |
| 1071 | that should have rejected any such requests. */ |
| 1072 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1073 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1074 | /* Try to find the lock. If we can't, then create a new one with |
| 1075 | kind 'lkk'. */ |
| 1076 | lk = map_locks_lookup_or_create( |
| 1077 | lkk, lock_ga, map_threads_reverse_lookup_SLOW(thr) ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1078 | tl_assert( HG_(is_sane_LockN)(lk) ); |
| 1079 | |
| 1080 | /* check libhb level entities exist */ |
| 1081 | tl_assert(thr->hbthr); |
| 1082 | tl_assert(lk->hbso); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1083 | |
| 1084 | if (lk->heldBy == NULL) { |
| 1085 | /* the lock isn't held. Simple. */ |
| 1086 | tl_assert(!lk->heldW); |
| 1087 | lockN_acquire_writer( lk, thr ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1088 | /* acquire a dependency from the lock's VCs */ |
| 1089 | libhb_so_recv( thr->hbthr, lk->hbso, True/*strong_recv*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1090 | goto noerror; |
| 1091 | } |
| 1092 | |
| 1093 | /* So the lock is already held. If held as a r-lock then |
| 1094 | libpthread must be buggy. */ |
| 1095 | tl_assert(lk->heldBy); |
| 1096 | if (!lk->heldW) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1097 | HG_(record_error_Misc)( |
| 1098 | thr, "Bug in libpthread: write lock " |
| 1099 | "granted on rwlock which is currently rd-held"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1100 | goto error; |
| 1101 | } |
| 1102 | |
| 1103 | /* So the lock is held in w-mode. If it's held by some other |
| 1104 | thread, then libpthread must be buggy. */ |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 1105 | tl_assert(VG_(sizeUniqueBag)(lk->heldBy) == 1); /* from precondition */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1106 | |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 1107 | if (thr != (Thread*)VG_(anyElementOfBag)(lk->heldBy)) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1108 | HG_(record_error_Misc)( |
| 1109 | thr, "Bug in libpthread: write lock " |
| 1110 | "granted on mutex/rwlock which is currently " |
| 1111 | "wr-held by a different thread"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1112 | goto error; |
| 1113 | } |
| 1114 | |
| 1115 | /* So the lock is already held in w-mode by 'thr'. That means this |
| 1116 | is an attempt to lock it recursively, which is only allowable |
| 1117 | for LK_mbRec kinded locks. Since this routine is called only |
| 1118 | once the lock has been acquired, this must also be a libpthread |
| 1119 | bug. */ |
| 1120 | if (lk->kind != LK_mbRec) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1121 | HG_(record_error_Misc)( |
| 1122 | thr, "Bug in libpthread: recursive write lock " |
| 1123 | "granted on mutex/wrlock which does not " |
| 1124 | "support recursion"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1125 | goto error; |
| 1126 | } |
| 1127 | |
| 1128 | /* So we are recursively re-locking a lock we already w-hold. */ |
| 1129 | lockN_acquire_writer( lk, thr ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1130 | /* acquire a dependency from the lock's VC. Probably pointless, |
| 1131 | but also harmless. */ |
| 1132 | libhb_so_recv( thr->hbthr, lk->hbso, True/*strong_recv*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1133 | goto noerror; |
| 1134 | |
| 1135 | noerror: |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 1136 | if (HG_(clo_track_lockorders)) { |
| 1137 | /* check lock order acquisition graph, and update. This has to |
| 1138 | happen before the lock is added to the thread's locksetA/W. */ |
| 1139 | laog__pre_thread_acquires_lock( thr, lk ); |
| 1140 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1141 | /* update the thread's held-locks set */ |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1142 | thr->locksetA = HG_(addToWS)( univ_lsets, thr->locksetA, (UWord)lk ); |
| 1143 | thr->locksetW = HG_(addToWS)( univ_lsets, thr->locksetW, (UWord)lk ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1144 | /* fall through */ |
| 1145 | |
| 1146 | error: |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1147 | tl_assert(HG_(is_sane_LockN)(lk)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | |
| 1151 | /* The lock at 'lock_ga' has acquired a reader. Make all necessary |
| 1152 | updates, and also do all possible error checks. */ |
| 1153 | static |
| 1154 | void evhH__post_thread_r_acquires_lock ( Thread* thr, |
| 1155 | LockKind lkk, Addr lock_ga ) |
| 1156 | { |
| 1157 | Lock* lk; |
| 1158 | |
| 1159 | /* Basically what we need to do is call lockN_acquire_reader. |
| 1160 | However, that will barf if any 'invalid' lock states would |
| 1161 | result. Therefore check before calling. Side effect is that |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1162 | 'HG_(is_sane_LockN)(lk)' is both a pre- and post-condition of this |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1163 | routine. |
| 1164 | |
| 1165 | Because this routine is only called after successful lock |
| 1166 | acquisition, we should not be asked to move the lock into any |
| 1167 | invalid states. Requests to do so are bugs in libpthread, since |
| 1168 | that should have rejected any such requests. */ |
| 1169 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1170 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1171 | /* Try to find the lock. If we can't, then create a new one with |
| 1172 | kind 'lkk'. Only a reader-writer lock can be read-locked, |
| 1173 | hence the first assertion. */ |
| 1174 | tl_assert(lkk == LK_rdwr); |
| 1175 | lk = map_locks_lookup_or_create( |
| 1176 | lkk, lock_ga, map_threads_reverse_lookup_SLOW(thr) ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1177 | tl_assert( HG_(is_sane_LockN)(lk) ); |
| 1178 | |
| 1179 | /* check libhb level entities exist */ |
| 1180 | tl_assert(thr->hbthr); |
| 1181 | tl_assert(lk->hbso); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1182 | |
| 1183 | if (lk->heldBy == NULL) { |
| 1184 | /* the lock isn't held. Simple. */ |
| 1185 | tl_assert(!lk->heldW); |
| 1186 | lockN_acquire_reader( lk, thr ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1187 | /* acquire a dependency from the lock's VC */ |
| 1188 | libhb_so_recv( thr->hbthr, lk->hbso, False/*!strong_recv*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1189 | goto noerror; |
| 1190 | } |
| 1191 | |
| 1192 | /* So the lock is already held. If held as a w-lock then |
| 1193 | libpthread must be buggy. */ |
| 1194 | tl_assert(lk->heldBy); |
| 1195 | if (lk->heldW) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1196 | HG_(record_error_Misc)( thr, "Bug in libpthread: read lock " |
| 1197 | "granted on rwlock which is " |
| 1198 | "currently wr-held"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1199 | goto error; |
| 1200 | } |
| 1201 | |
| 1202 | /* Easy enough. In short anybody can get a read-lock on a rwlock |
| 1203 | provided it is either unlocked or already in rd-held. */ |
| 1204 | lockN_acquire_reader( lk, thr ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1205 | /* acquire a dependency from the lock's VC. Probably pointless, |
| 1206 | but also harmless. */ |
| 1207 | libhb_so_recv( thr->hbthr, lk->hbso, False/*!strong_recv*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1208 | goto noerror; |
| 1209 | |
| 1210 | noerror: |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 1211 | if (HG_(clo_track_lockorders)) { |
| 1212 | /* check lock order acquisition graph, and update. This has to |
| 1213 | happen before the lock is added to the thread's locksetA/W. */ |
| 1214 | laog__pre_thread_acquires_lock( thr, lk ); |
| 1215 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1216 | /* update the thread's held-locks set */ |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1217 | thr->locksetA = HG_(addToWS)( univ_lsets, thr->locksetA, (UWord)lk ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1218 | /* but don't update thr->locksetW, since lk is only rd-held */ |
| 1219 | /* fall through */ |
| 1220 | |
| 1221 | error: |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1222 | tl_assert(HG_(is_sane_LockN)(lk)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | /* The lock at 'lock_ga' is just about to be unlocked. Make all |
| 1227 | necessary updates, and also do all possible error checks. */ |
| 1228 | static |
| 1229 | void evhH__pre_thread_releases_lock ( Thread* thr, |
| 1230 | Addr lock_ga, Bool isRDWR ) |
| 1231 | { |
| 1232 | Lock* lock; |
| 1233 | Word n; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1234 | Bool was_heldW; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1235 | |
| 1236 | /* This routine is called prior to a lock release, before |
| 1237 | libpthread has had a chance to validate the call. Hence we need |
| 1238 | to detect and reject any attempts to move the lock into an |
| 1239 | invalid state. Such attempts are bugs in the client. |
| 1240 | |
| 1241 | isRDWR is True if we know from the wrapper context that lock_ga |
| 1242 | should refer to a reader-writer lock, and is False if [ditto] |
| 1243 | lock_ga should refer to a standard mutex. */ |
| 1244 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1245 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1246 | lock = map_locks_maybe_lookup( lock_ga ); |
| 1247 | |
| 1248 | if (!lock) { |
| 1249 | /* We know nothing about a lock at 'lock_ga'. Nevertheless |
| 1250 | the client is trying to unlock it. So complain, then ignore |
| 1251 | the attempt. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1252 | HG_(record_error_UnlockBogus)( thr, lock_ga ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1253 | return; |
| 1254 | } |
| 1255 | |
| 1256 | tl_assert(lock->guestaddr == lock_ga); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1257 | tl_assert(HG_(is_sane_LockN)(lock)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (isRDWR && lock->kind != LK_rdwr) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1260 | HG_(record_error_Misc)( thr, "pthread_rwlock_unlock with a " |
| 1261 | "pthread_mutex_t* argument " ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1262 | } |
| 1263 | if ((!isRDWR) && lock->kind == LK_rdwr) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1264 | HG_(record_error_Misc)( thr, "pthread_mutex_unlock with a " |
| 1265 | "pthread_rwlock_t* argument " ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | if (!lock->heldBy) { |
| 1269 | /* The lock is not held. This indicates a serious bug in the |
| 1270 | client. */ |
| 1271 | tl_assert(!lock->heldW); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1272 | HG_(record_error_UnlockUnlocked)( thr, lock ); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1273 | tl_assert(!HG_(elemWS)( univ_lsets, thr->locksetA, (UWord)lock )); |
| 1274 | tl_assert(!HG_(elemWS)( univ_lsets, thr->locksetW, (UWord)lock )); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1275 | goto error; |
| 1276 | } |
| 1277 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1278 | /* test just above dominates */ |
| 1279 | tl_assert(lock->heldBy); |
| 1280 | was_heldW = lock->heldW; |
| 1281 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1282 | /* The lock is held. Is this thread one of the holders? If not, |
| 1283 | report a bug in the client. */ |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1284 | n = VG_(elemBag)( lock->heldBy, (UWord)thr ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1285 | tl_assert(n >= 0); |
| 1286 | if (n == 0) { |
| 1287 | /* We are not a current holder of the lock. This is a bug in |
| 1288 | the guest, and (per POSIX pthread rules) the unlock |
| 1289 | attempt will fail. So just complain and do nothing |
| 1290 | else. */ |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 1291 | Thread* realOwner = (Thread*)VG_(anyElementOfBag)( lock->heldBy ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1292 | tl_assert(HG_(is_sane_Thread)(realOwner)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1293 | tl_assert(realOwner != thr); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1294 | tl_assert(!HG_(elemWS)( univ_lsets, thr->locksetA, (UWord)lock )); |
| 1295 | tl_assert(!HG_(elemWS)( univ_lsets, thr->locksetW, (UWord)lock )); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1296 | HG_(record_error_UnlockForeign)( thr, realOwner, lock ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1297 | goto error; |
| 1298 | } |
| 1299 | |
| 1300 | /* Ok, we hold the lock 'n' times. */ |
| 1301 | tl_assert(n >= 1); |
| 1302 | |
| 1303 | lockN_release( lock, thr ); |
| 1304 | |
| 1305 | n--; |
| 1306 | tl_assert(n >= 0); |
| 1307 | |
| 1308 | if (n > 0) { |
| 1309 | tl_assert(lock->heldBy); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1310 | tl_assert(n == VG_(elemBag)( lock->heldBy, (UWord)thr )); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1311 | /* We still hold the lock. So either it's a recursive lock |
| 1312 | or a rwlock which is currently r-held. */ |
| 1313 | tl_assert(lock->kind == LK_mbRec |
| 1314 | || (lock->kind == LK_rdwr && !lock->heldW)); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1315 | tl_assert(HG_(elemWS)( univ_lsets, thr->locksetA, (UWord)lock )); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1316 | if (lock->heldW) |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1317 | tl_assert(HG_(elemWS)( univ_lsets, thr->locksetW, (UWord)lock )); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1318 | else |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1319 | tl_assert(!HG_(elemWS)( univ_lsets, thr->locksetW, (UWord)lock )); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1320 | } else { |
sewardj | 983f302 | 2009-05-21 14:49:55 +0000 | [diff] [blame] | 1321 | /* n is zero. This means we don't hold the lock any more. But |
| 1322 | if it's a rwlock held in r-mode, someone else could still |
| 1323 | hold it. Just do whatever sanity checks we can. */ |
| 1324 | if (lock->kind == LK_rdwr && lock->heldBy) { |
| 1325 | /* It's a rwlock. We no longer hold it but we used to; |
| 1326 | nevertheless it still appears to be held by someone else. |
| 1327 | The implication is that, prior to this release, it must |
| 1328 | have been shared by us and and whoever else is holding it; |
| 1329 | which in turn implies it must be r-held, since a lock |
| 1330 | can't be w-held by more than one thread. */ |
| 1331 | /* The lock is now R-held by somebody else: */ |
| 1332 | tl_assert(lock->heldW == False); |
| 1333 | } else { |
| 1334 | /* Normal case. It's either not a rwlock, or it's a rwlock |
| 1335 | that we used to hold in w-mode (which is pretty much the |
| 1336 | same thing as a non-rwlock.) Since this transaction is |
| 1337 | atomic (V does not allow multiple threads to run |
| 1338 | simultaneously), it must mean the lock is now not held by |
| 1339 | anybody. Hence assert for it. */ |
| 1340 | /* The lock is now not held by anybody: */ |
| 1341 | tl_assert(!lock->heldBy); |
| 1342 | tl_assert(lock->heldW == False); |
| 1343 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1344 | //if (lock->heldBy) { |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1345 | // tl_assert(0 == VG_(elemBag)( lock->heldBy, (UWord)thr )); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1346 | //} |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1347 | /* update this thread's lockset accordingly. */ |
| 1348 | thr->locksetA |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1349 | = HG_(delFromWS)( univ_lsets, thr->locksetA, (UWord)lock ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1350 | thr->locksetW |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 1351 | = HG_(delFromWS)( univ_lsets, thr->locksetW, (UWord)lock ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1352 | /* push our VC into the lock */ |
| 1353 | tl_assert(thr->hbthr); |
| 1354 | tl_assert(lock->hbso); |
| 1355 | /* If the lock was previously W-held, then we want to do a |
| 1356 | strong send, and if previously R-held, then a weak send. */ |
| 1357 | libhb_so_send( thr->hbthr, lock->hbso, was_heldW ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1358 | } |
| 1359 | /* fall through */ |
| 1360 | |
| 1361 | error: |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1362 | tl_assert(HG_(is_sane_LockN)(lock)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 1366 | /* ---------------------------------------------------------- */ |
| 1367 | /* -------- Event handlers proper (evh__* functions) -------- */ |
| 1368 | /* ---------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1369 | |
| 1370 | /* What is the Thread* for the currently running thread? This is |
| 1371 | absolutely performance critical. We receive notifications from the |
| 1372 | core for client code starts/stops, and cache the looked-up result |
| 1373 | in 'current_Thread'. Hence, for the vast majority of requests, |
| 1374 | finding the current thread reduces to a read of a global variable, |
| 1375 | provided get_current_Thread_in_C_C is inlined. |
| 1376 | |
| 1377 | Outside of client code, current_Thread is NULL, and presumably |
| 1378 | any uses of it will cause a segfault. Hence: |
| 1379 | |
| 1380 | - for uses definitely within client code, use |
| 1381 | get_current_Thread_in_C_C. |
| 1382 | |
| 1383 | - for all other uses, use get_current_Thread. |
| 1384 | */ |
| 1385 | |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1386 | static Thread *current_Thread = NULL, |
| 1387 | *current_Thread_prev = NULL; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1388 | |
| 1389 | static void evh__start_client_code ( ThreadId tid, ULong nDisp ) { |
| 1390 | if (0) VG_(printf)("start %d %llu\n", (Int)tid, nDisp); |
| 1391 | tl_assert(current_Thread == NULL); |
| 1392 | current_Thread = map_threads_lookup( tid ); |
| 1393 | tl_assert(current_Thread != NULL); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1394 | if (current_Thread != current_Thread_prev) { |
| 1395 | libhb_Thr_resumes( current_Thread->hbthr ); |
| 1396 | current_Thread_prev = current_Thread; |
| 1397 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1398 | } |
| 1399 | static void evh__stop_client_code ( ThreadId tid, ULong nDisp ) { |
| 1400 | if (0) VG_(printf)(" stop %d %llu\n", (Int)tid, nDisp); |
| 1401 | tl_assert(current_Thread != NULL); |
| 1402 | current_Thread = NULL; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1403 | libhb_maybe_GC(); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1404 | } |
| 1405 | static inline Thread* get_current_Thread_in_C_C ( void ) { |
| 1406 | return current_Thread; |
| 1407 | } |
| 1408 | static inline Thread* get_current_Thread ( void ) { |
| 1409 | ThreadId coretid; |
| 1410 | Thread* thr; |
| 1411 | thr = get_current_Thread_in_C_C(); |
| 1412 | if (LIKELY(thr)) |
| 1413 | return thr; |
| 1414 | /* evidently not in client code. Do it the slow way. */ |
| 1415 | coretid = VG_(get_running_tid)(); |
| 1416 | /* FIXME: get rid of the following kludge. It exists because |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1417 | evh__new_mem is called during initialisation (as notification |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1418 | of initial memory layout) and VG_(get_running_tid)() returns |
| 1419 | VG_INVALID_THREADID at that point. */ |
| 1420 | if (coretid == VG_INVALID_THREADID) |
| 1421 | coretid = 1; /* KLUDGE */ |
| 1422 | thr = map_threads_lookup( coretid ); |
| 1423 | return thr; |
| 1424 | } |
| 1425 | |
| 1426 | static |
| 1427 | void evh__new_mem ( Addr a, SizeT len ) { |
| 1428 | if (SHOW_EVENTS >= 2) |
| 1429 | VG_(printf)("evh__new_mem(%p, %lu)\n", (void*)a, len ); |
| 1430 | shadow_mem_make_New( get_current_Thread(), a, len ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1431 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1432 | all__sanity_check("evh__new_mem-post"); |
| 1433 | } |
| 1434 | |
| 1435 | static |
sewardj | 1f77fec | 2010-04-12 19:51:04 +0000 | [diff] [blame] | 1436 | void evh__new_mem_stack ( Addr a, SizeT len ) { |
| 1437 | if (SHOW_EVENTS >= 2) |
| 1438 | VG_(printf)("evh__new_mem_stack(%p, %lu)\n", (void*)a, len ); |
| 1439 | shadow_mem_make_New( get_current_Thread(), |
| 1440 | -VG_STACK_REDZONE_SZB + a, len ); |
| 1441 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
| 1442 | all__sanity_check("evh__new_mem_stack-post"); |
| 1443 | } |
| 1444 | |
| 1445 | static |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1446 | void evh__new_mem_w_tid ( Addr a, SizeT len, ThreadId tid ) { |
| 1447 | if (SHOW_EVENTS >= 2) |
| 1448 | VG_(printf)("evh__new_mem_w_tid(%p, %lu)\n", (void*)a, len ); |
| 1449 | shadow_mem_make_New( get_current_Thread(), a, len ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1450 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1451 | all__sanity_check("evh__new_mem_w_tid-post"); |
| 1452 | } |
| 1453 | |
| 1454 | static |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1455 | void evh__new_mem_w_perms ( Addr a, SizeT len, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1456 | Bool rr, Bool ww, Bool xx, ULong di_handle ) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1457 | if (SHOW_EVENTS >= 1) |
| 1458 | VG_(printf)("evh__new_mem_w_perms(%p, %lu, %d,%d,%d)\n", |
| 1459 | (void*)a, len, (Int)rr, (Int)ww, (Int)xx ); |
| 1460 | if (rr || ww || xx) |
| 1461 | shadow_mem_make_New( get_current_Thread(), a, len ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1462 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1463 | all__sanity_check("evh__new_mem_w_perms-post"); |
| 1464 | } |
| 1465 | |
| 1466 | static |
| 1467 | void evh__set_perms ( Addr a, SizeT len, |
| 1468 | Bool rr, Bool ww, Bool xx ) { |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1469 | // This handles mprotect requests. If the memory is being put |
| 1470 | // into no-R no-W state, paint it as NoAccess, for the reasons |
| 1471 | // documented at evh__die_mem_munmap(). |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1472 | if (SHOW_EVENTS >= 1) |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1473 | VG_(printf)("evh__set_perms(%p, %lu, r=%d w=%d x=%d)\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1474 | (void*)a, len, (Int)rr, (Int)ww, (Int)xx ); |
| 1475 | /* Hmm. What should we do here, that actually makes any sense? |
| 1476 | Let's say: if neither readable nor writable, then declare it |
| 1477 | NoAccess, else leave it alone. */ |
| 1478 | if (!(rr || ww)) |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1479 | shadow_mem_make_NoAccess_AHAE( get_current_Thread(), a, len ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1480 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1481 | all__sanity_check("evh__set_perms-post"); |
| 1482 | } |
| 1483 | |
| 1484 | static |
| 1485 | void evh__die_mem ( Addr a, SizeT len ) { |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1486 | // Urr, libhb ignores this. |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1487 | if (SHOW_EVENTS >= 2) |
| 1488 | VG_(printf)("evh__die_mem(%p, %lu)\n", (void*)a, len ); |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1489 | shadow_mem_make_NoAccess_NoFX( get_current_Thread(), a, len ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1490 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1491 | all__sanity_check("evh__die_mem-post"); |
| 1492 | } |
| 1493 | |
| 1494 | static |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1495 | void evh__die_mem_munmap ( Addr a, SizeT len ) { |
| 1496 | // It's important that libhb doesn't ignore this. If, as is likely, |
| 1497 | // the client is subject to address space layout randomization, |
| 1498 | // then unmapped areas may never get remapped over, even in long |
| 1499 | // runs. If we just ignore them we wind up with large resource |
| 1500 | // (VTS) leaks in libhb. So force them to NoAccess, so that all |
| 1501 | // VTS references in the affected area are dropped. Marking memory |
| 1502 | // as NoAccess is expensive, but we assume that munmap is sufficiently |
| 1503 | // rare that the space gains of doing this are worth the costs. |
| 1504 | if (SHOW_EVENTS >= 2) |
| 1505 | VG_(printf)("evh__die_mem_munmap(%p, %lu)\n", (void*)a, len ); |
| 1506 | shadow_mem_make_NoAccess_AHAE( get_current_Thread(), a, len ); |
| 1507 | } |
| 1508 | |
| 1509 | static |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 1510 | void evh__untrack_mem ( Addr a, SizeT len ) { |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1511 | // Libhb doesn't ignore this. |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 1512 | if (SHOW_EVENTS >= 2) |
| 1513 | VG_(printf)("evh__untrack_mem(%p, %lu)\n", (void*)a, len ); |
| 1514 | shadow_mem_make_Untracked( get_current_Thread(), a, len ); |
| 1515 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
| 1516 | all__sanity_check("evh__untrack_mem-post"); |
| 1517 | } |
| 1518 | |
| 1519 | static |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1520 | void evh__copy_mem ( Addr src, Addr dst, SizeT len ) { |
| 1521 | if (SHOW_EVENTS >= 2) |
| 1522 | VG_(printf)("evh__copy_mem(%p, %p, %lu)\n", (void*)src, (void*)dst, len ); |
| 1523 | shadow_mem_scopy_range( get_current_Thread(), src, dst, len ); |
| 1524 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
| 1525 | all__sanity_check("evh__copy_mem-post"); |
| 1526 | } |
| 1527 | |
| 1528 | static |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1529 | void evh__pre_thread_ll_create ( ThreadId parent, ThreadId child ) |
| 1530 | { |
| 1531 | if (SHOW_EVENTS >= 1) |
| 1532 | VG_(printf)("evh__pre_thread_ll_create(p=%d, c=%d)\n", |
| 1533 | (Int)parent, (Int)child ); |
| 1534 | |
| 1535 | if (parent != VG_INVALID_THREADID) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1536 | Thread* thr_p; |
| 1537 | Thread* thr_c; |
| 1538 | Thr* hbthr_p; |
| 1539 | Thr* hbthr_c; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1540 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1541 | tl_assert(HG_(is_sane_ThreadId)(parent)); |
| 1542 | tl_assert(HG_(is_sane_ThreadId)(child)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1543 | tl_assert(parent != child); |
| 1544 | |
| 1545 | thr_p = map_threads_maybe_lookup( parent ); |
| 1546 | thr_c = map_threads_maybe_lookup( child ); |
| 1547 | |
| 1548 | tl_assert(thr_p != NULL); |
| 1549 | tl_assert(thr_c == NULL); |
| 1550 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1551 | hbthr_p = thr_p->hbthr; |
| 1552 | tl_assert(hbthr_p != NULL); |
sewardj | 6062664 | 2011-03-10 15:14:37 +0000 | [diff] [blame] | 1553 | tl_assert( libhb_get_Thr_hgthread(hbthr_p) == thr_p ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1554 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1555 | hbthr_c = libhb_create ( hbthr_p ); |
| 1556 | |
| 1557 | /* Create a new thread record for the child. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1558 | /* a Thread for the new thread ... */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1559 | thr_c = mk_Thread( hbthr_c ); |
sewardj | 6062664 | 2011-03-10 15:14:37 +0000 | [diff] [blame] | 1560 | tl_assert( libhb_get_Thr_hgthread(hbthr_c) == NULL ); |
| 1561 | libhb_set_Thr_hgthread(hbthr_c, thr_c); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1562 | |
| 1563 | /* and bind it in the thread-map table */ |
| 1564 | map_threads[child] = thr_c; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1565 | tl_assert(thr_c->coretid == VG_INVALID_THREADID); |
| 1566 | thr_c->coretid = child; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1567 | |
| 1568 | /* Record where the parent is so we can later refer to this in |
| 1569 | error messages. |
| 1570 | |
| 1571 | On amd64-linux, this entails a nasty glibc-2.5 specific hack. |
| 1572 | The stack snapshot is taken immediately after the parent has |
| 1573 | returned from its sys_clone call. Unfortunately there is no |
| 1574 | unwind info for the insn following "syscall" - reading the |
| 1575 | glibc sources confirms this. So we ask for a snapshot to be |
| 1576 | taken as if RIP was 3 bytes earlier, in a place where there |
| 1577 | is unwind info. Sigh. |
| 1578 | */ |
| 1579 | { Word first_ip_delta = 0; |
| 1580 | # if defined(VGP_amd64_linux) |
| 1581 | first_ip_delta = -3; |
| 1582 | # endif |
| 1583 | thr_c->created_at = VG_(record_ExeContext)(parent, first_ip_delta); |
| 1584 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1587 | if (HG_(clo_sanity_flags) & SCE_THREADS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1588 | all__sanity_check("evh__pre_thread_create-post"); |
| 1589 | } |
| 1590 | |
| 1591 | static |
| 1592 | void evh__pre_thread_ll_exit ( ThreadId quit_tid ) |
| 1593 | { |
| 1594 | Int nHeld; |
| 1595 | Thread* thr_q; |
| 1596 | if (SHOW_EVENTS >= 1) |
| 1597 | VG_(printf)("evh__pre_thread_ll_exit(thr=%d)\n", |
| 1598 | (Int)quit_tid ); |
| 1599 | |
| 1600 | /* quit_tid has disappeared without joining to any other thread. |
| 1601 | Therefore there is no synchronisation event associated with its |
| 1602 | exit and so we have to pretty much treat it as if it was still |
| 1603 | alive but mysteriously making no progress. That is because, if |
| 1604 | we don't know when it really exited, then we can never say there |
| 1605 | is a point in time when we're sure the thread really has |
| 1606 | finished, and so we need to consider the possibility that it |
| 1607 | lingers indefinitely and continues to interact with other |
| 1608 | threads. */ |
| 1609 | /* However, it might have rendezvous'd with a thread that called |
| 1610 | pthread_join with this one as arg, prior to this point (that's |
| 1611 | how NPTL works). In which case there has already been a prior |
| 1612 | sync event. So in any case, just let the thread exit. On NPTL, |
| 1613 | all thread exits go through here. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1614 | tl_assert(HG_(is_sane_ThreadId)(quit_tid)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1615 | thr_q = map_threads_maybe_lookup( quit_tid ); |
| 1616 | tl_assert(thr_q != NULL); |
| 1617 | |
| 1618 | /* Complain if this thread holds any locks. */ |
| 1619 | nHeld = HG_(cardinalityWS)( univ_lsets, thr_q->locksetA ); |
| 1620 | tl_assert(nHeld >= 0); |
| 1621 | if (nHeld > 0) { |
| 1622 | HChar buf[80]; |
| 1623 | VG_(sprintf)(buf, "Exiting thread still holds %d lock%s", |
| 1624 | nHeld, nHeld > 1 ? "s" : ""); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1625 | HG_(record_error_Misc)( thr_q, buf ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1628 | /* Not much to do here: |
| 1629 | - tell libhb the thread is gone |
| 1630 | - clear the map_threads entry, in order that the Valgrind core |
| 1631 | can re-use it. */ |
sewardj | 61bc2c5 | 2011-02-09 10:34:00 +0000 | [diff] [blame] | 1632 | /* Cleanup actions (next 5 lines) copied in evh__atfork_child; keep |
| 1633 | in sync. */ |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1634 | tl_assert(thr_q->hbthr); |
| 1635 | libhb_async_exit(thr_q->hbthr); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1636 | tl_assert(thr_q->coretid == quit_tid); |
| 1637 | thr_q->coretid = VG_INVALID_THREADID; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1638 | map_threads_delete( quit_tid ); |
| 1639 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1640 | if (HG_(clo_sanity_flags) & SCE_THREADS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1641 | all__sanity_check("evh__pre_thread_ll_exit-post"); |
| 1642 | } |
| 1643 | |
sewardj | 61bc2c5 | 2011-02-09 10:34:00 +0000 | [diff] [blame] | 1644 | /* This is called immediately after fork, for the child only. 'tid' |
| 1645 | is the only surviving thread (as per POSIX rules on fork() in |
| 1646 | threaded programs), so we have to clean up map_threads to remove |
| 1647 | entries for any other threads. */ |
| 1648 | static |
| 1649 | void evh__atfork_child ( ThreadId tid ) |
| 1650 | { |
| 1651 | UInt i; |
| 1652 | Thread* thr; |
| 1653 | /* Slot 0 should never be used. */ |
| 1654 | thr = map_threads_maybe_lookup( 0/*INVALID*/ ); |
| 1655 | tl_assert(!thr); |
| 1656 | /* Clean up all other slots except 'tid'. */ |
| 1657 | for (i = 1; i < VG_N_THREADS; i++) { |
| 1658 | if (i == tid) |
| 1659 | continue; |
| 1660 | thr = map_threads_maybe_lookup(i); |
| 1661 | if (!thr) |
| 1662 | continue; |
| 1663 | /* Cleanup actions (next 5 lines) copied from end of |
| 1664 | evh__pre_thread_ll_exit; keep in sync. */ |
| 1665 | tl_assert(thr->hbthr); |
| 1666 | libhb_async_exit(thr->hbthr); |
| 1667 | tl_assert(thr->coretid == i); |
| 1668 | thr->coretid = VG_INVALID_THREADID; |
| 1669 | map_threads_delete(i); |
| 1670 | } |
| 1671 | } |
| 1672 | |
philippe | d40aff5 | 2014-06-16 20:00:14 +0000 | [diff] [blame] | 1673 | /* generate a dependence from the hbthr_q quitter to the hbthr_s stayer. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1674 | static |
philippe | d40aff5 | 2014-06-16 20:00:14 +0000 | [diff] [blame] | 1675 | void generate_quitter_stayer_dependence (Thr* hbthr_q, Thr* hbthr_s) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1676 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1677 | SO* so; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1678 | /* Allocate a temporary synchronisation object and use it to send |
| 1679 | an imaginary message from the quitter to the stayer, the purpose |
| 1680 | being to generate a dependence from the quitter to the |
| 1681 | stayer. */ |
| 1682 | so = libhb_so_alloc(); |
| 1683 | tl_assert(so); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1684 | /* Send last arg of _so_send as False, since the sending thread |
| 1685 | doesn't actually exist any more, so we don't want _so_send to |
| 1686 | try taking stack snapshots of it. */ |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 1687 | libhb_so_send(hbthr_q, so, True/*strong_send*//*?!? wrt comment above*/); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1688 | libhb_so_recv(hbthr_s, so, True/*strong_recv*/); |
| 1689 | libhb_so_dealloc(so); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1690 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 1691 | /* Tell libhb that the quitter has been reaped. Note that we might |
| 1692 | have to be cleverer about this, to exclude 2nd and subsequent |
| 1693 | notifications for the same hbthr_q, in the case where the app is |
| 1694 | buggy (calls pthread_join twice or more on the same thread) AND |
| 1695 | where libpthread is also buggy and doesn't return ESRCH on |
| 1696 | subsequent calls. (If libpthread isn't thusly buggy, then the |
| 1697 | wrapper for pthread_join in hg_intercepts.c will stop us getting |
| 1698 | notified here multiple times for the same joinee.) See also |
| 1699 | comments in helgrind/tests/jointwice.c. */ |
| 1700 | libhb_joinedwith_done(hbthr_q); |
philippe | d40aff5 | 2014-06-16 20:00:14 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | |
| 1704 | static |
| 1705 | void evh__HG_PTHREAD_JOIN_POST ( ThreadId stay_tid, Thread* quit_thr ) |
| 1706 | { |
| 1707 | Thread* thr_s; |
| 1708 | Thread* thr_q; |
| 1709 | Thr* hbthr_s; |
| 1710 | Thr* hbthr_q; |
| 1711 | |
| 1712 | if (SHOW_EVENTS >= 1) |
| 1713 | VG_(printf)("evh__post_thread_join(stayer=%d, quitter=%p)\n", |
| 1714 | (Int)stay_tid, quit_thr ); |
| 1715 | |
| 1716 | tl_assert(HG_(is_sane_ThreadId)(stay_tid)); |
| 1717 | |
| 1718 | thr_s = map_threads_maybe_lookup( stay_tid ); |
| 1719 | thr_q = quit_thr; |
| 1720 | tl_assert(thr_s != NULL); |
| 1721 | tl_assert(thr_q != NULL); |
| 1722 | tl_assert(thr_s != thr_q); |
| 1723 | |
| 1724 | hbthr_s = thr_s->hbthr; |
| 1725 | hbthr_q = thr_q->hbthr; |
| 1726 | tl_assert(hbthr_s != hbthr_q); |
| 1727 | tl_assert( libhb_get_Thr_hgthread(hbthr_s) == thr_s ); |
| 1728 | tl_assert( libhb_get_Thr_hgthread(hbthr_q) == thr_q ); |
| 1729 | |
| 1730 | generate_quitter_stayer_dependence (hbthr_q, hbthr_s); |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 1731 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1732 | /* evh__pre_thread_ll_exit issues an error message if the exiting |
| 1733 | thread holds any locks. No need to check here. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1734 | |
| 1735 | /* This holds because, at least when using NPTL as the thread |
| 1736 | library, we should be notified the low level thread exit before |
| 1737 | we hear of any join event on it. The low level exit |
| 1738 | notification feeds through into evh__pre_thread_ll_exit, |
| 1739 | which should clear the map_threads entry for it. Hence we |
| 1740 | expect there to be no map_threads entry at this point. */ |
| 1741 | tl_assert( map_threads_maybe_reverse_lookup_SLOW(thr_q) |
| 1742 | == VG_INVALID_THREADID); |
| 1743 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1744 | if (HG_(clo_sanity_flags) & SCE_THREADS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1745 | all__sanity_check("evh__post_thread_join-post"); |
| 1746 | } |
| 1747 | |
| 1748 | static |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1749 | void evh__pre_mem_read ( CorePart part, ThreadId tid, const HChar* s, |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1750 | Addr a, SizeT size) { |
| 1751 | if (SHOW_EVENTS >= 2 |
| 1752 | || (SHOW_EVENTS >= 1 && size != 1)) |
| 1753 | VG_(printf)("evh__pre_mem_read(ctid=%d, \"%s\", %p, %lu)\n", |
| 1754 | (Int)tid, s, (void*)a, size ); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1755 | shadow_mem_cread_range( map_threads_lookup(tid), a, size); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1756 | if (size >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1757 | all__sanity_check("evh__pre_mem_read-post"); |
| 1758 | } |
| 1759 | |
| 1760 | static |
| 1761 | void evh__pre_mem_read_asciiz ( CorePart part, ThreadId tid, |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1762 | const HChar* s, Addr a ) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1763 | Int len; |
| 1764 | if (SHOW_EVENTS >= 1) |
| 1765 | VG_(printf)("evh__pre_mem_asciiz(ctid=%d, \"%s\", %p)\n", |
| 1766 | (Int)tid, s, (void*)a ); |
sewardj | 234e558 | 2011-02-09 12:47:23 +0000 | [diff] [blame] | 1767 | // Don't segfault if the string starts in an obviously stupid |
| 1768 | // place. Actually we should check the whole string, not just |
| 1769 | // the start address, but that's too much trouble. At least |
| 1770 | // checking the first byte is better than nothing. See #255009. |
| 1771 | if (!VG_(am_is_valid_for_client) (a, 1, VKI_PROT_READ)) |
| 1772 | return; |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1773 | len = VG_(strlen)( (HChar*) a ); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1774 | shadow_mem_cread_range( map_threads_lookup(tid), a, len+1 ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1775 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1776 | all__sanity_check("evh__pre_mem_read_asciiz-post"); |
| 1777 | } |
| 1778 | |
| 1779 | static |
florian | e543f30 | 2012-10-21 19:43:43 +0000 | [diff] [blame] | 1780 | void evh__pre_mem_write ( CorePart part, ThreadId tid, const HChar* s, |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1781 | Addr a, SizeT size ) { |
| 1782 | if (SHOW_EVENTS >= 1) |
| 1783 | VG_(printf)("evh__pre_mem_write(ctid=%d, \"%s\", %p, %lu)\n", |
| 1784 | (Int)tid, s, (void*)a, size ); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1785 | shadow_mem_cwrite_range( map_threads_lookup(tid), a, size); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1786 | if (size >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1787 | all__sanity_check("evh__pre_mem_write-post"); |
| 1788 | } |
| 1789 | |
| 1790 | static |
| 1791 | void evh__new_mem_heap ( Addr a, SizeT len, Bool is_inited ) { |
| 1792 | if (SHOW_EVENTS >= 1) |
| 1793 | VG_(printf)("evh__new_mem_heap(%p, %lu, inited=%d)\n", |
| 1794 | (void*)a, len, (Int)is_inited ); |
| 1795 | // FIXME: this is kinda stupid |
| 1796 | if (is_inited) { |
| 1797 | shadow_mem_make_New(get_current_Thread(), a, len); |
| 1798 | } else { |
| 1799 | shadow_mem_make_New(get_current_Thread(), a, len); |
| 1800 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1801 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1802 | all__sanity_check("evh__pre_mem_read-post"); |
| 1803 | } |
| 1804 | |
| 1805 | static |
| 1806 | void evh__die_mem_heap ( Addr a, SizeT len ) { |
sewardj | 622fe49 | 2011-03-11 21:06:59 +0000 | [diff] [blame] | 1807 | Thread* thr; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1808 | if (SHOW_EVENTS >= 1) |
| 1809 | VG_(printf)("evh__die_mem_heap(%p, %lu)\n", (void*)a, len ); |
sewardj | 622fe49 | 2011-03-11 21:06:59 +0000 | [diff] [blame] | 1810 | thr = get_current_Thread(); |
| 1811 | tl_assert(thr); |
| 1812 | if (HG_(clo_free_is_write)) { |
| 1813 | /* Treat frees as if the memory was written immediately prior to |
| 1814 | the free. This shakes out more races, specifically, cases |
| 1815 | where memory is referenced by one thread, and freed by |
| 1816 | another, and there's no observable synchronisation event to |
| 1817 | guarantee that the reference happens before the free. */ |
| 1818 | shadow_mem_cwrite_range(thr, a, len); |
| 1819 | } |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1820 | shadow_mem_make_NoAccess_NoFX( thr, a, len ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1821 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1822 | all__sanity_check("evh__pre_mem_read-post"); |
| 1823 | } |
| 1824 | |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1825 | /* --- Event handlers called from generated code --- */ |
| 1826 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1827 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1828 | void evh__mem_help_cread_1(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1829 | Thread* thr = get_current_Thread_in_C_C(); |
| 1830 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1831 | LIBHB_CREAD_1(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1832 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1833 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1834 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1835 | void evh__mem_help_cread_2(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1836 | Thread* thr = get_current_Thread_in_C_C(); |
| 1837 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1838 | LIBHB_CREAD_2(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1839 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1840 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1841 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1842 | void evh__mem_help_cread_4(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1843 | Thread* thr = get_current_Thread_in_C_C(); |
| 1844 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1845 | LIBHB_CREAD_4(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1846 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1847 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1848 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1849 | void evh__mem_help_cread_8(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1850 | Thread* thr = get_current_Thread_in_C_C(); |
| 1851 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1852 | LIBHB_CREAD_8(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1853 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1854 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1855 | static VG_REGPARM(2) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1856 | void evh__mem_help_cread_N(Addr a, SizeT size) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1857 | Thread* thr = get_current_Thread_in_C_C(); |
| 1858 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1859 | LIBHB_CREAD_N(hbthr, a, size); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1863 | void evh__mem_help_cwrite_1(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1864 | Thread* thr = get_current_Thread_in_C_C(); |
| 1865 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1866 | LIBHB_CWRITE_1(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1867 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1868 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1869 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1870 | void evh__mem_help_cwrite_2(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1871 | Thread* thr = get_current_Thread_in_C_C(); |
| 1872 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1873 | LIBHB_CWRITE_2(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1874 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1875 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1876 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1877 | void evh__mem_help_cwrite_4(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1878 | Thread* thr = get_current_Thread_in_C_C(); |
| 1879 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1880 | LIBHB_CWRITE_4(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1881 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1882 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1883 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1884 | void evh__mem_help_cwrite_8(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1885 | Thread* thr = get_current_Thread_in_C_C(); |
| 1886 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1887 | LIBHB_CWRITE_8(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1888 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1889 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1890 | static VG_REGPARM(2) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1891 | void evh__mem_help_cwrite_N(Addr a, SizeT size) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1892 | Thread* thr = get_current_Thread_in_C_C(); |
| 1893 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1894 | LIBHB_CWRITE_N(hbthr, a, size); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1897 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 1898 | /* ------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1899 | /* -------------- events to do with mutexes -------------- */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 1900 | /* ------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1901 | |
| 1902 | /* EXPOSITION only: by intercepting lock init events we can show the |
| 1903 | user where the lock was initialised, rather than only being able to |
| 1904 | show where it was first locked. Intercepting lock initialisations |
| 1905 | is not necessary for the basic operation of the race checker. */ |
| 1906 | static |
| 1907 | void evh__HG_PTHREAD_MUTEX_INIT_POST( ThreadId tid, |
| 1908 | void* mutex, Word mbRec ) |
| 1909 | { |
| 1910 | if (SHOW_EVENTS >= 1) |
| 1911 | VG_(printf)("evh__hg_PTHREAD_MUTEX_INIT_POST(ctid=%d, mbRec=%ld, %p)\n", |
| 1912 | (Int)tid, mbRec, (void*)mutex ); |
| 1913 | tl_assert(mbRec == 0 || mbRec == 1); |
| 1914 | map_locks_lookup_or_create( mbRec ? LK_mbRec : LK_nonRec, |
| 1915 | (Addr)mutex, tid ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1916 | if (HG_(clo_sanity_flags) & SCE_LOCKS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1917 | all__sanity_check("evh__hg_PTHREAD_MUTEX_INIT_POST"); |
| 1918 | } |
| 1919 | |
| 1920 | static |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 1921 | void evh__HG_PTHREAD_MUTEX_DESTROY_PRE( ThreadId tid, void* mutex, |
| 1922 | Bool mutex_is_init ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1923 | { |
| 1924 | Thread* thr; |
| 1925 | Lock* lk; |
| 1926 | if (SHOW_EVENTS >= 1) |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 1927 | VG_(printf)("evh__hg_PTHREAD_MUTEX_DESTROY_PRE" |
| 1928 | "(ctid=%d, %p, isInit=%d)\n", |
| 1929 | (Int)tid, (void*)mutex, (Int)mutex_is_init ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1930 | |
| 1931 | thr = map_threads_maybe_lookup( tid ); |
| 1932 | /* cannot fail - Thread* must already exist */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1933 | tl_assert( HG_(is_sane_Thread)(thr) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1934 | |
| 1935 | lk = map_locks_maybe_lookup( (Addr)mutex ); |
| 1936 | |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 1937 | if (lk == NULL && mutex_is_init) { |
| 1938 | /* We're destroying a mutex which we don't have any record of, |
| 1939 | and which appears to have the value PTHREAD_MUTEX_INITIALIZER. |
| 1940 | Assume it never got used, and so we don't need to do anything |
| 1941 | more. */ |
| 1942 | goto out; |
| 1943 | } |
| 1944 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1945 | if (lk == NULL || (lk->kind != LK_nonRec && lk->kind != LK_mbRec)) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1946 | HG_(record_error_Misc)( |
| 1947 | thr, "pthread_mutex_destroy with invalid argument" ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | if (lk) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1951 | tl_assert( HG_(is_sane_LockN)(lk) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1952 | tl_assert( lk->guestaddr == (Addr)mutex ); |
| 1953 | if (lk->heldBy) { |
| 1954 | /* Basically act like we unlocked the lock */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1955 | HG_(record_error_Misc)( |
| 1956 | thr, "pthread_mutex_destroy of a locked mutex" ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1957 | /* remove lock from locksets of all owning threads */ |
| 1958 | remove_Lock_from_locksets_of_all_owning_Threads( lk ); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 1959 | VG_(deleteBag)( lk->heldBy ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1960 | lk->heldBy = NULL; |
| 1961 | lk->heldW = False; |
| 1962 | lk->acquired_at = NULL; |
| 1963 | } |
| 1964 | tl_assert( !lk->heldBy ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1965 | tl_assert( HG_(is_sane_LockN)(lk) ); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 1966 | |
| 1967 | if (HG_(clo_track_lockorders)) |
| 1968 | laog__handle_one_lock_deletion(lk); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1969 | map_locks_delete( lk->guestaddr ); |
| 1970 | del_LockN( lk ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 1973 | out: |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1974 | if (HG_(clo_sanity_flags) & SCE_LOCKS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1975 | all__sanity_check("evh__hg_PTHREAD_MUTEX_DESTROY_PRE"); |
| 1976 | } |
| 1977 | |
| 1978 | static void evh__HG_PTHREAD_MUTEX_LOCK_PRE ( ThreadId tid, |
| 1979 | void* mutex, Word isTryLock ) |
| 1980 | { |
| 1981 | /* Just check the mutex is sane; nothing else to do. */ |
| 1982 | // 'mutex' may be invalid - not checked by wrapper |
| 1983 | Thread* thr; |
| 1984 | Lock* lk; |
| 1985 | if (SHOW_EVENTS >= 1) |
| 1986 | VG_(printf)("evh__hg_PTHREAD_MUTEX_LOCK_PRE(ctid=%d, mutex=%p)\n", |
| 1987 | (Int)tid, (void*)mutex ); |
| 1988 | |
| 1989 | tl_assert(isTryLock == 0 || isTryLock == 1); |
| 1990 | thr = map_threads_maybe_lookup( tid ); |
| 1991 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 1992 | |
| 1993 | lk = map_locks_maybe_lookup( (Addr)mutex ); |
| 1994 | |
| 1995 | if (lk && (lk->kind == LK_rdwr)) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1996 | HG_(record_error_Misc)( thr, "pthread_mutex_lock with a " |
| 1997 | "pthread_rwlock_t* argument " ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | if ( lk |
| 2001 | && isTryLock == 0 |
| 2002 | && (lk->kind == LK_nonRec || lk->kind == LK_rdwr) |
| 2003 | && lk->heldBy |
| 2004 | && lk->heldW |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 2005 | && VG_(elemBag)( lk->heldBy, (UWord)thr ) > 0 ) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2006 | /* uh, it's a non-recursive lock and we already w-hold it, and |
| 2007 | this is a real lock operation (not a speculative "tryLock" |
| 2008 | kind of thing). Duh. Deadlock coming up; but at least |
| 2009 | produce an error message. */ |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 2010 | const HChar* errstr = "Attempt to re-lock a " |
| 2011 | "non-recursive lock I already hold"; |
| 2012 | const HChar* auxstr = "Lock was previously acquired"; |
sewardj | 8fef625 | 2010-07-29 05:28:02 +0000 | [diff] [blame] | 2013 | if (lk->acquired_at) { |
| 2014 | HG_(record_error_Misc_w_aux)( thr, errstr, auxstr, lk->acquired_at ); |
| 2015 | } else { |
| 2016 | HG_(record_error_Misc)( thr, errstr ); |
| 2017 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | static void evh__HG_PTHREAD_MUTEX_LOCK_POST ( ThreadId tid, void* mutex ) |
| 2022 | { |
| 2023 | // only called if the real library call succeeded - so mutex is sane |
| 2024 | Thread* thr; |
| 2025 | if (SHOW_EVENTS >= 1) |
| 2026 | VG_(printf)("evh__HG_PTHREAD_MUTEX_LOCK_POST(ctid=%d, mutex=%p)\n", |
| 2027 | (Int)tid, (void*)mutex ); |
| 2028 | |
| 2029 | thr = map_threads_maybe_lookup( tid ); |
| 2030 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2031 | |
| 2032 | evhH__post_thread_w_acquires_lock( |
| 2033 | thr, |
| 2034 | LK_mbRec, /* if not known, create new lock with this LockKind */ |
| 2035 | (Addr)mutex |
| 2036 | ); |
| 2037 | } |
| 2038 | |
| 2039 | static void evh__HG_PTHREAD_MUTEX_UNLOCK_PRE ( ThreadId tid, void* mutex ) |
| 2040 | { |
| 2041 | // 'mutex' may be invalid - not checked by wrapper |
| 2042 | Thread* thr; |
| 2043 | if (SHOW_EVENTS >= 1) |
| 2044 | VG_(printf)("evh__HG_PTHREAD_MUTEX_UNLOCK_PRE(ctid=%d, mutex=%p)\n", |
| 2045 | (Int)tid, (void*)mutex ); |
| 2046 | |
| 2047 | thr = map_threads_maybe_lookup( tid ); |
| 2048 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2049 | |
| 2050 | evhH__pre_thread_releases_lock( thr, (Addr)mutex, False/*!isRDWR*/ ); |
| 2051 | } |
| 2052 | |
| 2053 | static void evh__HG_PTHREAD_MUTEX_UNLOCK_POST ( ThreadId tid, void* mutex ) |
| 2054 | { |
| 2055 | // only called if the real library call succeeded - so mutex is sane |
| 2056 | Thread* thr; |
| 2057 | if (SHOW_EVENTS >= 1) |
| 2058 | VG_(printf)("evh__hg_PTHREAD_MUTEX_UNLOCK_POST(ctid=%d, mutex=%p)\n", |
| 2059 | (Int)tid, (void*)mutex ); |
| 2060 | thr = map_threads_maybe_lookup( tid ); |
| 2061 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2062 | |
| 2063 | // anything we should do here? |
| 2064 | } |
| 2065 | |
| 2066 | |
sewardj | 5a644da | 2009-08-11 10:35:58 +0000 | [diff] [blame] | 2067 | /* ------------------------------------------------------- */ |
sewardj | 1f77fec | 2010-04-12 19:51:04 +0000 | [diff] [blame] | 2068 | /* -------------- events to do with spinlocks ------------ */ |
sewardj | 5a644da | 2009-08-11 10:35:58 +0000 | [diff] [blame] | 2069 | /* ------------------------------------------------------- */ |
| 2070 | |
| 2071 | /* All a bit of a kludge. Pretend we're really dealing with ordinary |
| 2072 | pthread_mutex_t's instead, for the most part. */ |
| 2073 | |
| 2074 | static void evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE( ThreadId tid, |
| 2075 | void* slock ) |
| 2076 | { |
| 2077 | Thread* thr; |
| 2078 | Lock* lk; |
| 2079 | /* In glibc's kludgey world, we're either initialising or unlocking |
| 2080 | it. Since this is the pre-routine, if it is locked, unlock it |
| 2081 | and take a dependence edge. Otherwise, do nothing. */ |
| 2082 | |
| 2083 | if (SHOW_EVENTS >= 1) |
| 2084 | VG_(printf)("evh__hg_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE" |
| 2085 | "(ctid=%d, slock=%p)\n", |
| 2086 | (Int)tid, (void*)slock ); |
| 2087 | |
| 2088 | thr = map_threads_maybe_lookup( tid ); |
| 2089 | /* cannot fail - Thread* must already exist */; |
| 2090 | tl_assert( HG_(is_sane_Thread)(thr) ); |
| 2091 | |
| 2092 | lk = map_locks_maybe_lookup( (Addr)slock ); |
| 2093 | if (lk && lk->heldBy) { |
| 2094 | /* it's held. So do the normal pre-unlock actions, as copied |
| 2095 | from evh__HG_PTHREAD_MUTEX_UNLOCK_PRE. This stupidly |
| 2096 | duplicates the map_locks_maybe_lookup. */ |
| 2097 | evhH__pre_thread_releases_lock( thr, (Addr)slock, |
| 2098 | False/*!isRDWR*/ ); |
| 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | static void evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_POST( ThreadId tid, |
| 2103 | void* slock ) |
| 2104 | { |
| 2105 | Lock* lk; |
| 2106 | /* More kludgery. If the lock has never been seen before, do |
| 2107 | actions as per evh__HG_PTHREAD_MUTEX_INIT_POST. Else do |
| 2108 | nothing. */ |
| 2109 | |
| 2110 | if (SHOW_EVENTS >= 1) |
| 2111 | VG_(printf)("evh__hg_PTHREAD_SPIN_INIT_OR_UNLOCK_POST" |
| 2112 | "(ctid=%d, slock=%p)\n", |
| 2113 | (Int)tid, (void*)slock ); |
| 2114 | |
| 2115 | lk = map_locks_maybe_lookup( (Addr)slock ); |
| 2116 | if (!lk) { |
| 2117 | map_locks_lookup_or_create( LK_nonRec, (Addr)slock, tid ); |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | static void evh__HG_PTHREAD_SPIN_LOCK_PRE( ThreadId tid, |
| 2122 | void* slock, Word isTryLock ) |
| 2123 | { |
| 2124 | evh__HG_PTHREAD_MUTEX_LOCK_PRE( tid, slock, isTryLock ); |
| 2125 | } |
| 2126 | |
| 2127 | static void evh__HG_PTHREAD_SPIN_LOCK_POST( ThreadId tid, |
| 2128 | void* slock ) |
| 2129 | { |
| 2130 | evh__HG_PTHREAD_MUTEX_LOCK_POST( tid, slock ); |
| 2131 | } |
| 2132 | |
| 2133 | static void evh__HG_PTHREAD_SPIN_DESTROY_PRE( ThreadId tid, |
| 2134 | void* slock ) |
| 2135 | { |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 2136 | evh__HG_PTHREAD_MUTEX_DESTROY_PRE( tid, slock, 0/*!isInit*/ ); |
sewardj | 5a644da | 2009-08-11 10:35:58 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2140 | /* ----------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2141 | /* --------------- events to do with CVs --------------- */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2142 | /* ----------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2143 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2144 | /* A mapping from CV to (the SO associated with it, plus some |
| 2145 | auxiliary data for error checking). When the CV is |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2146 | signalled/broadcasted upon, we do a 'send' into the SO, and when a |
| 2147 | wait on it completes, we do a 'recv' from the SO. This is believed |
| 2148 | to give the correct happens-before events arising from CV |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2149 | signallings/broadcasts. |
| 2150 | */ |
| 2151 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2152 | /* .so is the SO for this CV. |
| 2153 | .mx_ga is the associated mutex, when .nWaiters > 0 |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2154 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2155 | POSIX says effectively that the first pthread_cond_{timed}wait call |
| 2156 | causes a dynamic binding between the CV and the mutex, and that |
| 2157 | lasts until such time as the waiter count falls to zero. Hence |
| 2158 | need to keep track of the number of waiters in order to do |
| 2159 | consistency tracking. */ |
| 2160 | typedef |
| 2161 | struct { |
| 2162 | SO* so; /* libhb-allocated SO */ |
| 2163 | void* mx_ga; /* addr of associated mutex, if any */ |
| 2164 | UWord nWaiters; /* # threads waiting on the CV */ |
| 2165 | } |
| 2166 | CVInfo; |
| 2167 | |
| 2168 | |
| 2169 | /* pthread_cond_t* -> CVInfo* */ |
| 2170 | static WordFM* map_cond_to_CVInfo = NULL; |
| 2171 | |
| 2172 | static void map_cond_to_CVInfo_INIT ( void ) { |
| 2173 | if (UNLIKELY(map_cond_to_CVInfo == NULL)) { |
| 2174 | map_cond_to_CVInfo = VG_(newFM)( HG_(zalloc), |
| 2175 | "hg.mctCI.1", HG_(free), NULL ); |
| 2176 | tl_assert(map_cond_to_CVInfo != NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2177 | } |
| 2178 | } |
| 2179 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2180 | static CVInfo* map_cond_to_CVInfo_lookup_or_alloc ( void* cond ) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2181 | UWord key, val; |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2182 | map_cond_to_CVInfo_INIT(); |
| 2183 | if (VG_(lookupFM)( map_cond_to_CVInfo, &key, &val, (UWord)cond )) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2184 | tl_assert(key == (UWord)cond); |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2185 | return (CVInfo*)val; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2186 | } else { |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2187 | SO* so = libhb_so_alloc(); |
| 2188 | CVInfo* cvi = HG_(zalloc)("hg.mctCloa.1", sizeof(CVInfo)); |
| 2189 | cvi->so = so; |
| 2190 | cvi->mx_ga = 0; |
| 2191 | VG_(addToFM)( map_cond_to_CVInfo, (UWord)cond, (UWord)cvi ); |
| 2192 | return cvi; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2193 | } |
| 2194 | } |
| 2195 | |
philippe | 8bfc215 | 2012-07-06 23:38:24 +0000 | [diff] [blame] | 2196 | static CVInfo* map_cond_to_CVInfo_lookup_NO_alloc ( void* cond ) { |
| 2197 | UWord key, val; |
| 2198 | map_cond_to_CVInfo_INIT(); |
| 2199 | if (VG_(lookupFM)( map_cond_to_CVInfo, &key, &val, (UWord)cond )) { |
| 2200 | tl_assert(key == (UWord)cond); |
| 2201 | return (CVInfo*)val; |
| 2202 | } else { |
| 2203 | return NULL; |
| 2204 | } |
| 2205 | } |
| 2206 | |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 2207 | static void map_cond_to_CVInfo_delete ( ThreadId tid, |
| 2208 | void* cond, Bool cond_is_init ) { |
philippe | 8bfc215 | 2012-07-06 23:38:24 +0000 | [diff] [blame] | 2209 | Thread* thr; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2210 | UWord keyW, valW; |
philippe | 8bfc215 | 2012-07-06 23:38:24 +0000 | [diff] [blame] | 2211 | |
| 2212 | thr = map_threads_maybe_lookup( tid ); |
| 2213 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2214 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2215 | map_cond_to_CVInfo_INIT(); |
philippe | 2411197 | 2013-03-18 22:48:22 +0000 | [diff] [blame] | 2216 | if (VG_(lookupFM)( map_cond_to_CVInfo, &keyW, &valW, (UWord)cond )) { |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2217 | CVInfo* cvi = (CVInfo*)valW; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2218 | tl_assert(keyW == (UWord)cond); |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2219 | tl_assert(cvi); |
| 2220 | tl_assert(cvi->so); |
philippe | 8bfc215 | 2012-07-06 23:38:24 +0000 | [diff] [blame] | 2221 | if (cvi->nWaiters > 0) { |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 2222 | HG_(record_error_Misc)( |
| 2223 | thr, "pthread_cond_destroy:" |
| 2224 | " destruction of condition variable being waited upon"); |
philippe | 2411197 | 2013-03-18 22:48:22 +0000 | [diff] [blame] | 2225 | /* Destroying a cond var being waited upon outcome is EBUSY and |
| 2226 | variable is not destroyed. */ |
| 2227 | return; |
philippe | 8bfc215 | 2012-07-06 23:38:24 +0000 | [diff] [blame] | 2228 | } |
philippe | 2411197 | 2013-03-18 22:48:22 +0000 | [diff] [blame] | 2229 | if (!VG_(delFromFM)( map_cond_to_CVInfo, &keyW, &valW, (UWord)cond )) |
| 2230 | tl_assert(0); // cond var found above, and not here ??? |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2231 | libhb_so_dealloc(cvi->so); |
| 2232 | cvi->mx_ga = 0; |
| 2233 | HG_(free)(cvi); |
philippe | 8bfc215 | 2012-07-06 23:38:24 +0000 | [diff] [blame] | 2234 | } else { |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 2235 | /* We have no record of this CV. So complain about it |
| 2236 | .. except, don't bother to complain if it has exactly the |
| 2237 | value PTHREAD_COND_INITIALIZER, since it might be that the CV |
| 2238 | was initialised like that but never used. */ |
| 2239 | if (!cond_is_init) { |
| 2240 | HG_(record_error_Misc)( |
| 2241 | thr, "pthread_cond_destroy: destruction of unknown cond var"); |
| 2242 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | static void evh__HG_PTHREAD_COND_SIGNAL_PRE ( ThreadId tid, void* cond ) |
| 2247 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2248 | /* 'tid' has signalled on 'cond'. As per the comment above, bind |
| 2249 | cond to a SO if it is not already so bound, and 'send' on the |
| 2250 | SO. This is later used by other thread(s) which successfully |
| 2251 | exit from a pthread_cond_wait on the same cv; then they 'recv' |
| 2252 | from the SO, thereby acquiring a dependency on this signalling |
| 2253 | event. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2254 | Thread* thr; |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2255 | CVInfo* cvi; |
| 2256 | //Lock* lk; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2257 | |
| 2258 | if (SHOW_EVENTS >= 1) |
| 2259 | VG_(printf)("evh__HG_PTHREAD_COND_SIGNAL_PRE(ctid=%d, cond=%p)\n", |
| 2260 | (Int)tid, (void*)cond ); |
| 2261 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2262 | thr = map_threads_maybe_lookup( tid ); |
| 2263 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2264 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2265 | cvi = map_cond_to_CVInfo_lookup_or_alloc( cond ); |
| 2266 | tl_assert(cvi); |
| 2267 | tl_assert(cvi->so); |
| 2268 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2269 | // error-if: mutex is bogus |
| 2270 | // error-if: mutex is not locked |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2271 | // Hmm. POSIX doesn't actually say that it's an error to call |
| 2272 | // pthread_cond_signal with the associated mutex being unlocked. |
| 2273 | // Although it does say that it should be "if consistent scheduling |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 2274 | // is desired." For that reason, print "dubious" if the lock isn't |
| 2275 | // held by any thread. Skip the "dubious" if it is held by some |
| 2276 | // other thread; that sounds straight-out wrong. |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2277 | // |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 2278 | // Anybody who writes code that signals on a CV without holding |
| 2279 | // the associated MX needs to be shipped off to a lunatic asylum |
| 2280 | // ASAP, even though POSIX doesn't actually declare such behaviour |
| 2281 | // illegal -- it makes code extremely difficult to understand/ |
| 2282 | // reason about. In particular it puts the signalling thread in |
| 2283 | // a situation where it is racing against the released waiter |
| 2284 | // as soon as the signalling is done, and so there needs to be |
| 2285 | // some auxiliary synchronisation mechanism in the program that |
| 2286 | // makes this safe -- or the race(s) need to be harmless, or |
| 2287 | // probably nonexistent. |
| 2288 | // |
| 2289 | if (1) { |
| 2290 | Lock* lk = NULL; |
| 2291 | if (cvi->mx_ga != 0) { |
| 2292 | lk = map_locks_maybe_lookup( (Addr)cvi->mx_ga ); |
| 2293 | } |
| 2294 | /* note: lk could be NULL. Be careful. */ |
| 2295 | if (lk) { |
| 2296 | if (lk->kind == LK_rdwr) { |
| 2297 | HG_(record_error_Misc)(thr, |
| 2298 | "pthread_cond_{signal,broadcast}: associated lock is a rwlock"); |
| 2299 | } |
| 2300 | if (lk->heldBy == NULL) { |
| 2301 | HG_(record_error_Misc)(thr, |
| 2302 | "pthread_cond_{signal,broadcast}: dubious: " |
| 2303 | "associated lock is not held by any thread"); |
| 2304 | } |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 2305 | if (lk->heldBy != NULL && 0 == VG_(elemBag)(lk->heldBy, (UWord)thr)) { |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 2306 | HG_(record_error_Misc)(thr, |
| 2307 | "pthread_cond_{signal,broadcast}: " |
| 2308 | "associated lock is not held by calling thread"); |
| 2309 | } |
| 2310 | } else { |
| 2311 | /* Couldn't even find the damn thing. */ |
| 2312 | // But actually .. that's not necessarily an error. We don't |
| 2313 | // know the (CV,MX) binding until a pthread_cond_wait or bcast |
| 2314 | // shows us what it is, and if that may not have happened yet. |
| 2315 | // So just keep quiet in this circumstance. |
| 2316 | //HG_(record_error_Misc)( thr, |
| 2317 | // "pthread_cond_{signal,broadcast}: " |
| 2318 | // "no or invalid mutex associated with cond"); |
| 2319 | } |
| 2320 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2321 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2322 | libhb_so_send( thr->hbthr, cvi->so, True/*strong_send*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | /* returns True if it reckons 'mutex' is valid and held by this |
| 2326 | thread, else False */ |
| 2327 | static Bool evh__HG_PTHREAD_COND_WAIT_PRE ( ThreadId tid, |
| 2328 | void* cond, void* mutex ) |
| 2329 | { |
| 2330 | Thread* thr; |
| 2331 | Lock* lk; |
| 2332 | Bool lk_valid = True; |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2333 | CVInfo* cvi; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2334 | |
| 2335 | if (SHOW_EVENTS >= 1) |
| 2336 | VG_(printf)("evh__hg_PTHREAD_COND_WAIT_PRE" |
| 2337 | "(ctid=%d, cond=%p, mutex=%p)\n", |
| 2338 | (Int)tid, (void*)cond, (void*)mutex ); |
| 2339 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2340 | thr = map_threads_maybe_lookup( tid ); |
| 2341 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2342 | |
| 2343 | lk = map_locks_maybe_lookup( (Addr)mutex ); |
| 2344 | |
| 2345 | /* Check for stupid mutex arguments. There are various ways to be |
| 2346 | a bozo. Only complain once, though, even if more than one thing |
| 2347 | is wrong. */ |
| 2348 | if (lk == NULL) { |
| 2349 | lk_valid = False; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2350 | HG_(record_error_Misc)( |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2351 | thr, |
| 2352 | "pthread_cond_{timed}wait called with invalid mutex" ); |
| 2353 | } else { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2354 | tl_assert( HG_(is_sane_LockN)(lk) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2355 | if (lk->kind == LK_rdwr) { |
| 2356 | lk_valid = False; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2357 | HG_(record_error_Misc)( |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2358 | thr, "pthread_cond_{timed}wait called with mutex " |
| 2359 | "of type pthread_rwlock_t*" ); |
| 2360 | } else |
| 2361 | if (lk->heldBy == NULL) { |
| 2362 | lk_valid = False; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2363 | HG_(record_error_Misc)( |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2364 | thr, "pthread_cond_{timed}wait called with un-held mutex"); |
| 2365 | } else |
| 2366 | if (lk->heldBy != NULL |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 2367 | && VG_(elemBag)( lk->heldBy, (UWord)thr ) == 0) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2368 | lk_valid = False; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2369 | HG_(record_error_Misc)( |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2370 | thr, "pthread_cond_{timed}wait called with mutex " |
| 2371 | "held by a different thread" ); |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | // error-if: cond is also associated with a different mutex |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2376 | cvi = map_cond_to_CVInfo_lookup_or_alloc(cond); |
| 2377 | tl_assert(cvi); |
| 2378 | tl_assert(cvi->so); |
| 2379 | if (cvi->nWaiters == 0) { |
| 2380 | /* form initial (CV,MX) binding */ |
| 2381 | cvi->mx_ga = mutex; |
| 2382 | } |
| 2383 | else /* check existing (CV,MX) binding */ |
| 2384 | if (cvi->mx_ga != mutex) { |
| 2385 | HG_(record_error_Misc)( |
| 2386 | thr, "pthread_cond_{timed}wait: cond is associated " |
| 2387 | "with a different mutex"); |
| 2388 | } |
| 2389 | cvi->nWaiters++; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2390 | |
| 2391 | return lk_valid; |
| 2392 | } |
| 2393 | |
| 2394 | static void evh__HG_PTHREAD_COND_WAIT_POST ( ThreadId tid, |
sewardj | ff427c9 | 2013-10-14 12:13:52 +0000 | [diff] [blame] | 2395 | void* cond, void* mutex, |
| 2396 | Bool timeout) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2397 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2398 | /* A pthread_cond_wait(cond, mutex) completed successfully. Find |
| 2399 | the SO for this cond, and 'recv' from it so as to acquire a |
| 2400 | dependency edge back to the signaller/broadcaster. */ |
| 2401 | Thread* thr; |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2402 | CVInfo* cvi; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2403 | |
| 2404 | if (SHOW_EVENTS >= 1) |
| 2405 | VG_(printf)("evh__HG_PTHREAD_COND_WAIT_POST" |
sewardj | ff427c9 | 2013-10-14 12:13:52 +0000 | [diff] [blame] | 2406 | "(ctid=%d, cond=%p, mutex=%p)\n, timeout=%d", |
| 2407 | (Int)tid, (void*)cond, (void*)mutex, (Int)timeout ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2408 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2409 | thr = map_threads_maybe_lookup( tid ); |
| 2410 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2411 | |
| 2412 | // error-if: cond is also associated with a different mutex |
| 2413 | |
philippe | 8bfc215 | 2012-07-06 23:38:24 +0000 | [diff] [blame] | 2414 | cvi = map_cond_to_CVInfo_lookup_NO_alloc( cond ); |
| 2415 | if (!cvi) { |
| 2416 | /* This could be either a bug in helgrind or the guest application |
| 2417 | that did an error (e.g. cond var was destroyed by another thread. |
| 2418 | Let's assume helgrind is perfect ... |
| 2419 | Note that this is similar to drd behaviour. */ |
| 2420 | HG_(record_error_Misc)(thr, "condition variable has been destroyed while" |
| 2421 | " being waited upon"); |
| 2422 | return; |
| 2423 | } |
| 2424 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2425 | tl_assert(cvi); |
| 2426 | tl_assert(cvi->so); |
| 2427 | tl_assert(cvi->nWaiters > 0); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2428 | |
sewardj | ff427c9 | 2013-10-14 12:13:52 +0000 | [diff] [blame] | 2429 | if (!timeout && !libhb_so_everSent(cvi->so)) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2430 | /* Hmm. How can a wait on 'cond' succeed if nobody signalled |
| 2431 | it? If this happened it would surely be a bug in the threads |
| 2432 | library. Or one of those fabled "spurious wakeups". */ |
| 2433 | HG_(record_error_Misc)( thr, "Bug in libpthread: pthread_cond_wait " |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 2434 | "succeeded" |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2435 | " without prior pthread_cond_post"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2436 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2437 | |
| 2438 | /* anyway, acquire a dependency on it. */ |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2439 | libhb_so_recv( thr->hbthr, cvi->so, True/*strong_recv*/ ); |
| 2440 | |
| 2441 | cvi->nWaiters--; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
philippe | 19dfe03 | 2013-03-24 20:10:23 +0000 | [diff] [blame] | 2444 | static void evh__HG_PTHREAD_COND_INIT_POST ( ThreadId tid, |
| 2445 | void* cond, void* cond_attr ) |
| 2446 | { |
| 2447 | CVInfo* cvi; |
| 2448 | |
| 2449 | if (SHOW_EVENTS >= 1) |
| 2450 | VG_(printf)("evh__HG_PTHREAD_COND_INIT_POST" |
| 2451 | "(ctid=%d, cond=%p, cond_attr=%p)\n", |
| 2452 | (Int)tid, (void*)cond, (void*) cond_attr ); |
| 2453 | |
| 2454 | cvi = map_cond_to_CVInfo_lookup_or_alloc( cond ); |
| 2455 | tl_assert (cvi); |
| 2456 | tl_assert (cvi->so); |
| 2457 | } |
| 2458 | |
| 2459 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2460 | static void evh__HG_PTHREAD_COND_DESTROY_PRE ( ThreadId tid, |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 2461 | void* cond, Bool cond_is_init ) |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2462 | { |
| 2463 | /* Deal with destroy events. The only purpose is to free storage |
| 2464 | associated with the CV, so as to avoid any possible resource |
| 2465 | leaks. */ |
| 2466 | if (SHOW_EVENTS >= 1) |
| 2467 | VG_(printf)("evh__HG_PTHREAD_COND_DESTROY_PRE" |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 2468 | "(ctid=%d, cond=%p, cond_is_init=%d)\n", |
| 2469 | (Int)tid, (void*)cond, (Int)cond_is_init ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2470 | |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 2471 | map_cond_to_CVInfo_delete( tid, cond, cond_is_init ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2475 | /* ------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2476 | /* -------------- events to do with rwlocks -------------- */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2477 | /* ------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2478 | |
| 2479 | /* EXPOSITION only */ |
| 2480 | static |
| 2481 | void evh__HG_PTHREAD_RWLOCK_INIT_POST( ThreadId tid, void* rwl ) |
| 2482 | { |
| 2483 | if (SHOW_EVENTS >= 1) |
| 2484 | VG_(printf)("evh__hg_PTHREAD_RWLOCK_INIT_POST(ctid=%d, %p)\n", |
| 2485 | (Int)tid, (void*)rwl ); |
| 2486 | map_locks_lookup_or_create( LK_rdwr, (Addr)rwl, tid ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2487 | if (HG_(clo_sanity_flags) & SCE_LOCKS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2488 | all__sanity_check("evh__hg_PTHREAD_RWLOCK_INIT_POST"); |
| 2489 | } |
| 2490 | |
| 2491 | static |
| 2492 | void evh__HG_PTHREAD_RWLOCK_DESTROY_PRE( ThreadId tid, void* rwl ) |
| 2493 | { |
| 2494 | Thread* thr; |
| 2495 | Lock* lk; |
| 2496 | if (SHOW_EVENTS >= 1) |
| 2497 | VG_(printf)("evh__hg_PTHREAD_RWLOCK_DESTROY_PRE(ctid=%d, %p)\n", |
| 2498 | (Int)tid, (void*)rwl ); |
| 2499 | |
| 2500 | thr = map_threads_maybe_lookup( tid ); |
| 2501 | /* cannot fail - Thread* must already exist */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2502 | tl_assert( HG_(is_sane_Thread)(thr) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2503 | |
| 2504 | lk = map_locks_maybe_lookup( (Addr)rwl ); |
| 2505 | |
| 2506 | if (lk == NULL || lk->kind != LK_rdwr) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2507 | HG_(record_error_Misc)( |
| 2508 | thr, "pthread_rwlock_destroy with invalid argument" ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
| 2511 | if (lk) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2512 | tl_assert( HG_(is_sane_LockN)(lk) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2513 | tl_assert( lk->guestaddr == (Addr)rwl ); |
| 2514 | if (lk->heldBy) { |
| 2515 | /* Basically act like we unlocked the lock */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2516 | HG_(record_error_Misc)( |
| 2517 | thr, "pthread_rwlock_destroy of a locked mutex" ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2518 | /* remove lock from locksets of all owning threads */ |
| 2519 | remove_Lock_from_locksets_of_all_owning_Threads( lk ); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 2520 | VG_(deleteBag)( lk->heldBy ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2521 | lk->heldBy = NULL; |
| 2522 | lk->heldW = False; |
sewardj | 1c7e833 | 2007-11-29 13:04:03 +0000 | [diff] [blame] | 2523 | lk->acquired_at = NULL; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2524 | } |
| 2525 | tl_assert( !lk->heldBy ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2526 | tl_assert( HG_(is_sane_LockN)(lk) ); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 2527 | |
| 2528 | if (HG_(clo_track_lockorders)) |
| 2529 | laog__handle_one_lock_deletion(lk); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2530 | map_locks_delete( lk->guestaddr ); |
| 2531 | del_LockN( lk ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2534 | if (HG_(clo_sanity_flags) & SCE_LOCKS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2535 | all__sanity_check("evh__hg_PTHREAD_RWLOCK_DESTROY_PRE"); |
| 2536 | } |
| 2537 | |
| 2538 | static |
sewardj | 789c3c5 | 2008-02-25 12:10:07 +0000 | [diff] [blame] | 2539 | void evh__HG_PTHREAD_RWLOCK_LOCK_PRE ( ThreadId tid, |
| 2540 | void* rwl, |
| 2541 | Word isW, Word isTryLock ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2542 | { |
| 2543 | /* Just check the rwl is sane; nothing else to do. */ |
| 2544 | // 'rwl' may be invalid - not checked by wrapper |
| 2545 | Thread* thr; |
| 2546 | Lock* lk; |
| 2547 | if (SHOW_EVENTS >= 1) |
| 2548 | VG_(printf)("evh__hg_PTHREAD_RWLOCK_LOCK_PRE(ctid=%d, isW=%d, %p)\n", |
| 2549 | (Int)tid, (Int)isW, (void*)rwl ); |
| 2550 | |
| 2551 | tl_assert(isW == 0 || isW == 1); /* assured us by wrapper */ |
sewardj | 789c3c5 | 2008-02-25 12:10:07 +0000 | [diff] [blame] | 2552 | tl_assert(isTryLock == 0 || isTryLock == 1); /* assured us by wrapper */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2553 | thr = map_threads_maybe_lookup( tid ); |
| 2554 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2555 | |
| 2556 | lk = map_locks_maybe_lookup( (Addr)rwl ); |
| 2557 | if ( lk |
| 2558 | && (lk->kind == LK_nonRec || lk->kind == LK_mbRec) ) { |
| 2559 | /* Wrong kind of lock. Duh. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2560 | HG_(record_error_Misc)( |
| 2561 | thr, "pthread_rwlock_{rd,rw}lock with a " |
| 2562 | "pthread_mutex_t* argument " ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | static |
| 2567 | void evh__HG_PTHREAD_RWLOCK_LOCK_POST ( ThreadId tid, void* rwl, Word isW ) |
| 2568 | { |
| 2569 | // only called if the real library call succeeded - so mutex is sane |
| 2570 | Thread* thr; |
| 2571 | if (SHOW_EVENTS >= 1) |
| 2572 | VG_(printf)("evh__hg_PTHREAD_RWLOCK_LOCK_POST(ctid=%d, isW=%d, %p)\n", |
| 2573 | (Int)tid, (Int)isW, (void*)rwl ); |
| 2574 | |
| 2575 | tl_assert(isW == 0 || isW == 1); /* assured us by wrapper */ |
| 2576 | thr = map_threads_maybe_lookup( tid ); |
| 2577 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2578 | |
| 2579 | (isW ? evhH__post_thread_w_acquires_lock |
| 2580 | : evhH__post_thread_r_acquires_lock)( |
| 2581 | thr, |
| 2582 | LK_rdwr, /* if not known, create new lock with this LockKind */ |
| 2583 | (Addr)rwl |
| 2584 | ); |
| 2585 | } |
| 2586 | |
| 2587 | static void evh__HG_PTHREAD_RWLOCK_UNLOCK_PRE ( ThreadId tid, void* rwl ) |
| 2588 | { |
| 2589 | // 'rwl' may be invalid - not checked by wrapper |
| 2590 | Thread* thr; |
| 2591 | if (SHOW_EVENTS >= 1) |
| 2592 | VG_(printf)("evh__HG_PTHREAD_RWLOCK_UNLOCK_PRE(ctid=%d, rwl=%p)\n", |
| 2593 | (Int)tid, (void*)rwl ); |
| 2594 | |
| 2595 | thr = map_threads_maybe_lookup( tid ); |
| 2596 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2597 | |
| 2598 | evhH__pre_thread_releases_lock( thr, (Addr)rwl, True/*isRDWR*/ ); |
| 2599 | } |
| 2600 | |
| 2601 | static void evh__HG_PTHREAD_RWLOCK_UNLOCK_POST ( ThreadId tid, void* rwl ) |
| 2602 | { |
| 2603 | // only called if the real library call succeeded - so mutex is sane |
| 2604 | Thread* thr; |
| 2605 | if (SHOW_EVENTS >= 1) |
| 2606 | VG_(printf)("evh__hg_PTHREAD_RWLOCK_UNLOCK_POST(ctid=%d, rwl=%p)\n", |
| 2607 | (Int)tid, (void*)rwl ); |
| 2608 | thr = map_threads_maybe_lookup( tid ); |
| 2609 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2610 | |
| 2611 | // anything we should do here? |
| 2612 | } |
| 2613 | |
| 2614 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2615 | /* ---------------------------------------------------------- */ |
| 2616 | /* -------------- events to do with semaphores -------------- */ |
| 2617 | /* ---------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2618 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2619 | /* This is similar to but not identical to the handling for condition |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2620 | variables. */ |
| 2621 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2622 | /* For each semaphore, we maintain a stack of SOs. When a 'post' |
| 2623 | operation is done on a semaphore (unlocking, essentially), a new SO |
| 2624 | is created for the posting thread, the posting thread does a strong |
| 2625 | send to it (which merely installs the posting thread's VC in the |
| 2626 | SO), and the SO is pushed on the semaphore's stack. |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2627 | |
| 2628 | Later, when a (probably different) thread completes 'wait' on the |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2629 | semaphore, we pop a SO off the semaphore's stack (which should be |
| 2630 | nonempty), and do a strong recv from it. This mechanism creates |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2631 | dependencies between posters and waiters of the semaphore. |
| 2632 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2633 | It may not be necessary to use a stack - perhaps a bag of SOs would |
| 2634 | do. But we do need to keep track of how many unused-up posts have |
| 2635 | happened for the semaphore. |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2636 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2637 | Imagine T1 and T2 both post once on a semaphore S, and T3 waits |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2638 | twice on S. T3 cannot complete its waits without both T1 and T2 |
| 2639 | posting. The above mechanism will ensure that T3 acquires |
| 2640 | dependencies on both T1 and T2. |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2641 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2642 | When a semaphore is initialised with value N, we do as if we'd |
| 2643 | posted N times on the semaphore: basically create N SOs and do a |
| 2644 | strong send to all of then. This allows up to N waits on the |
| 2645 | semaphore to acquire a dependency on the initialisation point, |
| 2646 | which AFAICS is the correct behaviour. |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2647 | |
| 2648 | We don't emit an error for DESTROY_PRE on a semaphore we don't know |
| 2649 | about. We should. |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2650 | */ |
| 2651 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2652 | /* sem_t* -> XArray* SO* */ |
| 2653 | static WordFM* map_sem_to_SO_stack = NULL; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2654 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2655 | static void map_sem_to_SO_stack_INIT ( void ) { |
| 2656 | if (map_sem_to_SO_stack == NULL) { |
| 2657 | map_sem_to_SO_stack = VG_(newFM)( HG_(zalloc), "hg.mstSs.1", |
| 2658 | HG_(free), NULL ); |
| 2659 | tl_assert(map_sem_to_SO_stack != NULL); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2660 | } |
| 2661 | } |
| 2662 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2663 | static void push_SO_for_sem ( void* sem, SO* so ) { |
| 2664 | UWord keyW; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2665 | XArray* xa; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2666 | tl_assert(so); |
| 2667 | map_sem_to_SO_stack_INIT(); |
| 2668 | if (VG_(lookupFM)( map_sem_to_SO_stack, |
| 2669 | &keyW, (UWord*)&xa, (UWord)sem )) { |
| 2670 | tl_assert(keyW == (UWord)sem); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2671 | tl_assert(xa); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2672 | VG_(addToXA)( xa, &so ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2673 | } else { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2674 | xa = VG_(newXA)( HG_(zalloc), "hg.pSfs.1", HG_(free), sizeof(SO*) ); |
| 2675 | VG_(addToXA)( xa, &so ); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 2676 | VG_(addToFM)( map_sem_to_SO_stack, (UWord)sem, (UWord)xa ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2677 | } |
| 2678 | } |
| 2679 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2680 | static SO* mb_pop_SO_for_sem ( void* sem ) { |
| 2681 | UWord keyW; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2682 | XArray* xa; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2683 | SO* so; |
| 2684 | map_sem_to_SO_stack_INIT(); |
| 2685 | if (VG_(lookupFM)( map_sem_to_SO_stack, |
| 2686 | &keyW, (UWord*)&xa, (UWord)sem )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2687 | /* xa is the stack for this semaphore. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2688 | Word sz; |
| 2689 | tl_assert(keyW == (UWord)sem); |
| 2690 | sz = VG_(sizeXA)( xa ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2691 | tl_assert(sz >= 0); |
| 2692 | if (sz == 0) |
| 2693 | return NULL; /* odd, the stack is empty */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2694 | so = *(SO**)VG_(indexXA)( xa, sz-1 ); |
| 2695 | tl_assert(so); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2696 | VG_(dropTailXA)( xa, 1 ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2697 | return so; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2698 | } else { |
| 2699 | /* hmm, that's odd. No stack for this semaphore. */ |
| 2700 | return NULL; |
| 2701 | } |
| 2702 | } |
| 2703 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2704 | static void evh__HG_POSIX_SEM_DESTROY_PRE ( ThreadId tid, void* sem ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2705 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2706 | UWord keyW, valW; |
| 2707 | SO* so; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2708 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2709 | if (SHOW_EVENTS >= 1) |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2710 | VG_(printf)("evh__HG_POSIX_SEM_DESTROY_PRE(ctid=%d, sem=%p)\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2711 | (Int)tid, (void*)sem ); |
| 2712 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2713 | map_sem_to_SO_stack_INIT(); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2714 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2715 | /* Empty out the semaphore's SO stack. This way of doing it is |
| 2716 | stupid, but at least it's easy. */ |
| 2717 | while (1) { |
| 2718 | so = mb_pop_SO_for_sem( sem ); |
| 2719 | if (!so) break; |
| 2720 | libhb_so_dealloc(so); |
| 2721 | } |
| 2722 | |
| 2723 | if (VG_(delFromFM)( map_sem_to_SO_stack, &keyW, &valW, (UWord)sem )) { |
| 2724 | XArray* xa = (XArray*)valW; |
| 2725 | tl_assert(keyW == (UWord)sem); |
| 2726 | tl_assert(xa); |
| 2727 | tl_assert(VG_(sizeXA)(xa) == 0); /* preceding loop just emptied it */ |
| 2728 | VG_(deleteXA)(xa); |
| 2729 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2732 | static |
| 2733 | void evh__HG_POSIX_SEM_INIT_POST ( ThreadId tid, void* sem, UWord value ) |
| 2734 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2735 | SO* so; |
| 2736 | Thread* thr; |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2737 | |
| 2738 | if (SHOW_EVENTS >= 1) |
| 2739 | VG_(printf)("evh__HG_POSIX_SEM_INIT_POST(ctid=%d, sem=%p, value=%lu)\n", |
| 2740 | (Int)tid, (void*)sem, value ); |
| 2741 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2742 | thr = map_threads_maybe_lookup( tid ); |
| 2743 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2744 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2745 | /* Empty out the semaphore's SO stack. This way of doing it is |
| 2746 | stupid, but at least it's easy. */ |
| 2747 | while (1) { |
| 2748 | so = mb_pop_SO_for_sem( sem ); |
| 2749 | if (!so) break; |
| 2750 | libhb_so_dealloc(so); |
| 2751 | } |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2752 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2753 | /* If we don't do this check, the following while loop runs us out |
| 2754 | of memory for stupid initial values of 'value'. */ |
| 2755 | if (value > 10000) { |
| 2756 | HG_(record_error_Misc)( |
| 2757 | thr, "sem_init: initial value exceeds 10000; using 10000" ); |
| 2758 | value = 10000; |
| 2759 | } |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2760 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2761 | /* Now create 'valid' new SOs for the thread, do a strong send to |
| 2762 | each of them, and push them all on the stack. */ |
| 2763 | for (; value > 0; value--) { |
| 2764 | Thr* hbthr = thr->hbthr; |
| 2765 | tl_assert(hbthr); |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2766 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2767 | so = libhb_so_alloc(); |
| 2768 | libhb_so_send( hbthr, so, True/*strong send*/ ); |
| 2769 | push_SO_for_sem( sem, so ); |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | static void evh__HG_POSIX_SEM_POST_PRE ( ThreadId tid, void* sem ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2774 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2775 | /* 'tid' has posted on 'sem'. Create a new SO, do a strong send to |
| 2776 | it (iow, write our VC into it, then tick ours), and push the SO |
| 2777 | on on a stack of SOs associated with 'sem'. This is later used |
| 2778 | by other thread(s) which successfully exit from a sem_wait on |
| 2779 | the same sem; by doing a strong recv from SOs popped of the |
| 2780 | stack, they acquire dependencies on the posting thread |
| 2781 | segment(s). */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2782 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2783 | Thread* thr; |
| 2784 | SO* so; |
| 2785 | Thr* hbthr; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2786 | |
| 2787 | if (SHOW_EVENTS >= 1) |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2788 | VG_(printf)("evh__HG_POSIX_SEM_POST_PRE(ctid=%d, sem=%p)\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2789 | (Int)tid, (void*)sem ); |
| 2790 | |
| 2791 | thr = map_threads_maybe_lookup( tid ); |
| 2792 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2793 | |
| 2794 | // error-if: sem is bogus |
| 2795 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2796 | hbthr = thr->hbthr; |
| 2797 | tl_assert(hbthr); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2798 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2799 | so = libhb_so_alloc(); |
| 2800 | libhb_so_send( hbthr, so, True/*strong send*/ ); |
| 2801 | push_SO_for_sem( sem, so ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2804 | static void evh__HG_POSIX_SEM_WAIT_POST ( ThreadId tid, void* sem ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2805 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2806 | /* A sem_wait(sem) completed successfully. Pop the posting-SO for |
| 2807 | the 'sem' from this semaphore's SO-stack, and do a strong recv |
| 2808 | from it. This creates a dependency back to one of the post-ers |
| 2809 | for the semaphore. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2810 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2811 | Thread* thr; |
| 2812 | SO* so; |
| 2813 | Thr* hbthr; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2814 | |
| 2815 | if (SHOW_EVENTS >= 1) |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2816 | VG_(printf)("evh__HG_POSIX_SEM_WAIT_POST(ctid=%d, sem=%p)\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2817 | (Int)tid, (void*)sem ); |
| 2818 | |
| 2819 | thr = map_threads_maybe_lookup( tid ); |
| 2820 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2821 | |
| 2822 | // error-if: sem is bogus |
| 2823 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2824 | so = mb_pop_SO_for_sem( sem ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2825 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2826 | if (so) { |
| 2827 | hbthr = thr->hbthr; |
| 2828 | tl_assert(hbthr); |
| 2829 | |
| 2830 | libhb_so_recv( hbthr, so, True/*strong recv*/ ); |
| 2831 | libhb_so_dealloc(so); |
| 2832 | } else { |
| 2833 | /* Hmm. How can a wait on 'sem' succeed if nobody posted to it? |
| 2834 | If this happened it would surely be a bug in the threads |
| 2835 | library. */ |
| 2836 | HG_(record_error_Misc)( |
| 2837 | thr, "Bug in libpthread: sem_wait succeeded on" |
| 2838 | " semaphore without prior sem_post"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2843 | /* -------------------------------------------------------- */ |
| 2844 | /* -------------- events to do with barriers -------------- */ |
| 2845 | /* -------------------------------------------------------- */ |
| 2846 | |
| 2847 | typedef |
| 2848 | struct { |
| 2849 | Bool initted; /* has it yet been initted by guest? */ |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2850 | Bool resizable; /* is resizing allowed? */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2851 | UWord size; /* declared size */ |
| 2852 | XArray* waiting; /* XA of Thread*. # present is 0 .. .size */ |
| 2853 | } |
| 2854 | Bar; |
| 2855 | |
| 2856 | static Bar* new_Bar ( void ) { |
| 2857 | Bar* bar = HG_(zalloc)( "hg.nB.1 (new_Bar)", sizeof(Bar) ); |
| 2858 | tl_assert(bar); |
| 2859 | /* all fields are zero */ |
| 2860 | tl_assert(bar->initted == False); |
| 2861 | return bar; |
| 2862 | } |
| 2863 | |
| 2864 | static void delete_Bar ( Bar* bar ) { |
| 2865 | tl_assert(bar); |
| 2866 | if (bar->waiting) |
| 2867 | VG_(deleteXA)(bar->waiting); |
| 2868 | HG_(free)(bar); |
| 2869 | } |
| 2870 | |
| 2871 | /* A mapping which stores auxiliary data for barriers. */ |
| 2872 | |
| 2873 | /* pthread_barrier_t* -> Bar* */ |
| 2874 | static WordFM* map_barrier_to_Bar = NULL; |
| 2875 | |
| 2876 | static void map_barrier_to_Bar_INIT ( void ) { |
| 2877 | if (UNLIKELY(map_barrier_to_Bar == NULL)) { |
| 2878 | map_barrier_to_Bar = VG_(newFM)( HG_(zalloc), |
| 2879 | "hg.mbtBI.1", HG_(free), NULL ); |
| 2880 | tl_assert(map_barrier_to_Bar != NULL); |
| 2881 | } |
| 2882 | } |
| 2883 | |
| 2884 | static Bar* map_barrier_to_Bar_lookup_or_alloc ( void* barrier ) { |
| 2885 | UWord key, val; |
| 2886 | map_barrier_to_Bar_INIT(); |
| 2887 | if (VG_(lookupFM)( map_barrier_to_Bar, &key, &val, (UWord)barrier )) { |
| 2888 | tl_assert(key == (UWord)barrier); |
| 2889 | return (Bar*)val; |
| 2890 | } else { |
| 2891 | Bar* bar = new_Bar(); |
| 2892 | VG_(addToFM)( map_barrier_to_Bar, (UWord)barrier, (UWord)bar ); |
| 2893 | return bar; |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | static void map_barrier_to_Bar_delete ( void* barrier ) { |
| 2898 | UWord keyW, valW; |
| 2899 | map_barrier_to_Bar_INIT(); |
| 2900 | if (VG_(delFromFM)( map_barrier_to_Bar, &keyW, &valW, (UWord)barrier )) { |
| 2901 | Bar* bar = (Bar*)valW; |
| 2902 | tl_assert(keyW == (UWord)barrier); |
| 2903 | delete_Bar(bar); |
| 2904 | } |
| 2905 | } |
| 2906 | |
| 2907 | |
| 2908 | static void evh__HG_PTHREAD_BARRIER_INIT_PRE ( ThreadId tid, |
| 2909 | void* barrier, |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2910 | UWord count, |
| 2911 | UWord resizable ) |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2912 | { |
| 2913 | Thread* thr; |
| 2914 | Bar* bar; |
| 2915 | |
| 2916 | if (SHOW_EVENTS >= 1) |
| 2917 | VG_(printf)("evh__HG_PTHREAD_BARRIER_INIT_PRE" |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2918 | "(tid=%d, barrier=%p, count=%lu, resizable=%lu)\n", |
| 2919 | (Int)tid, (void*)barrier, count, resizable ); |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2920 | |
| 2921 | thr = map_threads_maybe_lookup( tid ); |
| 2922 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2923 | |
| 2924 | if (count == 0) { |
| 2925 | HG_(record_error_Misc)( |
| 2926 | thr, "pthread_barrier_init: 'count' argument is zero" |
| 2927 | ); |
| 2928 | } |
| 2929 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2930 | if (resizable != 0 && resizable != 1) { |
| 2931 | HG_(record_error_Misc)( |
| 2932 | thr, "pthread_barrier_init: invalid 'resizable' argument" |
| 2933 | ); |
| 2934 | } |
| 2935 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2936 | bar = map_barrier_to_Bar_lookup_or_alloc(barrier); |
| 2937 | tl_assert(bar); |
| 2938 | |
| 2939 | if (bar->initted) { |
| 2940 | HG_(record_error_Misc)( |
| 2941 | thr, "pthread_barrier_init: barrier is already initialised" |
| 2942 | ); |
| 2943 | } |
| 2944 | |
| 2945 | if (bar->waiting && VG_(sizeXA)(bar->waiting) > 0) { |
| 2946 | tl_assert(bar->initted); |
| 2947 | HG_(record_error_Misc)( |
sewardj | 553655c | 2008-11-14 19:41:19 +0000 | [diff] [blame] | 2948 | thr, "pthread_barrier_init: threads are waiting at barrier" |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2949 | ); |
| 2950 | VG_(dropTailXA)(bar->waiting, VG_(sizeXA)(bar->waiting)); |
| 2951 | } |
| 2952 | if (!bar->waiting) { |
| 2953 | bar->waiting = VG_(newXA)( HG_(zalloc), "hg.eHPBIP.1", HG_(free), |
| 2954 | sizeof(Thread*) ); |
| 2955 | } |
| 2956 | |
| 2957 | tl_assert(bar->waiting); |
| 2958 | tl_assert(VG_(sizeXA)(bar->waiting) == 0); |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2959 | bar->initted = True; |
| 2960 | bar->resizable = resizable == 1 ? True : False; |
| 2961 | bar->size = count; |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | |
| 2965 | static void evh__HG_PTHREAD_BARRIER_DESTROY_PRE ( ThreadId tid, |
| 2966 | void* barrier ) |
| 2967 | { |
sewardj | 553655c | 2008-11-14 19:41:19 +0000 | [diff] [blame] | 2968 | Thread* thr; |
| 2969 | Bar* bar; |
| 2970 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2971 | /* Deal with destroy events. The only purpose is to free storage |
| 2972 | associated with the barrier, so as to avoid any possible |
| 2973 | resource leaks. */ |
| 2974 | if (SHOW_EVENTS >= 1) |
| 2975 | VG_(printf)("evh__HG_PTHREAD_BARRIER_DESTROY_PRE" |
| 2976 | "(tid=%d, barrier=%p)\n", |
| 2977 | (Int)tid, (void*)barrier ); |
| 2978 | |
sewardj | 553655c | 2008-11-14 19:41:19 +0000 | [diff] [blame] | 2979 | thr = map_threads_maybe_lookup( tid ); |
| 2980 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2981 | |
| 2982 | bar = map_barrier_to_Bar_lookup_or_alloc(barrier); |
| 2983 | tl_assert(bar); |
| 2984 | |
| 2985 | if (!bar->initted) { |
| 2986 | HG_(record_error_Misc)( |
| 2987 | thr, "pthread_barrier_destroy: barrier was never initialised" |
| 2988 | ); |
| 2989 | } |
| 2990 | |
| 2991 | if (bar->initted && bar->waiting && VG_(sizeXA)(bar->waiting) > 0) { |
| 2992 | HG_(record_error_Misc)( |
| 2993 | thr, "pthread_barrier_destroy: threads are waiting at barrier" |
| 2994 | ); |
| 2995 | } |
| 2996 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2997 | /* Maybe we shouldn't do this; just let it persist, so that when it |
| 2998 | is reinitialised we don't need to do any dynamic memory |
| 2999 | allocation? The downside is a potentially unlimited space leak, |
| 3000 | if the client creates (in turn) a large number of barriers all |
| 3001 | at different locations. Note that if we do later move to the |
| 3002 | don't-delete-it scheme, we need to mark the barrier as |
| 3003 | uninitialised again since otherwise a later _init call will |
sewardj | 553655c | 2008-11-14 19:41:19 +0000 | [diff] [blame] | 3004 | elicit a duplicate-init error. */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3005 | map_barrier_to_Bar_delete( barrier ); |
| 3006 | } |
| 3007 | |
| 3008 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 3009 | /* All the threads have arrived. Now do the Interesting Bit. Get a |
| 3010 | new synchronisation object and do a weak send to it from all the |
| 3011 | participating threads. This makes its vector clocks be the join of |
| 3012 | all the individual threads' vector clocks. Then do a strong |
| 3013 | receive from it back to all threads, so that their VCs are a copy |
| 3014 | of it (hence are all equal to the join of their original VCs.) */ |
| 3015 | static void do_barrier_cross_sync_and_empty ( Bar* bar ) |
| 3016 | { |
| 3017 | /* XXX check bar->waiting has no duplicates */ |
| 3018 | UWord i; |
| 3019 | SO* so = libhb_so_alloc(); |
| 3020 | |
| 3021 | tl_assert(bar->waiting); |
| 3022 | tl_assert(VG_(sizeXA)(bar->waiting) == bar->size); |
| 3023 | |
| 3024 | /* compute the join ... */ |
| 3025 | for (i = 0; i < bar->size; i++) { |
| 3026 | Thread* t = *(Thread**)VG_(indexXA)(bar->waiting, i); |
| 3027 | Thr* hbthr = t->hbthr; |
| 3028 | libhb_so_send( hbthr, so, False/*weak send*/ ); |
| 3029 | } |
| 3030 | /* ... and distribute to all threads */ |
| 3031 | for (i = 0; i < bar->size; i++) { |
| 3032 | Thread* t = *(Thread**)VG_(indexXA)(bar->waiting, i); |
| 3033 | Thr* hbthr = t->hbthr; |
| 3034 | libhb_so_recv( hbthr, so, True/*strong recv*/ ); |
| 3035 | } |
| 3036 | |
| 3037 | /* finally, we must empty out the waiting vector */ |
| 3038 | VG_(dropTailXA)(bar->waiting, VG_(sizeXA)(bar->waiting)); |
| 3039 | |
| 3040 | /* and we don't need this any more. Perhaps a stack-allocated |
| 3041 | SO would be better? */ |
| 3042 | libhb_so_dealloc(so); |
| 3043 | } |
| 3044 | |
| 3045 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3046 | static void evh__HG_PTHREAD_BARRIER_WAIT_PRE ( ThreadId tid, |
| 3047 | void* barrier ) |
| 3048 | { |
sewardj | 1c466b7 | 2008-11-19 11:52:14 +0000 | [diff] [blame] | 3049 | /* This function gets called after a client thread calls |
| 3050 | pthread_barrier_wait but before it arrives at the real |
| 3051 | pthread_barrier_wait. |
| 3052 | |
| 3053 | Why is the following correct? It's a bit subtle. |
| 3054 | |
| 3055 | If this is not the last thread arriving at the barrier, we simply |
| 3056 | note its presence and return. Because valgrind (at least as of |
| 3057 | Nov 08) is single threaded, we are guaranteed safe from any race |
| 3058 | conditions when in this function -- no other client threads are |
| 3059 | running. |
| 3060 | |
| 3061 | If this is the last thread, then we are again the only running |
| 3062 | thread. All the other threads will have either arrived at the |
| 3063 | real pthread_barrier_wait or are on their way to it, but in any |
| 3064 | case are guaranteed not to be able to move past it, because this |
| 3065 | thread is currently in this function and so has not yet arrived |
| 3066 | at the real pthread_barrier_wait. That means that: |
| 3067 | |
| 3068 | 1. While we are in this function, none of the other threads |
| 3069 | waiting at the barrier can move past it. |
| 3070 | |
| 3071 | 2. When this function returns (and simulated execution resumes), |
| 3072 | this thread and all other waiting threads will be able to move |
| 3073 | past the real barrier. |
| 3074 | |
| 3075 | Because of this, it is now safe to update the vector clocks of |
| 3076 | all threads, to represent the fact that they all arrived at the |
| 3077 | barrier and have all moved on. There is no danger of any |
| 3078 | complications to do with some threads leaving the barrier and |
| 3079 | racing back round to the front, whilst others are still leaving |
| 3080 | (which is the primary source of complication in correct handling/ |
| 3081 | implementation of barriers). That can't happen because we update |
| 3082 | here our data structures so as to indicate that the threads have |
| 3083 | passed the barrier, even though, as per (2) above, they are |
| 3084 | guaranteed not to pass the barrier until we return. |
| 3085 | |
| 3086 | This relies crucially on Valgrind being single threaded. If that |
| 3087 | changes, this will need to be reconsidered. |
| 3088 | */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3089 | Thread* thr; |
| 3090 | Bar* bar; |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 3091 | UWord present; |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3092 | |
| 3093 | if (SHOW_EVENTS >= 1) |
| 3094 | VG_(printf)("evh__HG_PTHREAD_BARRIER_WAIT_PRE" |
| 3095 | "(tid=%d, barrier=%p)\n", |
| 3096 | (Int)tid, (void*)barrier ); |
| 3097 | |
| 3098 | thr = map_threads_maybe_lookup( tid ); |
| 3099 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 3100 | |
| 3101 | bar = map_barrier_to_Bar_lookup_or_alloc(barrier); |
| 3102 | tl_assert(bar); |
| 3103 | |
| 3104 | if (!bar->initted) { |
| 3105 | HG_(record_error_Misc)( |
| 3106 | thr, "pthread_barrier_wait: barrier is uninitialised" |
| 3107 | ); |
| 3108 | return; /* client is broken .. avoid assertions below */ |
| 3109 | } |
| 3110 | |
| 3111 | /* guaranteed by _INIT_PRE above */ |
| 3112 | tl_assert(bar->size > 0); |
| 3113 | tl_assert(bar->waiting); |
| 3114 | |
| 3115 | VG_(addToXA)( bar->waiting, &thr ); |
| 3116 | |
| 3117 | /* guaranteed by this function */ |
| 3118 | present = VG_(sizeXA)(bar->waiting); |
| 3119 | tl_assert(present > 0 && present <= bar->size); |
| 3120 | |
| 3121 | if (present < bar->size) |
| 3122 | return; |
| 3123 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 3124 | do_barrier_cross_sync_and_empty(bar); |
| 3125 | } |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3126 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3127 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 3128 | static void evh__HG_PTHREAD_BARRIER_RESIZE_PRE ( ThreadId tid, |
| 3129 | void* barrier, |
| 3130 | UWord newcount ) |
| 3131 | { |
| 3132 | Thread* thr; |
| 3133 | Bar* bar; |
| 3134 | UWord present; |
| 3135 | |
| 3136 | if (SHOW_EVENTS >= 1) |
| 3137 | VG_(printf)("evh__HG_PTHREAD_BARRIER_RESIZE_PRE" |
| 3138 | "(tid=%d, barrier=%p, newcount=%lu)\n", |
| 3139 | (Int)tid, (void*)barrier, newcount ); |
| 3140 | |
| 3141 | thr = map_threads_maybe_lookup( tid ); |
| 3142 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 3143 | |
| 3144 | bar = map_barrier_to_Bar_lookup_or_alloc(barrier); |
| 3145 | tl_assert(bar); |
| 3146 | |
| 3147 | if (!bar->initted) { |
| 3148 | HG_(record_error_Misc)( |
| 3149 | thr, "pthread_barrier_resize: barrier is uninitialised" |
| 3150 | ); |
| 3151 | return; /* client is broken .. avoid assertions below */ |
| 3152 | } |
| 3153 | |
| 3154 | if (!bar->resizable) { |
| 3155 | HG_(record_error_Misc)( |
| 3156 | thr, "pthread_barrier_resize: barrier is may not be resized" |
| 3157 | ); |
| 3158 | return; /* client is broken .. avoid assertions below */ |
| 3159 | } |
| 3160 | |
| 3161 | if (newcount == 0) { |
| 3162 | HG_(record_error_Misc)( |
| 3163 | thr, "pthread_barrier_resize: 'newcount' argument is zero" |
| 3164 | ); |
| 3165 | return; /* client is broken .. avoid assertions below */ |
| 3166 | } |
| 3167 | |
| 3168 | /* guaranteed by _INIT_PRE above */ |
| 3169 | tl_assert(bar->size > 0); |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3170 | tl_assert(bar->waiting); |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 3171 | /* Guaranteed by this fn */ |
| 3172 | tl_assert(newcount > 0); |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3173 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 3174 | if (newcount >= bar->size) { |
| 3175 | /* Increasing the capacity. There's no possibility of threads |
| 3176 | moving on from the barrier in this situation, so just note |
| 3177 | the fact and do nothing more. */ |
| 3178 | bar->size = newcount; |
| 3179 | } else { |
| 3180 | /* Decreasing the capacity. If we decrease it to be equal or |
| 3181 | below the number of waiting threads, they will now move past |
| 3182 | the barrier, so need to mess with dep edges in the same way |
| 3183 | as if the barrier had filled up normally. */ |
| 3184 | present = VG_(sizeXA)(bar->waiting); |
| 3185 | tl_assert(present >= 0 && present <= bar->size); |
| 3186 | if (newcount <= present) { |
| 3187 | bar->size = present; /* keep the cross_sync call happy */ |
| 3188 | do_barrier_cross_sync_and_empty(bar); |
| 3189 | } |
| 3190 | bar->size = newcount; |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3191 | } |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3195 | /* ----------------------------------------------------- */ |
| 3196 | /* ----- events to do with user-specified HB edges ----- */ |
| 3197 | /* ----------------------------------------------------- */ |
| 3198 | |
| 3199 | /* A mapping from arbitrary UWord tag to the SO associated with it. |
| 3200 | The UWord tags are meaningless to us, interpreted only by the |
| 3201 | user. */ |
| 3202 | |
| 3203 | |
| 3204 | |
| 3205 | /* UWord -> SO* */ |
| 3206 | static WordFM* map_usertag_to_SO = NULL; |
| 3207 | |
| 3208 | static void map_usertag_to_SO_INIT ( void ) { |
| 3209 | if (UNLIKELY(map_usertag_to_SO == NULL)) { |
| 3210 | map_usertag_to_SO = VG_(newFM)( HG_(zalloc), |
| 3211 | "hg.mutS.1", HG_(free), NULL ); |
| 3212 | tl_assert(map_usertag_to_SO != NULL); |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | static SO* map_usertag_to_SO_lookup_or_alloc ( UWord usertag ) { |
| 3217 | UWord key, val; |
| 3218 | map_usertag_to_SO_INIT(); |
| 3219 | if (VG_(lookupFM)( map_usertag_to_SO, &key, &val, usertag )) { |
| 3220 | tl_assert(key == (UWord)usertag); |
| 3221 | return (SO*)val; |
| 3222 | } else { |
| 3223 | SO* so = libhb_so_alloc(); |
| 3224 | VG_(addToFM)( map_usertag_to_SO, usertag, (UWord)so ); |
| 3225 | return so; |
| 3226 | } |
| 3227 | } |
| 3228 | |
sewardj | 6015d0e | 2011-03-11 19:10:48 +0000 | [diff] [blame] | 3229 | static void map_usertag_to_SO_delete ( UWord usertag ) { |
| 3230 | UWord keyW, valW; |
| 3231 | map_usertag_to_SO_INIT(); |
| 3232 | if (VG_(delFromFM)( map_usertag_to_SO, &keyW, &valW, usertag )) { |
| 3233 | SO* so = (SO*)valW; |
| 3234 | tl_assert(keyW == usertag); |
| 3235 | tl_assert(so); |
| 3236 | libhb_so_dealloc(so); |
| 3237 | } |
| 3238 | } |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3239 | |
| 3240 | |
| 3241 | static |
| 3242 | void evh__HG_USERSO_SEND_PRE ( ThreadId tid, UWord usertag ) |
| 3243 | { |
| 3244 | /* TID is just about to notionally sent a message on a notional |
| 3245 | abstract synchronisation object whose identity is given by |
| 3246 | USERTAG. Bind USERTAG to a real SO if it is not already so |
sewardj | 8c50d3c | 2011-03-11 18:38:12 +0000 | [diff] [blame] | 3247 | bound, and do a 'weak send' on the SO. This joins the vector |
| 3248 | clocks from this thread into any vector clocks already present |
| 3249 | in the SO. The resulting SO vector clocks are later used by |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3250 | other thread(s) which successfully 'receive' from the SO, |
sewardj | 8c50d3c | 2011-03-11 18:38:12 +0000 | [diff] [blame] | 3251 | thereby acquiring a dependency on all the events that have |
| 3252 | previously signalled on this SO. */ |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3253 | Thread* thr; |
| 3254 | SO* so; |
| 3255 | |
| 3256 | if (SHOW_EVENTS >= 1) |
| 3257 | VG_(printf)("evh__HG_USERSO_SEND_PRE(ctid=%d, usertag=%#lx)\n", |
| 3258 | (Int)tid, usertag ); |
| 3259 | |
| 3260 | thr = map_threads_maybe_lookup( tid ); |
| 3261 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 3262 | |
| 3263 | so = map_usertag_to_SO_lookup_or_alloc( usertag ); |
| 3264 | tl_assert(so); |
| 3265 | |
sewardj | 8c50d3c | 2011-03-11 18:38:12 +0000 | [diff] [blame] | 3266 | libhb_so_send( thr->hbthr, so, False/*!strong_send*/ ); |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3267 | } |
| 3268 | |
| 3269 | static |
| 3270 | void evh__HG_USERSO_RECV_POST ( ThreadId tid, UWord usertag ) |
| 3271 | { |
| 3272 | /* TID has just notionally received a message from a notional |
| 3273 | abstract synchronisation object whose identity is given by |
| 3274 | USERTAG. Bind USERTAG to a real SO if it is not already so |
| 3275 | bound. If the SO has at some point in the past been 'sent' on, |
| 3276 | to a 'strong receive' on it, thereby acquiring a dependency on |
| 3277 | the sender. */ |
| 3278 | Thread* thr; |
| 3279 | SO* so; |
| 3280 | |
| 3281 | if (SHOW_EVENTS >= 1) |
| 3282 | VG_(printf)("evh__HG_USERSO_RECV_POST(ctid=%d, usertag=%#lx)\n", |
| 3283 | (Int)tid, usertag ); |
| 3284 | |
| 3285 | thr = map_threads_maybe_lookup( tid ); |
| 3286 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 3287 | |
| 3288 | so = map_usertag_to_SO_lookup_or_alloc( usertag ); |
| 3289 | tl_assert(so); |
| 3290 | |
| 3291 | /* Acquire a dependency on it. If the SO has never so far been |
| 3292 | sent on, then libhb_so_recv will do nothing. So we're safe |
| 3293 | regardless of SO's history. */ |
| 3294 | libhb_so_recv( thr->hbthr, so, True/*strong_recv*/ ); |
| 3295 | } |
| 3296 | |
sewardj | 6015d0e | 2011-03-11 19:10:48 +0000 | [diff] [blame] | 3297 | static |
| 3298 | void evh__HG_USERSO_FORGET_ALL ( ThreadId tid, UWord usertag ) |
| 3299 | { |
| 3300 | /* TID declares that any happens-before edges notionally stored in |
| 3301 | USERTAG can be deleted. If (as would normally be the case) a |
| 3302 | SO is associated with USERTAG, then the assocation is removed |
| 3303 | and all resources associated with SO are freed. Importantly, |
| 3304 | that frees up any VTSs stored in SO. */ |
| 3305 | if (SHOW_EVENTS >= 1) |
| 3306 | VG_(printf)("evh__HG_USERSO_FORGET_ALL(ctid=%d, usertag=%#lx)\n", |
| 3307 | (Int)tid, usertag ); |
| 3308 | |
| 3309 | map_usertag_to_SO_delete( usertag ); |
| 3310 | } |
| 3311 | |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3312 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3313 | /*--------------------------------------------------------------*/ |
| 3314 | /*--- Lock acquisition order monitoring ---*/ |
| 3315 | /*--------------------------------------------------------------*/ |
| 3316 | |
| 3317 | /* FIXME: here are some optimisations still to do in |
| 3318 | laog__pre_thread_acquires_lock. |
| 3319 | |
| 3320 | The graph is structured so that if L1 --*--> L2 then L1 must be |
| 3321 | acquired before L2. |
| 3322 | |
| 3323 | The common case is that some thread T holds (eg) L1 L2 and L3 and |
| 3324 | is repeatedly acquiring and releasing Ln, and there is no ordering |
| 3325 | error in what it is doing. Hence it repeatly: |
| 3326 | |
| 3327 | (1) searches laog to see if Ln --*--> {L1,L2,L3}, which always |
| 3328 | produces the answer No (because there is no error). |
| 3329 | |
| 3330 | (2) adds edges {L1,L2,L3} --> Ln to laog, which are already present |
| 3331 | (because they already got added the first time T acquired Ln). |
| 3332 | |
| 3333 | Hence cache these two events: |
| 3334 | |
| 3335 | (1) Cache result of the query from last time. Invalidate the cache |
| 3336 | any time any edges are added to or deleted from laog. |
| 3337 | |
| 3338 | (2) Cache these add-edge requests and ignore them if said edges |
| 3339 | have already been added to laog. Invalidate the cache any time |
| 3340 | any edges are deleted from laog. |
| 3341 | */ |
| 3342 | |
| 3343 | typedef |
| 3344 | struct { |
| 3345 | WordSetID inns; /* in univ_laog */ |
| 3346 | WordSetID outs; /* in univ_laog */ |
| 3347 | } |
| 3348 | LAOGLinks; |
| 3349 | |
| 3350 | /* lock order acquisition graph */ |
| 3351 | static WordFM* laog = NULL; /* WordFM Lock* LAOGLinks* */ |
| 3352 | |
| 3353 | /* EXPOSITION ONLY: for each edge in 'laog', record the two places |
| 3354 | where that edge was created, so that we can show the user later if |
| 3355 | we need to. */ |
| 3356 | typedef |
| 3357 | struct { |
| 3358 | Addr src_ga; /* Lock guest addresses for */ |
| 3359 | Addr dst_ga; /* src/dst of the edge */ |
| 3360 | ExeContext* src_ec; /* And corresponding places where that */ |
| 3361 | ExeContext* dst_ec; /* ordering was established */ |
| 3362 | } |
| 3363 | LAOGLinkExposition; |
| 3364 | |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3365 | static Word cmp_LAOGLinkExposition ( UWord llx1W, UWord llx2W ) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3366 | /* Compare LAOGLinkExposition*s by (src_ga,dst_ga) field pair. */ |
| 3367 | LAOGLinkExposition* llx1 = (LAOGLinkExposition*)llx1W; |
| 3368 | LAOGLinkExposition* llx2 = (LAOGLinkExposition*)llx2W; |
| 3369 | if (llx1->src_ga < llx2->src_ga) return -1; |
| 3370 | if (llx1->src_ga > llx2->src_ga) return 1; |
| 3371 | if (llx1->dst_ga < llx2->dst_ga) return -1; |
| 3372 | if (llx1->dst_ga > llx2->dst_ga) return 1; |
| 3373 | return 0; |
| 3374 | } |
| 3375 | |
| 3376 | static WordFM* laog_exposition = NULL; /* WordFM LAOGLinkExposition* NULL */ |
| 3377 | /* end EXPOSITION ONLY */ |
| 3378 | |
| 3379 | |
sewardj | a65db10 | 2009-01-26 10:45:16 +0000 | [diff] [blame] | 3380 | __attribute__((noinline)) |
| 3381 | static void laog__init ( void ) |
| 3382 | { |
| 3383 | tl_assert(!laog); |
| 3384 | tl_assert(!laog_exposition); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 3385 | tl_assert(HG_(clo_track_lockorders)); |
sewardj | a65db10 | 2009-01-26 10:45:16 +0000 | [diff] [blame] | 3386 | |
| 3387 | laog = VG_(newFM)( HG_(zalloc), "hg.laog__init.1", |
| 3388 | HG_(free), NULL/*unboxedcmp*/ ); |
| 3389 | |
| 3390 | laog_exposition = VG_(newFM)( HG_(zalloc), "hg.laog__init.2", HG_(free), |
| 3391 | cmp_LAOGLinkExposition ); |
| 3392 | tl_assert(laog); |
| 3393 | tl_assert(laog_exposition); |
| 3394 | } |
| 3395 | |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3396 | static void laog__show ( const HChar* who ) { |
| 3397 | UWord i, ws_size; |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3398 | UWord* ws_words; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3399 | Lock* me; |
| 3400 | LAOGLinks* links; |
| 3401 | VG_(printf)("laog (requested by %s) {\n", who); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3402 | VG_(initIterFM)( laog ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3403 | me = NULL; |
| 3404 | links = NULL; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3405 | while (VG_(nextIterFM)( laog, (UWord*)&me, |
| 3406 | (UWord*)&links )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3407 | tl_assert(me); |
| 3408 | tl_assert(links); |
| 3409 | VG_(printf)(" node %p:\n", me); |
| 3410 | HG_(getPayloadWS)( &ws_words, &ws_size, univ_laog, links->inns ); |
| 3411 | for (i = 0; i < ws_size; i++) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3412 | VG_(printf)(" inn %#lx\n", ws_words[i] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3413 | HG_(getPayloadWS)( &ws_words, &ws_size, univ_laog, links->outs ); |
| 3414 | for (i = 0; i < ws_size; i++) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3415 | VG_(printf)(" out %#lx\n", ws_words[i] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3416 | me = NULL; |
| 3417 | links = NULL; |
| 3418 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3419 | VG_(doneIterFM)( laog ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3420 | VG_(printf)("}\n"); |
| 3421 | } |
| 3422 | |
sewardj | 866c80c | 2011-10-22 19:29:51 +0000 | [diff] [blame] | 3423 | static void univ_laog_do_GC ( void ) { |
| 3424 | Word i; |
| 3425 | LAOGLinks* links; |
| 3426 | Word seen = 0; |
| 3427 | Int prev_next_gc_univ_laog = next_gc_univ_laog; |
| 3428 | const UWord univ_laog_cardinality = HG_(cardinalityWSU)( univ_laog); |
| 3429 | |
| 3430 | Bool *univ_laog_seen = HG_(zalloc) ( "hg.gc_univ_laog.1", |
| 3431 | (Int) univ_laog_cardinality |
| 3432 | * sizeof(Bool) ); |
| 3433 | // univ_laog_seen[*] set to 0 (False) by zalloc. |
| 3434 | |
| 3435 | if (VG_(clo_stats)) |
| 3436 | VG_(message)(Vg_DebugMsg, |
| 3437 | "univ_laog_do_GC enter cardinality %'10d\n", |
| 3438 | (Int)univ_laog_cardinality); |
| 3439 | |
| 3440 | VG_(initIterFM)( laog ); |
| 3441 | links = NULL; |
| 3442 | while (VG_(nextIterFM)( laog, NULL, (UWord*)&links )) { |
| 3443 | tl_assert(links); |
| 3444 | tl_assert(links->inns >= 0 && links->inns < univ_laog_cardinality); |
| 3445 | univ_laog_seen[links->inns] = True; |
| 3446 | tl_assert(links->outs >= 0 && links->outs < univ_laog_cardinality); |
| 3447 | univ_laog_seen[links->outs] = True; |
| 3448 | links = NULL; |
| 3449 | } |
| 3450 | VG_(doneIterFM)( laog ); |
| 3451 | |
| 3452 | for (i = 0; i < (Int)univ_laog_cardinality; i++) { |
| 3453 | if (univ_laog_seen[i]) |
| 3454 | seen++; |
| 3455 | else |
| 3456 | HG_(dieWS) ( univ_laog, (WordSet)i ); |
| 3457 | } |
| 3458 | |
| 3459 | HG_(free) (univ_laog_seen); |
| 3460 | |
| 3461 | // We need to decide the value of the next_gc. |
| 3462 | // 3 solutions were looked at: |
| 3463 | // Sol 1: garbage collect at seen * 2 |
| 3464 | // This solution was a lot slower, probably because we both do a lot of |
| 3465 | // garbage collection and do not keep long enough laog WV that will become |
| 3466 | // useful again very soon. |
| 3467 | // Sol 2: garbage collect at a percentage increase of the current cardinality |
| 3468 | // (with a min increase of 1) |
| 3469 | // Trials on a small test program with 1%, 5% and 10% increase was done. |
| 3470 | // 1% is slightly faster than 5%, which is slightly slower than 10%. |
| 3471 | // However, on a big application, this caused the memory to be exhausted, |
| 3472 | // as even a 1% increase of size at each gc becomes a lot, when many gc |
| 3473 | // are done. |
| 3474 | // Sol 3: always garbage collect at current cardinality + 1. |
| 3475 | // This solution was the fastest of the 3 solutions, and caused no memory |
| 3476 | // exhaustion in the big application. |
| 3477 | // |
| 3478 | // With regards to cost introduced by gc: on the t2t perf test (doing only |
| 3479 | // lock/unlock operations), t2t 50 10 2 was about 25% faster than the |
| 3480 | // version with garbage collection. With t2t 50 20 2, my machine started |
| 3481 | // to page out, and so the garbage collected version was much faster. |
| 3482 | // On smaller lock sets (e.g. t2t 20 5 2, giving about 100 locks), the |
| 3483 | // difference performance is insignificant (~ 0.1 s). |
| 3484 | // Of course, it might be that real life programs are not well represented |
| 3485 | // by t2t. |
| 3486 | |
| 3487 | // If ever we want to have a more sophisticated control |
| 3488 | // (e.g. clo options to control the percentage increase or fixed increased), |
| 3489 | // we should do it here, eg. |
| 3490 | // next_gc_univ_laog = prev_next_gc_univ_laog + VG_(clo_laog_gc_fixed); |
| 3491 | // Currently, we just hard-code the solution 3 above. |
| 3492 | next_gc_univ_laog = prev_next_gc_univ_laog + 1; |
| 3493 | |
| 3494 | if (VG_(clo_stats)) |
| 3495 | VG_(message) |
| 3496 | (Vg_DebugMsg, |
| 3497 | "univ_laog_do_GC exit seen %'8d next gc at cardinality %'10d\n", |
| 3498 | (Int)seen, next_gc_univ_laog); |
| 3499 | } |
| 3500 | |
| 3501 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3502 | __attribute__((noinline)) |
| 3503 | static void laog__add_edge ( Lock* src, Lock* dst ) { |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3504 | UWord keyW; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3505 | LAOGLinks* links; |
| 3506 | Bool presentF, presentR; |
| 3507 | if (0) VG_(printf)("laog__add_edge %p %p\n", src, dst); |
| 3508 | |
| 3509 | /* Take the opportunity to sanity check the graph. Record in |
| 3510 | presentF if there is already a src->dst mapping in this node's |
| 3511 | forwards links, and presentR if there is already a src->dst |
| 3512 | mapping in this node's backwards links. They should agree! |
| 3513 | Also, we need to know whether the edge was already present so as |
| 3514 | to decide whether or not to update the link details mapping. We |
| 3515 | can compute presentF and presentR essentially for free, so may |
| 3516 | as well do this always. */ |
| 3517 | presentF = presentR = False; |
| 3518 | |
| 3519 | /* Update the out edges for src */ |
| 3520 | keyW = 0; |
| 3521 | links = NULL; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3522 | if (VG_(lookupFM)( laog, &keyW, (UWord*)&links, (UWord)src )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3523 | WordSetID outs_new; |
| 3524 | tl_assert(links); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3525 | tl_assert(keyW == (UWord)src); |
| 3526 | outs_new = HG_(addToWS)( univ_laog, links->outs, (UWord)dst ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3527 | presentF = outs_new == links->outs; |
| 3528 | links->outs = outs_new; |
| 3529 | } else { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3530 | links = HG_(zalloc)("hg.lae.1", sizeof(LAOGLinks)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3531 | links->inns = HG_(emptyWS)( univ_laog ); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3532 | links->outs = HG_(singletonWS)( univ_laog, (UWord)dst ); |
| 3533 | VG_(addToFM)( laog, (UWord)src, (UWord)links ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3534 | } |
| 3535 | /* Update the in edges for dst */ |
| 3536 | keyW = 0; |
| 3537 | links = NULL; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3538 | if (VG_(lookupFM)( laog, &keyW, (UWord*)&links, (UWord)dst )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3539 | WordSetID inns_new; |
| 3540 | tl_assert(links); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3541 | tl_assert(keyW == (UWord)dst); |
| 3542 | inns_new = HG_(addToWS)( univ_laog, links->inns, (UWord)src ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3543 | presentR = inns_new == links->inns; |
| 3544 | links->inns = inns_new; |
| 3545 | } else { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3546 | links = HG_(zalloc)("hg.lae.2", sizeof(LAOGLinks)); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3547 | links->inns = HG_(singletonWS)( univ_laog, (UWord)src ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3548 | links->outs = HG_(emptyWS)( univ_laog ); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3549 | VG_(addToFM)( laog, (UWord)dst, (UWord)links ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
| 3552 | tl_assert( (presentF && presentR) || (!presentF && !presentR) ); |
| 3553 | |
| 3554 | if (!presentF && src->acquired_at && dst->acquired_at) { |
| 3555 | LAOGLinkExposition expo; |
| 3556 | /* If this edge is entering the graph, and we have acquired_at |
| 3557 | information for both src and dst, record those acquisition |
| 3558 | points. Hence, if there is later a violation of this |
| 3559 | ordering, we can show the user the two places in which the |
| 3560 | required src-dst ordering was previously established. */ |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3561 | if (0) VG_(printf)("acquire edge %#lx %#lx\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3562 | src->guestaddr, dst->guestaddr); |
| 3563 | expo.src_ga = src->guestaddr; |
| 3564 | expo.dst_ga = dst->guestaddr; |
| 3565 | expo.src_ec = NULL; |
| 3566 | expo.dst_ec = NULL; |
| 3567 | tl_assert(laog_exposition); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3568 | if (VG_(lookupFM)( laog_exposition, NULL, NULL, (UWord)&expo )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3569 | /* we already have it; do nothing */ |
| 3570 | } else { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3571 | LAOGLinkExposition* expo2 = HG_(zalloc)("hg.lae.3", |
| 3572 | sizeof(LAOGLinkExposition)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3573 | expo2->src_ga = src->guestaddr; |
| 3574 | expo2->dst_ga = dst->guestaddr; |
| 3575 | expo2->src_ec = src->acquired_at; |
| 3576 | expo2->dst_ec = dst->acquired_at; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3577 | VG_(addToFM)( laog_exposition, (UWord)expo2, (UWord)NULL ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3578 | } |
| 3579 | } |
sewardj | 866c80c | 2011-10-22 19:29:51 +0000 | [diff] [blame] | 3580 | |
| 3581 | if (HG_(cardinalityWSU) (univ_laog) >= next_gc_univ_laog) |
| 3582 | univ_laog_do_GC(); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | __attribute__((noinline)) |
| 3586 | static void laog__del_edge ( Lock* src, Lock* dst ) { |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3587 | UWord keyW; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3588 | LAOGLinks* links; |
sewardj | 866c80c | 2011-10-22 19:29:51 +0000 | [diff] [blame] | 3589 | if (0) VG_(printf)("laog__del_edge enter %p %p\n", src, dst); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3590 | /* Update the out edges for src */ |
| 3591 | keyW = 0; |
| 3592 | links = NULL; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3593 | if (VG_(lookupFM)( laog, &keyW, (UWord*)&links, (UWord)src )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3594 | tl_assert(links); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3595 | tl_assert(keyW == (UWord)src); |
| 3596 | links->outs = HG_(delFromWS)( univ_laog, links->outs, (UWord)dst ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3597 | } |
| 3598 | /* Update the in edges for dst */ |
| 3599 | keyW = 0; |
| 3600 | links = NULL; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3601 | if (VG_(lookupFM)( laog, &keyW, (UWord*)&links, (UWord)dst )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3602 | tl_assert(links); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3603 | tl_assert(keyW == (UWord)dst); |
| 3604 | links->inns = HG_(delFromWS)( univ_laog, links->inns, (UWord)src ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3605 | } |
sewardj | 866c80c | 2011-10-22 19:29:51 +0000 | [diff] [blame] | 3606 | |
| 3607 | /* Remove the exposition of src,dst (if present) */ |
| 3608 | { |
| 3609 | LAOGLinkExposition *fm_expo; |
| 3610 | |
| 3611 | LAOGLinkExposition expo; |
| 3612 | expo.src_ga = src->guestaddr; |
| 3613 | expo.dst_ga = dst->guestaddr; |
| 3614 | expo.src_ec = NULL; |
| 3615 | expo.dst_ec = NULL; |
| 3616 | |
| 3617 | if (VG_(delFromFM) (laog_exposition, |
| 3618 | (UWord*)&fm_expo, NULL, (UWord)&expo )) { |
| 3619 | HG_(free) (fm_expo); |
| 3620 | } |
| 3621 | } |
| 3622 | |
| 3623 | /* deleting edges can increase nr of of WS so check for gc. */ |
| 3624 | if (HG_(cardinalityWSU) (univ_laog) >= next_gc_univ_laog) |
| 3625 | univ_laog_do_GC(); |
| 3626 | if (0) VG_(printf)("laog__del_edge exit\n"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | __attribute__((noinline)) |
| 3630 | static WordSetID /* in univ_laog */ laog__succs ( Lock* lk ) { |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3631 | UWord keyW; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3632 | LAOGLinks* links; |
| 3633 | keyW = 0; |
| 3634 | links = NULL; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3635 | if (VG_(lookupFM)( laog, &keyW, (UWord*)&links, (UWord)lk )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3636 | tl_assert(links); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3637 | tl_assert(keyW == (UWord)lk); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3638 | return links->outs; |
| 3639 | } else { |
| 3640 | return HG_(emptyWS)( univ_laog ); |
| 3641 | } |
| 3642 | } |
| 3643 | |
| 3644 | __attribute__((noinline)) |
| 3645 | static WordSetID /* in univ_laog */ laog__preds ( Lock* lk ) { |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3646 | UWord keyW; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3647 | LAOGLinks* links; |
| 3648 | keyW = 0; |
| 3649 | links = NULL; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3650 | if (VG_(lookupFM)( laog, &keyW, (UWord*)&links, (UWord)lk )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3651 | tl_assert(links); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3652 | tl_assert(keyW == (UWord)lk); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3653 | return links->inns; |
| 3654 | } else { |
| 3655 | return HG_(emptyWS)( univ_laog ); |
| 3656 | } |
| 3657 | } |
| 3658 | |
| 3659 | __attribute__((noinline)) |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3660 | static void laog__sanity_check ( const HChar* who ) { |
| 3661 | UWord i, ws_size; |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3662 | UWord* ws_words; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3663 | Lock* me; |
| 3664 | LAOGLinks* links; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3665 | VG_(initIterFM)( laog ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3666 | me = NULL; |
| 3667 | links = NULL; |
| 3668 | if (0) VG_(printf)("laog sanity check\n"); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3669 | while (VG_(nextIterFM)( laog, (UWord*)&me, |
| 3670 | (UWord*)&links )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3671 | tl_assert(me); |
| 3672 | tl_assert(links); |
| 3673 | HG_(getPayloadWS)( &ws_words, &ws_size, univ_laog, links->inns ); |
| 3674 | for (i = 0; i < ws_size; i++) { |
| 3675 | if ( ! HG_(elemWS)( univ_laog, |
| 3676 | laog__succs( (Lock*)ws_words[i] ), |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3677 | (UWord)me )) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3678 | goto bad; |
| 3679 | } |
| 3680 | HG_(getPayloadWS)( &ws_words, &ws_size, univ_laog, links->outs ); |
| 3681 | for (i = 0; i < ws_size; i++) { |
| 3682 | if ( ! HG_(elemWS)( univ_laog, |
| 3683 | laog__preds( (Lock*)ws_words[i] ), |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3684 | (UWord)me )) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3685 | goto bad; |
| 3686 | } |
| 3687 | me = NULL; |
| 3688 | links = NULL; |
| 3689 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3690 | VG_(doneIterFM)( laog ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3691 | return; |
| 3692 | |
| 3693 | bad: |
| 3694 | VG_(printf)("laog__sanity_check(%s) FAILED\n", who); |
| 3695 | laog__show(who); |
| 3696 | tl_assert(0); |
| 3697 | } |
| 3698 | |
| 3699 | /* If there is a path in laog from 'src' to any of the elements in |
| 3700 | 'dst', return an arbitrarily chosen element of 'dst' reachable from |
| 3701 | 'src'. If no path exist from 'src' to any element in 'dst', return |
| 3702 | NULL. */ |
| 3703 | __attribute__((noinline)) |
| 3704 | static |
| 3705 | Lock* laog__do_dfs_from_to ( Lock* src, WordSetID dsts /* univ_lsets */ ) |
| 3706 | { |
| 3707 | Lock* ret; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3708 | Word ssz; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3709 | XArray* stack; /* of Lock* */ |
| 3710 | WordFM* visited; /* Lock* -> void, iow, Set(Lock*) */ |
| 3711 | Lock* here; |
| 3712 | WordSetID succs; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3713 | UWord succs_size, i; |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3714 | UWord* succs_words; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3715 | //laog__sanity_check(); |
| 3716 | |
| 3717 | /* If the destination set is empty, we can never get there from |
| 3718 | 'src' :-), so don't bother to try */ |
| 3719 | if (HG_(isEmptyWS)( univ_lsets, dsts )) |
| 3720 | return NULL; |
| 3721 | |
| 3722 | ret = NULL; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3723 | stack = VG_(newXA)( HG_(zalloc), "hg.lddft.1", HG_(free), sizeof(Lock*) ); |
| 3724 | visited = VG_(newFM)( HG_(zalloc), "hg.lddft.2", HG_(free), NULL/*unboxedcmp*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3725 | |
| 3726 | (void) VG_(addToXA)( stack, &src ); |
| 3727 | |
| 3728 | while (True) { |
| 3729 | |
| 3730 | ssz = VG_(sizeXA)( stack ); |
| 3731 | |
| 3732 | if (ssz == 0) { ret = NULL; break; } |
| 3733 | |
| 3734 | here = *(Lock**) VG_(indexXA)( stack, ssz-1 ); |
| 3735 | VG_(dropTailXA)( stack, 1 ); |
| 3736 | |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3737 | if (HG_(elemWS)( univ_lsets, dsts, (UWord)here )) { ret = here; break; } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3738 | |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3739 | if (VG_(lookupFM)( visited, NULL, NULL, (UWord)here )) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3740 | continue; |
| 3741 | |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3742 | VG_(addToFM)( visited, (UWord)here, 0 ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3743 | |
| 3744 | succs = laog__succs( here ); |
| 3745 | HG_(getPayloadWS)( &succs_words, &succs_size, univ_laog, succs ); |
| 3746 | for (i = 0; i < succs_size; i++) |
| 3747 | (void) VG_(addToXA)( stack, &succs_words[i] ); |
| 3748 | } |
| 3749 | |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3750 | VG_(deleteFM)( visited, NULL, NULL ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3751 | VG_(deleteXA)( stack ); |
| 3752 | return ret; |
| 3753 | } |
| 3754 | |
| 3755 | |
| 3756 | /* Thread 'thr' is acquiring 'lk'. Check for inconsistent ordering |
| 3757 | between 'lk' and the locks already held by 'thr' and issue a |
| 3758 | complaint if so. Also, update the ordering graph appropriately. |
| 3759 | */ |
| 3760 | __attribute__((noinline)) |
| 3761 | static void laog__pre_thread_acquires_lock ( |
| 3762 | Thread* thr, /* NB: BEFORE lock is added */ |
| 3763 | Lock* lk |
| 3764 | ) |
| 3765 | { |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3766 | UWord* ls_words; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3767 | UWord ls_size, i; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3768 | Lock* other; |
| 3769 | |
| 3770 | /* It may be that 'thr' already holds 'lk' and is recursively |
| 3771 | relocking in. In this case we just ignore the call. */ |
| 3772 | /* NB: univ_lsets really is correct here */ |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3773 | if (HG_(elemWS)( univ_lsets, thr->locksetA, (UWord)lk )) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3774 | return; |
| 3775 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3776 | /* First, the check. Complain if there is any path in laog from lk |
| 3777 | to any of the locks already held by thr, since if any such path |
| 3778 | existed, it would mean that previously lk was acquired before |
| 3779 | (rather than after, as we are doing here) at least one of those |
| 3780 | locks. |
| 3781 | */ |
| 3782 | other = laog__do_dfs_from_to(lk, thr->locksetA); |
| 3783 | if (other) { |
| 3784 | LAOGLinkExposition key, *found; |
| 3785 | /* So we managed to find a path lk --*--> other in the graph, |
| 3786 | which implies that 'lk' should have been acquired before |
| 3787 | 'other' but is in fact being acquired afterwards. We present |
| 3788 | the lk/other arguments to record_error_LockOrder in the order |
| 3789 | in which they should have been acquired. */ |
| 3790 | /* Go look in the laog_exposition mapping, to find the allocation |
| 3791 | points for this edge, so we can show the user. */ |
| 3792 | key.src_ga = lk->guestaddr; |
| 3793 | key.dst_ga = other->guestaddr; |
| 3794 | key.src_ec = NULL; |
| 3795 | key.dst_ec = NULL; |
| 3796 | found = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3797 | if (VG_(lookupFM)( laog_exposition, |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3798 | (UWord*)&found, NULL, (UWord)&key )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3799 | tl_assert(found != &key); |
| 3800 | tl_assert(found->src_ga == key.src_ga); |
| 3801 | tl_assert(found->dst_ga == key.dst_ga); |
| 3802 | tl_assert(found->src_ec); |
| 3803 | tl_assert(found->dst_ec); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3804 | HG_(record_error_LockOrder)( |
philippe | 46daf0d | 2014-07-29 20:08:15 +0000 | [diff] [blame] | 3805 | thr, lk, other, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 3806 | found->src_ec, found->dst_ec, other->acquired_at ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3807 | } else { |
| 3808 | /* Hmm. This can't happen (can it?) */ |
philippe | ebe2580 | 2013-01-30 23:21:34 +0000 | [diff] [blame] | 3809 | /* Yes, it can happen: see tests/tc14_laog_dinphils. |
| 3810 | Imagine we have 3 philosophers A B C, and the forks |
| 3811 | between them: |
| 3812 | |
| 3813 | C |
| 3814 | |
| 3815 | fCA fBC |
| 3816 | |
| 3817 | A fAB B |
| 3818 | |
| 3819 | Let's have the following actions: |
| 3820 | A takes fCA,fAB |
| 3821 | A releases fCA,fAB |
| 3822 | B takes fAB,fBC |
| 3823 | B releases fAB,fBC |
| 3824 | C takes fBC,fCA |
| 3825 | C releases fBC,fCA |
| 3826 | |
| 3827 | Helgrind will report a lock order error when C takes fCA. |
| 3828 | Effectively, we have a deadlock if the following |
| 3829 | sequence is done: |
| 3830 | A takes fCA |
| 3831 | B takes fAB |
| 3832 | C takes fBC |
| 3833 | |
| 3834 | The error reported is: |
| 3835 | Observed (incorrect) order fBC followed by fCA |
| 3836 | but the stack traces that have established the required order |
| 3837 | are not given. |
| 3838 | |
| 3839 | This is because there is no pair (fCA, fBC) in laog exposition : |
| 3840 | the laog_exposition records all pairs of locks between a new lock |
| 3841 | taken by a thread and all the already taken locks. |
| 3842 | So, there is no laog_exposition (fCA, fBC) as no thread ever |
| 3843 | first locked fCA followed by fBC. |
| 3844 | |
| 3845 | In other words, when the deadlock cycle involves more than |
| 3846 | two locks, then helgrind does not report the sequence of |
| 3847 | operations that created the cycle. |
| 3848 | |
| 3849 | However, we can report the current stack trace (where |
| 3850 | lk is being taken), and the stack trace where other was acquired: |
| 3851 | Effectively, the variable 'other' contains a lock currently |
| 3852 | held by this thread, with its 'acquired_at'. */ |
| 3853 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3854 | HG_(record_error_LockOrder)( |
philippe | 46daf0d | 2014-07-29 20:08:15 +0000 | [diff] [blame] | 3855 | thr, lk, other, |
philippe | ebe2580 | 2013-01-30 23:21:34 +0000 | [diff] [blame] | 3856 | NULL, NULL, other->acquired_at ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | /* Second, add to laog the pairs |
| 3861 | (old, lk) | old <- locks already held by thr |
| 3862 | Since both old and lk are currently held by thr, their acquired_at |
| 3863 | fields must be non-NULL. |
| 3864 | */ |
| 3865 | tl_assert(lk->acquired_at); |
| 3866 | HG_(getPayloadWS)( &ls_words, &ls_size, univ_lsets, thr->locksetA ); |
| 3867 | for (i = 0; i < ls_size; i++) { |
| 3868 | Lock* old = (Lock*)ls_words[i]; |
| 3869 | tl_assert(old->acquired_at); |
| 3870 | laog__add_edge( old, lk ); |
| 3871 | } |
| 3872 | |
| 3873 | /* Why "except_Locks" ? We're here because a lock is being |
| 3874 | acquired by a thread, and we're in an inconsistent state here. |
| 3875 | See the call points in evhH__post_thread_{r,w}_acquires_lock. |
| 3876 | When called in this inconsistent state, locks__sanity_check duly |
| 3877 | barfs. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3878 | if (HG_(clo_sanity_flags) & SCE_LAOG) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3879 | all_except_Locks__sanity_check("laog__pre_thread_acquires_lock-post"); |
| 3880 | } |
| 3881 | |
sewardj | 866c80c | 2011-10-22 19:29:51 +0000 | [diff] [blame] | 3882 | /* Allocates a duplicate of words. Caller must HG_(free) the result. */ |
| 3883 | static UWord* UWordV_dup(UWord* words, Word words_size) |
| 3884 | { |
| 3885 | UInt i; |
| 3886 | |
| 3887 | if (words_size == 0) |
| 3888 | return NULL; |
| 3889 | |
| 3890 | UWord *dup = HG_(zalloc) ("hg.dup.1", (SizeT) words_size * sizeof(UWord)); |
| 3891 | |
| 3892 | for (i = 0; i < words_size; i++) |
| 3893 | dup[i] = words[i]; |
| 3894 | |
| 3895 | return dup; |
| 3896 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3897 | |
| 3898 | /* Delete from 'laog' any pair mentioning a lock in locksToDelete */ |
| 3899 | |
| 3900 | __attribute__((noinline)) |
| 3901 | static void laog__handle_one_lock_deletion ( Lock* lk ) |
| 3902 | { |
| 3903 | WordSetID preds, succs; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 3904 | UWord preds_size, succs_size, i, j; |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3905 | UWord *preds_words, *succs_words; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3906 | |
| 3907 | preds = laog__preds( lk ); |
| 3908 | succs = laog__succs( lk ); |
| 3909 | |
sewardj | 866c80c | 2011-10-22 19:29:51 +0000 | [diff] [blame] | 3910 | // We need to duplicate the payload, as these can be garbage collected |
| 3911 | // during the del/add operations below. |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3912 | HG_(getPayloadWS)( &preds_words, &preds_size, univ_laog, preds ); |
sewardj | 866c80c | 2011-10-22 19:29:51 +0000 | [diff] [blame] | 3913 | preds_words = UWordV_dup(preds_words, preds_size); |
| 3914 | |
| 3915 | HG_(getPayloadWS)( &succs_words, &succs_size, univ_laog, succs ); |
| 3916 | succs_words = UWordV_dup(succs_words, succs_size); |
| 3917 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3918 | for (i = 0; i < preds_size; i++) |
| 3919 | laog__del_edge( (Lock*)preds_words[i], lk ); |
| 3920 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3921 | for (j = 0; j < succs_size; j++) |
| 3922 | laog__del_edge( lk, (Lock*)succs_words[j] ); |
| 3923 | |
| 3924 | for (i = 0; i < preds_size; i++) { |
| 3925 | for (j = 0; j < succs_size; j++) { |
| 3926 | if (preds_words[i] != succs_words[j]) { |
| 3927 | /* This can pass unlocked locks to laog__add_edge, since |
| 3928 | we're deleting stuff. So their acquired_at fields may |
| 3929 | be NULL. */ |
| 3930 | laog__add_edge( (Lock*)preds_words[i], (Lock*)succs_words[j] ); |
| 3931 | } |
| 3932 | } |
| 3933 | } |
sewardj | 866c80c | 2011-10-22 19:29:51 +0000 | [diff] [blame] | 3934 | |
| 3935 | if (preds_words) |
| 3936 | HG_(free) (preds_words); |
| 3937 | if (succs_words) |
| 3938 | HG_(free) (succs_words); |
| 3939 | |
| 3940 | // Remove lk information from laog links FM |
| 3941 | { |
| 3942 | LAOGLinks *links; |
| 3943 | Lock* linked_lk; |
| 3944 | |
| 3945 | if (VG_(delFromFM) (laog, |
| 3946 | (UWord*)&linked_lk, (UWord*)&links, (UWord)lk)) { |
| 3947 | tl_assert (linked_lk == lk); |
| 3948 | HG_(free) (links); |
| 3949 | } |
| 3950 | } |
| 3951 | /* FIXME ??? What about removing lock lk data from EXPOSITION ??? */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
sewardj | 1cbc12f | 2008-11-10 16:16:46 +0000 | [diff] [blame] | 3954 | //__attribute__((noinline)) |
| 3955 | //static void laog__handle_lock_deletions ( |
| 3956 | // WordSetID /* in univ_laog */ locksToDelete |
| 3957 | // ) |
| 3958 | //{ |
| 3959 | // Word i, ws_size; |
| 3960 | // UWord* ws_words; |
| 3961 | // |
sewardj | 1cbc12f | 2008-11-10 16:16:46 +0000 | [diff] [blame] | 3962 | // |
| 3963 | // HG_(getPayloadWS)( &ws_words, &ws_size, univ_lsets, locksToDelete ); |
sewardj | 866c80c | 2011-10-22 19:29:51 +0000 | [diff] [blame] | 3964 | // UWordV_dup call needed here ... |
sewardj | 1cbc12f | 2008-11-10 16:16:46 +0000 | [diff] [blame] | 3965 | // for (i = 0; i < ws_size; i++) |
| 3966 | // laog__handle_one_lock_deletion( (Lock*)ws_words[i] ); |
| 3967 | // |
| 3968 | // if (HG_(clo_sanity_flags) & SCE_LAOG) |
| 3969 | // all__sanity_check("laog__handle_lock_deletions-post"); |
| 3970 | //} |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3971 | |
| 3972 | |
| 3973 | /*--------------------------------------------------------------*/ |
| 3974 | /*--- Malloc/free replacements ---*/ |
| 3975 | /*--------------------------------------------------------------*/ |
| 3976 | |
| 3977 | typedef |
| 3978 | struct { |
| 3979 | void* next; /* required by m_hashtable */ |
| 3980 | Addr payload; /* ptr to actual block */ |
| 3981 | SizeT szB; /* size requested */ |
| 3982 | ExeContext* where; /* where it was allocated */ |
| 3983 | Thread* thr; /* allocating thread */ |
| 3984 | } |
| 3985 | MallocMeta; |
| 3986 | |
| 3987 | /* A hash table of MallocMetas, used to track malloc'd blocks |
| 3988 | (obviously). */ |
| 3989 | static VgHashTable hg_mallocmeta_table = NULL; |
| 3990 | |
philippe | 5fbc976 | 2013-12-01 19:28:48 +0000 | [diff] [blame] | 3991 | /* MallocMeta are small elements. We use a pool to avoid |
| 3992 | the overhead of malloc for each MallocMeta. */ |
| 3993 | static PoolAlloc *MallocMeta_poolalloc = NULL; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3994 | |
| 3995 | static MallocMeta* new_MallocMeta ( void ) { |
philippe | 5fbc976 | 2013-12-01 19:28:48 +0000 | [diff] [blame] | 3996 | MallocMeta* md = VG_(allocEltPA) (MallocMeta_poolalloc); |
| 3997 | VG_(memset)(md, 0, sizeof(MallocMeta)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3998 | return md; |
| 3999 | } |
| 4000 | static void delete_MallocMeta ( MallocMeta* md ) { |
philippe | 5fbc976 | 2013-12-01 19:28:48 +0000 | [diff] [blame] | 4001 | VG_(freeEltPA)(MallocMeta_poolalloc, md); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | |
| 4005 | /* Allocate a client block and set up the metadata for it. */ |
| 4006 | |
| 4007 | static |
| 4008 | void* handle_alloc ( ThreadId tid, |
| 4009 | SizeT szB, SizeT alignB, Bool is_zeroed ) |
| 4010 | { |
| 4011 | Addr p; |
| 4012 | MallocMeta* md; |
| 4013 | |
| 4014 | tl_assert( ((SSizeT)szB) >= 0 ); |
| 4015 | p = (Addr)VG_(cli_malloc)(alignB, szB); |
| 4016 | if (!p) { |
| 4017 | return NULL; |
| 4018 | } |
| 4019 | if (is_zeroed) |
| 4020 | VG_(memset)((void*)p, 0, szB); |
| 4021 | |
| 4022 | /* Note that map_threads_lookup must succeed (cannot assert), since |
| 4023 | memory can only be allocated by currently alive threads, hence |
| 4024 | they must have an entry in map_threads. */ |
| 4025 | md = new_MallocMeta(); |
| 4026 | md->payload = p; |
| 4027 | md->szB = szB; |
| 4028 | md->where = VG_(record_ExeContext)( tid, 0 ); |
| 4029 | md->thr = map_threads_lookup( tid ); |
| 4030 | |
| 4031 | VG_(HT_add_node)( hg_mallocmeta_table, (VgHashNode*)md ); |
| 4032 | |
| 4033 | /* Tell the lower level memory wranglers. */ |
| 4034 | evh__new_mem_heap( p, szB, is_zeroed ); |
| 4035 | |
| 4036 | return (void*)p; |
| 4037 | } |
| 4038 | |
| 4039 | /* Re the checks for less-than-zero (also in hg_cli__realloc below): |
| 4040 | Cast to a signed type to catch any unexpectedly negative args. |
| 4041 | We're assuming here that the size asked for is not greater than |
| 4042 | 2^31 bytes (for 32-bit platforms) or 2^63 bytes (for 64-bit |
| 4043 | platforms). */ |
| 4044 | static void* hg_cli__malloc ( ThreadId tid, SizeT n ) { |
| 4045 | if (((SSizeT)n) < 0) return NULL; |
| 4046 | return handle_alloc ( tid, n, VG_(clo_alignment), |
| 4047 | /*is_zeroed*/False ); |
| 4048 | } |
| 4049 | static void* hg_cli____builtin_new ( ThreadId tid, SizeT n ) { |
| 4050 | if (((SSizeT)n) < 0) return NULL; |
| 4051 | return handle_alloc ( tid, n, VG_(clo_alignment), |
| 4052 | /*is_zeroed*/False ); |
| 4053 | } |
| 4054 | static void* hg_cli____builtin_vec_new ( ThreadId tid, SizeT n ) { |
| 4055 | if (((SSizeT)n) < 0) return NULL; |
| 4056 | return handle_alloc ( tid, n, VG_(clo_alignment), |
| 4057 | /*is_zeroed*/False ); |
| 4058 | } |
| 4059 | static void* hg_cli__memalign ( ThreadId tid, SizeT align, SizeT n ) { |
| 4060 | if (((SSizeT)n) < 0) return NULL; |
| 4061 | return handle_alloc ( tid, n, align, |
| 4062 | /*is_zeroed*/False ); |
| 4063 | } |
| 4064 | static void* hg_cli__calloc ( ThreadId tid, SizeT nmemb, SizeT size1 ) { |
| 4065 | if ( ((SSizeT)nmemb) < 0 || ((SSizeT)size1) < 0 ) return NULL; |
| 4066 | return handle_alloc ( tid, nmemb*size1, VG_(clo_alignment), |
| 4067 | /*is_zeroed*/True ); |
| 4068 | } |
| 4069 | |
| 4070 | |
| 4071 | /* Free a client block, including getting rid of the relevant |
| 4072 | metadata. */ |
| 4073 | |
| 4074 | static void handle_free ( ThreadId tid, void* p ) |
| 4075 | { |
| 4076 | MallocMeta *md, *old_md; |
| 4077 | SizeT szB; |
| 4078 | |
| 4079 | /* First see if we can find the metadata for 'p'. */ |
| 4080 | md = (MallocMeta*) VG_(HT_lookup)( hg_mallocmeta_table, (UWord)p ); |
| 4081 | if (!md) |
| 4082 | return; /* apparently freeing a bogus address. Oh well. */ |
| 4083 | |
| 4084 | tl_assert(md->payload == (Addr)p); |
| 4085 | szB = md->szB; |
| 4086 | |
| 4087 | /* Nuke the metadata block */ |
| 4088 | old_md = (MallocMeta*) |
| 4089 | VG_(HT_remove)( hg_mallocmeta_table, (UWord)p ); |
| 4090 | tl_assert(old_md); /* it must be present - we just found it */ |
| 4091 | tl_assert(old_md == md); |
| 4092 | tl_assert(old_md->payload == (Addr)p); |
| 4093 | |
| 4094 | VG_(cli_free)((void*)old_md->payload); |
| 4095 | delete_MallocMeta(old_md); |
| 4096 | |
| 4097 | /* Tell the lower level memory wranglers. */ |
| 4098 | evh__die_mem_heap( (Addr)p, szB ); |
| 4099 | } |
| 4100 | |
| 4101 | static void hg_cli__free ( ThreadId tid, void* p ) { |
| 4102 | handle_free(tid, p); |
| 4103 | } |
| 4104 | static void hg_cli____builtin_delete ( ThreadId tid, void* p ) { |
| 4105 | handle_free(tid, p); |
| 4106 | } |
| 4107 | static void hg_cli____builtin_vec_delete ( ThreadId tid, void* p ) { |
| 4108 | handle_free(tid, p); |
| 4109 | } |
| 4110 | |
| 4111 | |
| 4112 | static void* hg_cli__realloc ( ThreadId tid, void* payloadV, SizeT new_size ) |
| 4113 | { |
| 4114 | MallocMeta *md, *md_new, *md_tmp; |
| 4115 | SizeT i; |
| 4116 | |
| 4117 | Addr payload = (Addr)payloadV; |
| 4118 | |
| 4119 | if (((SSizeT)new_size) < 0) return NULL; |
| 4120 | |
| 4121 | md = (MallocMeta*) VG_(HT_lookup)( hg_mallocmeta_table, (UWord)payload ); |
| 4122 | if (!md) |
| 4123 | return NULL; /* apparently realloc-ing a bogus address. Oh well. */ |
| 4124 | |
| 4125 | tl_assert(md->payload == payload); |
| 4126 | |
| 4127 | if (md->szB == new_size) { |
| 4128 | /* size unchanged */ |
| 4129 | md->where = VG_(record_ExeContext)(tid, 0); |
| 4130 | return payloadV; |
| 4131 | } |
| 4132 | |
| 4133 | if (md->szB > new_size) { |
| 4134 | /* new size is smaller */ |
| 4135 | md->szB = new_size; |
| 4136 | md->where = VG_(record_ExeContext)(tid, 0); |
| 4137 | evh__die_mem_heap( md->payload + new_size, md->szB - new_size ); |
| 4138 | return payloadV; |
| 4139 | } |
| 4140 | |
| 4141 | /* else */ { |
| 4142 | /* new size is bigger */ |
| 4143 | Addr p_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size); |
| 4144 | |
| 4145 | /* First half kept and copied, second half new */ |
| 4146 | // FIXME: shouldn't we use a copier which implements the |
| 4147 | // memory state machine? |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4148 | evh__copy_mem( payload, p_new, md->szB ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4149 | evh__new_mem_heap ( p_new + md->szB, new_size - md->szB, |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4150 | /*inited*/False ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4151 | /* FIXME: can anything funny happen here? specifically, if the |
| 4152 | old range contained a lock, then die_mem_heap will complain. |
| 4153 | Is that the correct behaviour? Not sure. */ |
| 4154 | evh__die_mem_heap( payload, md->szB ); |
| 4155 | |
| 4156 | /* Copy from old to new */ |
| 4157 | for (i = 0; i < md->szB; i++) |
| 4158 | ((UChar*)p_new)[i] = ((UChar*)payload)[i]; |
| 4159 | |
| 4160 | /* Because the metadata hash table is index by payload address, |
| 4161 | we have to get rid of the old hash table entry and make a new |
| 4162 | one. We can't just modify the existing metadata in place, |
| 4163 | because then it would (almost certainly) be in the wrong hash |
| 4164 | chain. */ |
| 4165 | md_new = new_MallocMeta(); |
| 4166 | *md_new = *md; |
| 4167 | |
| 4168 | md_tmp = VG_(HT_remove)( hg_mallocmeta_table, payload ); |
| 4169 | tl_assert(md_tmp); |
| 4170 | tl_assert(md_tmp == md); |
| 4171 | |
| 4172 | VG_(cli_free)((void*)md->payload); |
| 4173 | delete_MallocMeta(md); |
| 4174 | |
| 4175 | /* Update fields */ |
| 4176 | md_new->where = VG_(record_ExeContext)( tid, 0 ); |
| 4177 | md_new->szB = new_size; |
| 4178 | md_new->payload = p_new; |
| 4179 | md_new->thr = map_threads_lookup( tid ); |
| 4180 | |
| 4181 | /* and add */ |
| 4182 | VG_(HT_add_node)( hg_mallocmeta_table, (VgHashNode*)md_new ); |
| 4183 | |
| 4184 | return (void*)p_new; |
| 4185 | } |
| 4186 | } |
| 4187 | |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 4188 | static SizeT hg_cli_malloc_usable_size ( ThreadId tid, void* p ) |
| 4189 | { |
| 4190 | MallocMeta *md = VG_(HT_lookup)( hg_mallocmeta_table, (UWord)p ); |
| 4191 | |
| 4192 | // There may be slop, but pretend there isn't because only the asked-for |
| 4193 | // area will have been shadowed properly. |
| 4194 | return ( md ? md->szB : 0 ); |
| 4195 | } |
| 4196 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4197 | |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 4198 | /* For error creation: map 'data_addr' to a malloc'd chunk, if any. |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 4199 | Slow linear search. With a bit of hash table help if 'data_addr' |
| 4200 | is either the start of a block or up to 15 word-sized steps along |
| 4201 | from the start of a block. */ |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 4202 | |
| 4203 | static inline Bool addr_is_in_MM_Chunk( MallocMeta* mm, Addr a ) |
| 4204 | { |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 4205 | /* Accept 'a' as within 'mm' if 'mm's size is zero and 'a' points |
| 4206 | right at it. */ |
| 4207 | if (UNLIKELY(mm->szB == 0 && a == mm->payload)) |
| 4208 | return True; |
| 4209 | /* else normal interval rules apply */ |
| 4210 | if (LIKELY(a < mm->payload)) return False; |
| 4211 | if (LIKELY(a >= mm->payload + mm->szB)) return False; |
| 4212 | return True; |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 4215 | Bool HG_(mm_find_containing_block)( /*OUT*/ExeContext** where, |
philippe | 0c9ac8d | 2014-07-18 00:03:58 +0000 | [diff] [blame] | 4216 | /*OUT*/UInt* tnr, |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 4217 | /*OUT*/Addr* payload, |
| 4218 | /*OUT*/SizeT* szB, |
| 4219 | Addr data_addr ) |
| 4220 | { |
| 4221 | MallocMeta* mm; |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 4222 | Int i; |
| 4223 | const Int n_fast_check_words = 16; |
| 4224 | |
| 4225 | /* First, do a few fast searches on the basis that data_addr might |
| 4226 | be exactly the start of a block or up to 15 words inside. This |
| 4227 | can happen commonly via the creq |
| 4228 | _VG_USERREQ__HG_CLEAN_MEMORY_HEAPBLOCK. */ |
| 4229 | for (i = 0; i < n_fast_check_words; i++) { |
| 4230 | mm = VG_(HT_lookup)( hg_mallocmeta_table, |
| 4231 | data_addr - (UWord)(UInt)i * sizeof(UWord) ); |
| 4232 | if (UNLIKELY(mm && addr_is_in_MM_Chunk(mm, data_addr))) |
| 4233 | goto found; |
| 4234 | } |
| 4235 | |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 4236 | /* Well, this totally sucks. But without using an interval tree or |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 4237 | some such, it's hard to see how to do better. We have to check |
| 4238 | every block in the entire table. */ |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 4239 | VG_(HT_ResetIter)(hg_mallocmeta_table); |
| 4240 | while ( (mm = VG_(HT_Next)(hg_mallocmeta_table)) ) { |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 4241 | if (UNLIKELY(addr_is_in_MM_Chunk(mm, data_addr))) |
| 4242 | goto found; |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 4243 | } |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 4244 | |
| 4245 | /* Not found. Bah. */ |
| 4246 | return False; |
| 4247 | /*NOTREACHED*/ |
| 4248 | |
| 4249 | found: |
| 4250 | tl_assert(mm); |
| 4251 | tl_assert(addr_is_in_MM_Chunk(mm, data_addr)); |
| 4252 | if (where) *where = mm->where; |
philippe | 0c9ac8d | 2014-07-18 00:03:58 +0000 | [diff] [blame] | 4253 | if (tnr) *tnr = mm->thr->errmsg_index; |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 4254 | if (payload) *payload = mm->payload; |
| 4255 | if (szB) *szB = mm->szB; |
| 4256 | return True; |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4260 | /*--------------------------------------------------------------*/ |
| 4261 | /*--- Instrumentation ---*/ |
| 4262 | /*--------------------------------------------------------------*/ |
| 4263 | |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4264 | #define unop(_op, _arg1) IRExpr_Unop((_op),(_arg1)) |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4265 | #define binop(_op, _arg1, _arg2) IRExpr_Binop((_op),(_arg1),(_arg2)) |
| 4266 | #define mkexpr(_tmp) IRExpr_RdTmp((_tmp)) |
| 4267 | #define mkU32(_n) IRExpr_Const(IRConst_U32(_n)) |
| 4268 | #define mkU64(_n) IRExpr_Const(IRConst_U64(_n)) |
| 4269 | #define assign(_t, _e) IRStmt_WrTmp((_t), (_e)) |
| 4270 | |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4271 | /* This takes and returns atoms, of course. Not full IRExprs. */ |
| 4272 | static IRExpr* mk_And1 ( IRSB* sbOut, IRExpr* arg1, IRExpr* arg2 ) |
| 4273 | { |
| 4274 | tl_assert(arg1 && arg2); |
| 4275 | tl_assert(isIRAtom(arg1)); |
| 4276 | tl_assert(isIRAtom(arg2)); |
| 4277 | /* Generate 32to1(And32(1Uto32(arg1), 1Uto32(arg2))). Appalling |
| 4278 | code, I know. */ |
| 4279 | IRTemp wide1 = newIRTemp(sbOut->tyenv, Ity_I32); |
| 4280 | IRTemp wide2 = newIRTemp(sbOut->tyenv, Ity_I32); |
| 4281 | IRTemp anded = newIRTemp(sbOut->tyenv, Ity_I32); |
| 4282 | IRTemp res = newIRTemp(sbOut->tyenv, Ity_I1); |
| 4283 | addStmtToIRSB(sbOut, assign(wide1, unop(Iop_1Uto32, arg1))); |
| 4284 | addStmtToIRSB(sbOut, assign(wide2, unop(Iop_1Uto32, arg2))); |
| 4285 | addStmtToIRSB(sbOut, assign(anded, binop(Iop_And32, mkexpr(wide1), |
| 4286 | mkexpr(wide2)))); |
| 4287 | addStmtToIRSB(sbOut, assign(res, unop(Iop_32to1, mkexpr(anded)))); |
| 4288 | return mkexpr(res); |
| 4289 | } |
| 4290 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4291 | static void instrument_mem_access ( IRSB* sbOut, |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4292 | IRExpr* addr, |
| 4293 | Int szB, |
| 4294 | Bool isStore, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4295 | Int hWordTy_szB, |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4296 | Int goff_sp, |
| 4297 | IRExpr* guard ) /* NULL => True */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4298 | { |
| 4299 | IRType tyAddr = Ity_INVALID; |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 4300 | const HChar* hName = NULL; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4301 | void* hAddr = NULL; |
| 4302 | Int regparms = 0; |
| 4303 | IRExpr** argv = NULL; |
| 4304 | IRDirty* di = NULL; |
| 4305 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4306 | // THRESH is the size of the window above SP (well, |
| 4307 | // mostly above) that we assume implies a stack reference. |
| 4308 | const Int THRESH = 4096 * 4; // somewhat arbitrary |
| 4309 | const Int rz_szB = VG_STACK_REDZONE_SZB; |
| 4310 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4311 | tl_assert(isIRAtom(addr)); |
| 4312 | tl_assert(hWordTy_szB == 4 || hWordTy_szB == 8); |
| 4313 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4314 | tyAddr = typeOfIRExpr( sbOut->tyenv, addr ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4315 | tl_assert(tyAddr == Ity_I32 || tyAddr == Ity_I64); |
| 4316 | |
| 4317 | /* So the effective address is in 'addr' now. */ |
| 4318 | regparms = 1; // unless stated otherwise |
| 4319 | if (isStore) { |
| 4320 | switch (szB) { |
| 4321 | case 1: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4322 | hName = "evh__mem_help_cwrite_1"; |
| 4323 | hAddr = &evh__mem_help_cwrite_1; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4324 | argv = mkIRExprVec_1( addr ); |
| 4325 | break; |
| 4326 | case 2: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4327 | hName = "evh__mem_help_cwrite_2"; |
| 4328 | hAddr = &evh__mem_help_cwrite_2; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4329 | argv = mkIRExprVec_1( addr ); |
| 4330 | break; |
| 4331 | case 4: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4332 | hName = "evh__mem_help_cwrite_4"; |
| 4333 | hAddr = &evh__mem_help_cwrite_4; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4334 | argv = mkIRExprVec_1( addr ); |
| 4335 | break; |
| 4336 | case 8: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4337 | hName = "evh__mem_help_cwrite_8"; |
| 4338 | hAddr = &evh__mem_help_cwrite_8; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4339 | argv = mkIRExprVec_1( addr ); |
| 4340 | break; |
| 4341 | default: |
| 4342 | tl_assert(szB > 8 && szB <= 512); /* stay sane */ |
| 4343 | regparms = 2; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4344 | hName = "evh__mem_help_cwrite_N"; |
| 4345 | hAddr = &evh__mem_help_cwrite_N; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4346 | argv = mkIRExprVec_2( addr, mkIRExpr_HWord( szB )); |
| 4347 | break; |
| 4348 | } |
| 4349 | } else { |
| 4350 | switch (szB) { |
| 4351 | case 1: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4352 | hName = "evh__mem_help_cread_1"; |
| 4353 | hAddr = &evh__mem_help_cread_1; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4354 | argv = mkIRExprVec_1( addr ); |
| 4355 | break; |
| 4356 | case 2: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4357 | hName = "evh__mem_help_cread_2"; |
| 4358 | hAddr = &evh__mem_help_cread_2; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4359 | argv = mkIRExprVec_1( addr ); |
| 4360 | break; |
| 4361 | case 4: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4362 | hName = "evh__mem_help_cread_4"; |
| 4363 | hAddr = &evh__mem_help_cread_4; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4364 | argv = mkIRExprVec_1( addr ); |
| 4365 | break; |
| 4366 | case 8: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4367 | hName = "evh__mem_help_cread_8"; |
| 4368 | hAddr = &evh__mem_help_cread_8; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4369 | argv = mkIRExprVec_1( addr ); |
| 4370 | break; |
| 4371 | default: |
| 4372 | tl_assert(szB > 8 && szB <= 512); /* stay sane */ |
| 4373 | regparms = 2; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4374 | hName = "evh__mem_help_cread_N"; |
| 4375 | hAddr = &evh__mem_help_cread_N; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4376 | argv = mkIRExprVec_2( addr, mkIRExpr_HWord( szB )); |
| 4377 | break; |
| 4378 | } |
| 4379 | } |
| 4380 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4381 | /* Create the helper. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4382 | tl_assert(hName); |
| 4383 | tl_assert(hAddr); |
| 4384 | tl_assert(argv); |
| 4385 | di = unsafeIRDirty_0_N( regparms, |
| 4386 | hName, VG_(fnptr_to_fnentry)( hAddr ), |
| 4387 | argv ); |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4388 | |
| 4389 | if (! HG_(clo_check_stack_refs)) { |
| 4390 | /* We're ignoring memory references which are (obviously) to the |
| 4391 | stack. In fact just skip stack refs that are within 4 pages |
| 4392 | of SP (SP - the redzone, really), as that's simple, easy, and |
| 4393 | filters out most stack references. */ |
| 4394 | /* Generate the guard condition: "(addr - (SP - RZ)) >u N", for |
| 4395 | some arbitrary N. If that is true then addr is outside the |
| 4396 | range (SP - RZ .. SP + N - RZ). If N is smallish (a few |
| 4397 | pages) then we can say addr is within a few pages of SP and |
| 4398 | so can't possibly be a heap access, and so can be skipped. |
| 4399 | |
| 4400 | Note that the condition simplifies to |
| 4401 | (addr - SP + RZ) >u N |
| 4402 | which generates better code in x86/amd64 backends, but it does |
| 4403 | not unfortunately simplify to |
| 4404 | (addr - SP) >u (N - RZ) |
| 4405 | (would be beneficial because N - RZ is a constant) because |
| 4406 | wraparound arithmetic messes up the comparison. eg. |
| 4407 | 20 >u 10 == True, |
| 4408 | but (20 - 15) >u (10 - 15) == 5 >u (MAXINT-5) == False. |
| 4409 | */ |
| 4410 | IRTemp sp = newIRTemp(sbOut->tyenv, tyAddr); |
| 4411 | addStmtToIRSB( sbOut, assign(sp, IRExpr_Get(goff_sp, tyAddr))); |
| 4412 | |
| 4413 | /* "addr - SP" */ |
| 4414 | IRTemp addr_minus_sp = newIRTemp(sbOut->tyenv, tyAddr); |
| 4415 | addStmtToIRSB( |
| 4416 | sbOut, |
| 4417 | assign(addr_minus_sp, |
| 4418 | tyAddr == Ity_I32 |
| 4419 | ? binop(Iop_Sub32, addr, mkexpr(sp)) |
| 4420 | : binop(Iop_Sub64, addr, mkexpr(sp))) |
| 4421 | ); |
| 4422 | |
| 4423 | /* "addr - SP + RZ" */ |
| 4424 | IRTemp diff = newIRTemp(sbOut->tyenv, tyAddr); |
| 4425 | addStmtToIRSB( |
| 4426 | sbOut, |
| 4427 | assign(diff, |
| 4428 | tyAddr == Ity_I32 |
| 4429 | ? binop(Iop_Add32, mkexpr(addr_minus_sp), mkU32(rz_szB)) |
| 4430 | : binop(Iop_Add64, mkexpr(addr_minus_sp), mkU64(rz_szB))) |
| 4431 | ); |
| 4432 | |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4433 | /* guardA == "guard on the address" */ |
| 4434 | IRTemp guardA = newIRTemp(sbOut->tyenv, Ity_I1); |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4435 | addStmtToIRSB( |
| 4436 | sbOut, |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4437 | assign(guardA, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4438 | tyAddr == Ity_I32 |
| 4439 | ? binop(Iop_CmpLT32U, mkU32(THRESH), mkexpr(diff)) |
| 4440 | : binop(Iop_CmpLT64U, mkU64(THRESH), mkexpr(diff))) |
| 4441 | ); |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4442 | di->guard = mkexpr(guardA); |
| 4443 | } |
| 4444 | |
| 4445 | /* If there's a guard on the access itself (as supplied by the |
| 4446 | caller of this routine), we need to AND that in to any guard we |
| 4447 | might already have. */ |
| 4448 | if (guard) { |
| 4449 | di->guard = mk_And1(sbOut, di->guard, guard); |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | /* Add the helper. */ |
| 4453 | addStmtToIRSB( sbOut, IRStmt_Dirty(di) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4457 | /* Figure out if GA is a guest code address in the dynamic linker, and |
| 4458 | if so return True. Otherwise (and in case of any doubt) return |
| 4459 | False. (sidedly safe w/ False as the safe value) */ |
| 4460 | static Bool is_in_dynamic_linker_shared_object( Addr64 ga ) |
| 4461 | { |
| 4462 | DebugInfo* dinfo; |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 4463 | const HChar* soname; |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4464 | if (0) return False; |
| 4465 | |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 4466 | dinfo = VG_(find_DebugInfo)( (Addr)ga ); |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4467 | if (!dinfo) return False; |
| 4468 | |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 4469 | soname = VG_(DebugInfo_get_soname)(dinfo); |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4470 | tl_assert(soname); |
| 4471 | if (0) VG_(printf)("%s\n", soname); |
| 4472 | |
| 4473 | # if defined(VGO_linux) |
sewardj | 651cfa4 | 2010-01-11 13:02:19 +0000 | [diff] [blame] | 4474 | if (VG_STREQ(soname, VG_U_LD_LINUX_SO_3)) return True; |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4475 | if (VG_STREQ(soname, VG_U_LD_LINUX_SO_2)) return True; |
| 4476 | if (VG_STREQ(soname, VG_U_LD_LINUX_X86_64_SO_2)) return True; |
| 4477 | if (VG_STREQ(soname, VG_U_LD64_SO_1)) return True; |
carll | 582d582 | 2014-08-07 23:35:54 +0000 | [diff] [blame^] | 4478 | if (VG_STREQ(soname, VG_U_LD64_SO_2)) return True; |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4479 | if (VG_STREQ(soname, VG_U_LD_SO_1)) return True; |
| 4480 | # elif defined(VGO_darwin) |
| 4481 | if (VG_STREQ(soname, VG_U_DYLD)) return True; |
| 4482 | # else |
| 4483 | # error "Unsupported OS" |
| 4484 | # endif |
| 4485 | return False; |
| 4486 | } |
| 4487 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4488 | static |
| 4489 | IRSB* hg_instrument ( VgCallbackClosure* closure, |
| 4490 | IRSB* bbIn, |
| 4491 | VexGuestLayout* layout, |
| 4492 | VexGuestExtents* vge, |
florian | ca503be | 2012-10-07 21:59:42 +0000 | [diff] [blame] | 4493 | VexArchInfo* archinfo_host, |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4494 | IRType gWordTy, IRType hWordTy ) |
| 4495 | { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4496 | Int i; |
| 4497 | IRSB* bbOut; |
| 4498 | Addr64 cia; /* address of current insn */ |
| 4499 | IRStmt* st; |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4500 | Bool inLDSO = False; |
| 4501 | Addr64 inLDSOmask4K = 1; /* mismatches on first check */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4502 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4503 | const Int goff_sp = layout->offset_SP; |
| 4504 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4505 | if (gWordTy != hWordTy) { |
| 4506 | /* We don't currently support this case. */ |
| 4507 | VG_(tool_panic)("host/guest word size mismatch"); |
| 4508 | } |
| 4509 | |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4510 | if (VKI_PAGE_SIZE < 4096 || VG_(log2)(VKI_PAGE_SIZE) == -1) { |
| 4511 | VG_(tool_panic)("implausible or too-small VKI_PAGE_SIZE"); |
| 4512 | } |
| 4513 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4514 | /* Set up BB */ |
| 4515 | bbOut = emptyIRSB(); |
| 4516 | bbOut->tyenv = deepCopyIRTypeEnv(bbIn->tyenv); |
| 4517 | bbOut->next = deepCopyIRExpr(bbIn->next); |
| 4518 | bbOut->jumpkind = bbIn->jumpkind; |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 4519 | bbOut->offsIP = bbIn->offsIP; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4520 | |
| 4521 | // Copy verbatim any IR preamble preceding the first IMark |
| 4522 | i = 0; |
| 4523 | while (i < bbIn->stmts_used && bbIn->stmts[i]->tag != Ist_IMark) { |
| 4524 | addStmtToIRSB( bbOut, bbIn->stmts[i] ); |
| 4525 | i++; |
| 4526 | } |
| 4527 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4528 | // Get the first statement, and initial cia from it |
| 4529 | tl_assert(bbIn->stmts_used > 0); |
| 4530 | tl_assert(i < bbIn->stmts_used); |
| 4531 | st = bbIn->stmts[i]; |
| 4532 | tl_assert(Ist_IMark == st->tag); |
| 4533 | cia = st->Ist.IMark.addr; |
| 4534 | st = NULL; |
| 4535 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4536 | for (/*use current i*/; i < bbIn->stmts_used; i++) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4537 | st = bbIn->stmts[i]; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4538 | tl_assert(st); |
| 4539 | tl_assert(isFlatIRStmt(st)); |
| 4540 | switch (st->tag) { |
| 4541 | case Ist_NoOp: |
| 4542 | case Ist_AbiHint: |
| 4543 | case Ist_Put: |
| 4544 | case Ist_PutI: |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4545 | case Ist_Exit: |
| 4546 | /* None of these can contain any memory references. */ |
| 4547 | break; |
| 4548 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4549 | case Ist_IMark: |
| 4550 | /* no mem refs, but note the insn address. */ |
| 4551 | cia = st->Ist.IMark.addr; |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4552 | /* Don't instrument the dynamic linker. It generates a |
| 4553 | lot of races which we just expensively suppress, so |
| 4554 | it's pointless. |
| 4555 | |
| 4556 | Avoid flooding is_in_dynamic_linker_shared_object with |
| 4557 | requests by only checking at transitions between 4K |
| 4558 | pages. */ |
| 4559 | if ((cia & ~(Addr64)0xFFF) != inLDSOmask4K) { |
| 4560 | if (0) VG_(printf)("NEW %#lx\n", (Addr)cia); |
| 4561 | inLDSOmask4K = cia & ~(Addr64)0xFFF; |
| 4562 | inLDSO = is_in_dynamic_linker_shared_object(cia); |
| 4563 | } else { |
| 4564 | if (0) VG_(printf)("old %#lx\n", (Addr)cia); |
| 4565 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4566 | break; |
| 4567 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4568 | case Ist_MBE: |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4569 | switch (st->Ist.MBE.event) { |
| 4570 | case Imbe_Fence: |
| 4571 | break; /* not interesting */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4572 | default: |
| 4573 | goto unhandled; |
| 4574 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4575 | break; |
| 4576 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4577 | case Ist_CAS: { |
| 4578 | /* Atomic read-modify-write cycle. Just pretend it's a |
| 4579 | read. */ |
| 4580 | IRCAS* cas = st->Ist.CAS.details; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4581 | Bool isDCAS = cas->oldHi != IRTemp_INVALID; |
| 4582 | if (isDCAS) { |
| 4583 | tl_assert(cas->expdHi); |
| 4584 | tl_assert(cas->dataHi); |
| 4585 | } else { |
| 4586 | tl_assert(!cas->expdHi); |
| 4587 | tl_assert(!cas->dataHi); |
| 4588 | } |
| 4589 | /* Just be boring about it. */ |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4590 | if (!inLDSO) { |
| 4591 | instrument_mem_access( |
| 4592 | bbOut, |
| 4593 | cas->addr, |
| 4594 | (isDCAS ? 2 : 1) |
| 4595 | * sizeofIRType(typeOfIRExpr(bbIn->tyenv, cas->dataLo)), |
| 4596 | False/*!isStore*/, |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4597 | sizeofIRType(hWordTy), goff_sp, |
| 4598 | NULL/*no-guard*/ |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4599 | ); |
| 4600 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4601 | break; |
| 4602 | } |
| 4603 | |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 4604 | case Ist_LLSC: { |
| 4605 | /* We pretend store-conditionals don't exist, viz, ignore |
| 4606 | them. Whereas load-linked's are treated the same as |
| 4607 | normal loads. */ |
| 4608 | IRType dataTy; |
| 4609 | if (st->Ist.LLSC.storedata == NULL) { |
| 4610 | /* LL */ |
| 4611 | dataTy = typeOfIRTemp(bbIn->tyenv, st->Ist.LLSC.result); |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4612 | if (!inLDSO) { |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 4613 | instrument_mem_access( |
| 4614 | bbOut, |
| 4615 | st->Ist.LLSC.addr, |
| 4616 | sizeofIRType(dataTy), |
| 4617 | False/*!isStore*/, |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4618 | sizeofIRType(hWordTy), goff_sp, |
| 4619 | NULL/*no-guard*/ |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4620 | ); |
| 4621 | } |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 4622 | } else { |
| 4623 | /* SC */ |
| 4624 | /*ignore */ |
| 4625 | } |
| 4626 | break; |
| 4627 | } |
| 4628 | |
| 4629 | case Ist_Store: |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 4630 | if (!inLDSO) { |
| 4631 | instrument_mem_access( |
| 4632 | bbOut, |
| 4633 | st->Ist.Store.addr, |
| 4634 | sizeofIRType(typeOfIRExpr(bbIn->tyenv, st->Ist.Store.data)), |
| 4635 | True/*isStore*/, |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4636 | sizeofIRType(hWordTy), goff_sp, |
| 4637 | NULL/*no-guard*/ |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 4638 | ); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4639 | } |
njn | b83caf2 | 2009-05-25 01:47:56 +0000 | [diff] [blame] | 4640 | break; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4641 | |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4642 | case Ist_StoreG: { |
| 4643 | IRStoreG* sg = st->Ist.StoreG.details; |
| 4644 | IRExpr* data = sg->data; |
| 4645 | IRExpr* addr = sg->addr; |
| 4646 | IRType type = typeOfIRExpr(bbIn->tyenv, data); |
| 4647 | tl_assert(type != Ity_INVALID); |
| 4648 | instrument_mem_access( bbOut, addr, sizeofIRType(type), |
| 4649 | True/*isStore*/, |
| 4650 | sizeofIRType(hWordTy), |
| 4651 | goff_sp, sg->guard ); |
| 4652 | break; |
| 4653 | } |
| 4654 | |
| 4655 | case Ist_LoadG: { |
| 4656 | IRLoadG* lg = st->Ist.LoadG.details; |
| 4657 | IRType type = Ity_INVALID; /* loaded type */ |
| 4658 | IRType typeWide = Ity_INVALID; /* after implicit widening */ |
| 4659 | IRExpr* addr = lg->addr; |
| 4660 | typeOfIRLoadGOp(lg->cvt, &typeWide, &type); |
| 4661 | tl_assert(type != Ity_INVALID); |
| 4662 | instrument_mem_access( bbOut, addr, sizeofIRType(type), |
| 4663 | False/*!isStore*/, |
| 4664 | sizeofIRType(hWordTy), |
| 4665 | goff_sp, lg->guard ); |
| 4666 | break; |
| 4667 | } |
| 4668 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4669 | case Ist_WrTmp: { |
| 4670 | IRExpr* data = st->Ist.WrTmp.data; |
| 4671 | if (data->tag == Iex_Load) { |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4672 | if (!inLDSO) { |
| 4673 | instrument_mem_access( |
| 4674 | bbOut, |
| 4675 | data->Iex.Load.addr, |
| 4676 | sizeofIRType(data->Iex.Load.ty), |
| 4677 | False/*!isStore*/, |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4678 | sizeofIRType(hWordTy), goff_sp, |
| 4679 | NULL/*no-guard*/ |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4680 | ); |
| 4681 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4682 | } |
| 4683 | break; |
| 4684 | } |
| 4685 | |
| 4686 | case Ist_Dirty: { |
| 4687 | Int dataSize; |
| 4688 | IRDirty* d = st->Ist.Dirty.details; |
| 4689 | if (d->mFx != Ifx_None) { |
| 4690 | /* This dirty helper accesses memory. Collect the |
| 4691 | details. */ |
| 4692 | tl_assert(d->mAddr != NULL); |
| 4693 | tl_assert(d->mSize != 0); |
| 4694 | dataSize = d->mSize; |
| 4695 | if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify) { |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4696 | if (!inLDSO) { |
| 4697 | instrument_mem_access( |
| 4698 | bbOut, d->mAddr, dataSize, False/*!isStore*/, |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4699 | sizeofIRType(hWordTy), goff_sp, NULL/*no-guard*/ |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4700 | ); |
| 4701 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4702 | } |
| 4703 | if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify) { |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4704 | if (!inLDSO) { |
| 4705 | instrument_mem_access( |
| 4706 | bbOut, d->mAddr, dataSize, True/*isStore*/, |
sewardj | cafe505 | 2013-01-17 14:24:35 +0000 | [diff] [blame] | 4707 | sizeofIRType(hWordTy), goff_sp, NULL/*no-guard*/ |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4708 | ); |
| 4709 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4710 | } |
| 4711 | } else { |
| 4712 | tl_assert(d->mAddr == NULL); |
| 4713 | tl_assert(d->mSize == 0); |
| 4714 | } |
| 4715 | break; |
| 4716 | } |
| 4717 | |
| 4718 | default: |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4719 | unhandled: |
| 4720 | ppIRStmt(st); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4721 | tl_assert(0); |
| 4722 | |
| 4723 | } /* switch (st->tag) */ |
| 4724 | |
| 4725 | addStmtToIRSB( bbOut, st ); |
| 4726 | } /* iterate over bbIn->stmts */ |
| 4727 | |
| 4728 | return bbOut; |
| 4729 | } |
| 4730 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 4731 | #undef binop |
| 4732 | #undef mkexpr |
| 4733 | #undef mkU32 |
| 4734 | #undef mkU64 |
| 4735 | #undef assign |
| 4736 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4737 | |
| 4738 | /*----------------------------------------------------------------*/ |
| 4739 | /*--- Client requests ---*/ |
| 4740 | /*----------------------------------------------------------------*/ |
| 4741 | |
| 4742 | /* Sheesh. Yet another goddam finite map. */ |
| 4743 | static WordFM* map_pthread_t_to_Thread = NULL; /* pthread_t -> Thread* */ |
| 4744 | |
| 4745 | static void map_pthread_t_to_Thread_INIT ( void ) { |
| 4746 | if (UNLIKELY(map_pthread_t_to_Thread == NULL)) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4747 | map_pthread_t_to_Thread = VG_(newFM)( HG_(zalloc), "hg.mpttT.1", |
| 4748 | HG_(free), NULL ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4749 | tl_assert(map_pthread_t_to_Thread != NULL); |
| 4750 | } |
| 4751 | } |
| 4752 | |
philippe | d40aff5 | 2014-06-16 20:00:14 +0000 | [diff] [blame] | 4753 | /* A list of Ada dependent tasks and their masters. Used for implementing |
| 4754 | the Ada task termination semantic as implemented by the |
| 4755 | gcc gnat Ada runtime. */ |
| 4756 | typedef |
| 4757 | struct { |
| 4758 | void* dependent; // Ada Task Control Block of the Dependent |
| 4759 | void* master; // ATCB of the master |
| 4760 | Word master_level; // level of dependency between master and dependent |
| 4761 | Thread* hg_dependent; // helgrind Thread* for dependent task. |
| 4762 | } |
| 4763 | GNAT_dmml; |
| 4764 | static XArray* gnat_dmmls; /* of GNAT_dmml */ |
| 4765 | static void gnat_dmmls_INIT (void) |
| 4766 | { |
| 4767 | if (UNLIKELY(gnat_dmmls == NULL)) { |
| 4768 | gnat_dmmls = VG_(newXA) (HG_(zalloc), "hg.gnat_md.1", |
| 4769 | HG_(free), |
| 4770 | sizeof(GNAT_dmml) ); |
| 4771 | } |
| 4772 | } |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 4773 | static void print_monitor_help ( void ) |
| 4774 | { |
| 4775 | VG_(gdb_printf) |
| 4776 | ( |
| 4777 | "\n" |
| 4778 | "helgrind monitor commands:\n" |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 4779 | " info locks : show list of locks and their status\n" |
| 4780 | "\n"); |
| 4781 | } |
| 4782 | |
| 4783 | /* return True if request recognised, False otherwise */ |
| 4784 | static Bool handle_gdb_monitor_command (ThreadId tid, HChar *req) |
| 4785 | { |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 4786 | HChar* wcmd; |
| 4787 | HChar s[VG_(strlen(req))]; /* copy for strtok_r */ |
| 4788 | HChar *ssaveptr; |
| 4789 | Int kwdid; |
| 4790 | |
| 4791 | VG_(strcpy) (s, req); |
| 4792 | |
| 4793 | wcmd = VG_(strtok_r) (s, " ", &ssaveptr); |
| 4794 | /* NB: if possible, avoid introducing a new command below which |
| 4795 | starts with the same first letter(s) as an already existing |
| 4796 | command. This ensures a shorter abbreviation for the user. */ |
| 4797 | switch (VG_(keyword_id) |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 4798 | ("help info", |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 4799 | wcmd, kwd_report_duplicated_matches)) { |
| 4800 | case -2: /* multiple matches */ |
| 4801 | return True; |
| 4802 | case -1: /* not found */ |
| 4803 | return False; |
| 4804 | case 0: /* help */ |
| 4805 | print_monitor_help(); |
| 4806 | return True; |
| 4807 | case 1: /* info */ |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 4808 | wcmd = VG_(strtok_r) (NULL, " ", &ssaveptr); |
| 4809 | switch (kwdid = VG_(keyword_id) |
| 4810 | ("locks", |
| 4811 | wcmd, kwd_report_all)) { |
| 4812 | case -2: |
| 4813 | case -1: |
| 4814 | break; |
| 4815 | case 0: // locks |
| 4816 | { |
| 4817 | Int i; |
| 4818 | Lock* lk; |
| 4819 | for (i = 0, lk = admin_locks; lk; i++, lk = lk->admin_next) { |
| 4820 | pp_Lock(0, lk, |
| 4821 | True /* show_lock_addrdescr */, |
| 4822 | False /* show_internal_data */); |
| 4823 | } |
| 4824 | if (i == 0) |
| 4825 | VG_(gdb_printf) ("no locks\n"); |
| 4826 | } |
| 4827 | break; |
| 4828 | default: |
| 4829 | tl_assert(0); |
| 4830 | } |
| 4831 | return True; |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 4832 | default: |
| 4833 | tl_assert(0); |
| 4834 | return False; |
| 4835 | } |
| 4836 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4837 | |
| 4838 | static |
| 4839 | Bool hg_handle_client_request ( ThreadId tid, UWord* args, UWord* ret) |
| 4840 | { |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 4841 | if (!VG_IS_TOOL_USERREQ('H','G',args[0]) |
| 4842 | && VG_USERREQ__GDB_MONITOR_COMMAND != args[0]) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4843 | return False; |
| 4844 | |
| 4845 | /* Anything that gets past the above check is one of ours, so we |
| 4846 | should be able to handle it. */ |
| 4847 | |
| 4848 | /* default, meaningless return value, unless otherwise set */ |
| 4849 | *ret = 0; |
| 4850 | |
| 4851 | switch (args[0]) { |
| 4852 | |
| 4853 | /* --- --- User-visible client requests --- --- */ |
| 4854 | |
| 4855 | case VG_USERREQ__HG_CLEAN_MEMORY: |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4856 | if (0) VG_(printf)("VG_USERREQ__HG_CLEAN_MEMORY(%#lx,%ld)\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4857 | args[1], args[2]); |
| 4858 | /* Call die_mem to (expensively) tidy up properly, if there |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4859 | are any held locks etc in the area. Calling evh__die_mem |
| 4860 | and then evh__new_mem is a bit inefficient; probably just |
| 4861 | the latter would do. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4862 | if (args[2] > 0) { /* length */ |
| 4863 | evh__die_mem(args[1], args[2]); |
| 4864 | /* and then set it to New */ |
| 4865 | evh__new_mem(args[1], args[2]); |
| 4866 | } |
| 4867 | break; |
| 4868 | |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 4869 | case _VG_USERREQ__HG_CLEAN_MEMORY_HEAPBLOCK: { |
| 4870 | Addr payload = 0; |
| 4871 | SizeT pszB = 0; |
| 4872 | if (0) VG_(printf)("VG_USERREQ__HG_CLEAN_MEMORY_HEAPBLOCK(%#lx)\n", |
| 4873 | args[1]); |
philippe | 0c9ac8d | 2014-07-18 00:03:58 +0000 | [diff] [blame] | 4874 | if (HG_(mm_find_containing_block)(NULL, NULL, |
| 4875 | &payload, &pszB, args[1])) { |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 4876 | if (pszB > 0) { |
| 4877 | evh__die_mem(payload, pszB); |
| 4878 | evh__new_mem(payload, pszB); |
| 4879 | } |
| 4880 | *ret = pszB; |
| 4881 | } else { |
| 4882 | *ret = (UWord)-1; |
| 4883 | } |
| 4884 | break; |
| 4885 | } |
| 4886 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 4887 | case _VG_USERREQ__HG_ARANGE_MAKE_UNTRACKED: |
| 4888 | if (0) VG_(printf)("HG_ARANGE_MAKE_UNTRACKED(%#lx,%ld)\n", |
| 4889 | args[1], args[2]); |
| 4890 | if (args[2] > 0) { /* length */ |
| 4891 | evh__untrack_mem(args[1], args[2]); |
| 4892 | } |
| 4893 | break; |
| 4894 | |
| 4895 | case _VG_USERREQ__HG_ARANGE_MAKE_TRACKED: |
| 4896 | if (0) VG_(printf)("HG_ARANGE_MAKE_TRACKED(%#lx,%ld)\n", |
| 4897 | args[1], args[2]); |
| 4898 | if (args[2] > 0) { /* length */ |
| 4899 | evh__new_mem(args[1], args[2]); |
| 4900 | } |
| 4901 | break; |
| 4902 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4903 | /* --- --- Client requests for Helgrind's use only --- --- */ |
| 4904 | |
| 4905 | /* Some thread is telling us its pthread_t value. Record the |
| 4906 | binding between that and the associated Thread*, so we can |
| 4907 | later find the Thread* again when notified of a join by the |
| 4908 | thread. */ |
| 4909 | case _VG_USERREQ__HG_SET_MY_PTHREAD_T: { |
| 4910 | Thread* my_thr = NULL; |
| 4911 | if (0) |
| 4912 | VG_(printf)("SET_MY_PTHREAD_T (tid %d): pthread_t = %p\n", (Int)tid, |
| 4913 | (void*)args[1]); |
| 4914 | map_pthread_t_to_Thread_INIT(); |
| 4915 | my_thr = map_threads_maybe_lookup( tid ); |
| 4916 | /* This assertion should hold because the map_threads (tid to |
| 4917 | Thread*) binding should have been made at the point of |
| 4918 | low-level creation of this thread, which should have |
| 4919 | happened prior to us getting this client request for it. |
| 4920 | That's because this client request is sent from |
| 4921 | client-world from the 'thread_wrapper' function, which |
| 4922 | only runs once the thread has been low-level created. */ |
| 4923 | tl_assert(my_thr != NULL); |
| 4924 | /* So now we know that (pthread_t)args[1] is associated with |
| 4925 | (Thread*)my_thr. Note that down. */ |
| 4926 | if (0) |
| 4927 | VG_(printf)("XXXX: bind pthread_t %p to Thread* %p\n", |
| 4928 | (void*)args[1], (void*)my_thr ); |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 4929 | VG_(addToFM)( map_pthread_t_to_Thread, (UWord)args[1], (UWord)my_thr ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4930 | break; |
| 4931 | } |
| 4932 | |
| 4933 | case _VG_USERREQ__HG_PTH_API_ERROR: { |
| 4934 | Thread* my_thr = NULL; |
| 4935 | map_pthread_t_to_Thread_INIT(); |
| 4936 | my_thr = map_threads_maybe_lookup( tid ); |
| 4937 | tl_assert(my_thr); /* See justification above in SET_MY_PTHREAD_T */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4938 | HG_(record_error_PthAPIerror)( |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 4939 | my_thr, (HChar*)args[1], (UWord)args[2], (HChar*)args[3] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4940 | break; |
| 4941 | } |
| 4942 | |
| 4943 | /* This thread (tid) has completed a join with the quitting |
| 4944 | thread whose pthread_t is in args[1]. */ |
| 4945 | case _VG_USERREQ__HG_PTHREAD_JOIN_POST: { |
| 4946 | Thread* thr_q = NULL; /* quitter Thread* */ |
| 4947 | Bool found = False; |
| 4948 | if (0) |
| 4949 | VG_(printf)("NOTIFY_JOIN_COMPLETE (tid %d): quitter = %p\n", (Int)tid, |
| 4950 | (void*)args[1]); |
| 4951 | map_pthread_t_to_Thread_INIT(); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 4952 | found = VG_(lookupFM)( map_pthread_t_to_Thread, |
florian | 6bf3726 | 2012-10-21 03:23:36 +0000 | [diff] [blame] | 4953 | NULL, (UWord*)&thr_q, (UWord)args[1] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4954 | /* Can this fail? It would mean that our pthread_join |
| 4955 | wrapper observed a successful join on args[1] yet that |
| 4956 | thread never existed (or at least, it never lodged an |
| 4957 | entry in the mapping (via SET_MY_PTHREAD_T)). Which |
| 4958 | sounds like a bug in the threads library. */ |
| 4959 | // FIXME: get rid of this assertion; handle properly |
| 4960 | tl_assert(found); |
| 4961 | if (found) { |
| 4962 | if (0) |
| 4963 | VG_(printf)(".................... quitter Thread* = %p\n", |
| 4964 | thr_q); |
| 4965 | evh__HG_PTHREAD_JOIN_POST( tid, thr_q ); |
| 4966 | } |
| 4967 | break; |
| 4968 | } |
| 4969 | |
philippe | d40aff5 | 2014-06-16 20:00:14 +0000 | [diff] [blame] | 4970 | /* This thread (tid) is informing us of its master. */ |
| 4971 | case _VG_USERREQ__HG_GNAT_MASTER_HOOK: { |
| 4972 | GNAT_dmml dmml; |
| 4973 | dmml.dependent = (void*)args[1]; |
| 4974 | dmml.master = (void*)args[2]; |
| 4975 | dmml.master_level = (Word)args[3]; |
| 4976 | dmml.hg_dependent = map_threads_maybe_lookup( tid ); |
| 4977 | tl_assert(dmml.hg_dependent); |
| 4978 | |
| 4979 | if (0) |
| 4980 | VG_(printf)("HG_GNAT_MASTER_HOOK (tid %d): " |
| 4981 | "dependent = %p master = %p master_level = %ld" |
| 4982 | " dependent Thread* = %p\n", |
| 4983 | (Int)tid, dmml.dependent, dmml.master, dmml.master_level, |
| 4984 | dmml.hg_dependent); |
| 4985 | gnat_dmmls_INIT(); |
| 4986 | VG_(addToXA) (gnat_dmmls, &dmml); |
| 4987 | break; |
| 4988 | } |
| 4989 | |
| 4990 | /* This thread (tid) is informing us that it has completed a |
| 4991 | master. */ |
| 4992 | case _VG_USERREQ__HG_GNAT_MASTER_COMPLETED_HOOK: { |
| 4993 | Word n; |
| 4994 | const Thread *stayer = map_threads_maybe_lookup( tid ); |
| 4995 | const void *master = (void*)args[1]; |
| 4996 | const Word master_level = (Word) args[2]; |
| 4997 | tl_assert(stayer); |
| 4998 | |
| 4999 | if (0) |
| 5000 | VG_(printf)("HG_GNAT_MASTER_COMPLETED_HOOK (tid %d): " |
| 5001 | "self_id = %p master_level = %ld Thread* = %p\n", |
| 5002 | (Int)tid, master, master_level, stayer); |
| 5003 | |
| 5004 | gnat_dmmls_INIT(); |
| 5005 | /* Reverse loop on the array, simulating a pthread_join for |
| 5006 | the Dependent tasks of the completed master, and removing |
| 5007 | them from the array. */ |
| 5008 | for (n = VG_(sizeXA) (gnat_dmmls) - 1; n >= 0; n--) { |
| 5009 | GNAT_dmml *dmml = (GNAT_dmml*) VG_(indexXA)(gnat_dmmls, n); |
| 5010 | if (dmml->master == master |
| 5011 | && dmml->master_level == master_level) { |
| 5012 | if (0) |
| 5013 | VG_(printf)("quitter %p dependency to stayer %p\n", |
| 5014 | dmml->hg_dependent->hbthr, stayer->hbthr); |
| 5015 | tl_assert(dmml->hg_dependent->hbthr != stayer->hbthr); |
| 5016 | generate_quitter_stayer_dependence (dmml->hg_dependent->hbthr, |
| 5017 | stayer->hbthr); |
| 5018 | VG_(removeIndexXA) (gnat_dmmls, n); |
| 5019 | } |
| 5020 | } |
| 5021 | break; |
| 5022 | } |
| 5023 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5024 | /* EXPOSITION only: by intercepting lock init events we can show |
| 5025 | the user where the lock was initialised, rather than only |
| 5026 | being able to show where it was first locked. Intercepting |
| 5027 | lock initialisations is not necessary for the basic operation |
| 5028 | of the race checker. */ |
| 5029 | case _VG_USERREQ__HG_PTHREAD_MUTEX_INIT_POST: |
| 5030 | evh__HG_PTHREAD_MUTEX_INIT_POST( tid, (void*)args[1], args[2] ); |
| 5031 | break; |
| 5032 | |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 5033 | /* mutex=arg[1], mutex_is_init=arg[2] */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5034 | case _VG_USERREQ__HG_PTHREAD_MUTEX_DESTROY_PRE: |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 5035 | evh__HG_PTHREAD_MUTEX_DESTROY_PRE( tid, (void*)args[1], args[2] != 0 ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5036 | break; |
| 5037 | |
| 5038 | case _VG_USERREQ__HG_PTHREAD_MUTEX_UNLOCK_PRE: // pth_mx_t* |
| 5039 | evh__HG_PTHREAD_MUTEX_UNLOCK_PRE( tid, (void*)args[1] ); |
| 5040 | break; |
| 5041 | |
| 5042 | case _VG_USERREQ__HG_PTHREAD_MUTEX_UNLOCK_POST: // pth_mx_t* |
| 5043 | evh__HG_PTHREAD_MUTEX_UNLOCK_POST( tid, (void*)args[1] ); |
| 5044 | break; |
| 5045 | |
| 5046 | case _VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_PRE: // pth_mx_t*, Word |
| 5047 | evh__HG_PTHREAD_MUTEX_LOCK_PRE( tid, (void*)args[1], args[2] ); |
| 5048 | break; |
| 5049 | |
| 5050 | case _VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_POST: // pth_mx_t* |
| 5051 | evh__HG_PTHREAD_MUTEX_LOCK_POST( tid, (void*)args[1] ); |
| 5052 | break; |
| 5053 | |
| 5054 | /* This thread is about to do pthread_cond_signal on the |
| 5055 | pthread_cond_t* in arg[1]. Ditto pthread_cond_broadcast. */ |
| 5056 | case _VG_USERREQ__HG_PTHREAD_COND_SIGNAL_PRE: |
| 5057 | case _VG_USERREQ__HG_PTHREAD_COND_BROADCAST_PRE: |
| 5058 | evh__HG_PTHREAD_COND_SIGNAL_PRE( tid, (void*)args[1] ); |
| 5059 | break; |
| 5060 | |
| 5061 | /* Entry into pthread_cond_wait, cond=arg[1], mutex=arg[2]. |
| 5062 | Returns a flag indicating whether or not the mutex is believed to be |
| 5063 | valid for this operation. */ |
| 5064 | case _VG_USERREQ__HG_PTHREAD_COND_WAIT_PRE: { |
| 5065 | Bool mutex_is_valid |
| 5066 | = evh__HG_PTHREAD_COND_WAIT_PRE( tid, (void*)args[1], |
| 5067 | (void*)args[2] ); |
| 5068 | *ret = mutex_is_valid ? 1 : 0; |
| 5069 | break; |
| 5070 | } |
| 5071 | |
philippe | 19dfe03 | 2013-03-24 20:10:23 +0000 | [diff] [blame] | 5072 | /* Thread successfully completed pthread_cond_init: |
| 5073 | cond=arg[1], cond_attr=arg[2] */ |
| 5074 | case _VG_USERREQ__HG_PTHREAD_COND_INIT_POST: |
| 5075 | evh__HG_PTHREAD_COND_INIT_POST( tid, |
| 5076 | (void*)args[1], (void*)args[2] ); |
| 5077 | break; |
| 5078 | |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 5079 | /* cond=arg[1], cond_is_init=arg[2] */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5080 | case _VG_USERREQ__HG_PTHREAD_COND_DESTROY_PRE: |
sewardj | c02f6c4 | 2013-10-14 13:51:25 +0000 | [diff] [blame] | 5081 | evh__HG_PTHREAD_COND_DESTROY_PRE( tid, (void*)args[1], args[2] != 0 ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5082 | break; |
| 5083 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5084 | /* Thread successfully completed pthread_cond_wait, cond=arg[1], |
| 5085 | mutex=arg[2] */ |
| 5086 | case _VG_USERREQ__HG_PTHREAD_COND_WAIT_POST: |
| 5087 | evh__HG_PTHREAD_COND_WAIT_POST( tid, |
sewardj | ff427c9 | 2013-10-14 12:13:52 +0000 | [diff] [blame] | 5088 | (void*)args[1], (void*)args[2], |
| 5089 | (Bool)args[3] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5090 | break; |
| 5091 | |
| 5092 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_INIT_POST: |
| 5093 | evh__HG_PTHREAD_RWLOCK_INIT_POST( tid, (void*)args[1] ); |
| 5094 | break; |
| 5095 | |
| 5096 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_DESTROY_PRE: |
| 5097 | evh__HG_PTHREAD_RWLOCK_DESTROY_PRE( tid, (void*)args[1] ); |
| 5098 | break; |
| 5099 | |
sewardj | 789c3c5 | 2008-02-25 12:10:07 +0000 | [diff] [blame] | 5100 | /* rwlock=arg[1], isW=arg[2], isTryLock=arg[3] */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5101 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_LOCK_PRE: |
sewardj | 789c3c5 | 2008-02-25 12:10:07 +0000 | [diff] [blame] | 5102 | evh__HG_PTHREAD_RWLOCK_LOCK_PRE( tid, (void*)args[1], |
| 5103 | args[2], args[3] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5104 | break; |
| 5105 | |
| 5106 | /* rwlock=arg[1], isW=arg[2] */ |
| 5107 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_LOCK_POST: |
| 5108 | evh__HG_PTHREAD_RWLOCK_LOCK_POST( tid, (void*)args[1], args[2] ); |
| 5109 | break; |
| 5110 | |
| 5111 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_UNLOCK_PRE: |
| 5112 | evh__HG_PTHREAD_RWLOCK_UNLOCK_PRE( tid, (void*)args[1] ); |
| 5113 | break; |
| 5114 | |
| 5115 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_UNLOCK_POST: |
| 5116 | evh__HG_PTHREAD_RWLOCK_UNLOCK_POST( tid, (void*)args[1] ); |
| 5117 | break; |
| 5118 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 5119 | case _VG_USERREQ__HG_POSIX_SEM_INIT_POST: /* sem_t*, unsigned long */ |
| 5120 | evh__HG_POSIX_SEM_INIT_POST( tid, (void*)args[1], args[2] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5121 | break; |
| 5122 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 5123 | case _VG_USERREQ__HG_POSIX_SEM_DESTROY_PRE: /* sem_t* */ |
| 5124 | evh__HG_POSIX_SEM_DESTROY_PRE( tid, (void*)args[1] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5125 | break; |
| 5126 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 5127 | case _VG_USERREQ__HG_POSIX_SEM_POST_PRE: /* sem_t* */ |
| 5128 | evh__HG_POSIX_SEM_POST_PRE( tid, (void*)args[1] ); |
| 5129 | break; |
| 5130 | |
| 5131 | case _VG_USERREQ__HG_POSIX_SEM_WAIT_POST: /* sem_t* */ |
| 5132 | evh__HG_POSIX_SEM_WAIT_POST( tid, (void*)args[1] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5133 | break; |
| 5134 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 5135 | case _VG_USERREQ__HG_PTHREAD_BARRIER_INIT_PRE: |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 5136 | /* pth_bar_t*, ulong count, ulong resizable */ |
| 5137 | evh__HG_PTHREAD_BARRIER_INIT_PRE( tid, (void*)args[1], |
| 5138 | args[2], args[3] ); |
| 5139 | break; |
| 5140 | |
| 5141 | case _VG_USERREQ__HG_PTHREAD_BARRIER_RESIZE_PRE: |
| 5142 | /* pth_bar_t*, ulong newcount */ |
| 5143 | evh__HG_PTHREAD_BARRIER_RESIZE_PRE ( tid, (void*)args[1], |
| 5144 | args[2] ); |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 5145 | break; |
| 5146 | |
| 5147 | case _VG_USERREQ__HG_PTHREAD_BARRIER_WAIT_PRE: |
| 5148 | /* pth_bar_t* */ |
| 5149 | evh__HG_PTHREAD_BARRIER_WAIT_PRE( tid, (void*)args[1] ); |
| 5150 | break; |
| 5151 | |
| 5152 | case _VG_USERREQ__HG_PTHREAD_BARRIER_DESTROY_PRE: |
| 5153 | /* pth_bar_t* */ |
| 5154 | evh__HG_PTHREAD_BARRIER_DESTROY_PRE( tid, (void*)args[1] ); |
| 5155 | break; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5156 | |
sewardj | 5a644da | 2009-08-11 10:35:58 +0000 | [diff] [blame] | 5157 | case _VG_USERREQ__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE: |
| 5158 | /* pth_spinlock_t* */ |
| 5159 | evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE( tid, (void*)args[1] ); |
| 5160 | break; |
| 5161 | |
| 5162 | case _VG_USERREQ__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_POST: |
| 5163 | /* pth_spinlock_t* */ |
| 5164 | evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_POST( tid, (void*)args[1] ); |
| 5165 | break; |
| 5166 | |
| 5167 | case _VG_USERREQ__HG_PTHREAD_SPIN_LOCK_PRE: |
| 5168 | /* pth_spinlock_t*, Word */ |
| 5169 | evh__HG_PTHREAD_SPIN_LOCK_PRE( tid, (void*)args[1], args[2] ); |
| 5170 | break; |
| 5171 | |
| 5172 | case _VG_USERREQ__HG_PTHREAD_SPIN_LOCK_POST: |
| 5173 | /* pth_spinlock_t* */ |
| 5174 | evh__HG_PTHREAD_SPIN_LOCK_POST( tid, (void*)args[1] ); |
| 5175 | break; |
| 5176 | |
| 5177 | case _VG_USERREQ__HG_PTHREAD_SPIN_DESTROY_PRE: |
| 5178 | /* pth_spinlock_t* */ |
| 5179 | evh__HG_PTHREAD_SPIN_DESTROY_PRE( tid, (void*)args[1] ); |
| 5180 | break; |
| 5181 | |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 5182 | case _VG_USERREQ__HG_CLIENTREQ_UNIMP: { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 5183 | /* HChar* who */ |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 5184 | HChar* who = (HChar*)args[1]; |
| 5185 | HChar buf[50 + 50]; |
| 5186 | Thread* thr = map_threads_maybe_lookup( tid ); |
| 5187 | tl_assert( thr ); /* I must be mapped */ |
| 5188 | tl_assert( who ); |
| 5189 | tl_assert( VG_(strlen)(who) <= 50 ); |
| 5190 | VG_(sprintf)(buf, "Unimplemented client request macro \"%s\"", who ); |
| 5191 | /* record_error_Misc strdup's buf, so this is safe: */ |
| 5192 | HG_(record_error_Misc)( thr, buf ); |
| 5193 | break; |
| 5194 | } |
| 5195 | |
| 5196 | case _VG_USERREQ__HG_USERSO_SEND_PRE: |
| 5197 | /* UWord arbitrary-SO-tag */ |
| 5198 | evh__HG_USERSO_SEND_PRE( tid, args[1] ); |
| 5199 | break; |
| 5200 | |
| 5201 | case _VG_USERREQ__HG_USERSO_RECV_POST: |
| 5202 | /* UWord arbitrary-SO-tag */ |
| 5203 | evh__HG_USERSO_RECV_POST( tid, args[1] ); |
| 5204 | break; |
| 5205 | |
sewardj | 6015d0e | 2011-03-11 19:10:48 +0000 | [diff] [blame] | 5206 | case _VG_USERREQ__HG_USERSO_FORGET_ALL: |
| 5207 | /* UWord arbitrary-SO-tag */ |
| 5208 | evh__HG_USERSO_FORGET_ALL( tid, args[1] ); |
| 5209 | break; |
| 5210 | |
philippe | f577434 | 2014-05-03 11:12:50 +0000 | [diff] [blame] | 5211 | case VG_USERREQ__GDB_MONITOR_COMMAND: { |
| 5212 | Bool handled = handle_gdb_monitor_command (tid, (HChar*)args[1]); |
| 5213 | if (handled) |
| 5214 | *ret = 1; |
| 5215 | else |
| 5216 | *ret = 0; |
| 5217 | return handled; |
| 5218 | } |
| 5219 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5220 | default: |
| 5221 | /* Unhandled Helgrind client request! */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5222 | tl_assert2(0, "unhandled Helgrind client request 0x%lx", |
| 5223 | args[0]); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | return True; |
| 5227 | } |
| 5228 | |
| 5229 | |
| 5230 | /*----------------------------------------------------------------*/ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5231 | /*--- Setup ---*/ |
| 5232 | /*----------------------------------------------------------------*/ |
| 5233 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 5234 | static Bool hg_process_cmd_line_option ( const HChar* arg ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5235 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 5236 | const HChar* tmp_str; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5237 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 5238 | if VG_BOOL_CLO(arg, "--track-lockorders", |
| 5239 | HG_(clo_track_lockorders)) {} |
| 5240 | else if VG_BOOL_CLO(arg, "--cmp-race-err-addrs", |
| 5241 | HG_(clo_cmp_race_err_addrs)) {} |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 5242 | |
| 5243 | else if VG_XACT_CLO(arg, "--history-level=none", |
| 5244 | HG_(clo_history_level), 0); |
sewardj | f386139 | 2009-08-02 10:16:03 +0000 | [diff] [blame] | 5245 | else if VG_XACT_CLO(arg, "--history-level=approx", |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 5246 | HG_(clo_history_level), 1); |
| 5247 | else if VG_XACT_CLO(arg, "--history-level=full", |
| 5248 | HG_(clo_history_level), 2); |
sewardj | 849b0ed | 2008-12-21 10:43:10 +0000 | [diff] [blame] | 5249 | |
sewardj | f585e48 | 2009-08-16 22:52:29 +0000 | [diff] [blame] | 5250 | /* If you change the 10k/30mill limits, remember to also change |
sewardj | 849b0ed | 2008-12-21 10:43:10 +0000 | [diff] [blame] | 5251 | them in assertions at the top of event_map_maybe_GC. */ |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 5252 | else if VG_BINT_CLO(arg, "--conflict-cache-size", |
sewardj | f585e48 | 2009-08-16 22:52:29 +0000 | [diff] [blame] | 5253 | HG_(clo_conflict_cache_size), 10*1000, 30*1000*1000) {} |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5254 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 5255 | /* "stuvwx" --> stuvwx (binary) */ |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 5256 | else if VG_STR_CLO(arg, "--hg-sanity-flags", tmp_str) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5257 | Int j; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5258 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 5259 | if (6 != VG_(strlen)(tmp_str)) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5260 | VG_(message)(Vg_UserMsg, |
sewardj | 2411849 | 2009-07-15 14:50:02 +0000 | [diff] [blame] | 5261 | "--hg-sanity-flags argument must have 6 digits\n"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5262 | return False; |
| 5263 | } |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 5264 | for (j = 0; j < 6; j++) { |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 5265 | if ('0' == tmp_str[j]) { /* do nothing */ } |
| 5266 | else if ('1' == tmp_str[j]) HG_(clo_sanity_flags) |= (1 << (6-1-j)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5267 | else { |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 5268 | VG_(message)(Vg_UserMsg, "--hg-sanity-flags argument can " |
sewardj | 2411849 | 2009-07-15 14:50:02 +0000 | [diff] [blame] | 5269 | "only contain 0s and 1s\n"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5270 | return False; |
| 5271 | } |
| 5272 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5273 | if (0) VG_(printf)("XXX sanity flags: 0x%lx\n", HG_(clo_sanity_flags)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
sewardj | 622fe49 | 2011-03-11 21:06:59 +0000 | [diff] [blame] | 5276 | else if VG_BOOL_CLO(arg, "--free-is-write", |
| 5277 | HG_(clo_free_is_write)) {} |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 5278 | |
| 5279 | else if VG_XACT_CLO(arg, "--vts-pruning=never", |
| 5280 | HG_(clo_vts_pruning), 0); |
| 5281 | else if VG_XACT_CLO(arg, "--vts-pruning=auto", |
| 5282 | HG_(clo_vts_pruning), 1); |
| 5283 | else if VG_XACT_CLO(arg, "--vts-pruning=always", |
| 5284 | HG_(clo_vts_pruning), 2); |
| 5285 | |
| 5286 | else if VG_BOOL_CLO(arg, "--check-stack-refs", |
| 5287 | HG_(clo_check_stack_refs)) {} |
| 5288 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5289 | else |
| 5290 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
| 5291 | |
| 5292 | return True; |
| 5293 | } |
| 5294 | |
| 5295 | static void hg_print_usage ( void ) |
| 5296 | { |
| 5297 | VG_(printf)( |
sewardj | 622fe49 | 2011-03-11 21:06:59 +0000 | [diff] [blame] | 5298 | " --free-is-write=no|yes treat heap frees as writes [no]\n" |
sewardj | 849b0ed | 2008-12-21 10:43:10 +0000 | [diff] [blame] | 5299 | " --track-lockorders=no|yes show lock ordering errors? [yes]\n" |
njn | f6e8ca9 | 2009-08-07 02:18:00 +0000 | [diff] [blame] | 5300 | " --history-level=none|approx|full [full]\n" |
sewardj | f386139 | 2009-08-02 10:16:03 +0000 | [diff] [blame] | 5301 | " full: show both stack traces for a data race (can be very slow)\n" |
| 5302 | " approx: full trace for one thread, approx for the other (faster)\n" |
| 5303 | " none: only show trace for one thread in a race (fastest)\n" |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 5304 | " --conflict-cache-size=N size of 'full' history cache [1000000]\n" |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 5305 | " --check-stack-refs=no|yes race-check reads and writes on the\n" |
| 5306 | " main stack and thread stacks? [yes]\n" |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5307 | ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | static void hg_print_debug_usage ( void ) |
| 5311 | { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5312 | VG_(printf)(" --cmp-race-err-addrs=no|yes are data addresses in " |
| 5313 | "race errors significant? [no]\n"); |
sewardj | 849b0ed | 2008-12-21 10:43:10 +0000 | [diff] [blame] | 5314 | VG_(printf)(" --hg-sanity-flags=<XXXXXX> sanity check " |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 5315 | " at events (X = 0|1) [000000]\n"); |
| 5316 | VG_(printf)(" --hg-sanity-flags values:\n"); |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 5317 | VG_(printf)(" 010000 after changes to " |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5318 | "lock-order-acquisition-graph\n"); |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 5319 | VG_(printf)(" 001000 at memory accesses (NB: not currently used)\n"); |
| 5320 | VG_(printf)(" 000100 at mem permission setting for " |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5321 | "ranges >= %d bytes\n", SCE_BIGRANGE_T); |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 5322 | VG_(printf)(" 000010 at lock/unlock events\n"); |
| 5323 | VG_(printf)(" 000001 at thread create/join events\n"); |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 5324 | VG_(printf)( |
| 5325 | " --vts-pruning=never|auto|always [auto]\n" |
| 5326 | " never: is never done (may cause big space leaks in Helgrind)\n" |
| 5327 | " auto: done just often enough to keep space usage under control\n" |
| 5328 | " always: done after every VTS GC (mostly just a big time waster)\n" |
| 5329 | ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5330 | } |
| 5331 | |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 5332 | static void hg_print_stats (void) |
| 5333 | { |
| 5334 | |
| 5335 | if (1) { |
| 5336 | VG_(printf)("\n"); |
| 5337 | HG_(ppWSUstats)( univ_lsets, "univ_lsets" ); |
| 5338 | if (HG_(clo_track_lockorders)) { |
| 5339 | VG_(printf)("\n"); |
| 5340 | HG_(ppWSUstats)( univ_laog, "univ_laog" ); |
| 5341 | } |
| 5342 | } |
| 5343 | |
| 5344 | //zz VG_(printf)("\n"); |
| 5345 | //zz VG_(printf)(" hbefore: %'10lu queries\n", stats__hbefore_queries); |
| 5346 | //zz VG_(printf)(" hbefore: %'10lu cache 0 hits\n", stats__hbefore_cache0s); |
| 5347 | //zz VG_(printf)(" hbefore: %'10lu cache > 0 hits\n", stats__hbefore_cacheNs); |
| 5348 | //zz VG_(printf)(" hbefore: %'10lu graph searches\n", stats__hbefore_gsearches); |
| 5349 | //zz VG_(printf)(" hbefore: %'10lu of which slow\n", |
| 5350 | //zz stats__hbefore_gsearches - stats__hbefore_gsearchFs); |
| 5351 | //zz VG_(printf)(" hbefore: %'10lu stack high water mark\n", |
| 5352 | //zz stats__hbefore_stk_hwm); |
| 5353 | //zz VG_(printf)(" hbefore: %'10lu cache invals\n", stats__hbefore_invals); |
| 5354 | //zz VG_(printf)(" hbefore: %'10lu probes\n", stats__hbefore_probes); |
| 5355 | |
| 5356 | VG_(printf)("\n"); |
| 5357 | VG_(printf)(" locksets: %'8d unique lock sets\n", |
| 5358 | (Int)HG_(cardinalityWSU)( univ_lsets )); |
| 5359 | if (HG_(clo_track_lockorders)) { |
| 5360 | VG_(printf)(" univ_laog: %'8d unique lock sets\n", |
| 5361 | (Int)HG_(cardinalityWSU)( univ_laog )); |
| 5362 | } |
| 5363 | |
| 5364 | //VG_(printf)("L(ast)L(ock) map: %'8lu inserts (%d map size)\n", |
| 5365 | // stats__ga_LL_adds, |
| 5366 | // (Int)(ga_to_lastlock ? VG_(sizeFM)( ga_to_lastlock ) : 0) ); |
| 5367 | |
| 5368 | VG_(printf)(" LockN-to-P map: %'8llu queries (%llu map size)\n", |
| 5369 | HG_(stats__LockN_to_P_queries), |
| 5370 | HG_(stats__LockN_to_P_get_map_size)() ); |
| 5371 | |
| 5372 | VG_(printf)("string table map: %'8llu queries (%llu map size)\n", |
| 5373 | HG_(stats__string_table_queries), |
| 5374 | HG_(stats__string_table_get_map_size)() ); |
| 5375 | if (HG_(clo_track_lockorders)) { |
| 5376 | VG_(printf)(" LAOG: %'8d map size\n", |
| 5377 | (Int)(laog ? VG_(sizeFM)( laog ) : 0)); |
| 5378 | VG_(printf)(" LAOG exposition: %'8d map size\n", |
| 5379 | (Int)(laog_exposition ? VG_(sizeFM)( laog_exposition ) : 0)); |
| 5380 | } |
| 5381 | |
| 5382 | VG_(printf)(" locks: %'8lu acquires, " |
| 5383 | "%'lu releases\n", |
| 5384 | stats__lockN_acquires, |
| 5385 | stats__lockN_releases |
| 5386 | ); |
| 5387 | VG_(printf)(" sanity checks: %'8lu\n", stats__sanity_checks); |
| 5388 | |
| 5389 | VG_(printf)("\n"); |
| 5390 | libhb_shutdown(True); // This in fact only print stats. |
| 5391 | } |
| 5392 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5393 | static void hg_fini ( Int exitcode ) |
| 5394 | { |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 5395 | if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) { |
| 5396 | VG_(message)(Vg_UserMsg, |
| 5397 | "For counts of detected and suppressed errors, " |
| 5398 | "rerun with: -v\n"); |
| 5399 | } |
| 5400 | |
| 5401 | if (VG_(clo_verbosity) == 1 && !VG_(clo_xml) |
| 5402 | && HG_(clo_history_level) >= 2) { |
| 5403 | VG_(umsg)( |
| 5404 | "Use --history-level=approx or =none to gain increased speed, at\n" ); |
| 5405 | VG_(umsg)( |
| 5406 | "the cost of reduced accuracy of conflicting-access information\n"); |
| 5407 | } |
| 5408 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5409 | if (SHOW_DATA_STRUCTURES) |
| 5410 | pp_everything( PP_ALL, "SK_(fini)" ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5411 | if (HG_(clo_sanity_flags)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5412 | all__sanity_check("SK_(fini)"); |
| 5413 | |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 5414 | if (VG_(clo_stats)) |
| 5415 | hg_print_stats(); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5416 | } |
| 5417 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5418 | /* FIXME: move these somewhere sane */ |
| 5419 | |
| 5420 | static |
| 5421 | void for_libhb__get_stacktrace ( Thr* hbt, Addr* frames, UWord nRequest ) |
| 5422 | { |
| 5423 | Thread* thr; |
| 5424 | ThreadId tid; |
| 5425 | UWord nActual; |
| 5426 | tl_assert(hbt); |
sewardj | 6062664 | 2011-03-10 15:14:37 +0000 | [diff] [blame] | 5427 | thr = libhb_get_Thr_hgthread( hbt ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5428 | tl_assert(thr); |
| 5429 | tid = map_threads_maybe_reverse_lookup_SLOW(thr); |
| 5430 | nActual = (UWord)VG_(get_StackTrace)( tid, frames, (UInt)nRequest, |
| 5431 | NULL, NULL, 0 ); |
| 5432 | tl_assert(nActual <= nRequest); |
| 5433 | for (; nActual < nRequest; nActual++) |
| 5434 | frames[nActual] = 0; |
| 5435 | } |
| 5436 | |
| 5437 | static |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 5438 | ExeContext* for_libhb__get_EC ( Thr* hbt ) |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5439 | { |
| 5440 | Thread* thr; |
| 5441 | ThreadId tid; |
| 5442 | ExeContext* ec; |
| 5443 | tl_assert(hbt); |
sewardj | 6062664 | 2011-03-10 15:14:37 +0000 | [diff] [blame] | 5444 | thr = libhb_get_Thr_hgthread( hbt ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5445 | tl_assert(thr); |
| 5446 | tid = map_threads_maybe_reverse_lookup_SLOW(thr); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 5447 | /* this will assert if tid is invalid */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5448 | ec = VG_(record_ExeContext)( tid, 0 ); |
sewardj | d52392d | 2008-11-08 20:36:26 +0000 | [diff] [blame] | 5449 | return ec; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 5453 | static void hg_post_clo_init ( void ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5454 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5455 | Thr* hbthr_root; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 5456 | |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 5457 | ///////////////////////////////////////////// |
| 5458 | hbthr_root = libhb_init( for_libhb__get_stacktrace, |
| 5459 | for_libhb__get_EC ); |
| 5460 | ///////////////////////////////////////////// |
| 5461 | |
| 5462 | |
| 5463 | if (HG_(clo_track_lockorders)) |
| 5464 | laog__init(); |
| 5465 | |
| 5466 | initialise_data_structures(hbthr_root); |
| 5467 | } |
| 5468 | |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 5469 | static void hg_info_location (Addr a) |
| 5470 | { |
| 5471 | (void) HG_(get_and_pp_addrdescr) (a); |
| 5472 | } |
| 5473 | |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 5474 | static void hg_pre_clo_init ( void ) |
| 5475 | { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5476 | VG_(details_name) ("Helgrind"); |
| 5477 | VG_(details_version) (NULL); |
| 5478 | VG_(details_description) ("a thread error detector"); |
| 5479 | VG_(details_copyright_author)( |
sewardj | 0f157dd | 2013-10-18 14:27:36 +0000 | [diff] [blame] | 5480 | "Copyright (C) 2007-2013, and GNU GPL'd, by OpenWorks LLP et al."); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5481 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | 9c08c0f | 2011-03-10 15:01:14 +0000 | [diff] [blame] | 5482 | VG_(details_avg_translation_sizeB) ( 320 ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5483 | |
| 5484 | VG_(basic_tool_funcs) (hg_post_clo_init, |
| 5485 | hg_instrument, |
| 5486 | hg_fini); |
| 5487 | |
| 5488 | VG_(needs_core_errors) (); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5489 | VG_(needs_tool_errors) (HG_(eq_Error), |
sewardj | 2411849 | 2009-07-15 14:50:02 +0000 | [diff] [blame] | 5490 | HG_(before_pp_Error), |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5491 | HG_(pp_Error), |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5492 | False,/*show TIDs for errors*/ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 5493 | HG_(update_extra), |
| 5494 | HG_(recognised_suppression), |
| 5495 | HG_(read_extra_suppression_info), |
| 5496 | HG_(error_matches_suppression), |
| 5497 | HG_(get_error_name), |
philippe | 4e32d67 | 2013-10-17 22:10:41 +0000 | [diff] [blame] | 5498 | HG_(get_extra_suppression_info), |
| 5499 | HG_(print_extra_suppression_use), |
| 5500 | HG_(update_extra_suppression_use)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5501 | |
sewardj | 2411849 | 2009-07-15 14:50:02 +0000 | [diff] [blame] | 5502 | VG_(needs_xml_output) (); |
| 5503 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5504 | VG_(needs_command_line_options)(hg_process_cmd_line_option, |
| 5505 | hg_print_usage, |
| 5506 | hg_print_debug_usage); |
| 5507 | VG_(needs_client_requests) (hg_handle_client_request); |
| 5508 | |
| 5509 | // FIXME? |
| 5510 | //VG_(needs_sanity_checks) (hg_cheap_sanity_check, |
| 5511 | // hg_expensive_sanity_check); |
| 5512 | |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 5513 | VG_(needs_print_stats) (hg_print_stats); |
philippe | 07c0852 | 2014-05-14 20:39:27 +0000 | [diff] [blame] | 5514 | VG_(needs_info_location) (hg_info_location); |
philippe | 8587b54 | 2013-12-15 20:24:43 +0000 | [diff] [blame] | 5515 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5516 | VG_(needs_malloc_replacement) (hg_cli__malloc, |
| 5517 | hg_cli____builtin_new, |
| 5518 | hg_cli____builtin_vec_new, |
| 5519 | hg_cli__memalign, |
| 5520 | hg_cli__calloc, |
| 5521 | hg_cli__free, |
| 5522 | hg_cli____builtin_delete, |
| 5523 | hg_cli____builtin_vec_delete, |
| 5524 | hg_cli__realloc, |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 5525 | hg_cli_malloc_usable_size, |
philippe | d99c26a | 2012-07-31 22:17:28 +0000 | [diff] [blame] | 5526 | HG_CLI__DEFAULT_MALLOC_REDZONE_SZB ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5527 | |
sewardj | 849b0ed | 2008-12-21 10:43:10 +0000 | [diff] [blame] | 5528 | /* 21 Dec 08: disabled this; it mostly causes H to start more |
| 5529 | slowly and use significantly more memory, without very often |
| 5530 | providing useful results. The user can request to load this |
| 5531 | information manually with --read-var-info=yes. */ |
| 5532 | if (0) VG_(needs_var_info)(); /* optional */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5533 | |
| 5534 | VG_(track_new_mem_startup) ( evh__new_mem_w_perms ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 5535 | VG_(track_new_mem_stack_signal)( evh__new_mem_w_tid ); |
| 5536 | VG_(track_new_mem_brk) ( evh__new_mem_w_tid ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5537 | VG_(track_new_mem_mmap) ( evh__new_mem_w_perms ); |
sewardj | 1f77fec | 2010-04-12 19:51:04 +0000 | [diff] [blame] | 5538 | VG_(track_new_mem_stack) ( evh__new_mem_stack ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5539 | |
| 5540 | // FIXME: surely this isn't thread-aware |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 5541 | VG_(track_copy_mem_remap) ( evh__copy_mem ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5542 | |
| 5543 | VG_(track_change_mem_mprotect) ( evh__set_perms ); |
| 5544 | |
| 5545 | VG_(track_die_mem_stack_signal)( evh__die_mem ); |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 5546 | VG_(track_die_mem_brk) ( evh__die_mem_munmap ); |
| 5547 | VG_(track_die_mem_munmap) ( evh__die_mem_munmap ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5548 | VG_(track_die_mem_stack) ( evh__die_mem ); |
| 5549 | |
| 5550 | // FIXME: what is this for? |
| 5551 | VG_(track_ban_mem_stack) (NULL); |
| 5552 | |
| 5553 | VG_(track_pre_mem_read) ( evh__pre_mem_read ); |
| 5554 | VG_(track_pre_mem_read_asciiz) ( evh__pre_mem_read_asciiz ); |
| 5555 | VG_(track_pre_mem_write) ( evh__pre_mem_write ); |
| 5556 | VG_(track_post_mem_write) (NULL); |
| 5557 | |
| 5558 | ///////////////// |
| 5559 | |
| 5560 | VG_(track_pre_thread_ll_create)( evh__pre_thread_ll_create ); |
| 5561 | VG_(track_pre_thread_ll_exit) ( evh__pre_thread_ll_exit ); |
| 5562 | |
| 5563 | VG_(track_start_client_code)( evh__start_client_code ); |
| 5564 | VG_(track_stop_client_code)( evh__stop_client_code ); |
| 5565 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5566 | /* Ensure that requirements for "dodgy C-as-C++ style inheritance" |
| 5567 | as described in comments at the top of pub_tool_hashtable.h, are |
| 5568 | met. Blargh. */ |
| 5569 | tl_assert( sizeof(void*) == sizeof(struct _MallocMeta*) ); |
| 5570 | tl_assert( sizeof(UWord) == sizeof(Addr) ); |
| 5571 | hg_mallocmeta_table |
| 5572 | = VG_(HT_construct)( "hg_malloc_metadata_table" ); |
| 5573 | |
philippe | 5fbc976 | 2013-12-01 19:28:48 +0000 | [diff] [blame] | 5574 | MallocMeta_poolalloc = VG_(newPA) ( sizeof(MallocMeta), |
| 5575 | 1000, |
| 5576 | HG_(zalloc), |
| 5577 | "hg_malloc_metadata_pool", |
| 5578 | HG_(free)); |
| 5579 | |
sewardj | 61bc2c5 | 2011-02-09 10:34:00 +0000 | [diff] [blame] | 5580 | // add a callback to clean up on (threaded) fork. |
| 5581 | VG_(atfork)(NULL/*pre*/, NULL/*parent*/, evh__atfork_child/*child*/); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | VG_DETERMINE_INTERFACE_VERSION(hg_pre_clo_init) |
| 5585 | |
| 5586 | /*--------------------------------------------------------------------*/ |
| 5587 | /*--- end hg_main.c ---*/ |
| 5588 | /*--------------------------------------------------------------------*/ |