如何判断虚拟内存页是否已被锁定?

How to tell if a virtual memory page has been locked?

比如说,如果在某个时候我的进程中的某个范围的虚拟内存被这样锁定:

//Memory was reserved & committed as such
void* pMem = ::VirtualAlloc(NULL, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

//...

//And then
::VirtualLock(pMem, 4096);

所以在我的进程的虚拟内存中有一个页面的任意地址,我可以判断它是否是 locked

通过使用 win32 api 这是不可能的。但如果使用 ZwQueryVirtualMemory with undocumented MEMORY_INFORMATION_CLASS values - this is possible. for data structures definition - see ntmmapi.h

我们需要使用 MemoryWorkingSetExInformation with MEMORY_WORKING_SET_EX_BLOCK 并使用 ULONG_PTR Locked : 1; 成员

演示测试:

NTSTATUS IsPageLocked(PVOID BaseAddress, BOOLEAN& Locked)
{
    MEMORY_WORKING_SET_EX_INFORMATION mwsei = { BaseAddress };

    NTSTATUS status = ZwQueryVirtualMemory(NtCurrentProcess(), 0, MemoryWorkingSetExInformation, &mwsei, sizeof(mwsei), 0);

    if (0 <= status)
    {
        if (mwsei.VirtualAttributes.Valid)
        {
            Locked = mwsei.VirtualAttributes.Locked;
        }
        else
        {
            status = STATUS_INVALID_ADDRESS;
        }
    }

    return status;
}

NTSTATUS IsPageLockedEx(PVOID BaseAddress, BOOLEAN& Locked)
{
    NTSTATUS status = IsPageLocked(BaseAddress, Locked);
    if (0 > status)
    {
        DbgPrint("IsPageLocked - error %x\n", status);
    }
    else
    {
        DbgPrint("IsPageLocked = %x\n", Locked);
    }
    return status;
}

void CheckVA()
{
    if (PVOID pv = VirtualAlloc(NULL, PAGE_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE))
    {
        BOOLEAN Locked;
        NTSTATUS status = IsPageLockedEx(pv, Locked);

        if (status == STATUS_INVALID_ADDRESS)
        {
            // at this point  physical memory for pv not yet committed. reference memory for commit physical page
            if (*(PBYTE)pv) __nop();

            status = IsPageLockedEx(pv, Locked);
        }

        if (VirtualLock(pv, PAGE_SIZE))
        {
            IsPageLockedEx(pv, Locked);

            if (VirtualUnlock(pv, PAGE_SIZE))
            {
                IsPageLockedEx(pv, Locked);
            }
            else
            {
                __debugbreak();
            }
        }

        VirtualFree(pv, 0, MEM_RELEASE);
    }
}

DbgPrint输出

IsPageLocked - error c0000141
IsPageLocked = 0
IsPageLocked = 1
IsPageLocked = 0

对于将来查看此内容的任何人,XP 和更高版本现在包括执行此操作的 API:QueryWorkingSet and QueryWorkingSetEx

寻找PSAPI_WORKING_SET_EX_BLOCK结构中的Locked位,类似于依赖ZwQueryVirtualMemory的答案。