Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sure that the dtor of CheckerSide actually kills the application and waits for it
[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   std::vector<s_mc_message_simcall_probe_one_t> probes(answer.transition_count);
120   if (answer.transition_count > 0) {
121     for (auto& probe : probes) {
122       ssize_t received = checker_side_->get_channel().receive(probe);
123       xbt_assert(received >= 0, "Could not receive response to ACTORS_PROBE message (%s)", strerror(errno));
124       xbt_assert(static_cast<size_t>(received) == sizeof probe,
125                  "Could not receive response to ACTORS_PROBE message (%zd bytes received != %zu bytes expected",
126                  received, sizeof probe);
127     }
128   }
129
130   whereto.clear();
131   std::move_iterator probes_iter(probes.begin());
132
133   for (const auto& actor : status) {
134     xbt_assert(actor.n_transitions == 0 || actor.n_transitions == actor.max_considered,
135                "If any transitions are serialized for an actor, it must match the "
136                "total number of transitions that can be considered for the actor "
137                "(currently %d), but only %d transition(s) was/were said to be encoded",
138                actor.max_considered, actor.n_transitions);
139
140     std::vector<std::shared_ptr<Transition>> actor_transitions;
141     for (int times_considered = 0; times_considered < actor.n_transitions; times_considered++, probes_iter++) {
142       std::stringstream stream((*probes_iter).buffer.data());
143       actor_transitions.emplace_back(deserialize_transition(actor.aid, times_considered, stream));
144     }
145
146     XBT_DEBUG("Received %zu transitions for actor %ld", actor_transitions.size(), actor.aid);
147     whereto.try_emplace(actor.aid, actor.aid, actor.enabled, actor.max_considered, std::move(actor_transitions));
148   }
149 }
150
151 void RemoteApp::check_deadlock() const
152 {
153   xbt_assert(checker_side_->get_channel().send(MessageType::DEADLOCK_CHECK) == 0, "Could not check deadlock state");
154   s_mc_message_int_t message;
155   ssize_t received = checker_side_->get_channel().receive(message);
156   xbt_assert(received != -1, "Could not receive message");
157   xbt_assert(received == sizeof message, "Broken message (size=%zd; expected %zu)", received, sizeof message);
158   xbt_assert(message.type == MessageType::DEADLOCK_CHECK_REPLY,
159              "Received unexpected message %s (%i); expected MessageType::DEADLOCK_CHECK_REPLY (%i)",
160              to_c_str(message.type), (int)message.type, (int)MessageType::DEADLOCK_CHECK_REPLY);
161
162   if (message.value != 0) {
163     auto* explo = Exploration::get_instance();
164     XBT_CINFO(mc_global, "Counter-example execution trace:");
165     for (auto const& frame : explo->get_textual_trace())
166       XBT_CINFO(mc_global, "  %s", frame.c_str());
167     XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
168              "--cfg=model-check/replay:'%s'",
169              explo->get_record_trace().to_string().c_str());
170     explo->log_state();
171     throw DeadlockError();
172   }
173 }
174
175 void RemoteApp::wait_for_requests()
176 {
177   checker_side_->wait_for_requests();
178 }
179
180 Transition* RemoteApp::handle_simcall(aid_t aid, int times_considered, bool new_transition)
181 {
182   s_mc_message_simcall_execute_t m = {};
183   m.type                           = MessageType::SIMCALL_EXECUTE;
184   m.aid_                           = aid;
185   m.times_considered_              = times_considered;
186   checker_side_->get_channel().send(m);
187
188   auto* memory = get_remote_process_memory();
189   if (memory != nullptr)
190     memory->clear_cache();
191   if (checker_side_->running())
192     checker_side_->dispatch_events(); // The app may send messages while processing the transition
193
194   s_mc_message_simcall_execute_answer_t answer;
195   ssize_t s = checker_side_->get_channel().receive(answer);
196   xbt_assert(s != -1, "Could not receive message");
197   xbt_assert(s == sizeof answer, "Broken message (size=%zd; expected %zu)", s, sizeof answer);
198   xbt_assert(answer.type == MessageType::SIMCALL_EXECUTE_ANSWER,
199              "Received unexpected message %s (%i); expected MessageType::SIMCALL_EXECUTE_ANSWER (%i)",
200              to_c_str(answer.type), (int)answer.type, (int)MessageType::SIMCALL_EXECUTE_ANSWER);
201
202   if (new_transition) {
203     std::stringstream stream(answer.buffer.data());
204     return deserialize_transition(aid, times_considered, stream);
205   } else
206     return nullptr;
207 }
208
209 void RemoteApp::finalize_app(bool terminate_asap)
210 {
211   checker_side_->finalize(terminate_asap);
212 }
213
214 } // namespace simgrid::mc