2 #include "threads-model.h"
8 typedef HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map_t;
10 NewFuzzer::NewFuzzer() :
13 thrd_selected_child_branch()
17 * @brief Register the ModelHistory and ModelExecution engine
19 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
21 this->history = history;
22 this->execution = execution;
25 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
27 int random_index = random() % rf_set->size();
29 thread_id_t tid = read->get_tid();
30 int thread_id = id_to_int(tid);
32 if (thrd_last_read_act.size() <= (uint) thread_id)
33 thrd_last_read_act.resize(thread_id + 1);
35 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
36 uint32_t func_id = (*thrd_func_list)[thread_id].back();
37 FuncNode * func_node = history->get_func_node(func_id);
38 inst_act_map_t * inst_act_map = func_node->get_inst_act_map(tid);
40 // A new read action is encountered, select a random child branch of current predicate
41 if (read != thrd_last_read_act[thread_id]) {
42 thrd_last_read_act[thread_id] = read;
44 FuncInst * read_inst = func_node->get_inst(read);
45 Predicate * curr_pred = func_node->get_predicate_tree_position(tid);
46 selectBranch(thread_id, curr_pred, read_inst);
49 Predicate * selected_branch = thrd_selected_child_branch[thread_id];
50 if (selected_branch == NULL)
53 PredExprSet * pred_expressions = selected_branch->get_pred_expressions();
55 // FuncInst * read_inst = selected_branch->get_func_inst();
56 // model_print("thread %d ", tid);
57 // read_inst->print();
60 if (pred_expressions->getSize() == 0)
63 for (uint index = 0; index < rf_set->size(); index++) {
64 ModelAction * write_act = (*rf_set)[index];
65 bool satisfy_predicate = true;
67 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
68 while (pred_expr_it->hasNext()) {
69 struct pred_expr * expression = pred_expr_it->next();
70 uint64_t last_read, write_val;
73 if (expression->token == NOPREDICATE)
76 switch(expression->token) {
78 FuncInst * to_be_compared;
79 ModelAction * last_act;
81 to_be_compared = expression->func_inst;
82 last_act = inst_act_map->get(to_be_compared);
84 last_read = last_act->get_reads_from_value();
85 write_val = write_act->get_write_value();
87 equality = (last_read == write_val);
88 if (equality != expression->value)
89 satisfy_predicate = false;
91 model_print("equality predicate\n");
94 model_print("nullity predicate, under construction\n");
97 model_print("unknown predicate token\n");
101 if (!satisfy_predicate)
105 /* TODO: collect all writes that satisfy predicate; if some of them violate
106 * modification graph, others can be chosen */
107 if (satisfy_predicate) {
108 model_print("^_^ satisfy predicate\n");
113 // TODO: make this thread sleep if no write satisfies the chosen predicate
117 void NewFuzzer::selectBranch(int thread_id, Predicate * curr_pred, FuncInst * read_inst)
119 if ( thrd_selected_child_branch.size() <= (uint) thread_id)
120 thrd_selected_child_branch.resize(thread_id + 1);
122 if (curr_pred == NULL || read_inst == NULL) {
123 thrd_selected_child_branch[thread_id] = NULL;
127 ModelVector<Predicate *> * children = curr_pred->get_children();
128 SnapVector<Predicate *> branches;
130 for (uint i = 0; i < children->size(); i++) {
131 Predicate * child = (*children)[i];
132 if (child->get_func_inst() == read_inst)
133 branches.push_back(child);
136 // predicate children have not been generated
137 if (branches.size() == 0) {
138 thrd_selected_child_branch[thread_id] = NULL;
142 // randomly select a branch
143 int random_index = random() % branches.size();
144 Predicate * random_branch = branches[ random_index ];
145 thrd_selected_child_branch[thread_id] = random_branch;
148 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
150 int thread_id = id_to_int(tid);
151 if (thrd_selected_child_branch.size() <= (uint) thread_id)
154 return thrd_selected_child_branch[thread_id];