From b058facc0d88718cab153e4fb1847e196267d244 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Wed, 3 Jul 2024 16:16:39 -0400 Subject: [PATCH] add array instance test --- .../cdacreader/tests/MethodTableTests.cs | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/native/managed/cdacreader/tests/MethodTableTests.cs b/src/native/managed/cdacreader/tests/MethodTableTests.cs index 1b28464b25f91f..7e659565391479 100644 --- a/src/native/managed/cdacreader/tests/MethodTableTests.cs +++ b/src/native/managed/cdacreader/tests/MethodTableTests.cs @@ -264,7 +264,7 @@ public void MethodTableEEClassInvalidThrows(MockTarget.Architecture arch) [Theory] [ClassData(typeof(MockTarget.StdArch))] - public void MethodTableGenericInstValid(MockTarget.Architecture arch) + public void ValidateGenericInstMethodTable(MockTarget.Architecture arch) { TargetTestHelpers targetTestHelpers = new(arch); const ulong SystemObjectMethodTableAddress = 0x00000000_7c000010; @@ -315,4 +315,62 @@ public void MethodTableGenericInstValid(MockTarget.Architecture arch) Assert.Equal(numMethods, metadataContract.GetNumMethods(genericInstanceMethodTableHandle)); }); } + + [Theory] + [ClassData(typeof(MockTarget.StdArch))] + public void ValidateArrayInstMethodTable(MockTarget.Architecture arch) + { + TargetTestHelpers targetTestHelpers = new(arch); + const ulong SystemObjectMethodTableAddress = 0x00000000_7c000010; + const ulong SystemObjectEEClassAddress = 0x00000000_7c0000d0; + TargetPointer systemObjectMethodTablePtr = new TargetPointer(SystemObjectMethodTableAddress); + TargetPointer systemObjectEEClassPtr = new TargetPointer(SystemObjectEEClassAddress); + + const ulong SystemArrayMethodTableAddress = 0x00000000_7c00a010; + const ulong SystemArrayEEClassAddress = 0x00000000_7c00a0d0; + TargetPointer systemArrayMethodTablePtr = new TargetPointer(SystemArrayMethodTableAddress); + TargetPointer systemArrayEEClassPtr = new TargetPointer(SystemArrayEEClassAddress); + + const ulong arrayInstanceMethodTableAddress = 0x00000000_330000a0; + const ulong arrayInstanceEEClassAddress = 0x00000000_330001d0; + TargetPointer arrayInstanceMethodTablePtr = new TargetPointer(arrayInstanceMethodTableAddress); + TargetPointer arrayInstanceEEClassPtr = new TargetPointer(arrayInstanceEEClassAddress); + + const uint arrayInstanceComponentSize = 392; + + RTSContractHelper(arch, + (builder) => + { + builder = AddSystemObject(targetTestHelpers, builder, systemObjectMethodTablePtr, systemObjectEEClassPtr); + const ushort systemArrayNumInterfaces = 4; + const ushort systemArrayNumMethods = 37; // Arbitrary. Not trying to exactly match the real System.Array + const uint systemArrayCorTypeAttr = (uint)(System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Class); + + builder = AddEEClass(targetTestHelpers, builder, systemArrayEEClassPtr, "EEClass System.Array", systemArrayMethodTablePtr, attr: systemArrayCorTypeAttr, numMethods: systemArrayNumMethods); + builder = AddMethodTable(targetTestHelpers, builder, systemArrayMethodTablePtr, "MethodTable System.Array", systemArrayEEClassPtr, + mtflags: default, mtflags2: default, baseSize: targetTestHelpers.ObjectBaseSize, + module: TargetPointer.Null, parentMethodTable: systemObjectMethodTablePtr, numInterfaces: systemArrayNumInterfaces, numVirtuals: 3); + + const uint arrayInst_mtflags = (uint)RuntimeTypeSystem_1.WFLAGS_HIGH.HasComponentSize | arrayInstanceComponentSize; + const uint arrayInstCorTypeAttr = (uint)(System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Class | System.Reflection.TypeAttributes.Sealed); + + builder = AddEEClass(targetTestHelpers, builder, arrayInstanceEEClassPtr, "EEClass ArrayInstance", arrayInstanceMethodTablePtr, attr: arrayInstCorTypeAttr, numMethods: systemArrayNumMethods); + builder = AddMethodTable(targetTestHelpers, builder, arrayInstanceMethodTablePtr, "MethodTable ArrayInstance", arrayInstanceEEClassPtr, + mtflags: arrayInst_mtflags, mtflags2: default, baseSize: targetTestHelpers.ObjectBaseSize, + module: TargetPointer.Null, parentMethodTable: systemArrayMethodTablePtr, numInterfaces: systemArrayNumInterfaces, numVirtuals: 3); + + return builder; + }, + (target) => + { + Contracts.IRuntimeTypeSystem metadataContract = target.Contracts.RuntimeTypeSystem; + Assert.NotNull(metadataContract); + Contracts.MethodTableHandle arrayInstanceMethodTableHandle = metadataContract.GetMethodTableHandle(arrayInstanceMethodTablePtr); + Assert.Equal(arrayInstanceMethodTablePtr.Value, arrayInstanceMethodTableHandle.Address.Value); + Assert.False(metadataContract.IsFreeObjectMethodTable(arrayInstanceMethodTableHandle)); + Assert.False(metadataContract.IsString(arrayInstanceMethodTableHandle)); + Assert.Equal(arrayInstanceComponentSize, metadataContract.GetComponentSize(arrayInstanceMethodTableHandle)); + }); + + } }