From 09438751ec7a9c7f6a8fc7165032727985691586 Mon Sep 17 00:00:00 2001 From: Moritz Date: Tue, 17 Nov 2026 15:09:49 +0000 Subject: [PATCH] [boring] Run the prebuilt Linux libraries on glibc 2.28, and detect other C libraries The prebuilt Linux libraries were built on Ubuntu 22.04, so they required glibc 2.34 (pthread functions that moved into libc), and failed to load on, for example, Debian 11, Ubuntu 20.04, and RHEL 8. They also failed to load on musl, such as on Alpine Linux, because the prebuilt library is picked by OS and architecture only. - Build the Linux libraries in manylinux_2_28 (AlmaLinux 8) containers, and check that they only require glibc 2.28. Older glibc can't run the actions' Node.js in the container. - In the `fetch` build mode, when building for the Linux machine the hook runs on, check its C library with gnu_get_libc_version(), and fail with a hint to use `buildMode: checkout` on musl or glibc older than 2.28, instead of bundling a library that fails to load. --- .github/workflows/boring_binaries.yaml | 23 ++++++--- pkgs/boring/CHANGELOG.md | 5 ++ pkgs/boring/README.md | 11 ++-- pkgs/boring/lib/src/hook_helpers/fetch.dart | 13 +++++ pkgs/boring/lib/src/hook_helpers/libc.dart | 56 +++++++++++++++++++++ pkgs/boring/test/hook_helpers_test.dart | 36 +++++++++++++ 6 files changed, 133 insertions(+), 11 deletions(-) create mode 100644 pkgs/boring/lib/src/hook_helpers/libc.dart diff --git a/.github/workflows/boring_binaries.yaml b/.github/workflows/boring_binaries.yaml index 8ed7bdace3..78db59323a 100644 --- a/.github/workflows/boring_binaries.yaml +++ b/.github/workflows/boring_binaries.yaml @@ -43,6 +43,7 @@ jobs: precompile: name: Precompile (${{ matrix.target-os }}-${{ matrix.target-arch }}${{ matrix.ios-sdk && format('-{0}', matrix.ios-sdk) || '' }}) runs-on: ${{ matrix.os }} + container: ${{ matrix.container }} permissions: contents: read id-token: write # To sign the provenance attestations of the libraries. @@ -60,16 +61,21 @@ jobs: # `macOSDeploymentTargets` and `iOSDeploymentTarget` in # tool/precompile_binaries.dart. include: - # The oldest available Ubuntu, so the libraries only require glibc - # 2.35. - - os: ubuntu-22.04 + # Built in manylinux_2_28 (AlmaLinux 8) containers, so the libraries + # run on glibc 2.28 and later, for example on Debian 10, Ubuntu 20.04, + # and RHEL 8, not only on the runner's glibc. Older glibc can't run + # the actions (Node.js) in the container. Keep `max-glibc` in sync + # with `prebuiltLinuxGlibc` in lib/src/hook_helpers/libc.dart. + - os: ubuntu-24.04 + container: quay.io/pypa/manylinux_2_28_x86_64 target-os: linux target-arch: x64 - max-glibc: '2.35' - - os: ubuntu-22.04-arm + max-glibc: '2.28' + - os: ubuntu-24.04-arm + container: quay.io/pypa/manylinux_2_28_aarch64 target-os: linux target-arch: arm64 - max-glibc: '2.35' + max-glibc: '2.28' - os: macos-15 target-os: macos target-arch: arm64 @@ -111,6 +117,11 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 with: persist-credentials: false + # The manylinux images have GCC, CMake, and Make, but setup-dart needs + # unzip. + - name: Install unzip (manylinux) + if: matrix.container + run: dnf install -y unzip - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d with: sdk: stable diff --git a/pkgs/boring/CHANGELOG.md b/pkgs/boring/CHANGELOG.md index aadded4955..54f09edda5 100644 --- a/pkgs/boring/CHANGELOG.md +++ b/pkgs/boring/CHANGELOG.md @@ -1,4 +1,9 @@ ## 0.4.3-wip +- The prebuilt Linux libraries run on glibc 2.28 and later (for example + Debian 10, Ubuntu 20.04, and RHEL 8), instead of 2.35. On systems with an + older glibc or another C library, such as musl on Alpine Linux, the + `fetch` build mode fails with a hint to use `buildMode: checkout`, instead + of bundling a library that fails to load. - BoringSSL is compiled with `OPENSSL_SMALL`, which for example makes the tree-shaken library of `package:webcrypto` about 19% smaller, and P-256 operations about 2x slower. diff --git a/pkgs/boring/README.md b/pkgs/boring/README.md index 60780b9fda..b0e35b6e48 100644 --- a/pkgs/boring/README.md +++ b/pkgs/boring/README.md @@ -60,11 +60,12 @@ hooks: - **`local`**: Uses a custom prebuilt dynamic library at `localPath`, which is bundled as is, without [tree-shaking](#tree-shaking). -Prebuilt libraries exist for Linux (x64 and arm64), macOS (arm64 and x64), -Windows (x64), Android (arm, arm64 and x64; API 21 and later), and iOS (arm64, -and the arm64 and x64 simulators; iOS 12 and later). Other targets require -`buildMode: checkout`, which requires CMake, Ninja, and a C toolchain (Clang or -GCC, Xcode, MSVC, or the Android NDK), plus NASM on Windows. +Prebuilt libraries exist for Linux (x64 and arm64; glibc 2.28 and later, not +musl), macOS (arm64 and x64), Windows (x64), Android (arm, arm64 and x64; API 21 +and later), and iOS (arm64, and the arm64 and x64 simulators; iOS 12 and later). +Other targets require `buildMode: checkout`, which requires CMake, Ninja, and a +C toolchain (Clang or GCC, Xcode, MSVC, or the Android NDK), plus NASM on +Windows. When the Dart runtime uses a sanitizer, for example with `dart test -p vm-asan`, the `fetch` build mode compiles BoringSSL from source with that diff --git a/pkgs/boring/lib/src/hook_helpers/fetch.dart b/pkgs/boring/lib/src/hook_helpers/fetch.dart index 85c4407e47..94e9eb4c68 100644 --- a/pkgs/boring/lib/src/hook_helpers/fetch.dart +++ b/pkgs/boring/lib/src/hook_helpers/fetch.dart @@ -11,6 +11,7 @@ import 'package:crypto/crypto.dart' show sha256; import 'package:hooks/hooks.dart'; import 'hashes.dart' show fileHashes, releaseRepository, releaseTag; +import 'libc.dart' show currentGlibcVersion, unsupportedLinuxLibcReason; import 'targets.dart' show libraryFileName, releaseAssetName, releaseAssetUrl, targetTripleFor; @@ -58,6 +59,18 @@ Future fetchPrebuiltLibrary( ); } + // The prebuilt libraries would fail to load on another C library. Only the + // C library of this machine is known, so this only checks builds for it, as + // in `dart run` and `dart test`. + if (targetOS == OS.linux && + OS.current == OS.linux && + targetArch == Architecture.current) { + final reason = unsupportedLinuxLibcReason(currentGlibcVersion()); + if (reason != null) { + throw BuildError(message: '$reason\n$_checkoutHint'); + } + } + final fileName = libraryFileName(targetOS, static: static); final cachedFile = File.fromUri( input.outputDirectoryShared diff --git a/pkgs/boring/lib/src/hook_helpers/libc.dart b/pkgs/boring/lib/src/hook_helpers/libc.dart new file mode 100644 index 0000000000..174f26d9b4 --- /dev/null +++ b/pkgs/boring/lib/src/hook_helpers/libc.dart @@ -0,0 +1,56 @@ +// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:ffi'; + +/// The oldest glibc that the prebuilt Linux libraries run on. +/// +/// Keep in sync with `max-glibc` of the Linux targets in +/// `.github/workflows/boring_binaries.yaml`, which checks the libraries. +const prebuiltLinuxGlibc = (2, 28); + +/// The version of the GNU C library (glibc) of this process, or `null` if the +/// process doesn't use glibc, for example on Alpine Linux, which uses musl. +(int, int)? currentGlibcVersion() { + final process = DynamicLibrary.process(); + if (!process.providesSymbol('gnu_get_libc_version')) { + return null; + } + final gnuGetLibcVersion = process + .lookupFunction Function(), Pointer Function()>( + 'gnu_get_libc_version', + ); + final version = gnuGetLibcVersion(); + final bytes = []; + for (var i = 0; version[i] != 0; i++) { + bytes.add(version[i]); + } + return parseGlibcVersion(String.fromCharCodes(bytes)); +} + +/// Parses the major and minor version from a glibc version string, such as +/// `2.35` or `2.39.9000`. +(int, int)? parseGlibcVersion(String version) { + final match = RegExp(r'^(\d+)\.(\d+)').firstMatch(version); + if (match == null) { + return null; + } + return (int.parse(match[1]!), int.parse(match[2]!)); +} + +/// Why the prebuilt Linux libraries can't run with the C library +/// [glibcVersion] (see [currentGlibcVersion]), or `null` if they can. +String? unsupportedLinuxLibcReason((int, int)? glibcVersion) { + final (minMajor, minMinor) = prebuiltLinuxGlibc; + if (glibcVersion == null) { + return 'The prebuilt Linux libraries require glibc, but this system uses ' + 'another C library, such as musl on Alpine Linux.'; + } + final (major, minor) = glibcVersion; + if (major < minMajor || (major == minMajor && minor < minMinor)) { + return 'The prebuilt Linux libraries require glibc $minMajor.$minMinor or ' + 'newer, but this system has glibc $major.$minor.'; + } + return null; +} diff --git a/pkgs/boring/test/hook_helpers_test.dart b/pkgs/boring/test/hook_helpers_test.dart index f9064829c5..0984f34a6e 100644 --- a/pkgs/boring/test/hook_helpers_test.dart +++ b/pkgs/boring/test/hook_helpers_test.dart @@ -7,6 +7,7 @@ import 'dart:io'; import 'package:boring/src/hook_helpers/build_options.dart'; import 'package:boring/src/hook_helpers/fetch.dart'; import 'package:boring/src/hook_helpers/hashes.dart'; +import 'package:boring/src/hook_helpers/libc.dart'; import 'package:boring/src/hook_helpers/targets.dart'; import 'package:code_assets/code_assets.dart'; import 'package:hooks/hooks.dart'; @@ -136,6 +137,41 @@ void main() { }); }); + group('libc.dart', () { + test('parses glibc versions', () { + expect(parseGlibcVersion('2.35'), equals((2, 35))); + expect(parseGlibcVersion('2.39.9000'), equals((2, 39))); + expect(parseGlibcVersion('unknown'), isNull); + }); + + test('accepts glibc from prebuiltLinuxGlibc on', () { + final (major, minor) = prebuiltLinuxGlibc; + expect(unsupportedLinuxLibcReason((major, minor)), isNull); + expect(unsupportedLinuxLibcReason((major, minor + 7)), isNull); + expect(unsupportedLinuxLibcReason((major + 1, 0)), isNull); + }); + + test('rejects older glibc and other C libraries', () { + final (major, minor) = prebuiltLinuxGlibc; + expect( + unsupportedLinuxLibcReason((major, minor - 1)), + contains('require glibc $major.$minor or newer'), + ); + expect(unsupportedLinuxLibcReason(null), contains('musl')); + }); + + test('finds the glibc of this process', () { + final version = currentGlibcVersion(); + if (Platform.isLinux) { + // The CI runners use glibc. + expect(version, isNotNull); + expect(unsupportedLinuxLibcReason(version), isNull); + } else { + expect(version, isNull); + } + }); + }); + group('fetchPrebuiltLibrary', () { test('throws a BuildError when no prebuilt binary exists for target', () { final builder = BuildInputBuilder()