98d56f2192fa7872364453fba10227cd04fb038e
[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 #include "schedule.h"
8
9 NewFuzzer::NewFuzzer() :
10         thrd_last_read_act(),
11         thrd_curr_pred(),
12         thrd_selected_child_branch(),
13         thrd_pruned_writes(),
14         paused_thread_set()
15 {}
16
17 /**
18  * @brief Register the ModelHistory and ModelExecution engine
19  */
20 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
21 {
22         this->history = history;
23         this->execution = execution;
24 }
25
26 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
27 {
28         thread_id_t tid = read->get_tid();
29         int thread_id = id_to_int(tid);
30
31         if (thrd_last_read_act.size() <= (uint) thread_id)
32                 thrd_last_read_act.resize(thread_id + 1);
33
34         // A new read action is encountered, select a random child branch of current predicate
35         if (read != thrd_last_read_act[thread_id]) {
36                 thrd_last_read_act[thread_id] = read;
37
38                 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
39                 uint32_t func_id = (*thrd_func_list)[thread_id].back();
40                 FuncNode * func_node = history->get_func_node(func_id);
41                 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
42                 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
43                 FuncInst * read_inst = func_node->get_inst(read);
44
45                 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
46                 prune_writes(tid, selected_branch, rf_set, inst_act_map);
47         }
48
49         // TODO: make this thread sleep if no write satisfies the chosen predicate
50         // if no read satisfies the selected predicate
51         if ( rf_set->size() == 0 ) {
52                 Thread * read_thread = execution->get_thread(tid);
53                 model_print("the %d read action of thread %d is unsuccessful\n", read->get_seq_number(), read_thread->get_id());
54
55                 // reset thread pending action and revert sequence numbers
56                 read_thread->set_pending(read);
57                 read->reset_seq_number();
58                 execution->restore_last_seq_num();
59                 
60                 conditional_sleep(read_thread);
61                 return -1;
62 /*
63                 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
64                 for (uint i = 0; i < pruned_writes->size(); i++)
65                         rf_set->push_back( (*pruned_writes)[i] );
66                 pruned_writes->clear();
67 */
68         }
69
70         ASSERT(rf_set->size() != 0);
71         int random_index = random() % rf_set->size();
72
73         return random_index;
74 }
75
76 /* Select a random branch from the children of curr_pred 
77  * @return The selected branch
78  */
79 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
80 {
81         int thread_id = id_to_int(tid);
82         if ( thrd_selected_child_branch.size() <= (uint) thread_id)
83                 thrd_selected_child_branch.resize(thread_id + 1);
84
85         if (curr_pred == NULL || read_inst == NULL) {
86                 thrd_selected_child_branch[thread_id] = NULL;
87                 return NULL;
88         }
89
90         ModelVector<Predicate *> * children = curr_pred->get_children();
91         SnapVector<Predicate *> branches;
92
93         for (uint i = 0; i < children->size(); i++) {
94                 Predicate * child = (*children)[i];
95                 if (child->get_func_inst() == read_inst)
96                         branches.push_back(child);
97         }
98
99         // predicate children have not been generated
100         if (branches.size() == 0) {
101                 thrd_selected_child_branch[thread_id] = NULL;
102                 return NULL;
103         }
104
105         // randomly select a branch
106         int random_index = random() % branches.size();
107         Predicate * random_branch = branches[ random_index ];
108         thrd_selected_child_branch[thread_id] = random_branch;
109
110         return random_branch;
111 }
112
113 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
114 {
115         int thread_id = id_to_int(tid);
116         if (thrd_selected_child_branch.size() <= (uint) thread_id)
117                 return NULL;
118
119         return thrd_selected_child_branch[thread_id];
120 }
121
122 /* Remove writes from the rf_set that do not satisfie the selected predicate, 
123  * and store them in thrd_pruned_writes
124  *
125  * @return true if rf_set is pruned
126  */
127 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
128         SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
129 {
130         if (pred == NULL)
131                 return false;
132
133         PredExprSet * pred_expressions = pred->get_pred_expressions();
134         if (pred_expressions->getSize() == 0)   // unset predicates
135                 return false;
136
137         int thread_id = id_to_int(tid);
138         uint old_size = thrd_pruned_writes.size();
139         if (thrd_pruned_writes.size() <= (uint) thread_id) {
140                 uint new_size = thread_id + 1;
141                 thrd_pruned_writes.resize(new_size);
142                 for (uint i = old_size; i < new_size; i++)
143                         thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
144         }
145         SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
146         pruned_writes->clear(); // clear the old pruned_writes set
147
148         bool pruned = false;
149         uint index = 0;
150         while ( index < rf_set->size() ) {
151                 ModelAction * write_act = (*rf_set)[index];
152                 bool satisfy_predicate = true;
153
154                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
155                 while (pred_expr_it->hasNext()) {
156                         struct pred_expr * expression = pred_expr_it->next();
157                         uint64_t write_val = write_act->get_write_value();
158                         bool equality;
159
160                         // No predicate, return false
161                         if (expression->token == NOPREDICATE)
162                                 return pruned;
163
164                         switch(expression->token) {
165                                 case EQUALITY:
166                                         FuncInst * to_be_compared;
167                                         ModelAction * last_act;
168                                         uint64_t last_read;
169
170                                         to_be_compared = expression->func_inst;
171                                         last_act = inst_act_map->get(to_be_compared);
172                                         last_read = last_act->get_reads_from_value();
173
174                                         equality = (write_val == last_read);
175                                         if (equality != expression->value)
176                                                 satisfy_predicate = false;
177                                         break;
178                                 case NULLITY:
179                                         equality = ((void*)write_val == NULL);
180                                         if (equality != expression->value)
181                                                 satisfy_predicate = false;
182                                         break;
183                                 default:
184                                         model_print("unknown predicate token\n");
185                                         break;
186                         }
187
188                         if (!satisfy_predicate)
189                                 break;
190                 }
191
192                 if (!satisfy_predicate) {
193                         ASSERT(rf_set != NULL);
194                         (*rf_set)[index] = rf_set->back();
195                         rf_set->pop_back();
196                         pruned_writes->push_back(write_act);
197                         pruned = true;
198                 } else
199                         index++;
200         }
201
202         return pruned;
203 }
204
205 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate. 
206  *
207  * @param thread A thread whose last action is a read
208  */
209 void NewFuzzer::conditional_sleep(Thread * thread)
210 {
211         model->getScheduler()->add_sleep(thread);
212         paused_thread_set.push_back(thread);
213 }
214
215 bool NewFuzzer::has_paused_threads()
216 {
217         return paused_thread_set.size() != 0;
218 }
219
220 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
221 {
222         if (numthreads == 0 && has_paused_threads()) {
223                 wake_up_paused_threads(threadlist, &numthreads);
224                 model_print("list size: %d\n", numthreads);
225                 model_print("active t id: %d\n", threadlist[0]);
226         }
227
228         int random_index = random() % numthreads;
229         int thread = threadlist[random_index];
230         thread_id_t curr_tid = int_to_id(thread);
231         return model->get_thread(curr_tid);
232 }
233
234 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
235 {
236         int random_index = random() % paused_thread_set.size();
237         Thread * thread = paused_thread_set[random_index];
238         model->getScheduler()->remove_sleep(thread);
239
240         paused_thread_set[random_index] = paused_thread_set.back();
241         paused_thread_set.pop_back();
242
243         model_print("thread %d is woken up\n", thread->get_id());
244         threadlist[*numthreads] = thread->get_id();
245         (*numthreads)++;
246 }
247
248 bool NewFuzzer::shouldWait(const ModelAction * act)
249 {
250         return random() & 1;
251 }