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