blob: aa9c68815f4c781ae04454af01f75fcd953a0190 [file] [log] [blame]
njn1d0825f2006-03-27 11:37:07 +00001
2/*--------------------------------------------------------------------*/
3/*--- malloc/free wrappers for detecting errors and updating bits. ---*/
4/*--- mc_malloc_wrappers.c ---*/
5/*--------------------------------------------------------------------*/
6
7/*
8 This file is part of MemCheck, a heavyweight Valgrind tool for
njne2656362007-03-10 02:27:44 +00009 detecting memory errors.
njn1d0825f2006-03-27 11:37:07 +000010
sewardj9ebd6e02007-01-08 06:01:59 +000011 Copyright (C) 2000-2007 Julian Seward
njn1d0825f2006-03-27 11:37:07 +000012 jseward@acm.org
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
29 The GNU General Public License is contained in the file COPYING.
30*/
31
32#include "pub_tool_basics.h"
33#include "pub_tool_execontext.h"
34#include "pub_tool_hashtable.h"
35#include "pub_tool_libcbase.h"
36#include "pub_tool_libcassert.h"
37#include "pub_tool_libcprint.h"
38#include "pub_tool_mallocfree.h"
39#include "pub_tool_options.h"
40#include "pub_tool_replacemalloc.h"
41#include "pub_tool_threadstate.h"
42#include "pub_tool_tooliface.h" // Needed for mc_include.h
sewardjc740d762006-10-05 17:59:23 +000043#include "pub_tool_stacktrace.h" // For VG_(get_and_pp_StackTrace)
njn1d0825f2006-03-27 11:37:07 +000044
45#include "mc_include.h"
46
47/*------------------------------------------------------------*/
48/*--- Defns ---*/
49/*------------------------------------------------------------*/
50
51/* Stats ... */
52static SizeT cmalloc_n_mallocs = 0;
53static SizeT cmalloc_n_frees = 0;
sewardjea9c15e2007-03-14 11:57:37 +000054static ULong cmalloc_bs_mallocd = 0;
njn1d0825f2006-03-27 11:37:07 +000055
sewardjc740d762006-10-05 17:59:23 +000056/* For debug printing to do with mempools: what stack trace
57 depth to show. */
58#define MEMPOOL_DEBUG_STACKTRACE_DEPTH 16
59
njn1d0825f2006-03-27 11:37:07 +000060
61/*------------------------------------------------------------*/
62/*--- Tracking malloc'd and free'd blocks ---*/
63/*------------------------------------------------------------*/
64
65/* Record malloc'd blocks. */
66VgHashTable MC_(malloc_list) = NULL;
67
68/* Memory pools. */
69VgHashTable MC_(mempool_list) = NULL;
70
71/* Records blocks after freeing. */
72static MC_Chunk* freed_list_start = NULL;
73static MC_Chunk* freed_list_end = NULL;
njnbf8c3502007-09-17 22:46:45 +000074static SSizeT freed_list_volume = 0;
njn1d0825f2006-03-27 11:37:07 +000075
76/* Put a shadow chunk on the freed blocks queue, possibly freeing up
77 some of the oldest blocks in the queue at the same time. */
78static void add_to_freed_queue ( MC_Chunk* mc )
79{
80 /* Put it at the end of the freed list */
81 if (freed_list_end == NULL) {
82 tl_assert(freed_list_start == NULL);
83 freed_list_end = freed_list_start = mc;
njn718d3b12006-12-16 00:54:12 +000084 freed_list_volume = mc->szB;
njn1d0825f2006-03-27 11:37:07 +000085 } else {
86 tl_assert(freed_list_end->next == NULL);
87 freed_list_end->next = mc;
88 freed_list_end = mc;
njn718d3b12006-12-16 00:54:12 +000089 freed_list_volume += mc->szB;
njn1d0825f2006-03-27 11:37:07 +000090 }
91 mc->next = NULL;
92
93 /* Release enough of the oldest blocks to bring the free queue
94 volume below vg_clo_freelist_vol. */
95
96 while (freed_list_volume > MC_(clo_freelist_vol)) {
97 MC_Chunk* mc1;
98
99 tl_assert(freed_list_start != NULL);
100 tl_assert(freed_list_end != NULL);
101
102 mc1 = freed_list_start;
njn718d3b12006-12-16 00:54:12 +0000103 freed_list_volume -= mc1->szB;
njn1d0825f2006-03-27 11:37:07 +0000104 /* VG_(printf)("volume now %d\n", freed_list_volume); */
105 tl_assert(freed_list_volume >= 0);
106
107 if (freed_list_start == freed_list_end) {
108 freed_list_start = freed_list_end = NULL;
109 } else {
110 freed_list_start = mc1->next;
111 }
112 mc1->next = NULL; /* just paranoia */
113
114 /* free MC_Chunk */
115 VG_(cli_free) ( (void*)(mc1->data) );
116 VG_(free) ( mc1 );
117 }
118}
119
120MC_Chunk* MC_(get_freed_list_head)(void)
121{
122 return freed_list_start;
123}
124
125/* Allocate its shadow chunk, put it on the appropriate list. */
126static
njn718d3b12006-12-16 00:54:12 +0000127MC_Chunk* create_MC_Chunk ( ThreadId tid, Addr p, SizeT szB,
njn1d0825f2006-03-27 11:37:07 +0000128 MC_AllocKind kind)
129{
130 MC_Chunk* mc = VG_(malloc)(sizeof(MC_Chunk));
131 mc->data = p;
njn718d3b12006-12-16 00:54:12 +0000132 mc->szB = szB;
njn1d0825f2006-03-27 11:37:07 +0000133 mc->allockind = kind;
sewardj39f34232007-11-09 23:02:28 +0000134 mc->where = VG_(record_ExeContext)(tid, 0/*first_ip_delta*/);
njn1d0825f2006-03-27 11:37:07 +0000135
136 /* Paranoia ... ensure the MC_Chunk is off-limits to the client, so
137 the mc->data field isn't visible to the leak checker. If memory
138 management is working correctly, any pointer returned by VG_(malloc)
139 should be noaccess as far as the client is concerned. */
njndbf7ca72006-03-31 11:57:59 +0000140 if (!MC_(check_mem_is_noaccess)( (Addr)mc, sizeof(MC_Chunk), NULL )) {
njn1d0825f2006-03-27 11:37:07 +0000141 VG_(tool_panic)("create_MC_Chunk: shadow area is accessible");
142 }
143 return mc;
144}
145
146/*------------------------------------------------------------*/
147/*--- client_malloc(), etc ---*/
148/*------------------------------------------------------------*/
149
150static Bool complain_about_silly_args(SizeT sizeB, Char* fn)
151{
152 // Cast to a signed type to catch any unexpectedly negative args. We're
153 // assuming here that the size asked for is not greater than 2^31 bytes
154 // (for 32-bit platforms) or 2^63 bytes (for 64-bit platforms).
155 if ((SSizeT)sizeB < 0) {
sewardj22faf712007-11-09 11:33:02 +0000156 if (!VG_(clo_xml))
157 VG_(message)(Vg_UserMsg, "Warning: silly arg (%ld) to %s()",
158 (SSizeT)sizeB, fn );
njn1d0825f2006-03-27 11:37:07 +0000159 return True;
160 }
161 return False;
162}
163
164static Bool complain_about_silly_args2(SizeT n, SizeT sizeB)
165{
166 if ((SSizeT)n < 0 || (SSizeT)sizeB < 0) {
sewardj22faf712007-11-09 11:33:02 +0000167 if (!VG_(clo_xml))
168 VG_(message)(Vg_UserMsg,
169 "Warning: silly args (%ld,%ld) to calloc()",
170 (SSizeT)n, (SSizeT)sizeB);
njn1d0825f2006-03-27 11:37:07 +0000171 return True;
172 }
173 return False;
174}
175
176/* Allocate memory and note change in memory available */
177__inline__
178void* MC_(new_block) ( ThreadId tid,
njn718d3b12006-12-16 00:54:12 +0000179 Addr p, SizeT szB, SizeT alignB, UInt rzB,
njn1d0825f2006-03-27 11:37:07 +0000180 Bool is_zeroed, MC_AllocKind kind, VgHashTable table)
181{
182 cmalloc_n_mallocs ++;
183
184 // Allocate and zero if necessary
185 if (p) {
186 tl_assert(MC_AllocCustom == kind);
187 } else {
188 tl_assert(MC_AllocCustom != kind);
njn718d3b12006-12-16 00:54:12 +0000189 p = (Addr)VG_(cli_malloc)( alignB, szB );
njn1d0825f2006-03-27 11:37:07 +0000190 if (!p) {
191 return NULL;
192 }
njn718d3b12006-12-16 00:54:12 +0000193 if (is_zeroed) VG_(memset)((void*)p, 0, szB);
njn1d0825f2006-03-27 11:37:07 +0000194 }
195
196 // Only update this stat if allocation succeeded.
sewardjea9c15e2007-03-14 11:57:37 +0000197 cmalloc_bs_mallocd += (ULong)szB;
njn1d0825f2006-03-27 11:37:07 +0000198
njn718d3b12006-12-16 00:54:12 +0000199 VG_(HT_add_node)( table, create_MC_Chunk(tid, p, szB, kind) );
njn1d0825f2006-03-27 11:37:07 +0000200
201 if (is_zeroed)
njn718d3b12006-12-16 00:54:12 +0000202 MC_(make_mem_defined)( p, szB );
njn1d0825f2006-03-27 11:37:07 +0000203 else
njn718d3b12006-12-16 00:54:12 +0000204 MC_(make_mem_undefined)( p, szB );
njn1d0825f2006-03-27 11:37:07 +0000205
206 return (void*)p;
207}
208
209void* MC_(malloc) ( ThreadId tid, SizeT n )
210{
211 if (complain_about_silly_args(n, "malloc")) {
212 return NULL;
213 } else {
214 return MC_(new_block) ( tid, 0, n, VG_(clo_alignment),
215 MC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MC_AllocMalloc,
216 MC_(malloc_list));
217 }
218}
219
220void* MC_(__builtin_new) ( ThreadId tid, SizeT n )
221{
222 if (complain_about_silly_args(n, "__builtin_new")) {
223 return NULL;
224 } else {
225 return MC_(new_block) ( tid, 0, n, VG_(clo_alignment),
226 MC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MC_AllocNew,
227 MC_(malloc_list));
228 }
229}
230
231void* MC_(__builtin_vec_new) ( ThreadId tid, SizeT n )
232{
233 if (complain_about_silly_args(n, "__builtin_vec_new")) {
234 return NULL;
235 } else {
236 return MC_(new_block) ( tid, 0, n, VG_(clo_alignment),
237 MC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MC_AllocNewVec,
238 MC_(malloc_list));
239 }
240}
241
njn718d3b12006-12-16 00:54:12 +0000242void* MC_(memalign) ( ThreadId tid, SizeT alignB, SizeT n )
njn1d0825f2006-03-27 11:37:07 +0000243{
244 if (complain_about_silly_args(n, "memalign")) {
245 return NULL;
246 } else {
njn718d3b12006-12-16 00:54:12 +0000247 return MC_(new_block) ( tid, 0, n, alignB,
njn1d0825f2006-03-27 11:37:07 +0000248 MC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MC_AllocMalloc,
249 MC_(malloc_list));
250 }
251}
252
253void* MC_(calloc) ( ThreadId tid, SizeT nmemb, SizeT size1 )
254{
255 if (complain_about_silly_args2(nmemb, size1)) {
256 return NULL;
257 } else {
258 return MC_(new_block) ( tid, 0, nmemb*size1, VG_(clo_alignment),
259 MC_MALLOC_REDZONE_SZB, /*is_zeroed*/True, MC_AllocMalloc,
260 MC_(malloc_list));
261 }
262}
263
264static
265void die_and_free_mem ( ThreadId tid, MC_Chunk* mc, SizeT rzB )
266{
267 /* Note: make redzones noaccess again -- just in case user made them
268 accessible with a client request... */
njn718d3b12006-12-16 00:54:12 +0000269 MC_(make_mem_noaccess)( mc->data-rzB, mc->szB + 2*rzB );
njn1d0825f2006-03-27 11:37:07 +0000270
271 /* Put it out of harm's way for a while, if not from a client request */
272 if (MC_AllocCustom != mc->allockind) {
273 /* Record where freed */
sewardj39f34232007-11-09 23:02:28 +0000274 mc->where = VG_(record_ExeContext) ( tid, 0/*first_ip_delta*/ );
njn1d0825f2006-03-27 11:37:07 +0000275 add_to_freed_queue ( mc );
276 } else {
277 VG_(free) ( mc );
278 }
279}
280
281__inline__
282void MC_(handle_free) ( ThreadId tid, Addr p, UInt rzB, MC_AllocKind kind )
283{
284 MC_Chunk* mc;
285
286 cmalloc_n_frees++;
287
288 mc = VG_(HT_remove) ( MC_(malloc_list), (UWord)p );
289 if (mc == NULL) {
290 MC_(record_free_error) ( tid, p );
291 } else {
292 /* check if it is a matching free() / delete / delete [] */
293 if (kind != mc->allockind) {
njn718d3b12006-12-16 00:54:12 +0000294 tl_assert(p == mc->data);
295 MC_(record_freemismatch_error) ( tid, mc );
njn1d0825f2006-03-27 11:37:07 +0000296 }
297 die_and_free_mem ( tid, mc, rzB );
298 }
299}
300
301void MC_(free) ( ThreadId tid, void* p )
302{
303 MC_(handle_free)(
304 tid, (Addr)p, MC_MALLOC_REDZONE_SZB, MC_AllocMalloc );
305}
306
307void MC_(__builtin_delete) ( ThreadId tid, void* p )
308{
309 MC_(handle_free)(
310 tid, (Addr)p, MC_MALLOC_REDZONE_SZB, MC_AllocNew);
311}
312
313void MC_(__builtin_vec_delete) ( ThreadId tid, void* p )
314{
315 MC_(handle_free)(
316 tid, (Addr)p, MC_MALLOC_REDZONE_SZB, MC_AllocNewVec);
317}
318
njn718d3b12006-12-16 00:54:12 +0000319void* MC_(realloc) ( ThreadId tid, void* p_old, SizeT new_szB )
njn1d0825f2006-03-27 11:37:07 +0000320{
321 MC_Chunk* mc;
322 void* p_new;
njn718d3b12006-12-16 00:54:12 +0000323 SizeT old_szB;
njn1d0825f2006-03-27 11:37:07 +0000324
325 cmalloc_n_frees ++;
326 cmalloc_n_mallocs ++;
sewardjea9c15e2007-03-14 11:57:37 +0000327 cmalloc_bs_mallocd += (ULong)new_szB;
njn1d0825f2006-03-27 11:37:07 +0000328
njn718d3b12006-12-16 00:54:12 +0000329 if (complain_about_silly_args(new_szB, "realloc"))
njn1d0825f2006-03-27 11:37:07 +0000330 return NULL;
331
332 /* Remove the old block */
333 mc = VG_(HT_remove) ( MC_(malloc_list), (UWord)p_old );
334 if (mc == NULL) {
335 MC_(record_free_error) ( tid, (Addr)p_old );
336 /* We return to the program regardless. */
337 return NULL;
338 }
339
340 /* check if its a matching free() / delete / delete [] */
341 if (MC_AllocMalloc != mc->allockind) {
342 /* can not realloc a range that was allocated with new or new [] */
njn718d3b12006-12-16 00:54:12 +0000343 tl_assert((Addr)p_old == mc->data);
344 MC_(record_freemismatch_error) ( tid, mc );
njn1d0825f2006-03-27 11:37:07 +0000345 /* but keep going anyway */
346 }
347
njn718d3b12006-12-16 00:54:12 +0000348 old_szB = mc->szB;
njn1d0825f2006-03-27 11:37:07 +0000349
njn718d3b12006-12-16 00:54:12 +0000350 if (old_szB == new_szB) {
njn1d0825f2006-03-27 11:37:07 +0000351 /* size unchanged */
sewardj39f34232007-11-09 23:02:28 +0000352 mc->where = VG_(record_ExeContext)(tid, 0/*first_ip_delta*/);
njn1d0825f2006-03-27 11:37:07 +0000353 p_new = p_old;
354
njn718d3b12006-12-16 00:54:12 +0000355 } else if (old_szB > new_szB) {
njn1d0825f2006-03-27 11:37:07 +0000356 /* new size is smaller */
njn718d3b12006-12-16 00:54:12 +0000357 MC_(make_mem_noaccess)( mc->data+new_szB, mc->szB-new_szB );
358 mc->szB = new_szB;
sewardj39f34232007-11-09 23:02:28 +0000359 mc->where = VG_(record_ExeContext)(tid, 0/*first_ip_delta*/);
njn1d0825f2006-03-27 11:37:07 +0000360 p_new = p_old;
361
362 } else {
363 /* new size is bigger */
364 /* Get new memory */
njn718d3b12006-12-16 00:54:12 +0000365 Addr a_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_szB);
njn1d0825f2006-03-27 11:37:07 +0000366
367 if (a_new) {
368 /* First half kept and copied, second half new, red zones as normal */
njndbf7ca72006-03-31 11:57:59 +0000369 MC_(make_mem_noaccess)( a_new-MC_MALLOC_REDZONE_SZB, MC_MALLOC_REDZONE_SZB );
njn718d3b12006-12-16 00:54:12 +0000370 MC_(copy_address_range_state)( (Addr)p_old, a_new, mc->szB );
371 MC_(make_mem_undefined)( a_new+mc->szB, new_szB-mc->szB );
372 MC_(make_mem_noaccess) ( a_new+new_szB, MC_MALLOC_REDZONE_SZB );
njn1d0825f2006-03-27 11:37:07 +0000373
374 /* Copy from old to new */
njn718d3b12006-12-16 00:54:12 +0000375 VG_(memcpy)((void*)a_new, p_old, mc->szB);
njn1d0825f2006-03-27 11:37:07 +0000376
377 /* Free old memory */
378 /* Nb: we have to allocate a new MC_Chunk for the new memory rather
379 than recycling the old one, so that any erroneous accesses to the
380 old memory are reported. */
381 die_and_free_mem ( tid, mc, MC_MALLOC_REDZONE_SZB );
382
383 // Allocate a new chunk.
njn718d3b12006-12-16 00:54:12 +0000384 mc = create_MC_Chunk( tid, a_new, new_szB, MC_AllocMalloc );
njn1d0825f2006-03-27 11:37:07 +0000385 }
386
387 p_new = (void*)a_new;
388 }
389
390 // Now insert the new mc (with a possibly new 'data' field) into
391 // malloc_list. If this realloc() did not increase the memory size, we
392 // will have removed and then re-added mc unnecessarily. But that's ok
393 // because shrinking a block with realloc() is (presumably) much rarer
394 // than growing it, and this way simplifies the growing case.
395 VG_(HT_add_node)( MC_(malloc_list), mc );
396
397 return p_new;
398}
399
400/* Memory pool stuff. */
401
402void MC_(create_mempool)(Addr pool, UInt rzB, Bool is_zeroed)
403{
sewardjc740d762006-10-05 17:59:23 +0000404 MC_Mempool* mp;
405
406 if (VG_(clo_verbosity) > 2) {
407 VG_(message)(Vg_UserMsg, "create_mempool(%p, %d, %d)",
408 pool, rzB, is_zeroed);
409 VG_(get_and_pp_StackTrace)
410 (VG_(get_running_tid)(), MEMPOOL_DEBUG_STACKTRACE_DEPTH);
411 }
412
413 mp = VG_(HT_lookup)(MC_(mempool_list), (UWord)pool);
414 if (mp != NULL) {
415 VG_(tool_panic)("MC_(create_mempool): duplicate pool creation");
416 }
417
418 mp = VG_(malloc)(sizeof(MC_Mempool));
njn1d0825f2006-03-27 11:37:07 +0000419 mp->pool = pool;
420 mp->rzB = rzB;
421 mp->is_zeroed = is_zeroed;
sewardj3f94a7d2007-08-25 07:19:08 +0000422 mp->chunks = VG_(HT_construct)( "MC_(create_mempool)" );
njn1d0825f2006-03-27 11:37:07 +0000423
424 /* Paranoia ... ensure this area is off-limits to the client, so
425 the mp->data field isn't visible to the leak checker. If memory
426 management is working correctly, anything pointer returned by
427 VG_(malloc) should be noaccess as far as the client is
428 concerned. */
njndbf7ca72006-03-31 11:57:59 +0000429 if (!MC_(check_mem_is_noaccess)( (Addr)mp, sizeof(MC_Mempool), NULL )) {
njn1d0825f2006-03-27 11:37:07 +0000430 VG_(tool_panic)("MC_(create_mempool): shadow area is accessible");
431 }
432
433 VG_(HT_add_node)( MC_(mempool_list), mp );
434}
435
436void MC_(destroy_mempool)(Addr pool)
437{
438 MC_Chunk* mc;
439 MC_Mempool* mp;
440
sewardjc740d762006-10-05 17:59:23 +0000441 if (VG_(clo_verbosity) > 2) {
442 VG_(message)(Vg_UserMsg, "destroy_mempool(%p)", pool);
443 VG_(get_and_pp_StackTrace)
444 (VG_(get_running_tid)(), MEMPOOL_DEBUG_STACKTRACE_DEPTH);
445 }
446
njn1d0825f2006-03-27 11:37:07 +0000447 mp = VG_(HT_remove) ( MC_(mempool_list), (UWord)pool );
448
449 if (mp == NULL) {
450 ThreadId tid = VG_(get_running_tid)();
451 MC_(record_illegal_mempool_error) ( tid, pool );
452 return;
453 }
454
455 // Clean up the chunks, one by one
456 VG_(HT_ResetIter)(mp->chunks);
457 while ( (mc = VG_(HT_Next)(mp->chunks)) ) {
458 /* Note: make redzones noaccess again -- just in case user made them
459 accessible with a client request... */
njn718d3b12006-12-16 00:54:12 +0000460 MC_(make_mem_noaccess)(mc->data-mp->rzB, mc->szB + 2*mp->rzB );
njn1d0825f2006-03-27 11:37:07 +0000461 }
462 // Destroy the chunk table
463 VG_(HT_destruct)(mp->chunks);
464
465 VG_(free)(mp);
466}
467
sewardjc740d762006-10-05 17:59:23 +0000468static Int
469mp_compar(void* n1, void* n2)
470{
471 MC_Chunk* mc1 = *(MC_Chunk**)n1;
472 MC_Chunk* mc2 = *(MC_Chunk**)n2;
473 return (mc1->data < mc2->data ? -1 : 1);
474}
475
476static void
477check_mempool_sane(MC_Mempool* mp)
478{
479 UInt n_chunks, i, bad = 0;
480 static UInt tick = 0;
481
482 MC_Chunk **chunks = (MC_Chunk**) VG_(HT_to_array)( mp->chunks, &n_chunks );
483 if (!chunks)
484 return;
485
486 if (VG_(clo_verbosity) > 1) {
487 if (tick++ >= 10000)
488 {
489 UInt total_pools = 0, total_chunks = 0;
490 MC_Mempool* mp2;
491
492 VG_(HT_ResetIter)(MC_(mempool_list));
493 while ( (mp2 = VG_(HT_Next)(MC_(mempool_list))) ) {
494 total_pools++;
495 VG_(HT_ResetIter)(mp2->chunks);
496 while (VG_(HT_Next)(mp2->chunks)) {
497 total_chunks++;
498 }
499 }
500
501 VG_(message)(Vg_UserMsg,
502 "Total mempools active: %d pools, %d chunks\n",
503 total_pools, total_chunks);
504 tick = 0;
505 }
506 }
507
508
509 VG_(ssort)((void*)chunks, n_chunks, sizeof(VgHashNode*), mp_compar);
510
511 /* Sanity check; assert that the blocks are now in order */
512 for (i = 0; i < n_chunks-1; i++) {
513 if (chunks[i]->data > chunks[i+1]->data) {
514 VG_(message)(Vg_UserMsg,
515 "Mempool chunk %d / %d is out of order "
516 "wrt. its successor",
517 i+1, n_chunks);
518 bad = 1;
519 }
520 }
521
522 /* Sanity check -- make sure they don't overlap */
523 for (i = 0; i < n_chunks-1; i++) {
njn718d3b12006-12-16 00:54:12 +0000524 if (chunks[i]->data + chunks[i]->szB > chunks[i+1]->data ) {
sewardjc740d762006-10-05 17:59:23 +0000525 VG_(message)(Vg_UserMsg,
526 "Mempool chunk %d / %d overlaps with its successor",
527 i+1, n_chunks);
528 bad = 1;
529 }
530 }
531
532 if (bad) {
533 VG_(message)(Vg_UserMsg,
534 "Bad mempool (%d chunks), dumping chunks for inspection:",
535 n_chunks);
536 for (i = 0; i < n_chunks; ++i) {
537 VG_(message)(Vg_UserMsg,
538 "Mempool chunk %d / %d: %d bytes [%x,%x), allocated:",
539 i+1,
540 n_chunks,
njn718d3b12006-12-16 00:54:12 +0000541 chunks[i]->szB,
sewardjc740d762006-10-05 17:59:23 +0000542 chunks[i]->data,
njn718d3b12006-12-16 00:54:12 +0000543 chunks[i]->data + chunks[i]->szB);
sewardjc740d762006-10-05 17:59:23 +0000544
545 VG_(pp_ExeContext)(chunks[i]->where);
546 }
547 }
548 VG_(free)(chunks);
549}
550
njn718d3b12006-12-16 00:54:12 +0000551void MC_(mempool_alloc)(ThreadId tid, Addr pool, Addr addr, SizeT szB)
njn1d0825f2006-03-27 11:37:07 +0000552{
sewardjc740d762006-10-05 17:59:23 +0000553 MC_Mempool* mp;
njn1d0825f2006-03-27 11:37:07 +0000554
sewardjc740d762006-10-05 17:59:23 +0000555 if (VG_(clo_verbosity) > 2) {
njn718d3b12006-12-16 00:54:12 +0000556 VG_(message)(Vg_UserMsg, "mempool_alloc(%p, %p, %d)", pool, addr, szB);
sewardjc740d762006-10-05 17:59:23 +0000557 VG_(get_and_pp_StackTrace) (tid, MEMPOOL_DEBUG_STACKTRACE_DEPTH);
558 }
559
560 mp = VG_(HT_lookup) ( MC_(mempool_list), (UWord)pool );
njn1d0825f2006-03-27 11:37:07 +0000561 if (mp == NULL) {
562 MC_(record_illegal_mempool_error) ( tid, pool );
563 } else {
sewardjc740d762006-10-05 17:59:23 +0000564 check_mempool_sane(mp);
njn718d3b12006-12-16 00:54:12 +0000565 MC_(new_block)(tid, addr, szB, /*ignored*/0, mp->rzB, mp->is_zeroed,
njn1d0825f2006-03-27 11:37:07 +0000566 MC_AllocCustom, mp->chunks);
sewardjc740d762006-10-05 17:59:23 +0000567 check_mempool_sane(mp);
njn1d0825f2006-03-27 11:37:07 +0000568 }
569}
570
571void MC_(mempool_free)(Addr pool, Addr addr)
572{
573 MC_Mempool* mp;
574 MC_Chunk* mc;
575 ThreadId tid = VG_(get_running_tid)();
576
577 mp = VG_(HT_lookup)(MC_(mempool_list), (UWord)pool);
578 if (mp == NULL) {
579 MC_(record_illegal_mempool_error)(tid, pool);
580 return;
581 }
582
sewardjc740d762006-10-05 17:59:23 +0000583 if (VG_(clo_verbosity) > 2) {
584 VG_(message)(Vg_UserMsg, "mempool_free(%p, %p)", pool, addr);
585 VG_(get_and_pp_StackTrace) (tid, MEMPOOL_DEBUG_STACKTRACE_DEPTH);
586 }
587
588 check_mempool_sane(mp);
njn1d0825f2006-03-27 11:37:07 +0000589 mc = VG_(HT_remove)(mp->chunks, (UWord)addr);
590 if (mc == NULL) {
591 MC_(record_free_error)(tid, (Addr)addr);
592 return;
593 }
594
sewardjc740d762006-10-05 17:59:23 +0000595 if (VG_(clo_verbosity) > 2) {
596 VG_(message)(Vg_UserMsg,
597 "mempool_free(%p, %p) freed chunk of %d bytes",
njn718d3b12006-12-16 00:54:12 +0000598 pool, addr, mc->szB);
sewardjc740d762006-10-05 17:59:23 +0000599 }
600
njn1d0825f2006-03-27 11:37:07 +0000601 die_and_free_mem ( tid, mc, mp->rzB );
sewardjc740d762006-10-05 17:59:23 +0000602 check_mempool_sane(mp);
njn1d0825f2006-03-27 11:37:07 +0000603}
604
sewardj2c1c9df2006-07-28 00:06:37 +0000605
njn718d3b12006-12-16 00:54:12 +0000606void MC_(mempool_trim)(Addr pool, Addr addr, SizeT szB)
sewardj2c1c9df2006-07-28 00:06:37 +0000607{
608 MC_Mempool* mp;
609 MC_Chunk* mc;
610 ThreadId tid = VG_(get_running_tid)();
611 UInt n_shadows, i;
612 VgHashNode** chunks;
613
sewardjc740d762006-10-05 17:59:23 +0000614 if (VG_(clo_verbosity) > 2) {
njn718d3b12006-12-16 00:54:12 +0000615 VG_(message)(Vg_UserMsg, "mempool_trim(%p, %p, %d)", pool, addr, szB);
sewardjc740d762006-10-05 17:59:23 +0000616 VG_(get_and_pp_StackTrace) (tid, MEMPOOL_DEBUG_STACKTRACE_DEPTH);
617 }
618
sewardj2c1c9df2006-07-28 00:06:37 +0000619 mp = VG_(HT_lookup)(MC_(mempool_list), (UWord)pool);
620 if (mp == NULL) {
621 MC_(record_illegal_mempool_error)(tid, pool);
622 return;
623 }
624
sewardjc740d762006-10-05 17:59:23 +0000625 check_mempool_sane(mp);
sewardj2c1c9df2006-07-28 00:06:37 +0000626 chunks = VG_(HT_to_array) ( mp->chunks, &n_shadows );
627 if (n_shadows == 0) {
628 tl_assert(chunks == NULL);
629 return;
630 }
631
632 tl_assert(chunks != NULL);
633 for (i = 0; i < n_shadows; ++i) {
sewardj8aeeaa92006-08-16 17:51:28 +0000634
sewardjc740d762006-10-05 17:59:23 +0000635 Addr lo, hi, min, max;
sewardj8aeeaa92006-08-16 17:51:28 +0000636
sewardj2c1c9df2006-07-28 00:06:37 +0000637 mc = (MC_Chunk*) chunks[i];
638
sewardj8aeeaa92006-08-16 17:51:28 +0000639 lo = mc->data;
njn718d3b12006-12-16 00:54:12 +0000640 hi = mc->szB == 0 ? mc->data : mc->data + mc->szB - 1;
sewardj2c1c9df2006-07-28 00:06:37 +0000641
njn718d3b12006-12-16 00:54:12 +0000642#define EXTENT_CONTAINS(x) ((addr <= (x)) && ((x) < addr + szB))
sewardj2c1c9df2006-07-28 00:06:37 +0000643
sewardj8aeeaa92006-08-16 17:51:28 +0000644 if (EXTENT_CONTAINS(lo) && EXTENT_CONTAINS(hi)) {
sewardj2c1c9df2006-07-28 00:06:37 +0000645
646 /* The current chunk is entirely within the trim extent: keep
647 it. */
648
649 continue;
650
sewardj8aeeaa92006-08-16 17:51:28 +0000651 } else if ( (! EXTENT_CONTAINS(lo)) &&
652 (! EXTENT_CONTAINS(hi)) ) {
sewardj2c1c9df2006-07-28 00:06:37 +0000653
654 /* The current chunk is entirely outside the trim extent:
655 delete it. */
656
657 if (VG_(HT_remove)(mp->chunks, (UWord)mc->data) == NULL) {
658 MC_(record_free_error)(tid, (Addr)mc->data);
659 VG_(free)(chunks);
sewardjc740d762006-10-05 17:59:23 +0000660 check_mempool_sane(mp);
sewardj2c1c9df2006-07-28 00:06:37 +0000661 return;
662 }
663 die_and_free_mem ( tid, mc, mp->rzB );
664
665 } else {
666
667 /* The current chunk intersects the trim extent: remove,
668 trim, and reinsert it. */
669
sewardj8aeeaa92006-08-16 17:51:28 +0000670 tl_assert(EXTENT_CONTAINS(lo) ||
671 EXTENT_CONTAINS(hi));
sewardj2c1c9df2006-07-28 00:06:37 +0000672 if (VG_(HT_remove)(mp->chunks, (UWord)mc->data) == NULL) {
673 MC_(record_free_error)(tid, (Addr)mc->data);
674 VG_(free)(chunks);
sewardjc740d762006-10-05 17:59:23 +0000675 check_mempool_sane(mp);
sewardj2c1c9df2006-07-28 00:06:37 +0000676 return;
677 }
678
sewardjc740d762006-10-05 17:59:23 +0000679 if (mc->data < addr) {
680 min = mc->data;
681 lo = addr;
682 } else {
683 min = addr;
684 lo = mc->data;
685 }
sewardj2c1c9df2006-07-28 00:06:37 +0000686
njn718d3b12006-12-16 00:54:12 +0000687 if (mc->data + szB > addr + szB) {
688 max = mc->data + szB;
689 hi = addr + szB;
sewardjc740d762006-10-05 17:59:23 +0000690 } else {
njn718d3b12006-12-16 00:54:12 +0000691 max = addr + szB;
692 hi = mc->data + szB;
sewardjc740d762006-10-05 17:59:23 +0000693 }
694
695 tl_assert(min <= lo);
sewardj2c1c9df2006-07-28 00:06:37 +0000696 tl_assert(lo < hi);
sewardjc740d762006-10-05 17:59:23 +0000697 tl_assert(hi <= max);
698
699 if (min < lo && !EXTENT_CONTAINS(min)) {
700 MC_(make_mem_noaccess)( min, lo - min);
701 }
702
703 if (hi < max && !EXTENT_CONTAINS(max)) {
704 MC_(make_mem_noaccess)( hi, max - hi );
705 }
706
sewardj2c1c9df2006-07-28 00:06:37 +0000707 mc->data = lo;
njn718d3b12006-12-16 00:54:12 +0000708 mc->szB = (UInt) (hi - lo);
sewardj2c1c9df2006-07-28 00:06:37 +0000709 VG_(HT_add_node)( mp->chunks, mc );
710 }
711
712#undef EXTENT_CONTAINS
713
714 }
sewardjc740d762006-10-05 17:59:23 +0000715 check_mempool_sane(mp);
sewardj2c1c9df2006-07-28 00:06:37 +0000716 VG_(free)(chunks);
717}
718
sewardjc740d762006-10-05 17:59:23 +0000719void MC_(move_mempool)(Addr poolA, Addr poolB)
720{
721 MC_Mempool* mp;
722
723 if (VG_(clo_verbosity) > 2) {
724 VG_(message)(Vg_UserMsg, "move_mempool(%p, %p)", poolA, poolB);
725 VG_(get_and_pp_StackTrace)
726 (VG_(get_running_tid)(), MEMPOOL_DEBUG_STACKTRACE_DEPTH);
727 }
728
729 mp = VG_(HT_remove) ( MC_(mempool_list), (UWord)poolA );
730
731 if (mp == NULL) {
732 ThreadId tid = VG_(get_running_tid)();
733 MC_(record_illegal_mempool_error) ( tid, poolA );
734 return;
735 }
736
737 mp->pool = poolB;
738 VG_(HT_add_node)( MC_(mempool_list), mp );
739}
740
njn718d3b12006-12-16 00:54:12 +0000741void MC_(mempool_change)(Addr pool, Addr addrA, Addr addrB, SizeT szB)
sewardjc740d762006-10-05 17:59:23 +0000742{
743 MC_Mempool* mp;
744 MC_Chunk* mc;
745 ThreadId tid = VG_(get_running_tid)();
746
747 if (VG_(clo_verbosity) > 2) {
748 VG_(message)(Vg_UserMsg, "mempool_change(%p, %p, %p, %d)",
njn718d3b12006-12-16 00:54:12 +0000749 pool, addrA, addrB, szB);
sewardjc740d762006-10-05 17:59:23 +0000750 VG_(get_and_pp_StackTrace) (tid, MEMPOOL_DEBUG_STACKTRACE_DEPTH);
751 }
752
753 mp = VG_(HT_lookup)(MC_(mempool_list), (UWord)pool);
754 if (mp == NULL) {
755 MC_(record_illegal_mempool_error)(tid, pool);
756 return;
757 }
758
759 check_mempool_sane(mp);
760
761 mc = VG_(HT_remove)(mp->chunks, (UWord)addrA);
762 if (mc == NULL) {
763 MC_(record_free_error)(tid, (Addr)addrA);
764 return;
765 }
766
767 mc->data = addrB;
njn718d3b12006-12-16 00:54:12 +0000768 mc->szB = szB;
sewardjc740d762006-10-05 17:59:23 +0000769 VG_(HT_add_node)( mp->chunks, mc );
770
771 check_mempool_sane(mp);
772}
773
774Bool MC_(mempool_exists)(Addr pool)
775{
776 MC_Mempool* mp;
777
778 mp = VG_(HT_lookup)(MC_(mempool_list), (UWord)pool);
779 if (mp == NULL) {
780 return False;
781 }
782 return True;
783}
784
785
njn1d0825f2006-03-27 11:37:07 +0000786/*------------------------------------------------------------*/
787/*--- Statistics printing ---*/
788/*------------------------------------------------------------*/
789
790void MC_(print_malloc_stats) ( void )
791{
792 MC_Chunk* mc;
793 SizeT nblocks = 0;
sewardjea9c15e2007-03-14 11:57:37 +0000794 ULong nbytes = 0;
njn1d0825f2006-03-27 11:37:07 +0000795
796 if (VG_(clo_verbosity) == 0)
797 return;
798 if (VG_(clo_xml))
799 return;
800
801 /* Count memory still in use. */
802 VG_(HT_ResetIter)(MC_(malloc_list));
803 while ( (mc = VG_(HT_Next)(MC_(malloc_list))) ) {
804 nblocks++;
sewardjea9c15e2007-03-14 11:57:37 +0000805 nbytes += (ULong)mc->szB;
njn1d0825f2006-03-27 11:37:07 +0000806 }
807
808 VG_(message)(Vg_UserMsg,
sewardjea9c15e2007-03-14 11:57:37 +0000809 "malloc/free: in use at exit: %,llu bytes in %,lu blocks.",
njn1d0825f2006-03-27 11:37:07 +0000810 nbytes, nblocks);
811 VG_(message)(Vg_UserMsg,
sewardjea9c15e2007-03-14 11:57:37 +0000812 "malloc/free: %,lu allocs, %,lu frees, %,llu bytes allocated.",
njn1d0825f2006-03-27 11:37:07 +0000813 cmalloc_n_mallocs,
814 cmalloc_n_frees, cmalloc_bs_mallocd);
815 if (VG_(clo_verbosity) > 1)
816 VG_(message)(Vg_UserMsg, "");
817}
818
819/*--------------------------------------------------------------------*/
820/*--- end ---*/
821/*--------------------------------------------------------------------*/