Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Put everything in position to re-fork the verified App
[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_);
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   shutdown();
47 }
48
49 void RemoteApp::restore_initial_state()
50 {
51   if (initial_snapshot_ == nullptr) { // No memory introspection
52     shutdown();
53     checker_side_.reset(
54         nullptr); // We need to destroy the existing CheckerSide before creating the new one, or libevent gets crazy
55     checker_side_.reset(new simgrid::mc::CheckerSide(app_args_));
56   } else
57     initial_snapshot_->restore(checker_side_->get_remote_memory());
58 }
59
60 unsigned long RemoteApp::get_maxpid() const
61 {
62   // note: we could maybe cache it and count the actor creation on checker side too.
63   // But counting correctly accross state checkpoint/restore would be annoying.
64
65   checker_side_->get_channel().send(MessageType::ACTORS_MAXPID);
66   s_mc_message_int_t answer;
67   ssize_t answer_size = checker_side_->get_channel().receive(answer);
68   xbt_assert(answer_size != -1, "Could not receive message");
69   xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
70   xbt_assert(answer.type == MessageType::ACTORS_MAXPID_REPLY,
71              "Received unexpected message %s (%i); expected MessageType::ACTORS_MAXPID_REPLY (%i)",
72              to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_MAXPID_REPLY);
73
74   return answer.value;
75 }
76
77 void RemoteApp::get_actors_status(std::map<aid_t, ActorState>& whereto) const
78 {
79   // The messaging happens as follows:
80   //
81   // CheckerSide                  AppSide
82   // send ACTORS_STATUS ---->
83   //                    <----- send ACTORS_STATUS_REPLY
84   //                    <----- send `N` `s_mc_message_actors_status_one_t` structs
85   //                    <----- send `M` `s_mc_message_simcall_probe_one_t` structs
86   checker_side_->get_channel().send(MessageType::ACTORS_STATUS);
87
88   s_mc_message_actors_status_answer_t answer;
89   ssize_t answer_size = checker_side_->get_channel().receive(answer);
90   xbt_assert(answer_size != -1, "Could not receive message");
91   xbt_assert(answer_size == sizeof answer, "Broken message (size=%zd; expected %zu)", answer_size, sizeof answer);
92   xbt_assert(answer.type == MessageType::ACTORS_STATUS_REPLY,
93              "Received unexpected message %s (%i); expected MessageType::ACTORS_STATUS_REPLY (%i)",
94              to_c_str(answer.type), (int)answer.type, (int)MessageType::ACTORS_STATUS_REPLY);
95
96   // Message sanity checks
97   xbt_assert(answer.count >= 0, "Received an ACTOR_STATUS_REPLY message with an actor count of '%d' < 0", answer.count);
98   xbt_assert(answer.transition_count >= 0, "Received an ACTOR_STATUS_REPLY message with transition_count '%d' < 0",
99              answer.transition_count);
100   xbt_assert(answer.transition_count == 0 || answer.count >= 0,
101              "Received an ACTOR_STATUS_REPLY message with no actor data "
102              "but with transition data nonetheless");
103
104   std::vector<s_mc_message_actors_status_one_t> status(answer.count);
105   if (answer.count > 0) {
106     size_t size      = status.size() * sizeof(s_mc_message_actors_status_one_t);
107     ssize_t received = checker_side_->get_channel().receive(status.data(), size);
108     xbt_assert(static_cast<size_t>(received) == size);
109   }
110
111   // Ensures that each actor sends precisely `answer.transition_count` transitions. While technically
112   // this doesn't catch the edge case where actor A sends 3 instead of 2 and actor B sends 2 instead
113   // of 3 transitions, that is ignored here since that invariant needs to be enforced on the AppSide
114   const auto expected_transitions = std::accumulate(
115       status.begin(), status.end(), 0, [](int total, const auto& actor) { return total + actor.n_transitions; });
116   xbt_assert(expected_transitions == answer.transition_count,
117              "Expected to receive %d transition(s) but was only notified of %d by the app side", expected_transitions,
118              answer.transition_count);
119
120   std::vector<s_mc_message_simcall_probe_one_t> probes(answer.transition_count);
121   if (answer.transition_count > 0) {
122     for (auto& probe : probes) {
123       ssize_t received = checker_side_->get_channel().receive(probe);
124       xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno));
125       xbt_assert(static_cast<size_t>(received) == sizeof probe,
126                  "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected",
127                  received, sizeof probe);
128     }
129   }
130
131   whereto.clear();
132   std::move_iterator probes_iter(probes.begin());
133
134   for (const auto& actor : status) {
135     xbt_assert(actor.n_transitions == 0 || actor.n_transitions == actor.max_considered,
136                "If any transitions are serialized for an actor, it must match the "
137                "total number of transitions that can be considered for the actor "
138                "(currently %d), but only %d transition(s) was/were said to be encoded",
139                actor.max_considered, actor.n_transitions);
140
141     std::vector<std::shared_ptr<Transition>> actor_transitions;
142     for (int times_considered = 0; times_considered < actor.n_transitions; times_considered++, probes_iter++) {
143       std::stringstream stream((*probes_iter).buffer.data());
144       actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
145     }
146
147     XBT_DEBUG("Received %zu transitions for actor %ld", actor_transitions.size(), actor.aid);
148     whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
149   }
150 }
151
152 void RemoteApp::check_deadlock() const
153 {
154   xbt_assert(checker_side_->get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
155   s_mc_message_int_t message;
156   ssize_t received = checker_side_->get_channel().receive(message);
157   xbt_assert(received != -1, "Could not receive message");
158   xbt_assert(received == sizeof message, "Broken message (size=%zd; expected %zu)", received, sizeof message);
159   xbt_assert(message.type == MessageType::DEADLOCK_CHECK_REPLY,
160              "Received unexpected message %s (%i); expected MessageType::DEADLOCK_CHECK_REPLY (%i)",
161              to_c_str(message.type), (int)message.type, (int)MessageType::DEADLOCK_CHECK_REPLY);
162
163   if (message.value != 0) {
164     auto* explo = Exploration::get_instance();
165     XBT_CINFO(mc_global, "Counter-example execution trace:");
166     for (auto const& frame : explo->get_textual_trace())
167       XBT_CINFO(mc_global, "  %s", frame.c_str());
168     XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
169              "--cfg=model-check/replay:'%s'",
170              explo->get_record_trace().to_string().c_str());
171     explo->log_state();
172     throw DeadlockError();
173   }
174 }
175
176 void RemoteApp::wait_for_requests()
177 {
178   checker_side_->wait_for_requests();
179 }
180
181 void RemoteApp::shutdown()
182 {
183   XBT_DEBUG("Shutting down model-checker");
184
185   if (checker_side_->running()) {
186     XBT_DEBUG("Killing process");
187     finalize_app(true);
188     kill(checker_side_->get_pid(), SIGKILL);
189     checker_side_->terminate();
190   }
191 }
192
193 Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition)
194 {
195   s_mc_message_simcall_execute_t m = {};
196   m.type                           = MessageType::SIMCALL_EXECUTE;
197   m.aid_                           = aid;
198   m.times_considered_              = times_considered;
199   checker_side_->get_channel().send(m);
200
201   get_remote_process_memory().clear_cache();
202   if (checker_side_->running())
203     checker_side_->dispatch_events(); // The app may send messages while processing the transition
204
205   s_mc_message_simcall_execute_answer_t answer;
206   ssize_t s = checker_side_->get_channel().receive(answer);
207   xbt_assert(s != -1, "Could not receive message");
208   xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
209   xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
210              "Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_ANSWER (%i)",
211              to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_ANSWER);
212
213   if (new_transition) {
214     std::stringstream stream(answer.buffer.data());
215     return deserialize_transition(aid, times_considered, stream);
216   } else
217     return nullptr;
218 }
219
220 void RemoteApp::finalize_app(bool terminate_asap)
221 {
222   s_mc_message_int_t m = {};
223   m.type               = MessageType::FINALIZE;
224   m.value              = terminate_asap;
225   xbt_assert(checker_side_->get_channel().send(m) == 0, "Could not ask the app to finalize on need");
226
227   s_mc_message_t answer;
228   ssize_t s = checker_side_->get_channel().receive(answer);
229   xbt_assert(s != -1, "Could not receive answer to FINALIZE");
230   xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
231   xbt_assert(answer.type == MessageType::FINALIZE_REPLY,
232              "Received unexpected message %s (%i); expected MessageType::FINALIZE_REPLY (%i)", to_c_str(answer.type),
233              (int)answer.type, (int)MessageType::FINALIZE_REPLY);
234 }
235
236 } // namespace simgrid::mc