Repository navigation
Handle read-only code pages when writing debugger native patches - #137611
Merged
tommcdon merged 2 commits intoNov 16, 2026
Merged
Conversation
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: 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. |
Contributor
|
Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag |
rcj1
approved these changes
Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When the debugger patches native code (interop debugging unmanaged breakpoints, skip-breakpoint, and the out-of-process
SetThreadContextin-place single-step path), the target page is often not writable. W^X RX views arePAGE_EXECUTE_READ. Previously, a failedWriteProcessMemorywas ignored or only asserted.This change adds a
WriteRemotePatchByteshelper used byApplyRemotePatchandRemoveRemotePatch. 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
MEM_IMAGEpages fall back toPAGE_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.FlushInstructionCacheis called after a successful write. On checked builds, a failure to restore protection asserts.RemoveRemotePatchtreats a write failure as benign (code unloaded) only whenVirtualQueryExsucceeds and reports the page as not committed.EnableInPlaceSingleStepping/HandleInPlaceSingleStepno longer duplicate theVirtualProtectExlogic. If the patch cannot be reapplied,HandleInPlaceSingleSteplogs and asserts, then still clears stepping state and resumes the other threads instead of throwing with them suspended.Setup/FixupForSkipBreakpoint.Validation
clr.runtimeChecked x64, Checked arm64, and Release x64 (plus CoreLib) all build with 0 warnings and 0 errors.Steppingclass plusAttach.BreakTest: 9/9 CoreCLR pass withoutDOTNET_OutOfProcessSetContext, and 9/9 pass withDOTNET_OutOfProcessSetContext=1.PAGE_EXECUTE_READ(0x20) page, the protected retry succeeded (hr=0x0) for both patch removal and reapply, and allSteppingtests 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.