1 /* Copyright (c) 2008-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. */
12 #include "src/internal_config.h"
13 #include "src/mc/mc_private.hpp"
14 #include "src/smpi/include/private.hpp"
15 #include "xbt/file.hpp"
16 #include "xbt/mmalloc.h"
17 #include "xbt/module.h"
19 #include "src/xbt/mmalloc/mmprivate.h"
21 #include "src/simix/smx_private.hpp"
24 #include <libunwind.h>
26 #include "src/mc/mc_private.hpp"
29 #include "src/mc/mc_config.hpp"
30 #include "src/mc/mc_hash.hpp"
31 #include "src/mc/mc_mmu.hpp"
32 #include "src/mc/mc_smx.hpp"
33 #include "src/mc/remote/mc_protocol.h"
34 #include "src/mc/sosp/mc_snapshot.hpp"
36 #include "src/mc/sosp/RegionSnapshot.hpp"
38 using simgrid::mc::remote;
40 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc, "Logging specific to mc_checkpoint");
42 #define PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC)
43 #define PROT_RW (PROT_READ | PROT_WRITE)
44 #define PROT_RX (PROT_READ | PROT_EXEC)
49 /************************************ Free functions **************************************/
50 /*****************************************************************************************/
52 /** @brief Restore a region from a snapshot
54 * @param region Target region
56 static void restore(RegionSnapshot* region)
58 switch (region->storage_type()) {
59 case simgrid::mc::StorageType::Flat:
60 mc_model_checker->process().write_bytes(region->flat_data().get(), region->size(), region->permanent_address());
63 case simgrid::mc::StorageType::Chunked:
64 xbt_assert(((region->permanent_address().address()) & (xbt_pagesize - 1)) == 0, "Not at the beginning of a page");
65 xbt_assert(simgrid::mc::mmu::chunk_count(region->size()) == region->page_data().page_count());
67 for (size_t i = 0; i != region->page_data().page_count(); ++i) {
69 (void*)simgrid::mc::mmu::join(i, (std::uintptr_t)(void*)region->permanent_address().address());
70 const void* source_page = region->page_data().page(i);
71 mc_model_checker->process().write_bytes(source_page, xbt_pagesize, remote(target_page));
76 case simgrid::mc::StorageType::Privatized:
77 for (auto& p : region->privatized_data())
81 default: // includes StorageType::NoData
82 xbt_die("Storage type not supported");
87 static void get_memory_regions(simgrid::mc::RemoteClient* process, simgrid::mc::Snapshot* snapshot)
89 snapshot->snapshot_regions_.clear();
91 for (auto const& object_info : process->object_infos)
92 snapshot->add_region(simgrid::mc::RegionType::Data, object_info.get(), object_info->start_rw, object_info->start_rw,
93 object_info->end_rw - object_info->start_rw);
95 xbt_mheap_t heap = process->get_heap();
96 void* start_heap = heap->base;
97 void* end_heap = heap->breakval;
99 snapshot->add_region(simgrid::mc::RegionType::Heap, nullptr, start_heap, start_heap,
100 (char*)end_heap - (char*)start_heap);
101 snapshot->heap_bytes_used_ = mmalloc_get_bytes_used_remote(heap->heaplimit, process->get_malloc_info());
102 snapshot->privatization_index_ = simgrid::mc::ProcessIndexMissing;
105 if (mc_model_checker->process().privatized() && MC_smpi_process_count())
106 // snapshot->privatization_index = smpi_loaded_page
107 mc_model_checker->process().read_variable("smpi_loaded_page", &snapshot->privatization_index_,
108 sizeof(snapshot->privatization_index_));
112 /** @brief Fills the position of the segments (executable, read-only, read/write).
114 // TODO, use the ELF segment information for more robustness
115 void find_object_address(std::vector<simgrid::xbt::VmMap> const& maps, simgrid::mc::ObjectInformation* result)
117 std::string name = simgrid::xbt::Path(result->file_name).get_base_name();
119 for (size_t i = 0; i < maps.size(); ++i) {
120 simgrid::xbt::VmMap const& reg = maps[i];
121 if (maps[i].pathname.empty())
123 std::string map_basename = simgrid::xbt::Path(maps[i].pathname).get_base_name();
124 if (map_basename != name)
127 // This is the non-GNU_RELRO-part of the data segment:
128 if (reg.prot == PROT_RW) {
129 xbt_assert(not result->start_rw, "Multiple read-write segments for %s, not supported", maps[i].pathname.c_str());
130 result->start_rw = (char*)reg.start_addr;
131 result->end_rw = (char*)reg.end_addr;
133 // The next VMA might be end of the data segment:
134 if (i + 1 < maps.size() && maps[i + 1].pathname.empty() && maps[i + 1].prot == PROT_RW &&
135 maps[i + 1].start_addr == reg.end_addr)
136 result->end_rw = (char*)maps[i + 1].end_addr;
139 // This is the text segment:
140 else if (reg.prot == PROT_RX) {
141 xbt_assert(not result->start_exec, "Multiple executable segments for %s, not supported",
142 maps[i].pathname.c_str());
143 result->start_exec = (char*)reg.start_addr;
144 result->end_exec = (char*)reg.end_addr;
146 // The next VMA might be end of the data segment:
147 if (i + 1 < maps.size() && maps[i + 1].pathname.empty() && maps[i + 1].prot == PROT_RW &&
148 maps[i + 1].start_addr == reg.end_addr) {
149 result->start_rw = (char*)maps[i + 1].start_addr;
150 result->end_rw = (char*)maps[i + 1].end_addr;
154 // This is the GNU_RELRO-part of the data segment:
155 else if (reg.prot == PROT_READ) {
156 xbt_assert(not result->start_ro, "Multiple read only segments for %s, not supported", maps[i].pathname.c_str());
157 result->start_ro = (char*)reg.start_addr;
158 result->end_ro = (char*)reg.end_addr;
162 result->start = result->start_rw;
163 if ((const void*)result->start_ro < result->start)
164 result->start = result->start_ro;
165 if ((const void*)result->start_exec < result->start)
166 result->start = result->start_exec;
168 result->end = result->end_rw;
169 if (result->end_ro && (const void*)result->end_ro > result->end)
170 result->end = result->end_ro;
171 if (result->end_exec && (const void*)result->end_exec > result->end)
172 result->end = result->end_exec;
174 xbt_assert(result->start_exec || result->start_rw || result->start_ro);
177 /************************************* Take Snapshot ************************************/
178 /****************************************************************************************/
180 /** @brief Checks whether the variable is in scope for a given IP.
182 * A variable may be defined only from a given value of IP.
184 * @param var Variable description
185 * @param scope Scope description
186 * @param ip Instruction pointer
187 * @return true if the variable is valid
189 static bool valid_variable(simgrid::mc::Variable* var, simgrid::mc::Frame* scope, const void* ip)
191 // The variable is not yet valid:
192 if (scope->range.begin() + var->start_scope > (std::uint64_t)ip)
198 static void fill_local_variables_values(mc_stack_frame_t stack_frame, simgrid::mc::Frame* scope, int process_index,
199 std::vector<s_local_variable_t>& result)
201 if (not scope || not scope->range.contain(stack_frame->ip))
204 for (simgrid::mc::Variable& current_variable : scope->variables) {
206 if (not valid_variable(¤t_variable, scope, (void*)stack_frame->ip))
209 s_local_variable_t new_var;
210 new_var.subprogram = stack_frame->frame;
211 new_var.ip = stack_frame->ip;
212 new_var.name = current_variable.name;
213 new_var.type = current_variable.type;
214 new_var.address = nullptr;
216 if (current_variable.address != nullptr)
217 new_var.address = current_variable.address;
218 else if (not current_variable.location_list.empty()) {
219 simgrid::dwarf::Location location = simgrid::dwarf::resolve(
220 current_variable.location_list, current_variable.object_info, &(stack_frame->unw_cursor),
221 (void*)stack_frame->frame_base, &mc_model_checker->process(), process_index);
223 if (not location.in_memory())
224 xbt_die("Cannot handle non-address variable");
225 new_var.address = location.address();
228 xbt_die("No address");
230 result.push_back(std::move(new_var));
233 // Recursive processing of nested scopes:
234 for (simgrid::mc::Frame& nested_scope : scope->scopes)
235 fill_local_variables_values(stack_frame, &nested_scope, process_index, result);
238 static std::vector<s_local_variable_t> get_local_variables_values(std::vector<s_mc_stack_frame_t>& stack_frames,
241 std::vector<s_local_variable_t> variables;
242 for (s_mc_stack_frame_t& stack_frame : stack_frames)
243 fill_local_variables_values(&stack_frame, stack_frame.frame, process_index, variables);
247 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(simgrid::mc::UnwindContext* stack_context)
249 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
250 std::vector<s_mc_stack_frame_t> result;
252 unw_cursor_t c = stack_context->cursor();
254 // TODO, check condition check (unw_init_local==0 means end of frame)
258 s_mc_stack_frame_t stack_frame;
260 stack_frame.unw_cursor = c;
265 unw_get_reg(&c, UNW_REG_IP, &ip);
266 unw_get_reg(&c, UNW_REG_SP, &sp);
271 // TODO, use real addresses in frame_t instead of fixing it here
273 simgrid::mc::Frame* frame = process->find_function(remote(ip));
274 stack_frame.frame = frame;
277 stack_frame.frame_name = frame->name;
278 stack_frame.frame_base = (unw_word_t)frame->frame_base(c);
280 stack_frame.frame_base = 0;
281 stack_frame.frame_name = std::string();
284 result.push_back(std::move(stack_frame));
286 /* Stop before context switch with maestro */
287 if (frame != nullptr && frame->name == "smx_ctx_wrapper")
290 int ret = unw_step(&c);
292 xbt_die("Unexpected end of stack.");
294 xbt_die("Error while unwinding stack");
297 xbt_assert(not result.empty(), "unw_init_local failed");
302 static void take_snapshot_stacks(simgrid::mc::Snapshot* snapshot)
304 for (auto const& stack : mc_model_checker->process().stack_areas()) {
305 s_mc_snapshot_stack_t st;
307 // Read the context from remote process:
308 unw_context_t context;
309 mc_model_checker->process().read_bytes(&context, sizeof(context), remote(stack.context));
311 st.context.initialize(&mc_model_checker->process(), &context);
313 st.stack_frames = unwind_stack_frames(&st.context);
314 st.local_variables = get_local_variables_values(st.stack_frames, stack.process_index);
315 st.process_index = stack.process_index;
317 unw_word_t sp = st.stack_frames[0].sp;
319 snapshot->stacks_.push_back(std::move(st));
321 size_t stack_size = (char*)stack.address + stack.size - (char*)sp;
322 snapshot->stack_sizes_.push_back(stack_size);
326 static void snapshot_handle_ignore(simgrid::mc::Snapshot* snapshot)
328 xbt_assert(snapshot->process());
331 for (auto const& region : mc_model_checker->process().ignored_regions()) {
332 s_mc_snapshot_ignored_data_t ignored_data;
333 ignored_data.start = (void*)region.addr;
334 ignored_data.data.resize(region.size);
335 // TODO, we should do this once per privatization segment:
336 snapshot->process()->read_bytes(ignored_data.data.data(), region.size, remote(region.addr),
337 simgrid::mc::ProcessIndexDisabled);
338 snapshot->ignored_data_.push_back(std::move(ignored_data));
342 for (auto const& region : mc_model_checker->process().ignored_regions())
343 snapshot->process()->clear_bytes(remote(region.addr), region.size);
346 static void snapshot_ignore_restore(simgrid::mc::Snapshot* snapshot)
348 for (auto const& ignored_data : snapshot->ignored_data_)
349 snapshot->process()->write_bytes(ignored_data.data.data(), ignored_data.data.size(), remote(ignored_data.start));
352 std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state)
354 XBT_DEBUG("Taking snapshot %i", num_state);
356 simgrid::mc::RemoteClient* mc_process = &mc_model_checker->process();
358 std::shared_ptr<simgrid::mc::Snapshot> snapshot = std::make_shared<simgrid::mc::Snapshot>(mc_process, num_state);
360 for (auto const& p : mc_model_checker->process().actors())
361 snapshot->enabled_processes_.insert(p.copy.getBuffer()->get_pid());
363 snapshot_handle_ignore(snapshot.get());
365 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
366 get_memory_regions(mc_process, snapshot.get());
368 snapshot->to_ignore_ = mc_model_checker->process().ignored_heap();
370 if (_sg_mc_max_visited_states > 0 || not _sg_mc_property_file.get().empty()) {
371 take_snapshot_stacks(snapshot.get());
373 snapshot->hash_ = simgrid::mc::hash(*snapshot);
376 snapshot_ignore_restore(snapshot.get());
380 static inline void restore_snapshot_regions(simgrid::mc::Snapshot* snapshot)
382 for (std::unique_ptr<simgrid::mc::RegionSnapshot> const& region : snapshot->snapshot_regions_) {
383 // For privatized, variables we decided it was not necessary to take the snapshot:
385 restore(region.get());
389 if (snapshot->privatization_index_ >= 0) {
390 // Fix the privatization mmap:
391 s_mc_message_restore_t message{MC_MESSAGE_RESTORE, snapshot->privatization_index_};
392 mc_model_checker->process().getChannel().send(message);
397 void restore_snapshot(std::shared_ptr<simgrid::mc::Snapshot> snapshot)
399 XBT_DEBUG("Restore snapshot %i", snapshot->num_state_);
400 restore_snapshot_regions(snapshot.get());
401 snapshot_ignore_restore(snapshot.get());
402 mc_model_checker->process().clear_cache();
406 } // namespace simgrid