Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Sanitize how we know the current MC mode
[simgrid.git] / src / mc / mc_client_api.cpp
1 /* Copyright (c) 2008-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/simix.hpp"
7 #include "src/kernel/actor/ActorImpl.hpp"
8 #include "src/mc/mc_config.hpp"
9 #include "src/mc/mc_private.hpp"
10 #include "src/mc/mc_record.hpp"
11 #include "src/mc/mc_replay.hpp"
12 #include "src/mc/remote/AppSide.hpp"
13 #include "xbt/asserts.h"
14 #include "xbt/random.hpp"
15
16 /* Implementation of the user API from the App to the Checker (see modelchecker.h)  */
17
18 int MC_random(int min, int max)
19 {
20 #if SIMGRID_HAVE_MC
21   xbt_assert(simgrid::mc::model_checking_mode != simgrid::mc::ModelCheckingMode::CHECKER_SIDE,
22              "This should be called from the client side");
23 #endif
24   if (not MC_is_active() && not MC_record_replay_is_active()) { // no need to do a simcall in this case
25     static simgrid::xbt::random::XbtRandom prng;
26     return prng.uniform_int(min, max);
27   }
28   simgrid::kernel::actor::RandomSimcall observer{simgrid::kernel::actor::ActorImpl::self(), min, max};
29   return simgrid::kernel::actor::simcall_answered([&observer] { return observer.get_value(); }, &observer);
30 }
31
32 void MC_assert(int prop)
33 {
34   // Cannot used xbt_assert here, or it would be an infinite recursion.
35 #if SIMGRID_HAVE_MC
36   xbt_assert(simgrid::mc::model_checking_mode != simgrid::mc::ModelCheckingMode::CHECKER_SIDE,
37              "This should be called from the client side");
38   if (not prop) {
39     if (MC_is_active())
40       simgrid::mc::AppSide::get()->report_assertion_failure();
41     if (MC_record_replay_is_active())
42       xbt_die("MC assertion failed");
43   }
44 #else
45   if (not prop)
46     xbt_die("Safety property violation detected without the model-checker");
47 #endif
48 }
49
50 int MC_is_active()
51 {
52   return simgrid::mc::model_checking_mode == simgrid::mc::ModelCheckingMode::APP_SIDE ||
53          simgrid::mc::model_checking_mode == simgrid::mc::ModelCheckingMode::CHECKER_SIDE;
54 }
55
56 void MC_automaton_new_propositional_symbol_pointer(const char *name, int* value)
57 {
58 #if SIMGRID_HAVE_MC
59   xbt_assert(simgrid::mc::model_checking_mode != simgrid::mc::ModelCheckingMode::CHECKER_SIDE,
60              "This should be called from the client side");
61   simgrid::mc::AppSide::get()->declare_symbol(name, value);
62 #endif
63 }
64
65 void MC_ignore(void* addr, size_t size)
66 {
67 #if SIMGRID_HAVE_MC
68   xbt_assert(simgrid::mc::model_checking_mode != simgrid::mc::ModelCheckingMode::CHECKER_SIDE,
69              "This should be called from the client side");
70   simgrid::mc::AppSide::get()->ignore_memory(addr, size);
71 #endif
72 }
73
74 void MC_ignore_heap(void *address, size_t size)
75 {
76 #if SIMGRID_HAVE_MC
77   xbt_assert(simgrid::mc::model_checking_mode != simgrid::mc::ModelCheckingMode::CHECKER_SIDE,
78              "This should be called from the client side");
79   simgrid::mc::AppSide::get()->ignore_heap(address, size);
80 #endif
81 }
82
83 void MC_unignore_heap(void* address, size_t size)
84 {
85 #if SIMGRID_HAVE_MC
86   xbt_assert(simgrid::mc::model_checking_mode != simgrid::mc::ModelCheckingMode::CHECKER_SIDE,
87              "This should be called from the client side");
88   simgrid::mc::AppSide::get()->unignore_heap(address, size);
89 #endif
90 }