[PATCH] test: self: add setjmp/longjmp/initjmp test

Sascha Hauer sha at pengutronix.de
Mon Jun 5 06:44:51 PDT 2023


On Mon, Jun 05, 2023 at 08:27:55AM +0200, Ahmad Fatoum wrote:
> We depend on sjlj for OP-TEE early loading on ARM32 as well as for
> the bthread infrastructure, which powers the state machine behind the
> non-blocking USB mass storage gadget implementation.
> 
> Let's add a simple test to verify everything continues to work as
> expected.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
> ---
>  test/self/Kconfig  |  5 +++
>  test/self/Makefile |  1 +
>  test/self/setjmp.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 104 insertions(+)
>  create mode 100644 test/self/setjmp.c

Applied, thanks

Sascha

> 
> diff --git a/test/self/Kconfig b/test/self/Kconfig
> index d1ca6a701df3..dcbfa85896f7 100644
> --- a/test/self/Kconfig
> +++ b/test/self/Kconfig
> @@ -40,6 +40,7 @@ config SELFTEST_ENABLE_ALL
>  	imply SELFTEST_MMU
>  	imply SELFTEST_REGULATOR
>  	imply SELFTEST_STRING
> +	imply SELFTEST_SETJMP
>  	help
>  	  Selects all self-tests compatible with current configuration
>  
> @@ -87,4 +88,8 @@ config SELFTEST_STRING
>  	bool "String library selftest"
>  	select VERSION_CMP
>  
> +config SELFTEST_SETJMP
> +	bool "setjmp/longjmp library selftest"
> +	depends on ARCH_HAS_SJLJ
> +
>  endif
> diff --git a/test/self/Makefile b/test/self/Makefile
> index a66f34671e5a..b6f4395147dd 100644
> --- a/test/self/Makefile
> +++ b/test/self/Makefile
> @@ -12,6 +12,7 @@ obj-$(CONFIG_SELFTEST_JSON) += json.o
>  obj-$(CONFIG_SELFTEST_DIGEST) += digest.o
>  obj-$(CONFIG_SELFTEST_MMU) += mmu.o
>  obj-$(CONFIG_SELFTEST_STRING) += string.o
> +obj-$(CONFIG_SELFTEST_SETJMP) += setjmp.o
>  
>  clean-files := *.dtb *.dtb.S .*.dtc .*.pre .*.dts *.dtb.z
>  clean-files += *.dtbo *.dtbo.S .*.dtso
> diff --git a/test/self/setjmp.c b/test/self/setjmp.c
> new file mode 100644
> index 000000000000..1cbb44bdf941
> --- /dev/null
> +++ b/test/self/setjmp.c
> @@ -0,0 +1,98 @@
> +// SPDX-License-Identifier: MIT
> +// SPDX-FileCopyrightText: Copyright (C) 2003 Hewlett-Packard
> +/*
> + * Code contributed by David Mosberger-Tang <davidm at hpl.hp.com>
> + * to libunwind - a platform-independent unwind library
> + */
> +
> +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
> +
> +#include <common.h>
> +#include <bselftest.h>
> +#include <string.h>
> +#include <asm/setjmp.h>
> +
> +BSELFTEST_GLOBALS();
> +
> +static __noreturn void raise_longjmp(jmp_buf jbuf, int i, int n)
> +{
> +	while (i < n)
> +		raise_longjmp(jbuf, i + 1, n);
> +
> +	longjmp(jbuf, n);
> +}
> +
> +static jmp_buf jbuf;
> +
> +static void __noreturn initjmp_entry(void)
> +{
> +	volatile u32 arr[256];
> +	int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(arr); i++)
> +		writel(i, &arr[i]);
> +
> +	/* ensure arr[] is allocated on stack */
> +	OPTIMIZER_HIDE_VAR(i);
> +	if (i == 0)
> +		initjmp_entry();
> +
> +	longjmp(jbuf, 0x1337);
> +}
> +
> +static void test_setjmp(void)
> +{
> +	void *stack;
> +	jmp_buf ijbuf;
> +	volatile int i;
> +	int ret;
> +
> +	total_tests += 20;
> +
> +	for (i = 0; i < 10; ++i) {
> +		if ((ret = setjmp(jbuf))) {
> +			pr_debug("%s: secondary setjmp() return, ret=%d\n",
> +				 __func__, ret);
> +
> +			if (ret != i + 1) {
> +				printf("%s: setjmp() returned %d, expected %d\n",
> +					 __func__, ret, i + 1);
> +				failed_tests += 2;
> +			}
> +			continue;
> +		}
> +
> +		pr_debug("%s.%d: done with setjmp(); calling children\n",
> +			 __func__, i + 1);
> +
> +		raise_longjmp(jbuf, 0, i + 1);
> +
> +		printf("%s: raise_longjmp() returned unexpectedly\n", __func__);
> +		failed_tests++;
> +	}
> +
> +	stack = memalign(16, CONFIG_STACK_SIZE);
> +	if (WARN_ON(!stack)) {
> +		skipped_tests++;
> +		return;
> +	}
> +
> +	pr_debug("%s: testing initjmp\n", __func__);
> +
> +	total_tests += 1;
> +
> +	/* initialize new context with fresh stack */
> +	initjmp(ijbuf, initjmp_entry, stack + CONFIG_STACK_SIZE);
> +
> +	switch (setjmp(jbuf)) {
> +	case 0x1337:
> +		break;
> +	case 0:
> +		longjmp(ijbuf, 0x42);
> +	default:
> +		failed_tests++;
> +	}
> +
> +	free(stack);
> +}
> +bselftest(core, test_setjmp);
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list