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;
117 bool is_enabled(Thread *t) const;
118 bool is_enabled(thread_id_t tid) const;
120 thread_id_t get_next_id();
121 unsigned int get_num_threads() const;
122 Thread * get_current_thread() const;
124 void switch_from_master(Thread *thread);
125 uint64_t switch_to_master(ModelAction *act);
126 ClockVector * get_cv(thread_id_t tid) const;
127 ModelAction * get_parent_action(thread_id_t tid) const;
128 void check_promises_thread_disabled();
129 void check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv);
130 bool isfeasibleprefix() const;
132 bool assert_bug(const char *msg);
133 void assert_user_bug(const char *msg);
135 const model_params params;
136 Node * get_curr_node() const;
140 /** The scheduler to use: tracks the running/ready Threads */
141 Scheduler * const scheduler;
143 bool sleep_can_read_from(ModelAction *curr, const ModelAction *write);
144 bool thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader);
145 bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
146 bool has_asserted() const;
148 void set_bad_synchronization();
149 bool promises_expired() const;
150 void execute_sleep_set();
151 bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
152 void wake_up_sleeping_actions(ModelAction *curr);
153 modelclock_t get_next_seq_num();
155 bool next_execution();
156 ModelAction * check_current_action(ModelAction *curr);
157 bool initialize_curr_action(ModelAction **curr);
158 bool process_read(ModelAction *curr);
159 bool process_write(ModelAction *curr);
160 bool process_fence(ModelAction *curr);
161 bool process_mutex(ModelAction *curr);
162 bool process_thread_action(ModelAction *curr);
163 void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
164 bool read_from(ModelAction *act, const ModelAction *rf);
165 bool check_action_enabled(ModelAction *curr);
167 Thread * take_step(ModelAction *curr);
169 void check_recency(ModelAction *curr, const ModelAction *rf);
170 ModelAction * get_last_fence_conflict(ModelAction *act) const;
171 ModelAction * get_last_conflict(ModelAction *act) const;
172 void set_backtracking(ModelAction *act);
173 Thread * get_next_thread(ModelAction *curr);
174 bool set_latest_backtrack(ModelAction *act);
175 ModelAction * get_next_backtrack();
176 void reset_to_initial_state();
177 bool resolve_promises(ModelAction *curr);
178 void compute_promises(ModelAction *curr);
179 void compute_relseq_breakwrites(ModelAction *curr);
181 void mo_check_promises(const ModelAction *act, bool is_read_check);
182 void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
184 void check_curr_backtracking(ModelAction *curr);
185 void add_action_to_lists(ModelAction *act);
186 ModelAction * get_last_action(thread_id_t tid) const;
187 ModelAction * get_last_fence_release(thread_id_t tid) const;
188 ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
189 ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
190 ModelAction * get_last_unlock(ModelAction *curr) const;
191 void build_may_read_from(ModelAction *curr);
192 ModelAction * process_rmw(ModelAction *curr);
194 template <typename rf_type>
195 bool r_modification_order(ModelAction *curr, const rf_type *rf);
197 bool w_modification_order(ModelAction *curr);
198 void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
199 bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
200 bool resolve_release_sequences(void *location, work_queue_t *work_queue);
201 void add_future_value(const ModelAction *writer, ModelAction *reader);
203 ModelAction * new_uninitialized_action(void *location) const;
205 ModelAction *diverge;
206 ModelAction *earliest_diverge;
208 ucontext_t system_context;
209 action_list_t * const action_trace;
210 HashTable<int, Thread *, int> * const thread_map;
212 /** Per-object list of actions. Maps an object (i.e., memory location)
213 * to a trace of all actions performed on the object. */
214 HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_map;
216 /** Per-object list of actions. Maps an object (i.e., memory location)
217 * to a trace of all actions performed on the object. */
218 HashTable<const void *, action_list_t *, uintptr_t, 4> * const lock_waiters_map;
220 /** Per-object list of actions. Maps an object (i.e., memory location)
221 * to a trace of all actions performed on the object. */
222 HashTable<const void *, action_list_t *, uintptr_t, 4> * const condvar_waiters_map;
224 HashTable<void *, std::vector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
225 std::vector< Promise *, SnapshotAlloc<Promise *> > * const promises;
226 std::vector< struct PendingFutureValue, SnapshotAlloc<struct PendingFutureValue> > * const futurevalues;
229 * List of pending release sequences. Release sequences might be
230 * determined lazily as promises are fulfilled and modification orders
231 * are established. Each entry in the list may only be partially
232 * filled, depending on its pending status.
234 std::vector< struct release_seq *, SnapshotAlloc<struct release_seq *> > * const pending_rel_seqs;
236 std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > * const thrd_last_action;
237 std::vector< ModelAction *, SnapshotAlloc<ModelAction *> > * const thrd_last_fence_release;
238 NodeStack * const node_stack;
240 /** Private data members that should be snapshotted. They are grouped
241 * together for efficiency and maintainability. */
242 struct model_snapshot_members * const priv;
244 /** A special model-checker Thread; used for associating with
245 * model-checker-related ModelAcitons */
246 Thread *model_thread;
249 * @brief The modification order graph
251 * A directed acyclic graph recording observations of the modification
252 * order on all the atomic objects in the system. This graph should
253 * never contain any cycles, as that represents a violation of the
254 * memory model (total ordering). This graph really consists of many
255 * disjoint (unconnected) subgraphs, each graph corresponding to a
256 * separate ordering on a distinct object.
258 * The edges in this graph represent the "ordered before" relation,
259 * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
262 CycleGraph * const mo_graph;
264 /** @brief The cumulative execution stats */
265 struct execution_stats stats;
268 void print_infeasibility(const char *prefix) const;
269 bool is_feasible_prefix_ignore_relseq() const;
270 bool is_infeasible() const;
271 bool is_deadlocked() const;
272 bool is_complete_execution() const;
273 bool have_bug_reports() const;
274 void print_bugs() const;
275 void print_execution(bool printbugs) const;
276 void print_stats() const;
278 friend void user_main_wrapper();
281 extern ModelChecker *model;
283 #endif /* __MODEL_H__ */