From: Siddhesh Poyarekar <siddhesh@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/4] Add a check to ensure that a type may fit into host memory
Date: Thu, 27 Sep 2012 13:55:00 -0000 [thread overview]
Message-ID: <20120927185100.53c5efd3@spoyarek> (raw)
[-- Attachment #1: Type: text/plain, Size: 248 bytes --]
Hi,
This is part two of the bitpos expansion patch. This implements checks
in some places in the code to ensure that a type size in ULONGEST is
small enough to fit into host memory. Tested for regressions on x86_64
Fedora 16.
Regards,
Siddhesh
[-- Attachment #2: ChangeLog-ensure_sizet --]
[-- Type: text/plain, Size: 905 bytes --]
gdb/ChangeLog
* alpha-tdep.c (alpha_push_dummy_call) Check for underflow in
SP.
* cp-valprint (cp_print_value): Ensure BASECLASS fits into
size_t.
* dwarf2loc.c (read_pieced_value): Ensure that THIS_SIZE fits
into size_t.
(write_pieced_value): Likewise.
* findcmd.c (parse_find_args): Ensure PATTERN_BUF_SIZE fits into
size_t.
* p-valprint (pascal_object_print_value): Ensure BASECLASS fits
into size_t.
* utils.c (ulongest_fits_host_or_error): New function to find if
a ULONGEST number fits into size_t.
* utils.h: Declare ulongest_fits_host_or_error.
* valops.c (search_struct_method): Ensure BASECLASS fits into
size_t.
* value.c (allocate_value_lazy): Ensure TYPE fits into size_t.
(allocate_value_contents): Likewise.
(set_value_enclosing_type): Ensure NEW_ENCL_TYPE fits into
size_t.
* vax-tdep.c (vax_return_value): Ensure that TYPE fits into
size_t.
[-- Attachment #3: bitpos-ensure-size_t.patch --]
[-- Type: text/x-patch, Size: 5572 bytes --]
diff --git a/gdb/alpha-tdep.c b/gdb/alpha-tdep.c
index e5b75d2..ff719d8 100644
--- a/gdb/alpha-tdep.c
+++ b/gdb/alpha-tdep.c
@@ -414,6 +414,13 @@ alpha_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
accumulate_size = 0;
else
accumulate_size -= sizeof(arg_reg_buffer);
+
+ /* Check for underflow. */
+ if (sp - accumulate_size > sp)
+ error (_("Insufficient memory in GDB host for arguments, "
+ "need %s bytes, but less than %s bytes available."),
+ plongest (accumulate_size), plongest (CORE_ADDR_MAX - sp));
+
sp -= accumulate_size;
/* Keep sp aligned to a multiple of 16 as the ABI requires. */
diff --git a/gdb/cp-valprint.c b/gdb/cp-valprint.c
index 8bc329e..2373419 100644
--- a/gdb/cp-valprint.c
+++ b/gdb/cp-valprint.c
@@ -558,6 +558,8 @@ cp_print_value (struct type *type, struct type *real_type,
gdb_byte *buf;
struct cleanup *back_to;
+ ulongest_fits_host_or_error (TYPE_LENGTH (baseclass));
+
buf = xmalloc (TYPE_LENGTH (baseclass));
back_to = make_cleanup (xfree, buf);
diff --git a/gdb/dwarf2loc.c b/gdb/dwarf2loc.c
index bef4355..7106805 100644
--- a/gdb/dwarf2loc.c
+++ b/gdb/dwarf2loc.c
@@ -1593,6 +1593,8 @@ read_pieced_value (struct value *v)
this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
source_offset = source_offset_bits / 8;
+ ulongest_fits_host_or_error (this_size);
+
if (buffer_size < this_size)
{
buffer_size = this_size;
@@ -1784,6 +1786,7 @@ write_pieced_value (struct value *to, struct value *from)
}
else
{
+ ulongest_fits_host_or_error (this_size);
if (buffer_size < this_size)
{
buffer_size = this_size;
diff --git a/gdb/findcmd.c b/gdb/findcmd.c
index 116e0b0..baaee92 100644
--- a/gdb/findcmd.c
+++ b/gdb/findcmd.c
@@ -187,6 +187,7 @@ parse_find_args (char *args, ULONGEST *max_countp,
size_t current_offset = pattern_buf_end - pattern_buf;
pattern_buf_size = pattern_buf_size_need * 2;
+ ulongest_fits_host_or_error (pattern_buf_size);
pattern_buf = xrealloc (pattern_buf, pattern_buf_size);
pattern_buf_end = pattern_buf + current_offset;
}
diff --git a/gdb/p-valprint.c b/gdb/p-valprint.c
index 7be1c17..49c3a16 100644
--- a/gdb/p-valprint.c
+++ b/gdb/p-valprint.c
@@ -790,6 +790,7 @@ pascal_object_print_value (struct type *type, const gdb_byte *valaddr,
gdb_byte *buf;
struct cleanup *back_to;
+ ulongest_fits_host_or_error (TYPE_LENGTH (baseclass));
buf = xmalloc (TYPE_LENGTH (baseclass));
back_to = make_cleanup (xfree, buf);
diff --git a/gdb/utils.c b/gdb/utils.c
index 6026450..090c1d2 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3169,6 +3169,18 @@ host_address_to_string (const void *addr)
return str;
}
+/* Ensure that the input NUM is not larger than the maximum capacity of the
+ host system. We choose SIZE_MAX / 8 as a conservative estimate of the size
+ of a resource that a system may allocate. */
+void
+ulongest_fits_host_or_error (ULONGEST num)
+{
+ if (num > SIZE_MAX / 8)
+ error (_("Insufficient memory in host GDB for object of size %s bytes, "
+ "maximum allowed %s bytes."), pulongest (num),
+ pulongest (SIZE_MAX / 8));
+}
+
char *
gdb_realpath (const char *filename)
{
diff --git a/gdb/utils.h b/gdb/utils.h
index 4bb6ac8..d7d2e1a 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -23,6 +23,8 @@
#include "cleanups.h"
+extern void ulongest_fits_host_or_error (ULONGEST num);
+
extern void initialize_utils (void);
/* String utilities. */
diff --git a/gdb/valops.c b/gdb/valops.c
index 4458f6b..94e8f67 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -2273,6 +2273,7 @@ search_struct_method (const char *name, struct value **arg1p,
struct cleanup *back_to;
CORE_ADDR address;
+ ulongest_fits_host_or_error (TYPE_LENGTH (baseclass));
tmp = xmalloc (TYPE_LENGTH (baseclass));
back_to = make_cleanup (xfree, tmp);
address = value_address (*arg1p);
diff --git a/gdb/value.c b/gdb/value.c
index 79a89bb..4524221 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -662,6 +662,7 @@ allocate_value_lazy (struct type *type)
description correctly. */
check_typedef (type);
+ ulongest_fits_host_or_error (TYPE_LENGTH (type));
val = (struct value *) xzalloc (sizeof (struct value));
val->contents = NULL;
val->next = all_values;
@@ -693,6 +694,8 @@ allocate_value_lazy (struct type *type)
void
allocate_value_contents (struct value *val)
{
+ ulongest_fits_host_or_error (TYPE_LENGTH (val->enclosing_type));
+
if (!val->contents)
val->contents = (gdb_byte *) xzalloc (TYPE_LENGTH (val->enclosing_type));
}
@@ -2601,8 +2604,12 @@ void
set_value_enclosing_type (struct value *val, struct type *new_encl_type)
{
if (TYPE_LENGTH (new_encl_type) > TYPE_LENGTH (value_enclosing_type (val)))
- val->contents =
- (gdb_byte *) xrealloc (val->contents, TYPE_LENGTH (new_encl_type));
+ {
+ ulongest_fits_host_or_error (TYPE_LENGTH (new_encl_type));
+
+ val->contents =
+ (gdb_byte *) xrealloc (val->contents, TYPE_LENGTH (new_encl_type));
+ }
val->enclosing_type = new_encl_type;
}
diff --git a/gdb/vax-tdep.c b/gdb/vax-tdep.c
index 550b56b..04a3d9a 100644
--- a/gdb/vax-tdep.c
+++ b/gdb/vax-tdep.c
@@ -224,6 +224,7 @@ vax_return_value (struct gdbarch *gdbarch, struct value *function,
ULONGEST addr;
regcache_raw_read_unsigned (regcache, VAX_R0_REGNUM, &addr);
+ ulongest_fits_host_or_error (len);
read_memory (addr, readbuf, len);
}
next reply other threads:[~2012-09-27 13:55 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-27 13:55 Siddhesh Poyarekar [this message]
2012-11-09 18:02 ` Jan Kratochvil
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=20120927185100.53c5efd3@spoyarek \
--to=siddhesh@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