njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 3 | /*--- The leak checker. mc_leakcheck.c ---*/ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 7 | This file is part of MemCheck, a heavyweight Valgrind tool for |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 8 | detecting memory errors. |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 9 | |
sewardj | e4b0bf0 | 2006-06-05 23:21:15 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2006 Julian Seward |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 11 | jseward@acm.org |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 31 | #include "pub_tool_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame^] | 32 | #include "pub_tool_vki.h" |
njn | 4802b38 | 2005-06-11 04:58:29 +0000 | [diff] [blame] | 33 | #include "pub_tool_aspacemgr.h" |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 34 | #include "pub_tool_execontext.h" |
| 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 | de62cbf | 2005-06-10 22:08:14 +0000 | [diff] [blame] | 39 | #include "pub_tool_libcsignal.h" |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 40 | #include "pub_tool_machine.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 41 | #include "pub_tool_mallocfree.h" |
| 42 | #include "pub_tool_options.h" |
njn | 73c5134 | 2005-06-18 15:18:15 +0000 | [diff] [blame] | 43 | #include "pub_tool_signals.h" |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 44 | #include "pub_tool_tooliface.h" // Needed for mc_include.h |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 45 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 46 | #include "mc_include.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 47 | |
| 48 | #include <setjmp.h> // For jmp_buf |
| 49 | |
| 50 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 51 | /* Define to debug the memory-leak-detector. */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 52 | #define VG_DEBUG_LEAKCHECK 0 |
| 53 | #define VG_DEBUG_CLIQUE 0 |
| 54 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 55 | /*------------------------------------------------------------*/ |
| 56 | /*--- Low-level address-space scanning, for the leak ---*/ |
| 57 | /*--- detector. ---*/ |
| 58 | /*------------------------------------------------------------*/ |
| 59 | |
| 60 | static |
| 61 | jmp_buf memscan_jmpbuf; |
| 62 | |
| 63 | |
| 64 | static |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 65 | void scan_all_valid_memory_catcher ( Int sigNo, Addr addr ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 66 | { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 67 | if (0) |
| 68 | VG_(printf)("OUCH! sig=%d addr=%p\n", sigNo, addr); |
| 69 | if (sigNo == VKI_SIGSEGV || sigNo == VKI_SIGBUS) |
| 70 | __builtin_longjmp(memscan_jmpbuf, 1); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 73 | |
| 74 | /* TODO: GIVE THIS A PROPER HOME |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 75 | TODO: MERGE THIS WITH DUPLICATE IN m_main.c and coredump-elf.c. |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 76 | Extract from aspacem a vector of the current segment start |
| 77 | addresses. The vector is dynamically allocated and should be freed |
| 78 | by the caller when done. REQUIRES m_mallocfree to be running. |
| 79 | Writes the number of addresses required into *n_acquired. */ |
| 80 | |
| 81 | static Addr* get_seg_starts ( /*OUT*/Int* n_acquired ) |
| 82 | { |
| 83 | Addr* starts; |
sewardj | ae986ca | 2005-10-12 12:53:20 +0000 | [diff] [blame] | 84 | Int n_starts, r = 0; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 85 | |
| 86 | n_starts = 1; |
| 87 | while (True) { |
| 88 | starts = VG_(malloc)( n_starts * sizeof(Addr) ); |
| 89 | if (starts == NULL) |
| 90 | break; |
| 91 | r = VG_(am_get_segment_starts)( starts, n_starts ); |
| 92 | if (r >= 0) |
| 93 | break; |
| 94 | VG_(free)(starts); |
| 95 | n_starts *= 2; |
| 96 | } |
| 97 | |
| 98 | if (starts == NULL) { |
| 99 | *n_acquired = 0; |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | *n_acquired = r; |
| 104 | return starts; |
| 105 | } |
| 106 | |
| 107 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 108 | /*------------------------------------------------------------*/ |
| 109 | /*--- Detecting leaked (unreachable) malloc'd blocks. ---*/ |
| 110 | /*------------------------------------------------------------*/ |
| 111 | |
| 112 | /* A block is either |
| 113 | -- Proper-ly reached; a pointer to its start has been found |
| 114 | -- Interior-ly reached; only an interior pointer to it has been found |
| 115 | -- Unreached; so far, no pointers to any part of it have been found. |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 116 | -- IndirectLeak; leaked, but referred to by another leaked block |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 117 | */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 118 | typedef |
| 119 | enum { |
sewardj | 75d8eb9 | 2005-12-18 02:48:40 +0000 | [diff] [blame] | 120 | Unreached =0, |
| 121 | IndirectLeak =1, |
| 122 | Interior =2, |
| 123 | Proper =3 |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 124 | } |
| 125 | Reachedness; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 126 | |
| 127 | /* An entry in the mark stack */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 128 | typedef |
| 129 | struct { |
| 130 | Int next:30; /* Index of next in mark stack */ |
| 131 | UInt state:2; /* Reachedness */ |
| 132 | SizeT indirect; /* if Unreached, how much is unreachable from here */ |
| 133 | } |
| 134 | MarkStack; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 135 | |
| 136 | /* A block record, used for generating err msgs. */ |
| 137 | typedef |
| 138 | struct _LossRecord { |
| 139 | struct _LossRecord* next; |
| 140 | /* Where these lost blocks were allocated. */ |
| 141 | ExeContext* allocated_at; |
| 142 | /* Their reachability. */ |
| 143 | Reachedness loss_mode; |
| 144 | /* Number of blocks and total # bytes involved. */ |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 145 | SizeT total_bytes; |
| 146 | SizeT indirect_bytes; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 147 | UInt num_blocks; |
| 148 | } |
| 149 | LossRecord; |
| 150 | |
njn | 0297703 | 2005-05-17 04:00:11 +0000 | [diff] [blame] | 151 | /* The 'extra' struct for leak errors. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 152 | typedef |
| 153 | struct { |
| 154 | UInt n_this_record; |
| 155 | UInt n_total_records; |
| 156 | LossRecord* lossRecord; |
| 157 | } |
| 158 | LeakExtra; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 159 | |
| 160 | /* Find the i such that ptr points at or inside the block described by |
| 161 | shadows[i]. Return -1 if none found. This assumes that shadows[] |
| 162 | has been sorted on the ->data field. */ |
| 163 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 164 | #if VG_DEBUG_LEAKCHECK |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 165 | /* Used to sanity-check the fast binary-search mechanism. */ |
| 166 | static |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 167 | Int find_shadow_for_OLD ( Addr ptr, |
| 168 | MC_Chunk** shadows, |
| 169 | Int n_shadows ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 170 | |
| 171 | { |
| 172 | Int i; |
| 173 | Addr a_lo, a_hi; |
tom | 16ade0d | 2005-07-18 09:41:57 +0000 | [diff] [blame] | 174 | PROF_EVENT(70, "find_shadow_for_OLD"); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 175 | for (i = 0; i < n_shadows; i++) { |
tom | 16ade0d | 2005-07-18 09:41:57 +0000 | [diff] [blame] | 176 | PROF_EVENT(71, "find_shadow_for_OLD(loop)"); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 177 | a_lo = shadows[i]->data; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 178 | a_hi = ((Addr)shadows[i]->data) + shadows[i]->size; |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 179 | if (a_lo <= ptr && ptr < a_hi) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 180 | return i; |
| 181 | } |
| 182 | return -1; |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | |
| 187 | static |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 188 | Int find_shadow_for ( Addr ptr, |
| 189 | MC_Chunk** shadows, |
| 190 | Int n_shadows ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 191 | { |
| 192 | Addr a_mid_lo, a_mid_hi; |
| 193 | Int lo, mid, hi, retVal; |
| 194 | /* VG_(printf)("find shadow for %p = ", ptr); */ |
| 195 | retVal = -1; |
| 196 | lo = 0; |
| 197 | hi = n_shadows-1; |
| 198 | while (True) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 199 | /* invariant: current unsearched space is from lo to hi, inclusive. */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 200 | if (lo > hi) break; /* not found */ |
| 201 | |
| 202 | mid = (lo + hi) / 2; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 203 | a_mid_lo = shadows[mid]->data; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 204 | a_mid_hi = shadows[mid]->data + shadows[mid]->size; |
sewardj | 5bee4f8 | 2006-07-29 09:00:25 +0000 | [diff] [blame] | 205 | /* Extent of block 'mid' is [a_mid_lo .. a_mid_hi). |
| 206 | Special-case zero-sized blocks - treat them as if they had |
| 207 | size 1. Not doing so causes them to not cover any address |
| 208 | range at all and so will never be identified as the target of |
| 209 | any pointer, which causes them to be incorrectly reported as |
| 210 | definitely leaked. */ |
| 211 | if (shadows[mid]->size == 0) |
| 212 | a_mid_hi++; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 213 | |
| 214 | if (ptr < a_mid_lo) { |
| 215 | hi = mid-1; |
| 216 | continue; |
| 217 | } |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 218 | if (ptr >= a_mid_hi) { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 219 | lo = mid+1; |
| 220 | continue; |
| 221 | } |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 222 | tl_assert(ptr >= a_mid_lo && ptr < a_mid_hi); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 223 | retVal = mid; |
| 224 | break; |
| 225 | } |
| 226 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 227 | # if VG_DEBUG_LEAKCHECK |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 228 | tl_assert(retVal == find_shadow_for_OLD ( ptr, shadows, n_shadows )); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 229 | # endif |
| 230 | /* VG_(printf)("%d\n", retVal); */ |
| 231 | return retVal; |
| 232 | } |
| 233 | |
| 234 | /* Globals, for the following callback used by VG_(detect_memory_leaks). */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 235 | static MC_Chunk** lc_shadows; |
| 236 | static Int lc_n_shadows; |
| 237 | static MarkStack* lc_markstack; |
| 238 | static Int lc_markstack_top; |
| 239 | static Addr lc_min_mallocd_addr; |
| 240 | static Addr lc_max_mallocd_addr; |
| 241 | static SizeT lc_scanned; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 242 | |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 243 | static Bool (*lc_is_within_valid_secondary) (Addr addr); |
| 244 | static Bool (*lc_is_valid_aligned_word) (Addr addr); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 245 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 246 | static const HChar* str_lossmode ( Reachedness lossmode ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 247 | { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 248 | const HChar *loss = "?"; |
| 249 | switch (lossmode) { |
| 250 | case Unreached: loss = "definitely lost"; break; |
| 251 | case IndirectLeak: loss = "indirectly lost"; break; |
| 252 | case Interior: loss = "possibly lost"; break; |
| 253 | case Proper: loss = "still reachable"; break; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 254 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 255 | return loss; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 256 | } |
| 257 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 258 | static const HChar* xml_kind ( Reachedness lossmode ) |
| 259 | { |
| 260 | const HChar *loss = "?"; |
| 261 | switch (lossmode) { |
| 262 | case Unreached: loss = "Leak_DefinitelyLost"; break; |
| 263 | case IndirectLeak: loss = "Leak_IndirectlyLost"; break; |
| 264 | case Interior: loss = "Leak_PossiblyLost"; break; |
| 265 | case Proper: loss = "Leak_StillReachable"; break; |
| 266 | } |
| 267 | return loss; |
| 268 | } |
| 269 | |
| 270 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 271 | /* Used for printing leak errors, avoids exposing the LossRecord type (which |
| 272 | comes in as void*, requiring a cast. */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 273 | void MC_(pp_LeakError)(void* vextra) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 274 | { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 275 | HChar* xpre = VG_(clo_xml) ? " <what>" : ""; |
| 276 | HChar* xpost = VG_(clo_xml) ? "</what>" : ""; |
| 277 | |
njn | 0297703 | 2005-05-17 04:00:11 +0000 | [diff] [blame] | 278 | LeakExtra* extra = (LeakExtra*)vextra; |
| 279 | LossRecord* l = extra->lossRecord; |
| 280 | const Char *loss = str_lossmode(l->loss_mode); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 281 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 282 | if (VG_(clo_xml)) { |
sewardj | 97f7e0c | 2005-07-19 15:00:25 +0000 | [diff] [blame] | 283 | VG_(message)(Vg_UserMsg, " <kind>%t</kind>", xml_kind(l->loss_mode)); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 284 | } else { |
| 285 | VG_(message)(Vg_UserMsg, ""); |
| 286 | } |
| 287 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 288 | if (l->indirect_bytes) { |
| 289 | VG_(message)(Vg_UserMsg, |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 290 | "%s%,lu (%,lu direct, %,lu indirect) bytes in %,u blocks" |
| 291 | " are %s in loss record %,u of %,u%s", |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 292 | xpre, |
| 293 | l->total_bytes + l->indirect_bytes, |
| 294 | l->total_bytes, l->indirect_bytes, l->num_blocks, |
| 295 | loss, extra->n_this_record, extra->n_total_records, |
| 296 | xpost |
| 297 | ); |
| 298 | if (VG_(clo_xml)) { |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 299 | // Nb: don't put commas in these XML numbers |
| 300 | VG_(message)(Vg_UserMsg, " <leakedbytes>%lu</leakedbytes>", |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 301 | l->total_bytes + l->indirect_bytes); |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 302 | VG_(message)(Vg_UserMsg, " <leakedblocks>%u</leakedblocks>", |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 303 | l->num_blocks); |
| 304 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 305 | } else { |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 306 | VG_(message)( |
| 307 | Vg_UserMsg, |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 308 | "%s%,lu bytes in %,u blocks are %s in loss record %,u of %,u%s", |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 309 | xpre, |
| 310 | l->total_bytes, l->num_blocks, |
| 311 | loss, extra->n_this_record, extra->n_total_records, |
| 312 | xpost |
| 313 | ); |
| 314 | if (VG_(clo_xml)) { |
| 315 | VG_(message)(Vg_UserMsg, " <leakedbytes>%d</leakedbytes>", |
| 316 | l->total_bytes); |
| 317 | VG_(message)(Vg_UserMsg, " <leakedblocks>%d</leakedblocks>", |
| 318 | l->num_blocks); |
| 319 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 320 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 321 | VG_(pp_ExeContext)(l->allocated_at); |
| 322 | } |
| 323 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 324 | SizeT MC_(bytes_leaked) = 0; |
| 325 | SizeT MC_(bytes_indirect) = 0; |
| 326 | SizeT MC_(bytes_dubious) = 0; |
| 327 | SizeT MC_(bytes_reachable) = 0; |
| 328 | SizeT MC_(bytes_suppressed) = 0; |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 329 | |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 330 | static Int lc_compar(void* n1, void* n2) |
| 331 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 332 | MC_Chunk* mc1 = *(MC_Chunk**)n1; |
| 333 | MC_Chunk* mc2 = *(MC_Chunk**)n2; |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 334 | return (mc1->data < mc2->data ? -1 : 1); |
| 335 | } |
| 336 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 337 | /* If ptr is pointing to a heap-allocated block which hasn't been seen |
| 338 | before, push it onto the mark stack. Clique is the index of the |
| 339 | clique leader; -1 if none. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 340 | static void lc_markstack_push_WRK(Addr ptr, Int clique) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 341 | { |
| 342 | Int sh_no; |
| 343 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 344 | /* quick filter */ |
| 345 | if (!VG_(am_is_valid_for_client)(ptr, 1, VKI_PROT_NONE)) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 346 | return; |
| 347 | |
| 348 | sh_no = find_shadow_for(ptr, lc_shadows, lc_n_shadows); |
| 349 | |
| 350 | if (VG_DEBUG_LEAKCHECK) |
| 351 | VG_(printf)("ptr=%p -> block %d\n", ptr, sh_no); |
| 352 | |
| 353 | if (sh_no == -1) |
| 354 | return; |
| 355 | |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 356 | tl_assert(sh_no >= 0 && sh_no < lc_n_shadows); |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 357 | tl_assert(ptr >= lc_shadows[sh_no]->data); |
sewardj | 5bee4f8 | 2006-07-29 09:00:25 +0000 | [diff] [blame] | 358 | tl_assert(ptr < lc_shadows[sh_no]->data |
| 359 | + lc_shadows[sh_no]->size |
| 360 | + (lc_shadows[sh_no]->size==0 ? 1 : 0)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 361 | |
| 362 | if (lc_markstack[sh_no].state == Unreached) { |
| 363 | if (0) |
| 364 | VG_(printf)("pushing %p-%p\n", lc_shadows[sh_no]->data, |
| 365 | lc_shadows[sh_no]->data + lc_shadows[sh_no]->size); |
| 366 | |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 367 | tl_assert(lc_markstack[sh_no].next == -1); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 368 | lc_markstack[sh_no].next = lc_markstack_top; |
| 369 | lc_markstack_top = sh_no; |
| 370 | } |
| 371 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 372 | tl_assert(clique >= -1 && clique < lc_n_shadows); |
| 373 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 374 | if (clique != -1) { |
| 375 | if (0) |
| 376 | VG_(printf)("mopup: %d: %p is %d\n", |
| 377 | sh_no, lc_shadows[sh_no]->data, lc_markstack[sh_no].state); |
| 378 | |
| 379 | /* An unmarked block - add it to the clique. Add its size to |
| 380 | the clique-leader's indirect size. If the new block was |
| 381 | itself a clique leader, it isn't any more, so add its |
| 382 | indirect to the new clique leader. |
| 383 | |
| 384 | If this block *is* the clique leader, it means this is a |
| 385 | cyclic structure, so none of this applies. */ |
| 386 | if (lc_markstack[sh_no].state == Unreached) { |
| 387 | lc_markstack[sh_no].state = IndirectLeak; |
| 388 | |
| 389 | if (sh_no != clique) { |
| 390 | if (VG_DEBUG_CLIQUE) { |
| 391 | if (lc_markstack[sh_no].indirect) |
| 392 | VG_(printf)(" clique %d joining clique %d adding %d+%d bytes\n", |
| 393 | sh_no, clique, |
| 394 | lc_shadows[sh_no]->size, lc_markstack[sh_no].indirect); |
| 395 | else |
| 396 | VG_(printf)(" %d joining %d adding %d\n", |
| 397 | sh_no, clique, lc_shadows[sh_no]->size); |
| 398 | } |
| 399 | |
| 400 | lc_markstack[clique].indirect += lc_shadows[sh_no]->size; |
| 401 | lc_markstack[clique].indirect += lc_markstack[sh_no].indirect; |
| 402 | lc_markstack[sh_no].indirect = 0; /* shouldn't matter */ |
| 403 | } |
| 404 | } |
| 405 | } else if (ptr == lc_shadows[sh_no]->data) { |
| 406 | lc_markstack[sh_no].state = Proper; |
| 407 | } else { |
| 408 | if (lc_markstack[sh_no].state == Unreached) |
| 409 | lc_markstack[sh_no].state = Interior; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | static void lc_markstack_push(Addr ptr) |
| 414 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 415 | lc_markstack_push_WRK(ptr, -1); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* Return the top of the mark stack, if any. */ |
| 419 | static Int lc_markstack_pop(void) |
| 420 | { |
| 421 | Int ret = lc_markstack_top; |
| 422 | |
| 423 | if (ret != -1) { |
| 424 | lc_markstack_top = lc_markstack[ret].next; |
| 425 | lc_markstack[ret].next = -1; |
| 426 | } |
| 427 | |
| 428 | return ret; |
| 429 | } |
| 430 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 431 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 432 | /* Scan a block of memory between [start, start+len). This range may |
| 433 | be bogus, inaccessable, or otherwise strange; we deal with it. |
| 434 | |
| 435 | If clique != -1, it means we're gathering leaked memory into |
| 436 | cliques, and clique is the index of the current clique leader. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 437 | static void lc_scan_memory_WRK(Addr start, SizeT len, Int clique) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 438 | { |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 439 | Addr ptr = VG_ROUNDUP(start, sizeof(Addr)); |
| 440 | Addr end = VG_ROUNDDN(start+len, sizeof(Addr)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 441 | vki_sigset_t sigmask; |
| 442 | |
| 443 | if (VG_DEBUG_LEAKCHECK) |
tom | 16ade0d | 2005-07-18 09:41:57 +0000 | [diff] [blame] | 444 | VG_(printf)("scan %p-%p\n", start, start+len); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 445 | VG_(sigprocmask)(VKI_SIG_SETMASK, NULL, &sigmask); |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 446 | VG_(set_fault_catcher)(scan_all_valid_memory_catcher); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 447 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 448 | // lc_scanned += end-ptr; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 449 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 450 | if (!VG_(am_is_valid_for_client)(ptr, sizeof(Addr), VKI_PROT_READ)) |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 451 | ptr = VG_PGROUNDUP(ptr+1); /* first page bad */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 452 | |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 453 | while (ptr < end) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 454 | Addr addr; |
| 455 | |
| 456 | /* Skip invalid chunks */ |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 457 | if (!(*lc_is_within_valid_secondary)(ptr)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 458 | ptr = VG_ROUNDUP(ptr+1, SM_SIZE); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 459 | continue; |
| 460 | } |
| 461 | |
| 462 | /* Look to see if this page seems reasonble */ |
| 463 | if ((ptr % VKI_PAGE_SIZE) == 0) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 464 | if (!VG_(am_is_valid_for_client)(ptr, sizeof(Addr), VKI_PROT_READ)) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 465 | ptr += VKI_PAGE_SIZE; /* bad page - skip it */ |
| 466 | } |
| 467 | |
| 468 | if (__builtin_setjmp(memscan_jmpbuf) == 0) { |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 469 | if ((*lc_is_valid_aligned_word)(ptr)) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 470 | lc_scanned += sizeof(Addr); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 471 | addr = *(Addr *)ptr; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 472 | lc_markstack_push_WRK(addr, clique); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 473 | } else if (0 && VG_DEBUG_LEAKCHECK) |
| 474 | VG_(printf)("%p not valid\n", ptr); |
| 475 | ptr += sizeof(Addr); |
| 476 | } else { |
| 477 | /* We need to restore the signal mask, because we were |
| 478 | longjmped out of a signal handler. */ |
| 479 | VG_(sigprocmask)(VKI_SIG_SETMASK, &sigmask, NULL); |
| 480 | |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 481 | ptr = VG_PGROUNDUP(ptr+1); /* bad page - skip it */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | VG_(sigprocmask)(VKI_SIG_SETMASK, &sigmask, NULL); |
| 486 | VG_(set_fault_catcher)(NULL); |
| 487 | } |
| 488 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 489 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 490 | static void lc_scan_memory(Addr start, SizeT len) |
| 491 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 492 | lc_scan_memory_WRK(start, len, -1); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* Process the mark stack until empty. If mopup is true, then we're |
| 496 | actually gathering leaked blocks, so they should be marked |
| 497 | IndirectLeak. */ |
| 498 | static void lc_do_leakcheck(Int clique) |
| 499 | { |
| 500 | Int top; |
| 501 | |
| 502 | while((top = lc_markstack_pop()) != -1) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 503 | tl_assert(top >= 0 && top < lc_n_shadows); |
| 504 | tl_assert(lc_markstack[top].state != Unreached); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 505 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 506 | lc_scan_memory_WRK(lc_shadows[top]->data, lc_shadows[top]->size, clique); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 510 | static SizeT blocks_leaked; |
| 511 | static SizeT blocks_indirect; |
| 512 | static SizeT blocks_dubious; |
| 513 | static SizeT blocks_reachable; |
| 514 | static SizeT blocks_suppressed; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 515 | |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 516 | static void full_report(ThreadId tid) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 517 | { |
| 518 | Int i; |
| 519 | Int n_lossrecords; |
| 520 | LossRecord* errlist; |
| 521 | LossRecord* p; |
| 522 | Bool is_suppressed; |
njn | 0297703 | 2005-05-17 04:00:11 +0000 | [diff] [blame] | 523 | LeakExtra leak_extra; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 524 | |
| 525 | /* Go through and group lost structures into cliques. For each |
| 526 | Unreached block, push it onto the mark stack, and find all the |
| 527 | blocks linked to it. These are marked IndirectLeak, and their |
| 528 | size is added to the clique leader's indirect size. If one of |
| 529 | the found blocks was itself a clique leader (from a previous |
| 530 | pass), then the cliques are merged. */ |
| 531 | for (i = 0; i < lc_n_shadows; i++) { |
| 532 | if (VG_DEBUG_CLIQUE) |
| 533 | VG_(printf)("cliques: %d at %p -> %s\n", |
njn | 0297703 | 2005-05-17 04:00:11 +0000 | [diff] [blame] | 534 | i, lc_shadows[i]->data, str_lossmode(lc_markstack[i].state)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 535 | if (lc_markstack[i].state != Unreached) |
| 536 | continue; |
| 537 | |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 538 | tl_assert(lc_markstack_top == -1); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 539 | |
| 540 | if (VG_DEBUG_CLIQUE) |
| 541 | VG_(printf)("%d: gathering clique %p\n", i, lc_shadows[i]->data); |
| 542 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 543 | lc_markstack_push_WRK(lc_shadows[i]->data, i); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 544 | |
| 545 | lc_do_leakcheck(i); |
| 546 | |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 547 | tl_assert(lc_markstack_top == -1); |
sewardj | 75d8eb9 | 2005-12-18 02:48:40 +0000 | [diff] [blame] | 548 | tl_assert(lc_markstack[i].state == IndirectLeak |
| 549 | /* jrs 20051218: Ashley Pittman supplied a |
| 550 | custom-allocator test program which causes the == |
| 551 | IndirectLeak condition to fail - it causes .state |
| 552 | to be Unreached. Since I have no idea how this |
| 553 | clique stuff works and no time to figure it out, |
| 554 | just allow that condition too. This could well be |
| 555 | a completely bogus fix. It doesn't seem unsafe |
| 556 | given that in any case the .state field is |
| 557 | immediately overwritten by the next statement. */ |
| 558 | || lc_markstack[i].state == Unreached); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 559 | |
| 560 | lc_markstack[i].state = Unreached; /* Return to unreached state, |
| 561 | to indicate its a clique |
| 562 | leader */ |
| 563 | } |
| 564 | |
| 565 | /* Common up the lost blocks so we can print sensible error messages. */ |
| 566 | n_lossrecords = 0; |
| 567 | errlist = NULL; |
| 568 | for (i = 0; i < lc_n_shadows; i++) { |
| 569 | ExeContext* where = lc_shadows[i]->where; |
| 570 | |
| 571 | for (p = errlist; p != NULL; p = p->next) { |
| 572 | if (p->loss_mode == lc_markstack[i].state |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 573 | && VG_(eq_ExeContext) ( MC_(clo_leak_resolution), |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 574 | p->allocated_at, |
| 575 | where) ) { |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | if (p != NULL) { |
| 580 | p->num_blocks ++; |
| 581 | p->total_bytes += lc_shadows[i]->size; |
| 582 | p->indirect_bytes += lc_markstack[i].indirect; |
| 583 | } else { |
| 584 | n_lossrecords ++; |
| 585 | p = VG_(malloc)(sizeof(LossRecord)); |
| 586 | p->loss_mode = lc_markstack[i].state; |
| 587 | p->allocated_at = where; |
| 588 | p->total_bytes = lc_shadows[i]->size; |
| 589 | p->indirect_bytes = lc_markstack[i].indirect; |
| 590 | p->num_blocks = 1; |
| 591 | p->next = errlist; |
| 592 | errlist = p; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /* Print out the commoned-up blocks and collect summary stats. */ |
| 597 | for (i = 0; i < n_lossrecords; i++) { |
| 598 | Bool print_record; |
| 599 | LossRecord* p_min = NULL; |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 600 | SizeT n_min = ~(0x0L); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 601 | for (p = errlist; p != NULL; p = p->next) { |
| 602 | if (p->num_blocks > 0 && p->total_bytes < n_min) { |
| 603 | n_min = p->total_bytes + p->indirect_bytes; |
| 604 | p_min = p; |
| 605 | } |
| 606 | } |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 607 | tl_assert(p_min != NULL); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 608 | |
| 609 | /* Ok to have tst==NULL; it's only used if --gdb-attach=yes, and |
| 610 | we disallow that when --leak-check=yes. |
| 611 | |
njn | 0297703 | 2005-05-17 04:00:11 +0000 | [diff] [blame] | 612 | Prints the error if not suppressed, unless it's reachable (Proper |
| 613 | or IndirectLeak) and --show-reachable=no */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 614 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 615 | print_record = ( MC_(clo_show_reachable) || |
njn | 0297703 | 2005-05-17 04:00:11 +0000 | [diff] [blame] | 616 | Unreached == p_min->loss_mode || |
| 617 | Interior == p_min->loss_mode ); |
| 618 | |
| 619 | // Nb: because VG_(unique_error) does all the error processing |
| 620 | // immediately, and doesn't save the error, leakExtra can be |
| 621 | // stack-allocated. |
| 622 | leak_extra.n_this_record = i+1; |
| 623 | leak_extra.n_total_records = n_lossrecords; |
| 624 | leak_extra.lossRecord = p_min; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 625 | is_suppressed = |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 626 | MC_(record_leak_error) ( tid, &leak_extra, p_min->allocated_at, |
| 627 | print_record ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 628 | |
| 629 | if (is_suppressed) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 630 | blocks_suppressed += p_min->num_blocks; |
| 631 | MC_(bytes_suppressed) += p_min->total_bytes; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 632 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 633 | } else if (Unreached == p_min->loss_mode) { |
| 634 | blocks_leaked += p_min->num_blocks; |
| 635 | MC_(bytes_leaked) += p_min->total_bytes; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 636 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 637 | } else if (IndirectLeak == p_min->loss_mode) { |
| 638 | blocks_indirect += p_min->num_blocks; |
| 639 | MC_(bytes_indirect) += p_min->total_bytes; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 640 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 641 | } else if (Interior == p_min->loss_mode) { |
| 642 | blocks_dubious += p_min->num_blocks; |
| 643 | MC_(bytes_dubious) += p_min->total_bytes; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 644 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 645 | } else if (Proper == p_min->loss_mode) { |
| 646 | blocks_reachable += p_min->num_blocks; |
| 647 | MC_(bytes_reachable) += p_min->total_bytes; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 648 | |
| 649 | } else { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 650 | VG_(tool_panic)("generic_detect_memory_leaks: unknown loss mode"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 651 | } |
| 652 | p_min->num_blocks = 0; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /* Compute a quick summary of the leak check. */ |
tom | 151a639 | 2005-11-11 12:30:36 +0000 | [diff] [blame] | 657 | static void make_summary(void) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 658 | { |
| 659 | Int i; |
| 660 | |
| 661 | for(i = 0; i < lc_n_shadows; i++) { |
| 662 | SizeT size = lc_shadows[i]->size; |
| 663 | |
| 664 | switch(lc_markstack[i].state) { |
| 665 | case Unreached: |
| 666 | blocks_leaked++; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 667 | MC_(bytes_leaked) += size; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 668 | break; |
| 669 | |
| 670 | case Proper: |
| 671 | blocks_reachable++; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 672 | MC_(bytes_reachable) += size; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 673 | break; |
| 674 | |
| 675 | case Interior: |
| 676 | blocks_dubious++; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 677 | MC_(bytes_dubious) += size; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 678 | break; |
| 679 | |
| 680 | case IndirectLeak: /* shouldn't happen */ |
| 681 | blocks_indirect++; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 682 | MC_(bytes_indirect) += size; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 683 | break; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 688 | static MC_Chunk** |
| 689 | find_active_shadows(UInt* n_shadows) |
| 690 | { |
| 691 | /* Our goal is to construct a set of shadows that includes every |
| 692 | * mempool chunk, and every malloc region that *doesn't* contain a |
| 693 | * mempool chunk. We do this in several phases. |
| 694 | * |
| 695 | * First we collect all the malloc chunks into an array and sort it. |
| 696 | * We do this because we want to query the chunks by interior |
| 697 | * pointers, requiring binary search. |
| 698 | * |
| 699 | * Second we build an array containing a Bool for each malloc chunk, |
| 700 | * indicating whether it contains any mempools. |
| 701 | * |
| 702 | * Third we loop over the mempool tables. For each chunk in each |
| 703 | * pool, we set the entry in the Bool array corresponding to the |
| 704 | * malloc chunk containing the mempool chunk. |
| 705 | * |
| 706 | * Finally we copy the mempool chunks and the non-marked malloc |
| 707 | * chunks into a combined array of shadows, free our temporaries, |
| 708 | * and return the combined array. |
| 709 | */ |
| 710 | |
| 711 | MC_Mempool *mp; |
| 712 | MC_Chunk **mallocs, **shadows, *mc; |
| 713 | UInt n_mallocs, m, s; |
| 714 | Bool *malloc_chunk_holds_a_pool_chunk; |
| 715 | |
| 716 | mallocs = (MC_Chunk**) VG_(HT_to_array)( MC_(malloc_list), &n_mallocs ); |
| 717 | |
| 718 | if (n_mallocs == 0) { |
| 719 | tl_assert(mallocs == NULL); |
| 720 | *n_shadows = 0; |
| 721 | return NULL; |
| 722 | } |
| 723 | |
| 724 | VG_(ssort)((void*)mallocs, n_mallocs, |
| 725 | sizeof(VgHashNode*), lc_compar); |
| 726 | |
| 727 | malloc_chunk_holds_a_pool_chunk = VG_(calloc)( n_mallocs, sizeof(Bool) ); |
| 728 | |
| 729 | *n_shadows = n_mallocs; |
| 730 | |
| 731 | VG_(HT_ResetIter)(MC_(mempool_list)); |
| 732 | while ( (mp = VG_(HT_Next)(MC_(mempool_list))) ) { |
| 733 | VG_(HT_ResetIter)(mp->chunks); |
| 734 | while ( (mc = VG_(HT_Next)(mp->chunks)) ) { |
| 735 | |
| 736 | /* We'll need a shadow for this chunk. */ |
| 737 | ++(*n_shadows); |
| 738 | |
| 739 | /* Possibly invalidate the malloc holding the beginning of this chunk. */ |
| 740 | m = find_shadow_for(mc->data, mallocs, n_mallocs); |
| 741 | if (m != -1 && malloc_chunk_holds_a_pool_chunk[m] == False) { |
| 742 | tl_assert(*n_shadows > 0); |
| 743 | --(*n_shadows); |
| 744 | malloc_chunk_holds_a_pool_chunk[m] = True; |
| 745 | } |
| 746 | |
| 747 | /* Possibly invalidate the malloc holding the end of this chunk. */ |
| 748 | if (mc->size > 1) { |
| 749 | m = find_shadow_for(mc->data + (mc->size - 1), mallocs, n_mallocs); |
| 750 | if (m != -1 && malloc_chunk_holds_a_pool_chunk[m] == False) { |
| 751 | tl_assert(*n_shadows > 0); |
| 752 | --(*n_shadows); |
| 753 | malloc_chunk_holds_a_pool_chunk[m] = True; |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | tl_assert(*n_shadows > 0); |
| 760 | shadows = VG_(malloc)(sizeof(VgHashNode*) * (*n_shadows)); |
| 761 | s = 0; |
| 762 | |
| 763 | /* Copy the mempool chunks into the final array. */ |
| 764 | VG_(HT_ResetIter)(MC_(mempool_list)); |
| 765 | while ( (mp = VG_(HT_Next)(MC_(mempool_list))) ) { |
| 766 | VG_(HT_ResetIter)(mp->chunks); |
| 767 | while ( (mc = VG_(HT_Next)(mp->chunks)) ) { |
| 768 | tl_assert(s < *n_shadows); |
| 769 | shadows[s++] = mc; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /* Copy the malloc chunks into the final array. */ |
| 774 | for (m = 0; m < n_mallocs; ++m) { |
| 775 | if (!malloc_chunk_holds_a_pool_chunk[m]) { |
| 776 | tl_assert(s < *n_shadows); |
| 777 | shadows[s++] = mallocs[m]; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | tl_assert(s == *n_shadows); |
| 782 | VG_(free)(mallocs); |
| 783 | VG_(free)(malloc_chunk_holds_a_pool_chunk); |
| 784 | |
| 785 | return shadows; |
| 786 | } |
| 787 | |
| 788 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 789 | /* Top level entry point to leak detector. Call here, passing in |
| 790 | suitable address-validating functions (see comment at top of |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 791 | scan_all_valid_memory above). These functions used to encapsulate the |
| 792 | differences between Memcheck and Addrcheck; they no longer do but it |
| 793 | doesn't hurt to keep them here. |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 794 | */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 795 | void MC_(do_detect_memory_leaks) ( |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 796 | ThreadId tid, LeakCheckMode mode, |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 797 | Bool (*is_within_valid_secondary) ( Addr ), |
| 798 | Bool (*is_valid_aligned_word) ( Addr ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 799 | ) |
| 800 | { |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 801 | Int i; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 802 | |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 803 | tl_assert(mode != LC_Off); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 804 | |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 805 | lc_shadows = find_active_shadows(&lc_n_shadows); |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 806 | |
| 807 | /* Sort the array. */ |
| 808 | VG_(ssort)((void*)lc_shadows, lc_n_shadows, sizeof(VgHashNode*), lc_compar); |
| 809 | |
| 810 | /* Sanity check; assert that the blocks are now in order */ |
| 811 | for (i = 0; i < lc_n_shadows-1; i++) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 812 | tl_assert( lc_shadows[i]->data <= lc_shadows[i+1]->data); |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 813 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 814 | |
| 815 | /* Sanity check -- make sure they don't overlap */ |
| 816 | for (i = 0; i < lc_n_shadows-1; i++) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 817 | tl_assert( lc_shadows[i]->data + lc_shadows[i]->size |
sewardj | 7e9ed71 | 2005-12-18 02:37:50 +0000 | [diff] [blame] | 818 | <= lc_shadows[i+1]->data ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | if (lc_n_shadows == 0) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 822 | tl_assert(lc_shadows == NULL); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 823 | if (VG_(clo_verbosity) >= 1 && !VG_(clo_xml)) { |
sewardj | 37d06f2 | 2003-09-17 21:48:26 +0000 | [diff] [blame] | 824 | VG_(message)(Vg_UserMsg, |
njn | 6b23958 | 2005-12-19 19:33:36 +0000 | [diff] [blame] | 825 | "All heap blocks were freed -- no leaks are possible."); |
sewardj | 37d06f2 | 2003-09-17 21:48:26 +0000 | [diff] [blame] | 826 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 827 | return; |
| 828 | } |
| 829 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 830 | if (VG_(clo_verbosity) > 0 && !VG_(clo_xml)) |
nethercote | 0f19bce | 2003-12-02 10:17:44 +0000 | [diff] [blame] | 831 | VG_(message)(Vg_UserMsg, |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 832 | "searching for pointers to %,d not-freed blocks.", |
nethercote | 0f19bce | 2003-12-02 10:17:44 +0000 | [diff] [blame] | 833 | lc_n_shadows ); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 834 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 835 | lc_min_mallocd_addr = lc_shadows[0]->data; |
| 836 | lc_max_mallocd_addr = lc_shadows[lc_n_shadows-1]->data |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 837 | + lc_shadows[lc_n_shadows-1]->size; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 838 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 839 | lc_markstack = VG_(malloc)( lc_n_shadows * sizeof(*lc_markstack) ); |
| 840 | for (i = 0; i < lc_n_shadows; i++) { |
| 841 | lc_markstack[i].next = -1; |
| 842 | lc_markstack[i].state = Unreached; |
| 843 | lc_markstack[i].indirect = 0; |
| 844 | } |
| 845 | lc_markstack_top = -1; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 846 | |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 847 | lc_is_within_valid_secondary = is_within_valid_secondary; |
| 848 | lc_is_valid_aligned_word = is_valid_aligned_word; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 849 | |
| 850 | lc_scanned = 0; |
| 851 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 852 | /* Push roots onto the mark stack. Roots are: |
| 853 | - the integer registers of all threads |
| 854 | - all mappings belonging to the client, including stacks |
| 855 | - .. but excluding any client heap segments. |
| 856 | Client heap segments are excluded because we wish to differentiate |
| 857 | client heap blocks which are referenced only from inside the heap |
| 858 | from those outside. This facilitates the indirect vs direct loss |
| 859 | categorisation, which [if the users ever manage to understand it] |
| 860 | is really useful for detecting lost cycles. |
| 861 | */ |
| 862 | { NSegment* seg; |
| 863 | Addr* seg_starts; |
| 864 | Int n_seg_starts; |
| 865 | seg_starts = get_seg_starts( &n_seg_starts ); |
| 866 | tl_assert(seg_starts && n_seg_starts > 0); |
| 867 | /* VG_(am_show_nsegments)( 0,"leakcheck"); */ |
| 868 | for (i = 0; i < n_seg_starts; i++) { |
| 869 | seg = VG_(am_find_nsegment)( seg_starts[i] ); |
| 870 | tl_assert(seg); |
| 871 | if (seg->kind != SkFileC && seg->kind != SkAnonC) |
| 872 | continue; |
| 873 | if (!(seg->hasR && seg->hasW)) |
| 874 | continue; |
| 875 | if (seg->isCH) |
| 876 | continue; |
sewardj | de3ad73 | 2006-07-27 23:12:17 +0000 | [diff] [blame] | 877 | |
| 878 | /* Don't poke around in device segments as this may cause |
| 879 | hangs. Exclude /dev/zero just in case someone allocated |
| 880 | memory by explicitly mapping /dev/zero. */ |
| 881 | if (seg->kind == SkFileC |
| 882 | && (VKI_S_ISCHR(seg->mode) || VKI_S_ISBLK(seg->mode))) { |
| 883 | HChar* dev_name = VG_(am_get_filename)( seg ); |
| 884 | if (dev_name && 0 == VG_(strcmp)(dev_name, "/dev/zero")) { |
| 885 | /* don't skip /dev/zero */ |
| 886 | } else { |
| 887 | /* skip this device mapping */ |
| 888 | continue; |
| 889 | } |
| 890 | } |
| 891 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 892 | if (0) |
| 893 | VG_(printf)("ACCEPT %2d %p %p\n", i, seg->start, seg->end); |
| 894 | lc_scan_memory(seg->start, seg->end+1 - seg->start); |
| 895 | } |
| 896 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 897 | |
| 898 | /* Push registers onto mark stack */ |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 899 | VG_(apply_to_GP_regs)(lc_markstack_push); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 900 | |
| 901 | /* Keep walking the heap until everything is found */ |
| 902 | lc_do_leakcheck(-1); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 903 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 904 | if (VG_(clo_verbosity) > 0 && !VG_(clo_xml)) |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 905 | VG_(message)(Vg_UserMsg, "checked %,lu bytes.", lc_scanned); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 906 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 907 | blocks_leaked = MC_(bytes_leaked) = 0; |
| 908 | blocks_indirect = MC_(bytes_indirect) = 0; |
| 909 | blocks_dubious = MC_(bytes_dubious) = 0; |
| 910 | blocks_reachable = MC_(bytes_reachable) = 0; |
| 911 | blocks_suppressed = MC_(bytes_suppressed) = 0; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 912 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 913 | if (mode == LC_Full) |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 914 | full_report(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 915 | else |
| 916 | make_summary(); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 917 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 918 | if (VG_(clo_verbosity) > 0 && !VG_(clo_xml)) { |
nethercote | 0f19bce | 2003-12-02 10:17:44 +0000 | [diff] [blame] | 919 | VG_(message)(Vg_UserMsg, ""); |
| 920 | VG_(message)(Vg_UserMsg, "LEAK SUMMARY:"); |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 921 | VG_(message)(Vg_UserMsg, " definitely lost: %,lu bytes in %,lu blocks.", |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 922 | MC_(bytes_leaked), blocks_leaked ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 923 | if (blocks_indirect > 0) |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 924 | VG_(message)(Vg_UserMsg, " indirectly lost: %,lu bytes in %,lu blocks.", |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 925 | MC_(bytes_indirect), blocks_indirect ); |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 926 | VG_(message)(Vg_UserMsg, " possibly lost: %,lu bytes in %,lu blocks.", |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 927 | MC_(bytes_dubious), blocks_dubious ); |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 928 | VG_(message)(Vg_UserMsg, " still reachable: %,lu bytes in %,lu blocks.", |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 929 | MC_(bytes_reachable), blocks_reachable ); |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 930 | VG_(message)(Vg_UserMsg, " suppressed: %,lu bytes in %,lu blocks.", |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 931 | MC_(bytes_suppressed), blocks_suppressed ); |
njn | 6a32942 | 2005-03-12 20:38:13 +0000 | [diff] [blame] | 932 | if (mode == LC_Summary && blocks_leaked > 0) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 933 | VG_(message)(Vg_UserMsg, |
| 934 | "Use --leak-check=full to see details of leaked memory."); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 935 | else if (!MC_(clo_show_reachable)) { |
nethercote | 0f19bce | 2003-12-02 10:17:44 +0000 | [diff] [blame] | 936 | VG_(message)(Vg_UserMsg, |
| 937 | "Reachable blocks (those to which a pointer was found) are not shown."); |
| 938 | VG_(message)(Vg_UserMsg, |
| 939 | "To see them, rerun with: --show-reachable=yes"); |
| 940 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 941 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 942 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 943 | VG_(free) ( lc_shadows ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 944 | VG_(free) ( lc_markstack ); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | /*--------------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 948 | /*--- end ---*/ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 949 | /*--------------------------------------------------------------------*/ |
| 950 | |