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", 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", 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");
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", 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", 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", 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
);
681 /* Create a data section with write access */
682 Status
= NtCreateSection(&DataSectionHandle
,
683 SECTION_ALL_ACCESS
, // DesiredAccess
684 NULL
, // ObjectAttributes
686 PAGE_READWRITE
, // SectionPageProtection
687 SEC_COMMIT
, // AllocationAttributes
689 ok_ntstatus(Status
, STATUS_SUCCESS
);
691 /* Map the data section as flat mapping */
694 Status
= NtMapViewOfSection(DataSectionHandle
,
704 ok_ntstatus(Status
, STATUS_SUCCESS
);
705 //ok(ViewSize == 0x3f95cc48, "ViewSize wrong: 0x%lx\n");
707 /* Check the original data */
708 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
710 /* Modify the PE header (but do not flush!) */
711 *(ULONG
*)DataBase
= 0xdeadbabe;
712 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
714 /* Modify data in the .data section (but do not flush!) */
715 ok(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800) == 0x12345678,
716 "Data in .data section invalid: 0x%lx!\n", *(ULONG
*)((PUCHAR
)DataBase
+ 0x800));
717 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x87654321;
719 /* Now try to create an image section (should fail) */
720 Status
= NtCreateSection(&ImageSectionHandle
,
721 SECTION_ALL_ACCESS
, // DesiredAccess
722 NULL
, // ObjectAttributes
724 PAGE_READWRITE
, // SectionPageProtection
725 SEC_IMAGE
, // AllocationAttributes
727 ok_ntstatus(Status
, STATUS_INVALID_IMAGE_NOT_MZ
);
729 /* Restore the original header */
730 *(ULONG
*)DataBase
= 0x00905a4d;
732 /* Modify data in the .data section (but do not flush!) */
733 ok_hex(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800), 0x87654321);
734 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xdeadbabe;
736 /* Try to create an image section again */
737 Status
= NtCreateSection(&ImageSectionHandle
,
738 SECTION_ALL_ACCESS
, // DesiredAccess
739 NULL
, // ObjectAttributes
741 PAGE_READWRITE
, // SectionPageProtection
742 SEC_IMAGE
, // AllocationAttributes
744 ok_ntstatus(Status
, STATUS_SUCCESS
);
746 /* Map the image section */
749 Status
= NtMapViewOfSection(ImageSectionHandle
,
754 NULL
, // SectionOffset
760 ok_ntstatus(Status
, STATUS_IMAGE_NOT_AT_BASE
);
762 ok_ntstatus(Status
, STATUS_IMAGE_MACHINE_TYPE_MISMATCH
);
765 /* Check the header */
766 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
767 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Header not ok\n");
769 /* Check the data section. Either of these can be present! */
770 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
771 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
772 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
774 /* Now modify the data again */
775 *(ULONG
*)DataBase
= 0xdeadbabe;
776 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xf00dada;
779 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
780 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
781 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
782 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
783 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
787 Status
= NtFlushVirtualMemory(NtCurrentProcess(),
791 ok_ntstatus(Status
, STATUS_SUCCESS
);
793 /* Check the data again */
794 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
795 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
796 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
797 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
799 /* Restore the original header */
800 *(ULONG
*)DataBase
= 0x00905a4d;
801 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
803 /* Close the image mapping */
804 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
805 NtClose(ImageSectionHandle
);
807 /* Create an image section again */
808 Status
= NtCreateSection(&ImageSectionHandle
,
809 SECTION_ALL_ACCESS
, // DesiredAccess
810 NULL
, // ObjectAttributes
812 PAGE_READWRITE
, // SectionPageProtection
813 SEC_IMAGE
, // AllocationAttributes
815 ok_ntstatus(Status
, STATUS_SUCCESS
);
817 /* Map the image section again */
820 Status
= NtMapViewOfSection(ImageSectionHandle
,
831 ok_ntstatus(Status
, STATUS_IMAGE_NOT_AT_BASE
);
833 ok_ntstatus(Status
, STATUS_IMAGE_MACHINE_TYPE_MISMATCH
);
836 // This one doesn't always work, needs investigation
837 /* Check the .data section again */
838 //ok(*(ULONG*)((PUCHAR)ImageBase + 0x80000) == 0xf00dada,
839 // "Data should be synced: 0x%lx!\n", *(ULONG*)((PUCHAR)ImageBase + 0x80000));
841 /* Restore the original data */
842 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x12345678;
844 /* Close the data mapping */
845 NtUnmapViewOfSection(NtCurrentProcess(), DataBase
);
847 NtClose(DataSectionHandle
);
849 /* Try to allocate memory inside the image mapping */
850 DataBase
= (PUCHAR
)ImageBase
+ 0x20000;
852 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &DataBase
, 0, &ViewSize
, MEM_RESERVE
, PAGE_NOACCESS
);
853 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
857 NtClose(ImageSectionHandle
);
858 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
862 Test_ImageSection2(void)
864 UNICODE_STRING FileName
;
866 OBJECT_ATTRIBUTES FileObjectAttributes
;
867 IO_STATUS_BLOCK IoStatusBlock
;
868 HANDLE FileHandle
, ImageSectionHandle
;
869 PVOID ImageBase
, BaseAddress
;
871 LARGE_INTEGER MaximumSize
, SectionOffset
;
873 if (!RtlDosPathNameToNtPathName_U(L
"testdata\\nvoglv32.dll",
878 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
882 InitializeObjectAttributes(&FileObjectAttributes
,
888 Status
= NtOpenFile(&FileHandle
,
889 GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
890 &FileObjectAttributes
,
893 FILE_SYNCHRONOUS_IO_NONALERT
);
894 ok_ntstatus(Status
, STATUS_SUCCESS
);
895 printf("Opened file with handle %p\n", FileHandle
);
897 /* Create a data section with write access */
898 MaximumSize
.QuadPart
= 0x20000;
899 Status
= NtCreateSection(&ImageSectionHandle
,
900 SECTION_ALL_ACCESS
, // DesiredAccess
901 NULL
, // ObjectAttributes
902 &MaximumSize
, // MaximumSize
903 PAGE_READWRITE
, // SectionPageProtection
904 SEC_IMAGE
, // AllocationAttributes
906 ok_ntstatus(Status
, STATUS_SUCCESS
);
908 printf("Created image section with handle %p\n", ImageSectionHandle
);
911 /* Map the image section */
914 SectionOffset
.QuadPart
= 0x00000;
915 Status
= NtMapViewOfSection(ImageSectionHandle
,
925 ok_ntstatus(Status
, STATUS_SUCCESS
);
927 printf("Mapped image section at %p, value in text section: %lx\n",
928 ImageBase
, *((ULONG
*)((PCHAR
)ImageBase
+ 0x1196)));
931 /* Try to allocate a page after the section */
932 BaseAddress
= (PUCHAR
)ImageBase
+ 0x10000;
934 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
938 MEM_RESERVE
| MEM_COMMIT
,
940 printf("allocation status: %lx\n", Status
);
945 // doesn't work with WoW64!
947 Test_BasedSection(void)
950 HANDLE SectionHandle1
, SectionHandle2
;
951 LARGE_INTEGER MaximumSize
, SectionOffset
;
952 PVOID BaseAddress1
, BaseAddress2
;
955 /* Create a based section with SEC_COMMIT */
956 MaximumSize
.QuadPart
= 0x1000;
957 Status
= NtCreateSection(&SectionHandle1
,
962 SEC_COMMIT
| SEC_BASED
,
964 ok_ntstatus(Status
, STATUS_SUCCESS
);
966 /* Map the 1st section */
968 SectionOffset
.QuadPart
= 0;
970 Status
= NtMapViewOfSection(SectionHandle1
,
981 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
983 ok_ntstatus(Status
, STATUS_SUCCESS
);
986 /* Create a 2nd based section with SEC_COMMIT */
987 MaximumSize
.QuadPart
= 0x1000;
988 Status
= NtCreateSection(&SectionHandle2
,
993 SEC_COMMIT
| SEC_BASED
,
995 ok_ntstatus(Status
, STATUS_SUCCESS
);//
997 /* Map the 2nd section */
999 SectionOffset
.QuadPart
= 0;
1001 Status
= NtMapViewOfSection(SectionHandle2
,
1012 ok_ntstatus(Status
, STATUS_CONFLICTING_ADDRESSES
);
1014 ok_ntstatus(Status
, STATUS_SUCCESS
);
1017 ok_ntstatus(Status
, STATUS_SUCCESS
);
1018 ok((ULONG_PTR
)BaseAddress2
< (ULONG_PTR
)BaseAddress1
,
1019 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1
, BaseAddress2
);
1020 ok(((ULONG_PTR
)BaseAddress1
- (ULONG_PTR
)BaseAddress2
) == 0x10000,
1021 "Invalid addresses: BaseAddress1=%p, BaseAddress2=%p\n", BaseAddress1
, BaseAddress2
);
1025 START_TEST(NtMapViewOfSection
)
1027 Test_PageFileSection();
1028 Test_ImageSection();
1029 Test_BasedSection();