Construct the graph of methods based on the order that methods are called, rather...
[c11tester.git] / history.cc
1 #include <inttypes.h>
2 #include "history.h"
3 #include "action.h"
4 #include "funcnode.h"
5 #include "common.h"
6
7 #include "model.h"
8 #include "execution.h"
9 #include "newfuzzer.h"
10
11 /** @brief Constructor */
12 ModelHistory::ModelHistory() :
13         func_counter(1),        /* function id starts with 1 */
14         func_map(),
15         func_map_rev(),
16         func_nodes(),
17         write_history(),                // snapshot data structure
18         loc_func_nodes_map(),           // shapshot data structure
19         thrd_last_entered_func()        // snapshot data structure
20 {}
21
22 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
23 {
24         //model_print("thread %d entering func %d\n", tid, func_id);
25         uint id = id_to_int(tid);
26         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
27         SnapVector< SnapList<action_list_t *> *> *
28                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
29
30         if ( thrd_func_list->size() <= id ) {
31                 uint oldsize = thrd_func_list->size();
32                 thrd_func_list->resize( id + 1 );
33                 thrd_func_act_lists->resize( id + 1 );
34
35                 for (uint i = oldsize; i < id + 1; i++) {
36                         new (&(*thrd_func_list)[i]) func_id_list_t();
37                         // push 0 as a dummy function id to a void seg fault
38                         (*thrd_func_list)[i].push_back(0);
39
40                         (*thrd_func_act_lists)[i] = new SnapList<action_list_t *>();
41                 }
42         }
43
44         while ( thrd_last_entered_func.size() <= id ) {
45                 thrd_last_entered_func.push_back(0);    // 0 is a dummy function id
46         }
47
48         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
49         func_act_lists->push_back( new action_list_t() );
50
51         uint32_t last_entered_func_id = thrd_last_entered_func[id];
52         thrd_last_entered_func[id] = func_id;
53         (*thrd_func_list)[id].push_back(func_id);
54
55         if ( func_nodes.size() <= func_id )
56                 resize_func_nodes( func_id + 1 );
57
58         FuncNode * func_node = func_nodes[func_id];
59         func_node->init_predicate_tree_position(tid);
60         func_node->init_inst_act_map(tid);
61
62         /* Add edges between FuncNodes */
63         if (last_entered_func_id != 0) {
64                 FuncNode * last_func_node = func_nodes[last_entered_func_id];
65                 add_edges_between(last_func_node, func_node);
66         }
67 }
68
69 /* @param func_id a non-zero value */
70 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
71 {
72         uint32_t id = id_to_int(tid);
73         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
74         SnapVector< SnapList<action_list_t *> *> *
75                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
76
77         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
78         uint32_t last_func_id = (*thrd_func_list)[id].back();
79
80         if (last_func_id == func_id) {
81                 FuncNode * func_node = func_nodes[func_id];
82                 func_node->set_predicate_tree_position(tid, NULL);
83                 func_node->reset_inst_act_map(tid);
84
85                 action_list_t * curr_act_list = func_act_lists->back();
86
87                 /* defer the processing of curr_act_list until the function has exits a few times 
88                  * (currently twice) so that more information can be gathered to infer nullity predicates.
89                  */
90                 func_node->incr_exit_count();
91                 if (func_node->get_exit_count() >= 2) {
92                         SnapList<action_list_t *> * action_list_buffer = func_node->get_action_list_buffer();
93                         while (action_list_buffer->size() > 0) {
94                                 action_list_t * act_list = action_list_buffer->back();
95                                 action_list_buffer->pop_back();
96                                 func_node->update_tree(act_list);
97                         }
98
99                         func_node->update_tree(curr_act_list);
100                 } else
101                         func_node->get_action_list_buffer()->push_front(curr_act_list);
102
103                 (*thrd_func_list)[id].pop_back();
104                 func_act_lists->pop_back();
105         } else {
106                 model_print("trying to exit with a wrong function id\n");
107                 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
108         }
109         //model_print("thread %d exiting func %d\n", tid, func_id);
110 }
111
112 void ModelHistory::resize_func_nodes(uint32_t new_size)
113 {
114         uint32_t old_size = func_nodes.size();
115
116         if ( old_size < new_size )
117                 func_nodes.resize(new_size);
118
119         for (uint32_t id = old_size; id < new_size; id++) {
120                 const char * func_name = func_map_rev[id];
121                 FuncNode * func_node = new FuncNode(this);
122                 func_node->set_func_id(id);
123                 func_node->set_func_name(func_name);
124                 func_nodes[id] = func_node;
125         }
126 }
127
128 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
129 {
130         /* return if thread i has not entered any function or has exited
131            from all functions */
132         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
133         SnapVector< SnapList<action_list_t *> *> *
134                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
135
136         uint32_t id = id_to_int(tid);
137         if ( thrd_func_list->size() <= id )
138                 return;
139
140         /* get the function id that thread i is currently in */
141         uint32_t func_id = (*thrd_func_list)[id].back();
142         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
143
144         if (act->is_write()) {
145                 void * location = act->get_location();
146                 uint64_t value = act->get_write_value();
147                 add_to_write_history(location, value);
148
149                 /* update FuncNodes */
150                 SnapList<FuncNode *> * func_nodes = loc_func_nodes_map.get(location);
151                 sllnode<FuncNode *> * it = NULL;
152                 if (func_nodes != NULL)
153                         it = func_nodes->begin();
154
155                 for (; it != NULL; it = it->getNext()) {
156                         FuncNode * func_node = it->getVal();
157                         func_node->add_to_val_loc_map(value, location);
158                 }
159         }
160
161         /* the following does not care about actions without a position */
162         if (func_id == 0 || act->get_position() == NULL)
163                 return;
164
165         bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
166
167         action_list_t * curr_act_list = func_act_lists->back();
168         ASSERT(curr_act_list != NULL);
169
170         ModelAction * last_act = NULL;
171         if (curr_act_list->size() != 0)
172                 last_act = curr_act_list->back();
173
174         /* skip actions that are paused by fuzzer (sequence number is 0), 
175          * that are second part of a read modify write or actions with the same sequence number */
176         modelclock_t curr_seq_number = act->get_seq_number();
177         if (curr_seq_number == 0 || second_part_of_rmw ||
178                 (last_act != NULL && last_act->get_seq_number() == curr_seq_number) )
179                 return;
180
181         if ( func_nodes.size() <= func_id )
182                 resize_func_nodes( func_id + 1 );
183
184         FuncNode * func_node = func_nodes[func_id];
185
186         /* add to curr_inst_list */
187         curr_act_list->push_back(act);
188         func_node->add_inst(act);
189
190         if (act->is_read()) {
191                 func_node->update_inst_act_map(tid, act);
192
193                 // Update predicate tree position
194                 Fuzzer * fuzzer = model->get_execution()->getFuzzer();
195                 Predicate * selected_branch = fuzzer->get_selected_child_branch(tid);
196                 func_node->set_predicate_tree_position(tid, selected_branch);
197         }
198 }
199
200 /* return the FuncNode given its func_id  */
201 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
202 {
203         if (func_nodes.size() <= func_id)       // this node has not been added to func_nodes
204                 return NULL;
205
206         return func_nodes[func_id];
207 }
208
209 void ModelHistory::add_to_write_history(void * location, uint64_t write_val)
210 {
211         value_set_t * write_set = write_history.get(location);
212
213         if (write_set == NULL) {
214                 write_set = new value_set_t();
215                 write_history.put(location, write_set);
216         }
217
218         write_set->add(write_val);
219 }
220
221 void ModelHistory::add_to_loc_func_nodes_map(void * location, FuncNode * node)
222 {
223         SnapList<FuncNode *> * func_node_list = loc_func_nodes_map.get(location);
224         if (func_node_list == NULL) {
225                 func_node_list = new SnapList<FuncNode *>();
226                 loc_func_nodes_map.put(location, func_node_list);
227         }
228
229         func_node_list->push_back(node);
230 }
231
232 /* Reallocate some snapshotted memories when new executions start */
233 void ModelHistory::set_new_exec_flag()
234 {
235         for (uint i = 1; i < func_nodes.size(); i++) {
236                 FuncNode * func_node = func_nodes[i];
237                 func_node->set_new_exec_flag();
238         }
239 }
240
241 /* Add edges between FuncNodes */
242 void ModelHistory::add_edges_between(FuncNode * prev_node, FuncNode * next_node)
243 {
244         prev_node->add_out_edge(next_node);
245 }
246
247 void ModelHistory::dump_func_node_graph()
248 {
249         model_print("digraph func_node_graph {\n");
250         for (uint i = 1; i < func_nodes.size(); i++) {
251                 FuncNode * node = func_nodes[i];
252                 ModelList<FuncNode *> * out_edges = node->get_out_edges();
253
254                 model_print("\"%p\" [label=\"%s\"]\n", node, node->get_func_name());
255                 mllnode<FuncNode *> * it;
256                 for (it = out_edges->begin(); it != NULL; it = it->getNext()) {
257                         FuncNode * other = it->getVal();
258                         model_print("\"%p\" -> \"%p\"\n", node, other);
259                 }
260         }
261         model_print("}\n");
262 }
263
264 void ModelHistory::print_func_node()
265 {
266         /* function id starts with 1 */
267         for (uint32_t i = 1; i < func_nodes.size(); i++) {
268                 FuncNode * func_node = func_nodes[i];
269
270                 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
271                 model_print("function %s has entry actions\n", func_node->get_func_name());
272
273                 mllnode<FuncInst*>* it;
274                 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
275                         FuncInst *inst = it->getVal();
276                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
277                 }
278         }
279 }