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