Logo AND Algorithmique Numérique Distribuée

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