Uh oh!
There was an error while loading. Please reload this page.
Extend type preinitialization support - #114374
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.
Uh oh!
There was an error while loading. Please reload this page.
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas |
MichalStrehovsky
commented
Apr 8, 2025
/azp run runtime-nativeaot-outerloop |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Sergio0694
left a comment
There was a problem hiding this comment.
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:
internalunsafestructID2D1EffectImplVtbl{publicdelegate* unmanaged[MemberFunction]<void*,Guid*,void**,int>QueryInterface;publicdelegate* unmanaged[MemberFunction]<void*,uint>AddRef;publicdelegate* unmanaged[MemberFunction]<void*,uint>Release;publicdelegate* unmanaged[MemberFunction]<void*,void*,void*,int>Initialize;publicdelegate* unmanaged[MemberFunction]<void*,int,int>PrepareForRender;publicdelegate* unmanaged[MemberFunction]<void*,void*,int>SetGraph;}internalunsafestructID2D1DrawTransformVtbl{publicdelegate* unmanaged[MemberFunction]<void*,Guid*,void**,int>QueryInterface;publicdelegate* unmanaged[MemberFunction]<void*,uint>AddRef;publicdelegate* unmanaged[MemberFunction]<void*,uint>Release;publicdelegate* unmanaged[MemberFunction]<void*,uint>GetInputCount;publicdelegate* unmanaged[MemberFunction]<void*,Rectangle*,Rectangle*,uint,int>MapOutputRectToInputRects;publicdelegate* unmanaged[MemberFunction]<void*,Rectangle*,Rectangle*,uint,Rectangle*,Rectangle*,int>MapInputRectsToOutputRect;publicdelegate* unmanaged[MemberFunction]<void*,uint,Rectangle,Rectangle*,int>MapInvalidRect;publicdelegate* unmanaged[MemberFunction]<void*,void*,int>SetDrawInfo;}internalunsafestructCustomD2D1EffectImpl{privatestaticreadonlyID2D1EffectImplVtblID2D1EffectImplVtbl;privatestaticreadonlyID2D1DrawTransformVtblID2D1DrawTransformVtbl;staticCustomD2D1EffectImpl(){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;}privatestaticclassID2D1EffectImplMethods{[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticintQueryInterface(void*@this,Guid*riid,void**ppvObject)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticuintAddRef(void*@this)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticuintRelease(void*@this)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticintInitialize(void*@this,void*effectContext,void*transformGraph)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticintPrepareForRender(void*@this,intchangeType)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticintSetGraph(void*@this,void*transformGraph)=>0;}privatestaticunsafeclassID2D1DrawTransformMethods{[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticintQueryInterface(void*@this,Guid*riid,void**ppvObject)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticuintAddRef(void*@this)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticuintRelease(void*@this)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticuintGetInputCount(void*@this)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticintMapOutputRectToInputRects(void*@this,Rectangle*outputRect,Rectangle*inputRects,uintinputRectsCount)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticintMapInputRectsToOutputRect(void*@this,Rectangle*inputRects,Rectangle*inputOpaqueSubRects,uintinputRectCount,Rectangle*outputRect,Rectangle*outputOpaqueSubRect)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticintMapInvalidRect(void*@this,uintinputIndex,RectangleinvalidInputRect,Rectangle*invalidOutputRect)=>0;[UnmanagedCallersOnly(CallConvs=[typeof(CallConvMemberFunction)])]publicstaticintSetDrawInfo(void*@this,void*drawInfo)=>0;}}Sergio0694
commented
Apr 9, 2025
@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 🙂 |
Uh oh!
There was an error while loading. Please reload this page.
MichalStrehovsky
commented
Apr 9, 2025
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
commented
Apr 9, 2025
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 <175728472+Copilot@users.noreply.github.com>
jkotas
left a comment
There was a problem hiding this comment.
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
commented
Apr 9, 2025
Wouldn't all these already be in a readonly section? I assumed when they get folded, they go into RVA fields? 🤔 |
MichalStrehovsky
commented
Apr 9, 2025
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
commented
Apr 9, 2025
/ba-g the Tools_Test failure is known and was fixed in #114424 |
jkotas
commented
Apr 9, 2025
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