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