model: add deadlock detection
[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 <list>
9 #include <vector>
10 #include <cstddef>
11 #include <ucontext.h>
12
13 #include "mymemory.h"
14 #include "action.h"
15 #include "hashtable.h"
16 #include "workqueue.h"
17 #include "config.h"
18 #include "modeltypes.h"
19
20 /* Forward declaration */
21 class NodeStack;
22 class CycleGraph;
23 class Promise;
24 class Scheduler;
25 class Thread;
26
27 /** @brief Shorthand for a list of release sequence heads */
28 typedef std::vector< const ModelAction *, ModelAlloc<const ModelAction *> > rel_heads_list_t;
29
30 /**
31  * Model checker parameter structure. Holds run-time configuration options for
32  * the model checker.
33  */
34 struct model_params {
35         int maxreads;
36         int maxfuturedelay;
37         unsigned int fairwindow;
38         unsigned int enabledcount;
39         unsigned int bound;
40
41         /** @brief Maximum number of future values that can be sent to the same
42          *  read */
43         int maxfuturevalues;
44
45         /** @brief Only generate a new future value/expiration pair if the
46          *  expiration time exceeds the existing one by more than the slop
47          *  value */
48         unsigned int expireslop;
49 };
50
51 struct PendingFutureValue {
52         ModelAction *writer;
53         ModelAction * act;
54 };
55
56 /**
57  * Structure for holding small ModelChecker members that should be snapshotted
58  */
59 struct model_snapshot_members {
60         ModelAction *current_action;
61         unsigned int next_thread_id;
62         modelclock_t used_sequence_numbers;
63         Thread *nextThread;
64         ModelAction *next_backtrack;
65 };
66
67 /** @brief Records information regarding a single pending release sequence */
68 struct release_seq {
69         /** @brief The acquire operation */
70         ModelAction *acquire;
71         /** @brief The head of the RMW chain from which 'acquire' reads; may be
72          *  equal to 'release' */
73         const ModelAction *rf;
74         /** @brief The head of the potential longest release sequence chain */
75         const ModelAction *release;
76         /** @brief The write(s) that may break the release sequence */
77         std::vector<const ModelAction *> writes;
78 };
79
80 /** @brief The central structure for model-checking */
81 class ModelChecker {
82 public:
83         ModelChecker(struct model_params params);
84         ~ModelChecker();
85
86         /** @returns the context for the main model-checking system thread */
87         ucontext_t * get_system_context() { return &system_context; }
88
89         /** Prints an execution summary with trace information. */
90         void print_summary();
91 #if SUPPORT_MOD_ORDER_DUMP
92         void dumpGraph(char *filename);
93 #endif
94
95         void add_thread(Thread *t);
96         void remove_thread(Thread *t);
97         Thread * get_thread(thread_id_t tid) const;
98         Thread * get_thread(ModelAction *act) const;
99
100         thread_id_t get_next_id();
101         unsigned int get_num_threads() const;
102         Thread * get_current_thread();
103
104         int switch_to_master(ModelAction *act);
105         ClockVector * get_cv(thread_id_t tid);
106         ModelAction * get_parent_action(thread_id_t tid);
107         bool next_execution();
108         bool isfeasible();
109         bool isfeasibleotherthanRMW();
110         bool isfinalfeasible();
111         void check_promises_thread_disabled();
112         void mo_check_promises(thread_id_t tid, const ModelAction *write);
113         void check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector * merge_cv);
114         void get_release_seq_heads(ModelAction *act, rel_heads_list_t *release_heads);
115         void finish_execution();
116         bool isfeasibleprefix();
117         void set_assert() {asserted=true;}
118         bool is_deadlocked() const;
119
120         /** @brief Alert the model-checker that an incorrectly-ordered
121          * synchronization was made */
122         void set_bad_synchronization() { bad_synchronization = true; }
123
124         const model_params params;
125         Scheduler * get_scheduler() { return scheduler;}
126         Node * get_curr_node();
127
128         MEMALLOC
129 private:
130         /** The scheduler to use: tracks the running/ready Threads */
131         Scheduler *scheduler;
132
133         bool sleep_can_read_from(ModelAction * curr, const ModelAction *write);
134         bool thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader);
135         bool mo_may_allow(const ModelAction * writer, const ModelAction *reader);
136         bool has_asserted() {return asserted;}
137         void reset_asserted() {asserted=false;}
138         int num_executions;
139         int num_feasible_executions;
140         bool promises_expired();
141         void execute_sleep_set();
142         void wake_up_sleeping_actions(ModelAction * curr);
143         modelclock_t get_next_seq_num();
144
145         /**
146          * Stores the ModelAction for the current thread action.  Call this
147          * immediately before switching from user- to system-context to pass
148          * data between them.
149          * @param act The ModelAction created by the user-thread action
150          */
151         void set_current_action(ModelAction *act) { priv->current_action = act; }
152         Thread * check_current_action(ModelAction *curr);
153         bool initialize_curr_action(ModelAction **curr);
154         bool process_read(ModelAction *curr, bool second_part_of_rmw);
155         bool process_write(ModelAction *curr);
156         bool process_mutex(ModelAction *curr);
157         bool process_thread_action(ModelAction *curr);
158         void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
159         bool check_action_enabled(ModelAction *curr);
160
161         bool take_step();
162
163         void check_recency(ModelAction *curr, const ModelAction *rf);
164         ModelAction * get_last_conflict(ModelAction *act);
165         void set_backtracking(ModelAction *act);
166         Thread * get_next_thread(ModelAction *curr);
167         ModelAction * get_next_backtrack();
168         void reset_to_initial_state();
169         bool resolve_promises(ModelAction *curr);
170         void compute_promises(ModelAction *curr);
171         void compute_relseq_breakwrites(ModelAction *curr);
172
173         void check_curr_backtracking(ModelAction * curr);
174         void add_action_to_lists(ModelAction *act);
175         ModelAction * get_last_action(thread_id_t tid) const;
176         ModelAction * get_last_seq_cst(ModelAction *curr) const;
177         ModelAction * get_last_unlock(ModelAction *curr) const;
178         void build_reads_from_past(ModelAction *curr);
179         ModelAction * process_rmw(ModelAction *curr);
180         void post_r_modification_order(ModelAction *curr, const ModelAction *rf);
181         bool r_modification_order(ModelAction *curr, const ModelAction *rf);
182         bool w_modification_order(ModelAction *curr);
183         bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
184         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
185
186         ModelAction *diverge;
187         ModelAction *earliest_diverge;
188
189         ucontext_t system_context;
190         action_list_t *action_trace;
191         HashTable<int, Thread *, int> *thread_map;
192
193         /** Per-object list of actions. Maps an object (i.e., memory location)
194          * to a trace of all actions performed on the object. */
195         HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
196
197         /** Per-object list of actions. Maps an object (i.e., memory location)
198          * to a trace of all actions performed on the object. */
199         HashTable<const void *, action_list_t, uintptr_t, 4> *lock_waiters_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> *condvar_waiters_map;
204
205         HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
206         std::vector< Promise *, SnapshotAlloc<Promise *> > *promises;
207         std::vector< struct PendingFutureValue, SnapshotAlloc<struct PendingFutureValue> > *futurevalues;
208
209         /**
210          * List of pending release sequences. Release sequences might be
211          * determined lazily as promises are fulfilled and modification orders
212          * are established. Each entry in the list may only be partially
213          * filled, depending on its pending status.
214          */
215         std::vector< struct release_seq *, SnapshotAlloc<struct release_seq *> > *pending_rel_seqs;
216
217         std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > *thrd_last_action;
218         NodeStack *node_stack;
219
220         /** Private data members that should be snapshotted. They are grouped
221          * together for efficiency and maintainability. */
222         struct model_snapshot_members *priv;
223
224         /** A special model-checker Thread; used for associating with
225          *  model-checker-related ModelAcitons */
226         Thread *model_thread;
227
228         /**
229          * @brief The modification order graph
230          *
231          * A directed acyclic graph recording observations of the modification
232          * order on all the atomic objects in the system. This graph should
233          * never contain any cycles, as that represents a violation of the
234          * memory model (total ordering). This graph really consists of many
235          * disjoint (unconnected) subgraphs, each graph corresponding to a
236          * separate ordering on a distinct object.
237          *
238          * The edges in this graph represent the "ordered before" relation,
239          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
240          * <tt>b</tt>.
241          */
242         CycleGraph *mo_graph;
243         bool failed_promise;
244         bool too_many_reads;
245         bool asserted;
246         /** @brief Incorrectly-ordered synchronization was made */
247         bool bad_synchronization;
248 };
249
250 extern ModelChecker *model;
251
252 #endif /* __MODEL_H__ */