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