Guard Page Exception - how to raise

1

A customer that uses our API gets a guard page exception. He uses VirtualAlloc and VirtualProtect.

When I run his example everything works fine.

I tried this example from Microsoft but VisualStudio does not throw an 0x80000001 exception even if I already turned it on in the exception menu under 'Debug'. But the example clearly states that:

The first attempt to lock the memory block fails, raising a STATUS_GUARD_PAGE_VIOLATION exception.

What do I need to do to get that exception?

Edit:

The customer does something like this:

SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
DWORD dwPageSize = systemInfo.dwPageSize;
size_t size = width * height * sizeof(MyStruct); 
while(size % dwPageSize) 
{
    height--;
    size = width * height * sizeof(MyStruct); 
}

size_t dataSize = size + dwPageSize;

MyStruct * my_struct = (MyStruct*)VirtualAlloc(NULL, dataSize, MEM_COMMIT | MEM_RESERVE , PAGE_READWRITE);
if (!my_struct) return;

LPVOID beginGuard = (char*)my_struct + size;
DWORD oldProtection;
BOOL b = VirtualProtect(beginGuard, dwPageSize, PAGE_READWRITE | PAGE_GUARD, &oldProtection);
if(!b) MessageBox(NULL, "Can't set guard page", "", 0);

doSomething(); // some API function

Somewhere in 'doSomething()' the mentioned exception gets thrown. But I can't help that customer because I don't get that exception.

c++
visual-studio-2010
exception
asked on Stack Overflow Dec 1, 2015 by thats_me • edited Dec 1, 2015 by thats_me

1 Answer

0

To raise an exception with code 0x80000001, you need to try to access memory that is allocated and protected using PAGE_GUARD flag. Write something like

MyStruct foo = my_struct[0];

and the exception will be raised.

As far as the sample from MSDN goes, you can see in the comments of that post that its explanation is incorrect because VirtualLock does not raise an exception.

answered on Stack Overflow Dec 1, 2015 by Marko Popovic

User contributions licensed under CC BY-SA 3.0