Skip to content

fuzz: add binary_length_from_base64 coverage to base64 fuzzer - #987

Merged
lemire merged 3 commits into
simdutf:masterfrom
aryanputta:fix/fuzzer-binary-length-from-base64
Jun 15, 2026
Merged

lemire merged 3 commits into
simdutf:masterfrom
aryanputta:fix/fuzzer-binary-length-from-base64

Conversation

@aryanputta

Copy link
Copy Markdown
Contributor

What

Adds binary_length_from_base64() calls to the base64 fuzzer
(fuzz/base64.cpp), closing the coverage gap identified in #981.

Why this matters

The double-count bug fixed in #978 (char16_t vectorised path counted
each 16-bit lane twice, returning ~2× the correct length) would have
been caught earlier if the fuzzer had called binary_length_from_base64.
@pauldreik confirmed the gap by inserting std::abort() and running
the fuzzers without a hit.

Changes

fuzz/base64.cpp — two additions to the decode<T>() template:

  1. Call impl->binary_length_from_base64(base64.data(), base64.size())
    after maximal_binary_length_from_base64 and assert
    binarylength <= maxbinarylength (exact ≤ upper bound must always hold).

  2. On SUCCESS, assert binarylength == convertresult.count (the exact
    predicted length must equal the number of bytes actually written).

  3. Add binarylength to decoderesult so the defaulted operator<=>
    catches divergence across implementations, and include it in the
    diagnostic dump on mismatch.

These assertions exercise both the char and char16_t instantiations
because decode<char> and decode<char16_t> are both called from
LLVMFuzzerTestOneInput.

Fixes #981.

Comment thread include/simdutf/implementation.h Outdated
} else
#endif
{
// For short inputs the scalar path (which this function can inline) is

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't mix the new tests in the fuzzer with code changes.

The changes to the fuzzer are an easy merge, but not so of other changes to the lib.

@aryanputta aryanputta Jun 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I dropped the include/simdutf/implementation.h commit entirely and force-pushed the PR now only extends the fuzzer (fuzz/base64.cpp). The unrelated short-input scalar fallback change is gone; I will open it as a separate PR if it is worth pursuing.

I rebuilt and ran the base64 fuzz target locally on a handful of inputs to confirm the two new assertions (binary_length <= maximal_binary_length and, on SUCCESS, binary_length == count) hold.

@lemire lemire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR should only extend the fuzzer.

The base64 fuzzers previously called only maximal_binary_length_from_base64(),
leaving binary_length_from_base64() unexercised by the fuzzer corpus.
This is the gap identified in simdutf#981: @pauldreik confirmed it by adding
std::abort() to the function path and running the fuzzers without a hit.

The double-count bug fixed in simdutf#978 (char16_t vectorised path counted each
16-bit lane twice) would have been found earlier with this coverage.

Changes to fuzz/base64.cpp:
- Add binarylength field to decoderesult (alongside maxbinarylength).
- In each implementation loop, call binary_length_from_base64() and
  assert two properties:
    1. binarylength <= maxbinarylength (invariant: exact <= upper bound)
    2. On SUCCESS, binarylength == convertresult.count (exact length
       matches the number of bytes actually written by base64_to_binary)
- Include binarylength in the cross-implementation comparison via the
  defaulted operator<=> so any divergence between implementations aborts.
- Add binarylength to the diagnostic dump on mismatch.

These assertions cover both the char and char16_t template instantiations
because decode<char> and decode<char16_t> are both exercised by
LLVMFuzzerTestOneInput.

Fixes simdutf#981.
@aryanputta
aryanputta force-pushed the fix/fuzzer-binary-length-from-base64 branch from 9be3413 to 40401dc Compare June 6, 2026 00:03
@pauldreik

Copy link
Copy Markdown
Collaborator

thanks for the PR!

the modified fuzzer uncovers an implementation difference. we need to look into that before merging.

I plan to submit a test later today.

@aryanputta

Copy link
Copy Markdown
Contributor Author

