2 * @brief Core model checker.
12 #include "hashtable.h"
13 #include "workqueue.h"
15 #include "modeltypes.h"
16 #include "stl-model.h"
20 /* Forward declaration */
29 struct model_snapshot_members;
31 /** @brief Shorthand for a list of release sequence heads */
32 typedef ModelVector<const ModelAction *> rel_heads_list_t;
33 typedef SnapList<ModelAction *> action_list_t;
35 /** @brief Model checker execution stats */
36 struct execution_stats {
37 int num_total; /**< @brief Total number of executions */
38 int num_infeasible; /**< @brief Number of infeasible executions */
39 int num_buggy_executions; /** @brief Number of buggy executions */
40 int num_complete; /**< @brief Number of feasible, non-buggy, complete executions */
41 int num_redundant; /**< @brief Number of redundant, aborted executions */
44 struct PendingFutureValue {
45 PendingFutureValue(ModelAction *writer, ModelAction *reader) :
46 writer(writer), reader(reader)
48 const ModelAction *writer;
52 /** @brief Records information regarding a single pending release sequence */
54 /** @brief The acquire operation */
56 /** @brief The read operation that may read from a release sequence;
57 * may be the same as acquire, or else an earlier action in the same
58 * thread (i.e., when 'acquire' is a fence-acquire) */
59 const ModelAction *read;
60 /** @brief The head of the RMW chain from which 'read' reads; may be
61 * equal to 'release' */
62 const ModelAction *rf;
63 /** @brief The head of the potential longest release sequence chain */
64 const ModelAction *release;
65 /** @brief The write(s) that may break the release sequence */
66 SnapVector<const ModelAction *> writes;
69 /** @brief The central structure for model-checking */
72 ModelChecker(struct model_params params);
77 /** @returns the context for the main model-checking system thread */
78 ucontext_t * get_system_context() { return &system_context; }
80 void print_summary() const;
81 #if SUPPORT_MOD_ORDER_DUMP
82 void dumpGraph(char *filename) const;
85 Thread * get_thread(thread_id_t tid) const;
86 Thread * get_thread(const ModelAction *act) const;
87 int get_promise_number(const Promise *promise) const;
89 bool is_enabled(Thread *t) const;
90 bool is_enabled(thread_id_t tid) const;
92 thread_id_t get_next_id();
93 unsigned int get_num_threads() const;
94 Thread * get_current_thread() const;
96 void switch_from_master(Thread *thread);
97 uint64_t switch_to_master(ModelAction *act);
98 ClockVector * get_cv(thread_id_t tid) const;
99 ModelAction * get_parent_action(thread_id_t tid) const;
100 void check_promises_thread_disabled();
101 void check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv);
102 bool isfeasibleprefix() const;
104 bool assert_bug(const char *msg, ...);
105 void assert_user_bug(const char *msg);
107 const model_params params;
108 Node * get_curr_node() const;
109 void add_trace_analysis(TraceAnalysis *a) {
110 trace_analyses->push_back(a);
113 action_list_t * get_actions_on_obj(void * obj, thread_id_t tid);
114 ModelAction * get_last_action(thread_id_t tid) const;
118 /** The scheduler to use: tracks the running/ready Threads */
119 Scheduler * const scheduler;
121 void add_thread(Thread *t);
123 bool sleep_can_read_from(ModelAction *curr, const ModelAction *write);
124 bool thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader) const;
125 bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
126 bool promises_may_allow(const ModelAction *writer, const ModelAction *reader) const;
127 bool has_asserted() const;
129 void set_bad_synchronization();
130 bool promises_expired() const;
131 void execute_sleep_set();
132 bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
133 void wake_up_sleeping_actions(ModelAction *curr);
134 modelclock_t get_next_seq_num();
136 bool next_execution();
137 ModelAction * check_current_action(ModelAction *curr);
138 bool initialize_curr_action(ModelAction **curr);
139 bool process_read(ModelAction *curr);
140 bool process_write(ModelAction *curr);
141 bool process_fence(ModelAction *curr);
142 bool process_mutex(ModelAction *curr);
143 bool process_thread_action(ModelAction *curr);
144 void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
145 bool read_from(ModelAction *act, const ModelAction *rf);
146 bool synchronize(const ModelAction *first, ModelAction *second);
147 bool check_action_enabled(ModelAction *curr);
149 Thread * take_step(ModelAction *curr);
150 bool should_terminate_execution();
152 template <typename T>
153 bool check_recency(ModelAction *curr, const T *rf) const;
155 template <typename T, typename U>
156 bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
158 ModelAction * get_last_fence_conflict(ModelAction *act) const;
159 ModelAction * get_last_conflict(ModelAction *act) const;
160 void set_backtracking(ModelAction *act);
161 Thread * action_select_next_thread(const ModelAction *curr) const;
162 Thread * get_next_thread();
163 bool set_latest_backtrack(ModelAction *act);
164 ModelAction * get_next_backtrack();
165 void reset_to_initial_state();
166 Promise * pop_promise_to_resolve(const ModelAction *curr);
167 bool resolve_promise(ModelAction *curr, Promise *promise);
168 void compute_promises(ModelAction *curr);
169 void compute_relseq_breakwrites(ModelAction *curr);
171 void mo_check_promises(const ModelAction *act, bool is_read_check);
172 void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
174 void check_curr_backtracking(ModelAction *curr);
175 void add_action_to_lists(ModelAction *act);
176 ModelAction * get_last_fence_release(thread_id_t tid) const;
177 ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
178 ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
179 ModelAction * get_last_unlock(ModelAction *curr) const;
180 void build_may_read_from(ModelAction *curr);
181 ModelAction * process_rmw(ModelAction *curr);
183 template <typename rf_type>
184 bool r_modification_order(ModelAction *curr, const rf_type *rf);
186 bool w_modification_order(ModelAction *curr, ModelVector<ModelAction *> *send_fv);
187 void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
188 bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
189 bool resolve_release_sequences(void *location, work_queue_t *work_queue);
190 void add_future_value(const ModelAction *writer, ModelAction *reader);
192 ModelAction * get_uninitialized_action(const ModelAction *curr) const;
194 ModelAction *diverge;
195 ModelAction *earliest_diverge;
197 ucontext_t system_context;
198 action_list_t * const action_trace;
199 HashTable<int, Thread *, int> * const thread_map;
201 /** Per-object list of actions. Maps an object (i.e., memory location)
202 * to a trace of all actions performed on the object. */
203 HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_map;
205 /** Per-object list of actions. Maps an object (i.e., memory location)
206 * to a trace of all actions performed on the object. */
207 HashTable<const void *, action_list_t *, uintptr_t, 4> * const condvar_waiters_map;
209 HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
210 SnapVector<Promise *> * const promises;
211 SnapVector<struct PendingFutureValue> * const futurevalues;
214 * List of pending release sequences. Release sequences might be
215 * determined lazily as promises are fulfilled and modification orders
216 * are established. Each entry in the list may only be partially
217 * filled, depending on its pending status.
219 SnapVector<struct release_seq *> * const pending_rel_seqs;
221 SnapVector<ModelAction *> * const thrd_last_action;
222 SnapVector<ModelAction *> * const thrd_last_fence_release;
223 NodeStack * const node_stack;
224 ModelVector<TraceAnalysis *> * trace_analyses;
227 /** Private data members that should be snapshotted. They are grouped
228 * together for efficiency and maintainability. */
229 struct model_snapshot_members * const priv;
231 /** A special model-checker Thread; used for associating with
232 * model-checker-related ModelAcitons */
233 Thread *model_thread;
236 * @brief The modification order graph
238 * A directed acyclic graph recording observations of the modification
239 * order on all the atomic objects in the system. This graph should
240 * never contain any cycles, as that represents a violation of the
241 * memory model (total ordering). This graph really consists of many
242 * disjoint (unconnected) subgraphs, each graph corresponding to a
243 * separate ordering on a distinct object.
245 * The edges in this graph represent the "ordered before" relation,
246 * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
249 CycleGraph * const mo_graph;
251 /** @brief The cumulative execution stats */
252 struct execution_stats stats;
254 void run_trace_analyses();
255 void print_infeasibility(const char *prefix) const;
256 bool is_feasible_prefix_ignore_relseq() const;
257 bool is_infeasible() const;
258 bool is_deadlocked() const;
259 bool too_many_steps() const;
260 bool is_complete_execution() const;
261 bool have_bug_reports() const;
262 void print_bugs() const;
263 void print_execution(bool printbugs) const;
264 void print_stats() const;
266 friend void user_main_wrapper();
269 extern ModelChecker *model;
271 #endif /* __MODEL_H__ */