Add support for converting normal writes into ModelActions after the fact
[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() const;
44 #if SUPPORT_MOD_ORDER_DUMP
45         void dumpGraph(char *filename) const;
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         bool have_bug_reports() const;
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         void print_infeasibility(const char *prefix) const;
79         bool is_infeasible() const;
80         bool is_deadlocked() const;
81
82         action_list_t * get_action_trace() { return &action_trace; }
83         Fuzzer * getFuzzer();
84         CycleGraph * const get_mo_graph() { return mo_graph; }
85         HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> * getCondMap() {return &cond_map;}
86         HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> * getMutexMap() {return &mutex_map;}
87         ModelAction * check_current_action(ModelAction *curr);
88
89         SnapVector<func_id_list_t *> * get_thrd_func_list() { return &thrd_func_list; }
90         SnapVector< SnapList<func_inst_list_t *> *> * get_thrd_func_inst_lists() { return &thrd_func_inst_lists; }
91
92         SNAPSHOTALLOC
93 private:
94         int get_execution_number() const;
95
96         ModelChecker *model;
97
98         struct model_params * params;
99
100         /** The scheduler to use: tracks the running/ready Threads */
101         Scheduler * const scheduler;
102
103         bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
104         void set_bad_synchronization();
105         bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
106         void wake_up_sleeping_actions(ModelAction *curr);
107         modelclock_t get_next_seq_num();
108
109         bool next_execution();
110         bool initialize_curr_action(ModelAction **curr);
111         void process_read(ModelAction *curr, SnapVector<ModelAction *> * rf_set);
112         void process_write(ModelAction *curr);
113         bool process_fence(ModelAction *curr);
114         bool process_mutex(ModelAction *curr);
115
116         bool process_thread_action(ModelAction *curr);
117         void read_from(ModelAction *act, ModelAction *rf);
118         bool synchronize(const ModelAction *first, ModelAction *second);
119
120         void add_action_to_lists(ModelAction *act);
121         void add_normal_write_to_lists(ModelAction *act);
122         void add_write_to_lists(ModelAction *act);
123         ModelAction * get_last_fence_release(thread_id_t tid) const;
124         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
125         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
126         ModelAction * get_last_unlock(ModelAction *curr) const;
127         SnapVector<ModelAction *> * build_may_read_from(ModelAction *curr);
128         ModelAction * process_rmw(ModelAction *curr);
129
130         bool r_modification_order(ModelAction *curr, const ModelAction *rf, SnapVector<const ModelAction *> *priorset, bool *canprune);
131         void w_modification_order(ModelAction *curr);
132         ClockVector * get_hb_from_write(ModelAction *rf) const;
133         ModelAction * get_uninitialized_action(ModelAction *curr) const;
134         ModelAction * convertNonAtomicStore(void*);
135
136         action_list_t action_trace;
137         SnapVector<Thread *> thread_map;
138         SnapVector<Thread *> pthread_map;
139         uint32_t pthread_counter;
140
141         /** Per-object list of actions. Maps an object (i.e., memory location)
142          * to a trace of all actions performed on the object. */
143         HashTable<const void *, action_list_t *, uintptr_t, 4> obj_map;
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         HashTable<const void *, action_list_t *, uintptr_t, 4> condvar_waiters_map;
148
149         HashTable<const void *, SnapVector<action_list_t> *, uintptr_t, 4> obj_thrd_map;
150
151         HashTable<const void *, SnapVector<action_list_t> *, uintptr_t, 4> obj_wr_thrd_map;
152
153         HashTable<const void *, ModelAction *, uintptr_t, 4> obj_last_sc_map;
154
155
156         HashTable<pthread_mutex_t *, cdsc::snapmutex *, uintptr_t, 4> mutex_map;
157         HashTable<pthread_cond_t *, cdsc::snapcondition_variable *, uintptr_t, 4> cond_map;
158
159         /**
160          * List of pending release sequences. Release sequences might be
161          * determined lazily as promises are fulfilled and modification orders
162          * are established. Each entry in the list may only be partially
163          * filled, depending on its pending status.
164          */
165
166         SnapVector<ModelAction *> thrd_last_action;
167         SnapVector<ModelAction *> thrd_last_fence_release;
168
169         /** A special model-checker Thread; used for associating with
170          *  model-checker-related ModelAcitons */
171         Thread *model_thread;
172
173         /** Private data members that should be snapshotted. They are grouped
174          * together for efficiency and maintainability. */
175         struct model_snapshot_members * const priv;
176
177         /**
178          * @brief The modification order graph
179          *
180          * A directed acyclic graph recording observations of the modification
181          * order on all the atomic objects in the system. This graph should
182          * never contain any cycles, as that represents a violation of the
183          * memory model (total ordering). This graph really consists of many
184          * disjoint (unconnected) subgraphs, each graph corresponding to a
185          * separate ordering on a distinct object.
186          *
187          * The edges in this graph represent the "ordered before" relation,
188          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
189          * <tt>b</tt>.
190          */
191         CycleGraph * const mo_graph;
192
193         Fuzzer * fuzzer;
194
195         Thread * action_select_next_thread(const ModelAction *curr) const;
196
197         /* thrd_func_list stores a list of function ids for each thread.
198          * Each element in thrd_func_list stores the functions that
199          * thread i has entered and yet to exit from
200          *
201          * This data structure is handled by ModelHistory
202          */
203         SnapVector< func_id_list_t * > thrd_func_list;
204
205         /* Keeps track of atomic actions that thread i has performed in some
206          * function. Index of SnapVector is thread id. SnapList simulates
207          * the call stack.
208          *
209          * This data structure is handled by ModelHistory
210          */
211         SnapVector< SnapList< func_inst_list_t *> *> thrd_func_inst_lists;
212 };
213
214 #endif  /* __EXECUTION_H__ */