Bug 1375522

Summary: Miscompile of simple function (with example)
Product: [Fedora] Fedora Reporter: alan
Component: cross-gccAssignee: David Howells <dhowells>
Status: CLOSED EOL QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 23CC: dan, dhowells
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-12-20 21:29:31 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Attachments:
Description Flags
Example code none

Description alan 2016-09-13 10:06:52 UTC
Created attachment 1200444 [details]
Example code

Description of problem:

In certain cases gcc wants to generate the equivalent of

move.b (%a0,-1),foo

but instead of generating moveq #-1.%d0, moveb(%a0,%d0.l) or similar it generates the bogus sequence

moveq #0,%d0
not.w %d0

(the fast way of generating a word value -1 not a long)

and as a result instead randomly pees on memory 64K offset


Version-Release number of selected component (if applicable):
5.3.1 2.fc23 x86_64

How reproducible:
100%


Steps to Reproduce:
1. Compile the attached sample c code with gcc  -mshort -m68000 -O2 -S 1.c
2. Examine 1.s
3 #facepalm

Actual results:

Complete nonsense (bogus noveq/not, bogus ext.l and leal.l of undefined %d0 value higher up)

Expected results:

A memcpy function

Additional info:

Building with -Os produces very different but valid code, building without -mshort produces valid code so it looks like -O2 -mshort triggers a bogus optimisation

Comment 1 David Howells 2016-09-15 15:12:32 UTC
(In reply to alan from comment #0)
> Complete nonsense (bogus noveq/not, bogus ext.l and leal.l of undefined %d0
> value higher up)

You didn't paste the asm you got, but in the asm I get from 6.1.1, %d0 is validly loaded with the size parameter:

        link.w %fp,#0
        move.l %a3,-(%sp)
        move.l %a2,-(%sp)
        move.l 8(%fp),%a0
        move.l 16(%fp),%d0

Looking at the frame layout:

    fp+ 0 -> prev frame pointer
    fp+ 4 -> ret addr
    fp+ 8 -> param d
    fp+12 -> param s
    fp+16 -> param sz

on the other hand, passing it through ext.l would seem to be wrong, given that it's a long int, not an int.

With regard to the main point of this BZ, rather than generating:

    moveb(%a0,%d0.l)

it should perhaps be generating:

    moveb(%a0,%d0.w)

and then having 0xffff in %d0 isn't a problem as it's sign-extended.

Comment 2 alan 2016-09-15 15:24:41 UTC
5.x generates

	link.w %fp,#0
	move.l %a3,-(%sp)
	move.l %a2,-(%sp)
	move.l 8(%fp),%a0
	move.l 12(%fp),%a1
	move.l 16(%fp),%d0
	jeq .L6
	move.l %a0,%a2
	ext.l %d0
	lea (%a0,%d0.l),%a3
.L3:
	addq.l #1,%a1
	moveq #0,%d0                                                           <-----
	not.w %d0                                                                   <----- Should be moveq #-1,%d0 ?
	move.b (%a1,%d0.l),(%a2)+                                   <----- Or just addq to a1 *after* the load !
	cmp.l %a2,%a3
	jne .L3
.L6:
	move.l %a0,%d0
	move.l (%sp)+,%a2
	move.l (%sp)+,%a3
	unlk %fp
	rts

Comment 3 David Howells 2016-11-18 08:38:37 UTC
To quote Jeff Law on https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77600#c2 :

This is a similar issue as I outlined in the other m68k -mshort BZ.  This time we need to look at how INTPTR_TYPE, which is defined in terms of LONG_TYPE_SIZE.   When LONG_TYPE_SIZE == 32, INTPTR_TYPE will be an "int" -mshort changes the size of an int from 32 to 16 bits.

Thus conversions to/from sizetype are going to do things you don't expect unless you know sizetype is 16 bits.

Again, if you configure for a bare metal target such as m68k-elf or avoid using -mshort on your linux target, you'll get the desired and expected result.

Comment 4 Fedora End Of Life 2016-11-25 09:34:47 UTC
This message is a reminder that Fedora 23 is nearing its end of life.
Approximately 4 (four) weeks from now Fedora will stop maintaining
and issuing updates for Fedora 23. It is Fedora's policy to close all
bug reports from releases that are no longer maintained. At that time
this bug will be closed as EOL if it remains open with a Fedora  'version'
of '23'.

Package Maintainer: If you wish for this bug to remain open because you
plan to fix it in a currently maintained version, simply change the 'version' 
to a later Fedora version.

Thank you for reporting this issue and we are sorry that we were not 
able to fix it before Fedora 23 is end of life. If you would still like 
to see this bug fixed and are able to reproduce it against a later version 
of Fedora, you are encouraged  change the 'version' to a later Fedora 
version prior this bug is closed as described in the policy above.

Although we aim to fix as many bugs as possible during every release's 
lifetime, sometimes those efforts are overtaken by events. Often a 
more recent Fedora release includes newer upstream software that fixes 
bugs or makes them obsolete.

Comment 5 Fedora End Of Life 2016-12-20 21:29:31 UTC
Fedora 23 changed to end-of-life (EOL) status on 2016-12-20. Fedora 23 is
no longer maintained, which means that it will not receive any further
security or bug fix updates. As a result we are closing this bug.

If you can reproduce this bug against a currently maintained version of
Fedora please feel free to reopen this bug against that version. If you
are unable to reopen this bug, please file a new report against the
current release. If you experience problems, please add a comment to this
bug.

Thank you for reporting this bug and we are sorry it could not be fixed.

Comment 6 Fedora End Of Life 2016-12-21 03:23:03 UTC
Fedora 23 changed to end-of-life (EOL) status on 2016-12-20. Fedora 23 is
no longer maintained, which means that it will not receive any further
security or bug fix updates. As a result we are closing this bug.

If you can reproduce this bug against a currently maintained version of
Fedora please feel free to reopen this bug against that version. If you
are unable to reopen this bug, please file a new report against the
current release. If you experience problems, please add a comment to this
bug.

Thank you for reporting this bug and we are sorry it could not be fixed.