From: Sean Anderson <seanga2@gmail.com>
To: u-boot@lists.denx.de
Subject: [PATCH v2 2/2] test: unit test for longjmp
Date: Tue, 23 Mar 2021 09:30:10 -0400 [thread overview]
Message-ID: <f658d29f-6108-3fa4-9ab8-ed960f24da5a@gmail.com> (raw)
In-Reply-To: <3962b81e-edc2-0e46-055f-6c144d87a08f@gmx.de>
On 3/22/21 12:42 PM, Heinrich Schuchardt wrote:
> On 22.03.21 14:30, Sean Anderson wrote:
>>
>> On 3/22/21 9:23 AM, Sean Anderson wrote:
>>>
>>> On 3/22/21 7:02 AM, Heinrich Schuchardt wrote:
>>>> Provide a unit test for the longjmp() library function
>>>>
>>>> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
>>>> ---
>>>> v2:
>>>> ??? no change
>>>> ---
>>>> ? test/lib/Makefile? |? 1 +
>>>> ? test/lib/longjmp.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
>>>> ? 2 files changed, 45 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..7571540ffc
>>>> --- /dev/null
>>>> +++ b/test/lib/longjmp.c
>>>> @@ -0,0 +1,44 @@
>>>> +// 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>
>>>> +
>>>> +/**
>>>> + * test_longjmp_ret() - get longjmp() return value
>>>> + *
>>>> + * @i:??????? value passed to longjmp()
>>>> + * Return:??? value returned by longjmp()
>>>> + */
>>>> +int test_longjmp_ret(int i)
>>>> +{
>>>> +??? jmp_buf env;
>>>> +??? int ret;
>>>> +
>>>> +??? ret = setjmp(env);
>>>> +??? if (ret)
>>>> +??????? return ret;
>>>> +??? 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, test_longjmp_ret(i));
>>>> +??? ut_asserteq(1, test_longjmp_ret(0));
>>>> +??? for (i = 1; i < 4; ++i)
>>>> +??????? ut_asserteq(i, test_longjmp_ret(i));
>>>> +??? return 0;
>>>> +}
>>>> +LIB_TEST(lib_test_longjmp, 0);
>>>> --
>>>> 2.30.2
>>>>
>>>
>>> Reviewed-by: Sean Anderson <seanga2@gmail.com>
>>> Tested-by: Sean Anderson <seanga2@gmail.com>
>>>
>>> Though I would like to test that variables are set correctly e.g. by
>>> doing
>>>
>>> int test_longjmp_ret(int i)
>>> {
>>> ???? jmp_buf env;
>>> ???? int ret;
>>>
>>> ???? ret = setjmp(env);
>>> ???? if (ret)
>>> ???????? return ret;
>>> ???? ret = 0x1000;
>>> ???? longjmp(env, i);
>>> ???? /* We should not arrive here */
>>> ???? return ret;
>>> }
>>>
>>> --Sean
>>
>> err, rather by doing
>>
>> int test_longjmp_ret(int i)
>> {
>> ???? jmp_buf env;
>> ???? int ret;
>> ???? int foo = i;
>>
>> ???? ret = setjmp(env);
>> ???? if (ret)
>> ???????? return foo;
>> ???? foo = 0x1000;
>> ???? longjmp(env, i);
>> ???? /* We should not arrive here */
>> ???? return foo;
>> }
>>
>> or something else which demonstrates that variables get reset to their
>> earlier values.
>>
>> --Sean
>
> Hello Sean,
>
> thank you for reviewing.
>
> Would the following make sense to you to check that the stack pointer is
> correctly restored?
>
>
> struct test_jmp_buf {
> jmp_buf env;
> 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 byby longjmp()
nit: by
> */
> 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;
> }
>
> Best regards
>
> Heinrich
>
Yes, this looks good.
--Sean
next prev parent reply other threads:[~2021-03-23 13:30 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-22 11:02 [PATCH v2 0/2] riscv: simplify longjmp Heinrich Schuchardt
2021-03-22 11:02 ` [PATCH v2 1/2] " Heinrich Schuchardt
2021-03-22 12:53 ` Sean Anderson
2021-03-23 11:54 ` Leo Liang
2021-03-22 11:02 ` [PATCH v2 2/2] test: unit test for longjmp Heinrich Schuchardt
2021-03-22 13:23 ` Sean Anderson
2021-03-22 13:30 ` Sean Anderson
2021-03-22 16:42 ` Heinrich Schuchardt
2021-03-23 13:30 ` Sean Anderson [this message]
2021-03-24 9:18 ` Andreas Schwab
2021-03-24 11:16 ` Heinrich Schuchardt
2021-03-24 12:39 ` Andreas Schwab
2021-03-24 13:28 ` Heinrich Schuchardt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f658d29f-6108-3fa4-9ab8-ed960f24da5a@gmail.com \
--to=seanga2@gmail.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox