move codes around in history.cc, and NewFuzzer::selectWrite is half working
[c11tester.git] / newfuzzer.cc
1 #include "newfuzzer.h"
2 #include "threads-model.h"
3 #include "model.h"
4 #include "action.h"
5 #include "execution.h"
6 #include "funcnode.h"
7
8 typedef HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map_t;
9
10 NewFuzzer::NewFuzzer() :
11         thrd_last_read_act(),
12         thrd_curr_pred(),
13         thrd_selected_child_branch()
14 {}
15
16 /**
17  * @brief Register the ModelHistory and ModelExecution engine
18  */
19 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
20 {
21         this->history = history;
22         this->execution = execution;
23 }
24
25 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
26 {
27         int random_index = random() % rf_set->size();
28
29         thread_id_t tid = read->get_tid();
30         int thread_id = id_to_int(tid);
31
32         if (thrd_last_read_act.size() <= (uint) thread_id)
33                 thrd_last_read_act.resize(thread_id + 1);
34
35         SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
36         uint32_t func_id = (*thrd_func_list)[thread_id].back();
37         FuncNode * func_node = history->get_func_node(func_id);
38         inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
39
40         // A new read action is encountered, select a random child branch of current predicate
41         if (read != thrd_last_read_act[thread_id]) {
42                 thrd_last_read_act[thread_id] = read;
43
44                 FuncInst * read_inst = func_node->get_inst(read);
45                 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
46                 selectBranch(thread_id, curr_pred, read_inst);
47         }
48
49         Predicate * selected_branch = thrd_selected_child_branch[thread_id];
50         if (selected_branch == NULL)
51                 return random_index;
52
53         PredExprSet * pred_expressions = selected_branch->get_pred_expressions();
54
55 //      FuncInst * read_inst = selected_branch->get_func_inst();
56 //      model_print("thread %d ", tid);
57 //      read_inst->print();
58
59         // unset predicates
60         if (pred_expressions->getSize() == 0)
61                 return random_index;
62
63         for (uint index = 0; index < rf_set->size(); index++) {
64                 ModelAction * write_act = (*rf_set)[index];
65                 bool satisfy_predicate = true;
66
67                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
68                 while (pred_expr_it->hasNext()) {
69                         struct pred_expr * expression = pred_expr_it->next();
70                         uint64_t last_read, write_val;
71                         bool equality;
72
73                         if (expression->token == NOPREDICATE)
74                                 return random_index;
75
76                         switch(expression->token) {
77                                 case EQUALITY:
78                                         FuncInst * to_be_compared;
79                                         ModelAction * last_act;
80
81                                         to_be_compared = expression->func_inst;
82                                         last_act = inst_act_map->get(to_be_compared);
83
84                                         last_read = last_act->get_reads_from_value();
85                                         write_val = write_act->get_write_value();
86
87                                         equality = (last_read == write_val);
88                                         if (equality != expression->value)
89                                                 satisfy_predicate = false;
90
91                                         model_print("equality predicate\n");
92                                         break;
93                                 case NULLITY:
94                                         model_print("nullity predicate, under construction\n");
95                                         break;
96                                 default:
97                                         model_print("unknown predicate token\n");
98                                         break;
99                         }
100
101                         if (!satisfy_predicate)
102                                 break;
103                 }
104
105                 /* TODO: collect all writes that satisfy predicate; if some of them violate
106                  * modification graph, others can be chosen */
107                 if (satisfy_predicate) {
108                         model_print("^_^ satisfy predicate\n");
109                         return index;
110                 }
111         }
112
113         // TODO: make this thread sleep if no write satisfies the chosen predicate
114         return random_index;
115 }
116
117 void NewFuzzer::selectBranch(int thread_id, Predicate * curr_pred, FuncInst * read_inst)
118 {
119         if ( thrd_selected_child_branch.size() <= (uint) thread_id)
120                 thrd_selected_child_branch.resize(thread_id + 1);
121
122         if (curr_pred == NULL || read_inst == NULL) {
123                 thrd_selected_child_branch[thread_id] = NULL;
124                 return;
125         }
126
127         ModelVector<Predicate *> * children = curr_pred->get_children();
128         SnapVector<Predicate *> branches;
129
130         for (uint i = 0; i < children->size(); i++) {
131                 Predicate * child = (*children)[i];
132                 if (child->get_func_inst() == read_inst)
133                         branches.push_back(child);
134         }
135
136         // predicate children have not been generated
137         if (branches.size() == 0) {
138                 thrd_selected_child_branch[thread_id] = NULL;
139                 return;
140         }
141
142         // randomly select a branch
143         int random_index = random() % branches.size();
144         Predicate * random_branch = branches[ random_index ];
145         thrd_selected_child_branch[thread_id] = random_branch;
146 }
147
148 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
149 {
150         int thread_id = id_to_int(tid);
151         if (thrd_selected_child_branch.size() <= (uint) thread_id)
152                 return NULL;
153
154         return thrd_selected_child_branch[thread_id];
155 }