-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Miha Zupan <[email protected]> Co-authored-by: Adam Sitnik <[email protected]>
- Loading branch information
1 parent
5b33115
commit 48ae967
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/libraries/Fuzzing/DotnetFuzzing/Dictionaries/typename.dict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
" " | ||
"0" | ||
"," | ||
"[*[" | ||
" System.Int32 " | ||
"System.Int32&&" | ||
"System.Int32[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]" | ||
"System.Int32[*******************************]" | ||
"System.Int32[][][][][][][][][][][][][][][][][][][][]" | ||
".NoNamespace" | ||
"Namespace.Ko\xC5\x9B\xC4\x87" | ||
"Declaring+Nested" | ||
"Declaring+Deep+Deeper+Nested" | ||
"Declaring+Deep+Deeper+Nested, Lib" | ||
"MyNamespace.MyType+NestedType" | ||
"Pointer*******" | ||
"Generic`1[[A]]" | ||
"Generic`2[[A],[B]]" | ||
"Generic`3[[A],[B],[C]]" | ||
"Generic`3[A,B,C]" | ||
"Generic`2[[System.Int32, mscorlib, Version=4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934],[System.Boolean, mscorlib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" | ||
"Generic`3[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089], System.Boolean, [System.Byte, other, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" | ||
"TypeNameWith\\TheEscapingCharacter" | ||
"TypeNameWith[[]]NestedSquareBrackets" | ||
"TypeNameWith\\[]+*&,AllSpecialCharacters" | ||
"System.Reflection.Metadata.Tests.TypeNameTests+NestedGeneric_0`1+NestedGeneric_1`2+NestedGeneric_2`3+NestedNonGeneric_3[[System.Int32, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Boolean, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int16, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Byte, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.SByte, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Reflection.Metadata.Tests, Version=9.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" |
65 changes: 65 additions & 0 deletions
65
src/libraries/Fuzzing/DotnetFuzzing/Fuzzers/TypeNameFuzzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
using System.Reflection.Metadata; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.InteropServices.Marshalling; | ||
using System.Text; | ||
|
||
namespace DotnetFuzzing.Fuzzers | ||
{ | ||
internal sealed class TypeNameFuzzer : IFuzzer | ||
{ | ||
public string[] TargetAssemblies { get; } = ["System.Reflection.Metadata"]; | ||
public string[] TargetCoreLibPrefixes => []; | ||
public string Dictionary => "typename.dict"; | ||
|
||
public void FuzzTarget(ReadOnlySpan<byte> bytes) | ||
{ | ||
Span<char> charSpan = new char[Encoding.UTF8.GetCharCount(bytes)]; | ||
Encoding.UTF8.GetChars(bytes, charSpan); | ||
using var poisonAfter = PooledBoundedMemory<char>.Rent(charSpan, PoisonPagePlacement.After); | ||
using var poisonBefore = PooledBoundedMemory<char>.Rent(charSpan, PoisonPagePlacement.Before); | ||
|
||
Test(poisonAfter.Span); | ||
Test(poisonBefore.Span); | ||
} | ||
|
||
private static void Test(Span<char> testSpan) | ||
{ | ||
if (TypeName.TryParse(testSpan, out TypeName? result1)) | ||
{ | ||
TypeName result2 = TypeName.Parse(testSpan); | ||
Assert.Equal(result1.Name, result2.Name); | ||
Assert.Equal(result1.FullName, result2.FullName); | ||
Assert.Equal(result1.AssemblyQualifiedName, result2.AssemblyQualifiedName); | ||
Assert.Equal(result1.IsSimple, result2.IsSimple); | ||
Assert.Equal(result1.IsNested, result2.IsNested); | ||
Assert.Equal(result1.IsArray, result2.IsArray); | ||
Assert.Equal(result1.GetNodeCount(), result2.GetNodeCount()); | ||
if (result1.AssemblyName != null) | ||
{ | ||
Assert.Equal(result1.AssemblyName.Name, result2.AssemblyName!.Name); | ||
Assert.Equal(result1.AssemblyName.FullName, result2.AssemblyName.FullName); | ||
Assert.Equal(result1.AssemblyName.CultureName, result2.AssemblyName.CultureName); | ||
Assert.Equal(result1.AssemblyName.Version?.ToString(), result2.AssemblyName.Version?.ToString()); | ||
} | ||
else | ||
{ | ||
Assert.Equal(result1.AssemblyName, result2.AssemblyName); | ||
} | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
TypeName.Parse(testSpan); | ||
Assert.Equal(true, false); // should never succeed | ||
} | ||
catch (ArgumentException) { } | ||
catch (InvalidOperationException) { } | ||
} | ||
} | ||
} | ||
} |