Skip to content

Commit

Permalink
Native aot trim annotations experiment (dotnet#65816)
Browse files Browse the repository at this point in the history
* Add nativeaot trim annotations experiment

* Fix mono libraries

* Update src/libraries/System.Reflection.Emit.Lightweight/ref/System.Reflection.Emit.Lightweight.cs

Co-authored-by: Eric Erhardt <[email protected]>

* FB on attribute message

* FB

* missed nativeaot message change

Co-authored-by: Eric Erhardt <[email protected]>
  • Loading branch information
LakshanF and eerhardt authored Mar 1, 2022
1 parent 1665aca commit dfe08b3
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 11 deletions.
1 change: 0 additions & 1 deletion eng/ApiCompatExcludeAttributes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
T:System.CLSCompliantAttribute
T:System.Diagnostics.CodeAnalysis.MemberNotNullAttribute
T:System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute
T:System.Diagnostics.DebuggerGuidedStepThroughAttribute
T:System.Runtime.CompilerServices.EagerStaticClassConstructionAttribute
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand All @@ -15,6 +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.")]
public static unsafe Array CreateInstance(Type elementType, int length)
{
if (elementType is null)
Expand Down Expand Up @@ -67,6 +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.")]
public static unsafe Array CreateInstance(Type elementType, params int[] lengths)
{
if (elementType is null)
Expand All @@ -92,6 +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.")]
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 @@ -139,6 +139,7 @@ private void InitManifestModule()

#region DefineDynamicAssembly

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[DynamicSecurityMethod] // Required to make Assembly.GetCallingAssembly reliable.
public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access)
{
Expand All @@ -149,6 +150,7 @@ public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyB
null);
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[DynamicSecurityMethod] // Required to make Assembly.GetCallingAssembly reliable.
public static AssemblyBuilder DefineDynamicAssembly(
AssemblyName name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public sealed class DynamicMethod : MethodInfo
// class initialization (ctor and init)
//

[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
Type[]? parameterTypes)
Expand All @@ -64,6 +65,7 @@ public DynamicMethod(string name,
true);
}

[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
Type[]? parameterTypes,
Expand All @@ -80,6 +82,7 @@ public DynamicMethod(string name,
true);
}

[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
Type[]? parameterTypes,
Expand All @@ -96,6 +99,7 @@ public DynamicMethod(string name,
false);
}

[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
Type[]? parameterTypes,
Expand All @@ -113,6 +117,7 @@ public DynamicMethod(string name,
false);
}

[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
MethodAttributes attributes,
CallingConventions callingConvention,
Expand All @@ -132,6 +137,7 @@ public DynamicMethod(string name,
false);
}

[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
Type[]? parameterTypes,
Expand All @@ -148,6 +154,7 @@ public DynamicMethod(string name,
false);
}

[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
Type? returnType,
Type[]? parameterTypes,
Expand All @@ -165,6 +172,7 @@ public DynamicMethod(string name,
false);
}

[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name,
MethodAttributes attributes,
CallingConventions callingConvention,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ public override bool IsDefined(Type attributeType, bool inherit)

public override Type[] GetGenericArguments() => m_inst ?? Type.EmptyTypes;

[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 MethodInfo MakeGenericMethod(params Type[] typeArguments)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,25 @@ public static IntPtr OffsetOf(Type t!!, string fieldName)
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern IntPtr OffsetOfHelper(IRuntimeFieldInfo f);

[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static byte ReadByte(object ptr, int ofs)
{
return ReadValueSlow(ptr, ofs, (IntPtr nativeHome, int offset) => ReadByte(nativeHome, offset));
}

[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static short ReadInt16(object ptr, int ofs)
{
return ReadValueSlow(ptr, ofs, (IntPtr nativeHome, int offset) => ReadInt16(nativeHome, offset));
}

[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static int ReadInt32(object ptr, int ofs)
{
return ReadValueSlow(ptr, ofs, (IntPtr nativeHome, int offset) => ReadInt32(nativeHome, offset));
}

[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static long ReadInt64([MarshalAs(UnmanagedType.AsAny), In] object ptr, int ofs)
{
return ReadValueSlow(ptr, ofs, (IntPtr nativeHome, int offset) => ReadInt64(nativeHome, offset));
Expand Down Expand Up @@ -102,21 +106,25 @@ private static unsafe T ReadValueSlow<T>(object ptr, int ofs, Func<IntPtr, int,
}
}

[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static void WriteByte(object ptr, int ofs, byte val)
{
WriteValueSlow(ptr, ofs, val, (IntPtr nativeHome, int offset, byte value) => WriteByte(nativeHome, offset, value));
}

[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static void WriteInt16(object ptr, int ofs, short val)
{
WriteValueSlow(ptr, ofs, val, (IntPtr nativeHome, int offset, short value) => Marshal.WriteInt16(nativeHome, offset, value));
}

[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static void WriteInt32(object ptr, int ofs, int val)
{
WriteValueSlow(ptr, ofs, val, (IntPtr nativeHome, int offset, int value) => Marshal.WriteInt32(nativeHome, offset, value));
}

[RequiresDynamicCode("Marshalling code for the object might not be available")]
public static void WriteInt64(object ptr, int ofs, long val)
{
WriteValueSlow(ptr, ofs, val, (IntPtr nativeHome, int offset, long value) => Marshal.WriteInt64(nativeHome, offset, value));
Expand Down Expand Up @@ -201,6 +209,7 @@ private static void PrelinkCore(MethodInfo m)
/// structure contains pointers to allocated blocks and "fDeleteOld" is
/// true, this routine will call DestroyStructure() first.
/// </summary>
[RequiresDynamicCode("Marshalling code for the object might not be available. Use the StructureToPtr<T> overload instead.")]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld);

Expand All @@ -214,6 +223,7 @@ private static void PrelinkCore(MethodInfo m)
/// Frees all substructures pointed to by the native memory block.
/// "structuretype" is used to provide layout information.
/// </summary>
[RequiresDynamicCode("Marshalling code for the object might not be available. Use the DestroyStructure<T> overload instead.")]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void DestroyStructure(IntPtr ptr, Type structuretype);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public override Module ManifestModule
}
}

[RequiresDynamicCode("Generating new code at runtime is not supported with native AOT.")]
[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access)
{
ReflectionEmitThrower.ThrowPlatformNotSupportedException();
return default;
}

[RequiresDynamicCode("Generating new code at runtime is not supported with native AOT.")]
[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, IEnumerable<CustomAttributeBuilder> assemblyAttributes)
{
ReflectionEmitThrower.ThrowPlatformNotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,49 @@ namespace System.Reflection.Emit
{
public sealed class DynamicMethod : MethodInfo
{
[RequiresDynamicCode("Generating new code at runtime is not supported with native AOT.")]
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility)
{
ReflectionEmitThrower.ThrowPlatformNotSupportedException();
}

[RequiresDynamicCode("Generating new code at runtime is not supported with native AOT.")]
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility)
{
ReflectionEmitThrower.ThrowPlatformNotSupportedException();
}

[RequiresDynamicCode("Generating new code at runtime is not supported with native AOT.")]
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, Type returnType, Type[] parameterTypes)
{
ReflectionEmitThrower.ThrowPlatformNotSupportedException();
}

[RequiresDynamicCode("Generating new code at runtime is not supported with native AOT.")]
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, Type returnType, Type[] parameterTypes, bool restrictedSkipVisibility)
{
ReflectionEmitThrower.ThrowPlatformNotSupportedException();
}

[RequiresDynamicCode("Generating new code at runtime is not supported with native AOT.")]
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, Type returnType, Type[] parameterTypes, Module m)
{
ReflectionEmitThrower.ThrowPlatformNotSupportedException();
}

[RequiresDynamicCode("Generating new code at runtime is not supported with native AOT.")]
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility)
{
ReflectionEmitThrower.ThrowPlatformNotSupportedException();
}

[RequiresDynamicCode("Generating new code at runtime is not supported with native AOT.")]
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner)
{
ReflectionEmitThrower.ThrowPlatformNotSupportedException();
}

[RequiresDynamicCode("Generating new code at runtime is not supported with native AOT.")]
[RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility)
{
ReflectionEmitThrower.ThrowPlatformNotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public override string[] GetEnumNames()
return new ReadOnlySpan<string>(ret).ToArray();
}

[RequiresDynamicCode("It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload instead.")]
public override Array GetEnumValues()
{
if (!IsEnum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ public void SetLocalSignature(byte[]? localSignature) { }
}
public sealed partial class DynamicMethod : System.Reflection.MethodInfo
{
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? parameterTypes, System.Reflection.Module m, bool skipVisibility) { }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type? returnType, System.Type[]? parameterTypes, System.Type owner, bool skipVisibility) { }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, System.Type? returnType, System.Type[]? parameterTypes) { }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, System.Type? returnType, System.Type[]? parameterTypes, bool restrictedSkipVisibility) { }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, System.Type? returnType, System.Type[]? parameterTypes, System.Reflection.Module m) { }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, System.Type? returnType, System.Type[]? parameterTypes, System.Reflection.Module m, bool skipVisibility) { }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, System.Type? returnType, System.Type[]? parameterTypes, System.Type owner) { }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating a DynamicMethod requires dynamic code.")]
public DynamicMethod(string name, System.Type? returnType, System.Type[]? parameterTypes, System.Type owner, bool skipVisibility) { }
public override System.Reflection.MethodAttributes Attributes { get { throw null; } }
public override System.Reflection.CallingConventions CallingConvention { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ internal AssemblyBuilder() { }
public override string Location { get { throw null; } }
public override System.Reflection.Module ManifestModule { get { throw null; } }
public override bool ReflectionOnly { get { throw null; } }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
public static System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
public static System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly(System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder>? assemblyAttributes) { throw null; }
public System.Reflection.Emit.ModuleBuilder DefineDynamicModule(string name) { throw null; }
public override bool Equals(object? obj) { throw null; }
Expand Down Expand Up @@ -333,6 +335,7 @@ internal MethodBuilder() { }
public override System.Reflection.ParameterInfo[] GetParameters() { throw null; }
public override object Invoke(object? obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? parameters, System.Globalization.CultureInfo? culture) { throw null; }
public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("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.Reflection.MethodInfo MakeGenericMethod(params System.Type[] typeArguments) { throw null; }
public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { }
Expand Down
Loading

0 comments on commit dfe08b3

Please sign in to comment.