From 5206cf4e7b6dd9d203d86c30f41aee2387c4c8dc Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Tue, 29 Oct 2024 14:58:54 -0700 Subject: [PATCH 1/2] Native AOT: marshal empty array as non-null --- .../tools/Common/TypeSystem/Interop/IL/Marshaller.cs | 7 +------ src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs | 3 +++ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs b/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs index 4160711b6a3601..471a9f30a6b182 100644 --- a/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs +++ b/src/coreclr/tools/Common/TypeSystem/Interop/IL/Marshaller.cs @@ -1377,16 +1377,11 @@ protected override void AllocAndTransformManagedToNative(ILCodeStream codeStream { ILLocalVariable vPinnedFirstElement = emitter.NewLocal(ManagedElementType.MakeByRefType(), true); - LoadManagedValue(codeStream); - codeStream.Emit(ILOpcode.ldlen); - codeStream.Emit(ILOpcode.conv_i4); - codeStream.Emit(ILOpcode.brfalse, lNullArray); - LoadManagedValue(codeStream); codeStream.Emit(ILOpcode.call, emitter.NewToken(getArrayDataReferenceMethod)); codeStream.EmitStLoc(vPinnedFirstElement); - // Fall through. If array didn't have elements, vPinnedFirstElement is zeroinit. + // Fall through. If array is null, vPinnedFirstElement is zeroinit. codeStream.EmitLabel(lNullArray); codeStream.EmitLdLoc(vPinnedFirstElement); codeStream.Emit(ILOpcode.conv_i); diff --git a/src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs b/src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs index 92fc0284e913e2..b526adab7d9e39 100644 --- a/src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs +++ b/src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs @@ -404,6 +404,9 @@ private static void TestArrays() ThrowIfNotEquals(0, CheckIncremental(arr, ArraySize), "Array marshalling failed"); + int[] empty = new int[0]; + ThrowIfNotEquals(0, CheckIncremental(empty, 0), "Empty array marshalling failed"); + Console.WriteLine("Testing marshalling blittable struct arrays"); Foo[] arr_foo = null; From 21f5e49ede75e2403bade8efcfe620654e742ea9 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Wed, 30 Oct 2024 13:04:05 -0700 Subject: [PATCH 2/2] Add smoke test for null array --- src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs b/src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs index b526adab7d9e39..d1edfff6c4eb23 100644 --- a/src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs +++ b/src/tests/nativeaot/SmokeTests/PInvoke/PInvoke.cs @@ -407,6 +407,9 @@ private static void TestArrays() int[] empty = new int[0]; ThrowIfNotEquals(0, CheckIncremental(empty, 0), "Empty array marshalling failed"); + int[] nullArray = null; + ThrowIfNotEquals(1, CheckIncremental(nullArray, 0), "Null array marshalling failed"); + Console.WriteLine("Testing marshalling blittable struct arrays"); Foo[] arr_foo = null;