Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] ptrace the model-checker application
[simgrid.git] / src / mc / mc_server.cpp
index 9ea15bd..e618b32 100644 (file)
 #include <sys/wait.h>
 #include <sys/socket.h>
 #include <sys/signalfd.h>
+#include <sys/ptrace.h>
 
 #include <xbt/log.h>
+#include <xbt/automaton.h>
+#include <xbt/automaton.hpp>
 
 #include "ModelChecker.hpp"
 #include "mc_protocol.h"
@@ -21,6 +24,8 @@
 #include "mc_private.h"
 #include "mc_ignore.h"
 #include "mcer_ignore.h"
+#include "mc_exit.h"
+#include "src/mc/mc_liveness.h"
 
 using simgrid::mc::remote;
 
@@ -34,35 +39,11 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_server, mc, "MC server logic");
 
 mc_server_t mc_server;
 
-struct mc_symbol_pointer_callback
-{
-  mc_process_t process;
-  void* value;
-};
-
-static int mc_symbol_pointer_callback_evaluate(void* p)
-{
-  struct mc_symbol_pointer_callback* callback = (struct mc_symbol_pointer_callback*) p;
-  int value;
-  callback->process->read_bytes(&value, sizeof(value), remote(callback->value));
-  return value;
-}
-
-s_mc_server::s_mc_server(pid_t pid, int socket)
-{
-  this->pid = pid;
-  this->socket = socket;
-}
+s_mc_server::s_mc_server(pid_t pid_, int socket_)
+  : pid(pid_), socket(socket_) {}
 
 void s_mc_server::start()
 {
-  /* Wait for the target process to initialize and exchange a HELLO messages
-   * before trying to look at its memory map.
-   */
-  int res = MC_protocol_hello(socket);
-  if (res != 0)
-    throw std::system_error(res, std::system_category());
-
   // Block SIGCHLD (this will be handled with accept/signalfd):
   sigset_t set;
   sigemptyset(&set);
@@ -88,13 +69,26 @@ void s_mc_server::start()
   signalfd_pollfd->fd = signal_fd;
   signalfd_pollfd->events = POLLIN;
   signalfd_pollfd->revents = 0;
+
+  XBT_DEBUG("Waiting for the model-checked process");
+  int status;
+
+  // The model-checked process SIGSTOP itself to signal it's ready:
+  pid_t res = waitpid(pid, &status, __WALL);
+  if (res < 0 || !WIFSTOPPED(status) || WSTOPSIG(status) != SIGSTOP)
+    xbt_die("Could not wait model-checked process");
+
+  // The model-checked process is ready, we can read its memory layout:
+  MC_init_model_checker(pid, socket);
+
+  ptrace(PTRACE_CONT, pid, 0, 0);
 }
 
 void s_mc_server::shutdown()
 {
   XBT_DEBUG("Shuting down model-checker");
 
-  mc_process_t process = &mc_model_checker->process();
+  simgrid::mc::Process* process = &mc_model_checker->process();
   int status = process->status();
   if (process->running()) {
     XBT_DEBUG("Killing process");
@@ -122,10 +116,9 @@ void s_mc_server::exit()
   }
 }
 
-void s_mc_server::resume(mc_process_t process)
+void s_mc_server::resume(simgrid::mc::Process* process)
 {
-  int socket = process->socket;
-  int res = MC_protocol_send_simple_message(socket, MC_MESSAGE_CONTINUE);
+  int res = process->send_message(MC_MESSAGE_CONTINUE);
   if (res)
     throw std::system_error(res, std::system_category());
   process->cache_flags = (mc_process_cache_flags_t) 0;
@@ -188,7 +181,8 @@ bool s_mc_server::handle_events()
           if (size != sizeof(message))
             xbt_die("Broken messsage");
           memcpy(&message, buffer, sizeof(message));
-          MC_heap_region_ignore_remove(message.addr, message.size);
+          MC_heap_region_ignore_remove(
+            (void *)(std::uintptr_t) message.addr, message.size);
           break;
         }
 
@@ -199,7 +193,7 @@ bool s_mc_server::handle_events()
             xbt_die("Broken messsage");
           memcpy(&message, buffer, sizeof(message));
           mc_model_checker->process().ignore_region(
-            (std::uint64_t)message.addr, message.size);
+            message.addr, message.size);
           break;
         }
 
@@ -222,15 +216,20 @@ bool s_mc_server::handle_events()
             xbt_die("Broken message");
           memcpy(&message, buffer, sizeof(message));
           if (message.callback)
-            xbt_die("Support for callbacks/functions symbols not implemented in client/server mode.");
+            xbt_die("Support for client-side function proposition is not implemented.");
           XBT_DEBUG("Received symbol: %s", message.name);
 
-          struct mc_symbol_pointer_callback* callback = xbt_new(struct mc_symbol_pointer_callback, 1);
-          callback->process = &mc_model_checker->process();
-          callback->value   = message.data;
+          if (_mc_property_automaton == NULL)
+            _mc_property_automaton = xbt_automaton_new();
+
+          simgrid::mc::Process* process = &mc_model_checker->process();
+          simgrid::mc::remote_ptr<int> address
+            = simgrid::mc::remote((int*) message.data);
+          simgrid::xbt::add_proposition(_mc_property_automaton,
+            message.name,
+            [process, address]() { return process->read(address); }
+            );
 
-          MC_automaton_new_propositional_symbol_callback(message.name,
-            mc_symbol_pointer_callback_evaluate, callback, free);
           break;
         }
 
@@ -239,7 +238,7 @@ bool s_mc_server::handle_events()
 
       case MC_MESSAGE_ASSERTION_FAILED:
         MC_report_assertion_error();
-        xbt_abort();
+        ::exit(SIMGRID_EXIT_SAFETY);
         break;
 
       default:
@@ -335,7 +334,7 @@ void s_mc_server::on_signal(const struct signalfd_siginfo* info)
   }
 }
 
-void MC_server_wait_client(mc_process_t process)
+void MC_server_wait_client(simgrid::mc::Process* process)
 {
   mc_server->resume(process);
   while (mc_model_checker->process().running()) {
@@ -344,14 +343,14 @@ void MC_server_wait_client(mc_process_t process)
   }
 }
 
-void MC_server_simcall_handle(mc_process_t process, unsigned long pid, int value)
+void MC_server_simcall_handle(simgrid::mc::Process* process, unsigned long pid, int value)
 {
   s_mc_simcall_handle_message m;
   memset(&m, 0, sizeof(m));
   m.type  = MC_MESSAGE_SIMCALL_HANDLE;
   m.pid   = pid;
   m.value = value;
-  MC_protocol_send(mc_model_checker->process().socket, &m, sizeof(m));
+  mc_model_checker->process().send_message(m);
   process->cache_flags = (mc_process_cache_flags_t) 0;
   while (mc_model_checker->process().running()) {
     if (!mc_server->handle_events())