cyclegraph: memory "leak", fixup
[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         int get_promise_number(const Promise *promise) const;
117
118         bool is_enabled(Thread *t) const;
119         bool is_enabled(thread_id_t tid) const;
120
121         thread_id_t get_next_id();
122         unsigned int get_num_threads() const;
123         Thread * get_current_thread() const;
124
125         void switch_from_master(Thread *thread);
126         uint64_t switch_to_master(ModelAction *act);
127         ClockVector * get_cv(thread_id_t tid) const;
128         ModelAction * get_parent_action(thread_id_t tid) const;
129         void check_promises_thread_disabled();
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);
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         template <typename T>
171         bool check_recency(ModelAction *curr, const T *rf) const;
172
173         template <typename T, typename U>
174         bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
175
176         ModelAction * get_last_fence_conflict(ModelAction *act) const;
177         ModelAction * get_last_conflict(ModelAction *act) const;
178         void set_backtracking(ModelAction *act);
179         Thread * action_select_next_thread(const ModelAction *curr) const;
180         Thread * get_next_thread();
181         bool set_latest_backtrack(ModelAction *act);
182         ModelAction * get_next_backtrack();
183         void reset_to_initial_state();
184         int get_promise_to_resolve(const ModelAction *curr) const;
185         bool resolve_promise(ModelAction *curr, unsigned int promise_idx);
186         void compute_promises(ModelAction *curr);
187         void compute_relseq_breakwrites(ModelAction *curr);
188
189         void mo_check_promises(const ModelAction *act, bool is_read_check);
190         void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
191
192         void check_curr_backtracking(ModelAction *curr);
193         void add_action_to_lists(ModelAction *act);
194         ModelAction * get_last_action(thread_id_t tid) const;
195         ModelAction * get_last_fence_release(thread_id_t tid) const;
196         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
197         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
198         ModelAction * get_last_unlock(ModelAction *curr) const;
199         void build_may_read_from(ModelAction *curr);
200         ModelAction * process_rmw(ModelAction *curr);
201
202         template <typename rf_type>
203         bool r_modification_order(ModelAction *curr, const rf_type *rf);
204
205         bool w_modification_order(ModelAction *curr, std::vector< ModelAction *, ModelAlloc<ModelAction *> > *send_fv);
206         void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
207         bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
208         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
209         void add_future_value(const ModelAction *writer, ModelAction *reader);
210
211         ModelAction * new_uninitialized_action(void *location) const;
212
213         ModelAction *diverge;
214         ModelAction *earliest_diverge;
215
216         ucontext_t system_context;
217         action_list_t * const action_trace;
218         HashTable<int, Thread *, int> * const thread_map;
219
220         /** Per-object list of actions. Maps an object (i.e., memory location)
221          * to a trace of all actions performed on the object. */
222         HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_map;
223
224         /** Per-object list of actions. Maps an object (i.e., memory location)
225          * to a trace of all actions performed on the object. */
226         HashTable<const void *, action_list_t *, uintptr_t, 4> * const lock_waiters_map;
227
228         /** Per-object list of actions. Maps an object (i.e., memory location)
229          * to a trace of all actions performed on the object. */
230         HashTable<const void *, action_list_t *, uintptr_t, 4> * const condvar_waiters_map;
231
232         HashTable<void *, std::vector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
233         std::vector< Promise *, SnapshotAlloc<Promise *> > * const promises;
234         std::vector< struct PendingFutureValue, SnapshotAlloc<struct PendingFutureValue> > * const futurevalues;
235
236         /**
237          * List of pending release sequences. Release sequences might be
238          * determined lazily as promises are fulfilled and modification orders
239          * are established. Each entry in the list may only be partially
240          * filled, depending on its pending status.
241          */
242         std::vector< struct release_seq *, SnapshotAlloc<struct release_seq *> > * const pending_rel_seqs;
243
244         std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > * const thrd_last_action;
245         std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > * const thrd_last_fence_release;
246         NodeStack * const node_stack;
247
248         /** Private data members that should be snapshotted. They are grouped
249          * together for efficiency and maintainability. */
250         struct model_snapshot_members * const priv;
251
252         /** A special model-checker Thread; used for associating with
253          *  model-checker-related ModelAcitons */
254         Thread *model_thread;
255
256         /**
257          * @brief The modification order graph
258          *
259          * A directed acyclic graph recording observations of the modification
260          * order on all the atomic objects in the system. This graph should
261          * never contain any cycles, as that represents a violation of the
262          * memory model (total ordering). This graph really consists of many
263          * disjoint (unconnected) subgraphs, each graph corresponding to a
264          * separate ordering on a distinct object.
265          *
266          * The edges in this graph represent the "ordered before" relation,
267          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
268          * <tt>b</tt>.
269          */
270         CycleGraph * const mo_graph;
271
272         /** @brief The cumulative execution stats */
273         struct execution_stats stats;
274         void record_stats();
275
276         void print_infeasibility(const char *prefix) const;
277         bool is_feasible_prefix_ignore_relseq() const;
278         bool is_infeasible() const;
279         bool is_deadlocked() const;
280         bool is_circular_wait(const Thread *t) const;
281         bool is_complete_execution() const;
282         bool have_bug_reports() const;
283         void print_bugs() const;
284         void print_execution(bool printbugs) const;
285         void print_stats() const;
286
287         friend void user_main_wrapper();
288 };
289
290 extern ModelChecker *model;
291
292 #endif /* __MODEL_H__ */