Logo AND Algorithmique Numérique Distribuée

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