b8bfb03580c324309c2af068d6239fed005f5eba
[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 #define INITIAL_THREAD_ID       0
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 #ifdef COLLECT_STAT
33 void print_atomic_accesses();
34 #endif
35
36 /** @brief The central structure for model-checking */
37 class ModelExecution {
38 public:
39         ModelExecution(ModelChecker *m, Scheduler *scheduler);
40         ~ModelExecution();
41
42         struct model_params * get_params() const { return params; }
43         void setParams(struct model_params * _params) {params = _params;}
44
45         Thread * take_step(ModelAction *curr);
46
47         void print_summary();
48         void print_tail();
49 #if SUPPORT_MOD_ORDER_DUMP
50         void dumpGraph(char *filename);
51 #endif
52
53         void add_thread(Thread *t);
54         Thread * get_thread(thread_id_t tid) const;
55         Thread * get_thread(const ModelAction *act) const;
56
57         uint32_t get_pthread_counter() { return pthread_counter; }
58         void incr_pthread_counter() { pthread_counter++; }
59         Thread * get_pthread(pthread_t pid);
60
61         bool is_enabled(Thread *t) const;
62         bool is_enabled(thread_id_t tid) const;
63
64         thread_id_t get_next_id();
65         unsigned int get_num_threads() const;
66
67         ClockVector * get_cv(thread_id_t tid) const;
68         ModelAction * get_parent_action(thread_id_t tid) const;
69
70         ModelAction * get_last_action(thread_id_t tid) const;
71
72         bool check_action_enabled(ModelAction *curr);
73
74         void assert_bug(const char *msg);
75
76         bool have_bug_reports() const;
77
78         SnapVector<bug_message *> * get_bugs() const;
79
80         bool has_asserted() const;
81         void set_assert();
82         bool is_complete_execution() const;
83
84         bool is_deadlocked() const;
85
86         action_list_t * get_action_trace() { return &action_trace; }
87         Fuzzer * getFuzzer();
88         CycleGraph * const get_mo_graph() { return mo_graph; }
89         HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> * getCondMap() {return &cond_map;}
90         HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> * getMutexMap() {return &mutex_map;}
91         ModelAction * check_current_action(ModelAction *curr);
92
93         bool isFinished() {return isfinished;}
94         void setFinished() {isfinished = true;}
95         void restore_last_seq_num();
96         void collectActions();
97         modelclock_t get_curr_seq_num();
98 #ifdef TLS
99         pthread_key_t getPthreadKey() {return pthreadkey;}
100 #endif
101         SNAPSHOTALLOC
102 private:
103         int get_execution_number() const;
104         bool should_wake_up(const ModelAction * asleep) const;
105         void wake_up_sleeping_actions();
106         modelclock_t get_next_seq_num();
107         bool next_execution();
108         bool initialize_curr_action(ModelAction **curr);
109         bool process_read(ModelAction *curr, SnapVector<ModelAction *> * rf_set);
110         void process_write(ModelAction *curr);
111         void process_fence(ModelAction *curr);
112         bool process_mutex(ModelAction *curr);
113         void process_thread_action(ModelAction *curr);
114         void read_from(ModelAction *act, ModelAction *rf);
115         bool synchronize(const ModelAction *first, ModelAction *second);
116         void add_action_to_lists(ModelAction *act, bool canprune);
117         void add_normal_write_to_lists(ModelAction *act);
118         void add_write_to_lists(ModelAction *act);
119         ModelAction * get_last_fence_release(thread_id_t tid) const;
120         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
121         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
122         ModelAction * get_last_unlock(ModelAction *curr) const;
123         SnapVector<ModelAction *> * build_may_read_from(ModelAction *curr);
124         ModelAction * process_rmw(ModelAction *curr);
125         bool r_modification_order(ModelAction *curr, const ModelAction *rf, SnapVector<ModelAction *> *priorset, bool *canprune);
126         void w_modification_order(ModelAction *curr);
127         ClockVector * get_hb_from_write(ModelAction *rf) const;
128         ModelAction * convertNonAtomicStore(void*);
129         ClockVector * computeMinimalCV();
130         void removeAction(ModelAction *act);
131         void fixupLastAct(ModelAction *act);
132
133 #ifdef TLS
134         pthread_key_t pthreadkey;
135 #endif
136         ModelChecker *model;
137         struct model_params * params;
138
139         /** The scheduler to use: tracks the running/ready Threads */
140         Scheduler * const scheduler;
141
142
143         SnapVector<Thread *> thread_map;
144         SnapVector<Thread *> pthread_map;
145         uint32_t pthread_counter;
146
147         action_list_t action_trace;
148
149
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          * Used only for SC fences, unlocks, & wait.
153          */
154         HashTable<const void *, simple_action_list_t *, uintptr_t, 2> obj_map;
155
156         /** Per-object list of actions. Maps an object (i.e., memory location)
157          * to a trace of all actions performed on the object. */
158         HashTable<const void *, simple_action_list_t *, uintptr_t, 2> condvar_waiters_map;
159
160         /** Per-object list of actions that each thread performed. */
161         HashTable<const void *, SnapVector<action_list_t> *, uintptr_t, 2> obj_thrd_map;
162
163         /** Per-object list of writes that each thread performed. */
164         HashTable<const void *, SnapVector<simple_action_list_t> *, uintptr_t, 2> obj_wr_thrd_map;
165
166         HashTable<const void *, ModelAction *, uintptr_t, 4> obj_last_sc_map;
167
168
169         HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> mutex_map;
170         HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> cond_map;
171
172         /**
173          * List of pending release sequences. Release sequences might be
174          * determined lazily as promises are fulfilled and modification orders
175          * are established. Each entry in the list may only be partially
176          * filled, depending on its pending status.
177          */
178
179         SnapVector<ModelAction *> thrd_last_action;
180         SnapVector<ModelAction *> thrd_last_fence_release;
181
182         /** A special model-checker Thread; used for associating with
183          *  model-checker-related ModelAcitons */
184         Thread *model_thread;
185
186         /** Private data members that should be snapshotted. They are grouped
187          * together for efficiency and maintainability. */
188         struct model_snapshot_members * const priv;
189
190         /**
191          * @brief The modification order graph
192          *
193          * A directed acyclic graph recording observations of the modification
194          * order on all the atomic objects in the system. This graph should
195          * never contain any cycles, as that represents a violation of the
196          * memory model (total ordering). This graph really consists of many
197          * disjoint (unconnected) subgraphs, each graph corresponding to a
198          * separate ordering on a distinct object.
199          *
200          * The edges in this graph represent the "ordered before" relation,
201          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
202          * <tt>b</tt>.
203          */
204         CycleGraph * const mo_graph;
205
206         Fuzzer * fuzzer;
207
208         Thread * action_select_next_thread(const ModelAction *curr) const;
209
210         bool isfinished;
211 };
212
213 #endif  /* __EXECUTION_H__ */