public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/114848] New: longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return
@ 2024-04-25  7:45 pinskia at gcc dot gnu.org
  2024-04-25  8:00 ` [Bug target/114848] " xry111 at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-25  7:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114848
           Summary: longarch: epilogue in _Unwind_RaiseException corrupts
                    return value due to __builtin_eh_return
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---
            Target: longarch64-linux-gnu

I reduced an miscompile for aarch64 inside _Unwind_RaiseException and I noticed
the same issue can be reproduced on powerpc64-linux-gnu and powerpc-linux-gnu.

Reduced testcase:
```
__attribute__((noipa,noinline))
int f(int *a, long offset, void *handler)
{
  if (*a == 5)
    return 5;
  __builtin_eh_return (offset, handler);
}

int main()
{
  int t = 5;
  if (f(&t, 0, 0) != 5)
    __builtin_abort();
}
```
This produces a load (of r4) in the epilogue part for the `return 5` path
(which is reduced from the end of stack path inside _Unwind_RaiseException).


```

        bne     $r13,$r12,.L2
        addi.w  $r4,$r0,5             # 0x5
        or      $r8,$r0,$r0
.L4:
        ld.d    $r1,$r3,40
        .cfi_remember_state
        .cfi_restore 1
        ld.d    $r5,$r3,24
        .cfi_restore 5
        ld.d    $r4,$r3,32
        .cfi_restore 4
        ld.d    $r6,$r3,16
        .cfi_restore 6
        ld.d    $r7,$r3,8
        .cfi_restore 7
        addi.d  $r3,$r3,48
        .cfi_def_cfa_offset 0
        add.d   $r3,$r3,$r8
        jr      $r1
```

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

* [Bug target/114848] longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return
  2024-04-25  7:45 [Bug target/114848] New: longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return pinskia at gcc dot gnu.org
@ 2024-04-25  8:00 ` xry111 at gcc dot gnu.org
  2024-04-25  8:04 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-04-25  8:00 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xry111 at gcc dot gnu.org

--- Comment #1 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
Hmm, AFAIK this should be already fixed with r14-6440?

I cannot reproduce it with r14-9823 but maybe it has regressed again in the
recent weeks.

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

* [Bug target/114848] longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return
  2024-04-25  7:45 [Bug target/114848] New: longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return pinskia at gcc dot gnu.org
  2024-04-25  8:00 ` [Bug target/114848] " xry111 at gcc dot gnu.org
@ 2024-04-25  8:04 ` pinskia at gcc dot gnu.org
  2024-04-25  8:11 ` [Bug target/114848] loongarch: " xry111 at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-25  8:04 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|14.0                        |13.2.0

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Xi Ruoyao from comment #1)
> Hmm, AFAIK this should be already fixed with r14-6440?
> 
> I cannot reproduce it with r14-9823 but maybe it has regressed again in the
> recent weeks.

Oh I only tested gcc 13.2.0. If it is fixed you can close it.

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

* [Bug target/114848] loongarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return
  2024-04-25  7:45 [Bug target/114848] New: longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return pinskia at gcc dot gnu.org
  2024-04-25  8:00 ` [Bug target/114848] " xry111 at gcc dot gnu.org
  2024-04-25  8:04 ` pinskia at gcc dot gnu.org
@ 2024-04-25  8:11 ` xry111 at gcc dot gnu.org
  2024-04-25  8:21 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-04-25  8:11 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|longarch: epilogue in       |loongarch: epilogue in
                   |_Unwind_RaiseException      |_Unwind_RaiseException
                   |corrupts return value due   |corrupts return value due
                   |to __builtin_eh_return      |to __builtin_eh_return

--- Comment #3 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> (In reply to Xi Ruoyao from comment #1)
> > Hmm, AFAIK this should be already fixed with r14-6440?
> > 
> > I cannot reproduce it with r14-9823 but maybe it has regressed again in the
> > recent weeks.
> 
> Oh I only tested gcc 13.2.0. If it is fixed you can close it.

Hmm it looks like we need a backport to releases/gcc-13 (and 12?)

I thought the bug was introduced by my shrink-wrap change (r14-545) so I didn't
proposed a backport.  But it seems I was wrong and the bug exists even before
r14-545.

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

* [Bug target/114848] loongarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return
  2024-04-25  7:45 [Bug target/114848] New: longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2024-04-25  8:11 ` [Bug target/114848] loongarch: " xry111 at gcc dot gnu.org
@ 2024-04-25  8:21 ` pinskia at gcc dot gnu.org
  2024-04-27  9:05 ` chenglulu at loongson dot cn
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-25  8:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Also it looks like I messed up comment #0 and forgot to change powerpc to
longarch64 :). That is what I get for trying to split this all out.

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

* [Bug target/114848] loongarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return
  2024-04-25  7:45 [Bug target/114848] New: longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return pinskia at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2024-04-25  8:21 ` pinskia at gcc dot gnu.org
