Logo AND Algorithmique Numérique Distribuée

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