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