Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
66b7bcaed48f582b540844577856efa65a8eb6b5
[simgrid.git] / src / mc / api / RemoteApp.cpp
1 /* Copyright (c) 2015-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/mc/api/RemoteApp.hpp"
7 #include "src/mc/explo/Exploration.hpp"
8 #include "src/mc/mc_config.hpp"
9 #include "xbt/asserts.h"
10 #include "src/mc/api/State.hpp"
11 #include "src/mc/mc_config.hpp"
12 #include "src/mc/mc_exit.hpp"
13 #include "src/mc/mc_private.hpp"
14 #include "xbt/log.h"
15 #include "xbt/system_error.hpp"
16 #include <signal.h>
17
18 #include <algorithm>
19 #include <array>
20 #include <memory>
21 #include <numeric>
22 #include <string>
23
24 #include <sys/ptrace.h>
25 #include <sys/wait.h>
26
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_Session, mc, "Model-checker session");
28 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
29
30 namespace simgrid::mc {
31
32 RemoteApp::RemoteApp(const std::vector<char*>& args, bool need_memory_introspection)
33 {
34   for (auto* arg : args)
35     app_args_.push_back(arg);
36
37   checker_side_ = std::make_unique<simgrid::mc::CheckerSide>(app_args_, need_memory_introspection);
38
39   if (need_memory_introspection)
40     initial_snapshot_ = std::make_shared<simgrid::mc::Snapshot>(0, page_store_, *checker_side_->get_remote_memory());
41 }
42
43 RemoteApp::~RemoteApp()
44 {
45   initial_snapshot_ = nullptr;
46   checker_side_     = nullptr;
47 }
48
49 void RemoteApp::restore_initial_state()
50 {
51   if (initial_snapshot_ == nullptr) { // No memory introspection
52     // We need to destroy the existing CheckerSide before creating the new one, or libevent gets crazy
53     checker_side_.reset(nullptr);
54     checker_side_.reset(new simgrid::mc::CheckerSide(app_args_, true));
55   } else
56     initial_snapshot_->restore(*checker_side_->get_remote_memory());
57 }
58
59 unsigned long RemoteApp::get_maxpid() const
60 {
61   // note: we could maybe cache it and count the actor creation on checker side too.
62   // But counting correctly accross state checkpoint/restore would be annoying.
63
64   checker_side_->get_channel().send(MessageType::ACTORS_MAXPID);
65   s_mc_message_int_t answer;
66   ssize_t answer_size = checker_side_->get_channel().receive(answer);
67   xbt_assert(answer_size != -1, "Could not receive message");
68   xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
69   xbt_assert(answer.type == MessageType::ACTORS_MAXPID_REPLY,
70              "Received unexpected message %s (%i); expected MessageType::ACTORS_MAXPID_REPLY (%i)",
71              to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_MAXPID_REPLY);
72
73   return answer.value;
74 }
75
76 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
77 {
78   // The messaging happens as follows:
79   //
80   // CheckerSide                  AppSide
81   // send ACTORS_STATUS ---->
82   //                    <----- send ACTORS_STATUS_REPLY
83   //                    <----- send `N` `s_mc_message_actors_status_one_t` structs
84   //                    <----- send `M` `s_mc_message_simcall_probe_one_t` structs
85   checker_side_->get_channel().send(MessageType::ACTORS_STATUS);
86
87   s_mc_message_actors_status_answer_t answer;
88   ssize_t answer_size = checker_side_->get_channel().receive(answer);
89   xbt_assert(answer_size != -1, "Could not receive message");
90   xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
91   xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY,
92              "Received unexpected message %s (%i); expected MessageType::ACTORS_STATUS_REPLY (%i)",
93              to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_STATUS_REPLY);
94
95   // Message sanity checks
96   xbt_assert(answer.count >= 0, "Received an ACTOR_STATUS_REPLY message with an actor count of '%d' < 0", answer.count);
97   xbt_assert(answer.transition_count >= 0, "Received an ACTOR_STATUS_REPLY message with transition_count '%d' < 0",
98              answer.transition_count);
99   xbt_assert(answer.transition_count == 0 || answer.count >= 0,
100              "Received an ACTOR_STATUS_REPLY message with no actor data "
101              "but with transition data nonetheless");
102
103   std::vector<s_mc_message_actors_status_one_t> status(answer.count);
104   if (answer.count > 0) {
105     size_t size      = status.size() * sizeof(s_mc_message_actors_status_one_t);
106     ssize_t received = checker_side_->get_channel().receive(status.data(), size);
107     xbt_assert(static_cast<size_t>(received) == size);
108   }
109
110   // Ensures that each actor sends precisely `answer.transition_count` transitions. While technically
111   // this doesn't catch the edge case where actor A sends 3 instead of 2 and actor B sends 2 instead
112   // of 3 transitions, that is ignored here since that invariant needs to be enforced on the AppSide
113   const auto expected_transitions = std::accumulate(
114       status.begin(), status.end(), 0, [](int total, const auto& actor) { return total + actor.n_transitions; });
115   xbt_assert(expected_transitions == answer.transition_count,
116              "Expected to receive %d transition(s) but was only notified of %d by the app side", expected_transitions,
117              answer.transition_count);
118
119   whereto.clear();
120
121   for (const auto& actor : status) {
122     xbt_assert(actor.n_transitions == 0 || actor.n_transitions == actor.max_considered,
123                "If any transitions are serialized for an actor, it must match the "
124                "total number of transitions that can be considered for the actor "
125                "(currently %d), but only %d transition(s) was/were said to be encoded",
126                actor.max_considered, actor.n_transitions);
127
128     std::vector<std::shared_ptr<Transition>> actor_transitions;
129     for (int times_considered = 0; times_considered < actor.n_transitions; times_considered++) {
130       s_mc_message_simcall_probe_one_t probe;
131       ssize_t received = checker_side_->get_channel().receive(probe);
132       xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno));
133       xbt_assert(static_cast<size_t>(received) == sizeof probe,
134                  "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected",
135                  received, sizeof probe);
136
137       std::stringstream stream(probe.buffer.data());
138       actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
139     }
140
141     XBT_DEBUG("Received %zu transitions for actor %ld", actor_transitions.size(), actor.aid);
142     whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
143   }
144 }
145
146 void RemoteApp::check_deadlock() const
147 {
148   xbt_assert(checker_side_->get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
149   s_mc_message_int_t message;
150   ssize_t received = checker_side_->get_channel().receive(message);
151   xbt_assert(received != -1, "Could not receive message");
152   xbt_assert(received == sizeof message, "Broken message (size=%zd; expected %zu)", received, sizeof message);
153   xbt_assert(message.type == MessageType::DEADLOCK_CHECK_REPLY,
154              "Received unexpected message %s (%i); expected MessageType::DEADLOCK_CHECK_REPLY (%i)",
155              to_c_str(message.type), (int)message.type, (int)MessageType::DEADLOCK_CHECK_REPLY);
156
157   if (message.value != 0) {
158     auto* explo = Exploration::get_instance();
159     XBT_CINFO(mc_global, "Counter-example execution trace:");
160     for (auto const& frame : explo->get_textual_trace())
161       XBT_CINFO(mc_global, "  %s", frame.c_str());
162     XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
163              "--cfg=model-check/replay:'%s'",
164              explo->get_record_trace().to_string().c_str());
165     explo->log_state();
166     throw DeadlockError();
167   }
168 }
169
170 void RemoteApp::wait_for_requests()
171 {
172   checker_side_->wait_for_requests();
173 }
174
175 Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition)
176 {
177   s_mc_message_simcall_execute_t m = {};
178   m.type                           = MessageType::SIMCALL_EXECUTE;
179   m.aid_                           = aid;
180   m.times_considered_              = times_considered;
181   checker_side_->get_channel().send(m);
182
183   if (auto* memory = get_remote_process_memory(); memory != nullptr)
184     memory->clear_cache();
185   if (checker_side_->running())
186     checker_side_->dispatch_events(); // The app may send messages while processing the transition
187
188   s_mc_message_simcall_execute_answer_t answer;
189   ssize_t s = checker_side_->get_channel().receive(answer);
190   xbt_assert(s != -1, "Could not receive message");
191   xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
192   xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
193              "Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_ANSWER (%i)",
194              to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_ANSWER);
195
196   if (new_transition) {
197     std::stringstream stream(answer.buffer.data());
198     return deserialize_transition(aid, times_considered, stream);
199   } else
200     return nullptr;
201 }
202
203 void RemoteApp::finalize_app(bool terminate_asap)
204 {
205   checker_side_->finalize(terminate_asap);
206 }
207
208 } // namespace simgrid::mc