From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH] test: self: add setjmp/longjmp/initjmp test
Date: Mon, 5 Jun 2023 08:27:55 +0200 [thread overview]
Message-ID: <20230605062755.3993563-1-a.fatoum@pengutronix.de> (raw)
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@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
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@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
next reply other threads:[~2023-06-05 6:29 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-05 6:27 Ahmad Fatoum [this message]
2023-06-05 13:44 ` Sascha Hauer
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=20230605062755.3993563-1-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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