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 */
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
13 "Logging specific to mm_diff in mmalloc");
15 extern char *xbt_binary_name;
17 void mmalloc_backtrace_block_display(xbt_mheap_t heap, size_t block){
21 if (heap->heapinfo[block].busy_block.bt_size == 0) {
22 fprintf(stderr,"No backtrace available for that block, sorry.\n");
26 memcpy(&e.bt,&(heap->heapinfo[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
27 e.used = heap->heapinfo[block].busy_block.bt_size;
29 xbt_ex_setup_backtrace(&e);
31 fprintf(stderr, "(backtrace not set)\n");
32 } else if (e.bt_strings == NULL) {
33 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
37 fprintf(stderr, "Backtrace of where the block %zu was malloced (%d frames):\n", block ,e.used);
38 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
39 fprintf(stderr,"%d",i);fflush(NULL);
40 fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
45 void mmalloc_backtrace_fragment_display(xbt_mheap_t mdp, size_t block, size_t frag){
49 memcpy(&e.bt,&(mdp->heapinfo[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
50 e.used = XBT_BACKTRACE_SIZE;
52 xbt_ex_setup_backtrace(&e);
54 fprintf(stderr, "(backtrace not set)\n");
55 } else if (e.bt_strings == NULL) {
56 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
60 fprintf(stderr, "Backtrace of where the fragment %zu in block %zu was malloced (%d frames):\n", frag, block ,e.used);
61 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
62 fprintf(stderr,"%d",i);fflush(NULL);
63 fprintf(stderr, "---> %s\n", e.bt_strings[i] + 4);
68 int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2){
70 if(mdp1 == NULL && mdp2 == NULL){
71 fprintf(stderr, "Malloc descriptors null\n");
75 int errors = mmalloc_compare_mdesc(mdp1, mdp2);
81 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2){
85 if(mdp1->heaplimit != mdp2->heaplimit){
86 fprintf(stderr,"Different limit of valid info table indices\n");
90 void* s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
92 fprintf(stderr, "s_heap->heapbase : %p\n", ((struct mdesc*)s_heap)->heapbase);
94 void *heapbase1 = (char *)mdp1 + BLOCKSIZE;
95 void *heapbase2 = (char *)mdp2 + BLOCKSIZE;
97 fprintf(stderr, "Heapbase1 : %p, Heapbase2 : %p\n", heapbase1, heapbase2);
98 fprintf(stderr, "Heapinfo : %p\n", mdp1->heapinfo);
100 malloc_info* heapinfo1 = (malloc_info *)((char *)mdp1 + ((char *)mdp1->heapinfo - (char *)s_heap));
101 malloc_info* heapinfo2 = (malloc_info *)((char *)mdp2 + ((char *)mdp2->heapinfo - (char *)s_heap));
103 fprintf(stderr, "Heapinfo1 : %p, Heapinfo2 : %p\n", heapinfo1, heapinfo2);
106 void *addr_block1 = NULL, *addr_block2 = NULL, *addr_frag1 = NULL, *addr_frag2 = NULL;
113 int total_distance = 0;
116 void *address_pointed1 = NULL, *address_pointed2 = NULL;
118 int block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
119 void *addr_block_pointed1 = NULL, *addr_block_pointed2 = NULL, *addr_frag_pointed1 = NULL, *addr_frag_pointed2 = NULL;
121 /* Check busy blocks*/
123 while(i < mdp1->heaplimit){
125 if(heapinfo1[i].type != heapinfo2[i].type){
126 fprintf(stderr,"Different type of block : %d - %d\n", heapinfo1[i].type, heapinfo2[i].type);
130 /* Get address of block i in each heap */
131 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
132 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
134 if(heapinfo1[i].type == 0){ /* busy large block */
136 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
137 fprintf(stderr,"Different size of a large cluster : %zu - %zu\n", heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
142 if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
143 fprintf(stderr,"Different busy_size of a large cluster : %zu - %zu\n", heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
148 /* Hamming distance on different blocks */
152 for(k=0;k<heapinfo1[i].busy_block.busy_size;k++){
154 if(memcmp(((char *)addr_block1) + k, ((char *)addr_block2) + k, 1) != 0){
156 fprintf(stderr, "Different byte (offset=%d) (%p - %p) in block %zu\n", k, (char *)addr_block1 + k, (char *)addr_block2 + k, i); fflush(NULL);
158 /* Check if pointer difference */
159 pointer_align = (k >> sizeof(void*)) * sizeof(void*);
160 address_pointed1 = *((void **)((char *)addr_block1 + pointer_align));
161 address_pointed2 = *((void **)((char *)addr_block2 + pointer_align));
163 fprintf(stderr, "Addresses pointed : %p - %p \n", address_pointed1, address_pointed2);
165 /* Get block number */
166 block_pointed1 = ((char*)address_pointed1 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
167 block_pointed2 = ((char*)address_pointed2 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
169 fprintf(stderr, "Blocks pointed : %d - %d\n", block_pointed1, block_pointed2);
171 if((char *) address_pointed1 < (char*)((struct mdesc*)s_heap)->heapbase || block_pointed1 > mdp1->heapsize || block_pointed1 < 1 || (char *) address_pointed2 < (char*)((struct mdesc*)s_heap)->heapbase || block_pointed2 > mdp2->heapsize || block_pointed2 < 1) {
172 fprintf(stderr, "Unknown pointer(s) ! \n");
178 addr_block_pointed1 = ((void*) (((ADDR2UINT((size_t)block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
179 addr_block_pointed2 = ((void*) (((ADDR2UINT((size_t)block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
181 fprintf(stderr, "Addr blocks pointed : %p - %p\n", addr_block_pointed1, addr_block_pointed2);
183 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){
185 if(heapinfo1[block_pointed1].type == 0){ // Large block
187 if(heapinfo1[block_pointed1].busy_block.busy_size == heapinfo2[block_pointed2].busy_block.busy_size){
189 if(memcmp(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size) != 0){
192 fprintf(stderr, "False difference detected\n");
199 }else{ // Fragmented block
201 /* Get fragment number */
202 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> ((struct mdesc*)s_heap)->heapinfo[block_pointed1].type;
203 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> ((struct mdesc*)s_heap)->heapinfo[block_pointed2].type;
205 fprintf(stderr, "Fragments pointed : %d - %d\n", frag_pointed1, frag_pointed2);
207 addr_frag_pointed1 = (char*)addr_block_pointed1 + (frag_pointed1 * (int)pow(2, heapinfo1[block_pointed1].type));
208 addr_frag_pointed2 = (char*)addr_block_pointed2 + (frag_pointed2 * (int)pow(2, heapinfo2[block_pointed2].type));
210 fprintf(stderr, "Addr frag pointed : %p - %p\n", addr_frag_pointed1, addr_frag_pointed2);
212 fprintf(stderr, "Size used in fragments pointed : %d - %d\n", heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]);
214 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] == heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]){
216 if(memcmp(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1]) != 0){
219 fprintf(stderr, "False difference detected\n");
229 if(((heapinfo1[block_pointed1].type == 0) && (heapinfo2[block_pointed2].type != 0)) || ((heapinfo1[block_pointed1].type != 0) && (heapinfo2[block_pointed2].type == 0))){
230 fprintf(stderr, "Pointers on blocks with different types \n");
234 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> ((struct mdesc*)s_heap)->heapinfo[block_pointed1].type;
235 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> ((struct mdesc*)s_heap)->heapinfo[block_pointed2].type;
237 fprintf(stderr, "Fragments pointed : %d - %d\n", frag_pointed1, frag_pointed2);
239 addr_frag_pointed1 = (char*)addr_block_pointed1 + (frag_pointed1 * (int)pow(2, heapinfo1[block_pointed1].type));
240 addr_frag_pointed2 = (char*)addr_block_pointed2 + (frag_pointed2 * (int)pow(2, heapinfo2[block_pointed2].type));
242 fprintf(stderr, "Addr frag pointed : %p - %p\n", addr_frag_pointed1, addr_frag_pointed2);
244 fprintf(stderr, "Size used in fragments pointed : %d - %d\n", heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]);
246 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] == heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]){
248 if(memcmp(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1]) != 0){
251 fprintf(stderr, "False difference detected\n");
265 fprintf(stderr,"\nDifferent data in large block %zu (size = %zu (in blocks), busy_size = %zu (in bytes))\n", i, heapinfo1[i].busy_block.size, heapinfo1[i].busy_block.busy_size);
267 fprintf(stderr, "Hamming distance between blocks : %d\n", distance);
268 mmalloc_backtrace_block_display(heapinfo1, i);
269 mmalloc_backtrace_block_display(heapinfo2, i);
270 fprintf(stderr, "\n");
272 total_distance += distance;
279 if(heapinfo1[i].type > 0){ /* busy fragmented block */
281 if(heapinfo1[i].type != heapinfo2[i].type){
282 fprintf(stderr,"Different size of fragments in fragmented block %zu : %d - %d\n", i, heapinfo1[i].type, heapinfo2[i].type); fflush(NULL);
286 if(heapinfo1[i].busy_frag.nfree != heapinfo2[i].busy_frag.nfree){
287 fprintf(stderr,"Different free fragments in fragmented block %zu : %zu - %zu\n", i, heapinfo1[i].busy_frag.nfree, heapinfo2[i].busy_frag.nfree); fflush(NULL);
291 if(heapinfo1[i].busy_frag.first != heapinfo2[i].busy_frag.first){
292 fprintf(stderr,"Different first free fragment in fragmented block %zu : %zu - %zu\n", i, heapinfo1[i].busy_frag.first, heapinfo2[i].busy_frag.first); fflush(NULL);
296 frag_size = pow(2, heapinfo1[i].type);
298 for(j=0; j< (BLOCKSIZE/frag_size); j++){
300 if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
301 fprintf(stderr,"Different busy_size for fragment %zu in block %zu : %hu - %hu\n", j, i, heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]); fflush(NULL);
305 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
307 addr_frag1 = (char *)addr_block1 + (j * frag_size);
308 addr_frag2 = (char *)addr_block2 + (j * frag_size);
310 /* Hamming distance on different blocks */
313 for(k=0;k<heapinfo1[i].busy_frag.frag_size[j];k++){
315 if(memcmp(((char *)addr_frag1) + k, ((char *)addr_frag2) + k, 1) != 0){
317 fprintf(stderr, "Different byte (offset=%d) (%p - %p) in fragment %zu in block %zu\n", k, (char *)addr_frag1 + k, (char *)addr_frag2 + k, j, i); fflush(NULL);
319 pointer_align = (k / sizeof(void*)) * sizeof(void*);
320 address_pointed1 = *((void **)((char *)addr_frag1 + pointer_align));
321 address_pointed2 = *((void **)((char *)addr_frag2 + pointer_align));
323 fprintf(stderr, "Addresses pointed : %p - %p \n", address_pointed1, address_pointed2);
326 block_pointed1 = ((char*)address_pointed1 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
327 block_pointed2 = ((char*)address_pointed2 - (char*)((struct mdesc*)s_heap)->heapbase) / BLOCKSIZE + 1;
329 fprintf(stderr, "Blocks pointed : %d - %d\n", block_pointed1, block_pointed2);
331 if((char *) address_pointed1 < (char*)((struct mdesc*)s_heap)->heapbase || block_pointed1 > mdp1->heapsize || block_pointed1 < 1 || (char *) address_pointed2 < (char*)((struct mdesc*)s_heap)->heapbase || block_pointed2 > mdp2->heapsize || block_pointed2 < 1) {
332 fprintf(stderr, "Unknown pointer(s) ! \n");
338 addr_block_pointed1 = ((void*) (((ADDR2UINT((size_t)block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
339 addr_block_pointed2 = ((void*) (((ADDR2UINT((size_t)block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
341 fprintf(stderr, "Addr block pointed : %p - %p\n", addr_block_pointed1, addr_block_pointed2);
343 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){
345 if(heapinfo1[block_pointed1].type == 0){ // Large block
347 if(heapinfo1[block_pointed1].busy_block.busy_size == heapinfo2[block_pointed2].busy_block.busy_size){
349 if(memcmp(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size) != 0){
352 fprintf(stderr, "False difference detected\n");
359 }else{ // Fragmented block
361 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> ((struct mdesc*)s_heap)->heapinfo[block_pointed1].type;
362 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> ((struct mdesc*)s_heap)->heapinfo[block_pointed2].type;
364 fprintf(stderr, "Fragments pointed : %d - %d\n", frag_pointed1, frag_pointed2);
366 addr_frag_pointed1 = (char*)addr_block_pointed1 + (frag_pointed1 * (int)pow(2, heapinfo1[block_pointed1].type));
367 addr_frag_pointed2 = (char*)addr_block_pointed2 + (frag_pointed2 * (int)pow(2, heapinfo2[block_pointed2].type));
369 fprintf(stderr, "Addr frag pointed : %p - %p\n", addr_frag_pointed1, addr_frag_pointed2);
371 fprintf(stderr, "Size used in fragments pointed : %d - %d\n", heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]);
373 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] == heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]){
375 if(memcmp(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1]) != 0){
378 fprintf(stderr, "False difference detected\n");
388 if(((heapinfo1[block_pointed1].type == 0) && (heapinfo2[block_pointed2].type != 0)) || ((heapinfo1[block_pointed1].type != 0) && (heapinfo2[block_pointed2].type == 0))){
389 fprintf(stderr, "Pointers on blocks with different types \n");
393 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> ((struct mdesc*)s_heap)->heapinfo[block_pointed1].type;
394 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> ((struct mdesc*)s_heap)->heapinfo[block_pointed2].type;
396 fprintf(stderr, "Fragments pointed : %d - %d\n", frag_pointed1, frag_pointed2);
398 addr_frag_pointed1 = (char*)addr_block_pointed1 + (frag_pointed1 * (int)pow(2, heapinfo1[block_pointed1].type));
399 addr_frag_pointed2 = (char*)addr_block_pointed2 + (frag_pointed2 * (int)pow(2, heapinfo2[block_pointed2].type));
401 fprintf(stderr, "Addr frag pointed : %p - %p\n", addr_frag_pointed1, addr_frag_pointed2);
403 fprintf(stderr, "Size used in fragments pointed : %d - %d\n", heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]);
405 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] == heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]){
407 if(memcmp(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1]) != 0){
410 fprintf(stderr, "False difference detected\n");
424 fprintf(stderr,"\nDifferent data in fragment %zu (size = %zu, size used = %hu) in block %zu \n", j, frag_size, heapinfo1[i].busy_frag.frag_size[j], i);
425 fprintf(stderr, "Hamming distance between fragments : %d\n", distance);
426 mmalloc_backtrace_fragment_display(heapinfo1, i, j);
427 mmalloc_backtrace_fragment_display(heapinfo2, i, j);
428 fprintf(stderr, "\n");
430 total_distance += distance;
439 }else{ /* free block */
450 fprintf(stderr, "Hamming distance between heap regions : %d\n", total_distance);
456 /* void *get_end_addr_heap(void *heap){ */
458 /* FILE *fp; /\* File pointer to process's proc maps file *\/ */
459 /* char *line = NULL; /\* Temporal storage for each line that is readed *\/ */
460 /* ssize_t read; /\* Number of bytes readed *\/ */
461 /* size_t n = 0; /\* Amount of bytes to read by getline *\/ */
463 /* fp = fopen("/proc/self/maps", "r"); */
466 /* perror("fopen failed"); */
469 /* xbt_dynar_t lfields = NULL; */
470 /* xbt_dynar_t start_end = NULL; */
471 /* void *start_addr; */
472 /* void *end_addr; */
474 /* while ((read = getline(&line, &n, fp)) != -1) { */
476 /* xbt_str_trim(line, NULL); */
477 /* xbt_str_strip_spaces(line); */
478 /* lfields = xbt_str_split(line,NULL); */
480 /* start_end = xbt_str_split(xbt_dynar_get_as(lfields, 0, char*), "-"); */
481 /* start_addr = (void *) strtoul(xbt_dynar_get_as(start_end, 0, char*), NULL, 16); */
482 /* end_addr = (void *) strtoul(xbt_dynar_get_as(start_end, 1, char*), NULL, 16); */
484 /* if(start_addr == heap){ */
487 /* xbt_dynar_reset(lfields); */
488 /* xbt_free(lfields); */
489 /* xbt_dynar_reset(start_end); */
490 /* xbt_free(start_end); */
491 /* return end_addr; */
496 /* xbt_dynar_reset(lfields); */
497 /* xbt_free(lfields); */
498 /* xbt_dynar_reset(start_end); */
499 /* xbt_free(start_end); */