4

私は Windows 7 、64ビットアプリケーションに取り組んでいます。

大きなページの割り当てが成功した後、VirtualProtect を使用して PAGE_GUARD 保護フラグを最初の大きなページに設定しようとしています。これは私が使用しているコードです:

unsigned long long memSize = 1024*1024*1024;
char* data = (char*)VirtualAlloc(NULL, memSize, MEM_RESERVE|MEM_COMMIT|MEM_LARGE_PAGES, PAGE_READWRITE);

//make the first large page in the allocated buffer be a guard page
DWORD oldProtect;
SIZE_T pageSize = GetLargePageMinimum();
LPVOID startPtr = data;
bool res = VirtualProtect(startPtr, pageSize, PAGE_READWRITE | PAGE_GUARD, &oldProtect);

GetLastError() はエラー 487 を返します - 「無効なアドレスにアクセスしようとしています。」

通常のページ (4KB) に PAGE_GUARD 保護を割り当てて設定している間、すべてが正常に機能しています。

大規模ページに対するガード保護はサポートされていますか? もしそうなら、私のコードの何が問題なのですか?

前もって感謝します。

4

1 に答える 1

6

Guard protection is not supported for large pages. It isn't explicitly documented, but it can be inferred. To quote from MSDN Large-Page Support:

The memory is always read/write and nonpageable (always resident in physical memory).

PAGE_GUARD works by setting PAGE_NOACCESS internally, and then resetting the page to the desired protection level after the page is read or written (as NOACCESS it will raise an exception that Windows handles internally). Since large pages must be read/write, the system is unable to implement the guard behavior as requested.

To be honest, I'm not sure if this is a limitation imposed by Windows itself or due a legacy limitation of our underlying x86 hardware. I think if it was a hardware limitation, that it may have been a limitation on earlier x86/32 architectures. I'm positive that it no longer applies to any modern x64 architectures. (newer Linux kernels, for example, can use large pages transparently without the programming having to make explicit requests)

于 2013-01-01T00:22:43.870 に答える