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