티스토리 뷰

https://github.com/LunarG/VulkanTools/blob/master/vktrace/vktrace_layer/vktrace_lib_pageguardmappedmemory.cpp

// OPT: Optimization by using page-guard for speed up capture
// The speed is extremely slow when use vktrace to capture DOOM4. It took over half a day and 900G of trace for a capture from
// beginning to the game menu.

로 시작하는 부분을 보면 대략 알 수 있다.

https://github.com/LunarG/VulkanTools/blob/master/vktrace/vktrace_layer/vktrace_lib_pageguard.cpp#L329

exception handler는 PageGuardExceptionHandler

mprotect 와 VirtualProtect로 memory 변경을 검출하여, 그 부분만 업데이한다.
각 변경사항은 page 단위로 관리 (검출, 저장) 한다

그냥 주소가 제대로 나오는지 테스트
원본소스에서는 page 단위로 mproect를 걸었지만, 여기선 그냥 전 영역 설정

// A short program to demonstrate dynamic memory allocation
// using a structured exception handler.

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>             // For exit

#define PAGELIMIT 80            // Number of pages to ask for

LPTSTR lpNxtPage;               // Address of the next page to ask for
DWORD dwPages = 0;              // Count of pages gotten so far
DWORD dwPageSize;               // Page size on this computer

LONG WINAPI PageGuardExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) {
    LPVOID* addr = (LPVOID*)ExceptionInfo->ExceptionRecord->ExceptionInformation[1];
    return EXCEPTION_CONTINUE_EXECUTION;
}

int main(VOID)
{
    LPVOID lpvBase;               // Base address of the test memory
    LPTSTR lpPtr;                 // Generic character pointer
    BOOL bSuccess;                // Flag
    DWORD i;                      // Generic counter
    SYSTEM_INFO sSysInfo;         // Useful information about the system

    GetSystemInfo(&sSysInfo);     // Initialize the structure.

    AddVectoredExceptionHandler(1, PageGuardExceptionHandler);

    _tprintf(TEXT("This computer has page size %d.\n"), sSysInfo.dwPageSize);

    dwPageSize = sSysInfo.dwPageSize;

    // Reserve pages in the virtual address space of the process.

    lpvBase = VirtualAlloc(
        NULL,                 // System selects address
        PAGELIMIT * dwPageSize, // Size of allocation
        MEM_RESERVE | MEM_COMMIT,          // Allocate reserved pages
        PAGE_READWRITE);       // Protection = no access

    DWORD oldProt = 0;
    VirtualProtect(lpvBase, PAGELIMIT * dwPageSize, PAGE_READWRITE | PAGE_GUARD , &oldProt);

    lpPtr = lpNxtPage = (LPTSTR)lpvBase;

    // Use structured exception handling when accessing the pages.
    // If a page fault occurs, the exception filter is executed to
    // commit another page from the reserved block of pages.

    for (i = 0; i < PAGELIMIT * dwPageSize; i++)
    {
        // Write to memory.
        *((char*)lpPtr + 0x64) = 'a';
    }

    // Release the block of pages when you are finished using them.

    bSuccess = VirtualFree(
        lpvBase,       // Base address of block
        0,             // Bytes of committed pages
        MEM_RELEASE);  // Decommit the pages

    _tprintf(TEXT("Release %s.\n"), bSuccess ? TEXT("succeeded") : TEXT("failed"));

}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크