On Tue, 25 Feb 2025 at 18:35, Jerome Forissier
<jerome.foriss...@linaro.org> wrote:
>
> Test the initjmp() function when HAVE_INITJMP is set.
>
> Signed-off-by: Jerome Forissier <jerome.foriss...@linaro.org>
> ---
>  test/lib/Makefile  |  1 +
>  test/lib/initjmp.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 73 insertions(+)
>  create mode 100644 test/lib/initjmp.c
>
> diff --git a/test/lib/Makefile b/test/lib/Makefile
> index 0e4cb8e3dfd..bf04685dae1 100644
> --- a/test/lib/Makefile
> +++ b/test/lib/Makefile
> @@ -14,6 +14,7 @@ obj-y += hexdump.o
>  obj-$(CONFIG_SANDBOX) += kconfig.o
>  obj-y += lmb.o
>  obj-$(CONFIG_HAVE_SETJMP) += longjmp.o
> +obj-$(CONFIG_HAVE_INITJMP) += initjmp.o
>  obj-$(CONFIG_CONSOLE_RECORD) += test_print.o
>  obj-$(CONFIG_SSCANF) += sscanf.o
>  obj-$(CONFIG_$(PHASE_)STRTO) += str.o
> diff --git a/test/lib/initjmp.c b/test/lib/initjmp.c
> new file mode 100644
> index 00000000000..52bbda8cc60
> --- /dev/null
> +++ b/test/lib/initjmp.c
> @@ -0,0 +1,72 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright 2025 Linaro Limited
> + *
> + * Unit test for initjmp()
> + */
> +
> +#include <compiler.h>
> +#include <asm/setjmp.h>
> +#include <stdbool.h>
> +#include <test/lib.h>
> +#include <test/ut.h>
> +#include <vsprintf.h>
> +
> +static bool ep_entered;
> +static jmp_buf return_buf;
> +
> +static void __noreturn entrypoint(void)
> +{
> +       ep_entered = true;
> +
> +       /* Jump back to the main routine */
> +       longjmp(return_buf, 1);
> +
> +       /* Not reached */
> +       panic("longjmp failed\n");
> +}
> +
> +static int lib_initjmp(struct unit_test_state *uts)
> +{
> +       int ret;
> +       void *stack;
> +       jmp_buf buf;
> +       int stack_sz = 1024;
> +
> +       ep_entered = false;
> +
> +       stack = malloc(stack_sz);
> +       ut_assertnonnull(stack);
> +
> +       ut_assert(!ep_entered);
> +
> +       /*
> +        * Prepare return_buf so that entrypoint may jump back just after the
> +        * if()
> +        */
> +       if (!setjmp(return_buf)) {
> +               /* return_buf initialized, entrypoint not yet called */
> +
> +               /*
> +                * Prepare another jump buffer to jump into entrypoint with 
> the
> +                * given stack
> +                */
> +               ret = initjmp(buf, entrypoint, stack + stack_sz);
> +               ut_assertok(ret);
> +
> +               /* Jump into entrypoint */
> +               longjmp(buf, 1);
> +               /*
> +                * Not reached since entrypoint is expected to branch after
> +                * the if()
> +                */
> +               ut_assert(false);
> +       }
> +
> +       ut_assert(ep_entered);
> +
> +       free(stack);
> +
> +       return 0;
> +}
> +LIB_TEST(lib_initjmp, 0);
> --
> 2.43.0
>

Reviewed-by: Ilias Apalodimas <ilias.apalodi...@linaro.org>

Reply via email to