2c2fa1
http://sourceware.org/ml/gdb-patches/2012-09/msg00632.html
2c2fa1
Subject: [PATCH 2/4] Add a check to ensure that a type may fit into host memory
2c2fa1
2c2fa1
2c2fa1
--MP_/PnL6l3LUsXWpZ/olqawWlzb
2c2fa1
Content-Type: text/plain; charset=US-ASCII
2c2fa1
Content-Transfer-Encoding: 7bit
2c2fa1
Content-Disposition: inline
2c2fa1
2c2fa1
Hi,
2c2fa1
2c2fa1
This is part two of the bitpos expansion patch.  This implements checks
2c2fa1
in some places in the code to ensure that a type size in ULONGEST is
2c2fa1
small enough to fit into host memory.  Tested for regressions on x86_64
2c2fa1
Fedora 16.
2c2fa1
2c2fa1
Regards,
2c2fa1
Siddhesh
2c2fa1
2c2fa1
--MP_/PnL6l3LUsXWpZ/olqawWlzb
2c2fa1
Content-Type: text/plain
2c2fa1
Content-Transfer-Encoding: quoted-printable
2c2fa1
Content-Disposition: attachment; filename=ChangeLog-ensure_sizet
2c2fa1
2c2fa1
gdb/ChangeLog
2c2fa1
2c2fa1
	* alpha-tdep.c (alpha_push_dummy_call) Check for underflow in
2c2fa1
	SP.
2c2fa1
	* cp-valprint (cp_print_value): Ensure BASECLASS fits into
2c2fa1
	size_t.
2c2fa1
	* dwarf2loc.c (read_pieced_value): Ensure that THIS_SIZE fits
2c2fa1
	into size_t.
2c2fa1
	(write_pieced_value): Likewise.
2c2fa1
	* findcmd.c (parse_find_args): Ensure PATTERN_BUF_SIZE fits into
2c2fa1
	size_t.
2c2fa1
	* p-valprint (pascal_object_print_value): Ensure BASECLASS fits
2c2fa1
	into size_t.
2c2fa1
	* utils.c (ulongest_fits_host_or_error): New function to find if
2c2fa1
	a ULONGEST number fits into size_t.
2c2fa1
	* utils.h: Declare ulongest_fits_host_or_error.
2c2fa1
	* valops.c (search_struct_method): Ensure BASECLASS fits into
2c2fa1
	size_t.
2c2fa1
	* value.c (allocate_value_lazy): Ensure TYPE fits into size_t.
2c2fa1
	(allocate_value_contents): Likewise.
2c2fa1
	(set_value_enclosing_type): Ensure NEW_ENCL_TYPE fits into
2c2fa1
	size_t.
2c2fa1
	* vax-tdep.c (vax_return_value): Ensure that TYPE fits into
2c2fa1
	size_t.
