model: remove public check_promises() interface
[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         bool isfeasibleprefix() const;
102
103         bool assert_bug(const char *msg, ...);
104         void assert_user_bug(const char *msg);
105
106         const model_params params;
107         Node * get_curr_node() const;
108         void add_trace_analysis(TraceAnalysis *a) {
109                 trace_analyses->push_back(a);
110         }
111
112         action_list_t * get_actions_on_obj(void * obj, thread_id_t tid);
113         ModelAction * get_last_action(thread_id_t tid) const;
114
115         MEMALLOC
116 private:
117         /** The scheduler to use: tracks the running/ready Threads */
118         Scheduler * const scheduler;
119
120         void add_thread(Thread *t);
121
122         bool sleep_can_read_from(ModelAction *curr, const ModelAction *write);
123         bool thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader) const;
124         bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
125         bool promises_may_allow(const ModelAction *writer, const ModelAction *reader) const;
126         bool has_asserted() const;
127         void set_assert();
128         void set_bad_synchronization();
129         bool promises_expired() const;
130         void execute_sleep_set();
131         bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
132         void wake_up_sleeping_actions(ModelAction *curr);
133         modelclock_t get_next_seq_num();
134
135         bool next_execution();
136         ModelAction * check_current_action(ModelAction *curr);
137         bool initialize_curr_action(ModelAction **curr);
138         bool process_read(ModelAction *curr);
139         bool process_write(ModelAction *curr);
140         bool process_fence(ModelAction *curr);
141         bool process_mutex(ModelAction *curr);
142         bool process_thread_action(ModelAction *curr);
143         void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
144         bool read_from(ModelAction *act, const ModelAction *rf);
145         bool synchronize(const ModelAction *first, ModelAction *second);
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 check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv);
171         void mo_check_promises(const ModelAction *act, bool is_read_check);
172         void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
173
174         void check_curr_backtracking(ModelAction *curr);
175         void add_action_to_lists(ModelAction *act);
176         ModelAction * get_last_fence_release(thread_id_t tid) const;
177         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
178         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
179         ModelAction * get_last_unlock(ModelAction *curr) const;
180         void build_may_read_from(ModelAction *curr);
181         ModelAction * process_rmw(ModelAction *curr);
182
183         template <typename rf_type>
184         bool r_modification_order(ModelAction *curr, const rf_type *rf);
185
186         bool w_modification_order(ModelAction *curr, ModelVector<ModelAction *> *send_fv);
187         void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
188         bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
189         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
190         void add_future_value(const ModelAction *writer, ModelAction *reader);
191
192         ModelAction * get_uninitialized_action(const ModelAction *curr) const;
193
194         ModelAction *diverge;
195         ModelAction *earliest_diverge;
196
197         ucontext_t system_context;
198         action_list_t * const action_trace;
199         HashTable<int, Thread *, int> * const thread_map;
200
201         /** Per-object list of actions. Maps an object (i.e., memory location)
202          * to a trace of all actions performed on the object. */
203         HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_map;
204
205         /** Per-object list of actions. Maps an object (i.e., memory location)
206          * to a trace of all actions performed on the object. */
207         HashTable<const void *, action_list_t *, uintptr_t, 4> * const condvar_waiters_map;
208
209         HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
210         SnapVector<Promise *> * const promises;
211         SnapVector<struct PendingFutureValue> * const futurevalues;
212
213         /**
214          * List of pending release sequences. Release sequences might be
215          * determined lazily as promises are fulfilled and modification orders
216          * are established. Each entry in the list may only be partially
217          * filled, depending on its pending status.
218          */
219         SnapVector<struct release_seq *> * const pending_rel_seqs;
220
221         SnapVector<ModelAction *> * const thrd_last_action;
222         SnapVector<ModelAction *> * const thrd_last_fence_release;
223         NodeStack * const node_stack;
224         ModelVector<TraceAnalysis *> * trace_analyses;
225
226
227         /** Private data members that should be snapshotted. They are grouped
228          * together for efficiency and maintainability. */
229         struct model_snapshot_members * const priv;
230
231         /** A special model-checker Thread; used for associating with
232          *  model-checker-related ModelAcitons */
233         Thread *model_thread;
234
235         /**
236          * @brief The modification order graph
237          *
238          * A directed acyclic graph recording observations of the modification
239          * order on all the atomic objects in the system. This graph should
240          * never contain any cycles, as that represents a violation of the
241          * memory model (total ordering). This graph really consists of many
242          * disjoint (unconnected) subgraphs, each graph corresponding to a
243          * separate ordering on a distinct object.
244          *
245          * The edges in this graph represent the "ordered before" relation,
246          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
247          * <tt>b</tt>.
248          */
249         CycleGraph * const mo_graph;
250
251         /** @brief The cumulative execution stats */
252         struct execution_stats stats;
253         void record_stats();
254         void run_trace_analyses();
255         void print_infeasibility(const char *prefix) const;
256         bool is_feasible_prefix_ignore_relseq() const;
257         bool is_infeasible() const;
258         bool is_deadlocked() const;
259         bool too_many_steps() const;
260         bool is_complete_execution() const;
261         bool have_bug_reports() const;
262         void print_bugs() const;
263         void print_execution(bool printbugs) const;
264         void print_stats() const;
265
266         friend void user_main_wrapper();
267 };
268
269 extern ModelChecker *model;
270
271 #endif /* __MODEL_H__ */