Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
createSafetyChecker without Session argument
[simgrid.git] / src / mc / checker / simgrid_mc.cpp
1 /* Copyright (c) 2015-2020. 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 #include "src/internal_config.h"
13 #include "src/mc/mc_api.hpp"
14
15 #if HAVE_SMPI
16 #include "smpi/smpi.h"
17 #endif
18
19 #include <cstring>
20 #include <memory>
21 #include <unistd.h>
22
23 using mcapi = simgrid::mc::mc_api;
24
25 static inline
26 char** argvdup(int argc, char** argv)
27 {
28   auto* argv_copy = new char*[argc + 1];
29   std::memcpy(argv_copy, argv, sizeof(char*) * argc);
30   argv_copy[argc] = nullptr;
31   return argv_copy;
32 }
33
34 static std::unique_ptr<simgrid::mc::Checker> create_checker(simgrid::mc::Session& session)
35 {
36   if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
37     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createCommunicationDeterminismChecker(session));
38   else if (_sg_mc_property_file.get().empty())
39     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createSafetyChecker());
40   else
41     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createLivenessChecker());
42 }
43
44 int main(int argc, char** argv)
45 {
46   if (argc < 2)
47     xbt_die("Missing arguments.\n");
48
49   // Currently, we need this before sg_config_init:
50   _sg_do_model_check = 1;
51
52   // The initialization function can touch argv.
53   // We make a copy of argv before modifying it in order to pass the original value to the model-checked application:
54   char** argv_copy = argvdup(argc, argv);
55   xbt_log_init(&argc, argv);
56 #if HAVE_SMPI
57   smpi_init_options(); // only performed once
58 #endif
59   sg_config_init(&argc, argv);
60   mcapi::get().initialize(argv_copy);
61   delete[] argv_copy;
62
63   auto checker = create_checker(*simgrid::mc::session);
64   int res      = SIMGRID_MC_EXIT_SUCCESS;
65   try {
66     checker->run();
67   } catch (const simgrid::mc::DeadlockError&) {
68     res = SIMGRID_MC_EXIT_DEADLOCK;
69   } catch (const simgrid::mc::TerminationError&) {
70     res = SIMGRID_MC_EXIT_NON_TERMINATION;
71   } catch (const simgrid::mc::LivenessError&) {
72     res = SIMGRID_MC_EXIT_LIVENESS;
73   }
74   checker = nullptr;
75   mcapi::get().s_close();
76   return res;
77 }