Bug fix
[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                 // Do nothing
152         } else if (act->is_write()) {
153                 /*
154                 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
155                 FuncInst * curr_inst = func_node->get_inst(act);
156
157                 if (curr_pred) {
158                         // Follow child
159                         curr_pred = curr_pred->follow_write_child(curr_inst);
160                 }
161                 func_node->set_predicate_tree_position(tid, curr_pred);
162                 */
163         }
164
165         func_node->update_tree(act);
166         last_action = act;
167 }
168
169 /* Return the FuncNode given its func_id  */
170 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
171 {
172         if (func_id == 0)
173                 return NULL;
174
175         // This node has not been added to func_nodes
176         if (func_nodes.size() <= func_id)
177                 return NULL;
178
179         return func_nodes[func_id];
180 }
181
182 /* Return the current FuncNode when given a thread id */
183 FuncNode * ModelHistory::get_curr_func_node(thread_id_t tid)
184 {
185         int thread_id = id_to_int(tid);
186         uint32_t func_id = (*thrd_func_list)[thread_id].back();
187
188         if (func_id != 0) {
189                 return func_nodes[func_id];
190         }
191
192         return NULL;
193 }
194
195 void ModelHistory::update_write_history(void * location, uint64_t write_val)
196 {
197         value_set_t * write_set = write_history->get(location);
198
199         if (write_set == NULL) {
200                 write_set = new value_set_t();
201                 write_history->put(location, write_set);
202         }
203
204         write_set->add(write_val);
205 }
206
207 void ModelHistory::update_loc_rd_func_nodes_map(void * location, FuncNode * node)
208 {
209         SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
210         func_node_list->push_back(node);
211 }
212
213 void ModelHistory::update_loc_wr_func_nodes_map(void * location, FuncNode * node)
214 {
215         SnapVector<FuncNode *> * func_node_list = getWrFuncNodes(location);
216         func_node_list->push_back(node);
217 }
218
219 SnapVector<FuncNode *> * ModelHistory::getRdFuncNodes(void * location)
220 {
221         SnapVector<FuncNode *> * func_node_list = loc_rd_func_nodes_map->get(location);
222         if (func_node_list == NULL) {
223                 func_node_list = new SnapVector<FuncNode *>();
224                 loc_rd_func_nodes_map->put(location, func_node_list);
225         }
226
227         return func_node_list;
228 }
229
230 SnapVector<FuncNode *> * ModelHistory::getWrFuncNodes(void * location)
231 {
232         SnapVector<FuncNode *> * func_node_list = loc_wr_func_nodes_map->get(location);
233         if (func_node_list == NULL) {
234                 func_node_list = new SnapVector<FuncNode *>();
235                 loc_wr_func_nodes_map->put(location, func_node_list);
236         }
237
238         return func_node_list;
239 }
240
241 /* When a thread is paused by Fuzzer, keep track of the condition it is waiting for */
242 void ModelHistory::add_waiting_write(ConcretePredicate * concrete)
243 {
244         void * location = concrete->get_location();
245         SnapVector<ConcretePredicate *> * waiting_conditions = loc_waiting_writes_map->get(location);
246         if (waiting_conditions == NULL) {
247                 waiting_conditions = new SnapVector<ConcretePredicate *>();
248                 loc_waiting_writes_map->put(location, waiting_conditions);
249         }
250
251         /* waiting_conditions should not have duplications */
252         waiting_conditions->push_back(concrete);
253
254         int thread_id = id_to_int(concrete->get_tid());
255         if (thrd_waiting_write->size() <= (uint) thread_id) {
256                 thrd_waiting_write->resize(thread_id + 1);
257         }
258
259         (*thrd_waiting_write)[thread_id] = concrete;
260 }
261
262 void ModelHistory::remove_waiting_write(thread_id_t tid)
263 {
264         ConcretePredicate * concrete = (*thrd_waiting_write)[ id_to_int(tid) ];
265         void * location = concrete->get_location();
266         SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
267
268         /* Linear search should be fine because presumably not many ConcretePredicates
269          * are at the same memory location */
270         for (uint i = 0;i < concrete_preds->size();i++) {
271                 ConcretePredicate * current = (*concrete_preds)[i];
272                 if (concrete == current) {
273                         (*concrete_preds)[i] = concrete_preds->back();
274                         concrete_preds->pop_back();
275                         break;
276                 }
277         }
278
279         int thread_id = id_to_int( concrete->get_tid() );
280         (*thrd_waiting_write)[thread_id] = NULL;
281         delete concrete;
282 }
283
284 /* Check if any other thread is waiting for this write action. If so, "notify" them */
285 void ModelHistory::check_waiting_write(ModelAction * write_act)
286 {
287         void * location = write_act->get_location();
288         uint64_t value = write_act->get_write_value();
289         SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
290         if (concrete_preds == NULL)
291                 return;
292
293         uint index = 0;
294         while (index < concrete_preds->size()) {
295                 ConcretePredicate * concrete_pred = (*concrete_preds)[index];
296                 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
297                 bool satisfy_predicate = true;
298                 /* Check if the written value satisfies every predicate expression */
299                 for (uint i = 0;i < concrete_exprs->size();i++) {
300                         struct concrete_pred_expr concrete = (*concrete_exprs)[i];
301                         bool equality = false;
302                         switch (concrete.token) {
303                         case EQUALITY:
304                                 equality = (value == concrete.value);
305                                 break;
306                         case NULLITY:
307                                 equality = ((void*)value == NULL);
308                                 break;
309                         default:
310                                 model_print("unknown predicate token");
311                                 break;
312                         }
313
314                         if (equality != concrete.equality) {
315                                 satisfy_predicate = false;
316                                 break;
317                         }
318                 }
319
320                 if (satisfy_predicate) {
321                         /* Wake up threads */
322                         thread_id_t tid = concrete_pred->get_tid();
323                         Thread * thread = model->get_thread(tid);
324
325                         //model_print("** thread %d is woken up\n", thread->get_id());
326                         ((NewFuzzer *)model->get_execution()->getFuzzer())->notify_paused_thread(thread);
327                 }
328
329                 index++;
330         }
331 }
332
333 WaitObj * ModelHistory::getWaitObj(thread_id_t tid)
334 {
335         int thread_id = id_to_int(tid);
336         int old_size = thrd_wait_obj->size();
337         if (old_size <= thread_id) {
338                 thrd_wait_obj->resize(thread_id + 1);
339                 for (int i = old_size;i < thread_id + 1;i++) {
340                         (*thrd_wait_obj)[i] = new WaitObj( int_to_id(i) );
341                 }
342         }
343
344         return (*thrd_wait_obj)[thread_id];
345 }
346
347 void ModelHistory::add_waiting_thread(thread_id_t self_id,
348                                                                                                                                                         thread_id_t waiting_for_id, FuncNode * target_node, int dist)
349 {
350         WaitObj * self_wait_obj = getWaitObj(self_id);
351         self_wait_obj->add_waiting_for(waiting_for_id, target_node, dist);
352
353         /* Update waited-by relation */
354         WaitObj * other_wait_obj = getWaitObj(waiting_for_id);
355         other_wait_obj->add_waited_by(self_id);
356 }
357
358 /* Thread tid is woken up (or notified), so it is not waiting for others anymore */
359 void ModelHistory::remove_waiting_thread(thread_id_t tid)
360 {
361         WaitObj * self_wait_obj = getWaitObj(tid);
362         thrd_id_set_t * waiting_for = self_wait_obj->getWaitingFor();
363
364         /* Remove tid from waited_by's */
365         thrd_id_set_iter * iter = waiting_for->iterator();
366         while (iter->hasNext()) {
367                 thread_id_t other_id = iter->next();
368                 WaitObj * other_wait_obj = getWaitObj(other_id);
369                 other_wait_obj->remove_waited_by(tid);
370         }
371
372         self_wait_obj->clear_waiting_for();
373         delete iter;
374 }
375
376 void ModelHistory::stop_waiting_for_node(thread_id_t self_id,
377                                                                                                                                                                  thread_id_t waiting_for_id, FuncNode * target_node)
378 {
379         WaitObj * self_wait_obj = getWaitObj(self_id);
380         bool thread_removed = self_wait_obj->remove_waiting_for_node(waiting_for_id, target_node);
381
382         // model_print("\t%d gives up %d on node %d\n", self_id, waiting_for_id, target_node->get_func_id());
383
384         /* If thread self_id is not waiting for waiting_for_id anymore */
385         if (thread_removed) {
386                 WaitObj * other_wait_obj = getWaitObj(waiting_for_id);
387                 other_wait_obj->remove_waited_by(self_id);
388
389                 thrd_id_set_t * self_waiting_for = self_wait_obj->getWaitingFor();
390                 if ( self_waiting_for->isEmpty() ) {
391                         // model_print("\tthread %d waits for nobody, wake up\n", self_id);
392                         ModelExecution * execution = model->get_execution();
393                         Thread * thread = execution->get_thread(self_id);
394                         ((NewFuzzer *)execution->getFuzzer())->notify_paused_thread(thread);
395                 }
396         }
397 }
398
399 bool ModelHistory::skip_action(ModelAction * act)
400 {
401         bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
402         modelclock_t curr_seq_number = act->get_seq_number();
403
404         /* Skip actions that are second part of a read modify write */
405         if (second_part_of_rmw)
406                 return true;
407
408         /* Skip actions with the same sequence number */
409         if (last_action != NULL) {
410                 if (last_action->get_seq_number() == curr_seq_number)
411                         return true;
412         }
413
414         /* Skip actions that are paused by fuzzer (sequence number is 0) */
415         if (curr_seq_number == 0)
416                 return true;
417
418         return false;
419 }
420
421 /* Monitor thread tid and decide whether other threads (that are waiting for tid)
422  * should keep waiting for this thread or not. Shall only be called when a thread
423  * enters a function.
424  *
425  * Heuristics: If the distance from the current FuncNode to some target node
426  * ever increases, stop waiting for this thread on this target node.
427  */
428 void ModelHistory::monitor_waiting_thread(uint32_t func_id, thread_id_t tid)
429 {
430         WaitObj * wait_obj = getWaitObj(tid);
431         thrd_id_set_t * waited_by = wait_obj->getWaitedBy();
432         FuncNode * curr_node = func_nodes[func_id];
433
434         /* For each thread waiting for tid */
435         thrd_id_set_iter * tid_iter = waited_by->iterator();
436         while (tid_iter->hasNext()) {
437                 thread_id_t waited_by_id = tid_iter->next();
438                 WaitObj * other_wait_obj = getWaitObj(waited_by_id);
439
440                 node_set_t * target_nodes = other_wait_obj->getTargetNodes(tid);
441                 node_set_iter * node_iter = target_nodes->iterator();
442                 while (node_iter->hasNext()) {
443                         FuncNode * target = node_iter->next();
444                         int old_dist = other_wait_obj->lookup_dist(tid, target);
445                         int new_dist = curr_node->compute_distance(target, old_dist);
446
447                         if (new_dist == -1) {
448                                 stop_waiting_for_node(waited_by_id, tid, target);
449                         }
450                 }
451
452                 delete node_iter;
453         }
454
455         delete tid_iter;
456 }
457
458 void ModelHistory::monitor_waiting_thread_counter(thread_id_t tid)
459 {
460         WaitObj * wait_obj = getWaitObj(tid);
461         thrd_id_set_t * waited_by = wait_obj->getWaitedBy();
462
463         // Thread tid has taken an action, update the counter for threads waiting for tid
464         thrd_id_set_iter * tid_iter = waited_by->iterator();
465         while (tid_iter->hasNext()) {
466                 thread_id_t waited_by_id = tid_iter->next();
467                 WaitObj * other_wait_obj = getWaitObj(waited_by_id);
468
469                 bool expire = other_wait_obj->incr_counter(tid);
470                 if (expire) {
471 //                      model_print("thread %d stops waiting for thread %d\n", waited_by_id, tid);
472                         wait_obj->remove_waited_by(waited_by_id);
473                         other_wait_obj->remove_waiting_for(tid);
474
475                         thrd_id_set_t * other_waiting_for = other_wait_obj->getWaitingFor();
476                         if ( other_waiting_for->isEmpty() ) {
477                                 // model_print("\tthread %d waits for nobody, wake up\n", self_id);
478                                 ModelExecution * execution = model->get_execution();
479                                 Thread * thread = execution->get_thread(waited_by_id);
480                                 ((NewFuzzer *)execution->getFuzzer())->notify_paused_thread(thread);
481                         }
482                 }
483         }
484
485         delete tid_iter;
486 }
487
488 /* Reallocate some snapshotted memories when new executions start */
489 void ModelHistory::set_new_exec_flag()
490 {
491         for (uint i = 1;i < func_nodes.size();i++) {
492                 FuncNode * func_node = func_nodes[i];
493                 func_node->set_new_exec_flag();
494         }
495 }
496
497 void ModelHistory::dump_func_node_graph()
498 {
499         model_print("digraph func_node_graph {\n");
500         for (uint i = 1;i < func_nodes.size();i++) {
501                 FuncNode * node = func_nodes[i];
502                 ModelList<FuncNode *> * out_edges = node->get_out_edges();
503
504                 model_print("\"%p\" [label=\"%s\"]\n", node, node->get_func_name());
505                 mllnode<FuncNode *> * it;
506                 for (it = out_edges->begin();it != NULL;it = it->getNext()) {
507                         FuncNode * other = it->getVal();
508                         model_print("\"%p\" -> \"%p\"\n", node, other);
509                 }
510         }
511         model_print("}\n");
512 }
513
514 void ModelHistory::print_func_node()
515 {
516         /* function id starts with 1 */
517         for (uint32_t i = 1;i < func_nodes.size();i++) {
518                 FuncNode * func_node = func_nodes[i];
519                 func_node->print_predicate_tree();
520
521 /*
522                 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
523                 model_print("function %s has entry actions\n", func_node->get_func_name());
524
525                 mllnode<FuncInst*>* it;
526                 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
527                         FuncInst *inst = it->getVal();
528                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
529                 }
530 */
531         }
532 }
533
534 void ModelHistory::print_waiting_threads()
535 {
536         ModelExecution * execution = model->get_execution();
537         for (unsigned int i = 0;i < execution->get_num_threads();i++) {
538                 thread_id_t tid = int_to_id(i);
539                 WaitObj * wait_obj = getWaitObj(tid);
540                 wait_obj->print_waiting_for();
541         }
542
543         for (unsigned int i = 0;i < execution->get_num_threads();i++) {
544                 thread_id_t tid = int_to_id(i);
545                 WaitObj * wait_obj = getWaitObj(tid);
546                 wait_obj->print_waited_by();
547         }
548 }