public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code
@ 2022-10-13 13:21 dennis.borde at ohb dot de
  2022-10-13 13:25 ` [Bug rtl-optimization/107248] " dennis.borde at ohb dot de
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: dennis.borde at ohb dot de @ 2022-10-13 13:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

            Bug ID: 107248
           Summary: Sparc V8 Invalid Stack Pointer Code
           Product: gcc
           Version: 7.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dennis.borde at ohb dot de
  Target Milestone: ---

Created attachment 53700
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53700&action=edit
source code to trigger the bug

Environment: GCC V7.1.0, Sparc V8, RTEMS V4.8.0

When compiling with optimization level -O2 (including -fschedule-insns2) the
compiler generates code like this:

(1) add %sp, 0x50, %g1
(2) add %sp, 0x50, %sp
(3) add %g1, %o0, %o0
(4) ld [ %o0 + -8 ], %o0

In line (2) the stack pointer is moved by 80 bytes forward, which means memory
is "freed".
In line (4) it accesses the "freed" stack memory.

When an interrupt occurs in between line (2) and (4) it will overwrite the
stack data and "corrupt" it for the reading in line (4).

E.g.: As part of the RTEMS _ISR_Handler() the interrupt stack frame is stored
(see label symbol save_isf). For more information see RTEMS source code.
However, this is just one example to show the order of instructions above is
not safe. It is not important for the bug itself.

Work-around: Compile with -fno-schedule-insns2

With the work-around the generated code looks like this:
(1) add %sp, 0x50, %g1
(2) add %g1, %o0, %o0
(3) ld [ %o0 + -8 ], %o0
(4) add %sp, 0x50, %sp 

Here the stack memory is "freed" (4) after the access (3).

It seems to be related to
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38644

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug rtl-optimization/107248] Sparc V8 Invalid Stack Pointer Code
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
@ 2022-10-13 13:25 ` dennis.borde at ohb dot de
  2022-10-13 20:28 ` [Bug target/107248] " pinskia at gcc dot gnu.org
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: dennis.borde at ohb dot de @ 2022-10-13 13:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

--- Comment #1 from Dennis Borde <dennis.borde at ohb dot de> ---
Created attachment 53701
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53701&action=edit
gcc -v output

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] Sparc V8 Invalid Stack Pointer Code
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
  2022-10-13 13:25 ` [Bug rtl-optimization/107248] " dennis.borde at ohb dot de
