| | | 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; |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.Buffers.Text; |
| | | 7 | | using System.Collections; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Diagnostics; |
| | | 10 | | using System.IO; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | using System.Runtime.InteropServices; |
| | | 13 | | using System.Text; |
| | | 14 | | using System.Text.Encodings.Web; |
| | | 15 | | using System.Text.Json; |
| | | 16 | | using System.Text.Unicode; |
| | | 17 | | using SharpFuzz; |
| | | 18 | | |
| | | 19 | | namespace DotnetFuzzing.Fuzzers; |
| | | 20 | | |
| | | 21 | | internal sealed class Utf8JsonWriterFuzzer : IFuzzer |
| | | 22 | | { |
| | 1 | 23 | | public string[] TargetAssemblies { get; } = ["System.Text.Json"]; |
| | | 24 | | |
| | 0 | 25 | | public string[] TargetCoreLibPrefixes => []; |
| | | 26 | | |
| | | 27 | | // One of the bytes in the input is used to set various test options. |
| | | 28 | | // Each bit in that byte represents a different option as indicated here. |
| | | 29 | | |
| | | 30 | | // Options for JsonWriterOptions |
| | | 31 | | private const byte IndentFlag = 1; |
| | | 32 | | private const byte EncoderFlag = 1 << 1; |
| | | 33 | | private const byte MaxDepthFlag = 1 << 2; |
| | | 34 | | private const byte NewLineFlag = 1 << 3; |
| | | 35 | | private const byte SkipValidationFlag = 1 << 4; |
| | | 36 | | |
| | | 37 | | // Options for choosing between base64, UTF-8 and UTF-16 encoding |
| | | 38 | | private const byte EncodingMask = 0b11 << 5; |
| | | 39 | | private const byte Utf8EncodingFlag = 0b00 << 5; |
| | | 40 | | private const byte Utf16EncodingFlag = 0b01 << 5; |
| | | 41 | | private const byte Base64EncodingFlag = 0b10 << 5; |
| | | 42 | | |
| | | 43 | | public void FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 0 | 44 | | { |
| | | 45 | | const int minLength = 10; // 2 ints, 1 byte, and 1 padding to align chars |
| | 0 | 46 | | if (bytes.Length < minLength) |
| | 0 | 47 | | { |
| | 0 | 48 | | return; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | // First 2 ints are used as indices to slice the input and the following byte is used for options |
| | 0 | 52 | | ReadOnlySpan<int> ints = MemoryMarshal.Cast<byte, int>(bytes); |
| | 0 | 53 | | int slice1 = ints[0]; |
| | 0 | 54 | | int slice2 = ints[1]; |
| | 0 | 55 | | byte optionsByte = bytes[8]; |
| | 0 | 56 | | bytes = bytes.Slice(minLength); |
| | 0 | 57 | | ReadOnlySpan<char> chars = MemoryMarshal.Cast<byte, char>(bytes); |
| | | 58 | | |
| | | 59 | | // Validate that the indices are within bounds of the input |
| | 0 | 60 | | int encoding = optionsByte & EncodingMask; |
| | 0 | 61 | | if (encoding is not Utf8EncodingFlag and not Utf16EncodingFlag and not Base64EncodingFlag) |
| | 0 | 62 | | { |
| | 0 | 63 | | return; |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | if (!(0 <= slice1 && slice1 <= slice2 && slice2 <= (encoding is Utf16EncodingFlag ? chars.Length : bytes.Length) |
| | 0 | 67 | | { |
| | 0 | 68 | | return; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // Set up options based on the first byte |
| | 0 | 72 | | bool indented = (optionsByte & IndentFlag) == 0; |
| | 0 | 73 | | JsonWriterOptions options = new() |
| | 0 | 74 | | { |
| | 0 | 75 | | Encoder = (optionsByte & EncoderFlag) == 0 ? JavaScriptEncoder.Default : JavaScriptEncoder.UnsafeRelaxedJson |
| | 0 | 76 | | Indented = indented, |
| | 0 | 77 | | MaxDepth = (optionsByte & MaxDepthFlag) == 0 ? 1 : 0, |
| | 0 | 78 | | NewLine = (optionsByte & NewLineFlag) == 0 ? "\n" : "\r\n", |
| | 0 | 79 | | SkipValidation = (optionsByte & SkipValidationFlag) == 0, |
| | 0 | 80 | | }; |
| | | 81 | | |
| | | 82 | | // Compute the expected result by using the encoder directly and the input |
| | 0 | 83 | | int maxExpandedSizeBytes = 6 * bytes.Length + 2; |
| | 0 | 84 | | byte[] expectedBuffer = ArrayPool<byte>.Shared.Rent(maxExpandedSizeBytes); |
| | 0 | 85 | | Span<byte> expected = |
| | 0 | 86 | | expectedBuffer.AsSpan(0, encoding == Utf16EncodingFlag |
| | 0 | 87 | | ? EncodeToUtf8(chars, expectedBuffer, options.Encoder) |
| | 0 | 88 | | : EncodeToUtf8(bytes, expectedBuffer, options.Encoder, encoding == Base64EncodingFlag)); |
| | | 89 | | |
| | | 90 | | // Compute the actual result by using Utf8JsonWriter. Each iteration is a different slice of the input, but the |
| | 0 | 91 | | byte[] actualBuffer = new byte[expected.Length]; |
| | 0 | 92 | | foreach (ReadOnlySpan<Range> ranges in new[] |
| | 0 | 93 | | { |
| | 0 | 94 | | new[] { 0.. }, |
| | 0 | 95 | | new[] { 0..slice1, slice1.. }, |
| | 0 | 96 | | new[] { 0..slice1, slice1..slice2, slice2.. }, |
| | 0 | 97 | | }) |
| | 0 | 98 | | { |
| | 0 | 99 | | using MemoryStream stream = new(actualBuffer); |
| | 0 | 100 | | using Utf8JsonWriter writer = new(stream, options); |
| | | 101 | | |
| | 0 | 102 | | if (encoding == Utf16EncodingFlag) |
| | 0 | 103 | | { |
| | 0 | 104 | | WriteStringValueSegments(writer, chars, ranges); |
| | 0 | 105 | | } |
| | | 106 | | else |
| | 0 | 107 | | { |
| | 0 | 108 | | WriteStringValueSegments(writer, bytes, ranges, encoding == Base64EncodingFlag); |
| | 0 | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | writer.Flush(); |
| | | 112 | | |
| | | 113 | | // Compare the expected and actual results |
| | 0 | 114 | | Assert.SequenceEqual(expected, actualBuffer); |
| | 0 | 115 | | Assert.Equal(expected.Length, writer.BytesCommitted); |
| | 0 | 116 | | Assert.Equal(0, writer.BytesPending); |
| | | 117 | | |
| | 0 | 118 | | Array.Clear(actualBuffer); |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | // Additional test for mixing UTF-8 and UTF-16 encoding. The alignment math is easier in UTF-16 mode so just run |
| | 0 | 122 | | if (encoding == Utf16EncodingFlag && !options.SkipValidation) |
| | 0 | 123 | | { |
| | 0 | 124 | | Array.Clear(expectedBuffer); |
| | | 125 | | |
| | 0 | 126 | | { |
| | 0 | 127 | | ReadOnlySpan<char> firstSegment = chars[slice1..]; |
| | 0 | 128 | | ReadOnlySpan<byte> secondSegment = bytes[0..(2 * slice1)]; |
| | | 129 | | |
| | 0 | 130 | | expected = expectedBuffer.AsSpan(0, EncodeToUtf8(firstSegment, expectedBuffer, options.Encoder)); |
| | | 131 | | |
| | 0 | 132 | | actualBuffer = new byte[expected.Length]; |
| | 0 | 133 | | using MemoryStream stream = new(actualBuffer); |
| | 0 | 134 | | using Utf8JsonWriter writer = new(stream, options); |
| | | 135 | | |
| | | 136 | | // UTF-16 + UTF-8 |
| | 0 | 137 | | writer.WriteStringValueSegment(firstSegment, false); |
| | 0 | 138 | | Assert.Throws<InvalidOperationException, ReadOnlySpan<byte>>(state => writer.WriteStringValueSegment(sta |
| | | 139 | | |
| | 0 | 140 | | stream.Position = 0; |
| | 0 | 141 | | writer.Reset(); |
| | | 142 | | |
| | | 143 | | // UTF-16 + Base64 |
| | 0 | 144 | | writer.WriteStringValueSegment(firstSegment, false); |
| | 0 | 145 | | Assert.Throws<InvalidOperationException, ReadOnlySpan<byte>>(state => writer.WriteBase64StringSegment(st |
| | 0 | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | Array.Clear(expectedBuffer); |
| | | 149 | | |
| | 0 | 150 | | { |
| | 0 | 151 | | ReadOnlySpan<byte> firstSegment = bytes[0..(2 * slice1)]; |
| | 0 | 152 | | ReadOnlySpan<char> secondSegment = chars[slice1..]; |
| | | 153 | | |
| | 0 | 154 | | expected = expectedBuffer.AsSpan(0, EncodeToUtf8(firstSegment, expectedBuffer, options.Encoder, base64En |
| | | 155 | | |
| | 0 | 156 | | actualBuffer = new byte[expected.Length]; |
| | 0 | 157 | | using MemoryStream stream = new(actualBuffer); |
| | 0 | 158 | | using Utf8JsonWriter writer = new(stream, options); |
| | | 159 | | |
| | | 160 | | // UTF-8 + UTF-16 |
| | 0 | 161 | | writer.WriteStringValueSegment(firstSegment, false); |
| | 0 | 162 | | Assert.Throws<InvalidOperationException, ReadOnlySpan<char>>(state => writer.WriteStringValueSegment(sta |
| | | 163 | | |
| | 0 | 164 | | stream.Position = 0; |
| | 0 | 165 | | writer.Reset(); |
| | | 166 | | |
| | | 167 | | // UTF-8 + Base64 |
| | 0 | 168 | | writer.WriteStringValueSegment(firstSegment, false); |
| | 0 | 169 | | Assert.Throws<InvalidOperationException, ReadOnlySpan<byte>>(state => writer.WriteBase64StringSegment(st |
| | 0 | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | Array.Clear(expectedBuffer); |
| | | 173 | | |
| | 0 | 174 | | { |
| | 0 | 175 | | ReadOnlySpan<byte> firstSegment = bytes[0..(2 * slice1)]; |
| | 0 | 176 | | ReadOnlySpan<char> secondSegment = chars[slice1..]; |
| | | 177 | | |
| | 0 | 178 | | expected = expectedBuffer.AsSpan(0, EncodeToUtf8(firstSegment, expectedBuffer, options.Encoder, base64En |
| | | 179 | | |
| | 0 | 180 | | actualBuffer = new byte[expected.Length]; |
| | 0 | 181 | | using MemoryStream stream = new(actualBuffer); |
| | 0 | 182 | | using Utf8JsonWriter writer = new(stream, options); |
| | | 183 | | |
| | | 184 | | // Base64 + UTF-16 |
| | 0 | 185 | | writer.WriteBase64StringSegment(firstSegment, false); |
| | 0 | 186 | | Assert.Throws<InvalidOperationException, ReadOnlySpan<char>>(state => writer.WriteStringValueSegment(sta |
| | | 187 | | |
| | 0 | 188 | | stream.Position = 0; |
| | 0 | 189 | | writer.Reset(); |
| | | 190 | | |
| | | 191 | | // Base64 + UTF-8 |
| | 0 | 192 | | writer.WriteBase64StringSegment(firstSegment, false); |
| | 0 | 193 | | Assert.Throws<InvalidOperationException, ReadOnlySpan<byte>>(state => writer.WriteStringValueSegment(sta |
| | 0 | 194 | | } |
| | 0 | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | ArrayPool<byte>.Shared.Return(expectedBuffer); |
| | 0 | 198 | | } |
| | | 199 | | |
| | | 200 | | private static void WriteStringValueSegments(Utf8JsonWriter writer, ReadOnlySpan<byte> bytes, ReadOnlySpan<Range> ra |
| | 0 | 201 | | { |
| | 0 | 202 | | if (base64Encode) |
| | 0 | 203 | | { |
| | 0 | 204 | | for (int i = 0; i < ranges.Length; i++) |
| | 0 | 205 | | { |
| | 0 | 206 | | writer.WriteBase64StringSegment(bytes[ranges[i]], i == ranges.Length - 1); |
| | 0 | 207 | | } |
| | 0 | 208 | | } |
| | | 209 | | else |
| | 0 | 210 | | { |
| | 0 | 211 | | for (int i = 0; i < ranges.Length; i++) |
| | 0 | 212 | | { |
| | 0 | 213 | | writer.WriteStringValueSegment(bytes[ranges[i]], i == ranges.Length - 1); |
| | 0 | 214 | | } |
| | 0 | 215 | | } |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | private static void WriteStringValueSegments(Utf8JsonWriter writer, ReadOnlySpan<char> chars, ReadOnlySpan<Range> ra |
| | 0 | 219 | | { |
| | 0 | 220 | | for (int i = 0; i < ranges.Length; i++) |
| | 0 | 221 | | { |
| | 0 | 222 | | writer.WriteStringValueSegment(chars[ranges[i]], i == ranges.Length - 1); |
| | 0 | 223 | | } |
| | 0 | 224 | | } |
| | | 225 | | |
| | | 226 | | private static int EncodeToUtf8(ReadOnlySpan<byte> bytes, Span<byte> destBuffer, JavaScriptEncoder encoder, bool bas |
| | 0 | 227 | | { |
| | 0 | 228 | | destBuffer[0] = (byte)'"'; |
| | | 229 | | |
| | | 230 | | int written; |
| | 0 | 231 | | if (base64Encode) |
| | 0 | 232 | | { |
| | 0 | 233 | | Base64.EncodeToUtf8(bytes, destBuffer[1..], out _, out written, isFinalBlock: true); |
| | 0 | 234 | | } |
| | | 235 | | else |
| | 0 | 236 | | { |
| | 0 | 237 | | encoder.EncodeUtf8(bytes, destBuffer[1..], out _, out written, isFinalBlock: true); |
| | 0 | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | destBuffer[++written] = (byte)'"'; |
| | 0 | 241 | | return written + 1; |
| | 0 | 242 | | } |
| | | 243 | | |
| | | 244 | | private static int EncodeToUtf8(ReadOnlySpan<char> chars, Span<byte> destBuffer, JavaScriptEncoder encoder) |
| | 0 | 245 | | { |
| | 0 | 246 | | int written = 1; |
| | 0 | 247 | | destBuffer[0] = (byte)'"'; |
| | 0 | 248 | | destBuffer[written += EncodeTranscode(chars, destBuffer[1..], encoder)] = (byte)'"'; |
| | 0 | 249 | | return written + 1; |
| | 0 | 250 | | } |
| | | 251 | | |
| | | 252 | | private static int EncodeTranscode(ReadOnlySpan<char> chars, Span<byte> destBuffer, JavaScriptEncoder encoder, bool |
| | 0 | 253 | | { |
| | 0 | 254 | | var utf16buffer = ArrayPool<char>.Shared.Rent(6 * chars.Length); |
| | 0 | 255 | | encoder.Encode(chars, utf16buffer, out _, out int written, isFinalBlock: true); |
| | | 256 | | |
| | 0 | 257 | | Utf8.FromUtf16(utf16buffer.AsSpan(0, written), destBuffer, out _, out written, isFinalBlock); |
| | 0 | 258 | | ArrayPool<char>.Shared.Return(utf16buffer); |
| | 0 | 259 | | return written; |
| | 0 | 260 | | } |
| | | 261 | | } |