Bug 8726

Summary: [3.2.1/3.3] gcc -O2 miscompiles Samba 2.2.7 on 32-bit sparc
Product: gcc Reporter: eggert
Component: rtl-optimizationAssignee: Eric Botcazou <ebotcazou>
Status: RESOLVED FIXED    
Severity: normal CC: ehrhardt, gcc-bugs
Priority: P2 Keywords: wrong-code
Version: 3.2.1   
Target Milestone: 3.1.x/3.2.x   
Host: sparc-sun-solaris2.8 Target: sparc-sun-solaris2.8
Build: sparc-sun-solaris2.8 Known to work:
Known to fail: Last reconfirmed:

Description eggert 2002-11-26 17:06:00 UTC
	Samba 2.2.7 (32-bit sparc) mishandles 64-bit file locks because
	GCC 3.2.1 miscompiles the file-locking code.  I have narrowed
	down the problem to a simple test case enclosed below.

Release:
3.2.1

Environment:
System: SunOS sic.twinsun.com 5.8 Generic_108528-17 sun4u sparc SUNW,UltraSPARC-IIi-Engine
Architecture: sun4

	
host: sparc-sun-solaris2.8
build: sparc-sun-solaris2.8
target: sparc-sun-solaris2.8
configured with: ../gcc-3.2.1/configure --disable-nls --enable-languages=c,c++ --prefix=/opt/reb/lib/gcc/gcc-3.2.1 --with-system-zlib

How-To-Repeat:
	Compile and run the following program with "gcc -O2 t.i; ./a.out".
	a.out will exit with nonzero status; it should exit with zero status.

# 1 "t.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "t.c"
int fcntl_lock(int fd, int op, long long offset, long long count, int type);

int vfswrap_lock(char *fsp, int fd, int op, long long offset, long long count, int type)
{
  int result;
  result = fcntl_lock(fd, op, offset, count,type);
  return result;
}

int fcntl_lock(int fd, int op, long long offset, long long count, int type)
{
  return type;
}

int
main (void)
{
  return vfswrap_lock (0, 1, 2, 3, 4, 5) != 5;
}


The problem can be found in the assembly code for vfswrap_lock.
The "st" followed by the "ldd" puts garbage into the "type" argument.

vfswrap_lock:
	!#PROLOGUE# 0
	add	%sp, -48, %sp
	!#PROLOGUE# 1
	mov	%o1, %o0
	mov	%o2, %o1
	mov	%o3, %o2
	st	%o5, [%sp+136]
	mov	%o4, %o3
	ldd	[%sp+136], %o4
	sethi	%hi(fcntl_lock), %g1
	jmpl	%g1 + %lo(fcntl_lock), %g0
	 sub	%sp, -48, %sp
Comment 1 eggert 2002-11-26 17:06:00 UTC
Fix:
	A workaround is to not compile with -O2.
Comment 2 Christian Ehrhardt 2002-11-27 14:05:17 UTC
From: "Christian Ehrhardt" <ehrhardt@mathematik.uni-ulm.de>
To: Paul Eggert <eggert@twinsun.com>
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: optimization/8726: GCC 3.2.1 miscompiles Samba 2.2.7 on 32-bit sparc
Date: Wed, 27 Nov 2002 14:05:17 +0100

 Hi,
 
 I can confirm this as a regression from 3.2. This is a somewhat
 modified example with types clean up a bit. Compile with -O2.
 The problem seems to be in the tail call optimizations.
 
 int fcntl_lock(int a, int b, int c, int d, double e, int f)
 {
   return f;
 }
 int vfswrap (int dummy, int a, int b, int c, int d, double e, int f)
 {
 	 return fcntl_lock(a, b, c, d, e, f);
 }
 int main (void)
 {
   return vfswrap (0, 1, 2, 3, 4, 5, 6);
 }
 
 The number of arguments and the placement of the double argument is
 important. From my tests it seems that the problem always arises if
 a 64bit argument is passed to vfswrap partly in register o5 and partly
 on the stack. Wenn passing all the arguments except the first one to 
 fcntl_lock the optimizer doesn't move the following stack argmuments
 when reusing the existing stack frame.
 
     regards  Christian
 
