[PRINTING]
[reactos.git] / rostests / kmtests / ntos_mm / MmSection.c
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
20 #define CheckSection(SectionObject, SectionFlag) do \
21 { \
22 SECTION_BASIC_INFORMATION Sbi; \
23 HANDLE SectionHandle = NULL; \
24 NTSTATUS Status; \
25 if (skip(SectionObject != NULL && \
26 SectionObject != (PVOID)0x5555555555555555ULL, \
27 "No section object\n")) \
28 break; \
29 Status = ObOpenObjectByPointer(SectionObject, OBJ_KERNEL_HANDLE, \
30 NULL, 0, MmSectionObjectType, \
31 KernelMode, &SectionHandle); \
32 ok_eq_hex(Status, STATUS_SUCCESS); \
33 ok(SectionHandle != NULL, "Section handle null\n"); \
34 if (!skip(NT_SUCCESS(Status) && SectionHandle, \
35 "No section handle\n")) \
36 { \
37 Status = ZwQuerySection(SectionHandle, SectionBasicInformation, \
38 &Sbi, sizeof Sbi, NULL); \
39 ok_eq_hex(Status, STATUS_SUCCESS); \
40 ok_eq_pointer(Sbi.BaseAddress, NULL); \
41 ok_eq_longlong(Sbi.Size.QuadPart, 1LL); \
42 ok_eq_hex(Sbi.Attributes, SectionFlag | SEC_FILE); \
43 ZwClose(SectionHandle); \
44 } \
45 } while (0)
46
47 #define TestMapView(SectionObject, ExpectAtBase, ExpectM) do \
48 { \
49 NTSTATUS Status; \
50 PVOID BaseAddress = NULL; \
51 SIZE_T ViewSize = 0; \
52 LARGE_INTEGER SectionOffset; \
53 if (skip(SectionObject != NULL && \
54 SectionObject != (PVOID)0x5555555555555555ULL, \
55 "No section object\n")) \
56 break; \
57 \
58 SectionOffset.QuadPart = 0; \
59 Status = MmMapViewOfSection(SectionObject, PsGetCurrentProcess(), \
60 &BaseAddress, 0, 1, &SectionOffset, \
61 &ViewSize, ViewUnmap, 0, PAGE_READONLY); \
62 ok_eq_hex(Status, ExpectAtBase ? STATUS_SUCCESS : STATUS_IMAGE_NOT_AT_BASE);\
63 if (!skip(NT_SUCCESS(Status), "Section not mapped\n")) \
64 { \
65 ok((LONG_PTR)BaseAddress > 0, "BaseAddress = %p\n", BaseAddress); \
66 ok_eq_uint(*(PUCHAR)BaseAddress, ExpectM ? 'M' : 0); \
67 Status = MmUnmapViewOfSection(PsGetCurrentProcess(), BaseAddress); \
68 ok_eq_hex(Status, STATUS_SUCCESS); \
69 } \
70 BaseAddress = NULL; \
71 ViewSize = 0; \
72 Status = MmMapViewOfSection(SectionObject, PsGetCurrentProcess(), \
73 &BaseAddress, 0, 1, &SectionOffset, \
74 &ViewSize, ViewUnmap, 0, \
75 PAGE_READONLY | PAGE_NOCACHE); \
76 ok_eq_hex(Status, ExpectAtBase ? STATUS_SUCCESS : STATUS_IMAGE_NOT_AT_BASE);\
77 if (!skip(NT_SUCCESS(Status), "Section not mapped\n")) \
78 { \
79 ok((LONG_PTR)BaseAddress > 0, "BaseAddress = %p\n", BaseAddress); \
80 ok_eq_uint(*(PUCHAR)BaseAddress, ExpectM ? 'M' : 0); \
81 Status = MmUnmapViewOfSection(PsGetCurrentProcess(), BaseAddress); \
82 ok_eq_hex(Status, STATUS_SUCCESS); \
83 } \
84 } while (0)
85
86 static
87 VOID
88 TestCreateSection(
89 IN HANDLE FileHandle1,
90 IN PFILE_OBJECT FileObject1,
91 IN HANDLE FileHandle2,
92 IN PFILE_OBJECT FileObject2)
93 {
94 NTSTATUS Status = STATUS_SUCCESS;
95 PVOID SectionObject;
96 LARGE_INTEGER MaximumSize;
97 ULONG PointerCount1, PointerCount2;
98
99 KmtStartSeh()
100 Status = MmCreateSection(NULL, 0, NULL, NULL, 0, SEC_RESERVE, NULL, NULL);
101 KmtEndSeh(STATUS_SUCCESS);
102 ok_eq_hex(Status, STATUS_INVALID_PAGE_PROTECTION);
103
104 if (!KmtIsCheckedBuild)
105 {
106 /* PAGE_NOACCESS and missing SEC_RESERVE/SEC_COMMIT/SEC_IMAGE assert */
107 KmtStartSeh()
108 Status = MmCreateSection(NULL, 0, NULL, NULL, PAGE_NOACCESS, SEC_RESERVE, NULL, NULL);
109 KmtEndSeh(STATUS_ACCESS_VIOLATION);
110
111 KmtStartSeh()
112 Status = MmCreateSection(NULL, 0, NULL, NULL, PAGE_NOACCESS, 0, NULL, NULL);
113 KmtEndSeh(STATUS_ACCESS_VIOLATION);
114 }
115
116 SectionObject = KmtInvalidPointer;
117 KmtStartSeh()
118 Status = MmCreateSection(&SectionObject, 0, NULL, NULL, 0, SEC_RESERVE, NULL, NULL);
119 KmtEndSeh(STATUS_SUCCESS);
120 ok_eq_hex(Status, STATUS_INVALID_PAGE_PROTECTION);
121 ok_eq_pointer(SectionObject, KmtInvalidPointer);
122
123 if (SectionObject && SectionObject != KmtInvalidPointer)
124 ObDereferenceObject(SectionObject);
125
126 KmtStartSeh()
127 Status = MmCreateSection(NULL, 0, NULL, NULL, PAGE_READONLY, SEC_RESERVE, NULL, NULL);
128 KmtEndSeh(STATUS_ACCESS_VIOLATION);
129
130 SectionObject = KmtInvalidPointer;
131 KmtStartSeh()
132 Status = MmCreateSection(&SectionObject, 0, NULL, NULL, PAGE_READONLY, SEC_RESERVE, NULL, NULL);
133 KmtEndSeh(STATUS_ACCESS_VIOLATION);
134 ok_eq_pointer(SectionObject, KmtInvalidPointer);
135
136 if (SectionObject && SectionObject != KmtInvalidPointer)
137 ObDereferenceObject(SectionObject);
138
139 SectionObject = KmtInvalidPointer;
140 MaximumSize.QuadPart = 0;
141 KmtStartSeh()
142 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, NULL, NULL);
143 KmtEndSeh(STATUS_SUCCESS);
144 ok_eq_hex(Status, STATUS_INVALID_FILE_FOR_SECTION);
145 ok_eq_longlong(MaximumSize.QuadPart, 0LL);
146 ok_eq_pointer(SectionObject, KmtInvalidPointer);
147
148 if (SectionObject && SectionObject != KmtInvalidPointer)
149 ObDereferenceObject(SectionObject);
150
151 MaximumSize.QuadPart = 0;
152 KmtStartSeh()
153 Status = MmCreateSection(NULL, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_RESERVE, NULL, NULL);
154 KmtEndSeh(STATUS_SUCCESS);
155 ok_eq_hex(Status, STATUS_INVALID_PARAMETER_4);
156 ok_eq_longlong(MaximumSize.QuadPart, 0LL);
157
158 if (SectionObject && SectionObject != KmtInvalidPointer)
159 ObDereferenceObject(SectionObject);
160
161 MaximumSize.QuadPart = 1;
162 KmtStartSeh()
163 Status = MmCreateSection(NULL, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_RESERVE, NULL, NULL);
164 KmtEndSeh(STATUS_ACCESS_VIOLATION);
165 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
166
167 if (SectionObject && SectionObject != KmtInvalidPointer)
168 ObDereferenceObject(SectionObject);
169
170 SectionObject = KmtInvalidPointer;
171 MaximumSize.QuadPart = 0;
172 KmtStartSeh()
173 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_RESERVE, NULL, NULL);
174 KmtEndSeh(STATUS_SUCCESS);
175 ok_eq_hex(Status, STATUS_INVALID_PARAMETER_4);
176 ok_eq_longlong(MaximumSize.QuadPart, 0LL);
177 ok_eq_pointer(SectionObject, KmtInvalidPointer);
178
179 if (SectionObject && SectionObject != KmtInvalidPointer)
180 ObDereferenceObject(SectionObject);
181
182 /* page file section */
183 SectionObject = KmtInvalidPointer;
184 MaximumSize.QuadPart = 1;
185 KmtStartSeh()
186 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_RESERVE, NULL, NULL);
187 KmtEndSeh(STATUS_SUCCESS);
188 ok_eq_hex(Status, STATUS_SUCCESS);
189 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
190 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
191 ok(SectionObject != NULL, "Section object pointer NULL\n");
192
193 if (SectionObject && SectionObject != KmtInvalidPointer)
194 ObDereferenceObject(SectionObject);
195
196 if (!skip(FileHandle1 != NULL && FileObject1 != NULL &&
197 FileHandle2 != NULL && FileObject2 != NULL, "No file handle or object\n"))
198 {
199 PointerCount1 = 3;
200 PointerCount2 = 3;
201 /* image section */
202 CheckObject(FileHandle2, PointerCount2, 1L);
203 SectionObject = KmtInvalidPointer;
204 MaximumSize.QuadPart = 1;
205 KmtStartSeh()
206 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, FileHandle2, NULL);
207 KmtEndSeh(STATUS_SUCCESS);
208 ok_eq_hex(Status, STATUS_SUCCESS);
209 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
210 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
211 ok(SectionObject != NULL, "Section object pointer NULL\n");
212 CheckObject(FileHandle2, PointerCount2, 1L);
213 CheckSection(SectionObject, SEC_IMAGE);
214 TestMapView(SectionObject, FALSE, TRUE);
215
216 if (SectionObject && SectionObject != KmtInvalidPointer)
217 ObDereferenceObject(SectionObject);
218
219 CheckObject(FileHandle2, PointerCount2, 1L);
220 SectionObject = KmtInvalidPointer;
221 MaximumSize.QuadPart = 1;
222 KmtStartSeh()
223 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, NULL, FileObject2);
224 KmtEndSeh(STATUS_SUCCESS);
225 ok_eq_hex(Status, STATUS_SUCCESS);
226 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
227 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
228 ok(SectionObject != NULL, "Section object pointer NULL\n");
229 ++PointerCount2;
230 CheckObject(FileHandle2, PointerCount2, 1L);
231 CheckSection(SectionObject, 0);
232 TestMapView(SectionObject, TRUE, TRUE);
233
234 if (SectionObject && SectionObject != KmtInvalidPointer)
235 ObDereferenceObject(SectionObject);
236 //--PointerCount2; // ????
237
238 CheckObject(FileHandle2, PointerCount2, 1L);
239 SectionObject = KmtInvalidPointer;
240 MaximumSize.QuadPart = 1;
241 KmtStartSeh()
242 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, FileHandle2, FileObject2);
243 KmtEndSeh(STATUS_SUCCESS);
244 ok_eq_hex(Status, STATUS_SUCCESS);
245 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
246 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
247 ok(SectionObject != NULL, "Section object pointer NULL\n");
248 CheckObject(FileHandle2, PointerCount2, 1L);
249 CheckSection(SectionObject, 0);
250 TestMapView(SectionObject, TRUE, TRUE);
251
252 if (SectionObject && SectionObject != KmtInvalidPointer)
253 ObDereferenceObject(SectionObject);
254
255 /* image section with inappropriate file */
256 CheckObject(FileHandle1, PointerCount1, 1L);
257 SectionObject = KmtInvalidPointer;
258 MaximumSize.QuadPart = 1;
259 KmtStartSeh()
260 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, FileHandle1, NULL);
261 KmtEndSeh(STATUS_SUCCESS);
262 ok_eq_hex(Status, STATUS_INVALID_IMAGE_NOT_MZ);
263 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
264 ok_eq_pointer(SectionObject, KmtInvalidPointer);
265 CheckObject(FileHandle1, PointerCount1, 1L);
266
267 if (SectionObject && SectionObject != KmtInvalidPointer)
268 ObDereferenceObject(SectionObject);
269
270 CheckObject(FileHandle1, PointerCount1, 1L);
271 SectionObject = KmtInvalidPointer;
272 MaximumSize.QuadPart = 1;
273 KmtStartSeh()
274 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, NULL, FileObject1);
275 KmtEndSeh(STATUS_SUCCESS);
276 ok_eq_hex(Status, STATUS_SUCCESS);
277 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
278 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
279 ok(SectionObject != NULL, "Section object pointer NULL\n");
280 ++PointerCount1;
281 CheckObject(FileHandle1, PointerCount1, 1L);
282 CheckSection(SectionObject, 0);
283 TestMapView(SectionObject, TRUE, FALSE);
284
285 if (SectionObject && SectionObject != KmtInvalidPointer)
286 ObDereferenceObject(SectionObject);
287 //--PointerCount1; // ????
288
289 CheckObject(FileHandle1, PointerCount1, 1L);
290 SectionObject = KmtInvalidPointer;
291 MaximumSize.QuadPart = 1;
292 KmtStartSeh()
293 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, FileHandle1, FileObject1);
294 KmtEndSeh(STATUS_SUCCESS);
295 ok_eq_hex(Status, STATUS_SUCCESS);
296 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
297 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
298 ok(SectionObject != NULL, "Section object pointer NULL\n");
299 CheckObject(FileHandle1, PointerCount1, 1L);
300 CheckSection(SectionObject, 0);
301 TestMapView(SectionObject, TRUE, FALSE);
302
303 if (SectionObject && SectionObject != KmtInvalidPointer)
304 ObDereferenceObject(SectionObject);
305
306 /* image section with two different files */
307 CheckObject(FileHandle1, PointerCount1, 1L);
308 SectionObject = KmtInvalidPointer;
309 MaximumSize.QuadPart = 1;
310 KmtStartSeh()
311 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, FileHandle1, FileObject2);
312 KmtEndSeh(STATUS_SUCCESS);
313 ok_eq_hex(Status, STATUS_SUCCESS);
314 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
315 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
316 ok(SectionObject != NULL, "Section object pointer NULL\n");
317 CheckObject(FileHandle1, PointerCount1, 1L);
318 CheckObject(FileHandle2, PointerCount2, 1L);
319 CheckSection(SectionObject, 0);
320 TestMapView(SectionObject, TRUE, TRUE);
321
322 if (SectionObject && SectionObject != KmtInvalidPointer)
323 ObDereferenceObject(SectionObject);
324
325 CheckObject(FileHandle1, PointerCount1, 1L);
326 SectionObject = KmtInvalidPointer;
327 MaximumSize.QuadPart = 1;
328 KmtStartSeh()
329 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_IMAGE, FileHandle2, FileObject1);
330 KmtEndSeh(STATUS_SUCCESS);
331 ok_eq_hex(Status, STATUS_SUCCESS);
332 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
333 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
334 ok(SectionObject != NULL, "Section object pointer NULL\n");
335 CheckObject(FileHandle1, PointerCount1, 1L);
336 CheckObject(FileHandle2, PointerCount2, 1L);
337 CheckSection(SectionObject, 0);
338 TestMapView(SectionObject, TRUE, FALSE);
339
340 if (SectionObject && SectionObject != KmtInvalidPointer)
341 ObDereferenceObject(SectionObject);
342
343 /* data file section */
344 CheckObject(FileHandle1, PointerCount1, 1L);
345 SectionObject = KmtInvalidPointer;
346 MaximumSize.QuadPart = 1;
347 KmtStartSeh()
348 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_RESERVE, FileHandle1, NULL);
349 KmtEndSeh(STATUS_SUCCESS);
350 ok_eq_hex(Status, STATUS_SUCCESS);
351 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
352 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
353 ok(SectionObject != NULL, "Section object pointer NULL\n");
354 CheckObject(FileHandle1, PointerCount1, 1L);
355 CheckSection(SectionObject, 0);
356 TestMapView(SectionObject, TRUE, FALSE);
357
358 if (SectionObject && SectionObject != KmtInvalidPointer)
359 ObDereferenceObject(SectionObject);
360
361 CheckObject(FileHandle1, PointerCount1, 1L);
362 SectionObject = KmtInvalidPointer;
363 MaximumSize.QuadPart = 1;
364 KmtStartSeh()
365 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_RESERVE, NULL, FileObject1);
366 KmtEndSeh(STATUS_SUCCESS);
367 ok_eq_hex(Status, STATUS_SUCCESS);
368 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
369 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
370 ok(SectionObject != NULL, "Section object pointer NULL\n");
371 CheckObject(FileHandle1, PointerCount1, 1L);
372 CheckSection(SectionObject, 0);
373 TestMapView(SectionObject, TRUE, FALSE);
374
375 if (SectionObject && SectionObject != KmtInvalidPointer)
376 ObDereferenceObject(SectionObject);
377
378 CheckObject(FileHandle1, PointerCount1, 1L);
379 SectionObject = KmtInvalidPointer;
380 MaximumSize.QuadPart = 1;
381 KmtStartSeh()
382 Status = MmCreateSection(&SectionObject, 0, NULL, &MaximumSize, PAGE_READONLY, SEC_RESERVE, FileHandle1, FileObject1);
383 KmtEndSeh(STATUS_SUCCESS);
384 ok_eq_hex(Status, STATUS_SUCCESS);
385 ok_eq_longlong(MaximumSize.QuadPart, 1LL);
386 ok(SectionObject != KmtInvalidPointer, "Section object pointer untouched\n");
387 ok(SectionObject != NULL, "Section object pointer NULL\n");
388 CheckObject(FileHandle1, PointerCount1, 1L);
389 CheckSection(SectionObject, 0);
390 TestMapView(SectionObject, TRUE, FALSE);
391
392 if (SectionObject && SectionObject != KmtInvalidPointer)
393 ObDereferenceObject(SectionObject);
394
395 CheckObject(FileHandle1, PointerCount1, 1L);
396 }
397 }
398
399 static
400 VOID
401 TestPhysicalMemorySection(VOID)
402 {
403 NTSTATUS Status;
404 UNICODE_STRING SectionName = RTL_CONSTANT_STRING(L"\\Device\\PhysicalMemory");
405 OBJECT_ATTRIBUTES ObjectAttributes;
406 HANDLE SectionHandle;
407 PVOID SectionObject;
408 PUCHAR MyPage;
409 PHYSICAL_ADDRESS MyPagePhysical;
410 PUCHAR ZeroPageContents;
411 PHYSICAL_ADDRESS ZeroPagePhysical;
412 PVOID Mapping;
413 PUCHAR MappingBytes;
414 SIZE_T ViewSize;
415 SIZE_T EqualBytes;
416
417 MyPage = ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, 'MPmK');
418 if (skip(MyPage != NULL, "Out of memory\n"))
419 return;
420 MyPagePhysical = MmGetPhysicalAddress(MyPage);
421 RtlFillMemory(MyPage + 0 * PAGE_SIZE / 4, PAGE_SIZE / 4, 0x23);
422 RtlFillMemory(MyPage + 1 * PAGE_SIZE / 4, PAGE_SIZE / 4, 0x67);
423 RtlFillMemory(MyPage + 2 * PAGE_SIZE / 4, PAGE_SIZE / 4, 0xab);
424 RtlFillMemory(MyPage + 3 * PAGE_SIZE / 4, PAGE_SIZE / 4, 0xef);
425
426 ZeroPageContents = ExAllocatePoolWithTag(PagedPool, PAGE_SIZE, 'ZPmK');
427 if (skip(ZeroPageContents != NULL, "Out of memory\n"))
428 {
429 ExFreePoolWithTag(MyPage, 'MPmK');
430 return;
431 }
432 ZeroPagePhysical.QuadPart = 0;
433
434 Mapping = MmMapIoSpace(ZeroPagePhysical, PAGE_SIZE, MmCached);
435 if (skip(Mapping != NULL, "Failed to map zero page\n"))
436 {
437 ExFreePoolWithTag(ZeroPageContents, 'ZPmK');
438 ExFreePoolWithTag(MyPage, 'MPmK');
439 return;
440 }
441
442 RtlCopyMemory(ZeroPageContents, Mapping, PAGE_SIZE);
443 MmUnmapIoSpace(Mapping, PAGE_SIZE);
444
445 InitializeObjectAttributes(&ObjectAttributes,
446 &SectionName,
447 OBJ_KERNEL_HANDLE,
448 NULL,
449 NULL);
450 Status = ZwOpenSection(&SectionHandle, SECTION_ALL_ACCESS, &ObjectAttributes);
451 ok_eq_hex(Status, STATUS_SUCCESS);
452 if (!skip(NT_SUCCESS(Status), "No section\n"))
453 {
454 /* Map zero page and compare */
455 Mapping = NULL;
456 ViewSize = PAGE_SIZE;
457 Status = ZwMapViewOfSection(SectionHandle,
458 ZwCurrentProcess(),
459 &Mapping,
460 0,
461 0,
462 &ZeroPagePhysical,
463 &ViewSize,
464 ViewUnmap,
465 0,
466 PAGE_READWRITE);
467 ok_eq_hex(Status, STATUS_SUCCESS);
468 if (!skip(NT_SUCCESS(Status), "No view\n"))
469 {
470 ok((LONG_PTR)Mapping > 0, "Mapping = %p\n", Mapping);
471 EqualBytes = RtlCompareMemory(Mapping,
472 ZeroPageContents,
473 PAGE_SIZE);
474 ok_eq_size(EqualBytes, PAGE_SIZE);
475 Status = ZwUnmapViewOfSection(ZwCurrentProcess(), Mapping);
476 ok_eq_hex(Status, STATUS_SUCCESS);
477 }
478
479 /* Map the zero page non-cached */
480 Mapping = NULL;
481 ViewSize = PAGE_SIZE;
482 Status = ZwMapViewOfSection(SectionHandle,
483 ZwCurrentProcess(),
484 &Mapping,
485 0,
486 0,
487 &ZeroPagePhysical,
488 &ViewSize,
489 ViewUnmap,
490 0,
491 PAGE_READWRITE | PAGE_NOCACHE);
492 ok_eq_hex(Status, STATUS_SUCCESS);
493 if (!skip(NT_SUCCESS(Status), "No view\n"))
494 {
495 ok((LONG_PTR)Mapping > 0, "Mapping = %p\n", Mapping);
496 EqualBytes = RtlCompareMemory(Mapping,
497 ZeroPageContents,
498 PAGE_SIZE);
499 ok_eq_size(EqualBytes, PAGE_SIZE);
500 Status = ZwUnmapViewOfSection(ZwCurrentProcess(), Mapping);
501 ok_eq_hex(Status, STATUS_SUCCESS);
502 }
503
504 /* Map our NP page, compare, and check that modifications are reflected */
505 Mapping = NULL;
506 ViewSize = PAGE_SIZE;
507 Status = ZwMapViewOfSection(SectionHandle,
508 ZwCurrentProcess(),
509 &Mapping,
510 0,
511 0,
512 &MyPagePhysical,
513 &ViewSize,
514 ViewUnmap,
515 0,
516 PAGE_READWRITE);
517 ok_eq_hex(Status, STATUS_SUCCESS);
518 if (!skip(NT_SUCCESS(Status), "No view\n"))
519 {
520 ok((LONG_PTR)Mapping > 0, "Mapping = %p\n", Mapping);
521 EqualBytes = RtlCompareMemory(Mapping,
522 MyPage,
523 PAGE_SIZE);
524 ok_eq_size(EqualBytes, PAGE_SIZE);
525
526 MappingBytes = Mapping;
527 ok(MappingBytes[5] == 0x23, "Mapping[5] = 0x%x\n", MappingBytes[5]);
528 ok(MyPage[5] == 0x23, "MyPage[5] = 0x%x\n", MyPage[5]);
529
530 MyPage[5] = 0x44;
531 ok(MappingBytes[5] == 0x44, "Mapping[5] = 0x%x\n", MappingBytes[5]);
532 ok(MyPage[5] == 0x44, "MyPage[5] = 0x%x\n", MyPage[5]);
533
534 MappingBytes[5] = 0x88;
535 ok(MappingBytes[5] == 0x88, "Mapping[5] = 0x%x\n", MappingBytes[5]);
536 ok(MyPage[5] == 0x88, "MyPage[5] = 0x%x\n", MyPage[5]);
537
538 Status = ZwUnmapViewOfSection(ZwCurrentProcess(), Mapping);
539 ok_eq_hex(Status, STATUS_SUCCESS);
540 }
541
542 Status = ZwClose(SectionHandle);
543 ok_eq_hex(Status, STATUS_SUCCESS);
544 }
545
546 /* Try flag 0x80000000, which ROS calls SEC_PHYSICALMEMORY */
547 InitializeObjectAttributes(&ObjectAttributes,
548 NULL,
549 OBJ_KERNEL_HANDLE,
550 NULL,
551 NULL);
552 Status = ZwCreateSection(&SectionHandle,
553 SECTION_ALL_ACCESS,
554 &ObjectAttributes,
555 NULL,
556 PAGE_READWRITE,
557 0x80000000,
558 NULL);
559 ok_eq_hex(Status, STATUS_INVALID_PARAMETER_6);
560 if (NT_SUCCESS(Status))
561 ZwClose(SectionHandle);
562
563 /* Assertion failure: AllocationAttributes & SEC_IMAGE | SEC_RESERVE | SEC_COMMIT */
564 if (!KmtIsCheckedBuild)
565 {
566 InitializeObjectAttributes(&ObjectAttributes,
567 NULL,
568 OBJ_KERNEL_HANDLE,
569 NULL,
570 NULL);
571 Status = MmCreateSection(&SectionObject,
572 SECTION_ALL_ACCESS,
573 &ObjectAttributes,
574 NULL,
575 PAGE_READWRITE,
576 0x80000000,
577 NULL,
578 NULL);
579 ok_eq_hex(Status, STATUS_INVALID_PARAMETER_6);
580 if (NT_SUCCESS(Status))
581 ObDereferenceObject(SectionObject);
582 }
583
584 InitializeObjectAttributes(&ObjectAttributes,
585 NULL,
586 OBJ_KERNEL_HANDLE,
587 NULL,
588 NULL);
589 Status = MmCreateSection(&SectionObject,
590 SECTION_ALL_ACCESS,
591 &ObjectAttributes,
592 NULL,
593 PAGE_READWRITE,
594 SEC_RESERVE | 0x80000000,
595 NULL,
596 NULL);
597 ok_eq_hex(Status, STATUS_INVALID_PARAMETER_6);
598 if (NT_SUCCESS(Status))
599 ObDereferenceObject(SectionObject);
600
601 ExFreePoolWithTag(ZeroPageContents, 'ZPmK');
602 ExFreePoolWithTag(MyPage, 'MPmK');
603 }
604
605 START_TEST(MmSection)
606 {
607 NTSTATUS Status;
608 HANDLE FileHandle1 = NULL, FileHandle2 = NULL;
609 PFILE_OBJECT FileObject1 = NULL, FileObject2 = NULL;
610 OBJECT_ATTRIBUTES ObjectAttributes;
611 IO_STATUS_BLOCK IoStatusBlock;
612 UNICODE_STRING FileName1 = RTL_CONSTANT_STRING(L"\\SystemRoot\\kmtest-MmSection.txt");
613 UNICODE_STRING FileName2 = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\ntdll.dll");
614 LARGE_INTEGER FileOffset;
615 UCHAR FileData = 0;
616
617 ok(ExGetPreviousMode() == UserMode, "Previous mode is kernel mode\n");
618 /* create a one-byte file that we can use */
619 InitializeObjectAttributes(&ObjectAttributes, &FileName1, OBJ_CASE_INSENSITIVE, NULL, NULL);
620 Status = ZwCreateFile(&FileHandle1, GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_SUPERSEDE, FILE_NON_DIRECTORY_FILE, NULL, 0);
621 ok_eq_hex(Status, STATUS_SUCCESS);
622 ok_eq_ulongptr(IoStatusBlock.Information, FILE_CREATED);
623 ok(FileHandle1 != NULL, "FileHandle1 is NULL\n");
624 if (FileHandle1)
625 {
626 FileOffset.QuadPart = 0;
627 Status = ZwWriteFile(FileHandle1, NULL, NULL, NULL, &IoStatusBlock, &FileData, sizeof FileData, &FileOffset, NULL);
628 ok(Status == STATUS_SUCCESS || Status == STATUS_PENDING, "Status = 0x%08lx\n", Status);
629 Status = ZwWaitForSingleObject(FileHandle1, FALSE, NULL);
630 ok_eq_hex(Status, STATUS_SUCCESS);
631 ok_eq_ulongptr(IoStatusBlock.Information, 1);
632 Status = ZwClose(FileHandle1);
633 ok_eq_hex(Status, STATUS_SUCCESS);
634 FileHandle1 = NULL;
635 }
636
637 InitializeObjectAttributes(&ObjectAttributes, &FileName1, OBJ_CASE_INSENSITIVE, NULL, NULL);
638 Status = ZwCreateFile(&FileHandle1, GENERIC_ALL, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_OPEN, FILE_NON_DIRECTORY_FILE | FILE_DELETE_ON_CLOSE, NULL, 0);
639 ok_eq_hex(Status, STATUS_SUCCESS);
640 ok_eq_ulongptr(IoStatusBlock.Information, FILE_OPENED);
641 ok(FileHandle1 != NULL, "FileHandle1 is NULL\n");
642 CheckObject(FileHandle1, 2L, 1L);
643
644 InitializeObjectAttributes(&ObjectAttributes, &FileName2, OBJ_CASE_INSENSITIVE, NULL, NULL);
645 Status = ZwCreateFile(&FileHandle2, GENERIC_READ, &ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_OPEN, FILE_NON_DIRECTORY_FILE, NULL, 0);
646 ok_eq_hex(Status, STATUS_SUCCESS);
647 ok_eq_ulongptr(IoStatusBlock.Information, FILE_OPENED);
648 ok(FileHandle2 != NULL, "FileHandle2 is NULL\n");
649
650 if (!skip(Status == STATUS_SUCCESS && FileHandle1 != NULL, "Failed to open file 1\n"))
651 {
652 Status = ObReferenceObjectByHandle(FileHandle1, FILE_READ_DATA | FILE_WRITE_DATA, *IoFileObjectType, KernelMode, (PVOID *)&FileObject1, NULL);
653 ok_eq_hex(Status, STATUS_SUCCESS);
654 ok(FileObject1 != NULL, "FileObject1 is NULL\n");
655 CheckObject(FileHandle1, 3L, 1L);
656 }
657
658 if (!skip(Status == STATUS_SUCCESS && FileHandle2 != NULL, "Failed to open file 2\n"))
659 {
660 Status = ObReferenceObjectByHandle(FileHandle2, FILE_READ_DATA | FILE_WRITE_DATA, *IoFileObjectType, KernelMode, (PVOID *)&FileObject2, NULL);
661 ok_eq_hex(Status, STATUS_SUCCESS);
662 ok(FileObject2 != NULL, "FileObject2 is NULL\n");
663 }
664
665 trace("FileHandle1=%p, FileObject1=%p\n", FileHandle1, FileObject1);
666 trace("FileHandle2=%p, FileObject2=%p\n", FileHandle2, FileObject2);
667 TestCreateSection(FileHandle1, FileObject1, FileHandle2, FileObject2);
668
669 if (FileObject2)
670 ObDereferenceObject(FileObject2);
671 if (FileObject1)
672 ObDereferenceObject(FileObject1);
673 if (FileHandle2)
674 ZwClose(FileHandle2);
675 if (FileHandle1)
676 ZwClose(FileHandle1);
677
678 TestPhysicalMemorySection();
679 }