From a3f25c581892de970bd7c5e5a72b767b2fe29eb2 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Thu, 31 Aug 2023 14:28:07 +0200 Subject: [PATCH 1/5] Implement NSObject.RegisterToggleRef stub for NativeAOT to fix warning --- tools/dotnet-linker/AppBundleRewriter.cs | 6 ++++++ .../Steps/ManagedRegistrarStep.cs | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/tools/dotnet-linker/AppBundleRewriter.cs b/tools/dotnet-linker/AppBundleRewriter.cs index 7dfbeb340863..b846069ce929 100644 --- a/tools/dotnet-linker/AppBundleRewriter.cs +++ b/tools/dotnet-linker/AppBundleRewriter.cs @@ -370,6 +370,12 @@ public MethodReference Foundation_NSObject_FlagsSetterMethod { return GetMethodReference (PlatformAssembly, Foundation_NSObject, "set_flags", "Foundation.NSObject::set_flags", predicate: null, out var _); } } + + public MethodReference Foundation_NSObject_RegisterToggleRef { + get { + return GetMethodReference (PlatformAssembly, Foundation_NSObject, "RegisterToggleRef", "Foundation.NSObject::RegisterToggleRef", predicate: null, out var _); + } + } #else public FieldReference Foundation_NSObject_FlagsField { get { diff --git a/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs b/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs index 6af5a4915df6..4997c802ba70 100644 --- a/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs +++ b/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs @@ -166,6 +166,8 @@ protected override void TryProcessAssembly (AssemblyDefinition assembly) var md = abr.RegistrarHelper_RuntimeTypeHandleEquals.Resolve (); md.IsPublic = true; Annotations.Mark (md); + } else if (App.XamarinRuntime == XamarinRuntime.NativeAOT && Configuration.Profile.IsProductAssembly (assembly)) { + ImplementNSObjectRegisterToggleRefMethodStub (); } abr.ClearCurrentAssembly (); @@ -1376,5 +1378,21 @@ MethodDefinition CloneConstructorWithNativeHandle (MethodDefinition ctor) return clonedCtor; } + + void ImplementNSObjectRegisterToggleRefMethodStub () + { + // The NSObject.RegisterToggleRef method is a Mono icall that is unused in NativeAOT + // and we need to modify it so that ILC doesn't report the following warning: + // + // ILC: Method '[Microsoft.iOS]Foundation.NSObject.RegisterToggleRef(NSObject,native int,bool)' will always throw because: + // Invalid IL or CLR metadata in 'Void Foundation.NSObject.RegisterToggleRef(Foundation.NSObject, IntPtr, Boolean)' + // + var registerToggleRef = abr.Foundation_NSObject_RegisterToggleRef.Resolve (); + registerToggleRef.IsPublic = false; + registerToggleRef.IsInternalCall = false; + + registerToggleRef.CreateBody(out var il); + il.Emit (OpCodes.Ret); + } } } From 7c6c68d691bea786d2360fbf8cfd8b670ee38c91 Mon Sep 17 00:00:00 2001 From: GitHub Actions Autoformatter Date: Thu, 31 Aug 2023 12:43:00 +0000 Subject: [PATCH 2/5] Auto-format source code --- tools/dotnet-linker/Steps/ManagedRegistrarStep.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs b/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs index 4997c802ba70..174560e6f7dc 100644 --- a/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs +++ b/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs @@ -1391,7 +1391,7 @@ void ImplementNSObjectRegisterToggleRefMethodStub () registerToggleRef.IsPublic = false; registerToggleRef.IsInternalCall = false; - registerToggleRef.CreateBody(out var il); + registerToggleRef.CreateBody (out var il); il.Emit (OpCodes.Ret); } } From a9db400036adc2b49da61668a0565a665c6662ab Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 5 Sep 2023 09:49:39 +0200 Subject: [PATCH 3/5] Do not try to implement the RegisterToggleRef stub on Mac --- tools/dotnet-linker/AppBundleRewriter.cs | 19 +++++++++++++------ .../Steps/ManagedRegistrarStep.cs | 16 +++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/tools/dotnet-linker/AppBundleRewriter.cs b/tools/dotnet-linker/AppBundleRewriter.cs index b846069ce929..d583bdde8047 100644 --- a/tools/dotnet-linker/AppBundleRewriter.cs +++ b/tools/dotnet-linker/AppBundleRewriter.cs @@ -370,12 +370,6 @@ public MethodReference Foundation_NSObject_FlagsSetterMethod { return GetMethodReference (PlatformAssembly, Foundation_NSObject, "set_flags", "Foundation.NSObject::set_flags", predicate: null, out var _); } } - - public MethodReference Foundation_NSObject_RegisterToggleRef { - get { - return GetMethodReference (PlatformAssembly, Foundation_NSObject, "RegisterToggleRef", "Foundation.NSObject::RegisterToggleRef", predicate: null, out var _); - } - } #else public FieldReference Foundation_NSObject_FlagsField { get { @@ -1226,6 +1220,19 @@ public MethodReference Unsafe_AsRef { } } +#if NET + public bool TryGet_NSObject_RegisterToggleRef(out MethodDefinition? md) { + // the NSObject.RegisterToggleRef method isn't present on all platforms (for example on Mac) + try { + _ = GetMethodReference (PlatformAssembly, Foundation_NSObject, "RegisterToggleRef", "Foundation.NSObject::RegisterToggleRef", predicate: null, out md); + return true; + } catch (InvalidOperationException) { + md = null; + return false; + } + } +#endif + public void SetCurrentAssembly (AssemblyDefinition value) { current_assembly = value; diff --git a/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs b/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs index 174560e6f7dc..0a6790fd52df 100644 --- a/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs +++ b/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs @@ -1381,18 +1381,20 @@ MethodDefinition CloneConstructorWithNativeHandle (MethodDefinition ctor) void ImplementNSObjectRegisterToggleRefMethodStub () { - // The NSObject.RegisterToggleRef method is a Mono icall that is unused in NativeAOT - // and we need to modify it so that ILC doesn't report the following warning: + // The NSObject.RegisterToggleRef method is a Mono icall that is unused in NativeAOT. + // The method isn't included on all platforms but when it is present, we need to modify it + // so that ILC can trim it and it doesn't report the following warning: // // ILC: Method '[Microsoft.iOS]Foundation.NSObject.RegisterToggleRef(NSObject,native int,bool)' will always throw because: // Invalid IL or CLR metadata in 'Void Foundation.NSObject.RegisterToggleRef(Foundation.NSObject, IntPtr, Boolean)' // - var registerToggleRef = abr.Foundation_NSObject_RegisterToggleRef.Resolve (); - registerToggleRef.IsPublic = false; - registerToggleRef.IsInternalCall = false; + if (abr.TryGet_NSObject_RegisterToggleRef(out var registerToggleRef)) { + registerToggleRef!.IsPublic = false; + registerToggleRef!.IsInternalCall = false; - registerToggleRef.CreateBody (out var il); - il.Emit (OpCodes.Ret); + registerToggleRef!.CreateBody(out var il); + il.Emit (OpCodes.Ret); + } } } } From 81fab74ea739b146751918b8eddab39eaf3f732b Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 5 Sep 2023 09:56:11 +0200 Subject: [PATCH 4/5] Add a TODO comment --- tools/dotnet-linker/Steps/ManagedRegistrarStep.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs b/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs index 0a6790fd52df..50a9d462648a 100644 --- a/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs +++ b/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs @@ -166,7 +166,10 @@ protected override void TryProcessAssembly (AssemblyDefinition assembly) var md = abr.RegistrarHelper_RuntimeTypeHandleEquals.Resolve (); md.IsPublic = true; Annotations.Mark (md); - } else if (App.XamarinRuntime == XamarinRuntime.NativeAOT && Configuration.Profile.IsProductAssembly (assembly)) { + } + + // TODO: Move this to a separate "MakeEverythingWorkWithNativeAOTStep" linker step + if (App.XamarinRuntime == XamarinRuntime.NativeAOT && Configuration.Profile.IsProductAssembly (assembly)) { ImplementNSObjectRegisterToggleRefMethodStub (); } From c4bca2093c1668955400a51c379b7235ea842ef6 Mon Sep 17 00:00:00 2001 From: GitHub Actions Autoformatter Date: Tue, 5 Sep 2023 09:11:39 +0000 Subject: [PATCH 5/5] Auto-format source code --- tools/dotnet-linker/AppBundleRewriter.cs | 3 ++- tools/dotnet-linker/Steps/ManagedRegistrarStep.cs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/dotnet-linker/AppBundleRewriter.cs b/tools/dotnet-linker/AppBundleRewriter.cs index d583bdde8047..8f29f8fed6b4 100644 --- a/tools/dotnet-linker/AppBundleRewriter.cs +++ b/tools/dotnet-linker/AppBundleRewriter.cs @@ -1221,7 +1221,8 @@ public MethodReference Unsafe_AsRef { } #if NET - public bool TryGet_NSObject_RegisterToggleRef(out MethodDefinition? md) { + public bool TryGet_NSObject_RegisterToggleRef (out MethodDefinition? md) + { // the NSObject.RegisterToggleRef method isn't present on all platforms (for example on Mac) try { _ = GetMethodReference (PlatformAssembly, Foundation_NSObject, "RegisterToggleRef", "Foundation.NSObject::RegisterToggleRef", predicate: null, out md); diff --git a/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs b/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs index 50a9d462648a..43164d229366 100644 --- a/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs +++ b/tools/dotnet-linker/Steps/ManagedRegistrarStep.cs @@ -1391,11 +1391,11 @@ void ImplementNSObjectRegisterToggleRefMethodStub () // ILC: Method '[Microsoft.iOS]Foundation.NSObject.RegisterToggleRef(NSObject,native int,bool)' will always throw because: // Invalid IL or CLR metadata in 'Void Foundation.NSObject.RegisterToggleRef(Foundation.NSObject, IntPtr, Boolean)' // - if (abr.TryGet_NSObject_RegisterToggleRef(out var registerToggleRef)) { + if (abr.TryGet_NSObject_RegisterToggleRef (out var registerToggleRef)) { registerToggleRef!.IsPublic = false; registerToggleRef!.IsInternalCall = false; - registerToggleRef!.CreateBody(out var il); + registerToggleRef!.CreateBody (out var il); il.Emit (OpCodes.Ret); } }