2 #include "threads-model.h"
8 #include "concretepredicate.h"
12 #include "execution.h"
14 NewFuzzer::NewFuzzer() :
17 thrd_selected_child_branch(),
23 * @brief Register the ModelHistory and ModelExecution engine
25 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
27 this->history = history;
28 this->execution = execution;
31 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
33 // return random() % rf_set->size();
35 thread_id_t tid = read->get_tid();
36 int thread_id = id_to_int(tid);
38 if (thrd_last_read_act.size() <= (uint) thread_id)
39 thrd_last_read_act.resize(thread_id + 1);
41 // A new read action is encountered, select a random child branch of current predicate
42 if (read != thrd_last_read_act[thread_id]) {
43 thrd_last_read_act[thread_id] = read;
45 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
46 uint32_t func_id = (*thrd_func_list)[thread_id].back();
47 FuncNode * func_node = history->get_func_node(func_id);
48 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
49 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
50 FuncInst * read_inst = func_node->get_inst(read);
52 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
53 prune_writes(tid, selected_branch, rf_set, inst_act_map);
56 // No write satisfies the selected predicate
57 if ( rf_set->size() == 0 ) {
58 Thread * read_thread = execution->get_thread(tid);
59 model_print("the %d read action of thread %d is unsuccessful\n", read->get_seq_number(), read_thread->get_id());
61 // reset thread pending action and revert sequence numbers
62 read_thread->set_pending(read);
63 read->reset_seq_number();
64 execution->restore_last_seq_num();
66 conditional_sleep(read_thread);
69 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
70 for (uint i = 0; i < pruned_writes->size(); i++)
71 rf_set->push_back( (*pruned_writes)[i] );
72 pruned_writes->clear();
76 ASSERT(rf_set->size() != 0);
77 int random_index = random() % rf_set->size();
82 /* Select a random branch from the children of curr_pred
83 * @return The selected branch
85 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
87 int thread_id = id_to_int(tid);
88 if ( thrd_selected_child_branch.size() <= (uint) thread_id)
89 thrd_selected_child_branch.resize(thread_id + 1);
91 if (curr_pred == NULL || read_inst == NULL) {
92 thrd_selected_child_branch[thread_id] = NULL;
96 ModelVector<Predicate *> * children = curr_pred->get_children();
97 SnapVector<Predicate *> branches;
99 for (uint i = 0; i < children->size(); i++) {
100 Predicate * child = (*children)[i];
101 if (child->get_func_inst() == read_inst)
102 branches.push_back(child);
105 // predicate children have not been generated
106 if (branches.size() == 0) {
107 thrd_selected_child_branch[thread_id] = NULL;
111 // randomly select a branch
112 int random_index = random() % branches.size();
113 Predicate * random_branch = branches[ random_index ];
114 thrd_selected_child_branch[thread_id] = random_branch;
116 return random_branch;
119 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
121 int thread_id = id_to_int(tid);
122 if (thrd_selected_child_branch.size() <= (uint) thread_id)
125 return thrd_selected_child_branch[thread_id];
128 /* Remove writes from the rf_set that do not satisfie the selected predicate,
129 * and store them in thrd_pruned_writes
131 * @return true if rf_set is pruned
133 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
134 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
139 PredExprSet * pred_expressions = pred->get_pred_expressions();
140 if (pred_expressions->getSize() == 0) // unset predicates
143 int thread_id = id_to_int(tid);
144 uint old_size = thrd_pruned_writes.size();
145 if (thrd_pruned_writes.size() <= (uint) thread_id) {
146 uint new_size = thread_id + 1;
147 thrd_pruned_writes.resize(new_size);
148 for (uint i = old_size; i < new_size; i++)
149 thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
151 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
152 pruned_writes->clear(); // clear the old pruned_writes set
157 ConcretePredicate * concrete_pred = pred->evaluate(inst_act_map, tid);
158 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
160 while ( index < rf_set->size() ) {
161 ModelAction * write_act = (*rf_set)[index];
162 uint64_t write_val = write_act->get_write_value();
163 bool satisfy_predicate = true;
165 for (uint i = 0; i < concrete_exprs->size(); i++) {
166 struct concrete_pred_expr concrete = (*concrete_exprs)[i];
169 switch (concrete.token) {
173 equality = (write_val == concrete.value);
174 if (equality != concrete.equality)
175 satisfy_predicate = false;
178 equality = ((void*)write_val == NULL);
179 if (equality != concrete.equality)
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 /* Force waking up one of threads paused by Fuzzer */
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 /* Notify one of conditional sleeping threads if the desired write is available */
249 bool NewFuzzer::notify_conditional_sleep(Thread * thread)
254 bool NewFuzzer::shouldWait(const ModelAction * act)