Skip to content

Data Corruption in System.Buffers.Text.Base64.EncodeToUtf8 #118143

@jeffhandley

Description

@jeffhandley

We started observing intermittent CI failures on tests involving Base64 encoding in late June. This is recognized as a high-risk potential regression lurking in the .NET 10 release, and we need to find the root cause of this in time to resolve it before the .NET 10 RC1 release.

Here are the CI failures associated with this root cause:

  1. Cryptography.Tests: No PEM encoded data found #117163
    2. WasmTestOnChrome-ST-System.Memory.Tests errors #117169
  2. System.Xml.Tests failing #117614
  3. System.Security.Cryptography.Rsa.Tests.RSAXml Test Suite Fails with Strings Differ/Containers Differ #117665

Both #117163 and #117169 were reported on June 30, which is the earliest recognized signal of this issue. As was noted in #117665:

That seems... bad?

-f6 ba 82 83 26 0c 39 91 1b 8b 5d b1 8a 0f f3 6a 78 d5 59 a8 0d 64 29 3d d0 0c 35 87 56 00 ...
+f4 0a 80 80 06 00 38 01 00 88 0d 80 88 0f c0 68 08 c0 58 08 00 64 09 00 d0 0c 00 84 06 00 ...
It's like something is clearing every other 6 bits?

  • F4BA XOR F40A is 02B0: 0b0000_0010_1011_0000
  • 8B5D XOR 880D is 0350: 0b0000_0011_0101_0000

Since base64 is a 6-bit map... I wonder if this is a base64 encoding problem.

This is happening across multiple architectures and operating systems, as well as across different physical machines.

@eiriktsarpalis conducted a preliminary investigation, reporting:

  • The failing tests seem to all share a call to System.Buffers.Text.Base64.EncodeToUtf8, with that method producing corrupted data
  • Some of the tests exercise the API through the System.Convert APIs, which only use the Base64 class implementation if vectorization is available and applicable
    if (Vector128.IsHardwareAccelerated && !insertLineBreaks && bytes.Length >= Base64VectorizationLengthThreshold)
    {
    ToBase64CharsLargeNoLineBreaks(bytes, chars, charLengthRequired);
    }
  • The relevant implementation in System/Buffers/Text/Base64Helper/Base64EncoderHelper.cs hasn't been updated since October 2024

/cc @artl93 @jeffschwMSFT @marek-safar @lewing for visibility/awareness

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions