Skip to content

Check failed commits when creating Windows memory-mapped views - #136792

Open
RutulPatel8 wants to merge 4 commits into
dotnet:mainfrom
RutulPatel8:fix/mmf-windows-commit-failure-136599
Open

RutulPatel8 wants to merge 4 commits into
dotnet:mainfrom
RutulPatel8:fix/mmf-windows-commit-failure-136599

Conversation

@RutulPatel8

@RutulPatel8 RutulPatel8 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #136599

MemoryMappedView.CreateView ignored the return value of VirtualAlloc(MEM_COMMIT) and checked the already-valid mapping handle. If committing a delayed view failed, callers received an accessor over reserved memory and an in-range write could crash the process.

Check the committed address, dispose the view on failure, and throw the Windows error at view creation. The regression test runs in a child process with a job memory limit set relative to its current private memory usage. A 128 MiB view exceeds the remaining commit allowance on both 32-bit and 64-bit Windows; the test disposes an accessor if it unexpectedly succeeds.

Validation: the Windows test project and library build with zero warnings or errors. Running the test against the installed unpatched runtime reproduces the bug: accessor creation incorrectly succeeds. I could not run it against the PR-built runtime locally because this machine lacks the Visual Studio C++ tools needed to build the repository testhost; CI will run the PR version.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 1, 2026
@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: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

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.

🟡 Changes recommended

The test assumes a 1 TiB view must fail on 64-bit Windows, causing false failures on hosts with sufficient available commit and leaving any unexpectedly successful accessor undisposed until finalization.

1 open issue
What changed in this PR

This PR addresses #136599 by detecting failed memory commits when creating Windows memory-mapped views, rather than returning an accessor over uncommitted memory.

Changes:

  • Check the address returned by VirtualAlloc, enabling cleanup and error reporting when committing a view fails.
  • Add a Windows-only outer-loop regression test for delayed-allocation commit failure.
File Description
src/​libraries/​System.IO.MemoryMappedFiles/​tests/​MemoryMappedFile.CreateNew.Tests.cs Adds a large-view commit-failure regression test.
src/​libraries/​System.IO.MemoryMappedFiles/​src/​System/​IO/​MemoryMappedFiles/​MemoryMappedView.Windows.cs Checks the commit result instead of the existing mapping handle.

Review effort: Balanced

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void FailedViewCommit_DoesNotCreateAccessor()
{
if (IntPtr.Size != 8)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there anything that prevents this test from working on 32-bit?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nothing fundamental prevents 32-bit coverage. I removed the architecture guard in fa12a31. The child process now sets its job memory limit to its current private usage plus 32 MiB and requests a 128 MiB view, so it can exercise the failed commit on either architecture. The Windows test project builds; CI will validate 32-bit execution.

JobObjectExtendedLimitInformation limits = default;
limits.BasicLimitInformation.LimitFlags = 0x100; // JOB_OBJECT_LIMIT_PROCESS_MEMORY
limits.ProcessMemoryLimit = checked((nuint)(process.PrivateMemorySize64 + commitHeadroom));
if (!SetInformationJobObject(job, 9, ref limits, (uint)Marshal.SizeOf<JobObjectExtendedLimitInformation>()))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (!SetInformationJobObject(job, 9, ref limits, (uint)Marshal.SizeOf<JobObjectExtendedLimitInformation>()))
if (!SetInformationJobObject(job, 9, ref limits, sizeof(JobObjectExtendedLimitInformation)))

Marshal.SizeOf is a legacy API. We prefer sizeof and blittable types for interop.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Interop.Kernel32.VirtualQuery(viewHandle, ref viewInfo, sizeof(MEMORY_BASIC_INFORMATION));

Could you please fix this and the other instance of Marshal.SizeOf in this file to use sizeof while we are on it?

Also, could you please delete the duplicate src\libraries\Common\src\Interop\Windows\Kernel32\Interop.MEMORY_BASIC_INFO.cs - it is identical to src\libraries\Common\src\Interop\Windows\Kernel32\Interop.MEMORY_BASIC_INFORMATION.cs


JobObjectExtendedLimitInformation limits = default;
limits.BasicLimitInformation.LimitFlags = 0x100; // JOB_OBJECT_LIMIT_PROCESS_MEMORY
limits.ProcessMemoryLimit = checked((nuint)(process.PrivateMemorySize64 + commitHeadroom));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the hardcoded limit that you had here before was better. This makes the test behavior dependent on what other tests in the test process happen to be doing at the moment.

@RutulPatel8

Copy link
Copy Markdown
Contributor Author

Updated in a64d4e2:

  • The Windows x86 and x64 CI failures were caused by the job-limit test: CreateViewAccessor succeeded, so its expected exception was unreliable. I reproduced this locally with both process and job memory limits; a 128 MiB mapped view still succeeded under a 100 MiB job limit. I removed the test rather than keep a platform-dependent failure.
  • Changed both VirtualQuery size arguments to sizeof(MEMORY_BASIC_INFORMATION) and removed the duplicate interop definition, updating its two project references.

The production fix still captures the Win32 error immediately after the failed VirtualAlloc, disposes the view handle, and throws that error. Please review the updated patch.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MemoryMappedFile (Windows): failed MEM_COMMIT is ignored, accessor over reserved memory crashes the process

3 participants