Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More cleanups in Assembly/Loader/Binder area #58462

Merged
merged 11 commits into from
Sep 8, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ public partial class AssemblyLoadContext
private static extern IntPtr InitializeAssemblyLoadContext(IntPtr ptrAssemblyLoadContext, bool fRepresentsTPALoadContext, bool isCollectible);

[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
private static extern void PrepareForAssemblyLoadContextRelease(IntPtr ptrNativeAssemblyLoadContext, IntPtr ptrAssemblyLoadContextStrong);
private static extern void PrepareForAssemblyLoadContextRelease(IntPtr ptrNativeAssemblyBinder, IntPtr ptrAssemblyLoadContextStrong);

[RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")]
[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
private static extern void LoadFromStream(IntPtr ptrNativeAssemblyLoadContext, IntPtr ptrAssemblyArray, int iAssemblyArrayLen, IntPtr ptrSymbols, int iSymbolArrayLen, ObjectHandleOnStack retAssembly);
private static extern void LoadFromStream(IntPtr ptrNativeAssemblyBinder, IntPtr ptrAssemblyArray, int iAssemblyArrayLen, IntPtr ptrSymbols, int iSymbolArrayLen, ObjectHandleOnStack retAssembly);

[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
internal static extern void InternalSetProfileRoot(string directoryPath);

[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
internal static extern void InternalStartProfile(string? profile, IntPtr ptrNativeAssemblyLoadContext);
internal static extern void InternalStartProfile(string? profile, IntPtr ptrNativeAssemblyBinder);

[RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")]
[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
private static extern void LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, string? ilPath, string? niPath, ObjectHandleOnStack retAssembly);
private static extern void LoadFromPath(IntPtr ptrNativeAssemblyBinder, string? ilPath, string? niPath, ObjectHandleOnStack retAssembly);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Assembly[] GetLoadedAssemblies();
Expand Down Expand Up @@ -73,7 +73,7 @@ internal unsafe Assembly InternalLoad(ReadOnlySpan<byte> arrAssembly, ReadOnlySp

#if TARGET_WINDOWS
[DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
private static extern IntPtr LoadFromInMemoryModuleInternal(IntPtr ptrNativeAssemblyLoadContext, IntPtr hModule, ObjectHandleOnStack retAssembly);
private static extern IntPtr LoadFromInMemoryModuleInternal(IntPtr ptrNativeAssemblyBinder, IntPtr hModule, ObjectHandleOnStack retAssembly);


/// <summary>
Expand Down
73 changes: 36 additions & 37 deletions src/coreclr/binder/assemblybindercommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// Helper function in the VM, invoked by the Binder, to invoke the host assembly resolver
extern HRESULT RuntimeInvokeHostAssemblyResolver(INT_PTR pManagedAssemblyLoadContextToBindWithin,
BINDER_SPACE::AssemblyName *pAssemblyName,
DefaultAssemblyBinder *pTPABinder,
DefaultAssemblyBinder *pDefaultBinder,
BINDER_SPACE::Assembly **ppLoadedAssembly);

#endif // !defined(DACCESS_COMPILE)
Expand Down Expand Up @@ -619,10 +619,10 @@ namespace BINDER_SPACE

/* static */
HRESULT AssemblyBinderCommon::BindLocked(ApplicationContext *pApplicationContext,
AssemblyName *pAssemblyName,
bool skipVersionCompatibilityCheck,
bool excludeAppPaths,
BindResult *pBindResult)
AssemblyName *pAssemblyName,
bool skipVersionCompatibilityCheck,
bool excludeAppPaths,
BindResult *pBindResult)
{
HRESULT hr = S_OK;

Expand Down Expand Up @@ -688,8 +688,8 @@ namespace BINDER_SPACE

/* static */
HRESULT AssemblyBinderCommon::FindInExecutionContext(ApplicationContext *pApplicationContext,
AssemblyName *pAssemblyName,
ContextEntry **ppContextEntry)
AssemblyName *pAssemblyName,
ContextEntry **ppContextEntry)
{
_ASSERTE(pApplicationContext != NULL);
_ASSERTE(pAssemblyName != NULL);
Expand Down Expand Up @@ -759,10 +759,10 @@ namespace BINDER_SPACE

ReleaseHolder<Assembly> pAssembly;
hr = AssemblyBinderCommon::GetAssembly(relativePath,
FALSE /* fIsInTPA */,
&pAssembly,
NULL, // szMDAssemblyPath
bundleFileLocation);
FALSE /* fIsInTPA */,
&pAssembly,
NULL, // szMDAssemblyPath
bundleFileLocation);

BinderTracing::PathProbed(relativePath, BinderTracing::PathSource::Bundle, hr);

Expand Down Expand Up @@ -808,8 +808,8 @@ namespace BINDER_SPACE
CombinePath(fileName, relativePath, fileName);

hr = AssemblyBinderCommon::GetAssembly(fileName,
FALSE /* fIsInTPA */,
&pAssembly);
FALSE /* fIsInTPA */,
&pAssembly);
BinderTracing::PathProbed(fileName, pathSource, hr);

// Missing files are okay and expected when probing
Expand Down Expand Up @@ -979,9 +979,9 @@ namespace BINDER_SPACE
*/
/* static */
HRESULT AssemblyBinderCommon::BindByTpaList(ApplicationContext *pApplicationContext,
AssemblyName *pRequestedAssemblyName,
bool excludeAppPaths,
BindResult *pBindResult)
AssemblyName *pRequestedAssemblyName,
bool excludeAppPaths,
BindResult *pBindResult)
{
HRESULT hr = S_OK;

Expand Down Expand Up @@ -1152,7 +1152,6 @@ namespace BINDER_SPACE
HRESULT AssemblyBinderCommon::GetAssembly(SString &assemblyPath,
BOOL fIsInTPA,
Assembly **ppAssembly,

// If assemblyPath refers to a native image without metadata,
// szMDAssemblyPath gives the alternative file to get metadata.
LPCTSTR szMDAssemblyPath,
Expand Down Expand Up @@ -1210,7 +1209,7 @@ namespace BINDER_SPACE

/* static */
HRESULT AssemblyBinderCommon::Register(ApplicationContext *pApplicationContext,
BindResult *pBindResult)
BindResult *pBindResult)
{
HRESULT hr = S_OK;

Expand Down Expand Up @@ -1242,9 +1241,9 @@ namespace BINDER_SPACE

/* static */
HRESULT AssemblyBinderCommon::RegisterAndGetHostChosen(ApplicationContext *pApplicationContext,
LONG kContextVersion,
BindResult *pBindResult,
BindResult *pHostBindResult)
LONG kContextVersion,
BindResult *pBindResult,
BindResult *pHostBindResult)
{
HRESULT hr = S_OK;

Expand All @@ -1264,7 +1263,7 @@ namespace BINDER_SPACE
if (kContextVersion != pApplicationContext->GetVersion())
{
IF_FAIL_GO(AssemblyBinderCommon::OtherBindInterfered(pApplicationContext,
pBindResult));
pBindResult));

if (hr == S_FALSE)
{
Expand Down Expand Up @@ -1325,9 +1324,9 @@ namespace BINDER_SPACE

#if !defined(DACCESS_COMPILE)
HRESULT AssemblyBinderCommon::BindUsingHostAssemblyResolver(/* in */ INT_PTR pManagedAssemblyLoadContextToBindWithin,
/* in */ AssemblyName *pAssemblyName,
/* in */ DefaultAssemblyBinder *pTPABinder,
/* out */ Assembly **ppAssembly)
/* in */ AssemblyName *pAssemblyName,
/* in */ DefaultAssemblyBinder *pDefaultBinder,
/* out */ Assembly **ppAssembly)
{
HRESULT hr = E_FAIL;

Expand All @@ -1336,7 +1335,7 @@ HRESULT AssemblyBinderCommon::BindUsingHostAssemblyResolver(/* in */ INT_PTR pMa
// RuntimeInvokeHostAssemblyResolver will perform steps 2-4 of CustomAssemblyBinder::BindAssemblyByName.
BINDER_SPACE::Assembly *pLoadedAssembly = NULL;
hr = RuntimeInvokeHostAssemblyResolver(pManagedAssemblyLoadContextToBindWithin,
pAssemblyName, pTPABinder, &pLoadedAssembly);
pAssemblyName, pDefaultBinder, &pLoadedAssembly);
if (SUCCEEDED(hr))
{
_ASSERTE(pLoadedAssembly != NULL);
Expand All @@ -1348,11 +1347,11 @@ HRESULT AssemblyBinderCommon::BindUsingHostAssemblyResolver(/* in */ INT_PTR pMa

/* static */
HRESULT AssemblyBinderCommon::BindUsingPEImage(/* in */ AssemblyBinder* pBinder,
/* in */ BINDER_SPACE::AssemblyName *pAssemblyName,
/* in */ PEImage *pPEImage,
/* in */ PEKIND peKind,
/* in */ IMDInternalImport *pIMetaDataAssemblyImport,
/* [retval] [out] */ Assembly **ppAssembly)
/* in */ BINDER_SPACE::AssemblyName *pAssemblyName,
/* in */ PEImage *pPEImage,
/* in */ PEKIND peKind,
/* in */ IMDInternalImport *pIMetaDataAssemblyImport,
/* [retval] [out] */ Assembly **ppAssembly)
{
HRESULT hr = E_FAIL;

Expand Down Expand Up @@ -1460,12 +1459,12 @@ HRESULT AssemblyBinderCommon::BindUsingPEImage(/* in */ AssemblyBinder* pBinder
return hr;
}

HRESULT AssemblyBinderCommon::DefaultBinderSetupContext(DefaultAssemblyBinder** ppTPABinder)
HRESULT AssemblyBinderCommon::DefaultBinderSetupContext(DefaultAssemblyBinder** ppDefaultBinder)
{
HRESULT hr = S_OK;
EX_TRY
{
if (ppTPABinder != NULL)
if (ppDefaultBinder != NULL)
{
NewHolder<DefaultAssemblyBinder> pBinder;
SAFE_NEW(pBinder, DefaultAssemblyBinder);
Expand All @@ -1475,7 +1474,7 @@ HRESULT AssemblyBinderCommon::DefaultBinderSetupContext(DefaultAssemblyBinder**
if (SUCCEEDED(hr))
{
pBinder->SetManagedAssemblyLoadContext(NULL);
*ppTPABinder = pBinder.Extract();
*ppDefaultBinder = pBinder.Extract();
}
}
}
Expand All @@ -1485,9 +1484,9 @@ HRESULT AssemblyBinderCommon::DefaultBinderSetupContext(DefaultAssemblyBinder**
return hr;
}

HRESULT AssemblyBinderCommon::GetAssemblyIdentity(LPCSTR szTextualIdentity,
BINDER_SPACE::ApplicationContext* pApplicationContext,
NewHolder<AssemblyIdentityUTF8>& assemblyIdentityHolder)
HRESULT AssemblyBinderCommon::GetAssemblyIdentity(LPCSTR szTextualIdentity,
BINDER_SPACE::ApplicationContext* pApplicationContext,
NewHolder<AssemblyIdentityUTF8>& assemblyIdentityHolder)
{
HRESULT hr = S_OK;
_ASSERTE(szTextualIdentity != NULL);
Expand Down
34 changes: 16 additions & 18 deletions src/coreclr/binder/bindertracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ namespace

void GetAssemblyLoadContextNameFromManagedALC(INT_PTR managedALC, /* out */ SString &alcName)
{
if (managedALC == GetAppDomain()->GetTPABinderContext()->GetManagedAssemblyLoadContext())
if (managedALC == GetAppDomain()->GetDefaultBinder()->GetManagedAssemblyLoadContext())
{
alcName.Set(W("Default"));
return;
Expand All @@ -107,19 +107,17 @@ namespace
GCPROTECT_END();
}

void GetAssemblyLoadContextNameFromBindContext(AssemblyBinder *bindContext, AppDomain *domain, /*out*/ SString &alcName)
void GetAssemblyLoadContextNameFromBinder(AssemblyBinder *binder, AppDomain *domain, /*out*/ SString &alcName)
{
_ASSERTE(bindContext != nullptr);
_ASSERTE(binder != nullptr);

if (bindContext == domain->GetTPABinderContext())
if (binder->IsDefault())
{
alcName.Set(W("Default"));
}
else
{
CustomAssemblyBinder* alcBinder = static_cast<CustomAssemblyBinder*>(bindContext);

GetAssemblyLoadContextNameFromManagedALC(alcBinder->GetManagedAssemblyLoadContext(), alcName);
GetAssemblyLoadContextNameFromManagedALC(binder->GetManagedAssemblyLoadContext(), alcName);
}
}

Expand All @@ -128,11 +126,11 @@ namespace
_ASSERTE(spec != nullptr);

AppDomain *domain = spec->GetAppDomain();
AssemblyBinder* bindContext = spec->GetBindingContext();
if (bindContext == nullptr)
bindContext = spec->GetBindingContextFromParentAssembly(domain);
AssemblyBinder* binder = spec->GetBinder();
if (binder == nullptr)
binder = spec->GetBinderFromParentAssembly(domain);

GetAssemblyLoadContextNameFromBindContext(bindContext, domain, alcName);
GetAssemblyLoadContextNameFromBinder(binder, domain, alcName);
}

void PopulateBindRequest(/*inout*/ BinderTracing::AssemblyBindOperation::BindRequest &request)
Expand All @@ -154,11 +152,11 @@ namespace
peAssembly->GetDisplayName(request.RequestingAssembly);

AppDomain *domain = parentAssembly->GetAppDomain();
AssemblyBinder *bindContext = peAssembly->GetBindingContext();
if (bindContext == nullptr)
bindContext = domain->GetTPABinderContext(); // System.Private.CoreLib returns null
AssemblyBinder *binder = peAssembly->GetBinder();
if (binder == nullptr)
binder = domain->GetDefaultBinder(); // System.Private.CoreLib returns null

GetAssemblyLoadContextNameFromBindContext(bindContext, domain, request.RequestingAssemblyLoadContext);
GetAssemblyLoadContextNameFromBinder(binder, domain, request.RequestingAssemblyLoadContext);
}

GetAssemblyLoadContextNameFromSpec(spec, request.AssemblyLoadContext);
Expand Down Expand Up @@ -241,14 +239,14 @@ namespace BinderTracing

namespace BinderTracing
{
ResolutionAttemptedOperation::ResolutionAttemptedOperation(AssemblyName *assemblyName, AssemblyBinder* bindContext, INT_PTR managedALC, const HRESULT& hr)
ResolutionAttemptedOperation::ResolutionAttemptedOperation(AssemblyName *assemblyName, AssemblyBinder* binder, INT_PTR managedALC, const HRESULT& hr)
: m_hr { hr }
, m_stage { Stage::NotYetStarted }
, m_tracingEnabled { BinderTracing::IsEnabled() }
, m_assemblyNameObject { assemblyName }
, m_pFoundAssembly { nullptr }
{
_ASSERTE(bindContext != nullptr || managedALC != 0);
_ASSERTE(binder != nullptr || managedALC != 0);

if (!m_tracingEnabled)
return;
Expand All @@ -264,7 +262,7 @@ namespace BinderTracing
}
else
{
GetAssemblyLoadContextNameFromBindContext(bindContext, GetAppDomain(), m_assemblyLoadContextName);
GetAssemblyLoadContextNameFromBinder(binder, GetAppDomain(), m_assemblyLoadContextName);
}
}

Expand Down
Loading