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