90c8a55e5f96f7ca93a6adcd6c6f28fdac96fa4a
[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 <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
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 *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
142         MEMALLOC
143 private:
144         /** The scheduler to use: tracks the running/ready Threads */
145         Scheduler * const scheduler;
146
147         void add_thread(Thread *t);
148
149         bool sleep_can_read_from(ModelAction *curr, const ModelAction *write);
150         bool thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader) const;
151         bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
152         bool promises_may_allow(const ModelAction *writer, const ModelAction *reader) const;
153         bool has_asserted() const;
154         void set_assert();
155         void set_bad_synchronization();
156         bool promises_expired() const;
157         void execute_sleep_set();
158         bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
159         void wake_up_sleeping_actions(ModelAction *curr);
160         modelclock_t get_next_seq_num();
161
162         bool next_execution();
163         ModelAction * check_current_action(ModelAction *curr);
164         bool initialize_curr_action(ModelAction **curr);
165         bool process_read(ModelAction *curr);
166         bool process_write(ModelAction *curr);
167         bool process_fence(ModelAction *curr);
168         bool process_mutex(ModelAction *curr);
169         bool process_thread_action(ModelAction *curr);
170         void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
171         bool read_from(ModelAction *act, const ModelAction *rf);
172         bool check_action_enabled(ModelAction *curr);
173
174         Thread * take_step(ModelAction *curr);
175         bool should_terminate_execution();
176
177         template <typename T>
178         bool check_recency(ModelAction *curr, const T *rf) const;
179
180         template <typename T, typename U>
181         bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
182
183         ModelAction * get_last_fence_conflict(ModelAction *act) const;
184         ModelAction * get_last_conflict(ModelAction *act) const;
185         void set_backtracking(ModelAction *act);
186         Thread * action_select_next_thread(const ModelAction *curr) const;
187         Thread * get_next_thread();
188         bool set_latest_backtrack(ModelAction *act);
189         ModelAction * get_next_backtrack();
190         void reset_to_initial_state();
191         Promise * pop_promise_to_resolve(const ModelAction *curr);
192         bool resolve_promise(ModelAction *curr, Promise *promise);
193         void compute_promises(ModelAction *curr);
194         void compute_relseq_breakwrites(ModelAction *curr);
195
196         void mo_check_promises(const ModelAction *act, bool is_read_check);
197         void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
198
199         void check_curr_backtracking(ModelAction *curr);
200         void add_action_to_lists(ModelAction *act);
201         ModelAction * get_last_action(thread_id_t tid) const;
202         ModelAction * get_last_fence_release(thread_id_t tid) const;
203         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
204         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
205         ModelAction * get_last_unlock(ModelAction *curr) const;
206         void build_may_read_from(ModelAction *curr);
207         ModelAction * process_rmw(ModelAction *curr);
208
209         template <typename rf_type>
210         bool r_modification_order(ModelAction *curr, const rf_type *rf);
211
212         bool w_modification_order(ModelAction *curr, ModelVector<ModelAction *> *send_fv);
213         void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
214         bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
215         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
216         void add_future_value(const ModelAction *writer, ModelAction *reader);
217
218         ModelAction * get_uninitialized_action(const ModelAction *curr) const;
219
220         ModelAction *diverge;
221         ModelAction *earliest_diverge;
222
223         ucontext_t system_context;
224         action_list_t * const action_trace;
225         HashTable<int, Thread *, int> * const thread_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 obj_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         ModelVector<Trace_Analysis *> * trace_analyses;
251
252
253         /** Private data members that should be snapshotted. They are grouped
254          * together for efficiency and maintainability. */
255         struct model_snapshot_members * const priv;
256
257         /** A special model-checker Thread; used for associating with
258          *  model-checker-related ModelAcitons */
259         Thread *model_thread;
260
261         /**
262          * @brief The modification order graph
263          *
264          * A directed acyclic graph recording observations of the modification
265          * order on all the atomic objects in the system. This graph should
266          * never contain any cycles, as that represents a violation of the
267          * memory model (total ordering). This graph really consists of many
268          * disjoint (unconnected) subgraphs, each graph corresponding to a
269          * separate ordering on a distinct object.
270          *
271          * The edges in this graph represent the "ordered before" relation,
272          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
273          * <tt>b</tt>.
274          */
275         CycleGraph * const mo_graph;
276
277         /** @brief The cumulative execution stats */
278         struct execution_stats stats;
279         void record_stats();
280         void run_trace_analyses();
281         void print_infeasibility(const char *prefix) const;
282         bool is_feasible_prefix_ignore_relseq() const;
283         bool is_infeasible() const;
284         bool is_deadlocked() const;
285         bool is_complete_execution() const;
286         bool have_bug_reports() const;
287         void print_bugs() const;
288         void print_execution(bool printbugs) const;
289         void print_stats() const;
290
291         friend void user_main_wrapper();
292 };
293
294 extern ModelChecker *model;
295
296 #endif /* __MODEL_H__ */