Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Joel Brobecker <brobecker@adacore.com>
To: Tom Tromey <tromey@redhat.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH 13/40] fix cleanup handling in macho_symfile_read
Date: Mon, 03 Jun 2013 13:32:00 -0000	[thread overview]
Message-ID: <20130603133214.GA23924@adacore.com> (raw)
In-Reply-To: <668178b2c763a59e4651dabd10ee38fdddc37d2a.1368124285.git.tromey@redhat.com>

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

Hi Tom,

On Thu, May 09, 2013 at 12:51:10PM -0600, Tom Tromey wrote:
> macho_symfile_read leaks a cleanup by assigning to 'back_to' too late.
> 
> 	* machoread.c (macho_symfile_read): Assign first cleanup to
> 	'back_to'.

This patch exposes what looks like a fairly awful memory interdependency
between macho_symfile_read and macho_symfile_read_all_oso via
macho_symtab_read/macho_register_oso.  By doing this, which looks
completely correct, we're actually causing the memory to be released
earlier, which then creates dangling pointers, thus causing a SEGV
during symbol reading. Below is the analysis I submitted internally.

I am planning on looking deeper at how to make things cleaner, and
avoid the use of a global variable for that. But my day is almost
over, and I think it's a significant-enough regression that it's
worth reverting until I get this fixed, which should hopefully be
a day or two at most (a side-effect of the current approach is that
it is harder to follow compared to local variables)! Attached is
the revert, tested on x86_64-darwin - so sorry for undoing this
work, but I will fix things ASAP.

The analysis file at AdaCore:
| Which fixes a cleanup leak, but in the process exposes a memory
| management problem. More precisely, macho_symfile_read allocates
| an array of asymbol's, and installs a cleanup:
| 
|           symbol_table = (asymbol **) xmalloc (storage_needed);
|           back_to = make_cleanup (xfree, symbol_table);
| 
| The old code was not saving the cleanup handle returned by
| make_cleanup (thus the leak). The problem is that the code
| then calls macho_symtab_read before calling do_cleanups:
| 
|           macho_symtab_read (objfile, symcount, symbol_table);
| 
|           install_minimal_symbols (objfile);
|           do_cleanups (back_to);
| 
| This is when things go wrong, because macho_symtab_read registers
| the symbols from SYMBOL_TABLE via macho_register_oso.  The latter
| just stacks the list of OSOs to be processed later, via the call
| to macho_symfile_read_all_oso at the end of macho_symfile_read.
| At that point in time, the cleanup has been done and the data saved
| by macho_symtab_read is now stale.
| 
| Attached is a patch that extends the lifetime of all cleanups to
| the entire function, rather than just the sub-block. This side-steps
| the issue mostly successfully, but I still get a failure in
| segv_on_reload:
| 
|     (gdb) run
|     `/private/var/folders/zz/zyxvpxvq6csfxvn_n00001zc0000gv/T/gdb-TS-LD8OGZ/D109
| +-003__segv_on_reload/g' has changed; re-reading symbols.
|     gdb(66092) malloc: *** error for object 0x7fdf41000ed0: pointer being freed
| +was not allocated
| 
| I think the memory management model between the various functions
| should be more explicit anyway, rather than depend on a global
| variable. So I will work on that.

For the record, you patch was:

> --- a/gdb/machoread.c
> +++ b/gdb/machoread.c
> @@ -871,10 +871,10 @@ macho_symfile_read (struct objfile *objfile, int symfile_flags)
>            struct cleanup *back_to;
>  
>  	  symbol_table = (asymbol **) xmalloc (storage_needed);
> -	  make_cleanup (xfree, symbol_table);
> +	  back_to = make_cleanup (xfree, symbol_table);
>  
>            init_minimal_symbol_collection ();
> -          back_to = make_cleanup_discard_minimal_symbols ();
> +          make_cleanup_discard_minimal_symbols ();
>  
>  	  symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);

This is what I checked in:

gdb/Changelog:

        Revert:
        * machoread.c (macho_symfile_read): Assign first cleanup to
        'back_to'.

-- 
Joel

[-- Attachment #2: 0001-Revert-fix-cleanup-handling-in-macho_symfile_read.patch --]
[-- Type: text/x-diff, Size: 1678 bytes --]

From 47ca19ca1b982adb3e63f00f6e132942a2e624dc Mon Sep 17 00:00:00 2001
From: Joel Brobecker <brobecker@adacore.com>
Date: Mon, 3 Jun 2013 17:19:51 +0400
Subject: [PATCH] Revert "fix cleanup handling in macho_symfile_read"

This patch indirectly causes a SEGV by creating a dangling pointer.
Reverting this patch while working on a clearer memory management
method for this part of the code.

gdb/Changelog:

        Revert:
        * machoread.c (macho_symfile_read): Assign first cleanup to
        'back_to'.
---
 gdb/ChangeLog   |    6 ++++++
 gdb/machoread.c |    4 ++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 8649045..906cb06 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2013-06-03  Joel Brobecker  <brobecker@adacore.com>
+
+	Revert (indirectly causes a SIGSEGV):
+	* machoread.c (macho_symfile_read): Assign first cleanup to
+	'back_to'.
+
 2013-06-03  Yao Qi  <yao@codesourcery.com>
 
 	* mi/mi-cmd-var.c (mi_no_values, mi_simple_values): Move to
diff --git a/gdb/machoread.c b/gdb/machoread.c
index d294960..9877f07 100644
--- a/gdb/machoread.c
+++ b/gdb/machoread.c
@@ -871,10 +871,10 @@ macho_symfile_read (struct objfile *objfile, int symfile_flags)
           struct cleanup *back_to;
 
 	  symbol_table = (asymbol **) xmalloc (storage_needed);
-	  back_to = make_cleanup (xfree, symbol_table);
+	  make_cleanup (xfree, symbol_table);
 
           init_minimal_symbol_collection ();
-          make_cleanup_discard_minimal_symbols ();
+          back_to = make_cleanup_discard_minimal_symbols ();
 
 	  symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
 
-- 
1.7.10.4


  reply	other threads:[~2013-06-03 13:32 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-09 18:47 [PATCH 00/40] add cleanup checker and fix cleanup bugs Tom Tromey, Tom Tromey
2013-05-09 18:48 ` [PATCH 01/40] add the cleanup checker Tom Tromey, Tom Tromey
2013-05-17 16:15   ` Tom Tromey
2013-05-09 18:49 ` [PATCH 02/40] some cleanup checker fixes Tom Tromey, Tom Tromey
2013-05-09 18:49 ` [PATCH 06/40] remove erroneous return from setup_user_args Tom Tromey, Tom Tromey
2013-05-09 18:49 ` [PATCH 03/40] fix print_command_1 Tom Tromey, Tom Tromey
2013-05-09 18:49 ` [PATCH 05/40] fix cleanup buglet in ada-lang.c Tom Tromey, Tom Tromey
2013-05-30 16:31   ` Tom Tromey
2013-05-09 18:49 ` [PATCH 04/40] fix cleanups in som_symtab_read Tom Tromey, Tom Tromey
2013-05-09 18:50 ` [PATCH 08/40] fix up cleanup handling in internal_vproblem Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 20/40] fix py-prettyprint.c Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 23/40] fix one bug in symfile.c Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 13/40] fix cleanup handling in macho_symfile_read Tom Tromey, Tom Tromey
2013-06-03 13:32   ` Joel Brobecker [this message]
2013-06-03 16:09     ` Tom Tromey
2013-06-04 13:30       ` Joel Brobecker
2013-06-11 10:21         ` [patch/Darwin] Fix cleanup leak in machoread.c:macho_symfile_read Joel Brobecker
2013-06-18 15:42           ` Tom Tromey
2013-05-09 18:51 ` [PATCH 12/40] fix cleanup handling in m32r_load Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 10/40] cleanup fixes for inf-ptrace.c Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 24/40] fix mipsread.c Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 17/40] simplify cli-logging.c for analysis Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 18/40] fix py-breakpoint.c Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 09/40] cleanup fixes for remote-mips.c Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 21/40] fix py-value.c Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 14/40] fix two buglets in breakpoint.c Tom Tromey, Tom Tromey
2013-05-13  2:33   ` Yao Qi
2013-05-13 14:58     ` Tom Tromey
2013-05-13 16:44     ` Tom Tromey
2013-05-14  0:37       ` Yao Qi
2013-05-17 16:19       ` Tom Tromey
2013-05-09 18:51 ` [PATCH 19/40] fix py-frame.c Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 16/40] fix varobj.c Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 15/40] make a cleanup unconditionally in tracepoint.c Tom Tromey, Tom Tromey
2013-05-30  9:23   ` Yao Qi
2013-05-30 13:23     ` Tom Tromey
2013-05-09 18:51 ` [PATCH 11/40] fix list_available_thread_groups Tom Tromey, Tom Tromey
2013-05-09 18:51 ` [PATCH 22/40] fix symtab.c Tom Tromey, Tom Tromey
2013-05-09 18:52 ` [PATCH 32/40] fix dbxread.c Tom Tromey, Tom Tromey
2013-05-09 18:52 ` [PATCH 27/40] fix cp-namespace.c Tom Tromey, Tom Tromey
2013-05-09 18:52 ` [PATCH 28/40] use explicit returns to avoid checker confusion Tom Tromey, Tom Tromey
2013-05-24  0:51   ` asmwarrior
2013-05-24  1:30     ` Tom Tromey
2013-05-24 15:41       ` Tom Tromey
2013-05-26 11:21         ` asmwarrior
2013-05-09 18:52 ` [PATCH 29/40] fix in solib-aix.c Tom Tromey, Tom Tromey
2013-05-09 18:52 ` [PATCH 33/40] fix mi-cmd-stack.c Tom Tromey, Tom Tromey
2013-05-09 18:52 ` [PATCH 31/40] fix source.c Tom Tromey, Tom Tromey
2013-05-09 18:52 ` [PATCH 26/40] fix top.c Tom Tromey, Tom Tromey
2013-05-09 18:52 ` [PATCH 34/40] fix cli-script.c Tom Tromey, Tom Tromey
2013-05-09 18:52 ` [PATCH 35/40] fix mi-cmd-var.c Tom Tromey, Tom Tromey
2013-05-17 16:16   ` Tom Tromey
2013-05-09 18:52 ` [PATCH 36/40] introduce dangling_cleanup attribute and change source to use it Tom Tromey, Tom Tromey
2013-05-17 16:17   ` Tom Tromey
2013-05-09 18:52 ` [PATCH 30/40] fix linux-thread-db.c Tom Tromey, Tom Tromey
2013-05-09 18:52 ` [PATCH 25/40] fix one bug in stabsread.c Tom Tromey, Tom Tromey
2013-05-09 18:53 ` [PATCH 37/40] fix breakpoint_1 Tom Tromey, Tom Tromey
2013-05-17 16:18   ` Tom Tromey
2013-05-09 18:53 ` [PATCH 38/40] some fixes to infrun.c Tom Tromey, Tom Tromey
2013-05-17 16:21   ` Tom Tromey
2013-05-09 18:53 ` [PATCH 40/40] fix up xml-support.c Tom Tromey, Tom Tromey
2013-05-30 17:41   ` Tom Tromey
2013-05-09 18:53 ` [PATCH 39/40] fix compile_rx_or_error Tom Tromey, Tom Tromey
2013-05-13  2:53   ` Yao Qi
2013-05-13 14:59     ` Tom Tromey
2013-05-30 17:39       ` Tom Tromey
2013-05-09 19:45 ` [PATCH 07/40] fix linespec bug noticed by the checker Tom Tromey, Tom Tromey
2013-05-09 21:21 ` [PATCH 00/40] add cleanup checker and fix cleanup bugs Phil Muldoon
2013-05-10 14:27   ` Tom Tromey
2013-05-10  8:23 ` Joel Brobecker
2013-05-10 14:34   ` Tom Tromey
2013-05-11 10:41     ` Joel Brobecker
2013-05-14 19:04       ` Tom Tromey
2013-05-30 16:17 ` Tom Tromey

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=20130603133214.GA23924@adacore.com \
    --to=brobecker@adacore.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@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