[NTDLL_APITEST]

Add more tests for NtAllocateVirtualMemory and NtMapViewOfSection, make tests 64 bit compatible.

svn path=/trunk/; revision=58684
This commit is contained in:
Timo Kreuzer 2013-04-05 19:19:14 +00:00
parent 7b8d59daea
commit f73c34fe9a
2 changed files with 133 additions and 19 deletions

View file

@ -207,7 +207,7 @@ CheckSize(ULONG_PTR Base, SIZE_T InSize, SIZE_T ExpectedSize)
SIZE_T Size; SIZE_T Size;
/* Reserve memory */ /* Reserve memory */
BaseAddress = UlongToPtr(Base); BaseAddress = (PVOID)Base;
Size = InSize; Size = InSize;
Status = NtAllocateVirtualMemory(NtCurrentProcess(), Status = NtAllocateVirtualMemory(NtCurrentProcess(),
&BaseAddress, &BaseAddress,
@ -216,7 +216,7 @@ CheckSize(ULONG_PTR Base, SIZE_T InSize, SIZE_T ExpectedSize)
MEM_RESERVE, MEM_RESERVE,
PAGE_NOACCESS); PAGE_NOACCESS);
ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed!\n"); ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed!\n");
ok(BaseAddress == UlongToPtr(Base & ~0xFFFF), "Got back wrong base address: %p\n", BaseAddress); ok(BaseAddress == (PVOID)(Base & ~((ULONG_PTR)0xFFFF)), "Got back wrong base address: %p\n", BaseAddress);
ok(Size == ExpectedSize, "Alloc of 0x%Ix: got back wrong size: 0x%Ix, expected 0x%Ix\n", InSize, Size, ExpectedSize); ok(Size == ExpectedSize, "Alloc of 0x%Ix: got back wrong size: 0x%Ix, expected 0x%Ix\n", InSize, Size, ExpectedSize);
Status = NtFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &Size, MEM_RELEASE); Status = NtFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &Size, MEM_RELEASE);
ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed!\n"); ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed!\n");
@ -324,7 +324,7 @@ CheckAlignment()
_SEH2_TRY _SEH2_TRY
{ {
*(int*)UlongToPtr(BaseAddress) = 1; *(int*)BaseAddress = 1;
*(int*)UlongToPtr(0x50004080) = 1; *(int*)UlongToPtr(0x50004080) = 1;
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
@ -401,7 +401,7 @@ CheckAdjacentVADs()
PAGE_NOACCESS); PAGE_NOACCESS);
ok(!NT_SUCCESS(Status), "NtAllocateVirtualMemory should fail!\n"); ok(!NT_SUCCESS(Status), "NtAllocateVirtualMemory should fail!\n");
/* Allocate a page */ /* Commit a page */
BaseAddress = UlongToPtr(0x50000000); BaseAddress = UlongToPtr(0x50000000);
Size = 0x1000; Size = 0x1000;
Status = NtAllocateVirtualMemory(NtCurrentProcess(), Status = NtAllocateVirtualMemory(NtCurrentProcess(),
@ -412,7 +412,7 @@ CheckAdjacentVADs()
PAGE_READWRITE); PAGE_READWRITE);
ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed!\n"); ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed!\n");
/* Allocate another page */ /* Commit another page */
BaseAddress = UlongToPtr(0x50002000); BaseAddress = UlongToPtr(0x50002000);
Size = 0x1000; Size = 0x1000;
Status = NtAllocateVirtualMemory(NtCurrentProcess(), Status = NtAllocateVirtualMemory(NtCurrentProcess(),
@ -426,7 +426,6 @@ CheckAdjacentVADs()
_SEH2_TRY _SEH2_TRY
{ {
*(int*)UlongToPtr(0x50000000) = 1; *(int*)UlongToPtr(0x50000000) = 1;
//*(int*)UlongToPtr(0x50002000) = 1;
} }
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{ {
@ -434,6 +433,17 @@ CheckAdjacentVADs()
} }
_SEH2_END; _SEH2_END;
_SEH2_TRY
{
(void)*(volatile int*)UlongToPtr(0x50002000);
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
Status = _SEH2_GetExceptionCode();
}
_SEH2_END;
ok(Status = STATUS_ACCESS_VIOLATION, "Should get an exception!\n");
/* Allocate 3 pages, on top of the previous 2 */ /* Allocate 3 pages, on top of the previous 2 */
BaseAddress = UlongToPtr(0x50000000); BaseAddress = UlongToPtr(0x50000000);
Size = 0x3000; Size = 0x3000;
@ -442,9 +452,20 @@ CheckAdjacentVADs()
0, 0,
&Size, &Size,
MEM_COMMIT, MEM_COMMIT,
PAGE_NOACCESS); PAGE_READONLY);
ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed!\n"); ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed!\n");
_SEH2_TRY
{
*(int*)UlongToPtr(0x50000000) = 1;
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
Status = _SEH2_GetExceptionCode();
}
_SEH2_END;
ok(Status = STATUS_ACCESS_VIOLATION, "Should get an exception!\n");
/* Try to free the whole region at once */ /* Try to free the whole region at once */
BaseAddress = UlongToPtr(0x50000000); BaseAddress = UlongToPtr(0x50000000);
Size = 0x30000; Size = 0x30000;
@ -466,6 +487,25 @@ CheckAdjacentVADs()
Status = NtFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &Size, MEM_RELEASE); Status = NtFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &Size, MEM_RELEASE);
ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed!\n"); ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed!\n");
/* Reserve 3 full 64k region */
BaseAddress = UlongToPtr(0x50000000);
Size = 0x30000;
Status = NtAllocateVirtualMemory(NtCurrentProcess(),
&BaseAddress,
0,
&Size,
MEM_RESERVE,
PAGE_NOACCESS);
ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed!\n");
if (!NT_SUCCESS(Status))
return;
/* Release the 64k region in the middle */
BaseAddress = UlongToPtr(0x50010000);
Size = 0x10000;
Status = NtFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &Size, MEM_RELEASE);
ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed!\n");
} }
#define RUNS 32 #define RUNS 32

