clean out includes, etc.
[model-checker.git] / execution.h
1 /** @file execution.h
2  *  @brief Model-checker core
3  */
4
5 #ifndef __EXECUTION_H__
6 #define __EXECUTION_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 "params.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 struct model_snapshot_members;
28 class ModelChecker;
29 struct bug_message;
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 struct PendingFutureValue {
36         PendingFutureValue(ModelAction *writer, ModelAction *reader) :
37                 writer(writer), reader(reader)
38         { }
39         const ModelAction *writer;
40         ModelAction *reader;
41 };
42
43 /** @brief Records information regarding a single pending release sequence */
44 struct release_seq {
45         /** @brief The acquire operation */
46         ModelAction *acquire;
47         /** @brief The read operation that may read from a release sequence;
48          *  may be the same as acquire, or else an earlier action in the same
49          *  thread (i.e., when 'acquire' is a fence-acquire) */
50         const ModelAction *read;
51         /** @brief The head of the RMW chain from which 'read' reads; may be
52          *  equal to 'release' */
53         const ModelAction *rf;
54         /** @brief The head of the potential longest release sequence chain */
55         const ModelAction *release;
56         /** @brief The write(s) that may break the release sequence */
57         SnapVector<const ModelAction *> writes;
58 };
59
60 /** @brief The central structure for model-checking */
61 class ModelExecution {
62 public:
63         ModelExecution(struct model_params *params, Scheduler *scheduler, NodeStack *node_stack);
64         ~ModelExecution();
65
66         Thread * take_step(ModelAction *curr);
67         void fixup_release_sequences();
68
69         void print_summary() const;
70 #if SUPPORT_MOD_ORDER_DUMP
71         void dumpGraph(char *filename) const;
72 #endif
73
74         void add_thread(Thread *t);
75         Thread * get_thread(thread_id_t tid) const;
76         Thread * get_thread(const ModelAction *act) const;
77         int get_promise_number(const Promise *promise) const;
78
79         bool is_enabled(Thread *t) const;
80         bool is_enabled(thread_id_t tid) const;
81
82         thread_id_t get_next_id();
83         unsigned int get_num_threads() const;
84
85         ClockVector * get_cv(thread_id_t tid) const;
86         ModelAction * get_parent_action(thread_id_t tid) const;
87         void check_promises_thread_disabled();
88         bool isfeasibleprefix() const;
89
90         action_list_t * get_actions_on_obj(void * obj, thread_id_t tid) const;
91         ModelAction * get_last_action(thread_id_t tid) const;
92
93         bool check_action_enabled(ModelAction *curr);
94
95         bool assert_bug(const char *msg);
96         bool have_bug_reports() const;
97         SnapVector<bug_message *> * get_bugs() const;
98
99         bool has_asserted() const;
100         void set_assert();
101         bool is_complete_execution() const;
102
103         void print_infeasibility(const char *prefix) const;
104         bool is_feasible_prefix_ignore_relseq() const;
105         bool is_infeasible() const;
106         bool is_deadlocked() const;
107         bool too_many_steps() const;
108
109         ModelAction * get_next_backtrack();
110
111         action_list_t * get_action_trace() const { return action_trace; }
112
113         void increment_execution_number() { execution_number++; }
114
115         MEMALLOC
116 private:
117         const model_params * const params;
118
119         /** The scheduler to use: tracks the running/ready Threads */
120         Scheduler * const scheduler;
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         void set_bad_synchronization();
127         bool promises_expired() const;
128         bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
129         void wake_up_sleeping_actions(ModelAction *curr);
130         modelclock_t get_next_seq_num();
131
132         bool next_execution();
133         ModelAction * check_current_action(ModelAction *curr);
134         bool initialize_curr_action(ModelAction **curr);
135         bool process_read(ModelAction *curr);
136         bool process_write(ModelAction *curr);
137         bool process_fence(ModelAction *curr);
138         bool process_mutex(ModelAction *curr);
139         bool process_thread_action(ModelAction *curr);
140         void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
141         bool read_from(ModelAction *act, const ModelAction *rf);
142         bool synchronize(const ModelAction *first, ModelAction *second);
143
144         template <typename T>
145         bool check_recency(ModelAction *curr, const T *rf) const;
146
147         template <typename T, typename U>
148         bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
149
150         ModelAction * get_last_fence_conflict(ModelAction *act) const;
151         ModelAction * get_last_conflict(ModelAction *act) const;
152         void set_backtracking(ModelAction *act);
153         bool set_latest_backtrack(ModelAction *act);
154         Promise * pop_promise_to_resolve(const ModelAction *curr);
155         bool resolve_promise(ModelAction *curr, Promise *promise);
156         void compute_promises(ModelAction *curr);
157         void compute_relseq_breakwrites(ModelAction *curr);
158
159         void check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv);
160         void mo_check_promises(const ModelAction *act, bool is_read_check);
161         void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
162
163         void check_curr_backtracking(ModelAction *curr);
164         void add_action_to_lists(ModelAction *act);
165         ModelAction * get_last_fence_release(thread_id_t tid) const;
166         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
167         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
168         ModelAction * get_last_unlock(ModelAction *curr) const;
169         void build_may_read_from(ModelAction *curr);
170         ModelAction * process_rmw(ModelAction *curr);
171
172         template <typename rf_type>
173         bool r_modification_order(ModelAction *curr, const rf_type *rf);
174
175         bool w_modification_order(ModelAction *curr, ModelVector<ModelAction *> *send_fv);
176         void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
177         bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
178         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
179         void add_future_value(const ModelAction *writer, ModelAction *reader);
180
181         ModelAction * get_uninitialized_action(const ModelAction *curr) const;
182
183         action_list_t * const action_trace;
184         HashTable<int, Thread *, int> * const thread_map;
185
186         /** Per-object list of actions. Maps an object (i.e., memory location)
187          * to a trace of all actions performed on the object. */
188         HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_map;
189
190         /** Per-object list of actions. Maps an object (i.e., memory location)
191          * to a trace of all actions performed on the object. */
192         HashTable<const void *, action_list_t *, uintptr_t, 4> * const condvar_waiters_map;
193
194         HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
195         SnapVector<Promise *> * const promises;
196         SnapVector<struct PendingFutureValue> * const futurevalues;
197
198         /**
199          * List of pending release sequences. Release sequences might be
200          * determined lazily as promises are fulfilled and modification orders
201          * are established. Each entry in the list may only be partially
202          * filled, depending on its pending status.
203          */
204         SnapVector<struct release_seq *> * const pending_rel_seqs;
205
206         SnapVector<ModelAction *> * const thrd_last_action;
207         SnapVector<ModelAction *> * const thrd_last_fence_release;
208         NodeStack * const node_stack;
209
210         /** A special model-checker Thread; used for associating with
211          *  model-checker-related ModelAcitons */
212         Thread *model_thread;
213
214         /** Private data members that should be snapshotted. They are grouped
215          * together for efficiency and maintainability. */
216         struct model_snapshot_members * const priv;
217
218         /**
219          * @brief The modification order graph
220          *
221          * A directed acyclic graph recording observations of the modification
222          * order on all the atomic objects in the system. This graph should
223          * never contain any cycles, as that represents a violation of the
224          * memory model (total ordering). This graph really consists of many
225          * disjoint (unconnected) subgraphs, each graph corresponding to a
226          * separate ordering on a distinct object.
227          *
228          * The edges in this graph represent the "ordered before" relation,
229          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
230          * <tt>b</tt>.
231          */
232         CycleGraph * const mo_graph;
233
234         int execution_number;
235
236         Thread * action_select_next_thread(const ModelAction *curr) const;
237 };
238
239 #endif /* __EXECUTION_H__ */