fba6eccdcbfb23f45740fc1fad08b32e74fa564b
[cdsspec-compiler.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 <inttypes.h>
10
11 #include "mymemory.h"
12 #include "hashtable.h"
13 #include "workqueue.h"
14 #include "config.h"
15 #include "modeltypes.h"
16 #include "stl-model.h"
17 #include "context.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 class Trace_Analysis;
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 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         bool sc_trace_analysis;
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 *reader) :
78                 writer(writer), reader(reader)
79         { }
80         const ModelAction *writer;
81         ModelAction *reader;
82 };
83
84 /** @brief Records information regarding a single pending release sequence */
85 struct release_seq {
86         /** @brief The acquire operation */
87         ModelAction *acquire;
88         /** @brief The read operation that may read from a release sequence;
89          *  may be the same as acquire, or else an earlier action in the same
90          *  thread (i.e., when 'acquire' is a fence-acquire) */
91         const ModelAction *read;
92         /** @brief The head of the RMW chain from which 'read' reads; may be
93          *  equal to 'release' */
94         const ModelAction *rf;
95         /** @brief The head of the potential longest release sequence chain */
96         const ModelAction *release;
97         /** @brief The write(s) that may break the release sequence */
98         SnapVector<const ModelAction *> writes;
99 };
100
101 /** @brief The central structure for model-checking */
102 class ModelChecker {
103 public:
104         ModelChecker(struct model_params params);
105         ~ModelChecker();
106
107         void run();
108
109         /** @returns the context for the main model-checking system thread */
110         ucontext_t * get_system_context() { return &system_context; }
111
112         void print_summary() const;
113 #if SUPPORT_MOD_ORDER_DUMP
114         void dumpGraph(char *filename) const;
115 #endif
116
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         void add_trace_analysis(Trace_Analysis * a) {
142                 trace_analyses->push_back(a);
143         }
144
145         action_list_t * get_actions_on_obj(void * obj, thread_id_t tid);
146         ModelAction * get_last_action(thread_id_t tid) const;
147
148         MEMALLOC
149 private:
150         /** The scheduler to use: tracks the running/ready Threads */
151         Scheduler * const scheduler;
152
153         void add_thread(Thread *t);
154
155         bool sleep_can_read_from(ModelAction *curr, const ModelAction *write);
156         bool thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader) const;
157         bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
158         bool promises_may_allow(const ModelAction *writer, const ModelAction *reader) const;
159         bool has_asserted() const;
160         void set_assert();
161         void set_bad_synchronization();
162         bool promises_expired() const;
163         void execute_sleep_set();
164         bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
165         void wake_up_sleeping_actions(ModelAction *curr);
166         modelclock_t get_next_seq_num();
167
168         bool next_execution();
169         ModelAction * check_current_action(ModelAction *curr);
170         bool initialize_curr_action(ModelAction **curr);
171         bool process_read(ModelAction *curr);
172         bool process_write(ModelAction *curr);
173         bool process_fence(ModelAction *curr);
174         bool process_mutex(ModelAction *curr);
175         bool process_thread_action(ModelAction *curr);
176         void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
177         bool read_from(ModelAction *act, const ModelAction *rf);
178         bool check_action_enabled(ModelAction *curr);
179
180         Thread * take_step(ModelAction *curr);
181         bool should_terminate_execution();
182
183         template <typename T>
184         bool check_recency(ModelAction *curr, const T *rf) const;
185
186         template <typename T, typename U>
187         bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
188
189         ModelAction * get_last_fence_conflict(ModelAction *act) const;
190         ModelAction * get_last_conflict(ModelAction *act) const;
191         void set_backtracking(ModelAction *act);
192         Thread * action_select_next_thread(const ModelAction *curr) const;
193         Thread * get_next_thread();
194         bool set_latest_backtrack(ModelAction *act);
195         ModelAction * get_next_backtrack();
196         void reset_to_initial_state();
197         Promise * pop_promise_to_resolve(const ModelAction *curr);
198         bool resolve_promise(ModelAction *curr, Promise *promise);
199         void compute_promises(ModelAction *curr);
200         void compute_relseq_breakwrites(ModelAction *curr);
201
202         void mo_check_promises(const ModelAction *act, bool is_read_check);
203         void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
204
205         void check_curr_backtracking(ModelAction *curr);
206         void add_action_to_lists(ModelAction *act);
207         ModelAction * get_last_fence_release(thread_id_t tid) const;
208         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
209         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
210         ModelAction * get_last_unlock(ModelAction *curr) const;
211         void build_may_read_from(ModelAction *curr);
212         ModelAction * process_rmw(ModelAction *curr);
213
214         template <typename rf_type>
215         bool r_modification_order(ModelAction *curr, const rf_type *rf);
216
217         bool w_modification_order(ModelAction *curr, ModelVector<ModelAction *> *send_fv);
218         void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
219         bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
220         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
221         void add_future_value(const ModelAction *writer, ModelAction *reader);
222
223         ModelAction * get_uninitialized_action(const ModelAction *curr) const;
224
225         ModelAction *diverge;
226         ModelAction *earliest_diverge;
227
228         ucontext_t system_context;
229         action_list_t * const action_trace;
230         HashTable<int, Thread *, int> * const thread_map;
231
232         /** Per-object list of actions. Maps an object (i.e., memory location)
233          * to a trace of all actions performed on the object. */
234         HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_map;
235
236         /** Per-object list of actions. Maps an object (i.e., memory location)
237          * to a trace of all actions performed on the object. */
238         HashTable<const void *, action_list_t *, uintptr_t, 4> * const condvar_waiters_map;
239
240         HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
241         SnapVector<Promise *> * const promises;
242         SnapVector<struct PendingFutureValue> * const futurevalues;
243
244         /**
245          * List of pending release sequences. Release sequences might be
246          * determined lazily as promises are fulfilled and modification orders
247          * are established. Each entry in the list may only be partially
248          * filled, depending on its pending status.
249          */
250         SnapVector<struct release_seq *> * const pending_rel_seqs;
251
252         SnapVector<ModelAction *> * const thrd_last_action;
253         SnapVector<ModelAction *> * const thrd_last_fence_release;
254         NodeStack * const node_stack;
255         ModelVector<Trace_Analysis *> * trace_analyses;
256
257
258         /** Private data members that should be snapshotted. They are grouped
259          * together for efficiency and maintainability. */
260         struct model_snapshot_members * const priv;
261
262         /** A special model-checker Thread; used for associating with
263          *  model-checker-related ModelAcitons */
264         Thread *model_thread;
265
266         /**
267          * @brief The modification order graph
268          *
269          * A directed acyclic graph recording observations of the modification
270          * order on all the atomic objects in the system. This graph should
271          * never contain any cycles, as that represents a violation of the
272          * memory model (total ordering). This graph really consists of many
273          * disjoint (unconnected) subgraphs, each graph corresponding to a
274          * separate ordering on a distinct object.
275          *
276          * The edges in this graph represent the "ordered before" relation,
277          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
278          * <tt>b</tt>.
279          */
280         CycleGraph * const mo_graph;
281
282         /** @brief The cumulative execution stats */
283         struct execution_stats stats;
284         void record_stats();
285         void run_trace_analyses();
286         void print_infeasibility(const char *prefix) const;
287         bool is_feasible_prefix_ignore_relseq() const;
288         bool is_infeasible() const;
289         bool is_deadlocked() const;
290         bool too_many_steps() const;
291         bool is_complete_execution() const;
292         bool have_bug_reports() const;
293         void print_bugs() const;
294         void print_execution(bool printbugs) const;
295         void print_stats() const;
296
297         friend void user_main_wrapper();
298 };
299
300 extern ModelChecker *model;
301
302 #endif /* __MODEL_H__ */