You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 6634a7c
Browse filesBrowse the repository at this point in the historyBrowse files
Fix pointer array copying and various array pinning regressions in CoreCLR (#134284)
<!-- -->
## Summary
Fix copying and pinning regressions in CoreCLR's built-in array
marshalling:
- **Incorrect copying:** the array metadata retained the pointee type
while using a pointer-sized VARTYPE. The generic marshaler consequently
used the pointee's size: a four-element `byte*[]` allocated and copied
four bytes instead of 32 on x64.
- **Lost pinning:** the pinning predicate required a blittable
value-type method table, excluding pointer and function-pointer type
descriptors.
- **Enum pinning:** evaluate pinning against the enum's underlying
primitive instead of the enum's own layout flags.
- **Unicode character pinning:** recognize `char[]` when its selected
native representation is UTF-16, including explicit `I2`/`U2` element
types. ANSI and Boolean conversions continue to use copying.
Preserve the actual element `TypeHandle` in the marshalling operands,
and select `nint` only as the carrier for generic copying. Managed
allocation retains the declared pointer-array type and preserves null
native arrays. Eligible forward P/Invokes use the existing pinning path;
ref/out and reverse calls continue to copy.
Treat pointer elements as opaque native-sized values, including function
pointers, struct pointers, and pointer-to-pointer elements. Update the
managed compatibility assertions and native layout consumers
accordingly. SAFEARRAY pointer support and non-pointer conversion rules
are not broadened.
## Regression coverage
Added **324 cases** in the existing interop suites: 250
pointer/native-integer cases and 74 enum/character/conversion-control
cases. Coverage includes forward P/Invoke and unmanaged-function-pointer
delegates, required copying, exact copy-back array types, ref/out
parameters, reverse callbacks, fixed-array fields, null/empty and
odd-length arrays, and pinning across compacting GC. Pointer coverage
includes `float*`, struct pointers, pointer-to-pointer elements, and
managed/unmanaged function pointers.
The enum cases cover all eight standard underlying integral types.
Character cases cover UTF-16, ANSI, and explicit element-type overrides;
Boolean controls ensure conversion still occurs. Assertions distinguish
passing managed contents directly from copying. Native exports and
callback types explicitly use Cdecl for x86.
## Validation
The product fixes and full 324-case regression set were validated
locally:
| Runtime configuration | New cases passed | Total related array cases
passed |
| --- | ---: | ---: |
| Windows x64 Checked | 324 | 344 |
| Windows x64 Release | 324 | 344 |
| Windows x86 Checked | 324 | 343 |
The x86 difference is a 64-bit-only SAFEARRAY element-size mismatch
control. Result XML was checked for failures, rather than relying only
on the merged runner's exit code.
After the final test-only renames and runtime-specific data splits, the
x64 Checked managed interop tests were rebuilt and rerun: **344 passed,
0 failed**, including all 324 added cases. The Release and x86 results
above precede those test-only changes.
Baseline copying, pinning, and function-pointer failures were
reproduced. With only the copying repair applied, 80 copy cases passed
while all four pinning-direction checks still failed, verifying that
pinning does not merely hide a broken copy path.
The enum/character additions were also run before their fix: 32 pinning
cases failed while all ANSI and Boolean conversion controls passed. All
74 added enum/character/control cases pass with the updated predicate.
`IntPtr[]` and `UIntPtr[]` already behaved correctly on this checkout;
their product behavior is unchanged and is covered by the new tests.
## Runtime-specific test exclusions
CI exposed separate NativeAOT and Mono issues; this PR does **not**
change either runtime's implementation. Split theory data into passing
and failing partitions and use narrowly scoped `ActiveIssue` attributes:
- **NativeAOT:** exclude exactly 27 failing rows: null fixed-array
fields, null ref/out arrays, and nonempty reverse-copy cases. Tracked by
#134628; the remaining 223 pointer cases stay enabled.
- **Compiled Mono:** exclude exactly 17 failing function-pointer rows,
tracked by #90308. Null function-pointer rows, ordinary
data-pointer rows, and Mono interpreter function-pointer coverage remain
enabled. Other method-level Mono exclusions for copying, layout, and
character semantics remain.
- **WebAssembly:** retain the pointer-test exclusion for the shared test
host's missing unmanaged-callback thunks, tracked by
#124219.
The newly excluded row sets match the recorded CI failures exactly, and
the generated merged runner's predicates and skip reporting were
checked. NativeAOT and Mono behavior was investigated from CI evidence
and source inspection; neither runtime was rerun locally. Linux and
ARM64 were not run locally.
## Generated-code comparison
Release x64 P/Invoke stubs, with tiering and ReadyToRun disabled:
| Direction | Baseline bytes | Fixed bytes | Baseline instructions |
Fixed instructions |
| --- | ---: | ---: | ---: | ---: |
| Default | 294 | 185 | 86 | 58 |
| In | 294 | 185 | 86 | 58 |
| Out | 303 | 185 | 89 | 58 |
| In/Out | 332 | 185 | 97 | 58 |
The fixed pinning stubs contain no unmanaged-allocation helper,
`Memmove`, or `CoTaskMemFree` calls. These are code-generation
measurements, not benchmark timings.
For the new enum and UTF-16 declarations, Release x64 stubs shrink from
281-285 bytes (82-83 instructions) to 170 bytes (54 instructions),
likewise eliminating allocation/copy/free calls. The ANSI and Boolean
control stubs retain their previous code sizes and call paths.
Resolves#134174Resolves#134576Resolves#134577Resolves#134575Resolves#134573
> [!NOTE]
> This PR was created with assistance from GitHub Copilot.
---------
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 3a58d7c5-bde9-4e44-a0bb-320dd6b721df
internal sealed class LPSTRArrayElementMarshaler<TBestFit,TThrowOnUnmappable>:IArrayElementMarshaler<string?,LPSTRArrayElementMarshaler<TBestFit,TThrowOnUnmappable>>
$"Managedarray type {managed.GetType()}is not compatiblewith expected element type {typeof(T)}");
2474
+
}
2475
+
2459
2476
public staticunsafevoidConvertArrayContentsToUnmanaged<T,TMarshaler>(Arraymanaged,byte*pNative,intnumElements)
2460
2477
where TMarshaler : IArrayMarshaler<T,TMarshaler>
2461
2478
{
2462
-
// Assert that the array is actually an array of compatible type.
2463
-
Debug.Assert(managedis not null);
2464
-
Debug.Assert(managed.GetType().GetElementType()!.MakeArrayType().IsAssignableTo(typeof(T[])), $"Managedarray type {managed.GetType()}is not compatiblewith expected element type {typeof(T)}");
public staticunsafevoidConvertArrayContentsToManaged<T,TMarshaler>(Arraymanaged,byte*pNative,intnumElements)
2469
2484
where TMarshaler : IArrayMarshaler<T,TMarshaler>
2470
2485
{
2471
-
// Assert that the array is actually an array of compatible type.
2472
-
Debug.Assert(managedis not null);
2473
-
Debug.Assert(managed.GetType().GetElementType()!.MakeArrayType().IsAssignableTo(typeof(T[])), $"Managedarray type {managed.GetType()}is not compatiblewith expected element type {typeof(T)}");
0 commit comments