Logo AND Algorithmique Numérique Distribuée

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