Logo AND Algorithmique Numérique Distribuée

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