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

未关闭 适合新手
#136,599 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp

调研方向

阅读 src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Windows.cs,尤其是第 62–70 行,其中 VirtualAlloc 为延迟分配提交页面。检查附近的代码如何报告 native 分配失败并释放视图。完成标准是:提交失败时,视图创建会抛出异常,而不是返回一个可能在写入时崩溃的 accessor。

由索引模型根据 Issue 内容生成。

描述

area-System.IO bug untriaged

Description

MemoryMappedView.CreateView calls Interop.VirtualAlloc(..., MEM_COMMIT, ...) for DelayAllocatePages views but discards its return value. The following failure check tests viewHandle.IsInvalid (the already-valid map view handle) instead, so a failed commit is treated as success and the view is published over MEM_RESERVE pages.

Repro

Plain safe C#. The view is larger than the system commit limit.

using System.IO.MemoryMappedFiles;

using var mmf = MemoryMappedFile.CreateNew(null, 1L << 42, MemoryMappedFileAccess.ReadWrite,
    MemoryMappedFileOptions.DelayAllocatePages, HandleInheritability.None);
using var acc = mmf.CreateViewAccessor(0, 1L << 40);
Console.WriteLine("accessor created, capacity=" + acc.Capacity);
acc.Write(0, (byte)1);
Console.WriteLine("write succeeded");

Actual behavior

net11.0 RC2, win-x64 (64 GB RAM, commit limit ~103 GB). CreateViewAccessor succeeds, then an in-range write is a fatal AccessViolationException:

accessor created, capacity=1099511627776
Fatal error.

(1L << 36 = 64 GB views succeed on this machine, so it only reproduces above the commit limit.)

Expected behavior

CreateViewAccessor / CreateViewStream throws (e.g. IOException / Win32Exception for the commit failure). Safe accessor APIs never fault.

Notes

  • src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Windows.cs:62-70.
  • Fix: capture the VirtualAlloc result, and on failure dispose the view and throw with the last error.
主要语言
C#
星标
18.4k
派生
5.8k
平均合并
2 天 15 小时
30 天内合并 PR
620

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dotnet/runtime 的其他 Issue

查看 dotnet/runtime 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。