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