ReactOS 0.4.16-dev-1489-g8fbbb41
MmSection.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite Section Object test
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8#include <kmt_test.h>
9
10#define CheckObject(Handle, Pointers, Handles) do \
11{ \
12 PUBLIC_OBJECT_BASIC_INFORMATION ObjectInfo; \
13 Status = ZwQueryObject(Handle, ObjectBasicInformation, \
14 &ObjectInfo, sizeof ObjectInfo, NULL); \
15 ok_eq_hex(Status, STATUS_SUCCESS); \
16 ok_eq_ulong(ObjectInfo.PointerCount, Pointers); \
17 ok_eq_ulong(ObjectInfo.HandleCount, Handles); \
18} while (0)
19
20static
21void
23{
25 HANDLE SectionHandle = NULL;
27
28 if (skip(SectionObject != NULL &&
29 SectionObject != (PVOID)0x5555555555555555ULL,
30 "No section object\n"))
31 return;
32
35 KernelMode, &SectionHandle);
36 ok(Status == STATUS_SUCCESS, "Line %lu: Status = 0x%lx, expected STATUS_SUCCESS\n", Line, Status);
37 ok(SectionHandle != NULL, "Line %lu: Section handle null\n", Line);
38 if (!skip(NT_SUCCESS(Status) && SectionHandle,
39 "No section handle\n"))
40 {
42 &Sbi, sizeof Sbi, NULL);
43 ok(Status == STATUS_SUCCESS, "Line %lu: Status = 0x%lx, expected STATUS_SUCCESS\n", Line, Status);
44 ok(Sbi.BaseAddress == NULL, "Line %lu: Sbi.BaseAddress = 0x%p, expected NULL\n", Line, Sbi.BaseAddress);
45 ok(Sbi.Size.QuadPart == 1LL, "Line %lu: Sbi.Size.QuadPart = 0x%I64x, expected 1\n", Line, Sbi.Size.QuadPart);
46 ok(Sbi.Attributes == (SectionFlag | SEC_FILE), "Line %lu: Sbi.Attributes = 0x%lx, expected 0x%x\n", Line, Sbi.Attributes, (SectionFlag | SEC_FILE));
47 ZwClose(SectionHandle);
48 }
49}
50#define CheckSection(SectionObject, SectionFlag) CheckSection_(__LINE__, SectionObject, SectionFlag)
51
52#define TestMapView(SectionObject, ExpectAtBase, ExpectM) do \
53{ \
54 NTSTATUS Status; \
55 PVOID BaseAddress = NULL; \
56 SIZE_T ViewSize = 0; \
57 LARGE_INTEGER SectionOffset; \
58 if (skip(SectionObject != NULL && \
59 SectionObject != (PVOID)0x5555555555555555ULL, \
60 "No section object\n")) \
61 break; \
62 \
63 SectionOffset.QuadPart = 0; \
64 Status = MmMapViewOfSection(SectionObject, PsGetCurrentProcess(), \
65 &BaseAddress, 0, 1, &SectionOffset, \
66 &ViewSize, ViewUnmap, 0, PAGE_READONLY); \
67 ok_eq_hex(Status, ExpectAtBase ? STATUS_SUCCESS : STATUS_IMAGE_NOT_AT_BASE);\
68 if (!skip(NT_SUCCESS(Status), "Section not mapped\n")) \
69 { \
70 ok((LONG_PTR)BaseAddress > 0, "BaseAddress = %p\n", BaseAddress); \
71 ok_eq_uint(*(PUCHAR)BaseAddress, ExpectM ? 'M' : 0); \
72 Status = MmUnmapViewOfSection(PsGetCurrentProcess(), BaseAddress); \
73 ok_eq_hex(Status, STATUS_SUCCESS); \
74 } \
75 BaseAddress = NULL; \
76 ViewSize = 0; \
77 Status = MmMapViewOfSection(SectionObject, PsGetCurrentProcess(), \
78 &BaseAddress, 0, 1, &SectionOffset, \
79 &ViewSize, ViewUnmap, 0, \
80 PAGE_READONLY | PAGE_NOCACHE); \
81 ok_eq_hex(Status, ExpectAtBase ? STATUS_SUCCESS : STATUS_IMAGE_NOT_AT_BASE);\
82 if (!skip(NT_SUCCESS(Status), "Section not mapped\n")) \
83 { \
84 ok((LONG_PTR)BaseAddress > 0, "BaseAddress = %p\n", BaseAddress); \
85 ok_eq_uint(*(PUCHAR)BaseAddress, ExpectM ? 'M' : 0); \
86 Status = MmUnmapViewOfSection(PsGetCurrentProcess(), BaseAddress); \
87 ok_eq_hex(Status, STATUS_SUCCESS); \
88 } \
89} while (0)
90
91static
92VOID
94 IN HANDLE FileHandle1,
95 IN PFILE_OBJECT FileObject1,
96 IN HANDLE FileHandle2,
97 IN PFILE_OBJECT FileObject2)
98{
99 NTSTATUS Status = STATUS_SUCCESS, ExceptionStatus = STATUS_SUCCESS;
102 ULONG PointerCount1, PointerCount2;
103
105 {
107 }
109 {
110 ExceptionStatus = _SEH2_GetExceptionCode();
111 }
112 _SEH2_END;
113
115 {
116 ok_eq_hex(ExceptionStatus, STATUS_SUCCESS);
118 }
119 else
120 {
121 ok_eq_hex(ExceptionStatus, STATUS_ACCESS_VIOLATION);
123 }
124
126 {
127 /* PAGE_NOACCESS and missing SEC_RESERVE/SEC_COMMIT/SEC_IMAGE assert */
131
135 }
136
139 {
141 }
143 {
144 ExceptionStatus = _SEH2_GetExceptionCode();
145 }
146 _SEH2_END;
147
149 {
150 ok_eq_hex(ExceptionStatus, STATUS_SUCCESS);
152 }
153 else
154 {
155 ok_eq_hex(ExceptionStatus, STATUS_ACCESS_VIOLATION);
157 }
159
162
166
172
175
177 MaximumSize.QuadPart = 0;
182 ok_eq_longlong(MaximumSize.QuadPart, 0LL);
184
187
188 MaximumSize.QuadPart = 0;
193 ok_eq_longlong(MaximumSize.QuadPart, 0LL);
194
197
198 MaximumSize.QuadPart = -1;
203 ok_eq_longlong(MaximumSize.QuadPart, -1LL);
204
207
208 MaximumSize.QuadPart = 1;
212 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
213
216
218 MaximumSize.QuadPart = 0;
223 ok_eq_longlong(MaximumSize.QuadPart, 0LL);
225
228
229 /* page file section */
231 MaximumSize.QuadPart = 1;
236 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
237 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
238 ok(SectionObject != NULL, "Section object pointer NULL\n");
239
242
243 if (!skip(FileHandle1 != NULL && FileObject1 != NULL &&
244 FileHandle2 != NULL && FileObject2 != NULL, "No file handle or object\n"))
245 {
247 {
248 PointerCount1 = 3;
249 PointerCount2 = 3;
250 }
251 else
252 {
253#ifdef _M_IX86
254 PointerCount1 = 31;
255 PointerCount2 = 33;
256#else
257 PointerCount1 = 32767;
258 PointerCount2 = 32769;
259#endif
260 }
261 /* image section */
262 CheckObject(FileHandle2, PointerCount2, 1L);
264 MaximumSize.QuadPart = 1;
269 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
270 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
271 ok(SectionObject != NULL, "Section object pointer NULL\n");
273 PointerCount2 -= 2;
274 CheckObject(FileHandle2, PointerCount2, 1L);
278
281
283 PointerCount2--;
284 CheckObject(FileHandle2, PointerCount2, 1L);
286 MaximumSize.QuadPart = 1;
292 else
294 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
296 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
297 ok(SectionObject != NULL, "Section object pointer NULL\n");
299 ++PointerCount2;
300 else if (GetNTVersion() >= _WIN32_WINNT_WIN8)
301 --PointerCount2;
302 CheckObject(FileHandle2, PointerCount2, 1L);
305
308
310 PointerCount2--;
311 CheckObject(FileHandle2, PointerCount2, 1L);
313 MaximumSize.QuadPart = 1;
315 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, FileHandle2, FileObject2);
319 else
321 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
323 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
324 ok(SectionObject != NULL, "Section object pointer NULL\n");
326 PointerCount2--;
327 CheckObject(FileHandle2, PointerCount2, 1L);
330
333
334 /* image section with inappropriate file */
335 CheckObject(FileHandle1, PointerCount1, 1L);
337 MaximumSize.QuadPart = 1;
342 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
345 PointerCount1 -= 2;
346 CheckObject(FileHandle1, PointerCount1, 1L);
347
350
352 PointerCount1--;
353 CheckObject(FileHandle1, PointerCount1, 1L);
355 MaximumSize.QuadPart = 1;
361 else
363 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
365 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
366 ok(SectionObject != NULL, "Section object pointer NULL\n");
368 ++PointerCount1;
369 else if (GetNTVersion() >= _WIN32_WINNT_WIN8)
370 --PointerCount1;
371 CheckObject(FileHandle1, PointerCount1, 1L);
374
377
379 --PointerCount1;
380 CheckObject(FileHandle1, PointerCount1, 1L);
382 MaximumSize.QuadPart = 1;
384 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, FileHandle1, FileObject1);
388 else
390 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
392 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
393 ok(SectionObject != NULL, "Section object pointer NULL\n");
395 --PointerCount1;
396 CheckObject(FileHandle1, PointerCount1, 1L);
399
402
403 /* image section with two different files */
405 --PointerCount1;
406 CheckObject(FileHandle1, PointerCount1, 1L);
408 MaximumSize.QuadPart = 1;
410 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, FileHandle1, FileObject2);
414 else
416 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
418 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
419 ok(SectionObject != NULL, "Section object pointer NULL\n");
421 {
422 --PointerCount1;
423 --PointerCount2;
424 }
425 CheckObject(FileHandle1, PointerCount1, 1L);
426 CheckObject(FileHandle2, PointerCount2, 1L);
429
432
434 --PointerCount1;
435 CheckObject(FileHandle1, PointerCount1, 1L);
437 MaximumSize.QuadPart = 1;
439 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, FileHandle2, FileObject1);
443 else
445 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
447 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
448 ok(SectionObject != NULL, "Section object pointer NULL\n");
450 {
451 --PointerCount1;
452 --PointerCount2;
453 }
454 CheckObject(FileHandle1, PointerCount1, 1L);
455 CheckObject(FileHandle2, PointerCount2, 1L);
458
461
462 /* data file section */
464 --PointerCount1;
465 CheckObject(FileHandle1, PointerCount1, 1L);
467 MaximumSize.QuadPart = 1;
472 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
473 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
474 ok(SectionObject != NULL, "Section object pointer NULL\n");
476 PointerCount1 -= 2;
477 CheckObject(FileHandle1, PointerCount1, 1L);
481
484
486 --PointerCount1;
487 CheckObject(FileHandle1, PointerCount1, 1L);
489 MaximumSize.QuadPart = 1;
493 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
494 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
495 ok(SectionObject != NULL, "Section object pointer NULL\n");
497 --PointerCount1;
498 CheckObject(FileHandle1, PointerCount1, 1L);
502
505
507 --PointerCount1;
508 CheckObject(FileHandle1, PointerCount1, 1L);
510 MaximumSize.QuadPart = 1;
512 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_RESERVE, FileHandle1, FileObject1);
515 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
516 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
517 ok(SectionObject != NULL, "Section object pointer NULL\n");
519 --PointerCount1;
520 CheckObject(FileHandle1, PointerCount1, 1L);
524
527
529 --PointerCount1;
530 CheckObject(FileHandle1, PointerCount1, 1L);
531 }
532}
533
534static
535VOID
537{
539 UNICODE_STRING SectionName = RTL_CONSTANT_STRING(L"\\Device\\PhysicalMemory");
541 HANDLE SectionHandle;
543 PUCHAR MyPage;
544 PHYSICAL_ADDRESS MyPagePhysical;
545 PUCHAR ZeroPageContents;
546 PHYSICAL_ADDRESS ZeroPagePhysical;
549 SYSTEM_BASIC_INFORMATION BasicInfo;
550 PUCHAR MappingBytes;
552 SIZE_T EqualBytes;
553 struct
554 {
558 } *UserStruct;
559 PVOID UserMem;
560 SIZE_T UserSize;
561
563 if (skip(MyPage != NULL, "Out of memory\n"))
564 return;
565 MyPagePhysical = MmGetPhysicalAddress(MyPage);
566 RtlFillMemory(MyPage + 0 * PAGE_SIZE / 4, PAGE_SIZE / 4, 0x23);
567 RtlFillMemory(MyPage + 1 * PAGE_SIZE / 4, PAGE_SIZE / 4, 0x67);
568 RtlFillMemory(MyPage + 2 * PAGE_SIZE / 4, PAGE_SIZE / 4, 0xab);
569 RtlFillMemory(MyPage + 3 * PAGE_SIZE / 4, PAGE_SIZE / 4, 0xef);
570
571 ZeroPageContents = ExAllocatePoolWithTag(PagedPool, PAGE_SIZE, 'ZPmK');
572 if (skip(ZeroPageContents != NULL, "Out of memory\n"))
573 {
574 ExFreePoolWithTag(MyPage, 'MPmK');
575 return;
576 }
577 ZeroPagePhysical.QuadPart = 0;
578
579 Mapping = MmMapIoSpace(ZeroPagePhysical, PAGE_SIZE, MmCached);
580 if (skip(Mapping != NULL, "Failed to map zero page\n"))
581 {
582 ExFreePoolWithTag(ZeroPageContents, 'ZPmK');
583 ExFreePoolWithTag(MyPage, 'MPmK');
584 return;
585 }
586
587 RtlCopyMemory(ZeroPageContents, Mapping, PAGE_SIZE);
589
591 &SectionName,
592 0,
593 NULL,
594 NULL);
597 if (!skip(NT_SUCCESS(Status), "No section\n"))
598 {
599 /* Map zero page and compare */
600 Mapping = NULL;
602 Status = ZwMapViewOfSection(SectionHandle,
604 &Mapping,
605 0,
606 0,
607 &ZeroPagePhysical,
608 &ViewSize,
609 ViewUnmap,
610 0,
613 if (!skip(NT_SUCCESS(Status), "No view\n"))
614 {
615 ok((LONG_PTR)Mapping > 0, "Mapping = %p\n", Mapping);
616 EqualBytes = RtlCompareMemory(Mapping,
617 ZeroPageContents,
618 PAGE_SIZE);
619 ok_eq_size(EqualBytes, PAGE_SIZE);
620 Status = ZwUnmapViewOfSection(ZwCurrentProcess(), Mapping);
622 }
623
624 /* Map the zero page non-cached */
625 Mapping = NULL;
627 Status = ZwMapViewOfSection(SectionHandle,
629 &Mapping,
630 0,
631 0,
632 &ZeroPagePhysical,
633 &ViewSize,
634 ViewUnmap,
635 0,
638 if (!skip(NT_SUCCESS(Status), "No view\n"))
639 {
640 ok((LONG_PTR)Mapping > 0, "Mapping = %p\n", Mapping);
641 EqualBytes = RtlCompareMemory(Mapping,
642 ZeroPageContents,
643 PAGE_SIZE);
644 ok_eq_size(EqualBytes, PAGE_SIZE);
645 Status = ZwUnmapViewOfSection(ZwCurrentProcess(), Mapping);
647 }
648
649 /* Map our NP page, compare, and check that modifications are reflected */
650 Mapping = NULL;
652 Status = ZwMapViewOfSection(SectionHandle,
654 &Mapping,
655 0,
656 0,
657 &MyPagePhysical,
658 &ViewSize,
659 ViewUnmap,
660 0,
663 if (!skip(NT_SUCCESS(Status), "No view\n"))
664 {
665 ok((LONG_PTR)Mapping > 0, "Mapping = %p\n", Mapping);
666 EqualBytes = RtlCompareMemory(Mapping,
667 MyPage,
668 PAGE_SIZE);
669 ok_eq_size(EqualBytes, PAGE_SIZE);
670
671 MappingBytes = Mapping;
672 ok(MappingBytes[5] == 0x23, "Mapping[5] = 0x%x\n", MappingBytes[5]);
673 ok(MyPage[5] == 0x23, "MyPage[5] = 0x%x\n", MyPage[5]);
674
675 MyPage[5] = 0x44;
676 ok(MappingBytes[5] == 0x44, "Mapping[5] = 0x%x\n", MappingBytes[5]);
677 ok(MyPage[5] == 0x44, "MyPage[5] = 0x%x\n", MyPage[5]);
678
679 MappingBytes[5] = 0x88;
680 ok(MappingBytes[5] == 0x88, "Mapping[5] = 0x%x\n", MappingBytes[5]);
681 ok(MyPage[5] == 0x88, "MyPage[5] = 0x%x\n", MyPage[5]);
682
683 Status = ZwUnmapViewOfSection(ZwCurrentProcess(), Mapping);
685 }
686
687 /* Unaligned mapping will get aligned automatically */
688 Mapping = NULL;
689 ViewSize = PAGE_SIZE - 4;
690 PhysicalAddress.QuadPart = MyPagePhysical.QuadPart + 4;
691 Status = ZwMapViewOfSection(SectionHandle,
693 &Mapping,
694 0,
695 0,
697 &ViewSize,
698 ViewUnmap,
699 0,
702 if (!skip(NT_SUCCESS(Status), "No view\n"))
703 {
704 ok((LONG_PTR)Mapping > 0, "Mapping = %p\n", Mapping);
705 ok(((ULONG_PTR)Mapping % PAGE_SIZE) == 0, "Mapping = %p\n", Mapping);
708
709 EqualBytes = RtlCompareMemory(Mapping,
710 MyPage,
711 PAGE_SIZE);
712 ok_eq_size(EqualBytes, PAGE_SIZE);
713
714 Status = ZwUnmapViewOfSection(ZwCurrentProcess(), Mapping);
716 }
717
718 /* The following tests need to pass parameters in user mode */
719 UserStruct = NULL;
720 UserMem = NULL;
721 UserSize = PAGE_SIZE;
722 Status = ZwAllocateVirtualMemory(ZwCurrentProcess(),
723 &UserMem,
724 0,
725 &UserSize,
729 if (NT_SUCCESS(Status))
730 UserStruct = UserMem;
731
732 /* Find highest physical page -- only kernel can map beyond this */
734 &BasicInfo,
735 sizeof(BasicInfo),
736 NULL);
738 trace("HighestPhysicalPageNumber: %lx\n", BasicInfo.HighestPhysicalPageNumber);
739
740 /* Start one page before highest physical -- succeeds for user/kernel */
741 Mapping = NULL;
744 Status = ZwMapViewOfSection(SectionHandle,
746 &Mapping,
747 0,
748 0,
750 &ViewSize,
751 ViewUnmap,
752 0,
755 if (NT_SUCCESS(Status))
756 ZwUnmapViewOfSection(ZwCurrentProcess(), Mapping);
757
758 /* Repeat from user mode */
759 if (!skip(UserStruct != NULL, "No user memory\n"))
760 {
762 UserStruct->Mapping = NULL;
763 UserStruct->PhysicalAddress.QuadPart = PhysicalAddress.QuadPart;
764 UserStruct->ViewSize = PAGE_SIZE;
766
767 Status = NtMapViewOfSection(SectionHandle,
769 &UserStruct->Mapping,
770 0,
771 0,
772 &UserStruct->PhysicalAddress,
773 &UserStruct->ViewSize,
774 ViewUnmap,
775 0,
778 if (NT_SUCCESS(Status))
779 {
781 ZwUnmapViewOfSection(ZwCurrentProcess(), UserStruct->Mapping);
783 }
784 }
785
786 /* Now start at highest physical -- fails for user */
787 Mapping = NULL;
790 Status = ZwMapViewOfSection(SectionHandle,
792 &Mapping,
793 0,
794 0,
796 &ViewSize,
797 ViewUnmap,
798 0,
801 if (NT_SUCCESS(Status))
802 ZwUnmapViewOfSection(ZwCurrentProcess(), Mapping);
803
804 /* Repeat from user mode */
805 if (!skip(UserStruct != NULL, "No user memory\n"))
806 {
808 UserStruct->Mapping = NULL;
809 UserStruct->PhysicalAddress.QuadPart = PhysicalAddress.QuadPart;
810 UserStruct->ViewSize = PAGE_SIZE;
812
813 Status = NtMapViewOfSection(SectionHandle,
815 &UserStruct->Mapping,
816 0,
817 0,
818 &UserStruct->PhysicalAddress,
819 &UserStruct->ViewSize,
820 ViewUnmap,
821 0,
825 else
827 if (NT_SUCCESS(Status))
828 {
830 ZwUnmapViewOfSection(ZwCurrentProcess(), UserStruct->Mapping);
832 }
833 }
834
835 /* End of view crosses highest physical -- fails for user */
836 Mapping = NULL;
837 ViewSize = 2 * PAGE_SIZE;
839 Status = ZwMapViewOfSection(SectionHandle,
841 &Mapping,
842 0,
843 0,
845 &ViewSize,
846 ViewUnmap,
847 0,
850 if (NT_SUCCESS(Status))
851 ZwUnmapViewOfSection(ZwCurrentProcess(), Mapping);
852
853 /* Repeat from user mode */
854 if (!skip(UserStruct != NULL, "No user memory\n"))
855 {
857 UserStruct->Mapping = NULL;
858 UserStruct->PhysicalAddress.QuadPart = PhysicalAddress.QuadPart;
859 UserStruct->ViewSize = 2 * PAGE_SIZE;
861
862 Status = NtMapViewOfSection(SectionHandle,
864 &UserStruct->Mapping,
865 0,
866 0,
867 &UserStruct->PhysicalAddress,
868 &UserStruct->ViewSize,
869 ViewUnmap,
870 0,
874 else
876 if (NT_SUCCESS(Status))
877 {
879 ZwUnmapViewOfSection(ZwCurrentProcess(), UserStruct->Mapping);
881 }
882 }
883
884 /* Free user memory and close section */
885 if (!skip(UserStruct != NULL, "No user memory\n"))
886 {
887 UserSize = 0;
888 Status = ZwFreeVirtualMemory(ZwCurrentProcess(),
889 &UserMem,
890 &UserSize,
893 }
894
895 Status = ObCloseHandle(SectionHandle, UserMode);
897 }
898
899 /* Try flag 0x80000000, which ROS calls SEC_PHYSICALMEMORY */
901 NULL,
903 NULL,
904 NULL);
905 Status = ZwCreateSection(&SectionHandle,
908 NULL,
910 0x80000000,
911 NULL);
913 if (NT_SUCCESS(Status))
914 ZwClose(SectionHandle);
915
916 /* Assertion failure: AllocationAttributes & SEC_IMAGE | SEC_RESERVE | SEC_COMMIT */
918 {
920 NULL,
922 NULL,
923 NULL);
927 NULL,
929 0x80000000,
930 NULL,
931 NULL);
933 if (NT_SUCCESS(Status))
935 }
936
938 {
940 NULL,
942 NULL,
943 NULL);
947 NULL,
949 SEC_RESERVE | 0x80000000,
950 NULL,
951 NULL);
953 if (NT_SUCCESS(Status))
955 }
956
957 ExFreePoolWithTag(ZeroPageContents, 'ZPmK');
958 ExFreePoolWithTag(MyPage, 'MPmK');
959}
960
961START_TEST(MmSection)
962{
964 HANDLE FileHandle1 = NULL, FileHandle2 = NULL;
965 PFILE_OBJECT FileObject1 = NULL, FileObject2 = NULL;
968 UNICODE_STRING FileName1 = RTL_CONSTANT_STRING(L"\\SystemRoot\\kmtest-MmSection.txt");
969 UNICODE_STRING FileName2 = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\ntdll.dll");
971 UCHAR FileData = 0;
972
973 ok(ExGetPreviousMode() == UserMode, "Previous mode is kernel mode\n");
974 /* create a one-byte file that we can use */
979 ok(FileHandle1 != NULL, "FileHandle1 is NULL\n");
980 if (FileHandle1)
981 {
982 FileOffset.QuadPart = 0;
983 Status = ZwWriteFile(FileHandle1, NULL, NULL, NULL, &IoStatusBlock, &FileData, sizeof FileData, &FileOffset, NULL);
984 ok(Status == STATUS_SUCCESS || Status == STATUS_PENDING, "Status = 0x%08lx\n", Status);
985 Status = ZwWaitForSingleObject(FileHandle1, FALSE, NULL);
988 Status = ZwClose(FileHandle1);
990 FileHandle1 = NULL;
991 }
992
997 ok(FileHandle1 != NULL, "FileHandle1 is NULL\n");
999 CheckObject(FileHandle1, 2L, 1L);
1000#ifdef _M_IX86
1001 else
1002 CheckObject(FileHandle1, 33L, 1L);
1003#else
1004 else
1005 CheckObject(FileHandle1, 32769L, 1L);
1006#endif
1007
1012 ok(FileHandle2 != NULL, "FileHandle2 is NULL\n");
1013
1014 if (!skip(Status == STATUS_SUCCESS && FileHandle1 != NULL, "Failed to open file 1\n"))
1015 {
1018 ok(FileObject1 != NULL, "FileObject1 is NULL\n");
1020 CheckObject(FileHandle1, 3L, 1L);
1021#ifdef _M_IX86
1022 else
1023 CheckObject(FileHandle1, 32L, 1L);
1024#else
1025 else
1026 CheckObject(FileHandle1, 32768L, 1L);
1027#endif
1028 }
1029
1030 if (!skip(Status == STATUS_SUCCESS && FileHandle2 != NULL, "Failed to open file 2\n"))
1031 {
1034 ok(FileObject2 != NULL, "FileObject2 is NULL\n");
1035 }
1036
1037 trace("FileHandle1=%p, FileObject1=%p\n", FileHandle1, FileObject1);
1038 trace("FileHandle2=%p, FileObject2=%p\n", FileHandle2, FileObject2);
1039 TestCreateSection(FileHandle1, FileObject1, FileHandle2, FileObject2);
1040
1041 if (FileObject2)
1042 ObDereferenceObject(FileObject2);
1043 if (FileObject1)
1044 ObDereferenceObject(FileObject1);
1045 if (FileHandle2)
1046 ZwClose(FileHandle2);
1047 if (FileHandle1)
1048 ZwClose(FileHandle1);
1049
1051}
NTSTATUS NTAPI NtMapViewOfSection(IN HANDLE SectionHandle, IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, IN SIZE_T CommitSize, IN OUT PLARGE_INTEGER SectionOffset OPTIONAL, IN OUT PSIZE_T ViewSize, IN SECTION_INHERIT InheritDisposition, IN ULONG AllocationType, IN ULONG Protect)
Definition: section.c:3257
static VOID TestCreateSection(IN HANDLE FileHandle1, IN PFILE_OBJECT FileObject1, IN HANDLE FileHandle2, IN PFILE_OBJECT FileObject2)
Definition: MmSection.c:93
#define TestMapView(SectionObject, ExpectAtBase, ExpectM)
Definition: MmSection.c:52
static VOID TestPhysicalMemorySection(VOID)
Definition: MmSection.c:536
#define CheckObject(Handle, Pointers, Handles)
Definition: MmSection.c:10
#define CheckSection(SectionObject, SectionFlag)
Definition: MmSection.c:50
static void CheckSection_(ULONG Line, PVOID SectionObject, ULONG SectionFlag)
Definition: MmSection.c:22
static const WCHAR FileName2[]
#define ok_eq_pointer(value, expected)
Definition: apitest.h:44
#define ok_eq_hex(value, expected)
Definition: apitest.h:62
#define ok_eq_ulong(value, expected)
Definition: apitest.h:48
#define ok_eq_longlong(value, expected)
Definition: apitest.h:49
#define GetNTVersion()
Definition: apitest.h:17
#define ok_eq_size(value, expected)
Definition: apitest.h:54
#define ok_eq_ulongptr(value, expected)
Definition: apitest.h:56
#define trace
Definition: atltest.h:70
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
LONG NTSTATUS
Definition: precomp.h:26
#define FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
#define FILE_DELETE_ON_CLOSE
Definition: constants.h:494
static FILEDATA FileData[MAX_FDS]
Definition: fs.c:51
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:160
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define STATUS_PENDING
Definition: d3dkmdt.h:43
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define PAGE_READONLY
Definition: compat.h:138
#define GENERIC_READ
Definition: compat.h:135
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
#define L(x)
Definition: resources.c:13
static PVOID Mapping[EMS_PHYSICAL_PAGES]
Definition: emsdrv.c:41
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
#define NonPagedPool
Definition: env_spec_w32.h:307
#define PagedPool
Definition: env_spec_w32.h:308
#define ExGetPreviousMode
Definition: ex.h:143
NTSYSAPI NTSTATUS NTAPI ZwQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
@ SystemBasicInformation
Definition: ntddk_ex.h:11
#define FILE_OPEN
Definition: from_kernel.h:54
#define FILE_SUPERSEDE
Definition: from_kernel.h:53
_Must_inspect_result_ _Outptr_ PVOID * SectionObject
Definition: fsrtlfuncs.h:860
Status
Definition: gdiplustypes.h:25
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:599
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
POBJECT_TYPE IoFileObjectType
Definition: iomgr.c:36
VOID NTAPI MmUnmapIoSpace(IN PVOID BaseAddress, IN SIZE_T NumberOfBytes)
Definition: iosup.c:193
PVOID NTAPI MmMapIoSpace(IN PHYSICAL_ADDRESS PhysicalAddress, IN SIZE_T NumberOfBytes, IN MEMORY_CACHING_TYPE CacheType)
Definition: iosup.c:47
#define KmtStartSeh()
Definition: kmt_test.h:285
#define KmtInvalidPointer
Definition: kmt_test.h:283
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:291
BOOLEAN KmtIsCheckedBuild
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
#define KernelMode
Definition: asm.h:38
#define UserMode
Definition: asm.h:39
NTSYSAPI NTSTATUS NTAPI ZwOpenSection(_Out_ PHANDLE SectionHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes)
NTSYSAPI NTSTATUS NTAPI ZwQuerySection(_In_ HANDLE SectionHandle, _In_ SECTION_INFORMATION_CLASS SectionInformationClass, _Out_ PVOID SectionInformation, _In_ SIZE_T Length, _Out_opt_ PSIZE_T ResultLength)
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:362
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:408
#define SEC_IMAGE
Definition: mmtypes.h:97
#define SEC_FILE
Definition: mmtypes.h:96
@ SectionBasicInformation
Definition: mmtypes.h:195
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define SYNCHRONIZE
Definition: nt_native.h:61
#define FILE_WRITE_DATA
Definition: nt_native.h:631
#define FILE_READ_DATA
Definition: nt_native.h:628
#define PAGE_NOCACHE
Definition: nt_native.h:1311
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define SEC_RESERVE
Definition: nt_native.h:1323
#define FILE_CREATED
Definition: nt_native.h:770
#define NtCurrentProcess()
Definition: nt_native.h:1657
@ ViewUnmap
Definition: nt_native.h:1279
#define GENERIC_ALL
Definition: nt_native.h:92
#define FILE_OPENED
Definition: nt_native.h:769
#define MEM_RELEASE
Definition: nt_native.h:1316
#define GENERIC_WRITE
Definition: nt_native.h:90
#define MEM_COMMIT
Definition: nt_native.h:1313
#define PAGE_NOACCESS
Definition: nt_native.h:1302
PHYSICAL_ADDRESS NTAPI MmGetPhysicalAddress(IN PVOID Address)
Definition: stubs.c:685
#define STATUS_INVALID_PARAMETER_4
Definition: ntstatus.h:478
#define STATUS_INVALID_IMAGE_NOT_MZ
Definition: ntstatus.h:539
#define STATUS_INVALID_FILE_FOR_SECTION
Definition: ntstatus.h:269
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define STATUS_INVALID_PARAMETER_6
Definition: ntstatus.h:480
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:305
#define STATUS_SECTION_TOO_BIG
Definition: ntstatus.h:300
NTSTATUS NTAPI ObCloseHandle(IN HANDLE Handle, IN KPROCESSOR_MODE AccessMode)
Definition: obhandle.c:3379
NTSTATUS NTAPI ObOpenObjectByPointer(IN PVOID Object, IN ULONG HandleAttributes, IN PACCESS_STATE PassedAccessState, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PHANDLE Handle)
Definition: obhandle.c:2742
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:494
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:181
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:82
#define _SEH2_END
Definition: pseh2_64.h:171
#define _SEH2_TRY
Definition: pseh2_64.h:71
#define _WIN32_WINNT_WIN10
Definition: sdkddkver.h:32
#define _WIN32_WINNT_WS03
Definition: sdkddkver.h:23
#define _WIN32_WINNT_WIN8
Definition: sdkddkver.h:29
NTSTATUS NTAPI MmCreateSection(OUT PVOID *Section, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN PLARGE_INTEGER MaximumSize, IN ULONG SectionPageProtection, IN ULONG AllocationAttributes, IN HANDLE FileHandle OPTIONAL, IN PFILE_OBJECT FileObject OPTIONAL)
Definition: section.c:4671
POBJECT_TYPE MmSectionObjectType
Definition: section.c:192
#define STATUS_SUCCESS
Definition: shellext.h:65
Definition: ncftp.h:79
LARGE_INTEGER Size
Definition: mmtypes.h:336
#define LL
Definition: tui.h:166
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
uint64_t ULONGLONG
Definition: typedefs.h:67
LONGLONG QuadPart
Definition: typedefs.h:114
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS PhysicalAddress
Definition: iotypes.h:1098
* PFILE_OBJECT
Definition: iotypes.h:1998
@ MmCached
Definition: mmtypes.h:130
#define ObDereferenceObject
Definition: obfuncs.h:203
unsigned char UCHAR
Definition: xmlstorage.h:181
#define ZwCurrentProcess()