Skip to content

Reject SAFEARRAY to IntPtr[] marshalling with mismatched native element size - #129444

Merged
elinor-fung merged 5 commits into
dotnet:mainfrom
elinor-fung:fix/safearray-intptr-size-mismatch
Jun 17, 2026
Merged

elinor-fung merged 5 commits into
dotnet:mainfrom
elinor-fung:fix/safearray-intptr-size-mismatch

Conversation

@elinor-fung

@elinor-fung elinor-fung commented Jun 15, 2026 •

Copy link
Copy Markdown
Member

When marshalling a SAFEARRAY as a IntPtr/UIntPtr array, we could end up going past the length of the array if the element size is less than the pointer size.

Reject the element size mismatch with SafeArrayTypeMismatchException

Note

This pull request was authored with assistance from GitHub Copilot.

…nt size

A native VT_I4/VT_INT/VT_UI4/VT_UINT SAFEARRAY has 4-byte elements, but IntPtr/UIntPtr is 8 bytes in a 64-bit process. When marshalling such a SAFEARRAY to or from a managed IntPtr[]/UIntPtr[], BlittableArrayMarshaler<T> copied sizeof(IntPtr) bytes per element, which does not match the 4-byte native element size.

Reject the element size mismatch with SafeArrayTypeMismatchException in GetInstantiatedSafeArrayMethod, the single point that instantiates the content converter for all SAFEARRAY marshalling paths, so the mismatch is detected before any data is copied.

Add a regression test (gated on Is64BitProcess) that exercises both marshalling directions via deliberately mismatched [MarshalAs(SafeArray, VT_I4)] IntPtr[,] declarations of the existing SafeArrayNative entry points.

Co-authored-by: Copilot <[email protected]>
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

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 adds a guard in CoreCLR SAFEARRAY marshalling to reject cases where the managed element type (IntPtr/UIntPtr) implies pointer-sized copies but the native SAFEARRAY VARTYPE implies a different element size (e.g., VT_I4 = 4 bytes). It also adds a regression test to ensure the mismatch fails fast with SafeArrayTypeMismatchException.

Changes:

  • Add an element-size mismatch check for IntPtr/UIntPtr in GetInstantiatedSafeArrayMethod, throwing SafeArrayTypeMismatchException before any content copy occurs.
  • Add a 64-bit-only regression test that validates both marshalling directions throw when [MarshalAs(SafeArraySubType = VT_I4)] is applied to an IntPtr[,].
Show a summary per file
File Description
src/coreclr/vm/olevariant.cpp Reject SAFEARRAY content converter instantiation when IntPtr/UIntPtr size doesn’t match the native VARTYPE element size.
src/tests/Interop/ArrayMarshalling/SafeArray/SafeArrayTest.cs Add regression coverage for the mismatched SAFEARRAY subtype vs IntPtr element size scenario (64-bit).

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings June 15, 2026 22:43

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.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Comment thread src/coreclr/vm/olevariant.cpp Resolved
Copilot AI review requested due to automatic review settings June 15, 2026 23:19

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.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Per PR feedback, co-locate the IntPtr/UIntPtr native-element-size validation with the element-type selection logic via a small GetPointerSizedElementTypeForSafeArrayVarType helper, instead of a separate check in GetInstantiatedSafeArrayMethod.

Co-authored-by: Copilot <[email protected]>
Comment thread src/coreclr/vm/olevariant.cpp Resolved
Comment thread src/coreclr/vm/olevariant.cpp Resolved
Comment thread src/coreclr/vm/olevariant.cpp Resolved
Comment thread src/coreclr/vm/olevariant.cpp Resolved
Co-authored-by: Aaron R Robinson <[email protected]>
Copilot AI review requested due to automatic review settings June 16, 2026 20:36

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.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new

@elinor-fung

Copy link
Copy Markdown
Member Author

/ba-g unrelated timeouts

@elinor-fung
elinor-fung merged commit 39a6fba into dotnet:main Jun 17, 2026
116 of 120 checks passed
@github-project-automation github-project-automation Bot moved this to Done in AppModel Jun 17, 2026
@elinor-fung
elinor-fung deleted the fix/safearray-intptr-size-mismatch branch June 17, 2026 20:42
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 18, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
…nt size (#129444)

When marshalling a SAFEARRAY as a `IntPtr`/`UIntPtr` array, we could end
up going past the length of the array if the element size is less than
the pointer size.

Reject the element size mismatch with `SafeArrayTypeMismatchException`.

---------

Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Aaron R Robinson <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants