cDAC: Remove ad-hoc StrategyBasedComWrappers instantiation#127113
cDAC: Remove ad-hoc StrategyBasedComWrappers instantiation#127113max-charlamb merged 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Removes per-call StrategyBasedComWrappers allocations in cDAC COM entrypoints and fixes COM identity comparison for ClrDataAppDomain.IsSameObject by unwrapping CCWs to their underlying managed objects.
Changes:
- Reuse a single static
StrategyBasedComWrappersinstance in cDAC entrypoints instead of allocating one per call. - Fix
ClrDataAppDomain.IsSameObjectto useComWrappers.TryGetObject(and add a null check) so identity comparisons work for CCWs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/native/managed/cdac/mscordaccore_universal/Entrypoints.cs | Replaces per-entrypoint StrategyBasedComWrappers instantiation with a shared static instance for COM wrapping/unwrapping. |
| src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/ClrDataAppDomain.cs | Fixes IsSameObject to correctly unwrap CCWs (and handle null input) so it no longer always returns S_FALSE. |
5e8d283 to
0826108
Compare
- Fix ClrDataAppDomain.IsSameObject to use ComWrappers.TryGetObject instead of creating a new StrategyBasedComWrappers per call. The previous code used GetOrCreateObjectForComInstance with CreateObjectFlags.None, which always creates a new RCW wrapper and never unwraps CCWs - so the identity check always failed (returned S_FALSE). TryGetObject correctly unwraps any CCW to its original managed object. - Add null check for the appDomain parameter in IsSameObject. - Replace all StrategyBasedComWrappers usage in Entrypoints.cs with ComInterfaceMarshaller<T>, which uses the built-in static DefaultMarshallingInstance singleton. This ensures cache coherence with the generated COM interop code and eliminates the need to instantiate StrategyBasedComWrappers directly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
0826108 to
3dab415
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (1)
src/native/managed/cdac/mscordaccore_universal/Entrypoints.cs:120
ComInterfaceMarshaller<IDacDbiInterface>.ConvertToManagedwill throw (e.g., InvalidCastException) iflegacyImplPtris not actually anIDacDbiInterfacepointer. Since this method is[UnmanagedCallersOnly]and has no try/catch, that exception would tear down the process, and the subsequentis not Legacy.IDacDbiInterfacecheck will never be reached. Prefer validating viaMarshal.QueryInterfacefor theIDacDbiInterfaceIID (returnE_NOINTERFACE/COR_E_INVALIDCASTon failure) and only then callingConvertToManaged, or wrap the conversion in a try/catch and translate failures to an HRESULT.
if (legacyImplPtr != IntPtr.Zero)
{
legacyObj = ComInterfaceMarshaller<IDacDbiInterface>.ConvertToManaged((void*)legacyImplPtr);
if (legacyObj is not Legacy.IDacDbiInterface)
{
*obj = IntPtr.Zero;
return HResults.COR_E_INVALIDCAST; // E_NOINTERFACE
}
|
Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag |
🤖 Copilot Code Review — PR #127113Note This review was AI/Copilot-generated (claude-opus-4.6 primary, with gpt-5.3-codex and goldeneye sub-agent reviews synthesized). Holistic AssessmentMotivation: The PR fixes a real bug in Approach: Using Summary: Detailed Findings✅
|
Note
This PR was AI/Copilot-generated.
Summary
Removes all direct
StrategyBasedComWrappersinstantiation from the cDAC, fixing a bug inClrDataAppDomain.IsSameObjectand improving the entrypoint code to use the standardComInterfaceMarshaller<T>API.Changes
Bug fix:
ClrDataAppDomain.IsSameObject(always returnedS_FALSE)The previous code created a
new StrategyBasedComWrappers()and calledGetOrCreateObjectForComInstancewithCreateObjectFlags.None. This always creates a new RCW (aComObjectwrapper) — it never unwraps CCWs back to their original managed object. So theobj is ClrDataAppDomaincheck always failed, andIsSameObjectalways returnedS_FALSE.Fix: Use
ComWrappers.TryGetObject(nint, out object?)instead. This is a static method that correctly unwraps any CCW (from anyComWrappersinstance) back to its original managed object, making the identity comparison work as intended.Cleanup:
Entrypoints.cs— useComInterfaceMarshaller<T>The three entrypoint functions (
CreateSosInterface,CreateDacDbiInterface,CLRDataCreateInstanceImpl) each created anew StrategyBasedComWrappers()per call. These are replaced withComInterfaceMarshaller<T>calls, which internally useStrategyBasedComWrappers.DefaultMarshallingInstance— the same process-wide singleton used by all source-generated COM interop code. This ensures cache coherence and eliminates the need to instantiateStrategyBasedComWrappersdirectly.Testing
clr+libs) which was not available in this worktree; the changes are limited to COM interop plumbing and do not affect contract logic