Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
Prev Previous commit
Next Next commit
Add design document
Add strict check for precisely matched Generic constraints
  • Loading branch information
AaronRobinsonMSFT committed Mar 13, 2024
commit 9b26c9d3621fa3c50e5ad1dd2b08f7b97f2eba04
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);
```
11 changes: 11 additions & 0 deletions src/coreclr/vm/methodtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,17 @@ WORD MethodTable::GetNumMethods()
return GetClass()->GetNumMethods();
}

PTR_MethodTable MethodTable::GetTypicalMethodTable()
{
LIMITED_METHOD_DAC_CONTRACT;
if (IsArray())
return (PTR_MethodTable)this;

PTR_MethodTable methodTableMaybe = GetModule()->LookupTypeDef(GetCl()).AsMethodTable();
_ASSERTE(methodTableMaybe->IsTypicalTypeDefinition());
return methodTableMaybe;
}

//==========================================================================================
BOOL MethodTable::HasSameTypeDefAs(MethodTable *pMT)
{
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/vm/methodtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,8 @@ class MethodTable
return !HasInstantiation() || IsGenericTypeDefinition();
}

PTR_MethodTable GetTypicalMethodTable();

BOOL HasSameTypeDefAs(MethodTable *pMT);

//-------------------------------------------------------------------
Expand Down
78 changes: 77 additions & 1 deletion src/coreclr/vm/prestub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,64 @@ namespace
return true;
}

bool AreConstraintsEqual(const Instantiation& left, const Instantiation& right)
{
STANDARD_VM_CONTRACT;

DWORD argCount = left.GetNumArgs();
if (argCount != right.GetNumArgs())
return false;

for (DWORD i = 0; i < argCount; ++i)
{
TypeHandle tL = left[i];
TypeHandle tR = right[i];

// Check generic variable state are the same.
BOOL isGeneric = tL.IsGenericVariable();
if (isGeneric != tR.IsGenericVariable())
return false;

// Only generic variables have constraints.
if (!isGeneric)
continue;

TypeVarTypeDesc* tsL = tL.AsGenericVariable();
TypeVarTypeDesc* tsR = tR.AsGenericVariable();

//
// Verify general constraints
//
IMDInternalImport* importL = tsL->GetModule()->GetMDImport();
IMDInternalImport* importR = tsR->GetModule()->GetMDImport();

DWORD flagsL;
DWORD flagsR;
IfFailThrow(importL->GetGenericParamProps(tsL->GetToken(), NULL, &flagsL, NULL, NULL, NULL));
IfFailThrow(importR->GetGenericParamProps(tsR->GetToken(), NULL, &flagsR, NULL, NULL, NULL));
if ((flagsL & gpSpecialConstraintMask) != (flagsR & gpSpecialConstraintMask))
return false;

//
// Verify type constraints
//
DWORD constraintCountL;
DWORD constraintCountR;
TypeHandle* cL = tsL->GetConstraints(&constraintCountL);
TypeHandle* cR = tsR->GetConstraints(&constraintCountR);
if (constraintCountL != constraintCountR)
return false;

for (DWORD j = 0; j < constraintCountL; ++j)
{
if (cL[j] != cR[j])
return false;
}
}

return true;
}

bool TrySetTargetMethod(
GenerationContext& cxt,
LPCUTF8 methodName,
Expand All @@ -1285,11 +1343,13 @@ namespace
TypeHandle targetType = cxt.TargetType;
_ASSERTE(!targetType.IsTypeDesc());

MethodTable* pMT = targetType.AsMethodTable();

MethodDesc* targetMaybe = NULL;

// Following a similar iteration pattern found in MemberLoader::FindMethod().
// However, we are only operating on the current type not walking the type hierarchy.
MethodTable::IntroducedMethodIterator iter(targetType.AsMethodTable());
MethodTable::IntroducedMethodIterator iter(pMT);
for (; iter.IsValid(); iter.Next())
{
MethodDesc* curr = iter.GetMethodDesc();
Expand Down Expand Up @@ -1325,6 +1385,22 @@ namespace
targetMaybe = curr;
}

if (pMT->HasInstantiation())
{
Instantiation decl = cxt.Declaration->GetMethodTable()->GetTypicalMethodTable()->GetInstantiation();
Instantiation target = pMT->GetTypicalMethodTable()->GetInstantiation();
if (!AreConstraintsEqual(decl, target))
COMPlusThrow(kInvalidProgramException, W("Argument_GenTypeConstraintsNotEqual"));
}

if (targetMaybe->HasMethodInstantiation())
{
Instantiation decl = cxt.Declaration->LoadTypicalMethodDefinition()->GetMethodInstantiation();
Instantiation target = targetMaybe->LoadTypicalMethodDefinition()->GetMethodInstantiation();
if (!AreConstraintsEqual(decl, target))
COMPlusThrow(kInvalidProgramException, W("Argument_GenMethodConstraintsNotEqual"));
}

cxt.TargetMethod = targetMaybe;
return cxt.TargetMethod != NULL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,12 @@
<data name="Argument_GenConstraintViolation" xml:space="preserve">
<value>GenericArguments[{0}], '{1}', on '{2}' violates the constraint of type '{3}'.</value>
</data>
<data name="Argument_GenTypeConstraintsNotEqual" xml:space="preserve">
<value>Generic type constraints do not match.</value>
</data>
<data name="Argument_GenMethodConstraintsNotEqual" xml:space="preserve">
<value>Generic method constraints do not match.</value>
</data>
<data name="Argument_GenericArgsCount" xml:space="preserve">
<value>The number of generic arguments provided doesn't equal the arity of the generic type definition.</value>
</data>
Expand Down Expand Up @@ -3346,7 +3352,7 @@
</data>
<data name="RFLCT_Targ_ITargMismatch_WithType" xml:space="preserve">
<value>Object type {0} does not match target type {1}.</value>
</data>
</data>
<data name="RFLCT_Targ_StatFldReqTarg" xml:space="preserve">
<value>Non-static field requires a target.</value>
</data>
Expand Down