Logo AND Algorithmique Numérique Distribuée

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