3f9e6c2ea8867338dedea119acc698cd8458dcd3
[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 Promise;
24 class Scheduler;
25 class Thread;
26 class ClockVector;
27 class TraceAnalysis;
28 class ModelExecution;
29 class ModelAction;
30
31 typedef SnapList<ModelAction *> action_list_t;
32
33 /** @brief Model checker execution stats */
34 struct execution_stats {
35         int num_total; /**< @brief Total number of executions */
36         int num_infeasible; /**< @brief Number of infeasible executions */
37         int num_buggy_executions; /** @brief Number of buggy executions */
38         int num_complete; /**< @brief Number of feasible, non-buggy, complete executions */
39         int num_redundant; /**< @brief Number of redundant, aborted executions */
40 };
41
42 /** @brief The central structure for model-checking */
43 class ModelChecker {
44 public:
45         ModelChecker(struct model_params params);
46         ~ModelChecker();
47
48         void run();
49
50         /** @returns the context for the main model-checking system thread */
51         ucontext_t * get_system_context() { return &system_context; }
52
53         const ModelExecution * get_execution() const { return execution; }
54
55         Thread * get_thread(thread_id_t tid) const;
56         Thread * get_thread(const ModelAction *act) const;
57         int get_promise_number(const Promise *promise) const;
58
59         bool is_enabled(Thread *t) const;
60         bool is_enabled(thread_id_t tid) const;
61
62         thread_id_t get_next_id();
63         unsigned int get_num_threads() const;
64         Thread * get_current_thread() const;
65
66         void switch_from_master(Thread *thread);
67         uint64_t switch_to_master(ModelAction *act);
68         ClockVector * get_cv(thread_id_t tid) const;
69         ModelAction * get_parent_action(thread_id_t tid) const;
70         void check_promises_thread_disabled();
71         bool isfeasibleprefix() const;
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) {
78                 trace_analyses->push_back(a);
79         }
80
81         action_list_t * get_actions_on_obj(void * obj, thread_id_t tid);
82         ModelAction * get_last_action(thread_id_t tid) const;
83
84         MEMALLOC
85 private:
86         /** The scheduler to use: tracks the running/ready Threads */
87         Scheduler * const scheduler;
88         NodeStack * const node_stack;
89         ModelExecution *execution;
90
91         void execute_sleep_set();
92
93         bool next_execution();
94         bool should_terminate_execution();
95
96         Thread * get_next_thread();
97         void reset_to_initial_state();
98
99
100         ModelAction *diverge;
101         ModelAction *earliest_diverge;
102
103         ucontext_t system_context;
104
105         ModelVector<TraceAnalysis *> * trace_analyses;
106
107         /** @brief The cumulative execution stats */
108         struct execution_stats stats;
109         void record_stats();
110         void run_trace_analyses();
111         void print_bugs() const;
112         void print_execution(bool printbugs) const;
113         void print_stats() const;
114
115         friend void user_main_wrapper();
116 };
117
118 extern ModelChecker *model;
119
120 #endif /* __MODEL_H__ */