2 #include "threads-model.h"
8 #include "concretepredicate.h"
13 #include "execution.h"
15 NewFuzzer::NewFuzzer() :
18 thrd_selected_child_branch(),
21 paused_thread_table(128)
25 * @brief Register the ModelHistory and ModelExecution engine
27 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
29 this->history = history;
30 this->execution = execution;
33 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
35 // return random() % rf_set->size();
37 thread_id_t tid = read->get_tid();
38 int thread_id = id_to_int(tid);
40 if (thrd_last_read_act.size() <= (uint) thread_id)
41 thrd_last_read_act.resize(thread_id + 1);
43 // A new read action is encountered, select a random child branch of current predicate
44 if (read != thrd_last_read_act[thread_id]) {
45 thrd_last_read_act[thread_id] = read;
47 FuncNode * func_node = history->get_curr_func_node(tid);
48 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
49 FuncInst * read_inst = func_node->get_inst(read);
50 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
52 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
53 prune_writes(tid, selected_branch, rf_set, inst_act_map);
56 // No write satisfies the selected predicate, so pause this thread.
57 if ( rf_set->size() == 0 ) {
58 Thread * read_thread = execution->get_thread(tid);
59 model_print("the %d read action of thread %d at %p is unsuccessful\n", read->get_seq_number(), read_thread->get_id(), read->get_location());
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);
72 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
73 for (uint i = 0; i < pruned_writes->size(); i++)
74 rf_set->push_back( (*pruned_writes)[i] );
75 pruned_writes->clear();
79 ASSERT(rf_set->size() != 0);
80 int random_index = random() % rf_set->size();
85 /* Select a random branch from the children of curr_pred
86 * @return The selected branch
88 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
90 int thread_id = id_to_int(tid);
91 if ( thrd_selected_child_branch.size() <= (uint) thread_id)
92 thrd_selected_child_branch.resize(thread_id + 1);
94 if (curr_pred == NULL || read_inst == NULL) {
95 thrd_selected_child_branch[thread_id] = NULL;
99 ModelVector<Predicate *> * children = curr_pred->get_children();
100 SnapVector<Predicate *> branches;
102 for (uint i = 0; i < children->size(); i++) {
103 Predicate * child = (*children)[i];
104 if (child->get_func_inst() == read_inst)
105 branches.push_back(child);
108 // predicate children have not been generated
109 if (branches.size() == 0) {
110 thrd_selected_child_branch[thread_id] = NULL;
114 // randomly select a branch
115 int random_index = random() % branches.size();
116 Predicate * random_branch = branches[ random_index ];
117 thrd_selected_child_branch[thread_id] = random_branch;
119 return random_branch;
122 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
124 int thread_id = id_to_int(tid);
125 if (thrd_selected_child_branch.size() <= (uint) thread_id)
128 return thrd_selected_child_branch[thread_id];
131 /* Remove writes from the rf_set that do not satisfie the selected predicate,
132 * and store them in thrd_pruned_writes
134 * @return true if rf_set is pruned
136 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
137 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
142 PredExprSet * pred_expressions = pred->get_pred_expressions();
143 if (pred_expressions->getSize() == 0) // unset predicates
146 int thread_id = id_to_int(tid);
147 uint old_size = thrd_pruned_writes.size();
148 if (thrd_pruned_writes.size() <= (uint) thread_id) {
149 uint new_size = thread_id + 1;
150 thrd_pruned_writes.resize(new_size);
151 for (uint i = old_size; i < new_size; i++)
152 thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
154 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
155 pruned_writes->clear(); // clear the old pruned_writes set
160 ConcretePredicate * concrete_pred = pred->evaluate(inst_act_map, tid);
161 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
163 while ( index < rf_set->size() ) {
164 ModelAction * write_act = (*rf_set)[index];
165 uint64_t write_val = write_act->get_write_value();
166 bool satisfy_predicate = true;
168 for (uint i = 0; i < concrete_exprs->size(); i++) {
169 struct concrete_pred_expr concrete = (*concrete_exprs)[i];
172 switch (concrete.token) {
176 equality = (write_val == concrete.value);
177 if (equality != concrete.equality)
178 satisfy_predicate = false;
181 equality = ((void*)write_val == NULL);
182 if (equality != concrete.equality)
183 satisfy_predicate = false;
186 model_print("unknown predicate token\n");
190 if (!satisfy_predicate)
194 if (!satisfy_predicate) {
195 ASSERT(rf_set != NULL);
196 (*rf_set)[index] = rf_set->back();
198 pruned_writes->push_back(write_act);
204 delete concrete_pred;
209 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate.
211 * @param thread A thread whose last action is a read
213 void NewFuzzer::conditional_sleep(Thread * thread)
215 int index = paused_thread_set.size();
217 model->getScheduler()->add_sleep(thread);
218 paused_thread_set.push_back(thread);
219 paused_thread_table.put(thread, index); // Update table
221 /* Add the waiting condition to ModelHistory */
222 ModelAction * read = thread->get_pending();
223 thread_id_t tid = thread->get_id();
224 FuncNode * func_node = history->get_curr_func_node(tid);
225 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
227 Predicate * selected_branch = get_selected_child_branch(tid);
228 ConcretePredicate * concrete = selected_branch->evaluate(inst_act_map, tid);
229 concrete->set_location(read->get_location());
231 history->add_waiting_write(concrete);
234 bool NewFuzzer::has_paused_threads()
236 return paused_thread_set.size() != 0;
239 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
241 if (numthreads == 0 && has_paused_threads()) {
242 wake_up_paused_threads(threadlist, &numthreads);
243 model_print("list size: %d, active t id: %d\n", numthreads, threadlist[0]);
246 int random_index = random() % numthreads;
247 int thread = threadlist[random_index];
248 thread_id_t curr_tid = int_to_id(thread);
249 return model->get_thread(curr_tid);
252 /* Force waking up one of threads paused by Fuzzer, because otherwise
253 * the Fuzzer is not making progress
255 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
257 int random_index = random() % paused_thread_set.size();
258 Thread * thread = paused_thread_set[random_index];
259 model->getScheduler()->remove_sleep(thread);
261 Thread * last_thread = paused_thread_set.back();
262 paused_thread_set[random_index] = last_thread;
263 paused_thread_set.pop_back();
264 paused_thread_table.put(last_thread, random_index); // Update table
265 paused_thread_table.remove(thread);
267 thread_id_t tid = thread->get_id();
268 history->remove_waiting_write(tid);
270 model_print("thread %d is woken up\n", tid);
271 threadlist[*numthreads] = tid;
275 /* Wake up conditional sleeping threads if the desired write is available */
276 void NewFuzzer::notify_paused_thread(Thread * thread)
278 ASSERT(paused_thread_table.contains(thread));
280 int index = paused_thread_table.get(thread);
281 model->getScheduler()->remove_sleep(thread);
283 Thread * last_thread = paused_thread_set.back();
284 paused_thread_set[index] = last_thread;
285 paused_thread_set.pop_back();
286 paused_thread_table.put(last_thread, index); // Update table
287 paused_thread_table.remove(thread);
289 thread_id_t tid = thread->get_id();
290 history->remove_waiting_write(tid);
293 /* Find threads that may write values that the pending read action is waiting for */
294 void NewFuzzer::find_threads(ModelAction * pending_read)
296 void * location = pending_read->get_location();
297 thread_id_t self_id = pending_read->get_tid();
298 HashSet<thread_id_t, int, 0> waiting_for_threads(64);
300 SnapVector<FuncNode *> * func_node_list = history->getWrFuncNodes(location);
301 for (uint i = 0; i < func_node_list->size(); i++) {
302 FuncNode * target_node = (*func_node_list)[i];
303 for (uint i = 1; i < execution->get_num_threads(); i++) {
304 thread_id_t tid = int_to_id(i);
308 FuncNode * node = history->get_curr_func_node(tid);
309 /* It is possible that thread tid is not in any FuncNode */
313 int distance = node->compute_distance(target_node);
314 if (distance != -1) {
315 waiting_for_threads.add(tid);
316 model_print("thread: %d; distance from node %d to node %d: %d\n", tid, node->get_func_id(), target_node->get_func_id(), distance);
323 /* Clear list first */
324 WaitObj * wait_obj = history->getWaitObj(self_id);
325 thrd_id_set_t * waiting_threads = wait_obj->getWaitingFor();
326 waiting_threads->reset();
328 HSIterator<thread_id_t, int, 0> * it = waiting_for_threads.iterator();
329 while (it->hasNext()) {
330 thread_id_t tid = it->next();
331 waiting_threads->add(tid);
335 bool NewFuzzer::shouldWait(const ModelAction * act)