Logo AND Algorithmique Numérique Distribuée

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