Author: tkreuzer Date: Fri Apr 5 19:19:14 2013 New Revision: 58684
URL: http://svn.reactos.org/svn/reactos?rev=58684&view=rev Log: [NTDLL_APITEST] Add more tests for NtAllocateVirtualMemory and NtMapViewOfSection, make tests 64 bit compatible.
Modified: trunk/rostests/apitests/ntdll/NtAllocateVirtualMemory.c trunk/rostests/apitests/ntdll/NtMapViewOfSection.c
Modified: trunk/rostests/apitests/ntdll/NtAllocateVirtualMemory.c URL: http://svn.reactos.org/svn/reactos/trunk/rostests/apitests/ntdll/NtAllocateV... ============================================================================== --- trunk/rostests/apitests/ntdll/NtAllocateVirtualMemory.c [iso-8859-1] (original) +++ trunk/rostests/apitests/ntdll/NtAllocateVirtualMemory.c [iso-8859-1] Fri Apr 5 19:19:14 2013 @@ -207,7 +207,7 @@ SIZE_T Size;
/* Reserve memory */ - BaseAddress = UlongToPtr(Base); + BaseAddress = (PVOID)Base; Size = InSize; Status = NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress, @@ -216,7 +216,7 @@ 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 @@
_SEH2_TRY { - *(int*)UlongToPtr(BaseAddress) = 1; + *(int*)BaseAddress = 1; *(int*)UlongToPtr(0x50004080) = 1; } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) @@ -401,7 +401,7 @@ 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 @@ 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 @@ _SEH2_TRY { *(int*)UlongToPtr(0x50000000) = 1; - //*(int*)UlongToPtr(0x50002000) = 1; } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { @@ -434,6 +433,17 @@ } _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,8 +452,19 @@ 0, &Size, MEM_COMMIT, - PAGE_NOACCESS); - ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed!\n"); + 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); @@ -462,6 +483,25 @@ ok(NT_SUCCESS(Status), "NtFreeVirtualMemory failed!\n");
BaseAddress = UlongToPtr(0x50020000); + Size = 0x10000; + 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");
Modified: trunk/rostests/apitests/ntdll/NtMapViewOfSection.c URL: http://svn.reactos.org/svn/reactos/trunk/rostests/apitests/ntdll/NtMapViewOf... ============================================================================== --- trunk/rostests/apitests/ntdll/NtMapViewOfSection.c [iso-8859-1] (original) +++ trunk/rostests/apitests/ntdll/NtMapViewOfSection.c [iso-8859-1] Fri Apr 5 19:19:14 2013 @@ -191,7 +191,7 @@ 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 @@ 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 @@ /* 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,8 +324,12 @@ 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, @@ -347,18 +354,31 @@ ViewShare, 0, PAGE_READONLY); +#ifdef _M_IX86 ok(Status == STATUS_SUCCESS, "NtMapViewOfSection failed, Status 0x%lx\n", Status); - - /* Check the data */ +#else + ok(Status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH, "NtMapViewOfSection failed, Status 0x%lx\n", Status); +#endif + + /* 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 @@
/* Check the data again */ ok(*(ULONG*)ImageBase == 0x00905a4d, "Data should not be synced!\n"); - - /* Restore the original data */ + 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 header */ *(ULONG*)DataBase = 0x00905a4d; ok(*(ULONG*)DataBase == 0x00905a4d, "Header not ok\n");
+ /* Close the image mapping */ + NtUnmapViewOfSection(NtCurrentProcess(), ImageBase); + 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); - NtUnmapViewOfSection(NtCurrentProcess(), DataBase); - NtClose(ImageSectionHandle); - NtClose(DataSectionHandle); - NtClose(FileHandle); } +
START_TEST(NtMapViewOfSection) { - //Test_PageFileSection(); + Test_PageFileSection(); Test_ImageSection(); } +