Revision c66650d29764e228eba40b7a59fdb70fa6567daa authored by Kees Cook on 22 September 2023, 17:53:19 UTC, committed by Dan Williams on 22 September 2023, 21:31:04 UTC
Prepare for the coming implementation by GCC and Clang of the __counted_by
attribute. Flexible array members annotated with __counted_by can have
their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS
(for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family
functions).

As found with Coccinelle[1], add __counted_by for struct cxl_cxims_data.
Additionally, since the element count member must be set before accessing
the annotated flexible array member, move its initialization earlier.

[1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci

Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Jonathan Cameron <jonathan.cameron@huawei.com>
Cc: Dave Jiang <dave.jiang@intel.com>
Cc: Alison Schofield <alison.schofield@intel.com>
Cc: Vishal Verma <vishal.l.verma@intel.com>
Cc: Ira Weiny <ira.weiny@intel.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: linux-cxl@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Vishal Verma <vishal.l.verma@intel.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Link: https://lore.kernel.org/r/20230922175319.work.096-kees@kernel.org
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
1 parent a76b625
Raw File
automata.h
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira  <bristot@kernel.org>
 *
 * Deterministic automata helper functions, to be used with the automata
 * models in C generated by the dot2k tool.
 */

/*
 * DECLARE_AUTOMATA_HELPERS - define a set of helper functions for automata
 *
 * Define a set of helper functions for automata. The 'name' argument is used
 * as suffix for the functions and data. These functions will handle automaton
 * with data type 'type'.
 */
#define DECLARE_AUTOMATA_HELPERS(name, type)					\
										\
/*										\
 * model_get_state_name_##name - return the (string) name of the given state	\
 */ 										\
static char *model_get_state_name_##name(enum states_##name state)		\
{										\
	if ((state < 0) || (state >= state_max_##name))				\
		return "INVALID";						\
										\
	return automaton_##name.state_names[state];				\
}										\
										\
/*										\
 * model_get_event_name_##name - return the (string) name of the given event	\
 */										\
static char *model_get_event_name_##name(enum events_##name event)		\
{										\
	if ((event < 0) || (event >= event_max_##name))				\
		return "INVALID";						\
										\
	return automaton_##name.event_names[event];				\
}										\
										\
/*										\
 * model_get_initial_state_##name - return the automaton's initial state		\
 */										\
static inline type model_get_initial_state_##name(void)				\
{										\
	return automaton_##name.initial_state;					\
}										\
										\
/*										\
 * model_get_next_state_##name - process an automaton event occurrence		\
 *										\
 * Given the current state (curr_state) and the event (event), returns		\
 * the next state, or INVALID_STATE in case of error.				\
 */										\
static inline type model_get_next_state_##name(enum states_##name curr_state,	\
					       enum events_##name event)	\
{										\
	if ((curr_state < 0) || (curr_state >= state_max_##name))		\
		return INVALID_STATE;						\
										\
	if ((event < 0) || (event >= event_max_##name))				\
		return INVALID_STATE;						\
										\
	return automaton_##name.function[curr_state][event];			\
}										\
										\
/*										\
 * model_is_final_state_##name - check if the given state is a final state	\
 */										\
static inline bool model_is_final_state_##name(enum states_##name state)	\
{										\
	if ((state < 0) || (state >= state_max_##name))				\
		return 0;							\
										\
	return automaton_##name.final_states[state];				\
}
back to top