Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexOutOfRangeException in WinMD assembly resolution #58319

Closed
andrew-boyarshin opened this issue Aug 28, 2021 · 1 comment
Closed

IndexOutOfRangeException in WinMD assembly resolution #58319

andrew-boyarshin opened this issue Aug 28, 2021 · 1 comment

Comments

@andrew-boyarshin
Copy link
Contributor

Description

<PackageReference Include="System.Reflection.MetadataLoadContext" Version="6.0.0-preview.7.21377.19" />
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

// Get the array of runtime assemblies.
// This will allow us to at least inspect types depending only on BCL.
var runtimeAssemblies = Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll");

// Create the list of assembly paths consisting of runtime assemblies and the input file.
const string? metadataFile = @"C:\Windows\System32\WinMetadata\Windows.Foundation.winmd";
List<string> paths = new(runtimeAssemblies.Append(metadataFile));

// Create MetadataLoadContext that can resolve assemblies using the created list.
PathAssemblyResolver resolver = new(paths);
MetadataLoadContext mlc = new(resolver);

foreach (var type in mlc.LoadFromAssemblyPath(metadataFile).GetTypes())
{
    var _ = type.IsValueType;
}

AssemblyReferenceHandle is "virtual" (Treatment is TypeRefTreatment.SystemDelegate, that gives AssemblyReferenceHandle.VirtualIndex.System_Runtime (0) RowID), but EcmaResolver is not prepared to handle that properly, instead, it tries to use AssemblyRefTable (MetadataTable), which obviously fails.

A bit of context:

case HandleKind.AssemblyReference:
{
AssemblyReferenceHandle arh = (AssemblyReferenceHandle)scope;
RoAssembly assembly = arh.ResolveAssembly(module);

public T GetOrAdd(EntityHandle handle, C context, Func<EntityHandle, C, T> factory)
{
Debug.Assert(!handle.IsNil);
Debug.Assert(factory != null);
int index = handle.GetToken().GetTokenRowNumber() - 1;
T?[] table = _table;
T? result = Volatile.Read(ref table[index]);

Configuration

.NET SDK 6.0.100-rc.2.21428.3, Windows 10.0.22000 x64

Regression?

Unlikely, but I don't know for sure.

Other information

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Reflection.TypeLoading.Ecma.MetadataTable`2.GetOrAdd(EntityHandle handle, C context, Func`3 factory)
   at System.Reflection.TypeLoading.Ecma.EcmaResolver.ResolveToAssemblyOrExceptionAssembly(AssemblyReferenceHandle handle, EcmaModule module)
   at System.Reflection.TypeLoading.Ecma.EcmaResolver.TryResolveAssembly(AssemblyReferenceHandle handle, EcmaModule module, Exception& e)
   at System.Reflection.TypeLoading.Ecma.EcmaResolver.ResolveAssembly(AssemblyReferenceHandle handle, EcmaModule module)
   at System.Reflection.TypeLoading.Ecma.EcmaResolver.ComputeTypeRefResolution(TypeReferenceHandle handle, EcmaModule module)
   at System.Reflection.TypeLoading.Ecma.EcmaResolver.<>c.<.cctor>b__15_1(EntityHandle h, EcmaModule m)
   at System.Reflection.TypeLoading.Ecma.MetadataTable`2.GetOrAdd(EntityHandle handle, C context, Func`3 factory)
   at System.Reflection.TypeLoading.Ecma.EcmaResolver.ResolveTypeRef(TypeReferenceHandle handle, EcmaModule module)
   at System.Reflection.TypeLoading.Ecma.EcmaResolver.ResolveTypeDefRefOrSpec(EntityHandle handle, EcmaModule module, TypeContext& typeContext)
   at System.Reflection.TypeLoading.Ecma.EcmaDefinitionType.SpecializeBaseType(RoType[] instantiation)
   at System.Reflection.TypeLoading.RoDefinitionType.ComputeBaseTypeWithoutDesktopQuirk()
   at System.Reflection.TypeLoading.RoType.ComputeBaseType()
   at System.Reflection.TypeLoading.RoType.GetRoBaseType()
   at System.Reflection.TypeLoading.RoType.get_BaseType()
   at System.Reflection.TypeLoading.RoType.ComputeBaseTypeClassification()
   at System.Reflection.TypeLoading.RoType.GetBaseTypeClassification()
   at System.Reflection.TypeLoading.RoType.IsValueTypeImpl()
   at System.Type.get_IsValueType()
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Reflection untriaged New issue has not been triaged by the area owner labels Aug 28, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 30, 2021
@buyaa-n buyaa-n added the bug label Sep 2, 2021
@buyaa-n buyaa-n added this to the 7.0.0 milestone Sep 2, 2021
@buyaa-n buyaa-n removed the untriaged New issue has not been triaged by the area owner label Sep 2, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Dec 8, 2021
@steveharter
Copy link
Member

We do not plan on supporting WinMD per #58344 (comment)

@ghost ghost locked as resolved and limited conversation to collaborators Aug 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

3 participants