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 template <typename T>
171 bool check_recency(ModelAction *curr, const T *rf) const;
173 template <typename T, typename U>
174 bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
176 ModelAction * get_last_fence_conflict(ModelAction *act) const;
177 ModelAction * get_last_conflict(ModelAction *act) const;
178 void set_backtracking(ModelAction *act);
179 Thread * get_next_thread(ModelAction *curr);
180 bool set_latest_backtrack(ModelAction *act);
181 ModelAction * get_next_backtrack();
182 void reset_to_initial_state();
183 int get_promise_to_resolve(const ModelAction *curr) const;
184 bool resolve_promise(ModelAction *curr, unsigned int promise_idx);
185 void compute_promises(ModelAction *curr);
186 void compute_relseq_breakwrites(ModelAction *curr);
188 void mo_check_promises(const ModelAction *act, bool is_read_check);
189 void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
191 void check_curr_backtracking(ModelAction *curr);
192 void add_action_to_lists(ModelAction *act);
193 ModelAction * get_last_action(thread_id_t tid) const;
194 ModelAction * get_last_fence_release(thread_id_t tid) const;
195 ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
196 ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
197 ModelAction * get_last_unlock(ModelAction *curr) const;
198 void build_may_read_from(ModelAction *curr);
199 ModelAction * process_rmw(ModelAction *curr);
201 template <typename rf_type>
202 bool r_modification_order(ModelAction *curr, const rf_type *rf);
204 bool w_modification_order(ModelAction *curr, std::vector< ModelAction *, ModelAlloc<ModelAction *> > *send_fv);
205 void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
206 bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
207 bool resolve_release_sequences(void *location, work_queue_t *work_queue);
208 void add_future_value(const ModelAction *writer, ModelAction *reader);
210 ModelAction * new_uninitialized_action(void *location) const;
212 ModelAction *diverge;
213 ModelAction *earliest_diverge;
215 ucontext_t system_context;
216 action_list_t * const action_trace;
217 HashTable<int, Thread *, int> * const thread_map;
219 /** Per-object list of actions. Maps an object (i.e., memory location)
220 * to a trace of all actions performed on the object. */
221 HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_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 lock_waiters_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 condvar_waiters_map;
231 HashTable<void *, std::vector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
232 std::vector< Promise *, SnapshotAlloc<Promise *> > * const promises;
233 std::vector< struct PendingFutureValue, SnapshotAlloc<struct PendingFutureValue> > * const futurevalues;
236 * List of pending release sequences. Release sequences might be
237 * determined lazily as promises are fulfilled and modification orders
238 * are established. Each entry in the list may only be partially
239 * filled, depending on its pending status.
241 std::vector< struct release_seq *, SnapshotAlloc<struct release_seq *> > * const pending_rel_seqs;
243 std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > * const thrd_last_action;
244 std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > * const thrd_last_fence_release;
245 NodeStack * const node_stack;
247 /** Private data members that should be snapshotted. They are grouped
248 * together for efficiency and maintainability. */
249 struct model_snapshot_members * const priv;
251 /** A special model-checker Thread; used for associating with
252 * model-checker-related ModelAcitons */
253 Thread *model_thread;
256 * @brief The modification order graph
258 * A directed acyclic graph recording observations of the modification
259 * order on all the atomic objects in the system. This graph should
260 * never contain any cycles, as that represents a violation of the
261 * memory model (total ordering). This graph really consists of many
262 * disjoint (unconnected) subgraphs, each graph corresponding to a
263 * separate ordering on a distinct object.
265 * The edges in this graph represent the "ordered before" relation,
266 * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
269 CycleGraph * const mo_graph;
271 /** @brief The cumulative execution stats */
272 struct execution_stats stats;
275 void print_infeasibility(const char *prefix) const;
276 bool is_feasible_prefix_ignore_relseq() const;
277 bool is_infeasible() const;
278 bool is_deadlocked() const;
279 bool check_deadlock(const Thread *t) const;
280 bool is_complete_execution() const;
281 bool have_bug_reports() const;
282 void print_bugs() const;
283 void print_execution(bool printbugs) const;
284 void print_stats() const;
286 friend void user_main_wrapper();
289 extern ModelChecker *model;
291 #endif /* __MODEL_H__ */