From: Danish Khateeb <danishkhateeb03@gmail.com>
To: "Willy Tarreau" <w@1wt.eu>, "Thomas Weißschuh" <linux@weissschuh.net>
Cc: Shuah Khan <shuah@kernel.org>,
Sven Schnelle <svens@linux.ibm.com>,
Benjamin Berg <benjamin.berg@intel.com>,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
Danish Khateeb <danishkhateeb03@gmail.com>
Subject: [PATCH v2 3/3] selftests/nolibc: test the FD_* macros
Date: Fri, 25 Sep 2026 16:16:00 -0500 [thread overview]
Message-ID: <20260925211600.119116-4-danishkhateeb03@gmail.com> (raw)
In-Reply-To: <20260925211600.119116-1-danishkhateeb03@gmail.com>
The only test that uses the FD_* macros passes fd 1, so nothing noticed
that they used the wrong bits for fds 31-63 of each word on 64-bit
architectures.
Check that FD_SET() and FD_CLR() change exactly one fd, for every fd
below FD_SETSIZE.
Assisted-by: LLM
Signed-off-by: Danish Khateeb <danishkhateeb03@gmail.com>
---
Notes:
v2:
- Dropped the select() test.
- Moved the FD_* test to the stdlib tests, with its helper in the same
order as the test cases.
- Shortened it: FD_CLR() is now checked on a set filled with memset().
v1: https://lore.kernel.org/all/20260924015222.31693-4-danishkhateeb03@gmail.com/
Tested with nolibc-test on x86_64 (GCC and clang), i386, arm, arm64 and
sparc64 (qemu-user), and against glibc. With the fix, fd_set passes
everywhere. Without it, fd_set fails on the 64-bit architectures (or
UBSan traps on the shift) and passes on the 32-bit ones, which were not
affected.
tools/testing/selftests/nolibc/nolibc-test.c | 21 ++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 37c11a7fce23..1995d67dcca6 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1841,6 +1841,26 @@ int test_time_types(void)
return 0;
}
+int test_fd_set(void)
+{
+ fd_set set, clr;
+ int fd, i;
+
+ /* FD_SET() and FD_CLR() must change exactly one fd */
+ for (fd = 0; fd < FD_SETSIZE; fd++) {
+ FD_ZERO(&set);
+ FD_SET(fd, &set);
+ memset(&clr, 0xff, sizeof(clr));
+ FD_CLR(fd, &clr);
+ for (i = 0; i < FD_SETSIZE; i++)
+ if (!!FD_ISSET(i, &set) != (i == fd) ||
+ !!FD_ISSET(i, &clr) != (i != fd))
+ return 1;
+ }
+
+ return 0;
+}
+
int test_malloc(void)
{
size_t sz_array1, sz_array2, sz_array3;
@@ -2022,6 +2042,7 @@ int run_stdlib(int min, int max)
CASE_TEST(memchr_foobar6_o); EXPECT_STREQ(1, memchr("foobar", 'o', 6), "oobar"); break;
CASE_TEST(memchr_foobar3_b); EXPECT_STRZR(1, memchr("foobar", 'b', 3)); break;
CASE_TEST(time_types); EXPECT_ZR(is_nolibc, test_time_types()); break;
+ CASE_TEST(fd_set); EXPECT_ZR(1, test_fd_set()); break;
CASE_TEST(makedev); EXPECT_EQ(1, makedev(0x12, 0x34), 0x1234); break;
CASE_TEST(major); EXPECT_EQ(1, major(0x1234), 0x12); break;
CASE_TEST(minor); EXPECT_EQ(1, minor(0x1234), 0x34); break;
--
2.55.0
prev parent reply other threads:[~2026-09-25 21:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-25 21:15 [PATCH v2 0/3] tools/nolibc: fix readdir_r() and the FD_* macros on 64-bit Danish Khateeb
2026-09-25 21:15 ` [PATCH v2 1/3] tools/nolibc: fix readdir_r() with 64-bit directory offsets Danish Khateeb
2026-09-25 21:15 ` [PATCH v2 2/3] tools/nolibc: fix FD_SET(), FD_CLR() and FD_ISSET() on 64-bit Danish Khateeb
2026-09-25 21:16 ` Danish Khateeb [this message]
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=20260925211600.119116-4-danishkhateeb03@gmail.com \
--to=danishkhateeb03@gmail.com \
--cc=benjamin.berg@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux@weissschuh.net \
--cc=shuah@kernel.org \
--cc=svens@linux.ibm.com \
--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
all inboxes | Powered by JetHome®