2 #include "threads-model.h"
9 #include "concretepredicate.h"
11 NewFuzzer::NewFuzzer() :
14 thrd_selected_child_branch(),
20 * @brief Register the ModelHistory and ModelExecution engine
22 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
24 this->history = history;
25 this->execution = execution;
28 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
30 // return random() % rf_set->size();
32 thread_id_t tid = read->get_tid();
33 int thread_id = id_to_int(tid);
35 if (thrd_last_read_act.size() <= (uint) thread_id)
36 thrd_last_read_act.resize(thread_id + 1);
38 // A new read action is encountered, select a random child branch of current predicate
39 if (read != thrd_last_read_act[thread_id]) {
40 thrd_last_read_act[thread_id] = read;
42 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
43 uint32_t func_id = (*thrd_func_list)[thread_id].back();
44 FuncNode * func_node = history->get_func_node(func_id);
45 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
46 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
47 FuncInst * read_inst = func_node->get_inst(read);
49 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
50 prune_writes(tid, selected_branch, rf_set, inst_act_map);
53 // No write satisfies the selected predicate
54 if ( rf_set->size() == 0 ) {
55 Thread * read_thread = execution->get_thread(tid);
56 model_print("the %d read action of thread %d is unsuccessful\n", read->get_seq_number(), read_thread->get_id());
58 // reset thread pending action and revert sequence numbers
59 read_thread->set_pending(read);
60 read->reset_seq_number();
61 execution->restore_last_seq_num();
63 conditional_sleep(read_thread);
66 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
67 for (uint i = 0; i < pruned_writes->size(); i++)
68 rf_set->push_back( (*pruned_writes)[i] );
69 pruned_writes->clear();
73 ASSERT(rf_set->size() != 0);
74 int random_index = random() % rf_set->size();
79 /* Select a random branch from the children of curr_pred
80 * @return The selected branch
82 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
84 int thread_id = id_to_int(tid);
85 if ( thrd_selected_child_branch.size() <= (uint) thread_id)
86 thrd_selected_child_branch.resize(thread_id + 1);
88 if (curr_pred == NULL || read_inst == NULL) {
89 thrd_selected_child_branch[thread_id] = NULL;
93 ModelVector<Predicate *> * children = curr_pred->get_children();
94 SnapVector<Predicate *> branches;
96 for (uint i = 0; i < children->size(); i++) {
97 Predicate * child = (*children)[i];
98 if (child->get_func_inst() == read_inst)
99 branches.push_back(child);
102 // predicate children have not been generated
103 if (branches.size() == 0) {
104 thrd_selected_child_branch[thread_id] = NULL;
108 // randomly select a branch
109 int random_index = random() % branches.size();
110 Predicate * random_branch = branches[ random_index ];
111 thrd_selected_child_branch[thread_id] = random_branch;
113 return random_branch;
116 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
118 int thread_id = id_to_int(tid);
119 if (thrd_selected_child_branch.size() <= (uint) thread_id)
122 return thrd_selected_child_branch[thread_id];
125 /* Remove writes from the rf_set that do not satisfie the selected predicate,
126 * and store them in thrd_pruned_writes
128 * @return true if rf_set is pruned
130 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
131 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
136 PredExprSet * pred_expressions = pred->get_pred_expressions();
137 if (pred_expressions->getSize() == 0) // unset predicates
140 int thread_id = id_to_int(tid);
141 uint old_size = thrd_pruned_writes.size();
142 if (thrd_pruned_writes.size() <= (uint) thread_id) {
143 uint new_size = thread_id + 1;
144 thrd_pruned_writes.resize(new_size);
145 for (uint i = old_size; i < new_size; i++)
146 thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
148 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
149 pruned_writes->clear(); // clear the old pruned_writes set
154 ConcretePredicate * concrete_pred = pred->evaluate(inst_act_map, tid);
155 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
157 while ( index < rf_set->size() ) {
158 ModelAction * write_act = (*rf_set)[index];
159 uint64_t write_val = write_act->get_write_value();
160 bool satisfy_predicate = true;
162 for (uint i = 0; i < concrete_exprs->size(); i++) {
163 struct concrete_pred_expr concrete = (*concrete_exprs)[i];
166 switch (concrete.token) {
170 equality = (write_val == concrete.value);
171 if (equality != concrete.equality)
172 satisfy_predicate = false;
175 equality = ((void*)write_val == NULL);
176 if (equality != concrete.equality)
177 satisfy_predicate = false;
180 model_print("unknown predicate token\n");
184 if (!satisfy_predicate)
188 if (!satisfy_predicate) {
189 ASSERT(rf_set != NULL);
190 (*rf_set)[index] = rf_set->back();
192 pruned_writes->push_back(write_act);
201 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate.
203 * @param thread A thread whose last action is a read
205 void NewFuzzer::conditional_sleep(Thread * thread)
207 model->getScheduler()->add_sleep(thread);
208 paused_thread_set.push_back(thread);
211 bool NewFuzzer::has_paused_threads()
213 return paused_thread_set.size() != 0;
216 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
218 if (numthreads == 0 && has_paused_threads()) {
219 wake_up_paused_threads(threadlist, &numthreads);
220 model_print("list size: %d\n", numthreads);
221 model_print("active t id: %d\n", threadlist[0]);
224 int random_index = random() % numthreads;
225 int thread = threadlist[random_index];
226 thread_id_t curr_tid = int_to_id(thread);
227 return model->get_thread(curr_tid);
230 /* Force waking up one of threads paused by Fuzzer */
231 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
233 int random_index = random() % paused_thread_set.size();
234 Thread * thread = paused_thread_set[random_index];
235 model->getScheduler()->remove_sleep(thread);
237 paused_thread_set[random_index] = paused_thread_set.back();
238 paused_thread_set.pop_back();
240 model_print("thread %d is woken up\n", thread->get_id());
241 threadlist[*numthreads] = thread->get_id();
245 /* Notify one of conditional sleeping threads if the desired write is available */
246 bool NewFuzzer::notify_conditional_sleep(Thread * thread)
251 bool NewFuzzer::shouldWait(const ModelAction * act)