< Summary

Information
Class: DotnetFuzzing.Fuzzers.Deflate64Fuzzer
Assembly: DotnetFuzzing
File(s): D:\runner\runtime\src\libraries\Fuzzing\DotnetFuzzing\Fuzzers\Deflate64Fuzzer.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 47
Coverable lines: 47
Total lines: 83
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
FuzzTarget(...)0%220%
.cctor()100%110%
CreateStream(...)100%110%
CopyToRentedArray(...)100%110%
TestArchive()0%220%

File(s)

D:\runner\runtime\src\libraries\Fuzzing\DotnetFuzzing\Fuzzers\Deflate64Fuzzer.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Linq;
 6using System.IO.Compression;
 7using System.Reflection;
 8using System.Threading.Tasks;
 9
 10namespace DotnetFuzzing.Fuzzers;
 11
 12internal sealed class Deflate64Fuzzer : IFuzzer
 13{
 014    public string[] TargetAssemblies { get; } = ["System.IO.Compression"];
 015    public string[] TargetCoreLibPrefixes => [];
 016    public string Corpus => "deflate64";
 17
 18    public void FuzzTarget(ReadOnlySpan<byte> bytes)
 019    {
 020        if (bytes.IsEmpty)
 021        {
 022            return;
 23        }
 24
 025        TestArchive(CopyToRentedArray(bytes), bytes.Length, async: false).GetAwaiter().GetResult();
 026        TestArchive(CopyToRentedArray(bytes), bytes.Length, async: true).GetAwaiter().GetResult();
 027    }
 28
 29#pragma warning disable IL2026 // RequiresUnreferencedCode
 030    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)
 034    {
 35#pragma warning disable IL2077 // dynamic access to non-public ctors
 036        return (Stream)Activator.CreateInstance(
 037            _deflateStreamType,
 038            bindingAttr: BindingFlags.NonPublic | BindingFlags.Instance,
 039            binder: null,
 040            args: new object[] { new MemoryStream(bytes, 0, length), ZipCompressionMethod.Deflate64, -1L },
 041            culture: null)!;
 42#pragma warning restore IL2077
 043    }
 44
 45    private byte[] CopyToRentedArray(ReadOnlySpan<byte> bytes)
 046    {
 047        byte[] buffer = ArrayPool<byte>.Shared.Rent(bytes.Length);
 48        try
 049        {
 050            bytes.CopyTo(buffer);
 051            return buffer;
 52        }
 053        catch
 054        {
 055            ArrayPool<byte>.Shared.Return(buffer);
 056            throw;
 57        }
 058    }
 59
 60    private async Task TestArchive(byte[] buffer, int length, bool async)
 061    {
 62        try
 063        {
 064            using var stream = CreateStream(buffer, length);
 065            if (async)
 066            {
 067                await stream.CopyToAsync(Stream.Null);
 068            }
 69            else
 070            {
 071                stream.CopyTo(Stream.Null);
 072            }
 073        }
 074        catch (InvalidDataException)
 075        {
 76            // ignore, this exception is expected to be thrown for invalid/corrupted archives.
 077        }
 78        finally
 079        {
 080            ArrayPool<byte>.Shared.Return(buffer);
 081        }
 082    }
 83}