| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | namespace DotnetFuzzing.Fuzzers; |
| | | 9 | | |
| | | 10 | | // The fuzzing infrastructure currently does not support fuzzing .NET Framework. |
| | | 11 | | // However, this test class, while running under .NET Core, was used to foward the fuzzing |
| | | 12 | | // input to a .NET Framework console app. That app had the same test semantics as the tests |
| | | 13 | | // here, although used slightly different supporting APIs since not all supporting library |
| | | 14 | | // and language features are present in .NET Framework. |
| | | 15 | | // This fowarding approach and .NET Framework test code is presevered in the original Pull |
| | | 16 | | // Request for this file. The approach used Base64 encoding to convert the incoming |
| | | 17 | | // ReadOnlySpan<byte> to a string which was then passed to the Main() method of the .NET |
| | | 18 | | // Framework app which was then converted back to bytes before being passed to the .NET |
| | | 19 | | // Framework fuzzing tests. |
| | | 20 | | internal sealed class TextEncodingFuzzer : IFuzzer |
| | | 21 | | { |
| | 0 | 22 | | string[] IFuzzer.TargetAssemblies => []; |
| | 1 | 23 | | string[] IFuzzer.TargetCoreLibPrefixes { get; } = ["System.Text"]; |
| | | 24 | | |
| | | 25 | | void IFuzzer.FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 0 | 26 | | { |
| | 0 | 27 | | using PooledBoundedMemory<byte> poisonAfter = PooledBoundedMemory<byte>.Rent(bytes, PoisonPagePlacement.After); |
| | | 28 | | |
| | 0 | 29 | | TestLatin1(poisonAfter.Span); |
| | 0 | 30 | | TestASCII(poisonAfter.Span); |
| | 0 | 31 | | TestUnicode(poisonAfter.Span); |
| | 0 | 32 | | TestUtf32(poisonAfter.Span); |
| | 0 | 33 | | TestUtf7(poisonAfter.Span); |
| | 0 | 34 | | TestUtf8(poisonAfter.Span); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | // We use individual methods for each encoding, so if there's an exception then |
| | | 38 | | // it's clear which encoding failed based on the call stack. |
| | | 39 | | |
| | | 40 | | private static void TestLatin1(ReadOnlySpan<byte> input) |
| | 0 | 41 | | { |
| | 0 | 42 | | TestWithSubstitution(input, Encoding.GetEncoding("ISO-8859-1")); |
| | 0 | 43 | | TestWithConvert(input, Encoding.GetEncoding("ISO-8859-1")); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | private static void TestASCII(ReadOnlySpan<byte> input) |
| | 0 | 47 | | { |
| | 0 | 48 | | TestWithSubstitution(input, new ASCIIEncoding()); |
| | 0 | 49 | | TestWithConvert(input, new ASCIIEncoding()); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | private static void TestUnicode(ReadOnlySpan<byte> input) |
| | 0 | 53 | | { |
| | 0 | 54 | | TestWithSubstitution(input, new UnicodeEncoding()); |
| | 0 | 55 | | TestWithExceptions(input, new UnicodeEncoding(bigEndian: false, byteOrderMark: false, throwOnInvalidBytes: true) |
| | 0 | 56 | | TestWithConvert(input, new UnicodeEncoding()); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | private static void TestUtf32(ReadOnlySpan<byte> input) |
| | 0 | 60 | | { |
| | 0 | 61 | | TestWithSubstitution(input, new UTF32Encoding()); |
| | 0 | 62 | | TestWithExceptions(input, new UTF32Encoding(bigEndian: false, byteOrderMark: false, throwOnInvalidCharacters: tr |
| | 0 | 63 | | TestWithConvert(input, new UTF32Encoding()); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | private static void TestUtf7(ReadOnlySpan<byte> input) |
| | 0 | 67 | | { |
| | | 68 | | #pragma warning disable SYSLIB0001 // Type or member is obsolete |
| | 0 | 69 | | TestWithSubstitution(input, new UTF7Encoding()); |
| | | 70 | | #pragma warning restore SYSLIB0001 |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | private static void TestUtf8(ReadOnlySpan<byte> input) |
| | 0 | 74 | | { |
| | 0 | 75 | | TestWithSubstitution(input, new UTF8Encoding()); |
| | 0 | 76 | | TestWithExceptions(input, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true)); |
| | 0 | 77 | | TestWithConvert(input, new UTF8Encoding()); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | private static void TestWithSubstitution(ReadOnlySpan<byte> input, Encoding encoding) |
| | 0 | 81 | | { |
| | 0 | 82 | | Decoder decoder = encoding.GetDecoder(); |
| | 0 | 83 | | int charCount = decoder.GetCharCount(input, flush: true); |
| | | 84 | | |
| | 0 | 85 | | using PooledBoundedMemory<char> chars = PooledBoundedMemory<char>.Rent(charCount, PoisonPagePlacement.After); |
| | 0 | 86 | | using PooledBoundedMemory<char> chars2 = PooledBoundedMemory<char>.Rent(charCount, PoisonPagePlacement.After); |
| | | 87 | | |
| | | 88 | | // *4 for worst case scenario (*2 for char->byte + *2 for encoding) |
| | | 89 | | // +2 is for possible Base64 padding with UTF7Encoding. |
| | 0 | 90 | | using PooledBoundedMemory<byte> bytes = PooledBoundedMemory<byte>.Rent(charCount * 4 + 2, PoisonPagePlacement.Af |
| | | 91 | | |
| | 0 | 92 | | decoder.Reset(); |
| | 0 | 93 | | int written = decoder.GetChars(input, chars.Span, flush: true); |
| | 0 | 94 | | Assert.Equal(charCount, written); |
| | | 95 | | |
| | 0 | 96 | | Encoder encoder = encoding.GetEncoder(); |
| | | 97 | | // We use flush:true here for UTF7Encoding which may do Base64 padding at the end. |
| | 0 | 98 | | int bytesWritten = encoder.GetBytes(chars.Span, bytes.Span, flush: true); |
| | | 99 | | |
| | | 100 | | // Decode the encoded values. Any substitutions will be comparable now. |
| | 0 | 101 | | decoder.Reset(); |
| | 0 | 102 | | written = decoder.GetChars(bytes.Span.Slice(0, bytesWritten), chars2.Span, flush: true); |
| | 0 | 103 | | Assert.Equal(charCount, written); |
| | | 104 | | |
| | | 105 | | // Verify that we round-tripped the values. |
| | 0 | 106 | | Assert.SequenceEqual<char>(chars.Span, chars2.Span); |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | // If there are substitutions, these cases will fail with DecoderFallbackException early on, |
| | | 110 | | // otherwise there should be no DecoderFallbackExceptions. |
| | | 111 | | private static void TestWithExceptions(ReadOnlySpan<byte> input, Encoding encoding) |
| | 0 | 112 | | { |
| | 0 | 113 | | Assert.Equal(typeof(DecoderExceptionFallback), encoding.DecoderFallback.GetType()); |
| | 0 | 114 | | Assert.Equal(typeof(EncoderExceptionFallback), encoding.EncoderFallback.GetType()); |
| | | 115 | | |
| | 0 | 116 | | Decoder decoder = encoding.GetDecoder(); |
| | | 117 | | |
| | | 118 | | int charCount; |
| | | 119 | | try |
| | 0 | 120 | | { |
| | 0 | 121 | | charCount = decoder.GetCharCount(input, flush: true); |
| | 0 | 122 | | } |
| | 0 | 123 | | catch (DecoderFallbackException) |
| | 0 | 124 | | { |
| | | 125 | | // The input is not valid without fallbacks. |
| | 0 | 126 | | return; |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | TestWithSubstitution(input, encoding); |
| | 0 | 130 | | } |
| | | 131 | | |
| | | 132 | | private static void TestWithConvert(ReadOnlySpan<byte> input, Encoding encoding) |
| | 0 | 133 | | { |
| | | 134 | | // Use a few boundary cases. |
| | 0 | 135 | | TestWithConvert(input, encoding, 1); |
| | 0 | 136 | | TestWithConvert(input, encoding, 2); |
| | 0 | 137 | | TestWithConvert(input, encoding, 3); |
| | 0 | 138 | | TestWithConvert(input, encoding, 4); |
| | 0 | 139 | | TestWithConvert(input, encoding, input.Length); |
| | | 140 | | |
| | 0 | 141 | | if (input.Length >= 6) |
| | 0 | 142 | | { |
| | 0 | 143 | | TestWithConvert(input, encoding, input.Length - 1); |
| | | 144 | | |
| | 0 | 145 | | if (input.Length >= 12) |
| | 0 | 146 | | { |
| | 0 | 147 | | TestWithConvert(input, encoding, input.Length / 2); |
| | 0 | 148 | | } |
| | 0 | 149 | | } |
| | 0 | 150 | | } |
| | | 151 | | |
| | | 152 | | // Verify that obtaining data using several Convert() calls matches the result from a single GetChars() call. |
| | | 153 | | private static void TestWithConvert(ReadOnlySpan<byte> input, Encoding encoding, int blockSize) |
| | 0 | 154 | | { |
| | 0 | 155 | | Decoder decoder = encoding.GetDecoder(); |
| | | 156 | | |
| | 0 | 157 | | int charCount = decoder.GetCharCount(input, flush: true); |
| | | 158 | | |
| | 0 | 159 | | using PooledBoundedMemory<char> chars = PooledBoundedMemory<char>.Rent(charCount, PoisonPagePlacement.After); |
| | 0 | 160 | | using PooledBoundedMemory<char> chars2 = PooledBoundedMemory<char>.Rent(charCount, PoisonPagePlacement.After); |
| | | 161 | | |
| | 0 | 162 | | decoder.Reset(); |
| | 0 | 163 | | int charsUsedTotal = 0; |
| | 0 | 164 | | int i = 0; |
| | | 165 | | |
| | 0 | 166 | | while (i < input.Length) |
| | 0 | 167 | | { |
| | 0 | 168 | | bool lastIteration = i + blockSize >= input.Length; |
| | 0 | 169 | | int bytesToRead = lastIteration ? input.Length - i : blockSize; |
| | | 170 | | |
| | 0 | 171 | | decoder.Convert( |
| | 0 | 172 | | input.Slice(i, bytesToRead), |
| | 0 | 173 | | chars.Span.Slice(charsUsedTotal, charCount - charsUsedTotal), |
| | 0 | 174 | | flush: lastIteration, |
| | 0 | 175 | | out int bytesUsed, |
| | 0 | 176 | | out int charsUsed, |
| | 0 | 177 | | out bool _); |
| | | 178 | | |
| | 0 | 179 | | i += bytesUsed; |
| | 0 | 180 | | charsUsedTotal += charsUsed; |
| | 0 | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | Assert.Equal(charsUsedTotal, charCount); |
| | 0 | 184 | | decoder.Reset(); |
| | 0 | 185 | | decoder.GetChars(input, chars2.Span, flush: true); |
| | 0 | 186 | | Assert.SequenceEqual<char>(chars.Span, chars2.Span); |
| | 0 | 187 | | } |
| | | 188 | | } |