Logo AND Algorithmique Numérique Distribuée

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