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