This is the mail archive of the libc-hacker@sourceware.org mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Fix BZ#3112: Memory leak in backtrace


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: pgp00000.pgp
Description: PGP signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]