diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/NativeFormat/NativeFormatRuntimeNamedTypeInfo.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/NativeFormat/NativeFormatRuntimeNamedTypeInfo.cs index 65f2c3597ecc21..2a0fc6f176cfbf 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/NativeFormat/NativeFormatRuntimeNamedTypeInfo.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/NativeFormat/NativeFormatRuntimeNamedTypeInfo.cs @@ -79,7 +79,11 @@ public sealed override bool IsByRefLike continue; if (guidStringArgumentHandle.HandleType != HandleType.ConstantStringValue) continue; - return new Guid(guidStringArgumentHandle.ToConstantStringValueHandle(_reader).GetString(_reader)); + + ConstantStringValueHandle constantStringValueHandle = guidStringArgumentHandle.ToConstantStringValueHandle(_reader); + + // Parse a 'Guid' directly from the encoded UTF8 buffer, instead of round-tripping through a 'string' + return Guid.Parse(_reader.ReadStringAsBytes(constantStringValueHandle)); } } return null; diff --git a/src/coreclr/tools/Common/Internal/Metadata/NativeFormat/NativeMetadataReader.cs b/src/coreclr/tools/Common/Internal/Metadata/NativeFormat/NativeMetadataReader.cs index 39d22fa8d2dd71..2379832cb73c9a 100644 --- a/src/coreclr/tools/Common/Internal/Metadata/NativeFormat/NativeMetadataReader.cs +++ b/src/coreclr/tools/Common/Internal/Metadata/NativeFormat/NativeMetadataReader.cs @@ -220,6 +220,16 @@ internal bool StringEquals(ConstantStringValueHandle handle, string value) { return _streamReader.StringEquals((uint)handle.Offset, value); } + + internal ReadOnlySpan ReadStringAsBytes(ConstantStringValueHandle handle) + { + if (handle.IsNil) + { + return []; + } + + return _streamReader.ReadStringAsBytes((uint)handle.Offset); + } } internal sealed partial class MetadataHeader diff --git a/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatReader.String.cs b/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatReader.String.cs index e3b90b71132bf8..4e1cab18f5e917 100644 --- a/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatReader.String.cs +++ b/src/coreclr/tools/Common/Internal/NativeFormat/NativeFormatReader.String.cs @@ -7,6 +7,7 @@ // UTF8 string reading methods // --------------------------------------------------------------------------- +using System; using System.Text; namespace Internal.NativeFormat @@ -35,6 +36,23 @@ public string ReadString(uint offset) return value; } + public unsafe ReadOnlySpan ReadStringAsBytes(uint offset) + { + uint numBytes; + offset = DecodeUnsigned(offset, out numBytes); + + if (numBytes != 0) + { + uint endOffset = offset + numBytes; + if (endOffset < numBytes || endOffset > _size) + ThrowBadImageFormatException(); + + return new(_base + offset, (int)numBytes); + } + + return []; + } + public unsafe uint DecodeString(uint offset, out string value) { uint numBytes;