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

[CoreCLR and native AOT] UnsafeAccessorAttribute supports generic parameters #99468

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
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
137 changes: 137 additions & 0 deletions docs/design/features/unsafeaccessors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# `UnsafeAccessorAttribute`

## 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.

## Semantics

This attribute will be applied to an `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 static` method will throw `MissingFieldException` or `MissingMethodException`.

For `Method`, `StaticMethod`, `Field`, and `StaticField`, the type of the first argument of the annotated `extern static` method identifies the owning type. Only the specific type defined will be examined for inaccessible members. The type hierarchy is not walked looking for a match.

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 return value for an accessor to a field can be `ref` if setting of the field is desired.

Constructors can be accessed using Constructor or Method.

The return type is considered for the signature match. Modreqs and modopts are initially not considered for the signature match. However, if an ambiguity exists ignoring modreqs and modopts, a precise match is attempted. If an ambiguity still exists, `AmbiguousMatchException` is thrown.

By default, the attributed method's name dictates the name of the method/field. This can cause confusion in some cases since language abstractions, like C# local functions, generate mangled IL names. The solution to this is to use the `nameof` mechanism and define the `Name` property.

Scenarios involving Generics may require creating new Generic types to contain the `extern static` method definition. The decision was made to require all `ELEMENT_TYPE_VAR` and `ELEMENT_TYPE_MVAR` instances to match identically type and generic parameter index. This means if the target method for access uses an `ELEMENT_TYPE_VAR`, the `extern static` method must also use an `ELEMENT_TYPE_VAR`. For example:

```csharp
class C<T>
{
T M<U>(U u) => default;
}

class Accessor<V>
{
// Correct - V is an ELEMENT_TYPE_VAR and W is ELEMENT_TYPE_VAR,
// respectively the same as T and U in the definition of C<T>::M<U>().
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "M")]
extern static void CallM<W>(C<V> c, W w);

// Incorrect - Since Y must be an ELEMENT_TYPE_VAR, but is ELEMENT_TYPE_MVAR below.
// [UnsafeAccessor(UnsafeAccessorKind.Method, Name = "M")]
// extern static void CallM<Y, Z>(C<Y> c, Z z);
}
```

Methods with the `UnsafeAccessorAttribute` that access members with Generic parameters are expected to have the same declared constraints with the target member. Failure to do so results in unspecified behavior. For example:

```csharp
class C<T>
{
T M<U>(U u) where U: Base => default;
}

class Accessor<V>
{
// Correct - Constraints match the target member.
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "M")]
extern static void CallM<W>(C<V> c, W w) where W: Base;

// Incorrect - Constraints do not match target member.
// [UnsafeAccessor(UnsafeAccessorKind.Method, Name = "M")]
// extern static void CallM<W>(C<V> c, W w);
}
```

## API

```csharp
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)
};
```

## API Usage

```csharp
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 = "<Name>k__BackingField")]
extern static ref string GetName(UserData userData);

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

Using Generics

```csharp
class UserData<T>
{
private T _field;
private UserData(T t) { _field = t; }
private U ConvertFieldToT<U>() => (U)_field;
}

// The Accessors class provides the Generic Type parameter for the method definitions.
class Accessors<V>
{
[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
extern static UserData<V> CallPrivateConstructor(V v);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "ConvertFieldToT")]
extern static U CallConvertFieldToT<U>(UserData<V> userData);
}

