Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mc: kill some useless includes
[simgrid.git] / src / mc / sosp / mc_checkpoint.cpp
1 /* Copyright (c) 2008-2019. 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 #ifndef WIN32
7 #include <sys/mman.h>
8 #endif
9
10 #include "xbt/file.hpp"
11
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"
17
18 using simgrid::mc::remote;
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc, "Logging specific to mc_checkpoint");
21
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)
25
26 namespace simgrid {
27 namespace mc {
28
29 /************************************  Free functions **************************************/
30 /*****************************************************************************************/
31
32 /** @brief Restore a region from a snapshot
33  *
34  *  @param region     Target region
35  */
36 static void restore(RegionSnapshot* region)
37 {
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());
41       break;
42
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());
46
47       for (size_t i = 0; i != region->page_data().page_count(); ++i) {
48         void* target_page =
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));
52       }
53
54       break;
55
56     case simgrid::mc::StorageType::Privatized:
57       for (auto& p : region->privatized_data())
58         restore(p.get());
59       break;
60
61     default: // includes StorageType::NoData
62       xbt_die("Storage type not supported");
63       break;
64   }
65 }
66
67 static void get_memory_regions(simgrid::mc::RemoteClient* process, simgrid::mc::Snapshot* snapshot)
68 {
69   snapshot->snapshot_regions_.clear();
70
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);
74
75   xbt_mheap_t heap = process->get_heap();
76   void* start_heap = heap->base;
77   void* end_heap   = heap->breakval;
78
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;
83
84 #if HAVE_SMPI
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_));
89 #endif
90 }
91
92 /** @brief Fills the position of the segments (executable, read-only, read/write).
93  * */
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)
96 {
97   std::string name = simgrid::xbt::Path(result->file_name).get_base_name();
98
99   for (size_t i = 0; i < maps.size(); ++i) {
100     simgrid::xbt::VmMap const& reg = maps[i];
101     if (maps[i].pathname.empty())
102       continue;
103     std::string map_basename = simgrid::xbt::Path(maps[i].pathname).get_base_name();
104     if (map_basename != name)
105       continue;
106
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;
112
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;
117     }
118
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;
125
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;
131       }
132     }
133
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;
139     }
140   }
141
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;
147
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;
153
154   xbt_assert(result->start_exec || result->start_rw || result->start_ro);
155 }
156
157 /************************************* Take Snapshot ************************************/
158 /****************************************************************************************/
159
160 /** @brief Checks whether the variable is in scope for a given IP.
161  *
162  *  A variable may be defined only from a given value of IP.
163  *
164  *  @param var   Variable description
165  *  @param scope Scope description
166  *  @param ip    Instruction pointer
167  *  @return      true if the variable is valid
168  * */
169 static bool valid_variable(simgrid::mc::Variable* var, simgrid::mc::Frame* scope, const void* ip)
170 {
171   // The variable is not yet valid:
172   if (scope->range.begin() + var->start_scope > (std::uint64_t)ip)
173     return false;
174   else
175     return true;
176 }
177
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)
180 {
181   if (not scope || not scope->range.contain(stack_frame->ip))
182     return;
183
184   for (simgrid::mc::Variable& current_variable : scope->variables) {
185
186     if (not valid_variable(&current_variable, scope, (void*)stack_frame->ip))
187       continue;
188
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;
195
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);
202
203       if (not location.in_memory())
204         xbt_die("Cannot handle non-address variable");
205       new_var.address = location.address();
206
207     } else
208       xbt_die("No address");
209
210     result.push_back(std::move(new_var));
211   }
212
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);
216 }
217
218 static std::vector<s_local_variable_t> get_local_variables_values(std::vector<s_mc_stack_frame_t>& stack_frames,
219                                                                   int process_index)
220 {
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);
224   return variables;
225 }
226
227 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(simgrid::mc::UnwindContext* stack_context)
228 {
229   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
230   std::vector<s_mc_stack_frame_t> result;
231
232   unw_cursor_t c = stack_context->cursor();
233
234   // TODO, check condition check (unw_init_local==0 means end of frame)
235
236   while (1) {
237
238     s_mc_stack_frame_t stack_frame;
239
240     stack_frame.unw_cursor = c;
241
242     unw_word_t ip;
243     unw_word_t sp;
244
245     unw_get_reg(&c, UNW_REG_IP, &ip);
246     unw_get_reg(&c, UNW_REG_SP, &sp);
247
248     stack_frame.ip = ip;
249     stack_frame.sp = sp;
250
251     // TODO, use real addresses in frame_t instead of fixing it here
252
253     simgrid::mc::Frame* frame = process->find_function(remote(ip));
254     stack_frame.frame         = frame;
255
256     if (frame) {
257       stack_frame.frame_name = frame->name;
258       stack_frame.frame_base = (unw_word_t)frame->frame_base(c);
259     } else {
260       stack_frame.frame_base = 0;
261       stack_frame.frame_name = std::string();
262     }
263
264     result.push_back(std::move(stack_frame));
265
266     /* Stop before context switch with maestro */
267     if (frame != nullptr && frame->name == "smx_ctx_wrapper")
268       break;
269
270     int ret = unw_step(&c);
271     if (ret == 0)
272       xbt_die("Unexpected end of stack.");
273     else if (ret < 0)
274       xbt_die("Error while unwinding stack");
275   }
276
277   xbt_assert(not result.empty(), "unw_init_local failed");
278
279   return result;
280 }
281
282 static void take_snapshot_stacks(simgrid::mc::Snapshot* snapshot)
283 {
284   for (auto const& stack : mc_model_checker->process().stack_areas()) {
285     s_mc_snapshot_stack_t st;
286
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));
290
291     st.context.initialize(&mc_model_checker->process(), &context);
292
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;
296
297     unw_word_t sp = st.stack_frames[0].sp;
298
299     snapshot->stacks_.push_back(std::move(st));
300
301     size_t stack_size = (char*)stack.address + stack.size - (char*)sp;
302     snapshot->stack_sizes_.push_back(stack_size);
303   }
304 }
305
306 static void snapshot_handle_ignore(simgrid::mc::Snapshot* snapshot)
307 {
308   xbt_assert(snapshot->process());
309
310   // Copy the memory:
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));
319   }
320
321   // Zero the memory:
322   for (auto const& region : mc_model_checker->process().ignored_regions())
323     snapshot->process()->clear_bytes(remote(region.addr), region.size);
324 }
325
326 static void snapshot_ignore_restore(simgrid::mc::Snapshot* snapshot)
327 {
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));
330 }
331
332 std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state)
333 {
334   XBT_DEBUG("Taking snapshot %i", num_state);
335
336   simgrid::mc::RemoteClient* mc_process = &mc_model_checker->process();
337
338   std::shared_ptr<simgrid::mc::Snapshot> snapshot = std::make_shared<simgrid::mc::Snapshot>(mc_process, num_state);
339
340   for (auto const& p : mc_model_checker->process().actors())
341     snapshot->enabled_processes_.insert(p.copy.getBuffer()->get_pid());
342
343   snapshot_handle_ignore(snapshot.get());
344
345   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
346   get_memory_regions(mc_process, snapshot.get());
347
348   snapshot->to_ignore_ = mc_model_checker->process().ignored_heap();
349
350   if (_sg_mc_max_visited_states > 0 || not _sg_mc_property_file.get().empty()) {
351     take_snapshot_stacks(snapshot.get());
352     if (_sg_mc_hash)
353       snapshot->hash_ = simgrid::mc::hash(*snapshot);
354   }
355
356   snapshot_ignore_restore(snapshot.get());
357   return snapshot;
358 }
359
360 static inline void restore_snapshot_regions(simgrid::mc::Snapshot* snapshot)
361 {
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:
364     if (region)
365       restore(region.get());
366   }
367
368 #if HAVE_SMPI
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);
373   }
374 #endif
375 }
376
377 void restore_snapshot(std::shared_ptr<simgrid::mc::Snapshot> snapshot)
378 {
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();
383 }
384
385 } // namespace mc
386 } // namespace simgrid