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