[NativeAOT] Fix MD array GC series (#86877)
authorVladimir Sadov <vsadov@microsoft.com>
Tue, 30 May 2023 06:03:26 +0000 (23:03 -0700)
committerGitHub <noreply@github.com>
Tue, 30 May 2023 06:03:26 +0000 (23:03 -0700)
* Fix MD array GC series

* add a test

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/EETypeCreator.cs
src/tests/nativeaot/SmokeTests/DynamicGenerics/B282745.cs
src/tests/nativeaot/SmokeTests/DynamicGenerics/DynamicGenerics.main.cs

index 20e5377..9e44b2e 100644 (file)
@@ -565,7 +565,7 @@ namespace Internal.Runtime.TypeLoader
             {
                 if (numSeries > 0)
                 {
-                    *ptr-- = (short)((bitfield.Count - last + baseOffset - 2) * IntPtr.Size);
+                    *ptr-- = (short)((bitfield.Count - last) * IntPtr.Size);
                     *ptr-- = numPtrs;
 
                     *(void**)gcdesc = (void*)-numSeries;
index dada3d9..7e9ea6a 100644 (file)
@@ -125,6 +125,54 @@ public static class B282745
         GenericType<object>.test();
     }
 
+    struct SomeGenStruct1<T>
+    {
+        public T o;
+        public int i;
+        public long l;
+        public long l1;
+    }
+
+    public class GenericType1<T>
+    {
+        public static void test()
+        {
+            int[] lengths = { 42, 2, 3 };
+            SomeGenStruct1<T>[,,] array = (SomeGenStruct1<T>[,,])Array.CreateInstance(typeof(SomeGenStruct1<T>), lengths);
+
+            for (int i = 0; i < 42; i++)
+            {
+                array[i,0,0].o = default(T);
+                array[i,0,0].i = GetIntPtrOnHeapAsInt();
+                array[i,0,0].l = GetIntPtrOnHeapAsInt();
+
+                array[i,1,2].o = default(T);
+                array[i,1,2].i = GetIntPtrOnHeapAsInt();
+                array[i,1,2].l = GetIntPtrOnHeapAsLong();
+
+                array[i,1,1].o = default(T);
+                array[i,1,1].i = GetIntPtrOnHeapAsInt();
+                array[i,1,1].l = GetIntPtrOnHeapAsLong();
+            }
+
+            GC.Collect();
+            GC.KeepAlive(array);
+
+            RuntimeTypeHandle arrayTypeHandle = array.GetType().TypeHandle;
+#if INTERNAL_CONTRACTS
+            Assert.IsTrue(RuntimeAugments.IsDynamicType(arrayTypeHandle));
+#endif
+        }
+    }
+
+    [MethodImpl(MethodImplOptions.NoInlining)]
+    [TestMethod]
+    public static void testMDArrayWithPointerLikeValuesOfKnownStructTypeLargerType()
+    {
+        GenericType1<object>.test();
+    }
+
     [MethodImpl(MethodImplOptions.NoInlining)]
     [TestMethod]
     public static void testMDArrayWithPointerLikeValuesOfUnknownStructReferenceType()
index 483cbdf..ea56825 100644 (file)
@@ -144,6 +144,7 @@ new CoreFXTestLibrary.Internal.TestInfo("PartialUniversalGen.Test.TestCornerCase
 new CoreFXTestLibrary.Internal.TestInfo("B282745.testIntMDArrayWithPointerLikeValues", () => global::B282745.testIntMDArrayWithPointerLikeValues(), null),
 new CoreFXTestLibrary.Internal.TestInfo("B282745.testLongMDArrayWithPointerLikeValues", () => global::B282745.testLongMDArrayWithPointerLikeValues(), null),
 new CoreFXTestLibrary.Internal.TestInfo("B282745.testMDArrayWithPointerLikeValuesOfKnownStructType", () => global::B282745.testMDArrayWithPointerLikeValuesOfKnownStructType(), null),
+new CoreFXTestLibrary.Internal.TestInfo("B282745.testMDArrayWithPointerLikeValuesOfKnownStructTypeLargerType", () => global::B282745.testMDArrayWithPointerLikeValuesOfKnownStructTypeLargerType(), null),
 new CoreFXTestLibrary.Internal.TestInfo("B282745.testMDArrayWithPointerLikeValuesOfUnknownStructReferenceType", () => global::B282745.testMDArrayWithPointerLikeValuesOfUnknownStructReferenceType(), null),
 new CoreFXTestLibrary.Internal.TestInfo("B282745.testMDArrayWithPointerLikeValuesOfUnknownStructPrimitiveType", () => global::B282745.testMDArrayWithPointerLikeValuesOfUnknownStructPrimitiveType(), null),
 new CoreFXTestLibrary.Internal.TestInfo("B282745.testMDArrayWith3Dimensions", () => global::B282745.testMDArrayWith3Dimensions(), null),