Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Small cleanups in mc::Snapshot
[simgrid.git] / src / mc / sosp / Snapshot.cpp
1 /* Copyright (c) 2014-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/mc/sosp/Snapshot.hpp"
7 #include "src/mc/mc_config.hpp"
8 #include "src/mc/mc_hash.hpp"
9
10 #include <cstddef> /* std::size_t */
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_snapshot, mc, "Taking and restoring snapshots");
13 namespace simgrid::mc {
14 /************************************* Take Snapshot ************************************/
15 /****************************************************************************************/
16
17 void Snapshot::snapshot_regions(RemoteProcess* process)
18 {
19   snapshot_regions_.clear();
20
21   for (auto const& object_info : process->object_infos)
22     add_region(RegionType::Data, object_info.get(), object_info->start_rw, object_info->end_rw - object_info->start_rw);
23
24   const s_xbt_mheap_t* heap = process->get_heap();
25   void* start_heap = heap->base;
26   void* end_heap   = heap->breakval;
27
28   add_region(RegionType::Heap, nullptr, start_heap, (char*)end_heap - (char*)start_heap);
29   heap_bytes_used_ = mmalloc_get_bytes_used_remote(heap->heaplimit, process->get_malloc_info());
30 }
31
32 /** @brief Checks whether the variable is in scope for a given IP.
33  *
34  *  A variable may be defined only from a given value of IP.
35  *
36  *  @param var   Variable description
37  *  @param scope Scope description
38  *  @param ip    Instruction pointer
39  *  @return      true if the variable is valid
40  * */
41 static bool valid_variable(const simgrid::mc::Variable* var, simgrid::mc::Frame* scope, const void* ip)
42 {
43   // The variable is not yet valid:
44   if (scope->range.begin() + var->start_scope > (std::uint64_t)ip)
45     return false;
46   else
47     return true;
48 }
49
50 static void fill_local_variables_values(mc_stack_frame_t stack_frame, Frame* scope,
51                                         std::vector<s_local_variable_t>& result)
52 {
53   if (not scope || not scope->range.contain(stack_frame->ip))
54     return;
55
56   for (const Variable& current_variable : scope->variables) {
57     if (not valid_variable(&current_variable, scope, (void*)stack_frame->ip))
58       continue;
59
60     if (not current_variable.type) {
61       XBT_VERB("Ignore local variable without type: '%s' [%s]", current_variable.name.c_str(),
62                stack_frame->frame->name.c_str());
63       continue;
64     }
65
66     s_local_variable_t new_var;
67     new_var.subprogram = stack_frame->frame;
68     new_var.ip         = stack_frame->ip;
69     new_var.name       = current_variable.name;
70     new_var.type       = current_variable.type;
71     new_var.address    = nullptr;
72
73     if (current_variable.address != nullptr)
74       new_var.address = current_variable.address;
75     else if (not current_variable.location_list.empty()) {
76       dwarf::Location location = simgrid::dwarf::resolve(current_variable.location_list, current_variable.object_info,
77                                                          &(stack_frame->unw_cursor), (void*)stack_frame->frame_base,
78                                                          &mc_model_checker->get_remote_process());
79
80       xbt_assert(location.in_memory(), "Cannot handle non-address variable");
81       new_var.address = location.address();
82     } else
83       xbt_die("No address");
84
85     result.push_back(std::move(new_var));
86   }
87
88   // Recursive processing of nested scopes:
89   for (Frame& nested_scope : scope->scopes)
90     fill_local_variables_values(stack_frame, &nested_scope, result);
91 }
92
93 static std::vector<s_local_variable_t> get_local_variables_values(std::vector<s_mc_stack_frame_t>& stack_frames)
94 {
95   std::vector<s_local_variable_t> variables;
96   for (s_mc_stack_frame_t& stack_frame : stack_frames)
97     fill_local_variables_values(&stack_frame, stack_frame.frame, variables);
98   return variables;
99 }
100
101 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(UnwindContext* stack_context)
102 {
103   const RemoteProcess* process = &mc_model_checker->get_remote_process();
104   std::vector<s_mc_stack_frame_t> result;
105
106   unw_cursor_t c = stack_context->cursor();
107
108   // TODO, check condition check (unw_init_local==0 means end of frame)
109
110   while (true) {
111     s_mc_stack_frame_t stack_frame;
112
113     stack_frame.unw_cursor = c;
114
115     unw_word_t ip;
116     unw_word_t sp;
117
118     unw_get_reg(&c, UNW_REG_IP, &ip);
119     unw_get_reg(&c, UNW_REG_SP, &sp);
120
121     stack_frame.ip = ip;
122     stack_frame.sp = sp;
123
124     // TODO, use real addresses in frame_t instead of fixing it here
125
126     Frame* frame              = process->find_function(remote(ip));
127     stack_frame.frame         = frame;
128
129     if (frame) {
130       stack_frame.frame_name = frame->name;
131       stack_frame.frame_base = (unw_word_t)frame->frame_base(c);
132     } else {
133       stack_frame.frame_base = 0;
134       stack_frame.frame_name = std::string();
135     }
136
137     result.push_back(std::move(stack_frame));
138
139     /* Stop before context switch with maestro */
140     if (frame != nullptr && frame->name == "smx_ctx_wrapper")
141       break;
142
143     int ret = unw_step(&c);
144     xbt_assert(ret >= 0, "Error while unwinding stack");
145     xbt_assert(ret != 0, "Unexpected end of stack.");
146   }
147
148   xbt_assert(not result.empty(), "unw_init_local failed");
149
150   return result;
151 }
152
153 void Snapshot::snapshot_stacks(RemoteProcess* process)
154 {
155   for (auto const& stack : process->stack_areas()) {
156     s_mc_snapshot_stack_t st;
157
158     // Read the context from remote process:
159     unw_context_t context;
160     process->read_bytes(&context, sizeof(context), remote(stack.context));
161
162     st.context.initialize(process, &context);
163
164     st.stack_frames    = unwind_stack_frames(&st.context);
165     st.local_variables = get_local_variables_values(st.stack_frames);
166
167     unw_word_t sp = st.stack_frames[0].sp;
168
169     stacks_.push_back(std::move(st));
170
171     size_t stack_size = (char*)stack.address + stack.size - (char*)sp;
172     stack_sizes_.push_back(stack_size);
173   }
174 }
175
176 void Snapshot::handle_ignore()
177 {
178   xbt_assert(get_remote_process());
179
180   // Copy the memory:
181   for (auto const& region : get_remote_process()->ignored_regions()) {
182     s_mc_snapshot_ignored_data_t ignored_data;
183     ignored_data.start = (void*)region.addr;
184     ignored_data.data.resize(region.size);
185     // TODO, we should do this once per privatization segment:
186     get_remote_process()->read_bytes(ignored_data.data.data(), region.size, remote(region.addr));
187     ignored_data_.push_back(std::move(ignored_data));
188   }
189
190   // Zero the memory:
191   for (auto const& region : get_remote_process()->ignored_regions())
192     get_remote_process()->clear_bytes(remote(region.addr), region.size);
193 }
194
195 void Snapshot::ignore_restore() const
196 {
197   for (auto const& ignored_data : ignored_data_)
198     get_remote_process()->write_bytes(ignored_data.data.data(), ignored_data.data.size(), remote(ignored_data.start));
199 }
200
201 Snapshot::Snapshot(long num_state, RemoteProcess* process) : AddressSpace(process), num_state_(num_state)
202 {
203   XBT_DEBUG("Taking snapshot %ld", num_state);
204
205   handle_ignore();
206
207   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
208   snapshot_regions(process);
209
210   to_ignore_ = process->ignored_heap();
211
212   if (_sg_mc_max_visited_states > 0 || not _sg_mc_property_file.get().empty()) {
213     snapshot_stacks(process);
214     hash_ = simgrid::mc::hash(*this);
215   }
216
217   ignore_restore();
218 }
219
220 void Snapshot::add_region(RegionType type, ObjectInformation* object_info, void* start_addr, std::size_t size)
221 {
222   if (type == RegionType::Data)
223     xbt_assert(object_info, "Missing object info for object.");
224   else if (type == RegionType::Heap)
225     xbt_assert(not object_info, "Unexpected object info for heap region.");
226
227   auto* region = new Region(type, start_addr, size);
228   region->object_info(object_info);
229   snapshot_regions_.push_back(std::unique_ptr<Region>(region));
230 }
231
232 void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, ReadOptions options) const
233 {
234   const Region* region = this->get_region((void*)address.address());
235   if (region) {
236     void* res = region->read(buffer, (void*)address.address(), size);
237     if (buffer == res || options & ReadOptions::lazy())
238       return res;
239     else {
240       memcpy(buffer, res, size);
241       return buffer;
242     }
243   } else
244     return this->get_remote_process()->read_bytes(buffer, size, address, options);
245 }
246 /** @brief Find the snapshotted region from a pointer
247  *
248  *  @param addr     Pointer
249  * */
250 Region* Snapshot::get_region(const void* addr) const
251 {
252   size_t n = snapshot_regions_.size();
253   for (size_t i = 0; i != n; ++i) {
254     Region* region = snapshot_regions_[i].get();
255     if (not(region && region->contain(simgrid::mc::remote(addr))))
256       continue;
257
258     return region;
259   }
260
261   return nullptr;
262 }
263
264 /** @brief Find the snapshotted region from a pointer, with a hinted_region */
265 Region* Snapshot::get_region(const void* addr, Region* hinted_region) const
266 {
267   if (hinted_region->contain(simgrid::mc::remote(addr)))
268     return hinted_region;
269   else
270     return get_region(addr);
271 }
272
273 void Snapshot::restore(RemoteProcess* process) const
274 {
275   XBT_DEBUG("Restore snapshot %ld", num_state_);
276
277   // Restore regions
278   for (std::unique_ptr<Region> const& region : snapshot_regions_) {
279     if (region) // privatized variables are not snapshotted
280       region.get()->restore();
281   }
282
283   ignore_restore();
284   process->clear_cache();
285 }
286
287 } // namespace simgrid::mc