Logo AND Algorithmique Numérique Distribuée

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