2013-09-22 19:07:35 +00:00
|
|
|
|
|
|
|
#include <apitest.h>
|
|
|
|
|
2011-07-11 19:31:34 +00:00
|
|
|
#define WIN32_NO_STATUS
|
2013-02-05 17:54:22 +00:00
|
|
|
#include <ndk/pstypes.h>
|
|
|
|
#include <ndk/mmfuncs.h>
|
2011-07-11 19:31:34 +00:00
|
|
|
|
|
|
|
static void Test_NtFreeVirtualMemory(void)
|
|
|
|
{
|
|
|
|
PVOID Buffer = NULL, Buffer2;
|
|
|
|
SIZE_T Length = PAGE_SIZE;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
Status = NtAllocateVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer,
|
|
|
|
0,
|
|
|
|
&Length,
|
|
|
|
MEM_RESERVE,
|
|
|
|
PAGE_READWRITE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status);
|
|
|
|
ok(Length == PAGE_SIZE, "Length mismatch : 0x%08lx\n", (ULONG)Length);
|
2011-07-11 19:31:34 +00:00
|
|
|
ok(((ULONG_PTR)Buffer % PAGE_SIZE) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
|
|
|
|
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer,
|
|
|
|
&Length,
|
|
|
|
MEM_DECOMMIT);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(Status == STATUS_SUCCESS, "NtFreeVirtualMemory failed : 0x%08lx\n", Status);
|
2011-07-11 19:31:34 +00:00
|
|
|
|
|
|
|
/* Now try to free more than we got */
|
|
|
|
Length++;
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer,
|
|
|
|
&Length,
|
|
|
|
MEM_DECOMMIT);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(Status == STATUS_UNABLE_TO_FREE_VM, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status);
|
2011-07-11 19:31:34 +00:00
|
|
|
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer,
|
|
|
|
&Length,
|
|
|
|
MEM_RELEASE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(Status == STATUS_UNABLE_TO_FREE_VM, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status);
|
2011-07-11 19:31:34 +00:00
|
|
|
|
|
|
|
/* Free out of bounds from the wrong origin */
|
|
|
|
Length = PAGE_SIZE;
|
|
|
|
Buffer2 = (PVOID)((ULONG_PTR)Buffer+1);
|
|
|
|
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer2,
|
|
|
|
&Length,
|
|
|
|
MEM_DECOMMIT);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(Status == STATUS_UNABLE_TO_FREE_VM, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status);
|
2011-07-11 19:31:34 +00:00
|
|
|
|
|
|
|
Buffer2 = (PVOID)((ULONG_PTR)Buffer+1);
|
|
|
|
Length = PAGE_SIZE;
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer2,
|
|
|
|
&Length,
|
|
|
|
MEM_RELEASE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(Status == STATUS_UNABLE_TO_FREE_VM, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status);
|
2011-07-11 19:31:34 +00:00
|
|
|
|
|
|
|
/* Same but in bounds */
|
|
|
|
Length = PAGE_SIZE - 1;
|
|
|
|
Buffer2 = (PVOID)((ULONG_PTR)Buffer+1);
|
|
|
|
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer2,
|
|
|
|
&Length,
|
|
|
|
MEM_DECOMMIT);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(Status == STATUS_SUCCESS, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status);
|
2011-07-11 19:31:34 +00:00
|
|
|
ok(Buffer2 == Buffer, "NtFreeVirtualMemory set wrong buffer.\n");
|
|
|
|
ok(Length == PAGE_SIZE, "NtFreeVirtualMemory did not round Length to PAGE_SIZE.\n");
|
|
|
|
|
|
|
|
Buffer2 = (PVOID)((ULONG_PTR)Buffer+1);
|
|
|
|
Length = PAGE_SIZE-1;
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer2,
|
|
|
|
&Length,
|
|
|
|
MEM_RELEASE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(Status == STATUS_SUCCESS, "NtFreeVirtualMemory returned status : 0x%08lx\n", Status);
|
2011-07-11 19:31:34 +00:00
|
|
|
ok(Buffer2 == Buffer, "NtFreeVirtualMemory set wrong buffer.\n");
|
|
|
|
ok(Length == PAGE_SIZE, "NtFreeVirtualMemory did not round Length to PAGE_SIZE.\n");
|
|
|
|
|
|
|
|
/* Now allocate two pages and try to free them one after the other */
|
|
|
|
Length = 2*PAGE_SIZE;
|
|
|
|
Status = NtAllocateVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer,
|
|
|
|
0,
|
|
|
|
&Length,
|
|
|
|
MEM_RESERVE,
|
|
|
|
PAGE_READWRITE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status);
|
|
|
|
ok(Length == 2*PAGE_SIZE, "Length mismatch : 0x%08lx\n", (ULONG)Length);
|
2011-07-11 19:31:34 +00:00
|
|
|
ok(((ULONG_PTR)Buffer % PAGE_SIZE) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
|
|
|
|
|
|
|
|
Buffer2 = Buffer;
|
|
|
|
Length = PAGE_SIZE;
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer2,
|
|
|
|
&Length,
|
|
|
|
MEM_RELEASE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed : 0x%08lx\n", Status);
|
|
|
|
ok(Length == PAGE_SIZE, "Length mismatch : 0x%08lx\n", (ULONG)Length);
|
2011-07-11 19:31:34 +00:00
|
|
|
ok(Buffer2 == Buffer, "The buffer is not aligned to PAGE_SIZE.\n");
|
|
|
|
|
|
|
|
Buffer2 = (PVOID)((ULONG_PTR)Buffer+PAGE_SIZE);
|
|
|
|
Length = PAGE_SIZE;
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer2,
|
|
|
|
&Length,
|
|
|
|
MEM_RELEASE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed : 0x%08lx\n", Status);
|
|
|
|
ok(Length == PAGE_SIZE, "Length mismatch : 0x%08lx\n", (ULONG)Length);
|
2011-07-11 19:31:34 +00:00
|
|
|
ok(Buffer2 == (PVOID)((ULONG_PTR)Buffer+PAGE_SIZE), "The buffer is not aligned to PAGE_SIZE.\n");
|
2011-07-12 17:36:52 +00:00
|
|
|
|
2011-07-12 17:55:20 +00:00
|
|
|
/* Same, but try to free the second page before the first one */
|
|
|
|
Length = 2*PAGE_SIZE;
|
|
|
|
Status = NtAllocateVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer,
|
|
|
|
0,
|
|
|
|
&Length,
|
|
|
|
MEM_RESERVE,
|
|
|
|
PAGE_READWRITE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status);
|
|
|
|
ok(Length == 2*PAGE_SIZE, "Length mismatch : 0x%08lx\n", (ULONG)Length);
|
2011-07-12 17:55:20 +00:00
|
|
|
ok(((ULONG_PTR)Buffer % PAGE_SIZE) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
|
|
|
|
|
|
|
|
Buffer2 = (PVOID)((ULONG_PTR)Buffer+PAGE_SIZE);
|
|
|
|
Length = PAGE_SIZE;
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer2,
|
|
|
|
&Length,
|
|
|
|
MEM_RELEASE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed : 0x%08lx\n", Status);
|
|
|
|
ok(Length == PAGE_SIZE, "Length mismatch : 0x%08lx\n", (ULONG)Length);
|
2011-07-12 17:55:20 +00:00
|
|
|
ok(Buffer2 == (PVOID)((ULONG_PTR)Buffer+PAGE_SIZE), "The buffer is not aligned to PAGE_SIZE.\n");
|
|
|
|
|
|
|
|
Buffer2 = Buffer;
|
|
|
|
Length = PAGE_SIZE;
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer2,
|
|
|
|
&Length,
|
|
|
|
MEM_RELEASE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed : 0x%08lx\n", Status);
|
|
|
|
ok(Length == PAGE_SIZE, "Length mismatch : 0x%08lx\n", (ULONG)Length);
|
2011-07-12 17:55:20 +00:00
|
|
|
ok(Buffer2 == Buffer, "The buffer is not aligned to PAGE_SIZE.\n");
|
|
|
|
|
2011-07-12 17:36:52 +00:00
|
|
|
/* Now allocate two pages and try to free them in the middle */
|
|
|
|
Length = 2*PAGE_SIZE;
|
|
|
|
Status = NtAllocateVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer,
|
|
|
|
0,
|
|
|
|
&Length,
|
|
|
|
MEM_RESERVE,
|
|
|
|
PAGE_READWRITE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed : 0x%08lx\n", Status);
|
|
|
|
ok(Length == 2*PAGE_SIZE, "Length mismatch : 0x%08lx\n", (ULONG)Length);
|
2011-07-12 17:36:52 +00:00
|
|
|
ok(((ULONG_PTR)Buffer % PAGE_SIZE) == 0, "The buffer is not aligned to PAGE_SIZE.\n");
|
|
|
|
|
|
|
|
Buffer2 = (PVOID)((ULONG_PTR)Buffer+1);
|
|
|
|
Length = PAGE_SIZE;
|
|
|
|
Status = NtFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&Buffer2,
|
|
|
|
&Length,
|
|
|
|
MEM_RELEASE);
|
2011-09-03 19:44:07 +00:00
|
|
|
ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed : 0x%08lx\n", Status);
|
|
|
|
ok(Length == 2*PAGE_SIZE, "Length mismatch : 0x%08lx\n", (ULONG)Length);
|
2011-07-12 17:36:52 +00:00
|
|
|
ok(Buffer2 == Buffer, "The buffer is not aligned to PAGE_SIZE.\n");
|
2011-07-11 19:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(NtFreeVirtualMemory)
|
|
|
|
{
|
|
|
|
Test_NtFreeVirtualMemory();
|
|
|
|
}
|