19143d36d4f433c5c734652fbd8fe7ae35a51853
[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
21 /* Forward declaration */
22 class NodeStack;
23 class CycleGraph;
24 class Promise;
25
26 /**
27  * Model checker parameter structure. Holds run-time configuration options for
28  * the model checker.
29  */
30 struct model_params {
31         int maxreads;
32 };
33
34 /**
35  * Structure for holding small ModelChecker members that should be snapshotted
36  */
37 struct model_snapshot_members {
38         ModelAction *current_action;
39         int next_thread_id;
40         modelclock_t used_sequence_numbers;
41         Thread *nextThread;
42         ModelAction *next_backtrack;
43
44         /** @see ModelChecker::lazy_sync_size */
45         unsigned int lazy_sync_size;
46 };
47
48 /** @brief The central structure for model-checking */
49 class ModelChecker {
50 public:
51         ModelChecker(struct model_params params);
52         ~ModelChecker();
53
54         /** @returns the context for the main model-checking system thread */
55         ucontext_t * get_system_context() { return &system_context; }
56
57         /** Prints an execution summary with trace information. */
58         void print_summary();
59
60         void add_thread(Thread *t);
61         void remove_thread(Thread *t);
62         Thread * get_thread(thread_id_t tid) { return thread_map->get(id_to_int(tid)); }
63         Thread * get_thread(ModelAction *act) { return get_thread(act->get_tid()); }
64
65         thread_id_t get_next_id();
66         int get_num_threads();
67         modelclock_t get_next_seq_num();
68
69         /** @return The currently executing Thread. */
70         Thread * get_current_thread() { return scheduler->get_current_thread(); }
71
72         int switch_to_master(ModelAction *act);
73         ClockVector * get_cv(thread_id_t tid);
74         ModelAction * get_parent_action(thread_id_t tid);
75         bool next_execution();
76         bool isfeasible();
77         bool isfinalfeasible();
78         void check_promises(ClockVector *old_cv, ClockVector * merge_cv);
79         void get_release_seq_heads(ModelAction *act,
80                         std::vector<const ModelAction *> *release_heads);
81
82         void finish_execution();
83         bool isfeasibleprefix();
84         void set_assert() {asserted=true;}
85
86         MEMALLOC
87 private:
88         /** The scheduler to use: tracks the running/ready Threads */
89         Scheduler *scheduler;
90
91         bool has_asserted() {return asserted;}
92         void reset_asserted() {asserted=false;}
93         int num_executions;
94
95         const model_params params;
96
97         /**
98          * Stores the ModelAction for the current thread action.  Call this
99          * immediately before switching from user- to system-context to pass
100          * data between them.
101          * @param act The ModelAction created by the user-thread action
102          */
103         void set_current_action(ModelAction *act) { priv->current_action = act; }
104         Thread * check_current_action(ModelAction *curr);
105         bool process_read(ModelAction *curr, Thread * th, bool second_part_of_rmw);
106
107         bool take_step();
108
109         void check_recency(ModelAction *curr, bool already_added);
110         ModelAction * get_last_conflict(ModelAction *act);
111         void set_backtracking(ModelAction *act);
112         Thread * get_next_thread(ModelAction *curr);
113         ModelAction * get_next_backtrack();
114         void reset_to_initial_state();
115         bool resolve_promises(ModelAction *curr);
116         void compute_promises(ModelAction *curr);
117
118         void add_action_to_lists(ModelAction *act);
119         ModelAction * get_last_action(thread_id_t tid);
120         ModelAction * get_last_seq_cst(const void *location);
121         void build_reads_from_past(ModelAction *curr);
122         ModelAction * process_rmw(ModelAction *curr);
123         void post_r_modification_order(ModelAction *curr, const ModelAction *rf);
124         bool r_modification_order(ModelAction *curr, const ModelAction *rf);
125         bool w_modification_order(ModelAction *curr);
126         bool release_seq_head(const ModelAction *rf,
127                         std::vector<const ModelAction *> *release_heads) const;
128         bool resolve_release_sequences(void *location);
129
130         ModelAction *diverge;
131
132         ucontext_t system_context;
133         action_list_t *action_trace;
134         HashTable<int, Thread *, int> *thread_map;
135
136         /** Per-object list of actions. Maps an object (i.e., memory location)
137          * to a trace of all actions performed on the object. */
138         HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
139
140         HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
141         std::vector<Promise *> *promises;
142
143         /**
144          * Collection of lists of objects that might synchronize with one or
145          * more release sequence. Release sequences might be determined lazily
146          * as promises are fulfilled and modification orders are established.
147          * This structure maps its lists by object location. Each ModelAction
148          * in the lists should be an acquire operation.
149          */
150         HashTable<void *, std::list<ModelAction *>, uintptr_t, 4> *lazy_sync_with_release;
151
152         /**
153          * Represents the total size of the
154          * ModelChecker::lazy_sync_with_release lists. This count should be
155          * snapshotted, so it is actually a pointer to a location within
156          * ModelChecker::priv
157          */
158         unsigned int *lazy_sync_size;
159
160         std::vector<ModelAction *> *thrd_last_action;
161         NodeStack *node_stack;
162
163         /** Private data members that should be snapshotted. They are grouped
164          * together for efficiency and maintainability. */
165         struct model_snapshot_members *priv;
166
167         /**
168          * @brief The modification order graph
169          *
170          * A directed acyclic graph recording observations of the modification
171          * order on all the atomic objects in the system. This graph should
172          * never contain any cycles, as that represents a violation of the
173          * memory model (total ordering). This graph really consists of many
174          * disjoint (unconnected) subgraphs, each graph corresponding to a
175          * separate ordering on a distinct object.
176          *
177          * The edges in this graph represent the "ordered before" relation,
178          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
179          * <tt>b</tt>.
180          */
181         CycleGraph *mo_graph;
182         bool failed_promise;
183         bool too_many_reads;
184         bool asserted;
185 };
186
187 extern ModelChecker *model;
188
189 #endif /* __MODEL_H__ */