public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3 1/3] gdbtypes: return value from get_unsigned_type_max
@ 2021-07-29 15:12 George Barrett
  2021-07-29 15:12 ` [PATCH v3 2/3] guile: fix make-value with pointer type George Barrett
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: George Barrett @ 2021-07-29 15:12 UTC (permalink / raw)
  To: gdb-patches; +Cc: George Barrett

Changes the signature of get_unsigned_type_max to return the computed
value rather than returning void and writing the value into a pointer
passed by the caller.

gdb/ChangeLog:

2021-07-30  George Barrett  <bob@bob131.so>

	* gdbtypes.h (get_unsigned_type_max): Change signature to
	return the result instead of accepting a pointer argument in
	which to store the result.
	* gdbtypes.c (get_unsigned_type_max): Likewise.
	* guile/scm-math.c (vlscm_convert_typed_number): Update caller
	of get_unsigned_type_max.
	(vlscm_integer_fits_p): Likewise.
---
 gdb/gdbtypes.c       | 6 +++---
 gdb/gdbtypes.h       | 2 +-
 gdb/guile/scm-math.c | 6 ++----
 3 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 1a261719422..12c7042e10a 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -1894,8 +1894,8 @@ lookup_struct_elt_type (struct type *type, const char *name, int noerr)
 /* Store in *MAX the largest number representable by unsigned integer type
    TYPE.  */
 
-void
-get_unsigned_type_max (struct type *type, ULONGEST *max)
+ULONGEST
+get_unsigned_type_max (struct type *type)
 {
   unsigned int n;
 
@@ -1905,7 +1905,7 @@ get_unsigned_type_max (struct type *type, ULONGEST *max)
 
   /* Written this way to avoid overflow.  */
   n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
-  *max = ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
+  return ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
 }
 
 /* Store in *MIN, *MAX the smallest and largest numbers representable by
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index d754f2fcd36..b47644b210e 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -2519,7 +2519,7 @@ extern struct type *lookup_unsigned_typename (const struct language_defn *,
 extern struct type *lookup_signed_typename (const struct language_defn *,
 					    const char *);
 
-extern void get_unsigned_type_max (struct type *, ULONGEST *);
+extern ULONGEST get_unsigned_type_max (struct type *);
 
 extern void get_signed_type_minmax (struct type *, LONGEST *, LONGEST *);
 
diff --git a/gdb/guile/scm-math.c b/gdb/guile/scm-math.c
index d9fd6718196..f1c032b6efa 100644
--- a/gdb/guile/scm-math.c
+++ b/gdb/guile/scm-math.c
@@ -529,9 +529,7 @@ vlscm_convert_typed_number (const char *func_name, int obj_arg_pos, SCM obj,
     {
       if (type->is_unsigned ())
 	{
-	  ULONGEST max;
-
-	  get_unsigned_type_max (type, &max);
+	  ULONGEST max = get_unsigned_type_max (type);
 	  if (!scm_is_unsigned_integer (obj, 0, max))
 	    {
 	      *except_scmp
@@ -580,7 +578,7 @@ vlscm_integer_fits_p (SCM obj, struct type *type)
       /* If scm_is_unsigned_integer can't work with this type, just punt.  */
       if (TYPE_LENGTH (type) > sizeof (uintmax_t))
 	return 0;
-      get_unsigned_type_max (type, &max);
+      max = get_unsigned_type_max (type);
       return scm_is_unsigned_integer (obj, 0, max);
     }
   else
-- 
2.31.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 2/3] guile: fix make-value with pointer type
  2021-07-29 15:12 [PATCH v3 1/3] gdbtypes: return value from get_unsigned_type_max George Barrett
@ 2021-07-29 15:12 ` George Barrett
  2021-07-29 16:55   ` Simon Marchi
  2021-07-29 15:12 ` [PATCH v3 3/3] guile/scm-math: indentation fixes George Barrett
  2021-07-29 16:53 ` [PATCH v3 1/3] gdbtypes: return value from get_unsigned_type_max Simon Marchi
  2 siblings, 1 reply; 6+ messages in thread
From: George Barrett @ 2021-07-29 15:12 UTC (permalink / raw)
  To: gdb-patches; +Cc: George Barrett

Calling the `make-value' procedure with an integer value and a pointer
type for the #:type argument triggers a failed assertion in
`get_unsigned_type_max', as that function doesn't consider pointers to
be an unsigned type. This commit fixes the issue by adding a separate
code path for pointers.

As previously suggested, range checking is done using a new helper
function in gdbtypes.

gdb/ChangeLog:

2021-07-30  George Barrett  <bob@bob131.so>

	* gdbtypes.h (get_pointer_type_max): Add declaration.
	* gdbtypes.c (get_pointer_type_max): Add definition for new
	helper function.
	* guile/scm-math.c (vlscm_convert_typed_number): Add code path
	for handling conversions to pointer types without failing an
	assert.

gdb/testsuite/ChangeLog:

2021-07-30  George Barrett  <bob@bob131.so>

	* gdb.guile/scm-math.exp (test_value_numeric_ops): Add test
	for creating pointers with make-value.
	(test_make_pointer_value, test_pointer_numeric_range): Add
	test procedures containing checks for integer-to-pointer
	validation.
---
 gdb/gdbtypes.c                       | 16 +++++++++++
 gdb/gdbtypes.h                       |  2 ++
 gdb/guile/scm-math.c                 | 16 +++++++++--
 gdb/testsuite/gdb.guile/scm-math.exp | 40 ++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 12c7042e10a..02fcf25099c 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -1925,6 +1925,22 @@ get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max)
   *max = ((ULONGEST) 1 << (n - 1)) - 1;
 }
 
+/* Store in *MAX the largest value representable by pointer type
+   TYPE. */
+
+CORE_ADDR
+get_pointer_type_max (struct type *type)
+{
+  unsigned int n;
+
+  type = check_typedef (type);
+  gdb_assert (type->code () == TYPE_CODE_PTR);
+  gdb_assert (TYPE_LENGTH (type) <= sizeof (CORE_ADDR));
+
+  n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
+  return ((((CORE_ADDR) 1 << (n - 1)) - 1) << 1) | 1;
+}
+
 /* Internal routine called by TYPE_VPTR_FIELDNO to return the value of
    cplus_stuff.vptr_fieldno.
 
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index b47644b210e..bfb7f2965a6 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -2523,6 +2523,8 @@ extern ULONGEST get_unsigned_type_max (struct type *);
 
 extern void get_signed_type_minmax (struct type *, LONGEST *, LONGEST *);
 
+extern CORE_ADDR get_pointer_type_max (struct type *);
+
 /* * Resolve all dynamic values of a type e.g. array bounds to static values.
    ADDR specifies the location of the variable the type is bound to.
    If TYPE has no dynamic properties return TYPE; otherwise a new type with
diff --git a/gdb/guile/scm-math.c b/gdb/guile/scm-math.c
index f1c032b6efa..173b822d2f9 100644
--- a/gdb/guile/scm-math.c
+++ b/gdb/guile/scm-math.c
@@ -524,8 +524,7 @@ vlscm_convert_typed_number (const char *func_name, int obj_arg_pos, SCM obj,
 			    int type_arg_pos, SCM type_scm, struct type *type,
 			    struct gdbarch *gdbarch, SCM *except_scmp)
 {
-  if (is_integral_type (type)
-      || type->code () == TYPE_CODE_PTR)
+  if (is_integral_type (type))
     {
       if (type->is_unsigned ())
 	{
@@ -556,6 +555,19 @@ vlscm_convert_typed_number (const char *func_name, int obj_arg_pos, SCM obj,
 	  return value_from_longest (type, gdbscm_scm_to_longest (obj));
 	}
     }
+  else if (type->code () == TYPE_CODE_PTR)
+    {
+      CORE_ADDR max = get_pointer_type_max (type);
+      if (!scm_is_unsigned_integer (obj, 0, max))
+	{
+	  *except_scmp
+	    = gdbscm_make_out_of_range_error (func_name,
+					      obj_arg_pos, obj,
+					_("value out of range for type"));
+	  return NULL;
+	}
+      return value_from_pointer (type, gdbscm_scm_to_ulongest (obj));
+    }
   else if (type->code () == TYPE_CODE_FLT)
     return value_from_host_double (type, scm_to_double (obj));
   else
diff --git a/gdb/testsuite/gdb.guile/scm-math.exp b/gdb/testsuite/gdb.guile/scm-math.exp
index ba975454ce5..ee428e5cc90 100644
--- a/gdb/testsuite/gdb.guile/scm-math.exp
+++ b/gdb/testsuite/gdb.guile/scm-math.exp
@@ -137,6 +137,15 @@ proc test_value_numeric_ops {} {
     gdb_test "gu (print (value-sub b a))" \
 	"= 3" "subtract two pointer values"
 
+    # Test pointer creation.
+
+    gdb_test_no_output "gu (define void-pointer-type (type-pointer (arch-void-type (current-arch))))"
+    gdb_scm_test_silent_cmd "gu (define null-pointer (make-value 0 #:type void-pointer-type))" \
+	"test make-value with pointer type"
+    gdb_test "gu (print null-pointer)" "= 0x0"
+    gdb_test "gu (print (equal? (value-type null-pointer) void-pointer-type))" \
+	"= #t"
+
     # Test some invalid operations.
 
     gdb_test_multiple "gu (print (value-add i '()))" "catch error in guile type conversion" {
@@ -237,6 +246,36 @@ proc test_value_numeric_ranges {} {
     }
 }
 
+# Helper routine for test_pointer_numeric_range.
+
+proc test_make_pointer_value { size } {
+    set max [get_max_uint $size]
+    set max_hex [string repeat "f" [expr "$size / 4"]]
+
+    gdb_test "gu (print (make-value $max #:type void-pointer-type))" \
+	"= 0x$max_hex" "test make-value void* max"
+    gdb_test "gu (print (make-value 0 #:type void-pointer-type))" \
+	"= 0x0" "test make-value void* 0"
+
+    gdb_test "gu (print (make-value (+ $max 1) #:type void-pointer-type))" \
+	"ERROR.*Out of range.*" "test make-value void* max+1"
+    gdb_test "gu (print (make-value -1 #:type void-pointer-type))" \
+	"ERROR.*Out of range.*" "test make-value void* -1"
+}
+
+proc test_pointer_numeric_range {} {
+    # We can't assume anything about sizeof (void*) on the target.
+    # Keep it simple for now, this will cover everything important for
+    # the major targets.
+    set pointer_size [get_sizeof "void*" 0]
+    if { $pointer_size == 4 } {
+	test_make_pointer_value 32
+    }
+    if { $pointer_size == 8 } {
+	test_make_pointer_value 64
+    }
+}
+
 proc test_value_boolean {} {
     # Note: Boolean values print as 0,1 because they are printed in the
     # current language (in this case C).
@@ -305,5 +344,6 @@ if ![gdb_guile_runto_main] {
 
 test_value_numeric_ops
 test_value_numeric_ranges
+test_pointer_numeric_range
 test_value_boolean
 test_value_compare
-- 
2.31.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 3/3] guile/scm-math: indentation fixes
  2021-07-29 15:12 [PATCH v3 1/3] gdbtypes: return value from get_unsigned_type_max George Barrett
  2021-07-29 15:12 ` [PATCH v3 2/3] guile: fix make-value with pointer type George Barrett
