add a new data structure in execution.h, which is used by history.cc to link FuncInsts
[c11tester.git] / execution.h
1 /** @file execution.h
2  *  @brief Model-checker core
3  */
4
5 #ifndef __EXECUTION_H__
6 #define __EXECUTION_H__
7
8 #include <cstddef>
9 #include <inttypes.h>
10
11 #include "mymemory.h"
12 #include "hashtable.h"
13 #include "config.h"
14 #include "modeltypes.h"
15 #include "stl-model.h"
16 #include "params.h"
17 #include "mypthread.h"
18 #include "mutex.h"
19 #include <condition_variable>
20 #include "classlist.h"
21
22 /** @brief Shorthand for a list of release sequence heads */
23 typedef ModelVector<const ModelAction *> rel_heads_list_t;
24 typedef SnapList<ModelAction *> action_list_t;
25
26 struct PendingFutureValue {
27         PendingFutureValue(ModelAction *writer, ModelAction *reader) :
28                 writer(writer), reader(reader)
29         { }
30         const ModelAction *writer;
31         ModelAction *reader;
32 };
33
34 /** @brief Records information regarding a single pending release sequence */
35 struct release_seq {
36         /** @brief The acquire operation */
37         ModelAction *acquire;
38         /** @brief The read operation that may read from a release sequence;
39          *  may be the same as acquire, or else an earlier action in the same
40          *  thread (i.e., when 'acquire' is a fence-acquire) */
41         const ModelAction *read;
42         /** @brief The head of the RMW chain from which 'read' reads; may be
43          *  equal to 'release' */
44         const ModelAction *rf;
45         /** @brief The head of the potential longest release sequence chain */
46         const ModelAction *release;
47         /** @brief The write(s) that may break the release sequence */
48         SnapVector<const ModelAction *> writes;
49 };
50
51 /** @brief The central structure for model-checking */
52 class ModelExecution {
53 public:
54         ModelExecution(ModelChecker *m,
55                                                                  Scheduler *scheduler,
56                                                                  NodeStack *node_stack);
57         ~ModelExecution();
58
59         struct model_params * get_params() const { return params; }
60         void setParams(struct model_params * _params) {params = _params;}
61
62         Thread * take_step(ModelAction *curr);
63
64         void print_summary() const;
65 #if SUPPORT_MOD_ORDER_DUMP
66         void dumpGraph(char *filename) const;
67 #endif
68
69         void add_thread(Thread *t);
70         Thread * get_thread(thread_id_t tid) const;
71         Thread * get_thread(const ModelAction *act) const;
72
73         uint32_t get_pthread_counter() { return pthread_counter; }
74         void incr_pthread_counter() { pthread_counter++; }
75         Thread * get_pthread(pthread_t pid);
76
77         bool is_enabled(Thread *t) const;
78         bool is_enabled(thread_id_t tid) const;
79
80         thread_id_t get_next_id();
81         unsigned int get_num_threads() const;
82
83         ClockVector * get_cv(thread_id_t tid) const;
84         ModelAction * get_parent_action(thread_id_t tid) const;
85         bool isfeasibleprefix() const;
86
87         ModelAction * get_last_action(thread_id_t tid) const;
88
89         bool check_action_enabled(ModelAction *curr);
90
91         bool assert_bug(const char *msg);
92         bool have_bug_reports() const;
93         SnapVector<bug_message *> * get_bugs() const;
94
95         bool has_asserted() const;
96         void set_assert();
97         bool is_complete_execution() const;
98
99         void print_infeasibility(const char *prefix) const;
100         bool is_infeasible() const;
101         bool is_deadlocked() const;
102
103         action_list_t * get_action_trace() { return &action_trace; }
104         Fuzzer * getFuzzer();
105         CycleGraph * const get_mo_graph() { return mo_graph; }
106         HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> * getCondMap() {return &cond_map;}
107         HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> * getMutexMap() {return &mutex_map;}
108         ModelAction * check_current_action(ModelAction *curr);
109
110         SnapVector<func_id_list_t *> * get_thrd_func_list() { return &thrd_func_list; }
111         SnapVector< SnapList<func_inst_list_t *> *> * get_thrd_func_inst_lists() { return &thrd_func_inst_lists; }
112
113         SNAPSHOTALLOC
114 private:
115         int get_execution_number() const;
116
117         ModelChecker *model;
118
119         struct model_params * params;
120
121         /** The scheduler to use: tracks the running/ready Threads */
122         Scheduler * const scheduler;
123
124         bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
125         void set_bad_synchronization();
126         bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
127         void wake_up_sleeping_actions(ModelAction *curr);
128         modelclock_t get_next_seq_num();
129
130         bool next_execution();
131         bool initialize_curr_action(ModelAction **curr);
132         void process_read(ModelAction *curr, SnapVector<const ModelAction *> * rf_set);
133         void process_write(ModelAction *curr);
134         bool process_fence(ModelAction *curr);
135         bool process_mutex(ModelAction *curr);
136
137         bool process_thread_action(ModelAction *curr);
138         bool read_from(ModelAction *act, const ModelAction *rf);
139         bool synchronize(const ModelAction *first, ModelAction *second);
140
141         void add_action_to_lists(ModelAction *act);
142         ModelAction * get_last_fence_release(thread_id_t tid) const;
143         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
144         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
145         ModelAction * get_last_unlock(ModelAction *curr) const;
146         SnapVector<const ModelAction *> * build_may_read_from(ModelAction *curr);
147         ModelAction * process_rmw(ModelAction *curr);
148
149         bool r_modification_order(ModelAction *curr, const ModelAction *rf, SnapVector<const ModelAction *> *priorset, bool *canprune);
150         void w_modification_order(ModelAction *curr);
151         void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
152         bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads) const;
153         ModelAction * get_uninitialized_action(const ModelAction *curr) const;
154
155         action_list_t action_trace;
156         SnapVector<Thread *> thread_map;
157         SnapVector<Thread *> pthread_map;
158         uint32_t pthread_counter;
159
160         /** Per-object list of actions. Maps an object (i.e., memory location)
161          * to a trace of all actions performed on the object. */
162         HashTable<const void *, action_list_t *, uintptr_t, 4> obj_map;
163
164         /** Per-object list of actions. Maps an object (i.e., memory location)
165          * to a trace of all actions performed on the object. */
166         HashTable<const void *, action_list_t *, uintptr_t, 4> condvar_waiters_map;
167
168         HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4> obj_thrd_map;
169
170         HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> mutex_map;
171         HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> cond_map;
172
173         /**
174          * List of pending release sequences. Release sequences might be
175          * determined lazily as promises are fulfilled and modification orders
176          * are established. Each entry in the list may only be partially
177          * filled, depending on its pending status.
178          */
179
180         SnapVector<ModelAction *> thrd_last_action;
181         SnapVector<ModelAction *> thrd_last_fence_release;
182         NodeStack * const node_stack;
183
184         /** A special model-checker Thread; used for associating with
185          *  model-checker-related ModelAcitons */
186         Thread *model_thread;
187
188         /** Private data members that should be snapshotted. They are grouped
189          * together for efficiency and maintainability. */
190         struct model_snapshot_members * const priv;
191
192         /**
193          * @brief The modification order graph
194          *
195          * A directed acyclic graph recording observations of the modification
196          * order on all the atomic objects in the system. This graph should
197          * never contain any cycles, as that represents a violation of the
198          * memory model (total ordering). This graph really consists of many
199          * disjoint (unconnected) subgraphs, each graph corresponding to a
200          * separate ordering on a distinct object.
201          *
202          * The edges in this graph represent the "ordered before" relation,
203          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
204          * <tt>b</tt>.
205          */
206         CycleGraph * const mo_graph;
207
208         Fuzzer * fuzzer;
209
210         Thread * action_select_next_thread(const ModelAction *curr) const;
211
212         /* thrd_func_list stores a list of function ids for each thread. 
213          * Each element in thrd_func_list stores the functions that
214          * thread i has entered and yet to exit from 
215          *
216          * This data structure is handled by ModelHistory
217          */
218         SnapVector< func_id_list_t * > thrd_func_list;
219
220         /* Keeps track of atomic actions that thread i has performed in some
221          * function. Index of SnapVector is thread id. SnapList simulates 
222          * the call stack. 
223          *
224          * This data structure is handled by ModelHistory
225          */
226         SnapVector< SnapList< func_inst_list_t *> *> thrd_func_inst_lists;
227 };
228
229 #endif  /* __EXECUTION_H__ */