Logo AND Algorithmique Numérique Distribuée

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