Thanks Paul, glad it caught something. That was the intent of the equality check: on SUCCESS, binary_length_from_base64 should match the decoded count, so a mismatch points at a real discrepancy rather than a fuzzer artifact. My guess is the char16_t double count path from #978, which is also the #981 coverage gap.

Happy to wait for your test. I will keep this PR to the fuzzer only so it stays an easy review, and I can rebase it behind your fix whenever it lands so CI stays green. Let me know if you want me to adjust the assertions in the meantime.

@aryanputta

Copy link
Copy Markdown
Contributor Author

Correction to my last message: #978 already landed (count /= 2 in src/generic/base64lengths.h is in master), so it is not that.

The difference looks like last chunk handling, and by design rather than a decode bug. binary_length_from_base64 takes no last_chunk option, so it only equals the decoded size under loose. The fuzzer also runs stop_before_partial, which per the enum contract ignores a partial final chunk and still returns SUCCESS, so it writes fewer bytes than the estimate. Minimal repro: "QQQ" with stop_before_partial gives length 2 but decodes 0 on SUCCESS.

If you agree, I will guard the equality check to last_chunk_option == loose, keeping this fuzzer only.

binary_length_from_base64 takes no last_chunk_handling option, so its
estimate only equals the decoded size under loose handling. Under strict
and stop_before_partial a partial final chunk is dropped, so a SUCCESS
result can legitimately write fewer bytes than the estimate. The fuzzer
found this: "QQQ" with stop_before_partial reports length 2 but decodes
0 on SUCCESS, tripping the assertion.

Guard the equality assertion to loose so the fuzzer no longer aborts on
this by-design case. The upper-bound check (binarylength <= maxbinarylength)
still runs for every last_chunk option.
@aryanputta

Copy link
Copy Markdown
Contributor Author

Pushed the guard I described above. The CIFuzz failure was the equality assertion, not a decode bug: binary_length_from_base64 takes no last_chunk option, so its estimate only matches the decoded count under loose handling. Under stop_before_partial a partial final chunk is dropped on SUCCESS, so it writes fewer bytes than the estimate ("QQQ" reports 2, decodes 0).

The equality check is now gated to last_chunk_handling_options::loose. The upper-bound check (binarylength <= maxbinarylength) still runs for every option. Verified locally: the pre-fix binary aborts on the "QQQ" + stop_before_partial input (binary_length_from_base64 (2) != decoded count (0)), the fixed binary exits clean. PR stays fuzzer-only.

binary_length_from_base64 may legitimately return different estimates
across implementations for invalid input (e.g. haswell vs westmere),
so storing it in decoderesult made the defaulted operator<=> abort on
the existing cross-implementation consistency check. Compute it as a
local for the per-impl upper-bound and loose-equality invariants only.
@aryanputta

Copy link
Copy Markdown
Contributor Author

Fixed the Fuzzing failure. The crash was self-inflicted: I had added binarylength as a field of decoderesult, whose defaulted operator<=> drives the cross-implementation consistency check. Since binary_length_from_base64 can legitimately return different estimates across kernels for invalid input (haswell reported 16 vs westmere/fallback 18 on the crashing case), that check aborted.

binarylength is now a local used only for the per-implementation invariants (upper bound against maximal_binary_length_from_base64, and equality with the decoded count under loose+SUCCESS). The scope is still fuzzer-only as requested.

@aryanputta

Copy link
Copy Markdown
Contributor Author

Friendly nudge: CI is green again after the cross-implementation fix, and the PR remains fuzzer-only as requested (only fuzz/base64.cpp is touched). @lemire the earlier 'should only extend the fuzzer' concern is addressed — would you mind re-reviewing or dismissing the stale change request so this can land? Happy to rebase if useful.

@lemire
lemire self-requested a review June 15, 2026 19:54

@lemire lemire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us merge once the tests go green.

