Bug fixes
[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
65         ModelAction * get_last_action(thread_id_t tid) const;
66
67         bool check_action_enabled(ModelAction *curr);
68
69         void assert_bug(const char *msg);
70
71         bool have_bug_reports() const;
72
73         SnapVector<bug_message *> * get_bugs() const;
74
75         bool has_asserted() const;
76         void set_assert();
77         bool is_complete_execution() const;
78
79         bool is_deadlocked() const;
80
81         action_list_t * get_action_trace() { return &action_trace; }
82         Fuzzer * getFuzzer();
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);
87
88         bool isFinished() {return isfinished;}
89         void setFinished() {isfinished = true;}
90         void restore_last_seq_num();
91         void collectActions();
92         modelclock_t get_curr_seq_num();
93 #ifdef TLS
94         pthread_key_t getPthreadKey() {return pthreadkey;}
95 #endif
96         SNAPSHOTALLOC
97 private:
98         int get_execution_number() const;
99         bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
100         bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
101         void wake_up_sleeping_actions(ModelAction *curr);
102         modelclock_t get_next_seq_num();
103         bool next_execution();
104         bool initialize_curr_action(ModelAction **curr);
105         bool process_read(ModelAction *curr, SnapVector<ModelAction *> * rf_set);
106         void process_write(ModelAction *curr);
107         bool process_fence(ModelAction *curr);
108         bool process_mutex(ModelAction *curr);
109         void process_thread_action(ModelAction *curr);
110         void read_from(ModelAction *act, ModelAction *rf);
111         bool synchronize(const ModelAction *first, ModelAction *second);
112         void add_action_to_lists(ModelAction *act, bool canprune);
113         void add_normal_write_to_lists(ModelAction *act);
114         void add_write_to_lists(ModelAction *act);
115         ModelAction * get_last_fence_release(thread_id_t tid) const;
116         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
117         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
118         ModelAction * get_last_unlock(ModelAction *curr) const;
119         SnapVector<ModelAction *> * build_may_read_from(ModelAction *curr);
120         ModelAction * process_rmw(ModelAction *curr);
121         bool r_modification_order(ModelAction *curr, const ModelAction *rf, SnapVector<ModelAction *> *priorset, bool *canprune, bool check_only = false);
122         void w_modification_order(ModelAction *curr);
123         ClockVector * get_hb_from_write(ModelAction *rf) const;
124         ModelAction * convertNonAtomicStore(void*);
125         ClockVector * computeMinimalCV();
126         void removeAction(ModelAction *act);
127         void fixupLastAct(ModelAction *act);
128
129 #ifdef TLS
130         pthread_key_t pthreadkey;
131 #endif
132         ModelChecker *model;
133         struct model_params * params;
134
135         /** The scheduler to use: tracks the running/ready Threads */
136         Scheduler * const scheduler;
137
138
139         SnapVector<Thread *> thread_map;
140         SnapVector<Thread *> pthread_map;
141         uint32_t pthread_counter;
142
143         action_list_t action_trace;
144
145
146         /** Per-object list of actions. Maps an object (i.e., memory location)
147          * to a trace of all actions performed on the object.
148          * Used only for SC fences, unlocks, & wait.
149          */
150         HashTable<const void *, action_list_t *, uintptr_t, 2> 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, 2> condvar_waiters_map;
155
156         /** Per-object list of actions that each thread performed. */
157         HashTable<const void *, SnapVector<action_list_t> *, uintptr_t, 2> obj_thrd_map;
158
159         /** Per-object list of writes that each thread performed. */
160         HashTable<const void *, SnapVector<action_list_t> *, uintptr_t, 2> obj_wr_thrd_map;
161
162         HashTable<const void *, ModelAction *, uintptr_t, 4> obj_last_sc_map;
163
164
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;
167
168         /**
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.
173          */
174
175         SnapVector<ModelAction *> thrd_last_action;
176         SnapVector<ModelAction *> thrd_last_fence_release;
177
178         /** A special model-checker Thread; used for associating with
179          *  model-checker-related ModelAcitons */
180         Thread *model_thread;
181
182         /** Private data members that should be snapshotted. They are grouped
183          * together for efficiency and maintainability. */
184         struct model_snapshot_members * const priv;
185
186         /**
187          * @brief The modification order graph
188          *
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.
195          *
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
198          * <tt>b</tt>.
199          */
200         CycleGraph * const mo_graph;
201
202         Fuzzer * fuzzer;
203
204         Thread * action_select_next_thread(const ModelAction *curr) const;
205         bool paused_by_fuzzer(const ModelAction * act) const;
206
207         bool isfinished;
208 };
209
210 #endif  /* __EXECUTION_H__ */