@ 2021-07-29 15:12 ` George Barrett
  2021-07-29 16:53 ` [PATCH v3 1/3] gdbtypes: return value from get_unsigned_type_max Simon Marchi
  2 siblings, 0 replies; 6+ messages in thread
From: George Barrett @ 2021-07-29 15:12 UTC (permalink / raw)
  To: gdb-patches; +Cc: George Barrett

Changes the indenting of a few expressions in
vlscm_convert_typed_number to be better in line with the prevailing
code style.

gdb/ChangeLog:

2021-07-30  George Barrett  <bob@bob131.so>

	* guile/scm-math.c (vlscm_convert_typed_number): Fix the
	indentation of calls to gdbscm_make_out_of_range_error.
---
 gdb/guile/scm-math.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/gdb/guile/scm-math.c b/gdb/guile/scm-math.c
index 173b822d2f9..2258bc498c1 100644
--- a/gdb/guile/scm-math.c
+++ b/gdb/guile/scm-math.c
@@ -532,9 +532,9 @@ vlscm_convert_typed_number (const char *func_name, int obj_arg_pos, SCM obj,
 	  if (!scm_is_unsigned_integer (obj, 0, max))
 	    {
 	      *except_scmp
-		= gdbscm_make_out_of_range_error (func_name,
-						  obj_arg_pos, obj,
-					_("value out of range for type"));
+		= gdbscm_make_out_of_range_error
+		    (func_name, obj_arg_pos, obj,
+		     _("value out of range for type"));
 	      return NULL;
 	    }
 	  return value_from_longest (type, gdbscm_scm_to_ulongest (obj));
@@ -547,9 +547,9 @@ vlscm_convert_typed_number (const char *func_name, int obj_arg_pos, SCM obj,
 	  if (!scm_is_signed_integer (obj, min, max))
 	    {
 	      *except_scmp
-		= gdbscm_make_out_of_range_error (func_name,
-						  obj_arg_pos, obj,
-					_("value out of range for type"));
+		= gdbscm_make_out_of_range_error
+		    (func_name, obj_arg_pos, obj,
+		     _("value out of range for type"));
 	      return NULL;
 	    }
 	  return value_from_longest (type, gdbscm_scm_to_longest (obj));
@@ -561,9 +561,9 @@ vlscm_convert_typed_number (const char *func_name, int obj_arg_pos, SCM obj,
       if (!scm_is_unsigned_integer (obj, 0, max))
 	{
 	  *except_scmp
-	    = gdbscm_make_out_of_range_error (func_name,
-					      obj_arg_pos, obj,
-					_("value out of range for type"));
+	    = gdbscm_make_out_of_range_error
+	        (func_name, obj_arg_pos, obj,
+		 _("value out of range for type"));
 	  return NULL;
 	}
       return value_from_pointer (type, gdbscm_scm_to_ulongest (obj));
-- 
2.31.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 1/3] gdbtypes: return value from get_unsigned_type_max
  2021-07-29 15:12 [PATCH v3 1/3] gdbtypes: return value from get_unsigned_type_max George Barrett
  2021-07-29 15:12 ` [PATCH v3 2/3] guile: fix make-value with pointer type George Barrett
  2021-07-29 15:12 ` [PATCH v3 3/3] guile/scm-math: indentation fixes George Barrett
@ 2021-07-29 16:53 ` Simon Marchi
  2021-07-29 17:01   ` George Barrett
  2 siblings, 1 reply; 6+ messages in thread
From: Simon Marchi @ 2021-07-29 16:53 UTC (permalink / raw)
  To: George Barrett, gdb-patches

Thanks, I'll push it with this small change:

On 2021-07-29 11:12 a.m., George Barrett via Gdb-patches wrote:
> Changes the signature of get_unsigned_type_max to return the computed
> value rather than returning void and writing the value into a pointer
> passed by the caller.
> 
> gdb/ChangeLog:
> 
> 2021-07-30  George Barrett  <bob@bob131.so>
> 
> 	* gdbtypes.h (get_unsigned_type_max): Change signature to
> 	return the result instead of accepting a pointer argument in
> 	which to store the result.
> 	* gdbtypes.c (get_unsigned_type_max): Likewise.
> 	* guile/scm-math.c (vlscm_convert_typed_number): Update caller
> 	of get_unsigned_type_max.
> 	(vlscm_integer_fits_p): Likewise.

Note that we don't use ChangeLog files anymore, so you don't have to
write these.

> diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
> index 1a261719422..12c7042e10a 100644
> --- a/gdb/gdbtypes.c
> +++ b/gdb/gdbtypes.c
> @@ -1894,8 +1894,8 @@ lookup_struct_elt_type (struct type *type, const char *name, int noerr)
>  /* Store in *MAX the largest number representable by unsigned integer type
>     TYPE.  */

I'll update this comment.

>  
> -void
> -get_unsigned_type_max (struct type *type, ULONGEST *max)
> +ULONGEST
> +get_unsigned_type_max (struct type *type)
>  {
>    unsigned int n;
>  
> @@ -1905,7 +1905,7 @@ get_unsigned_type_max (struct type *type, ULONGEST *max)
>  
>    /* Written this way to avoid overflow.  */
>    n = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
> -  *max = ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
> +  return ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1;
>  }
>  
>  /* Store in *MIN, *MAX the smallest and largest numbers representable by
> diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
> index d754f2fcd36..b47644b210e 100644
> --- a/gdb/gdbtypes.h
> +++ b/gdb/gdbtypes.h
> @@ -2519,7 +2519,7 @@ extern struct type *lookup_unsigned_typename (const struct language_defn *,
>  extern struct type *lookup_signed_typename (const struct language_defn *,
>  					    const char *);
>  
> -extern void get_unsigned_type_max (struct type *, ULONGEST *);
> +extern ULONGEST get_unsigned_type_max (struct type *);
>  
>  extern void get_signed_type_minmax (struct type *, LONGEST *, LONGEST *);
>  
> diff --git a/gdb/guile/scm-math.c b/gdb/guile/scm-math.c
> index d9fd6718196..f1c032b6efa 100644
> --- a/gdb/guile/scm-math.c
> +++ b/gdb/guile/scm-math.c
> @@ -529,9 +529,7 @@ vlscm_convert_typed_number (const char *func_name, int obj_arg_pos, SCM obj,
>      {
>        if (type->is_unsigned ())
>  	{
> -	  ULONGEST max;
> -
> -	  get_unsigned_type_max (type, &max);
> +	  ULONGEST max = get_unsigned_type_max (type);
>  	  if (!scm_is_unsigned_integer (obj, 0, max))
>  	    {
>  	      *except_scmp
> @@ -580,7 +578,7 @@ vlscm_integer_fits_p (SCM obj, struct type *type)
>        /* If scm_is_unsigned_integer can't work with this type, just punt.  */
>        if (TYPE_LENGTH (type) > sizeof (uintmax_t))
>  	return 0;
> -      get_unsigned_type_max (type, &max);
> +      max = get_unsigned_type_max (type);
>        return scm_is_unsigned_integer (obj, 0, max);

And move the declaration of `max` where it's initialized.

Thanks,

Simon

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 2/3] guile: fix make-value with pointer type
  2021-07-29 15:12 ` [PATCH v3 2/3] guile: fix make-value with pointer type George Barrett
@ 2021-07-29 16:55   ` Simon Marchi
  0 siblings, 0 replies; 6+ messages in thread
From: Simon Marchi @ 2021-07-29 16:55 UTC (permalink / raw)
  To: George Barrett, gdb-patches

I'll push it with this fixed:

> +/* Store in *MAX the largest value representable by pointer type
> +   TYPE. */

I'll update this comment to reflect that the function now returns the
value.

Simon

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 1/3] gdbtypes: return value from get_unsigned_type_max
  2021-07-29 16:53 ` [PATCH v3 1/3] gdbtypes: return value from get_unsigned_type_max Simon Marchi
@ 2021-07-29 17:01   ` George Barrett
  0 siblings, 0 replies; 6+ messages in thread
From: George Barrett @ 2021-07-29 17:01 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On Thu, Jul 29, 2021 at 12:53:51PM -0400, Simon Marchi wrote:
> Thanks, I'll push it with this small change:
> 
> <snip>

Ah, sorry I missed those. Clearly wasn't paying enough attention.

Thanks!

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-07-29 17:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-29 15:12 [PATCH v3 1/3] gdbtypes: return value from get_unsigned_type_max George Barrett
2021-07-29 15:12 ` [PATCH v3 2/3] guile: fix make-value with pointer type George Barrett
2021-07-29 16:55   ` Simon Marchi
2021-07-29 15:12 ` [PATCH v3 3/3] guile/scm-math: indentation fixes George Barrett
2021-07-29 16:53 ` [PATCH v3 1/3] gdbtypes: return value from get_unsigned_type_max Simon Marchi
2021-07-29 17:01   ` George Barrett

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).