Select a new predicate branch when the selected branch in the predicate tree fails...
[c11tester.git] / newfuzzer.cc
1 #include "newfuzzer.h"
2 #include "threads-model.h"
3 #include "action.h"
4 #include "history.h"
5 #include "funcnode.h"
6 #include "funcinst.h"
7 #include "predicate.h"
8 #include "concretepredicate.h"
9 #include "waitobj.h"
10
11 #include "model.h"
12 #include "schedule.h"
13 #include "execution.h"
14
15 NewFuzzer::NewFuzzer() :
16         thrd_last_read_act(),
17         thrd_last_func_inst(),
18         thrd_selected_child_branch(),
19         thrd_pruned_writes(),
20         paused_thread_list(),
21         paused_thread_table(128),
22         failed_predicates(32)
23 {}
24
25 /**
26  * @brief Register the ModelHistory and ModelExecution engine
27  */
28 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
29 {
30         this->history = history;
31         this->execution = execution;
32 }
33
34 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
35 {
36 //      return random() % rf_set->size();
37
38         thread_id_t tid = read->get_tid();
39         int thread_id = id_to_int(tid);
40
41         if (thrd_last_read_act.size() <= (uint) thread_id) {
42                 thrd_last_read_act.resize(thread_id + 1);
43                 thrd_last_func_inst.resize(thread_id + 1);
44         }
45
46         // A new read action is encountered, select a random child branch of current predicate
47         if (read != thrd_last_read_act[thread_id]) {
48                 FuncNode * func_node = history->get_curr_func_node(tid);
49                 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
50                 FuncInst * read_inst = func_node->get_inst(read);
51                 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
52
53                 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
54                 prune_writes(tid, selected_branch, rf_set, inst_act_map);
55
56                 if (!failed_predicates.isEmpty())
57                         failed_predicates.reset();
58
59                 thrd_last_read_act[thread_id] = read;
60                 thrd_last_func_inst[thread_id] = read_inst;
61         }
62
63         // No write satisfies the selected predicate, so pause this thread.
64         while ( rf_set->size() == 0 ) {
65                 Thread * read_thread = execution->get_thread(tid);
66                 model_print("the %d read action of thread %d at %p is unsuccessful\n", read->get_seq_number(), read_thread->get_id(), read->get_location());
67
68                 if (find_threads(read)) {
69                         // reset thread pending action and revert sequence numbers
70                         read_thread->set_pending(read);
71                         read->reset_seq_number();
72                         execution->restore_last_seq_num();
73
74                         conditional_sleep(read_thread);
75
76                         // Returning -1 stops the while loop of ModelExecution::process_read
77                         return -1;
78                 } else {
79                         Predicate * selected_branch = get_selected_child_branch(tid);
80                         failed_predicates.put(selected_branch, true);
81
82                         SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
83                         for (uint i = 0; i < pruned_writes->size(); i++) {
84                                 rf_set->push_back( (*pruned_writes)[i] );
85                         }
86
87                         // Reselect a predicate and prune writes
88                         Predicate * curr_pred = selected_branch->get_parent();
89                         FuncInst * read_inst = thrd_last_func_inst[thread_id];
90                         selected_branch = selectBranch(tid, curr_pred, read_inst);
91
92                         FuncNode * func_node = history->get_curr_func_node(tid);
93                         inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
94                         prune_writes(tid, selected_branch, rf_set, inst_act_map);
95
96                         ASSERT(selected_branch);
97                 }
98         }
99
100         ASSERT(rf_set->size() != 0);
101         int random_index = random() % rf_set->size();
102
103         return random_index;
104 }
105
106 /* Select a random branch from the children of curr_pred 
107  * @return The selected branch
108  */
109 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
110 {
111         int thread_id = id_to_int(tid);
112         if ( thrd_selected_child_branch.size() <= (uint) thread_id)
113                 thrd_selected_child_branch.resize(thread_id + 1);
114
115         if (curr_pred == NULL || read_inst == NULL) {
116                 thrd_selected_child_branch[thread_id] = NULL;
117                 return NULL;
118         }
119
120         ModelVector<Predicate *> * children = curr_pred->get_children();
121         SnapVector<Predicate *> branches;
122
123         for (uint i = 0; i < children->size(); i++) {
124                 Predicate * child = (*children)[i];
125                 if (child->get_func_inst() == read_inst && !failed_predicates.contains(child)) {
126                         branches.push_back(child);
127                 }
128         }
129
130         // predicate children have not been generated
131         if (branches.size() == 0) {
132                 thrd_selected_child_branch[thread_id] = NULL;
133                 return NULL;
134         }
135
136         // randomly select a branch
137         int random_index = random() % branches.size();
138         Predicate * random_branch = branches[ random_index ];
139         thrd_selected_child_branch[thread_id] = random_branch;
140
141         return random_branch;
142 }
143
144 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
145 {
146         int thread_id = id_to_int(tid);
147         if (thrd_selected_child_branch.size() <= (uint) thread_id)
148                 return NULL;
149
150         return thrd_selected_child_branch[thread_id];
151 }
152
153 /* Remove writes from the rf_set that do not satisfie the selected predicate, 
154  * and store them in thrd_pruned_writes
155  *
156  * @return true if rf_set is pruned
157  */
158 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
159         SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
160 {
161         if (pred == NULL)
162                 return false;
163
164         PredExprSet * pred_expressions = pred->get_pred_expressions();
165         if (pred_expressions->getSize() == 0)   // unset predicates
166                 return false;
167
168         int thread_id = id_to_int(tid);
169         uint old_size = thrd_pruned_writes.size();
170         if (thrd_pruned_writes.size() <= (uint) thread_id) {
171                 uint new_size = thread_id + 1;
172                 thrd_pruned_writes.resize(new_size);
173                 for (uint i = old_size; i < new_size; i++)
174                         thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
175         }
176         SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
177         pruned_writes->clear(); // clear the old pruned_writes set
178
179         bool pruned = false;
180         uint index = 0;
181
182         ConcretePredicate * concrete_pred = pred->evaluate(inst_act_map, tid);
183         SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
184
185         while ( index < rf_set->size() ) {
186                 ModelAction * write_act = (*rf_set)[index];
187                 uint64_t write_val = write_act->get_write_value();
188                 bool satisfy_predicate = true;
189
190                 for (uint i = 0; i < concrete_exprs->size(); i++) {
191                         struct concrete_pred_expr concrete = (*concrete_exprs)[i];
192                         bool equality;
193
194                         switch (concrete.token) {
195                                 case NOPREDICATE:
196                                         return false;
197                                 case EQUALITY:
198                                         equality = (write_val == concrete.value);
199                                         if (equality != concrete.equality)
200                                                 satisfy_predicate = false;
201                                         break;
202                                 case NULLITY:
203                                         equality = ((void*)write_val == NULL);
204                                         if (equality != concrete.equality)
205                                                 satisfy_predicate = false;
206                                         break;
207                                 default:
208                                         model_print("unknown predicate token\n");
209                                         break;
210                         }
211
212                         if (!satisfy_predicate)
213                                 break;
214                 }
215
216                 if (!satisfy_predicate) {
217                         ASSERT(rf_set != NULL);
218                         (*rf_set)[index] = rf_set->back();
219                         rf_set->pop_back();
220                         pruned_writes->push_back(write_act);
221                         pruned = true;
222                 } else
223                         index++;
224         }
225
226         delete concrete_pred;
227
228         return pruned;
229 }
230
231 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate. 
232  *
233  * @param thread A thread whose last action is a read
234  */
235 void NewFuzzer::conditional_sleep(Thread * thread)
236 {
237         int index = paused_thread_list.size();
238
239         model->getScheduler()->add_sleep(thread);
240         paused_thread_list.push_back(thread);
241         paused_thread_table.put(thread, index); // Update table
242
243         /* Add the waiting condition to ModelHistory */
244         ModelAction * read = thread->get_pending();
245         thread_id_t tid = thread->get_id();
246         FuncNode * func_node = history->get_curr_func_node(tid);
247         inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
248
249         Predicate * selected_branch = get_selected_child_branch(tid);
250         ConcretePredicate * concrete = selected_branch->evaluate(inst_act_map, tid);
251         concrete->set_location(read->get_location());
252
253         history->add_waiting_write(concrete);
254         /* history->add_waiting_thread is already called in find_threads */
255 }
256
257 bool NewFuzzer::has_paused_threads()
258 {
259         return paused_thread_list.size() != 0;
260 }
261
262 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
263 {
264         if (numthreads == 0 && has_paused_threads()) {
265                 wake_up_paused_threads(threadlist, &numthreads);
266                 //model_print("list size: %d, active t id: %d\n", numthreads, threadlist[0]);
267         }
268
269         int random_index = random() % numthreads;
270         int thread = threadlist[random_index];
271         thread_id_t curr_tid = int_to_id(thread);
272         return execution->get_thread(curr_tid);
273 }
274
275 /* Force waking up one of threads paused by Fuzzer, because otherwise
276  * the Fuzzer is not making progress
277  */
278 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
279 {
280         int random_index = random() % paused_thread_list.size();
281         Thread * thread = paused_thread_list[random_index];
282         model->getScheduler()->remove_sleep(thread);
283
284         Thread * last_thread = paused_thread_list.back();
285         paused_thread_list[random_index] = last_thread;
286         paused_thread_list.pop_back();
287         paused_thread_table.put(last_thread, random_index);     // Update table
288         paused_thread_table.remove(thread);
289
290         thread_id_t tid = thread->get_id();
291         history->remove_waiting_write(tid);
292         history->remove_waiting_thread(tid);
293
294         model_print("thread %d is woken up\n", tid);
295         threadlist[*numthreads] = tid;
296         (*numthreads)++;
297 }
298
299 /* Wake up conditional sleeping threads if the desired write is available */
300 void NewFuzzer::notify_paused_thread(Thread * thread)
301 {
302         ASSERT(paused_thread_table.contains(thread));
303
304         int index = paused_thread_table.get(thread);
305         model->getScheduler()->remove_sleep(thread);
306
307         Thread * last_thread = paused_thread_list.back();
308         paused_thread_list[index] = last_thread;
309         paused_thread_list.pop_back();
310         paused_thread_table.put(last_thread, index);    // Update table
311         paused_thread_table.remove(thread);
312
313         thread_id_t tid = thread->get_id();
314         history->remove_waiting_write(tid);
315         history->remove_waiting_thread(tid);
316 }
317
318 /* Find threads that may write values that the pending read action is waiting for
319  * @return True if any thread is found
320  */
321 bool NewFuzzer::find_threads(ModelAction * pending_read)
322 {
323         ASSERT(pending_read->is_read());
324
325         void * location = pending_read->get_location();
326         thread_id_t self_id = pending_read->get_tid();
327         bool finds_waiting_for = false;
328
329         SnapVector<FuncNode *> * func_node_list = history->getWrFuncNodes(location);
330         for (uint i = 0; i < func_node_list->size(); i++) {
331                 FuncNode * target_node = (*func_node_list)[i];
332                 for (uint i = 1; i < execution->get_num_threads(); i++) {
333                         thread_id_t tid = int_to_id(i);
334                         if (tid == self_id)
335                                 continue;
336
337                         FuncNode * node = history->get_curr_func_node(tid);
338                         /* It is possible that thread tid is not in any FuncNode */
339                         if (node == NULL)
340                                 continue;
341
342                         int distance = node->compute_distance(target_node);
343                         if (distance != -1) {
344                                 history->add_waiting_thread(self_id, tid, target_node, distance);
345                                 finds_waiting_for = true;
346                                 model_print("thread: %d; distance from node %d to node %d: %d\n", tid, node->get_func_id(), target_node->get_func_id(), distance);
347                         }
348                 }
349         }
350
351         return finds_waiting_for;
352 }
353
354 bool NewFuzzer::shouldWait(const ModelAction * act)
355 {
356         return random() & 1;
357 }