Skip to content

Commit f7cceb0

Browse files
authored
Native AOT: marshal empty array as non-null (#109360)
Fixes a discrepancy where coreclr marshals empty blittable arrays as non-null, but native AOT marshals them as null. With the fix, native AOT marshals them as non-null.
1 parent 6fa9cfc commit f7cceb0

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,16 +1377,11 @@ protected override void AllocAndTransformManagedToNative(ILCodeStream codeStream
13771377
{
13781378
ILLocalVariable vPinnedFirstElement = emitter.NewLocal(ManagedElementType.MakeByRefType(), true);
13791379

1380-
LoadManagedValue(codeStream);
1381-
codeStream.Emit(ILOpcode.ldlen);
1382-
codeStream.Emit(ILOpcode.conv_i4);
1383-
codeStream.Emit(ILOpcode.brfalse, lNullArray);
1384-
13851380
LoadManagedValue(codeStream);
13861381
codeStream.Emit(ILOpcode.call, emitter.NewToken(getArrayDataReferenceMethod));
13871382
codeStream.EmitStLoc(vPinnedFirstElement);
13881383

1389-
// Fall through. If array didn't have elements, vPinnedFirstElement is zeroinit.
1384+
// Fall through. If array is null, vPinnedFirstElement is zeroinit.
13901385
codeStream.EmitLabel(lNullArray);
13911386
codeStream.EmitLdLoc(vPinnedFirstElement);
13921387
codeStream.Emit(ILOpcode.conv_i);

src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,12 @@ private static void TestArrays()
404404

405405
ThrowIfNotEquals(0, CheckIncremental(arr, ArraySize), "Array marshalling failed");
406406

407+
int[] empty = new int[0];
408+
ThrowIfNotEquals(0, CheckIncremental(empty, 0), "Empty array marshalling failed");
409+
410+
int[] nullArray = null;
411+
ThrowIfNotEquals(1, CheckIncremental(nullArray, 0), "Null array marshalling failed");
412+
407413
Console.WriteLine("Testing marshalling blittable struct arrays");
408414

409415
Foo[] arr_foo = null;

0 commit comments

Comments
 (0)