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