When an atomic read action cannot read from a desired write, make this thread sleep...
[c11tester.git] / execution.h
1 /** @file execution.h
2  *  @brief Model-checker core
3  */
4
5 #ifndef __EXECUTION_H__
6 #define __EXECUTION_H__
7
8 #include <cstddef>
9 #include <inttypes.h>
10
11 #include "mymemory.h"
12 #include "hashtable.h"
13 #include "config.h"
14 #include "modeltypes.h"
15 #include "stl-model.h"
16 #include "params.h"
17 #include "mypthread.h"
18 #include "mutex.h"
19 #include <condition_variable>
20 #include "classlist.h"
21
22 typedef SnapList<ModelAction *> action_list_t;
23
24 struct PendingFutureValue {
25         PendingFutureValue(ModelAction *writer, ModelAction *reader) :
26                 writer(writer), reader(reader)
27         { }
28         const ModelAction *writer;
29         ModelAction *reader;
30 };
31
32 /** @brief The central structure for model-checking */
33 class ModelExecution {
34 public:
35         ModelExecution(ModelChecker *m, Scheduler *scheduler);
36         ~ModelExecution();
37
38         struct model_params * get_params() const { return params; }
39         void setParams(struct model_params * _params) {params = _params;}
40
41         Thread * take_step(ModelAction *curr);
42
43         void print_summary();
44 #if SUPPORT_MOD_ORDER_DUMP
45         void dumpGraph(char *filename);
46 #endif
47
48         void add_thread(Thread *t);
49         Thread * get_thread(thread_id_t tid) const;
50         Thread * get_thread(const ModelAction *act) const;
51
52         uint32_t get_pthread_counter() { return pthread_counter; }
53         void incr_pthread_counter() { pthread_counter++; }
54         Thread * get_pthread(pthread_t pid);
55
56         bool is_enabled(Thread *t) const;
57         bool is_enabled(thread_id_t tid) const;
58
59         thread_id_t get_next_id();
60         unsigned int get_num_threads() const;
61
62         ClockVector * get_cv(thread_id_t tid) const;
63         ModelAction * get_parent_action(thread_id_t tid) const;
64         bool isfeasibleprefix() const;
65
66         ModelAction * get_last_action(thread_id_t tid) const;
67
68         bool check_action_enabled(ModelAction *curr);
69
70         bool assert_bug(const char *msg);
71
72         bool have_bug_reports() const;
73
74         SnapVector<bug_message *> * get_bugs() const;
75
76         bool has_asserted() const;
77         void set_assert();
78         bool is_complete_execution() const;
79
80         void print_infeasibility(const char *prefix) const;
81         bool is_infeasible() const;
82         bool is_deadlocked() const;
83
84         action_list_t * get_action_trace() { return &action_trace; }
85         Fuzzer * getFuzzer();
86         CycleGraph * const get_mo_graph() { return mo_graph; }
87         HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> * getCondMap() {return &cond_map;}
88         HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> * getMutexMap() {return &mutex_map;}
89         ModelAction * check_current_action(ModelAction *curr);
90
91         SnapVector<func_id_list_t> * get_thrd_func_list() { return &thrd_func_list; }
92         SnapVector< SnapList<action_list_t *> *> * get_thrd_func_act_lists() { return &thrd_func_act_lists; }
93         bool isFinished() {return isfinished;}
94         void setFinished() {isfinished = true;}
95
96         void restore_last_seq_num();
97
98         SNAPSHOTALLOC
99 private:
100         int get_execution_number() const;
101
102         ModelChecker *model;
103
104         struct model_params * params;
105
106         /** The scheduler to use: tracks the running/ready Threads */
107         Scheduler * const scheduler;
108
109         bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
110         void set_bad_synchronization();
111         bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
112         void wake_up_sleeping_actions(ModelAction *curr);
113         modelclock_t get_next_seq_num();
114
115         bool next_execution();
116         bool initialize_curr_action(ModelAction **curr);
117         bool process_read(ModelAction *curr, SnapVector<ModelAction *> * rf_set);
118         void process_write(ModelAction *curr);
119         bool process_fence(ModelAction *curr);
120         bool process_mutex(ModelAction *curr);
121
122         void process_thread_action(ModelAction *curr);
123         void read_from(ModelAction *act, ModelAction *rf);
124         bool synchronize(const ModelAction *first, ModelAction *second);
125
126         void add_uninit_action_to_lists(ModelAction *act);
127         void add_action_to_lists(ModelAction *act);
128         void add_normal_write_to_lists(ModelAction *act);
129         void add_write_to_lists(ModelAction *act);
130         ModelAction * get_last_fence_release(thread_id_t tid) const;
131         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
132         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
133         ModelAction * get_last_unlock(ModelAction *curr) const;
134         SnapVector<ModelAction *> * build_may_read_from(ModelAction *curr);
135         ModelAction * process_rmw(ModelAction *curr);
136
137         bool r_modification_order(ModelAction *curr, const ModelAction *rf, SnapVector<const ModelAction *> *priorset, bool *canprune);
138         void w_modification_order(ModelAction *curr);
139         ClockVector * get_hb_from_write(ModelAction *rf) const;
140         ModelAction * get_uninitialized_action(ModelAction *curr) const;
141         ModelAction * convertNonAtomicStore(void*);
142
143         action_list_t action_trace;
144         SnapVector<Thread *> thread_map;
145         SnapVector<Thread *> pthread_map;
146         uint32_t pthread_counter;
147
148         /** Per-object list of actions. Maps an object (i.e., memory location)
149          * to a trace of all actions performed on the object. */
150         HashTable<const void *, action_list_t *, uintptr_t, 4> obj_map;
151
152         /** Per-object list of actions. Maps an object (i.e., memory location)
153          * to a trace of all actions performed on the object. */
154         HashTable<const void *, action_list_t *, uintptr_t, 4> condvar_waiters_map;
155
156         HashTable<const void *, SnapVector<action_list_t> *, uintptr_t, 4> obj_thrd_map;
157
158         HashTable<const void *, SnapVector<action_list_t> *, uintptr_t, 4> obj_wr_thrd_map;
159
160         HashTable<const void *, ModelAction *, uintptr_t, 4> obj_last_sc_map;
161
162
163         HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> mutex_map;
164         HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> cond_map;
165
166         /**
167          * List of pending release sequences. Release sequences might be
168          * determined lazily as promises are fulfilled and modification orders
169          * are established. Each entry in the list may only be partially
170          * filled, depending on its pending status.
171          */
172
173         SnapVector<ModelAction *> thrd_last_action;
174         SnapVector<ModelAction *> thrd_last_fence_release;
175
176         /** A special model-checker Thread; used for associating with
177          *  model-checker-related ModelAcitons */
178         Thread *model_thread;
179
180         /** Private data members that should be snapshotted. They are grouped
181          * together for efficiency and maintainability. */
182         struct model_snapshot_members * const priv;
183
184         /**
185          * @brief The modification order graph
186          *
187          * A directed acyclic graph recording observations of the modification
188          * order on all the atomic objects in the system. This graph should
189          * never contain any cycles, as that represents a violation of the
190          * memory model (total ordering). This graph really consists of many
191          * disjoint (unconnected) subgraphs, each graph corresponding to a
192          * separate ordering on a distinct object.
193          *
194          * The edges in this graph represent the "ordered before" relation,
195          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
196          * <tt>b</tt>.
197          */
198         CycleGraph * const mo_graph;
199
200         Fuzzer * fuzzer;
201
202         Thread * action_select_next_thread(const ModelAction *curr) const;
203
204         /* thrd_func_list stores a list of function ids for each thread.
205          * Each element in thrd_func_list stores the functions that
206          * thread i has entered and yet to exit from
207          *
208          * This data structure is handled by ModelHistory
209          */
210         SnapVector<func_id_list_t> thrd_func_list;
211
212         /* Keeps track of atomic actions that thread i has performed in some
213          * function. Index of SnapVector is thread id. SnapList simulates
214          * the call stack.
215          */
216         SnapVector< SnapList<action_list_t *> *> thrd_func_act_lists;
217         bool isfinished;
218 };
219
220 #endif  /* __EXECUTION_H__ */