Skip to content

[mono] Fix SizeConst array marshalling overflows - #137082

Merged
BrzVlad merged 1 commit into
dotnet:mainfrom
BrzVlad:fix-mono-struct-to-ptr
Nov 6, 2026
Merged

BrzVlad merged 1 commit into
dotnet:mainfrom
BrzVlad:fix-mono-struct-to-ptr

Conversation

@BrzVlad

@BrzVlad BrzVlad commented Nov 6, 2026

Copy link
Copy Markdown
Member

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.

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.
@azure-pipelines

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

@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.

@BrzVlad
BrzVlad requested a review from lateralusX November 6, 2026 12:48
@BrzVlad
BrzVlad merged commit 1a5e871 into dotnet:main Nov 6, 2026
110 of 112 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 7, 2026
@BrzVlad

BrzVlad commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@BrzVlad

BrzVlad commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

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.

2 participants