From f73c34fe9a15930473f05f3086b3da0a114b4111 Mon Sep 17 00:00:00 2001 From: Timo Kreuzer Date: Fri, 5 Apr 2013 19:19:14 +0000 Subject: [PATCH] [NTDLL_APITEST] Add more tests for NtAllocateVirtualMemory and NtMapViewOfSection, make tests 64 bit compatible. svn path=/trunk/; revision=58684 --- .../apitests/ntdll/NtAllocateVirtualMemory.c | 54 ++++++++-- rostests/apitests/ntdll/NtMapViewOfSection.c | 98 ++++++++++++++++--- 2 files changed, 133 insertions(+), 19 deletions(-) diff --git a/rostests/apitests/ntdll/NtAllocateVirtualMemory.c b/rostests/apitests/ntdll/NtAllocateVirtualMemory.c index 597f3328959..dfe972364e2 100644 --- a/rostests/apitests/ntdll/NtAllocateVirtualMemory.c +++ b/rostests/apitests/ntdll/NtAllocateVirtualMemory.c @@ -207,7 +207,7 @@ CheckSize(ULONG_PTR Base, SIZE_T InSize, SIZE_T ExpectedSize) SIZE_T Size; /* Reserve memory */ - BaseAddress = UlongToPtr(Base); + BaseAddress = (PVOID)Base; Size = InSize; Status = NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress, @@ -216,7 +216,7 @@ CheckSize(ULONG_PTR Base, SIZE_T InSize, SIZE_T ExpectedSize) MEM_RESERVE, PAGE_NOACCESS); 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); Status = NtFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &Size, MEM_RELEASE); ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed!\n"); @@ -324,7 +324,7 @@ CheckAlignment() _SEH2_TRY { - *(int*)UlongToPtr(BaseAddress) = 1; + *(int*)BaseAddress = 1; *(int*)UlongToPtr(0x50004080) = 1; } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) @@ -401,7 +401,7 @@ CheckAdjacentVADs() PAGE_NOACCESS); ok(!NT_SUCCESS(Status), "NtAllocateVirtualMemory should fail!\n"); - /* Allocate a page */ + /* Commit a page */ BaseAddress = UlongToPtr(0x50000000); Size = 0x1000; Status = NtAllocateVirtualMemory(NtCurrentProcess(), @@ -412,7 +412,7 @@ CheckAdjacentVADs() PAGE_READWRITE); ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed!\n"); - /* Allocate another page */ + /* Commit another page */ BaseAddress = UlongToPtr(0x50002000); Size = 0x1000; Status = NtAllocateVirtualMemory(NtCurrentProcess(), @@ -426,7 +426,6 @@ CheckAdjacentVADs() _SEH2_TRY { *(int*)UlongToPtr(0x50000000) = 1; - //*(int*)UlongToPtr(0x50002000) = 1; } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { @@ -434,6 +433,17 @@ CheckAdjacentVADs() } _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 */ BaseAddress = UlongToPtr(0x50000000); Size = 0x3000; @@ -442,9 +452,20 @@ CheckAdjacentVADs() 0, &Size, MEM_COMMIT, - PAGE_NOACCESS); + PAGE_READONLY); 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 */ BaseAddress = UlongToPtr(0x50000000); Size = 0x30000; @@ -466,6 +487,25 @@ CheckAdjacentVADs() Status = NtFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &Size, MEM_RELEASE); 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 diff --git a/rostests/apitests/ntdll/NtMapViewOfSection.c b/rostests/apitests/ntdll/NtMapViewOfSection.c index 08b300cc4e3..3cee1577775 100644 --- a/rostests/apitests/ntdll/NtMapViewOfSection.c +++ b/rostests/apitests/ntdll/NtMapViewOfSection.c @@ -191,7 +191,7 @@ Test_PageFileSection(void) ok(Status == STATUS_UNABLE_TO_DELETE_SECTION, "NtFreeVirtualMemory failed with wrong Status %lx\n", Status); - BaseAddress = (PVOID)0x40000000; + BaseAddress = UlongToPtr(0x40000000); SectionOffset.QuadPart = 0; ViewSize = 0x1000; Status = NtMapViewOfSection(SectionHandle, @@ -208,7 +208,7 @@ Test_PageFileSection(void) if (!NT_SUCCESS(Status)) return; - ok(BaseAddress == (PVOID)0x40000000, "Invalid BaseAddress: %p", BaseAddress); + ok(BaseAddress == UlongToPtr(0x40000000), "Invalid BaseAddress: %p", BaseAddress); BaseAddress = (PVOID)0x40080000; SectionOffset.QuadPart = 0x10000; @@ -305,13 +305,16 @@ Test_ImageSection(void) /* Check the original data */ 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; - - /* Check the data */ 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, SECTION_ALL_ACCESS, // DesiredAccess NULL, // ObjectAttributes @@ -321,9 +324,13 @@ Test_ImageSection(void) FileHandle); 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; + /* 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 */ Status = NtCreateSection(&ImageSectionHandle, SECTION_ALL_ACCESS, // DesiredAccess @@ -347,18 +354,31 @@ Test_ImageSection(void) 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 */ + /* Check the header */ ok(*(ULONG*)DataBase == 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 */ *(ULONG*)DataBase = 0xdeadbabe; + *(ULONG*)((PUCHAR)DataBase + 0x800) = 0xf00dada; /* Check the data */ ok(*(ULONG*)DataBase == 0xdeadbabe, "Header not ok\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 */ ViewSize = 0x1000; @@ -370,21 +390,75 @@ Test_ImageSection(void) /* Check the data again */ 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; ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n"); - /* Cleanup */ + /* Close the image mapping */ NtUnmapViewOfSection(NtCurrentProcess(), ImageBase); - NtUnmapViewOfSection(NtCurrentProcess(), DataBase); 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); + + /* 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(ImageSectionHandle); + NtUnmapViewOfSection(NtCurrentProcess(), ImageBase); } + START_TEST(NtMapViewOfSection) { - //Test_PageFileSection(); + Test_PageFileSection(); Test_ImageSection(); } +