runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[hot reload] Validate / Implement support for adding `static extern` methods

Open lambdageek opened this issue 1 year ago • 2 comments

Related Roslyn issue: https://github.com/dotnet/roslyn/issues/73421

The [UnsafeAccessorAttribute] (API Proposal) is used on static extern functions to instruct the .NET runtime to generate an accessor method to a type member that bypass the normal .NET visibility checks.

   public class MyClass
   {
      private int _hidden;
      ...
    }

    public static partial class AccessHelpers
    {
        [UnsafeAccessor(UnsafeAccessorKind.Field, Name="_hidden")]
        public static extern ref int GetHiddenField(MyClass c);    // runtime generated implementation
    }

    ...

    public static MyClass DeserializeMyClass(string s)
    {
        MyClass x = new MyClass();
        ref int hiddenFieldRef = ref AccessHelpers.GetHiddenField(x);
        hiddenFieldRef = int.Parse(s);
        return x;
     }    
}

With [UnsafeAccessor] it becomes possible to use source-generated reflection-free serializers that can serialize and deserialize fields of user-defined classes. For example, a user may add a new field _hidden to MyClass and the source generator will add a new accessor method to AccessHelpers and generate a method DeserializeMyClass that uses the access method to populate the hidden field.

Currently if the user adds a second private field _hidden2 and the generator adds a new static extern method, EnC will report a rude edit:

error ENC0025: Adding an extern method requires restarting the application.

Note that it should work with generics, too

After Roslyn adds support for EnC deltas to include static extern methods, we need to validate that such deltas are processed appropriately by the .NET runtimes:

  • [ ] Validate/implement CoreCLR hot reload support for static extern methods
  • [ ] Validate/implement Mono hot reload support for static extern methods

lambdageek avatar May 10 '24 14:05 lambdageek

Tagging subscribers to this area: @tommcdon See info in area-owners.md if you want to be subscribed.

Is this a .NET 9 ask?

AaronRobinsonMSFT avatar May 10 '24 15:05 AaronRobinsonMSFT