[src] Disable a few code paths on macOS if dynamic registration is disabled. (#20178)

This makes the linker able to trim away a few methods that aren't trim-safe
when using the managed static registrar (and thus not warn about these methods
doing un-trimmable stuff).

Contributes towards #10405.
This commit is contained in:
Rolf Bjarne Kvinge 2024-02-28 09:01:24 +01:00 коммит произвёл GitHub
Родитель 90992e464b
Коммит ffb3041720
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 14 добавлений и 31 удалений

Просмотреть файл

@ -62,7 +62,12 @@ namespace AppKit {
initialized = true;
#if NET
if (Runtime.DynamicRegistrationSupported)
Runtime.RegisterAssemblies ();
#else
Runtime.RegisterAssemblies ();
#endif
// Runtime hosts embedding MonoMac may use a different sync context
// and call NSApplicationMain externally prior to this Init, so only

Просмотреть файл

@ -173,8 +173,13 @@ namespace ObjCRuntime {
static Assembly? ResolvingEventHandler (AssemblyLoadContext sender, AssemblyName assemblyName)
{
if (xamarin_locate_assembly_resource (assemblyName.Name!, assemblyName.CultureName, assemblyName.Name + ".dll", out var path))
return sender.LoadFromAssemblyPath (path);
if (xamarin_locate_assembly_resource (assemblyName.Name!, assemblyName.CultureName, assemblyName.Name + ".dll", out var path)) {
if (DynamicRegistrationSupported) {
return sender.LoadFromAssemblyPath (path);
}
log_coreclr ($" Resolved the assembly {assemblyName} to {path}, but dynamic registration is not enabled, so won't load the assembly.");
}
return null;
}
@ -250,6 +255,7 @@ namespace ObjCRuntime {
}
// Returns a retained MonoObject. Caller must release.
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "This method is only called to retrieve the assembly where the entry point is, and the entry point is not trimmed away, so this is safe.")]
static IntPtr FindAssembly (IntPtr assembly_name)
{
if (IsNativeAOT)

Просмотреть файл

@ -8,30 +8,7 @@ namespace Xamarin.Tests {
[TestCase (ApplePlatform.TVOS, "tvossimulator-x64")]
public void TrimmerWarningsManagedStaticRegistrar (ApplePlatform platform, string runtimeIdentifiers)
{
ExpectedBuildMessage [] expectedWarnings;
switch (platform) {
case ApplePlatform.iOS:
case ApplePlatform.TVOS:
expectedWarnings = new ExpectedBuildMessage [] {
};
break;
case ApplePlatform.MacOSX:
expectedWarnings = new ExpectedBuildMessage [] {
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.CoreCLR.cs" /* line 276 */, "ObjCRuntime.Runtime.FindAssembly(IntPtr): Using member 'System.Reflection.Assembly.LoadFrom(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types and members the loaded assembly depends on might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.CoreCLR.cs" /* line 177 */, "ObjCRuntime.Runtime.ResolvingEventHandler(AssemblyLoadContext, AssemblyName): Using member 'System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types and members the loaded assembly depends on might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 714 */, "ObjCRuntime.Runtime.CollectReferencedAssemblies(List<Assembly>, Assembly): Using member 'System.Reflection.Assembly.GetReferencedAssemblies()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Assembly references might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 648 */, "ObjCRuntime.Runtime.GetEntryAssembly(): Using member 'System.Reflection.Assembly.LoadFile(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types and members the loaded assembly depends on might be removed."),
};
break;
case ApplePlatform.MacCatalyst:
expectedWarnings = new ExpectedBuildMessage [] {
};
break;
default:
Assert.Fail ($"Unknown platform: {platform}");
return;
}
TrimmerWarnings (platform, runtimeIdentifiers, "managed-static", expectedWarnings);
TrimmerWarnings (platform, runtimeIdentifiers, "managed-static", Array.Empty<ExpectedBuildMessage> ());
}
[Test]
@ -68,12 +45,8 @@ namespace Xamarin.Tests {
new ExpectedBuildMessage ("src/ObjCRuntime/Blocks.cs" /* line 427 */, "ObjCRuntime.BlockLiteral.GetDelegateProxyType(MethodInfo, UInt32, MethodInfo&): Value passed to parameter 'interfaceType' of method 'System.Type.GetInterfaceMap(Type)' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements."),
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 541 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveMethod(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/Class.cs" /* line 528 */, "ObjCRuntime.Class.ResolveToken(Assembly, Module, UInt32): Using member 'System.Reflection.Module.ResolveType(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Trimming changes metadata tokens."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.CoreCLR.cs" /* line 276 */, "ObjCRuntime.Runtime.FindAssembly(IntPtr): Using member 'System.Reflection.Assembly.LoadFrom(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types and members the loaded assembly depends on might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.CoreCLR.cs" /* line 177 */, "ObjCRuntime.Runtime.ResolvingEventHandler(AssemblyLoadContext, AssemblyName): Using member 'System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types and members the loaded assembly depends on might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 714 */, "ObjCRuntime.Runtime.CollectReferencedAssemblies(List<Assembly>, Assembly): Using member 'System.Reflection.Assembly.GetReferencedAssemblies()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Assembly references might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 2434 */, "ObjCRuntime.Runtime.FindClosedMethod(Type, MethodBase): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'closed_type' of method 'ObjCRuntime.Runtime.FindClosedMethod(Type, MethodBase)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 1279 */, "ObjCRuntime.Runtime.FindPropertyInfo(MethodInfo): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'System.Reflection.MemberInfo.DeclaringType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 648 */, "ObjCRuntime.Runtime.GetEntryAssembly(): Using member 'System.Reflection.Assembly.LoadFile(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types and members the loaded assembly depends on might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 1673 */, "ObjCRuntime.Runtime.GetIntPtr_BoolConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'ObjCRuntime.Runtime.GetIntPtr_BoolConstructor(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 1623 */, "ObjCRuntime.Runtime.GetIntPtrConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter 'type' of method 'ObjCRuntime.Runtime.GetIntPtrConstructor(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
};
@ -181,7 +154,6 @@ namespace Xamarin.Tests {
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 499 */, "Registrar.DynamicRegistrar.VerifyIsConstrainedToNSObject(Type, Type&): Call to 'System.Type.MakeGenericType(Type[])' can not be statically analyzed. It's not possible to guarantee the availability of requirements of the generic type."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 33 */, "Registrar.SharedDynamic.PrepareInterfaceMethodMapping(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.FindInterfaces(TypeFilter, Object)'. The parameter 'type' of method 'Registrar.SharedDynamic.PrepareInterfaceMethodMapping(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 39 */, "Registrar.SharedDynamic.PrepareInterfaceMethodMapping(Type): Value passed to parameter 'interfaceType' of method 'System.Type.GetInterfaceMap(Type)' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.CoreCLR.cs" /* line 276 */, "ObjCRuntime.Runtime.FindAssembly(IntPtr): Using member 'System.Reflection.Assembly.LoadFrom(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types and members the loaded assembly depends on might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.CoreCLR.cs" /* line 177 */, "ObjCRuntime.Runtime.ResolvingEventHandler(AssemblyLoadContext, AssemblyName): Using member 'System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types and members the loaded assembly depends on might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 714 */, "ObjCRuntime.Runtime.CollectReferencedAssemblies(List<Assembly>, Assembly): Using member 'System.Reflection.Assembly.GetReferencedAssemblies()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Assembly references might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 2434 */, "ObjCRuntime.Runtime.FindClosedMethod(Type, MethodBase): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter 'closed_type' of method 'ObjCRuntime.Runtime.FindClosedMethod(Type, MethodBase)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),