From patchwork Wed Sep 6 10:49:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Edwards X-Patchwork-Id: 75360 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 5D261385841A for ; Wed, 6 Sep 2023 10:50:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5D261385841A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1693997410; bh=CEgGtEMQdpwyaJugKPPcVsldkkzS2AXqmT2KUDdMB88=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=r7oioFaROzxO2x7piPT7fWnSN4hUJNGmm1z1F+e8frKLO1ljkPr0iLKlLp+5JndMZ U/e6p+CFoFvDR+uPApA3rZpddSB02GYbs5z+qpuIJzfurmyTk8fG0yzyJsDVGbCS4s HakYRdz6GD55a4bi+rn6YSdcIaDQjzc6v9plIvL0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id 397AA3858C3A for ; Wed, 6 Sep 2023 10:49:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 397AA3858C3A Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-31c93d2a24fso2766253f8f.2 for ; Wed, 06 Sep 2023 03:49:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693997385; x=1694602185; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CEgGtEMQdpwyaJugKPPcVsldkkzS2AXqmT2KUDdMB88=; b=WFmPMLD53GgrSkzjqyZ1vYfbDFWfQDW1c8feFR81RXkvfCMqP1Ahd3fdZFyd0Jq/+F A9UBUzHc2onNCKLkpKIgKGZLJOgZ4fYA7Ydq65X4U1HOWEGzTc+V9/VpFOMflmAsqKBE cTTLrg0OTwmcvo05OmguEzkuTEYRgcazcNpTDD0HVj118OCEW4Cki1E27k9RLe94gJST p9rcxddFTE11qXpYBqTCHMJUd+kg9Nlc6IG5VWrHnzanWnHwmgD6fGhUqP2mgJPgE0qK gSamvA8iWd3gd9Z3S3Pgkvbjm/5a4nE9vMP6iWC2EQtlGXH1oD7qm0WUFvREZv0PEHYo 9/mQ== X-Gm-Message-State: AOJu0Yx8QL+kgK+2DqD8nmV6nU7DKFh5VqEgs66RgO1ZpXrHVwGZhoWW cu+cfzvKUwz20gAIsQhwnP7PG04GA/Bidg4EEg0= X-Google-Smtp-Source: AGHT+IHg60EzwyVMACnIqsRhRUGAqfQFYTgYDx+sDWt2GfuAF7FezCe1bDeZhAQ+UcsFL5QSKgh48g== X-Received: by 2002:a5d:6b01:0:b0:314:1ce9:3c86 with SMTP id v1-20020a5d6b01000000b003141ce93c86mr2381932wrw.0.1693997385692; Wed, 06 Sep 2023 03:49:45 -0700 (PDT) Received: from AWAVAUATUSH.aristanetworks.com ([46.7.23.185]) by smtp.gmail.com with ESMTPSA id l16-20020adfe9d0000000b00317e77106dbsm20021357wrn.48.2023.09.06.03.49.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Sep 2023 03:49:44 -0700 (PDT) To: libc-alpha@sourceware.org Cc: Peter Edwards Subject: [PATCH] elf: Fix memory leaks for dl_scope_free_list (bug 26641) Date: Wed, 6 Sep 2023 11:49:39 +0100 Message-ID: <20230906104939.718623-1-peadar@arista.com> X-Mailer: git-send-email 2.42.0.111.gd814540bb7 MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Peter Edwards via Libc-alpha From: Peter Edwards Reply-To: Peter Edwards Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" _dl_scope_free is used to maintain a cache of scope lists, so a number of them can be free'd all at once (thus avoiding contention on a lock) Once 50 scopes have been accumulated, the next call to _dl_scope_free will free the accumulated 50 entries. However, it does *not* free the just-passed entry. Secondly, there's a __libc_freeres function for releasing the _dl_scope_free list when tearing down libc's innards. This just free's the actual list, but not its content. Update it to also free the content. This fixes valgrind observed leaks from dl_open_worker where these lists are allocated. Signed-off-by: Peter Edwards --- elf/dl-libc.c | 9 +++++++-- elf/dl-scope.c | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/elf/dl-libc.c b/elf/dl-libc.c index c12e52f330..0b2e30ea13 100644 --- a/elf/dl-libc.c +++ b/elf/dl-libc.c @@ -329,7 +329,12 @@ __dl_libc_freemem (void) malloc), and in the static library it's in .bss space. */ free_slotinfo (&GL(dl_tls_dtv_slotinfo_list)->next); - void *scope_free_list = GL(dl_scope_free_list); + struct dl_scope_free_list *scope_free_list = GL(dl_scope_free_list); GL(dl_scope_free_list) = NULL; - free (scope_free_list); + if (scope_free_list != NULL) + { + while (scope_free_list->count > 0) + free (scope_free_list->list[--scope_free_list->count]); + free (scope_free_list); + } } diff --git a/elf/dl-scope.c b/elf/dl-scope.c index 2d4653c6e6..dba77068bd 100644 --- a/elf/dl-scope.c +++ b/elf/dl-scope.c @@ -51,6 +51,7 @@ _dl_scope_free (void *old) THREAD_GSCOPE_WAIT (); while (fsl->count > 0) free (fsl->list[--fsl->count]); + free (old); return 1; } return 0;