Message ID | 20230328-nolibc-printf-test-v2-0-f72bdf210190@weissschuh.net |
---|---|
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1729307vqo; Sun, 2 Apr 2023 06:04:07 -0700 (PDT) X-Google-Smtp-Source: AKy350bSMr+bv6Xll4e6+xInj0mDy4RJT9Nmx6yoxGLYl7nTRxTUqNtZxrnKQmSpF015y0JgQS1u X-Received: by 2002:aa7:cd71:0:b0:4fa:c7c9:62ea with SMTP id ca17-20020aa7cd71000000b004fac7c962eamr32315519edb.11.1680440647336; Sun, 02 Apr 2023 06:04:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680440647; cv=none; d=google.com; s=arc-20160816; b=no0vJsWvLI16TRvnjEUrQEVUahR4wzRbhPWquRpAEnxw27xoP7I/IWuYdsuQxMuYZx 77y9yIt7zvaWDFYuBI30o566Voi7yozpQjEnKhxduOfzoMXFAPS+L80X+RKRju7TpplN 25Pc9PIFBVrwmvkst06UF0hgaNKX6kWFO+udeiNvAVbSon1w8nGwcMne/JFFrrpJBFlD ibII59XGj7Zlx3zuzPOEjUM4sf/+eY8cpRFOGAvB6K0WsQmSf2izkV+Nf1KKGxT8po6l pktVGyF9dwEZm1uabsstTnW4FBj1la5uQmuw2AVL0T2YUvHBVtnGHCiRrcZr9q567Qbv fhyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:content-transfer-encoding:mime-version :message-id:date:subject:dkim-signature:from; bh=Dla8f2hpfD+9v39teMpgeGqArqPCqk1c/lvvE1jNEBs=; b=UF/EeBicbNm5UppE+0sMBfmM8ZV45MyiNQRs/hNzRUbqUD+PZSnY57wGjeKxMQfgCQ qq5aD7Ehzj++4PgKIxcFM/gGhmMeEmWZDOKI5OW7ao5yRV1eAzQatn1tD36nwL0TjJAW Dmd76rZpn/CuU+Pso2nIph7GtUi4nWykB0N0QydXtbh0TZygHWu5yl0nLKKRrVVul1DH J4OqPcUnR2KXcZZGeaL5opXB7O48ozGiCsbf9Hx0aCj6cYI6hpM1H4uKVKhjKMFOjpSK 8NdCngqIGIvU5hYTClSTWhewVAMvQIetCay6foo7BEuxpATym3hQGvEYH3KxHaLyg6cl 9Luw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=ZJ0rL9Kn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s3-20020aa7cb03000000b00501dd471e58si5907055edt.37.2023.04.02.06.03.42; Sun, 02 Apr 2023 06:04:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=ZJ0rL9Kn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230405AbjDBNCz (ORCPT <rfc822;dexuan.linux@gmail.com> + 99 others); Sun, 2 Apr 2023 09:02:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229646AbjDBNCx (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 2 Apr 2023 09:02:53 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89F8C10A89; Sun, 2 Apr 2023 06:02:52 -0700 (PDT) From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= <linux@weissschuh.net> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1680440570; bh=jGJ6EiY3Winw0q2v+5CI2620DGzPf3nK0gGxSUtKjAs=; h=From:Subject:Date:To:Cc:From; b=ZJ0rL9KnuyeHKFzSy7q0YfATKGDvwG6NhfNZIpo6eVMLSBpVxTC/HDGx35OJcpTVN dOt5Veh2RE3nJFjsJJHOHYywE8m/Td7Wf5A4ApNajFiKg8Ga1mmQutmr3cZCXu2+mU VqD3EJaUTfcoTVQBx2EA2WdHEsT5Kt4JZ5CuQk/A= Subject: [PATCH v2 0/3] tools/nolibc: add testcases for vfprintf Date: Sun, 02 Apr 2023 13:02:44 +0000 Message-Id: <20230328-nolibc-printf-test-v2-0-f72bdf210190@weissschuh.net> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-B4-Tracking: v=1; b=H4sIAPR8KWQC/x2N0QqDMAwAf0XyvECNCLJfGXtoa9RAidJ0YyD++ 4KPd3DcCcZV2ODZnVD5Kya7OtCjg7xFXRlldgYKNISBJtS9SMp4VNG2YGNrGEaax5gy9f0EHqZ ojKlGzZun+inF5VF5kd99er2v6w9E92fReQAAAA== To: Willy Tarreau <w@1wt.eu>, Shuah Khan <shuah@kernel.org> Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= <linux@weissschuh.net> X-Mailer: b4 0.12.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680440570; l=1057; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=jGJ6EiY3Winw0q2v+5CI2620DGzPf3nK0gGxSUtKjAs=; b=UQup0goPbq+gsa8fg/qhuwxM4NAL2GpAzORdVA+22mLKPuKAXEhZvGry+MQ+s8OtuZZwKJnAN XZhAyWjb6a+AYDgZmaX21o86VkiuSQ/jdwpVZiftKPAW1PgRVJ/8SQJ X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762069732346263999?= X-GMAIL-MSGID: =?utf-8?q?1762069732346263999?= |
Series |
tools/nolibc: add testcases for vfprintf
|
|
Message
Thomas Weißschuh
April 2, 2023, 1:02 p.m. UTC
vfprintf() is complex and so far did not have proper tests.
This series is based on the "dev" branch of the RCU tree.
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
Changes in v2:
- Include <sys/mman.h> for tests.
- Implement FILE* in terms of integer pointers.
- Provide fdopen() and fileno().
- Link to v1: https://lore.kernel.org/lkml/20230328-nolibc-printf-test-v1-0-d7290ec893dd@weissschuh.net/
---
Thomas Weißschuh (3):
tools/nolibc: add wrapper for memfd_create
tools/nolibc: implement fd-based FILE streams
tools/nolibc: add testcases for vfprintf
tools/include/nolibc/stdio.h | 60 +++++++++++----------
tools/include/nolibc/sys.h | 23 ++++++++
tools/testing/selftests/nolibc/nolibc-test.c | 78 ++++++++++++++++++++++++++++
3 files changed, 134 insertions(+), 27 deletions(-)
---
base-commit: a63baab5f60110f3631c98b55d59066f1c68c4f7
change-id: 20230328-nolibc-printf-test-052d5abc2118
Best regards,
Comments
Thomas, On Sun, Apr 02, 2023 at 01:02:44PM +0000, Thomas Weißschuh wrote: > vfprintf() is complex and so far did not have proper tests. > > This series is based on the "dev" branch of the RCU tree. I've just ran it with glibc to see: $ gcc nolibc-test.c $ ./a.out vfprintf Running test 'vfprintf' 0 empty "" = "" [OK] 1 simple written(3) != read(0) [FAIL] 2 string written(3) != read(0) [FAIL] 3 number written(4) != read(0) [FAIL] 4 negnumber written(5) != read(0) [FAIL] 5 unsigned written(5) != read(0) [FAIL] 6 char written(1) != read(0) [FAIL] 7 hex written(1) != read(0) [FAIL] 8 pointer written(5) != 3 [FAIL] Errors during this test: 8 The main issue was that glibc uses buffered writes by default. I could fix them with fflush() (which we don't have so it required an ifndef), and this also made me realize that we were missing an fclose() as well for compatibility with glibc. With this it got better: Running test 'vfprintf' 0 empty "" = "" [OK] 1 simple "foo" = "foo" [OK] 2 string "foo" = "foo" [OK] 3 number "1234" = "1234" [OK] 4 negnumber "-1234" = "-1234" [OK] 5 unsigned "12345" = "12345" [OK] 6 char "c" = "c" [OK] 7 hex "f" = "f" [OK] 8 pointer written(5) != 3 [FAIL] Errors during this test: 1 This is caused by glibc emitting "(nil)" while we emit "0x0" for a NULL pointer since we use the same code as when dumping integers. I could fix that one as well by printing (void*)1 instead, which shows "0x1" for both. This gives me the patch below on top of yours, which I think would make sense to integrate in this form or a simplified one if we manage to add fflush() and fclose() earlier. What do you think ? Thanks, Willy diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 28a8d77078dc..2958dc3eca93 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -678,6 +678,7 @@ static int expect_vfprintf(int llen, size_t c, const char *expected, const char int ret, fd, w, r; char buf[100]; va_list args; + FILE *memfile; fd = memfd_create("vfprintf", 0); if (fd == -1) { @@ -685,8 +686,14 @@ static int expect_vfprintf(int llen, size_t c, const char *expected, const char return 1; } + memfile = fdopen(fd, "w+"); + if (!memfile) { + pad_spc(llen, 64, "[FAIL]\n"); + return 1; + } + va_start(args, fmt); - w = vfprintf(fdopen(fd, "w+"), fmt, args); + w = vfprintf(memfile, fmt, args); va_end(args); if (w != c) { @@ -695,12 +702,19 @@ static int expect_vfprintf(int llen, size_t c, const char *expected, const char return 1; } +#ifndef _NOLIBC_STDIO_H + fflush(memfile); +#endif lseek(fd, 0, SEEK_SET); r = read(fd, buf, sizeof(buf) - 1); buf[r] = '\0'; +#ifndef _NOLIBC_STDIO_H + fclose(memfile); +#else close(fd); +#endif if (r != w) { llen += printf(" written(%d) != read(%d)", w, r); @@ -737,7 +751,7 @@ static int run_vfprintf(int min, int max) 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_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 */
On 2023-04-02 18:43:21+0200, Willy Tarreau wrote: > Thomas, > > On Sun, Apr 02, 2023 at 01:02:44PM +0000, Thomas Weißschuh wrote: > > vfprintf() is complex and so far did not have proper tests. > > > > This series is based on the "dev" branch of the RCU tree. > > I've just ran it with glibc to see: > > $ gcc nolibc-test.c > $ ./a.out vfprintf > Running test 'vfprintf' > 0 empty "" = "" [OK] > 1 simple written(3) != read(0) [FAIL] > 2 string written(3) != read(0) [FAIL] > 3 number written(4) != read(0) [FAIL] > 4 negnumber written(5) != read(0) [FAIL] > 5 unsigned written(5) != read(0) [FAIL] > 6 char written(1) != read(0) [FAIL] > 7 hex written(1) != read(0) [FAIL] > 8 pointer written(5) != 3 [FAIL] > Errors during this test: 8 > > The main issue was that glibc uses buffered writes by default. > > I could fix them with fflush() (which we don't have so it required an > ifndef), and this also made me realize that we were missing an fclose() > as well for compatibility with glibc. With this it got better: > > Running test 'vfprintf' > 0 empty "" = "" [OK] > 1 simple "foo" = "foo" [OK] > 2 string "foo" = "foo" [OK] > 3 number "1234" = "1234" [OK] > 4 negnumber "-1234" = "-1234" [OK] > 5 unsigned "12345" = "12345" [OK] > 6 char "c" = "c" [OK] > 7 hex "f" = "f" [OK] > 8 pointer written(5) != 3 [FAIL] > Errors during this test: 1 > > This is caused by glibc emitting "(nil)" while we emit "0x0" for a NULL > pointer since we use the same code as when dumping integers. I could fix > that one as well by printing (void*)1 instead, which shows "0x1" for both. > > This gives me the patch below on top of yours, which I think would make > sense to integrate in this form or a simplified one if we manage to add > fflush() and fclose() earlier. > > What do you think ? > > Thanks, > Willy > > > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c > index 28a8d77078dc..2958dc3eca93 100644 > --- a/tools/testing/selftests/nolibc/nolibc-test.c > +++ b/tools/testing/selftests/nolibc/nolibc-test.c > @@ -678,6 +678,7 @@ static int expect_vfprintf(int llen, size_t c, const char *expected, const char > int ret, fd, w, r; > char buf[100]; > va_list args; > + FILE *memfile; > > fd = memfd_create("vfprintf", 0); > if (fd == -1) { > @@ -685,8 +686,14 @@ static int expect_vfprintf(int llen, size_t c, const char *expected, const char > return 1; > } > > + memfile = fdopen(fd, "w+"); > + if (!memfile) { > + pad_spc(llen, 64, "[FAIL]\n"); > + return 1; > + } > + > va_start(args, fmt); > - w = vfprintf(fdopen(fd, "w+"), fmt, args); > + w = vfprintf(memfile, fmt, args); > va_end(args); > > if (w != c) { > @@ -695,12 +702,19 @@ static int expect_vfprintf(int llen, size_t c, const char *expected, const char > return 1; > } > > +#ifndef _NOLIBC_STDIO_H > + fflush(memfile); > +#endif > lseek(fd, 0, SEEK_SET); > > r = read(fd, buf, sizeof(buf) - 1); > buf[r] = '\0'; > > +#ifndef _NOLIBC_STDIO_H > + fclose(memfile); > +#else > close(fd); > +#endif Wouldn't it be nicer to implement fflush/fclose in nolibc? I can send a v3 with that. > if (r != w) { > llen += printf(" written(%d) != read(%d)", w, r); > @@ -737,7 +751,7 @@ static int run_vfprintf(int min, int max) > 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_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 */
On Sun, Apr 02, 2023 at 05:17:53PM +0000, Thomas Weißschuh wrote: > Wouldn't it be nicer to implement fflush/fclose in nolibc? Yes that was my point, I just didn't know whether you still had some time left to assign to this or not, hence my question. > I can send a v3 with that. Its currently running tests here. It looks pretty good, I guess I'm going to bless it. Thanks! Willy