Skip to content

Conversation

@MichalStrehovsky
Copy link
Member

Extends preinitialization support to cover scenario in #114354 (comment).

  • We only supported ldsflda instruction if the field was on the same type as we're preinitializing. This lifts the restriction to support loading addresses of readonly fields on other types, even if they have a static constructor, provided the cctor can be interpreted. One complication here is that this exposes us to correctness issues when address of the same field is loaded multiple times. We need to make sure nested preinit results are interned. The newly added TestByRefFieldAddressEquality test tests things that go wrong if we don't do interning.
  • We didn't support calling methods on types that have a static constructor. This lifts the restriction if the static constructor is side effect free (approximated by "can be interpreted").
  • We had limitations around ldobj that is used when dereferencing pointers. This lifts some of the restrictions and we can now ldobj many valuetypes, including the special vtable ones.

Cc @dotnet/ilc-contrib @Sergio0694

Extends preinitialization support to cover scenario in dotnet#114354 (comment).

* We only supported `ldsflda` instruction if the field was on the same type as we're preinitializing. This lifts the restriction to support loading addresses of readonly fields on other types, even if they have a static constructor, provided the cctor can be interpreted. One complication here is that this exposes us to correctness issues when address of the same field is loaded multiple times. We need to make sure nested preinit results are interned. The newly added `TestByRefFieldAddressEquality` test tests things that go wrong if we don't do interning.
* We didn't support calling methods on types that have a static constructor. This lifts the restriction if the static constructor is side effect free (approximated by "can be interpreted").
* We had limitations around `ldobj` that is used when dereferencing pointers. This lifts some of the restrictions and we can now `ldobj` many valuetypes, including the special vtable ones.
Copilot AI review requested due to automatic review settings April 8, 2025 13:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

@MichalStrehovsky
Copy link
Member Author

/azp run runtime-nativeaot-outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

@Sergio0694 Sergio0694 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're also adding a bunch more tests, here's another one we could add, just to cover more possible scenarios. This one covers COM composition scenarios with custom blittable COM object types (eg. like TerraFX and ComputeSharp work). This one I ported from ComputeSharp and has two vtables in the type, for composition:

internal unsafe struct ID2D1EffectImplVtbl
{
    public delegate* unmanaged[MemberFunction]<void*, Guid*, void**, int> QueryInterface;
    public delegate* unmanaged[MemberFunction]<void*, uint> AddRef;
    public delegate* unmanaged[MemberFunction]<void*, uint> Release;
    public delegate* unmanaged[MemberFunction]<void*, void*, void*, int> Initialize;
    public delegate* unmanaged[MemberFunction]<void*, int, int> PrepareForRender;
    public delegate* unmanaged[MemberFunction]<void*, void*, int> SetGraph;
}

internal unsafe struct ID2D1DrawTransformVtbl
{
    public delegate* unmanaged[MemberFunction]<void*, Guid*, void**, int> QueryInterface;
    public delegate* unmanaged[MemberFunction]<void*, uint> AddRef;
    public delegate* unmanaged[MemberFunction]<void*, uint> Release;
    public delegate* unmanaged[MemberFunction]<void*, uint> GetInputCount;
    public delegate* unmanaged[MemberFunction]<void*, Rectangle*, Rectangle*, uint, int> MapOutputRectToInputRects;
    public delegate* unmanaged[MemberFunction]<void*, Rectangle*, Rectangle*, uint, Rectangle*, Rectangle*, int> MapInputRectsToOutputRect;
    public delegate* unmanaged[MemberFunction]<void*, uint, Rectangle, Rectangle*, int> MapInvalidRect;
    public delegate* unmanaged[MemberFunction]<void*, void*, int> SetDrawInfo;
}

internal unsafe struct CustomD2D1EffectImpl
{
    private static readonly ID2D1EffectImplVtbl ID2D1EffectImplVtbl;
    private static readonly ID2D1DrawTransformVtbl ID2D1DrawTransformVtbl;

    static CustomD2D1EffectImpl()
    {
        ID2D1EffectImplVtbl.QueryInterface = &ID2D1EffectImplMethods.QueryInterface;
        ID2D1EffectImplVtbl.AddRef = &ID2D1EffectImplMethods.AddRef;
        ID2D1EffectImplVtbl.Release = &ID2D1EffectImplMethods.Release;
        ID2D1EffectImplVtbl.Initialize = &ID2D1EffectImplMethods.Initialize;
        ID2D1EffectImplVtbl.PrepareForRender = &ID2D1EffectImplMethods.PrepareForRender;
        ID2D1EffectImplVtbl.SetGraph = &ID2D1EffectImplMethods.SetGraph;

        ID2D1DrawTransformVtbl.QueryInterface = &ID2D1DrawTransformMethods.QueryInterface;
        ID2D1DrawTransformVtbl.AddRef = &ID2D1DrawTransformMethods.AddRef;
        ID2D1DrawTransformVtbl.Release = &ID2D1DrawTransformMethods.Release;
        ID2D1DrawTransformVtbl.GetInputCount = &ID2D1DrawTransformMethods.GetInputCount;
        ID2D1DrawTransformVtbl.MapOutputRectToInputRects = &ID2D1DrawTransformMethods.MapOutputRectToInputRects;
        ID2D1DrawTransformVtbl.MapInputRectsToOutputRect = &ID2D1DrawTransformMethods.MapInputRectsToOutputRect;
        ID2D1DrawTransformVtbl.MapInvalidRect = &ID2D1DrawTransformMethods.MapInvalidRect;
        ID2D1DrawTransformVtbl.SetDrawInfo = &ID2D1DrawTransformMethods.SetDrawInfo;
    }

