2 * @brief Core model checker.
12 #include "hashtable.h"
13 #include "workqueue.h"
15 #include "modeltypes.h"
16 #include "stl-model.h"
19 /* 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;
32 typedef SnapList<ModelAction *> action_list_t;
35 * Model checker parameter structure. Holds run-time configuration options for
42 bool sc_trace_analysis;
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 *reader) :
78 writer(writer), reader(reader)
80 const ModelAction *writer;
84 /** @brief Records information regarding a single pending release sequence */
86 /** @brief The acquire operation */
88 /** @brief The read operation that may read from a release sequence;
89 * may be the same as acquire, or else an earlier action in the same
90 * thread (i.e., when 'acquire' is a fence-acquire) */
91 const ModelAction *read;
92 /** @brief The head of the RMW chain from which 'read' reads; may be
93 * equal to 'release' */
94 const ModelAction *rf;
95 /** @brief The head of the potential longest release sequence chain */
96 const ModelAction *release;
97 /** @brief The write(s) that may break the release sequence */
98 SnapVector<const ModelAction *> writes;
101 /** @brief The central structure for model-checking */
104 ModelChecker(struct model_params params);
109 /** @returns the context for the main model-checking system thread */
110 ucontext_t * get_system_context() { return &system_context; }
112 void print_summary() const;
113 #if SUPPORT_MOD_ORDER_DUMP
114 void dumpGraph(char *filename) const;
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;
141 void add_trace_analysis(Trace_Analysis * a) {
142 trace_analyses->push_back(a);
145 action_list_t * get_actions_on_obj(void * obj, thread_id_t tid);
146 ModelAction * get_last_action(thread_id_t tid) const;
150 /** The scheduler to use: tracks the running/ready Threads */
151 Scheduler * const scheduler;
153 void add_thread(Thread *t);
155 bool sleep_can_read_from(ModelAction *curr, const ModelAction *write);
156 bool thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader) const;
157 bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
158 bool promises_may_allow(const ModelAction *writer, const ModelAction *reader) const;
159 bool has_asserted() const;
161 void set_bad_synchronization();
162 bool promises_expired() const;
163 void execute_sleep_set();
164 bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
165 void wake_up_sleeping_actions(ModelAction *curr);
166 modelclock_t get_next_seq_num();
168 bool next_execution();
169 ModelAction * check_current_action(ModelAction *curr);
170 bool initialize_curr_action(ModelAction **curr);
171 bool process_read(ModelAction *curr);
172 bool process_write(ModelAction *curr);
173 bool process_fence(ModelAction *curr);
174 bool process_mutex(ModelAction *curr);
175 bool process_thread_action(ModelAction *curr);
176 void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
177 bool read_from(ModelAction *act, const ModelAction *rf);
178 bool check_action_enabled(ModelAction *curr);
180 Thread * take_step(ModelAction *curr);
181 bool should_terminate_execution();
183 template <typename T>
184 bool check_recency(ModelAction *curr, const T *rf) const;
186 template <typename T, typename U>
187 bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
189 ModelAction * get_last_fence_conflict(ModelAction *act) const;
190 ModelAction * get_last_conflict(ModelAction *act) const;
191 void set_backtracking(ModelAction *act);
192 Thread * action_select_next_thread(const ModelAction *curr) const;
193 Thread * get_next_thread();
194 bool set_latest_backtrack(ModelAction *act);
195 ModelAction * get_next_backtrack();
196 void reset_to_initial_state();
197 Promise * pop_promise_to_resolve(const ModelAction *curr);
198 bool resolve_promise(ModelAction *curr, Promise *promise);
199 void compute_promises(ModelAction *curr);
200 void compute_relseq_breakwrites(ModelAction *curr);
202 void mo_check_promises(const ModelAction *act, bool is_read_check);
203 void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
205 void check_curr_backtracking(ModelAction *curr);
206 void add_action_to_lists(ModelAction *act);
207 ModelAction * get_last_fence_release(thread_id_t tid) const;
208 ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
209 ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
210 ModelAction * get_last_unlock(ModelAction *curr) const;
211 void build_may_read_from(ModelAction *curr);
212 ModelAction * process_rmw(ModelAction *curr);
214 template <typename rf_type>
215 bool r_modification_order(ModelAction *curr, const rf_type *rf);
217 bool w_modification_order(ModelAction *curr, ModelVector<ModelAction *> *send_fv);
218 void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
219 bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
220 bool resolve_release_sequences(void *location, work_queue_t *work_queue);
221 void add_future_value(const ModelAction *writer, ModelAction *reader);
223 ModelAction * get_uninitialized_action(const ModelAction *curr) const;
225 ModelAction *diverge;
226 ModelAction *earliest_diverge;
228 ucontext_t system_context;
229 action_list_t * const action_trace;
230 HashTable<int, Thread *, int> * const thread_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 obj_map;
236 /** Per-object list of actions. Maps an object (i.e., memory location)
237 * to a trace of all actions performed on the object. */
238 HashTable<const void *, action_list_t *, uintptr_t, 4> * const condvar_waiters_map;
240 HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
241 SnapVector<Promise *> * const promises;
242 SnapVector<struct PendingFutureValue> * const futurevalues;
245 * List of pending release sequences. Release sequences might be
246 * determined lazily as promises are fulfilled and modification orders
247 * are established. Each entry in the list may only be partially
248 * filled, depending on its pending status.
250 SnapVector<struct release_seq *> * const pending_rel_seqs;
252 SnapVector<ModelAction *> * const thrd_last_action;
253 SnapVector<ModelAction *> * const thrd_last_fence_release;
254 NodeStack * const node_stack;
255 ModelVector<Trace_Analysis *> * trace_analyses;
258 /** Private data members that should be snapshotted. They are grouped
259 * together for efficiency and maintainability. */
260 struct model_snapshot_members * const priv;
262 /** A special model-checker Thread; used for associating with
263 * model-checker-related ModelAcitons */
264 Thread *model_thread;
267 * @brief The modification order graph
269 * A directed acyclic graph recording observations of the modification
270 * order on all the atomic objects in the system. This graph should
271 * never contain any cycles, as that represents a violation of the
272 * memory model (total ordering). This graph really consists of many
273 * disjoint (unconnected) subgraphs, each graph corresponding to a
274 * separate ordering on a distinct object.
276 * The edges in this graph represent the "ordered before" relation,
277 * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
280 CycleGraph * const mo_graph;
282 /** @brief The cumulative execution stats */
283 struct execution_stats stats;
285 void run_trace_analyses();
286 void print_infeasibility(const char *prefix) const;
287 bool is_feasible_prefix_ignore_relseq() const;
288 bool is_infeasible() const;
289 bool is_deadlocked() const;
290 bool is_complete_execution() const;
291 bool have_bug_reports() const;
292 void print_bugs() const;
293 void print_execution(bool printbugs) const;
294 void print_stats() const;
296 friend void user_main_wrapper();
299 extern ModelChecker *model;
301 #endif /* __MODEL_H__ */