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 | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 11 | Copyright (C) 2007-2010 OpenWorks LLP |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 12 | info@open-works.co.uk |
| 13 | |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 14 | Copyright (C) 2007-2010 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" |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 40 | #include "pub_tool_libcassert.h" |
| 41 | #include "pub_tool_libcbase.h" |
| 42 | #include "pub_tool_libcprint.h" |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 43 | #include "pub_tool_threadstate.h" |
| 44 | #include "pub_tool_tooliface.h" |
| 45 | #include "pub_tool_hashtable.h" |
| 46 | #include "pub_tool_replacemalloc.h" |
| 47 | #include "pub_tool_machine.h" |
| 48 | #include "pub_tool_options.h" |
| 49 | #include "pub_tool_xarray.h" |
| 50 | #include "pub_tool_stacktrace.h" |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 51 | #include "pub_tool_wordfm.h" |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 52 | #include "pub_tool_debuginfo.h" // VG_(find_seginfo), VG_(seginfo_soname) |
| 53 | #include "pub_tool_redir.h" // sonames for the dynamic linkers |
| 54 | #include "pub_tool_vki.h" // VKI_PAGE_SIZE |
sewardj | 61bc2c5 | 2011-02-09 10:34:00 +0000 | [diff] [blame] | 55 | #include "pub_tool_libcproc.h" // VG_(atfork) |
sewardj | 234e558 | 2011-02-09 12:47:23 +0000 | [diff] [blame] | 56 | #include "pub_tool_aspacemgr.h" // VG_(am_is_valid_for_client) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 57 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 58 | #include "hg_basics.h" |
| 59 | #include "hg_wordset.h" |
| 60 | #include "hg_lock_n_thread.h" |
| 61 | #include "hg_errors.h" |
| 62 | |
| 63 | #include "libhb.h" |
| 64 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 65 | #include "helgrind.h" |
| 66 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 67 | |
| 68 | // FIXME: new_mem_w_tid ignores the supplied tid. (wtf?!) |
| 69 | |
| 70 | // FIXME: when client destroys a lock or a CV, remove these |
| 71 | // from our mappings, so that the associated SO can be freed up |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 72 | |
| 73 | /*----------------------------------------------------------------*/ |
| 74 | /*--- ---*/ |
| 75 | /*----------------------------------------------------------------*/ |
| 76 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 77 | /* Note this needs to be compiled with -fno-strict-aliasing, since it |
| 78 | contains a whole bunch of calls to lookupFM etc which cast between |
| 79 | Word and pointer types. gcc rightly complains this breaks ANSI C |
| 80 | strict aliasing rules, at -O2. No complaints at -O, but -O2 gives |
| 81 | worthwhile performance benefits over -O. |
sewardj | c17be79 | 2007-11-10 22:50:13 +0000 | [diff] [blame] | 82 | */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 83 | |
| 84 | // FIXME what is supposed to happen to locks in memory which |
| 85 | // is relocated as a result of client realloc? |
| 86 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 87 | // FIXME put referencing ThreadId into Thread and get |
| 88 | // rid of the slow reverse mapping function. |
| 89 | |
| 90 | // FIXME accesses to NoAccess areas: change state to Excl? |
| 91 | |
| 92 | // FIXME report errors for accesses of NoAccess memory? |
| 93 | |
| 94 | // FIXME pth_cond_wait/timedwait wrappers. Even if these fail, |
| 95 | // the thread still holds the lock. |
| 96 | |
| 97 | /* ------------ Debug/trace options ------------ */ |
| 98 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 99 | // 0 for silent, 1 for some stuff, 2 for lots of stuff |
| 100 | #define SHOW_EVENTS 0 |
| 101 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 102 | |
| 103 | static void all__sanity_check ( Char* who ); /* fwds */ |
| 104 | |
| 105 | #define HG_CLI__MALLOC_REDZONE_SZB 16 /* let's say */ |
| 106 | |
| 107 | // 0 for none, 1 for dump at end of run |
| 108 | #define SHOW_DATA_STRUCTURES 0 |
| 109 | |
| 110 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 111 | /* ------------ Misc comments ------------ */ |
| 112 | |
| 113 | // FIXME: don't hardwire initial entries for root thread. |
| 114 | // Instead, let the pre_thread_ll_create handler do this. |
| 115 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 116 | |
| 117 | /*----------------------------------------------------------------*/ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 118 | /*--- Primary data structures ---*/ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 119 | /*----------------------------------------------------------------*/ |
| 120 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 121 | /* Admin linked list of Threads */ |
| 122 | static Thread* admin_threads = NULL; |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 123 | Thread* get_admin_threads ( void ) { return admin_threads; } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 124 | |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 125 | /* Admin double linked list of Locks */ |
| 126 | /* We need a double linked list to properly and efficiently |
| 127 | handle del_LockN. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 128 | static Lock* admin_locks = NULL; |
| 129 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 130 | /* Mapping table for core ThreadIds to Thread* */ |
| 131 | static Thread** map_threads = NULL; /* Array[VG_N_THREADS] of Thread* */ |
| 132 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 133 | /* Mapping table for lock guest addresses to Lock* */ |
| 134 | static WordFM* map_locks = NULL; /* WordFM LockAddr Lock* */ |
| 135 | |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 136 | /* The word-set universes for lock sets. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 137 | static WordSetU* univ_lsets = NULL; /* sets of Lock* */ |
| 138 | static WordSetU* univ_laog = NULL; /* sets of Lock*, for LAOG */ |
| 139 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 140 | /* Allow libhb to get at the universe of locksets stored |
| 141 | here. Sigh. */ |
| 142 | WordSetU* HG_(get_univ_lsets) ( void ) { return univ_lsets; } |
| 143 | |
| 144 | /* Allow libhb to get at the list of locks stored here. Ditto |
| 145 | sigh. */ |
| 146 | Lock* HG_(get_admin_locks) ( void ) { return admin_locks; } |
| 147 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 148 | |
| 149 | /*----------------------------------------------------------------*/ |
| 150 | /*--- Simple helpers for the data structures ---*/ |
| 151 | /*----------------------------------------------------------------*/ |
| 152 | |
| 153 | static UWord stats__lockN_acquires = 0; |
| 154 | static UWord stats__lockN_releases = 0; |
| 155 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 156 | static |
| 157 | ThreadId map_threads_maybe_reverse_lookup_SLOW ( Thread* thr ); /*fwds*/ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 158 | |
| 159 | /* --------- Constructors --------- */ |
| 160 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 161 | static Thread* mk_Thread ( Thr* hbthr ) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 162 | static Int indx = 1; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 163 | Thread* thread = HG_(zalloc)( "hg.mk_Thread.1", sizeof(Thread) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 164 | thread->locksetA = HG_(emptyWS)( univ_lsets ); |
| 165 | thread->locksetW = HG_(emptyWS)( univ_lsets ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 166 | thread->magic = Thread_MAGIC; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 167 | thread->hbthr = hbthr; |
| 168 | thread->coretid = VG_INVALID_THREADID; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 169 | thread->created_at = NULL; |
| 170 | thread->announced = False; |
| 171 | thread->errmsg_index = indx++; |
| 172 | thread->admin = admin_threads; |
| 173 | admin_threads = thread; |
| 174 | return thread; |
| 175 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 176 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 177 | // Make a new lock which is unlocked (hence ownerless) |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 178 | // and insert the new lock in admin_locks double linked list. |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 179 | static Lock* mk_LockN ( LockKind kind, Addr guestaddr ) { |
| 180 | static ULong unique = 0; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 181 | Lock* lock = HG_(zalloc)( "hg.mk_Lock.1", sizeof(Lock) ); |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 182 | /* begin: add to double linked list */ |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 183 | if (admin_locks) |
| 184 | admin_locks->admin_prev = lock; |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 185 | lock->admin_next = admin_locks; |
| 186 | lock->admin_prev = NULL; |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 187 | admin_locks = lock; |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 188 | /* end: add */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 189 | lock->unique = unique++; |
| 190 | lock->magic = LockN_MAGIC; |
| 191 | lock->appeared_at = NULL; |
| 192 | lock->acquired_at = NULL; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 193 | lock->hbso = libhb_so_alloc(); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 194 | lock->guestaddr = guestaddr; |
| 195 | lock->kind = kind; |
| 196 | lock->heldW = False; |
| 197 | lock->heldBy = NULL; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 198 | tl_assert(HG_(is_sane_LockN)(lock)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 199 | return lock; |
| 200 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 201 | |
| 202 | /* Release storage for a Lock. Also release storage in .heldBy, if |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 203 | any. Removes from admin_locks double linked list. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 204 | static void del_LockN ( Lock* lk ) |
| 205 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 206 | tl_assert(HG_(is_sane_LockN)(lk)); |
| 207 | tl_assert(lk->hbso); |
| 208 | libhb_so_dealloc(lk->hbso); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 209 | if (lk->heldBy) |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 210 | VG_(deleteBag)( lk->heldBy ); |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 211 | /* begin: del lock from double linked list */ |
| 212 | if (lk == admin_locks) { |
| 213 | tl_assert(lk->admin_prev == NULL); |
| 214 | if (lk->admin_next) |
| 215 | lk->admin_next->admin_prev = NULL; |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 216 | admin_locks = lk->admin_next; |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 217 | } |
| 218 | else { |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 219 | tl_assert(lk->admin_prev != NULL); |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 220 | lk->admin_prev->admin_next = lk->admin_next; |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 221 | if (lk->admin_next) |
| 222 | lk->admin_next->admin_prev = lk->admin_prev; |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 223 | } |
sewardj | 0f64c9e | 2011-03-10 17:40:22 +0000 | [diff] [blame] | 224 | /* end: del */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 225 | VG_(memset)(lk, 0xAA, sizeof(*lk)); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 226 | HG_(free)(lk); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* Update 'lk' to reflect that 'thr' now has a write-acquisition of |
| 230 | it. This is done strictly: only combinations resulting from |
| 231 | correct program and libpthread behaviour are allowed. */ |
| 232 | static void lockN_acquire_writer ( Lock* lk, Thread* thr ) |
| 233 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 234 | tl_assert(HG_(is_sane_LockN)(lk)); |
| 235 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 236 | |
| 237 | stats__lockN_acquires++; |
| 238 | |
| 239 | /* EXPOSITION only */ |
| 240 | /* We need to keep recording snapshots of where the lock was |
| 241 | acquired, so as to produce better lock-order error messages. */ |
| 242 | if (lk->acquired_at == NULL) { |
| 243 | ThreadId tid; |
| 244 | tl_assert(lk->heldBy == NULL); |
| 245 | tid = map_threads_maybe_reverse_lookup_SLOW(thr); |
| 246 | lk->acquired_at |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 247 | = VG_(record_ExeContext)(tid, 0/*first_ip_delta*/); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 248 | } else { |
| 249 | tl_assert(lk->heldBy != NULL); |
| 250 | } |
| 251 | /* end EXPOSITION only */ |
| 252 | |
| 253 | switch (lk->kind) { |
| 254 | case LK_nonRec: |
| 255 | case_LK_nonRec: |
| 256 | tl_assert(lk->heldBy == NULL); /* can't w-lock recursively */ |
| 257 | tl_assert(!lk->heldW); |
| 258 | lk->heldW = True; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 259 | lk->heldBy = VG_(newBag)( HG_(zalloc), "hg.lNaw.1", HG_(free) ); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 260 | VG_(addToBag)( lk->heldBy, (Word)thr ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 261 | break; |
| 262 | case LK_mbRec: |
| 263 | if (lk->heldBy == NULL) |
| 264 | goto case_LK_nonRec; |
| 265 | /* 2nd and subsequent locking of a lock by its owner */ |
| 266 | tl_assert(lk->heldW); |
| 267 | /* assert: lk is only held by one thread .. */ |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 268 | tl_assert(VG_(sizeUniqueBag(lk->heldBy)) == 1); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 269 | /* assert: .. and that thread is 'thr'. */ |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 270 | tl_assert(VG_(elemBag)(lk->heldBy, (Word)thr) |
| 271 | == VG_(sizeTotalBag)(lk->heldBy)); |
| 272 | VG_(addToBag)(lk->heldBy, (Word)thr); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 273 | break; |
| 274 | case LK_rdwr: |
| 275 | tl_assert(lk->heldBy == NULL && !lk->heldW); /* must be unheld */ |
| 276 | goto case_LK_nonRec; |
| 277 | default: |
| 278 | tl_assert(0); |
| 279 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 280 | tl_assert(HG_(is_sane_LockN)(lk)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static void lockN_acquire_reader ( Lock* lk, Thread* thr ) |
| 284 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 285 | tl_assert(HG_(is_sane_LockN)(lk)); |
| 286 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 287 | /* can only add reader to a reader-writer lock. */ |
| 288 | tl_assert(lk->kind == LK_rdwr); |
| 289 | /* lk must be free or already r-held. */ |
| 290 | tl_assert(lk->heldBy == NULL |
| 291 | || (lk->heldBy != NULL && !lk->heldW)); |
| 292 | |
| 293 | stats__lockN_acquires++; |
| 294 | |
| 295 | /* EXPOSITION only */ |
| 296 | /* We need to keep recording snapshots of where the lock was |
| 297 | acquired, so as to produce better lock-order error messages. */ |
| 298 | if (lk->acquired_at == NULL) { |
| 299 | ThreadId tid; |
| 300 | tl_assert(lk->heldBy == NULL); |
| 301 | tid = map_threads_maybe_reverse_lookup_SLOW(thr); |
| 302 | lk->acquired_at |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 303 | = VG_(record_ExeContext)(tid, 0/*first_ip_delta*/); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 304 | } else { |
| 305 | tl_assert(lk->heldBy != NULL); |
| 306 | } |
| 307 | /* end EXPOSITION only */ |
| 308 | |
| 309 | if (lk->heldBy) { |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 310 | VG_(addToBag)(lk->heldBy, (Word)thr); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 311 | } else { |
| 312 | lk->heldW = False; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 313 | lk->heldBy = VG_(newBag)( HG_(zalloc), "hg.lNar.1", HG_(free) ); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 314 | VG_(addToBag)( lk->heldBy, (Word)thr ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 315 | } |
| 316 | tl_assert(!lk->heldW); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 317 | tl_assert(HG_(is_sane_LockN)(lk)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* Update 'lk' to reflect a release of it by 'thr'. This is done |
| 321 | strictly: only combinations resulting from correct program and |
| 322 | libpthread behaviour are allowed. */ |
| 323 | |
| 324 | static void lockN_release ( Lock* lk, Thread* thr ) |
| 325 | { |
| 326 | Bool b; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 327 | tl_assert(HG_(is_sane_LockN)(lk)); |
| 328 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 329 | /* lock must be held by someone */ |
| 330 | tl_assert(lk->heldBy); |
| 331 | stats__lockN_releases++; |
| 332 | /* Remove it from the holder set */ |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 333 | b = VG_(delFromBag)(lk->heldBy, (Word)thr); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 334 | /* thr must actually have been a holder of lk */ |
| 335 | tl_assert(b); |
| 336 | /* normalise */ |
| 337 | tl_assert(lk->acquired_at); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 338 | if (VG_(isEmptyBag)(lk->heldBy)) { |
| 339 | VG_(deleteBag)(lk->heldBy); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 340 | lk->heldBy = NULL; |
| 341 | lk->heldW = False; |
| 342 | lk->acquired_at = NULL; |
| 343 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 344 | tl_assert(HG_(is_sane_LockN)(lk)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static void remove_Lock_from_locksets_of_all_owning_Threads( Lock* lk ) |
| 348 | { |
| 349 | Thread* thr; |
| 350 | if (!lk->heldBy) { |
| 351 | tl_assert(!lk->heldW); |
| 352 | return; |
| 353 | } |
| 354 | /* for each thread that holds this lock do ... */ |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 355 | VG_(initIterBag)( lk->heldBy ); |
| 356 | while (VG_(nextIterBag)( lk->heldBy, (Word*)&thr, NULL )) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 357 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 358 | tl_assert(HG_(elemWS)( univ_lsets, |
| 359 | thr->locksetA, (Word)lk )); |
| 360 | thr->locksetA |
| 361 | = HG_(delFromWS)( univ_lsets, thr->locksetA, (Word)lk ); |
| 362 | |
| 363 | if (lk->heldW) { |
| 364 | tl_assert(HG_(elemWS)( univ_lsets, |
| 365 | thr->locksetW, (Word)lk )); |
| 366 | thr->locksetW |
| 367 | = HG_(delFromWS)( univ_lsets, thr->locksetW, (Word)lk ); |
| 368 | } |
| 369 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 370 | VG_(doneIterBag)( lk->heldBy ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 371 | } |
| 372 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 373 | |
| 374 | /*----------------------------------------------------------------*/ |
| 375 | /*--- Print out the primary data structures ---*/ |
| 376 | /*----------------------------------------------------------------*/ |
| 377 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 378 | #define PP_THREADS (1<<1) |
| 379 | #define PP_LOCKS (1<<2) |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 380 | #define PP_ALL (PP_THREADS | PP_LOCKS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 381 | |
| 382 | |
| 383 | static const Int sHOW_ADMIN = 0; |
| 384 | |
| 385 | static void space ( Int n ) |
| 386 | { |
| 387 | Int i; |
| 388 | Char spaces[128+1]; |
| 389 | tl_assert(n >= 0 && n < 128); |
| 390 | if (n == 0) |
| 391 | return; |
| 392 | for (i = 0; i < n; i++) |
| 393 | spaces[i] = ' '; |
| 394 | spaces[i] = 0; |
| 395 | tl_assert(i < 128+1); |
| 396 | VG_(printf)("%s", spaces); |
| 397 | } |
| 398 | |
| 399 | static void pp_Thread ( Int d, Thread* t ) |
| 400 | { |
| 401 | space(d+0); VG_(printf)("Thread %p {\n", t); |
| 402 | if (sHOW_ADMIN) { |
| 403 | space(d+3); VG_(printf)("admin %p\n", t->admin); |
| 404 | space(d+3); VG_(printf)("magic 0x%x\n", (UInt)t->magic); |
| 405 | } |
| 406 | space(d+3); VG_(printf)("locksetA %d\n", (Int)t->locksetA); |
| 407 | space(d+3); VG_(printf)("locksetW %d\n", (Int)t->locksetW); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 408 | space(d+0); VG_(printf)("}\n"); |
| 409 | } |
| 410 | |
| 411 | static void pp_admin_threads ( Int d ) |
| 412 | { |
| 413 | Int i, n; |
| 414 | Thread* t; |
| 415 | for (n = 0, t = admin_threads; t; n++, t = t->admin) { |
| 416 | /* nothing */ |
| 417 | } |
| 418 | space(d); VG_(printf)("admin_threads (%d records) {\n", n); |
| 419 | for (i = 0, t = admin_threads; t; i++, t = t->admin) { |
| 420 | if (0) { |
| 421 | space(n); |
| 422 | VG_(printf)("admin_threads record %d of %d:\n", i, n); |
| 423 | } |
| 424 | pp_Thread(d+3, t); |
| 425 | } |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 426 | space(d); VG_(printf)("}\n"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static void pp_map_threads ( Int d ) |
| 430 | { |
njn | 4c245e5 | 2009-03-15 23:25:38 +0000 | [diff] [blame] | 431 | Int i, n = 0; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 432 | space(d); VG_(printf)("map_threads "); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 433 | for (i = 0; i < VG_N_THREADS; i++) { |
| 434 | if (map_threads[i] != NULL) |
| 435 | n++; |
| 436 | } |
| 437 | VG_(printf)("(%d entries) {\n", n); |
| 438 | for (i = 0; i < VG_N_THREADS; i++) { |
| 439 | if (map_threads[i] == NULL) |
| 440 | continue; |
| 441 | space(d+3); |
| 442 | VG_(printf)("coretid %d -> Thread %p\n", i, map_threads[i]); |
| 443 | } |
| 444 | space(d); VG_(printf)("}\n"); |
| 445 | } |
| 446 | |
| 447 | static const HChar* show_LockKind ( LockKind lkk ) { |
| 448 | switch (lkk) { |
| 449 | case LK_mbRec: return "mbRec"; |
| 450 | case LK_nonRec: return "nonRec"; |
| 451 | case LK_rdwr: return "rdwr"; |
| 452 | default: tl_assert(0); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | static void pp_Lock ( Int d, Lock* lk ) |
| 457 | { |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 458 | space(d+0); VG_(printf)("Lock %p (ga %#lx) {\n", lk, lk->guestaddr); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 459 | if (sHOW_ADMIN) { |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 460 | space(d+3); VG_(printf)("admin_n %p\n", lk->admin_next); |
| 461 | space(d+3); VG_(printf)("admin_p %p\n", lk->admin_prev); |
| 462 | space(d+3); VG_(printf)("magic 0x%x\n", (UInt)lk->magic); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 463 | } |
| 464 | space(d+3); VG_(printf)("unique %llu\n", lk->unique); |
| 465 | space(d+3); VG_(printf)("kind %s\n", show_LockKind(lk->kind)); |
| 466 | space(d+3); VG_(printf)("heldW %s\n", lk->heldW ? "yes" : "no"); |
| 467 | space(d+3); VG_(printf)("heldBy %p", lk->heldBy); |
| 468 | if (lk->heldBy) { |
| 469 | Thread* thr; |
| 470 | Word count; |
| 471 | VG_(printf)(" { "); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 472 | VG_(initIterBag)( lk->heldBy ); |
| 473 | while (VG_(nextIterBag)( lk->heldBy, (Word*)&thr, &count )) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 474 | VG_(printf)("%lu:%p ", count, thr); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 475 | VG_(doneIterBag)( lk->heldBy ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 476 | VG_(printf)("}"); |
| 477 | } |
| 478 | VG_(printf)("\n"); |
| 479 | space(d+0); VG_(printf)("}\n"); |
| 480 | } |
| 481 | |
| 482 | static void pp_admin_locks ( Int d ) |
| 483 | { |
| 484 | Int i, n; |
| 485 | Lock* lk; |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 486 | for (n = 0, lk = admin_locks; lk; n++, lk = lk->admin_next) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 487 | /* nothing */ |
| 488 | } |
| 489 | space(d); VG_(printf)("admin_locks (%d records) {\n", n); |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 490 | for (i = 0, lk = admin_locks; lk; i++, lk = lk->admin_next) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 491 | if (0) { |
| 492 | space(n); |
| 493 | VG_(printf)("admin_locks record %d of %d:\n", i, n); |
| 494 | } |
| 495 | pp_Lock(d+3, lk); |
| 496 | } |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 497 | space(d); VG_(printf)("}\n"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static void pp_map_locks ( Int d ) |
| 501 | { |
| 502 | void* gla; |
| 503 | Lock* lk; |
| 504 | space(d); VG_(printf)("map_locks (%d entries) {\n", |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 505 | (Int)VG_(sizeFM)( map_locks )); |
| 506 | VG_(initIterFM)( map_locks ); |
| 507 | while (VG_(nextIterFM)( map_locks, (Word*)&gla, |
sewardj | b5f2964 | 2007-11-16 12:02:43 +0000 | [diff] [blame] | 508 | (Word*)&lk )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 509 | space(d+3); |
| 510 | VG_(printf)("guest %p -> Lock %p\n", gla, lk); |
| 511 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 512 | VG_(doneIterFM)( map_locks ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 513 | space(d); VG_(printf)("}\n"); |
| 514 | } |
| 515 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 516 | static void pp_everything ( Int flags, Char* caller ) |
| 517 | { |
| 518 | Int d = 0; |
| 519 | VG_(printf)("\n"); |
| 520 | VG_(printf)("All_Data_Structures (caller = \"%s\") {\n", caller); |
| 521 | if (flags & PP_THREADS) { |
| 522 | VG_(printf)("\n"); |
| 523 | pp_admin_threads(d+3); |
| 524 | VG_(printf)("\n"); |
| 525 | pp_map_threads(d+3); |
| 526 | } |
| 527 | if (flags & PP_LOCKS) { |
| 528 | VG_(printf)("\n"); |
| 529 | pp_admin_locks(d+3); |
| 530 | VG_(printf)("\n"); |
| 531 | pp_map_locks(d+3); |
| 532 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 533 | |
| 534 | VG_(printf)("\n"); |
| 535 | VG_(printf)("}\n"); |
| 536 | VG_(printf)("\n"); |
| 537 | } |
| 538 | |
| 539 | #undef SHOW_ADMIN |
| 540 | |
| 541 | |
| 542 | /*----------------------------------------------------------------*/ |
| 543 | /*--- Initialise the primary data structures ---*/ |
| 544 | /*----------------------------------------------------------------*/ |
| 545 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 546 | static void initialise_data_structures ( Thr* hbthr_root ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 547 | { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 548 | Thread* thr; |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 549 | WordSetID wsid; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 550 | |
| 551 | /* Get everything initialised and zeroed. */ |
| 552 | tl_assert(admin_threads == NULL); |
| 553 | tl_assert(admin_locks == NULL); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 554 | |
| 555 | tl_assert(sizeof(Addr) == sizeof(Word)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 556 | |
| 557 | tl_assert(map_threads == NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 558 | map_threads = HG_(zalloc)( "hg.ids.1", VG_N_THREADS * sizeof(Thread*) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 559 | tl_assert(map_threads != NULL); |
| 560 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 561 | tl_assert(sizeof(Addr) == sizeof(Word)); |
| 562 | tl_assert(map_locks == NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 563 | map_locks = VG_(newFM)( HG_(zalloc), "hg.ids.2", HG_(free), |
| 564 | NULL/*unboxed Word cmp*/); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 565 | tl_assert(map_locks != NULL); |
| 566 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 567 | tl_assert(univ_lsets == NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 568 | univ_lsets = HG_(newWordSetU)( HG_(zalloc), "hg.ids.4", HG_(free), |
| 569 | 8/*cacheSize*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 570 | tl_assert(univ_lsets != NULL); |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 571 | /* Ensure that univ_lsets is non-empty, with lockset zero being the |
| 572 | empty lockset. hg_errors.c relies on the assumption that |
| 573 | lockset number zero in univ_lsets is always valid. */ |
| 574 | wsid = HG_(emptyWS)(univ_lsets); |
| 575 | tl_assert(wsid == 0); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 576 | |
| 577 | tl_assert(univ_laog == NULL); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 578 | if (HG_(clo_track_lockorders)) { |
| 579 | univ_laog = HG_(newWordSetU)( HG_(zalloc), "hg.ids.5 (univ_laog)", |
| 580 | HG_(free), 24/*cacheSize*/ ); |
| 581 | tl_assert(univ_laog != NULL); |
| 582 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 583 | |
| 584 | /* Set up entries for the root thread */ |
| 585 | // FIXME: this assumes that the first real ThreadId is 1 |
| 586 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 587 | /* a Thread for the new thread ... */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 588 | thr = mk_Thread(hbthr_root); |
| 589 | thr->coretid = 1; /* FIXME: hardwires an assumption about the |
| 590 | identity of the root thread. */ |
sewardj | 6062664 | 2011-03-10 15:14:37 +0000 | [diff] [blame] | 591 | tl_assert( libhb_get_Thr_hgthread(hbthr_root) == NULL ); |
| 592 | libhb_set_Thr_hgthread(hbthr_root, thr); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 593 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 594 | /* and bind it in the thread-map table. */ |
| 595 | tl_assert(HG_(is_sane_ThreadId)(thr->coretid)); |
| 596 | tl_assert(thr->coretid != VG_INVALID_THREADID); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 597 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 598 | map_threads[thr->coretid] = thr; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 599 | |
| 600 | tl_assert(VG_INVALID_THREADID == 0); |
| 601 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 602 | all__sanity_check("initialise_data_structures"); |
| 603 | } |
| 604 | |
| 605 | |
| 606 | /*----------------------------------------------------------------*/ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 607 | /*--- map_threads :: array[core-ThreadId] of Thread* ---*/ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 608 | /*----------------------------------------------------------------*/ |
| 609 | |
| 610 | /* Doesn't assert if the relevant map_threads entry is NULL. */ |
| 611 | static Thread* map_threads_maybe_lookup ( ThreadId coretid ) |
| 612 | { |
| 613 | Thread* thr; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 614 | tl_assert( HG_(is_sane_ThreadId)(coretid) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 615 | thr = map_threads[coretid]; |
| 616 | return thr; |
| 617 | } |
| 618 | |
| 619 | /* Asserts if the relevant map_threads entry is NULL. */ |
| 620 | static inline Thread* map_threads_lookup ( ThreadId coretid ) |
| 621 | { |
| 622 | Thread* thr; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 623 | tl_assert( HG_(is_sane_ThreadId)(coretid) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 624 | thr = map_threads[coretid]; |
| 625 | tl_assert(thr); |
| 626 | return thr; |
| 627 | } |
| 628 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 629 | /* Do a reverse lookup. Does not assert if 'thr' is not found in |
| 630 | map_threads. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 631 | static ThreadId map_threads_maybe_reverse_lookup_SLOW ( Thread* thr ) |
| 632 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 633 | ThreadId tid; |
| 634 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 635 | /* Check nobody used the invalid-threadid slot */ |
| 636 | tl_assert(VG_INVALID_THREADID >= 0 && VG_INVALID_THREADID < VG_N_THREADS); |
| 637 | tl_assert(map_threads[VG_INVALID_THREADID] == NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 638 | tid = thr->coretid; |
| 639 | tl_assert(HG_(is_sane_ThreadId)(tid)); |
| 640 | return tid; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /* Do a reverse lookup. Warning: POTENTIALLY SLOW. Asserts if 'thr' |
| 644 | is not found in map_threads. */ |
| 645 | static ThreadId map_threads_reverse_lookup_SLOW ( Thread* thr ) |
| 646 | { |
| 647 | ThreadId tid = map_threads_maybe_reverse_lookup_SLOW( thr ); |
| 648 | tl_assert(tid != VG_INVALID_THREADID); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 649 | tl_assert(map_threads[tid]); |
| 650 | tl_assert(map_threads[tid]->coretid == tid); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 651 | return tid; |
| 652 | } |
| 653 | |
| 654 | static void map_threads_delete ( ThreadId coretid ) |
| 655 | { |
| 656 | Thread* thr; |
| 657 | tl_assert(coretid != 0); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 658 | tl_assert( HG_(is_sane_ThreadId)(coretid) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 659 | thr = map_threads[coretid]; |
| 660 | tl_assert(thr); |
| 661 | map_threads[coretid] = NULL; |
| 662 | } |
| 663 | |
| 664 | |
| 665 | /*----------------------------------------------------------------*/ |
| 666 | /*--- map_locks :: WordFM guest-Addr-of-lock Lock* ---*/ |
| 667 | /*----------------------------------------------------------------*/ |
| 668 | |
| 669 | /* Make sure there is a lock table entry for the given (lock) guest |
| 670 | address. If not, create one of the stated 'kind' in unheld state. |
| 671 | In any case, return the address of the existing or new Lock. */ |
| 672 | static |
| 673 | Lock* map_locks_lookup_or_create ( LockKind lkk, Addr ga, ThreadId tid ) |
| 674 | { |
| 675 | Bool found; |
| 676 | Lock* oldlock = NULL; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 677 | tl_assert(HG_(is_sane_ThreadId)(tid)); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 678 | found = VG_(lookupFM)( map_locks, |
sewardj | b5f2964 | 2007-11-16 12:02:43 +0000 | [diff] [blame] | 679 | NULL, (Word*)&oldlock, (Word)ga ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 680 | if (!found) { |
| 681 | Lock* lock = mk_LockN(lkk, ga); |
| 682 | lock->appeared_at = VG_(record_ExeContext)( tid, 0 ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 683 | tl_assert(HG_(is_sane_LockN)(lock)); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 684 | VG_(addToFM)( map_locks, (Word)ga, (Word)lock ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 685 | tl_assert(oldlock == NULL); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 686 | return lock; |
| 687 | } else { |
| 688 | tl_assert(oldlock != NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 689 | tl_assert(HG_(is_sane_LockN)(oldlock)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 690 | tl_assert(oldlock->guestaddr == ga); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 691 | return oldlock; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | static Lock* map_locks_maybe_lookup ( Addr ga ) |
| 696 | { |
| 697 | Bool found; |
| 698 | Lock* lk = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 699 | found = VG_(lookupFM)( map_locks, NULL, (Word*)&lk, (Word)ga ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 700 | tl_assert(found ? lk != NULL : lk == NULL); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 701 | return lk; |
| 702 | } |
| 703 | |
| 704 | static void map_locks_delete ( Addr ga ) |
| 705 | { |
| 706 | Addr ga2 = 0; |
| 707 | Lock* lk = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 708 | VG_(delFromFM)( map_locks, |
sewardj | b5f2964 | 2007-11-16 12:02:43 +0000 | [diff] [blame] | 709 | (Word*)&ga2, (Word*)&lk, (Word)ga ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 710 | /* delFromFM produces the val which is being deleted, if it is |
| 711 | found. So assert it is non-null; that in effect asserts that we |
| 712 | are deleting a (ga, Lock) pair which actually exists. */ |
| 713 | tl_assert(lk != NULL); |
| 714 | tl_assert(ga2 == ga); |
| 715 | } |
| 716 | |
| 717 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 718 | |
| 719 | /*----------------------------------------------------------------*/ |
| 720 | /*--- Sanity checking the data structures ---*/ |
| 721 | /*----------------------------------------------------------------*/ |
| 722 | |
| 723 | static UWord stats__sanity_checks = 0; |
| 724 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 725 | static void laog__sanity_check ( Char* who ); /* fwds */ |
| 726 | |
| 727 | /* REQUIRED INVARIANTS: |
| 728 | |
| 729 | Thread vs Segment/Lock/SecMaps |
| 730 | |
| 731 | for each t in Threads { |
| 732 | |
| 733 | // Thread.lockset: each element is really a valid Lock |
| 734 | |
| 735 | // Thread.lockset: each Lock in set is actually held by that thread |
| 736 | for lk in Thread.lockset |
| 737 | lk == LockedBy(t) |
| 738 | |
| 739 | // Thread.csegid is a valid SegmentID |
| 740 | // and the associated Segment has .thr == t |
| 741 | |
| 742 | } |
| 743 | |
| 744 | all thread Locksets are pairwise empty under intersection |
| 745 | (that is, no lock is claimed to be held by more than one thread) |
| 746 | -- this is guaranteed if all locks in locksets point back to their |
| 747 | owner threads |
| 748 | |
| 749 | Lock vs Thread/Segment/SecMaps |
| 750 | |
| 751 | for each entry (gla, la) in map_locks |
| 752 | gla == la->guest_addr |
| 753 | |
| 754 | for each lk in Locks { |
| 755 | |
| 756 | lk->tag is valid |
| 757 | lk->guest_addr does not have shadow state NoAccess |
| 758 | if lk == LockedBy(t), then t->lockset contains lk |
| 759 | if lk == UnlockedBy(segid) then segid is valid SegmentID |
| 760 | and can be mapped to a valid Segment(seg) |
| 761 | and seg->thr->lockset does not contain lk |
| 762 | if lk == UnlockedNew then (no lockset contains lk) |
| 763 | |
| 764 | secmaps for lk has .mbHasLocks == True |
| 765 | |
| 766 | } |
| 767 | |
| 768 | Segment vs Thread/Lock/SecMaps |
| 769 | |
| 770 | the Segment graph is a dag (no cycles) |
| 771 | all of the Segment graph must be reachable from the segids |
| 772 | mentioned in the Threads |
| 773 | |
| 774 | for seg in Segments { |
| 775 | |
| 776 | seg->thr is a sane Thread |
| 777 | |
| 778 | } |
| 779 | |
| 780 | SecMaps vs Segment/Thread/Lock |
| 781 | |
| 782 | for sm in SecMaps { |
| 783 | |
| 784 | sm properly aligned |
| 785 | if any shadow word is ShR or ShM then .mbHasShared == True |
| 786 | |
| 787 | for each Excl(segid) state |
| 788 | map_segments_lookup maps to a sane Segment(seg) |
| 789 | for each ShM/ShR(tsetid,lsetid) state |
| 790 | each lk in lset is a valid Lock |
| 791 | each thr in tset is a valid thread, which is non-dead |
| 792 | |
| 793 | } |
| 794 | */ |
| 795 | |
| 796 | |
| 797 | /* Return True iff 'thr' holds 'lk' in some mode. */ |
| 798 | static Bool thread_is_a_holder_of_Lock ( Thread* thr, Lock* lk ) |
| 799 | { |
| 800 | if (lk->heldBy) |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 801 | return VG_(elemBag)( lk->heldBy, (Word)thr ) > 0; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 802 | else |
| 803 | return False; |
| 804 | } |
| 805 | |
| 806 | /* Sanity check Threads, as far as possible */ |
| 807 | __attribute__((noinline)) |
| 808 | static void threads__sanity_check ( Char* who ) |
| 809 | { |
| 810 | #define BAD(_str) do { how = (_str); goto bad; } while (0) |
| 811 | Char* how = "no error"; |
| 812 | Thread* thr; |
| 813 | WordSetID wsA, wsW; |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 814 | UWord* ls_words; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 815 | Word ls_size, i; |
| 816 | Lock* lk; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 817 | for (thr = admin_threads; thr; thr = thr->admin) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 818 | if (!HG_(is_sane_Thread)(thr)) BAD("1"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 819 | wsA = thr->locksetA; |
| 820 | wsW = thr->locksetW; |
| 821 | // locks held in W mode are a subset of all locks held |
| 822 | if (!HG_(isSubsetOf)( univ_lsets, wsW, wsA )) BAD("7"); |
| 823 | HG_(getPayloadWS)( &ls_words, &ls_size, univ_lsets, wsA ); |
| 824 | for (i = 0; i < ls_size; i++) { |
| 825 | lk = (Lock*)ls_words[i]; |
| 826 | // Thread.lockset: each element is really a valid Lock |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 827 | if (!HG_(is_sane_LockN)(lk)) BAD("2"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 828 | // Thread.lockset: each Lock in set is actually held by that |
| 829 | // thread |
| 830 | if (!thread_is_a_holder_of_Lock(thr,lk)) BAD("3"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | return; |
| 834 | bad: |
| 835 | VG_(printf)("threads__sanity_check: who=\"%s\", bad=\"%s\"\n", who, how); |
| 836 | tl_assert(0); |
| 837 | #undef BAD |
| 838 | } |
| 839 | |
| 840 | |
| 841 | /* Sanity check Locks, as far as possible */ |
| 842 | __attribute__((noinline)) |
| 843 | static void locks__sanity_check ( Char* who ) |
| 844 | { |
| 845 | #define BAD(_str) do { how = (_str); goto bad; } while (0) |
| 846 | Char* how = "no error"; |
| 847 | Addr gla; |
| 848 | Lock* lk; |
| 849 | Int i; |
| 850 | // # entries in admin_locks == # entries in map_locks |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 851 | for (i = 0, lk = admin_locks; lk; i++, lk = lk->admin_next) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 852 | ; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 853 | if (i != VG_(sizeFM)(map_locks)) BAD("1"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 854 | // for each entry (gla, lk) in map_locks |
| 855 | // gla == lk->guest_addr |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 856 | VG_(initIterFM)( map_locks ); |
| 857 | while (VG_(nextIterFM)( map_locks, |
sewardj | b5f2964 | 2007-11-16 12:02:43 +0000 | [diff] [blame] | 858 | (Word*)&gla, (Word*)&lk )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 859 | if (lk->guestaddr != gla) BAD("2"); |
| 860 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 861 | VG_(doneIterFM)( map_locks ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 862 | // scan through admin_locks ... |
sewardj | 1d7c332 | 2011-02-28 09:22:51 +0000 | [diff] [blame] | 863 | for (lk = admin_locks; lk; lk = lk->admin_next) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 864 | // lock is sane. Quite comprehensive, also checks that |
| 865 | // referenced (holder) threads are sane. |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 866 | if (!HG_(is_sane_LockN)(lk)) BAD("3"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 867 | // map_locks binds guest address back to this lock |
| 868 | if (lk != map_locks_maybe_lookup(lk->guestaddr)) BAD("4"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 869 | // look at all threads mentioned as holders of this lock. Ensure |
| 870 | // this lock is mentioned in their locksets. |
| 871 | if (lk->heldBy) { |
| 872 | Thread* thr; |
| 873 | Word count; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 874 | VG_(initIterBag)( lk->heldBy ); |
| 875 | while (VG_(nextIterBag)( lk->heldBy, |
sewardj | b5f2964 | 2007-11-16 12:02:43 +0000 | [diff] [blame] | 876 | (Word*)&thr, &count )) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 877 | // HG_(is_sane_LockN) above ensures these |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 878 | tl_assert(count >= 1); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 879 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 880 | if (!HG_(elemWS)(univ_lsets, thr->locksetA, (Word)lk)) |
| 881 | BAD("6"); |
| 882 | // also check the w-only lockset |
| 883 | if (lk->heldW |
| 884 | && !HG_(elemWS)(univ_lsets, thr->locksetW, (Word)lk)) |
| 885 | BAD("7"); |
| 886 | if ((!lk->heldW) |
| 887 | && HG_(elemWS)(univ_lsets, thr->locksetW, (Word)lk)) |
| 888 | BAD("8"); |
| 889 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 890 | VG_(doneIterBag)( lk->heldBy ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 891 | } else { |
| 892 | /* lock not held by anybody */ |
| 893 | if (lk->heldW) BAD("9"); /* should be False if !heldBy */ |
| 894 | // since lk is unheld, then (no lockset contains lk) |
| 895 | // hmm, this is really too expensive to check. Hmm. |
| 896 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | return; |
| 900 | bad: |
| 901 | VG_(printf)("locks__sanity_check: who=\"%s\", bad=\"%s\"\n", who, how); |
| 902 | tl_assert(0); |
| 903 | #undef BAD |
| 904 | } |
| 905 | |
| 906 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 907 | static void all_except_Locks__sanity_check ( Char* who ) { |
| 908 | stats__sanity_checks++; |
| 909 | if (0) VG_(printf)("all_except_Locks__sanity_check(%s)\n", who); |
| 910 | threads__sanity_check(who); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 911 | if (HG_(clo_track_lockorders)) |
| 912 | laog__sanity_check(who); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 913 | } |
| 914 | static void all__sanity_check ( Char* who ) { |
| 915 | all_except_Locks__sanity_check(who); |
| 916 | locks__sanity_check(who); |
| 917 | } |
| 918 | |
| 919 | |
| 920 | /*----------------------------------------------------------------*/ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 921 | /*--- Shadow value and address range handlers ---*/ |
| 922 | /*----------------------------------------------------------------*/ |
| 923 | |
| 924 | static void laog__pre_thread_acquires_lock ( Thread*, Lock* ); /* fwds */ |
sewardj | 1cbc12f | 2008-11-10 16:16:46 +0000 | [diff] [blame] | 925 | //static void laog__handle_lock_deletions ( WordSetID ); /* fwds */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 926 | static inline Thread* get_current_Thread ( void ); /* fwds */ |
sewardj | 1cbc12f | 2008-11-10 16:16:46 +0000 | [diff] [blame] | 927 | __attribute__((noinline)) |
| 928 | static void laog__handle_one_lock_deletion ( Lock* lk ); /* fwds */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 929 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 930 | |
| 931 | /* Block-copy states (needed for implementing realloc()). */ |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 932 | /* FIXME this copies shadow memory; it doesn't apply the MSM to it. |
| 933 | Is that a problem? (hence 'scopy' rather than 'ccopy') */ |
| 934 | static void shadow_mem_scopy_range ( Thread* thr, |
| 935 | Addr src, Addr dst, SizeT len ) |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 936 | { |
| 937 | Thr* hbthr = thr->hbthr; |
| 938 | tl_assert(hbthr); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 939 | libhb_copy_shadow_state( hbthr, src, dst, len ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 940 | } |
| 941 | |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 942 | static void shadow_mem_cread_range ( Thread* thr, Addr a, SizeT len ) |
| 943 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 944 | Thr* hbthr = thr->hbthr; |
| 945 | tl_assert(hbthr); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 946 | LIBHB_CREAD_N(hbthr, a, len); |
| 947 | } |
| 948 | |
| 949 | static void shadow_mem_cwrite_range ( Thread* thr, Addr a, SizeT len ) { |
| 950 | Thr* hbthr = thr->hbthr; |
| 951 | tl_assert(hbthr); |
| 952 | LIBHB_CWRITE_N(hbthr, a, len); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | static void shadow_mem_make_New ( Thread* thr, Addr a, SizeT len ) |
| 956 | { |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 957 | libhb_srange_new( thr->hbthr, a, len ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 958 | } |
| 959 | |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 960 | static void shadow_mem_make_NoAccess_NoFX ( Thread* thr, Addr aIN, SizeT len ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 961 | { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 962 | if (0 && len > 500) |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 963 | VG_(printf)("make NoAccess_NoFX ( %#lx, %ld )\n", aIN, len ); |
| 964 | // has no effect (NoFX) |
| 965 | libhb_srange_noaccess_NoFX( thr->hbthr, aIN, len ); |
| 966 | } |
| 967 | |
| 968 | static void shadow_mem_make_NoAccess_AHAE ( Thread* thr, Addr aIN, SizeT len ) |
| 969 | { |
| 970 | if (0 && len > 500) |
| 971 | VG_(printf)("make NoAccess_AHAE ( %#lx, %ld )\n", aIN, len ); |
| 972 | // Actually Has An Effect (AHAE) |
| 973 | libhb_srange_noaccess_AHAE( thr->hbthr, aIN, len ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 974 | } |
| 975 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 976 | static void shadow_mem_make_Untracked ( Thread* thr, Addr aIN, SizeT len ) |
| 977 | { |
| 978 | if (0 && len > 500) |
| 979 | VG_(printf)("make Untracked ( %#lx, %ld )\n", aIN, len ); |
| 980 | libhb_srange_untrack( thr->hbthr, aIN, len ); |
| 981 | } |
| 982 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 983 | |
| 984 | /*----------------------------------------------------------------*/ |
| 985 | /*--- Event handlers (evh__* functions) ---*/ |
| 986 | /*--- plus helpers (evhH__* functions) ---*/ |
| 987 | /*----------------------------------------------------------------*/ |
| 988 | |
| 989 | /*--------- Event handler helpers (evhH__* functions) ---------*/ |
| 990 | |
| 991 | /* Create a new segment for 'thr', making it depend (.prev) on its |
| 992 | existing segment, bind together the SegmentID and Segment, and |
| 993 | return both of them. Also update 'thr' so it references the new |
| 994 | Segment. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 995 | //zz static |
| 996 | //zz void evhH__start_new_segment_for_thread ( /*OUT*/SegmentID* new_segidP, |
| 997 | //zz /*OUT*/Segment** new_segP, |
| 998 | //zz Thread* thr ) |
| 999 | //zz { |
| 1000 | //zz Segment* cur_seg; |
| 1001 | //zz tl_assert(new_segP); |
| 1002 | //zz tl_assert(new_segidP); |
| 1003 | //zz tl_assert(HG_(is_sane_Thread)(thr)); |
| 1004 | //zz cur_seg = map_segments_lookup( thr->csegid ); |
| 1005 | //zz tl_assert(cur_seg); |
| 1006 | //zz tl_assert(cur_seg->thr == thr); /* all sane segs should point back |
| 1007 | //zz at their owner thread. */ |
| 1008 | //zz *new_segP = mk_Segment( thr, cur_seg, NULL/*other*/ ); |
| 1009 | //zz *new_segidP = alloc_SegmentID(); |
| 1010 | //zz map_segments_add( *new_segidP, *new_segP ); |
| 1011 | //zz thr->csegid = *new_segidP; |
| 1012 | //zz } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1013 | |
| 1014 | |
| 1015 | /* The lock at 'lock_ga' has acquired a writer. Make all necessary |
| 1016 | updates, and also do all possible error checks. */ |
| 1017 | static |
| 1018 | void evhH__post_thread_w_acquires_lock ( Thread* thr, |
| 1019 | LockKind lkk, Addr lock_ga ) |
| 1020 | { |
| 1021 | Lock* lk; |
| 1022 | |
| 1023 | /* Basically what we need to do is call lockN_acquire_writer. |
| 1024 | However, that will barf if any 'invalid' lock states would |
| 1025 | result. Therefore check before calling. Side effect is that |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1026 | '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] | 1027 | routine. |
| 1028 | |
| 1029 | Because this routine is only called after successful lock |
| 1030 | acquisition, we should not be asked to move the lock into any |
| 1031 | invalid states. Requests to do so are bugs in libpthread, since |
| 1032 | that should have rejected any such requests. */ |
| 1033 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1034 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1035 | /* Try to find the lock. If we can't, then create a new one with |
| 1036 | kind 'lkk'. */ |
| 1037 | lk = map_locks_lookup_or_create( |
| 1038 | lkk, lock_ga, map_threads_reverse_lookup_SLOW(thr) ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1039 | tl_assert( HG_(is_sane_LockN)(lk) ); |
| 1040 | |
| 1041 | /* check libhb level entities exist */ |
| 1042 | tl_assert(thr->hbthr); |
| 1043 | tl_assert(lk->hbso); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1044 | |
| 1045 | if (lk->heldBy == NULL) { |
| 1046 | /* the lock isn't held. Simple. */ |
| 1047 | tl_assert(!lk->heldW); |
| 1048 | lockN_acquire_writer( lk, thr ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1049 | /* acquire a dependency from the lock's VCs */ |
| 1050 | libhb_so_recv( thr->hbthr, lk->hbso, True/*strong_recv*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1051 | goto noerror; |
| 1052 | } |
| 1053 | |
| 1054 | /* So the lock is already held. If held as a r-lock then |
| 1055 | libpthread must be buggy. */ |
| 1056 | tl_assert(lk->heldBy); |
| 1057 | if (!lk->heldW) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1058 | HG_(record_error_Misc)( |
| 1059 | thr, "Bug in libpthread: write lock " |
| 1060 | "granted on rwlock which is currently rd-held"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1061 | goto error; |
| 1062 | } |
| 1063 | |
| 1064 | /* So the lock is held in w-mode. If it's held by some other |
| 1065 | thread, then libpthread must be buggy. */ |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 1066 | tl_assert(VG_(sizeUniqueBag)(lk->heldBy) == 1); /* from precondition */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1067 | |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 1068 | if (thr != (Thread*)VG_(anyElementOfBag)(lk->heldBy)) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1069 | HG_(record_error_Misc)( |
| 1070 | thr, "Bug in libpthread: write lock " |
| 1071 | "granted on mutex/rwlock which is currently " |
| 1072 | "wr-held by a different thread"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1073 | goto error; |
| 1074 | } |
| 1075 | |
| 1076 | /* So the lock is already held in w-mode by 'thr'. That means this |
| 1077 | is an attempt to lock it recursively, which is only allowable |
| 1078 | for LK_mbRec kinded locks. Since this routine is called only |
| 1079 | once the lock has been acquired, this must also be a libpthread |
| 1080 | bug. */ |
| 1081 | if (lk->kind != LK_mbRec) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1082 | HG_(record_error_Misc)( |
| 1083 | thr, "Bug in libpthread: recursive write lock " |
| 1084 | "granted on mutex/wrlock which does not " |
| 1085 | "support recursion"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1086 | goto error; |
| 1087 | } |
| 1088 | |
| 1089 | /* So we are recursively re-locking a lock we already w-hold. */ |
| 1090 | lockN_acquire_writer( lk, thr ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1091 | /* acquire a dependency from the lock's VC. Probably pointless, |
| 1092 | but also harmless. */ |
| 1093 | libhb_so_recv( thr->hbthr, lk->hbso, True/*strong_recv*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1094 | goto noerror; |
| 1095 | |
| 1096 | noerror: |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 1097 | if (HG_(clo_track_lockorders)) { |
| 1098 | /* check lock order acquisition graph, and update. This has to |
| 1099 | happen before the lock is added to the thread's locksetA/W. */ |
| 1100 | laog__pre_thread_acquires_lock( thr, lk ); |
| 1101 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1102 | /* update the thread's held-locks set */ |
| 1103 | thr->locksetA = HG_(addToWS)( univ_lsets, thr->locksetA, (Word)lk ); |
| 1104 | thr->locksetW = HG_(addToWS)( univ_lsets, thr->locksetW, (Word)lk ); |
| 1105 | /* fall through */ |
| 1106 | |
| 1107 | error: |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1108 | tl_assert(HG_(is_sane_LockN)(lk)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | /* The lock at 'lock_ga' has acquired a reader. Make all necessary |
| 1113 | updates, and also do all possible error checks. */ |
| 1114 | static |
| 1115 | void evhH__post_thread_r_acquires_lock ( Thread* thr, |
| 1116 | LockKind lkk, Addr lock_ga ) |
| 1117 | { |
| 1118 | Lock* lk; |
| 1119 | |
| 1120 | /* Basically what we need to do is call lockN_acquire_reader. |
| 1121 | However, that will barf if any 'invalid' lock states would |
| 1122 | result. Therefore check before calling. Side effect is that |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1123 | '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] | 1124 | routine. |
| 1125 | |
| 1126 | Because this routine is only called after successful lock |
| 1127 | acquisition, we should not be asked to move the lock into any |
| 1128 | invalid states. Requests to do so are bugs in libpthread, since |
| 1129 | that should have rejected any such requests. */ |
| 1130 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1131 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1132 | /* Try to find the lock. If we can't, then create a new one with |
| 1133 | kind 'lkk'. Only a reader-writer lock can be read-locked, |
| 1134 | hence the first assertion. */ |
| 1135 | tl_assert(lkk == LK_rdwr); |
| 1136 | lk = map_locks_lookup_or_create( |
| 1137 | lkk, lock_ga, map_threads_reverse_lookup_SLOW(thr) ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1138 | tl_assert( HG_(is_sane_LockN)(lk) ); |
| 1139 | |
| 1140 | /* check libhb level entities exist */ |
| 1141 | tl_assert(thr->hbthr); |
| 1142 | tl_assert(lk->hbso); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1143 | |
| 1144 | if (lk->heldBy == NULL) { |
| 1145 | /* the lock isn't held. Simple. */ |
| 1146 | tl_assert(!lk->heldW); |
| 1147 | lockN_acquire_reader( lk, thr ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1148 | /* acquire a dependency from the lock's VC */ |
| 1149 | libhb_so_recv( thr->hbthr, lk->hbso, False/*!strong_recv*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1150 | goto noerror; |
| 1151 | } |
| 1152 | |
| 1153 | /* So the lock is already held. If held as a w-lock then |
| 1154 | libpthread must be buggy. */ |
| 1155 | tl_assert(lk->heldBy); |
| 1156 | if (lk->heldW) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1157 | HG_(record_error_Misc)( thr, "Bug in libpthread: read lock " |
| 1158 | "granted on rwlock which is " |
| 1159 | "currently wr-held"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1160 | goto error; |
| 1161 | } |
| 1162 | |
| 1163 | /* Easy enough. In short anybody can get a read-lock on a rwlock |
| 1164 | provided it is either unlocked or already in rd-held. */ |
| 1165 | lockN_acquire_reader( lk, thr ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1166 | /* acquire a dependency from the lock's VC. Probably pointless, |
| 1167 | but also harmless. */ |
| 1168 | libhb_so_recv( thr->hbthr, lk->hbso, False/*!strong_recv*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1169 | goto noerror; |
| 1170 | |
| 1171 | noerror: |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 1172 | if (HG_(clo_track_lockorders)) { |
| 1173 | /* check lock order acquisition graph, and update. This has to |
| 1174 | happen before the lock is added to the thread's locksetA/W. */ |
| 1175 | laog__pre_thread_acquires_lock( thr, lk ); |
| 1176 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1177 | /* update the thread's held-locks set */ |
| 1178 | thr->locksetA = HG_(addToWS)( univ_lsets, thr->locksetA, (Word)lk ); |
| 1179 | /* but don't update thr->locksetW, since lk is only rd-held */ |
| 1180 | /* fall through */ |
| 1181 | |
| 1182 | error: |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1183 | tl_assert(HG_(is_sane_LockN)(lk)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | /* The lock at 'lock_ga' is just about to be unlocked. Make all |
| 1188 | necessary updates, and also do all possible error checks. */ |
| 1189 | static |
| 1190 | void evhH__pre_thread_releases_lock ( Thread* thr, |
| 1191 | Addr lock_ga, Bool isRDWR ) |
| 1192 | { |
| 1193 | Lock* lock; |
| 1194 | Word n; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1195 | Bool was_heldW; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1196 | |
| 1197 | /* This routine is called prior to a lock release, before |
| 1198 | libpthread has had a chance to validate the call. Hence we need |
| 1199 | to detect and reject any attempts to move the lock into an |
| 1200 | invalid state. Such attempts are bugs in the client. |
| 1201 | |
| 1202 | isRDWR is True if we know from the wrapper context that lock_ga |
| 1203 | should refer to a reader-writer lock, and is False if [ditto] |
| 1204 | lock_ga should refer to a standard mutex. */ |
| 1205 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1206 | tl_assert(HG_(is_sane_Thread)(thr)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1207 | lock = map_locks_maybe_lookup( lock_ga ); |
| 1208 | |
| 1209 | if (!lock) { |
| 1210 | /* We know nothing about a lock at 'lock_ga'. Nevertheless |
| 1211 | the client is trying to unlock it. So complain, then ignore |
| 1212 | the attempt. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1213 | HG_(record_error_UnlockBogus)( thr, lock_ga ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1214 | return; |
| 1215 | } |
| 1216 | |
| 1217 | tl_assert(lock->guestaddr == lock_ga); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1218 | tl_assert(HG_(is_sane_LockN)(lock)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1219 | |
| 1220 | if (isRDWR && lock->kind != LK_rdwr) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1221 | HG_(record_error_Misc)( thr, "pthread_rwlock_unlock with a " |
| 1222 | "pthread_mutex_t* argument " ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1223 | } |
| 1224 | if ((!isRDWR) && lock->kind == LK_rdwr) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1225 | HG_(record_error_Misc)( thr, "pthread_mutex_unlock with a " |
| 1226 | "pthread_rwlock_t* argument " ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | if (!lock->heldBy) { |
| 1230 | /* The lock is not held. This indicates a serious bug in the |
| 1231 | client. */ |
| 1232 | tl_assert(!lock->heldW); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1233 | HG_(record_error_UnlockUnlocked)( thr, lock ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1234 | tl_assert(!HG_(elemWS)( univ_lsets, thr->locksetA, (Word)lock )); |
| 1235 | tl_assert(!HG_(elemWS)( univ_lsets, thr->locksetW, (Word)lock )); |
| 1236 | goto error; |
| 1237 | } |
| 1238 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1239 | /* test just above dominates */ |
| 1240 | tl_assert(lock->heldBy); |
| 1241 | was_heldW = lock->heldW; |
| 1242 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1243 | /* The lock is held. Is this thread one of the holders? If not, |
| 1244 | report a bug in the client. */ |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 1245 | n = VG_(elemBag)( lock->heldBy, (Word)thr ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1246 | tl_assert(n >= 0); |
| 1247 | if (n == 0) { |
| 1248 | /* We are not a current holder of the lock. This is a bug in |
| 1249 | the guest, and (per POSIX pthread rules) the unlock |
| 1250 | attempt will fail. So just complain and do nothing |
| 1251 | else. */ |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 1252 | Thread* realOwner = (Thread*)VG_(anyElementOfBag)( lock->heldBy ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1253 | tl_assert(HG_(is_sane_Thread)(realOwner)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1254 | tl_assert(realOwner != thr); |
| 1255 | tl_assert(!HG_(elemWS)( univ_lsets, thr->locksetA, (Word)lock )); |
| 1256 | tl_assert(!HG_(elemWS)( univ_lsets, thr->locksetW, (Word)lock )); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1257 | HG_(record_error_UnlockForeign)( thr, realOwner, lock ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1258 | goto error; |
| 1259 | } |
| 1260 | |
| 1261 | /* Ok, we hold the lock 'n' times. */ |
| 1262 | tl_assert(n >= 1); |
| 1263 | |
| 1264 | lockN_release( lock, thr ); |
| 1265 | |
| 1266 | n--; |
| 1267 | tl_assert(n >= 0); |
| 1268 | |
| 1269 | if (n > 0) { |
| 1270 | tl_assert(lock->heldBy); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 1271 | tl_assert(n == VG_(elemBag)( lock->heldBy, (Word)thr )); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1272 | /* We still hold the lock. So either it's a recursive lock |
| 1273 | or a rwlock which is currently r-held. */ |
| 1274 | tl_assert(lock->kind == LK_mbRec |
| 1275 | || (lock->kind == LK_rdwr && !lock->heldW)); |
| 1276 | tl_assert(HG_(elemWS)( univ_lsets, thr->locksetA, (Word)lock )); |
| 1277 | if (lock->heldW) |
| 1278 | tl_assert(HG_(elemWS)( univ_lsets, thr->locksetW, (Word)lock )); |
| 1279 | else |
| 1280 | tl_assert(!HG_(elemWS)( univ_lsets, thr->locksetW, (Word)lock )); |
| 1281 | } else { |
sewardj | 983f302 | 2009-05-21 14:49:55 +0000 | [diff] [blame] | 1282 | /* n is zero. This means we don't hold the lock any more. But |
| 1283 | if it's a rwlock held in r-mode, someone else could still |
| 1284 | hold it. Just do whatever sanity checks we can. */ |
| 1285 | if (lock->kind == LK_rdwr && lock->heldBy) { |
| 1286 | /* It's a rwlock. We no longer hold it but we used to; |
| 1287 | nevertheless it still appears to be held by someone else. |
| 1288 | The implication is that, prior to this release, it must |
| 1289 | have been shared by us and and whoever else is holding it; |
| 1290 | which in turn implies it must be r-held, since a lock |
| 1291 | can't be w-held by more than one thread. */ |
| 1292 | /* The lock is now R-held by somebody else: */ |
| 1293 | tl_assert(lock->heldW == False); |
| 1294 | } else { |
| 1295 | /* Normal case. It's either not a rwlock, or it's a rwlock |
| 1296 | that we used to hold in w-mode (which is pretty much the |
| 1297 | same thing as a non-rwlock.) Since this transaction is |
| 1298 | atomic (V does not allow multiple threads to run |
| 1299 | simultaneously), it must mean the lock is now not held by |
| 1300 | anybody. Hence assert for it. */ |
| 1301 | /* The lock is now not held by anybody: */ |
| 1302 | tl_assert(!lock->heldBy); |
| 1303 | tl_assert(lock->heldW == False); |
| 1304 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1305 | //if (lock->heldBy) { |
| 1306 | // tl_assert(0 == VG_(elemBag)( lock->heldBy, (Word)thr )); |
| 1307 | //} |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1308 | /* update this thread's lockset accordingly. */ |
| 1309 | thr->locksetA |
| 1310 | = HG_(delFromWS)( univ_lsets, thr->locksetA, (Word)lock ); |
| 1311 | thr->locksetW |
| 1312 | = HG_(delFromWS)( univ_lsets, thr->locksetW, (Word)lock ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1313 | /* push our VC into the lock */ |
| 1314 | tl_assert(thr->hbthr); |
| 1315 | tl_assert(lock->hbso); |
| 1316 | /* If the lock was previously W-held, then we want to do a |
| 1317 | strong send, and if previously R-held, then a weak send. */ |
| 1318 | libhb_so_send( thr->hbthr, lock->hbso, was_heldW ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1319 | } |
| 1320 | /* fall through */ |
| 1321 | |
| 1322 | error: |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1323 | tl_assert(HG_(is_sane_LockN)(lock)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 1327 | /* ---------------------------------------------------------- */ |
| 1328 | /* -------- Event handlers proper (evh__* functions) -------- */ |
| 1329 | /* ---------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1330 | |
| 1331 | /* What is the Thread* for the currently running thread? This is |
| 1332 | absolutely performance critical. We receive notifications from the |
| 1333 | core for client code starts/stops, and cache the looked-up result |
| 1334 | in 'current_Thread'. Hence, for the vast majority of requests, |
| 1335 | finding the current thread reduces to a read of a global variable, |
| 1336 | provided get_current_Thread_in_C_C is inlined. |
| 1337 | |
| 1338 | Outside of client code, current_Thread is NULL, and presumably |
| 1339 | any uses of it will cause a segfault. Hence: |
| 1340 | |
| 1341 | - for uses definitely within client code, use |
| 1342 | get_current_Thread_in_C_C. |
| 1343 | |
| 1344 | - for all other uses, use get_current_Thread. |
| 1345 | */ |
| 1346 | |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1347 | static Thread *current_Thread = NULL, |
| 1348 | *current_Thread_prev = NULL; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1349 | |
| 1350 | static void evh__start_client_code ( ThreadId tid, ULong nDisp ) { |
| 1351 | if (0) VG_(printf)("start %d %llu\n", (Int)tid, nDisp); |
| 1352 | tl_assert(current_Thread == NULL); |
| 1353 | current_Thread = map_threads_lookup( tid ); |
| 1354 | tl_assert(current_Thread != NULL); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1355 | if (current_Thread != current_Thread_prev) { |
| 1356 | libhb_Thr_resumes( current_Thread->hbthr ); |
| 1357 | current_Thread_prev = current_Thread; |
| 1358 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1359 | } |
| 1360 | static void evh__stop_client_code ( ThreadId tid, ULong nDisp ) { |
| 1361 | if (0) VG_(printf)(" stop %d %llu\n", (Int)tid, nDisp); |
| 1362 | tl_assert(current_Thread != NULL); |
| 1363 | current_Thread = NULL; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1364 | libhb_maybe_GC(); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1365 | } |
| 1366 | static inline Thread* get_current_Thread_in_C_C ( void ) { |
| 1367 | return current_Thread; |
| 1368 | } |
| 1369 | static inline Thread* get_current_Thread ( void ) { |
| 1370 | ThreadId coretid; |
| 1371 | Thread* thr; |
| 1372 | thr = get_current_Thread_in_C_C(); |
| 1373 | if (LIKELY(thr)) |
| 1374 | return thr; |
| 1375 | /* evidently not in client code. Do it the slow way. */ |
| 1376 | coretid = VG_(get_running_tid)(); |
| 1377 | /* FIXME: get rid of the following kludge. It exists because |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1378 | evh__new_mem is called during initialisation (as notification |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1379 | of initial memory layout) and VG_(get_running_tid)() returns |
| 1380 | VG_INVALID_THREADID at that point. */ |
| 1381 | if (coretid == VG_INVALID_THREADID) |
| 1382 | coretid = 1; /* KLUDGE */ |
| 1383 | thr = map_threads_lookup( coretid ); |
| 1384 | return thr; |
| 1385 | } |
| 1386 | |
| 1387 | static |
| 1388 | void evh__new_mem ( Addr a, SizeT len ) { |
| 1389 | if (SHOW_EVENTS >= 2) |
| 1390 | VG_(printf)("evh__new_mem(%p, %lu)\n", (void*)a, len ); |
| 1391 | shadow_mem_make_New( get_current_Thread(), a, len ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1392 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1393 | all__sanity_check("evh__new_mem-post"); |
| 1394 | } |
| 1395 | |
| 1396 | static |
sewardj | 1f77fec | 2010-04-12 19:51:04 +0000 | [diff] [blame] | 1397 | void evh__new_mem_stack ( Addr a, SizeT len ) { |
| 1398 | if (SHOW_EVENTS >= 2) |
| 1399 | VG_(printf)("evh__new_mem_stack(%p, %lu)\n", (void*)a, len ); |
| 1400 | shadow_mem_make_New( get_current_Thread(), |
| 1401 | -VG_STACK_REDZONE_SZB + a, len ); |
| 1402 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
| 1403 | all__sanity_check("evh__new_mem_stack-post"); |
| 1404 | } |
| 1405 | |
| 1406 | static |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1407 | void evh__new_mem_w_tid ( Addr a, SizeT len, ThreadId tid ) { |
| 1408 | if (SHOW_EVENTS >= 2) |
| 1409 | VG_(printf)("evh__new_mem_w_tid(%p, %lu)\n", (void*)a, len ); |
| 1410 | shadow_mem_make_New( get_current_Thread(), a, len ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1411 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1412 | all__sanity_check("evh__new_mem_w_tid-post"); |
| 1413 | } |
| 1414 | |
| 1415 | static |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1416 | void evh__new_mem_w_perms ( Addr a, SizeT len, |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1417 | Bool rr, Bool ww, Bool xx, ULong di_handle ) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1418 | if (SHOW_EVENTS >= 1) |
| 1419 | VG_(printf)("evh__new_mem_w_perms(%p, %lu, %d,%d,%d)\n", |
| 1420 | (void*)a, len, (Int)rr, (Int)ww, (Int)xx ); |
| 1421 | if (rr || ww || xx) |
| 1422 | shadow_mem_make_New( get_current_Thread(), a, len ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1423 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1424 | all__sanity_check("evh__new_mem_w_perms-post"); |
| 1425 | } |
| 1426 | |
| 1427 | static |
| 1428 | void evh__set_perms ( Addr a, SizeT len, |
| 1429 | Bool rr, Bool ww, Bool xx ) { |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1430 | // This handles mprotect requests. If the memory is being put |
| 1431 | // into no-R no-W state, paint it as NoAccess, for the reasons |
| 1432 | // documented at evh__die_mem_munmap(). |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1433 | if (SHOW_EVENTS >= 1) |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1434 | 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] | 1435 | (void*)a, len, (Int)rr, (Int)ww, (Int)xx ); |
| 1436 | /* Hmm. What should we do here, that actually makes any sense? |
| 1437 | Let's say: if neither readable nor writable, then declare it |
| 1438 | NoAccess, else leave it alone. */ |
| 1439 | if (!(rr || ww)) |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1440 | shadow_mem_make_NoAccess_AHAE( get_current_Thread(), a, len ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1441 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1442 | all__sanity_check("evh__set_perms-post"); |
| 1443 | } |
| 1444 | |
| 1445 | static |
| 1446 | void evh__die_mem ( Addr a, SizeT len ) { |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1447 | // Urr, libhb ignores this. |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1448 | if (SHOW_EVENTS >= 2) |
| 1449 | VG_(printf)("evh__die_mem(%p, %lu)\n", (void*)a, len ); |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1450 | shadow_mem_make_NoAccess_NoFX( get_current_Thread(), a, len ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1451 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1452 | all__sanity_check("evh__die_mem-post"); |
| 1453 | } |
| 1454 | |
| 1455 | static |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1456 | void evh__die_mem_munmap ( Addr a, SizeT len ) { |
| 1457 | // It's important that libhb doesn't ignore this. If, as is likely, |
| 1458 | // the client is subject to address space layout randomization, |
| 1459 | // then unmapped areas may never get remapped over, even in long |
| 1460 | // runs. If we just ignore them we wind up with large resource |
| 1461 | // (VTS) leaks in libhb. So force them to NoAccess, so that all |
| 1462 | // VTS references in the affected area are dropped. Marking memory |
| 1463 | // as NoAccess is expensive, but we assume that munmap is sufficiently |
| 1464 | // rare that the space gains of doing this are worth the costs. |
| 1465 | if (SHOW_EVENTS >= 2) |
| 1466 | VG_(printf)("evh__die_mem_munmap(%p, %lu)\n", (void*)a, len ); |
| 1467 | shadow_mem_make_NoAccess_AHAE( get_current_Thread(), a, len ); |
| 1468 | } |
| 1469 | |
| 1470 | static |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 1471 | void evh__untrack_mem ( Addr a, SizeT len ) { |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1472 | // Libhb doesn't ignore this. |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 1473 | if (SHOW_EVENTS >= 2) |
| 1474 | VG_(printf)("evh__untrack_mem(%p, %lu)\n", (void*)a, len ); |
| 1475 | shadow_mem_make_Untracked( get_current_Thread(), a, len ); |
| 1476 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
| 1477 | all__sanity_check("evh__untrack_mem-post"); |
| 1478 | } |
| 1479 | |
| 1480 | static |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1481 | void evh__copy_mem ( Addr src, Addr dst, SizeT len ) { |
| 1482 | if (SHOW_EVENTS >= 2) |
| 1483 | VG_(printf)("evh__copy_mem(%p, %p, %lu)\n", (void*)src, (void*)dst, len ); |
| 1484 | shadow_mem_scopy_range( get_current_Thread(), src, dst, len ); |
| 1485 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
| 1486 | all__sanity_check("evh__copy_mem-post"); |
| 1487 | } |
| 1488 | |
| 1489 | static |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1490 | void evh__pre_thread_ll_create ( ThreadId parent, ThreadId child ) |
| 1491 | { |
| 1492 | if (SHOW_EVENTS >= 1) |
| 1493 | VG_(printf)("evh__pre_thread_ll_create(p=%d, c=%d)\n", |
| 1494 | (Int)parent, (Int)child ); |
| 1495 | |
| 1496 | if (parent != VG_INVALID_THREADID) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1497 | Thread* thr_p; |
| 1498 | Thread* thr_c; |
| 1499 | Thr* hbthr_p; |
| 1500 | Thr* hbthr_c; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1501 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1502 | tl_assert(HG_(is_sane_ThreadId)(parent)); |
| 1503 | tl_assert(HG_(is_sane_ThreadId)(child)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1504 | tl_assert(parent != child); |
| 1505 | |
| 1506 | thr_p = map_threads_maybe_lookup( parent ); |
| 1507 | thr_c = map_threads_maybe_lookup( child ); |
| 1508 | |
| 1509 | tl_assert(thr_p != NULL); |
| 1510 | tl_assert(thr_c == NULL); |
| 1511 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1512 | hbthr_p = thr_p->hbthr; |
| 1513 | tl_assert(hbthr_p != NULL); |
sewardj | 6062664 | 2011-03-10 15:14:37 +0000 | [diff] [blame] | 1514 | tl_assert( libhb_get_Thr_hgthread(hbthr_p) == thr_p ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1515 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1516 | hbthr_c = libhb_create ( hbthr_p ); |
| 1517 | |
| 1518 | /* Create a new thread record for the child. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1519 | /* a Thread for the new thread ... */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1520 | thr_c = mk_Thread( hbthr_c ); |
sewardj | 6062664 | 2011-03-10 15:14:37 +0000 | [diff] [blame] | 1521 | tl_assert( libhb_get_Thr_hgthread(hbthr_c) == NULL ); |
| 1522 | libhb_set_Thr_hgthread(hbthr_c, thr_c); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1523 | |
| 1524 | /* and bind it in the thread-map table */ |
| 1525 | map_threads[child] = thr_c; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1526 | tl_assert(thr_c->coretid == VG_INVALID_THREADID); |
| 1527 | thr_c->coretid = child; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1528 | |
| 1529 | /* Record where the parent is so we can later refer to this in |
| 1530 | error messages. |
| 1531 | |
| 1532 | On amd64-linux, this entails a nasty glibc-2.5 specific hack. |
| 1533 | The stack snapshot is taken immediately after the parent has |
| 1534 | returned from its sys_clone call. Unfortunately there is no |
| 1535 | unwind info for the insn following "syscall" - reading the |
| 1536 | glibc sources confirms this. So we ask for a snapshot to be |
| 1537 | taken as if RIP was 3 bytes earlier, in a place where there |
| 1538 | is unwind info. Sigh. |
| 1539 | */ |
| 1540 | { Word first_ip_delta = 0; |
| 1541 | # if defined(VGP_amd64_linux) |
| 1542 | first_ip_delta = -3; |
| 1543 | # endif |
| 1544 | thr_c->created_at = VG_(record_ExeContext)(parent, first_ip_delta); |
| 1545 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1548 | if (HG_(clo_sanity_flags) & SCE_THREADS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1549 | all__sanity_check("evh__pre_thread_create-post"); |
| 1550 | } |
| 1551 | |
| 1552 | static |
| 1553 | void evh__pre_thread_ll_exit ( ThreadId quit_tid ) |
| 1554 | { |
| 1555 | Int nHeld; |
| 1556 | Thread* thr_q; |
| 1557 | if (SHOW_EVENTS >= 1) |
| 1558 | VG_(printf)("evh__pre_thread_ll_exit(thr=%d)\n", |
| 1559 | (Int)quit_tid ); |
| 1560 | |
| 1561 | /* quit_tid has disappeared without joining to any other thread. |
| 1562 | Therefore there is no synchronisation event associated with its |
| 1563 | exit and so we have to pretty much treat it as if it was still |
| 1564 | alive but mysteriously making no progress. That is because, if |
| 1565 | we don't know when it really exited, then we can never say there |
| 1566 | is a point in time when we're sure the thread really has |
| 1567 | finished, and so we need to consider the possibility that it |
| 1568 | lingers indefinitely and continues to interact with other |
| 1569 | threads. */ |
| 1570 | /* However, it might have rendezvous'd with a thread that called |
| 1571 | pthread_join with this one as arg, prior to this point (that's |
| 1572 | how NPTL works). In which case there has already been a prior |
| 1573 | sync event. So in any case, just let the thread exit. On NPTL, |
| 1574 | all thread exits go through here. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1575 | tl_assert(HG_(is_sane_ThreadId)(quit_tid)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1576 | thr_q = map_threads_maybe_lookup( quit_tid ); |
| 1577 | tl_assert(thr_q != NULL); |
| 1578 | |
| 1579 | /* Complain if this thread holds any locks. */ |
| 1580 | nHeld = HG_(cardinalityWS)( univ_lsets, thr_q->locksetA ); |
| 1581 | tl_assert(nHeld >= 0); |
| 1582 | if (nHeld > 0) { |
| 1583 | HChar buf[80]; |
| 1584 | VG_(sprintf)(buf, "Exiting thread still holds %d lock%s", |
| 1585 | nHeld, nHeld > 1 ? "s" : ""); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1586 | HG_(record_error_Misc)( thr_q, buf ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1589 | /* Not much to do here: |
| 1590 | - tell libhb the thread is gone |
| 1591 | - clear the map_threads entry, in order that the Valgrind core |
| 1592 | can re-use it. */ |
sewardj | 61bc2c5 | 2011-02-09 10:34:00 +0000 | [diff] [blame] | 1593 | /* Cleanup actions (next 5 lines) copied in evh__atfork_child; keep |
| 1594 | in sync. */ |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1595 | tl_assert(thr_q->hbthr); |
| 1596 | libhb_async_exit(thr_q->hbthr); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1597 | tl_assert(thr_q->coretid == quit_tid); |
| 1598 | thr_q->coretid = VG_INVALID_THREADID; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1599 | map_threads_delete( quit_tid ); |
| 1600 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1601 | if (HG_(clo_sanity_flags) & SCE_THREADS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1602 | all__sanity_check("evh__pre_thread_ll_exit-post"); |
| 1603 | } |
| 1604 | |
sewardj | 61bc2c5 | 2011-02-09 10:34:00 +0000 | [diff] [blame] | 1605 | /* This is called immediately after fork, for the child only. 'tid' |
| 1606 | is the only surviving thread (as per POSIX rules on fork() in |
| 1607 | threaded programs), so we have to clean up map_threads to remove |
| 1608 | entries for any other threads. */ |
| 1609 | static |
| 1610 | void evh__atfork_child ( ThreadId tid ) |
| 1611 | { |
| 1612 | UInt i; |
| 1613 | Thread* thr; |
| 1614 | /* Slot 0 should never be used. */ |
| 1615 | thr = map_threads_maybe_lookup( 0/*INVALID*/ ); |
| 1616 | tl_assert(!thr); |
| 1617 | /* Clean up all other slots except 'tid'. */ |
| 1618 | for (i = 1; i < VG_N_THREADS; i++) { |
| 1619 | if (i == tid) |
| 1620 | continue; |
| 1621 | thr = map_threads_maybe_lookup(i); |
| 1622 | if (!thr) |
| 1623 | continue; |
| 1624 | /* Cleanup actions (next 5 lines) copied from end of |
| 1625 | evh__pre_thread_ll_exit; keep in sync. */ |
| 1626 | tl_assert(thr->hbthr); |
| 1627 | libhb_async_exit(thr->hbthr); |
| 1628 | tl_assert(thr->coretid == i); |
| 1629 | thr->coretid = VG_INVALID_THREADID; |
| 1630 | map_threads_delete(i); |
| 1631 | } |
| 1632 | } |
| 1633 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1634 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1635 | static |
| 1636 | void evh__HG_PTHREAD_JOIN_POST ( ThreadId stay_tid, Thread* quit_thr ) |
| 1637 | { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1638 | Thread* thr_s; |
| 1639 | Thread* thr_q; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1640 | Thr* hbthr_s; |
| 1641 | Thr* hbthr_q; |
| 1642 | SO* so; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1643 | |
| 1644 | if (SHOW_EVENTS >= 1) |
| 1645 | VG_(printf)("evh__post_thread_join(stayer=%d, quitter=%p)\n", |
| 1646 | (Int)stay_tid, quit_thr ); |
| 1647 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1648 | tl_assert(HG_(is_sane_ThreadId)(stay_tid)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1649 | |
| 1650 | thr_s = map_threads_maybe_lookup( stay_tid ); |
| 1651 | thr_q = quit_thr; |
| 1652 | tl_assert(thr_s != NULL); |
| 1653 | tl_assert(thr_q != NULL); |
| 1654 | tl_assert(thr_s != thr_q); |
| 1655 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1656 | hbthr_s = thr_s->hbthr; |
| 1657 | hbthr_q = thr_q->hbthr; |
| 1658 | tl_assert(hbthr_s != hbthr_q); |
sewardj | 6062664 | 2011-03-10 15:14:37 +0000 | [diff] [blame] | 1659 | tl_assert( libhb_get_Thr_hgthread(hbthr_s) == thr_s ); |
| 1660 | tl_assert( libhb_get_Thr_hgthread(hbthr_q) == thr_q ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1661 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1662 | /* Allocate a temporary synchronisation object and use it to send |
| 1663 | an imaginary message from the quitter to the stayer, the purpose |
| 1664 | being to generate a dependence from the quitter to the |
| 1665 | stayer. */ |
| 1666 | so = libhb_so_alloc(); |
| 1667 | tl_assert(so); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1668 | /* Send last arg of _so_send as False, since the sending thread |
| 1669 | doesn't actually exist any more, so we don't want _so_send to |
| 1670 | try taking stack snapshots of it. */ |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 1671 | libhb_so_send(hbthr_q, so, True/*strong_send*//*?!? wrt comment above*/); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1672 | libhb_so_recv(hbthr_s, so, True/*strong_recv*/); |
| 1673 | libhb_so_dealloc(so); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1674 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 1675 | /* Tell libhb that the quitter has been reaped. Note that we might |
| 1676 | have to be cleverer about this, to exclude 2nd and subsequent |
| 1677 | notifications for the same hbthr_q, in the case where the app is |
| 1678 | buggy (calls pthread_join twice or more on the same thread) AND |
| 1679 | where libpthread is also buggy and doesn't return ESRCH on |
| 1680 | subsequent calls. (If libpthread isn't thusly buggy, then the |
| 1681 | wrapper for pthread_join in hg_intercepts.c will stop us getting |
| 1682 | notified here multiple times for the same joinee.) See also |
| 1683 | comments in helgrind/tests/jointwice.c. */ |
| 1684 | libhb_joinedwith_done(hbthr_q); |
| 1685 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1686 | /* evh__pre_thread_ll_exit issues an error message if the exiting |
| 1687 | thread holds any locks. No need to check here. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1688 | |
| 1689 | /* This holds because, at least when using NPTL as the thread |
| 1690 | library, we should be notified the low level thread exit before |
| 1691 | we hear of any join event on it. The low level exit |
| 1692 | notification feeds through into evh__pre_thread_ll_exit, |
| 1693 | which should clear the map_threads entry for it. Hence we |
| 1694 | expect there to be no map_threads entry at this point. */ |
| 1695 | tl_assert( map_threads_maybe_reverse_lookup_SLOW(thr_q) |
| 1696 | == VG_INVALID_THREADID); |
| 1697 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1698 | if (HG_(clo_sanity_flags) & SCE_THREADS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1699 | all__sanity_check("evh__post_thread_join-post"); |
| 1700 | } |
| 1701 | |
| 1702 | static |
| 1703 | void evh__pre_mem_read ( CorePart part, ThreadId tid, Char* s, |
| 1704 | Addr a, SizeT size) { |
| 1705 | if (SHOW_EVENTS >= 2 |
| 1706 | || (SHOW_EVENTS >= 1 && size != 1)) |
| 1707 | VG_(printf)("evh__pre_mem_read(ctid=%d, \"%s\", %p, %lu)\n", |
| 1708 | (Int)tid, s, (void*)a, size ); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1709 | shadow_mem_cread_range( map_threads_lookup(tid), a, size); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1710 | if (size >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1711 | all__sanity_check("evh__pre_mem_read-post"); |
| 1712 | } |
| 1713 | |
| 1714 | static |
| 1715 | void evh__pre_mem_read_asciiz ( CorePart part, ThreadId tid, |
| 1716 | Char* s, Addr a ) { |
| 1717 | Int len; |
| 1718 | if (SHOW_EVENTS >= 1) |
| 1719 | VG_(printf)("evh__pre_mem_asciiz(ctid=%d, \"%s\", %p)\n", |
| 1720 | (Int)tid, s, (void*)a ); |
sewardj | 234e558 | 2011-02-09 12:47:23 +0000 | [diff] [blame] | 1721 | // Don't segfault if the string starts in an obviously stupid |
| 1722 | // place. Actually we should check the whole string, not just |
| 1723 | // the start address, but that's too much trouble. At least |
| 1724 | // checking the first byte is better than nothing. See #255009. |
| 1725 | if (!VG_(am_is_valid_for_client) (a, 1, VKI_PROT_READ)) |
| 1726 | return; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1727 | len = VG_(strlen)( (Char*) a ); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1728 | shadow_mem_cread_range( map_threads_lookup(tid), a, len+1 ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1729 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1730 | all__sanity_check("evh__pre_mem_read_asciiz-post"); |
| 1731 | } |
| 1732 | |
| 1733 | static |
| 1734 | void evh__pre_mem_write ( CorePart part, ThreadId tid, Char* s, |
| 1735 | Addr a, SizeT size ) { |
| 1736 | if (SHOW_EVENTS >= 1) |
| 1737 | VG_(printf)("evh__pre_mem_write(ctid=%d, \"%s\", %p, %lu)\n", |
| 1738 | (Int)tid, s, (void*)a, size ); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1739 | shadow_mem_cwrite_range( map_threads_lookup(tid), a, size); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1740 | if (size >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1741 | all__sanity_check("evh__pre_mem_write-post"); |
| 1742 | } |
| 1743 | |
| 1744 | static |
| 1745 | void evh__new_mem_heap ( Addr a, SizeT len, Bool is_inited ) { |
| 1746 | if (SHOW_EVENTS >= 1) |
| 1747 | VG_(printf)("evh__new_mem_heap(%p, %lu, inited=%d)\n", |
| 1748 | (void*)a, len, (Int)is_inited ); |
| 1749 | // FIXME: this is kinda stupid |
| 1750 | if (is_inited) { |
| 1751 | shadow_mem_make_New(get_current_Thread(), a, len); |
| 1752 | } else { |
| 1753 | shadow_mem_make_New(get_current_Thread(), a, len); |
| 1754 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1755 | if (len >= SCE_BIGRANGE_T && (HG_(clo_sanity_flags) & SCE_BIGRANGE)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1756 | all__sanity_check("evh__pre_mem_read-post"); |
| 1757 | } |
| 1758 | |
| 1759 | static |
| 1760 | void evh__die_mem_heap ( Addr a, SizeT len ) { |
sewardj | 622fe49 | 2011-03-11 21:06:59 +0000 | [diff] [blame] | 1761 | Thread* thr; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1762 | if (SHOW_EVENTS >= 1) |
| 1763 | VG_(printf)("evh__die_mem_heap(%p, %lu)\n", (void*)a, len ); |
sewardj | 622fe49 | 2011-03-11 21:06:59 +0000 | [diff] [blame] | 1764 | thr = get_current_Thread(); |
| 1765 | tl_assert(thr); |
| 1766 | if (HG_(clo_free_is_write)) { |
| 1767 | /* Treat frees as if the memory was written immediately prior to |
| 1768 | the free. This shakes out more races, specifically, cases |
| 1769 | where memory is referenced by one thread, and freed by |
| 1770 | another, and there's no observable synchronisation event to |
| 1771 | guarantee that the reference happens before the free. */ |
| 1772 | shadow_mem_cwrite_range(thr, a, len); |
| 1773 | } |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 1774 | shadow_mem_make_NoAccess_NoFX( thr, a, len ); |
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-post"); |
| 1777 | } |
| 1778 | |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1779 | /* --- Event handlers called from generated code --- */ |
| 1780 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1781 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1782 | void evh__mem_help_cread_1(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1783 | Thread* thr = get_current_Thread_in_C_C(); |
| 1784 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1785 | LIBHB_CREAD_1(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1786 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1787 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1788 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1789 | void evh__mem_help_cread_2(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1790 | Thread* thr = get_current_Thread_in_C_C(); |
| 1791 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1792 | LIBHB_CREAD_2(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1793 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1794 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1795 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1796 | void evh__mem_help_cread_4(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1797 | Thread* thr = get_current_Thread_in_C_C(); |
| 1798 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1799 | LIBHB_CREAD_4(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1800 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1801 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1802 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1803 | void evh__mem_help_cread_8(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1804 | Thread* thr = get_current_Thread_in_C_C(); |
| 1805 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1806 | LIBHB_CREAD_8(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1807 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1808 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1809 | static VG_REGPARM(2) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1810 | void evh__mem_help_cread_N(Addr a, SizeT size) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1811 | Thread* thr = get_current_Thread_in_C_C(); |
| 1812 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1813 | LIBHB_CREAD_N(hbthr, a, size); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1817 | void evh__mem_help_cwrite_1(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1818 | Thread* thr = get_current_Thread_in_C_C(); |
| 1819 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1820 | LIBHB_CWRITE_1(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1821 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1822 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1823 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1824 | void evh__mem_help_cwrite_2(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1825 | Thread* thr = get_current_Thread_in_C_C(); |
| 1826 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1827 | LIBHB_CWRITE_2(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1828 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1829 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1830 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1831 | void evh__mem_help_cwrite_4(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1832 | Thread* thr = get_current_Thread_in_C_C(); |
| 1833 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1834 | LIBHB_CWRITE_4(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1835 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1836 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1837 | static VG_REGPARM(1) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1838 | void evh__mem_help_cwrite_8(Addr a) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1839 | Thread* thr = get_current_Thread_in_C_C(); |
| 1840 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1841 | LIBHB_CWRITE_8(hbthr, a); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1842 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1843 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1844 | static VG_REGPARM(2) |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1845 | void evh__mem_help_cwrite_N(Addr a, SizeT size) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1846 | Thread* thr = get_current_Thread_in_C_C(); |
| 1847 | Thr* hbthr = thr->hbthr; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 1848 | LIBHB_CWRITE_N(hbthr, a, size); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1851 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 1852 | /* ------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1853 | /* -------------- events to do with mutexes -------------- */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 1854 | /* ------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1855 | |
| 1856 | /* EXPOSITION only: by intercepting lock init events we can show the |
| 1857 | user where the lock was initialised, rather than only being able to |
| 1858 | show where it was first locked. Intercepting lock initialisations |
| 1859 | is not necessary for the basic operation of the race checker. */ |
| 1860 | static |
| 1861 | void evh__HG_PTHREAD_MUTEX_INIT_POST( ThreadId tid, |
| 1862 | void* mutex, Word mbRec ) |
| 1863 | { |
| 1864 | if (SHOW_EVENTS >= 1) |
| 1865 | VG_(printf)("evh__hg_PTHREAD_MUTEX_INIT_POST(ctid=%d, mbRec=%ld, %p)\n", |
| 1866 | (Int)tid, mbRec, (void*)mutex ); |
| 1867 | tl_assert(mbRec == 0 || mbRec == 1); |
| 1868 | map_locks_lookup_or_create( mbRec ? LK_mbRec : LK_nonRec, |
| 1869 | (Addr)mutex, tid ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1870 | if (HG_(clo_sanity_flags) & SCE_LOCKS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1871 | all__sanity_check("evh__hg_PTHREAD_MUTEX_INIT_POST"); |
| 1872 | } |
| 1873 | |
| 1874 | static |
| 1875 | void evh__HG_PTHREAD_MUTEX_DESTROY_PRE( ThreadId tid, void* mutex ) |
| 1876 | { |
| 1877 | Thread* thr; |
| 1878 | Lock* lk; |
| 1879 | if (SHOW_EVENTS >= 1) |
| 1880 | VG_(printf)("evh__hg_PTHREAD_MUTEX_DESTROY_PRE(ctid=%d, %p)\n", |
| 1881 | (Int)tid, (void*)mutex ); |
| 1882 | |
| 1883 | thr = map_threads_maybe_lookup( tid ); |
| 1884 | /* cannot fail - Thread* must already exist */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1885 | tl_assert( HG_(is_sane_Thread)(thr) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1886 | |
| 1887 | lk = map_locks_maybe_lookup( (Addr)mutex ); |
| 1888 | |
| 1889 | if (lk == NULL || (lk->kind != LK_nonRec && lk->kind != LK_mbRec)) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1890 | HG_(record_error_Misc)( |
| 1891 | thr, "pthread_mutex_destroy with invalid argument" ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | if (lk) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1895 | tl_assert( HG_(is_sane_LockN)(lk) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1896 | tl_assert( lk->guestaddr == (Addr)mutex ); |
| 1897 | if (lk->heldBy) { |
| 1898 | /* Basically act like we unlocked the lock */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1899 | HG_(record_error_Misc)( |
| 1900 | thr, "pthread_mutex_destroy of a locked mutex" ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1901 | /* remove lock from locksets of all owning threads */ |
| 1902 | remove_Lock_from_locksets_of_all_owning_Threads( lk ); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 1903 | VG_(deleteBag)( lk->heldBy ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1904 | lk->heldBy = NULL; |
| 1905 | lk->heldW = False; |
| 1906 | lk->acquired_at = NULL; |
| 1907 | } |
| 1908 | tl_assert( !lk->heldBy ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1909 | tl_assert( HG_(is_sane_LockN)(lk) ); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 1910 | |
| 1911 | if (HG_(clo_track_lockorders)) |
| 1912 | laog__handle_one_lock_deletion(lk); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1913 | map_locks_delete( lk->guestaddr ); |
| 1914 | del_LockN( lk ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1917 | if (HG_(clo_sanity_flags) & SCE_LOCKS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1918 | all__sanity_check("evh__hg_PTHREAD_MUTEX_DESTROY_PRE"); |
| 1919 | } |
| 1920 | |
| 1921 | static void evh__HG_PTHREAD_MUTEX_LOCK_PRE ( ThreadId tid, |
| 1922 | void* mutex, Word isTryLock ) |
| 1923 | { |
| 1924 | /* Just check the mutex is sane; nothing else to do. */ |
| 1925 | // 'mutex' may be invalid - not checked by wrapper |
| 1926 | Thread* thr; |
| 1927 | Lock* lk; |
| 1928 | if (SHOW_EVENTS >= 1) |
| 1929 | VG_(printf)("evh__hg_PTHREAD_MUTEX_LOCK_PRE(ctid=%d, mutex=%p)\n", |
| 1930 | (Int)tid, (void*)mutex ); |
| 1931 | |
| 1932 | tl_assert(isTryLock == 0 || isTryLock == 1); |
| 1933 | thr = map_threads_maybe_lookup( tid ); |
| 1934 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 1935 | |
| 1936 | lk = map_locks_maybe_lookup( (Addr)mutex ); |
| 1937 | |
| 1938 | if (lk && (lk->kind == LK_rdwr)) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 1939 | HG_(record_error_Misc)( thr, "pthread_mutex_lock with a " |
| 1940 | "pthread_rwlock_t* argument " ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | if ( lk |
| 1944 | && isTryLock == 0 |
| 1945 | && (lk->kind == LK_nonRec || lk->kind == LK_rdwr) |
| 1946 | && lk->heldBy |
| 1947 | && lk->heldW |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 1948 | && VG_(elemBag)( lk->heldBy, (Word)thr ) > 0 ) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1949 | /* uh, it's a non-recursive lock and we already w-hold it, and |
| 1950 | this is a real lock operation (not a speculative "tryLock" |
| 1951 | kind of thing). Duh. Deadlock coming up; but at least |
| 1952 | produce an error message. */ |
sewardj | 8fef625 | 2010-07-29 05:28:02 +0000 | [diff] [blame] | 1953 | HChar* errstr = "Attempt to re-lock a " |
| 1954 | "non-recursive lock I already hold"; |
| 1955 | HChar* auxstr = "Lock was previously acquired"; |
| 1956 | if (lk->acquired_at) { |
| 1957 | HG_(record_error_Misc_w_aux)( thr, errstr, auxstr, lk->acquired_at ); |
| 1958 | } else { |
| 1959 | HG_(record_error_Misc)( thr, errstr ); |
| 1960 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | static void evh__HG_PTHREAD_MUTEX_LOCK_POST ( ThreadId tid, void* mutex ) |
| 1965 | { |
| 1966 | // only called if the real library call succeeded - so mutex is sane |
| 1967 | Thread* thr; |
| 1968 | if (SHOW_EVENTS >= 1) |
| 1969 | VG_(printf)("evh__HG_PTHREAD_MUTEX_LOCK_POST(ctid=%d, mutex=%p)\n", |
| 1970 | (Int)tid, (void*)mutex ); |
| 1971 | |
| 1972 | thr = map_threads_maybe_lookup( tid ); |
| 1973 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 1974 | |
| 1975 | evhH__post_thread_w_acquires_lock( |
| 1976 | thr, |
| 1977 | LK_mbRec, /* if not known, create new lock with this LockKind */ |
| 1978 | (Addr)mutex |
| 1979 | ); |
| 1980 | } |
| 1981 | |
| 1982 | static void evh__HG_PTHREAD_MUTEX_UNLOCK_PRE ( ThreadId tid, void* mutex ) |
| 1983 | { |
| 1984 | // 'mutex' may be invalid - not checked by wrapper |
| 1985 | Thread* thr; |
| 1986 | if (SHOW_EVENTS >= 1) |
| 1987 | VG_(printf)("evh__HG_PTHREAD_MUTEX_UNLOCK_PRE(ctid=%d, mutex=%p)\n", |
| 1988 | (Int)tid, (void*)mutex ); |
| 1989 | |
| 1990 | thr = map_threads_maybe_lookup( tid ); |
| 1991 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 1992 | |
| 1993 | evhH__pre_thread_releases_lock( thr, (Addr)mutex, False/*!isRDWR*/ ); |
| 1994 | } |
| 1995 | |
| 1996 | static void evh__HG_PTHREAD_MUTEX_UNLOCK_POST ( ThreadId tid, void* mutex ) |
| 1997 | { |
| 1998 | // only called if the real library call succeeded - so mutex is sane |
| 1999 | Thread* thr; |
| 2000 | if (SHOW_EVENTS >= 1) |
| 2001 | VG_(printf)("evh__hg_PTHREAD_MUTEX_UNLOCK_POST(ctid=%d, mutex=%p)\n", |
| 2002 | (Int)tid, (void*)mutex ); |
| 2003 | thr = map_threads_maybe_lookup( tid ); |
| 2004 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2005 | |
| 2006 | // anything we should do here? |
| 2007 | } |
| 2008 | |
| 2009 | |
sewardj | 5a644da | 2009-08-11 10:35:58 +0000 | [diff] [blame] | 2010 | /* ------------------------------------------------------- */ |
sewardj | 1f77fec | 2010-04-12 19:51:04 +0000 | [diff] [blame] | 2011 | /* -------------- events to do with spinlocks ------------ */ |
sewardj | 5a644da | 2009-08-11 10:35:58 +0000 | [diff] [blame] | 2012 | /* ------------------------------------------------------- */ |
| 2013 | |
| 2014 | /* All a bit of a kludge. Pretend we're really dealing with ordinary |
| 2015 | pthread_mutex_t's instead, for the most part. */ |
| 2016 | |
| 2017 | static void evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE( ThreadId tid, |
| 2018 | void* slock ) |
| 2019 | { |
| 2020 | Thread* thr; |
| 2021 | Lock* lk; |
| 2022 | /* In glibc's kludgey world, we're either initialising or unlocking |
| 2023 | it. Since this is the pre-routine, if it is locked, unlock it |
| 2024 | and take a dependence edge. Otherwise, do nothing. */ |
| 2025 | |
| 2026 | if (SHOW_EVENTS >= 1) |
| 2027 | VG_(printf)("evh__hg_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE" |
| 2028 | "(ctid=%d, slock=%p)\n", |
| 2029 | (Int)tid, (void*)slock ); |
| 2030 | |
| 2031 | thr = map_threads_maybe_lookup( tid ); |
| 2032 | /* cannot fail - Thread* must already exist */; |
| 2033 | tl_assert( HG_(is_sane_Thread)(thr) ); |
| 2034 | |
| 2035 | lk = map_locks_maybe_lookup( (Addr)slock ); |
| 2036 | if (lk && lk->heldBy) { |
| 2037 | /* it's held. So do the normal pre-unlock actions, as copied |
| 2038 | from evh__HG_PTHREAD_MUTEX_UNLOCK_PRE. This stupidly |
| 2039 | duplicates the map_locks_maybe_lookup. */ |
| 2040 | evhH__pre_thread_releases_lock( thr, (Addr)slock, |
| 2041 | False/*!isRDWR*/ ); |
| 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | static void evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_POST( ThreadId tid, |
| 2046 | void* slock ) |
| 2047 | { |
| 2048 | Lock* lk; |
| 2049 | /* More kludgery. If the lock has never been seen before, do |
| 2050 | actions as per evh__HG_PTHREAD_MUTEX_INIT_POST. Else do |
| 2051 | nothing. */ |
| 2052 | |
| 2053 | if (SHOW_EVENTS >= 1) |
| 2054 | VG_(printf)("evh__hg_PTHREAD_SPIN_INIT_OR_UNLOCK_POST" |
| 2055 | "(ctid=%d, slock=%p)\n", |
| 2056 | (Int)tid, (void*)slock ); |
| 2057 | |
| 2058 | lk = map_locks_maybe_lookup( (Addr)slock ); |
| 2059 | if (!lk) { |
| 2060 | map_locks_lookup_or_create( LK_nonRec, (Addr)slock, tid ); |
| 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | static void evh__HG_PTHREAD_SPIN_LOCK_PRE( ThreadId tid, |
| 2065 | void* slock, Word isTryLock ) |
| 2066 | { |
| 2067 | evh__HG_PTHREAD_MUTEX_LOCK_PRE( tid, slock, isTryLock ); |
| 2068 | } |
| 2069 | |
| 2070 | static void evh__HG_PTHREAD_SPIN_LOCK_POST( ThreadId tid, |
| 2071 | void* slock ) |
| 2072 | { |
| 2073 | evh__HG_PTHREAD_MUTEX_LOCK_POST( tid, slock ); |
| 2074 | } |
| 2075 | |
| 2076 | static void evh__HG_PTHREAD_SPIN_DESTROY_PRE( ThreadId tid, |
| 2077 | void* slock ) |
| 2078 | { |
| 2079 | evh__HG_PTHREAD_MUTEX_DESTROY_PRE( tid, slock ); |
| 2080 | } |
| 2081 | |
| 2082 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2083 | /* ----------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2084 | /* --------------- events to do with CVs --------------- */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2085 | /* ----------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2086 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2087 | /* A mapping from CV to (the SO associated with it, plus some |
| 2088 | auxiliary data for error checking). When the CV is |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2089 | signalled/broadcasted upon, we do a 'send' into the SO, and when a |
| 2090 | wait on it completes, we do a 'recv' from the SO. This is believed |
| 2091 | to give the correct happens-before events arising from CV |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2092 | signallings/broadcasts. |
| 2093 | */ |
| 2094 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2095 | /* .so is the SO for this CV. |
| 2096 | .mx_ga is the associated mutex, when .nWaiters > 0 |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2097 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2098 | POSIX says effectively that the first pthread_cond_{timed}wait call |
| 2099 | causes a dynamic binding between the CV and the mutex, and that |
| 2100 | lasts until such time as the waiter count falls to zero. Hence |
| 2101 | need to keep track of the number of waiters in order to do |
| 2102 | consistency tracking. */ |
| 2103 | typedef |
| 2104 | struct { |
| 2105 | SO* so; /* libhb-allocated SO */ |
| 2106 | void* mx_ga; /* addr of associated mutex, if any */ |
| 2107 | UWord nWaiters; /* # threads waiting on the CV */ |
| 2108 | } |
| 2109 | CVInfo; |
| 2110 | |
| 2111 | |
| 2112 | /* pthread_cond_t* -> CVInfo* */ |
| 2113 | static WordFM* map_cond_to_CVInfo = NULL; |
| 2114 | |
| 2115 | static void map_cond_to_CVInfo_INIT ( void ) { |
| 2116 | if (UNLIKELY(map_cond_to_CVInfo == NULL)) { |
| 2117 | map_cond_to_CVInfo = VG_(newFM)( HG_(zalloc), |
| 2118 | "hg.mctCI.1", HG_(free), NULL ); |
| 2119 | tl_assert(map_cond_to_CVInfo != NULL); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2120 | } |
| 2121 | } |
| 2122 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2123 | static CVInfo* map_cond_to_CVInfo_lookup_or_alloc ( void* cond ) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2124 | UWord key, val; |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2125 | map_cond_to_CVInfo_INIT(); |
| 2126 | if (VG_(lookupFM)( map_cond_to_CVInfo, &key, &val, (UWord)cond )) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2127 | tl_assert(key == (UWord)cond); |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2128 | return (CVInfo*)val; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2129 | } else { |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2130 | SO* so = libhb_so_alloc(); |
| 2131 | CVInfo* cvi = HG_(zalloc)("hg.mctCloa.1", sizeof(CVInfo)); |
| 2132 | cvi->so = so; |
| 2133 | cvi->mx_ga = 0; |
| 2134 | VG_(addToFM)( map_cond_to_CVInfo, (UWord)cond, (UWord)cvi ); |
| 2135 | return cvi; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2136 | } |
| 2137 | } |
| 2138 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2139 | static void map_cond_to_CVInfo_delete ( void* cond ) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2140 | UWord keyW, valW; |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2141 | map_cond_to_CVInfo_INIT(); |
| 2142 | if (VG_(delFromFM)( map_cond_to_CVInfo, &keyW, &valW, (UWord)cond )) { |
| 2143 | CVInfo* cvi = (CVInfo*)valW; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2144 | tl_assert(keyW == (UWord)cond); |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2145 | tl_assert(cvi); |
| 2146 | tl_assert(cvi->so); |
| 2147 | libhb_so_dealloc(cvi->so); |
| 2148 | cvi->mx_ga = 0; |
| 2149 | HG_(free)(cvi); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | static void evh__HG_PTHREAD_COND_SIGNAL_PRE ( ThreadId tid, void* cond ) |
| 2154 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2155 | /* 'tid' has signalled on 'cond'. As per the comment above, bind |
| 2156 | cond to a SO if it is not already so bound, and 'send' on the |
| 2157 | SO. This is later used by other thread(s) which successfully |
| 2158 | exit from a pthread_cond_wait on the same cv; then they 'recv' |
| 2159 | from the SO, thereby acquiring a dependency on this signalling |
| 2160 | event. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2161 | Thread* thr; |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2162 | CVInfo* cvi; |
| 2163 | //Lock* lk; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2164 | |
| 2165 | if (SHOW_EVENTS >= 1) |
| 2166 | VG_(printf)("evh__HG_PTHREAD_COND_SIGNAL_PRE(ctid=%d, cond=%p)\n", |
| 2167 | (Int)tid, (void*)cond ); |
| 2168 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2169 | thr = map_threads_maybe_lookup( tid ); |
| 2170 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2171 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2172 | cvi = map_cond_to_CVInfo_lookup_or_alloc( cond ); |
| 2173 | tl_assert(cvi); |
| 2174 | tl_assert(cvi->so); |
| 2175 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2176 | // error-if: mutex is bogus |
| 2177 | // error-if: mutex is not locked |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2178 | // Hmm. POSIX doesn't actually say that it's an error to call |
| 2179 | // pthread_cond_signal with the associated mutex being unlocked. |
| 2180 | // Although it does say that it should be "if consistent scheduling |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 2181 | // is desired." For that reason, print "dubious" if the lock isn't |
| 2182 | // held by any thread. Skip the "dubious" if it is held by some |
| 2183 | // other thread; that sounds straight-out wrong. |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2184 | // |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 2185 | // Anybody who writes code that signals on a CV without holding |
| 2186 | // the associated MX needs to be shipped off to a lunatic asylum |
| 2187 | // ASAP, even though POSIX doesn't actually declare such behaviour |
| 2188 | // illegal -- it makes code extremely difficult to understand/ |
| 2189 | // reason about. In particular it puts the signalling thread in |
| 2190 | // a situation where it is racing against the released waiter |
| 2191 | // as soon as the signalling is done, and so there needs to be |
| 2192 | // some auxiliary synchronisation mechanism in the program that |
| 2193 | // makes this safe -- or the race(s) need to be harmless, or |
| 2194 | // probably nonexistent. |
| 2195 | // |
| 2196 | if (1) { |
| 2197 | Lock* lk = NULL; |
| 2198 | if (cvi->mx_ga != 0) { |
| 2199 | lk = map_locks_maybe_lookup( (Addr)cvi->mx_ga ); |
| 2200 | } |
| 2201 | /* note: lk could be NULL. Be careful. */ |
| 2202 | if (lk) { |
| 2203 | if (lk->kind == LK_rdwr) { |
| 2204 | HG_(record_error_Misc)(thr, |
| 2205 | "pthread_cond_{signal,broadcast}: associated lock is a rwlock"); |
| 2206 | } |
| 2207 | if (lk->heldBy == NULL) { |
| 2208 | HG_(record_error_Misc)(thr, |
| 2209 | "pthread_cond_{signal,broadcast}: dubious: " |
| 2210 | "associated lock is not held by any thread"); |
| 2211 | } |
| 2212 | if (lk->heldBy != NULL && 0 == VG_(elemBag)(lk->heldBy, (Word)thr)) { |
| 2213 | HG_(record_error_Misc)(thr, |
| 2214 | "pthread_cond_{signal,broadcast}: " |
| 2215 | "associated lock is not held by calling thread"); |
| 2216 | } |
| 2217 | } else { |
| 2218 | /* Couldn't even find the damn thing. */ |
| 2219 | // But actually .. that's not necessarily an error. We don't |
| 2220 | // know the (CV,MX) binding until a pthread_cond_wait or bcast |
| 2221 | // shows us what it is, and if that may not have happened yet. |
| 2222 | // So just keep quiet in this circumstance. |
| 2223 | //HG_(record_error_Misc)( thr, |
| 2224 | // "pthread_cond_{signal,broadcast}: " |
| 2225 | // "no or invalid mutex associated with cond"); |
| 2226 | } |
| 2227 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2228 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2229 | libhb_so_send( thr->hbthr, cvi->so, True/*strong_send*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | /* returns True if it reckons 'mutex' is valid and held by this |
| 2233 | thread, else False */ |
| 2234 | static Bool evh__HG_PTHREAD_COND_WAIT_PRE ( ThreadId tid, |
| 2235 | void* cond, void* mutex ) |
| 2236 | { |
| 2237 | Thread* thr; |
| 2238 | Lock* lk; |
| 2239 | Bool lk_valid = True; |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2240 | CVInfo* cvi; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2241 | |
| 2242 | if (SHOW_EVENTS >= 1) |
| 2243 | VG_(printf)("evh__hg_PTHREAD_COND_WAIT_PRE" |
| 2244 | "(ctid=%d, cond=%p, mutex=%p)\n", |
| 2245 | (Int)tid, (void*)cond, (void*)mutex ); |
| 2246 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2247 | thr = map_threads_maybe_lookup( tid ); |
| 2248 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2249 | |
| 2250 | lk = map_locks_maybe_lookup( (Addr)mutex ); |
| 2251 | |
| 2252 | /* Check for stupid mutex arguments. There are various ways to be |
| 2253 | a bozo. Only complain once, though, even if more than one thing |
| 2254 | is wrong. */ |
| 2255 | if (lk == NULL) { |
| 2256 | lk_valid = False; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2257 | HG_(record_error_Misc)( |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2258 | thr, |
| 2259 | "pthread_cond_{timed}wait called with invalid mutex" ); |
| 2260 | } else { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2261 | tl_assert( HG_(is_sane_LockN)(lk) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2262 | if (lk->kind == LK_rdwr) { |
| 2263 | lk_valid = False; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2264 | HG_(record_error_Misc)( |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2265 | thr, "pthread_cond_{timed}wait called with mutex " |
| 2266 | "of type pthread_rwlock_t*" ); |
| 2267 | } else |
| 2268 | if (lk->heldBy == NULL) { |
| 2269 | lk_valid = False; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2270 | HG_(record_error_Misc)( |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2271 | thr, "pthread_cond_{timed}wait called with un-held mutex"); |
| 2272 | } else |
| 2273 | if (lk->heldBy != NULL |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 2274 | && VG_(elemBag)( lk->heldBy, (Word)thr ) == 0) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2275 | lk_valid = False; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2276 | HG_(record_error_Misc)( |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2277 | thr, "pthread_cond_{timed}wait called with mutex " |
| 2278 | "held by a different thread" ); |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | // error-if: cond is also associated with a different mutex |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2283 | cvi = map_cond_to_CVInfo_lookup_or_alloc(cond); |
| 2284 | tl_assert(cvi); |
| 2285 | tl_assert(cvi->so); |
| 2286 | if (cvi->nWaiters == 0) { |
| 2287 | /* form initial (CV,MX) binding */ |
| 2288 | cvi->mx_ga = mutex; |
| 2289 | } |
| 2290 | else /* check existing (CV,MX) binding */ |
| 2291 | if (cvi->mx_ga != mutex) { |
| 2292 | HG_(record_error_Misc)( |
| 2293 | thr, "pthread_cond_{timed}wait: cond is associated " |
| 2294 | "with a different mutex"); |
| 2295 | } |
| 2296 | cvi->nWaiters++; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2297 | |
| 2298 | return lk_valid; |
| 2299 | } |
| 2300 | |
| 2301 | static void evh__HG_PTHREAD_COND_WAIT_POST ( ThreadId tid, |
| 2302 | void* cond, void* mutex ) |
| 2303 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2304 | /* A pthread_cond_wait(cond, mutex) completed successfully. Find |
| 2305 | the SO for this cond, and 'recv' from it so as to acquire a |
| 2306 | dependency edge back to the signaller/broadcaster. */ |
| 2307 | Thread* thr; |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2308 | CVInfo* cvi; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2309 | |
| 2310 | if (SHOW_EVENTS >= 1) |
| 2311 | VG_(printf)("evh__HG_PTHREAD_COND_WAIT_POST" |
| 2312 | "(ctid=%d, cond=%p, mutex=%p)\n", |
| 2313 | (Int)tid, (void*)cond, (void*)mutex ); |
| 2314 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2315 | thr = map_threads_maybe_lookup( tid ); |
| 2316 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2317 | |
| 2318 | // error-if: cond is also associated with a different mutex |
| 2319 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2320 | cvi = map_cond_to_CVInfo_lookup_or_alloc( cond ); |
| 2321 | tl_assert(cvi); |
| 2322 | tl_assert(cvi->so); |
| 2323 | tl_assert(cvi->nWaiters > 0); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2324 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2325 | if (!libhb_so_everSent(cvi->so)) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2326 | /* Hmm. How can a wait on 'cond' succeed if nobody signalled |
| 2327 | it? If this happened it would surely be a bug in the threads |
| 2328 | library. Or one of those fabled "spurious wakeups". */ |
| 2329 | HG_(record_error_Misc)( thr, "Bug in libpthread: pthread_cond_wait " |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 2330 | "succeeded" |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2331 | " without prior pthread_cond_post"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2332 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2333 | |
| 2334 | /* anyway, acquire a dependency on it. */ |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2335 | libhb_so_recv( thr->hbthr, cvi->so, True/*strong_recv*/ ); |
| 2336 | |
| 2337 | cvi->nWaiters--; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | static void evh__HG_PTHREAD_COND_DESTROY_PRE ( ThreadId tid, |
| 2341 | void* cond ) |
| 2342 | { |
| 2343 | /* Deal with destroy events. The only purpose is to free storage |
| 2344 | associated with the CV, so as to avoid any possible resource |
| 2345 | leaks. */ |
| 2346 | if (SHOW_EVENTS >= 1) |
| 2347 | VG_(printf)("evh__HG_PTHREAD_COND_DESTROY_PRE" |
| 2348 | "(ctid=%d, cond=%p)\n", |
| 2349 | (Int)tid, (void*)cond ); |
| 2350 | |
sewardj | 0211454 | 2009-07-28 20:52:36 +0000 | [diff] [blame] | 2351 | map_cond_to_CVInfo_delete( cond ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2355 | /* ------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2356 | /* -------------- events to do with rwlocks -------------- */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2357 | /* ------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2358 | |
| 2359 | /* EXPOSITION only */ |
| 2360 | static |
| 2361 | void evh__HG_PTHREAD_RWLOCK_INIT_POST( ThreadId tid, void* rwl ) |
| 2362 | { |
| 2363 | if (SHOW_EVENTS >= 1) |
| 2364 | VG_(printf)("evh__hg_PTHREAD_RWLOCK_INIT_POST(ctid=%d, %p)\n", |
| 2365 | (Int)tid, (void*)rwl ); |
| 2366 | map_locks_lookup_or_create( LK_rdwr, (Addr)rwl, tid ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2367 | if (HG_(clo_sanity_flags) & SCE_LOCKS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2368 | all__sanity_check("evh__hg_PTHREAD_RWLOCK_INIT_POST"); |
| 2369 | } |
| 2370 | |
| 2371 | static |
| 2372 | void evh__HG_PTHREAD_RWLOCK_DESTROY_PRE( ThreadId tid, void* rwl ) |
| 2373 | { |
| 2374 | Thread* thr; |
| 2375 | Lock* lk; |
| 2376 | if (SHOW_EVENTS >= 1) |
| 2377 | VG_(printf)("evh__hg_PTHREAD_RWLOCK_DESTROY_PRE(ctid=%d, %p)\n", |
| 2378 | (Int)tid, (void*)rwl ); |
| 2379 | |
| 2380 | thr = map_threads_maybe_lookup( tid ); |
| 2381 | /* cannot fail - Thread* must already exist */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2382 | tl_assert( HG_(is_sane_Thread)(thr) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2383 | |
| 2384 | lk = map_locks_maybe_lookup( (Addr)rwl ); |
| 2385 | |
| 2386 | if (lk == NULL || lk->kind != LK_rdwr) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2387 | HG_(record_error_Misc)( |
| 2388 | thr, "pthread_rwlock_destroy with invalid argument" ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | if (lk) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2392 | tl_assert( HG_(is_sane_LockN)(lk) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2393 | tl_assert( lk->guestaddr == (Addr)rwl ); |
| 2394 | if (lk->heldBy) { |
| 2395 | /* Basically act like we unlocked the lock */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2396 | HG_(record_error_Misc)( |
| 2397 | thr, "pthread_rwlock_destroy of a locked mutex" ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2398 | /* remove lock from locksets of all owning threads */ |
| 2399 | remove_Lock_from_locksets_of_all_owning_Threads( lk ); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 2400 | VG_(deleteBag)( lk->heldBy ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2401 | lk->heldBy = NULL; |
| 2402 | lk->heldW = False; |
sewardj | 1c7e833 | 2007-11-29 13:04:03 +0000 | [diff] [blame] | 2403 | lk->acquired_at = NULL; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2404 | } |
| 2405 | tl_assert( !lk->heldBy ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2406 | tl_assert( HG_(is_sane_LockN)(lk) ); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 2407 | |
| 2408 | if (HG_(clo_track_lockorders)) |
| 2409 | laog__handle_one_lock_deletion(lk); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2410 | map_locks_delete( lk->guestaddr ); |
| 2411 | del_LockN( lk ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2414 | if (HG_(clo_sanity_flags) & SCE_LOCKS) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2415 | all__sanity_check("evh__hg_PTHREAD_RWLOCK_DESTROY_PRE"); |
| 2416 | } |
| 2417 | |
| 2418 | static |
sewardj | 789c3c5 | 2008-02-25 12:10:07 +0000 | [diff] [blame] | 2419 | void evh__HG_PTHREAD_RWLOCK_LOCK_PRE ( ThreadId tid, |
| 2420 | void* rwl, |
| 2421 | Word isW, Word isTryLock ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2422 | { |
| 2423 | /* Just check the rwl is sane; nothing else to do. */ |
| 2424 | // 'rwl' may be invalid - not checked by wrapper |
| 2425 | Thread* thr; |
| 2426 | Lock* lk; |
| 2427 | if (SHOW_EVENTS >= 1) |
| 2428 | VG_(printf)("evh__hg_PTHREAD_RWLOCK_LOCK_PRE(ctid=%d, isW=%d, %p)\n", |
| 2429 | (Int)tid, (Int)isW, (void*)rwl ); |
| 2430 | |
| 2431 | tl_assert(isW == 0 || isW == 1); /* assured us by wrapper */ |
sewardj | 789c3c5 | 2008-02-25 12:10:07 +0000 | [diff] [blame] | 2432 | tl_assert(isTryLock == 0 || isTryLock == 1); /* assured us by wrapper */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2433 | thr = map_threads_maybe_lookup( tid ); |
| 2434 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2435 | |
| 2436 | lk = map_locks_maybe_lookup( (Addr)rwl ); |
| 2437 | if ( lk |
| 2438 | && (lk->kind == LK_nonRec || lk->kind == LK_mbRec) ) { |
| 2439 | /* Wrong kind of lock. Duh. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2440 | HG_(record_error_Misc)( |
| 2441 | thr, "pthread_rwlock_{rd,rw}lock with a " |
| 2442 | "pthread_mutex_t* argument " ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2443 | } |
| 2444 | } |
| 2445 | |
| 2446 | static |
| 2447 | void evh__HG_PTHREAD_RWLOCK_LOCK_POST ( ThreadId tid, void* rwl, Word isW ) |
| 2448 | { |
| 2449 | // only called if the real library call succeeded - so mutex is sane |
| 2450 | Thread* thr; |
| 2451 | if (SHOW_EVENTS >= 1) |
| 2452 | VG_(printf)("evh__hg_PTHREAD_RWLOCK_LOCK_POST(ctid=%d, isW=%d, %p)\n", |
| 2453 | (Int)tid, (Int)isW, (void*)rwl ); |
| 2454 | |
| 2455 | tl_assert(isW == 0 || isW == 1); /* assured us by wrapper */ |
| 2456 | thr = map_threads_maybe_lookup( tid ); |
| 2457 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2458 | |
| 2459 | (isW ? evhH__post_thread_w_acquires_lock |
| 2460 | : evhH__post_thread_r_acquires_lock)( |
| 2461 | thr, |
| 2462 | LK_rdwr, /* if not known, create new lock with this LockKind */ |
| 2463 | (Addr)rwl |
| 2464 | ); |
| 2465 | } |
| 2466 | |
| 2467 | static void evh__HG_PTHREAD_RWLOCK_UNLOCK_PRE ( ThreadId tid, void* rwl ) |
| 2468 | { |
| 2469 | // 'rwl' may be invalid - not checked by wrapper |
| 2470 | Thread* thr; |
| 2471 | if (SHOW_EVENTS >= 1) |
| 2472 | VG_(printf)("evh__HG_PTHREAD_RWLOCK_UNLOCK_PRE(ctid=%d, rwl=%p)\n", |
| 2473 | (Int)tid, (void*)rwl ); |
| 2474 | |
| 2475 | thr = map_threads_maybe_lookup( tid ); |
| 2476 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2477 | |
| 2478 | evhH__pre_thread_releases_lock( thr, (Addr)rwl, True/*isRDWR*/ ); |
| 2479 | } |
| 2480 | |
| 2481 | static void evh__HG_PTHREAD_RWLOCK_UNLOCK_POST ( ThreadId tid, void* rwl ) |
| 2482 | { |
| 2483 | // only called if the real library call succeeded - so mutex is sane |
| 2484 | Thread* thr; |
| 2485 | if (SHOW_EVENTS >= 1) |
| 2486 | VG_(printf)("evh__hg_PTHREAD_RWLOCK_UNLOCK_POST(ctid=%d, rwl=%p)\n", |
| 2487 | (Int)tid, (void*)rwl ); |
| 2488 | thr = map_threads_maybe_lookup( tid ); |
| 2489 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2490 | |
| 2491 | // anything we should do here? |
| 2492 | } |
| 2493 | |
| 2494 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2495 | /* ---------------------------------------------------------- */ |
| 2496 | /* -------------- events to do with semaphores -------------- */ |
| 2497 | /* ---------------------------------------------------------- */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2498 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2499 | /* This is similar to but not identical to the handling for condition |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2500 | variables. */ |
| 2501 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2502 | /* For each semaphore, we maintain a stack of SOs. When a 'post' |
| 2503 | operation is done on a semaphore (unlocking, essentially), a new SO |
| 2504 | is created for the posting thread, the posting thread does a strong |
| 2505 | send to it (which merely installs the posting thread's VC in the |
| 2506 | SO), and the SO is pushed on the semaphore's stack. |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2507 | |
| 2508 | Later, when a (probably different) thread completes 'wait' on the |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2509 | semaphore, we pop a SO off the semaphore's stack (which should be |
| 2510 | nonempty), and do a strong recv from it. This mechanism creates |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2511 | dependencies between posters and waiters of the semaphore. |
| 2512 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2513 | It may not be necessary to use a stack - perhaps a bag of SOs would |
| 2514 | do. But we do need to keep track of how many unused-up posts have |
| 2515 | happened for the semaphore. |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2516 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2517 | 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] | 2518 | twice on S. T3 cannot complete its waits without both T1 and T2 |
| 2519 | posting. The above mechanism will ensure that T3 acquires |
| 2520 | dependencies on both T1 and T2. |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2521 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2522 | When a semaphore is initialised with value N, we do as if we'd |
| 2523 | posted N times on the semaphore: basically create N SOs and do a |
| 2524 | strong send to all of then. This allows up to N waits on the |
| 2525 | semaphore to acquire a dependency on the initialisation point, |
| 2526 | which AFAICS is the correct behaviour. |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2527 | |
| 2528 | We don't emit an error for DESTROY_PRE on a semaphore we don't know |
| 2529 | about. We should. |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2530 | */ |
| 2531 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2532 | /* sem_t* -> XArray* SO* */ |
| 2533 | static WordFM* map_sem_to_SO_stack = NULL; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2534 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2535 | static void map_sem_to_SO_stack_INIT ( void ) { |
| 2536 | if (map_sem_to_SO_stack == NULL) { |
| 2537 | map_sem_to_SO_stack = VG_(newFM)( HG_(zalloc), "hg.mstSs.1", |
| 2538 | HG_(free), NULL ); |
| 2539 | tl_assert(map_sem_to_SO_stack != NULL); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2540 | } |
| 2541 | } |
| 2542 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2543 | static void push_SO_for_sem ( void* sem, SO* so ) { |
| 2544 | UWord keyW; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2545 | XArray* xa; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2546 | tl_assert(so); |
| 2547 | map_sem_to_SO_stack_INIT(); |
| 2548 | if (VG_(lookupFM)( map_sem_to_SO_stack, |
| 2549 | &keyW, (UWord*)&xa, (UWord)sem )) { |
| 2550 | tl_assert(keyW == (UWord)sem); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2551 | tl_assert(xa); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2552 | VG_(addToXA)( xa, &so ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2553 | } else { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2554 | xa = VG_(newXA)( HG_(zalloc), "hg.pSfs.1", HG_(free), sizeof(SO*) ); |
| 2555 | VG_(addToXA)( xa, &so ); |
| 2556 | VG_(addToFM)( map_sem_to_SO_stack, (Word)sem, (Word)xa ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2557 | } |
| 2558 | } |
| 2559 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2560 | static SO* mb_pop_SO_for_sem ( void* sem ) { |
| 2561 | UWord keyW; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2562 | XArray* xa; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2563 | SO* so; |
| 2564 | map_sem_to_SO_stack_INIT(); |
| 2565 | if (VG_(lookupFM)( map_sem_to_SO_stack, |
| 2566 | &keyW, (UWord*)&xa, (UWord)sem )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2567 | /* xa is the stack for this semaphore. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2568 | Word sz; |
| 2569 | tl_assert(keyW == (UWord)sem); |
| 2570 | sz = VG_(sizeXA)( xa ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2571 | tl_assert(sz >= 0); |
| 2572 | if (sz == 0) |
| 2573 | return NULL; /* odd, the stack is empty */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2574 | so = *(SO**)VG_(indexXA)( xa, sz-1 ); |
| 2575 | tl_assert(so); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2576 | VG_(dropTailXA)( xa, 1 ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2577 | return so; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2578 | } else { |
| 2579 | /* hmm, that's odd. No stack for this semaphore. */ |
| 2580 | return NULL; |
| 2581 | } |
| 2582 | } |
| 2583 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2584 | static void evh__HG_POSIX_SEM_DESTROY_PRE ( ThreadId tid, void* sem ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2585 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2586 | UWord keyW, valW; |
| 2587 | SO* so; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2588 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2589 | if (SHOW_EVENTS >= 1) |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2590 | VG_(printf)("evh__HG_POSIX_SEM_DESTROY_PRE(ctid=%d, sem=%p)\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2591 | (Int)tid, (void*)sem ); |
| 2592 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2593 | map_sem_to_SO_stack_INIT(); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2594 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2595 | /* Empty out the semaphore's SO stack. This way of doing it is |
| 2596 | stupid, but at least it's easy. */ |
| 2597 | while (1) { |
| 2598 | so = mb_pop_SO_for_sem( sem ); |
| 2599 | if (!so) break; |
| 2600 | libhb_so_dealloc(so); |
| 2601 | } |
| 2602 | |
| 2603 | if (VG_(delFromFM)( map_sem_to_SO_stack, &keyW, &valW, (UWord)sem )) { |
| 2604 | XArray* xa = (XArray*)valW; |
| 2605 | tl_assert(keyW == (UWord)sem); |
| 2606 | tl_assert(xa); |
| 2607 | tl_assert(VG_(sizeXA)(xa) == 0); /* preceding loop just emptied it */ |
| 2608 | VG_(deleteXA)(xa); |
| 2609 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2612 | static |
| 2613 | void evh__HG_POSIX_SEM_INIT_POST ( ThreadId tid, void* sem, UWord value ) |
| 2614 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2615 | SO* so; |
| 2616 | Thread* thr; |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2617 | |
| 2618 | if (SHOW_EVENTS >= 1) |
| 2619 | VG_(printf)("evh__HG_POSIX_SEM_INIT_POST(ctid=%d, sem=%p, value=%lu)\n", |
| 2620 | (Int)tid, (void*)sem, value ); |
| 2621 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2622 | thr = map_threads_maybe_lookup( tid ); |
| 2623 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2624 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2625 | /* Empty out the semaphore's SO stack. This way of doing it is |
| 2626 | stupid, but at least it's easy. */ |
| 2627 | while (1) { |
| 2628 | so = mb_pop_SO_for_sem( sem ); |
| 2629 | if (!so) break; |
| 2630 | libhb_so_dealloc(so); |
| 2631 | } |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2632 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2633 | /* If we don't do this check, the following while loop runs us out |
| 2634 | of memory for stupid initial values of 'value'. */ |
| 2635 | if (value > 10000) { |
| 2636 | HG_(record_error_Misc)( |
| 2637 | thr, "sem_init: initial value exceeds 10000; using 10000" ); |
| 2638 | value = 10000; |
| 2639 | } |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2640 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2641 | /* Now create 'valid' new SOs for the thread, do a strong send to |
| 2642 | each of them, and push them all on the stack. */ |
| 2643 | for (; value > 0; value--) { |
| 2644 | Thr* hbthr = thr->hbthr; |
| 2645 | tl_assert(hbthr); |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2646 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2647 | so = libhb_so_alloc(); |
| 2648 | libhb_so_send( hbthr, so, True/*strong send*/ ); |
| 2649 | push_SO_for_sem( sem, so ); |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | static void evh__HG_POSIX_SEM_POST_PRE ( ThreadId tid, void* sem ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2654 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2655 | /* 'tid' has posted on 'sem'. Create a new SO, do a strong send to |
| 2656 | it (iow, write our VC into it, then tick ours), and push the SO |
| 2657 | on on a stack of SOs associated with 'sem'. This is later used |
| 2658 | by other thread(s) which successfully exit from a sem_wait on |
| 2659 | the same sem; by doing a strong recv from SOs popped of the |
| 2660 | stack, they acquire dependencies on the posting thread |
| 2661 | segment(s). */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2662 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2663 | Thread* thr; |
| 2664 | SO* so; |
| 2665 | Thr* hbthr; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2666 | |
| 2667 | if (SHOW_EVENTS >= 1) |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2668 | VG_(printf)("evh__HG_POSIX_SEM_POST_PRE(ctid=%d, sem=%p)\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2669 | (Int)tid, (void*)sem ); |
| 2670 | |
| 2671 | thr = map_threads_maybe_lookup( tid ); |
| 2672 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2673 | |
| 2674 | // error-if: sem is bogus |
| 2675 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2676 | hbthr = thr->hbthr; |
| 2677 | tl_assert(hbthr); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2678 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2679 | so = libhb_so_alloc(); |
| 2680 | libhb_so_send( hbthr, so, True/*strong send*/ ); |
| 2681 | push_SO_for_sem( sem, so ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2684 | static void evh__HG_POSIX_SEM_WAIT_POST ( ThreadId tid, void* sem ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2685 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2686 | /* A sem_wait(sem) completed successfully. Pop the posting-SO for |
| 2687 | the 'sem' from this semaphore's SO-stack, and do a strong recv |
| 2688 | from it. This creates a dependency back to one of the post-ers |
| 2689 | for the semaphore. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2690 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2691 | Thread* thr; |
| 2692 | SO* so; |
| 2693 | Thr* hbthr; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2694 | |
| 2695 | if (SHOW_EVENTS >= 1) |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 2696 | VG_(printf)("evh__HG_POSIX_SEM_WAIT_POST(ctid=%d, sem=%p)\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2697 | (Int)tid, (void*)sem ); |
| 2698 | |
| 2699 | thr = map_threads_maybe_lookup( tid ); |
| 2700 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2701 | |
| 2702 | // error-if: sem is bogus |
| 2703 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 2704 | so = mb_pop_SO_for_sem( 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 | if (so) { |
| 2707 | hbthr = thr->hbthr; |
| 2708 | tl_assert(hbthr); |
| 2709 | |
| 2710 | libhb_so_recv( hbthr, so, True/*strong recv*/ ); |
| 2711 | libhb_so_dealloc(so); |
| 2712 | } else { |
| 2713 | /* Hmm. How can a wait on 'sem' succeed if nobody posted to it? |
| 2714 | If this happened it would surely be a bug in the threads |
| 2715 | library. */ |
| 2716 | HG_(record_error_Misc)( |
| 2717 | thr, "Bug in libpthread: sem_wait succeeded on" |
| 2718 | " semaphore without prior sem_post"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2723 | /* -------------------------------------------------------- */ |
| 2724 | /* -------------- events to do with barriers -------------- */ |
| 2725 | /* -------------------------------------------------------- */ |
| 2726 | |
| 2727 | typedef |
| 2728 | struct { |
| 2729 | Bool initted; /* has it yet been initted by guest? */ |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2730 | Bool resizable; /* is resizing allowed? */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2731 | UWord size; /* declared size */ |
| 2732 | XArray* waiting; /* XA of Thread*. # present is 0 .. .size */ |
| 2733 | } |
| 2734 | Bar; |
| 2735 | |
| 2736 | static Bar* new_Bar ( void ) { |
| 2737 | Bar* bar = HG_(zalloc)( "hg.nB.1 (new_Bar)", sizeof(Bar) ); |
| 2738 | tl_assert(bar); |
| 2739 | /* all fields are zero */ |
| 2740 | tl_assert(bar->initted == False); |
| 2741 | return bar; |
| 2742 | } |
| 2743 | |
| 2744 | static void delete_Bar ( Bar* bar ) { |
| 2745 | tl_assert(bar); |
| 2746 | if (bar->waiting) |
| 2747 | VG_(deleteXA)(bar->waiting); |
| 2748 | HG_(free)(bar); |
| 2749 | } |
| 2750 | |
| 2751 | /* A mapping which stores auxiliary data for barriers. */ |
| 2752 | |
| 2753 | /* pthread_barrier_t* -> Bar* */ |
| 2754 | static WordFM* map_barrier_to_Bar = NULL; |
| 2755 | |
| 2756 | static void map_barrier_to_Bar_INIT ( void ) { |
| 2757 | if (UNLIKELY(map_barrier_to_Bar == NULL)) { |
| 2758 | map_barrier_to_Bar = VG_(newFM)( HG_(zalloc), |
| 2759 | "hg.mbtBI.1", HG_(free), NULL ); |
| 2760 | tl_assert(map_barrier_to_Bar != NULL); |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | static Bar* map_barrier_to_Bar_lookup_or_alloc ( void* barrier ) { |
| 2765 | UWord key, val; |
| 2766 | map_barrier_to_Bar_INIT(); |
| 2767 | if (VG_(lookupFM)( map_barrier_to_Bar, &key, &val, (UWord)barrier )) { |
| 2768 | tl_assert(key == (UWord)barrier); |
| 2769 | return (Bar*)val; |
| 2770 | } else { |
| 2771 | Bar* bar = new_Bar(); |
| 2772 | VG_(addToFM)( map_barrier_to_Bar, (UWord)barrier, (UWord)bar ); |
| 2773 | return bar; |
| 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | static void map_barrier_to_Bar_delete ( void* barrier ) { |
| 2778 | UWord keyW, valW; |
| 2779 | map_barrier_to_Bar_INIT(); |
| 2780 | if (VG_(delFromFM)( map_barrier_to_Bar, &keyW, &valW, (UWord)barrier )) { |
| 2781 | Bar* bar = (Bar*)valW; |
| 2782 | tl_assert(keyW == (UWord)barrier); |
| 2783 | delete_Bar(bar); |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | |
| 2788 | static void evh__HG_PTHREAD_BARRIER_INIT_PRE ( ThreadId tid, |
| 2789 | void* barrier, |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2790 | UWord count, |
| 2791 | UWord resizable ) |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2792 | { |
| 2793 | Thread* thr; |
| 2794 | Bar* bar; |
| 2795 | |
| 2796 | if (SHOW_EVENTS >= 1) |
| 2797 | VG_(printf)("evh__HG_PTHREAD_BARRIER_INIT_PRE" |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2798 | "(tid=%d, barrier=%p, count=%lu, resizable=%lu)\n", |
| 2799 | (Int)tid, (void*)barrier, count, resizable ); |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2800 | |
| 2801 | thr = map_threads_maybe_lookup( tid ); |
| 2802 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2803 | |
| 2804 | if (count == 0) { |
| 2805 | HG_(record_error_Misc)( |
| 2806 | thr, "pthread_barrier_init: 'count' argument is zero" |
| 2807 | ); |
| 2808 | } |
| 2809 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2810 | if (resizable != 0 && resizable != 1) { |
| 2811 | HG_(record_error_Misc)( |
| 2812 | thr, "pthread_barrier_init: invalid 'resizable' argument" |
| 2813 | ); |
| 2814 | } |
| 2815 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2816 | bar = map_barrier_to_Bar_lookup_or_alloc(barrier); |
| 2817 | tl_assert(bar); |
| 2818 | |
| 2819 | if (bar->initted) { |
| 2820 | HG_(record_error_Misc)( |
| 2821 | thr, "pthread_barrier_init: barrier is already initialised" |
| 2822 | ); |
| 2823 | } |
| 2824 | |
| 2825 | if (bar->waiting && VG_(sizeXA)(bar->waiting) > 0) { |
| 2826 | tl_assert(bar->initted); |
| 2827 | HG_(record_error_Misc)( |
sewardj | 553655c | 2008-11-14 19:41:19 +0000 | [diff] [blame] | 2828 | thr, "pthread_barrier_init: threads are waiting at barrier" |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2829 | ); |
| 2830 | VG_(dropTailXA)(bar->waiting, VG_(sizeXA)(bar->waiting)); |
| 2831 | } |
| 2832 | if (!bar->waiting) { |
| 2833 | bar->waiting = VG_(newXA)( HG_(zalloc), "hg.eHPBIP.1", HG_(free), |
| 2834 | sizeof(Thread*) ); |
| 2835 | } |
| 2836 | |
| 2837 | tl_assert(bar->waiting); |
| 2838 | tl_assert(VG_(sizeXA)(bar->waiting) == 0); |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2839 | bar->initted = True; |
| 2840 | bar->resizable = resizable == 1 ? True : False; |
| 2841 | bar->size = count; |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | |
| 2845 | static void evh__HG_PTHREAD_BARRIER_DESTROY_PRE ( ThreadId tid, |
| 2846 | void* barrier ) |
| 2847 | { |
sewardj | 553655c | 2008-11-14 19:41:19 +0000 | [diff] [blame] | 2848 | Thread* thr; |
| 2849 | Bar* bar; |
| 2850 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2851 | /* Deal with destroy events. The only purpose is to free storage |
| 2852 | associated with the barrier, so as to avoid any possible |
| 2853 | resource leaks. */ |
| 2854 | if (SHOW_EVENTS >= 1) |
| 2855 | VG_(printf)("evh__HG_PTHREAD_BARRIER_DESTROY_PRE" |
| 2856 | "(tid=%d, barrier=%p)\n", |
| 2857 | (Int)tid, (void*)barrier ); |
| 2858 | |
sewardj | 553655c | 2008-11-14 19:41:19 +0000 | [diff] [blame] | 2859 | thr = map_threads_maybe_lookup( tid ); |
| 2860 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2861 | |
| 2862 | bar = map_barrier_to_Bar_lookup_or_alloc(barrier); |
| 2863 | tl_assert(bar); |
| 2864 | |
| 2865 | if (!bar->initted) { |
| 2866 | HG_(record_error_Misc)( |
| 2867 | thr, "pthread_barrier_destroy: barrier was never initialised" |
| 2868 | ); |
| 2869 | } |
| 2870 | |
| 2871 | if (bar->initted && bar->waiting && VG_(sizeXA)(bar->waiting) > 0) { |
| 2872 | HG_(record_error_Misc)( |
| 2873 | thr, "pthread_barrier_destroy: threads are waiting at barrier" |
| 2874 | ); |
| 2875 | } |
| 2876 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2877 | /* Maybe we shouldn't do this; just let it persist, so that when it |
| 2878 | is reinitialised we don't need to do any dynamic memory |
| 2879 | allocation? The downside is a potentially unlimited space leak, |
| 2880 | if the client creates (in turn) a large number of barriers all |
| 2881 | at different locations. Note that if we do later move to the |
| 2882 | don't-delete-it scheme, we need to mark the barrier as |
| 2883 | uninitialised again since otherwise a later _init call will |
sewardj | 553655c | 2008-11-14 19:41:19 +0000 | [diff] [blame] | 2884 | elicit a duplicate-init error. */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2885 | map_barrier_to_Bar_delete( barrier ); |
| 2886 | } |
| 2887 | |
| 2888 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2889 | /* All the threads have arrived. Now do the Interesting Bit. Get a |
| 2890 | new synchronisation object and do a weak send to it from all the |
| 2891 | participating threads. This makes its vector clocks be the join of |
| 2892 | all the individual threads' vector clocks. Then do a strong |
| 2893 | receive from it back to all threads, so that their VCs are a copy |
| 2894 | of it (hence are all equal to the join of their original VCs.) */ |
| 2895 | static void do_barrier_cross_sync_and_empty ( Bar* bar ) |
| 2896 | { |
| 2897 | /* XXX check bar->waiting has no duplicates */ |
| 2898 | UWord i; |
| 2899 | SO* so = libhb_so_alloc(); |
| 2900 | |
| 2901 | tl_assert(bar->waiting); |
| 2902 | tl_assert(VG_(sizeXA)(bar->waiting) == bar->size); |
| 2903 | |
| 2904 | /* compute the join ... */ |
| 2905 | for (i = 0; i < bar->size; i++) { |
| 2906 | Thread* t = *(Thread**)VG_(indexXA)(bar->waiting, i); |
| 2907 | Thr* hbthr = t->hbthr; |
| 2908 | libhb_so_send( hbthr, so, False/*weak send*/ ); |
| 2909 | } |
| 2910 | /* ... and distribute to all threads */ |
| 2911 | for (i = 0; i < bar->size; i++) { |
| 2912 | Thread* t = *(Thread**)VG_(indexXA)(bar->waiting, i); |
| 2913 | Thr* hbthr = t->hbthr; |
| 2914 | libhb_so_recv( hbthr, so, True/*strong recv*/ ); |
| 2915 | } |
| 2916 | |
| 2917 | /* finally, we must empty out the waiting vector */ |
| 2918 | VG_(dropTailXA)(bar->waiting, VG_(sizeXA)(bar->waiting)); |
| 2919 | |
| 2920 | /* and we don't need this any more. Perhaps a stack-allocated |
| 2921 | SO would be better? */ |
| 2922 | libhb_so_dealloc(so); |
| 2923 | } |
| 2924 | |
| 2925 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2926 | static void evh__HG_PTHREAD_BARRIER_WAIT_PRE ( ThreadId tid, |
| 2927 | void* barrier ) |
| 2928 | { |
sewardj | 1c466b7 | 2008-11-19 11:52:14 +0000 | [diff] [blame] | 2929 | /* This function gets called after a client thread calls |
| 2930 | pthread_barrier_wait but before it arrives at the real |
| 2931 | pthread_barrier_wait. |
| 2932 | |
| 2933 | Why is the following correct? It's a bit subtle. |
| 2934 | |
| 2935 | If this is not the last thread arriving at the barrier, we simply |
| 2936 | note its presence and return. Because valgrind (at least as of |
| 2937 | Nov 08) is single threaded, we are guaranteed safe from any race |
| 2938 | conditions when in this function -- no other client threads are |
| 2939 | running. |
| 2940 | |
| 2941 | If this is the last thread, then we are again the only running |
| 2942 | thread. All the other threads will have either arrived at the |
| 2943 | real pthread_barrier_wait or are on their way to it, but in any |
| 2944 | case are guaranteed not to be able to move past it, because this |
| 2945 | thread is currently in this function and so has not yet arrived |
| 2946 | at the real pthread_barrier_wait. That means that: |
| 2947 | |
| 2948 | 1. While we are in this function, none of the other threads |
| 2949 | waiting at the barrier can move past it. |
| 2950 | |
| 2951 | 2. When this function returns (and simulated execution resumes), |
| 2952 | this thread and all other waiting threads will be able to move |
| 2953 | past the real barrier. |
| 2954 | |
| 2955 | Because of this, it is now safe to update the vector clocks of |
| 2956 | all threads, to represent the fact that they all arrived at the |
| 2957 | barrier and have all moved on. There is no danger of any |
| 2958 | complications to do with some threads leaving the barrier and |
| 2959 | racing back round to the front, whilst others are still leaving |
| 2960 | (which is the primary source of complication in correct handling/ |
| 2961 | implementation of barriers). That can't happen because we update |
| 2962 | here our data structures so as to indicate that the threads have |
| 2963 | passed the barrier, even though, as per (2) above, they are |
| 2964 | guaranteed not to pass the barrier until we return. |
| 2965 | |
| 2966 | This relies crucially on Valgrind being single threaded. If that |
| 2967 | changes, this will need to be reconsidered. |
| 2968 | */ |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2969 | Thread* thr; |
| 2970 | Bar* bar; |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 2971 | UWord present; |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 2972 | |
| 2973 | if (SHOW_EVENTS >= 1) |
| 2974 | VG_(printf)("evh__HG_PTHREAD_BARRIER_WAIT_PRE" |
| 2975 | "(tid=%d, barrier=%p)\n", |
| 2976 | (Int)tid, (void*)barrier ); |
| 2977 | |
| 2978 | thr = map_threads_maybe_lookup( tid ); |
| 2979 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 2980 | |
| 2981 | bar = map_barrier_to_Bar_lookup_or_alloc(barrier); |
| 2982 | tl_assert(bar); |
| 2983 | |
| 2984 | if (!bar->initted) { |
| 2985 | HG_(record_error_Misc)( |
| 2986 | thr, "pthread_barrier_wait: barrier is uninitialised" |
| 2987 | ); |
| 2988 | return; /* client is broken .. avoid assertions below */ |
| 2989 | } |
| 2990 | |
| 2991 | /* guaranteed by _INIT_PRE above */ |
| 2992 | tl_assert(bar->size > 0); |
| 2993 | tl_assert(bar->waiting); |
| 2994 | |
| 2995 | VG_(addToXA)( bar->waiting, &thr ); |
| 2996 | |
| 2997 | /* guaranteed by this function */ |
| 2998 | present = VG_(sizeXA)(bar->waiting); |
| 2999 | tl_assert(present > 0 && present <= bar->size); |
| 3000 | |
| 3001 | if (present < bar->size) |
| 3002 | return; |
| 3003 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 3004 | do_barrier_cross_sync_and_empty(bar); |
| 3005 | } |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3006 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3007 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 3008 | static void evh__HG_PTHREAD_BARRIER_RESIZE_PRE ( ThreadId tid, |
| 3009 | void* barrier, |
| 3010 | UWord newcount ) |
| 3011 | { |
| 3012 | Thread* thr; |
| 3013 | Bar* bar; |
| 3014 | UWord present; |
| 3015 | |
| 3016 | if (SHOW_EVENTS >= 1) |
| 3017 | VG_(printf)("evh__HG_PTHREAD_BARRIER_RESIZE_PRE" |
| 3018 | "(tid=%d, barrier=%p, newcount=%lu)\n", |
| 3019 | (Int)tid, (void*)barrier, newcount ); |
| 3020 | |
| 3021 | thr = map_threads_maybe_lookup( tid ); |
| 3022 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 3023 | |
| 3024 | bar = map_barrier_to_Bar_lookup_or_alloc(barrier); |
| 3025 | tl_assert(bar); |
| 3026 | |
| 3027 | if (!bar->initted) { |
| 3028 | HG_(record_error_Misc)( |
| 3029 | thr, "pthread_barrier_resize: barrier is uninitialised" |
| 3030 | ); |
| 3031 | return; /* client is broken .. avoid assertions below */ |
| 3032 | } |
| 3033 | |
| 3034 | if (!bar->resizable) { |
| 3035 | HG_(record_error_Misc)( |
| 3036 | thr, "pthread_barrier_resize: barrier is may not be resized" |
| 3037 | ); |
| 3038 | return; /* client is broken .. avoid assertions below */ |
| 3039 | } |
| 3040 | |
| 3041 | if (newcount == 0) { |
| 3042 | HG_(record_error_Misc)( |
| 3043 | thr, "pthread_barrier_resize: 'newcount' argument is zero" |
| 3044 | ); |
| 3045 | return; /* client is broken .. avoid assertions below */ |
| 3046 | } |
| 3047 | |
| 3048 | /* guaranteed by _INIT_PRE above */ |
| 3049 | tl_assert(bar->size > 0); |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3050 | tl_assert(bar->waiting); |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 3051 | /* Guaranteed by this fn */ |
| 3052 | tl_assert(newcount > 0); |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3053 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 3054 | if (newcount >= bar->size) { |
| 3055 | /* Increasing the capacity. There's no possibility of threads |
| 3056 | moving on from the barrier in this situation, so just note |
| 3057 | the fact and do nothing more. */ |
| 3058 | bar->size = newcount; |
| 3059 | } else { |
| 3060 | /* Decreasing the capacity. If we decrease it to be equal or |
| 3061 | below the number of waiting threads, they will now move past |
| 3062 | the barrier, so need to mess with dep edges in the same way |
| 3063 | as if the barrier had filled up normally. */ |
| 3064 | present = VG_(sizeXA)(bar->waiting); |
| 3065 | tl_assert(present >= 0 && present <= bar->size); |
| 3066 | if (newcount <= present) { |
| 3067 | bar->size = present; /* keep the cross_sync call happy */ |
| 3068 | do_barrier_cross_sync_and_empty(bar); |
| 3069 | } |
| 3070 | bar->size = newcount; |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3071 | } |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3075 | /* ----------------------------------------------------- */ |
| 3076 | /* ----- events to do with user-specified HB edges ----- */ |
| 3077 | /* ----------------------------------------------------- */ |
| 3078 | |
| 3079 | /* A mapping from arbitrary UWord tag to the SO associated with it. |
| 3080 | The UWord tags are meaningless to us, interpreted only by the |
| 3081 | user. */ |
| 3082 | |
| 3083 | |
| 3084 | |
| 3085 | /* UWord -> SO* */ |
| 3086 | static WordFM* map_usertag_to_SO = NULL; |
| 3087 | |
| 3088 | static void map_usertag_to_SO_INIT ( void ) { |
| 3089 | if (UNLIKELY(map_usertag_to_SO == NULL)) { |
| 3090 | map_usertag_to_SO = VG_(newFM)( HG_(zalloc), |
| 3091 | "hg.mutS.1", HG_(free), NULL ); |
| 3092 | tl_assert(map_usertag_to_SO != NULL); |
| 3093 | } |
| 3094 | } |
| 3095 | |
| 3096 | static SO* map_usertag_to_SO_lookup_or_alloc ( UWord usertag ) { |
| 3097 | UWord key, val; |
| 3098 | map_usertag_to_SO_INIT(); |
| 3099 | if (VG_(lookupFM)( map_usertag_to_SO, &key, &val, usertag )) { |
| 3100 | tl_assert(key == (UWord)usertag); |
| 3101 | return (SO*)val; |
| 3102 | } else { |
| 3103 | SO* so = libhb_so_alloc(); |
| 3104 | VG_(addToFM)( map_usertag_to_SO, usertag, (UWord)so ); |
| 3105 | return so; |
| 3106 | } |
| 3107 | } |
| 3108 | |
sewardj | 6015d0e | 2011-03-11 19:10:48 +0000 | [diff] [blame] | 3109 | static void map_usertag_to_SO_delete ( UWord usertag ) { |
| 3110 | UWord keyW, valW; |
| 3111 | map_usertag_to_SO_INIT(); |
| 3112 | if (VG_(delFromFM)( map_usertag_to_SO, &keyW, &valW, usertag )) { |
| 3113 | SO* so = (SO*)valW; |
| 3114 | tl_assert(keyW == usertag); |
| 3115 | tl_assert(so); |
| 3116 | libhb_so_dealloc(so); |
| 3117 | } |
| 3118 | } |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3119 | |
| 3120 | |
| 3121 | static |
| 3122 | void evh__HG_USERSO_SEND_PRE ( ThreadId tid, UWord usertag ) |
| 3123 | { |
| 3124 | /* TID is just about to notionally sent a message on a notional |
| 3125 | abstract synchronisation object whose identity is given by |
| 3126 | USERTAG. Bind USERTAG to a real SO if it is not already so |
sewardj | 8c50d3c | 2011-03-11 18:38:12 +0000 | [diff] [blame] | 3127 | bound, and do a 'weak send' on the SO. This joins the vector |
| 3128 | clocks from this thread into any vector clocks already present |
| 3129 | in the SO. The resulting SO vector clocks are later used by |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3130 | other thread(s) which successfully 'receive' from the SO, |
sewardj | 8c50d3c | 2011-03-11 18:38:12 +0000 | [diff] [blame] | 3131 | thereby acquiring a dependency on all the events that have |
| 3132 | previously signalled on this SO. */ |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3133 | Thread* thr; |
| 3134 | SO* so; |
| 3135 | |
| 3136 | if (SHOW_EVENTS >= 1) |
| 3137 | VG_(printf)("evh__HG_USERSO_SEND_PRE(ctid=%d, usertag=%#lx)\n", |
| 3138 | (Int)tid, usertag ); |
| 3139 | |
| 3140 | thr = map_threads_maybe_lookup( tid ); |
| 3141 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 3142 | |
| 3143 | so = map_usertag_to_SO_lookup_or_alloc( usertag ); |
| 3144 | tl_assert(so); |
| 3145 | |
sewardj | 8c50d3c | 2011-03-11 18:38:12 +0000 | [diff] [blame] | 3146 | libhb_so_send( thr->hbthr, so, False/*!strong_send*/ ); |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | static |
| 3150 | void evh__HG_USERSO_RECV_POST ( ThreadId tid, UWord usertag ) |
| 3151 | { |
| 3152 | /* TID has just notionally received a message from a notional |
| 3153 | abstract synchronisation object whose identity is given by |
| 3154 | USERTAG. Bind USERTAG to a real SO if it is not already so |
| 3155 | bound. If the SO has at some point in the past been 'sent' on, |
| 3156 | to a 'strong receive' on it, thereby acquiring a dependency on |
| 3157 | the sender. */ |
| 3158 | Thread* thr; |
| 3159 | SO* so; |
| 3160 | |
| 3161 | if (SHOW_EVENTS >= 1) |
| 3162 | VG_(printf)("evh__HG_USERSO_RECV_POST(ctid=%d, usertag=%#lx)\n", |
| 3163 | (Int)tid, usertag ); |
| 3164 | |
| 3165 | thr = map_threads_maybe_lookup( tid ); |
| 3166 | tl_assert(thr); /* cannot fail - Thread* must already exist */ |
| 3167 | |
| 3168 | so = map_usertag_to_SO_lookup_or_alloc( usertag ); |
| 3169 | tl_assert(so); |
| 3170 | |
| 3171 | /* Acquire a dependency on it. If the SO has never so far been |
| 3172 | sent on, then libhb_so_recv will do nothing. So we're safe |
| 3173 | regardless of SO's history. */ |
| 3174 | libhb_so_recv( thr->hbthr, so, True/*strong_recv*/ ); |
| 3175 | } |
| 3176 | |
sewardj | 6015d0e | 2011-03-11 19:10:48 +0000 | [diff] [blame] | 3177 | static |
| 3178 | void evh__HG_USERSO_FORGET_ALL ( ThreadId tid, UWord usertag ) |
| 3179 | { |
| 3180 | /* TID declares that any happens-before edges notionally stored in |
| 3181 | USERTAG can be deleted. If (as would normally be the case) a |
| 3182 | SO is associated with USERTAG, then the assocation is removed |
| 3183 | and all resources associated with SO are freed. Importantly, |
| 3184 | that frees up any VTSs stored in SO. */ |
| 3185 | if (SHOW_EVENTS >= 1) |
| 3186 | VG_(printf)("evh__HG_USERSO_FORGET_ALL(ctid=%d, usertag=%#lx)\n", |
| 3187 | (Int)tid, usertag ); |
| 3188 | |
| 3189 | map_usertag_to_SO_delete( usertag ); |
| 3190 | } |
| 3191 | |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 3192 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3193 | /*--------------------------------------------------------------*/ |
| 3194 | /*--- Lock acquisition order monitoring ---*/ |
| 3195 | /*--------------------------------------------------------------*/ |
| 3196 | |
| 3197 | /* FIXME: here are some optimisations still to do in |
| 3198 | laog__pre_thread_acquires_lock. |
| 3199 | |
| 3200 | The graph is structured so that if L1 --*--> L2 then L1 must be |
| 3201 | acquired before L2. |
| 3202 | |
| 3203 | The common case is that some thread T holds (eg) L1 L2 and L3 and |
| 3204 | is repeatedly acquiring and releasing Ln, and there is no ordering |
| 3205 | error in what it is doing. Hence it repeatly: |
| 3206 | |
| 3207 | (1) searches laog to see if Ln --*--> {L1,L2,L3}, which always |
| 3208 | produces the answer No (because there is no error). |
| 3209 | |
| 3210 | (2) adds edges {L1,L2,L3} --> Ln to laog, which are already present |
| 3211 | (because they already got added the first time T acquired Ln). |
| 3212 | |
| 3213 | Hence cache these two events: |
| 3214 | |
| 3215 | (1) Cache result of the query from last time. Invalidate the cache |
| 3216 | any time any edges are added to or deleted from laog. |
| 3217 | |
| 3218 | (2) Cache these add-edge requests and ignore them if said edges |
| 3219 | have already been added to laog. Invalidate the cache any time |
| 3220 | any edges are deleted from laog. |
| 3221 | */ |
| 3222 | |
| 3223 | typedef |
| 3224 | struct { |
| 3225 | WordSetID inns; /* in univ_laog */ |
| 3226 | WordSetID outs; /* in univ_laog */ |
| 3227 | } |
| 3228 | LAOGLinks; |
| 3229 | |
| 3230 | /* lock order acquisition graph */ |
| 3231 | static WordFM* laog = NULL; /* WordFM Lock* LAOGLinks* */ |
| 3232 | |
| 3233 | /* EXPOSITION ONLY: for each edge in 'laog', record the two places |
| 3234 | where that edge was created, so that we can show the user later if |
| 3235 | we need to. */ |
| 3236 | typedef |
| 3237 | struct { |
| 3238 | Addr src_ga; /* Lock guest addresses for */ |
| 3239 | Addr dst_ga; /* src/dst of the edge */ |
| 3240 | ExeContext* src_ec; /* And corresponding places where that */ |
| 3241 | ExeContext* dst_ec; /* ordering was established */ |
| 3242 | } |
| 3243 | LAOGLinkExposition; |
| 3244 | |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3245 | static Word cmp_LAOGLinkExposition ( UWord llx1W, UWord llx2W ) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3246 | /* Compare LAOGLinkExposition*s by (src_ga,dst_ga) field pair. */ |
| 3247 | LAOGLinkExposition* llx1 = (LAOGLinkExposition*)llx1W; |
| 3248 | LAOGLinkExposition* llx2 = (LAOGLinkExposition*)llx2W; |
| 3249 | if (llx1->src_ga < llx2->src_ga) return -1; |
| 3250 | if (llx1->src_ga > llx2->src_ga) return 1; |
| 3251 | if (llx1->dst_ga < llx2->dst_ga) return -1; |
| 3252 | if (llx1->dst_ga > llx2->dst_ga) return 1; |
| 3253 | return 0; |
| 3254 | } |
| 3255 | |
| 3256 | static WordFM* laog_exposition = NULL; /* WordFM LAOGLinkExposition* NULL */ |
| 3257 | /* end EXPOSITION ONLY */ |
| 3258 | |
| 3259 | |
sewardj | a65db10 | 2009-01-26 10:45:16 +0000 | [diff] [blame] | 3260 | __attribute__((noinline)) |
| 3261 | static void laog__init ( void ) |
| 3262 | { |
| 3263 | tl_assert(!laog); |
| 3264 | tl_assert(!laog_exposition); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 3265 | tl_assert(HG_(clo_track_lockorders)); |
sewardj | a65db10 | 2009-01-26 10:45:16 +0000 | [diff] [blame] | 3266 | |
| 3267 | laog = VG_(newFM)( HG_(zalloc), "hg.laog__init.1", |
| 3268 | HG_(free), NULL/*unboxedcmp*/ ); |
| 3269 | |
| 3270 | laog_exposition = VG_(newFM)( HG_(zalloc), "hg.laog__init.2", HG_(free), |
| 3271 | cmp_LAOGLinkExposition ); |
| 3272 | tl_assert(laog); |
| 3273 | tl_assert(laog_exposition); |
| 3274 | } |
| 3275 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3276 | static void laog__show ( Char* who ) { |
| 3277 | Word i, ws_size; |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3278 | UWord* ws_words; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3279 | Lock* me; |
| 3280 | LAOGLinks* links; |
| 3281 | VG_(printf)("laog (requested by %s) {\n", who); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3282 | VG_(initIterFM)( laog ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3283 | me = NULL; |
| 3284 | links = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3285 | while (VG_(nextIterFM)( laog, (Word*)&me, |
sewardj | b5f2964 | 2007-11-16 12:02:43 +0000 | [diff] [blame] | 3286 | (Word*)&links )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3287 | tl_assert(me); |
| 3288 | tl_assert(links); |
| 3289 | VG_(printf)(" node %p:\n", me); |
| 3290 | HG_(getPayloadWS)( &ws_words, &ws_size, univ_laog, links->inns ); |
| 3291 | for (i = 0; i < ws_size; i++) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3292 | VG_(printf)(" inn %#lx\n", ws_words[i] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3293 | HG_(getPayloadWS)( &ws_words, &ws_size, univ_laog, links->outs ); |
| 3294 | for (i = 0; i < ws_size; i++) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3295 | VG_(printf)(" out %#lx\n", ws_words[i] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3296 | me = NULL; |
| 3297 | links = NULL; |
| 3298 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3299 | VG_(doneIterFM)( laog ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3300 | VG_(printf)("}\n"); |
| 3301 | } |
| 3302 | |
| 3303 | __attribute__((noinline)) |
| 3304 | static void laog__add_edge ( Lock* src, Lock* dst ) { |
| 3305 | Word keyW; |
| 3306 | LAOGLinks* links; |
| 3307 | Bool presentF, presentR; |
| 3308 | if (0) VG_(printf)("laog__add_edge %p %p\n", src, dst); |
| 3309 | |
| 3310 | /* Take the opportunity to sanity check the graph. Record in |
| 3311 | presentF if there is already a src->dst mapping in this node's |
| 3312 | forwards links, and presentR if there is already a src->dst |
| 3313 | mapping in this node's backwards links. They should agree! |
| 3314 | Also, we need to know whether the edge was already present so as |
| 3315 | to decide whether or not to update the link details mapping. We |
| 3316 | can compute presentF and presentR essentially for free, so may |
| 3317 | as well do this always. */ |
| 3318 | presentF = presentR = False; |
| 3319 | |
| 3320 | /* Update the out edges for src */ |
| 3321 | keyW = 0; |
| 3322 | links = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3323 | if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)src )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3324 | WordSetID outs_new; |
| 3325 | tl_assert(links); |
| 3326 | tl_assert(keyW == (Word)src); |
| 3327 | outs_new = HG_(addToWS)( univ_laog, links->outs, (Word)dst ); |
| 3328 | presentF = outs_new == links->outs; |
| 3329 | links->outs = outs_new; |
| 3330 | } else { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3331 | links = HG_(zalloc)("hg.lae.1", sizeof(LAOGLinks)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3332 | links->inns = HG_(emptyWS)( univ_laog ); |
| 3333 | links->outs = HG_(singletonWS)( univ_laog, (Word)dst ); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3334 | VG_(addToFM)( laog, (Word)src, (Word)links ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3335 | } |
| 3336 | /* Update the in edges for dst */ |
| 3337 | keyW = 0; |
| 3338 | links = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3339 | if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)dst )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3340 | WordSetID inns_new; |
| 3341 | tl_assert(links); |
| 3342 | tl_assert(keyW == (Word)dst); |
| 3343 | inns_new = HG_(addToWS)( univ_laog, links->inns, (Word)src ); |
| 3344 | presentR = inns_new == links->inns; |
| 3345 | links->inns = inns_new; |
| 3346 | } else { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3347 | links = HG_(zalloc)("hg.lae.2", sizeof(LAOGLinks)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3348 | links->inns = HG_(singletonWS)( univ_laog, (Word)src ); |
| 3349 | links->outs = HG_(emptyWS)( univ_laog ); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3350 | VG_(addToFM)( laog, (Word)dst, (Word)links ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | tl_assert( (presentF && presentR) || (!presentF && !presentR) ); |
| 3354 | |
| 3355 | if (!presentF && src->acquired_at && dst->acquired_at) { |
| 3356 | LAOGLinkExposition expo; |
| 3357 | /* If this edge is entering the graph, and we have acquired_at |
| 3358 | information for both src and dst, record those acquisition |
| 3359 | points. Hence, if there is later a violation of this |
| 3360 | ordering, we can show the user the two places in which the |
| 3361 | required src-dst ordering was previously established. */ |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 3362 | if (0) VG_(printf)("acquire edge %#lx %#lx\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3363 | src->guestaddr, dst->guestaddr); |
| 3364 | expo.src_ga = src->guestaddr; |
| 3365 | expo.dst_ga = dst->guestaddr; |
| 3366 | expo.src_ec = NULL; |
| 3367 | expo.dst_ec = NULL; |
| 3368 | tl_assert(laog_exposition); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3369 | if (VG_(lookupFM)( laog_exposition, NULL, NULL, (Word)&expo )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3370 | /* we already have it; do nothing */ |
| 3371 | } else { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3372 | LAOGLinkExposition* expo2 = HG_(zalloc)("hg.lae.3", |
| 3373 | sizeof(LAOGLinkExposition)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3374 | expo2->src_ga = src->guestaddr; |
| 3375 | expo2->dst_ga = dst->guestaddr; |
| 3376 | expo2->src_ec = src->acquired_at; |
| 3377 | expo2->dst_ec = dst->acquired_at; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3378 | VG_(addToFM)( laog_exposition, (Word)expo2, (Word)NULL ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3379 | } |
| 3380 | } |
| 3381 | } |
| 3382 | |
| 3383 | __attribute__((noinline)) |
| 3384 | static void laog__del_edge ( Lock* src, Lock* dst ) { |
| 3385 | Word keyW; |
| 3386 | LAOGLinks* links; |
| 3387 | if (0) VG_(printf)("laog__del_edge %p %p\n", src, dst); |
| 3388 | /* Update the out edges for src */ |
| 3389 | keyW = 0; |
| 3390 | links = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3391 | if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)src )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3392 | tl_assert(links); |
| 3393 | tl_assert(keyW == (Word)src); |
| 3394 | links->outs = HG_(delFromWS)( univ_laog, links->outs, (Word)dst ); |
| 3395 | } |
| 3396 | /* Update the in edges for dst */ |
| 3397 | keyW = 0; |
| 3398 | links = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3399 | if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)dst )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3400 | tl_assert(links); |
| 3401 | tl_assert(keyW == (Word)dst); |
| 3402 | links->inns = HG_(delFromWS)( univ_laog, links->inns, (Word)src ); |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | __attribute__((noinline)) |
| 3407 | static WordSetID /* in univ_laog */ laog__succs ( Lock* lk ) { |
| 3408 | Word keyW; |
| 3409 | LAOGLinks* links; |
| 3410 | keyW = 0; |
| 3411 | links = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3412 | if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)lk )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3413 | tl_assert(links); |
| 3414 | tl_assert(keyW == (Word)lk); |
| 3415 | return links->outs; |
| 3416 | } else { |
| 3417 | return HG_(emptyWS)( univ_laog ); |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | __attribute__((noinline)) |
| 3422 | static WordSetID /* in univ_laog */ laog__preds ( Lock* lk ) { |
| 3423 | Word keyW; |
| 3424 | LAOGLinks* links; |
| 3425 | keyW = 0; |
| 3426 | links = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3427 | if (VG_(lookupFM)( laog, &keyW, (Word*)&links, (Word)lk )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3428 | tl_assert(links); |
| 3429 | tl_assert(keyW == (Word)lk); |
| 3430 | return links->inns; |
| 3431 | } else { |
| 3432 | return HG_(emptyWS)( univ_laog ); |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | __attribute__((noinline)) |
| 3437 | static void laog__sanity_check ( Char* who ) { |
| 3438 | Word i, ws_size; |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3439 | UWord* ws_words; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3440 | Lock* me; |
| 3441 | LAOGLinks* links; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3442 | VG_(initIterFM)( laog ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3443 | me = NULL; |
| 3444 | links = NULL; |
| 3445 | if (0) VG_(printf)("laog sanity check\n"); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3446 | while (VG_(nextIterFM)( laog, (Word*)&me, |
sewardj | b5f2964 | 2007-11-16 12:02:43 +0000 | [diff] [blame] | 3447 | (Word*)&links )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3448 | tl_assert(me); |
| 3449 | tl_assert(links); |
| 3450 | HG_(getPayloadWS)( &ws_words, &ws_size, univ_laog, links->inns ); |
| 3451 | for (i = 0; i < ws_size; i++) { |
| 3452 | if ( ! HG_(elemWS)( univ_laog, |
| 3453 | laog__succs( (Lock*)ws_words[i] ), |
| 3454 | (Word)me )) |
| 3455 | goto bad; |
| 3456 | } |
| 3457 | HG_(getPayloadWS)( &ws_words, &ws_size, univ_laog, links->outs ); |
| 3458 | for (i = 0; i < ws_size; i++) { |
| 3459 | if ( ! HG_(elemWS)( univ_laog, |
| 3460 | laog__preds( (Lock*)ws_words[i] ), |
| 3461 | (Word)me )) |
| 3462 | goto bad; |
| 3463 | } |
| 3464 | me = NULL; |
| 3465 | links = NULL; |
| 3466 | } |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3467 | VG_(doneIterFM)( laog ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3468 | return; |
| 3469 | |
| 3470 | bad: |
| 3471 | VG_(printf)("laog__sanity_check(%s) FAILED\n", who); |
| 3472 | laog__show(who); |
| 3473 | tl_assert(0); |
| 3474 | } |
| 3475 | |
| 3476 | /* If there is a path in laog from 'src' to any of the elements in |
| 3477 | 'dst', return an arbitrarily chosen element of 'dst' reachable from |
| 3478 | 'src'. If no path exist from 'src' to any element in 'dst', return |
| 3479 | NULL. */ |
| 3480 | __attribute__((noinline)) |
| 3481 | static |
| 3482 | Lock* laog__do_dfs_from_to ( Lock* src, WordSetID dsts /* univ_lsets */ ) |
| 3483 | { |
| 3484 | Lock* ret; |
| 3485 | Word i, ssz; |
| 3486 | XArray* stack; /* of Lock* */ |
| 3487 | WordFM* visited; /* Lock* -> void, iow, Set(Lock*) */ |
| 3488 | Lock* here; |
| 3489 | WordSetID succs; |
| 3490 | Word succs_size; |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3491 | UWord* succs_words; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3492 | //laog__sanity_check(); |
| 3493 | |
| 3494 | /* If the destination set is empty, we can never get there from |
| 3495 | 'src' :-), so don't bother to try */ |
| 3496 | if (HG_(isEmptyWS)( univ_lsets, dsts )) |
| 3497 | return NULL; |
| 3498 | |
| 3499 | ret = NULL; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3500 | stack = VG_(newXA)( HG_(zalloc), "hg.lddft.1", HG_(free), sizeof(Lock*) ); |
| 3501 | visited = VG_(newFM)( HG_(zalloc), "hg.lddft.2", HG_(free), NULL/*unboxedcmp*/ ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3502 | |
| 3503 | (void) VG_(addToXA)( stack, &src ); |
| 3504 | |
| 3505 | while (True) { |
| 3506 | |
| 3507 | ssz = VG_(sizeXA)( stack ); |
| 3508 | |
| 3509 | if (ssz == 0) { ret = NULL; break; } |
| 3510 | |
| 3511 | here = *(Lock**) VG_(indexXA)( stack, ssz-1 ); |
| 3512 | VG_(dropTailXA)( stack, 1 ); |
| 3513 | |
| 3514 | if (HG_(elemWS)( univ_lsets, dsts, (Word)here )) { ret = here; break; } |
| 3515 | |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3516 | if (VG_(lookupFM)( visited, NULL, NULL, (Word)here )) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3517 | continue; |
| 3518 | |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3519 | VG_(addToFM)( visited, (Word)here, 0 ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3520 | |
| 3521 | succs = laog__succs( here ); |
| 3522 | HG_(getPayloadWS)( &succs_words, &succs_size, univ_laog, succs ); |
| 3523 | for (i = 0; i < succs_size; i++) |
| 3524 | (void) VG_(addToXA)( stack, &succs_words[i] ); |
| 3525 | } |
| 3526 | |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3527 | VG_(deleteFM)( visited, NULL, NULL ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3528 | VG_(deleteXA)( stack ); |
| 3529 | return ret; |
| 3530 | } |
| 3531 | |
| 3532 | |
| 3533 | /* Thread 'thr' is acquiring 'lk'. Check for inconsistent ordering |
| 3534 | between 'lk' and the locks already held by 'thr' and issue a |
| 3535 | complaint if so. Also, update the ordering graph appropriately. |
| 3536 | */ |
| 3537 | __attribute__((noinline)) |
| 3538 | static void laog__pre_thread_acquires_lock ( |
| 3539 | Thread* thr, /* NB: BEFORE lock is added */ |
| 3540 | Lock* lk |
| 3541 | ) |
| 3542 | { |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3543 | UWord* ls_words; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3544 | Word ls_size, i; |
| 3545 | Lock* other; |
| 3546 | |
| 3547 | /* It may be that 'thr' already holds 'lk' and is recursively |
| 3548 | relocking in. In this case we just ignore the call. */ |
| 3549 | /* NB: univ_lsets really is correct here */ |
| 3550 | if (HG_(elemWS)( univ_lsets, thr->locksetA, (Word)lk )) |
| 3551 | return; |
| 3552 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3553 | /* First, the check. Complain if there is any path in laog from lk |
| 3554 | to any of the locks already held by thr, since if any such path |
| 3555 | existed, it would mean that previously lk was acquired before |
| 3556 | (rather than after, as we are doing here) at least one of those |
| 3557 | locks. |
| 3558 | */ |
| 3559 | other = laog__do_dfs_from_to(lk, thr->locksetA); |
| 3560 | if (other) { |
| 3561 | LAOGLinkExposition key, *found; |
| 3562 | /* So we managed to find a path lk --*--> other in the graph, |
| 3563 | which implies that 'lk' should have been acquired before |
| 3564 | 'other' but is in fact being acquired afterwards. We present |
| 3565 | the lk/other arguments to record_error_LockOrder in the order |
| 3566 | in which they should have been acquired. */ |
| 3567 | /* Go look in the laog_exposition mapping, to find the allocation |
| 3568 | points for this edge, so we can show the user. */ |
| 3569 | key.src_ga = lk->guestaddr; |
| 3570 | key.dst_ga = other->guestaddr; |
| 3571 | key.src_ec = NULL; |
| 3572 | key.dst_ec = NULL; |
| 3573 | found = NULL; |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 3574 | if (VG_(lookupFM)( laog_exposition, |
sewardj | b5f2964 | 2007-11-16 12:02:43 +0000 | [diff] [blame] | 3575 | (Word*)&found, NULL, (Word)&key )) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3576 | tl_assert(found != &key); |
| 3577 | tl_assert(found->src_ga == key.src_ga); |
| 3578 | tl_assert(found->dst_ga == key.dst_ga); |
| 3579 | tl_assert(found->src_ec); |
| 3580 | tl_assert(found->dst_ec); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3581 | HG_(record_error_LockOrder)( |
| 3582 | thr, lk->guestaddr, other->guestaddr, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 3583 | found->src_ec, found->dst_ec, other->acquired_at ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3584 | } else { |
| 3585 | /* Hmm. This can't happen (can it?) */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3586 | HG_(record_error_LockOrder)( |
| 3587 | thr, lk->guestaddr, other->guestaddr, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 3588 | NULL, NULL, NULL ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3589 | } |
| 3590 | } |
| 3591 | |
| 3592 | /* Second, add to laog the pairs |
| 3593 | (old, lk) | old <- locks already held by thr |
| 3594 | Since both old and lk are currently held by thr, their acquired_at |
| 3595 | fields must be non-NULL. |
| 3596 | */ |
| 3597 | tl_assert(lk->acquired_at); |
| 3598 | HG_(getPayloadWS)( &ls_words, &ls_size, univ_lsets, thr->locksetA ); |
| 3599 | for (i = 0; i < ls_size; i++) { |
| 3600 | Lock* old = (Lock*)ls_words[i]; |
| 3601 | tl_assert(old->acquired_at); |
| 3602 | laog__add_edge( old, lk ); |
| 3603 | } |
| 3604 | |
| 3605 | /* Why "except_Locks" ? We're here because a lock is being |
| 3606 | acquired by a thread, and we're in an inconsistent state here. |
| 3607 | See the call points in evhH__post_thread_{r,w}_acquires_lock. |
| 3608 | When called in this inconsistent state, locks__sanity_check duly |
| 3609 | barfs. */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3610 | if (HG_(clo_sanity_flags) & SCE_LAOG) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3611 | all_except_Locks__sanity_check("laog__pre_thread_acquires_lock-post"); |
| 3612 | } |
| 3613 | |
| 3614 | |
| 3615 | /* Delete from 'laog' any pair mentioning a lock in locksToDelete */ |
| 3616 | |
| 3617 | __attribute__((noinline)) |
| 3618 | static void laog__handle_one_lock_deletion ( Lock* lk ) |
| 3619 | { |
| 3620 | WordSetID preds, succs; |
| 3621 | Word preds_size, succs_size, i, j; |
sewardj | 250ec2e | 2008-02-15 22:02:30 +0000 | [diff] [blame] | 3622 | UWord *preds_words, *succs_words; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3623 | |
| 3624 | preds = laog__preds( lk ); |
| 3625 | succs = laog__succs( lk ); |
| 3626 | |
| 3627 | HG_(getPayloadWS)( &preds_words, &preds_size, univ_laog, preds ); |
| 3628 | for (i = 0; i < preds_size; i++) |
| 3629 | laog__del_edge( (Lock*)preds_words[i], lk ); |
| 3630 | |
| 3631 | HG_(getPayloadWS)( &succs_words, &succs_size, univ_laog, succs ); |
| 3632 | for (j = 0; j < succs_size; j++) |
| 3633 | laog__del_edge( lk, (Lock*)succs_words[j] ); |
| 3634 | |
| 3635 | for (i = 0; i < preds_size; i++) { |
| 3636 | for (j = 0; j < succs_size; j++) { |
| 3637 | if (preds_words[i] != succs_words[j]) { |
| 3638 | /* This can pass unlocked locks to laog__add_edge, since |
| 3639 | we're deleting stuff. So their acquired_at fields may |
| 3640 | be NULL. */ |
| 3641 | laog__add_edge( (Lock*)preds_words[i], (Lock*)succs_words[j] ); |
| 3642 | } |
| 3643 | } |
| 3644 | } |
| 3645 | } |
| 3646 | |
sewardj | 1cbc12f | 2008-11-10 16:16:46 +0000 | [diff] [blame] | 3647 | //__attribute__((noinline)) |
| 3648 | //static void laog__handle_lock_deletions ( |
| 3649 | // WordSetID /* in univ_laog */ locksToDelete |
| 3650 | // ) |
| 3651 | //{ |
| 3652 | // Word i, ws_size; |
| 3653 | // UWord* ws_words; |
| 3654 | // |
sewardj | 1cbc12f | 2008-11-10 16:16:46 +0000 | [diff] [blame] | 3655 | // |
| 3656 | // HG_(getPayloadWS)( &ws_words, &ws_size, univ_lsets, locksToDelete ); |
| 3657 | // for (i = 0; i < ws_size; i++) |
| 3658 | // laog__handle_one_lock_deletion( (Lock*)ws_words[i] ); |
| 3659 | // |
| 3660 | // if (HG_(clo_sanity_flags) & SCE_LAOG) |
| 3661 | // all__sanity_check("laog__handle_lock_deletions-post"); |
| 3662 | //} |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3663 | |
| 3664 | |
| 3665 | /*--------------------------------------------------------------*/ |
| 3666 | /*--- Malloc/free replacements ---*/ |
| 3667 | /*--------------------------------------------------------------*/ |
| 3668 | |
| 3669 | typedef |
| 3670 | struct { |
| 3671 | void* next; /* required by m_hashtable */ |
| 3672 | Addr payload; /* ptr to actual block */ |
| 3673 | SizeT szB; /* size requested */ |
| 3674 | ExeContext* where; /* where it was allocated */ |
| 3675 | Thread* thr; /* allocating thread */ |
| 3676 | } |
| 3677 | MallocMeta; |
| 3678 | |
| 3679 | /* A hash table of MallocMetas, used to track malloc'd blocks |
| 3680 | (obviously). */ |
| 3681 | static VgHashTable hg_mallocmeta_table = NULL; |
| 3682 | |
| 3683 | |
| 3684 | static MallocMeta* new_MallocMeta ( void ) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3685 | MallocMeta* md = HG_(zalloc)( "hg.new_MallocMeta.1", sizeof(MallocMeta) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3686 | tl_assert(md); |
| 3687 | return md; |
| 3688 | } |
| 3689 | static void delete_MallocMeta ( MallocMeta* md ) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3690 | HG_(free)(md); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
| 3693 | |
| 3694 | /* Allocate a client block and set up the metadata for it. */ |
| 3695 | |
| 3696 | static |
| 3697 | void* handle_alloc ( ThreadId tid, |
| 3698 | SizeT szB, SizeT alignB, Bool is_zeroed ) |
| 3699 | { |
| 3700 | Addr p; |
| 3701 | MallocMeta* md; |
| 3702 | |
| 3703 | tl_assert( ((SSizeT)szB) >= 0 ); |
| 3704 | p = (Addr)VG_(cli_malloc)(alignB, szB); |
| 3705 | if (!p) { |
| 3706 | return NULL; |
| 3707 | } |
| 3708 | if (is_zeroed) |
| 3709 | VG_(memset)((void*)p, 0, szB); |
| 3710 | |
| 3711 | /* Note that map_threads_lookup must succeed (cannot assert), since |
| 3712 | memory can only be allocated by currently alive threads, hence |
| 3713 | they must have an entry in map_threads. */ |
| 3714 | md = new_MallocMeta(); |
| 3715 | md->payload = p; |
| 3716 | md->szB = szB; |
| 3717 | md->where = VG_(record_ExeContext)( tid, 0 ); |
| 3718 | md->thr = map_threads_lookup( tid ); |
| 3719 | |
| 3720 | VG_(HT_add_node)( hg_mallocmeta_table, (VgHashNode*)md ); |
| 3721 | |
| 3722 | /* Tell the lower level memory wranglers. */ |
| 3723 | evh__new_mem_heap( p, szB, is_zeroed ); |
| 3724 | |
| 3725 | return (void*)p; |
| 3726 | } |
| 3727 | |
| 3728 | /* Re the checks for less-than-zero (also in hg_cli__realloc below): |
| 3729 | Cast to a signed type to catch any unexpectedly negative args. |
| 3730 | We're assuming here that the size asked for is not greater than |
| 3731 | 2^31 bytes (for 32-bit platforms) or 2^63 bytes (for 64-bit |
| 3732 | platforms). */ |
| 3733 | static void* hg_cli__malloc ( ThreadId tid, SizeT n ) { |
| 3734 | if (((SSizeT)n) < 0) return NULL; |
| 3735 | return handle_alloc ( tid, n, VG_(clo_alignment), |
| 3736 | /*is_zeroed*/False ); |
| 3737 | } |
| 3738 | static void* hg_cli____builtin_new ( ThreadId tid, SizeT n ) { |
| 3739 | if (((SSizeT)n) < 0) return NULL; |
| 3740 | return handle_alloc ( tid, n, VG_(clo_alignment), |
| 3741 | /*is_zeroed*/False ); |
| 3742 | } |
| 3743 | static void* hg_cli____builtin_vec_new ( ThreadId tid, SizeT n ) { |
| 3744 | if (((SSizeT)n) < 0) return NULL; |
| 3745 | return handle_alloc ( tid, n, VG_(clo_alignment), |
| 3746 | /*is_zeroed*/False ); |
| 3747 | } |
| 3748 | static void* hg_cli__memalign ( ThreadId tid, SizeT align, SizeT n ) { |
| 3749 | if (((SSizeT)n) < 0) return NULL; |
| 3750 | return handle_alloc ( tid, n, align, |
| 3751 | /*is_zeroed*/False ); |
| 3752 | } |
| 3753 | static void* hg_cli__calloc ( ThreadId tid, SizeT nmemb, SizeT size1 ) { |
| 3754 | if ( ((SSizeT)nmemb) < 0 || ((SSizeT)size1) < 0 ) return NULL; |
| 3755 | return handle_alloc ( tid, nmemb*size1, VG_(clo_alignment), |
| 3756 | /*is_zeroed*/True ); |
| 3757 | } |
| 3758 | |
| 3759 | |
| 3760 | /* Free a client block, including getting rid of the relevant |
| 3761 | metadata. */ |
| 3762 | |
| 3763 | static void handle_free ( ThreadId tid, void* p ) |
| 3764 | { |
| 3765 | MallocMeta *md, *old_md; |
| 3766 | SizeT szB; |
| 3767 | |
| 3768 | /* First see if we can find the metadata for 'p'. */ |
| 3769 | md = (MallocMeta*) VG_(HT_lookup)( hg_mallocmeta_table, (UWord)p ); |
| 3770 | if (!md) |
| 3771 | return; /* apparently freeing a bogus address. Oh well. */ |
| 3772 | |
| 3773 | tl_assert(md->payload == (Addr)p); |
| 3774 | szB = md->szB; |
| 3775 | |
| 3776 | /* Nuke the metadata block */ |
| 3777 | old_md = (MallocMeta*) |
| 3778 | VG_(HT_remove)( hg_mallocmeta_table, (UWord)p ); |
| 3779 | tl_assert(old_md); /* it must be present - we just found it */ |
| 3780 | tl_assert(old_md == md); |
| 3781 | tl_assert(old_md->payload == (Addr)p); |
| 3782 | |
| 3783 | VG_(cli_free)((void*)old_md->payload); |
| 3784 | delete_MallocMeta(old_md); |
| 3785 | |
| 3786 | /* Tell the lower level memory wranglers. */ |
| 3787 | evh__die_mem_heap( (Addr)p, szB ); |
| 3788 | } |
| 3789 | |
| 3790 | static void hg_cli__free ( ThreadId tid, void* p ) { |
| 3791 | handle_free(tid, p); |
| 3792 | } |
| 3793 | static void hg_cli____builtin_delete ( ThreadId tid, void* p ) { |
| 3794 | handle_free(tid, p); |
| 3795 | } |
| 3796 | static void hg_cli____builtin_vec_delete ( ThreadId tid, void* p ) { |
| 3797 | handle_free(tid, p); |
| 3798 | } |
| 3799 | |
| 3800 | |
| 3801 | static void* hg_cli__realloc ( ThreadId tid, void* payloadV, SizeT new_size ) |
| 3802 | { |
| 3803 | MallocMeta *md, *md_new, *md_tmp; |
| 3804 | SizeT i; |
| 3805 | |
| 3806 | Addr payload = (Addr)payloadV; |
| 3807 | |
| 3808 | if (((SSizeT)new_size) < 0) return NULL; |
| 3809 | |
| 3810 | md = (MallocMeta*) VG_(HT_lookup)( hg_mallocmeta_table, (UWord)payload ); |
| 3811 | if (!md) |
| 3812 | return NULL; /* apparently realloc-ing a bogus address. Oh well. */ |
| 3813 | |
| 3814 | tl_assert(md->payload == payload); |
| 3815 | |
| 3816 | if (md->szB == new_size) { |
| 3817 | /* size unchanged */ |
| 3818 | md->where = VG_(record_ExeContext)(tid, 0); |
| 3819 | return payloadV; |
| 3820 | } |
| 3821 | |
| 3822 | if (md->szB > new_size) { |
| 3823 | /* new size is smaller */ |
| 3824 | md->szB = new_size; |
| 3825 | md->where = VG_(record_ExeContext)(tid, 0); |
| 3826 | evh__die_mem_heap( md->payload + new_size, md->szB - new_size ); |
| 3827 | return payloadV; |
| 3828 | } |
| 3829 | |
| 3830 | /* else */ { |
| 3831 | /* new size is bigger */ |
| 3832 | Addr p_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size); |
| 3833 | |
| 3834 | /* First half kept and copied, second half new */ |
| 3835 | // FIXME: shouldn't we use a copier which implements the |
| 3836 | // memory state machine? |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 3837 | evh__copy_mem( payload, p_new, md->szB ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3838 | evh__new_mem_heap ( p_new + md->szB, new_size - md->szB, |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 3839 | /*inited*/False ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3840 | /* FIXME: can anything funny happen here? specifically, if the |
| 3841 | old range contained a lock, then die_mem_heap will complain. |
| 3842 | Is that the correct behaviour? Not sure. */ |
| 3843 | evh__die_mem_heap( payload, md->szB ); |
| 3844 | |
| 3845 | /* Copy from old to new */ |
| 3846 | for (i = 0; i < md->szB; i++) |
| 3847 | ((UChar*)p_new)[i] = ((UChar*)payload)[i]; |
| 3848 | |
| 3849 | /* Because the metadata hash table is index by payload address, |
| 3850 | we have to get rid of the old hash table entry and make a new |
| 3851 | one. We can't just modify the existing metadata in place, |
| 3852 | because then it would (almost certainly) be in the wrong hash |
| 3853 | chain. */ |
| 3854 | md_new = new_MallocMeta(); |
| 3855 | *md_new = *md; |
| 3856 | |
| 3857 | md_tmp = VG_(HT_remove)( hg_mallocmeta_table, payload ); |
| 3858 | tl_assert(md_tmp); |
| 3859 | tl_assert(md_tmp == md); |
| 3860 | |
| 3861 | VG_(cli_free)((void*)md->payload); |
| 3862 | delete_MallocMeta(md); |
| 3863 | |
| 3864 | /* Update fields */ |
| 3865 | md_new->where = VG_(record_ExeContext)( tid, 0 ); |
| 3866 | md_new->szB = new_size; |
| 3867 | md_new->payload = p_new; |
| 3868 | md_new->thr = map_threads_lookup( tid ); |
| 3869 | |
| 3870 | /* and add */ |
| 3871 | VG_(HT_add_node)( hg_mallocmeta_table, (VgHashNode*)md_new ); |
| 3872 | |
| 3873 | return (void*)p_new; |
| 3874 | } |
| 3875 | } |
| 3876 | |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 3877 | static SizeT hg_cli_malloc_usable_size ( ThreadId tid, void* p ) |
| 3878 | { |
| 3879 | MallocMeta *md = VG_(HT_lookup)( hg_mallocmeta_table, (UWord)p ); |
| 3880 | |
| 3881 | // There may be slop, but pretend there isn't because only the asked-for |
| 3882 | // area will have been shadowed properly. |
| 3883 | return ( md ? md->szB : 0 ); |
| 3884 | } |
| 3885 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3886 | |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 3887 | /* For error creation: map 'data_addr' to a malloc'd chunk, if any. |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 3888 | Slow linear search. With a bit of hash table help if 'data_addr' |
| 3889 | is either the start of a block or up to 15 word-sized steps along |
| 3890 | from the start of a block. */ |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 3891 | |
| 3892 | static inline Bool addr_is_in_MM_Chunk( MallocMeta* mm, Addr a ) |
| 3893 | { |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 3894 | /* Accept 'a' as within 'mm' if 'mm's size is zero and 'a' points |
| 3895 | right at it. */ |
| 3896 | if (UNLIKELY(mm->szB == 0 && a == mm->payload)) |
| 3897 | return True; |
| 3898 | /* else normal interval rules apply */ |
| 3899 | if (LIKELY(a < mm->payload)) return False; |
| 3900 | if (LIKELY(a >= mm->payload + mm->szB)) return False; |
| 3901 | return True; |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 3904 | Bool HG_(mm_find_containing_block)( /*OUT*/ExeContext** where, |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 3905 | /*OUT*/Addr* payload, |
| 3906 | /*OUT*/SizeT* szB, |
| 3907 | Addr data_addr ) |
| 3908 | { |
| 3909 | MallocMeta* mm; |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 3910 | Int i; |
| 3911 | const Int n_fast_check_words = 16; |
| 3912 | |
| 3913 | /* First, do a few fast searches on the basis that data_addr might |
| 3914 | be exactly the start of a block or up to 15 words inside. This |
| 3915 | can happen commonly via the creq |
| 3916 | _VG_USERREQ__HG_CLEAN_MEMORY_HEAPBLOCK. */ |
| 3917 | for (i = 0; i < n_fast_check_words; i++) { |
| 3918 | mm = VG_(HT_lookup)( hg_mallocmeta_table, |
| 3919 | data_addr - (UWord)(UInt)i * sizeof(UWord) ); |
| 3920 | if (UNLIKELY(mm && addr_is_in_MM_Chunk(mm, data_addr))) |
| 3921 | goto found; |
| 3922 | } |
| 3923 | |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 3924 | /* Well, this totally sucks. But without using an interval tree or |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 3925 | some such, it's hard to see how to do better. We have to check |
| 3926 | every block in the entire table. */ |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 3927 | VG_(HT_ResetIter)(hg_mallocmeta_table); |
| 3928 | while ( (mm = VG_(HT_Next)(hg_mallocmeta_table)) ) { |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 3929 | if (UNLIKELY(addr_is_in_MM_Chunk(mm, data_addr))) |
| 3930 | goto found; |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 3931 | } |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 3932 | |
| 3933 | /* Not found. Bah. */ |
| 3934 | return False; |
| 3935 | /*NOTREACHED*/ |
| 3936 | |
| 3937 | found: |
| 3938 | tl_assert(mm); |
| 3939 | tl_assert(addr_is_in_MM_Chunk(mm, data_addr)); |
| 3940 | if (where) *where = mm->where; |
| 3941 | if (payload) *payload = mm->payload; |
| 3942 | if (szB) *szB = mm->szB; |
| 3943 | return True; |
sewardj | 095d61e | 2010-03-11 13:43:18 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3947 | /*--------------------------------------------------------------*/ |
| 3948 | /*--- Instrumentation ---*/ |
| 3949 | /*--------------------------------------------------------------*/ |
| 3950 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 3951 | #define binop(_op, _arg1, _arg2) IRExpr_Binop((_op),(_arg1),(_arg2)) |
| 3952 | #define mkexpr(_tmp) IRExpr_RdTmp((_tmp)) |
| 3953 | #define mkU32(_n) IRExpr_Const(IRConst_U32(_n)) |
| 3954 | #define mkU64(_n) IRExpr_Const(IRConst_U64(_n)) |
| 3955 | #define assign(_t, _e) IRStmt_WrTmp((_t), (_e)) |
| 3956 | |
| 3957 | static void instrument_mem_access ( IRSB* sbOut, |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3958 | IRExpr* addr, |
| 3959 | Int szB, |
| 3960 | Bool isStore, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 3961 | Int hWordTy_szB, |
| 3962 | Int goff_sp ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3963 | { |
| 3964 | IRType tyAddr = Ity_INVALID; |
| 3965 | HChar* hName = NULL; |
| 3966 | void* hAddr = NULL; |
| 3967 | Int regparms = 0; |
| 3968 | IRExpr** argv = NULL; |
| 3969 | IRDirty* di = NULL; |
| 3970 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 3971 | // THRESH is the size of the window above SP (well, |
| 3972 | // mostly above) that we assume implies a stack reference. |
| 3973 | const Int THRESH = 4096 * 4; // somewhat arbitrary |
| 3974 | const Int rz_szB = VG_STACK_REDZONE_SZB; |
| 3975 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3976 | tl_assert(isIRAtom(addr)); |
| 3977 | tl_assert(hWordTy_szB == 4 || hWordTy_szB == 8); |
| 3978 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 3979 | tyAddr = typeOfIRExpr( sbOut->tyenv, addr ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3980 | tl_assert(tyAddr == Ity_I32 || tyAddr == Ity_I64); |
| 3981 | |
| 3982 | /* So the effective address is in 'addr' now. */ |
| 3983 | regparms = 1; // unless stated otherwise |
| 3984 | if (isStore) { |
| 3985 | switch (szB) { |
| 3986 | case 1: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 3987 | hName = "evh__mem_help_cwrite_1"; |
| 3988 | hAddr = &evh__mem_help_cwrite_1; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3989 | argv = mkIRExprVec_1( addr ); |
| 3990 | break; |
| 3991 | case 2: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 3992 | hName = "evh__mem_help_cwrite_2"; |
| 3993 | hAddr = &evh__mem_help_cwrite_2; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3994 | argv = mkIRExprVec_1( addr ); |
| 3995 | break; |
| 3996 | case 4: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 3997 | hName = "evh__mem_help_cwrite_4"; |
| 3998 | hAddr = &evh__mem_help_cwrite_4; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 3999 | argv = mkIRExprVec_1( addr ); |
| 4000 | break; |
| 4001 | case 8: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4002 | hName = "evh__mem_help_cwrite_8"; |
| 4003 | hAddr = &evh__mem_help_cwrite_8; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4004 | argv = mkIRExprVec_1( addr ); |
| 4005 | break; |
| 4006 | default: |
| 4007 | tl_assert(szB > 8 && szB <= 512); /* stay sane */ |
| 4008 | regparms = 2; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4009 | hName = "evh__mem_help_cwrite_N"; |
| 4010 | hAddr = &evh__mem_help_cwrite_N; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4011 | argv = mkIRExprVec_2( addr, mkIRExpr_HWord( szB )); |
| 4012 | break; |
| 4013 | } |
| 4014 | } else { |
| 4015 | switch (szB) { |
| 4016 | case 1: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4017 | hName = "evh__mem_help_cread_1"; |
| 4018 | hAddr = &evh__mem_help_cread_1; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4019 | argv = mkIRExprVec_1( addr ); |
| 4020 | break; |
| 4021 | case 2: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4022 | hName = "evh__mem_help_cread_2"; |
| 4023 | hAddr = &evh__mem_help_cread_2; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4024 | argv = mkIRExprVec_1( addr ); |
| 4025 | break; |
| 4026 | case 4: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4027 | hName = "evh__mem_help_cread_4"; |
| 4028 | hAddr = &evh__mem_help_cread_4; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4029 | argv = mkIRExprVec_1( addr ); |
| 4030 | break; |
| 4031 | case 8: |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4032 | hName = "evh__mem_help_cread_8"; |
| 4033 | hAddr = &evh__mem_help_cread_8; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4034 | argv = mkIRExprVec_1( addr ); |
| 4035 | break; |
| 4036 | default: |
| 4037 | tl_assert(szB > 8 && szB <= 512); /* stay sane */ |
| 4038 | regparms = 2; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4039 | hName = "evh__mem_help_cread_N"; |
| 4040 | hAddr = &evh__mem_help_cread_N; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4041 | argv = mkIRExprVec_2( addr, mkIRExpr_HWord( szB )); |
| 4042 | break; |
| 4043 | } |
| 4044 | } |
| 4045 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4046 | /* Create the helper. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4047 | tl_assert(hName); |
| 4048 | tl_assert(hAddr); |
| 4049 | tl_assert(argv); |
| 4050 | di = unsafeIRDirty_0_N( regparms, |
| 4051 | hName, VG_(fnptr_to_fnentry)( hAddr ), |
| 4052 | argv ); |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4053 | |
| 4054 | if (! HG_(clo_check_stack_refs)) { |
| 4055 | /* We're ignoring memory references which are (obviously) to the |
| 4056 | stack. In fact just skip stack refs that are within 4 pages |
| 4057 | of SP (SP - the redzone, really), as that's simple, easy, and |
| 4058 | filters out most stack references. */ |
| 4059 | /* Generate the guard condition: "(addr - (SP - RZ)) >u N", for |
| 4060 | some arbitrary N. If that is true then addr is outside the |
| 4061 | range (SP - RZ .. SP + N - RZ). If N is smallish (a few |
| 4062 | pages) then we can say addr is within a few pages of SP and |
| 4063 | so can't possibly be a heap access, and so can be skipped. |
| 4064 | |
| 4065 | Note that the condition simplifies to |
| 4066 | (addr - SP + RZ) >u N |
| 4067 | which generates better code in x86/amd64 backends, but it does |
| 4068 | not unfortunately simplify to |
| 4069 | (addr - SP) >u (N - RZ) |
| 4070 | (would be beneficial because N - RZ is a constant) because |
| 4071 | wraparound arithmetic messes up the comparison. eg. |
| 4072 | 20 >u 10 == True, |
| 4073 | but (20 - 15) >u (10 - 15) == 5 >u (MAXINT-5) == False. |
| 4074 | */ |
| 4075 | IRTemp sp = newIRTemp(sbOut->tyenv, tyAddr); |
| 4076 | addStmtToIRSB( sbOut, assign(sp, IRExpr_Get(goff_sp, tyAddr))); |
| 4077 | |
| 4078 | /* "addr - SP" */ |
| 4079 | IRTemp addr_minus_sp = newIRTemp(sbOut->tyenv, tyAddr); |
| 4080 | addStmtToIRSB( |
| 4081 | sbOut, |
| 4082 | assign(addr_minus_sp, |
| 4083 | tyAddr == Ity_I32 |
| 4084 | ? binop(Iop_Sub32, addr, mkexpr(sp)) |
| 4085 | : binop(Iop_Sub64, addr, mkexpr(sp))) |
| 4086 | ); |
| 4087 | |
| 4088 | /* "addr - SP + RZ" */ |
| 4089 | IRTemp diff = newIRTemp(sbOut->tyenv, tyAddr); |
| 4090 | addStmtToIRSB( |
| 4091 | sbOut, |
| 4092 | assign(diff, |
| 4093 | tyAddr == Ity_I32 |
| 4094 | ? binop(Iop_Add32, mkexpr(addr_minus_sp), mkU32(rz_szB)) |
| 4095 | : binop(Iop_Add64, mkexpr(addr_minus_sp), mkU64(rz_szB))) |
| 4096 | ); |
| 4097 | |
| 4098 | IRTemp guard = newIRTemp(sbOut->tyenv, Ity_I1); |
| 4099 | addStmtToIRSB( |
| 4100 | sbOut, |
| 4101 | assign(guard, |
| 4102 | tyAddr == Ity_I32 |
| 4103 | ? binop(Iop_CmpLT32U, mkU32(THRESH), mkexpr(diff)) |
| 4104 | : binop(Iop_CmpLT64U, mkU64(THRESH), mkexpr(diff))) |
| 4105 | ); |
| 4106 | di->guard = mkexpr(guard); |
| 4107 | } |
| 4108 | |
| 4109 | /* Add the helper. */ |
| 4110 | addStmtToIRSB( sbOut, IRStmt_Dirty(di) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4114 | /* Figure out if GA is a guest code address in the dynamic linker, and |
| 4115 | if so return True. Otherwise (and in case of any doubt) return |
| 4116 | False. (sidedly safe w/ False as the safe value) */ |
| 4117 | static Bool is_in_dynamic_linker_shared_object( Addr64 ga ) |
| 4118 | { |
| 4119 | DebugInfo* dinfo; |
| 4120 | const UChar* soname; |
| 4121 | if (0) return False; |
| 4122 | |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 4123 | dinfo = VG_(find_DebugInfo)( (Addr)ga ); |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4124 | if (!dinfo) return False; |
| 4125 | |
sewardj | e3f1e59 | 2009-07-31 09:41:29 +0000 | [diff] [blame] | 4126 | soname = VG_(DebugInfo_get_soname)(dinfo); |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4127 | tl_assert(soname); |
| 4128 | if (0) VG_(printf)("%s\n", soname); |
| 4129 | |
| 4130 | # if defined(VGO_linux) |
sewardj | 651cfa4 | 2010-01-11 13:02:19 +0000 | [diff] [blame] | 4131 | if (VG_STREQ(soname, VG_U_LD_LINUX_SO_3)) return True; |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4132 | if (VG_STREQ(soname, VG_U_LD_LINUX_SO_2)) return True; |
| 4133 | if (VG_STREQ(soname, VG_U_LD_LINUX_X86_64_SO_2)) return True; |
| 4134 | if (VG_STREQ(soname, VG_U_LD64_SO_1)) return True; |
| 4135 | if (VG_STREQ(soname, VG_U_LD_SO_1)) return True; |
| 4136 | # elif defined(VGO_darwin) |
| 4137 | if (VG_STREQ(soname, VG_U_DYLD)) return True; |
| 4138 | # else |
| 4139 | # error "Unsupported OS" |
| 4140 | # endif |
| 4141 | return False; |
| 4142 | } |
| 4143 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4144 | static |
| 4145 | IRSB* hg_instrument ( VgCallbackClosure* closure, |
| 4146 | IRSB* bbIn, |
| 4147 | VexGuestLayout* layout, |
| 4148 | VexGuestExtents* vge, |
| 4149 | IRType gWordTy, IRType hWordTy ) |
| 4150 | { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4151 | Int i; |
| 4152 | IRSB* bbOut; |
| 4153 | Addr64 cia; /* address of current insn */ |
| 4154 | IRStmt* st; |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4155 | Bool inLDSO = False; |
| 4156 | Addr64 inLDSOmask4K = 1; /* mismatches on first check */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4157 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4158 | const Int goff_sp = layout->offset_SP; |
| 4159 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4160 | if (gWordTy != hWordTy) { |
| 4161 | /* We don't currently support this case. */ |
| 4162 | VG_(tool_panic)("host/guest word size mismatch"); |
| 4163 | } |
| 4164 | |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4165 | if (VKI_PAGE_SIZE < 4096 || VG_(log2)(VKI_PAGE_SIZE) == -1) { |
| 4166 | VG_(tool_panic)("implausible or too-small VKI_PAGE_SIZE"); |
| 4167 | } |
| 4168 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4169 | /* Set up BB */ |
| 4170 | bbOut = emptyIRSB(); |
| 4171 | bbOut->tyenv = deepCopyIRTypeEnv(bbIn->tyenv); |
| 4172 | bbOut->next = deepCopyIRExpr(bbIn->next); |
| 4173 | bbOut->jumpkind = bbIn->jumpkind; |
| 4174 | |
| 4175 | // Copy verbatim any IR preamble preceding the first IMark |
| 4176 | i = 0; |
| 4177 | while (i < bbIn->stmts_used && bbIn->stmts[i]->tag != Ist_IMark) { |
| 4178 | addStmtToIRSB( bbOut, bbIn->stmts[i] ); |
| 4179 | i++; |
| 4180 | } |
| 4181 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4182 | // Get the first statement, and initial cia from it |
| 4183 | tl_assert(bbIn->stmts_used > 0); |
| 4184 | tl_assert(i < bbIn->stmts_used); |
| 4185 | st = bbIn->stmts[i]; |
| 4186 | tl_assert(Ist_IMark == st->tag); |
| 4187 | cia = st->Ist.IMark.addr; |
| 4188 | st = NULL; |
| 4189 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4190 | for (/*use current i*/; i < bbIn->stmts_used; i++) { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4191 | st = bbIn->stmts[i]; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4192 | tl_assert(st); |
| 4193 | tl_assert(isFlatIRStmt(st)); |
| 4194 | switch (st->tag) { |
| 4195 | case Ist_NoOp: |
| 4196 | case Ist_AbiHint: |
| 4197 | case Ist_Put: |
| 4198 | case Ist_PutI: |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4199 | case Ist_Exit: |
| 4200 | /* None of these can contain any memory references. */ |
| 4201 | break; |
| 4202 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4203 | case Ist_IMark: |
| 4204 | /* no mem refs, but note the insn address. */ |
| 4205 | cia = st->Ist.IMark.addr; |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4206 | /* Don't instrument the dynamic linker. It generates a |
| 4207 | lot of races which we just expensively suppress, so |
| 4208 | it's pointless. |
| 4209 | |
| 4210 | Avoid flooding is_in_dynamic_linker_shared_object with |
| 4211 | requests by only checking at transitions between 4K |
| 4212 | pages. */ |
| 4213 | if ((cia & ~(Addr64)0xFFF) != inLDSOmask4K) { |
| 4214 | if (0) VG_(printf)("NEW %#lx\n", (Addr)cia); |
| 4215 | inLDSOmask4K = cia & ~(Addr64)0xFFF; |
| 4216 | inLDSO = is_in_dynamic_linker_shared_object(cia); |
| 4217 | } else { |
| 4218 | if (0) VG_(printf)("old %#lx\n", (Addr)cia); |
| 4219 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4220 | break; |
| 4221 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4222 | case Ist_MBE: |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4223 | switch (st->Ist.MBE.event) { |
| 4224 | case Imbe_Fence: |
| 4225 | break; /* not interesting */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4226 | default: |
| 4227 | goto unhandled; |
| 4228 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4229 | break; |
| 4230 | |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4231 | case Ist_CAS: { |
| 4232 | /* Atomic read-modify-write cycle. Just pretend it's a |
| 4233 | read. */ |
| 4234 | IRCAS* cas = st->Ist.CAS.details; |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4235 | Bool isDCAS = cas->oldHi != IRTemp_INVALID; |
| 4236 | if (isDCAS) { |
| 4237 | tl_assert(cas->expdHi); |
| 4238 | tl_assert(cas->dataHi); |
| 4239 | } else { |
| 4240 | tl_assert(!cas->expdHi); |
| 4241 | tl_assert(!cas->dataHi); |
| 4242 | } |
| 4243 | /* Just be boring about it. */ |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4244 | if (!inLDSO) { |
| 4245 | instrument_mem_access( |
| 4246 | bbOut, |
| 4247 | cas->addr, |
| 4248 | (isDCAS ? 2 : 1) |
| 4249 | * sizeofIRType(typeOfIRExpr(bbIn->tyenv, cas->dataLo)), |
| 4250 | False/*!isStore*/, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4251 | sizeofIRType(hWordTy), goff_sp |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4252 | ); |
| 4253 | } |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4254 | break; |
| 4255 | } |
| 4256 | |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 4257 | case Ist_LLSC: { |
| 4258 | /* We pretend store-conditionals don't exist, viz, ignore |
| 4259 | them. Whereas load-linked's are treated the same as |
| 4260 | normal loads. */ |
| 4261 | IRType dataTy; |
| 4262 | if (st->Ist.LLSC.storedata == NULL) { |
| 4263 | /* LL */ |
| 4264 | dataTy = typeOfIRTemp(bbIn->tyenv, st->Ist.LLSC.result); |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4265 | if (!inLDSO) { |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 4266 | instrument_mem_access( |
| 4267 | bbOut, |
| 4268 | st->Ist.LLSC.addr, |
| 4269 | sizeofIRType(dataTy), |
| 4270 | False/*!isStore*/, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4271 | sizeofIRType(hWordTy), goff_sp |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4272 | ); |
| 4273 | } |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 4274 | } else { |
| 4275 | /* SC */ |
| 4276 | /*ignore */ |
| 4277 | } |
| 4278 | break; |
| 4279 | } |
| 4280 | |
| 4281 | case Ist_Store: |
| 4282 | /* It seems we pretend that store-conditionals don't |
| 4283 | exist, viz, just ignore them ... */ |
| 4284 | if (!inLDSO) { |
| 4285 | instrument_mem_access( |
| 4286 | bbOut, |
| 4287 | st->Ist.Store.addr, |
| 4288 | sizeofIRType(typeOfIRExpr(bbIn->tyenv, st->Ist.Store.data)), |
| 4289 | True/*isStore*/, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4290 | sizeofIRType(hWordTy), goff_sp |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 4291 | ); |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4292 | } |
njn | b83caf2 | 2009-05-25 01:47:56 +0000 | [diff] [blame] | 4293 | break; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4294 | |
| 4295 | case Ist_WrTmp: { |
sewardj | 1c0ce7a | 2009-07-01 08:10:49 +0000 | [diff] [blame] | 4296 | /* ... whereas here we don't care whether a load is a |
| 4297 | vanilla one or a load-linked. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4298 | IRExpr* data = st->Ist.WrTmp.data; |
| 4299 | if (data->tag == Iex_Load) { |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4300 | if (!inLDSO) { |
| 4301 | instrument_mem_access( |
| 4302 | bbOut, |
| 4303 | data->Iex.Load.addr, |
| 4304 | sizeofIRType(data->Iex.Load.ty), |
| 4305 | False/*!isStore*/, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4306 | sizeofIRType(hWordTy), goff_sp |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4307 | ); |
| 4308 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4309 | } |
| 4310 | break; |
| 4311 | } |
| 4312 | |
| 4313 | case Ist_Dirty: { |
| 4314 | Int dataSize; |
| 4315 | IRDirty* d = st->Ist.Dirty.details; |
| 4316 | if (d->mFx != Ifx_None) { |
| 4317 | /* This dirty helper accesses memory. Collect the |
| 4318 | details. */ |
| 4319 | tl_assert(d->mAddr != NULL); |
| 4320 | tl_assert(d->mSize != 0); |
| 4321 | dataSize = d->mSize; |
| 4322 | if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify) { |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4323 | if (!inLDSO) { |
| 4324 | instrument_mem_access( |
| 4325 | bbOut, d->mAddr, dataSize, False/*!isStore*/, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4326 | sizeofIRType(hWordTy), goff_sp |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4327 | ); |
| 4328 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4329 | } |
| 4330 | if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify) { |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4331 | if (!inLDSO) { |
| 4332 | instrument_mem_access( |
| 4333 | bbOut, d->mAddr, dataSize, True/*isStore*/, |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4334 | sizeofIRType(hWordTy), goff_sp |
sewardj | a0eee32 | 2009-07-31 08:46:35 +0000 | [diff] [blame] | 4335 | ); |
| 4336 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4337 | } |
| 4338 | } else { |
| 4339 | tl_assert(d->mAddr == NULL); |
| 4340 | tl_assert(d->mSize == 0); |
| 4341 | } |
| 4342 | break; |
| 4343 | } |
| 4344 | |
| 4345 | default: |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4346 | unhandled: |
| 4347 | ppIRStmt(st); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4348 | tl_assert(0); |
| 4349 | |
| 4350 | } /* switch (st->tag) */ |
| 4351 | |
| 4352 | addStmtToIRSB( bbOut, st ); |
| 4353 | } /* iterate over bbIn->stmts */ |
| 4354 | |
| 4355 | return bbOut; |
| 4356 | } |
| 4357 | |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4358 | #undef binop |
| 4359 | #undef mkexpr |
| 4360 | #undef mkU32 |
| 4361 | #undef mkU64 |
| 4362 | #undef assign |
| 4363 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4364 | |
| 4365 | /*----------------------------------------------------------------*/ |
| 4366 | /*--- Client requests ---*/ |
| 4367 | /*----------------------------------------------------------------*/ |
| 4368 | |
| 4369 | /* Sheesh. Yet another goddam finite map. */ |
| 4370 | static WordFM* map_pthread_t_to_Thread = NULL; /* pthread_t -> Thread* */ |
| 4371 | |
| 4372 | static void map_pthread_t_to_Thread_INIT ( void ) { |
| 4373 | if (UNLIKELY(map_pthread_t_to_Thread == NULL)) { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4374 | map_pthread_t_to_Thread = VG_(newFM)( HG_(zalloc), "hg.mpttT.1", |
| 4375 | HG_(free), NULL ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4376 | tl_assert(map_pthread_t_to_Thread != NULL); |
| 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | |
| 4381 | static |
| 4382 | Bool hg_handle_client_request ( ThreadId tid, UWord* args, UWord* ret) |
| 4383 | { |
| 4384 | if (!VG_IS_TOOL_USERREQ('H','G',args[0])) |
| 4385 | return False; |
| 4386 | |
| 4387 | /* Anything that gets past the above check is one of ours, so we |
| 4388 | should be able to handle it. */ |
| 4389 | |
| 4390 | /* default, meaningless return value, unless otherwise set */ |
| 4391 | *ret = 0; |
| 4392 | |
| 4393 | switch (args[0]) { |
| 4394 | |
| 4395 | /* --- --- User-visible client requests --- --- */ |
| 4396 | |
| 4397 | case VG_USERREQ__HG_CLEAN_MEMORY: |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4398 | if (0) VG_(printf)("VG_USERREQ__HG_CLEAN_MEMORY(%#lx,%ld)\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4399 | args[1], args[2]); |
| 4400 | /* Call die_mem to (expensively) tidy up properly, if there |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4401 | are any held locks etc in the area. Calling evh__die_mem |
| 4402 | and then evh__new_mem is a bit inefficient; probably just |
| 4403 | the latter would do. */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4404 | if (args[2] > 0) { /* length */ |
| 4405 | evh__die_mem(args[1], args[2]); |
| 4406 | /* and then set it to New */ |
| 4407 | evh__new_mem(args[1], args[2]); |
| 4408 | } |
| 4409 | break; |
| 4410 | |
sewardj | c8028ad | 2010-05-05 09:34:42 +0000 | [diff] [blame] | 4411 | case _VG_USERREQ__HG_CLEAN_MEMORY_HEAPBLOCK: { |
| 4412 | Addr payload = 0; |
| 4413 | SizeT pszB = 0; |
| 4414 | if (0) VG_(printf)("VG_USERREQ__HG_CLEAN_MEMORY_HEAPBLOCK(%#lx)\n", |
| 4415 | args[1]); |
| 4416 | if (HG_(mm_find_containing_block)(NULL, &payload, &pszB, args[1])) { |
| 4417 | if (pszB > 0) { |
| 4418 | evh__die_mem(payload, pszB); |
| 4419 | evh__new_mem(payload, pszB); |
| 4420 | } |
| 4421 | *ret = pszB; |
| 4422 | } else { |
| 4423 | *ret = (UWord)-1; |
| 4424 | } |
| 4425 | break; |
| 4426 | } |
| 4427 | |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 4428 | case _VG_USERREQ__HG_ARANGE_MAKE_UNTRACKED: |
| 4429 | if (0) VG_(printf)("HG_ARANGE_MAKE_UNTRACKED(%#lx,%ld)\n", |
| 4430 | args[1], args[2]); |
| 4431 | if (args[2] > 0) { /* length */ |
| 4432 | evh__untrack_mem(args[1], args[2]); |
| 4433 | } |
| 4434 | break; |
| 4435 | |
| 4436 | case _VG_USERREQ__HG_ARANGE_MAKE_TRACKED: |
| 4437 | if (0) VG_(printf)("HG_ARANGE_MAKE_TRACKED(%#lx,%ld)\n", |
| 4438 | args[1], args[2]); |
| 4439 | if (args[2] > 0) { /* length */ |
| 4440 | evh__new_mem(args[1], args[2]); |
| 4441 | } |
| 4442 | break; |
| 4443 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4444 | /* --- --- Client requests for Helgrind's use only --- --- */ |
| 4445 | |
| 4446 | /* Some thread is telling us its pthread_t value. Record the |
| 4447 | binding between that and the associated Thread*, so we can |
| 4448 | later find the Thread* again when notified of a join by the |
| 4449 | thread. */ |
| 4450 | case _VG_USERREQ__HG_SET_MY_PTHREAD_T: { |
| 4451 | Thread* my_thr = NULL; |
| 4452 | if (0) |
| 4453 | VG_(printf)("SET_MY_PTHREAD_T (tid %d): pthread_t = %p\n", (Int)tid, |
| 4454 | (void*)args[1]); |
| 4455 | map_pthread_t_to_Thread_INIT(); |
| 4456 | my_thr = map_threads_maybe_lookup( tid ); |
| 4457 | /* This assertion should hold because the map_threads (tid to |
| 4458 | Thread*) binding should have been made at the point of |
| 4459 | low-level creation of this thread, which should have |
| 4460 | happened prior to us getting this client request for it. |
| 4461 | That's because this client request is sent from |
| 4462 | client-world from the 'thread_wrapper' function, which |
| 4463 | only runs once the thread has been low-level created. */ |
| 4464 | tl_assert(my_thr != NULL); |
| 4465 | /* So now we know that (pthread_t)args[1] is associated with |
| 4466 | (Thread*)my_thr. Note that down. */ |
| 4467 | if (0) |
| 4468 | VG_(printf)("XXXX: bind pthread_t %p to Thread* %p\n", |
| 4469 | (void*)args[1], (void*)my_thr ); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 4470 | VG_(addToFM)( map_pthread_t_to_Thread, (Word)args[1], (Word)my_thr ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4471 | break; |
| 4472 | } |
| 4473 | |
| 4474 | case _VG_USERREQ__HG_PTH_API_ERROR: { |
| 4475 | Thread* my_thr = NULL; |
| 4476 | map_pthread_t_to_Thread_INIT(); |
| 4477 | my_thr = map_threads_maybe_lookup( tid ); |
| 4478 | tl_assert(my_thr); /* See justification above in SET_MY_PTHREAD_T */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4479 | HG_(record_error_PthAPIerror)( |
| 4480 | my_thr, (HChar*)args[1], (Word)args[2], (HChar*)args[3] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4481 | break; |
| 4482 | } |
| 4483 | |
| 4484 | /* This thread (tid) has completed a join with the quitting |
| 4485 | thread whose pthread_t is in args[1]. */ |
| 4486 | case _VG_USERREQ__HG_PTHREAD_JOIN_POST: { |
| 4487 | Thread* thr_q = NULL; /* quitter Thread* */ |
| 4488 | Bool found = False; |
| 4489 | if (0) |
| 4490 | VG_(printf)("NOTIFY_JOIN_COMPLETE (tid %d): quitter = %p\n", (Int)tid, |
| 4491 | (void*)args[1]); |
| 4492 | map_pthread_t_to_Thread_INIT(); |
sewardj | 896f6f9 | 2008-08-19 08:38:52 +0000 | [diff] [blame] | 4493 | found = VG_(lookupFM)( map_pthread_t_to_Thread, |
sewardj | b5f2964 | 2007-11-16 12:02:43 +0000 | [diff] [blame] | 4494 | NULL, (Word*)&thr_q, (Word)args[1] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4495 | /* Can this fail? It would mean that our pthread_join |
| 4496 | wrapper observed a successful join on args[1] yet that |
| 4497 | thread never existed (or at least, it never lodged an |
| 4498 | entry in the mapping (via SET_MY_PTHREAD_T)). Which |
| 4499 | sounds like a bug in the threads library. */ |
| 4500 | // FIXME: get rid of this assertion; handle properly |
| 4501 | tl_assert(found); |
| 4502 | if (found) { |
| 4503 | if (0) |
| 4504 | VG_(printf)(".................... quitter Thread* = %p\n", |
| 4505 | thr_q); |
| 4506 | evh__HG_PTHREAD_JOIN_POST( tid, thr_q ); |
| 4507 | } |
| 4508 | break; |
| 4509 | } |
| 4510 | |
| 4511 | /* EXPOSITION only: by intercepting lock init events we can show |
| 4512 | the user where the lock was initialised, rather than only |
| 4513 | being able to show where it was first locked. Intercepting |
| 4514 | lock initialisations is not necessary for the basic operation |
| 4515 | of the race checker. */ |
| 4516 | case _VG_USERREQ__HG_PTHREAD_MUTEX_INIT_POST: |
| 4517 | evh__HG_PTHREAD_MUTEX_INIT_POST( tid, (void*)args[1], args[2] ); |
| 4518 | break; |
| 4519 | |
| 4520 | case _VG_USERREQ__HG_PTHREAD_MUTEX_DESTROY_PRE: |
| 4521 | evh__HG_PTHREAD_MUTEX_DESTROY_PRE( tid, (void*)args[1] ); |
| 4522 | break; |
| 4523 | |
| 4524 | case _VG_USERREQ__HG_PTHREAD_MUTEX_UNLOCK_PRE: // pth_mx_t* |
| 4525 | evh__HG_PTHREAD_MUTEX_UNLOCK_PRE( tid, (void*)args[1] ); |
| 4526 | break; |
| 4527 | |
| 4528 | case _VG_USERREQ__HG_PTHREAD_MUTEX_UNLOCK_POST: // pth_mx_t* |
| 4529 | evh__HG_PTHREAD_MUTEX_UNLOCK_POST( tid, (void*)args[1] ); |
| 4530 | break; |
| 4531 | |
| 4532 | case _VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_PRE: // pth_mx_t*, Word |
| 4533 | evh__HG_PTHREAD_MUTEX_LOCK_PRE( tid, (void*)args[1], args[2] ); |
| 4534 | break; |
| 4535 | |
| 4536 | case _VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_POST: // pth_mx_t* |
| 4537 | evh__HG_PTHREAD_MUTEX_LOCK_POST( tid, (void*)args[1] ); |
| 4538 | break; |
| 4539 | |
| 4540 | /* This thread is about to do pthread_cond_signal on the |
| 4541 | pthread_cond_t* in arg[1]. Ditto pthread_cond_broadcast. */ |
| 4542 | case _VG_USERREQ__HG_PTHREAD_COND_SIGNAL_PRE: |
| 4543 | case _VG_USERREQ__HG_PTHREAD_COND_BROADCAST_PRE: |
| 4544 | evh__HG_PTHREAD_COND_SIGNAL_PRE( tid, (void*)args[1] ); |
| 4545 | break; |
| 4546 | |
| 4547 | /* Entry into pthread_cond_wait, cond=arg[1], mutex=arg[2]. |
| 4548 | Returns a flag indicating whether or not the mutex is believed to be |
| 4549 | valid for this operation. */ |
| 4550 | case _VG_USERREQ__HG_PTHREAD_COND_WAIT_PRE: { |
| 4551 | Bool mutex_is_valid |
| 4552 | = evh__HG_PTHREAD_COND_WAIT_PRE( tid, (void*)args[1], |
| 4553 | (void*)args[2] ); |
| 4554 | *ret = mutex_is_valid ? 1 : 0; |
| 4555 | break; |
| 4556 | } |
| 4557 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4558 | /* cond=arg[1] */ |
| 4559 | case _VG_USERREQ__HG_PTHREAD_COND_DESTROY_PRE: |
| 4560 | evh__HG_PTHREAD_COND_DESTROY_PRE( tid, (void*)args[1] ); |
| 4561 | break; |
| 4562 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4563 | /* Thread successfully completed pthread_cond_wait, cond=arg[1], |
| 4564 | mutex=arg[2] */ |
| 4565 | case _VG_USERREQ__HG_PTHREAD_COND_WAIT_POST: |
| 4566 | evh__HG_PTHREAD_COND_WAIT_POST( tid, |
| 4567 | (void*)args[1], (void*)args[2] ); |
| 4568 | break; |
| 4569 | |
| 4570 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_INIT_POST: |
| 4571 | evh__HG_PTHREAD_RWLOCK_INIT_POST( tid, (void*)args[1] ); |
| 4572 | break; |
| 4573 | |
| 4574 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_DESTROY_PRE: |
| 4575 | evh__HG_PTHREAD_RWLOCK_DESTROY_PRE( tid, (void*)args[1] ); |
| 4576 | break; |
| 4577 | |
sewardj | 789c3c5 | 2008-02-25 12:10:07 +0000 | [diff] [blame] | 4578 | /* rwlock=arg[1], isW=arg[2], isTryLock=arg[3] */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4579 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_LOCK_PRE: |
sewardj | 789c3c5 | 2008-02-25 12:10:07 +0000 | [diff] [blame] | 4580 | evh__HG_PTHREAD_RWLOCK_LOCK_PRE( tid, (void*)args[1], |
| 4581 | args[2], args[3] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4582 | break; |
| 4583 | |
| 4584 | /* rwlock=arg[1], isW=arg[2] */ |
| 4585 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_LOCK_POST: |
| 4586 | evh__HG_PTHREAD_RWLOCK_LOCK_POST( tid, (void*)args[1], args[2] ); |
| 4587 | break; |
| 4588 | |
| 4589 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_UNLOCK_PRE: |
| 4590 | evh__HG_PTHREAD_RWLOCK_UNLOCK_PRE( tid, (void*)args[1] ); |
| 4591 | break; |
| 4592 | |
| 4593 | case _VG_USERREQ__HG_PTHREAD_RWLOCK_UNLOCK_POST: |
| 4594 | evh__HG_PTHREAD_RWLOCK_UNLOCK_POST( tid, (void*)args[1] ); |
| 4595 | break; |
| 4596 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 4597 | case _VG_USERREQ__HG_POSIX_SEM_INIT_POST: /* sem_t*, unsigned long */ |
| 4598 | evh__HG_POSIX_SEM_INIT_POST( tid, (void*)args[1], args[2] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4599 | break; |
| 4600 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 4601 | case _VG_USERREQ__HG_POSIX_SEM_DESTROY_PRE: /* sem_t* */ |
| 4602 | evh__HG_POSIX_SEM_DESTROY_PRE( tid, (void*)args[1] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4603 | break; |
| 4604 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 4605 | case _VG_USERREQ__HG_POSIX_SEM_POST_PRE: /* sem_t* */ |
| 4606 | evh__HG_POSIX_SEM_POST_PRE( tid, (void*)args[1] ); |
| 4607 | break; |
| 4608 | |
| 4609 | case _VG_USERREQ__HG_POSIX_SEM_WAIT_POST: /* sem_t* */ |
| 4610 | evh__HG_POSIX_SEM_WAIT_POST( tid, (void*)args[1] ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4611 | break; |
| 4612 | |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 4613 | case _VG_USERREQ__HG_PTHREAD_BARRIER_INIT_PRE: |
sewardj | 406bac8 | 2010-03-03 23:03:40 +0000 | [diff] [blame] | 4614 | /* pth_bar_t*, ulong count, ulong resizable */ |
| 4615 | evh__HG_PTHREAD_BARRIER_INIT_PRE( tid, (void*)args[1], |
| 4616 | args[2], args[3] ); |
| 4617 | break; |
| 4618 | |
| 4619 | case _VG_USERREQ__HG_PTHREAD_BARRIER_RESIZE_PRE: |
| 4620 | /* pth_bar_t*, ulong newcount */ |
| 4621 | evh__HG_PTHREAD_BARRIER_RESIZE_PRE ( tid, (void*)args[1], |
| 4622 | args[2] ); |
sewardj | 9f569b7 | 2008-11-13 13:33:09 +0000 | [diff] [blame] | 4623 | break; |
| 4624 | |
| 4625 | case _VG_USERREQ__HG_PTHREAD_BARRIER_WAIT_PRE: |
| 4626 | /* pth_bar_t* */ |
| 4627 | evh__HG_PTHREAD_BARRIER_WAIT_PRE( tid, (void*)args[1] ); |
| 4628 | break; |
| 4629 | |
| 4630 | case _VG_USERREQ__HG_PTHREAD_BARRIER_DESTROY_PRE: |
| 4631 | /* pth_bar_t* */ |
| 4632 | evh__HG_PTHREAD_BARRIER_DESTROY_PRE( tid, (void*)args[1] ); |
| 4633 | break; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4634 | |
sewardj | 5a644da | 2009-08-11 10:35:58 +0000 | [diff] [blame] | 4635 | case _VG_USERREQ__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE: |
| 4636 | /* pth_spinlock_t* */ |
| 4637 | evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_PRE( tid, (void*)args[1] ); |
| 4638 | break; |
| 4639 | |
| 4640 | case _VG_USERREQ__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_POST: |
| 4641 | /* pth_spinlock_t* */ |
| 4642 | evh__HG_PTHREAD_SPIN_INIT_OR_UNLOCK_POST( tid, (void*)args[1] ); |
| 4643 | break; |
| 4644 | |
| 4645 | case _VG_USERREQ__HG_PTHREAD_SPIN_LOCK_PRE: |
| 4646 | /* pth_spinlock_t*, Word */ |
| 4647 | evh__HG_PTHREAD_SPIN_LOCK_PRE( tid, (void*)args[1], args[2] ); |
| 4648 | break; |
| 4649 | |
| 4650 | case _VG_USERREQ__HG_PTHREAD_SPIN_LOCK_POST: |
| 4651 | /* pth_spinlock_t* */ |
| 4652 | evh__HG_PTHREAD_SPIN_LOCK_POST( tid, (void*)args[1] ); |
| 4653 | break; |
| 4654 | |
| 4655 | case _VG_USERREQ__HG_PTHREAD_SPIN_DESTROY_PRE: |
| 4656 | /* pth_spinlock_t* */ |
| 4657 | evh__HG_PTHREAD_SPIN_DESTROY_PRE( tid, (void*)args[1] ); |
| 4658 | break; |
| 4659 | |
sewardj | ed2e72e | 2009-08-14 11:08:24 +0000 | [diff] [blame] | 4660 | case _VG_USERREQ__HG_CLIENTREQ_UNIMP: { |
| 4661 | /* char* who */ |
| 4662 | HChar* who = (HChar*)args[1]; |
| 4663 | HChar buf[50 + 50]; |
| 4664 | Thread* thr = map_threads_maybe_lookup( tid ); |
| 4665 | tl_assert( thr ); /* I must be mapped */ |
| 4666 | tl_assert( who ); |
| 4667 | tl_assert( VG_(strlen)(who) <= 50 ); |
| 4668 | VG_(sprintf)(buf, "Unimplemented client request macro \"%s\"", who ); |
| 4669 | /* record_error_Misc strdup's buf, so this is safe: */ |
| 4670 | HG_(record_error_Misc)( thr, buf ); |
| 4671 | break; |
| 4672 | } |
| 4673 | |
| 4674 | case _VG_USERREQ__HG_USERSO_SEND_PRE: |
| 4675 | /* UWord arbitrary-SO-tag */ |
| 4676 | evh__HG_USERSO_SEND_PRE( tid, args[1] ); |
| 4677 | break; |
| 4678 | |
| 4679 | case _VG_USERREQ__HG_USERSO_RECV_POST: |
| 4680 | /* UWord arbitrary-SO-tag */ |
| 4681 | evh__HG_USERSO_RECV_POST( tid, args[1] ); |
| 4682 | break; |
| 4683 | |
sewardj | 6015d0e | 2011-03-11 19:10:48 +0000 | [diff] [blame] | 4684 | case _VG_USERREQ__HG_USERSO_FORGET_ALL: |
| 4685 | /* UWord arbitrary-SO-tag */ |
| 4686 | evh__HG_USERSO_FORGET_ALL( tid, args[1] ); |
| 4687 | break; |
| 4688 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4689 | default: |
| 4690 | /* Unhandled Helgrind client request! */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4691 | tl_assert2(0, "unhandled Helgrind client request 0x%lx", |
| 4692 | args[0]); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | return True; |
| 4696 | } |
| 4697 | |
| 4698 | |
| 4699 | /*----------------------------------------------------------------*/ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4700 | /*--- Setup ---*/ |
| 4701 | /*----------------------------------------------------------------*/ |
| 4702 | |
| 4703 | static Bool hg_process_cmd_line_option ( Char* arg ) |
| 4704 | { |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 4705 | Char* tmp_str; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4706 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 4707 | if VG_BOOL_CLO(arg, "--track-lockorders", |
| 4708 | HG_(clo_track_lockorders)) {} |
| 4709 | else if VG_BOOL_CLO(arg, "--cmp-race-err-addrs", |
| 4710 | HG_(clo_cmp_race_err_addrs)) {} |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4711 | |
| 4712 | else if VG_XACT_CLO(arg, "--history-level=none", |
| 4713 | HG_(clo_history_level), 0); |
sewardj | f386139 | 2009-08-02 10:16:03 +0000 | [diff] [blame] | 4714 | else if VG_XACT_CLO(arg, "--history-level=approx", |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4715 | HG_(clo_history_level), 1); |
| 4716 | else if VG_XACT_CLO(arg, "--history-level=full", |
| 4717 | HG_(clo_history_level), 2); |
sewardj | 849b0ed | 2008-12-21 10:43:10 +0000 | [diff] [blame] | 4718 | |
sewardj | f585e48 | 2009-08-16 22:52:29 +0000 | [diff] [blame] | 4719 | /* If you change the 10k/30mill limits, remember to also change |
sewardj | 849b0ed | 2008-12-21 10:43:10 +0000 | [diff] [blame] | 4720 | them in assertions at the top of event_map_maybe_GC. */ |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 4721 | else if VG_BINT_CLO(arg, "--conflict-cache-size", |
sewardj | f585e48 | 2009-08-16 22:52:29 +0000 | [diff] [blame] | 4722 | HG_(clo_conflict_cache_size), 10*1000, 30*1000*1000) {} |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4723 | |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 4724 | /* "stuvwx" --> stuvwx (binary) */ |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 4725 | else if VG_STR_CLO(arg, "--hg-sanity-flags", tmp_str) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4726 | Int j; |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4727 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 4728 | if (6 != VG_(strlen)(tmp_str)) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4729 | VG_(message)(Vg_UserMsg, |
sewardj | 2411849 | 2009-07-15 14:50:02 +0000 | [diff] [blame] | 4730 | "--hg-sanity-flags argument must have 6 digits\n"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4731 | return False; |
| 4732 | } |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 4733 | for (j = 0; j < 6; j++) { |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 4734 | if ('0' == tmp_str[j]) { /* do nothing */ } |
| 4735 | 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] | 4736 | else { |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 4737 | VG_(message)(Vg_UserMsg, "--hg-sanity-flags argument can " |
sewardj | 2411849 | 2009-07-15 14:50:02 +0000 | [diff] [blame] | 4738 | "only contain 0s and 1s\n"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4739 | return False; |
| 4740 | } |
| 4741 | } |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4742 | 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] | 4743 | } |
| 4744 | |
sewardj | 622fe49 | 2011-03-11 21:06:59 +0000 | [diff] [blame] | 4745 | else if VG_BOOL_CLO(arg, "--free-is-write", |
| 4746 | HG_(clo_free_is_write)) {} |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4747 | |
| 4748 | else if VG_XACT_CLO(arg, "--vts-pruning=never", |
| 4749 | HG_(clo_vts_pruning), 0); |
| 4750 | else if VG_XACT_CLO(arg, "--vts-pruning=auto", |
| 4751 | HG_(clo_vts_pruning), 1); |
| 4752 | else if VG_XACT_CLO(arg, "--vts-pruning=always", |
| 4753 | HG_(clo_vts_pruning), 2); |
| 4754 | |
| 4755 | else if VG_BOOL_CLO(arg, "--check-stack-refs", |
| 4756 | HG_(clo_check_stack_refs)) {} |
| 4757 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4758 | else |
| 4759 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
| 4760 | |
| 4761 | return True; |
| 4762 | } |
| 4763 | |
| 4764 | static void hg_print_usage ( void ) |
| 4765 | { |
| 4766 | VG_(printf)( |
sewardj | 622fe49 | 2011-03-11 21:06:59 +0000 | [diff] [blame] | 4767 | " --free-is-write=no|yes treat heap frees as writes [no]\n" |
sewardj | 849b0ed | 2008-12-21 10:43:10 +0000 | [diff] [blame] | 4768 | " --track-lockorders=no|yes show lock ordering errors? [yes]\n" |
njn | f6e8ca9 | 2009-08-07 02:18:00 +0000 | [diff] [blame] | 4769 | " --history-level=none|approx|full [full]\n" |
sewardj | f386139 | 2009-08-02 10:16:03 +0000 | [diff] [blame] | 4770 | " full: show both stack traces for a data race (can be very slow)\n" |
| 4771 | " approx: full trace for one thread, approx for the other (faster)\n" |
| 4772 | " none: only show trace for one thread in a race (fastest)\n" |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4773 | " --conflict-cache-size=N size of 'full' history cache [1000000]\n" |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4774 | " --check-stack-refs=no|yes race-check reads and writes on the\n" |
| 4775 | " main stack and thread stacks? [yes]\n" |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4776 | ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
| 4779 | static void hg_print_debug_usage ( void ) |
| 4780 | { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4781 | VG_(printf)(" --cmp-race-err-addrs=no|yes are data addresses in " |
| 4782 | "race errors significant? [no]\n"); |
sewardj | 849b0ed | 2008-12-21 10:43:10 +0000 | [diff] [blame] | 4783 | VG_(printf)(" --hg-sanity-flags=<XXXXXX> sanity check " |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 4784 | " at events (X = 0|1) [000000]\n"); |
| 4785 | VG_(printf)(" --hg-sanity-flags values:\n"); |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 4786 | VG_(printf)(" 010000 after changes to " |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4787 | "lock-order-acquisition-graph\n"); |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 4788 | VG_(printf)(" 001000 at memory accesses (NB: not currently used)\n"); |
| 4789 | VG_(printf)(" 000100 at mem permission setting for " |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4790 | "ranges >= %d bytes\n", SCE_BIGRANGE_T); |
sewardj | 11e352f | 2007-11-30 11:11:02 +0000 | [diff] [blame] | 4791 | VG_(printf)(" 000010 at lock/unlock events\n"); |
| 4792 | VG_(printf)(" 000001 at thread create/join events\n"); |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame^] | 4793 | VG_(printf)( |
| 4794 | " --vts-pruning=never|auto|always [auto]\n" |
| 4795 | " never: is never done (may cause big space leaks in Helgrind)\n" |
| 4796 | " auto: done just often enough to keep space usage under control\n" |
| 4797 | " always: done after every VTS GC (mostly just a big time waster)\n" |
| 4798 | ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4801 | static void hg_fini ( Int exitcode ) |
| 4802 | { |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 4803 | if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) { |
| 4804 | VG_(message)(Vg_UserMsg, |
| 4805 | "For counts of detected and suppressed errors, " |
| 4806 | "rerun with: -v\n"); |
| 4807 | } |
| 4808 | |
| 4809 | if (VG_(clo_verbosity) == 1 && !VG_(clo_xml) |
| 4810 | && HG_(clo_history_level) >= 2) { |
| 4811 | VG_(umsg)( |
| 4812 | "Use --history-level=approx or =none to gain increased speed, at\n" ); |
| 4813 | VG_(umsg)( |
| 4814 | "the cost of reduced accuracy of conflicting-access information\n"); |
| 4815 | } |
| 4816 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4817 | if (SHOW_DATA_STRUCTURES) |
| 4818 | pp_everything( PP_ALL, "SK_(fini)" ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4819 | if (HG_(clo_sanity_flags)) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4820 | all__sanity_check("SK_(fini)"); |
| 4821 | |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 4822 | if (VG_(clo_stats)) { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4823 | |
| 4824 | if (1) { |
| 4825 | VG_(printf)("\n"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4826 | HG_(ppWSUstats)( univ_lsets, "univ_lsets" ); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 4827 | if (HG_(clo_track_lockorders)) { |
| 4828 | VG_(printf)("\n"); |
| 4829 | HG_(ppWSUstats)( univ_laog, "univ_laog" ); |
| 4830 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4831 | } |
| 4832 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4833 | //zz VG_(printf)("\n"); |
| 4834 | //zz VG_(printf)(" hbefore: %'10lu queries\n", stats__hbefore_queries); |
| 4835 | //zz VG_(printf)(" hbefore: %'10lu cache 0 hits\n", stats__hbefore_cache0s); |
| 4836 | //zz VG_(printf)(" hbefore: %'10lu cache > 0 hits\n", stats__hbefore_cacheNs); |
| 4837 | //zz VG_(printf)(" hbefore: %'10lu graph searches\n", stats__hbefore_gsearches); |
| 4838 | //zz VG_(printf)(" hbefore: %'10lu of which slow\n", |
| 4839 | //zz stats__hbefore_gsearches - stats__hbefore_gsearchFs); |
| 4840 | //zz VG_(printf)(" hbefore: %'10lu stack high water mark\n", |
| 4841 | //zz stats__hbefore_stk_hwm); |
| 4842 | //zz VG_(printf)(" hbefore: %'10lu cache invals\n", stats__hbefore_invals); |
| 4843 | //zz VG_(printf)(" hbefore: %'10lu probes\n", stats__hbefore_probes); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4844 | |
| 4845 | VG_(printf)("\n"); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4846 | VG_(printf)(" locksets: %'8d unique lock sets\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4847 | (Int)HG_(cardinalityWSU)( univ_lsets )); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 4848 | if (HG_(clo_track_lockorders)) { |
| 4849 | VG_(printf)(" univ_laog: %'8d unique lock sets\n", |
| 4850 | (Int)HG_(cardinalityWSU)( univ_laog )); |
| 4851 | } |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4852 | |
sewardj | d52392d | 2008-11-08 20:36:26 +0000 | [diff] [blame] | 4853 | //VG_(printf)("L(ast)L(ock) map: %'8lu inserts (%d map size)\n", |
| 4854 | // stats__ga_LL_adds, |
| 4855 | // (Int)(ga_to_lastlock ? VG_(sizeFM)( ga_to_lastlock ) : 0) ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4856 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4857 | VG_(printf)(" LockN-to-P map: %'8llu queries (%llu map size)\n", |
| 4858 | HG_(stats__LockN_to_P_queries), |
| 4859 | HG_(stats__LockN_to_P_get_map_size)() ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4860 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4861 | VG_(printf)("string table map: %'8llu queries (%llu map size)\n", |
| 4862 | HG_(stats__string_table_queries), |
| 4863 | HG_(stats__string_table_get_map_size)() ); |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 4864 | if (HG_(clo_track_lockorders)) { |
| 4865 | VG_(printf)(" LAOG: %'8d map size\n", |
| 4866 | (Int)(laog ? VG_(sizeFM)( laog ) : 0)); |
| 4867 | VG_(printf)(" LAOG exposition: %'8d map size\n", |
| 4868 | (Int)(laog_exposition ? VG_(sizeFM)( laog_exposition ) : 0)); |
| 4869 | } |
| 4870 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4871 | VG_(printf)(" locks: %'8lu acquires, " |
| 4872 | "%'lu releases\n", |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4873 | stats__lockN_acquires, |
| 4874 | stats__lockN_releases |
| 4875 | ); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 4876 | VG_(printf)(" sanity checks: %'8lu\n", stats__sanity_checks); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4877 | |
| 4878 | VG_(printf)("\n"); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4879 | libhb_shutdown(True); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4880 | } |
| 4881 | } |
| 4882 | |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4883 | /* FIXME: move these somewhere sane */ |
| 4884 | |
| 4885 | static |
| 4886 | void for_libhb__get_stacktrace ( Thr* hbt, Addr* frames, UWord nRequest ) |
| 4887 | { |
| 4888 | Thread* thr; |
| 4889 | ThreadId tid; |
| 4890 | UWord nActual; |
| 4891 | tl_assert(hbt); |
sewardj | 6062664 | 2011-03-10 15:14:37 +0000 | [diff] [blame] | 4892 | thr = libhb_get_Thr_hgthread( hbt ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4893 | tl_assert(thr); |
| 4894 | tid = map_threads_maybe_reverse_lookup_SLOW(thr); |
| 4895 | nActual = (UWord)VG_(get_StackTrace)( tid, frames, (UInt)nRequest, |
| 4896 | NULL, NULL, 0 ); |
| 4897 | tl_assert(nActual <= nRequest); |
| 4898 | for (; nActual < nRequest; nActual++) |
| 4899 | frames[nActual] = 0; |
| 4900 | } |
| 4901 | |
| 4902 | static |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4903 | ExeContext* for_libhb__get_EC ( Thr* hbt ) |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4904 | { |
| 4905 | Thread* thr; |
| 4906 | ThreadId tid; |
| 4907 | ExeContext* ec; |
| 4908 | tl_assert(hbt); |
sewardj | 6062664 | 2011-03-10 15:14:37 +0000 | [diff] [blame] | 4909 | thr = libhb_get_Thr_hgthread( hbt ); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4910 | tl_assert(thr); |
| 4911 | tid = map_threads_maybe_reverse_lookup_SLOW(thr); |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4912 | /* this will assert if tid is invalid */ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4913 | ec = VG_(record_ExeContext)( tid, 0 ); |
sewardj | d52392d | 2008-11-08 20:36:26 +0000 | [diff] [blame] | 4914 | return ec; |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 4918 | static void hg_post_clo_init ( void ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4919 | { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4920 | Thr* hbthr_root; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 4921 | |
sewardj | c1fb9d2 | 2011-02-28 09:03:44 +0000 | [diff] [blame] | 4922 | ///////////////////////////////////////////// |
| 4923 | hbthr_root = libhb_init( for_libhb__get_stacktrace, |
| 4924 | for_libhb__get_EC ); |
| 4925 | ///////////////////////////////////////////// |
| 4926 | |
| 4927 | |
| 4928 | if (HG_(clo_track_lockorders)) |
| 4929 | laog__init(); |
| 4930 | |
| 4931 | initialise_data_structures(hbthr_root); |
| 4932 | } |
| 4933 | |
| 4934 | static void hg_pre_clo_init ( void ) |
| 4935 | { |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4936 | VG_(details_name) ("Helgrind"); |
| 4937 | VG_(details_version) (NULL); |
| 4938 | VG_(details_description) ("a thread error detector"); |
| 4939 | VG_(details_copyright_author)( |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 4940 | "Copyright (C) 2007-2010, and GNU GPL'd, by OpenWorks LLP et al."); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4941 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | 9c08c0f | 2011-03-10 15:01:14 +0000 | [diff] [blame] | 4942 | VG_(details_avg_translation_sizeB) ( 320 ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4943 | |
| 4944 | VG_(basic_tool_funcs) (hg_post_clo_init, |
| 4945 | hg_instrument, |
| 4946 | hg_fini); |
| 4947 | |
| 4948 | VG_(needs_core_errors) (); |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4949 | VG_(needs_tool_errors) (HG_(eq_Error), |
sewardj | 2411849 | 2009-07-15 14:50:02 +0000 | [diff] [blame] | 4950 | HG_(before_pp_Error), |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4951 | HG_(pp_Error), |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4952 | False,/*show TIDs for errors*/ |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 4953 | HG_(update_extra), |
| 4954 | HG_(recognised_suppression), |
| 4955 | HG_(read_extra_suppression_info), |
| 4956 | HG_(error_matches_suppression), |
| 4957 | HG_(get_error_name), |
sewardj | 588adef | 2009-08-15 22:41:51 +0000 | [diff] [blame] | 4958 | HG_(get_extra_suppression_info)); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4959 | |
sewardj | 2411849 | 2009-07-15 14:50:02 +0000 | [diff] [blame] | 4960 | VG_(needs_xml_output) (); |
| 4961 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4962 | VG_(needs_command_line_options)(hg_process_cmd_line_option, |
| 4963 | hg_print_usage, |
| 4964 | hg_print_debug_usage); |
| 4965 | VG_(needs_client_requests) (hg_handle_client_request); |
| 4966 | |
| 4967 | // FIXME? |
| 4968 | //VG_(needs_sanity_checks) (hg_cheap_sanity_check, |
| 4969 | // hg_expensive_sanity_check); |
| 4970 | |
| 4971 | VG_(needs_malloc_replacement) (hg_cli__malloc, |
| 4972 | hg_cli____builtin_new, |
| 4973 | hg_cli____builtin_vec_new, |
| 4974 | hg_cli__memalign, |
| 4975 | hg_cli__calloc, |
| 4976 | hg_cli__free, |
| 4977 | hg_cli____builtin_delete, |
| 4978 | hg_cli____builtin_vec_delete, |
| 4979 | hg_cli__realloc, |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 4980 | hg_cli_malloc_usable_size, |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4981 | HG_CLI__MALLOC_REDZONE_SZB ); |
| 4982 | |
sewardj | 849b0ed | 2008-12-21 10:43:10 +0000 | [diff] [blame] | 4983 | /* 21 Dec 08: disabled this; it mostly causes H to start more |
| 4984 | slowly and use significantly more memory, without very often |
| 4985 | providing useful results. The user can request to load this |
| 4986 | information manually with --read-var-info=yes. */ |
| 4987 | if (0) VG_(needs_var_info)(); /* optional */ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4988 | |
| 4989 | VG_(track_new_mem_startup) ( evh__new_mem_w_perms ); |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 4990 | VG_(track_new_mem_stack_signal)( evh__new_mem_w_tid ); |
| 4991 | VG_(track_new_mem_brk) ( evh__new_mem_w_tid ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4992 | VG_(track_new_mem_mmap) ( evh__new_mem_w_perms ); |
sewardj | 1f77fec | 2010-04-12 19:51:04 +0000 | [diff] [blame] | 4993 | VG_(track_new_mem_stack) ( evh__new_mem_stack ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4994 | |
| 4995 | // FIXME: surely this isn't thread-aware |
sewardj | 23f1200 | 2009-07-24 08:45:08 +0000 | [diff] [blame] | 4996 | VG_(track_copy_mem_remap) ( evh__copy_mem ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 4997 | |
| 4998 | VG_(track_change_mem_mprotect) ( evh__set_perms ); |
| 4999 | |
| 5000 | VG_(track_die_mem_stack_signal)( evh__die_mem ); |
sewardj | fd35d49 | 2011-03-17 19:39:55 +0000 | [diff] [blame] | 5001 | VG_(track_die_mem_brk) ( evh__die_mem_munmap ); |
| 5002 | VG_(track_die_mem_munmap) ( evh__die_mem_munmap ); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5003 | VG_(track_die_mem_stack) ( evh__die_mem ); |
| 5004 | |
| 5005 | // FIXME: what is this for? |
| 5006 | VG_(track_ban_mem_stack) (NULL); |
| 5007 | |
| 5008 | VG_(track_pre_mem_read) ( evh__pre_mem_read ); |
| 5009 | VG_(track_pre_mem_read_asciiz) ( evh__pre_mem_read_asciiz ); |
| 5010 | VG_(track_pre_mem_write) ( evh__pre_mem_write ); |
| 5011 | VG_(track_post_mem_write) (NULL); |
| 5012 | |
| 5013 | ///////////////// |
| 5014 | |
| 5015 | VG_(track_pre_thread_ll_create)( evh__pre_thread_ll_create ); |
| 5016 | VG_(track_pre_thread_ll_exit) ( evh__pre_thread_ll_exit ); |
| 5017 | |
| 5018 | VG_(track_start_client_code)( evh__start_client_code ); |
| 5019 | VG_(track_stop_client_code)( evh__stop_client_code ); |
| 5020 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5021 | /* Ensure that requirements for "dodgy C-as-C++ style inheritance" |
| 5022 | as described in comments at the top of pub_tool_hashtable.h, are |
| 5023 | met. Blargh. */ |
| 5024 | tl_assert( sizeof(void*) == sizeof(struct _MallocMeta*) ); |
| 5025 | tl_assert( sizeof(UWord) == sizeof(Addr) ); |
| 5026 | hg_mallocmeta_table |
| 5027 | = VG_(HT_construct)( "hg_malloc_metadata_table" ); |
| 5028 | |
sewardj | 61bc2c5 | 2011-02-09 10:34:00 +0000 | [diff] [blame] | 5029 | // add a callback to clean up on (threaded) fork. |
| 5030 | VG_(atfork)(NULL/*pre*/, NULL/*parent*/, evh__atfork_child/*child*/); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
| 5033 | VG_DETERMINE_INTERFACE_VERSION(hg_pre_clo_init) |
| 5034 | |
| 5035 | /*--------------------------------------------------------------------*/ |
| 5036 | /*--- end hg_main.c ---*/ |
| 5037 | /*--------------------------------------------------------------------*/ |