Skip to content

Commit 09ecd8b

Browse files
Daniel Bristot de Oliveirarostedt
authored andcommitted
rv/include: Add helper functions for deterministic automata
Formally, a deterministic automaton, denoted by G, is defined as a quintuple: G = { X, E, f, x_0, X_m } where: - X is the set of states; - E is the finite set of events; - x_0 is the initial state; - X_m (subset of X) is the set of marked states. - f : X x E -> X $ is the transition function. It defines the state transition in the occurrence of a event from E in the state X. In the special case of deterministic automata, the occurrence of the event in E in a state in X has a deterministic next state from X. An automaton can also be represented using a graphical format of vertices (nodes) and edges. The open-source tool Graphviz can produce this graphic format using the (textual) DOT language as the source code. The dot2c tool presented in this paper: De Oliveira, Daniel Bristot; Cucinotta, Tommaso; De Oliveira, Romulo Silva. Efficient formal verification for the Linux kernel. In: International Conference on Software Engineering and Formal Methods. Springer, Cham, 2019. p. 315-332. Translates a deterministic automaton in the DOT format into a C source code representation that to be used for monitoring. This header file implements helper functions to facilitate the usage of the C output from dot2c/k for monitoring. Link: https://lkml.kernel.org/r/563234f2bfa84b540f60cf9e39c2d9f0eea95a55.1659052063.git.bristot@kernel.org Cc: Wim Van Sebroeck <[email protected]> Cc: Guenter Roeck <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Will Deacon <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Marco Elver <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: "Paul E. McKenney" <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Gabriele Paoloni <[email protected]> Cc: Juri Lelli <[email protected]> Cc: Clark Williams <[email protected]> Cc: Tao Zhou <[email protected]> Cc: Randy Dunlap <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Signed-off-by: Daniel Bristot de Oliveira <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 04acadc commit 09ecd8b

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

include/rv/automata.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira <[email protected]>
4+
*
5+
* Deterministic automata helper functions, to be used with the automata
6+
* models in C generated by the dot2k tool.
7+
*/
8+
9+
/*
10+
* DECLARE_AUTOMATA_HELPERS - define a set of helper functions for automata
11+
*
12+
* Define a set of helper functions for automata. The 'name' argument is used
13+
* as suffix for the functions and data. These functions will handle automaton
14+
* with data type 'type'.
15+
*/
16+
#define DECLARE_AUTOMATA_HELPERS(name, type) \
17+
\
18+
/* \
19+
* model_get_state_name_##name - return the (string) name of the given state \
20+
*/ \
21+
static char *model_get_state_name_##name(enum states_##name state) \
22+
{ \
23+
if ((state < 0) || (state >= state_max_##name)) \
24+
return "INVALID"; \
25+
\
26+
return automaton_##name.state_names[state]; \
27+
} \
28+
\
29+
/* \
30+
* model_get_event_name_##name - return the (string) name of the given event \
31+
*/ \
32+
static char *model_get_event_name_##name(enum events_##name event) \
33+
{ \
34+
if ((event < 0) || (event >= event_max_##name)) \
35+
return "INVALID"; \
36+
\
37+
return automaton_##name.event_names[event]; \
38+
} \
39+
\
40+
/* \
41+
* model_get_initial_state_##name - return the automaton's initial state \
42+
*/ \
43+
static inline type model_get_initial_state_##name(void) \
44+
{ \
45+
return automaton_##name.initial_state; \
46+
} \
47+
\
48+
/* \
49+
* model_get_next_state_##name - process an automaton event occurrence \
50+
* \
51+
* Given the current state (curr_state) and the event (event), returns \
52+
* the next state, or INVALID_STATE in case of error. \
53+
*/ \
54+
static inline type model_get_next_state_##name(enum states_##name curr_state, \
55+
enum events_##name event) \
56+
{ \
57+
if ((curr_state < 0) || (curr_state >= state_max_##name)) \
58+
return INVALID_STATE; \
59+
\
60+
if ((event < 0) || (event >= event_max_##name)) \
61+
return INVALID_STATE; \
62+
\
63+
return automaton_##name.function[curr_state][event]; \
64+
} \
65+
\
66+
/* \
67+
* model_is_final_state_##name - check if the given state is a final state \
68+
*/ \
69+
static inline bool model_is_final_state_##name(enum states_##name state) \
70+
{ \
71+
if ((state < 0) || (state >= state_max_##name)) \
72+
return 0; \
73+
\
74+
return automaton_##name.final_states[state]; \
75+
}

0 commit comments

Comments
 (0)