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;
382 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
383 heapinfo1 = NULL, heapinfo2 = NULL;
384 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
385 to_ignore1 = NULL, to_ignore2 = NULL;
386 equals_to1 = NULL, equals_to2 = NULL;
390 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
392 if(heap1 == NULL && heap2 == NULL){
393 XBT_DEBUG("Malloc descriptors null");
397 /* Start comparison */
398 size_t i1, i2, j1, j2, k;
399 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
400 int nb_diff1 = 0, nb_diff2 = 0;
402 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
404 int equal, res_compare = 0;
406 /* Check busy blocks*/
410 while(i1 <= heaplimit){
412 if(heapinfo1[i1].type == -1){ /* Free block */
417 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
419 if(heapinfo1[i1].type == 0){ /* Large block */
421 if(is_stack(addr_block1)){
422 for(k=0; k < heapinfo1[i1].busy_block.size; k++)
423 equals_to1[i1+k][0] = new_heap_area(i1, -1);
424 for(k=0; k < heapinfo2[i1].busy_block.size; k++)
425 equals_to2[i1+k][0] = new_heap_area(i1, -1);
426 i1 += heapinfo1[i1].busy_block.size;
430 if(equals_to1[i1][0] != NULL){
439 /* Try first to associate to same block in the other heap */
440 if(heapinfo2[i1].type == heapinfo1[i1].type){
442 if(equals_to2[i1][0] == NULL){
444 addr_block2 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
446 res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0);
448 if(res_compare == 0){
449 for(k=1; k < heapinfo2[i1].busy_block.size; k++)
450 equals_to2[i1+k][0] = new_heap_area(i1, -1);
451 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
452 equals_to1[i1+k][0] = new_heap_area(i1, -1);
454 i1 += heapinfo1[i1].busy_block.size;
457 xbt_dynar_reset(previous);
463 while(i2 <= heaplimit && !equal){
465 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
472 if(heapinfo2[i2].type != 0){
477 if(equals_to2[i2][0] != NULL){
482 res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0);
484 if(res_compare == 0){
485 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
486 equals_to2[i2+k][0] = new_heap_area(i1, -1);
487 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
488 equals_to1[i1+k][0] = new_heap_area(i2, -1);
490 i1 += heapinfo1[i1].busy_block.size;
493 xbt_dynar_reset(previous);
500 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1[i1].busy_block.busy_size, addr_block1);
506 }else{ /* Fragmented block */
508 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
510 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
513 if(equals_to1[i1][j1] != NULL)
516 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
521 /* Try first to associate to same fragment in the other heap */
522 if(heapinfo2[i1].type == heapinfo1[i1].type){
524 if(equals_to2[i1][j1] == NULL){
526 addr_block2 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
527 addr_frag2 = (void*) ((char *)addr_block2 + (j1 << ((xbt_mheap_t)s_heap)->heapinfo[i1].type));
529 res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0);
534 xbt_dynar_reset(previous);
540 while(i2 <= heaplimit && !equal){
542 if(heapinfo2[i2].type <= 0){
547 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
549 if(i2 == i1 && j2 == j1)
552 if(equals_to2[i2][j2] != NULL)
555 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
556 addr_frag2 = (void*) ((char *)addr_block2 + (j2 <<((xbt_mheap_t)s_heap)->heapinfo[i2].type));
558 res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0);
560 if(res_compare == 0){
562 xbt_dynar_reset(previous);
566 xbt_dynar_reset(previous);
575 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);
590 /* All blocks/fragments are equal to another block/fragment ? */
592 void *real_addr_frag1 = NULL, *real_addr_block1 = NULL, *real_addr_block2 = NULL, *real_addr_frag2 = NULL;
595 if(heapinfo1[i].type == 0){
597 if(heapinfo1[i].busy_block.busy_size > 0){
598 if(equals_to1[i][0] == NULL){
599 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
600 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
601 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
602 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
609 if(heapinfo1[i].type > 0){
610 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
611 real_addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
612 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
614 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
615 if(equals_to1[i][j] == NULL){
616 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
617 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
618 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
619 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]);
620 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
632 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
637 if(heapinfo2[i].type == 0){
639 if(heapinfo2[i].busy_block.busy_size > 0){
640 if(equals_to2[i][0] == NULL){
641 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
642 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
643 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
644 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
651 if(heapinfo2[i].type > 0){
652 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
653 real_addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
654 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
656 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
657 if(equals_to2[i][j] == NULL){
658 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
659 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
660 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
661 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]);
662 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
674 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
676 xbt_dynar_free(&previous);
677 real_addr_frag1 = NULL, real_addr_block1 = NULL, real_addr_block2 = NULL, real_addr_frag2 = NULL;
679 return ((nb_diff1 > 0) || (nb_diff2 > 0));
682 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){
685 void *addr_pointed1, *addr_pointed2;
686 int pointer_align, ignore1, ignore2, res_compare;
690 if(check_ignore > 0){
691 if((ignore1 = heap_comparison_ignore_size(to_ignore1, (char *)real_area1 + i)) > 0){
692 if((ignore2 = heap_comparison_ignore_size(to_ignore2, (char *)real_area2 + i)) == ignore1){
700 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
702 pointer_align = (i / sizeof(void*)) * sizeof(void*);
703 addr_pointed1 = *((void **)((char *)area1 + pointer_align));
704 addr_pointed2 = *((void **)((char *)area2 + pointer_align));
706 if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
707 i = pointer_align + sizeof(void *);
709 }else if((addr_pointed1 > s_heap) && ((char *)addr_pointed1 < (char *)s_heap + STD_HEAP_SIZE)
710 && (addr_pointed2 > s_heap) && ((char *)addr_pointed2 < (char *)s_heap + STD_HEAP_SIZE)){
711 res_compare = compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, NULL, 0);
714 i = pointer_align + sizeof(void *);
731 static int compare_heap_area_with_type(void *real_area1, void *real_area2, void *area1, void *area2,
732 xbt_dynar_t previous, xbt_dict_t all_types, xbt_dict_t other_types, char *type_id,
733 int area_size, int check_ignore, int pointer_level){
735 if(is_stack(real_area1) && is_stack(real_area2))
738 size_t ignore1, ignore2;
740 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
743 dw_type_t type = xbt_dict_get_or_null(all_types, type_id);
744 dw_type_t subtype, subsubtype;
745 int res, elm_size, i, switch_types = 0;
746 unsigned int cursor = 0;
748 void *addr_pointed1, *addr_pointed2;;
753 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
755 if(strcmp(type->name, "char") == 0){ /* String, hence random (arbitrary ?) size */
756 if(real_area1 == real_area2)
759 return (memcmp(area1, area2, area_size) != 0);
761 if(area_size != -1 && type->size != area_size)
764 return (memcmp(area1, area2, type->size) != 0);
767 case e_dw_enumeration_type:
768 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
770 if(area_size != -1 && type->size != area_size)
773 return (memcmp(area1, area2, type->size) != 0);
776 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);
778 case e_dw_const_type:
781 case e_dw_array_type:
782 subtype = xbt_dict_get_or_null(all_types, type->dw_type_id);
783 switch(subtype->type){
785 case e_dw_enumeration_type:
786 case e_dw_pointer_type:
787 case e_dw_structure_type:
788 case e_dw_union_type:
789 if(subtype->size == 0){ /*declaration of the type, need the complete description */
790 type_desc = get_type_description(all_types, subtype->name);
792 subtype = xbt_dict_get_or_null(all_types, type_desc);
794 subtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
798 elm_size = subtype->size;
801 case e_dw_volatile_type:
802 subsubtype = xbt_dict_get_or_null(all_types, subtype->dw_type_id);
803 if(subsubtype->size == 0){ /*declaration of the type, need the complete description */
804 type_desc = get_type_description(all_types, subsubtype->name);
806 subsubtype = xbt_dict_get_or_null(all_types, type_desc);
808 subsubtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
812 elm_size = subsubtype->size;
818 for(i=0; i<type->size; i++){
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, other_types, all_types, type->dw_type_id, type->size, check_ignore, pointer_level);
822 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);
827 case e_dw_pointer_type:
828 if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(all_types, type->dw_type_id))->type == e_dw_subroutine_type){
829 addr_pointed1 = *((void **)(area1));
830 addr_pointed2 = *((void **)(area2));
831 return (addr_pointed1 != addr_pointed2);;
834 if(pointer_level > 1){ /* Array of pointers */
835 for(i=0; i<(area_size/sizeof(void *)); i++){
836 addr_pointed1 = *((void **)((char *)area1 + (i*sizeof(void *))));
837 addr_pointed2 = *((void **)((char *)area2 + (i*sizeof(void *))));
838 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)
839 res = compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level);
841 res = (addr_pointed1 != addr_pointed2);
846 addr_pointed1 = *((void **)(area1));
847 addr_pointed2 = *((void **)(area2));
848 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)
849 return compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level);
851 return (addr_pointed1 != addr_pointed2);
855 case e_dw_structure_type:
856 if(type->size == 0){ /*declaration of the structure, need the complete description */
857 type_desc = get_type_description(all_types, type->name);
859 type = xbt_dict_get_or_null(all_types, type_desc);
861 type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
865 if(area_size != -1 && type->size != area_size){
866 if(area_size>type->size && area_size%type->size == 0){
867 for(i=0; i<(area_size/type->size); i++){
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, other_types, all_types, type_id, -1, check_ignore, 0);
871 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);
880 xbt_dynar_foreach(type->members, cursor, member){
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, other_types, all_types, member->dw_type_id, -1, check_ignore, 0);
884 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);
890 case e_dw_union_type:
891 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
894 return compare_heap_area_without_type(real_area1, real_area2, area1, area2, previous, all_types, other_types, type->size, check_ignore);
896 case e_dw_volatile_type:
897 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);
907 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){
910 ssize_t block1, frag1, block2, frag2;
912 int check_ignore = 0;
914 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
915 void *area1_to_compare, *area2_to_compare;
916 dw_type_t type = NULL;
922 if(previous == NULL){
923 previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
927 block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
928 block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
930 if(is_block_stack((int)block1) && is_block_stack((int)block2)){
931 add_heap_area_pair(previous, block1, -1, block2, -1);
933 match_equals(previous);
934 xbt_dynar_free(&previous);
939 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)){
941 xbt_dynar_free(&previous);
946 addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
947 addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
950 type = xbt_dict_get_or_null(all_types, type_id);
952 if(type->dw_type_id == NULL){
953 type_desc = get_type_description(all_types, type->name);
955 type = xbt_dict_get_or_null(all_types, type_desc);
957 type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
959 type = xbt_dict_get_or_null(all_types, type->dw_type_id);
962 if((type->type == e_dw_pointer_type) || ((type->type == e_dw_base_type) && (!strcmp(type->name, "char"))))
965 type_size = type->size;
968 if((heapinfo1[block1].type == -1) && (heapinfo2[block2].type == -1)){ /* Free block */
971 match_equals(previous);
972 xbt_dynar_free(&previous);
976 }else if((heapinfo1[block1].type == 0) && (heapinfo2[block2].type == 0)){ /* Complete block */
978 if(equals_to1[block1][0] != NULL && equals_to2[block2][0] != NULL){
979 if(equal_blocks(block1, block2)){
981 match_equals(previous);
982 xbt_dynar_free(&previous);
989 if(type_size != heapinfo1[block1].busy_block.busy_size && type_size != heapinfo2[block2].busy_block.busy_size && strcmp(type->name, "s_smx_context") != 0)
993 if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size){
995 xbt_dynar_free(&previous);
1000 if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size){
1002 xbt_dynar_free(&previous);
1007 if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
1009 match_equals(previous);
1010 xbt_dynar_free(&previous);
1015 size = heapinfo1[block1].busy_block.busy_size;
1019 match_equals(previous);
1020 xbt_dynar_free(&previous);
1028 area1_to_compare = addr_block1;
1029 area2_to_compare = addr_block2;
1031 if((heapinfo1[block1].busy_block.ignore > 0) && (heapinfo2[block2].busy_block.ignore == heapinfo1[block1].busy_block.ignore))
1032 check_ignore = heapinfo1[block1].busy_block.ignore;
1034 }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){ /* Fragmented block */
1036 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1037 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1039 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1040 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1042 area1_to_compare = addr_frag1;
1043 area2_to_compare = addr_frag2;
1045 if(type_size != -1){
1046 if(heapinfo1[block1].busy_frag.frag_size[frag1] == -1 || heapinfo2[block2].busy_frag.frag_size[frag2] == -1)
1048 if(type_size != heapinfo1[block1].busy_frag.frag_size[frag1] || type_size != heapinfo2[block2].busy_frag.frag_size[frag2])
1052 if(equals_to1[block1][frag1] != NULL && equals_to2[block2][frag2] != NULL){
1053 if(equal_fragments(block1, frag1, block2, frag2)){
1055 match_equals(previous);
1056 xbt_dynar_free(&previous);
1062 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1063 if(type_size == -1){
1067 xbt_dynar_free(&previous);
1073 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1075 match_equals(previous);
1076 xbt_dynar_free(&previous);
1081 size = heapinfo1[block1].busy_frag.frag_size[frag1];
1085 match_equals(previous);
1086 xbt_dynar_free(&previous);
1091 if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && ( heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1092 check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1097 xbt_dynar_free(&previous);
1104 /* Start comparison*/
1105 if(type_id != NULL){
1106 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);
1107 if(res_compare != 0){
1109 xbt_dynar_free(&previous);
1113 res_compare = compare_heap_area_without_type(area1, area2, area1_to_compare, area2_to_compare, previous, all_types, other_types, size, check_ignore);
1114 if(res_compare != 0){
1116 xbt_dynar_free(&previous);
1122 match_equals(previous);
1123 xbt_dynar_free(&previous);
1129 /*********************************************** Miscellaneous ***************************************************/
1130 /****************************************************************************************************************/
1133 int get_pointed_area_size(void *area, int heap){
1136 malloc_info *heapinfo;
1139 heapinfo = heapinfo1;
1141 heapinfo = heapinfo2;
1143 block = ((char*)area - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
1145 if(((char *)area < (char*)((xbt_mheap_t)s_heap)->heapbase) || (block > heapsize1) || (block < 1))
1148 if(heapinfo[block].type == -1){ /* Free block */
1150 }else if(heapinfo[block].type == 0){ /* Complete block */
1151 return (int)heapinfo[block].busy_block.busy_size;
1153 frag = ((uintptr_t) (ADDR2UINT (area) % (BLOCKSIZE))) >> heapinfo[block].type;
1154 return (int)heapinfo[block].busy_frag.frag_size[frag];
1159 char *get_type_description(xbt_dict_t types, char *type_name){
1161 xbt_dict_cursor_t dict_cursor;
1165 xbt_dict_foreach(types, dict_cursor, type_origin, type){
1166 if(type->name && (strcmp(type->name, type_name) == 0) && type->size > 0){
1167 xbt_dict_cursor_free(&dict_cursor);
1172 xbt_dict_cursor_free(&dict_cursor);
1178 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1181 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
1183 if(heap1 == NULL && heap1 == NULL){
1184 XBT_DEBUG("Malloc descriptors null");
1188 if(heap1->heaplimit != heap2->heaplimit){
1189 XBT_DEBUG("Different limit of valid info table indices");
1193 /* Heap information */
1194 heaplimit = ((struct mdesc *)heap1)->heaplimit;
1196 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1198 heapbase1 = (char *)heap1 + BLOCKSIZE;
1199 heapbase2 = (char *)heap2 + BLOCKSIZE;
1201 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
1202 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
1204 heapsize1 = heap1->heapsize;
1205 heapsize2 = heap2->heapsize;
1207 /* Start comparison */
1209 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1213 /* Check busy blocks*/
1217 while(i <= heaplimit){
1219 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
1220 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
1222 if(heapinfo1[i].type != heapinfo2[i].type){
1224 distance += BLOCKSIZE;
1225 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
1230 if(heapinfo1[i].type == -1){ /* Free block */
1235 if(heapinfo1[i].type == 0){ /* Large block */
1237 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
1238 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1239 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1240 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);
1244 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1245 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1246 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1247 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);
1253 //while(k < (heapinfo1[i].busy_block.busy_size)){
1254 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
1255 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
1263 }else { /* Fragmented block */
1265 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
1267 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
1268 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
1270 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
1275 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1276 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1277 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);
1283 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1284 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
1285 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){