    private static class ID2D1EffectImplMethods
    {
        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static int QueryInterface(void* @this, Guid* riid, void** ppvObject) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static uint AddRef(void* @this) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static uint Release(void* @this) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static int Initialize(void* @this, void* effectContext, void* transformGraph) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static int PrepareForRender(void* @this, int changeType) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static int SetGraph(void* @this, void* transformGraph) => 0;
    }

    private static unsafe class ID2D1DrawTransformMethods
    {
        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static int QueryInterface(void* @this, Guid* riid, void** ppvObject) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static uint AddRef(void* @this) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static uint Release(void* @this) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static uint GetInputCount(void* @this) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static int MapOutputRectToInputRects(void* @this, Rectangle* outputRect, Rectangle* inputRects, uint inputRectsCount) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static int MapInputRectsToOutputRect(void* @this, Rectangle* inputRects, Rectangle* inputOpaqueSubRects, uint inputRectCount, Rectangle* outputRect, Rectangle* outputOpaqueSubRect) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static int MapInvalidRect(void* @this, uint inputIndex, Rectangle invalidInputRect, Rectangle* invalidOutputRect) => 0;

        [UnmanagedCallersOnly(CallConvs = [typeof(CallConvMemberFunction)])]
        public static int SetDrawInfo(void* @this, void* drawInfo) => 0;
    }
}

@Sergio0694
Copy link
Contributor

@MichalStrehovsky not entirely sure if these changes are also meant to potentially support preinitializing more than a single field in a type. If so and this snippet should just work, feel free to include the test, if you want to get more coverage. If this would instead require more work, feel free to ignore it, not a priority (and in case the "one field per cctor" restriction is a thing, we can just adapt on our side and just split vtables out into separate types in that case). Up to you 🙂

@MichalStrehovsky
Copy link
Member Author

The outerloop run looks good. Linux-x64 runs into a common OOM in LibraryImportGenerator.Tests and both the Windows legs run into the UCRT bug that I have a workaround for in #109006 (we might want to approve & merge, this is the top reliability issue in nativeaot outerloops at the moment).

@MichalStrehovsky
Copy link
Member Author

@MichalStrehovsky not entirely sure if these changes are also meant to potentially support preinitializing more than a single field in a type. If so and this snippet should just work, feel free to include the test, if you want to get more coverage. If this would instead require more work, feel free to ignore it, not a priority (and in case the "one field per cctor" restriction is a thing, we can just adapt on our side and just split vtables out into separate types in that case). Up to you 🙂

Yeah, this just works. The interpreter is pretty composable. So if type X can be preinitialized and type Y can be preinitialized and you merged the two types into one (unioning the fields and concatenating the cctors), the merged type will be preinitialized too, barring hitting the 100000 instruction count limit. I'll not add the test, we don't directly test the composability of everything.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

I forgot to ask on the previous PR: What would it take to place the readonly static vtable fields (or readonly static fields in general) into a read only section in the binary?

@Sergio0694
Copy link
Contributor

Wouldn't all these already be in a readonly section? I assumed when they get folded, they go into RVA fields? 🤔

@MichalStrehovsky
Copy link
Member Author

I forgot to ask on the previous PR: What would it take to place the readonly static vtable fields (or readonly static fields in general) into a read only section in the binary?

It would only make a meaningful difference when data dehydration is turned off. Since we plan to turn it off by default on Windows only, it would be a Windows-only feature.

I think it should be easy enough if we say that all non-GC static fields on the type need to be read only for this to happen. If we wanted to do it field-by-field, it would be much harder.

@MichalStrehovsky
Copy link
Member Author

/ba-g the Tools_Test failure is known and was fixed in #114424

@MichalStrehovsky MichalStrehovsky merged commit a6553ac into dotnet:main Apr 9, 2025
99 of 102 checks passed
@MichalStrehovsky MichalStrehovsky deleted the morepreinit branch April 9, 2025 20:48
@jkotas
Copy link
Member

jkotas commented Apr 9, 2025

it would be a Windows-only feature.
all non-GC static fields on the type need to be read only for this to happen

These limitations sound reasonable to me. It would help with security (we have other Windows-only mitigations) and private working set (separating readonly and writeable data reduces dirty pages).

@github-actions github-actions bot locked and limited conversation to collaborators May 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants