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);
150 bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
151 bool has_asserted() const;
153 void set_bad_synchronization();
154 bool promises_expired() const;
155 void execute_sleep_set();
156 bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
157 void wake_up_sleeping_actions(ModelAction *curr);
158 modelclock_t get_next_seq_num();
160 bool next_execution();
161 ModelAction * check_current_action(ModelAction *curr);
162 bool initialize_curr_action(ModelAction **curr);
163 bool process_read(ModelAction *curr);
164 bool process_write(ModelAction *curr);
165 bool process_fence(ModelAction *curr);
166 bool process_mutex(ModelAction *curr);
167 bool process_thread_action(ModelAction *curr);
168 void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
169 bool read_from(ModelAction *act, const ModelAction *rf);
170 bool check_action_enabled(ModelAction *curr);
172 Thread * take_step(ModelAction *curr);
174 template <typename T>
175 bool check_recency(ModelAction *curr, const T *rf) const;
177 template <typename T, typename U>
178 bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
180 ModelAction * get_last_fence_conflict(ModelAction *act) const;
181 ModelAction * get_last_conflict(ModelAction *act) const;
182 void set_backtracking(ModelAction *act);
183 Thread * action_select_next_thread(const ModelAction *curr) const;
184 Thread * get_next_thread();
185 bool set_latest_backtrack(ModelAction *act);
186 ModelAction * get_next_backtrack();
187 void reset_to_initial_state();
188 Promise * pop_promise_to_resolve(const ModelAction *curr);
189 bool resolve_promise(ModelAction *curr, Promise *promise);
190 void compute_promises(ModelAction *curr);
191 void compute_relseq_breakwrites(ModelAction *curr);
193 void mo_check_promises(const ModelAction *act, bool is_read_check);
194 void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
196 void check_curr_backtracking(ModelAction *curr);
197 void add_action_to_lists(ModelAction *act);
198 ModelAction * get_last_action(thread_id_t tid) const;
199 ModelAction * get_last_fence_release(thread_id_t tid) const;
200 ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
201 ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
202 ModelAction * get_last_unlock(ModelAction *curr) const;
203 void build_may_read_from(ModelAction *curr);
204 ModelAction * process_rmw(ModelAction *curr);
206 template <typename rf_type>
207 bool r_modification_order(ModelAction *curr, const rf_type *rf);
209 bool w_modification_order(ModelAction *curr, ModelVector<ModelAction *> *send_fv);
210 void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
211 bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
212 bool resolve_release_sequences(void *location, work_queue_t *work_queue);
213 void add_future_value(const ModelAction *writer, ModelAction *reader);
215 ModelAction * get_uninitialized_action(const ModelAction *curr) const;
217 ModelAction *diverge;
218 ModelAction *earliest_diverge;
220 ucontext_t system_context;
221 action_list_t * const action_trace;
222 HashTable<int, Thread *, int> * const thread_map;
224 /** Per-object list of actions. Maps an object (i.e., memory location)
225 * to a trace of all actions performed on the object. */
226 HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_map;
228 /** Per-object list of actions. Maps an object (i.e., memory location)
229 * to a trace of all actions performed on the object. */
230 HashTable<const void *, action_list_t *, uintptr_t, 4> * const lock_waiters_map;
232 /** Per-object list of actions. Maps an object (i.e., memory location)
233 * to a trace of all actions performed on the object. */
234 HashTable<const void *, action_list_t *, uintptr_t, 4> * const condvar_waiters_map;
236 HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
237 SnapVector<Promise *> * const promises;
238 SnapVector<struct PendingFutureValue> * const futurevalues;
241 * List of pending release sequences. Release sequences might be
242 * determined lazily as promises are fulfilled and modification orders
243 * are established. Each entry in the list may only be partially
244 * filled, depending on its pending status.
246 SnapVector<struct release_seq *> * const pending_rel_seqs;
248 SnapVector<ModelAction *> * const thrd_last_action;
249 SnapVector<ModelAction *> * const thrd_last_fence_release;
250 NodeStack * const node_stack;
252 /** Private data members that should be snapshotted. They are grouped
253 * together for efficiency and maintainability. */
254 struct model_snapshot_members * const priv;
256 /** A special model-checker Thread; used for associating with
257 * model-checker-related ModelAcitons */
258 Thread *model_thread;
261 * @brief The modification order graph
263 * A directed acyclic graph recording observations of the modification
264 * order on all the atomic objects in the system. This graph should
265 * never contain any cycles, as that represents a violation of the
266 * memory model (total ordering). This graph really consists of many
267 * disjoint (unconnected) subgraphs, each graph corresponding to a
268 * separate ordering on a distinct object.
270 * The edges in this graph represent the "ordered before" relation,
271 * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
274 CycleGraph * const mo_graph;
276 /** @brief The cumulative execution stats */
277 struct execution_stats stats;
280 void print_infeasibility(const char *prefix) const;
281 bool is_feasible_prefix_ignore_relseq() const;
282 bool is_infeasible() const;
283 bool is_deadlocked() const;
284 bool is_circular_wait(const Thread *t) const;
285 bool is_complete_execution() const;
286 bool have_bug_reports() const;
287 void print_bugs() const;
288 void print_execution(bool printbugs) const;
289 void print_stats() const;
291 friend void user_main_wrapper();
294 extern ModelChecker *model;
296 #endif /* __MODEL_H__ */