Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
35d07cbb368066f91c447dcbcf89b5b438ccb598
[simgrid.git] / src / mc / sosp / mc_snapshot.hpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_SNAPSHOT_HPP
7 #define SIMGRID_MC_SNAPSHOT_HPP
8
9 #include "src/mc/ModelChecker.hpp"
10 #include "src/mc/inspect/mc_unw.hpp"
11 #include "src/mc/remote/RemoteClient.hpp"
12 #include "src/mc/sosp/RegionSnapshot.hpp"
13
14 // ***** Snapshot region
15
16 static XBT_ALWAYS_INLINE void* mc_translate_address_region_chunked(uintptr_t addr, simgrid::mc::RegionSnapshot* region)
17 {
18   auto split                = simgrid::mc::mmu::split(addr - region->start().address());
19   auto pageno               = split.first;
20   auto offset               = split.second;
21   const void* snapshot_page = region->page_data().page(pageno);
22   return (char*)snapshot_page + offset;
23 }
24
25 static XBT_ALWAYS_INLINE void* mc_translate_address_region(uintptr_t addr, simgrid::mc::RegionSnapshot* region,
26                                                            int process_index)
27 {
28   switch (region->storage_type()) {
29     case simgrid::mc::StorageType::Flat: {
30       uintptr_t offset = (uintptr_t)addr - (uintptr_t)region->start().address();
31       return (void*)((uintptr_t)region->flat_data().get() + offset);
32     }
33     case simgrid::mc::StorageType::Chunked:
34       return mc_translate_address_region_chunked(addr, region);
35     case simgrid::mc::StorageType::Privatized: {
36       xbt_assert(process_index >= 0, "Missing process index for privatized region");
37       xbt_assert((size_t)process_index < region->privatized_data().size(), "Out of range process index");
38       simgrid::mc::RegionSnapshot& subregion = region->privatized_data()[process_index];
39       return mc_translate_address_region(addr, &subregion, process_index);
40     }
41     default: // includes StorageType::NoData
42       xbt_die("Storage type not supported");
43   }
44 }
45
46 // ***** MC Snapshot
47
48 /** Ignored data
49  *
50  *  Some parts of the snapshot are ignored by zeroing them out: the real
51  *  values is stored here.
52  * */
53 struct s_mc_snapshot_ignored_data_t {
54   void* start;
55   std::vector<char> data;
56 };
57
58 /** Information about a given stack frame */
59 struct s_mc_stack_frame_t {
60   /** Instruction pointer */
61   unw_word_t ip;
62   /** Stack pointer */
63   unw_word_t sp;
64   unw_word_t frame_base;
65   simgrid::mc::Frame* frame;
66   std::string frame_name;
67   unw_cursor_t unw_cursor;
68 };
69 typedef s_mc_stack_frame_t* mc_stack_frame_t;
70
71 struct s_local_variable_t {
72   simgrid::mc::Frame* subprogram;
73   unsigned long ip;
74   std::string name;
75   simgrid::mc::Type* type;
76   void* address;
77   int region;
78 };
79 typedef s_local_variable_t* local_variable_t;
80
81 struct XBT_PRIVATE s_mc_snapshot_stack_t {
82   std::vector<s_local_variable_t> local_variables;
83   simgrid::mc::UnwindContext context;
84   std::vector<s_mc_stack_frame_t> stack_frames;
85   int process_index;
86 };
87 typedef s_mc_snapshot_stack_t* mc_snapshot_stack_t;
88
89 namespace simgrid {
90 namespace mc {
91
92 class XBT_PRIVATE Snapshot final : public AddressSpace {
93 public:
94   Snapshot(RemoteClient* process, int num_state);
95   ~Snapshot() = default;
96
97   /* Initialization */
98   void add_region(RegionType type, ObjectInformation* object_info, void* start_addr, void* permanent_addr,
99                   std::size_t size);
100
101   /* Regular use */
102   const void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, int process_index = ProcessIndexAny,
103                          ReadOptions options = ReadOptions::none()) const override;
104   RegionSnapshot* get_region(const void* addr, int process_index) const;
105   RegionSnapshot* get_region(const void* addr, int process_index, RegionSnapshot* hinted_region) const;
106
107   // To be private
108   int num_state_;
109   std::size_t heap_bytes_used_;
110   std::vector<std::unique_ptr<RegionSnapshot>> snapshot_regions_;
111   std::set<pid_t> enabled_processes_;
112   int privatization_index_;
113   std::vector<std::size_t> stack_sizes_;
114   std::vector<s_mc_snapshot_stack_t> stacks_;
115   std::vector<simgrid::mc::IgnoredHeapRegion> to_ignore_;
116   std::uint64_t hash_ = 0;
117   std::vector<s_mc_snapshot_ignored_data_t> ignored_data_;
118 };
119 } // namespace mc
120 } // namespace simgrid
121
122 static const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot);
123
124 namespace simgrid {
125 namespace mc {
126
127 XBT_PRIVATE std::shared_ptr<Snapshot> take_snapshot(int num_state);
128 XBT_PRIVATE void restore_snapshot(std::shared_ptr<Snapshot> snapshot);
129 } // namespace mc
130 } // namespace simgrid
131
132 const void* MC_region_read_fragmented(simgrid::mc::RegionSnapshot* region, void* target, const void* addr,
133                                       std::size_t size);
134
135 int MC_snapshot_region_memcmp(const void* addr1, simgrid::mc::RegionSnapshot* region1, const void* addr2,
136                               simgrid::mc::RegionSnapshot* region2, std::size_t size);
137
138 static XBT_ALWAYS_INLINE const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot)
139 {
140   if (snapshot == nullptr)
141     xbt_die("snapshot is nullptr");
142   return mc_model_checker->process().get_heap()->breakval;
143 }
144
145 /** @brief Read memory from a snapshot region
146  *
147  *  @param addr    Process (non-snapshot) address of the data
148  *  @param region  Snapshot memory region where the data is located
149  *  @param target  Buffer to store the value
150  *  @param size    Size of the data to read in bytes
151  *  @return Pointer where the data is located (target buffer of original location)
152  */
153 static XBT_ALWAYS_INLINE const void* MC_region_read(simgrid::mc::RegionSnapshot* region, void* target, const void* addr,
154                                                     std::size_t size)
155 {
156   xbt_assert(region);
157
158   std::uintptr_t offset = (std::uintptr_t)addr - (std::uintptr_t)region->start().address();
159
160   xbt_assert(region->contain(simgrid::mc::remote(addr)), "Trying to read out of the region boundary.");
161
162   switch (region->storage_type()) {
163     case simgrid::mc::StorageType::Flat:
164       return (char*)region->flat_data().get() + offset;
165
166     case simgrid::mc::StorageType::Chunked: {
167       // Last byte of the region:
168       void* end = (char*)addr + size - 1;
169       if (simgrid::mc::mmu::same_chunk((std::uintptr_t)addr, (std::uintptr_t)end)) {
170         // The memory is contained in a single page:
171         return mc_translate_address_region_chunked((uintptr_t)addr, region);
172       }
173       // Otherwise, the memory spans several pages:
174       return MC_region_read_fragmented(region, target, addr, size);
175     }
176
177     default:
178       // includes StorageType::NoData and StorageType::Privatized (we currently do not pass the process_index to this
179       // function so we assume that the privatized region has been resolved in the callers)
180       xbt_die("Storage type not supported");
181   }
182 }
183
184 static XBT_ALWAYS_INLINE void* MC_region_read_pointer(simgrid::mc::RegionSnapshot* region, const void* addr)
185 {
186   void* res;
187   return *(void**)MC_region_read(region, &res, addr, sizeof(void*));
188 }
189
190 #endif