Repository navigation
Reject SAFEARRAY to IntPtr[] marshalling with mismatched native element size - #129444
Merged
elinor-fung merged 5 commits intoJun 17, 2026
Merged
Conversation
…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]>
Contributor
|
Tagging subscribers to this area: @dotnet/interop-contrib |
Contributor
There was a problem hiding this comment.
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/UIntPtrinGetInstantiatedSafeArrayMethod, throwingSafeArrayTypeMismatchExceptionbefore 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 anIntPtr[,].
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]>
elinor-fung
commented
Jun 15, 2026
This was referenced Jun 16, 2026
Open
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]>
AaronRobinsonMSFT
approved these changes
Jun 16, 2026
Co-authored-by: Aaron R Robinson <[email protected]>
Member
Author
|
/ba-g unrelated timeouts |
AaronRobinsonMSFT
approved these changes
Jun 17, 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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When marshalling a SAFEARRAY as a
IntPtr/UIntPtrarray, 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
SafeArrayTypeMismatchExceptionNote
This pull request was authored with assistance from GitHub Copilot.