Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / mc / mc_compare.cpp
1 /* Copyright (c) 2012-2015. 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 <cinttypes>
9
10 #include <utility>
11 #include <unordered_set>
12
13 #include <xbt/sysdep.h>
14
15 #include "src/internal_config.h"
16 #include "mc_object_info.h"
17 #include "mc_safety.h"
18 #include "mc_liveness.h"
19 #include "mc_private.h"
20 #include "mc_smx.h"
21 #include "mc_dwarf.hpp"
22
23 #include "src/mc/Frame.hpp"
24 #include "src/mc/ObjectInformation.hpp"
25 #include "src/mc/Variable.hpp"
26
27 #ifdef HAVE_SMPI
28 #include "src/smpi/private.h"
29 #endif
30
31 #include "xbt/mmalloc.h"
32 #include "src/xbt/mmalloc/mmprivate.h"
33
34 #include "src/xbt/probes.h"
35
36 using simgrid::mc::remote;
37
38 extern "C" {
39
40 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_compare, xbt,
41                                 "Logging specific to mc_compare in mc");
42
43 }
44
45 namespace simgrid {
46 namespace mc {
47
48 /** A hash which works with more stuff
49  *
50  *  It can hash pairs: the standard hash currently doesn't include this.
51  */
52 template<class X> struct hash : public std::hash<X> {};
53
54 template<class X, class Y>
55 struct hash<std::pair<X,Y>> {
56   std::size_t operator()(std::pair<X,Y>const& x) const
57   {
58     struct hash<X> h1;
59     struct hash<X> h2;
60     return h1(x.first) ^ h2(x.second);
61   }
62 };
63
64 struct ComparisonState {
65   std::unordered_set<std::pair<void*, void*>, hash<std::pair<void*, void*>>> compared_pointers;
66 };
67
68 }
69 }
70
71 using simgrid::mc::ComparisonState;
72
73 extern "C" {
74
75 /************************** Snapshot comparison *******************************/
76 /******************************************************************************/
77
78 static int compare_areas_with_type(ComparisonState& state,
79                                    int process_index,
80                                    void* real_area1, mc_snapshot_t snapshot1, mc_mem_region_t region1,
81                                    void* real_area2, mc_snapshot_t snapshot2, mc_mem_region_t region2,
82                                    simgrid::mc::Type* type, int pointer_level)
83 {
84   simgrid::mc::Process* process = &mc_model_checker->process();
85
86   simgrid::mc::Type* subtype;
87   simgrid::mc::Type* subsubtype;
88   int elm_size, i, res;
89
90   top:
91   switch (type->type) {
92   case DW_TAG_unspecified_type:
93     return 1;
94
95   case DW_TAG_base_type:
96   case DW_TAG_enumeration_type:
97   case DW_TAG_union_type:
98   {
99     return MC_snapshot_region_memcmp(
100       real_area1, region1, real_area2, region2,
101       type->byte_size) != 0;
102   }
103   case DW_TAG_typedef:
104   case DW_TAG_volatile_type:
105   case DW_TAG_const_type:
106     // Poor man's TCO:
107     type = type->subtype;
108     goto top;
109   case DW_TAG_array_type:
110     subtype = type->subtype;
111     switch (subtype->type) {
112     case DW_TAG_unspecified_type:
113       return 1;
114
115     case DW_TAG_base_type:
116     case DW_TAG_enumeration_type:
117     case DW_TAG_pointer_type:
118     case DW_TAG_reference_type:
119     case DW_TAG_rvalue_reference_type:
120     case DW_TAG_structure_type:
121     case DW_TAG_class_type:
122     case DW_TAG_union_type:
123       if (subtype->full_type)
124         subtype = subtype->full_type;
125       elm_size = subtype->byte_size;
126       break;
127     case DW_TAG_const_type:
128     case DW_TAG_typedef:
129     case DW_TAG_volatile_type:
130       subsubtype = subtype->subtype;
131       if (subsubtype->full_type)
132         subsubtype = subsubtype->full_type;
133       elm_size = subsubtype->byte_size;
134       break;
135     default:
136       return 0;
137       break;
138     }
139     for (i = 0; i < type->element_count; i++) {
140       size_t off = i * elm_size;
141       res = compare_areas_with_type(state, process_index,
142             (char*) real_area1 + off, snapshot1, region1,
143             (char*) real_area2 + off, snapshot2, region2,
144             type->subtype, pointer_level);
145       if (res == 1)
146         return res;
147     }
148     break;
149   case DW_TAG_pointer_type:
150   case DW_TAG_reference_type:
151   case DW_TAG_rvalue_reference_type:
152   {
153     void* addr_pointed1 = MC_region_read_pointer(region1, real_area1);
154     void* addr_pointed2 = MC_region_read_pointer(region2, real_area2);
155
156     if (type->subtype && type->subtype->type == DW_TAG_subroutine_type) {
157       return (addr_pointed1 != addr_pointed2);
158     } else {
159
160       if (addr_pointed1 == NULL && addr_pointed2 == NULL)
161         return 0;
162       if (addr_pointed1 == NULL || addr_pointed2 == NULL)
163         return 1;
164       if (!state.compared_pointers.insert(
165           std::make_pair(addr_pointed1, addr_pointed2)).second)
166         return 0;
167
168       pointer_level++;
169
170       // Some cases are not handled here:
171       // * the pointers lead to different areas (one to the heap, the other to the RW segment ...);
172       // * a pointer leads to the read-only segment of the current object;
173       // * a pointer lead to a different ELF object.
174
175       if (addr_pointed1 > process->heap_address
176           && addr_pointed1 < mc_snapshot_get_heap_end(snapshot1)) {
177         if (!
178             (addr_pointed2 > process->heap_address
179              && addr_pointed2 < mc_snapshot_get_heap_end(snapshot2)))
180           return 1;
181         // The pointers are both in the heap:
182         return compare_heap_area(process_index, addr_pointed1, addr_pointed2, snapshot1,
183                                  snapshot2, NULL, type->subtype, pointer_level);
184       }
185
186       // The pointers are both in the current object R/W segment:
187       else if (region1->contain(simgrid::mc::remote(addr_pointed1))) {
188         if (!region2->contain(simgrid::mc::remote(addr_pointed2)))
189           return 1;
190         if (!type->type_id)
191           return (addr_pointed1 != addr_pointed2);
192         else {
193           return compare_areas_with_type(state, process_index,
194                                          addr_pointed1, snapshot1, region1,
195                                          addr_pointed2, snapshot2, region2,
196                                          type->subtype, pointer_level);
197         }
198       }
199
200       // TODO, We do not handle very well the case where
201       // it belongs to a different (non-heap) region from the current one.
202
203       else {
204         return (addr_pointed1 != addr_pointed2);
205       }
206     }
207     break;
208   }
209   case DW_TAG_structure_type:
210   case DW_TAG_class_type:
211     for(simgrid::mc::Member& member : type->members) {
212       void *member1 = simgrid::dwarf::resolve_member(
213         real_area1, type, &member, snapshot1, process_index);
214       void *member2 = simgrid::dwarf::resolve_member(
215         real_area2, type, &member, snapshot2, process_index);
216       mc_mem_region_t subregion1 = mc_get_region_hinted(member1, snapshot1, process_index, region1);
217       mc_mem_region_t subregion2 = mc_get_region_hinted(member2, snapshot2, process_index, region2);
218       res =
219           compare_areas_with_type(state, process_index,
220                                   member1, snapshot1, subregion1,
221                                   member2, snapshot2, subregion2,
222                                   member.type, pointer_level);
223       if (res == 1)
224         return res;
225     }
226     break;
227   case DW_TAG_subroutine_type:
228     return -1;
229     break;
230   default:
231     XBT_VERB("Unknown case : %d", type->type);
232     break;
233   }
234
235   return 0;
236 }
237
238 static int compare_global_variables(simgrid::mc::ObjectInformation* object_info,
239                                     int process_index,
240                                     mc_mem_region_t r1,
241                                     mc_mem_region_t r2, mc_snapshot_t snapshot1,
242                                     mc_snapshot_t snapshot2)
243 {
244   xbt_assert(r1 && r2, "Missing region.");
245
246 #ifdef HAVE_SMPI
247   if (r1->storage_type() == simgrid::mc::StorageType::Privatized) {
248     xbt_assert(process_index >= 0);
249     if (r2->storage_type() != simgrid::mc::StorageType::Privatized) {
250       return 1;
251     }
252
253     size_t process_count = MC_smpi_process_count();
254     xbt_assert(process_count == r1->privatized_data().size()
255       && process_count == r2->privatized_data().size());
256
257     // Compare the global variables separately for each simulates process:
258     for (size_t process_index = 0; process_index < process_count; process_index++) {
259       int is_diff = compare_global_variables(object_info, process_index,
260         &r1->privatized_data()[process_index],
261         &r2->privatized_data()[process_index],
262         snapshot1, snapshot2);
263       if (is_diff) return 1;
264     }
265     return 0;
266   }
267 #else
268   xbt_assert(r1->storage_type() != simgrid::mc::StorageType::Privatized);
269 #endif
270   xbt_assert(r2->storage_type() != simgrid::mc::StorageType::Privatized);
271
272   ComparisonState state;
273
274   std::vector<simgrid::mc::Variable>& variables = object_info->global_variables;
275
276   for (simgrid::mc::Variable& current_var : variables) {
277
278     // If the variable is not in this object, skip it:
279     // We do not expect to find a pointer to something which is not reachable
280     // by the global variables.
281     if ((char *) current_var.address < (char *) object_info->start_rw
282         || (char *) current_var.address > (char *) object_info->end_rw)
283       continue;
284
285     simgrid::mc::Type* bvariable_type = current_var.type;
286     int res =
287         compare_areas_with_type(state, process_index,
288                                 (char *) current_var.address, snapshot1, r1,
289                                 (char *) current_var.address, snapshot2, r2,
290                                 bvariable_type, 0);
291     if (res == 1) {
292       XBT_TRACE3(mc, global_diff, -1, -1, current_var->name);
293       XBT_VERB("Global variable %s (%p) is different between snapshots",
294                current_var.name.c_str(),
295                (char *) current_var.address);
296       return 1;
297     }
298
299   }
300
301   return 0;
302
303 }
304
305 static int compare_local_variables(int process_index,
306                                    mc_snapshot_t snapshot1,
307                                    mc_snapshot_t snapshot2,
308                                    mc_snapshot_stack_t stack1,
309                                    mc_snapshot_stack_t stack2)
310 {
311   ComparisonState state;
312
313   if (stack1->local_variables.size() != stack2->local_variables.size()) {
314     XBT_VERB("Different number of local variables");
315     return 1;
316   } else {
317     unsigned int cursor = 0;
318     local_variable_t current_var1, current_var2;
319     int res;
320     while (cursor < stack1->local_variables.size()) {
321       current_var1 = &stack1->local_variables[cursor];
322       current_var2 = &stack1->local_variables[cursor];
323       if (current_var1->name != current_var2->name
324           || current_var1->subprogram != current_var1->subprogram
325           || current_var1->ip != current_var2->ip) {
326         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
327         XBT_VERB
328             ("Different name of variable (%s - %s) "
329              "or frame (%s - %s) or ip (%lu - %lu)",
330              current_var1->name.c_str(),
331              current_var2->name.c_str(),
332              current_var1->subprogram->name.c_str(),
333              current_var2->subprogram->name.c_str(),
334              current_var1->ip, current_var2->ip);
335         return 1;
336       }
337       // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
338
339         simgrid::mc::Type* subtype = current_var1->type;
340         res =
341             compare_areas_with_type(state, process_index,
342                                     current_var1->address, snapshot1, mc_get_snapshot_region(current_var1->address, snapshot1, process_index),
343                                     current_var2->address, snapshot2, mc_get_snapshot_region(current_var2->address, snapshot2, process_index),
344                                     subtype, 0);
345
346       if (res == 1) {
347         // TODO, fix current_varX->subprogram->name to include name if DW_TAG_inlined_subprogram
348         XBT_TRACE3(mc, local_diff, -1, -1, current_var1->name);
349         XBT_VERB
350             ("Local variable %s (%p - %p) in frame %s "
351              "is different between snapshots",
352              current_var1->name.c_str(),
353              current_var1->address,
354              current_var2->address,
355              current_var1->subprogram->name.c_str());
356         return res;
357       }
358       cursor++;
359     }
360     return 0;
361   }
362 }
363
364 int snapshot_compare(void *state1, void *state2)
365 {
366   simgrid::mc::Process* process = &mc_model_checker->process();
367
368   mc_snapshot_t s1, s2;
369   int num1, num2;
370
371   if (_sg_mc_liveness) {        /* Liveness MC */
372     s1 = ((mc_visited_pair_t) state1)->graph_state->system_state;
373     s2 = ((mc_visited_pair_t) state2)->graph_state->system_state;
374     num1 = ((mc_visited_pair_t) state1)->num;
375     num2 = ((mc_visited_pair_t) state2)->num;
376   }else if (_sg_mc_termination) { /* Non-progressive cycle MC */
377     s1 = ((mc_state_t) state1)->system_state;
378     s2 = ((mc_state_t) state2)->system_state;
379     num1 = ((mc_state_t) state1)->num;
380     num2 = ((mc_state_t) state2)->num;
381   } else {                      /* Safety or comm determinism MC */
382     s1 = ((mc_visited_state_t) state1)->system_state;
383     s2 = ((mc_visited_state_t) state2)->system_state;
384     num1 = ((mc_visited_state_t) state1)->num;
385     num2 = ((mc_visited_state_t) state2)->num;
386   }
387
388   int errors = 0;
389   int res_init;
390
391   int hash_result = 0;
392   if (_sg_mc_hash) {
393     hash_result = (s1->hash != s2->hash);
394     if (hash_result) {
395       XBT_TRACE2(mc, hash_diff, num1, num2);
396       XBT_VERB("(%d - %d) Different hash : 0x%" PRIx64 "--0x%" PRIx64, num1,
397                num2, s1->hash, s2->hash);
398 #ifndef MC_DEBUG
399       return 1;
400 #endif
401     } else {
402       XBT_VERB("(%d - %d) Same hash : 0x%" PRIx64, num1, num2, s1->hash);
403     }
404   }
405
406   /* Compare enabled processes */
407   if (s1->enabled_processes != s2->enabled_processes) {
408       //XBT_TRACE3(mc, state_diff, num1, num2, "Different enabled processes");
409       XBT_VERB("(%d - %d) Different enabled processes", num1, num2);
410       // return 1; ??
411   }
412
413   unsigned long i = 0;
414   size_t size_used1, size_used2;
415   int is_diff = 0;
416
417   /* Compare size of stacks */
418   while (i < s1->stacks.size()) {
419     size_used1 = s1->stack_sizes[i];
420     size_used2 = s2->stack_sizes[i];
421     if (size_used1 != size_used2) {
422 #ifdef MC_DEBUG
423       XBT_DEBUG("(%d - %d) Different size used in stacks : %zu - %zu", num1,
424                 num2, size_used1, size_used2);
425       errors++;
426       is_diff = 1;
427 #else
428 #ifdef MC_VERBOSE
429       XBT_VERB("(%d - %d) Different size used in stacks : %zu - %zu", num1,
430                num2, size_used1, size_used2);
431 #endif
432       XBT_TRACE3(mc, state_diff, num1, num2, "Different stack size");
433       return 1;
434 #endif
435     }
436     i++;
437   }
438
439   /* Init heap information used in heap comparison algorithm */
440   xbt_mheap_t heap1 = (xbt_mheap_t)s1->read_bytes(
441     alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
442     remote(process->heap_address),
443     simgrid::mc::ProcessIndexMissing, simgrid::mc::AddressSpace::Lazy);
444   xbt_mheap_t heap2 = (xbt_mheap_t)s2->read_bytes(
445     alloca(sizeof(struct mdesc)), sizeof(struct mdesc),
446     remote(process->heap_address),
447     simgrid::mc::ProcessIndexMissing, simgrid::mc::AddressSpace::Lazy);
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_TRACE3(mc, state_diff, num1, num2, "Different heap information");
456     XBT_VERB("(%d - %d) Different heap information", num1, num2);
457 #endif
458
459     return 1;
460 #endif
461   }
462
463   /* Stacks comparison */
464   unsigned cursor = 0;
465   int diff_local = 0;
466 #ifdef MC_DEBUG
467   is_diff = 0;
468 #endif
469   mc_snapshot_stack_t stack1, stack2;
470   while (cursor < s1->stacks.size()) {
471     stack1 = &s1->stacks[cursor];
472     stack2 = &s1->stacks[cursor];
473
474     if (stack1->process_index != stack2->process_index) {
475       diff_local = 1;
476       XBT_DEBUG("(%d - %d) Stacks with different process index (%i vs %i)", num1, num2,
477         stack1->process_index, stack2->process_index);
478     }
479     else diff_local =
480         compare_local_variables(stack1->process_index, s1, s2, stack1, stack2);
481     if (diff_local > 0) {
482       XBT_TRACE3(mc, state_diff, num1, num2, "Different local variables");
483 #ifdef MC_DEBUG
484       XBT_DEBUG("(%d - %d) Different local variables between stacks %d", num1,
485                 num2, cursor + 1);
486       errors++;
487       is_diff = 1;
488 #else
489
490 #ifdef MC_VERBOSE
491       XBT_VERB("(%d - %d) Different local variables between stacks %d", num1,
492                num2, cursor + 1);
493 #endif
494
495       reset_heap_information();
496
497       return 1;
498 #endif
499     }
500     cursor++;
501   }
502
503   size_t regions_count = s1->snapshot_regions.size();
504   // TODO, raise a difference instead?
505   xbt_assert(regions_count == s2->snapshot_regions.size());
506
507   for (size_t k = 0; k != regions_count; ++k) {
508     mc_mem_region_t region1 = s1->snapshot_regions[k].get();
509     mc_mem_region_t region2 = s2->snapshot_regions[k].get();
510
511     // Preconditions:
512     if (region1->region_type() != simgrid::mc::RegionType::Data)
513       continue;
514
515     xbt_assert(region1->region_type() == region2->region_type());
516     xbt_assert(region1->object_info() == region2->object_info());
517     xbt_assert(region1->object_info());
518
519     std::string const& name = region1->object_info()->file_name;
520
521     /* Compare global variables */
522     is_diff =
523       compare_global_variables(region1->object_info(  ), simgrid::mc::AddressSpace::Normal,
524         region1, region2,
525         s1, s2);
526
527     if (is_diff != 0) {
528       XBT_TRACE3(mc, state_diff, num1, num2, "Different global variables");
529 #ifdef MC_DEBUG
530       XBT_DEBUG("(%d - %d) Different global variables in %s",
531         num1, num2, name.c_str());
532       errors++;
533 #else
534 #ifdef MC_VERBOSE
535       XBT_VERB("(%d - %d) Different global variables in %s",
536         num1, num2, name.c_str());
537 #endif
538
539       return 1;
540 #endif
541     }
542   }
543
544   /* Compare heap */
545   if (mmalloc_compare_heap(s1, s2) > 0) {
546     XBT_TRACE3(mc, state_diff, num1, num2, "Different heap");
547
548 #ifdef MC_DEBUG
549     XBT_DEBUG("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
550     errors++;
551 #else
552
553 #ifdef MC_VERBOSE
554     XBT_VERB("(%d - %d) Different heap (mmalloc_compare)", num1, num2);
555 #endif
556
557     return 1;
558 #endif
559   }
560
561   reset_heap_information();
562
563 #ifdef MC_VERBOSE
564   if (errors || hash_result)
565     XBT_VERB("(%d - %d) Difference found", num1, num2);
566   else
567     XBT_VERB("(%d - %d) No difference found", num1, num2);
568 #endif
569
570 #if defined(MC_DEBUG) && defined(MC_VERBOSE)
571   if (_sg_mc_hash) {
572     // * false positive SHOULD be avoided.
573     // * There MUST not be any false negative.
574
575     XBT_VERB("(%d - %d) State equality hash test is %s %s", num1, num2,
576              (hash_result != 0) == (errors != 0) ? "true" : "false",
577              !hash_result ? "positive" : "negative");
578   }
579 #endif
580
581   return errors > 0 || hash_result;
582
583 }
584
585 }