SIZE_T Size;
/* Reserve memory */
- BaseAddress = UlongToPtr(Base);
+ BaseAddress = (PVOID)Base;
Size = InSize;
Status = NtAllocateVirtualMemory(NtCurrentProcess(),
&BaseAddress,
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");
_SEH2_TRY
{
- *(int*)UlongToPtr(BaseAddress) = 1;
+ *(int*)BaseAddress = 1;
*(int*)UlongToPtr(0x50004080) = 1;
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
PAGE_NOACCESS);
ok(!NT_SUCCESS(Status), "NtAllocateVirtualMemory should fail!\n");
- /* Allocate a page */
+ /* Commit a page */
BaseAddress = UlongToPtr(0x50000000);
Size = 0x1000;
Status = NtAllocateVirtualMemory(NtCurrentProcess(),
PAGE_READWRITE);
ok(NT_SUCCESS(Status), "NtAllocateVirtualMemory failed!\n");
- /* Allocate another page */
+ /* Commit another page */
BaseAddress = UlongToPtr(0x50002000);
Size = 0x1000;
Status = NtAllocateVirtualMemory(NtCurrentProcess(),
_SEH2_TRY
{
*(int*)UlongToPtr(0x50000000) = 1;
- //*(int*)UlongToPtr(0x50002000) = 1;
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
}
_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;
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;
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
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,
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;
/* 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
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
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;
/* 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();
}
+