Fix a bug
[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                 update_write_history(location, value);
148
149                 /* Update FuncNodes that may read from this location */
150                 SnapList<FuncNode *> * func_nodes = loc_func_nodes_map.get(location);
151                 if (func_nodes != NULL) {
152                         sllnode<FuncNode *> * it = func_nodes->begin();
153                         for (; it != NULL; it = it->getNext()) {
154                                 FuncNode * func_node = it->getVal();
155                                 func_node->add_to_val_loc_map(value, location);
156                         }
157                 }
158         }
159
160         /* the following does not care about actions without a position */
161         if (func_id == 0 || act->get_position() == NULL)
162                 return;
163
164         bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
165
166         action_list_t * curr_act_list = func_act_lists->back();
167         ASSERT(curr_act_list != NULL);
168
169         modelclock_t curr_seq_number = act->get_seq_number();
170         /* Skip actions that are second part of a read modify write or actions with the same sequence number */
171         if (curr_act_list->size() != 0) {
172                 ModelAction * last_act = curr_act_list->back();
173                 if (second_part_of_rmw || last_act->get_seq_number() == curr_seq_number)
174                         return;
175         }
176
177         /* skip actions that are paused by fuzzer (sequence number is 0) */
178         if (curr_seq_number == 0)
179                 return;
180
181         FuncNode * func_node = func_nodes[func_id];
182
183         /* add to curr_inst_list */
184         curr_act_list->push_back(act);
185         func_node->add_inst(act);
186
187         if (act->is_read()) {
188                 func_node->update_inst_act_map(tid, act);
189
190                 // Update predicate tree position
191                 Fuzzer * fuzzer = model->get_execution()->getFuzzer();
192                 Predicate * selected_branch = fuzzer->get_selected_child_branch(tid);
193                 func_node->set_predicate_tree_position(tid, selected_branch);
194         }
195 }
196
197 /* return the FuncNode given its func_id  */
198 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
199 {
200         if (func_nodes.size() <= func_id)       // this node has not been added to func_nodes
201                 return NULL;
202
203         return func_nodes[func_id];
204 }
205
206 void ModelHistory::update_write_history(void * location, uint64_t write_val)
207 {
208         value_set_t * write_set = write_history.get(location);
209
210         if (write_set == NULL) {
211                 write_set = new value_set_t();
212                 write_history.put(location, write_set);
213         }
214
215         write_set->add(write_val);
216 }
217
218 void ModelHistory::update_loc_func_nodes_map(void * location, FuncNode * node)
219 {
220         SnapList<FuncNode *> * func_node_list = loc_func_nodes_map.get(location);
221         if (func_node_list == NULL) {
222                 func_node_list = new SnapList<FuncNode *>();
223                 loc_func_nodes_map.put(location, func_node_list);
224         }
225
226         func_node_list->push_back(node);
227 }
228
229 void ModelHistory::update_loc_wr_func_nodes_map(void * location, FuncNode * node)
230 {
231         SnapList<FuncNode *> * func_node_list = loc_wr_func_nodes_map.get(location);
232         if (func_node_list == NULL) {
233                 func_node_list = new SnapList<FuncNode *>();
234                 loc_func_nodes_map.put(location, func_node_list);
235         }
236
237         func_node_list->push_back(node);
238 }
239
240 /* Reallocate some snapshotted memories when new executions start */
241 void ModelHistory::set_new_exec_flag()
242 {
243         for (uint i = 1; i < func_nodes.size(); i++) {
244                 FuncNode * func_node = func_nodes[i];
245                 func_node->set_new_exec_flag();
246         }
247 }
248
249 /* Add edges between FuncNodes */
250 void ModelHistory::add_edges_between(FuncNode * prev_node, FuncNode * next_node)
251 {
252         prev_node->add_out_edge(next_node);
253 }
254
255 void ModelHistory::dump_func_node_graph()
256 {
257         model_print("digraph func_node_graph {\n");
258         for (uint i = 1; i < func_nodes.size(); i++) {
259                 FuncNode * node = func_nodes[i];
260                 ModelList<FuncNode *> * out_edges = node->get_out_edges();
261
262                 model_print("\"%p\" [label=\"%s\"]\n", node, node->get_func_name());
263                 mllnode<FuncNode *> * it;
264                 for (it = out_edges->begin(); it != NULL; it = it->getNext()) {
265                         FuncNode * other = it->getVal();
266                         model_print("\"%p\" -> \"%p\"\n", node, other);
267                 }
268         }
269         model_print("}\n");
270 }
271
272 void ModelHistory::print_func_node()
273 {
274         /* function id starts with 1 */
275         for (uint32_t i = 1; i < func_nodes.size(); i++) {
276                 FuncNode * func_node = func_nodes[i];
277
278                 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
279                 model_print("function %s has entry actions\n", func_node->get_func_name());
280
281                 mllnode<FuncInst*>* it;
282                 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
283                         FuncInst *inst = it->getVal();
284                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
285                 }
286         }
287 }