-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Use (Un)ManagedValuesSource.Length as length condition in marshalling and unmarshalling loops #118190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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 marshalling issue where null arrays with non-zero length parameters could cause crashes or undefined behavior. The fix ensures that marshalling loops use the actual length of the spans returned from marshaller methods rather than assuming they match the provided length parameter.
Key changes:
- Modified marshalling source generation to use span lengths instead of provided length parameters
- Updated array and span marshallers to handle null unmanaged values by returning empty spans
- Added comprehensive tests for null array scenarios across different marshalling contexts
Reviewed Changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ElementsMarshalling.cs |
Updates code generation to use span.Length instead of numElements parameter in marshalling loops |
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/ArrayMarshaller.cs |
Adds null checks to return empty spans when unmanaged pointers are null |
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/PointerArrayMarshaller.cs |
Adds null checks to return empty spans when unmanaged pointers are null |
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/ReadOnlySpanMarshaller.cs |
Adds null checks to return empty spans when unmanaged pointers are null |
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/SpanMarshaller.cs |
Adds null checks to return empty spans when unmanaged pointers are null |
src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/ComInterfaces/INullableOutArray.cs |
Test interface for nullable array scenarios with COM interop |
src/libraries/System.Runtime.InteropServices/tests/TestAssets/SharedTypes/ComInterfaces/INullArrayCases.cs |
Comprehensive test interface covering various null array edge cases |
src/libraries/System.Runtime.InteropServices/tests/TestAssets/NativeExports/Arrays.cs |
Native export methods for testing null array scenarios |
src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.Tests/ArrayTests.cs |
Unit tests for null array handling in LibraryImportGenerator |
src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Tests/INullableOutArrayTests.cs |
Test for nullable array marshalling in COM scenarios |
src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Tests/INullArrayTests.cs |
Comprehensive tests for null array edge cases in COM interop |
|
Tagging subscribers to this area: @dotnet/interop-contrib |
...ies/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/ArrayMarshaller.cs
Show resolved
Hide resolved
...tem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/PointerArrayMarshaller.cs
Show resolved
Hide resolved
...tem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/ReadOnlySpanMarshaller.cs
Show resolved
Hide resolved
...ries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/SpanMarshaller.cs
Show resolved
Hide resolved
...ries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/SpanMarshaller.cs
Show resolved
Hide resolved
...ibraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Tests/INullArrayTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Aaron Robinson <[email protected]>
… into ComNullableArgs
… and unmarshalling loops (dotnet#118190) Instead of assuming the Span returned from Get(Un)ManagedValuesSource is the same length as the length parameter, use the length of the span returned from the marshaller methods in the for loop condition. This allows the marshaller to recognize native values that have a different length than expected. In particular, if an array parameter is null, allow the ArrayMarshaller to return empty spans. Also, updates the Array, Span, ReadOnlySpan, and PointerArray marshallers to check for null unmanaged values when creating unmanaged source/destination spans. Co-authored-by: Aaron Robinson <[email protected]>
Fixes #118135
Instead of assuming the Span returned from Get(Un)ManagedValuesSource is the same length as the length parameter, use the length of the span returned from the marshaller methods in the for loop condition. This allows the marshaller to recognize native values that have a different length than expected. In particular, if an array parameter is null, allow the ArrayMarshaller to return empty spans.
Also, updates the Array, Span, ReadOnlySpan, and PointerArray marshallers to check for null unmanaged values when creating unmanaged source/destination spans.