1 /* mm_diff - Memory snapshooting and comparison */
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
13 "Logging specific to mm_diff in mmalloc");
15 extern char *xbt_binary_name;
17 xbt_dynar_t mmalloc_ignore;
19 typedef struct s_heap_area_pair{
24 }s_heap_area_pair_t, *heap_area_pair_t;
26 static void heap_area_pair_free(heap_area_pair_t pair);
27 static void heap_area_pair_free_voidp(void *d);
28 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
29 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
31 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous);
32 static void match_equals(xbt_dynar_t list);
34 static size_t heap_comparison_ignore(void *address);
36 void mmalloc_backtrace_block_display(void* heapinfo, int block){
40 if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
41 XBT_DEBUG("No backtrace available for that block, sorry.");
45 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
46 e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size;
48 xbt_ex_setup_backtrace(&e);
50 XBT_DEBUG("(backtrace not set)");
51 } else if (e.bt_strings == NULL) {
52 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
56 XBT_DEBUG("Backtrace of where the block %d was malloced (%d frames):", block ,e.used);
57 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
58 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
64 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
68 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
69 e.used = XBT_BACKTRACE_SIZE;
71 xbt_ex_setup_backtrace(&e);
73 XBT_DEBUG("(backtrace not set)");
74 } else if (e.bt_strings == NULL) {
75 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
79 XBT_DEBUG("Backtrace of where the fragment %d in block %d was malloced (%d frames):", frag, block ,e.used);
80 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
81 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
87 void *s_heap, *heapbase1, *heapbase2;
88 malloc_info *heapinfo1, *heapinfo2;
89 size_t heaplimit, heapsize1, heapsize2;
93 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
95 if(heap1 == NULL && heap1 == NULL){
96 XBT_DEBUG("Malloc descriptors null");
100 if(heap1->heaplimit != heap2->heaplimit){
101 XBT_DEBUG("Different limit of valid info table indices");
105 /* Heap information */
106 heaplimit = ((struct mdesc *)heap1)->heaplimit;
108 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
110 heapbase1 = (char *)heap1 + BLOCKSIZE;
111 heapbase2 = (char *)heap2 + BLOCKSIZE;
113 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
114 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
116 heapsize1 = heap1->heapsize;
117 heapsize2 = heap2->heapsize;
119 /* Start comparison */
120 size_t i1, i2, j1, j2, k;
121 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
122 size_t frag_size1, frag_size2;
124 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
130 /* Check busy blocks*/
134 while(i1 < heaplimit){
139 if(heapinfo1[i1].type == -1){ /* Free block */
144 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
146 if(heapinfo1[i1].type == 0){ /* Large block */
148 while(i2 <= heaplimit && !equal){
150 if(heapinfo2[i2].type != 0){
155 if(heapinfo2[i2].busy_block.equal_to == 1){
160 if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
165 if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
170 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
173 add_heap_area_pair(previous, i1, -1, i2, -1);
174 if(!compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous)){
175 for(k=0; k < heapinfo2[i2].busy_block.size; k++)
176 heapinfo2[i2+k].busy_block.equal_to = 1;
177 for(k=0; k < heapinfo1[i1].busy_block.size; k++)
178 heapinfo1[i1+k].busy_block.equal_to = 1;
180 match_equals(previous);
182 xbt_dynar_reset(previous);
188 }else{ /* Fragmented block */
190 frag_size1 = 1 << heapinfo1[i1].type;
192 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
194 if(heapinfo1[i1].busy_frag.frag_size[j1] == 0) /* Free fragment */
197 addr_frag1 = (void*) ((char *)addr_block1 + (j1 * frag_size1));
202 while(i2 <= heaplimit && !equal){
204 if(heapinfo2[i2].type <= 0){
209 frag_size2 = 1 << heapinfo2[i2].type;
211 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
213 if(heapinfo2[i2].busy_frag.equal_to[j2] == 1){
217 if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]){ /* Different size_used */
221 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
222 addr_frag2 = (void*) ((char *)addr_block2 + (j2 * frag_size2));
225 add_heap_area_pair(previous, i1, j1, i2, j2);
226 if(!compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous)){
227 heapinfo2[i2].busy_frag.equal_to[j2] = 1;
228 heapinfo1[i1].busy_frag.equal_to[j1] = 1;
230 match_equals(previous);
233 xbt_dynar_reset(previous);
249 /* All blocks/fragments are equal to another block/fragment ? */
251 int nb_diff1 = 0, nb_diff2 = 0;
252 size_t frag_size = 0;
255 if(heapinfo1[i].type == 0){
256 if(heapinfo1[i].busy_block.busy_size > 0){
257 if(heapinfo1[i].busy_block.equal_to == -1){
258 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
259 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
260 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
261 mmalloc_backtrace_block_display((void*)heapinfo1, i);
267 if(heapinfo1[i].type > 0){
268 frag_size = 1 << heapinfo1[i].type;
269 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
270 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
271 if(heapinfo1[i].busy_frag.equal_to[j] == -1){
272 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
273 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
274 addr_frag1 = (void*) ((char *)addr_block1 + (j * frag_size));
275 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
276 mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
287 XBT_DEBUG("Different blocks or fragments in heap1 : %d\n", nb_diff1);
292 if(heapinfo2[i].type == 0){
293 if(heapinfo2[i].busy_block.busy_size > 0){
294 if(heapinfo2[i].busy_block.equal_to == -1){
295 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
296 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
297 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
298 mmalloc_backtrace_block_display((void*)heapinfo2, i);
304 if(heapinfo2[i].type > 0){
305 frag_size = 1 << heapinfo2[i].type;
306 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
307 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
308 if(heapinfo2[i].busy_frag.equal_to[j] == -1){
309 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
310 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
311 addr_frag2 = (void*) ((char *)addr_block2 + (j * frag_size));
312 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
313 mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
323 XBT_DEBUG("Different blocks or fragments in heap2 : %d\n", nb_diff2);
326 /* Reset equal information */
330 if(heapinfo1[i].type == 0){
331 heapinfo1[i].busy_block.equal_to = -1;
333 if(heapinfo1[i].type > 0){
334 for(j=0; j < MAX_FRAGMENT_PER_BLOCK; j++){
335 heapinfo1[i].busy_frag.equal_to[j] = -1;
344 if(heapinfo2[i].type == 0){
345 heapinfo2[i].busy_block.equal_to = -1;
347 if(heapinfo2[i].type > 0){
348 for(j=0; j < MAX_FRAGMENT_PER_BLOCK; j++){
349 heapinfo2[i].busy_frag.equal_to[j] = -1;
355 xbt_dynar_free(&previous);
357 return ((nb_diff1 > 0) || (nb_diff2 > 0));
361 static size_t heap_comparison_ignore(void *address){
362 unsigned int cursor = 0;
364 int end = xbt_dynar_length(mmalloc_ignore) - 1;
365 mc_ignore_region_t region;
368 cursor = (start + end) / 2;
369 region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
370 if(region->address == address)
372 if(region->address < address)
374 if(region->address > address)
382 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous){
384 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
385 void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
386 size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
387 size_t frag_size, frag_size1, frag_size2;
389 void *current_area1, *current_area2;
393 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
395 current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
396 if((ignore1 = heap_comparison_ignore(current_area1)) > 0){
397 current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
398 if((ignore2 = heap_comparison_ignore(current_area2)) == ignore1){
407 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
409 /* Check pointer difference */
410 pointer_align = (i / sizeof(void*)) * sizeof(void*);
411 address_pointed1 = *((void **)((char *)area1 + pointer_align));
412 address_pointed2 = *((void **)((char *)area2 + pointer_align));
414 /* Get pointed blocks number */
415 block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
416 block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
418 /* Check if valid blocks number */
419 if((char *)address_pointed1 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed1 > heapsize1 || block_pointed1 < 1 || (char *)address_pointed2 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed2 > heapsize2 || block_pointed2 < 1)
422 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
424 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
425 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
427 if(heapinfo1[block_pointed1].type == 0){ /* Large block */
429 if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
433 if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
437 if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
439 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous);
446 }else{ /* Fragmented block */
448 /* Get pointed fragments number */
449 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
450 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
452 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
455 frag_size = 1 << heapinfo1[block_pointed1].type;
457 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 * frag_size));
458 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 * frag_size));
460 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
462 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous);
473 if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
475 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
476 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
478 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
479 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
481 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
484 frag_size1 = 1 << heapinfo1[block_pointed1].type;
485 frag_size2 = 1 << heapinfo1[block_pointed2].type;
487 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 * frag_size1));
488 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 * frag_size2));
490 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
492 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous);
505 i = pointer_align + sizeof(void *);
519 static void heap_area_pair_free(heap_area_pair_t pair){
526 static void heap_area_pair_free_voidp(void *d)
528 heap_area_pair_free((heap_area_pair_t) * (void **) d);
531 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
533 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
534 heap_area_pair_t pair = NULL;
535 pair = xbt_new0(s_heap_area_pair_t, 1);
536 pair->block1 = block1;
537 pair->fragment1 = fragment1;
538 pair->block2 = block2;
539 pair->fragment2 = fragment2;
541 xbt_dynar_push(list, &pair);
549 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
551 unsigned int cursor = 0;
552 heap_area_pair_t current_pair;
554 xbt_dynar_foreach(list, cursor, current_pair){
555 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
562 static void match_equals(xbt_dynar_t list){
564 unsigned int cursor = 0;
565 heap_area_pair_t current_pair;
567 xbt_dynar_foreach(list, cursor, current_pair){
568 if(current_pair->fragment1 != -1){
569 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = 1;
570 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = 1;
572 heapinfo1[current_pair->block1].busy_block.equal_to = 1;
573 heapinfo2[current_pair->block2].busy_block.equal_to = 1;