Logo AND Algorithmique Numérique Distribuée

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