2 #include "threads-model.h"
8 #include "concretepredicate.h"
12 #include "execution.h"
14 NewFuzzer::NewFuzzer() :
17 thrd_selected_child_branch(),
20 paused_thread_table(128)
24 * @brief Register the ModelHistory and ModelExecution engine
26 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
28 this->history = history;
29 this->execution = execution;
32 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
34 // return random() % rf_set->size();
36 thread_id_t tid = read->get_tid();
37 int thread_id = id_to_int(tid);
39 if (thrd_last_read_act.size() <= (uint) thread_id)
40 thrd_last_read_act.resize(thread_id + 1);
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;
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);
51 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
52 prune_writes(tid, selected_branch, rf_set, inst_act_map);
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());
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();
65 conditional_sleep(read_thread);
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();
75 ASSERT(rf_set->size() != 0);
76 int random_index = random() % rf_set->size();
81 /* Select a random branch from the children of curr_pred
82 * @return The selected branch
84 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
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);
90 if (curr_pred == NULL || read_inst == NULL) {
91 thrd_selected_child_branch[thread_id] = NULL;
95 ModelVector<Predicate *> * children = curr_pred->get_children();
96 SnapVector<Predicate *> branches;
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);
104 // predicate children have not been generated
105 if (branches.size() == 0) {
106 thrd_selected_child_branch[thread_id] = NULL;
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;
115 return random_branch;
118 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
120 int thread_id = id_to_int(tid);
121 if (thrd_selected_child_branch.size() <= (uint) thread_id)
124 return thrd_selected_child_branch[thread_id];
127 /* Remove writes from the rf_set that do not satisfie the selected predicate,
128 * and store them in thrd_pruned_writes
130 * @return true if rf_set is pruned
132 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
133 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
138 PredExprSet * pred_expressions = pred->get_pred_expressions();
139 if (pred_expressions->getSize() == 0) // unset predicates
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 *>();
150 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
151 pruned_writes->clear(); // clear the old pruned_writes set
156 ConcretePredicate * concrete_pred = pred->evaluate(inst_act_map, tid);
157 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
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;
164 for (uint i = 0; i < concrete_exprs->size(); i++) {
165 struct concrete_pred_expr concrete = (*concrete_exprs)[i];
168 switch (concrete.token) {
172 equality = (write_val == concrete.value);
173 if (equality != concrete.equality)
174 satisfy_predicate = false;
177 equality = ((void*)write_val == NULL);
178 if (equality != concrete.equality)
179 satisfy_predicate = false;
182 model_print("unknown predicate token\n");
186 if (!satisfy_predicate)
190 if (!satisfy_predicate) {
191 ASSERT(rf_set != NULL);
192 (*rf_set)[index] = rf_set->back();
194 pruned_writes->push_back(write_act);
200 delete concrete_pred;
205 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate.
207 * @param thread A thread whose last action is a read
209 void NewFuzzer::conditional_sleep(Thread * thread)
211 int index = paused_thread_set.size();
213 model->getScheduler()->add_sleep(thread);
214 paused_thread_set.push_back(thread);
215 paused_thread_table.put(thread, index); // Update table
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);
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());
227 history->add_waiting_write(concrete);
230 bool NewFuzzer::has_paused_threads()
232 return paused_thread_set.size() != 0;
235 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
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]);
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);
249 /* Force waking up one of threads paused by Fuzzer, because otherwise
250 * the Fuzzer is not making progress
252 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
254 int random_index = random() % paused_thread_set.size();
255 Thread * thread = paused_thread_set[random_index];
256 model->getScheduler()->remove_sleep(thread);
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);
264 thread_id_t tid = thread->get_id();
265 history->remove_waiting_write(tid);
267 model_print("thread %d is woken up\n", tid);
268 threadlist[*numthreads] = tid;
272 /* Wake up conditional sleeping threads if the desired write is available */
273 void NewFuzzer::notify_paused_thread(Thread * thread)
275 ASSERT(paused_thread_table.contains(thread));
277 int index = paused_thread_table.get(thread);
278 model->getScheduler()->remove_sleep(thread);
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);
286 thread_id_t tid = thread->get_id();
287 history->remove_waiting_write(tid);
290 bool NewFuzzer::shouldWait(const ModelAction * act)