1 /* mm_diff - Memory snapshooting and comparison */
3 /* Copyright (c) 2008-2013. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/ex_interface.h" /* internals of backtrace setup */
12 #include "xbt/mmalloc.h"
13 #include "mc/datatypes.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
16 "Logging specific to mm_diff in mmalloc");
18 xbt_dynar_t mc_heap_comparison_ignore;
19 xbt_dynar_t stacks_areas;
20 void *maestro_stack_start, *maestro_stack_end;
23 /********************************* Backtrace ***********************************/
24 /******************************************************************************/
26 static void mmalloc_backtrace_block_display(void* heapinfo, int block){
30 /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
31 /* fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
35 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
36 /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
38 /* xbt_ex_setup_backtrace(&e); */
39 /* if (e.used == 0) { */
40 /* fprintf(stderr, "(backtrace not set)\n"); */
41 /* } else if (e.bt_strings == NULL) { */
42 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
46 /* fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
47 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
48 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
53 static void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
57 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
58 /* e.used = XBT_BACKTRACE_SIZE; */
60 /* xbt_ex_setup_backtrace(&e); */
61 /* if (e.used == 0) { */
62 /* fprintf(stderr, "(backtrace not set)\n"); */
63 /* } else if (e.bt_strings == NULL) { */
64 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
68 /* fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
69 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
70 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
76 static void mmalloc_backtrace_display(void *addr){
78 /* size_t block, frag_nb; */
81 /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
83 /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
85 /* type = heap->heapinfo[block].type; */
88 /* case -1 : /\* Free block *\/ */
89 /* fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
92 /* case 0: /\* Large block *\/ */
93 /* mmalloc_backtrace_block_display(heap->heapinfo, block); */
95 /* default: /\* Fragmented block *\/ */
96 /* frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
97 /* if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
98 /* fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
101 /* mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
107 static int compare_backtrace(int b1, int f1, int b2, int f2){
110 for(i=0; i< XBT_BACKTRACE_SIZE; i++){
111 if(heapinfo1[b1].busy_frag.bt[f1][i] != heapinfo2[b2].busy_frag.bt[f2][i]){
112 //mmalloc_backtrace_fragment_display((void*)heapinfo1, b1, f1);
113 //mmalloc_backtrace_fragment_display((void*)heapinfo2, b2, f2);
118 for(i=0; i< heapinfo1[b1].busy_block.bt_size; i++){
119 if(heapinfo1[b1].busy_block.bt[i] != heapinfo2[b2].busy_block.bt[i]){
120 //mmalloc_backtrace_block_display((void*)heapinfo1, b1);
121 //mmalloc_backtrace_block_display((void*)heapinfo2, b2);
130 /*********************************** Heap comparison ***********************************/
131 /***************************************************************************************/
133 __thread void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
134 __thread malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
135 __thread size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
136 __thread xbt_dynar_t to_ignore1 = NULL, to_ignore2 = NULL;
137 __thread heap_area_t **equals_to1, **equals_to2;
139 /*********************************** Free functions ************************************/
141 static void heap_area_pair_free(heap_area_pair_t pair){
146 static void heap_area_pair_free_voidp(void *d){
147 heap_area_pair_free((heap_area_pair_t) * (void **) d);
150 static void heap_area_free(heap_area_t area){
155 /************************************************************************************/
157 static heap_area_t new_heap_area(int block, int fragment){
158 heap_area_t area = NULL;
159 area = xbt_new0(s_heap_area_t, 1);
161 area->fragment = fragment;
166 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
168 unsigned int cursor = 0;
169 heap_area_pair_t current_pair;
171 xbt_dynar_foreach(list, cursor, current_pair){
172 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
179 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
181 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
182 heap_area_pair_t pair = NULL;
183 pair = xbt_new0(s_heap_area_pair_t, 1);
184 pair->block1 = block1;
185 pair->fragment1 = fragment1;
186 pair->block2 = block2;
187 pair->fragment2 = fragment2;
189 xbt_dynar_push(list, &pair);
197 static size_t heap_comparison_ignore_size(xbt_dynar_t ignore_list, void *address){
199 unsigned int cursor = 0;
201 int end = xbt_dynar_length(ignore_list) - 1;
202 mc_heap_ignore_region_t region;
205 cursor = (start + end) / 2;
206 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(ignore_list, cursor, mc_heap_ignore_region_t);
207 if(region->address == address)
209 if(region->address < address)
211 if(region->address > address)
218 static int is_stack(void *address){
219 unsigned int cursor = 0;
220 stack_region_t stack;
222 xbt_dynar_foreach(stacks_areas, cursor, stack){
223 if(address == stack->address)
230 static int is_block_stack(int block){
231 unsigned int cursor = 0;
232 stack_region_t stack;
234 xbt_dynar_foreach(stacks_areas, cursor, stack){
235 if(block == stack->block)
242 static void match_equals(xbt_dynar_t list){
244 unsigned int cursor = 0;
245 heap_area_pair_t current_pair;
246 heap_area_t previous_area;
248 xbt_dynar_foreach(list, cursor, current_pair){
250 if(current_pair->fragment1 != -1){
252 if(equals_to1[current_pair->block1][current_pair->fragment1] != NULL){
253 previous_area = equals_to1[current_pair->block1][current_pair->fragment1];
254 heap_area_free(equals_to2[previous_area->block][previous_area->fragment]);
255 equals_to2[previous_area->block][previous_area->fragment] = NULL;
256 heap_area_free(previous_area);
258 if(equals_to2[current_pair->block2][current_pair->fragment2] != NULL){
259 previous_area = equals_to2[current_pair->block2][current_pair->fragment2];
260 heap_area_free(equals_to1[previous_area->block][previous_area->fragment]);
261 equals_to1[previous_area->block][previous_area->fragment] = NULL;
262 heap_area_free(previous_area);
265 equals_to1[current_pair->block1][current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
266 equals_to2[current_pair->block2][current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
270 if(equals_to1[current_pair->block1][0] != NULL){
271 previous_area = equals_to1[current_pair->block1][0];
272 heap_area_free(equals_to2[previous_area->block][0]);
273 equals_to2[previous_area->block][0] = NULL;
274 heap_area_free(previous_area);
276 if(equals_to2[current_pair->block2][0] != NULL){
277 previous_area = equals_to2[current_pair->block2][0];
278 heap_area_free(equals_to1[previous_area->block][0]);
279 equals_to1[previous_area->block][0] = NULL;
280 heap_area_free(previous_area);
283 equals_to1[current_pair->block1][0] = new_heap_area(current_pair->block2, current_pair->fragment2);
284 equals_to2[current_pair->block2][0] = new_heap_area(current_pair->block1, current_pair->fragment1);
291 static int equal_blocks(int b1, int b2){
293 if(equals_to1[b1][0]->block == b2 && equals_to2[b2][0]->block == b1)
299 static int equal_fragments(int b1, int f1, int b2, int f2){
301 if(equals_to1[b1][f1]->block == b2 && equals_to1[b1][f1]->fragment == f2 && equals_to2[b2][f2]->block == b1 && equals_to2[b2][f2]->fragment == f1)
307 int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t i1, xbt_dynar_t i2){
309 if((((struct mdesc *)heap1)->heaplimit != ((struct mdesc *)heap2)->heaplimit) || ((((struct mdesc *)heap1)->heapsize != ((struct mdesc *)heap2)->heapsize) ))
314 heaplimit = ((struct mdesc *)heap1)->heaplimit;
316 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
318 heapbase1 = (char *)heap1 + BLOCKSIZE;
319 heapbase2 = (char *)heap2 + BLOCKSIZE;
321 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)((struct mdesc *)heap1)->heapinfo - (char *)s_heap)));
322 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)((struct mdesc *)heap2)->heapinfo - (char *)s_heap)));
324 heapsize1 = heap1->heapsize;
325 heapsize2 = heap2->heapsize;
330 equals_to1 = malloc(heaplimit * sizeof(heap_area_t *));
331 for(i=0; i<=heaplimit; i++){
332 equals_to1[i] = malloc(MAX_FRAGMENT_PER_BLOCK * sizeof(heap_area_t));
333 for(j=0; j<MAX_FRAGMENT_PER_BLOCK; j++)
334 equals_to1[i][j] = NULL;
338 equals_to2 = malloc(heaplimit * sizeof(heap_area_t *));
339 for(i=0; i<=heaplimit; i++){
340 equals_to2[i] = malloc(MAX_FRAGMENT_PER_BLOCK * sizeof(heap_area_t));
341 for(j=0; j<MAX_FRAGMENT_PER_BLOCK; j++)
342 equals_to2[i][j] = NULL;
346 MC_ignore_global_variable("heaplimit");
347 MC_ignore_global_variable("s_heap");
348 MC_ignore_global_variable("heapbase1");
349 MC_ignore_global_variable("heapbase2");
350 MC_ignore_global_variable("heapinfo1");
351 MC_ignore_global_variable("heapinfo2");
352 MC_ignore_global_variable("heapsize1");
353 MC_ignore_global_variable("heapsize2");
354 MC_ignore_global_variable("to_ignore1");
355 MC_ignore_global_variable("to_ignore2");
356 MC_ignore_global_variable("equals_to1");
357 MC_ignore_global_variable("equals_to2");
364 void reset_heap_information(){
368 for(i=0; i<heaplimit; i++){
369 for(j=0; j<MAX_FRAGMENT_PER_BLOCK;j++){
370 heap_area_free(equals_to1[i][j]);
371 equals_to1[i][j] = NULL;
372 heap_area_free(equals_to2[i][j]);
373 equals_to2[i][j] = NULL;
380 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
381 heapinfo1 = NULL, heapinfo2 = NULL;
382 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
383 to_ignore1 = NULL, to_ignore2 = NULL;
384 equals_to1 = NULL, equals_to2 = NULL;
388 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
390 if(heap1 == NULL && heap2 == NULL){
391 XBT_DEBUG("Malloc descriptors null");
395 /* Start comparison */
396 size_t i1, i2, j1, j2, k;
397 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
398 int nb_diff1 = 0, nb_diff2 = 0;
400 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
402 int equal, res_compare = 0;
404 /* Check busy blocks*/
408 while(i1 <= heaplimit){
410 if(heapinfo1[i1].type == -1){ /* Free block */
415 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
417 if(heapinfo1[i1].type == 0){ /* Large block */
419 if(is_stack(addr_block1)){
420 for(k=0; k < heapinfo1[i1].busy_block.size; k++)
421 equals_to1[i1+k][0] = new_heap_area(i1, -1);
422 for(k=0; k < heapinfo2[i1].busy_block.size; k++)
423 equals_to2[i1+k][0] = new_heap_area(i1, -1);
424 i1 += heapinfo1[i1].busy_block.size;
428 if(equals_to1[i1][0] != NULL){
437 /* Try first to associate to same block in the other heap */
438 if(heapinfo2[i1].type == heapinfo1[i1].type){
440 if(equals_to2[i1][0] == NULL){
442 addr_block2 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
444 res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0);
446 if(res_compare == 0){
447 for(k=1; k < heapinfo2[i1].busy_block.size; k++)
448 equals_to2[i1+k][0] = new_heap_area(i1, -1);
449 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
450 equals_to1[i1+k][0] = new_heap_area(i1, -1);
452 i1 += heapinfo1[i1].busy_block.size;
455 xbt_dynar_reset(previous);
461 while(i2 <= heaplimit && !equal){
463 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
470 if(heapinfo2[i2].type != 0){
475 if(equals_to2[i2][0] != NULL){
480 res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0);
482 if(res_compare == 0){
483 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
484 equals_to2[i2+k][0] = new_heap_area(i1, -1);
485 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
486 equals_to1[i1+k][0] = new_heap_area(i2, -1);
488 i1 += heapinfo1[i1].busy_block.size;
491 xbt_dynar_reset(previous);
498 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1[i1].busy_block.busy_size, addr_block1);
504 }else{ /* Fragmented block */
506 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
508 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
511 if(equals_to1[i1][j1] != NULL)
514 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
519 /* Try first to associate to same fragment in the other heap */
520 if(heapinfo2[i1].type == heapinfo1[i1].type){
522 if(equals_to2[i1][j1] == NULL){
524 addr_block2 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
525 addr_frag2 = (void*) ((char *)addr_block2 + (j1 << ((xbt_mheap_t)s_heap)->heapinfo[i1].type));
527 res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0);
532 xbt_dynar_reset(previous);
538 while(i2 <= heaplimit && !equal){
540 if(heapinfo2[i2].type <= 0){
545 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
547 if(i2 == i1 && j2 == j1)
550 if(equals_to2[i2][j2] != NULL)
553 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
554 addr_frag2 = (void*) ((char *)addr_block2 + (j2 <<((xbt_mheap_t)s_heap)->heapinfo[i2].type));
556 res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0);
558 if(res_compare == 0){
560 xbt_dynar_reset(previous);
564 xbt_dynar_reset(previous);
573 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n", i1, j1, heapinfo1[i1].busy_frag.frag_size[j1], addr_frag1);
588 /* All blocks/fragments are equal to another block/fragment ? */
590 void *real_addr_frag1 = NULL, *real_addr_block1 = NULL, *real_addr_block2 = NULL, *real_addr_frag2 = NULL;
593 if(heapinfo1[i].type == 0){
595 if(heapinfo1[i].busy_block.busy_size > 0){
596 if(equals_to1[i][0] == NULL){
597 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
598 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
599 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
600 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
607 if(heapinfo1[i].type > 0){
608 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
609 real_addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
610 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
612 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
613 if(equals_to1[i][j] == NULL){
614 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
615 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
616 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
617 XBT_DEBUG("Block %zu, Fragment %zu (%p - %p) not found (size used = %zd)", i, j, addr_frag1, real_addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
618 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
630 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
635 if(heapinfo2[i].type == 0){
637 if(heapinfo2[i].busy_block.busy_size > 0){
638 if(equals_to2[i][0] == NULL){
639 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
640 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
641 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
642 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
649 if(heapinfo2[i].type > 0){
650 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
651 real_addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
652 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
654 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
655 if(equals_to2[i][j] == NULL){
656 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
657 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
658 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
659 XBT_DEBUG( "Block %zu, Fragment %zu (%p - %p) not found (size used = %zd)", i, j, addr_frag2, real_addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
660 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
672 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
674 xbt_dynar_free(&previous);
675 real_addr_frag1 = NULL, real_addr_block1 = NULL, real_addr_block2 = NULL, real_addr_frag2 = NULL;
677 return ((nb_diff1 > 0) || (nb_diff2 > 0));
680 static int compare_heap_area_without_type(void *real_area1, void *real_area2, void *area1, void *area2, xbt_dynar_t previous, xbt_dict_t all_types, xbt_dict_t other_types, int size, int check_ignore){
683 void *addr_pointed1, *addr_pointed2;
684 int pointer_align, ignore1, ignore2, res_compare;
688 if(check_ignore > 0){
689 if((ignore1 = heap_comparison_ignore_size(to_ignore1, (char *)real_area1 + i)) > 0){
690 if((ignore2 = heap_comparison_ignore_size(to_ignore2, (char *)real_area2 + i)) == ignore1){
698 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
700 pointer_align = (i / sizeof(void*)) * sizeof(void*);
701 addr_pointed1 = *((void **)((char *)area1 + pointer_align));
702 addr_pointed2 = *((void **)((char *)area2 + pointer_align));
704 if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
705 i = pointer_align + sizeof(void *);
707 }else if((addr_pointed1 > s_heap) && ((char *)addr_pointed1 < (char *)s_heap + STD_HEAP_SIZE)
708 && (addr_pointed2 > s_heap) && ((char *)addr_pointed2 < (char *)s_heap + STD_HEAP_SIZE)){
709 res_compare = compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, NULL, 0);
712 i = pointer_align + sizeof(void *);
729 static int compare_heap_area_with_type(void *real_area1, void *real_area2, void *area1, void *area2,
730 xbt_dynar_t previous, xbt_dict_t all_types, xbt_dict_t other_types, char *type_id,
731 int area_size, int check_ignore, int pointer_level){
733 if(is_stack(real_area1) && is_stack(real_area2))
736 size_t ignore1, ignore2;
738 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
741 dw_type_t type = xbt_dict_get_or_null(all_types, type_id);
742 dw_type_t subtype, subsubtype;
743 int res, elm_size, i, switch_types = 0;
744 unsigned int cursor = 0;
746 void *addr_pointed1, *addr_pointed2;;
751 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
753 if(strcmp(type->name, "char") == 0){ /* String, hence random (arbitrary ?) size */
754 if(real_area1 == real_area2)
757 return (memcmp(area1, area2, area_size) != 0);
759 if(area_size != -1 && type->size != area_size)
762 return (memcmp(area1, area2, type->size) != 0);
765 case e_dw_enumeration_type:
766 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
768 if(area_size != -1 && type->size != area_size)
771 return (memcmp(area1, area2, type->size) != 0);
774 return compare_heap_area_with_type(real_area1, real_area2, area1, area2, previous, all_types, other_types, type->dw_type_id, area_size, check_ignore, pointer_level);
776 case e_dw_const_type:
779 case e_dw_array_type:
780 subtype = xbt_dict_get_or_null(all_types, type->dw_type_id);
781 switch(subtype->type){
783 case e_dw_enumeration_type:
784 case e_dw_pointer_type:
785 case e_dw_structure_type:
786 case e_dw_union_type:
787 if(subtype->size == 0){ /*declaration of the type, need the complete description */
788 type_desc = get_type_description(all_types, subtype->name);
790 subtype = xbt_dict_get_or_null(all_types, type_desc);
792 subtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
796 elm_size = subtype->size;
799 case e_dw_volatile_type:
800 subsubtype = xbt_dict_get_or_null(all_types, subtype->dw_type_id);
801 if(subsubtype->size == 0){ /*declaration of the type, need the complete description */
802 type_desc = get_type_description(all_types, subsubtype->name);
804 subsubtype = xbt_dict_get_or_null(all_types, type_desc);
806 subsubtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
810 elm_size = subsubtype->size;
816 for(i=0; i<type->size; i++){
818 res = compare_heap_area_with_type((char *)real_area1 + (i*elm_size), (char *)real_area2 + (i*elm_size), (char *)area1 + (i*elm_size), (char *)area2 + (i*elm_size), previous, other_types, all_types, type->dw_type_id, type->size, check_ignore, pointer_level);
820 res = compare_heap_area_with_type((char *)real_area1 + (i*elm_size), (char *)real_area2 + (i*elm_size), (char *)area1 + (i*elm_size), (char *)area2 + (i*elm_size), previous, all_types, other_types, type->dw_type_id, type->size, check_ignore, pointer_level);
825 case e_dw_pointer_type:
826 if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(all_types, type->dw_type_id))->type == e_dw_subroutine_type){
827 addr_pointed1 = *((void **)(area1));
828 addr_pointed2 = *((void **)(area2));
829 return (addr_pointed1 != addr_pointed2);;
832 if(pointer_level > 1){ /* Array of pointers */
833 for(i=0; i<(area_size/sizeof(void *)); i++){
834 addr_pointed1 = *((void **)((char *)area1 + (i*sizeof(void *))));
835 addr_pointed2 = *((void **)((char *)area2 + (i*sizeof(void *))));
836 if(addr_pointed1 > s_heap && (char *)addr_pointed1 < (char*) s_heap + STD_HEAP_SIZE && addr_pointed2 > s_heap && (char *)addr_pointed2 < (char*) s_heap + STD_HEAP_SIZE)
837 res = compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level);
839 res = (addr_pointed1 != addr_pointed2);
844 addr_pointed1 = *((void **)(area1));
845 addr_pointed2 = *((void **)(area2));
846 if(addr_pointed1 > s_heap && (char *)addr_pointed1 < (char*) s_heap + STD_HEAP_SIZE && addr_pointed2 > s_heap && (char *)addr_pointed2 < (char*) s_heap + STD_HEAP_SIZE)
847 return compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level);
849 return (addr_pointed1 != addr_pointed2);
853 case e_dw_structure_type:
854 if(type->size == 0){ /*declaration of the structure, need the complete description */
855 type_desc = get_type_description(all_types, type->name);
857 type = xbt_dict_get_or_null(all_types, type_desc);
859 type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
863 if(area_size != -1 && type->size != area_size){
864 if(area_size>type->size && area_size%type->size == 0){
865 for(i=0; i<(area_size/type->size); i++){
867 res = compare_heap_area_with_type((char *)real_area1 + (i*type->size), (char *)real_area2 + (i*type->size), (char *)area1 + (i*type->size), (char *)area2 + (i*type->size), previous, other_types, all_types, type_id, -1, check_ignore, 0);
869 res = compare_heap_area_with_type((char *)real_area1 + (i*type->size), (char *)real_area2 + (i*type->size), (char *)area1 + (i*type->size), (char *)area2 + (i*type->size), previous, all_types, other_types, type_id, -1, check_ignore, 0);
878 xbt_dynar_foreach(type->members, cursor, member){
880 res = compare_heap_area_with_type((char *)real_area1 + member->offset, (char *)real_area2 + member->offset, (char *)area1 + member->offset, (char *)area2 + member->offset, previous, other_types, all_types, member->dw_type_id, -1, check_ignore, 0);
882 res = compare_heap_area_with_type((char *)real_area1 + member->offset, (char *)real_area2 + member->offset, (char *)area1 + member->offset, (char *)area2 + member->offset, previous, all_types, other_types, member->dw_type_id, -1, check_ignore, 0);
888 case e_dw_union_type:
889 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
892 return compare_heap_area_without_type(real_area1, real_area2, area1, area2, previous, all_types, other_types, type->size, check_ignore);
894 case e_dw_volatile_type:
895 return compare_heap_area_with_type(real_area1, real_area2, area1, area2, previous, all_types, other_types, type->dw_type_id, area_size, check_ignore, pointer_level);
905 int compare_heap_area(void *area1, void* area2, xbt_dynar_t previous, xbt_dict_t all_types, xbt_dict_t other_types, char *type_id, int pointer_level){
908 ssize_t block1, frag1, block2, frag2;
910 int check_ignore = 0;
912 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
913 void *area1_to_compare, *area2_to_compare;
914 dw_type_t type = NULL;
920 if(previous == NULL){
921 previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
925 block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
926 block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
928 if(is_block_stack((int)block1) && is_block_stack((int)block2)){
929 add_heap_area_pair(previous, block1, -1, block2, -1);
931 match_equals(previous);
932 xbt_dynar_free(&previous);
937 if(((char *)area1 < (char*)((xbt_mheap_t)s_heap)->heapbase) || (block1 > heapsize1) || (block1 < 1) || ((char *)area2 < (char*)((xbt_mheap_t)s_heap)->heapbase) || (block2 > heapsize2) || (block2 < 1)){
939 xbt_dynar_free(&previous);
944 addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
945 addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
948 type = xbt_dict_get_or_null(all_types, type_id);
950 if(type->dw_type_id == NULL){
951 type_desc = get_type_description(all_types, type->name);
953 type = xbt_dict_get_or_null(all_types, type_desc);
955 type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
957 type = xbt_dict_get_or_null(all_types, type->dw_type_id);
960 if((type->type == e_dw_pointer_type) || ((type->type == e_dw_base_type) && (!strcmp(type->name, "char"))))
963 type_size = type->size;
966 if((heapinfo1[block1].type == -1) && (heapinfo2[block2].type == -1)){ /* Free block */
969 match_equals(previous);
970 xbt_dynar_free(&previous);
974 }else if((heapinfo1[block1].type == 0) && (heapinfo2[block2].type == 0)){ /* Complete block */
976 if(equals_to1[block1][0] != NULL && equals_to2[block2][0] != NULL){
977 if(equal_blocks(block1, block2)){
979 match_equals(previous);
980 xbt_dynar_free(&previous);
987 if(type_size != heapinfo1[block1].busy_block.busy_size && type_size != heapinfo2[block2].busy_block.busy_size && strcmp(type->name, "s_smx_context") != 0)
991 if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size){
993 xbt_dynar_free(&previous);
998 if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size){
1000 xbt_dynar_free(&previous);
1005 if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
1007 match_equals(previous);
1008 xbt_dynar_free(&previous);
1013 size = heapinfo1[block1].busy_block.busy_size;
1017 match_equals(previous);
1018 xbt_dynar_free(&previous);
1026 area1_to_compare = addr_block1;
1027 area2_to_compare = addr_block2;
1029 if((heapinfo1[block1].busy_block.ignore > 0) && (heapinfo2[block2].busy_block.ignore == heapinfo1[block1].busy_block.ignore))
1030 check_ignore = heapinfo1[block1].busy_block.ignore;
1032 }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){ /* Fragmented block */
1034 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1035 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1037 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1038 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1040 area1_to_compare = addr_frag1;
1041 area2_to_compare = addr_frag2;
1043 if(type_size != -1){
1044 if(heapinfo1[block1].busy_frag.frag_size[frag1] == -1 || heapinfo2[block2].busy_frag.frag_size[frag2] == -1)
1046 if(type_size != heapinfo1[block1].busy_frag.frag_size[frag1] || type_size != heapinfo2[block2].busy_frag.frag_size[frag2])
1050 if(equals_to1[block1][frag1] != NULL && equals_to2[block2][frag2] != NULL){
1051 if(equal_fragments(block1, frag1, block2, frag2)){
1053 match_equals(previous);
1054 xbt_dynar_free(&previous);
1060 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1061 if(type_size == -1){
1065 xbt_dynar_free(&previous);
1071 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1073 match_equals(previous);
1074 xbt_dynar_free(&previous);
1079 size = heapinfo1[block1].busy_frag.frag_size[frag1];
1083 match_equals(previous);
1084 xbt_dynar_free(&previous);
1089 if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && ( heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1090 check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1095 xbt_dynar_free(&previous);
1102 /* Start comparison*/
1103 if(type_id != NULL){
1104 res_compare = compare_heap_area_with_type(area1, area2, area1_to_compare, area2_to_compare, previous, all_types, other_types, type_id, size, check_ignore, pointer_level);
1105 if(res_compare != 0){
1107 xbt_dynar_free(&previous);
1111 res_compare = compare_heap_area_without_type(area1, area2, area1_to_compare, area2_to_compare, previous, all_types, other_types, size, check_ignore);
1112 if(res_compare != 0){
1114 xbt_dynar_free(&previous);
1120 match_equals(previous);
1121 xbt_dynar_free(&previous);
1127 /*********************************************** Miscellaneous ***************************************************/
1128 /****************************************************************************************************************/
1131 int get_pointed_area_size(void *area, int heap){
1134 malloc_info *heapinfo;
1137 heapinfo = heapinfo1;
1139 heapinfo = heapinfo2;
1141 block = ((char*)area - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
1143 if(((char *)area < (char*)((xbt_mheap_t)s_heap)->heapbase) || (block > heapsize1) || (block < 1))
1146 if(heapinfo[block].type == -1){ /* Free block */
1148 }else if(heapinfo[block].type == 0){ /* Complete block */
1149 return (int)heapinfo[block].busy_block.busy_size;
1151 frag = ((uintptr_t) (ADDR2UINT (area) % (BLOCKSIZE))) >> heapinfo[block].type;
1152 return (int)heapinfo[block].busy_frag.frag_size[frag];
1157 char *get_type_description(xbt_dict_t types, char *type_name){
1159 xbt_dict_cursor_t dict_cursor;
1163 xbt_dict_foreach(types, dict_cursor, type_origin, type){
1164 if(type->name && (strcmp(type->name, type_name) == 0) && type->size > 0){
1165 xbt_dict_cursor_free(&dict_cursor);
1170 xbt_dict_cursor_free(&dict_cursor);
1176 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1179 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
1181 if(heap1 == NULL && heap1 == NULL){
1182 XBT_DEBUG("Malloc descriptors null");
1186 if(heap1->heaplimit != heap2->heaplimit){
1187 XBT_DEBUG("Different limit of valid info table indices");
1191 /* Heap information */
1192 heaplimit = ((struct mdesc *)heap1)->heaplimit;
1194 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1196 heapbase1 = (char *)heap1 + BLOCKSIZE;
1197 heapbase2 = (char *)heap2 + BLOCKSIZE;
1199 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
1200 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
1202 heapsize1 = heap1->heapsize;
1203 heapsize2 = heap2->heapsize;
1205 /* Start comparison */
1207 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1211 /* Check busy blocks*/
1215 while(i <= heaplimit){
1217 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
1218 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
1220 if(heapinfo1[i].type != heapinfo2[i].type){
1222 distance += BLOCKSIZE;
1223 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
1228 if(heapinfo1[i].type == -1){ /* Free block */
1233 if(heapinfo1[i].type == 0){ /* Large block */
1235 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
1236 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1237 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1238 XBT_DEBUG("Different larger of cluster at block %zu : %zu - %zu -> distance = %d", i, heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size, distance);
1242 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1243 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1244 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1245 XBT_DEBUG("Different size used oin large cluster at block %zu : %zu - %zu -> distance = %d", i, heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size, distance);
1251 //while(k < (heapinfo1[i].busy_block.busy_size)){
1252 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
1253 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
1261 }else { /* Fragmented block */
1263 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
1265 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
1266 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
1268 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
1273 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1274 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1275 XBT_DEBUG("Different size used in fragment %zu in block %zu : %d - %d -> distance = %d", j, i, heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j], distance);
1281 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1282 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
1283 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){