When an atomic read action cannot read from a desired write, make this thread sleep...
[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                 read_thread->set_pending(read);
56                 read->reset_seq_number();       // revert some operations
57                 execution->restore_last_seq_num();
58                 
59                 conditional_sleep(read_thread);
60                 return -1;
61 /*
62                 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
63                 for (uint i = 0; i < pruned_writes->size(); i++)
64                         rf_set->push_back( (*pruned_writes)[i] );
65                 pruned_writes->clear();
66 */
67         }
68
69         ASSERT(rf_set->size() != 0);
70         int random_index = random() % rf_set->size();
71
72         return random_index;
73 }
74
75 /* Select a random branch from the children of curr_pred 
76  * @return The selected branch
77  */
78 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
79 {
80         int thread_id = id_to_int(tid);
81         if ( thrd_selected_child_branch.size() <= (uint) thread_id)
82                 thrd_selected_child_branch.resize(thread_id + 1);
83
84         if (curr_pred == NULL || read_inst == NULL) {
85                 thrd_selected_child_branch[thread_id] = NULL;
86                 return NULL;
87         }
88
89         ModelVector<Predicate *> * children = curr_pred->get_children();
90         SnapVector<Predicate *> branches;
91
92         for (uint i = 0; i < children->size(); i++) {
93                 Predicate * child = (*children)[i];
94                 if (child->get_func_inst() == read_inst)
95                         branches.push_back(child);
96         }
97
98         // predicate children have not been generated
99         if (branches.size() == 0) {
100                 thrd_selected_child_branch[thread_id] = NULL;
101                 return NULL;
102         }
103
104         // randomly select a branch
105         int random_index = random() % branches.size();
106         Predicate * random_branch = branches[ random_index ];
107         thrd_selected_child_branch[thread_id] = random_branch;
108
109         return random_branch;
110 }
111
112 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
113 {
114         int thread_id = id_to_int(tid);
115         if (thrd_selected_child_branch.size() <= (uint) thread_id)
116                 return NULL;
117
118         return thrd_selected_child_branch[thread_id];
119 }
120
121 /* Remove writes from the rf_set that do not satisfie the selected predicate, 
122  * and store them in thrd_pruned_writes
123  *
124  * @return true if rf_set is pruned
125  */
126 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
127         SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
128 {
129         if (pred == NULL)
130                 return false;
131
132         PredExprSet * pred_expressions = pred->get_pred_expressions();
133         if (pred_expressions->getSize() == 0)   // unset predicates
134                 return false;
135
136         int thread_id = id_to_int(tid);
137         uint old_size = thrd_pruned_writes.size();
138         if (thrd_pruned_writes.size() <= (uint) thread_id) {
139                 uint new_size = thread_id + 1;
140                 thrd_pruned_writes.resize(new_size);
141                 for (uint i = old_size; i < new_size; i++)
142                         thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
143         }
144         SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
145         pruned_writes->clear(); // clear the old pruned_writes set
146
147         bool pruned = false;
148         uint index = 0;
149         while ( index < rf_set->size() ) {
150                 ModelAction * write_act = (*rf_set)[index];
151                 bool satisfy_predicate = true;
152
153                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
154                 while (pred_expr_it->hasNext()) {
155                         struct pred_expr * expression = pred_expr_it->next();
156                         uint64_t write_val = write_act->get_write_value();
157                         bool equality;
158
159                         // No predicate, return false
160                         if (expression->token == NOPREDICATE)
161                                 return pruned;
162
163                         switch(expression->token) {
164                                 case EQUALITY:
165                                         FuncInst * to_be_compared;
166                                         ModelAction * last_act;
167                                         uint64_t last_read;
168
169                                         to_be_compared = expression->func_inst;
170                                         last_act = inst_act_map->get(to_be_compared);
171                                         last_read = last_act->get_reads_from_value();
172
173                                         equality = (write_val == last_read);
174                                         if (equality != expression->value)
175                                                 satisfy_predicate = false;
176                                         break;
177                                 case NULLITY:
178                                         equality = ((void*)write_val == NULL);
179                                         if (equality != expression->value)
180                                                 satisfy_predicate = false;
181                                         break;
182                                 default:
183                                         model_print("unknown predicate token\n");
184                                         break;
185                         }
186
187                         if (!satisfy_predicate)
188                                 break;
189                 }
190
191                 if (!satisfy_predicate) {
192                         ASSERT(rf_set != NULL);
193                         (*rf_set)[index] = rf_set->back();
194                         rf_set->pop_back();
195                         pruned_writes->push_back(write_act);
196                         pruned = true;
197                 } else
198                         index++;
199         }
200
201         return pruned;
202 }
203
204 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate. 
205  *
206  * @param thread A thread whose last action is a read
207  */
208 void NewFuzzer::conditional_sleep(Thread * thread)
209 {
210         model->getScheduler()->add_sleep(thread);
211         paused_thread_set.push_back(thread);
212 }
213
214 bool NewFuzzer::has_paused_threads()
215 {
216         return paused_thread_set.size() != 0;
217 }
218
219 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
220 {
221         if (numthreads == 0 && has_paused_threads()) {
222                 wake_up_paused_threads(threadlist, &numthreads);
223                 model_print("list size: %d\n", numthreads);
224                 model_print("active t id: %d\n", threadlist[0]);
225         }
226
227         int random_index = random() % numthreads;
228         int thread = threadlist[random_index];
229         thread_id_t curr_tid = int_to_id(thread);
230         return model->get_thread(curr_tid);
231 }
232
233 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
234 {
235         int random_index = random() % paused_thread_set.size();
236         Thread * thread = paused_thread_set[random_index];
237         model->getScheduler()->remove_sleep(thread);
238
239         paused_thread_set[random_index] = paused_thread_set.back();
240         paused_thread_set.pop_back();
241
242         model_print("thread %d is woken up\n", thread->get_id());
243         threadlist[*numthreads] = thread->get_id();
244         (*numthreads)++;
245 }