| | | 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.IO.Compression; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | |
| | | 8 | | namespace DotnetFuzzing.Fuzzers; |
| | | 9 | | |
| | | 10 | | internal sealed class ZipArchiveFuzzer : IFuzzer |
| | | 11 | | { |
| | 1 | 12 | | public string[] TargetAssemblies { get; } = ["System.IO.Compression"]; |
| | 0 | 13 | | public string[] TargetCoreLibPrefixes => []; |
| | 0 | 14 | | public string Corpus => "ziparchive"; |
| | | 15 | | |
| | | 16 | | public void FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 705 | 17 | | { |
| | 705 | 18 | | if (bytes.IsEmpty) |
| | 0 | 19 | | { |
| | 0 | 20 | | return; |
| | | 21 | | } |
| | | 22 | | |
| | 705 | 23 | | TestArchive(CopyToRentedArray(bytes), bytes.Length, async: false).GetAwaiter().GetResult(); |
| | 705 | 24 | | TestArchive(CopyToRentedArray(bytes), bytes.Length, async: true).GetAwaiter().GetResult(); |
| | 705 | 25 | | } |
| | | 26 | | |
| | | 27 | | private byte[] CopyToRentedArray(ReadOnlySpan<byte> bytes) |
| | 1410 | 28 | | { |
| | 1410 | 29 | | byte[] buffer = ArrayPool<byte>.Shared.Rent(bytes.Length); |
| | | 30 | | try |
| | 1410 | 31 | | { |
| | 1410 | 32 | | bytes.CopyTo(buffer); |
| | 1410 | 33 | | return buffer; |
| | | 34 | | } |
| | 0 | 35 | | catch |
| | 0 | 36 | | { |
| | 0 | 37 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 0 | 38 | | throw; |
| | | 39 | | } |
| | 1410 | 40 | | } |
| | | 41 | | |
| | | 42 | | private async Task TestArchive(byte[] buffer, int length, bool async) |
| | 1410 | 43 | | { |
| | | 44 | | try |
| | 1410 | 45 | | { |
| | 1410 | 46 | | using var stream = new MemoryStream(buffer, 0, length); |
| | | 47 | | |
| | | 48 | | ZipArchive archive; |
| | | 49 | | |
| | 1410 | 50 | | if (async) |
| | 705 | 51 | | { |
| | 705 | 52 | | archive = await ZipArchive.CreateAsync(stream, ZipArchiveMode.Read, leaveOpen: false, entryNameEncoding: |
| | 182 | 53 | | } |
| | | 54 | | else |
| | 705 | 55 | | { |
| | 705 | 56 | | archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: false, entryNameEncoding: null); |
| | 578 | 57 | | } |
| | | 58 | | |
| | 3536 | 59 | | foreach (var entry in archive.Entries) |
| | 826 | 60 | | { |
| | | 61 | | // Access entry properties to simulate usage |
| | 826 | 62 | | _ = entry.FullName; |
| | 826 | 63 | | _ = entry.Length; |
| | 826 | 64 | | _ = entry.Comment; |
| | 826 | 65 | | _ = entry.LastWriteTime; |
| | 826 | 66 | | _ = entry.Crc32; |
| | 826 | 67 | | } |
| | | 68 | | |
| | 364 | 69 | | if (async) |
| | 182 | 70 | | { |
| | 182 | 71 | | await archive.DisposeAsync(); |
| | 182 | 72 | | } |
| | | 73 | | else |
| | 182 | 74 | | { |
| | 182 | 75 | | archive.Dispose(); |
| | 182 | 76 | | } |
| | 364 | 77 | | } |
| | 1046 | 78 | | catch (InvalidDataException) |
| | 1046 | 79 | | { |
| | | 80 | | // ignore, this exception is expected to be thrown for invalid/corrupted archives. |
| | 1046 | 81 | | } |
| | | 82 | | finally |
| | 1410 | 83 | | { |
| | 1410 | 84 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 1410 | 85 | | } |
| | 1410 | 86 | | } |
| | | 87 | | } |