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