Skip to content

Fix MarshalArrayAsParam/AsLPArray/AsLPArrayTest test#126443

Merged
MichalStrehovsky merged 4 commits intodotnet:mainfrom
eduardo-vp:fix-81674
Apr 2, 2026
Merged

Fix MarshalArrayAsParam/AsLPArray/AsLPArrayTest test#126443
MichalStrehovsky merged 4 commits intodotnet:mainfrom
eduardo-vp:fix-81674

Conversation

@eduardo-vp
Copy link
Copy Markdown
Member

@eduardo-vp eduardo-vp commented Apr 2, 2026

When marshalling an [Out, MarshalAs(UnmanagedType.LPArray)] string[] parameter, the marshaller allocates a native buffer but skips initialization (TransformManagedToNative is skipped for out-only parameters). Native callees can attempt to free those uninitialized pointers and cause heap corruption.

Fixes #81674.

Copy link
Copy Markdown
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.

Pull request overview

This PR fixes a NativeAOT P/Invoke marshalling bug for [Out, MarshalAs(UnmanagedType.LPArray)] array parameters (notably string[]), where the native buffer was allocated but left uninitialized, allowing native code to free garbage pointers and corrupt the heap.

Changes:

  • Zero-initialize the newly allocated native LPArray buffer for out-only (!In) array parameters in the IL marshaller.
  • Re-enable the previously skipped NativeAOT test for AsLPArrayTest now that the underlying issue is fixed.

Reviewed changes

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

File Description
src/tests/Interop/PInvoke/Array/MarshalArrayAsParam/AsLPArray/AsLPArrayTest.cs Removes the NativeAOT ActiveIssue skip so the regression scenario is exercised again.
src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs Ensures out-only LPArray allocations are zeroed via initblk, preventing invalid frees of uninitialized pointer slots.

@MichalStrehovsky
Copy link
Copy Markdown
Member

/azp run runtime-nativeaot-outerloop

Copy link
Copy Markdown
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

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

LGTM!

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Removed ActiveIssue attributes related to VARIANT marshalling and updated issue references.
@MichalStrehovsky
Copy link
Copy Markdown
Member

We had one more test disabled on this. The disablement also didn't make sense and was doubly disabled (looks like we ended up with both in some bulk updates). If there is any variant marshalling and the test fails, we'll just condition it correctly.

(Cc @jkoritzinsky)

@MichalStrehovsky
Copy link
Copy Markdown
Member

/azp run runtime-nativeaot-outerloop

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copilot AI review requested due to automatic review settings April 2, 2026 08:57
@MichalStrehovsky
Copy link
Copy Markdown
Member

/azp run runtime-nativeaot-outerloop

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Copy Markdown
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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@MichalStrehovsky
Copy link
Copy Markdown
Member

/azp run runtime-nativeaot-outerloop

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky
Copy link
Copy Markdown
Member

/ba-g GC test failure was fixed in #126440

@MichalStrehovsky MichalStrehovsky enabled auto-merge (squash) April 2, 2026 21:22
@MichalStrehovsky MichalStrehovsky merged commit 8f1bc42 into dotnet:main Apr 2, 2026
136 of 142 checks passed
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.

PInvoke/Array/MarshalArrayAsParam/AsLPArray/AsLPArrayTest failing

5 participants