public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
From: Andreas Jaeger <aj@suse.de>
To: libc-hacker@sources.redhat.com
Subject: Fix BZ#3112: Memory leak in backtrace
Date: Sat, 27 Oct 2007 15:26:00 -0000	[thread overview]
Message-ID: <878x5opoim.fsf@suse.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 4829 bytes --]


The appended patch frees the dlopened object if we check for memory
leaks.

Tested on Linux/x86-64 (which uses the ia64 code).  ok to commit?

Andreas

2007-10-27  Andreas Jaeger  <aj@suse.de>

	[BZ #3112]
	* sysdeps/ia64/backtrace.c (init): Free shared library.
	(__cleanup): Free shared library when exiting.
	* sysdeps/i386/backtrace.c (init): Free shared library.
	(__cleanup): Free shared library when exiting.

============================================================
Index: sysdeps/i386/backtrace.c
--- sysdeps/i386/backtrace.c	14 Jun 2005 15:54:05 -0000	1.7
+++ sysdeps/i386/backtrace.c	27 Oct 2007 15:02:27 -0000
@@ -1,5 +1,5 @@
 /* Return backtrace of current program state.
-   Copyright (C) 1998, 2000, 2003, 2004, 2005 Free Software Foundation, Inc.
+   Copyright (C) 1998, 2000, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
 
@@ -36,21 +36,26 @@ static _Unwind_Reason_Code (*unwind_back
 static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
 static _Unwind_Ptr (*unwind_getcfa) (struct _Unwind_Context *);
 static _Unwind_Ptr (*unwind_getgr) (struct _Unwind_Context *, int);
+static void *libgcc_handle;
 
 static void
 init (void)
 {
-  void *handle = __libc_dlopen ("libgcc_s.so.1");
+  libgcc_handle = __libc_dlopen ("libgcc_s.so.1");
 
-  if (handle == NULL)
+  if (libgcc_handle == NULL)
     return;
 
-  unwind_backtrace = __libc_dlsym (handle, "_Unwind_Backtrace");
-  unwind_getip = __libc_dlsym (handle, "_Unwind_GetIP");
-  unwind_getcfa = __libc_dlsym (handle, "_Unwind_GetCFA");
-  unwind_getgr = __libc_dlsym (handle, "_Unwind_GetGR");
+  unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
+  unwind_getip = __libc_dlsym (libgcc_handle, "_Unwind_GetIP");
+  unwind_getcfa = __libc_dlsym (libgcc_handle, "_Unwind_GetCFA");
+  unwind_getgr = __libc_dlsym (libgcc_handle, "_Unwind_GetGR");
   if (unwind_getip == NULL || unwind_getgr == NULL || unwind_getcfa == NULL)
-    unwind_backtrace = NULL;
+    {
+      unwind_backtrace = NULL;
+      __libc_dlclose (libgcc_handle);
+      libgcc_handle = NULL;
+    }
 }
 #else
 # define unwind_backtrace _Unwind_Backtrace
@@ -142,3 +147,16 @@ __backtrace (array, size)
 }
 weak_alias (__backtrace, backtrace)
 libc_hidden_def (__backtrace)
+
+#ifdef SHARED
+static void
+__cleanup (void)
+{
+  if (libgcc_handle)
+    __libc_dlclose (libgcc_handle);
+}
+
+/* Make sure the shared object is freed if we want to free everything
+   before exiting.  */
+text_set_element (__libc_subfreeres, __cleanup);
+#endif
============================================================
Index: sysdeps/ia64/backtrace.c
--- sysdeps/ia64/backtrace.c	14 Jun 2005 15:54:05 -0000	1.3
+++ sysdeps/ia64/backtrace.c	27 Oct 2007 15:02:27 -0000
@@ -1,5 +1,5 @@
 /* Return backtrace of current program state.
-   Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
 
@@ -33,19 +33,24 @@ struct trace_arg
 #ifdef SHARED
 static _Unwind_Reason_Code (*unwind_backtrace) (_Unwind_Trace_Fn, void *);
 static _Unwind_Ptr (*unwind_getip) (struct _Unwind_Context *);
+static void *libgcc_handle;
 
 static void
 init (void)
 {
-  void *handle = __libc_dlopen ("libgcc_s.so.1");
+  libgcc_handle = __libc_dlopen ("libgcc_s.so.1");
 
-  if (handle == NULL)
+  if (libgcc_handle == NULL)
     return;
 
-  unwind_backtrace = __libc_dlsym (handle, "_Unwind_Backtrace");
-  unwind_getip = __libc_dlsym (handle, "_Unwind_GetIP");
+  unwind_backtrace = __libc_dlsym (libgcc_handle, "_Unwind_Backtrace");
+  unwind_getip = __libc_dlsym (libgcc_handle, "_Unwind_GetIP");
   if (unwind_getip == NULL)
-    unwind_backtrace = NULL;
+    {
+      unwind_backtrace = NULL;
+      __libc_dlclose (libgcc_handle);
+      libgcc_handle = NULL;
+    }
 }
 #else
 # define unwind_backtrace _Unwind_Backtrace
@@ -91,3 +96,16 @@ __backtrace (array, size)
 }
 weak_alias (__backtrace, backtrace)
 libc_hidden_def (__backtrace)
+
+#ifdef SHARED
+static void
+__cleanup (void)
+{
+  if (libgcc_handle)
+    __libc_dlclose (libgcc_handle);
+}
+
+/* Make sure the shared object is freed if we want to free everything
+   before exiting.  */
+text_set_element (__libc_subfreeres, __cleanup);
+#endif

-- 
 Andreas Jaeger, Director Platform / openSUSE, aj@suse.de
  SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg)
   Maxfeldstr. 5, 90409 Nürnberg, Germany
    GPG fingerprint = 93A3 365E CE47 B889 DF7F  FED1 389A 563C C272 A126

[-- Attachment #2: Type: application/pgp-signature, Size: 193 bytes --]

             reply	other threads:[~2007-10-27 15:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-27 15:26 Andreas Jaeger [this message]
2007-10-28  1:30 ` Ulrich Drepper

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=878x5opoim.fsf@suse.de \
    --to=aj@suse.de \
    --cc=libc-hacker@sources.redhat.com \
    /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).