Toward computing which threads a paused thread may wait for
[c11tester.git] / history.cc
1 #include <inttypes.h>
2 #include "history.h"
3 #include "action.h"
4 #include "funcnode.h"
5 #include "funcinst.h"
6 #include "common.h"
7 #include "concretepredicate.h"
8
9 #include "model.h"
10 #include "execution.h"
11 #include "newfuzzer.h"
12
13 /** @brief Constructor */
14 ModelHistory::ModelHistory() :
15         func_counter(1),        /* function id starts with 1 */
16         func_map(),
17         func_map_rev(),
18         func_nodes()
19 {
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);
28 }
29
30 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
31 {
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();
39
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 );
44
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);
49
50                         (*thrd_func_act_lists)[i] = new SnapList<action_list_t *>();
51                         thrd_last_entered_func->push_back(0);
52                 }
53         }
54
55         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
56         func_act_lists->push_back( new action_list_t() );
57
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);
61
62         if ( func_nodes.size() <= func_id )
63                 resize_func_nodes( func_id + 1 );
64
65         FuncNode * func_node = func_nodes[func_id];
66         func_node->init_predicate_tree_position(tid);
67         func_node->init_inst_act_map(tid);
68
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);
73         }
74 }
75
76 /* @param func_id a non-zero value */
77 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
78 {
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();
84
85         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
86         uint32_t last_func_id = (*thrd_func_list)[id].back();
87
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);
92
93                 action_list_t * curr_act_list = func_act_lists->back();
94
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.
97                  */
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);
105                         }
106
107                         func_node->update_tree(curr_act_list);
108                 } else
109                         func_node->get_action_list_buffer()->push_front(curr_act_list);
110
111                 (*thrd_func_list)[id].pop_back();
112                 func_act_lists->pop_back();
113         } else {
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);
116         }
117         //model_print("thread %d exiting func %d\n", tid, func_id);
118 }
119
120 void ModelHistory::resize_func_nodes(uint32_t new_size)
121 {
122         uint32_t old_size = func_nodes.size();
123
124         if ( old_size < new_size )
125                 func_nodes.resize(new_size);
126
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;
133         }
134 }
135
136 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
137 {
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();
144
145         uint32_t id = id_to_int(tid);
146         if ( thrd_func_list->size() <= id )
147                 return;
148
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];
152
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);
157
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);
163                 }
164
165                 check_waiting_write(act);
166         }
167
168         /* The following does not care about actions without a position */
169         if (func_id == 0 || act->get_position() == NULL)
170                 return;
171
172         action_list_t * curr_act_list = func_act_lists->back();
173         ASSERT(curr_act_list != NULL);
174
175         if (skip_action(act, curr_act_list))
176                 return;
177
178         FuncNode * func_node = func_nodes[func_id];
179
180         /* Add to curr_inst_list */
181         curr_act_list->push_back(act);
182         func_node->add_inst(act);
183
184         if (act->is_read()) {
185                 func_node->update_inst_act_map(tid, act);
186
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);
191         }
192 }
193
194 /* Return the FuncNode given its func_id  */
195 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
196 {
197         if (func_id == 0)
198                 return NULL;
199
200         // This node has not been added to func_nodes
201         if (func_nodes.size() <= func_id)
202                 return NULL;
203
204         return func_nodes[func_id];
205 }
206
207 /* Return the current FuncNode when given a thread id */
208 FuncNode * ModelHistory::get_curr_func_node(thread_id_t tid)
209 {
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();
213
214         if (func_id != 0) {
215                 return func_nodes[func_id];
216         }
217
218         return NULL;
219 }
220
221 void ModelHistory::update_write_history(void * location, uint64_t write_val)
222 {
223         value_set_t * write_set = write_history->get(location);
224
225         if (write_set == NULL) {
226                 write_set = new value_set_t();
227                 write_history->put(location, write_set);
228         }
229
230         write_set->add(write_val);
231 }
232
233 void ModelHistory::update_loc_rd_func_nodes_map(void * location, FuncNode * node)
234 {
235         SnapVector<FuncNode *> * func_node_list = getRdFuncNodes(location);
236         func_node_list->push_back(node);
237 }
238
239 void ModelHistory::update_loc_wr_func_nodes_map(void * location, FuncNode * node)
240 {
241         SnapVector<FuncNode *> * func_node_list = getWrFuncNodes(location);
242         func_node_list->push_back(node);
243 }
244
245 SnapVector<FuncNode *> * ModelHistory::getRdFuncNodes(void * location)
246 {
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);
251         }
252
253         return func_node_list;
254 }
255
256 SnapVector<FuncNode *> * ModelHistory::getWrFuncNodes(void * location)
257 {
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);
262         }
263
264         return func_node_list;
265 }
266
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)
269 {
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);
275         }
276
277         /* waiting_conditions should not have duplications */
278         waiting_conditions->push_back(concrete);
279
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);
283         }
284
285         (*thrd_waiting_write)[thread_id] = concrete;
286 }
287
288 void ModelHistory::remove_waiting_write(thread_id_t tid)
289 {
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);
293
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();
299                         break;
300                 }
301         }
302
303         int thread_id = id_to_int( concrete->get_tid() );
304         (*thrd_waiting_write)[thread_id] = NULL;
305         delete concrete;
306 }
307
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)
310 {
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)
316                 return;
317
318         uint index = 0;
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];
326                         bool equality;
327                         switch (concrete.token) {
328                                 case EQUALITY:
329                                         equality = (value == concrete.value);
330                                         break;
331                                 case NULLITY:
332                                         equality = ((void*)value == NULL);
333                                         break;
334                                 default:
335                                         model_print("unknown predicate token");
336                                         break;
337                         }
338
339                         if (equality != concrete.equality) {
340                                 satisfy_predicate = false;
341                                 break;
342                         }
343                 }
344
345                 if (satisfy_predicate) {
346                         to_remove.push_back(concrete_pred);
347                 }
348
349                 index++;
350         }
351
352         for (uint i = 0; i < to_remove.size(); i++) {
353                 ConcretePredicate * concrete_pred = to_remove[i];
354
355                 /* Wake up threads */
356                 thread_id_t tid = concrete_pred->get_tid();
357                 Thread * thread = model->get_thread(tid);
358
359                 model_print("** thread %d is woken up\n", thread->get_id());
360                 model->get_execution()->getFuzzer()->notify_paused_thread(thread);
361         }
362 }
363
364 SnapVector<inst_act_map_t *> * ModelHistory::getThrdInstActMap(uint32_t func_id)
365 {
366         ASSERT(func_id != 0);
367
368         SnapVector<inst_act_map_t *> * maps = func_inst_act_maps->get(func_id);
369         if (maps == NULL) {
370                 maps = new SnapVector<inst_act_map_t *>();
371                 func_inst_act_maps->put(func_id, maps);
372         }
373
374         return maps;
375 }
376
377 bool ModelHistory::skip_action(ModelAction * act, SnapList<ModelAction *> * curr_act_list)
378 {
379         bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
380         modelclock_t curr_seq_number = act->get_seq_number();
381
382         /* Skip actions that are second part of a read modify write */
383         if (second_part_of_rmw)
384                 return true;
385
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)
390                         return true;
391         }
392
393         /* Skip actions that are paused by fuzzer (sequence number is 0) */
394         if (curr_seq_number == 0)
395                 return true;
396
397         return false;
398 }
399
400 void ModelHistory::update_func_threads_map(uint32_t func_id, thread_id_t tid)
401 {
402         SnapVector<thread_id_t> * thread_ids = get_calling_threads(func_id);
403         thread_ids->push_back(tid);
404 }
405
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)
408 {
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);
414         }
415
416         return thread_ids;
417 }
418
419 /* Reallocate some snapshotted memories when new executions start */
420 void ModelHistory::set_new_exec_flag()
421 {
422         for (uint i = 1; i < func_nodes.size(); i++) {
423                 FuncNode * func_node = func_nodes[i];
424                 func_node->set_new_exec_flag();
425         }
426 }
427
428 void ModelHistory::dump_func_node_graph()
429 {
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();
434
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);
440                 }
441         }
442         model_print("}\n");
443 }
444
445 void ModelHistory::print_func_node()
446 {
447         /* function id starts with 1 */
448         for (uint32_t i = 1; i < func_nodes.size(); i++) {
449                 FuncNode * func_node = func_nodes[i];
450
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());
453
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());
458                 }
459         }
460 }