model/action: move action_list_t to model.h
[cdsspec-compiler.git] / model.h
1 /** @file model.h
2  *  @brief Core model checker.
3  */
4
5 #ifndef __MODEL_H__
6 #define __MODEL_H__
7
8 #include <vector>
9 #include <cstddef>
10 #include <ucontext.h>
11
12 #include "mymemory.h"
13 #include "action.h"
14 #include "hashtable.h"
15 #include "workqueue.h"
16 #include "config.h"
17 #include "modeltypes.h"
18
19 /* Forward declaration */
20 class NodeStack;
21 class CycleGraph;
22 class Promise;
23 class Scheduler;
24 class Thread;
25 struct model_snapshot_members;
26
27 /** @brief Shorthand for a list of release sequence heads */
28 typedef std::vector< const ModelAction *, ModelAlloc<const ModelAction *> > rel_heads_list_t;
29
30 typedef std::list< ModelAction *, SnapshotAlloc<ModelAction *> > action_list_t;
31
32 /**
33  * Model checker parameter structure. Holds run-time configuration options for
34  * the model checker.
35  */
36 struct model_params {
37         int maxreads;
38         int maxfuturedelay;
39         unsigned int fairwindow;
40         unsigned int enabledcount;
41         unsigned int bound;
42
43         /** @brief Maximum number of future values that can be sent to the same
44          *  read */
45         int maxfuturevalues;
46
47         /** @brief Only generate a new future value/expiration pair if the
48          *  expiration time exceeds the existing one by more than the slop
49          *  value */
50         unsigned int expireslop;
51
52         /** @brief Verbosity (0 = quiet; 1 = noisy) */
53         int verbose;
54
55         /** @brief Command-line argument count to pass to user program */
56         int argc;
57
58         /** @brief Command-line arguments to pass to user program */
59         char **argv;
60 };
61
62 /** @brief Model checker execution stats */
63 struct execution_stats {
64         int num_total; /**< @brief Total number of executions */
65         int num_infeasible; /**< @brief Number of infeasible executions */
66         int num_buggy_executions; /** @brief Number of buggy executions */
67         int num_complete; /**< @brief Number of feasible, non-buggy, complete executions */
68         int num_redundant; /**< @brief Number of redundant, aborted executions */
69 };
70
71 struct PendingFutureValue {
72         PendingFutureValue(ModelAction *writer, ModelAction *act) : writer(writer), act(act) { }
73         const ModelAction *writer;
74         ModelAction *act;
75 };
76
77 /** @brief Records information regarding a single pending release sequence */
78 struct release_seq {
79         /** @brief The acquire operation */
80         ModelAction *acquire;
81         /** @brief The read operation that may read from a release sequence;
82          *  may be the same as acquire, or else an earlier action in the same
83          *  thread (i.e., when 'acquire' is a fence-acquire) */
84         const ModelAction *read;
85         /** @brief The head of the RMW chain from which 'read' reads; may be
86          *  equal to 'release' */
87         const ModelAction *rf;
88         /** @brief The head of the potential longest release sequence chain */
89         const ModelAction *release;
90         /** @brief The write(s) that may break the release sequence */
91         std::vector<const ModelAction *> writes;
92 };
93
94 /** @brief The central structure for model-checking */
95 class ModelChecker {
96 public:
97         ModelChecker(struct model_params params);
98         ~ModelChecker();
99
100         void run();
101
102         /** @returns the context for the main model-checking system thread */
103         ucontext_t * get_system_context() { return &system_context; }
104
105         void print_summary() const;
106 #if SUPPORT_MOD_ORDER_DUMP
107         void dumpGraph(char *filename) const;
108 #endif
109
110         void add_thread(Thread *t);
111         void remove_thread(Thread *t);
112         Thread * get_thread(thread_id_t tid) const;
113         Thread * get_thread(const ModelAction *act) const;
114
115         bool is_enabled(Thread *t) const;
116         bool is_enabled(thread_id_t tid) const;
117
118         thread_id_t get_next_id();
119         unsigned int get_num_threads() const;
120         Thread * get_current_thread() const;
121
122         void switch_from_master(Thread *thread);
123         uint64_t switch_to_master(ModelAction *act);
124         ClockVector * get_cv(thread_id_t tid) const;
125         ModelAction * get_parent_action(thread_id_t tid) const;
126         void check_promises_thread_disabled();
127         void mo_check_promises(const ModelAction *act, bool is_read_check);
128         void check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv);
129         bool isfeasibleprefix() const;
130
131         bool assert_bug(const char *msg);
132         void assert_user_bug(const char *msg);
133
134         const model_params params;
135         Node * get_curr_node() const;
136
137         MEMALLOC
138 private:
139         /** The scheduler to use: tracks the running/ready Threads */
140         Scheduler *scheduler;
141
142         bool sleep_can_read_from(ModelAction *curr, const ModelAction *write);
143         bool thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader);
144         bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
145         bool has_asserted() const;
146         void set_assert();
147         void set_bad_synchronization();
148         bool promises_expired() const;
149         void execute_sleep_set();
150         void wake_up_sleeping_actions(ModelAction *curr);
151         modelclock_t get_next_seq_num();
152
153         bool next_execution();
154         ModelAction * check_current_action(ModelAction *curr);
155         bool initialize_curr_action(ModelAction **curr);
156         bool process_read(ModelAction *curr, bool second_part_of_rmw);
157         bool process_write(ModelAction *curr);
158         bool process_fence(ModelAction *curr);
159         bool process_mutex(ModelAction *curr);
160         bool process_thread_action(ModelAction *curr);
161         void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
162         bool read_from(ModelAction *act, const ModelAction *rf);
163         bool check_action_enabled(ModelAction *curr);
164
165         Thread * take_step(ModelAction *curr);
166
167         void check_recency(ModelAction *curr, const ModelAction *rf);
168         ModelAction * get_last_conflict(ModelAction *act);
169         void set_backtracking(ModelAction *act);
170         Thread * get_next_thread(ModelAction *curr);
171         bool set_latest_backtrack(ModelAction *act);
172         ModelAction * get_next_backtrack();
173         void reset_to_initial_state();
174         bool resolve_promises(ModelAction *curr);
175         void compute_promises(ModelAction *curr);
176         void compute_relseq_breakwrites(ModelAction *curr);
177
178         void check_curr_backtracking(ModelAction *curr);
179         void add_action_to_lists(ModelAction *act);
180         ModelAction * get_last_action(thread_id_t tid) const;
181         ModelAction * get_last_fence_release(thread_id_t tid) const;
182         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
183         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
184         ModelAction * get_last_unlock(ModelAction *curr) const;
185         void build_reads_from_past(ModelAction *curr);
186         ModelAction * process_rmw(ModelAction *curr);
187
188         template <typename rf_type>
189         bool r_modification_order(ModelAction *curr, const rf_type *rf);
190
191         bool w_modification_order(ModelAction *curr);
192         void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
193         bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
194         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
195         void add_future_value(const ModelAction *writer, ModelAction *reader);
196
197         ModelAction * new_uninitialized_action(void *location) const;
198
199         ModelAction *diverge;
200         ModelAction *earliest_diverge;
201
202         ucontext_t system_context;
203         action_list_t *action_trace;
204         HashTable<int, Thread *, int> *thread_map;
205
206         /** Per-object list of actions. Maps an object (i.e., memory location)
207          * to a trace of all actions performed on the object. */
208         HashTable<const void *, action_list_t *, uintptr_t, 4> *obj_map;
209
210         /** Per-object list of actions. Maps an object (i.e., memory location)
211          * to a trace of all actions performed on the object. */
212         HashTable<const void *, action_list_t *, uintptr_t, 4> *lock_waiters_map;
213
214         /** Per-object list of actions. Maps an object (i.e., memory location)
215          * to a trace of all actions performed on the object. */
216         HashTable<const void *, action_list_t *, uintptr_t, 4> *condvar_waiters_map;
217
218         HashTable<void *, std::vector<action_list_t> *, uintptr_t, 4 > *obj_thrd_map;
219         std::vector< Promise *, SnapshotAlloc<Promise *> > *promises;
220         std::vector< struct PendingFutureValue, SnapshotAlloc<struct PendingFutureValue> > *futurevalues;
221
222         /**
223          * List of pending release sequences. Release sequences might be
224          * determined lazily as promises are fulfilled and modification orders
225          * are established. Each entry in the list may only be partially
226          * filled, depending on its pending status.
227          */
228         std::vector< struct release_seq *, SnapshotAlloc<struct release_seq *> > *pending_rel_seqs;
229
230         std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > *thrd_last_action;
231         std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > *thrd_last_fence_release;
232         NodeStack *node_stack;
233
234         /** Private data members that should be snapshotted. They are grouped
235          * together for efficiency and maintainability. */
236         struct model_snapshot_members *priv;
237
238         /** A special model-checker Thread; used for associating with
239          *  model-checker-related ModelAcitons */
240         Thread *model_thread;
241
242         /**
243          * @brief The modification order graph
244          *
245          * A directed acyclic graph recording observations of the modification
246          * order on all the atomic objects in the system. This graph should
247          * never contain any cycles, as that represents a violation of the
248          * memory model (total ordering). This graph really consists of many
249          * disjoint (unconnected) subgraphs, each graph corresponding to a
250          * separate ordering on a distinct object.
251          *
252          * The edges in this graph represent the "ordered before" relation,
253          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
254          * <tt>b</tt>.
255          */
256         CycleGraph *mo_graph;
257
258         /** @brief The cumulative execution stats */
259         struct execution_stats stats;
260         void record_stats();
261
262         void print_infeasibility(const char *prefix) const;
263         bool is_feasible_prefix_ignore_relseq() const;
264         bool is_infeasible() const;
265         bool is_deadlocked() const;
266         bool is_complete_execution() const;
267         bool have_bug_reports() const;
268         void print_bugs() const;
269         void print_execution(bool printbugs) const;
270         void print_stats() const;
271
272         friend void user_main_wrapper();
273 };
274
275 extern ModelChecker *model;
276
277 #endif /* __MODEL_H__ */