Fix methods
[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 #include "waitobj.h"
9
10 #include "model.h"
11 #include "execution.h"
12 #include "newfuzzer.h"
13
14 /** @brief Constructor */
15 ModelHistory::ModelHistory() :
16         func_counter(1),        /* function id starts with 1 */
17         func_map(),
18         func_map_rev(),
19         func_nodes()
20 {
21         /* The following are snapshot data structures */
22         write_history = new HashTable<void *, value_set_t *, uintptr_t, 4>();
23         loc_rd_func_nodes_map = new HashTable<void *, SnapVector<FuncNode *> *, uintptr_t, 0>();
24         loc_wr_func_nodes_map = new HashTable<void *, SnapVector<FuncNode *> *, uintptr_t, 0>();
25         loc_waiting_writes_map = new HashTable<void *, SnapVector<ConcretePredicate *> *, uintptr_t, 0>();
26         thrd_waiting_write = new SnapVector<ConcretePredicate *>();
27         thrd_wait_obj = new SnapVector<WaitObj *>();
28         func_inst_act_maps = new HashTable<uint32_t, SnapVector<inst_act_map_t *> *, int, 0>(128);
29 }
30
31 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
32 {
33         //model_print("thread %d entering func %d\n", tid, func_id);
34         ModelExecution * execution = model->get_execution();
35         uint id = id_to_int(tid);
36         SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
37         SnapVector< SnapList<action_list_t *> *> *
38                 thrd_func_act_lists = execution->get_thrd_func_act_lists();
39         SnapVector<uint32_t> * thrd_last_entered_func = execution->get_thrd_last_entered_func();
40
41         if ( thrd_func_list->size() <= id ) {
42                 uint oldsize = thrd_func_list->size();
43                 thrd_func_list->resize( id + 1 );
44                 thrd_func_act_lists->resize( id + 1 );
45
46                 for (uint i = oldsize; i < id + 1; i++) {
47                         // push 0 as a dummy function id to a void seg fault
48                         new (&(*thrd_func_list)[i]) func_id_list_t();
49                         (*thrd_func_list)[i].push_back(0);
50
51                         (*thrd_func_act_lists)[i] = new SnapList<action_list_t *>();
52                         thrd_last_entered_func->push_back(0);
53                 }
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         ModelExecution * execution = model->get_execution();
81         uint32_t id = id_to_int(tid);
82         SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
83         SnapVector< SnapList<action_list_t *> *> *
84                 thrd_func_act_lists = execution->get_thrd_func_act_lists();
85
86         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
87         uint32_t last_func_id = (*thrd_func_list)[id].back();
88
89         if (last_func_id == func_id) {
90                 FuncNode * func_node = func_nodes[func_id];
91                 func_node->set_predicate_tree_position(tid, NULL);
92                 func_node->reset_inst_act_map(tid);
93
94                 action_list_t * curr_act_list = func_act_lists->back();
95
96                 /* defer the processing of curr_act_list until the function has exits a few times 
97                  * (currently twice) so that more information can be gathered to infer nullity predicates.
98                  */
99                 func_node->incr_exit_count();
100                 if (func_node->get_exit_count() >= 2) {
101                         SnapList<action_list_t *> * action_list_buffer = func_node->get_action_list_buffer();
102                         while (action_list_buffer->size() > 0) {
103                                 action_list_t * act_list = action_list_buffer->back();
104                                 action_list_buffer->pop_back();
105                                 func_node->update_tree(act_list);
106                         }
107
108                         func_node->update_tree(curr_act_list);
109                 } else
110                         func_node->get_action_list_buffer()->push_front(curr_act_list);
111
112                 (*thrd_func_list)[id].pop_back();
113                 func_act_lists->pop_back();
114         } else {
115                 model_print("trying to exit with a wrong function id\n");
116                 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
117         }
118         //model_print("thread %d exiting func %d\n", tid, func_id);
119 }
120
121 void ModelHistory::resize_func_nodes(uint32_t new_size)
122 {
123         uint32_t old_size = func_nodes.size();
124
125         if ( old_size < new_size )
126                 func_nodes.resize(new_size);
127
128         for (uint32_t id = old_size; id < new_size; id++) {
129                 const char * func_name = func_map_rev[id];
130                 FuncNode * func_node = new FuncNode(this);
131                 func_node->set_func_id(id);
132                 func_node->set_func_name(func_name);
133                 func_nodes[id] = func_node;
134         }
135 }
136
137 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
138 {
139         ModelExecution * execution = model->get_execution();
140         /* Return if thread i has not entered any function or has exited
141            from all functions */
142         SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
143         SnapVector< SnapList<action_list_t *> *> *
144                 thrd_func_act_lists = execution->get_thrd_func_act_lists();
145
146         uint32_t id = id_to_int(tid);
147         if ( thrd_func_list->size() <= id )
148                 return;
149
150         /* Get the function id that thread i is currently in */
151         uint32_t func_id = (*thrd_func_list)[id].back();
152         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
153
154         if (act->is_write()) {
155                 void * location = act->get_location();
156                 uint64_t value = act->get_write_value();
157                 update_write_history(location, value);
158
159                 /* Update FuncNodes that may read from this location */
160                 SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
161                 for (uint i = 0; i < func_node_list->size(); i++) {
162                         FuncNode * func_node = (*func_node_list)[i];
163                         func_node->add_to_val_loc_map(value, location);
164                 }
165
166                 check_waiting_write(act);
167         }
168
169         /* The following does not care about actions without a position */
170         if (func_id == 0 || act->get_position() == NULL)
171                 return;
172
173         action_list_t * curr_act_list = func_act_lists->back();
174         ASSERT(curr_act_list != NULL);
175
176         if (skip_action(act, curr_act_list))
177                 return;
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 = 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_id == 0)
199                 return NULL;
200
201         // This node has not been added to func_nodes
202         if (func_nodes.size() <= func_id)
203                 return NULL;
204
205         return func_nodes[func_id];
206 }
207
208 /* Return the current FuncNode when given a thread id */
209 FuncNode * ModelHistory::get_curr_func_node(thread_id_t tid)
210 {
211         int thread_id = id_to_int(tid);
212         SnapVector<func_id_list_t> * thrd_func_list =  model->get_execution()->get_thrd_func_list();
213         uint32_t func_id = (*thrd_func_list)[thread_id].back();
214
215         if (func_id != 0) {
216                 return func_nodes[func_id];
217         }
218
219         return NULL;
220 }
221
222 void ModelHistory::update_write_history(void * location, uint64_t write_val)
223 {
224         value_set_t * write_set = write_history->get(location);
225
226         if (write_set == NULL) {
227                 write_set = new value_set_t();
228                 write_history->put(location, write_set);
229         }
230
231         write_set->add(write_val);
232 }
233
234 void ModelHistory::update_loc_rd_func_nodes_map(void * location, FuncNode * node)
235 {
236         SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
237         func_node_list->push_back(node);
238 }
239
240 void ModelHistory::update_loc_wr_func_nodes_map(void * location, FuncNode * node)
241 {
242         SnapVector<FuncNode *> * func_node_list = getWrFuncNodes(location);
243         func_node_list->push_back(node);
244 }
245
246 SnapVector<FuncNode *> * ModelHistory::getRdFuncNodes(void * location)
247 {
248         SnapVector<FuncNode *> * func_node_list = loc_rd_func_nodes_map->get(location);
249         if (func_node_list == NULL) {
250                 func_node_list = new SnapVector<FuncNode *>();
251                 loc_rd_func_nodes_map->put(location, func_node_list);
252         }
253
254         return func_node_list;
255 }
256
257 SnapVector<FuncNode *> * ModelHistory::getWrFuncNodes(void * location)
258 {
259         SnapVector<FuncNode *> * func_node_list = loc_wr_func_nodes_map->get(location);
260         if (func_node_list == NULL) {
261                 func_node_list = new SnapVector<FuncNode *>();
262                 loc_wr_func_nodes_map->put(location, func_node_list);
263         }
264
265         return func_node_list;
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         /* Linear search should be fine because presumably not many ConcretePredicates
296          * are at the same memory location */
297         for (uint i = 0; i < concrete_preds->size(); i++) {
298                 ConcretePredicate * current = (*concrete_preds)[i];
299                 if (concrete == current) {
300                         (*concrete_preds)[i] = concrete_preds->back();
301                         concrete_preds->pop_back();
302                         break;
303                 }
304         }
305
306         int thread_id = id_to_int( concrete->get_tid() );
307         (*thrd_waiting_write)[thread_id] = NULL;
308         delete concrete;
309 }
310
311 /* Check if any other thread is waiting for this write action. If so, "notify" them */
312 void ModelHistory::check_waiting_write(ModelAction * write_act)
313 {
314         void * location = write_act->get_location();
315         uint64_t value = write_act->get_write_value();
316         SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
317         SnapVector<ConcretePredicate *> to_remove = SnapVector<ConcretePredicate *>();
318         if (concrete_preds == NULL)
319                 return;
320
321         uint index = 0;
322         while (index < concrete_preds->size()) {
323                 ConcretePredicate * concrete_pred = (*concrete_preds)[index];
324                 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
325                 bool satisfy_predicate = true;
326                 /* Check if the written value satisfies every predicate expression */
327                 for (uint i = 0; i < concrete_exprs->size(); i++) {
328                         struct concrete_pred_expr concrete = (*concrete_exprs)[i];
329                         bool equality;
330                         switch (concrete.token) {
331                                 case EQUALITY:
332                                         equality = (value == concrete.value);
333                                         break;
334                                 case NULLITY:
335                                         equality = ((void*)value == NULL);
336                                         break;
337                                 default:
338                                         model_print("unknown predicate token");
339                                         break;
340                         }
341
342                         if (equality != concrete.equality) {
343                                 satisfy_predicate = false;
344                                 break;
345                         }
346                 }
347
348                 if (satisfy_predicate) {
349                         to_remove.push_back(concrete_pred);
350                 }
351
352                 index++;
353         }
354
355         for (uint i = 0; i < to_remove.size(); i++) {
356                 ConcretePredicate * concrete_pred = to_remove[i];
357
358                 /* Wake up threads */
359                 thread_id_t tid = concrete_pred->get_tid();
360                 Thread * thread = model->get_thread(tid);
361
362                 model_print("** thread %d is woken up\n", thread->get_id());
363                 model->get_execution()->getFuzzer()->notify_paused_thread(thread);
364         }
365 }
366
367 WaitObj * ModelHistory::getWaitObj(thread_id_t tid)
368 {
369         int thread_id = id_to_int(tid);
370         int old_size = thrd_wait_obj->size();
371         if (old_size <= thread_id) {
372                 thrd_wait_obj->resize(thread_id + 1);
373                 for (int i = old_size; i < thread_id + 1; i++) {
374                         (*thrd_wait_obj)[i] = new WaitObj( int_to_id(i) );
375                 }
376         }
377
378         return (*thrd_wait_obj)[thread_id];
379 }
380
381 void ModelHistory::add_waiting_thread(thread_id_t self_id,
382                 thread_id_t waiting_for_id, int dist)
383 {
384         WaitObj * self_wait_obj = getWaitObj(self_id);
385         self_wait_obj->add_waiting_for(waiting_for_id, dist);
386
387         /* Update waited-by relation */
388         WaitObj * other_wait_obj = getWaitObj(waiting_for_id);
389         other_wait_obj->add_waited_by(self_id);
390 }
391
392 void ModelHistory::remove_waiting_thread(thread_id_t tid)
393 {
394         WaitObj * self_wait_obj = getWaitObj(tid);
395         thrd_id_set_t * waiting_for = self_wait_obj->getWaitingFor();
396
397         thrd_id_set_iter * iter = waiting_for->iterator();
398         while (iter->hasNext()) {
399                 thread_id_t other_id = iter->next();
400                 WaitObj * other_wait_obj = getWaitObj(other_id);
401                 other_wait_obj->remove_waited_by(tid);
402         }
403
404         waiting_for->reset();
405 }
406
407 SnapVector<inst_act_map_t *> * ModelHistory::getThrdInstActMap(uint32_t func_id)
408 {
409         ASSERT(func_id != 0);
410
411         SnapVector<inst_act_map_t *> * maps = func_inst_act_maps->get(func_id);
412         if (maps == NULL) {
413                 maps = new SnapVector<inst_act_map_t *>();
414                 func_inst_act_maps->put(func_id, maps);
415         }
416
417         return maps;
418 }
419
420 bool ModelHistory::skip_action(ModelAction * act, SnapList<ModelAction *> * curr_act_list)
421 {
422         bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
423         modelclock_t curr_seq_number = act->get_seq_number();
424
425         /* Skip actions that are second part of a read modify write */
426         if (second_part_of_rmw)
427                 return true;
428
429         /* Skip actions with the same sequence number */
430         if (curr_act_list->size() != 0) {
431                 ModelAction * last_act = curr_act_list->back();
432                 if (last_act->get_seq_number() == curr_seq_number)
433                         return true;
434         }
435
436         /* Skip actions that are paused by fuzzer (sequence number is 0) */
437         if (curr_seq_number == 0)
438                 return true;
439
440         return false;
441 }
442
443 /* Reallocate some snapshotted memories when new executions start */
444 void ModelHistory::set_new_exec_flag()
445 {
446         for (uint i = 1; i < func_nodes.size(); i++) {
447                 FuncNode * func_node = func_nodes[i];
448                 func_node->set_new_exec_flag();
449         }
450 }
451
452 void ModelHistory::dump_func_node_graph()
453 {
454         model_print("digraph func_node_graph {\n");
455         for (uint i = 1; i < func_nodes.size(); i++) {
456                 FuncNode * node = func_nodes[i];
457                 ModelList<FuncNode *> * out_edges = node->get_out_edges();
458
459                 model_print("\"%p\" [label=\"%s\"]\n", node, node->get_func_name());
460                 mllnode<FuncNode *> * it;
461                 for (it = out_edges->begin(); it != NULL; it = it->getNext()) {
462                         FuncNode * other = it->getVal();
463                         model_print("\"%p\" -> \"%p\"\n", node, other);
464                 }
465         }
466         model_print("}\n");
467 }
468
469 void ModelHistory::print_func_node()
470 {
471         /* function id starts with 1 */
472         for (uint32_t i = 1; i < func_nodes.size(); i++) {
473                 FuncNode * func_node = func_nodes[i];
474
475                 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
476                 model_print("function %s has entry actions\n", func_node->get_func_name());
477
478                 mllnode<FuncInst*>* it;
479                 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
480                         FuncInst *inst = it->getVal();
481                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
482                 }
483         }
484 }
485
486 void ModelHistory::print_waiting_threads()
487 {
488         ModelExecution * execution = model->get_execution();
489         for (unsigned int i = 0; i < execution->get_num_threads();i++) {
490                 thread_id_t tid = int_to_id(i);
491                 WaitObj * wait_obj = getWaitObj(tid);
492                 wait_obj->print_waiting_for();
493         }
494
495         for (unsigned int i = 0; i < execution->get_num_threads();i++) {
496                 thread_id_t tid = int_to_id(i);
497                 WaitObj * wait_obj = getWaitObj(tid);
498                 wait_obj->print_waited_by();
499         }
500 }