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