Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Don't create a COM weak reference if the object is an aggregated COMW…
…rappers RCW.
  • Loading branch information
jkoritzinsky authored and github-actions committed Nov 6, 2021
commit 81851387939ade0e360fb42dad717a593148c756
2 changes: 1 addition & 1 deletion src/coreclr/vm/interoplibinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ComWrappersNative
static void MarkWrapperAsComActivated(_In_ IUnknown* wrapperMaybe);

public: // Unwrapping support
static IUnknown* GetIdentityForObject(_In_ OBJECTREF* objectPROTECTED, _In_ REFIID riid, _Out_ INT64* wrapperId);
static IUnknown* GetIdentityForObject(_In_ OBJECTREF* objectPROTECTED, _In_ REFIID riid, _Out_ INT64* wrapperId, _Out_ bool* isAggregated);
static bool HasManagedObjectComWrapper(_In_ OBJECTREF object, _Out_ bool* isActive);

public: // GC interaction
Expand Down
12 changes: 11 additions & 1 deletion src/coreclr/vm/interoplibinterface_comwrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ namespace
// The EOC is "detached" and no longer used to map between identity and a managed object.
// This will only be set if the EOC was inserted into the cache.
Flags_Detached = 8,

// This EOC is an aggregated instance
Flags_Aggregated = 16
};
DWORD Flags;

Expand Down Expand Up @@ -901,6 +904,12 @@ namespace
(uniqueInstance
? ExternalObjectContext::Flags_None
: ExternalObjectContext::Flags_InCache);

if (flags & CreateObjectFlags::CreateObjectFlags_Aggregated)
{
eocFlags |= ExternalObjectContext::Flags_Aggregated;
}

ExternalObjectContext::Construct(
resultHolder.GetContext(),
identity,
Expand Down Expand Up @@ -1774,7 +1783,7 @@ bool GlobalComWrappersForTrackerSupport::TryGetOrCreateObjectForComInstance(
objRef);
}

IUnknown* ComWrappersNative::GetIdentityForObject(_In_ OBJECTREF* objectPROTECTED, _In_ REFIID riid, _Out_ INT64* wrapperId)
IUnknown* ComWrappersNative::GetIdentityForObject(_In_ OBJECTREF* objectPROTECTED, _In_ REFIID riid, _Out_ INT64* wrapperId, _Out_ bool* isAggregated)
{
CONTRACTL
{
Expand Down Expand Up @@ -1807,6 +1816,7 @@ IUnknown* ComWrappersNative::GetIdentityForObject(_In_ OBJECTREF* objectPROTECTE
{
ExternalObjectContext* context = reinterpret_cast<ExternalObjectContext*>(contextMaybe);
*wrapperId = context->WrapperId;
*isAggregated = (context->Flags & ExternalObjectContext::Flags_Aggregated) != 0;

IUnknown* identity = reinterpret_cast<IUnknown*>(context->Identity);
GCX_PREEMP();
Expand Down
16 changes: 12 additions & 4 deletions src/coreclr/vm/weakreferencenative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ struct WeakHandleSpinLockHolder
//
// In order to qualify to be used with a HNDTYPE_WEAK_NATIVE_COM, the incoming object must:
// * be an RCW
// * not be an aggregated RCW
// * respond to a QI for IWeakReferenceSource
// * succeed when asked for an IWeakReference*
//
Expand Down Expand Up @@ -149,7 +150,14 @@ NativeComWeakHandleInfo* GetComWeakReferenceInfo(OBJECTREF* pObject)
#endif
{
#ifdef FEATURE_COMWRAPPERS
pWeakReferenceSource = reinterpret_cast<IWeakReferenceSource*>(ComWrappersNative::GetIdentityForObject(pObject, IID_IWeakReferenceSource, &wrapperId));
bool isAggregated = false;
pWeakReferenceSource = reinterpret_cast<IWeakReferenceSource*>(ComWrappersNative::GetIdentityForObject(pObject, IID_IWeakReferenceSource, &wrapperId, &isAggregated));
if (isAggregated)
{
// If the RCW is an aggregated RCW, then the managed object cannot be recreated from the IUnknown as the outer IUnknown wraps the managed object.
// In this case, don't create a weak reference backed by a COM weak reference.
pWeakReferenceSource = nullptr;
}
#endif
}

Expand Down Expand Up @@ -448,7 +456,7 @@ FCIMPL3(void, WeakReferenceNative::Create, WeakReferenceObject * pThisUNSAFE, Ob
_ASSERTE(gc.pThis->GetMethodTable()->CanCastToClass(pWeakReferenceMT));

// Create the handle.
#if defined(FEATURE_COMINTEROP) || defined(FEATURE_COMWRAPPERS)
#if defined(FEATURE_COMINTEROP) || defined(FEATURE_COMWRAPPERS)
NativeComWeakHandleInfo *comWeakHandleInfo = nullptr;
if (gc.pTarget != NULL)
{
Expand Down Expand Up @@ -690,7 +698,7 @@ FCIMPL1(Object *, WeakReferenceNative::GetTarget, WeakReferenceObject * pThisUNS

OBJECTREF pTarget = GetWeakReferenceTarget(pThis);

#if defined(FEATURE_COMINTEROP) || defined(FEATURE_COMWRAPPERS)
#if defined(FEATURE_COMINTEROP) || defined(FEATURE_COMWRAPPERS)
// If we found an object, or we're not a native COM weak reference, then we're done. Othewrise
// we can try to create a new RCW to the underlying native COM object if it's still alive.
if (pTarget != NULL || !IsNativeComWeakReferenceHandle(pThis->m_Handle))
Expand Down Expand Up @@ -718,7 +726,7 @@ FCIMPL1(Object *, WeakReferenceOfTNative::GetTarget, WeakReferenceObject * pThis
OBJECTREF pTarget = GetWeakReferenceTarget(pThis);


#if defined(FEATURE_COMINTEROP) || defined(FEATURE_COMWRAPPERS)
#if defined(FEATURE_COMINTEROP) || defined(FEATURE_COMWRAPPERS)
// If we found an object, or we're not a native COM weak reference, then we're done. Othewrise
// we can try to create a new RCW to the underlying native COM object if it's still alive.
if (pTarget != NULL || !IsNativeComWeakReferenceHandle(pThis->m_Handle))
Expand Down