Skip to content

Harden lock-free MMF dump reader#1470

Open
leculver wants to merge 1 commit into
microsoft:mainfrom
leculver:split/a-mmf-reader-hardening
Open

Harden lock-free MMF dump reader#1470
leculver wants to merge 1 commit into
microsoft:mainfrom
leculver:split/a-mmf-reader-hardening

Conversation

@leculver
Copy link
Copy Markdown
Contributor

@leculver leculver commented Jun 3, 2026

Summary

Hardens the existing opt-in lock-free memory-mapped dump reader for concurrent read/direct-span use.

This PR keeps the existing reader architecture, but closes correctness and lifetime gaps found during review:

  • validates dump-controlled segment file ranges with overflow-safe subtraction checks
  • filters invalid file ranges before publishing the segment table
  • handles overlapping virtual-address segments without losing valid memory
  • fails closed after disposal before touching mapped memory or delegated reader state
  • makes Dispose idempotent for concurrent callers with an atomic disposed transition
  • releases acquired MMF resources and disposes the inner reader if construction fails after pointer acquisition
  • updates the public/test contract for concurrent reads/direct spans and dispose quiescence

Part of #420.

Validation

  • Built as part of the split branch verification: dotnet build src/Microsoft.Diagnostics.Runtime/Microsoft.Diagnostics.Runtime.csproj --framework net10.0 -- passed with 0 warnings and 0 errors.
  • This split branch was recombined with the other reduced branches and compared against lockfree-mmf-threadsafe; all non-stress paths matched the original combined branch.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 hardens ClrMD’s opt-in lock-free, memory-mapped dump reader to support concurrent memory reads and direct-span access more safely, while tightening lifetime/cleanup behavior and validating dump-controlled segment ranges.

Changes:

  • Updates the lock-free MMF reader to support concurrent read/direct-span access via a striped per-thread segment cache, plus safer segment range validation and overlap handling.
  • Strengthens disposal and construction-failure cleanup semantics (idempotent dispose, fail-closed after dispose, release MMF pointer/resources on failure).
  • Updates public/test contract and adds stress/regression tests for concurrent reads/direct spans, invalid segments, overlap behavior, and dispose semantics.
Show a summary per file
File Description
src/Microsoft.Diagnostics.Runtime/DataTargetOptions.cs Updates option documentation to reflect concurrent read/direct-span safety and dispose quiescence requirements.
src/Microsoft.Diagnostics.Runtime/DataReaders/LockFreeMmfDataReader.cs Implements concurrency hardening, safer segment table construction/validation, overlap handling, and disposal/cleanup improvements.
src/Microsoft.Diagnostics.Runtime.Tests/src/MemoryReaderTests.cs Adds concurrency and regression tests for lock-free MMF reader behavior and cleanup contracts.
src/Microsoft.Diagnostics.Runtime.Tests/src/Attributes/DataReaderKind.cs Updates enum documentation to match the revised thread-safety/dispose contract.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 1

Comment on lines +209 to +210
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ThrowIfDisposed() => ObjectDisposedException.ThrowIf(_disposed != 0, this);
Make the existing opt-in lock-free memory-mapped dump reader safe for concurrent read/direct-span use. This adds overflow-safe segment bounds validation, filters invalid file ranges, handles overlapping virtual-address segments without losing valid memory, fails closed after disposal, makes Dispose idempotent for concurrent callers, and cleans up acquired MMF resources on constructor failure. Update the public/test contract and add focused MemoryReaderTests coverage for malformed segments, overlapping segments, use-after-dispose, constructor cleanup, and concurrent Dispose.
@leculver leculver force-pushed the split/a-mmf-reader-hardening branch from 64b97f1 to 3c2565e Compare June 3, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants