Bug 255320

Summary: Wrong setjmp return value in FreeBSD 13 (at least on aarch64)
Product: Base System Reporter: Philipp <pkk>
Component: miscAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Many People CC: alois+freebsd, emaste, grahamperrin
Priority: --- Keywords: needs-qa
Version: 13.0-RELEASE   
Hardware: Any   
OS: Any   
See Also: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=268521

Description Philipp 2021-04-22 08:21:17 UTC
I noticed some programs failing on my Raspi 4 on FreeBSD 13, and tracked the issue down to setjmp behaviour.

Apparently setjmp returns 0 when it should return 1.

The FreeBSD man page for setjmp / longjmp does _not_ state that setjmp should return 1 on a longjmp with argument 0. But it states that the functions are conforming to the ISO C90 standard. All C standards, including ISO C90 mandate that setjmp should return 1 on a longjmp with argument 0.

I don't know if this is an issue specific to aarch64, or just some generic FreeBSD issue, as the Raspi is my only FreeBSD system.

I created a small test program to reproduce the issue; it fails on the Raspi, but passes on Debian GNU/Linux on amd64:

I compile using cc test.c, execute via ./a.out.

#include <setjmp.h>
#include <assert.h>

jmp_buf buf;

void g(void)
{
	// When called with an argument of 0, longjmp() should make setjmp() return 1 instead.
	longjmp(buf, 0);
	g();
}

void f1(void)
{
	static int i;
	int j;
	i = 0;
	j = setjmp(buf);
	assert(i == j);
	i++;
	if(!j)
		g();
}

int main(void)
{
	f1();	
	return 0;
}
Comment 1 Ed Maste freebsd_committer freebsd_triage 2021-08-06 23:06:39 UTC
On FreeBSD's arm64 reference system I do not reproduce a problem

FreeBSD ref13-amd64.nyi.freebsd.org 13.0-STABLE FreeBSD 13.0-STABLE #0 stable/13-n245208-5d02b30ebd8: Tue Apr 13 09:30:31 UTC 2021     root@build-13.freebsd.org:/usr/obj/usr/src/sys/CLUSTER13  amd64

FreeBSD clang version 11.0.1 (git@github.com:llvm/llvm-project.git llvmorg-11.0.1-0-g43ff75f2c3fe)
Target: x86_64-unknown-freebsd13.0
Thread model: posix
InstalledDir: /usr/bin

ref13-amd64% cc test.c
ref13-amd64% ./a.out
ref13-amd64%
Comment 2 Philipp 2022-05-18 06:02:12 UTC
I can still reproduce the issue on my Raspi 4 after upgrading to FreeBSD 13.1.
Comment 3 Alois Klink 2022-12-22 23:47:55 UTC
I can confirm that this still affects aarch64 as of the FreeBSD CURRENT main branch. I have a patch and test case at https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=268521.
Comment 4 Philipp 2023-09-14 08:43:14 UTC
This works for me now (FreeBSD 13.2).

Looks like the following commit fixed it:
https://cgit.freebsd.org/src/commit/?id=9fb118bebced1452a46756a13be0161021b10905
Comment 5 Ed Maste freebsd_committer freebsd_triage 2023-09-14 08:53:26 UTC
Thank you for identifying the commit.