Repository navigation
[release/11.0] [mono] Fix SizeConst array marshalling overflows - #137214
Open
github-actions[bot] wants to merge 1 commit into
Open
github-actions[bot] wants to merge 1 commit into
github-actions[bot] wants to merge 1 commit into
Conversation
For example a type declared as
```
[StructLayout(LayoutKind.Sequential)]
internal struct MyStruct
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public int[] Values;
}
```
can be marshalled with the `Marshal.StructureToPtr(str, buffer, false);`
to buffer. Mono had problems if the struct to marshal contained an array
that didn't match the `SizeConst` attribute argument. For blittable
types, like in the above definition, if the array was smaller, then we
would copy more bytes than the array holds. For non-blittable types, if
the array was larger, we would write more bytes, overflowing the
destination buffer. The fix throws exception at runtime if the array
doesn't contain enough elements and limits the copying to the
`SizeConst` amount, preventing both source read and destination write
overflows.
github-actions
Bot
requested review from
steveisok,
thaystg and
vitek-karas
as code owners
November 9, 2026 21:08
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/interop-contrib |
BrzVlad
approved these changes
Nov 9, 2026
This was referenced Nov 10, 2026
Member
|
Hi, the code complete date for 11.0.1 (the December 2026 release) is Monday 16 November. Make sure to merge this PR on that date at the latest, or it won't make it into that release. As a reminder, if this is a product change, you also need Tactics approval before merging this PR (test-only or infra-only changes don't require Tactics approval). Happy to help with the merge if needed! |
This branch has not been deployed
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Backport of #137082 to release/11.0
/cc @BrzVlad
Customer Impact
On all mono targets, when using
MarshalAsAttributewith theByValArrayoption, we would fail to check at runtime whether the array to marshal is matching theSizeConstfrom the attribute. If the application has such a bug in the marshalling code, it could accidentally do unsafe buffer overflow accesses, rather than throwing an exception.Regression
Testing
Reproduced the issue locally with a simple test.
Risk
Low. Fix is small and targeted, we add a check for the array size which throws an
ArgumentException, same way as coreclr does it.