Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <andrew.burgess@embecosm.com>
To: Tom de Vries <tdevries@suse.de>
Cc: "Ludovic Courtès" <ludo@gnu.org>, gdb-patches@sourceware.org
Subject: Re: [PATCH][gdb/guile] Don't allow libguile to change libgmp mem fns
Date: Mon, 3 May 2021 12:18:42 +0100	[thread overview]
Message-ID: <20210503111842.GA6612@embecosm.com> (raw)
In-Reply-To: <20210503085428.GA20738@delia>

* Tom de Vries <tdevries@suse.de> [2021-05-03 10:54:29 +0200]:

> Hi,
> 
> Since gdb commit 880ae75a2b7 "gdb delay guile initialization until
> gdbscm_finish_initialization" I'm running into:
> ...
> (gdb) print My_Var > 10.0^M
> free(): invalid pointer^M
> ERROR: GDB process no longer exists
> GDB process exited with wait status 5995 exp9 0 0 CHILDKILLED SIGABRT SIGABRT
> UNRESOLVED: gdb.ada/fixed_cmp.exp: gnat_encodings=all: print My_Var > 10.0
> ...
> 
> The problem is that both gdb and libguile try to set the libgmp memory functions,
> and since the gdb commit the ones from libguile are effective, which results
> in gdb freeing some memory in a way that is not compatible with the way that
> memory was actually allocated.
> 
> The fact that libguile tries to set the libgmp memory functions is a bug which
> should be fixed starting version v3.0.6.
> 
> Meanwhile, work around this in gdb by not allowing libguile to set the libgomp
> memory functions.

Thanks for looking into this, and sorry for causing the breakage.

I had a read through the bug, and this solution seems to make sense,
however, I had two thoughts.

First, I already had to solve a similar problem for Python when doing
this work.  For Python the issue related to which signal handlers were
installed.

Though the problem was Python specific, I figured that it was cleaner
to make the solution generic, so I placed the fix in
gdb/extension.c:ext_lang_initialization - look for the use of
scoped_default_sigint.

I wonder if this fix should similarly be placed at this level?  I'll
leave this choice up to you, I don't feel strongly on this, but
thought it might be worth mentioning.

For the second thought, see below...

> 
> Tested on x86_64-linux.
> 
> Any comments?
> 
> Thanks,
> - Tom
> 
> [gdb/guile] Don't allow libguile to change libgmp mem fns
> 
> gdb/ChangeLog:
> 
> 2021-05-03  Tom de Vries  <tdevries@suse.de>
> 
> 	PR guile/27806
> 	* guile/guile.c (gdbscm_initialize): Save and restore libgmp memory
> 	functions.
> 
> ---
>  gdb/guile/guile.c | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
> 
> diff --git a/gdb/guile/guile.c b/gdb/guile/guile.c
> index bdf15cd498b..6ee8b3f47ce 100644
> --- a/gdb/guile/guile.c
> +++ b/gdb/guile/guile.c
> @@ -662,10 +662,32 @@ gdbscm_initialize (const struct extension_language_defn *extlang)
>    {
>      gdb::block_signals blocker;
>  
> +    /* There are libguile versions (f.i. v3.0.5) that by default call
> +       mp_get_memory_functions during initialization to install custom
> +       libgmp memory functions.  This is considered a bug and should be
> +       fixed starting v3.0.6.
> +       Before gdb commit 880ae75a2b7 "gdb delay guile initialization until
> +       gdbscm_finish_initialization", that bug had no effect for gdb,
> +       because gdb subsequently called mp_get_memory_functions to install
> +       its own custom functions in _initialize_gmp_utils.  However, since
> +       aforementioned gdb commit the initialization order is reversed,
> +       allowing libguile to install a custom malloc that is incompatible
> +       with the custom free as used in gmp-utils.c, resulting in a
> +       "double free or corruption (out)" error.
> +       Work around the libguile bug by saving the libgmp memory functions
> +       before guile initialization, and restoring them afterwards.  */
> +    void *(*alloc_func) (size_t);
> +    void *(*realloc_func) (void *, size_t, size_t);
> +    void (*free_func) (void *, size_t);
> +    mp_get_memory_functions (&alloc_func, &realloc_func, &free_func);

I think any time we do SAVE-VALUE -> WORK -> RESTORE-VALUE, we should
be wrapping this up in an RAII class.  Right now I don't believe
scm_with_guile can throw an exception, but you never know how the code
will change in the future, and creating an RAII class now just makes
things future proof.

Thanks,
Andrew

> +
>      /* scm_with_guile is the most portable way to initialize Guile.  Plus
>         we need to initialize the Guile support while in Guile mode (e.g.,
>         called from within a call to scm_with_guile).  */
>      scm_with_guile (call_initialize_gdb_module, NULL);
> +
> +    /* Restore libgmp memory functions.  */
> +    mp_set_memory_functions (alloc_func, realloc_func, free_func);
>    }
>  
>    /* Set Guile's backtrace to match the "set guile print-stack" default.

  reply	other threads:[~2021-05-03 11:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-03  8:54 Tom de Vries
2021-05-03 11:18 ` Andrew Burgess [this message]
2021-05-03 12:20   ` Tom de Vries
2021-05-03 20:10 ` Ludovic Courtès via Gdb-patches
2021-05-04  8:27   ` Tom de Vries

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=20210503111842.GA6612@embecosm.com \
    --to=andrew.burgess@embecosm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=ludo@gnu.org \
    --cc=tdevries@suse.de \
    /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