Skip to content

Handle read-only code pages when writing debugger native patches - #137611

Merged
tommcdon merged 2 commits into
dotnet:mainfrom
tommcdon:users/tommcdon/dbi-remote-patch-protection
Nov 16, 2026
Merged

tommcdon merged 2 commits into
dotnet:mainfrom
tommcdon:users/tommcdon/dbi-remote-patch-protection

Conversation

@tommcdon

Copy link
Copy Markdown
Member

Summary

When the debugger patches native code (interop debugging unmanaged breakpoints, skip-breakpoint, and the out-of-process SetThreadContext in-place single-step path), the target page is often not writable. W^X RX views are PAGE_EXECUTE_READ. Previously, a failed WriteProcessMemory was ignored or only asserted.

This change adds a WriteRemotePatchBytes helper used by ApplyRemotePatch and RemoveRemotePatch. If the direct write fails, the helper temporarily makes the bytes writable, retries the write, and restores the original protection. Failures are now propagated to callers.

Details

  • Only MEM_IMAGE pages fall back to PAGE_EXECUTE_WRITECOPY. Copy-on-write on a mapped section would detach the RX view from the RW alias of the W^X double mapping, so later JIT writes would no longer be visible.
  • FlushInstructionCache is called after a successful write. On checked builds, a failure to restore protection asserts.
  • RemoveRemotePatch treats a write failure as benign (code unloaded) only when VirtualQueryEx succeeds and reports the page as not committed.
  • EnableInPlaceSingleStepping / HandleInPlaceSingleStep no longer duplicate the VirtualProtectEx logic. If the patch cannot be reapplied, HandleInPlaceSingleStep logs and asserts, then still clears stepping state and resumes the other threads instead of throwing with them suspended.
  • Checked-build assertions are kept in Setup/FixupForSkipBreakpoint.

Validation

  • Builds: clr.runtime Checked x64, Checked arm64, and Release x64 (plus CoreLib) all build with 0 warnings and 0 errors.
  • dotnet-diagnostictests (MDbg), Windows x64, legacy DAC, branch Release runtime and DBI:
    • Stepping class plus Attach.BreakTest: 9/9 CoreCLR pass without DOTNET_OutOfProcessSetContext, and 9/9 pass with DOTNET_OutOfProcessSetContext=1.
  • With a Checked DBI and RS logging, the in-place single-step path was hit: the direct write failed on a PAGE_EXECUTE_READ (0x20) page, the protected retry succeeded (hr=0x0) for both patch removal and reapply, and all Stepping tests passed.

Not covered: the interop-debugging-only callers (Set/ClearUnmanagedBreakpoint, Setup/FixupForSkipBreakpoint) are compile-validated only. Not run: x86 builds, a cDAC pass, and ARM64 runtime tests (ARM64 is build-only).

Note

This PR description and part of the code changes were generated with the assistance of GitHub Copilot.

tommcdon and others added 2 commits November 13, 2026 22:22
ApplyRemotePatch wrote the breakpoint opcode with WriteVirtual and always
returned S_OK. When the target address is in a non-writable executable page
(for example a mapped PAGE_EXECUTE_READ view), the write fails with
ERROR_NOACCESS. Checked/Debug DBIs assert; Release DBIs silently report a
breakpoint that was never installed.

Add WriteRemotePatchBytes, which retries a failed write after temporarily
making the bytes writable (PAGE_EXECUTE_READWRITE, falling back to
PAGE_EXECUTE_WRITECOPY, matching EnableInPlaceSingleStepping) and restores
the original protection. ApplyRemotePatch and RemoveRemotePatch now use it
and propagate failures, except that RemoveRemotePatch still treats
uncommitted (unloaded) memory as success.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: d89c36e0-8c75-47fe-8c8a-23e497d32b9f
- Only fall back to PAGE_EXECUTE_WRITECOPY for MEM_IMAGE pages; copy-on-write
  on a mapped section would detach the RX view from its W^X RW alias.
- Assert (checked) when restoring the original page protection fails.
- Flush the instruction cache after a successful patch write.
- RemoveRemotePatch only treats a write failure as benign when VirtualQueryEx
  succeeds and reports the page as not committed.
- Preserve checked-build assertions in Setup/FixupForSkipBreakpoint.
- Remove redundant VirtualProtectEx in EnableInPlaceSingleStepping and
  HandleInPlaceSingleStep; on reapply failure still clear stepping state and
  resume the other threads instead of throwing.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 07b5ec2d-502e-4f1d-b25f-4963bc95653f
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag
See info in area-owners.md if you want to be subscribed.

@tommcdon
tommcdon merged commit 9ec6ef1 into dotnet:main Nov 16, 2026
101 of 103 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants