Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'xbt_random' into 'master'
[simgrid.git] / src / mc / checker / simgrid_mc.cpp
1 /* Copyright (c) 2015-2019. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/sg_config.hpp"
8 #include "src/mc/Session.hpp"
9 #include "src/mc/checker/Checker.hpp"
10 #include "src/mc/mc_config.hpp"
11 #include "src/mc/mc_exit.hpp"
12
13 #include <cstring>
14 #include <memory>
15 #include <unistd.h>
16
17 static inline
18 char** argvdup(int argc, char** argv)
19 {
20   char** argv_copy = new char*[argc + 1];
21   std::memcpy(argv_copy, argv, sizeof(char*) * argc);
22   argv_copy[argc] = nullptr;
23   return argv_copy;
24 }
25
26 static std::unique_ptr<simgrid::mc::Checker> create_checker(simgrid::mc::Session& session)
27 {
28   if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
29     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createCommunicationDeterminismChecker(session));
30   else if (_sg_mc_property_file.get().empty())
31     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createSafetyChecker(session));
32   else
33     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createLivenessChecker(session));
34 }
35
36 int main(int argc, char** argv)
37 {
38   if (argc < 2)
39     xbt_die("Missing arguments.\n");
40
41   // Currently, we need this before sg_config_init:
42   _sg_do_model_check = 1;
43
44   // The initialization function can touch argv.
45   // We make a copy of argv before modifying it in order to pass the original
46   // value to the model-checked:
47   char** argv_copy = argvdup(argc, argv);
48   xbt_log_init(&argc, argv);
49   sg_config_init(&argc, argv);
50
51   simgrid::mc::session = new simgrid::mc::Session([argv_copy] { execvp(argv_copy[1], argv_copy + 1); });
52   delete[] argv_copy;
53
54   std::unique_ptr<simgrid::mc::Checker> checker = create_checker(*simgrid::mc::session);
55   int res                                       = SIMGRID_MC_EXIT_SUCCESS;
56   try {
57     checker->run();
58   } catch (const simgrid::mc::DeadlockError&) {
59     res = SIMGRID_MC_EXIT_DEADLOCK;
60   } catch (const simgrid::mc::TerminationError&) {
61     res = SIMGRID_MC_EXIT_NON_TERMINATION;
62   } catch (const simgrid::mc::LivenessError&) {
63     res = SIMGRID_MC_EXIT_LIVENESS;
64   }
65   checker = nullptr;
66   simgrid::mc::session->close();
67   return res;
68 }