Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: Alan Hayward <Alan.Hayward@arm.com>
Cc: Yao Qi <qiyaoltc@gmail.com>,
	"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>,
	nd <nd@arm.com>
Subject: Re: [PATCH 3/11] Add MIPS_MAX_REGISTER_SIZE (2/4)
Date: Fri, 26 May 2017 10:26:00 -0000	[thread overview]
Message-ID: <73867a49-889b-171d-db60-d545da68b8dd@redhat.com> (raw)
In-Reply-To: <CADAFE2E-16AA-4979-9857-CFC2F90B1C91@arm.com>

On 05/26/2017 09:54 AM, Alan Hayward wrote:

> 2017-05-25  Alan Hayward  <alan.hayward@arm.com>
> 
> 	* defs.h (copy_integer_to_size): New declaration.
> 	* findvar.c (copy_integer_to_size): New function.
> 	(do_cuint_test): New selftest function.
> 	(do_cint_test): New selftest function.
> 	(copy_integer_to_size_test): Likewise.
> 	(_initialize_findvar): Likewise.
> 	* mips-fbsd-tdep.c (mips_fbsd_supply_reg): Use raw_supply_integer.
> 	(mips_fbsd_collect_reg): Use raw_collect_integer.
> 	* mips-linux-tdep.c (supply_32bit_reg): Use raw_supply_integer.
> 	(mips64_fill_gregset): Use raw_collect_integer
> 	(mips64_fill_fpregset): Use raw_supply_integer.
> 	* regcache.c (regcache::raw_supply_integer): New function.
> 	(regcache::raw_collect_integer): Likewise.
> 	* regcache.h: (regcache::raw_supply_integer): New declaration.
>         (regcache::raw_collect_integer): Likewise.

Last line looks like needs s/spaces/tab/.

> +#if GDB_SELF_TEST
> +namespace selftests {
> +namespace findvar_tests {
> +
> +/* Functions to test copy_integer_to_size.  Store SOURCE_VAL with size
> +   SOURCE_SIZE to a buffer, making sure no sign extending happens at this
> +   stage.  Copy buffer to a new buffer using copy_integer_to_size.  Extract
> +   copied value and compare to DEST_VAL.  Do all of this for both LITTLE and
> +   BIG target endians.  */
> +
> +static void
> +do_cuint_test (long dest_val, int dest_size, ULONGEST src_val, int src_size)
> +{
> +  for (int i = 0; i < 2 ; i++)
> +    {
> +      ULONGEST srcbuf_val = 0, destbuf_val = 0;
> +      gdb_byte* srcbuf = (gdb_byte*) &srcbuf_val;
> +      gdb_byte* destbuf = (gdb_byte*) &destbuf_val;

Nit, I'd prefer writing instead:

      gdb_byte srcbuf[sizeof (ULONGEST)] = {};
      gdb_byte destbuf[sizeof (ULONGEST)] = {};

I.e., get rid of the local ULONGEST variables.  It took me a second
to realize that below you're writing to these variables instead of
to "dest_val" directly, which made me first wonder about
strict aliasing issues.

(But see further below.)

> +static void
> +do_cint_test (long dest_val, int dest_size, LONGEST src_val, int src_size)
> +{
> +  for (int i = 0; i < 2 ; i++)
> +    {
> +      LONGEST srcbuf_val = 0, destbuf_val = 0;
> +      gdb_byte* srcbuf = (gdb_byte*) &srcbuf_val;
> +      gdb_byte* destbuf = (gdb_byte*) &destbuf_val;

Ditto.

> +      enum bfd_endian byte_order = i ? BFD_ENDIAN_LITTLE : BFD_ENDIAN_BIG;
> +
> +      store_unsigned_integer (srcbuf, src_size, byte_order, src_val);
> +      copy_integer_to_size (destbuf, dest_size, srcbuf, src_size, true,
> +			    byte_order);
> +      SELF_CHECK (dest_val == extract_signed_integer (destbuf, dest_size,
> +						      byte_order));
> +    }
> +}
> +
> +static void
> +copy_integer_to_size_test ()
> +{

...

> +  do_cint_test (0xffffffffdeadbeef, 8, 0xdeadbeef, 4);
> +  do_cint_test (0xffffffffffadbeef, 8, 0xdeadbeef, 3);
> +  do_cint_test (0xffffffffffffbeef, 2, 0xbeef, 3);

Note that:

> +static void
> +do_cint_test (long dest_val, int dest_size, LONGEST src_val, int src_size)

"long" can be 32-bit, depending on host.  It's 32-bit on 64-bit Windows.

So you're passing 0xffffffffffffbeef, which gets truncated
to 0xffffbeef, but then sign extended here again:

      SELF_CHECK (dest_val == extract_signed_integer (destbuf, dest_size,
						      byte_order));

and it'll work out (by chance).

I think it would be better to always pass an unsigned ULONGEST
as dest_val:

 static void
 do_cint_test (ULONGEST dest_val, int dest_size, LONGEST src_val, int src_size)

likewise for do_cuint_test, of course.

Makes me notice that you didn't add any unsigned test for
src/dest size > 4, which would fail on 64-bit Windows, for the
same reason (32-bit long), like:

 do_cuint_test (0xff12345678, 8, 0xff12345678, 6);


I notice you have no tests that exercise src_size == dest_size,
for multiple sizes.  It'd be good to add them.


Another issue with the test is that by relying on
extract_(un)signed_integer, the test can't notice if the
zero / sign extension in copy_integer_bytes filled the right
number of bytes:

> +  do_cint_test (0xffffffffffffbeef, 2, 0xbeef, 3);

I.e., above, the last test _must_ not write more than
two bytes to the destination, so those leading
0xFFs before "beef" are artificial.

Passing ULONGEST as destination value avoids that problem, making
you write that last test as:

   do_cint_test (0x000000000000beef, 2, 0xbeef, 3);


That brings us to the next problem, that you can't tell
whether zero extension writes the zeros in the first place.

I.e., here:

 do_cuint_test (0x12345678, 8, 0x12345678, 4);

Given do_cuint_test does this:

+      ULONGEST srcbuf_val = 0, destbuf_val = 0;

You can't tell whether copy_integer_to_size really
filled in bytes 4-8 with zero.

You can get that back by making the do_cint_test/do_int_test
functions memset the temporary buffers with some byte
that the callers never pass down as part of any src/dest value.
Like, say reserve 0xaa for that:

      gdb_byte srcbuf[sizeof (ULONGEST)];
      gdb_byte destbuf[sizeof (ULONGEST)];

      memset (srcbuf, 0xaa, sizeof (srcbuf));
      memset (destbuf, 0xaa, sizeof (srcbuf));

(with suitable comments)

So in sum:

#1 - ULONGEST as destination value, both signed and unsigned tests.
#2 - Add unsigned tests with size >4
#3 - Add tests src_size == dest_size
#4 - Use gdb_byte arrays for temporary buffers, and memset them with 0xaa.
#5 - Adjust tests to pass.
#6 - Add comments where appropriate.

Thanks,
Pedro Alves


  reply	other threads:[~2017-05-26 10:26 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-04 10:12 [PATCH 3/11] Add MIPS_MAX_REGISTER_SIZE Alan Hayward
2017-04-04 17:19 ` John Baldwin
2017-04-05 10:27   ` Alan Hayward
2017-04-11 15:37 ` Yao Qi
2017-05-05  8:03   ` [PATCH 3/11] Add MIPS_MAX_REGISTER_SIZE (1/4) Alan Hayward
2017-05-05 21:44     ` Yao Qi
2017-05-05  8:04   ` [PATCH 3/11] Add MIPS_MAX_REGISTER_SIZE (4/4) Alan Hayward
     [not found]     ` <434A7317-C19A-4B53-8CB1-C7B4ACEC7D17@arm.com>
2017-06-08 20:27       ` Maciej W. Rozycki
2017-06-09 10:31         ` Alan Hayward
     [not found]           ` <ebebb23e-e61b-5eff-b666-f2632e554322@redhat.com>
2017-06-09 11:48             ` Maciej W. Rozycki
2017-06-09 12:05               ` Pedro Alves
2017-06-09 13:23                 ` Maciej W. Rozycki
2017-06-09 14:29                   ` Pedro Alves
2017-06-12  9:09                     ` Alan Hayward
2017-06-12 18:11                       ` Pedro Alves
2017-06-12 14:29                     ` Maciej W. Rozycki
2017-05-05  8:04   ` [PATCH 3/11] Add MIPS_MAX_REGISTER_SIZE (2/4) Alan Hayward
2017-05-05 19:51     ` John Baldwin
2017-05-12  8:53     ` Yao Qi
2017-05-16 11:16       ` Alan Hayward
2017-05-22 12:07         ` Yao Qi
2017-05-22 16:05           ` Alan Hayward
2017-05-22 17:15             ` Pedro Alves
2017-05-23 17:49               ` Alan Hayward
2017-05-23 18:30                 ` Pedro Alves
2017-05-24  9:08                   ` Alan Hayward
2017-05-24  9:29                     ` Pedro Alves
     [not found]                     ` <40597975-9458-e9af-8915-9d303bb1ed98@redhat.com>
2017-05-24 10:20                       ` Alan Hayward
     [not found]                         ` <d32041ef-9df3-316a-68ca-3fe2a2797539@redhat.com>
2017-05-24 19:45                           ` Alan Hayward
2017-05-25 10:46                             ` Pedro Alves
2017-05-25 11:43                               ` Yao Qi
2017-05-25 11:48                                 ` Pedro Alves
2017-05-26  8:54                                   ` Alan Hayward
2017-05-26 10:26                                     ` Pedro Alves [this message]
2017-05-26 15:30                                       ` Alan Hayward
2017-05-26 15:49                                         ` Pedro Alves
2017-05-26 16:18                                           ` Alan Hayward
2017-05-26 16:00                                         ` John Baldwin
2017-05-05  8:04   ` [PATCH 3/11] Add MIPS_MAX_REGISTER_SIZE (3/4) Alan Hayward
2017-05-05 21:54     ` Yao Qi

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=73867a49-889b-171d-db60-d545da68b8dd@redhat.com \
    --to=palves@redhat.com \
    --cc=Alan.Hayward@arm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=nd@arm.com \
    --cc=qiyaoltc@gmail.com \
    /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