#1
|
|||
|
|||
VirtualAlloc fails on specific memory address
I'm trying to manage a (theoretical) simple task: allocate memory at an address specified by me. However, I don't get it to work for some reason.
I simply use VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect); If lpAddress is set to NULL, Windows will allocate the memory at an address it selects by itself. This works without any problems. However, if I specify any memory location, the function will fail with ERROR_INVALID_ADDRESS from GetLastError. I traced a bit in the Windows kernel, but I found only one error check: If lpAddress is below 0x00010000, GetLastError will return ERROR_INVALID_PARAMETER. The function even fails if I'm trying to force allocation of the memory region which Windows will give me if I set lpAddress to NULL. Example: Code:
VirtualAlloc(0x00830000, 0x4000, MEM_COMMIT, PAGE_READWRITE) -> function fails with ERROR_INVALID_ADDRESS VirtualAlloc(NULL, 0x4000, MEM_COMMIT, PAGE_READWRITE) -> function allocates 0x4000 bytes at address 0x00830000 |
#2
|
|||
|
|||
You only have access to memory in your process address space so 400000 to 600000 or so usually. You are trying to address memory outside of this space which will yield the exception.
|
#3
|
|||
|
|||
@MarkusO:
Quote from MSDN: Quote:
I could allocate any memory region I tried. The mistake here was that MarkusO tried to commit a page which was not reserved. I never heard about that "process address space" rule, like you call it, before. |
#4
|
|||
|
|||
MEM_RESERVE The function reserves a range of the process's virtual address space without allocating any actual physical storage in memory or in the paging file on disk. Other memory allocation functions, such as malloc and LocalAlloc, cannot use a reserved range of memory until it is released. You can commit reserved memory pages in subsequent calls to the VirtualAllocEx function.
seems we have to do 2 virtualalloc's one with reserve one with commit at least i understand it that way |
#5
|
|||
|
|||
Correct way to do what you need:
VirtualAlloc(0x00830000, 0x4000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) The second example works fine because: "If the value of the lpAddress parameter is NULL, specifying MEM_COMMIT without MEM_RESERVE causes the function to BOTH reserve and commit the memory". (MSDN) |
#6
|
|||
|
|||
So I really missed something, even if it is nothing "special". Next time I will read the API documentation better.
Thx @all |
#7
|
|||
|
|||
Read it:
typedef struct _SYSTEM_INFO { // sinf union { DWORD dwOemId; struct { WORD wProcessorArchitecture; WORD wReserved; }; }; DWORD dwPageSize; LPVOID lpMinimumApplicationAddress; !!! LPVOID lpMaximumApplicationAddress; !!! DWORD dwActiveProcessorMask; DWORD dwNumberOfProcessors; DWORD dwProcessorType; DWORD dwAllocationGranularity; WORD wProcessorLevel; WORD wProcessorRevision; } SYSTEM_INFO; |
#8
|
|||
|
|||
@SLV:
I guess you are posting in answer to my question about the post from "Snatch" above. ("process address space so 400000 to 600000 or so usually") But when I run "GetSystemInfo" I get: lpMinimumApplicationAddress = 00010000h lpMinimumApplicationAddress = 7FFFFFFFh But this is something I really don't need this API for since it will be the same for all home computers and workstations and for 99.99% of the servers. It is also no answer to the initial question posted by "MarkusO". |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Unpack OneWay.dll problem,Import REConstructor v1.6 Final fails. | winndy | General Discussion | 3 | 01-07-2006 10:22 |
MapConv fails to apply IDA .MAP labels or comments | 5Alive | General Discussion | 5 | 05-25-2005 19:55 |
Allocating memory at a specific location | redbull | General Discussion | 5 | 04-18-2005 19:37 |