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