Skip to content

Commit 7cd266e

Browse files
authored
[cdac] Fix getting array function type in RuntimeTypeSystem.IsArrayMethod (#106512)
- Array method index should be the slot minus the number of virtuals, not the number of slots - Constructor index should include 3, not just greater than
1 parent 80f5d44 commit 7cd266e

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/native/managed/cdacreader/src/Contracts/RuntimeTypeSystem_1.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,14 +678,24 @@ public bool IsArrayMethod(MethodDescHandle methodDescHandle, out ArrayFunctionTy
678678
return false;
679679
}
680680

681-
int arrayMethodIndex = methodDesc.Slot - GetNumVtableSlots(GetTypeHandle(methodDesc.MethodTable));
682-
681+
// To get the array function index, subtract the number of virtuals from the method's slot
682+
// The array vtable looks like:
683+
// System.Object vtable
684+
// System.Array vtable
685+
// type[] vtable
686+
// Get
687+
// Set
688+
// Address
689+
// .ctor // possibly more
690+
// See ArrayMethodDesc for details in coreclr
691+
MethodTable methodTable = GetOrCreateMethodTable(methodDesc);
692+
int arrayMethodIndex = methodDesc.Slot - methodTable.NumVirtuals;
683693
functionType = arrayMethodIndex switch
684694
{
685695
0 => ArrayFunctionType.Get,
686696
1 => ArrayFunctionType.Set,
687697
2 => ArrayFunctionType.Address,
688-
> 3 => ArrayFunctionType.Constructor,
698+
>= 3 => ArrayFunctionType.Constructor,
689699
_ => throw new InvalidOperationException()
690700
};
691701

@@ -749,4 +759,11 @@ public bool IsILStub(MethodDescHandle methodDescHandle)
749759

750760
return AsDynamicMethodDesc(methodDesc).IsILStub;
751761
}
762+
763+
private MethodTable GetOrCreateMethodTable(MethodDesc methodDesc)
764+
{
765+
// Ensures that the method table is valid, created, and cached
766+
_ = GetTypeHandle(methodDesc.MethodTable);
767+
return _methodTables[methodDesc.MethodTable];
768+
}
752769
}

0 commit comments

Comments
 (0)