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