public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] guile: fix make-value with pointer type
@ 2021-05-19 22:26 George Barrett
  2021-06-03 11:42 ` Andrew Burgess
  0 siblings, 1 reply; 3+ messages in thread
From: George Barrett @ 2021-05-19 22:26 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.

gdb/ChangeLog:

2021-05-20  George Barrett  <bob@bob131.so>

	* 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-05-20  George Barrett  <bob@bob131.so>

	* gdb.guile/scm-math.exp (test_value_numeric_ops): Add test
	for creating pointers with make-value.
---
 gdb/guile/scm-math.c                 | 18 ++++++++++++++++--
 gdb/testsuite/gdb.guile/scm-math.exp |  9 +++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/gdb/guile/scm-math.c b/gdb/guile/scm-math.c
index d9fd6718196..9fc4ad505b4 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 ())
 	{
@@ -558,6 +557,21 @@ 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)
+    {
+      gdb_mpz max (1);
+      mpz_mul_2exp (max.val, max.val, gdbarch_addr_bit (type->arch ()));
+      mpz_sub_ui (max.val, max.val, 1);
+      if (!scm_is_unsigned_integer (obj, 0, max.as_integer<CORE_ADDR> ()))
+	{
+	  *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..c128b708d61 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" {
-- 
2.31.1

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

* Re: [PATCH] guile: fix make-value with pointer type
  2021-05-19 22:26 [PATCH] guile: fix make-value with pointer type George Barrett
@ 2021-06-03 11:42 ` Andrew Burgess
  2021-06-03 19:12   ` George Barrett
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Burgess @ 2021-06-03 11:42 UTC (permalink / raw)
  To: George Barrett; +Cc: gdb-patches

* George Barrett via Gdb-patches <gdb-patches@sourceware.org> [2021-05-20 08:26:28 +1000]:

> 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.
> 
> gdb/ChangeLog:
> 
> 2021-05-20  George Barrett  <bob@bob131.so>
> 
> 	* 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-05-20  George Barrett  <bob@bob131.so>
> 
> 	* gdb.guile/scm-math.exp (test_value_numeric_ops): Add test
> 	for creating pointers with make-value.
> ---
>  gdb/guile/scm-math.c                 | 18 ++++++++++++++++--
>  gdb/testsuite/gdb.guile/scm-math.exp |  9 +++++++++
>  2 files changed, 25 insertions(+), 2 deletions(-)
> 
> diff --git a/gdb/guile/scm-math.c b/gdb/guile/scm-math.c
> index d9fd6718196..9fc4ad505b4 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 ())
>  	{
> @@ -558,6 +557,21 @@ 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)
> +    {
> +      gdb_mpz max (1);
> +      mpz_mul_2exp (max.val, max.val, gdbarch_addr_bit (type->arch ()));
> +      mpz_sub_ui (max.val, max.val, 1);

The existing get_unsigned_type_max is only used from guile/scm-math.c,
so it is probably worth extending this API along the same lines, with
something like get_pointer_type_max maybe?

I also wondered about the use of the gmp library here, the existing
get_unsigned_type_max manages without this cost - could the new
get_pointer_type_max just solve this problem the same way?

Thanks,
Andrew


> +      if (!scm_is_unsigned_integer (obj, 0, max.as_integer<CORE_ADDR> ()))
> +	{
> +	  *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..c128b708d61 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" {
> -- 
> 2.31.1

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

* Re: [PATCH] guile: fix make-value with pointer type
  2021-06-03 11:42 ` Andrew Burgess
@ 2021-06-03 19:12   ` George Barrett
  0 siblings, 0 replies; 3+ messages in thread
From: George Barrett @ 2021-06-03 19:12 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

On Thu, Jun 03, 2021 at 12:42:25PM +0100, Andrew Burgess wrote:
> The existing get_unsigned_type_max is only used from guile/scm-math.c,
> so it is probably worth extending this API along the same lines, with
> something like get_pointer_type_max maybe?

Sure, sounds good.

> I also wondered about the use of the gmp library here, the existing
> get_unsigned_type_max manages without this cost - could the new
> get_pointer_type_max just solve this problem the same way?

I don't see why not (although I haven't quite digested the implementation of
get_unsigned_type_max yet). I was mainly aiming for simplicity (and, thinking
of some of the more exotic architectures I've read about, being a bit
paranoid).

Thanks

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

end of thread, other threads:[~2021-06-03 19:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-19 22:26 [PATCH] guile: fix make-value with pointer type George Barrett
2021-06-03 11:42 ` Andrew Burgess
2021-06-03 19:12   ` 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).