Skip to content

Commit

Permalink
Annotating Native CoreLib library to be AOT friendly (#66112)
Browse files Browse the repository at this point in the history
* Annotating Native CoreLib to be AOT friendly

* change array attribute msg

* FB

* Update src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs

Co-authored-by: Jan Kotas <[email protected]>

Co-authored-by: Jan Kotas <[email protected]>
  • Loading branch information
LakshanF and jkotas authored Mar 7, 2022
1 parent b0d1e89 commit 14c3a15
Show file tree
Hide file tree
Showing 24 changed files with 82 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace System
public abstract partial class Array : ICloneable, IList, IStructuralComparable, IStructuralEquatable
{
// Create instance will create an array
[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public static unsafe Array CreateInstance(Type elementType, int length)
{
if (elementType is null)
Expand Down Expand Up @@ -69,7 +69,7 @@ public static unsafe Array CreateInstance(Type elementType, int length1, int len
return InternalCreate((void*)t.TypeHandle.Value, 3, pLengths, null);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public static unsafe Array CreateInstance(Type elementType, params int[] lengths)
{
if (elementType is null)
Expand All @@ -95,7 +95,7 @@ public static unsafe Array CreateInstance(Type elementType, params int[] lengths
return InternalCreate((void*)t.TypeHandle.Value, lengths.Length, pLengths, null);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public static unsafe Array CreateInstance(Type elementType, int[] lengths, int[] lowerBounds)
{
if (elementType == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,13 @@ public override Type MakeByRefType()
return SymbolType.FormCompoundType("&", this, 0)!;
}

[RequiresDynamicCodeAttribute("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType()
{
return SymbolType.FormCompoundType("[]", this, 0)!;
}

[RequiresDynamicCodeAttribute("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType(int rank)
{
string s = GetRankString(rank);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ public override Type MakeByRefType()
return SymbolType.FormCompoundType("&", this, 0)!;
}

[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType()
{
return SymbolType.FormCompoundType("[]", this, 0)!;
}

[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType(int rank)
{
string s = GetRankString(rank);
Expand Down Expand Up @@ -195,6 +197,7 @@ public override Type MakeArrayType(int rank)

public override Type GetGenericTypeDefinition() { throw new InvalidOperationException(); }

[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override Type MakeGenericType(params Type[] typeArguments) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1081,11 +1081,13 @@ public override Type MakeByRefType()
return SymbolType.FormCompoundType("&", this, 0)!;
}

[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType()
{
return SymbolType.FormCompoundType("[]", this, 0)!;
}

[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType(int rank)
{
string s = GetRankString(rank);
Expand Down Expand Up @@ -1165,6 +1167,7 @@ public GenericTypeParameterBuilder[] DefineGenericParameters(params string[] nam
return m_inst;
}

[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override Type MakeGenericType(params Type[] typeArguments)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -502,6 +503,8 @@ public static object ConvertNativeComInterfaceToManaged(IntPtr pUnk)
#endif
}

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "This API will be called from compiler generated code only.")]
internal static int AsAnyGetNativeSize(object o)
{
// Array, string and StringBuilder are not implemented.
Expand All @@ -516,6 +519,8 @@ o is string ||
return Marshal.SizeOf(o.GetType());
}

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "This API will be called from compiler generated code only.")]
internal static void AsAnyMarshalManagedToNative(object o, IntPtr address)
{
// Array, string and StringBuilder are not implemented.
Expand All @@ -542,6 +547,8 @@ o is string ||
Marshal.PtrToStructureImpl(address, o);
}

[UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
Justification = "This API will be called from compiler generated code only.")]
internal static void AsAnyCleanupNative(IntPtr address, object o)
{
// Array, string and StringBuilder are not implemented.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal bool IsSzArray
return EETypePtr.EETypePtrOf<Array>().ToPointer();
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public static Array CreateInstance(Type elementType, int length)
{
if (elementType is null)
Expand Down Expand Up @@ -111,7 +111,7 @@ public static unsafe Array CreateInstance(Type elementType, int length1, int len
return NewMultiDimArray(arrayType.TypeHandle.ToEETypePtr(), pLengths, 3);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public static Array CreateInstance(Type elementType, params int[] lengths)
{
if (elementType is null)
Expand Down Expand Up @@ -140,7 +140,7 @@ public static Array CreateInstance(Type elementType, params int[] lengths)
}
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public static Array CreateInstance(Type elementType, int[] lengths, int[] lowerBounds)
{
if (elementType is null)
Expand All @@ -157,7 +157,7 @@ public static Array CreateInstance(Type elementType, int[] lengths, int[] lowerB
return CreateMultiDimArray(elementType, lengths, lowerBounds);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
private static Array CreateSzArray(Type elementType, int length)
{
// Though our callers already validated length once, this parameter is passed via arrays, so we must check it again
Expand All @@ -169,7 +169,7 @@ private static Array CreateSzArray(Type elementType, int length)
return RuntimeImports.RhNewArray(arrayType.TypeHandle.ToEETypePtr(), length);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
private static Array CreateMultiDimArray(Type elementType, int[] lengths, int[] lowerBounds)
{
Debug.Assert(lengths != null);
Expand All @@ -186,7 +186,7 @@ private static Array CreateMultiDimArray(Type elementType, int[] lengths, int[]
return RuntimeAugments.NewMultiDimArray(arrayType.TypeHandle, lengths, lowerBounds);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
private static Type GetArrayTypeFromElementType(Type elementType, bool multiDim, int rank)
{
elementType = elementType.UnderlyingSystemType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,13 @@ protected override bool IsValueTypeImpl()
return default;
}

[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType()
{
return default;
}

[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType(int rank)
{
return default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,13 @@ protected override bool IsValueTypeImpl()
return default;
}

[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType()
{
return default;
}

[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType(int rank)
{
return default;
Expand All @@ -428,6 +430,7 @@ public override Type MakeByRefType()
return default;
}

[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override Type MakeGenericType(params Type[] typeArguments)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,13 @@ public override bool IsVariableBoundArray
}
}

[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType()
{
return default;
}

[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType(int rank)
{
return default;
Expand All @@ -637,6 +639,7 @@ public override Type MakeByRefType()
return default;
}

[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override Type MakeGenericType(params Type[] typeArguments)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ internal static RuntimeTypeInfo GetRuntimeTypeInfo(RuntimeTypeHandle typeHandle)
return RuntimeTypeTable.Table.GetOrAdd(new RuntimeTypeHandleKey(typeHandle));
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType()
{
// We support enough of MakeArrayType to make enum operations work
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public override Type GetGenericTypeDefinition()
throw new InvalidOperationException(SR.InvalidOperation_NotGenericType);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public sealed override Type MakeArrayType()
{
#if ENABLE_REFLECTION_TRACE
Expand All @@ -438,7 +438,7 @@ public sealed override Type MakeArrayType()
return this.GetArrayTypeWithTypeHandle();
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public sealed override Type MakeArrayType(int rank)
{
#if ENABLE_REFLECTION_TRACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static Variant()
{
// Variant size is the size of 4 pointers (16 bytes) on a 32-bit processor,
// and 3 pointers (24 bytes) on a 64-bit processor.
int variantSize = Marshal.SizeOf(typeof(Variant));
int variantSize = Marshal.SizeOf<Variant>();
if (IntPtr.Size == 4)
{
Debug.Assert(variantSize == (4 * IntPtr.Size));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Internal.Runtime.InteropServices
public static class ComponentActivator
{
private const string TrimIncompatibleWarningMessage = "Native hosting is not trim compatible and this warning will be seen if trimming is enabled.";
private const string NativeAOTIncompatibleWarningMessage = "The native code for the method requested might not be available at runtime.";

[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
Expand Down Expand Up @@ -49,6 +50,7 @@ private static string MarshalToString(IntPtr arg, string argName)
/// <param name="delegateTypeNative">Assembly qualified delegate type name</param>
/// <param name="reserved">Extensibility parameter (currently unused)</param>
/// <param name="functionHandle">Pointer where to store the function pointer result</param>
[RequiresDynamicCode(NativeAOTIncompatibleWarningMessage)]
[RequiresUnreferencedCode(TrimIncompatibleWarningMessage, Url = "https://aka.ms/dotnet-illink/nativehost")]
[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
Expand Down Expand Up @@ -103,6 +105,7 @@ public static unsafe int LoadAssemblyAndGetFunctionPointer(IntPtr assemblyPathNa
/// <param name="loadContext">Extensibility parameter (currently unused)</param>
/// <param name="reserved">Extensibility parameter (currently unused)</param>
/// <param name="functionHandle">Pointer where to store the function pointer result</param>
[RequiresDynamicCode(NativeAOTIncompatibleWarningMessage)]
[UnmanagedCallersOnly]
public static unsafe int GetFunctionPointer(IntPtr typeNameNative,
IntPtr methodNameNative,
Expand Down Expand Up @@ -166,6 +169,7 @@ private static IsolatedComponentLoadContext GetIsolatedComponentLoadContext(stri
return alc;
}

[RequiresDynamicCode(NativeAOTIncompatibleWarningMessage)]
[RequiresUnreferencedCode(TrimIncompatibleWarningMessage, Url = "https://aka.ms/dotnet-illink/nativehost")]
private static IntPtr InternalGetFunctionPointer(AssemblyLoadContext alc,
string typeName,
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ref MemoryMarshal.GetArrayDataReference(larray),
Debug.Assert(array != null);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public static Array CreateInstance(Type elementType, params long[] lengths)
{
if (lengths == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ public static ArrayList Synchronized(ArrayList list!!)
// downcasting all elements. This copy may fail and is an O(n) operation.
// Internally, this implementation calls Array.Copy.
//
[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public virtual Array ToArray(Type type!!)
{
Array array = Array.CreateInstance(type, _size);
Expand Down Expand Up @@ -1068,7 +1068,7 @@ public override void Sort(int index, int count, IComparer? comparer)
return array;
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Array ToArray(Type type!!)
{
Array array = Array.CreateInstance(type, _list.Count);
Expand Down Expand Up @@ -1459,7 +1459,7 @@ public override void Sort(int index, int count, IComparer? comparer)
}
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Array ToArray(Type type)
{
lock (_root)
Expand Down Expand Up @@ -1842,7 +1842,7 @@ public override void Sort(int index, int count, IComparer? comparer)
return _list.ToArray();
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Array ToArray(Type type)
{
return _list.ToArray(type);
Expand Down Expand Up @@ -2094,7 +2094,7 @@ public override void Sort(int index, int count, IComparer? comparer)
return _list.ToArray();
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Array ToArray(Type type)
{
return _list.ToArray(type);
Expand Down Expand Up @@ -2536,7 +2536,7 @@ public override object? this[int index]
return array;
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Array ToArray(Type type!!)
{
InternalUpdateRange();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ internal abstract class SignatureType : Type
public sealed override MemberTypes MemberType => MemberTypes.TypeInfo;

// Compositors
[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public sealed override Type MakeArrayType() => new SignatureArrayType(this, rank: 1, isMultiDim: false);
[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public sealed override Type MakeArrayType(int rank)
{
if (rank <= 0)
Expand Down
Loading

0 comments on commit 14c3a15

Please sign in to comment.