-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Extend type preinitialization support #114374
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
Conversation
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.
There was a problem hiding this 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.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/TypePreinit.cs
Outdated
Show resolved
Hide resolved
|
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas |
|
/azp run runtime-nativeaot-outerloop |
|
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this 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;
}
}|
@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 🙂 |
|
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). |
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. |
Co-authored-by: Copilot <[email protected]>
There was a problem hiding this 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?
|
Wouldn't all these already be in a readonly section? I assumed when they get folded, they go into RVA fields? 🤔 |
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. |
|
/ba-g the Tools_Test failure is known and was fixed in #114424 |
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). |
Extends preinitialization support to cover scenario in #114354 (comment).
ldsfldainstruction 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 addedTestByRefFieldAddressEqualitytest tests things that go wrong if we don't do interning.ldobjthat is used when dereferencing pointers. This lifts some of the restrictions and we can nowldobjmany valuetypes, including the special vtable ones.Cc @dotnet/ilc-contrib @Sergio0694