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;
351 if(heapinfo1[i].type > 0){
352 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
353 heap_area_free(heapinfo1[i].busy_frag.equal_to[j]);
354 heapinfo1[i].busy_frag.equal_to[j] = NULL;
363 if(heapinfo2[i].type == 0){
364 heap_area_free(heapinfo2[i].busy_block.equal_to);
365 heapinfo2[i].busy_block.equal_to = NULL;
367 if(heapinfo2[i].type > 0){
368 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
369 heap_area_free(heapinfo2[i].busy_frag.equal_to[j]);
370 heapinfo2[i].busy_frag.equal_to[j] = NULL;
376 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
377 heapinfo1 = NULL, heapinfo2 = NULL;
378 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
379 to_ignore1 = NULL, to_ignore2 = NULL;
383 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
385 if(heap1 == NULL && heap1 == NULL){
386 XBT_DEBUG("Malloc descriptors null");
390 if(heap1->heaplimit != heap2->heaplimit){
391 XBT_DEBUG("Different limit of valid info table indices");
395 /* Start comparison */
396 size_t i1, i2, j1, j2, k;
397 size_t current_block = -1; /* avoid "maybe uninitialized" warning */
398 size_t current_fragment;
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){
414 if(heapinfo1[i1].type == -1){ /* Free block */
419 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
421 if(heapinfo1[i1].type == 0){ /* Large block */
423 if(is_stack(addr_block1)){
424 for(k=0; k < heapinfo1[i1].busy_block.size; k++)
425 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
426 for(k=0; k < heapinfo2[i1].busy_block.size; k++)
427 heapinfo2[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
428 i1 = i1 + heapinfo1[current_block].busy_block.size;
432 if(heapinfo1[i1].busy_block.equal_to != NULL){
441 /* Try first to associate to same block in the other heap */
442 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
444 if(heapinfo2[current_block].busy_block.equal_to == NULL){
446 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
448 res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0); /*FIXME*/
450 if(res_compare == 0){
451 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
452 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
453 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
454 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
456 i1 = i1 + heapinfo1[current_block].busy_block.size;
459 xbt_dynar_reset(previous);
465 while(i2 <= heaplimit && !equal){
467 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
469 if(i2 == current_block){
474 if(heapinfo2[i2].type != 0){
479 if(heapinfo2[i2].busy_block.equal_to != NULL){
484 res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0); /*FIXME */
486 if(res_compare == 0){
487 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
488 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
489 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
490 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
492 i1 = i1 + heapinfo1[i1].busy_block.size;
495 xbt_dynar_reset(previous);
502 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1[i1].busy_block.busy_size, addr_block1);
508 }else{ /* Fragmented block */
510 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
512 current_fragment = j1;
514 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
517 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
520 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
525 /* Try first to associate to same fragment in the other heap */
526 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
528 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
530 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
531 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << ((xbt_mheap_t)s_heap)->heapinfo[current_block].type));
533 res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0); /*FIXME*/
538 xbt_dynar_reset(previous);
544 while(i2 <= heaplimit && !equal){
547 if(heapinfo2[i2].type <= 0){
552 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
554 if(i2 == current_block && j2 == current_fragment)
557 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
560 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
561 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << ((xbt_mheap_t)s_heap)->heapinfo[i2].type));
563 res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0); /*FIXME*/
565 if(res_compare == 0){
567 xbt_dynar_reset(previous);
571 xbt_dynar_reset(previous);
579 if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
580 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]);
595 /* All blocks/fragments are equal to another block/fragment ? */
597 void *real_addr_frag1 = NULL, *real_addr_block1 = NULL, *real_addr_block2 = NULL, *real_addr_frag2 = NULL;
600 if(heapinfo1[i].type == 0){
601 if(current_block == heaplimit){
602 if(heapinfo1[i].busy_block.busy_size > 0){
603 if(heapinfo1[i].busy_block.equal_to == NULL){
604 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
605 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
606 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
607 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
614 if(heapinfo1[i].type > 0){
615 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
616 real_addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
617 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
618 if(current_block == heaplimit){
619 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
620 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
621 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
622 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
623 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
624 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]);
625 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
636 if(current_block == heaplimit)
637 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
642 if(heapinfo2[i].type == 0){
643 if(current_block == heaplimit){
644 if(heapinfo2[i].busy_block.busy_size > 0){
645 if(heapinfo2[i].busy_block.equal_to == NULL){
646 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
647 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
648 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
649 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
656 if(heapinfo2[i].type > 0){
657 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
658 real_addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
659 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
660 if(current_block == heaplimit){
661 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
662 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
663 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
664 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
665 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
666 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]);
667 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
678 if(current_block == heaplimit)
679 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
681 xbt_dynar_free(&previous);
682 real_addr_frag1 = NULL, real_addr_block1 = NULL, real_addr_block2 = NULL, real_addr_frag2 = NULL;
684 return ((nb_diff1 > 0) || (nb_diff2 > 0));
687 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){
690 void *addr_pointed1, *addr_pointed2;
691 int pointer_align, ignore1, ignore2, res_compare;
695 if(check_ignore > 0){
696 if((ignore1 = heap_comparison_ignore_size(to_ignore1, (char *)real_area1 + i)) > 0){
697 if((ignore2 = heap_comparison_ignore_size(to_ignore2, (char *)real_area2 + i)) == ignore1){
705 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
707 pointer_align = (i / sizeof(void*)) * sizeof(void*);
708 addr_pointed1 = *((void **)((char *)area1 + pointer_align));
709 addr_pointed2 = *((void **)((char *)area2 + pointer_align));
711 if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
712 i = pointer_align + sizeof(void *);
714 }else if((addr_pointed1 > s_heap) && ((char *)addr_pointed1 < (char *)s_heap + STD_HEAP_SIZE)
715 && (addr_pointed2 > s_heap) && ((char *)addr_pointed2 < (char *)s_heap + STD_HEAP_SIZE)){
716 res_compare = compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, NULL, 0);
717 if(res_compare != 0){
720 i = pointer_align + sizeof(void *);
737 static int compare_heap_area_with_type(void *real_area1, void *real_area2, void *area1, void *area2,
738 xbt_dynar_t previous, xbt_dict_t all_types, xbt_dict_t other_types, char *type_id,
739 int area_size, int check_ignore, int pointer_level){
741 if(is_stack(real_area1) && is_stack(real_area2))
744 size_t ignore1, ignore2;
746 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
749 dw_type_t type = xbt_dict_get_or_null(all_types, type_id);
750 dw_type_t subtype, subsubtype;
751 int res, elm_size, i, switch_types = 0;
752 unsigned int cursor = 0;
754 void *addr_pointed1, *addr_pointed2;;
759 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
761 if(strcmp(type->name, "char") == 0){ /* Chaine de caractères, donc taille aléatoire */
762 return (memcmp(area1, area2, area_size) != 0);
764 if(area_size != -1 && type->size != area_size)
767 return (memcmp(area1, area2, type->size) != 0);
770 case e_dw_enumeration_type:
771 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
773 if(area_size != -1 && type->size != area_size)
776 return (memcmp(area1, area2, type->size) != 0);
779 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);
781 case e_dw_const_type:
784 case e_dw_array_type:
785 subtype = xbt_dict_get_or_null(all_types, type->dw_type_id);
786 switch(subtype->type){
788 case e_dw_enumeration_type:
789 case e_dw_pointer_type:
790 case e_dw_structure_type:
791 case e_dw_union_type:
792 if(subtype->size == 0){ /*declaration of the type, need the complete description */
793 type_desc = get_type_description(all_types, subtype->name);
795 subtype = xbt_dict_get_or_null(all_types, type_desc);
797 subtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
801 elm_size = subtype->size;
804 case e_dw_volatile_type:
805 subsubtype = xbt_dict_get_or_null(all_types, subtype->dw_type_id);
806 if(subsubtype->size == 0){ /*declaration of the type, need the complete description */
807 type_desc = get_type_description(all_types, subsubtype->name);
809 subsubtype = xbt_dict_get_or_null(all_types, type_desc);
811 subsubtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
815 elm_size = subsubtype->size;
821 for(i=0; i<type->size; i++){
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, other_types, all_types, type->dw_type_id, type->size, check_ignore, pointer_level);
825 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);
830 case e_dw_pointer_type:
831 if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(all_types, type->dw_type_id))->type == e_dw_subroutine_type){
832 addr_pointed1 = *((void **)(area1));
833 addr_pointed2 = *((void **)(area2));
834 return (addr_pointed1 != addr_pointed2);;
837 if(pointer_level > 1){ /* Array of pointers */
838 for(i=0; i<(area_size/sizeof(void *)); i++){
839 addr_pointed1 = *((void **)((char *)area1 + (i*sizeof(void *))));
840 addr_pointed2 = *((void **)((char *)area2 + (i*sizeof(void *))));
841 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)
842 res = compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level);
844 res = (addr_pointed1 != addr_pointed2);
849 addr_pointed1 = *((void **)(area1));
850 addr_pointed2 = *((void **)(area2));
851 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)
852 return compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level);
854 return (addr_pointed1 != addr_pointed2);
858 case e_dw_structure_type:
859 if(type->size == 0){ /*declaration of the structure, need the complete description */
860 type_desc = get_type_description(all_types, type->name);
862 type = xbt_dict_get_or_null(all_types, type_desc);
864 type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
868 if(area_size != -1 && type->size != area_size){
869 if(area_size>type->size && area_size%type->size == 0){
870 for(i=0; i<(area_size/type->size); i++){
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, other_types, all_types, type_id, -1, check_ignore, 0);
874 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);
883 xbt_dynar_foreach(type->members, cursor, member){
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, other_types, all_types, member->dw_type_id, -1, check_ignore, 0);
887 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);
893 case e_dw_union_type:
894 if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2)) == ignore1))
896 return compare_heap_area_without_type(real_area1, real_area2, area1, area2, previous, all_types, other_types, type->size, check_ignore);
898 case e_dw_volatile_type:
899 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);
909 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){
912 ssize_t block1, frag1, block2, frag2;
914 int check_ignore = 0;
916 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
917 void *area1_to_compare, *area2_to_compare;
921 if(previous == NULL){
922 previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
926 block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
927 block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
929 if(is_block_stack((int)block1) && is_block_stack((int)block2)){
930 add_heap_area_pair(previous, block1, -1, block2, -1);
932 match_equals(previous);
933 xbt_dynar_free(&previous);
938 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)){
940 xbt_dynar_free(&previous);
945 addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
946 addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
948 if(heapinfo1[block1].type == heapinfo2[block2].type){
950 if(heapinfo1[block1].type == -1){ /* Free block */
952 match_equals(previous);
953 xbt_dynar_free(&previous);
957 }else if(heapinfo1[block1].type == 0){ /* Complete block */
959 if(heapinfo1[block1].busy_block.equal_to != NULL && heapinfo2[block2].busy_block.equal_to != NULL){
960 if(equal_blocks(block1, block2)){
962 match_equals(previous);
963 xbt_dynar_free(&previous);
969 if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size){
971 xbt_dynar_free(&previous);
976 if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size){
978 xbt_dynar_free(&previous);
983 if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
985 match_equals(previous);
986 xbt_dynar_free(&previous);
991 size = heapinfo1[block1].busy_block.busy_size;
995 match_equals(previous);
996 xbt_dynar_free(&previous);
1004 area1_to_compare = addr_block1;
1005 area2_to_compare = addr_block2;
1007 if((heapinfo1[block1].busy_block.ignore > 0) && (heapinfo2[block2].busy_block.ignore == heapinfo1[block1].busy_block.ignore))
1008 check_ignore = heapinfo1[block1].busy_block.ignore;
1010 }else{ /* Frgamented block */
1012 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1013 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1015 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1016 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1018 area1_to_compare = addr_frag1;
1019 area2_to_compare = addr_frag2;
1021 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL && heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
1022 if(equal_fragments(block1, frag1, block2, frag2)){
1024 match_equals(previous);
1025 xbt_dynar_free(&previous);
1031 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1033 xbt_dynar_free(&previous);
1038 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1040 match_equals(previous);
1041 xbt_dynar_free(&previous);
1046 size = heapinfo1[block1].busy_frag.frag_size[frag1];
1050 match_equals(previous);
1051 xbt_dynar_free(&previous);
1056 if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && ( heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1057 check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1061 }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){
1063 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1064 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1066 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL || heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
1067 if(equal_fragments(block1, frag1, block2, frag2)){
1069 match_equals(previous);
1070 xbt_dynar_free(&previous);
1076 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1078 xbt_dynar_free(&previous);
1083 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1085 match_equals(previous);
1086 xbt_dynar_free(&previous);
1091 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1092 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1094 area1_to_compare = addr_frag1;
1095 area2_to_compare = addr_frag2;
1097 size = heapinfo1[block1].busy_frag.frag_size[frag1];
1101 match_equals(previous);
1102 xbt_dynar_free(&previous);
1107 if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && (heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1108 check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1112 xbt_dynar_free(&previous);
1118 /* Start comparison*/
1119 if(type_id != NULL){
1120 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);
1121 if(res_compare != 0){
1123 xbt_dynar_free(&previous);
1127 res_compare = compare_heap_area_without_type(area1, area2, area1_to_compare, area2_to_compare, previous, all_types, other_types, size, check_ignore);
1128 if(res_compare != 0){
1130 xbt_dynar_free(&previous);
1136 match_equals(previous);
1137 xbt_dynar_free(&previous);
1143 /*********************************************** Miscellaneous ***************************************************/
1144 /****************************************************************************************************************/
1147 int get_pointed_area_size(void *area, int heap){
1150 malloc_info *heapinfo;
1153 heapinfo = heapinfo1;
1155 heapinfo = heapinfo2;
1157 block = ((char*)area - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
1159 if(((char *)area < (char*)((xbt_mheap_t)s_heap)->heapbase) || (block > heapsize1) || (block < 1))
1162 if(heapinfo[block].type == -1){ /* Free block */
1164 }else if(heapinfo[block].type == 0){ /* Complete block */
1165 return (int)heapinfo[block].busy_block.busy_size;
1167 frag = ((uintptr_t) (ADDR2UINT (area) % (BLOCKSIZE))) >> heapinfo[block].type;
1168 return (int)heapinfo[block].busy_frag.frag_size[frag];
1173 char *get_type_description(xbt_dict_t types, char *type_name){
1175 xbt_dict_cursor_t dict_cursor;
1179 xbt_dict_foreach(types, dict_cursor, type_origin, type){
1180 if(type->name && (strcmp(type->name, type_name) == 0) && type->size > 0){
1181 xbt_dict_cursor_free(&dict_cursor);
1186 xbt_dict_cursor_free(&dict_cursor);
1192 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1195 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
1197 if(heap1 == NULL && heap1 == NULL){
1198 XBT_DEBUG("Malloc descriptors null");
1202 if(heap1->heaplimit != heap2->heaplimit){
1203 XBT_DEBUG("Different limit of valid info table indices");
1207 /* Heap information */
1208 heaplimit = ((struct mdesc *)heap1)->heaplimit;
1210 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1212 heapbase1 = (char *)heap1 + BLOCKSIZE;
1213 heapbase2 = (char *)heap2 + BLOCKSIZE;
1215 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
1216 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
1218 heapsize1 = heap1->heapsize;
1219 heapsize2 = heap2->heapsize;
1221 /* Start comparison */
1223 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1227 /* Check busy blocks*/
1231 while(i <= heaplimit){
1233 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
1234 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
1236 if(heapinfo1[i].type != heapinfo2[i].type){
1238 distance += BLOCKSIZE;
1239 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
1244 if(heapinfo1[i].type == -1){ /* Free block */
1249 if(heapinfo1[i].type == 0){ /* Large block */
1251 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
1252 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1253 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1254 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);
1258 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1259 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1260 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1261 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);
1267 //while(k < (heapinfo1[i].busy_block.busy_size)){
1268 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
1269 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
1277 }else { /* Fragmented block */
1279 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
1281 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
1282 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
1284 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
1289 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1290 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1291 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);
1297 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1298 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
1299 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){