1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_MC_SNAPSHOT_H
7 #define SIMGRID_MC_SNAPSHOT_H
14 #include "src/mc/ModelChecker.hpp"
15 #include "src/mc/RegionSnapshot.hpp"
16 #include "src/mc/mc_forward.hpp"
17 #include "src/mc/mc_unw.h"
21 // ***** Snapshot region
23 XBT_PRIVATE void mc_region_restore_sparse(simgrid::mc::RemoteClient* process, mc_mem_region_t reg);
25 static XBT_ALWAYS_INLINE void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
27 auto split = simgrid::mc::mmu::split(addr - region->start().address());
28 auto pageno = split.first;
29 auto offset = split.second;
30 const void* snapshot_page = region->page_data().page(pageno);
31 return (char*) snapshot_page + offset;
34 static XBT_ALWAYS_INLINE void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
36 switch (region->storage_type()) {
37 case simgrid::mc::StorageType::Flat:
39 uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
40 return (void *) ((uintptr_t) region->flat_data().get() + offset);
42 case simgrid::mc::StorageType::Chunked:
43 return mc_translate_address_region_chunked(addr, region);
44 case simgrid::mc::StorageType::Privatized:
46 xbt_assert(process_index >= 0, "Missing process index for privatized region");
47 xbt_assert((size_t)process_index < region->privatized_data().size(), "Out of range process index");
48 simgrid::mc::RegionSnapshot& subregion = region->privatized_data()[process_index];
49 return mc_translate_address_region(addr, &subregion, process_index);
51 case simgrid::mc::StorageType::NoData:
53 xbt_die("Storage type not supported");
57 XBT_PRIVATE mc_mem_region_t mc_get_snapshot_region(const void* addr, const simgrid::mc::Snapshot* snapshot,
65 * Some parts of the snapshot are ignored by zeroing them out: the real
66 * values is stored here.
68 typedef struct s_mc_snapshot_ignored_data {
70 std::vector<char> data;
71 } s_mc_snapshot_ignored_data_t;
72 typedef s_mc_snapshot_ignored_data_t* mc_snapshot_ignored_data_t;
74 typedef struct s_fd_infos{
77 off_t current_position;
80 typedef s_fd_infos_t* fd_infos_t;
82 /** Information about a given stack frame */
83 typedef struct s_mc_stack_frame {
84 /** Instruction pointer */
88 unw_word_t frame_base;
89 simgrid::mc::Frame* frame;
90 std::string frame_name;
91 unw_cursor_t unw_cursor;
93 typedef s_mc_stack_frame_t* mc_stack_frame_t;
95 typedef struct s_local_variable{
96 simgrid::mc::Frame* subprogram;
99 simgrid::mc::Type* type;
102 } s_local_variable_t;
103 typedef s_local_variable_t* local_variable_t;
105 typedef struct XBT_PRIVATE s_mc_snapshot_stack {
106 std::vector<s_local_variable> local_variables;
107 simgrid::mc::UnwindContext context;
108 std::vector<s_mc_stack_frame_t> stack_frames;
110 } s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
115 class XBT_PRIVATE Snapshot final : public AddressSpace {
117 Snapshot(RemoteClient* process, int num_state);
118 ~Snapshot() = default;
119 const void* read_bytes(void* buffer, std::size_t size,
120 RemotePtr<void> address, int process_index = ProcessIndexAny,
121 ReadOptions options = ReadOptions::none()) const override;
125 std::size_t heap_bytes_used;
126 std::vector<std::unique_ptr<s_mc_mem_region_t>> snapshot_regions;
127 std::set<pid_t> enabled_processes;
128 int privatization_index;
129 std::vector<std::size_t> stack_sizes;
130 std::vector<s_mc_snapshot_stack_t> stacks;
131 std::vector<simgrid::mc::IgnoredHeapRegion> to_ignore;
133 std::vector<s_mc_snapshot_ignored_data> ignored_data;
134 std::vector<s_fd_infos_t> current_fds;
142 static XBT_ALWAYS_INLINE mc_mem_region_t mc_get_region_hinted(void* addr, simgrid::mc::Snapshot* snapshot,
143 int process_index, mc_mem_region_t region)
145 if (region->contain(simgrid::mc::remote(addr)))
148 return mc_get_snapshot_region(addr, snapshot, process_index);
151 static const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot);
160 XBT_PRIVATE std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state);
161 XBT_PRIVATE void restore_snapshot(std::shared_ptr<simgrid::mc::Snapshot> snapshot);
170 XBT_PRIVATE void mc_restore_page_snapshot_region(simgrid::mc::RemoteClient* process, void* start_addr,
171 simgrid::mc::ChunkedData const& pagenos);
173 const void* MC_region_read_fragmented(
174 mc_mem_region_t region, void* target, const void* addr, std::size_t size);
176 int MC_snapshot_region_memcmp(
177 const void* addr1, mc_mem_region_t region1,
178 const void* addr2, mc_mem_region_t region2, std::size_t size);
179 XBT_PRIVATE int MC_snapshot_memcmp(
180 const void* addr1, simgrid::mc::Snapshot* snapshot1,
181 const void* addr2, simgrid::mc::Snapshot* snapshot2, int process_index, std::size_t size);
183 static XBT_ALWAYS_INLINE const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot)
185 if(snapshot==nullptr)
186 xbt_die("snapshot is nullptr");
187 return mc_model_checker->process().get_heap()->breakval;
190 /** @brief Read memory from a snapshot region
192 * @param addr Process (non-snapshot) address of the data
193 * @param region Snapshot memory region where the data is located
194 * @param target Buffer to store the value
195 * @param size Size of the data to read in bytes
196 * @return Pointer where the data is located (target buffer of original location)
198 static XBT_ALWAYS_INLINE const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr,
203 std::uintptr_t offset = (std::uintptr_t)addr - (std::uintptr_t)region->start().address();
205 xbt_assert(region->contain(simgrid::mc::remote(addr)), "Trying to read out of the region boundary.");
207 switch (region->storage_type()) {
208 case simgrid::mc::StorageType::NoData:
210 xbt_die("Storage type not supported");
212 case simgrid::mc::StorageType::Flat:
213 return (char*) region->flat_data().get() + offset;
215 case simgrid::mc::StorageType::Chunked:
217 // Last byte of the region:
218 void* end = (char*) addr + size - 1;
219 if (simgrid::mc::mmu::sameChunk((std::uintptr_t) addr, (std::uintptr_t) end) ) {
220 // The memory is contained in a single page:
221 return mc_translate_address_region_chunked((uintptr_t) addr, region);
223 // The memory spans several pages:
224 return MC_region_read_fragmented(region, target, addr, size);
228 // We currently do not pass the process_index to this function so we assume
229 // that the privatized region has been resolved in the callers:
230 case simgrid::mc::StorageType::Privatized:
231 xbt_die("Storage type not supported");
235 static XBT_ALWAYS_INLINE void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
238 return *(void**) MC_region_read(region, &res, addr, sizeof(void*));