fix bug by changing MEMALLOC to SNAPSHOTALLOC
[c11tester.git] / newfuzzer.cc
1 #include "newfuzzer.h"
2 #include "threads-model.h"
3 #include "model.h"
4 #include "action.h"
5 #include "execution.h"
6 #include "funcnode.h"
7
8 NewFuzzer::NewFuzzer() :
9         thrd_last_read_act(),
10         thrd_curr_pred(),
11         thrd_selected_child_branch(),
12         thrd_pruned_writes()
13 {}
14
15 /**
16  * @brief Register the ModelHistory and ModelExecution engine
17  */
18 void NewFuzzer::register_engine(ModelHistory * history, ModelExecution *execution)
19 {
20         this->history = history;
21         this->execution = execution;
22 }
23
24 int NewFuzzer::selectWrite(ModelAction *read, SnapVector<ModelAction *> * rf_set)
25 {
26         thread_id_t tid = read->get_tid();
27         int thread_id = id_to_int(tid);
28
29         if (thrd_last_read_act.size() <= (uint) thread_id)
30                 thrd_last_read_act.resize(thread_id + 1);
31
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;
35
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);
42
43                 Predicate * selected_branch = selectBranch(tid, curr_pred, read_inst);
44                 prune_writes(tid, selected_branch, rf_set, inst_act_map);
45         }
46
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] );
53         }
54
55         ASSERT(rf_set->size() != 0);
56         int random_index = random() % rf_set->size();
57
58         return random_index;
59 }
60
61 /* Select a random branch from the children of curr_pred 
62  * @return The selected branch
63  */
64 Predicate * NewFuzzer::selectBranch(thread_id_t tid, Predicate * curr_pred, FuncInst * read_inst)
65 {
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);
69
70         if (curr_pred == NULL || read_inst == NULL) {
71                 thrd_selected_child_branch[thread_id] = NULL;
72                 return NULL;
73         }
74
75         ModelVector<Predicate *> * children = curr_pred->get_children();
76         SnapVector<Predicate *> branches;
77
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);
82         }
83
84         // predicate children have not been generated
85         if (branches.size() == 0) {
86                 thrd_selected_child_branch[thread_id] = NULL;
87                 return NULL;
88         }
89
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;
94
95         return random_branch;
96 }
97
98 Predicate * NewFuzzer::get_selected_child_branch(thread_id_t tid)
99 {
100         int thread_id = id_to_int(tid);
101         if (thrd_selected_child_branch.size() <= (uint) thread_id)
102                 return NULL;
103
104         return thrd_selected_child_branch[thread_id];
105 }
106
107 /* Remove writes from the rf_set that do not satisfie the selected predicate, 
108  * and store them in thrd_pruned_writes
109  *
110  * @return true if rf_set is pruned
111  */
112 bool NewFuzzer::prune_writes(thread_id_t tid, Predicate * pred,
113         SnapVector<ModelAction *> * rf_set, inst_act_map_t * inst_act_map)
114 {
115         if (pred == NULL)
116                 return false;
117
118         PredExprSet * pred_expressions = pred->get_pred_expressions();
119         if (pred_expressions->getSize() == 0)   // unset predicates
120                 return false;
121
122         int thread_id = id_to_int(tid);
123         uint old_size = thrd_pruned_writes.size();
124         if (thrd_pruned_writes.size() <= (uint) thread_id) {
125                 uint new_size = thread_id + 1;
126                 thrd_pruned_writes.resize(new_size);
127                 for (uint i = old_size; i < new_size; i++)
128                         thrd_pruned_writes[i] = new SnapVector<ModelAction *>();
129         }
130         SnapVector<ModelAction *> * pruned_writes = thrd_pruned_writes[thread_id];
131         pruned_writes->clear(); // clear the old pruned_writes set
132
133         bool pruned = false;
134         uint index = 0;
135         while ( index < rf_set->size() ) {
136                 ModelAction * write_act = (*rf_set)[index];
137                 bool satisfy_predicate = true;
138
139                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
140                 while (pred_expr_it->hasNext()) {
141                         struct pred_expr * expression = pred_expr_it->next();
142                         uint64_t write_val = write_act->get_write_value();
143                         bool equality;
144
145                         // No predicate, return false
146                         if (expression->token == NOPREDICATE)
147                                 return pruned;
148
149                         switch(expression->token) {
150                                 case EQUALITY:
151                                         FuncInst * to_be_compared;
152                                         ModelAction * last_act;
153                                         uint64_t last_read;
154
155                                         to_be_compared = expression->func_inst;
156                                         last_act = inst_act_map->get(to_be_compared);
157                                         last_read = last_act->get_reads_from_value();
158
159                                         equality = (write_val == last_read);
160                                         if (equality != expression->value)
161                                                 satisfy_predicate = false;
162                                         break;
163                                 case NULLITY:
164                                         equality = ((void*)write_val == NULL);
165                                         if (equality != expression->value)
166                                                 satisfy_predicate = false;
167                                         break;
168                                 default:
169                                         model_print("unknown predicate token\n");
170                                         break;
171                         }
172
173                         if (!satisfy_predicate)
174                                 break;
175                 }
176
177                 if (!satisfy_predicate) {
178                         ASSERT(rf_set != NULL);
179                         (*rf_set)[index] = rf_set->back();
180                         rf_set->pop_back();
181                         pruned_writes->push_back(write_act);
182                         pruned = true;
183                 } else
184                         index++;
185         }
186
187         return pruned;
188 }