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