diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs
index d4c2e93237d078..94683cea769356 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs
@@ -449,6 +449,14 @@ internal static unsafe bool ObjectHasComponentSize(object obj)
return GetMethodTable(obj)->HasComponentSize;
}
+ // Returns true iff the type of the object declares a finalizer.
+ // Callers are required to keep obj alive
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static unsafe bool ObjectHasFinalizer(object obj)
+ {
+ return GetMethodTable(obj)->HasFinalizer;
+ }
+
///
/// Boxes a given value using an input to determine its type.
///
diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs
index 487c460e1c1b23..4f392bf2bac552 100644
--- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs
+++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs
@@ -213,6 +213,14 @@ internal static unsafe bool ObjectHasComponentSize(object obj)
return GetMethodTable(obj)->HasComponentSize;
}
+ // Returns true iff the type of the object declares a finalizer.
+ // Callers are required to keep obj alive
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ internal static unsafe bool ObjectHasFinalizer(object obj)
+ {
+ return GetMethodTable(obj)->IsFinalizable;
+ }
+
public static void PrepareMethod(RuntimeMethodHandle method)
{
if (method.Value == IntPtr.Zero)
diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
index 7bc461cef8d54c..e24968b025bdd3 100644
--- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
+++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
@@ -186,7 +186,8 @@ internal static void DetachNonPromotedObjects()
ReferenceTrackerNativeObjectWrapper? nativeObjectWrapper = Unsafe.As(weakNativeObjectWrapperHandle.Target);
if (nativeObjectWrapper != null &&
nativeObjectWrapper.TrackerObject != IntPtr.Zero &&
- !RuntimeImports.RhIsPromoted(nativeObjectWrapper.ProxyHandle.Target))
+ nativeObjectWrapper.ProxyHandle.TryGetTarget(out object? proxyTarget) &&
+ !RuntimeImports.RhIsPromoted(proxyTarget))
{
// Notify the wrapper it was not promoted and is being collected.
BeforeWrapperFinalized(nativeObjectWrapper.TrackerObject);
@@ -205,9 +206,9 @@ internal static unsafe class FindReferenceTargetsCallback
internal ref struct Instance
{
private readonly IntPtr _vtable; // First field is IUnknown based vtable.
- public GCHandle RootObject;
+ public WeakGCHandle