2 #include "threads-model.h"
8 #include "concretepredicate.h"
12 #include "execution.h"
14 NewFuzzer::NewFuzzer() :
17 thrd_selected_child_branch(),
20 paused_thread_table(128)
24 * @brief Register the ModelHistory and ModelExecution engine
26 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
28 this->history = history;
29 this->execution = execution;
32 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
34 // return random() % rf_set->size();
36 thread_id_t tid = read->get_tid();
37 int thread_id = id_to_int(tid);
39 if (thrd_last_read_act.size() <= (uint) thread_id)
40 thrd_last_read_act.resize(thread_id + 1);
42 // A new read action is encountered, select a random child branch of current predicate
43 if (read != thrd_last_read_act[thread_id]) {
44 thrd_last_read_act[thread_id] = read;
46 FuncNode * func_node = history->get_curr_func_node(tid);
47 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
48 FuncInst * read_inst = func_node->get_inst(read);
49 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
51 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
52 prune_writes(tid, selected_branch, rf_set, inst_act_map);
55 // No write satisfies the selected predicate, so pause this thread.
56 if ( rf_set->size() == 0 ) {
57 Thread * read_thread = execution->get_thread(tid);
58 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());
60 // reset thread pending action and revert sequence numbers
61 read_thread->set_pending(read);
62 read->reset_seq_number();
63 execution->restore_last_seq_num();
65 conditional_sleep(read_thread);
71 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
72 for (uint i = 0; i < pruned_writes->size(); i++)
73 rf_set->push_back( (*pruned_writes)[i] );
74 pruned_writes->clear();
78 ASSERT(rf_set->size() != 0);
79 int random_index = random() % rf_set->size();
84 /* Select a random branch from the children of curr_pred
85 * @return The selected branch
87 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
89 int thread_id = id_to_int(tid);
90 if ( thrd_selected_child_branch.size() <= (uint) thread_id)
91 thrd_selected_child_branch.resize(thread_id + 1);
93 if (curr_pred == NULL || read_inst == NULL) {
94 thrd_selected_child_branch[thread_id] = NULL;
98 ModelVector<Predicate *> * children = curr_pred->get_children();
99 SnapVector<Predicate *> branches;
101 for (uint i = 0; i < children->size(); i++) {
102 Predicate * child = (*children)[i];
103 if (child->get_func_inst() == read_inst)
104 branches.push_back(child);
107 // predicate children have not been generated
108 if (branches.size() == 0) {
109 thrd_selected_child_branch[thread_id] = NULL;
113 // randomly select a branch
114 int random_index = random() % branches.size();
115 Predicate * random_branch = branches[ random_index ];
116 thrd_selected_child_branch[thread_id] = random_branch;
118 return random_branch;
121 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
123 int thread_id = id_to_int(tid);
124 if (thrd_selected_child_branch.size() <= (uint) thread_id)
127 return thrd_selected_child_branch[thread_id];
130 /* Remove writes from the rf_set that do not satisfie the selected predicate,
131 * and store them in thrd_pruned_writes
133 * @return true if rf_set is pruned
135 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
136 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
141 PredExprSet * pred_expressions = pred->get_pred_expressions();
142 if (pred_expressions->getSize() == 0) // unset predicates
145 int thread_id = id_to_int(tid);
146 uint old_size = thrd_pruned_writes.size();
147 if (thrd_pruned_writes.size() <= (uint) thread_id) {
148 uint new_size = thread_id + 1;
149 thrd_pruned_writes.resize(new_size);
150 for (uint i = old_size; i < new_size; i++)
151 thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
153 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
154 pruned_writes->clear(); // clear the old pruned_writes set
159 ConcretePredicate * concrete_pred = pred->evaluate(inst_act_map, tid);
160 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
162 while ( index < rf_set->size() ) {
163 ModelAction * write_act = (*rf_set)[index];
164 uint64_t write_val = write_act->get_write_value();
165 bool satisfy_predicate = true;
167 for (uint i = 0; i < concrete_exprs->size(); i++) {
168 struct concrete_pred_expr concrete = (*concrete_exprs)[i];
171 switch (concrete.token) {
175 equality = (write_val == concrete.value);
176 if (equality != concrete.equality)
177 satisfy_predicate = false;
180 equality = ((void*)write_val == NULL);
181 if (equality != concrete.equality)
182 satisfy_predicate = false;
185 model_print("unknown predicate token\n");
189 if (!satisfy_predicate)
193 if (!satisfy_predicate) {
194 ASSERT(rf_set != NULL);
195 (*rf_set)[index] = rf_set->back();
197 pruned_writes->push_back(write_act);
203 delete concrete_pred;
208 /* @brief Put a thread to sleep because no writes in rf_set satisfies the selected predicate.
210 * @param thread A thread whose last action is a read
212 void NewFuzzer::conditional_sleep(Thread * thread)
214 int index = paused_thread_set.size();
216 model->getScheduler()->add_sleep(thread);
217 paused_thread_set.push_back(thread);
218 paused_thread_table.put(thread, index); // Update table
220 /* Add the waiting condition to ModelHistory */
221 ModelAction * read = thread->get_pending();
222 thread_id_t tid = thread->get_id();
223 FuncNode * func_node = history->get_curr_func_node(tid);
224 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
226 Predicate * selected_branch = get_selected_child_branch(tid);
227 ConcretePredicate * concrete = selected_branch->evaluate(inst_act_map, tid);
228 concrete->set_location(read->get_location());
230 history->add_waiting_write(concrete);
233 bool NewFuzzer::has_paused_threads()
235 return paused_thread_set.size() != 0;
238 Thread * NewFuzzer::selectThread(int * threadlist, int numthreads)
240 if (numthreads == 0 && has_paused_threads()) {
241 wake_up_paused_threads(threadlist, &numthreads);
242 model_print("list size: %d, active t id: %d\n", numthreads, threadlist[0]);
245 int random_index = random() % numthreads;
246 int thread = threadlist[random_index];
247 thread_id_t curr_tid = int_to_id(thread);
248 return model->get_thread(curr_tid);
251 /* Force waking up one of threads paused by Fuzzer, because otherwise
252 * the Fuzzer is not making progress
254 void NewFuzzer::wake_up_paused_threads(int * threadlist, int * numthreads)
256 int random_index = random() % paused_thread_set.size();
257 Thread * thread = paused_thread_set[random_index];
258 model->getScheduler()->remove_sleep(thread);
260 Thread * last_thread = paused_thread_set.back();
261 paused_thread_set[random_index] = last_thread;
262 paused_thread_set.pop_back();
263 paused_thread_table.put(last_thread, random_index); // Update table
264 paused_thread_table.remove(thread);
266 thread_id_t tid = thread->get_id();
267 history->remove_waiting_write(tid);
269 model_print("thread %d is woken up\n", tid);
270 threadlist[*numthreads] = tid;
274 /* Wake up conditional sleeping threads if the desired write is available */
275 void NewFuzzer::notify_paused_thread(Thread * thread)
277 ASSERT(paused_thread_table.contains(thread));
279 int index = paused_thread_table.get(thread);
280 model->getScheduler()->remove_sleep(thread);
282 Thread * last_thread = paused_thread_set.back();
283 paused_thread_set[index] = last_thread;
284 paused_thread_set.pop_back();
285 paused_thread_table.put(last_thread, index); // Update table
286 paused_thread_table.remove(thread);
288 thread_id_t tid = thread->get_id();
289 history->remove_waiting_write(tid);
292 /* Find threads that may write values that the pending read action is waiting for */
293 void NewFuzzer::find_threads(ModelAction * pending_read)
295 void * location = pending_read->get_location();
296 thread_id_t self_id = pending_read->get_tid();
298 SnapVector<FuncNode *> * func_node_list = history->getWrFuncNodes(location);
299 for (uint i = 0; i < func_node_list->size(); i++) {
300 FuncNode * target_node = (*func_node_list)[i];
301 model_print("node %s may write to loc %p\n", target_node->get_func_name(), location);
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);
312 int distance = node->compute_distance(target_node);
313 model_print("thread: %d; distance from node %d to node %d: %d\n", tid, node->get_func_id(), target_node->get_func_id(), distance);
318 bool NewFuzzer::shouldWait(const ModelAction * act)