2 #include "threads-model.h"
8 #include "concretepredicate.h"
10 NewFuzzer::NewFuzzer() :
13 thrd_selected_child_branch(),
19 * @brief Register the ModelHistory and ModelExecution engine
21 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
23 this->history = history;
24 this->execution = execution;
27 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
29 // return random() % rf_set->size();
31 thread_id_t tid = read->get_tid();
32 int thread_id = id_to_int(tid);
34 if (thrd_last_read_act.size() <= (uint) thread_id)
35 thrd_last_read_act.resize(thread_id + 1);
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;
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);
48 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
49 prune_writes(tid, selected_branch, rf_set, inst_act_map);
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());
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();
62 conditional_sleep(read_thread);
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();
72 ASSERT(rf_set->size() != 0);
73 int random_index = random() % rf_set->size();
78 /* Select a random branch from the children of curr_pred
79 * @return The selected branch
81 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
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);
87 if (curr_pred == NULL || read_inst == NULL) {
88 thrd_selected_child_branch[thread_id] = NULL;
92 ModelVector<Predicate *> * children = curr_pred->get_children();
93 SnapVector<Predicate *> branches;
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);
101 // predicate children have not been generated
102 if (branches.size() == 0) {
103 thrd_selected_child_branch[thread_id] = NULL;
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;
112 return random_branch;
115 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
117 int thread_id = id_to_int(tid);
118 if (thrd_selected_child_branch.size() <= (uint) thread_id)
121 return thrd_selected_child_branch[thread_id];
124 /* Remove writes from the rf_set that do not satisfie the selected predicate,
125 * and store them in thrd_pruned_writes
127 * @return true if rf_set is pruned
129 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
130 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
135 PredExprSet * pred_expressions = pred->get_pred_expressions();
136 if (pred_expressions->getSize() == 0) // unset predicates
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 *>();
147 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
148 pruned_writes->clear(); // clear the old pruned_writes set
152 SnapVector<struct concrete_pred_expr> concrete_exprs = pred->evaluate(inst_act_map);
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;
159 for (uint i = 0; i < concrete_exprs.size(); i++) {
160 struct concrete_pred_expr concrete = concrete_exprs[i];
163 switch (concrete.token) {
167 equality = (write_val == concrete.value);
168 if (equality != concrete.equality)
169 satisfy_predicate = false;
172 equality = ((void*)write_val == NULL);
173 if (equality != concrete.equality)
174 satisfy_predicate = false;
177 model_print("unknown predicate token\n");
181 if (!satisfy_predicate)
185 if (!satisfy_predicate) {
186 ASSERT(rf_set != NULL);
187 (*rf_set)[index] = rf_set->back();
189 pruned_writes->push_back(write_act);
198 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate.
200 * @param thread A thread whose last action is a read
202 void NewFuzzer::conditional_sleep(Thread * thread)
204 model->getScheduler()->add_sleep(thread);
205 paused_thread_set.push_back(thread);
208 bool NewFuzzer::has_paused_threads()
210 return paused_thread_set.size() != 0;
213 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
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]);
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);
227 /* Force waking up one of threads paused by Fuzzer */
228 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
230 int random_index = random() % paused_thread_set.size();
231 Thread * thread = paused_thread_set[random_index];
232 model->getScheduler()->remove_sleep(thread);
234 paused_thread_set[random_index] = paused_thread_set.back();
235 paused_thread_set.pop_back();
237 model_print("thread %d is woken up\n", thread->get_id());
238 threadlist[*numthreads] = thread->get_id();
242 /* Notify one of conditional sleeping threads if the desired write is available */
243 bool NewFuzzer::notify_conditional_sleep(Thread * thread)
248 bool NewFuzzer::shouldWait(const ModelAction * act)