[API Proposal]: Zero-overhead member access with suppressed visibility checks · Issue #81741 · dotnet/runtime (original) (raw)

Background and motivation

Number of existing .NET serializers depend on skipping member visibility checks for data serialization. Examples include System.Text.Json or EF Core. In order to skip the visibility checks, the serializers typically use dynamically emitted code (Reflection.Emit or Linq.Expressions) and classic reflection APIs as slow fallback. Neither of these two options are great for source generated serializers and native AOT compilation. This API proposal introduces a first class zero-overhead mechanism for skipping visibility checks.

API Proposal

namespace System.Runtime.CompilerServices;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class UnsafeAccessorAttribute : Attribute { public UnsafeAccessorAttribute(UnsafeAccessorKind kind);

public UnsafeAccessorKind Kind { get; }

// The name defaults to the annotated method name if not specified.
// The name must be null for constructors
public string? Name { get; set; }

}

public enum UnsafeAccessorKind { Constructor, // call instance constructor (newobj in IL) Method, // call instance method (callvirt in IL) StaticMethod, // call static method (call in IL) Field, // address of instance field (ldflda in IL) StaticField // address of static field (ldsflda in IL)

// Potential additions to handle niche cases
// FieldGet, // get value of instance field (`ldfld` in IL). Required for `ref` fields.  
// FieldSet, // get value of instance field (`stfld` in IL). Required for `ref` fields.  
// NonVirtualMethod, // call instance method non-virtually (`call` in IL).

};

This attribute will be applied on extern static method. The implementation of the extern static method annotated with this attribute will be provided by the runtime based on the information in the attribute and the signature of the method that the attribute is applied to. The runtime will try to find the matching method or field and forward the call to it. If the matching method or field is not found, the body of the extern method will throw MissingFieldException or MissingMethodException.

For UnsafeAccessorKind.{Static}Method and UnsafeAccessorKind.{Static}Field, the type of the first argument of the annotated extern method identifies the owning type. The value of the first argument is treated as @this pointer for instance fields and methods. The first argument must be passed as ref for instance fields and methods on structs. The value of the first argument is not used by the implementation for static fields and methods.

The generic parameters of the extern static method are concatenation of the type and method generic arguments of the target method. For example, extern static void Method1<T1, T2>(Class1<T1> @this) can be used to call Class1<T1>.Method1<T2>(). The generic constraints of the extern static method must match generic constraints of the target type, field or method.

Return type is considered for the signature match. modreqs and modopts are not considered for the signature match.

API Usage

class UserData { private UserData() { } public string Name { get; set; } }

[UnsafeAccessor(UnsafeAccessorKind.Constructor)] extern static UserData CallPrivateConstructor();

// This API allows accessing backing fields for auto-implemented properties with unspeakable names [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "k__BackingField")] extern static ref string GetName(UserData @this);

UserData ud = CallPrivateConstructor(); GetName(ud) = "Joe";

Alternative Designs

Risks