Repository navigation
Check failed commits when creating Windows memory-mapped views - #136792
RutulPatel8 wants to merge 4 commits into
Conversation
|
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. |
|
Tagging subscribers to this area: @dotnet/area-system-io |
There was a problem hiding this comment.
🟡 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) |
There was a problem hiding this comment.
Is there anything that prevents this test from working on 32-bit?
There was a problem hiding this comment.
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>())) |
There was a problem hiding this comment.
| 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.
There was a problem hiding this comment.
| 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)); |
There was a problem hiding this comment.
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.
|
Updated in a64d4e2:
The production fix still captures the Win32 error immediately after the failed |

Fixes #136599
MemoryMappedView.CreateViewignored the return value ofVirtualAlloc(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.