From patchwork Sun Apr 2 13:02:45 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: 78240 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1730952vqo; Sun, 2 Apr 2023 06:06:38 -0700 (PDT) X-Google-Smtp-Source: AKy350b354YJi6+jyeCM+Hrfb7dMZX2im88wOwMSLdqnV8sVnXSywUCszrrKOiSyX+KJ0ENR38G4 X-Received: by 2002:a17:907:6b8d:b0:947:3d04:61dc with SMTP id rg13-20020a1709076b8d00b009473d0461dcmr12292732ejc.77.1680440798047; Sun, 02 Apr 2023 06:06:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680440798; cv=none; d=google.com; s=arc-20160816; b=zdGcRm4DWqLAQJF5/Z/aPhadhRDwewr9n+LNtC1yW9sfseqVyvKSqYjuWwMQhH5l+f zaFzqJilz7AEUnqyIs/sjtEcEwTiI5XjNrS9mP2fEWCyry1DQ5QVkRN3PzvnCMlUYIxE ase8YtepF2iq7XRsQYgvwDn8p0Qol1OuKMuw/Jx2B+iEEsxHaHS2hymdo4bMaAKEz5j6 LFfP0SNlMaFAxuC+e88kB9tJDFuSX2R75SB6nmhaItwf42eKt5hhsu1rfoRCdxnCooHN 5SH06OJ/kQkFgQdAWLT1alLnBN4O+cVOVeEUF7mLWyMUtbC2MAhEspMOqWWw2EzlTeMC l41g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:dkim-signature :from; bh=Tn92QrYrO30FJIGvcj7KKlVx9kMcoU0T+e1tbWXUs+A=; b=EXfKU3zL9LrHdFdc8BNZSMjHFn7CsTrjE60IlXZ3reD8PCPzpX0DqBA8eSyE8iOXXD c3oxnFlqJt/yzf+DLTAdv21DCLmocfDt/kpcBCDVPUOt5CYQ1n3fRuZqG2FR90wMS5ev mZqeBPP7P0BhLjuAX1naF4Cwkel5jUp43Wj2OjBtHLaf45Am7xbdLlTbd+9KIGQ/cQBr C+JkG4SRUGER6EsXSNXzzkoSpndC2R73eJArtQpiM7Y7VyMpFfkV0k1y1JQq8jOKa59Z uKY/mG6phrbWJmOSrehu8RbFEZEEzP26MUE7jTMlaizL0sJfyaGb9+1zyTr4BiJfokd9 0lYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b="pqdHX2/y"; 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 hw13-20020a170907a0cd00b00947c7226bc6si4850940ejc.150.2023.04.02.06.06.13; Sun, 02 Apr 2023 06:06:38 -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="pqdHX2/y"; 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 S230298AbjDBNC5 (ORCPT + 99 others); Sun, 2 Apr 2023 09:02:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230316AbjDBNCx (ORCPT ); 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 9D8E811151; Sun, 2 Apr 2023 06:02:52 -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=1680440570; bh=WpL7AHZ/RVZDepQeqXUZNCLzamt9+Jfw6eToN9dbdkk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=pqdHX2/ysrjGil1F8CJvU3kSvvN0g51gBI/jMf8KQ4eq3O07Q8BlWw5rQbWTK1s5M 3mAp4bMylFffDG/R2qBr/qyl+k4BxdsIPiizR1qawC3fxjPIWrmYdmz+Kef8gdziqB +N8jnnbecQC/IkFE2c1eKXoNQDh/T0qFoBwromJQ= Date: Sun, 02 Apr 2023 13:02:45 +0000 Subject: [PATCH v2 1/3] tools/nolibc: add wrapper for memfd_create MIME-Version: 1.0 Message-Id: <20230328-nolibc-printf-test-v2-1-f72bdf210190@weissschuh.net> References: <20230328-nolibc-printf-test-v2-0-f72bdf210190@weissschuh.net> In-Reply-To: <20230328-nolibc-printf-test-v2-0-f72bdf210190@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=1680440570; l=1058; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=WpL7AHZ/RVZDepQeqXUZNCLzamt9+Jfw6eToN9dbdkk=; b=fZuToNS5fW3RpPlq3PQshf/DvtoGs9y00jV2XPNuT/mGd1z22og9U2HDGMtjzWtGJ6g3OOfdY DdkW1Gy8q4WDjOB5w5x31JvvBmMvRSbTf+Ks9TVWkkiFA1yUAoF/WXp 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762069890156096387?= X-GMAIL-MSGID: =?utf-8?q?1762069890156096387?= 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 Sun Apr 2 13:02:46 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: 78239 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1729510vqo; Sun, 2 Apr 2023 06:04:27 -0700 (PDT) X-Google-Smtp-Source: AKy350bayAS1VWHapaGd7glFqOvzSHiEQze5E5hVdKO//fZwPau+mNPBAWxWWXCBJdc0AX64sU90 X-Received: by 2002:a17:906:5856:b0:930:1391:da7c with SMTP id h22-20020a170906585600b009301391da7cmr29055107ejs.60.1680440667578; Sun, 02 Apr 2023 06:04:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680440667; cv=none; d=google.com; s=arc-20160816; b=ZsEGoxBofCTyo7cAAxZbA0p1h7KkOkKaLW+fyc/gAtcp7mcEYY6vumawILFOZvOaGf hQ8szqW8sw/W/lLUEWFGNDwJH8lsDh9tQiamMTW1JVtaLpCHgwX3KQx+vAhhYfhDPGyQ T8P+5A5iQek0Q8DHRnUqHRrL9EfrSktd9TToOq6NGRaLFEUlR2bpA/ujRehQxw8araBn iao9UlsWCM15C+paHDU3YCmUDUzUGU7N/id6N4Btbf2s83NyU+qise6FAlin2fgy+SHg nINUyWCsXFFpRN0MIVIPn9PqysnQyG8wE/UDbGwLOTkVLWadKOgnTTMpbxRVkqXE6bNi 7Z6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:dkim-signature :from; bh=0rySYs/uedDXbQ8lnc/wuGQO0NJwQlNc/qxuEYmFJow=; b=BB7vn8wm+sg+DZo3DDXm6M8gqw2CxH1RtR8b68FaLIHze1JWTF33aUFN2sKjZl6XcL 2vQTMrENmyB8X7SQH1x4xUJepc1yqTVAGR6xb8l3NAqrxicohhHuJmngrtjrRc/2Aesj mH2lbqX4BszSBS2ObjOr2ftLlyKOnHCozRjDOpv7HXjz/gYB04/sYIOEQUTx+lR5OIml jyTGhXvPbgM1NLzRKjumfOPKGZZC0H8oKSylbcGgxbvO4cbpc+gArBaPUZLslm+7Vndu ggNKpHfVIC6pOXiIiPcaAd4CXiZ/huLGLRKuGl2hvTNWA/xe+wnJeTl91qFojyYtWNi3 t2lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=IH8eEvhV; 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 bo8-20020a0564020b2800b004accb68cc93si4694353edb.387.2023.04.02.06.04.04; Sun, 02 Apr 2023 06:04:27 -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=IH8eEvhV; 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 S230241AbjDBNDA (ORCPT + 99 others); Sun, 2 Apr 2023 09:03:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230381AbjDBNCy (ORCPT ); Sun, 2 Apr 2023 09:02:54 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8F1F11159; Sun, 2 Apr 2023 06:02:52 -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=1680440570; bh=T8RVOs4azHVELdWLvY+5zv0X2y22kGN/1wJMaJIVLtc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=IH8eEvhVyAcAtKynq+H8IW/Krqlmz4GRaD9yr3ys4uDrIJvRBYJo/zXE8FHlIQULA ilMRHtpqjFUrIUge4slsizPDekitgtUo45FoQ6Vn9NVXBQJHu7W3z3jbwUZbc/X2lF vp0qC4To3HcnJs7sttaBEUDhWnTlM1ByU7DuKaew= Date: Sun, 02 Apr 2023 13:02:46 +0000 Subject: [PATCH v2 2/3] tools/nolibc: implement fd-based FILE streams MIME-Version: 1.0 Message-Id: <20230328-nolibc-printf-test-v2-2-f72bdf210190@weissschuh.net> References: <20230328-nolibc-printf-test-v2-0-f72bdf210190@weissschuh.net> In-Reply-To: <20230328-nolibc-printf-test-v2-0-f72bdf210190@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=1680440570; l=3061; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=T8RVOs4azHVELdWLvY+5zv0X2y22kGN/1wJMaJIVLtc=; b=5iR9MA+v57ECoqD2l9JxLhY8lvr1vpmRApDWKYWF44Ra2kojXz7Zb+78WA3ZY0UoFxngpL6un BvpTdEDrFI9BdC182hzgWdJK01UvDSj38Poi7mvwaqsIU270Wrf3Bdt 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762069753444041852?= X-GMAIL-MSGID: =?utf-8?q?1762069753444041852?= This enables the usage of the stream APIs with arbitrary filedescriptors. It will be used by a future testcase. Signed-off-by: Thomas Weißschuh --- Willy: This uses intptr_t instead of uintptr_t as proposed because uintptr_t can not be negative. --- tools/include/nolibc/stdio.h | 60 ++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 96ac8afc5aee..edf1009e9203 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -21,17 +21,40 @@ #define EOF (-1) #endif -/* 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=~fd for fd>=0 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; -/* 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*)(intptr_t)~STDIN_FILENO; +static __attribute__((unused)) FILE* const stdout = (FILE*)(intptr_t)~STDOUT_FILENO; +static __attribute__((unused)) FILE* const stderr = (FILE*)(intptr_t)~STDERR_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) + return NULL; + return (FILE*)(intptr_t)~fd; +} + +/* provides the fd from of stream. */ +static __attribute__((unused)) +int fileno(FILE *stream) +{ + intptr_t i = (intptr_t)stream; + + if (i > 0) { + SET_ERRNO(EBADF); + return -1; + } + return ~i; +} /* getc(), fgetc(), getchar() */ @@ -41,14 +64,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(fileno(stream), &ch, 1) <= 0) return EOF; return ch; } @@ -68,14 +85,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(fileno(stream), &ch, 1) <= 0) return EOF; return ch; } @@ -96,12 +107,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 = 3 + (long)stream; + int fd = fileno(stream); while (size) { ret = write(fd, buf, size); From patchwork Sun Apr 2 13:02:47 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: 78241 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1736136vqo; Sun, 2 Apr 2023 06:15:56 -0700 (PDT) X-Google-Smtp-Source: AKy350bqOiCJwQfPjyOgliGKl+e7a9fKdDfXEpREweEPcw0d7e5JVhvb04G3D9yWPr7aGMIb/9Ah X-Received: by 2002:a17:907:3f82:b0:92e:c4c9:7a43 with SMTP id hr2-20020a1709073f8200b0092ec4c97a43mr39949843ejc.25.1680441356657; Sun, 02 Apr 2023 06:15:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680441356; cv=none; d=google.com; s=arc-20160816; b=mBpNMU7cPlutGvmSktAIyqXwSyiH8fIJmpHM4bKJGJzCCfkgZ1nNjiN8Ha+6N5pQWw Jyr+BzWPxis2W8e5S0jG3Hf2kCiyk3LDhRL4zanmDo9hU9BIT4IwKOh557E3N0vdr3g1 mXhAw256AcDwVFNfYEtS9iNRuksxjcnglyu+mNn8MObNY+CsQ0qhW6Upu+x/JHEyF9DI UncoS6711v9wrmjqvPChwSlys11Vw7KfPJ+g939D50Xm+xjXnX2oGfT0ZJBe1j6kxSJo DUGZv9vA+ne/Lbp2xOxMc9S7cKkAsCZK8i4/vOYrS9W9rVeh3zlfVtYq1OUOTJEBzZOt 9Lnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:dkim-signature :from; bh=u9hAWuHjPd+pafLytqNoZdzEacQSZA6iVW7BwFK+JYg=; b=al3QSCQuwM9P+vG7tgFBkMr3OKJGHNJPykiV6Y+REFaUIg6rEmoxhpq9ejxmg/4mT+ hkIWbBAvV0uiQbEoOgBWV3ZjD5tCa6gzZaQJcCU8qnpPbIkyWCfGGffsx67lRwpYFXP3 yx8rVtKqhYwrF29UxFsljrmXFKsvfhC+Ka0COYaeyz57255bU+whLQzqsPz0UlyNHm6n 2FvkteltpzHDhJS4q70THUNjywNf83EHG3/t7mnrG3G+vuEUCa52iwD+uG9m/kBgjpJr 5u4MGakuCM6vzex1YyxtyivY4y5jPhL35aY0lSEkfik/PoAuDqO7CrBgXGlCambzUUp5 MGcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=msmqM2dQ; 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 qm7-20020a170907674700b00947ab463d2dsi1950472ejc.674.2023.04.02.06.15.29; Sun, 02 Apr 2023 06:15:56 -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=msmqM2dQ; 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 S230421AbjDBNDE (ORCPT + 99 others); Sun, 2 Apr 2023 09:03:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230343AbjDBNCy (ORCPT ); Sun, 2 Apr 2023 09:02:54 -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 C0A0311155; Sun, 2 Apr 2023 06:02:52 -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=1680440571; bh=XslKaVqkZRJWWcajYBGB4tWOiC4UW2nlnB382eAXHxc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=msmqM2dQ3kAiWKucBkxXplxVGB2jMEBPZRLYrjyv8WZlD+E276wbrK555iilnCOEq SiHlXDopE9sV+wPUM80DE+OhR91m2YTGxTQxQaokv+a9q5NOuZVyPTEm1qX4UTLUIW ofjtLoeq4QVTm14KGiCJxWC3bYmUswI73Q+m8g50= Date: Sun, 02 Apr 2023 13:02:47 +0000 Subject: [PATCH v2 3/3] tools/nolibc: add testcases for vfprintf MIME-Version: 1.0 Message-Id: <20230328-nolibc-printf-test-v2-3-f72bdf210190@weissschuh.net> References: <20230328-nolibc-printf-test-v2-0-f72bdf210190@weissschuh.net> In-Reply-To: <20230328-nolibc-printf-test-v2-0-f72bdf210190@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=1680440570; l=3312; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=XslKaVqkZRJWWcajYBGB4tWOiC4UW2nlnB382eAXHxc=; b=vzNuipwmsMWMEEg1jyjZTdKduVLj7U5A3uQYanHsSo+Z5QTnoxqCOKuIxfYn0MV2uEmx8h9+5 Zt1lBrDVzy1BD3gKaJLtZT4K23noI7BkiGunGkkWqO4nr8+Pots53Ma 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: 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?1762070475830837511?= vfprintf() is complex and so far did not have proper tests. Signed-off-by: Thomas Weißschuh --- tools/testing/selftests/nolibc/nolibc-test.c | 78 ++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 47013b78972e..25a64bda2722 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 @@ -667,6 +668,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(fdopen(fd, "w+"), 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 +851,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 } };