Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix null pointer dereference.
[simgrid.git] / src / mc / VisitedState.cpp
1 /* Copyright (c) 2011-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 <unistd.h>
7 #include <sys/wait.h>
8
9 #include <memory>
10
11 #include <boost/range/algorithm.hpp>
12
13 #include "xbt/log.h"
14 #include "xbt/sysdep.h"
15
16 #include "src/mc/VisitedState.hpp"
17 #include "src/mc/mc_comm_pattern.hpp"
18 #include "src/mc/mc_private.hpp"
19 #include "src/mc/mc_smx.hpp"
20 #include "src/mc/remote/RemoteClient.hpp"
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_VisitedState, mc, "Logging specific to state equality detection mechanisms");
23
24 namespace simgrid {
25 namespace mc {
26
27 static int snapshot_compare(simgrid::mc::VisitedState* state1, simgrid::mc::VisitedState* state2)
28 {
29   simgrid::mc::Snapshot* s1 = state1->system_state.get();
30   simgrid::mc::Snapshot* s2 = state2->system_state.get();
31   int num1 = state1->num;
32   int num2 = state2->num;
33   return snapshot_compare(num1, s1, num2, s2);
34 }
35
36 /** @brief Save the current state */
37 VisitedState::VisitedState(unsigned long state_number) : num(state_number)
38 {
39   simgrid::mc::RemoteClient* process = &(mc_model_checker->process());
40   this->heap_bytes_used = mmalloc_get_bytes_used_remote(
41     process->get_heap()->heaplimit,
42     process->get_malloc_info());
43
44   this->actors_count = mc_model_checker->process().actors().size();
45
46   this->system_state = simgrid::mc::take_snapshot(state_number);
47   this->original_num = -1;
48 }
49
50 void VisitedStates::prune()
51 {
52   while (states_.size() > (std::size_t)_sg_mc_max_visited_states) {
53     XBT_DEBUG("Try to remove visited state (maximum number of stored states reached)");
54     auto min_element = boost::range::min_element(states_,
55       [](std::unique_ptr<simgrid::mc::VisitedState>& a, std::unique_ptr<simgrid::mc::VisitedState>& b) {
56         return a->num < b->num;
57       });
58     xbt_assert(min_element != states_.end());
59     // and drop it:
60     states_.erase(min_element);
61     XBT_DEBUG("Remove visited state (maximum number of stored states reached)");
62   }
63 }
64
65 /** @brief Checks whether a given state has already been visited by the algorithm. */
66 std::unique_ptr<simgrid::mc::VisitedState> VisitedStates::addVisitedState(
67   unsigned long state_number, simgrid::mc::State* graph_state, bool compare_snpashots)
68 {
69   std::unique_ptr<simgrid::mc::VisitedState> new_state =
70     std::unique_ptr<simgrid::mc::VisitedState>(new VisitedState(state_number));
71   graph_state->system_state = new_state->system_state;
72   XBT_DEBUG("Snapshot %p of visited state %d (exploration stack state %d)",
73     new_state->system_state.get(), new_state->num, graph_state->num);
74
75   auto range =
76       boost::range::equal_range(states_, new_state.get(), simgrid::mc::DerefAndCompareByActorsCountAndUsedHeap());
77
78   if (compare_snpashots)
79     for (auto i = range.first; i != range.second; ++i) {
80       auto& visited_state = *i;
81       if (snapshot_compare(visited_state.get(), new_state.get()) == 0) {
82         // The state has been visited:
83
84         std::unique_ptr<simgrid::mc::VisitedState> old_state =
85           std::move(visited_state);
86
87         if (old_state->original_num == -1) // I'm the copy of an original process
88           new_state->original_num = old_state->num;
89         else // I'm the copy of a copy
90           new_state->original_num = old_state->original_num;
91
92         if (dot_output == nullptr)
93           XBT_DEBUG("State %d already visited ! (equal to state %d)",
94                     new_state->num, old_state->num);
95         else
96           XBT_DEBUG("State %d already visited ! (equal to state %d (state %d in dot_output))",
97                     new_state->num, old_state->num, new_state->original_num);
98
99         /* Replace the old state with the new one (with a bigger num)
100            (when the max number of visited states is reached,  the oldest
101            one is removed according to its number (= with the min number) */
102         XBT_DEBUG("Replace visited state %d with the new visited state %d",
103           old_state->num, new_state->num);
104
105         visited_state = std::move(new_state);
106         return old_state;
107       }
108     }
109
110   XBT_DEBUG("Insert new visited state %d (total : %lu)", new_state->num, (unsigned long) states_.size());
111   states_.insert(range.first, std::move(new_state));
112   this->prune();
113   return nullptr;
114 }
115
116 }
117 }