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 | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2008 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) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 68 | VG_(printf)("OUCH! sig=%d addr=%#lx\n", sigNo, addr); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 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) { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 88 | starts = VG_(malloc)( "mc.gss.1", n_starts * sizeof(Addr) ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 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 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 112 | /* An entry in the mark stack */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 113 | typedef |
| 114 | struct { |
| 115 | Int next:30; /* Index of next in mark stack */ |
| 116 | UInt state:2; /* Reachedness */ |
| 117 | SizeT indirect; /* if Unreached, how much is unreachable from here */ |
| 118 | } |
| 119 | MarkStack; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 120 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 121 | /* Find the i such that ptr points at or inside the block described by |
| 122 | shadows[i]. Return -1 if none found. This assumes that shadows[] |
| 123 | has been sorted on the ->data field. */ |
| 124 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 125 | #if VG_DEBUG_LEAKCHECK |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 126 | /* Used to sanity-check the fast binary-search mechanism. */ |
| 127 | static |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 128 | Int find_shadow_for_OLD ( Addr ptr, |
| 129 | MC_Chunk** shadows, |
| 130 | Int n_shadows ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 131 | |
| 132 | { |
| 133 | Int i; |
| 134 | Addr a_lo, a_hi; |
tom | 16ade0d | 2005-07-18 09:41:57 +0000 | [diff] [blame] | 135 | PROF_EVENT(70, "find_shadow_for_OLD"); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 136 | for (i = 0; i < n_shadows; i++) { |
tom | 16ade0d | 2005-07-18 09:41:57 +0000 | [diff] [blame] | 137 | PROF_EVENT(71, "find_shadow_for_OLD(loop)"); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 138 | a_lo = shadows[i]->data; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 139 | a_hi = ((Addr)shadows[i]->data) + shadows[i]->szB; |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 140 | if (a_lo <= ptr && ptr < a_hi) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 141 | return i; |
| 142 | } |
| 143 | return -1; |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | |
| 148 | static |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 149 | Int find_shadow_for ( Addr ptr, |
| 150 | MC_Chunk** shadows, |
| 151 | Int n_shadows ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 152 | { |
| 153 | Addr a_mid_lo, a_mid_hi; |
| 154 | Int lo, mid, hi, retVal; |
| 155 | /* VG_(printf)("find shadow for %p = ", ptr); */ |
| 156 | retVal = -1; |
| 157 | lo = 0; |
| 158 | hi = n_shadows-1; |
| 159 | while (True) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 160 | /* invariant: current unsearched space is from lo to hi, inclusive. */ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 161 | if (lo > hi) break; /* not found */ |
| 162 | |
| 163 | mid = (lo + hi) / 2; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 164 | a_mid_lo = shadows[mid]->data; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 165 | a_mid_hi = shadows[mid]->data + shadows[mid]->szB; |
sewardj | 5bee4f8 | 2006-07-29 09:00:25 +0000 | [diff] [blame] | 166 | /* Extent of block 'mid' is [a_mid_lo .. a_mid_hi). |
| 167 | Special-case zero-sized blocks - treat them as if they had |
| 168 | size 1. Not doing so causes them to not cover any address |
| 169 | range at all and so will never be identified as the target of |
| 170 | any pointer, which causes them to be incorrectly reported as |
| 171 | definitely leaked. */ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 172 | if (shadows[mid]->szB == 0) |
sewardj | 5bee4f8 | 2006-07-29 09:00:25 +0000 | [diff] [blame] | 173 | a_mid_hi++; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 174 | |
| 175 | if (ptr < a_mid_lo) { |
| 176 | hi = mid-1; |
| 177 | continue; |
| 178 | } |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 179 | if (ptr >= a_mid_hi) { |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 180 | lo = mid+1; |
| 181 | continue; |
| 182 | } |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 183 | tl_assert(ptr >= a_mid_lo && ptr < a_mid_hi); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 184 | retVal = mid; |
| 185 | break; |
| 186 | } |
| 187 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 188 | # if VG_DEBUG_LEAKCHECK |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 189 | tl_assert(retVal == find_shadow_for_OLD ( ptr, shadows, n_shadows )); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 190 | # endif |
| 191 | /* VG_(printf)("%d\n", retVal); */ |
| 192 | return retVal; |
| 193 | } |
| 194 | |
| 195 | /* Globals, for the following callback used by VG_(detect_memory_leaks). */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 196 | static MC_Chunk** lc_shadows; |
| 197 | static Int lc_n_shadows; |
| 198 | static MarkStack* lc_markstack; |
| 199 | static Int lc_markstack_top; |
| 200 | static Addr lc_min_mallocd_addr; |
| 201 | static Addr lc_max_mallocd_addr; |
| 202 | static SizeT lc_scanned; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 203 | |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 204 | static Bool (*lc_is_within_valid_secondary) (Addr addr); |
| 205 | static Bool (*lc_is_valid_aligned_word) (Addr addr); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 206 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 207 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 208 | SizeT MC_(bytes_leaked) = 0; |
| 209 | SizeT MC_(bytes_indirect) = 0; |
| 210 | SizeT MC_(bytes_dubious) = 0; |
| 211 | SizeT MC_(bytes_reachable) = 0; |
| 212 | SizeT MC_(bytes_suppressed) = 0; |
njn | 47363ab | 2003-04-21 13:24:40 +0000 | [diff] [blame] | 213 | |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 214 | SizeT MC_(blocks_leaked) = 0; |
| 215 | SizeT MC_(blocks_indirect) = 0; |
| 216 | SizeT MC_(blocks_dubious) = 0; |
| 217 | SizeT MC_(blocks_reachable) = 0; |
| 218 | SizeT MC_(blocks_suppressed) = 0; |
| 219 | |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 220 | static Int lc_compar(void* n1, void* n2) |
| 221 | { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 222 | MC_Chunk* mc1 = *(MC_Chunk**)n1; |
| 223 | MC_Chunk* mc2 = *(MC_Chunk**)n2; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 224 | if (mc1->data < mc2->data) return -1; |
| 225 | if (mc1->data > mc2->data) return 1; |
| 226 | return 0; |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 227 | } |
| 228 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 229 | /* If ptr is pointing to a heap-allocated block which hasn't been seen |
| 230 | before, push it onto the mark stack. Clique is the index of the |
| 231 | clique leader; -1 if none. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 232 | static void lc_markstack_push_WRK(Addr ptr, Int clique) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 233 | { |
| 234 | Int sh_no; |
| 235 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 236 | /* quick filter */ |
| 237 | if (!VG_(am_is_valid_for_client)(ptr, 1, VKI_PROT_NONE)) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 238 | return; |
| 239 | |
| 240 | sh_no = find_shadow_for(ptr, lc_shadows, lc_n_shadows); |
| 241 | |
| 242 | if (VG_DEBUG_LEAKCHECK) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 243 | VG_(printf)("ptr=%#lx -> block %d\n", ptr, sh_no); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 244 | |
| 245 | if (sh_no == -1) |
| 246 | return; |
| 247 | |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 248 | tl_assert(sh_no >= 0 && sh_no < lc_n_shadows); |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 249 | tl_assert(ptr >= lc_shadows[sh_no]->data); |
sewardj | 5bee4f8 | 2006-07-29 09:00:25 +0000 | [diff] [blame] | 250 | tl_assert(ptr < lc_shadows[sh_no]->data |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 251 | + lc_shadows[sh_no]->szB |
| 252 | + (lc_shadows[sh_no]->szB==0 ? 1 : 0)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 253 | |
| 254 | if (lc_markstack[sh_no].state == Unreached) { |
| 255 | if (0) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 256 | VG_(printf)("pushing %#lx-%#lx\n", lc_shadows[sh_no]->data, |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 257 | lc_shadows[sh_no]->data + lc_shadows[sh_no]->szB); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 258 | |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 259 | tl_assert(lc_markstack[sh_no].next == -1); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 260 | lc_markstack[sh_no].next = lc_markstack_top; |
| 261 | lc_markstack_top = sh_no; |
| 262 | } |
| 263 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 264 | tl_assert(clique >= -1 && clique < lc_n_shadows); |
| 265 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 266 | if (clique != -1) { |
| 267 | if (0) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 268 | VG_(printf)("mopup: %d: %#lx is %d\n", |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 269 | sh_no, lc_shadows[sh_no]->data, lc_markstack[sh_no].state); |
| 270 | |
| 271 | /* An unmarked block - add it to the clique. Add its size to |
| 272 | the clique-leader's indirect size. If the new block was |
| 273 | itself a clique leader, it isn't any more, so add its |
| 274 | indirect to the new clique leader. |
| 275 | |
| 276 | If this block *is* the clique leader, it means this is a |
| 277 | cyclic structure, so none of this applies. */ |
| 278 | if (lc_markstack[sh_no].state == Unreached) { |
| 279 | lc_markstack[sh_no].state = IndirectLeak; |
| 280 | |
| 281 | if (sh_no != clique) { |
| 282 | if (VG_DEBUG_CLIQUE) { |
| 283 | if (lc_markstack[sh_no].indirect) |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 284 | VG_(printf)(" clique %d joining clique %d adding %lu+%lu bytes\n", |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 285 | sh_no, clique, |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 286 | lc_shadows[sh_no]->szB + 0UL, |
| 287 | lc_markstack[sh_no].indirect); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 288 | else |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 289 | VG_(printf)(" %d joining %d adding %lu\n", |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 290 | sh_no, clique, |
| 291 | lc_shadows[sh_no]->szB + 0UL); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 292 | } |
| 293 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 294 | lc_markstack[clique].indirect += lc_shadows[sh_no]->szB; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 295 | lc_markstack[clique].indirect += lc_markstack[sh_no].indirect; |
| 296 | lc_markstack[sh_no].indirect = 0; /* shouldn't matter */ |
| 297 | } |
| 298 | } |
| 299 | } else if (ptr == lc_shadows[sh_no]->data) { |
| 300 | lc_markstack[sh_no].state = Proper; |
| 301 | } else { |
| 302 | if (lc_markstack[sh_no].state == Unreached) |
| 303 | lc_markstack[sh_no].state = Interior; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | static void lc_markstack_push(Addr ptr) |
| 308 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 309 | lc_markstack_push_WRK(ptr, -1); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* Return the top of the mark stack, if any. */ |
| 313 | static Int lc_markstack_pop(void) |
| 314 | { |
| 315 | Int ret = lc_markstack_top; |
| 316 | |
| 317 | if (ret != -1) { |
| 318 | lc_markstack_top = lc_markstack[ret].next; |
| 319 | lc_markstack[ret].next = -1; |
| 320 | } |
| 321 | |
| 322 | return ret; |
| 323 | } |
| 324 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 325 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 326 | /* Scan a block of memory between [start, start+len). This range may |
| 327 | be bogus, inaccessable, or otherwise strange; we deal with it. |
| 328 | |
| 329 | If clique != -1, it means we're gathering leaked memory into |
| 330 | cliques, and clique is the index of the current clique leader. */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 331 | static void lc_scan_memory_WRK(Addr start, SizeT len, Int clique) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 332 | { |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 333 | Addr ptr = VG_ROUNDUP(start, sizeof(Addr)); |
| 334 | Addr end = VG_ROUNDDN(start+len, sizeof(Addr)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 335 | vki_sigset_t sigmask; |
| 336 | |
| 337 | if (VG_DEBUG_LEAKCHECK) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 338 | VG_(printf)("scan %#lx-%#lx\n", start, start+len); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 339 | VG_(sigprocmask)(VKI_SIG_SETMASK, NULL, &sigmask); |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 340 | VG_(set_fault_catcher)(scan_all_valid_memory_catcher); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 341 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 342 | // lc_scanned += end-ptr; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 343 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 344 | if (!VG_(am_is_valid_for_client)(ptr, sizeof(Addr), VKI_PROT_READ)) |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 345 | ptr = VG_PGROUNDUP(ptr+1); /* first page bad */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 346 | |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 347 | while (ptr < end) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 348 | Addr addr; |
| 349 | |
| 350 | /* Skip invalid chunks */ |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 351 | if (!(*lc_is_within_valid_secondary)(ptr)) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 352 | ptr = VG_ROUNDUP(ptr+1, SM_SIZE); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 353 | continue; |
| 354 | } |
| 355 | |
| 356 | /* Look to see if this page seems reasonble */ |
| 357 | if ((ptr % VKI_PAGE_SIZE) == 0) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 358 | if (!VG_(am_is_valid_for_client)(ptr, sizeof(Addr), VKI_PROT_READ)) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 359 | ptr += VKI_PAGE_SIZE; /* bad page - skip it */ |
| 360 | } |
| 361 | |
| 362 | if (__builtin_setjmp(memscan_jmpbuf) == 0) { |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 363 | if ((*lc_is_valid_aligned_word)(ptr)) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 364 | lc_scanned += sizeof(Addr); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 365 | addr = *(Addr *)ptr; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 366 | lc_markstack_push_WRK(addr, clique); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 367 | } else if (0 && VG_DEBUG_LEAKCHECK) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 368 | VG_(printf)("%#lx not valid\n", ptr); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 369 | ptr += sizeof(Addr); |
| 370 | } else { |
| 371 | /* We need to restore the signal mask, because we were |
| 372 | longjmped out of a signal handler. */ |
| 373 | VG_(sigprocmask)(VKI_SIG_SETMASK, &sigmask, NULL); |
| 374 | |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 375 | ptr = VG_PGROUNDUP(ptr+1); /* bad page - skip it */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | VG_(sigprocmask)(VKI_SIG_SETMASK, &sigmask, NULL); |
| 380 | VG_(set_fault_catcher)(NULL); |
| 381 | } |
| 382 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 383 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 384 | static void lc_scan_memory(Addr start, SizeT len) |
| 385 | { |
sewardj | f1efbd4 | 2009-01-24 01:42:20 +0000 | [diff] [blame] | 386 | if (VG_(clo_verbosity) > 2) { |
| 387 | VG_(message)(Vg_DebugMsg, " Scanning segment: %#lx..%#lx (%ld)", |
njn | bc00bb7 | 2009-01-19 00:43:34 +0000 | [diff] [blame] | 388 | start, start+len-1, len); |
| 389 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 390 | lc_scan_memory_WRK(start, len, -1); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* Process the mark stack until empty. If mopup is true, then we're |
| 394 | actually gathering leaked blocks, so they should be marked |
| 395 | IndirectLeak. */ |
| 396 | static void lc_do_leakcheck(Int clique) |
| 397 | { |
| 398 | Int top; |
| 399 | |
| 400 | while((top = lc_markstack_pop()) != -1) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 401 | tl_assert(top >= 0 && top < lc_n_shadows); |
| 402 | tl_assert(lc_markstack[top].state != Unreached); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 403 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 404 | lc_scan_memory_WRK(lc_shadows[top]->data, lc_shadows[top]->szB, clique); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 408 | static void full_report(ThreadId tid) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 409 | { |
| 410 | Int i; |
| 411 | Int n_lossrecords; |
| 412 | LossRecord* errlist; |
| 413 | LossRecord* p; |
| 414 | Bool is_suppressed; |
| 415 | |
| 416 | /* Go through and group lost structures into cliques. For each |
| 417 | Unreached block, push it onto the mark stack, and find all the |
| 418 | blocks linked to it. These are marked IndirectLeak, and their |
| 419 | size is added to the clique leader's indirect size. If one of |
| 420 | the found blocks was itself a clique leader (from a previous |
| 421 | pass), then the cliques are merged. */ |
| 422 | for (i = 0; i < lc_n_shadows; i++) { |
| 423 | if (VG_DEBUG_CLIQUE) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 424 | VG_(printf)("cliques: %d at %#lx -> Loss state %d\n", |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 425 | i, lc_shadows[i]->data, lc_markstack[i].state); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 426 | if (lc_markstack[i].state != Unreached) |
| 427 | continue; |
| 428 | |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 429 | tl_assert(lc_markstack_top == -1); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 430 | |
| 431 | if (VG_DEBUG_CLIQUE) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 432 | VG_(printf)("%d: gathering clique %#lx\n", i, lc_shadows[i]->data); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 433 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 434 | lc_markstack_push_WRK(lc_shadows[i]->data, i); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 435 | |
| 436 | lc_do_leakcheck(i); |
| 437 | |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 438 | tl_assert(lc_markstack_top == -1); |
sewardj | 75d8eb9 | 2005-12-18 02:48:40 +0000 | [diff] [blame] | 439 | tl_assert(lc_markstack[i].state == IndirectLeak |
| 440 | /* jrs 20051218: Ashley Pittman supplied a |
| 441 | custom-allocator test program which causes the == |
| 442 | IndirectLeak condition to fail - it causes .state |
| 443 | to be Unreached. Since I have no idea how this |
| 444 | clique stuff works and no time to figure it out, |
| 445 | just allow that condition too. This could well be |
| 446 | a completely bogus fix. It doesn't seem unsafe |
| 447 | given that in any case the .state field is |
| 448 | immediately overwritten by the next statement. */ |
| 449 | || lc_markstack[i].state == Unreached); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 450 | |
| 451 | lc_markstack[i].state = Unreached; /* Return to unreached state, |
| 452 | to indicate its a clique |
| 453 | leader */ |
| 454 | } |
| 455 | |
| 456 | /* Common up the lost blocks so we can print sensible error messages. */ |
| 457 | n_lossrecords = 0; |
| 458 | errlist = NULL; |
| 459 | for (i = 0; i < lc_n_shadows; i++) { |
| 460 | ExeContext* where = lc_shadows[i]->where; |
| 461 | |
| 462 | for (p = errlist; p != NULL; p = p->next) { |
| 463 | if (p->loss_mode == lc_markstack[i].state |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 464 | && VG_(eq_ExeContext) ( MC_(clo_leak_resolution), |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 465 | p->allocated_at, |
| 466 | where) ) { |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | if (p != NULL) { |
| 471 | p->num_blocks ++; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 472 | p->total_bytes += lc_shadows[i]->szB; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 473 | p->indirect_bytes += lc_markstack[i].indirect; |
| 474 | } else { |
| 475 | n_lossrecords ++; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 476 | p = VG_(malloc)( "mc.fr.1", sizeof(LossRecord)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 477 | p->loss_mode = lc_markstack[i].state; |
| 478 | p->allocated_at = where; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 479 | p->total_bytes = lc_shadows[i]->szB; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 480 | p->indirect_bytes = lc_markstack[i].indirect; |
| 481 | p->num_blocks = 1; |
| 482 | p->next = errlist; |
| 483 | errlist = p; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /* Print out the commoned-up blocks and collect summary stats. */ |
| 488 | for (i = 0; i < n_lossrecords; i++) { |
| 489 | Bool print_record; |
| 490 | LossRecord* p_min = NULL; |
njn | 0fd92f4 | 2005-10-06 03:32:42 +0000 | [diff] [blame] | 491 | SizeT n_min = ~(0x0L); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 492 | for (p = errlist; p != NULL; p = p->next) { |
| 493 | if (p->num_blocks > 0 && p->total_bytes < n_min) { |
| 494 | n_min = p->total_bytes + p->indirect_bytes; |
| 495 | p_min = p; |
| 496 | } |
| 497 | } |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 498 | tl_assert(p_min != NULL); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 499 | |
| 500 | /* Ok to have tst==NULL; it's only used if --gdb-attach=yes, and |
| 501 | we disallow that when --leak-check=yes. |
| 502 | |
njn | 0297703 | 2005-05-17 04:00:11 +0000 | [diff] [blame] | 503 | Prints the error if not suppressed, unless it's reachable (Proper |
| 504 | or IndirectLeak) and --show-reachable=no */ |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 505 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 506 | print_record = ( MC_(clo_show_reachable) || |
njn | 0297703 | 2005-05-17 04:00:11 +0000 | [diff] [blame] | 507 | Unreached == p_min->loss_mode || |
| 508 | Interior == p_min->loss_mode ); |
| 509 | |
| 510 | // Nb: because VG_(unique_error) does all the error processing |
| 511 | // immediately, and doesn't save the error, leakExtra can be |
| 512 | // stack-allocated. |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 513 | is_suppressed = |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 514 | MC_(record_leak_error) ( tid, i+1, n_lossrecords, p_min, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 515 | print_record ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 516 | |
| 517 | if (is_suppressed) { |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 518 | MC_(blocks_suppressed) += p_min->num_blocks; |
| 519 | MC_(bytes_suppressed) += p_min->total_bytes; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 520 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 521 | } else if (Unreached == p_min->loss_mode) { |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 522 | MC_(blocks_leaked) += p_min->num_blocks; |
| 523 | MC_(bytes_leaked) += p_min->total_bytes; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 524 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 525 | } else if (IndirectLeak == p_min->loss_mode) { |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 526 | MC_(blocks_indirect) += p_min->num_blocks; |
| 527 | MC_(bytes_indirect) += p_min->total_bytes; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 528 | |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 529 | } else if (Interior == p_min->loss_mode) { |
| 530 | MC_(blocks_dubious) += p_min->num_blocks; |
| 531 | MC_(bytes_dubious) += p_min->total_bytes; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 532 | |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 533 | } else if (Proper == p_min->loss_mode) { |
| 534 | MC_(blocks_reachable) += p_min->num_blocks; |
| 535 | MC_(bytes_reachable) += p_min->total_bytes; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 536 | |
| 537 | } else { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 538 | VG_(tool_panic)("generic_detect_memory_leaks: unknown loss mode"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 539 | } |
| 540 | p_min->num_blocks = 0; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /* Compute a quick summary of the leak check. */ |
tom | 151a639 | 2005-11-11 12:30:36 +0000 | [diff] [blame] | 545 | static void make_summary(void) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 546 | { |
| 547 | Int i; |
| 548 | |
| 549 | for(i = 0; i < lc_n_shadows; i++) { |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 550 | SizeT size = lc_shadows[i]->szB; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 551 | |
| 552 | switch(lc_markstack[i].state) { |
| 553 | case Unreached: |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 554 | MC_(blocks_leaked)++; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 555 | MC_(bytes_leaked) += size; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 556 | break; |
| 557 | |
| 558 | case Proper: |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 559 | MC_(blocks_reachable)++; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 560 | MC_(bytes_reachable) += size; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 561 | break; |
| 562 | |
| 563 | case Interior: |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 564 | MC_(blocks_dubious)++; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 565 | MC_(bytes_dubious) += size; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 566 | break; |
| 567 | |
| 568 | case IndirectLeak: /* shouldn't happen */ |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 569 | MC_(blocks_indirect)++; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 570 | MC_(bytes_indirect) += size; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 571 | break; |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 576 | static MC_Chunk** |
| 577 | find_active_shadows(UInt* n_shadows) |
| 578 | { |
| 579 | /* Our goal is to construct a set of shadows that includes every |
| 580 | * mempool chunk, and every malloc region that *doesn't* contain a |
| 581 | * mempool chunk. We do this in several phases. |
| 582 | * |
| 583 | * First we collect all the malloc chunks into an array and sort it. |
| 584 | * We do this because we want to query the chunks by interior |
| 585 | * pointers, requiring binary search. |
| 586 | * |
| 587 | * Second we build an array containing a Bool for each malloc chunk, |
| 588 | * indicating whether it contains any mempools. |
| 589 | * |
| 590 | * Third we loop over the mempool tables. For each chunk in each |
| 591 | * pool, we set the entry in the Bool array corresponding to the |
| 592 | * malloc chunk containing the mempool chunk. |
| 593 | * |
| 594 | * Finally we copy the mempool chunks and the non-marked malloc |
| 595 | * chunks into a combined array of shadows, free our temporaries, |
| 596 | * and return the combined array. |
| 597 | */ |
| 598 | |
| 599 | MC_Mempool *mp; |
| 600 | MC_Chunk **mallocs, **shadows, *mc; |
| 601 | UInt n_mallocs, m, s; |
| 602 | Bool *malloc_chunk_holds_a_pool_chunk; |
| 603 | |
| 604 | mallocs = (MC_Chunk**) VG_(HT_to_array)( MC_(malloc_list), &n_mallocs ); |
| 605 | |
| 606 | if (n_mallocs == 0) { |
| 607 | tl_assert(mallocs == NULL); |
| 608 | *n_shadows = 0; |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | VG_(ssort)((void*)mallocs, n_mallocs, |
| 613 | sizeof(VgHashNode*), lc_compar); |
| 614 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 615 | malloc_chunk_holds_a_pool_chunk = VG_(calloc)( "mc.fas.1", |
| 616 | n_mallocs, sizeof(Bool) ); |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 617 | |
| 618 | *n_shadows = n_mallocs; |
| 619 | |
| 620 | VG_(HT_ResetIter)(MC_(mempool_list)); |
| 621 | while ( (mp = VG_(HT_Next)(MC_(mempool_list))) ) { |
| 622 | VG_(HT_ResetIter)(mp->chunks); |
| 623 | while ( (mc = VG_(HT_Next)(mp->chunks)) ) { |
| 624 | |
| 625 | /* We'll need a shadow for this chunk. */ |
| 626 | ++(*n_shadows); |
| 627 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 628 | /* Possibly invalidate the malloc holding the beginning of |
| 629 | this chunk. */ |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 630 | m = find_shadow_for(mc->data, mallocs, n_mallocs); |
| 631 | if (m != -1 && malloc_chunk_holds_a_pool_chunk[m] == False) { |
| 632 | tl_assert(*n_shadows > 0); |
| 633 | --(*n_shadows); |
| 634 | malloc_chunk_holds_a_pool_chunk[m] = True; |
| 635 | } |
| 636 | |
| 637 | /* Possibly invalidate the malloc holding the end of this chunk. */ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 638 | if (mc->szB > 1) { |
| 639 | m = find_shadow_for(mc->data + (mc->szB - 1), mallocs, n_mallocs); |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 640 | if (m != -1 && malloc_chunk_holds_a_pool_chunk[m] == False) { |
| 641 | tl_assert(*n_shadows > 0); |
| 642 | --(*n_shadows); |
| 643 | malloc_chunk_holds_a_pool_chunk[m] = True; |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | tl_assert(*n_shadows > 0); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 650 | shadows = VG_(malloc)("mc.fas.2", sizeof(VgHashNode*) * (*n_shadows)); |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 651 | s = 0; |
| 652 | |
| 653 | /* Copy the mempool chunks into the final array. */ |
| 654 | VG_(HT_ResetIter)(MC_(mempool_list)); |
| 655 | while ( (mp = VG_(HT_Next)(MC_(mempool_list))) ) { |
| 656 | VG_(HT_ResetIter)(mp->chunks); |
| 657 | while ( (mc = VG_(HT_Next)(mp->chunks)) ) { |
| 658 | tl_assert(s < *n_shadows); |
| 659 | shadows[s++] = mc; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /* Copy the malloc chunks into the final array. */ |
| 664 | for (m = 0; m < n_mallocs; ++m) { |
| 665 | if (!malloc_chunk_holds_a_pool_chunk[m]) { |
| 666 | tl_assert(s < *n_shadows); |
| 667 | shadows[s++] = mallocs[m]; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | tl_assert(s == *n_shadows); |
| 672 | VG_(free)(mallocs); |
| 673 | VG_(free)(malloc_chunk_holds_a_pool_chunk); |
| 674 | |
| 675 | return shadows; |
| 676 | } |
| 677 | |
| 678 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 679 | /* Top level entry point to leak detector. Call here, passing in |
| 680 | suitable address-validating functions (see comment at top of |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 681 | scan_all_valid_memory above). These functions used to encapsulate the |
| 682 | differences between Memcheck and Addrcheck; they no longer do but it |
| 683 | doesn't hurt to keep them here. |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 684 | */ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 685 | void MC_(do_detect_memory_leaks) ( |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 686 | ThreadId tid, LeakCheckMode mode, |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 687 | Bool (*is_within_valid_secondary) ( Addr ), |
| 688 | Bool (*is_valid_aligned_word) ( Addr ) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 689 | ) |
| 690 | { |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 691 | Int i; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 692 | |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 693 | tl_assert(mode != LC_Off); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 694 | |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 695 | lc_shadows = find_active_shadows(&lc_n_shadows); |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 696 | |
| 697 | /* Sort the array. */ |
| 698 | VG_(ssort)((void*)lc_shadows, lc_n_shadows, sizeof(VgHashNode*), lc_compar); |
| 699 | |
| 700 | /* Sanity check; assert that the blocks are now in order */ |
| 701 | for (i = 0; i < lc_n_shadows-1; i++) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 702 | tl_assert( lc_shadows[i]->data <= lc_shadows[i+1]->data); |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 703 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 704 | |
sewardj | 9269147 | 2008-05-29 12:23:24 +0000 | [diff] [blame] | 705 | /* Sanity check -- make sure they don't overlap. But do allow |
| 706 | exact duplicates. If this assertion fails, it may mean that the |
| 707 | application has done something stupid with |
| 708 | VALGRIND_MALLOCLIKE_BLOCK client requests, specifically, has |
| 709 | made overlapping requests (which are nonsensical). Another way |
| 710 | to screw up is to use VALGRIND_MALLOCLIKE_BLOCK for stack |
| 711 | locations; again nonsensical. */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 712 | for (i = 0; i < lc_n_shadows-1; i++) { |
tom | 026debf | 2008-07-18 08:48:04 +0000 | [diff] [blame] | 713 | Bool nonsense_overlap = ! ( |
| 714 | /* normal case - no overlap */ |
| 715 | (lc_shadows[i]->data + lc_shadows[i]->szB <= lc_shadows[i+1]->data) |
| 716 | || |
| 717 | /* degenerate case: exact duplicates */ |
| 718 | (lc_shadows[i]->data == lc_shadows[i+1]->data |
| 719 | && lc_shadows[i]->szB == lc_shadows[i+1]->szB) |
| 720 | ); |
| 721 | if (nonsense_overlap) { |
| 722 | VG_(message)(Vg_UserMsg, "Block [0x%lx, 0x%lx) overlaps with block [0x%lx, 0x%lx)", |
| 723 | lc_shadows[ i]->data, (lc_shadows[ i]->data + lc_shadows[ i]->szB), |
| 724 | lc_shadows[1+ i]->data, (lc_shadows[1+ i]->data + lc_shadows[1+ i]->szB) ); |
| 725 | } |
| 726 | tl_assert (!nonsense_overlap); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | if (lc_n_shadows == 0) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 730 | tl_assert(lc_shadows == NULL); |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 731 | if (VG_(clo_verbosity) >= 1 && !VG_(clo_xml)) { |
sewardj | 37d06f2 | 2003-09-17 21:48:26 +0000 | [diff] [blame] | 732 | VG_(message)(Vg_UserMsg, |
njn | 6b23958 | 2005-12-19 19:33:36 +0000 | [diff] [blame] | 733 | "All heap blocks were freed -- no leaks are possible."); |
sewardj | 37d06f2 | 2003-09-17 21:48:26 +0000 | [diff] [blame] | 734 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 735 | return; |
| 736 | } |
| 737 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 738 | if (VG_(clo_verbosity) > 0 && !VG_(clo_xml)) |
nethercote | 0f19bce | 2003-12-02 10:17:44 +0000 | [diff] [blame] | 739 | VG_(message)(Vg_UserMsg, |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 740 | "searching for pointers to %'d not-freed blocks.", |
nethercote | 0f19bce | 2003-12-02 10:17:44 +0000 | [diff] [blame] | 741 | lc_n_shadows ); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 742 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 743 | lc_min_mallocd_addr = lc_shadows[0]->data; |
| 744 | lc_max_mallocd_addr = lc_shadows[lc_n_shadows-1]->data |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 745 | + lc_shadows[lc_n_shadows-1]->szB; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 746 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 747 | lc_markstack = VG_(malloc)( "mc.ddml.1", |
| 748 | lc_n_shadows * sizeof(*lc_markstack) ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 749 | for (i = 0; i < lc_n_shadows; i++) { |
| 750 | lc_markstack[i].next = -1; |
| 751 | lc_markstack[i].state = Unreached; |
| 752 | lc_markstack[i].indirect = 0; |
| 753 | } |
| 754 | lc_markstack_top = -1; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 755 | |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 756 | lc_is_within_valid_secondary = is_within_valid_secondary; |
| 757 | lc_is_valid_aligned_word = is_valid_aligned_word; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 758 | |
| 759 | lc_scanned = 0; |
| 760 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 761 | /* Push roots onto the mark stack. Roots are: |
| 762 | - the integer registers of all threads |
| 763 | - all mappings belonging to the client, including stacks |
| 764 | - .. but excluding any client heap segments. |
| 765 | Client heap segments are excluded because we wish to differentiate |
| 766 | client heap blocks which are referenced only from inside the heap |
| 767 | from those outside. This facilitates the indirect vs direct loss |
| 768 | categorisation, which [if the users ever manage to understand it] |
| 769 | is really useful for detecting lost cycles. |
| 770 | */ |
sewardj | 72a28b2 | 2006-10-17 02:23:23 +0000 | [diff] [blame] | 771 | { Addr* seg_starts; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 772 | Int n_seg_starts; |
| 773 | seg_starts = get_seg_starts( &n_seg_starts ); |
| 774 | tl_assert(seg_starts && n_seg_starts > 0); |
| 775 | /* VG_(am_show_nsegments)( 0,"leakcheck"); */ |
| 776 | for (i = 0; i < n_seg_starts; i++) { |
sewardj | 72a28b2 | 2006-10-17 02:23:23 +0000 | [diff] [blame] | 777 | NSegment const* seg = VG_(am_find_nsegment)( seg_starts[i] ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 778 | tl_assert(seg); |
| 779 | if (seg->kind != SkFileC && seg->kind != SkAnonC) |
| 780 | continue; |
| 781 | if (!(seg->hasR && seg->hasW)) |
| 782 | continue; |
| 783 | if (seg->isCH) |
| 784 | continue; |
sewardj | de3ad73 | 2006-07-27 23:12:17 +0000 | [diff] [blame] | 785 | |
| 786 | /* Don't poke around in device segments as this may cause |
| 787 | hangs. Exclude /dev/zero just in case someone allocated |
| 788 | memory by explicitly mapping /dev/zero. */ |
| 789 | if (seg->kind == SkFileC |
| 790 | && (VKI_S_ISCHR(seg->mode) || VKI_S_ISBLK(seg->mode))) { |
sewardj | 72a28b2 | 2006-10-17 02:23:23 +0000 | [diff] [blame] | 791 | HChar* dev_name = VG_(am_get_filename)( (NSegment*)seg ); |
sewardj | de3ad73 | 2006-07-27 23:12:17 +0000 | [diff] [blame] | 792 | if (dev_name && 0 == VG_(strcmp)(dev_name, "/dev/zero")) { |
| 793 | /* don't skip /dev/zero */ |
| 794 | } else { |
| 795 | /* skip this device mapping */ |
| 796 | continue; |
| 797 | } |
| 798 | } |
| 799 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 800 | if (0) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 801 | VG_(printf)("ACCEPT %2d %#lx %#lx\n", i, seg->start, seg->end); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 802 | lc_scan_memory(seg->start, seg->end+1 - seg->start); |
| 803 | } |
| 804 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 805 | |
| 806 | /* Push registers onto mark stack */ |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 807 | VG_(apply_to_GP_regs)(lc_markstack_push); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 808 | |
| 809 | /* Keep walking the heap until everything is found */ |
| 810 | lc_do_leakcheck(-1); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 811 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 812 | if (VG_(clo_verbosity) > 0 && !VG_(clo_xml)) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 813 | VG_(message)(Vg_UserMsg, "checked %'lu bytes.", lc_scanned); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 814 | |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 815 | MC_(blocks_leaked) = MC_(bytes_leaked) = 0; |
| 816 | MC_(blocks_indirect) = MC_(bytes_indirect) = 0; |
| 817 | MC_(blocks_dubious) = MC_(bytes_dubious) = 0; |
| 818 | MC_(blocks_reachable) = MC_(bytes_reachable) = 0; |
| 819 | MC_(blocks_suppressed) = MC_(bytes_suppressed) = 0; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 820 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 821 | if (mode == LC_Full) |
njn | b8dca86 | 2005-03-14 02:42:44 +0000 | [diff] [blame] | 822 | full_report(tid); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 823 | else |
| 824 | make_summary(); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 825 | |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 826 | if (VG_(clo_verbosity) > 0 && !VG_(clo_xml)) { |
nethercote | 0f19bce | 2003-12-02 10:17:44 +0000 | [diff] [blame] | 827 | VG_(message)(Vg_UserMsg, ""); |
| 828 | VG_(message)(Vg_UserMsg, "LEAK SUMMARY:"); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 829 | VG_(message)(Vg_UserMsg, " definitely lost: %'lu bytes in %'lu blocks.", |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 830 | MC_(bytes_leaked), MC_(blocks_leaked) ); |
| 831 | if (MC_(blocks_indirect) > 0) |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 832 | VG_(message)(Vg_UserMsg, " indirectly lost: %'lu bytes in %'lu blocks.", |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 833 | MC_(bytes_indirect), MC_(blocks_indirect) ); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 834 | VG_(message)(Vg_UserMsg, " possibly lost: %'lu bytes in %'lu blocks.", |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 835 | MC_(bytes_dubious), MC_(blocks_dubious) ); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 836 | VG_(message)(Vg_UserMsg, " still reachable: %'lu bytes in %'lu blocks.", |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 837 | MC_(bytes_reachable), MC_(blocks_reachable) ); |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 838 | VG_(message)(Vg_UserMsg, " suppressed: %'lu bytes in %'lu blocks.", |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 839 | MC_(bytes_suppressed), MC_(blocks_suppressed) ); |
sewardj | ce4717e | 2006-12-06 18:05:54 +0000 | [diff] [blame] | 840 | if (mode == LC_Summary |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 841 | && (MC_(blocks_leaked) + MC_(blocks_indirect) |
| 842 | + MC_(blocks_dubious) + MC_(blocks_reachable)) > 0) { |
sewardj | ce4717e | 2006-12-06 18:05:54 +0000 | [diff] [blame] | 843 | VG_(message)(Vg_UserMsg, |
| 844 | "Rerun with --leak-check=full to see details of leaked memory."); |
| 845 | } |
njn | 8df80b2 | 2009-03-02 05:11:06 +0000 | [diff] [blame] | 846 | if (MC_(blocks_reachable) > 0 && !MC_(clo_show_reachable) && mode == LC_Full) { |
nethercote | 0f19bce | 2003-12-02 10:17:44 +0000 | [diff] [blame] | 847 | VG_(message)(Vg_UserMsg, |
| 848 | "Reachable blocks (those to which a pointer was found) are not shown."); |
| 849 | VG_(message)(Vg_UserMsg, |
sewardj | ce4717e | 2006-12-06 18:05:54 +0000 | [diff] [blame] | 850 | "To see them, rerun with: --leak-check=full --show-reachable=yes"); |
nethercote | 0f19bce | 2003-12-02 10:17:44 +0000 | [diff] [blame] | 851 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 852 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 853 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 854 | VG_(free) ( lc_shadows ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 855 | VG_(free) ( lc_markstack ); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /*--------------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 859 | /*--- end ---*/ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 860 | /*--------------------------------------------------------------------*/ |
| 861 | |