Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix null pointer dereference.
[simgrid.git] / src / mc / mc_state.cpp
1 /* Copyright (c) 2008-2019. 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/mc_comm_pattern.hpp"
7 #include "src/mc/mc_request.hpp"
8 #include "src/mc/mc_smx.hpp"
9 #include "src/mc/mc_state.hpp"
10
11 #include <boost/range/algorithm.hpp>
12
13 using simgrid::mc::remote;
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_state, mc, "Logging specific to MC (state)");
16
17 namespace simgrid {
18 namespace mc {
19
20 State::State(unsigned long state_number) : num(state_number)
21 {
22   this->internal_comm.clear();
23   std::memset(&this->internal_req, 0, sizeof(this->internal_req));
24   std::memset(&this->executed_req, 0, sizeof(this->executed_req));
25
26   actorStates.resize(MC_smx_get_maxpid());
27   /* Stateful model checking */
28   if ((_sg_mc_checkpoint > 0 && (state_number % _sg_mc_checkpoint == 0)) || _sg_mc_termination) {
29     system_state = simgrid::mc::take_snapshot(num);
30     if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
31       MC_state_copy_incomplete_communications_pattern(this);
32       MC_state_copy_index_communications_pattern(this);
33     }
34   }
35 }
36
37 std::size_t State::interleaveSize() const
38 {
39   return boost::range::count_if(this->actorStates, [](simgrid::mc::ProcessState const& p) { return p.isTodo(); });
40 }
41
42 Transition State::getTransition() const
43 {
44   return this->transition;
45 }
46
47 }
48 }
49
50 /* Search an enabled transition for the given process.
51  *
52  * This can be seen as an iterator returning the next transition of the process.
53  *
54  * We only consider the processes that are both
55  *  - marked "to be interleaved" in their ProcessState (controlled by the checker algorithm).
56  *  - which simcall can currently be executed (like a comm where the other partner is already known)
57  * Once we returned the last enabled transition of a process, it is marked done.
58  *
59  * Things can get muddled with the WAITANY and TESTANY simcalls, that are rewritten on the fly to a bunch of WAIT
60  * (resp TEST) transitions using the transition.argument field to remember what was the last returned sub-transition.
61  */
62 static inline smx_simcall_t MC_state_get_request_for_process(simgrid::mc::State* state, smx_actor_t actor)
63 {
64   /* reset the outgoing transition */
65   simgrid::mc::ProcessState* procstate = &state->actorStates[actor->get_pid()];
66   state->transition.pid                = -1;
67   state->transition.argument           = -1;
68   state->executed_req.call             = SIMCALL_NONE;
69
70   if (not simgrid::mc::actor_is_enabled(actor))
71     return nullptr; // Not executable in the application
72
73   smx_simcall_t req = nullptr;
74   switch (actor->simcall.call) {
75     case SIMCALL_COMM_WAITANY:
76       state->transition.argument = -1;
77       while (procstate->times_considered < simcall_comm_waitany__get__count(&actor->simcall)) {
78         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall, procstate->times_considered++)) {
79           state->transition.argument = procstate->times_considered - 1;
80           break;
81         }
82       }
83
84       if (procstate->times_considered >= simcall_comm_waitany__get__count(&actor->simcall))
85         procstate->setDone();
86       if (state->transition.argument != -1)
87         req = &actor->simcall;
88       break;
89
90     case SIMCALL_COMM_TESTANY: {
91       unsigned start_count       = procstate->times_considered;
92       state->transition.argument = -1;
93       while (procstate->times_considered < simcall_comm_testany__get__count(&actor->simcall))
94         if (simgrid::mc::request_is_enabled_by_idx(&actor->simcall, procstate->times_considered++)) {
95           state->transition.argument = procstate->times_considered - 1;
96           break;
97         }
98
99       if (procstate->times_considered >= simcall_comm_testany__get__count(&actor->simcall))
100         procstate->setDone();
101
102       if (state->transition.argument != -1 || start_count == 0)
103         req = &actor->simcall;
104
105       break;
106     }
107
108     case SIMCALL_COMM_WAIT: {
109       simgrid::mc::RemotePtr<simgrid::kernel::activity::CommImpl> remote_act =
110           remote(static_cast<simgrid::kernel::activity::CommImpl*>(simcall_comm_wait__getraw__comm(&actor->simcall)));
111       simgrid::mc::Remote<simgrid::kernel::activity::CommImpl> temp_act;
112       mc_model_checker->process().read(temp_act, remote_act);
113       simgrid::kernel::activity::CommImpl* act = temp_act.getBuffer();
114       if (act->src_actor_.get() && act->dst_actor_.get())
115         state->transition.argument = 0;
116       else if (act->src_actor_.get() == nullptr && act->type == simgrid::kernel::activity::CommImpl::Type::READY &&
117                act->detached == 1)
118         state->transition.argument = 0;
119       else
120         state->transition.argument = -1;
121       procstate->setDone();
122       req = &actor->simcall;
123       break;
124     }
125
126     case SIMCALL_MC_RANDOM: {
127       int min_value              = simcall_mc_random__get__min(&actor->simcall);
128       state->transition.argument = procstate->times_considered + min_value;
129       procstate->times_considered++;
130       if (state->transition.argument == simcall_mc_random__get__max(&actor->simcall))
131         procstate->setDone();
132       req = &actor->simcall;
133       break;
134     }
135
136     default:
137       procstate->setDone();
138       state->transition.argument = 0;
139       req                        = &actor->simcall;
140       break;
141   }
142   if (not req)
143     return nullptr;
144
145   state->transition.pid = actor->get_pid();
146   state->executed_req = *req;
147   // Fetch the data of the request and translate it:
148   state->internal_req = *req;
149
150   /* The waitany and testany request are transformed into a wait or test request over the corresponding communication
151    * action so it can be treated later by the dependence function. */
152   switch (req->call) {
153   case SIMCALL_COMM_WAITANY: {
154     state->internal_req.call = SIMCALL_COMM_WAIT;
155     simgrid::kernel::activity::CommImpl* remote_comm;
156     remote_comm =
157         mc_model_checker->process().read(remote(simcall_comm_waitany__get__comms(req) + state->transition.argument));
158     mc_model_checker->process().read(state->internal_comm, remote(remote_comm));
159     simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
160     simcall_comm_wait__set__timeout(&state->internal_req, 0);
161     break;
162   }
163
164   case SIMCALL_COMM_TESTANY:
165     state->internal_req.call = SIMCALL_COMM_TEST;
166
167     if (state->transition.argument > 0) {
168       simgrid::kernel::activity::CommImpl* remote_comm =
169           mc_model_checker->process().read(remote(simcall_comm_testany__get__comms(req) + state->transition.argument));
170       mc_model_checker->process().read(state->internal_comm, remote(remote_comm));
171     }
172
173     simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
174     simcall_comm_test__set__result(&state->internal_req, state->transition.argument);
175     break;
176
177   case SIMCALL_COMM_WAIT:
178     mc_model_checker->process().read_bytes(&state->internal_comm, sizeof(state->internal_comm),
179                                            remote(simcall_comm_wait__getraw__comm(req)));
180     simcall_comm_wait__set__comm(&state->executed_req, state->internal_comm.getBuffer());
181     simcall_comm_wait__set__comm(&state->internal_req, state->internal_comm.getBuffer());
182     break;
183
184   case SIMCALL_COMM_TEST:
185     mc_model_checker->process().read_bytes(&state->internal_comm, sizeof(state->internal_comm),
186                                            remote(simcall_comm_test__getraw__comm(req)));
187     simcall_comm_test__set__comm(&state->executed_req, state->internal_comm.getBuffer());
188     simcall_comm_test__set__comm(&state->internal_req, state->internal_comm.getBuffer());
189     break;
190
191   default:
192     /* No translation needed */
193     break;
194   }
195
196   return req;
197 }
198
199 smx_simcall_t MC_state_get_request(simgrid::mc::State* state)
200 {
201   for (auto& actor : mc_model_checker->process().actors()) {
202     /* Only consider the actors that were marked as interleaving by the checker algorithm */
203     if (not state->actorStates[actor.copy.getBuffer()->get_pid()].isTodo())
204       continue;
205
206     smx_simcall_t res = MC_state_get_request_for_process(state, actor.copy.getBuffer());
207     if (res)
208       return res;
209   }
210   return nullptr;
211 }