Using a for loop to prune writes that violate modification order is wrong
[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 "concretepredicate.h"
8 #include "waitobj.h"
9
10 #include "model.h"
11 #include "schedule.h"
12 #include "execution.h"
13
14 NewFuzzer::NewFuzzer() :
15         thrd_last_read_act(),
16         thrd_last_func_inst(),
17         tmp_branches_storage(),
18         thrd_selected_child_branch(),
19         thrd_pruned_writes(),
20         paused_thread_list(),
21         paused_thread_table(128),
22         failed_predicates(32),
23         dist_info_vec()
24 {}
25
26 /**
27  * @brief Register the ModelHistory and ModelExecution engine
28  */
29 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
30 {
31         this->history = history;
32         this->execution = execution;
33 }
34
35 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
36 {
37 //      return random() % rf_set->size();
38
39         thread_id_t tid = read->get_tid();
40         int thread_id = id_to_int(tid);
41
42         if (thrd_last_read_act.size() <= (uint) thread_id) {
43                 thrd_last_read_act.resize(thread_id + 1);
44                 thrd_last_func_inst.resize(thread_id + 1);
45         }
46
47         // A new read action is encountered, select a random child branch of current predicate
48         if (read != thrd_last_read_act[thread_id]) {
49                 FuncNode * func_node = history->get_curr_func_node(tid);
50                 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
51                 FuncInst * read_inst = func_node->get_inst(read);
52                 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
53
54                 if (curr_pred != NULL)  {
55                         Predicate * selected_branch = NULL;
56
57                         if (check_store_visibility(curr_pred, read_inst, inst_act_map, rf_set))
58                                 selected_branch = selectBranch(tid, curr_pred, read_inst);
59                         else {
60                                 // no child of curr_pred matches read_inst, check back edges
61                                 PredSet * back_edges = curr_pred->get_backedges();
62                                 PredSetIter * it = back_edges->iterator();
63
64                                 while (it->hasNext()) {
65                                         curr_pred = it->next();
66                                         if (check_store_visibility(curr_pred, read_inst, inst_act_map, rf_set)) {
67                                                 selected_branch = selectBranch(tid, curr_pred, read_inst);
68                                                 break;
69                                         }
70                                 }
71                         }
72
73                         prune_writes(tid, selected_branch, rf_set, inst_act_map);
74                 }
75
76                 if (!failed_predicates.isEmpty())
77                         failed_predicates.reset();
78
79                 thrd_last_read_act[thread_id] = read;
80                 thrd_last_func_inst[thread_id] = read_inst;
81
82                 ASSERT(rf_set->size() != 0);
83         }
84
85         // No write satisfies the selected predicate, so pause this thread.
86         while ( rf_set->size() == 0 ) {
87                 Predicate * selected_branch = get_selected_child_branch(tid);
88
89                 //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());
90
91                 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
92                 for (uint i = 0;i < pruned_writes->size();i++) {
93                         rf_set->push_back( (*pruned_writes)[i] );
94                 }
95
96                 // Reselect a predicate and prune writes
97                 Predicate * curr_pred = selected_branch->get_parent();
98                 FuncInst * read_inst = thrd_last_func_inst[thread_id];
99                 selected_branch = selectBranch(tid, curr_pred, read_inst);
100
101                 FuncNode * func_node = history->get_curr_func_node(tid);
102                 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
103                 prune_writes(tid, selected_branch, rf_set, inst_act_map);
104
105                 ASSERT(selected_branch);
106         }
107
108         ASSERT(rf_set->size() != 0);
109         int random_index = random() % rf_set->size();
110
111         return random_index;
112 }
113
114 /* For children of curr_pred that matches read_inst.
115  * If any store in rf_set satisfies the a child's predicate,
116  * increment the store visibility count for it.
117  *
118  * @return False if no child matches read_inst
119  */
120 bool NewFuzzer::check_store_visibility(Predicate * curr_pred, FuncInst * read_inst,
121 inst_act_map_t * inst_act_map, SnapVector<ModelAction *> * rf_set)
122 {
123         ASSERT(!rf_set->empty());
124         if (curr_pred == NULL || read_inst == NULL)
125                 return false;
126
127         ModelVector<Predicate *> * children = curr_pred->get_children();
128         bool any_child_match = false;
129
130         /* Iterate over all predicate children */
131         for (uint i = 0;i < children->size();i++) {
132                 Predicate * branch = (*children)[i];
133
134                 /* The children predicates may have different FuncInsts */
135                 if (branch->get_func_inst() == read_inst) {
136                         PredExprSet * pred_expressions = branch->get_pred_expressions();
137                         any_child_match = true;
138
139                         /* Do not check unset predicates */
140                         if (pred_expressions->isEmpty())
141                                 continue;
142
143                         branch->incr_total_checking_count();
144
145                         /* Iterate over all write actions */
146                         for (uint j = 0;j < rf_set->size();j++) {
147                                 ModelAction * write_act = (*rf_set)[j];
148                                 uint64_t write_val = write_act->get_write_value();
149                                 bool dummy = true;
150                                 bool satisfy_predicate = check_predicate_expressions(pred_expressions, inst_act_map, write_val, &dummy);
151
152                                 /* If one write value satisfies the predicate, go to check the next predicate */
153                                 if (satisfy_predicate) {
154                                         branch->incr_store_visible_count();
155                                         break;
156                                 }
157                         }
158                 }
159         }
160
161         return any_child_match;
162 }
163
164
165 /* Select a random branch from the children of curr_pred
166  * @return The selected branch
167  */
168 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
169 {
170         int thread_id = id_to_int(tid);
171         if ( thrd_selected_child_branch.size() <= (uint) thread_id)
172                 thrd_selected_child_branch.resize(thread_id + 1);
173
174         if (curr_pred == NULL || read_inst == NULL) {
175                 thrd_selected_child_branch[thread_id] = NULL;
176                 return NULL;
177         }
178
179         ModelVector<Predicate *> * children = curr_pred->get_children();
180         tmp_branches_storage.clear();
181
182         for (uint i = 0; i < children->size(); i++) {
183                 Predicate * child = (*children)[i];
184                 if (child->get_func_inst() == read_inst && !failed_predicates.contains(child)) {
185                         tmp_branches_storage.push_back(child);
186                 }
187         }
188
189         // predicate children have not been generated
190         if (tmp_branches_storage.size() == 0) {
191                 thrd_selected_child_branch[thread_id] = NULL;
192                 return NULL;
193         }
194
195         int index = choose_index(&tmp_branches_storage, 0);
196         Predicate * random_branch = tmp_branches_storage[ index ];
197         thrd_selected_child_branch[thread_id] = random_branch;
198
199         return random_branch;
200 }
201
202 /**
203  * @brief Select a branch from the given predicate branch
204  */
205 int NewFuzzer::choose_index(SnapVector<Predicate *> * branches, uint32_t numerator)
206 {
207         return random() % branches->size();
208 }
209
210 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
211 {
212         int thread_id = id_to_int(tid);
213         if (thrd_selected_child_branch.size() <= (uint) thread_id)
214                 return NULL;
215
216         return thrd_selected_child_branch[thread_id];
217 }
218
219 /* Remove writes from the rf_set that do not satisfie the selected predicate,
220  * and store them in thrd_pruned_writes
221  *
222  * @return true if rf_set is pruned
223  */
224 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
225 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
226 {
227         if (pred == NULL)
228                 return false;
229
230         PredExprSet * pred_expressions = pred->get_pred_expressions();
231         if (pred_expressions->getSize() == 0)   // unset predicates
232                 return false;
233
234         int thread_id = id_to_int(tid);
235         uint old_size = thrd_pruned_writes.size();
236         if (thrd_pruned_writes.size() <= (uint) thread_id) {
237                 uint new_size = thread_id + 1;
238                 thrd_pruned_writes.resize(new_size);
239                 for (uint i = old_size;i < new_size;i++)
240                         thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
241         }
242         SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
243         pruned_writes->clear(); // clear the old pruned_writes set
244
245         bool pruned = false;
246         uint index = 0;
247
248         while ( index < rf_set->size() ) {
249                 ModelAction * write_act = (*rf_set)[index];
250                 uint64_t write_val = write_act->get_write_value();
251                 bool no_predicate = false;
252                 bool satisfy_predicate = check_predicate_expressions(pred_expressions, inst_act_map, write_val, &no_predicate);
253
254                 if (no_predicate)
255                         return false;
256
257                 if (!satisfy_predicate) {
258                         ASSERT(rf_set != NULL);
259                         (*rf_set)[index] = rf_set->back();
260                         rf_set->pop_back();
261                         pruned_writes->push_back(write_act);
262                         pruned = true;
263                 } else
264                         index++;
265         }
266
267         return pruned;
268 }
269
270 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate.
271  *
272  * @param thread A thread whose last action is a read
273  */
274 void NewFuzzer::conditional_sleep(Thread * thread)
275 {
276         int index = paused_thread_list.size();
277
278         model->getScheduler()->add_sleep(thread);
279         paused_thread_list.push_back(thread);
280         paused_thread_table.put(thread, index); // Update table
281
282         /* Add the waiting condition to ModelHistory */
283         ModelAction * read = thread->get_pending();
284         thread_id_t tid = thread->get_id();
285         FuncNode * func_node = history->get_curr_func_node(tid);
286         inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
287
288         Predicate * selected_branch = get_selected_child_branch(tid);
289         ConcretePredicate * concrete = selected_branch->evaluate(inst_act_map, tid);
290         concrete->set_location(read->get_location());
291
292         history->add_waiting_write(concrete);
293         /* history->add_waiting_thread is already called in find_threads */
294 }
295
296 bool NewFuzzer::has_paused_threads()
297 {
298         return paused_thread_list.size() != 0;
299 }
300
301 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
302 {
303         if (numthreads == 0 && has_paused_threads()) {
304                 wake_up_paused_threads(threadlist, &numthreads);
305                 //model_print("list size: %d, active t id: %d\n", numthreads, threadlist[0]);
306         }
307
308         int random_index = random() % numthreads;
309         int thread = threadlist[random_index];
310         thread_id_t curr_tid = int_to_id(thread);
311         return execution->get_thread(curr_tid);
312 }
313
314 /* Force waking up one of threads paused by Fuzzer, because otherwise
315  * the Fuzzer is not making progress
316  */
317 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
318 {
319         int random_index = random() % paused_thread_list.size();
320         Thread * thread = paused_thread_list[random_index];
321         model->getScheduler()->remove_sleep(thread);
322
323         Thread * last_thread = paused_thread_list.back();
324         paused_thread_list[random_index] = last_thread;
325         paused_thread_list.pop_back();
326         paused_thread_table.put(last_thread, random_index);     // Update table
327         paused_thread_table.remove(thread);
328
329         thread_id_t tid = thread->get_id();
330         history->remove_waiting_write(tid);
331         history->remove_waiting_thread(tid);
332
333         threadlist[*numthreads] = tid;
334         (*numthreads)++;
335
336 /*--
337         Predicate * selected_branch = get_selected_child_branch(tid);
338         update_predicate_score(selected_branch, SLEEP_FAIL_TYPE3);
339  */
340
341         model_print("thread %d is woken up\n", tid);
342 }
343
344 /* Wake up conditional sleeping threads if the desired write is available */
345 void NewFuzzer::notify_paused_thread(Thread * thread)
346 {
347         ASSERT(paused_thread_table.contains(thread));
348
349         int index = paused_thread_table.get(thread);
350         model->getScheduler()->remove_sleep(thread);
351
352         Thread * last_thread = paused_thread_list.back();
353         paused_thread_list[index] = last_thread;
354         paused_thread_list.pop_back();
355         paused_thread_table.put(last_thread, index);    // Update table
356         paused_thread_table.remove(thread);
357
358         thread_id_t tid = thread->get_id();
359         history->remove_waiting_write(tid);
360         history->remove_waiting_thread(tid);
361
362 /*--
363         Predicate * selected_branch = get_selected_child_branch(tid);
364         update_predicate_score(selected_branch, SLEEP_SUCCESS);
365  */
366
367         model_print("** thread %d is woken up\n", tid);
368 }
369
370 /* Find threads that may write values that the pending read action is waiting for.
371  * Side effect: waiting thread related info are stored in dist_info_vec
372  *
373  * @return True if any thread is found
374  */
375 bool NewFuzzer::find_threads(ModelAction * pending_read)
376 {
377         ASSERT(pending_read->is_read());
378
379         void * location = pending_read->get_location();
380         thread_id_t self_id = pending_read->get_tid();
381         bool finds_waiting_for = false;
382
383         SnapVector<FuncNode *> * func_node_list = history->getWrFuncNodes(location);
384         for (uint i = 0;i < func_node_list->size();i++) {
385                 FuncNode * target_node = (*func_node_list)[i];
386                 for (uint i = 1;i < execution->get_num_threads();i++) {
387                         thread_id_t tid = int_to_id(i);
388                         if (tid == self_id)
389                                 continue;
390
391                         FuncNode * node = history->get_curr_func_node(tid);
392                         /* It is possible that thread tid is not in any FuncNode */
393                         if (node == NULL)
394                                 continue;
395
396                         int distance = node->compute_distance(target_node);
397                         if (distance != -1) {
398                                 finds_waiting_for = true;
399                                 //model_print("thread: %d; distance from node %d to node %d: %d\n", tid, node->get_func_id(), target_node->get_func_id(), distance);
400
401                                 dist_info_vec.push_back(node_dist_info(tid, target_node, distance));
402                         }
403                 }
404         }
405
406         return finds_waiting_for;
407 }
408
409 bool NewFuzzer::check_predicate_expressions(PredExprSet * pred_expressions,
410 inst_act_map_t * inst_act_map, uint64_t write_val, bool * no_predicate)
411 {
412         bool satisfy_predicate = true;
413
414         PredExprSetIter * pred_expr_it = pred_expressions->iterator();
415         while (pred_expr_it->hasNext()) {
416                 struct pred_expr * expression = pred_expr_it->next();
417                 bool equality;
418
419                 switch (expression->token) {
420                         case NOPREDICATE:
421                                 *no_predicate = true;
422                                 break;
423                         case EQUALITY:
424                                 FuncInst * to_be_compared;
425                                 ModelAction * last_act;
426                                 uint64_t last_read;
427
428                                 to_be_compared = expression->func_inst;
429                                 last_act = inst_act_map->get(to_be_compared);
430                                 last_read = last_act->get_reads_from_value();
431
432                                 equality = (write_val == last_read);
433                                 if (equality != expression->value)
434                                         satisfy_predicate = false;
435                                 break;
436                         case NULLITY:
437                                 // TODO: implement likely to be null
438                                 equality = ((void*) (write_val & 0xffffffff) == NULL);
439                                 if (equality != expression->value)
440                                         satisfy_predicate = false;
441                                 break;
442                         default:
443                                 model_print("unknown predicate token\n");
444                                 break;
445                 }
446
447                 if (!satisfy_predicate)
448                         break;
449         }
450
451         return satisfy_predicate;
452 }
453
454 bool NewFuzzer::shouldWait(const ModelAction * act)
455 {
456         return random() & 1;
457 }