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 // return random() % rf_set->size();
30 thread_id_t tid = read->get_tid();
31 int thread_id = id_to_int(tid);
33 if (thrd_last_read_act.size() <= (uint) thread_id)
34 thrd_last_read_act.resize(thread_id + 1);
36 // A new read action is encountered, select a random child branch of current predicate
37 if (read != thrd_last_read_act[thread_id]) {
38 thrd_last_read_act[thread_id] = read;
40 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
41 uint32_t func_id = (*thrd_func_list)[thread_id].back();
42 FuncNode * func_node = history->get_func_node(func_id);
43 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
44 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
45 FuncInst * read_inst = func_node->get_inst(read);
47 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
48 prune_writes(tid, selected_branch, rf_set, inst_act_map);
51 // No write satisfies the selected predicate
52 if ( rf_set->size() == 0 ) {
53 Thread * read_thread = execution->get_thread(tid);
54 model_print("the %d read action of thread %d is unsuccessful\n", read->get_seq_number(), read_thread->get_id());
56 // reset thread pending action and revert sequence numbers
57 read_thread->set_pending(read);
58 read->reset_seq_number();
59 execution->restore_last_seq_num();
61 conditional_sleep(read_thread);
64 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
65 for (uint i = 0; i < pruned_writes->size(); i++)
66 rf_set->push_back( (*pruned_writes)[i] );
67 pruned_writes->clear();
71 ASSERT(rf_set->size() != 0);
72 int random_index = random() % rf_set->size();
77 /* Select a random branch from the children of curr_pred
78 * @return The selected branch
80 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
82 int thread_id = id_to_int(tid);
83 if ( thrd_selected_child_branch.size() <= (uint) thread_id)
84 thrd_selected_child_branch.resize(thread_id + 1);
86 if (curr_pred == NULL || read_inst == NULL) {
87 thrd_selected_child_branch[thread_id] = NULL;
91 ModelVector<Predicate *> * children = curr_pred->get_children();
92 SnapVector<Predicate *> branches;
94 for (uint i = 0; i < children->size(); i++) {
95 Predicate * child = (*children)[i];
96 if (child->get_func_inst() == read_inst)
97 branches.push_back(child);
100 // predicate children have not been generated
101 if (branches.size() == 0) {
102 thrd_selected_child_branch[thread_id] = NULL;
106 // randomly select a branch
107 int random_index = random() % branches.size();
108 Predicate * random_branch = branches[ random_index ];
109 thrd_selected_child_branch[thread_id] = random_branch;
111 return random_branch;
114 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
116 int thread_id = id_to_int(tid);
117 if (thrd_selected_child_branch.size() <= (uint) thread_id)
120 return thrd_selected_child_branch[thread_id];
123 /* Remove writes from the rf_set that do not satisfie the selected predicate,
124 * and store them in thrd_pruned_writes
126 * @return true if rf_set is pruned
128 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
129 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
134 PredExprSet * pred_expressions = pred->get_pred_expressions();
135 if (pred_expressions->getSize() == 0) // unset predicates
138 int thread_id = id_to_int(tid);
139 uint old_size = thrd_pruned_writes.size();
140 if (thrd_pruned_writes.size() <= (uint) thread_id) {
141 uint new_size = thread_id + 1;
142 thrd_pruned_writes.resize(new_size);
143 for (uint i = old_size; i < new_size; i++)
144 thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
146 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
147 pruned_writes->clear(); // clear the old pruned_writes set
151 SnapVector<struct concrete_pred_expr> concrete_exprs = pred->evaluate(inst_act_map);
153 while ( index < rf_set->size() ) {
154 ModelAction * write_act = (*rf_set)[index];
155 uint64_t write_val = write_act->get_write_value();
156 bool satisfy_predicate = true;
158 for (uint i = 0; i < concrete_exprs.size(); i++) {
159 struct concrete_pred_expr concrete = concrete_exprs[i];
162 switch (concrete.token) {
166 equality = (write_val == concrete.value);
167 if (equality != concrete.equality)
168 satisfy_predicate = false;
171 equality = ((void*)write_val == NULL);
172 if (equality != concrete.equality)
173 satisfy_predicate = false;
176 model_print("unknown predicate token\n");
180 if (!satisfy_predicate)
184 if (!satisfy_predicate) {
185 ASSERT(rf_set != NULL);
186 (*rf_set)[index] = rf_set->back();
188 pruned_writes->push_back(write_act);
197 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate.
199 * @param thread A thread whose last action is a read
201 void NewFuzzer::conditional_sleep(Thread * thread)
203 model->getScheduler()->add_sleep(thread);
204 paused_thread_set.push_back(thread);
207 bool NewFuzzer::has_paused_threads()
209 return paused_thread_set.size() != 0;
212 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
214 if (numthreads == 0 && has_paused_threads()) {
215 wake_up_paused_threads(threadlist, &numthreads);
216 model_print("list size: %d\n", numthreads);
217 model_print("active t id: %d\n", threadlist[0]);
220 int random_index = random() % numthreads;
221 int thread = threadlist[random_index];
222 thread_id_t curr_tid = int_to_id(thread);
223 return model->get_thread(curr_tid);
226 /* Force waking up one of threads paused by Fuzzer */
227 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
229 int random_index = random() % paused_thread_set.size();
230 Thread * thread = paused_thread_set[random_index];
231 model->getScheduler()->remove_sleep(thread);
233 paused_thread_set[random_index] = paused_thread_set.back();
234 paused_thread_set.pop_back();
236 model_print("thread %d is woken up\n", thread->get_id());
237 threadlist[*numthreads] = thread->get_id();
241 /* Notify one of conditional sleeping threads if the desired write is available */
242 bool NewFuzzer::notify_conditional_sleep(Thread * thread)
247 bool NewFuzzer::shouldWait(const ModelAction * act)