blob: 239f2c10f3c26ff192f6019f57c768cba6ab637f [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
47/*------------------------------------------------------------*/
48/*--- Tracking malloc'd and free'd blocks ---*/
49/*------------------------------------------------------------*/
50
51/* Record malloc'd blocks. Nb: Addrcheck and Memcheck construct this
52 separately in their respective initialisation functions. */
53VgHashTable MAC_(malloc_list) = NULL;
54
55/* Records blocks after freeing. */
56static MAC_Chunk* freed_list_start = NULL;
57static MAC_Chunk* freed_list_end = NULL;
58static Int freed_list_volume = 0;
59
60/* Put a shadow chunk on the freed blocks queue, possibly freeing up
61 some of the oldest blocks in the queue at the same time. */
62static void add_to_freed_queue ( MAC_Chunk* mc )
63{
64 MAC_Chunk* sc1;
65
66 /* Put it at the end of the freed list */
67 if (freed_list_end == NULL) {
68 sk_assert(freed_list_start == NULL);
69 freed_list_end = freed_list_start = mc;
70 freed_list_volume = mc->size;
71 } else {
72 sk_assert(freed_list_end->next == NULL);
73 freed_list_end->next = mc;
74 freed_list_end = mc;
75 freed_list_volume += mc->size;
76 }
77 mc->next = NULL;
78
79 /* Release enough of the oldest blocks to bring the free queue
80 volume below vg_clo_freelist_vol. */
81
82 while (freed_list_volume > MAC_(clo_freelist_vol)) {
83 sk_assert(freed_list_start != NULL);
84 sk_assert(freed_list_end != NULL);
85
86 sc1 = freed_list_start;
87 freed_list_volume -= sc1->size;
88 /* VG_(printf)("volume now %d\n", freed_list_volume); */
89 sk_assert(freed_list_volume >= 0);
90
91 if (freed_list_start == freed_list_end) {
92 freed_list_start = freed_list_end = NULL;
93 } else {
94 freed_list_start = sc1->next;
95 }
96 sc1->next = NULL; /* just paranoia */
97
98 /* free MAC_Chunk */
99 VG_(cli_free) ( (void*)(sc1->data) );
100 VG_(free) ( sc1 );
101 }
102}
103
104/* Return the first shadow chunk satisfying the predicate p. */
105MAC_Chunk* MAC_(first_matching_freed_MAC_Chunk) ( Bool (*p)(MAC_Chunk*) )
106{
107 MAC_Chunk* mc;
108
109 /* No point looking through freed blocks if we're not keeping
110 them around for a while... */
111 for (mc = freed_list_start; mc != NULL; mc = mc->next)
112 if (p(mc))
113 return mc;
114
115 return NULL;
116}
117
njn10785452003-05-20 16:38:24 +0000118/* Allocate its shadow chunk, put it on the appropriate list. */
119static
120void add_MAC_Chunk ( ThreadState* tst, Addr p, UInt size, MAC_AllocKind kind )
njn3e884182003-04-15 13:03:23 +0000121{
122 MAC_Chunk* mc;
123
124 mc = VG_(malloc)(sizeof(MAC_Chunk));
125 mc->data = p;
126 mc->size = size;
127 mc->allockind = kind;
128 mc->where = VG_(get_ExeContext)(tst);
129
130 VG_(HT_add_node)( MAC_(malloc_list), (VgHashNode*)mc );
131}
132
133/*------------------------------------------------------------*/
134/*--- client_malloc(), etc ---*/
135/*------------------------------------------------------------*/
136
137/* Function pointers for the two skins to track interesting events. */
138void (*MAC_(new_mem_heap)) ( Addr a, UInt len, Bool is_inited );
139void (*MAC_(ban_mem_heap)) ( Addr a, UInt len );
140void (*MAC_(die_mem_heap)) ( Addr a, UInt len );
141void (*MAC_(copy_mem_heap))( Addr from, Addr to, UInt len );
142
143/* Allocate memory and note change in memory available */
njn10785452003-05-20 16:38:24 +0000144__inline__
145void MAC_(new_block) ( ThreadState* tst, Addr p, UInt size,
146 UInt rzB, Bool is_zeroed, MAC_AllocKind kind )
njn3e884182003-04-15 13:03:23 +0000147{
njn3e884182003-04-15 13:03:23 +0000148 VGP_PUSHCC(VgpCliMalloc);
149
150 cmalloc_n_mallocs ++;
151 cmalloc_bs_mallocd += size;
152
njn10785452003-05-20 16:38:24 +0000153 add_MAC_Chunk( tst, p, size, kind );
njn3e884182003-04-15 13:03:23 +0000154
njn10785452003-05-20 16:38:24 +0000155 MAC_(ban_mem_heap)( p-rzB, rzB );
njn3e884182003-04-15 13:03:23 +0000156 MAC_(new_mem_heap)( p, size, is_zeroed );
njn10785452003-05-20 16:38:24 +0000157 MAC_(ban_mem_heap)( p+size, rzB );
njn3e884182003-04-15 13:03:23 +0000158
159 VGP_POPCC(VgpCliMalloc);
njn3e884182003-04-15 13:03:23 +0000160}
161
162void* SK_(malloc) ( ThreadState* tst, Int n )
163{
164 if (n < 0) {
165 VG_(message)(Vg_UserMsg, "Warning: silly arg (%d) to malloc()", n );
166 return NULL;
167 } else {
njn10785452003-05-20 16:38:24 +0000168 Addr p = (Addr)VG_(cli_malloc)( VG_(clo_alignment), n );
169 MAC_(new_block) ( tst, p, n, VG_(vg_malloc_redzone_szB),
170 /*is_zeroed*/False, MAC_AllocMalloc );
171 return (void*)p;
njn3e884182003-04-15 13:03:23 +0000172 }
173}
174
175void* SK_(__builtin_new) ( ThreadState* tst, Int n )
176{
177 if (n < 0) {
178 VG_(message)(Vg_UserMsg, "Warning: silly arg (%d) to __builtin_new()", n);
179 return NULL;
180 } else {
njn10785452003-05-20 16:38:24 +0000181 Addr p = (Addr)VG_(cli_malloc)( VG_(clo_alignment), n );
182 MAC_(new_block) ( tst, p, n, VG_(vg_malloc_redzone_szB),
183 /*is_zeroed*/False, MAC_AllocNew );
184 return (void*)p;
njn3e884182003-04-15 13:03:23 +0000185 }
186}
187
188void* SK_(__builtin_vec_new) ( ThreadState* tst, Int n )
189{
190 if (n < 0) {
191 VG_(message)(Vg_UserMsg,
192 "Warning: silly arg (%d) to __builtin_vec_new()", n );
193 return NULL;
194 } else {
njn10785452003-05-20 16:38:24 +0000195 Addr p = (Addr)VG_(cli_malloc)( VG_(clo_alignment), n );
196 MAC_(new_block) ( tst, p, n, VG_(vg_malloc_redzone_szB),
197 /*is_zeroed*/False, MAC_AllocNewVec );
198 return (void*)p;
njn3e884182003-04-15 13:03:23 +0000199 }
200}
201
202void* SK_(memalign) ( ThreadState* tst, Int align, Int n )
203{
204 if (n < 0) {
205 VG_(message)(Vg_UserMsg, "Warning: silly arg (%d) to memalign()", n);
206 return NULL;
207 } else {
njn10785452003-05-20 16:38:24 +0000208 Addr p = (Addr)VG_(cli_malloc)( align, n );
209 MAC_(new_block) ( tst, p, n, VG_(vg_malloc_redzone_szB),
210 /*is_zeroed*/False, MAC_AllocMalloc );
211 return (void*)p;
njn3e884182003-04-15 13:03:23 +0000212 }
213}
214
215void* SK_(calloc) ( ThreadState* tst, Int nmemb, Int size1 )
216{
njn10785452003-05-20 16:38:24 +0000217 Int n, i;
njn3e884182003-04-15 13:03:23 +0000218
njn10785452003-05-20 16:38:24 +0000219 n = nmemb * size1;
njn3e884182003-04-15 13:03:23 +0000220
221 if (nmemb < 0 || size1 < 0) {
222 VG_(message)(Vg_UserMsg, "Warning: silly args (%d,%d) to calloc()",
223 nmemb, size1 );
224 return NULL;
225 } else {
njn10785452003-05-20 16:38:24 +0000226 Addr p = (Addr)VG_(cli_malloc)( VG_(clo_alignment), n );
227 MAC_(new_block) ( tst, p, n, VG_(vg_malloc_redzone_szB),
228 /*is_zeroed*/True, MAC_AllocMalloc );
229 for (i = 0; i < n; i++)
njn3e884182003-04-15 13:03:23 +0000230 ((UChar*)p)[i] = 0;
njn10785452003-05-20 16:38:24 +0000231 return (void*)p;
njn3e884182003-04-15 13:03:23 +0000232 }
233}
234
235static
236void die_and_free_mem ( ThreadState* tst, MAC_Chunk* mc,
njn10785452003-05-20 16:38:24 +0000237 MAC_Chunk** prev_chunks_next_ptr, UInt rzB )
njn3e884182003-04-15 13:03:23 +0000238{
239 /* Note: ban redzones again -- just in case user de-banned them
240 with a client request... */
njn10785452003-05-20 16:38:24 +0000241 MAC_(ban_mem_heap)( mc->data-rzB, rzB );
njn3e884182003-04-15 13:03:23 +0000242 MAC_(die_mem_heap)( mc->data, mc->size );
njn10785452003-05-20 16:38:24 +0000243 MAC_(ban_mem_heap)( mc->data+mc->size, rzB );
njn3e884182003-04-15 13:03:23 +0000244
245 /* Remove mc from the malloclist using prev_chunks_next_ptr to
246 avoid repeating the hash table lookup. Can't remove until at least
247 after free and free_mismatch errors are done because they use
248 describe_addr() which looks for it in malloclist. */
249 *prev_chunks_next_ptr = mc->next;
250
251 /* Record where freed */
252 mc->where = VG_(get_ExeContext) ( tst );
253
njn10785452003-05-20 16:38:24 +0000254 /* Put it out of harm's way for a while, if not from a client request */
255 if (MAC_AllocCustom != mc->allockind)
256 add_to_freed_queue ( mc );
257 else
258 VG_(free) ( mc );
njn3e884182003-04-15 13:03:23 +0000259}
260
261
njn10785452003-05-20 16:38:24 +0000262__inline__
263void MAC_(handle_free) ( ThreadState* tst, Addr p, UInt rzB,
264 MAC_AllocKind kind )
njn3e884182003-04-15 13:03:23 +0000265{
266 MAC_Chunk* mc;
267 MAC_Chunk** prev_chunks_next_ptr;
268
269 VGP_PUSHCC(VgpCliMalloc);
270
271 cmalloc_n_frees++;
272
273 mc = (MAC_Chunk*)VG_(HT_get_node) ( MAC_(malloc_list), (UInt)p,
274 (VgHashNode***)&prev_chunks_next_ptr );
njn3e884182003-04-15 13:03:23 +0000275 if (mc == NULL) {
njn10785452003-05-20 16:38:24 +0000276 MAC_(record_free_error) ( tst, p );
njn3e884182003-04-15 13:03:23 +0000277 VGP_POPCC(VgpCliMalloc);
278 return;
279 }
280
281 /* check if its a matching free() / delete / delete [] */
282 if (kind != mc->allockind) {
njn10785452003-05-20 16:38:24 +0000283 MAC_(record_freemismatch_error) ( tst, p );
njn3e884182003-04-15 13:03:23 +0000284 }
285
njn10785452003-05-20 16:38:24 +0000286 die_and_free_mem ( tst, mc, prev_chunks_next_ptr, rzB );
njn3e884182003-04-15 13:03:23 +0000287 VGP_POPCC(VgpCliMalloc);
288}
289
290void SK_(free) ( ThreadState* tst, void* p )
291{
njn10785452003-05-20 16:38:24 +0000292 MAC_(handle_free)(tst, (Addr)p, VG_(vg_malloc_redzone_szB), MAC_AllocMalloc);
njn3e884182003-04-15 13:03:23 +0000293}
294
295void SK_(__builtin_delete) ( ThreadState* tst, void* p )
296{
njn10785452003-05-20 16:38:24 +0000297 MAC_(handle_free)(tst, (Addr)p, VG_(vg_malloc_redzone_szB), MAC_AllocNew);
njn3e884182003-04-15 13:03:23 +0000298}
299
300void SK_(__builtin_vec_delete) ( ThreadState* tst, void* p )
301{
njn10785452003-05-20 16:38:24 +0000302 MAC_(handle_free)(tst, (Addr)p, VG_(vg_malloc_redzone_szB), MAC_AllocNewVec);
njn3e884182003-04-15 13:03:23 +0000303}
304
305void* SK_(realloc) ( ThreadState* tst, void* p, Int new_size )
306{
307 MAC_Chunk *mc;
308 MAC_Chunk **prev_chunks_next_ptr;
309 UInt i;
310
311 VGP_PUSHCC(VgpCliMalloc);
312
313 cmalloc_n_frees ++;
314 cmalloc_n_mallocs ++;
315 cmalloc_bs_mallocd += new_size;
316
317 if (new_size < 0) {
318 VG_(message)(Vg_UserMsg,
319 "Warning: silly arg (%d) to realloc()", new_size );
320 return NULL;
321 }
322
323 /* First try and find the block. */
324 mc = (MAC_Chunk*)VG_(HT_get_node) ( MAC_(malloc_list), (UInt)p,
325 (VgHashNode***)&prev_chunks_next_ptr );
326
327 if (mc == NULL) {
328 MAC_(record_free_error) ( tst, (Addr)p );
329 /* Perhaps we should return to the program regardless. */
330 VGP_POPCC(VgpCliMalloc);
331 return NULL;
332 }
333
334 /* check if its a matching free() / delete / delete [] */
335 if (MAC_AllocMalloc != mc->allockind) {
336 /* can not realloc a range that was allocated with new or new [] */
337 MAC_(record_freemismatch_error) ( tst, (Addr)p );
338 /* but keep going anyway */
339 }
340
341 if (mc->size == new_size) {
342 /* size unchanged */
343 VGP_POPCC(VgpCliMalloc);
344 return p;
345
346 } else if (mc->size > new_size) {
347 /* new size is smaller */
348 MAC_(die_mem_heap)( mc->data+new_size, mc->size-new_size );
349 mc->size = new_size;
350 VGP_POPCC(VgpCliMalloc);
351 return p;
352
353 } else {
354 /* new size is bigger */
355 Addr p_new;
356
357 /* Get new memory */
358 p_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size);
359
360 /* First half kept and copied, second half new,
361 red zones as normal */
362 MAC_(ban_mem_heap) ( p_new-VG_(vg_malloc_redzone_szB),
363 VG_(vg_malloc_redzone_szB) );
364 MAC_(copy_mem_heap)( (Addr)p, p_new, mc->size );
365 MAC_(new_mem_heap) ( p_new+mc->size, new_size-mc->size, /*inited*/False );
366 MAC_(ban_mem_heap) ( p_new+new_size, VG_(vg_malloc_redzone_szB) );
367
368 /* Copy from old to new */
369 for (i = 0; i < mc->size; i++)
370 ((UChar*)p_new)[i] = ((UChar*)p)[i];
371
372 /* Free old memory */
njn10785452003-05-20 16:38:24 +0000373 die_and_free_mem ( tst, mc, prev_chunks_next_ptr,
374 VG_(vg_malloc_redzone_szB) );
njn3e884182003-04-15 13:03:23 +0000375
376 /* this has to be after die_and_free_mem, otherwise the
377 former succeeds in shorting out the new block, not the
378 old, in the case when both are on the same list. */
379 add_MAC_Chunk ( tst, p_new, new_size, MAC_AllocMalloc );
380
381 VGP_POPCC(VgpCliMalloc);
382 return (void*)p_new;
383 }
384}
385
386void MAC_(print_malloc_stats) ( void )
387{
388 UInt nblocks = 0, nbytes = 0;
389
390 /* Mmm... more lexical scoping */
391 void count_one_chunk(VgHashNode* node) {
392 MAC_Chunk* mc = (MAC_Chunk*)node;
393 nblocks ++;
394 nbytes += mc->size;
395 }
396
397 if (VG_(clo_verbosity) == 0)
398 return;
399
400 /* Count memory still in use. */
401 VG_(HT_apply_to_all_nodes)(MAC_(malloc_list), count_one_chunk);
402
403 VG_(message)(Vg_UserMsg,
404 "malloc/free: in use at exit: %d bytes in %d blocks.",
405 nbytes, nblocks);
406 VG_(message)(Vg_UserMsg,
407 "malloc/free: %d allocs, %d frees, %u bytes allocated.",
408 cmalloc_n_mallocs,
409 cmalloc_n_frees, cmalloc_bs_mallocd);
410 if (VG_(clo_verbosity) > 1)
411 VG_(message)(Vg_UserMsg, "");
412}
413
414/*--------------------------------------------------------------------*/
415/*--- end mac_malloc_wrappers.c ---*/
416/*--------------------------------------------------------------------*/