public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* bug in gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5)
@ 2005-08-02 13:34 Mark Frazer
  2005-08-02 13:46 ` Richard Guenther
  2005-08-02 13:48 ` Mark Frazer
  0 siblings, 2 replies; 5+ messages in thread
From: Mark Frazer @ 2005-08-02 13:34 UTC (permalink / raw)
  To: gcc

Hello.  I'm not on the list, so please CC me with any replies.

I have come across a bug found during some code which serializes
doubles.  The bug is only encountered when the optimization level is set
to -O2 or greater.

The bug is not encountered when compiled under
	gcc (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
at any optimization level.

The de-serialization code is:

typedef unsigned char uint8;
static uint8 next_byte(uint &offset, std::vector<uint8> const &bytecode)
   throw (std::invalid_argument)
{
   if (offset >= bytecode.size())
      throw (std::invalid_argument("Unexpected end of bytecode"));
   return bytecode[offset++];
}

double parse_double(uint &offset, std::vector<unsigned char> const &bytecode)
   throw (std::invalid_argument)
{
   typedef unsigned long long uint64;
   uint64 rtn = uint64(next_byte(offset, bytecode)) << 56;
   rtn |= uint64(next_byte(offset, bytecode)) << 48;
   rtn |= uint64(next_byte(offset, bytecode)) << 40;
   rtn |= uint64(next_byte(offset, bytecode)) << 32;
   rtn |= uint64(next_byte(offset, bytecode)) << 24;
   rtn |= uint64(next_byte(offset, bytecode)) << 16;
   rtn |= uint64(next_byte(offset, bytecode)) << 8;
   rtn |= uint64(next_byte(offset, bytecode));
   return *reinterpret_cast<double*>(&rtn);
}

Full source code to a demonstration of the bug, and a Makefile is at
	http://mjfrazer.org/~mjfrazer/tmp/pack-test/
The tar file in the directory contains all the other files, so you just
need to grab that.

cheers
-mark

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

* Re: bug in gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5)
  2005-08-02 13:34 bug in gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5) Mark Frazer
@ 2005-08-02 13:46 ` Richard Guenther
  2005-08-02 13:47   ` Mark Frazer
  2005-08-02 13:48 ` Mark Frazer
  1 sibling, 1 reply; 5+ messages in thread
From: Richard Guenther @ 2005-08-02 13:46 UTC (permalink / raw)
  To: Mark Frazer; +Cc: gcc

On 8/2/05, Mark Frazer <mark@mjfrazer.org> wrote:
> Hello.  I'm not on the list, so please CC me with any replies.
> 
> I have come across a bug found during some code which serializes
> doubles.  The bug is only encountered when the optimization level is set
> to -O2 or greater.
> 
> The bug is not encountered when compiled under
>         gcc (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
> at any optimization level.
> 
> The de-serialization code is:
> 
> typedef unsigned char uint8;
> static uint8 next_byte(uint &offset, std::vector<uint8> const &bytecode)
>    throw (std::invalid_argument)
> {
>    if (offset >= bytecode.size())
>       throw (std::invalid_argument("Unexpected end of bytecode"));
>    return bytecode[offset++];
> }
> 
> double parse_double(uint &offset, std::vector<unsigned char> const &bytecode)
>    throw (std::invalid_argument)
> {
>    typedef unsigned long long uint64;
>    uint64 rtn = uint64(next_byte(offset, bytecode)) << 56;
>    rtn |= uint64(next_byte(offset, bytecode)) << 48;
>    rtn |= uint64(next_byte(offset, bytecode)) << 40;
>    rtn |= uint64(next_byte(offset, bytecode)) << 32;
>    rtn |= uint64(next_byte(offset, bytecode)) << 24;
>    rtn |= uint64(next_byte(offset, bytecode)) << 16;
>    rtn |= uint64(next_byte(offset, bytecode)) << 8;
>    rtn |= uint64(next_byte(offset, bytecode));
>    return *reinterpret_cast<double*>(&rtn);
> }
> 
> Full source code to a demonstration of the bug, and a Makefile is at
>         http://mjfrazer.org/~mjfrazer/tmp/pack-test/
> The tar file in the directory contains all the other files, so you just
> need to grab that.

Try -fno-strict-aliasing.  This may be related to PR23192.

Richard.

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

* Re: bug in gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5)
  2005-08-02 13:46 ` Richard Guenther
