1 /* Copyright (c) 2015-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/sg_config.hpp"
7 #include "src/internal_config.h"
8 #include "src/mc/explo/Exploration.hpp"
9 #include "src/mc/mc_config.hpp"
10 #include "src/mc/mc_exit.hpp"
13 #include "smpi/smpi.h"
16 #include <boost/tokenizer.hpp>
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(mc);
23 static simgrid::config::Flag<std::string> _sg_mc_setenv{
24 "model-check/setenv", "Extra environment variables to pass to the child process (ex: 'AZE=aze;QWE=qwe').", "",
25 [](std::string_view value) {
26 xbt_assert(value.empty() || value.find('=', 0) != std::string_view::npos,
27 "The 'model-check/setenv' parameter must be like 'AZE=aze', but it does not contain an equal sign.");
30 int main(int argc, char** argv)
32 xbt_assert(argc >= 2, "Missing arguments");
34 // Currently, we need this before sg_config_init:
35 _sg_do_model_check = 1;
37 // The initialization function can touch argv.
38 // We make a copy of argv before modifying it in order to pass the original value to the model-checked application:
39 std::vector<char*> argv_copy{argv, argv + argc + 1};
41 xbt_log_init(&argc, argv);
43 smpi_init_options(); // only performed once
45 sg_config_init(&argc, argv);
47 simgrid::mc::ExplorationAlgorithm algo;
48 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
49 algo = simgrid::mc::ExplorationAlgorithm::CommDeterminism;
50 else if (_sg_mc_unfolding_checker)
51 algo = simgrid::mc::ExplorationAlgorithm::UDPOR;
52 else if (_sg_mc_property_file.get().empty())
53 algo = simgrid::mc::ExplorationAlgorithm::Safety;
55 algo = simgrid::mc::ExplorationAlgorithm::Liveness;
57 std::unordered_map<std::string, std::string> environment;
58 /** Setup the tokenizer that parses the string **/
59 using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
60 boost::char_separator<char> semicol_sep(";");
61 boost::char_separator<char> equal_sep("=");
62 Tokenizer token_vars(_sg_mc_setenv.get(), semicol_sep); /* Iterate over all FOO=foo parts */
63 for (const auto& token : token_vars) {
64 std::vector<std::string> kv;
65 Tokenizer token_kv(token, equal_sep);
66 for (const auto& t : token_kv) /* Iterate over 'FOO' and then 'foo' in that 'FOO=foo' */
68 xbt_assert(kv.size() == 2, "Parse error on 'model-check/setenv' value %s. Does it contain an equal sign?",
70 environment[kv[0]] = kv[1];
72 auto remote_app = std::make_unique<simgrid::mc::RemoteApp>([argv_copy, &environment] {
74 while (argv_copy[i] != nullptr && argv_copy[i][0] == '-')
77 for (auto const& [key, val] : environment) {
78 XBT_INFO("setenv '%s'='%s'", key.c_str(), val.c_str());
79 setenv(key.c_str(), val.c_str(), 1);
81 xbt_assert(argv_copy[i] != nullptr,
82 "Unable to find a binary to exec on the command line. Did you only pass config flags?");
83 execvp(argv_copy[i], argv_copy.data() + i);
84 xbt_die("The model-checked process failed to exec(%s): %s", argv_copy[i], strerror(errno));
87 simgrid::mc::Exploration* explo;
89 case simgrid::mc::ExplorationAlgorithm::CommDeterminism:
90 explo = simgrid::mc::create_communication_determinism_checker(*remote_app.get());
93 case simgrid::mc::ExplorationAlgorithm::UDPOR:
94 explo = simgrid::mc::create_udpor_checker(*remote_app.get());
97 case simgrid::mc::ExplorationAlgorithm::Safety:
98 explo = simgrid::mc::create_dfs_exploration(*remote_app.get());
101 case simgrid::mc::ExplorationAlgorithm::Liveness:
102 explo = simgrid::mc::create_liveness_checker(*remote_app.get());
108 mc_model_checker->set_exploration(explo);
109 std::unique_ptr<simgrid::mc::Exploration> checker{explo};
113 } catch (const simgrid::mc::DeadlockError&) {
114 return SIMGRID_MC_EXIT_DEADLOCK;
115 } catch (const simgrid::mc::TerminationError&) {
116 return SIMGRID_MC_EXIT_NON_TERMINATION;
117 } catch (const simgrid::mc::LivenessError&) {
118 return SIMGRID_MC_EXIT_LIVENESS;
120 return SIMGRID_MC_EXIT_SUCCESS;