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 3/3] selftests/nolibc: test the FD_* macros and select() on a high fd
Date: Wed, 23 Sep 2026 20:52:22 -0500 [thread overview]
Message-ID: <20260924015222.31693-4-danishkhateeb03@gmail.com> (raw)
In-Reply-To: <20260924015222.31693-1-danishkhateeb03@gmail.com>
The only select() test that passes an fd_set uses fd 1, so nothing
noticed that the FD_* macros used the wrong bits for fds 31-63 of each
word on 64-bit architectures.
Add a test that FD_SET() and FD_CLR() change exactly one fd, for every
fd below FD_SETSIZE, and one that select() reports a readable pipe
duplicated to fd 40, which also checks that the macros agree with the
kernel on the layout of the set.
Assisted-by: LLM
Signed-off-by: Danish Khateeb <danishkhateeb03@gmail.com>
---
tools/testing/selftests/nolibc/nolibc-test.c | 54 ++++++++++++++++++++
1 file changed, 54 insertions(+)
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index f62183a46205..d9a8bfd4b21b 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1369,6 +1369,58 @@ int test_pipe(void)
return !!memcmp(buf, msg, len);
}
+int test_fd_set(void)
+{
+ fd_set fds;
+ int fd, i;
+
+ for (fd = 0; fd < FD_SETSIZE; fd++) {
+ /* FD_SET() must set this fd and no other */
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ for (i = 0; i < FD_SETSIZE; i++)
+ if (!!FD_ISSET(i, &fds) != (i == fd))
+ return 1;
+
+ /* FD_CLR() must clear this fd and no other */
+ for (i = 0; i < FD_SETSIZE; i++)
+ FD_SET(i, &fds);
+ FD_CLR(fd, &fds);
+ for (i = 0; i < FD_SETSIZE; i++)
+ if (!!FD_ISSET(i, &fds) != (i != fd))
+ return 1;
+ }
+
+ return 0;
+}
+
+int test_select_high_fd(void)
+{
+ struct timeval tv = { 0 };
+ int pipefd[2], fd, ret = 1;
+ fd_set fds;
+
+ if (pipe(pipefd) == -1)
+ return 1;
+
+ /* fd 40 is in the upper half of a 64-bit fd_set word */
+ fd = dup2(pipefd[0], 40);
+ if (fd == -1)
+ goto out;
+
+ write(pipefd[1], "x", 1);
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ if (select(fd + 1, &fds, NULL, NULL, &tv) == 1 && FD_ISSET(fd, &fds))
+ ret = 0;
+
+ close(fd);
+out:
+ close(pipefd[0]);
+ close(pipefd[1]);
+ return ret;
+}
+
int test_rlimit(void)
{
struct rlimit rlim = {
@@ -1711,6 +1763,7 @@ int run_syscall(int min, int max)
CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0] = (char []){"/"}, [1] = NULL }, NULL), -1, EACCES); break;
CASE_TEST(fchdir_stdin); EXPECT_SYSER(1, fchdir(STDIN_FILENO), -1, ENOTDIR); break;
CASE_TEST(fchdir_badfd); EXPECT_SYSER(1, fchdir(-1), -1, EBADF); break;
+ CASE_TEST(fd_set); EXPECT_ZR(1, test_fd_set()); break;
CASE_TEST(fdopendir_notdir); EXPECT_SYSER(1, (uintptr_t)fdopendir(STDIN_FILENO), (uintptr_t)NULL, ENOTDIR); break;
CASE_TEST(file_stream); EXPECT_SYSZR(1, test_file_stream()); break;
CASE_TEST(file_stream_wsr); EXPECT_SYSZR(1, test_file_stream_wsr()); break;
@@ -1753,6 +1806,7 @@ int run_syscall(int min, int max)
CASE_TEST(select_null); EXPECT_SYSZR(1, ({ struct timeval tv = { 0 }; select(0, NULL, NULL, NULL, &tv); })); break;
CASE_TEST(select_stdout); EXPECT_SYSNE(1, ({ fd_set fds; FD_ZERO(&fds); FD_SET(1, &fds); select(2, NULL, &fds, NULL, NULL); }), -1); break;
CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break;
+ CASE_TEST(select_high_fd); EXPECT_SYSZR(1, test_select_high_fd()); break;
CASE_TEST(sendfile); EXPECT_SYSZR(1, test_sendfile()); break;
CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break;
CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break;
--
2.55.0
prev parent reply other threads:[~2026-09-24 1:52 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-24 1:52 [PATCH 0/3] tools/nolibc: fix readdir_r() and the FD_* macros on 64-bit Danish Khateeb
2026-09-24 1:52 ` [PATCH 1/3] tools/nolibc: fix readdir_r() with 64-bit directory offsets Danish Khateeb
2026-09-24 1:52 ` [PATCH 2/3] tools/nolibc: fix FD_SET(), FD_CLR() and FD_ISSET() on 64-bit Danish Khateeb
2026-09-24 1:52 ` 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=20260924015222.31693-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®