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