Skip to content

fix(ffi): clear BoringSSL errors left by other code before each operation - #474

Closed
mosuem wants to merge 10 commits into
google:masterfrom
mosuem:clear-boringssl-errors
Closed

mosuem wants to merge 10 commits into
google:masterfrom
mosuem:clear-boringssl-errors

Conversation

@mosuem

@mosuem mosuem commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Note

This PR was generated by an AI coding agent (Jetski) on behalf of @mosuem.

Important

Stacked on #417 (switch to package:boring). Until #417 lands, this diff also shows its commits; only the last commit, fix(ffi): clear BoringSSL errors left by other code before each operation, belongs to this PR.

With package:boring, every package using BoringSSL on a thread shares one error queue, whereas webcrypto's own copy of BoringSSL had a separate one. An error that another package leaves behind is then reported as the message of webcrypto's next failure, because extractBoringSslError() reports the least recent (most specific) error on the queue.

For example, after another package left an EC UNKNOWN_GROUP error on the queue:

RsaOaepPrivateKey.importPkcs8Key([1, 2, 3], Hash.sha256)
FormatException: error:0f00007b:elliptic curve routines:OPENSSL_internal:UNKNOWN_GROUP

instead of ...:public key routines:OPENSSL_internal:DECODE_ERROR.

Change

  • Add _arena / _arenaStream, which clear the queue and then run BoringArena.using / BoringArena.stream, and use them for all 31 BoringSSL call sites in lib/src/impl_ffi/. This follows the advice in package:boring's README ("Call ERR_clear_error() before a call whose errors you report").
  • No other isolate can run on the thread until the operation awaits, and webcrypto already consumes errors right after each failing call. So this covers every synchronous call.
  • Results don't change, because success and failure come from return values. Only error messages could be wrong.
  • test/ffi/stale_boringssl_errors_test.dart leaves a stale error, then checks that RSA and EC import failures report their own errors and leave the queue empty. It fails without the fix.

All 1585 VM tests pass locally.

Replaces the internal `_Scope` arena with `BoringArena` from package:boring:

- `_Scope.sync` / `_Scope.async` -> `BoringArena.run`, which releases after a
  returned Future completes (the old `assert(T is! Future)` guard was a
  no-op), and `_Scope.stream` -> `BoringArena.stream`.
- `dataAsPointer` -> `copyBytes`, `createCBS` / `createCBB` -> `cbs` / `cbb`,
  `CBB.copy()` -> `CBB.toBytes()`, and `defer(() => X_free(p))` ->
  `using(p, X_free)`. `move` is unchanged.
- Keeps a small `create()` extension that maps `nullptr` to OperationError.
- `checkErrorStack` uses `extractBoringSslError()`, which removes the last
  use of package:ffi, so the dependency is dropped.
- Remove the update-boringssl workflow. It ran
  tool/bump-boringssl-revision.sh and read tool/REVISION, which this PR
  deletes along with the vendored BoringSSL. BoringSSL bumps now happen in
  package:boring, and Dependabot's pub config picks up boring releases.
- Declare boring as a hosted `^0.4.0` dependency and pin the unreleased
  commit with dependency_overrides, fixing the invalid_dependency warning
  that failed the lints job. The example app needs the same override
  because overrides don't propagate through its path dependency.
- Bump the pin to 60072877, whose CI runs the Wycheproof and x509-limbo
  conformance suites. lib/, hook/ and src/ are unchanged since 410fae8e.
The TODO in checkErrorStack proposed clearing BoringSSL errors in every
finally via BoringArena and asserting that none are left. That doesn't
work in Dart: the error queue is thread-local and an isolate may resume
on a different OS thread after an await, so a clear or assert at arena
release can miss errors or see another isolate's.

Errors are already consumed right after each failing call (_checkOp,
_checkData, and the explicit clear in _verifyStream), and checkErrorStack
already fails tests that leave errors behind. Replace both TODOs with
comments saying so, and state the rule once above _checkOp.
0.4.2 renamed the BoringArena API:
- static `BoringArena.run` -> `BoringArena.using`
- instance `using(resource, release)` -> `register(resource, release)`

Also update the README's system dependencies: package:boring downloads
prebuilt BoringSSL by default, so CMake is no longer needed, and update the
example app's pubspec.lock.

boring 0.4.2 requires Dart ^3.13.3. The SDK constraint stays at ^3.10.0,
because raising it changes the formatter's language version and would
reformat 25 unrelated files. Pub enforces the effective minimum via boring.
Removing ffigen dropped the transitive constraint that kept coverage at
1.15.1. coverage 1.16.0's format_coverage rejects the Firefox coverage JSON
written by package:test, which fails the Linux desktop job.
…tion

With package:boring, every package using BoringSSL on a thread shares its
error queue, while webcrypto's own copy of BoringSSL had a separate one. An
error that another package left behind was reported as the message of
webcrypto's next failure, because extractBoringSslError() reports the least
recent (most specific) error. For example, a leftover EC UNKNOWN_GROUP error
replaced the DECODE_ERROR of a malformed RSA PKCS#8 key.

Run all BoringSSL calls in _arena / _arenaStream, which clear the queue
before the operation, like package:boring's README recommends. No other
isolate runs on the thread until the operation awaits, and errors are already
consumed right after each failing call, so this covers every synchronous
call. Results don't change: only error messages could be wrong.
@mosuem

mosuem commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

Replaced by #476 (rebased onto the latest use-package-boring and stacked on #475 via gh stack, stack #477).

@mosuem mosuem closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant