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