Bug 48142

Summary: [4.6/4.7 Regression] miscompilation with -Os -mpreferred-stack-boundary=5 -fstack-check
Product: gcc Reporter: Zdenek Sojka <zsojka>
Component: targetAssignee: Eric Botcazou <ebotcazou>
Status: RESOLVED FIXED    
Severity: normal CC: ebotcazou, hjl.tools, jakub, ubizjak
Priority: P3 Keywords: wrong-code
Version: 4.7.0   
Target Milestone: 4.6.1   
URL: http://gcc.gnu.org/ml/gcc-patches/2011-03/msg02125.html
Host: Target: x86_64-pc-linux-gnu
Build: Known to work:
Known to fail: 4.6.0, 4.7.0 Last reconfirmed: 2011-03-16 10:55:01
Attachments: reduced testcase

Description Zdenek Sojka 2011-03-16 01:27:08 UTC
Created attachment 23674 [details]
reduced testcase

Output:
$ g++ -Os -mpreferred-stack-boundary=5 -fstack-check=specific -fno-omit-frame-pointer testcase.C
$ valgrind -q ./a.out 
==16640== Invalid read of size 8
==16640==    at 0x4007D0: main (testcase.C:6)
==16640==  Address 0xfffffffffffffff8 is not stack'd, malloc'd or (recently) free'd

(gdb) i r rsp
rsp            0xfffffffffffffff8       0xfffffffffffffff8
(gdb) disassemble
   0x00000000004007c6 <+82>:    pop    %rcx
   0x00000000004007c7 <+83>:    pop    %r10
   0x00000000004007c9 <+85>:    xor    %eax,%eax
   0x00000000004007cb <+87>:    pop    %rbp
   0x00000000004007cc <+88>:    lea    -0x8(%r10),%rsp
=> 0x00000000004007d0 <+92>:    retq   
End of assembler dump.

Tested revisions:
r171001 - fail
4.6 r170955 - fail
4.5 r170955 - OK
Comment 1 Richard Biener 2011-03-16 10:55:01 UTC
Confirmed.
Comment 2 Jakub Jelinek 2011-03-16 14:56:01 UTC
Caused by http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=161952
Comment 3 Eric Botcazou 2011-03-17 08:20:51 UTC
I'll fix once 4.6.0 is out.
Comment 4 Eric Botcazou 2011-03-31 11:11:03 UTC
Author: ebotcazou
Date: Thu Mar 31 11:10:59 2011
New Revision: 171777

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=171777
Log:
	PR target/48142
	* config/i386/i386.c (ix86_adjust_stack_and_probe): Differentiate
	frame-related from frame-unrelated adjustments to the stack pointer.

Added:
    trunk/gcc/testsuite/g++.dg/other/i386-9.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/i386/i386.c
    trunk/gcc/testsuite/ChangeLog
Comment 5 Eric Botcazou 2011-03-31 11:14:44 UTC
Author: ebotcazou
Date: Thu Mar 31 11:14:41 2011
New Revision: 171778

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=171778
Log:
	PR target/48142
	* config/i386/i386.c (ix86_adjust_stack_and_probe): Differentiate
	frame-related from frame-unrelated adjustments to the stack pointer.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/other/i386-9.C
      - copied unchanged from r171777, trunk/gcc/testsuite/g++.dg/other/i386-9.C
Modified:
    branches/gcc-4_6-branch/gcc/ChangeLog
    branches/gcc-4_6-branch/gcc/config/i386/i386.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Comment 6 Eric Botcazou 2011-03-31 11:16:15 UTC
.
Comment 7 UroŇ° Bizjak 2011-04-22 18:53:24 UTC
This test fails again after the fix for PR target/48723 [1,2].

PR 48723 fails due to clear omission of accounting, but this PR is more involved and IMO, the fix there is wrong.

Reopened.

[1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48723
[2] http://gcc.gnu.org/ml/gcc-patches/2011-04/msg01883.html
Comment 8 Eric Botcazou 2011-04-22 19:49:10 UTC
> This test fails again after the fix for PR target/48723 [1,2].

Sure, stack checking is totally broken:

Running /home/eric/svn/gcc/gcc/testsuite/gnat.dg/dg.exp ...
FAIL: gnat.dg/stack_check1.adb (test for excess errors)
WARNING: gnat.dg/stack_check1.adb compilation failed to produce executable
FAIL: gnat.dg/stack_check2.adb (test for excess errors)
WARNING: gnat.dg/stack_check2.adb compilation failed to produce executable

FAIL:   c52103x
FAIL:   c52104x
FAIL:   c52104y
FAIL:   cb1010a
FAIL:   cb1010c
FAIL:   cb1010d

FAIL: g++.dg/other/i386-9.C (internal compiler error)
FAIL: g++.dg/other/i386-9.C (test for excess errors)

I don't understand how the patch can have been installed.  I'd suggest reverting it ASAP (and assigning PR target/48723 to me).
Comment 9 UroŇ° Bizjak 2011-04-23 07:38:35 UTC
(In reply to comment #8)
> > This test fails again after the fix for PR target/48723 [1,2].
> 
> Sure, stack checking is totally broken:
> 
> Running /home/eric/svn/gcc/gcc/testsuite/gnat.dg/dg.exp ...
> FAIL: gnat.dg/stack_check1.adb (test for excess errors)
> WARNING: gnat.dg/stack_check1.adb compilation failed to produce executable
> FAIL: gnat.dg/stack_check2.adb (test for excess errors)
> WARNING: gnat.dg/stack_check2.adb compilation failed to produce executable
> 
> FAIL:   c52103x
> FAIL:   c52104x
> FAIL:   c52104y
> FAIL:   cb1010a
> FAIL:   cb1010c
> FAIL:   cb1010d
> 
> FAIL: g++.dg/other/i386-9.C (internal compiler error)
> FAIL: g++.dg/other/i386-9.C (test for excess errors)
> 
> I don't understand how the patch can have been installed.  I'd suggest
> reverting it ASAP (and assigning PR target/48723 to me).

OK, I have reverted it (I don't have ADA installed, and didn't notice g++ failure...).
Comment 10 Eric Botcazou 2011-04-27 13:09:44 UTC
> OK, I have reverted it (I don't have ADA installed, and didn't notice g++
> failure...).

Thanks.