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 // reset thread pending action and revert sequence numbers
56 read_thread->set_pending(read);
57 read->reset_seq_number();
58 execution->restore_last_seq_num();
60 conditional_sleep(read_thread);
63 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
64 for (uint i = 0; i < pruned_writes->size(); i++)
65 rf_set->push_back( (*pruned_writes)[i] );
66 pruned_writes->clear();
70 ASSERT(rf_set->size() != 0);
71 int random_index = random() % rf_set->size();
76 /* Select a random branch from the children of curr_pred
77 * @return The selected branch
79 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
81 int thread_id = id_to_int(tid);
82 if ( thrd_selected_child_branch.size() <= (uint) thread_id)
83 thrd_selected_child_branch.resize(thread_id + 1);
85 if (curr_pred == NULL || read_inst == NULL) {
86 thrd_selected_child_branch[thread_id] = NULL;
90 ModelVector<Predicate *> * children = curr_pred->get_children();
91 SnapVector<Predicate *> branches;
93 for (uint i = 0; i < children->size(); i++) {
94 Predicate * child = (*children)[i];
95 if (child->get_func_inst() == read_inst)
96 branches.push_back(child);
99 // predicate children have not been generated
100 if (branches.size() == 0) {
101 thrd_selected_child_branch[thread_id] = NULL;
105 // randomly select a branch
106 int random_index = random() % branches.size();
107 Predicate * random_branch = branches[ random_index ];
108 thrd_selected_child_branch[thread_id] = random_branch;
110 return random_branch;
113 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
115 int thread_id = id_to_int(tid);
116 if (thrd_selected_child_branch.size() <= (uint) thread_id)
119 return thrd_selected_child_branch[thread_id];
122 /* Remove writes from the rf_set that do not satisfie the selected predicate,
123 * and store them in thrd_pruned_writes
125 * @return true if rf_set is pruned
127 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
128 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
133 PredExprSet * pred_expressions = pred->get_pred_expressions();
134 if (pred_expressions->getSize() == 0) // unset predicates
137 int thread_id = id_to_int(tid);
138 uint old_size = thrd_pruned_writes.size();
139 if (thrd_pruned_writes.size() <= (uint) thread_id) {
140 uint new_size = thread_id + 1;
141 thrd_pruned_writes.resize(new_size);
142 for (uint i = old_size; i < new_size; i++)
143 thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
145 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
146 pruned_writes->clear(); // clear the old pruned_writes set
150 while ( index < rf_set->size() ) {
151 ModelAction * write_act = (*rf_set)[index];
152 bool satisfy_predicate = true;
154 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
155 while (pred_expr_it->hasNext()) {
156 struct pred_expr * expression = pred_expr_it->next();
157 uint64_t write_val = write_act->get_write_value();
160 // No predicate, return false
161 if (expression->token == NOPREDICATE)
164 switch(expression->token) {
166 FuncInst * to_be_compared;
167 ModelAction * last_act;
170 to_be_compared = expression->func_inst;
171 last_act = inst_act_map->get(to_be_compared);
172 last_read = last_act->get_reads_from_value();
174 equality = (write_val == last_read);
175 if (equality != expression->value)
176 satisfy_predicate = false;
179 equality = ((void*)write_val == NULL);
180 if (equality != expression->value)
181 satisfy_predicate = false;
184 model_print("unknown predicate token\n");
188 if (!satisfy_predicate)
192 if (!satisfy_predicate) {
193 ASSERT(rf_set != NULL);
194 (*rf_set)[index] = rf_set->back();
196 pruned_writes->push_back(write_act);
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 model->getScheduler()->add_sleep(thread);
212 paused_thread_set.push_back(thread);
215 bool NewFuzzer::has_paused_threads()
217 return paused_thread_set.size() != 0;
220 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
222 if (numthreads == 0 && has_paused_threads()) {
223 wake_up_paused_threads(threadlist, &numthreads);
224 model_print("list size: %d\n", numthreads);
225 model_print("active t id: %d\n", threadlist[0]);
228 int random_index = random() % numthreads;
229 int thread = threadlist[random_index];
230 thread_id_t curr_tid = int_to_id(thread);
231 return model->get_thread(curr_tid);
234 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
236 int random_index = random() % paused_thread_set.size();
237 Thread * thread = paused_thread_set[random_index];
238 model->getScheduler()->remove_sleep(thread);
240 paused_thread_set[random_index] = paused_thread_set.back();
241 paused_thread_set.pop_back();
243 model_print("thread %d is woken up\n", thread->get_id());
244 threadlist[*numthreads] = thread->get_id();
248 bool NewFuzzer::shouldWait(const ModelAction * act)