Logo AND Algorithmique Numérique Distribuée

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