public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "gyunaev at ulduzsoft dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/111347] New: Memory leak loading a shared library built with --static-libsdtc++ when version script is used
Date: Fri, 08 Sep 2023 21:58:54 +0000	[thread overview]
Message-ID: <bug-111347-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111347

            Bug ID: 111347
           Summary: Memory leak loading a shared library built with
                    --static-libsdtc++ when version script is used
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gyunaev at ulduzsoft dot com
  Target Milestone: ---

Created attachment 55860
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55860&action=edit
The source code of the library, tester and Makefile

Gcc 13.2 built from the source. Linux x86_64, OpenSuSe Leap 15.5.

Our team has found that when building a C++ DSO using --static-libgcc and
--static-libstdc++ and using a version script to hide the visibility of
symbols, loading and unloading the resulting shared library leaks memory each
time such dlopen/dlclose happens. 

If a version script is not used, there is no leak.

Smallest shared library source which reproduces the issue:

------ libtest.cpp ------
#include <string>

int test()
{
    std::string a;
    return a.length();
}
---

The version script:
------ libtest.ver ------
{
    global:
        test;

    local:
        *;
};
---

Built with:

g++ -fPIC -shared -static-libgcc -static-libstdc++
-Wl,--version-script=libtest.ver libtest.cpp -o libtest.so

load-unload of this library leaks memory each time it unloads.
Loading-unloading is all that is needed - you don't need to call test or even
dlsym it. Easiest to see it when running dlclose( dlopen( "./libtest.so",
RTLD_NOW)) in a loop.

If you remove --version-script and rebuild the library, load-unload will not
leak.

Experimentally we found out that adding the symbol "_ZNSt8messagesIwE2idE;"
into global: section of version script would stop the leak, but this isn't
obvious to me why. This symbol demangles into std::messages<wchar_t>::id;

Attaching the tester, makefile and the loader.

             reply	other threads:[~2023-09-08 21:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-08 21:58 gyunaev at ulduzsoft dot com [this message]
2023-09-08 22:01 ` [Bug libstdc++/111347] " pinskia at gcc dot gnu.org
2023-09-09  1:28 ` gyunaev at ulduzsoft dot com
2023-09-09  4:49 ` gyunaev at ulduzsoft dot com
2023-09-11 11:18 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-111347-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).