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 warnings in our code #18629

Closed
31 tasks
Tracked by #80905
rolfbjarne opened this issue Aug 3, 2023 · 1 comment · Fixed by #18909
Closed
31 tasks
Tracked by #80905

NativeAOT: fix warnings in our code #18629

rolfbjarne opened this issue Aug 3, 2023 · 1 comment · Fixed by #18909
Assignees
Labels
dotnet An issue or pull request related to .NET (6)
Milestone

Comments

@rolfbjarne
Copy link
Member

rolfbjarne commented Aug 3, 2023

Enabling warnings for NativeAOT reports some warnings. We should fix these.

Keeping a list of each of them individually, so they can also be fixed individually.

Ref: #18571.

  • ObjCRuntime/Blocks.cs(502,4): Trim analysis warning IL2075: ObjCRuntime.BlockLiteral.GetBlockForDelegate(MethodInfo, Object, UInt32, String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethod(String, BindingFlags)'. The return value of method 'ObjCRuntime.BlockLiteral.GetDelegateProxyType(MethodInfo, UInt32, MethodInfo&)' 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.
  • ObjCRuntime/Blocks.cs(507,4): Trim analysis warning IL2075: ObjCRuntime.BlockLiteral.GetBlockForDelegate(MethodInfo, Object, UInt32, String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetField(String, BindingFlags)'. The return value of method 'ObjCRuntime.BlockLiteral.GetDelegateProxyType(MethodInfo, UInt32, MethodInfo&)' 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.
  • ObjCRuntime/Blocks.cs(415,26): Trim analysis warning IL2075: ObjCRuntime.BlockLiteral.GetDelegateProxyType(MethodInfo, UInt32, MethodInfo&): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. 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.
  • ObjCRuntime/Blocks.cs(419,5): Trim analysis warning IL2062: 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.
  • Foundation/NSObject2.cs(236,4): Trim analysis warning IL2087: Foundation.NSObject.AllocateNSObject(IntPtr): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(Type)'. The generic parameter 'T' of 'Foundation.NSObject.AllocateNSObject(IntPtr)' 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.
  • Foundation/NSObject2.cs(247,5): Trim analysis warning IL2072: Foundation.NSObject.CreateNSObject(IntPtr, IntPtr, NSObject.Flags): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(Type)'. The return value of method 'ObjCRuntime.Runtime.GetGCHandleTarget(IntPtr)' 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.
  • ObjCRuntime/Runtime.CoreCLR.cs(267,4): Trim analysis warning IL2026: 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.
  • ObjCRuntime/Runtime.cs(2202,23): Trim analysis warning IL2070: 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.
  • ObjCRuntime/Runtime.cs(1219,23): Trim analysis warning IL2075: 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.
  • ObjCRuntime/Runtime.cs(1458,4): Trim analysis warning IL2070: 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.
  • ObjCRuntime/Runtime.cs(1413,4): Trim analysis warning IL2070: 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.
  • ObjCRuntime/Runtime.CoreCLR.cs(171,5): Trim analysis warning IL2026: 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.
  • Foundation/NSObject2.cs(236): Trim analysis warning IL2087: Foundation.NSObject.AllocateNSObject(IntPtr): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(Type)'. The generic parameter 'T' of 'Foundation.NSObject.AllocateNSObject(IntPtr)' 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.
  • Foundation/NSObject2.cs(247): Trim analysis warning IL2072: Foundation.NSObject.CreateNSObject(IntPtr,IntPtr,NSObject.Flags): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(Type)'. The return value of method 'ObjCRuntime.Runtime.GetGCHandleTarget(IntPtr)' 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.
  • ObjCRuntime/Runtime.CoreCLR.cs(171): Trim analysis warning IL2026: 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.
  • ObjCRuntime/Runtime.CoreCLR.cs(267): Trim analysis warning IL2026: 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.
  • ObjCRuntime/Runtime.CoreCLR.cs(471): AOT analysis warning IL3050: ObjCRuntime.Runtime.StructureToPtr(Object,IntPtr): Using member 'System.Runtime.InteropServices.Marshal.StructureToPtr(Object,IntPtr,Boolean)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Marshalling code for the object might not be available. Use the StructureToPtr overload instead.
  • ObjCRuntime/Runtime.CoreCLR.cs(640): AOT analysis warning IL3050: ObjCRuntime.Runtime.SizeOf(Type): Using member 'System.Runtime.InteropServices.Marshal.SizeOf(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Marshalling code for the object might not be available. Use the SizeOf overload instead.
  • ObjCRuntime/Runtime.CoreCLR.cs(827): AOT analysis warning IL3050: ObjCRuntime.Runtime.CreateArray(Runtime.MonoObject*,UInt64): Using member 'System.Array.CreateInstance(Type,Int32)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available.
  • ObjCRuntime/Runtime.CoreCLR.cs(952): AOT analysis warning IL3050: ObjCRuntime.Runtime.PtrToStructure(IntPtr,Type): Using member 'System.Runtime.InteropServices.Marshal.PtrToStructure(IntPtr,Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Marshalling code for the object might not be available.
  • ObjCRuntime/Runtime.cs(1106): AOT analysis warning IL3050: ObjCRuntime.Runtime.GetDelegateForBlock(IntPtr,Type): Using member 'System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(IntPtr,Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Marshalling code for the delegate might not be available. Use the GetDelegateForFunctionPointer overload instead.
  • ObjCRuntime/Runtime.cs(1219): Trim analysis warning IL2075: 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.
  • ObjCRuntime/Runtime.cs(1413): Trim analysis warning IL2070: ObjCRuntime.Runtime.GetIntPtrConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter '#0' 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.
  • ObjCRuntime/Runtime.cs(1458): Trim analysis warning IL2070: ObjCRuntime.Runtime.GetIntPtr_BoolConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructors(BindingFlags)'. The parameter '#0' 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.
  • ObjCRuntime/Runtime.cs(2202): Trim analysis warning IL2070: ObjCRuntime.Runtime.FindClosedMethod(Type,MethodBase): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethods(BindingFlags)'. The parameter '#0' 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.
  • ObjCRuntime/Blocks.cs(223): AOT analysis warning IL3050: ObjCRuntime.BlockLiteral.SetupBlockImpl(Delegate,Delegate,Boolean,Byte[]): Using member 'System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(Delegate)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Marshalling code for the delegate might not be available. Use the GetFunctionPointerForDelegate overload instead.
  • ObjCRuntime/Blocks.cs(415): Trim analysis warning IL2075: ObjCRuntime.BlockLiteral.GetDelegateProxyType(MethodInfo,UInt32,MethodInfo&): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. 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.
  • ObjCRuntime/Blocks.cs(419): Trim analysis warning IL2062: 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.
  • ObjCRuntime/Blocks.cs(502): Trim analysis warning IL2075: ObjCRuntime.BlockLiteral.GetBlockForDelegate(MethodInfo,Object,UInt32,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicMethods' in call to 'System.Type.GetMethod(String,BindingFlags)'. The return value of method 'ObjCRuntime.BlockLiteral.GetDelegateProxyType(MethodInfo,UInt32,MethodInfo&)' 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.
  • ObjCRuntime/Blocks.cs(507): Trim analysis warning IL2075: ObjCRuntime.BlockLiteral.GetBlockForDelegate(MethodInfo,Object,UInt32,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetField(String,BindingFlags)'. The return value of method 'ObjCRuntime.BlockLiteral.GetDelegateProxyType(MethodInfo,UInt32,MethodInfo&)' 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.
  • ObjCRuntime/Runtime.CoreCLR.cs(450): AOT analysis warning IL3050: ObjCRuntime.Runtime.MarshalStructure(!!0): Using member 'System.Runtime.InteropServices.Marshal.SizeOf(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Marshalling code for the object might not be available. Use the SizeOf overload instead.
@rolfbjarne rolfbjarne added the dotnet An issue or pull request related to .NET (6) label Aug 3, 2023
@rolfbjarne rolfbjarne added this to the .NET 8 milestone Aug 3, 2023
@rolfbjarne rolfbjarne self-assigned this Aug 17, 2023
rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Sep 1, 2023
…th NativeAOT.

We have numerous methods that we should never end up executing when using
NativeAOT, in particular methods called from native code.

Ideally we'd be able to link these away completely, but that's a much larger
refactoring, so in the meantime we can do something simpler: just throw an
exception in these methods if using NativeAOT.

There are two advantages:

* Smaller apps.
* No warnings from NativeAOT about these methods doing things that aren't
  trimmer-safe.

Contributes towards xamarin#18629.
rolfbjarne added a commit that referenced this issue Sep 5, 2023
…#18905)

Use unsafe code to copy a struct instead of reflection, this is faster,
and also fully AOT-compatible.

Contributes towards #18629.
rolfbjarne added a commit that referenced this issue Sep 5, 2023
…18904)

If anything we're supposed to link with *.o files, not publish them, but
since we're currently not handling any *.o files, just explicitly remove them
from the build. This avoids a warning where ComputeBundleLocation would issue
a warning about not knowing what to do with them.

Contributes towards #18629.

---------

Co-authored-by: Ivan Povazan <55002338+ivanpovazan@users.noreply.github.com>
rolfbjarne added a commit that referenced this issue Sep 5, 2023
…elegate type. (#18906)

This makes it possible to call the generic
Marshal.GetDelegateForFunctionPointer<T> method instead of the non-generic 
Marshal.GetDelegateForFunctionPointer method, which is easier for AOT
compilers to handle (no reflection for Marshal.GetDelegateForFunctionPointer<T>).

Contributes towards #18629.
rolfbjarne added a commit that referenced this issue Sep 7, 2023
…th NativeAOT. (#18907)

We have numerous methods that we should never end up executing when using
NativeAOT, in particular methods called from native code.

Ideally we'd be able to link these away completely, but that's a much larger
refactoring, so in the meantime we can do something simpler: just throw an
exception in these methods if using NativeAOT.

There are two advantages:

* Smaller apps.
* No warnings from NativeAOT about these methods doing things that aren't
  trimmer-safe.

Contributes towards #18629.
rolfbjarne added a commit that referenced this issue Sep 12, 2023
@rolfbjarne
Copy link
Member Author

Fixed in #18909.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet An issue or pull request related to .NET (6)
Projects
None yet
1 participant