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