public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Andrew Burgess <aburgess@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH] gdb/python: avoid throwing an exception over libopcodes code
Date: Mon, 24 Oct 2022 14:20:42 -0400	[thread overview]
Message-ID: <3ac49ef4-4bcf-2abc-4e06-0c9f3b8c9991@simark.ca> (raw)
In-Reply-To: <87r0yx6sr7.fsf@redhat.com>

On 10/24/22 13:22, Andrew Burgess via Gdb-patches wrote:
>> Can we make gdbpy_disassembler::read_memory_func "noexcept", so that if
>> an exception escapes that function, regardless of the architecture,
>> there will be an apparent GDB crash?
> 
> Yes!  This was my first thought once I spotted this bug.
> 
> So, what I originally tried was this, in gdb/disasm.h, I applied this:
> 
> diff --git a/gdb/disasm.h b/gdb/disasm.h
> index b7d16744c20..7a217dfad12 100644
> --- a/gdb/disasm.h
> +++ b/gdb/disasm.h
> @@ -52,7 +52,9 @@ struct gdb_disassemble_info
>  protected:
>  
>    /* Types for the function callbacks within m_di.  */
> -  using read_memory_ftype = decltype (disassemble_info::read_memory_func);
> +  using read_memory_ftype
> +    = int (*) (bfd_vma, bfd_byte *, unsigned int,
> +              struct disassemble_info *) noexcept;
>    using memory_error_ftype = decltype (disassemble_info::memory_error_func);
>    using print_address_ftype = decltype (disassemble_info::print_address_func);
>    using fprintf_ftype = decltype (disassemble_info::fprintf_func);
> 
> What I was trying to do was require that _every_ read memory function
> that we have in GDB _must_ be noexcept.  This makes sense I think - the
> same issue I ran into here could just as easily crop up elsewhere if we
> tried to throw an exception.
> 
> Unfortunately, the above doesn't build, I run into this warning/error:
> 
>   In file included from ../../src/gdb/disasm.c:25:
>   ../../src/gdb/disasm.h: In constructor ‘gdb_printing_disassembler::gdb_printing_disassembler(gdbarch*, ui_file*, gdb_disassemble_info::read_memory_ftype, gdb_disassemble_info::memory_error_ftype, gdb_disassemble_info::print_address_ftype)’:
>   ../../src/gdb/disasm.h:116:3: error: mangled name for ‘gdb_printing_disassembler::gdb_printing_disassembler(gdbarch*, ui_file*, gdb_disassemble_info::read_memory_ftype, gdb_disassemble_info::memory_error_ftype, gdb_disassemble_info::print_address_ftype)’ will change in C++17 because the exception specification is part of a function type [-Werror=noexcept-type]
>     116 |   gdb_printing_disassembler (struct gdbarch *gdbarch,
>         |   ^~~~~~~~~~~~~~~~~~~~~~~~~
> 
> At this point I figured I couldn't use noexcept in the API like this, so
> gave up.

I think this warning could safely be silenced, since we don't care about
the mangling name change.

> But I think this was a mistake.  I just tried, and turns out I can add
> noexcept to gdbpy_disassembler::read_memory_func, and everything
> compiles fine, which, thinking about it more makes perfect sense.  The
> noexcept function is more restrictive, so passing it to a function
> pointer type that doesn't include noexcept can't hurt.

That's what I tried to, but your idea of putting it on read_memory_ftype
would be stronger, if it's possible.

> What I'd like to do though it create a single patch that adds noexcept
> to all the disassembler callbacks throughout GDB in one go.  I'll reply
> to this message once I have that patch ready, but if it's OK, I'll leave
> that as a separate change to the original patch?

Yeah sure.

>>
>>>
>>> This commit proposes an alternative solution that does not rely on
>>> throwing a C++ exception.
>>>
>>> When we spot an unhandled Python exception in frame #0, we will store
>>> the details of this exception within the gdbpy_disassembler object
>>> currently in use.  Then we return to libopcodes a value indicating
>>> that the memory_read failed.
>>>
>>> libopcodes will now continue to disassemble as though that memory read
>>> failed (with one special case described below), then, when we
>>> eventually return to disasmpy_builtin_disassemble we check to see if
>>> there is an exception stored in the gdbpy_disassembler object.  If
>>> there is then this exception can immediately be installed, and then we
>>> return back to Python, when the user will be able to catch the
>>> exception.
>>>
>>> There is one extra change in gdbpy_disassembler::read_memory_func.
>>> After the first call that results in an exception being stored on the
>>> gdbpy_disassembler object, any future calls to the ::read_memory_func
>>> function will immediately return as if the read failed.  This avoids
>>> any additional calls into user supplied Python code.
>>>
>>> My thinking here is that should the first call fail with some unknown
>>> error, GDB should not keep trying with any additional calls.  This
>>> maintains the illusion that the exception raised from
>>> MyInfo.read_memory is immediately raised by
>>> gdb.disassembler.builtin_disassemble.  I have no tests for this change
>>> though - to trigger this issue would rely on a libopcodes disassembler
>>> that will try to read further memory even after the first failed
>>> read.  I'm not aware of any such disassembler that currently does
>>> this, but that doesn't mean such a disassembler couldn't exist in the
>>> future.
>>
>> I don't really understand the need for this change.  The
>> read_memory_func interface should mostly be stateless, if you try to
>> read an invalid address, you get an error.  If you then try to read a
>> valid address, I don't see why that wouldn't work.
> 
> If the error/exception that occurs the first time is a gdb.MemoryError
> then I agree with you completely.
> 
> The question is about any other exception type.  Imagine this totally
> made up nonsense example:
> 
>   class MyInfo(gdb.disassembler.DisassembleInfo):
>       def __init__(self, info):
>           super().__init__(info)
> 
>       def read_memory(self, length, offset):
>           if read_resource_from_somewhere_remote():
>             return super().read_memory(length, offset)
>           else:
>             raise RemoteResourceUnavailableError()
> 
> If the disassembler tries to read memory, and for some reason the
> read_resource_from_somewhere_remote() call fails, we raise a
> RemoteResourceUnavailableError exception.
> 
> My expectation (and my desire for the API) is that the
> RemoteResourceUnavailableError will _immediately_ be re-raised by the
> corresponding builtin_disassemble call.
> 
> Without the line you are commenting on though, it is possible that the
> disassembler will repeatedly call the read_memory method, and so
> repeatedly raise the RemoteResourceUnavailableError error.
> 
> I don't know if I'm doing a good job of arguing my position :/ does the
> above make any sense?

I don't really understand the problem with raising
RemoteResourceUnavailableError repeatedly, if the disassembler calls
read_memory repeatedly.

Ah, I might get it now that I read your comment below.  Is that you have
these stack frames:

 0. gdb Python read_memory stack frames
 1. opcodes disassembler stack frames
 2. other gdb stack frames

The Python exception is stored in frames 0, and normally restored in
frames 2.  However, it could be that frames 0 store an exception, then
control goes back to frames 1, which handle the error return code, but
somehow think they can recover and so continue and call read_memory
again.  And so we in frames 0 again, but the packaged Python exception
from last time is still there.

If so, I think it would be fine to drop the original exception upon
entering frames 0 again.  It would be as if frames 1 caught the
exception (if it were a C++ exception) and decided it was safe to
ignore.  It would be surprising thought for a disassembler to do that.
After getting an error that can't be explained, you'd think they would
return immediately with an error.

Simon

  parent reply	other threads:[~2022-10-24 18:20 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-24 12:50 Andrew Burgess
2022-10-24 15:50 ` Simon Marchi
2022-10-24 17:22   ` Andrew Burgess
2022-10-24 17:45     ` [PATCH] gdb/disasm: mark functions passed to the disassembler noexcept Andrew Burgess
2022-10-24 18:24       ` Simon Marchi
2022-10-24 18:20     ` Simon Marchi [this message]
2022-10-27 10:38       ` [PATCH] gdb/python: avoid throwing an exception over libopcodes code Andrew Burgess
2022-10-27 15:38 ` [PATCHv2 0/3] " Andrew Burgess
2022-10-27 15:38   ` [PATCHv2 1/3] " Andrew Burgess
2022-11-28 14:39     ` Simon Marchi
2022-11-28 19:26       ` Andrew Burgess
2022-10-27 15:38   ` [PATCHv2 2/3] gdb/disasm: mark functions passed to the disassembler noexcept Andrew Burgess
2022-10-27 15:38   ` [PATCHv2 3/3] gdb: mark disassembler function callback types as noexcept Andrew Burgess
2022-11-18 16:57   ` [PATCHv3 0/3] gdb/python: avoid throwing an exception over libopcodes code Andrew Burgess
2022-11-18 16:57     ` [PATCHv3 1/3] " Andrew Burgess
2022-11-18 16:57     ` [PATCHv3 2/3] gdb/disasm: mark functions passed to the disassembler noexcept Andrew Burgess
2022-11-18 16:57     ` [PATCHv3 3/3] gdb: mark disassembler function callback types as noexcept Andrew Burgess
2022-11-28  8:35     ` [PATCHv3 0/3] gdb/python: avoid throwing an exception over libopcodes code 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=3ac49ef4-4bcf-2abc-4e06-0c9f3b8c9991@simark.ca \
    --to=simark@simark.ca \
    --cc=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /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).