1 /* Copyright (c) 2014-2023. 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 "src/mc/sosp/Snapshot.hpp"
7 #include "src/mc/mc_config.hpp"
9 #include <cstddef> /* std::size_t */
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_snapshot, mc, "Taking and restoring snapshots");
12 namespace simgrid::mc {
13 /************************************* Take Snapshot ************************************/
14 /****************************************************************************************/
16 void Snapshot::snapshot_regions(RemoteProcessMemory& process_memory)
18 snapshot_regions_.clear();
20 for (auto const& object_info : process_memory.object_infos)
21 add_region(RegionType::Data, object_info.get(), object_info->start_rw, object_info->end_rw - object_info->start_rw);
23 const s_xbt_mheap_t* heap = process_memory.get_heap();
24 void* start_heap = heap->base;
25 void* end_heap = heap->breakval;
27 add_region(RegionType::Heap, nullptr, start_heap, (char*)end_heap - (char*)start_heap);
28 heap_bytes_used_ = mmalloc_get_bytes_used_remote(heap->heaplimit, process_memory.get_malloc_info());
31 /** @brief Checks whether the variable is in scope for a given IP.
33 * A variable may be defined only from a given value of IP.
35 * @param var Variable description
36 * @param scope Scope description
37 * @param ip Instruction pointer
38 * @return true if the variable is valid
40 static bool valid_variable(const simgrid::mc::Variable* var, simgrid::mc::Frame* scope, const void* ip)
42 // The variable is not yet valid:
43 if (scope->range.begin() + var->start_scope > (std::uint64_t)ip)
49 static void fill_local_variables_values(mc_stack_frame_t stack_frame, Frame* scope,
50 std::vector<s_local_variable_t>& result)
52 if (not scope || not scope->range.contain(stack_frame->ip))
55 for (const Variable& current_variable : scope->variables) {
56 if (not valid_variable(¤t_variable, scope, (void*)stack_frame->ip))
59 if (not current_variable.type) {
60 XBT_VERB("Ignore local variable without type: '%s' [%s]", current_variable.name.c_str(),
61 stack_frame->frame->name.c_str());
65 s_local_variable_t new_var;
66 new_var.subprogram = stack_frame->frame;
67 new_var.ip = stack_frame->ip;
68 new_var.name = current_variable.name;
69 new_var.type = current_variable.type;
70 new_var.address = nullptr;
72 if (current_variable.address != nullptr)
73 new_var.address = current_variable.address;
74 else if (not current_variable.location_list.empty()) {
75 dwarf::Location location = simgrid::dwarf::resolve(current_variable.location_list, current_variable.object_info,
76 &(stack_frame->unw_cursor), (void*)stack_frame->frame_base,
77 &mc_model_checker->get_remote_process_memory());
79 xbt_assert(location.in_memory(), "Cannot handle non-address variable");
80 new_var.address = location.address();
82 xbt_die("No address");
84 result.push_back(std::move(new_var));
87 // Recursive processing of nested scopes:
88 for (Frame& nested_scope : scope->scopes)
89 fill_local_variables_values(stack_frame, &nested_scope, result);
92 static std::vector<s_local_variable_t> get_local_variables_values(std::vector<s_mc_stack_frame_t>& stack_frames)
94 std::vector<s_local_variable_t> variables;
95 for (s_mc_stack_frame_t& stack_frame : stack_frames)
96 fill_local_variables_values(&stack_frame, stack_frame.frame, variables);
100 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(UnwindContext* stack_context)
102 const RemoteProcessMemory* process_memory = &mc_model_checker->get_remote_process_memory();
103 std::vector<s_mc_stack_frame_t> result;
105 unw_cursor_t c = stack_context->cursor();
107 // TODO, check condition check (unw_init_local==0 means end of frame)
110 s_mc_stack_frame_t stack_frame;
112 stack_frame.unw_cursor = c;
117 unw_get_reg(&c, UNW_REG_IP, &ip);
118 unw_get_reg(&c, UNW_REG_SP, &sp);
123 // TODO, use real addresses in frame_t instead of fixing it here
125 Frame* frame = process_memory->find_function(remote(ip));
126 stack_frame.frame = frame;
129 stack_frame.frame_name = frame->name;
130 stack_frame.frame_base = (unw_word_t)frame->frame_base(c);
132 stack_frame.frame_base = 0;
133 stack_frame.frame_name = "";
136 result.push_back(std::move(stack_frame));
138 /* Stop before context switch with maestro */
139 if (frame != nullptr && frame->name == "smx_ctx_wrapper")
142 int ret = unw_step(&c);
143 xbt_assert(ret >= 0, "Error while unwinding stack");
144 xbt_assert(ret != 0, "Unexpected end of stack.");
147 xbt_assert(not result.empty(), "unw_init_local failed");
152 void Snapshot::snapshot_stacks(RemoteProcessMemory& process_memory)
154 for (auto const& stack : process_memory.stack_areas()) {
155 s_mc_snapshot_stack_t st;
157 // Read the context from remote process memory:
158 unw_context_t context;
159 process_memory.read_bytes(&context, sizeof(context), remote(stack.context));
161 st.context.initialize(process_memory, &context);
163 st.stack_frames = unwind_stack_frames(&st.context);
164 st.local_variables = get_local_variables_values(st.stack_frames);
166 unw_word_t sp = st.stack_frames[0].sp;
168 stacks_.push_back(std::move(st));
170 size_t stack_size = (char*)stack.address + stack.size - (char*)sp;
171 stack_sizes_.push_back(stack_size);
175 void Snapshot::handle_ignore()
177 xbt_assert(get_remote_process_memory());
180 for (auto const& region : get_remote_process_memory()->ignored_regions()) {
181 s_mc_snapshot_ignored_data_t ignored_data;
182 ignored_data.start = (void*)region.addr;
183 ignored_data.data.resize(region.size);
184 // TODO, we should do this once per privatization segment:
185 get_remote_process_memory()->read_bytes(ignored_data.data.data(), region.size, remote(region.addr));
186 ignored_data_.push_back(std::move(ignored_data));
190 for (auto const& region : get_remote_process_memory()->ignored_regions())
191 get_remote_process_memory()->clear_bytes(remote(region.addr), region.size);
194 void Snapshot::ignore_restore() const
196 for (auto const& ignored_data : ignored_data_)
197 get_remote_process_memory()->write_bytes(ignored_data.data.data(), ignored_data.data.size(),
198 remote(ignored_data.start));
201 Snapshot::Snapshot(long num_state, PageStore& store, RemoteProcessMemory& memory)
202 : AddressSpace(memory), page_store_(store), num_state_(num_state)
204 XBT_DEBUG("Taking snapshot %ld", num_state);
208 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
209 snapshot_regions(memory);
211 to_ignore_ = memory.ignored_heap();
213 if (_sg_mc_max_visited_states > 0 || not _sg_mc_property_file.get().empty()) {
214 snapshot_stacks(memory);
215 hash_ = this->do_hash();
221 void Snapshot::add_region(RegionType type, ObjectInformation* object_info, void* start_addr, std::size_t size)
223 if (type == RegionType::Data)
224 xbt_assert(object_info, "Missing object info for object.");
225 else if (type == RegionType::Heap)
226 xbt_assert(not object_info, "Unexpected object info for heap region.");
228 auto* region = new Region(page_store_, type, start_addr, size);
229 region->object_info(object_info);
230 snapshot_regions_.push_back(std::unique_ptr<Region>(region));
233 void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, ReadOptions options) const
235 const Region* region = this->get_region((void*)address.address());
237 void* res = region->read(buffer, (void*)address.address(), size);
238 if (buffer == res || options & ReadOptions::lazy())
241 memcpy(buffer, res, size);
245 return this->get_remote_process_memory()->read_bytes(buffer, size, address, options);
247 /** @brief Find the snapshotted region from a pointer
249 * @param addr Pointer
251 Region* Snapshot::get_region(const void* addr) const
253 size_t n = snapshot_regions_.size();
254 for (size_t i = 0; i != n; ++i) {
255 Region* region = snapshot_regions_[i].get();
256 if (not(region && region->contain(simgrid::mc::remote(addr))))
265 /** @brief Find the snapshotted region from a pointer, with a hinted_region */
266 Region* Snapshot::get_region(const void* addr, Region* hinted_region) const
268 if (hinted_region->contain(simgrid::mc::remote(addr)))
269 return hinted_region;
271 return get_region(addr);
274 void Snapshot::restore(RemoteProcessMemory& memory) const
276 XBT_DEBUG("Restore snapshot %ld", num_state_);
279 for (std::unique_ptr<Region> const& region : snapshot_regions_) {
280 if (region) // privatized variables are not snapshotted
281 region.get()->restore();
285 memory.clear_cache();
288 /* ----------- Hashing logic -------------- */
290 hash_type state_ = 5381LL;
293 template <class T> void update(T& x) { state_ = (state_ << 5) + state_ + x; }
294 hash_type value() const { return state_; }
296 hash_type Snapshot::do_hash() const
298 XBT_DEBUG("START hash %ld", num_state_);
304 // * basic stack frame information
305 // * stack frame local variables
306 XBT_DEBUG("END hash %ld", num_state_);
310 } // namespace simgrid::mc