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 | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2010 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 | ac1e033 | 2009-05-08 00:39:31 +0000 | [diff] [blame] | 33 | #include "pub_tool_aspacehl.h" |
njn | 4802b38 | 2005-06-11 04:58:29 +0000 | [diff] [blame] | 34 | #include "pub_tool_aspacemgr.h" |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 35 | #include "pub_tool_execontext.h" |
| 36 | #include "pub_tool_hashtable.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 37 | #include "pub_tool_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 38 | #include "pub_tool_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 39 | #include "pub_tool_libcprint.h" |
njn | de62cbf | 2005-06-10 22:08:14 +0000 | [diff] [blame] | 40 | #include "pub_tool_libcsignal.h" |
njn | 6ace3ea | 2005-06-17 03:06:27 +0000 | [diff] [blame] | 41 | #include "pub_tool_machine.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 42 | #include "pub_tool_mallocfree.h" |
| 43 | #include "pub_tool_options.h" |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 44 | #include "pub_tool_oset.h" |
njn | 73c5134 | 2005-06-18 15:18:15 +0000 | [diff] [blame] | 45 | #include "pub_tool_signals.h" |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 46 | #include "pub_tool_libcsetjmp.h" // setjmp facilities |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 47 | #include "pub_tool_tooliface.h" // Needed for mc_include.h |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 48 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 49 | #include "mc_include.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 50 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 51 | /*------------------------------------------------------------*/ |
| 52 | /*--- An overview of leak checking. ---*/ |
| 53 | /*------------------------------------------------------------*/ |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 54 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 55 | // Leak-checking is a directed-graph traversal problem. The graph has |
| 56 | // two kinds of nodes: |
| 57 | // - root-set nodes: |
| 58 | // - GP registers of all threads; |
| 59 | // - valid, aligned, pointer-sized data words in valid client memory, |
| 60 | // including stacks, but excluding words within client heap-allocated |
| 61 | // blocks (they are excluded so that later on we can differentiate |
| 62 | // between heap blocks that are indirectly leaked vs. directly leaked). |
| 63 | // - heap-allocated blocks. A block is a mempool chunk or a malloc chunk |
| 64 | // that doesn't contain a mempool chunk. Nb: the terms "blocks" and |
| 65 | // "chunks" are used interchangeably below. |
| 66 | // |
| 67 | // There are two kinds of edges: |
| 68 | // - start-pointers, i.e. pointers to the start of a block; |
| 69 | // - interior-pointers, i.e. pointers to the interior of a block. |
| 70 | // |
| 71 | // We use "pointers" rather than "edges" below. |
| 72 | // |
| 73 | // Root set nodes only point to blocks. Blocks only point to blocks; |
| 74 | // a block can point to itself. |
| 75 | // |
| 76 | // The aim is to traverse the graph and determine the status of each block. |
| 77 | // |
| 78 | // There are 9 distinct cases. See memcheck/docs/mc-manual.xml for details. |
| 79 | // Presenting all nine categories to the user is probably too much. |
| 80 | // Currently we do this: |
| 81 | // - definitely lost: case 3 |
| 82 | // - indirectly lost: case 4, 9 |
| 83 | // - possibly lost: cases 5..8 |
| 84 | // - still reachable: cases 1, 2 |
| 85 | // |
| 86 | // It's far from clear that this is the best possible categorisation; it's |
| 87 | // accreted over time without any central guiding principle. |
| 88 | |
| 89 | /*------------------------------------------------------------*/ |
| 90 | /*--- XXX: Thoughts for improvement. ---*/ |
| 91 | /*------------------------------------------------------------*/ |
| 92 | |
| 93 | // From the user's point of view: |
| 94 | // - If they aren't using interior-pointers, they just have to fix the |
| 95 | // directly lost blocks, and the indirectly lost ones will be fixed as |
| 96 | // part of that. Any possibly lost blocks will just be due to random |
| 97 | // pointer garbage and can be ignored. |
| 98 | // |
| 99 | // - If they are using interior-pointers, the fact that they currently are not |
| 100 | // being told which ones might be directly lost vs. indirectly lost makes |
| 101 | // it hard to know where to begin. |
| 102 | // |
| 103 | // All this makes me wonder if new option is warranted: |
| 104 | // --follow-interior-pointers. By default it would be off, the leak checker |
| 105 | // wouldn't follow interior-pointers and there would only be 3 categories: |
| 106 | // R, DL, IL. |
| 107 | // |
| 108 | // If turned on, then it would show 7 categories (R, DL, IL, DR/DL, IR/IL, |
| 109 | // IR/IL/DL, IL/DL). That output is harder to understand but it's your own |
| 110 | // damn fault for using interior-pointers... |
| 111 | // |
| 112 | // ---- |
| 113 | // |
| 114 | // Also, why are two blank lines printed between each loss record? |
njn | c2f8b1b | 2009-08-10 06:47:00 +0000 | [diff] [blame] | 115 | // [bug 197930] |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 116 | // |
| 117 | // ---- |
| 118 | // |
| 119 | // Also, --show-reachable is a bad name because it also turns on the showing |
| 120 | // of indirectly leaked blocks(!) It would be better named --show-all or |
| 121 | // --show-all-heap-blocks, because that's the end result. |
| 122 | // |
| 123 | // ---- |
| 124 | // |
| 125 | // Also, the VALGRIND_LEAK_CHECK and VALGRIND_QUICK_LEAK_CHECK aren't great |
| 126 | // names. VALGRIND_FULL_LEAK_CHECK and VALGRIND_SUMMARY_LEAK_CHECK would be |
| 127 | // better. |
| 128 | // |
| 129 | // ---- |
| 130 | // |
| 131 | // Also, VALGRIND_COUNT_LEAKS and VALGRIND_COUNT_LEAK_BLOCKS aren't great as |
| 132 | // they combine direct leaks and indirect leaks into one. New, more precise |
| 133 | // ones (they'll need new names) would be good. If more categories are |
| 134 | // used, as per the --follow-interior-pointers option, they should be |
| 135 | // updated accordingly. And they should use a struct to return the values. |
| 136 | // |
| 137 | // ---- |
| 138 | // |
| 139 | // Also, for this case: |
| 140 | // |
| 141 | // (4) p4 BBB ---> AAA |
| 142 | // |
| 143 | // BBB is definitely directly lost. AAA is definitely indirectly lost. |
| 144 | // Here's the relevant loss records printed for a full check (each block is |
| 145 | // 16 bytes): |
| 146 | // |
| 147 | // ==20397== 16 bytes in 1 blocks are indirectly lost in loss record 9 of 15 |
| 148 | // ==20397== at 0x4C2694E: malloc (vg_replace_malloc.c:177) |
| 149 | // ==20397== by 0x400521: mk (leak-cases.c:49) |
| 150 | // ==20397== by 0x400578: main (leak-cases.c:72) |
| 151 | // |
| 152 | // ==20397== 32 (16 direct, 16 indirect) bytes in 1 blocks are definitely |
| 153 | // lost in loss record 14 of 15 |
| 154 | // ==20397== at 0x4C2694E: malloc (vg_replace_malloc.c:177) |
| 155 | // ==20397== by 0x400521: mk (leak-cases.c:49) |
| 156 | // ==20397== by 0x400580: main (leak-cases.c:72) |
| 157 | // |
| 158 | // The first one is fine -- it describes AAA. |
| 159 | // |
| 160 | // The second one is for BBB. It's correct in that 16 bytes in 1 block are |
| 161 | // directly lost. It's also correct that 16 are indirectly lost as a result, |
| 162 | // but it means that AAA is being counted twice in the loss records. (It's |
| 163 | // not, thankfully, counted twice in the summary counts). Argh. |
| 164 | // |
| 165 | // This would be less confusing for the second one: |
| 166 | // |
| 167 | // ==20397== 16 bytes in 1 blocks are definitely lost in loss record 14 |
| 168 | // of 15 (and 16 bytes in 1 block are indirectly lost as a result; they |
| 169 | // are mentioned elsewhere (if --show-reachable=yes is given!)) |
| 170 | // ==20397== at 0x4C2694E: malloc (vg_replace_malloc.c:177) |
| 171 | // ==20397== by 0x400521: mk (leak-cases.c:49) |
| 172 | // ==20397== by 0x400580: main (leak-cases.c:72) |
| 173 | // |
| 174 | // But ideally we'd present the loss record for the directly lost block and |
| 175 | // then the resultant indirectly lost blocks and make it clear the |
| 176 | // dependence. Double argh. |
| 177 | |
| 178 | /*------------------------------------------------------------*/ |
| 179 | /*--- The actual algorithm. ---*/ |
| 180 | /*------------------------------------------------------------*/ |
| 181 | |
| 182 | // - Find all the blocks (a.k.a. chunks) to check. Mempool chunks require |
| 183 | // some special treatment because they can be within malloc'd blocks. |
| 184 | // - Scan every word in the root set (GP registers and valid |
| 185 | // non-heap memory words). |
| 186 | // - First, we skip if it doesn't point to valid memory. |
| 187 | // - Then, we see if it points to the start or interior of a block. If |
| 188 | // so, we push the block onto the mark stack and mark it as having been |
| 189 | // reached. |
| 190 | // - Then, we process the mark stack, repeating the scanning for each block; |
| 191 | // this can push more blocks onto the mark stack. We repeat until the |
| 192 | // mark stack is empty. Each block is marked as definitely or possibly |
| 193 | // reachable, depending on whether interior-pointers were required to |
| 194 | // reach it. |
| 195 | // - At this point we know for every block if it's reachable or not. |
| 196 | // - We then push each unreached block onto the mark stack, using the block |
| 197 | // number as the "clique" number. |
| 198 | // - We process the mark stack again, this time grouping blocks into cliques |
| 199 | // in order to facilitate the directly/indirectly lost categorisation. |
| 200 | // - We group blocks by their ExeContexts and categorisation, and print them |
| 201 | // if --leak-check=full. We also print summary numbers. |
| 202 | // |
| 203 | // A note on "cliques": |
| 204 | // - A directly lost block is one with no pointers to it. An indirectly |
| 205 | // lost block is one that is pointed to by a directly or indirectly lost |
| 206 | // block. |
| 207 | // - Each directly lost block has zero or more indirectly lost blocks |
| 208 | // hanging off it. All these blocks together form a "clique". The |
| 209 | // directly lost block is called the "clique leader". The clique number |
| 210 | // is the number (in lc_chunks[]) of the clique leader. |
| 211 | // - Actually, a directly lost block may be pointed to if it's part of a |
| 212 | // cycle. In that case, there may be more than one choice for the clique |
| 213 | // leader, and the choice is arbitrary. Eg. if you have A-->B and B-->A |
| 214 | // either A or B could be the clique leader. |
| 215 | // - Cliques cannot overlap, and will be truncated to avoid this. Eg. if we |
| 216 | // have A-->C and B-->C, the two cliques will be {A,C} and {B}, or {A} and |
| 217 | // {B,C} (again the choice is arbitrary). This is because we don't want |
| 218 | // to count a block as indirectly lost more than once. |
| 219 | // |
| 220 | // A note on 'is_prior_definite': |
| 221 | // - This is a boolean used in various places that indicates if the chain |
| 222 | // up to the prior node (prior to the one being considered) is definite. |
| 223 | // - In the clique == -1 case: |
| 224 | // - if True it means that the prior node is a root-set node, or that the |
| 225 | // prior node is a block which is reachable from the root-set via |
| 226 | // start-pointers. |
| 227 | // - if False it means that the prior node is a block that is only |
| 228 | // reachable from the root-set via a path including at least one |
| 229 | // interior-pointer. |
| 230 | // - In the clique != -1 case, currently it's always True because we treat |
| 231 | // start-pointers and interior-pointers the same for direct/indirect leak |
| 232 | // checking. If we added a PossibleIndirectLeak state then this would |
| 233 | // change. |
| 234 | |
| 235 | |
| 236 | // Define to debug the memory-leak-detector. |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 237 | #define VG_DEBUG_LEAKCHECK 0 |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 238 | #define VG_DEBUG_CLIQUE 0 |
| 239 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 240 | |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 241 | /*------------------------------------------------------------*/ |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 242 | /*--- Getting the initial chunks, and searching them. ---*/ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 243 | /*------------------------------------------------------------*/ |
| 244 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 245 | // Compare the MC_Chunks by 'data' (i.e. the address of the block). |
| 246 | static Int compare_MC_Chunks(void* n1, void* n2) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 247 | { |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 248 | MC_Chunk* mc1 = *(MC_Chunk**)n1; |
| 249 | MC_Chunk* mc2 = *(MC_Chunk**)n2; |
| 250 | if (mc1->data < mc2->data) return -1; |
| 251 | if (mc1->data > mc2->data) return 1; |
| 252 | return 0; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 253 | } |
| 254 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 255 | #if VG_DEBUG_LEAKCHECK |
| 256 | // Used to sanity-check the fast binary-search mechanism. |
| 257 | static |
| 258 | Int find_chunk_for_OLD ( Addr ptr, |
| 259 | MC_Chunk** chunks, |
| 260 | Int n_chunks ) |
| 261 | |
| 262 | { |
| 263 | Int i; |
| 264 | Addr a_lo, a_hi; |
| 265 | PROF_EVENT(70, "find_chunk_for_OLD"); |
| 266 | for (i = 0; i < n_chunks; i++) { |
| 267 | PROF_EVENT(71, "find_chunk_for_OLD(loop)"); |
| 268 | a_lo = chunks[i]->data; |
| 269 | a_hi = ((Addr)chunks[i]->data) + chunks[i]->szB; |
| 270 | if (a_lo <= ptr && ptr < a_hi) |
| 271 | return i; |
| 272 | } |
| 273 | return -1; |
| 274 | } |
| 275 | #endif |
| 276 | |
| 277 | // Find the i such that ptr points at or inside the block described by |
| 278 | // chunks[i]. Return -1 if none found. This assumes that chunks[] |
| 279 | // has been sorted on the 'data' field. |
| 280 | static |
| 281 | Int find_chunk_for ( Addr ptr, |
| 282 | MC_Chunk** chunks, |
| 283 | Int n_chunks ) |
| 284 | { |
| 285 | Addr a_mid_lo, a_mid_hi; |
| 286 | Int lo, mid, hi, retVal; |
| 287 | // VG_(printf)("find chunk for %p = ", ptr); |
| 288 | retVal = -1; |
| 289 | lo = 0; |
| 290 | hi = n_chunks-1; |
| 291 | while (True) { |
| 292 | // Invariant: current unsearched space is from lo to hi, inclusive. |
| 293 | if (lo > hi) break; // not found |
| 294 | |
| 295 | mid = (lo + hi) / 2; |
| 296 | a_mid_lo = chunks[mid]->data; |
| 297 | a_mid_hi = chunks[mid]->data + chunks[mid]->szB; |
| 298 | // Extent of block 'mid' is [a_mid_lo .. a_mid_hi). |
| 299 | // Special-case zero-sized blocks - treat them as if they had |
| 300 | // size 1. Not doing so causes them to not cover any address |
| 301 | // range at all and so will never be identified as the target of |
| 302 | // any pointer, which causes them to be incorrectly reported as |
| 303 | // definitely leaked. |
| 304 | if (chunks[mid]->szB == 0) |
| 305 | a_mid_hi++; |
| 306 | |
| 307 | if (ptr < a_mid_lo) { |
| 308 | hi = mid-1; |
| 309 | continue; |
| 310 | } |
| 311 | if (ptr >= a_mid_hi) { |
| 312 | lo = mid+1; |
| 313 | continue; |
| 314 | } |
| 315 | tl_assert(ptr >= a_mid_lo && ptr < a_mid_hi); |
| 316 | retVal = mid; |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | # if VG_DEBUG_LEAKCHECK |
| 321 | tl_assert(retVal == find_chunk_for_OLD ( ptr, chunks, n_chunks )); |
| 322 | # endif |
| 323 | // VG_(printf)("%d\n", retVal); |
| 324 | return retVal; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | static MC_Chunk** |
| 329 | find_active_chunks(UInt* pn_chunks) |
| 330 | { |
| 331 | // Our goal is to construct a set of chunks that includes every |
| 332 | // mempool chunk, and every malloc region that *doesn't* contain a |
| 333 | // mempool chunk. |
| 334 | MC_Mempool *mp; |
| 335 | MC_Chunk **mallocs, **chunks, *mc; |
| 336 | UInt n_mallocs, n_chunks, m, s; |
| 337 | Bool *malloc_chunk_holds_a_pool_chunk; |
| 338 | |
| 339 | // First we collect all the malloc chunks into an array and sort it. |
| 340 | // We do this because we want to query the chunks by interior |
| 341 | // pointers, requiring binary search. |
| 342 | mallocs = (MC_Chunk**) VG_(HT_to_array)( MC_(malloc_list), &n_mallocs ); |
| 343 | if (n_mallocs == 0) { |
| 344 | tl_assert(mallocs == NULL); |
| 345 | *pn_chunks = 0; |
| 346 | return NULL; |
| 347 | } |
| 348 | VG_(ssort)(mallocs, n_mallocs, sizeof(VgHashNode*), compare_MC_Chunks); |
| 349 | |
| 350 | // Then we build an array containing a Bool for each malloc chunk, |
| 351 | // indicating whether it contains any mempools. |
| 352 | malloc_chunk_holds_a_pool_chunk = VG_(calloc)( "mc.fas.1", |
| 353 | n_mallocs, sizeof(Bool) ); |
| 354 | n_chunks = n_mallocs; |
| 355 | |
| 356 | // Then we loop over the mempool tables. For each chunk in each |
| 357 | // pool, we set the entry in the Bool array corresponding to the |
| 358 | // malloc chunk containing the mempool chunk. |
| 359 | VG_(HT_ResetIter)(MC_(mempool_list)); |
| 360 | while ( (mp = VG_(HT_Next)(MC_(mempool_list))) ) { |
| 361 | VG_(HT_ResetIter)(mp->chunks); |
| 362 | while ( (mc = VG_(HT_Next)(mp->chunks)) ) { |
| 363 | |
| 364 | // We'll need to record this chunk. |
| 365 | n_chunks++; |
| 366 | |
| 367 | // Possibly invalidate the malloc holding the beginning of this chunk. |
| 368 | m = find_chunk_for(mc->data, mallocs, n_mallocs); |
| 369 | if (m != -1 && malloc_chunk_holds_a_pool_chunk[m] == False) { |
| 370 | tl_assert(n_chunks > 0); |
| 371 | n_chunks--; |
| 372 | malloc_chunk_holds_a_pool_chunk[m] = True; |
| 373 | } |
| 374 | |
| 375 | // Possibly invalidate the malloc holding the end of this chunk. |
| 376 | if (mc->szB > 1) { |
| 377 | m = find_chunk_for(mc->data + (mc->szB - 1), mallocs, n_mallocs); |
| 378 | if (m != -1 && malloc_chunk_holds_a_pool_chunk[m] == False) { |
| 379 | tl_assert(n_chunks > 0); |
| 380 | n_chunks--; |
| 381 | malloc_chunk_holds_a_pool_chunk[m] = True; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | tl_assert(n_chunks > 0); |
| 387 | |
| 388 | // Create final chunk array. |
| 389 | chunks = VG_(malloc)("mc.fas.2", sizeof(VgHashNode*) * (n_chunks)); |
| 390 | s = 0; |
| 391 | |
| 392 | // Copy the mempool chunks and the non-marked malloc chunks into a |
| 393 | // combined array of chunks. |
| 394 | VG_(HT_ResetIter)(MC_(mempool_list)); |
| 395 | while ( (mp = VG_(HT_Next)(MC_(mempool_list))) ) { |
| 396 | VG_(HT_ResetIter)(mp->chunks); |
| 397 | while ( (mc = VG_(HT_Next)(mp->chunks)) ) { |
| 398 | tl_assert(s < n_chunks); |
| 399 | chunks[s++] = mc; |
| 400 | } |
| 401 | } |
| 402 | for (m = 0; m < n_mallocs; ++m) { |
| 403 | if (!malloc_chunk_holds_a_pool_chunk[m]) { |
| 404 | tl_assert(s < n_chunks); |
| 405 | chunks[s++] = mallocs[m]; |
| 406 | } |
| 407 | } |
| 408 | tl_assert(s == n_chunks); |
| 409 | |
| 410 | // Free temporaries. |
| 411 | VG_(free)(mallocs); |
| 412 | VG_(free)(malloc_chunk_holds_a_pool_chunk); |
| 413 | |
| 414 | *pn_chunks = n_chunks; |
| 415 | |
| 416 | return chunks; |
| 417 | } |
| 418 | |
| 419 | /*------------------------------------------------------------*/ |
| 420 | /*--- The leak detector proper. ---*/ |
| 421 | /*------------------------------------------------------------*/ |
| 422 | |
| 423 | // Holds extra info about each block during leak checking. |
| 424 | typedef |
| 425 | struct { |
| 426 | UInt state:2; // Reachedness. |
tom | 1d0f3f6 | 2010-10-04 20:55:21 +0000 | [diff] [blame] | 427 | UInt pending:1; // Scan pending. |
| 428 | SizeT indirect_szB : (sizeof(SizeT)*8)-3; // If Unreached, how many bytes |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 429 | // are unreachable from here. |
| 430 | } |
| 431 | LC_Extra; |
| 432 | |
| 433 | // An array holding pointers to every chunk we're checking. Sorted by address. |
| 434 | static MC_Chunk** lc_chunks; |
| 435 | // How many chunks we're dealing with. |
| 436 | static Int lc_n_chunks; |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 437 | // chunks will be converted and merged in loss record, maintained in lr_table |
| 438 | // lr_table elements are kept from one leak_search to another to implement |
| 439 | // the "print new/changed leaks" client request |
| 440 | static OSet* lr_table; |
| 441 | |
| 442 | // DeltaMode used the last time we called detect_memory_leaks. |
| 443 | // The recorded leak errors must be output using a logic based on this delta_mode. |
| 444 | // The below avoids replicating the delta_mode in each LossRecord. |
| 445 | LeakCheckDeltaMode MC_(detect_memory_leaks_last_delta_mode); |
| 446 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 447 | |
| 448 | // This has the same number of entries as lc_chunks, and each entry |
| 449 | // in lc_chunks corresponds with the entry here (ie. lc_chunks[i] and |
| 450 | // lc_extras[i] describe the same block). |
| 451 | static LC_Extra* lc_extras; |
| 452 | |
| 453 | // Records chunks that are currently being processed. Each element in the |
| 454 | // stack is an index into lc_chunks and lc_extras. Its size is |
| 455 | // 'lc_n_chunks' because in the worst case that's how many chunks could be |
| 456 | // pushed onto it (actually I think the maximum is lc_n_chunks-1 but let's |
| 457 | // be conservative). |
| 458 | static Int* lc_markstack; |
| 459 | // The index of the top element of the stack; -1 if the stack is empty, 0 if |
| 460 | // the stack has one element, 1 if it has two, etc. |
| 461 | static Int lc_markstack_top; |
| 462 | |
| 463 | // Keeps track of how many bytes of memory we've scanned, for printing. |
| 464 | // (Nb: We don't keep track of how many register bytes we've scanned.) |
| 465 | static SizeT lc_scanned_szB; |
| 466 | |
| 467 | |
| 468 | SizeT MC_(bytes_leaked) = 0; |
| 469 | SizeT MC_(bytes_indirect) = 0; |
| 470 | SizeT MC_(bytes_dubious) = 0; |
| 471 | SizeT MC_(bytes_reachable) = 0; |
| 472 | SizeT MC_(bytes_suppressed) = 0; |
| 473 | |
| 474 | SizeT MC_(blocks_leaked) = 0; |
| 475 | SizeT MC_(blocks_indirect) = 0; |
| 476 | SizeT MC_(blocks_dubious) = 0; |
| 477 | SizeT MC_(blocks_reachable) = 0; |
| 478 | SizeT MC_(blocks_suppressed) = 0; |
| 479 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 480 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 481 | // Determines if a pointer is to a chunk. Returns the chunk number et al |
| 482 | // via call-by-reference. |
| 483 | static Bool |
| 484 | lc_is_a_chunk_ptr(Addr ptr, Int* pch_no, MC_Chunk** pch, LC_Extra** pex) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 485 | { |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 486 | Int ch_no; |
| 487 | MC_Chunk* ch; |
| 488 | LC_Extra* ex; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 489 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 490 | // Quick filter. |
| 491 | if (!VG_(am_is_valid_for_client)(ptr, 1, VKI_PROT_READ)) { |
| 492 | return False; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 493 | } else { |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 494 | ch_no = find_chunk_for(ptr, lc_chunks, lc_n_chunks); |
| 495 | tl_assert(ch_no >= -1 && ch_no < lc_n_chunks); |
| 496 | |
| 497 | if (ch_no == -1) { |
| 498 | return False; |
| 499 | } else { |
| 500 | // Ok, we've found a pointer to a chunk. Get the MC_Chunk and its |
| 501 | // LC_Extra. |
| 502 | ch = lc_chunks[ch_no]; |
| 503 | ex = &(lc_extras[ch_no]); |
| 504 | |
| 505 | tl_assert(ptr >= ch->data); |
| 506 | tl_assert(ptr < ch->data + ch->szB + (ch->szB==0 ? 1 : 0)); |
| 507 | |
| 508 | if (VG_DEBUG_LEAKCHECK) |
| 509 | VG_(printf)("ptr=%#lx -> block %d\n", ptr, ch_no); |
| 510 | |
| 511 | *pch_no = ch_no; |
| 512 | *pch = ch; |
| 513 | *pex = ex; |
| 514 | |
| 515 | return True; |
| 516 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 520 | // Push a chunk (well, just its index) onto the mark stack. |
| 521 | static void lc_push(Int ch_no, MC_Chunk* ch) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 522 | { |
tom | 1d0f3f6 | 2010-10-04 20:55:21 +0000 | [diff] [blame] | 523 | if (!lc_extras[ch_no].pending) { |
| 524 | if (0) { |
| 525 | VG_(printf)("pushing %#lx-%#lx\n", ch->data, ch->data + ch->szB); |
| 526 | } |
| 527 | lc_markstack_top++; |
| 528 | tl_assert(lc_markstack_top < lc_n_chunks); |
| 529 | lc_markstack[lc_markstack_top] = ch_no; |
| 530 | tl_assert(!lc_extras[ch_no].pending); |
| 531 | lc_extras[ch_no].pending = True; |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 532 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 533 | } |
| 534 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 535 | // Return the index of the chunk on the top of the mark stack, or -1 if |
| 536 | // there isn't one. |
| 537 | static Bool lc_pop(Int* ret) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 538 | { |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 539 | if (-1 == lc_markstack_top) { |
| 540 | return False; |
| 541 | } else { |
| 542 | tl_assert(0 <= lc_markstack_top && lc_markstack_top < lc_n_chunks); |
| 543 | *ret = lc_markstack[lc_markstack_top]; |
| 544 | lc_markstack_top--; |
tom | 1d0f3f6 | 2010-10-04 20:55:21 +0000 | [diff] [blame] | 545 | tl_assert(lc_extras[*ret].pending); |
| 546 | lc_extras[*ret].pending = False; |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 547 | return True; |
| 548 | } |
| 549 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 550 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 551 | |
| 552 | // If 'ptr' is pointing to a heap-allocated block which hasn't been seen |
| 553 | // before, push it onto the mark stack. |
| 554 | static void |
| 555 | lc_push_without_clique_if_a_chunk_ptr(Addr ptr, Bool is_prior_definite) |
| 556 | { |
| 557 | Int ch_no; |
| 558 | MC_Chunk* ch; |
| 559 | LC_Extra* ex; |
| 560 | |
| 561 | if ( ! lc_is_a_chunk_ptr(ptr, &ch_no, &ch, &ex) ) |
| 562 | return; |
tom | 1d0f3f6 | 2010-10-04 20:55:21 +0000 | [diff] [blame] | 563 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 564 | // Possibly upgrade the state, ie. one of: |
| 565 | // - Unreached --> Possible |
| 566 | // - Unreached --> Reachable |
| 567 | // - Possible --> Reachable |
tom | 1d0f3f6 | 2010-10-04 20:55:21 +0000 | [diff] [blame] | 568 | if (ptr == ch->data && is_prior_definite && ex->state != Reachable) { |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 569 | // 'ptr' points to the start of the block, and the prior node is |
| 570 | // definite, which means that this block is definitely reachable. |
| 571 | ex->state = Reachable; |
| 572 | |
tom | 1d0f3f6 | 2010-10-04 20:55:21 +0000 | [diff] [blame] | 573 | // State has changed to Reachable so (re)scan the block to make |
| 574 | // sure any blocks it points to are correctly marked. |
| 575 | lc_push(ch_no, ch); |
| 576 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 577 | } else if (ex->state == Unreached) { |
| 578 | // Either 'ptr' is a interior-pointer, or the prior node isn't definite, |
| 579 | // which means that we can only mark this block as possibly reachable. |
| 580 | ex->state = Possible; |
tom | 1d0f3f6 | 2010-10-04 20:55:21 +0000 | [diff] [blame] | 581 | |
| 582 | // State has changed to Possible so (re)scan the block to make |
| 583 | // sure any blocks it points to are correctly marked. |
| 584 | lc_push(ch_no, ch); |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
| 588 | static void |
| 589 | lc_push_if_a_chunk_ptr_register(Addr ptr) |
| 590 | { |
| 591 | lc_push_without_clique_if_a_chunk_ptr(ptr, /*is_prior_definite*/True); |
| 592 | } |
| 593 | |
| 594 | // If ptr is pointing to a heap-allocated block which hasn't been seen |
| 595 | // before, push it onto the mark stack. Clique is the index of the |
| 596 | // clique leader. |
| 597 | static void |
| 598 | lc_push_with_clique_if_a_chunk_ptr(Addr ptr, Int clique) |
| 599 | { |
| 600 | Int ch_no; |
| 601 | MC_Chunk* ch; |
| 602 | LC_Extra* ex; |
| 603 | |
| 604 | tl_assert(0 <= clique && clique < lc_n_chunks); |
| 605 | |
| 606 | if ( ! lc_is_a_chunk_ptr(ptr, &ch_no, &ch, &ex) ) |
| 607 | return; |
| 608 | |
| 609 | // If it's not Unreached, it's already been handled so ignore it. |
| 610 | // If ch_no==clique, it's the clique leader, which means this is a cyclic |
| 611 | // structure; again ignore it because it's already been handled. |
| 612 | if (ex->state == Unreached && ch_no != clique) { |
| 613 | // Note that, unlike reachable blocks, we currently don't distinguish |
| 614 | // between start-pointers and interior-pointers here. We probably |
| 615 | // should, though. |
| 616 | ex->state = IndirectLeak; |
| 617 | lc_push(ch_no, ch); |
| 618 | |
| 619 | // Add the block to the clique, and add its size to the |
| 620 | // clique-leader's indirect size. Also, if the new block was |
| 621 | // itself a clique leader, it isn't any more, so add its |
| 622 | // indirect_szB to the new clique leader. |
| 623 | if (VG_DEBUG_CLIQUE) { |
| 624 | if (ex->indirect_szB > 0) |
| 625 | VG_(printf)(" clique %d joining clique %d adding %lu+%lu\n", |
| 626 | ch_no, clique, (SizeT)ch->szB, (SizeT)ex->indirect_szB); |
| 627 | else |
| 628 | VG_(printf)(" block %d joining clique %d adding %lu\n", |
| 629 | ch_no, clique, (SizeT)ch->szB); |
| 630 | } |
| 631 | |
| 632 | lc_extras[clique].indirect_szB += ch->szB; |
| 633 | lc_extras[clique].indirect_szB += ex->indirect_szB; |
| 634 | ex->indirect_szB = 0; // Shouldn't matter. |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | static void |
| 639 | lc_push_if_a_chunk_ptr(Addr ptr, Int clique, Bool is_prior_definite) |
| 640 | { |
| 641 | if (-1 == clique) |
| 642 | lc_push_without_clique_if_a_chunk_ptr(ptr, is_prior_definite); |
| 643 | else |
| 644 | lc_push_with_clique_if_a_chunk_ptr(ptr, clique); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 645 | } |
| 646 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 647 | |
sewardj | 97d3ebb | 2011-04-11 18:36:34 +0000 | [diff] [blame] | 648 | static VG_MINIMAL_JMP_BUF(memscan_jmpbuf); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 649 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 650 | static |
| 651 | void scan_all_valid_memory_catcher ( Int sigNo, Addr addr ) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 652 | { |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 653 | if (0) |
| 654 | VG_(printf)("OUCH! sig=%d addr=%#lx\n", sigNo, addr); |
| 655 | if (sigNo == VKI_SIGSEGV || sigNo == VKI_SIGBUS) |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 656 | VG_MINIMAL_LONGJMP(memscan_jmpbuf); |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | // Scan a block of memory between [start, start+len). This range may |
| 660 | // be bogus, inaccessable, or otherwise strange; we deal with it. For each |
| 661 | // valid aligned word we assume it's a pointer to a chunk a push the chunk |
| 662 | // onto the mark stack if so. |
| 663 | static void |
| 664 | lc_scan_memory(Addr start, SizeT len, Bool is_prior_definite, Int clique) |
| 665 | { |
| 666 | Addr ptr = VG_ROUNDUP(start, sizeof(Addr)); |
njn | 13bfd85 | 2005-06-02 03:52:53 +0000 | [diff] [blame] | 667 | Addr end = VG_ROUNDDN(start+len, sizeof(Addr)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 668 | vki_sigset_t sigmask; |
| 669 | |
| 670 | if (VG_DEBUG_LEAKCHECK) |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 671 | VG_(printf)("scan %#lx-%#lx (%lu)\n", start, end, len); |
| 672 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 673 | VG_(sigprocmask)(VKI_SIG_SETMASK, NULL, &sigmask); |
njn | 695c16e | 2005-03-27 03:40:28 +0000 | [diff] [blame] | 674 | VG_(set_fault_catcher)(scan_all_valid_memory_catcher); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 675 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 676 | // We might be in the middle of a page. Do a cheap check to see if |
| 677 | // it's valid; if not, skip onto the next page. |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 678 | if (!VG_(am_is_valid_for_client)(ptr, sizeof(Addr), VKI_PROT_READ)) |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 679 | ptr = VG_PGROUNDUP(ptr+1); // First page is bad - skip it. |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 680 | |
sewardj | 05fe85e | 2005-04-27 22:46:36 +0000 | [diff] [blame] | 681 | while (ptr < end) { |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 682 | Addr addr; |
| 683 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 684 | // Skip invalid chunks. |
| 685 | if ( ! MC_(is_within_valid_secondary)(ptr) ) { |
| 686 | ptr = VG_ROUNDUP(ptr+1, SM_SIZE); |
| 687 | continue; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 688 | } |
| 689 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 690 | // Look to see if this page seems reasonable. |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 691 | if ((ptr % VKI_PAGE_SIZE) == 0) { |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 692 | if (!VG_(am_is_valid_for_client)(ptr, sizeof(Addr), VKI_PROT_READ)) { |
| 693 | ptr += VKI_PAGE_SIZE; // Bad page - skip it. |
| 694 | continue; |
| 695 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 696 | } |
| 697 | |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 698 | if (VG_MINIMAL_SETJMP(memscan_jmpbuf) == 0) { |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 699 | if ( MC_(is_valid_aligned_word)(ptr) ) { |
| 700 | lc_scanned_szB += sizeof(Addr); |
| 701 | addr = *(Addr *)ptr; |
| 702 | // If we get here, the scanned word is in valid memory. Now |
| 703 | // let's see if its contents point to a chunk. |
| 704 | lc_push_if_a_chunk_ptr(addr, clique, is_prior_definite); |
| 705 | } else if (0 && VG_DEBUG_LEAKCHECK) { |
| 706 | VG_(printf)("%#lx not valid\n", ptr); |
| 707 | } |
| 708 | ptr += sizeof(Addr); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 709 | } else { |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 710 | // We need to restore the signal mask, because we were |
| 711 | // longjmped out of a signal handler. |
| 712 | VG_(sigprocmask)(VKI_SIG_SETMASK, &sigmask, NULL); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 713 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 714 | ptr = VG_PGROUNDUP(ptr+1); // Bad page - skip it. |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
| 718 | VG_(sigprocmask)(VKI_SIG_SETMASK, &sigmask, NULL); |
| 719 | VG_(set_fault_catcher)(NULL); |
| 720 | } |
| 721 | |
sewardj | 45d94cc | 2005-04-20 14:44:11 +0000 | [diff] [blame] | 722 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 723 | // Process the mark stack until empty. |
| 724 | static void lc_process_markstack(Int clique) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 725 | { |
njn | e3675d6 | 2009-05-19 02:08:25 +0000 | [diff] [blame] | 726 | Int top = -1; // shut gcc up |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 727 | Bool is_prior_definite; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 728 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 729 | while (lc_pop(&top)) { |
tom | 1d0f3f6 | 2010-10-04 20:55:21 +0000 | [diff] [blame] | 730 | tl_assert(top >= 0 && top < lc_n_chunks); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 731 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 732 | // See comment about 'is_prior_definite' at the top to understand this. |
| 733 | is_prior_definite = ( Possible != lc_extras[top].state ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 734 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 735 | lc_scan_memory(lc_chunks[top]->data, lc_chunks[top]->szB, |
| 736 | is_prior_definite, clique); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 740 | static Word cmp_LossRecordKey_LossRecord(const void* key, const void* elem) |
| 741 | { |
| 742 | LossRecordKey* a = (LossRecordKey*)key; |
| 743 | LossRecordKey* b = &(((LossRecord*)elem)->key); |
| 744 | |
| 745 | // Compare on states first because that's fast. |
| 746 | if (a->state < b->state) return -1; |
| 747 | if (a->state > b->state) return 1; |
| 748 | // Ok, the states are equal. Now compare the locations, which is slower. |
| 749 | if (VG_(eq_ExeContext)( |
| 750 | MC_(clo_leak_resolution), a->allocated_at, b->allocated_at)) |
| 751 | return 0; |
| 752 | // Different locations. Ordering is arbitrary, just use the ec pointer. |
| 753 | if (a->allocated_at < b->allocated_at) return -1; |
| 754 | if (a->allocated_at > b->allocated_at) return 1; |
| 755 | VG_(tool_panic)("bad LossRecord comparison"); |
| 756 | } |
| 757 | |
| 758 | static Int cmp_LossRecords(void* va, void* vb) |
| 759 | { |
| 760 | LossRecord* lr_a = *(LossRecord**)va; |
| 761 | LossRecord* lr_b = *(LossRecord**)vb; |
| 762 | SizeT total_szB_a = lr_a->szB + lr_a->indirect_szB; |
| 763 | SizeT total_szB_b = lr_b->szB + lr_b->indirect_szB; |
| 764 | |
| 765 | // First compare by sizes. |
| 766 | if (total_szB_a < total_szB_b) return -1; |
| 767 | if (total_szB_a > total_szB_b) return 1; |
| 768 | // If size are equal, compare by states. |
| 769 | if (lr_a->key.state < lr_b->key.state) return -1; |
| 770 | if (lr_a->key.state > lr_b->key.state) return 1; |
njn | e10c7f8 | 2009-05-06 06:52:47 +0000 | [diff] [blame] | 771 | // If they're still equal here, it doesn't matter that much, but we keep |
| 772 | // comparing other things so that regtests are as deterministic as |
| 773 | // possible. So: compare num_blocks. |
| 774 | if (lr_a->num_blocks < lr_b->num_blocks) return -1; |
| 775 | if (lr_a->num_blocks > lr_b->num_blocks) return 1; |
| 776 | // Finally, compare ExeContext addresses... older ones are likely to have |
| 777 | // lower addresses. |
| 778 | if (lr_a->key.allocated_at < lr_b->key.allocated_at) return -1; |
| 779 | if (lr_a->key.allocated_at > lr_b->key.allocated_at) return 1; |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 780 | return 0; |
| 781 | } |
| 782 | |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 783 | static void print_results(ThreadId tid, LeakCheckParams lcp) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 784 | { |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 785 | Int i, n_lossrecords; |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 786 | LossRecord** lr_array; |
| 787 | LossRecord* lr; |
| 788 | Bool is_suppressed; |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 789 | SizeT old_bytes_leaked = MC_(bytes_leaked); /* to report delta in summary */ |
| 790 | SizeT old_bytes_indirect = MC_(bytes_indirect); |
| 791 | SizeT old_bytes_dubious = MC_(bytes_dubious); |
| 792 | SizeT old_bytes_reachable = MC_(bytes_reachable); |
| 793 | SizeT old_bytes_suppressed = MC_(bytes_suppressed); |
| 794 | SizeT old_blocks_leaked = MC_(blocks_leaked); |
| 795 | SizeT old_blocks_indirect = MC_(blocks_indirect); |
| 796 | SizeT old_blocks_dubious = MC_(blocks_dubious); |
| 797 | SizeT old_blocks_reachable = MC_(blocks_reachable); |
| 798 | SizeT old_blocks_suppressed = MC_(blocks_suppressed); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 799 | |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 800 | if (lr_table == NULL) |
| 801 | // Create the lr_table, which holds the loss records. |
| 802 | // If the lr_table already exists, it means it contains |
| 803 | // loss_records from the previous leak search. The old_* |
| 804 | // values in these records are used to implement the |
| 805 | // leak check delta mode |
| 806 | lr_table = |
| 807 | VG_(OSetGen_Create)(offsetof(LossRecord, key), |
| 808 | cmp_LossRecordKey_LossRecord, |
| 809 | VG_(malloc), "mc.pr.1", |
| 810 | VG_(free)); |
| 811 | |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 812 | |
| 813 | // Convert the chunks into loss records, merging them where appropriate. |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 814 | for (i = 0; i < lc_n_chunks; i++) { |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 815 | MC_Chunk* ch = lc_chunks[i]; |
| 816 | LC_Extra* ex = &(lc_extras)[i]; |
| 817 | LossRecord* old_lr; |
| 818 | LossRecordKey lrkey; |
| 819 | lrkey.state = ex->state; |
| 820 | lrkey.allocated_at = ch->where; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 821 | |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 822 | old_lr = VG_(OSetGen_Lookup)(lr_table, &lrkey); |
| 823 | if (old_lr) { |
| 824 | // We found an existing loss record matching this chunk. Update the |
| 825 | // loss record's details in-situ. This is safe because we don't |
| 826 | // change the elements used as the OSet key. |
| 827 | old_lr->szB += ch->szB; |
| 828 | old_lr->indirect_szB += ex->indirect_szB; |
| 829 | old_lr->num_blocks++; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 830 | } else { |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 831 | // No existing loss record matches this chunk. Create a new loss |
| 832 | // record, initialise it from the chunk, and insert it into lr_table. |
| 833 | lr = VG_(OSetGen_AllocNode)(lr_table, sizeof(LossRecord)); |
| 834 | lr->key = lrkey; |
| 835 | lr->szB = ch->szB; |
| 836 | lr->indirect_szB = ex->indirect_szB; |
| 837 | lr->num_blocks = 1; |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 838 | lr->old_szB = 0; |
| 839 | lr->old_indirect_szB = 0; |
| 840 | lr->old_num_blocks = 0; |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 841 | VG_(OSetGen_Insert)(lr_table, lr); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 842 | } |
| 843 | } |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 844 | n_lossrecords = VG_(OSetGen_Size)(lr_table); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 845 | |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 846 | // Create an array of pointers to the loss records. |
| 847 | lr_array = VG_(malloc)("mc.pr.2", n_lossrecords * sizeof(LossRecord*)); |
| 848 | i = 0; |
| 849 | VG_(OSetGen_ResetIter)(lr_table); |
| 850 | while ( (lr = VG_(OSetGen_Next)(lr_table)) ) { |
| 851 | lr_array[i++] = lr; |
| 852 | } |
| 853 | tl_assert(i == n_lossrecords); |
| 854 | |
| 855 | // Sort the array by loss record sizes. |
| 856 | VG_(ssort)(lr_array, n_lossrecords, sizeof(LossRecord*), |
| 857 | cmp_LossRecords); |
| 858 | |
| 859 | // Zero totals. |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 860 | MC_(blocks_leaked) = MC_(bytes_leaked) = 0; |
| 861 | MC_(blocks_indirect) = MC_(bytes_indirect) = 0; |
| 862 | MC_(blocks_dubious) = MC_(bytes_dubious) = 0; |
| 863 | MC_(blocks_reachable) = MC_(bytes_reachable) = 0; |
| 864 | MC_(blocks_suppressed) = MC_(bytes_suppressed) = 0; |
| 865 | |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 866 | // Print the loss records (in size order) and collect summary stats. |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 867 | for (i = 0; i < n_lossrecords; i++) { |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 868 | Bool count_as_error, print_record, delta_considered; |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 869 | // Rules for printing: |
| 870 | // - We don't show suppressed loss records ever (and that's controlled |
| 871 | // within the error manager). |
| 872 | // - We show non-suppressed loss records that are not "reachable" if |
| 873 | // --leak-check=yes. |
| 874 | // - We show all non-suppressed loss records if --leak-check=yes and |
| 875 | // --show-reachable=yes. |
| 876 | // |
| 877 | // Nb: here "reachable" means Reachable *or* IndirectLeak; note that |
| 878 | // this is different to "still reachable" used elsewhere because it |
| 879 | // includes indirectly lost blocks! |
| 880 | // |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 881 | lr = lr_array[i]; |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 882 | switch (lcp.deltamode) { |
| 883 | case LCD_Any: |
| 884 | delta_considered = lr->num_blocks > 0; |
| 885 | break; |
| 886 | case LCD_Increased: |
| 887 | delta_considered |
| 888 | = lr_array[i]->szB > lr_array[i]->old_szB |
| 889 | || lr_array[i]->indirect_szB > lr_array[i]->old_indirect_szB |
| 890 | || lr->num_blocks > lr->old_num_blocks; |
| 891 | break; |
| 892 | case LCD_Changed: |
| 893 | delta_considered = lr_array[i]->szB != lr_array[i]->old_szB |
| 894 | || lr_array[i]->indirect_szB != lr_array[i]->old_indirect_szB |
| 895 | || lr->num_blocks != lr->old_num_blocks; |
| 896 | break; |
| 897 | default: |
| 898 | tl_assert(0); |
| 899 | } |
| 900 | |
| 901 | print_record = lcp.mode == LC_Full && delta_considered && |
| 902 | ( lcp.show_reachable || |
njn | 2667055 | 2009-08-13 00:02:30 +0000 | [diff] [blame] | 903 | Unreached == lr->key.state || |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 904 | ( lcp.show_possibly_lost && |
bart | 3cedf57 | 2010-08-26 10:56:27 +0000 | [diff] [blame] | 905 | Possible == lr->key.state ) ); |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 906 | // We don't count a leaks as errors with lcp.mode==LC_Summary. |
njn | 2667055 | 2009-08-13 00:02:30 +0000 | [diff] [blame] | 907 | // Otherwise you can get high error counts with few or no error |
| 908 | // messages, which can be confusing. Also, you could argue that |
| 909 | // indirect leaks should be counted as errors, but it seems better to |
| 910 | // make the counting criteria similar to the printing criteria. So we |
| 911 | // don't count them. |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 912 | count_as_error = lcp.mode == LC_Full && delta_considered && |
njn | 2667055 | 2009-08-13 00:02:30 +0000 | [diff] [blame] | 913 | ( Unreached == lr->key.state || |
| 914 | Possible == lr->key.state ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 915 | is_suppressed = |
njn | 18afe5d | 2009-08-10 08:25:39 +0000 | [diff] [blame] | 916 | MC_(record_leak_error) ( tid, i+1, n_lossrecords, lr, print_record, |
| 917 | count_as_error ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 918 | |
| 919 | if (is_suppressed) { |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 920 | MC_(blocks_suppressed) += lr->num_blocks; |
| 921 | MC_(bytes_suppressed) += lr->szB; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 922 | |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 923 | } else if (Unreached == lr->key.state) { |
| 924 | MC_(blocks_leaked) += lr->num_blocks; |
| 925 | MC_(bytes_leaked) += lr->szB; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 926 | |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 927 | } else if (IndirectLeak == lr->key.state) { |
| 928 | MC_(blocks_indirect) += lr->num_blocks; |
| 929 | MC_(bytes_indirect) += lr->szB; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 930 | |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 931 | } else if (Possible == lr->key.state) { |
| 932 | MC_(blocks_dubious) += lr->num_blocks; |
| 933 | MC_(bytes_dubious) += lr->szB; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 934 | |
njn | 29a5c01 | 2009-05-06 06:15:55 +0000 | [diff] [blame] | 935 | } else if (Reachable == lr->key.state) { |
| 936 | MC_(blocks_reachable) += lr->num_blocks; |
| 937 | MC_(bytes_reachable) += lr->szB; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 938 | |
| 939 | } else { |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 940 | VG_(tool_panic)("unknown loss mode"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 941 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 942 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 943 | |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 944 | for (i = 0; i < n_lossrecords; i++) |
| 945 | { |
| 946 | if (lr->num_blocks == 0) |
| 947 | // remove from lr_table the old loss_records with 0 bytes found |
| 948 | VG_(OSetGen_Remove) (lr_table, &lr_array[i]->key); |
| 949 | else |
| 950 | { |
| 951 | // move the leak sizes to old_* and zero the current sizes |
| 952 | // for next leak search |
| 953 | lr_array[i]->old_szB = lr_array[i]->szB; |
| 954 | lr_array[i]->old_indirect_szB = lr_array[i]->indirect_szB; |
| 955 | lr_array[i]->old_num_blocks = lr_array[i]->num_blocks; |
| 956 | lr_array[i]->szB = 0; |
| 957 | lr_array[i]->indirect_szB = 0; |
| 958 | lr_array[i]->num_blocks = 0; |
| 959 | } |
| 960 | } |
| 961 | VG_(free)(lr_array); |
| 962 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 963 | if (VG_(clo_verbosity) > 0 && !VG_(clo_xml)) { |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 964 | char d_bytes[20]; |
| 965 | char d_blocks[20]; |
| 966 | |
sewardj | 6b523cd | 2009-07-15 14:49:40 +0000 | [diff] [blame] | 967 | VG_(umsg)("LEAK SUMMARY:\n"); |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 968 | VG_(umsg)(" definitely lost: %'lu%s bytes in %'lu%s blocks\n", |
| 969 | MC_(bytes_leaked), |
| 970 | MC_(snprintf_delta) (d_bytes, 20, MC_(bytes_leaked), old_bytes_leaked, lcp.deltamode), |
| 971 | MC_(blocks_leaked), |
| 972 | MC_(snprintf_delta) (d_blocks, 20, MC_(blocks_leaked), old_blocks_leaked, lcp.deltamode)); |
| 973 | VG_(umsg)(" indirectly lost: %'lu%s bytes in %'lu%s blocks\n", |
| 974 | MC_(bytes_indirect), |
| 975 | MC_(snprintf_delta) (d_bytes, 20, MC_(bytes_indirect), old_bytes_indirect, lcp.deltamode), |
| 976 | MC_(blocks_indirect), |
| 977 | MC_(snprintf_delta) (d_blocks, 20, MC_(blocks_indirect), old_blocks_indirect, lcp.deltamode) ); |
| 978 | VG_(umsg)(" possibly lost: %'lu%s bytes in %'lu%s blocks\n", |
| 979 | MC_(bytes_dubious), |
| 980 | MC_(snprintf_delta) (d_bytes, 20, MC_(bytes_dubious), old_bytes_dubious, lcp.deltamode), |
| 981 | MC_(blocks_dubious), |
| 982 | MC_(snprintf_delta) (d_blocks, 20, MC_(blocks_dubious), old_blocks_dubious, lcp.deltamode) ); |
| 983 | VG_(umsg)(" still reachable: %'lu%s bytes in %'lu%s blocks\n", |
| 984 | MC_(bytes_reachable), |
| 985 | MC_(snprintf_delta) (d_bytes, 20, MC_(bytes_reachable), old_bytes_reachable, lcp.deltamode), |
| 986 | MC_(blocks_reachable), |
| 987 | MC_(snprintf_delta) (d_blocks, 20, MC_(blocks_reachable), old_blocks_reachable, lcp.deltamode) ); |
| 988 | VG_(umsg)(" suppressed: %'lu%s bytes in %'lu%s blocks\n", |
| 989 | MC_(bytes_suppressed), |
| 990 | MC_(snprintf_delta) (d_bytes, 20, MC_(bytes_suppressed), old_bytes_suppressed, lcp.deltamode), |
| 991 | MC_(blocks_suppressed), |
| 992 | MC_(snprintf_delta) (d_blocks, 20, MC_(blocks_suppressed), old_blocks_suppressed, lcp.deltamode) ); |
| 993 | if (lcp.mode != LC_Full && |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 994 | (MC_(blocks_leaked) + MC_(blocks_indirect) + |
| 995 | MC_(blocks_dubious) + MC_(blocks_reachable)) > 0) { |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 996 | if (lcp.requested_by_monitor_command) |
| 997 | VG_(umsg)("To see details of leaked memory, give 'full' arg to mc.leak_check\n"); |
| 998 | else |
| 999 | VG_(umsg)("Rerun with --leak-check=full to see details " |
| 1000 | "of leaked memory\n"); |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1001 | } |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 1002 | if (lcp.mode == LC_Full && |
| 1003 | MC_(blocks_reachable) > 0 && !lcp.show_reachable) |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1004 | { |
sewardj | 6b523cd | 2009-07-15 14:49:40 +0000 | [diff] [blame] | 1005 | VG_(umsg)("Reachable blocks (those to which a pointer " |
| 1006 | "was found) are not shown.\n"); |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 1007 | if (lcp.requested_by_monitor_command) |
| 1008 | VG_(umsg)("To see them, add 'reachable any' args to mc.leak_check\n"); |
| 1009 | else |
| 1010 | VG_(umsg)("To see them, rerun with: --leak-check=full " |
| 1011 | "--show-reachable=yes\n"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1012 | } |
njn | b6267bd | 2009-08-12 00:14:16 +0000 | [diff] [blame] | 1013 | VG_(umsg)("\n"); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1017 | /*------------------------------------------------------------*/ |
| 1018 | /*--- Top-level entry point. ---*/ |
| 1019 | /*------------------------------------------------------------*/ |
sewardj | 3cf26a5 | 2006-07-27 23:48:53 +0000 | [diff] [blame] | 1020 | |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 1021 | void MC_(detect_memory_leaks) ( ThreadId tid, LeakCheckParams lcp) |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1022 | { |
njn | b965efb | 2009-08-10 07:36:54 +0000 | [diff] [blame] | 1023 | Int i, j; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1024 | |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 1025 | tl_assert(lcp.mode != LC_Off); |
| 1026 | |
| 1027 | MC_(detect_memory_leaks_last_delta_mode) = lcp.deltamode; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1028 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1029 | // Get the chunks, stop if there were none. |
| 1030 | lc_chunks = find_active_chunks(&lc_n_chunks); |
| 1031 | if (lc_n_chunks == 0) { |
| 1032 | tl_assert(lc_chunks == NULL); |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 1033 | if (lr_table != NULL) { |
| 1034 | // forget the previous recorded LossRecords as next leak search will in any case |
| 1035 | // just create new leaks. |
| 1036 | // Maybe it would be better to rather call print_result ? |
| 1037 | // (at least when leak decrease are requested) |
| 1038 | // This will then output all LossRecords with a size decreasing to 0 |
| 1039 | VG_(OSetGen_Destroy) (lr_table); |
| 1040 | } |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 1041 | if (VG_(clo_verbosity) >= 1 && !VG_(clo_xml)) { |
njn | b6267bd | 2009-08-12 00:14:16 +0000 | [diff] [blame] | 1042 | VG_(umsg)("All heap blocks were freed -- no leaks are possible\n"); |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 1043 | VG_(umsg)("\n"); |
sewardj | 37d06f2 | 2003-09-17 21:48:26 +0000 | [diff] [blame] | 1044 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1045 | return; |
| 1046 | } |
| 1047 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1048 | // Sort the array so blocks are in ascending order in memory. |
| 1049 | VG_(ssort)(lc_chunks, lc_n_chunks, sizeof(VgHashNode*), compare_MC_Chunks); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1050 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1051 | // Sanity check -- make sure they're in order. |
| 1052 | for (i = 0; i < lc_n_chunks-1; i++) { |
| 1053 | tl_assert( lc_chunks[i]->data <= lc_chunks[i+1]->data); |
| 1054 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1055 | |
njn | b965efb | 2009-08-10 07:36:54 +0000 | [diff] [blame] | 1056 | // Sanity check -- make sure they don't overlap. The one exception is that |
| 1057 | // we allow a MALLOCLIKE block to sit entirely within a malloc() block. |
| 1058 | // This is for bug 100628. If this occurs, we ignore the malloc() block |
| 1059 | // for leak-checking purposes. This is a hack and probably should be done |
| 1060 | // better, but at least it's consistent with mempools (which are treated |
| 1061 | // like this in find_active_chunks). Mempools have a separate VgHashTable |
| 1062 | // for mempool chunks, but if custom-allocated blocks are put in a separate |
| 1063 | // table from normal heap blocks it makes free-mismatch checking more |
| 1064 | // difficult. |
| 1065 | // |
| 1066 | // If this check fails, it probably means that the application |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1067 | // has done something stupid with VALGRIND_MALLOCLIKE_BLOCK client |
njn | b965efb | 2009-08-10 07:36:54 +0000 | [diff] [blame] | 1068 | // requests, eg. has made overlapping requests (which are |
| 1069 | // nonsensical), or used VALGRIND_MALLOCLIKE_BLOCK for stack locations; |
| 1070 | // again nonsensical. |
| 1071 | // |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1072 | for (i = 0; i < lc_n_chunks-1; i++) { |
| 1073 | MC_Chunk* ch1 = lc_chunks[i]; |
| 1074 | MC_Chunk* ch2 = lc_chunks[i+1]; |
njn | b965efb | 2009-08-10 07:36:54 +0000 | [diff] [blame] | 1075 | |
| 1076 | Addr start1 = ch1->data; |
| 1077 | Addr start2 = ch2->data; |
| 1078 | Addr end1 = ch1->data + ch1->szB - 1; |
| 1079 | Addr end2 = ch2->data + ch2->szB - 1; |
| 1080 | Bool isCustom1 = ch1->allockind == MC_AllocCustom; |
| 1081 | Bool isCustom2 = ch2->allockind == MC_AllocCustom; |
| 1082 | |
| 1083 | if (end1 < start2) { |
| 1084 | // Normal case - no overlap. |
| 1085 | |
| 1086 | // We used to allow exact duplicates, I'm not sure why. --njn |
| 1087 | //} else if (start1 == start2 && end1 == end2) { |
| 1088 | // Degenerate case: exact duplicates. |
| 1089 | |
| 1090 | } else if (start1 >= start2 && end1 <= end2 && isCustom1 && !isCustom2) { |
| 1091 | // Block i is MALLOCLIKE and entirely within block i+1. |
| 1092 | // Remove block i+1. |
| 1093 | for (j = i+1; j < lc_n_chunks-1; j++) { |
| 1094 | lc_chunks[j] = lc_chunks[j+1]; |
| 1095 | } |
| 1096 | lc_n_chunks--; |
| 1097 | |
| 1098 | } else if (start2 >= start1 && end2 <= end1 && isCustom2 && !isCustom1) { |
| 1099 | // Block i+1 is MALLOCLIKE and entirely within block i. |
| 1100 | // Remove block i. |
| 1101 | for (j = i; j < lc_n_chunks-1; j++) { |
| 1102 | lc_chunks[j] = lc_chunks[j+1]; |
| 1103 | } |
| 1104 | lc_n_chunks--; |
| 1105 | |
| 1106 | } else { |
| 1107 | VG_(umsg)("Block 0x%lx..0x%lx overlaps with block 0x%lx..0x%lx", |
bart | 3c4fa9f | 2011-05-09 10:46:55 +0000 | [diff] [blame] | 1108 | start1, end1, start2, end2); |
njn | b965efb | 2009-08-10 07:36:54 +0000 | [diff] [blame] | 1109 | VG_(umsg)("This is usually caused by using VALGRIND_MALLOCLIKE_BLOCK"); |
| 1110 | VG_(umsg)("in an inappropriate way."); |
| 1111 | tl_assert (0); |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1112 | } |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | // Initialise lc_extras. |
| 1116 | lc_extras = VG_(malloc)( "mc.dml.2", lc_n_chunks * sizeof(LC_Extra) ); |
| 1117 | for (i = 0; i < lc_n_chunks; i++) { |
| 1118 | lc_extras[i].state = Unreached; |
tom | 1d0f3f6 | 2010-10-04 20:55:21 +0000 | [diff] [blame] | 1119 | lc_extras[i].pending = False; |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1120 | lc_extras[i].indirect_szB = 0; |
| 1121 | } |
| 1122 | |
| 1123 | // Initialise lc_markstack. |
| 1124 | lc_markstack = VG_(malloc)( "mc.dml.2", lc_n_chunks * sizeof(Int) ); |
| 1125 | for (i = 0; i < lc_n_chunks; i++) { |
| 1126 | lc_markstack[i] = -1; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1127 | } |
| 1128 | lc_markstack_top = -1; |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1129 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1130 | // Verbosity. |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 1131 | if (VG_(clo_verbosity) > 1 && !VG_(clo_xml)) { |
njn | b6267bd | 2009-08-12 00:14:16 +0000 | [diff] [blame] | 1132 | VG_(umsg)( "Searching for pointers to %'d not-freed blocks\n", |
sewardj | 6b523cd | 2009-07-15 14:49:40 +0000 | [diff] [blame] | 1133 | lc_n_chunks ); |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 1134 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1135 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1136 | // Scan the memory root-set, pushing onto the mark stack any blocks |
| 1137 | // pointed to. |
| 1138 | { |
| 1139 | Int n_seg_starts; |
njn | ac1e033 | 2009-05-08 00:39:31 +0000 | [diff] [blame] | 1140 | Addr* seg_starts = VG_(get_segment_starts)( &n_seg_starts ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1141 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1142 | tl_assert(seg_starts && n_seg_starts > 0); |
sewardj | de3ad73 | 2006-07-27 23:12:17 +0000 | [diff] [blame] | 1143 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1144 | lc_scanned_szB = 0; |
sewardj | de3ad73 | 2006-07-27 23:12:17 +0000 | [diff] [blame] | 1145 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1146 | // VG_(am_show_nsegments)( 0, "leakcheck"); |
| 1147 | for (i = 0; i < n_seg_starts; i++) { |
| 1148 | SizeT seg_size; |
| 1149 | NSegment const* seg = VG_(am_find_nsegment)( seg_starts[i] ); |
| 1150 | tl_assert(seg); |
| 1151 | |
| 1152 | if (seg->kind != SkFileC && seg->kind != SkAnonC) continue; |
| 1153 | if (!(seg->hasR && seg->hasW)) continue; |
| 1154 | if (seg->isCH) continue; |
| 1155 | |
| 1156 | // Don't poke around in device segments as this may cause |
| 1157 | // hangs. Exclude /dev/zero just in case someone allocated |
| 1158 | // memory by explicitly mapping /dev/zero. |
| 1159 | if (seg->kind == SkFileC |
| 1160 | && (VKI_S_ISCHR(seg->mode) || VKI_S_ISBLK(seg->mode))) { |
| 1161 | HChar* dev_name = VG_(am_get_filename)( (NSegment*)seg ); |
| 1162 | if (dev_name && 0 == VG_(strcmp)(dev_name, "/dev/zero")) { |
| 1163 | // Don't skip /dev/zero. |
| 1164 | } else { |
| 1165 | // Skip this device mapping. |
| 1166 | continue; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | if (0) |
| 1171 | VG_(printf)("ACCEPT %2d %#lx %#lx\n", i, seg->start, seg->end); |
| 1172 | |
| 1173 | // Scan the segment. We use -1 for the clique number, because this |
| 1174 | // is a root-set. |
| 1175 | seg_size = seg->end - seg->start + 1; |
| 1176 | if (VG_(clo_verbosity) > 2) { |
| 1177 | VG_(message)(Vg_DebugMsg, |
sewardj | 6b523cd | 2009-07-15 14:49:40 +0000 | [diff] [blame] | 1178 | " Scanning root segment: %#lx..%#lx (%lu)\n", |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1179 | seg->start, seg->end, seg_size); |
| 1180 | } |
| 1181 | lc_scan_memory(seg->start, seg_size, /*is_prior_definite*/True, -1); |
| 1182 | } |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1183 | } |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1184 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1185 | // Scan GP registers for chunk pointers. |
| 1186 | VG_(apply_to_GP_regs)(lc_push_if_a_chunk_ptr_register); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1187 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1188 | // Process the pushed blocks. After this, every block that is reachable |
| 1189 | // from the root-set has been traced. |
| 1190 | lc_process_markstack(/*clique*/-1); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1191 | |
njn | b6267bd | 2009-08-12 00:14:16 +0000 | [diff] [blame] | 1192 | if (VG_(clo_verbosity) > 1 && !VG_(clo_xml)) { |
| 1193 | VG_(umsg)("Checked %'lu bytes\n", lc_scanned_szB); |
| 1194 | VG_(umsg)( "\n" ); |
| 1195 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1196 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1197 | // Trace all the leaked blocks to determine which are directly leaked and |
| 1198 | // which are indirectly leaked. For each Unreached block, push it onto |
| 1199 | // the mark stack, and find all the as-yet-Unreached blocks reachable |
| 1200 | // from it. These form a clique and are marked IndirectLeak, and their |
| 1201 | // size is added to the clique leader's indirect size. If one of the |
| 1202 | // found blocks was itself a clique leader (from a previous clique), then |
| 1203 | // the cliques are merged. |
| 1204 | for (i = 0; i < lc_n_chunks; i++) { |
| 1205 | MC_Chunk* ch = lc_chunks[i]; |
| 1206 | LC_Extra* ex = &(lc_extras[i]); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1207 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1208 | if (VG_DEBUG_CLIQUE) |
| 1209 | VG_(printf)("cliques: %d at %#lx -> Loss state %d\n", |
| 1210 | i, ch->data, ex->state); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1211 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1212 | tl_assert(lc_markstack_top == -1); |
| 1213 | |
| 1214 | if (ex->state == Unreached) { |
| 1215 | if (VG_DEBUG_CLIQUE) |
| 1216 | VG_(printf)("%d: gathering clique %#lx\n", i, ch->data); |
| 1217 | |
| 1218 | // Push this Unreached block onto the stack and process it. |
| 1219 | lc_push(i, ch); |
| 1220 | lc_process_markstack(i); |
| 1221 | |
| 1222 | tl_assert(lc_markstack_top == -1); |
| 1223 | tl_assert(ex->state == Unreached); |
nethercote | 0f19bce | 2003-12-02 10:17:44 +0000 | [diff] [blame] | 1224 | } |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1225 | } |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1226 | |
sewardj | c8bd1df | 2011-06-26 12:41:33 +0000 | [diff] [blame] | 1227 | print_results( tid, lcp); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1228 | |
njn | 8225cc0 | 2009-03-09 22:52:24 +0000 | [diff] [blame] | 1229 | VG_(free) ( lc_chunks ); |
| 1230 | VG_(free) ( lc_extras ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1231 | VG_(free) ( lc_markstack ); |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | /*--------------------------------------------------------------------*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1235 | /*--- end ---*/ |
njn | 43c799e | 2003-04-08 00:08:52 +0000 | [diff] [blame] | 1236 | /*--------------------------------------------------------------------*/ |
| 1237 | |