User account creation filtered due to spam.

Bug 43904 - Wrong code with -foptimize-sibling-calls and memcpy on x86_64
Summary: Wrong code with -foptimize-sibling-calls and memcpy on x86_64
Status: RESOLVED DUPLICATE of bug 43572
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-04-26 23:45 UTC by Tavian Barnes
Modified: 2010-04-27 00:23 UTC (History)
5 users (show)

See Also:
Host: x86_64-unknown-linux-gnu
Target: x86_64-unknown-linux-gnu
Build: x86_64-unknown-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Full testcase (284 bytes, text/plain)
2010-04-26 23:47 UTC, Tavian Barnes
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tavian Barnes 2010-04-26 23:45:16 UTC
On x86_64, gcc 4.5.0, this code generates bad assembly:

--- C code ---
typedef unsigned long size_t;
void *memcpy(void *dest, const void *src, size_t n);

void
buggy_init(void *ptr, size_t size)
{
  const char *str = "Hello world!";
  memcpy(ptr, &str, size);
}
--------------

Compiled with gcc -O -foptimize-sibling-calls, the generated assembly looks like this:

--------------
buggy_init:
        movq    %rsi, %rdx
        movq    $.LC0, -16(%rsp)
        leaq    -16(%rsp), %rsi
        jmp     memcpy
--------------

which passes rsp-16 as memcpy's second argument.  memcpy overwrites this part of the stack, and copies the wrong value, which causes a crash later.
Comment 1 Andrew Pinski 2010-04-26 23:47:12 UTC

*** This bug has been marked as a duplicate of 43572 ***
Comment 2 Tavian Barnes 2010-04-26 23:47:53 UTC
Created attachment 20497 [details]
Full testcase

Proper output:

Stored: 0x40071c
Got:    0x40071c
Hello world!

Output with -O -foptimize-sibling-calls:

Stored: 0x40070c
Got:    0x1
[1]    15940 segmentation fault (core dumped)  ./a.out
Comment 3 hjl@gcc.gnu.org 2010-04-27 00:25:46 UTC
Subject: Bug 43904

Author: hjl
Date: Tue Apr 27 00:25:18 2010
New Revision: 158757

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=158757
Log:
Add a run-time testcase for PR tree-optimization/43904.

2010-04-26  H.J. Lu  <hongjiu.lu@intel.com>

	PR tree-optimization/43904
	* gcc.dg/tree-ssa/tailcall-6.c: New.

Added:
    trunk/gcc/testsuite/gcc.dg/tree-ssa/tailcall-6.c
Modified:
    trunk/gcc/testsuite/ChangeLog

Comment 4 hjl@gcc.gnu.org 2010-04-27 00:30:24 UTC
Subject: Bug 43904

Author: hjl
Date: Tue Apr 27 00:30:00 2010
New Revision: 158758

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=158758
Log:
Backport testcase from mainline.

2010-04-26  H.J. Lu  <hongjiu.lu@intel.com>

	Backport from mainline
	2010-04-26  H.J. Lu  <hongjiu.lu@intel.com>

	PR tree-optimization/43904
	* gcc.dg/tree-ssa/tailcall-6.c: New.

Added:
    branches/gcc-4_5-branch/gcc/testsuite/gcc.dg/tree-ssa/tailcall-6.c
      - copied unchanged from r158757, trunk/gcc/testsuite/gcc.dg/tree-ssa/tailcall-6.c
Modified:
    branches/gcc-4_5-branch/gcc/testsuite/ChangeLog