linux/include/kunit/try-catch.h
Mickaël Salaün 3a35c13007 kunit: Handle test faults
Previously, when a kernel test thread crashed (e.g. NULL pointer
dereference, general protection fault), the KUnit test hanged for 30
seconds and exited with a timeout error.

Fix this issue by waiting on task_struct->vfork_done instead of the
custom kunit_try_catch.try_completion, and track the execution state by
initially setting try_result with -EINTR and only setting it to 0 if
the test passed.

Fix kunit_generic_run_threadfn_adapter() signature by returning 0
instead of calling kthread_complete_and_exit().  Because thread's exit
code is never checked, always set it to 0 to make it clear.  To make
this explicit, export kthread_exit() for KUnit tests built as module.

Fix the -EINTR error message, which couldn't be reached until now.

This is tested with a following patch.

Cc: Brendan Higgins <brendanhiggins@google.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: David Gow <davidgow@google.com>
Tested-by: Rae Moar <rmoar@google.com>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
Link: https://lore.kernel.org/r/20240408074625.65017-5-mic@digikod.net
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2024-05-06 14:22:02 -06:00

63 lines
2.2 KiB
C

/* SPDX-License-Identifier: GPL-2.0 */
/*
* An API to allow a function, that may fail, to be executed, and recover in a
* controlled manner.
*
* Copyright (C) 2019, Google LLC.
* Author: Brendan Higgins <brendanhiggins@google.com>
*/
#ifndef _KUNIT_TRY_CATCH_H
#define _KUNIT_TRY_CATCH_H
#include <linux/types.h>
typedef void (*kunit_try_catch_func_t)(void *);
struct kunit;
/**
* struct kunit_try_catch - provides a generic way to run code which might fail.
* @test: The test case that is currently being executed.
* @try_result: Contains any errno obtained while running test case.
* @try: The function, the test case, to attempt to run.
* @catch: The function called if @try bails out.
* @context: used to pass user data to the try and catch functions.
*
* kunit_try_catch provides a generic, architecture independent way to execute
* an arbitrary function of type kunit_try_catch_func_t which may bail out by
* calling kunit_try_catch_throw(). If kunit_try_catch_throw() is called, @try
* is stopped at the site of invocation and @catch is called.
*
* struct kunit_try_catch provides a generic interface for the functionality
* needed to implement kunit->abort() which in turn is needed for implementing
* assertions. Assertions allow stating a precondition for a test simplifying
* how test cases are written and presented.
*
* Assertions are like expectations, except they abort (call
* kunit_try_catch_throw()) when the specified condition is not met. This is
* useful when you look at a test case as a logical statement about some piece
* of code, where assertions are the premises for the test case, and the
* conclusion is a set of predicates, rather expectations, that must all be
* true. If your premises are violated, it does not makes sense to continue.
*/
struct kunit_try_catch {
/* private: internal use only. */
struct kunit *test;
int try_result;
kunit_try_catch_func_t try;
kunit_try_catch_func_t catch;
void *context;
};
void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context);
void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch);
static inline int kunit_try_catch_get_result(struct kunit_try_catch *try_catch)
{
return try_catch->try_result;
}
#endif /* _KUNIT_TRY_CATCH_H */