2 * @brief Core model checker.
13 #include "hashtable.h"
14 #include "workqueue.h"
16 #include "modeltypes.h"
17 #include "stl-model.h"
19 /* Forward declaration */
27 struct model_snapshot_members;
29 /** @brief Shorthand for a list of release sequence heads */
30 typedef ModelVector<const ModelAction *> rel_heads_list_t;
32 typedef SnapList<ModelAction *> action_list_t;
35 * Model checker parameter structure. Holds run-time configuration options for
42 unsigned int fairwindow;
43 unsigned int enabledcount;
45 unsigned int uninitvalue;
47 /** @brief Maximum number of future values that can be sent to the same
51 /** @brief Only generate a new future value/expiration pair if the
52 * expiration time exceeds the existing one by more than the slop
54 unsigned int expireslop;
56 /** @brief Verbosity (0 = quiet; 1 = noisy) */
59 /** @brief Command-line argument count to pass to user program */
62 /** @brief Command-line arguments to pass to user program */
66 /** @brief Model checker execution stats */
67 struct execution_stats {
68 int num_total; /**< @brief Total number of executions */
69 int num_infeasible; /**< @brief Number of infeasible executions */
70 int num_buggy_executions; /** @brief Number of buggy executions */
71 int num_complete; /**< @brief Number of feasible, non-buggy, complete executions */
72 int num_redundant; /**< @brief Number of redundant, aborted executions */
75 struct PendingFutureValue {
76 PendingFutureValue(ModelAction *writer, ModelAction *reader) :
77 writer(writer), reader(reader)
79 const ModelAction *writer;
83 /** @brief Records information regarding a single pending release sequence */
85 /** @brief The acquire operation */
87 /** @brief The read operation that may read from a release sequence;
88 * may be the same as acquire, or else an earlier action in the same
89 * thread (i.e., when 'acquire' is a fence-acquire) */
90 const ModelAction *read;
91 /** @brief The head of the RMW chain from which 'read' reads; may be
92 * equal to 'release' */
93 const ModelAction *rf;
94 /** @brief The head of the potential longest release sequence chain */
95 const ModelAction *release;
96 /** @brief The write(s) that may break the release sequence */
97 SnapVector<const ModelAction *> writes;
100 /** @brief The central structure for model-checking */
103 ModelChecker(struct model_params params);
108 /** @returns the context for the main model-checking system thread */
109 ucontext_t * get_system_context() { return &system_context; }
111 void print_summary() const;
112 #if SUPPORT_MOD_ORDER_DUMP
113 void dumpGraph(char *filename) const;
116 Thread * get_thread(thread_id_t tid) const;
117 Thread * get_thread(const ModelAction *act) const;
118 int get_promise_number(const Promise *promise) const;
120 bool is_enabled(Thread *t) const;
121 bool is_enabled(thread_id_t tid) const;
123 thread_id_t get_next_id();
124 unsigned int get_num_threads() const;
125 Thread * get_current_thread() const;
127 void switch_from_master(Thread *thread);
128 uint64_t switch_to_master(ModelAction *act);
129 ClockVector * get_cv(thread_id_t tid) const;
130 ModelAction * get_parent_action(thread_id_t tid) const;
131 void check_promises_thread_disabled();
132 void check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv);
133 bool isfeasibleprefix() const;
135 bool assert_bug(const char *msg);
136 void assert_user_bug(const char *msg);
138 const model_params params;
139 Node * get_curr_node() const;
143 /** The scheduler to use: tracks the running/ready Threads */
144 Scheduler * const scheduler;
146 void add_thread(Thread *t);
148 bool sleep_can_read_from(ModelAction *curr, const ModelAction *write);
149 bool thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader) const;
150 bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
151 bool promises_may_allow(const ModelAction *writer, const ModelAction *reader) const;
152 bool has_asserted() const;
154 void set_bad_synchronization();
155 bool promises_expired() const;
156 void execute_sleep_set();
157 bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
158 void wake_up_sleeping_actions(ModelAction *curr);
159 modelclock_t get_next_seq_num();
161 bool next_execution();
162 ModelAction * check_current_action(ModelAction *curr);
163 bool initialize_curr_action(ModelAction **curr);
164 bool process_read(ModelAction *curr);
165 bool process_write(ModelAction *curr);
166 bool process_fence(ModelAction *curr);
167 bool process_mutex(ModelAction *curr);
168 bool process_thread_action(ModelAction *curr);
169 void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
170 bool read_from(ModelAction *act, const ModelAction *rf);
171 bool check_action_enabled(ModelAction *curr);
173 Thread * take_step(ModelAction *curr);
175 template <typename T>
176 bool check_recency(ModelAction *curr, const T *rf) const;
178 template <typename T, typename U>
179 bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
181 ModelAction * get_last_fence_conflict(ModelAction *act) const;
182 ModelAction * get_last_conflict(ModelAction *act) const;
183 void set_backtracking(ModelAction *act);
184 Thread * action_select_next_thread(const ModelAction *curr) const;
185 Thread * get_next_thread();
186 bool set_latest_backtrack(ModelAction *act);
187 ModelAction * get_next_backtrack();
188 void reset_to_initial_state();
189 Promise * pop_promise_to_resolve(const ModelAction *curr);
190 bool resolve_promise(ModelAction *curr, Promise *promise);
191 void compute_promises(ModelAction *curr);
192 void compute_relseq_breakwrites(ModelAction *curr);
194 void mo_check_promises(const ModelAction *act, bool is_read_check);
195 void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
197 void check_curr_backtracking(ModelAction *curr);
198 void add_action_to_lists(ModelAction *act);
199 ModelAction * get_last_action(thread_id_t tid) const;
200 ModelAction * get_last_fence_release(thread_id_t tid) const;
201 ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
202 ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
203 ModelAction * get_last_unlock(ModelAction *curr) const;
204 void build_may_read_from(ModelAction *curr);
205 ModelAction * process_rmw(ModelAction *curr);
207 template <typename rf_type>
208 bool r_modification_order(ModelAction *curr, const rf_type *rf);
210 bool w_modification_order(ModelAction *curr, ModelVector<ModelAction *> *send_fv);
211 void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
212 bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
213 bool resolve_release_sequences(void *location, work_queue_t *work_queue);
214 void add_future_value(const ModelAction *writer, ModelAction *reader);
216 ModelAction * get_uninitialized_action(const ModelAction *curr) const;
218 ModelAction *diverge;
219 ModelAction *earliest_diverge;
221 ucontext_t system_context;
222 action_list_t * const action_trace;
223 HashTable<int, Thread *, int> * const thread_map;
225 /** Per-object list of actions. Maps an object (i.e., memory location)
226 * to a trace of all actions performed on the object. */
227 HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_map;
229 /** Per-object list of actions. Maps an object (i.e., memory location)
230 * to a trace of all actions performed on the object. */
231 HashTable<const void *, action_list_t *, uintptr_t, 4> * const lock_waiters_map;
233 /** Per-object list of actions. Maps an object (i.e., memory location)
234 * to a trace of all actions performed on the object. */
235 HashTable<const void *, action_list_t *, uintptr_t, 4> * const condvar_waiters_map;
237 HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
238 SnapVector<Promise *> * const promises;
239 SnapVector<struct PendingFutureValue> * const futurevalues;
242 * List of pending release sequences. Release sequences might be
243 * determined lazily as promises are fulfilled and modification orders
244 * are established. Each entry in the list may only be partially
245 * filled, depending on its pending status.
247 SnapVector<struct release_seq *> * const pending_rel_seqs;
249 SnapVector<ModelAction *> * const thrd_last_action;
250 SnapVector<ModelAction *> * const thrd_last_fence_release;
251 NodeStack * const node_stack;
253 /** Private data members that should be snapshotted. They are grouped
254 * together for efficiency and maintainability. */
255 struct model_snapshot_members * const priv;
257 /** A special model-checker Thread; used for associating with
258 * model-checker-related ModelAcitons */
259 Thread *model_thread;
262 * @brief The modification order graph
264 * A directed acyclic graph recording observations of the modification
265 * order on all the atomic objects in the system. This graph should
266 * never contain any cycles, as that represents a violation of the
267 * memory model (total ordering). This graph really consists of many
268 * disjoint (unconnected) subgraphs, each graph corresponding to a
269 * separate ordering on a distinct object.
271 * The edges in this graph represent the "ordered before" relation,
272 * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
275 CycleGraph * const mo_graph;
277 /** @brief The cumulative execution stats */
278 struct execution_stats stats;
281 void print_infeasibility(const char *prefix) const;
282 bool is_feasible_prefix_ignore_relseq() const;
283 bool is_infeasible() const;
284 bool is_deadlocked() const;
285 bool is_circular_wait(const Thread *t) const;
286 bool is_complete_execution() const;
287 bool have_bug_reports() const;
288 void print_bugs() const;
289 void print_execution(bool printbugs) const;
290 void print_stats() const;
292 friend void user_main_wrapper();
295 extern ModelChecker *model;
297 #endif /* __MODEL_H__ */