Logo AND Algorithmique Numérique Distribuée

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