Comment 3 Joe Buck 2003-02-14 23:38:23 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed as a regression, present in 3.2.2
Comment 4 Eric Botcazou 2003-02-15 08:18:03 UTC
Responsible-Changed-From-To: unassigned->ebotcazou
Responsible-Changed-Why: I'm handling this.
Comment 5 Eric Botcazou 2003-03-07 07:52:43 UTC
From: ebotcazou@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: optimization/8726
Date: 7 Mar 2003 07:52:43 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_3-branch
 Changes by:	ebotcazou@gcc.gnu.org	2003-03-07 07:52:43
 
 Modified files:
 	gcc            : ChangeLog function.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/gcc.c-torture/execute: 20030307-1.c 
 
 Log message:
 	PR optimization/8726
 	Backport patch from mainline:
 	
 	2003-01-08  Dale Johannesen  <dalej@apple.com>
 	
 	* function.c (assign_parms):  Don't set pretend_args_size if
 	REG_PARM_STACK_SPACE.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.16114.2.270&r2=1.16114.2.271
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/function.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.389.2.1&r2=1.389.2.2
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.2261.2.76&r2=1.2261.2.77
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20030307-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.2.1
 

Comment 6 Eric Botcazou 2003-03-07 07:58:39 UTC
From: ebotcazou@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: optimization/8726
Date: 7 Mar 2003 07:58:39 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_2-branch
 Changes by:	ebotcazou@gcc.gnu.org	2003-03-07 07:58:39
 
 Modified files:
 	gcc            : ChangeLog function.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/gcc.c-torture/execute: 20030307-1.c 
 
 Log message:
 	PR optimization/8726
 	Backport patch from mainline:
 	
 	2003-01-08  Dale Johannesen  <dalej@apple.com>
 	
 	* function.c (assign_parms):  Don't set pretend_args_size if
 	REG_PARM_STACK_SPACE.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.13152.2.657.2.248&r2=1.13152.2.657.2.249
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/function.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.347.2.10.2.2&r2=1.347.2.10.2.3
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.1672.2.166.2.103&r2=1.1672.2.166.2.104
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/20030307-1.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=NONE&r2=1.1.4.1
 
Comment 7 Eric Botcazou 2003-03-07 08:10:50 UTC
State-Changed-From-To: analyzed->closed
State-Changed-Why: Fixed. See http://gcc.gnu.org/ml/gcc-patches/2003-03/msg00567.html
Comment 8 Christian Ehrhardt 2004-01-27 12:05:15 UTC
This is still (again?) broken at least on the tree-ssa branch as
of 26-Jan-2004. See testcase 20030307-1.c.
Comment 9 Eric Botcazou 2004-01-27 12:15:20 UTC
No, the problem is fixed.  The failure of the testcase is unrelated and should
disappear with the next merge from mainline.
Comment 10 Christian Ehrhardt 2004-01-27 13:04:33 UTC
Subject: Re:  [3.2.1/3.3] gcc -O2 miscompiles Samba 2.2.7 on 32-bit sparc

On Tue, Jan 27, 2004 at 12:15:24PM -0000, ebotcazou at gcc dot gnu dot org wrote:
> No, the problem is fixed.  The failure of the testcase is unrelated and should
> disappear with the next merge from mainline.

Sorry and thanks for the Info. Do you happen to know if there are
other known failures on sparc/tree-ssa? I get 44 testsuite failure
mostly in gcc/testsuite/gcc.dg/compat.

   regards   Christian

Comment 11 Eric Botcazou 2004-01-27 14:23:36 UTC
I don't know where they come from.  I don't plan to work on tree-ssa until after
it is merged in mainline.