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

[NativeAOT] Fix ILC warning for NSObject.RegisterToggleRef #18889

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions tools/dotnet-linker/AppBundleRewriter.cs
Expand Up @@ -1220,6 +1220,20 @@ public FieldReference GetFieldReference (AssemblyDefinition assembly, TypeRefere
}
}

#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;
Expand Down
23 changes: 23 additions & 0 deletions tools/dotnet-linker/Steps/ManagedRegistrarStep.cs
Expand Up @@ -168,6 +168,11 @@ protected override void TryProcessAssembly (AssemblyDefinition assembly)
Annotations.Mark (md);
}

// TODO: Move this to a separate "MakeEverythingWorkWithNativeAOTStep" linker step
if (App.XamarinRuntime == XamarinRuntime.NativeAOT && Configuration.Profile.IsProductAssembly (assembly)) {
ImplementNSObjectRegisterToggleRefMethodStub ();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really fit with the managed static registrar, but I think it's good for now.

At some point we might want to move random stuff out of the managed static registrar and into a "MakeEverythingWorkWithNativeAOTStep"...

}

abr.ClearCurrentAssembly ();
}

Expand Down Expand Up @@ -1376,5 +1381,23 @@ MethodDefinition CloneConstructorWithNativeHandle (MethodDefinition ctor)

return clonedCtor;
}

void ImplementNSObjectRegisterToggleRefMethodStub ()
{
// 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)'
//
if (abr.TryGet_NSObject_RegisterToggleRef (out var registerToggleRef)) {
registerToggleRef!.IsPublic = false;
registerToggleRef!.IsInternalCall = false;

registerToggleRef!.CreateBody (out var il);
il.Emit (OpCodes.Ret);
}
}
}
}