From patchwork Fri May 22 20:37:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos O'Donell X-Patchwork-Id: 39354 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B8AC3388B024; Fri, 22 May 2020 20:37:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B8AC3388B024 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1590179851; bh=3n7Vf0vsIVWQxyBIeNMVLjzlvafpfLm9itSvWD9p1cw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=HwhxYJdUTldTJpo06YKDSaApCXnqVGe8RFjVg3h2NrmKeALiftb/VNKtOZk72aCDg tai2+p8cRdFbAkZwkpP6/k1nSMilZD8wU0fHVy3iDR1VzMOAYl8awDR6BGfq8KzCqi psWBzjE21v/i1ZDLV1Mh73Eg0OjKcdj2/+IDlwO0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by sourceware.org (Postfix) with ESMTP id 3E027386F824 for ; Fri, 22 May 2020 20:37:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3E027386F824 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-143-ptSSaOz4McSnjUed2r7jOA-1; Fri, 22 May 2020 16:37:23 -0400 X-MC-Unique: ptSSaOz4McSnjUed2r7jOA-1 Received: by mail-qv1-f70.google.com with SMTP id q10so11792832qvs.16 for ; Fri, 22 May 2020 13:37:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:organization:message-id:date :user-agent:mime-version:content-language:content-transfer-encoding; bh=3n7Vf0vsIVWQxyBIeNMVLjzlvafpfLm9itSvWD9p1cw=; b=i0nJL0ZPPDrL+F+NThr1Qor7+hXfZkTcn4B03XdRAWXvAtAjrf93WPW04tiFEoTxqY wCQONPg8gOau3HOJTI42n9NIjQVt9OOdA7coPVrRfLpsYv9FdhykJkuNgcPWa6U6pVgK 8hw1MSFiIBxQyRe037Bxc45wMql1gJKb4RLpAMTPPO19d1YIHvYnrJY4bLc9C7Fk6OOj 5b1Ybjklc0AHLjorpW46Pr/8/8nz6P55nECxoorN1oWsWeGQIjARngZKSDrUBBhaXpMX hnYgWb7vAzH3M/LzGdwuipkiVUysVBQ51S7QWMNRtcSKUQHvvdwrVH+25qwMeHN5lgxT MpWw== X-Gm-Message-State: AOAM532RX5RuOg8DQ8oHyeyg7OqN78GnghlTWDAGHGMqvKKKejd+Hz9f yN3KxSZ60e2BJYlLbKlb+Mxt4+85w3Zh7GRm1twKq0cR5bo3w+2wih2sjuSAgYWmCQyR7mPT/W5 T8ZGXAUK2mCfILC1xSQVd X-Received: by 2002:a0c:eacb:: with SMTP id y11mr5552799qvp.141.1590179842884; Fri, 22 May 2020 13:37:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdk9WSNoD6D2PZCODbysZ0f0WEH57HG/tKRRQPQSt80E4/S0s2nk79mw5K/26EBMp4oMlUQA== X-Received: by 2002:a0c:eacb:: with SMTP id y11mr5552790qvp.141.1590179842597; Fri, 22 May 2020 13:37:22 -0700 (PDT) Received: from [192.168.1.4] (198-84-170-103.cpe.teksavvy.com. [198.84.170.103]) by smtp.gmail.com with ESMTPSA id f7sm8339286qtg.96.2020.05.22.13.37.21 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 22 May 2020 13:37:22 -0700 (PDT) To: libc-alpha Subject: [PATCH] Add comments to explain when a stream is freed by, __libc_freeres(). Organization: Red Hat Message-ID: <131703c8-e0b2-579b-8100-bbde41407f04@redhat.com> Date: Fri, 22 May 2020 16:37:20 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 Content-Language: en-US X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Carlos O'Donell via Libc-alpha From: Carlos O'Donell Reply-To: Carlos O'Donell Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" In bug 26022 a user reports that valgrind shows two unfreed allocations when using stderr. This is normal, we don't free unbuffered streams in __libc_freeres() because users expect that such streams will be available all the way through the process shutdown. To suppress this I've filed the following valgrind issue: https://bugs.kde.org/show_bug.cgi?id=421931 --- libio/genops.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/libio/genops.c b/libio/genops.c index 28419cc963..d533d47fd5 100644 --- a/libio/genops.c +++ b/libio/genops.c @@ -768,7 +768,7 @@ weak_alias (_IO_flush_all_linebuffered, _flushlbf) function sin the libc_freeres section. Those are called as part of the atexit routine, just like _IO_cleanup. The problem is we do not know whether the freeres code is called first or _IO_cleanup. - if the former is the case, we set the DEALLOC_BUFFER variable to + if the former is the case, we set the dealloc_buffers variable to true and _IO_unbuffer_all will take care of the rest. If _IO_unbuffer_all is called first we add the streams to a list which the freeres function later can walk through. */ @@ -796,8 +796,23 @@ _IO_unbuffer_all (void) legacy = 1; #endif + /* We free a stream if: + + (1) The stream is buffered and was used (_mode != 0), and + so the user can't expect any output to be printed during + shutdown since absolute order of destructors is not + explicitly guaranteed. + + (2) The stream is buffered and is a legacy stream used for + backwards compatibility with legacy libstdc++ + implementations. + + By default stderr starts unbuffered and so by default we + never process it here, and as such it is always available for + late destructors to use, and will not be freed by the + buffer_free function below (unless it is changed by the + user). */ if (! (fp->_flags & _IO_UNBUFFERED) - /* Iff stream is un-orientated, it wasn't used. */ && (legacy || fp->_mode != 0)) { #ifdef _IO_MTSAFE_IO @@ -844,11 +859,13 @@ _IO_unbuffer_all (void) #endif } - +/* Called by __libc_freeres() to free streams. */ libc_freeres_fn (buffer_free) { dealloc_buffers = true; + /* See the note in _IO_unbuffer_all about when a stream is + considered for freeing. */ while (freeres_list != NULL) { free (freeres_list->_freeres_buf);