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. */
10 #include "xbt/file.hpp"
12 #include "src/mc/mc_config.hpp"
13 #include "src/mc/mc_hash.hpp"
14 #include "src/mc/mc_private.hpp"
15 #include "src/mc/mc_smx.hpp"
16 #include "src/mc/sosp/mc_snapshot.hpp"
18 using simgrid::mc::remote;
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc, "Logging specific to mc_checkpoint");
22 #define PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC)
23 #define PROT_RW (PROT_READ | PROT_WRITE)
24 #define PROT_RX (PROT_READ | PROT_EXEC)
29 /************************************ Free functions **************************************/
30 /*****************************************************************************************/
32 /** @brief Restore a region from a snapshot
34 * @param region Target region
36 static void restore(RegionSnapshot* region)
38 switch (region->storage_type()) {
39 case simgrid::mc::StorageType::Flat:
40 mc_model_checker->process().write_bytes(region->flat_data().get(), region->size(), region->permanent_address());
43 case simgrid::mc::StorageType::Chunked:
44 xbt_assert(((region->permanent_address().address()) & (xbt_pagesize - 1)) == 0, "Not at the beginning of a page");
45 xbt_assert(simgrid::mc::mmu::chunk_count(region->size()) == region->page_data().page_count());
47 for (size_t i = 0; i != region->page_data().page_count(); ++i) {
49 (void*)simgrid::mc::mmu::join(i, (std::uintptr_t)(void*)region->permanent_address().address());
50 const void* source_page = region->page_data().page(i);
51 mc_model_checker->process().write_bytes(source_page, xbt_pagesize, remote(target_page));
56 case simgrid::mc::StorageType::Privatized:
57 for (auto& p : region->privatized_data())
61 default: // includes StorageType::NoData
62 xbt_die("Storage type not supported");
67 static void get_memory_regions(simgrid::mc::RemoteClient* process, simgrid::mc::Snapshot* snapshot)
69 snapshot->snapshot_regions_.clear();
71 for (auto const& object_info : process->object_infos)
72 snapshot->add_region(simgrid::mc::RegionType::Data, object_info.get(), object_info->start_rw, object_info->start_rw,
73 object_info->end_rw - object_info->start_rw);
75 xbt_mheap_t heap = process->get_heap();
76 void* start_heap = heap->base;
77 void* end_heap = heap->breakval;
79 snapshot->add_region(simgrid::mc::RegionType::Heap, nullptr, start_heap, start_heap,
80 (char*)end_heap - (char*)start_heap);
81 snapshot->heap_bytes_used_ = mmalloc_get_bytes_used_remote(heap->heaplimit, process->get_malloc_info());
82 snapshot->privatization_index_ = simgrid::mc::ProcessIndexMissing;
85 if (mc_model_checker->process().privatized() && MC_smpi_process_count())
86 // snapshot->privatization_index = smpi_loaded_page
87 mc_model_checker->process().read_variable("smpi_loaded_page", &snapshot->privatization_index_,
88 sizeof(snapshot->privatization_index_));
92 /** @brief Fills the position of the segments (executable, read-only, read/write).
94 // TODO, use the ELF segment information for more robustness
95 void find_object_address(std::vector<simgrid::xbt::VmMap> const& maps, simgrid::mc::ObjectInformation* result)
97 std::string name = simgrid::xbt::Path(result->file_name).get_base_name();
99 for (size_t i = 0; i < maps.size(); ++i) {
100 simgrid::xbt::VmMap const& reg = maps[i];
101 if (maps[i].pathname.empty())
103 std::string map_basename = simgrid::xbt::Path(maps[i].pathname).get_base_name();
104 if (map_basename != name)
107 // This is the non-GNU_RELRO-part of the data segment:
108 if (reg.prot == PROT_RW) {
109 xbt_assert(not result->start_rw, "Multiple read-write segments for %s, not supported", maps[i].pathname.c_str());
110 result->start_rw = (char*)reg.start_addr;
111 result->end_rw = (char*)reg.end_addr;
113 // The next VMA might be end of the data segment:
114 if (i + 1 < maps.size() && maps[i + 1].pathname.empty() && maps[i + 1].prot == PROT_RW &&
115 maps[i + 1].start_addr == reg.end_addr)
116 result->end_rw = (char*)maps[i + 1].end_addr;
119 // This is the text segment:
120 else if (reg.prot == PROT_RX) {
121 xbt_assert(not result->start_exec, "Multiple executable segments for %s, not supported",
122 maps[i].pathname.c_str());
123 result->start_exec = (char*)reg.start_addr;
124 result->end_exec = (char*)reg.end_addr;
126 // The next VMA might be end of the data segment:
127 if (i + 1 < maps.size() && maps[i + 1].pathname.empty() && maps[i + 1].prot == PROT_RW &&
128 maps[i + 1].start_addr == reg.end_addr) {
129 result->start_rw = (char*)maps[i + 1].start_addr;
130 result->end_rw = (char*)maps[i + 1].end_addr;
134 // This is the GNU_RELRO-part of the data segment:
135 else if (reg.prot == PROT_READ) {
136 xbt_assert(not result->start_ro, "Multiple read only segments for %s, not supported", maps[i].pathname.c_str());
137 result->start_ro = (char*)reg.start_addr;
138 result->end_ro = (char*)reg.end_addr;
142 result->start = result->start_rw;
143 if ((const void*)result->start_ro < result->start)
144 result->start = result->start_ro;
145 if ((const void*)result->start_exec < result->start)
146 result->start = result->start_exec;
148 result->end = result->end_rw;
149 if (result->end_ro && (const void*)result->end_ro > result->end)
150 result->end = result->end_ro;
151 if (result->end_exec && (const void*)result->end_exec > result->end)
152 result->end = result->end_exec;
154 xbt_assert(result->start_exec || result->start_rw || result->start_ro);
157 /************************************* Take Snapshot ************************************/
158 /****************************************************************************************/
160 /** @brief Checks whether the variable is in scope for a given IP.
162 * A variable may be defined only from a given value of IP.
164 * @param var Variable description
165 * @param scope Scope description
166 * @param ip Instruction pointer
167 * @return true if the variable is valid
169 static bool valid_variable(simgrid::mc::Variable* var, simgrid::mc::Frame* scope, const void* ip)
171 // The variable is not yet valid:
172 if (scope->range.begin() + var->start_scope > (std::uint64_t)ip)
178 static void fill_local_variables_values(mc_stack_frame_t stack_frame, simgrid::mc::Frame* scope, int process_index,
179 std::vector<s_local_variable_t>& result)
181 if (not scope || not scope->range.contain(stack_frame->ip))
184 for (simgrid::mc::Variable& current_variable : scope->variables) {
186 if (not valid_variable(¤t_variable, scope, (void*)stack_frame->ip))
189 s_local_variable_t new_var;
190 new_var.subprogram = stack_frame->frame;
191 new_var.ip = stack_frame->ip;
192 new_var.name = current_variable.name;
193 new_var.type = current_variable.type;
194 new_var.address = nullptr;
196 if (current_variable.address != nullptr)
197 new_var.address = current_variable.address;
198 else if (not current_variable.location_list.empty()) {
199 simgrid::dwarf::Location location = simgrid::dwarf::resolve(
200 current_variable.location_list, current_variable.object_info, &(stack_frame->unw_cursor),
201 (void*)stack_frame->frame_base, &mc_model_checker->process(), process_index);
203 if (not location.in_memory())
204 xbt_die("Cannot handle non-address variable");
205 new_var.address = location.address();
208 xbt_die("No address");
210 result.push_back(std::move(new_var));
213 // Recursive processing of nested scopes:
214 for (simgrid::mc::Frame& nested_scope : scope->scopes)
215 fill_local_variables_values(stack_frame, &nested_scope, process_index, result);
218 static std::vector<s_local_variable_t> get_local_variables_values(std::vector<s_mc_stack_frame_t>& stack_frames,
221 std::vector<s_local_variable_t> variables;
222 for (s_mc_stack_frame_t& stack_frame : stack_frames)
223 fill_local_variables_values(&stack_frame, stack_frame.frame, process_index, variables);
227 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(simgrid::mc::UnwindContext* stack_context)
229 simgrid::mc::RemoteClient* process = &mc_model_checker->process();
230 std::vector<s_mc_stack_frame_t> result;
232 unw_cursor_t c = stack_context->cursor();
234 // TODO, check condition check (unw_init_local==0 means end of frame)
238 s_mc_stack_frame_t stack_frame;
240 stack_frame.unw_cursor = c;
245 unw_get_reg(&c, UNW_REG_IP, &ip);
246 unw_get_reg(&c, UNW_REG_SP, &sp);
251 // TODO, use real addresses in frame_t instead of fixing it here
253 simgrid::mc::Frame* frame = process->find_function(remote(ip));
254 stack_frame.frame = frame;
257 stack_frame.frame_name = frame->name;
258 stack_frame.frame_base = (unw_word_t)frame->frame_base(c);
260 stack_frame.frame_base = 0;
261 stack_frame.frame_name = std::string();
264 result.push_back(std::move(stack_frame));
266 /* Stop before context switch with maestro */
267 if (frame != nullptr && frame->name == "smx_ctx_wrapper")
270 int ret = unw_step(&c);
272 xbt_die("Unexpected end of stack.");
274 xbt_die("Error while unwinding stack");
277 xbt_assert(not result.empty(), "unw_init_local failed");
282 static void take_snapshot_stacks(simgrid::mc::Snapshot* snapshot)
284 for (auto const& stack : mc_model_checker->process().stack_areas()) {
285 s_mc_snapshot_stack_t st;
287 // Read the context from remote process:
288 unw_context_t context;
289 mc_model_checker->process().read_bytes(&context, sizeof(context), remote(stack.context));
291 st.context.initialize(&mc_model_checker->process(), &context);
293 st.stack_frames = unwind_stack_frames(&st.context);
294 st.local_variables = get_local_variables_values(st.stack_frames, stack.process_index);
295 st.process_index = stack.process_index;
297 unw_word_t sp = st.stack_frames[0].sp;
299 snapshot->stacks_.push_back(std::move(st));
301 size_t stack_size = (char*)stack.address + stack.size - (char*)sp;
302 snapshot->stack_sizes_.push_back(stack_size);
306 static void snapshot_handle_ignore(simgrid::mc::Snapshot* snapshot)
308 xbt_assert(snapshot->process());
311 for (auto const& region : mc_model_checker->process().ignored_regions()) {
312 s_mc_snapshot_ignored_data_t ignored_data;
313 ignored_data.start = (void*)region.addr;
314 ignored_data.data.resize(region.size);
315 // TODO, we should do this once per privatization segment:
316 snapshot->process()->read_bytes(ignored_data.data.data(), region.size, remote(region.addr),
317 simgrid::mc::ProcessIndexDisabled);
318 snapshot->ignored_data_.push_back(std::move(ignored_data));
322 for (auto const& region : mc_model_checker->process().ignored_regions())
323 snapshot->process()->clear_bytes(remote(region.addr), region.size);
326 static void snapshot_ignore_restore(simgrid::mc::Snapshot* snapshot)
328 for (auto const& ignored_data : snapshot->ignored_data_)
329 snapshot->process()->write_bytes(ignored_data.data.data(), ignored_data.data.size(), remote(ignored_data.start));
332 std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state)
334 XBT_DEBUG("Taking snapshot %i", num_state);
336 simgrid::mc::RemoteClient* mc_process = &mc_model_checker->process();
338 std::shared_ptr<simgrid::mc::Snapshot> snapshot = std::make_shared<simgrid::mc::Snapshot>(mc_process, num_state);
340 for (auto const& p : mc_model_checker->process().actors())
341 snapshot->enabled_processes_.insert(p.copy.getBuffer()->get_pid());
343 snapshot_handle_ignore(snapshot.get());
345 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
346 get_memory_regions(mc_process, snapshot.get());
348 snapshot->to_ignore_ = mc_model_checker->process().ignored_heap();
350 if (_sg_mc_max_visited_states > 0 || not _sg_mc_property_file.get().empty()) {
351 take_snapshot_stacks(snapshot.get());
353 snapshot->hash_ = simgrid::mc::hash(*snapshot);
356 snapshot_ignore_restore(snapshot.get());
360 static inline void restore_snapshot_regions(simgrid::mc::Snapshot* snapshot)
362 for (std::unique_ptr<simgrid::mc::RegionSnapshot> const& region : snapshot->snapshot_regions_) {
363 // For privatized, variables we decided it was not necessary to take the snapshot:
365 restore(region.get());
369 if (snapshot->privatization_index_ >= 0) {
370 // Fix the privatization mmap:
371 s_mc_message_restore_t message{MC_MESSAGE_RESTORE, snapshot->privatization_index_};
372 mc_model_checker->process().getChannel().send(message);
377 void restore_snapshot(std::shared_ptr<simgrid::mc::Snapshot> snapshot)
379 XBT_DEBUG("Restore snapshot %i", snapshot->num_state_);
380 restore_snapshot_regions(snapshot.get());
381 snapshot_ignore_restore(snapshot.get());
382 mc_model_checker->process().clear_cache();
386 } // namespace simgrid