The ZeroBits 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:
* ZeroBits == 0: no constraint on address range * 0 < ZeroBits <= 15: returned address should have as many upper bits set to 0, starting at bit 31. In 64bit mode, upper 64bits should all be 0 as well. * 15 < ZeroBits <= 31: unsure, but probably same as ZeroBits == 15. * ZeroBits > 31: (64bit/WoW64 only) ZeroBits 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: Rémi Bernon rbernon@codeweavers.com --- dlls/kernel32/tests/virtual.c | 72 +++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+)
diff --git a/dlls/kernel32/tests/virtual.c b/dlls/kernel32/tests/virtual.c index 474955630fd..c6a9f34954a 100644 --- a/dlls/kernel32/tests/virtual.c +++ b/dlls/kernel32/tests/virtual.c @@ -228,10 +228,12 @@ static void test_VirtualAllocEx(void) static void test_VirtualAlloc(void) { void *addr1, *addr2; + UINT_PTR mask; DWORD old_prot; MEMORY_BASIC_INFORMATION info; NTSTATUS status; SIZE_T size; + BOOL is_wow64;
SetLastError(0xdeadbeef); addr1 = VirtualAlloc(0, 0, MEM_RESERVE, PAGE_NOACCESS); @@ -473,6 +475,76 @@ static void test_VirtualAlloc(void) ok(status == STATUS_INVALID_PARAMETER_3, "NtAllocateVirtualMemory returned %08x\n", status); if (status == STATUS_SUCCESS) ok(VirtualFree(addr2, 0, MEM_RELEASE), "VirtualFree failed\n");
+ /* 1 zero bits should zero 63-31 upper bits */ + size = 0x1000; + addr2 = NULL; + mask = 0xffffffff8000ffffull; + status = pNtAllocateVirtualMemory(GetCurrentProcess(), &addr2, 1, &size, + MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + ok((status == STATUS_SUCCESS || broken(status == STATUS_INVALID_PARAMETER_3) /* winxp */) + && ((uintptr_t)addr2 & mask) == 0, + "NtAllocateVirtualMemory returned %08x, addr2: %x, mask: %x\n", status, addr2, mask); + if (status == STATUS_SUCCESS) + { + size = 0; + status = pNtFreeVirtualMemory(GetCurrentProcess(), &addr2, &size, MEM_RELEASE); + ok(status == STATUS_SUCCESS, "pNtFreeVirtualMemory return %08x, addr2: %p\n", status, addr2); + } + + /* 2 zero bits should zero 63-31 upper bits */ + size = 0x1000; + addr2 = NULL; + mask = 0xffffffff8000ffffull; + status = pNtAllocateVirtualMemory(GetCurrentProcess(), &addr2, 2, &size, + MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + ok(status == STATUS_SUCCESS && ((uintptr_t)addr2 & mask) == 0, + "NtAllocateVirtualMemory returned %08x, addr2: %x, mask: %x\n", status, addr2, mask); + if (status == STATUS_SUCCESS) + { + size = 0; + status = pNtFreeVirtualMemory(GetCurrentProcess(), &addr2, &size, MEM_RELEASE); + ok(status == STATUS_SUCCESS, "pNtFreeVirtualMemory return %08x, addr2: %p\n", status, addr2); + } + + /* 15 zero bits should zero 63-17 upper bits */ + size = 0x1000; + addr2 = NULL; + mask = 0xfffffffffff7ffffull; + status = pNtAllocateVirtualMemory(GetCurrentProcess(), &addr2, 15, &size, + MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + ok((status == STATUS_SUCCESS || status == STATUS_NO_MEMORY) && ((uintptr_t)addr2 & mask) == 0, + "NtAllocateVirtualMemory returned %08x, addr2: %x, mask: %x\n", status, addr2, mask); + if (status == STATUS_SUCCESS) + { + size = 0; + status = pNtFreeVirtualMemory(GetCurrentProcess(), &addr2, &size, MEM_RELEASE); + ok(status == STATUS_SUCCESS, "pNtFreeVirtualMemory return %08x, addr2: %p\n", status, addr2); + } + + /* zero bits > 31 should be considered as bitmask on 64bit and WoW64 */ + size = 0x1000; + addr2 = NULL; + mask = 0xffffffff7000ffffull; + status = pNtAllocateVirtualMemory(GetCurrentProcess(), &addr2, 0x1fffffff, &size, + MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + + if (sizeof(void *) == sizeof(int) && (!pIsWow64Process || + !pIsWow64Process( GetCurrentProcess(), &is_wow64 ) || !is_wow64)) + { + ok(status == STATUS_INVALID_PARAMETER_3, "NtAllocateVirtualMemory returned %08x\n", status); + } + else + { + ok(status == STATUS_SUCCESS && ((uintptr_t)addr2 & mask) == 0, + "NtAllocateVirtualMemory returned %08x, addr2: %x, mask: %x\n", status, addr2, mask); + if (status == STATUS_SUCCESS) + { + size = 0; + status = pNtFreeVirtualMemory(GetCurrentProcess(), &addr2, &size, MEM_RELEASE); + ok(status == STATUS_SUCCESS, "pNtFreeVirtualMemory return %08x, addr2: %p\n", status, addr2); + } + } + /* AT_ROUND_TO_PAGE flag is not supported for VirtualAlloc */ SetLastError(0xdeadbeef); addr2 = VirtualAlloc(addr1, 0x1000, MEM_RESERVE | MEM_COMMIT | AT_ROUND_TO_PAGE, PAGE_EXECUTE_READWRITE);