Commit 8878492d authored by Rémi Bernon's avatar Rémi Bernon Committed by Alexandre Julliard

ntdll/tests: Add NtAllocateVirtualMemory tests for zero_bits behavior.

The zero_bits parameter doesn't behave as expected, and some 64bit code use it to allocate memory in the lower 32bit address space. The expected full behaviour is: * zero_bits == 0: no constraint on address range * 0 < zero_bits <= 15: returned address should have as many upper bits set to 0, starting at bit 31. In 64bit mode, upper 32bits should all be 0 as well. * 15 < zero_bits <= 31: unsure, but probably same as zero_bits == 15. * zero_bits > 31: (64bit/WoW64 only) zero_bits behaves as a bitmask, as if it was set to the number of leading 0 in the bitmask, works in the whole 64bit range. Signed-off-by: 's avatarRémi Bernon <rbernon@codeweavers.com> Signed-off-by: 's avatarHuw Davies <huw@codeweavers.com> Signed-off-by: 's avatarAlexandre Julliard <julliard@winehq.org>
parent f39c1eee
...@@ -26,11 +26,15 @@ ...@@ -26,11 +26,15 @@
#include "winternl.h" #include "winternl.h"
#include "wine/test.h" #include "wine/test.h"
static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
static void test_AllocateVirtualMemory(void) static void test_AllocateVirtualMemory(void)
{ {
void *addr1, *addr2; void *addr1, *addr2;
NTSTATUS status; NTSTATUS status;
SIZE_T size; SIZE_T size;
ULONG zero_bits;
BOOL is_wow64;
/* simple allocation should success */ /* simple allocation should success */
size = 0x1000; size = 0x1000;
...@@ -60,6 +64,46 @@ static void test_AllocateVirtualMemory(void) ...@@ -60,6 +64,46 @@ static void test_AllocateVirtualMemory(void)
ok(status == STATUS_SUCCESS, "NtFreeVirtualMemory return %08x, addr2: %p\n", status, addr2); ok(status == STATUS_SUCCESS, "NtFreeVirtualMemory return %08x, addr2: %p\n", status, addr2);
} }
/* 1 zero bits should zero 63-31 upper bits */
size = 0x1000;
addr2 = NULL;
zero_bits = 1;
status = NtAllocateVirtualMemory(NtCurrentProcess(), &addr2, 1, &size,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
ok(status == STATUS_SUCCESS || status == STATUS_NO_MEMORY ||
broken(status == STATUS_INVALID_PARAMETER_3) /* winxp */,
"NtAllocateVirtualMemory returned %08x\n", status);
if (status == STATUS_SUCCESS)
{
ok(((UINT_PTR)addr2 >> (32 - zero_bits)) == 0,
"NtAllocateVirtualMemory returned address: %p\n", addr2);
size = 0;
status = NtFreeVirtualMemory(NtCurrentProcess(), &addr2, &size, MEM_RELEASE);
ok(status == STATUS_SUCCESS, "NtFreeVirtualMemory return %08x, addr2: %p\n", status, addr2);
}
for (zero_bits = 2; zero_bits <= 20; zero_bits++)
{
size = 0x1000;
addr2 = NULL;
status = NtAllocateVirtualMemory(NtCurrentProcess(), &addr2, zero_bits, &size,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
ok(status == STATUS_SUCCESS || status == STATUS_NO_MEMORY ||
broken(zero_bits == 20 && status == STATUS_CONFLICTING_ADDRESSES) /* w1064v1809 */,
"NtAllocateVirtualMemory with %d zero_bits returned %08x\n", zero_bits, status);
if (status == STATUS_SUCCESS)
{
todo_wine_if((UINT_PTR)addr2 >> (32 - zero_bits))
ok(((UINT_PTR)addr2 >> (32 - zero_bits)) == 0,
"NtAllocateVirtualMemory with %d zero_bits returned address %p\n", zero_bits, addr2);
size = 0;
status = NtFreeVirtualMemory(NtCurrentProcess(), &addr2, &size, MEM_RELEASE);
ok(status == STATUS_SUCCESS, "NtFreeVirtualMemory return %08x, addr2: %p\n", status, addr2);
}
}
/* 21 zero bits never succeeds */ /* 21 zero bits never succeeds */
size = 0x1000; size = 0x1000;
addr2 = NULL; addr2 = NULL;
...@@ -83,6 +127,35 @@ static void test_AllocateVirtualMemory(void) ...@@ -83,6 +127,35 @@ static void test_AllocateVirtualMemory(void)
ok(status == STATUS_INVALID_PARAMETER_3 || status == STATUS_INVALID_PARAMETER, ok(status == STATUS_INVALID_PARAMETER_3 || status == STATUS_INVALID_PARAMETER,
"NtAllocateVirtualMemory returned %08x\n", status); "NtAllocateVirtualMemory returned %08x\n", status);
/* zero bits > 31 should be considered as bitmask on 64bit and WoW64 */
size = 0x1000;
addr2 = NULL;
zero_bits = 0x1fffffff;
status = NtAllocateVirtualMemory(NtCurrentProcess(), &addr2, zero_bits, &size,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (sizeof(void *) == sizeof(int) && (!pIsWow64Process ||
!pIsWow64Process(NtCurrentProcess(), &is_wow64) || !is_wow64))
{
ok(status == STATUS_INVALID_PARAMETER_3, "NtAllocateVirtualMemory returned %08x\n", status);
}
else
{
todo_wine
ok(status == STATUS_SUCCESS || status == STATUS_NO_MEMORY,
"NtAllocateVirtualMemory returned %08x\n", status);
if (status == STATUS_SUCCESS)
{
todo_wine
ok(((UINT_PTR)addr2 & ~zero_bits) == 0,
"NtAllocateVirtualMemory returned address %p\n", addr2);
size = 0;
status = NtFreeVirtualMemory(NtCurrentProcess(), &addr2, &size, MEM_RELEASE);
ok(status == STATUS_SUCCESS, "NtFreeVirtualMemory return %08x, addr2: %p\n", status, addr2);
}
}
/* AT_ROUND_TO_PAGE flag is not supported for NtAllocateVirtualMemory */ /* AT_ROUND_TO_PAGE flag is not supported for NtAllocateVirtualMemory */
size = 0x1000; size = 0x1000;
addr2 = (char *)addr1 + 0x1000; addr2 = (char *)addr1 + 0x1000;
...@@ -99,6 +172,10 @@ static void test_AllocateVirtualMemory(void) ...@@ -99,6 +172,10 @@ static void test_AllocateVirtualMemory(void)
START_TEST(virtual) START_TEST(virtual)
{ {
SYSTEM_BASIC_INFORMATION sbi; SYSTEM_BASIC_INFORMATION sbi;
HMODULE hkernel32;
hkernel32 = GetModuleHandleA("kernel32.dll");
pIsWow64Process = (void *)GetProcAddress(hkernel32, "IsWow64Process");
NtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), NULL); NtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), NULL);
trace("system page size %#x\n", sbi.PageSize); trace("system page size %#x\n", sbi.PageSize);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment