Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into 'task-token'
[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 using namespace simgrid::mc;
17
18 /* Implementation of the user API from the App to the Checker (see modelchecker.h)  */
19
20 int MC_random(int min, int max)
21 {
22   xbt_assert(get_model_checking_mode() != ModelCheckingMode::CHECKER_SIDE,
23              "This should be called from the client side");
24
25   if (not MC_is_active() && not MC_record_replay_is_active()) { // no need to do a simcall in this case
26     static simgrid::xbt::random::XbtRandom prng;
27     return prng.uniform_int(min, max);
28   }
29   simgrid::kernel::actor::RandomSimcall observer{simgrid::kernel::actor::ActorImpl::self(), min, max};
30   return simgrid::kernel::actor::simcall_answered([&observer] { return observer.get_value(); }, &observer);
31 }
32
33 void MC_assert(int prop)
34 {
35   // Cannot used xbt_assert here, or it would be an infinite recursion.
36   xbt_assert(get_model_checking_mode() != ModelCheckingMode::CHECKER_SIDE,
37              "This should be called from the client side");
38 #if SIMGRID_HAVE_MC
39   if (not prop) {
40     if (MC_is_active())
41       AppSide::get()->report_assertion_failure();
42     if (MC_record_replay_is_active())
43       xbt_die("MC assertion failed");
44   }
45 #else
46   if (not prop)
47     xbt_die("Safety property violation detected without the model-checker");
48 #endif
49 }
50
51 int MC_is_active()
52 {
53   return get_model_checking_mode() == ModelCheckingMode::APP_SIDE ||
54          get_model_checking_mode() == ModelCheckingMode::CHECKER_SIDE;
55 }
56
57 void MC_automaton_new_propositional_symbol_pointer(const char *name, int* value)
58 {
59 #if SIMGRID_HAVE_STATEFUL_MC
60   xbt_assert(get_model_checking_mode() != ModelCheckingMode::CHECKER_SIDE,
61              "This should be called from the client side");
62   if (MC_is_active())
63     AppSide::get()->declare_symbol(name, value);
64 #endif
65 }
66
67 void MC_ignore(void* addr, size_t size)
68 {
69 #if SIMGRID_HAVE_STATEFUL_MC
70   xbt_assert(get_model_checking_mode() != ModelCheckingMode::CHECKER_SIDE,
71              "This should be called from the client side");
72   if (MC_is_active())
73     AppSide::get()->ignore_memory(addr, size);
74 #endif
75 }
76
77 void MC_unignore(void* addr, size_t size)
78 {
79 #if SIMGRID_HAVE_STATEFUL_MC
80   xbt_assert(get_model_checking_mode() != ModelCheckingMode::CHECKER_SIDE,
81              "This should be called from the client side");
82   if (MC_is_active())
83     AppSide::get()->unignore_memory(addr, size);
84 #endif
85 }
86
87 void MC_ignore_heap(void *address, size_t size)
88 {
89 #if SIMGRID_HAVE_STATEFUL_MC
90   xbt_assert(get_model_checking_mode() != ModelCheckingMode::CHECKER_SIDE,
91              "This should be called from the client side");
92   if (MC_is_active())
93     AppSide::get()->ignore_heap(address, size);
94 #endif
95 }
96
97 void MC_unignore_heap(void* address, size_t size)
98 {
99 #if SIMGRID_HAVE_STATEFUL_MC
100   xbt_assert(get_model_checking_mode() != ModelCheckingMode::CHECKER_SIDE,
101              "This should be called from the client side");
102   if (MC_is_active())
103     AppSide::get()->unignore_heap(address, size);
104 #endif
105 }