@@ -761,8 +761,10 @@ extern "C" EXPORT_API ObjectHandleID EXPORT_CC coreclr_unity_profiler_class_get_
761
761
if (pAssembly == NULL )
762
762
return NULL ;
763
763
764
+ // Use binder to get access to the managed AssemblyLoadContext handle.
765
+ // Note that the binder may be NULL if the assembly is being unloaded.
764
766
AssemblyBinder* pAssemblyBinder = pAssembly->GetPEAssembly ()->GetAssemblyBinder ();
765
- if (pAssemblyBinder->IsDefault ())
767
+ if (pAssemblyBinder == NULL || pAssemblyBinder ->IsDefault ())
766
768
return NULL ;
767
769
768
770
// ManagedAssemblyLoadContext is a handle to the managed AssemblyLoadContext object
@@ -777,8 +779,10 @@ extern "C" EXPORT_API ObjectHandleID EXPORT_CC coreclr_unity_profiler_get_manage
777
779
if (pAssembly == NULL )
778
780
return NULL ;
779
781
782
+ // Use binder to get access to the managed AssemblyLoadContext handle.
783
+ // Note that the binder may be NULL if the assembly is being unloaded.
780
784
AssemblyBinder* pAssemblyBinder = pAssembly->GetPEAssembly ()->GetAssemblyBinder ();
781
- if (pAssemblyBinder->IsDefault ())
785
+ if (pAssemblyBinder == NULL || pAssemblyBinder ->IsDefault ())
782
786
return NULL ;
783
787
784
788
// ManagedAssemblyLoadContext is a handle to the managed AssemblyLoadContext object
@@ -805,6 +809,39 @@ extern "C" EXPORT_API ObjectHandleID EXPORT_CC coreclr_unity_profiler_assembly_l
805
809
return (ObjectHandleID)loaderAllocator->GetLoaderAllocatorObjectHandle ();
806
810
}
807
811
812
+ // Return the AssemblyLoadContext handle for the given LoaderAllocator checking whether or not LoaderAllocator is still alive.
813
+ // LoaderAllocator is the main object that tracks AssemblyLoadContext liveness as AssemblyLoadContext is a simple wrapper around a native
814
+ // reference to LoaderAllocator.
815
+ extern " C" EXPORT_API ObjectHandleID EXPORT_CC coreclr_unity_profiler_loader_allocator_get_assembly_load_context_handle (void * nativeLoaderAllocator)
816
+ {
817
+ STATIC_CONTRACT_NOTHROW;
818
+
819
+ LoaderAllocator* loaderAllocator = (LoaderAllocator*)nativeLoaderAllocator;
820
+ if (loaderAllocator == NULL )
821
+ return NULL ;
822
+
823
+ // If it is already unloaded there is no need to return the handle
824
+ if (loaderAllocator->IsUnloaded ())
825
+ return NULL ;
826
+
827
+ // Ensure that the LoaderAllocator is AssemblyLoaderAllocator
828
+ LoaderAllocatorID* loaderAllocatorID = loaderAllocator->Id ();
829
+ if (loaderAllocatorID == NULL )
830
+ return NULL ;
831
+
832
+ if (loaderAllocatorID->GetType () != LAT_Assembly)
833
+ return NULL ;
834
+
835
+ // Use binder to get access to the managed AssemblyLoadContext handle
836
+ AssemblyLoaderAllocator* assemblyLoaderAllocator = (AssemblyLoaderAllocator*)loaderAllocator;
837
+ AssemblyBinder* pAssemblyBinder = assemblyLoaderAllocator->GetBinder ();
838
+ if (pAssemblyBinder == NULL )
839
+ return NULL ;
840
+
841
+ // ManagedAssemblyLoadContext is a handle to the managed AssemblyLoadContext object
842
+ return (ObjectHandleID)pAssemblyBinder->GetManagedAssemblyLoadContext ();
843
+ }
844
+
808
845
extern " C" EXPORT_API gboolean EXPORT_CC coreclr_unity_gc_concurrent_mode (gboolean state)
809
846
{
810
847
CONTRACTL
@@ -885,4 +922,4 @@ extern "C" EXPORT_API bool EXPORT_CC coreclr_unity_get_stackframe_info_from_ip(v
885
922
}
886
923
887
924
return false ;
888
- }
925
+ }
0 commit comments