1d032e1b1e0727113f8167d6e0f09a84ed93fbc1
[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 <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 "context.h"
17 #include "params.h"
18
19 #include <map>
20
21 /* Forward declaration */
22 class Node;
23 class NodeStack;
24 class CycleGraph;
25 class Promise;
26 class Scheduler;
27 class Thread;
28 class ClockVector;
29 class TraceAnalysis;
30 class ModelExecution;
31 class ModelAction;
32
33 typedef SnapList<ModelAction *> action_list_t;
34
35 /** @brief Model checker execution stats */
36 struct execution_stats {
37         int num_total; /**< @brief Total number of executions */
38         int num_infeasible; /**< @brief Number of infeasible executions */
39         int num_buggy_executions; /** @brief Number of buggy executions */
40         int num_complete; /**< @brief Number of feasible, non-buggy, complete executions */
41         int num_redundant; /**< @brief Number of redundant, aborted executions */
42 };
43
44 /** @brief The central structure for model-checking */
45 class ModelChecker {
46 public:
47         ModelChecker(struct model_params params);
48         ~ModelChecker();
49
50         void run();
51
52         /** Restart the model checker, intended for pluggins. */
53         void restart();
54
55         /** Exit the model checker, intended for pluggins. */
56         void exit_model_checker();
57
58         /** Check the exit_flag. */
59         bool get_exit_flag() const { return exit_flag; }
60
61         /** @returns the context for the main model-checking system thread */
62         ucontext_t * get_system_context() { return &system_context; }
63
64         ModelExecution * get_execution() const { return execution; }
65
66         int get_execution_number() const { return execution_number; }
67
68         Thread * get_thread(thread_id_t tid) const;
69         Thread * get_thread(const ModelAction *act) const;
70
71         Thread * get_current_thread() const;
72
73         void switch_from_master(Thread *thread);
74         uint64_t switch_to_master(ModelAction *act);
75
76         bool assert_bug(const char *msg, ...);
77         void assert_user_bug(const char *msg);
78
79         const model_params params;
80         void add_trace_analysis(TraceAnalysis *a) {     trace_analyses.push_back(a); }
81         void set_inspect_plugin(TraceAnalysis *a) {     inspect_plugin=a;       }
82         MEMALLOC
83         std::map<pthread_t, ModelAction*> pthread_map;
84 private:
85         /** Flag indicates whether to restart the model checker. */
86         bool restart_flag;
87         /** Flag indicates whether to exit the model checker. */
88         bool exit_flag;
89
90         /** The scheduler to use: tracks the running/ready Threads */
91         Scheduler * const scheduler;
92         NodeStack * const node_stack;
93         ModelExecution *execution;
94
95         int execution_number;
96
97         unsigned int get_num_threads() const;
98
99         void execute_sleep_set();
100
101         bool next_execution();
102         bool should_terminate_execution();
103
104         Thread * get_next_thread();
105         void reset_to_initial_state();
106
107
108         ModelAction *diverge;
109         ModelAction *earliest_diverge;
110
111         ucontext_t system_context;
112
113         ModelVector<TraceAnalysis *> trace_analyses;
114
115         /** @bref Implement restart. */
116         void do_restart();
117         /** @bref Plugin that can inspect new actions. */
118         TraceAnalysis *inspect_plugin;
119         /** @brief The cumulative execution stats */
120         struct execution_stats stats;
121         void record_stats();
122         void run_trace_analyses();
123         void print_bugs() const;
124         void print_execution(bool printbugs) const;
125         void print_stats() const;
126
127         friend void user_main_wrapper();
128 };
129
130 extern ModelChecker *model;
131
132 #endif /* __MODEL_H__ */