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