linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Berg <benjamin@sipsolutions.net>
To: "Willy Tarreau" <w@1wt.eu>,
	"Thomas Weißschuh" <linux@weissschuh.net>,
	linux-kselftest@vger.kernel.org
Cc: Benjamin Berg <benjamin.berg@intel.com>
Subject: [PATCH v2 2/4] selftests/nolibc: validate order of constructor calls
Date: Thu, 10 Jul 2025 12:39:48 +0200	[thread overview]
Message-ID: <20250710103950.1272379-3-benjamin@sipsolutions.net> (raw)
In-Reply-To: <20250710103950.1272379-1-benjamin@sipsolutions.net>

From: Benjamin Berg <benjamin.berg@intel.com>

Add new helpers to track multiple steps as bits in an integer. Store
each step in a bit and use the lowest bit to store whether all steps
occurred in the correct order and only once.

Use this for the constructor tests.

Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>

---

v2:
- Newly added patch
---
 .../selftests/nolibc/nolibc-test-linkage.c    | 17 ++++++++--
 tools/testing/selftests/nolibc/nolibc-test.c  | 31 ++++++++++++++++---
 2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/nolibc/nolibc-test-linkage.c b/tools/testing/selftests/nolibc/nolibc-test-linkage.c
index 0636d1b6e808..4435f389570b 100644
--- a/tools/testing/selftests/nolibc/nolibc-test-linkage.c
+++ b/tools/testing/selftests/nolibc/nolibc-test-linkage.c
@@ -4,6 +4,19 @@
 
 #include <errno.h>
 
+/*
+ * Set BIT(step + 1), BIT(0) shows whether all steps ran once and in order
+ *
+ * Copied from nolibc-test.c.
+ */
+#define MARK_STEP_DONE(val, step) do {					\
+	if ((val) == 0 && (step) == 0)					\
+		(val) |= 0x1;						\
+	else if (!(val & (1 << (step))) || (val) & (1 << ((step) + 1)))	\
+		(val) &= ~0x1;						\
+	(val) |= 1 << ((step) + 1);					\
+	} while (0)
+
 void *linkage_test_errno_addr(void)
 {
 	return &errno;
@@ -14,11 +27,11 @@ int linkage_test_constructor_test_value = 0;
 __attribute__((constructor))
 static void constructor1(void)
 {
-	linkage_test_constructor_test_value |= 1 << 0;
+	MARK_STEP_DONE(linkage_test_constructor_test_value, 0);
 }
 
 __attribute__((constructor))
 static void constructor2(void)
 {
-	linkage_test_constructor_test_value |= 1 << 1;
+	MARK_STEP_DONE(linkage_test_constructor_test_value, 1);
 }
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 97efc98b6a3d..d612150d2ea3 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -72,6 +72,15 @@ static const int is_nolibc =
 #endif
 ;
 
+/* Set BIT(step + 1), BIT(0) shows whether all steps ran once and in order */
+#define MARK_STEP_DONE(val, step) do {					\
+	if ((val) == 0 && (step) == 0)					\
+		(val) |= 0x1;						\
+	else if ((val) & (1 << ((step) + 1)) || !(val & (1 << (step))))	\
+		(val) &= ~0x1;						\
+	(val) |= 1 << ((step) + 1);					\
+	} while (0)
+
 /* definition of a series of tests */
 struct test {
 	const char *name;              /* test name */
@@ -389,6 +398,20 @@ int expect_syserr2(int expr, int expret, int experr1, int experr2, int llen)
 }
 
 
+#define EXPECT_STEPS(cond, expr, num_steps)			\
+	do { if (!(cond)) result(llen, SKIPPED); else ret += expect_steps(expr, llen, num_steps); } while (0)
+
+static __attribute__((unused))
+int expect_steps(uint64_t expr, int llen, int num_steps)
+{
+	int ret = !(expr == ((uint64_t)1 << (num_steps + 1)) - 1);
+
+	llen += printf(" = %llx ", (long long)expr);
+	result(llen, ret ? FAIL : OK);
+	return ret;
+}
+
+
 #define EXPECT_PTRZR(cond, expr)				\
 	do { if (!(cond)) result(llen, SKIPPED); else ret += expect_ptrzr(expr, llen); } while (0)
 
@@ -690,14 +713,14 @@ int expect_strtox(int llen, void *func, const char *input, int base, intmax_t ex
 __attribute__((constructor))
 static void constructor1(void)
 {
-	constructor_test_value |= 1 << 0;
+	MARK_STEP_DONE(constructor_test_value, 0);
 }
 
 __attribute__((constructor))
 static void constructor2(int argc, char **argv, char **envp)
 {
 	if (argc && argv && envp)
-		constructor_test_value |= 1 << 1;
+		MARK_STEP_DONE(constructor_test_value, 1);
 }
 
 int run_startup(int min, int max)
@@ -736,9 +759,9 @@ int run_startup(int min, int max)
 		CASE_TEST(environ_HOME);     EXPECT_PTRNZ(1, getenv("HOME")); break;
 		CASE_TEST(auxv_addr);        EXPECT_PTRGT(test_auxv != (void *)-1, test_auxv, brk); break;
 		CASE_TEST(auxv_AT_UID);      EXPECT_EQ(1, getauxval(AT_UID), getuid()); break;
-		CASE_TEST(constructor);      EXPECT_EQ(is_nolibc, constructor_test_value, 0x3); break;
+		CASE_TEST(constructor);      EXPECT_STEPS(is_nolibc, constructor_test_value, 2); break;
 		CASE_TEST(linkage_errno);    EXPECT_PTREQ(1, linkage_test_errno_addr(), &errno); break;
-		CASE_TEST(linkage_constr);   EXPECT_EQ(1, linkage_test_constructor_test_value, 0x3); break;
+		CASE_TEST(linkage_constr);   EXPECT_STEPS(1, linkage_test_constructor_test_value, 2); break;
 		case __LINE__:
 			return ret; /* must be last */
 		/* note: do not set any defaults so as to permit holes above */
-- 
2.50.0


  parent reply	other threads:[~2025-07-10 10:43 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-10 10:39 [PATCH v2 0/4] signal handling support for nolibc Benjamin Berg
2025-07-10 10:39 ` [PATCH v2 1/4] selftests/nolibc: fix EXPECT_NZ macro Benjamin Berg
2025-07-11  4:46   ` Thomas Weißschuh
2025-07-10 10:39 ` Benjamin Berg [this message]
2025-07-11  4:50   ` [PATCH v2 2/4] selftests/nolibc: validate order of constructor calls Thomas Weißschuh
2025-07-10 10:39 ` [PATCH v2 3/4] tools/nolibc: add more generic bitmask macros for FD_* Benjamin Berg
2025-07-10 10:39 ` [PATCH v2 4/4] tools/nolibc: add signal support Benjamin Berg
2025-07-11  5:40   ` Thomas Weißschuh
2025-07-11  7:25     ` Benjamin Berg
2025-07-13 15:02       ` Thomas Weißschuh
2025-07-13 15:50         ` Willy Tarreau
2025-07-14 12:52           ` Benjamin Berg
2025-07-14 13:33             ` Thomas Weißschuh

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=20250710103950.1272379-3-benjamin@sipsolutions.net \
    --to=benjamin@sipsolutions.net \
    --cc=benjamin.berg@intel.com \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=w@1wt.eu \
    /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;
as well as URLs for NNTP newsgroup(s).