Change the return type of Predicate::evaluate
[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 "history.h"
7 #include "funcnode.h"
8 #include "schedule.h"
9 #include "concretepredicate.h"
10
11 NewFuzzer::NewFuzzer() :
12         thrd_last_read_act(),
13         thrd_curr_pred(),
14         thrd_selected_child_branch(),
15         thrd_pruned_writes(),
16         paused_thread_set()
17 {}
18
19 /**
20  * @brief Register the ModelHistory and ModelExecution engine
21  */
22 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
23 {
24         this->history = history;
25         this->execution = execution;
26 }
27
28 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
29 {
30 //      return random() % rf_set->size();
31
32         thread_id_t tid = read->get_tid();
33         int thread_id = id_to_int(tid);
34
35         if (thrd_last_read_act.size() <= (uint) thread_id)
36                 thrd_last_read_act.resize(thread_id + 1);
37
38         // A new read action is encountered, select a random child branch of current predicate
39         if (read != thrd_last_read_act[thread_id]) {
40                 thrd_last_read_act[thread_id] = read;
41
42                 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
43                 uint32_t func_id = (*thrd_func_list)[thread_id].back();
44                 FuncNode * func_node = history->get_func_node(func_id);
45                 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
46                 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
47                 FuncInst * read_inst = func_node->get_inst(read);
48
49                 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
50                 prune_writes(tid, selected_branch, rf_set, inst_act_map);
51         }
52
53         // No write satisfies the selected predicate
54         if ( rf_set->size() == 0 ) {
55                 Thread * read_thread = execution->get_thread(tid);
56                 model_print("the %d read action of thread %d is unsuccessful\n", read->get_seq_number(), read_thread->get_id());
57
58                 // reset thread pending action and revert sequence numbers
59                 read_thread->set_pending(read);
60                 read->reset_seq_number();
61                 execution->restore_last_seq_num();
62
63                 conditional_sleep(read_thread);
64                 return -1;
65 /*
66                 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
67                 for (uint i = 0; i < pruned_writes->size(); i++)
68                         rf_set->push_back( (*pruned_writes)[i] );
69                 pruned_writes->clear();
70 */
71         }
72
73         ASSERT(rf_set->size() != 0);
74         int random_index = random() % rf_set->size();
75
76         return random_index;
77 }
78
79 /* Select a random branch from the children of curr_pred 
80  * @return The selected branch
81  */
82 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
83 {
84         int thread_id = id_to_int(tid);
85         if ( thrd_selected_child_branch.size() <= (uint) thread_id)
86                 thrd_selected_child_branch.resize(thread_id + 1);
87
88         if (curr_pred == NULL || read_inst == NULL) {
89                 thrd_selected_child_branch[thread_id] = NULL;
90                 return NULL;
91         }
92
93         ModelVector<Predicate *> * children = curr_pred->get_children();
94         SnapVector<Predicate *> branches;
95
96         for (uint i = 0; i < children->size(); i++) {
97                 Predicate * child = (*children)[i];
98                 if (child->get_func_inst() == read_inst)
99                         branches.push_back(child);
100         }
101
102         // predicate children have not been generated
103         if (branches.size() == 0) {
104                 thrd_selected_child_branch[thread_id] = NULL;
105                 return NULL;
106         }
107
108         // randomly select a branch
109         int random_index = random() % branches.size();
110         Predicate * random_branch = branches[ random_index ];
111         thrd_selected_child_branch[thread_id] = random_branch;
112
113         return random_branch;
114 }
115
116 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
117 {
118         int thread_id = id_to_int(tid);
119         if (thrd_selected_child_branch.size() <= (uint) thread_id)
120                 return NULL;
121
122         return thrd_selected_child_branch[thread_id];
123 }
124
125 /* Remove writes from the rf_set that do not satisfie the selected predicate, 
126  * and store them in thrd_pruned_writes
127  *
128  * @return true if rf_set is pruned
129  */
130 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
131         SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
132 {
133         if (pred == NULL)
134                 return false;
135
136         PredExprSet * pred_expressions = pred->get_pred_expressions();
137         if (pred_expressions->getSize() == 0)   // unset predicates
138                 return false;
139
140         int thread_id = id_to_int(tid);
141         uint old_size = thrd_pruned_writes.size();
142         if (thrd_pruned_writes.size() <= (uint) thread_id) {
143                 uint new_size = thread_id + 1;
144                 thrd_pruned_writes.resize(new_size);
145                 for (uint i = old_size; i < new_size; i++)
146                         thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
147         }
148         SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
149         pruned_writes->clear(); // clear the old pruned_writes set
150
151         bool pruned = false;
152         uint index = 0;
153
154         ConcretePredicate * concrete_pred = pred->evaluate(inst_act_map, tid);
155         SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
156
157         while ( index < rf_set->size() ) {
158                 ModelAction * write_act = (*rf_set)[index];
159                 uint64_t write_val = write_act->get_write_value();
160                 bool satisfy_predicate = true;
161
162                 for (uint i = 0; i < concrete_exprs->size(); i++) {
163                         struct concrete_pred_expr concrete = (*concrete_exprs)[i];
164                         bool equality;
165
166                         switch (concrete.token) {
167                                 case NOPREDICATE:
168                                         return false;
169                                 case EQUALITY:
170                                         equality = (write_val == concrete.value);
171                                         if (equality != concrete.equality)
172                                                 satisfy_predicate = false;
173                                         break;
174                                 case NULLITY:
175                                         equality = ((void*)write_val == NULL);
176                                         if (equality != concrete.equality)
177                                                 satisfy_predicate = false;
178                                         break;
179                                 default:
180                                         model_print("unknown predicate token\n");
181                                         break;
182                         }
183
184                         if (!satisfy_predicate)
185                                 break;
186                 }
187
188                 if (!satisfy_predicate) {
189                         ASSERT(rf_set != NULL);
190                         (*rf_set)[index] = rf_set->back();
191                         rf_set->pop_back();
192                         pruned_writes->push_back(write_act);
193                         pruned = true;
194                 } else
195                         index++;
196         }
197
198         return pruned;
199 }
200
201 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate. 
202  *
203  * @param thread A thread whose last action is a read
204  */
205 void NewFuzzer::conditional_sleep(Thread * thread)
206 {
207         model->getScheduler()->add_sleep(thread);
208         paused_thread_set.push_back(thread);
209 }
210
211 bool NewFuzzer::has_paused_threads()
212 {
213         return paused_thread_set.size() != 0;
214 }
215
216 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
217 {
218         if (numthreads == 0 && has_paused_threads()) {
219                 wake_up_paused_threads(threadlist, &numthreads);
220                 model_print("list size: %d\n", numthreads);
221                 model_print("active t id: %d\n", threadlist[0]);
222         }
223
224         int random_index = random() % numthreads;
225         int thread = threadlist[random_index];
226         thread_id_t curr_tid = int_to_id(thread);
227         return model->get_thread(curr_tid);
228 }
229
230 /* Force waking up one of threads paused by Fuzzer */
231 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
232 {
233         int random_index = random() % paused_thread_set.size();
234         Thread * thread = paused_thread_set[random_index];
235         model->getScheduler()->remove_sleep(thread);
236
237         paused_thread_set[random_index] = paused_thread_set.back();
238         paused_thread_set.pop_back();
239
240         model_print("thread %d is woken up\n", thread->get_id());
241         threadlist[*numthreads] = thread->get_id();
242         (*numthreads)++;
243 }
244
245 /* Notify one of conditional sleeping threads if the desired write is available */
246 bool NewFuzzer::notify_conditional_sleep(Thread * thread)
247 {
248         
249 }
250
251 bool NewFuzzer::shouldWait(const ModelAction * act)
252 {
253         return random() & 1;
254 }