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