Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid unsafe things
[simgrid.git] / src / mc / mc_checkpoint.cpp
index 6e7d1e5..3716919 100644 (file)
 #include <link.h>
 #include <dirent.h>
 
+#ifndef WIN32
+#include <sys/mman.h>
+#endif
+
 #include "src/internal_config.h"
 #include "src/mc/mc_private.h"
 #include "xbt/module.h"
 #include "src/mc/mc_private.h"
 #include <mc/mc.h>
 
-#include "src/mc/mc_snapshot.h"
+#include "src/mc/mc_hash.hpp"
 #include "src/mc/mc_mmu.h"
-#include "src/mc/mc_unw.h"
-#include "src/mc/mc_protocol.h"
 #include "src/mc/mc_smx.h"
-#include "mc_hash.hpp"
+#include "src/mc/mc_snapshot.h"
+#include "src/mc/mc_unw.h"
+#include "src/mc/remote/mc_protocol.h"
 
 #include "src/mc/RegionSnapshot.hpp"
 #include "src/mc/ObjectInformation.hpp"
@@ -53,7 +57,7 @@ namespace mc {
 
 /** @brief Restore a region from a snapshot
  *
- *  @param reg     Target region
+ *  @param region     Target region
  */
 static void restore(mc_mem_region_t region)
 {
@@ -82,7 +86,7 @@ static void restore(mc_mem_region_t region)
 #if HAVE_SMPI
 RegionSnapshot privatized_region(
     RegionType region_type, void *start_addr, void* permanent_addr,
-    std::size_t size, const RegionSnapshot* ref_region
+    std::size_t size
     )
 {
   size_t process_count = MC_smpi_process_count();
@@ -99,18 +103,14 @@ RegionSnapshot privatized_region(
 
   std::vector<simgrid::mc::RegionSnapshot> data;
   data.reserve(process_count);
-  for (size_t i = 0; i < process_count; i++) {
-    const simgrid::mc::RegionSnapshot* ref_privatized_region = nullptr;
-    if (ref_region && ref_region->storage_type() == StorageType::Privatized)
-      ref_privatized_region = &ref_region->privatized_data()[i];
+  for (size_t i = 0; i < process_count; i++)
     data.push_back(simgrid::mc::region(region_type, start_addr,
-      privatisation_regions[i].address, size, ref_privatized_region));
-  }
+      privatisation_regions[i].address, size));
 
   simgrid::mc::RegionSnapshot region = simgrid::mc::RegionSnapshot(
     region_type, start_addr, permanent_addr, size);
   region.privatized_data(std::move(data));
-  return std::move(region);
+  return region;
 }
 #endif
 
@@ -126,20 +126,16 @@ void add_region(int index, simgrid::mc::Snapshot* snapshot,
   else if (type == simgrid::mc::RegionType::Heap)
     xbt_assert(!object_info, "Unexpected object info for heap region.");
 
-  simgrid::mc::RegionSnapshot const* ref_region = nullptr;
-  if (mc_model_checker->parent_snapshot_)
-    ref_region = mc_model_checker->parent_snapshot_->snapshot_regions[index].get();
-
   simgrid::mc::RegionSnapshot region;
 #if HAVE_SMPI
   const bool privatization_aware = object_info
     && mc_model_checker->process().privatized(*object_info);
   if (privatization_aware && MC_smpi_process_count())
     region = simgrid::mc::privatized_region(
-      type, start_addr, permanent_addr, size, ref_region);
+      type, start_addr, permanent_addr, size);
   else
 #endif
-    region = simgrid::mc::region(type, start_addr, permanent_addr, size, ref_region);
+    region = simgrid::mc::region(type, start_addr, permanent_addr, size);
 
   region.object_info(object_info);
   snapshot->snapshot_regions[index]
@@ -250,18 +246,18 @@ void find_object_address(
   }
 
   result->start = result->start_rw;
-  if ((const void*) result->start_ro > result->start)
+  if ((const void*) result->start_ro < result->start)
     result->start = result->start_ro;
-  if ((const void*) result->start_exec > result->start)
+  if ((const void*) result->start_exec < result->start)
     result->start = result->start_exec;
 
   result->end = result->end_rw;
-  if (result->end_ro && (const void*) result->end_ro < result->end)
+  if (result->end_ro && (const void*) result->end_ro > result->end)
     result->end = result->end_ro;
   if (result->end_exec && (const void*) result->end_exec > result->end)
     result->end = result->end_exec;
 
-  xbt_assert(rresult->start_exec || result->start_rw || result->start_ro);
+  xbt_assert(result->start_exec || result->start_rw || result->start_ro);
 
   free(name);
 }
