Reorganize codes
[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                 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
48                 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
49                 FuncInst * read_inst = func_node->get_inst(read);
50
51                 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
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 is unsuccessful\n", read->get_seq_number(), read_thread->get_id());
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                 return -1;
67 /*
68                 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
69                 for (uint i = 0; i < pruned_writes->size(); i++)
70                         rf_set->push_back( (*pruned_writes)[i] );
71                 pruned_writes->clear();
72 */
73         }
74
75         ASSERT(rf_set->size() != 0);
76         int random_index = random() % rf_set->size();
77
78         return random_index;
79 }
80
81 /* Select a random branch from the children of curr_pred 
82  * @return The selected branch
83  */
84 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
85 {
86         int thread_id = id_to_int(tid);
87         if ( thrd_selected_child_branch.size() <= (uint) thread_id)
88                 thrd_selected_child_branch.resize(thread_id + 1);
89
90         if (curr_pred == NULL || read_inst == NULL) {
91                 thrd_selected_child_branch[thread_id] = NULL;
92                 return NULL;
93         }
94
95         ModelVector<Predicate *> * children = curr_pred->get_children();
96         SnapVector<Predicate *> branches;
97
98         for (uint i = 0; i < children->size(); i++) {
99                 Predicate * child = (*children)[i];
100                 if (child->get_func_inst() == read_inst)
101                         branches.push_back(child);
102         }
103
104         // predicate children have not been generated
105         if (branches.size() == 0) {
106                 thrd_selected_child_branch[thread_id] = NULL;
107                 return NULL;
108         }
109
110         // randomly select a branch
111         int random_index = random() % branches.size();
112         Predicate * random_branch = branches[ random_index ];
113         thrd_selected_child_branch[thread_id] = random_branch;
114
115         return random_branch;
116 }
117
118 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
119 {
120         int thread_id = id_to_int(tid);
121         if (thrd_selected_child_branch.size() <= (uint) thread_id)
122                 return NULL;
123
124         return thrd_selected_child_branch[thread_id];
125 }
126
127 /* Remove writes from the rf_set that do not satisfie the selected predicate, 
128  * and store them in thrd_pruned_writes
129  *
130  * @return true if rf_set is pruned
131  */
132 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
133         SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
134 {
135         if (pred == NULL)
136                 return false;
137
138         PredExprSet * pred_expressions = pred->get_pred_expressions();
139         if (pred_expressions->getSize() == 0)   // unset predicates
140                 return false;
141
142         int thread_id = id_to_int(tid);
143         uint old_size = thrd_pruned_writes.size();
144         if (thrd_pruned_writes.size() <= (uint) thread_id) {
145                 uint new_size = thread_id + 1;
146                 thrd_pruned_writes.resize(new_size);
147                 for (uint i = old_size; i < new_size; i++)
148                         thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
149         }
150         SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
151         pruned_writes->clear(); // clear the old pruned_writes set
152
153         bool pruned = false;
154         uint index = 0;
155
156         ConcretePredicate * concrete_pred = pred->evaluate(inst_act_map, tid);
157         SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
158
159         while ( index < rf_set->size() ) {
160                 ModelAction * write_act = (*rf_set)[index];
161                 uint64_t write_val = write_act->get_write_value();
162                 bool satisfy_predicate = true;
163
164                 for (uint i = 0; i < concrete_exprs->size(); i++) {
165                         struct concrete_pred_expr concrete = (*concrete_exprs)[i];
166                         bool equality;
167
168                         switch (concrete.token) {
169                                 case NOPREDICATE:
170                                         return false;
171                                 case EQUALITY:
172                                         equality = (write_val == concrete.value);
173                                         if (equality != concrete.equality)
174                                                 satisfy_predicate = false;
175                                         break;
176                                 case NULLITY:
177                                         equality = ((void*)write_val == NULL);
178                                         if (equality != concrete.equality)
179                                                 satisfy_predicate = false;
180                                         break;
181                                 default:
182                                         model_print("unknown predicate token\n");
183                                         break;
184                         }
185
186                         if (!satisfy_predicate)
187                                 break;
188                 }
189
190                 if (!satisfy_predicate) {
191                         ASSERT(rf_set != NULL);
192                         (*rf_set)[index] = rf_set->back();
193                         rf_set->pop_back();
194                         pruned_writes->push_back(write_act);
195                         pruned = true;
196                 } else
197                         index++;
198         }
199
200         delete concrete_pred;
201
202         return pruned;
203 }
204
205 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate. 
206  *
207  * @param thread A thread whose last action is a read
208  */
209 void NewFuzzer::conditional_sleep(Thread * thread)
210 {
211         int index = paused_thread_set.size();
212
213         model->getScheduler()->add_sleep(thread);
214         paused_thread_set.push_back(thread);
215         paused_thread_table.put(thread, index); // Update table
216
217         /*  */
218         ModelAction * read = thread->get_pending();
219         thread_id_t tid = thread->get_id();
220         FuncNode * func_node = history->get_curr_func_node(tid);
221         inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
222
223         Predicate * selected_branch = get_selected_child_branch(tid);
224         ConcretePredicate * concrete = selected_branch->evaluate(inst_act_map, tid);
225         concrete->set_location(read->get_location());
226
227         history->add_waiting_write(concrete);
228 }
229
230 bool NewFuzzer::has_paused_threads()
231 {
232         return paused_thread_set.size() != 0;
233 }
234
235 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
236 {
237         if (numthreads == 0 && has_paused_threads()) {
238                 wake_up_paused_threads(threadlist, &numthreads);
239                 model_print("list size: %d\n", numthreads);
240                 model_print("active t id: %d\n", threadlist[0]);
241         }
242
243         int random_index = random() % numthreads;
244         int thread = threadlist[random_index];
245         thread_id_t curr_tid = int_to_id(thread);
246         return model->get_thread(curr_tid);
247 }
248
249 /* Force waking up one of threads paused by Fuzzer, because otherwise
250  * the Fuzzer is not making progress
251  */
252 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
253 {
254         int random_index = random() % paused_thread_set.size();
255         Thread * thread = paused_thread_set[random_index];
256         model->getScheduler()->remove_sleep(thread);
257
258         Thread * last_thread = paused_thread_set.back();
259         paused_thread_set[random_index] = last_thread;
260         paused_thread_set.pop_back();
261         paused_thread_table.put(last_thread, random_index);     // Update table
262         paused_thread_table.remove(thread);
263
264         thread_id_t tid = thread->get_id();
265         history->remove_waiting_write(tid);
266
267         model_print("thread %d is woken up\n", tid);
268         threadlist[*numthreads] = tid;
269         (*numthreads)++;
270 }
271
272 /* Wake up conditional sleeping threads if the desired write is available */
273 void NewFuzzer::notify_paused_thread(Thread * thread)
274 {
275         ASSERT(paused_thread_table.contains(thread));
276
277         int index = paused_thread_table.get(thread);
278         model->getScheduler()->remove_sleep(thread);
279
280         Thread * last_thread = paused_thread_set.back();
281         paused_thread_set[index] = last_thread;
282         paused_thread_set.pop_back();
283         paused_thread_table.put(last_thread, index);    // Update table
284         paused_thread_table.remove(thread);
285
286         thread_id_t tid = thread->get_id();
287         history->remove_waiting_write(tid);
288 }
289
290 bool NewFuzzer::shouldWait(const ModelAction * act)
291 {
292         return random() & 1;
293 }