-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Enable setting the TypeMap entrypoint assembly via a RuntimeHostConfigurationOption #121513
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
base: main
Are you sure you want to change the base?
Changes from all commits
0edbd53
cd198c0
fe74d8a
bc4753d
8a2335c
0d42667
90e9f40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -189,7 +189,16 @@ private static unsafe CallbackContext CreateMaps( | |||||||||||||||||||||||||||||
| delegate* unmanaged<CallbackContext*, ProcessAttributesCallbackArg*, Interop.BOOL> newExternalTypeEntry, | ||||||||||||||||||||||||||||||
| delegate* unmanaged<CallbackContext*, ProcessAttributesCallbackArg*, Interop.BOOL> newProxyTypeEntry) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| RuntimeAssembly? startingAssembly = (RuntimeAssembly?)Assembly.GetEntryAssembly(); | ||||||||||||||||||||||||||||||
| RuntimeAssembly? startingAssembly; | ||||||||||||||||||||||||||||||
| if (AppContext.GetData("System.Runtime.InteropServices.TypeMappingEntryAssembly") is string entryAssemblyName) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| startingAssembly = Assembly.Load(entryAssemblyName) as RuntimeAssembly; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| startingAssembly = Assembly.Load(entryAssemblyName) as RuntimeAssembly; | |
| try | |
| { | |
| startingAssembly = Assembly.Load(entryAssemblyName) as RuntimeAssembly; | |
| } | |
| catch (Exception ex) when ( | |
| ex is FileNotFoundException || | |
| ex is FileLoadException || | |
| ex is BadImageFormatException) | |
| { | |
| throw new InvalidOperationException( | |
| $"Failed to load assembly '{entryAssemblyName}' specified by 'System.Runtime.InteropServices.TypeMappingEntryAssembly'.", | |
| ex); | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| // This library defines TypeMap entries that should be used when | ||
| // TypeMapLib5 is specified as the TypeMappingEntryAssembly | ||
| [assembly: TypeMap<AlternateEntryPoint>("lib5_type1", typeof(Lib5Type1))] | ||
| [assembly: TypeMap<AlternateEntryPoint>("lib5_type2", typeof(Lib5Type2))] | ||
|
|
||
| [assembly: TypeMapAssociation<AlternateEntryPoint>(typeof(Lib5Type1), typeof(Lib5Proxy1))] | ||
| [assembly: TypeMapAssociation<AlternateEntryPoint>(typeof(Lib5Type2), typeof(Lib5Proxy2))] | ||
|
|
||
| public class Lib5Type1 { } | ||
| public class Lib5Type2 { } | ||
| public class Lib5Proxy1 { } | ||
| public class Lib5Proxy2 { } | ||
|
|
||
| public class AlternateEntryPoint { } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Runtime.InteropServices; | ||
| using Xunit; | ||
|
|
||
| // Note: This test does NOT define any TypeMap attributes. | ||
| // The TypeMappingEntryAssembly is set to TypeMapLib5 via RuntimeHostConfigurationOption, | ||
| // so the type maps should be resolved from TypeMapLib5 instead of this assembly. | ||
|
|
||
| public class TypeMapEntryAssemblyTest | ||
| { | ||
| [Fact] | ||
| public static void Validate_TypeMappingEntryAssembly_ExternalMap() | ||
| { | ||
| Console.WriteLine(nameof(Validate_TypeMappingEntryAssembly_ExternalMap)); | ||
|
|
||
| // These types should be resolved from TypeMapLib5's type maps | ||
| IReadOnlyDictionary<string, Type> externalMap = TypeMapping.GetOrCreateExternalTypeMapping<AlternateEntryPoint>(); | ||
|
|
||
| Assert.Equal(typeof(Lib5Type1), externalMap["lib5_type1"]); | ||
| Assert.Equal(typeof(Lib5Type2), externalMap["lib5_type2"]); | ||
|
|
||
| Assert.True(externalMap.TryGetValue("lib5_type1", out Type? type1)); | ||
| Assert.Equal(typeof(Lib5Type1), type1); | ||
|
|
||
| Assert.True(externalMap.TryGetValue("lib5_type2", out Type? type2)); | ||
| Assert.Equal(typeof(Lib5Type2), type2); | ||
|
|
||
| Assert.False(externalMap.TryGetValue("nonexistent", out Type? _)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void Validate_TypeMappingEntryAssembly_ProxyMap() | ||
| { | ||
| Console.WriteLine(nameof(Validate_TypeMappingEntryAssembly_ProxyMap)); | ||
|
|
||
| // These proxy mappings should be resolved from TypeMapLib5's type maps | ||
| IReadOnlyDictionary<Type, Type> proxyMap = TypeMapping.GetOrCreateProxyTypeMapping<AlternateEntryPoint>(); | ||
|
|
||
| Assert.Equal(typeof(Lib5Proxy1), proxyMap[typeof(Lib5Type1)]); | ||
| Assert.Equal(typeof(Lib5Proxy2), proxyMap[typeof(Lib5Type2)]); | ||
|
|
||
| Assert.True(proxyMap.TryGetValue(typeof(Lib5Type1), out Type? proxy1)); | ||
| Assert.Equal(typeof(Lib5Proxy1), proxy1); | ||
|
|
||
| Assert.True(proxyMap.TryGetValue(typeof(Lib5Type2), out Type? proxy2)); | ||
| Assert.Equal(typeof(Lib5Proxy2), proxy2); | ||
|
|
||
| Assert.False(proxyMap.TryGetValue(typeof(string), out Type? _)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
| <PropertyGroup> | ||||
| <OutputType>Exe</OutputType> | ||||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||||
| <RequiresProcessIsolation>true</RequiresProcessIsolation> | ||||
| <TypeMapEntryAssembly>TypeMapLib5</TypeMapEntryAssembly> | ||||
| <MonoAotIncompatible>true</MonoAotIncompatible> | ||||
| <DisableProjectBuild Condition="'$(RuntimeFlavor)' == 'mono'">true</DisableProjectBuild> | ||||
| <IlcGenerateMstatFile>true</IlcGenerateMstatFile> | ||||
| <IlcGenerateDgmlFile>true</IlcGenerateDgmlFile> | ||||
| <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> | ||||
| </PropertyGroup> | ||||
|
|
||||
| <ItemGroup> | ||||
| <Compile Include="TypeMapEntryAssemblyApp.cs" /> | ||||
| </ItemGroup> | ||||
|
|
||||
| <ItemGroup> | ||||
| <ProjectReference Include="TypeMapLib5.csproj" /> | ||||
| <ProjectReference Include="TypeMapLib1.csproj" /> | ||||
| </ItemGroup> | ||||
|
|
||||
| <ItemGroup> | ||||
| <RuntimeHostConfigurationOption Include="System.Runtime.InteropServices.TypeMappingEntryAssembly" | ||||
| Value="$(TypeMapEntryAssembly)" | ||||
| Condition="'$(TypeMapEntryAssembly)' != ''" | ||||
|
||||
| Condition="'$(TypeMapEntryAssembly)' != ''" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="Lib5.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="TypeMapLib1.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -254,7 +254,18 @@ protected virtual void Initialize() | |||||||||||||||||
| { | ||||||||||||||||||
| InitializeCorelibAttributeXml(); | ||||||||||||||||||
| Context.Pipeline.InitializeMarkHandlers(Context, MarkContext); | ||||||||||||||||||
| _typeMapHandler.Initialize(Context, this, Annotations.GetEntryPointAssembly()); | ||||||||||||||||||
|
|
||||||||||||||||||
| // Check for TypeMappingEntryAssembly override from Features | ||||||||||||||||||
| AssemblyDefinition? startingAssembly = null; | ||||||||||||||||||
| if (Context.Features.TryGetValue("System.Runtime.InteropServices.TypeMappingEntryAssembly", out string? assemblyNameString)) | ||||||||||||||||||
| { | ||||||||||||||||||
| var assemblyName = AssemblyNameReference.Parse(assemblyNameString); | ||||||||||||||||||
| startingAssembly = Context.TryResolve(assemblyName); | ||||||||||||||||||
|
||||||||||||||||||
| startingAssembly = Context.TryResolve(assemblyName); | |
| startingAssembly = Context.TryResolve(assemblyName); | |
| if (startingAssembly is null) | |
| { | |
| Context.LogWarning( | |
| $"Could not resolve assembly '{assemblyNameString}' specified by the 'System.Runtime.InteropServices.TypeMappingEntryAssembly' feature. Falling back to the entry point assembly.", | |
| origin: null); | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using Mono.Linker.Tests.Cases.Reflection.Dependencies; | ||
|
|
||
| [assembly: TypeMap<TypeMapEntryAssemblyGroup>("entry_type1", typeof(EntryType1))] | ||
| [assembly: TypeMap<TypeMapEntryAssemblyGroup>("entry_type2", typeof(EntryType2))] | ||
|
|
||
| [assembly: TypeMapAssociation<TypeMapEntryAssemblyGroup>(typeof(EntrySource1), typeof(EntryProxy1))] | ||
| [assembly: TypeMapAssociation<TypeMapEntryAssemblyGroup>(typeof(EntrySource2), typeof(EntryProxy2))] | ||
|
|
||
| namespace Mono.Linker.Tests.Cases.Reflection.Dependencies | ||
| { | ||
| public class TypeMapEntryAssemblyGroup { } | ||
|
|
||
| public class EntryType1 { } | ||
| public class EntryType2 { } | ||
|
|
||
| public class EntrySource1 { } | ||
| public class EntrySource2 { } | ||
|
|
||
| public class EntryProxy1 { } | ||
| public class EntryProxy2 { } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Licensed to the .NET Foundation and contributors. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using Mono.Linker.Tests.Cases.Expectations.Assertions; | ||
| using Mono.Linker.Tests.Cases.Expectations.Metadata; | ||
| using Mono.Linker.Tests.Cases.Reflection.Dependencies; | ||
|
|
||
| namespace Mono.Linker.Tests.Cases.Reflection | ||
| { | ||
| [SetupCompileBefore("TypeMapEntryAssemblyLib.dll", new[] { "Dependencies/TypeMapEntryAssemblyLib.cs" })] | ||
| #if NATIVEAOT | ||
| [SetupLinkerArgument("--runtimeknob", "System.Runtime.InteropServices.TypeMappingEntryAssembly", "TypeMapEntryAssemblyLib")] | ||
| #else | ||
| [SetupLinkerArgument("--feature", "System.Runtime.InteropServices.TypeMappingEntryAssembly", "TypeMapEntryAssemblyLib")] | ||
| #endif | ||
|
|
||
| // The TypeMapEntryAssemblyGroup is defined in TypeMapEntryAssemblyLib, so its attributes should be kept | ||
| [KeptTypeInAssembly("TypeMapEntryAssemblyLib.dll", typeof(TypeMapEntryAssemblyGroup))] | ||
| [KeptTypeInAssembly("TypeMapEntryAssemblyLib.dll", typeof(EntryType1))] | ||
| [KeptTypeInAssembly("TypeMapEntryAssemblyLib.dll", typeof(EntryType2))] | ||
| [KeptTypeInAssembly("TypeMapEntryAssemblyLib.dll", typeof(EntrySource1))] | ||
| [KeptTypeInAssembly("TypeMapEntryAssemblyLib.dll", typeof(EntrySource2))] | ||
| [KeptTypeInAssembly("TypeMapEntryAssemblyLib.dll", typeof(EntryProxy1))] | ||
| [KeptTypeInAssembly("TypeMapEntryAssemblyLib.dll", typeof(EntryProxy2))] | ||
|
|
||
| [KeptAttributeInAssembly("TypeMapEntryAssemblyLib.dll", typeof(TypeMapAttribute<TypeMapEntryAssemblyGroup>))] | ||
| [KeptAttributeInAssembly("TypeMapEntryAssemblyLib.dll", typeof(TypeMapAssociationAttribute<TypeMapEntryAssemblyGroup>))] | ||
|
|
||
| public class TypeMapEntryAssembly | ||
| { | ||
| public static void Main() | ||
| { | ||
| // Access the type map to ensure it gets used | ||
| var externalMap = TypeMapping.GetOrCreateExternalTypeMapping<TypeMapEntryAssemblyGroup>(); | ||
| var proxyMap = TypeMapping.GetOrCreateProxyTypeMapping<TypeMapEntryAssemblyGroup>(); | ||
|
|
||
| Console.WriteLine(externalMap); | ||
| Console.WriteLine(proxyMap); | ||
| _ = new EntrySource1(); | ||
| _ = new EntrySource2(); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.