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