256 lines
11 KiB
C
256 lines
11 KiB
C
/*
|
|
* Unit test suite for the virtual memory APIs.
|
|
*
|
|
* Copyright 2019 Remi Bernon for CodeWeavers
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "ntstatus.h"
|
|
#define WIN32_NO_STATUS
|
|
#include "windef.h"
|
|
#include "winternl.h"
|
|
#include "wine/test.h"
|
|
|
|
static unsigned int page_size;
|
|
|
|
static NTSTATUS (WINAPI *pRtlCreateUserStack)(SIZE_T, SIZE_T, ULONG, SIZE_T, SIZE_T, INITIAL_TEB *);
|
|
static NTSTATUS (WINAPI *pRtlFreeUserStack)(void *);
|
|
static BOOL (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
|
|
|
|
static void test_AllocateVirtualMemory(void)
|
|
{
|
|
void *addr1, *addr2;
|
|
NTSTATUS status;
|
|
SIZE_T size;
|
|
ULONG zero_bits;
|
|
BOOL is_wow64;
|
|
|
|
/* simple allocation should success */
|
|
size = 0x1000;
|
|
addr1 = NULL;
|
|
status = NtAllocateVirtualMemory(NtCurrentProcess(), &addr1, 0, &size,
|
|
MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
|
ok(status == STATUS_SUCCESS, "NtAllocateVirtualMemory returned %08x\n", status);
|
|
|
|
/* allocation conflicts because of 64k align */
|
|
size = 0x1000;
|
|
addr2 = (char *)addr1 + 0x1000;
|
|
status = NtAllocateVirtualMemory(NtCurrentProcess(), &addr2, 0, &size,
|
|
MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
|
ok(status == STATUS_CONFLICTING_ADDRESSES, "NtAllocateVirtualMemory returned %08x\n", status);
|
|
|
|
/* it should conflict, even when zero_bits is explicitly set */
|
|
size = 0x1000;
|
|
addr2 = (char *)addr1 + 0x1000;
|
|
status = NtAllocateVirtualMemory(NtCurrentProcess(), &addr2, 12, &size,
|
|
MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
|
ok(status == STATUS_CONFLICTING_ADDRESSES, "NtAllocateVirtualMemory returned %08x\n", status);
|
|
if (status == STATUS_SUCCESS)
|
|
{
|
|
size = 0;
|
|
status = NtFreeVirtualMemory(NtCurrentProcess(), &addr2, &size, MEM_RELEASE);
|
|
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 */
|
|
size = 0x1000;
|
|
addr2 = NULL;
|
|
status = NtAllocateVirtualMemory(NtCurrentProcess(), &addr2, 21, &size,
|
|
MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
|
todo_wine
|
|
ok(status == STATUS_NO_MEMORY || status == STATUS_INVALID_PARAMETER,
|
|
"NtAllocateVirtualMemory returned %08x\n", status);
|
|
if (status == STATUS_SUCCESS)
|
|
{
|
|
size = 0;
|
|
status = NtFreeVirtualMemory(NtCurrentProcess(), &addr2, &size, MEM_RELEASE);
|
|
ok(status == STATUS_SUCCESS, "NtFreeVirtualMemory return %08x, addr2: %p\n", status, addr2);
|
|
}
|
|
|
|
/* 22 zero bits is invalid */
|
|
size = 0x1000;
|
|
addr2 = NULL;
|
|
status = NtAllocateVirtualMemory(NtCurrentProcess(), &addr2, 22, &size,
|
|
MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
|
ok(status == STATUS_INVALID_PARAMETER_3 || status == STATUS_INVALID_PARAMETER,
|
|
"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
|
|
{
|
|
ok(status == STATUS_SUCCESS || status == STATUS_NO_MEMORY,
|
|
"NtAllocateVirtualMemory returned %08x\n", status);
|
|
if (status == STATUS_SUCCESS)
|
|
{
|
|
todo_wine_if((UINT_PTR)addr2 & ~zero_bits)
|
|
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 */
|
|
size = 0x1000;
|
|
addr2 = (char *)addr1 + 0x1000;
|
|
status = NtAllocateVirtualMemory(NtCurrentProcess(), &addr2, 0, &size,
|
|
MEM_RESERVE | MEM_COMMIT | AT_ROUND_TO_PAGE, PAGE_EXECUTE_READWRITE);
|
|
ok(status == STATUS_INVALID_PARAMETER_5 || status == STATUS_INVALID_PARAMETER,
|
|
"NtAllocateVirtualMemory returned %08x\n", status);
|
|
|
|
size = 0;
|
|
status = NtFreeVirtualMemory(NtCurrentProcess(), &addr1, &size, MEM_RELEASE);
|
|
ok(status == STATUS_SUCCESS, "NtFreeVirtualMemory failed\n");
|
|
}
|
|
|
|
static void test_RtlCreateUserStack(void)
|
|
{
|
|
IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
|
|
SIZE_T default_commit = nt->OptionalHeader.SizeOfStackCommit;
|
|
SIZE_T default_reserve = nt->OptionalHeader.SizeOfStackReserve;
|
|
INITIAL_TEB stack = {0};
|
|
unsigned int i;
|
|
NTSTATUS ret;
|
|
|
|
struct
|
|
{
|
|
SIZE_T commit, reserve, commit_align, reserve_align, expect_commit, expect_reserve;
|
|
}
|
|
tests[] =
|
|
{
|
|
{ 0, 0, 1, 1, default_commit, default_reserve},
|
|
{ 0x2000, 0, 1, 1, 0x2000, default_reserve},
|
|
{ 0x4000, 0, 1, 1, 0x4000, default_reserve},
|
|
{ 0, 0x200000, 1, 1, default_commit, 0x200000},
|
|
{ 0x4000, 0x200000, 1, 1, 0x4000, 0x200000},
|
|
{0x100000, 0x100000, 1, 1, 0x100000, 0x100000},
|
|
{ 0x20000, 0x20000, 1, 1, 0x20000, 0x100000},
|
|
|
|
{ 0, 0x110000, 1, 1, default_commit, 0x110000},
|
|
{ 0, 0x110000, 1, 0x40000, default_commit, 0x140000},
|
|
{ 0, 0x140000, 1, 0x40000, default_commit, 0x140000},
|
|
{ 0x11000, 0x140000, 1, 0x40000, 0x11000, 0x140000},
|
|
{ 0x11000, 0x140000, 0x4000, 0x40000, 0x14000, 0x140000},
|
|
{ 0, 0, 0x4000, 0x400000,
|
|
(default_commit + 0x3fff) & ~0x3fff,
|
|
(default_reserve + 0x3fffff) & ~0x3fffff},
|
|
};
|
|
|
|
if (!pRtlCreateUserStack)
|
|
{
|
|
win_skip("RtlCreateUserStack() is missing\n");
|
|
return;
|
|
}
|
|
|
|
for (i = 0; i < ARRAY_SIZE(tests); ++i)
|
|
{
|
|
memset(&stack, 0xcc, sizeof(stack));
|
|
ret = pRtlCreateUserStack(tests[i].commit, tests[i].reserve, 0,
|
|
tests[i].commit_align, tests[i].reserve_align, &stack);
|
|
ok(!ret, "%u: got status %#x\n", i, ret);
|
|
ok(!stack.OldStackBase, "%u: got OldStackBase %p\n", i, stack.OldStackBase);
|
|
ok(!stack.OldStackLimit, "%u: got OldStackLimit %p\n", i, stack.OldStackLimit);
|
|
ok(!((ULONG_PTR)stack.DeallocationStack & (page_size - 1)),
|
|
"%u: got unaligned memory %p\n", i, stack.DeallocationStack);
|
|
ok((ULONG_PTR)stack.StackBase - (ULONG_PTR)stack.DeallocationStack == tests[i].expect_reserve,
|
|
"%u: got reserve %#lx\n", i, (ULONG_PTR)stack.StackBase - (ULONG_PTR)stack.DeallocationStack);
|
|
todo_wine ok((ULONG_PTR)stack.StackBase - (ULONG_PTR)stack.StackLimit == tests[i].expect_commit,
|
|
"%u: got commit %#lx\n", i, (ULONG_PTR)stack.StackBase - (ULONG_PTR)stack.StackLimit);
|
|
pRtlFreeUserStack(stack.DeallocationStack);
|
|
}
|
|
|
|
ret = pRtlCreateUserStack(0x11000, 0x110000, 0, 1, 0, &stack);
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#x\n", ret);
|
|
|
|
ret = pRtlCreateUserStack(0x11000, 0x110000, 0, 0, 1, &stack);
|
|
ok(ret == STATUS_INVALID_PARAMETER, "got %#x\n", ret);
|
|
}
|
|
|
|
START_TEST(virtual)
|
|
{
|
|
SYSTEM_BASIC_INFORMATION sbi;
|
|
HMODULE mod;
|
|
|
|
mod = GetModuleHandleA("kernel32.dll");
|
|
pIsWow64Process = (void *)GetProcAddress(mod, "IsWow64Process");
|
|
|
|
mod = GetModuleHandleA("ntdll.dll");
|
|
pRtlCreateUserStack = (void *)GetProcAddress(mod, "RtlCreateUserStack");
|
|
pRtlFreeUserStack = (void *)GetProcAddress(mod, "RtlFreeUserStack");
|
|
|
|
NtQuerySystemInformation(SystemBasicInformation, &sbi, sizeof(sbi), NULL);
|
|
trace("system page size %#x\n", sbi.PageSize);
|
|
page_size = sbi.PageSize;
|
|
|
|
test_AllocateVirtualMemory();
|
|
test_RtlCreateUserStack();
|
|
}
|