* [PATCH v4 0/2] riscv: simplify longjmp
@ 2021-03-25 7:31 Heinrich Schuchardt
2021-03-25 7:31 ` [PATCH v4 1/2] " Heinrich Schuchardt
2021-03-25 7:31 ` [PATCH v4 2/2] test: unit test for longjmp Heinrich Schuchardt
0 siblings, 2 replies; 4+ messages in thread
From: Heinrich Schuchardt @ 2021-03-25 7:31 UTC (permalink / raw)
To: u-boot
The implementation of longjmp() is simplified.
A unit test for longjmp() is provided.
For testing use
CONFIG_UNIT_TEST=y
CONFIG_CMD_SETEXPR=n
and execute
ut lib
v4:
in unit test use volatile for variable changed between setjmp and
longjmp
v3:
improve unit test
v2:
correct title of patch 1
Heinrich Schuchardt (2):
riscv: simplify longjmp
test: unit test for longjmp
arch/riscv/lib/setjmp.S | 8 ++---
test/lib/Makefile | 1 +
test/lib/longjmp.c | 73 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 76 insertions(+), 6 deletions(-)
create mode 100644 test/lib/longjmp.c
--
2.30.2
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v4 1/2] riscv: simplify longjmp 2021-03-25 7:31 [PATCH v4 0/2] riscv: simplify longjmp Heinrich Schuchardt @ 2021-03-25 7:31 ` Heinrich Schuchardt 2021-03-25 7:31 ` [PATCH v4 2/2] test: unit test for longjmp Heinrich Schuchardt 1 sibling, 0 replies; 4+ messages in thread From: Heinrich Schuchardt @ 2021-03-25 7:31 UTC (permalink / raw) To: u-boot The value returned by setjmp must be nonzero. If zero is passed as parameter it must be replaced by 1. This patch reduces the code size a bit. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Reviewed-by: Sean Anderson <seanga2@gmail.com> Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com> --- v4: no change v3: no change v2: correct typo in title --- arch/riscv/lib/setjmp.S | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/arch/riscv/lib/setjmp.S b/arch/riscv/lib/setjmp.S index 72bc9241f6..99d6195827 100644 --- a/arch/riscv/lib/setjmp.S +++ b/arch/riscv/lib/setjmp.S @@ -54,12 +54,8 @@ ENTRY(longjmp) LOAD_IDX(sp, 13) /* Move the return value in place, but return 1 if passed 0. */ - beq a1, zero, longjmp_1 - mv a0, a1 - ret - - longjmp_1: - li a0, 1 + seqz a0, a1 + add a0, a0, a1 ret ENDPROC(longjmp) .popsection -- 2.30.2 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v4 2/2] test: unit test for longjmp 2021-03-25 7:31 [PATCH v4 0/2] riscv: simplify longjmp Heinrich Schuchardt 2021-03-25 7:31 ` [PATCH v4 1/2] " Heinrich Schuchardt @ 2021-03-25 7:31 ` Heinrich Schuchardt 2021-03-26 1:45 ` Leo Liang 1 sibling, 1 reply; 4+ messages in thread From: Heinrich Schuchardt @ 2021-03-25 7:31 UTC (permalink / raw) To: u-boot Provide a unit test for the longjmp() library function Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> ---- v4: use volatile for variable changed between setjmp and longjmp v3: check variable on stack v2: no change --- test/lib/Makefile | 1 + test/lib/longjmp.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 test/lib/longjmp.c diff --git a/test/lib/Makefile b/test/lib/Makefile index 97c11e35a8..a30f615aa9 100644 --- a/test/lib/Makefile +++ b/test/lib/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_EFI_LOADER) += efi_device_path.o obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o obj-y += hexdump.o obj-y += lmb.o +obj-y += longjmp.o obj-$(CONFIG_CONSOLE_RECORD) += test_print.o obj-$(CONFIG_SSCANF) += sscanf.o obj-y += string.o diff --git a/test/lib/longjmp.c b/test/lib/longjmp.c new file mode 100644 index 0000000000..bd1fe1aff6 --- /dev/null +++ b/test/lib/longjmp.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Test setjmp(), longjmp() + * + * Copyright (c) 2021, Heinrich Schuchardt <xypron.glpk@gmx.de> + */ + +#include <common.h> +#include <test/lib.h> +#include <test/test.h> +#include <test/ut.h> +#include <asm/setjmp.h> + +struct test_jmp_buf { + jmp_buf env; + /* + * Auto-variables changed between setjmp() and longjmp() must be + * static or volatile. Otherwise the result is undefined. + */ + volatile int val; +}; + +/** + * test_longjmp() - test longjmp function + * + * @i is passed to longjmp. + * @i << 8 is set in the environment structure. + * + * @env: environment + * @i: value passed to longjmp() + */ +static void noinline test_longjmp(struct test_jmp_buf *env, int i) +{ + env->val = i << 8; + longjmp(env->env, i); +} + +/** + * test_setjmp() - test setjmp function + * + * setjmp() will return the value @i passed to longjmp() if @i is non-zero. + * For @i == 0 we expect return value 1. + * + * @i << 8 will be set by test_longjmp in the environment structure. + * This value can be used to check that the stack frame is restored. + * + * We return the XORed values to allow simply check both@once. + * + * @i: value passed to longjmp() + * Return: values return by longjmp() + */ +static int test_setjmp(int i) +{ + struct test_jmp_buf env; + int ret; + + env.val = -1; + ret = setjmp(env.env); + if (ret) + return ret ^ env.val; + test_longjmp(&env, i); + /* We should not arrive here */ + return 0x1000; +} + +static int lib_test_longjmp(struct unit_test_state *uts) +{ + int i; + + for (i = -3; i < 0; ++i) + ut_asserteq(i ^ (i << 8) , test_setjmp(i)); + ut_asserteq(1, test_setjmp(0)); + for (i = 1; i < 4; ++i) + ut_asserteq(i ^ (i << 8), test_setjmp(i)); + return 0; +} +LIB_TEST(lib_test_longjmp, 0); -- 2.30.2 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v4 2/2] test: unit test for longjmp 2021-03-25 7:31 ` [PATCH v4 2/2] test: unit test for longjmp Heinrich Schuchardt @ 2021-03-26 1:45 ` Leo Liang 0 siblings, 0 replies; 4+ messages in thread From: Leo Liang @ 2021-03-26 1:45 UTC (permalink / raw) To: u-boot On Thu, Mar 25, 2021 at 03:31:50PM +0800, Heinrich Schuchardt wrote: > Provide a unit test for the longjmp() library function > > Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> > ---- > v4: > use volatile for variable changed between setjmp and longjmp > v3: > check variable on stack > v2: > no change > --- > test/lib/Makefile | 1 + > test/lib/longjmp.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 78 insertions(+) > create mode 100644 test/lib/longjmp.c > > diff --git a/test/lib/Makefile b/test/lib/Makefile > index 97c11e35a8..a30f615aa9 100644 > --- a/test/lib/Makefile > +++ b/test/lib/Makefile > @@ -7,6 +7,7 @@ obj-$(CONFIG_EFI_LOADER) += efi_device_path.o > obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o > obj-y += hexdump.o > obj-y += lmb.o > +obj-y += longjmp.o > obj-$(CONFIG_CONSOLE_RECORD) += test_print.o > obj-$(CONFIG_SSCANF) += sscanf.o > obj-y += string.o > diff --git a/test/lib/longjmp.c b/test/lib/longjmp.c > new file mode 100644 > index 0000000000..bd1fe1aff6 > --- /dev/null > +++ b/test/lib/longjmp.c > @@ -0,0 +1,77 @@ > +// SPDX-License-Identifier: GPL-2.0+ > +/* > + * Test setjmp(), longjmp() > + * > + * Copyright (c) 2021, Heinrich Schuchardt <xypron.glpk@gmx.de> > + */ > + > +#include <common.h> > +#include <test/lib.h> > +#include <test/test.h> > +#include <test/ut.h> > +#include <asm/setjmp.h> > + > +struct test_jmp_buf { > + jmp_buf env; > + /* > + * Auto-variables changed between setjmp() and longjmp() must be > + * static or volatile. Otherwise the result is undefined. > + */ > + volatile int val; > +}; > + > +/** > + * test_longjmp() - test longjmp function > + * > + * @i is passed to longjmp. > + * @i << 8 is set in the environment structure. > + * > + * @env: environment > + * @i: value passed to longjmp() > + */ > +static void noinline test_longjmp(struct test_jmp_buf *env, int i) > +{ > + env->val = i << 8; > + longjmp(env->env, i); > +} > + > +/** > + * test_setjmp() - test setjmp function > + * > + * setjmp() will return the value @i passed to longjmp() if @i is non-zero. > + * For @i == 0 we expect return value 1. > + * > + * @i << 8 will be set by test_longjmp in the environment structure. > + * This value can be used to check that the stack frame is restored. > + * > + * We return the XORed values to allow simply check both at once. > + * > + * @i: value passed to longjmp() > + * Return: values return by longjmp() > + */ > +static int test_setjmp(int i) > +{ > + struct test_jmp_buf env; > + int ret; > + > + env.val = -1; > + ret = setjmp(env.env); > + if (ret) > + return ret ^ env.val; > + test_longjmp(&env, i); > + /* We should not arrive here */ > + return 0x1000; > +} > + > +static int lib_test_longjmp(struct unit_test_state *uts) > +{ > + int i; > + > + for (i = -3; i < 0; ++i) > + ut_asserteq(i ^ (i << 8) , test_setjmp(i)); > + ut_asserteq(1, test_setjmp(0)); > + for (i = 1; i < 4; ++i) > + ut_asserteq(i ^ (i << 8), test_setjmp(i)); > + return 0; > +} > +LIB_TEST(lib_test_longjmp, 0); > -- > 2.30.2 > Reviewed-by: Leo Yu-Chi Liang <ycliang@andestech.com> ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-03-26 1:45 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2021-03-25 7:31 [PATCH v4 0/2] riscv: simplify longjmp Heinrich Schuchardt 2021-03-25 7:31 ` [PATCH v4 1/2] " Heinrich Schuchardt 2021-03-25 7:31 ` [PATCH v4 2/2] test: unit test for longjmp Heinrich Schuchardt 2021-03-26 1:45 ` Leo Liang
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox