John Kalane
John Kalane

Reputation: 1185

Why does VirtualAlloc() require flProtect = PAGE_NOACCESS when flAllocationType = MEM_RESERVE?

The following call to VirtualAlloc() returns an error ERROR_INVALID_PARAMETER on x64:

LPVOID lp = VirtualAlloc(NULL, 0x10000000000, MEM_RESERVE, 0);

That is, I have to use PAGE_NOACCESS as VirtualAlloc's last argument. But when I look at the definition of PAGE_NOACCESS here, I find:

Disables all access to the committed region of pages. An attempt to read from, write to, or execute the committed region results in an access violation.

which has nothing to do with reserving a range of addresses in the process virtual address space.

It seems like the requirement for flProtect to be equal to PAGE_NOACCESS, when flAllocationTime = MEM_RESERVE, is simply missing in MS docs. Can someone confirm this?

Upvotes: 2

Views: 1669

Answers (1)

MicroVirus
MicroVirus

Reputation: 5477

VirtualAlloc requires for its last parameter a memory protection constant. None of the memory protection constants are 0, so 0 is not a valid value. This is the ERROR_INVALID_PARAMETER error you are seeing. Therefore, you have to choose at least one of the constants given. One choice is to use PAGE_NOACCESS.

I admit, this is not very clear from the documentation, but it is a general fact that you can not assume that 0 is always a valid value for any flag parameter; you always need to check what values you are allowed to give. If 0 is an accepted value then it is listed as such or explicitly mentioned.

Upvotes: 4

Related Questions