4 #define WIN32_NO_STATUS
16 PLARGE_INTEGER SectionOffset
,
18 SECTION_INHERIT InheritDisposition
,
23 Test_PageFileSection(void)
27 LARGE_INTEGER MaximumSize
, SectionOffset
;
28 PVOID BaseAddress
, BaseAddress2
;
32 /* Create a page file backed section with SEC_COMMIT */
33 MaximumSize
.QuadPart
= 0x20000;
34 Status
= NtCreateSection(&SectionHandle
,
41 ok_ntstatus(Status
, STATUS_SUCCESS
);
42 if (!NT_SUCCESS(Status
))
45 /* Try to map a page at an address that is not 64k aligned */
46 BaseAddress
= (PVOID
)0x30001000;
47 SectionOffset
.QuadPart
= 0;
49 Status
= NtMapViewOfSection(SectionHandle
,
59 ok_ntstatus(Status
, STATUS_MAPPED_ALIGNMENT
);
61 /* Try to map a page with execute rights */
62 BaseAddress
= (PVOID
)0x30000000;
63 SectionOffset
.QuadPart
= 0;
65 Status
= NtMapViewOfSection(SectionHandle
,
74 PAGE_EXECUTE_READWRITE
);
75 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
77 /* Try to map 2 pages with MEM_COMMIT */
78 BaseAddress
= (PVOID
)0x30000000;
79 SectionOffset
.QuadPart
= 0;
81 Status
= NtMapViewOfSection(SectionHandle
,
91 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_9
);
93 /* Map 2 pages, without MEM_COMMIT */
94 BaseAddress
= (PVOID
)0x30000000;
95 SectionOffset
.QuadPart
= 0;
97 Status
= NtMapViewOfSection(SectionHandle
,
107 ok_ntstatus(Status
, STATUS_SUCCESS
);
109 /* We must be able to access the memory */
112 *(PULONG
)BaseAddress
= 1;
116 ok(FALSE
, "Got an exception\n");
120 /* Commit a page in the section */
121 BaseAddress
= (PVOID
)0x30000000;
123 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
129 ok_ntstatus(Status
, STATUS_SUCCESS
);
131 /* Try to decommit a page in the section */
132 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
136 ok_ntstatus(Status
, STATUS_UNABLE_TO_DELETE_SECTION
);
138 /* Try to commit a range larger than the section */
139 BaseAddress
= (PVOID
)0x30000000;
141 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
147 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
149 /* Try to commit a page after the section */
150 BaseAddress
= (PVOID
)0x30002000;
152 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
158 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
160 /* Try to allocate a page after the section */
161 BaseAddress
= (PVOID
)0x30002000;
163 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
167 MEM_RESERVE
| MEM_COMMIT
,
169 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
171 /* Need to go to next 64k boundary */
172 BaseAddress
= (PVOID
)0x30010000;
174 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
178 MEM_RESERVE
| MEM_COMMIT
,
180 ok_ntstatus(Status
, STATUS_SUCCESS
);
181 if (!NT_SUCCESS(Status
))
184 /* Free the allocation */
185 BaseAddress
= (PVOID
)0x30010000;
187 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
191 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed with Status %lx\n", Status
);
193 /* Try to release the section mapping with NtFreeVirtualMemory */
194 BaseAddress
= (PVOID
)0x30000000;
196 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
200 ok_ntstatus(Status
, STATUS_UNABLE_TO_DELETE_SECTION
);
202 /* Commit a page in the section */
203 BaseAddress
= (PVOID
)0x30001000;
205 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
211 ok_ntstatus(Status
, STATUS_SUCCESS
);
213 /* Try to decommit the page */
214 BaseAddress
= (PVOID
)0x30001000;
216 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
220 ok_ntstatus(Status
, STATUS_UNABLE_TO_DELETE_SECTION
);
222 BaseAddress
= UlongToPtr(0x40000000);
223 SectionOffset
.QuadPart
= 0;
225 Status
= NtMapViewOfSection(SectionHandle
,
235 ok_ntstatus(Status
, STATUS_SUCCESS
);
236 if (!NT_SUCCESS(Status
))
239 ok(BaseAddress
== UlongToPtr(0x40000000), "Invalid BaseAddress: %p\n", BaseAddress
);
241 BaseAddress
= (PVOID
)0x40080000;
242 SectionOffset
.QuadPart
= 0x10000;
244 Status
= NtMapViewOfSection(SectionHandle
,
254 ok_ntstatus(Status
, STATUS_SUCCESS
);
256 ok(BaseAddress
== (PVOID
)0x40080000, "Invalid BaseAddress: %p\n", BaseAddress
);
258 /* Commit a page in the section */
259 BaseAddress
= (PVOID
)0x40000000;
260 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
266 ok_ntstatus(Status
, STATUS_SUCCESS
);
268 /* Close the mapping */
269 NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
270 NtClose(SectionHandle
);
272 /* Create a page file backed section, but only reserved */
273 MaximumSize
.QuadPart
= 0x20000;
274 Status
= NtCreateSection(&SectionHandle
,
281 ok_ntstatus(Status
, STATUS_SUCCESS
);
283 /* Try to map 1 page, passing MEM_RESERVE */
285 SectionOffset
.QuadPart
= 0;
286 ViewSize
= PAGE_SIZE
;
287 Status
= NtMapViewOfSection(SectionHandle
,
297 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_9
);
299 /* Try to map 1 page using MEM_COMMIT */
301 SectionOffset
.QuadPart
= 0;
302 ViewSize
= PAGE_SIZE
;
303 Status
= NtMapViewOfSection(SectionHandle
,
313 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_9
);
315 /* Map 2 pages, but commit 1 */
317 SectionOffset
.QuadPart
= 0;
318 ViewSize
= 2 * PAGE_SIZE
;
319 Status
= NtMapViewOfSection(SectionHandle
,
329 ok_ntstatus(Status
, STATUS_SUCCESS
);
331 /* We must be able to access the 1st page */
332 Status
= STATUS_SUCCESS
;
335 *(PUCHAR
)BaseAddress
= 1;
339 Status
= _SEH2_GetExceptionCode();
342 ok_ntstatus(Status
, STATUS_SUCCESS
);
344 /* We must not be able to access the 2nd page */
345 Status
= STATUS_SUCCESS
;
348 *((PUCHAR
)BaseAddress
+ PAGE_SIZE
) = 1;
352 Status
= _SEH2_GetExceptionCode();
355 ok_ntstatus(Status
, STATUS_ACCESS_VIOLATION
);
357 /* Map the 2 pages again into a different memory location */
359 Status
= NtMapViewOfSection(SectionHandle
,
369 ok_ntstatus(Status
, STATUS_SUCCESS
);
371 /* Commit a the 2nd page in the 2nd memory location */
372 BaseAddress2
= (PUCHAR
)BaseAddress2
+ PAGE_SIZE
;
373 ViewSize
= PAGE_SIZE
;
374 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
380 ok_ntstatus(Status
, STATUS_SUCCESS
);
382 /* Try to commit again (the already committed page) */
383 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
389 ok_ntstatus(Status
, STATUS_SUCCESS
);
391 /* We must be able to access the memory in the 2nd page of the 1st memory location */
392 Status
= STATUS_SUCCESS
;
395 *((PUCHAR
)BaseAddress
+ PAGE_SIZE
) = 2;
399 Status
= _SEH2_GetExceptionCode();
402 ok_ntstatus(Status
, STATUS_SUCCESS
);
404 ok(*(PULONG
)BaseAddress2
== 2, "Value in memory was wrong\n");
406 /* Close the mapping */
407 NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
408 NtUnmapViewOfSection(NtCurrentProcess(), (PUCHAR
)BaseAddress2
- PAGE_SIZE
);
409 NtClose(SectionHandle
);
411 /* Try to create a 512 GB page file backed section with committed pages */
412 MaximumSize
.QuadPart
= 0x8000000000;
413 Status
= NtCreateSection(&SectionHandle
,
420 ok_ntstatus(Status
, STATUS_COMMITMENT_LIMIT
);
422 /* Try to create a huge page file backed section with PAGE_NOACCESS protection */
423 MaximumSize
.QuadPart
= 0x8000000000;
424 Status
= NtCreateSection(&SectionHandle
,
431 ok_ntstatus(Status
, STATUS_INVALID_PAGE_PROTECTION
);
433 /* Try to create a very huge page file backed section, but only reserved */
434 MaximumSize
.QuadPart
= 0x80000000000;
435 Status
= NtCreateSection(&SectionHandle
,
443 ok_ntstatus(Status
, STATUS_INSUFFICIENT_RESOURCES
);
445 /* WoW64 returns STATUS_INSUFFICIENT_RESOURCES */
446 ok((Status
== STATUS_INSUFFICIENT_RESOURCES
) || (Status
== STATUS_SECTION_TOO_BIG
),
447 "got wrong Status: 0x%lx\n", Status
);
450 /* Try to create a even huger page file backed section, but only reserved */
451 MaximumSize
.QuadPart
= 0x800000000000;
452 Status
= NtCreateSection(&SectionHandle
,
459 ok_ntstatus(Status
, STATUS_SECTION_TOO_BIG
);
461 /* Create a 8 GB page file backed section, but only reserved */
462 MaximumSize
.QuadPart
= 0x200000000;
463 Status
= NtCreateSection(&SectionHandle
,
470 ok_ntstatus(Status
, STATUS_SUCCESS
);
472 /* Pass a too large region size */
474 SectionOffset
.QuadPart
= 0;
475 ViewSize
= MAXULONG_PTR
;
476 Status
= NtMapViewOfSection(SectionHandle
,
487 ok_ntstatus(Status
, STATUS_INVALID_PARAMETER_3
);
489 /* WoW64 returns STATUS_INVALID_PARAMETER_4 */
490 ok((Status
== STATUS_INVALID_PARAMETER_4
) || (Status
== STATUS_INVALID_PARAMETER_3
),
491 "got wrong Status: 0x%lx\n", Status
);
494 /* Pass 0 region size */
496 SectionOffset
.QuadPart
= 0;
498 Status
= NtMapViewOfSection(SectionHandle
,
509 ok_ntstatus(Status
, STATUS_SUCCESS
);
510 ok(ViewSize
== 0x200000000, "wrong ViewSize: 0x%Ix\n", ViewSize
);
512 /* WoW64 returns STATUS_NO_MEMORY */
513 ok((Status
== STATUS_NO_MEMORY
) || (Status
== STATUS_INVALID_VIEW_SIZE
),
514 "got wrong Status: 0x%lx\n", Status
);
515 ok(ViewSize
== 0, "wrong ViewSize: 0x%Ix\n", ViewSize
);
518 /* Map with PAGE_NOACCESS */
520 SectionOffset
.QuadPart
= 0;
521 ViewSize
= 0x20000000;
522 Status
= NtMapViewOfSection(SectionHandle
,
532 ok_ntstatus(Status
, STATUS_SUCCESS
);
534 /* Try to change protection to read/write */
537 BaseAddress2
= BaseAddress
;
538 Status
= NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2
, &ViewSize
, PAGE_READWRITE
, &OldProtect
);
539 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
540 // Windows 2003 returns bogus
541 //ok(OldProtect == PAGE_READWRITE, "Wrong protection returned: %u\n", OldProtect);
543 /* Test read access */
544 Status
= STATUS_SUCCESS
;
547 (void)(*(volatile char*)BaseAddress2
);
549 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
551 Status
= _SEH2_GetExceptionCode();
554 ok_ntstatus(Status
, STATUS_ACCESS_VIOLATION
);
556 /* Try to change protection to read/write */
559 BaseAddress2
= BaseAddress
;
560 Status
= NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2
, &ViewSize
, PAGE_READWRITE
, &OldProtect
);
561 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
563 ok(OldProtect
== 0, "Wrong protection returned: 0x%lx\n", OldProtect
);
565 // Windows 2003 returns bogus
568 /* Try to change protection to readonly */
571 BaseAddress2
= BaseAddress
;
572 Status
= NtProtectVirtualMemory(NtCurrentProcess(), &BaseAddress2
, &ViewSize
, PAGE_READONLY
, &OldProtect
);
573 ok_ntstatus(Status
, STATUS_SECTION_PROTECTION
);
575 //ok(OldProtect == 0, "Wrong protection returned: 0x%lx\n", OldProtect);
577 // Windows 2003 returns bogus
582 BaseAddress2
= BaseAddress
;
583 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_READONLY
);
584 ok_ntstatus(Status
, STATUS_SUCCESS
);
585 ok(BaseAddress2
== BaseAddress
, "Invalid base address: %p\n", BaseAddress2
);
587 /* Commit the page again */
589 BaseAddress2
= BaseAddress
;
590 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_READONLY
);
591 ok_ntstatus(Status
, STATUS_SUCCESS
);
592 ok(BaseAddress2
== BaseAddress
, "Invalid base address: %p\n", BaseAddress2
);
594 /* Test read access */
595 Status
= STATUS_SUCCESS
;
598 (void)(*(volatile char*)BaseAddress2
);
600 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
602 Status
= _SEH2_GetExceptionCode();
605 ok_ntstatus(Status
, STATUS_SUCCESS
);
607 /* Test write access */
608 Status
= STATUS_SUCCESS
;
611 *(char*)BaseAddress2
= 1;
613 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
615 Status
= _SEH2_GetExceptionCode();
618 ok_ntstatus(Status
, STATUS_ACCESS_VIOLATION
);
620 /* Update protection to PAGE_READWRITE */
622 BaseAddress2
= BaseAddress
;
623 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_READWRITE
);
624 ok_ntstatus(Status
, STATUS_SUCCESS
);
626 /* Test write access */
627 Status
= STATUS_SUCCESS
;
630 *(char*)BaseAddress2
= 1;
632 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
634 Status
= _SEH2_GetExceptionCode();
637 ok_ntstatus(Status
, STATUS_SUCCESS
);
639 /* Update protection to PAGE_EXECUTE_READWRITE (1 page) */
641 BaseAddress2
= BaseAddress
;
642 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &BaseAddress2
, 0, &ViewSize
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
643 ok_ntstatus(Status
, STATUS_SUCCESS
);
648 Test_ImageSection(void)
650 UNICODE_STRING FileName
;
652 OBJECT_ATTRIBUTES FileObjectAttributes
;
653 IO_STATUS_BLOCK IoStatusBlock
;
654 HANDLE FileHandle
, DataSectionHandle
, ImageSectionHandle
;
655 PVOID DataBase
, ImageBase
;
658 if (!RtlDosPathNameToNtPathName_U(L
"testdata\\test.dll",
663 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
667 InitializeObjectAttributes(&FileObjectAttributes
,
673 Status
= NtOpenFile(&FileHandle
,
674 GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
675 &FileObjectAttributes
,
678 FILE_SYNCHRONOUS_IO_NONALERT
);
679 ok_ntstatus(Status
, STATUS_SUCCESS
);
680 if (!NT_SUCCESS(Status
))
682 skip("Failed to open file\n");
686 /* Create a data section with write access */
687 Status
= NtCreateSection(&DataSectionHandle
,
688 SECTION_ALL_ACCESS
, // DesiredAccess
689 NULL
, // ObjectAttributes
691 PAGE_READWRITE
, // SectionPageProtection
692 SEC_COMMIT
, // AllocationAttributes
694 ok_ntstatus(Status
, STATUS_SUCCESS
);
695 if (!NT_SUCCESS(Status
))
697 skip("Failed to create data section\n");
702 /* Map the data section as flat mapping */
705 Status
= NtMapViewOfSection(DataSectionHandle
,
715 ok_ntstatus(Status
, STATUS_SUCCESS
);
716 //ok(ViewSize == 0x3f95cc48, "ViewSize wrong: 0x%lx\n");
717 if (!NT_SUCCESS(Status
))
719 skip("Failed to map view of data section\n");
720 NtClose(DataSectionHandle
);
725 /* Check the original data */
726 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
728 /* Modify the PE header (but do not flush!) */
729 *(ULONG
*)DataBase
= 0xdeadbabe;
730 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
732 /* Modify data in the .data section (but do not flush!) */
733 ok(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800) == 0x12345678,
734 "Data in .data section invalid: 0x%lx!\n", *(ULONG
*)((PUCHAR
)DataBase
+ 0x800));
735 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x87654321;
737 /* Now try to create an image section (should fail) */
738 Status
= NtCreateSection(&ImageSectionHandle
,
739 SECTION_ALL_ACCESS
, // DesiredAccess
740 NULL
, // ObjectAttributes
742 PAGE_READWRITE
, // SectionPageProtection
743 SEC_IMAGE
, // AllocationAttributes
745 ok_ntstatus(Status
, STATUS_INVALID_IMAGE_NOT_MZ
);
746 if (NT_SUCCESS(Status
)) NtClose(ImageSectionHandle
);
748 /* Restore the original header */
749 *(ULONG
*)DataBase
= 0x00905a4d;
751 /* Modify data in the .data section (but do not flush!) */
752 ok_hex(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800), 0x87654321);
753 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xdeadbabe;
755 /* Try to create an image section again */
756 Status
= NtCreateSection(&ImageSectionHandle
,
757 SECTION_ALL_ACCESS
, // DesiredAccess
758 NULL
, // ObjectAttributes
760 PAGE_READWRITE
, // SectionPageProtection
761 SEC_IMAGE
, // AllocationAttributes
763 ok_ntstatus(Status
, STATUS_SUCCESS
);
764 if (!NT_SUCCESS(Status
))
766 skip("Failed to create image section\n");
767 NtClose(DataSectionHandle
);
772 /* Map the image section */
775 Status
= NtMapViewOfSection(ImageSectionHandle
,
780 NULL
, // SectionOffset
786 ok_ntstatus(Status
, STATUS_IMAGE_NOT_AT_BASE
);
788 ok_ntstatus(Status
, STATUS_IMAGE_MACHINE_TYPE_MISMATCH
);
790 if (!NT_SUCCESS(Status
))
792 skip("Failed to map view of image section\n");
793 NtClose(ImageSectionHandle
);
794 NtClose(DataSectionHandle
);
799 /* Check the header */
800 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
801 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Header not ok\n");
803 /* Check the data section. Either of these can be present! */
804 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
805 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
806 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
808 /* Now modify the data again */
809 *(ULONG
*)DataBase
= 0xdeadbabe;
810 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xf00dada;
813 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
814 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
815 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
816 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
817 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
821 Status
= NtFlushVirtualMemory(NtCurrentProcess(),
825 ok_ntstatus(Status
, STATUS_SUCCESS
);
827 /* Check the data again */
828 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
829 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
830 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
831 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
833 /* Restore the original header */
834 *(ULONG
*)DataBase
= 0x00905a4d;
835 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
837 /* Close the image mapping */
838 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
839 NtClose(ImageSectionHandle
);
841 /* Create an image section again */
842 Status
= NtCreateSection(&ImageSectionHandle
,
843 SECTION_ALL_ACCESS
, // DesiredAccess
844 NULL
, // ObjectAttributes
846 PAGE_READWRITE
, // SectionPageProtection
847 SEC_IMAGE
, // AllocationAttributes
849 ok_ntstatus(Status
, STATUS_SUCCESS
);
850 if (!NT_SUCCESS(Status
))
852 skip("Failed to create image section\n");
853 NtClose(DataSectionHandle
);
858 /* Map the image section again */
861 Status
= NtMapViewOfSection(ImageSectionHandle
,
872 ok_ntstatus(Status
, STATUS_IMAGE_NOT_AT_BASE
);
874 ok_ntstatus(Status
, STATUS_IMAGE_MACHINE_TYPE_MISMATCH
);
876 if (!NT_SUCCESS(Status
))
878 skip("Failed to map view of image section\n");
879 NtClose(ImageSectionHandle
);
880 NtClose(DataSectionHandle
);
885 // This one doesn't always work, needs investigation
886 /* Check the .data section again */
887 //ok(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0xf00dada,
888 // "Data should be synced: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
890 /* Restore the original data */
891 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x12345678;
893 /* Close the data mapping */
894 NtUnmapViewOfSection(NtCurrentProcess(), DataBase
);
896 NtClose(DataSectionHandle
);
898 /* Try to allocate memory inside the image mapping */
899 DataBase
= (PUCHAR
)ImageBase
+ 0x20000;
901 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &DataBase
, 0, &ViewSize
, MEM_RESERVE
, PAGE_NOACCESS
);
902 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
906 NtClose(ImageSectionHandle
);
907 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
911 Test_ImageSection2(void)
913 UNICODE_STRING FileName
;
915 OBJECT_ATTRIBUTES FileObjectAttributes
;
916 IO_STATUS_BLOCK IoStatusBlock
;
917 HANDLE FileHandle
, ImageSectionHandle
;
918 PVOID ImageBase
, BaseAddress
;
920 LARGE_INTEGER MaximumSize
, SectionOffset
;
922 if (!RtlDosPathNameToNtPathName_U(L
"testdata\\nvoglv32.dll",
927 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
931 InitializeObjectAttributes(&FileObjectAttributes
,
937 Status
= NtOpenFile(&FileHandle
,
938 GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
939 &FileObjectAttributes
,
942 FILE_SYNCHRONOUS_IO_NONALERT
);
943 ok_ntstatus(Status
, STATUS_SUCCESS
);
944 printf("Opened file with handle %p\n", FileHandle
);
946 /* Create a data section with write access */
947 MaximumSize
.QuadPart
= 0x20000;
948 Status
= NtCreateSection(&ImageSectionHandle
,
949 SECTION_ALL_ACCESS
, // DesiredAccess
950 NULL
, // ObjectAttributes
951 &MaximumSize
, // MaximumSize
952 PAGE_READWRITE
, // SectionPageProtection
953 SEC_IMAGE
, // AllocationAttributes
955 ok_ntstatus(Status
, STATUS_SUCCESS
);
957 printf("Created image section with handle %p\n", ImageSectionHandle
);
960 /* Map the image section */
963 SectionOffset
.QuadPart
= 0x00000;
964 Status
= NtMapViewOfSection(ImageSectionHandle
,
974 ok_ntstatus(Status
, STATUS_SUCCESS
);
976 printf("Mapped image section at %p, value in text section: %lx\n",
977 ImageBase
, *((ULONG
*)((PCHAR
)ImageBase
+ 0x1196)));
980 /* Try to allocate a page after the section */
981 BaseAddress
= (PUCHAR
)ImageBase
+ 0x10000;
983 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
987 MEM_RESERVE
| MEM_COMMIT
,
989 printf("allocation status: %lx\n", Status
);
994 // doesn't work with WoW64!
996 Test_BasedSection(void)
999 HANDLE SectionHandle1
, SectionHandle2
;
1000 LARGE_INTEGER MaximumSize
, SectionOffset
;
1001 PVOID BaseAddress1
, BaseAddress2
;
1004 /* Create a based section with SEC_COMMIT */
1005 MaximumSize
.QuadPart
= 0x1000;
1006 Status
= NtCreateSection(&SectionHandle1
,
1011 SEC_COMMIT
| SEC_BASED
,
1013 ok_ntstatus(Status
, STATUS_SUCCESS
);
1015 /* Map the 1st section */
1016 BaseAddress1
= NULL
;
1017 SectionOffset
.QuadPart
= 0;
1019 Status
= NtMapViewOfSection(SectionHandle1
,
1030 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
1032 ok_ntstatus(Status
, STATUS_SUCCESS
);
1035 /* Create a 2nd based section with SEC_COMMIT */
1036 MaximumSize
.QuadPart
= 0x1000;
1037 Status
= NtCreateSection(&SectionHandle2
,
1042 SEC_COMMIT
| SEC_BASED
,
1044 ok_ntstatus(Status
, STATUS_SUCCESS
);//
1046 /* Map the 2nd section */
1047 BaseAddress2
= NULL
;
1048 SectionOffset
.QuadPart
= 0;
1050 Status
= NtMapViewOfSection(SectionHandle2
,
1061 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
1063 ok_ntstatus(Status
, STATUS_SUCCESS
);
1066 ok_ntstatus(Status
, STATUS_SUCCESS
);
1067 ok((ULONG_PTR
)BaseAddress2
< (ULONG_PTR
)BaseAddress1
,
1068 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1
, BaseAddress2
);
1069 ok(((ULONG_PTR
)BaseAddress1
- (ULONG_PTR
)BaseAddress2
) == 0x10000,
1070 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1
, BaseAddress2
);
1074 START_TEST(NtMapViewOfSection
)
1076 Test_PageFileSection();
1077 Test_ImageSection();
1078 Test_BasedSection();