Skip to content

Pin blittable layout classes in NativeAOT marshalling#130279

Merged
MichalStrehovsky merged 1 commit into
dotnet:mainfrom
MichalStrehovsky:nativeaot-blittable-layoutclass-pinning
Jul 9, 2026
Merged

Pin blittable layout classes in NativeAOT marshalling#130279
MichalStrehovsky merged 1 commit into
dotnet:mainfrom
MichalStrehovsky:nativeaot-blittable-layoutclass-pinning

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Fixes #81673.

NativeAOT's ILC always copied blittable layout classes passed by value into a separate native struct, honoring In/Out flags that default to In-only. This broke pointer identity, implicit [In,Out] write-back, and [Out] semantics for blittable layout classes.

Fold pinning support into LayoutClassPtrMarshaller: when marshalling a non-byref, non-return blittable layout class argument CLR->native, pin the managed object and pass an interior pointer directly to native code, mirroring CoreCLR's ILBlittablePtrMarshaller. Byref, field, reverse, return, and non-blittable scenarios continue to copy through the struct marshalling thunks.

Re-enables the previously-disabled LayoutClass interop tests (#81673).

NativeAOT's ILC always copied blittable layout classes passed by value
into a separate native struct, honoring In/Out flags that default to
In-only. This broke pointer identity, implicit [In,Out] write-back, and
[Out] semantics for blittable layout classes.

Fold pinning support into LayoutClassPtrMarshaller: when marshalling a
non-byref, non-return blittable layout class argument CLR->native, pin
the managed object and pass an interior pointer directly to native code,
mirroring CoreCLR's ILBlittablePtrMarshaller. Byref, field, reverse,
return, and non-blittable scenarios continue to copy through the struct
marshalling thunks.

Re-enables the previously-disabled LayoutClass interop tests (dotnet#81673).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates NativeAOT’s marshalling for blittable layout classes passed by value so they can be pinned and passed to native code via an interior pointer (instead of always copying into a temporary native struct), and re-enables the corresponding interop tests that were previously disabled for NativeAOT.

Changes:

  • Re-enable Interop/LayoutClass tests on NativeAOT by removing the ActiveIssue(#81673) skips.
  • Add a pinning-based forward-argument path to LayoutClassPtrMarshaller for blittable layout classes, and skip copy-back/cleanup in that path.

Reviewed changes

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

File Description
src/tests/Interop/LayoutClass/LayoutClassTest.cs Removes the NativeAOT-specific ActiveIssue(#81673) skips to re-enable LayoutClass interop coverage.
src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.Aot.cs Implements pinning-based CLR→native marshalling for blittable layout class arguments (forward, non-byref, non-return) and bypasses copy-back/cleanup in that mode.

Comment thread src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.Aot.cs
Comment thread src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.Aot.cs
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/azp run runtime-nativeaot-outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment thread src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.Aot.cs
Comment thread src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.Aot.cs
Comment thread src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.Aot.cs
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

(I'm looking at these long-known interop bugs because libgit2/libgit2sharp#2082 (comment) came to my attention. If we do generate marshalling for something and the marshalling doesn't work like CoreCLR, it's an annoying bug. They're trying to switch to LibraryImport but they have trouble doing so and they may not be the only ones - they didn't file a bug, just assumed this is the "normal native AOT experience"; it isn't).

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

/ba-g timeout in a networking test that looks like another manifestation of #126867

@MichalStrehovsky MichalStrehovsky enabled auto-merge (squash) July 9, 2026 06:50
@MichalStrehovsky MichalStrehovsky merged commit 57265b0 into dotnet:main Jul 9, 2026
137 of 143 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

interop\LayoutClass\LayoutClassTest failing

4 participants