加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – MEMORY_BASIC_INFORMATION结构中的BaseAddress和Allo

发布时间:2020-12-14 04:11:01 所属栏目:Windows 来源:网络整理
导读:在MSDN中,我发现以下` BaseAddress – 指向页面区域的基址的指针. AllocationBase – 指向VirtualAlloc函数分配的一系列页面的基址的指针. BaseAddress成员指向的页面包含在此分配范围内. 但我真的不明白有什么区别.谁能告诉我区别? (不像在MSDN :)) Window
在MSDN中,我发现以下`

BaseAddress – 指向页面区域的基址的指针.

AllocationBase – 指向VirtualAlloc函数分配的一系列页面的基址的指针. BaseAddress成员指向的页面包含在此分配范围内.

但我真的不明白有什么区别.谁能告诉我区别? (不像在MSDN :))

Windows上的虚拟内存分配的粒度为64千字节,即SYSTEM_INFO.dwAllocationGranularity的值.但虚拟内存页面为4096字节,SYSTEM_INFO.dwPageSize的值.

使用VirtualAlloc分配虚拟内存时,您将始终获得一个BaseAddress等于AllocationBase的块.但是,如果您随后更改了此块中一个或多个页面的页面保护,那么您可以观察到此块被细分为不同的BaseAddress.使用示例程序最佳显示,在MSVC上运行:

#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <conio.h>

void showmem(void* mem) {
    MEMORY_BASIC_INFORMATION info = {};
    VirtualQuery(mem,&info,sizeof info);
    printf("Alloc = %p,base = %p,size = %d,protect = %dn",info.AllocationBase,info.BaseAddress,info.RegionSize,info.Protect);
}


int main() {
    BYTE* mem = (BYTE*)VirtualAlloc(0,65536,MEM_COMMIT,PAGE_READWRITE);
    printf("%s","Initial allocation:n");
    showmem(mem);

    DWORD oldprotect;
    BOOL ok = VirtualProtect(mem + 4096,4096,PAGE_NOACCESS,&oldprotect);
    printf("%s","nAfter protection changes:n");
    showmem(mem);
    showmem(mem + 4096);
    showmem(mem + 4096 + 4096);

    _getch();
    return 0;
}

该程序的示例输出:

Initial allocation:
Alloc = 00ED0000,base = 00ED0000,size = 65536,protect = 4

After protection changes:
Alloc = 00ED0000,size = 4096,protect = 4
Alloc = 00ED0000,base = 00ED1000,protect = 1
Alloc = 00ED0000,base = 00ED2000,size = 57344,protect = 4

并注意VirtualProtect()调用如何要求原始块在具有不同BaseAddress但具有相同AllocationBase的3个区域中分割.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读