public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* MIPS: va_arg is unable to correct extract an empty zero-length array
@ 2011-03-15 18:00 Nick Clifton
  2011-03-15 23:39 ` Richard Sandiford
  0 siblings, 1 reply; 5+ messages in thread
From: Nick Clifton @ 2011-03-15 18:00 UTC (permalink / raw)
  To: echristo, rdsandiford; +Cc: gcc-bugs

[-- Attachment #1: Type: text/plain, Size: 684 bytes --]

Hi Eric, Hi Richard,

  A customer has reported the following bug with the MIPS target.  Since
  it is for a GNU extension to the C language (zero-length arrays) that
  is being used in a non-intended fashion (the zero-length array is in a
  structure with no other fields) I doubt if you will want to
  investigate the problem too much, but I thought that it was worth
  reporting anyway:

    % mips64vr-elf-gcc -mgp32 -O2 -Tddb.ld -march=vr5500 bug.c
    % mips64vr-elf-run a.out
    assertion "arg4 == va4" failed: file "bug.c", line 40, function: foo

  As an aside if the type3 structure in bug.c is given another,
  non-zero-length field, then the test passes.

Cheers
  Nick


[-- Attachment #2: bug.c --]
[-- Type: text/plain, Size: 699 bytes --]

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>

typedef int * type1;

typedef struct 
{
  union u1 { double f1; long int f2; } f3[0];
} type3;

typedef int type4;

static type1 arg1 = 0;
static double arg2 = 1.0;
static type3 arg3 = {{}};
static type4 arg4 = 0x12345678;

void
foo (double parm,
     ...)
{
  va_list  ap;
  type1    va1;
  double   va2;
  type3    va3;
  type4    va4;

  va_start (ap, parm);

  va1 = va_arg (ap, type1);
  assert (arg1 == va1);

  va2 = va_arg (ap, double);
  assert (arg2 == va2);

  va3 = va_arg (ap, type3);

  va4 = va_arg (ap, type4);
  assert (arg4 == va4);

  va_end (ap);
}

int
main (void)
{
  foo (1.0, arg1, arg2, arg3, arg4);
  return 0;
}

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

* Re: MIPS: va_arg is unable to correct extract an empty zero-length array
  2011-03-15 18:00 MIPS: va_arg is unable to correct extract an empty zero-length array Nick Clifton
@ 2011-03-15 23:39 ` Richard Sandiford
  2011-03-16 14:56   ` Nick Clifton
  0 siblings, 1 reply; 5+ messages in thread
From: Richard Sandiford @ 2011-03-15 23:39 UTC (permalink / raw)
  To: Nick Clifton; +Cc: echristo, gcc-bugs

Nick Clifton <nickc@redhat.com> writes:
> Hi Eric, Hi Richard,
>
>   A customer has reported the following bug with the MIPS target.  Since
>   it is for a GNU extension to the C language (zero-length arrays) that
>   is being used in a non-intended fashion (the zero-length array is in a
>   structure with no other fields) I doubt if you will want to
>   investigate the problem too much, but I thought that it was worth
>   reporting anyway:
>
>     % mips64vr-elf-gcc -mgp32 -O2 -Tddb.ld -march=vr5500 bug.c
>     % mips64vr-elf-run a.out
>     assertion "arg4 == va4" failed: file "bug.c", line 40, function: foo
>
>   As an aside if the type3 structure in bug.c is given another,
>   non-zero-length field, then the test passes.

Interesting test case :-)  Certainly looks like a genuine bug though.
I see the same thing happens for varargs that are passed on the stack
as well (not just those passed in registers).

Fortunately, it looks like the bug is on the varargs side, so no ABI
change is needed.  Does the attached patch work?  I'll try to do a
full test this weekend.

Richard


Index: gcc/config/mips/mips.c
===================================================================
--- gcc/config/mips/mips.c	(revision 170697)
+++ gcc/config/mips/mips.c	(working copy)
@@ -5625,6 +5625,10 @@
 		     NULL_TREE);
       size = int_size_in_bytes (type);
 
+      /* Even zero-sized arguments occupy one byte.  */
+      if (size == 0)
+	size = 1;
+
       if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
 	  && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
 	{


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

* Re: MIPS: va_arg is unable to correct extract an empty zero-length array
  2011-03-15 23:39 ` Richard Sandiford
@ 2011-03-16 14:56   ` Nick Clifton
  2011-03-17 22:00     ` Richard Sandiford
  2011-03-20 21:06     ` Richard Sandiford
  0 siblings, 2 replies; 5+ messages in thread
From: Nick Clifton @ 2011-03-16 14:56 UTC (permalink / raw)
  To: echristo, gcc-bugs, rdsandiford

Hi Richard,

> +      /* Even zero-sized arguments occupy one byte.  */
> +      if (size == 0)
> +	size = 1;

That fixes it!  Thanks.

Will you apply this patch yourself, or should I submit the patch and the 
test case as a separate email to gcc-patches ?

Cheers
   Nick



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

* Re: MIPS: va_arg is unable to correct extract an empty zero-length array
  2011-03-16 14:56   ` Nick Clifton
@ 2011-03-17 22:00     ` Richard Sandiford
  2011-03-20 21:06     ` Richard Sandiford
  1 sibling, 0 replies; 5+ messages in thread
From: Richard Sandiford @ 2011-03-17 22:00 UTC (permalink / raw)
  To: Nick Clifton; +Cc: echristo, gcc-bugs

Nick Clifton <nickc@redhat.com> writes:
>> +      /* Even zero-sized arguments occupy one byte.  */
>> +      if (size == 0)
>> +	size = 1;
>
> That fixes it!

Great!  Thanks for confirming.

> Will you apply this patch yourself, or should I submit the patch and the 
> test case as a separate email to gcc-patches ?

It's OK, I'll try to do it this weekend.  I'd like to add a testcase for
stack arguments as well.

Richard


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

* Re: MIPS: va_arg is unable to correct extract an empty zero-length array
  2011-03-16 14:56   ` Nick Clifton
  2011-03-17 22:00     ` Richard Sandiford
@ 2011-03-20 21:06     ` Richard Sandiford
  1 sibling, 0 replies; 5+ messages in thread
From: Richard Sandiford @ 2011-03-20 21:06 UTC (permalink / raw)
  To: Nick Clifton; +Cc: gcc-bugs

Nick Clifton <nickc@redhat.com> writes:
> Hi Richard,
>
>> +      /* Even zero-sized arguments occupy one byte.  */
>> +      if (size == 0)
>> +	size = 1;
>
> That fixes it!  Thanks.
>
> Will you apply this patch yourself, or should I submit the patch and the 
> test case as a separate email to gcc-patches ?

It turns out the patch was wrong.  The testsuite has several
other tests for zero-sized varargs, and we handle those correctly.
The difference here seems to be that the type has doubleword alignment.

The type doesn't occupy room as such.  The problem is that its alignment
is still honoured, so if you have something like:

  word
  double-word-aligned zero-size value
  word

then a word of padding is inserted between the arguments.

I'll need to think about this a bit more...

Richard


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

end of thread, other threads:[~2011-03-20 19:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-15 18:00 MIPS: va_arg is unable to correct extract an empty zero-length array Nick Clifton
2011-03-15 23:39 ` Richard Sandiford
2011-03-16 14:56   ` Nick Clifton
2011-03-17 22:00     ` Richard Sandiford
2011-03-20 21:06     ` Richard Sandiford

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