Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sthread: Add a way to verify accesses to non-reentrant data structures
[simgrid.git] / src / mc / transition / Transition.cpp
1 /* Copyright (c) 2015-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 "src/mc/transition/Transition.hpp"
7 #include "src/kernel/actor/Simcall.hpp"
8 #include "xbt/asserts.h"
9 #include "xbt/string.hpp"
10 #include <simgrid/config.h>
11
12 #if SIMGRID_HAVE_MC
13 #include "src/mc/ModelChecker.hpp"
14 #include "src/mc/transition/TransitionActorJoin.hpp"
15 #include "src/mc/transition/TransitionAny.hpp"
16 #include "src/mc/transition/TransitionComm.hpp"
17 #include "src/mc/transition/TransitionObjectAccess.hpp"
18 #include "src/mc/transition/TransitionRandom.hpp"
19 #include "src/mc/transition/TransitionSynchro.hpp"
20 #endif
21
22 #include <sstream>
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_transition, mc, "Logging specific to MC transitions");
25
26 namespace simgrid::mc {
27 unsigned long Transition::executed_transitions_ = 0;
28 unsigned long Transition::replayed_transitions_ = 0;
29
30 // Do not move this to the header, to ensure that we have a vtable for Transition
31 Transition::~Transition() = default;
32
33 std::string Transition::to_string(bool) const
34 {
35   return "";
36 }
37 std::string Transition::dot_string() const
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   const char* color = colors[(aid_ - 1) % colors.size()];
43
44   return xbt::string_printf("label = \"[(%ld)] %s\", color = %s, fontcolor = %s", aid_, Transition::to_c_str(type_),
45                             color, color);
46 }
47 void Transition::replay() const
48 {
49   replayed_transitions_++;
50
51 #if SIMGRID_HAVE_MC
52   mc_model_checker->handle_simcall(aid_, times_considered_, false);
53   mc_model_checker->wait_for_requests();
54 #endif
55 }
56
57 Transition* deserialize_transition(aid_t issuer, int times_considered, std::stringstream& stream)
58 {
59 #if SIMGRID_HAVE_MC
60   short type;
61   xbt_assert(stream >> type);
62
63   switch (auto simcall = static_cast<Transition::Type>(type)) {
64     case Transition::Type::BARRIER_ASYNC_LOCK:
65     case Transition::Type::BARRIER_WAIT:
66       return new BarrierTransition(issuer, times_considered, simcall, stream);
67
68     case Transition::Type::COMM_ASYNC_RECV:
69       return new CommRecvTransition(issuer, times_considered, stream);
70     case Transition::Type::COMM_ASYNC_SEND:
71       return new CommSendTransition(issuer, times_considered, stream);
72     case Transition::Type::COMM_TEST:
73       return new CommTestTransition(issuer, times_considered, stream);
74     case Transition::Type::COMM_WAIT:
75       return new CommWaitTransition(issuer, times_considered, stream);
76
77     case Transition::Type::TESTANY:
78       return new TestAnyTransition(issuer, times_considered, stream);
79     case Transition::Type::WAITANY:
80       return new WaitAnyTransition(issuer, times_considered, stream);
81
82     case Transition::Type::RANDOM:
83       return new RandomTransition(issuer, times_considered, stream);
84
85     case Transition::Type::MUTEX_TRYLOCK:
86     case Transition::Type::MUTEX_ASYNC_LOCK:
87     case Transition::Type::MUTEX_TEST:
88     case Transition::Type::MUTEX_WAIT:
89     case Transition::Type::MUTEX_UNLOCK:
90       return new MutexTransition(issuer, times_considered, simcall, stream);
91
92     case Transition::Type::SEM_ASYNC_LOCK:
93     case Transition::Type::SEM_UNLOCK:
94     case Transition::Type::SEM_WAIT:
95       return new SemaphoreTransition(issuer, times_considered, simcall, stream);
96
97     case Transition::Type::ACTOR_JOIN:
98       return new ActorJoinTransition(issuer, times_considered, stream);
99
100     case Transition::Type::OBJECT_ACCESS:
101       return new ObjectAccessTransition(issuer, times_considered, stream);
102
103     case Transition::Type::UNKNOWN:
104       return new Transition(Transition::Type::UNKNOWN, issuer, times_considered);
105
106     default:
107       break;
108   }
109   xbt_die("Invalid transition type %d received. Did you implement a new observer in the app without implementing the "
110           "corresponding transition in the checker?",
111           type);
112 #else
113   xbt_die("Deserializing transitions is only interesting in MC mode.");
114 #endif
115 }
116
117 } // namespace simgrid::mc