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

Make global ComWrappers for marshalling respect requested IID #36054

Merged
merged 2 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 23 additions & 10 deletions src/coreclr/src/vm/interopconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,15 @@ IUnknown *GetComIPFromObjectRef(OBJECTREF *poref, MethodTable *pMT, BOOL bSecuri

if (TryGetComIPFromObjectRefUsingComWrappers(*poref, &pUnk))
{
pUnk.SuppressRelease();
RETURN pUnk;
GUID iid;
pMT->GetGuid(&iid, /*bGenerateIfNotFound*/ FALSE, /*bClassic*/ FALSE);

IUnknown* pvObj;
hr = SafeQueryInterface(pUnk, iid, &pvObj);
if (FAILED(hr))
COMPlusThrowHR(hr);

RETURN pvObj;
}

SyncBlock* pBlock = (*poref)->GetSyncBlock();
Expand Down Expand Up @@ -177,15 +184,20 @@ IUnknown *GetComIPFromObjectRef(OBJECTREF *poref, ComIpType ReqIpType, ComIpType
{
hr = S_OK;

SafeComHolder<IUnknown> pvObj;
IUnknown* pvObj;
if (ReqIpType & ComIpType_Dispatch)
{
hr = pUnk->QueryInterface(IID_IDispatch, &pvObj);
hr = SafeQueryInterface(pUnk, IID_IDispatch, &pvObj);
pUnk->Release();
}
else if (ReqIpType & ComIpType_Inspectable)
{
SafeComHolder<IInspectable> pvObj;
hr = pUnk->QueryInterface(IID_IInspectable, &pvObj);
hr = SafeQueryInterface(pUnk, IID_IInspectable, &pvObj);
pUnk->Release();
}
else
{
pvObj = pUnk;
}

if (FAILED(hr))
Expand All @@ -194,7 +206,7 @@ IUnknown *GetComIPFromObjectRef(OBJECTREF *poref, ComIpType ReqIpType, ComIpType
if (pFetchedIpType != NULL)
*pFetchedIpType = ReqIpType;

RETURN pUnk;
RETURN pvObj;
}

MethodTable *pMT = (*poref)->GetMethodTable();
Expand Down Expand Up @@ -464,12 +476,13 @@ IUnknown *GetComIPFromObjectRef(OBJECTREF *poref, REFIID iid, bool throwIfNoComI

if (TryGetComIPFromObjectRefUsingComWrappers(*poref, &pUnk))
{
SafeComHolder<IUnknown> pvObj;
hr = pUnk->QueryInterface(iid, &pvObj);
IUnknown* pvObj;
hr = SafeQueryInterface(pUnk, iid, &pvObj);
pUnk->Release();
if (FAILED(hr))
COMPlusThrowHR(hr);

RETURN pUnk;
RETURN pvObj;
}

MethodTable *pMT = (*poref)->GetMethodTable();
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/tests/src/Interop/COM/ComWrappers/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace ComWrappersTests.Common
// Managed object with native wrapper definition.
//
[Guid("447BB9ED-DA48-4ABC-8963-5BB5C3E0AA09")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ITest
{
void SetValue(int i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ extern public static int UpdateTestObjectAsIInspectable(

[DllImport(nameof(MockReferenceTrackerRuntime))]
extern public static int UpdateTestObjectAsInterface(
[MarshalAs(UnmanagedType.Interface)] Test testObj,
[MarshalAs(UnmanagedType.Interface)] ITest testObj,
int i,
[Out, MarshalAs(UnmanagedType.Interface)] out Test ret);
[Out, MarshalAs(UnmanagedType.Interface)] out ITest ret);
}

private const string ManagedServerTypeName = "ConsumeNETServerTesting";
Expand Down Expand Up @@ -298,6 +298,11 @@ private static void ValidateMarshalAPIs(bool validateUseRegistered)
IntPtr dispatchWrapper = Marshal.GetIDispatchForObject(dispatchObj);
Assert.AreNotEqual(IntPtr.Zero, dispatchWrapper);
Assert.AreEqual(dispatchObj, registeredWrapper.LastComputeVtablesObject, "Registered ComWrappers instance should have been called");

Console.WriteLine($" -- Validate Marshal.GetIDispatchForObject != Marshal.GetIUnknownForObject...");
IntPtr unknownWrapper = Marshal.GetIUnknownForObject(dispatchObj);
Assert.AreNotEqual(IntPtr.Zero, unknownWrapper);
Assert.AreNotEqual(unknownWrapper, dispatchWrapper);
}

Console.WriteLine($" -- Validate Marshal.GetObjectForIUnknown...");
Expand Down Expand Up @@ -326,33 +331,33 @@ private static void ValidatePInvokes(bool validateUseRegistered)
GlobalComWrappers.Instance.ReturnInvalid = !validateUseRegistered;

Console.WriteLine($" -- Validate MarshalAs IUnknown...");
ValidateInterfaceMarshaler<object>(MarshalInterface.UpdateTestObjectAsIUnknown, validateUseRegistered);
ValidateInterfaceMarshaler<object>(MarshalInterface.UpdateTestObjectAsIUnknown, shouldSucceed: validateUseRegistered);
object obj = MarshalInterface.CreateTrackerObjectAsIUnknown();
Assert.AreEqual(validateUseRegistered, obj is FakeWrapper, $"Should{(validateUseRegistered ? string.Empty : "not")} have returned {nameof(FakeWrapper)} instance");

if (validateUseRegistered)
{
Console.WriteLine($" -- Validate MarshalAs IDispatch...");
ValidateInterfaceMarshaler<object>(MarshalInterface.UpdateTestObjectAsIDispatch, validateUseRegistered, new TestEx(IID_IDISPATCH));
ValidateInterfaceMarshaler<object>(MarshalInterface.UpdateTestObjectAsIDispatch, shouldSucceed: true, new TestEx(IID_IDISPATCH));

Console.WriteLine($" -- Validate MarshalAs IInspectable...");
ValidateInterfaceMarshaler<object>(MarshalInterface.UpdateTestObjectAsIInspectable, validateUseRegistered, new TestEx(IID_IINSPECTABLE));
ValidateInterfaceMarshaler<object>(MarshalInterface.UpdateTestObjectAsIInspectable, shouldSucceed: true, new TestEx(IID_IINSPECTABLE));
}

Console.WriteLine($" -- Validate MarshalAs Interface...");
ValidateInterfaceMarshaler<Test>(MarshalInterface.UpdateTestObjectAsInterface, validateUseRegistered);
ValidateInterfaceMarshaler<ITest>(MarshalInterface.UpdateTestObjectAsInterface, shouldSucceed: true);

if (validateUseRegistered)
{
Assert.Throws<InvalidCastException>(() => MarshalInterface.CreateTrackerObjectWrongType());

FakeWrapper wrapper = MarshalInterface.CreateTrackerObjectAsInterface();
Assert.IsNotNull(obj, $"Should have returned {nameof(FakeWrapper)} instance");
Assert.IsNotNull(wrapper, $"Should have returned {nameof(FakeWrapper)} instance");
}
}

private delegate int UpdateTestObject<T>(T testObj, int i, out T ret) where T : class;
private static void ValidateInterfaceMarshaler<T>(UpdateTestObject<T> func, bool validateUseRegistered, Test testObj = null) where T : class
private static void ValidateInterfaceMarshaler<T>(UpdateTestObject<T> func, bool shouldSucceed, Test testObj = null) where T : class
{
const int E_NOINTERFACE = unchecked((int)0x80004002);
int value = 10;
Expand All @@ -363,7 +368,7 @@ private static void ValidateInterfaceMarshaler<T>(UpdateTestObject<T> func, bool
T retObj;
int hr = func(testObj as T, value, out retObj);
Assert.AreEqual(testObj, GlobalComWrappers.Instance.LastComputeVtablesObject, "Registered ComWrappers instance should have been called");
if (validateUseRegistered)
if (shouldSucceed)
{
Assert.IsTrue(retObj is Test);
Assert.AreEqual(value, testObj.GetValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,5 +374,10 @@ extern "C" DLL_EXPORT int STDMETHODCALLTYPE UpdateTestObjectAsInterface(ITest *o
if (obj == nullptr)
return E_POINTER;

return UpdateTestObjectAsIUnknown(obj, i, (IUnknown**)out);
HRESULT hr;
RETURN_IF_FAILED(obj->SetValue(i));

obj->AddRef();
*out = obj;
return S_OK;
}