Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mmalloc] Add new block type for heapinfo blocks
[simgrid.git] / src / mc / mc_compare.cpp
1 /* Copyright (c) 2012-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <inttypes.h>
8 #include <boost/unordered_set.hpp>
9
10 #include "mc_private.h"
11
12 #include "xbt/mmalloc.h"
13 #include "xbt/mmalloc/mmprivate.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, mc,
16                                 "Logging specific to mc_compare");
17
18 typedef struct s_pointers_pair {
19   void *p1;
20   void *p2;
21   bool operator==(s_pointers_pair const& x) const {
22     return this->p1 == x.p1 && this->p2 == x.p2;
23   }
24   bool operator<(s_pointers_pair const& x) const {
25     return this->p1 < x.p1 || (this->p1 == x.p1 && this->p2 < x.p2);
26   }
27 } s_pointers_pair_t, *pointers_pair_t;
28
29 namespace boost {
30   template<>
31   struct hash<s_pointers_pair> {
32     typedef uintptr_t result_type;
33     result_type operator()(s_pointers_pair const& x) const {
34       return (result_type) x.p1 ^
35         ((result_type) x.p2 << 8 | (result_type) x.p2 >> (8*sizeof(uintptr_t) - 8));
36     }
37   };
38 }
39
40 struct mc_compare_state {
41   boost::unordered_set<s_pointers_pair> compared_pointers;
42 };
43
44 extern "C" {
45
46 /************************** Free functions ****************************/
47 /********************************************************************/
48
49 static void stack_region_free(stack_region_t s)
50 {
51   if (s) {
52     xbt_free(s->process_name);
53     xbt_free(s);
54   }
55 }
56
57 static void stack_region_free_voidp(void *s)
58 {
59   stack_region_free((stack_region_t) * (void **) s);
60 }
61
62 static void pointers_pair_free(pointers_pair_t p)
63 {
64   xbt_free(p);
65 }
66
67 static void pointers_pair_free_voidp(void *p)
68 {
69   pointers_pair_free((pointers_pair_t) * (void **) p);
70 }
71
72 /************************** Snapshot comparison *******************************/
73 /******************************************************************************/
74
75 /** \brief Try to add a pair a compared pointers to the set of compared pointers
76  *
77  *  \result !=0 if the pointers were added (they were not in the set),
78  *      0 otherwise (they were already in the set)
79  */
80 static int add_compared_pointers(mc_compare_state& state, void *p1, void *p2)
81 {
82   s_pointers_pair_t new_pair;
83   new_pair.p1 = p1;
84   new_pair.p2 = p2;
85   return state.compared_pointers.insert(new_pair).second ? 1 : 0;
86 }
87
88 static int compare_areas_with_type(struct mc_compare_state& state,
89                                    void* real_area1, mc_snapshot_t snapshot1, mc_mem_region_t region1,
90                                    void* real_area2, mc_snapshot_t snapshot2, mc_mem_region_t region2,
91                                    dw_type_t type, int pointer_level)
92 {
93   unsigned int cursor = 0;
94   dw_type_t member, subtype, subsubtype;
95   int elm_size, i, res;
96
97   top:
98   switch (type->type) {
99   case DW_TAG_unspecified_type:
100     return 1;
101
102   case DW_TAG_base_type:
103   case DW_TAG_enumeration_type:
104   case DW_TAG_union_type:
105   {
106     return mc_snapshot_region_memcmp(
107       real_area1, region1, real_area2, region2,
108       type->byte_size) != 0;
109   }
110   case DW_TAG_typedef:
111   case DW_TAG_volatile_type:
112   case DW_TAG_const_type:
113     // Poor man's TCO:
114     type = type->subtype;
115     goto top;
116   case DW_TAG_array_type:
117     subtype = type->subtype;
118     switch (subtype->type) {
119     case DW_TAG_unspecified_type:
120       return 1;
121
122     case DW_TAG_base_type:
123     case DW_TAG_enumeration_type:
124     case DW_TAG_pointer_type:
125     case DW_TAG_reference_type:
126     case DW_TAG_rvalue_reference_type:
127     case DW_TAG_structure_type:
128     case DW_TAG_class_type:
129     case DW_TAG_union_type:
130       if (subtype->full_type)
131         subtype = subtype->full_type;
132       elm_size = subtype->byte_size;
133       break;
134     case DW_TAG_const_type:
135     case DW_TAG_typedef:
136     case DW_TAG_volatile_type:
137       subsubtype = subtype->subtype;
138       if (subsubtype->full_type)
139         subsubtype = subsubtype->full_type;
140       elm_size = subsubtype->byte_size;
141       break;
142     default:
143       return 0;
144       break;
145     }
146     for (i = 0; i < type->element_count; i++) {
147       size_t off = i * elm_size;
148       res = compare_areas_with_type(state,
149             (char*) real_area1 + off, snapshot1, region1,
150             (char*) real_area2 + off, snapshot2, region2,
151             type->subtype, pointer_level);
152       if (res == 1)
153         return res;
154     }
155     break;
156   case DW_TAG_pointer_type:
157   case DW_TAG_reference_type:
158   case DW_TAG_rvalue_reference_type:
159   {
160     void* addr_pointed1 = mc_snapshot_read_pointer_region(real_area1, region1);
161     void* addr_pointed2 = mc_snapshot_read_pointer_region(real_area2, region2);
162
163     if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
164       return (addr_pointed1 != addr_pointed2);
165     } else {
166
167       if (addr_pointed1 == NULL && addr_pointed2 == NULL)
168         return 0;
169       if (!add_compared_pointers(state, addr_pointed1, addr_pointed2))
170         return 0;
171
172       pointer_level++;
173
174       // Some cases are not handled here:
175       // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
176       // * a pointer leads to the read-only segment of the current object;
177       // * a pointer lead to a different ELF object.
178
179       if (addr_pointed1 > std_heap
180           && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
181         if (!
182             (addr_pointed2 > std_heap
183              && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
184           return 1;
185         // The pointers are both in the heap:
186         return compare_heap_area(addr_pointed1, addr_pointed2, snapshot1,
187                                  snapshot2, NULL, type->subtype, pointer_level);
188       }
189       // The pointers are both in the current object R/W segment:
190       else if (addr_pointed1 > region1->start_addr
191                && (char *) addr_pointed1 <= (char *) region1->start_addr + region1->size) {
192         if (!
193             (addr_pointed2 > region2->start_addr
194              && (char *) addr_pointed2 <= (char *) region2->start_addr + region2->size))
195           return 1;
196         if (type->dw_type_id == NULL)
197           return (addr_pointed1 != addr_pointed2);
198         else {
199           return compare_areas_with_type(state,
200                                          addr_pointed1, snapshot1, region1,
201                                          addr_pointed2, snapshot2, region2,
202                                          type->subtype, pointer_level);
203         }
204       }
205
206       else {
207         return (addr_pointed1 != addr_pointed2);
208       }
209     }
210     break;
211   }
212   case DW_TAG_structure_type:
213   case DW_TAG_class_type:
214     xbt_dynar_foreach(type->members, cursor, member) {
215       void *member1 =
216         mc_member_resolve(real_area1, type, member, snapshot1);
217       void *member2 =
218         mc_member_resolve(real_area2, type, member, snapshot2);
219       mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, region1);
220       mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, region2);
221       res =
222           compare_areas_with_type(state,
223                                   member1, snapshot1, subregion1,
224                                   member2, snapshot2, subregion2,
225                                   member->subtype, pointer_level);
226       if (res == 1)
227         return res;
228     }
229     break;
230   case DW_TAG_subroutine_type:
231     return -1;
232     break;
233   default:
234     XBT_VERB("Unknown case : %d", type->type);
235     break;
236   }
237
238   return 0;
239 }
240
241 static int compare_global_variables(int region_type, mc_mem_region_t r1,
242                                     mc_mem_region_t r2, mc_snapshot_t snapshot1,
243                                     mc_snapshot_t snapshot2)
244 {
245   xbt_assert(r1 && r2,
246     "Missing region. Did you enable SMPI privatisation? It is not compatible with state comparison.");
247   struct mc_compare_state state;
248
249   xbt_dynar_t variables;
250   int res;
251   unsigned int cursor = 0;
252   dw_variable_t current_var;
253
254   mc_object_info_t object_info = NULL;
255   if (region_type == 2) {
256     object_info = mc_binary_info;
257   } else {
258     object_info = mc_libsimgrid_info;
259   }
260   variables = object_info->global_variables;
261
262   xbt_dynar_foreach(variables, cursor, current_var) {
263
264     // If the variable is not in this object, skip it:
265     // We do not expect to find a pointer to something which is not reachable
266     // by the global variables.
267     if ((char *) current_var->address < (char *) object_info->start_rw
268         || (char *) current_var->address > (char *) object_info->end_rw)
269       continue;
270
271     dw_type_t bvariable_type = current_var->type;
272     res =
273         compare_areas_with_type(state,
274                                 (char *) current_var->address, snapshot1, r1,
275                                 (char *) current_var->address, snapshot2, r2,
276                                 bvariable_type, 0);
277     if (res == 1) {
278       XBT_VERB("Global variable %s (%p) is different between snapshots",
279                current_var->name, (char *) current_var->address);
280       return 1;
281     }
282
283   }
284
285   return 0;
286
287 }
288
289 static int compare_local_variables(mc_snapshot_t snapshot1,
290                                    mc_snapshot_t snapshot2,
291                                    mc_snapshot_stack_t stack1,
292                                    mc_snapshot_stack_t stack2)
293 {
294   struct mc_compare_state state;
295
296   if (xbt_dynar_length(stack1->local_variables) !=
297       xbt_dynar_length(stack2->local_variables)) {
298     XBT_VERB("Different number of local variables");
299     return 1;
300   } else {
301     unsigned int cursor = 0;
302     local_variable_t current_var1, current_var2;
303     int res;
304     while (cursor < xbt_dynar_length(stack1->local_variables)) {
305       current_var1 =
306           (local_variable_t) xbt_dynar_get_as(stack1->local_variables, cursor,
307                                               local_variable_t);
308       current_var2 =
309           (local_variable_t) xbt_dynar_get_as(stack2->local_variables, cursor,
310                                               local_variable_t);
311       if (strcmp(current_var1->name, current_var2->name) != 0
312           || current_var1->subprogram != current_var1->subprogram
313           || current_var1->ip != current_var2->ip) {
314         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
315         XBT_VERB
316             ("Different name of variable (%s - %s) or frame (%s - %s) or ip (%lu - %lu)",
317              current_var1->name, current_var2->name,
318              current_var1->subprogram->name, current_var2->subprogram->name,
319              current_var1->ip, current_var2->ip);
320         return 1;
321       }
322       // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
323
324         dw_type_t subtype = current_var1->type;
325         res =
326             compare_areas_with_type(state,
327                                     current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1),
328                                     current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2),
329                                     subtype, 0);
330
331       if (res == 1) {
332         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
333         XBT_VERB
334             ("Local variable %s (%p - %p) in frame %s  is different between snapshots",
335              current_var1->name, current_var1->address, current_var2->address,
336              current_var1->subprogram->name);
337         return res;
338       }
339       cursor++;
340     }
341     return 0;
342   }
343 }
344
345 int snapshot_compare(void *state1, void *state2)
346 {
347
348   mc_snapshot_t s1, s2;
349   int num1, num2;
350
351   if (_sg_mc_liveness) {        /* Liveness MC */
352     s1 = ((mc_visited_pair_t) state1)->graph_state->system_state;
353     s2 = ((mc_visited_pair_t) state2)->graph_state->system_state;
354     num1 = ((mc_visited_pair_t) state1)->num;
355     num2 = ((mc_visited_pair_t) state2)->num;
356   } else {                      /* Safety or comm determinism MC */
357     s1 = ((mc_visited_state_t) state1)->system_state;
358     s2 = ((mc_visited_state_t) state2)->system_state;
359     num1 = ((mc_visited_state_t) state1)->num;
360     num2 = ((mc_visited_state_t) state2)->num;
361   }
362
363   int errors = 0;
364   int res_init;
365
366   xbt_os_timer_t global_timer = xbt_os_timer_new();
367   xbt_os_timer_t timer = xbt_os_timer_new();
368
369   xbt_os_walltimer_start(global_timer);
370
371 #ifdef MC_DEBUG
372   xbt_os_walltimer_start(timer);
373 #endif
374
375   int hash_result = 0;
376   if (_sg_mc_hash) {
377     hash_result = (s1->hash != s2->hash);
378     if (hash_result) {
379       XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
380                num2, s1->hash, s2->hash);
381 #ifndef MC_DEBUG
382       return 1;
383 #endif
384     } else {
385       XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
386     }
387   }
388
389   /* Compare enabled processes */
390   unsigned int cursor;
391   int pid;
392   xbt_dynar_foreach(s1->enabled_processes, cursor, pid){
393     if(!xbt_dynar_member(s2->enabled_processes, &pid))
394       XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
395   }
396
397   unsigned long i = 0;
398   size_t size_used1, size_used2;
399   int is_diff = 0;
400
401   /* Compare size of stacks */
402   while (i < xbt_dynar_length(s1->stacks)) {
403     size_used1 = s1->stack_sizes[i];
404     size_used2 = s2->stack_sizes[i];
405     if (size_used1 != size_used2) {
406 #ifdef MC_DEBUG
407       if (is_diff == 0) {
408         xbt_os_walltimer_stop(timer);
409         mc_comp_times->stacks_sizes_comparison_time =
410             xbt_os_timer_elapsed(timer);
411       }
412       XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
413                 num2, size_used1, size_used2);
414       errors++;
415       is_diff = 1;
416 #else
417 #ifdef MC_VERBOSE
418       XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
419                num2, size_used1, size_used2);
420 #endif
421
422       xbt_os_walltimer_stop(timer);
423       xbt_os_timer_free(timer);
424       xbt_os_walltimer_stop(global_timer);
425       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
426       xbt_os_timer_free(global_timer);
427
428       return 1;
429 #endif
430     }
431     i++;
432   }
433
434 #ifdef MC_DEBUG
435   if (is_diff == 0)
436     xbt_os_walltimer_stop(timer);
437   xbt_os_walltimer_start(timer);
438 #endif
439
440   /* Init heap information used in heap comparison algorithm */
441   xbt_mheap_t heap1 = (xbt_mheap_t) mc_snapshot_read(std_heap, s1,
442     alloca(sizeof(struct mdesc)), sizeof(struct mdesc));
443   xbt_mheap_t heap2 = (xbt_mheap_t) mc_snapshot_read(std_heap, s2,
444     alloca(sizeof(struct mdesc)), sizeof(struct mdesc));
445   res_init = init_heap_information(heap1, heap2, s1->to_ignore, s2->to_ignore);
446   if (res_init == -1) {
447 #ifdef MC_DEBUG
448     XBT_DEBUG("(%d - %d) Different heap information", num1, num2);
449     errors++;
450 #else
451 #ifdef MC_VERBOSE
452     XBT_VERB("(%d - %d) Different heap information", num1, num2);
453 #endif
454
455     xbt_os_walltimer_stop(global_timer);
456     mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
457     xbt_os_timer_free(global_timer);
458
459     return 1;
460 #endif
461   }
462 #ifdef MC_DEBUG
463   xbt_os_walltimer_start(timer);
464 #endif
465
466   /* Stacks comparison */
467   cursor = 0;
468   int diff_local = 0;
469 #ifdef MC_DEBUG
470   is_diff = 0;
471 #endif
472   mc_snapshot_stack_t stack1, stack2;
473
474   while (cursor < xbt_dynar_length(s1->stacks)) {
475     stack1 =
476         (mc_snapshot_stack_t) xbt_dynar_get_as(s1->stacks, cursor,
477                                                mc_snapshot_stack_t);
478     stack2 =
479         (mc_snapshot_stack_t) xbt_dynar_get_as(s2->stacks, cursor,
480                                                mc_snapshot_stack_t);
481     diff_local =
482         compare_local_variables(s1, s2, stack1, stack2);
483     if (diff_local > 0) {
484 #ifdef MC_DEBUG
485       if (is_diff == 0) {
486         xbt_os_walltimer_stop(timer);
487         mc_comp_times->stacks_comparison_time = xbt_os_timer_elapsed(timer);
488       }
489       XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
490                 num2, cursor + 1);
491       errors++;
492       is_diff = 1;
493 #else
494
495 #ifdef MC_VERBOSE
496       XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
497                num2, cursor + 1);
498 #endif
499
500       reset_heap_information();
501       xbt_os_walltimer_stop(timer);
502       xbt_os_timer_free(timer);
503       xbt_os_walltimer_stop(global_timer);
504       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
505       xbt_os_timer_free(global_timer);
506
507       return 1;
508 #endif
509     }
510     cursor++;
511   }
512
513
514
515   const char *names[3] = { "?", "libsimgrid", "binary" };
516 #ifdef MC_DEBUG
517   double *times[3] = {
518     NULL,
519     &mc_comp_times->libsimgrid_global_variables_comparison_time,
520     &mc_comp_times->binary_global_variables_comparison_time
521   };
522 #endif
523
524   int k = 0;
525   for (k = 2; k != 0; --k) {
526 #ifdef MC_DEBUG
527     if (is_diff == 0)
528       xbt_os_walltimer_stop(timer);
529     xbt_os_walltimer_start(timer);
530 #endif
531
532     /* Compare global variables */
533     is_diff =
534         compare_global_variables(k, s1->regions[k], s2->regions[k], s1, s2);
535     if (is_diff != 0) {
536 #ifdef MC_DEBUG
537       xbt_os_walltimer_stop(timer);
538       *times[k] = xbt_os_timer_elapsed(timer);
539       XBT_DEBUG("(%d - %d) Different global variables in %s", num1, num2,
540                 names[k]);
541       errors++;
542 #else
543 #ifdef MC_VERBOSE
544       XBT_VERB("(%d - %d) Different global variables in %s", num1, num2,
545                names[k]);
546 #endif
547
548       reset_heap_information();
549       xbt_os_walltimer_stop(timer);
550       xbt_os_timer_free(timer);
551       xbt_os_walltimer_stop(global_timer);
552       mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
553       xbt_os_timer_free(global_timer);
554
555       return 1;
556 #endif
557     }
558   }
559
560 #ifdef MC_DEBUG
561   xbt_os_walltimer_start(timer);
562 #endif
563
564   /* Compare heap */
565   if (mmalloc_compare_heap(s1, s2) > 0) {
566
567 #ifdef MC_DEBUG
568     xbt_os_walltimer_stop(timer);
569     mc_comp_times->heap_comparison_time = xbt_os_timer_elapsed(timer);
570     XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
571     errors++;
572 #else
573
574 #ifdef MC_VERBOSE
575     XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
576 #endif
577
578     reset_heap_information();
579     xbt_os_walltimer_stop(timer);
580     xbt_os_timer_free(timer);
581     xbt_os_walltimer_stop(global_timer);
582     mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
583     xbt_os_timer_free(global_timer);
584
585     return 1;
586 #endif
587   } else {
588 #ifdef MC_DEBUG
589     xbt_os_walltimer_stop(timer);
590 #endif
591   }
592
593   reset_heap_information();
594
595   xbt_os_walltimer_stop(timer);
596   xbt_os_timer_free(timer);
597
598 #ifdef MC_VERBOSE
599   xbt_os_walltimer_stop(global_timer);
600   mc_snapshot_comparison_time = xbt_os_timer_elapsed(global_timer);
601 #endif
602
603   xbt_os_timer_free(global_timer);
604
605 #ifdef MC_DEBUG
606   print_comparison_times();
607 #endif
608
609 #ifdef MC_VERBOSE
610   if (errors || hash_result)
611     XBT_VERB("(%d - %d) Difference found", num1, num2);
612   else
613     XBT_VERB("(%d - %d) No difference found", num1, num2);
614 #endif
615
616 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
617   if (_sg_mc_hash) {
618     // * false positive SHOULD be avoided.
619     // * There MUST not be any false negative.
620
621     XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
622              (hash_result != 0) == (errors != 0) ? "true" : "false",
623              !hash_result ? "positive" : "negative");
624   }
625 #endif
626
627   return errors > 0 || hash_result;
628
629 }
630
631 /***************************** Statistics *****************************/
632 /*******************************************************************/
633
634 void print_comparison_times()
635 {
636   XBT_DEBUG("*** Comparison times ***");
637   XBT_DEBUG("- Nb processes : %f", mc_comp_times->nb_processes_comparison_time);
638   XBT_DEBUG("- Nb bytes used : %f", mc_comp_times->bytes_used_comparison_time);
639   XBT_DEBUG("- Stacks sizes : %f", mc_comp_times->stacks_sizes_comparison_time);
640   XBT_DEBUG("- Binary global variables : %f",
641             mc_comp_times->binary_global_variables_comparison_time);
642   XBT_DEBUG("- Libsimgrid global variables : %f",
643             mc_comp_times->libsimgrid_global_variables_comparison_time);
644   XBT_DEBUG("- Heap : %f", mc_comp_times->heap_comparison_time);
645   XBT_DEBUG("- Stacks : %f", mc_comp_times->stacks_comparison_time);
646 }
647
648 /**************************** MC snapshot compare simcall **************************/
649 /***********************************************************************************/
650
651 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall,
652                                    mc_snapshot_t s1, mc_snapshot_t s2)
653 {
654   return snapshot_compare(s1, s2);
655 }
656
657 int MC_compare_snapshots(void *s1, void *s2)
658 {
659
660   return simcall_mc_compare_snapshots(s1, s2);
661
662 }
663
664 }