2 #include "threads-model.h"
9 NewFuzzer::NewFuzzer() :
12 thrd_selected_child_branch(),
18 * @brief Register the ModelHistory and ModelExecution engine
20 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
22 this->history = history;
23 this->execution = execution;
26 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
28 thread_id_t tid = read->get_tid();
29 int thread_id = id_to_int(tid);
31 if (thrd_last_read_act.size() <= (uint) thread_id)
32 thrd_last_read_act.resize(thread_id + 1);
34 // A new read action is encountered, select a random child branch of current predicate
35 if (read != thrd_last_read_act[thread_id]) {
36 thrd_last_read_act[thread_id] = read;
38 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
39 uint32_t func_id = (*thrd_func_list)[thread_id].back();
40 FuncNode * func_node = history->get_func_node(func_id);
41 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
42 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
43 FuncInst * read_inst = func_node->get_inst(read);
45 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
46 prune_writes(tid, selected_branch, rf_set, inst_act_map);
49 // TODO: make this thread sleep if no write satisfies the chosen predicate
50 // if no read satisfies the selected predicate
51 if ( rf_set->size() == 0 ) {
52 Thread * read_thread = execution->get_thread(tid);
53 model_print("the %d read action of thread %d is unsuccessful\n", read->get_seq_number(), read_thread->get_id());
55 read_thread->set_pending(read);
56 read->reset_seq_number(); // revert some operations
57 execution->restore_last_seq_num();
59 conditional_sleep(read_thread);
62 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
63 for (uint i = 0; i < pruned_writes->size(); i++)
64 rf_set->push_back( (*pruned_writes)[i] );
65 pruned_writes->clear();
69 ASSERT(rf_set->size() != 0);
70 int random_index = random() % rf_set->size();
75 /* Select a random branch from the children of curr_pred
76 * @return The selected branch
78 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
80 int thread_id = id_to_int(tid);
81 if ( thrd_selected_child_branch.size() <= (uint) thread_id)
82 thrd_selected_child_branch.resize(thread_id + 1);
84 if (curr_pred == NULL || read_inst == NULL) {
85 thrd_selected_child_branch[thread_id] = NULL;
89 ModelVector<Predicate *> * children = curr_pred->get_children();
90 SnapVector<Predicate *> branches;
92 for (uint i = 0; i < children->size(); i++) {
93 Predicate * child = (*children)[i];
94 if (child->get_func_inst() == read_inst)
95 branches.push_back(child);
98 // predicate children have not been generated
99 if (branches.size() == 0) {
100 thrd_selected_child_branch[thread_id] = NULL;
104 // randomly select a branch
105 int random_index = random() % branches.size();
106 Predicate * random_branch = branches[ random_index ];
107 thrd_selected_child_branch[thread_id] = random_branch;
109 return random_branch;
112 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
114 int thread_id = id_to_int(tid);
115 if (thrd_selected_child_branch.size() <= (uint) thread_id)
118 return thrd_selected_child_branch[thread_id];
121 /* Remove writes from the rf_set that do not satisfie the selected predicate,
122 * and store them in thrd_pruned_writes
124 * @return true if rf_set is pruned
126 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
127 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
132 PredExprSet * pred_expressions = pred->get_pred_expressions();
133 if (pred_expressions->getSize() == 0) // unset predicates
136 int thread_id = id_to_int(tid);
137 uint old_size = thrd_pruned_writes.size();
138 if (thrd_pruned_writes.size() <= (uint) thread_id) {
139 uint new_size = thread_id + 1;
140 thrd_pruned_writes.resize(new_size);
141 for (uint i = old_size; i < new_size; i++)
142 thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
144 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
145 pruned_writes->clear(); // clear the old pruned_writes set
149 while ( index < rf_set->size() ) {
150 ModelAction * write_act = (*rf_set)[index];
151 bool satisfy_predicate = true;
153 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
154 while (pred_expr_it->hasNext()) {
155 struct pred_expr * expression = pred_expr_it->next();
156 uint64_t write_val = write_act->get_write_value();
159 // No predicate, return false
160 if (expression->token == NOPREDICATE)
163 switch(expression->token) {
165 FuncInst * to_be_compared;
166 ModelAction * last_act;
169 to_be_compared = expression->func_inst;
170 last_act = inst_act_map->get(to_be_compared);
171 last_read = last_act->get_reads_from_value();
173 equality = (write_val == last_read);
174 if (equality != expression->value)
175 satisfy_predicate = false;
178 equality = ((void*)write_val == NULL);
179 if (equality != expression->value)
180 satisfy_predicate = false;
183 model_print("unknown predicate token\n");
187 if (!satisfy_predicate)
191 if (!satisfy_predicate) {
192 ASSERT(rf_set != NULL);
193 (*rf_set)[index] = rf_set->back();
195 pruned_writes->push_back(write_act);
204 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate.
206 * @param thread A thread whose last action is a read
208 void NewFuzzer::conditional_sleep(Thread * thread)
210 model->getScheduler()->add_sleep(thread);
211 paused_thread_set.push_back(thread);
214 bool NewFuzzer::has_paused_threads()
216 return paused_thread_set.size() != 0;
219 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
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]);
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);
233 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
235 int random_index = random() % paused_thread_set.size();
236 Thread * thread = paused_thread_set[random_index];
237 model->getScheduler()->remove_sleep(thread);
239 paused_thread_set[random_index] = paused_thread_set.back();
240 paused_thread_set.pop_back();
242 model_print("thread %d is woken up\n", thread->get_id());
243 threadlist[*numthreads] = thread->get_id();