1 /* Copyright (c) 2014-2019. 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 #include <cstddef> /* std::size_t */
8 #include "src/mc/mc_config.hpp"
9 #include "src/mc/mc_hash.hpp"
10 #include "src/mc/mc_smx.hpp"
11 #include "src/mc/sosp/mc_snapshot.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_snapshot, mc, "Taking and restoring snapshots");
15 /** @brief Read memory from a snapshot region broken across fragmented pages
17 * @param addr Process (non-snapshot) address of the data
18 * @param region Snapshot memory region where the data is located
19 * @param target Buffer to store the value
20 * @param size Size of the data to read in bytes
21 * @return Pointer where the data is located (target buffer of original location)
23 const void* MC_region_read_fragmented(simgrid::mc::RegionSnapshot* region, void* target, const void* addr, size_t size)
25 // Last byte of the memory area:
26 void* end = (char*)addr + size - 1;
28 // TODO, we assume the chunks are aligned to natural chunk boundaries.
29 // We should remove this assumption.
31 // Page of the last byte of the memory area:
32 size_t page_end = simgrid::mc::mmu::split((std::uintptr_t)end).first;
37 xbt_die("Missing destination buffer for fragmented memory access");
40 while (simgrid::mc::mmu::split((std::uintptr_t)addr).first != page_end) {
41 void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
42 void* next_page = (void*)simgrid::mc::mmu::join(simgrid::mc::mmu::split((std::uintptr_t)addr).first + 1, 0);
43 size_t readable = (char*)next_page - (char*)addr;
44 memcpy(dest, snapshot_addr, readable);
45 addr = (char*)addr + readable;
46 dest = (char*)dest + readable;
51 void* snapshot_addr = mc_translate_address_region_chunked((uintptr_t)addr, region);
52 memcpy(dest, snapshot_addr, size);
57 /** Compare memory between snapshots (with known regions)
59 * @param addr1 Address in the first snapshot
60 * @param region1 Region of the address in the first snapshot
61 * @param addr2 Address in the second snapshot
62 * @param region2 Region of the address in the second snapshot
63 * @return same semantic as memcmp
65 int MC_snapshot_region_memcmp(const void* addr1, simgrid::mc::RegionSnapshot* region1, const void* addr2,
66 simgrid::mc::RegionSnapshot* region2, size_t size)
68 // Using alloca() for large allocations may trigger stack overflow:
69 // use malloc if the buffer is too big.
70 bool stack_alloc = size < 64;
71 void* buffer1a = nullptr;
72 void* buffer2a = nullptr;
73 if (region1 != nullptr && region1->storage_type() != simgrid::mc::StorageType::Flat)
74 buffer1a = stack_alloc ? alloca(size) : ::operator new(size);
75 if (region2 != nullptr && region2->storage_type() != simgrid::mc::StorageType::Flat)
76 buffer2a = stack_alloc ? alloca(size) : ::operator new(size);
77 const void* buffer1 = MC_region_read(region1, buffer1a, addr1, size);
78 const void* buffer2 = MC_region_read(region2, buffer2a, addr2, size);
80 if (buffer1 == buffer2)
83 res = memcmp(buffer1, buffer2, size);
84 if (not stack_alloc) {
85 ::operator delete(buffer1a);
86 ::operator delete(buffer2a);
93 /************************************* Take Snapshot ************************************/
94 /****************************************************************************************/
96 void simgrid::mc::Snapshot::snapshot_regions(simgrid::mc::RemoteClient* process)
98 snapshot_regions_.clear();
100 for (auto const& object_info : process->object_infos)
101 add_region(simgrid::mc::RegionType::Data, object_info.get(), object_info->start_rw, object_info->start_rw,
102 object_info->end_rw - object_info->start_rw);
104 xbt_mheap_t heap = process->get_heap();
105 void* start_heap = heap->base;
106 void* end_heap = heap->breakval;
108 add_region(simgrid::mc::RegionType::Heap, nullptr, start_heap, start_heap, (char*)end_heap - (char*)start_heap);
109 heap_bytes_used_ = mmalloc_get_bytes_used_remote(heap->heaplimit, process->get_malloc_info());
112 /** @brief Checks whether the variable is in scope for a given IP.
114 * A variable may be defined only from a given value of IP.
116 * @param var Variable description
117 * @param scope Scope description
118 * @param ip Instruction pointer
119 * @return true if the variable is valid
121 static bool valid_variable(simgrid::mc::Variable* var, simgrid::mc::Frame* scope, const void* ip)
123 // The variable is not yet valid:
124 if (scope->range.begin() + var->start_scope > (std::uint64_t)ip)
130 static void fill_local_variables_values(mc_stack_frame_t stack_frame, simgrid::mc::Frame* scope,
131 std::vector<s_local_variable_t>& result)
133 if (not scope || not scope->range.contain(stack_frame->ip))
136 for (simgrid::mc::Variable& current_variable : scope->variables) {
138 if (not valid_variable(¤t_variable, scope, (void*)stack_frame->ip))
141 s_local_variable_t new_var;
142 new_var.subprogram = stack_frame->frame;
143 new_var.ip = stack_frame->ip;
144 new_var.name = current_variable.name;
145 new_var.type = current_variable.type;
146 new_var.address = nullptr;
148 if (current_variable.address != nullptr)
149 new_var.address = current_variable.address;
150 else if (not current_variable.location_list.empty()) {
151 simgrid::dwarf::Location location = simgrid::dwarf::resolve(
152 current_variable.location_list, current_variable.object_info, &(stack_frame->unw_cursor),
153 (void*)stack_frame->frame_base, &mc_model_checker->process());
155 if (not location.in_memory())
156 xbt_die("Cannot handle non-address variable");
157 new_var.address = location.address();
160 xbt_die("No address");
162 result.push_back(std::move(new_var));
165 // Recursive processing of nested scopes:
166 for (simgrid::mc::Frame& nested_scope : scope->scopes)
167 fill_local_variables_values(stack_frame, &nested_scope, result);
170 static std::vector<s_local_variable_t> get_local_variables_values(std::vector<s_mc_stack_frame_t>& stack_frames)
172 std::vector<s_local_variable_t> variables;
173 for (s_mc_stack_frame_t& stack_frame : stack_frames)
174 fill_local_variables_values(&stack_frame, stack_frame.frame, variables);
178 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(simgrid::mc::UnwindContext* stack_context)
180 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
181 std::vector<s_mc_stack_frame_t> result;
183 unw_cursor_t c = stack_context->cursor();
185 // TODO, check condition check (unw_init_local==0 means end of frame)
189 s_mc_stack_frame_t stack_frame;
191 stack_frame.unw_cursor = c;
196 unw_get_reg(&c, UNW_REG_IP, &ip);
197 unw_get_reg(&c, UNW_REG_SP, &sp);
202 // TODO, use real addresses in frame_t instead of fixing it here
204 simgrid::mc::Frame* frame = process->find_function(remote(ip));
205 stack_frame.frame = frame;
208 stack_frame.frame_name = frame->name;
209 stack_frame.frame_base = (unw_word_t)frame->frame_base(c);
211 stack_frame.frame_base = 0;
212 stack_frame.frame_name = std::string();
215 result.push_back(std::move(stack_frame));
217 /* Stop before context switch with maestro */
218 if (frame != nullptr && frame->name == "smx_ctx_wrapper")
221 int ret = unw_step(&c);
223 xbt_die("Unexpected end of stack.");
225 xbt_die("Error while unwinding stack");
228 xbt_assert(not result.empty(), "unw_init_local failed");
233 void simgrid::mc::Snapshot::snapshot_stacks(simgrid::mc::RemoteClient* process)
235 for (auto const& stack : process->stack_areas()) {
236 s_mc_snapshot_stack_t st;
238 // Read the context from remote process:
239 unw_context_t context;
240 mc_model_checker->process().read_bytes(&context, sizeof(context), remote(stack.context));
242 st.context.initialize(&mc_model_checker->process(), &context);
244 st.stack_frames = unwind_stack_frames(&st.context);
245 st.local_variables = get_local_variables_values(st.stack_frames);
247 unw_word_t sp = st.stack_frames[0].sp;
249 stacks_.push_back(std::move(st));
251 size_t stack_size = (char*)stack.address + stack.size - (char*)sp;
252 stack_sizes_.push_back(stack_size);
256 static void snapshot_handle_ignore(simgrid::mc::Snapshot* snapshot)
258 xbt_assert(snapshot->process());
261 for (auto const& region : mc_model_checker->process().ignored_regions()) {
262 s_mc_snapshot_ignored_data_t ignored_data;
263 ignored_data.start = (void*)region.addr;
264 ignored_data.data.resize(region.size);
265 // TODO, we should do this once per privatization segment:
266 snapshot->process()->read_bytes(ignored_data.data.data(), region.size, remote(region.addr));
267 snapshot->ignored_data_.push_back(std::move(ignored_data));
271 for (auto const& region : mc_model_checker->process().ignored_regions())
272 snapshot->process()->clear_bytes(remote(region.addr), region.size);
274 static void snapshot_ignore_restore(simgrid::mc::Snapshot* snapshot)
276 for (auto const& ignored_data : snapshot->ignored_data_)
277 snapshot->process()->write_bytes(ignored_data.data.data(), ignored_data.data.size(), remote(ignored_data.start));
280 Snapshot::Snapshot(int _num_state, RemoteClient* process)
281 : AddressSpace(process)
282 , num_state_(_num_state)
283 , heap_bytes_used_(0)
284 , enabled_processes_()
287 for (auto const& p : process->actors())
288 enabled_processes_.insert(p.copy.getBuffer()->get_pid());
290 snapshot_handle_ignore(this);
292 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
293 snapshot_regions(process);
295 to_ignore_ = process->ignored_heap();
297 if (_sg_mc_max_visited_states > 0 || not _sg_mc_property_file.get().empty()) {
298 snapshot_stacks(process);
300 hash_ = simgrid::mc::hash(*this);
303 snapshot_ignore_restore(this);
306 void Snapshot::add_region(RegionType type, ObjectInformation* object_info, void* start_addr, void* permanent_addr,
309 if (type == simgrid::mc::RegionType::Data)
310 xbt_assert(object_info, "Missing object info for object.");
311 else if (type == simgrid::mc::RegionType::Heap)
312 xbt_assert(not object_info, "Unexpected object info for heap region.");
314 simgrid::mc::RegionSnapshot* region = simgrid::mc::region(type, start_addr, permanent_addr, size);
315 region->object_info(object_info);
316 snapshot_regions_.push_back(std::unique_ptr<simgrid::mc::RegionSnapshot>(std::move(region)));
319 const void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, ReadOptions options) const
321 RegionSnapshot* region = this->get_region((void*)address.address());
323 const void* res = MC_region_read(region, buffer, (void*)address.address(), size);
324 if (buffer == res || options & ReadOptions::lazy())
327 memcpy(buffer, res, size);
331 return this->process()->read_bytes(buffer, size, address, options);
333 /** @brief Find the snapshoted region from a pointer
335 * @param addr Pointer
337 RegionSnapshot* Snapshot::get_region(const void* addr) const
339 size_t n = snapshot_regions_.size();
340 for (size_t i = 0; i != n; ++i) {
341 RegionSnapshot* region = snapshot_regions_[i].get();
342 if (not(region && region->contain(simgrid::mc::remote(addr))))
351 /** @brief Find the snapshoted region from a pointer, with a hinted_region */
352 RegionSnapshot* Snapshot::get_region(const void* addr, RegionSnapshot* hinted_region) const
354 if (hinted_region->contain(simgrid::mc::remote(addr)))
355 return hinted_region;
357 return get_region(addr);
360 void Snapshot::restore(RemoteClient* process)
362 XBT_DEBUG("Restore snapshot %i", num_state_);
365 for (std::unique_ptr<simgrid::mc::RegionSnapshot> const& region : snapshot_regions_) {
366 if (region) // privatized variables are not snapshoted
367 region.get()->restore();
370 snapshot_ignore_restore(this);
371 process->clear_cache();
375 } // namespace simgrid