diff --git a/eng/Versions.props b/eng/Versions.props index 0ff857849146a..47e6c7846c769 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -119,7 +119,7 @@ 8.0.0 5.0.0 4.5.5 - 9.0.0-preview.4.24219.3 + 9.0.0-preview.4.24227.6 9.0.0-preview.4.24219.3 6.0.0 5.0.0 @@ -210,7 +210,7 @@ 0.11.4-alpha.24222.1 - 9.0.0-preview.4.24219.3 + 9.0.0-preview.4.24227.6 9.0.0-preview.5.24223.2 diff --git a/src/coreclr/tools/Common/TypeSystem/Common/Utilities/CustomAttributeTypeNameParser.cs b/src/coreclr/tools/Common/TypeSystem/Common/Utilities/CustomAttributeTypeNameParser.cs index 72779375f4697..33a86023ced27 100644 --- a/src/coreclr/tools/Common/TypeSystem/Common/Utilities/CustomAttributeTypeNameParser.cs +++ b/src/coreclr/tools/Common/TypeSystem/Common/Utilities/CustomAttributeTypeNameParser.cs @@ -29,6 +29,8 @@ namespace System.Reflection { internal partial struct TypeNameParser { + private static readonly TypeNameParseOptions s_typeNameParseOptions = new() { MaxNodes = int.MaxValue }; + private ModuleDesc _module; private bool _throwIfNotFound; private Func _canonResolver; @@ -36,7 +38,7 @@ internal partial struct TypeNameParser public static TypeDesc ResolveType(ModuleDesc module, string name, bool throwIfNotFound, Func canonResolver) { - if (!TypeName.TryParse(name.AsSpan(), out TypeName parsed)) + if (!TypeName.TryParse(name.AsSpan(), out TypeName parsed, s_typeNameParseOptions)) { ThrowHelper.ThrowTypeLoadException(name, module); } diff --git a/src/coreclr/tools/ILVerification/ILVerification.projitems b/src/coreclr/tools/ILVerification/ILVerification.projitems index 3b2c30ddef1f1..c0f3644b1ab34 100644 --- a/src/coreclr/tools/ILVerification/ILVerification.projitems +++ b/src/coreclr/tools/ILVerification/ILVerification.projitems @@ -66,30 +66,6 @@ Utilities\CustomAttributeTypeNameParser.cs - - Utilities\HexConverter.cs - - - Utilities\AssemblyNameFormatter.cs - - - Utilities\AssemblyNameParser.cs - - - Utilities\Metadata\AssemblyNameInfo.cs - - - Utilities\TypeName.cs - - - Utilities\TypeNameParserOptions.cs - - - Utilities\TypeNameParser.cs - - - Utilities\TypeNameParserHelpers.cs - System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs diff --git a/src/coreclr/tools/ILVerify/ILVerify.csproj b/src/coreclr/tools/ILVerify/ILVerify.csproj index e1915c65d6f2d..804f32ddc7bc3 100644 --- a/src/coreclr/tools/ILVerify/ILVerify.csproj +++ b/src/coreclr/tools/ILVerify/ILVerify.csproj @@ -17,6 +17,7 @@ + diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/TypeNameParser.Dataflow.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/TypeNameParser.Dataflow.cs index 5b0cfdb12b444..278b6c4735afc 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/TypeNameParser.Dataflow.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/TypeNameParser.Dataflow.cs @@ -9,6 +9,8 @@ namespace System.Reflection { internal partial struct TypeNameParser { + private static readonly TypeNameParseOptions s_typeNameParseOptions = new() { MaxNodes = int.MaxValue }; + private TypeSystemContext _context; private ModuleDesc _callingModule; private List _referencedModules; @@ -17,7 +19,7 @@ internal partial struct TypeNameParser public static TypeDesc ResolveType(string name, ModuleDesc callingModule, TypeSystemContext context, List referencedModules, out bool typeWasNotFoundInAssemblyNorBaseLibrary) { - if (!TypeName.TryParse(name, out TypeName parsed)) + if (!TypeName.TryParse(name, out TypeName parsed, s_typeNameParseOptions)) { typeWasNotFoundInAssemblyNorBaseLibrary = false; return null; diff --git a/src/coreclr/tools/aot/ILCompiler.TypeSystem/ILCompiler.TypeSystem.csproj b/src/coreclr/tools/aot/ILCompiler.TypeSystem/ILCompiler.TypeSystem.csproj index 23afa3780b1ab..1a5db3b44445d 100644 --- a/src/coreclr/tools/aot/ILCompiler.TypeSystem/ILCompiler.TypeSystem.csproj +++ b/src/coreclr/tools/aot/ILCompiler.TypeSystem/ILCompiler.TypeSystem.csproj @@ -198,39 +198,12 @@ Utilities\CustomAttributeTypeNameParser.cs - - Utilities\HexConverter.cs - - - Utilities\AssemblyNameFormatter.cs - - - Utilities\AssemblyNameParser.cs - - - Utilities\AssemblyNameInfo.cs - - - Utilities\TypeName.cs - - - Utilities\TypeNameParser.cs - - - Utilities\TypeNameParserHelpers.cs - - - Utilities\TypeNameParserOptions.cs - Utilities\CustomAttributeTypeNameParser.Helpers Utilities\ValueStringBuilder.cs - - Utilities\ValueStringBuilder.AppendSpanFormattable.cs - Utilities\GCPointerMap.Algorithm.cs @@ -741,6 +714,10 @@ + + + + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/libraries/Common/src/System/Reflection/Metadata/AssemblyNameInfo.cs b/src/libraries/Common/src/System/Reflection/Metadata/AssemblyNameInfo.cs index ee586e3b57a8c..cb4de9467f660 100644 --- a/src/libraries/Common/src/System/Reflection/Metadata/AssemblyNameInfo.cs +++ b/src/libraries/Common/src/System/Reflection/Metadata/AssemblyNameInfo.cs @@ -21,10 +21,10 @@ namespace System.Reflection.Metadata /// It's a more lightweight, immutable version of that does not pre-allocate instances. /// [DebuggerDisplay("{FullName}")] -#if SYSTEM_PRIVATE_CORELIB - internal -#else +#if SYSTEM_REFLECTION_METADATA public +#else + internal #endif sealed class AssemblyNameInfo { @@ -182,11 +182,7 @@ public AssemblyName ToAssemblyName() public static AssemblyNameInfo Parse(ReadOnlySpan assemblyName) => TryParse(assemblyName, out AssemblyNameInfo? result) ? result! -#if SYSTEM_REFLECTION_METADATA || SYSTEM_PRIVATE_CORELIB : throw new ArgumentException(SR.InvalidAssemblyName, nameof(assemblyName)); -#else // tools that reference this file as a link - : throw new ArgumentException("The given assembly name was invalid.", nameof(assemblyName)); -#endif /// /// Tries to parse a span of characters into an assembly name. diff --git a/src/libraries/Common/src/System/Reflection/Metadata/TypeName.cs b/src/libraries/Common/src/System/Reflection/Metadata/TypeName.cs index 2fac2f8ffd74d..1464f0248280b 100644 --- a/src/libraries/Common/src/System/Reflection/Metadata/TypeName.cs +++ b/src/libraries/Common/src/System/Reflection/Metadata/TypeName.cs @@ -16,10 +16,10 @@ namespace System.Reflection.Metadata { [DebuggerDisplay("{AssemblyQualifiedName}")] -#if SYSTEM_PRIVATE_CORELIB - internal -#else +#if SYSTEM_REFLECTION_METADATA public +#else + internal #endif sealed class TypeName { diff --git a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParser.cs b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParser.cs index 7c75f3ee844d6..dcba442054a5d 100644 --- a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParser.cs +++ b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParser.cs @@ -17,7 +17,8 @@ namespace System.Reflection.Metadata [DebuggerDisplay("{_inputString}")] internal ref struct TypeNameParser { - private static readonly TypeNameParseOptions _defaults = new(); + private static readonly TypeNameParseOptions s_defaults = new(); + private readonly bool _throwOnError; private readonly TypeNameParseOptions _parseOptions; private ReadOnlySpan _inputString; @@ -26,7 +27,7 @@ private TypeNameParser(ReadOnlySpan name, bool throwOnError, TypeNameParse { _inputString = name; _throwOnError = throwOnError; - _parseOptions = options ?? _defaults; + _parseOptions = options ?? s_defaults; } internal static TypeName? Parse(ReadOnlySpan typeName, bool throwOnError, TypeNameParseOptions? options = default) @@ -50,7 +51,7 @@ private TypeNameParser(ReadOnlySpan name, bool throwOnError, TypeNameParse { if (throwOnError) { - if (recursiveDepth >= parser._parseOptions.MaxNodes) + if (parser._parseOptions.IsMaxDepthExceeded(recursiveDepth)) { ThrowInvalidOperation_MaxNodesExceeded(parser._parseOptions.MaxNodes); } @@ -249,7 +250,7 @@ private bool TryParseAssemblyName(ref AssemblyNameInfo? assemblyName) private bool TryDive(ref int depth) { - if (depth >= _parseOptions.MaxNodes) + if (_parseOptions.IsMaxDepthExceeded(depth)) { return false; } diff --git a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserHelpers.cs b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserHelpers.cs index 3783f56c73d4b..1cdc5e230ad96 100644 --- a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserHelpers.cs +++ b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserHelpers.cs @@ -331,53 +331,64 @@ internal static bool TryStripFirstCharAndTrailingSpaces(ref ReadOnlySpan s } [DoesNotReturn] - internal static void ThrowInvalidOperation_MaxNodesExceeded(int limit) => throw -#if SYSTEM_REFLECTION_METADATA - new InvalidOperationException(SR.Format(SR.InvalidOperation_MaxNodesExceeded, limit)); -#else // corelib and tools that reference this file as a link - new InvalidOperationException(); -#endif + internal static void ThrowArgumentException_InvalidTypeName(int errorIndex) + { + throw new ArgumentException(SR.Argument_InvalidTypeName, $"typeName@{errorIndex}"); + } [DoesNotReturn] - internal static void ThrowArgumentException_InvalidTypeName(int errorIndex) => throw -#if SYSTEM_PRIVATE_CORELIB - new ArgumentException(SR.Arg_ArgumentException, $"typeName@{errorIndex}"); -#elif SYSTEM_REFLECTION_METADATA - new ArgumentException(SR.Argument_InvalidTypeName, $"typeName@{errorIndex}"); -#else // tools that reference this file as a link - new ArgumentException(); + internal static void ThrowInvalidOperation_MaxNodesExceeded(int limit) + { +#if SYSTEM_REFLECTION_METADATA + throw new InvalidOperationException(SR.Format(SR.InvalidOperation_MaxNodesExceeded, limit)); +#else + Debug.Fail("Expected to be unreachable"); + throw new InvalidOperationException(); #endif + } [DoesNotReturn] - internal static void ThrowInvalidOperation_NotGenericType() => throw -#if SYSTEM_REFLECTION_METADATA || SYSTEM_PRIVATE_CORELIB - new InvalidOperationException(SR.InvalidOperation_NotGenericType); -#else // tools that reference this file as a link - new InvalidOperationException(); + internal static void ThrowInvalidOperation_NotGenericType() + { +#if SYSTEM_REFLECTION_METADATA + throw new InvalidOperationException(SR.InvalidOperation_NotGenericType); +#else + Debug.Fail("Expected to be unreachable"); + throw new InvalidOperationException(); #endif + } [DoesNotReturn] - internal static void ThrowInvalidOperation_NotNestedType() => throw + internal static void ThrowInvalidOperation_NotNestedType() + { #if SYSTEM_REFLECTION_METADATA - new InvalidOperationException(SR.InvalidOperation_NotNestedType); -#else // corelib and tools that reference this file as a link - new InvalidOperationException(); + throw new InvalidOperationException(SR.InvalidOperation_NotNestedType); +#else + Debug.Fail("Expected to be unreachable"); + throw new InvalidOperationException(); #endif + } [DoesNotReturn] - internal static void ThrowInvalidOperation_NoElement() => throw + internal static void ThrowInvalidOperation_NoElement() + { #if SYSTEM_REFLECTION_METADATA - new InvalidOperationException(SR.InvalidOperation_NoElement); -#else // corelib and tools that reference this file as a link - new InvalidOperationException(); + throw new InvalidOperationException(SR.InvalidOperation_NoElement); +#else + Debug.Fail("Expected to be unreachable"); + throw new InvalidOperationException(); #endif + } [DoesNotReturn] - internal static void ThrowInvalidOperation_HasToBeArrayClass() => throw -#if SYSTEM_REFLECTION_METADATA || SYSTEM_PRIVATE_CORELIB - new InvalidOperationException(SR.Argument_HasToBeArrayClass); -#else // tools that reference this file as a link - new InvalidOperationException(); + internal static void ThrowInvalidOperation_HasToBeArrayClass() + { +#if SYSTEM_REFLECTION_METADATA + throw new InvalidOperationException(SR.Argument_HasToBeArrayClass); +#else + Debug.Fail("Expected to be unreachable"); + throw new InvalidOperationException(); #endif + } } } diff --git a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserOptions.cs b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserOptions.cs index 66350c9a44cac..551876e73147c 100644 --- a/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserOptions.cs +++ b/src/libraries/Common/src/System/Reflection/Metadata/TypeNameParserOptions.cs @@ -3,19 +3,9 @@ namespace System.Reflection.Metadata { -#if SYSTEM_PRIVATE_CORELIB - internal -#else - public -#endif - sealed class TypeNameParseOptions + public sealed class TypeNameParseOptions { - private int _maxNodes = -#if SYSTEM_PRIVATE_CORELIB - int.MaxValue; // CoreLib has never introduced any limits -#else - 20; -#endif + private int _maxNodes = 20; /// /// Limits the maximum value of node count that parser can handle. @@ -37,5 +27,7 @@ public int MaxNodes _maxNodes = value; } } + + internal bool IsMaxDepthExceeded(int depth) => depth >= _maxNodes; } } diff --git a/src/libraries/Common/src/System/Reflection/TypeNameParser.Helpers.cs b/src/libraries/Common/src/System/Reflection/TypeNameParser.Helpers.cs index bcb2125d3f4ea..84ef2137fb095 100644 --- a/src/libraries/Common/src/System/Reflection/TypeNameParser.Helpers.cs +++ b/src/libraries/Common/src/System/Reflection/TypeNameParser.Helpers.cs @@ -8,6 +8,28 @@ #nullable enable +#if SYSTEM_PRIVATE_CORELIB +namespace System.Reflection.Metadata +{ + internal struct TypeNameParseOptions + { + public TypeNameParseOptions() { } +#pragma warning disable CA1822 // Mark members as static + // CoreLib does not enforce any limits + public bool IsMaxDepthExceeded(int _) => false; + public int MaxNodes + { + get + { + Debug.Fail("Expected to be unreachable"); + return 0; + } + } +#pragma warning restore CA1822 + } +} +#endif + namespace System.Reflection { internal partial struct TypeNameParser diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index fccffa63fa159..f1b61a1ea67c4 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -1424,6 +1424,9 @@ Common\System\LocalAppContextSwitches.Common.cs + + Common\System\HexConverter.cs + Common\System\HResults.cs @@ -1493,9 +1496,6 @@ Common\System\Reflection\Metadata\TypeNameParserHelpers.cs - - Common\System\Reflection\Metadata\TypeNameParserOptions.cs - Common\System\Runtime\Versioning\NonVersionableAttribute.cs @@ -1599,11 +1599,6 @@ - - - Common\System\HexConverter.cs - - Common\Interop\Windows\Advapi32\Interop.EncryptDecrypt.cs