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