UserData<string> ud = Accessors<string>.CallPrivateConstructor("Joe");
Accessors<string>.CallPrivateConstructor<object>(ud);
```
10 changes: 5 additions & 5 deletions src/coreclr/tools/Common/TypeSystem/IL/Stubs/ILEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -686,27 +686,27 @@ private ILToken NewToken(object value, int tokenType)

public ILToken NewToken(TypeDesc value)
{
return NewToken(value, 0x01000000);
return NewToken(value, 0x01000000); // mdtTypeRef
}

public ILToken NewToken(MethodDesc value)
{
return NewToken(value, 0x0a000000);
return NewToken(value, 0x0a000000); // mdtMemberRef
}

public ILToken NewToken(FieldDesc value)
{
return NewToken(value, 0x0a000000);
return NewToken(value, 0x0a000000); // mdtMemberRef
}

public ILToken NewToken(string value)
{
return NewToken(value, 0x70000000);
return NewToken(value, 0x70000000); // mdtString
}

public ILToken NewToken(MethodSignature value)
{
return NewToken(value, 0x11000000);
return NewToken(value, 0x11000000); // mdtSignature
}

public ILLocalVariable NewLocal(TypeDesc localType, bool isPinned = false)
Expand Down
38 changes: 27 additions & 11 deletions src/coreclr/tools/Common/TypeSystem/IL/UnsafeAccessors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ public static MethodIL TryGetIL(EcmaMethod method)
return GenerateAccessorBadImageFailure(method);
}

// Block generic support early
if (method.HasInstantiation || method.OwningType.HasInstantiation)
{
return GenerateAccessorBadImageFailure(method);
}

if (!TryParseUnsafeAccessorAttribute(method, decodedAttribute.Value, out UnsafeAccessorKind kind, out string name))
{
return GenerateAccessorBadImageFailure(method);
Expand Down Expand Up @@ -232,15 +226,22 @@ private static bool ValidateTargetType(TypeDesc targetTypeMaybe, out TypeDesc va
targetType = null;
}

// We do not support signature variables as a target (for example, VAR and MVAR).
if (targetType is SignatureVariable)
{
targetType = null;
}

validated = targetType;
return validated != null;
}

private static bool DoesMethodMatchUnsafeAccessorDeclaration(ref GenerationContext context, MethodDesc method, bool ignoreCustomModifiers)
private static bool DoesMethodMatchUnsafeAccessorDeclaration(
ref GenerationContext context,
MethodSignature declSig,
MethodSignature maybeSig,
bool ignoreCustomModifiers)
{
MethodSignature declSig = context.Declaration.Signature;
MethodSignature maybeSig = method.Signature;

// Check if we need to also validate custom modifiers.
// If we are, do it first.
if (!ignoreCustomModifiers)
Expand Down Expand Up @@ -386,7 +387,10 @@ private static bool TrySetTargetMethod(ref GenerationContext context, string nam
}

// Check signature
if (!DoesMethodMatchUnsafeAccessorDeclaration(ref context, md, ignoreCustomModifiers))
if (!DoesMethodMatchUnsafeAccessorDeclaration(ref context,
context.Declaration.Signature,
md.Signature,
ignoreCustomModifiers))
{
continue;
}
Expand All @@ -411,6 +415,18 @@ private static bool TrySetTargetMethod(ref GenerationContext context, string nam
}

isAmbiguous = false;

if (targetMaybe != null && targetMaybe.HasInstantiation)
{
TypeDesc[] methodInstantiation = new TypeDesc[targetMaybe.Instantiation.Length];
for (int i = 0; i < methodInstantiation.Length; ++i)
{
methodInstantiation[i] = targetMaybe.Context.GetSignatureVariable(i, true);
}

targetMaybe = targetMaybe.Context.GetInstantiatedMethod(targetMaybe, new Instantiation(methodInstantiation));
}

context.TargetMethod = targetMaybe;
return context.TargetMethod != null;
}
Expand Down
11 changes: 4 additions & 7 deletions src/coreclr/vm/callconvbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,12 @@ namespace
{
STANDARD_VM_CONTRACT;

TypeHandle type;
MethodDesc* pMD;
FieldDesc* pFD;
ResolvedToken resolved{};
pResolver->ResolveToken(token, &resolved);

pResolver->ResolveToken(token, &type, &pMD, &pFD);
_ASSERTE(!resolved.TypeHandle.IsNull());

_ASSERTE(!type.IsNull());

*nameOut = type.GetMethodTable()->GetFullyQualifiedNameInfo(namespaceOut);
*nameOut = resolved.TypeHandle.GetMethodTable()->GetFullyQualifiedNameInfo(namespaceOut);

return S_OK;
}
Expand Down
31 changes: 21 additions & 10 deletions src/coreclr/vm/dynamicmethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ void LCGMethodResolver::AddToUsedIndCellList(BYTE * indcell)

}

void LCGMethodResolver::ResolveToken(mdToken token, TypeHandle * pTH, MethodDesc ** ppMD, FieldDesc ** ppFD)
void LCGMethodResolver::ResolveToken(mdToken token, ResolvedToken* resolvedToken)
{
STANDARD_VM_CONTRACT;

Expand All @@ -1335,24 +1335,35 @@ void LCGMethodResolver::ResolveToken(mdToken token, TypeHandle * pTH, MethodDesc

DECLARE_ARGHOLDER_ARRAY(args, 5);

TypeHandle handle;
MethodDesc* pMD = NULL;
FieldDesc* pFD = NULL;
args[ARGNUM_0] = OBJECTREF_TO_ARGHOLDER(ObjectFromHandle(m_managedResolver));
args[ARGNUM_1] = DWORD_TO_ARGHOLDER(token);
args[ARGNUM_2] = pTH;
args[ARGNUM_3] = ppMD;
args[ARGNUM_4] = ppFD;
args[ARGNUM_2] = &handle;
args[ARGNUM_3] = &pMD;
args[ARGNUM_4] = &pFD;

CALL_MANAGED_METHOD_NORET(args);

_ASSERTE(*ppMD == NULL || *ppFD == NULL);
_ASSERTE(pMD == NULL || pFD == NULL);

if (pTH->IsNull())
if (handle.IsNull())
{
if (*ppMD != NULL) *pTH = (*ppMD)->GetMethodTable();
else
if (*ppFD != NULL) *pTH = (*ppFD)->GetEnclosingMethodTable();
if (pMD != NULL)
{
handle = pMD->GetMethodTable();
}
else if (pFD != NULL)
{
handle = pFD->GetEnclosingMethodTable();
}
}

_ASSERTE(!pTH->IsNull());
_ASSERTE(!handle.IsNull());
resolvedToken->TypeHandle = handle;
resolvedToken->Method = pMD;
resolvedToken->Field = pFD;
}

//---------------------------------------------------------------------------------------
Expand Down
13 changes: 11 additions & 2 deletions src/coreclr/vm/dynamicmethod.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ class ChunkAllocator
void Delete();
};

struct ResolvedToken final
{
TypeHandle TypeHandle;
SigPointer TypeSignature;
SigPointer MethodSignature;
MethodDesc* Method;
FieldDesc* Field;
};

//---------------------------------------------------------------------------------------
//
class DynamicResolver
Expand Down Expand Up @@ -90,7 +99,7 @@ class DynamicResolver
virtual OBJECTHANDLE ConstructStringLiteral(mdToken metaTok) = 0;
virtual BOOL IsValidStringRef(mdToken metaTok) = 0;
virtual STRINGREF GetStringLiteral(mdToken metaTok) = 0;
virtual void ResolveToken(mdToken token, TypeHandle * pTH, MethodDesc ** ppMD, FieldDesc ** ppFD) = 0;
virtual void ResolveToken(mdToken token, ResolvedToken* resolvedToken) = 0;
virtual SigPointer ResolveSignature(mdToken token) = 0;
virtual SigPointer ResolveSignatureForVarArg(mdToken token) = 0;
virtual void GetEHInfo(unsigned EHnumber, CORINFO_EH_CLAUSE* clause) = 0;
Expand Down Expand Up @@ -141,7 +150,7 @@ class LCGMethodResolver : public DynamicResolver

OBJECTHANDLE ConstructStringLiteral(mdToken metaTok);
BOOL IsValidStringRef(mdToken metaTok);
void ResolveToken(mdToken token, TypeHandle * pTH, MethodDesc ** ppMD, FieldDesc ** ppFD);
void ResolveToken(mdToken token, ResolvedToken* resolvedToken);
SigPointer ResolveSignature(mdToken token);
SigPointer ResolveSignatureForVarArg(mdToken token);
void GetEHInfo(unsigned EHnumber, CORINFO_EH_CLAUSE* clause);
Expand Down
Loading
Loading