Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Inline another useless function in MC and a define symbol
[simgrid.git] / src / mc / api.cpp
1 /* Copyright (c) 2020-2022. 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 "api.hpp"
7
8 #include "src/kernel/activity/MailboxImpl.hpp"
9 #include "src/kernel/activity/MutexImpl.hpp"
10 #include "src/kernel/actor/SimcallObserver.hpp"
11 #include "src/mc/Session.hpp"
12 #include "src/mc/checker/Checker.hpp"
13 #include "src/mc/mc_base.hpp"
14 #include "src/mc/mc_comm_pattern.hpp"
15 #include "src/mc/mc_exit.hpp"
16 #include "src/mc/mc_pattern.hpp"
17 #include "src/mc/mc_private.hpp"
18 #include "src/mc/remote/RemoteProcess.hpp"
19 #include "src/surf/HostImpl.hpp"
20
21 #include <xbt/asserts.h>
22 #include <xbt/log.h>
23 #include "simgrid/s4u/Host.hpp"
24 #include "xbt/string.hpp"
25 #if HAVE_SMPI
26 #include "src/smpi/include/smpi_request.hpp"
27 #endif
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Api, mc, "Logging specific to MC Facade APIs ");
30 XBT_LOG_EXTERNAL_CATEGORY(mc_global);
31
32 using Simcall = simgrid::simix::Simcall;
33
34 namespace simgrid {
35 namespace mc {
36
37 static inline const char* get_color(int id)
38 {
39   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
40                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
41                                                        "lightblue", "tan"}};
42   return colors[id % colors.size()];
43 }
44
45 static std::string pointer_to_string(void* pointer)
46 {
47   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? xbt::string_printf("%p", pointer) : "(verbose only)";
48 }
49
50 static std::string buff_size_to_string(size_t buff_size)
51 {
52   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? std::to_string(buff_size) : "(verbose only)";
53 }
54
55 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
56  *
57  *  This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
58  *  sense that we could achieve the same thing by having ActorInformation
59  *  inherit from s_smx_actor_t but we don't really want to do that.
60  */
61 simgrid::mc::ActorInformation* Api::actor_info_cast(smx_actor_t actor) const
62 {
63   simgrid::mc::ActorInformation temp;
64   std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
65
66   auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
67   return process_info;
68 }
69
70 xbt::string const& Api::get_actor_host_name(smx_actor_t actor) const
71 {
72   if (mc_model_checker == nullptr)
73     return actor->get_host()->get_name();
74
75   const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
76
77   // Read the simgrid::xbt::string in the MCed process:
78   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
79
80   if (not info->hostname) {
81     Remote<s4u::Host> temp_host = process->read(remote(actor->get_host()));
82     auto remote_string_address  = remote(&xbt::string::to_string_data(temp_host.get_buffer()->get_impl()->get_name()));
83     simgrid::xbt::string_data remote_string = process->read(remote_string_address);
84     std::vector<char> hostname(remote_string.len + 1);
85     // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
86     process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
87     info->hostname = &mc_model_checker->get_host_name(hostname.data());
88   }
89   return *info->hostname;
90 }
91
92 xbt::string const& Api::get_actor_name(smx_actor_t actor) const
93 {
94   if (mc_model_checker == nullptr)
95     return actor->get_name();
96
97   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
98   if (info->name.empty()) {
99     const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
100
101     simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
102     info->name = process->read_string(remote(string_data.data), string_data.len);
103   }
104   return info->name;
105 }
106
107 std::string Api::get_actor_string(smx_actor_t actor) const
108 {
109   std::string res;
110   if (actor) {
111     res = "(" + std::to_string(actor->get_pid()) + ")";
112     if (actor->get_host())
113       res += std::string(get_actor_host_name(actor)) + " (" + std::string(get_actor_name(actor)) + ")";
114     else
115       res += get_actor_name(actor);
116   } else
117     res = "(0) ()";
118   return res;
119 }
120
121 std::string Api::get_actor_dot_label(smx_actor_t actor) const
122 {
123   std::string res = "(" + std::to_string(actor->get_pid()) + ")";
124   if (actor->get_host())
125     res += get_actor_host_name(actor);
126   return res;
127 }
128
129 simgrid::mc::Checker* Api::initialize(char** argv, simgrid::mc::CheckerAlgorithm algo) const
130 {
131   auto session = new simgrid::mc::Session([argv] {
132     int i = 1;
133     while (argv[i] != nullptr && argv[i][0] == '-')
134       i++;
135     xbt_assert(argv[i] != nullptr,
136                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
137     execvp(argv[i], argv + i);
138     xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
139   });
140
141   simgrid::mc::Checker* checker;
142   switch (algo) {
143     case CheckerAlgorithm::CommDeterminism:
144       checker = simgrid::mc::create_communication_determinism_checker(session);
145       break;
146
147     case CheckerAlgorithm::UDPOR:
148       checker = simgrid::mc::create_udpor_checker(session);
149       break;
150
151     case CheckerAlgorithm::Safety:
152       checker = simgrid::mc::create_safety_checker(session);
153       break;
154
155     case CheckerAlgorithm::Liveness:
156       checker = simgrid::mc::create_liveness_checker(session);
157       break;
158
159     default:
160       THROW_IMPOSSIBLE;
161   }
162
163   // FIXME: session and checker are never deleted
164   simgrid::mc::session_singleton = session;
165   mc_model_checker->setChecker(checker);
166   return checker;
167 }
168
169 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
170 {
171   return mc_model_checker->get_remote_process().actors();
172 }
173
174 unsigned long Api::get_maxpid() const
175 {
176   return mc_model_checker->get_remote_process().get_maxpid();
177 }
178
179 int Api::get_actors_size() const
180 {
181   return mc_model_checker->get_remote_process().actors().size();
182 }
183
184 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
185 {
186   return remote(static_cast<kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request)));
187 }
188
189 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
190 {
191   auto addr      = simcall_comm_waitany__getraw__comms(request) + value;
192   auto comm_addr = mc_model_checker->get_remote_process().read(remote(addr));
193   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
194 }
195
196 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
197 {
198   Remote<kernel::activity::CommImpl> temp_activity;
199   mc_model_checker->get_remote_process().read(temp_activity, addr);
200   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
201
202   char* remote_name = mc_model_checker->get_remote_process().read<char*>(RemotePtr<char*>(
203       (uint64_t)(activity->get_mailbox() ? &activity->get_mailbox()->get_name() : &activity->mbox_cpy->get_name())));
204   auto rdv          = mc_model_checker->get_remote_process().read_string(RemotePtr<char>(remote_name));
205   return rdv;
206 }
207
208 unsigned long Api::get_pattern_comm_src_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
209 {
210   Remote<kernel::activity::CommImpl> temp_activity;
211   mc_model_checker->get_remote_process().read(temp_activity, addr);
212   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
213   auto src_proc =
214       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->src_actor_.get()))->get_pid();
215   return src_proc;
216 }
217
218 unsigned long Api::get_pattern_comm_dst_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
219 {
220   Remote<kernel::activity::CommImpl> temp_activity;
221   mc_model_checker->get_remote_process().read(temp_activity, addr);
222   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
223   auto src_proc =
224       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->dst_actor_.get()))->get_pid();
225   return src_proc;
226 }
227
228 std::vector<char> Api::get_pattern_comm_data(RemotePtr<kernel::activity::CommImpl> const& addr) const
229 {
230   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
231   mc_model_checker->get_remote_process().read(temp_comm, addr);
232   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
233
234   std::vector<char> buffer{};
235   if (comm->src_buff_ != nullptr) {
236     buffer.resize(comm->src_buff_size_);
237     mc_model_checker->get_remote_process().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
238   }
239   return buffer;
240 }
241
242 #if HAVE_SMPI
243 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
244 {
245   Remote<simgrid::smpi::Request> mpi_request;
246   mc_model_checker->get_remote_process().read(
247       mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
248   return mpi_request.get_buffer()->detached();
249 }
250 #endif
251
252 smx_actor_t Api::get_src_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
253 {
254   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
255   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
256   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
257
258   auto src_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
259   return src_proc;
260 }
261
262 smx_actor_t Api::get_dst_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
263 {
264   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
265   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
266   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
267
268   auto dst_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
269   return dst_proc;
270 }
271
272 std::size_t Api::get_remote_heap_bytes() const
273 {
274   RemoteProcess& process    = mc_model_checker->get_remote_process();
275   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
276   return heap_bytes_used;
277 }
278
279 void Api::mc_inc_visited_states() const
280 {
281   mc_model_checker->visited_states++;
282 }
283
284 unsigned long Api::mc_get_visited_states() const
285 {
286   return mc_model_checker->visited_states;
287 }
288
289 void Api::mc_check_deadlock() const
290 {
291   if (mc_model_checker->checkDeadlock()) {
292     XBT_CINFO(mc_global, "**************************");
293     XBT_CINFO(mc_global, "*** DEADLOCK DETECTED ***");
294     XBT_CINFO(mc_global, "**************************");
295     XBT_CINFO(mc_global, "Counter-example execution trace:");
296     for (auto const& s : mc_model_checker->getChecker()->get_textual_trace())
297       XBT_CINFO(mc_global, "  %s", s.c_str());
298     simgrid::mc::dumpRecordPath();
299     simgrid::mc::session_singleton->log_state();
300     throw DeadlockError();
301   }
302 }
303
304 /** Get the issuer of a simcall (`req->issuer`)
305  *
306  *  In split-process mode, it does the black magic necessary to get an address
307  *  of a (shallow) copy of the data structure the issuer SIMIX actor in the local
308  *  address space.
309  *
310  *  @param process the MCed process
311  *  @param req     the simcall (copied in the local process)
312  */
313 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
314 {
315   xbt_assert(mc_model_checker != nullptr);
316
317   // This is the address of the smx_actor in the MCed process:
318   auto address = simgrid::mc::remote(req->issuer_);
319
320   // Lookup by address:
321   for (auto& actor : mc_model_checker->get_remote_process().actors())
322     if (actor.address == address)
323       return actor.copy.get_buffer();
324   for (auto& actor : mc_model_checker->get_remote_process().dead_actors())
325     if (actor.address == address)
326       return actor.copy.get_buffer();
327
328   xbt_die("Issuer not found");
329 }
330
331 RemotePtr<kernel::activity::MailboxImpl> Api::get_mbox_remote_addr(smx_simcall_t const req) const
332 {
333   if (req->call_ == Simcall::COMM_ISEND)
334     return remote(simcall_comm_isend__get__mbox(req));
335   if (req->call_ == Simcall::COMM_IRECV)
336     return remote(simcall_comm_irecv__get__mbox(req));
337   THROW_IMPOSSIBLE;
338 }
339
340 RemotePtr<kernel::activity::ActivityImpl> Api::get_comm_remote_addr(smx_simcall_t const req) const
341 {
342   if (req->call_ == Simcall::COMM_ISEND)
343     return remote(simcall_comm_isend__getraw__result(req));
344   if (req->call_ == Simcall::COMM_IRECV)
345     return remote(simcall_comm_irecv__getraw__result(req));
346   THROW_IMPOSSIBLE;
347 }
348
349 void Api::mc_exit(int status) const
350 {
351   mc_model_checker->exit(status);
352 }
353
354 std::string Api::request_get_dot_output(aid_t aid, int value) const
355 {
356   const char* color = get_color(aid - 1);
357   return "label = \"" + mc_model_checker->simcall_dot_label(aid, value) + "\", color = " + color +
358          ", fontcolor = " + color;
359 }
360
361 #if HAVE_SMPI
362 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
363 {
364   void* simcall_data = nullptr;
365   if (type == Simcall::COMM_ISEND)
366     simcall_data = simcall_comm_isend__get__data(simcall);
367   else if (type == Simcall::COMM_IRECV)
368     simcall_data = simcall_comm_irecv__get__data(simcall);
369   Remote<simgrid::smpi::Request> mpi_request;
370   mc_model_checker->get_remote_process().read(mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
371   return mpi_request.get_buffer()->tag();
372 }
373 #endif
374
375 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
376 {
377   system_state->restore(&mc_model_checker->get_remote_process());
378 }
379
380 void Api::log_state() const
381 {
382   session_singleton->log_state();
383 }
384
385 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
386 {
387   return simgrid::mc::snapshot_equal(s1, s2);
388 }
389
390 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
391 {
392   auto snapshot = new simgrid::mc::Snapshot(num_state);
393   return snapshot;
394 }
395
396 void Api::s_close() const
397 {
398   session_singleton->close();
399 }
400
401 void Api::automaton_load(const char* file) const
402 {
403   if (simgrid::mc::property_automaton == nullptr)
404     simgrid::mc::property_automaton = xbt_automaton_new();
405
406   xbt_automaton_load(simgrid::mc::property_automaton, file);
407 }
408
409 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
410 {
411   unsigned int cursor = 0;
412   std::vector<int> values;
413   xbt_automaton_propositional_symbol_t ps = nullptr;
414   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
415     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
416   return values;
417 }
418
419 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
420 {
421   std::vector<xbt_automaton_state_t> automaton_stack;
422   unsigned int cursor = 0;
423   xbt_automaton_state_t automaton_state;
424   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
425     if (automaton_state->type == -1)
426       automaton_stack.push_back(automaton_state);
427   return automaton_stack;
428 }
429
430 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
431 {
432   unsigned int cursor                    = 0;
433   xbt_automaton_propositional_symbol_t p = nullptr;
434   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
435     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
436       return cursor;
437   }
438   return -1;
439 }
440
441 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
442 {
443   mc::property_automaton->current_state = automaton_state;
444 }
445
446 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
447 {
448   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
449   return transition->label;
450 }
451
452 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
453 {
454   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
455   return transition->dst;
456 }
457
458 } // namespace mc
459 } // namespace simgrid