Repository navigation
libc, rtld: translate osreldate to a FreeBSD version for ABI checks #644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /*- | ||
| * SPDX-License-Identifier: BSD-2-Clause | ||
| * | ||
| * Copyright (c) 2026 The MidnightBSD Project | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions | ||
| * are met: | ||
| * 1. Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * 2. Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
| * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| * SUCH DAMAGE. | ||
| */ | ||
|
|
||
| #include <sys/param.h> | ||
|
|
||
| #include "libc_private.h" | ||
|
|
||
| /* | ||
| * The kernel reports __MidnightBSD_version as its osreldate, but libc and | ||
| * rtld compare the value returned by __getosreldate() against FreeBSD | ||
| * version thresholds (INO64_FIRST, P_OSREL_*, ...). Translate a | ||
| * MidnightBSD version to the __FreeBSD_version of the tree that release | ||
| * was built from, so those checks see the features the kernel really has. | ||
| * | ||
| * Each entry is the first MidnightBSD version with that FreeBSD version. | ||
| * Where one MidnightBSD version spanned several FreeBSD versions, the | ||
| * lowest is used, so a feature is never assumed before it existed. Keep | ||
| * this table in sync with sys/sys/param.h when the FreeBSD base changes. | ||
| */ | ||
| static const struct { | ||
| int mnbsd; | ||
| int fbsd; | ||
| } osrel_map[] = { | ||
| { 400001, 1305500 }, /* 4.0 */ | ||
| { 301006, 1204500 }, /* 3.1 */ | ||
| { 300003, 1203507 }, /* 3.0 */ | ||
| { 200000, 1103507 }, /* 2.0 */ | ||
| { 100001, 1004501 }, /* 1.0 */ | ||
| }; | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| /* | ||
| * MidnightBSD versions are below the oldest FreeBSD version userland still | ||
| * checks for. Anything at or above this is already a FreeBSD version (for | ||
| * example, a jail created with a FreeBSD osreldate) and is returned | ||
| * unchanged. | ||
| * | ||
| * Values below this are always treated as MidnightBSD versions, even | ||
| * though FreeBSD 4-6 used the same range. That is safe: this code only | ||
| * runs in MidnightBSD userland, and a FreeBSD 4-6 jail runs its own libc | ||
| * and rtld, which never call it. | ||
| */ | ||
| #define OSREL_FREEBSD_MIN 700000 | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| int | ||
| __osrel_to_freebsd(int osrel) | ||
| { | ||
| size_t i; | ||
|
|
||
| if (osrel <= 0 || osrel >= OSREL_FREEBSD_MIN) | ||
| return (osrel); | ||
|
|
||
| /* A kernel at least as new as this libc has everything it knows. */ | ||
| if (osrel >= __MidnightBSD_version) | ||
| return (__FreeBSD_version); | ||
|
|
||
| for (i = 0; i < nitems(osrel_map); i++) { | ||
| if (osrel >= osrel_map[i].mnbsd) | ||
| return (osrel_map[i].fbsd); | ||
| } | ||
|
|
||
| /* Older than any release with a FreeBSD 10 base. */ | ||
| return (osrel); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /*- | ||
| * SPDX-License-Identifier: BSD-2-Clause | ||
| * | ||
| * Copyright (c) 2026 The MidnightBSD Project | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions | ||
| * are met: | ||
| * 1. Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * 2. Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
| * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| * SUCH DAMAGE. | ||
| */ | ||
|
|
||
| /* | ||
| * Tests for __osrel_to_freebsd(), which libc and rtld use to turn the | ||
| * kernel's MidnightBSD osreldate into a FreeBSD version for ABI checks. | ||
| * The function is private to libc, so its source is built into this test. | ||
| */ | ||
|
|
||
| #include <sys/param.h> | ||
| #include <sys/sysctl.h> | ||
|
|
||
| #include <atf-c.h> | ||
|
|
||
| int __osrel_to_freebsd(int); | ||
|
Check failure on line 39 in lib/libc/tests/gen/osrel_freebsd_test.c
|
||
|
|
||
| /* From lib/libc/sys/compat-ino64.h. */ | ||
| #define INO64_FIRST 1200031 | ||
|
|
||
| ATF_TC_WITHOUT_HEAD(passthrough); | ||
| ATF_TC_BODY(passthrough, tc) | ||
| { | ||
| /* Unset, invalid, and values that are already FreeBSD versions. */ | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(0), 0); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(-1), -1); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(700000), 700000); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(1305500), 1305500); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(1400000), 1400000); | ||
|
|
||
| /* Older than any release with a FreeBSD 10 base. */ | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(9005), 9005); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(100000), 100000); | ||
| } | ||
|
|
||
| ATF_TC_WITHOUT_HEAD(table_boundaries); | ||
| ATF_TC_BODY(table_boundaries, tc) | ||
| { | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(100001), 1004501); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(199999), 1004501); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(200000), 1103507); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(300002), 1103507); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(300003), 1203507); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(301005), 1203507); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(301006), 1204500); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(400000), 1204500); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(400001), 1305500); | ||
| } | ||
|
|
||
| ATF_TC_WITHOUT_HEAD(current_release); | ||
| ATF_TC_BODY(current_release, tc) | ||
| { | ||
| /* A kernel at least as new as this libc maps to its FreeBSD base. */ | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(__MidnightBSD_version), | ||
| __FreeBSD_version); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(__MidnightBSD_version + 1), | ||
| __FreeBSD_version); | ||
| ATF_CHECK_EQ(__osrel_to_freebsd(699999), __FreeBSD_version); | ||
| } | ||
|
|
||
| ATF_TC_WITHOUT_HEAD(running_kernel); | ||
| ATF_TC_BODY(running_kernel, tc) | ||
| { | ||
| size_t len; | ||
| int osrel; | ||
|
|
||
| len = sizeof(osrel); | ||
| ATF_REQUIRE(sysctlbyname("kern.osreldate", &osrel, &len, NULL, 0) == | ||
| 0); | ||
|
|
||
| /* | ||
| * The running kernel has the ino64 syscalls, so libc must not fall | ||
| * back to the freebsd11 compat versions. | ||
| */ | ||
| ATF_CHECK_MSG(__osrel_to_freebsd(osrel) >= INO64_FIRST, | ||
| "kern.osreldate %d translates to %d, below INO64_FIRST", osrel, | ||
| __osrel_to_freebsd(osrel)); | ||
| } | ||
|
|
||
| ATF_TP_ADD_TCS(tp) | ||
| { | ||
| ATF_TP_ADD_TC(tp, passthrough); | ||
| ATF_TP_ADD_TC(tp, table_boundaries); | ||
| ATF_TP_ADD_TC(tp, current_release); | ||
| ATF_TP_ADD_TC(tp, running_kernel); | ||
|
|
||
| return (atf_no_error()); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.