-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Fix COM interface method calling wrong method for base/derived classes #128913
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
Open
elinor-fung
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
elinor-fung:com-virt-wrong-method
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/tests/Interop/COM/VirtualMethodOverride/VirtualMethodOverrideTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using Xunit; | ||
|
|
||
| [ComVisible(true)] | ||
| [Guid("A1111111-0000-0000-0000-000000000001")] | ||
| public interface IFoo | ||
| { | ||
| void DoWork(); | ||
| } | ||
|
|
||
| [ComVisible(true)] | ||
| [Guid("A1111111-0000-0000-0000-000000000002")] | ||
| [ComDefaultInterface(typeof(IFoo))] | ||
| public class Foo : IFoo | ||
| { | ||
| public virtual void DoWork() => VirtualMethodOverrideTest.LastCalledType = nameof(Foo); | ||
| } | ||
|
|
||
| [ComVisible(true)] | ||
| [Guid("A1111111-0000-0000-0000-000000000003")] | ||
| [ComDefaultInterface(typeof(IFoo))] | ||
| public class FooDerived : Foo | ||
| { | ||
| public override void DoWork() => VirtualMethodOverrideTest.LastCalledType = nameof(FooDerived); | ||
| } | ||
|
|
||
| [ComVisible(true)] | ||
| [Guid("B2222222-0000-0000-0000-000000000001")] | ||
| public interface IBar | ||
| { | ||
| void DoWork(); | ||
| } | ||
|
|
||
| [ComVisible(true)] | ||
| [Guid("B2222222-0000-0000-0000-000000000002")] | ||
| [ComDefaultInterface(typeof(IBar))] | ||
| public class Bar : IBar | ||
| { | ||
| public virtual void DoWork() => VirtualMethodOverrideTest.LastCalledType = nameof(Bar); | ||
| } | ||
|
|
||
| [ComVisible(true)] | ||
| [Guid("B2222222-0000-0000-0000-000000000003")] | ||
| [ComDefaultInterface(typeof(IBar))] | ||
| public class BarDerived : Bar | ||
| { | ||
| public override void DoWork() => VirtualMethodOverrideTest.LastCalledType = nameof(BarDerived); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that COM-to-CLR dispatch correctly resolves virtual method overrides | ||
| /// regardless of whether the base or derived class is accessed via COM first. | ||
| /// </summary> | ||
| public class VirtualMethodOverrideTest | ||
| { | ||
| internal static string? LastCalledType; | ||
|
|
||
| [UnmanagedFunctionPointer(CallingConvention.StdCall)] | ||
| delegate int DoWorkDelegate(IntPtr pThis); | ||
|
|
||
| private static int CallDoWork(IntPtr pInterface, int slot) | ||
| { | ||
| IntPtr vtbl = Marshal.ReadIntPtr(pInterface); | ||
| IntPtr fnPtr = Marshal.ReadIntPtr(vtbl, slot * IntPtr.Size); | ||
| Assert.NotEqual(IntPtr.Zero, fnPtr); | ||
|
|
||
| var fn = Marshal.GetDelegateForFunctionPointer<DoWorkDelegate>(fnPtr); | ||
| return fn(pInterface); | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void DerivedFirst() | ||
| { | ||
| int doWorkSlot = Marshal.GetStartComSlot(typeof(IFoo)); | ||
| IntPtr pDerived = IntPtr.Zero; | ||
| IntPtr pBase = IntPtr.Zero; | ||
| try | ||
| { | ||
| pDerived = Marshal.GetComInterfaceForObject(new FooDerived(), typeof(IFoo)); | ||
| pBase = Marshal.GetComInterfaceForObject(new Foo(), typeof(IFoo)); | ||
|
|
||
| LastCalledType = null; | ||
| Assert.True(CallDoWork(pDerived, doWorkSlot) >= 0); | ||
| Assert.Equal(nameof(FooDerived), LastCalledType); | ||
|
|
||
| LastCalledType = null; | ||
| Assert.True(CallDoWork(pBase, doWorkSlot) >= 0); | ||
| Assert.Equal(nameof(Foo), LastCalledType); | ||
| } | ||
| finally | ||
| { | ||
| if (pDerived != IntPtr.Zero) | ||
| Marshal.Release(pDerived); | ||
|
|
||
| if (pBase != IntPtr.Zero) | ||
| Marshal.Release(pBase); | ||
| } | ||
|
elinor-fung marked this conversation as resolved.
|
||
| } | ||
|
|
||
| [Fact] | ||
| public static void BaseFirst() | ||
| { | ||
| int doWorkSlot = Marshal.GetStartComSlot(typeof(IBar)); | ||
| IntPtr pBase = IntPtr.Zero; | ||
| IntPtr pDerived = IntPtr.Zero; | ||
| try | ||
| { | ||
| pBase = Marshal.GetComInterfaceForObject(new Bar(), typeof(IBar)); | ||
| pDerived = Marshal.GetComInterfaceForObject(new BarDerived(), typeof(IBar)); | ||
|
|
||
| LastCalledType = null; | ||
| Assert.True(CallDoWork(pBase, doWorkSlot) >= 0); | ||
| Assert.Equal(nameof(Bar), LastCalledType); | ||
|
|
||
| LastCalledType = null; | ||
| Assert.True(CallDoWork(pDerived, doWorkSlot) >= 0); | ||
| Assert.Equal(nameof(BarDerived), LastCalledType); | ||
| } | ||
| finally | ||
| { | ||
| if (pBase != IntPtr.Zero) Marshal.Release(pBase); | ||
| if (pDerived != IntPtr.Zero) Marshal.Release(pDerived); | ||
| } | ||
| } | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
src/tests/Interop/COM/VirtualMethodOverride/VirtualMethodOverrideTest.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
| <NativeAotIncompatible>true</NativeAotIncompatible> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="VirtualMethodOverrideTest.cs" /> | ||
| </ItemGroup> | ||
| </Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.