(转)Magical Values in VC(2008-08-19 19:04:21)
WFC Technical Note 006 - Funny Memory Values$Revision: 1 $ Last Modified $Date: 3/26/00 11:01a $
IntroductionMicrosoft has put quite a lot of memory leak detection helpers in Windows NT. They have not done a good job of advertising it. This document describes some of the things I've deciphered while debugging code.
Funny Memory ValuesMany times while debugging programs, I will come across memory that is filled with "funny" values. After some playing around (i.e. hacking) with the Win32 API, I was able to figure out what they meant. Some of these values have been documented in places but never all together. The values for the tags presented here are hexadecimal because that's the way Developer's Studio presents them in the memory window.
Samuel R. Blackburn
Magic debug values are specific values written to memory during allocation or deallocation, so that it will later be possible to tell whether or not they have become corrupted, and to make it obvious when values taken from uninitialized memory are being used.
Since it is unlikely that any given 32-bit integer would take this specific value, the appearance of such a number in a debugger or memory dump can indicate common errors such as buffer overflows, or uninitialized variables.
Memory is usually viewed in hexadecimal, so common values used are often repeated digits or hexspeak.
Famous and common examples include:
: Used in Quake 3 for an optimized inverse square root function. 
: Used by a number of RTOSes
: Used in embedded development because the alternating bit pattern (10100101) creates an easily recognized pattern on oscilloscopes and logic analyzers.
: Used by Microsoft's HeapAlloc() to mark "no man's land" guard bytes after allocated heap memory
: Used by Apple as the "Boot Zero Block" magic number
: A startup to this value to initialize all free memory to catch errant pointers
: Used by Microsoft's LocalAlloc(LMEM_FIXED) to mark uninitialised allocated heap memory
: Burroughs large systems "uninitialized" memory (48-bit words)
: Error Code returned to the Microsoft eVC debugger when connection is severed to the debuggee
: Used on IBM RS/6000 64-bit systems to indicate uninitialized CPU registers
: On Sun Microsystems' Solaris, marks uninitialised kernel memory (KMEM_UNINITIALIZED_PATTERN)
: Used by Microsoft .NET as a magic number in resource files
: A memory leak tracking tool which it will change the MMU tables so that all references to address zero
: Used by both Mach-O ("Fat binary" in both 68k and PowerPC) to identify object files and the Java programming language to identify .class files
: Used by Sun Microsystems' Solaris debugging kernel to mark kmemfree() memory
: Seen in Intel Mach-O binaries on Apple Computer's Mac OS X platform (see
: Used by Microsoft's C++ debugging runtime library to mark uninitialised stack memory
: Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory
: Used by MicroQuill's SmartHeap and Microsoft's C++ debugging heap to mark freed heap memory
: Used at the start of Silicon Graphics' IRIX arena files
: Famously used on IBM systems such as the RS/6000, also used in the original Mac OS operating systems, OPENSTEP Enterprise, and the Commodore Amiga. On Sun Microsystems' Solaris, marks freed kernel memory (KMEM_FREE_PATTERN)
: A Microsoft Windows STOP Error code used when the user manually initiates the crash.
: All the newly allocated memory which is not explicitly cleared when it is munged
: From MicroQuill's SmartHeap
: Comes at the end to identify every OSA script
: Used by Microsoft's C++ debugging heap to mark "no man's land" guard bytes before and after allocated heap memory
: Seen in PowerPC Mach-O binaries on Apple Computer's Mac OS X platform. On Sun Microsystems' Solaris, marks the red zone (KMEM_REDZONE_PATTERN)
: Used by Microsoft's HeapFree() to mark freed heap memory
: Used by Linux reboot() syscall
The prevalence of these values in Microsoft technology is no coincidence; they are discussed in detail in Steve Maguire's book Writing Solid Code from Microsoft Press. He gives a variety of criteria for these values, such as:
- They should not be useful; that is, most algorithms that operate on them should be expected to do something unusual. Numbers like zero don't fit this criterion.
- They should be easily recognized by the programmer as invalid values in the debugger.
- On machines that don't have byte alignment, they should be odd numbers, so that dereferencing them as addresses causes an exception.
- They should cause an exception, or perhaps even a debugger break, if executed as code.
Since they were often used to mark areas of memory that were essentially empty, some of these terms came to be used in phrases meaning "gone, aborted, flushed from memory"; e.g. "Your program is DEADBEEF".
ZUG programming language initialized memory to either
development environment and to
0x0000 in the live
environment, on the basis that uninitialised variables should be
encouraged to misbehave under development to trap them, but
encouraged to behave in a live environment to reduce