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