blob: 11621c31cda323706bc66b1a0575a805961fdf62 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00003/*--- Memory-related stuff: segment initialisation and tracking, ---*/
4/*--- stack operations ---*/
sewardjde4a1d02002-03-22 01:27:54 +00005/*--- vg_memory.c ---*/
6/*--------------------------------------------------------------------*/
7
8/*
njnc9539842002-10-02 13:26:35 +00009 This file is part of Valgrind, an extensible x86 protected-mode
10 emulator for monitoring program execution on x86-Unixes.
sewardjde4a1d02002-03-22 01:27:54 +000011
12 Copyright (C) 2000-2002 Julian Seward
13 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000014
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
njn25e49d8e72002-09-23 09:36:25 +000030 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000031*/
32
33#include "vg_include.h"
34
sewardja4495682002-10-21 07:29:59 +000035/* Define to debug the memory-leak-detector. */
36/* #define VG_DEBUG_LEAKCHECK */
37
sewardjde4a1d02002-03-22 01:27:54 +000038
njn25e49d8e72002-09-23 09:36:25 +000039/*--------------------------------------------------------------*/
40/*--- Initialise program data/text etc on program startup. ---*/
41/*--------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +000042
njn25e49d8e72002-09-23 09:36:25 +000043typedef
44 struct _ExeSeg {
45 Addr start;
46 UInt size;
47 struct _ExeSeg* next;
sewardjde4a1d02002-03-22 01:27:54 +000048 }
njn25e49d8e72002-09-23 09:36:25 +000049 ExeSeg;
sewardjde4a1d02002-03-22 01:27:54 +000050
njn25e49d8e72002-09-23 09:36:25 +000051/* 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*/
58static ExeSeg* exeSegsHead = NULL;
sewardjde4a1d02002-03-22 01:27:54 +000059
njn25e49d8e72002-09-23 09:36:25 +000060/* 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). */
65static 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;
sewardjde4a1d02002-03-22 01:27:54 +000071
njn25e49d8e72002-09-23 09:36:25 +000072 /* 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;
sewardjde4a1d02002-03-22 01:27:54 +000078
njn25e49d8e72002-09-23 09:36:25 +000079 /* 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);
sewardjde4a1d02002-03-22 01:27:54 +000095 }
sewardjde4a1d02002-03-22 01:27:54 +000096 }
njn25e49d8e72002-09-23 09:36:25 +000097}
98
99static 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);
sewardjde4a1d02002-03-22 01:27:54 +0000118 return True;
119}
120
njn25e49d8e72002-09-23 09:36:25 +0000121/* 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 */
128void VG_(new_exe_segment) ( Addr a, UInt len )
sewardjde4a1d02002-03-22 01:27:54 +0000129{
njn25e49d8e72002-09-23 09:36:25 +0000130 add_exe_segment_to_list( a, len );
131 VG_(maybe_read_symbols)();
sewardjde4a1d02002-03-22 01:27:54 +0000132}
133
njn25e49d8e72002-09-23 09:36:25 +0000134/* 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.
140void VG_(remove_if_exe_segment) ( Addr a, UInt len )
sewardjde4a1d02002-03-22 01:27:54 +0000141{
njn25e49d8e72002-09-23 09:36:25 +0000142 if (remove_if_exe_segment_from_list( a, len )) {
143 VG_(invalidate_translations) ( a, len );
144 VG_(maybe_unload_symbols) ( a, len );
sewardjde4a1d02002-03-22 01:27:54 +0000145 }
146}
147
148
njn25e49d8e72002-09-23 09:36:25 +0000149static
150void startup_segment_callback ( Addr start, UInt size,
151 Char rr, Char ww, Char xx,
152 UInt foffset, UChar* filename )
sewardjde4a1d02002-03-22 01:27:54 +0000153{
njn25e49d8e72002-09-23 09:36:25 +0000154 UInt r_esp;
155 Bool is_stack_segment;
sewardjde4a1d02002-03-22 01:27:54 +0000156
njn25e49d8e72002-09-23 09:36:25 +0000157 /* Sanity check ... if this is the executable's text segment,
158 ensure it is loaded where we think it ought to be. Any file
159 name which doesn't contain ".so" is assumed to be the
160 executable. */
161 if (filename != NULL
162 && xx == 'x'
163 && VG_(strstr(filename, ".so")) == NULL
164 ) {
165 /* We assume this is the executable. */
166 if (start != VG_ASSUMED_EXE_BASE) {
167 VG_(message)(Vg_UserMsg,
168 "FATAL: executable base addr not as assumed.");
169 VG_(message)(Vg_UserMsg, "name %s, actual %p, assumed %p.",
170 filename, start, VG_ASSUMED_EXE_BASE);
171 VG_(message)(Vg_UserMsg,
172 "One reason this could happen is that you have a shared object");
173 VG_(message)(Vg_UserMsg,
174 " whose name doesn't contain the characters \".so\", so Valgrind ");
175 VG_(message)(Vg_UserMsg,
176 "naively assumes it is the executable. ");
177 VG_(message)(Vg_UserMsg,
178 "In that case, rename it appropriately.");
njne427a662002-10-02 11:08:25 +0000179 VG_(core_panic)("VG_ASSUMED_EXE_BASE doesn't match reality");
sewardjde4a1d02002-03-22 01:27:54 +0000180 }
181 }
njn25e49d8e72002-09-23 09:36:25 +0000182
183 if (0)
184 VG_(message)(Vg_DebugMsg,
185 "initial map %8x-%8x %c%c%c? %8x (%d) (%s)",
186 start,start+size,rr,ww,xx,foffset,
187 size, filename?filename:(UChar*)"NULL");
188
189 if (rr != 'r' && xx != 'x' && ww != 'w') {
sewardj6e4234e2002-10-06 00:28:21 +0000190 /* Implausible as it seems, R H 6.2 generates such segments:
191 40067000-400ac000 r-xp 00000000 08:05 320686 /usr/X11R6/lib/libXt.so.6.0
192 400ac000-400ad000 ---p 00045000 08:05 320686 /usr/X11R6/lib/libXt.so.6.0
193 400ad000-400b0000 rw-p 00045000 08:05 320686 /usr/X11R6/lib/libXt.so.6.0
194 when running xedit. So just ignore them. */
195 if (0)
196 VG_(printf)("No permissions on a segment mapped from %s\n",
197 filename?filename:(UChar*)"NULL");
198 return;
sewardjde4a1d02002-03-22 01:27:54 +0000199 }
njn25e49d8e72002-09-23 09:36:25 +0000200
201 /* This parallels what happens when we mmap some new memory */
202 if (filename != NULL && xx == 'x') {
203 VG_(new_exe_segment)( start, size );
204 }
205 VG_TRACK( new_mem_startup, start, size, rr=='r', ww=='w', xx=='x' );
206
207 /* If this is the stack segment mark all below %esp as noaccess. */
208 r_esp = VG_(baseBlock)[VGOFF_(m_esp)];
209 is_stack_segment = start <= r_esp && r_esp < start+size;
210 if (is_stack_segment) {
211 if (0)
212 VG_(message)(Vg_DebugMsg, "invalidating stack area: %x .. %x",
213 start,r_esp);
214 VG_TRACK( die_mem_stack, start, r_esp-start );
215 }
216}
217
218
219/* 1. Records exe segments from /proc/pid/maps -- always necessary, because
220 if they're munmap()ed we need to know if they were executable in order
221 to discard translations. Also checks there's no exe segment overlaps.
222
223 2. Marks global variables that might be accessed from generated code;
224
225 3. Sets up the end of the data segment so that vg_syscalls.c can make
226 sense of calls to brk().
227 */
228void VG_(init_memory) ( void )
229{
230 /* 1 and 2 */
231 VG_(read_procselfmaps) ( startup_segment_callback );
232
233 /* 3 */
234 VG_TRACK( post_mem_write, (Addr) & VG_(running_on_simd_CPU), 1 );
235 VG_TRACK( post_mem_write, (Addr) & VG_(clo_trace_malloc), 1 );
236 VG_TRACK( post_mem_write, (Addr) & VG_(clo_sloppy_malloc), 1 );
237
238 /* 4 */
239 VG_(init_dataseg_end_for_brk)();
sewardjde4a1d02002-03-22 01:27:54 +0000240}
241
242
243/*------------------------------------------------------------*/
244/*--- Tracking permissions around %esp changes. ---*/
245/*------------------------------------------------------------*/
246
247/*
248 The stack
249 ~~~~~~~~~
250 The stack's segment seems to be dynamically extended downwards
251 by the kernel as the stack pointer moves down. Initially, a
252 1-page (4k) stack is allocated. When %esp moves below that for
253 the first time, presumably a page fault occurs. The kernel
254 detects that the faulting address is in the range from %esp upwards
255 to the current valid stack. It then extends the stack segment
256 downwards for enough to cover the faulting address, and resumes
257 the process (invisibly). The process is unaware of any of this.
258
259 That means that Valgrind can't spot when the stack segment is
260 being extended. Fortunately, we want to precisely and continuously
261 update stack permissions around %esp, so we need to spot all
262 writes to %esp anyway.
263
264 The deal is: when %esp is assigned a lower value, the stack is
265 being extended. Create a secondary maps to fill in any holes
266 between the old stack ptr and this one, if necessary. Then
267 mark all bytes in the area just "uncovered" by this %esp change
268 as write-only.
269
270 When %esp goes back up, mark the area receded over as unreadable
271 and unwritable.
272
273 Just to record the %esp boundary conditions somewhere convenient:
274 %esp always points to the lowest live byte in the stack. All
275 addresses below %esp are not live; those at and above it are.
276*/
277
sewardj1e8cdc92002-04-18 11:37:52 +0000278/* Does this address look like something in or vaguely near the
279 current thread's stack? */
sewardjee588a92002-10-05 15:28:29 +0000280static __attribute__((unused))
sewardj1e8cdc92002-04-18 11:37:52 +0000281Bool is_plausible_stack_addr ( ThreadState* tst, Addr aa )
sewardjde4a1d02002-03-22 01:27:54 +0000282{
283 UInt a = (UInt)aa;
njn25e49d8e72002-09-23 09:36:25 +0000284 //PROF_EVENT(100); PPP
sewardj1e8cdc92002-04-18 11:37:52 +0000285 if (a <= tst->stack_highest_word &&
286 a > tst->stack_highest_word - VG_PLAUSIBLE_STACK_SIZE)
sewardjde4a1d02002-03-22 01:27:54 +0000287 return True;
288 else
289 return False;
290}
291
292
sewardjde4a1d02002-03-22 01:27:54 +0000293/* Kludgey ... how much does %esp have to change before we reckon that
294 the application is switching stacks ? */
295#define VG_HUGE_DELTA (VG_PLAUSIBLE_STACK_SIZE / 4)
296
sewardjee588a92002-10-05 15:28:29 +0000297static __attribute__((unused))
298Addr get_page_base ( Addr a )
sewardjde4a1d02002-03-22 01:27:54 +0000299{
300 return a & ~(VKI_BYTES_PER_PAGE-1);
301}
302
njn25e49d8e72002-09-23 09:36:25 +0000303static void vg_handle_esp_assignment_SLOWLY ( Addr old_esp, Addr new_esp );
sewardjde4a1d02002-03-22 01:27:54 +0000304
njn25e49d8e72002-09-23 09:36:25 +0000305__attribute__ ((regparm (1)))
306void VG_(handle_esp_assignment) ( Addr new_esp )
sewardjde4a1d02002-03-22 01:27:54 +0000307{
njn25e49d8e72002-09-23 09:36:25 +0000308 UInt old_esp;
309 Int delta;
sewardjde4a1d02002-03-22 01:27:54 +0000310
njn25e49d8e72002-09-23 09:36:25 +0000311 VGP_MAYBE_PUSHCC(VgpStack);
312
313 old_esp = VG_(baseBlock)[VGOFF_(m_esp)];
314 delta = ((Int)new_esp) - ((Int)old_esp);
315
316 /* Update R_ESP */
317 VG_(baseBlock)[VGOFF_(m_esp)] = new_esp;
318
319 //PROF_EVENT(101); PPP
sewardjde4a1d02002-03-22 01:27:54 +0000320
321# ifndef VG_DEBUG_MEMORY
322
sewardja6e9fb92002-12-14 23:18:06 +0000323 /* if (IS_ALIGNED4_ADDR(old_esp) && IS_ALIGNED4_ADDR(new_esp)) { */
324 if (IS_ALIGNED4_ADDR((old_esp|new_esp))) {
sewardjde4a1d02002-03-22 01:27:54 +0000325
326 /* Deal with the most common cases fast. These are ordered in
327 the sequence most common first. */
328
njn25e49d8e72002-09-23 09:36:25 +0000329# ifdef VG_PROFILE_MEMORY
330 // PPP
sewardj749124b2002-11-11 08:32:06 +0000331 if (delta == - 4) PROF_EVENT(102);
332 else if (delta == 4) PROF_EVENT(103);
333 else if (delta == -12) PROF_EVENT(104);
334 else if (delta == - 8) PROF_EVENT(105);
335 else if (delta == 16) PROF_EVENT(106);
336 else if (delta == 12) PROF_EVENT(107);
337 else if (delta == 0) PROF_EVENT(108);
338 else if (delta == 8) PROF_EVENT(109);
339 else if (delta == -16) PROF_EVENT(110);
340 else if (delta == 20) PROF_EVENT(111);
341 else if (delta == -20) PROF_EVENT(112);
342 else if (delta == 24) PROF_EVENT(113);
343 else if (delta == -24) PROF_EVENT(114);
344 else if (delta > 0) PROF_EVENT(115); // PPP: new: aligned_big_pos
345 else PROF_EVENT(116); // PPP: new: aligned_big_neg
njn25e49d8e72002-09-23 09:36:25 +0000346# endif
347
sewardj799f5212002-10-01 09:09:23 +0000348 if (delta < 0 && delta > -2000) {
njn25e49d8e72002-09-23 09:36:25 +0000349 VG_TRACK(new_mem_stack_aligned, new_esp, -delta);
sewardj799f5212002-10-01 09:09:23 +0000350 VGP_MAYBE_POPCC(VgpStack);
351 return;
352 }
353 else
354 if (delta > 0 && delta < 2000) {
njn25e49d8e72002-09-23 09:36:25 +0000355 VG_TRACK(die_mem_stack_aligned, old_esp, delta);
sewardj799f5212002-10-01 09:09:23 +0000356 VGP_MAYBE_POPCC(VgpStack);
357 return;
sewardjde4a1d02002-03-22 01:27:54 +0000358 }
sewardj8afd9fe2002-10-02 01:38:40 +0000359 if (delta == 0) {
360 VGP_MAYBE_POPCC(VgpStack);
361 return;
362 }
sewardj799f5212002-10-01 09:09:23 +0000363 /* otherwise fall onto the slow-but-general case */
sewardjde4a1d02002-03-22 01:27:54 +0000364 }
365
366# endif
367
368 /* The above special cases handle 90% to 95% of all the stack
369 adjustments. The rest we give to the slow-but-general
370 mechanism. */
sewardj799f5212002-10-01 09:09:23 +0000371 /* VG_(printf)("big delta %d\n", delta); */
njn25e49d8e72002-09-23 09:36:25 +0000372 vg_handle_esp_assignment_SLOWLY ( old_esp, new_esp );
373 VGP_MAYBE_POPCC(VgpStack);
sewardjde4a1d02002-03-22 01:27:54 +0000374}
375
376
njn25e49d8e72002-09-23 09:36:25 +0000377static void vg_handle_esp_assignment_SLOWLY ( Addr old_esp, Addr new_esp )
sewardjde4a1d02002-03-22 01:27:54 +0000378{
njn25e49d8e72002-09-23 09:36:25 +0000379 Int delta;
380
381 delta = ((Int)new_esp) - ((Int)old_esp);
382 //VG_(printf)("delta %d (%x) %x --> %x\n", delta, delta, old_esp, new_esp);
383 //PROF_EVENT(120); PPP
sewardjde4a1d02002-03-22 01:27:54 +0000384 if (-(VG_HUGE_DELTA) < delta && delta < VG_HUGE_DELTA) {
385 /* "Ordinary" stack change. */
386 if (new_esp < old_esp) {
387 /* Moving down; the stack is growing. */
njn25e49d8e72002-09-23 09:36:25 +0000388 //PROF_EVENT(121); PPP
389 VG_TRACK( new_mem_stack, new_esp, -delta );
390
391 } else if (new_esp > old_esp) {
sewardjde4a1d02002-03-22 01:27:54 +0000392 /* Moving up; the stack is shrinking. */
njn25e49d8e72002-09-23 09:36:25 +0000393 //PROF_EVENT(122); PPP
394 VG_TRACK( die_mem_stack, old_esp, delta );
395
396 } else {
397 /* when old_esp == new_esp */
398 //PROF_EVENT(123); PPP
sewardjde4a1d02002-03-22 01:27:54 +0000399 }
njn25e49d8e72002-09-23 09:36:25 +0000400 return;
sewardjde4a1d02002-03-22 01:27:54 +0000401 }
402
403 /* %esp has changed by more than HUGE_DELTA. We take this to mean
404 that the application is switching to a new stack, for whatever
405 reason, and we attempt to initialise the permissions around the
406 new stack in some plausible way. All pretty kludgey; needed to
407 make netscape-4.07 run without generating thousands of error
408 contexts.
409
410 If we appear to be switching back to the main stack, don't mess
411 with the permissions in the area at and above the stack ptr.
412 Otherwise, we're switching to an alternative stack; make the
413 area above %esp readable -- this doesn't seem right -- the right
414 thing to do would be to make it writable -- but is needed to
415 avoid huge numbers of errs in netscape. To be investigated. */
416
sewardjee588a92002-10-05 15:28:29 +0000417 {
418# if 0
419 Addr invalid_down_to = get_page_base(new_esp)
sewardjde4a1d02002-03-22 01:27:54 +0000420 - 0 * VKI_BYTES_PER_PAGE;
421 Addr valid_up_to = get_page_base(new_esp) + VKI_BYTES_PER_PAGE
422 + 0 * VKI_BYTES_PER_PAGE;
sewardj1e8cdc92002-04-18 11:37:52 +0000423 ThreadState* tst = VG_(get_current_thread_state)();
sewardjee588a92002-10-05 15:28:29 +0000424# endif
njn25e49d8e72002-09-23 09:36:25 +0000425 //PROF_EVENT(124); PPP
sewardjde4a1d02002-03-22 01:27:54 +0000426 if (VG_(clo_verbosity) > 1)
427 VG_(message)(Vg_UserMsg, "Warning: client switching stacks? "
njn25e49d8e72002-09-23 09:36:25 +0000428 "%%esp: %p --> %p", old_esp, new_esp);
sewardjde4a1d02002-03-22 01:27:54 +0000429 /* VG_(printf)("na %p, %%esp %p, wr %p\n",
430 invalid_down_to, new_esp, valid_up_to ); */
sewardj8afd9fe2002-10-02 01:38:40 +0000431# if 0
432 /* JRS 20021001: following discussions with John Regehr, just
433 remove this. If a stack switch happens, it seems best not to
434 mess at all with memory permissions. Seems to work well with
435 Netscape 4.X. Really the only remaining difficulty is knowing
436 exactly when a stack switch is happening. */
njn25e49d8e72002-09-23 09:36:25 +0000437 VG_TRACK( die_mem_stack, invalid_down_to, new_esp - invalid_down_to );
sewardj1e8cdc92002-04-18 11:37:52 +0000438 if (!is_plausible_stack_addr(tst, new_esp)) {
njn25e49d8e72002-09-23 09:36:25 +0000439 VG_TRACK( post_mem_write, new_esp, valid_up_to - new_esp );
sewardjde4a1d02002-03-22 01:27:54 +0000440 }
sewardj8afd9fe2002-10-02 01:38:40 +0000441# endif
sewardjde4a1d02002-03-22 01:27:54 +0000442 }
443}
444
445
sewardjde4a1d02002-03-22 01:27:54 +0000446/*--------------------------------------------------------------------*/
sewardja4495682002-10-21 07:29:59 +0000447/*--- Support for memory leak detectors ---*/
448/*--------------------------------------------------------------------*/
449
450/*------------------------------------------------------------*/
451/*--- Low-level address-space scanning, for the leak ---*/
452/*--- detector. ---*/
453/*------------------------------------------------------------*/
454
455static
456jmp_buf memscan_jmpbuf;
457
458
459static
460void vg_scan_all_valid_memory_sighandler ( Int sigNo )
461{
462 __builtin_longjmp(memscan_jmpbuf, 1);
463}
464
465
466/* Safely (avoiding SIGSEGV / SIGBUS) scan the entire valid address
467 space and pass the addresses and values of all addressible,
468 defined, aligned words to notify_word. This is the basis for the
469 leak detector. Returns the number of calls made to notify_word.
470
471 Addresses are validated 3 ways. First we enquire whether (addr >>
472 16) denotes a 64k chunk in use, by asking is_valid_64k_chunk(). If
473 so, we decide for ourselves whether each x86-level (4 K) page in
474 the chunk is safe to inspect. If yes, we enquire with
475 is_valid_address() whether or not each of the 1024 word-locations
476 on the page is valid. Only if so are that address and its contents
477 passed to notify_word.
478
479 This is all to avoid duplication of this machinery between the
480 memcheck and addrcheck skins.
481*/
482static
483UInt vg_scan_all_valid_memory ( Bool is_valid_64k_chunk ( UInt ),
484 Bool is_valid_address ( Addr ),
485 void (*notify_word)( Addr, UInt ) )
486{
487 /* All volatile, because some gccs seem paranoid about longjmp(). */
sewardj6ccdd712002-12-22 19:11:14 +0000488 volatile Bool anyValid;
sewardja4495682002-10-21 07:29:59 +0000489 volatile Addr pageBase, addr;
490 volatile UInt res, numPages, page, primaryMapNo;
491 volatile UInt page_first_word, nWordsNotified;
492
493 vki_ksigaction sigbus_saved;
494 vki_ksigaction sigbus_new;
495 vki_ksigaction sigsegv_saved;
496 vki_ksigaction sigsegv_new;
497 vki_ksigset_t blockmask_saved;
498 vki_ksigset_t unblockmask_new;
499
500 /* Temporarily install a new sigsegv and sigbus handler, and make
501 sure SIGBUS, SIGSEGV and SIGTERM are unblocked. (Perhaps the
502 first two can never be blocked anyway?) */
503
504 sigbus_new.ksa_handler = vg_scan_all_valid_memory_sighandler;
505 sigbus_new.ksa_flags = VKI_SA_ONSTACK | VKI_SA_RESTART;
506 sigbus_new.ksa_restorer = NULL;
507 res = VG_(ksigemptyset)( &sigbus_new.ksa_mask );
508 sk_assert(res == 0);
509
510 sigsegv_new.ksa_handler = vg_scan_all_valid_memory_sighandler;
511 sigsegv_new.ksa_flags = VKI_SA_ONSTACK | VKI_SA_RESTART;
512 sigsegv_new.ksa_restorer = NULL;
513 res = VG_(ksigemptyset)( &sigsegv_new.ksa_mask );
514 sk_assert(res == 0+0);
515
516 res = VG_(ksigemptyset)( &unblockmask_new );
517 res |= VG_(ksigaddset)( &unblockmask_new, VKI_SIGBUS );
518 res |= VG_(ksigaddset)( &unblockmask_new, VKI_SIGSEGV );
519 res |= VG_(ksigaddset)( &unblockmask_new, VKI_SIGTERM );
520 sk_assert(res == 0+0+0);
521
522 res = VG_(ksigaction)( VKI_SIGBUS, &sigbus_new, &sigbus_saved );
523 sk_assert(res == 0+0+0+0);
524
525 res = VG_(ksigaction)( VKI_SIGSEGV, &sigsegv_new, &sigsegv_saved );
526 sk_assert(res == 0+0+0+0+0);
527
528 res = VG_(ksigprocmask)( VKI_SIG_UNBLOCK, &unblockmask_new, &blockmask_saved );
529 sk_assert(res == 0+0+0+0+0+0);
530
531 /* The signal handlers are installed. Actually do the memory scan. */
532 numPages = 1 << (32-VKI_BYTES_PER_PAGE_BITS);
533 sk_assert(numPages == 1048576);
534 sk_assert(4096 == (1 << VKI_BYTES_PER_PAGE_BITS));
535
536 nWordsNotified = 0;
537
538 for (page = 0; page < numPages; page++) {
539
540 /* Base address of this 4k page. */
541 pageBase = page << VKI_BYTES_PER_PAGE_BITS;
542
543 /* Skip if this page is in an unused 64k chunk. */
544 primaryMapNo = pageBase >> 16;
545 if (!is_valid_64k_chunk(primaryMapNo))
546 continue;
547
sewardj6ccdd712002-12-22 19:11:14 +0000548 /* Next, establish whether or not we want to consider any
549 locations on this page. We need to do so before actually
550 prodding it, because prodding it when in fact it is not
551 needed can cause a page fault which under some rare
552 circumstances can cause the kernel to extend the stack
553 segment all the way down to here, which is seriously bad.
554 Hence: */
555 anyValid = False;
556 for (addr = pageBase; addr < pageBase+VKI_BYTES_PER_PAGE; addr += 4) {
557 if (is_valid_address(addr)) {
558 anyValid = True;
559 break;
560 }
561 }
562
563 if (!anyValid)
564 continue; /* nothing interesting here .. move to the next page */
565
sewardja4495682002-10-21 07:29:59 +0000566 /* Ok, we have to prod cautiously at the page and see if it
567 explodes or not. */
568 if (__builtin_setjmp(memscan_jmpbuf) == 0) {
569 /* try this ... */
570 page_first_word = * (volatile UInt*)pageBase;
571 /* we get here if we didn't get a fault */
572 /* Scan the page */
573 for (addr = pageBase; addr < pageBase+VKI_BYTES_PER_PAGE; addr += 4) {
574 if (is_valid_address(addr)) {
575 nWordsNotified++;
576 notify_word ( addr, *(UInt*)addr );
577 }
578 }
579 } else {
580 /* We get here if reading the first word of the page caused a
581 fault, which in turn caused the signal handler to longjmp.
582 Ignore this page. */
583 if (0)
584 VG_(printf)(
585 "vg_scan_all_valid_memory_sighandler: ignoring page at %p\n",
586 (void*)pageBase
587 );
588 }
589 }
590
591 /* Restore signal state to whatever it was before. */
592 res = VG_(ksigaction)( VKI_SIGBUS, &sigbus_saved, NULL );
593 sk_assert(res == 0 +0);
594
595 res = VG_(ksigaction)( VKI_SIGSEGV, &sigsegv_saved, NULL );
596 sk_assert(res == 0 +0 +0);
597
598 res = VG_(ksigprocmask)( VKI_SIG_SETMASK, &blockmask_saved, NULL );
599 sk_assert(res == 0 +0 +0 +0);
600
601 return nWordsNotified;
602}
603
604
605/*------------------------------------------------------------*/
606/*--- Detecting leaked (unreachable) malloc'd blocks. ---*/
607/*------------------------------------------------------------*/
608
609/* A block is either
610 -- Proper-ly reached; a pointer to its start has been found
611 -- Interior-ly reached; only an interior pointer to it has been found
612 -- Unreached; so far, no pointers to any part of it have been found.
613*/
614typedef
615 enum { Unreached, Interior, Proper }
616 Reachedness;
617
618/* A block record, used for generating err msgs. */
619typedef
620 struct _LossRecord {
621 struct _LossRecord* next;
622 /* Where these lost blocks were allocated. */
623 ExeContext* allocated_at;
624 /* Their reachability. */
625 Reachedness loss_mode;
626 /* Number of blocks and total # bytes involved. */
627 UInt total_bytes;
628 UInt num_blocks;
629 }
630 LossRecord;
631
632
633/* Find the i such that ptr points at or inside the block described by
634 shadows[i]. Return -1 if none found. This assumes that shadows[]
635 has been sorted on the ->data field. */
636
637#ifdef VG_DEBUG_LEAKCHECK
638/* Used to sanity-check the fast binary-search mechanism. */
639static
640Int find_shadow_for_OLD ( Addr ptr,
641 ShadowChunk** shadows,
642 Int n_shadows )
643
644{
645 Int i;
646 Addr a_lo, a_hi;
647 PROF_EVENT(70);
648 for (i = 0; i < n_shadows; i++) {
649 PROF_EVENT(71);
650 a_lo = shadows[i]->data;
651 a_hi = ((Addr)shadows[i]->data) + shadows[i]->size - 1;
652 if (a_lo <= ptr && ptr <= a_hi)
653 return i;
654 }
655 return -1;
656}
657#endif
658
659
660static
661Int find_shadow_for ( Addr ptr,
662 ShadowChunk** shadows,
663 Int n_shadows )
664{
665 Addr a_mid_lo, a_mid_hi;
666 Int lo, mid, hi, retVal;
667 /* VG_(printf)("find shadow for %p = ", ptr); */
668 retVal = -1;
669 lo = 0;
670 hi = n_shadows-1;
671 while (True) {
672 /* invariant: current unsearched space is from lo to hi,
673 inclusive. */
674 if (lo > hi) break; /* not found */
675
676 mid = (lo + hi) / 2;
677 a_mid_lo = shadows[mid]->data;
678 a_mid_hi = ((Addr)shadows[mid]->data) + shadows[mid]->size - 1;
679
680 if (ptr < a_mid_lo) {
681 hi = mid-1;
682 continue;
683 }
684 if (ptr > a_mid_hi) {
685 lo = mid+1;
686 continue;
687 }
688 sk_assert(ptr >= a_mid_lo && ptr <= a_mid_hi);
689 retVal = mid;
690 break;
691 }
692
693# ifdef VG_DEBUG_LEAKCHECK
694 vg_assert(retVal == find_shadow_for_OLD ( ptr, shadows, n_shadows ));
695# endif
696 /* VG_(printf)("%d\n", retVal); */
697 return retVal;
698}
699
700
701
702static
703void sort_malloc_shadows ( ShadowChunk** shadows, UInt n_shadows )
704{
705 Int incs[14] = { 1, 4, 13, 40, 121, 364, 1093, 3280,
706 9841, 29524, 88573, 265720,
707 797161, 2391484 };
708 Int lo = 0;
709 Int hi = n_shadows-1;
710 Int i, j, h, bigN, hp;
711 ShadowChunk* v;
712
713 bigN = hi - lo + 1; if (bigN < 2) return;
sewardj6219d672002-11-11 08:36:52 +0000714 hp = 0; while (hp < 14 && incs[hp] < bigN) hp++; hp--;
715 vg_assert(0 <= hp && hp < 14);
sewardja4495682002-10-21 07:29:59 +0000716
717 for (; hp >= 0; hp--) {
718 h = incs[hp];
719 i = lo + h;
720 while (1) {
721 if (i > hi) break;
722 v = shadows[i];
723 j = i;
724 while (shadows[j-h]->data > v->data) {
725 shadows[j] = shadows[j-h];
726 j = j - h;
727 if (j <= (lo + h - 1)) break;
728 }
729 shadows[j] = v;
730 i++;
731 }
732 }
733}
734
735
736/* Globals, for the callback used by VG_(detect_memory_leaks). */
737
738static ShadowChunk** vglc_shadows;
739static Int vglc_n_shadows;
740static Reachedness* vglc_reachedness;
741static Addr vglc_min_mallocd_addr;
742static Addr vglc_max_mallocd_addr;
743
744static
745void vg_detect_memory_leaks_notify_addr ( Addr a, UInt word_at_a )
746{
747 Int sh_no;
748 Addr ptr;
749
750 /* Rule out some known causes of bogus pointers. Mostly these do
751 not cause much trouble because only a few false pointers can
752 ever lurk in these places. This mainly stops it reporting that
753 blocks are still reachable in stupid test programs like this
754
755 int main (void) { char* a = malloc(100); return 0; }
756
757 which people seem inordinately fond of writing, for some reason.
758
759 Note that this is a complete kludge. It would be better to
760 ignore any addresses corresponding to valgrind.so's .bss and
761 .data segments, but I cannot think of a reliable way to identify
762 where the .bss segment has been put. If you can, drop me a
763 line.
764 */
765 if (VG_(within_stack)(a)) return;
766 if (VG_(within_m_state_static)(a)) return;
767 if (a == (Addr)(&vglc_min_mallocd_addr)) return;
768 if (a == (Addr)(&vglc_max_mallocd_addr)) return;
769
770 /* OK, let's get on and do something Useful for a change. */
771
772 ptr = (Addr)word_at_a;
773 if (ptr >= vglc_min_mallocd_addr && ptr <= vglc_max_mallocd_addr) {
774 /* Might be legitimate; we'll have to investigate further. */
775 sh_no = find_shadow_for ( ptr, vglc_shadows, vglc_n_shadows );
776 if (sh_no != -1) {
777 /* Found a block at/into which ptr points. */
778 sk_assert(sh_no >= 0 && sh_no < vglc_n_shadows);
779 sk_assert(ptr < vglc_shadows[sh_no]->data
780 + vglc_shadows[sh_no]->size);
781 /* Decide whether Proper-ly or Interior-ly reached. */
782 if (ptr == vglc_shadows[sh_no]->data) {
783 if (0) VG_(printf)("pointer at %p to %p\n", a, word_at_a );
784 vglc_reachedness[sh_no] = Proper;
785 } else {
786 if (vglc_reachedness[sh_no] == Unreached)
787 vglc_reachedness[sh_no] = Interior;
788 }
789 }
790 }
791}
792
793
sewardj99aac972002-12-26 01:53:45 +0000794/* Stuff for figuring out if a leak report should be suppressed. */
795static
796Bool leaksupp_matches_callers(Supp* su, Char caller_obj[][M_VG_ERRTXT],
797 Char caller_fun[][M_VG_ERRTXT])
798{
799 Int i;
800
801 for (i = 0; su->caller[i] != NULL; i++) {
802 switch (su->caller_ty[i]) {
803 case ObjName: if (VG_(string_match)(su->caller[i],
804 caller_obj[i])) break;
805 return False;
806 case FunName: if (VG_(string_match)(su->caller[i],
807 caller_fun[i])) break;
808 return False;
809 default: VG_(skin_panic)("leaksupp_matches_callers");
810 }
811 }
812
813 /* If we reach here, it's a match */
814 return True;
815}
816
817
818/* Does a leak record match a suppression? ie is this a suppressible
819 leak? Tries to minimise the number of symbol searches since they
820 are expensive. Copy n paste (more or less) of
821 is_suppressible_error. We have to pass in the actual value of
822 LeakSupp for comparison since this is the core and LeakSupp is a
823 skin-specific value. */
824static
825Bool is_suppressible_leak ( ExeContext* allocated_at,
826 UInt /*CoreErrorKind*/ leakSupp )
827{
828 Int i;
829
830 Char caller_obj[VG_N_SUPP_CALLERS][M_VG_ERRTXT];
831 Char caller_fun[VG_N_SUPP_CALLERS][M_VG_ERRTXT];
832
833 Supp* su;
834
835 /* get_objname_fnname() writes the function name and object name if
836 it finds them in the debug info. so the strings in the suppression
837 file should match these.
838 */
839
840 /* Initialise these strs so they are always safe to compare, even
841 if get_objname_fnname doesn't write anything to them. */
842 for (i = 0; i < VG_N_SUPP_CALLERS; i++)
843 caller_obj[i][0] = caller_fun[i][0] = 0;
844
845 for (i = 0; i < VG_N_SUPP_CALLERS && i < VG_(clo_backtrace_size); i++) {
846 VG_(get_objname_fnname) ( allocated_at->eips[i],
847 caller_obj[i], M_VG_ERRTXT,
848 caller_fun[i], M_VG_ERRTXT );
849 }
850
851 /* See if the leak record any suppression. */
852 for (su = VG_(get_suppressions)(); su != NULL; su = su->next) {
853 if (VG_(get_supp_kind)(su) == (CoreErrorKind)leakSupp
854 && leaksupp_matches_callers(su, caller_obj, caller_fun)) {
855 return True;
856 }
857 }
858 return False; /* no matches */
859}
860
sewardja4495682002-10-21 07:29:59 +0000861/* Top level entry point to leak detector. Call here, passing in
862 suitable address-validating functions (see comment at top of
863 vg_scan_all_valid_memory above). All this is to avoid duplication
864 of the leak-detection code for the Memcheck and Addrcheck skins.
865 Also pass in a skin-specific function to extract the .where field
866 for allocated blocks, an indication of the resolution wanted for
867 distinguishing different allocation points, and whether or not
868 reachable blocks should be shown.
869*/
870void VG_(generic_detect_memory_leaks) (
871 Bool is_valid_64k_chunk ( UInt ),
872 Bool is_valid_address ( Addr ),
873 ExeContext* get_where ( ShadowChunk* ),
874 VgRes leak_resolution,
sewardj99aac972002-12-26 01:53:45 +0000875 Bool show_reachable,
876 UInt /*CoreErrorKind*/ leakSupp
sewardja4495682002-10-21 07:29:59 +0000877)
878{
879 Int i;
880 Int blocks_leaked, bytes_leaked;
881 Int blocks_dubious, bytes_dubious;
882 Int blocks_reachable, bytes_reachable;
sewardj99aac972002-12-26 01:53:45 +0000883 Int blocks_suppressed, bytes_suppressed;
sewardja4495682002-10-21 07:29:59 +0000884 Int n_lossrecords;
885 UInt bytes_notified;
886
887 LossRecord* errlist;
888 LossRecord* p;
889
890 /* VG_(get_malloc_shadows) allocates storage for shadows */
891 vglc_shadows = VG_(get_malloc_shadows)( &vglc_n_shadows );
892 if (vglc_n_shadows == 0) {
893 sk_assert(vglc_shadows == NULL);
894 VG_(message)(Vg_UserMsg,
sewardjdc7a98f2002-12-15 13:35:40 +0000895 "No malloc'd blocks -- no leaks are possible.");
sewardja4495682002-10-21 07:29:59 +0000896 return;
897 }
898
899 VG_(message)(Vg_UserMsg,
900 "searching for pointers to %d not-freed blocks.",
901 vglc_n_shadows );
902 sort_malloc_shadows ( vglc_shadows, vglc_n_shadows );
903
904 /* Sanity check; assert that the blocks are now in order and that
905 they don't overlap. */
906 for (i = 0; i < vglc_n_shadows-1; i++) {
907 sk_assert( ((Addr)vglc_shadows[i]->data)
908 < ((Addr)vglc_shadows[i+1]->data) );
909 sk_assert( ((Addr)vglc_shadows[i]->data) + vglc_shadows[i]->size
910 < ((Addr)vglc_shadows[i+1]->data) );
911 }
912
913 vglc_min_mallocd_addr = ((Addr)vglc_shadows[0]->data);
914 vglc_max_mallocd_addr = ((Addr)vglc_shadows[vglc_n_shadows-1]->data)
915 + vglc_shadows[vglc_n_shadows-1]->size - 1;
916
917 vglc_reachedness
918 = VG_(malloc)( vglc_n_shadows * sizeof(Reachedness) );
919 for (i = 0; i < vglc_n_shadows; i++)
920 vglc_reachedness[i] = Unreached;
921
922 /* Do the scan of memory. */
923 bytes_notified
924 = VKI_BYTES_PER_WORD
925 * vg_scan_all_valid_memory (
926 is_valid_64k_chunk,
927 is_valid_address,
928 &vg_detect_memory_leaks_notify_addr
929 );
930
931 VG_(message)(Vg_UserMsg, "checked %d bytes.", bytes_notified);
932
sewardja4495682002-10-21 07:29:59 +0000933 /* Common up the lost blocks so we can print sensible error
934 messages. */
935
936 n_lossrecords = 0;
937 errlist = NULL;
938 for (i = 0; i < vglc_n_shadows; i++) {
939
940 /* 'where' stored in 'skin_extra' field; extract using function
941 supplied by the calling skin. */
942 ExeContext* where = get_where ( vglc_shadows[i] );
943
944 for (p = errlist; p != NULL; p = p->next) {
945 if (p->loss_mode == vglc_reachedness[i]
946 && VG_(eq_ExeContext) ( leak_resolution,
947 p->allocated_at,
948 where) ) {
949 break;
950 }
951 }
952 if (p != NULL) {
953 p->num_blocks ++;
954 p->total_bytes += vglc_shadows[i]->size;
955 } else {
956 n_lossrecords ++;
957 p = VG_(malloc)(sizeof(LossRecord));
958 p->loss_mode = vglc_reachedness[i];
959 p->allocated_at = where;
960 p->total_bytes = vglc_shadows[i]->size;
961 p->num_blocks = 1;
962 p->next = errlist;
963 errlist = p;
964 }
965 }
sewardj99aac972002-12-26 01:53:45 +0000966
967 /* Print out the commoned-up blocks and collect summary stats. */
sewardja4495682002-10-21 07:29:59 +0000968
sewardj99aac972002-12-26 01:53:45 +0000969 blocks_leaked = bytes_leaked = 0;
970 blocks_dubious = bytes_dubious = 0;
971 blocks_reachable = bytes_reachable = 0;
972 blocks_suppressed = bytes_suppressed = 0;
973
sewardja4495682002-10-21 07:29:59 +0000974 for (i = 0; i < n_lossrecords; i++) {
975 LossRecord* p_min = NULL;
976 UInt n_min = 0xFFFFFFFF;
977 for (p = errlist; p != NULL; p = p->next) {
978 if (p->num_blocks > 0 && p->total_bytes < n_min) {
979 n_min = p->total_bytes;
980 p_min = p;
981 }
982 }
983 sk_assert(p_min != NULL);
984
sewardj99aac972002-12-26 01:53:45 +0000985 if (is_suppressible_leak(p_min->allocated_at, leakSupp)) {
986 blocks_suppressed += p_min->num_blocks;
987 bytes_suppressed += p_min->total_bytes;
988 p_min->num_blocks = 0;
989 continue;
990 } else {
991 switch (p_min->loss_mode) {
992 case Unreached:
993 blocks_leaked += p_min->num_blocks;
994 bytes_leaked += p_min->total_bytes;
995 break;
996 case Interior:
997 blocks_dubious += p_min->num_blocks;
998 bytes_dubious += p_min->total_bytes;
999 break;
1000 case Proper:
1001 blocks_reachable += p_min->num_blocks;
1002 bytes_reachable += p_min->total_bytes;
1003 break;
1004 default:
1005 VG_(core_panic)("generic_detect_memory_leaks: "
1006 "unknown loss mode");
1007 }
1008 }
1009
sewardja4495682002-10-21 07:29:59 +00001010 if ( (!show_reachable) && (p_min->loss_mode == Proper)) {
1011 p_min->num_blocks = 0;
1012 continue;
1013 }
1014
1015 VG_(message)(Vg_UserMsg, "");
1016 VG_(message)(
1017 Vg_UserMsg,
1018 "%d bytes in %d blocks are %s in loss record %d of %d",
1019 p_min->total_bytes, p_min->num_blocks,
1020 p_min->loss_mode==Unreached ? "definitely lost" :
1021 (p_min->loss_mode==Interior ? "possibly lost"
1022 : "still reachable"),
1023 i+1, n_lossrecords
1024 );
1025 VG_(pp_ExeContext)(p_min->allocated_at);
1026 p_min->num_blocks = 0;
1027 }
1028
1029 VG_(message)(Vg_UserMsg, "");
1030 VG_(message)(Vg_UserMsg, "LEAK SUMMARY:");
1031 VG_(message)(Vg_UserMsg, " definitely lost: %d bytes in %d blocks.",
1032 bytes_leaked, blocks_leaked );
1033 VG_(message)(Vg_UserMsg, " possibly lost: %d bytes in %d blocks.",
1034 bytes_dubious, blocks_dubious );
1035 VG_(message)(Vg_UserMsg, " still reachable: %d bytes in %d blocks.",
1036 bytes_reachable, blocks_reachable );
sewardj99aac972002-12-26 01:53:45 +00001037 VG_(message)(Vg_UserMsg, " suppressed: %d bytes in %d blocks.",
1038 bytes_suppressed, blocks_suppressed );
sewardja4495682002-10-21 07:29:59 +00001039 if (!show_reachable) {
1040 VG_(message)(Vg_UserMsg,
1041 "Reachable blocks (those to which a pointer was found) are not shown.");
1042 VG_(message)(Vg_UserMsg,
1043 "To see them, rerun with: --show-reachable=yes");
1044 }
1045 VG_(message)(Vg_UserMsg, "");
1046
1047 VG_(free) ( vglc_shadows );
1048 VG_(free) ( vglc_reachedness );
1049}
1050
1051
1052/*--------------------------------------------------------------------*/
sewardjde4a1d02002-03-22 01:27:54 +00001053/*--- end vg_memory.c ---*/
1054/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00001055