2 * @brief Core model checker.
14 #include "hashtable.h"
15 #include "workqueue.h"
17 #include "modeltypes.h"
19 /* Forward declaration */
27 struct model_snapshot_members;
29 /** @brief Shorthand for a list of release sequence heads */
30 typedef std::vector< const ModelAction *, ModelAlloc<const ModelAction *> > rel_heads_list_t;
32 typedef std::list< ModelAction *, SnapshotAlloc<ModelAction *> > action_list_t;
35 * Model checker parameter structure. Holds run-time configuration options for
41 unsigned int fairwindow;
42 unsigned int enabledcount;
45 /** @brief Maximum number of future values that can be sent to the same
49 /** @brief Only generate a new future value/expiration pair if the
50 * expiration time exceeds the existing one by more than the slop
52 unsigned int expireslop;
54 /** @brief Verbosity (0 = quiet; 1 = noisy) */
57 /** @brief Command-line argument count to pass to user program */
60 /** @brief Command-line arguments to pass to user program */
64 /** @brief Model checker execution stats */
65 struct execution_stats {
66 int num_total; /**< @brief Total number of executions */
67 int num_infeasible; /**< @brief Number of infeasible executions */
68 int num_buggy_executions; /** @brief Number of buggy executions */
69 int num_complete; /**< @brief Number of feasible, non-buggy, complete executions */
70 int num_redundant; /**< @brief Number of redundant, aborted executions */
73 struct PendingFutureValue {
74 PendingFutureValue(ModelAction *writer, ModelAction *act) : writer(writer), act(act) { }
75 const ModelAction *writer;
79 /** @brief Records information regarding a single pending release sequence */
81 /** @brief The acquire operation */
83 /** @brief The read operation that may read from a release sequence;
84 * may be the same as acquire, or else an earlier action in the same
85 * thread (i.e., when 'acquire' is a fence-acquire) */
86 const ModelAction *read;
87 /** @brief The head of the RMW chain from which 'read' reads; may be
88 * equal to 'release' */
89 const ModelAction *rf;
90 /** @brief The head of the potential longest release sequence chain */
91 const ModelAction *release;
92 /** @brief The write(s) that may break the release sequence */
93 std::vector<const ModelAction *> writes;
96 /** @brief The central structure for model-checking */
99 ModelChecker(struct model_params params);
104 /** @returns the context for the main model-checking system thread */
105 ucontext_t * get_system_context() { return &system_context; }
107 void print_summary() const;
108 #if SUPPORT_MOD_ORDER_DUMP
109 void dumpGraph(char *filename) const;
112 void add_thread(Thread *t);
113 void remove_thread(Thread *t);
114 Thread * get_thread(thread_id_t tid) const;
115 Thread * get_thread(const ModelAction *act) const;
116 int get_promise_number(const Promise *promise) const;
118 bool is_enabled(Thread *t) const;
119 bool is_enabled(thread_id_t tid) const;
121 thread_id_t get_next_id();
122 unsigned int get_num_threads() const;
123 Thread * get_current_thread() const;
125 void switch_from_master(Thread *thread);
126 uint64_t switch_to_master(ModelAction *act);
127 ClockVector * get_cv(thread_id_t tid) const;
128 ModelAction * get_parent_action(thread_id_t tid) const;
129 void check_promises_thread_disabled();
130 void check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv);
131 bool isfeasibleprefix() const;
133 bool assert_bug(const char *msg);
134 void assert_user_bug(const char *msg);
136 const model_params params;
137 Node * get_curr_node() const;
141 /** The scheduler to use: tracks the running/ready Threads */
142 Scheduler * const scheduler;
144 bool sleep_can_read_from(ModelAction *curr, const ModelAction *write);
145 bool thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader);
146 bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
147 bool has_asserted() const;
149 void set_bad_synchronization();
150 bool promises_expired() const;
151 void execute_sleep_set();
152 bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
153 void wake_up_sleeping_actions(ModelAction *curr);
154 modelclock_t get_next_seq_num();
156 bool next_execution();
157 ModelAction * check_current_action(ModelAction *curr);
158 bool initialize_curr_action(ModelAction **curr);
159 bool process_read(ModelAction *curr);
160 bool process_write(ModelAction *curr);
161 bool process_fence(ModelAction *curr);
162 bool process_mutex(ModelAction *curr);
163 bool process_thread_action(ModelAction *curr);
164 void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
165 bool read_from(ModelAction *act, const ModelAction *rf);
166 bool check_action_enabled(ModelAction *curr);
168 Thread * take_step(ModelAction *curr);
170 bool check_recency(ModelAction *curr, const ModelAction *rf) const;
172 template <typename T>
173 bool should_read_instead(const ModelAction *curr, const ModelAction *rf, const T *other_rf) const;
175 ModelAction * get_last_fence_conflict(ModelAction *act) const;
176 ModelAction * get_last_conflict(ModelAction *act) const;
177 void set_backtracking(ModelAction *act);
178 Thread * get_next_thread(ModelAction *curr);
179 bool set_latest_backtrack(ModelAction *act);
180 ModelAction * get_next_backtrack();
181 void reset_to_initial_state();
182 int get_promise_to_resolve(const ModelAction *curr) const;
183 bool resolve_promise(ModelAction *curr, unsigned int promise_idx);
184 void compute_promises(ModelAction *curr);
185 void compute_relseq_breakwrites(ModelAction *curr);
187 void mo_check_promises(const ModelAction *act, bool is_read_check);
188 void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
190 void check_curr_backtracking(ModelAction *curr);
191 void add_action_to_lists(ModelAction *act);
192 ModelAction * get_last_action(thread_id_t tid) const;
193 ModelAction * get_last_fence_release(thread_id_t tid) const;
194 ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
195 ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
196 ModelAction * get_last_unlock(ModelAction *curr) const;
197 void build_may_read_from(ModelAction *curr);
198 ModelAction * process_rmw(ModelAction *curr);
200 template <typename rf_type>
201 bool r_modification_order(ModelAction *curr, const rf_type *rf);
203 bool w_modification_order(ModelAction *curr, std::vector< ModelAction *, ModelAlloc<ModelAction *> > *send_fv);
204 void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
205 bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
206 bool resolve_release_sequences(void *location, work_queue_t *work_queue);
207 void add_future_value(const ModelAction *writer, ModelAction *reader);
209 ModelAction * new_uninitialized_action(void *location) const;
211 ModelAction *diverge;
212 ModelAction *earliest_diverge;
214 ucontext_t system_context;
215 action_list_t * const action_trace;
216 HashTable<int, Thread *, int> * const thread_map;
218 /** Per-object list of actions. Maps an object (i.e., memory location)
219 * to a trace of all actions performed on the object. */
220 HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_map;
222 /** Per-object list of actions. Maps an object (i.e., memory location)
223 * to a trace of all actions performed on the object. */
224 HashTable<const void *, action_list_t *, uintptr_t, 4> * const lock_waiters_map;
226 /** Per-object list of actions. Maps an object (i.e., memory location)
227 * to a trace of all actions performed on the object. */
228 HashTable<const void *, action_list_t *, uintptr_t, 4> * const condvar_waiters_map;
230 HashTable<void *, std::vector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
231 std::vector< Promise *, SnapshotAlloc<Promise *> > * const promises;
232 std::vector< struct PendingFutureValue, SnapshotAlloc<struct PendingFutureValue> > * const futurevalues;
235 * List of pending release sequences. Release sequences might be
236 * determined lazily as promises are fulfilled and modification orders
237 * are established. Each entry in the list may only be partially
238 * filled, depending on its pending status.
240 std::vector< struct release_seq *, SnapshotAlloc<struct release_seq *> > * const pending_rel_seqs;
242 std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > * const thrd_last_action;
243 std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > * const thrd_last_fence_release;
244 NodeStack * const node_stack;
246 /** Private data members that should be snapshotted. They are grouped
247 * together for efficiency and maintainability. */
248 struct model_snapshot_members * const priv;
250 /** A special model-checker Thread; used for associating with
251 * model-checker-related ModelAcitons */
252 Thread *model_thread;
255 * @brief The modification order graph
257 * A directed acyclic graph recording observations of the modification
258 * order on all the atomic objects in the system. This graph should
259 * never contain any cycles, as that represents a violation of the
260 * memory model (total ordering). This graph really consists of many
261 * disjoint (unconnected) subgraphs, each graph corresponding to a
262 * separate ordering on a distinct object.
264 * The edges in this graph represent the "ordered before" relation,
265 * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
268 CycleGraph * const mo_graph;
270 /** @brief The cumulative execution stats */
271 struct execution_stats stats;
274 void print_infeasibility(const char *prefix) const;
275 bool is_feasible_prefix_ignore_relseq() const;
276 bool is_infeasible() const;
277 bool is_deadlocked() const;
278 bool is_complete_execution() const;
279 bool have_bug_reports() const;
280 void print_bugs() const;
281 void print_execution(bool printbugs) const;
282 void print_stats() const;
284 friend void user_main_wrapper();
287 extern ModelChecker *model;
289 #endif /* __MODEL_H__ */