From patchwork Tue Mar 28 21:01:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13191555 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 6E61BC77B60 for ; Tue, 28 Mar 2023 21:01:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229728AbjC1VBm (ORCPT ); Tue, 28 Mar 2023 17:01:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229601AbjC1VBh (ORCPT ); Tue, 28 Mar 2023 17:01:37 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [IPv6:2a01:4f8:c010:41de::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B75841716; Tue, 28 Mar 2023 14:01:36 -0700 (PDT) From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1680037294; bh=WpL7AHZ/RVZDepQeqXUZNCLzamt9+Jfw6eToN9dbdkk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=qZPhWCAQ9mtX5UcSjcGvLyKkKjhiFzgMte5Vjg6gqQl1RcW2iPGKrnzfc4GnJVj6C otaonLDTMl6al5TaZvKq0qGD+93+YqIWGndU7V0VfGhew6tioj8XEpCHWMvlknqcQk tnBtmN8qgUHwVBhs2FQI62qykfFvs3RBYSWN3fq4= Date: Tue, 28 Mar 2023 21:01:29 +0000 Subject: [PATCH 1/3] tools/nolibc: add wrapper for memfd_create MIME-Version: 1.0 Message-Id: <20230328-nolibc-printf-test-v1-1-d7290ec893dd@weissschuh.net> References: <20230328-nolibc-printf-test-v1-0-d7290ec893dd@weissschuh.net> In-Reply-To: <20230328-nolibc-printf-test-v1-0-d7290ec893dd@weissschuh.net> To: Willy Tarreau , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.12.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680037292; l=1058; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=WpL7AHZ/RVZDepQeqXUZNCLzamt9+Jfw6eToN9dbdkk=; b=hxBrlQLFrwwDVd42h6UbrnnXBWKCutgcco3Nq/SMyjiRMzJ3HK4yzYu5PEOfWL0E8UFWSQxGz /ooa/8xHDRiDpwNiHshJJA0TawDG/PFtrF+dRBinwEkoHrWIvaaLRG1 X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This is useful for users and will also be used by a future testcase. Signed-off-by: Thomas Weißschuh --- 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; } + +/* + * 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 = sys_memfd_create(name, flags); + + if (ret < 0) { + SET_ERRNO(-ret); + ret = -1; + } + return ret; +} + /* make sure to include all global symbols */ #include "nolibc.h" From patchwork Tue Mar 28 21:01:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13191556 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 46FBFC761A6 for ; Tue, 28 Mar 2023 21:01:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229477AbjC1VBn (ORCPT ); Tue, 28 Mar 2023 17:01:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229621AbjC1VBi (ORCPT ); Tue, 28 Mar 2023 17:01:38 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8F151989; Tue, 28 Mar 2023 14:01:36 -0700 (PDT) From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1680037294; bh=gTyXb86YCdpSyD5ksJfQiCofzh+RXt/YaEbOdrzwMus=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=AVeSNlXApxdNHbGdAjrWP0AtUxQM11T9+3tWwkk3fASmPMd4yyHncAGNk3etiqkCB p7MnOQoGPfbDXIslyAqPJKB/KA9FRuCsfobNCRFfjYpJev95pJysVh0cEUqph3nksb i1Yp8B8jjP0M6RsDOsnOmmotOJSwBXJAwJrmpcm8= Date: Tue, 28 Mar 2023 21:01:30 +0000 Subject: [PATCH 2/3] tools/nolibc: let FILE streams contain an fd MIME-Version: 1.0 Message-Id: <20230328-nolibc-printf-test-v1-2-d7290ec893dd@weissschuh.net> References: <20230328-nolibc-printf-test-v1-0-d7290ec893dd@weissschuh.net> In-Reply-To: <20230328-nolibc-printf-test-v1-0-d7290ec893dd@weissschuh.net> To: Willy Tarreau , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.12.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680037292; l=2316; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=gTyXb86YCdpSyD5ksJfQiCofzh+RXt/YaEbOdrzwMus=; b=Q2O9+eUZgks7p8V+sdQe74LF15Bx0aOaZwaUz56aK619ovw3OXm/kFcEM3/1t7IECAMaoCvsn kuC0GQaLxfODzavdooRKXyrEwvbfVuO2UtvFJUjVBkOeszZ3R6o3JdS X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This enables the usage of the stream APIs with arbitrary filedescriptors. It will be used by a future testcase. Users can also use nolibc-specific code to do the same. Signed-off-by: Thomas Weißschuh --- tools/include/nolibc/stdio.h | 36 +++++++----------------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 96ac8afc5aee..cb58912b98e5 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -21,17 +21,13 @@ #define EOF (-1) #endif -/* just define FILE as a non-empty type */ typedef struct FILE { - char dummy[1]; + int fd; } FILE; -/* We define the 3 common stdio files as constant invalid pointers that - * are easily recognized. - */ -static __attribute__((unused)) FILE* const stdin = (FILE*)-3; -static __attribute__((unused)) FILE* const stdout = (FILE*)-2; -static __attribute__((unused)) FILE* const stderr = (FILE*)-1; +static __attribute__((unused)) FILE* const stdin = &(FILE){ STDIN_FILENO }; +static __attribute__((unused)) FILE* const stdout = &(FILE){ STDOUT_FILENO }; +static __attribute__((unused)) FILE* const stderr = &(FILE){ STDERR_FILENO }; /* getc(), fgetc(), getchar() */ @@ -41,14 +37,8 @@ static __attribute__((unused)) int fgetc(FILE* stream) { unsigned char ch; - int fd; - if (stream < stdin || stream > stderr) - return EOF; - - fd = 3 + (long)stream; - - if (read(fd, &ch, 1) <= 0) + if (read(stream->fd, &ch, 1) <= 0) return EOF; return ch; } @@ -68,14 +58,8 @@ static __attribute__((unused)) int fputc(int c, FILE* stream) { unsigned char ch = c; - int fd; - if (stream < stdin || stream > stderr) - return EOF; - - fd = 3 + (long)stream; - - if (write(fd, &ch, 1) <= 0) + if (write(stream->fd, &ch, 1) <= 0) return EOF; return ch; } @@ -96,15 +80,9 @@ 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 = 3 + (long)stream; while (size) { - ret = write(fd, buf, size); + ret = write(stream->fd, buf, size); if (ret <= 0) return EOF; size -= ret; From patchwork Tue Mar 28 21:01:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Patchwork-Id: 13191554 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 95CC3C76196 for ; Tue, 28 Mar 2023 21:01:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229716AbjC1VBk (ORCPT ); Tue, 28 Mar 2023 17:01:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229468AbjC1VBh (ORCPT ); Tue, 28 Mar 2023 17:01:37 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [IPv6:2a01:4f8:c010:41de::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3FFCAA; Tue, 28 Mar 2023 14:01:36 -0700 (PDT) From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1680037294; bh=alY0x/VaDr1RAvciNoovBBwjq3qApmPEX1CtPxUvARM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SiXVj8KLZGAflZmMsn7KDZM8aoIOrYeNbXlXjDoDmJVHxeFc7fQboV1jMKXMuCevY MChpXsfdQSfNPgwKq7vCv0P4RID7Lr/Xfi4FR+AtNV3b5lqZ9WbjRB0FDDMDbhY0TN CkZ5ZLq3s4FLJmqA+7qZpPPOvdhli9HHcM6mdrDY= Date: Tue, 28 Mar 2023 21:01:31 +0000 Subject: [PATCH 3/3] tools/nolibc: add testcases for vfprintf MIME-Version: 1.0 Message-Id: <20230328-nolibc-printf-test-v1-3-d7290ec893dd@weissschuh.net> References: <20230328-nolibc-printf-test-v1-0-d7290ec893dd@weissschuh.net> In-Reply-To: <20230328-nolibc-printf-test-v1-0-d7290ec893dd@weissschuh.net> To: Willy Tarreau , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.12.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680037292; l=3117; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=alY0x/VaDr1RAvciNoovBBwjq3qApmPEX1CtPxUvARM=; b=yTO6uZHjWLASU6OtjcrG7qIb+skiSX463rZSJ0Ha2r82kwYLOYMBYnfeM9r2ROXh0iMPa48P7 JR/xEk/gMCdAUMf4euhA8rpzMxTjhpVDuQYN5QgRFqoo8y214ruJ86H X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org vfprintf() is complex and so far did not have proper tests. Signed-off-by: Thomas Weißschuh --- tools/testing/selftests/nolibc/nolibc-test.c | 77 ++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 47013b78972e..cc60c0f7363d 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -667,6 +667,82 @@ int run_stdlib(int min, int max) return ret; } +#define EXPECT_VFPRINTF(c, expected, fmt, ...) \ + ret += 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]; + va_list args; + + fd = memfd_create("vfprintf", 0); + if (fd == -1) { + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + + va_start(args, fmt); + w = vfprintf(&(FILE) { fd }, fmt, args); + va_end(args); + + if (w != c) { + llen += printf(" written(%d) != %d", w, (int) c); + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + + lseek(fd, 0, SEEK_SET); + + r = read(fd, buf, sizeof(buf) - 1); + buf[r] = '\0'; + + close(fd); + + if (r != w) { + llen += printf(" written(%d) != read(%d)", w, r); + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + + llen += printf(" \"%s\" = \"%s\"", expected, buf); + ret = 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 = 0; + void *p1, *p2; + + for (test = min; test >= 0 && test <= max; test++) { + int llen = 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, "0x0", "%p", NULL); 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]; @@ -774,6 +850,7 @@ static const struct test test_names[] = { /* add new tests here */ { .name = "syscall", .func = run_syscall }, { .name = "stdlib", .func = run_stdlib }, + { .name = "vfprintf", .func = run_vfprintf }, { .name = "protection", .func = run_protection }, { 0 } };