Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9951c16294971989cae997d0b69ede210c28ba04
[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 #include <unistd.h>
7
8 #ifndef WIN32
9 #include <sys/mman.h>
10 #endif
11
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"
18
19 #include "src/xbt/mmalloc/mmprivate.h"
20
21 #include "src/simix/smx_private.hpp"
22
23 #include <libelf.h>
24 #include <libunwind.h>
25
26 #include "src/mc/mc_private.hpp"
27 #include <mc/mc.h>
28
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/mc_unw.hpp"
34 #include "src/mc/remote/mc_protocol.h"
35 #include "src/mc/sosp/mc_snapshot.hpp"
36
37 #include "src/mc/Frame.hpp"
38 #include "src/mc/ObjectInformation.hpp"
39 #include "src/mc/Variable.hpp"
40 #include "src/mc/sosp/RegionSnapshot.hpp"
41
42 using simgrid::mc::remote;
43
44 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc, "Logging specific to mc_checkpoint");
45
46 #define PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC)
47 #define PROT_RW (PROT_READ | PROT_WRITE)
48 #define PROT_RX (PROT_READ | PROT_EXEC)
49
50 namespace simgrid {
51 namespace mc {
52
53 /************************************  Free functions **************************************/
54 /*****************************************************************************************/
55
56 /** @brief Restore a region from a snapshot
57  *
58  *  @param region     Target region
59  */
60 static void restore(RegionSnapshot* region)
61 {
62   switch (region->storage_type()) {
63     case simgrid::mc::StorageType::Flat:
64       mc_model_checker->process().write_bytes(region->flat_data().get(), region->size(), region->permanent_address());
65       break;
66
67     case simgrid::mc::StorageType::Chunked:
68       xbt_assert(((region->permanent_address().address()) & (xbt_pagesize - 1)) == 0, "Not at the beginning of a page");
69       xbt_assert(simgrid::mc::mmu::chunkCount(region->size()) == region->page_data().page_count());
70
71       for (size_t i = 0; i != region->page_data().page_count(); ++i) {
72         void* target_page =
73             (void*)simgrid::mc::mmu::join(i, (std::uintptr_t)(void*)region->permanent_address().address());
74         const void* source_page = region->page_data().page(i);
75         mc_model_checker->process().write_bytes(source_page, xbt_pagesize, remote(target_page));
76       }
77
78       break;
79
80     case simgrid::mc::StorageType::Privatized:
81       for (auto& p : region->privatized_data())
82         restore(&p);
83       break;
84
85     default: // includes StorageType::NoData
86       xbt_die("Storage type not supported");
87       break;
88   }
89 }
90
91 #if HAVE_SMPI
92 RegionSnapshot privatized_region(RegionType region_type, void* start_addr, void* permanent_addr, std::size_t size)
93 {
94   size_t process_count = MC_smpi_process_count();
95
96   // Read smpi_privatization_regions from MCed:
97   smpi_privatization_region_t remote_smpi_privatization_regions;
98   mc_model_checker->process().read_variable("smpi_privatization_regions", &remote_smpi_privatization_regions,
99                                             sizeof(remote_smpi_privatization_regions));
100   s_smpi_privatization_region_t privatization_regions[process_count];
101   mc_model_checker->process().read_bytes(&privatization_regions, sizeof(privatization_regions),
102                                          remote(remote_smpi_privatization_regions));
103
104   std::vector<RegionSnapshot> data;
105   data.reserve(process_count);
106   for (size_t i = 0; i < process_count; i++)
107     data.push_back(region(region_type, start_addr, privatization_regions[i].address, size));
108
109   RegionSnapshot region = RegionSnapshot(region_type, start_addr, permanent_addr, size);
110   region.privatized_data(std::move(data));
111   return region;
112 }
113 #endif
114
115 static void add_region(int index, simgrid::mc::Snapshot* snapshot, simgrid::mc::RegionType type,
116                        simgrid::mc::ObjectInformation* object_info, void* start_addr, void* permanent_addr,
117                        std::size_t size)
118 {
119   if (type == simgrid::mc::RegionType::Data)
120     xbt_assert(object_info, "Missing object info for object.");
121   else if (type == simgrid::mc::RegionType::Heap)
122     xbt_assert(not object_info, "Unexpected object info for heap region.");
123
124   simgrid::mc::RegionSnapshot region;
125 #if HAVE_SMPI
126   const bool privatization_aware = object_info && mc_model_checker->process().privatized(*object_info);
127   if (privatization_aware && MC_smpi_process_count())
128     region = simgrid::mc::privatized_region(type, start_addr, permanent_addr, size);
129   else
130 #endif
131     region = simgrid::mc::region(type, start_addr, permanent_addr, size);
132
133   region.object_info(object_info);
134   snapshot->snapshot_regions[index] =
135       std::unique_ptr<simgrid::mc::RegionSnapshot>(new simgrid::mc::RegionSnapshot(std::move(region)));
136 }
137
138 static void get_memory_regions(simgrid::mc::RemoteClient* process, simgrid::mc::Snapshot* snapshot)
139 {
140   const size_t n = process->object_infos.size();
141   snapshot->snapshot_regions.resize(n + 1);
142   int i = 0;
143   for (auto const& object_info : process->object_infos) {
144     add_region(i, snapshot, simgrid::mc::RegionType::Data, object_info.get(), object_info->start_rw,
145                object_info->start_rw, object_info->end_rw - object_info->start_rw);
146     ++i;
147   }
148
149   xbt_mheap_t heap = process->get_heap();
150   void* start_heap = heap->base;
151   void* end_heap   = heap->breakval;
152
153   add_region(n, snapshot, simgrid::mc::RegionType::Heap, nullptr, start_heap, start_heap,
154              (char*)end_heap - (char*)start_heap);
155   snapshot->heap_bytes_used = mmalloc_get_bytes_used_remote(heap->heaplimit, process->get_malloc_info());
156
157 #if HAVE_SMPI
158   if (mc_model_checker->process().privatized() && MC_smpi_process_count())
159     // snapshot->privatization_index = smpi_loaded_page
160     mc_model_checker->process().read_variable("smpi_loaded_page", &snapshot->privatization_index,
161                                               sizeof(snapshot->privatization_index));
162   else
163 #endif
164     snapshot->privatization_index = simgrid::mc::ProcessIndexMissing;
165 }
166
167 /** @brief Fills the position of the segments (executable, read-only, read/write).
168  * */
169 // TODO, use the ELF segment information for more robustness
170 void find_object_address(std::vector<simgrid::xbt::VmMap> const& maps, simgrid::mc::ObjectInformation* result)
171 {
172   std::string name = simgrid::xbt::Path(result->file_name).get_base_name();
173
174   for (size_t i = 0; i < maps.size(); ++i) {
175     simgrid::xbt::VmMap const& reg = maps[i];
176     if (maps[i].pathname.empty())
177       continue;
178     std::string map_basename = simgrid::xbt::Path(maps[i].pathname).get_base_name();
179     if (map_basename != name)
180       continue;
181
182     // This is the non-GNU_RELRO-part of the data segment:
183     if (reg.prot == PROT_RW) {
184       xbt_assert(not result->start_rw, "Multiple read-write segments for %s, not supported", maps[i].pathname.c_str());
185       result->start_rw = (char*)reg.start_addr;
186       result->end_rw   = (char*)reg.end_addr;
187
188       // The next VMA might be end of the data segment:
189       if (i + 1 < maps.size() && maps[i + 1].pathname.empty() && maps[i + 1].prot == PROT_RW &&
190           maps[i + 1].start_addr == reg.end_addr)
191         result->end_rw = (char*)maps[i + 1].end_addr;
192     }
193
194     // This is the text segment:
195     else if (reg.prot == PROT_RX) {
196       xbt_assert(not result->start_exec, "Multiple executable segments for %s, not supported",
197                  maps[i].pathname.c_str());
198       result->start_exec = (char*)reg.start_addr;
199       result->end_exec   = (char*)reg.end_addr;
200
201       // The next VMA might be end of the data segment:
202       if (i + 1 < maps.size() && maps[i + 1].pathname.empty() && maps[i + 1].prot == PROT_RW &&
203           maps[i + 1].start_addr == reg.end_addr) {
204         result->start_rw = (char*)maps[i + 1].start_addr;
205         result->end_rw   = (char*)maps[i + 1].end_addr;
206       }
207     }
208
209     // This is the GNU_RELRO-part of the data segment:
210     else if (reg.prot == PROT_READ) {
211       xbt_assert(not result->start_ro, "Multiple read only segments for %s, not supported", maps[i].pathname.c_str());
212       result->start_ro = (char*)reg.start_addr;
213       result->end_ro   = (char*)reg.end_addr;
214     }
215   }
216
217   result->start = result->start_rw;
218   if ((const void*)result->start_ro < result->start)
219     result->start = result->start_ro;
220   if ((const void*)result->start_exec < result->start)
221     result->start = result->start_exec;
222
223   result->end = result->end_rw;
224   if (result->end_ro && (const void*)result->end_ro > result->end)
225     result->end = result->end_ro;
226   if (result->end_exec && (const void*)result->end_exec > result->end)
227     result->end = result->end_exec;
228
229   xbt_assert(result->start_exec || result->start_rw || result->start_ro);
230 }
231
232 /************************************* Take Snapshot ************************************/
233 /****************************************************************************************/
234
235 /** @brief Checks whether the variable is in scope for a given IP.
236  *
237  *  A variable may be defined only from a given value of IP.
238  *
239  *  @param var   Variable description
240  *  @param scope Scope description
241  *  @param ip    Instruction pointer
242  *  @return      true if the variable is valid
243  * */
244 static bool valid_variable(simgrid::mc::Variable* var, simgrid::mc::Frame* scope, const void* ip)
245 {
246   // The variable is not yet valid:
247   if (scope->range.begin() + var->start_scope > (std::uint64_t)ip)
248     return false;
249   else
250     return true;
251 }
252
253 static void fill_local_variables_values(mc_stack_frame_t stack_frame, simgrid::mc::Frame* scope, int process_index,
254                                         std::vector<s_local_variable_t>& result)
255 {
256   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
257
258   if (not scope || not scope->range.contain(stack_frame->ip))
259     return;
260
261   for (simgrid::mc::Variable& current_variable : scope->variables) {
262
263     if (not valid_variable(&current_variable, scope, (void*)stack_frame->ip))
264       continue;
265
266     int region_type;
267     // FIXME, get rid of `region_type`
268     if ((long)stack_frame->ip > (long)process->libsimgrid_info->start_exec)
269       region_type = 1;
270     else
271       region_type = 2;
272
273     s_local_variable_t new_var;
274     new_var.subprogram = stack_frame->frame;
275     new_var.ip         = stack_frame->ip;
276     new_var.name       = current_variable.name;
277     new_var.type       = current_variable.type;
278     new_var.region     = region_type;
279     new_var.address    = nullptr;
280
281     if (current_variable.address != nullptr)
282       new_var.address = current_variable.address;
283     else if (not current_variable.location_list.empty()) {
284       simgrid::dwarf::Location location = simgrid::dwarf::resolve(
285           current_variable.location_list, current_variable.object_info, &(stack_frame->unw_cursor),
286           (void*)stack_frame->frame_base, &mc_model_checker->process(), process_index);
287
288       if (not location.in_memory())
289         xbt_die("Cannot handle non-address variable");
290       new_var.address = location.address();
291
292     } else
293       xbt_die("No address");
294
295     result.push_back(std::move(new_var));
296   }
297
298   // Recursive processing of nested scopes:
299   for (simgrid::mc::Frame& nested_scope : scope->scopes)
300     fill_local_variables_values(stack_frame, &nested_scope, process_index, result);
301 }
302
303 static std::vector<s_local_variable_t> get_local_variables_values(std::vector<s_mc_stack_frame_t>& stack_frames,
304                                                                   int process_index)
305 {
306   std::vector<s_local_variable_t> variables;
307   for (s_mc_stack_frame_t& stack_frame : stack_frames)
308     fill_local_variables_values(&stack_frame, stack_frame.frame, process_index, variables);
309   return variables;
310 }
311
312 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(simgrid::mc::UnwindContext* stack_context)
313 {
314   simgrid::mc::RemoteClient* process = &mc_model_checker->process();
315   std::vector<s_mc_stack_frame_t> result;
316
317   unw_cursor_t c = stack_context->cursor();
318
319   // TODO, check condition check (unw_init_local==0 means end of frame)
320
321   while (1) {
322
323     s_mc_stack_frame_t stack_frame;
324
325     stack_frame.unw_cursor = c;
326
327     unw_word_t ip;
328     unw_word_t sp;
329
330     unw_get_reg(&c, UNW_REG_IP, &ip);
331     unw_get_reg(&c, UNW_REG_SP, &sp);
332
333     stack_frame.ip = ip;
334     stack_frame.sp = sp;
335
336     // TODO, use real addresses in frame_t instead of fixing it here
337
338     simgrid::mc::Frame* frame = process->find_function(remote(ip));
339     stack_frame.frame         = frame;
340
341     if (frame) {
342       stack_frame.frame_name = frame->name;
343       stack_frame.frame_base = (unw_word_t)frame->frame_base(c);
344     } else {
345       stack_frame.frame_base = 0;
346       stack_frame.frame_name = std::string();
347     }
348
349     result.push_back(std::move(stack_frame));
350
351     /* Stop before context switch with maestro */
352     if (frame != nullptr && frame->name == "smx_ctx_wrapper")
353       break;
354
355     int ret = unw_step(&c);
356     if (ret == 0)
357       xbt_die("Unexpected end of stack.");
358     else if (ret < 0)
359       xbt_die("Error while unwinding stack");
360   }
361
362   if (result.empty()) {
363     XBT_INFO("unw_init_local failed");
364     xbt_abort();
365   }
366
367   return result;
368 }
369
370 static std::vector<s_mc_snapshot_stack_t> take_snapshot_stacks(simgrid::mc::Snapshot* snapshot)
371 {
372   std::vector<s_mc_snapshot_stack_t> res;
373
374   for (auto const& stack : mc_model_checker->process().stack_areas()) {
375     s_mc_snapshot_stack_t st;
376
377     // Read the context from remote process:
378     unw_context_t context;
379     mc_model_checker->process().read_bytes(&context, sizeof(context), remote(stack.context));
380
381     st.context.initialize(&mc_model_checker->process(), &context);
382
383     st.stack_frames    = unwind_stack_frames(&st.context);
384     st.local_variables = get_local_variables_values(st.stack_frames, stack.process_index);
385     st.process_index   = stack.process_index;
386
387     unw_word_t sp = st.stack_frames[0].sp;
388
389     res.push_back(std::move(st));
390
391     size_t stack_size = (char*)stack.address + stack.size - (char*)sp;
392     snapshot->stack_sizes.push_back(stack_size);
393   }
394
395   return res;
396 }
397
398 static void snapshot_handle_ignore(simgrid::mc::Snapshot* snapshot)
399 {
400   xbt_assert(snapshot->process());
401
402   // Copy the memory:
403   for (auto const& region : mc_model_checker->process().ignored_regions()) {
404     s_mc_snapshot_ignored_data_t ignored_data;
405     ignored_data.start = (void*)region.addr;
406     ignored_data.data.resize(region.size);
407     // TODO, we should do this once per privatization segment:
408     snapshot->process()->read_bytes(ignored_data.data.data(), region.size, remote(region.addr),
409                                     simgrid::mc::ProcessIndexDisabled);
410     snapshot->ignored_data.push_back(std::move(ignored_data));
411   }
412
413   // Zero the memory:
414   for (auto const& region : mc_model_checker->process().ignored_regions())
415     snapshot->process()->clear_bytes(remote(region.addr), region.size);
416 }
417
418 static void snapshot_ignore_restore(simgrid::mc::Snapshot* snapshot)
419 {
420   for (auto const& ignored_data : snapshot->ignored_data)
421     snapshot->process()->write_bytes(ignored_data.data.data(), ignored_data.data.size(), remote(ignored_data.start));
422 }
423
424 std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state)
425 {
426   XBT_DEBUG("Taking snapshot %i", num_state);
427
428   simgrid::mc::RemoteClient* mc_process = &mc_model_checker->process();
429
430   std::shared_ptr<simgrid::mc::Snapshot> snapshot = std::make_shared<simgrid::mc::Snapshot>(mc_process, num_state);
431
432   for (auto const& p : mc_model_checker->process().actors())
433     snapshot->enabled_processes.insert(p.copy.getBuffer()->get_pid());
434
435   snapshot_handle_ignore(snapshot.get());
436
437   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
438   get_memory_regions(mc_process, snapshot.get());
439
440   snapshot->to_ignore = mc_model_checker->process().ignored_heap();
441
442   if (_sg_mc_max_visited_states > 0 || not _sg_mc_property_file.get().empty()) {
443     snapshot->stacks = take_snapshot_stacks(snapshot.get());
444     if (_sg_mc_hash)
445       snapshot->hash = simgrid::mc::hash(*snapshot);
446     else
447       snapshot->hash = 0;
448   } else
449     snapshot->hash = 0;
450
451   snapshot_ignore_restore(snapshot.get());
452   return snapshot;
453 }
454
455 static inline void restore_snapshot_regions(simgrid::mc::Snapshot* snapshot)
456 {
457   for (std::unique_ptr<simgrid::mc::RegionSnapshot> const& region : snapshot->snapshot_regions) {
458     // For privatized, variables we decided it was not necessary to take the snapshot:
459     if (region)
460       restore(region.get());
461   }
462
463 #if HAVE_SMPI
464   if (snapshot->privatization_index >= 0) {
465     // Fix the privatization mmap:
466     s_mc_message_restore_t message{MC_MESSAGE_RESTORE, snapshot->privatization_index};
467     mc_model_checker->process().getChannel().send(message);
468   }
469 #endif
470 }
471
472 void restore_snapshot(std::shared_ptr<simgrid::mc::Snapshot> snapshot)
473 {
474   XBT_DEBUG("Restore snapshot %i", snapshot->num_state);
475   restore_snapshot_regions(snapshot.get());
476   snapshot_ignore_restore(snapshot.get());
477   mc_model_checker->process().clear_cache();
478 }
479
480 } // namespace mc
481 } // namespace simgrid