From 97be0eae9b34bf5b071170baeb94a5e06cd1daed Mon Sep 17 00:00:00 2001 From: diviocity Date: Mon, 30 Nov 2026 04:30:32 +0100 Subject: [PATCH] Restore GNU arithmetic library exports and verify MPFR thread isolation --- gmp/PKGBUILD.vile | 13 +++++++++---- mpfr/PKGBUILD.vile | 12 ++++++++---- mpfr/thread-state.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 mpfr/thread-state.c diff --git a/gmp/PKGBUILD.vile b/gmp/PKGBUILD.vile index 7c08d62..dc4a72a 100644 --- a/gmp/PKGBUILD.vile +++ b/gmp/PKGBUILD.vile @@ -5,7 +5,7 @@ libc := if arm { "musl>=1.2.6-4" } else { "musl" } defpkgbuild(gmp, #:pkgname, "gmp", #:pkgver, "6.3.0", - #:pkgrel, if arm { 3 } else { 2 }, + #:pkgrel, 3, #:pkgdesc, "GNU multiple precision arithmetic library", #:arch, [#:x86-64, #:aarch64], #:url, "https://gmplib.org/", @@ -14,12 +14,17 @@ defpkgbuild(gmp, #:source, ["https://distfiles.alpinelinux.org/distfiles/v3.21/gmp-6.3.0.tar.xz"], #:sha256sums, ["a3c2b80201b89e68616f4ad30bc66aee4927c3ce50e33929ca819d5c43538898"], #:build, fn() { - ; Upstream provides runtime CPU dispatch on x86-64. - dispatch := if arm { "--disable-fat" } else { "--enable-fat" } run("env", "CFLAGS=-O2 -std=gnu17", "CC_FOR_BUILD=gcc", "vinix-configure", "gmp-6.3.0/configure", - "--enable-shared", "--enable-static", "--disable-cxx", dispatch) + "--enable-shared", "--enable-static", "--disable-cxx", "--disable-fat") run("make") }, + #:check, fn() { + if not(arm) { + for-each(fn(symbol) { + run("sh", "-ec", "nm -D --defined-only \"$1\" | awk -v name=\"$2\" '$3 == name { found = 1 } END { exit !found }'", "check-mpn", ".libs/libgmp.so", symbol) + }, list("__gmpn_add_nc", "__gmpn_addaddmul_1msb0", "__gmpn_modexact_1_odd", "__gmpn_mul_1c", "__gmpn_sub_nc")) + } + }, #:package, fn() { run("make", string-append("DESTDIR=", pkgdir()), "install") run("rm", "-f", string-append(pkgdir(), "/usr/share/info/dir")) diff --git a/mpfr/PKGBUILD.vile b/mpfr/PKGBUILD.vile index e519f14..9cda59c 100644 --- a/mpfr/PKGBUILD.vile +++ b/mpfr/PKGBUILD.vile @@ -5,19 +5,23 @@ libc := if arm { "musl>=1.2.6-4" } else { "musl" } defpkgbuild(mpfr, #:pkgname, "mpfr", #:pkgver, "4.2.1", - #:pkgrel, if arm { 3 } else { 2 }, + #:pkgrel, if arm { 4 } else { 3 }, #:pkgdesc, "Multiple precision floating-point arithmetic with correct rounding", #:arch, [#:x86-64, #:aarch64], #:url, "https://www.mpfr.org/", #:license, "LGPL-3.0-or-later", #:depends, list(libc, if arm { "gmp>=6.3.0-3" } else { "gmp" }), - #:source, ["https://distfiles.alpinelinux.org/distfiles/v3.21/mpfr-4.2.1.tar.xz"], - #:sha256sums, ["277807353a6726978996945af13e52829e3abd7a9a5b7fb2793894e18f1fcbb2"], + #:source, ["https://distfiles.alpinelinux.org/distfiles/v3.21/mpfr-4.2.1.tar.xz", "thread-state.c"], + #:sha256sums, ["277807353a6726978996945af13e52829e3abd7a9a5b7fb2793894e18f1fcbb2", "2950b3f63b5596ce23f83edbee76ef80d3266aa936ff88dc7fab5d97b6ce8acd"], #:build, fn() { run("env", "CFLAGS=-O2 -std=gnu17", "CC_FOR_BUILD=gcc", "vinix-configure", "mpfr-4.2.1/configure", - "--enable-shared", "--enable-static") + "--enable-shared", "--enable-static", "--enable-thread-safe") run("make") }, + #:check, fn() { + run("vinix-cc", "-std=gnu17", "-pthread", "-Impfr-4.2.1/src", "thread-state.c", "-Lsrc/.libs", "-lmpfr", "-lgmp", "-o", "thread-state") + run("vinix-run", "--library-path", "src/.libs", "./thread-state") + }, #:package, fn() { run("make", string-append("DESTDIR=", pkgdir()), "install") run("rm", "-f", string-append(pkgdir(), "/usr/share/info/dir")) diff --git a/mpfr/thread-state.c b/mpfr/thread-state.c new file mode 100644 index 0000000..8fd3b83 --- /dev/null +++ b/mpfr/thread-state.c @@ -0,0 +1,47 @@ +#include +#include +#include + +static pthread_barrier_t changed; +static pthread_barrier_t checked; + +static void *set_worker_defaults(void *unused) +{ + (void)unused; + mpfr_set_default_prec(113); + mpfr_set_default_rounding_mode(MPFR_RNDD); + mpfr_clear_flags(); + mpfr_set_overflow(); + pthread_barrier_wait(&changed); + pthread_barrier_wait(&checked); + return NULL; +} + +int main(void) +{ + pthread_t thread; + if (pthread_barrier_init(&changed, NULL, 2) || + pthread_barrier_init(&checked, NULL, 2)) + return 1; + mpfr_set_default_prec(67); + mpfr_set_default_rounding_mode(MPFR_RNDU); + mpfr_clear_flags(); + mpfr_set_underflow(); + if (pthread_create(&thread, NULL, set_worker_defaults, NULL)) + return 1; + pthread_barrier_wait(&changed); + int independent = mpfr_get_default_prec() == 67 + && mpfr_get_default_rounding_mode() == MPFR_RNDU + && mpfr_underflow_p() && !mpfr_overflow_p(); + pthread_barrier_wait(&checked); + if (pthread_join(thread, NULL)) + return 1; + pthread_barrier_destroy(&changed); + pthread_barrier_destroy(&checked); + if (!independent || !mpfr_buildopt_tls_p()) { + fputs("MPFR thread-local defaults or flags changed\n", stderr); + return 1; + } + puts("MPFR kept separate precision, rounding and flags in two threads"); + return 0; +} -- 2.56.1