public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: Tom de Vries <tdevries@suse.de>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH] [gdb/python] Throw MemoryError in inferior.read_memory if malloc fails
Date: Thu, 11 Apr 2024 10:07:29 -0600	[thread overview]
Message-ID: <875xwn51dq.fsf@tromey.com> (raw)
In-Reply-To: <20240411105257.15421-1-tdevries@suse.de> (Tom de Vries's message of "Thu, 11 Apr 2024 12:52:57 +0200")

>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:

Tom> PR python/31631 reports a gdb internal error when doing:
Tom> ...
Tom> (gdb) python gdb.selected_inferior().read_memory (0, 0xffffffffffffffff)
Tom> utils.c:709: internal-error: virtual memory exhausted.
Tom> A problem internal to GDB has been detected,
Tom> further debugging may prove unreliable.
Tom> ...

Tom> Fix this by throwing a python MemoryError, such that we have instead:
Tom> ...
Tom> (gdb) python gdb.selected_inferior().read_memory (0, 0xffffffffffffffff)
Tom> Python Exception <class 'MemoryError'>:
Tom> Error occurred in Python.
Tom> (gdb)
Tom> ...

I tend to think you will regret opening this door, because I imagine
there are a large number of ways to crash gdb by passing nonsensical
values to Python APIs.

Tom>  @request("readMemory")
Tom>  @capability("supportsReadMemoryRequest")
Tom>  def read_memory(*, memoryReference: str, offset: int = 0, count: int, **extra):
Tom>      addr = int(memoryReference, 0) + offset
Tom> -    buf = gdb.selected_inferior().read_memory(addr, count)
Tom> +    oom = False
Tom> +    try:
Tom> +        buf = gdb.selected_inferior().read_memory(addr, count)
Tom> +    except MemoryError:
Tom> +        oom = True
Tom> +    if oom:
Tom> +        raise DAPException("Out of memory")

This should probably chain the memory error in the except block and
re-throw.  See https://peps.python.org/pep-3134/

However I don't really understand why this is needed.  Isn't the
exception already propagated back to the server thread?

Tom> +      /* We used to use xmalloc, which does this trick to avoid malloc
Tom> +	 returning a nullptr for a valid reason.  Keep doing the same.  */
Tom> +      if (length == 0)
Tom> +	length = 1;

This is most likely a workaround for vendor implementations of malloc
that return NULL for malloc(0).  See
https://www.gnu.org/software/gnulib/manual/html_node/malloc.html

However, here this is not necessary, because a 0-length memory read is
meaningless, and so this case can simply be reported as an error.

Tom

  reply	other threads:[~2024-04-11 16:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-11 10:52 Tom de Vries
2024-04-11 16:07 ` Tom Tromey [this message]
2024-04-12  7:09   ` Tom de Vries
2024-04-15 16:16     ` Tom Tromey
2024-04-16 13:55       ` Tom de Vries
2024-04-16 19:10     ` Simon Marchi
2024-04-17  8:29       ` 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=875xwn51dq.fsf@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).