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