Prelimit number of bytes to read in "vFile:pread:"

While handling "vFile:pread:" packets, gdbserver would read the
number of bytes requested regardless of whether this would fit
into the reply packet.  gdbserver would then return a packet's
worth of data and discard the remainder.  When accessing large
binaries GDB (via BFD) routinely makes large "vFile:pread:"
requests, resulting in gdbserver allocating large unnecessary
buffers and reading some portions of the file many times over.

This commit causes gdbserver to limit the number of bytes to be
read to a sensible maximum prior to allocating buffers and reading
data.

gdb/gdbserver/ChangeLog:

	* hostio.c (handle_pread): Do not attempt to read more data
	than hostio_reply_with_data can fit in a packet.
This commit is contained in:
Gary Benson 2015-08-19 13:53:24 +01:00
parent 64d94ba01a
commit 45face3ba1
2 changed files with 17 additions and 0 deletions

View file

@ -1,3 +1,8 @@
2015-08-19 Gary Benson <gbenson@redhat.com>
* hostio.c (handle_pread): Do not attempt to read more data
than hostio_reply_with_data can fit in a packet.
2015-08-18 Joel Brobecker <brobecker@adacore.com> 2015-08-18 Joel Brobecker <brobecker@adacore.com>
* linux-aarch32-low.c (NT_ARM_VFP): Define if not already defined. * linux-aarch32-low.c (NT_ARM_VFP): Define if not already defined.

View file

@ -344,6 +344,7 @@ handle_pread (char *own_buf, int *new_packet_len)
{ {
int fd, ret, len, offset, bytes_sent; int fd, ret, len, offset, bytes_sent;
char *p, *data; char *p, *data;
static int max_reply_size = -1;
p = own_buf + strlen ("vFile:pread:"); p = own_buf + strlen ("vFile:pread:");
@ -359,6 +360,17 @@ handle_pread (char *own_buf, int *new_packet_len)
return; return;
} }
/* Do not attempt to read more than the maximum number of bytes
hostio_reply_with_data can fit in a packet. We may still read
too much because of escaping, but this is handled below. */
if (max_reply_size == -1)
{
sprintf (own_buf, "F%x;", PBUFSIZ);
max_reply_size = PBUFSIZ - strlen (own_buf);
}
if (len > max_reply_size)
len = max_reply_size;
data = xmalloc (len); data = xmalloc (len);
#ifdef HAVE_PREAD #ifdef HAVE_PREAD
ret = pread (fd, data, len, offset); ret = pread (fd, data, len, offset);