-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add Base64Fuzzer to the repo #103011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Base64Fuzzer to the repo #103011
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/Base64Fuzzer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
|
|
||
| using System.Buffers; | ||
| using System.Buffers.Text; | ||
|
|
||
| namespace DotnetFuzzing.Fuzzers | ||
| { | ||
| internal class Base64Fuzzer : IFuzzer | ||
| { | ||
| public string[] TargetAssemblies => []; | ||
|
|
||
| public string[] TargetCoreLibPrefixes => ["System.Buffers.Text"]; | ||
|
|
||
| public void FuzzTarget(ReadOnlySpan<byte> bytes) | ||
| { | ||
| using PooledBoundedMemory<byte> inputPoisoned = PooledBoundedMemory<byte>.Rent(bytes, PoisonPagePlacement.After); | ||
| Span<byte> input = inputPoisoned.Span; | ||
| int maxEncodedLength = Base64.GetMaxEncodedToUtf8Length(bytes.Length); | ||
| using PooledBoundedMemory<byte> destPoisoned = PooledBoundedMemory<byte>.Rent(maxEncodedLength, PoisonPagePlacement.After); | ||
| Span<byte> encoderDest = destPoisoned.Span; | ||
|
|
||
| { // IsFinalBlock = true | ||
| OperationStatus status = Base64.EncodeToUtf8(input, encoderDest, out int bytesConsumed, out int bytesEncoded); | ||
|
|
||
| Assert.Equal(OperationStatus.Done, status); | ||
| Assert.Equal(bytes.Length, bytesConsumed); | ||
| Assert.Equal(true, maxEncodedLength >= bytesEncoded); | ||
|
|
||
| using PooledBoundedMemory<byte> decoderDestPoisoned = PooledBoundedMemory<byte>.Rent(Base64.GetMaxDecodedFromUtf8Length(bytesEncoded), PoisonPagePlacement.After); | ||
| status = Base64.DecodeFromUtf8(encoderDest.Slice(0, bytesEncoded), decoderDestPoisoned.Span, out int bytesRead, out int bytesDecoded); | ||
|
|
||
| Assert.Equal(OperationStatus.Done, status); | ||
| Assert.Equal(bytes.Length, bytesDecoded); | ||
| Assert.Equal(bytesEncoded, bytesRead); | ||
| Assert.SequenceEqual(bytes, decoderDestPoisoned.Span.Slice(0, bytesDecoded)); | ||
| } | ||
|
|
||
| { // IsFinalBlock = false | ||
| encoderDest.Clear(); | ||
| OperationStatus status = Base64.EncodeToUtf8(input, encoderDest, out int bytesConsumed, out int bytesEncoded, isFinalBlock: false); | ||
| Span<byte> decodeInput = encoderDest.Slice(0, bytesEncoded); | ||
| using PooledBoundedMemory<byte> decoderDestPoisoned = PooledBoundedMemory<byte>.Rent(Base64.GetMaxDecodedFromUtf8Length(bytesEncoded), PoisonPagePlacement.After); | ||
| Span<byte> decoderDest = decoderDestPoisoned.Span; | ||
|
|
||
| if (bytes.Length % 3 == 0) | ||
| { | ||
| Assert.Equal(OperationStatus.Done, status); | ||
| Assert.Equal(bytes.Length, bytesConsumed); | ||
| Assert.Equal(true, maxEncodedLength >= bytesEncoded); | ||
|
|
||
|
|
||
| status = Base64.DecodeFromUtf8(decodeInput, decoderDest, out int bytesRead, out int bytesDecoded, isFinalBlock: false); | ||
|
|
||
| Assert.Equal(OperationStatus.Done, status); | ||
| Assert.Equal(bytes.Length, bytesDecoded); | ||
| Assert.Equal(bytesEncoded, bytesRead); | ||
| Assert.SequenceEqual(bytes, decoderDest.Slice(0, bytesDecoded)); | ||
| } | ||
| else | ||
| { | ||
| Assert.Equal(OperationStatus.NeedMoreData, status); | ||
| Assert.Equal(true, maxEncodedLength >= bytesEncoded); | ||
|
|
||
| status = Base64.DecodeFromUtf8(decodeInput, decoderDest, out int bytesRead, out int bytesDecoded, isFinalBlock: false); | ||
|
|
||
| if (decodeInput.Length % 4 == 0) | ||
| { | ||
| Assert.Equal(OperationStatus.Done, status); | ||
| Assert.Equal(bytesConsumed, bytesDecoded); | ||
| Assert.Equal(bytesEncoded, bytesRead); | ||
| } | ||
| else | ||
| { | ||
| Assert.Equal(OperationStatus.NeedMoreData, status); | ||
| Assert.SequenceEqual(bytes.Slice(0, bytesDecoded), decoderDest.Slice(0, bytesDecoded)); | ||
buyaa-n marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| { // Encode / decode in place | ||
| encoderDest.Clear(); | ||
| input.CopyTo(encoderDest); | ||
| OperationStatus status = Base64.EncodeToUtf8InPlace(encoderDest, input.Length, out int bytesEncoded); | ||
|
|
||
| Assert.Equal(OperationStatus.Done, status); | ||
| Assert.Equal(true, maxEncodedLength >= bytesEncoded); | ||
|
|
||
| status = Base64.DecodeFromUtf8InPlace(encoderDest.Slice(0, bytesEncoded), out int bytesDecoded); | ||
|
|
||
| Assert.Equal(OperationStatus.Done, status); | ||
| Assert.Equal(bytes.Length, bytesDecoded); | ||
| Assert.SequenceEqual(bytes, encoderDest.Slice(0, bytesDecoded)); | ||
| } | ||
|
|
||
| { // Run IsValid, Decode overloads with the random input values | ||
| encoderDest.Clear(); | ||
| Base64.IsValid(input); | ||
| Base64.DecodeFromUtf8(input, encoderDest, out _, out _); | ||
| Base64.DecodeFromUtf8InPlace(input, out _); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.