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