Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid into no_simix_global
[simgrid.git] / src / mc / api.cpp
1 /* Copyright (c) 2020-2021. 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
31 using Simcall = simgrid::simix::Simcall;
32
33 namespace simgrid {
34 namespace mc {
35
36 static inline const char* get_color(int id)
37 {
38   static constexpr std::array<const char*, 13> colors{{"blue", "red", "green3", "goldenrod", "brown", "purple",
39                                                        "magenta", "turquoise4", "gray25", "forestgreen", "hotpink",
40                                                        "lightblue", "tan"}};
41   return colors[id % colors.size()];
42 }
43
44 static std::string pointer_to_string(void* pointer)
45 {
46   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? xbt::string_printf("%p", pointer) : "(verbose only)";
47 }
48
49 static std::string buff_size_to_string(size_t buff_size)
50 {
51   return XBT_LOG_ISENABLED(Api, xbt_log_priority_verbose) ? std::to_string(buff_size) : "(verbose only)";
52 }
53
54 static void simcall_translate(smx_simcall_t req,
55                               simgrid::mc::Remote<simgrid::kernel::activity::CommImpl>& buffered_comm);
56
57 static bool request_is_enabled_by_idx(const RemoteProcess& process, smx_simcall_t req, unsigned int idx)
58 {
59   kernel::activity::CommImpl* remote_act = nullptr;
60   switch (req->call_) {
61     case Simcall::COMM_WAIT:
62       /* FIXME: check also that src and dst processes are not suspended */
63       remote_act = simcall_comm_wait__getraw__comm(req);
64       break;
65
66     case Simcall::COMM_WAITANY:
67       remote_act = process.read(remote(simcall_comm_waitany__get__comms(req) + idx));
68       break;
69
70     case Simcall::COMM_TESTANY:
71       remote_act = process.read(remote(simcall_comm_testany__get__comms(req) + idx));
72       break;
73
74     default:
75       return true;
76   }
77
78   Remote<kernel::activity::CommImpl> temp_comm;
79   process.read(temp_comm, remote(remote_act));
80   const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
81   return comm->src_actor_.get() && comm->dst_actor_.get();
82 }
83
84 /* Search an enabled transition for the given process.
85  *
86  * This can be seen as an iterator returning the next transition of the process.
87  *
88  * We only consider the processes that are both
89  *  - marked "to be interleaved" in their ActorState (controlled by the checker algorithm).
90  *  - which simcall can currently be executed (like a comm where the other partner is already known)
91  * Once we returned the last enabled transition of a process, it is marked done.
92  *
93  * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
94  * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
95  */
96 static inline smx_simcall_t MC_state_choose_request_for_process(const RemoteProcess& process, simgrid::mc::State* state,
97                                                                 smx_actor_t actor)
98 {
99   /* reset the outgoing transition */
100   simgrid::mc::ActorState* procstate = &state->actor_states_[actor->get_pid()];
101   state->transition_.aid_              = -1;
102   state->transition_.times_considered_ = -1;
103   state->transition_.textual[0]        = '\0';
104   state->executed_req_.call_         = Simcall::NONE;
105
106   if (not simgrid::mc::actor_is_enabled(actor))
107     return nullptr; // Not executable in the application
108
109   smx_simcall_t req = nullptr;
110   if (actor->simcall_.observer_ != nullptr) {
111     state->transition_.times_considered_ = procstate->get_times_considered_and_inc();
112     if (actor->simcall_.mc_max_consider_ <= procstate->get_times_considered())
113       procstate->set_done();
114     req = &actor->simcall_;
115   } else
116     switch (actor->simcall_.call_) {
117       case Simcall::COMM_WAITANY:
118         while (procstate->get_times_considered() < simcall_comm_waitany__get__count(&actor->simcall_)) {
119           if (simgrid::mc::request_is_enabled_by_idx(process, &actor->simcall_, procstate->get_times_considered())) {
120             state->transition_.times_considered_ = procstate->get_times_considered_and_inc();
121             break;
122           }
123           procstate->get_times_considered_and_inc();
124         }
125
126         if (procstate->get_times_considered() >= simcall_comm_waitany__get__count(&actor->simcall_))
127           procstate->set_done();
128         if (state->transition_.times_considered_ != -1)
129           req = &actor->simcall_;
130         break;
131
132       case Simcall::COMM_TESTANY:
133         while (procstate->get_times_considered() < simcall_comm_testany__get__count(&actor->simcall_)) {
134           if (simgrid::mc::request_is_enabled_by_idx(process, &actor->simcall_, procstate->get_times_considered())) {
135             state->transition_.times_considered_ = procstate->get_times_considered_and_inc();
136             break;
137           }
138           procstate->get_times_considered_and_inc();
139         }
140
141         if (procstate->get_times_considered() >= simcall_comm_testany__get__count(&actor->simcall_))
142           procstate->set_done();
143         if (state->transition_.times_considered_ != -1)
144           req = &actor->simcall_;
145         break;
146
147       case Simcall::COMM_WAIT: {
148         simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
149             remote(simcall_comm_wait__get__comm(&actor->simcall_));
150         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
151         process.read(temp_act, remote_act);
152         const simgrid::kernel::activity::CommImpl* act = temp_act.get_buffer();
153         if (act->src_actor_.get() && act->dst_actor_.get())
154           state->transition_.times_considered_ = 0; // OK
155         else if (act->src_actor_.get() == nullptr && act->state_ == simgrid::kernel::activity::State::READY &&
156                  act->detached())
157           state->transition_.times_considered_ = 0; // OK
158         procstate->set_done();
159         req = &actor->simcall_;
160         break;
161       }
162
163       default:
164         procstate->set_done();
165         state->transition_.times_considered_ = 0;
166         req                                  = &actor->simcall_;
167         break;
168     }
169   if (not req)
170     return nullptr;
171
172   state->transition_.aid_ = actor->get_pid();
173   state->executed_req_    = *req;
174
175   // Fetch the data of the request and translate it:
176   state->internal_req_ = *req;
177   state->internal_req_.mc_value_ = state->transition_.times_considered_;
178   simcall_translate(&state->internal_req_, state->internal_comm_);
179
180   return req;
181 }
182
183 static void simcall_translate(smx_simcall_t req,
184                               simgrid::mc::Remote<simgrid::kernel::activity::CommImpl>& buffered_comm)
185 {
186   simgrid::kernel::activity::CommImpl* chosen_comm;
187
188   /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
189    * action so it can be treated later by the dependence function. */
190   switch (req->call_) {
191     case Simcall::COMM_WAITANY:
192       req->call_  = Simcall::COMM_WAIT;
193       chosen_comm =
194           mc_model_checker->get_remote_process().read(remote(simcall_comm_waitany__get__comms(req) + req->mc_value_));
195
196       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
197       simcall_comm_wait__set__comm(req, buffered_comm.get_buffer());
198       simcall_comm_wait__set__timeout(req, 0);
199       break;
200
201     case Simcall::COMM_TESTANY:
202       req->call_  = Simcall::COMM_TEST;
203       chosen_comm =
204           mc_model_checker->get_remote_process().read(remote(simcall_comm_testany__get__comms(req) + req->mc_value_));
205
206       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
207       simcall_comm_test__set__comm(req, buffered_comm.get_buffer());
208       simcall_comm_test__set__result(req, req->mc_value_);
209       break;
210
211     case Simcall::COMM_WAIT:
212       chosen_comm = simcall_comm_wait__get__comm(req);
213       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
214       simcall_comm_wait__set__comm(req, buffered_comm.get_buffer());
215       break;
216
217     case Simcall::COMM_TEST:
218       chosen_comm = simcall_comm_test__get__comm(req);
219       mc_model_checker->get_remote_process().read(buffered_comm, remote(chosen_comm));
220       simcall_comm_test__set__comm(req, buffered_comm.get_buffer());
221       break;
222
223     default:
224       /* No translation needed */
225       break;
226   }
227 }
228
229 simgrid::kernel::activity::CommImpl* Api::get_comm_or_nullptr(smx_simcall_t const r) const
230 {
231   if (r->call_ == Simcall::COMM_WAIT)
232     return simcall_comm_wait__get__comm(r);
233   if (r->call_ == Simcall::COMM_TEST)
234     return simcall_comm_test__get__comm(r);
235   return nullptr;
236 }
237
238 /** Statically "upcast" a s_smx_actor_t into an ActorInformation
239  *
240  *  This gets 'actorInfo' from '&actorInfo->copy'. It upcasts in the
241  *  sense that we could achieve the same thing by having ActorInformation
242  *  inherit from s_smx_actor_t but we don't really want to do that.
243  */
244 simgrid::mc::ActorInformation* Api::actor_info_cast(smx_actor_t actor) const
245 {
246   simgrid::mc::ActorInformation temp;
247   std::size_t offset = (char*)temp.copy.get_buffer() - (char*)&temp;
248
249   auto* process_info = reinterpret_cast<simgrid::mc::ActorInformation*>((char*)actor - offset);
250   return process_info;
251 }
252
253 bool Api::simcall_check_dependency(smx_simcall_t req1, smx_simcall_t req2) const
254 {
255   const auto IRECV = Simcall::COMM_IRECV;
256   const auto ISEND = Simcall::COMM_ISEND;
257   const auto TEST  = Simcall::COMM_TEST;
258   const auto WAIT  = Simcall::COMM_WAIT;
259
260   if (req1->issuer_ == req2->issuer_)
261     return false;
262
263   /* The independence theorem only consider 4 simcalls. All others are dependent with anything. */
264   if (req1->call_ != ISEND && req1->call_ != IRECV && req1->call_ != TEST && req1->call_ != WAIT)
265     return true;
266   if (req2->call_ != ISEND && req2->call_ != IRECV && req2->call_ != TEST && req2->call_ != WAIT)
267     return true;
268
269   /* Timeouts in wait transitions are not considered by the independence theorem, thus assumed dependent */
270   if ((req1->call_ == WAIT && simcall_comm_wait__get__timeout(req1) > 0) ||
271       (req2->call_ == WAIT && simcall_comm_wait__get__timeout(req2) > 0))
272     return true;
273
274   /* Make sure that req1 and req2 are in alphabetic order */
275   if (req1->call_ > req2->call_) {
276     auto temp = req1;
277     req1      = req2;
278     req2      = temp;
279   }
280
281   auto comm1 = get_comm_or_nullptr(req1);
282   auto comm2 = get_comm_or_nullptr(req2);
283
284   /* First case: that's not the same kind of request (we also know that req1 < req2 alphabetically) */
285   if (req1->call_ != req2->call_) {
286     if (req1->call_ == IRECV && req2->call_ == ISEND)
287       return false;
288
289     if ((req1->call_ == IRECV || req1->call_ == ISEND) && req2->call_ == WAIT) {
290       auto mbox1 = get_mbox_remote_addr(req1);
291       auto mbox2 = remote(comm2->mbox_cpy);
292
293       if (mbox1 != mbox2 && simcall_comm_wait__get__timeout(req2) <= 0)
294         return false;
295
296       if ((req1->issuer_ != comm2->src_actor_.get()) && (req1->issuer_ != comm2->dst_actor_.get()) &&
297           simcall_comm_wait__get__timeout(req2) <= 0)
298         return false;
299
300       if ((req1->call_ == ISEND) && (comm2->type_ == kernel::activity::CommImpl::Type::SEND) &&
301           (comm2->src_buff_ != simcall_comm_isend__get__src_buff(req1)) && simcall_comm_wait__get__timeout(req2) <= 0)
302         return false;
303
304       if ((req1->call_ == IRECV) && (comm2->type_ == kernel::activity::CommImpl::Type::RECEIVE) &&
305           (comm2->dst_buff_ != simcall_comm_irecv__get__dst_buff(req1)) && simcall_comm_wait__get__timeout(req2) <= 0)
306         return false;
307     }
308
309     /* FIXME: the following rule assumes that the result of the isend/irecv call is not stored in a buffer used in the
310      * test call. */
311 #if 0
312   if((req1->call == ISEND || req1->call == IRECV)
313       &&  req2->call == TEST)
314     return false;
315 #endif
316
317     if (req1->call_ == TEST && req2->call_ == WAIT &&
318         (comm1->src_actor_.get() == nullptr || comm1->dst_actor_.get() == nullptr))
319       return false;
320
321     if (req1->call_ == TEST &&
322         (simcall_comm_test__get__comm(req1) == nullptr || comm1->src_buff_ == nullptr || comm1->dst_buff_ == nullptr))
323       return false;
324     if (req2->call_ == TEST &&
325         (simcall_comm_test__get__comm(req2) == nullptr || comm2->src_buff_ == nullptr || comm2->dst_buff_ == nullptr))
326       return false;
327
328     if (req1->call_ == TEST && req2->call_ == WAIT && comm1->src_buff_ == comm2->src_buff_ &&
329         comm1->dst_buff_ == comm2->dst_buff_)
330       return false;
331
332     if (req1->call_ == TEST && req2->call_ == WAIT && comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr &&
333         comm2->src_buff_ != nullptr && comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ &&
334         comm1->dst_buff_ != comm2->dst_buff_ && comm2->dst_buff_ != comm1->src_buff_)
335       return false;
336
337     return true;
338   }
339
340   /* Second case: req1 and req2 are of the same call type */
341   switch (req1->call_) {
342     case ISEND:
343       return simcall_comm_isend__get__mbox(req1) == simcall_comm_isend__get__mbox(req2);
344     case IRECV:
345       return simcall_comm_irecv__get__mbox(req1) == simcall_comm_irecv__get__mbox(req2);
346     case WAIT:
347       if (comm1->src_buff_ == comm2->src_buff_ && comm1->dst_buff_ == comm2->dst_buff_)
348         return false;
349       if (comm1->src_buff_ != nullptr && comm1->dst_buff_ != nullptr && comm2->src_buff_ != nullptr &&
350           comm2->dst_buff_ != nullptr && comm1->dst_buff_ != comm2->src_buff_ && comm1->dst_buff_ != comm2->dst_buff_ &&
351           comm2->dst_buff_ != comm1->src_buff_)
352         return false;
353       return true;
354     default:
355       return true;
356   }
357 }
358
359 xbt::string const& Api::get_actor_host_name(smx_actor_t actor) const
360 {
361   if (mc_model_checker == nullptr)
362     return actor->get_host()->get_name();
363
364   const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
365
366   // Read the simgrid::xbt::string in the MCed process:
367   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
368
369   if (not info->hostname) {
370     Remote<s4u::Host> temp_host = process->read(remote(actor->get_host()));
371     auto remote_string_address  = remote(&xbt::string::to_string_data(temp_host.get_buffer()->get_impl()->get_name()));
372     simgrid::xbt::string_data remote_string = process->read(remote_string_address);
373     std::vector<char> hostname(remote_string.len + 1);
374     // no need to read the terminating null byte, and thus hostname[remote_string.len] is guaranteed to be '\0'
375     process->read_bytes(hostname.data(), remote_string.len, remote(remote_string.data));
376     info->hostname = &mc_model_checker->get_host_name(hostname.data());
377   }
378   return *info->hostname;
379 }
380
381 xbt::string const& Api::get_actor_name(smx_actor_t actor) const
382 {
383   if (mc_model_checker == nullptr)
384     return actor->get_name();
385
386   simgrid::mc::ActorInformation* info = actor_info_cast(actor);
387   if (info->name.empty()) {
388     const simgrid::mc::RemoteProcess* process = &mc_model_checker->get_remote_process();
389
390     simgrid::xbt::string_data string_data = simgrid::xbt::string::to_string_data(actor->name_);
391     info->name = process->read_string(remote(string_data.data), string_data.len);
392   }
393   return info->name;
394 }
395
396 std::string Api::get_actor_string(smx_actor_t actor) const
397 {
398   std::string res;
399   if (actor) {
400     res = "(" + std::to_string(actor->get_pid()) + ")";
401     if (actor->get_host())
402       res += std::string(get_actor_host_name(actor)) + " (" + std::string(get_actor_name(actor)) + ")";
403     else
404       res += get_actor_name(actor);
405   } else
406     res = "(0) ()";
407   return res;
408 }
409
410 std::string Api::get_actor_dot_label(smx_actor_t actor) const
411 {
412   std::string res = "(" + std::to_string(actor->get_pid()) + ")";
413   if (actor->get_host())
414     res += get_actor_host_name(actor);
415   return res;
416 }
417
418 simgrid::mc::Checker* Api::initialize(char** argv, simgrid::mc::CheckerAlgorithm algo) const
419 {
420   auto session = new simgrid::mc::Session([argv] {
421     int i = 1;
422     while (argv[i] != nullptr && argv[i][0] == '-')
423       i++;
424     xbt_assert(argv[i] != nullptr,
425                "Unable to find a binary to exec on the command line. Did you only pass config flags?");
426     execvp(argv[i], argv + i);
427     xbt_die("The model-checked process failed to exec(%s): %s", argv[i], strerror(errno));
428   });
429
430   simgrid::mc::Checker* checker;
431   switch (algo) {
432     case CheckerAlgorithm::CommDeterminism:
433       checker = simgrid::mc::create_communication_determinism_checker(session);
434       break;
435
436     case CheckerAlgorithm::UDPOR:
437       checker = simgrid::mc::create_udpor_checker(session);
438       break;
439
440     case CheckerAlgorithm::Safety:
441       checker = simgrid::mc::create_safety_checker(session);
442       break;
443
444     case CheckerAlgorithm::Liveness:
445       checker = simgrid::mc::create_liveness_checker(session);
446       break;
447
448     default:
449       THROW_IMPOSSIBLE;
450   }
451
452   // FIXME: session and checker are never deleted
453   simgrid::mc::session_singleton = session;
454   mc_model_checker->setChecker(checker);
455   return checker;
456 }
457
458 std::vector<simgrid::mc::ActorInformation>& Api::get_actors() const
459 {
460   return mc_model_checker->get_remote_process().actors();
461 }
462
463 unsigned long Api::get_maxpid() const
464 {
465   return mc_model_checker->get_remote_process().get_maxpid();
466 }
467
468 int Api::get_actors_size() const
469 {
470   return mc_model_checker->get_remote_process().actors().size();
471 }
472
473 RemotePtr<kernel::activity::CommImpl> Api::get_comm_isend_raw_addr(smx_simcall_t request) const
474 {
475   return remote(static_cast<kernel::activity::CommImpl*>(simcall_comm_isend__getraw__result(request)));
476 }
477
478 RemotePtr<kernel::activity::CommImpl> Api::get_comm_waitany_raw_addr(smx_simcall_t request, int value) const
479 {
480   auto addr      = simcall_comm_waitany__getraw__comms(request) + value;
481   auto comm_addr = mc_model_checker->get_remote_process().read(remote(addr));
482   return RemotePtr<kernel::activity::CommImpl>(static_cast<kernel::activity::CommImpl*>(comm_addr));
483 }
484
485 std::string Api::get_pattern_comm_rdv(RemotePtr<kernel::activity::CommImpl> const& addr) const
486 {
487   Remote<kernel::activity::CommImpl> temp_activity;
488   mc_model_checker->get_remote_process().read(temp_activity, addr);
489   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
490
491   char* remote_name = mc_model_checker->get_remote_process().read<char*>(RemotePtr<char*>(
492       (uint64_t)(activity->get_mailbox() ? &activity->get_mailbox()->get_name() : &activity->mbox_cpy->get_name())));
493   auto rdv          = mc_model_checker->get_remote_process().read_string(RemotePtr<char>(remote_name));
494   return rdv;
495 }
496
497 unsigned long Api::get_pattern_comm_src_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
498 {
499   Remote<kernel::activity::CommImpl> temp_activity;
500   mc_model_checker->get_remote_process().read(temp_activity, addr);
501   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
502   auto src_proc =
503       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->src_actor_.get()))->get_pid();
504   return src_proc;
505 }
506
507 unsigned long Api::get_pattern_comm_dst_proc(RemotePtr<kernel::activity::CommImpl> const& addr) const
508 {
509   Remote<kernel::activity::CommImpl> temp_activity;
510   mc_model_checker->get_remote_process().read(temp_activity, addr);
511   const kernel::activity::CommImpl* activity = temp_activity.get_buffer();
512   auto src_proc =
513       mc_model_checker->get_remote_process().resolve_actor(mc::remote(activity->dst_actor_.get()))->get_pid();
514   return src_proc;
515 }
516
517 std::vector<char> Api::get_pattern_comm_data(RemotePtr<kernel::activity::CommImpl> const& addr) const
518 {
519   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
520   mc_model_checker->get_remote_process().read(temp_comm, addr);
521   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
522
523   std::vector<char> buffer{};
524   if (comm->src_buff_ != nullptr) {
525     buffer.resize(comm->src_buff_size_);
526     mc_model_checker->get_remote_process().read_bytes(buffer.data(), buffer.size(), remote(comm->src_buff_));
527   }
528   return buffer;
529 }
530
531 #if HAVE_SMPI
532 bool Api::check_send_request_detached(smx_simcall_t const& simcall) const
533 {
534   Remote<simgrid::smpi::Request> mpi_request;
535   mc_model_checker->get_remote_process().read(
536       mpi_request, remote(static_cast<smpi::Request*>(simcall_comm_isend__get__data(simcall))));
537   return mpi_request.get_buffer()->detached();
538 }
539 #endif
540
541 smx_actor_t Api::get_src_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
542 {
543   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
544   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
545   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
546
547   auto src_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->src_actor_.get()));
548   return src_proc;
549 }
550
551 smx_actor_t Api::get_dst_actor(RemotePtr<kernel::activity::CommImpl> const& comm_addr) const
552 {
553   simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_comm;
554   mc_model_checker->get_remote_process().read(temp_comm, comm_addr);
555   const simgrid::kernel::activity::CommImpl* comm = temp_comm.get_buffer();
556
557   auto dst_proc = mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(comm->dst_actor_.get()));
558   return dst_proc;
559 }
560
561 std::size_t Api::get_remote_heap_bytes() const
562 {
563   RemoteProcess& process    = mc_model_checker->get_remote_process();
564   auto heap_bytes_used      = mmalloc_get_bytes_used_remote(process.get_heap()->heaplimit, process.get_malloc_info());
565   return heap_bytes_used;
566 }
567
568 void Api::mc_inc_visited_states() const
569 {
570   mc_model_checker->visited_states++;
571 }
572
573 void Api::mc_inc_executed_trans() const
574 {
575   mc_model_checker->executed_transitions++;
576 }
577
578 unsigned long Api::mc_get_visited_states() const
579 {
580   return mc_model_checker->visited_states;
581 }
582
583 unsigned long Api::mc_get_executed_trans() const
584 {
585   return mc_model_checker->executed_transitions;
586 }
587
588 void Api::mc_check_deadlock() const
589 {
590   if (mc_model_checker->checkDeadlock()) {
591     MC_show_deadlock();
592     throw DeadlockError();
593   }
594 }
595
596 /** Get the issuer of a simcall (`req->issuer`)
597  *
598  *  In split-process mode, it does the black magic necessary to get an address
599  *  of a (shallow) copy of the data structure the issuer SIMIX actor in the local
600  *  address space.
601  *
602  *  @param process the MCed process
603  *  @param req     the simcall (copied in the local process)
604  */
605 smx_actor_t Api::simcall_get_issuer(s_smx_simcall const* req) const
606 {
607   xbt_assert(mc_model_checker != nullptr);
608
609   // This is the address of the smx_actor in the MCed process:
610   auto address = simgrid::mc::remote(req->issuer_);
611
612   // Lookup by address:
613   for (auto& actor : mc_model_checker->get_remote_process().actors())
614     if (actor.address == address)
615       return actor.copy.get_buffer();
616   for (auto& actor : mc_model_checker->get_remote_process().dead_actors())
617     if (actor.address == address)
618       return actor.copy.get_buffer();
619
620   xbt_die("Issuer not found");
621 }
622
623 long Api::simcall_get_actor_id(s_smx_simcall const* req) const
624 {
625   return simcall_get_issuer(req)->get_pid();
626 }
627
628 RemotePtr<kernel::activity::MailboxImpl> Api::get_mbox_remote_addr(smx_simcall_t const req) const
629 {
630   if (req->call_ == Simcall::COMM_ISEND)
631     return remote(simcall_comm_isend__get__mbox(req));
632   if (req->call_ == Simcall::COMM_IRECV)
633     return remote(simcall_comm_irecv__get__mbox(req));
634   THROW_IMPOSSIBLE;
635 }
636
637 RemotePtr<kernel::activity::ActivityImpl> Api::get_comm_remote_addr(smx_simcall_t const req) const
638 {
639   if (req->call_ == Simcall::COMM_ISEND)
640     return remote(simcall_comm_isend__getraw__result(req));
641   if (req->call_ == Simcall::COMM_IRECV)
642     return remote(simcall_comm_irecv__getraw__result(req));
643   THROW_IMPOSSIBLE;
644 }
645
646 bool Api::mc_is_null() const
647 {
648   auto is_null = (mc_model_checker == nullptr) ? true : false;
649   return is_null;
650 }
651
652 Checker* Api::mc_get_checker() const
653 {
654   return mc_model_checker->getChecker();
655 }
656
657 void Api::handle_simcall(Transition const& transition) const
658 {
659   mc_model_checker->handle_simcall(transition);
660 }
661
662 void Api::mc_wait_for_requests() const
663 {
664   mc_model_checker->wait_for_requests();
665 }
666
667 void Api::mc_exit(int status) const
668 {
669   mc_model_checker->exit(status);
670 }
671
672 void Api::dump_record_path() const
673 {
674   simgrid::mc::dumpRecordPath();
675 }
676
677 smx_simcall_t Api::mc_state_choose_request(simgrid::mc::State* state) const
678 {
679   RemoteProcess& process = mc_model_checker->get_remote_process();
680   for (auto& actor : process.actors()) {
681     /* Only consider the actors that were marked as interleaving by the checker algorithm */
682     if (not state->actor_states_[actor.copy.get_buffer()->get_pid()].is_todo())
683       continue;
684
685     smx_simcall_t res = MC_state_choose_request_for_process(process, state, actor.copy.get_buffer());
686     if (res)
687       return res;
688   }
689   return nullptr;
690 }
691
692 std::list<transition_detail_t> Api::get_enabled_transitions(simgrid::mc::State* state) const
693 {
694   std::list<transition_detail_t> tr_list{};
695
696   for (auto& actor : mc_model_checker->get_remote_process().actors()) {
697     auto actor_pid  = actor.copy.get_buffer()->get_pid();
698     auto actor_impl = actor.copy.get_buffer();
699
700     // Only consider the actors that were marked as interleaving by the checker algorithm
701     if (not state->actor_states_[actor_pid].is_todo())
702       continue;
703     // Not executable in the application
704     if (not simgrid::mc::actor_is_enabled(actor_impl))
705       continue;
706
707     auto transition       = std::make_unique<s_transition_detail>();
708     Simcall simcall_call  = actor_impl->simcall_.call_;
709     smx_simcall_t simcall = &actor_impl->simcall_;
710     transition->call_     = simcall_call;
711     switch (simcall_call) {
712       case Simcall::COMM_ISEND:
713       case Simcall::COMM_IRECV:
714         transition->mbox_remote_addr = get_mbox_remote_addr(simcall);
715         transition->comm_remote_addr = get_comm_remote_addr(simcall);
716         break;
717
718       default:
719         break;
720     }
721     tr_list.emplace_back(std::move(transition));
722   }
723
724   return tr_list;
725 }
726
727 std::string Api::request_to_string(smx_simcall_t req, int value) const
728 {
729   xbt_assert(mc_model_checker != nullptr, "Must be called from MCer");
730
731   std::string type;
732   std::string args;
733
734   smx_actor_t issuer = simcall_get_issuer(req);
735
736   if (issuer->simcall_.observer_ != nullptr)
737     return mc_model_checker->simcall_to_string(issuer->get_pid(), value);
738
739   switch (req->call_) {
740     case Simcall::COMM_ISEND:
741       type = "iSend";
742       args = "src=" + get_actor_string(issuer);
743       args += ", buff=" + pointer_to_string(simcall_comm_isend__get__src_buff(req));
744       args += ", size=" + buff_size_to_string(simcall_comm_isend__get__src_buff_size(req));
745       break;
746
747     case Simcall::COMM_IRECV: {
748       size_t* remote_size = simcall_comm_irecv__get__dst_buff_size(req);
749       size_t size         = 0;
750       if (remote_size)
751         mc_model_checker->get_remote_process().read_bytes(&size, sizeof(size), remote(remote_size));
752
753       type = "iRecv";
754       args = "dst=" + get_actor_string(issuer);
755       args += ", buff=" + pointer_to_string(simcall_comm_irecv__get__dst_buff(req));
756       args += ", size=" + buff_size_to_string(size);
757       break;
758     }
759
760     case Simcall::COMM_WAIT: {
761       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_wait__get__comm(req);
762       if (value == -1) {
763         type = "WaitTimeout";
764         args = "comm=" + pointer_to_string(remote_act);
765       } else {
766         type = "Wait";
767
768         simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_activity;
769         const simgrid::kernel::activity::CommImpl* act;
770         mc_model_checker->get_remote_process().read(temp_activity, remote(remote_act));
771         act = temp_activity.get_buffer();
772
773         smx_actor_t src_proc =
774             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
775         smx_actor_t dst_proc =
776             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
777         args = "comm=" + pointer_to_string(remote_act);
778         args += " [" + get_actor_string(src_proc) + "-> " + get_actor_string(dst_proc) + "]";
779       }
780       break;
781     }
782
783     case Simcall::COMM_TEST: {
784       simgrid::kernel::activity::CommImpl* remote_act = simcall_comm_test__get__comm(req);
785       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_activity;
786       const simgrid::kernel::activity::CommImpl* act;
787       mc_model_checker->get_remote_process().read(temp_activity, remote(remote_act));
788       act = temp_activity.get_buffer();
789
790       if (act->src_actor_.get() == nullptr || act->dst_actor_.get() == nullptr) {
791         type = "Test FALSE";
792         args = "comm=" + pointer_to_string(remote_act);
793       } else {
794         type = "Test TRUE";
795
796         smx_actor_t src_proc =
797             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->src_actor_.get()));
798         smx_actor_t dst_proc =
799             mc_model_checker->get_remote_process().resolve_actor(simgrid::mc::remote(act->dst_actor_.get()));
800         args = "comm=" + pointer_to_string(remote_act);
801         args += " [" + get_actor_string(src_proc) + " -> " + get_actor_string(dst_proc) + "]";
802       }
803       break;
804     }
805
806     case Simcall::COMM_WAITANY: {
807       type         = "WaitAny";
808       size_t count = simcall_comm_waitany__get__count(req);
809       if (count > 0) {
810         simgrid::kernel::activity::CommImpl* remote_sync;
811         remote_sync =
812             mc_model_checker->get_remote_process().read(remote(simcall_comm_waitany__get__comms(req) + value));
813         args = "comm=" + pointer_to_string(remote_sync) + xbt::string_printf("(%d of %zu)", value + 1, count);
814       } else
815         args = "comm at idx " + std::to_string(value);
816       break;
817     }
818
819     case Simcall::COMM_TESTANY:
820       if (value == -1) {
821         type = "TestAny FALSE";
822         args = "-";
823       } else {
824         type = "TestAny";
825         args = xbt::string_printf("(%d of %zu)", value + 1, simcall_comm_testany__get__count(req));
826       }
827       break;
828
829     default:
830       type = SIMIX_simcall_name(*req);
831       args = "??";
832       break;
833   }
834
835   return "[" + get_actor_string(issuer) + "] " + type + "(" + args + ")";
836 }
837
838 std::string Api::request_get_dot_output(smx_simcall_t req, int value) const
839 {
840   const smx_actor_t issuer = simcall_get_issuer(req);
841   const char* color        = get_color(issuer->get_pid() - 1);
842
843   std::string label;
844
845   if (req->observer_ != nullptr) {
846     label = mc_model_checker->simcall_dot_label(issuer->get_pid(), value);
847   } else
848     switch (req->call_) {
849       case Simcall::COMM_ISEND:
850         label = "[" + get_actor_dot_label(issuer) + "] iSend";
851         break;
852
853       case Simcall::COMM_IRECV:
854         label = "[" + get_actor_dot_label(issuer) + "] iRecv";
855         break;
856
857       case Simcall::COMM_WAIT:
858         if (value == -1) {
859           label = "[" + get_actor_dot_label(issuer) + "] WaitTimeout";
860         } else {
861           kernel::activity::ActivityImpl* remote_act = simcall_comm_wait__get__comm(req);
862           Remote<kernel::activity::CommImpl> temp_comm;
863           mc_model_checker->get_remote_process().read(temp_comm,
864                                                       remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
865           const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
866
867           const kernel::actor::ActorImpl* src_proc =
868               mc_model_checker->get_remote_process().resolve_actor(mc::remote(comm->src_actor_.get()));
869           const kernel::actor::ActorImpl* dst_proc =
870               mc_model_checker->get_remote_process().resolve_actor(mc::remote(comm->dst_actor_.get()));
871           label = "[" + get_actor_dot_label(issuer) + "] Wait";
872           label += " [(" + std::to_string(src_proc ? src_proc->get_pid() : 0) + ")";
873           label += "->(" + std::to_string(dst_proc ? dst_proc->get_pid() : 0) + ")]";
874         }
875         break;
876
877       case Simcall::COMM_TEST: {
878         kernel::activity::ActivityImpl* remote_act = simcall_comm_test__get__comm(req);
879         Remote<simgrid::kernel::activity::CommImpl> temp_comm;
880         mc_model_checker->get_remote_process().read(temp_comm,
881                                                     remote(static_cast<kernel::activity::CommImpl*>(remote_act)));
882         const kernel::activity::CommImpl* comm = temp_comm.get_buffer();
883         if (comm->src_actor_.get() == nullptr || comm->dst_actor_.get() == nullptr) {
884           label = "[" + get_actor_dot_label(issuer) + "] Test FALSE";
885         } else {
886           label = "[" + get_actor_dot_label(issuer) + "] Test TRUE";
887         }
888         break;
889       }
890
891       case Simcall::COMM_WAITANY:
892         label = "[" + get_actor_dot_label(issuer) + "] WaitAny";
893         label += xbt::string_printf(" [%d of %zu]", value + 1, simcall_comm_waitany__get__count(req));
894         break;
895
896       case Simcall::COMM_TESTANY:
897         if (value == -1) {
898           label = "[" + get_actor_dot_label(issuer) + "] TestAny FALSE";
899         } else {
900           label = "[" + get_actor_dot_label(issuer) + "] TestAny TRUE";
901           label += xbt::string_printf(" [%d of %zu]", value + 1, simcall_comm_testany__get__count(req));
902         }
903         break;
904
905       default:
906         THROW_UNIMPLEMENTED;
907     }
908
909   return "label = \"" + label + "\", color = " + color + ", fontcolor = " + color;
910 }
911
912 #if HAVE_SMPI
913 int Api::get_smpi_request_tag(smx_simcall_t const& simcall, simgrid::simix::Simcall type) const
914 {
915   void* simcall_data = nullptr;
916   if (type == Simcall::COMM_ISEND)
917     simcall_data = simcall_comm_isend__get__data(simcall);
918   else if (type == Simcall::COMM_IRECV)
919     simcall_data = simcall_comm_irecv__get__data(simcall);
920   Remote<simgrid::smpi::Request> mpi_request;
921   mc_model_checker->get_remote_process().read(mpi_request, remote(static_cast<smpi::Request*>(simcall_data)));
922   return mpi_request.get_buffer()->tag();
923 }
924 #endif
925
926 void Api::restore_state(std::shared_ptr<simgrid::mc::Snapshot> system_state) const
927 {
928   system_state->restore(&mc_model_checker->get_remote_process());
929 }
930
931 void Api::log_state() const
932 {
933   session_singleton->log_state();
934 }
935
936 bool Api::snapshot_equal(const Snapshot* s1, const Snapshot* s2) const
937 {
938   return simgrid::mc::snapshot_equal(s1, s2);
939 }
940
941 simgrid::mc::Snapshot* Api::take_snapshot(int num_state) const
942 {
943   auto snapshot = new simgrid::mc::Snapshot(num_state);
944   return snapshot;
945 }
946
947 void Api::s_close() const
948 {
949   session_singleton->close();
950 }
951
952 void Api::execute(Transition& transition, smx_simcall_t simcall) const
953 {
954   /* FIXME: once all simcalls have observers, kill the simcall parameter and use mc_model_checker->simcall_to_string() */
955   transition.textual = request_to_string(simcall, transition.times_considered_);
956   session_singleton->execute(transition);
957 }
958
959 void Api::automaton_load(const char* file) const
960 {
961   MC_automaton_load(file);
962 }
963
964 std::vector<int> Api::automaton_propositional_symbol_evaluate() const
965 {
966   unsigned int cursor = 0;
967   std::vector<int> values;
968   xbt_automaton_propositional_symbol_t ps = nullptr;
969   xbt_dynar_foreach (mc::property_automaton->propositional_symbols, cursor, ps)
970     values.push_back(xbt_automaton_propositional_symbol_evaluate(ps));
971   return values;
972 }
973
974 std::vector<xbt_automaton_state_t> Api::get_automaton_state() const
975 {
976   std::vector<xbt_automaton_state_t> automaton_stack;
977   unsigned int cursor = 0;
978   xbt_automaton_state_t automaton_state;
979   xbt_dynar_foreach (mc::property_automaton->states, cursor, automaton_state)
980     if (automaton_state->type == -1)
981       automaton_stack.push_back(automaton_state);
982   return automaton_stack;
983 }
984
985 int Api::compare_automaton_exp_label(const xbt_automaton_exp_label* l) const
986 {
987   unsigned int cursor                    = 0;
988   xbt_automaton_propositional_symbol_t p = nullptr;
989   xbt_dynar_foreach (simgrid::mc::property_automaton->propositional_symbols, cursor, p) {
990     if (std::strcmp(xbt_automaton_propositional_symbol_get_name(p), l->u.predicat) == 0)
991       return cursor;
992   }
993   return -1;
994 }
995
996 void Api::set_property_automaton(xbt_automaton_state_t const& automaton_state) const
997 {
998   mc::property_automaton->current_state = automaton_state;
999 }
1000
1001 xbt_automaton_exp_label_t Api::get_automaton_transition_label(xbt_dynar_t const& dynar, int index) const
1002 {
1003   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1004   return transition->label;
1005 }
1006
1007 xbt_automaton_state_t Api::get_automaton_transition_dst(xbt_dynar_t const& dynar, int index) const
1008 {
1009   const xbt_automaton_transition* transition = xbt_dynar_get_as(dynar, index, xbt_automaton_transition_t);
1010   return transition->dst;
1011 }
1012
1013 } // namespace mc
1014 } // namespace simgrid