Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : get offset for each different byte between two blocks/fragments
[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
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
11                                 "Logging specific to mm_diff in mmalloc");
12
13 extern char *xbt_binary_name;
14
15 void mmalloc_backtrace_display(xbt_mheap_t mdp, void *ptr){
16   size_t block = BLOCK(ptr);
17   int type;
18   xbt_ex_t e;
19
20   if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
21     fprintf(stderr,"Ouch, this pointer is not mine. I cannot display its backtrace. I refuse it to death!!\n");
22     abort();
23   }
24
25   type = mdp->heapinfo[block].type;
26
27   if (type != 0) {
28     //fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
29     return;
30   }
31   if (mdp->heapinfo[block].busy_block.bt_size == 0) {
32     fprintf(stderr,"No backtrace available for that block, sorry.\n");
33     return;
34   }
35
36   memcpy(&e.bt,&(mdp->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
37   e.used = mdp->heapinfo[block].busy_block.bt_size;
38
39   xbt_ex_setup_backtrace(&e);
40   if (e.used == 0) {
41     fprintf(stderr, "(backtrace not set)\n");
42   } else if (e.bt_strings == NULL) {
43     fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
44   } else {
45     int i;
46
47     fprintf(stderr, "Backtrace of where the block %p where malloced (%d frames):\n",ptr,e.used);
48     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
49       fprintf(stderr,"%d",i);fflush(NULL);
50       fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
51     }
52   }
53 }
54
55
56 void mmalloc_backtrace_block_display(xbt_mheap_t mdp, size_t block){
57
58   int type;
59   xbt_ex_t e;
60
61   type = mdp->heapinfo[block].type;
62
63   if (type != 0) {
64     fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
65     return;
66   }
67   if (mdp->heapinfo[block].busy_block.bt_size == 0) {
68     fprintf(stderr,"No backtrace available for that block, sorry.\n");
69     return;
70   }
71
72   memcpy(&e.bt,&(mdp->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
73   e.used = mdp->heapinfo[block].busy_block.bt_size;
74
75   xbt_ex_setup_backtrace(&e);
76   if (e.used == 0) {
77     fprintf(stderr, "(backtrace not set)\n");
78   } else if (e.bt_strings == NULL) {
79     fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
80   } else {
81     int i;
82
83     fprintf(stderr, "Backtrace of where the block %zu where malloced (%d frames):\n", block ,e.used);
84     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
85       fprintf(stderr,"%d",i);fflush(NULL);
86       fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
87     }
88   }
89 }
90
91 void mmalloc_backtrace_fragment_display(xbt_mheap_t mdp, size_t block, size_t frag){
92   
93   xbt_ex_t e;
94
95   memcpy(&e.bt,&(mdp->heapinfo[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
96   e.used = XBT_BACKTRACE_SIZE;
97
98   xbt_ex_setup_backtrace(&e);
99   if (e.used == 0) {
100     fprintf(stderr, "(backtrace not set)\n");
101   } else if (e.bt_strings == NULL) {
102     fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
103   } else {
104     int i;
105
106     fprintf(stderr, "Backtrace of where the fragment %zu in block %zu where malloced (%d frames):\n", frag, block ,e.used);
107     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
108       fprintf(stderr,"%d",i);fflush(NULL);
109       fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
110     }
111   }
112 }
113
114 int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2){
115
116   if(mdp1 == NULL && mdp2 == NULL){
117     XBT_DEBUG("Malloc descriptors null\n");
118     return 0;
119   }
120
121   int errors = mmalloc_compare_mdesc(mdp1, mdp2);
122
123   return (errors > 0);
124
125 }
126
127 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2){
128
129   int errors = 0;
130
131   if(mdp1->headersize != mdp2->headersize){
132     fprintf(stderr, "Different size of the file header for the mapped files\n");
133     return 1;
134   }
135
136   if(mdp1->refcount != mdp2->refcount){
137     fprintf(stderr, "Different number of processes that attached the heap\n");
138     return 1;
139   }
140
141   if(strcmp(mdp1->magic, mdp2->magic) != 0){
142     fprintf(stderr,"Different magic number\n");
143     return 1;
144   }
145
146   if(mdp1->flags != mdp2->flags){
147     fprintf(stderr,"Different flags\n");  
148     return 1;
149   }
150
151   if(mdp1->heapsize != mdp2->heapsize){
152     fprintf(stderr,"Different number of info entries\n");
153     return 1;
154   }
155   
156
157   if(mdp1->heapbase != mdp2->heapbase){
158     fprintf(stderr,"Different first block of the heap\n");
159     return 1;
160   }
161
162
163   if(mdp1->heapindex != mdp2->heapindex){
164     fprintf(stderr,"Different index for the heap table : %zu - %zu\n", mdp1->heapindex, mdp2->heapindex);
165     return 1;
166   }
167
168
169   if(mdp1->base != mdp2->base){
170     fprintf(stderr,"Different base address of the memory region\n");
171     return 1;
172   }
173
174   if(mdp1->breakval != mdp2->breakval){
175     fprintf(stderr,"Different current location in the memory region\n");
176     return 1;
177   }
178
179   if(mdp1->top != mdp2->top){
180     fprintf(stderr,"Different end of the current location in the memory region\n");
181     return 1;
182   }
183
184   if(mdp1->heaplimit != mdp2->heaplimit){
185     fprintf(stderr,"Different limit of valid info table indices\n");
186     return 1;
187   }
188
189   if(mdp1->fd != mdp2->fd){
190     fprintf(stderr,"Different file descriptor for the file to which this malloc heap is mapped\n");
191     return 1;
192   }
193
194   if(mdp1->version != mdp2->version){
195     fprintf(stderr,"Different version of the mmalloc package\n");
196     return 1;
197   }
198
199
200   size_t i, j;
201   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
202   size_t frag_size;
203
204   i = 0;
205
206   int k;
207   int distance = 0;
208
209   /* Check busy blocks*/
210
211   while(i < mdp1->heapindex){
212
213     if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
214       fprintf(stderr,"Different type of block : %d - %d\n", mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
215       errors++;
216     }
217
218     addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + (i * BLOCKSIZE);
219     addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + (i * BLOCKSIZE);
220
221     if(mdp1->heapinfo[i].type == 0){ /* busy large block */
222
223       if(mdp1->heapinfo[i].busy_block.size != mdp2->heapinfo[i].busy_block.size){
224         fprintf(stderr,"Different size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.size, mdp2->heapinfo[i].busy_block.size);
225         errors++;
226       } 
227
228       if(mdp1->heapinfo[i].busy_block.busy_size != mdp2->heapinfo[i].busy_block.busy_size){
229         fprintf(stderr,"Different busy_size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.busy_size, mdp2->heapinfo[i].busy_block.busy_size);
230         errors++;
231       } 
232
233       if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy_block.busy_size)) != 0){
234         fprintf(stderr,"\nDifferent data in large block %zu (size = %zu (in blocks), busy_size = %zu (in bytes))\n", i, mdp1->heapinfo[i].busy_block.size, mdp1->heapinfo[i].busy_block.busy_size);
235
236         /* Hamming distance on different blocks */
237         distance = 0;
238         for(k=0;k<mdp1->heapinfo[i].busy_block.busy_size;k++){
239           if(memcmp(((char *)addr_block1) + k, ((char *)addr_block2) + k, 1) != 0){
240             fprintf(stderr, "Different byte (offset=%d) (%p - %p) in block %zu\n", k, (char *)addr_block1, (char *)addr_block2, i); 
241             distance++;
242           }
243         }
244
245         fprintf(stderr, "Hamming distance between blocks : %d\n", distance);
246
247         mmalloc_backtrace_block_display(mdp1, i);
248         mmalloc_backtrace_block_display(mdp2, i);
249         errors++;
250       }
251
252    
253       i = i + mdp1->heapinfo[i].busy_block.size;
254
255     }else{
256       
257       if(mdp1->heapinfo[i].type > 0){ /* busy fragmented block */
258
259         if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
260           fprintf(stderr,"Different size of fragments in fragmented block %zu : %d - %d\n", i, mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
261           errors++;
262         }
263
264         if(mdp1->heapinfo[i].busy_frag.nfree != mdp2->heapinfo[i].busy_frag.nfree){
265           fprintf(stderr,"Different free fragments in fragmented block %zu : %zu - %zu\n", i, mdp1->heapinfo[i].busy_frag.nfree, mdp2->heapinfo[i].busy_frag.nfree);
266           errors++;
267         } 
268         
269         if(mdp1->heapinfo[i].busy_frag.first != mdp2->heapinfo[i].busy_frag.first){
270           fprintf(stderr,"Different busy_size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.busy_size, mdp2->heapinfo[i].busy_block.busy_size);
271           errors++;
272         } 
273
274         frag_size = pow(2, mdp1->heapinfo[i].type);
275
276         for(j=0; j< (BLOCKSIZE/frag_size); j++){
277
278           if(mdp1->heapinfo[i].busy_frag.frag_size[j] != mdp2->heapinfo[i].busy_frag.frag_size[j]){
279             fprintf(stderr,"Different busy_size for fragment %zu in block %zu : %hu - %hu\n", j, i, mdp1->heapinfo[i].busy_frag.frag_size[j], mdp2->heapinfo[i].busy_frag.frag_size[j]);
280             errors++;
281           }
282
283           if(mdp1->heapinfo[i].busy_frag.frag_size[j] > 0){
284             
285             addr_frag1 = (char *)addr_block1 + (j * frag_size);
286             addr_frag2 = (char *)addr_block2 + (j * frag_size);
287
288             if(memcmp(addr_frag1, addr_frag2, mdp1->heapinfo[i].busy_frag.frag_size[j]) != 0){
289               fprintf(stderr,"\nDifferent data in fragment %zu (size = %zu, size used = %hu) in block %zu \n", j, frag_size, mdp1->heapinfo[i].busy_frag.frag_size[j], i);
290
291               /* Hamming distance on different blocks */
292               distance = 0;
293               for(k=0;k<mdp1->heapinfo[i].busy_frag.frag_size[j];k++){
294                 if(memcmp(((char *)addr_frag1) + k, ((char *)addr_frag2) + k, 1) != 0){
295                   fprintf(stderr, "Different byte (offset=%d) (%p - %p) in fragment %zu in block %zu\n", k, (char *)addr_frag1, (char *)addr_frag2, j, i); 
296                   distance++;
297                 }
298               }
299
300               fprintf(stderr, "Hamming distance between fragments : %d\n", distance);
301
302               mmalloc_backtrace_fragment_display(mdp1, i, j);
303               mmalloc_backtrace_fragment_display(mdp2, i, j);
304               errors++;
305             }
306
307           }
308         }
309
310         i++;
311
312       }else{ /* free block */
313
314         i++;
315
316       }
317       
318     }
319
320   }
321
322
323   return (errors);
324 }
325
326
327 void mmalloc_display_info_heap(xbt_mheap_t h){
328
329 }
330
331
332