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

Annotating Native CoreLib library to be AOT friendly #66112

Merged
merged 4 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)!;
}

[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The native code for the array might not be available at runtime.")]
public override Type MakeArrayType()
{
return SymbolType.FormCompoundType("[]", this, 0)!;
}

[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The native code for the array might not be available at runtime.")]
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 native code for the array might not be available at runtime.")]
public override Type MakeArrayType()
{
return SymbolType.FormCompoundType("[]", this, 0)!;
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
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 native code for the array might not be available at runtime.")]
public override Type MakeArrayType()
{
return SymbolType.FormCompoundType("[]", this, 0)!;
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
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,7 @@ public static object ConvertNativeComInterfaceToManaged(IntPtr pUnk)
#endif
}

[RequiresDynamicCode("Marshalling code for the object might not be available. Use the SizeOf<T> overload instead.")]
internal static int AsAnyGetNativeSize(object o)
{
// Array, string and StringBuilder are not implemented.
Expand All @@ -516,6 +518,7 @@ o is string ||
return Marshal.SizeOf(o.GetType());
}

[RequiresDynamicCode("Marshalling code for the object might not be available. Use the StructureToPtr<T> overload instead.")]
internal static void AsAnyMarshalManagedToNative(object o, IntPtr address)
{
// Array, string and StringBuilder are not implemented.
Expand All @@ -542,6 +545,7 @@ o is string ||
Marshal.PtrToStructureImpl(address, o);
}

[RequiresDynamicCode("Marshalling code for the object might not be available. Use the DestroyStructure<T> overload instead.")]
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 @@ -357,11 +357,13 @@ protected override bool IsValueTypeImpl()
return default;
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Type MakeArrayType()
{
return default;
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
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 native code for the array might not be available at runtime.")]
public override Type MakeArrayType()
{
return default;
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
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 native code for the array might not be available at runtime.")]
public override Type MakeArrayType()
{
return default;
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
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 @@ -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 @@ -164,6 +167,7 @@ private static IsolatedComponentLoadContext GetIsolatedComponentLoadContext(stri
return alc;
}

[RequiresDynamicCode("The native code for the method requested might not be available at runtime.")]
[RequiresUnreferencedCode(TrimIncompatibleWarningMessage, Url = "https://aka.ms/dotnet-illink/nativehost")]
private static IntPtr InternalGetFunctionPointer(AssemblyLoadContext alc,
string typeName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ internal ResourceReader(Stream stream, Dictionary<string, ResourceLocator> resCa
ReadResources();
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL3050:RequiresDynamicCode",
Justification = "InitializeBinaryFormatter will get trimmed out when AllowCustomResourceTypes is set to false. " +
"When set to true, we will already throw a warning for this feature switch, so we suppress this one in order for" +
"the user to only get one error.")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "InitializeBinaryFormatter will get trimmed out when AllowCustomResourceTypes is set to false. " +
"When set to true, we will already throw a warning for this feature switch, so we suppress this one in order for" +
Expand Down Expand Up @@ -85,6 +89,7 @@ private object DeserializeObject(int typeIndex)
}

// Issue https://github.com/dotnet/runtime/issues/39290 tracks finding an alternative to BinaryFormatter
[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("The CustomResourceTypesSupport feature switch has been enabled for this app which is being trimmed. " +
"Custom readers as well as custom objects on the resources file are not observable by the trimmer and so required assemblies, types and members may be removed.")]
private bool InitializeBinaryFormatter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ internal EnumBuilder() { }
protected override bool IsPointerImpl() { throw null; }
protected override bool IsPrimitiveImpl() { throw null; }
protected override bool IsValueTypeImpl() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The native code for the array might not be available at runtime.")]
public override System.Type MakeArrayType() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The native code for the array might not be available at runtime.")]
public override System.Type MakeArrayType(int rank) { throw null; }
public override System.Type MakeByRefType() { throw null; }
public override System.Type MakePointerType() { throw null; }
Expand Down Expand Up @@ -286,9 +288,12 @@ internal GenericTypeParameterBuilder() { }
protected override bool IsPrimitiveImpl() { throw null; }
public override bool IsSubclassOf(System.Type c) { throw null; }
protected override bool IsValueTypeImpl() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The native code for the array might not be available at runtime.")]
public override System.Type MakeArrayType() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The native code for the array might not be available at runtime.")]
public override System.Type MakeArrayType(int rank) { throw null; }
public override System.Type MakeByRefType() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The native code for this instantiation might not be available at runtime.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("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 System.Type MakeGenericType(params System.Type[] typeArguments) { throw null; }
public override System.Type MakePointerType() { throw null; }
Expand Down Expand Up @@ -571,9 +576,12 @@ public void DefineMethodOverride(System.Reflection.MethodInfo methodInfoBody, Sy
protected override bool IsPointerImpl() { throw null; }
protected override bool IsPrimitiveImpl() { throw null; }
public override bool IsSubclassOf(System.Type c) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The native code for the array might not be available at runtime.")]
public override System.Type MakeArrayType() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The native code for the array might not be available at runtime.")]
public override System.Type MakeArrayType(int rank) { throw null; }
public override System.Type MakeByRefType() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("The native code for this instantiation might not be available at runtime.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("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 System.Type MakeGenericType(params System.Type[] typeArguments) { throw null; }
public override System.Type MakePointerType() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,13 @@ public override bool IsDefined(Type attributeType, bool inherit)
return _tb.IsDefined(attributeType, inherit);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Type MakeArrayType()
{
return new ArrayType(this, 0);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Type MakeArrayType(int rank)
{
if (rank < 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,13 @@ public override int GetHashCode()
return base.GetHashCode();
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Type MakeArrayType()
{
return new ArrayType(this, 0);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Type MakeArrayType(int rank)
{
if (rank < 1)
Expand All @@ -494,6 +496,7 @@ public override Type MakeByRefType()
return new ByRefType(this);
}

[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 @@ -1372,11 +1372,13 @@ public override bool IsSZArray
}
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Type MakeArrayType()
{
return new ArrayType(this, 0);
}

[RequiresDynamicCode("The native code for the array might not be available at runtime.")]
public override Type MakeArrayType(int rank)
{
if (rank < 1)
Expand All @@ -1389,6 +1391,7 @@ public override Type MakeByRefType()
return new ByRefType(this);
}

[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