From 76330c6c2b3568705412619890a6fddb344e560d Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Tue, 11 Jun 2019 22:18:45 +0200 Subject: [PATCH] further simplify the MC initialization --- src/mc/Session.cpp | 39 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/src/mc/Session.cpp b/src/mc/Session.cpp index 8c0926ef6a..5d00dbc95c 100644 --- a/src/mc/Session.cpp +++ b/src/mc/Session.cpp @@ -24,24 +24,23 @@ namespace mc { static void setup_child_environment(int socket) { #ifdef __linux__ - // Make sure we do not outlive our parent: + // Make sure we do not outlive our parent sigset_t mask; sigemptyset (&mask); xbt_assert(sigprocmask(SIG_SETMASK, &mask, nullptr) >= 0, "Could not unblock signals"); xbt_assert(prctl(PR_SET_PDEATHSIG, SIGHUP) == 0, "Could not PR_SET_PDEATHSIG"); #endif - // Remove CLOEXEC in order to pass the socket to the exec-ed program: + // Remove CLOEXEC to pass the socket to the application int fdflags = fcntl(socket, F_GETFD, 0); xbt_assert(fdflags != -1 && fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) != -1, "Could not remove CLOEXEC for socket"); - // Set environment: + // Set environment so that mmalloc gets used in application setenv(MC_ENV_VARIABLE, "1", 1); - // Disable lazy relocation in the model-checked process. - // We don't want the model-checked process to modify its .got.plt during - // snapshot. + // Disable lazy relocation in the model-checked process to prevent the application from + // modifying its .got.plt during snapshot. setenv("LC_BIND_NOW", "1", 1); char buffer[64]; @@ -50,21 +49,6 @@ static void setup_child_environment(int socket) setenv(MC_ENV_SOCKET_FD, buffer, 1); } -/** Execute some code in a forked process */ -template -static inline -pid_t do_fork(F code) -{ - pid_t pid = fork(); - xbt_assert(pid >= 0, "Could not fork model-checked process"); - if (pid != 0) - return pid; - - // Child-process: - code(); - _exit(EXIT_SUCCESS); -} - Session::Session(const std::function& code) { #if HAVE_SMPI @@ -75,18 +59,19 @@ Session::Session(const std::function& code) // Create a AF_LOCAL socketpair used for exchanging messages // between the model-checker process (ourselves) and the model-checked // process: - int res; int sockets[2]; - res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets); - if (res == -1) - throw simgrid::xbt::errno_error("Could not create socketpair"); + int res = socketpair(AF_LOCAL, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockets); + xbt_assert(res != -1, "Could not create socketpair"); - pid_t pid = do_fork([sockets, &code] { + pid_t pid = fork(); + xbt_assert(pid >= 0, "Could not fork model-checked process"); + + if (pid == 0) { // Child ::close(sockets[1]); setup_child_environment(sockets[0]); code(); xbt_die("The model-checked process failed to exec()"); - }); + } // Parent (model-checker): ::close(sockets[0]); -- 2.20.1