Logo AND Algorithmique Numérique Distribuée

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