njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 1 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 3 | /*--- Helgrind: checking for data races in threaded programs. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 4 | /*--- hg_main.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 8 | This file is part of Helgrind, a Valgrind tool for detecting |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 9 | data races in threaded programs. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 10 | |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 11 | Copyright (C) 2002-2004 Nicholas Nethercote |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 12 | njn25@cam.ac.uk |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
| 29 | The GNU General Public License is contained in the file COPYING. |
| 30 | */ |
| 31 | |
| 32 | #include "vg_skin.h" |
sewardj | 7f3ad22 | 2002-11-13 22:11:53 +0000 | [diff] [blame] | 33 | #include "helgrind.h" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 34 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 35 | static UInt n_eraser_warnings = 0; |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 36 | static UInt n_lockorder_warnings = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 37 | |
| 38 | /*------------------------------------------------------------*/ |
| 39 | /*--- Debug guff ---*/ |
| 40 | /*------------------------------------------------------------*/ |
| 41 | |
sewardj | e11d6c8 | 2002-12-15 02:00:41 +0000 | [diff] [blame] | 42 | #define DEBUG_LOCK_TABLE 0 /* Print lock table at end */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 43 | |
| 44 | #define DEBUG_MAKE_ACCESSES 0 /* Print make_access() calls */ |
| 45 | #define DEBUG_LOCKS 0 /* Print lock()/unlock() calls and locksets */ |
| 46 | #define DEBUG_NEW_LOCKSETS 0 /* Print new locksets when created */ |
| 47 | #define DEBUG_ACCESSES 0 /* Print reads, writes */ |
| 48 | #define DEBUG_MEM_LOCKSET_CHANGES 0 |
| 49 | /* Print when an address's lockset |
| 50 | changes; only useful with |
| 51 | DEBUG_ACCESSES */ |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 52 | #define SLOW_ASSERTS 0 /* do expensive asserts */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 53 | #define DEBUG_VIRGIN_READS 0 /* Dump around address on VIRGIN reads */ |
| 54 | |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 55 | #if SLOW_ASSERTS |
| 56 | #define SK_ASSERT(x) sk_assert(x) |
| 57 | #else |
| 58 | #define SK_ASSERT(x) |
| 59 | #endif |
| 60 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 61 | /* heavyweight LockSet sanity checking: |
| 62 | 0 == never |
| 63 | 1 == after important ops |
| 64 | 2 == As 1 and also after pthread_mutex_* ops (excessively slow) |
| 65 | */ |
| 66 | #define LOCKSET_SANITY 0 |
| 67 | |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 68 | /* Rotate an unsigned quantity left */ |
| 69 | #define ROTL(x, n) (((x) << (n)) | ((x) >> ((sizeof(x)*8)-(n)))) |
| 70 | |
| 71 | /* round a up to the next multiple of N. N must be a power of 2 */ |
| 72 | #define ROUNDUP(a, N) ((a + N - 1) & ~(N-1)) |
| 73 | |
| 74 | /* Round a down to the next multiple of N. N must be a power of 2 */ |
| 75 | #define ROUNDDN(a, N) ((a) & ~(N-1)) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 76 | |
| 77 | /*------------------------------------------------------------*/ |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 78 | /*--- Command line options ---*/ |
| 79 | /*------------------------------------------------------------*/ |
| 80 | |
| 81 | static enum { |
| 82 | EC_None, |
| 83 | EC_Some, |
| 84 | EC_All |
| 85 | } clo_execontext = EC_None; |
| 86 | |
sewardj | e1a39f4 | 2002-12-15 01:56:17 +0000 | [diff] [blame] | 87 | static Bool clo_priv_stacks = False; |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 88 | |
| 89 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 90 | /*--- Crude profiling machinery. ---*/ |
| 91 | /*------------------------------------------------------------*/ |
| 92 | |
| 93 | // PPP: work out if I want this |
| 94 | |
| 95 | #define PROF_EVENT(x) |
| 96 | #if 0 |
| 97 | #ifdef VG_PROFILE_MEMORY |
| 98 | |
| 99 | #define N_PROF_EVENTS 150 |
| 100 | |
| 101 | static UInt event_ctr[N_PROF_EVENTS]; |
| 102 | |
| 103 | void VGE_(done_prof_mem) ( void ) |
| 104 | { |
| 105 | Int i; |
| 106 | for (i = 0; i < N_PROF_EVENTS; i++) { |
| 107 | if ((i % 10) == 0) |
| 108 | VG_(printf)("\n"); |
| 109 | if (event_ctr[i] > 0) |
| 110 | VG_(printf)( "prof mem event %2d: %d\n", i, event_ctr[i] ); |
| 111 | } |
| 112 | VG_(printf)("\n"); |
| 113 | } |
| 114 | |
| 115 | #define PROF_EVENT(ev) \ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 116 | do { sk_assert((ev) >= 0 && (ev) < N_PROF_EVENTS); \ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 117 | event_ctr[ev]++; \ |
| 118 | } while (False); |
| 119 | |
| 120 | #else |
| 121 | |
| 122 | //static void init_prof_mem ( void ) { } |
| 123 | // void VG_(done_prof_mem) ( void ) { } |
| 124 | |
| 125 | #define PROF_EVENT(ev) /* */ |
| 126 | |
| 127 | #endif /* VG_PROFILE_MEMORY */ |
| 128 | |
| 129 | /* Event index. If just the name of the fn is given, this means the |
| 130 | number of calls to the fn. Otherwise it is the specified event. |
| 131 | |
| 132 | [PPP: snip event numbers...] |
| 133 | */ |
| 134 | #endif /* 0 */ |
| 135 | |
| 136 | |
| 137 | /*------------------------------------------------------------*/ |
| 138 | /*--- Data defns. ---*/ |
| 139 | /*------------------------------------------------------------*/ |
| 140 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 141 | typedef |
| 142 | struct _HG_Chunk { |
| 143 | struct _HG_Chunk* next; |
| 144 | Addr data; /* ptr to actual block */ |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 145 | Int size; /* size requested */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 146 | ExeContext* where; /* where it was allocated */ |
| 147 | ThreadId tid; /* allocating thread */ |
| 148 | } |
| 149 | HG_Chunk; |
| 150 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 151 | typedef enum |
sewardj | 7f3ad22 | 2002-11-13 22:11:53 +0000 | [diff] [blame] | 152 | { Vge_VirginInit, Vge_NonVirginInit, Vge_SegmentInit, Vge_Error } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 153 | VgeInitStatus; |
| 154 | |
sewardj | c808ef5 | 2002-11-13 22:43:26 +0000 | [diff] [blame] | 155 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 156 | /* Should add up to 32 to fit in one word */ |
| 157 | #define OTHER_BITS 30 |
| 158 | #define STATE_BITS 2 |
| 159 | |
| 160 | #define ESEC_MAP_WORDS 16384 /* Words per secondary map */ |
| 161 | |
| 162 | /* This is for indicating that a memory block has been initialised but not |
| 163 | * really directly by a particular thread... (eg. text/data initialised |
| 164 | * automatically at startup). |
| 165 | * Must be different to virgin_word.other */ |
| 166 | #define TID_INDICATING_NONVIRGIN 1 |
| 167 | |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 168 | /* Magic packed TLS used for error suppression; if word state is Excl |
| 169 | and tid is this, then it means all access are OK without changing |
| 170 | state and without raising any more errors */ |
| 171 | #define TLSP_INDICATING_ALL ((1 << OTHER_BITS) - 1) |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 172 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 173 | /* Number of entries must fit in STATE_BITS bits */ |
| 174 | typedef enum { Vge_Virgin, Vge_Excl, Vge_Shar, Vge_SharMod } pth_state; |
| 175 | |
sewardj | c808ef5 | 2002-11-13 22:43:26 +0000 | [diff] [blame] | 176 | static inline const Char *pp_state(pth_state st) |
| 177 | { |
| 178 | const Char *ret; |
| 179 | |
| 180 | switch(st) { |
| 181 | case Vge_Virgin: ret = "virgin"; break; |
| 182 | case Vge_Excl: ret = "exclusive"; break; |
| 183 | case Vge_Shar: ret = "shared RO"; break; |
| 184 | case Vge_SharMod: ret = "shared RW"; break; |
| 185 | default: ret = "???"; |
| 186 | } |
| 187 | return ret; |
| 188 | } |
| 189 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 190 | typedef |
| 191 | struct { |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 192 | /* gcc arranges this bitfield with state in the 2LSB and other |
| 193 | in the 30MSB, which is what we want */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 194 | UInt state:STATE_BITS; |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 195 | UInt other:OTHER_BITS; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 196 | } shadow_word; |
| 197 | |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 198 | #define SW(st, other) ((shadow_word) { st, other }) |
| 199 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 200 | typedef |
| 201 | struct { |
| 202 | shadow_word swords[ESEC_MAP_WORDS]; |
| 203 | } |
| 204 | ESecMap; |
| 205 | |
| 206 | static ESecMap* primary_map[ 65536 ]; |
| 207 | static ESecMap distinguished_secondary_map; |
| 208 | |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 209 | static const shadow_word virgin_sword = SW(Vge_Virgin, 0); |
| 210 | static const shadow_word error_sword = SW(Vge_Excl, TLSP_INDICATING_ALL); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 211 | |
| 212 | #define VGE_IS_DISTINGUISHED_SM(smap) \ |
| 213 | ((smap) == &distinguished_secondary_map) |
| 214 | |
| 215 | #define ENSURE_MAPPABLE(addr,caller) \ |
| 216 | do { \ |
| 217 | if (VGE_IS_DISTINGUISHED_SM(primary_map[(addr) >> 16])) { \ |
| 218 | primary_map[(addr) >> 16] = alloc_secondary_map(caller); \ |
| 219 | /*VG_(printf)("new 2map because of %p\n", addr);*/ \ |
| 220 | } \ |
| 221 | } while(0) |
| 222 | |
| 223 | |
sewardj | c808ef5 | 2002-11-13 22:43:26 +0000 | [diff] [blame] | 224 | /* Parallel map which contains execution contexts when words last |
| 225 | changed state (if required) */ |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 226 | |
sewardj | c808ef5 | 2002-11-13 22:43:26 +0000 | [diff] [blame] | 227 | typedef struct EC_EIP { |
| 228 | union u_ec_eip { |
| 229 | Addr eip; |
| 230 | ExeContext *ec; |
sewardj | 72baa7a | 2002-12-09 23:32:58 +0000 | [diff] [blame] | 231 | } uu_ec_eip; |
sewardj | c808ef5 | 2002-11-13 22:43:26 +0000 | [diff] [blame] | 232 | UInt state:STATE_BITS; |
| 233 | UInt tls:OTHER_BITS; /* packed TLS */ |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 234 | } EC_EIP; |
| 235 | |
sewardj | c808ef5 | 2002-11-13 22:43:26 +0000 | [diff] [blame] | 236 | #define NULL_EC_EIP ((EC_EIP){ { 0 }, 0, 0}) |
| 237 | |
| 238 | #define EIP(eip, prev, tls) ((EC_EIP) { (union u_ec_eip)(eip), (prev).state, packTLS(tls) }) |
| 239 | #define EC(ec, prev, tls) ((EC_EIP) { (union u_ec_eip)(ec), (prev).state, packTLS(tls) }) |
| 240 | |
| 241 | static inline UInt packEC(ExeContext *ec) |
| 242 | { |
| 243 | SK_ASSERT(((UInt)ec & ((1 << STATE_BITS)-1)) == 0); |
| 244 | return ((UInt)ec) >> STATE_BITS; |
| 245 | } |
| 246 | |
| 247 | static inline ExeContext *unpackEC(UInt i) |
| 248 | { |
| 249 | return (ExeContext *)(i << STATE_BITS); |
| 250 | } |
| 251 | |
| 252 | /* Lose 2 LSB of eip */ |
| 253 | static inline UInt packEIP(Addr eip) |
| 254 | { |
| 255 | return ((UInt)eip) >> STATE_BITS; |
| 256 | } |
| 257 | |
| 258 | static inline Addr unpackEIP(UInt i) |
| 259 | { |
| 260 | return (Addr)(i << STATE_BITS); |
| 261 | } |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 262 | |
| 263 | typedef struct { |
| 264 | EC_EIP execontext[ESEC_MAP_WORDS]; |
| 265 | } ExeContextMap; |
| 266 | |
| 267 | static ExeContextMap** execontext_map; |
| 268 | |
| 269 | static inline void setExeContext(Addr a, EC_EIP ec) |
| 270 | { |
| 271 | UInt idx = (a >> 16) & 0xffff; |
| 272 | UInt off = (a >> 2) & 0x3fff; |
| 273 | |
| 274 | if (execontext_map[idx] == NULL) { |
| 275 | execontext_map[idx] = VG_(malloc)(sizeof(ExeContextMap)); |
| 276 | VG_(memset)(execontext_map[idx], 0, sizeof(ExeContextMap)); |
| 277 | } |
| 278 | |
| 279 | execontext_map[idx]->execontext[off] = ec; |
| 280 | } |
| 281 | |
| 282 | static inline EC_EIP getExeContext(Addr a) |
| 283 | { |
| 284 | UInt idx = (a >> 16) & 0xffff; |
| 285 | UInt off = (a >> 2) & 0x3fff; |
sewardj | c808ef5 | 2002-11-13 22:43:26 +0000 | [diff] [blame] | 286 | EC_EIP ec = NULL_EC_EIP; |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 287 | |
| 288 | if (execontext_map[idx] != NULL) |
| 289 | ec = execontext_map[idx]->execontext[off]; |
| 290 | |
| 291 | return ec; |
| 292 | } |
| 293 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 294 | /*------------------------------------------------------------*/ |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 295 | /*--- Thread lifetime segments ---*/ |
| 296 | /*------------------------------------------------------------*/ |
| 297 | |
| 298 | /* |
| 299 | * This mechanism deals with the common case of a parent thread |
| 300 | * creating a structure for a child thread, and then passing ownership |
| 301 | * of the structure to that thread. It similarly copes with a child |
| 302 | * thread passing information back to another thread waiting to join |
| 303 | * on it. |
| 304 | * |
| 305 | * Each thread's lifetime can be partitioned into segments. Those |
| 306 | * segments are arranged to form an interference graph which indicates |
| 307 | * whether two thread lifetime segments can possibly be concurrent. |
| 308 | * If not, then memory with is exclusively accessed by one TLS can be |
daywalker | 7e73e5f | 2003-07-04 16:18:15 +0000 | [diff] [blame] | 309 | * passed on to another TLS without an error occurring, and without |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 310 | * moving it from Excl state. |
| 311 | * |
| 312 | * At present this only considers thread creation and join as |
| 313 | * synchronisation events for creating new lifetime segments, but |
| 314 | * others may be possible (like mutex operations). |
| 315 | */ |
| 316 | |
| 317 | typedef struct _ThreadLifeSeg ThreadLifeSeg; |
| 318 | |
| 319 | struct _ThreadLifeSeg { |
| 320 | ThreadId tid; |
| 321 | ThreadLifeSeg *prior[2]; /* Previous lifetime segments */ |
| 322 | UInt refcount; /* Number of memory locations pointing here */ |
| 323 | UInt mark; /* mark used for graph traversal */ |
| 324 | ThreadLifeSeg *next; /* list of all TLS */ |
| 325 | }; |
| 326 | |
| 327 | static ThreadLifeSeg *all_tls; |
| 328 | static UInt tls_since_gc; |
| 329 | #define TLS_SINCE_GC 10000 |
| 330 | |
| 331 | /* current mark used for TLS graph traversal */ |
| 332 | static UInt tlsmark; |
| 333 | |
| 334 | static ThreadLifeSeg *thread_seg[VG_N_THREADS]; |
| 335 | |
| 336 | |
| 337 | static void tls_gc(void) |
| 338 | { |
| 339 | /* XXX later. Walk through all TLSs and look for ones with 0 |
| 340 | refcount and remove them from the structure and free them. |
| 341 | Could probably get rid of ThreadLifeSeg.refcount and simply use |
| 342 | mark-sweep from the shadow table. */ |
| 343 | VG_(printf)("WRITEME: TLS GC\n"); |
| 344 | } |
| 345 | |
| 346 | static void newTLS(ThreadId tid) |
| 347 | { |
| 348 | static const Bool debug = False; |
| 349 | ThreadLifeSeg *tls; |
| 350 | |
| 351 | /* Initial NULL */ |
| 352 | if (thread_seg[tid] == NULL) { |
| 353 | tls = VG_(malloc)(sizeof(*tls)); |
| 354 | tls->tid = tid; |
| 355 | tls->prior[0] = tls->prior[1] = NULL; |
| 356 | tls->refcount = 0; |
| 357 | tls->mark = tlsmark-1; |
| 358 | |
| 359 | tls->next = all_tls; |
| 360 | all_tls = tls; |
| 361 | tls_since_gc++; |
| 362 | |
| 363 | thread_seg[tid] = tls; |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | /* Previous TLS was unused, so just recycle */ |
| 368 | if (thread_seg[tid]->refcount == 0) { |
| 369 | if (debug) |
| 370 | VG_(printf)("newTLS; recycling TLS %p for tid %u\n", |
| 371 | thread_seg[tid], tid); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | /* Use existing TLS for this tid as a prior for new TLS */ |
| 376 | tls = VG_(malloc)(sizeof(*tls)); |
| 377 | tls->tid = tid; |
| 378 | tls->prior[0] = thread_seg[tid]; |
| 379 | tls->prior[1] = NULL; |
| 380 | tls->refcount = 0; |
| 381 | tls->mark = tlsmark-1; |
| 382 | |
| 383 | tls->next = all_tls; |
| 384 | all_tls = tls; |
| 385 | if (++tls_since_gc > TLS_SINCE_GC) { |
| 386 | tls_gc(); |
| 387 | tls_since_gc = 0; |
| 388 | } |
| 389 | |
| 390 | if (debug) |
| 391 | VG_(printf)("newTLS: made new TLS %p for tid %u (prior %p(%u))\n", |
| 392 | tls, tid, tls->prior[0], tls->prior[0]->tid); |
| 393 | |
| 394 | thread_seg[tid] = tls; |
| 395 | } |
| 396 | |
| 397 | /* clear out a TLS for a thread that's died */ |
| 398 | static void clearTLS(ThreadId tid) |
| 399 | { |
| 400 | newTLS(tid); |
| 401 | |
| 402 | thread_seg[tid]->prior[0] = NULL; |
| 403 | thread_seg[tid]->prior[1] = NULL; |
| 404 | } |
| 405 | |
| 406 | static void addPriorTLS(ThreadId tid, ThreadId prior) |
| 407 | { |
| 408 | static const Bool debug = False; |
| 409 | ThreadLifeSeg *tls = thread_seg[tid]; |
| 410 | |
| 411 | if (debug) |
| 412 | VG_(printf)("making TLS %p(%u) prior to TLS %p(%u)\n", |
| 413 | thread_seg[prior], prior, tls, tid); |
| 414 | |
| 415 | sk_assert(thread_seg[tid] != NULL); |
| 416 | sk_assert(thread_seg[prior] != NULL); |
| 417 | |
| 418 | if (tls->prior[0] == NULL) |
| 419 | tls->prior[0] = thread_seg[prior]; |
| 420 | else { |
| 421 | sk_assert(tls->prior[1] == NULL); |
| 422 | tls->prior[1] = thread_seg[prior]; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /* Return True if prior is definitely not concurrent with tls */ |
| 427 | static Bool tlsIsDisjoint(const ThreadLifeSeg *tls, |
| 428 | const ThreadLifeSeg *prior) |
| 429 | { |
| 430 | Bool isPrior(const ThreadLifeSeg *t) { |
| 431 | if (t == NULL || t->mark == tlsmark) |
| 432 | return False; |
| 433 | |
| 434 | if (t == prior) |
| 435 | return True; |
| 436 | |
| 437 | ((ThreadLifeSeg *)t)->mark = tlsmark; |
| 438 | |
| 439 | return isPrior(t->prior[0]) || isPrior(t->prior[1]); |
| 440 | } |
| 441 | tlsmark++; /* new traversal mark */ |
| 442 | |
| 443 | return isPrior(tls); |
| 444 | } |
| 445 | |
| 446 | static inline UInt packTLS(ThreadLifeSeg *tls) |
| 447 | { |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 448 | SK_ASSERT(((UInt)tls & ((1 << STATE_BITS)-1)) == 0); |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 449 | return ((UInt)tls) >> STATE_BITS; |
| 450 | } |
| 451 | |
| 452 | static inline ThreadLifeSeg *unpackTLS(UInt i) |
| 453 | { |
| 454 | return (ThreadLifeSeg *)(i << STATE_BITS); |
| 455 | } |
| 456 | |
| 457 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 458 | /*--- Low-level support for memory tracking. ---*/ |
| 459 | /*------------------------------------------------------------*/ |
| 460 | |
| 461 | /* |
| 462 | All reads and writes are recorded in the memory map, which |
| 463 | records the state of all memory in the process. The memory map is |
| 464 | organised like that for normal Valgrind, except each that everything |
| 465 | is done at word-level instead of byte-level, and each word has only |
| 466 | one word of shadow (instead of 36 bits). |
| 467 | |
| 468 | As for normal Valgrind there is a distinguished secondary map. But we're |
| 469 | working at word-granularity, so it has 16k word entries instead of 64k byte |
| 470 | entries. Lookup is done as follows: |
| 471 | |
| 472 | bits 31..16: primary map lookup |
| 473 | bits 15.. 2: secondary map lookup |
| 474 | bits 1.. 0: ignored |
| 475 | */ |
| 476 | |
| 477 | |
| 478 | /*------------------------------------------------------------*/ |
| 479 | /*--- Basic bitmap management, reading and writing. ---*/ |
| 480 | /*------------------------------------------------------------*/ |
| 481 | |
| 482 | /* Allocate and initialise a secondary map, marking all words as virgin. */ |
| 483 | |
| 484 | /* Just a value that isn't a real pointer */ |
| 485 | #define SEC_MAP_ACCESS (shadow_word*)0x99 |
| 486 | |
| 487 | |
| 488 | static |
| 489 | ESecMap* alloc_secondary_map ( __attribute__ ((unused)) Char* caller ) |
| 490 | { |
| 491 | ESecMap* map; |
| 492 | UInt i; |
| 493 | //PROF_EVENT(10); PPP |
| 494 | |
| 495 | /* It just happens that a SecMap occupies exactly 18 pages -- |
| 496 | although this isn't important, so the following assert is |
| 497 | spurious. (SSS: not true for ESecMaps -- they're 16 pages) */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 498 | sk_assert(0 == (sizeof(ESecMap) % VKI_BYTES_PER_PAGE)); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 499 | map = (ESecMap *)VG_(shadow_alloc)(sizeof(ESecMap)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 500 | |
| 501 | for (i = 0; i < ESEC_MAP_WORDS; i++) |
| 502 | map->swords[i] = virgin_sword; |
| 503 | |
| 504 | return map; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | /* Set a word. The byte give by 'a' could be anywhere in the word -- the whole |
| 509 | * word gets set. */ |
sewardj | 5686735 | 2003-10-12 10:27:06 +0000 | [diff] [blame] | 510 | static /* __inline__ */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 511 | void set_sword ( Addr a, shadow_word sword ) |
| 512 | { |
| 513 | ESecMap* sm; |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 514 | shadow_word *oldsw; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 515 | |
| 516 | //PROF_EVENT(23); PPP |
| 517 | ENSURE_MAPPABLE(a, "VGE_(set_sword)"); |
| 518 | |
| 519 | /* Use bits 31..16 for primary, 15..2 for secondary lookup */ |
| 520 | sm = primary_map[a >> 16]; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 521 | sk_assert(sm != &distinguished_secondary_map); |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 522 | oldsw = &sm->swords[(a & 0xFFFC) >> 2]; |
| 523 | if (oldsw->state == Vge_Excl && oldsw->other != TLSP_INDICATING_ALL) { |
| 524 | ThreadLifeSeg *tls = unpackTLS(oldsw->other); |
| 525 | tls->refcount--; |
| 526 | } |
| 527 | |
| 528 | if (sword.state == Vge_Excl && sword.other != TLSP_INDICATING_ALL) { |
| 529 | ThreadLifeSeg *tls = unpackTLS(sword.other); |
| 530 | tls->refcount++; |
| 531 | } |
| 532 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 533 | sm->swords[(a & 0xFFFC) >> 2] = sword; |
| 534 | |
| 535 | if (VGE_IS_DISTINGUISHED_SM(sm)) { |
| 536 | VG_(printf)("wrote to distinguished 2ndary map! 0x%x\n", a); |
| 537 | // XXX: may be legit, but I want to know when it happens --njn |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 538 | VG_(skin_panic)("wrote to distinguished 2ndary map!"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | |
| 543 | static __inline__ |
| 544 | shadow_word* get_sword_addr ( Addr a ) |
| 545 | { |
| 546 | /* Use bits 31..16 for primary, 15..2 for secondary lookup */ |
| 547 | ESecMap* sm = primary_map[a >> 16]; |
| 548 | UInt sm_off = (a & 0xFFFC) >> 2; |
| 549 | |
| 550 | if (VGE_IS_DISTINGUISHED_SM(sm)) { |
| 551 | VG_(printf)("accessed distinguished 2ndary map! 0x%x\n", a); |
| 552 | // XXX: may be legit, but I want to know when it happens --njn |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 553 | //VG_(skin_panic)("accessed distinguished 2ndary map!"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 554 | return SEC_MAP_ACCESS; |
| 555 | } |
| 556 | |
| 557 | //PROF_EVENT(21); PPP |
| 558 | return & (sm->swords[sm_off]); |
| 559 | } |
| 560 | |
| 561 | |
| 562 | // SSS: rename these so they're not so similar to memcheck, unless it's |
| 563 | // appropriate of course |
| 564 | |
| 565 | static __inline__ |
| 566 | void init_virgin_sword(Addr a) |
| 567 | { |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 568 | if (clo_execontext != EC_None) |
| 569 | setExeContext(a, NULL_EC_EIP); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 570 | set_sword(a, virgin_sword); |
| 571 | } |
| 572 | |
sewardj | 7f3ad22 | 2002-11-13 22:11:53 +0000 | [diff] [blame] | 573 | static __inline__ |
| 574 | void init_error_sword(Addr a) |
| 575 | { |
| 576 | set_sword(a, error_sword); |
| 577 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 578 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 579 | static __inline__ |
| 580 | void init_nonvirgin_sword(Addr a) |
| 581 | { |
| 582 | shadow_word sword; |
sewardj | b52a1b0 | 2002-10-23 21:38:22 +0000 | [diff] [blame] | 583 | ThreadId tid = VG_(get_current_or_recent_tid)(); |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 584 | ThreadLifeSeg *tls; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 585 | |
sewardj | b52a1b0 | 2002-10-23 21:38:22 +0000 | [diff] [blame] | 586 | sk_assert(tid != VG_INVALID_THREADID); |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 587 | tls = thread_seg[tid]; |
| 588 | |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 589 | sword = SW(Vge_Excl, packTLS(tls)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 590 | set_sword(a, sword); |
| 591 | } |
| 592 | |
| 593 | |
| 594 | /* In this case, we treat it for Eraser's sake like virgin (it hasn't |
| 595 | * been inited by a particular thread, it's just done automatically upon |
| 596 | * startup), but we mark its .state specially so it doesn't look like an |
| 597 | * uninited read. */ |
| 598 | static __inline__ |
| 599 | void init_magically_inited_sword(Addr a) |
| 600 | { |
| 601 | shadow_word sword; |
| 602 | |
sewardj | b52a1b0 | 2002-10-23 21:38:22 +0000 | [diff] [blame] | 603 | sk_assert(VG_INVALID_THREADID == VG_(get_current_tid)()); |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 604 | |
| 605 | sword = SW(Vge_Virgin, TID_INDICATING_NONVIRGIN); |
| 606 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 607 | set_sword(a, virgin_sword); |
| 608 | } |
| 609 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 610 | |
sewardj | 274c601 | 2002-10-22 04:54:55 +0000 | [diff] [blame] | 611 | /*------------------------------------------------------------*/ |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 612 | /*--- Implementation of lock sets. ---*/ |
sewardj | 274c601 | 2002-10-22 04:54:55 +0000 | [diff] [blame] | 613 | /*------------------------------------------------------------*/ |
| 614 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 615 | typedef struct _Mutex Mutex; /* forward decl */ |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 616 | typedef struct _LockSet LockSet; |
| 617 | |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 618 | typedef enum MutexState { |
| 619 | MxUnknown, /* don't know */ |
| 620 | MxUnlocked, /* unlocked */ |
| 621 | MxLocked, /* locked */ |
| 622 | MxDead /* destroyed */ |
| 623 | } MutexState; |
| 624 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 625 | struct _Mutex { |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 626 | Addr mutexp; |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 627 | Mutex *next; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 628 | |
| 629 | MutexState state; /* mutex state */ |
| 630 | ThreadId tid; /* owner */ |
| 631 | ExeContext *location; /* where the last change happened */ |
sewardj | 274c601 | 2002-10-22 04:54:55 +0000 | [diff] [blame] | 632 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 633 | const LockSet *lockdep; /* set of locks we depend on */ |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 634 | UInt mark; /* mark for graph traversal */ |
| 635 | }; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 636 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 637 | static inline Int mutex_cmp(const Mutex *a, const Mutex *b) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 638 | { |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 639 | return a->mutexp - b->mutexp; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 640 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 641 | |
sewardj | 274c601 | 2002-10-22 04:54:55 +0000 | [diff] [blame] | 642 | struct _LockSet { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 643 | Int setsize; /* number of members */ |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 644 | UInt hash; /* hash code */ |
| 645 | LockSet *next; /* next in hash chain */ |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 646 | const Mutex *mutex[0]; /* locks */ |
sewardj | 274c601 | 2002-10-22 04:54:55 +0000 | [diff] [blame] | 647 | }; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 648 | |
| 649 | static const LockSet *emptyset; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 650 | |
| 651 | /* Each one is an index into the lockset table. */ |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 652 | static const LockSet *thread_locks[VG_N_THREADS]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 653 | |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 654 | #define LOCKSET_HASH_SZ 1021 |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 655 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 656 | static LockSet *lockset_hash[LOCKSET_HASH_SZ]; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 657 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 658 | /* Pack and unpack a LockSet pointer into shadow_word.other */ |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 659 | static inline UInt packLockSet(const LockSet *p) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 660 | { |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 661 | UInt id; |
| 662 | |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 663 | SK_ASSERT(((UInt)p & ((1 << STATE_BITS)-1)) == 0); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 664 | id = ((UInt)p) >> STATE_BITS; |
| 665 | |
| 666 | return id; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 667 | } |
| 668 | |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 669 | static inline const LockSet *unpackLockSet(UInt id) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 670 | { |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 671 | return (LockSet *)(id << STATE_BITS); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 672 | } |
| 673 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 674 | static |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 675 | void pp_LockSet(const LockSet* p) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 676 | { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 677 | Int i; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 678 | VG_(printf)("{ "); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 679 | for(i = 0; i < p->setsize; i++) { |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 680 | const Mutex *mx = p->mutex[i]; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 681 | |
| 682 | VG_(printf)("%p%(y ", mx->mutexp, mx->mutexp); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 683 | } |
| 684 | VG_(printf)("}\n"); |
| 685 | } |
| 686 | |
| 687 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 688 | static void print_LockSet(const Char *s, const LockSet *ls) |
| 689 | { |
| 690 | VG_(printf)("%s: ", s); |
| 691 | pp_LockSet(ls); |
| 692 | } |
| 693 | |
| 694 | /* Compute the hash of a LockSet */ |
sewardj | 5686735 | 2003-10-12 10:27:06 +0000 | [diff] [blame] | 695 | static UInt hash_LockSet_w_wo(const LockSet *ls, |
| 696 | const Mutex *with, |
| 697 | const Mutex *without) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 698 | { |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 699 | Int i; |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 700 | UInt hash = ls->setsize + (with != NULL) - (without != NULL); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 701 | |
| 702 | sk_assert(with == NULL || with != without); |
| 703 | |
| 704 | for(i = 0; with != NULL || i < ls->setsize; i++) { |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 705 | const Mutex *mx = i >= ls->setsize ? NULL : ls->mutex[i]; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 706 | |
| 707 | if (without && mutex_cmp(without, mx) == 0) |
| 708 | continue; |
| 709 | |
| 710 | if (with && (mx == NULL || mutex_cmp(with, mx) < 0)) { |
| 711 | mx = with; |
| 712 | with = NULL; |
| 713 | i--; |
| 714 | } |
| 715 | |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 716 | hash = ROTL(hash, 17); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 717 | hash ^= (UInt)mx->mutexp; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | return hash % LOCKSET_HASH_SZ; |
| 721 | } |
| 722 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 723 | static inline UInt hash_LockSet_with(const LockSet *ls, const Mutex *with) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 724 | { |
| 725 | UInt hash = hash_LockSet_w_wo(ls, with, NULL); |
| 726 | |
| 727 | if (0) |
| 728 | VG_(printf)("hash_with %p+%p -> %d\n", ls, with->mutexp, hash); |
| 729 | |
| 730 | return hash; |
| 731 | } |
| 732 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 733 | static inline UInt hash_LockSet_without(const LockSet *ls, const Mutex *without) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 734 | { |
| 735 | UInt hash = hash_LockSet_w_wo(ls, NULL, without); |
| 736 | |
| 737 | if (0) |
| 738 | VG_(printf)("hash_with %p-%p -> %d\n", ls, without->mutexp, hash); |
| 739 | |
| 740 | return hash; |
| 741 | } |
| 742 | |
| 743 | static inline UInt hash_LockSet(const LockSet *ls) |
| 744 | { |
| 745 | UInt hash = hash_LockSet_w_wo(ls, NULL, NULL); |
| 746 | |
| 747 | if (0) |
| 748 | VG_(printf)("hash %p -> %d\n", ls, hash); |
| 749 | |
| 750 | return hash; |
| 751 | } |
| 752 | |
| 753 | static |
| 754 | Bool structural_eq_LockSet(const LockSet* a, const LockSet* b) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 755 | { |
| 756 | Int i; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 757 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 758 | if (a == b) |
| 759 | return True; |
| 760 | if (a->setsize != b->setsize) |
| 761 | return False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 762 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 763 | for(i = 0; i < a->setsize; i++) { |
| 764 | if (mutex_cmp(a->mutex[i], b->mutex[i]) != 0) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 765 | return False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 766 | } |
| 767 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 768 | return True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | |
| 772 | /* Tricky: equivalent to (compare(insert(missing_elem, a), b)), but |
| 773 | * doesn't do the insertion. Returns True if they match. |
| 774 | */ |
| 775 | static Bool |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 776 | weird_LockSet_equals(const LockSet* a, const LockSet* b, |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 777 | const Mutex *missing_mutex) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 778 | { |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 779 | static const Bool debug = False; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 780 | Int ia, ib; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 781 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 782 | /* Idea is to try and match each element of b against either an |
| 783 | element of a, or missing_mutex. */ |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 784 | |
| 785 | if (debug) { |
| 786 | print_LockSet("weird_LockSet_equals a", a); |
| 787 | print_LockSet(" b", b); |
| 788 | VG_(printf)( " missing: %p%(y\n", |
| 789 | missing_mutex->mutexp, missing_mutex->mutexp); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 790 | } |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 791 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 792 | if ((a->setsize + 1) != b->setsize) { |
| 793 | if (debug) |
| 794 | VG_(printf)(" fastpath length mismatch -> 0\n"); |
| 795 | return False; |
| 796 | } |
| 797 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 798 | /* There are three phases to this compare: |
| 799 | 1 the section from the start of a up to missing_mutex |
| 800 | 2 missing mutex itself |
| 801 | 3 the section after missing_mutex to the end of a |
| 802 | */ |
| 803 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 804 | ia = 0; |
| 805 | ib = 0; |
| 806 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 807 | /* 1: up to missing_mutex */ |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 808 | for(; ia < a->setsize && mutex_cmp(a->mutex[ia], missing_mutex) < 0; ia++, ib++) { |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 809 | if (debug) { |
| 810 | print_LockSet(" 1:a", a); |
| 811 | print_LockSet(" 1:b", b); |
| 812 | } |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 813 | if (ib == b->setsize || mutex_cmp(a->mutex[ia], b->mutex[ib]) != 0) |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 814 | return False; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /* 2: missing_mutex itself */ |
| 818 | if (debug) { |
| 819 | VG_(printf)( " 2:missing: %p%(y\n", |
| 820 | missing_mutex->mutexp, missing_mutex->mutexp); |
| 821 | print_LockSet(" 2: b", b); |
| 822 | } |
| 823 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 824 | sk_assert(ia == a->setsize || mutex_cmp(a->mutex[ia], missing_mutex) >= 0); |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 825 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 826 | if (ib == b->setsize || mutex_cmp(missing_mutex, b->mutex[ib]) != 0) |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 827 | return False; |
| 828 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 829 | ib++; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 830 | |
| 831 | /* 3: after missing_mutex to end */ |
| 832 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 833 | for(; ia < a->setsize && ib < b->setsize; ia++, ib++) { |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 834 | if (debug) { |
| 835 | print_LockSet(" 3:a", a); |
| 836 | print_LockSet(" 3:b", b); |
| 837 | } |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 838 | if (mutex_cmp(a->mutex[ia], b->mutex[ib]) != 0) |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 839 | return False; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | if (debug) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 843 | VG_(printf)(" ia=%d ib=%d --> %d\n", ia, ib, ia == a->setsize && ib == b->setsize); |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 844 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 845 | return ia == a->setsize && ib == b->setsize; |
| 846 | } |
| 847 | |
| 848 | |
| 849 | |
| 850 | static const LockSet *lookup_LockSet(const LockSet *set) |
| 851 | { |
| 852 | UInt bucket = set->hash; |
| 853 | LockSet *ret; |
| 854 | |
| 855 | for(ret = lockset_hash[bucket]; ret != NULL; ret = ret->next) |
| 856 | if (set == ret || structural_eq_LockSet(set, ret)) |
| 857 | return ret; |
| 858 | |
| 859 | return NULL; |
| 860 | } |
| 861 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 862 | static const LockSet *lookup_LockSet_with(const LockSet *set, Mutex *mutex) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 863 | { |
| 864 | UInt bucket = hash_LockSet_with(set, mutex); |
| 865 | const LockSet *ret; |
| 866 | |
| 867 | for(ret = lockset_hash[bucket]; ret != NULL; ret = ret->next) |
| 868 | if (weird_LockSet_equals(set, ret, mutex)) |
| 869 | return ret; |
| 870 | |
| 871 | return NULL; |
| 872 | } |
| 873 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 874 | static const LockSet *lookup_LockSet_without(const LockSet *set, Mutex *mutex) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 875 | { |
| 876 | UInt bucket = hash_LockSet_without(set, mutex); |
| 877 | const LockSet *ret; |
| 878 | |
| 879 | for(ret = lockset_hash[bucket]; ret != NULL; ret = ret->next) |
| 880 | if (weird_LockSet_equals(ret, set, mutex)) |
| 881 | return ret; |
| 882 | |
| 883 | return NULL; |
| 884 | } |
| 885 | |
| 886 | static void insert_LockSet(LockSet *set) |
| 887 | { |
| 888 | UInt hash = hash_LockSet(set); |
| 889 | |
| 890 | set->hash = hash; |
| 891 | |
| 892 | sk_assert(lookup_LockSet(set) == NULL); |
| 893 | |
| 894 | set->next = lockset_hash[hash]; |
| 895 | lockset_hash[hash] = set; |
| 896 | } |
| 897 | |
| 898 | static inline |
| 899 | LockSet *alloc_LockSet(UInt setsize) |
| 900 | { |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 901 | LockSet *ret = VG_(malloc)(sizeof(*ret) + sizeof(Mutex *) * setsize); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 902 | ret->setsize = setsize; |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | static inline |
| 907 | void free_LockSet(LockSet *p) |
| 908 | { |
| 909 | /* assert: not present in hash */ |
| 910 | VG_(free)(p); |
| 911 | } |
| 912 | |
njn | b4aee05 | 2003-04-15 14:09:58 +0000 | [diff] [blame] | 913 | static |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 914 | void pp_all_LockSets ( void ) |
| 915 | { |
| 916 | Int i; |
| 917 | Int sets, buckets; |
| 918 | |
| 919 | sets = buckets = 0; |
| 920 | for (i = 0; i < LOCKSET_HASH_SZ; i++) { |
| 921 | const LockSet *ls = lockset_hash[i]; |
| 922 | Bool first = True; |
| 923 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 924 | for(; ls != NULL; ls = ls->next) { |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 925 | if (first) { |
| 926 | buckets++; |
| 927 | VG_(printf)("[%4d] = ", i); |
| 928 | } else |
| 929 | VG_(printf)(" "); |
| 930 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 931 | sets++; |
| 932 | first = False; |
| 933 | pp_LockSet(ls); |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | VG_(printf)("%d distinct LockSets in %d buckets\n", sets, buckets); |
| 938 | } |
| 939 | |
| 940 | static inline Bool isempty(const LockSet *ls) |
| 941 | { |
| 942 | return ls == NULL || ls->setsize == 0; |
| 943 | } |
| 944 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 945 | static Bool ismember(const LockSet *ls, const Mutex *mx) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 946 | { |
| 947 | Int i; |
| 948 | |
| 949 | /* XXX use binary search */ |
| 950 | for(i = 0; i < ls->setsize; i++) |
| 951 | if (mutex_cmp(mx, ls->mutex[i]) == 0) |
| 952 | return True; |
| 953 | |
| 954 | return False; |
| 955 | } |
| 956 | |
| 957 | /* Check invariants: |
| 958 | - all locksets are unique |
| 959 | - each set is an array in strictly increasing order of mutex addr |
| 960 | */ |
| 961 | static |
| 962 | void sanity_check_locksets ( const Char* caller ) |
| 963 | { |
| 964 | Int i; |
| 965 | const Char *badness; |
| 966 | LockSet *ls; |
| 967 | |
| 968 | for(i = 0; i < LOCKSET_HASH_SZ; i++) { |
| 969 | |
| 970 | for(ls = lockset_hash[i]; ls != NULL; ls = ls->next) { |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 971 | const Mutex *prev; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 972 | Int j; |
| 973 | |
| 974 | if (hash_LockSet(ls) != ls->hash) { |
| 975 | badness = "mismatched hash"; |
| 976 | goto bad; |
| 977 | } |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 978 | if (ls->hash != (UInt)i) { |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 979 | badness = "wrong bucket"; |
| 980 | goto bad; |
| 981 | } |
| 982 | if (lookup_LockSet(ls) != ls) { |
| 983 | badness = "non-unique set"; |
| 984 | goto bad; |
| 985 | } |
| 986 | |
| 987 | prev = ls->mutex[0]; |
| 988 | for(j = 1; j < ls->setsize; j++) { |
| 989 | if (mutex_cmp(prev, ls->mutex[j]) >= 0) { |
| 990 | badness = "mutexes out of order"; |
| 991 | goto bad; |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | return; |
| 997 | |
| 998 | bad: |
| 999 | VG_(printf)("sanity_check_locksets: " |
| 1000 | "i = %d, ls=%p badness = %s, caller = %s\n", |
| 1001 | i, ls, badness, caller); |
| 1002 | pp_all_LockSets(); |
| 1003 | VG_(skin_panic)("sanity_check_locksets"); |
| 1004 | } |
| 1005 | |
| 1006 | static |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1007 | LockSet *add_LockSet(const LockSet *ls, const Mutex *mx) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1008 | { |
| 1009 | static const Bool debug = False; |
| 1010 | LockSet *ret = NULL; |
| 1011 | Int i, j; |
| 1012 | |
| 1013 | if (debug || DEBUG_MEM_LOCKSET_CHANGES) { |
| 1014 | VG_(printf)("add-IN mutex %p%(y\n", mx->mutexp, mx->mutexp); |
| 1015 | print_LockSet("add-IN", ls); |
| 1016 | } |
| 1017 | |
| 1018 | if (debug || LOCKSET_SANITY) |
| 1019 | sanity_check_locksets("add-IN"); |
| 1020 | |
| 1021 | sk_assert(!ismember(ls, mx)); |
| 1022 | |
| 1023 | ret = alloc_LockSet(ls->setsize+1); |
| 1024 | |
| 1025 | for(i = j = 0; i < ls->setsize; i++) { |
| 1026 | if (debug) |
| 1027 | VG_(printf)("i=%d j=%d ls->mutex[i]=%p mx=%p\n", |
| 1028 | i, j, ls->mutex[i]->mutexp, mx ? mx->mutexp : 0); |
| 1029 | if (mx && mutex_cmp(mx, ls->mutex[i]) < 0) { |
| 1030 | ret->mutex[j++] = mx; |
| 1031 | mx = NULL; |
| 1032 | } |
| 1033 | ret->mutex[j++] = ls->mutex[i]; |
| 1034 | } |
| 1035 | |
| 1036 | /* not added in loop - must be after */ |
| 1037 | if (mx) |
| 1038 | ret->mutex[j++] = mx; |
| 1039 | |
| 1040 | sk_assert(j == ret->setsize); |
| 1041 | |
| 1042 | if (debug || LOCKSET_SANITY) { |
| 1043 | print_LockSet("add-OUT", ret); |
| 1044 | sanity_check_locksets("add-OUT"); |
| 1045 | } |
| 1046 | return ret; |
| 1047 | } |
| 1048 | |
| 1049 | /* Builds ls with mx removed. mx should actually be in ls! |
| 1050 | (a checked assertion). Resulting set should not already |
| 1051 | exist in the table (unchecked). |
| 1052 | */ |
| 1053 | static |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1054 | LockSet *remove_LockSet ( const LockSet *ls, const Mutex *mx ) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1055 | { |
| 1056 | static const Bool debug = False; |
| 1057 | LockSet *ret = NULL; |
| 1058 | Int i, j; |
| 1059 | |
| 1060 | if (debug || DEBUG_MEM_LOCKSET_CHANGES) { |
| 1061 | print_LockSet("remove-IN", ls); |
| 1062 | } |
| 1063 | |
| 1064 | if (debug || LOCKSET_SANITY) |
| 1065 | sanity_check_locksets("remove-IN"); |
| 1066 | |
| 1067 | sk_assert(ismember(ls, mx)); |
| 1068 | |
| 1069 | ret = alloc_LockSet(ls->setsize-1); |
| 1070 | |
| 1071 | for(i = j = 0; i < ls->setsize; i++) { |
| 1072 | if (mutex_cmp(ls->mutex[i], mx) == 0) |
| 1073 | continue; |
| 1074 | ret->mutex[j++] = ls->mutex[i]; |
| 1075 | } |
| 1076 | |
| 1077 | sk_assert(j == ret->setsize); |
| 1078 | |
| 1079 | if (debug || LOCKSET_SANITY) { |
| 1080 | print_LockSet("remove-OUT", ret); |
| 1081 | sanity_check_locksets("remove-OUT"); |
| 1082 | } |
| 1083 | return ret; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | /* Builds the intersection, and then unbuilds it if it's already in the table. |
| 1088 | */ |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1089 | static const LockSet *_intersect(const LockSet *a, const LockSet *b) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1090 | { |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1091 | static const Bool debug = False; |
| 1092 | Int iret; |
| 1093 | Int ia, ib; |
| 1094 | Int size; |
| 1095 | LockSet *ret; |
| 1096 | const LockSet *found; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1097 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1098 | if (debug || LOCKSET_SANITY) |
| 1099 | sanity_check_locksets("intersect-IN"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1100 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1101 | if (debug || DEBUG_MEM_LOCKSET_CHANGES) { |
| 1102 | print_LockSet("intersect a", a); |
| 1103 | print_LockSet("intersect b", b); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1106 | /* count the size of the new set */ |
| 1107 | size = 0; |
| 1108 | ia = ib = 0; |
| 1109 | for(size = ia = ib = 0; ia < a->setsize && ib < b->setsize; ) { |
| 1110 | if (mutex_cmp(a->mutex[ia], b->mutex[ib]) == 0) { |
| 1111 | size++; |
| 1112 | ia++; |
| 1113 | ib++; |
| 1114 | } else if (mutex_cmp(a->mutex[ia], b->mutex[ib]) < 0) { |
| 1115 | ia++; |
| 1116 | } else { |
| 1117 | sk_assert(mutex_cmp(a->mutex[ia], b->mutex[ib]) > 0); |
| 1118 | ib++; |
| 1119 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1122 | /* Build the intersection of the two sets */ |
| 1123 | ret = alloc_LockSet(size); |
| 1124 | for (iret = ia = ib = 0; ia < a->setsize && ib < b->setsize; ) { |
| 1125 | if (mutex_cmp(a->mutex[ia], b->mutex[ib]) == 0) { |
| 1126 | sk_assert(iret < ret->setsize); |
| 1127 | ret->mutex[iret++] = a->mutex[ia]; |
| 1128 | ia++; |
| 1129 | ib++; |
| 1130 | } else if (mutex_cmp(a->mutex[ia], b->mutex[ib]) < 0) { |
| 1131 | ia++; |
| 1132 | } else { |
| 1133 | sk_assert(mutex_cmp(a->mutex[ia], b->mutex[ib]) > 0); |
| 1134 | ib++; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | ret->hash = hash_LockSet(ret); |
| 1139 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1140 | /* Now search for it in the table, adding it if not seen before */ |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1141 | found = lookup_LockSet(ret); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1142 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1143 | if (found != NULL) { |
| 1144 | free_LockSet(ret); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1145 | } else { |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1146 | insert_LockSet(ret); |
| 1147 | found = ret; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1150 | if (debug || LOCKSET_SANITY) { |
| 1151 | print_LockSet("intersect-OUT", found); |
| 1152 | sanity_check_locksets("intersect-OUT"); |
| 1153 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1154 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1155 | return found; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1158 | /* inline the fastpath */ |
| 1159 | static inline const LockSet *intersect(const LockSet *a, const LockSet *b) |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1160 | { |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1161 | static const Bool debug = False; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1162 | |
| 1163 | /* Fast case -- when the two are the same */ |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1164 | if (a == b) { |
| 1165 | if (debug || DEBUG_MEM_LOCKSET_CHANGES) { |
| 1166 | print_LockSet("intersect-same fastpath", a); |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1167 | } |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1168 | return a; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1171 | if (isempty(a) || isempty(b)) { |
| 1172 | if (debug) |
| 1173 | VG_(printf)("intersect empty fastpath\n"); |
| 1174 | return emptyset; |
| 1175 | } |
| 1176 | |
| 1177 | return _intersect(a, b); |
| 1178 | } |
| 1179 | |
| 1180 | |
| 1181 | static const LockSet *ls_union(const LockSet *a, const LockSet *b) |
| 1182 | { |
| 1183 | static const Bool debug = False; |
| 1184 | Int iret; |
| 1185 | Int ia, ib; |
| 1186 | Int size; |
| 1187 | LockSet *ret; |
| 1188 | const LockSet *found; |
| 1189 | |
| 1190 | if (debug || LOCKSET_SANITY) |
| 1191 | sanity_check_locksets("union-IN"); |
| 1192 | |
| 1193 | /* Fast case -- when the two are the same */ |
| 1194 | if (a == b) { |
| 1195 | if (debug || DEBUG_MEM_LOCKSET_CHANGES) { |
| 1196 | print_LockSet("union-same fastpath", a); |
| 1197 | } |
| 1198 | return a; |
| 1199 | } |
| 1200 | |
| 1201 | if (isempty(a)) { |
| 1202 | if (debug) |
| 1203 | print_LockSet("union a=empty b", b); |
| 1204 | return b; |
| 1205 | } |
| 1206 | if (isempty(b)) { |
| 1207 | if (debug) |
| 1208 | print_LockSet("union b=empty a", a); |
| 1209 | return a; |
| 1210 | } |
| 1211 | |
| 1212 | if (debug || DEBUG_MEM_LOCKSET_CHANGES) { |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1213 | print_LockSet("union a", a); |
| 1214 | print_LockSet("union b", b); |
| 1215 | } |
| 1216 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1217 | /* count the size of the new set */ |
| 1218 | for(size = ia = ib = 0; (ia < a->setsize) || (ib < b->setsize); ) { |
| 1219 | Int cmp; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1220 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1221 | if ((ia < a->setsize) && (ib < b->setsize)) |
| 1222 | cmp = mutex_cmp(a->mutex[ia], b->mutex[ib]); |
| 1223 | else if (ia == a->setsize) |
| 1224 | cmp = 1; |
| 1225 | else |
| 1226 | cmp = -1; |
| 1227 | |
| 1228 | if (cmp == 0) { |
| 1229 | size++; |
| 1230 | ia++; |
| 1231 | ib++; |
| 1232 | } else if (cmp < 0) { |
| 1233 | size++; |
| 1234 | ia++; |
| 1235 | } else { |
| 1236 | sk_assert(cmp > 0); |
| 1237 | size++; |
| 1238 | ib++; |
| 1239 | } |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1242 | /* Build the intersection of the two sets */ |
| 1243 | ret = alloc_LockSet(size); |
| 1244 | for (iret = ia = ib = 0; (ia < a->setsize) || (ib < b->setsize); ) { |
| 1245 | Int cmp; |
| 1246 | sk_assert(iret < ret->setsize); |
| 1247 | |
| 1248 | if ((ia < a->setsize) && (ib < b->setsize)) |
| 1249 | cmp = mutex_cmp(a->mutex[ia], b->mutex[ib]); |
| 1250 | else if (ia == a->setsize) |
| 1251 | cmp = 1; |
| 1252 | else |
| 1253 | cmp = -1; |
| 1254 | |
| 1255 | if (cmp == 0) { |
| 1256 | ret->mutex[iret++] = a->mutex[ia]; |
| 1257 | ia++; |
| 1258 | ib++; |
| 1259 | } else if (cmp < 0) { |
| 1260 | ret->mutex[iret++] = a->mutex[ia]; |
| 1261 | ia++; |
| 1262 | } else { |
| 1263 | sk_assert(cmp > 0); |
| 1264 | ret->mutex[iret++] = b->mutex[ib]; |
| 1265 | ib++; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | sk_assert(iret == ret->setsize); |
| 1270 | |
| 1271 | ret->hash = hash_LockSet(ret); |
| 1272 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1273 | /* Now search for it in the table, adding it if not seen before */ |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1274 | found = lookup_LockSet(ret); |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1275 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1276 | if (found != NULL) { |
| 1277 | if (debug) |
| 1278 | print_LockSet("union found existing set", found); |
| 1279 | free_LockSet(ret); |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1280 | } else { |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1281 | if (debug) |
| 1282 | print_LockSet("union inserting new set", ret); |
| 1283 | insert_LockSet(ret); |
| 1284 | found = ret; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1287 | if (debug || LOCKSET_SANITY) { |
| 1288 | print_LockSet("union-OUT", found); |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1289 | sanity_check_locksets("union-OUT"); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1290 | } |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1291 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1292 | return found; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | /*------------------------------------------------------------*/ |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1296 | /*--- Implementation of mutex structure. ---*/ |
| 1297 | /*------------------------------------------------------------*/ |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1298 | |
| 1299 | static UInt graph_mark; /* current mark we're using for graph traversal */ |
| 1300 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1301 | static void record_mutex_error(ThreadId tid, Mutex *mutex, |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1302 | Char *str, ExeContext *ec); |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1303 | static void record_lockgraph_error(ThreadId tid, Mutex *mutex, |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1304 | const LockSet *lockset_holding, |
| 1305 | const LockSet *lockset_prev); |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1306 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1307 | static void set_mutex_state(Mutex *mutex, MutexState state, ThreadId tid); |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1308 | |
| 1309 | #define M_MUTEX_HASHSZ 1021 |
| 1310 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1311 | static Mutex *mutex_hash[M_MUTEX_HASHSZ]; |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1312 | static UInt total_mutexes; |
| 1313 | |
| 1314 | static const Char *pp_MutexState(MutexState st) |
| 1315 | { |
| 1316 | switch(st) { |
| 1317 | case MxLocked: return "Locked"; |
| 1318 | case MxUnlocked: return "Unlocked"; |
| 1319 | case MxDead: return "Dead"; |
| 1320 | case MxUnknown: return "Unknown"; |
| 1321 | } |
| 1322 | return "???"; |
| 1323 | } |
| 1324 | |
| 1325 | static void pp_all_mutexes() |
| 1326 | { |
| 1327 | Int i; |
| 1328 | Int locks, buckets; |
| 1329 | |
| 1330 | locks = buckets = 0; |
| 1331 | for(i = 0; i < M_MUTEX_HASHSZ; i++) { |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1332 | Mutex *mx; |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1333 | Bool first = True; |
| 1334 | |
| 1335 | for(mx = mutex_hash[i]; mx != NULL; mx = mx->next) { |
| 1336 | if (first) { |
| 1337 | buckets++; |
| 1338 | VG_(printf)("[%4d] = ", i); |
| 1339 | } else |
| 1340 | VG_(printf)(" "); |
| 1341 | locks++; |
| 1342 | first = False; |
| 1343 | VG_(printf)("%p [%8s] -> %p%(y\n", |
| 1344 | mx, pp_MutexState(mx->state), mx->mutexp, mx->mutexp); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | VG_(printf)("%d locks in %d buckets (%d allocated)\n", |
| 1349 | locks, buckets, total_mutexes); |
| 1350 | } |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1351 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1352 | /* find or create a Mutex for a program's mutex use */ |
| 1353 | static Mutex *get_mutex(Addr mutexp) |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1354 | { |
| 1355 | UInt bucket = ((UInt)mutexp) % M_MUTEX_HASHSZ; |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1356 | Mutex *mp; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1357 | |
| 1358 | for(mp = mutex_hash[bucket]; mp != NULL; mp = mp->next) |
| 1359 | if (mp->mutexp == mutexp) |
| 1360 | return mp; |
| 1361 | |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1362 | total_mutexes++; |
| 1363 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1364 | mp = VG_(malloc)(sizeof(*mp)); |
| 1365 | mp->mutexp = mutexp; |
| 1366 | mp->next = mutex_hash[bucket]; |
| 1367 | mutex_hash[bucket] = mp; |
| 1368 | |
| 1369 | mp->state = MxUnknown; |
| 1370 | mp->tid = VG_INVALID_THREADID; |
| 1371 | mp->location = NULL; |
| 1372 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1373 | mp->lockdep = emptyset; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1374 | mp->mark = graph_mark - 1; |
| 1375 | |
| 1376 | return mp; |
| 1377 | } |
| 1378 | |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1379 | /* Find all mutexes in a range of memory, and call the callback. |
| 1380 | Remove the mutex from the hash if the callback returns True (mutex |
| 1381 | structure itself is not freed, because it may be pointed to by a |
| 1382 | LockSet. */ |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1383 | static void find_mutex_range(Addr start, Addr end, Bool (*action)(Mutex *)) |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1384 | { |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1385 | UInt first = start % M_MUTEX_HASHSZ; |
| 1386 | UInt last = (end+1) % M_MUTEX_HASHSZ; |
| 1387 | UInt i; |
| 1388 | |
| 1389 | /* Single pass over the hash table, looking for likely hashes */ |
| 1390 | for(i = first; i != last; ) { |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1391 | Mutex *mx; |
| 1392 | Mutex **prev = &mutex_hash[i]; |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1393 | |
| 1394 | for(mx = mutex_hash[i]; mx != NULL; prev = &mx->next, mx = mx->next) { |
| 1395 | if (mx->mutexp >= start && mx->mutexp < end && (*action)(mx)) |
| 1396 | *prev = mx->next; |
| 1397 | } |
| 1398 | |
| 1399 | if (++i == M_MUTEX_HASHSZ) |
| 1400 | i = 0; |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1401 | } |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | #define MARK_LOOP (graph_mark+0) |
| 1405 | #define MARK_DONE (graph_mark+1) |
| 1406 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1407 | static Bool check_cycle(const Mutex *start, const LockSet* lockset) |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1408 | { |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1409 | Bool check_cycle_inner(const Mutex *mutex, const LockSet *ls) |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 1410 | { |
| 1411 | static const Bool debug = False; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1412 | Int i; |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (mutex->mark == MARK_LOOP) |
| 1415 | return True; /* found cycle */ |
| 1416 | if (mutex->mark == MARK_DONE) |
| 1417 | return False; /* been here before, its OK */ |
| 1418 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1419 | ((Mutex*)mutex)->mark = MARK_LOOP; |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 1420 | |
| 1421 | if (debug) |
| 1422 | VG_(printf)("mark=%d visiting %p%(y mutex->lockset=%d\n", |
| 1423 | graph_mark, mutex->mutexp, mutex->mutexp, mutex->lockdep); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1424 | for(i = 0; i < ls->setsize; i++) { |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1425 | const Mutex *mx = ls->mutex[i]; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1426 | |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 1427 | if (debug) |
| 1428 | VG_(printf)(" %y ls=%p (ls->mutex=%p%(y)\n", |
| 1429 | mutex->mutexp, ls, |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1430 | mx->mutexp, mx->mutexp); |
| 1431 | if (check_cycle_inner(mx, mx->lockdep)) |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 1432 | return True; |
| 1433 | } |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1434 | ((Mutex*)mutex)->mark = MARK_DONE; |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 1435 | |
| 1436 | return False; |
| 1437 | } |
| 1438 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1439 | graph_mark += 2; /* clear all marks */ |
| 1440 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1441 | return check_cycle_inner(start, lockset); |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 1444 | /* test to see if a mutex state change would be problematic; this |
| 1445 | makes no changes to the mutex state. This should be called before |
| 1446 | the locking thread has actually blocked. */ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1447 | static void test_mutex_state(Mutex *mutex, MutexState state, ThreadId tid) |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1448 | { |
| 1449 | static const Bool debug = False; |
| 1450 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1451 | if (mutex->state == MxDead) { |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1452 | Char *str; |
| 1453 | |
| 1454 | switch(state) { |
| 1455 | case MxLocked: str = "lock dead mutex"; break; |
| 1456 | case MxUnlocked: str = "unlock dead mutex"; break; |
| 1457 | default: str = "operate on dead mutex"; break; |
| 1458 | } |
| 1459 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1460 | /* can't do anything legal to a destroyed mutex */ |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1461 | record_mutex_error(tid, mutex, str, mutex->location); |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1462 | return; |
| 1463 | } |
| 1464 | |
| 1465 | switch(state) { |
| 1466 | case MxLocked: |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 1467 | sk_assert(!check_cycle(mutex, mutex->lockdep)); |
| 1468 | |
| 1469 | if (debug) |
| 1470 | print_LockSet("thread holding", thread_locks[tid]); |
| 1471 | |
| 1472 | if (check_cycle(mutex, thread_locks[tid])) |
| 1473 | record_lockgraph_error(tid, mutex, thread_locks[tid], mutex->lockdep); |
| 1474 | else { |
| 1475 | mutex->lockdep = ls_union(mutex->lockdep, thread_locks[tid]); |
| 1476 | |
| 1477 | if (debug) { |
| 1478 | VG_(printf)("giving mutex %p%(y lockdep = %p ", |
| 1479 | mutex->mutexp, mutex->mutexp, mutex->lockdep); |
| 1480 | print_LockSet("lockdep", mutex->lockdep); |
| 1481 | } |
| 1482 | } |
| 1483 | break; |
| 1484 | |
| 1485 | case MxUnlocked: |
| 1486 | if (debug) |
| 1487 | print_LockSet("thread holding", thread_locks[tid]); |
| 1488 | |
| 1489 | if (mutex->state != MxLocked) { |
| 1490 | record_mutex_error(tid, mutex, |
| 1491 | "unlock non-locked mutex", mutex->location); |
| 1492 | } |
| 1493 | if (mutex->tid != tid) { |
| 1494 | record_mutex_error(tid, mutex, |
| 1495 | "unlock someone else's mutex", mutex->location); |
| 1496 | } |
| 1497 | break; |
| 1498 | |
| 1499 | case MxDead: |
| 1500 | break; |
| 1501 | |
| 1502 | default: |
| 1503 | break; |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | /* Update a mutex state. Expects most error testing and reporting to |
| 1508 | have happened in test_mutex_state(). The assumption is that no |
| 1509 | client code is run by thread tid between test and set, either |
| 1510 | because it is blocked or test and set are called together |
| 1511 | atomically. |
| 1512 | |
| 1513 | Setting state to MxDead is the exception, since that can happen as |
| 1514 | a result of any thread freeing memory; in this case set_mutex_state |
| 1515 | does all the error reporting as well. |
| 1516 | */ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1517 | static void set_mutex_state(Mutex *mutex, MutexState state, ThreadId tid) |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 1518 | { |
| 1519 | static const Bool debug = False; |
| 1520 | |
| 1521 | if (debug) |
| 1522 | VG_(printf)("\ntid %d changing mutex (%p)->%p%(y state %s -> %s\n", |
| 1523 | tid, mutex, mutex->mutexp, mutex->mutexp, |
| 1524 | pp_MutexState(mutex->state), pp_MutexState(state)); |
| 1525 | |
| 1526 | if (mutex->state == MxDead) { |
| 1527 | /* can't do anything legal to a destroyed mutex */ |
| 1528 | return; |
| 1529 | } |
| 1530 | |
| 1531 | switch(state) { |
| 1532 | case MxLocked: |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1533 | if (mutex->state == MxLocked) { |
| 1534 | if (mutex->tid != tid) |
| 1535 | record_mutex_error(tid, mutex, "take lock held by someone else", |
| 1536 | mutex->location); |
| 1537 | else |
| 1538 | record_mutex_error(tid, mutex, "take lock we already hold", |
| 1539 | mutex->location); |
| 1540 | |
| 1541 | VG_(skin_panic)("core should have checked this\n"); |
| 1542 | break; |
| 1543 | } |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1544 | |
| 1545 | sk_assert(!check_cycle(mutex, mutex->lockdep)); |
| 1546 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1547 | mutex->tid = tid; |
| 1548 | break; |
| 1549 | |
| 1550 | case MxUnlocked: |
| 1551 | if (debug) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 1552 | print_LockSet("thread holding", thread_locks[tid]); |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1553 | |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 1554 | if (mutex->state != MxLocked || mutex->tid != tid) |
| 1555 | break; |
| 1556 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1557 | mutex->tid = VG_INVALID_THREADID; |
| 1558 | break; |
| 1559 | |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1560 | case MxDead: |
| 1561 | if (mutex->state == MxLocked) { |
| 1562 | /* forcably remove offending lock from thread's lockset */ |
| 1563 | sk_assert(ismember(thread_locks[mutex->tid], mutex)); |
| 1564 | thread_locks[mutex->tid] = remove_LockSet(thread_locks[mutex->tid], mutex); |
| 1565 | mutex->tid = VG_INVALID_THREADID; |
| 1566 | |
| 1567 | record_mutex_error(tid, mutex, |
| 1568 | "free locked mutex", mutex->location); |
| 1569 | } |
| 1570 | break; |
| 1571 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1572 | default: |
| 1573 | break; |
| 1574 | } |
| 1575 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1576 | mutex->location = VG_(get_ExeContext)(tid); |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 1577 | mutex->state = state; |
| 1578 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1579 | |
| 1580 | /*------------------------------------------------------------*/ |
| 1581 | /*--- Setting and checking permissions. ---*/ |
| 1582 | /*------------------------------------------------------------*/ |
| 1583 | |
| 1584 | static |
| 1585 | void set_address_range_state ( Addr a, UInt len /* in bytes */, |
| 1586 | VgeInitStatus status ) |
| 1587 | { |
sewardj | 1806d7f | 2002-10-22 05:05:49 +0000 | [diff] [blame] | 1588 | Addr end; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1589 | |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1590 | /* only clean up dead mutexes */ |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 1591 | Bool cleanmx(Mutex *mx) { |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1592 | return mx->state == MxDead; |
| 1593 | } |
| 1594 | |
| 1595 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1596 | # if DEBUG_MAKE_ACCESSES |
| 1597 | VG_(printf)("make_access: 0x%x, %u, status=%u\n", a, len, status); |
| 1598 | # endif |
| 1599 | //PROF_EVENT(30); PPP |
| 1600 | |
| 1601 | if (len == 0) |
| 1602 | return; |
| 1603 | |
| 1604 | if (len > 100 * 1000 * 1000) |
| 1605 | VG_(message)(Vg_UserMsg, |
| 1606 | "Warning: set address range state: large range %d", |
| 1607 | len); |
| 1608 | |
| 1609 | VGP_PUSHCC(VgpSARP); |
| 1610 | |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 1611 | /* Remove mutexes in recycled memory range from hash */ |
| 1612 | find_mutex_range(a, a+len, cleanmx); |
| 1613 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1614 | /* Memory block may not be aligned or a whole word multiple. In neat cases, |
| 1615 | * we have to init len/4 words (len is in bytes). In nasty cases, it's |
| 1616 | * len/4+1 words. This works out which it is by aligning the block and |
| 1617 | * seeing if the end byte is in the same word as it is for the unaligned |
| 1618 | * block; if not, it's the awkward case. */ |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 1619 | end = ROUNDUP(a + len, 4); |
| 1620 | a = ROUNDDN(a, 4); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1621 | |
| 1622 | /* Do it ... */ |
| 1623 | switch (status) { |
| 1624 | case Vge_VirginInit: |
| 1625 | for ( ; a < end; a += 4) { |
| 1626 | //PROF_EVENT(31); PPP |
| 1627 | init_virgin_sword(a); |
| 1628 | } |
| 1629 | break; |
| 1630 | |
| 1631 | case Vge_NonVirginInit: |
| 1632 | for ( ; a < end; a += 4) { |
| 1633 | //PROF_EVENT(31); PPP |
| 1634 | init_nonvirgin_sword(a); |
| 1635 | } |
| 1636 | break; |
| 1637 | |
| 1638 | case Vge_SegmentInit: |
| 1639 | for ( ; a < end; a += 4) { |
| 1640 | //PROF_EVENT(31); PPP |
| 1641 | init_magically_inited_sword(a); |
| 1642 | } |
| 1643 | break; |
sewardj | 7f3ad22 | 2002-11-13 22:11:53 +0000 | [diff] [blame] | 1644 | |
| 1645 | case Vge_Error: |
| 1646 | for ( ; a < end; a += 4) { |
| 1647 | //PROF_EVENT(31); PPP |
| 1648 | init_error_sword(a); |
| 1649 | } |
| 1650 | break; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1651 | |
| 1652 | default: |
| 1653 | VG_(printf)("init_status = %u\n", status); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1654 | VG_(skin_panic)("Unexpected Vge_InitStatus"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | /* Check that zero page and highest page have not been written to |
| 1658 | -- this could happen with buggy syscall wrappers. Today |
| 1659 | (2001-04-26) had precisely such a problem with |
| 1660 | __NR_setitimer. */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 1661 | sk_assert(SK_(cheap_sanity_check)()); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1662 | VGP_POPCC(VgpSARP); |
| 1663 | } |
| 1664 | |
| 1665 | |
| 1666 | static void make_segment_readable ( Addr a, UInt len ) |
| 1667 | { |
| 1668 | //PROF_EVENT(??); PPP |
| 1669 | set_address_range_state ( a, len, Vge_SegmentInit ); |
| 1670 | } |
| 1671 | |
| 1672 | static void make_writable ( Addr a, UInt len ) |
| 1673 | { |
| 1674 | //PROF_EVENT(36); PPP |
| 1675 | set_address_range_state( a, len, Vge_VirginInit ); |
| 1676 | } |
| 1677 | |
| 1678 | static void make_readable ( Addr a, UInt len ) |
| 1679 | { |
| 1680 | //PROF_EVENT(37); PPP |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 1681 | set_address_range_state( a, len, Vge_VirginInit ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1685 | /* Block-copy states (needed for implementing realloc()). */ |
| 1686 | static void copy_address_range_state(Addr src, Addr dst, UInt len) |
| 1687 | { |
| 1688 | UInt i; |
| 1689 | |
| 1690 | //PROF_EVENT(40); PPP |
| 1691 | for (i = 0; i < len; i += 4) { |
| 1692 | shadow_word sword = *(get_sword_addr ( src+i )); |
| 1693 | //PROF_EVENT(41); PPP |
| 1694 | set_sword ( dst+i, sword ); |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | // SSS: put these somewhere better |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1699 | static void eraser_mem_read (Addr a, UInt data_size, ThreadId tid); |
| 1700 | static void eraser_mem_write(Addr a, UInt data_size, ThreadId tid); |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 1701 | |
| 1702 | #define REGPARM(x) __attribute__((regparm (x))) |
| 1703 | |
| 1704 | static void eraser_mem_help_read_1(Addr a) REGPARM(1); |
| 1705 | static void eraser_mem_help_read_2(Addr a) REGPARM(1); |
| 1706 | static void eraser_mem_help_read_4(Addr a) REGPARM(1); |
| 1707 | static void eraser_mem_help_read_N(Addr a, UInt size) REGPARM(2); |
| 1708 | |
| 1709 | static void eraser_mem_help_write_1(Addr a, UInt val) REGPARM(2); |
| 1710 | static void eraser_mem_help_write_2(Addr a, UInt val) REGPARM(2); |
| 1711 | static void eraser_mem_help_write_4(Addr a, UInt val) REGPARM(2); |
| 1712 | static void eraser_mem_help_write_N(Addr a, UInt size) REGPARM(2); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1713 | |
sewardj | 7a5ebcf | 2002-11-13 22:42:13 +0000 | [diff] [blame] | 1714 | static void bus_lock(void); |
| 1715 | static void bus_unlock(void); |
| 1716 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1717 | static |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1718 | void eraser_pre_mem_read(CorePart part, ThreadId tid, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1719 | Char* s, UInt base, UInt size ) |
| 1720 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1721 | if (tid > 50) { VG_(printf)("pid = %d, s = `%s`, part = %d\n", tid, s, part); VG_(skin_panic)("a");} |
| 1722 | eraser_mem_read(base, size, tid); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | static |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1726 | void eraser_pre_mem_read_asciiz(CorePart part, ThreadId tid, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1727 | Char* s, UInt base ) |
| 1728 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1729 | eraser_mem_read(base, VG_(strlen)((Char*)base), tid); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | static |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1733 | void eraser_pre_mem_write(CorePart part, ThreadId tid, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1734 | Char* s, UInt base, UInt size ) |
| 1735 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1736 | eraser_mem_write(base, size, tid); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | |
| 1740 | |
| 1741 | static |
| 1742 | void eraser_new_mem_startup( Addr a, UInt len, Bool rr, Bool ww, Bool xx ) |
| 1743 | { |
njn | 1f3a909 | 2002-10-04 09:22:30 +0000 | [diff] [blame] | 1744 | /* Ignore the permissions, just make it readable. Seems to work... */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1745 | make_segment_readable(a, len); |
| 1746 | } |
| 1747 | |
| 1748 | |
| 1749 | static |
| 1750 | void eraser_new_mem_heap ( Addr a, UInt len, Bool is_inited ) |
| 1751 | { |
| 1752 | if (is_inited) { |
| 1753 | make_readable(a, len); |
| 1754 | } else { |
| 1755 | make_writable(a, len); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | static |
| 1760 | void eraser_set_perms (Addr a, UInt len, |
sewardj | 40f8ebe | 2002-10-23 21:46:13 +0000 | [diff] [blame] | 1761 | Bool rr, Bool ww, Bool xx) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1762 | { |
| 1763 | if (rr) make_readable(a, len); |
| 1764 | else if (ww) make_writable(a, len); |
| 1765 | /* else do nothing */ |
| 1766 | } |
| 1767 | |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 1768 | static |
| 1769 | void eraser_new_mem_stack_private(Addr a, UInt len) |
| 1770 | { |
| 1771 | set_address_range_state(a, len, Vge_NonVirginInit); |
| 1772 | } |
| 1773 | |
| 1774 | static |
| 1775 | void eraser_new_mem_stack(Addr a, UInt len) |
| 1776 | { |
| 1777 | set_address_range_state(a, len, Vge_VirginInit); |
| 1778 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1779 | |
| 1780 | /*--------------------------------------------------------------*/ |
| 1781 | /*--- Initialise the memory audit system on program startup. ---*/ |
| 1782 | /*--------------------------------------------------------------*/ |
| 1783 | |
| 1784 | static |
| 1785 | void init_shadow_memory(void) |
| 1786 | { |
| 1787 | Int i; |
| 1788 | |
| 1789 | for (i = 0; i < ESEC_MAP_WORDS; i++) |
| 1790 | distinguished_secondary_map.swords[i] = virgin_sword; |
| 1791 | |
| 1792 | /* These entries gradually get overwritten as the used address |
| 1793 | space expands. */ |
| 1794 | for (i = 0; i < 65536; i++) |
| 1795 | primary_map[i] = &distinguished_secondary_map; |
| 1796 | } |
| 1797 | |
| 1798 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1799 | /*------------------------------------------------------------*/ |
| 1800 | /*--- malloc() et al replacements ---*/ |
| 1801 | /*------------------------------------------------------------*/ |
| 1802 | |
njn | b4aee05 | 2003-04-15 14:09:58 +0000 | [diff] [blame] | 1803 | static VgHashTable hg_malloc_list = NULL; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1804 | |
| 1805 | #define N_FREED_CHUNKS 2 |
| 1806 | static Int freechunkptr = 0; |
| 1807 | static HG_Chunk *freechunks[N_FREED_CHUNKS]; |
| 1808 | |
| 1809 | /* Use a small redzone (paranoia) */ |
| 1810 | UInt VG_(vg_malloc_redzone_szB) = 4; |
| 1811 | |
| 1812 | |
| 1813 | /* Allocate a user-chunk of size bytes. Also allocate its shadow |
| 1814 | block, make the shadow block point at the user block. Put the |
| 1815 | shadow chunk on the appropriate list, and set all memory |
| 1816 | protections correctly. */ |
| 1817 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1818 | static void add_HG_Chunk ( ThreadId tid, Addr p, UInt size ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1819 | { |
| 1820 | HG_Chunk* hc; |
| 1821 | |
| 1822 | hc = VG_(malloc)(sizeof(HG_Chunk)); |
| 1823 | hc->data = p; |
| 1824 | hc->size = size; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1825 | hc->where = VG_(get_ExeContext)(tid); |
| 1826 | hc->tid = tid; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1827 | |
| 1828 | VG_(HT_add_node)( hg_malloc_list, (VgHashNode*)hc ); |
| 1829 | } |
| 1830 | |
| 1831 | /* Allocate memory and note change in memory available */ |
| 1832 | static __inline__ |
njn | 34ac027 | 2003-09-30 14:20:00 +0000 | [diff] [blame] | 1833 | void* alloc_and_new_mem ( Int size, UInt alignment, Bool is_zeroed ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1834 | { |
| 1835 | Addr p; |
| 1836 | |
njn | 34ac027 | 2003-09-30 14:20:00 +0000 | [diff] [blame] | 1837 | if (size < 0) return NULL; |
| 1838 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1839 | p = (Addr)VG_(cli_malloc)(alignment, size); |
njn | 34ac027 | 2003-09-30 14:20:00 +0000 | [diff] [blame] | 1840 | if (is_zeroed) VG_(memset)((void*)p, 0, size); |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1841 | add_HG_Chunk ( VG_(get_current_or_recent_tid)(), p, size ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1842 | eraser_new_mem_heap( p, size, is_zeroed ); |
| 1843 | |
| 1844 | return (void*)p; |
| 1845 | } |
| 1846 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1847 | void* SK_(malloc) ( Int n ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1848 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1849 | return alloc_and_new_mem ( n, VG_(clo_alignment), /*is_zeroed*/False ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1852 | void* SK_(__builtin_new) ( Int n ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1853 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1854 | return alloc_and_new_mem ( n, VG_(clo_alignment), /*is_zeroed*/False ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1857 | void* SK_(__builtin_vec_new) ( Int n ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1858 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1859 | return alloc_and_new_mem ( n, VG_(clo_alignment), /*is_zeroed*/False ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1862 | void* SK_(memalign) ( Int align, Int n ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1863 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1864 | return alloc_and_new_mem ( n, align, /*is_zeroed*/False ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
njn | 34ac027 | 2003-09-30 14:20:00 +0000 | [diff] [blame] | 1867 | void* SK_(calloc) ( Int nmemb, Int size ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1868 | { |
njn | 34ac027 | 2003-09-30 14:20:00 +0000 | [diff] [blame] | 1869 | return alloc_and_new_mem ( nmemb*size, VG_(clo_alignment), |
| 1870 | /*is_zeroed*/True ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | static |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1874 | void die_and_free_mem ( ThreadId tid, HG_Chunk* hc, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1875 | HG_Chunk** prev_chunks_next_ptr ) |
| 1876 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1877 | Addr start = hc->data; |
| 1878 | Addr end = start + hc->size; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1879 | |
| 1880 | Bool deadmx(Mutex *mx) { |
| 1881 | if (mx->state != MxDead) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1882 | set_mutex_state(mx, MxDead, tid); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1883 | |
| 1884 | return False; |
| 1885 | } |
| 1886 | |
| 1887 | /* Remove hc from the malloclist using prev_chunks_next_ptr to |
| 1888 | avoid repeating the hash table lookup. Can't remove until at least |
| 1889 | after free and free_mismatch errors are done because they use |
| 1890 | describe_addr() which looks for it in malloclist. */ |
| 1891 | *prev_chunks_next_ptr = hc->next; |
| 1892 | |
| 1893 | /* Record where freed */ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1894 | hc->where = VG_(get_ExeContext) ( tid ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1895 | |
| 1896 | /* maintain a small window so that the error reporting machinery |
| 1897 | knows about this memory */ |
| 1898 | if (freechunks[freechunkptr] != NULL) { |
| 1899 | /* free HG_Chunk */ |
| 1900 | HG_Chunk* sc1 = freechunks[freechunkptr]; |
| 1901 | VG_(cli_free) ( (void*)(sc1->data) ); |
| 1902 | VG_(free) ( sc1 ); |
| 1903 | } |
| 1904 | |
| 1905 | freechunks[freechunkptr] = hc; |
| 1906 | |
| 1907 | if (++freechunkptr == N_FREED_CHUNKS) |
| 1908 | freechunkptr = 0; |
| 1909 | |
| 1910 | /* mark all mutexes in range dead */ |
| 1911 | find_mutex_range(start, end, deadmx); |
| 1912 | } |
| 1913 | |
| 1914 | |
| 1915 | static __inline__ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1916 | void handle_free ( void* p ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1917 | { |
| 1918 | HG_Chunk* hc; |
| 1919 | HG_Chunk** prev_chunks_next_ptr; |
| 1920 | |
| 1921 | hc = (HG_Chunk*)VG_(HT_get_node) ( hg_malloc_list, (UInt)p, |
| 1922 | (VgHashNode***)&prev_chunks_next_ptr ); |
| 1923 | if (hc == NULL) { |
| 1924 | return; |
| 1925 | } |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1926 | die_and_free_mem ( VG_(get_current_or_recent_tid)(), |
| 1927 | hc, prev_chunks_next_ptr ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1930 | void SK_(free) ( void* p ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1931 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1932 | handle_free(p); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1935 | void SK_(__builtin_delete) ( void* p ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1936 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1937 | handle_free(p); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1940 | void SK_(__builtin_vec_delete) ( void* p ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1941 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1942 | handle_free(p); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1945 | void* SK_(realloc) ( void* p, Int new_size ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1946 | { |
| 1947 | HG_Chunk *hc; |
| 1948 | HG_Chunk **prev_chunks_next_ptr; |
sewardj | 05bcdcb | 2003-05-18 10:05:38 +0000 | [diff] [blame] | 1949 | Int i; |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1950 | ThreadId tid = VG_(get_current_or_recent_tid)(); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1951 | |
| 1952 | /* First try and find the block. */ |
| 1953 | hc = (HG_Chunk*)VG_(HT_get_node) ( hg_malloc_list, (UInt)p, |
| 1954 | (VgHashNode***)&prev_chunks_next_ptr ); |
| 1955 | |
| 1956 | if (hc == NULL) { |
| 1957 | return NULL; |
| 1958 | } |
| 1959 | |
| 1960 | if (hc->size == new_size) { |
| 1961 | /* size unchanged */ |
njn | 398044f | 2003-07-24 17:39:59 +0000 | [diff] [blame] | 1962 | hc->where = VG_(get_ExeContext)(tid); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1963 | return p; |
| 1964 | |
| 1965 | } else if (hc->size > new_size) { |
| 1966 | /* new size is smaller */ |
| 1967 | hc->size = new_size; |
njn | 398044f | 2003-07-24 17:39:59 +0000 | [diff] [blame] | 1968 | hc->where = VG_(get_ExeContext)(tid); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1969 | return p; |
| 1970 | |
| 1971 | } else { |
| 1972 | /* new size is bigger */ |
| 1973 | Addr p_new; |
| 1974 | |
| 1975 | /* Get new memory */ |
| 1976 | p_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size); |
| 1977 | |
| 1978 | /* First half kept and copied, second half new */ |
| 1979 | copy_address_range_state( (Addr)p, p_new, hc->size ); |
| 1980 | eraser_new_mem_heap ( p_new+hc->size, new_size-hc->size, |
| 1981 | /*inited*/False ); |
| 1982 | |
| 1983 | /* Copy from old to new */ |
| 1984 | for (i = 0; i < hc->size; i++) |
| 1985 | ((UChar*)p_new)[i] = ((UChar*)p)[i]; |
| 1986 | |
| 1987 | /* Free old memory */ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1988 | die_and_free_mem ( tid, hc, prev_chunks_next_ptr ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1989 | |
| 1990 | /* this has to be after die_and_free_mem, otherwise the |
| 1991 | former succeeds in shorting out the new block, not the |
| 1992 | old, in the case when both are on the same list. */ |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 1993 | add_HG_Chunk ( tid, p_new, new_size ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1994 | |
| 1995 | return (void*)p_new; |
| 1996 | } |
| 1997 | } |
| 1998 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1999 | /*--------------------------------------------------------------*/ |
| 2000 | /*--- Machinery to support sanity checking ---*/ |
| 2001 | /*--------------------------------------------------------------*/ |
| 2002 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2003 | Bool SK_(cheap_sanity_check) ( void ) |
| 2004 | { |
jseward | 9800fd3 | 2004-01-04 23:08:04 +0000 | [diff] [blame] | 2005 | /* nothing useful we can rapidly check */ |
| 2006 | return True; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2009 | Bool SK_(expensive_sanity_check)(void) |
| 2010 | { |
| 2011 | Int i; |
| 2012 | |
| 2013 | /* Make sure nobody changed the distinguished secondary. */ |
| 2014 | for (i = 0; i < ESEC_MAP_WORDS; i++) |
| 2015 | if (distinguished_secondary_map.swords[i].other != virgin_sword.other || |
| 2016 | distinguished_secondary_map.swords[i].state != virgin_sword.state) |
| 2017 | return False; |
| 2018 | |
| 2019 | return True; |
| 2020 | } |
| 2021 | |
| 2022 | |
| 2023 | /*--------------------------------------------------------------*/ |
| 2024 | /*--- Instrumentation ---*/ |
| 2025 | /*--------------------------------------------------------------*/ |
| 2026 | |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2027 | static UInt stk_ld, nonstk_ld, stk_st, nonstk_st; |
| 2028 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2029 | /* Create and return an instrumented version of cb_in. Free cb_in |
| 2030 | before returning. */ |
| 2031 | UCodeBlock* SK_(instrument) ( UCodeBlock* cb_in, Addr not_used ) |
| 2032 | { |
| 2033 | UCodeBlock* cb; |
| 2034 | Int i; |
| 2035 | UInstr* u_in; |
| 2036 | Int t_size = INVALID_TEMPREG; |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2037 | Int ntemps; |
| 2038 | Bool *stackref = NULL; |
sewardj | 7a5ebcf | 2002-11-13 22:42:13 +0000 | [diff] [blame] | 2039 | Bool locked = False; /* lock prefix */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2040 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2041 | cb = VG_(setup_UCodeBlock)(cb_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2042 | |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2043 | /* stackref[] is used for super-simple value tracking to keep note |
| 2044 | of which tempregs currently hold a value which is derived from |
| 2045 | ESP or EBP, and is therefore likely stack-relative if used as |
| 2046 | the address for LOAD or STORE. */ |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2047 | ntemps = VG_(get_num_temps)(cb); |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2048 | stackref = VG_(malloc)(sizeof(*stackref) * ntemps); |
| 2049 | VG_(memset)(stackref, 0, sizeof(*stackref) * ntemps); |
| 2050 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2051 | for (i = 0; i < VG_(get_num_instrs)(cb_in); i++) { |
| 2052 | u_in = VG_(get_instr)(cb_in, i); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2053 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2054 | switch (u_in->opcode) { |
| 2055 | |
| 2056 | case NOP: case CALLM_S: case CALLM_E: |
| 2057 | break; |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2058 | |
sewardj | 7a5ebcf | 2002-11-13 22:42:13 +0000 | [diff] [blame] | 2059 | case LOCK: |
| 2060 | locked = True; |
| 2061 | uInstr0(cb, CCALL, 0); |
| 2062 | uCCall(cb, (Addr)bus_lock, 0, 0, False); |
| 2063 | break; |
| 2064 | |
| 2065 | case JMP: case INCEIP: |
| 2066 | if (locked) { |
| 2067 | uInstr0(cb, CCALL, 0); |
| 2068 | uCCall(cb, (Addr)bus_unlock, 0, 0, False); |
| 2069 | } |
| 2070 | locked = False; |
| 2071 | VG_(copy_UInstr)(cb, u_in); |
| 2072 | break; |
| 2073 | |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2074 | case GET: |
| 2075 | sk_assert(u_in->tag1 == ArchReg); |
| 2076 | sk_assert(u_in->tag2 == TempReg); |
| 2077 | sk_assert(u_in->val2 < ntemps); |
| 2078 | |
| 2079 | stackref[u_in->val2] = (u_in->size == 4 && |
| 2080 | (u_in->val1 == R_ESP || u_in->val1 == R_EBP)); |
| 2081 | VG_(copy_UInstr)(cb, u_in); |
| 2082 | break; |
| 2083 | |
| 2084 | case MOV: |
| 2085 | if (u_in->size == 4 && u_in->tag1 == TempReg) { |
| 2086 | sk_assert(u_in->tag2 == TempReg); |
| 2087 | stackref[u_in->val2] = stackref[u_in->val1]; |
| 2088 | } |
| 2089 | VG_(copy_UInstr)(cb, u_in); |
| 2090 | break; |
| 2091 | |
| 2092 | case LEA1: |
| 2093 | case ADD: case SUB: |
| 2094 | if (u_in->size == 4 && u_in->tag1 == TempReg) { |
| 2095 | sk_assert(u_in->tag2 == TempReg); |
| 2096 | stackref[u_in->val2] |= stackref[u_in->val1]; |
| 2097 | } |
| 2098 | VG_(copy_UInstr)(cb, u_in); |
| 2099 | break; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2100 | |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 2101 | case LOAD: { |
| 2102 | void (*help)(Addr); |
| 2103 | sk_assert(1 == u_in->size || 2 == u_in->size || 4 == u_in->size); |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2104 | sk_assert(u_in->tag1 == TempReg); |
| 2105 | |
| 2106 | if (!clo_priv_stacks || !stackref[u_in->val1]) { |
| 2107 | nonstk_ld++; |
| 2108 | |
| 2109 | switch(u_in->size) { |
| 2110 | case 1: help = eraser_mem_help_read_1; break; |
| 2111 | case 2: help = eraser_mem_help_read_2; break; |
| 2112 | case 4: help = eraser_mem_help_read_4; break; |
| 2113 | default: |
| 2114 | VG_(skin_panic)("bad size"); |
| 2115 | } |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2116 | |
| 2117 | /* XXX all registers should be flushed to baseblock |
| 2118 | here */ |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2119 | uInstr1(cb, CCALL, 0, TempReg, u_in->val1); |
| 2120 | uCCall(cb, (Addr)help, 1, 1, False); |
| 2121 | } else |
| 2122 | stk_ld++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2123 | |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 2124 | VG_(copy_UInstr)(cb, u_in); |
| 2125 | t_size = INVALID_TEMPREG; |
| 2126 | break; |
| 2127 | } |
| 2128 | |
| 2129 | case FPU_R: { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 2130 | sk_assert(1 == u_in->size || 2 == u_in->size || 4 == u_in->size || |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2131 | 8 == u_in->size || 10 == u_in->size); |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 2132 | |
| 2133 | t_size = newTemp(cb); |
| 2134 | uInstr2(cb, MOV, 4, Literal, 0, TempReg, t_size); |
| 2135 | uLiteral(cb, (UInt)u_in->size); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2136 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2137 | /* XXX all registers should be flushed to baseblock |
| 2138 | here */ |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 2139 | uInstr2(cb, CCALL, 0, TempReg, u_in->val2, TempReg, t_size); |
| 2140 | uCCall(cb, (Addr) & eraser_mem_help_read_N, 2, 2, False); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2141 | |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 2142 | VG_(copy_UInstr)(cb, u_in); |
| 2143 | t_size = INVALID_TEMPREG; |
| 2144 | break; |
| 2145 | } |
| 2146 | |
| 2147 | case STORE: { |
| 2148 | void (*help)(Addr, UInt); |
| 2149 | sk_assert(1 == u_in->size || 2 == u_in->size || 4 == u_in->size); |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2150 | sk_assert(u_in->tag2 == TempReg); |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 2151 | |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2152 | if (!clo_priv_stacks || !stackref[u_in->val2]) { |
| 2153 | nonstk_st++; |
| 2154 | |
| 2155 | switch(u_in->size) { |
| 2156 | case 1: help = eraser_mem_help_write_1; break; |
| 2157 | case 2: help = eraser_mem_help_write_2; break; |
| 2158 | case 4: help = eraser_mem_help_write_4; break; |
| 2159 | default: |
| 2160 | VG_(skin_panic)("bad size"); |
| 2161 | } |
| 2162 | |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2163 | /* XXX all registers should be flushed to baseblock |
| 2164 | here */ |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2165 | uInstr2(cb, CCALL, 0, TempReg, u_in->val2, TempReg, u_in->val1); |
| 2166 | uCCall(cb, (Addr)help, 2, 2, False); |
| 2167 | } else |
| 2168 | stk_st++; |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 2169 | |
| 2170 | VG_(copy_UInstr)(cb, u_in); |
| 2171 | t_size = INVALID_TEMPREG; |
| 2172 | break; |
| 2173 | } |
| 2174 | |
| 2175 | case FPU_W: { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame] | 2176 | sk_assert(1 == u_in->size || 2 == u_in->size || 4 == u_in->size || |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2177 | 8 == u_in->size || 10 == u_in->size); |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 2178 | |
| 2179 | t_size = newTemp(cb); |
| 2180 | uInstr2(cb, MOV, 4, Literal, 0, TempReg, t_size); |
| 2181 | uLiteral(cb, (UInt)u_in->size); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2182 | /* XXX all registers should be flushed to baseblock |
| 2183 | here */ |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 2184 | uInstr2(cb, CCALL, 0, TempReg, u_in->val2, TempReg, t_size); |
| 2185 | uCCall(cb, (Addr) & eraser_mem_help_write_N, 2, 2, False); |
| 2186 | |
| 2187 | VG_(copy_UInstr)(cb, u_in); |
| 2188 | t_size = INVALID_TEMPREG; |
| 2189 | break; |
| 2190 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2191 | |
sewardj | 3d7c9c8 | 2003-03-26 21:08:13 +0000 | [diff] [blame] | 2192 | case MMX1: case MMX2: case MMX3: |
| 2193 | case MMX2_MemRd: case MMX2_MemWr: |
sewardj | 4fbe6e9 | 2003-06-15 21:54:34 +0000 | [diff] [blame] | 2194 | case MMX2_ERegRd: case MMX2_ERegWr: |
sewardj | 3d7c9c8 | 2003-03-26 21:08:13 +0000 | [diff] [blame] | 2195 | VG_(skin_panic)( |
| 2196 | "I don't know how to instrument MMXish stuff (yet)"); |
| 2197 | break; |
| 2198 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2199 | default: |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2200 | /* conservative tromping */ |
| 2201 | if (0 && u_in->tag1 == TempReg) /* can val1 ever be dest? */ |
| 2202 | stackref[u_in->val1] = False; |
| 2203 | if (u_in->tag2 == TempReg) |
| 2204 | stackref[u_in->val2] = False; |
| 2205 | if (u_in->tag3 == TempReg) |
| 2206 | stackref[u_in->val3] = False; |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 2207 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2208 | break; |
| 2209 | } |
| 2210 | } |
| 2211 | |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 2212 | VG_(free)(stackref); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 2213 | VG_(free_UCodeBlock)(cb_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2214 | return cb; |
| 2215 | } |
| 2216 | |
| 2217 | |
| 2218 | /*--------------------------------------------------------------------*/ |
| 2219 | /*--- Error and suppression handling ---*/ |
| 2220 | /*--------------------------------------------------------------------*/ |
| 2221 | |
| 2222 | typedef |
| 2223 | enum { |
| 2224 | /* Possible data race */ |
| 2225 | EraserSupp |
| 2226 | } |
| 2227 | EraserSuppKind; |
| 2228 | |
| 2229 | /* What kind of error it is. */ |
| 2230 | typedef |
| 2231 | enum { |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2232 | EraserErr, /* data-race */ |
| 2233 | MutexErr, /* mutex operations */ |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2234 | LockGraphErr, /* mutex order error */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2235 | } |
| 2236 | EraserErrorKind; |
| 2237 | |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2238 | /* The classification of a faulting address. */ |
| 2239 | typedef |
| 2240 | enum { Undescribed, /* as-yet unclassified */ |
| 2241 | Stack, |
| 2242 | Unknown, /* classification yielded nothing useful */ |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 2243 | Mallocd, |
| 2244 | Freed, |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2245 | Segment |
| 2246 | } |
| 2247 | AddrKind; |
| 2248 | /* Records info about a faulting address. */ |
| 2249 | typedef |
| 2250 | struct { |
| 2251 | /* ALL */ |
| 2252 | AddrKind akind; |
| 2253 | /* Freed, Mallocd */ |
| 2254 | Int blksize; |
| 2255 | /* Freed, Mallocd */ |
| 2256 | Int rwoffset; |
| 2257 | /* Freed, Mallocd */ |
| 2258 | ExeContext* lastchange; |
| 2259 | ThreadId lasttid; |
| 2260 | /* Stack */ |
| 2261 | ThreadId stack_tid; |
| 2262 | /* Segment */ |
| 2263 | const Char* filename; |
| 2264 | const Char* section; |
| 2265 | /* True if is just-below %esp -- could be a gcc bug. */ |
| 2266 | Bool maybe_gcc; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2267 | /* symbolic address description */ |
| 2268 | Char *expr; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2269 | } |
| 2270 | AddrInfo; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2271 | |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2272 | /* What kind of memory access is involved in the error? */ |
| 2273 | typedef |
| 2274 | enum { ReadAxs, WriteAxs, ExecAxs } |
| 2275 | AxsKind; |
| 2276 | |
| 2277 | /* Extra context for memory errors */ |
| 2278 | typedef |
| 2279 | struct { |
| 2280 | AxsKind axskind; |
| 2281 | Int size; |
| 2282 | AddrInfo addrinfo; |
| 2283 | Bool isWrite; |
| 2284 | shadow_word prevstate; |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2285 | /* MutexErr, LockGraphErr */ |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 2286 | Mutex *mutex; |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 2287 | EC_EIP lasttouched; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2288 | ThreadId lasttid; |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2289 | /* LockGraphErr */ |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2290 | const LockSet *held_lockset; |
| 2291 | const LockSet *prev_lockset; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2292 | } |
| 2293 | HelgrindError; |
| 2294 | |
| 2295 | static __inline__ |
| 2296 | void clear_AddrInfo ( AddrInfo* ai ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2297 | { |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2298 | ai->akind = Unknown; |
| 2299 | ai->blksize = 0; |
| 2300 | ai->rwoffset = 0; |
| 2301 | ai->lastchange = NULL; |
| 2302 | ai->lasttid = VG_INVALID_THREADID; |
| 2303 | ai->filename = NULL; |
| 2304 | ai->section = "???"; |
| 2305 | ai->stack_tid = VG_INVALID_THREADID; |
| 2306 | ai->maybe_gcc = False; |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2307 | ai->expr = NULL; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2310 | static __inline__ |
| 2311 | void clear_HelgrindError ( HelgrindError* err_extra ) |
| 2312 | { |
| 2313 | err_extra->axskind = ReadAxs; |
| 2314 | err_extra->size = 0; |
| 2315 | err_extra->mutex = NULL; |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 2316 | err_extra->lasttouched= NULL_EC_EIP; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2317 | err_extra->lasttid = VG_INVALID_THREADID; |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2318 | err_extra->prev_lockset = 0; |
| 2319 | err_extra->held_lockset = 0; |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 2320 | err_extra->prevstate = SW(Vge_Virgin, 0); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2321 | clear_AddrInfo ( &err_extra->addrinfo ); |
| 2322 | err_extra->isWrite = False; |
| 2323 | } |
| 2324 | |
| 2325 | |
| 2326 | |
| 2327 | /* Describe an address as best you can, for error messages, |
| 2328 | putting the result in ai. */ |
| 2329 | |
| 2330 | static void describe_addr ( Addr a, AddrInfo* ai ) |
| 2331 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2332 | HG_Chunk* hc; |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 2333 | Int i; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2334 | |
| 2335 | /* Nested functions, yeah. Need the lexical scoping of 'a'. */ |
| 2336 | |
| 2337 | /* Closure for searching thread stacks */ |
| 2338 | Bool addr_is_in_bounds(Addr stack_min, Addr stack_max) |
| 2339 | { |
| 2340 | return (stack_min <= a && a <= stack_max); |
| 2341 | } |
| 2342 | /* Closure for searching malloc'd and free'd lists */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2343 | Bool addr_is_in_block(VgHashNode *node) |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2344 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2345 | HG_Chunk* hc2 = (HG_Chunk*)node; |
| 2346 | return (hc2->data <= a && a < hc2->data + hc2->size); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | /* Search for it in segments */ |
| 2350 | { |
| 2351 | const SegInfo *seg; |
| 2352 | |
| 2353 | for(seg = VG_(next_seginfo)(NULL); |
| 2354 | seg != NULL; |
| 2355 | seg = VG_(next_seginfo)(seg)) { |
| 2356 | Addr base = VG_(seg_start)(seg); |
| 2357 | UInt size = VG_(seg_size)(seg); |
| 2358 | const UChar *filename = VG_(seg_filename)(seg); |
| 2359 | |
| 2360 | if (a >= base && a < base+size) { |
| 2361 | ai->akind = Segment; |
| 2362 | ai->blksize = size; |
| 2363 | ai->rwoffset = a - base; |
| 2364 | ai->filename = filename; |
| 2365 | |
| 2366 | switch(VG_(seg_sect_kind)(a)) { |
| 2367 | case Vg_SectText: ai->section = "text"; break; |
| 2368 | case Vg_SectData: ai->section = "data"; break; |
| 2369 | case Vg_SectBSS: ai->section = "BSS"; break; |
| 2370 | case Vg_SectGOT: ai->section = "GOT"; break; |
| 2371 | case Vg_SectPLT: ai->section = "PLT"; break; |
| 2372 | case Vg_SectUnknown: |
| 2373 | default: |
| 2374 | ai->section = "???"; break; |
| 2375 | } |
| 2376 | |
| 2377 | return; |
| 2378 | } |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | /* Search for a currently malloc'd block which might bracket it. */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2383 | hc = (HG_Chunk*)VG_(HT_first_match)(hg_malloc_list, addr_is_in_block); |
| 2384 | if (NULL != hc) { |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2385 | ai->akind = Mallocd; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2386 | ai->blksize = hc->size; |
| 2387 | ai->rwoffset = (Int)a - (Int)(hc->data); |
| 2388 | ai->lastchange = hc->where; |
| 2389 | ai->lasttid = hc->tid; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2390 | return; |
| 2391 | } |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 2392 | |
| 2393 | /* Look in recently freed memory */ |
| 2394 | for(i = 0; i < N_FREED_CHUNKS; i++) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2395 | hc = freechunks[i]; |
| 2396 | if (hc == NULL) |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 2397 | continue; |
| 2398 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2399 | if (a >= hc->data && a < hc->data + hc->size) { |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 2400 | ai->akind = Freed; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 2401 | ai->blksize = hc->size; |
| 2402 | ai->rwoffset = a - hc->data; |
| 2403 | ai->lastchange = hc->where; |
| 2404 | ai->lasttid = hc->tid; |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 2405 | return; |
| 2406 | } |
| 2407 | } |
| 2408 | |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2409 | /* Clueless ... */ |
| 2410 | ai->akind = Unknown; |
| 2411 | return; |
| 2412 | } |
| 2413 | |
| 2414 | |
njn | 7e61481 | 2003-04-21 22:04:03 +0000 | [diff] [blame] | 2415 | /* Updates the copy with address info if necessary. */ |
| 2416 | UInt SK_(update_extra)(Error* err) |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2417 | { |
njn | 7e61481 | 2003-04-21 22:04:03 +0000 | [diff] [blame] | 2418 | HelgrindError* extra; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2419 | |
njn | 7e61481 | 2003-04-21 22:04:03 +0000 | [diff] [blame] | 2420 | extra = (HelgrindError*)VG_(get_error_extra)(err); |
| 2421 | if (extra != NULL && Undescribed == extra->addrinfo.akind) { |
| 2422 | describe_addr ( VG_(get_error_address)(err), &(extra->addrinfo) ); |
| 2423 | } |
| 2424 | return sizeof(HelgrindError); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2427 | static void record_eraser_error ( ThreadId tid, Addr a, Bool is_write, |
sewardj | 0f81169 | 2002-10-22 04:59:26 +0000 | [diff] [blame] | 2428 | shadow_word prevstate ) |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2429 | { |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 2430 | shadow_word *sw; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2431 | HelgrindError err_extra; |
| 2432 | |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2433 | n_eraser_warnings++; |
| 2434 | |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2435 | clear_HelgrindError(&err_extra); |
| 2436 | err_extra.isWrite = is_write; |
| 2437 | err_extra.addrinfo.akind = Undescribed; |
| 2438 | err_extra.prevstate = prevstate; |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 2439 | if (clo_execontext) |
| 2440 | err_extra.lasttouched = getExeContext(a); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2441 | err_extra.addrinfo.expr = VG_(describe_addr)(tid, a); |
| 2442 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2443 | VG_(maybe_record_error)( tid, EraserErr, a, |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2444 | (is_write ? "writing" : "reading"), |
| 2445 | &err_extra); |
| 2446 | |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 2447 | sw = get_sword_addr(a); |
| 2448 | if (sw->state == Vge_Excl && sw->other != TLSP_INDICATING_ALL) { |
| 2449 | ThreadLifeSeg *tls = unpackTLS(sw->other); |
| 2450 | tls->refcount--; |
| 2451 | } |
| 2452 | |
sewardj | 7f3ad22 | 2002-11-13 22:11:53 +0000 | [diff] [blame] | 2453 | set_sword(a, error_sword); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 2456 | static void record_mutex_error(ThreadId tid, Mutex *mutex, |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2457 | Char *str, ExeContext *ec) |
| 2458 | { |
| 2459 | HelgrindError err_extra; |
| 2460 | |
| 2461 | clear_HelgrindError(&err_extra); |
| 2462 | err_extra.addrinfo.akind = Undescribed; |
| 2463 | err_extra.mutex = mutex; |
sewardj | c808ef5 | 2002-11-13 22:43:26 +0000 | [diff] [blame] | 2464 | err_extra.lasttouched = EC(ec, virgin_sword, thread_seg[tid]); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2465 | err_extra.lasttid = tid; |
| 2466 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2467 | VG_(maybe_record_error)(tid, MutexErr, |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2468 | (Addr)mutex->mutexp, str, &err_extra); |
| 2469 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2470 | |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 2471 | static void record_lockgraph_error(ThreadId tid, Mutex *mutex, |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2472 | const LockSet *lockset_holding, |
| 2473 | const LockSet *lockset_prev) |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2474 | { |
| 2475 | HelgrindError err_extra; |
| 2476 | |
| 2477 | n_lockorder_warnings++; |
| 2478 | |
| 2479 | clear_HelgrindError(&err_extra); |
| 2480 | err_extra.addrinfo.akind = Undescribed; |
| 2481 | err_extra.mutex = mutex; |
| 2482 | |
sewardj | c808ef5 | 2002-11-13 22:43:26 +0000 | [diff] [blame] | 2483 | err_extra.lasttouched = EC(mutex->location, virgin_sword, 0); |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2484 | err_extra.held_lockset = lockset_holding; |
| 2485 | err_extra.prev_lockset = lockset_prev; |
| 2486 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2487 | VG_(maybe_record_error)(tid, LockGraphErr, mutex->mutexp, "", &err_extra); |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2490 | Bool SK_(eq_SkinError) ( VgRes not_used, Error* e1, Error* e2 ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2491 | { |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2492 | Char *e1s, *e2s; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2493 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2494 | sk_assert(VG_(get_error_kind)(e1) == VG_(get_error_kind)(e2)); |
| 2495 | |
| 2496 | switch (VG_(get_error_kind)(e1)) { |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2497 | case EraserErr: |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2498 | return VG_(get_error_address)(e1) == VG_(get_error_address)(e2); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2499 | |
| 2500 | case MutexErr: |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2501 | return VG_(get_error_address)(e1) == VG_(get_error_address)(e2); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2504 | e1s = VG_(get_error_string)(e1); |
| 2505 | e2s = VG_(get_error_string)(e2); |
| 2506 | if (e1s != e2s) return False; |
| 2507 | if (0 != VG_(strcmp)(e1s, e2s)) return False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2508 | return True; |
| 2509 | } |
| 2510 | |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2511 | static void pp_AddrInfo ( Addr a, AddrInfo* ai ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2512 | { |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2513 | if (ai->expr != NULL) |
| 2514 | VG_(message)(Vg_UserMsg, |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2515 | " Address %p == %s", a, ai->expr); |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2516 | |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2517 | switch (ai->akind) { |
| 2518 | case Stack: |
| 2519 | VG_(message)(Vg_UserMsg, |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2520 | " Address %p is on thread %d's stack", |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2521 | a, ai->stack_tid); |
| 2522 | break; |
| 2523 | case Unknown: |
jsgf | cb1d1c0 | 2003-10-14 21:55:10 +0000 | [diff] [blame] | 2524 | if (ai->expr != NULL) |
| 2525 | break; |
| 2526 | |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2527 | /* maybe_gcc is never set to True! This is a hangover from code |
| 2528 | in Memcheck */ |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2529 | if (ai->maybe_gcc) { |
| 2530 | VG_(message)(Vg_UserMsg, |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2531 | " Address %p is just below %%esp. Possibly a bug in GCC/G++", |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2532 | a); |
| 2533 | VG_(message)(Vg_UserMsg, |
| 2534 | " v 2.96 or 3.0.X. To suppress, use: --workaround-gcc296-bugs=yes"); |
| 2535 | } else { |
| 2536 | VG_(message)(Vg_UserMsg, |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2537 | " Address %p is not stack'd, malloc'd or free'd", a); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2538 | } |
| 2539 | break; |
| 2540 | case Segment: |
| 2541 | VG_(message)(Vg_UserMsg, |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2542 | " Address %p is in %s section of %s", |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2543 | a, ai->section, ai->filename); |
| 2544 | break; |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 2545 | case Mallocd: |
| 2546 | case Freed: { |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2547 | UInt delta; |
| 2548 | UChar* relative; |
| 2549 | if (ai->rwoffset < 0) { |
| 2550 | delta = (UInt)(- ai->rwoffset); |
| 2551 | relative = "before"; |
| 2552 | } else if (ai->rwoffset >= ai->blksize) { |
| 2553 | delta = ai->rwoffset - ai->blksize; |
| 2554 | relative = "after"; |
| 2555 | } else { |
| 2556 | delta = ai->rwoffset; |
| 2557 | relative = "inside"; |
| 2558 | } |
| 2559 | VG_(message)(Vg_UserMsg, |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2560 | " Address %p is %d bytes %s a block of size %d %s by thread %d", |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2561 | a, delta, relative, |
| 2562 | ai->blksize, |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 2563 | ai->akind == Mallocd ? "alloc'd" : "freed", |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2564 | ai->lasttid); |
sewardj | 5481f8f | 2002-10-20 19:43:47 +0000 | [diff] [blame] | 2565 | |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2566 | VG_(pp_ExeContext)(ai->lastchange); |
| 2567 | break; |
| 2568 | } |
| 2569 | default: |
| 2570 | VG_(skin_panic)("pp_AddrInfo"); |
| 2571 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2574 | static Char *lockset_str(const Char *prefix, const LockSet *lockset) |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2575 | { |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2576 | Char *buf, *cp; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2577 | Int i; |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2578 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2579 | buf = VG_(malloc)((prefix == NULL ? 0 : VG_(strlen)(prefix)) + |
| 2580 | lockset->setsize * 120 + |
| 2581 | 1); |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2582 | |
| 2583 | cp = buf; |
| 2584 | if (prefix) |
| 2585 | cp += VG_(sprintf)(cp, "%s", prefix); |
| 2586 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2587 | for(i = 0; i < lockset->setsize; i++) |
| 2588 | cp += VG_(sprintf)(cp, "%p%(y, ", lockset->mutex[i]->mutexp, |
| 2589 | lockset->mutex[i]->mutexp); |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2590 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2591 | if (lockset->setsize) |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2592 | cp[-2] = '\0'; |
| 2593 | else |
| 2594 | *cp = '\0'; |
| 2595 | |
| 2596 | return buf; |
| 2597 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2598 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 2599 | void SK_(pp_SkinError) ( Error* err ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2600 | { |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2601 | HelgrindError *extra = (HelgrindError *)VG_(get_error_extra)(err); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2602 | Char buf[100]; |
| 2603 | Char *msg = buf; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2604 | const LockSet *ls; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2605 | |
| 2606 | *msg = '\0'; |
| 2607 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2608 | switch(VG_(get_error_kind)(err)) { |
| 2609 | case EraserErr: { |
| 2610 | Addr err_addr = VG_(get_error_address)(err); |
| 2611 | |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2612 | VG_(message)(Vg_UserMsg, "Possible data race %s variable at %p %(y", |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2613 | VG_(get_error_string)(err), err_addr, err_addr); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 2614 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2615 | pp_AddrInfo(err_addr, &extra->addrinfo); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2616 | |
| 2617 | switch(extra->prevstate.state) { |
| 2618 | case Vge_Virgin: |
| 2619 | /* shouldn't be possible to go directly from virgin -> error */ |
| 2620 | VG_(sprintf)(buf, "virgin!?"); |
| 2621 | break; |
| 2622 | |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 2623 | case Vge_Excl: { |
| 2624 | ThreadLifeSeg *tls = unpackTLS(extra->prevstate.other); |
| 2625 | |
| 2626 | sk_assert(tls != unpackTLS(TLSP_INDICATING_ALL)); |
| 2627 | VG_(sprintf)(buf, "exclusively owned by thread %u", tls->tid); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2628 | break; |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 2629 | } |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2630 | |
| 2631 | case Vge_Shar: |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2632 | case Vge_SharMod: |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 2633 | ls = unpackLockSet(extra->prevstate.other); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2634 | |
| 2635 | if (isempty(ls)) { |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2636 | VG_(sprintf)(buf, "shared %s, no locks", |
| 2637 | extra->prevstate.state == Vge_Shar ? "RO" : "RW"); |
| 2638 | break; |
| 2639 | } |
| 2640 | |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2641 | msg = lockset_str(extra->prevstate.state == Vge_Shar ? |
| 2642 | "shared RO, locked by:" : |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2643 | "shared RW, locked by:", ls); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2644 | |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2645 | break; |
| 2646 | } |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2647 | |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 2648 | if (*msg) |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2649 | VG_(message)(Vg_UserMsg, " Previous state: %s", msg); |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 2650 | |
sewardj | 72baa7a | 2002-12-09 23:32:58 +0000 | [diff] [blame] | 2651 | if (clo_execontext == EC_Some |
| 2652 | && extra->lasttouched.uu_ec_eip.eip != 0) { |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 2653 | Char file[100]; |
| 2654 | UInt line; |
sewardj | 72baa7a | 2002-12-09 23:32:58 +0000 | [diff] [blame] | 2655 | Addr eip = extra->lasttouched.uu_ec_eip.eip; |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 2656 | |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2657 | VG_(message)(Vg_UserMsg, " Word at %p last changed state from %s by thread %u", |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2658 | err_addr, |
sewardj | c808ef5 | 2002-11-13 22:43:26 +0000 | [diff] [blame] | 2659 | pp_state(extra->lasttouched.state), |
| 2660 | unpackTLS(extra->lasttouched.tls)->tid); |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 2661 | |
| 2662 | if (VG_(get_filename_linenum)(eip, file, sizeof(file), &line)) { |
| 2663 | VG_(message)(Vg_UserMsg, " at %p: %y (%s:%u)", |
| 2664 | eip, eip, file, line); |
| 2665 | } else if (VG_(get_objname)(eip, file, sizeof(file))) { |
| 2666 | VG_(message)(Vg_UserMsg, " at %p: %y (in %s)", |
| 2667 | eip, eip, file); |
| 2668 | } else { |
| 2669 | VG_(message)(Vg_UserMsg, " at %p: %y", eip, eip); |
| 2670 | } |
sewardj | 72baa7a | 2002-12-09 23:32:58 +0000 | [diff] [blame] | 2671 | } else if (clo_execontext == EC_All |
| 2672 | && extra->lasttouched.uu_ec_eip.ec != NULL) { |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2673 | VG_(message)(Vg_UserMsg, " Word at %p last changed state from %s in tid %u", |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2674 | err_addr, |
sewardj | c808ef5 | 2002-11-13 22:43:26 +0000 | [diff] [blame] | 2675 | pp_state(extra->lasttouched.state), |
| 2676 | unpackTLS(extra->lasttouched.tls)->tid); |
sewardj | 72baa7a | 2002-12-09 23:32:58 +0000 | [diff] [blame] | 2677 | VG_(pp_ExeContext)(extra->lasttouched.uu_ec_eip.ec); |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 2678 | } |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2679 | break; |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2680 | } |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2681 | |
| 2682 | case MutexErr: |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 2683 | VG_(message)(Vg_UserMsg, "Mutex problem at %p%(y trying to %s", |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2684 | VG_(get_error_address)(err), |
| 2685 | VG_(get_error_address)(err), |
| 2686 | VG_(get_error_string)(err)); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 2687 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
sewardj | 72baa7a | 2002-12-09 23:32:58 +0000 | [diff] [blame] | 2688 | if (extra->lasttouched.uu_ec_eip.ec != NULL) { |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2689 | VG_(message)(Vg_UserMsg, " last touched by thread %d", extra->lasttid); |
sewardj | 72baa7a | 2002-12-09 23:32:58 +0000 | [diff] [blame] | 2690 | VG_(pp_ExeContext)(extra->lasttouched.uu_ec_eip.ec); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2691 | } |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2692 | pp_AddrInfo(VG_(get_error_address)(err), &extra->addrinfo); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2693 | break; |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2694 | |
| 2695 | case LockGraphErr: { |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2696 | const LockSet *heldset = extra->held_lockset; |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2697 | Addr err_addr = VG_(get_error_address)(err); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2698 | Int i; |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2699 | |
| 2700 | msg = lockset_str(NULL, heldset); |
| 2701 | |
| 2702 | VG_(message)(Vg_UserMsg, "Mutex %p%(y locked in inconsistent order", |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2703 | err_addr, err_addr); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 2704 | VG_(pp_ExeContext)( VG_(get_error_where)(err) ); |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2705 | VG_(message)(Vg_UserMsg, " while holding locks %s", msg); |
| 2706 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2707 | for(i = 0; i < heldset->setsize; i++) { |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 2708 | const Mutex *lsmx = heldset->mutex[i]; |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2709 | |
sewardj | 542494b | 2002-11-13 22:46:13 +0000 | [diff] [blame] | 2710 | /* needs to be a recursive search+display */ |
| 2711 | if (0 && !ismember(lsmx->lockdep, extra->mutex)) |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2712 | continue; |
| 2713 | |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2714 | VG_(message)(Vg_UserMsg, " %p%(y last locked at", |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2715 | lsmx->mutexp, lsmx->mutexp); |
| 2716 | VG_(pp_ExeContext)(lsmx->location); |
| 2717 | VG_(free)(msg); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2718 | msg = lockset_str(NULL, lsmx->lockdep); |
nethercote | 3b390c7 | 2003-11-13 17:53:43 +0000 | [diff] [blame] | 2719 | VG_(message)(Vg_UserMsg, " while depending on locks %s", msg); |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | break; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2723 | } |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | if (msg != buf) |
| 2727 | VG_(free)(msg); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2731 | Bool SK_(recognised_suppression) ( Char* name, Supp *su ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2732 | { |
| 2733 | if (0 == VG_(strcmp)(name, "Eraser")) { |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2734 | VG_(set_supp_kind)(su, EraserSupp); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2735 | return True; |
| 2736 | } else { |
| 2737 | return False; |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2742 | Bool SK_(read_extra_suppression_info) ( Int fd, Char* buf, Int nBuf, Supp* su ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2743 | { |
| 2744 | /* do nothing -- no extra suppression info present. Return True to |
| 2745 | indicate nothing bad happened. */ |
| 2746 | return True; |
| 2747 | } |
| 2748 | |
| 2749 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 2750 | Bool SK_(error_matches_suppression)(Error* err, Supp* su) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2751 | { |
nethercote | 64366b4 | 2003-12-01 13:11:47 +0000 | [diff] [blame] | 2752 | sk_assert(VG_(get_supp_kind)(su) == EraserSupp); |
| 2753 | |
| 2754 | return (VG_(get_error_kind)(err) == EraserErr); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 2757 | extern Char* SK_(get_error_name) ( Error* err ) |
| 2758 | { |
| 2759 | if (EraserErr == VG_(get_error_kind)(err)) { |
| 2760 | return "Eraser"; |
| 2761 | } else { |
| 2762 | return NULL; /* Other errors types can't be suppressed */ |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | extern void SK_(print_extra_suppression_info) ( Error* err ) |
| 2767 | { |
| 2768 | /* Do nothing */ |
| 2769 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2770 | |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 2771 | static void eraser_pre_mutex_lock(ThreadId tid, void* void_mutex) |
| 2772 | { |
| 2773 | Mutex *mutex = get_mutex((Addr)void_mutex); |
| 2774 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2775 | test_mutex_state(mutex, MxLocked, tid); |
sewardj | dca8411 | 2002-11-13 22:29:34 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2778 | static void eraser_post_mutex_lock(ThreadId tid, void* void_mutex) |
| 2779 | { |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2780 | static const Bool debug = False; |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 2781 | Mutex *mutex = get_mutex((Addr)void_mutex); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2782 | const LockSet* ls; |
| 2783 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2784 | set_mutex_state(mutex, MxLocked, tid); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2785 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2786 | # if DEBUG_LOCKS |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 2787 | VG_(printf)("lock (%u, %p)\n", tid, mutex->mutexp); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2788 | # endif |
| 2789 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2790 | /* VG_(printf)("LOCK: held %d, new %p\n", thread_locks[tid], mutex); */ |
| 2791 | # if LOCKSET_SANITY > 1 |
| 2792 | sanity_check_locksets("eraser_post_mutex_lock-IN"); |
| 2793 | # endif |
| 2794 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2795 | ls = lookup_LockSet_with(thread_locks[tid], mutex); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2796 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2797 | if (ls == NULL) { |
| 2798 | LockSet *newset = add_LockSet(thread_locks[tid], mutex); |
| 2799 | insert_LockSet(newset); |
| 2800 | ls = newset; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2801 | } |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2802 | thread_locks[tid] = ls; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2803 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2804 | if (debug || DEBUG_LOCKS) |
| 2805 | VG_(printf)("tid %u now has lockset %p\n", tid, ls); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2806 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2807 | if (debug || LOCKSET_SANITY > 1) |
| 2808 | sanity_check_locksets("eraser_post_mutex_lock-OUT"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | |
| 2812 | static void eraser_post_mutex_unlock(ThreadId tid, void* void_mutex) |
| 2813 | { |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 2814 | static const Bool debug = False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2815 | Int i = 0; |
sewardj | 39a4d84 | 2002-11-13 22:14:30 +0000 | [diff] [blame] | 2816 | Mutex *mutex = get_mutex((Addr)void_mutex); |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2817 | const LockSet *ls; |
| 2818 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2819 | test_mutex_state(mutex, MxUnlocked, tid); |
| 2820 | set_mutex_state(mutex, MxUnlocked, tid); |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 2821 | |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 2822 | if (!ismember(thread_locks[tid], mutex)) |
| 2823 | return; |
| 2824 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 2825 | if (debug || DEBUG_LOCKS) |
| 2826 | VG_(printf)("unlock(%u, %p%(y)\n", tid, mutex->mutexp, mutex->mutexp); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2827 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 2828 | if (debug || LOCKSET_SANITY > 1) |
| 2829 | sanity_check_locksets("eraser_post_mutex_unlock-IN"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2830 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2831 | ls = lookup_LockSet_without(thread_locks[tid], mutex); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2832 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2833 | if (ls == NULL) { |
| 2834 | LockSet *newset = remove_LockSet(thread_locks[tid], mutex); |
| 2835 | insert_LockSet(newset); |
| 2836 | ls = newset; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | /* Update the thread's lock vector */ |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 2840 | if (debug || DEBUG_LOCKS) |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2841 | VG_(printf)("tid %u reverts from %p to lockset %p\n", |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 2842 | tid, thread_locks[tid], i); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2843 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 2844 | thread_locks[tid] = ls; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2845 | |
sewardj | c26cc25 | 2002-10-23 21:58:55 +0000 | [diff] [blame] | 2846 | if (debug || LOCKSET_SANITY > 1) |
| 2847 | sanity_check_locksets("eraser_post_mutex_unlock-OUT"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | |
| 2851 | /* --------------------------------------------------------------------- |
| 2852 | Checking memory reads and writes |
| 2853 | ------------------------------------------------------------------ */ |
| 2854 | |
| 2855 | /* Behaviour on reads and writes: |
| 2856 | * |
| 2857 | * VIR EXCL SHAR SH_MOD |
| 2858 | * ---------------------------------------------------------------- |
| 2859 | * rd/wr, 1st thread | - EXCL - - |
| 2860 | * rd, new thread | - SHAR - - |
| 2861 | * wr, new thread | - SH_MOD - - |
| 2862 | * rd | error! - SHAR SH_MOD |
| 2863 | * wr | EXCL - SH_MOD SH_MOD |
| 2864 | * ---------------------------------------------------------------- |
| 2865 | */ |
| 2866 | |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 2867 | static inline |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2868 | void dump_around_a(Addr a) |
| 2869 | { |
| 2870 | UInt i; |
| 2871 | shadow_word* sword; |
| 2872 | VG_(printf)("NEARBY:\n"); |
| 2873 | for (i = a - 12; i <= a + 12; i += 4) { |
| 2874 | sword = get_sword_addr(i); |
| 2875 | VG_(printf)(" %x -- tid: %u, state: %u\n", i, sword->other, sword->state); |
| 2876 | } |
| 2877 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2878 | |
| 2879 | #if DEBUG_ACCESSES |
| 2880 | #define DEBUG_STATE(args...) \ |
| 2881 | VG_(printf)("(%u) ", size), \ |
| 2882 | VG_(printf)(args) |
| 2883 | #else |
| 2884 | #define DEBUG_STATE(args...) |
| 2885 | #endif |
| 2886 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2887 | static void eraser_mem_read_word(Addr a, ThreadId tid) |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 2888 | { |
sewardj | 72baa7a | 2002-12-09 23:32:58 +0000 | [diff] [blame] | 2889 | shadow_word* sword /* egcs-2.91.66 complains uninit */ = NULL; |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 2890 | shadow_word prevstate; |
| 2891 | ThreadLifeSeg *tls; |
| 2892 | const LockSet *ls; |
| 2893 | Bool statechange = False; |
| 2894 | |
| 2895 | static const void *const states[4] = { |
| 2896 | [Vge_Virgin] &&st_virgin, |
| 2897 | [Vge_Excl] &&st_excl, |
| 2898 | [Vge_Shar] &&st_shar, |
| 2899 | [Vge_SharMod] &&st_sharmod, |
| 2900 | }; |
| 2901 | |
| 2902 | tls = thread_seg[tid]; |
| 2903 | sk_assert(tls != NULL && tls->tid == tid); |
| 2904 | |
| 2905 | sword = get_sword_addr(a); |
| 2906 | if (sword == SEC_MAP_ACCESS) { |
| 2907 | VG_(printf)("read distinguished 2ndary map! 0x%x\n", a); |
| 2908 | return; |
| 2909 | } |
| 2910 | |
| 2911 | prevstate = *sword; |
| 2912 | |
| 2913 | goto *states[sword->state]; |
| 2914 | |
| 2915 | /* This looks like reading of unitialised memory, may be legit. Eg. |
| 2916 | * calloc() zeroes its values, so untouched memory may actually be |
| 2917 | * initialised. Leave that stuff to Valgrind. */ |
| 2918 | st_virgin: |
| 2919 | if (TID_INDICATING_NONVIRGIN == sword->other) { |
| 2920 | DEBUG_STATE("Read VIRGIN --> EXCL: %8x, %u\n", a, tid); |
| 2921 | if (DEBUG_VIRGIN_READS) |
| 2922 | dump_around_a(a); |
| 2923 | } else { |
| 2924 | DEBUG_STATE("Read SPECIAL --> EXCL: %8x, %u\n", a, tid); |
| 2925 | } |
| 2926 | statechange = True; |
| 2927 | *sword = SW(Vge_Excl, packTLS(tls)); /* remember exclusive owner */ |
| 2928 | tls->refcount++; |
| 2929 | goto done; |
| 2930 | |
| 2931 | st_excl: { |
| 2932 | ThreadLifeSeg *sw_tls = unpackTLS(sword->other); |
| 2933 | |
| 2934 | if (tls == sw_tls) { |
| 2935 | DEBUG_STATE("Read EXCL: %8x, %u\n", a, tid); |
| 2936 | } else if (unpackTLS(TLSP_INDICATING_ALL) == sw_tls) { |
| 2937 | DEBUG_STATE("Read EXCL/ERR: %8x, %u\n", a, tid); |
| 2938 | } else if (tlsIsDisjoint(tls, sw_tls)) { |
| 2939 | DEBUG_STATE("Read EXCL(%u) --> EXCL: %8x, %u\n", sw_tls->tid, a, tid); |
| 2940 | statechange = True; |
| 2941 | sword->other = packTLS(tls); |
| 2942 | sw_tls->refcount--; |
| 2943 | tls->refcount++; |
| 2944 | } else { |
| 2945 | DEBUG_STATE("Read EXCL(%u) --> SHAR: %8x, %u\n", sw_tls->tid, a, tid); |
| 2946 | sw_tls->refcount--; |
| 2947 | statechange = True; |
| 2948 | *sword = SW(Vge_Shar, packLockSet(thread_locks[tid])); |
| 2949 | |
| 2950 | if (DEBUG_MEM_LOCKSET_CHANGES) |
| 2951 | print_LockSet("excl read locks", unpackLockSet(sword->other)); |
| 2952 | } |
| 2953 | goto done; |
| 2954 | } |
| 2955 | |
| 2956 | st_shar: |
| 2957 | DEBUG_STATE("Read SHAR: %8x, %u\n", a, tid); |
| 2958 | sword->other = packLockSet(intersect(unpackLockSet(sword->other), |
| 2959 | thread_locks[tid])); |
| 2960 | statechange = sword->other != prevstate.other; |
| 2961 | goto done; |
| 2962 | |
| 2963 | st_sharmod: |
| 2964 | DEBUG_STATE("Read SHAR_MOD: %8x, %u\n", a, tid); |
| 2965 | ls = intersect(unpackLockSet(sword->other), |
| 2966 | thread_locks[tid]); |
| 2967 | sword->other = packLockSet(ls); |
| 2968 | |
| 2969 | statechange = sword->other != prevstate.other; |
| 2970 | |
| 2971 | if (isempty(ls)) { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2972 | record_eraser_error(tid, a, False /* !is_write */, prevstate); |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 2973 | } |
| 2974 | goto done; |
| 2975 | |
| 2976 | done: |
| 2977 | if (clo_execontext != EC_None && statechange) { |
| 2978 | EC_EIP eceip; |
| 2979 | |
| 2980 | if (clo_execontext == EC_Some) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2981 | eceip = EIP(VG_(get_EIP)(tid), prevstate, tls); |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 2982 | else |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2983 | eceip = EC(VG_(get_ExeContext)(tid), prevstate, tls); |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 2984 | setExeContext(a, eceip); |
| 2985 | } |
| 2986 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2987 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2988 | static void eraser_mem_read(Addr a, UInt size, ThreadId tid) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2989 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2990 | Addr end; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2991 | |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 2992 | end = ROUNDUP(a+size, 4); |
| 2993 | a = ROUNDDN(a, 4); |
| 2994 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 2995 | for ( ; a < end; a += 4) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2996 | eraser_mem_read_word(a, tid); |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 2999 | static void eraser_mem_write_word(Addr a, ThreadId tid) |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3000 | { |
| 3001 | ThreadLifeSeg *tls; |
sewardj | 72baa7a | 2002-12-09 23:32:58 +0000 | [diff] [blame] | 3002 | shadow_word* sword /* egcs-2.91.66 complains uninit */ = NULL; |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3003 | shadow_word prevstate; |
| 3004 | Bool statechange = False; |
| 3005 | static const void *const states[4] = { |
| 3006 | [Vge_Virgin] &&st_virgin, |
| 3007 | [Vge_Excl] &&st_excl, |
| 3008 | [Vge_Shar] &&st_shar, |
| 3009 | [Vge_SharMod] &&st_sharmod, |
| 3010 | }; |
| 3011 | |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 3012 | tls = thread_seg[tid]; |
| 3013 | sk_assert(tls != NULL && tls->tid == tid); |
| 3014 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3015 | sword = get_sword_addr(a); |
| 3016 | if (sword == SEC_MAP_ACCESS) { |
| 3017 | VG_(printf)("read distinguished 2ndary map! 0x%x\n", a); |
| 3018 | return; |
| 3019 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3020 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3021 | prevstate = *sword; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3022 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3023 | goto *states[sword->state]; |
sewardj | 16748af | 2002-10-22 04:55:54 +0000 | [diff] [blame] | 3024 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3025 | st_virgin: |
| 3026 | if (TID_INDICATING_NONVIRGIN == sword->other) |
| 3027 | DEBUG_STATE("Write VIRGIN --> EXCL: %8x, %u\n", a, tid); |
| 3028 | else |
| 3029 | DEBUG_STATE("Write SPECIAL --> EXCL: %8x, %u\n", a, tid); |
| 3030 | statechange = True; |
| 3031 | *sword = SW(Vge_Excl, packTLS(tls));/* remember exclusive owner */ |
| 3032 | tls->refcount++; |
| 3033 | goto done; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3034 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3035 | st_excl: { |
| 3036 | ThreadLifeSeg *sw_tls = unpackTLS(sword->other); |
| 3037 | |
| 3038 | if (tls == sw_tls) { |
| 3039 | DEBUG_STATE("Write EXCL: %8x, %u\n", a, tid); |
| 3040 | goto done; |
| 3041 | } else if (unpackTLS(TLSP_INDICATING_ALL) == sw_tls) { |
| 3042 | DEBUG_STATE("Write EXCL/ERR: %8x, %u\n", a, tid); |
| 3043 | goto done; |
| 3044 | } else if (tlsIsDisjoint(tls, sw_tls)) { |
| 3045 | DEBUG_STATE("Write EXCL(%u) --> EXCL: %8x, %u\n", sw_tls->tid, a, tid); |
| 3046 | sword->other = packTLS(tls); |
| 3047 | sw_tls->refcount--; |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 3048 | tls->refcount++; |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 3049 | goto done; |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3050 | } else { |
| 3051 | DEBUG_STATE("Write EXCL(%u) --> SHAR_MOD: %8x, %u\n", sw_tls->tid, a, tid); |
| 3052 | statechange = True; |
| 3053 | sw_tls->refcount--; |
| 3054 | *sword = SW(Vge_SharMod, packLockSet(thread_locks[tid])); |
| 3055 | if(DEBUG_MEM_LOCKSET_CHANGES) |
| 3056 | print_LockSet("excl write locks", unpackLockSet(sword->other)); |
| 3057 | goto SHARED_MODIFIED; |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 3058 | } |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3059 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3060 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3061 | st_shar: |
| 3062 | DEBUG_STATE("Write SHAR --> SHAR_MOD: %8x, %u\n", a, tid); |
| 3063 | sword->state = Vge_SharMod; |
| 3064 | sword->other = packLockSet(intersect(unpackLockSet(sword->other), |
| 3065 | thread_locks[tid])); |
| 3066 | statechange = True; |
| 3067 | goto SHARED_MODIFIED; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3068 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3069 | st_sharmod: |
| 3070 | DEBUG_STATE("Write SHAR_MOD: %8x, %u\n", a, tid); |
| 3071 | sword->other = packLockSet(intersect(unpackLockSet(sword->other), |
| 3072 | thread_locks[tid])); |
| 3073 | statechange = sword->other != prevstate.other; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3074 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3075 | SHARED_MODIFIED: |
| 3076 | if (isempty(unpackLockSet(sword->other))) { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3077 | record_eraser_error(tid, a, True /* is_write */, prevstate); |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3078 | } |
| 3079 | goto done; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3080 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3081 | done: |
| 3082 | if (clo_execontext != EC_None && statechange) { |
| 3083 | EC_EIP eceip; |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 3084 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3085 | if (clo_execontext == EC_Some) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3086 | eceip = EIP(VG_(get_EIP)(tid), prevstate, tls); |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3087 | else |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3088 | eceip = EC(VG_(get_ExeContext)(tid), prevstate, tls); |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3089 | setExeContext(a, eceip); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3090 | } |
| 3091 | } |
| 3092 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3093 | static void eraser_mem_write(Addr a, UInt size, ThreadId tid) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3094 | { |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 3095 | Addr end; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3096 | |
sewardj | 8fac99a | 2002-11-13 22:31:26 +0000 | [diff] [blame] | 3097 | end = ROUNDUP(a+size, 4); |
| 3098 | a = ROUNDDN(a, 4); |
| 3099 | |
sewardj | 18cd4a5 | 2002-11-13 22:37:41 +0000 | [diff] [blame] | 3100 | for ( ; a < end; a += 4) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3101 | eraser_mem_write_word(a, tid); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | #undef DEBUG_STATE |
| 3105 | |
nethercote | 31212bc | 2004-02-29 15:50:04 +0000 | [diff] [blame^] | 3106 | REGPARM(1) static void eraser_mem_help_read_1(Addr a) |
sewardj | 7ab2aca | 2002-10-20 19:40:32 +0000 | [diff] [blame] | 3107 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3108 | eraser_mem_read(a, 1, VG_(get_current_tid)()); |
sewardj | 7ab2aca | 2002-10-20 19:40:32 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
nethercote | 31212bc | 2004-02-29 15:50:04 +0000 | [diff] [blame^] | 3111 | REGPARM(1) static void eraser_mem_help_read_2(Addr a) |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3112 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3113 | eraser_mem_read(a, 2, VG_(get_current_tid)()); |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
nethercote | 31212bc | 2004-02-29 15:50:04 +0000 | [diff] [blame^] | 3116 | REGPARM(1) static void eraser_mem_help_read_4(Addr a) |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3117 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3118 | eraser_mem_read(a, 4, VG_(get_current_tid)()); |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
nethercote | 31212bc | 2004-02-29 15:50:04 +0000 | [diff] [blame^] | 3121 | REGPARM(2) static void eraser_mem_help_read_N(Addr a, UInt size) |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3122 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3123 | eraser_mem_read(a, size, VG_(get_current_tid)()); |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
nethercote | 31212bc | 2004-02-29 15:50:04 +0000 | [diff] [blame^] | 3126 | REGPARM(2) static void eraser_mem_help_write_1(Addr a, UInt val) |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3127 | { |
| 3128 | if (*(UChar *)a != val) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3129 | eraser_mem_write(a, 1, VG_(get_current_tid)()); |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3130 | } |
nethercote | 31212bc | 2004-02-29 15:50:04 +0000 | [diff] [blame^] | 3131 | REGPARM(2) static void eraser_mem_help_write_2(Addr a, UInt val) |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3132 | { |
| 3133 | if (*(UShort *)a != val) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3134 | eraser_mem_write(a, 2, VG_(get_current_tid)()); |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3135 | } |
nethercote | 31212bc | 2004-02-29 15:50:04 +0000 | [diff] [blame^] | 3136 | REGPARM(2) static void eraser_mem_help_write_4(Addr a, UInt val) |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3137 | { |
| 3138 | if (*(UInt *)a != val) |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3139 | eraser_mem_write(a, 4, VG_(get_current_tid)()); |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3140 | } |
nethercote | 31212bc | 2004-02-29 15:50:04 +0000 | [diff] [blame^] | 3141 | REGPARM(2) static void eraser_mem_help_write_N(Addr a, UInt size) |
sewardj | 7ab2aca | 2002-10-20 19:40:32 +0000 | [diff] [blame] | 3142 | { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3143 | eraser_mem_write(a, size, VG_(get_current_tid)()); |
sewardj | 7ab2aca | 2002-10-20 19:40:32 +0000 | [diff] [blame] | 3144 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3145 | |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 3146 | static void hg_thread_create(ThreadId parent, ThreadId child) |
| 3147 | { |
| 3148 | if (0) |
| 3149 | VG_(printf)("CREATE: %u creating %u\n", parent, child); |
| 3150 | |
| 3151 | newTLS(child); |
| 3152 | addPriorTLS(child, parent); |
| 3153 | |
| 3154 | newTLS(parent); |
| 3155 | } |
| 3156 | |
| 3157 | static void hg_thread_join(ThreadId joiner, ThreadId joinee) |
| 3158 | { |
| 3159 | if (0) |
| 3160 | VG_(printf)("JOIN: %u joining on %u\n", joiner, joinee); |
| 3161 | |
| 3162 | newTLS(joiner); |
| 3163 | addPriorTLS(joiner, joinee); |
| 3164 | |
| 3165 | clearTLS(joinee); |
| 3166 | } |
| 3167 | |
sewardj | 7a5ebcf | 2002-11-13 22:42:13 +0000 | [diff] [blame] | 3168 | static Int __BUS_HARDWARE_LOCK__; |
| 3169 | |
| 3170 | static void bus_lock(void) |
| 3171 | { |
| 3172 | ThreadId tid = VG_(get_current_tid)(); |
| 3173 | eraser_pre_mutex_lock(tid, &__BUS_HARDWARE_LOCK__); |
| 3174 | eraser_post_mutex_lock(tid, &__BUS_HARDWARE_LOCK__); |
| 3175 | } |
| 3176 | |
| 3177 | static void bus_unlock(void) |
| 3178 | { |
| 3179 | ThreadId tid = VG_(get_current_tid)(); |
| 3180 | eraser_post_mutex_unlock(tid, &__BUS_HARDWARE_LOCK__); |
| 3181 | } |
| 3182 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3183 | /*--------------------------------------------------------------------*/ |
sewardj | 7f3ad22 | 2002-11-13 22:11:53 +0000 | [diff] [blame] | 3184 | /*--- Client requests ---*/ |
| 3185 | /*--------------------------------------------------------------------*/ |
| 3186 | |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 3187 | Bool SK_(handle_client_request)(ThreadId tid, UInt *args, UInt *ret) |
sewardj | 7f3ad22 | 2002-11-13 22:11:53 +0000 | [diff] [blame] | 3188 | { |
| 3189 | if (!VG_IS_SKIN_USERREQ('H','G',args[0])) |
| 3190 | return False; |
| 3191 | |
| 3192 | switch(args[0]) { |
| 3193 | case VG_USERREQ__HG_CLEAN_MEMORY: |
| 3194 | set_address_range_state(args[1], args[2], Vge_VirginInit); |
| 3195 | *ret = 0; /* meaningless */ |
| 3196 | break; |
| 3197 | |
| 3198 | case VG_USERREQ__HG_KNOWN_RACE: |
| 3199 | set_address_range_state(args[1], args[2], Vge_Error); |
| 3200 | *ret = 0; /* meaningless */ |
| 3201 | break; |
| 3202 | |
| 3203 | default: |
| 3204 | return False; |
| 3205 | } |
| 3206 | |
| 3207 | return True; |
| 3208 | } |
| 3209 | |
| 3210 | |
| 3211 | /*--------------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3212 | /*--- Setup ---*/ |
| 3213 | /*--------------------------------------------------------------------*/ |
| 3214 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 3215 | void SK_(pre_clo_init)(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3216 | { |
| 3217 | Int i; |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 3218 | LockSet *empty; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3219 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 3220 | VG_(details_name) ("Helgrind"); |
| 3221 | VG_(details_version) (NULL); |
| 3222 | VG_(details_description) ("a data race detector"); |
| 3223 | VG_(details_copyright_author)( |
nethercote | bb1c991 | 2004-01-04 16:43:23 +0000 | [diff] [blame] | 3224 | "Copyright (C) 2002-2004, and GNU GPL'd, by Nicholas Nethercote."); |
nethercote | 421281e | 2003-11-20 16:20:55 +0000 | [diff] [blame] | 3225 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | 78210aa | 2002-12-01 02:55:46 +0000 | [diff] [blame] | 3226 | VG_(details_avg_translation_sizeB) ( 115 ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3227 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 3228 | VG_(needs_core_errors)(); |
| 3229 | VG_(needs_skin_errors)(); |
| 3230 | VG_(needs_data_syms)(); |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 3231 | VG_(needs_client_requests)(); |
| 3232 | VG_(needs_command_line_options)(); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3233 | VG_(needs_shadow_memory)(); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3234 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3235 | VG_(init_new_mem_startup) (& eraser_new_mem_startup); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3236 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 3237 | /* stack ones not decided until VG_(post_clo_init)() */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3238 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3239 | VG_(init_new_mem_brk) (& make_writable); |
| 3240 | VG_(init_new_mem_mmap) (& eraser_new_mem_startup); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3241 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3242 | VG_(init_change_mem_mprotect) (& eraser_set_perms); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3243 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3244 | VG_(init_ban_mem_stack) (NULL); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3245 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3246 | VG_(init_die_mem_stack) (NULL); |
| 3247 | VG_(init_die_mem_stack_signal) (NULL); |
| 3248 | VG_(init_die_mem_brk) (NULL); |
| 3249 | VG_(init_die_mem_munmap) (NULL); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3250 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3251 | VG_(init_pre_mem_read) (& eraser_pre_mem_read); |
| 3252 | VG_(init_pre_mem_read_asciiz) (& eraser_pre_mem_read_asciiz); |
| 3253 | VG_(init_pre_mem_write) (& eraser_pre_mem_write); |
| 3254 | VG_(init_post_mem_write) (NULL); |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 3255 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3256 | VG_(init_post_thread_create) (& hg_thread_create); |
| 3257 | VG_(init_post_thread_join) (& hg_thread_join); |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 3258 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3259 | VG_(init_pre_mutex_lock) (& eraser_pre_mutex_lock); |
| 3260 | VG_(init_post_mutex_lock) (& eraser_post_mutex_lock); |
| 3261 | VG_(init_post_mutex_unlock) (& eraser_post_mutex_unlock); |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 3262 | |
sewardj | a5b3aec | 2002-10-22 05:09:36 +0000 | [diff] [blame] | 3263 | VG_(register_compact_helper)((Addr) & eraser_mem_help_read_1); |
| 3264 | VG_(register_compact_helper)((Addr) & eraser_mem_help_read_2); |
| 3265 | VG_(register_compact_helper)((Addr) & eraser_mem_help_read_4); |
| 3266 | VG_(register_noncompact_helper)((Addr) & eraser_mem_help_read_N); |
| 3267 | |
| 3268 | VG_(register_compact_helper)((Addr) & eraser_mem_help_write_1); |
| 3269 | VG_(register_compact_helper)((Addr) & eraser_mem_help_write_2); |
| 3270 | VG_(register_compact_helper)((Addr) & eraser_mem_help_write_4); |
| 3271 | VG_(register_noncompact_helper)((Addr) & eraser_mem_help_write_N); |
njn | d04b7c6 | 2002-10-03 14:05:52 +0000 | [diff] [blame] | 3272 | |
sewardj | 7a5ebcf | 2002-11-13 22:42:13 +0000 | [diff] [blame] | 3273 | VG_(register_noncompact_helper)((Addr) & bus_lock); |
| 3274 | VG_(register_noncompact_helper)((Addr) & bus_unlock); |
| 3275 | |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 3276 | for(i = 0; i < LOCKSET_HASH_SZ; i++) |
| 3277 | lockset_hash[i] = NULL; |
| 3278 | |
| 3279 | empty = alloc_LockSet(0); |
| 3280 | insert_LockSet(empty); |
| 3281 | emptyset = empty; |
| 3282 | |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 3283 | /* Init lock table and thread segments */ |
| 3284 | for (i = 0; i < VG_N_THREADS; i++) { |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 3285 | thread_locks[i] = empty; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3286 | |
sewardj | c4a810d | 2002-11-13 22:25:51 +0000 | [diff] [blame] | 3287 | newTLS(i); |
| 3288 | } |
| 3289 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3290 | init_shadow_memory(); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 3291 | hg_malloc_list = VG_(HT_construct)(); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 3294 | static Bool match_Bool(Char *arg, Char *argstr, Bool *ret) |
| 3295 | { |
| 3296 | Int len = VG_(strlen)(argstr); |
| 3297 | |
| 3298 | if (VG_(strncmp)(arg, argstr, len) == 0) { |
| 3299 | if (VG_(strcmp)(arg+len, "yes") == 0) { |
| 3300 | *ret = True; |
| 3301 | return True; |
| 3302 | } else if (VG_(strcmp)(arg+len, "no") == 0) { |
| 3303 | *ret = False; |
| 3304 | return True; |
| 3305 | } else |
| 3306 | VG_(bad_option)(arg); |
| 3307 | } |
| 3308 | return False; |
| 3309 | } |
| 3310 | |
sewardj | 406270b | 2002-11-13 22:18:09 +0000 | [diff] [blame] | 3311 | static Bool match_str(Char *arg, Char *argstr, Char **ret) |
| 3312 | { |
| 3313 | Int len = VG_(strlen)(argstr); |
| 3314 | |
| 3315 | if (VG_(strncmp)(arg, argstr, len) == 0) { |
| 3316 | *ret = VG_(strdup)(arg+len); |
| 3317 | return True; |
| 3318 | } |
| 3319 | |
| 3320 | return False; |
| 3321 | } |
sewardj | 406270b | 2002-11-13 22:18:09 +0000 | [diff] [blame] | 3322 | |
| 3323 | Bool SK_(process_cmd_line_option)(Char* arg) |
| 3324 | { |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 3325 | Char *str; |
| 3326 | |
| 3327 | if (match_str(arg, "--show-last-access=", &str)) { |
| 3328 | Bool ok = True; |
| 3329 | if (VG_(strcmp)(str, "no") == 0) |
| 3330 | clo_execontext = EC_None; |
| 3331 | else if (VG_(strcmp)(str, "some") == 0) |
| 3332 | clo_execontext = EC_Some; |
| 3333 | else if (VG_(strcmp)(str, "all") == 0) |
| 3334 | clo_execontext = EC_All; |
| 3335 | else { |
| 3336 | ok = False; |
| 3337 | VG_(bad_option)(arg); |
| 3338 | } |
| 3339 | |
| 3340 | VG_(free)(str); |
| 3341 | if (ok) |
| 3342 | return True; |
| 3343 | } |
| 3344 | |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 3345 | if (match_Bool(arg, "--private-stacks=", &clo_priv_stacks)) |
| 3346 | return True; |
| 3347 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 3348 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
sewardj | 406270b | 2002-11-13 22:18:09 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 3351 | void SK_(print_usage)(void) |
sewardj | 406270b | 2002-11-13 22:18:09 +0000 | [diff] [blame] | 3352 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 3353 | VG_(printf)( |
sewardj | e11d6c8 | 2002-12-15 02:00:41 +0000 | [diff] [blame] | 3354 | " --private-stacks=yes|no assume thread stacks are used privately [no]\n" |
| 3355 | " --show-last-access=no|some|all\n" |
| 3356 | " show location of last word access on error [no]\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 3357 | ); |
| 3358 | VG_(replacement_malloc_print_usage)(); |
sewardj | 406270b | 2002-11-13 22:18:09 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 3361 | void SK_(print_debug_usage)(void) |
| 3362 | { |
| 3363 | VG_(replacement_malloc_print_debug_usage)(); |
| 3364 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3365 | |
| 3366 | void SK_(post_clo_init)(void) |
| 3367 | { |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 3368 | void (*stack_tracker)(Addr a, UInt len); |
| 3369 | |
sewardj | 499e3de | 2002-11-13 22:22:25 +0000 | [diff] [blame] | 3370 | if (clo_execontext) { |
| 3371 | execontext_map = VG_(malloc)(sizeof(ExeContextMap *) * 65536); |
| 3372 | VG_(memset)(execontext_map, 0, sizeof(ExeContextMap *) * 65536); |
| 3373 | } |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 3374 | |
njn | 810086f | 2002-11-14 12:42:47 +0000 | [diff] [blame] | 3375 | if (clo_priv_stacks) |
| 3376 | stack_tracker = & eraser_new_mem_stack_private; |
| 3377 | else |
| 3378 | stack_tracker = & eraser_new_mem_stack; |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 3379 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3380 | VG_(init_new_mem_stack) (stack_tracker); |
| 3381 | VG_(init_new_mem_stack_signal) (stack_tracker); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | |
njn | 7d9f94d | 2003-04-22 21:41:40 +0000 | [diff] [blame] | 3385 | void SK_(fini)(Int exitcode) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3386 | { |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 3387 | if (DEBUG_LOCK_TABLE) { |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 3388 | pp_all_LockSets(); |
sewardj | dac0a44 | 2002-11-13 22:08:40 +0000 | [diff] [blame] | 3389 | pp_all_mutexes(); |
| 3390 | } |
sewardj | 4bffb23 | 2002-11-13 21:46:34 +0000 | [diff] [blame] | 3391 | |
| 3392 | if (LOCKSET_SANITY) |
| 3393 | sanity_check_locksets("SK_(fini)"); |
| 3394 | |
sewardj | ff2c923 | 2002-11-13 21:44:39 +0000 | [diff] [blame] | 3395 | VG_(message)(Vg_UserMsg, "%u possible data races found; %u lock order problems", |
| 3396 | n_eraser_warnings, n_lockorder_warnings); |
sewardj | f637432 | 2002-11-13 22:35:55 +0000 | [diff] [blame] | 3397 | |
| 3398 | if (0) |
| 3399 | VG_(printf)("stk_ld:%u+stk_st:%u = %u nonstk_ld:%u+nonstk_st:%u = %u %u%%\n", |
| 3400 | stk_ld, stk_st, stk_ld + stk_st, |
| 3401 | nonstk_ld, nonstk_st, nonstk_ld + nonstk_st, |
| 3402 | ((stk_ld+stk_st)*100) / (stk_ld + stk_st + nonstk_ld + nonstk_st)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 3405 | /* Uses a 1:1 mapping */ |
| 3406 | VG_DETERMINE_INTERFACE_VERSION(SK_(pre_clo_init), 1.0) |
| 3407 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3408 | /*--------------------------------------------------------------------*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 3409 | /*--- end hg_main.c ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3410 | /*--------------------------------------------------------------------*/ |