2 * @brief Model-checker core
5 #ifndef __EXECUTION_H__
6 #define __EXECUTION_H__
12 #include "hashtable.h"
14 #include "modeltypes.h"
15 #include "stl-model.h"
17 #include "mypthread.h"
19 #include <condition_variable>
20 #include "classlist.h"
22 typedef SnapList<ModelAction *> action_list_t;
24 struct PendingFutureValue {
25 PendingFutureValue(ModelAction *writer, ModelAction *reader) :
26 writer(writer), reader(reader)
28 const ModelAction *writer;
32 /** @brief The central structure for model-checking */
33 class ModelExecution {
35 ModelExecution(ModelChecker *m, Scheduler *scheduler);
38 struct model_params * get_params() const { return params; }
39 void setParams(struct model_params * _params) {params = _params;}
41 Thread * take_step(ModelAction *curr);
44 #if SUPPORT_MOD_ORDER_DUMP
45 void dumpGraph(char *filename);
48 void add_thread(Thread *t);
49 Thread * get_thread(thread_id_t tid) const;
50 Thread * get_thread(const ModelAction *act) const;
52 uint32_t get_pthread_counter() { return pthread_counter; }
53 void incr_pthread_counter() { pthread_counter++; }
54 Thread * get_pthread(pthread_t pid);
56 bool is_enabled(Thread *t) const;
57 bool is_enabled(thread_id_t tid) const;
59 thread_id_t get_next_id();
60 unsigned int get_num_threads() const;
62 ClockVector * get_cv(thread_id_t tid) const;
63 ModelAction * get_parent_action(thread_id_t tid) const;
65 ModelAction * get_last_action(thread_id_t tid) const;
67 bool check_action_enabled(ModelAction *curr);
69 void assert_bug(const char *msg);
71 bool have_bug_reports() const;
73 SnapVector<bug_message *> * get_bugs() const;
75 bool has_asserted() const;
77 bool is_complete_execution() const;
79 bool is_deadlocked() const;
81 action_list_t * get_action_trace() { return &action_trace; }
83 CycleGraph * const get_mo_graph() { return mo_graph; }
84 HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> * getCondMap() {return &cond_map;}
85 HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> * getMutexMap() {return &mutex_map;}
86 ModelAction * check_current_action(ModelAction *curr);
88 SnapVector<func_id_list_t> * get_thrd_func_list() { return &thrd_func_list; }
89 SnapVector<uint32_t> * get_thrd_last_entered_func() { return &thrd_last_entered_func; }
90 SnapVector< SnapList<action_list_t *> *> * get_thrd_func_act_lists() { return &thrd_func_act_lists; }
91 bool isFinished() {return isfinished;}
92 void setFinished() {isfinished = true;}
94 void restore_last_seq_num();
96 pthread_key_t getPthreadKey() {return pthreadkey;}
101 pthread_key_t pthreadkey;
103 int get_execution_number() const;
107 struct model_params * params;
109 /** The scheduler to use: tracks the running/ready Threads */
110 Scheduler * const scheduler;
112 bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
113 bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
114 void wake_up_sleeping_actions(ModelAction *curr);
115 modelclock_t get_next_seq_num();
117 bool next_execution();
118 bool initialize_curr_action(ModelAction **curr);
119 bool process_read(ModelAction *curr, SnapVector<ModelAction *> * rf_set);
120 void process_write(ModelAction *curr);
121 bool process_fence(ModelAction *curr);
122 bool process_mutex(ModelAction *curr);
124 void process_thread_action(ModelAction *curr);
125 void read_from(ModelAction *act, ModelAction *rf);
126 bool synchronize(const ModelAction *first, ModelAction *second);
128 void add_uninit_action_to_lists(ModelAction *act);
129 void add_action_to_lists(ModelAction *act);
130 void add_normal_write_to_lists(ModelAction *act);
131 void add_write_to_lists(ModelAction *act);
132 ModelAction * get_last_fence_release(thread_id_t tid) const;
133 ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
134 ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
135 ModelAction * get_last_unlock(ModelAction *curr) const;
136 SnapVector<ModelAction *> * build_may_read_from(ModelAction *curr);
137 ModelAction * process_rmw(ModelAction *curr);
139 bool r_modification_order(ModelAction *curr, const ModelAction *rf, SnapVector<const ModelAction *> *priorset, bool *canprune, bool check_only = false);
140 void w_modification_order(ModelAction *curr);
141 ClockVector * get_hb_from_write(ModelAction *rf) const;
142 ModelAction * get_uninitialized_action(ModelAction *curr) const;
143 ModelAction * convertNonAtomicStore(void*);
145 action_list_t action_trace;
146 SnapVector<Thread *> thread_map;
147 SnapVector<Thread *> pthread_map;
148 uint32_t pthread_counter;
150 /** Per-object list of actions. Maps an object (i.e., memory location)
151 * to a trace of all actions performed on the object. */
152 HashTable<const void *, action_list_t *, uintptr_t, 2> obj_map;
154 /** Per-object list of actions. Maps an object (i.e., memory location)
155 * to a trace of all actions performed on the object. */
156 HashTable<const void *, action_list_t *, uintptr_t, 2> condvar_waiters_map;
158 HashTable<const void *, SnapVector<action_list_t> *, uintptr_t, 2> obj_thrd_map;
160 HashTable<const void *, SnapVector<action_list_t> *, uintptr_t, 2> obj_wr_thrd_map;
162 HashTable<const void *, ModelAction *, uintptr_t, 4> obj_last_sc_map;
165 HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> mutex_map;
166 HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> cond_map;
169 * List of pending release sequences. Release sequences might be
170 * determined lazily as promises are fulfilled and modification orders
171 * are established. Each entry in the list may only be partially
172 * filled, depending on its pending status.
175 SnapVector<ModelAction *> thrd_last_action;
176 SnapVector<ModelAction *> thrd_last_fence_release;
178 /** A special model-checker Thread; used for associating with
179 * model-checker-related ModelAcitons */
180 Thread *model_thread;
182 /** Private data members that should be snapshotted. They are grouped
183 * together for efficiency and maintainability. */
184 struct model_snapshot_members * const priv;
187 * @brief The modification order graph
189 * A directed acyclic graph recording observations of the modification
190 * order on all the atomic objects in the system. This graph should
191 * never contain any cycles, as that represents a violation of the
192 * memory model (total ordering). This graph really consists of many
193 * disjoint (unconnected) subgraphs, each graph corresponding to a
194 * separate ordering on a distinct object.
196 * The edges in this graph represent the "ordered before" relation,
197 * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
200 CycleGraph * const mo_graph;
204 Thread * action_select_next_thread(const ModelAction *curr) const;
205 bool paused_by_fuzzer(const ModelAction * act) const;
207 /* thrd_func_list stores a list of function ids for each thread.
208 * Each element in thrd_func_list stores the functions that
209 * thread i has entered and yet to exit from
211 * This data structure is handled by ModelHistory
213 SnapVector<func_id_list_t> thrd_func_list;
214 SnapVector<uint32_t> thrd_last_entered_func;
216 /* Keeps track of atomic actions that thread i has performed in some
217 * function. Index of SnapVector is thread id. SnapList simulates
220 SnapVector< SnapList<action_list_t *> *> thrd_func_act_lists;
224 #endif /* __EXECUTION_H__ */