Skip to content

Commit dd65835

Browse files
Merge pull request #284 from Unity-Technologies/update-loaderallocator-helperapi
Added coreclr_unity_profiler_loader_allocator_get_assembly_load_context_handle api
2 parents 4c642aa + 14f87e8 commit dd65835

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/coreclr/dlls/mscoree/mscorwks_ntdef.src

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ EXPORTS
3737
coreclr_unity_profiler_class_get_assembly_load_context_handle
3838
coreclr_unity_profiler_get_managed_assembly_load_context
3939
coreclr_unity_profiler_assembly_load_context_get_loader_allocator_handle
40+
coreclr_unity_profiler_loader_allocator_get_assembly_load_context_handle
4041
coreclr_unity_gc_concurrent_mode
4142
mono_assembly_get_assemblyref
4243
mono_class_get_name

src/coreclr/dlls/mscoree/mscorwks_unixexports.src

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ coreclr_unity_profiler_register
2323
coreclr_unity_profiler_class_get_assembly_load_context_handle
2424
coreclr_unity_profiler_get_managed_assembly_load_context
2525
coreclr_unity_profiler_assembly_load_context_get_loader_allocator_handle
26+
coreclr_unity_profiler_loader_allocator_get_assembly_load_context_handle
2627
coreclr_unity_gc_concurrent_mode
2728
mono_assembly_get_assemblyref
2829
mono_class_get_name

src/coreclr/vm/mono/mono_coreclr.cpp

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,10 @@ extern "C" EXPORT_API ObjectHandleID EXPORT_CC coreclr_unity_profiler_class_get_
761761
if (pAssembly == NULL)
762762
return NULL;
763763

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.
764766
AssemblyBinder* pAssemblyBinder = pAssembly->GetPEAssembly()->GetAssemblyBinder();
765-
if (pAssemblyBinder->IsDefault())
767+
if (pAssemblyBinder == NULL || pAssemblyBinder->IsDefault())
766768
return NULL;
767769

768770
// 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
777779
if (pAssembly == NULL)
778780
return NULL;
779781

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.
780784
AssemblyBinder* pAssemblyBinder = pAssembly->GetPEAssembly()->GetAssemblyBinder();
781-
if (pAssemblyBinder->IsDefault())
785+
if (pAssemblyBinder == NULL || pAssemblyBinder->IsDefault())
782786
return NULL;
783787

784788
// 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
805809
return (ObjectHandleID)loaderAllocator->GetLoaderAllocatorObjectHandle();
806810
}
807811

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+
808845
extern "C" EXPORT_API gboolean EXPORT_CC coreclr_unity_gc_concurrent_mode(gboolean state)
809846
{
810847
CONTRACTL
@@ -885,4 +922,4 @@ extern "C" EXPORT_API bool EXPORT_CC coreclr_unity_get_stackframe_info_from_ip(v
885922
}
886923

887924
return false;
888-
}
925+
}

0 commit comments

Comments
 (0)