2 * PROJECT: ReactOS API Tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for NtMapViewOfSection
5 * PROGRAMMERS: Timo Kreuzer
12 Test_PageFileSection(void)
16 LARGE_INTEGER MaximumSize
, SectionOffset
;
17 PVOID BaseAddress
, BaseAddress2
;
21 /* Create a page file backed section with SEC_COMMIT */
22 MaximumSize
.QuadPart
= 0x20000;
23 Status
= NtCreateSection(&SectionHandle
,
30 ok_ntstatus(Status
, STATUS_SUCCESS
);
31 if (!NT_SUCCESS(Status
))
34 /* Try to map a page at an address that is not 64k aligned */
35 BaseAddress
= (PVOID
)0x30001000;
36 SectionOffset
.QuadPart
= 0;
38 Status
= NtMapViewOfSection(SectionHandle
,
48 ok_ntstatus(Status
, STATUS_MAPPED_ALIGNMENT
);
50 /* Try to map a page with execute rights */
51 BaseAddress
= (PVOID
)0x30000000;
52 SectionOffset
.QuadPart
= 0;
54 Status
= NtMapViewOfSection(SectionHandle
,
63 PAGE_EXECUTE_READWRITE
);
64 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
66 /* Try to map 2 pages with MEM_COMMIT */
67 BaseAddress
= (PVOID
)0x30000000;
68 SectionOffset
.QuadPart
= 0;
70 Status
= NtMapViewOfSection(SectionHandle
,
80 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_9
);
82 /* Try to map 1 page, with free base address and zero bits compatible with 64k granularity */
84 SectionOffset
.QuadPart
= 0;
86 Status
= NtMapViewOfSection(SectionHandle
,
96 ok_ntstatus(Status
, STATUS_SUCCESS
);
97 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
98 ok_ntstatus(Status
, STATUS_SUCCESS
);
101 ULONG_PTR gran
= 64 * 1024;
102 ULONG_PTR ZeroBits
= 11;
104 ok_hex(gran
, 0x10000);
106 ok_hex(gran
, 0x8000000);
108 ok_hex(gran
, 0x10000);
110 ok_hex((gran
<< ZeroBits
) >> ZeroBits
, gran
);
114 /* Try to map 1 page, with free base address and zero bits incompatible with 64k granularity */
116 SectionOffset
.QuadPart
= 0;
118 Status
= NtMapViewOfSection(SectionHandle
,
128 ok_ntstatus(Status
, STATUS_NO_MEMORY
);
130 /* Try to map 1 page, with base address and zero bits being compatible */
131 BaseAddress
= (PVOID
)0x30000000;
132 SectionOffset
.QuadPart
= 0;
134 Status
= NtMapViewOfSection(SectionHandle
,
144 ok_ntstatus(Status
, STATUS_SUCCESS
);
145 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
146 ok_ntstatus(Status
, STATUS_SUCCESS
);
148 /* Try to map 1 page, with base address and zero bits being incompatible */
149 BaseAddress
= (PVOID
)0x30000000;
150 SectionOffset
.QuadPart
= 0;
152 Status
= NtMapViewOfSection(SectionHandle
,
162 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_4
);
164 /* Map 2 pages, without MEM_COMMIT */
165 BaseAddress
= (PVOID
)0x30000000;
166 SectionOffset
.QuadPart
= 0;
168 Status
= NtMapViewOfSection(SectionHandle
,
178 ok_ntstatus(Status
, STATUS_SUCCESS
);
180 /* We must be able to access the memory */
183 *(PULONG
)BaseAddress
= 1;
187 ok(FALSE
, "Got an exception\n");
191 /* Commit a page in the section */
192 BaseAddress
= (PVOID
)0x30000000;
194 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
200 ok_ntstatus(Status
, STATUS_SUCCESS
);
202 /* Try to decommit a page in the section */
203 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
207 ok_ntstatus(Status
, STATUS_UNABLE_TO_DELETE_SECTION
);
209 /* Try to commit a range larger than the section */
210 BaseAddress
= (PVOID
)0x30000000;
212 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
218 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
220 /* Try to commit a page after the section */
221 BaseAddress
= (PVOID
)0x30002000;
223 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
229 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
231 /* Try to allocate a page after the section */
232 BaseAddress
= (PVOID
)0x30002000;
234 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
238 MEM_RESERVE
| MEM_COMMIT
,
240 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
242 /* Need to go to next 64k boundary */
243 BaseAddress
= (PVOID
)0x30010000;
245 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
249 MEM_RESERVE
| MEM_COMMIT
,
251 ok_ntstatus(Status
, STATUS_SUCCESS
);
252 if (!NT_SUCCESS(Status
))
255 /* Free the allocation */
256 BaseAddress
= (PVOID
)0x30010000;
258 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
262 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed with Status %lx\n", Status
);
264 /* Try to release the section mapping with NtFreeVirtualMemory */
265 BaseAddress
= (PVOID
)0x30000000;
267 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
271 ok_ntstatus(Status
, STATUS_UNABLE_TO_DELETE_SECTION
);
273 /* Commit a page in the section */
274 BaseAddress
= (PVOID
)0x30001000;
276 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
282 ok_ntstatus(Status
, STATUS_SUCCESS
);
284 /* Try to decommit the page */
285 BaseAddress
= (PVOID
)0x30001000;
287 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
291 ok_ntstatus(Status
, STATUS_UNABLE_TO_DELETE_SECTION
);
293 BaseAddress
= UlongToPtr(0x40000000);
294 SectionOffset
.QuadPart
= 0;
296 Status
= NtMapViewOfSection(SectionHandle
,
306 ok_ntstatus(Status
, STATUS_SUCCESS
);
307 if (!NT_SUCCESS(Status
))
310 ok(BaseAddress
== UlongToPtr(0x40000000), "Invalid BaseAddress: %p\n", BaseAddress
);
312 BaseAddress
= (PVOID
)0x40080000;
313 SectionOffset
.QuadPart
= 0x10000;
315 Status
= NtMapViewOfSection(SectionHandle
,
325 ok_ntstatus(Status
, STATUS_SUCCESS
);
327 ok(BaseAddress
== (PVOID
)0x40080000, "Invalid BaseAddress: %p\n", BaseAddress
);
329 /* Commit a page in the section */
330 BaseAddress
= (PVOID
)0x40000000;
331 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
337 ok_ntstatus(Status
, STATUS_SUCCESS
);
339 /* Close the mapping */
340 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
341 ok_ntstatus(Status
, STATUS_SUCCESS
);
342 BaseAddress
= (PVOID
)0x30000000;
343 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
344 ok_ntstatus(Status
, STATUS_SUCCESS
);
345 Status
= NtClose(SectionHandle
);
346 ok_ntstatus(Status
, STATUS_SUCCESS
);
348 /* Create a page file backed section, but only reserved */
349 MaximumSize
.QuadPart
= 0x20000;
350 Status
= NtCreateSection(&SectionHandle
,
357 ok_ntstatus(Status
, STATUS_SUCCESS
);
359 /* Try to map 1 page, passing MEM_RESERVE */
361 SectionOffset
.QuadPart
= 0;
362 ViewSize
= PAGE_SIZE
;
363 Status
= NtMapViewOfSection(SectionHandle
,
373 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_9
);
375 /* Try to map 1 page using MEM_COMMIT */
377 SectionOffset
.QuadPart
= 0;
378 ViewSize
= PAGE_SIZE
;
379 Status
= NtMapViewOfSection(SectionHandle
,
389 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_9
);
391 /* Map 2 pages, but commit 1 */
393 SectionOffset
.QuadPart
= 0;
394 ViewSize
= 2 * PAGE_SIZE
;
395 Status
= NtMapViewOfSection(SectionHandle
,
405 ok_ntstatus(Status
, STATUS_SUCCESS
);
407 /* We must be able to access the 1st page */
408 Status
= STATUS_SUCCESS
;
411 *(PUCHAR
)BaseAddress
= 1;
415 Status
= _SEH2_GetExceptionCode();
418 ok_ntstatus(Status
, STATUS_SUCCESS
);
420 /* We must not be able to access the 2nd page */
421 Status
= STATUS_SUCCESS
;
424 *((PUCHAR
)BaseAddress
+ PAGE_SIZE
) = 1;
428 Status
= _SEH2_GetExceptionCode();
431 ok_ntstatus(Status
, STATUS_ACCESS_VIOLATION
);
433 /* Map the 2 pages again into a different memory location */
435 Status
= NtMapViewOfSection(SectionHandle
,
445 ok_ntstatus(Status
, STATUS_SUCCESS
);
447 /* Commit a the 2nd page in the 2nd memory location */
448 BaseAddress2
= (PUCHAR
)BaseAddress2
+ PAGE_SIZE
;
449 ViewSize
= PAGE_SIZE
;
450 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
456 ok_ntstatus(Status
, STATUS_SUCCESS
);
458 /* Try to commit again (the already committed page) */
459 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
465 ok_ntstatus(Status
, STATUS_SUCCESS
);
467 /* We must be able to access the memory in the 2nd page of the 1st memory location */
468 Status
= STATUS_SUCCESS
;
471 *((PUCHAR
)BaseAddress
+ PAGE_SIZE
) = 2;
475 Status
= _SEH2_GetExceptionCode();
478 ok_ntstatus(Status
, STATUS_SUCCESS
);
480 ok(*(PULONG
)BaseAddress2
== 2, "Value in memory was wrong\n");
482 /* Close the mapping */
483 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
484 ok_ntstatus(Status
, STATUS_SUCCESS
);
485 Status
= NtUnmapViewOfSection(NtCurrentProcess(), (PUCHAR
)BaseAddress2
- PAGE_SIZE
);
486 ok_ntstatus(Status
, STATUS_SUCCESS
);
487 Status
= NtClose(SectionHandle
);
488 ok_ntstatus(Status
, STATUS_SUCCESS
);
490 /* Try to create a 512 GB page file backed section with committed pages */
491 MaximumSize
.QuadPart
= 0x8000000000;
492 Status
= NtCreateSection(&SectionHandle
,
499 ok_ntstatus(Status
, STATUS_COMMITMENT_LIMIT
);
501 /* Try to create a huge page file backed section with PAGE_NOACCESS protection */
502 MaximumSize
.QuadPart
= 0x8000000000;
503 Status
= NtCreateSection(&SectionHandle
,
510 ok_ntstatus(Status
, STATUS_INVALID_PAGE_PROTECTION
);
512 /* Try to create a very huge page file backed section, but only reserved */
513 MaximumSize
.QuadPart
= 0x80000000000;
514 Status
= NtCreateSection(&SectionHandle
,
522 ok_ntstatus(Status
, STATUS_INSUFFICIENT_RESOURCES
);
524 /* WoW64 returns STATUS_INSUFFICIENT_RESOURCES */
525 ok((Status
== STATUS_INSUFFICIENT_RESOURCES
) || (Status
== STATUS_SECTION_TOO_BIG
),
526 "got wrong Status: 0x%lx\n", Status
);
529 /* Try to create a even huger page file backed section, but only reserved */
530 MaximumSize
.QuadPart
= 0x800000000000;
531 Status
= NtCreateSection(&SectionHandle
,
538 ok_ntstatus(Status
, STATUS_SECTION_TOO_BIG
);
540 /* Create a 8 GB page file backed section, but only reserved */
541 MaximumSize
.QuadPart
= 0x200000000;
542 Status
= NtCreateSection(&SectionHandle
,
549 ok_ntstatus(Status
, STATUS_SUCCESS
);
551 /* Pass a too large region size */
553 SectionOffset
.QuadPart
= 0;
554 ViewSize
= MAXULONG_PTR
;
555 Status
= NtMapViewOfSection(SectionHandle
,
566 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_3
);
568 /* WoW64 returns STATUS_INVALID_PARAMETER_4 */
569 ok((Status
== STATUS_INVALID_PARAMETER_4
) || (Status
== STATUS_INVALID_PARAMETER_3
),
570 "got wrong Status: 0x%lx\n", Status
);
573 /* Pass 0 region size */
575 SectionOffset
.QuadPart
= 0;
577 Status
= NtMapViewOfSection(SectionHandle
,
588 ok_ntstatus(Status
, STATUS_SUCCESS
);
589 ok(ViewSize
== 0x200000000, "wrong ViewSize: 0x%Ix\n", ViewSize
);
591 /* WoW64 returns STATUS_NO_MEMORY */
592 ok((Status
== STATUS_NO_MEMORY
) || (Status
== STATUS_INVALID_VIEW_SIZE
),
593 "got wrong Status: 0x%lx\n", Status
);
594 ok(ViewSize
== 0, "wrong ViewSize: 0x%Ix\n", ViewSize
);
597 /* Map with PAGE_NOACCESS */
599 SectionOffset
.QuadPart
= 0;
600 ViewSize
= 0x20000000;
601 Status
= NtMapViewOfSection(SectionHandle
,
611 ok_ntstatus(Status
, STATUS_SUCCESS
);
613 /* Try to change protection to read/write */
616 BaseAddress2
= BaseAddress
;
617 Status
= NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2
, &ViewSize
, PAGE_READWRITE
, &OldProtect
);
618 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
619 // Windows 2003 returns bogus
620 //ok(OldProtect == PAGE_READWRITE, "Wrong protection returned: %u\n", OldProtect);
622 /* Test read access */
623 Status
= STATUS_SUCCESS
;
626 (void)(*(volatile char*)BaseAddress2
);
628 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
630 Status
= _SEH2_GetExceptionCode();
633 ok_ntstatus(Status
, STATUS_ACCESS_VIOLATION
);
635 /* Try to change protection to read/write */
638 BaseAddress2
= BaseAddress
;
639 Status
= NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2
, &ViewSize
, PAGE_READWRITE
, &OldProtect
);
640 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
642 ok(OldProtect
== 0, "Wrong protection returned: 0x%lx\n", OldProtect
);
644 // Windows 2003 returns bogus
647 /* Try to change protection to readonly */
650 BaseAddress2
= BaseAddress
;
651 Status
= NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2
, &ViewSize
, PAGE_READONLY
, &OldProtect
);
652 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
654 //ok(OldProtect == 0, "Wrong protection returned: 0x%lx\n", OldProtect);
656 // Windows 2003 returns bogus
661 BaseAddress2
= BaseAddress
;
662 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_READONLY
);
663 ok_ntstatus(Status
, STATUS_SUCCESS
);
664 ok(BaseAddress2
== BaseAddress
, "Invalid base address: %p\n", BaseAddress2
);
666 /* Commit the page again */
668 BaseAddress2
= BaseAddress
;
669 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_READONLY
);
670 ok_ntstatus(Status
, STATUS_SUCCESS
);
671 ok(BaseAddress2
== BaseAddress
, "Invalid base address: %p\n", BaseAddress2
);
673 /* Test read access */
674 Status
= STATUS_SUCCESS
;
677 (void)(*(volatile char*)BaseAddress2
);
679 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
681 Status
= _SEH2_GetExceptionCode();
684 ok_ntstatus(Status
, STATUS_SUCCESS
);
686 /* Test write access */
687 Status
= STATUS_SUCCESS
;
690 *(char*)BaseAddress2
= 1;
692 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
694 Status
= _SEH2_GetExceptionCode();
697 ok_ntstatus(Status
, STATUS_ACCESS_VIOLATION
);
699 /* Update protection to PAGE_READWRITE */
701 BaseAddress2
= BaseAddress
;
702 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_READWRITE
);
703 ok_ntstatus(Status
, STATUS_SUCCESS
);
705 /* Test write access */
706 Status
= STATUS_SUCCESS
;
709 *(char*)BaseAddress2
= 1;
711 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
713 Status
= _SEH2_GetExceptionCode();
716 ok_ntstatus(Status
, STATUS_SUCCESS
);
718 /* Update protection to PAGE_EXECUTE_READWRITE (1 page) */
720 BaseAddress2
= BaseAddress
;
721 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
722 ok_ntstatus(Status
, STATUS_SUCCESS
);
724 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
725 ok_ntstatus(Status
, STATUS_SUCCESS
);
726 Status
= NtClose(SectionHandle
);
727 ok_ntstatus(Status
, STATUS_SUCCESS
);
731 Test_ImageSection(void)
733 UNICODE_STRING FileName
;
735 OBJECT_ATTRIBUTES FileObjectAttributes
;
736 IO_STATUS_BLOCK IoStatusBlock
;
737 WCHAR TestDllPath
[MAX_PATH
];
738 HANDLE FileHandle
, DataSectionHandle
, ImageSectionHandle
;
739 PVOID DataBase
, ImageBase
;
742 GetModuleFileNameW(NULL
, TestDllPath
, RTL_NUMBER_OF(TestDllPath
));
743 wcsrchr(TestDllPath
, L
'\\')[1] = UNICODE_NULL
;
744 StringCbCatW(TestDllPath
, sizeof(TestDllPath
), L
"testdata\\test.dll");
745 if (!RtlDosPathNameToNtPathName_U(TestDllPath
,
750 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
754 InitializeObjectAttributes(&FileObjectAttributes
,
760 Status
= NtOpenFile(&FileHandle
,
761 GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
762 &FileObjectAttributes
,
765 FILE_SYNCHRONOUS_IO_NONALERT
);
766 ok_ntstatus(Status
, STATUS_SUCCESS
);
767 if (!NT_SUCCESS(Status
))
769 skip("Failed to open file\n");
773 /* Create a data section with write access */
774 Status
= NtCreateSection(&DataSectionHandle
,
775 SECTION_ALL_ACCESS
, // DesiredAccess
776 NULL
, // ObjectAttributes
778 PAGE_READWRITE
, // SectionPageProtection
779 SEC_COMMIT
, // AllocationAttributes
781 ok_ntstatus(Status
, STATUS_SUCCESS
);
782 if (!NT_SUCCESS(Status
))
784 skip("Failed to create data section\n");
789 /* Map the data section as flat mapping */
792 Status
= NtMapViewOfSection(DataSectionHandle
,
802 ok_ntstatus(Status
, STATUS_SUCCESS
);
803 //ok(ViewSize == 0x3f95cc48, "ViewSize wrong: 0x%lx\n");
804 if (!NT_SUCCESS(Status
))
806 skip("Failed to map view of data section\n");
807 NtClose(DataSectionHandle
);
812 /* Check the original data */
813 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
815 /* Modify the PE header (but do not flush!) */
816 *(ULONG
*)DataBase
= 0xdeadbabe;
817 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
819 /* Modify data in the .data section (but do not flush!) */
820 ok(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800) == 0x12345678,
821 "Data in .data section invalid: 0x%lx!\n", *(ULONG
*)((PUCHAR
)DataBase
+ 0x800));
822 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x87654321;
824 /* Now try to create an image section (should fail) */
825 Status
= NtCreateSection(&ImageSectionHandle
,
826 SECTION_ALL_ACCESS
, // DesiredAccess
827 NULL
, // ObjectAttributes
829 PAGE_READWRITE
, // SectionPageProtection
830 SEC_IMAGE
, // AllocationAttributes
832 ok_ntstatus(Status
, STATUS_INVALID_IMAGE_NOT_MZ
);
833 if (NT_SUCCESS(Status
)) NtClose(ImageSectionHandle
);
835 /* Restore the original header */
836 *(ULONG
*)DataBase
= 0x00905a4d;
838 /* Modify data in the .data section (but do not flush!) */
839 ok_hex(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800), 0x87654321);
840 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xdeadbabe;
842 /* Try to create an image section again */
843 Status
= NtCreateSection(&ImageSectionHandle
,
844 SECTION_ALL_ACCESS
, // DesiredAccess
845 NULL
, // ObjectAttributes
847 PAGE_READWRITE
, // SectionPageProtection
848 SEC_IMAGE
, // AllocationAttributes
850 ok_ntstatus(Status
, STATUS_SUCCESS
);
851 if (!NT_SUCCESS(Status
))
853 skip("Failed to create image section\n");
854 NtClose(DataSectionHandle
);
859 /* Map the image section */
862 Status
= NtMapViewOfSection(ImageSectionHandle
,
867 NULL
, // SectionOffset
873 ok_ntstatus(Status
, STATUS_SUCCESS
);
875 ok_ntstatus(Status
, STATUS_IMAGE_MACHINE_TYPE_MISMATCH
);
877 if (!NT_SUCCESS(Status
))
879 skip("Failed to map view of image section\n");
880 NtClose(ImageSectionHandle
);
881 NtClose(DataSectionHandle
);
886 /* Check the header */
887 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
888 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Header not ok\n");
890 /* Check the data section. Either of these can be present! */
891 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
892 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
893 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
895 /* Now modify the data again */
896 *(ULONG
*)DataBase
= 0xdeadbabe;
897 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xf00dada;
900 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
901 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
902 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
903 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
904 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
908 Status
= NtFlushVirtualMemory(NtCurrentProcess(),
912 ok_ntstatus(Status
, STATUS_SUCCESS
);
914 /* Check the data again */
915 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
916 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
917 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
918 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
920 /* Restore the original header */
921 *(ULONG
*)DataBase
= 0x00905a4d;
922 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
924 /* Close the image mapping */
925 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
926 NtClose(ImageSectionHandle
);
928 /* Create an image section again */
929 Status
= NtCreateSection(&ImageSectionHandle
,
930 SECTION_ALL_ACCESS
, // DesiredAccess
931 NULL
, // ObjectAttributes
933 PAGE_READWRITE
, // SectionPageProtection
934 SEC_IMAGE
, // AllocationAttributes
936 ok_ntstatus(Status
, STATUS_SUCCESS
);
937 if (!NT_SUCCESS(Status
))
939 skip("Failed to create image section\n");
940 NtClose(DataSectionHandle
);
945 /* Map the image section again */
948 Status
= NtMapViewOfSection(ImageSectionHandle
,
959 ok_ntstatus(Status
, STATUS_SUCCESS
);
961 ok_ntstatus(Status
, STATUS_IMAGE_MACHINE_TYPE_MISMATCH
);
963 if (!NT_SUCCESS(Status
))
965 skip("Failed to map view of image section\n");
966 NtClose(ImageSectionHandle
);
967 NtClose(DataSectionHandle
);
972 // This one doesn't always work, needs investigation
973 /* Check the .data section again */
974 //ok(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0xf00dada,
975 // "Data should be synced: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
977 /* Restore the original data */
978 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x12345678;
980 /* Close the data mapping */
981 NtUnmapViewOfSection(NtCurrentProcess(), DataBase
);
983 NtClose(DataSectionHandle
);
985 /* Try to allocate memory inside the image mapping */
986 DataBase
= (PUCHAR
)ImageBase
+ 0x20000;
988 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &DataBase
, 0, &ViewSize
, MEM_RESERVE
, PAGE_NOACCESS
);
989 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
993 NtClose(ImageSectionHandle
);
994 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
998 Test_ImageSection2(void)
1000 UNICODE_STRING FileName
;
1002 OBJECT_ATTRIBUTES FileObjectAttributes
;
1003 IO_STATUS_BLOCK IoStatusBlock
;
1004 HANDLE FileHandle
, ImageSectionHandle
;
1005 PVOID ImageBase
, BaseAddress
;
1007 LARGE_INTEGER MaximumSize
, SectionOffset
;
1009 if (!RtlDosPathNameToNtPathName_U(L
"testdata\\nvoglv32.dll",
1014 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
1018 InitializeObjectAttributes(&FileObjectAttributes
,
1024 Status
= NtOpenFile(&FileHandle
,
1025 GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
1026 &FileObjectAttributes
,
1029 FILE_SYNCHRONOUS_IO_NONALERT
);
1030 ok_ntstatus(Status
, STATUS_SUCCESS
);
1031 printf("Opened file with handle %p\n", FileHandle
);
1033 /* Create a data section with write access */
1034 MaximumSize
.QuadPart
= 0x20000;
1035 Status
= NtCreateSection(&ImageSectionHandle
,
1036 SECTION_ALL_ACCESS
, // DesiredAccess
1037 NULL
, // ObjectAttributes
1038 &MaximumSize
, // MaximumSize
1039 PAGE_READWRITE
, // SectionPageProtection
1040 SEC_IMAGE
, // AllocationAttributes
1042 ok_ntstatus(Status
, STATUS_SUCCESS
);
1044 printf("Created image section with handle %p\n", ImageSectionHandle
);
1047 /* Map the image section */
1050 SectionOffset
.QuadPart
= 0x00000;
1051 Status
= NtMapViewOfSection(ImageSectionHandle
,
1061 ok_ntstatus(Status
, STATUS_SUCCESS
);
1063 printf("Mapped image section at %p, value in text section: %lx\n",
1064 ImageBase
, *((ULONG
*)((PCHAR
)ImageBase
+ 0x1196)));
1067 /* Try to allocate a page after the section */
1068 BaseAddress
= (PUCHAR
)ImageBase
+ 0x10000;
1070 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
1074 MEM_RESERVE
| MEM_COMMIT
,
1076 printf("allocation status: %lx\n", Status
);
1081 // doesn't work with WoW64!
1083 Test_BasedSection(void)
1086 HANDLE SectionHandle1
, SectionHandle2
;
1087 LARGE_INTEGER MaximumSize
, SectionOffset
;
1088 PVOID BaseAddress1
, BaseAddress2
;
1091 /* Create a based section with SEC_COMMIT */
1092 MaximumSize
.QuadPart
= 0x1000;
1093 Status
= NtCreateSection(&SectionHandle1
,
1098 SEC_COMMIT
| SEC_BASED
,
1100 ok_ntstatus(Status
, STATUS_SUCCESS
);
1102 /* Map the 1st section */
1103 BaseAddress1
= NULL
;
1104 SectionOffset
.QuadPart
= 0;
1106 Status
= NtMapViewOfSection(SectionHandle1
,
1117 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
1119 ok_ntstatus(Status
, STATUS_SUCCESS
);
1122 /* Create a 2nd based section with SEC_COMMIT */
1123 MaximumSize
.QuadPart
= 0x1000;
1124 Status
= NtCreateSection(&SectionHandle2
,
1129 SEC_COMMIT
| SEC_BASED
,
1131 ok_ntstatus(Status
, STATUS_SUCCESS
);//
1133 /* Map the 2nd section */
1134 BaseAddress2
= NULL
;
1135 SectionOffset
.QuadPart
= 0;
1137 Status
= NtMapViewOfSection(SectionHandle2
,
1148 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
1150 ok_ntstatus(Status
, STATUS_SUCCESS
);
1151 ok((ULONG_PTR
)BaseAddress2
< (ULONG_PTR
)BaseAddress1
,
1152 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1
, BaseAddress2
);
1153 ok(((ULONG_PTR
)BaseAddress1
- (ULONG_PTR
)BaseAddress2
) == 0x10000,
1154 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1
, BaseAddress2
);
1158 #define BYTES4(x) x, x, x, x
1159 #define BYTES8(x) BYTES4(x), BYTES4(x)
1160 #define BYTES16(x) BYTES8(x), BYTES8(x)
1161 #define BYTES32(x) BYTES16(x), BYTES16(x)
1162 #define BYTES64(x) BYTES32(x), BYTES32(x)
1163 #define BYTES128(x) BYTES64(x), BYTES64(x)
1164 #define BYTES256(x) BYTES128(x), BYTES128(x)
1165 #define BYTES512(x) BYTES256(x), BYTES256(x)
1166 #define BYTES1024(x) BYTES512(x), BYTES512(x)
1168 static struct _MY_IMAGE_FILE
1170 IMAGE_DOS_HEADER doshdr
;
1172 IMAGE_NT_HEADERS32 nthdrs
;
1173 IMAGE_SECTION_HEADER text_header
;
1174 IMAGE_SECTION_HEADER rossym_header
;
1175 IMAGE_SECTION_HEADER rsrc_header
;
1176 IMAGE_SECTION_HEADER clc_header
;
1178 BYTE text_data
[0x400];
1179 BYTE rossym_data
[0x400];
1180 BYTE rsrc_data
[0x400];
1181 BYTE clc_data
[0x1000];
1184 /* IMAGE_DOS_HEADER */
1186 IMAGE_DOS_SIGNATURE
, 144, 3, 0, 4, 0, 0xFFFF, 0, 0xB8, 0, 0, 0, 0x40,
1187 0, { 0 }, 0, 0, { 0 }, 0x80
1189 /* binary to print "This program cannot be run in DOS mode." */
1191 0x1F0E, 0x0EBA, 0xB400, 0xCD09, 0xB821, 0x4C01, 0x21CD, 0x6854, 0x7369,
1192 0x7020, 0x6F72, 0x7267, 0x6D61, 0x6320, 0x6E61, 0x6F6E, 0x2074, 0x6562,
1193 0x7220, 0x6E75, 0x6920, 0x206E, 0x4F44, 0x2053, 0x6F6D, 0x6564, 0x0D2E,
1194 0x0A0D, 0x0024, 0x0000, 0x0000, 0x0000
1196 /* IMAGE_NT_HEADERS32 */
1198 IMAGE_NT_SIGNATURE
, /* Signature */
1199 /* IMAGE_FILE_HEADER */
1201 IMAGE_FILE_MACHINE_I386
, /* Machine */
1202 4, /* NumberOfSections */
1203 0x47EFDF09, /* TimeDateStamp */
1204 0, /* PointerToSymbolTable */
1205 0, /* NumberOfSymbols */
1206 0xE0, /* SizeOfOptionalHeader */
1207 IMAGE_FILE_32BIT_MACHINE
| IMAGE_FILE_LOCAL_SYMS_STRIPPED
|
1208 IMAGE_FILE_LINE_NUMS_STRIPPED
| IMAGE_FILE_EXECUTABLE_IMAGE
|
1209 IMAGE_FILE_DLL
, /* Characteristics */
1211 /* IMAGE_OPTIONAL_HEADER32 */
1213 IMAGE_NT_OPTIONAL_HDR32_MAGIC
, /* Magic */
1214 8, /* MajorLinkerVersion */
1215 0, /* MinorLinkerVersion */
1216 0x400, /* SizeOfCode */
1217 0x000, /* SizeOfInitializedData */
1218 0, /* SizeOfUninitializedData */
1219 0x2000, /* AddressOfEntryPoint */
1220 0x2000, /* BaseOfCode */
1221 0x0000, /* BaseOfData */
1222 0x400000, /* ImageBase */
1223 0x2000, /* SectionAlignment */
1224 0x200, /* FileAlignment */
1225 4, /* MajorOperatingSystemVersion */
1226 0, /* MinorOperatingSystemVersion */
1227 0, /* MajorImageVersion */
1228 0, /* MinorImageVersion */
1229 4, /* MajorSubsystemVersion */
1230 0, /* MinorSubsystemVersion */
1231 0, /* Win32VersionValue */
1232 0xa000, /* SizeOfImage */
1233 0x400, /* SizeOfHeaders */
1235 IMAGE_SUBSYSTEM_WINDOWS_CUI
, /* Subsystem */
1236 IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
|
1237 IMAGE_DLLCHARACTERISTICS_NO_SEH
|
1238 IMAGE_DLLCHARACTERISTICS_NX_COMPAT
, /* DllCharacteristics */
1239 0x100000, /* SizeOfStackReserve */
1240 0x1000, /* SizeOfStackCommit */
1241 0x100000, /* SizeOfHeapReserve */
1242 0x1000, /* SizeOfHeapCommit */
1243 0, /* LoaderFlags */
1244 0x10, /* NumberOfRvaAndSizes */
1245 /* IMAGE_DATA_DIRECTORY */
1247 { 0 }, /* Export Table */
1248 { 0 }, /* Import Table */
1249 { 0 }, /* Resource Table */
1250 { 0 }, /* Exception Table */
1251 { 0 }, /* Certificate Table */
1252 { 0 }, /* Base Relocation Table */
1254 { 0 }, /* Copyright */
1255 { 0 }, /* Global Ptr */
1256 { 0 }, /* TLS Table */
1257 { 0 }, /* Load Config Table */
1258 { 0 }, /* Bound Import */
1260 { 0 }, /* Delay Import Descriptor */
1261 { 0 }, /* CLI Header */
1262 { 0 } /* Reserved */
1266 /* IMAGE_SECTION_HEADER */
1269 { 0x394 }, /* Misc.VirtualSize */
1270 0x2000, /* VirtualAddress */
1271 0x400, /* SizeOfRawData */
1272 0x400, /* PointerToRawData */
1273 0, /* PointerToRelocations */
1274 0, /* PointerToLinenumbers */
1275 0, /* NumberOfRelocations */
1276 0, /* NumberOfLinenumbers */
1277 IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_EXECUTE
|
1278 IMAGE_SCN_CNT_CODE
, /* Characteristics */
1280 /* IMAGE_SECTION_HEADER */
1282 ".rossym", /* Name */
1283 { 0x100 }, /* Misc.VirtualSize */
1284 0x4000, /* VirtualAddress */
1285 0x400, /* SizeOfRawData */
1286 0x800, /* PointerToRawData */
1287 0, /* PointerToRelocations */
1288 0, /* PointerToLinenumbers */
1289 0, /* NumberOfRelocations */
1290 0, /* NumberOfLinenumbers */
1292 IMAGE_SCN_MEM_READ
| IMAGE_SCN_TYPE_NOLOAD
, /* Characteristics */
1294 /* IMAGE_SECTION_HEADER */
1297 { 0x100 }, /* Misc.VirtualSize */
1298 0x6000, /* VirtualAddress */
1299 0x400, /* SizeOfRawData */
1300 0xC00, /* PointerToRawData */
1301 0, /* PointerToRelocations */
1302 0, /* PointerToLinenumbers */
1303 0, /* NumberOfRelocations */
1304 0, /* NumberOfLinenumbers */
1305 IMAGE_SCN_MEM_READ
|
1306 IMAGE_SCN_CNT_INITIALIZED_DATA
, /* Characteristics */
1308 /* IMAGE_SECTION_HEADER */
1311 { 0x2000 }, /* Misc.VirtualSize */
1312 0x8000, /* VirtualAddress */
1313 0x1000, /* SizeOfRawData */
1314 0x1000, /* PointerToRawData */
1315 0, /* PointerToRelocations */
1316 0, /* PointerToLinenumbers */
1317 0, /* NumberOfRelocations */
1318 0, /* NumberOfLinenumbers */
1320 IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_EXECUTE
|
1321 IMAGE_SCN_CNT_UNINITIALIZED_DATA
| IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_CNT_CODE
, /* Characteristics */
1329 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
1361 C_ASSERT(FIELD_OFFSET(struct _MY_IMAGE_FILE
, text_data
) == 0x400);
1362 C_ASSERT(FIELD_OFFSET(struct _MY_IMAGE_FILE
, rossym_data
) == 0x800);
1363 C_ASSERT(FIELD_OFFSET(struct _MY_IMAGE_FILE
, rsrc_data
) == 0xc00);
1364 C_ASSERT(FIELD_OFFSET(struct _MY_IMAGE_FILE
, clc_data
) == 0x1000);
1368 Test_SectionContents(BOOL Relocate
)
1371 WCHAR TempPath
[MAX_PATH
];
1372 WCHAR FileName
[MAX_PATH
];
1374 HANDLE SectionHandle
;
1375 LARGE_INTEGER SectionOffset
;
1382 Length
= GetTempPathW(MAX_PATH
, TempPath
);
1383 ok(Length
!= 0, "GetTempPathW failed with %lu\n", GetLastError());
1384 Length
= GetTempFileNameW(TempPath
, L
"nta", 0, FileName
);
1385 ok(Length
!= 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1386 Handle
= CreateFileW(FileName
,
1393 if (Handle
== INVALID_HANDLE_VALUE
)
1395 skip("Failed to create temp file %ls, error %lu\n", FileName
, GetLastError());
1400 ok((ULONG_PTR
)GetModuleHandle(NULL
) <= 0x80000000, "Module at %p\n", GetModuleHandle(NULL
));
1401 ImageFile
.nthdrs
.OptionalHeader
.ImageBase
= (ULONG
)(ULONG_PTR
)GetModuleHandle(NULL
);
1405 ImageFile
.nthdrs
.OptionalHeader
.ImageBase
= 0xe400000;
1408 Success
= WriteFile(Handle
,
1413 ok(Success
== TRUE
, "WriteFile failed with %lu\n", GetLastError());
1414 ok(Written
== sizeof(ImageFile
), "WriteFile wrote %lu bytes\n", Written
);
1416 Status
= NtCreateSection(&SectionHandle
,
1420 PAGE_EXECUTE_READWRITE
,
1423 ok_ntstatus(Status
, STATUS_SUCCESS
);
1425 if (NT_SUCCESS(Status
))
1427 /* Map the section with */
1429 SectionOffset
.QuadPart
= 0;
1431 Status
= NtMapViewOfSection(SectionHandle
,
1442 ok_ntstatus(Status
, STATUS_IMAGE_NOT_AT_BASE
);
1444 ok_ntstatus(Status
, STATUS_SUCCESS
);
1445 if (NT_SUCCESS(Status
))
1447 PUCHAR Bytes
= BaseAddress
;
1448 #define TEST_BYTE(n, v) StartSeh() ok_hex(Bytes[n], v); EndSeh(STATUS_SUCCESS);
1449 #define TEST_WRITE(n) StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_SUCCESS);
1450 #define TEST_NOWRITE(n) StartSeh() *(volatile UCHAR *)&Bytes[n] = Bytes[n]; EndSeh(STATUS_ACCESS_VIOLATION);
1451 TEST_NOWRITE(0x2000);
1452 TEST_BYTE(0x2000, 0xc3);
1453 TEST_BYTE(0x2001, 0x00);
1454 TEST_NOWRITE(0x4000);
1455 TEST_BYTE(0x4000, 0x01);
1456 TEST_BYTE(0x4001, 0x23);
1457 TEST_BYTE(0x4007, 0xef);
1458 TEST_BYTE(0x4008, 0xaa);
1459 TEST_BYTE(0x4010, 0xbb);
1460 TEST_BYTE(0x4020, 0xcc);
1461 TEST_BYTE(0x4040, 0xdd);
1462 TEST_BYTE(0x4080, 0xee);
1463 TEST_BYTE(0x40c0, 0xff);
1464 TEST_BYTE(0x40ff, 0xff);
1465 TEST_BYTE(0x4100, 0x00);
1466 TEST_BYTE(0x41ff, 0x00);
1467 TEST_NOWRITE(0x6000);
1468 TEST_BYTE(0x6000, 0xee);
1469 TEST_BYTE(0x60ff, 0x55);
1470 TEST_BYTE(0x6100, 0xee);
1471 TEST_BYTE(0x63ff, 0xdd);
1472 TEST_BYTE(0x6400, 0x00);
1474 TEST_BYTE(0x8000, 0x11);
1475 TEST_BYTE(0x8400, 0x33);
1476 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
1477 ok_ntstatus(Status
, STATUS_SUCCESS
);
1479 Status
= NtClose(SectionHandle
);
1480 ok_ntstatus(Status
, STATUS_SUCCESS
);
1483 CloseHandle(Handle
);
1484 DeleteFileW(FileName
);
1488 Test_EmptyFile(VOID
)
1491 WCHAR TempPath
[MAX_PATH
];
1492 WCHAR FileName
[MAX_PATH
];
1494 HANDLE SectionHandle
;
1497 Length
= GetTempPathW(MAX_PATH
, TempPath
);
1498 ok(Length
!= 0, "GetTempPathW failed with %lu\n", GetLastError());
1499 Length
= GetTempFileNameW(TempPath
, L
"nta", 0, FileName
);
1500 ok(Length
!= 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1501 Handle
= CreateFileW(FileName
,
1508 if (Handle
== INVALID_HANDLE_VALUE
)
1510 skip("Failed to create temp file %ls, error %lu\n", FileName
, GetLastError());
1514 Status
= NtCreateSection(&SectionHandle
,
1515 STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1516 0, 0, PAGE_READONLY
, SEC_COMMIT
, Handle
);
1517 ok_ntstatus(Status
, STATUS_MAPPED_FILE_SIZE_ZERO
);
1519 if (NT_SUCCESS(Status
))
1520 NtClose(SectionHandle
);
1522 Status
= NtCreateSection(&SectionHandle
,
1523 STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1524 0, 0, PAGE_READONLY
, SEC_IMAGE
, Handle
);
1525 ok_ntstatus(Status
, STATUS_INVALID_FILE_FOR_SECTION
);
1527 if (NT_SUCCESS(Status
))
1528 NtClose(SectionHandle
);
1530 CloseHandle(Handle
);
1531 DeleteFileW(FileName
);
1538 WCHAR TempPath
[MAX_PATH
];
1539 WCHAR FileName
[MAX_PATH
];
1541 SIZE_T ViewSize
= 0;
1543 HANDLE SectionHandle
;
1547 DWORD Written
, Error
;
1550 Length
= GetTempPathW(MAX_PATH
, TempPath
);
1551 ok(Length
!= 0, "GetTempPathW failed with %lu\n", GetLastError());
1552 Length
= GetTempFileNameW(TempPath
, L
"nta", 0, FileName
);
1553 ok(Length
!= 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1554 Handle
= CreateFileW(FileName
, FILE_ALL_ACCESS
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
1556 Success
= WriteFile(Handle
, "TESTDATA", 8, &Written
, NULL
);
1557 ok(Success
== TRUE
, "WriteFile failed with %lu\n", GetLastError());
1558 ok(Written
== 8, "WriteFile wrote %lu bytes\n", Written
);
1560 Written
= SetFilePointer(Handle
, 6, NULL
, FILE_BEGIN
);
1561 ok(Written
== 6, "SetFilePointer returned %lu bytes\n", Written
);
1562 Success
= SetEndOfFile(Handle
);
1563 ok(Success
== TRUE
, "SetEndOfFile failed with %lu\n", GetLastError());
1565 Status
= NtCreateSection(&SectionHandle
,
1566 STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1567 0, 0, PAGE_READONLY
, SEC_COMMIT
, Handle
);
1568 ok_ntstatus(Status
, STATUS_SUCCESS
);
1571 Status
= NtMapViewOfSection(SectionHandle
, NtCurrentProcess(), &BaseAddress
, 0,
1572 0, 0, &ViewSize
, ViewShare
, 0, PAGE_READONLY
);
1573 ok_ntstatus(Status
, STATUS_SUCCESS
);
1577 // First we test data that was truncated even before the file mapping was opened
1578 Length
= strlen((char*)BaseAddress
);
1579 ok(Length
== 6, "Old data was not properly erased! (Length=%lu)\n", Length
);
1582 // Now we truncate the file on disk some more
1583 Written
= SetFilePointer(Handle
, 4, NULL
, FILE_BEGIN
);
1584 ok(Written
== 4, "SetFilePointer returned %lu bytes\n", Written
);
1585 Success
= SetEndOfFile(Handle
);
1586 Error
= GetLastError();
1587 ok(Success
== FALSE
, "SetEndOfFile succeeded\n");
1588 ok(Error
== ERROR_USER_MAPPED_FILE
, "SetEndOfFile did not set error to ERROR_USER_MAPPED_FILE (%lu)\n", Error
);
1592 Length
= strlen((char*)BaseAddress
);
1593 ok(Length
== 6, "Length should not have changed! (Length=%lu)\n", Length
);
1596 // Unmap and set the end shorter.
1597 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
1598 ok_ntstatus(Status
, STATUS_SUCCESS
);
1599 Success
= CloseHandle(SectionHandle
);
1600 ok(Success
== TRUE
, "CloseHandle failed with %lu\n", GetLastError());
1602 Written
= SetFilePointer(Handle
, 4, NULL
, FILE_BEGIN
);
1603 ok(Written
== 4, "SetFilePointer returned %lu bytes\n", Written
);
1604 Success
= SetEndOfFile(Handle
);
1605 ok(Success
== TRUE
, "SetEndOfFile failed with %lu\n", GetLastError());
1607 Status
= NtCreateSection(&SectionHandle
,
1608 STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1609 0, 0, PAGE_READONLY
, SEC_COMMIT
, Handle
);
1610 ok_ntstatus(Status
, STATUS_SUCCESS
);
1613 Status
= NtMapViewOfSection(SectionHandle
, NtCurrentProcess(), &BaseAddress
, 0,
1614 0, 0, &ViewSize
, ViewShare
, 0, PAGE_READONLY
);
1615 ok_ntstatus(Status
, STATUS_SUCCESS
);
1618 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
1619 ok_ntstatus(Status
, STATUS_SUCCESS
);
1620 Success
= CloseHandle(SectionHandle
);
1621 ok(Success
== TRUE
, "CloseHandle failed with %lu\n", GetLastError());
1622 Success
= CloseHandle(Handle
);
1623 ok(Success
== TRUE
, "CloseHandle failed with %lu\n", GetLastError());
1625 Success
= DeleteFileW(FileName
);
1626 ok(Success
== TRUE
, "DeleteFileW failed with %lu\n", GetLastError());
1629 START_TEST(NtMapViewOfSection
)
1631 Test_PageFileSection();
1632 Test_ImageSection();
1633 Test_BasedSection();
1634 Test_SectionContents(FALSE
);
1635 Test_SectionContents(TRUE
);