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 */
11 #include "xbt/mmalloc.h"
12 #include "mc/datatypes.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
15 "Logging specific to mm_diff in mmalloc");
17 xbt_dynar_t mc_heap_comparison_ignore;
18 xbt_dynar_t stacks_areas;
19 void *maestro_stack_start, *maestro_stack_end;
22 /********************************* Backtrace ***********************************/
23 /******************************************************************************/
25 static void mmalloc_backtrace_block_display(void* heapinfo, int block){
29 /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
30 /* fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
34 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
35 /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
37 /* xbt_ex_setup_backtrace(&e); */
38 /* if (e.used == 0) { */
39 /* fprintf(stderr, "(backtrace not set)\n"); */
40 /* } else if (e.bt_strings == NULL) { */
41 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
45 /* fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
46 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
47 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
52 static void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
56 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
57 /* e.used = XBT_BACKTRACE_SIZE; */
59 /* xbt_ex_setup_backtrace(&e); */
60 /* if (e.used == 0) { */
61 /* fprintf(stderr, "(backtrace not set)\n"); */
62 /* } else if (e.bt_strings == NULL) { */
63 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
67 /* fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
68 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
69 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
75 static void mmalloc_backtrace_display(void *addr){
77 /* size_t block, frag_nb; */
80 /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
82 /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
84 /* type = heap->heapinfo[block].type; */
87 /* case -1 : /\* Free block *\/ */
88 /* fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
91 /* case 0: /\* Large block *\/ */
92 /* mmalloc_backtrace_block_display(heap->heapinfo, block); */
94 /* default: /\* Fragmented block *\/ */
95 /* frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
96 /* if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
97 /* fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
100 /* mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
106 static int compare_backtrace(int b1, int f1, int b2, int f2){
109 for(i=0; i< XBT_BACKTRACE_SIZE; i++){
110 if(heapinfo1[b1].busy_frag.bt[f1][i] != heapinfo2[b2].busy_frag.bt[f2][i]){
111 //mmalloc_backtrace_fragment_display((void*)heapinfo1, b1, f1);
112 //mmalloc_backtrace_fragment_display((void*)heapinfo2, b2, f2);
117 for(i=0; i< heapinfo1[b1].busy_block.bt_size; i++){
118 if(heapinfo1[b1].busy_block.bt[i] != heapinfo2[b2].busy_block.bt[i]){
119 //mmalloc_backtrace_block_display((void*)heapinfo1, b1);
120 //mmalloc_backtrace_block_display((void*)heapinfo2, b2);
129 /*********************************** Heap comparison ***********************************/
130 /***************************************************************************************/
132 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
133 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
134 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
135 xbt_dynar_t to_ignore1 = NULL, to_ignore2 = NULL;
137 /*********************************** Free functions ************************************/
139 static void heap_area_pair_free(heap_area_pair_t pair){
144 static void heap_area_pair_free_voidp(void *d){
145 heap_area_pair_free((heap_area_pair_t) * (void **) d);
148 static void heap_area_free(heap_area_t area){
153 /************************************************************************************/
155 static heap_area_t new_heap_area(int block, int fragment){
156 heap_area_t area = NULL;
157 area = xbt_new0(s_heap_area_t, 1);
159 area->fragment = fragment;
164 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
166 unsigned int cursor = 0;
167 heap_area_pair_t current_pair;
169 xbt_dynar_foreach(list, cursor, current_pair){
170 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
177 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
179 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
180 heap_area_pair_t pair = NULL;
181 pair = xbt_new0(s_heap_area_pair_t, 1);
182 pair->block1 = block1;
183 pair->fragment1 = fragment1;
184 pair->block2 = block2;
185 pair->fragment2 = fragment2;
187 xbt_dynar_push(list, &pair);
195 static size_t heap_comparison_ignore_size(xbt_dynar_t ignore_list, void *address){
197 unsigned int cursor = 0;
199 int end = xbt_dynar_length(ignore_list) - 1;
200 mc_heap_ignore_region_t region;
203 cursor = (start + end) / 2;
204 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(ignore_list, cursor, mc_heap_ignore_region_t);
205 if(region->address == address)
207 if(region->address < address)
209 if(region->address > address)
216 static int is_stack(void *address){
217 unsigned int cursor = 0;
218 stack_region_t stack;
220 xbt_dynar_foreach(stacks_areas, cursor, stack){
221 if(address == stack->address)
228 static int is_block_stack(int block){
229 unsigned int cursor = 0;
230 stack_region_t stack;
232 xbt_dynar_foreach(stacks_areas, cursor, stack){
233 if(block == stack->block)
240 static void match_equals(xbt_dynar_t list){
242 unsigned int cursor = 0;
243 heap_area_pair_t current_pair;
244 heap_area_t previous_area;
246 xbt_dynar_foreach(list, cursor, current_pair){
248 if(current_pair->fragment1 != -1){
250 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
251 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
252 heap_area_free(heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
253 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
254 heap_area_free(previous_area);
256 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
257 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
258 heap_area_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
259 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
260 heap_area_free(previous_area);
263 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
264 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
268 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
269 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
270 heap_area_free(heapinfo2[previous_area->block].busy_block.equal_to);
271 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
272 heap_area_free(previous_area);
274 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
275 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
276 heap_area_free(heapinfo1[previous_area->block].busy_block.equal_to);
277 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
278 heap_area_free(previous_area);
281 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
282 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
289 static int equal_blocks(int b1, int b2){
290 if(heapinfo1[b1].busy_block.equal_to != NULL){
291 if(heapinfo2[b2].busy_block.equal_to != NULL){
292 if(((heap_area_t)(heapinfo1[b1].busy_block.equal_to))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_block.equal_to))->block == b1)
299 static int equal_fragments(int b1, int f1, int b2, int f2){
300 if(heapinfo1[b1].busy_frag.equal_to[f1] != NULL){
301 if(heapinfo2[b2].busy_frag.equal_to[f2] != NULL){
302 if(((heap_area_t)(heapinfo1[b1].busy_frag.equal_to[f1]))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_frag.equal_to[f2]))->block == b1 && ((heap_area_t)(heapinfo1[b1].busy_frag.equal_to[f1]))->fragment == f2 && ((heap_area_t)(heapinfo2[b2].busy_frag.equal_to[f2]))->fragment == f1)
309 void init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t i1, xbt_dynar_t i2){
311 heaplimit = ((struct mdesc *)heap1)->heaplimit;
313 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
315 heapbase1 = (char *)heap1 + BLOCKSIZE;
316 heapbase2 = (char *)heap2 + BLOCKSIZE;
318 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)((struct mdesc *)heap1)->heapinfo - (char *)s_heap)));
319 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)((struct mdesc *)heap2)->heapinfo - (char *)s_heap)));
321 heapsize1 = heap1->heapsize;
322 heapsize2 = heap2->heapsize;
328 MC_ignore_global_variable("heaplimit");
329 MC_ignore_global_variable("s_heap");
330 MC_ignore_global_variable("heapbase1");
331 MC_ignore_global_variable("heapbase2");
332 MC_ignore_global_variable("heapinfo1");
333 MC_ignore_global_variable("heapinfo2");
334 MC_ignore_global_variable("heapsize1");
335 MC_ignore_global_variable("heapsize2");
336 MC_ignore_global_variable("to_ignore1");
337 MC_ignore_global_variable("to_ignore2");
342 void reset_heap_information(){
347 if(heapinfo1[i].type == 0){
348 heap_area_free(heapinfo1[i].busy_block.equal_to);
349 heapinfo1[i].busy_block.equal_to = NULL;
350 }else if(heapinfo1[i].type > 0){
351 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
352 heap_area_free(heapinfo1[i].busy_frag.equal_to[j]);
353 heapinfo1[i].busy_frag.equal_to[j] = NULL;
362 if(heapinfo2[i].type == 0){
363 heap_area_free(heapinfo2[i].busy_block.equal_to);
364 heapinfo2[i].busy_block.equal_to = NULL;
365 }else if(heapinfo2[i].type > 0){
366 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
367 heap_area_free(heapinfo2[i].busy_frag.equal_to[j]);
368 heapinfo2[i].busy_frag.equal_to[j] = NULL;
374 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
375 heapinfo1 = NULL, heapinfo2 = NULL;
376 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
377 to_ignore1 = NULL, to_ignore2 = NULL;
381 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
383 if(heap1 == NULL && heap1 == NULL){
384 XBT_DEBUG("Malloc descriptors null");
388 if(heap1->heaplimit != heap2->heaplimit){
389 XBT_DEBUG("Different limit of valid info table indices");
393 /* Start comparison */
394 size_t i1, i2, j1, j2, k;
395 size_t current_block = -1; /* avoid "maybe uninitialized" warning */
396 size_t current_fragment;
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){
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 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
424 for(k=0; k < heapinfo2[i1].busy_block.size; k++)
425 heapinfo2[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
426 i1 = i1 + heapinfo1[current_block].busy_block.size;
430 if(heapinfo1[i1].busy_block.equal_to != NULL){
439 /* Try first to associate to same block in the other heap */
440 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
442 if(heapinfo2[current_block].busy_block.equal_to == NULL){
444 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
446 res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0); /*FIXME*/
448 if(res_compare == 0){
449 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
450 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
451 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
452 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
454 i1 = i1 + heapinfo1[current_block].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));
467 if(i2 == current_block){
472 if(heapinfo2[i2].type != 0){
477 if(heapinfo2[i2].busy_block.equal_to != NULL){
482 res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0); /*FIXME */
484 if(res_compare == 0){
485 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
486 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
487 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
488 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
490 i1 = 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 current_fragment = j1;
512 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
515 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
518 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
523 /* Try first to associate to same fragment in the other heap */
524 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
526 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
528 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
529 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << ((xbt_mheap_t)s_heap)->heapinfo[current_block].type));
531 res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0); /*FIXME*/
536 xbt_dynar_reset(previous);
542 while(i2 <= heaplimit && !equal){
545 if(heapinfo2[i2].type <= 0){
550 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
552 if(i2 == current_block && j2 == current_fragment)
555 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
558 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
559 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << ((xbt_mheap_t)s_heap)->heapinfo[i2].type));
561 res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0); /*FIXME*/
563 if(res_compare == 0){
565 xbt_dynar_reset(previous);
569 xbt_dynar_reset(previous);
577 if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
578 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %zd, address = %p, ignore %d)\n", i1, j1, heapinfo1[i1].busy_frag.frag_size[j1], addr_frag1, heapinfo1[i1].busy_frag.ignore[j1]);
593 /* All blocks/fragments are equal to another block/fragment ? */
595 void *real_addr_frag1 = NULL, *real_addr_block1 = NULL, *real_addr_block2 = NULL, *real_addr_frag2 = NULL;
598 if(heapinfo1[i].type == 0){
599 if(current_block == heaplimit){
600 if(heapinfo1[i].busy_block.busy_size > 0){
601 if(heapinfo1[i].busy_block.equal_to == NULL){
602 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
603 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
604 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
605 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
612 if(heapinfo1[i].type > 0){
613 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
614 real_addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
615 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
616 if(current_block == heaplimit){
617 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
618 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
619 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
620 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
621 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
622 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]);
623 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
634 if(current_block == heaplimit)
635 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
640 if(heapinfo2[i].type == 0){
641 if(current_block == heaplimit){
642 if(heapinfo2[i].busy_block.busy_size > 0){
643 if(heapinfo2[i].busy_block.equal_to == NULL){
644 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
645 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
646 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
647 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
654 if(heapinfo2[i].type > 0){
655 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
656 real_addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
657 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
658 if(current_block == heaplimit){
659 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
660 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
661 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
662 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
663 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
664 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]);
665 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
676 if(current_block == heaplimit)
677 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
679 xbt_dynar_free(&previous);
680 real_addr_frag1 = NULL, real_addr_block1 = NULL, real_addr_block2 = NULL, real_addr_frag2 = NULL;
682 return ((nb_diff1 > 0) || (nb_diff2 > 0));
685 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){
688 void *addr_pointed1, *addr_pointed2;
689 int pointer_align, ignore1, ignore2, res_compare;
693 if(check_ignore > 0){
694 if((ignore1 = heap_comparison_ignore_size(to_ignore1, (char *)real_area1 + i)) > 0){
695 if((ignore2 = heap_comparison_ignore_size(to_ignore2, (char *)real_area2 + i)) == ignore1){
703 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
705 pointer_align = (i / sizeof(void*)) * sizeof(void*);
706 addr_pointed1 = *((void **)((char *)area1 + pointer_align));
707 addr_pointed2 = *((void **)((char *)area2 + pointer_align));
709 if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
710 i = pointer_align + sizeof(void *);
712 }else if((addr_pointed1 > s_heap) && ((char *)addr_pointed1 < (char *)s_heap + STD_HEAP_SIZE)
713 && (addr_pointed2 > s_heap) && ((char *)addr_pointed2 < (char *)s_heap + STD_HEAP_SIZE)){
714 res_compare = compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, NULL, 0);
715 if(res_compare != 0){
718 i = pointer_align + sizeof(void *);
735 static int compare_heap_area_with_type(void *real_area1, void *real_area2, void *area1, void *area2,
736 xbt_dynar_t previous, xbt_dict_t all_types, xbt_dict_t other_types, char *type_id,
737 int area_size, int check_ignore, int pointer_level){
739 if(is_stack(real_area1) && is_stack(real_area2))
742 size_t ignore1, ignore2;
744 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
747 dw_type_t type = xbt_dict_get_or_null(all_types, type_id);
748 dw_type_t subtype, subsubtype;
749 int res, elm_size, i, switch_types = 0;
750 unsigned int cursor = 0;
752 void *addr_pointed1, *addr_pointed2;;
757 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
759 if(strcmp(type->name, "char") == 0){ /* String, hence random (arbitrary ?) size */
760 return (memcmp(area1, area2, area_size) != 0);
762 if(area_size != -1 && type->size != area_size)
765 return (memcmp(area1, area2, type->size) != 0);
768 case e_dw_enumeration_type:
769 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
771 if(area_size != -1 && type->size != area_size)
774 return (memcmp(area1, area2, type->size) != 0);
777 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);
779 case e_dw_const_type:
782 case e_dw_array_type:
783 subtype = xbt_dict_get_or_null(all_types, type->dw_type_id);
784 switch(subtype->type){
786 case e_dw_enumeration_type:
787 case e_dw_pointer_type:
788 case e_dw_structure_type:
789 case e_dw_union_type:
790 if(subtype->size == 0){ /*declaration of the type, need the complete description */
791 type_desc = get_type_description(all_types, subtype->name);
793 subtype = xbt_dict_get_or_null(all_types, type_desc);
795 subtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
799 elm_size = subtype->size;
802 case e_dw_volatile_type:
803 subsubtype = xbt_dict_get_or_null(all_types, subtype->dw_type_id);
804 if(subsubtype->size == 0){ /*declaration of the type, need the complete description */
805 type_desc = get_type_description(all_types, subsubtype->name);
807 subsubtype = xbt_dict_get_or_null(all_types, type_desc);
809 subsubtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
813 elm_size = subsubtype->size;
819 for(i=0; i<type->size; i++){
821 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);
823 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);
828 case e_dw_pointer_type:
829 if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(all_types, type->dw_type_id))->type == e_dw_subroutine_type){
830 addr_pointed1 = *((void **)(area1));
831 addr_pointed2 = *((void **)(area2));
832 return (addr_pointed1 != addr_pointed2);;
835 if(pointer_level > 1){ /* Array of pointers */
836 for(i=0; i<(area_size/sizeof(void *)); i++){
837 addr_pointed1 = *((void **)((char *)area1 + (i*sizeof(void *))));
838 addr_pointed2 = *((void **)((char *)area2 + (i*sizeof(void *))));
839 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)
840 res = compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level);
842 res = (addr_pointed1 != addr_pointed2);
847 addr_pointed1 = *((void **)(area1));
848 addr_pointed2 = *((void **)(area2));
849 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)
850 return compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level);
852 return (addr_pointed1 != addr_pointed2);
856 case e_dw_structure_type:
857 if(type->size == 0){ /*declaration of the structure, need the complete description */
858 type_desc = get_type_description(all_types, type->name);
860 type = xbt_dict_get_or_null(all_types, type_desc);
862 type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
866 if(area_size != -1 && type->size != area_size){
867 if(area_size>type->size && area_size%type->size == 0){
868 for(i=0; i<(area_size/type->size); i++){
870 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);
872 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);
881 xbt_dynar_foreach(type->members, cursor, member){
883 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);
885 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);
891 case e_dw_union_type:
892 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;
919 if(previous == NULL){
920 previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
924 block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
925 block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
927 if(is_block_stack((int)block1) && is_block_stack((int)block2)){
928 add_heap_area_pair(previous, block1, -1, block2, -1);
930 match_equals(previous);
931 xbt_dynar_free(&previous);
936 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)){
938 xbt_dynar_free(&previous);
943 addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
944 addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
946 if(heapinfo1[block1].type == heapinfo2[block2].type){
948 if(heapinfo1[block1].type == -1){ /* Free block */
950 match_equals(previous);
951 xbt_dynar_free(&previous);
955 }else if(heapinfo1[block1].type == 0){ /* Complete block */
957 if(heapinfo1[block1].busy_block.equal_to != NULL && heapinfo2[block2].busy_block.equal_to != NULL){
958 if(equal_blocks(block1, block2)){
960 match_equals(previous);
961 xbt_dynar_free(&previous);
967 if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size){
969 xbt_dynar_free(&previous);
974 if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size){
976 xbt_dynar_free(&previous);
981 if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
983 match_equals(previous);
984 xbt_dynar_free(&previous);
989 size = heapinfo1[block1].busy_block.busy_size;
993 match_equals(previous);
994 xbt_dynar_free(&previous);
1002 area1_to_compare = addr_block1;
1003 area2_to_compare = addr_block2;
1005 if((heapinfo1[block1].busy_block.ignore > 0) && (heapinfo2[block2].busy_block.ignore == heapinfo1[block1].busy_block.ignore))
1006 check_ignore = heapinfo1[block1].busy_block.ignore;
1008 }else{ /* Frgamented block */
1010 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1011 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1013 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1014 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1016 area1_to_compare = addr_frag1;
1017 area2_to_compare = addr_frag2;
1019 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL && heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
1020 if(equal_fragments(block1, frag1, block2, frag2)){
1022 match_equals(previous);
1023 xbt_dynar_free(&previous);
1029 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1031 xbt_dynar_free(&previous);
1036 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1038 match_equals(previous);
1039 xbt_dynar_free(&previous);
1044 size = heapinfo1[block1].busy_frag.frag_size[frag1];
1048 match_equals(previous);
1049 xbt_dynar_free(&previous);
1054 if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && ( heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1055 check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1059 }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){
1061 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1062 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1064 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL || heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
1065 if(equal_fragments(block1, frag1, block2, frag2)){
1067 match_equals(previous);
1068 xbt_dynar_free(&previous);
1074 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1076 xbt_dynar_free(&previous);
1081 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1083 match_equals(previous);
1084 xbt_dynar_free(&previous);
1089 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1090 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1092 area1_to_compare = addr_frag1;
1093 area2_to_compare = addr_frag2;
1095 size = heapinfo1[block1].busy_frag.frag_size[frag1];
1099 match_equals(previous);
1100 xbt_dynar_free(&previous);
1105 if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && (heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1106 check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1110 xbt_dynar_free(&previous);
1116 /* Start comparison*/
1117 if(type_id != NULL){
1118 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);
1119 if(res_compare != 0){
1121 xbt_dynar_free(&previous);
1125 res_compare = compare_heap_area_without_type(area1, area2, area1_to_compare, area2_to_compare, previous, all_types, other_types, size, check_ignore);
1126 if(res_compare != 0){
1128 xbt_dynar_free(&previous);
1134 match_equals(previous);
1135 xbt_dynar_free(&previous);
1141 /*********************************************** Miscellaneous ***************************************************/
1142 /****************************************************************************************************************/
1145 int get_pointed_area_size(void *area, int heap){
1148 malloc_info *heapinfo;
1151 heapinfo = heapinfo1;
1153 heapinfo = heapinfo2;
1155 block = ((char*)area - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
1157 if(((char *)area < (char*)((xbt_mheap_t)s_heap)->heapbase) || (block > heapsize1) || (block < 1))
1160 if(heapinfo[block].type == -1){ /* Free block */
1162 }else if(heapinfo[block].type == 0){ /* Complete block */
1163 return (int)heapinfo[block].busy_block.busy_size;
1165 frag = ((uintptr_t) (ADDR2UINT (area) % (BLOCKSIZE))) >> heapinfo[block].type;
1166 return (int)heapinfo[block].busy_frag.frag_size[frag];
1171 char *get_type_description(xbt_dict_t types, char *type_name){
1173 xbt_dict_cursor_t dict_cursor;
1177 xbt_dict_foreach(types, dict_cursor, type_origin, type){
1178 if(type->name && (strcmp(type->name, type_name) == 0) && type->size > 0){
1179 xbt_dict_cursor_free(&dict_cursor);
1184 xbt_dict_cursor_free(&dict_cursor);
1190 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1193 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
1195 if(heap1 == NULL && heap1 == NULL){
1196 XBT_DEBUG("Malloc descriptors null");
1200 if(heap1->heaplimit != heap2->heaplimit){
1201 XBT_DEBUG("Different limit of valid info table indices");
1205 /* Heap information */
1206 heaplimit = ((struct mdesc *)heap1)->heaplimit;
1208 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1210 heapbase1 = (char *)heap1 + BLOCKSIZE;
1211 heapbase2 = (char *)heap2 + BLOCKSIZE;
1213 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
1214 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
1216 heapsize1 = heap1->heapsize;
1217 heapsize2 = heap2->heapsize;
1219 /* Start comparison */
1221 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1225 /* Check busy blocks*/
1229 while(i <= heaplimit){
1231 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
1232 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
1234 if(heapinfo1[i].type != heapinfo2[i].type){
1236 distance += BLOCKSIZE;
1237 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
1242 if(heapinfo1[i].type == -1){ /* Free block */
1247 if(heapinfo1[i].type == 0){ /* Large block */
1249 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
1250 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1251 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1252 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);
1256 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1257 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1258 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1259 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);
1265 //while(k < (heapinfo1[i].busy_block.busy_size)){
1266 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
1267 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
1275 }else { /* Fragmented block */
1277 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
1279 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
1280 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
1282 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
1287 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1288 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1289 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);
1295 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1296 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
1297 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){