@@ -274,7 +270,7 @@ void find_object_address(
  *  A variable may be defined only from a given value of IP.
  *
  *  \param var   Variable description
- *  \param frame Scope description
+ *  \param scope Scope description
  *  \param ip    Instruction pointer
  *  \return      true if the variable is valid
  * */
@@ -296,7 +292,7 @@ static void fill_local_variables_values(mc_stack_frame_t stack_frame,
 {
   simgrid::mc::Process* process = &mc_model_checker->process();
 
-  if (!scope->range.contain(stack_frame->ip))
+  if (!scope || !scope->range.contain(stack_frame->ip))
     return;
 
   for(simgrid::mc::Variable& current_variable :
@@ -353,7 +349,7 @@ static std::vector<s_local_variable> get_local_variables_values(
   std::vector<s_local_variable> variables;
   for (s_mc_stack_frame_t& stack_frame : stack_frames)
     fill_local_variables_values(&stack_frame, stack_frame.frame, process_index, variables);
-  return std::move(variables);
+  return variables;
 }
 
 static std::vector<s_mc_stack_frame_t> unwind_stack_frames(simgrid::mc::UnwindContext* stack_context)
@@ -412,10 +408,10 @@ static std::vector<s_mc_stack_frame_t> unwind_stack_frames(simgrid::mc::UnwindCo
     xbt_abort();
   }
 
-  return std::move(result);
+  return result;
 };
 
-static std::vector<s_mc_snapshot_stack_t> take_snapshot_stacks(simgrid::mc::Snapshot* snapshot)
+static std::vector<s_mc_snapshot_stack_t> take_snapshot_stacks(simgrid::mc::Snapshot* snapshot)
 {
   std::vector<s_mc_snapshot_stack_t> res;
 
@@ -439,10 +435,10 @@ static std::vector<s_mc_snapshot_stack_t> take_snapshot_stacks(simgrid::mc::Snap
 
     size_t stack_size =
       (char*) stack.address + stack.size - (char*) sp;
-    (*snapshot)->stack_sizes.push_back(stack_size);
+    snapshot->stack_sizes.push_back(stack_size);
   }
 
-  return std::move(res);
+  return res;
 
 }
 
@@ -531,7 +527,7 @@ static std::vector<s_fd_infos_t> get_current_fds(pid_t pid)
 
     // We don't handle them.
     // It does not mean we should silently ignore them however.
-    if (strncmp(link, "pipe:", 5) == 0 || strncmp(link, "socket:", 7) == 0)
+    if (strncmp(link, "pipe:", std::strlen("pipe:")) == 0 || strncmp(link, "socket:", std::strlen("socket:")) == 0)
       continue;
 
     // If dot_output enabled, do not handle the corresponding file
@@ -545,7 +541,7 @@ static std::vector<s_fd_infos_t> get_current_fds(pid_t pid)
     }
 
     // This is probably a shared memory used by lttng-ust:
-    if(strncmp("/dev/shm/ust-shm-tmp-", link, 21)==0)
+    if(strncmp("/dev/shm/ust-shm-tmp-", link, std::strlen("/dev/shm/ust-shm-tmp-"))==0)
       continue;
 
     // Add an entry for this FD in the snapshot:
@@ -558,39 +554,32 @@ static std::vector<s_fd_infos_t> get_current_fds(pid_t pid)
   }
 
   closedir (fd_dir);
-  return std::move(fds);
+  return fds;
 }
 
