1 /* Copyright (c) 2007-2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <sys/types.h> // off_t
11 #include <stdint.h> // size_t
17 #include <simgrid_config.h>
18 #include "../xbt/mmalloc/mmprivate.h"
19 #include <xbt/asserts.h>
20 #include <xbt/dynar.h>
22 #include "mc_forward.h"
23 #include "ModelChecker.hpp"
24 #include "PageStore.hpp"
25 #include "mc_mmalloc.h"
26 #include "mc/AddressSpace.hpp"
28 #include "RegionSnapshot.hpp"
32 // ***** Snapshot region
34 XBT_INTERNAL void mc_region_restore_sparse(mc_process_t process, mc_mem_region_t reg);
36 static inline __attribute__((always_inline))
37 void* mc_translate_address_region_chunked(uintptr_t addr, mc_mem_region_t region)
39 size_t pageno = mc_page_number((void*)region->start().address(), (void*) addr);
40 const void* snapshot_page =
41 region->page_data().page(pageno);
42 return (char*) snapshot_page + mc_page_offset((void*) addr);
45 static inline __attribute__((always_inline))
46 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region, int process_index)
48 switch (region->storage_type()) {
49 case simgrid::mc::StorageType::NoData:
51 xbt_die("Storage type not supported");
53 case simgrid::mc::StorageType::Flat:
55 uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
56 return (void *) ((uintptr_t) region->flat_data().data() + offset);
59 case simgrid::mc::StorageType::Chunked:
60 return mc_translate_address_region_chunked(addr, region);
62 case simgrid::mc::StorageType::Privatized:
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);
74 XBT_INTERNAL mc_mem_region_t mc_get_snapshot_region(
75 const void* addr, const s_mc_snapshot_t *snapshot, int process_index);
83 * Some parts of the snapshot are ignored by zeroing them out: the real
84 * values is stored here.
86 typedef struct s_mc_snapshot_ignored_data {
88 std::vector<char> data;
89 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
91 typedef struct s_fd_infos{
94 off_t current_position;
96 }s_fd_infos_t, *fd_infos_t;
98 /** Information about a given stack frame
101 typedef struct s_mc_stack_frame {
102 /** Instruction pointer */
106 unw_word_t frame_base;
108 std::string frame_name;
109 unw_cursor_t unw_cursor;
110 } s_mc_stack_frame_t, *mc_stack_frame_t;
112 typedef struct s_local_variable{
113 dw_frame_t subprogram;
119 } s_local_variable_t, *local_variable_t;
121 typedef struct s_mc_snapshot_stack{
122 std::vector<s_local_variable> local_variables;
123 mc_unw_context_t context;
124 xbt_dynar_t stack_frames; // mc_stack_frame_t
127 s_mc_snapshot_stack()
128 : context(nullptr), stack_frames(nullptr), process_index(0)
130 ~s_mc_snapshot_stack();
131 s_mc_snapshot_stack(s_mc_snapshot_stack& p) = delete;
132 s_mc_snapshot_stack& operator=(s_mc_snapshot_stack&) = delete;
134 s_mc_snapshot_stack(s_mc_snapshot_stack&& that)
136 *this = std::move(that);
138 s_mc_snapshot_stack& operator=(s_mc_snapshot_stack&& that)
140 this->local_variables = std::move(that.local_variables);
141 this->context = std::move(that.context);
142 that.context = nullptr;
143 this->stack_frames = std::move(that.stack_frames);
144 that.stack_frames = nullptr;
145 this->process_index = that.process_index;
146 that.process_index = 0;
150 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
152 typedef struct s_mc_global_t {
153 mc_snapshot_t snapshot;
156 int initial_communications_pattern_done;
157 int recv_deterministic;
158 int send_deterministic;
161 }s_mc_global_t, *mc_global_t;
166 class Snapshot : public AddressSpace {
170 const void* read_bytes(void* buffer, std::size_t size,
171 remote_ptr<void> address, int process_index = ProcessIndexAny,
172 ReadMode mode = Normal) const MC_OVERRIDE;
173 public: // To be private
174 mc_process_t process;
176 size_t heap_bytes_used;
177 std::vector<std::unique_ptr<s_mc_mem_region_t>> snapshot_regions;
178 std::set<pid_t> enabled_processes;
179 int privatization_index;
180 std::vector<size_t> stack_sizes;
181 std::vector<s_mc_snapshot_stack_t> stacks;
182 std::vector<s_mc_heap_ignore_region_t> to_ignore;
184 std::vector<s_mc_snapshot_ignored_data> ignored_data;
185 std::vector<s_fd_infos_t> current_fds;
193 static inline __attribute__ ((always_inline))
194 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
196 if (region->contain(simgrid::mc::remote(addr)))
199 return mc_get_snapshot_region(addr, snapshot, process_index);
202 static const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
204 XBT_INTERNAL mc_snapshot_t MC_take_snapshot(int num_state);
205 XBT_INTERNAL void MC_restore_snapshot(mc_snapshot_t);
207 XBT_INTERNAL void mc_restore_page_snapshot_region(
208 mc_process_t process,
209 void* start_addr, simgrid::mc::PerPageCopy const& pagenos);
211 MC_SHOULD_BE_INTERNAL const void* MC_region_read_fragmented(
212 mc_mem_region_t region, void* target, const void* addr, size_t size);
214 MC_SHOULD_BE_INTERNAL int MC_snapshot_region_memcmp(
215 const void* addr1, mc_mem_region_t region1,
216 const void* addr2, mc_mem_region_t region2, size_t size);
217 XBT_INTERNAL int MC_snapshot_memcmp(
218 const void* addr1, mc_snapshot_t snapshot1,
219 const void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
221 static inline __attribute__ ((always_inline))
222 const void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot)
225 xbt_die("snapshot is NULL");
226 return mc_model_checker->process().get_heap()->breakval;
229 /** @brief Read memory from a snapshot region
231 * @param addr Process (non-snapshot) address of the data
232 * @param region Snapshot memory region where the data is located
233 * @param target Buffer to store the value
234 * @param size Size of the data to read in bytes
235 * @return Pointer where the data is located (target buffer of original location)
237 static inline __attribute__((always_inline))
238 const void* MC_region_read(mc_mem_region_t region, void* target, const void* addr, size_t size)
242 uintptr_t offset = (uintptr_t) addr - (uintptr_t) region->start().address();
244 xbt_assert(region->contain(simgrid::mc::remote(addr)),
245 "Trying to read out of the region boundary.");
247 switch (region->storage_type()) {
248 case simgrid::mc::StorageType::NoData:
250 xbt_die("Storage type not supported");
252 case simgrid::mc::StorageType::Flat:
253 return (char*) region->flat_data().data() + offset;
255 case simgrid::mc::StorageType::Chunked:
257 // Last byte of the region:
258 void* end = (char*) addr + size - 1;
259 if (mc_same_page(addr, end) ) {
260 // The memory is contained in a single page:
261 return mc_translate_address_region_chunked((uintptr_t) addr, region);
263 // The memory spans several pages:
264 return MC_region_read_fragmented(region, target, addr, size);
268 // We currently do not pass the process_index to this function so we assume
269 // that the privatized region has been resolved in the callers:
270 case simgrid::mc::StorageType::Privatized:
271 xbt_die("Storage type not supported");
275 static inline __attribute__ ((always_inline))
276 void* MC_region_read_pointer(mc_mem_region_t region, const void* addr)
279 return *(void**) MC_region_read(region, &res, addr, sizeof(void*));
284 XBT_INTERNAL int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2,
285 std::vector<s_mc_heap_ignore_region_t>* i1,
286 std::vector<s_mc_heap_ignore_region_t>* i2);