sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3 | /*--- Memory-related stuff: segment initialisation and tracking, ---*/ |
| 4 | /*--- stack operations ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 5 | /*--- vg_memory.c ---*/ |
| 6 | /*--------------------------------------------------------------------*/ |
| 7 | |
| 8 | /* |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 9 | This file is part of Valgrind, an extensible x86 protected-mode |
| 10 | emulator for monitoring program execution on x86-Unixes. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 11 | |
| 12 | Copyright (C) 2000-2002 Julian Seward |
| 13 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 14 | |
| 15 | This program is free software; you can redistribute it and/or |
| 16 | modify it under the terms of the GNU General Public License as |
| 17 | published by the Free Software Foundation; either version 2 of the |
| 18 | License, or (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 28 | 02111-1307, USA. |
| 29 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 30 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #include "vg_include.h" |
| 34 | |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 35 | /* Define to debug the memory-leak-detector. */ |
| 36 | /* #define VG_DEBUG_LEAKCHECK */ |
| 37 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 38 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 39 | /*--------------------------------------------------------------*/ |
| 40 | /*--- Initialise program data/text etc on program startup. ---*/ |
| 41 | /*--------------------------------------------------------------*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 42 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 43 | typedef |
| 44 | struct _ExeSeg { |
| 45 | Addr start; |
| 46 | UInt size; |
| 47 | struct _ExeSeg* next; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 48 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 49 | ExeSeg; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 50 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 51 | /* The list of current executable segments loaded. Required so that when a |
| 52 | segment is munmap'd, if it's executable we can recognise it as such and |
| 53 | invalidate translations for it, and drop any basic-block specific |
| 54 | information being stored. If symbols are being used, this list will have |
| 55 | the same segments recorded in it as the SegInfo symbols list (but much |
| 56 | less information about each segment). |
| 57 | */ |
| 58 | static ExeSeg* exeSegsHead = NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 59 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 60 | /* Prepend it -- mmaps/munmaps likely to follow a stack pattern(?) so this |
| 61 | is good. |
| 62 | Also check no segments overlap, which would be very bad. Check is linear |
| 63 | for each seg added (quadratic overall) but the total number should be |
| 64 | small (konqueror has around 50 --njn). */ |
| 65 | static void add_exe_segment_to_list( a, len ) |
| 66 | { |
| 67 | Addr lo = a; |
| 68 | Addr hi = a + len - 1; |
| 69 | ExeSeg* es; |
| 70 | ExeSeg* es2; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 71 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 72 | /* Prepend it */ |
| 73 | es = (ExeSeg*)VG_(arena_malloc)(VG_AR_CORE, sizeof(ExeSeg)); |
| 74 | es->start = a; |
| 75 | es->size = len; |
| 76 | es->next = exeSegsHead; |
| 77 | exeSegsHead = es; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 78 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 79 | /* Check there's no overlap with the rest of the list */ |
| 80 | for (es2 = es->next; es2 != NULL; es2 = es2->next) { |
| 81 | Addr lo2 = es2->start; |
| 82 | Addr hi2 = es2->start + es2->size - 1; |
| 83 | Bool overlap; |
| 84 | vg_assert(lo < hi); |
| 85 | vg_assert(lo2 < hi2); |
| 86 | /* the main assertion */ |
| 87 | overlap = (lo <= lo2 && lo2 <= hi) |
| 88 | || (lo <= hi2 && hi2 <= hi); |
| 89 | if (overlap) { |
| 90 | VG_(printf)("\n\nOVERLAPPING EXE SEGMENTS\n" |
| 91 | " new: start %p, size %d\n" |
| 92 | " old: start %p, size %d\n\n", |
| 93 | es->start, es->size, es2->start, es2->size ); |
| 94 | vg_assert(! overlap); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 95 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 96 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static Bool remove_if_exe_segment_from_list( Addr a, UInt len ) |
| 100 | { |
| 101 | ExeSeg **prev_next_ptr = & exeSegsHead, |
| 102 | *curr = exeSegsHead; |
| 103 | |
| 104 | while (True) { |
| 105 | if (curr == NULL) break; |
| 106 | if (a == curr->start) break; |
| 107 | prev_next_ptr = &curr->next; |
| 108 | curr = curr->next; |
| 109 | } |
| 110 | if (curr == NULL) |
| 111 | return False; |
| 112 | |
| 113 | vg_assert(*prev_next_ptr == curr); |
| 114 | |
| 115 | *prev_next_ptr = curr->next; |
| 116 | |
| 117 | VG_(arena_free)(VG_AR_CORE, curr); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 118 | return True; |
| 119 | } |
| 120 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 121 | /* Records the exe segment in the ExeSeg list (checking for overlaps), and |
| 122 | reads debug info if required. Note the entire /proc/pid/maps file is |
| 123 | read for the debug info, but it just reads symbols for newly added exe |
| 124 | segments. This is required to find out their names if they have one. So |
| 125 | we don't use this at startup because it's overkill and can screw reading |
| 126 | of /proc/pid/maps. |
| 127 | */ |
| 128 | void VG_(new_exe_segment) ( Addr a, UInt len ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 129 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 130 | add_exe_segment_to_list( a, len ); |
| 131 | VG_(maybe_read_symbols)(); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 132 | } |
| 133 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 134 | /* Invalidate translations as necessary (also discarding any basic |
| 135 | block-specific info retained by the skin) and unload any debug |
| 136 | symbols. */ |
| 137 | // Nb: remove_if_exe_segment_from_list() and VG_(maybe_unload_symbols)() |
| 138 | // both ignore 'len', but that seems that's ok for most programs... see |
| 139 | // comment above vg_syscalls.c:mmap_segment() et al for more details. |
| 140 | void VG_(remove_if_exe_segment) ( Addr a, UInt len ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 141 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 142 | if (remove_if_exe_segment_from_list( a, len )) { |
| 143 | VG_(invalidate_translations) ( a, len ); |
| 144 | VG_(maybe_unload_symbols) ( a, len ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 149 | static |
| 150 | void startup_segment_callback ( Addr start, UInt size, |
| 151 | Char rr, Char ww, Char xx, |
| 152 | UInt foffset, UChar* filename ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 153 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 154 | UInt r_esp; |
| 155 | Bool is_stack_segment; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 156 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 157 | if (0) |
| 158 | VG_(message)(Vg_DebugMsg, |
| 159 | "initial map %8x-%8x %c%c%c? %8x (%d) (%s)", |
| 160 | start,start+size,rr,ww,xx,foffset, |
| 161 | size, filename?filename:(UChar*)"NULL"); |
| 162 | |
| 163 | if (rr != 'r' && xx != 'x' && ww != 'w') { |
sewardj | 6e4234e | 2002-10-06 00:28:21 +0000 | [diff] [blame] | 164 | /* Implausible as it seems, R H 6.2 generates such segments: |
| 165 | 40067000-400ac000 r-xp 00000000 08:05 320686 /usr/X11R6/lib/libXt.so.6.0 |
| 166 | 400ac000-400ad000 ---p 00045000 08:05 320686 /usr/X11R6/lib/libXt.so.6.0 |
| 167 | 400ad000-400b0000 rw-p 00045000 08:05 320686 /usr/X11R6/lib/libXt.so.6.0 |
| 168 | when running xedit. So just ignore them. */ |
| 169 | if (0) |
| 170 | VG_(printf)("No permissions on a segment mapped from %s\n", |
| 171 | filename?filename:(UChar*)"NULL"); |
| 172 | return; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 173 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 174 | |
| 175 | /* This parallels what happens when we mmap some new memory */ |
| 176 | if (filename != NULL && xx == 'x') { |
| 177 | VG_(new_exe_segment)( start, size ); |
| 178 | } |
| 179 | VG_TRACK( new_mem_startup, start, size, rr=='r', ww=='w', xx=='x' ); |
| 180 | |
| 181 | /* If this is the stack segment mark all below %esp as noaccess. */ |
| 182 | r_esp = VG_(baseBlock)[VGOFF_(m_esp)]; |
| 183 | is_stack_segment = start <= r_esp && r_esp < start+size; |
| 184 | if (is_stack_segment) { |
| 185 | if (0) |
| 186 | VG_(message)(Vg_DebugMsg, "invalidating stack area: %x .. %x", |
| 187 | start,r_esp); |
| 188 | VG_TRACK( die_mem_stack, start, r_esp-start ); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /* 1. Records exe segments from /proc/pid/maps -- always necessary, because |
| 194 | if they're munmap()ed we need to know if they were executable in order |
| 195 | to discard translations. Also checks there's no exe segment overlaps. |
| 196 | |
| 197 | 2. Marks global variables that might be accessed from generated code; |
| 198 | |
| 199 | 3. Sets up the end of the data segment so that vg_syscalls.c can make |
| 200 | sense of calls to brk(). |
| 201 | */ |
| 202 | void VG_(init_memory) ( void ) |
| 203 | { |
| 204 | /* 1 and 2 */ |
| 205 | VG_(read_procselfmaps) ( startup_segment_callback ); |
| 206 | |
| 207 | /* 3 */ |
| 208 | VG_TRACK( post_mem_write, (Addr) & VG_(running_on_simd_CPU), 1 ); |
| 209 | VG_TRACK( post_mem_write, (Addr) & VG_(clo_trace_malloc), 1 ); |
| 210 | VG_TRACK( post_mem_write, (Addr) & VG_(clo_sloppy_malloc), 1 ); |
| 211 | |
| 212 | /* 4 */ |
| 213 | VG_(init_dataseg_end_for_brk)(); |
sewardj | d5815ec | 2003-04-06 12:23:27 +0000 | [diff] [blame] | 214 | |
| 215 | /* kludge: some newer kernels place a "sysinfo" page up high, with |
| 216 | vsyscalls in it, and possibly some other stuff in the future. */ |
| 217 | if (VG_(sysinfo_page_exists)) { |
| 218 | VG_(new_exe_segment)( VG_(sysinfo_page_addr), 4096 ); |
| 219 | VG_TRACK( new_mem_startup, VG_(sysinfo_page_addr), 4096, |
| 220 | True, True, True ); |
| 221 | } |
| 222 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 223 | } |
| 224 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 225 | /*------------------------------------------------------------*/ |
| 226 | /*--- Tracking permissions around %esp changes. ---*/ |
| 227 | /*------------------------------------------------------------*/ |
| 228 | |
| 229 | /* |
| 230 | The stack |
| 231 | ~~~~~~~~~ |
| 232 | The stack's segment seems to be dynamically extended downwards |
| 233 | by the kernel as the stack pointer moves down. Initially, a |
| 234 | 1-page (4k) stack is allocated. When %esp moves below that for |
| 235 | the first time, presumably a page fault occurs. The kernel |
| 236 | detects that the faulting address is in the range from %esp upwards |
| 237 | to the current valid stack. It then extends the stack segment |
| 238 | downwards for enough to cover the faulting address, and resumes |
| 239 | the process (invisibly). The process is unaware of any of this. |
| 240 | |
| 241 | That means that Valgrind can't spot when the stack segment is |
| 242 | being extended. Fortunately, we want to precisely and continuously |
| 243 | update stack permissions around %esp, so we need to spot all |
| 244 | writes to %esp anyway. |
| 245 | |
| 246 | The deal is: when %esp is assigned a lower value, the stack is |
| 247 | being extended. Create a secondary maps to fill in any holes |
| 248 | between the old stack ptr and this one, if necessary. Then |
| 249 | mark all bytes in the area just "uncovered" by this %esp change |
| 250 | as write-only. |
| 251 | |
| 252 | When %esp goes back up, mark the area receded over as unreadable |
| 253 | and unwritable. |
| 254 | |
| 255 | Just to record the %esp boundary conditions somewhere convenient: |
| 256 | %esp always points to the lowest live byte in the stack. All |
| 257 | addresses below %esp are not live; those at and above it are. |
| 258 | */ |
| 259 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 260 | /* Kludgey ... how much does %esp have to change before we reckon that |
| 261 | the application is switching stacks ? */ |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame^] | 262 | #define VG_PLAUSIBLE_STACK_SIZE 8000000 |
| 263 | #define VG_HUGE_DELTA (VG_PLAUSIBLE_STACK_SIZE / 4) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 264 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame^] | 265 | /* This function gets called if new_mem_stack and/or die_mem_stack are |
| 266 | tracked by the skin, and one of the specialised cases (eg. new_mem_stack_4) |
| 267 | isn't used in preference */ |
| 268 | __attribute__((regparm(1))) |
| 269 | void VG_(unknown_esp_update)(Addr new_ESP) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 270 | { |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame^] | 271 | Addr old_ESP = VG_(get_archreg)(R_ESP); |
| 272 | Int delta = (Int)new_ESP - (Int)old_ESP; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 273 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame^] | 274 | if (delta < -(VG_HUGE_DELTA) || VG_HUGE_DELTA < delta) { |
| 275 | /* %esp has changed by more than HUGE_DELTA. We take this to mean |
| 276 | that the application is switching to a new stack, for whatever |
| 277 | reason. |
| 278 | |
| 279 | JRS 20021001: following discussions with John Regehr, if a stack |
| 280 | switch happens, it seems best not to mess at all with memory |
| 281 | permissions. Seems to work well with Netscape 4.X. Really the |
| 282 | only remaining difficulty is knowing exactly when a stack switch is |
| 283 | happening. */ |
| 284 | if (VG_(clo_verbosity) > 1) |
| 285 | VG_(message)(Vg_UserMsg, "Warning: client switching stacks? " |
| 286 | "%%esp: %p --> %p", old_ESP, new_ESP); |
| 287 | } else if (delta < 0) { |
| 288 | VG_TRACK( new_mem_stack, new_ESP, -delta ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 289 | |
njn | 9b007f6 | 2003-04-07 14:40:25 +0000 | [diff] [blame^] | 290 | } else if (delta > 0) { |
| 291 | VG_TRACK( die_mem_stack, old_ESP, delta ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 295 | /*--------------------------------------------------------------------*/ |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 296 | /*--- Support for memory leak detectors ---*/ |
| 297 | /*--------------------------------------------------------------------*/ |
| 298 | |
| 299 | /*------------------------------------------------------------*/ |
| 300 | /*--- Low-level address-space scanning, for the leak ---*/ |
| 301 | /*--- detector. ---*/ |
| 302 | /*------------------------------------------------------------*/ |
| 303 | |
| 304 | static |
| 305 | jmp_buf memscan_jmpbuf; |
| 306 | |
| 307 | |
| 308 | static |
| 309 | void vg_scan_all_valid_memory_sighandler ( Int sigNo ) |
| 310 | { |
| 311 | __builtin_longjmp(memscan_jmpbuf, 1); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | /* Safely (avoiding SIGSEGV / SIGBUS) scan the entire valid address |
| 316 | space and pass the addresses and values of all addressible, |
| 317 | defined, aligned words to notify_word. This is the basis for the |
| 318 | leak detector. Returns the number of calls made to notify_word. |
| 319 | |
| 320 | Addresses are validated 3 ways. First we enquire whether (addr >> |
| 321 | 16) denotes a 64k chunk in use, by asking is_valid_64k_chunk(). If |
| 322 | so, we decide for ourselves whether each x86-level (4 K) page in |
| 323 | the chunk is safe to inspect. If yes, we enquire with |
| 324 | is_valid_address() whether or not each of the 1024 word-locations |
| 325 | on the page is valid. Only if so are that address and its contents |
| 326 | passed to notify_word. |
| 327 | |
| 328 | This is all to avoid duplication of this machinery between the |
| 329 | memcheck and addrcheck skins. |
| 330 | */ |
| 331 | static |
| 332 | UInt vg_scan_all_valid_memory ( Bool is_valid_64k_chunk ( UInt ), |
| 333 | Bool is_valid_address ( Addr ), |
| 334 | void (*notify_word)( Addr, UInt ) ) |
| 335 | { |
| 336 | /* All volatile, because some gccs seem paranoid about longjmp(). */ |
sewardj | 6ccdd71 | 2002-12-22 19:11:14 +0000 | [diff] [blame] | 337 | volatile Bool anyValid; |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 338 | volatile Addr pageBase, addr; |
| 339 | volatile UInt res, numPages, page, primaryMapNo; |
| 340 | volatile UInt page_first_word, nWordsNotified; |
| 341 | |
| 342 | vki_ksigaction sigbus_saved; |
| 343 | vki_ksigaction sigbus_new; |
| 344 | vki_ksigaction sigsegv_saved; |
| 345 | vki_ksigaction sigsegv_new; |
| 346 | vki_ksigset_t blockmask_saved; |
| 347 | vki_ksigset_t unblockmask_new; |
| 348 | |
| 349 | /* Temporarily install a new sigsegv and sigbus handler, and make |
| 350 | sure SIGBUS, SIGSEGV and SIGTERM are unblocked. (Perhaps the |
| 351 | first two can never be blocked anyway?) */ |
| 352 | |
| 353 | sigbus_new.ksa_handler = vg_scan_all_valid_memory_sighandler; |
| 354 | sigbus_new.ksa_flags = VKI_SA_ONSTACK | VKI_SA_RESTART; |
| 355 | sigbus_new.ksa_restorer = NULL; |
| 356 | res = VG_(ksigemptyset)( &sigbus_new.ksa_mask ); |
| 357 | sk_assert(res == 0); |
| 358 | |
| 359 | sigsegv_new.ksa_handler = vg_scan_all_valid_memory_sighandler; |
| 360 | sigsegv_new.ksa_flags = VKI_SA_ONSTACK | VKI_SA_RESTART; |
| 361 | sigsegv_new.ksa_restorer = NULL; |
| 362 | res = VG_(ksigemptyset)( &sigsegv_new.ksa_mask ); |
| 363 | sk_assert(res == 0+0); |
| 364 | |
| 365 | res = VG_(ksigemptyset)( &unblockmask_new ); |
| 366 | res |= VG_(ksigaddset)( &unblockmask_new, VKI_SIGBUS ); |
| 367 | res |= VG_(ksigaddset)( &unblockmask_new, VKI_SIGSEGV ); |
| 368 | res |= VG_(ksigaddset)( &unblockmask_new, VKI_SIGTERM ); |
| 369 | sk_assert(res == 0+0+0); |
| 370 | |
| 371 | res = VG_(ksigaction)( VKI_SIGBUS, &sigbus_new, &sigbus_saved ); |
| 372 | sk_assert(res == 0+0+0+0); |
| 373 | |
| 374 | res = VG_(ksigaction)( VKI_SIGSEGV, &sigsegv_new, &sigsegv_saved ); |
| 375 | sk_assert(res == 0+0+0+0+0); |
| 376 | |
| 377 | res = VG_(ksigprocmask)( VKI_SIG_UNBLOCK, &unblockmask_new, &blockmask_saved ); |
| 378 | sk_assert(res == 0+0+0+0+0+0); |
| 379 | |
| 380 | /* The signal handlers are installed. Actually do the memory scan. */ |
| 381 | numPages = 1 << (32-VKI_BYTES_PER_PAGE_BITS); |
| 382 | sk_assert(numPages == 1048576); |
| 383 | sk_assert(4096 == (1 << VKI_BYTES_PER_PAGE_BITS)); |
| 384 | |
| 385 | nWordsNotified = 0; |
| 386 | |
| 387 | for (page = 0; page < numPages; page++) { |
| 388 | |
| 389 | /* Base address of this 4k page. */ |
| 390 | pageBase = page << VKI_BYTES_PER_PAGE_BITS; |
| 391 | |
| 392 | /* Skip if this page is in an unused 64k chunk. */ |
| 393 | primaryMapNo = pageBase >> 16; |
| 394 | if (!is_valid_64k_chunk(primaryMapNo)) |
| 395 | continue; |
| 396 | |
sewardj | 6ccdd71 | 2002-12-22 19:11:14 +0000 | [diff] [blame] | 397 | /* Next, establish whether or not we want to consider any |
| 398 | locations on this page. We need to do so before actually |
| 399 | prodding it, because prodding it when in fact it is not |
| 400 | needed can cause a page fault which under some rare |
| 401 | circumstances can cause the kernel to extend the stack |
| 402 | segment all the way down to here, which is seriously bad. |
| 403 | Hence: */ |
| 404 | anyValid = False; |
| 405 | for (addr = pageBase; addr < pageBase+VKI_BYTES_PER_PAGE; addr += 4) { |
| 406 | if (is_valid_address(addr)) { |
| 407 | anyValid = True; |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | if (!anyValid) |
| 413 | continue; /* nothing interesting here .. move to the next page */ |
| 414 | |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 415 | /* Ok, we have to prod cautiously at the page and see if it |
| 416 | explodes or not. */ |
| 417 | if (__builtin_setjmp(memscan_jmpbuf) == 0) { |
| 418 | /* try this ... */ |
| 419 | page_first_word = * (volatile UInt*)pageBase; |
| 420 | /* we get here if we didn't get a fault */ |
| 421 | /* Scan the page */ |
| 422 | for (addr = pageBase; addr < pageBase+VKI_BYTES_PER_PAGE; addr += 4) { |
| 423 | if (is_valid_address(addr)) { |
| 424 | nWordsNotified++; |
| 425 | notify_word ( addr, *(UInt*)addr ); |
| 426 | } |
| 427 | } |
| 428 | } else { |
| 429 | /* We get here if reading the first word of the page caused a |
| 430 | fault, which in turn caused the signal handler to longjmp. |
| 431 | Ignore this page. */ |
| 432 | if (0) |
| 433 | VG_(printf)( |
| 434 | "vg_scan_all_valid_memory_sighandler: ignoring page at %p\n", |
| 435 | (void*)pageBase |
| 436 | ); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /* Restore signal state to whatever it was before. */ |
| 441 | res = VG_(ksigaction)( VKI_SIGBUS, &sigbus_saved, NULL ); |
| 442 | sk_assert(res == 0 +0); |
| 443 | |
| 444 | res = VG_(ksigaction)( VKI_SIGSEGV, &sigsegv_saved, NULL ); |
| 445 | sk_assert(res == 0 +0 +0); |
| 446 | |
| 447 | res = VG_(ksigprocmask)( VKI_SIG_SETMASK, &blockmask_saved, NULL ); |
| 448 | sk_assert(res == 0 +0 +0 +0); |
| 449 | |
| 450 | return nWordsNotified; |
| 451 | } |
| 452 | |
| 453 | |
| 454 | /*------------------------------------------------------------*/ |
| 455 | /*--- Detecting leaked (unreachable) malloc'd blocks. ---*/ |
| 456 | /*------------------------------------------------------------*/ |
| 457 | |
| 458 | /* A block is either |
| 459 | -- Proper-ly reached; a pointer to its start has been found |
| 460 | -- Interior-ly reached; only an interior pointer to it has been found |
| 461 | -- Unreached; so far, no pointers to any part of it have been found. |
| 462 | */ |
| 463 | typedef |
| 464 | enum { Unreached, Interior, Proper } |
| 465 | Reachedness; |
| 466 | |
| 467 | /* A block record, used for generating err msgs. */ |
| 468 | typedef |
| 469 | struct _LossRecord { |
| 470 | struct _LossRecord* next; |
| 471 | /* Where these lost blocks were allocated. */ |
| 472 | ExeContext* allocated_at; |
| 473 | /* Their reachability. */ |
| 474 | Reachedness loss_mode; |
| 475 | /* Number of blocks and total # bytes involved. */ |
| 476 | UInt total_bytes; |
| 477 | UInt num_blocks; |
| 478 | } |
| 479 | LossRecord; |
| 480 | |
| 481 | |
| 482 | /* Find the i such that ptr points at or inside the block described by |
| 483 | shadows[i]. Return -1 if none found. This assumes that shadows[] |
| 484 | has been sorted on the ->data field. */ |
| 485 | |
| 486 | #ifdef VG_DEBUG_LEAKCHECK |
| 487 | /* Used to sanity-check the fast binary-search mechanism. */ |
| 488 | static |
| 489 | Int find_shadow_for_OLD ( Addr ptr, |
| 490 | ShadowChunk** shadows, |
| 491 | Int n_shadows ) |
| 492 | |
| 493 | { |
| 494 | Int i; |
| 495 | Addr a_lo, a_hi; |
| 496 | PROF_EVENT(70); |
| 497 | for (i = 0; i < n_shadows; i++) { |
| 498 | PROF_EVENT(71); |
| 499 | a_lo = shadows[i]->data; |
| 500 | a_hi = ((Addr)shadows[i]->data) + shadows[i]->size - 1; |
| 501 | if (a_lo <= ptr && ptr <= a_hi) |
| 502 | return i; |
| 503 | } |
| 504 | return -1; |
| 505 | } |
| 506 | #endif |
| 507 | |
| 508 | |
| 509 | static |
| 510 | Int find_shadow_for ( Addr ptr, |
| 511 | ShadowChunk** shadows, |
| 512 | Int n_shadows ) |
| 513 | { |
| 514 | Addr a_mid_lo, a_mid_hi; |
| 515 | Int lo, mid, hi, retVal; |
| 516 | /* VG_(printf)("find shadow for %p = ", ptr); */ |
| 517 | retVal = -1; |
| 518 | lo = 0; |
| 519 | hi = n_shadows-1; |
| 520 | while (True) { |
| 521 | /* invariant: current unsearched space is from lo to hi, |
| 522 | inclusive. */ |
| 523 | if (lo > hi) break; /* not found */ |
| 524 | |
| 525 | mid = (lo + hi) / 2; |
| 526 | a_mid_lo = shadows[mid]->data; |
| 527 | a_mid_hi = ((Addr)shadows[mid]->data) + shadows[mid]->size - 1; |
| 528 | |
| 529 | if (ptr < a_mid_lo) { |
| 530 | hi = mid-1; |
| 531 | continue; |
| 532 | } |
| 533 | if (ptr > a_mid_hi) { |
| 534 | lo = mid+1; |
| 535 | continue; |
| 536 | } |
| 537 | sk_assert(ptr >= a_mid_lo && ptr <= a_mid_hi); |
| 538 | retVal = mid; |
| 539 | break; |
| 540 | } |
| 541 | |
| 542 | # ifdef VG_DEBUG_LEAKCHECK |
| 543 | vg_assert(retVal == find_shadow_for_OLD ( ptr, shadows, n_shadows )); |
| 544 | # endif |
| 545 | /* VG_(printf)("%d\n", retVal); */ |
| 546 | return retVal; |
| 547 | } |
| 548 | |
| 549 | |
| 550 | |
| 551 | static |
| 552 | void sort_malloc_shadows ( ShadowChunk** shadows, UInt n_shadows ) |
| 553 | { |
| 554 | Int incs[14] = { 1, 4, 13, 40, 121, 364, 1093, 3280, |
| 555 | 9841, 29524, 88573, 265720, |
| 556 | 797161, 2391484 }; |
| 557 | Int lo = 0; |
| 558 | Int hi = n_shadows-1; |
| 559 | Int i, j, h, bigN, hp; |
| 560 | ShadowChunk* v; |
| 561 | |
| 562 | bigN = hi - lo + 1; if (bigN < 2) return; |
sewardj | 6219d67 | 2002-11-11 08:36:52 +0000 | [diff] [blame] | 563 | hp = 0; while (hp < 14 && incs[hp] < bigN) hp++; hp--; |
| 564 | vg_assert(0 <= hp && hp < 14); |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 565 | |
| 566 | for (; hp >= 0; hp--) { |
| 567 | h = incs[hp]; |
| 568 | i = lo + h; |
| 569 | while (1) { |
| 570 | if (i > hi) break; |
| 571 | v = shadows[i]; |
| 572 | j = i; |
| 573 | while (shadows[j-h]->data > v->data) { |
| 574 | shadows[j] = shadows[j-h]; |
| 575 | j = j - h; |
| 576 | if (j <= (lo + h - 1)) break; |
| 577 | } |
| 578 | shadows[j] = v; |
| 579 | i++; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | |
| 585 | /* Globals, for the callback used by VG_(detect_memory_leaks). */ |
| 586 | |
| 587 | static ShadowChunk** vglc_shadows; |
| 588 | static Int vglc_n_shadows; |
| 589 | static Reachedness* vglc_reachedness; |
| 590 | static Addr vglc_min_mallocd_addr; |
| 591 | static Addr vglc_max_mallocd_addr; |
| 592 | |
| 593 | static |
| 594 | void vg_detect_memory_leaks_notify_addr ( Addr a, UInt word_at_a ) |
| 595 | { |
| 596 | Int sh_no; |
| 597 | Addr ptr; |
| 598 | |
| 599 | /* Rule out some known causes of bogus pointers. Mostly these do |
| 600 | not cause much trouble because only a few false pointers can |
| 601 | ever lurk in these places. This mainly stops it reporting that |
| 602 | blocks are still reachable in stupid test programs like this |
| 603 | |
| 604 | int main (void) { char* a = malloc(100); return 0; } |
| 605 | |
| 606 | which people seem inordinately fond of writing, for some reason. |
| 607 | |
| 608 | Note that this is a complete kludge. It would be better to |
| 609 | ignore any addresses corresponding to valgrind.so's .bss and |
| 610 | .data segments, but I cannot think of a reliable way to identify |
| 611 | where the .bss segment has been put. If you can, drop me a |
| 612 | line. |
| 613 | */ |
| 614 | if (VG_(within_stack)(a)) return; |
| 615 | if (VG_(within_m_state_static)(a)) return; |
| 616 | if (a == (Addr)(&vglc_min_mallocd_addr)) return; |
| 617 | if (a == (Addr)(&vglc_max_mallocd_addr)) return; |
| 618 | |
| 619 | /* OK, let's get on and do something Useful for a change. */ |
| 620 | |
| 621 | ptr = (Addr)word_at_a; |
| 622 | if (ptr >= vglc_min_mallocd_addr && ptr <= vglc_max_mallocd_addr) { |
| 623 | /* Might be legitimate; we'll have to investigate further. */ |
| 624 | sh_no = find_shadow_for ( ptr, vglc_shadows, vglc_n_shadows ); |
| 625 | if (sh_no != -1) { |
| 626 | /* Found a block at/into which ptr points. */ |
| 627 | sk_assert(sh_no >= 0 && sh_no < vglc_n_shadows); |
| 628 | sk_assert(ptr < vglc_shadows[sh_no]->data |
| 629 | + vglc_shadows[sh_no]->size); |
| 630 | /* Decide whether Proper-ly or Interior-ly reached. */ |
| 631 | if (ptr == vglc_shadows[sh_no]->data) { |
| 632 | if (0) VG_(printf)("pointer at %p to %p\n", a, word_at_a ); |
| 633 | vglc_reachedness[sh_no] = Proper; |
| 634 | } else { |
| 635 | if (vglc_reachedness[sh_no] == Unreached) |
| 636 | vglc_reachedness[sh_no] = Interior; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | |
sewardj | 99aac97 | 2002-12-26 01:53:45 +0000 | [diff] [blame] | 643 | /* Stuff for figuring out if a leak report should be suppressed. */ |
| 644 | static |
| 645 | Bool leaksupp_matches_callers(Supp* su, Char caller_obj[][M_VG_ERRTXT], |
| 646 | Char caller_fun[][M_VG_ERRTXT]) |
| 647 | { |
| 648 | Int i; |
| 649 | |
| 650 | for (i = 0; su->caller[i] != NULL; i++) { |
| 651 | switch (su->caller_ty[i]) { |
| 652 | case ObjName: if (VG_(string_match)(su->caller[i], |
| 653 | caller_obj[i])) break; |
| 654 | return False; |
| 655 | case FunName: if (VG_(string_match)(su->caller[i], |
| 656 | caller_fun[i])) break; |
| 657 | return False; |
| 658 | default: VG_(skin_panic)("leaksupp_matches_callers"); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /* If we reach here, it's a match */ |
| 663 | return True; |
| 664 | } |
| 665 | |
| 666 | |
| 667 | /* Does a leak record match a suppression? ie is this a suppressible |
| 668 | leak? Tries to minimise the number of symbol searches since they |
| 669 | are expensive. Copy n paste (more or less) of |
| 670 | is_suppressible_error. We have to pass in the actual value of |
| 671 | LeakSupp for comparison since this is the core and LeakSupp is a |
| 672 | skin-specific value. */ |
| 673 | static |
| 674 | Bool is_suppressible_leak ( ExeContext* allocated_at, |
| 675 | UInt /*CoreErrorKind*/ leakSupp ) |
| 676 | { |
| 677 | Int i; |
| 678 | |
| 679 | Char caller_obj[VG_N_SUPP_CALLERS][M_VG_ERRTXT]; |
| 680 | Char caller_fun[VG_N_SUPP_CALLERS][M_VG_ERRTXT]; |
| 681 | |
| 682 | Supp* su; |
| 683 | |
| 684 | /* get_objname_fnname() writes the function name and object name if |
| 685 | it finds them in the debug info. so the strings in the suppression |
| 686 | file should match these. |
| 687 | */ |
| 688 | |
| 689 | /* Initialise these strs so they are always safe to compare, even |
| 690 | if get_objname_fnname doesn't write anything to them. */ |
| 691 | for (i = 0; i < VG_N_SUPP_CALLERS; i++) |
| 692 | caller_obj[i][0] = caller_fun[i][0] = 0; |
| 693 | |
| 694 | for (i = 0; i < VG_N_SUPP_CALLERS && i < VG_(clo_backtrace_size); i++) { |
| 695 | VG_(get_objname_fnname) ( allocated_at->eips[i], |
| 696 | caller_obj[i], M_VG_ERRTXT, |
| 697 | caller_fun[i], M_VG_ERRTXT ); |
| 698 | } |
| 699 | |
| 700 | /* See if the leak record any suppression. */ |
| 701 | for (su = VG_(get_suppressions)(); su != NULL; su = su->next) { |
| 702 | if (VG_(get_supp_kind)(su) == (CoreErrorKind)leakSupp |
| 703 | && leaksupp_matches_callers(su, caller_obj, caller_fun)) { |
| 704 | return True; |
| 705 | } |
| 706 | } |
| 707 | return False; /* no matches */ |
| 708 | } |
| 709 | |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 710 | /* Top level entry point to leak detector. Call here, passing in |
| 711 | suitable address-validating functions (see comment at top of |
| 712 | vg_scan_all_valid_memory above). All this is to avoid duplication |
| 713 | of the leak-detection code for the Memcheck and Addrcheck skins. |
| 714 | Also pass in a skin-specific function to extract the .where field |
| 715 | for allocated blocks, an indication of the resolution wanted for |
| 716 | distinguishing different allocation points, and whether or not |
| 717 | reachable blocks should be shown. |
| 718 | */ |
| 719 | void VG_(generic_detect_memory_leaks) ( |
| 720 | Bool is_valid_64k_chunk ( UInt ), |
| 721 | Bool is_valid_address ( Addr ), |
| 722 | ExeContext* get_where ( ShadowChunk* ), |
| 723 | VgRes leak_resolution, |
sewardj | 99aac97 | 2002-12-26 01:53:45 +0000 | [diff] [blame] | 724 | Bool show_reachable, |
| 725 | UInt /*CoreErrorKind*/ leakSupp |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 726 | ) |
| 727 | { |
| 728 | Int i; |
| 729 | Int blocks_leaked, bytes_leaked; |
| 730 | Int blocks_dubious, bytes_dubious; |
| 731 | Int blocks_reachable, bytes_reachable; |
sewardj | 99aac97 | 2002-12-26 01:53:45 +0000 | [diff] [blame] | 732 | Int blocks_suppressed, bytes_suppressed; |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 733 | Int n_lossrecords; |
| 734 | UInt bytes_notified; |
| 735 | |
| 736 | LossRecord* errlist; |
| 737 | LossRecord* p; |
| 738 | |
| 739 | /* VG_(get_malloc_shadows) allocates storage for shadows */ |
| 740 | vglc_shadows = VG_(get_malloc_shadows)( &vglc_n_shadows ); |
| 741 | if (vglc_n_shadows == 0) { |
| 742 | sk_assert(vglc_shadows == NULL); |
| 743 | VG_(message)(Vg_UserMsg, |
sewardj | dc7a98f | 2002-12-15 13:35:40 +0000 | [diff] [blame] | 744 | "No malloc'd blocks -- no leaks are possible."); |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 745 | return; |
| 746 | } |
| 747 | |
| 748 | VG_(message)(Vg_UserMsg, |
| 749 | "searching for pointers to %d not-freed blocks.", |
| 750 | vglc_n_shadows ); |
| 751 | sort_malloc_shadows ( vglc_shadows, vglc_n_shadows ); |
| 752 | |
| 753 | /* Sanity check; assert that the blocks are now in order and that |
| 754 | they don't overlap. */ |
| 755 | for (i = 0; i < vglc_n_shadows-1; i++) { |
| 756 | sk_assert( ((Addr)vglc_shadows[i]->data) |
| 757 | < ((Addr)vglc_shadows[i+1]->data) ); |
| 758 | sk_assert( ((Addr)vglc_shadows[i]->data) + vglc_shadows[i]->size |
| 759 | < ((Addr)vglc_shadows[i+1]->data) ); |
| 760 | } |
| 761 | |
| 762 | vglc_min_mallocd_addr = ((Addr)vglc_shadows[0]->data); |
| 763 | vglc_max_mallocd_addr = ((Addr)vglc_shadows[vglc_n_shadows-1]->data) |
| 764 | + vglc_shadows[vglc_n_shadows-1]->size - 1; |
| 765 | |
| 766 | vglc_reachedness |
| 767 | = VG_(malloc)( vglc_n_shadows * sizeof(Reachedness) ); |
| 768 | for (i = 0; i < vglc_n_shadows; i++) |
| 769 | vglc_reachedness[i] = Unreached; |
| 770 | |
| 771 | /* Do the scan of memory. */ |
| 772 | bytes_notified |
| 773 | = VKI_BYTES_PER_WORD |
| 774 | * vg_scan_all_valid_memory ( |
| 775 | is_valid_64k_chunk, |
| 776 | is_valid_address, |
| 777 | &vg_detect_memory_leaks_notify_addr |
| 778 | ); |
| 779 | |
| 780 | VG_(message)(Vg_UserMsg, "checked %d bytes.", bytes_notified); |
| 781 | |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 782 | /* Common up the lost blocks so we can print sensible error |
| 783 | messages. */ |
| 784 | |
| 785 | n_lossrecords = 0; |
| 786 | errlist = NULL; |
| 787 | for (i = 0; i < vglc_n_shadows; i++) { |
| 788 | |
| 789 | /* 'where' stored in 'skin_extra' field; extract using function |
| 790 | supplied by the calling skin. */ |
| 791 | ExeContext* where = get_where ( vglc_shadows[i] ); |
| 792 | |
| 793 | for (p = errlist; p != NULL; p = p->next) { |
| 794 | if (p->loss_mode == vglc_reachedness[i] |
| 795 | && VG_(eq_ExeContext) ( leak_resolution, |
| 796 | p->allocated_at, |
| 797 | where) ) { |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | if (p != NULL) { |
| 802 | p->num_blocks ++; |
| 803 | p->total_bytes += vglc_shadows[i]->size; |
| 804 | } else { |
| 805 | n_lossrecords ++; |
| 806 | p = VG_(malloc)(sizeof(LossRecord)); |
| 807 | p->loss_mode = vglc_reachedness[i]; |
| 808 | p->allocated_at = where; |
| 809 | p->total_bytes = vglc_shadows[i]->size; |
| 810 | p->num_blocks = 1; |
| 811 | p->next = errlist; |
| 812 | errlist = p; |
| 813 | } |
| 814 | } |
sewardj | 99aac97 | 2002-12-26 01:53:45 +0000 | [diff] [blame] | 815 | |
| 816 | /* Print out the commoned-up blocks and collect summary stats. */ |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 817 | |
sewardj | 99aac97 | 2002-12-26 01:53:45 +0000 | [diff] [blame] | 818 | blocks_leaked = bytes_leaked = 0; |
| 819 | blocks_dubious = bytes_dubious = 0; |
| 820 | blocks_reachable = bytes_reachable = 0; |
| 821 | blocks_suppressed = bytes_suppressed = 0; |
| 822 | |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 823 | for (i = 0; i < n_lossrecords; i++) { |
| 824 | LossRecord* p_min = NULL; |
| 825 | UInt n_min = 0xFFFFFFFF; |
| 826 | for (p = errlist; p != NULL; p = p->next) { |
| 827 | if (p->num_blocks > 0 && p->total_bytes < n_min) { |
| 828 | n_min = p->total_bytes; |
| 829 | p_min = p; |
| 830 | } |
| 831 | } |
| 832 | sk_assert(p_min != NULL); |
| 833 | |
sewardj | 99aac97 | 2002-12-26 01:53:45 +0000 | [diff] [blame] | 834 | if (is_suppressible_leak(p_min->allocated_at, leakSupp)) { |
| 835 | blocks_suppressed += p_min->num_blocks; |
| 836 | bytes_suppressed += p_min->total_bytes; |
| 837 | p_min->num_blocks = 0; |
| 838 | continue; |
| 839 | } else { |
| 840 | switch (p_min->loss_mode) { |
| 841 | case Unreached: |
| 842 | blocks_leaked += p_min->num_blocks; |
| 843 | bytes_leaked += p_min->total_bytes; |
| 844 | break; |
| 845 | case Interior: |
| 846 | blocks_dubious += p_min->num_blocks; |
| 847 | bytes_dubious += p_min->total_bytes; |
| 848 | break; |
| 849 | case Proper: |
| 850 | blocks_reachable += p_min->num_blocks; |
| 851 | bytes_reachable += p_min->total_bytes; |
| 852 | break; |
| 853 | default: |
| 854 | VG_(core_panic)("generic_detect_memory_leaks: " |
| 855 | "unknown loss mode"); |
| 856 | } |
| 857 | } |
| 858 | |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 859 | if ( (!show_reachable) && (p_min->loss_mode == Proper)) { |
| 860 | p_min->num_blocks = 0; |
| 861 | continue; |
| 862 | } |
| 863 | |
| 864 | VG_(message)(Vg_UserMsg, ""); |
| 865 | VG_(message)( |
| 866 | Vg_UserMsg, |
| 867 | "%d bytes in %d blocks are %s in loss record %d of %d", |
| 868 | p_min->total_bytes, p_min->num_blocks, |
| 869 | p_min->loss_mode==Unreached ? "definitely lost" : |
| 870 | (p_min->loss_mode==Interior ? "possibly lost" |
| 871 | : "still reachable"), |
| 872 | i+1, n_lossrecords |
| 873 | ); |
| 874 | VG_(pp_ExeContext)(p_min->allocated_at); |
| 875 | p_min->num_blocks = 0; |
| 876 | } |
| 877 | |
| 878 | VG_(message)(Vg_UserMsg, ""); |
| 879 | VG_(message)(Vg_UserMsg, "LEAK SUMMARY:"); |
| 880 | VG_(message)(Vg_UserMsg, " definitely lost: %d bytes in %d blocks.", |
| 881 | bytes_leaked, blocks_leaked ); |
| 882 | VG_(message)(Vg_UserMsg, " possibly lost: %d bytes in %d blocks.", |
| 883 | bytes_dubious, blocks_dubious ); |
| 884 | VG_(message)(Vg_UserMsg, " still reachable: %d bytes in %d blocks.", |
| 885 | bytes_reachable, blocks_reachable ); |
sewardj | 99aac97 | 2002-12-26 01:53:45 +0000 | [diff] [blame] | 886 | VG_(message)(Vg_UserMsg, " suppressed: %d bytes in %d blocks.", |
| 887 | bytes_suppressed, blocks_suppressed ); |
sewardj | a449568 | 2002-10-21 07:29:59 +0000 | [diff] [blame] | 888 | if (!show_reachable) { |
| 889 | VG_(message)(Vg_UserMsg, |
| 890 | "Reachable blocks (those to which a pointer was found) are not shown."); |
| 891 | VG_(message)(Vg_UserMsg, |
| 892 | "To see them, rerun with: --show-reachable=yes"); |
| 893 | } |
| 894 | VG_(message)(Vg_UserMsg, ""); |
| 895 | |
| 896 | VG_(free) ( vglc_shadows ); |
| 897 | VG_(free) ( vglc_reachedness ); |
| 898 | } |
| 899 | |
| 900 | |
| 901 | /*--------------------------------------------------------------------*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 902 | /*--- end vg_memory.c ---*/ |
| 903 | /*--------------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 904 | |