From patchwork Fri Jun 7 12:13:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Maciej W. Rozycki" X-Patchwork-Id: 1945081 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=gfM6xpHS; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vwg921gB8z20Q5 for ; Fri, 7 Jun 2024 22:13:58 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6FA5439CC296 for ; Fri, 7 Jun 2024 12:13:56 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 768A639CC29A for ; Fri, 7 Jun 2024 12:13:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 768A639CC29A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 768A639CC29A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717762415; cv=none; b=bgya2zFzqlkAQb4HlzgHAY/XGc6bvzZ14TXT0ZQqUs34wEH1g9ZgAsnBEjcH/z5NJw3TrzkFHp3rZF8/757ZFyZH1NwvFfL5nDQ43TOznOWkadFtq80lnj4yBxq+lYhp0DTaCMcntLGwoCtWnF1YvrKouf8Rp+ArjHLFZtd7MXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717762415; c=relaxed/simple; bh=nSTVwY3ukmcfK2bY9vP2hKYjI4bitaJmNv8ilwgXrMg=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=eX5IsoPlzKsd2VcNKWZXU7HJ4sbvaQK7bzyojUnTD2KwvQ+ea4nH7W95Cy3FFuSw7/L+X9KdxEn3CGTMEDEjum2jwr7QATEoxyM0mNPHvE00RKY3AG2H9DU8JLsN1mOnEnznxK+vbiqx5KLVOS+CgEYmYd9N46pBNhZTs72jiBc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717762413; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=sMy+ZMYao6MRgexfEHxtVrGDA0Yaz9XqrYefoc55kg0=; b=gfM6xpHS+e6IB3sZfYMDFULnMjWjSt+7kZ6uhKlMEj78yy4ZqZg5VMG58vquB1UeBLU4pn UxWR1IvYHSPKdhxU/dUchIL6mYcJiFSBBuQP5kGLTm2hLMZlgDC6v5/PVG/ne0cAQOYB3P eOZ/dFVwcOFB4eK2q1VhXYeCPo6VjZQ= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-355-4KHoVocNNpucOUKHf_fX2w-1; Fri, 07 Jun 2024 08:13:29 -0400 X-MC-Unique: 4KHoVocNNpucOUKHf_fX2w-1 Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-57a306c4b1eso1245833a12.0 for ; Fri, 07 Jun 2024 05:13:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717762407; x=1718367207; h=mime-version:message-id:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=sMy+ZMYao6MRgexfEHxtVrGDA0Yaz9XqrYefoc55kg0=; b=r0IRjGyrHZweHFPnek/JgNWE/wcyRirOd515KNxUxfs/sJ5ru8CS7t+lgxiijYWx4B F8W6J0xWZB7DLfVov511aG30uPdNGfgYe/ZAEUrJ76YqFFeRcKIHMJXdhQem24kkSYOU TFUs6KqxjpFCisMambweZBN5LuwC0QU/jMckG0/q9hlJfLJNbdoG49o1QJuvqwlk1foB nzDPWHdIMTsidRx1J/ymBne+BP+DBg7QrYiwZrEl7HGSnQc7m2mRNeHIGKW71L30/0gf cpjnEcYpmnf9X2KzoGq68Cj+u9juKSN+33kZnzMxkkG7Gz4nIl+zIn0x2jEmnthXMOK4 krNg== X-Gm-Message-State: AOJu0Yx+nWu1JSVHdpH7Y6PL0jBjw6R37pIMO2C3lYsRDaZTJ/d4zOQ8 sLaMtLI2mKr4oZU9YtxRdcbSldbMl1ErIqMiM9ZYYLSmlYwdpjdgiHMetODr4AhdCzfNSWw9F2Z N67DK6mXIUTbTWoYKu5FSMb8DdOa7yi/ZXQU6zphjY9b7y/eTWCso4b0RpNqfTMoC63iRCuZ4W+ 1oDd3Gcukm5W7ts2uuWRdpyb3ie5Q/F5wFgU8nEQ== X-Received: by 2002:a50:8a9d:0:b0:57c:5996:cfc8 with SMTP id 4fb4d7f45d1cf-57c5996d144mr895187a12.5.1717762407413; Fri, 07 Jun 2024 05:13:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFKBsFZwClxus+JrWc9SgKhVaaAbdYsw8OFYRiZ4b2MS0FtOT7b/YnBoNy7VJqLRHKB3QDctA== X-Received: by 2002:a50:8a9d:0:b0:57c:5996:cfc8 with SMTP id 4fb4d7f45d1cf-57c5996d144mr895167a12.5.1717762406860; Fri, 07 Jun 2024 05:13:26 -0700 (PDT) Received: from tpp.orcam.me.uk (tpp.orcam.me.uk. [81.187.245.177]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57aae1042cfsm2659822a12.46.2024.06.07.05.13.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 05:13:26 -0700 (PDT) Date: Fri, 7 Jun 2024 13:13:24 +0100 (BST) From: "Maciej W. Rozycki" To: libc-alpha@sourceware.org cc: Carlos O'Donell Subject: [PATCH] stdio-common: Add test for vfscanf with matches longer than INT_MAX [BZ #27650] Message-ID: <7bd09b7b-bbd4-ac32-02d5-687ec5e01986@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_ASCII_DIVIDERS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Complement commit b03e4d7bd25b ("stdio: fix vfscanf with matches longer than INT_MAX (bug 27650)") and add a test case for the issue, inspired by the reproducer provided with the bug report. This has been verified to succeed as from the commit referred and fail beforehand. As the test requires 2GiB of data to be passed around its performance has been evaluated using a choice of systems and the execution time determined to be respectively in the range of 10s for POWER9@2.166GHz, 28s for FU740@1.2GHz, and 48s for 74Kf@950MHz. As this is on the verge of and beyond the default timeout it has been increased by the factor of 8. Regardless, following recent practice the test has been added to the standard rather than extended set. --- Hi, This has been verified with the `powerpc64le-linux-gnu' (IBM POWER9) native target and then the same host and the `riscv64-linux-gnu' (SiFive FU740) and `mips-linux-gnu' (o32 ABI) (MIPS 74Kf) targets. This is so as to assess performance requirements for the test case. And with respect to these I have been referred off list to: for the choice between the standard and the extended set of tests. Any questions, comments or concerns? Otherwise OK to apply? Maciej --- stdio-common/Makefile | 2 stdio-common/tst-scanf-bz27650.c | 216 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 218 insertions(+) glibc-tst-scanf-bz27650.diff Index: glibc/stdio-common/Makefile =================================================================== --- glibc.orig/stdio-common/Makefile +++ glibc/stdio-common/Makefile @@ -244,6 +244,7 @@ tests := \ tst-scanf-binary-c23 \ tst-scanf-binary-gnu11 \ tst-scanf-binary-gnu89 \ + tst-scanf-bz27650 \ tst-scanf-intn \ tst-scanf-round \ tst-scanf-to_inpunct \ @@ -314,6 +315,7 @@ generated += \ tst-printf-fp-free.mtrace \ tst-printf-fp-leak-mem.out \ tst-printf-fp-leak.mtrace \ + tst-scanf-bz27650.mtrace \ tst-vfprintf-width-prec-mem.out \ tst-vfprintf-width-prec.mtrace \ # generated Index: glibc/stdio-common/tst-scanf-bz27650.c =================================================================== --- /dev/null +++ glibc/stdio-common/tst-scanf-bz27650.c @@ -0,0 +1,216 @@ +/* Test for BZ #27650, formatted input matching beyond MAX_INT. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +/* Produce a stream of more than MAX_INT characters to stdout of which + none is the new line character. This is executed as a subprocess + and the caller wants a void callee, upon the return from which the + process will terminate successfully, so in the case of a failure we + need to explicitly call exit with the failure status. */ + +static void +do_write (void *arg) +{ + static const char s[] = { [0 ... 4095] = 'a' }; + size_t i; + + for (i = 0; i <= INT_MAX / sizeof (s); i++) + if (fwrite (s, 1, sizeof (s), stdout) != sizeof (s)) + { + int err = errno; + + /* Close our stdout so that there's no risk for us to block + while `fscanf' is waiting on our stdout in `do_read' and + nothing checking our stderr. If closing has failed, then + refrain from reporting anything, for the same reason. */ + if (fclose (stdout) == 0) + error (0, err, "%s: fwrite: output error", __func__); + exit (EXIT_FAILURE); + } +} + +/* Consume a stream of more than MAX_INT characters from IN of which + none is the new line character. The call to fscanf is supposed + to complete upon the EOF condition on IN, however in the presence + of BZ #27650 it will terminate prematurely with characters still + outstanding in IN. Diagnose the condition and return status + accordingly. */ + +static int +do_read (FILE *in) +{ + int v; + + v = fscanf (in, "%*[^\n]"); + if (v == EOF || errno != 0) + { + error (0, errno, "%s: fscanf: input failure", __func__); + return EXIT_FAILURE; + } + + if (!feof (in)) + { + v = fgetc (in); + if (v == EOF) + error (0, errno, "%s: fgetc: input failure", __func__); + else if (v == '\n') + error (0, 0, "%s: unexpected new line character received", __func__); + else + error (0, 0, + "%s: character received after end of file expected: \\x%02x", + __func__, v); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} + +/* Run do_write in a subprocess and communicate its output produced to + stdout via a pipe to do_read. Upon completion of do_read consume + any outstanding input from do_write and report any issues. Return + success or failure based on the status of the subprocess and ours. */ + +int +do_test (void) +{ + struct support_subprocess target; + FILE *chdout, *chderr; + int chdstatus; + int status; + + mtrace (); + + target = support_subprocess (do_write, NULL); + chdout = fdopen (target.stdout_pipe[0], "r"); + if (chdout == NULL) + { + error (0, errno, "fdopen"); + status = EXIT_FAILURE; + } + else + status = do_read (chdout); + + /* Switch the pipes to the non-blocking mode to make sure do_write + does not lock up waiting output and consume any outstanding input + received. Discard any output from do_write's stdout and pass any + output from do_write's stderr along to our stderr. */ + if (fcntl (target.stdout_pipe[0], F_SETFL, O_NONBLOCK) == -1 + || fcntl (target.stderr_pipe[0], F_SETFL, O_NONBLOCK) == -1) + { + error (0, errno, "fcntl (F_SETFL)"); + status = EXIT_FAILURE; + } + else if (chdout != NULL) + { + chderr = fdopen (target.stderr_pipe[0], "r"); + if (chderr != NULL) + { + struct pollfd fds[] = + { { .fd = target.stderr_pipe[0], .events = POLLIN }, + { .fd = target.stdout_pipe[0], .events = POLLIN } }; + FILE *ss[array_length (fds)][2] = + { { chderr, stderr }, { chdout } }; + int pollstatus; + size_t i; + + while ((pollstatus = poll (fds, array_length (fds), -1)) >= 0) + { + bool stop; + + stop = false; + for (i = 0; i < array_length (fds); i++) + { + char buf[1024]; + char *s; + + if (fds[i].revents & POLLERR) + fds[i].fd = -1; + else if (fds[i].revents & POLLIN) + do + { + s = fgets (buf, sizeof (buf), ss[i][0]); + if (s != NULL) + { + if (ss[i][1] != NULL + && fputs (buf, ss[i][1]) == EOF) + { + error (0, errno, "fputs"); + status = EXIT_FAILURE; + stop = true; + } + } + else if (errno == EAGAIN) + clearerr (chderr); + else + { + error (0, errno, "fgets"); + status = EXIT_FAILURE; + stop = true; + } + } + while (s != NULL); + else if (fds[i].revents & POLLHUP) + fds[i].fd = -1; + } + if (stop) + break; + + stop = true; + for (i = 0; i < array_length (fds); i++) + if (fds[i].fd >= 0) + { + stop = false; + break; + } + if (stop) + break; + } + if (pollstatus < 0) + { + error (0, errno, "poll"); + status = EXIT_FAILURE; + } + } + } + + /* Combine our subprocess's status and intended ours. Only succeed + if both are good. */ + chdstatus = support_process_wait (&target); + if (status == EXIT_SUCCESS && WIFEXITED (chdstatus)) + return WEXITSTATUS (chdstatus); + else if (status != EXIT_SUCCESS) + return status; + else + return EXIT_FAILURE; +} + +#define TIMEOUT (DEFAULT_TIMEOUT * 8) +#include