Logo AND Algorithmique Numérique Distribuée

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