Skip to content

Commit 708171d

Browse files
Block implementation details on System.Object (#67574)
When reflection blocking is disabled (like we do for libraries tests), still block implementation details on System.Object because tests (or anyone else for that matter) don't like to see them. Move implementation details methods on System.Object elsewhere.
1 parent 7db963a commit 708171d

File tree

31 files changed

+166
-193
lines changed

31 files changed

+166
-193
lines changed

src/coreclr/jit/importer.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3868,11 +3868,11 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
38683868
mustExpand |= IsTargetAbi(CORINFO_CORERT_ABI);
38693869
break;
38703870

3871+
case NI_Internal_Runtime_MethodTable_Of:
38713872
case NI_System_ByReference_ctor:
38723873
case NI_System_ByReference_get_Value:
38733874
case NI_System_Activator_AllocatorOf:
38743875
case NI_System_Activator_DefaultConstructorOf:
3875-
case NI_System_Object_MethodTableOf:
38763876
case NI_System_EETypePtr_EETypePtrOf:
38773877
mustExpand = true;
38783878
break;
@@ -4037,9 +4037,9 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
40374037
break;
40384038
}
40394039

4040+
case NI_Internal_Runtime_MethodTable_Of:
40404041
case NI_System_Activator_AllocatorOf:
40414042
case NI_System_Activator_DefaultConstructorOf:
4042-
case NI_System_Object_MethodTableOf:
40434043
case NI_System_EETypePtr_EETypePtrOf:
40444044
{
40454045
assert(IsTargetAbi(CORINFO_CORERT_ABI)); // Only CoreRT supports it.
@@ -5233,10 +5233,6 @@ NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method)
52335233
{
52345234
result = NI_System_Object_GetType;
52355235
}
5236-
else if (strcmp(methodName, "MethodTableOf") == 0)
5237-
{
5238-
result = NI_System_Object_MethodTableOf;
5239-
}
52405236
}
52415237
else if (strcmp(className, "RuntimeTypeHandle") == 0)
52425238
{
@@ -5336,6 +5332,16 @@ NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method)
53365332
}
53375333
}
53385334
}
5335+
else if (strcmp(namespaceName, "Internal.Runtime") == 0)
5336+
{
5337+
if (strcmp(className, "MethodTable") == 0)
5338+
{
5339+
if (strcmp(methodName, "Of") == 0)
5340+
{
5341+
result = NI_Internal_Runtime_MethodTable_Of;
5342+
}
5343+
}
5344+
}
53395345
else if (strcmp(namespaceName, "System.Threading") == 0)
53405346
{
53415347
if (strcmp(className, "Thread") == 0)

src/coreclr/jit/namedintrinsiclist.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ enum NamedIntrinsic : unsigned short
7979
NI_System_ByReference_get_Value,
8080
NI_System_Activator_AllocatorOf,
8181
NI_System_Activator_DefaultConstructorOf,
82-
NI_System_Object_MethodTableOf,
8382
NI_System_EETypePtr_EETypePtrOf,
8483

84+
NI_Internal_Runtime_MethodTable_Of,
85+
8586
NI_System_Runtime_CompilerServices_RuntimeHelpers_CreateSpan,
8687
NI_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray,
8788
NI_System_Runtime_CompilerServices_RuntimeHelpers_IsKnownConstant,

src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/StartupCodeHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private static unsafe object[] InitializeStatics(IntPtr gcStaticRegionStart, int
239239
// It actually has all GC fields including non-preinitialized fields and we simply copy over the
240240
// entire blob to this object, overwriting everything.
241241
IntPtr pPreInitDataAddr = *(pBlock + 1);
242-
RuntimeImports.RhBulkMoveWithWriteBarrier(ref obj.GetRawData(), ref *(byte *)pPreInitDataAddr, obj.GetRawDataSize());
242+
RuntimeImports.RhBulkMoveWithWriteBarrier(ref obj.GetRawData(), ref *(byte *)pPreInitDataAddr, obj.GetRawObjectDataSize());
243243
}
244244

245245
// Call write barrier directly. Assigning object reference does a type check.

src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ internal static bool SupportsWritableData
201201
}
202202
}
203203

