Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MC: simplify initialization and kill api::set_checker()
[simgrid.git] / src / mc / checker / simgrid_mc.cpp
1 /* Copyright (c) 2015-2021. 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   if (argc < 2)
35     xbt_die("Missing arguments.\n");
36
37   // Currently, we need this before sg_config_init:
38   _sg_do_model_check = 1;
39
40   // The initialization function can touch argv.
41   // We make a copy of argv before modifying it in order to pass the original value to the model-checked application:
42   char** argv_copy = argvdup(argc, argv);
43
44   xbt_log_init(&argc, argv);
45 #if HAVE_SMPI
46   smpi_init_options(); // only performed once
47 #endif
48   sg_config_init(&argc, argv);
49
50   simgrid::mc::CheckerAlgorithm algo;
51   if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
52     algo = simgrid::mc::CheckerAlgorithm::CommDeterminism;
53   else if (_sg_mc_unfolding_checker)
54     algo = simgrid::mc::CheckerAlgorithm::UDPOR;
55   else if (_sg_mc_property_file.get().empty())
56     algo = simgrid::mc::CheckerAlgorithm::Safety;
57   else
58     algo = simgrid::mc::CheckerAlgorithm::Liveness;
59
60   int res      = SIMGRID_MC_EXIT_SUCCESS;
61   auto checker = api::get().initialize(argv_copy, algo);
62   try {
63     checker->run();
64   } catch (const simgrid::mc::DeadlockError&) {
65     res = SIMGRID_MC_EXIT_DEADLOCK;
66   } catch (const simgrid::mc::TerminationError&) {
67     res = SIMGRID_MC_EXIT_NON_TERMINATION;
68   } catch (const simgrid::mc::LivenessError&) {
69     res = SIMGRID_MC_EXIT_LIVENESS;
70   }
71   checker = nullptr;
72   api::get().s_close();
73   delete[] argv_copy;
74   return res;
75 }