towards fuzzing only
[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
18 #include "mutex.h"
19 #include <condition_variable>
20
21 /* Forward declaration */
22 class Node;
23 class NodeStack;
24 class CycleGraph;
25 class Scheduler;
26 class Thread;
27 class ClockVector;
28 struct model_snapshot_members;
29 class ModelChecker;
30 struct bug_message;
31
32 /** @brief Shorthand for a list of release sequence heads */
33 typedef ModelVector<const ModelAction *> rel_heads_list_t;
34 typedef SnapList<ModelAction *> action_list_t;
35
36 struct PendingFutureValue {
37         PendingFutureValue(ModelAction *writer, ModelAction *reader) :
38                 writer(writer), reader(reader)
39         { }
40         const ModelAction *writer;
41         ModelAction *reader;
42 };
43
44 /** @brief Records information regarding a single pending release sequence */
45 struct release_seq {
46         /** @brief The acquire operation */
47         ModelAction *acquire;
48         /** @brief The read operation that may read from a release sequence;
49          *  may be the same as acquire, or else an earlier action in the same
50          *  thread (i.e., when 'acquire' is a fence-acquire) */
51         const ModelAction *read;
52         /** @brief The head of the RMW chain from which 'read' reads; may be
53          *  equal to 'release' */
54         const ModelAction *rf;
55         /** @brief The head of the potential longest release sequence chain */
56         const ModelAction *release;
57         /** @brief The write(s) that may break the release sequence */
58         SnapVector<const ModelAction *> writes;
59 };
60
61 /** @brief The central structure for model-checking */
62 class ModelExecution {
63 public:
64         ModelExecution(ModelChecker *m,
65                         const struct model_params *params,
66                         Scheduler *scheduler,
67                         NodeStack *node_stack);
68         ~ModelExecution();
69
70         const struct model_params * get_params() const { return params; }
71
72         Thread * take_step(ModelAction *curr);
73
74         void print_summary() const;
75 #if SUPPORT_MOD_ORDER_DUMP
76         void dumpGraph(char *filename) const;
77 #endif
78
79         void add_thread(Thread *t);
80         Thread * get_thread(thread_id_t tid) const;
81         Thread * get_thread(const ModelAction *act) const;
82
83         pthread_t get_pthread_counter() { return pthread_counter; }
84         void incr_pthread_counter() { pthread_counter++; }
85         Thread * get_pthread(pthread_t pid);
86
87         bool is_enabled(Thread *t) const;
88         bool is_enabled(thread_id_t tid) const;
89
90         thread_id_t get_next_id();
91         unsigned int get_num_threads() const;
92
93         ClockVector * get_cv(thread_id_t tid) const;
94         ModelAction * get_parent_action(thread_id_t tid) const;
95         bool isfeasibleprefix() const;
96
97         action_list_t * get_actions_on_obj(void * obj, thread_id_t tid) const;
98         ModelAction * get_last_action(thread_id_t tid) const;
99
100         bool check_action_enabled(ModelAction *curr);
101
102         bool assert_bug(const char *msg);
103         bool have_bug_reports() const;
104         SnapVector<bug_message *> * get_bugs() const;
105
106         bool has_asserted() const;
107         void set_assert();
108         bool is_complete_execution() const;
109
110         void print_infeasibility(const char *prefix) const;
111         bool is_infeasible() const;
112         bool is_deadlocked() const;
113         bool too_many_steps() const;
114
115         action_list_t * get_action_trace() { return &action_trace; }
116
117         CycleGraph * const get_mo_graph() { return mo_graph; }
118   HashTable<pthread_cond_t *, cdsc::condition_variable *, uintptr_t, 4> * getCondMap() {return &cond_map;}
119   HashTable<pthread_mutex_t *, cdsc::mutex *, uintptr_t, 4> * getMutexMap() {return &mutex_map;}
120   
121         SNAPSHOTALLOC
122 private:
123         int get_execution_number() const;
124
125         ModelChecker *model;
126
127         const model_params * const params;
128
129         /** The scheduler to use: tracks the running/ready Threads */
130         Scheduler * const scheduler;
131
132         bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
133         void set_bad_synchronization();
134         void set_bad_sc_read();
135         bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
136         void wake_up_sleeping_actions(ModelAction *curr);
137         modelclock_t get_next_seq_num();
138
139         bool next_execution();
140         ModelAction * check_current_action(ModelAction *curr);
141         bool initialize_curr_action(ModelAction **curr);
142   bool process_read(ModelAction *curr, ModelVector<ModelAction *> * rf_set);
143         bool process_write(ModelAction *curr);
144         bool process_fence(ModelAction *curr);
145         bool process_mutex(ModelAction *curr);
146
147         bool process_thread_action(ModelAction *curr);
148         bool read_from(ModelAction *act, const ModelAction *rf);
149         bool synchronize(const ModelAction *first, ModelAction *second);
150
151         void add_action_to_lists(ModelAction *act);
152         ModelAction * get_last_fence_release(thread_id_t tid) const;
153         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
154         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
155         ModelAction * get_last_unlock(ModelAction *curr) const;
156   ModelVector<ModelAction *> * build_may_read_from(ModelAction *curr);
157         ModelAction * process_rmw(ModelAction *curr);
158
159         template <typename rf_type>
160         bool r_modification_order(ModelAction *curr, const rf_type *rf);
161
162         bool w_modification_order(ModelAction *curr);
163         void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
164   bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads) const;
165         ModelAction * get_uninitialized_action(const ModelAction *curr) const;
166
167         action_list_t action_trace;
168         SnapVector<Thread *> thread_map;
169         SnapVector<Thread *> pthread_map;
170         pthread_t pthread_counter;
171
172         /** Per-object list of actions. Maps an object (i.e., memory location)
173          * to a trace of all actions performed on the object. */
174         HashTable<const void *, action_list_t *, uintptr_t, 4> obj_map;
175
176         /** Per-object list of actions. Maps an object (i.e., memory location)
177          * to a trace of all actions performed on the object. */
178         HashTable<const void *, action_list_t *, uintptr_t, 4> condvar_waiters_map;
179
180         HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4> obj_thrd_map;
181
182   HashTable<pthread_mutex_t *, cdsc::mutex *, uintptr_t, 4> mutex_map;
183         HashTable<pthread_cond_t *, cdsc::condition_variable *, uintptr_t, 4> cond_map;
184
185         /**
186          * List of pending release sequences. Release sequences might be
187          * determined lazily as promises are fulfilled and modification orders
188          * are established. Each entry in the list may only be partially
189          * filled, depending on its pending status.
190          */
191
192         SnapVector<ModelAction *> thrd_last_action;
193         SnapVector<ModelAction *> thrd_last_fence_release;
194         NodeStack * const node_stack;
195
196         /** A special model-checker Thread; used for associating with
197          *  model-checker-related ModelAcitons */
198         Thread *model_thread;
199
200         /** Private data members that should be snapshotted. They are grouped
201          * together for efficiency and maintainability. */
202         struct model_snapshot_members * const priv;
203
204         /**
205          * @brief The modification order graph
206          *
207          * A directed acyclic graph recording observations of the modification
208          * order on all the atomic objects in the system. This graph should
209          * never contain any cycles, as that represents a violation of the
210          * memory model (total ordering). This graph really consists of many
211          * disjoint (unconnected) subgraphs, each graph corresponding to a
212          * separate ordering on a distinct object.
213          *
214          * The edges in this graph represent the "ordered before" relation,
215          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
216          * <tt>b</tt>.
217          */
218         CycleGraph * const mo_graph;
219
220         Thread * action_select_next_thread(const ModelAction *curr) const;
221 };
222
223 #endif /* __EXECUTION_H__ */