Bug 268521 - arm64 libc: fix longjmp with 0 value
Summary: arm64 libc: fix longjmp with 0 value
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: arm (show other bugs)
Version: CURRENT
Hardware: arm64 Any
: --- Affects Many People
Assignee: Jessica Clarke
URL: https://github.com/aloisklink/freebsd...
Keywords: needs-qa
Depends on:
Blocks:
 
Reported: 2022-12-22 23:36 UTC by Alois Klink
Modified: 2024-06-14 15:06 UTC (History)
3 users (show)

See Also:


Attachments
`git format-patch` patch file (1.89 KB, patch)
2022-12-22 23:36 UTC, Alois Klink
no flags Details | Diff
Test case (warning, tests all architectures, not just aarch64) (4.50 KB, patch)
2022-12-22 23:45 UTC, Alois Klink
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Alois Klink 2022-12-22 23:36:00 UTC
Created attachment 238981 [details]
`git format-patch` patch file

On arm64/aarch64, calling `longjmp(x, 0);` makes `setjmp(x)` return 0,
which normally causes an infinite loop, and is against the ISO C
standard for setjmp/longjmp. Instead, using a value of 0 should
make `setjmp` return 1:
    
> The `longjmp` function cannot cause the `setjmp` macro to return the
> value 0; if `val` is 0, the `setjmp` macro returns the value 1.
>
> _Taken from ยง7.13.2.1.4 of the C99 spec_

This has already been reported in https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=255320, but the contributing docs weren't clear on what to do if I had a patch for an already existing problem report, so I thought I'd make another problem report so it has `[PATCH]` in the name.

My commit is also available on GitHub https://github.com/aloisklink/freebsd-src/tree/fix-longjmp-with-0-val

I also have a patch that adds tests for `longjmp(x, 0)` at https://github.com/aloisklink/freebsd-src/commit/007af6a46677b143f9544fd30e30a1b9f1048ae6
However, since there might be a few architectures that suffer from this bug, I'm not 100% sure if this okay to merge. I'll make a new PR for it.
Comment 1 Alois Klink 2022-12-22 23:45:35 UTC
Created attachment 238982 [details]
Test case (warning, tests all architectures, not just aarch64)

Oh, it looks like you can upload test cases to Bugzilla :)

I'll upload the test case here then, and somebody can let me know if I should make another PR for it too.

I've got a feeling that a bunch of other architectures have this same bug, so I'm not sure if it should yet be merged.

Cherry-pick https://anonhg.netbsd.org/src/rev/1f14ece9dc46 from
upstream (NetBSD) source.

Original commit title was:

> PR/56066: Jessica Clarke:
> Add tests for calling {_,}longjmp with a zero value.

See [NetBSD PR/56066](https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=56066)

---

The author of this patch is taken from the NetBSD code repo. However, the original author of this patch in the NetBSD PR is `Jessica Clarke <jrtc27@FreeBSD.org>`. Should we credit them as the commit author instead?
Comment 2 Graham Perrin freebsd_committer freebsd_triage 2022-12-23 06:52:23 UTC
Triage: summary line tags such as [patch] are no longer used.
Comment 3 Alois Klink 2022-12-31 19:48:25 UTC
(In reply to Graham Perrin from comment #2)

It may be worth updating https://docs.freebsd.org/en/articles/contributing/.

It still recommends putting `[PATCH]` in the PR synopsis.

---

I've also found out that RISC-V is also affected by this issue, and since I had a QEMU VM set up for it, I've made a fix for it in https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=268684

Also, is it worth CC-ing in `Jessica Clarke <jrtc27@FreeBSD.org>`? They submitted a patch for a test-case for the same issue in NetBSD, but I'm guessing from the `FreeBSD.org` domain in their email, they might also be interested in seeing this fixed in FreeBSD.
Comment 4 Jessica Clarke freebsd_committer freebsd_triage 2023-01-09 18:46:45 UTC
Didn't notice this bug when committing https://cgit.FreeBSD.org/src/commit/?id=9fb118bebced1452a46756a13be0161021b10905, so here's my best commit-hook@FreeBSD.org impression:

A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=9fb118bebced1452a46756a13be0161021b10905

commit 9fb118bebced1452a46756a13be0161021b10905
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2023-01-09 18:34:43 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2023-01-09 18:34:43 +0000

    libc: Fix longjmp/_longjmp(buf, 0) for AArch64 and RISC-V

    These architectures fail to handle this special case, and will cause the
    corresponding setjmp/_setjmp to return 0 rather than 1. Fix this and add
    regression tests (also committed upstream).

    PR:             268684
    Reviewed by:    arichardson, jhb
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D29363

 contrib/netbsd-tests/lib/libc/setjmp/t_setjmp.c | 50 ++++++++++++++++++++++---
 lib/libc/aarch64/gen/_setjmp.S                  |  3 +-
 lib/libc/aarch64/gen/setjmp.S                   |  3 +-
 lib/libc/riscv/gen/_setjmp.S                    |  3 ++
 lib/libc/riscv/gen/setjmp.S                     |  3 ++
 5 files changed, 55 insertions(+), 7 deletions(-)