@ 2005-08-02 13:47   ` Mark Frazer
  2005-08-02 14:32     ` Mark Frazer
  0 siblings, 1 reply; 5+ messages in thread
From: Mark Frazer @ 2005-08-02 13:47 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc

Richard Guenther <richard.guenther@gmail.com> [05/08/02 09:29]:
> Try -fno-strict-aliasing.  This may be related to PR23192.

-fno-strict-aliasing does indeed make the problem go away.

thanks!
-mark

-- 
Forget your stupid theme park! I'm gonna make my own! With hookers! And
blackjack! In fact, forget the theme park! - Bender

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

* Re: bug in gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5)
  2005-08-02 13:34 bug in gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5) Mark Frazer
  2005-08-02 13:46 ` Richard Guenther
@ 2005-08-02 13:48 ` Mark Frazer
  1 sibling, 0 replies; 5+ messages in thread
From: Mark Frazer @ 2005-08-02 13:48 UTC (permalink / raw)
  To: gcc

Mark Frazer <mark@mjfrazer.org> [05/08/02 09:18]:
> Hello.  I'm not on the list, so please CC me with any replies.
> 
> I have come across a bug found during some code which serializes
> doubles.  The bug is only encountered when the optimization level is set
> to -O2 or greater.

Oh, I forgot to mention that I'm running Fedora Core 4 on ia32.

[mjfrazer@pacific pack-test]$ gcc --version
gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[mjfrazer@pacific pack-test]$ uname -a
Linux pacific.mjfrazer.org 2.6.11-1.1369_FC4 #1 Thu Jun 2 22:55:56 EDT 2005 i686 athlon i386 GNU/Linux

-mark
-- 
Forget your stupid theme park! I'm gonna make my own! With hookers! And
blackjack! In fact, forget the theme park! - Bender

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

* Re: bug in gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5)
  2005-08-02 13:47   ` Mark Frazer
@ 2005-08-02 14:32     ` Mark Frazer
  0 siblings, 0 replies; 5+ messages in thread
From: Mark Frazer @ 2005-08-02 14:32 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc

Mark Frazer <mark@mjfrazer.org> [05/08/02 09:32]:
> Richard Guenther <richard.guenther@gmail.com> [05/08/02 09:29]:
> > Try -fno-strict-aliasing.  This may be related to PR23192.
> 
> -fno-strict-aliasing does indeed make the problem go away.

changing the de-serialization function to:

      double parse_double(uint &offset, vector<uint8> const &bytecode)
         throw (std::invalid_argument)
      {
         union {
            uint64 ival;
            double dval;
         } rtn;
         rtn.ival  = uint64(next_byte(offset, bytecode)) << 56;
         rtn.ival |= uint64(next_byte(offset, bytecode)) << 48;
         rtn.ival |= uint64(next_byte(offset, bytecode)) << 40;
         rtn.ival |= uint64(next_byte(offset, bytecode)) << 32;
         rtn.ival |= uint64(next_byte(offset, bytecode)) << 24;
         rtn.ival |= uint64(next_byte(offset, bytecode)) << 16;
         rtn.ival |= uint64(next_byte(offset, bytecode)) << 8;
         rtn.ival |= uint64(next_byte(offset, bytecode));
         return rtn.dval;
      }

Allows for the strict-aliasing optimization to be left in.  So, it seems
the bug was mine, not gcc's.

I'm off to search for other reinterpret_cast abuses in my code...

cheers
-mark
-- 
To Captain Bender! He's the best! ...at being a big jerk who's stupid and
his big ugly face is as dumb as a butt! - Fry

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

end of thread, other threads:[~2005-08-02 14:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-02 13:34 bug in gcc (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5) Mark Frazer
2005-08-02 13:46 ` Richard Guenther
2005-08-02 13:47   ` Mark Frazer
2005-08-02 14:32     ` Mark Frazer
2005-08-02 13:48 ` Mark Frazer

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