Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
68e9fdadce1f46af33a07fbfec61769e3867787c
[simgrid.git] / src / mc / mc_snapshot.h
1 /* Copyright (c) 2007-2017. 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_H
7 #define SIMGRID_MC_SNAPSHOT_H
8
9 #include <cstdint>
10 #include <cstddef>
11
12 #include <vector>
13 #include <set>
14 #include <string>
15 #include <memory>
16
17 #include <sys/types.h> // off_t
18
19 #include "src/xbt/mmalloc/mmprivate.h"
20 #include "xbt/asserts.h"
21 #include "xbt/base.h"
22
23 #include "src/mc/ModelChecker.hpp"
24 #include "src/mc/RegionSnapshot.hpp"
25 #include "src/mc/mc_forward.hpp"
26 #include "src/mc/mc_unw.h"
27
28 SG_BEGIN_DECL()
29
30 // ***** Snapshot region
31
32 XBT_PRIVATE void mc_region_restore_sparse(simgrid::mc::Process* process, mc_mem_region_t reg);
33
34 static XBT_ALWAYS_INLINE void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
35 {
36   auto split = simgrid::mc::mmu::split(addr - region->start().address());
37   auto pageno = split.first;
38   auto offset = split.second;
39   const void* snapshot_page = region->page_data().page(pageno);
40   return (char*) snapshot_page + offset;
41 }
42
43 static XBT_ALWAYS_INLINE void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
44 {
45   switch (region->storage_type()) {
46   case simgrid::mc::StorageType::NoData:
47   default:
48     xbt_die("Storage type not supported");
49
50   case simgrid::mc::StorageType::Flat:
51     {
52       uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
53       return (void *) ((uintptr_t) region->flat_data().get() + offset);
54     }
55
56   case simgrid::mc::StorageType::Chunked:
57     return mc_translate_address_region_chunked(addr, region);
58
59   case simgrid::mc::StorageType::Privatized:
60     {
61       xbt_assert(process_index >=0,
62         "Missing process index for privatized region");
63       xbt_assert((size_t) process_index < region->privatized_data().size(),
64         "Out of range process index");
65       simgrid::mc::RegionSnapshot& subregion= region->privatized_data()[process_index];
66       return mc_translate_address_region(addr, &subregion, process_index);
67     }
68   }
69 }
70
71 XBT_PRIVATE mc_mem_region_t mc_get_snapshot_region(
72   const void* addr, const simgrid::mc::Snapshot *snapshot, int process_index);
73
74 }
75
76 // ***** MC Snapshot
77
78 /** Ignored data
79  *
80  *  Some parts of the snapshot are ignored by zeroing them out: the real
81  *  values is stored here.
82  * */
83 typedef struct s_mc_snapshot_ignored_data {
84   void* start;
85   std::vector<char> data;
86 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
87
88 typedef struct s_fd_infos{
89   std::string filename;
90   int number;
91   off_t current_position;
92   int flags;
93 }s_fd_infos_t, *fd_infos_t;
94
95 /** Information about a given stack frame
96  *
97  */
98 typedef struct s_mc_stack_frame {
99   /** Instruction pointer */
100   unw_word_t ip;
101   /** Stack pointer */
102   unw_word_t sp;
103   unw_word_t frame_base;
104   simgrid::mc::Frame* frame;
105   std::string frame_name;
106   unw_cursor_t unw_cursor;
107 } s_mc_stack_frame_t, *mc_stack_frame_t;
108
109 typedef struct s_local_variable{
110   simgrid::mc::Frame* subprogram;
111   unsigned long ip;
112   std::string name;
113   simgrid::mc::Type* type;
114   void *address;
115   int region;
116 } s_local_variable_t, *local_variable_t;
117
118 typedef struct XBT_PRIVATE s_mc_snapshot_stack {
119   std::vector<s_local_variable> local_variables;
120   simgrid::mc::UnwindContext context;
121   std::vector<s_mc_stack_frame_t> stack_frames;
122   int process_index;
123 } s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
124
125 namespace simgrid {
126 namespace mc {
127
128 class XBT_PRIVATE Snapshot final : public AddressSpace {
129 public:
130   Snapshot(Process* process, int num_state);
131   ~Snapshot() = default;
132   const void* read_bytes(void* buffer, std::size_t size,
133     RemotePtr<void> address, int process_index = ProcessIndexAny,
134     ReadOptions options = ReadOptions::none()) const override;
135 public: // To be private
136   int num_state;
137   std::size_t heap_bytes_used;
138   std::vector<std::unique_ptr<s_mc_mem_region_t>> snapshot_regions;
139   std::set<pid_t> enabled_processes;
140   int privatization_index;
141   std::vector<std::size_t> stack_sizes;
142   std::vector<s_mc_snapshot_stack_t> stacks;
143   std::vector<simgrid::mc::IgnoredHeapRegion> to_ignore;
144   std::uint64_t hash;
145   std::vector<s_mc_snapshot_ignored_data> ignored_data;
146   std::vector<s_fd_infos_t> current_fds;
147 };
148
149 }
150 }
151
152 extern "C" {
153
154 static XBT_ALWAYS_INLINE mc_mem_region_t mc_get_region_hinted(void* addr, simgrid::mc::Snapshot* snapshot,
155                                                               int process_index, mc_mem_region_t region)
156 {
157   if (region->contain(simgrid::mc::remote(addr)))
158     return region;
159   else
160     return mc_get_snapshot_region(addr, snapshot, process_index);
161 }
162
163 static const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot);
164
165 }
166
167 #ifdef __cplusplus
168
169 namespace simgrid {
170 namespace mc {
171
172 XBT_PRIVATE std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state);
173 XBT_PRIVATE void restore_snapshot(std::shared_ptr<simgrid::mc::Snapshot> snapshot);
174
175 }
176 }
177
178 #endif
179
180 extern "C" {
181
182 XBT_PRIVATE void mc_restore_page_snapshot_region(
183   simgrid::mc::Process* process,
184   void* start_addr, simgrid::mc::ChunkedData const& pagenos);
185
186 const void* MC_region_read_fragmented(
187   mc_mem_region_t region, void* target, const void* addr, std::size_t size);
188
189 int MC_snapshot_region_memcmp(
190   const void* addr1, mc_mem_region_t region1,
191   const void* addr2, mc_mem_region_t region2, std::size_t size);
192 XBT_PRIVATE int MC_snapshot_memcmp(
193   const void* addr1, simgrid::mc::Snapshot* snapshot1,
194   const void* addr2, simgrid::mc::Snapshot* snapshot2, int process_index, std::size_t size);
195
196 static XBT_ALWAYS_INLINE const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot)
197 {
198   if(snapshot==nullptr)
199       xbt_die("snapshot is nullptr");
200   return mc_model_checker->process().get_heap()->breakval;
201 }
202
203 /** @brief Read memory from a snapshot region
204  *
205  *  @param addr    Process (non-snapshot) address of the data
206  *  @param region  Snapshot memory region where the data is located
207  *  @param target  Buffer to store the value
208  *  @param size    Size of the data to read in bytes
209  *  @return Pointer where the data is located (target buffer of original location)
210  */
211 static XBT_ALWAYS_INLINE const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr,
212                                                     std::size_t size)
213 {
214   xbt_assert(region);
215
216   std::uintptr_t offset = (std::uintptr_t)addr - (std::uintptr_t)region->start().address();
217
218   xbt_assert(region->contain(simgrid::mc::remote(addr)), "Trying to read out of the region boundary.");
219
220   switch (region->storage_type()) {
221   case simgrid::mc::StorageType::NoData:
222   default:
223     xbt_die("Storage type not supported");
224
225   case simgrid::mc::StorageType::Flat:
226     return (char*) region->flat_data().get() + offset;
227
228   case simgrid::mc::StorageType::Chunked:
229     {
230       // Last byte of the region:
231       void* end = (char*) addr + size - 1;
232       if (simgrid::mc::mmu::sameChunk((std::uintptr_t) addr, (std::uintptr_t) end) ) {
233         // The memory is contained in a single page:
234         return mc_translate_address_region_chunked((uintptr_t) addr, region);
235       } else {
236         // The memory spans several pages:
237         return MC_region_read_fragmented(region, target, addr, size);
238       }
239     }
240
241   // We currently do not pass the process_index to this function so we assume
242   // that the privatized region has been resolved in the callers:
243   case simgrid::mc::StorageType::Privatized:
244     xbt_die("Storage type not supported");
245   }
246 }
247
248 static XBT_ALWAYS_INLINE void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
249 {
250   void* res;
251   return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
252 }
253
254 SG_END_DECL()
255
256 #endif