堆的调试
#include "stdafx.h"
#include <windows.h>
int main()
{
HLOCAL h1,h2,h3,h4,h5,h6;
HANDLE hp;
hp = HeapCreate(0,0x1000,0x10000);
__asm int 3
h1 = HeapAlloc(hp,HEAP_ZERO_MEMORY,3);
h2 = HeapAlloc(hp,HEAP_ZERO_MEMORY,5);
h3 = HeapAlloc(hp,HEAP_ZERO_MEMORY,6);
h4 = HeapAlloc(hp,HEAP_ZERO_MEMORY,8);
h5 = HeapAlloc(hp,HEAP_ZERO_MEMORY,19);
h6 = HeapAlloc(hp,HEAP_ZERO_MEMORY,24);
//free block and prevent coaleses
HeapFree(hp,0,h1); //free to freelist[2]
HeapFree(hp,0,h3); //free to freelist[2]
HeapFree(hp,0,h5); //free to freelist[4]
HeapFree(hp,0,h4); //coalese h3,h4,h5,link the large block to freelist[8]
return 0;
}
环境 | 备注 | |
---|---|---|
操作系统 | Windows2000 | 分配策略堆操作系统敏感 |
编译器 | vc++6.0 | |
编译选项 | 默认 | VS2003、VS2005的GS编译选项会使实验失败 |
build | release版本 | debug版本会失败 |
调试堆与调试栈不同,不能直接使用od、windbg等来加载程序,因为对管理函数会检测到调试状态而采用调试堆管理策略。
调试堆&常态堆不同:(类似于debug版本的PE和release版本的PE)
1.调试堆不适应快表,只使用空表分配。
2.所有的堆块被加上了多余的16字节尾部来防止溢出(防止程序溢出而不是堆溢出),包括8个字节的0xAB和8个字节的0x00。
3.块首的标志位不同。
所以在创建堆之后加一个人工断点:_asm int 3 。然后让程序单独执行,当程序堆初始化完后,断点会中断程序,这时再用od attach进程,就能看到真