1d57aff24SDaniel Bristot de OliveiraDeterministic Automata Monitor Synthesis
2d57aff24SDaniel Bristot de Oliveira========================================
3d57aff24SDaniel Bristot de Oliveira
4*d56b699dSBjorn HelgaasThe starting point for the application of runtime verification (RV) techniques
5d57aff24SDaniel Bristot de Oliveirais the *specification* or *modeling* of the desired (or undesired) behavior
6d57aff24SDaniel Bristot de Oliveiraof the system under scrutiny.
7d57aff24SDaniel Bristot de Oliveira
8d57aff24SDaniel Bristot de OliveiraThe formal representation needs to be then *synthesized* into a *monitor*
9d57aff24SDaniel Bristot de Oliveirathat can then be used in the analysis of the trace of the system. The
10d57aff24SDaniel Bristot de Oliveira*monitor* connects to the system via an *instrumentation* that converts
11d57aff24SDaniel Bristot de Oliveirathe events from the *system* to the events of the *specification*.
12d57aff24SDaniel Bristot de Oliveira
13d57aff24SDaniel Bristot de Oliveira
14d57aff24SDaniel Bristot de OliveiraIn Linux terms, the runtime verification monitors are encapsulated inside
15d57aff24SDaniel Bristot de Oliveirathe *RV monitor* abstraction. The RV monitor includes a set of instances
16d57aff24SDaniel Bristot de Oliveiraof the monitor (per-cpu monitor, per-task monitor, and so on), the helper
17d57aff24SDaniel Bristot de Oliveirafunctions that glue the monitor to the system reference model, and the
18d57aff24SDaniel Bristot de Oliveiratrace output as a reaction to event parsing and exceptions, as depicted
19d57aff24SDaniel Bristot de Oliveirabelow::
20d57aff24SDaniel Bristot de Oliveira
21d57aff24SDaniel Bristot de Oliveira Linux  +----- RV Monitor ----------------------------------+ Formal
22d57aff24SDaniel Bristot de Oliveira  Realm |                                                   |  Realm
23d57aff24SDaniel Bristot de Oliveira  +-------------------+     +----------------+     +-----------------+
24d57aff24SDaniel Bristot de Oliveira  |   Linux kernel    |     |     Monitor    |     |     Reference   |
25d57aff24SDaniel Bristot de Oliveira  |     Tracing       |  -> |   Instance(s)  | <-  |       Model     |
26d57aff24SDaniel Bristot de Oliveira  | (instrumentation) |     | (verification) |     | (specification) |
27d57aff24SDaniel Bristot de Oliveira  +-------------------+     +----------------+     +-----------------+
28d57aff24SDaniel Bristot de Oliveira         |                          |                       |
29d57aff24SDaniel Bristot de Oliveira         |                          V                       |
30d57aff24SDaniel Bristot de Oliveira         |                     +----------+                 |
31d57aff24SDaniel Bristot de Oliveira         |                     | Reaction |                 |
32d57aff24SDaniel Bristot de Oliveira         |                     +--+--+--+-+                 |
33d57aff24SDaniel Bristot de Oliveira         |                        |  |  |                   |
34d57aff24SDaniel Bristot de Oliveira         |                        |  |  +-> trace output ?  |
35d57aff24SDaniel Bristot de Oliveira         +------------------------|--|----------------------+
36d57aff24SDaniel Bristot de Oliveira                                  |  +----> panic ?
37d57aff24SDaniel Bristot de Oliveira                                  +-------> <user-specified>
38d57aff24SDaniel Bristot de Oliveira
39d57aff24SDaniel Bristot de OliveiraDA monitor synthesis
40d57aff24SDaniel Bristot de Oliveira--------------------
41d57aff24SDaniel Bristot de Oliveira
42d57aff24SDaniel Bristot de OliveiraThe synthesis of automata-based models into the Linux *RV monitor* abstraction
43d57aff24SDaniel Bristot de Oliveirais automated by the dot2k tool and the rv/da_monitor.h header file that
44d57aff24SDaniel Bristot de Oliveiracontains a set of macros that automatically generate the monitor's code.
45d57aff24SDaniel Bristot de Oliveira
46d57aff24SDaniel Bristot de Oliveiradot2k
47d57aff24SDaniel Bristot de Oliveira-----
48d57aff24SDaniel Bristot de Oliveira
49d57aff24SDaniel Bristot de OliveiraThe dot2k utility leverages dot2c by converting an automaton model in
50d57aff24SDaniel Bristot de Oliveirathe DOT format into the C representation [1] and creating the skeleton of
51d57aff24SDaniel Bristot de Oliveiraa kernel monitor in C.
52d57aff24SDaniel Bristot de Oliveira
53d57aff24SDaniel Bristot de OliveiraFor example, it is possible to transform the wip.dot model present in
54d57aff24SDaniel Bristot de Oliveira[1] into a per-cpu monitor with the following command::
55d57aff24SDaniel Bristot de Oliveira
56d57aff24SDaniel Bristot de Oliveira  $ dot2k -d wip.dot -t per_cpu
57d57aff24SDaniel Bristot de Oliveira
58d57aff24SDaniel Bristot de OliveiraThis will create a directory named wip/ with the following files:
59d57aff24SDaniel Bristot de Oliveira
60d57aff24SDaniel Bristot de Oliveira- wip.h: the wip model in C
61d57aff24SDaniel Bristot de Oliveira- wip.c: the RV monitor
62d57aff24SDaniel Bristot de Oliveira
63d57aff24SDaniel Bristot de OliveiraThe wip.c file contains the monitor declaration and the starting point for
64d57aff24SDaniel Bristot de Oliveirathe system instrumentation.
65d57aff24SDaniel Bristot de Oliveira
66d57aff24SDaniel Bristot de OliveiraMonitor macros
67d57aff24SDaniel Bristot de Oliveira--------------
68d57aff24SDaniel Bristot de Oliveira
69d57aff24SDaniel Bristot de OliveiraThe rv/da_monitor.h enables automatic code generation for the *Monitor
70d57aff24SDaniel Bristot de OliveiraInstance(s)* using C macros.
71d57aff24SDaniel Bristot de Oliveira
72d57aff24SDaniel Bristot de OliveiraThe benefits of the usage of macro for monitor synthesis are 3-fold as it:
73d57aff24SDaniel Bristot de Oliveira
74d57aff24SDaniel Bristot de Oliveira- Reduces the code duplication;
75d57aff24SDaniel Bristot de Oliveira- Facilitates the bug fix/improvement;
76d57aff24SDaniel Bristot de Oliveira- Avoids the case of developers changing the core of the monitor code
77d57aff24SDaniel Bristot de Oliveira  to manipulate the model in a (let's say) non-standard way.
78d57aff24SDaniel Bristot de Oliveira
79d57aff24SDaniel Bristot de OliveiraThis initial implementation presents three different types of monitor instances:
80d57aff24SDaniel Bristot de Oliveira
81d57aff24SDaniel Bristot de Oliveira- ``#define DECLARE_DA_MON_GLOBAL(name, type)``
82d57aff24SDaniel Bristot de Oliveira- ``#define DECLARE_DA_MON_PER_CPU(name, type)``
83d57aff24SDaniel Bristot de Oliveira- ``#define DECLARE_DA_MON_PER_TASK(name, type)``
84d57aff24SDaniel Bristot de Oliveira
85d57aff24SDaniel Bristot de OliveiraThe first declares the functions for a global deterministic automata monitor,
86d57aff24SDaniel Bristot de Oliveirathe second for monitors with per-cpu instances, and the third with per-task
87d57aff24SDaniel Bristot de Oliveirainstances.
88d57aff24SDaniel Bristot de Oliveira
89d57aff24SDaniel Bristot de OliveiraIn all cases, the 'name' argument is a string that identifies the monitor, and
90d57aff24SDaniel Bristot de Oliveirathe 'type' argument is the data type used by dot2k on the representation of
91d57aff24SDaniel Bristot de Oliveirathe model in C.
92d57aff24SDaniel Bristot de Oliveira
93d57aff24SDaniel Bristot de OliveiraFor example, the wip model with two states and three events can be
94d57aff24SDaniel Bristot de Oliveirastored in an 'unsigned char' type. Considering that the preemption control
95d57aff24SDaniel Bristot de Oliveirais a per-cpu behavior, the monitor declaration in the 'wip.c' file is::
96d57aff24SDaniel Bristot de Oliveira
97d57aff24SDaniel Bristot de Oliveira  DECLARE_DA_MON_PER_CPU(wip, unsigned char);
98d57aff24SDaniel Bristot de Oliveira
99d57aff24SDaniel Bristot de OliveiraThe monitor is executed by sending events to be processed via the functions
100d57aff24SDaniel Bristot de Oliveirapresented below::
101d57aff24SDaniel Bristot de Oliveira
102d57aff24SDaniel Bristot de Oliveira  da_handle_event_$(MONITOR_NAME)($(event from event enum));
103d57aff24SDaniel Bristot de Oliveira  da_handle_start_event_$(MONITOR_NAME)($(event from event enum));
104d57aff24SDaniel Bristot de Oliveira  da_handle_start_run_event_$(MONITOR_NAME)($(event from event enum));
105d57aff24SDaniel Bristot de Oliveira
106d57aff24SDaniel Bristot de OliveiraThe function ``da_handle_event_$(MONITOR_NAME)()`` is the regular case where
107d57aff24SDaniel Bristot de Oliveirathe event will be processed if the monitor is processing events.
108d57aff24SDaniel Bristot de Oliveira
109d57aff24SDaniel Bristot de OliveiraWhen a monitor is enabled, it is placed in the initial state of the automata.
110d57aff24SDaniel Bristot de OliveiraHowever, the monitor does not know if the system is in the *initial state*.
111d57aff24SDaniel Bristot de Oliveira
112d57aff24SDaniel Bristot de OliveiraThe ``da_handle_start_event_$(MONITOR_NAME)()`` function is used to notify the
113d57aff24SDaniel Bristot de Oliveiramonitor that the system is returning to the initial state, so the monitor can
114d57aff24SDaniel Bristot de Oliveirastart monitoring the next event.
115d57aff24SDaniel Bristot de Oliveira
116d57aff24SDaniel Bristot de OliveiraThe ``da_handle_start_run_event_$(MONITOR_NAME)()`` function is used to notify
117d57aff24SDaniel Bristot de Oliveirathe monitor that the system is known to be in the initial state, so the
118d57aff24SDaniel Bristot de Oliveiramonitor can start monitoring and monitor the current event.
119d57aff24SDaniel Bristot de Oliveira
120d57aff24SDaniel Bristot de OliveiraUsing the wip model as example, the events "preempt_disable" and
121d57aff24SDaniel Bristot de Oliveira"sched_waking" should be sent to monitor, respectively, via [2]::
122d57aff24SDaniel Bristot de Oliveira
123d57aff24SDaniel Bristot de Oliveira  da_handle_event_wip(preempt_disable_wip);
124d57aff24SDaniel Bristot de Oliveira  da_handle_event_wip(sched_waking_wip);
125d57aff24SDaniel Bristot de Oliveira
126d57aff24SDaniel Bristot de OliveiraWhile the event "preempt_enabled" will use::
127d57aff24SDaniel Bristot de Oliveira
128d57aff24SDaniel Bristot de Oliveira  da_handle_start_event_wip(preempt_enable_wip);
129d57aff24SDaniel Bristot de Oliveira
130d57aff24SDaniel Bristot de OliveiraTo notify the monitor that the system will be returning to the initial state,
131d57aff24SDaniel Bristot de Oliveiraso the system and the monitor should be in sync.
132d57aff24SDaniel Bristot de Oliveira
133d57aff24SDaniel Bristot de OliveiraFinal remarks
134d57aff24SDaniel Bristot de Oliveira-------------
135d57aff24SDaniel Bristot de Oliveira
136d57aff24SDaniel Bristot de OliveiraWith the monitor synthesis in place using the rv/da_monitor.h and
137d57aff24SDaniel Bristot de Oliveiradot2k, the developer's work should be limited to the instrumentation
138d57aff24SDaniel Bristot de Oliveiraof the system, increasing the confidence in the overall approach.
139d57aff24SDaniel Bristot de Oliveira
140d57aff24SDaniel Bristot de Oliveira[1] For details about deterministic automata format and the translation
141d57aff24SDaniel Bristot de Oliveirafrom one representation to another, see::
142d57aff24SDaniel Bristot de Oliveira
143d57aff24SDaniel Bristot de Oliveira  Documentation/trace/rv/deterministic_automata.rst
144d57aff24SDaniel Bristot de Oliveira
145d57aff24SDaniel Bristot de Oliveira[2] dot2k appends the monitor's name suffix to the events enums to
146d57aff24SDaniel Bristot de Oliveiraavoid conflicting variables when exporting the global vmlinux.h
147d57aff24SDaniel Bristot de Oliveirause by BPF programs.
148