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