4 #define WIN32_NO_STATUS
16 PLARGE_INTEGER SectionOffset
,
18 SECTION_INHERIT InheritDisposition
,
23 Test_PageFileSection(void)
27 LARGE_INTEGER MaximumSize
, SectionOffset
;
31 /* Create a page file backed section */
32 MaximumSize
.QuadPart
= 0x20000;
33 Status
= NtCreateSection(&SectionHandle
,
40 ok(NT_SUCCESS(Status
), "NtCreateSection failed with Status %lx\n", Status
);
41 if (!NT_SUCCESS(Status
))
44 /* Try to map a page at an address that is not 64k aligned */
45 BaseAddress
= (PVOID
)0x30001000;
46 SectionOffset
.QuadPart
= 0;
48 Status
= NtMapViewOfSection(SectionHandle
,
58 ok(Status
== STATUS_MAPPED_ALIGNMENT
,
59 "NtMapViewOfSection returned wrong Status %lx\n", Status
);
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(Status
== STATUS_SECTION_PROTECTION
,
76 "NtMapViewOfSection returned wrong Status %lx\n", Status
);
78 /* Map 2 pages, not comitting them */
79 BaseAddress
= (PVOID
)0x30000000;
80 SectionOffset
.QuadPart
= 0;
82 Status
= NtMapViewOfSection(SectionHandle
,
92 ok(NT_SUCCESS(Status
), "NtMapViewOfSection failed with Status %lx\n", Status
);
93 if (!NT_SUCCESS(Status
))
96 /* Commit a page in the section */
97 BaseAddress
= (PVOID
)0x30000000;
99 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
105 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed with Status %lx\n", Status
);
106 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
110 ok(Status
== STATUS_UNABLE_TO_DELETE_SECTION
, "NtFreeVirtualMemory returned wrong Status %lx\n", Status
);
111 /* Try to commit a range larger than the section */
112 BaseAddress
= (PVOID
)0x30000000;
114 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
120 ok(Status
== STATUS_CONFLICTING_ADDRESSES
,
121 "NtAllocateVirtualMemory failed with wrong Status %lx\n", Status
);
123 /* Try to commit a page after the section */
124 BaseAddress
= (PVOID
)0x30002000;
126 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
132 ok(!NT_SUCCESS(Status
), "NtAllocateVirtualMemory Should fail\n");
134 /* Try to allocate a page after the section */
135 BaseAddress
= (PVOID
)0x30002000;
137 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
141 MEM_RESERVE
| MEM_COMMIT
,
143 ok(!NT_SUCCESS(Status
), "NtAllocateVirtualMemory should fail\n");
145 /* Need to go to next 64k boundary */
146 BaseAddress
= (PVOID
)0x30010000;
148 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
152 MEM_RESERVE
| MEM_COMMIT
,
154 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed with Status %lx\n", Status
);
155 if (!NT_SUCCESS(Status
))
158 /* Free the allocation */
159 BaseAddress
= (PVOID
)0x30010000;
161 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
165 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed with Status %lx\n", Status
);
167 /* Free the section mapping */
168 BaseAddress
= (PVOID
)0x30000000;
170 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
174 ok(Status
== STATUS_UNABLE_TO_DELETE_SECTION
,
175 "NtFreeVirtualMemory failed with wrong Status %lx\n", Status
);
177 /* Commit a page in the section */
178 BaseAddress
= (PVOID
)0x30001000;
180 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
186 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed with Status %lx\n", Status
);
188 /* Try to decommit the page */
189 BaseAddress
= (PVOID
)0x30001000;
191 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
195 ok(Status
== STATUS_UNABLE_TO_DELETE_SECTION
,
196 "NtFreeVirtualMemory failed with wrong Status %lx\n", Status
);
198 BaseAddress
= UlongToPtr(0x40000000);
199 SectionOffset
.QuadPart
= 0;
201 Status
= NtMapViewOfSection(SectionHandle
,
211 ok(NT_SUCCESS(Status
), "NtMapViewOfSection failed with Status %lx\n", Status
);
212 if (!NT_SUCCESS(Status
))
215 ok(BaseAddress
== UlongToPtr(0x40000000), "Invalid BaseAddress: %p", BaseAddress
);
217 BaseAddress
= (PVOID
)0x40080000;
218 SectionOffset
.QuadPart
= 0x10000;
220 Status
= NtMapViewOfSection(SectionHandle
,
230 ok(NT_SUCCESS(Status
), "NtMapViewOfSection failed with Status %lx\n", Status
);
231 if (!NT_SUCCESS(Status
))
234 ok(BaseAddress
== (PVOID
)0x40080000, "Invalid BaseAddress: %p", BaseAddress
);
236 /* Commit a page in the section */
237 BaseAddress
= (PVOID
)0x40000000;
238 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
244 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed with Status %lx\n", Status
);
245 if (!NT_SUCCESS(Status
))
251 Test_ImageSection(void)
253 UNICODE_STRING FileName
;
255 OBJECT_ATTRIBUTES FileObjectAttributes
;
256 IO_STATUS_BLOCK IoStatusBlock
;
257 HANDLE FileHandle
, DataSectionHandle
, ImageSectionHandle
;
258 PVOID DataBase
, ImageBase
;
261 if (!RtlDosPathNameToNtPathName_U(L
"testdata\\test.dll",
266 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
270 InitializeObjectAttributes(&FileObjectAttributes
,
276 Status
= NtOpenFile(&FileHandle
,
277 GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
278 &FileObjectAttributes
,
281 FILE_SYNCHRONOUS_IO_NONALERT
);
282 ok(Status
== STATUS_SUCCESS
, "NtOpenFile failed, Status 0x%lx\n", Status
);
284 /* Create a data section with write access */
285 Status
= NtCreateSection(&DataSectionHandle
,
286 SECTION_ALL_ACCESS
, // DesiredAccess
287 NULL
, // ObjectAttributes
289 PAGE_READWRITE
, // SectionPageProtection
290 SEC_COMMIT
, // AllocationAttributes
292 ok(Status
== STATUS_SUCCESS
, "NtCreateSection failed, Status 0x%lx\n", Status
);
294 /* Map the data section */
297 Status
= NtMapViewOfSection(DataSectionHandle
,
307 ok(Status
== STATUS_SUCCESS
, "NtMapViewOfSection failed, Status 0x%lx\n", Status
);
309 /* Check the original data */
310 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
312 /* Modify the PE header (but do not flush!) */
313 *(ULONG
*)DataBase
= 0xdeadbabe;
314 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
316 /* Modify data in the .data section (but do not flush!) */
317 ok(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800) == 0x12345678,
318 "Data in .data section invalid: 0x%lx!\n", *(ULONG
*)((PUCHAR
)DataBase
+ 0x800));
319 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x87654321;
321 /* Now try to create an image section (should fail) */
322 Status
= NtCreateSection(&ImageSectionHandle
,
323 SECTION_ALL_ACCESS
, // DesiredAccess
324 NULL
, // ObjectAttributes
326 PAGE_READWRITE
, // SectionPageProtection
327 SEC_IMAGE
, // AllocationAttributes
329 ok(Status
== STATUS_INVALID_IMAGE_NOT_MZ
, "NtCreateSection failed, Status 0x%lx\n", Status
);
331 /* Restore the original header */
332 *(ULONG
*)DataBase
= 0x00905a4d;
334 /* Modify data in the .data section (but do not flush!) */
335 ok_hex(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800), 0x87654321);
336 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xdeadbabe;
338 /* Try to create an image section again */
339 Status
= NtCreateSection(&ImageSectionHandle
,
340 SECTION_ALL_ACCESS
, // DesiredAccess
341 NULL
, // ObjectAttributes
343 PAGE_READWRITE
, // SectionPageProtection
344 SEC_IMAGE
, // AllocationAttributes
346 ok(Status
== STATUS_SUCCESS
, "NtCreateSection failed, Status 0x%lx\n", Status
);
348 /* Map the image section */
351 Status
= NtMapViewOfSection(ImageSectionHandle
,
362 ok(Status
== STATUS_SUCCESS
, "NtMapViewOfSection failed, Status 0x%lx\n", Status
);
364 ok(Status
== STATUS_IMAGE_MACHINE_TYPE_MISMATCH
, "NtMapViewOfSection failed, Status 0x%lx\n", Status
);
367 /* Check the header */
368 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
369 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Header not ok\n");
371 /* Check the data section. Either of these can be present! */
372 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
373 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
374 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
376 /* Now modify the data again */
377 *(ULONG
*)DataBase
= 0xdeadbabe;
378 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xf00dada;
381 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
382 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
383 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
384 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
385 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
389 Status
= NtFlushVirtualMemory(NtCurrentProcess(),
393 ok(Status
== STATUS_SUCCESS
, "NtFlushVirtualMemory failed, Status 0x%lx\n", Status
);
395 /* Check the data again */
396 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
397 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
398 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
399 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
401 /* Restore the original header */
402 *(ULONG
*)DataBase
= 0x00905a4d;
403 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
405 /* Close the image mapping */
406 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
407 NtClose(ImageSectionHandle
);
409 /* Create an image section again */
410 Status
= NtCreateSection(&ImageSectionHandle
,
411 SECTION_ALL_ACCESS
, // DesiredAccess
412 NULL
, // ObjectAttributes
414 PAGE_READWRITE
, // SectionPageProtection
415 SEC_IMAGE
, // AllocationAttributes
417 ok(Status
== STATUS_SUCCESS
, "NtCreateSection failed, Status 0x%lx\n", Status
);
419 /* Map the image section again */
422 Status
= NtMapViewOfSection(ImageSectionHandle
,
433 ok(Status
== STATUS_SUCCESS
, "NtMapViewOfSection failed, Status 0x%lx\n", Status
);
435 ok(Status
== STATUS_IMAGE_MACHINE_TYPE_MISMATCH
, "NtMapViewOfSection failed, Status 0x%lx\n", Status
);
438 /* Check the .data section again */
439 ok(*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0xf00dada,
440 "Data should be synced: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
442 /* Restore the original data */
443 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x12345678;
445 /* Close the data mapping */
446 NtUnmapViewOfSection(NtCurrentProcess(), DataBase
);
448 NtClose(DataSectionHandle
);
450 /* Try to allocate memory inside the image mapping */
451 DataBase
= (PUCHAR
)ImageBase
+ 0x20000;
453 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &DataBase
, 0, &ViewSize
, MEM_RESERVE
, PAGE_NOACCESS
);
454 ok(Status
== STATUS_CONFLICTING_ADDRESSES
, "Wrong Status: 0x%lx\n", Status
);
458 NtClose(ImageSectionHandle
);
459 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
463 START_TEST(NtMapViewOfSection
)
465 Test_PageFileSection();