2 #include "threads-model.h"
8 NewFuzzer::NewFuzzer() :
11 thrd_selected_child_branch(),
16 * @brief Register the ModelHistory and ModelExecution engine
18 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
20 this->history = history;
21 this->execution = execution;
24 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
26 thread_id_t tid = read->get_tid();
27 int thread_id = id_to_int(tid);
29 if (thrd_last_read_act.size() <= (uint) thread_id)
30 thrd_last_read_act.resize(thread_id + 1);
32 // A new read action is encountered, select a random child branch of current predicate
33 if (read != thrd_last_read_act[thread_id]) {
34 thrd_last_read_act[thread_id] = read;
36 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
37 uint32_t func_id = (*thrd_func_list)[thread_id].back();
38 FuncNode * func_node = history->get_func_node(func_id);
39 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
40 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
41 FuncInst * read_inst = func_node->get_inst(read);
43 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
44 prune_writes(tid, selected_branch, rf_set, inst_act_map);
47 // TODO: make this thread sleep if no write satisfies the chosen predicate
48 // if no read satisfies the selected predicate
49 if ( rf_set->size() == 0 ) {
50 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
51 for (uint i = 0; i < pruned_writes->size(); i++)
52 rf_set->push_back( (*pruned_writes)[i] );
55 ASSERT(rf_set->size() != 0);
56 int random_index = random() % rf_set->size();
61 /* Select a random branch from the children of curr_pred
62 * @return The selected branch
64 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
66 int thread_id = id_to_int(tid);
67 if ( thrd_selected_child_branch.size() <= (uint) thread_id)
68 thrd_selected_child_branch.resize(thread_id + 1);
70 if (curr_pred == NULL || read_inst == NULL) {
71 thrd_selected_child_branch[thread_id] = NULL;
75 ModelVector<Predicate *> * children = curr_pred->get_children();
76 SnapVector<Predicate *> branches;
78 for (uint i = 0; i < children->size(); i++) {
79 Predicate * child = (*children)[i];
80 if (child->get_func_inst() == read_inst)
81 branches.push_back(child);
84 // predicate children have not been generated
85 if (branches.size() == 0) {
86 thrd_selected_child_branch[thread_id] = NULL;
90 // randomly select a branch
91 int random_index = random() % branches.size();
92 Predicate * random_branch = branches[ random_index ];
93 thrd_selected_child_branch[thread_id] = random_branch;
98 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
100 int thread_id = id_to_int(tid);
101 if (thrd_selected_child_branch.size() <= (uint) thread_id)
104 return thrd_selected_child_branch[thread_id];
107 /* Remove writes from the rf_set that do not satisfie the selected predicate,
108 * and store them in thrd_pruned_writes
110 * @return true if rf_set is pruned
112 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
113 SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
118 PredExprSet * pred_expressions = pred->get_pred_expressions();
119 if (pred_expressions->getSize() == 0) // unset predicates
122 int thread_id = id_to_int(tid);
125 uint old_size = thrd_pruned_writes.size();
126 if (thrd_pruned_writes.size() <= (uint) thread_id) {
127 uint new_size = thread_id + 1;
128 thrd_pruned_writes.resize(new_size);
129 for (uint i = old_size; i < new_size; i++)
130 thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
132 SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
133 pruned_writes->clear(); // clear the old pruned_writes set
136 while ( index < rf_set->size() ) {
137 ModelAction * write_act = (*rf_set)[index];
138 bool satisfy_predicate = true;
140 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
141 while (pred_expr_it->hasNext()) {
142 struct pred_expr * expression = pred_expr_it->next();
143 uint64_t write_val = write_act->get_write_value();
146 // No predicate, return everything in the rf_set
147 if (expression->token == NOPREDICATE)
150 switch(expression->token) {
152 FuncInst * to_be_compared;
153 ModelAction * last_act;
156 to_be_compared = expression->func_inst;
157 last_act = inst_act_map->get(to_be_compared);
158 last_read = last_act->get_reads_from_value();
160 equality = (write_val == last_read);
161 if (equality != expression->value)
162 satisfy_predicate = false;
165 equality = ((void*)write_val == NULL);
166 if (equality != expression->value)
167 satisfy_predicate = false;
170 model_print("unknown predicate token\n");
174 if (!satisfy_predicate)
178 if (!satisfy_predicate) {
179 ASSERT(rf_set != NULL);
180 (*rf_set)[index] = rf_set->back();
182 pruned_writes->push_back(write_act);