linux/Documentation/dev-tools/kunit
David Gow 56778b49c9 kunit: Add a macro to wrap a deferred action function
KUnit's deferred action API accepts a void(*)(void *) function pointer
which is called when the test is exited. However, we very frequently
want to use existing functions which accept a single pointer, but which
may not be of type void*. While this is probably dodgy enough to be on
the wrong side of the C standard, it's been often used for similar
callbacks, and gcc's -Wcast-function-type seems to ignore cases where
the only difference is the type of the argument, assuming it's
compatible (i.e., they're both pointers to data).

However, clang 16 has introduced -Wcast-function-type-strict, which no
longer permits any deviation in function pointer type. This seems to be
because it'd break CFI, which validates the type of function calls.

This rather ruins our attempts to cast functions to defer them, and
leaves us with a few options. The one we've chosen is to implement a
macro which will generate a wrapper function which accepts a void*, and
casts the argument to the appropriate type.

For example, if you were trying to wrap:
void foo_close(struct foo *handle);
you could use:
KUNIT_DEFINE_ACTION_WRAPPER(kunit_action_foo_close,
			    foo_close,
			    struct foo *);

This would create a new kunit_action_foo_close() function, of type
kunit_action_t, which could be passed into kunit_add_action() and
similar functions.

In addition to defining this macro, update KUnit and its tests to use
it.

Link: https://github.com/ClangBuiltLinux/linux/issues/1750
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Tested-by: Nathan Chancellor <nathan@kernel.org>
Acked-by: Daniel Vetter <daniel@ffwll.ch>
Reviewed-by: Maxime Ripard <mripard@kernel.org>
Signed-off-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2023-12-18 13:21:14 -07:00
..
api Documentation: Add Function Redirection API docs 2023-02-08 14:28:17 -07:00
architecture.rst Documentation: kunit: Rename references to kunit_abort() 2023-06-06 08:57:37 -06:00
faq.rst Documentation: Kunit: Add ref for other kinds of tests 2022-09-30 13:19:11 -06:00
index.rst Documentation: kunit: Fix "How Do I Use This" / "Next Steps" sections 2022-12-12 14:13:48 -07:00
kunit_suitememorydiagram.svg Documentation: KUnit: Added KUnit Architecture 2021-12-23 12:49:06 -07:00
run_manual.rst Documentation: kunit: Reorganize documentation related to running tests 2021-12-23 12:49:43 -07:00
run_wrapper.rst kunit: Add documentation of KUnit test attributes 2023-07-26 13:29:46 -06:00
running_tips.rst kunit: Add documentation of KUnit test attributes 2023-07-26 13:29:46 -06:00
start.rst Documentation: Kunit: add MODULE_LICENSE to sample code 2023-05-31 12:42:40 -06:00
style.rst Documentation: KUnit: Restyle Test Style and Nomenclature page 2021-12-23 12:49:43 -07:00
usage.rst kunit: Add a macro to wrap a deferred action function 2023-12-18 13:21:14 -07:00