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