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

[release/5.0] Fix ComWrappers interaction with the IReferenceTracker interface #45622

Merged
merged 2 commits into from
Dec 8, 2020

Conversation

AaronRobinsonMSFT
Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT commented Dec 5, 2020

Issues: microsoft/CsWinRT#413, microsoft/microsoft-ui-xaml#3719

Description

A fundamental flaw was discovered during consumption of the ComWrappers API (introduced .NET 5) in WinUI 3.0 scenarios. This was related to COM Aggregation and how C#/WinRT generated interop code.

The work here corrects this issue and provides additional testing.

Customer Impact

Scenarios involving WinRT support provided by C#/WinRT with aggregation of WinRT types will leak memory with no recourse. This impacts consumption of the WinUI stack.

Regression

This API is new in .NET 5.0. However the API is used to address the removal of WinRT built-in support - #37672.

Testing

Additional E2E testing has been added in this PR. The scenario was also manually validated on a local build against the official XAML/Jupiter runtime using hand crafted interop code. A private build of coreclr was then shared with the C#/WinRT team for validation.

Risk

Minimal risk. The code paths in question are not used outside of the ComWrappers API scenario - only known consumer is C#/WinRT and WinUI. The risk for these two consumers are as follows:

  1. This fix is incomplete.
  2. This fix results in a crash in a WinUI scenario.

Risk (1) means leaks may still exist but they are no worse off than the current code. There has been verified improvement, but it is possible additional leaks exist. Risk (2) is slightly more concerning but is mitigable if this PR results in early collection or use after free scenarios. The C#/WinRT code generation tool can introduce a quirk to explicitly leak the object instead of attempting to participate in collection.

Neither of these are expected but they represent reduced risk from the CoreCLR perspective.

/cc @Scottj1s @davidwrighton @jeffschwMSFT @MikeHillberg @elinor-fung @jkoritzinsky

Convert Managed Object Wrapper (MOW) GC Handle from HNDTYPE_STRONG
    to HNDTYPE_REFCOUNTED.

Add new CreateObjectFlags value to indicate aggregation during
    CreateObject scenario. This isn't reflected in the managed
    .NET 5 API surface area.

In the ReferenceTracker scenario the ref count may never reach 0 so
    the MOW destructor needs to handle that case. The previous expectation
    for a null in the destructor was based on the STRONG handle logic.

During aggregation scenarios involving ReferenceTracker, ownership
    of the inner is now the responsibility of the runtime.
Copy link
Member

@jeffschwMSFT jeffschwMSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Please get a cr and we can take for consideration for 5.0.2

@AaronRobinsonMSFT AaronRobinsonMSFT changed the title Fix ComWrappers interaction with the IReferenceTracker interface [NET 5] Fix ComWrappers interaction with the IReferenceTracker interface Dec 5, 2020
@AaronRobinsonMSFT
Copy link
Member Author

AaronRobinsonMSFT commented Dec 5, 2020

CoreCLR failures are fixed with #45525

@AaronRobinsonMSFT AaronRobinsonMSFT changed the title [NET 5] Fix ComWrappers interaction with the IReferenceTracker interface [release/5.0] Fix ComWrappers interaction with the IReferenceTracker interface Dec 5, 2020
Copy link
Member

@jkoritzinsky jkoritzinsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one nit that I'm fine leaving in the .NET 5 PR. This looks good. I highly appreciate the very heavy commenting. Makes this a lot easier to mentally parse.

src/coreclr/src/interop/comwrappers.cpp Show resolved Hide resolved
@ViktorHofer
Copy link
Member

// Auto-generated message

69e114c which was merged 12/7 removed the intermediate src/coreclr/src/ folder. This PR needs to be updated as it touches files in that directory which causes conflicts.

To update your commits you can use this bash script: https://gist.github.com/ViktorHofer/6d24f62abdcddb518b4966ead5ef3783. Feel free to use the comment section of the gist to improve the script for others.

@AaronRobinsonMSFT
Copy link
Member Author

@ViktorHofer This PR should be okay right? It is going into the release/5.0 branch. Just want to confirm.

@ViktorHofer
Copy link
Member

ViktorHofer commented Dec 8, 2020

Uff I didn't filter PRs out that target 5.0 :( Yes that one is fine.

@leecow leecow removed the Servicing-consider Issue for next servicing release review label Dec 8, 2020
@jeffschwMSFT jeffschwMSFT added the Servicing-approved Approved for servicing release label Dec 8, 2020
@jeffschwMSFT
Copy link
Member

approved in tactics

@jeffschwMSFT jeffschwMSFT modified the milestones: 5.0.x, 5.0.2 Dec 8, 2020
@AaronRobinsonMSFT AaronRobinsonMSFT merged commit 96d7aec into dotnet:release/5.0 Dec 8, 2020
@AaronRobinsonMSFT AaronRobinsonMSFT deleted the tracker_bug branch December 8, 2020 21:18
@ghost ghost locked as resolved and limited conversation to collaborators Jan 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants