public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/40645]  New: Bus error caused by ldd/std instructions in struct copy.
@ 2009-07-04  1:33 dentongosnell at yahoo dot com
  2009-07-04  1:38 ` Andrew Pinski
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: dentongosnell at yahoo dot com @ 2009-07-04  1:33 UTC (permalink / raw)
  To: gcc-bugs

$ gcc -v
Using built-in specs.
Target: sparc-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1.1'
--with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3
--program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug
--enable-objc-gc --enable-mpfr --with-cpu=v8 --with-long-double-128
--enable-checking=release --build=sparc-linux-gnu --host=sparc-linux-gnu
--target=sparc-linux-gnu
Thread model: posix
gcc version 4.3.2 (Debian 4.3.2-1.1)

To trigger the bug :-

$ gcc align_bug.c
$ ./a.out
Bus error
$

Here is align_bug.c :-

---------

#include <stdio.h>

struct b_one {
  int i;
  double d;
};

struct b_two {
  int i1;
  int i2;
};

union myblock {
    struct b_one one;
    struct b_two two;
};

void myfunc(union myblock *dp1, union myblock *dp2)
{
  dp2->two = dp1->two;
}

int main()
{
  int w;
  struct b_two a = {1,2};
  struct b_two b;

  myfunc((union myblock *)&a, (union myblock *)&b);

  printf("%d %d\n", b.i1, b.i2);

  return 0;
}

----------

The problem seems to happen in "myfunc" when the compiled code tries
to copy the 8-byte structure dp2->two to dp1->two, using a ldd/std
instruction pair.  The problem seems to be that dp1 and dp2 (ie a and
b in main) aren't strictly enough aligned for that.  If you take out
the redundant "int w" in main then a and b happen to be aligned okay
and the bus error doesn't happen.

I think the compiler is assuming "union myblock" has the same
alignment as "struct b_one", which is more strictly aligned than
"struct b_two" because of its double member.

I realise that casting &a to (union myblock*) in main may technically
invoke undefined behaviour... but I think the cast is reasonable given
that union myblock contains the type of a.

One other thing, there is a flag "-mno-faster-structs" which this page
suggests would prevent this sort of ldd/std use
(http://gcc.gnu.org/onlinedocs/gcc/SPARC-Options.html).  Unfortunately
this flag doesn't seem to make any difference to this case.


-- 
           Summary: Bus error caused by ldd/std instructions in struct copy.
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dentongosnell at yahoo dot com
 GCC build triplet: sparc-linux-gnu
  GCC host triplet: sparc-linux-gnu
GCC target triplet: sparc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40645


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

* [Bug c/40645] Bus error caused by ldd/std instructions in struct copy.
  2009-07-04  1:33 [Bug c/40645] New: Bus error caused by ldd/std instructions in struct copy dentongosnell at yahoo dot com
  2009-07-04  1:38 ` Andrew Pinski
@ 2009-07-04  1:38 ` pinskia at gmail dot com
  2009-07-04  6:43 ` ebotcazou at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gmail dot com @ 2009-07-04  1:38 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gmail dot com  2009-07-04 01:38 -------
Subject: Re:   New: Bus error caused by ldd/std instructions in struct copy.

This code is undefined because of alignment requirments differences  
for the structs and the union.

Sent from my iPhone

On Jul 3, 2009, at 6:33 PM, "dentongosnell at yahoo dot com"
<gcc-bugzilla@gcc.gnu.org 
 > wrote:

> $ gcc -v
> Using built-in specs.
> Target: sparc-linux-gnu
> Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1.1 
> '
> --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
> --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable- 
> shared
> --with-system-zlib --libexecdir=/usr/lib --without-included-gettext
> --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/ 
> include/c++/4.3
> --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug
> --enable-objc-gc --enable-mpfr --with-cpu=v8 --with-long-double-128
> --enable-checking=release --build=sparc-linux-gnu --host=sparc-linux- 
> gnu
> --target=sparc-linux-gnu
> Thread model: posix
> gcc version 4.3.2 (Debian 4.3.2-1.1)
>
> To trigger the bug :-
>
> $ gcc align_bug.c
> $ ./a.out
> Bus error
> $
>
> Here is align_bug.c :-
>
> ---------
>
> #include <stdio.h>
>
> struct b_one {
>  int i;
>  double d;
> };
>
> struct b_two {
>  int i1;
>  int i2;
> };
>
> union myblock {
>    struct b_one one;
>    struct b_two two;
> };
>
> void myfunc(union myblock *dp1, union myblock *dp2)
> {
>  dp2->two = dp1->two;
> }
>
> int main()
> {
>  int w;
>  struct b_two a = {1,2};
>  struct b_two b;
>
>  myfunc((union myblock *)&a, (union myblock *)&b);
>
>  printf("%d %d\n", b.i1, b.i2);
>
>  return 0;
> }
>
> ----------
>
> The problem seems to happen in "myfunc" when the compiled code tries
> to copy the 8-byte structure dp2->two to dp1->two, using a ldd/std
> instruction pair.  The problem seems to be that dp1 and dp2 (ie a and
> b in main) aren't strictly enough aligned for that.  If you take out
> the redundant "int w" in main then a and b happen to be aligned okay
> and the bus error doesn't happen.
>
> I think the compiler is assuming "union myblock" has the same
> alignment as "struct b_one", which is more strictly aligned than
> "struct b_two" because of its double member.
>
> I realise that casting &a to (union myblock*) in main may technically
> invoke undefined behaviour... but I think the cast is reasonable given
> that union myblock contains the type of a.
>
> One other thing, there is a flag "-mno-faster-structs" which this page
> suggests would prevent this sort of ldd/std use
> (http://gcc.gnu.org/onlinedocs/gcc/SPARC-Options.html).  Unfortunately
> this flag doesn't seem to make any difference to this case.
>
>
> -- 
>           Summary: Bus error caused by ldd/std instructions in  
> struct copy.
>           Product: gcc
>           Version: 4.3.2
>            Status: UNCONFIRMED
>          Severity: normal
>          Priority: P3
>         Component: c
>        AssignedTo: unassigned at gcc dot gnu dot org
>        ReportedBy: dentongosnell at yahoo dot com
> GCC build triplet: sparc-linux-gnu
>  GCC host triplet: sparc-linux-gnu
> GCC target triplet: sparc-linux-gnu
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40645
>


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40645


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

* Re: [Bug c/40645]  New: Bus error caused by ldd/std instructions in struct copy.
  2009-07-04  1:33 [Bug c/40645] New: Bus error caused by ldd/std instructions in struct copy dentongosnell at yahoo dot com
@ 2009-07-04  1:38 ` Andrew Pinski
  2009-07-04  1:38 ` [Bug c/40645] " pinskia at gmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Andrew Pinski @ 2009-07-04  1:38 UTC (permalink / raw)
  To: gcc-bugzilla; +Cc: gcc-bugs

This code is undefined because of alignment requirments differences  
for the structs and the union.

Sent from my iPhone

On Jul 3, 2009, at 6:33 PM, "dentongosnell at yahoo dot com" <gcc-bugzilla@gcc.gnu.org 
 > wrote:

> $ gcc -v
> Using built-in specs.
> Target: sparc-linux-gnu
> Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1.1 
> '
> --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
> --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable- 
> shared
> --with-system-zlib --libexecdir=/usr/lib --without-included-gettext
> --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/ 
> include/c++/4.3
> --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug
> --enable-objc-gc --enable-mpfr --with-cpu=v8 --with-long-double-128
> --enable-checking=release --build=sparc-linux-gnu --host=sparc-linux- 
> gnu
> --target=sparc-linux-gnu
> Thread model: posix
> gcc version 4.3.2 (Debian 4.3.2-1.1)
>
> To trigger the bug :-
>
> $ gcc align_bug.c
> $ ./a.out
> Bus error
> $
>
> Here is align_bug.c :-
>
> ---------
>
> #include <stdio.h>
>
> struct b_one {
>  int i;
>  double d;
> };
>
> struct b_two {
>  int i1;
>  int i2;
> };
>
> union myblock {
>    struct b_one one;
>    struct b_two two;
> };
>
> void myfunc(union myblock *dp1, union myblock *dp2)
> {
>  dp2->two = dp1->two;
> }
>
> int main()
> {
>  int w;
>  struct b_two a = {1,2};
>  struct b_two b;
>
>  myfunc((union myblock *)&a, (union myblock *)&b);
>
>  printf("%d %d\n", b.i1, b.i2);
>
>  return 0;
> }
>
> ----------
>
> The problem seems to happen in "myfunc" when the compiled code tries
> to copy the 8-byte structure dp2->two to dp1->two, using a ldd/std
> instruction pair.  The problem seems to be that dp1 and dp2 (ie a and
> b in main) aren't strictly enough aligned for that.  If you take out
> the redundant "int w" in main then a and b happen to be aligned okay
> and the bus error doesn't happen.
>
> I think the compiler is assuming "union myblock" has the same
> alignment as "struct b_one", which is more strictly aligned than
> "struct b_two" because of its double member.
>
> I realise that casting &a to (union myblock*) in main may technically
> invoke undefined behaviour... but I think the cast is reasonable given
> that union myblock contains the type of a.
>
> One other thing, there is a flag "-mno-faster-structs" which this page
> suggests would prevent this sort of ldd/std use
> (http://gcc.gnu.org/onlinedocs/gcc/SPARC-Options.html).  Unfortunately
> this flag doesn't seem to make any difference to this case.
>
>
> -- 
>           Summary: Bus error caused by ldd/std instructions in  
> struct copy.
>           Product: gcc
>           Version: 4.3.2
>            Status: UNCONFIRMED
>          Severity: normal
>          Priority: P3
>         Component: c
>        AssignedTo: unassigned at gcc dot gnu dot org
>        ReportedBy: dentongosnell at yahoo dot com
> GCC build triplet: sparc-linux-gnu
>  GCC host triplet: sparc-linux-gnu
> GCC target triplet: sparc-linux-gnu
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40645
>


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

* [Bug c/40645] Bus error caused by ldd/std instructions in struct copy.
  2009-07-04  1:33 [Bug c/40645] New: Bus error caused by ldd/std instructions in struct copy dentongosnell at yahoo dot com
  2009-07-04  1:38 ` Andrew Pinski
  2009-07-04  1:38 ` [Bug c/40645] " pinskia at gmail dot com
@ 2009-07-04  6:43 ` ebotcazou at gcc dot gnu dot org
  2009-07-04 13:32 ` dentongosnell at yahoo dot com
  2009-07-04 13:52 ` ebotcazou at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: ebotcazou at gcc dot gnu dot org @ 2009-07-04  6:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from ebotcazou at gcc dot gnu dot org  2009-07-04 06:43 -------
> I think the compiler is assuming "union myblock" has the same
> alignment as "struct b_one", which is more strictly aligned than
> "struct b_two" because of its double member.

That's right and it's prescribed by the ABI.

> I realise that casting &a to (union myblock*) in main may technically
> invoke undefined behaviour... but I think the cast is reasonable given
> that union myblock contains the type of a.

The notion of "reasonable" would be quite tricky to define... it's indeed plain
undefined behaviour so a SIGBUS at run time is OK.

> One other thing, there is a flag "-mno-faster-structs" which this page
> suggests would prevent this sort of ldd/std use
> (http://gcc.gnu.org/onlinedocs/gcc/SPARC-Options.html).  Unfortunately
> this flag doesn't seem to make any difference to this case.

-mno-faster-structs is the default.


-- 

ebotcazou at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ebotcazou at gcc dot gnu dot
                   |                            |org
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40645


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

* [Bug c/40645] Bus error caused by ldd/std instructions in struct copy.
  2009-07-04  1:33 [Bug c/40645] New: Bus error caused by ldd/std instructions in struct copy dentongosnell at yahoo dot com
                   ` (2 preceding siblings ...)
  2009-07-04  6:43 ` ebotcazou at gcc dot gnu dot org
@ 2009-07-04 13:32 ` dentongosnell at yahoo dot com
  2009-07-04 13:52 ` ebotcazou at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: dentongosnell at yahoo dot com @ 2009-07-04 13:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from dentongosnell at yahoo dot com  2009-07-04 13:32 -------
(In reply to comment #2)

> 
> > One other thing, there is a flag "-mno-faster-structs" which this page
> > suggests would prevent this sort of ldd/std use
> > (http://gcc.gnu.org/onlinedocs/gcc/SPARC-Options.html).  Unfortunately
> > this flag doesn't seem to make any difference to this case.
> 
> -mno-faster-structs is the default.
> 

If "-mno-faster-structs" is the default, then surely it shouldn't be generating
ldd/std in this case (and assuming the 8-byte alignment for "union myblock"). 
The code it is generating is what I would expect if I passed
"-mfaster-structs", based on the above-mentioned documentation page.

In other words, gcc align_bug.c should give just ld/st pairs, and hence should
work without a bus error.

I think either the compiler or the documentation has a bug!


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40645


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

* [Bug c/40645] Bus error caused by ldd/std instructions in struct copy.
  2009-07-04  1:33 [Bug c/40645] New: Bus error caused by ldd/std instructions in struct copy dentongosnell at yahoo dot com
                   ` (3 preceding siblings ...)
  2009-07-04 13:32 ` dentongosnell at yahoo dot com
@ 2009-07-04 13:52 ` ebotcazou at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: ebotcazou at gcc dot gnu dot org @ 2009-07-04 13:52 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from ebotcazou at gcc dot gnu dot org  2009-07-04 13:52 -------
> If "-mno-faster-structs" is the default, then surely it shouldn't be
> generating ldd/std in this case (and assuming the 8-byte alignment for
> "union myblock").

No, -mno-faster-structs doesn't modify the ABI.  The SPARC ABI requires union
myblock to have 64-bit alignment because it contains a structure that has
64-bit alignment because it contains a double.  So the compiler can always use
ldd for union myblock.

-mfaster-structs does modify the ABI by promoting structures that have less
than 64-bit alignment as per the SPARC ABI, e.g. struct b_two.  Without it, ldd
cannot be used for struct b_two; with it, lld can be used for struct b_two.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40645


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

end of thread, other threads:[~2009-07-04 13:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-04  1:33 [Bug c/40645] New: Bus error caused by ldd/std instructions in struct copy dentongosnell at yahoo dot com
2009-07-04  1:38 ` Andrew Pinski
2009-07-04  1:38 ` [Bug c/40645] " pinskia at gmail dot com
2009-07-04  6:43 ` ebotcazou at gcc dot gnu dot org
2009-07-04 13:32 ` dentongosnell at yahoo dot com
2009-07-04 13:52 ` ebotcazou at gcc dot gnu dot org

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