8d10d51e7e2138904ae415cf8fa11c06c959765e
[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 "schedule.h"
14 #include "mymemory.h"
15 #include "libthreads.h"
16 #include "threads.h"
17 #include "action.h"
18 #include "clockvector.h"
19 #include "hashtable.h"
20 #include "workqueue.h"
21
22 /* Forward declaration */
23 class NodeStack;
24 class CycleGraph;
25 class Promise;
26
27 /** @brief Shorthand for a list of release sequence heads */
28 typedef std::vector< const ModelAction *, MyAlloc<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 };
38
39 struct PendingFutureValue {
40         uint64_t value;
41         modelclock_t expiration;
42         ModelAction * act;
43 };
44
45 /**
46  * Structure for holding small ModelChecker members that should be snapshotted
47  */
48 struct model_snapshot_members {
49         ModelAction *current_action;
50         int next_thread_id;
51         modelclock_t used_sequence_numbers;
52         Thread *nextThread;
53         ModelAction *next_backtrack;
54
55         /** @see ModelChecker::lazy_sync_size */
56         unsigned int lazy_sync_size;
57 };
58
59 /** @brief The central structure for model-checking */
60 class ModelChecker {
61 public:
62         ModelChecker(struct model_params params);
63         ~ModelChecker();
64
65         /** @returns the context for the main model-checking system thread */
66         ucontext_t * get_system_context() { return &system_context; }
67
68         /** Prints an execution summary with trace information. */
69         void print_summary();
70
71         void add_thread(Thread *t);
72         void remove_thread(Thread *t);
73         Thread * get_thread(thread_id_t tid) { return thread_map->get(id_to_int(tid)); }
74         Thread * get_thread(ModelAction *act) { return get_thread(act->get_tid()); }
75
76         thread_id_t get_next_id();
77         int get_num_threads();
78         modelclock_t get_next_seq_num();
79
80         /** @return The currently executing Thread. */
81         Thread * get_current_thread() { return scheduler->get_current_thread(); }
82
83         int switch_to_master(ModelAction *act);
84         ClockVector * get_cv(thread_id_t tid);
85         ModelAction * get_parent_action(thread_id_t tid);
86         bool next_execution();
87         bool isfeasible();
88         bool isfeasibleotherthanRMW();
89         bool isfinalfeasible();
90         void check_promises(ClockVector *old_cv, ClockVector * merge_cv);
91         void get_release_seq_heads(ModelAction *act, rel_heads_list_t *release_heads);
92         void finish_execution();
93         bool isfeasibleprefix();
94         void set_assert() {asserted=true;}
95
96         MEMALLOC
97 private:
98         /** The scheduler to use: tracks the running/ready Threads */
99         Scheduler *scheduler;
100
101         bool thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader);
102         bool has_asserted() {return asserted;}
103         void reset_asserted() {asserted=false;}
104         int num_executions;
105         int num_feasible_executions;
106         bool promises_expired();
107         const model_params params;
108
109         /**
110          * Stores the ModelAction for the current thread action.  Call this
111          * immediately before switching from user- to system-context to pass
112          * data between them.
113          * @param act The ModelAction created by the user-thread action
114          */
115         void set_current_action(ModelAction *act) { priv->current_action = act; }
116         Thread * check_current_action(ModelAction *curr);
117         ModelAction * initialize_curr_action(ModelAction *curr);
118         bool process_read(ModelAction *curr, bool second_part_of_rmw);
119         bool process_write(ModelAction *curr);
120         void process_mutex(ModelAction *curr);
121         bool check_action_enabled(ModelAction *curr);
122
123         bool take_step();
124
125         void check_recency(ModelAction *curr);
126         ModelAction * get_last_conflict(ModelAction *act);
127         void set_backtracking(ModelAction *act);
128         Thread * get_next_thread(ModelAction *curr);
129         ModelAction * get_next_backtrack();
130         void reset_to_initial_state();
131         bool resolve_promises(ModelAction *curr);
132         void compute_promises(ModelAction *curr);
133
134         void check_curr_backtracking(ModelAction * curr);
135         void add_action_to_lists(ModelAction *act);
136         ModelAction * get_last_action(thread_id_t tid);
137         ModelAction * get_last_seq_cst(ModelAction *curr);
138         ModelAction * get_last_unlock(ModelAction *curr);
139         void build_reads_from_past(ModelAction *curr);
140         ModelAction * process_rmw(ModelAction *curr);
141         void post_r_modification_order(ModelAction *curr, const ModelAction *rf);
142         bool r_modification_order(ModelAction *curr, const ModelAction *rf);
143         bool w_modification_order(ModelAction *curr);
144         bool release_seq_head(const ModelAction *rf, rel_heads_list_t *release_heads) const;
145         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
146         void do_complete_join(ModelAction *join);
147
148         ModelAction *diverge;
149
150         ucontext_t system_context;
151         action_list_t *action_trace;
152         HashTable<int, Thread *, int> *thread_map;
153
154         /** Per-object list of actions. Maps an object (i.e., memory location)
155          * to a trace of all actions performed on the object. */
156         HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
157
158         /** Per-object list of actions. Maps an object (i.e., memory location)
159          * to a trace of all actions performed on the object. */
160         HashTable<const void *, action_list_t, uintptr_t, 4> *lock_waiters_map;
161
162         HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
163         std::vector<Promise *> *promises;
164         std::vector<struct PendingFutureValue> *futurevalues;
165
166         /**
167          * Collection of lists of objects that might synchronize with one or
168          * more release sequence. Release sequences might be determined lazily
169          * as promises are fulfilled and modification orders are established.
170          * This structure maps its lists by object location. Each ModelAction
171          * in the lists should be an acquire operation.
172          */
173         HashTable<void *, action_list_t, uintptr_t, 4> *lazy_sync_with_release;
174
175         /**
176          * Represents the total size of the
177          * ModelChecker::lazy_sync_with_release lists. This count should be
178          * snapshotted, so it is actually a pointer to a location within
179          * ModelChecker::priv
180          */
181         unsigned int *lazy_sync_size;
182
183         std::vector<ModelAction *> *thrd_last_action;
184         NodeStack *node_stack;
185
186         /** Private data members that should be snapshotted. They are grouped
187          * together for efficiency and maintainability. */
188         struct model_snapshot_members *priv;
189
190         /**
191          * @brief The modification order graph
192          *
193          * A directed acyclic graph recording observations of the modification
194          * order on all the atomic objects in the system. This graph should
195          * never contain any cycles, as that represents a violation of the
196          * memory model (total ordering). This graph really consists of many
197          * disjoint (unconnected) subgraphs, each graph corresponding to a
198          * separate ordering on a distinct object.
199          *
200          * The edges in this graph represent the "ordered before" relation,
201          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
202          * <tt>b</tt>.
203          */
204         CycleGraph *mo_graph;
205         bool failed_promise;
206         bool too_many_reads;
207         bool asserted;
208 };
209
210 extern ModelChecker *model;
211
212 #endif /* __MODEL_H__ */