2 #include "threads-model.h"
8 #include "concretepredicate.h"
13 #include "execution.h"
15 NewFuzzer::NewFuzzer() :
17 thrd_last_func_inst(),
18 thrd_selected_child_branch(),
21 paused_thread_table(128),
26 * @brief Register the ModelHistory and ModelExecution engine
28 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
30 this->history = history;
31 this->execution = execution;
34 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
36 // return random() % rf_set->size();
38 thread_id_t tid = read->get_tid();
39 int thread_id = id_to_int(tid);
41 if (thrd_last_read_act.size() <= (uint) thread_id) {
42 thrd_last_read_act.resize(thread_id + 1);
43 thrd_last_func_inst.resize(thread_id + 1);
46 // A new read action is encountered, select a random child branch of current predicate
47 if (read != thrd_last_read_act[thread_id]) {
48 FuncNode * func_node = history->get_curr_func_node(tid);
49 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
50 FuncInst * read_inst = func_node->get_inst(read);
51 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
53 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
54 prune_writes(tid, selected_branch, rf_set, inst_act_map);
56 if (!failed_predicates.isEmpty())
57 failed_predicates.reset();
59 thrd_last_read_act[thread_id] = read;
60 thrd_last_func_inst[thread_id] = read_inst;
63 // No write satisfies the selected predicate, so pause this thread.
64 while ( rf_set->size() == 0 ) {
65 Thread * read_thread = execution->get_thread(tid);
66 //model_print("the %d read action of thread %d at %p is unsuccessful\n", read->get_seq_number(), read_thread->get_id(), read->get_location());
68 if (find_threads(read)) {
69 // reset thread pending action and revert sequence numbers
70 read_thread->set_pending(read);
71 read->reset_seq_number();
72 execution->restore_last_seq_num();
74 conditional_sleep(read_thread);
75 // Returning -1 stops the while loop of ModelExecution::process_read
78 Predicate * selected_branch = get_selected_child_branch(tid);
79 selected_branch->incr_fail_count();
80 failed_predicates.put(selected_branch, true);
82 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
83 for (uint i = 0; i < pruned_writes->size(); i++) {
84 rf_set->push_back( (*pruned_writes)[i] );
87 // Reselect a predicate and prune writes
88 Predicate * curr_pred = selected_branch->get_parent();
89 FuncInst * read_inst = thrd_last_func_inst[thread_id];
90 selected_branch = selectBranch(tid, curr_pred, read_inst);
92 FuncNode * func_node = history->get_curr_func_node(tid);
93 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
94 prune_writes(tid, selected_branch, rf_set, inst_act_map);
96 ASSERT(selected_branch);
100 ASSERT(rf_set->size() != 0);
101 int random_index = random() % rf_set->size();
106 /* Select a random branch from the children of curr_pred
107 * @return The selected branch
109 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
111 int thread_id = id_to_int(tid);
112 if ( thrd_selected_child_branch.size() <= (uint) thread_id)
113 thrd_selected_child_branch.resize(thread_id + 1);
115 if (curr_pred == NULL || read_inst == NULL) {
116 thrd_selected_child_branch[thread_id] = NULL;
120 ModelVector<Predicate *> * children = curr_pred->get_children();
121 SnapVector<Predicate *> branches;
122 uint32_t numerator = 1;
124 for (uint i = 0; i < children->size(); i++) {
125 Predicate * child = (*children)[i];
126 if (child->get_func_inst() == read_inst && !failed_predicates.contains(child)) {
127 branches.push_back(child);
129 // max of (exploration counts + 1)
130 if (child->get_expl_count() + 1 > numerator)
131 numerator = child->get_expl_count() + 1;
135 // predicate children have not been generated
136 if (branches.size() == 0) {
137 thrd_selected_child_branch[thread_id] = NULL;
141 // randomly select a branch
142 // int random_index = random() % branches.size();
143 // Predicate * random_branch = branches[ random_index ];
145 int index = choose_index(&branches, numerator);
146 Predicate * random_branch = branches[ index ];
147 thrd_selected_child_branch[thread_id] = random_branch;
149 return random_branch;
153 * @brief Select a branch from the given predicate branches based
154 * on their exploration counts.
156 * Let b_1, ..., b_n be branches with exploration counts c_1, ..., c_n
157 * M := max(c_1, ..., c_n) + 1
158 * Factor f_i := M / (c_i + 1)
159 * The probability p_i that branch b_i is selected:
160 * p_i := f_i / (f_1 + ... + f_n)
161 * = \fraction{ 1/(c_i + 1) }{ 1/(c_1 + 1) + ... + 1/(c_n + 1) }
163 * Note: (1) c_i + 1 is used because counts may be 0.
164 * (2) The numerator of f_i is chosen to reduce the effect of underflow
166 * @param numerator is M defined above
168 int NewFuzzer::choose_index(SnapVector<Predicate *> * branches, uint32_t numerator)
170 if (branches->size() == 1)
173 double total_factor = 0;
174 SnapVector<double> factors = SnapVector<double>( branches->size() + 1 );
175 for (uint i = 0; i < branches->size(); i++) {
176 Predicate * branch = (*branches)[i];
177 double factor = (double) numerator / (branch->get_expl_count() + 2 * branch->get_fail_count() + 1);
178 total_factor += factor;
179 factors.push_back(factor);
182 double prob = (double) random() / RAND_MAX;
186 for (uint i = 0; i < factors.size(); i++) {
188 prob_sum += (double) (factors[i] / total_factor);
189 if (prob_sum > prob) {
197 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
199 int thread_id = id_to_int(tid);
200 if (thrd_selected_child_branch.size() <= (uint) thread_id)
203 return thrd_selected_child_branch[thread_id];
206 /* Remove writes from the rf_set that do not satisfie the selected predicate,
207 * and store them in thrd_pruned_writes
209 * @return true if rf_set is pruned
211 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
212 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
217 PredExprSet * pred_expressions = pred->get_pred_expressions();
218 if (pred_expressions->getSize() == 0) // unset predicates
221 int thread_id = id_to_int(tid);
222 uint old_size = thrd_pruned_writes.size();
223 if (thrd_pruned_writes.size() <= (uint) thread_id) {
224 uint new_size = thread_id + 1;
225 thrd_pruned_writes.resize(new_size);
226 for (uint i = old_size; i < new_size; i++)
227 thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
229 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
230 pruned_writes->clear(); // clear the old pruned_writes set
235 while ( index < rf_set->size() ) {
236 ModelAction * write_act = (*rf_set)[index];
237 uint64_t write_val = write_act->get_write_value();
238 bool satisfy_predicate = true;
240 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
241 while (pred_expr_it->hasNext()) {
242 struct pred_expr * expression = pred_expr_it->next();
245 switch (expression->token) {
249 FuncInst * to_be_compared;
250 ModelAction * last_act;
253 to_be_compared = expression->func_inst;
254 last_act = inst_act_map->get(to_be_compared);
255 last_read = last_act->get_reads_from_value();
257 equality = (write_val == last_read);
258 if (equality != expression->value)
259 satisfy_predicate = false;
262 equality = ((void*)write_val == NULL);
263 if (equality != expression->value)
264 satisfy_predicate = false;
267 model_print("unknown predicate token\n");
271 if (!satisfy_predicate)
275 if (!satisfy_predicate) {
276 ASSERT(rf_set != NULL);
277 (*rf_set)[index] = rf_set->back();
279 pruned_writes->push_back(write_act);
288 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate.
290 * @param thread A thread whose last action is a read
292 void NewFuzzer::conditional_sleep(Thread * thread)
294 int index = paused_thread_list.size();
296 model->getScheduler()->add_sleep(thread);
297 paused_thread_list.push_back(thread);
298 paused_thread_table.put(thread, index); // Update table
300 /* Add the waiting condition to ModelHistory */
301 ModelAction * read = thread->get_pending();
302 thread_id_t tid = thread->get_id();
303 FuncNode * func_node = history->get_curr_func_node(tid);
304 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
306 Predicate * selected_branch = get_selected_child_branch(tid);
307 ConcretePredicate * concrete = selected_branch->evaluate(inst_act_map, tid);
308 concrete->set_location(read->get_location());
310 history->add_waiting_write(concrete);
311 /* history->add_waiting_thread is already called in find_threads */
314 bool NewFuzzer::has_paused_threads()
316 return paused_thread_list.size() != 0;
319 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
321 if (numthreads == 0 && has_paused_threads()) {
322 wake_up_paused_threads(threadlist, &numthreads);
323 //model_print("list size: %d, active t id: %d\n", numthreads, threadlist[0]);
326 int random_index = random() % numthreads;
327 int thread = threadlist[random_index];
328 thread_id_t curr_tid = int_to_id(thread);
329 return execution->get_thread(curr_tid);
332 /* Force waking up one of threads paused by Fuzzer, because otherwise
333 * the Fuzzer is not making progress
335 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
337 int random_index = random() % paused_thread_list.size();
338 Thread * thread = paused_thread_list[random_index];
339 model->getScheduler()->remove_sleep(thread);
341 Thread * last_thread = paused_thread_list.back();
342 paused_thread_list[random_index] = last_thread;
343 paused_thread_list.pop_back();
344 paused_thread_table.put(last_thread, random_index); // Update table
345 paused_thread_table.remove(thread);
347 thread_id_t tid = thread->get_id();
348 history->remove_waiting_write(tid);
349 history->remove_waiting_thread(tid);
351 //model_print("thread %d is woken up\n", tid);
352 threadlist[*numthreads] = tid;
355 Predicate * selected_branch = get_selected_child_branch(tid);
356 selected_branch->incr_fail_count();
357 model_print("thread %d is woken up\n", tid);
360 /* Wake up conditional sleeping threads if the desired write is available */
361 void NewFuzzer::notify_paused_thread(Thread * thread)
363 ASSERT(paused_thread_table.contains(thread));
365 int index = paused_thread_table.get(thread);
366 model->getScheduler()->remove_sleep(thread);
368 Thread * last_thread = paused_thread_list.back();
369 paused_thread_list[index] = last_thread;
370 paused_thread_list.pop_back();
371 paused_thread_table.put(last_thread, index); // Update table
372 paused_thread_table.remove(thread);
374 thread_id_t tid = thread->get_id();
375 history->remove_waiting_write(tid);
376 history->remove_waiting_thread(tid);
378 model_print("** thread %d is woken up\n", tid);
381 /* Find threads that may write values that the pending read action is waiting for
382 * @return True if any thread is found
384 bool NewFuzzer::find_threads(ModelAction * pending_read)
386 ASSERT(pending_read->is_read());
388 void * location = pending_read->get_location();
389 thread_id_t self_id = pending_read->get_tid();
390 bool finds_waiting_for = false;
392 SnapVector<FuncNode *> * func_node_list = history->getWrFuncNodes(location);
393 for (uint i = 0; i < func_node_list->size(); i++) {
394 FuncNode * target_node = (*func_node_list)[i];
395 for (uint i = 1; i < execution->get_num_threads(); i++) {
396 thread_id_t tid = int_to_id(i);
400 FuncNode * node = history->get_curr_func_node(tid);
401 /* It is possible that thread tid is not in any FuncNode */
405 int distance = node->compute_distance(target_node);
406 if (distance != -1) {
407 history->add_waiting_thread(self_id, tid, target_node, distance);
408 finds_waiting_for = true;
409 //model_print("thread: %d; distance from node %d to node %d: %d\n", tid, node->get_func_id(), target_node->get_func_id(), distance);
414 return finds_waiting_for;
417 bool NewFuzzer::shouldWait(const ModelAction * act)