Fix a memory bug
[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 #include "concretepredicate.h"
8
9 #include "model.h"
10 #include "execution.h"
11 #include "newfuzzer.h"
12
13 /** @brief Constructor */
14 ModelHistory::ModelHistory() :
15         func_counter(1),        /* function id starts with 1 */
16         func_map(),
17         func_map_rev(),
18         func_nodes()
19 {
20         /* The following are snapshot data structures */
21         write_history = new HashTable<void *, value_set_t *, uintptr_t, 4>();
22         loc_func_nodes_map = new HashTable<void *, SnapList<FuncNode *> *, uintptr_t, 0>();
23         loc_wr_func_nodes_map = new HashTable<void *, SnapList<FuncNode *> *, uintptr_t, 0>();
24         thrd_last_entered_func = new SnapVector<uint32_t>();
25         loc_waiting_writes_map = new HashTable<void *, SnapVector<ConcretePredicate *> *, uintptr_t, 0>();
26         thrd_waiting_write = new SnapVector<ConcretePredicate *>();
27         func_inst_act_maps = new HashTable<uint32_t, SnapVector<inst_act_map_t *> *, int, 0>();
28 }
29
30 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
31 {
32         //model_print("thread %d entering func %d\n", tid, func_id);
33         uint id = id_to_int(tid);
34         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
35         SnapVector< SnapList<action_list_t *> *> *
36                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
37
38         if ( thrd_func_list->size() <= id ) {
39                 uint oldsize = thrd_func_list->size();
40                 thrd_func_list->resize( id + 1 );
41                 thrd_func_act_lists->resize( id + 1 );
42
43                 for (uint i = oldsize; i < id + 1; i++) {
44                         new (&(*thrd_func_list)[i]) func_id_list_t();
45                         // push 0 as a dummy function id to a void seg fault
46                         (*thrd_func_list)[i].push_back(0);
47
48                         (*thrd_func_act_lists)[i] = new SnapList<action_list_t *>();
49                 }
50         }
51
52         while ( thrd_last_entered_func->size() <= id ) {
53                 thrd_last_entered_func->push_back(0);   // 0 is a dummy function id
54         }
55
56         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
57         func_act_lists->push_back( new action_list_t() );
58
59         uint32_t last_entered_func_id = (*thrd_last_entered_func)[id];
60         (*thrd_last_entered_func)[id] = func_id;
61         (*thrd_func_list)[id].push_back(func_id);
62
63         if ( func_nodes.size() <= func_id )
64                 resize_func_nodes( func_id + 1 );
65
66         FuncNode * func_node = func_nodes[func_id];
67         func_node->init_predicate_tree_position(tid);
68         func_node->init_inst_act_map(tid);
69
70         /* Add edges between FuncNodes */
71         if (last_entered_func_id != 0) {
72                 FuncNode * last_func_node = func_nodes[last_entered_func_id];
73                 last_func_node->add_out_edge(func_node);
74         }
75 }
76
77 /* @param func_id a non-zero value */
78 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
79 {
80         uint32_t id = id_to_int(tid);
81         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
82         SnapVector< SnapList<action_list_t *> *> *
83                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
84
85         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
86         uint32_t last_func_id = (*thrd_func_list)[id].back();
87
88         if (last_func_id == func_id) {
89                 FuncNode * func_node = func_nodes[func_id];
90                 func_node->set_predicate_tree_position(tid, NULL);
91                 func_node->reset_inst_act_map(tid);
92
93                 action_list_t * curr_act_list = func_act_lists->back();
94
95                 /* defer the processing of curr_act_list until the function has exits a few times 
96                  * (currently twice) so that more information can be gathered to infer nullity predicates.
97                  */
98                 func_node->incr_exit_count();
99                 if (func_node->get_exit_count() >= 2) {
100                         SnapList<action_list_t *> * action_list_buffer = func_node->get_action_list_buffer();
101                         while (action_list_buffer->size() > 0) {
102                                 action_list_t * act_list = action_list_buffer->back();
103                                 action_list_buffer->pop_back();
104                                 func_node->update_tree(act_list);
105                         }
106
107                         func_node->update_tree(curr_act_list);
108                 } else
109                         func_node->get_action_list_buffer()->push_front(curr_act_list);
110
111                 (*thrd_func_list)[id].pop_back();
112                 func_act_lists->pop_back();
113         } else {
114                 model_print("trying to exit with a wrong function id\n");
115                 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
116         }
117         //model_print("thread %d exiting func %d\n", tid, func_id);
118 }
119
120 void ModelHistory::resize_func_nodes(uint32_t new_size)
121 {
122         uint32_t old_size = func_nodes.size();
123
124         if ( old_size < new_size )
125                 func_nodes.resize(new_size);
126
127         for (uint32_t id = old_size; id < new_size; id++) {
128                 const char * func_name = func_map_rev[id];
129                 FuncNode * func_node = new FuncNode(this);
130                 func_node->set_func_id(id);
131                 func_node->set_func_name(func_name);
132                 func_nodes[id] = func_node;
133         }
134 }
135
136 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
137 {
138         /* return if thread i has not entered any function or has exited
139            from all functions */
140         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
141         SnapVector< SnapList<action_list_t *> *> *
142                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
143
144         uint32_t id = id_to_int(tid);
145         if ( thrd_func_list->size() <= id )
146                 return;
147
148         /* get the function id that thread i is currently in */
149         uint32_t func_id = (*thrd_func_list)[id].back();
150         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
151
152         if (act->is_write()) {
153                 void * location = act->get_location();
154                 uint64_t value = act->get_write_value();
155                 update_write_history(location, value);
156
157                 /* Update FuncNodes that may read from this location */
158                 SnapList<FuncNode *> * func_nodes = loc_func_nodes_map->get(location);
159                 if (func_nodes != NULL) {
160                         sllnode<FuncNode *> * it = func_nodes->begin();
161                         for (; it != NULL; it = it->getNext()) {
162                                 FuncNode * func_node = it->getVal();
163                                 func_node->add_to_val_loc_map(value, location);
164                         }
165                 }
166
167                 check_waiting_write(act);
168         }
169
170         /* the following does not care about actions without a position */
171         if (func_id == 0 || act->get_position() == NULL)
172                 return;
173
174         bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
175
176         action_list_t * curr_act_list = func_act_lists->back();
177         ASSERT(curr_act_list != NULL);
178
179         modelclock_t curr_seq_number = act->get_seq_number();
180         /* Skip actions that are second part of a read modify write or actions with the same sequence number */
181         if (curr_act_list->size() != 0) {
182                 ModelAction * last_act = curr_act_list->back();
183                 if (second_part_of_rmw || last_act->get_seq_number() == curr_seq_number)
184                         return;
185         }
186
187         /* skip actions that are paused by fuzzer (sequence number is 0) */
188         if (curr_seq_number == 0)
189                 return;
190
191         FuncNode * func_node = func_nodes[func_id];
192
193         /* add to curr_inst_list */
194         curr_act_list->push_back(act);
195         func_node->add_inst(act);
196
197         if (act->is_read()) {
198                 func_node->update_inst_act_map(tid, act);
199
200                 // Update predicate tree position
201                 Fuzzer * fuzzer = model->get_execution()->getFuzzer();
202                 Predicate * selected_branch = fuzzer->get_selected_child_branch(tid);
203                 func_node->set_predicate_tree_position(tid, selected_branch);
204         }
205 }
206
207 /* Return the FuncNode given its func_id  */
208 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
209 {
210         if (func_id == 0)
211                 return NULL;
212
213         // This node has not been added to func_nodes
214         if (func_nodes.size() <= func_id)
215                 return NULL;
216
217         return func_nodes[func_id];
218 }
219
220 /* Return the current FuncNode when given a thread id */
221 FuncNode * ModelHistory::get_curr_func_node(thread_id_t tid)
222 {
223         int thread_id = id_to_int(tid);
224         SnapVector<func_id_list_t> * thrd_func_list =  model->get_execution()->get_thrd_func_list();
225         uint32_t func_id = (*thrd_func_list)[thread_id].back();
226
227         if (func_id != 0) {
228                 return func_nodes[func_id];
229         }
230
231         return NULL;
232 }
233
234 void ModelHistory::update_write_history(void * location, uint64_t write_val)
235 {
236         value_set_t * write_set = write_history->get(location);
237
238         if (write_set == NULL) {
239                 write_set = new value_set_t();
240                 write_history->put(location, write_set);
241         }
242
243         write_set->add(write_val);
244 }
245
246 void ModelHistory::update_loc_func_nodes_map(void * location, FuncNode * node)
247 {
248         SnapList<FuncNode *> * func_node_list = loc_func_nodes_map->get(location);
249         if (func_node_list == NULL) {
250                 func_node_list = new SnapList<FuncNode *>();
251                 loc_func_nodes_map->put(location, func_node_list);
252         }
253
254         func_node_list->push_back(node);
255 }
256
257 void ModelHistory::update_loc_wr_func_nodes_map(void * location, FuncNode * node)
258 {
259         SnapList<FuncNode *> * func_node_list = loc_wr_func_nodes_map->get(location);
260         if (func_node_list == NULL) {
261                 func_node_list = new SnapList<FuncNode *>();
262                 loc_func_nodes_map->put(location, func_node_list);
263         }
264
265         func_node_list->push_back(node);
266 }
267
268 /* When a thread is paused by Fuzzer, keep track of the condition it is waiting for */
269 void ModelHistory::add_waiting_write(ConcretePredicate * concrete)
270 {
271         void * location = concrete->get_location();
272         SnapVector<ConcretePredicate *> * waiting_conditions = loc_waiting_writes_map->get(location);
273         if (waiting_conditions == NULL) {
274                 waiting_conditions = new SnapVector<ConcretePredicate *>();
275                 loc_waiting_writes_map->put(location, waiting_conditions);
276         }
277
278         /* waiting_conditions should not have duplications */
279         waiting_conditions->push_back(concrete);
280
281         int thread_id = id_to_int(concrete->get_tid());
282         if (thrd_waiting_write->size() <= (uint) thread_id) {
283                 thrd_waiting_write->resize(thread_id + 1);
284         }
285
286         (*thrd_waiting_write)[thread_id] = concrete;
287 }
288
289 void ModelHistory::remove_waiting_write(thread_id_t tid)
290 {
291         ConcretePredicate * concrete = (*thrd_waiting_write)[ id_to_int(tid) ];
292         void * location = concrete->get_location();
293         SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
294
295         for (uint i = 0; i < concrete_preds->size(); i++) {
296                 ConcretePredicate * current = (*concrete_preds)[i];
297                 if (concrete == current) {
298                         (*concrete_preds)[i] = concrete_preds->back();
299                         concrete_preds->pop_back();
300                         break;
301                 }
302         }
303
304         int thread_id = id_to_int( concrete->get_tid() );
305         (*thrd_waiting_write)[thread_id] = NULL;
306         delete concrete;
307 }
308
309 /* Check if any other thread is waiting for this write action. If so, "notify" them */
310 void ModelHistory::check_waiting_write(ModelAction * write_act)
311 {
312         void * location = write_act->get_location();
313         uint64_t value = write_act->get_write_value();
314         SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
315         SnapVector<ConcretePredicate *> to_remove = SnapVector<ConcretePredicate *>();
316         if (concrete_preds == NULL)
317                 return;
318
319         uint index = 0;
320         while (index < concrete_preds->size()) {
321                 ConcretePredicate * concrete_pred = (*concrete_preds)[index];
322                 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
323                 bool satisfy_predicate = true;
324                 /* Check if the written value satisfies every predicate expression */
325                 for (uint i = 0; i < concrete_exprs->size(); i++) {
326                         struct concrete_pred_expr concrete = (*concrete_exprs)[i];
327                         bool equality;
328                         switch (concrete.token) {
329                                 case EQUALITY:
330                                         equality = (value == concrete.value);
331                                         break;
332                                 case NULLITY:
333                                         equality = ((void*)value == NULL);
334                                         break;
335                                 default:
336                                         model_print("unknown predicate token");
337                                         break;
338                         }
339
340                         if (equality != concrete.equality) {
341                                 satisfy_predicate = false;
342                                 break;
343                         }
344                 }
345
346                 if (satisfy_predicate) {
347                         to_remove.push_back(concrete_pred);
348                 }
349
350                 index++;
351         }
352
353         for (uint i = 0; i < to_remove.size(); i++) {
354                 ConcretePredicate * concrete_pred = to_remove[i];
355
356                 /* Wake up threads */
357                 thread_id_t tid = concrete_pred->get_tid();
358                 Thread * thread = model->get_thread(tid);
359
360                 model_print("** thread %d is woken up\n", thread->get_id());
361                 model->get_execution()->getFuzzer()->notify_paused_thread(thread);
362         }
363 }
364
365 SnapVector<inst_act_map_t *> * ModelHistory::getThrdInstActMap(uint32_t func_id)
366 {
367         ASSERT(func_id != 0);
368
369         SnapVector<inst_act_map_t *> * maps = func_inst_act_maps->get(func_id);
370         if (maps == NULL) {
371                 maps = new SnapVector<inst_act_map_t *>();
372                 func_inst_act_maps->put(func_id, maps);
373         }
374
375         return maps;
376 }
377
378 /* Reallocate some snapshotted memories when new executions start */
379 void ModelHistory::set_new_exec_flag()
380 {
381         for (uint i = 1; i < func_nodes.size(); i++) {
382                 FuncNode * func_node = func_nodes[i];
383                 func_node->set_new_exec_flag();
384         }
385 }
386
387 void ModelHistory::dump_func_node_graph()
388 {
389         model_print("digraph func_node_graph {\n");
390         for (uint i = 1; i < func_nodes.size(); i++) {
391                 FuncNode * node = func_nodes[i];
392                 ModelList<FuncNode *> * out_edges = node->get_out_edges();
393
394                 model_print("\"%p\" [label=\"%s\"]\n", node, node->get_func_name());
395                 mllnode<FuncNode *> * it;
396                 for (it = out_edges->begin(); it != NULL; it = it->getNext()) {
397                         FuncNode * other = it->getVal();
398                         model_print("\"%p\" -> \"%p\"\n", node, other);
399                 }
400         }
401         model_print("}\n");
402 }
403
404 void ModelHistory::print_func_node()
405 {
406         /* function id starts with 1 */
407         for (uint32_t i = 1; i < func_nodes.size(); i++) {
408                 FuncNode * func_node = func_nodes[i];
409
410                 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
411                 model_print("function %s has entry actions\n", func_node->get_func_name());
412
413                 mllnode<FuncInst*>* it;
414                 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
415                         FuncInst *inst = it->getVal();
416                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
417                 }
418         }
419 }