-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Fixing COM Aggregation and XAML reference tracking on .NET AOT #90283
Fixing COM Aggregation and XAML reference tracking on .NET AOT #90283
Conversation
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas Issue DetailsThere were blocks put in place in AOT for COM aggregation and XAML reference tracking wasn't implemented. This PR adds support for both of them by porting the CoreCLR native code that existed to C# for AOT support. With these changes, the ComWrappers API tests all pass. The weak reference tests currently do fail due to that logic isn't ported yet which I am looking in now but starting the PR with the core changes in the meantime.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I'll defer to @AaronRobinsonMSFT for final sign-off.
src/libraries/Common/src/Interop/Windows/Ole32/Interop.CoGetContextToken.cs
Outdated
Show resolved
Hide resolved
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Show resolved
Hide resolved
src/libraries/Common/src/Interop/Windows/Ole32/Interop.CoGetContextToken.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
if (_pHandles == null) | ||
{ | ||
_capacity = DefaultCapacity; | ||
_pHandles = (IntPtr*)Interop.Ucrtbase.calloc((nuint)_capacity, (nuint)sizeof(IntPtr)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we do, the logic as ported checks if there was a previously allocated dependency handle at the slot by checking if it is not default / zero and if so overrides the target and dependent for it rather than allocating a new one.
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Outdated
Show resolved
Hide resolved
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Outdated
Show resolved
Hide resolved
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Outdated
Show resolved
Hide resolved
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
Thanks @manodasanW. So far so good. /cc @AustinWise |
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Aaron Robinson <arobins@microsoft.com> Co-authored-by: Jan Kotas <jkotas@microsoft.com> Co-authored-by: Jeremy Koritzinsky <jkoritzinsky@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just leaving a few thoughts here. This is all looking amazing!! 🎉
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Outdated
Show resolved
Hide resolved
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Outdated
Show resolved
Hide resolved
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
src/coreclr/nativeaot/System.Private.CoreLib/src/System/ComAwareWeakReference.NativeAot.cs
Outdated
Show resolved
Hide resolved
@jkotas it looks like the build is failing on osx-x64 Release NativeAOT due to the binary size increased. What options do we have here?
|
It means that a lot of the ComWrappers code ends up in the final binary even when none of it is actually used. The problem is most likely caused by the calls from WeakReference/ComAwareWeakReference. We either need to make all these calls from WeakReference to go through indirections that are initialized only after the Com wrappers tracking is initialized (ie replace the If that does not work for some reason, we may need end up introducing a feature switch to enable this feature in WinUI apps only. https://learn.microsoft.com/en-us/dotnet/core/runtime-config/threading#autoreleasepool-for-managed-threads is a good example of prior art. |
Pretty sure that's what you meant, but just to double check: did you just use WinUI as a reference here but the feature switch would just be a general-purpose feature switch like others in .NET, that anyone could use? Because WinUI 3 apps are not the only applications that would use the new stuff added in this PR. For instance, COM aggregation can be needed by any WASDK app (without WinUI 3) that also happens to have some types deriving from WinRT projection types. |
Correct. We should investigate first whether this feature switch is actually needed. |
src/coreclr/nativeaot/System.Private.CoreLib/src/System/ComAwareWeakReference.NativeAot.cs
Show resolved
Hide resolved
src/coreclr/nativeaot/System.Private.CoreLib/src/System/ComAwareWeakReference.NativeAot.cs
Outdated
Show resolved
Hide resolved
src/coreclr/nativeaot/System.Private.CoreLib/src/System/ComAwareWeakReference.NativeAot.cs
Outdated
Show resolved
Hide resolved
src/coreclr/nativeaot/System.Private.CoreLib/src/System/ComAwareWeakReference.NativeAot.cs
Outdated
Show resolved
Hide resolved
src/coreclr/nativeaot/System.Private.CoreLib/src/System/ComAwareWeakReference.NativeAot.cs
Outdated
Show resolved
Hide resolved
Looks like all the tests passed after the callback change. Thanks. |
src/coreclr/nativeaot/System.Private.CoreLib/src/System/ComAwareWeakReference.NativeAot.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some remaining nits
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Outdated
Show resolved
Hide resolved
...nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.NativeAot.cs
Outdated
Show resolved
Hide resolved
.../System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
Show resolved
Hide resolved
@@ -4181,6 +4181,9 @@ | |||
<data name="InvalidOperation_ComInteropRequireComWrapperInstance" xml:space="preserve"> | |||
<value>COM Interop requires ComWrapper instance registered for marshalling.</value> | |||
</data> | |||
<data name="InvalidOperation_ComInteropRequireComWrapperTrackerInstance" xml:space="preserve"> | |||
<value>COM Interop requires ComWrapper instance registered for reference tracker support.</value> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see this message in CoreCLR anywhere. Is the CoreCLR error handling different? Do we want to make it the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CoreCLR doesn't have a message, but following the code path, it does throw an ArgumentException in one case and in the other it seems it would hit a NullPointerException from a null access. Given in CoreCLR they are mixing between C# and the C++ layer for where this logic is and the error happens in a more generic function like CallReleaseObjects, I guess they didn't have a specific error message there. But here we are able to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AaronRobinsonMSFT Should we have a test for these paths?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. This code path in CoreCLR is a bit more contrived given the set up, but we can construct a test. Due to how the system is built up it is going to be some effort though.
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
There were blocks put in place in AOT for COM aggregation and XAML reference tracking wasn't implemented. This PR adds support for both of them by porting the CoreCLR native code that existed to C# for AOT support. With these changes, the ComWrappers API tests and weak reference tests all pass.
Fixes #89713
Fixes #85137