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 /************************************************************************************/
150 static heap_area_t new_heap_area(int block, int fragment){
151 heap_area_t area = NULL;
152 area = xbt_new0(s_heap_area_t, 1);
154 area->fragment = fragment;
159 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
161 unsigned int cursor = 0;
162 heap_area_pair_t current_pair;
164 xbt_dynar_foreach(list, cursor, current_pair){
165 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
172 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
174 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
175 heap_area_pair_t pair = NULL;
176 pair = xbt_new0(s_heap_area_pair_t, 1);
177 pair->block1 = block1;
178 pair->fragment1 = fragment1;
179 pair->block2 = block2;
180 pair->fragment2 = fragment2;
182 xbt_dynar_push(list, &pair);
190 static size_t heap_comparison_ignore_size(xbt_dynar_t ignore_list, void *address){
192 unsigned int cursor = 0;
194 int end = xbt_dynar_length(ignore_list) - 1;
195 mc_heap_ignore_region_t region;
198 cursor = (start + end) / 2;
199 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(ignore_list, cursor, mc_heap_ignore_region_t);
200 if(region->address == address)
202 if(region->address < address)
204 if(region->address > address)
211 static int is_stack(void *address){
212 unsigned int cursor = 0;
213 stack_region_t stack;
215 xbt_dynar_foreach(stacks_areas, cursor, stack){
216 if(address == stack->address)
223 static int is_block_stack(int block){
224 unsigned int cursor = 0;
225 stack_region_t stack;
227 xbt_dynar_foreach(stacks_areas, cursor, stack){
228 if(block == stack->block)
235 static void match_equals(xbt_dynar_t list){
237 unsigned int cursor = 0;
238 heap_area_pair_t current_pair;
239 heap_area_t previous_area;
241 xbt_dynar_foreach(list, cursor, current_pair){
243 if(current_pair->fragment1 != -1){
245 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
246 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
247 xbt_free(heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
248 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
249 xbt_free(previous_area);
251 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
252 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
253 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
254 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
255 xbt_free(previous_area);
258 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
259 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
263 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
264 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
265 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
266 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
267 xbt_free(previous_area);
269 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
270 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
271 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
272 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
273 xbt_free(previous_area);
276 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
277 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
284 static int equal_blocks(int b1, int b2){
285 if(heapinfo1[b1].busy_block.equal_to != NULL){
286 if(heapinfo2[b2].busy_block.equal_to != NULL){
287 if(((heap_area_t)(heapinfo1[b1].busy_block.equal_to))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_block.equal_to))->block == b1)
294 static int equal_fragments(int b1, int f1, int b2, int f2){
295 if(heapinfo1[b1].busy_frag.equal_to[f1] != NULL){
296 if(heapinfo2[b2].busy_frag.equal_to[f2] != NULL){
297 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)
304 void init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t i1, xbt_dynar_t i2){
306 heaplimit = ((struct mdesc *)heap1)->heaplimit;
308 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
310 heapbase1 = (char *)heap1 + BLOCKSIZE;
311 heapbase2 = (char *)heap2 + BLOCKSIZE;
313 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)((struct mdesc *)heap1)->heapinfo - (char *)s_heap)));
314 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)((struct mdesc *)heap2)->heapinfo - (char *)s_heap)));
316 heapsize1 = heap1->heapsize;
317 heapsize2 = heap2->heapsize;
323 MC_ignore_global_variable("heaplimit", 1);
324 MC_ignore_global_variable("s_heap", 1);
325 MC_ignore_global_variable("heapbase1", 1);
326 MC_ignore_global_variable("heapbase2", 1);
327 MC_ignore_global_variable("heapinfo1", 1);
328 MC_ignore_global_variable("heapinfo2", 1);
329 MC_ignore_global_variable("heapsize1", 1);
330 MC_ignore_global_variable("heapsize2", 1);
331 MC_ignore_global_variable("to_ignore1", 1);
332 MC_ignore_global_variable("to_ignore2", 1);
337 void reset_heap_information(){
342 if(heapinfo1[i].type == 0){
343 xbt_free(heapinfo1[i].busy_block.equal_to);
344 heapinfo1[i].busy_block.equal_to = NULL;
346 if(heapinfo1[i].type > 0){
347 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
348 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
349 heapinfo1[i].busy_frag.equal_to[j] = NULL;
358 if(heapinfo2[i].type == 0){
359 xbt_free(heapinfo2[i].busy_block.equal_to);
360 heapinfo2[i].busy_block.equal_to = NULL;
362 if(heapinfo2[i].type > 0){
363 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
364 xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
365 heapinfo2[i].busy_frag.equal_to[j] = NULL;
371 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
372 heapinfo1 = NULL, heapinfo2 = NULL;
373 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
374 to_ignore1 = NULL, to_ignore2 = NULL;
378 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
380 if(heap1 == NULL && heap1 == NULL){
381 XBT_DEBUG("Malloc descriptors null");
385 if(heap1->heaplimit != heap2->heaplimit){
386 XBT_DEBUG("Different limit of valid info table indices");
390 /* Start comparison */
391 size_t i1, i2, j1, j2, k;
392 size_t current_block = -1; /* avoid "maybe uninitialized" warning */
393 size_t current_fragment;
394 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
395 int nb_diff1 = 0, nb_diff2 = 0;
397 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
399 int equal, res_compare = 0;
401 /* Check busy blocks*/
405 while(i1 <= heaplimit){
409 if(heapinfo1[i1].type == -1){ /* Free block */
414 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
416 if(heapinfo1[i1].type == 0){ /* Large block */
418 if(is_stack(addr_block1)){
419 for(k=0; k < heapinfo1[i1].busy_block.size; k++)
420 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
421 for(k=0; k < heapinfo2[i1].busy_block.size; k++)
422 heapinfo2[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
423 i1 = i1 + heapinfo1[current_block].busy_block.size;
427 if(heapinfo1[i1].busy_block.equal_to != NULL){
436 /* Try first to associate to same block in the other heap */
437 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
439 if(heapinfo2[current_block].busy_block.equal_to == NULL){
441 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
443 res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0); /*FIXME*/
445 if(res_compare == 0){
446 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
447 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
448 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
449 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
451 i1 = i1 + heapinfo1[current_block].busy_block.size;
454 xbt_dynar_reset(previous);
460 while(i2 <= heaplimit && !equal){
462 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
464 if(i2 == current_block){
469 if(heapinfo2[i2].type != 0){
474 if(heapinfo2[i2].busy_block.equal_to != NULL){
479 res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0); /*FIXME */
481 if(res_compare == 0){
482 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
483 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
484 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
485 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
487 i1 = i1 + heapinfo1[i1].busy_block.size;
490 xbt_dynar_reset(previous);
497 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1[i1].busy_block.busy_size, addr_block1);
503 }else{ /* Fragmented block */
505 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
507 current_fragment = j1;
509 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
512 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
515 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
520 /* Try first to associate to same fragment in the other heap */
521 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
523 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
525 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
526 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << ((xbt_mheap_t)s_heap)->heapinfo[current_block].type));
528 res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0); /*FIXME*/
533 xbt_dynar_reset(previous);
539 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 == current_block && j2 == current_fragment)
552 if(heapinfo2[i2].busy_frag.equal_to[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); /*FIXME*/
560 if(res_compare == 0){
562 xbt_dynar_reset(previous);
566 xbt_dynar_reset(previous);
574 if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
575 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]);
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){
596 if(current_block == heaplimit){
597 if(heapinfo1[i].busy_block.busy_size > 0){
598 if(heapinfo1[i].busy_block.equal_to == 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++){
613 if(current_block == heaplimit){
614 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
615 if(heapinfo1[i].busy_frag.equal_to[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);
631 if(current_block == heaplimit)
632 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
637 if(heapinfo2[i].type == 0){
638 if(current_block == heaplimit){
639 if(heapinfo2[i].busy_block.busy_size > 0){
640 if(heapinfo2[i].busy_block.equal_to == 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++){
655 if(current_block == heaplimit){
656 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
657 if(heapinfo2[i].busy_frag.equal_to[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);
673 if(current_block == heaplimit)
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);
712 if(res_compare != 0){
715 i = pointer_align + sizeof(void *);
732 static int compare_heap_area_with_type(void *real_area1, void *real_area2, void *area1, void *area2,
733 xbt_dynar_t previous, xbt_dict_t all_types, xbt_dict_t other_types, char *type_id,
734 int area_size, int check_ignore, int pointer_level){
736 if(is_stack(real_area1) && is_stack(real_area2))
739 size_t ignore1, ignore2;
741 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
744 dw_type_t type = xbt_dict_get_or_null(all_types, type_id);
745 dw_type_t subtype, subsubtype;
746 int res, elm_size, i, switch_types = 0;
747 unsigned int cursor = 0;
749 void *addr_pointed1, *addr_pointed2;;
754 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
756 if(strcmp(type->name, "char") == 0){ /* Chaine de caractères, donc taille aléatoire */
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))
891 return compare_heap_area_without_type(real_area1, real_area2, area1, area2, previous, all_types, other_types, type->size, check_ignore);
893 case e_dw_volatile_type:
894 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);
904 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){
907 ssize_t block1, frag1, block2, frag2;
909 int check_ignore = 0;
911 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
912 void *area1_to_compare, *area2_to_compare;
916 if(previous == NULL){
917 previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
921 block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
922 block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
924 if(is_block_stack((int)block1) && is_block_stack((int)block2)){
925 add_heap_area_pair(previous, block1, -1, block2, -1);
927 match_equals(previous);
928 xbt_dynar_free(&previous);
933 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)){
935 xbt_dynar_free(&previous);
940 addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
941 addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
943 if(heapinfo1[block1].type == heapinfo2[block2].type){
945 if(heapinfo1[block1].type == -1){ /* Free block */
947 match_equals(previous);
948 xbt_dynar_free(&previous);
952 }else if(heapinfo1[block1].type == 0){ /* Complete block */
954 if(heapinfo1[block1].busy_block.equal_to != NULL && heapinfo2[block2].busy_block.equal_to != NULL){
955 if(equal_blocks(block1, block2)){
957 match_equals(previous);
958 xbt_dynar_free(&previous);
964 if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size){
966 xbt_dynar_free(&previous);
971 if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size){
973 xbt_dynar_free(&previous);
978 if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
980 match_equals(previous);
981 xbt_dynar_free(&previous);
986 size = heapinfo1[block1].busy_block.busy_size;
990 match_equals(previous);
991 xbt_dynar_free(&previous);
999 area1_to_compare = addr_block1;
1000 area2_to_compare = addr_block2;
1002 if((heapinfo1[block1].busy_block.ignore > 0) && (heapinfo2[block2].busy_block.ignore == heapinfo1[block1].busy_block.ignore))
1003 check_ignore = heapinfo1[block1].busy_block.ignore;
1005 }else{ /* Frgamented block */
1007 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1008 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1010 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1011 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1013 area1_to_compare = addr_frag1;
1014 area2_to_compare = addr_frag2;
1016 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL && heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
1017 if(equal_fragments(block1, frag1, block2, frag2)){
1019 match_equals(previous);
1020 xbt_dynar_free(&previous);
1026 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1028 xbt_dynar_free(&previous);
1033 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1035 match_equals(previous);
1036 xbt_dynar_free(&previous);
1041 size = heapinfo1[block1].busy_frag.frag_size[frag1];
1045 match_equals(previous);
1046 xbt_dynar_free(&previous);
1051 if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && ( heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1052 check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1056 }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){
1058 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1059 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1061 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL || heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
1062 if(equal_fragments(block1, frag1, block2, frag2)){
1064 match_equals(previous);
1065 xbt_dynar_free(&previous);
1071 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1073 xbt_dynar_free(&previous);
1078 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1080 match_equals(previous);
1081 xbt_dynar_free(&previous);
1086 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1087 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1089 area1_to_compare = addr_frag1;
1090 area2_to_compare = addr_frag2;
1092 size = heapinfo1[block1].busy_frag.frag_size[frag1];
1096 match_equals(previous);
1097 xbt_dynar_free(&previous);
1102 if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && (heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1103 check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1107 xbt_dynar_free(&previous);
1113 /* Start comparison*/
1114 if(type_id != NULL){
1115 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);
1116 if(res_compare != 0){
1118 xbt_dynar_free(&previous);
1122 res_compare = compare_heap_area_without_type(area1, area2, area1_to_compare, area2_to_compare, previous, all_types, other_types, size, check_ignore);
1123 if(res_compare != 0){
1125 xbt_dynar_free(&previous);
1131 match_equals(previous);
1132 xbt_dynar_free(&previous);
1138 /*********************************************** Miscellaneous ***************************************************/
1139 /****************************************************************************************************************/
1142 int get_pointed_area_size(void *area, int heap){
1145 malloc_info *heapinfo;
1148 heapinfo = heapinfo1;
1150 heapinfo = heapinfo2;
1152 block = ((char*)area - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
1154 if(((char *)area < (char*)((xbt_mheap_t)s_heap)->heapbase) || (block > heapsize1) || (block < 1))
1157 if(heapinfo[block].type == -1){ /* Free block */
1159 }else if(heapinfo[block].type == 0){ /* Complete block */
1160 return (int)heapinfo[block].busy_block.busy_size;
1162 frag = ((uintptr_t) (ADDR2UINT (area) % (BLOCKSIZE))) >> heapinfo[block].type;
1163 return (int)heapinfo[block].busy_frag.frag_size[frag];
1168 char *get_type_description(xbt_dict_t types, char *type_name){
1170 xbt_dict_cursor_t dict_cursor;
1174 xbt_dict_foreach(types, dict_cursor, type_origin, type){
1175 if(type->name && (strcmp(type->name, type_name) == 0) && type->size > 0){
1176 xbt_dict_cursor_free(&dict_cursor);
1181 xbt_dict_cursor_free(&dict_cursor);
1187 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1190 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
1192 if(heap1 == NULL && heap1 == NULL){
1193 XBT_DEBUG("Malloc descriptors null");
1197 if(heap1->heaplimit != heap2->heaplimit){
1198 XBT_DEBUG("Different limit of valid info table indices");
1202 /* Heap information */
1203 heaplimit = ((struct mdesc *)heap1)->heaplimit;
1205 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1207 heapbase1 = (char *)heap1 + BLOCKSIZE;
1208 heapbase2 = (char *)heap2 + BLOCKSIZE;
1210 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
1211 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
1213 heapsize1 = heap1->heapsize;
1214 heapsize2 = heap2->heapsize;
1216 /* Start comparison */
1218 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1222 /* Check busy blocks*/
1226 while(i <= heaplimit){
1228 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
1229 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
1231 if(heapinfo1[i].type != heapinfo2[i].type){
1233 distance += BLOCKSIZE;
1234 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
1239 if(heapinfo1[i].type == -1){ /* Free block */
1244 if(heapinfo1[i].type == 0){ /* Large block */
1246 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
1247 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1248 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1249 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);
1253 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1254 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1255 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1256 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);
1262 //while(k < (heapinfo1[i].busy_block.busy_size)){
1263 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
1264 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
1272 }else { /* Fragmented block */
1274 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
1276 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
1277 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
1279 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
1284 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1285 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1286 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);
1292 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1293 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
1294 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){