From patchwork Thu Sep 26 07:54:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lumingxiang X-Patchwork-Id: 1989713 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=uclibc-ng.org (client-ip=2a00:1828:2000:679::23; helo=helium.openadk.org; envelope-from=devel-bounces@uclibc-ng.org; receiver=patchwork.ozlabs.org) Received: from helium.openadk.org (helium.openadk.org [IPv6:2a00:1828:2000:679::23]) (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 4XDmk42r4Sz1xt2 for ; Thu, 26 Sep 2024 18:20:10 +1000 (AEST) Received: from helium.openadk.org (localhost [IPv6:::1]) by helium.openadk.org (Postfix) with ESMTP id 611163535C44; Thu, 26 Sep 2024 10:19:57 +0200 (CEST) Received: from h3cspam02-ex.h3c.com (smtp.h3c.com [60.191.123.50]) by helium.openadk.org (Postfix) with ESMTPS id 8B0313520F61 for ; Thu, 26 Sep 2024 09:54:59 +0200 (CEST) Received: from mail.maildlp.com ([172.25.15.154]) by h3cspam02-ex.h3c.com with ESMTP id 48Q7sQpc050500 for ; Thu, 26 Sep 2024 15:54:26 +0800 (+08) (envelope-from lu.mingxiang@h3c.com) Received: from DAG6EX11-BJD.srv.huawei-3com.com (unknown [10.153.34.13]) by mail.maildlp.com (Postfix) with ESMTP id 04F2B2004738 for ; Thu, 26 Sep 2024 16:00:28 +0800 (CST) Received: from DAG6EX09-BJD.srv.huawei-3com.com (10.153.34.11) by DAG6EX11-BJD.srv.huawei-3com.com (10.153.34.13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.27; Thu, 26 Sep 2024 15:54:26 +0800 Received: from DAG6EX09-BJD.srv.huawei-3com.com ([fe80::bdc5:ad7:2347:12a5]) by DAG6EX09-BJD.srv.huawei-3com.com ([fe80::bdc5:ad7:2347:12a5%4]) with mapi id 15.02.1258.027; Thu, 26 Sep 2024 15:54:26 +0800 From: Lumingxiang To: "devel@uclibc-ng.org" Thread-Topic: TLS memory-leak with dlopen Thread-Index: AdsP3MseHvTyqndZQCyFtKwPxpzXvA== Date: Thu, 26 Sep 2024 07:54:26 +0000 Message-ID: <43a99bde957d42369c331f1bbff80c52@h3c.com> Accept-Language: en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.142.32.239] x-sender-location: DAG2 MIME-Version: 1.0 X-DNSRBL: X-SPAM-SOURCE-CHECK: pass X-MAIL: h3cspam02-ex.h3c.com 48Q7sQpc050500 X-MailFrom: lu.mingxiang@h3c.com X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation Message-ID-Hash: DORYISGSAEHNJV357HABEQJ4RWWTXMAO X-Message-ID-Hash: DORYISGSAEHNJV357HABEQJ4RWWTXMAO X-Mailman-Approved-At: Thu, 26 Sep 2024 10:19:35 +0200 CC: Zhangchun X-Mailman-Version: 3.3.3 Precedence: list Subject: [uclibc-ng-devel] TLS memory-leak with dlopen List-Id: uClibc-ng Development Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: If a shared library (so) is loaded using dlopen and utilizes thread-local storage (TLS), the memory will not be freed upon thread exit, resulting in a memory leak. This issue has been resolved in glibc-2.14. However, it remains unresolved in uClibc. The following patch is based on the patch made in glibc and has been validated as effective on the x86-64 architecture. Ref: https://sourceware.org/bugzilla/show_bug.cgi?id=12650 How to reproduce: ------------------------------------------------------------------------------------------------------------------------------------- 本邮件及其附件含有新华三集团的保密信息,仅限于发送给上面地址中列出 的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、 或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本 邮件! This e-mail and its attachments contain confidential information from New H3C, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by phone or email immediately and delete it! diff --git a/libpthread/nptl/allocatestack.c b/libpthread/nptl/allocatestack.c index 7ef884543..941ef22f0 100644 --- a/libpthread/nptl/allocatestack.c +++ b/libpthread/nptl/allocatestack.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -241,6 +242,10 @@ get_cached_stack (size_t *sizep, void **memp) /* Clear the DTV. */ dtv_t *dtv = GET_DTV (TLS_TPADJ (result)); + for (size_t cnt = 0; cnt < dtv[-1].counter; ++cnt) + if (! dtv[1 + cnt].pointer.is_static + && dtv[1 + cnt].pointer.val != TLS_DTV_UNALLOCATED) + free (dtv[1 + cnt].pointer.val); memset (dtv, '\0', (dtv[-1].counter + 1) * sizeof (dtv_t)); /* Re-initialize the TLS. */ diff --git a/libpthread/nptl/sysdeps/generic/dl-tls.c b/libpthread/nptl/sysdeps/generic/dl-tls.c index 7d25e4706..7b7991be8 100644 --- a/libpthread/nptl/sysdeps/generic/dl-tls.c +++ b/libpthread/nptl/sysdeps/generic/dl-tls.c @@ -45,8 +45,6 @@ to allow dynamic loading of modules defining IE-model TLS data. */ # define TLS_STATIC_SURPLUS 64 + DL_NNS * 100 -/* Value used for dtv entries for which the allocation is delayed. */ -# define TLS_DTV_UNALLOCATED ((void *) -1l) #ifndef SHARED extern dtv_t static_dtv; diff --git a/libpthread/nptl/sysdeps/x86_64/dl-tls.h b/libpthread/nptl/sysdeps/x86_64/dl-tls.h index d6c338cda..5cac55f33 100644 --- a/libpthread/nptl/sysdeps/x86_64/dl-tls.h +++ b/libpthread/nptl/sysdeps/x86_64/dl-tls.h @@ -26,3 +26,6 @@ typedef struct extern void *__tls_get_addr (tls_index *ti); + +/* Value used for dtv entries for which the allocation is delayed. */ +#define TLS_DTV_UNALLOCATED ((void *) -1l)