Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not use MC_process_clock_add() for now, as it makes the exploration infinitely...
[simgrid.git] / src / mc / api / RemoteApp.cpp
index 3d63a21..a1217a6 100644 (file)
@@ -30,13 +30,7 @@ XBT_LOG_EXTERNAL_CATEGORY(mc_global);
 
 namespace simgrid::mc {
 
-static char master_socket_name[65] = {};
-static void cleanup_master_socket()
-{
-  if (master_socket_name[0] != '\0')
-    unlink(master_socket_name);
-  master_socket_name[0] = '\0';
-}
+static std::string master_socket_name;
 
 RemoteApp::RemoteApp(const std::vector<char*>& args, bool need_memory_introspection) : app_args_(args)
 {
@@ -45,44 +39,51 @@ RemoteApp::RemoteApp(const std::vector<char*>& args, bool need_memory_introspect
     checker_side_     = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
     initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0, page_store_, *checker_side_->get_remote_memory());
 #else
-    xbt_die("SimGrid was compiled without MC support.");
+    xbt_die("SimGrid MC was compiled without memory introspection support.");
 #endif
   } else {
     master_socket_ = socket(AF_UNIX,
 #ifdef __APPLE__
-                            SOCK_STREAM, /* Mac OSX does not have AF_UNIX + SOCK_SEQPACKET, even if that's faster*/
+                            SOCK_STREAM, /* Mac OSX does not have AF_UNIX + SOCK_SEQPACKET, even if that's faster */
 #else
                             SOCK_SEQPACKET,
 #endif
                             0);
     xbt_assert(master_socket_ != -1, "Cannot create the master socket: %s", strerror(errno));
 
+    master_socket_name = "/tmp/simgrid-mc-" + std::to_string(getpid());
+    master_socket_name.resize(MC_SOCKET_NAME_LEN); // truncate socket name if it's too long
+    master_socket_name.back() = '\0';              // ensure the data are null-terminated
+#ifdef __linux__
+    master_socket_name[0] = '\0'; // abstract socket, automatically removed after close
+#else
+    unlink(master_socket_name.c_str()); // remove possible stale socket before bind
+    atexit([]() {
+      if (not master_socket_name.empty())
+        unlink(master_socket_name.c_str());
+      master_socket_name.clear();
+    });
+#endif
+
     struct sockaddr_un serv_addr = {};
     serv_addr.sun_family         = AF_UNIX;
-    snprintf(serv_addr.sun_path, 64, "/tmp/simgrid-mc-%d", getpid());
-    strcpy(master_socket_name, serv_addr.sun_path);
-    auto addr_size = offsetof(struct sockaddr_un, sun_path) + strlen(serv_addr.sun_path);
+    master_socket_name.copy(serv_addr.sun_path, MC_SOCKET_NAME_LEN);
 
-    xbt_assert(bind(master_socket_, (struct sockaddr*)&serv_addr, addr_size) >= 0,
-               "Cannot bind the master socket to %s: %s.", serv_addr.sun_path, strerror(errno));
-    atexit(cleanup_master_socket);
+    xbt_assert(bind(master_socket_, (struct sockaddr*)&serv_addr, sizeof serv_addr) >= 0,
+               "Cannot bind the master socket to %c%s: %s.", (serv_addr.sun_path[0] ? serv_addr.sun_path[0] : '@'),
+               serv_addr.sun_path + 1, strerror(errno));
 
     xbt_assert(listen(master_socket_, SOMAXCONN) >= 0, "Cannot listen to the master socket: %s.", strerror(errno));
 
     application_factory_ = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
-    checker_side_        = application_factory_->clone(master_socket_);
+    checker_side_        = application_factory_->clone(master_socket_, master_socket_name);
   }
 }
 
-RemoteApp::~RemoteApp()
-{
-  checker_side_     = nullptr;
-}
-
 void RemoteApp::restore_initial_state()
 {
   if (initial_snapshot_ == nullptr) // No memory introspection
-    checker_side_ = application_factory_->clone(master_socket_);
+    checker_side_ = application_factory_->clone(master_socket_, master_socket_name);
 #if SIMGRID_HAVE_STATEFUL_MC
   else
     initial_snapshot_->restore(*checker_side_->get_remote_memory());
@@ -115,6 +116,10 @@ void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
   //                    <----- send ACTORS_STATUS_REPLY_COUNT
   //                    <----- send `N` ACTORS_STATUS_REPLY_TRANSITION (s_mc_message_actors_status_one_t)
   //                    <----- send `M` ACTORS_STATUS_REPLY_SIMCALL (s_mc_message_simcall_probe_one_t)
+  //
+  // Note that we also receive disabled transitions, because the guiding strategies need them to decide what could
+  // unlock actors.
+
   checker_side_->get_channel().send(MessageType::ACTORS_STATUS);
 
   s_mc_message_actors_status_answer_t answer;
@@ -140,7 +145,7 @@ void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
 
   for (const auto& actor : status) {
     std::vector<std::shared_ptr<Transition>> actor_transitions;
-    int n_transitions = actor.enabled ? actor.max_considered : 0;
+    int n_transitions = actor.max_considered;
     for (int times_considered = 0; times_considered < n_transitions; times_considered++) {
       s_mc_message_simcall_probe_one_t probe;
       ssize_t received = checker_side_->get_channel().receive(probe);
@@ -153,7 +158,8 @@ void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
       actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
     }
 
-    XBT_DEBUG("Received %zu transitions for actor %ld", actor_transitions.size(), actor.aid);
+    XBT_DEBUG("Received %zu transitions for actor %ld. The first one is %s", actor_transitions.size(), actor.aid,
+              (actor_transitions.size() > 0 ? actor_transitions[0]->to_string().c_str() : "null"));
     whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
   }
 }
@@ -178,7 +184,7 @@ void RemoteApp::check_deadlock() const
              "--cfg=model-check/replay:'%s'",
              explo->get_record_trace().to_string().c_str());
     explo->log_state();
-    throw DeadlockError();
+    throw McError(ExitStatus::DEADLOCK);
   }
 }