From nobody Tue Feb 10 23:53:32 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8B370C76196 for ; Sun, 2 Apr 2023 18:48:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231180AbjDBSsX (ORCPT ); Sun, 2 Apr 2023 14:48:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230470AbjDBSsT (ORCPT ); Sun, 2 Apr 2023 14:48:19 -0400 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D6ECA65B6 for ; Sun, 2 Apr 2023 11:48:18 -0700 (PDT) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 332ImDkS012487; Sun, 2 Apr 2023 20:48:13 +0200 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux@weissschuh.net, linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 1/4] tools/nolibc: add libc-test binary Date: Sun, 2 Apr 2023 20:48:03 +0200 Message-Id: <20230402184806.12440-2-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230402184806.12440-1-w@1wt.eu> References: <20230402184806.12440-1-w@1wt.eu> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Wei=C3=9Fschuh This can be used to easily compare the behavior of nolibc to the system libc. Signed-off-by: Thomas Wei=C3=9Fschuh Signed-off-by: Willy Tarreau --- tools/testing/selftests/nolibc/.gitignore | 1 + tools/testing/selftests/nolibc/Makefile | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/tools/testing/selftests/nolibc/.gitignore b/tools/testing/self= tests/nolibc/.gitignore index 4696df589d68..52f613cdad54 100644 --- a/tools/testing/selftests/nolibc/.gitignore +++ b/tools/testing/selftests/nolibc/.gitignore @@ -1,4 +1,5 @@ /initramfs/ +/libc-test /nolibc-test /run.out /sysroot/ diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selfte= sts/nolibc/Makefile index bbce57420465..0cbe13809b37 100644 --- a/tools/testing/selftests/nolibc/Makefile +++ b/tools/testing/selftests/nolibc/Makefile @@ -94,6 +94,7 @@ help: @echo " help this help" @echo " sysroot create the nolibc sysroot here (uses \$$ARCH)" @echo " nolibc-test build the executable (uses \$$CC and \$$CROSS_COMPI= LE)" + @echo " libc-test build an executable using the compiler's default li= bc instead" @echo " run-user runs the executable under QEMU (uses \$$ARCH, \$$TE= ST)" @echo " initramfs prepare the initramfs with nolibc-test" @echo " defconfig create a fresh new default config (uses \$$ARCH)" @@ -128,6 +129,9 @@ nolibc-test: nolibc-test.c sysroot/$(ARCH)/include $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ -nostdlib -static -Isysroot/$(ARCH)/include $< -lgcc =20 +libc-test: nolibc-test.c + $(QUIET_CC)$(CC) -o $@ $< + # qemu user-land test run-user: nolibc-test $(Q)qemu-$(QEMU_ARCH) ./nolibc-test > "$(CURDIR)/run.out" || : @@ -159,6 +163,8 @@ clean: $(Q)rm -rf sysroot $(call QUIET_CLEAN, nolibc-test) $(Q)rm -f nolibc-test + $(call QUIET_CLEAN, libc-test) + $(Q)rm -f libc-test $(call QUIET_CLEAN, initramfs) $(Q)rm -rf initramfs $(call QUIET_CLEAN, run.out) --=20 2.17.5 From nobody Tue Feb 10 23:53:32 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36833C76196 for ; Sun, 2 Apr 2023 18:48:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231203AbjDBSsc (ORCPT ); Sun, 2 Apr 2023 14:48:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231210AbjDBSs2 (ORCPT ); Sun, 2 Apr 2023 14:48:28 -0400 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 793128697 for ; Sun, 2 Apr 2023 11:48:25 -0700 (PDT) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 332ImDKD012488; Sun, 2 Apr 2023 20:48:13 +0200 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux@weissschuh.net, linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 2/4] tools/nolibc: add wrapper for memfd_create Date: Sun, 2 Apr 2023 20:48:04 +0200 Message-Id: <20230402184806.12440-3-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230402184806.12440-1-w@1wt.eu> References: <20230402184806.12440-1-w@1wt.eu> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Wei=C3=9Fschuh This is useful for users and will also be used by a future testcase. Signed-off-by: Thomas Wei=C3=9Fschuh Signed-off-by: Willy Tarreau --- tools/include/nolibc/sys.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 5d624dc63a42..bea9760dbd16 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -1365,6 +1365,29 @@ ssize_t write(int fd, const void *buf, size_t count) return ret; } =20 + +/* + * int memfd_create(const char *name, unsigned int flags); + */ + +static __attribute__((unused)) +int sys_memfd_create(const char *name, unsigned int flags) +{ + return my_syscall2(__NR_memfd_create, name, flags); +} + +static __attribute__((unused)) +int memfd_create(const char *name, unsigned int flags) +{ + ssize_t ret =3D sys_memfd_create(name, flags); + + if (ret < 0) { + SET_ERRNO(-ret); + ret =3D -1; + } + return ret; +} + /* make sure to include all global symbols */ #include "nolibc.h" =20 --=20 2.17.5 From nobody Tue Feb 10 23:53:32 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D4CFC77B60 for ; Sun, 2 Apr 2023 18:48:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231233AbjDBSsd (ORCPT ); Sun, 2 Apr 2023 14:48:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231213AbjDBSs3 (ORCPT ); Sun, 2 Apr 2023 14:48:29 -0400 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9BB2A6A59 for ; Sun, 2 Apr 2023 11:48:25 -0700 (PDT) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 332ImDTk012489; Sun, 2 Apr 2023 20:48:13 +0200 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux@weissschuh.net, linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 3/4] tools/nolibc: implement fd-based FILE streams Date: Sun, 2 Apr 2023 20:48:05 +0200 Message-Id: <20230402184806.12440-4-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230402184806.12440-1-w@1wt.eu> References: <20230402184806.12440-1-w@1wt.eu> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Wei=C3=9Fschuh This enables the usage of the stream APIs with arbitrary filedescriptors. It will be used by a future testcase. Signed-off-by: Thomas Wei=C3=9Fschuh Signed-off-by: Willy Tarreau --- tools/include/nolibc/stdio.h | 95 ++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 27 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 96ac8afc5aee..4add736c07aa 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -21,17 +21,75 @@ #define EOF (-1) #endif =20 -/* just define FILE as a non-empty type */ +/* just define FILE as a non-empty type. The value of the pointer gives + * the FD: FILE=3D~fd for fd>=3D0 or NULL for fd<0. This way positive FILE + * are immediately identified as abnormal entries (i.e. possible copies + * of valid pointers to something else). + */ typedef struct FILE { char dummy[1]; } FILE; =20 -/* We define the 3 common stdio files as constant invalid pointers that - * are easily recognized. - */ -static __attribute__((unused)) FILE* const stdin =3D (FILE*)-3; -static __attribute__((unused)) FILE* const stdout =3D (FILE*)-2; -static __attribute__((unused)) FILE* const stderr =3D (FILE*)-1; +static __attribute__((unused)) FILE* const stdin =3D (FILE*)(intptr_t)~ST= DIN_FILENO; +static __attribute__((unused)) FILE* const stdout =3D (FILE*)(intptr_t)~ST= DOUT_FILENO; +static __attribute__((unused)) FILE* const stderr =3D (FILE*)(intptr_t)~ST= DERR_FILENO; + +/* provides a FILE* equivalent of fd. The mode is ignored. */ +static __attribute__((unused)) +FILE *fdopen(int fd, const char *mode __attribute__((unused))) +{ + if (fd < 0) { + SET_ERRNO(EBADF); + return NULL; + } + return (FILE*)(intptr_t)~fd; +} + +/* provides the fd of stream. */ +static __attribute__((unused)) +int fileno(FILE *stream) +{ + intptr_t i =3D (intptr_t)stream; + + if (i >=3D 0) { + SET_ERRNO(EBADF); + return -1; + } + return ~i; +} + +/* flush a stream. */ +static __attribute__((unused)) +int fflush(FILE *stream) +{ + intptr_t i =3D (intptr_t)stream; + + /* NULL is valid here. */ + if (i > 0) { + SET_ERRNO(EBADF); + return -1; + } + + /* Don't do anything, nolibc does not support buffering. */ + return 0; +} + +/* flush a stream. */ +static __attribute__((unused)) +int fclose(FILE *stream) +{ + intptr_t i =3D (intptr_t)stream; + + if (i >=3D 0) { + SET_ERRNO(EBADF); + return -1; + } + + if (close(~i)) + return EOF; + + return 0; +} =20 /* getc(), fgetc(), getchar() */ =20 @@ -41,14 +99,8 @@ static __attribute__((unused)) int fgetc(FILE* stream) { unsigned char ch; - int fd; =20 - if (stream < stdin || stream > stderr) - return EOF; - - fd =3D 3 + (long)stream; - - if (read(fd, &ch, 1) <=3D 0) + if (read(fileno(stream), &ch, 1) <=3D 0) return EOF; return ch; } @@ -68,14 +120,8 @@ static __attribute__((unused)) int fputc(int c, FILE* stream) { unsigned char ch =3D c; - int fd; - - if (stream < stdin || stream > stderr) - return EOF; - - fd =3D 3 + (long)stream; =20 - if (write(fd, &ch, 1) <=3D 0) + if (write(fileno(stream), &ch, 1) <=3D 0) return EOF; return ch; } @@ -96,12 +142,7 @@ static __attribute__((unused)) int _fwrite(const void *buf, size_t size, FILE *stream) { ssize_t ret; - int fd; - - if (stream < stdin || stream > stderr) - return EOF; - - fd =3D 3 + (long)stream; + int fd =3D fileno(stream); =20 while (size) { ret =3D write(fd, buf, size); --=20 2.17.5 From nobody Tue Feb 10 23:53:32 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA96BC76196 for ; Sun, 2 Apr 2023 18:48:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231274AbjDBSsq (ORCPT ); Sun, 2 Apr 2023 14:48:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231277AbjDBSsm (ORCPT ); Sun, 2 Apr 2023 14:48:42 -0400 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A8FAB9018 for ; Sun, 2 Apr 2023 11:48:32 -0700 (PDT) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 332ImDKm012490; Sun, 2 Apr 2023 20:48:13 +0200 From: Willy Tarreau To: "Paul E. McKenney" Cc: linux@weissschuh.net, linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 4/4] tools/nolibc: add testcases for vfprintf Date: Sun, 2 Apr 2023 20:48:06 +0200 Message-Id: <20230402184806.12440-5-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230402184806.12440-1-w@1wt.eu> References: <20230402184806.12440-1-w@1wt.eu> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Wei=C3=9Fschuh vfprintf() is complex and so far did not have proper tests. Signed-off-by: Thomas Wei=C3=9Fschuh Signed-off-by: Willy Tarreau --- tools/testing/selftests/nolibc/nolibc-test.c | 86 ++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index 1bafbd8da6af..888da60eb5ba 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -669,6 +670,90 @@ int run_stdlib(int min, int max) return ret; } =20 +#define EXPECT_VFPRINTF(c, expected, fmt, ...) \ + ret +=3D expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__) + +static int expect_vfprintf(int llen, size_t c, const char *expected, const= char *fmt, ...) +{ + int ret, fd, w, r; + char buf[100]; + FILE *memfile; + va_list args; + + fd =3D memfd_create("vfprintf", 0); + if (fd =3D=3D -1) { + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + + memfile =3D fdopen(fd, "w+"); + if (!memfile) { + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + + va_start(args, fmt); + w =3D vfprintf(memfile, fmt, args); + va_end(args); + + if (w !=3D c) { + llen +=3D printf(" written(%d) !=3D %d", w, (int) c); + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + + fflush(memfile); + lseek(fd, 0, SEEK_SET); + + r =3D read(fd, buf, sizeof(buf) - 1); + buf[r] =3D '\0'; + + fclose(memfile); + + if (r !=3D w) { + llen +=3D printf(" written(%d) !=3D read(%d)", w, r); + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + + llen +=3D printf(" \"%s\" =3D \"%s\"", expected, buf); + ret =3D strncmp(expected, buf, c); + + pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n"); + return ret; +} + +static int run_vfprintf(int min, int max) +{ + int test; + int tmp; + int ret =3D 0; + void *p1, *p2; + + for (test =3D min; test >=3D 0 && test <=3D max; test++) { + int llen =3D 0; // line length + + /* avoid leaving empty lines below, this will insert holes into + * test numbers. + */ + switch (test + __LINE__ + 1) { + CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break; + CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break; + CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break; + CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break; + CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break; + CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break; + CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break; + CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break; + CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); = break; + case __LINE__: + return ret; /* must be last */ + /* note: do not set any defaults so as to permit holes above */ + } + } + return ret; +} + static int smash_stack(void) { char buf[100]; @@ -777,6 +862,7 @@ static const struct test test_names[] =3D { /* add new tests here */ { .name =3D "syscall", .func =3D run_syscall }, { .name =3D "stdlib", .func =3D run_stdlib }, + { .name =3D "vfprintf", .func =3D run_vfprintf }, { .name =3D "protection", .func =3D run_protection }, { 0 } }; --=20 2.17.5