Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : remove arg std_heap_addr in mmalloc_compare_heap and mmalloc_compare_...
[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   //size_t block = BLOCK(ptr);
58   int type;
59   xbt_ex_t e;
60
61   /*if ((char *) ptr < (char *) mdp->heapbase || block > mdp->heapsize) {
62     fprintf(stderr,"Ouch, this pointer is not mine. I cannot display its backtrace. I refuse it to death!!\n");
63     abort();
64     }*/
65
66   type = mdp->heapinfo[block].type;
67
68   if (type != 0) {
69     fprintf(stderr,"Only full blocks are backtraced for now. Ignoring your request.\n");
70     return;
71   }
72   if (mdp->heapinfo[block].busy_block.bt_size == 0) {
73     fprintf(stderr,"No backtrace available for that block, sorry.\n");
74     return;
75   }
76
77   memcpy(&e.bt,&(mdp->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
78   e.used = mdp->heapinfo[block].busy_block.bt_size;
79
80   xbt_ex_setup_backtrace(&e);
81   if (e.used == 0) {
82     fprintf(stderr, "(backtrace not set)\n");
83   } else if (e.bt_strings == NULL) {
84     fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
85   } else {
86     int i;
87
88     fprintf(stderr, "Backtrace of where the block %zu where malloced (%d frames):\n", block ,e.used);
89     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
90       fprintf(stderr,"%d",i);fflush(NULL);
91       fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
92     }
93   }
94 }
95
96 int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2){
97
98   if(mdp1 == NULL && mdp2 == NULL){
99     XBT_DEBUG("Malloc descriptors null\n");
100     return 0;
101   }
102
103   int errors = mmalloc_compare_mdesc(mdp1, mdp2);
104
105   return (errors > 0);
106
107 }
108
109 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2){
110
111   if(mdp1->headersize != mdp2->headersize){
112     fprintf(stderr, "Different size of the file header for the mapped files\n");
113     return 1;
114   }
115
116   if(mdp1->refcount != mdp2->refcount){
117     fprintf(stderr, "Different number of processes that attached the heap\n");
118     return 1;
119   }
120
121   if(strcmp(mdp1->magic, mdp2->magic) != 0){
122     fprintf(stderr,"Different magic number\n");
123     return 1;
124   }
125
126   if(mdp1->flags != mdp2->flags){
127     fprintf(stderr,"Different flags\n");  
128     return 1;
129   }
130
131   if(mdp1->heapsize != mdp2->heapsize){
132     fprintf(stderr,"Different number of info entries\n");
133     return 1;
134   }
135   
136
137   if(mdp1->heapbase != mdp2->heapbase){
138     fprintf(stderr,"Different first block of the heap\n");
139     return 1;
140   }
141
142
143   if(mdp1->heapindex != mdp2->heapindex){
144     fprintf(stderr,"Different index for the heap table : %zu - %zu\n", mdp1->heapindex, mdp2->heapindex);
145     return 1;
146   }
147
148
149   if(mdp1->base != mdp2->base){
150     fprintf(stderr,"Different base address of the memory region\n");
151     return 1;
152   }
153
154   if(mdp1->breakval != mdp2->breakval){
155     fprintf(stderr,"Different current location in the memory region\n");
156     return 1;
157   }
158
159   if(mdp1->top != mdp2->top){
160     fprintf(stderr,"Different end of the current location in the memory region\n");
161     return 1;
162   }
163
164   if(mdp1->heaplimit != mdp2->heaplimit){
165     fprintf(stderr,"Different limit of valid info table indices\n");
166     return 1;
167   }
168
169   if(mdp1->fd != mdp2->fd){
170     fprintf(stderr,"Different file descriptor for the file to which this malloc heap is mapped\n");
171     return 1;
172   }
173
174   if(mdp1->version != mdp2->version){
175     fprintf(stderr,"Different version of the mmalloc package\n");
176     return 1;
177   }
178
179
180   size_t i, j;
181   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
182   size_t frag_size;
183
184   i = 0;
185
186   /* Check busy blocks*/
187
188   while(i < mdp1->heapindex){
189
190     if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
191       fprintf(stderr,"Different type of block : %d - %d\n", mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
192       return 1;
193     }
194
195     addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + (i * BLOCKSIZE);
196     addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + (i * BLOCKSIZE);
197
198     if(mdp1->heapinfo[i].type == 0){ /* busy large block */
199
200       if(mdp1->heapinfo[i].busy_block.size != mdp2->heapinfo[i].busy_block.size){
201         fprintf(stderr,"Different size of a large cluster : %zu - %zu\n", mdp1->heapinfo[i].busy_block.size, mdp2->heapinfo[i].busy_block.size);
202         return 1;
203       } 
204
205       if(mdp1->heapinfo[i].busy_block.busy_size != mdp2->heapinfo[i].busy_block.busy_size){
206         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);
207         return 1;
208       } 
209
210       if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy_block.busy_size)) != 0){
211         fprintf(stderr,"Different 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);
212         fprintf(stderr, "Backtrace size : %d\n", mdp1->heapinfo[i].busy_block.bt_size);
213         mmalloc_backtrace_block_display(mdp1, i);
214         return 1;
215       }
216
217       //fprintf(stderr, "Backtrace size : %d\n", mdp1->heapinfo[i].busy_block.bt_size);
218       //mmalloc_backtrace_block_display(mdp1, i);
219         
220       i = i + mdp1->heapinfo[i].busy_block.size;
221
222     }else{
223       
224       if(mdp1->heapinfo[i].type > 0){ /* busy fragmented block */
225
226         if(mdp1->heapinfo[i].type != mdp2->heapinfo[i].type){
227           fprintf(stderr,"Different size of fragments in fragmented block %zu : %d - %d\n", i, mdp1->heapinfo[i].type, mdp2->heapinfo[i].type);
228           return 1;
229         }
230
231         if(mdp1->heapinfo[i].busy_frag.nfree != mdp2->heapinfo[i].busy_frag.nfree){
232           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);
233           return 1;
234         } 
235         
236         if(mdp1->heapinfo[i].busy_frag.first != mdp2->heapinfo[i].busy_frag.first){
237           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);
238           return 1;
239         } 
240
241         for(j=0; j< MAX_FRAGMENT_PER_BLOCK; j++){
242
243           if(mdp1->heapinfo[i].busy_frag.frag_size[j] != mdp2->heapinfo[i].busy_frag.frag_size[j]){
244             fprintf(stderr,"Different busy_size for fragment %zu in block %zu : %u - %u\n", j, i, mdp1->heapinfo[i].busy_frag.frag_size[j], mdp2->heapinfo[i].busy_frag.frag_size[j]);
245             return 1;
246           }
247
248           if(mdp1->heapinfo[i].busy_frag.frag_size[j] > 0){
249             
250             frag_size = pow(2, mdp1->heapinfo[i].type);
251             addr_frag1 = (char *)addr_block1 + (j * frag_size);
252             addr_frag2 = (char *)addr_block2 + (j * frag_size);
253
254             if(memcmp(addr_frag1, addr_frag2, frag_size) != 0){
255               fprintf(stderr,"Different data in fragment %zu in block %zu \n", j, i);
256               return 1;
257             }
258
259           }
260         }
261
262         i++;
263
264       }else{ /* free block */
265
266         i++;
267
268       }
269       
270     }
271
272   }
273
274
275   return 0;
276 }
277
278
279 void mmalloc_display_info_heap(xbt_mheap_t h){
280
281 }
282
283
284