Bug 81945 - Producing bad code with -O0 but not with -O2
Producing bad code with -O0 but not with -O2
Status: CLOSED NOTABUG
Product: Red Hat Linux
Classification: Retired
Component: gcc3 (Show other bugs)
7.3
i686 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2003-01-15 12:01 EST by Federico Mena-Quintero
Modified: 2008-05-01 11:38 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2003-01-15 12:17:14 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:


Attachments (Terms of Use)
Test case program. (315 bytes, text/plain)
2003-01-15 12:02 EST, Federico Mena-Quintero
no flags Details
Better test case program (356 bytes, text/plain)
2003-01-15 12:06 EST, Federico Mena-Quintero
no flags Details

  None (edit)
Description Federico Mena-Quintero 2003-01-15 12:01:58 EST
From Bugzilla Helper:
User-Agent: Mozilla/5.0 Galeon/1.2.6 (X11; Linux i686; U;) Gecko/20021119

Description of problem:
I am using gcc 3.0.4.  The attached (tiny) program gives a segmentation fault
when you build it with -O0, but it runs fine if you compile it with -O2.  This
is on i686 Linux.

How reproducible:
Always

Steps to Reproduce:
1. Build the attached program with -O0.
2. Run the program; watch it crash.
3. Build the program with -O2.
4. Run it again; it runs fine and displays "n = 1".
    

Additional info:

With -O0, I get this code:
inc_ref:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $4, %esp
        movl    8(%ebp), %eax
        movl    8(%ebp), %edx
#APP
        movl $1, %eax
lock
xadd %eax, (%eax)
incl %eax
#NO_APP
        movl    %eax, -4(%ebp)
        movl    %ebp, %esp
        popl    %ebp
        ret
which of course causes a SIGSEGV, as %eax is 1 and (%eax) is evil.

But with -O2, I get this:
inc_ref:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $4, %esp
        movl    8(%ebp), %edx
#APP
        movl $1, %eax
lock
xadd %eax, (%edx)
incl %eax
#NO_APP
        movl    %ebp, %esp
        popl    %ebp
        ret
Comment 1 Federico Mena-Quintero 2003-01-15 12:02:49 EST
Created attachment 89381 [details]
Test case program.
Comment 2 Jakub Jelinek 2003-01-15 12:06:22 EST
Can you reproduce it with gcc 3.2.1? gcc 3.0.x is totally unsupported.
Comment 3 Federico Mena-Quintero 2003-01-15 12:06:40 EST
Created attachment 89382 [details]
Better test case program

This version also prints the return value of inc_ref.
Comment 4 Federico Mena-Quintero 2003-01-15 12:07:23 EST
Let me get that version of gcc and I'll tell you.
Comment 5 Jakub Jelinek 2003-01-15 12:17:14 EST
Actually, looking at the testcase, the testcase is buggy.
1) you miss an earlyclobber, so gcc is allowed to use (%eax) for "m" (*ref)
2) you should use "1" not "m" in the second *ref constraint
3) you should actually return some value from the function, setting %eax and
   relying nobody clobbers it between the __asm and end of function is bogus
Comment 6 Federico Mena-Quintero 2003-01-15 14:33:02 EST
That code came from OpenOffice.org (it's from their reference-counting
functions), so I assumed it was correct.  Sorry to have bothered you with this.
 I will inform the OO.o team about this bug.  Thanks, Jakub!

Note You need to log in before you can comment on or make changes to this bug.