1 /* Copyright (c) 2007-2018. 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_HPP
7 #define SIMGRID_MC_SNAPSHOT_HPP
9 #include "src/mc/ModelChecker.hpp"
10 #include "src/mc/mc_unw.hpp"
11 #include "src/mc/remote/RemoteClient.hpp"
12 #include "src/mc/sosp/RegionSnapshot.hpp"
14 // ***** Snapshot region
16 XBT_PRIVATE void mc_region_restore_sparse(simgrid::mc::RemoteClient* process, mc_mem_region_t reg);
18 static XBT_ALWAYS_INLINE void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
20 auto split = simgrid::mc::mmu::split(addr - region->start().address());
21 auto pageno = split.first;
22 auto offset = split.second;
23 const void* snapshot_page = region->page_data().page(pageno);
24 return (char*)snapshot_page + offset;
27 static XBT_ALWAYS_INLINE void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
29 switch (region->storage_type()) {
30 case simgrid::mc::StorageType::Flat: {
31 uintptr_t offset = (uintptr_t)addr - (uintptr_t)region->start().address();
32 return (void*)((uintptr_t)region->flat_data().get() + offset);
34 case simgrid::mc::StorageType::Chunked:
35 return mc_translate_address_region_chunked(addr, region);
36 case simgrid::mc::StorageType::Privatized: {
37 xbt_assert(process_index >= 0, "Missing process index for privatized region");
38 xbt_assert((size_t)process_index < region->privatized_data().size(), "Out of range process index");
39 simgrid::mc::RegionSnapshot& subregion = region->privatized_data()[process_index];
40 return mc_translate_address_region(addr, &subregion, process_index);
42 default: // includes StorageType::NoData
43 xbt_die("Storage type not supported");
47 XBT_PRIVATE mc_mem_region_t mc_get_snapshot_region(const void* addr, const simgrid::mc::Snapshot* snapshot,
54 * Some parts of the snapshot are ignored by zeroing them out: the real
55 * values is stored here.
57 struct s_mc_snapshot_ignored_data_t {
59 std::vector<char> data;
65 off_t current_position;
69 /** Information about a given stack frame */
70 struct s_mc_stack_frame_t {
71 /** Instruction pointer */
75 unw_word_t frame_base;
76 simgrid::mc::Frame* frame;
77 std::string frame_name;
78 unw_cursor_t unw_cursor;
80 typedef s_mc_stack_frame_t* mc_stack_frame_t;
82 struct s_local_variable_t {
83 simgrid::mc::Frame* subprogram;
86 simgrid::mc::Type* type;
90 typedef s_local_variable_t* local_variable_t;
92 struct XBT_PRIVATE s_mc_snapshot_stack_t {
93 std::vector<s_local_variable_t> local_variables;
94 simgrid::mc::UnwindContext context;
95 std::vector<s_mc_stack_frame_t> stack_frames;
98 typedef s_mc_snapshot_stack_t* mc_snapshot_stack_t;
103 class XBT_PRIVATE Snapshot final : public AddressSpace {
105 Snapshot(RemoteClient* process, int num_state);
106 ~Snapshot() = default;
107 const void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, int process_index = ProcessIndexAny,
108 ReadOptions options = ReadOptions::none()) const override;
112 std::size_t heap_bytes_used;
113 std::vector<std::unique_ptr<s_mc_mem_region_t>> snapshot_regions;
114 std::set<pid_t> enabled_processes;
115 int privatization_index;
116 std::vector<std::size_t> stack_sizes;
117 std::vector<s_mc_snapshot_stack_t> stacks;
118 std::vector<simgrid::mc::IgnoredHeapRegion> to_ignore;
120 std::vector<s_mc_snapshot_ignored_data_t> ignored_data;
121 std::vector<s_fd_infos_t> current_fds;
124 } // namespace simgrid
126 static XBT_ALWAYS_INLINE mc_mem_region_t mc_get_region_hinted(void* addr, simgrid::mc::Snapshot* snapshot,
127 int process_index, mc_mem_region_t region)
129 if (region->contain(simgrid::mc::remote(addr)))
132 return mc_get_snapshot_region(addr, snapshot, process_index);
135 static const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot);
140 XBT_PRIVATE std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state);
141 XBT_PRIVATE void restore_snapshot(std::shared_ptr<simgrid::mc::Snapshot> snapshot);
143 } // namespace simgrid
145 XBT_PRIVATE void mc_restore_page_snapshot_region(simgrid::mc::RemoteClient* process, void* start_addr,
146 simgrid::mc::ChunkedData const& pagenos);
148 const void* MC_region_read_fragmented(mc_mem_region_t region, void* target, const void* addr, std::size_t size);
150 int MC_snapshot_region_memcmp(const void* addr1, mc_mem_region_t region1, const void* addr2, mc_mem_region_t region2,
153 static XBT_ALWAYS_INLINE const void* mc_snapshot_get_heap_end(simgrid::mc::Snapshot* snapshot)
155 if (snapshot == nullptr)
156 xbt_die("snapshot is nullptr");
157 return mc_model_checker->process().get_heap()->breakval;
160 /** @brief Read memory from a snapshot region
162 * @param addr Process (non-snapshot) address of the data
163 * @param region Snapshot memory region where the data is located
164 * @param target Buffer to store the value
165 * @param size Size of the data to read in bytes
166 * @return Pointer where the data is located (target buffer of original location)
168 static XBT_ALWAYS_INLINE const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr,
173 std::uintptr_t offset = (std::uintptr_t)addr - (std::uintptr_t)region->start().address();
175 xbt_assert(region->contain(simgrid::mc::remote(addr)), "Trying to read out of the region boundary.");
177 switch (region->storage_type()) {
178 case simgrid::mc::StorageType::Flat:
179 return (char*)region->flat_data().get() + offset;
181 case simgrid::mc::StorageType::Chunked: {
182 // Last byte of the region:
183 void* end = (char*)addr + size - 1;
184 if (simgrid::mc::mmu::sameChunk((std::uintptr_t)addr, (std::uintptr_t)end)) {
185 // The memory is contained in a single page:
186 return mc_translate_address_region_chunked((uintptr_t)addr, region);
188 // Otherwise, the memory spans several pages:
189 return MC_region_read_fragmented(region, target, addr, size);
193 // includes StorageType::NoData and StorageType::Privatized (we currently do not pass the process_index to this
194 // function so we assume that the privatized region has been resolved in the callers)
195 xbt_die("Storage type not supported");
199 static XBT_ALWAYS_INLINE void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
202 return *(void**)MC_region_read(region, &res, addr, sizeof(void*));