View file

@ -191,7 +191,7 @@ Test_PageFileSection(void)
ok(Status == STATUS_UNABLE_TO_DELETE_SECTION, ok(Status == STATUS_UNABLE_TO_DELETE_SECTION,
"NtFreeVirtualMemory failed with wrong Status %lx\n", Status); "NtFreeVirtualMemory failed with wrong Status %lx\n", Status);
BaseAddress = (PVOID)0x40000000; BaseAddress = UlongToPtr(0x40000000);
SectionOffset.QuadPart = 0; SectionOffset.QuadPart = 0;
ViewSize = 0x1000; ViewSize = 0x1000;
Status = NtMapViewOfSection(SectionHandle, Status = NtMapViewOfSection(SectionHandle,
@ -208,7 +208,7 @@ Test_PageFileSection(void)
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
return; return;
ok(BaseAddress == (PVOID)0x40000000, "Invalid BaseAddress: %p", BaseAddress); ok(BaseAddress == UlongToPtr(0x40000000), "Invalid BaseAddress: %p", BaseAddress);
BaseAddress = (PVOID)0x40080000; BaseAddress = (PVOID)0x40080000;
SectionOffset.QuadPart = 0x10000; SectionOffset.QuadPart = 0x10000;
@ -305,13 +305,16 @@ Test_ImageSection(void)
/* Check the original data */ /* Check the original data */
ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n"); ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
/* Now modify the data in the data section */ /* Modify the PE header (but do not flush!) */
*(ULONG*)DataBase = 0xdeadbabe; *(ULONG*)DataBase = 0xdeadbabe;
/* Check the data */
ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n"); ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n");
/* Now try to create an image section */ /* Modify data in the .data section (but do not flush!) */
ok(*(ULONG*)((PUCHAR)DataBase + 0x800) == 0x12345678,
"Data in .data section invalid: 0x%lx!\n", *(ULONG*)((PUCHAR)DataBase + 0x800));
*(ULONG*)((PUCHAR)DataBase + 0x800) = 0x87654321;
/* Now try to create an image section (should fail) */
Status = NtCreateSection(&ImageSectionHandle, Status = NtCreateSection(&ImageSectionHandle,
SECTION_ALL_ACCESS, // DesiredAccess SECTION_ALL_ACCESS, // DesiredAccess
NULL, // ObjectAttributes NULL, // ObjectAttributes
@ -321,9 +324,13 @@ Test_ImageSection(void)
FileHandle); FileHandle);
ok(Status == STATUS_INVALID_IMAGE_NOT_MZ, "NtCreateSection failed, Status 0x%lx\n", Status); ok(Status == STATUS_INVALID_IMAGE_NOT_MZ, "NtCreateSection failed, Status 0x%lx\n", Status);
/* Restore the original data */ /* Restore the original header */
*(ULONG*)DataBase = 0x00905a4d; *(ULONG*)DataBase = 0x00905a4d;
/* Modify data in the .data section (but do not flush!) */
ok_hex(*(ULONG*)((PUCHAR)DataBase + 0x800), 0x87654321);
*(ULONG*)((PUCHAR)DataBase + 0x800) = 0xdeadbabe;
/* Try to create an image section again */ /* Try to create an image section again */
Status = NtCreateSection(&ImageSectionHandle, Status = NtCreateSection(&ImageSectionHandle,
SECTION_ALL_ACCESS, // DesiredAccess SECTION_ALL_ACCESS, // DesiredAccess
@ -347,18 +354,31 @@ Test_ImageSection(void)
ViewShare, ViewShare,
0, 0,
PAGE_READONLY); PAGE_READONLY);
#ifdef _M_IX86
ok(Status == STATUS_SUCCESS, "NtMapViewOfSection failed, Status 0x%lx\n", Status); ok(Status == STATUS_SUCCESS, "NtMapViewOfSection failed, Status 0x%lx\n", Status);
#else
ok(Status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH, "NtMapViewOfSection failed, Status 0x%lx\n", Status);
#endif
/* Check the data */ /* Check the header */
ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n"); ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
ok(*(ULONG*)ImageBase == 0x00905a4d, "Header not ok\n"); ok(*(ULONG*)ImageBase == 0x00905a4d, "Header not ok\n");
/* Check the data section. Either of these can be present! */
ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
"Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
/* Now modify the data again */ /* Now modify the data again */
*(ULONG*)DataBase = 0xdeadbabe; *(ULONG*)DataBase = 0xdeadbabe;
*(ULONG*)((PUCHAR)DataBase + 0x800) = 0xf00dada;
/* Check the data */ /* Check the data */
ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n"); ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\n");
ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n"); ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n");
ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
"Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
/* Flush the view */ /* Flush the view */
ViewSize = 0x1000; ViewSize = 0x1000;
@ -370,21 +390,75 @@ Test_ImageSection(void)
/* Check the data again */ /* Check the data again */
ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n"); ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n");
ok((*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x87654321) ||
(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0x12345678),
"Wrong value in data section: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
/* Restore the original data */ /* Restore the original header */
*(ULONG*)DataBase = 0x00905a4d; *(ULONG*)DataBase = 0x00905a4d;
ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n"); ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
/* Cleanup */ /* Close the image mapping */
NtUnmapViewOfSection(NtCurrentProcess(), ImageBase); NtUnmapViewOfSection(NtCurrentProcess(), ImageBase);
NtUnmapViewOfSection(NtCurrentProcess(), DataBase);
NtClose(ImageSectionHandle); NtClose(ImageSectionHandle);
/* Create an image section again */
Status = NtCreateSection(&ImageSectionHandle,
SECTION_ALL_ACCESS, // DesiredAccess
NULL, // ObjectAttributes
NULL, // MaximumSize
PAGE_READWRITE, // SectionPageProtection
SEC_IMAGE, // AllocationAttributes
FileHandle);
ok(Status == STATUS_SUCCESS, "NtCreateSection failed, Status 0x%lx\n", Status);
/* Map the image section again */
ImageBase = NULL;
ViewSize = 0;
Status = NtMapViewOfSection(ImageSectionHandle,
NtCurrentProcess(),
&ImageBase,
0,
0,
NULL,
&ViewSize,
ViewShare,
0,
PAGE_READONLY);
#ifdef _M_IX86
ok(Status == STATUS_SUCCESS, "NtMapViewOfSection failed, Status 0x%lx\n", Status);
#else
ok(Status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH, "NtMapViewOfSection failed, Status 0x%lx\n", Status);
#endif
/* Check the .data section again */
ok(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0xf00dada,
"Data should be synced: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
/* Restore the original data */
*(ULONG*)((PUCHAR)DataBase + 0x800) = 0x12345678;
/* Close the data mapping */
NtUnmapViewOfSection(NtCurrentProcess(), DataBase);
NtClose(DataSectionHandle); NtClose(DataSectionHandle);
/* Try to allocate memory inside the image mapping */
DataBase = (PUCHAR)ImageBase + 0x20000;
ViewSize = 0x1000;
Status = NtAllocateVirtualMemory(NtCurrentProcess(), &DataBase, 0, &ViewSize, MEM_RESERVE, PAGE_NOACCESS);
ok(Status == STATUS_CONFLICTING_ADDRESSES, "Wrong Status: 0x%lx\n", Status);
/* Cleanup */
NtClose(FileHandle); NtClose(FileHandle);
NtClose(ImageSectionHandle);
NtUnmapViewOfSection(NtCurrentProcess(), ImageBase);
} }
START_TEST(NtMapViewOfSection) START_TEST(NtMapViewOfSection)
{ {
//Test_PageFileSection(); Test_PageFileSection();
Test_ImageSection(); Test_ImageSection();
} }