Logo AND Algorithmique Numérique Distribuée

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