| | | 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.Linq; |
| | | 6 | | using System.IO.Compression; |
| | | 7 | | using System.Reflection; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | |
| | | 10 | | namespace DotnetFuzzing.Fuzzers; |
| | | 11 | | |
| | | 12 | | internal sealed class Deflate64Fuzzer : IFuzzer |
| | | 13 | | { |
| | 1 | 14 | | public string[] TargetAssemblies { get; } = ["System.IO.Compression"]; |
| | 0 | 15 | | public string[] TargetCoreLibPrefixes => []; |
| | 0 | 16 | | public string Corpus => "deflate64"; |
| | | 17 | | |
| | | 18 | | public void FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 0 | 19 | | { |
| | 0 | 20 | | if (bytes.IsEmpty) |
| | 0 | 21 | | { |
| | 0 | 22 | | return; |
| | | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | TestArchive(CopyToRentedArray(bytes), bytes.Length, async: false).GetAwaiter().GetResult(); |
| | 0 | 26 | | TestArchive(CopyToRentedArray(bytes), bytes.Length, async: true).GetAwaiter().GetResult(); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | #pragma warning disable IL2026 // RequiresUnreferencedCode |
| | 0 | 30 | | private static readonly Type _deflateStreamType = typeof(ZipArchive).Assembly.GetType("System.IO.Compression.Deflate |
| | | 31 | | #pragma warning restore IL2026 |
| | | 32 | | |
| | | 33 | | private static Stream CreateStream(byte[] bytes, int length) |
| | 0 | 34 | | { |
| | | 35 | | #pragma warning disable IL2077 // dynamic access to non-public ctors |
| | 0 | 36 | | return (Stream)Activator.CreateInstance( |
| | 0 | 37 | | _deflateStreamType, |
| | 0 | 38 | | bindingAttr: BindingFlags.NonPublic | BindingFlags.Instance, |
| | 0 | 39 | | binder: null, |
| | 0 | 40 | | args: new object[] { new MemoryStream(bytes, 0, length), ZipCompressionMethod.Deflate64, -1L }, |
| | 0 | 41 | | culture: null)!; |
| | | 42 | | #pragma warning restore IL2077 |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | private byte[] CopyToRentedArray(ReadOnlySpan<byte> bytes) |
| | 0 | 46 | | { |
| | 0 | 47 | | byte[] buffer = ArrayPool<byte>.Shared.Rent(bytes.Length); |
| | | 48 | | try |
| | 0 | 49 | | { |
| | 0 | 50 | | bytes.CopyTo(buffer); |
| | 0 | 51 | | return buffer; |
| | | 52 | | } |
| | 0 | 53 | | catch |
| | 0 | 54 | | { |
| | 0 | 55 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 0 | 56 | | throw; |
| | | 57 | | } |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | private async Task TestArchive(byte[] buffer, int length, bool async) |
| | 0 | 61 | | { |
| | | 62 | | try |
| | 0 | 63 | | { |
| | 0 | 64 | | using var stream = CreateStream(buffer, length); |
| | 0 | 65 | | if (async) |
| | 0 | 66 | | { |
| | 0 | 67 | | await stream.CopyToAsync(Stream.Null); |
| | 0 | 68 | | } |
| | | 69 | | else |
| | 0 | 70 | | { |
| | 0 | 71 | | stream.CopyTo(Stream.Null); |
| | 0 | 72 | | } |
| | 0 | 73 | | } |
| | 0 | 74 | | catch (InvalidDataException) |
| | 0 | 75 | | { |
| | | 76 | | // ignore, this exception is expected to be thrown for invalid/corrupted archives. |
| | 0 | 77 | | } |
| | | 78 | | finally |
| | 0 | 79 | | { |
| | 0 | 80 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 0 | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | } |