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

Exit the lock before we call into user code and handle losing the race for the RCW table #110828

Merged
merged 16 commits into from
Jan 7, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -968,21 +968,40 @@ private unsafe bool TryGetOrCreateObjectForComInstanceInternal(
_rcwCache.Remove(identity);
}
}
}

if (wrapperMaybe is not null)
{
retValue = wrapperMaybe;

if (wrapperMaybe is not null)
using (_lock.EnterScope())
{
retValue = wrapperMaybe;
NativeObjectWrapper wrapper = NativeObjectWrapper.Create(
identity,
inner,
this,
retValue,
flags);
if (!s_rcwTable.TryAdd(retValue, wrapper))
if (!s_rcwTable.TryGetValue(retValue, out NativeObjectWrapper? wrapper))
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
{
wrapper.Release();
throw new NotSupportedException();
// Exit the lock before we create the native object wrapper
// as we may execute arbitrary user code.
_lock.Exit();
try
{
NativeObjectWrapper newWrapper = NativeObjectWrapper.Create(
identity,
inner,
this,
retValue,
flags);

wrapper = s_rcwTable.GetValue(retValue, _ => newWrapper);
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
if (wrapper != newWrapper)
{
newWrapper.Release();
}
}
finally
{
_lock.Enter();
}
}

_rcwCache.Add(identity, wrapper._proxyHandle);
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
AddWrapperToReferenceTrackerHandleCache(wrapper);
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
return true;
Expand Down Expand Up @@ -1026,16 +1045,17 @@ private unsafe bool TryGetOrCreateObjectForComInstanceInternal(

if (flags.HasFlag(CreateObjectFlags.UniqueInstance))
{
NativeObjectWrapper wrapper = NativeObjectWrapper.Create(
NativeObjectWrapper newWrapper = NativeObjectWrapper.Create(
identity,
inner,
null, // No need to cache NativeObjectWrapper for unique instances. They are not cached.
retValue,
flags);
if (!s_rcwTable.TryAdd(retValue, wrapper))

NativeObjectWrapper wrapper = s_rcwTable.GetValue(retValue, _ => newWrapper);
if (wrapper != newWrapper)
{
wrapper.Release();
throw new NotSupportedException();
newWrapper.Release();
}
AddWrapperToReferenceTrackerHandleCache(wrapper);
return true;
Expand All @@ -1061,17 +1081,19 @@ private unsafe bool TryGetOrCreateObjectForComInstanceInternal(
}
else
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
{
NativeObjectWrapper wrapper = NativeObjectWrapper.Create(
NativeObjectWrapper newWrapper = NativeObjectWrapper.Create(
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
identity,
inner,
this,
retValue,
flags);
if (!s_rcwTable.TryAdd(retValue, wrapper))

NativeObjectWrapper wrapper = s_rcwTable.GetValue(retValue, _ => newWrapper);
if (wrapper != newWrapper)
{
wrapper.Release();
throw new NotSupportedException();
newWrapper.Release();
}

_rcwCache.Add(identity, wrapper._proxyHandle);
AddWrapperToReferenceTrackerHandleCache(wrapper);
}
Expand Down
Loading