win 32プラットフォームのダウングレードに関する説明(Win 32 Debug CRT Heap)(下)


この前はどこまで話しましたかhttp://www.cnblogs.com/coolhysteria/archive/2012/09/22/Win32DebugCRTHeap1.html)?はい、36バイトです.
まず何も言いません.まず二つのリンクを提供します.権威のあるマイクロソフトからのMSDNです.http://msdn.microsoft.com/zh-cn/library/bebs9zyz.aspx)ここにある大神Andrew Birkettのブログが来ました.http://www.nobugs.org/developer/win32/debug_crt_heap.htmlを選択します
段のデバッグスタックに関する情報構造体の説明は以下の通りです.
 
Currenently、the block header structure used to store the debug heap's bookeeping information is declead as follows in the DBGINT.H header file:
typedef struct _CrtMemBlockHeader
{
// Pointer to the block allocated just before this one:
    struct _CrtMemBlockHeader *pBlockHeaderNext;
// Pointer to the block allocated just after this one:
    struct _CrtMemBlockHeader *pBlockHeaderPrev;
    char *szFileName;    // File name
    int nLine;           // Line number
    size_t nDataSize;    // Size of user block
    int nBlockUse;       // Type of block
    long lRequest;       // Allocation number
// Buffer just before (lower than) the user's memory:
    unsigned char gap[nNoMansLandSize];
} _CrtMemBlockHeader;

/* In an actual memory block in the debug heap,
 * this structure is followed by:
 *   unsigned char data[nDataSize];
 *   unsigned char anotherGap[nNoMansLandSize];
 */
The NoMansLand buffers on either side of the user data ara of the block are currently 4 byte s in size,and are filled with a known byte value used by the debug heap routines to verift the limits of the memory The debug heap also fills new memory blocks with a known value. If you elect to keep freed blocks in the heap's linked list as explined below、these freed blocks are also filled with a known value. Currenently、the actual byte values used aras follows:
NoMansLand(0 xFD)
The「NoMansLand」buffers on eigther side of the memory used by an appication are currently filled with 0 xFD.
Freed blocks(0 xDD)
The freed blocks kept unused in the debug heap's linked list when the _CRTDBG_DELAY_FREEMEMDF flags is set are currently filled with 0 xDD.
New object(0 xCD)
New object s are filled with 0 xCD when they are allocated.
 
有关win32平台下调试堆的描述(Win32 Debug CRT Heap) (下)_第1张图片
 前の話では必要な10 bytesが全部0 xCDに初期化されています.つまり、このマークを通して、私たちのデータを簡単に見つけられます.
 
 
もう一枚の図を示します.デバッグの情報をイメージして説明してもいいです.
有关win32平台下调试堆的描述(Win32 Debug CRT Heap) (下)_第2张图片
この図はシームレスに対応できます.CrtMem BlockHeader構造体私たちは直接図を見て話します.
1.0 xFDFDは私の眼球をいっぱい詰めました.私達の10 bytesデータは1セットの0 xFDFDFDに囲まれています.上ではNo mans landと記載されています.これは伝説の「禁足島」ですか?Dは名前の通り、「無人区」はもちろんこの二つの区域に入ることができないという意味です.私達は逆に押します.この二つのエリアを修正しなければならないなら、どうすればいいですか?間違いなく越境します.つまり、このNoMansLandは一旦修正されたら、プログラムのどこかがアウトになります.このように簡単にいくつかのメモリがオーバーフローしていますか? :P
コードを書いてみます.
 1 #include 
 2 #include 
 3 #include <string.h>
 4 int main(int argc, char **argv)
 5 {
 6     char *p = (char *)malloc(sizeof(char) * 10);
 7     strncpy(p, "hello,boy!!",12);
 8 
 9     
10     free(p);
11     p = NULL;
12 
13     return 0;
14 }
デバッグ:
有关win32平台下调试堆的描述(Win32 Debug CRT Heap) (下)_第3张图片
明らかにオーバーフローはオーバーフローとアンダーフローに分けられます.このコードはオーバーフローです.つまり、どのような状況であれ、発生したらプログラムが異常になり、エラーが発生します.(
 
2.私を見て、あなたの大きさが分かります.10 bytesを割り当てました.つまり、0 xA bytesです.コミッションの中にはちょうど0 xAと同じフィールドがあります.間違いなく、あなたが見た4バイトは瞬時にどれぐらいのメモリを割り当てられましたか?便利ですか?:
 
3:前のテーブルは誰ですか?後ろのテーブルは誰ですか?
先にコードを与えます.
 1 #include 
 2 #include 
 3 #include <string.h>
 4 int main(int argc, char **argv)
 5 {
 6     char *p = (char *)malloc(sizeof(char) * 10);
 7     char *q = ((char *)malloc(sizeof(char) * 11));
 8     strncpy(p, "hello,boy",10);
 9     strncpy(q, "hello,girl", 11);
10 
11     
12     free(p);
13     p = NULL;
14 
15     return 0;
16 }
そして調整します.
有关win32平台下调试堆的描述(Win32 Debug CRT Heap) (下)_第4张图片
微妙な法則が発見されましたか?だからここでは言いません.D
 
以上の3つのフィールドはデバッグ中によく使われていますが、残りのいくつかのフィールド(構造体メンバーで説明します)は確かに使われていません.しかし、コメントによると、デバッグによって、具体的なファイル、行番号などが分かります.
    char *szFileName;    // File name
    int nLine;           // Line number
    size_t nDataSize;    // Size of user block
    int nBlockUse;       // Type of block
    long lRequest;       // Allocation number
デバッグで積み上げた内容についてはここまでにして、皆さんに助けを与えたいと思います.知識に対する自分の記憶と理解も深めます.
理論+実践というのが王道です.デバッグの実際的な使い方については参考してください.
http://www.cnblogs.com/coolhysteria/archive/2012/10/27/Win32DebugCRTHeapGoAndDo.html