@lemire
lemire merged commit e232915 into simdutf:master Jun 15, 2026
73 of 74 checks passed
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
simdutf 9.1.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed
* scripts/add_function.py: fixes by @BenjaminBucher in simdutf/simdutf#971
* workaround missing std::unreachable by @pauldreik in simdutf/simdutf#966
* update to cmake 3.18 everywhere by @pauldreik in simdutf/simdutf#967
* Remove redundant checks in scalar utf8 validation by @BenjaminBucher in simdutf/simdutf#970
* test: strengthen conversion result coverage by @parasol-aser in simdutf/simdutf#972
* add test where rewind causes integer underflow by @BenjaminBucher in simdutf/simdutf#975
* icelake: use unsigned shift base in utf16_to_latin1 tail mask by @metsw24-max in simdutf/simdutf#976
* icelake: use unsigned shift base in latin1_to_utf16 tail mask by @metsw24-max in simdutf/simdutf#977
* fix doubled length in binary_length_from_base64 for char16 input by @metsw24-max in simdutf/simdutf#978
* removing <algorithm> header by @lemire in simdutf/simdutf#979
* scripts/add_function.py: Better #if detection by @BenjaminBucher in simdutf/simdutf#985
* fixing issue 989 by @lemire in simdutf/simdutf#991
* fuzz: add binary_length_from_base64 coverage to base64 fuzzer by @aryanputta in simdutf/simdutf#987
* upgrading our 'unused' macros + silence silly warnings on 3 functions by @lemire in simdutf/simdutf#993
* Fix some out-of-bounds warnings from clang 23 by @erikcorry in simdutf/simdutf#995
* using __builtin_popcountll by @lemire in simdutf/simdutf#997
* add missing includes by @pauldreik in simdutf/simdutf#998
* Add RVV vectorized base64 encoding by @rajeshgangam in simdutf/simdutf#996
* arm64/base64: reuse scalar base64 tables for encoding by @tantei3 in simdutf/simdutf#1000
* arm64/base64: optimize insert_line_feed16 by @tantei3 in simdutf/simdutf#1001
* icelake: use unsigned shift base in all remaining tail/mask expressions by @lemire in simdutf/simdutf#1004
* tools/fastbase64: fix size_t -> int conversion warning by @lemire in simdutf/simdutf#1005
* potential fix for vs issue by @lemire in simdutf/simdutf#1006
* adding optimization from PR 1001 to westmere and haswell by @lemire in simdutf/simdutf#1003
* include what you use - std::abort by @pauldreik in simdutf/simdutf#1007
* add missing include for std::memcpy by @pauldreik in simdutf/simdutf#1009
* Avoid including algorithm just for std::min by @pauldreik in simdutf/simdutf#1010
* optimize the utf16 to utf8 transcoding. by @lemire in simdutf/simdutf#1011
* icelake/base64: optimized newline encoding for line_len < 64 by @tantei3 in simdutf/simdutf#1012
* arm64: test comparison masks with shrn + fcmp instead of a reduction by @lemire in simdutf/simdutf#1013
* icelake/base64: Add SIMD tail for binary_length_from_base64 by @tantei3 in simdutf/simdutf#1015
* [doc] document the cost of _safe variants by @lemire in simdutf/simdutf#1020
* Add convert_utf16_to_utf8_with_replacement to the C API by @lemire in simdutf/simdutf#1021
* utf16_to_utf8: report bytes written on error, drop the utf8_length re-walk by @lemire in simdutf/simdutf#1022

## New Contributors
* @BenjaminBucher made their first contribution in simdutf/simdutf#971
* @parasol-aser made their first contribution in simdutf/simdutf#972
* @metsw24-max made their first contribution in simdutf/simdutf#976
* @aryanputta made their first contribution in simdutf/simdutf#987
* @rajeshgangam made their first contribution in simdutf/simdutf#996

**Full Changelog**: https://github.com/simdutf/simdutf/compare/v9.0.0...v9.1.0</pre>
  <p>View the full release notes at <a href="https://github.com/simdutf/simdutf/releases/tag/v9.1.0">https://github.com/simdutf/simdutf/releases/tag/v9.1.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!17087
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.

Ensure fuzzer coverage for binary_length_from_base64()

3 participants