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