问题描述
我使用Debian的挤压,并已经注意到,内存始终是零。这是新的linux发行版?前一段时间,我相信我可以利用看跌期权()和垃圾将被输出。
I am using Debian squeeze and have noticed that memory is always zeroed. Is this new in linux distributions ? Some time ago, I believe I could use puts() and garbage would be output.
我运行这个测试程序很多次,但在评价结果总是相同的。 (我有randomize_va_space = 2 sysctl.conf的,所以我知道,内存在不同的地点是在每次运行使用。)
I run this test program many times but the commented results are always the same. (I have randomize_va_space=2 in sysctl.conf so I know that memory in different locations is being used at each run.)
char *a = malloc(50000000);
a[49999999] = '\0';
puts(a); // it outputs nothing since all are zeroes
printf("%p\n", a);
if(a[5000] == '\0') // this condition is always true
{
puts("It is a nul char.");
}
是否有可能使系统不为零的内存?什么选择会这样的Debian安装挤压已经激活了始终为零内存?
Is it possible to make the system not zero memory ? What options could this Debian squeeze installation have activated that always zero memory ?
推荐答案
在任何现代的操作系统,只有这样,新获得的内存将包含非零值是,如果你的程序pviously释放的内存$ P $得到了由<$ C重用$ C>的malloc 。当从操作系统(内核)中得到新的存储器,它是最初的纯粹虚拟的。它没有物理存在;相反,它被映射为一个单一的共享内存页的全0字节写入时复制映射。当您第一次尝试写,内核将陷阱写入,分配的物理内存的新页面,复制原始页面的内容(在这种情况下都是0字节)到新页面,然后恢复你的程序。如果内核知道新分配的物理内存已经是零填充,它甚至能够优化了副本的一步。
On any modern operating system, the only way newly obtained memory will contain nonzero values is if memory previously freed by your program got reused by malloc
. When new memory is obtained from the operating system (kernel), it is initially purely virtual. It has no physical existence; instead it is mapped as copy-on-write mappings of a single shared memory page that's full of 0 bytes. The first time you attempt to write to it, the kernel will trap the write, allocate a new page of physical memory, copy the contents of the original page (which in this case are all 0 bytes) to the new page, and then resume your program. If the kernel knows the newly allocated physical memory is already zero-filled, it might even be able to optimize out the copy step.
本程序是必要的和有效的。这是必要的,因为移交内存可能包含来自内核或其他用户的进程的进程专用数据将是一个关键的安全漏洞。这是有效的,因为没有清空,在分配时进行; 零填充的页面只是引用到共享零页。
This procedure is both necessary and efficient. It's necessary because handing over memory that might contain private data from the kernel or another user's processes to your process would be a critical security breach. It's efficient because no zeroing is performed at allocation time; the "zero-filled" pages are just reference to a shared zero page.
这篇关于内核内存零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!