[PATCH v2 2/2] selftests/nolibc: Add a very basic test for fallocate()

From: Daniel Palmer

Date: Fri Apr 17 2026 - 07:27:45 EST


1: Create a tmp file, fallocate() to make it a bit bigger, check the
size is what was expected.

2: Try to fallocate() (1 << 28),

3: Try to fallocate() (1 << 60), this causes a EFBIG

Signed-off-by: Daniel Palmer <daniel@xxxxxxxxx>
---
tools/testing/selftests/nolibc/nolibc-test.c | 58 ++++++++++++++++++++
1 file changed, 58 insertions(+)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index d3c4facb54c0..6dcc2f631c3e 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -896,6 +896,63 @@ int test_getpagesize(void)
return !c;
}

+int test_fallocate(void)
+{
+ struct stat st;
+ int fd, r;
+
+ /* Create a new tmp file */
+ fd = open("/tmp", O_TMPFILE | O_RDWR, 0644);
+ if (fd == -1)
+ return -1;
+
+ /* Expand it to 42 bytes */
+ r = fallocate(fd, 0, 0, 42);
+ if (r)
+ goto close_tmpfile;
+
+ /* Get the new stat */
+ r = fstat(fd, &st);
+ if (r)
+ goto close_tmpfile;
+
+ /* It should be 42 bytes long */
+ if (st.st_size != 42) {
+ r = -1;
+ goto close_tmpfile;
+ }
+
+ /* Now try to allocate 256MiB, this shouldn't fail
+ */
+ r = fallocate(fd, 0, 0, (1ll << 28));
+ if (r)
+ goto close_tmpfile;
+
+ /* Check that a massive size that happens to be the
+ * opposite endian version of the above. This should
+ * returns an error and errno = EFBIG indicating the
+ * value was passed correctly but the kernel rejected
+ * it.
+ */
+ r = fallocate(fd, 0, 0, (1ll << (28 + 32)));
+ if (r != -1) {
+ r = -1;
+ goto close_tmpfile;
+ }
+ if (errno != EFBIG) {
+ r = -1;
+ goto close_tmpfile;
+ }
+
+ /* Test passed */
+ r = 0;
+
+close_tmpfile:
+ close(fd);
+
+ return r;
+}
+
int test_file_stream(void)
{
FILE *f;
@@ -1442,6 +1499,7 @@ int run_syscall(int min, int max)
CASE_TEST(dup3_0); tmp = dup3(0, 100, 0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
CASE_TEST(dup3_m1); tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break;
+ CASE_TEST(fallocate); EXPECT_SYSZR(1, test_fallocate()); 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(file_stream); EXPECT_SYSZR(1, test_file_stream()); break;
--
2.51.0