我知道操作系统有时会使用某些模式(如0xCD和0xDD)初始化内存.我想知道的是何时以及为什么会发生这种情况.
这是否特定于编译器使用?
对于这个,malloc/new和free/delete的工作方式是否相同?
它是特定于平台的吗?
它会出现在其他操作系统上,例如Linux或VxWorks吗?
我的理解是这只发生在Win32调试配置中,它用于检测内存溢出并帮助编译器捕获异常.
你能举一个关于这个初始化如何有用的实际例子吗?
我记得读过一些东西(可能在Code Complete 2中),在分配内存时将内存初始化为已知模式是好的,某些模式会触发Win32中的中断,这将导致调试器中出现异常.
这有多便携?
快速总结一下,当编译为调试模式时,Microsoft编译器用于各种无主/未初始化内存的位置(支持可能因编译器版本而异):
Value Name Description
------ -------- -------------------------
0xCD Clean Memory Allocated memory via malloc or new but never
written by the application.
0xDD Dead Memory Memory that has been released with delete or free.
Used to detect writing through dangling pointers.
0xED or Aligned Fence 'No man's land' for aligned allocations. Using a
0xBD different value here than 0xFD allows the runtime
to detect not only writing outside the allocation,
but to also detect mixing alignment-specific
allocation/deallocation routines with the regular
ones.
0xFD Fence Memory Also known as "no mans land." This is used to wrap
the allocated memory (surrounding it with a fence)
and is used to detect indexing arrays out of
bounds or other accesses (especially writes) past
the end (or start) of an allocated block.
0xFD or Buffer slack Used to fill slack space in some memory buffers
0xFE (unused parts of `std::string` or the user buffer
passed to `fread()`). 0xFD is used in VS 2005 (maybe
some prior versions, too), 0xFE is used in VS 2008
and later.
0xCC When the code is compiled with the /GZ option,
uninitialized variables are automatically assigned
to this value (at byte level).
// the following magic values are done by the OS, not the C runtime:
0xAB (Allocated Block?) Memory allocated by LocalAlloc().
0xBAADF00D Bad Food Memory allocated by LocalAlloc() with LMEM_FIXED,but
not yet written to.
0xFEEEFEEE OS fill heap memory, which was marked for usage,
but wasn't allocated by HeapAlloc() or LocalAlloc().
Or that memory just has been freed by HeapFree().
免责声明:表格来自我所说的一些笔记 - 它们可能不是100%正确(或连贯).
其中许多值在vc/crt/src/dbgheap.c中定义:
/* * The following values are non-zero, constant, odd, large, and atypical * Non-zero values help find bugs assuming zero filled data. * Constant values are good so that memory filling is deterministic * (to help make bugs reproducable). Of course it is bad if * the constant filling of weird values masks a bug. * Mathematically odd numbers are good for finding bugs assuming a cleared * lower bit. * Large numbers (byte values at least) are less typical, and are good * at finding bad addresses. * Atypical values (i.e. not too often) are good since they typically * cause early detection in code. * For the case of no-man's land and free blocks, if you store to any * of these locations, the memory integrity checker will detect it. * * _bAlignLandFill has been changed from 0xBD to 0xED, to ensure that * 4 bytes of that (0xEDEDEDED) would give an inaccessible address under 3gb. */ static unsigned char _bNoMansLandFill = 0xFD; /* fill no-man's land with this */ static unsigned char _bAlignLandFill = 0xED; /* fill no-man's land for aligned routines */ static unsigned char _bDeadLandFill = 0xDD; /* fill free objects with this */ static unsigned char _bCleanLandFill = 0xCD; /* fill new objects with this */
还有一些调试运行时将填充具有已知值的缓冲区(或部分缓冲区),例如std::string
分配中的"松弛"空间或传递给的缓冲区fread()
.这些情况使用给定名称_SECURECRT_FILL_BUFFER_PATTERN
(定义crtdefs.h
)的值.我不确定它何时被引入,但它至少在VS 2005(VC++ 8)的调试运行时中.
最初用于填充这些缓冲区的值是0xFD
- 与没有人的土地相同的值.但是,在VS 2008(VC++ 9)中,值已更改为0xFE
.我假设这是因为可能存在填充操作将超过缓冲区末尾的情况,例如,如果调用者传入的缓冲区大小太大而无法调用fread()
.在这种情况下,该值0xFD
可能不会触发检测到此溢出,因为如果缓冲区大小仅为1,则填充值将与用于初始化该金丝雀的无人值的土地值相同.没有人的土地没有变化意味着不会注意到超限.
因此,VS 2008中的填充值发生了变化,因此这种情况会改变无人的陆地金丝雀,从而导致运行时检测到问题.
正如其他人所指出的那样,这些值的一个关键属性是指针变量,其中一个值被解除引用,它将导致访问冲突,因为在标准的32位Windows配置中,用户模式地址不会高于0x7fffffff.
关于填充值0xCCCCCCCC的一个不错的属性是在x86汇编中,操作码0xCC是int3操作码,它是软件断点中断.因此,如果您尝试在未填充该填充值的未初始化内存中执行代码,您将立即触发断点,操作系统将允许您附加调试器(或终止该进程).
它是特定于编译器和操作系统的,Visual Studio将不同类型的内存设置为不同的值,以便在调试器中您可以轻松查看是否已经进入malloced内存,固定数组或未初始化对象.当我谷歌搜索他们时,有人会发布细节...
http://msdn.microsoft.com/en-us/library/974tc9t1.aspx