Windows内存映射默认情况下文件内容始终为零? [英] Are Windows Memory Mapped File contents always zeroed by default?

查看:167
本文介绍了Windows内存映射默认情况下文件内容始终为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据经验确定,在我的系统上,创建为一定大小的内存映射文件默认情况下总是完全归零。例如,使用调用

I've determined empirically that, on my system, a memory mapped file created to be a certain size is always completely zeroed by default. For example, using the call

HANDLE hMM = 
    CreateFileMapping (h,
                        NULL,
                        PAGE_READWRITE,
                        0,
                        0x01400000,//20MB
                        NULL);

..并写入该文件的映射视图总是导致一个20MB的文件, ,除非我写了非零数据。

.. and writing into a mapped view of that file always results in a 20MB file that is completely zeroed, except where I have written non-zero data.

我想知道文件的未初始化部分是否可以假定为零。

I'm wondering if uninitialized parts of the file can be assumed to be zeros. Is this behavior guaranteed on Windows in general?

推荐答案

CreateFileMapping 文档(注释


如果文件已扩展,则文件的旧结尾和文件的新结尾之间的文件内容不能保证为零;该行为由文件系统定义。

If the file is extended, the contents of the file between the old end of the file and the new end of the file are not guaranteed to be zero; the behavior is defined by the file system.

因此,如果磁盘上的文件开始为空,则不是保证被归零(因为你正在扩展它);我不认为文件系统驱动程序会冒这样泄露潜在敏感信息的风险,但谁知道,也许一些文件系统驱动程序回收已经用于您的进程的页面(这不应该是一个安全风险)。

so, if your file on disk starts empty, it's not guaranteed to be zeroed (since you are expanding it); I don't think that file system drivers would take the risk of leaking potentially sensitive information that way, but who knows, maybe some file system driver recycles pages already used for your process (and this shouldn't be a security risk).

另一方面,我不知道不提供安全性的文件系统(例如FAT)是否会关注给你集群的内容

On the other hand, I don't know if filesystems that do not offer security at all (e.g. FAT) would be so concerned to give you the content of the clusters that they happened to allocate for the new part of the file.

如果你正在创建一个不是由磁盘上的文件支持的内存段,而是由页面文件保证您获得的内存全部清零:

If, instead, you are creating a memory section not backed by a file on disk but by the paging file it's guaranteed that the memory you get is all zeroed:


由操作系统页面文件支持的文件映射对象的页面的初始内容是0(零)。

The initial contents of the pages in a file mapping object backed by the operating system paging file are 0 (zero).

这可能是因为当创建一个只有内存的分页文件时,内存管理器有完全的控制权发生了什么,它从空白页面池中取出页面。

This is guaranteed probably because when creating a memory-only paging file the memory manager has the complete control on what's going on, and it takes the pages from the blanked pages pool.

这篇关于Windows内存映射默认情况下文件内容始终为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