@ 2022-10-13 20:28 ` pinskia at gcc dot gnu.org
  2022-10-13 22:09 ` [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2 ebotcazou at gcc dot gnu.org
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-13 20:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-10-13
           Keywords|                            |ABI, wrong-code
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Looks like for sparc_leaf_function_p, there is a missing a emit_insn
(gen_frame_blockage ()) .

Which was added in the non leaf case with r0-114040-ge98b1defdd2c6b .

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
  2022-10-13 13:25 ` [Bug rtl-optimization/107248] " dennis.borde at ohb dot de
  2022-10-13 20:28 ` [Bug target/107248] " pinskia at gcc dot gnu.org
@ 2022-10-13 22:09 ` ebotcazou at gcc dot gnu.org
  2022-10-13 22:10 ` ebotcazou at gcc dot gnu.org
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2022-10-13 22:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

Eric Botcazou <ebotcazou at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ebotcazou at gcc dot gnu.org
            Summary|Sparc V8 Invalid Stack      |wrong scheduling of stack
                   |Pointer Code                |adjustment in leaf function
                   |                            |at -O2
           Keywords|ABI                         |
             Target|sparc-rtems                 |sparc-*-*

--- Comment #3 from Eric Botcazou <ebotcazou at gcc dot gnu.org> ---
Yes, it's the same bug as on ARM and MIPS, but one decade later...

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
                   ` (2 preceding siblings ...)
  2022-10-13 22:09 ` [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2 ebotcazou at gcc dot gnu.org
@ 2022-10-13 22:10 ` ebotcazou at gcc dot gnu.org
  2022-10-14  9:56 ` cvs-commit at gcc dot gnu.org
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2022-10-13 22:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

Eric Botcazou <ebotcazou at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |ebotcazou at gcc dot gnu.org

--- Comment #4 from Eric Botcazou <ebotcazou at gcc dot gnu.org> ---
Fixing.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
                   ` (3 preceding siblings ...)
  2022-10-13 22:10 ` ebotcazou at gcc dot gnu.org
@ 2022-10-14  9:56 ` cvs-commit at gcc dot gnu.org
  2022-10-14  9:57 ` cvs-commit at gcc dot gnu.org
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-10-14  9:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Eric Botcazou <ebotcazou@gcc.gnu.org>:

https://gcc.gnu.org/g:e39b170695a161feba7401b7d21d824db9ee1f8f

commit r13-3296-ge39b170695a161feba7401b7d21d824db9ee1f8f
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Fri Oct 14 11:52:04 2022 +0200

    Fix PR target/107248

    This is the infamous PR rtl-optimization/38644 rearing its ugly head for
    leaf functions on SPARC more than a decade later...  Richard E.'s generic
    solution has never been implemented so let's do as other RISC back-ends
did.

    gcc/
            PR target/107248
            * config/sparc/sparc.cc (sparc_expand_prologue): Emit a frame
            blockage for leaf functions.
            (sparc_flat_expand_prologue): Emit frame instead of full blockage.
            (sparc_expand_epilogue): Emit a frame blockage for leaf functions.
            (sparc_flat_expand_epilogue): Emit frame instead of full blockage.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
                   ` (4 preceding siblings ...)
  2022-10-14  9:56 ` cvs-commit at gcc dot gnu.org
@ 2022-10-14  9:57 ` cvs-commit at gcc dot gnu.org
  2022-10-14  9:58 ` cvs-commit at gcc dot gnu.org
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-10-14  9:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by Eric Botcazou
<ebotcazou@gcc.gnu.org>:

https://gcc.gnu.org/g:a5a6598d5b1d29741993371310c0bb8ca57e190c

commit r12-8831-ga5a6598d5b1d29741993371310c0bb8ca57e190c
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Fri Oct 14 11:52:04 2022 +0200

    Fix PR target/107248

    This is the infamous PR rtl-optimization/38644 rearing its ugly head for
    leaf functions on SPARC more than a decade later...  Richard E.'s generic
    solution has never been implemented so let's do as other RISC back-ends
did.

    gcc/
            PR target/107248
            * config/sparc/sparc.cc (sparc_expand_prologue): Emit a frame
            blockage for leaf functions.
            (sparc_flat_expand_prologue): Emit frame instead of full blockage.
            (sparc_expand_epilogue): Emit a frame blockage for leaf functions.
            (sparc_flat_expand_epilogue): Emit frame instead of full blockage.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
                   ` (5 preceding siblings ...)
  2022-10-14  9:57 ` cvs-commit at gcc dot gnu.org
@ 2022-10-14  9:58 ` cvs-commit at gcc dot gnu.org
  2022-10-14 10:00 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-10-14  9:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Eric Botcazou
<ebotcazou@gcc.gnu.org>:

https://gcc.gnu.org/g:3f4b65df625edae3e8829718af721ad2330b3f22

commit r11-10311-g3f4b65df625edae3e8829718af721ad2330b3f22
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Fri Oct 14 11:52:04 2022 +0200

    Fix PR target/107248

    This is the infamous PR rtl-optimization/38644 rearing its ugly head for
    leaf functions on SPARC more than a decade later...  Richard E.'s generic
    solution has never been implemented so let's do as other RISC back-ends
did.

    gcc/
            PR target/107248
            * config/sparc/sparc.c (sparc_expand_prologue): Emit a frame
            blockage for leaf functions.
            (sparc_flat_expand_prologue): Emit frame instead of full blockage.
            (sparc_expand_epilogue): Emit a frame blockage for leaf functions.
            (sparc_flat_expand_epilogue): Emit frame instead of full blockage.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
                   ` (6 preceding siblings ...)
  2022-10-14  9:58 ` cvs-commit at gcc dot gnu.org
@ 2022-10-14 10:00 ` cvs-commit at gcc dot gnu.org
  2022-10-14 10:03 ` ebotcazou at gcc dot gnu.org
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-10-14 10:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Eric Botcazou
<ebotcazou@gcc.gnu.org>:

https://gcc.gnu.org/g:d0ef37c35b7ff7324b4567652380f32079d46088

commit r10-11034-gd0ef37c35b7ff7324b4567652380f32079d46088
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Fri Oct 14 11:52:04 2022 +0200

    Fix PR target/107248

    This is the infamous PR rtl-optimization/38644 rearing its ugly head for
    leaf functions on SPARC more than a decade later...  Richard E.'s generic
    solution has never been implemented so let's do as other RISC back-ends
did.

    gcc/
            PR target/107248
            * config/sparc/sparc.c (sparc_expand_prologue): Emit a frame
            blockage for leaf functions.
            (sparc_flat_expand_prologue): Emit frame instead of full blockage.
            (sparc_expand_epilogue): Emit a frame blockage for leaf functions.
            (sparc_flat_expand_epilogue): Emit frame instead of full blockage.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
                   ` (7 preceding siblings ...)
  2022-10-14 10:00 ` cvs-commit at gcc dot gnu.org
@ 2022-10-14 10:03 ` ebotcazou at gcc dot gnu.org
  2022-10-17 11:05 ` dennis.borde at ohb dot de
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2022-10-14 10:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

Eric Botcazou <ebotcazou at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
   Target Milestone|---                         |10.5
         Resolution|---                         |FIXED

--- Comment #9 from Eric Botcazou <ebotcazou at gcc dot gnu.org> ---
Fixed on all active branches, but the fix should be backportable onto older
release branches without any change.  Thanks for reporting the problem.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
                   ` (8 preceding siblings ...)
  2022-10-14 10:03 ` ebotcazou at gcc dot gnu.org
@ 2022-10-17 11:05 ` dennis.borde at ohb dot de
  2022-10-25  8:00 ` ebotcazou at gcc dot gnu.org
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: dennis.borde at ohb dot de @ 2022-10-17 11:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

--- Comment #10 from Dennis Borde <dennis.borde at ohb dot de> ---
Much more important: Thanks for fixing it :-)

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
                   ` (9 preceding siblings ...)
  2022-10-17 11:05 ` dennis.borde at ohb dot de
@ 2022-10-25  8:00 ` ebotcazou at gcc dot gnu.org
  2023-01-03  8:30 ` cederman at gaisler dot com
  2023-01-03  8:58 ` ebotcazou at gcc dot gnu.org
  12 siblings, 0 replies; 14+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2022-10-25  8:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

--- Comment #11 from Eric Botcazou <ebotcazou at gcc dot gnu.org> ---
> Much more important: Thanks for fixing it :-)

You're welcome.  It looks like -mtune=leon or -mtune-leon3 can mitigate it.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
                   ` (10 preceding siblings ...)
  2022-10-25  8:00 ` ebotcazou at gcc dot gnu.org
@ 2023-01-03  8:30 ` cederman at gaisler dot com
  2023-01-03  8:58 ` ebotcazou at gcc dot gnu.org
  12 siblings, 0 replies; 14+ messages in thread
From: cederman at gaisler dot com @ 2023-01-03  8:30 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

Daniel Cederman <cederman at gaisler dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |cederman at gaisler dot com

--- Comment #12 from Daniel Cederman <cederman at gaisler dot com> ---
Just to make it clear, since we have had customers asking about it, it is still
possible to trigger this issue with -mtune=leon or -mtune-leon3, though it
might make it less likely to happen.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2
  2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
                   ` (11 preceding siblings ...)
  2023-01-03  8:30 ` cederman at gaisler dot com
@ 2023-01-03  8:58 ` ebotcazou at gcc dot gnu.org
  12 siblings, 0 replies; 14+ messages in thread
From: ebotcazou at gcc dot gnu.org @ 2023-01-03  8:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107248

--- Comment #13 from Eric Botcazou <ebotcazou at gcc dot gnu.org> ---
> Just to make it clear, since we have had customers asking about it, it is
> still possible to trigger this issue with -mtune=leon or -mtune-leon3,
> though it might make it less likely to happen.

Very likely not, but proving it would be quite hard.

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-01-03  8:58 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-13 13:21 [Bug rtl-optimization/107248] New: Sparc V8 Invalid Stack Pointer Code dennis.borde at ohb dot de
2022-10-13 13:25 ` [Bug rtl-optimization/107248] " dennis.borde at ohb dot de
2022-10-13 20:28 ` [Bug target/107248] " pinskia at gcc dot gnu.org
2022-10-13 22:09 ` [Bug target/107248] wrong scheduling of stack adjustment in leaf function at -O2 ebotcazou at gcc dot gnu.org
2022-10-13 22:10 ` ebotcazou at gcc dot gnu.org
2022-10-14  9:56 ` cvs-commit at gcc dot gnu.org
2022-10-14  9:57 ` cvs-commit at gcc dot gnu.org
2022-10-14  9:58 ` cvs-commit at gcc dot gnu.org
2022-10-14 10:00 ` cvs-commit at gcc dot gnu.org
2022-10-14 10:03 ` ebotcazou at gcc dot gnu.org
2022-10-17 11:05 ` dennis.borde at ohb dot de
2022-10-25  8:00 ` ebotcazou at gcc dot gnu.org
2023-01-03  8:30 ` cederman at gaisler dot com
2023-01-03  8:58 ` ebotcazou at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).