-simgrid::mc::Snapshot* take_snapshot(int num_state)
+std::shared_ptr<simgrid::mc::Snapshot> take_snapshot(int num_state)
 {
   XBT_DEBUG("Taking snapshot %i", num_state);
 
   simgrid::mc::Process* mc_process = &mc_model_checker->process();
 
-  simgrid::mc::Snapshot* snapshot = new simgrid::mc::Snapshot(mc_process);
+  std::shared_ptr<simgrid::mc::Snapshot> snapshot = std::make_shared<simgrid::mc::Snapshot>(mc_process, num_state);
 
-  snapshot->num_state = num_state;
+  for (auto& p : mc_model_checker->process().actors())
+    snapshot->enabled_processes.insert(p.copy.getBuffer()->pid);
 
-  for (auto& p : mc_model_checker->process().simix_processes())
-    snapshot->enabled_processes.insert(p.copy.pid);
-
-  snapshot_handle_ignore(snapshot);
+  snapshot_handle_ignore(snapshot.get());
 
   if (_sg_mc_snapshot_fds)
     snapshot->current_fds = get_current_fds(mc_model_checker->process().pid());
 
-  const bool use_soft_dirty = _sg_mc_sparse_checkpoint && _sg_mc_soft_dirty;
-
   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
-  get_memory_regions(mc_process, snapshot);
-  if (use_soft_dirty)
-    mc_process->reset_soft_dirty();
+  get_memory_regions(mc_process, snapshot.get());
 
   snapshot->to_ignore = mc_model_checker->process().ignored_heap();
 
-  if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
-    snapshot->stacks =
-        take_snapshot_stacks(&snapshot);
+  if (_sg_mc_max_visited_states > 0 || strcmp(_sg_mc_property_file, "")) {
+    snapshot->stacks = take_snapshot_stacks(snapshot.get());
     if (_sg_mc_hash)
       snapshot->hash = simgrid::mc::hash(*snapshot);
     else
@@ -598,9 +587,7 @@ simgrid::mc::Snapshot* take_snapshot(int num_state)
   } else
     snapshot->hash = 0;
 
-  snapshot_ignore_restore(snapshot);
-  if (use_soft_dirty)
-    mc_model_checker->parent_snapshot_ = snapshot;
+  snapshot_ignore_restore(snapshot.get());
   return snapshot;
 }
 
@@ -628,8 +615,7 @@ void restore_snapshot_regions(simgrid::mc::Snapshot* snapshot)
 static inline
 void restore_snapshot_fds(simgrid::mc::Snapshot* snapshot)
 {
-  if (mc_mode == MC_MODE_SERVER)
-    xbt_die("FD snapshot not implemented in client/server mode.");
+  xbt_die("FD snapshot not implemented in client/server mode.");
 
   for (auto const& fd : snapshot->current_fds) {
     
@@ -645,19 +631,14 @@ void restore_snapshot_fds(simgrid::mc::Snapshot* snapshot)
   }
 }
 
-void restore_snapshot(simgrid::mc::Snapshot* snapshot)
+void restore_snapshot(std::shared_ptr<simgrid::mc::Snapshot> snapshot)
 {
   XBT_DEBUG("Restore snapshot %i", snapshot->num_state);
-  const bool use_soft_dirty = _sg_mc_sparse_checkpoint && _sg_mc_soft_dirty;
-  restore_snapshot_regions(snapshot);
+  restore_snapshot_regions(snapshot.get());
   if (_sg_mc_snapshot_fds)
-    restore_snapshot_fds(snapshot);
-  if (use_soft_dirty)
-    mc_model_checker->process().reset_soft_dirty();
-  snapshot_ignore_restore(snapshot);
+    restore_snapshot_fds(snapshot.get());
+  snapshot_ignore_restore(snapshot.get());
   mc_model_checker->process().clear_cache();
-  if (use_soft_dirty)
-    mc_model_checker->parent_snapshot_ = snapshot;
 }
 
 }