Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e19bd9597000def5e18c08bc7669dced45d3032d
[simgrid.git] / src / xbt / mmalloc / mm_diff.c
1 /* mm_diff - Memory snapshooting and comparison                             */
2
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
9 #include "xbt/str.h"
10 #include "mc/mc.h"
11 #include "xbt/mmalloc.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
14                                 "Logging specific to mm_diff in mmalloc");
15
16 xbt_dynar_t mc_heap_comparison_ignore;
17 xbt_dynar_t stacks_areas;
18
19 static void heap_area_pair_free(heap_area_pair_t pair);
20 static void heap_area_pair_free_voidp(void *d);
21 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
22 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
23 static heap_area_t new_heap_area(int block, int fragment);
24
25 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore);
26 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals);
27
28 static int in_mc_comparison_ignore(int block, int fragment);
29 static size_t heap_comparison_ignore_size(void *address);
30 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2);
31 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a);
32
33 static char* is_stack(void *address);
34
35 void mmalloc_backtrace_block_display(void* heapinfo, int block){
36
37   xbt_ex_t e;
38
39   if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
40     fprintf(stderr, "No backtrace available for that block, sorry.\n");
41     return;
42   }
43
44   memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
45   e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size;
46
47   xbt_ex_setup_backtrace(&e);
48   if (e.used == 0) {
49     fprintf(stderr, "(backtrace not set)\n");
50   } else if (e.bt_strings == NULL) {
51     fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
52   } else {
53     int i;
54
55     fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used);
56     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
57       fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4);
58     }
59   }
60 }
61
62 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
63
64   xbt_ex_t e;
65
66   memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
67   e.used = XBT_BACKTRACE_SIZE;
68
69   xbt_ex_setup_backtrace(&e);
70   if (e.used == 0) {
71     fprintf(stderr, "(backtrace not set)\n");
72   } else if (e.bt_strings == NULL) {
73     fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
74   } else {
75     int i;
76
77     fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used);
78     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
79       fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4);
80     }
81   }
82
83 }
84
85 void mmalloc_backtrace_display(void *addr){
86
87   size_t block, frag_nb;
88   int type;
89   
90   xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
91
92   block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1);
93
94   type = heap->heapinfo[block].type;
95
96   switch(type){
97   case -1 : /* Free block */
98     fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n");
99     xbt_abort();
100     break; 
101   case 0: /* Large block */
102     mmalloc_backtrace_block_display(heap->heapinfo, block);
103     break;
104   default: /* Fragmented block */
105     frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type;
106     if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
107       fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n");
108       xbt_abort();
109     }
110     mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb);
111     break;
112   }
113 }
114
115
116 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
117 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
118 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
119
120 int ignore_done = 0;
121
122 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stack1, xbt_dynar_t *stack2, xbt_dynar_t *equals){
123
124   if(heap1 == NULL && heap1 == NULL){
125     XBT_DEBUG("Malloc descriptors null");
126     return 0;
127   }
128
129   if(heap1->heaplimit != heap2->heaplimit){
130     XBT_DEBUG("Different limit of valid info table indices");
131     return 1;
132   }
133
134   /* Heap information */
135   heaplimit = ((struct mdesc *)heap1)->heaplimit;
136
137   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
138
139   heapbase1 = (char *)heap1 + BLOCKSIZE;
140   heapbase2 = (char *)heap2 + BLOCKSIZE;
141
142   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
143   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
144
145   heapsize1 = heap1->heapsize;
146   heapsize2 = heap2->heapsize;
147
148   /* Start comparison */
149   size_t i1, i2, j1, j2, k, current_block, current_fragment;
150   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
151   void *real_addr_block1, *real_addr_block2;
152   char *stack_name;
153
154   xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
155
156   int equal, res_compare = 0;
157
158   /* Init equal information */
159   i1 = 1;
160
161   while(i1<=heaplimit){
162     if(heapinfo1[i1].type == 0){
163       heapinfo1[i1].busy_block.equal_to = NULL;
164     }
165     if(heapinfo1[i1].type > 0){
166       for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
167         heapinfo1[i1].busy_frag.equal_to[j1] = NULL;
168       }
169     }
170     i1++; 
171   }
172
173   i2 = 1;
174
175   while(i2<=heaplimit){
176     if(heapinfo2[i2].type == 0){
177       heapinfo2[i2].busy_block.equal_to = NULL;
178     }
179     if(heapinfo2[i2].type > 0){
180       for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
181         heapinfo2[i2].busy_frag.equal_to[j2] = NULL;
182       }
183     }
184     i2++; 
185   }
186
187   /* Check busy blocks*/
188
189   i1 = 1;
190
191   while(i1 <= heaplimit){
192
193     current_block = i1;
194
195     if(heapinfo1[i1].type == -1){ /* Free block */
196       i1++;
197       continue;
198     }
199
200     addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
201     real_addr_block1 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block1) - (char *)heapbase1);
202
203     if(heapinfo1[i1].type == 0){  /* Large block */
204       
205       if((stack_name = is_stack(real_addr_block1)) != NULL){
206         stack_region_t stack = xbt_new0(s_stack_region_t, 1);
207         stack->address = addr_block1;
208         stack->process_name = strdup(stack_name);
209         stack->size = heapinfo1[i1].busy_block.busy_size;
210         xbt_dynar_push(*stack1, &stack);
211         res_compare = -1;
212       }
213
214       if(heapinfo1[i1].busy_block.busy_size == 0){
215         i1++;
216         continue;
217       }
218
219       if(heapinfo1[i1].busy_block.equal_to != NULL){
220         i1++;
221         continue;
222       }
223     
224       i2 = 1;
225       equal = 0;
226       res_compare = 0;
227   
228       /* Try first to associate to same block in the other heap */
229       if(heapinfo2[current_block].type == heapinfo1[current_block].type){
230
231         if(heapinfo2[current_block].busy_block.equal_to == NULL){  
232         
233           if(heapinfo1[current_block].busy_block.busy_size == heapinfo2[current_block].busy_block.busy_size){
234
235             addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
236             real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
237           
238             if((stack_name = is_stack(real_addr_block2)) != NULL){
239               stack_region_t stack = xbt_new0(s_stack_region_t, 1);
240               stack->address = addr_block2;
241               stack->process_name = strdup(stack_name);
242               stack->size = heapinfo2[current_block].busy_block.busy_size;
243               xbt_dynar_push(*stack2, &stack);
244               res_compare = -1;
245             }
246         
247             add_heap_area_pair(previous, current_block, -1, current_block, -1);
248         
249             if(res_compare != -1){
250               if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
251                 if(in_mc_comparison_ignore((int)current_block, -1))
252                   res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 1);
253                 else
254                   res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
255               }else{
256                 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
257               }
258             }
259         
260             if(res_compare == 0 || res_compare == -1){
261               for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
262                 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
263               for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
264                 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
265               equal = 1;
266               match_equals(previous, equals);
267               i1 = i1 + heapinfo1[current_block].busy_block.size;
268             }
269         
270             xbt_dynar_reset(previous);
271         
272           }
273
274         }
275         
276       }
277
278       while(i2 <= heaplimit && !equal){
279
280         addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));        
281         real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
282         
283         if((stack_name = is_stack(real_addr_block2)) != NULL){
284           stack_region_t stack = xbt_new0(s_stack_region_t, 1);
285           stack->address = addr_block2;
286           stack->process_name = strdup(stack_name);
287           stack->size = heapinfo2[i2].busy_block.busy_size;
288           xbt_dynar_push(*stack2, &stack);
289           res_compare = -1;
290         }
291            
292         if(i2 == current_block){
293           i2++;
294           continue;
295         }
296
297         if(heapinfo2[i2].type != 0){
298           i2++;
299           continue;
300         }
301
302         if(heapinfo2[i2].busy_block.equal_to != NULL){         
303           i2++;
304           continue;
305         }
306         
307         if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
308           i2++;
309           continue;
310         }
311         
312         if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
313           i2++;
314           continue;
315         }
316
317         /* Comparison */
318         add_heap_area_pair(previous, i1, -1, i2, -1);
319         
320         if(res_compare != -1){
321           if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
322             if(in_mc_comparison_ignore((int)i1, -1))
323               res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
324             else
325               res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
326           }else{
327             res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
328           }
329         }
330         
331         if(res_compare == 0 || res_compare == -1){
332           for(k=1; k < heapinfo2[i2].busy_block.size; k++)
333             heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
334           for(k=1; k < heapinfo1[i1].busy_block.size; k++)
335             heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
336           equal = 1;
337           match_equals(previous, equals);
338           i1 = i1 + heapinfo1[i1].busy_block.size;
339         }
340
341         xbt_dynar_reset(previous);
342
343         i2++;
344
345       }
346
347       if(!equal)
348         i1++;
349       
350     }else{ /* Fragmented block */
351
352       for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
353
354         current_fragment = j1;
355
356         if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
357           continue;
358
359         if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
360           continue;
361
362         addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
363
364         i2 = 1;
365         equal = 0;
366         
367         /* Try first to associate to same fragment in the other heap */
368         if(heapinfo2[current_block].type == heapinfo1[current_block].type){
369
370           if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){  
371           
372               if(heapinfo1[current_block].busy_frag.frag_size[current_fragment] == heapinfo2[current_block].busy_frag.frag_size[current_fragment]){
373
374                 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
375                 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << heapinfo2[current_block].type));
376                
377                 add_heap_area_pair(previous, current_block, current_fragment, current_block, current_fragment);
378             
379                 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
380                   if(in_mc_comparison_ignore((int)current_block, (int)current_fragment))
381                     res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 1);
382                   else
383                     res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
384                 }else{
385                   res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
386                 }
387
388                 if(res_compare == 0){
389                   equal = 1;
390                   match_equals(previous, equals);
391                 }
392             
393                 xbt_dynar_reset(previous);
394             
395               }
396
397             }
398         }
399
400         while(i2 <= heaplimit && !equal){
401
402           
403           if(heapinfo2[i2].type <= 0){
404             i2++;
405             continue;
406           }
407
408           for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
409
410             if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
411               continue;
412
413             if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)                
414               continue;              
415              
416             if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]) /* Different size_used */    
417               continue;
418              
419             addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
420             addr_frag2 = (void*) ((char *)addr_block2 + (j2 << heapinfo2[i2].type));
421              
422             /* Comparison */
423             add_heap_area_pair(previous, i1, j1, i2, j2);
424             
425             if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
426               if(in_mc_comparison_ignore((int)i1, (int)j1))
427                 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
428               else
429                 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
430             }else{
431               res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
432             }
433
434             if(res_compare == 0){
435               equal = 1;
436               match_equals(previous, equals);
437               break;
438             }
439
440             xbt_dynar_reset(previous);
441
442           }
443
444           i2++;
445
446         }
447
448       }
449
450       i1++;
451       
452     }
453
454   }
455
456   /* All blocks/fragments are equal to another block/fragment ? */
457   size_t i = 1, j = 0;
458   int nb_diff1 = 0, nb_diff2 = 0;
459  
460   while(i<heaplimit){
461     if(heapinfo1[i].type == 0){
462       if(heapinfo1[i].busy_block.busy_size > 0){
463         if(heapinfo1[i].busy_block.equal_to == NULL){
464           if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
465             addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
466             XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
467             mmalloc_backtrace_block_display((void*)heapinfo1, i);
468           }
469           nb_diff1++;
470         }else{
471           xbt_free(heapinfo1[i].busy_block.equal_to);
472         }
473       }
474     }
475     if(heapinfo1[i].type > 0){
476       addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
477       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
478         if(heapinfo1[i].busy_frag.frag_size[j] > 0){
479           if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
480             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
481               addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
482               XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
483               mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
484             }
485             nb_diff1++;
486           }else{
487             xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
488           }
489         }
490       }
491     }
492     
493     i++; 
494   }
495
496   XBT_DEBUG("Different blocks or fragments in heap1 : %d", nb_diff1);
497
498   i = 1;
499
500   while(i<heaplimit){
501     if(heapinfo2[i].type == 0){
502       if(heapinfo2[i].busy_block.busy_size > 0){
503         if(heapinfo2[i].busy_block.equal_to == NULL){
504           if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
505             addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
506             XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
507             mmalloc_backtrace_block_display((void*)heapinfo2, i);
508           }
509           nb_diff2++;
510         }else{
511           xbt_free(heapinfo2[i].busy_block.equal_to);
512         }
513       }
514     }
515     if(heapinfo2[i].type > 0){
516       addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
517       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
518         if(heapinfo2[i].busy_frag.frag_size[j] > 0){
519           if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
520             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
521               addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
522               XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
523               mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
524             }
525             nb_diff2++;
526           }else{
527             xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
528           }
529         }
530       }
531     }
532     i++; 
533   }
534
535   XBT_DEBUG("Different blocks or fragments in heap2 : %d", nb_diff2);
536
537   xbt_dynar_free(&previous);
538   ignore_done = 0;
539   s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
540   heapinfo1 = NULL, heapinfo2 = NULL;
541   heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
542
543   return ((nb_diff1 > 0) || (nb_diff2 > 0));
544 }
545
546 static heap_area_t new_heap_area(int block, int fragment){
547   heap_area_t area = NULL;
548   area = xbt_new0(s_heap_area_t, 1);
549   area->block = block;
550   area->fragment = fragment;
551   return area;
552 }
553
554 static int in_mc_comparison_ignore(int block, int fragment){
555
556   unsigned int cursor = 0;
557   int start = 0;
558   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
559   mc_heap_ignore_region_t region;
560
561   while(start <= end){
562     cursor = (start + end) / 2;
563     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
564     if(region->block == block){
565       if(region->fragment == fragment)
566         return 1;
567       if(region->fragment < fragment)
568         start = cursor + 1;
569       if(region->fragment > fragment)
570         end = cursor - 1;
571     }
572     if(region->block < block)
573       start = cursor + 1;
574     if(region->block > block)
575       end = cursor - 1; 
576   }
577
578   return 0;
579 }
580
581 static size_t heap_comparison_ignore_size(void *address){
582   unsigned int cursor = 0;
583   int start = 0;
584   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
585   mc_heap_ignore_region_t region;
586
587   while(start <= end){
588     cursor = (start + end) / 2;
589     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
590     if(region->address == address)
591       return region->size;
592     if(region->address < address)
593       start = cursor + 1;
594     if(region->address > address)
595       end = cursor - 1;   
596   }
597
598   return 0;
599 }
600
601
602 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore){
603
604   size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
605   void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
606   size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
607   int res_compare;
608   void *current_area1, *current_area2;
609  
610   while(i<size){
611
612     if(check_ignore){
613
614       current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
615       if((ignore1 = heap_comparison_ignore_size(current_area1)) > 0){
616         current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
617         if((ignore2 = heap_comparison_ignore_size(current_area2))  == ignore1){
618           i = i + ignore2;
619           ignore_done++;
620           continue;
621         }
622       }
623
624     }
625    
626     if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
627
628       /* Check pointer difference */
629       pointer_align = (i / sizeof(void*)) * sizeof(void*);
630       address_pointed1 = *((void **)((char *)area1 + pointer_align));
631       address_pointed2 = *((void **)((char *)area2 + pointer_align));
632
633       /* Get pointed blocks number */ 
634       block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
635       block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
636
637       /* Check if valid blocks number */
638       if((char *)address_pointed1 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed1 > heapsize1 || block_pointed1 < 1 || (char *)address_pointed2 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed2 > heapsize2 || block_pointed2 < 1)
639         return 1;
640
641       if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
642
643         addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
644         addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
645         
646         if(heapinfo1[block_pointed1].type == 0){ /* Large block */
647
648           if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
649             return 1;
650           }
651
652           if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
653             return 1;
654           }
655
656           if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
657
658             if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
659               if(in_mc_comparison_ignore(block_pointed1, -1))
660                 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
661               else
662                 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
663             }else{
664               res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
665             }
666             
667             if(res_compare == 1)    
668               return 1;
669            
670           }
671           
672         }else{ /* Fragmented block */
673
674           /* Get pointed fragments number */ 
675           frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
676           frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
677          
678           if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */    
679             return 1;
680             
681           addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
682           addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
683
684           if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
685
686             if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
687               if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
688                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
689               else
690                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
691             }else{
692               res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
693             }
694
695             if(res_compare == 1)
696               return 1;
697            
698           }
699           
700         }
701           
702       }else{
703
704         if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
705           
706           addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
707           addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
708        
709           frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
710           frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
711
712           if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */  
713             return 1;
714
715           addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
716           addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
717
718           if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
719
720             if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
721               if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
722                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
723               else
724                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
725             }else{
726               res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
727             }
728             
729             if(res_compare == 1)
730               return 1;
731            
732           }
733
734         }else{
735           return 1;
736         }
737
738       }
739
740       i = pointer_align + sizeof(void *);
741       
742     }else{
743
744       i++;
745
746     }
747   }
748
749   return 0;
750   
751
752 }
753
754 static void heap_area_pair_free(heap_area_pair_t pair){
755   if (pair){
756     free(pair);
757     pair = NULL;
758   }
759 }
760
761 static void heap_area_pair_free_voidp(void *d)
762 {
763   heap_area_pair_free((heap_area_pair_t) * (void **) d);
764 }
765
766 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
767
768   if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
769     heap_area_pair_t pair = NULL;
770     pair = xbt_new0(s_heap_area_pair_t, 1);
771     pair->block1 = block1;
772     pair->fragment1 = fragment1;
773     pair->block2 = block2;
774     pair->fragment2 = fragment2;
775     
776     xbt_dynar_push(list, &pair); 
777
778     return 1;
779   }
780
781   return 0;
782 }
783  
784 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
785   
786   unsigned int cursor = 0;
787   heap_area_pair_t current_pair;
788
789   xbt_dynar_foreach(list, cursor, current_pair){
790     if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
791       return 0; 
792   }
793   
794   return 1;
795 }
796
797 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals){
798
799   unsigned int cursor = 0;
800   heap_area_pair_t current_pair;
801   heap_area_t previous_area;
802
803   void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
804
805   xbt_dynar_foreach(list, cursor, current_pair){
806
807     if(current_pair->fragment1 != -1){
808
809       real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
810       real_addr_frag1 = (void*) ((char *)real_addr_block1 + (current_pair->fragment1 << heapinfo1[current_pair->block1].type));
811       real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
812       real_addr_frag2 = (void*) ((char *)real_addr_block2 + (current_pair->fragment2 << heapinfo2[current_pair->block2].type));
813
814       if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){    
815         remove_heap_equality(equals, 1, real_addr_frag1);
816         previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
817         xbt_free( heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
818         heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
819         xbt_free(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1]); 
820       }
821       if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){        
822         remove_heap_equality(equals, 2, real_addr_frag2); 
823         previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
824         xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
825         heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
826         xbt_free(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2]);
827       }
828       
829       if(real_addr_frag1 != real_addr_frag2)
830         add_heap_equality(equals, real_addr_frag1, real_addr_frag2);
831
832       heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
833       heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
834
835     }else{
836
837       real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
838       real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
839
840       if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
841         remove_heap_equality(equals, 1, real_addr_block1);
842         previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
843         xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
844         heapinfo2[previous_area->block].busy_block.equal_to = NULL; 
845         xbt_free(heapinfo1[current_pair->block1].busy_block.equal_to);
846       }
847       if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
848         remove_heap_equality(equals, 2, real_addr_block2);
849         previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
850         xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
851         heapinfo1[previous_area->block].busy_block.equal_to = NULL;
852         xbt_free(heapinfo2[current_pair->block2].busy_block.equal_to);
853       }
854       
855       if(real_addr_block1 != real_addr_block2)
856         add_heap_equality(equals, real_addr_block1, real_addr_block2);
857
858       heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
859       heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
860
861     }
862   }
863
864
865 }
866
867 #ifndef max
868 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
869 #endif
870
871 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
872
873   if(heap1 == NULL && heap1 == NULL){
874     XBT_DEBUG("Malloc descriptors null");
875     return 0;
876   }
877
878   if(heap1->heaplimit != heap2->heaplimit){
879     XBT_DEBUG("Different limit of valid info table indices");
880     return 1;
881   }
882
883   /* Heap information */
884   heaplimit = ((struct mdesc *)heap1)->heaplimit;
885
886   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
887
888   heapbase1 = (char *)heap1 + BLOCKSIZE;
889   heapbase2 = (char *)heap2 + BLOCKSIZE;
890
891   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
892   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
893
894   heapsize1 = heap1->heapsize;
895   heapsize2 = heap2->heapsize;
896
897   /* Start comparison */
898   size_t i, j, k;
899   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
900
901   int distance = 0;
902
903   /* Check busy blocks*/
904
905   i = 1;
906
907   while(i <= heaplimit){
908
909     addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
910     addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
911
912     if(heapinfo1[i].type != heapinfo2[i].type){
913   
914       distance += BLOCKSIZE;
915       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
916       i++;
917     
918     }else{
919
920       if(heapinfo1[i].type == -1){ /* Free block */
921         i++;
922         continue;
923       }
924
925       if(heapinfo1[i].type == 0){ /* Large block */
926        
927         if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
928           distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
929           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
930           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);
931           continue;
932         }
933
934         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
935           distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
936           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
937           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);
938           continue;
939           }*/
940
941         k = 0;
942
943         //while(k < (heapinfo1[i].busy_block.busy_size)){
944         while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
945           if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
946             distance ++;
947           }
948           k++;
949         } 
950
951         i++;
952
953       }else { /* Fragmented block */
954
955         for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
956
957           addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
958           addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
959
960           if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
961             continue;
962           }
963           
964           
965           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
966             distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
967             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); 
968             continue;
969             }*/
970    
971           k=0;
972
973           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
974           while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
975             if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
976               distance ++;
977             }
978             k++;
979           }
980
981         }
982
983         i++;
984
985       }
986       
987     }
988
989   }
990
991   return distance;
992   
993 }
994
995 static char * is_stack(void *address){
996   unsigned int cursor = 0;
997   stack_region_t stack;
998
999   xbt_dynar_foreach(stacks_areas, cursor, stack){
1000     if(address == stack->address)
1001       return stack->process_name;
1002   }
1003
1004   return NULL;
1005 }
1006
1007 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2){
1008   
1009   if(xbt_dynar_is_empty(*equals)){
1010
1011     heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1012     he->address1 = a1;
1013     he->address2 = a2;
1014
1015     xbt_dynar_insert_at(*equals, 0, &he);
1016   
1017   }else{
1018
1019     unsigned int cursor = 0;
1020     int start = 0;
1021     int end = xbt_dynar_length(*equals) - 1;
1022     heap_equality_t current_equality = NULL;
1023
1024     while(start <= end){
1025       cursor = (start + end) / 2;
1026       current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1027       if(current_equality->address1 == a1){
1028         if(current_equality->address2 == a2)
1029           return;
1030         if(current_equality->address2 < a2)
1031           start = cursor + 1;
1032         if(current_equality->address2 > a2)
1033           end = cursor - 1;
1034       }
1035       if(current_equality->address1 < a1)
1036         start = cursor + 1;
1037       if(current_equality->address1 > a1)
1038         end = cursor - 1; 
1039     }
1040
1041     heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1042     he->address1 = a1;
1043     he->address2 = a2;
1044   
1045     if(current_equality->address1 < a1)
1046       xbt_dynar_insert_at(*equals, cursor + 1 , &he);
1047     else
1048        xbt_dynar_insert_at(*equals, cursor, &he); 
1049
1050   }
1051
1052 }
1053
1054 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a){
1055   
1056   unsigned int cursor = 0;
1057   heap_equality_t current_equality;
1058   int found = 0;
1059
1060   if(address == 1){
1061
1062     int start = 0;
1063     int end = xbt_dynar_length(*equals) - 1;
1064
1065
1066     while(start <= end && found == 0){
1067       cursor = (start + end) / 2;
1068       current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1069       if(current_equality->address1 == a)
1070         found = 1;
1071       if(current_equality->address1 < a)
1072         start = cursor + 1;
1073       if(current_equality->address1 > a)
1074         end = cursor - 1; 
1075     }
1076
1077     if(found == 1)
1078       xbt_dynar_remove_at(*equals, cursor, NULL);
1079   
1080   }else{
1081
1082     xbt_dynar_foreach(*equals, cursor, current_equality){
1083       if(current_equality->address2 == a){
1084         found = 1;
1085         break;
1086       }
1087     }
1088
1089     if(found == 1)
1090       xbt_dynar_remove_at(*equals, cursor, NULL);
1091
1092   }
1093
1094   
1095 }
1096
1097 int is_free_area(void *area, xbt_mheap_t heap){
1098
1099   void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1100   malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1101   size_t heapsize = heap->heapsize;
1102
1103   /* Get block number */ 
1104   size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1105   size_t fragment;
1106
1107   /* Check if valid block number */
1108   if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1109     return 0;
1110
1111   if(heapinfo[block].type < 0)
1112     return 1;
1113
1114   if(heapinfo[block].type == 0)
1115     return 0;
1116
1117   if(heapinfo[block].type > 0){
1118     fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1119     if(heapinfo[block].busy_frag.frag_size[fragment] == 0)
1120       return 1;  
1121   }
1122
1123   return 0;
1124   
1125
1126
1127 }