blob: 42434de82026ebb72d4dabe4383618212a245b44 [file] [log] [blame]
njn3e884182003-04-15 13:03:23 +00001
2/*--------------------------------------------------------------------*/
3/*--- malloc/free wrappers for detecting errors and updating bits. ---*/
4/*--- mac_malloc_wrappers.c ---*/
5/*--------------------------------------------------------------------*/
6
7/*
8 This file is part of MemCheck, a heavyweight Valgrind skin for
9 detecting memory errors, and AddrCheck, a lightweight Valgrind skin
10 for detecting memory errors.
11
njn0e1b5142003-04-15 14:58:06 +000012 Copyright (C) 2000-2003 Julian Seward
njn3e884182003-04-15 13:03:23 +000013 jseward@acm.org
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
30 The GNU General Public License is contained in the file COPYING.
31*/
32
33#include "mac_shared.h"
34
35/*------------------------------------------------------------*/
36/*--- Defns ---*/
37/*------------------------------------------------------------*/
38
39/* Stats ... */
40static UInt cmalloc_n_mallocs = 0;
41static UInt cmalloc_n_frees = 0;
42static UInt cmalloc_bs_mallocd = 0;
43
44/* We want a 16B redzone on heap blocks for Addrcheck and Memcheck */
45UInt VG_(vg_malloc_redzone_szB) = 16;
46
sewardjf9025f72003-07-12 01:41:24 +000047/* Function pointers for the two skins to track interesting events. */
48void (*MAC_(new_mem_heap)) ( Addr a, UInt len, Bool is_inited );
49void (*MAC_(ban_mem_heap)) ( Addr a, UInt len );
50void (*MAC_(die_mem_heap)) ( Addr a, UInt len );
51void (*MAC_(copy_mem_heap))( Addr from, Addr to, UInt len );
52
53
njn3e884182003-04-15 13:03:23 +000054/*------------------------------------------------------------*/
55/*--- Tracking malloc'd and free'd blocks ---*/
56/*------------------------------------------------------------*/
57
58/* Record malloc'd blocks. Nb: Addrcheck and Memcheck construct this
59 separately in their respective initialisation functions. */
60VgHashTable MAC_(malloc_list) = NULL;
61
62/* Records blocks after freeing. */
63static MAC_Chunk* freed_list_start = NULL;
64static MAC_Chunk* freed_list_end = NULL;
65static Int freed_list_volume = 0;
66
67/* Put a shadow chunk on the freed blocks queue, possibly freeing up
68 some of the oldest blocks in the queue at the same time. */
69static void add_to_freed_queue ( MAC_Chunk* mc )
70{
71 MAC_Chunk* sc1;
72
73 /* Put it at the end of the freed list */
74 if (freed_list_end == NULL) {
75 sk_assert(freed_list_start == NULL);
76 freed_list_end = freed_list_start = mc;
77 freed_list_volume = mc->size;
78 } else {
79 sk_assert(freed_list_end->next == NULL);
80 freed_list_end->next = mc;
81 freed_list_end = mc;
82 freed_list_volume += mc->size;
83 }
84 mc->next = NULL;
85
86 /* Release enough of the oldest blocks to bring the free queue
87 volume below vg_clo_freelist_vol. */
88
89 while (freed_list_volume > MAC_(clo_freelist_vol)) {
90 sk_assert(freed_list_start != NULL);
91 sk_assert(freed_list_end != NULL);
92
93 sc1 = freed_list_start;
94 freed_list_volume -= sc1->size;
95 /* VG_(printf)("volume now %d\n", freed_list_volume); */
96 sk_assert(freed_list_volume >= 0);
97
98 if (freed_list_start == freed_list_end) {
99 freed_list_start = freed_list_end = NULL;
100 } else {
101 freed_list_start = sc1->next;
102 }
103 sc1->next = NULL; /* just paranoia */
104
105 /* free MAC_Chunk */
106 VG_(cli_free) ( (void*)(sc1->data) );
107 VG_(free) ( sc1 );
108 }
109}
110
111/* Return the first shadow chunk satisfying the predicate p. */
112MAC_Chunk* MAC_(first_matching_freed_MAC_Chunk) ( Bool (*p)(MAC_Chunk*) )
113{
114 MAC_Chunk* mc;
115
116 /* No point looking through freed blocks if we're not keeping
117 them around for a while... */
118 for (mc = freed_list_start; mc != NULL; mc = mc->next)
119 if (p(mc))
120 return mc;
121
122 return NULL;
123}
124
njn10785452003-05-20 16:38:24 +0000125/* Allocate its shadow chunk, put it on the appropriate list. */
126static
127void add_MAC_Chunk ( ThreadState* tst, Addr p, UInt size, MAC_AllocKind kind )
njn3e884182003-04-15 13:03:23 +0000128{
129 MAC_Chunk* mc;
130
131 mc = VG_(malloc)(sizeof(MAC_Chunk));
132 mc->data = p;
133 mc->size = size;
134 mc->allockind = kind;
135 mc->where = VG_(get_ExeContext)(tst);
136
sewardjf9025f72003-07-12 01:41:24 +0000137 /* The following line puts the shadow chunk, and hence the pointer
138 to the real chunk, off-limits to the client. This seems to be
139 necessary to make the leak checker work reliably. Problem is,
140 this seems to point to something deeper being wrong: this chunk
141 is allocated in the AR_SKIN arena and so should by default be
142 off-limits to the client anyway. */
143 MAC_(ban_mem_heap)( (Addr)mc, sizeof(MAC_Chunk));
144
njn3e884182003-04-15 13:03:23 +0000145 VG_(HT_add_node)( MAC_(malloc_list), (VgHashNode*)mc );
146}
147
148/*------------------------------------------------------------*/
149/*--- client_malloc(), etc ---*/
150/*------------------------------------------------------------*/
151
njn3e884182003-04-15 13:03:23 +0000152/* Allocate memory and note change in memory available */
njn10785452003-05-20 16:38:24 +0000153__inline__
154void MAC_(new_block) ( ThreadState* tst, Addr p, UInt size,
155 UInt rzB, Bool is_zeroed, MAC_AllocKind kind )
njn3e884182003-04-15 13:03:23 +0000156{
njn3e884182003-04-15 13:03:23 +0000157 VGP_PUSHCC(VgpCliMalloc);
158
159 cmalloc_n_mallocs ++;
160 cmalloc_bs_mallocd += size;
161
njn10785452003-05-20 16:38:24 +0000162 add_MAC_Chunk( tst, p, size, kind );
njn3e884182003-04-15 13:03:23 +0000163
njn10785452003-05-20 16:38:24 +0000164 MAC_(ban_mem_heap)( p-rzB, rzB );
njn3e884182003-04-15 13:03:23 +0000165 MAC_(new_mem_heap)( p, size, is_zeroed );
njn10785452003-05-20 16:38:24 +0000166 MAC_(ban_mem_heap)( p+size, rzB );
njn3e884182003-04-15 13:03:23 +0000167
168 VGP_POPCC(VgpCliMalloc);
njn3e884182003-04-15 13:03:23 +0000169}
170
171void* SK_(malloc) ( ThreadState* tst, Int n )
172{
173 if (n < 0) {
174 VG_(message)(Vg_UserMsg, "Warning: silly arg (%d) to malloc()", n );
175 return NULL;
176 } else {
njn10785452003-05-20 16:38:24 +0000177 Addr p = (Addr)VG_(cli_malloc)( VG_(clo_alignment), n );
178 MAC_(new_block) ( tst, p, n, VG_(vg_malloc_redzone_szB),
179 /*is_zeroed*/False, MAC_AllocMalloc );
180 return (void*)p;
njn3e884182003-04-15 13:03:23 +0000181 }
182}
183
184void* SK_(__builtin_new) ( ThreadState* tst, Int n )
185{
186 if (n < 0) {
187 VG_(message)(Vg_UserMsg, "Warning: silly arg (%d) to __builtin_new()", n);
188 return NULL;
189 } else {
njn10785452003-05-20 16:38:24 +0000190 Addr p = (Addr)VG_(cli_malloc)( VG_(clo_alignment), n );
191 MAC_(new_block) ( tst, p, n, VG_(vg_malloc_redzone_szB),
192 /*is_zeroed*/False, MAC_AllocNew );
193 return (void*)p;
njn3e884182003-04-15 13:03:23 +0000194 }
195}
196
197void* SK_(__builtin_vec_new) ( ThreadState* tst, Int n )
198{
199 if (n < 0) {
200 VG_(message)(Vg_UserMsg,
201 "Warning: silly arg (%d) to __builtin_vec_new()", n );
202 return NULL;
203 } else {
njn10785452003-05-20 16:38:24 +0000204 Addr p = (Addr)VG_(cli_malloc)( VG_(clo_alignment), n );
205 MAC_(new_block) ( tst, p, n, VG_(vg_malloc_redzone_szB),
206 /*is_zeroed*/False, MAC_AllocNewVec );
207 return (void*)p;
njn3e884182003-04-15 13:03:23 +0000208 }
209}
210
211void* SK_(memalign) ( ThreadState* tst, Int align, Int n )
212{
213 if (n < 0) {
214 VG_(message)(Vg_UserMsg, "Warning: silly arg (%d) to memalign()", n);
215 return NULL;
216 } else {
njn10785452003-05-20 16:38:24 +0000217 Addr p = (Addr)VG_(cli_malloc)( align, n );
218 MAC_(new_block) ( tst, p, n, VG_(vg_malloc_redzone_szB),
219 /*is_zeroed*/False, MAC_AllocMalloc );
220 return (void*)p;
njn3e884182003-04-15 13:03:23 +0000221 }
222}
223
224void* SK_(calloc) ( ThreadState* tst, Int nmemb, Int size1 )
225{
njn10785452003-05-20 16:38:24 +0000226 Int n, i;
njn3e884182003-04-15 13:03:23 +0000227
njn10785452003-05-20 16:38:24 +0000228 n = nmemb * size1;
njn3e884182003-04-15 13:03:23 +0000229
230 if (nmemb < 0 || size1 < 0) {
231 VG_(message)(Vg_UserMsg, "Warning: silly args (%d,%d) to calloc()",
232 nmemb, size1 );
233 return NULL;
234 } else {
njn10785452003-05-20 16:38:24 +0000235 Addr p = (Addr)VG_(cli_malloc)( VG_(clo_alignment), n );
236 MAC_(new_block) ( tst, p, n, VG_(vg_malloc_redzone_szB),
237 /*is_zeroed*/True, MAC_AllocMalloc );
238 for (i = 0; i < n; i++)
njn3e884182003-04-15 13:03:23 +0000239 ((UChar*)p)[i] = 0;
njn10785452003-05-20 16:38:24 +0000240 return (void*)p;
njn3e884182003-04-15 13:03:23 +0000241 }
242}
243
244static
245void die_and_free_mem ( ThreadState* tst, MAC_Chunk* mc,
njn10785452003-05-20 16:38:24 +0000246 MAC_Chunk** prev_chunks_next_ptr, UInt rzB )
njn3e884182003-04-15 13:03:23 +0000247{
248 /* Note: ban redzones again -- just in case user de-banned them
249 with a client request... */
njn10785452003-05-20 16:38:24 +0000250 MAC_(ban_mem_heap)( mc->data-rzB, rzB );
njn3e884182003-04-15 13:03:23 +0000251 MAC_(die_mem_heap)( mc->data, mc->size );
njn10785452003-05-20 16:38:24 +0000252 MAC_(ban_mem_heap)( mc->data+mc->size, rzB );
njn3e884182003-04-15 13:03:23 +0000253
254 /* Remove mc from the malloclist using prev_chunks_next_ptr to
255 avoid repeating the hash table lookup. Can't remove until at least
256 after free and free_mismatch errors are done because they use
257 describe_addr() which looks for it in malloclist. */
258 *prev_chunks_next_ptr = mc->next;
259
260 /* Record where freed */
261 mc->where = VG_(get_ExeContext) ( tst );
262
njn10785452003-05-20 16:38:24 +0000263 /* Put it out of harm's way for a while, if not from a client request */
264 if (MAC_AllocCustom != mc->allockind)
265 add_to_freed_queue ( mc );
266 else
267 VG_(free) ( mc );
njn3e884182003-04-15 13:03:23 +0000268}
269
270
njn10785452003-05-20 16:38:24 +0000271__inline__
272void MAC_(handle_free) ( ThreadState* tst, Addr p, UInt rzB,
273 MAC_AllocKind kind )
njn3e884182003-04-15 13:03:23 +0000274{
275 MAC_Chunk* mc;
276 MAC_Chunk** prev_chunks_next_ptr;
277
278 VGP_PUSHCC(VgpCliMalloc);
279
280 cmalloc_n_frees++;
281
282 mc = (MAC_Chunk*)VG_(HT_get_node) ( MAC_(malloc_list), (UInt)p,
283 (VgHashNode***)&prev_chunks_next_ptr );
njn3e884182003-04-15 13:03:23 +0000284 if (mc == NULL) {
njn10785452003-05-20 16:38:24 +0000285 MAC_(record_free_error) ( tst, p );
njn3e884182003-04-15 13:03:23 +0000286 VGP_POPCC(VgpCliMalloc);
287 return;
288 }
289
290 /* check if its a matching free() / delete / delete [] */
291 if (kind != mc->allockind) {
njn10785452003-05-20 16:38:24 +0000292 MAC_(record_freemismatch_error) ( tst, p );
njn3e884182003-04-15 13:03:23 +0000293 }
294
njn10785452003-05-20 16:38:24 +0000295 die_and_free_mem ( tst, mc, prev_chunks_next_ptr, rzB );
njn3e884182003-04-15 13:03:23 +0000296 VGP_POPCC(VgpCliMalloc);
297}
298
299void SK_(free) ( ThreadState* tst, void* p )
300{
njn10785452003-05-20 16:38:24 +0000301 MAC_(handle_free)(tst, (Addr)p, VG_(vg_malloc_redzone_szB), MAC_AllocMalloc);
njn3e884182003-04-15 13:03:23 +0000302}
303
304void SK_(__builtin_delete) ( ThreadState* tst, void* p )
305{
njn10785452003-05-20 16:38:24 +0000306 MAC_(handle_free)(tst, (Addr)p, VG_(vg_malloc_redzone_szB), MAC_AllocNew);
njn3e884182003-04-15 13:03:23 +0000307}
308
309void SK_(__builtin_vec_delete) ( ThreadState* tst, void* p )
310{
njn10785452003-05-20 16:38:24 +0000311 MAC_(handle_free)(tst, (Addr)p, VG_(vg_malloc_redzone_szB), MAC_AllocNewVec);
njn3e884182003-04-15 13:03:23 +0000312}
313
314void* SK_(realloc) ( ThreadState* tst, void* p, Int new_size )
315{
316 MAC_Chunk *mc;
317 MAC_Chunk **prev_chunks_next_ptr;
318 UInt i;
319
320 VGP_PUSHCC(VgpCliMalloc);
321
322 cmalloc_n_frees ++;
323 cmalloc_n_mallocs ++;
324 cmalloc_bs_mallocd += new_size;
325
326 if (new_size < 0) {
327 VG_(message)(Vg_UserMsg,
328 "Warning: silly arg (%d) to realloc()", new_size );
329 return NULL;
330 }
331
332 /* First try and find the block. */
333 mc = (MAC_Chunk*)VG_(HT_get_node) ( MAC_(malloc_list), (UInt)p,
334 (VgHashNode***)&prev_chunks_next_ptr );
335
336 if (mc == NULL) {
337 MAC_(record_free_error) ( tst, (Addr)p );
338 /* Perhaps we should return to the program regardless. */
339 VGP_POPCC(VgpCliMalloc);
340 return NULL;
341 }
342
343 /* check if its a matching free() / delete / delete [] */
344 if (MAC_AllocMalloc != mc->allockind) {
345 /* can not realloc a range that was allocated with new or new [] */
346 MAC_(record_freemismatch_error) ( tst, (Addr)p );
347 /* but keep going anyway */
348 }
349
350 if (mc->size == new_size) {
351 /* size unchanged */
352 VGP_POPCC(VgpCliMalloc);
353 return p;
354
355 } else if (mc->size > new_size) {
356 /* new size is smaller */
357 MAC_(die_mem_heap)( mc->data+new_size, mc->size-new_size );
358 mc->size = new_size;
359 VGP_POPCC(VgpCliMalloc);
360 return p;
361
362 } else {
363 /* new size is bigger */
364 Addr p_new;
365
366 /* Get new memory */
367 p_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size);
368
369 /* First half kept and copied, second half new,
370 red zones as normal */
371 MAC_(ban_mem_heap) ( p_new-VG_(vg_malloc_redzone_szB),
372 VG_(vg_malloc_redzone_szB) );
373 MAC_(copy_mem_heap)( (Addr)p, p_new, mc->size );
374 MAC_(new_mem_heap) ( p_new+mc->size, new_size-mc->size, /*inited*/False );
375 MAC_(ban_mem_heap) ( p_new+new_size, VG_(vg_malloc_redzone_szB) );
376
377 /* Copy from old to new */
378 for (i = 0; i < mc->size; i++)
379 ((UChar*)p_new)[i] = ((UChar*)p)[i];
380
381 /* Free old memory */
njn10785452003-05-20 16:38:24 +0000382 die_and_free_mem ( tst, mc, prev_chunks_next_ptr,
383 VG_(vg_malloc_redzone_szB) );
njn3e884182003-04-15 13:03:23 +0000384
385 /* this has to be after die_and_free_mem, otherwise the
386 former succeeds in shorting out the new block, not the
387 old, in the case when both are on the same list. */
388 add_MAC_Chunk ( tst, p_new, new_size, MAC_AllocMalloc );
389
390 VGP_POPCC(VgpCliMalloc);
391 return (void*)p_new;
392 }
393}
394
395void MAC_(print_malloc_stats) ( void )
396{
397 UInt nblocks = 0, nbytes = 0;
398
399 /* Mmm... more lexical scoping */
400 void count_one_chunk(VgHashNode* node) {
401 MAC_Chunk* mc = (MAC_Chunk*)node;
402 nblocks ++;
403 nbytes += mc->size;
404 }
405
406 if (VG_(clo_verbosity) == 0)
407 return;
408
409 /* Count memory still in use. */
410 VG_(HT_apply_to_all_nodes)(MAC_(malloc_list), count_one_chunk);
411
412 VG_(message)(Vg_UserMsg,
413 "malloc/free: in use at exit: %d bytes in %d blocks.",
414 nbytes, nblocks);
415 VG_(message)(Vg_UserMsg,
416 "malloc/free: %d allocs, %d frees, %u bytes allocated.",
417 cmalloc_n_mallocs,
418 cmalloc_n_frees, cmalloc_bs_mallocd);
419 if (VG_(clo_verbosity) > 1)
420 VG_(message)(Vg_UserMsg, "");
421}
422
423/*--------------------------------------------------------------------*/
424/*--- end mac_malloc_wrappers.c ---*/
425/*--------------------------------------------------------------------*/