From patchwork Tue Jun 6 14:11:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Schwab X-Patchwork-Id: 1791213 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=u4/WEh0U; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QbC8f2gKWz20QH for ; Wed, 7 Jun 2023 00:12:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 420873857BB3 for ; Tue, 6 Jun 2023 14:12:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 420873857BB3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686060720; bh=JqPmkQafqzCQ/tM+j03wDypUEIoOqSImyTXk7Sda74Q=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=u4/WEh0Upa0BUFTiMrNjv1ScwXUydr9DOTlhYt8uDA00TBE1JIxkCH3Jb3A0moaoR /3MGZmZgTLDOm4ZFcICq5mXpeu+/BCEmGksJxrPZcfmiwSTzPKAipCx5++vpGefOES JMAi6KLXwUCySrRxuV/0wDeCUazWVETieymAVXF0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 36FA33857731 for ; Tue, 6 Jun 2023 14:11:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 36FA33857731 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 710DA219C3 for ; Tue, 6 Jun 2023 14:11:05 +0000 (UTC) Received: from hawking.suse.de (unknown [10.168.4.11]) by relay2.suse.de (Postfix) with ESMTP id 674902C141 for ; Tue, 6 Jun 2023 14:11:05 +0000 (UTC) Received: by hawking.suse.de (Postfix, from userid 17005) id 5B8904A0394; Tue, 6 Jun 2023 16:11:05 +0200 (CEST) To: libc-alpha@sourceware.org Subject: [PATCH] Always do locking when accessing streams (bug 15142, bug 14697) X-Yow: I'd like some JUNK FOOD... and then I want to be ALONE -- Date: Tue, 06 Jun 2023 16:11:05 +0200 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andreas Schwab via Libc-alpha From: Andreas Schwab Reply-To: Andreas Schwab Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Now that abort no longer calls fflush there is no reason to avoid locking the stdio streams anywhere. This fixes a conformance issue and potential heap corruption during exit. --- libio/genops.c | 43 ++++++++++--------------------------------- libio/libioP.h | 1 - 2 files changed, 10 insertions(+), 34 deletions(-) diff --git a/libio/genops.c b/libio/genops.c index 7131312704..fbd8dd9e75 100644 --- a/libio/genops.c +++ b/libio/genops.c @@ -682,7 +682,7 @@ _IO_adjust_column (unsigned start, const char *line, int count) libc_hidden_def (_IO_adjust_column) int -_IO_flush_all_lockp (int do_lock) +_IO_flush_all (void) { int result = 0; FILE *fp; @@ -695,8 +695,7 @@ _IO_flush_all_lockp (int do_lock) for (fp = (FILE *) _IO_list_all; fp != NULL; fp = fp->_chain) { run_fp = fp; - if (do_lock) - _IO_flockfile (fp); + _IO_flockfile (fp); if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base) || (_IO_vtable_offset (fp) == 0 @@ -706,8 +705,7 @@ _IO_flush_all_lockp (int do_lock) && _IO_OVERFLOW (fp, EOF) == EOF) result = EOF; - if (do_lock) - _IO_funlockfile (fp); + _IO_funlockfile (fp); run_fp = NULL; } @@ -718,14 +716,6 @@ _IO_flush_all_lockp (int do_lock) return result; } - - -int -_IO_flush_all (void) -{ - /* We want locking. */ - return _IO_flush_all_lockp (1); -} libc_hidden_def (_IO_flush_all) void @@ -791,6 +781,9 @@ _IO_unbuffer_all (void) { int legacy = 0; + run_fp = fp; + _IO_flockfile (fp); + #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1) if (__glibc_unlikely (_IO_vtable_offset (fp) != 0)) legacy = 1; @@ -800,18 +793,6 @@ _IO_unbuffer_all (void) /* Iff stream is un-orientated, it wasn't used. */ && (legacy || fp->_mode != 0)) { -#ifdef _IO_MTSAFE_IO - int cnt; -#define MAXTRIES 2 - for (cnt = 0; cnt < MAXTRIES; ++cnt) - if (fp->_lock == NULL || _IO_lock_trylock (*fp->_lock) == 0) - break; - else - /* Give the other thread time to finish up its use of the - stream. */ - __sched_yield (); -#endif - if (! legacy && ! dealloc_buffers && !(fp->_flags & _IO_USER_BUF)) { fp->_flags |= _IO_USER_BUF; @@ -825,17 +806,15 @@ _IO_unbuffer_all (void) if (! legacy && fp->_mode > 0) _IO_wsetb (fp, NULL, NULL, 0); - -#ifdef _IO_MTSAFE_IO - if (cnt < MAXTRIES && fp->_lock != NULL) - _IO_lock_unlock (*fp->_lock); -#endif } /* Make sure that never again the wide char functions can be used. */ if (! legacy) fp->_mode = -1; + + _IO_funlockfile (fp); + run_fp = NULL; } #ifdef _IO_MTSAFE_IO @@ -861,9 +840,7 @@ __libio_freemem (void) int _IO_cleanup (void) { - /* We do *not* want locking. Some threads might use streams but - that is their problem, we flush them underneath them. */ - int result = _IO_flush_all_lockp (0); + int result = _IO_flush_all (); /* We currently don't have a reliable mechanism for making sure that C++ static destructors are executed in the correct order. diff --git a/libio/libioP.h b/libio/libioP.h index d777553cb0..745278e076 100644 --- a/libio/libioP.h +++ b/libio/libioP.h @@ -537,7 +537,6 @@ extern int _IO_new_do_write (FILE *, const char *, size_t); extern int _IO_old_do_write (FILE *, const char *, size_t); extern int _IO_wdo_write (FILE *, const wchar_t *, size_t); libc_hidden_proto (_IO_wdo_write) -extern int _IO_flush_all_lockp (int); extern int _IO_flush_all (void); libc_hidden_proto (_IO_flush_all) extern void _IO_flush_all_linebuffered (void);