Skip to content

Fix COM wrapper weak-handle defensive copy in ReferenceTrackerNativeObjectWrapper - #129668

Merged
jkotas merged 3 commits into
mainfrom
copilot/bugfix-potential-double-free-comwrappers
Jun 22, 2026
Merged

jkotas merged 3 commits into
mainfrom
copilot/bugfix-potential-double-free-comwrappers

Conversation

Copilot AI commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

This change addresses a COM wrapper lifetime bug where GCHandle.Free() could execute on a defensive copy of _nativeObjectWrapperWeakHandle, making cleanup non-idempotent under finalization/release interleavings.
The fix is surgical: remove readonly so Free() updates the actual field state.

  • What changed

    • In System.Runtime.InteropServices.ComWrappers.ReferenceTrackerNativeObjectWrapper, changed:
      • internal readonly GCHandle _nativeObjectWrapperWeakHandle;
      • to internal GCHandle _nativeObjectWrapperWeakHandle;
  • Why this matters

    • GCHandle is a mutable struct.
    • Calling mutating members on a readonly field can operate on a defensive copy.
    • For Release()/finalizer race handling, this can lead to stale IsAllocated state and duplicate free behavior.
  • Code example

    // Before
    internal readonly GCHandle _nativeObjectWrapperWeakHandle;
    
    // After
    internal GCHandle _nativeObjectWrapperWeakHandle;

Copilot AI requested review from Copilot and removed request for Copilot June 21, 2026 13:37
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jun 21, 2026
@jkotas jkotas added area-System.Runtime.InteropServices and removed area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI labels Jun 21, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Copilot AI requested review from Copilot and removed request for Copilot June 21, 2026 14:21
Copilot AI changed the title [WIP] Fix potential double free in ComWrappers implementation Fix COM wrapper weak-handle defensive copy in ReferenceTrackerNativeObjectWrapper Jun 21, 2026
Copilot AI requested a review from jkotas June 21, 2026 14:22
@jkotas
jkotas requested a review from AaronRobinsonMSFT June 21, 2026 14:37
@jkotas
jkotas marked this pull request as ready for review June 21, 2026 14:37
Copilot AI review requested due to automatic review settings June 21, 2026 14:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a correctness issue in CoreLib’s COM wrapper tracking path by ensuring _nativeObjectWrapperWeakHandle.Free() mutates the actual stored GCHandle field (avoiding readonly-field defensive copies) so cleanup becomes reliably idempotent.

Changes:

  • Changed ReferenceTrackerNativeObjectWrapper._nativeObjectWrapperWeakHandle from readonly to mutable to prevent GCHandle.Free() from operating on a defensive copy.
  • Ensures subsequent cleanup paths (e.g., re-release/finalizer flows) can observe the freed state.

Copilot AI requested a review from jkotas June 22, 2026 03:43
@jkotas
jkotas merged commit 2fb0567 into main Jun 22, 2026
145 checks passed
@jkotas
jkotas deleted the copilot/bugfix-potential-double-free-comwrappers branch June 22, 2026 21:59
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 23, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
…bjectWrapper (#129668)

This change addresses a COM wrapper lifetime bug where `GCHandle.Free()`
could execute on a defensive copy of `_nativeObjectWrapperWeakHandle`,
making cleanup non-idempotent under finalization/release interleavings.
The fix is surgical: remove `readonly` so `Free()` updates the actual
field state.

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: jkotas <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants