* Fix BZ#3112: Memory leak in backtrace
@ 2007-10-27 15:26 Andreas Jaeger
2007-10-28 1:30 ` Ulrich Drepper
0 siblings, 1 reply; 2+ messages in thread
From: Andreas Jaeger @ 2007-10-27 15:26 UTC (permalink / raw)
To: libc-hacker
[-- 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 --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Fix BZ#3112: Memory leak in backtrace
2007-10-27 15:26 Fix BZ#3112: Memory leak in backtrace Andreas Jaeger
@ 2007-10-28 1:30 ` Ulrich Drepper
0 siblings, 0 replies; 2+ messages in thread
From: Ulrich Drepper @ 2007-10-28 1:30 UTC (permalink / raw)
To: Andreas Jaeger; +Cc: libc-hacker
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
The patch is mostly correct. The cleanup function is defined
differently these days, though. I checked in a corrected version.
- --
â§ Ulrich Drepper â§ Red Hat, Inc. â§ 444 Castro St â§ Mountain View, CA â
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
iD8DBQFHI+YN2ijCOnn/RHQRApymAJwPSXCRs5ylJGrFzxUJ3L8gu5rt6wCaAkhL
G0Rej/B957AgsInsRso4tRE=
=sW2y
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-10-28 1:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-27 15:26 Fix BZ#3112: Memory leak in backtrace Andreas Jaeger
2007-10-28 1:30 ` Ulrich Drepper
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).