From d16ab4c25e86d839e40bd4c4ca08cd3e260f64e9 Mon Sep 17 00:00:00 2001 From: Moritz Date: Tue, 17 Nov 2026 15:02:18 +0000 Subject: [PATCH 1/3] [boring] Bind BN_ULONG as UintPtr BoringSSL's BN_ULONG is uint64_t on 64-bit targets and uint32_t on 32-bit targets (openssl/base.h). ffigen runs on a 64-bit host, so the bindings used Uint64, which passes and returns the wrong values for the BN_*_word functions on 32-bit targets such as Android arm. For example, BN_set_word(bn, 65537) set the wrong RSA public exponent in package:webcrypto (fixed there in google/webcrypto.dart#432 before it moved to package:boring). Map BN_ULONG to the pointer-sized UintPtr in tool/ffigen.dart, and keep the BN_ULONG and DartBN_ULONG typedefs in bindings.dart, because ffigen no longer generates them. --- pkgs/boring/CHANGELOG.md | 3 ++ pkgs/boring/lib/bindings.dart | 11 +++++++ pkgs/boring/lib/src/bindings/boringssl.g.dart | 32 +++++++++---------- pkgs/boring/test/boring_test.dart | 14 ++++++++ pkgs/boring/tool/ffigen.dart | 8 +++++ 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/pkgs/boring/CHANGELOG.md b/pkgs/boring/CHANGELOG.md index 00cece7f64..6930333a13 100644 --- a/pkgs/boring/CHANGELOG.md +++ b/pkgs/boring/CHANGELOG.md @@ -1,4 +1,7 @@ ## 0.4.3-wip +- Fixed the bindings of the `BN_*_word` functions on 32-bit targets, such as + Android arm. `BN_ULONG` is now `UintPtr` instead of `Uint64`, because it is + `uint32_t` on 32-bit targets. - Update BoringSSL to [8f2554dc](https://boringssl.googlesource.com/boringssl/+/8f2554dc344d403fd6d628773d39637c323eb801). ## 0.4.2 diff --git a/pkgs/boring/lib/bindings.dart b/pkgs/boring/lib/bindings.dart index a15c8ca93e..eff717e3f2 100644 --- a/pkgs/boring/lib/bindings.dart +++ b/pkgs/boring/lib/bindings.dart @@ -16,6 +16,17 @@ import 'src/bindings/boringssl.g.dart' as bssl; export 'src/arena.dart'; export 'src/bindings/boringssl.g.dart'; +/// BoringSSL's `BN_ULONG`, a word of a [bssl.BIGNUM]. +/// +/// It's pointer-sized: `uint64_t` on 64-bit targets and `uint32_t` on 32-bit +/// targets. The generated bindings use [ffi.UintPtr] for it. +// ignore: camel_case_types +typedef BN_ULONG = ffi.UintPtr; + +/// The Dart type of [BN_ULONG]. +// ignore: camel_case_types +typedef DartBN_ULONG = int; + /// An [ffi.Allocator] backed by BoringSSL's [bssl.OPENSSL_malloc] and /// [bssl.OPENSSL_free]. /// diff --git a/pkgs/boring/lib/src/bindings/boringssl.g.dart b/pkgs/boring/lib/src/bindings/boringssl.g.dart index 99f87a1937..d4e7ce9505 100644 --- a/pkgs/boring/lib/src/bindings/boringssl.g.dart +++ b/pkgs/boring/lib/src/bindings/boringssl.g.dart @@ -4106,7 +4106,7 @@ external int BN_MONT_CTX_set( /// BN_abs_is_word returns one if the absolute value of `bn` equals `w` and zero /// otherwise. @meta.RecordUse() -@ffi.Native, BN_ULONG)>( +@ffi.Native, ffi.UintPtr)>( symbol: 'bssl_dart_BN_abs_is_word', ) external int BN_abs_is_word( @@ -4132,7 +4132,7 @@ external int BN_add( /// BN_add_word adds `w` to `a`. It returns one on success and zero otherwise. @meta.RecordUse() -@ffi.Native, BN_ULONG)>( +@ffi.Native, ffi.UintPtr)>( symbol: 'bssl_dart_BN_add_word', ) external int BN_add_word( @@ -4339,7 +4339,7 @@ external int BN_cmp( /// BN_cmp_word is like `BN_cmp` except it takes its second argument as a /// `BN_ULONG` instead of a `BIGNUM`. @meta.RecordUse() -@ffi.Native, BN_ULONG)>( +@ffi.Native, ffi.UintPtr)>( symbol: 'bssl_dart_BN_cmp_word', ) external int BN_cmp_word( @@ -4419,7 +4419,7 @@ external int BN_div( /// BN_div_word sets `numerator` = `numerator`/`divisor` and returns the /// remainder or (BN_ULONG)-1 on error. @meta.RecordUse() -@ffi.Native, BN_ULONG)>( +@ffi.Native, ffi.UintPtr)>( symbol: 'bssl_dart_BN_div_word', ) external int BN_div_word( @@ -4664,7 +4664,7 @@ external int BN_get_u64( /// too large to be represented as a single word, the maximum possible value /// will be returned. @meta.RecordUse() -@ffi.Native)>( +@ffi.Native)>( symbol: 'bssl_dart_BN_get_word', ) external int BN_get_word( @@ -4793,7 +4793,7 @@ external int BN_is_prime_fasttest_ex( /// BN_is_word returns one if `bn` is exactly `w` and zero otherwise. @meta.RecordUse() -@ffi.Native, BN_ULONG)>( +@ffi.Native, ffi.UintPtr)>( symbol: 'bssl_dart_BN_is_word', ) external int BN_is_word( @@ -5030,7 +5030,7 @@ external int BN_mod_exp_mont_consttime( @ffi.Native< ffi.Int Function( ffi.Pointer, - BN_ULONG, + ffi.UintPtr, ffi.Pointer, ffi.Pointer, ffi.Pointer, @@ -5339,7 +5339,7 @@ external int BN_mod_sub_quick( /// BN_mod_word returns `a` mod `w` or (BN_ULONG)-1 on error. @meta.RecordUse() -@ffi.Native, BN_ULONG)>( +@ffi.Native, ffi.UintPtr)>( symbol: 'bssl_dart_BN_mod_word', ) external int BN_mod_word( @@ -5388,7 +5388,7 @@ external int BN_mul( /// BN_mul_word sets `bn` = `bn` * `w`. It returns one on success or zero on /// allocation failure. @meta.RecordUse() -@ffi.Native, BN_ULONG)>( +@ffi.Native, ffi.UintPtr)>( symbol: 'bssl_dart_BN_mul_word', ) external int BN_mul_word( @@ -5443,7 +5443,7 @@ external int BN_num_bits( ); @meta.RecordUse() -@ffi.Native( +@ffi.Native( symbol: 'bssl_dart_BN_num_bits_word', ) external int BN_num_bits_word( @@ -5599,7 +5599,7 @@ external int BN_rand_range( /// otherwise. @meta.RecordUse() @ffi.Native< - ffi.Int Function(ffi.Pointer, BN_ULONG, ffi.Pointer) + ffi.Int Function(ffi.Pointer, ffi.UintPtr, ffi.Pointer) >(symbol: 'bssl_dart_BN_rand_range_ex') external int BN_rand_range_ex( ffi.Pointer r, @@ -5671,7 +5671,7 @@ external int BN_set_u64( /// BN_set_word sets `bn` to `value`. It returns one on success or zero on /// allocation failure. @meta.RecordUse() -@ffi.Native, BN_ULONG)>( +@ffi.Native, ffi.UintPtr)>( symbol: 'bssl_dart_BN_set_word', ) external int BN_set_word( @@ -5733,7 +5733,7 @@ external int BN_sub( /// BN_sub_word subtracts `w` from `a`. It returns one on success and zero on /// allocation failure. @meta.RecordUse() -@ffi.Native, BN_ULONG)>( +@ffi.Native, ffi.UintPtr)>( symbol: 'bssl_dart_BN_sub_word', ) external int BN_sub_word( @@ -32451,8 +32451,6 @@ typedef BLAKE2B_CTX = blake2b_state_st; typedef BN_CTX = bignum_ctx; typedef BN_GENCB = bn_gencb_st; typedef BN_MONT_CTX = bn_mont_ctx_st; -typedef BN_ULONG = ffi.Uint64; -typedef DartBN_ULONG = int; typedef BUF_MEM = buf_mem_st; typedef CBB = cbb_st; typedef CBS = cbs_st; @@ -36622,7 +36620,7 @@ final class bignum_ctx extends ffi.Opaque {} final class bignum_st extends ffi.Struct { /// d is a pointer to an array of `width` `BN_BITS2`-bit chunks in /// little-endian order. This stores the absolute value of the number. - external ffi.Pointer d; + external ffi.Pointer d; /// width is the number of elements of `d` which are valid. This value is not /// necessarily minimal; the most-significant words of `d` may be zero. @@ -36655,7 +36653,7 @@ final class bignum_st extends ffi.Struct { static ffi.Pointer $allocate( ffi.Allocator $allocator, { - required ffi.Pointer d, + required ffi.Pointer d, required int width, required int dmax, required int neg, diff --git a/pkgs/boring/test/boring_test.dart b/pkgs/boring/test/boring_test.dart index 524fe840ca..62a2468485 100644 --- a/pkgs/boring/test/boring_test.dart +++ b/pkgs/boring/test/boring_test.dart @@ -17,6 +17,20 @@ void main() { expect(ssl.BORINGSSL_self_test(), equals(1)); }); + test('BN_ULONG is pointer-sized', () { + // BN_ULONG is uint32_t on 32-bit targets, where binding it as Uint64 + // passes and returns the wrong values. + expect(BN_ULONG, equals(ffi.UintPtr)); + final bn = ssl.BN_new(); + try { + expect(ssl.BN_set_word(bn, 65537), equals(1)); + expect(ssl.BN_get_word(bn), equals(65537)); + expect(ssl.BN_is_word(bn, 65537), equals(1)); + } finally { + ssl.BN_free(bn); + } + }); + test('opensslAllocator allocates, frees, and exposes nativeFree', () { using((arena) { final buf = arena(32); diff --git a/pkgs/boring/tool/ffigen.dart b/pkgs/boring/tool/ffigen.dart index 86e65095da..f4e790ffbd 100644 --- a/pkgs/boring/tool/ffigen.dart +++ b/pkgs/boring/tool/ffigen.dart @@ -52,6 +52,8 @@ const _vaListFunctions = { 'OPENSSL_vasprintf', }; +const _ffiImport = LibraryImport('ffi', 'dart:ffi'); + Future main() async { final packageRoot = Platform.script.resolve('../'); final includeDir = packageRoot.resolve('third_party/boringssl/src/include'); @@ -97,6 +99,12 @@ Future main() async { '-U__PRAGMA_REDEFINE_EXTNAME', ], ), + // ffigen runs on a 64-bit host, where BoringSSL's BN_ULONG is uint64_t, + // but it's uint32_t on 32-bit targets (see openssl/base.h). Map it to the + // pointer-sized UintPtr, which matches both. + importType: (declaration) => declaration.originalName == 'BN_ULONG' + ? ImportedType(_ffiImport, 'UintPtr', 'int', 'BN_ULONG') + : null, visitors: [ Visitor( func: (node) { From 56ca107e5545113288ac0f5a11eaed85357c8d4e Mon Sep 17 00:00:00 2001 From: Moritz Date: Tue, 17 Nov 2026 15:06:14 +0000 Subject: [PATCH 2/3] [boring] Compile with OPENSSL_SMALL and the sanitizer of the Dart runtime - Compile BoringSSL with OPENSSL_SMALL. Its smaller precomputed tables, mainly for P-256, are used by every application that uses ECDSA or ECDH, so tree-shaking can't remove them. For package:webcrypto, this makes the tree-shaken library on Linux x64 19% smaller (1.12 MB -> 0.91 MB), and P-256 signing about 2x slower (26 -> 54 us). AES-GCM is unaffected. - When the build config has a sanitizer, for example in `dart test -p vm-asan`, compile BoringSSL with it and Clang, like package:webcrypto's build hook did. The prebuilt libraries aren't instrumented, so the `fetch` build mode builds from source in that case. Add an `asan` CI job. - List BoringSSL's `.inc` files as dependencies of the `checkout` build, like google/webcrypto.dart#429. --- .github/workflows/boring.yaml | 17 +++++++++++++++++ pkgs/boring/CHANGELOG.md | 7 +++++++ pkgs/boring/README.md | 9 +++++++++ pkgs/boring/hook/build.dart | 32 ++++++++++++++++++++++++++++++-- pkgs/boring/src/CMakeLists.txt | 27 +++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 2 deletions(-) diff --git a/.github/workflows/boring.yaml b/.github/workflows/boring.yaml index 89a85f287d..b6154549f2 100644 --- a/.github/workflows/boring.yaml +++ b/.github/workflows/boring.yaml @@ -115,6 +115,23 @@ jobs: run: dart test if: always() && steps.install.outcome == 'success' + # Build BoringSSL with AddressSanitizer through the build hook, and run the + # tests on the Dart VM built with it. + asan: + needs: analyze + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + persist-credentials: false + - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d + with: + sdk: dev + - name: Install dependencies + run: dart pub get + - name: Run tests with AddressSanitizer + run: dart test -c cli -p vm-asan test/boring_test.dart + # Check the bundled BoringSSL and the bindings against Project Wycheproof. conformance: needs: analyze diff --git a/pkgs/boring/CHANGELOG.md b/pkgs/boring/CHANGELOG.md index 6930333a13..aadded4955 100644 --- a/pkgs/boring/CHANGELOG.md +++ b/pkgs/boring/CHANGELOG.md @@ -1,4 +1,11 @@ ## 0.4.3-wip +- 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. +- When the Dart runtime uses a sanitizer (`asan`, `msan`, or `tsan`), the + `fetch` and `checkout` build modes compile BoringSSL with it, using Clang. + The prebuilt libraries aren't instrumented, so `fetch` builds from source. +- The `checkout` build mode rebuilds when BoringSSL's `.inc` files change. - Fixed the bindings of the `BN_*_word` functions on 32-bit targets, such as Android arm. `BN_ULONG` is now `UintPtr` instead of `Uint64`, because it is `uint32_t` on 32-bit targets. diff --git a/pkgs/boring/README.md b/pkgs/boring/README.md index 6b1ac96a42..60780b9fda 100644 --- a/pkgs/boring/README.md +++ b/pkgs/boring/README.md @@ -66,6 +66,15 @@ 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 +sanitizer and Clang instead, because the prebuilt libraries aren't +instrumented. + +BoringSSL is compiled with `OPENSSL_SMALL`, which trades slower elliptic-curve +operations (for example about 2x slower P-256 signatures) for smaller +precomputed tables that tree-shaking can't remove. + ## Tree-Shaking When linking is enabled (`dart build`, and Flutter profile and release builds), diff --git a/pkgs/boring/hook/build.dart b/pkgs/boring/hook/build.dart index 3ef944e508..0971bfc0a9 100644 --- a/pkgs/boring/hook/build.dart +++ b/pkgs/boring/hook/build.dart @@ -31,7 +31,22 @@ Future main(List args) async { // the application uses from the static library. final static = input.config.linkingEnabled; - switch (buildOptions.buildMode) { + // The prebuilt libraries aren't built with a sanitizer, but all native + // code must be compiled with the sanitizer of the Dart runtime that loads + // it (for example `dart test -p vm-asan`), so build from source. + final sanitizer = input.config.code.sanitizer; + final buildMode = + sanitizer != null && buildOptions.buildMode == BuildModeEnum.fetch + ? BuildModeEnum.checkout + : buildOptions.buildMode; + if (buildMode != buildOptions.buildMode) { + stdout.writeln( + 'boring: building from source, because there are no prebuilt ' + 'libraries with the ${sanitizer!.name} sanitizer.', + ); + } + + switch (buildMode) { case BuildModeEnum.fetch: final library = await fetchPrebuiltLibrary(input, static: static); _addLibrary(input, output, library, static: static); @@ -106,9 +121,11 @@ Future _buildLocalCMake( final sourceDir = packageRoot.resolve('src/'); final targetOS = input.config.code.targetOS; final targetArch = input.config.code.targetArchitecture; + final sanitizer = input.config.code.sanitizer; stdout.writeln( - 'boring: building native asset with CMake for $targetOS-$targetArch.', + 'boring: building native asset with CMake for $targetOS-$targetArch' + '${sanitizer == null ? '' : ' with the ${sanitizer.name} sanitizer'}.', ); // Installs both the dynamic and the static library, see src/CMakeLists.txt. @@ -118,6 +135,14 @@ Future _buildLocalCMake( defines: { 'CMAKE_BUILD_TYPE': 'Release', 'CMAKE_INSTALL_PREFIX': installDir.toFilePath(), + // The Dart SDK's sanitizer runtimes come from Clang, so compile with + // Clang instead of the compiler of the build config's toolchain. + if (sanitizer != null) ...{ + 'CMAKE_TOOLCHAIN_FILE': '', + 'CMAKE_C_COMPILER': 'clang', + 'CMAKE_CXX_COMPILER': 'clang++', + 'BSSL_DART_SANITIZER': sanitizer.name, + }, }, targets: ['install'], parallelUseAllProcessors: true, @@ -147,6 +172,9 @@ final _buildDependencyExtensions = { '.cmake', '.cpp', '.h', + // BoringSSL includes `.cc.inc` and `.c.inc` files, for example in + // crypto/fipsmodule/. + '.inc', '.s', 'CMakeLists.txt', }; diff --git a/pkgs/boring/src/CMakeLists.txt b/pkgs/boring/src/CMakeLists.txt index bfcf60c90d..3c2ddaf072 100644 --- a/pkgs/boring/src/CMakeLists.txt +++ b/pkgs/boring/src/CMakeLists.txt @@ -15,6 +15,12 @@ include(../third_party/boringssl/sources.cmake) # Symbol prefixing: all BoringSSL symbols become bssl_dart_ add_definitions(-DBORINGSSL_PREFIX=bssl_dart) +# Smaller precomputed tables, mainly for P-256, in exchange for slower +# elliptic-curve operations. The P-256 tables are used whenever an application +# uses ECDSA or ECDH, so tree-shaking can't remove them: this makes the +# tree-shaken library of package:webcrypto about 19% smaller on Linux x64, and +# P-256 signing about 2x slower (tens of microseconds on desktop). +add_definitions(-DOPENSSL_SMALL) # Keep BoringSSL free of C++ runtime symbols (such as operator delete from # virtual destructors), since CMake and hook/link.dart (CLinker) link the # library with the C compiler driver. @@ -126,6 +132,27 @@ if(WIN32) add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE=0) endif() +# The sanitizer of the Dart runtime, set by hook/build.dart from the +# `sanitizer` of the build config. All native code must be compiled with the +# same sanitizer as the runtime that loads it. +if(DEFINED BSSL_DART_SANITIZER) + if(BSSL_DART_SANITIZER STREQUAL "asan") + set(BSSL_DART_SANITIZER_FLAG "-fsanitize=address") + elseif(BSSL_DART_SANITIZER STREQUAL "msan") + set(BSSL_DART_SANITIZER_FLAG "-fsanitize=memory") + elseif(BSSL_DART_SANITIZER STREQUAL "tsan") + set(BSSL_DART_SANITIZER_FLAG "-fsanitize=thread") + else() + message(FATAL_ERROR "Unsupported sanitizer: ${BSSL_DART_SANITIZER}") + endif() + set(CMAKE_C_FLAGS + "${CMAKE_C_FLAGS} ${BSSL_DART_SANITIZER_FLAG} -fno-omit-frame-pointer") + set(CMAKE_CXX_FLAGS + "${CMAKE_CXX_FLAGS} ${BSSL_DART_SANITIZER_FLAG} -fno-omit-frame-pointer") + set(CMAKE_SHARED_LINKER_FLAGS + "${CMAKE_SHARED_LINKER_FLAGS} ${BSSL_DART_SANITIZER_FLAG}") +endif() + # The sources are compiled once, and linked into both libraries: # * bssl_dart, a dynamic library with all of BoringSSL, bundled as is when # linking is disabled (for example in `dart run` and `dart test`). From 806435c3f0e222a080ff91af2331aa2dc95e9a54 Mon Sep 17 00:00:00 2001 From: Moritz Date: Tue, 17 Nov 2026 15:35:02 +0000 Subject: [PATCH 3/3] [boring] Document and check the glibc requirement of the prebuilt Linux libraries The prebuilt Linux libraries are built on Ubuntu 22.04, the oldest Ubuntu that GitHub Actions has, so they require glibc 2.34, for the pthread functions that moved into libc in 2.34. The README didn't say so, and the libraries are picked by OS and architecture only, so they failed to load on older distributions and on other C libraries, such as musl on Alpine Linux. - List the requirement in the README. - 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.34, instead of bundling a library that fails to load. - Tighten the ABI check of the binaries workflow from 2.35 to 2.34, what the libraries need today, so the requirement can't silently grow. --- .github/workflows/boring_binaries.yaml | 8 +-- pkgs/boring/CHANGELOG.md | 5 ++ pkgs/boring/README.md | 6 +++ pkgs/boring/lib/src/hook_helpers/fetch.dart | 13 +++++ pkgs/boring/lib/src/hook_helpers/libc.dart | 58 +++++++++++++++++++++ pkgs/boring/test/hook_helpers_test.dart | 36 +++++++++++++ 6 files changed, 123 insertions(+), 3 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..33a220fdf6 100644 --- a/.github/workflows/boring_binaries.yaml +++ b/.github/workflows/boring_binaries.yaml @@ -61,15 +61,17 @@ jobs: # tool/precompile_binaries.dart. include: # The oldest available Ubuntu, so the libraries only require glibc - # 2.35. + # 2.34, for the pthread functions that moved into libc in 2.34. Keep + # `max-glibc` in sync with `prebuiltLinuxGlibc` in + # lib/src/hook_helpers/libc.dart. - os: ubuntu-22.04 target-os: linux target-arch: x64 - max-glibc: '2.35' + max-glibc: '2.34' - os: ubuntu-22.04-arm target-os: linux target-arch: arm64 - max-glibc: '2.35' + max-glibc: '2.34' - os: macos-15 target-os: macos target-arch: arm64 diff --git a/pkgs/boring/CHANGELOG.md b/pkgs/boring/CHANGELOG.md index aadded4955..0a02ccb22e 100644 --- a/pkgs/boring/CHANGELOG.md +++ b/pkgs/boring/CHANGELOG.md @@ -1,4 +1,9 @@ ## 0.4.3-wip +- On Linux, the `fetch` build mode checks that the machine has glibc 2.34 or + later, which the prebuilt libraries require, and otherwise fails with a hint + to use `buildMode: checkout`, instead of bundling a library that fails to + load. This includes distributions with another C library, such as musl on + Alpine Linux. The README now lists the requirement. - 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..82b071d0a6 100644 --- a/pkgs/boring/README.md +++ b/pkgs/boring/README.md @@ -66,6 +66,12 @@ 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. +The prebuilt Linux libraries require glibc 2.34 or later, for example Ubuntu +22.04, Debian 12, RHEL 9, Fedora 35, or Amazon Linux 2023. On older +distributions, and on distributions with another C library, such as musl on +Alpine Linux, the `fetch` build mode fails with a hint to use `buildMode: +checkout` instead, which compiles BoringSSL for the C library of the machine. + 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 sanitizer and Clang instead, because the prebuilt libraries aren't 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..25ff6785d1 --- /dev/null +++ b/pkgs/boring/lib/src/hook_helpers/libc.dart @@ -0,0 +1,58 @@ +// 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. +/// +/// They're built on the oldest Ubuntu that GitHub Actions has, 22.04, and the +/// pthread functions they use moved into libc in glibc 2.34. Keep in sync with +/// `max-glibc` of the Linux targets in `.github/workflows/boring_binaries.yaml`, +/// which checks the libraries, and with README.md. +const prebuiltLinuxGlibc = (2, 34); + +/// 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()