204+
[Intrinsic]
205+
internal static extern MethodTable* Of<T>();
206+
204207
private ushort _usComponentSize;
205208
private ushort _usFlags;
206209
private uint _uBaseSize;

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ private static IntPtr RhpCidResolve_Worker(object pObject, IntPtr pCell)
3131
// We don't update the dispatch cell cache if this is IDynamicInterfaceCastable because this
3232
// scenario is by-design dynamic. There is no guarantee that another instance with the same MethodTable
3333
// as the one we just resolved would do the resolution the same way. We will need to ask again.
34-
if (!pObject.MethodTable->IsIDynamicInterfaceCastable)
34+
if (!pObject.GetMethodTable()->IsIDynamicInterfaceCastable)
3535
{
36-
return InternalCalls.RhpUpdateDispatchCellCache(pCell, pTargetCode, pObject.MethodTable, ref cellInfo);
36+
return InternalCalls.RhpUpdateDispatchCellCache(pCell, pTargetCode, pObject.GetMethodTable(), ref cellInfo);
3737
}
3838
else
3939
{
@@ -56,7 +56,7 @@ private static IntPtr RhpResolveInterfaceMethod(object pObject, IntPtr pCell)
5656
return IntPtr.Zero;
5757
}
5858

59-
MethodTable* pInstanceType = pObject.MethodTable;
59+
MethodTable* pInstanceType = pObject.GetMethodTable();
6060

6161
// This method is used for the implementation of LOAD_VIRT_FUNCTION and in that case the mapping we want
6262
// may already be in the cache.
@@ -98,7 +98,7 @@ private static IntPtr RhResolveDispatchOnType(EETypePtr instanceType, EETypePtr
9898
private static unsafe IntPtr RhResolveDispatchWorker(object pObject, void* cell, ref DispatchCellInfo cellInfo)
9999
{
100100
// Type of object we're dispatching on.
101-
MethodTable* pInstanceType = pObject.MethodTable;
101+
MethodTable* pInstanceType = pObject.GetMethodTable();
102102

103103
if (cellInfo.CellType == DispatchCellType.InterfaceAndSlot)
104104
{

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private struct OSCONTEXT
141141
private static void OnFirstChanceExceptionViaClassLib(object exception)
142142
{
143143
IntPtr pOnFirstChanceFunction =
144-
(IntPtr)InternalCalls.RhpGetClasslibFunctionFromEEType(exception.MethodTable, ClassLibFunctionId.OnFirstChance);
144+
(IntPtr)InternalCalls.RhpGetClasslibFunctionFromEEType(exception.GetMethodTable(), ClassLibFunctionId.OnFirstChance);
145145

146146
if (pOnFirstChanceFunction == IntPtr.Zero)
147147
{
@@ -161,7 +161,7 @@ private static void OnFirstChanceExceptionViaClassLib(object exception)
161161
private static void OnUnhandledExceptionViaClassLib(object exception)
162162
{
163163
IntPtr pOnUnhandledExceptionFunction =
164-
(IntPtr)InternalCalls.RhpGetClasslibFunctionFromEEType(exception.MethodTable, ClassLibFunctionId.OnUnhandledException);
164+
(IntPtr)InternalCalls.RhpGetClasslibFunctionFromEEType(exception.GetMethodTable(), ClassLibFunctionId.OnUnhandledException);
165165

166166
if (pOnUnhandledExceptionFunction == IntPtr.Zero)
167167
{

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/RuntimeExports.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ public static unsafe void RhUnboxAny(object? o, ref byte data, EETypePtr pUnboxT
171171

172172
if (ptrUnboxToEEType->IsNullable)
173173
{
174-
isValid = (o == null) || TypeCast.AreTypesEquivalent(o.MethodTable, ptrUnboxToEEType->NullableType);
174+
isValid = (o == null) || TypeCast.AreTypesEquivalent(o.GetMethodTable(), ptrUnboxToEEType->NullableType);
175175
}
176176
else
177177
{
178-
isValid = (o != null) && UnboxAnyTypeCompare(o.MethodTable, ptrUnboxToEEType);
178+
isValid = (o != null) && UnboxAnyTypeCompare(o.GetMethodTable(), ptrUnboxToEEType);
179179
}
180180

181181
if (!isValid)
@@ -207,7 +207,7 @@ public static unsafe void RhUnboxAny(object? o, ref byte data, EETypePtr pUnboxT
207207
[RuntimeExport("RhUnbox2")]
208208
public static unsafe ref byte RhUnbox2(MethodTable* pUnboxToEEType, object obj)
209209
{
210-
if ((obj == null) || !UnboxAnyTypeCompare(obj.MethodTable, pUnboxToEEType))
210+
if ((obj == null) || !UnboxAnyTypeCompare(obj.GetMethodTable(), pUnboxToEEType))
211211
{
212212
ExceptionIDs exID = obj == null ? ExceptionIDs.NullReference : ExceptionIDs.InvalidCast;
213213
throw pUnboxToEEType->GetClasslibException(exID);
@@ -218,7 +218,7 @@ public static unsafe ref byte RhUnbox2(MethodTable* pUnboxToEEType, object obj)
218218
[RuntimeExport("RhUnboxNullable")]
219219
public static unsafe void RhUnboxNullable(ref byte data, MethodTable* pUnboxToEEType, object obj)
220220
{
221-
if ((obj != null) && !TypeCast.AreTypesEquivalent(obj.MethodTable, pUnboxToEEType->NullableType))
221+
if ((obj != null) && !TypeCast.AreTypesEquivalent(obj.GetMethodTable(), pUnboxToEEType->NullableType))
222222
{
223223
throw pUnboxToEEType->GetClasslibException(ExceptionIDs.InvalidCast);
224224
}
@@ -242,7 +242,7 @@ public static unsafe void RhUnbox(object? obj, ref byte data, MethodTable* pUnbo
242242
return;
243243
}
244244

245-
MethodTable* pEEType = obj.MethodTable;
245+
MethodTable* pEEType = obj.GetMethodTable();
246246

247247
// Can unbox value types only.
248248
Debug.Assert(pEEType->IsValueType);
@@ -282,10 +282,10 @@ public static unsafe object RhMemberwiseClone(object src)
282282
{
283283
object objClone;
284284

285-
if (src.MethodTable->IsArray)
286-
objClone = RhNewArray(src.MethodTable, Unsafe.As<Array>(src).Length);
285+
if (src.GetMethodTable()->IsArray)
286+
objClone = RhNewArray(src.GetMethodTable(), Unsafe.As<Array>(src).Length);
287287
else
288-
objClone = RhNewObject(src.MethodTable);
288+
objClone = RhNewObject(src.GetMethodTable());
289289

290290
InternalCalls.RhpCopyObjectContents(objClone, src);
291291

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/TypeCast.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ internal enum AssignmentVariation
4444
[RuntimeExport("RhTypeCast_IsInstanceOfClass")]
4545
public static unsafe object IsInstanceOfClass(MethodTable* pTargetType, object obj)
4646
{
47-
if (obj == null || obj.MethodTable == pTargetType)
47+
if (obj == null || obj.GetMethodTable() == pTargetType)
4848
{
4949
return obj;
5050
}
5151

52-
MethodTable* pObjType = obj.MethodTable;
52+
MethodTable* pObjType = obj.GetMethodTable();
5353

5454
Debug.Assert(!pTargetType->IsParameterizedType, "IsInstanceOfClass called with parameterized MethodTable");
5555
Debug.Assert(!pTargetType->IsInterface, "IsInstanceOfClass called with interface MethodTable");
@@ -177,7 +177,7 @@ public static unsafe object IsInstanceOfArray(MethodTable* pTargetType, object o
177177
return null;
178178
}
179179

180-
MethodTable* pObjType = obj.MethodTable;
180+
MethodTable* pObjType = obj.GetMethodTable();
181181

182182
Debug.Assert(pTargetType->IsArray, "IsInstanceOfArray called with non-array MethodTable");
183183
Debug.Assert(!pTargetType->IsCloned, "cloned array types are disallowed");
@@ -244,7 +244,7 @@ public static unsafe object IsInstanceOfInterface(MethodTable* pTargetType, obje
244244
return null;
245245
}
246246

247-
MethodTable* pObjType = obj.MethodTable;
247+
MethodTable* pObjType = obj.GetMethodTable();
248248

249249
if (CastCache.AreTypesAssignableInternal_SourceNotTarget_BoxedSource(pObjType, pTargetType, null))
250250
return obj;
@@ -661,7 +661,7 @@ public static unsafe object CheckCastInterface(MethodTable* pTargetType, object
661661
return null;
662662
}
663663

664-
MethodTable* pObjType = obj.MethodTable;
664+
MethodTable* pObjType = obj.GetMethodTable();
665665

666666
if (CastCache.AreTypesAssignableInternal_SourceNotTarget_BoxedSource(pObjType, pTargetType, null))
667667
return obj;
@@ -687,21 +687,21 @@ public static unsafe void CheckArrayStore(object array, object obj)
687687
return;
688688
}
689689

690-
Debug.Assert(array.MethodTable->IsArray, "first argument must be an array");
690+
Debug.Assert(array.GetMethodTable()->IsArray, "first argument must be an array");
691691

692-
MethodTable* arrayElemType = array.MethodTable->RelatedParameterType;
693-
if (CastCache.AreTypesAssignableInternal(obj.MethodTable, arrayElemType, AssignmentVariation.BoxedSource, null))
692+
MethodTable* arrayElemType = array.GetMethodTable()->RelatedParameterType;
693+
if (CastCache.AreTypesAssignableInternal(obj.GetMethodTable(), arrayElemType, AssignmentVariation.BoxedSource, null))
694694
return;
695695

696696
// If object type implements IDynamicInterfaceCastable then there's one more way to check whether it implements
697697
// the interface.
698-
if (obj.MethodTable->IsIDynamicInterfaceCastable && IsInstanceOfInterfaceViaIDynamicInterfaceCastable(arrayElemType, obj, throwing: false))
698+
if (obj.GetMethodTable()->IsIDynamicInterfaceCastable && IsInstanceOfInterfaceViaIDynamicInterfaceCastable(arrayElemType, obj, throwing: false))
699699
return;
700700

701701
// Throw the array type mismatch exception defined by the classlib, using the input array's MethodTable*
702702
// to find the correct classlib.
703703

704-
throw array.MethodTable->GetClasslibException(ExceptionIDs.ArrayTypeMismatch);
704+
throw array.GetMethodTable()->GetClasslibException(ExceptionIDs.ArrayTypeMismatch);
705705
}
706706

707707
[RuntimeExport("RhTypeCast_CheckVectorElemAddr")]
@@ -712,9 +712,9 @@ public static unsafe void CheckVectorElemAddr(MethodTable* elemType, object arra
712712
return;
713713
}
714714

715-
Debug.Assert(array.MethodTable->IsArray, "second argument must be an array");
715+
Debug.Assert(array.GetMethodTable()->IsArray, "second argument must be an array");
716716

717-
MethodTable* arrayElemType = array.MethodTable->RelatedParameterType;
717+
MethodTable* arrayElemType = array.GetMethodTable()->RelatedParameterType;
718718

719719
if (!AreTypesEquivalent(elemType, arrayElemType)
720720
// In addition to the exactness check, add another check to allow non-exact matches through
@@ -730,7 +730,7 @@ public static unsafe void CheckVectorElemAddr(MethodTable* elemType, object arra
730730
// Throw the array type mismatch exception defined by the classlib, using the input array's MethodTable*
731731
// to find the correct classlib.
732732

733-
throw array.MethodTable->GetClasslibException(ExceptionIDs.ArrayTypeMismatch);
733+
throw array.GetMethodTable()->GetClasslibException(ExceptionIDs.ArrayTypeMismatch);
734734
}
735735
}
736736

@@ -746,7 +746,7 @@ internal struct ArrayElement
746746
public static unsafe void StelemRef(Array array, int index, object obj)
747747
{
748748
// This is supported only on arrays
749-
Debug.Assert(array.MethodTable->IsArray, "first argument must be an array");
749+
Debug.Assert(array.GetMethodTable()->IsArray, "first argument must be an array");
750750

751751
#if INPLACE_RUNTIME
752752
// this will throw appropriate exceptions if array is null or access is out of range.
@@ -766,12 +766,12 @@ public static unsafe void StelemRef(Array array, int index, object obj)
766766
ref object element = ref Unsafe.Add(ref rawData, index);
767767
#endif
768768

769-
MethodTable* elementType = array.MethodTable->RelatedParameterType;
769+
MethodTable* elementType = array.GetMethodTable()->RelatedParameterType;
770770

771771
if (obj == null)
772772
goto assigningNull;
773773

774-
if (elementType != obj.MethodTable)
774+
if (elementType != obj.GetMethodTable())
775775
goto notExactMatch;
776776

777777
doWrite:
@@ -785,7 +785,7 @@ public static unsafe void StelemRef(Array array, int index, object obj)
785785
notExactMatch:
786786
#if INPLACE_RUNTIME
787787
// This optimization only makes sense for inplace runtime where there's only one System.Object.
788-
if (array.MethodTable == MethodTableOf<object[]>())
788+
if (array.GetMethodTable() == MethodTable.Of<object[]>())
789789
goto doWrite;
790790
#endif
791791

@@ -795,15 +795,15 @@ public static unsafe void StelemRef(Array array, int index, object obj)
795795
[MethodImpl(MethodImplOptions.NoInlining)]
796796
private static unsafe void StelemRef_Helper(ref object element, MethodTable* elementType, object obj)
797797
{
798-
if (CastCache.AreTypesAssignableInternal(obj.MethodTable, elementType, AssignmentVariation.BoxedSource, null))
798+
if (CastCache.AreTypesAssignableInternal(obj.GetMethodTable(), elementType, AssignmentVariation.BoxedSource, null))
799799
{
800800
InternalCalls.RhpAssignRef(ref element, obj);
801801
}
802802
else
803803
{
804804
// If object type implements IDynamicInterfaceCastable then there's one more way to check whether it implements
805805
// the interface.
806-
if (!obj.MethodTable->IsIDynamicInterfaceCastable || !IsInstanceOfInterfaceViaIDynamicInterfaceCastable(elementType, obj, throwing: false))
806+
if (!obj.GetMethodTable()->IsIDynamicInterfaceCastable || !IsInstanceOfInterfaceViaIDynamicInterfaceCastable(elementType, obj, throwing: false))
807807
{
808808
// Throw the array type mismatch exception defined by the classlib, using the input array's
809809
// MethodTable* to find the correct classlib.
@@ -816,17 +816,17 @@ private static unsafe void StelemRef_Helper(ref object element, MethodTable* ele
816816
[RuntimeExport("RhpLdelemaRef")]
817817
public static unsafe ref object LdelemaRef(Array array, int index, IntPtr elementType)
818818
{
819-
Debug.Assert(array.MethodTable->IsArray, "first argument must be an array");
819+
Debug.Assert(array.GetMethodTable()->IsArray, "first argument must be an array");
820820

821821
MethodTable* elemType = (MethodTable*)elementType;
822-
MethodTable* arrayElemType = array.MethodTable->RelatedParameterType;
822+
MethodTable* arrayElemType = array.GetMethodTable()->RelatedParameterType;
823823

824824
if (!AreTypesEquivalent(elemType, arrayElemType))
825825
{
826826
// Throw the array type mismatch exception defined by the classlib, using the input array's MethodTable*
827827
// to find the correct classlib.
828828

829-
throw array.MethodTable->GetClasslibException(ExceptionIDs.ArrayTypeMismatch);
829+
throw array.GetMethodTable()->GetClasslibException(ExceptionIDs.ArrayTypeMismatch);
830830
}
831831

832832
ref object rawData = ref Unsafe.As<byte, object>(ref Unsafe.As<RawArrayData>(array).Data);

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/__Finalizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static unsafe void DrainQueue()
6060
// Call the finalizer on the current target object. If the finalizer throws we'll fail
6161
// fast via normal Redhawk exception semantics (since we don't attempt to catch
6262
// anything).
63-
((delegate*<object, void>)target.MethodTable->FinalizerCode)(target);
63+
((delegate*<object, void>)target.GetMethodTable()->FinalizerCode)(target);
6464
}
6565
}
6666
}

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ public static object CheckArgumentForDirectFieldAccess(object srcObject, Runtime
703703

704704
public static bool IsAssignable(object srcObject, RuntimeTypeHandle dstType)
705705
{
706-
EETypePtr srcEEType = srcObject.EETypePtr;
706+
EETypePtr srcEEType = srcObject.GetEETypePtr();
707707
return RuntimeImports.AreTypesAssignable(srcEEType, dstType.ToEETypePtr());
708708
}
709709

@@ -851,7 +851,7 @@ public static void ReportUnhandledException(Exception exception)
851851

852852
public static unsafe RuntimeTypeHandle GetRuntimeTypeHandleFromObjectReference(object obj)
853853
{
854-
return new RuntimeTypeHandle(obj.EETypePtr);
854+
return new RuntimeTypeHandle(obj.GetEETypePtr());
855855
}
856856

857857
// Move memory which may be on the heap which may have object references in it.

0 commit comments

Comments
 (0)