public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/64037] New: Miscompilation with LTO and enum class : char parameter
@ 2014-11-23 23:07 js at alien8 dot de
  2014-11-23 23:08 ` [Bug c++/64037] " js at alien8 dot de
                   ` (28 more replies)
  0 siblings, 29 replies; 30+ messages in thread
From: js at alien8 dot de @ 2014-11-23 23:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64037

            Bug ID: 64037
           Summary: Miscompilation with LTO and enum class : char
                    parameter
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: js at alien8 dot de

Created attachment 34082
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=34082&action=edit
Minimal test case

On Linux with the x86-64 target, code that contains a byte-sized enum class as
a function parameter is miscompiled when -Os and -flto is enabled. A minimal
example is attached. I also attached g++ --verbose --version output for both
compilers where this triggers (4.9.1 and 4.9.2).

To reproduce the problem compile the attached source with:
g++ -std=gnu++11 -Os -flto test.cc -o test

Run ./test and you get this output: deadbe02
Expected output: 00000002

The problem boils down to the fact that the third parameter to foo (see below)
is passed in DL (the rest of RDX is not cleared). In the function, no code is
emitted to clear the rest of RDX, but it is obviously assumed to be zero.

enum class X : unsigned char {
  V = 2,
};

// noinline and noclone are only there to reduce the example. This
// also triggers without these annotations in larger projects.
void foo(unsigned &out, unsigned a, X b) __attribute__((noinline,noclone));

void foo(unsigned &out, unsigned a, X b)
{
  out = static_cast<unsigned>(b);
}

int main()
{
  // Load obvious value into EDX
  unsigned deadbeef = 0xDEADBEEF;
  asm volatile ("" : "+d" (deadbeef));

  unsigned out;
  foo(out, 2, X::V);
...

0000000000400500 <main>:
  400500:    48 83 ec 18              sub    rsp,0x18
  400504:    ba ef be ad de           mov    edx,0xdeadbeef  <- EDX is loaded
with some garbage
  400509:    48 8d 7c 24 0c           lea    rdi,[rsp+0xc]
  40050e:    b2 02                    mov    dl,0x2          <- Parameter is
put in DL, without clearing RDX first
  400510:    be 02 00 00 00           mov    esi,0x2
  400515:    e8 0c 01 00 00           call   400626 <foo(unsigned int&,
unsigned int, X)>


0000000000400626 <foo(unsigned int&, unsigned int, X)>:
  400626:    89 17                    mov    DWORD PTR [rdi],edx  <- EDX still
contains remnants of 0xDEADBEEF here!
  400628:    c3                       ret


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

end of thread, other threads:[~2014-12-19 19:49 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-23 23:07 [Bug c++/64037] New: Miscompilation with LTO and enum class : char parameter js at alien8 dot de
2014-11-23 23:08 ` [Bug c++/64037] " js at alien8 dot de
2014-11-23 23:08 ` js at alien8 dot de
2014-11-23 23:09 ` js at alien8 dot de
2014-11-23 23:15 ` [Bug target/64037] " pinskia at gcc dot gnu.org
2014-11-24 11:13 ` rguenth at gcc dot gnu.org
2014-11-24 17:48 ` hjl.tools at gmail dot com
2014-11-24 17:49 ` pinskia at gcc dot gnu.org
2014-11-24 19:25 ` [Bug rtl-optimization/64037] " hjl.tools at gmail dot com
2014-11-24 19:52 ` hjl.tools at gmail dot com
2014-11-24 21:33 ` hjl.tools at gmail dot com
2014-11-24 21:39 ` hjl.tools at gmail dot com
2014-11-24 21:40 ` hjl.tools at gmail dot com
2014-11-25 18:23 ` [Bug rtl-optimization/64037] [4.8/4.9/5 Regression] Miscompilation with -Os " hjl.tools at gmail dot com
2014-11-28 15:28 ` hjl at gcc dot gnu.org
2014-11-28 15:32 ` [Bug rtl-optimization/64037] [4.8/4.9 " hjl.tools at gmail dot com
2014-12-05 11:48 ` hjl at gcc dot gnu.org
2014-12-05 12:03 ` hjl at gcc dot gnu.org
2014-12-05 12:07 ` hjl.tools at gmail dot com
2014-12-09 14:35 ` uros at gcc dot gnu.org
2014-12-09 14:41 ` uros at gcc dot gnu.org
2014-12-09 14:44 ` uros at gcc dot gnu.org
2014-12-09 14:48 ` [Bug rtl-optimization/64037] [4.8/4.9/5 " ubizjak at gmail dot com
2014-12-10 13:22 ` rguenth at gcc dot gnu.org
2014-12-14 16:04 ` hjl at gcc dot gnu.org
2014-12-14 16:07 ` hjl at gcc dot gnu.org
2014-12-15 16:01 ` hjl.tools at gmail dot com
2014-12-19 13:40 ` jakub at gcc dot gnu.org
2014-12-19 19:20 ` hjl at gcc dot gnu.org
2014-12-19 19:49 ` hjl.tools at gmail dot com

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