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
11 #define WIN32_NO_STATUS
12 #include <ndk/ntndk.h>
15 Test_PageFileSection(void)
19 LARGE_INTEGER MaximumSize
, SectionOffset
;
20 PVOID BaseAddress
, BaseAddress2
;
24 /* Create a page file backed section with SEC_COMMIT */
25 MaximumSize
.QuadPart
= 0x20000;
26 Status
= NtCreateSection(&SectionHandle
,
33 ok_ntstatus(Status
, STATUS_SUCCESS
);
34 if (!NT_SUCCESS(Status
))
37 /* Try to map a page at an address that is not 64k aligned */
38 BaseAddress
= (PVOID
)0x30001000;
39 SectionOffset
.QuadPart
= 0;
41 Status
= NtMapViewOfSection(SectionHandle
,
51 ok_ntstatus(Status
, STATUS_MAPPED_ALIGNMENT
);
53 /* Try to map a page with execute rights */
54 BaseAddress
= (PVOID
)0x30000000;
55 SectionOffset
.QuadPart
= 0;
57 Status
= NtMapViewOfSection(SectionHandle
,
66 PAGE_EXECUTE_READWRITE
);
67 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
69 /* Try to map 2 pages with MEM_COMMIT */
70 BaseAddress
= (PVOID
)0x30000000;
71 SectionOffset
.QuadPart
= 0;
73 Status
= NtMapViewOfSection(SectionHandle
,
83 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_9
);
85 /* Try to map 1 page, with free base address and zero bits compatible with 64k granularity */
87 SectionOffset
.QuadPart
= 0;
89 Status
= NtMapViewOfSection(SectionHandle
,
99 ok_ntstatus(Status
, STATUS_SUCCESS
);
100 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
101 ok_ntstatus(Status
, STATUS_SUCCESS
);
104 ULONG_PTR gran
= 64 * 1024;
105 ULONG_PTR ZeroBits
= 11;
107 ok_hex(gran
, 0x10000);
109 ok_hex(gran
, 0x8000000);
111 ok_hex(gran
, 0x10000);
113 ok_hex((gran
<< ZeroBits
) >> ZeroBits
, gran
);
117 /* Try to map 1 page, with free base address and zero bits incompatible with 64k granularity */
119 SectionOffset
.QuadPart
= 0;
121 Status
= NtMapViewOfSection(SectionHandle
,
131 ok_ntstatus(Status
, STATUS_NO_MEMORY
);
133 /* Try to map 1 page, with base address and zero bits being compatible */
134 BaseAddress
= (PVOID
)0x30000000;
135 SectionOffset
.QuadPart
= 0;
137 Status
= NtMapViewOfSection(SectionHandle
,
147 ok_ntstatus(Status
, STATUS_SUCCESS
);
148 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
149 ok_ntstatus(Status
, STATUS_SUCCESS
);
151 /* Try to map 1 page, with base address and zero bits being incompatible */
152 BaseAddress
= (PVOID
)0x30000000;
153 SectionOffset
.QuadPart
= 0;
155 Status
= NtMapViewOfSection(SectionHandle
,
165 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_4
);
167 /* Map 2 pages, without MEM_COMMIT */
168 BaseAddress
= (PVOID
)0x30000000;
169 SectionOffset
.QuadPart
= 0;
171 Status
= NtMapViewOfSection(SectionHandle
,
181 ok_ntstatus(Status
, STATUS_SUCCESS
);
183 /* We must be able to access the memory */
186 *(PULONG
)BaseAddress
= 1;
190 ok(FALSE
, "Got an exception\n");
194 /* Commit a page in the section */
195 BaseAddress
= (PVOID
)0x30000000;
197 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
203 ok_ntstatus(Status
, STATUS_SUCCESS
);
205 /* Try to decommit a page in the section */
206 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
210 ok_ntstatus(Status
, STATUS_UNABLE_TO_DELETE_SECTION
);
212 /* Try to commit a range larger than the section */
213 BaseAddress
= (PVOID
)0x30000000;
215 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
221 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
223 /* Try to commit a page after the section */
224 BaseAddress
= (PVOID
)0x30002000;
226 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
232 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
234 /* Try to allocate a page after the section */
235 BaseAddress
= (PVOID
)0x30002000;
237 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
241 MEM_RESERVE
| MEM_COMMIT
,
243 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
245 /* Need to go to next 64k boundary */
246 BaseAddress
= (PVOID
)0x30010000;
248 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
252 MEM_RESERVE
| MEM_COMMIT
,
254 ok_ntstatus(Status
, STATUS_SUCCESS
);
255 if (!NT_SUCCESS(Status
))
258 /* Free the allocation */
259 BaseAddress
= (PVOID
)0x30010000;
261 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
265 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed with Status %lx\n", Status
);
267 /* Try to release the section mapping with NtFreeVirtualMemory */
268 BaseAddress
= (PVOID
)0x30000000;
270 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
274 ok_ntstatus(Status
, STATUS_UNABLE_TO_DELETE_SECTION
);
276 /* Commit a page in the section */
277 BaseAddress
= (PVOID
)0x30001000;
279 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
285 ok_ntstatus(Status
, STATUS_SUCCESS
);
287 /* Try to decommit the page */
288 BaseAddress
= (PVOID
)0x30001000;
290 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
294 ok_ntstatus(Status
, STATUS_UNABLE_TO_DELETE_SECTION
);
296 BaseAddress
= UlongToPtr(0x40000000);
297 SectionOffset
.QuadPart
= 0;
299 Status
= NtMapViewOfSection(SectionHandle
,
309 ok_ntstatus(Status
, STATUS_SUCCESS
);
310 if (!NT_SUCCESS(Status
))
313 ok(BaseAddress
== UlongToPtr(0x40000000), "Invalid BaseAddress: %p\n", BaseAddress
);
315 BaseAddress
= (PVOID
)0x40080000;
316 SectionOffset
.QuadPart
= 0x10000;
318 Status
= NtMapViewOfSection(SectionHandle
,
328 ok_ntstatus(Status
, STATUS_SUCCESS
);
330 ok(BaseAddress
== (PVOID
)0x40080000, "Invalid BaseAddress: %p\n", BaseAddress
);
332 /* Commit a page in the section */
333 BaseAddress
= (PVOID
)0x40000000;
334 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
340 ok_ntstatus(Status
, STATUS_SUCCESS
);
342 /* Close the mapping */
343 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
344 ok_ntstatus(Status
, STATUS_SUCCESS
);
345 BaseAddress
= (PVOID
)0x30000000;
346 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
347 ok_ntstatus(Status
, STATUS_SUCCESS
);
348 Status
= NtClose(SectionHandle
);
349 ok_ntstatus(Status
, STATUS_SUCCESS
);
351 /* Create a page file backed section, but only reserved */
352 MaximumSize
.QuadPart
= 0x20000;
353 Status
= NtCreateSection(&SectionHandle
,
360 ok_ntstatus(Status
, STATUS_SUCCESS
);
362 /* Try to map 1 page, passing MEM_RESERVE */
364 SectionOffset
.QuadPart
= 0;
365 ViewSize
= PAGE_SIZE
;
366 Status
= NtMapViewOfSection(SectionHandle
,
376 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_9
);
378 /* Try to map 1 page using MEM_COMMIT */
380 SectionOffset
.QuadPart
= 0;
381 ViewSize
= PAGE_SIZE
;
382 Status
= NtMapViewOfSection(SectionHandle
,
392 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_9
);
394 /* Map 2 pages, but commit 1 */
396 SectionOffset
.QuadPart
= 0;
397 ViewSize
= 2 * PAGE_SIZE
;
398 Status
= NtMapViewOfSection(SectionHandle
,
408 ok_ntstatus(Status
, STATUS_SUCCESS
);
410 /* We must be able to access the 1st page */
411 Status
= STATUS_SUCCESS
;
414 *(PUCHAR
)BaseAddress
= 1;
418 Status
= _SEH2_GetExceptionCode();
421 ok_ntstatus(Status
, STATUS_SUCCESS
);
423 /* We must not be able to access the 2nd page */
424 Status
= STATUS_SUCCESS
;
427 *((PUCHAR
)BaseAddress
+ PAGE_SIZE
) = 1;
431 Status
= _SEH2_GetExceptionCode();
434 ok_ntstatus(Status
, STATUS_ACCESS_VIOLATION
);
436 /* Map the 2 pages again into a different memory location */
438 Status
= NtMapViewOfSection(SectionHandle
,
448 ok_ntstatus(Status
, STATUS_SUCCESS
);
450 /* Commit a the 2nd page in the 2nd memory location */
451 BaseAddress2
= (PUCHAR
)BaseAddress2
+ PAGE_SIZE
;
452 ViewSize
= PAGE_SIZE
;
453 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
459 ok_ntstatus(Status
, STATUS_SUCCESS
);
461 /* Try to commit again (the already committed page) */
462 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
468 ok_ntstatus(Status
, STATUS_SUCCESS
);
470 /* We must be able to access the memory in the 2nd page of the 1st memory location */
471 Status
= STATUS_SUCCESS
;
474 *((PUCHAR
)BaseAddress
+ PAGE_SIZE
) = 2;
478 Status
= _SEH2_GetExceptionCode();
481 ok_ntstatus(Status
, STATUS_SUCCESS
);
483 ok(*(PULONG
)BaseAddress2
== 2, "Value in memory was wrong\n");
485 /* Close the mapping */
486 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
487 ok_ntstatus(Status
, STATUS_SUCCESS
);
488 Status
= NtUnmapViewOfSection(NtCurrentProcess(), (PUCHAR
)BaseAddress2
- PAGE_SIZE
);
489 ok_ntstatus(Status
, STATUS_SUCCESS
);
490 Status
= NtClose(SectionHandle
);
491 ok_ntstatus(Status
, STATUS_SUCCESS
);
493 /* Try to create a 512 GB page file backed section with committed pages */
494 MaximumSize
.QuadPart
= 0x8000000000;
495 Status
= NtCreateSection(&SectionHandle
,
502 ok_ntstatus(Status
, STATUS_COMMITMENT_LIMIT
);
504 /* Try to create a huge page file backed section with PAGE_NOACCESS protection */
505 MaximumSize
.QuadPart
= 0x8000000000;
506 Status
= NtCreateSection(&SectionHandle
,
513 ok_ntstatus(Status
, STATUS_INVALID_PAGE_PROTECTION
);
515 /* Try to create a very huge page file backed section, but only reserved */
516 MaximumSize
.QuadPart
= 0x80000000000;
517 Status
= NtCreateSection(&SectionHandle
,
525 ok_ntstatus(Status
, STATUS_INSUFFICIENT_RESOURCES
);
527 /* WoW64 returns STATUS_INSUFFICIENT_RESOURCES */
528 ok((Status
== STATUS_INSUFFICIENT_RESOURCES
) || (Status
== STATUS_SECTION_TOO_BIG
),
529 "got wrong Status: 0x%lx\n", Status
);
532 /* Try to create a even huger page file backed section, but only reserved */
533 MaximumSize
.QuadPart
= 0x800000000000;
534 Status
= NtCreateSection(&SectionHandle
,
541 ok_ntstatus(Status
, STATUS_SECTION_TOO_BIG
);
543 /* Create a 8 GB page file backed section, but only reserved */
544 MaximumSize
.QuadPart
= 0x200000000;
545 Status
= NtCreateSection(&SectionHandle
,
552 ok_ntstatus(Status
, STATUS_SUCCESS
);
554 /* Pass a too large region size */
556 SectionOffset
.QuadPart
= 0;
557 ViewSize
= MAXULONG_PTR
;
558 Status
= NtMapViewOfSection(SectionHandle
,
569 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_3
);
571 /* WoW64 returns STATUS_INVALID_PARAMETER_4 */
572 ok((Status
== STATUS_INVALID_PARAMETER_4
) || (Status
== STATUS_INVALID_PARAMETER_3
),
573 "got wrong Status: 0x%lx\n", Status
);
576 /* Pass 0 region size */
578 SectionOffset
.QuadPart
= 0;
580 Status
= NtMapViewOfSection(SectionHandle
,
591 ok_ntstatus(Status
, STATUS_SUCCESS
);
592 ok(ViewSize
== 0x200000000, "wrong ViewSize: 0x%Ix\n", ViewSize
);
594 /* WoW64 returns STATUS_NO_MEMORY */
595 ok((Status
== STATUS_NO_MEMORY
) || (Status
== STATUS_INVALID_VIEW_SIZE
),
596 "got wrong Status: 0x%lx\n", Status
);
597 ok(ViewSize
== 0, "wrong ViewSize: 0x%Ix\n", ViewSize
);
600 /* Map with PAGE_NOACCESS */
602 SectionOffset
.QuadPart
= 0;
603 ViewSize
= 0x20000000;
604 Status
= NtMapViewOfSection(SectionHandle
,
614 ok_ntstatus(Status
, STATUS_SUCCESS
);
616 /* Try to change protection to read/write */
619 BaseAddress2
= BaseAddress
;
620 Status
= NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2
, &ViewSize
, PAGE_READWRITE
, &OldProtect
);
621 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
622 // Windows 2003 returns bogus
623 //ok(OldProtect == PAGE_READWRITE, "Wrong protection returned: %u\n", OldProtect);
625 /* Test read access */
626 Status
= STATUS_SUCCESS
;
629 (void)(*(volatile char*)BaseAddress2
);
631 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
633 Status
= _SEH2_GetExceptionCode();
636 ok_ntstatus(Status
, STATUS_ACCESS_VIOLATION
);
638 /* Try to change protection to read/write */
641 BaseAddress2
= BaseAddress
;
642 Status
= NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2
, &ViewSize
, PAGE_READWRITE
, &OldProtect
);
643 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
645 ok(OldProtect
== 0, "Wrong protection returned: 0x%lx\n", OldProtect
);
647 // Windows 2003 returns bogus
650 /* Try to change protection to readonly */
653 BaseAddress2
= BaseAddress
;
654 Status
= NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2
, &ViewSize
, PAGE_READONLY
, &OldProtect
);
655 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
657 //ok(OldProtect == 0, "Wrong protection returned: 0x%lx\n", OldProtect);
659 // Windows 2003 returns bogus
664 BaseAddress2
= BaseAddress
;
665 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_READONLY
);
666 ok_ntstatus(Status
, STATUS_SUCCESS
);
667 ok(BaseAddress2
== BaseAddress
, "Invalid base address: %p\n", BaseAddress2
);
669 /* Commit the page again */
671 BaseAddress2
= BaseAddress
;
672 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_READONLY
);
673 ok_ntstatus(Status
, STATUS_SUCCESS
);
674 ok(BaseAddress2
== BaseAddress
, "Invalid base address: %p\n", BaseAddress2
);
676 /* Test read access */
677 Status
= STATUS_SUCCESS
;
680 (void)(*(volatile char*)BaseAddress2
);
682 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
684 Status
= _SEH2_GetExceptionCode();
687 ok_ntstatus(Status
, STATUS_SUCCESS
);
689 /* Test write access */
690 Status
= STATUS_SUCCESS
;
693 *(char*)BaseAddress2
= 1;
695 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
697 Status
= _SEH2_GetExceptionCode();
700 ok_ntstatus(Status
, STATUS_ACCESS_VIOLATION
);
702 /* Update protection to PAGE_READWRITE */
704 BaseAddress2
= BaseAddress
;
705 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_READWRITE
);
706 ok_ntstatus(Status
, STATUS_SUCCESS
);
708 /* Test write access */
709 Status
= STATUS_SUCCESS
;
712 *(char*)BaseAddress2
= 1;
714 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
716 Status
= _SEH2_GetExceptionCode();
719 ok_ntstatus(Status
, STATUS_SUCCESS
);
721 /* Update protection to PAGE_EXECUTE_READWRITE (1 page) */
723 BaseAddress2
= BaseAddress
;
724 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
725 ok_ntstatus(Status
, STATUS_SUCCESS
);
727 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
728 ok_ntstatus(Status
, STATUS_SUCCESS
);
729 Status
= NtClose(SectionHandle
);
730 ok_ntstatus(Status
, STATUS_SUCCESS
);
734 Test_ImageSection(void)
736 UNICODE_STRING FileName
;
738 OBJECT_ATTRIBUTES FileObjectAttributes
;
739 IO_STATUS_BLOCK IoStatusBlock
;
740 WCHAR TestDllPath
[MAX_PATH
];
741 HANDLE FileHandle
, DataSectionHandle
, ImageSectionHandle
;
742 PVOID DataBase
, ImageBase
;
745 GetModuleFileNameW(NULL
, TestDllPath
, RTL_NUMBER_OF(TestDllPath
));
746 wcsrchr(TestDllPath
, L
'\\')[1] = UNICODE_NULL
;
747 StringCbCatW(TestDllPath
, sizeof(TestDllPath
), L
"testdata\\test.dll");
748 if (!RtlDosPathNameToNtPathName_U(TestDllPath
,
753 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
757 InitializeObjectAttributes(&FileObjectAttributes
,
763 Status
= NtOpenFile(&FileHandle
,
764 GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
765 &FileObjectAttributes
,
768 FILE_SYNCHRONOUS_IO_NONALERT
);
769 ok_ntstatus(Status
, STATUS_SUCCESS
);
770 if (!NT_SUCCESS(Status
))
772 skip("Failed to open file\n");
776 /* Create a data section with write access */
777 Status
= NtCreateSection(&DataSectionHandle
,
778 SECTION_ALL_ACCESS
, // DesiredAccess
779 NULL
, // ObjectAttributes
781 PAGE_READWRITE
, // SectionPageProtection
782 SEC_COMMIT
, // AllocationAttributes
784 ok_ntstatus(Status
, STATUS_SUCCESS
);
785 if (!NT_SUCCESS(Status
))
787 skip("Failed to create data section\n");
792 /* Map the data section as flat mapping */
795 Status
= NtMapViewOfSection(DataSectionHandle
,
805 ok_ntstatus(Status
, STATUS_SUCCESS
);
806 //ok(ViewSize == 0x3f95cc48, "ViewSize wrong: 0x%lx\n");
807 if (!NT_SUCCESS(Status
))
809 skip("Failed to map view of data section\n");
810 NtClose(DataSectionHandle
);
815 /* Check the original data */
816 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
818 /* Modify the PE header (but do not flush!) */
819 *(ULONG
*)DataBase
= 0xdeadbabe;
820 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
822 /* Modify data in the .data section (but do not flush!) */
823 ok(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800) == 0x12345678,
824 "Data in .data section invalid: 0x%lx!\n", *(ULONG
*)((PUCHAR
)DataBase
+ 0x800));
825 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x87654321;
827 /* Now try to create an image section (should fail) */
828 Status
= NtCreateSection(&ImageSectionHandle
,
829 SECTION_ALL_ACCESS
, // DesiredAccess
830 NULL
, // ObjectAttributes
832 PAGE_READWRITE
, // SectionPageProtection
833 SEC_IMAGE
, // AllocationAttributes
835 ok_ntstatus(Status
, STATUS_INVALID_IMAGE_NOT_MZ
);
836 if (NT_SUCCESS(Status
)) NtClose(ImageSectionHandle
);
838 /* Restore the original header */
839 *(ULONG
*)DataBase
= 0x00905a4d;
841 /* Modify data in the .data section (but do not flush!) */
842 ok_hex(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800), 0x87654321);
843 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xdeadbabe;
845 /* Try to create an image section again */
846 Status
= NtCreateSection(&ImageSectionHandle
,
847 SECTION_ALL_ACCESS
, // DesiredAccess
848 NULL
, // ObjectAttributes
850 PAGE_READWRITE
, // SectionPageProtection
851 SEC_IMAGE
, // AllocationAttributes
853 ok_ntstatus(Status
, STATUS_SUCCESS
);
854 if (!NT_SUCCESS(Status
))
856 skip("Failed to create image section\n");
857 NtClose(DataSectionHandle
);
862 /* Map the image section */
865 Status
= NtMapViewOfSection(ImageSectionHandle
,
870 NULL
, // SectionOffset
876 ok_ntstatus(Status
, STATUS_SUCCESS
);
878 ok_ntstatus(Status
, STATUS_IMAGE_MACHINE_TYPE_MISMATCH
);
880 if (!NT_SUCCESS(Status
))
882 skip("Failed to map view of image section\n");
883 NtClose(ImageSectionHandle
);
884 NtClose(DataSectionHandle
);
889 /* Check the header */
890 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
891 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Header not ok\n");
893 /* Check the data section. Either of these can be present! */
894 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
895 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
896 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
898 /* Now modify the data again */
899 *(ULONG
*)DataBase
= 0xdeadbabe;
900 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xf00dada;
903 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
904 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
905 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
906 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
907 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
911 Status
= NtFlushVirtualMemory(NtCurrentProcess(),
915 ok_ntstatus(Status
, STATUS_SUCCESS
);
917 /* Check the data again */
918 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
919 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
920 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
921 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
923 /* Restore the original header */
924 *(ULONG
*)DataBase
= 0x00905a4d;
925 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
927 /* Close the image mapping */
928 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
929 NtClose(ImageSectionHandle
);
931 /* Create an image section again */
932 Status
= NtCreateSection(&ImageSectionHandle
,
933 SECTION_ALL_ACCESS
, // DesiredAccess
934 NULL
, // ObjectAttributes
936 PAGE_READWRITE
, // SectionPageProtection
937 SEC_IMAGE
, // AllocationAttributes
939 ok_ntstatus(Status
, STATUS_SUCCESS
);
940 if (!NT_SUCCESS(Status
))
942 skip("Failed to create image section\n");
943 NtClose(DataSectionHandle
);
948 /* Map the image section again */
951 Status
= NtMapViewOfSection(ImageSectionHandle
,
962 ok_ntstatus(Status
, STATUS_SUCCESS
);
964 ok_ntstatus(Status
, STATUS_IMAGE_MACHINE_TYPE_MISMATCH
);
966 if (!NT_SUCCESS(Status
))
968 skip("Failed to map view of image section\n");
969 NtClose(ImageSectionHandle
);
970 NtClose(DataSectionHandle
);
975 // This one doesn't always work, needs investigation
976 /* Check the .data section again */
977 //ok(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0xf00dada,
978 // "Data should be synced: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
980 /* Restore the original data */
981 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x12345678;
983 /* Close the data mapping */
984 NtUnmapViewOfSection(NtCurrentProcess(), DataBase
);
986 NtClose(DataSectionHandle
);
988 /* Try to allocate memory inside the image mapping */
989 DataBase
= (PUCHAR
)ImageBase
+ 0x20000;
991 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &DataBase
, 0, &ViewSize
, MEM_RESERVE
, PAGE_NOACCESS
);
992 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
996 NtClose(ImageSectionHandle
);
997 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
1001 Test_ImageSection2(void)
1003 UNICODE_STRING FileName
;
1005 OBJECT_ATTRIBUTES FileObjectAttributes
;
1006 IO_STATUS_BLOCK IoStatusBlock
;
1007 HANDLE FileHandle
, ImageSectionHandle
;
1008 PVOID ImageBase
, BaseAddress
;
1010 LARGE_INTEGER MaximumSize
, SectionOffset
;
1012 if (!RtlDosPathNameToNtPathName_U(L
"testdata\\nvoglv32.dll",
1017 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
1021 InitializeObjectAttributes(&FileObjectAttributes
,
1027 Status
= NtOpenFile(&FileHandle
,
1028 GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
1029 &FileObjectAttributes
,
1032 FILE_SYNCHRONOUS_IO_NONALERT
);
1033 ok_ntstatus(Status
, STATUS_SUCCESS
);
1034 printf("Opened file with handle %p\n", FileHandle
);
1036 /* Create a data section with write access */
1037 MaximumSize
.QuadPart
= 0x20000;
1038 Status
= NtCreateSection(&ImageSectionHandle
,
1039 SECTION_ALL_ACCESS
, // DesiredAccess
1040 NULL
, // ObjectAttributes
1041 &MaximumSize
, // MaximumSize
1042 PAGE_READWRITE
, // SectionPageProtection
1043 SEC_IMAGE
, // AllocationAttributes
1045 ok_ntstatus(Status
, STATUS_SUCCESS
);
1047 printf("Created image section with handle %p\n", ImageSectionHandle
);
1050 /* Map the image section */
1053 SectionOffset
.QuadPart
= 0x00000;
1054 Status
= NtMapViewOfSection(ImageSectionHandle
,
1064 ok_ntstatus(Status
, STATUS_SUCCESS
);
1066 printf("Mapped image section at %p, value in text section: %lx\n",
1067 ImageBase
, *((ULONG
*)((PCHAR
)ImageBase
+ 0x1196)));
1070 /* Try to allocate a page after the section */
1071 BaseAddress
= (PUCHAR
)ImageBase
+ 0x10000;
1073 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
1077 MEM_RESERVE
| MEM_COMMIT
,
1079 printf("allocation status: %lx\n", Status
);
1084 // doesn't work with WoW64!
1086 Test_BasedSection(void)
1089 HANDLE SectionHandle1
, SectionHandle2
;
1090 LARGE_INTEGER MaximumSize
, SectionOffset
;
1091 PVOID BaseAddress1
, BaseAddress2
;
1094 /* Create a based section with SEC_COMMIT */
1095 MaximumSize
.QuadPart
= 0x1000;
1096 Status
= NtCreateSection(&SectionHandle1
,
1101 SEC_COMMIT
| SEC_BASED
,
1103 ok_ntstatus(Status
, STATUS_SUCCESS
);
1105 /* Map the 1st section */
1106 BaseAddress1
= NULL
;
1107 SectionOffset
.QuadPart
= 0;
1109 Status
= NtMapViewOfSection(SectionHandle1
,
1120 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
1122 ok_ntstatus(Status
, STATUS_SUCCESS
);
1125 /* Create a 2nd based section with SEC_COMMIT */
1126 MaximumSize
.QuadPart
= 0x1000;
1127 Status
= NtCreateSection(&SectionHandle2
,
1132 SEC_COMMIT
| SEC_BASED
,
1134 ok_ntstatus(Status
, STATUS_SUCCESS
);//
1136 /* Map the 2nd section */
1137 BaseAddress2
= NULL
;
1138 SectionOffset
.QuadPart
= 0;
1140 Status
= NtMapViewOfSection(SectionHandle2
,
1151 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
1153 ok_ntstatus(Status
, STATUS_SUCCESS
);
1154 ok((ULONG_PTR
)BaseAddress2
< (ULONG_PTR
)BaseAddress1
,
1155 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1
, BaseAddress2
);
1156 ok(((ULONG_PTR
)BaseAddress1
- (ULONG_PTR
)BaseAddress2
) == 0x10000,
1157 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1
, BaseAddress2
);
1161 #define BYTES4(x) x, x, x, x
1162 #define BYTES8(x) BYTES4(x), BYTES4(x)
1163 #define BYTES16(x) BYTES8(x), BYTES8(x)
1164 #define BYTES32(x) BYTES16(x), BYTES16(x)
1165 #define BYTES64(x) BYTES32(x), BYTES32(x)
1166 #define BYTES128(x) BYTES64(x), BYTES64(x)
1167 #define BYTES256(x) BYTES128(x), BYTES128(x)
1168 #define BYTES512(x) BYTES256(x), BYTES256(x)
1169 #define BYTES1024(x) BYTES512(x), BYTES512(x)
1171 static struct _MY_IMAGE_FILE
1173 IMAGE_DOS_HEADER doshdr
;
1175 IMAGE_NT_HEADERS32 nthdrs
;
1176 IMAGE_SECTION_HEADER text_header
;
1177 IMAGE_SECTION_HEADER rossym_header
;
1178 IMAGE_SECTION_HEADER rsrc_header
;
1180 BYTE text_data
[0x400];
1181 BYTE rossym_data
[0x400];
1182 BYTE rsrc_data
[0x400];
1185 /* IMAGE_DOS_HEADER */
1187 IMAGE_DOS_SIGNATURE
, 144, 3, 0, 4, 0, 0xFFFF, 0, 0xB8, 0, 0, 0, 0x40,
1188 0, { 0 }, 0, 0, { 0 }, 0x80
1190 /* binary to print "This program cannot be run in DOS mode." */
1192 0x1F0E, 0x0EBA, 0xB400, 0xCD09, 0xB821, 0x4C01, 0x21CD, 0x6854, 0x7369,
1193 0x7020, 0x6F72, 0x7267, 0x6D61, 0x6320, 0x6E61, 0x6F6E, 0x2074, 0x6562,
1194 0x7220, 0x6E75, 0x6920, 0x206E, 0x4F44, 0x2053, 0x6F6D, 0x6564, 0x0D2E,
1195 0x0A0D, 0x0024, 0x0000, 0x0000, 0x0000
1197 /* IMAGE_NT_HEADERS32 */
1199 IMAGE_NT_SIGNATURE
, /* Signature */
1200 /* IMAGE_FILE_HEADER */
1202 IMAGE_FILE_MACHINE_I386
, /* Machine */
1203 3, /* NumberOfSections */
1204 0x47EFDF09, /* TimeDateStamp */
1205 0, /* PointerToSymbolTable */
1206 0, /* NumberOfSymbols */
1207 0xE0, /* SizeOfOptionalHeader */
1208 IMAGE_FILE_32BIT_MACHINE
| IMAGE_FILE_LOCAL_SYMS_STRIPPED
|
1209 IMAGE_FILE_LINE_NUMS_STRIPPED
| IMAGE_FILE_EXECUTABLE_IMAGE
|
1210 IMAGE_FILE_DLL
, /* Characteristics */
1212 /* IMAGE_OPTIONAL_HEADER32 */
1214 IMAGE_NT_OPTIONAL_HDR32_MAGIC
, /* Magic */
1215 8, /* MajorLinkerVersion */
1216 0, /* MinorLinkerVersion */
1217 0x400, /* SizeOfCode */
1218 0x000, /* SizeOfInitializedData */
1219 0, /* SizeOfUninitializedData */
1220 0x2000, /* AddressOfEntryPoint */
1221 0x2000, /* BaseOfCode */
1222 0x0000, /* BaseOfData */
1223 0x400000, /* ImageBase */
1224 0x2000, /* SectionAlignment */
1225 0x200, /* FileAlignment */
1226 4, /* MajorOperatingSystemVersion */
1227 0, /* MinorOperatingSystemVersion */
1228 0, /* MajorImageVersion */
1229 0, /* MinorImageVersion */
1230 4, /* MajorSubsystemVersion */
1231 0, /* MinorSubsystemVersion */
1232 0, /* Win32VersionValue */
1233 0x8000, /* SizeOfImage */
1234 0x200, /* SizeOfHeaders */
1236 IMAGE_SUBSYSTEM_WINDOWS_CUI
, /* Subsystem */
1237 IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
|
1238 IMAGE_DLLCHARACTERISTICS_NO_SEH
|
1239 IMAGE_DLLCHARACTERISTICS_NX_COMPAT
, /* DllCharacteristics */
1240 0x100000, /* SizeOfStackReserve */
1241 0x1000, /* SizeOfStackCommit */
1242 0x100000, /* SizeOfHeapReserve */
1243 0x1000, /* SizeOfHeapCommit */
1244 0, /* LoaderFlags */
1245 0x10, /* NumberOfRvaAndSizes */
1246 /* IMAGE_DATA_DIRECTORY */
1248 { 0 }, /* Export Table */
1249 { 0 }, /* Import Table */
1250 { 0 }, /* Resource Table */
1251 { 0 }, /* Exception Table */
1252 { 0 }, /* Certificate Table */
1253 { 0 }, /* Base Relocation Table */
1255 { 0 }, /* Copyright */
1256 { 0 }, /* Global Ptr */
1257 { 0 }, /* TLS Table */
1258 { 0 }, /* Load Config Table */
1259 { 0 }, /* Bound Import */
1261 { 0 }, /* Delay Import Descriptor */
1262 { 0 }, /* CLI Header */
1263 { 0 } /* Reserved */
1267 /* IMAGE_SECTION_HEADER */
1270 { 0x394 }, /* Misc.VirtualSize */
1271 0x2000, /* VirtualAddress */
1272 0x400, /* SizeOfRawData */
1273 0x200, /* PointerToRawData */
1274 0, /* PointerToRelocations */
1275 0, /* PointerToLinenumbers */
1276 0, /* NumberOfRelocations */
1277 0, /* NumberOfLinenumbers */
1278 IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_EXECUTE
|
1279 IMAGE_SCN_CNT_CODE
, /* Characteristics */
1281 /* IMAGE_SECTION_HEADER */
1283 ".rossym", /* Name */
1284 { 0x100 }, /* Misc.VirtualSize */
1285 0x4000, /* VirtualAddress */
1286 0x400, /* SizeOfRawData */
1287 0x600, /* PointerToRawData */
1288 0, /* PointerToRelocations */
1289 0, /* PointerToLinenumbers */
1290 0, /* NumberOfRelocations */
1291 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 0xA00, /* PointerToRawData */
1301 0, /* PointerToRelocations */
1302 0, /* PointerToLinenumbers */
1303 0, /* NumberOfRelocations */
1304 0, /* NumberOfLinenumbers */
1305 IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
, /* Characteristics */
1312 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
1324 C_ASSERT(FIELD_OFFSET(struct _MY_IMAGE_FILE
, text_data
) == 0x200);
1325 C_ASSERT(FIELD_OFFSET(struct _MY_IMAGE_FILE
, rossym_data
) == 0x600);
1326 C_ASSERT(FIELD_OFFSET(struct _MY_IMAGE_FILE
, rsrc_data
) == 0xa00);
1330 Test_NoLoadSection(BOOL Relocate
)
1333 WCHAR TempPath
[MAX_PATH
];
1334 WCHAR FileName
[MAX_PATH
];
1336 HANDLE SectionHandle
;
1337 LARGE_INTEGER SectionOffset
;
1344 Length
= GetTempPathW(MAX_PATH
, TempPath
);
1345 ok(Length
!= 0, "GetTempPathW failed with %lu\n", GetLastError());
1346 Length
= GetTempFileNameW(TempPath
, L
"nta", 0, FileName
);
1347 ok(Length
!= 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1348 Handle
= CreateFileW(FileName
,
1355 if (Handle
== INVALID_HANDLE_VALUE
)
1357 skip("Failed to create temp file %ls, error %lu\n", FileName
, GetLastError());
1362 ok((ULONG_PTR
)GetModuleHandle(NULL
) <= 0x80000000, "Module at %p\n", GetModuleHandle(NULL
));
1363 ImageFile
.nthdrs
.OptionalHeader
.ImageBase
= (ULONG
)(ULONG_PTR
)GetModuleHandle(NULL
);
1367 ImageFile
.nthdrs
.OptionalHeader
.ImageBase
= 0xe400000;
1370 Success
= WriteFile(Handle
,
1375 ok(Success
== TRUE
, "WriteFile failed with %lu\n", GetLastError());
1376 ok(Written
== sizeof(ImageFile
), "WriteFile wrote %lu bytes\n", Written
);
1378 Status
= NtCreateSection(&SectionHandle
,
1382 PAGE_EXECUTE_READWRITE
,
1385 ok_ntstatus(Status
, STATUS_SUCCESS
);
1387 if (NT_SUCCESS(Status
))
1389 /* Map the section with */
1391 SectionOffset
.QuadPart
= 0;
1393 Status
= NtMapViewOfSection(SectionHandle
,
1404 ok_ntstatus(Status
, STATUS_IMAGE_NOT_AT_BASE
);
1406 ok_ntstatus(Status
, STATUS_SUCCESS
);
1407 if (NT_SUCCESS(Status
))
1409 PUCHAR Bytes
= BaseAddress
;
1410 #define TEST_BYTE(n, v) StartSeh() ok_hex(Bytes[n], v); EndSeh(STATUS_SUCCESS);
1411 TEST_BYTE(0x2000, 0xc3);
1412 TEST_BYTE(0x2001, 0x00);
1413 TEST_BYTE(0x4000, 0x01);
1414 TEST_BYTE(0x4001, 0x23);
1415 TEST_BYTE(0x4007, 0xef);
1416 TEST_BYTE(0x4008, 0xaa);
1417 TEST_BYTE(0x4010, 0xbb);
1418 TEST_BYTE(0x4020, 0xcc);
1419 TEST_BYTE(0x4040, 0xdd);
1420 TEST_BYTE(0x4080, 0xee);
1421 TEST_BYTE(0x40c0, 0xff);
1422 TEST_BYTE(0x40ff, 0xff);
1423 TEST_BYTE(0x4100, 0x00);
1424 TEST_BYTE(0x41ff, 0x00);
1425 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
1426 ok_ntstatus(Status
, STATUS_SUCCESS
);
1428 Status
= NtClose(SectionHandle
);
1429 ok_ntstatus(Status
, STATUS_SUCCESS
);
1432 CloseHandle(Handle
);
1433 DeleteFileW(FileName
);
1437 Test_EmptyFile(VOID
)
1440 WCHAR TempPath
[MAX_PATH
];
1441 WCHAR FileName
[MAX_PATH
];
1443 HANDLE SectionHandle
;
1446 Length
= GetTempPathW(MAX_PATH
, TempPath
);
1447 ok(Length
!= 0, "GetTempPathW failed with %lu\n", GetLastError());
1448 Length
= GetTempFileNameW(TempPath
, L
"nta", 0, FileName
);
1449 ok(Length
!= 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1450 Handle
= CreateFileW(FileName
,
1457 if (Handle
== INVALID_HANDLE_VALUE
)
1459 skip("Failed to create temp file %ls, error %lu\n", FileName
, GetLastError());
1463 Status
= NtCreateSection(&SectionHandle
,
1464 STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1465 0, 0, PAGE_READONLY
, SEC_COMMIT
, Handle
);
1466 ok_ntstatus(Status
, STATUS_MAPPED_FILE_SIZE_ZERO
);
1468 if (NT_SUCCESS(Status
))
1469 NtClose(SectionHandle
);
1471 Status
= NtCreateSection(&SectionHandle
,
1472 STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1473 0, 0, PAGE_READONLY
, SEC_IMAGE
, Handle
);
1474 ok_ntstatus(Status
, STATUS_INVALID_FILE_FOR_SECTION
);
1476 if (NT_SUCCESS(Status
))
1477 NtClose(SectionHandle
);
1479 CloseHandle(Handle
);
1480 DeleteFileW(FileName
);
1487 WCHAR TempPath
[MAX_PATH
];
1488 WCHAR FileName
[MAX_PATH
];
1490 SIZE_T ViewSize
= 0;
1492 HANDLE SectionHandle
;
1496 DWORD Written
, Error
;
1499 Length
= GetTempPathW(MAX_PATH
, TempPath
);
1500 ok(Length
!= 0, "GetTempPathW failed with %lu\n", GetLastError());
1501 Length
= GetTempFileNameW(TempPath
, L
"nta", 0, FileName
);
1502 ok(Length
!= 0, "GetTempFileNameW failed with %lu\n", GetLastError());
1503 Handle
= CreateFileW(FileName
, FILE_ALL_ACCESS
, 0, NULL
, CREATE_ALWAYS
, 0, NULL
);
1505 Success
= WriteFile(Handle
, "TESTDATA", 8, &Written
, NULL
);
1506 ok(Success
== TRUE
, "WriteFile failed with %lu\n", GetLastError());
1507 ok(Written
== 8, "WriteFile wrote %lu bytes\n", Written
);
1509 Written
= SetFilePointer(Handle
, 6, NULL
, FILE_BEGIN
);
1510 ok(Written
== 6, "SetFilePointer returned %lu bytes\n", Written
);
1511 Success
= SetEndOfFile(Handle
);
1512 ok(Success
== TRUE
, "SetEndOfFile failed with %lu\n", 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_SUCCESS
);
1520 Status
= NtMapViewOfSection(SectionHandle
, NtCurrentProcess(), &BaseAddress
, 0,
1521 0, 0, &ViewSize
, ViewShare
, 0, PAGE_READONLY
);
1522 ok_ntstatus(Status
, STATUS_SUCCESS
);
1526 // First we test data that was truncated even before the file mapping was opened
1527 Length
= strlen((char*)BaseAddress
);
1528 ok(Length
== 6, "Old data was not properly erased! (Length=%lu)\n", Length
);
1531 // Now we truncate the file on disk some more
1532 Written
= SetFilePointer(Handle
, 4, NULL
, FILE_BEGIN
);
1533 ok(Written
== 4, "SetFilePointer returned %lu bytes\n", Written
);
1534 Success
= SetEndOfFile(Handle
);
1535 Error
= GetLastError();
1536 ok(Success
== FALSE
, "SetEndOfFile succeeded\n");
1537 ok(Error
== ERROR_USER_MAPPED_FILE
, "SetEndOfFile did not set error to ERROR_USER_MAPPED_FILE (%lu)\n", Error
);
1541 Length
= strlen((char*)BaseAddress
);
1542 ok(Length
== 6, "Length should not have changed! (Length=%lu)\n", Length
);
1545 // Unmap and set the end shorter.
1546 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
1547 ok_ntstatus(Status
, STATUS_SUCCESS
);
1548 Success
= CloseHandle(SectionHandle
);
1549 ok(Success
== TRUE
, "CloseHandle failed with %lu\n", GetLastError());
1551 Written
= SetFilePointer(Handle
, 4, NULL
, FILE_BEGIN
);
1552 ok(Written
== 4, "SetFilePointer returned %lu bytes\n", Written
);
1553 Success
= SetEndOfFile(Handle
);
1554 ok(Success
== TRUE
, "SetEndOfFile failed with %lu\n", GetLastError());
1556 Status
= NtCreateSection(&SectionHandle
,
1557 STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1558 0, 0, PAGE_READONLY
, SEC_COMMIT
, Handle
);
1559 ok_ntstatus(Status
, STATUS_SUCCESS
);
1562 Status
= NtMapViewOfSection(SectionHandle
, NtCurrentProcess(), &BaseAddress
, 0,
1563 0, 0, &ViewSize
, ViewShare
, 0, PAGE_READONLY
);
1564 ok_ntstatus(Status
, STATUS_SUCCESS
);
1567 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
1568 ok_ntstatus(Status
, STATUS_SUCCESS
);
1569 Success
= CloseHandle(SectionHandle
);
1570 ok(Success
== TRUE
, "CloseHandle failed with %lu\n", GetLastError());
1571 Success
= CloseHandle(Handle
);
1572 ok(Success
== TRUE
, "CloseHandle failed with %lu\n", GetLastError());
1574 Success
= DeleteFileW(FileName
);
1575 ok(Success
== TRUE
, "DeleteFileW failed with %lu\n", GetLastError());
1578 START_TEST(NtMapViewOfSection
)
1580 Test_PageFileSection();
1581 Test_ImageSection();
1582 Test_BasedSection();
1583 Test_NoLoadSection(FALSE
);
1584 Test_NoLoadSection(TRUE
);