7 #include "concretepredicate.h"
10 #include "execution.h"
11 #include "newfuzzer.h"
13 /** @brief Constructor */
14 ModelHistory::ModelHistory() :
15 func_counter(1), /* function id starts with 1 */
20 /* The following are snapshot data structures */
21 write_history = new HashTable<void *, value_set_t *, uintptr_t, 4>();
22 loc_rd_func_nodes_map = new HashTable<void *, SnapVector<FuncNode *> *, uintptr_t, 0>();
23 loc_wr_func_nodes_map = new HashTable<void *, SnapVector<FuncNode *> *, uintptr_t, 0>();
24 loc_waiting_writes_map = new HashTable<void *, SnapVector<ConcretePredicate *> *, uintptr_t, 0>();
25 thrd_waiting_write = new SnapVector<ConcretePredicate *>();
26 func_inst_act_maps = new HashTable<uint32_t, SnapVector<inst_act_map_t *> *, int, 0>(128);
27 func_threads_map = new HashTable<uint32_t, SnapVector<thread_id_t> *, int, 0>(128);
30 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
32 //model_print("thread %d entering func %d\n", tid, func_id);
33 ModelExecution * execution = model->get_execution();
34 uint id = id_to_int(tid);
35 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
36 SnapVector< SnapList<action_list_t *> *> *
37 thrd_func_act_lists = execution->get_thrd_func_act_lists();
38 SnapVector<uint32_t> * thrd_last_entered_func = execution->get_thrd_last_entered_func();
40 if ( thrd_func_list->size() <= id ) {
41 uint oldsize = thrd_func_list->size();
42 thrd_func_list->resize( id + 1 );
43 thrd_func_act_lists->resize( id + 1 );
45 for (uint i = oldsize; i < id + 1; i++) {
46 // push 0 as a dummy function id to a void seg fault
47 new (&(*thrd_func_list)[i]) func_id_list_t();
48 (*thrd_func_list)[i].push_back(0);
50 (*thrd_func_act_lists)[i] = new SnapList<action_list_t *>();
51 thrd_last_entered_func->push_back(0);
55 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
56 func_act_lists->push_back( new action_list_t() );
58 uint32_t last_entered_func_id = (*thrd_last_entered_func)[id];
59 (*thrd_last_entered_func)[id] = func_id;
60 (*thrd_func_list)[id].push_back(func_id);
62 if ( func_nodes.size() <= func_id )
63 resize_func_nodes( func_id + 1 );
65 FuncNode * func_node = func_nodes[func_id];
66 func_node->init_predicate_tree_position(tid);
67 func_node->init_inst_act_map(tid);
69 /* Add edges between FuncNodes */
70 if (last_entered_func_id != 0) {
71 FuncNode * last_func_node = func_nodes[last_entered_func_id];
72 last_func_node->add_out_edge(func_node);
76 /* @param func_id a non-zero value */
77 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
79 ModelExecution * execution = model->get_execution();
80 uint32_t id = id_to_int(tid);
81 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
82 SnapVector< SnapList<action_list_t *> *> *
83 thrd_func_act_lists = execution->get_thrd_func_act_lists();
85 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
86 uint32_t last_func_id = (*thrd_func_list)[id].back();
88 if (last_func_id == func_id) {
89 FuncNode * func_node = func_nodes[func_id];
90 func_node->set_predicate_tree_position(tid, NULL);
91 func_node->reset_inst_act_map(tid);
93 action_list_t * curr_act_list = func_act_lists->back();
95 /* defer the processing of curr_act_list until the function has exits a few times
96 * (currently twice) so that more information can be gathered to infer nullity predicates.
98 func_node->incr_exit_count();
99 if (func_node->get_exit_count() >= 2) {
100 SnapList<action_list_t *> * action_list_buffer = func_node->get_action_list_buffer();
101 while (action_list_buffer->size() > 0) {
102 action_list_t * act_list = action_list_buffer->back();
103 action_list_buffer->pop_back();
104 func_node->update_tree(act_list);
107 func_node->update_tree(curr_act_list);
109 func_node->get_action_list_buffer()->push_front(curr_act_list);
111 (*thrd_func_list)[id].pop_back();
112 func_act_lists->pop_back();
114 model_print("trying to exit with a wrong function id\n");
115 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
117 //model_print("thread %d exiting func %d\n", tid, func_id);
120 void ModelHistory::resize_func_nodes(uint32_t new_size)
122 uint32_t old_size = func_nodes.size();
124 if ( old_size < new_size )
125 func_nodes.resize(new_size);
127 for (uint32_t id = old_size; id < new_size; id++) {
128 const char * func_name = func_map_rev[id];
129 FuncNode * func_node = new FuncNode(this);
130 func_node->set_func_id(id);
131 func_node->set_func_name(func_name);
132 func_nodes[id] = func_node;
136 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
138 ModelExecution * execution = model->get_execution();
139 /* Return if thread i has not entered any function or has exited
140 from all functions */
141 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
142 SnapVector< SnapList<action_list_t *> *> *
143 thrd_func_act_lists = execution->get_thrd_func_act_lists();
145 uint32_t id = id_to_int(tid);
146 if ( thrd_func_list->size() <= id )
149 /* Get the function id that thread i is currently in */
150 uint32_t func_id = (*thrd_func_list)[id].back();
151 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
153 if (act->is_write()) {
154 void * location = act->get_location();
155 uint64_t value = act->get_write_value();
156 update_write_history(location, value);
158 /* Update FuncNodes that may read from this location */
159 SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
160 for (uint i = 0; i < func_node_list->size(); i++) {
161 FuncNode * func_node = (*func_node_list)[i];
162 func_node->add_to_val_loc_map(value, location);
165 check_waiting_write(act);
168 /* The following does not care about actions without a position */
169 if (func_id == 0 || act->get_position() == NULL)
172 action_list_t * curr_act_list = func_act_lists->back();
173 ASSERT(curr_act_list != NULL);
175 if (skip_action(act, curr_act_list))
178 FuncNode * func_node = func_nodes[func_id];
180 /* Add to curr_inst_list */
181 curr_act_list->push_back(act);
182 func_node->add_inst(act);
184 if (act->is_read()) {
185 func_node->update_inst_act_map(tid, act);
187 // Update predicate tree position
188 Fuzzer * fuzzer = execution->getFuzzer();
189 Predicate * selected_branch = fuzzer->get_selected_child_branch(tid);
190 func_node->set_predicate_tree_position(tid, selected_branch);
194 /* Return the FuncNode given its func_id */
195 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
200 // This node has not been added to func_nodes
201 if (func_nodes.size() <= func_id)
204 return func_nodes[func_id];
207 /* Return the current FuncNode when given a thread id */
208 FuncNode * ModelHistory::get_curr_func_node(thread_id_t tid)
210 int thread_id = id_to_int(tid);
211 SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
212 uint32_t func_id = (*thrd_func_list)[thread_id].back();
215 return func_nodes[func_id];
221 void ModelHistory::update_write_history(void * location, uint64_t write_val)
223 value_set_t * write_set = write_history->get(location);
225 if (write_set == NULL) {
226 write_set = new value_set_t();
227 write_history->put(location, write_set);
230 write_set->add(write_val);
233 void ModelHistory::update_loc_rd_func_nodes_map(void * location, FuncNode * node)
235 SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
236 func_node_list->push_back(node);
239 void ModelHistory::update_loc_wr_func_nodes_map(void * location, FuncNode * node)
241 SnapVector<FuncNode *> * func_node_list = getWrFuncNodes(location);
242 func_node_list->push_back(node);
245 SnapVector<FuncNode *> * ModelHistory::getRdFuncNodes(void * location)
247 SnapVector<FuncNode *> * func_node_list = loc_rd_func_nodes_map->get(location);
248 if (func_node_list == NULL) {
249 func_node_list = new SnapVector<FuncNode *>();
250 loc_wr_func_nodes_map->put(location, func_node_list);
253 return func_node_list;
256 SnapVector<FuncNode *> * ModelHistory::getWrFuncNodes(void * location)
258 SnapVector<FuncNode *> * func_node_list = loc_wr_func_nodes_map->get(location);
259 if (func_node_list == NULL) {
260 func_node_list = new SnapVector<FuncNode *>();
261 loc_wr_func_nodes_map->put(location, func_node_list);
264 return func_node_list;
267 /* When a thread is paused by Fuzzer, keep track of the condition it is waiting for */
268 void ModelHistory::add_waiting_write(ConcretePredicate * concrete)
270 void * location = concrete->get_location();
271 SnapVector<ConcretePredicate *> * waiting_conditions = loc_waiting_writes_map->get(location);
272 if (waiting_conditions == NULL) {
273 waiting_conditions = new SnapVector<ConcretePredicate *>();
274 loc_waiting_writes_map->put(location, waiting_conditions);
277 /* waiting_conditions should not have duplications */
278 waiting_conditions->push_back(concrete);
280 int thread_id = id_to_int(concrete->get_tid());
281 if (thrd_waiting_write->size() <= (uint) thread_id) {
282 thrd_waiting_write->resize(thread_id + 1);
285 (*thrd_waiting_write)[thread_id] = concrete;
288 void ModelHistory::remove_waiting_write(thread_id_t tid)
290 ConcretePredicate * concrete = (*thrd_waiting_write)[ id_to_int(tid) ];
291 void * location = concrete->get_location();
292 SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
294 for (uint i = 0; i < concrete_preds->size(); i++) {
295 ConcretePredicate * current = (*concrete_preds)[i];
296 if (concrete == current) {
297 (*concrete_preds)[i] = concrete_preds->back();
298 concrete_preds->pop_back();
303 int thread_id = id_to_int( concrete->get_tid() );
304 (*thrd_waiting_write)[thread_id] = NULL;
308 /* Check if any other thread is waiting for this write action. If so, "notify" them */
309 void ModelHistory::check_waiting_write(ModelAction * write_act)
311 void * location = write_act->get_location();
312 uint64_t value = write_act->get_write_value();
313 SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
314 SnapVector<ConcretePredicate *> to_remove = SnapVector<ConcretePredicate *>();
315 if (concrete_preds == NULL)
319 while (index < concrete_preds->size()) {
320 ConcretePredicate * concrete_pred = (*concrete_preds)[index];
321 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
322 bool satisfy_predicate = true;
323 /* Check if the written value satisfies every predicate expression */
324 for (uint i = 0; i < concrete_exprs->size(); i++) {
325 struct concrete_pred_expr concrete = (*concrete_exprs)[i];
327 switch (concrete.token) {
329 equality = (value == concrete.value);
332 equality = ((void*)value == NULL);
335 model_print("unknown predicate token");
339 if (equality != concrete.equality) {
340 satisfy_predicate = false;
345 if (satisfy_predicate) {
346 to_remove.push_back(concrete_pred);
352 for (uint i = 0; i < to_remove.size(); i++) {
353 ConcretePredicate * concrete_pred = to_remove[i];
355 /* Wake up threads */
356 thread_id_t tid = concrete_pred->get_tid();
357 Thread * thread = model->get_thread(tid);
359 model_print("** thread %d is woken up\n", thread->get_id());
360 model->get_execution()->getFuzzer()->notify_paused_thread(thread);
364 SnapVector<inst_act_map_t *> * ModelHistory::getThrdInstActMap(uint32_t func_id)
366 ASSERT(func_id != 0);
368 SnapVector<inst_act_map_t *> * maps = func_inst_act_maps->get(func_id);
370 maps = new SnapVector<inst_act_map_t *>();
371 func_inst_act_maps->put(func_id, maps);
377 bool ModelHistory::skip_action(ModelAction * act, SnapList<ModelAction *> * curr_act_list)
379 bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
380 modelclock_t curr_seq_number = act->get_seq_number();
382 /* Skip actions that are second part of a read modify write */
383 if (second_part_of_rmw)
386 /* Skip actions with the same sequence number */
387 if (curr_act_list->size() != 0) {
388 ModelAction * last_act = curr_act_list->back();
389 if (last_act->get_seq_number() == curr_seq_number)
393 /* Skip actions that are paused by fuzzer (sequence number is 0) */
394 if (curr_seq_number == 0)
400 void ModelHistory::update_func_threads_map(uint32_t func_id, thread_id_t tid)
402 SnapVector<thread_id_t> * thread_ids = get_calling_threads(func_id);
403 thread_ids->push_back(tid);
406 /* Return a vector of thread_id's that have called this function before */
407 SnapVector<thread_id_t> * ModelHistory::get_calling_threads(uint32_t func_id)
409 SnapVector<thread_id_t> * thread_ids = func_threads_map->get(func_id);
410 if (thread_ids == NULL) {
411 /* Make sure the result can be iterated without checking nullity */
412 thread_ids = new SnapVector<thread_id_t>();
413 func_threads_map->put(func_id, thread_ids);
419 /* Reallocate some snapshotted memories when new executions start */
420 void ModelHistory::set_new_exec_flag()
422 for (uint i = 1; i < func_nodes.size(); i++) {
423 FuncNode * func_node = func_nodes[i];
424 func_node->set_new_exec_flag();
428 void ModelHistory::dump_func_node_graph()
430 model_print("digraph func_node_graph {\n");
431 for (uint i = 1; i < func_nodes.size(); i++) {
432 FuncNode * node = func_nodes[i];
433 ModelList<FuncNode *> * out_edges = node->get_out_edges();
435 model_print("\"%p\" [label=\"%s\"]\n", node, node->get_func_name());
436 mllnode<FuncNode *> * it;
437 for (it = out_edges->begin(); it != NULL; it = it->getNext()) {
438 FuncNode * other = it->getVal();
439 model_print("\"%p\" -> \"%p\"\n", node, other);
445 void ModelHistory::print_func_node()
447 /* function id starts with 1 */
448 for (uint32_t i = 1; i < func_nodes.size(); i++) {
449 FuncNode * func_node = func_nodes[i];
451 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
452 model_print("function %s has entry actions\n", func_node->get_func_name());
454 mllnode<FuncInst*>* it;
455 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
456 FuncInst *inst = it->getVal();
457 model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());