2c2fa1
2c2fa1
--MP_/PnL6l3LUsXWpZ/olqawWlzb
2c2fa1
Content-Type: text/x-patch
2c2fa1
Content-Transfer-Encoding: 7bit
2c2fa1
Content-Disposition: attachment; filename=bitpos-ensure-size_t.patch
2c2fa1
2c2fa1
Index: gdb-7.5.50.20130118/gdb/alpha-tdep.c
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130118.orig/gdb/alpha-tdep.c	2013-01-18 23:33:59.277047324 +0100
2c2fa1
+++ gdb-7.5.50.20130118/gdb/alpha-tdep.c	2013-01-18 23:34:02.678051846 +0100
2c2fa1
@@ -414,6 +414,13 @@ alpha_push_dummy_call (struct gdbarch *g
2c2fa1
     accumulate_size = 0;
2c2fa1
   else
2c2fa1
     accumulate_size -= sizeof(arg_reg_buffer);
2c2fa1
+
2c2fa1
+  /* Check for underflow.  */
2c2fa1
+  if (sp - accumulate_size > sp)
2c2fa1
+    error (_("Insufficient memory in GDB host for arguments, "
2c2fa1
+	     "need %s bytes, but less than %s bytes available."),
2c2fa1
+	   plongest (accumulate_size), plongest (CORE_ADDR_MAX - sp));
2c2fa1
+
2c2fa1
   sp -= accumulate_size;
2c2fa1
 
2c2fa1
   /* Keep sp aligned to a multiple of 16 as the ABI requires.  */
2c2fa1
Index: gdb-7.5.50.20130118/gdb/cp-valprint.c
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130118.orig/gdb/cp-valprint.c	2013-01-18 23:33:59.278047326 +0100
2c2fa1
+++ gdb-7.5.50.20130118/gdb/cp-valprint.c	2013-01-18 23:34:02.679051848 +0100
2c2fa1
@@ -540,6 +540,8 @@ cp_print_value (struct type *type, struc
2c2fa1
 		  gdb_byte *buf;
2c2fa1
 		  struct cleanup *back_to;
2c2fa1
 
2c2fa1
+		  ulongest_fits_host_or_error (TYPE_LENGTH (baseclass));
2c2fa1
+
2c2fa1
 		  buf = xmalloc (TYPE_LENGTH (baseclass));
2c2fa1
 		  back_to = make_cleanup (xfree, buf);
2c2fa1
 
2c2fa1
Index: gdb-7.5.50.20130118/gdb/dwarf2loc.c
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130118.orig/gdb/dwarf2loc.c	2013-01-18 23:33:59.280047332 +0100
2c2fa1
+++ gdb-7.5.50.20130118/gdb/dwarf2loc.c	2013-01-18 23:34:02.680051851 +0100
2c2fa1
@@ -1784,6 +1784,8 @@ read_pieced_value (struct value *v)
2c2fa1
 
2c2fa1
       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
2c2fa1
       source_offset = source_offset_bits / 8;
2c2fa1
+      ulongest_fits_host_or_error (this_size);
2c2fa1
+
2c2fa1
       if (buffer_size < this_size)
2c2fa1
 	{
2c2fa1
 	  buffer_size = this_size;
2c2fa1
@@ -1975,6 +1977,7 @@ write_pieced_value (struct value *to, st
2c2fa1
 	}
2c2fa1
       else
2c2fa1
 	{
2c2fa1
+	  ulongest_fits_host_or_error (this_size);
2c2fa1
 	  if (buffer_size < this_size)
2c2fa1
 	    {
2c2fa1
 	      buffer_size = this_size;
2c2fa1
Index: gdb-7.5.50.20130118/gdb/findcmd.c
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130118.orig/gdb/findcmd.c	2013-01-18 23:33:59.280047332 +0100
2c2fa1
+++ gdb-7.5.50.20130118/gdb/findcmd.c	2013-01-18 23:34:02.680051851 +0100
2c2fa1
@@ -187,6 +187,7 @@ parse_find_args (char *args, ULONGEST *m
2c2fa1
 	  size_t current_offset = pattern_buf_end - pattern_buf;
2c2fa1
 
2c2fa1
 	  pattern_buf_size = pattern_buf_size_need * 2;
2c2fa1
+	  ulongest_fits_host_or_error (pattern_buf_size);
2c2fa1
 	  pattern_buf = xrealloc (pattern_buf, pattern_buf_size);
2c2fa1
 	  pattern_buf_end = pattern_buf + current_offset;
2c2fa1
 	}
2c2fa1
Index: gdb-7.5.50.20130118/gdb/p-valprint.c
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130118.orig/gdb/p-valprint.c	2013-01-18 23:33:59.281047334 +0100
2c2fa1
+++ gdb-7.5.50.20130118/gdb/p-valprint.c	2013-01-18 23:34:02.680051851 +0100
2c2fa1
@@ -797,6 +797,7 @@ pascal_object_print_value (struct type *
2c2fa1
 	      gdb_byte *buf;
2c2fa1
 	      struct cleanup *back_to;
2c2fa1
 
2c2fa1
+	      ulongest_fits_host_or_error (TYPE_LENGTH (baseclass));
2c2fa1
 	      buf = xmalloc (TYPE_LENGTH (baseclass));
2c2fa1
 	      back_to = make_cleanup (xfree, buf);
2c2fa1
 
2c2fa1
Index: gdb-7.5.50.20130118/gdb/utils.c
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130118.orig/gdb/utils.c	2013-01-18 23:33:59.282047336 +0100
2c2fa1
+++ gdb-7.5.50.20130118/gdb/utils.c	2013-01-18 23:34:02.681051854 +0100
2c2fa1
@@ -3219,6 +3219,18 @@ host_address_to_string (const void *addr
2c2fa1
   return str;
2c2fa1
 }
2c2fa1
 
2c2fa1
+/* Ensure that the input NUM is not larger than the maximum capacity of the
2c2fa1
+   host system.  We choose SIZE_MAX / 8 as a conservative estimate of the size
2c2fa1
+   of a resource that a system may allocate.  */
2c2fa1
+void
2c2fa1
+ulongest_fits_host_or_error (ULONGEST num)
2c2fa1
+{
2c2fa1
+  if (num > SIZE_MAX / 8)
2c2fa1
+    error (_("Insufficient memory in host GDB for object of size %s bytes, "
2c2fa1
+	     "maximum allowed %s bytes."), pulongest (num),
2c2fa1
+	   pulongest (SIZE_MAX / 8));
2c2fa1
+}
2c2fa1
+
2c2fa1
 char *
2c2fa1
 gdb_realpath (const char *filename)
2c2fa1
 {
2c2fa1
Index: gdb-7.5.50.20130118/gdb/valops.c
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130118.orig/gdb/valops.c	2013-01-18 23:33:59.283047338 +0100
2c2fa1
+++ gdb-7.5.50.20130118/gdb/valops.c	2013-01-18 23:34:02.682051856 +0100
2c2fa1
@@ -2369,6 +2369,7 @@ search_struct_method (const char *name,
2c2fa1
 	      struct cleanup *back_to;
2c2fa1
 	      CORE_ADDR address;
2c2fa1
 
2c2fa1
+	      ulongest_fits_host_or_error (TYPE_LENGTH (baseclass));
2c2fa1
 	      tmp = xmalloc (TYPE_LENGTH (baseclass));
2c2fa1
 	      back_to = make_cleanup (xfree, tmp);
2c2fa1
 	      address = value_address (*arg1p);
2c2fa1
Index: gdb-7.5.50.20130118/gdb/value.c
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130118.orig/gdb/value.c	2013-01-18 23:33:59.285047342 +0100
2c2fa1
+++ gdb-7.5.50.20130118/gdb/value.c	2013-01-18 23:34:02.683051858 +0100
2c2fa1
@@ -663,6 +663,7 @@ allocate_value_lazy (struct type *type)
2c2fa1
      description correctly.  */
2c2fa1
   check_typedef (type);
2c2fa1
 
2c2fa1
+  ulongest_fits_host_or_error (TYPE_LENGTH (type));
2c2fa1
   val = (struct value *) xzalloc (sizeof (struct value));
2c2fa1
   val->contents = NULL;
2c2fa1
   val->next = all_values;
2c2fa1
@@ -694,6 +695,8 @@ allocate_value_lazy (struct type *type)
2c2fa1
 void
2c2fa1
 allocate_value_contents (struct value *val)
2c2fa1
 {
2c2fa1
+  ulongest_fits_host_or_error (TYPE_LENGTH (val->enclosing_type));
2c2fa1
+
2c2fa1
   if (!val->contents)
2c2fa1
     val->contents = (gdb_byte *) xzalloc (TYPE_LENGTH (val->enclosing_type));
2c2fa1
 }
2c2fa1
@@ -2672,8 +2675,12 @@ void
2c2fa1
 set_value_enclosing_type (struct value *val, struct type *new_encl_type)
2c2fa1
 {
2c2fa1
   if (TYPE_LENGTH (new_encl_type) > TYPE_LENGTH (value_enclosing_type (val))) 
2c2fa1
-    val->contents =
2c2fa1
-      (gdb_byte *) xrealloc (val->contents, TYPE_LENGTH (new_encl_type));
2c2fa1
+    {
2c2fa1
+      ulongest_fits_host_or_error (TYPE_LENGTH (new_encl_type));
2c2fa1
+
2c2fa1
+      val->contents =
2c2fa1
+	(gdb_byte *) xrealloc (val->contents, TYPE_LENGTH (new_encl_type));
2c2fa1
+    }
2c2fa1
 
2c2fa1
   val->enclosing_type = new_encl_type;
2c2fa1
 }
2c2fa1
Index: gdb-7.5.50.20130118/gdb/vax-tdep.c
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130118.orig/gdb/vax-tdep.c	2013-01-18 23:34:02.683051858 +0100
2c2fa1
+++ gdb-7.5.50.20130118/gdb/vax-tdep.c	2013-01-18 23:34:34.950094198 +0100
2c2fa1
@@ -223,6 +223,7 @@ vax_return_value (struct gdbarch *gdbarc
2c2fa1
 	  ULONGEST addr;
2c2fa1
 
2c2fa1
 	  regcache_raw_read_unsigned (regcache, VAX_R0_REGNUM, &addr);
2c2fa1
+	  ulongest_fits_host_or_error (TYPE_LENGTH (type));
2c2fa1
 	  read_memory (addr, readbuf, len);
2c2fa1
 	}
2c2fa1
 
2c2fa1
Index: gdb-7.5.50.20130118/gdb/defs.h
2c2fa1
===================================================================
2c2fa1
--- gdb-7.5.50.20130118.orig/gdb/defs.h	2013-01-18 23:34:02.684051860 +0100
2c2fa1
+++ gdb-7.5.50.20130118/gdb/defs.h	2013-01-18 23:34:14.238067193 +0100
2c2fa1
@@ -801,4 +801,6 @@ enum block_enum
2c2fa1
 
2c2fa1
 #include "utils.h"
2c2fa1
 
2c2fa1
+extern void ulongest_fits_host_or_error (ULONGEST num);
2c2fa1
+
2c2fa1
 #endif /* #ifndef DEFS_H */