Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection.Metadata;

using Internal.TypeSystem;
using Internal.TypeSystem.Ecma;

using DependencyList = ILCompiler.DependencyAnalysisFramework.DependencyNodeCore<ILCompiler.DependencyAnalysis.NodeFactory>.DependencyList;

namespace ILCompiler.DependencyAnalysis
{
internal static class ReflectionInvokeSupportDependencyAlgorithm
{
// Inserts dependencies to make the following corner case work (we need MethodTable for `MyStruct[]`):
//
// struct MyStruct
// {
// public static int Count(params MyStruct[] myStructs)
// {
// return myStructs.Length;
// }
//
// public static void Main()
// {
// typeof(MyStruct).InvokeMember(nameof(Count), BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object[] { default(MyStruct) });
// }
// }
public static void GetDependenciesFromParamsArray(ref DependencyList dependencies, NodeFactory factory, MethodDesc method)
{
MethodSignature sig = method.Signature;
if (sig.Length < 1 || !sig[sig.Length - 1].IsArray)
return;

if (method.GetTypicalMethodDefinition() is not EcmaMethod ecmaMethod)
return;

MetadataReader reader = ecmaMethod.MetadataReader;
MethodDefinition methodDef = reader.GetMethodDefinition(ecmaMethod.Handle);

foreach (ParameterHandle paramHandle in methodDef.GetParameters())
{
Parameter param = reader.GetParameter(paramHandle);
if (param.SequenceNumber == sig.Length /* SequenceNumber is 1-based */)
{
if (!reader.GetCustomAttributeHandle(param.GetCustomAttributes(), "System", "ParamArrayAttribute").IsNil)
{
dependencies ??= new DependencyList();
dependencies.Add(
factory.ConstructedTypeSymbol(sig[sig.Length - 1].NormalizeInstantiation()),
"Reflection invoke");
}

break;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ public void GetDependenciesDueToReflectability(ref DependencyList dependencies,
{
// We're going to generate a mapping table entry for this. Collect dependencies.
ReflectionInvokeMapNode.AddDependenciesDueToReflectability(ref dependencies, factory, method);

ReflectionInvokeSupportDependencyAlgorithm.GetDependenciesFromParamsArray(ref dependencies, factory, method);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
<Compile Include="Compiler\DependencyAnalysis\MethodExceptionHandlingInfoNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\ModuleInitializerListNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\ObjectGetTypeFlowDependenciesNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\ReflectionInvokeSupportDependencyAlgorithm.cs" />
<Compile Include="Compiler\DependencyAnalysis\ReflectionMethodBodyScanner.cs" />
<Compile Include="Compiler\DependencyAnalysis\StructMarshallingDataNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\Target_ARM64\ARM64TentativeMethodNode.cs" />
Expand Down
21 changes: 21 additions & 0 deletions src/tests/nativeaot/SmokeTests/Reflection/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private static int Main()
TestGetUninitializedObject.Run();
TestInstanceFields.Run();
TestReflectionInvoke.Run();
TestInvokeMemberParamsCornerCase.Run();
TestDefaultInterfaceInvoke.Run();
TestCovariantReturnInvoke.Run();
#if !CODEGEN_CPP
Expand Down Expand Up @@ -264,6 +265,26 @@ public static unsafe void Run()
}
}

class TestInvokeMemberParamsCornerCase
{
public struct MyStruct { }

public static int Count(params MyStruct[] myStructs)
{
return myStructs.Length;
}

public static void Run()
{
Console.WriteLine(nameof(TestInvokeMemberParamsCornerCase));

// Needs MethodTable for MyStruct[] and the compiler should have created it.
typeof(TestInvokeMemberParamsCornerCase).InvokeMember(nameof(Count),
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
null, null, new object[] { default(MyStruct) });
}
}

class TestDefaultInterfaceInvoke
{
interface IFoo<T>
Expand Down