@ 2024-04-27  9:05 ` chenglulu at loongson dot cn
  2024-04-30  1:09 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: chenglulu at loongson dot cn @ 2024-04-27  9:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from chenglulu <chenglulu at loongson dot cn> ---
(In reply to Xi Ruoyao from comment #3)
> (In reply to Andrew Pinski from comment #2)
> > (In reply to Xi Ruoyao from comment #1)
> > > Hmm, AFAIK this should be already fixed with r14-6440?
> > > 
> > > I cannot reproduce it with r14-9823 but maybe it has regressed again in the
> > > recent weeks.
> > 
> > Oh I only tested gcc 13.2.0. If it is fixed you can close it.
> 
> Hmm it looks like we need a backport to releases/gcc-13 (and 12?)

I have backpointed r14-6440 to gcc-13 and gcc-12 and am testing

> 
> I thought the bug was introduced by my shrink-wrap change (r14-545) so I
> didn't proposed a backport.  But it seems I was wrong and the bug exists
> even before r14-545.

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

* [Bug target/114848] loongarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return
  2024-04-25  7:45 [Bug target/114848] New: longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return pinskia at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2024-04-27  9:05 ` chenglulu at loongson dot cn
@ 2024-04-30  1:09 ` cvs-commit at gcc dot gnu.org
  2024-04-30  1:11 ` cvs-commit at gcc dot gnu.org
  2024-04-30  1:20 ` xry111 at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-04-30  1:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by LuluCheng
<chenglulu@gcc.gnu.org>:

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

commit r12-10403-gbb78099d2624b52c781ed6e5d85e43d54c3cda1a
Author: Yang Yujie <yangyujie@loongson.cn>
Date:   Fri Dec 8 18:01:18 2023 +0800

    LoongArch: Fix eh_return epilogue for normal returns.

    On LoongArch, the regitsters $r4 - $r7 (EH_RETURN_DATA_REGNO) will be saved
    and restored in the function prologue and epilogue if the given function
calls
    __builtin_eh_return.  This causes the return value to be overwritten on
normal
    return paths and breaks a rare case of libgcc's _Unwind_RaiseException.

    gcc/ChangeLog:

            PR target/114848
            * config/loongarch/loongarch.cc: Do not restore the saved eh_return
            data registers ($r4-$r7) for a normal return of a function that
calls
            __builtin_eh_return elsewhere.
            * config/loongarch/loongarch-protos.h: Same.
            * config/loongarch/loongarch.md: Same.

    gcc/testsuite/ChangeLog:

            * gcc.target/loongarch/eh_return-normal-return.c: New test.

    (cherry picked from commit 4b421728289e6f1caa0dfaa953a11698ab95d37d)

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

* [Bug target/114848] loongarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return
  2024-04-25  7:45 [Bug target/114848] New: longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return pinskia at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2024-04-30  1:09 ` cvs-commit at gcc dot gnu.org
@ 2024-04-30  1:11 ` cvs-commit at gcc dot gnu.org
  2024-04-30  1:20 ` xry111 at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-04-30  1:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by LuluCheng
<chenglulu@gcc.gnu.org>:

https://gcc.gnu.org/g:88f22217521564e1a956e14ac55456caa160e055

commit r13-8661-g88f22217521564e1a956e14ac55456caa160e055
Author: Yang Yujie <yangyujie@loongson.cn>
Date:   Fri Dec 8 18:01:18 2023 +0800

    LoongArch: Fix eh_return epilogue for normal returns.

    On LoongArch, the regitsters $r4 - $r7 (EH_RETURN_DATA_REGNO) will be saved
    and restored in the function prologue and epilogue if the given function
calls
    __builtin_eh_return.  This causes the return value to be overwritten on
normal
    return paths and breaks a rare case of libgcc's _Unwind_RaiseException.

    gcc/ChangeLog:

            PR target/114848
            * config/loongarch/loongarch.cc: Do not restore the saved eh_return
            data registers ($r4-$r7) for a normal return of a function that
calls
            __builtin_eh_return elsewhere.
            * config/loongarch/loongarch-protos.h: Same.
            * config/loongarch/loongarch.md: Same.

    gcc/testsuite/ChangeLog:

            * gcc.target/loongarch/eh_return-normal-return.c: New test.

    (cherry picked from commit 4b421728289e6f1caa0dfaa953a11698ab95d37d)

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

* [Bug target/114848] loongarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return
  2024-04-25  7:45 [Bug target/114848] New: longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return pinskia at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2024-04-30  1:11 ` cvs-commit at gcc dot gnu.org
@ 2024-04-30  1:20 ` xry111 at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-04-30  1:20 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
   Target Milestone|---                         |12.4
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #8 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
Fixed for 12 and 13.

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

end of thread, other threads:[~2024-04-30  1:20 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-25  7:45 [Bug target/114848] New: longarch: epilogue in _Unwind_RaiseException corrupts return value due to __builtin_eh_return pinskia at gcc dot gnu.org
2024-04-25  8:00 ` [Bug target/114848] " xry111 at gcc dot gnu.org
2024-04-25  8:04 ` pinskia at gcc dot gnu.org
2024-04-25  8:11 ` [Bug target/114848] loongarch: " xry111 at gcc dot gnu.org
2024-04-25  8:21 ` pinskia at gcc dot gnu.org
2024-04-27  9:05 ` chenglulu at loongson dot cn
2024-04-30  1:09 ` cvs-commit at gcc dot gnu.org
2024-04-30  1:11 ` cvs-commit at gcc dot gnu.org
2024-04-30  1:20 ` xry111 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).