fix mutex_trylock bug
[c11tester.git] / funcnode.h
1 #ifndef __FUNCNODE_H__
2 #define __FUNCNODE_H__
3
4 #include "hashset.h"
5 #include "hashfunction.h"
6 #include "classlist.h"
7 #include "threads-model.h"
8
9 #define MAX_DIST 10
10
11 typedef ModelList<FuncInst *> func_inst_list_mt;
12 typedef ModelVector<Predicate *> predicate_trace_t;
13
14 typedef HashTable<void *, FuncInst *, uintptr_t, 0, model_malloc, model_calloc, model_free> loc_inst_map_t;
15 typedef HashTable<FuncInst *, uint32_t, uintptr_t, 0, model_malloc, model_calloc, model_free> inst_id_map_t;
16 typedef HashTable<FuncInst *, Predicate *, uintptr_t, 0, model_malloc, model_calloc, model_free> inst_pred_map_t;
17
18 typedef enum edge_type {
19         IN_EDGE, OUT_EDGE, BI_EDGE
20 } edge_type_t;
21
22 class FuncNode {
23 public:
24         FuncNode(ModelHistory * history);
25         ~FuncNode();
26
27         uint32_t get_func_id() { return func_id; }
28         const char * get_func_name() { return func_name; }
29         void set_func_id(uint32_t id) { func_id = id; }
30         void set_func_name(const char * name) { func_name = name; }
31         void set_new_exec_flag();
32
33         void add_inst(ModelAction *act);
34         FuncInst * create_new_inst(ModelAction *act);
35         FuncInst * get_inst(ModelAction *act);
36
37         func_inst_list_mt * get_inst_list() { return &inst_list; }
38         func_inst_list_mt * get_entry_insts() { return &entry_insts; }
39         void add_entry_inst(FuncInst * inst);
40
41         void function_entry_handler(thread_id_t tid);
42         void function_exit_handler(thread_id_t tid);
43
44         void update_tree(ModelAction * act);
45
46         void add_to_val_loc_map(uint64_t val, void * loc);
47         void add_to_val_loc_map(value_set_t * values, void * loc);
48         void update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations);
49
50         Predicate * get_predicate_tree_position(thread_id_t tid);
51
52         void add_predicate_to_trace(thread_id_t tid, Predicate *pred);
53
54         uint64_t get_associated_read(thread_id_t tid, FuncInst * inst);
55
56         void add_out_edge(FuncNode * other);
57         ModelList<FuncNode *> * get_out_edges() { return &out_edges; }
58         int compute_distance(FuncNode * target, int max_step = MAX_DIST);
59
60         void print_predicate_tree();
61
62         MEMALLOC
63 private:
64         uint32_t func_id;
65         const char * func_name;
66         ModelHistory * history;
67         Predicate * predicate_tree_entry;       // A dummy node whose children are the real entries
68         Predicate * predicate_tree_exit;        // A dummy node
69
70         uint32_t inst_counter;
71         uint32_t marker;
72         uint32_t exit_count;
73         ModelVector< ModelVector<uint32_t> *> thrd_markers;
74         ModelVector<int> thrd_recursion_depth;  // Recursion depth starts from 0 to match with vector indexes.
75
76         void init_marker(thread_id_t tid);
77
78         /* Use source line number as the key of hashtable, to check if
79          * atomic operation with this line number has been added or not
80          */
81         HashTable<const char *, FuncInst *, uintptr_t, 4, model_malloc, model_calloc, model_free> func_inst_map;
82
83         /* List of all atomic actions in this function */
84         func_inst_list_mt inst_list;
85
86         /* Possible entry atomic actions in this function */
87         func_inst_list_mt entry_insts;
88
89         /* Map a FuncInst to the its predicate when updating predicate trees */
90         ModelVector< ModelVector<inst_pred_map_t *> * > thrd_inst_pred_maps;
91
92         /* Number FuncInsts to detect loops when updating predicate trees */
93         ModelVector< ModelVector<inst_id_map_t *> *> thrd_inst_id_maps;
94
95         /* Detect read actions at the same locations when updating predicate trees */
96         ModelVector< ModelVector<loc_inst_map_t *> *> thrd_loc_inst_maps;
97
98         void init_local_maps(thread_id_t tid);
99         void reset_local_maps(thread_id_t tid);
100
101         void update_inst_tree(func_inst_list_t * inst_list);
102         void update_predicate_tree(ModelAction * act);
103         bool follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act, Predicate ** unset_predicate);
104
105         void infer_predicates(FuncInst * next_inst, ModelAction * next_act, SnapVector<struct half_pred_expr *> * half_pred_expressions);
106         void generate_predicates(Predicate * curr_pred, FuncInst * next_inst, SnapVector<struct half_pred_expr *> * half_pred_expressions);
107         bool amend_predicate_expr(Predicate * curr_pred, FuncInst * next_inst, ModelAction * next_act);
108
109         /* Set of locations read by this FuncNode */
110         loc_set_t * read_locations;
111
112         /* Set of locations written to by this FuncNode */
113         loc_set_t * write_locations;
114
115         /* Keeps track of locations that have the same values written to */
116         HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash> * val_loc_map;
117
118         /* Keeps track of locations that may share the same value as key, deduced from val_loc_map */
119         HashTable<void *, loc_set_t *, uintptr_t, 0> * loc_may_equal_map;
120
121         HashTable<FuncInst *, bool, uintptr_t, 0, model_malloc, model_calloc, model_free> likely_null_set;
122
123         bool likely_reads_from_null(ModelAction * read);
124         // value_set_t * values_may_read_from;
125
126         /* Run-time position in the predicate tree for each thread
127          * The inner vector is used to deal with recursive functions. */
128         ModelVector< ModelVector<Predicate *> * > thrd_predicate_tree_position;
129
130         ModelVector< ModelVector<predicate_trace_t *> * > thrd_predicate_trace;
131
132         void set_predicate_tree_position(thread_id_t tid, Predicate * pred);
133
134         void init_predicate_tree_data_structure(thread_id_t tid);
135         void reset_predicate_tree_data_structure(thread_id_t tid);
136
137         /* Store the relation between this FuncNode and other FuncNodes */
138         HashTable<FuncNode *, edge_type_t, uintptr_t, 0, model_malloc, model_calloc, model_free> edge_table;
139
140         /* FuncNodes that follow this node */
141         ModelList<FuncNode *> out_edges;
142
143         void update_predicate_tree_weight(thread_id_t tid);
144 };
145
146 #endif  /* __FUNCNODE_H__ */