10 #include "newfuzzer.h"
12 /** @brief Constructor */
13 ModelHistory::ModelHistory() :
14 func_counter(1), /* function id starts with 1 */
18 write_history(), // snapshot data structure
19 loc_func_nodes_map(), // shapshot data structure
20 thrd_last_entered_func() // snapshot data structure
23 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
25 //model_print("thread %d entering func %d\n", tid, func_id);
26 uint id = id_to_int(tid);
27 SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
28 SnapVector< SnapList<action_list_t *> *> *
29 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
31 if ( thrd_func_list->size() <= id ) {
32 uint oldsize = thrd_func_list->size();
33 thrd_func_list->resize( id + 1 );
34 thrd_func_act_lists->resize( id + 1 );
36 for (uint i = oldsize; i < id + 1; i++) {
37 new (&(*thrd_func_list)[i]) func_id_list_t();
38 // push 0 as a dummy function id to a void seg fault
39 (*thrd_func_list)[i].push_back(0);
41 (*thrd_func_act_lists)[i] = new SnapList<action_list_t *>();
45 while ( thrd_last_entered_func.size() <= id ) {
46 thrd_last_entered_func.push_back(0); // 0 is a dummy function id
49 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
50 func_act_lists->push_back( new action_list_t() );
52 uint32_t last_entered_func_id = thrd_last_entered_func[id];
53 thrd_last_entered_func[id] = func_id;
54 (*thrd_func_list)[id].push_back(func_id);
56 if ( func_nodes.size() <= func_id )
57 resize_func_nodes( func_id + 1 );
59 FuncNode * func_node = func_nodes[func_id];
60 func_node->init_predicate_tree_position(tid);
61 func_node->init_inst_act_map(tid);
63 /* Add edges between FuncNodes */
64 if (last_entered_func_id != 0) {
65 FuncNode * last_func_node = func_nodes[last_entered_func_id];
66 last_func_node->add_out_edge(func_node);
70 /* @param func_id a non-zero value */
71 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
73 uint32_t id = id_to_int(tid);
74 SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
75 SnapVector< SnapList<action_list_t *> *> *
76 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
78 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
79 uint32_t last_func_id = (*thrd_func_list)[id].back();
81 if (last_func_id == func_id) {
82 FuncNode * func_node = func_nodes[func_id];
83 func_node->set_predicate_tree_position(tid, NULL);
84 func_node->reset_inst_act_map(tid);
86 action_list_t * curr_act_list = func_act_lists->back();
88 /* defer the processing of curr_act_list until the function has exits a few times
89 * (currently twice) so that more information can be gathered to infer nullity predicates.
91 func_node->incr_exit_count();
92 if (func_node->get_exit_count() >= 2) {
93 SnapList<action_list_t *> * action_list_buffer = func_node->get_action_list_buffer();
94 while (action_list_buffer->size() > 0) {
95 action_list_t * act_list = action_list_buffer->back();
96 action_list_buffer->pop_back();
97 func_node->update_tree(act_list);
100 func_node->update_tree(curr_act_list);
102 func_node->get_action_list_buffer()->push_front(curr_act_list);
104 (*thrd_func_list)[id].pop_back();
105 func_act_lists->pop_back();
107 model_print("trying to exit with a wrong function id\n");
108 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
110 //model_print("thread %d exiting func %d\n", tid, func_id);
113 void ModelHistory::resize_func_nodes(uint32_t new_size)
115 uint32_t old_size = func_nodes.size();
117 if ( old_size < new_size )
118 func_nodes.resize(new_size);
120 for (uint32_t id = old_size; id < new_size; id++) {
121 const char * func_name = func_map_rev[id];
122 FuncNode * func_node = new FuncNode(this);
123 func_node->set_func_id(id);
124 func_node->set_func_name(func_name);
125 func_nodes[id] = func_node;
129 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
131 /* return if thread i has not entered any function or has exited
132 from all functions */
133 SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
134 SnapVector< SnapList<action_list_t *> *> *
135 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
137 uint32_t id = id_to_int(tid);
138 if ( thrd_func_list->size() <= id )
141 /* get the function id that thread i is currently in */
142 uint32_t func_id = (*thrd_func_list)[id].back();
143 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
145 if (act->is_write()) {
146 void * location = act->get_location();
147 uint64_t value = act->get_write_value();
148 update_write_history(location, value);
150 /* Update FuncNodes that may read from this location */
151 SnapList<FuncNode *> * func_nodes = loc_func_nodes_map.get(location);
152 if (func_nodes != NULL) {
153 sllnode<FuncNode *> * it = func_nodes->begin();
154 for (; it != NULL; it = it->getNext()) {
155 FuncNode * func_node = it->getVal();
156 func_node->add_to_val_loc_map(value, location);
161 /* the following does not care about actions without a position */
162 if (func_id == 0 || act->get_position() == NULL)
165 bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
167 action_list_t * curr_act_list = func_act_lists->back();
168 ASSERT(curr_act_list != NULL);
170 modelclock_t curr_seq_number = act->get_seq_number();
171 /* Skip actions that are second part of a read modify write or actions with the same sequence number */
172 if (curr_act_list->size() != 0) {
173 ModelAction * last_act = curr_act_list->back();
174 if (second_part_of_rmw || last_act->get_seq_number() == curr_seq_number)
178 /* skip actions that are paused by fuzzer (sequence number is 0) */
179 if (curr_seq_number == 0)
182 FuncNode * func_node = func_nodes[func_id];
184 /* add to curr_inst_list */
185 curr_act_list->push_back(act);
186 func_node->add_inst(act);
188 if (act->is_read()) {
189 func_node->update_inst_act_map(tid, act);
191 // Update predicate tree position
192 Fuzzer * fuzzer = model->get_execution()->getFuzzer();
193 Predicate * selected_branch = fuzzer->get_selected_child_branch(tid);
194 func_node->set_predicate_tree_position(tid, selected_branch);
198 /* return the FuncNode given its func_id */
199 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
201 if (func_nodes.size() <= func_id) // this node has not been added to func_nodes
204 return func_nodes[func_id];
207 void ModelHistory::update_write_history(void * location, uint64_t write_val)
209 value_set_t * write_set = write_history.get(location);
211 if (write_set == NULL) {
212 write_set = new value_set_t();
213 write_history.put(location, write_set);
216 write_set->add(write_val);
219 void ModelHistory::update_loc_func_nodes_map(void * location, FuncNode * node)
221 SnapList<FuncNode *> * func_node_list = loc_func_nodes_map.get(location);
222 if (func_node_list == NULL) {
223 func_node_list = new SnapList<FuncNode *>();
224 loc_func_nodes_map.put(location, func_node_list);
227 func_node_list->push_back(node);
230 void ModelHistory::update_loc_wr_func_nodes_map(void * location, FuncNode * node)
232 SnapList<FuncNode *> * func_node_list = loc_wr_func_nodes_map.get(location);
233 if (func_node_list == NULL) {
234 func_node_list = new SnapList<FuncNode *>();
235 loc_func_nodes_map.put(location, func_node_list);
238 func_node_list->push_back(node);
241 /* Reallocate some snapshotted memories when new executions start */
242 void ModelHistory::set_new_exec_flag()
244 for (uint i = 1; i < func_nodes.size(); i++) {
245 FuncNode * func_node = func_nodes[i];
246 func_node->set_new_exec_flag();
250 void ModelHistory::dump_func_node_graph()
252 model_print("digraph func_node_graph {\n");
253 for (uint i = 1; i < func_nodes.size(); i++) {
254 FuncNode * node = func_nodes[i];
255 ModelList<FuncNode *> * out_edges = node->get_out_edges();
257 model_print("\"%p\" [label=\"%s\"]\n", node, node->get_func_name());
258 mllnode<FuncNode *> * it;
259 for (it = out_edges->begin(); it != NULL; it = it->getNext()) {
260 FuncNode * other = it->getVal();
261 model_print("\"%p\" -> \"%p\"\n", node, other);
267 void ModelHistory::print_func_node()
269 /* function id starts with 1 */
270 for (uint32_t i = 1; i < func_nodes.size(); i++) {
271 FuncNode * func_node = func_nodes[i];
273 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
274 model_print("function %s has entry actions\n", func_node->get_func_name());
276 mllnode<FuncInst*>* it;
277 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
278 FuncInst *inst = it->getVal();
279 model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());