model: add "bad synchronization" flag
[c11tester.git] / model.h
1 /** @file model.h
2  *  @brief Core model checker.
3  */
4
5 #ifndef __MODEL_H__
6 #define __MODEL_H__
7
8 #include <list>
9 #include <vector>
10 #include <cstddef>
11 #include <ucontext.h>
12
13 #include "schedule.h"
14 #include "mymemory.h"
15 #include "libthreads.h"
16 #include "threads.h"
17 #include "action.h"
18 #include "clockvector.h"
19 #include "hashtable.h"
20 #include "workqueue.h"
21
22 /* Forward declaration */
23 class NodeStack;
24 class CycleGraph;
25 class Promise;
26
27 /** @brief Shorthand for a list of release sequence heads */
28 typedef std::vector< const ModelAction *, MyAlloc<const ModelAction *> > rel_heads_list_t;
29
30 /**
31  * Model checker parameter structure. Holds run-time configuration options for
32  * the model checker.
33  */
34 struct model_params {
35         int maxreads;
36         int maxfuturedelay;
37         unsigned int fairwindow;
38         unsigned int enabledcount;
39 };
40
41 struct PendingFutureValue {
42         uint64_t value;
43         modelclock_t expiration;
44         ModelAction * act;
45 };
46
47 /**
48  * Structure for holding small ModelChecker members that should be snapshotted
49  */
50 struct model_snapshot_members {
51         ModelAction *current_action;
52         int next_thread_id;
53         modelclock_t used_sequence_numbers;
54         Thread *nextThread;
55         ModelAction *next_backtrack;
56 };
57
58 /** @brief The central structure for model-checking */
59 class ModelChecker {
60 public:
61         ModelChecker(struct model_params params);
62         ~ModelChecker();
63
64         /** @returns the context for the main model-checking system thread */
65         ucontext_t * get_system_context() { return &system_context; }
66
67         /** Prints an execution summary with trace information. */
68         void print_summary();
69
70         void add_thread(Thread *t);
71         void remove_thread(Thread *t);
72         Thread * get_thread(thread_id_t tid) { return thread_map->get(id_to_int(tid)); }
73         Thread * get_thread(ModelAction *act) { return get_thread(act->get_tid()); }
74
75         thread_id_t get_next_id();
76         int get_num_threads();
77         modelclock_t get_next_seq_num();
78
79         /** @return The currently executing Thread. */
80         Thread * get_current_thread() { return scheduler->get_current_thread(); }
81
82         int switch_to_master(ModelAction *act);
83         ClockVector * get_cv(thread_id_t tid);
84         ModelAction * get_parent_action(thread_id_t tid);
85         bool next_execution();
86         bool isfeasible();
87         bool isfeasibleotherthanRMW();
88         bool isfinalfeasible();
89         void check_promises(ClockVector *old_cv, ClockVector * merge_cv);
90         void get_release_seq_heads(ModelAction *act, rel_heads_list_t *release_heads);
91         void finish_execution();
92         bool isfeasibleprefix();
93         void set_assert() {asserted=true;}
94
95         /** @brief Alert the model-checker that an incorrectly-ordered
96          * synchronization was made */
97         void set_bad_synchronization() { bad_synchronization = true; }
98
99         const model_params params;
100
101         MEMALLOC
102 private:
103         /** The scheduler to use: tracks the running/ready Threads */
104         Scheduler *scheduler;
105
106         bool thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader);
107         bool has_asserted() {return asserted;}
108         void reset_asserted() {asserted=false;}
109         int num_executions;
110         int num_feasible_executions;
111         bool promises_expired();
112
113         /**
114          * Stores the ModelAction for the current thread action.  Call this
115          * immediately before switching from user- to system-context to pass
116          * data between them.
117          * @param act The ModelAction created by the user-thread action
118          */
119         void set_current_action(ModelAction *act) { priv->current_action = act; }
120         Thread * check_current_action(ModelAction *curr);
121         ModelAction * initialize_curr_action(ModelAction *curr);
122         bool process_read(ModelAction *curr, bool second_part_of_rmw);
123         bool process_write(ModelAction *curr);
124         bool process_mutex(ModelAction *curr);
125         bool process_thread_action(ModelAction *curr);
126         bool check_action_enabled(ModelAction *curr);
127
128         bool take_step();
129
130         void check_recency(ModelAction *curr, const ModelAction *rf);
131         ModelAction * get_last_conflict(ModelAction *act);
132         void set_backtracking(ModelAction *act);
133         Thread * get_next_thread(ModelAction *curr);
134         ModelAction * get_next_backtrack();
135         void reset_to_initial_state();
136         bool resolve_promises(ModelAction *curr);
137         void compute_promises(ModelAction *curr);
138
139         void check_curr_backtracking(ModelAction * curr);
140         void add_action_to_lists(ModelAction *act);
141         ModelAction * get_last_action(thread_id_t tid) const;
142         ModelAction * get_last_seq_cst(ModelAction *curr) const;
143         ModelAction * get_last_unlock(ModelAction *curr) const;
144         void build_reads_from_past(ModelAction *curr);
145         ModelAction * process_rmw(ModelAction *curr);
146         void post_r_modification_order(ModelAction *curr, const ModelAction *rf);
147         bool r_modification_order(ModelAction *curr, const ModelAction *rf);
148         bool w_modification_order(ModelAction *curr);
149         bool release_seq_head(const ModelAction *rf, rel_heads_list_t *release_heads) const;
150         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
151         void do_complete_join(ModelAction *join);
152
153         ModelAction *diverge;
154
155         ucontext_t system_context;
156         action_list_t *action_trace;
157         HashTable<int, Thread *, int> *thread_map;
158
159         /** Per-object list of actions. Maps an object (i.e., memory location)
160          * to a trace of all actions performed on the object. */
161         HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
162
163         /** Per-object list of actions. Maps an object (i.e., memory location)
164          * to a trace of all actions performed on the object. */
165         HashTable<const void *, action_list_t, uintptr_t, 4> *lock_waiters_map;
166
167         HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
168         std::vector<Promise *> *promises;
169         std::vector<struct PendingFutureValue> *futurevalues;
170
171         /**
172          * List of acquire actions that might synchronize with one or more
173          * release sequence. Release sequences might be determined lazily as
174          * promises are fulfilled and modification orders are established. Each
175          * ModelAction in this list must be an acquire operation.
176          */
177         std::vector<ModelAction *> *pending_acq_rel_seq;
178
179         std::vector<ModelAction *> *thrd_last_action;
180         NodeStack *node_stack;
181
182         /** Private data members that should be snapshotted. They are grouped
183          * together for efficiency and maintainability. */
184         struct model_snapshot_members *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 *mo_graph;
201         bool failed_promise;
202         bool too_many_reads;
203         bool asserted;
204         /** @brief Incorrectly-ordered synchronization was made */
205         bool bad_synchronization;
206 };
207
208 extern ModelChecker *model;
209
210 #endif /* __MODEL_H__ */