From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id D5D3B3858403 for ; Mon, 24 Oct 2022 18:20:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D5D3B3858403 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca Received: from [172.16.0.64] (192-222-180-24.qc.cable.ebox.net [192.222.180.24]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 46E521E0CB; Mon, 24 Oct 2022 14:20:43 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1666635643; bh=B64qtIQc42+hj7tFYe7sGhJz0GwJfS1sl1193vAPETk=; h=Date:Subject:To:References:From:In-Reply-To:From; b=iFGv0dqfRryDEslJ6zjiS3ZoDPs6ROsr1+FjBdwl5F8sqzwB3VKIRzSYIVL2XooD5 t0zoQQb6kk1mrxmo5p7BSdCFpJp1VwAclxAEYbc2CLYMUc0LqvwTs9ccasXd1xWtE3 swyC4VvbA61PMUpG1hoR/Swu7Ws0Dw+WpUX4Tcvw= Message-ID: <3ac49ef4-4bcf-2abc-4e06-0c9f3b8c9991@simark.ca> Date: Mon, 24 Oct 2022 14:20:42 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.2 Subject: Re: [PATCH] gdb/python: avoid throwing an exception over libopcodes code Content-Language: fr To: Andrew Burgess , gdb-patches@sourceware.org References: <20221024125016.2823862-1-aburgess@redhat.com> <499ecf26-0f57-2376-a617-9b6214319b4a@simark.ca> <87r0yx6sr7.fsf@redhat.com> From: Simon Marchi In-Reply-To: <87r0yx6sr7.fsf@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,SPF_HELO_PASS,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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