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