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

Simon Marchi <simark@simark.ca> writes:

> 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

More like:

  0. User Python code for read_memory,
  1. gdb frame that calls into Python read_memory,
  2. libopcodes disassembler stack frames,
  3. gdb frame that implemented Python builtin_disassemble call,
  4. User Python code that calls builtin_disassemble,
  5. gdb frame that calls into Python disassembler,
  6. other gdb stack frames.

What used to happen:

  - Python exception raised in frame 0,
  - Python exception converted to C++ exception in 1, and thrown,
  - C++ exception caught in 3 and converted back to a Python exception,
  - Python exception appears in frame 4 for the user to handle.

Now what happens:

  - Python exception raised in frame 0,
  - Python exception stored in frame 1, control returns to frame 2 with
    an error code,
  - Control might re-enter frame 1 again, but the stored exception means
    we immediately return with an error code,
  - Eventually the disassembler gives up and returns to frame 3,
  - We spot the stored exception and reinstall it in frame 3,
  - Python exception appears in frame 4 for the user to handle.

This gives the _illusion_ to the user that the exception raised in
read_memory immediately propagated up the stack and was raised as if
from the builtin_disassemble call.

Currently (thought this could change as this stuff has not yet been
released) the documentation says that the _only_ exception frame 1/2
know how to handle is gdb.MemoryError.  Anything else is not handled by
the disassembler.

If we allow frame 2 to keep calling to frame 1/0 even after an unknown
exception has been raised then this just feels weird to me.  Hopefully
the user code will just keep raising the same, or similar exception each
time.  If it doesn't though, then there's the possibility that the
disassembler results will be undefined.  Like you say, it gives the
_impression_ that the disassembler has handled the exception ... but it
really hasn't.

>
> 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.

I agree that it would be surprising for a disassembler to claim to
handle an error that it has, in fact, not handled.

> After getting an error that can't be explained, you'd think they would
> return immediately with an error.

Remember, libopcodes doesn't have a concept of "unexplained" errors.
There's only one error type in libopcode, memory errors.  This whole
mechanism is me attempting to implement the concept of "unexplained"
errors on top of libopcodes.

Initially I did this by bypassing libopcodes completely, but that
doesn't work because libopcode is C code.

So now, I'm trying to overload the memory-error mechanism, i.e. store
the unexplained error, then return the memory-error value.  In most
cases, the disassembler will return as soon as it sees the first
memory-error, so for (I'm guessing) 99% of cases, this whole discussion
is moot.  I'm just keen to lock down the actual behaviour and not leave
it at the whim of the disassembler, just in case we hit that 1% case.

I'll take another pass at patch 2/2 and see if I can silence the
compiler warning about using noexcept in the function type.

Thanks for your continued reviews,

Andrew


  reply	other threads:[~2022-10-27 10:38 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     ` [PATCH] gdb/python: avoid throwing an exception over libopcodes code Simon Marchi
2022-10-27 10:38       ` Andrew Burgess [this message]
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=87edut5z63.fsf@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=simark@simark.ca \
    /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).