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