Merge branch 'master' of /home/git/random-fuzzer into thread-switch
[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 #include "snapshot-interface.h"
20
21 /** @brief Model checker execution stats */
22 struct execution_stats {
23         int num_total;  /**< @brief Total number of executions */
24         int num_buggy_executions;       /** @brief Number of buggy executions */
25         int num_complete;       /**< @brief Number of feasible, non-buggy, complete executions */
26 };
27
28 /** @brief The central structure for model-checking */
29 class ModelChecker {
30 public:
31         ModelChecker();
32         ~ModelChecker();
33         model_params * getParams();
34         void run();
35
36         /** Exit the model checker, intended for pluggins. */
37         void exit_model_checker();
38
39         /** @returns the context for the main model-checking system thread */
40         ucontext_t * get_system_context() { return &system_context; }
41
42         ModelExecution * get_execution() const { return execution; }
43         ModelHistory * get_history() const { return history; }
44
45         int get_execution_number() const { return execution_number; }
46
47         Thread * get_thread(thread_id_t tid) const;
48         Thread * get_thread(const ModelAction *act) const;
49
50         Thread * get_current_thread() const;
51
52         void switch_from_master(Thread *thread);
53         uint64_t switch_to_master(ModelAction *act);
54         uint64_t switch_thread(ModelAction *act);
55
56         void startRunExecution(Thread *old);
57         void finishRunExecution(Thread *old);
58         void consumeAction();
59         void chooseThread(ModelAction *act, Thread *thr);
60         Thread * getNextThread();
61         void handleChosenThread(Thread *old);
62         void handleNewValidThread(Thread *old, Thread *next);
63
64         void assert_bug(const char *msg, ...);
65
66         void assert_user_bug(const char *msg);
67
68         model_params params;
69         void add_trace_analysis(TraceAnalysis *a) {     trace_analyses.push_back(a); }
70         void set_inspect_plugin(TraceAnalysis *a) {     inspect_plugin=a;       }
71         void startChecker();
72         Thread * getInitThread() {return init_thread;}
73         Scheduler * getScheduler() {return scheduler;}
74         MEMALLOC
75 private:
76         /** Snapshot id we return to restart. */
77         snapshot_id snapshot;
78
79         /** The scheduler to use: tracks the running/ready Threads */
80         Scheduler * const scheduler;
81         ModelHistory * history;
82         ModelExecution *execution;
83         Thread * init_thread;
84
85         int execution_number;
86
87         unsigned int curr_thread_num;
88
89         Thread * chosen_thread;
90
91         bool thread_chosen;
92         bool break_execution;
93
94         modelclock_t checkfree;
95
96         unsigned int get_num_threads() const;
97
98         void finish_execution(bool moreexecutions);
99         bool should_terminate_execution();
100
101         Thread * get_next_thread();
102         void reset_to_initial_state();
103
104         ucontext_t system_context;
105
106         ModelVector<TraceAnalysis *> trace_analyses;
107
108         /** @bref Plugin that can inspect new actions. */
109         TraceAnalysis *inspect_plugin;
110         /** @brief The cumulative execution stats */
111         struct execution_stats stats;
112         void record_stats();
113         void run_trace_analyses();
114         void print_bugs() const;
115         void print_execution(bool printbugs) const;
116         void print_stats() const;
117 };
118
119 extern ModelChecker *model;
120 void parse_options(struct model_params *params);
121 void install_trace_analyses(ModelExecution *execution);
122
123 #endif  /* __MODEL_H__ */