1e746045f7ab4e0c3a9e1d1fece5abdb3759db8f
[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 ModelHistory::~ModelHistory()
32 {
33         // TODO: complete deconstructor; maybe not needed
34         for (uint i = 0; i < thrd_wait_obj->size(); i++)
35                 delete (*thrd_wait_obj)[i];
36 }
37
38 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
39 {
40         //model_print("thread %d entering func %d\n", tid, func_id);
41         ModelExecution * execution = model->get_execution();
42         uint id = id_to_int(tid);
43
44         SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
45         SnapVector< SnapList<action_list_t *> *> *
46                 thrd_func_act_lists = execution->get_thrd_func_act_lists();
47         SnapVector<uint32_t> * thrd_last_entered_func = execution->get_thrd_last_entered_func();
48
49         if ( thrd_func_list->size() <= id ) {
50                 uint oldsize = thrd_func_list->size();
51                 thrd_func_list->resize( id + 1 );
52                 thrd_func_act_lists->resize( id + 1 );
53
54                 for (uint i = oldsize; i < id + 1; i++) {
55                         // push 0 as a dummy function id to a void seg fault
56                         new (&(*thrd_func_list)[i]) func_id_list_t();
57                         (*thrd_func_list)[i].push_back(0);
58
59                         (*thrd_func_act_lists)[i] = new SnapList<action_list_t *>();
60                         thrd_last_entered_func->push_back(0);
61                 }
62         }
63
64         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
65         func_act_lists->push_back( new action_list_t() );
66
67         uint32_t last_entered_func_id = (*thrd_last_entered_func)[id];
68         (*thrd_last_entered_func)[id] = func_id;
69         (*thrd_func_list)[id].push_back(func_id);
70
71         if ( func_nodes.size() <= func_id )
72                 resize_func_nodes( func_id + 1 );
73
74         FuncNode * func_node = func_nodes[func_id];
75         func_node->init_predicate_tree_position(tid);
76         func_node->init_inst_act_map(tid);
77
78         /* Add edges between FuncNodes */
79         if (last_entered_func_id != 0) {
80                 FuncNode * last_func_node = func_nodes[last_entered_func_id];
81                 last_func_node->add_out_edge(func_node);
82         }
83
84         monitor_waiting_thread(func_id, tid);
85 }
86
87 /* @param func_id a non-zero value */
88 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
89 {
90         ModelExecution * execution = model->get_execution();
91         uint32_t id = id_to_int(tid);
92         SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
93         SnapVector< SnapList<action_list_t *> *> *
94                 thrd_func_act_lists = execution->get_thrd_func_act_lists();
95
96         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
97         uint32_t last_func_id = (*thrd_func_list)[id].back();
98
99         if (last_func_id == func_id) {
100                 FuncNode * func_node = func_nodes[func_id];
101                 func_node->set_predicate_tree_position(tid, NULL);
102                 func_node->reset_inst_act_map(tid);
103
104                 action_list_t * curr_act_list = func_act_lists->back();
105
106                 /* defer the processing of curr_act_list until the function has exits a few times 
107                  * (currently twice) so that more information can be gathered to infer nullity predicates.
108                  */
109                 func_node->incr_exit_count();
110                 if (func_node->get_exit_count() >= 2) {
111                         SnapList<action_list_t *> * action_list_buffer = func_node->get_action_list_buffer();
112                         while (action_list_buffer->size() > 0) {
113                                 action_list_t * act_list = action_list_buffer->back();
114                                 action_list_buffer->pop_back();
115                                 func_node->update_tree(act_list);
116                         }
117
118                         func_node->update_tree(curr_act_list);
119                 } else
120                         func_node->get_action_list_buffer()->push_front(curr_act_list);
121
122                 (*thrd_func_list)[id].pop_back();
123                 func_act_lists->pop_back();
124         } else {
125                 model_print("trying to exit with a wrong function id\n");
126                 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
127         }
128         //model_print("thread %d exiting func %d\n", tid, func_id);
129 }
130
131 void ModelHistory::resize_func_nodes(uint32_t new_size)
132 {
133         uint32_t old_size = func_nodes.size();
134
135         if ( old_size < new_size )
136                 func_nodes.resize(new_size);
137
138         for (uint32_t id = old_size; id < new_size; id++) {
139                 const char * func_name = func_map_rev[id];
140                 FuncNode * func_node = new FuncNode(this);
141                 func_node->set_func_id(id);
142                 func_node->set_func_name(func_name);
143                 func_nodes[id] = func_node;
144         }
145 }
146
147 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
148 {
149         ModelExecution * execution = model->get_execution();
150         /* Return if thread i has not entered any function or has exited
151            from all functions */
152         SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
153         SnapVector< SnapList<action_list_t *> *> *
154                 thrd_func_act_lists = execution->get_thrd_func_act_lists();
155
156         uint32_t id = id_to_int(tid);
157         if ( thrd_func_list->size() <= id )
158                 return;
159
160         /* Get the function id that thread i is currently in */
161         uint32_t func_id = (*thrd_func_list)[id].back();
162         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
163
164         if (act->is_write()) {
165                 void * location = act->get_location();
166                 uint64_t value = act->get_write_value();
167                 update_write_history(location, value);
168
169                 /* Notify FuncNodes that may read from this location */
170                 SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
171                 for (uint i = 0; i < func_node_list->size(); i++) {
172                         FuncNode * func_node = (*func_node_list)[i];
173                         func_node->add_to_val_loc_map(value, location);
174                 }
175
176                 check_waiting_write(act);
177         }
178
179         /* The following does not care about actions without a position */
180         if (func_id == 0 || act->get_position() == NULL)
181                 return;
182
183         action_list_t * curr_act_list = func_act_lists->back();
184         ASSERT(curr_act_list != NULL);
185
186         if (skip_action(act, curr_act_list))
187                 return;
188
189         FuncNode * func_node = func_nodes[func_id];
190
191         /* Add to curr_inst_list */
192         curr_act_list->push_back(act);
193         func_node->add_inst(act);
194
195         if (act->is_read()) {
196                 func_node->update_inst_act_map(tid, act);
197
198                 // Update predicate tree position
199                 Fuzzer * fuzzer = execution->getFuzzer();
200                 Predicate * selected_branch = fuzzer->get_selected_child_branch(tid);
201                 func_node->set_predicate_tree_position(tid, selected_branch);
202         }
203 }
204
205 /* Return the FuncNode given its func_id  */
206 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
207 {
208         if (func_id == 0)
209                 return NULL;
210
211         // This node has not been added to func_nodes
212         if (func_nodes.size() <= func_id)
213                 return NULL;
214
215         return func_nodes[func_id];
216 }
217
218 /* Return the current FuncNode when given a thread id */
219 FuncNode * ModelHistory::get_curr_func_node(thread_id_t tid)
220 {
221         int thread_id = id_to_int(tid);
222         SnapVector<func_id_list_t> * thrd_func_list =  model->get_execution()->get_thrd_func_list();
223         uint32_t func_id = (*thrd_func_list)[thread_id].back();
224
225         if (func_id != 0) {
226                 return func_nodes[func_id];
227         }
228
229         return NULL;
230 }
231
232 void ModelHistory::update_write_history(void * location, uint64_t write_val)
233 {
234         value_set_t * write_set = write_history->get(location);
235
236         if (write_set == NULL) {
237                 write_set = new value_set_t();
238                 write_history->put(location, write_set);
239         }
240
241         write_set->add(write_val);
242 }
243
244 void ModelHistory::update_loc_rd_func_nodes_map(void * location, FuncNode * node)
245 {
246         SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
247         func_node_list->push_back(node);
248 }
249
250 void ModelHistory::update_loc_wr_func_nodes_map(void * location, FuncNode * node)
251 {
252         SnapVector<FuncNode *> * func_node_list = getWrFuncNodes(location);
253         func_node_list->push_back(node);
254 }
255
256 SnapVector<FuncNode *> * ModelHistory::getRdFuncNodes(void * location)
257 {
258         SnapVector<FuncNode *> * func_node_list = loc_rd_func_nodes_map->get(location);
259         if (func_node_list == NULL) {
260                 func_node_list = new SnapVector<FuncNode *>();
261                 loc_rd_func_nodes_map->put(location, func_node_list);
262         }
263
264         return func_node_list;
265 }
266
267 SnapVector<FuncNode *> * ModelHistory::getWrFuncNodes(void * location)
268 {
269         SnapVector<FuncNode *> * func_node_list = loc_wr_func_nodes_map->get(location);
270         if (func_node_list == NULL) {
271                 func_node_list = new SnapVector<FuncNode *>();
272                 loc_wr_func_nodes_map->put(location, func_node_list);
273         }
274
275         return func_node_list;
276 }
277
278 /* When a thread is paused by Fuzzer, keep track of the condition it is waiting for */
279 void ModelHistory::add_waiting_write(ConcretePredicate * concrete)
280 {
281         void * location = concrete->get_location();
282         SnapVector<ConcretePredicate *> * waiting_conditions = loc_waiting_writes_map->get(location);
283         if (waiting_conditions == NULL) {
284                 waiting_conditions = new SnapVector<ConcretePredicate *>();
285                 loc_waiting_writes_map->put(location, waiting_conditions);
286         }
287
288         /* waiting_conditions should not have duplications */
289         waiting_conditions->push_back(concrete);
290
291         int thread_id = id_to_int(concrete->get_tid());
292         if (thrd_waiting_write->size() <= (uint) thread_id) {
293                 thrd_waiting_write->resize(thread_id + 1);
294         }
295
296         (*thrd_waiting_write)[thread_id] = concrete;
297 }
298
299 void ModelHistory::remove_waiting_write(thread_id_t tid)
300 {
301         ConcretePredicate * concrete = (*thrd_waiting_write)[ id_to_int(tid) ];
302         void * location = concrete->get_location();
303         SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
304
305         /* Linear search should be fine because presumably not many ConcretePredicates
306          * are at the same memory location */
307         for (uint i = 0; i < concrete_preds->size(); i++) {
308                 ConcretePredicate * current = (*concrete_preds)[i];
309                 if (concrete == current) {
310                         (*concrete_preds)[i] = concrete_preds->back();
311                         concrete_preds->pop_back();
312                         break;
313                 }
314         }
315
316         int thread_id = id_to_int( concrete->get_tid() );
317         (*thrd_waiting_write)[thread_id] = NULL;
318         delete concrete;
319 }
320
321 /* Check if any other thread is waiting for this write action. If so, "notify" them */
322 void ModelHistory::check_waiting_write(ModelAction * write_act)
323 {
324         void * location = write_act->get_location();
325         uint64_t value = write_act->get_write_value();
326         SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
327         SnapVector<ConcretePredicate *> to_remove = SnapVector<ConcretePredicate *>();
328         if (concrete_preds == NULL)
329                 return;
330
331         uint index = 0;
332         while (index < concrete_preds->size()) {
333                 ConcretePredicate * concrete_pred = (*concrete_preds)[index];
334                 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
335                 bool satisfy_predicate = true;
336                 /* Check if the written value satisfies every predicate expression */
337                 for (uint i = 0; i < concrete_exprs->size(); i++) {
338                         struct concrete_pred_expr concrete = (*concrete_exprs)[i];
339                         bool equality;
340                         switch (concrete.token) {
341                                 case EQUALITY:
342                                         equality = (value == concrete.value);
343                                         break;
344                                 case NULLITY:
345                                         equality = ((void*)value == NULL);
346                                         break;
347                                 default:
348                                         model_print("unknown predicate token");
349                                         break;
350                         }
351
352                         if (equality != concrete.equality) {
353                                 satisfy_predicate = false;
354                                 break;
355                         }
356                 }
357
358                 if (satisfy_predicate) {
359                         to_remove.push_back(concrete_pred);
360                 }
361
362                 index++;
363         }
364
365         for (uint i = 0; i < to_remove.size(); i++) {
366                 ConcretePredicate * concrete_pred = to_remove[i];
367
368                 /* Wake up threads */
369                 thread_id_t tid = concrete_pred->get_tid();
370                 Thread * thread = model->get_thread(tid);
371
372                 model_print("** thread %d is woken up\n", thread->get_id());
373                 model->get_execution()->getFuzzer()->notify_paused_thread(thread);
374         }
375 }
376
377 WaitObj * ModelHistory::getWaitObj(thread_id_t tid)
378 {
379         int thread_id = id_to_int(tid);
380         int old_size = thrd_wait_obj->size();
381         if (old_size <= thread_id) {
382                 thrd_wait_obj->resize(thread_id + 1);
383                 for (int i = old_size; i < thread_id + 1; i++) {
384                         (*thrd_wait_obj)[i] = new WaitObj( int_to_id(i) );
385                 }
386         }
387
388         return (*thrd_wait_obj)[thread_id];
389 }
390
391 void ModelHistory::add_waiting_thread(thread_id_t self_id,
392         thread_id_t waiting_for_id, FuncNode * target_node, int dist)
393 {
394         WaitObj * self_wait_obj = getWaitObj(self_id);
395         self_wait_obj->add_waiting_for(waiting_for_id, target_node, dist);
396
397         /* Update waited-by relation */
398         WaitObj * other_wait_obj = getWaitObj(waiting_for_id);
399         other_wait_obj->add_waited_by(self_id);
400 }
401
402 /* Thread tid is woken up (or notified), so it is not waiting for others anymore */
403 void ModelHistory::remove_waiting_thread(thread_id_t tid)
404 {
405         WaitObj * self_wait_obj = getWaitObj(tid);
406         thrd_id_set_t * waiting_for = self_wait_obj->getWaitingFor();
407
408         /* Remove tid from waited_by's */
409         thrd_id_set_iter * iter = waiting_for->iterator();
410         while (iter->hasNext()) {
411                 thread_id_t other_id = iter->next();
412                 WaitObj * other_wait_obj = getWaitObj(other_id);
413                 other_wait_obj->remove_waited_by(tid);
414         }
415
416         self_wait_obj->clear_waiting_for();
417 }
418
419 void ModelHistory::stop_waiting_for(thread_id_t self_id,
420         thread_id_t waiting_for_id, FuncNode * target_node)
421 {
422         WaitObj * self_wait_obj = getWaitObj(self_id);
423         bool thread_removed = self_wait_obj->remove_waiting_for(waiting_for_id, target_node);
424
425         // model_print("\t%d gives up %d on node %d\n", self_id, waiting_for_id, target_node->get_func_id());
426
427         /* If thread self_id is not waiting for waiting_for_id anymore */
428         if (thread_removed) {
429                 WaitObj * other_wait_obj = getWaitObj(waiting_for_id);
430                 other_wait_obj->remove_waited_by(self_id);
431
432                 thrd_id_set_t * self_waiting_for = self_wait_obj->getWaitingFor();
433                 if ( self_waiting_for->isEmpty() ) {
434                         // model_print("\tthread %d waits for nobody, wake up\n", self_id);
435                         ModelExecution * execution = model->get_execution();
436                         Thread * thread = execution->get_thread(self_id);
437                         execution->getFuzzer()->notify_paused_thread(thread);
438                 }
439         }
440 }
441
442 SnapVector<inst_act_map_t *> * ModelHistory::getThrdInstActMap(uint32_t func_id)
443 {
444         ASSERT(func_id != 0);
445
446         SnapVector<inst_act_map_t *> * maps = func_inst_act_maps->get(func_id);
447         if (maps == NULL) {
448                 maps = new SnapVector<inst_act_map_t *>();
449                 func_inst_act_maps->put(func_id, maps);
450         }
451
452         return maps;
453 }
454
455 bool ModelHistory::skip_action(ModelAction * act, SnapList<ModelAction *> * curr_act_list)
456 {
457         bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
458         modelclock_t curr_seq_number = act->get_seq_number();
459
460         /* Skip actions that are second part of a read modify write */
461         if (second_part_of_rmw)
462                 return true;
463
464         /* Skip actions with the same sequence number */
465         if (curr_act_list->size() != 0) {
466                 ModelAction * last_act = curr_act_list->back();
467                 if (last_act->get_seq_number() == curr_seq_number)
468                         return true;
469         }
470
471         /* Skip actions that are paused by fuzzer (sequence number is 0) */
472         if (curr_seq_number == 0)
473                 return true;
474
475         return false;
476 }
477
478 /* Monitor thread tid and decide whether other threads (that are waiting for tid)
479  * should keep waiting for this thread or not. Shall only be called when a thread
480  * enters a function. 
481  *
482  * Heuristics: If the distance from the current FuncNode to some target node
483  * ever increases, stop waiting for this thread on this target node. 
484  */
485 void ModelHistory::monitor_waiting_thread(uint32_t func_id, thread_id_t tid)
486 {
487         WaitObj * wait_obj = getWaitObj(tid);
488         thrd_id_set_t * waited_by = wait_obj->getWaitedBy();
489         FuncNode * curr_node = func_nodes[func_id];
490
491         /* For each thread waiting for tid */
492         thrd_id_set_iter * tid_iter = waited_by->iterator();
493         while (tid_iter->hasNext()) {
494                 thread_id_t waited_by_id = tid_iter->next();
495                 WaitObj * other_wait_obj = getWaitObj(waited_by_id);
496
497                 node_set_t * target_nodes = other_wait_obj->getTargetNodes(tid);
498                 node_set_iter * node_iter = target_nodes->iterator();
499                 while (node_iter->hasNext()) {
500                         FuncNode * target = node_iter->next();
501                         int old_dist = other_wait_obj->lookup_dist(tid, target);
502                         int new_dist = curr_node->compute_distance(target, old_dist);
503
504                         if (new_dist == -1) {
505                                 stop_waiting_for(waited_by_id, tid, target);
506                         }
507                 }
508         }
509 }
510
511 /* Reallocate some snapshotted memories when new executions start */
512 void ModelHistory::set_new_exec_flag()
513 {
514         for (uint i = 1; i < func_nodes.size(); i++) {
515                 FuncNode * func_node = func_nodes[i];
516                 func_node->set_new_exec_flag();
517         }
518 }
519
520 void ModelHistory::dump_func_node_graph()
521 {
522         model_print("digraph func_node_graph {\n");
523         for (uint i = 1; i < func_nodes.size(); i++) {
524                 FuncNode * node = func_nodes[i];
525                 ModelList<FuncNode *> * out_edges = node->get_out_edges();
526
527                 model_print("\"%p\" [label=\"%s\"]\n", node, node->get_func_name());
528                 mllnode<FuncNode *> * it;
529                 for (it = out_edges->begin(); it != NULL; it = it->getNext()) {
530                         FuncNode * other = it->getVal();
531                         model_print("\"%p\" -> \"%p\"\n", node, other);
532                 }
533         }
534         model_print("}\n");
535 }
536
537 void ModelHistory::print_func_node()
538 {
539         /* function id starts with 1 */
540         for (uint32_t i = 1; i < func_nodes.size(); i++) {
541                 FuncNode * func_node = func_nodes[i];
542
543                 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
544                 model_print("function %s has entry actions\n", func_node->get_func_name());
545
546                 mllnode<FuncInst*>* it;
547                 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
548                         FuncInst *inst = it->getVal();
549                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
550                 }
551         }
552 }
553
554 void ModelHistory::print_waiting_threads()
555 {
556         ModelExecution * execution = model->get_execution();
557         for (unsigned int i = 0; i < execution->get_num_threads();i++) {
558                 thread_id_t tid = int_to_id(i);
559                 WaitObj * wait_obj = getWaitObj(tid);
560                 wait_obj->print_waiting_for();
561         }
562
563         for (unsigned int i = 0; i < execution->get_num_threads();i++) {
564                 thread_id_t tid = int_to_id(i);
565                 WaitObj * wait_obj = getWaitObj(tid);
566                 wait_obj->print_waited_by();
567         }
568 }