7 #include "concretepredicate.h"
11 #include "execution.h"
12 #include "newfuzzer.h"
14 /** @brief Constructor */
15 ModelHistory::ModelHistory() :
16 func_counter(1), /* function id starts with 1 */
21 /* The following are snapshot data structures */
22 write_history = new HashTable<void *, value_set_t *, uintptr_t, 4>();
23 loc_rd_func_nodes_map = new HashTable<void *, SnapVector<FuncNode *> *, uintptr_t, 0>();
24 loc_wr_func_nodes_map = new HashTable<void *, SnapVector<FuncNode *> *, uintptr_t, 0>();
25 loc_waiting_writes_map = new HashTable<void *, SnapVector<ConcretePredicate *> *, uintptr_t, 0>();
26 thrd_waiting_write = new SnapVector<ConcretePredicate *>();
27 thrd_wait_obj = new SnapVector<WaitObj *>();
28 func_inst_act_maps = new HashTable<uint32_t, SnapVector<inst_act_map_t *> *, int, 0>(128);
31 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
33 //model_print("thread %d entering func %d\n", tid, func_id);
34 ModelExecution * execution = model->get_execution();
35 uint id = id_to_int(tid);
36 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
37 SnapVector< SnapList<action_list_t *> *> *
38 thrd_func_act_lists = execution->get_thrd_func_act_lists();
39 SnapVector<uint32_t> * thrd_last_entered_func = execution->get_thrd_last_entered_func();
41 if ( thrd_func_list->size() <= id ) {
42 uint oldsize = thrd_func_list->size();
43 thrd_func_list->resize( id + 1 );
44 thrd_func_act_lists->resize( id + 1 );
46 for (uint i = oldsize; i < id + 1; i++) {
47 // push 0 as a dummy function id to a void seg fault
48 new (&(*thrd_func_list)[i]) func_id_list_t();
49 (*thrd_func_list)[i].push_back(0);
51 (*thrd_func_act_lists)[i] = new SnapList<action_list_t *>();
52 thrd_last_entered_func->push_back(0);
56 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
57 func_act_lists->push_back( new action_list_t() );
59 uint32_t last_entered_func_id = (*thrd_last_entered_func)[id];
60 (*thrd_last_entered_func)[id] = func_id;
61 (*thrd_func_list)[id].push_back(func_id);
63 if ( func_nodes.size() <= func_id )
64 resize_func_nodes( func_id + 1 );
66 FuncNode * func_node = func_nodes[func_id];
67 func_node->init_predicate_tree_position(tid);
68 func_node->init_inst_act_map(tid);
70 /* Add edges between FuncNodes */
71 if (last_entered_func_id != 0) {
72 FuncNode * last_func_node = func_nodes[last_entered_func_id];
73 last_func_node->add_out_edge(func_node);
77 /* @param func_id a non-zero value */
78 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
80 ModelExecution * execution = model->get_execution();
81 uint32_t id = id_to_int(tid);
82 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
83 SnapVector< SnapList<action_list_t *> *> *
84 thrd_func_act_lists = execution->get_thrd_func_act_lists();
86 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
87 uint32_t last_func_id = (*thrd_func_list)[id].back();
89 if (last_func_id == func_id) {
90 FuncNode * func_node = func_nodes[func_id];
91 func_node->set_predicate_tree_position(tid, NULL);
92 func_node->reset_inst_act_map(tid);
94 action_list_t * curr_act_list = func_act_lists->back();
96 /* defer the processing of curr_act_list until the function has exits a few times
97 * (currently twice) so that more information can be gathered to infer nullity predicates.
99 func_node->incr_exit_count();
100 if (func_node->get_exit_count() >= 2) {
101 SnapList<action_list_t *> * action_list_buffer = func_node->get_action_list_buffer();
102 while (action_list_buffer->size() > 0) {
103 action_list_t * act_list = action_list_buffer->back();
104 action_list_buffer->pop_back();
105 func_node->update_tree(act_list);
108 func_node->update_tree(curr_act_list);
110 func_node->get_action_list_buffer()->push_front(curr_act_list);
112 (*thrd_func_list)[id].pop_back();
113 func_act_lists->pop_back();
115 model_print("trying to exit with a wrong function id\n");
116 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
118 //model_print("thread %d exiting func %d\n", tid, func_id);
121 void ModelHistory::resize_func_nodes(uint32_t new_size)
123 uint32_t old_size = func_nodes.size();
125 if ( old_size < new_size )
126 func_nodes.resize(new_size);
128 for (uint32_t id = old_size; id < new_size; id++) {
129 const char * func_name = func_map_rev[id];
130 FuncNode * func_node = new FuncNode(this);
131 func_node->set_func_id(id);
132 func_node->set_func_name(func_name);
133 func_nodes[id] = func_node;
137 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
139 ModelExecution * execution = model->get_execution();
140 /* Return if thread i has not entered any function or has exited
141 from all functions */
142 SnapVector<func_id_list_t> * thrd_func_list = execution->get_thrd_func_list();
143 SnapVector< SnapList<action_list_t *> *> *
144 thrd_func_act_lists = execution->get_thrd_func_act_lists();
146 uint32_t id = id_to_int(tid);
147 if ( thrd_func_list->size() <= id )
150 /* Get the function id that thread i is currently in */
151 uint32_t func_id = (*thrd_func_list)[id].back();
152 SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
154 if (act->is_write()) {
155 void * location = act->get_location();
156 uint64_t value = act->get_write_value();
157 update_write_history(location, value);
159 /* Update FuncNodes that may read from this location */
160 SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
161 for (uint i = 0; i < func_node_list->size(); i++) {
162 FuncNode * func_node = (*func_node_list)[i];
163 func_node->add_to_val_loc_map(value, location);
166 check_waiting_write(act);
169 /* The following does not care about actions without a position */
170 if (func_id == 0 || act->get_position() == NULL)
173 action_list_t * curr_act_list = func_act_lists->back();
174 ASSERT(curr_act_list != NULL);
176 if (skip_action(act, curr_act_list))
179 FuncNode * func_node = func_nodes[func_id];
181 /* Add to curr_inst_list */
182 curr_act_list->push_back(act);
183 func_node->add_inst(act);
185 if (act->is_read()) {
186 func_node->update_inst_act_map(tid, act);
188 // Update predicate tree position
189 Fuzzer * fuzzer = execution->getFuzzer();
190 Predicate * selected_branch = fuzzer->get_selected_child_branch(tid);
191 func_node->set_predicate_tree_position(tid, selected_branch);
195 /* Return the FuncNode given its func_id */
196 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
201 // This node has not been added to func_nodes
202 if (func_nodes.size() <= func_id)
205 return func_nodes[func_id];
208 /* Return the current FuncNode when given a thread id */
209 FuncNode * ModelHistory::get_curr_func_node(thread_id_t tid)
211 int thread_id = id_to_int(tid);
212 SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
213 uint32_t func_id = (*thrd_func_list)[thread_id].back();
216 return func_nodes[func_id];
222 void ModelHistory::update_write_history(void * location, uint64_t write_val)
224 value_set_t * write_set = write_history->get(location);
226 if (write_set == NULL) {
227 write_set = new value_set_t();
228 write_history->put(location, write_set);
231 write_set->add(write_val);
234 void ModelHistory::update_loc_rd_func_nodes_map(void * location, FuncNode * node)
236 SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
237 func_node_list->push_back(node);
240 void ModelHistory::update_loc_wr_func_nodes_map(void * location, FuncNode * node)
242 SnapVector<FuncNode *> * func_node_list = getWrFuncNodes(location);
243 func_node_list->push_back(node);
246 SnapVector<FuncNode *> * ModelHistory::getRdFuncNodes(void * location)
248 SnapVector<FuncNode *> * func_node_list = loc_rd_func_nodes_map->get(location);
249 if (func_node_list == NULL) {
250 func_node_list = new SnapVector<FuncNode *>();
251 loc_rd_func_nodes_map->put(location, func_node_list);
254 return func_node_list;
257 SnapVector<FuncNode *> * ModelHistory::getWrFuncNodes(void * location)
259 SnapVector<FuncNode *> * func_node_list = loc_wr_func_nodes_map->get(location);
260 if (func_node_list == NULL) {
261 func_node_list = new SnapVector<FuncNode *>();
262 loc_wr_func_nodes_map->put(location, func_node_list);
265 return func_node_list;
268 /* When a thread is paused by Fuzzer, keep track of the condition it is waiting for */
269 void ModelHistory::add_waiting_write(ConcretePredicate * concrete)
271 void * location = concrete->get_location();
272 SnapVector<ConcretePredicate *> * waiting_conditions = loc_waiting_writes_map->get(location);
273 if (waiting_conditions == NULL) {
274 waiting_conditions = new SnapVector<ConcretePredicate *>();
275 loc_waiting_writes_map->put(location, waiting_conditions);
278 /* waiting_conditions should not have duplications */
279 waiting_conditions->push_back(concrete);
281 int thread_id = id_to_int(concrete->get_tid());
282 if (thrd_waiting_write->size() <= (uint) thread_id) {
283 thrd_waiting_write->resize(thread_id + 1);
286 (*thrd_waiting_write)[thread_id] = concrete;
289 void ModelHistory::remove_waiting_write(thread_id_t tid)
291 ConcretePredicate * concrete = (*thrd_waiting_write)[ id_to_int(tid) ];
292 void * location = concrete->get_location();
293 SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
295 /* Linear search should be fine because presumably not many ConcretePredicates
296 * are at the same memory location */
297 for (uint i = 0; i < concrete_preds->size(); i++) {
298 ConcretePredicate * current = (*concrete_preds)[i];
299 if (concrete == current) {
300 (*concrete_preds)[i] = concrete_preds->back();
301 concrete_preds->pop_back();
306 int thread_id = id_to_int( concrete->get_tid() );
307 (*thrd_waiting_write)[thread_id] = NULL;
311 /* Check if any other thread is waiting for this write action. If so, "notify" them */
312 void ModelHistory::check_waiting_write(ModelAction * write_act)
314 void * location = write_act->get_location();
315 uint64_t value = write_act->get_write_value();
316 SnapVector<ConcretePredicate *> * concrete_preds = loc_waiting_writes_map->get(location);
317 SnapVector<ConcretePredicate *> to_remove = SnapVector<ConcretePredicate *>();
318 if (concrete_preds == NULL)
322 while (index < concrete_preds->size()) {
323 ConcretePredicate * concrete_pred = (*concrete_preds)[index];
324 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
325 bool satisfy_predicate = true;
326 /* Check if the written value satisfies every predicate expression */
327 for (uint i = 0; i < concrete_exprs->size(); i++) {
328 struct concrete_pred_expr concrete = (*concrete_exprs)[i];
330 switch (concrete.token) {
332 equality = (value == concrete.value);
335 equality = ((void*)value == NULL);
338 model_print("unknown predicate token");
342 if (equality != concrete.equality) {
343 satisfy_predicate = false;
348 if (satisfy_predicate) {
349 to_remove.push_back(concrete_pred);
355 for (uint i = 0; i < to_remove.size(); i++) {
356 ConcretePredicate * concrete_pred = to_remove[i];
358 /* Wake up threads */
359 thread_id_t tid = concrete_pred->get_tid();
360 Thread * thread = model->get_thread(tid);
362 model_print("** thread %d is woken up\n", thread->get_id());
363 model->get_execution()->getFuzzer()->notify_paused_thread(thread);
367 WaitObj * ModelHistory::getWaitObj(thread_id_t tid)
369 int thread_id = id_to_int(tid);
370 int old_size = thrd_wait_obj->size();
371 if (old_size <= thread_id) {
372 thrd_wait_obj->resize(thread_id + 1);
373 for (int i = old_size; i < thread_id + 1; i++) {
374 (*thrd_wait_obj)[i] = new WaitObj( int_to_id(i) );
378 return (*thrd_wait_obj)[thread_id];
381 void ModelHistory::add_waiting_thread(thread_id_t self_id,
382 thread_id_t waiting_for_id, int dist)
384 WaitObj * self_wait_obj = getWaitObj(self_id);
385 self_wait_obj->add_waiting_for(waiting_for_id, dist);
387 /* Update waited-by relation */
388 WaitObj * other_wait_obj = getWaitObj(waiting_for_id);
389 other_wait_obj->add_waited_by(self_id);
392 void ModelHistory::remove_waiting_thread(thread_id_t tid)
394 WaitObj * self_wait_obj = getWaitObj(tid);
395 thrd_id_set_t * waiting_for = self_wait_obj->getWaitingFor();
397 thrd_id_set_iter * iter = waiting_for->iterator();
398 while (iter->hasNext()) {
399 thread_id_t other_id = iter->next();
400 WaitObj * other_wait_obj = getWaitObj(other_id);
401 other_wait_obj->remove_waited_by(tid);
404 waiting_for->reset();
407 SnapVector<inst_act_map_t *> * ModelHistory::getThrdInstActMap(uint32_t func_id)
409 ASSERT(func_id != 0);
411 SnapVector<inst_act_map_t *> * maps = func_inst_act_maps->get(func_id);
413 maps = new SnapVector<inst_act_map_t *>();
414 func_inst_act_maps->put(func_id, maps);
420 bool ModelHistory::skip_action(ModelAction * act, SnapList<ModelAction *> * curr_act_list)
422 bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
423 modelclock_t curr_seq_number = act->get_seq_number();
425 /* Skip actions that are second part of a read modify write */
426 if (second_part_of_rmw)
429 /* Skip actions with the same sequence number */
430 if (curr_act_list->size() != 0) {
431 ModelAction * last_act = curr_act_list->back();
432 if (last_act->get_seq_number() == curr_seq_number)
436 /* Skip actions that are paused by fuzzer (sequence number is 0) */
437 if (curr_seq_number == 0)
443 /* Reallocate some snapshotted memories when new executions start */
444 void ModelHistory::set_new_exec_flag()
446 for (uint i = 1; i < func_nodes.size(); i++) {
447 FuncNode * func_node = func_nodes[i];
448 func_node->set_new_exec_flag();
452 void ModelHistory::dump_func_node_graph()
454 model_print("digraph func_node_graph {\n");
455 for (uint i = 1; i < func_nodes.size(); i++) {
456 FuncNode * node = func_nodes[i];
457 ModelList<FuncNode *> * out_edges = node->get_out_edges();
459 model_print("\"%p\" [label=\"%s\"]\n", node, node->get_func_name());
460 mllnode<FuncNode *> * it;
461 for (it = out_edges->begin(); it != NULL; it = it->getNext()) {
462 FuncNode * other = it->getVal();
463 model_print("\"%p\" -> \"%p\"\n", node, other);
469 void ModelHistory::print_func_node()
471 /* function id starts with 1 */
472 for (uint32_t i = 1; i < func_nodes.size(); i++) {
473 FuncNode * func_node = func_nodes[i];
475 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
476 model_print("function %s has entry actions\n", func_node->get_func_name());
478 mllnode<FuncInst*>* it;
479 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
480 FuncInst *inst = it->getVal();
481 model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
486 void ModelHistory::print_waiting_threads()
488 ModelExecution * execution = model->get_execution();
489 for (unsigned int i = 0; i < execution->get_num_threads();i++) {
490 thread_id_t tid = int_to_id(i);
491 WaitObj * wait_obj = getWaitObj(tid);
492 wait_obj->print_waiting_for();
495 for (unsigned int i = 0; i < execution->get_num_threads();i++) {
496 thread_id_t tid = int_to_id(i);
497 WaitObj * wait_obj = getWaitObj(tid);
498 wait_obj->print_waited_by();