| | | 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.Numerics; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using System.Runtime.InteropServices; |
| | | 8 | | |
| | | 9 | | namespace DotnetFuzzing.Fuzzers; |
| | | 10 | | |
| | | 11 | | internal sealed class SearchValuesByteCharFuzzer : IFuzzer |
| | | 12 | | { |
| | 0 | 13 | | public string[] TargetAssemblies => []; |
| | 1 | 14 | | public string[] TargetCoreLibPrefixes { get; } = ["System.Buffers", "System.SpanHelpers", "System.PackedSpanHelpers" |
| | | 15 | | |
| | | 16 | | public unsafe void FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 0 | 17 | | { |
| | 0 | 18 | | int newLine = bytes.IndexOf((byte)'\n'); |
| | 0 | 19 | | if (newLine < 0) |
| | 0 | 20 | | { |
| | 0 | 21 | | return; |
| | | 22 | | } |
| | | 23 | | |
| | 0 | 24 | | ReadOnlySpan<byte> haystack = bytes.Slice(newLine + 1); |
| | 0 | 25 | | ReadOnlySpan<byte> values = bytes.Slice(0, newLine); |
| | | 26 | | |
| | 0 | 27 | | using var byteHaystack0 = PooledBoundedMemory<byte>.Rent(haystack, PoisonPagePlacement.Before); |
| | 0 | 28 | | using var byteHaystack1 = PooledBoundedMemory<byte>.Rent(haystack, PoisonPagePlacement.After); |
| | | 29 | | |
| | 0 | 30 | | Test(byteHaystack0.Span, byteHaystack1.Span, values, SearchValues.Create(values)); |
| | | 31 | | |
| | 0 | 32 | | if ((nuint)Unsafe.AsPointer(ref MemoryMarshal.GetReference(haystack)) % 2 != 0 && !haystack.IsEmpty) |
| | 0 | 33 | | { |
| | | 34 | | // Ensure that the haystack is 2-byte aligned now that we're about to cast it to chars. |
| | 0 | 35 | | haystack = haystack.Slice(1); |
| | 0 | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | using var charHaystack0 = PooledBoundedMemory<char>.Rent(MemoryMarshal.Cast<byte, char>(haystack), PoisonPagePla |
| | 0 | 39 | | using var charHaystack1 = PooledBoundedMemory<char>.Rent(MemoryMarshal.Cast<byte, char>(haystack), PoisonPagePla |
| | | 40 | | |
| | 0 | 41 | | ReadOnlySpan<char> charValues = MemoryMarshal.Cast<byte, char>(values); |
| | 0 | 42 | | Test(charHaystack0.Span, charHaystack1.Span, charValues, SearchValues.Create(charValues)); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | private static void Test<T>(ReadOnlySpan<T> haystack, ReadOnlySpan<T> haystackCopy, ReadOnlySpan<T> values, SearchVa |
| | | 46 | | where T : struct, INumber<T>, IMinMaxValue<T> |
| | 0 | 47 | | { |
| | 0 | 48 | | int indexOfAny = haystack.IndexOfAny(searchValues); |
| | 0 | 49 | | int indexOfAnyExcept = haystack.IndexOfAnyExcept(searchValues); |
| | 0 | 50 | | int lastIndexOfAny = haystack.LastIndexOfAny(searchValues); |
| | 0 | 51 | | int lastIndexOfAnyExcept = haystack.LastIndexOfAnyExcept(searchValues); |
| | | 52 | | |
| | 0 | 53 | | Assert.Equal(indexOfAny, haystackCopy.IndexOfAny(searchValues)); |
| | 0 | 54 | | Assert.Equal(indexOfAnyExcept, haystackCopy.IndexOfAnyExcept(searchValues)); |
| | 0 | 55 | | Assert.Equal(lastIndexOfAny, haystackCopy.LastIndexOfAny(searchValues)); |
| | 0 | 56 | | Assert.Equal(lastIndexOfAnyExcept, haystackCopy.LastIndexOfAnyExcept(searchValues)); |
| | | 57 | | |
| | 0 | 58 | | Assert.Equal(IndexOfAnyScalar(haystack, searchValues), indexOfAny); |
| | 0 | 59 | | Assert.Equal(IndexOfAnyExceptScalar(haystack, searchValues), indexOfAnyExcept); |
| | 0 | 60 | | Assert.Equal(LastIndexOfAnyScalar(haystack, searchValues), lastIndexOfAny); |
| | 0 | 61 | | Assert.Equal(LastIndexOfAnyExceptScalar(haystack, searchValues), lastIndexOfAnyExcept); |
| | | 62 | | |
| | 0 | 63 | | Assert.Equal(indexOfAny >= 0, lastIndexOfAny >= 0); |
| | 0 | 64 | | Assert.Equal(indexOfAnyExcept >= 0, lastIndexOfAnyExcept >= 0); |
| | | 65 | | |
| | 0 | 66 | | Assert.Equal(indexOfAny >= 0, haystack.ContainsAny(searchValues)); |
| | 0 | 67 | | Assert.Equal(indexOfAnyExcept >= 0, haystack.ContainsAnyExcept(searchValues)); |
| | | 68 | | |
| | 0 | 69 | | if (!values.IsEmpty) |
| | 0 | 70 | | { |
| | 0 | 71 | | Assert.Equal(0, values.IndexOfAny(searchValues)); |
| | 0 | 72 | | Assert.Equal(-1, values.IndexOfAnyExcept(searchValues)); |
| | 0 | 73 | | } |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | private static int IndexOfAnyScalar<T>(ReadOnlySpan<T> haystack, SearchValues<T> values) |
| | | 77 | | where T : IEquatable<T> |
| | 0 | 78 | | { |
| | 0 | 79 | | for (int i = 0; i < haystack.Length; i++) |
| | 0 | 80 | | { |
| | 0 | 81 | | if (values.Contains(haystack[i])) |
| | 0 | 82 | | { |
| | 0 | 83 | | return i; |
| | | 84 | | } |
| | 0 | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | return -1; |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | private static int IndexOfAnyExceptScalar<T>(ReadOnlySpan<T> haystack, SearchValues<T> values) |
| | | 91 | | where T : IEquatable<T> |
| | 0 | 92 | | { |
| | 0 | 93 | | for (int i = 0; i < haystack.Length; i++) |
| | 0 | 94 | | { |
| | 0 | 95 | | if (!values.Contains(haystack[i])) |
| | 0 | 96 | | { |
| | 0 | 97 | | return i; |
| | | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | return -1; |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | private static int LastIndexOfAnyScalar<T>(ReadOnlySpan<T> haystack, SearchValues<T> values) |
| | | 105 | | where T : IEquatable<T> |
| | 0 | 106 | | { |
| | 0 | 107 | | for (int i = haystack.Length - 1; i >= 0; i--) |
| | 0 | 108 | | { |
| | 0 | 109 | | if (values.Contains(haystack[i])) |
| | 0 | 110 | | { |
| | 0 | 111 | | return i; |
| | | 112 | | } |
| | 0 | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | return -1; |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | private static int LastIndexOfAnyExceptScalar<T>(ReadOnlySpan<T> haystack, SearchValues<T> values) |
| | | 119 | | where T : IEquatable<T> |
| | 0 | 120 | | { |
| | 0 | 121 | | for (int i = haystack.Length - 1; i >= 0; i--) |
| | 0 | 122 | | { |
| | 0 | 123 | | if (!values.Contains(haystack[i])) |
| | 0 | 124 | | { |
| | 0 | 125 | | return i; |
| | | 126 | | } |
| | 0 | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | return -1; |
| | 0 | 130 | | } |
| | | 131 | | } |