Exetools

Exetools (https://forum.exetools.com/index.php)
-   General Discussion (https://forum.exetools.com/forumdisplay.php?f=2)
-   -   VirtualAlloc fails on specific memory address (https://forum.exetools.com/showthread.php?t=8963)

MarkusO 01-18-2006 17:21

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

Did I miss something special or why doesn't the allocation of a specific memory region work?

Snatch 01-18-2006 18:03

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.

Kerlingen 01-18-2006 19:06

@MarkusO:
Quote from MSDN:
Quote:

The function fails if you attempt to commit a page that has not been reserved. The resulting error code is ERROR_INVALID_ADDRESS.
@Snatch:
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.

Human 01-19-2006 01:23

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

adaptor 01-19-2006 06:56

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)

MarkusO 01-19-2006 14:36

So I really missed something, even if it is nothing "special". Next time I will read the API documentation better.

Thx @all

SLV 01-22-2006 17:23

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;

Kerlingen 01-22-2006 18:11

@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".


All times are GMT +8. The time now is 11:13.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX