2 #define WIN32_NO_STATUS
5 #include <pseh/pseh2.h>
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
);
107 /* Try to commit a range larger than the section */
108 BaseAddress
= (PVOID
)0x30000000;
110 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
116 ok(Status
== STATUS_CONFLICTING_ADDRESSES
,
117 "NtAllocateVirtualMemory failed with wrong Status %lx\n", Status
);
119 /* Try to commit a page after the section */
120 BaseAddress
= (PVOID
)0x30002000;
122 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
128 ok(!NT_SUCCESS(Status
), "NtAllocateVirtualMemory Should fail\n");
130 /* Try to allocate a page after the section */
131 BaseAddress
= (PVOID
)0x30002000;
133 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
137 MEM_RESERVE
| MEM_COMMIT
,
139 ok(!NT_SUCCESS(Status
), "NtAllocateVirtualMemory should fail\n");
141 /* Need to go to next 64k boundary */
142 BaseAddress
= (PVOID
)0x30010000;
144 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
148 MEM_RESERVE
| MEM_COMMIT
,
150 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed with Status %lx\n", Status
);
151 if (!NT_SUCCESS(Status
))
154 /* Free the allocation */
155 BaseAddress
= (PVOID
)0x30010000;
157 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
161 ok(NT_SUCCESS(Status
), "NtFreeVirtualMemory failed with Status %lx\n", Status
);
163 /* Free the section mapping */
164 BaseAddress
= (PVOID
)0x30000000;
166 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
170 ok(Status
== STATUS_UNABLE_TO_DELETE_SECTION
,
171 "NtFreeVirtualMemory failed with wrong Status %lx\n", Status
);
173 /* Commit a page in the section */
174 BaseAddress
= (PVOID
)0x30001000;
176 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
182 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed with Status %lx\n", Status
);
184 /* Try to decommit the page */
185 BaseAddress
= (PVOID
)0x30001000;
187 Status
= NtFreeVirtualMemory(NtCurrentProcess(),
191 ok(Status
== STATUS_UNABLE_TO_DELETE_SECTION
,
192 "NtFreeVirtualMemory failed with wrong Status %lx\n", Status
);
194 BaseAddress
= UlongToPtr(0x40000000);
195 SectionOffset
.QuadPart
= 0;
197 Status
= NtMapViewOfSection(SectionHandle
,
207 ok(NT_SUCCESS(Status
), "NtMapViewOfSection failed with Status %lx\n", Status
);
208 if (!NT_SUCCESS(Status
))
211 ok(BaseAddress
== UlongToPtr(0x40000000), "Invalid BaseAddress: %p", BaseAddress
);
213 BaseAddress
= (PVOID
)0x40080000;
214 SectionOffset
.QuadPart
= 0x10000;
216 Status
= NtMapViewOfSection(SectionHandle
,
226 ok(NT_SUCCESS(Status
), "NtMapViewOfSection failed with Status %lx\n", Status
);
227 if (!NT_SUCCESS(Status
))
230 ok(BaseAddress
== (PVOID
)0x40080000, "Invalid BaseAddress: %p", BaseAddress
);
232 /* Commit a page in the section */
233 BaseAddress
= (PVOID
)0x40000000;
234 Status
= NtAllocateVirtualMemory(NtCurrentProcess(),
240 ok(NT_SUCCESS(Status
), "NtAllocateVirtualMemory failed with Status %lx\n", Status
);
241 if (!NT_SUCCESS(Status
))
247 Test_ImageSection(void)
249 UNICODE_STRING FileName
;
251 OBJECT_ATTRIBUTES FileObjectAttributes
;
252 IO_STATUS_BLOCK IoStatusBlock
;
253 HANDLE FileHandle
, DataSectionHandle
, ImageSectionHandle
;
254 PVOID DataBase
, ImageBase
;
257 if (!RtlDosPathNameToNtPathName_U(L
"testdata\\test.dll",
262 ok(0, "RtlDosPathNameToNtPathName_U failed\n");
266 InitializeObjectAttributes(&FileObjectAttributes
,
272 Status
= NtOpenFile(&FileHandle
,
273 GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
,
274 &FileObjectAttributes
,
277 FILE_SYNCHRONOUS_IO_NONALERT
);
278 ok(Status
== STATUS_SUCCESS
, "NtOpenFile failed, Status 0x%lx\n", Status
);
280 /* Create a data section with write access */
281 Status
= NtCreateSection(&DataSectionHandle
,
282 SECTION_ALL_ACCESS
, // DesiredAccess
283 NULL
, // ObjectAttributes
285 PAGE_READWRITE
, // SectionPageProtection
286 SEC_COMMIT
, // AllocationAttributes
288 ok(Status
== STATUS_SUCCESS
, "NtCreateSection failed, Status 0x%lx\n", Status
);
290 /* Map the data section */
293 Status
= NtMapViewOfSection(DataSectionHandle
,
303 ok(Status
== STATUS_SUCCESS
, "NtMapViewOfSection failed, Status 0x%lx\n", Status
);
305 /* Check the original data */
306 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
308 /* Modify the PE header (but do not flush!) */
309 *(ULONG
*)DataBase
= 0xdeadbabe;
310 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
312 /* Modify data in the .data section (but do not flush!) */
313 ok(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800) == 0x12345678,
314 "Data in .data section invalid: 0x%lx!\n", *(ULONG
*)((PUCHAR
)DataBase
+ 0x800));
315 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x87654321;
317 /* Now try to create an image section (should fail) */
318 Status
= NtCreateSection(&ImageSectionHandle
,
319 SECTION_ALL_ACCESS
, // DesiredAccess
320 NULL
, // ObjectAttributes
322 PAGE_READWRITE
, // SectionPageProtection
323 SEC_IMAGE
, // AllocationAttributes
325 ok(Status
== STATUS_INVALID_IMAGE_NOT_MZ
, "NtCreateSection failed, Status 0x%lx\n", Status
);
327 /* Restore the original header */
328 *(ULONG
*)DataBase
= 0x00905a4d;
330 /* Modify data in the .data section (but do not flush!) */
331 ok_hex(*(ULONG
*)((PUCHAR
)DataBase
+ 0x800), 0x87654321);
332 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xdeadbabe;
334 /* Try to create an image section again */
335 Status
= NtCreateSection(&ImageSectionHandle
,
336 SECTION_ALL_ACCESS
, // DesiredAccess
337 NULL
, // ObjectAttributes
339 PAGE_READWRITE
, // SectionPageProtection
340 SEC_IMAGE
, // AllocationAttributes
342 ok(Status
== STATUS_SUCCESS
, "NtCreateSection failed, Status 0x%lx\n", Status
);
344 /* Map the image section */
347 Status
= NtMapViewOfSection(ImageSectionHandle
,
358 ok(Status
== STATUS_SUCCESS
, "NtMapViewOfSection failed, Status 0x%lx\n", Status
);
360 ok(Status
== STATUS_IMAGE_MACHINE_TYPE_MISMATCH
, "NtMapViewOfSection failed, Status 0x%lx\n", Status
);
363 /* Check the header */
364 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
365 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Header not ok\n");
367 /* Check the data section. Either of these can be present! */
368 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
369 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
370 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
372 /* Now modify the data again */
373 *(ULONG
*)DataBase
= 0xdeadbabe;
374 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0xf00dada;
377 ok(*(ULONG
*)DataBase
== 0xdeadbabe, "Header not ok\n");
378 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
379 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
380 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
381 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
385 Status
= NtFlushVirtualMemory(NtCurrentProcess(),
389 ok(Status
== STATUS_SUCCESS
, "NtFlushVirtualMemory failed, Status 0x%lx\n", Status
);
391 /* Check the data again */
392 ok(*(ULONG
*)ImageBase
== 0x00905a4d, "Data should not be synced!\n");
393 ok((*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x87654321) ||
394 (*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0x12345678),
395 "Wrong value in data section: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
397 /* Restore the original header */
398 *(ULONG
*)DataBase
= 0x00905a4d;
399 ok(*(ULONG
*)DataBase
== 0x00905a4d, "Header not ok\n");
401 /* Close the image mapping */
402 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
403 NtClose(ImageSectionHandle
);
405 /* Create an image section again */
406 Status
= NtCreateSection(&ImageSectionHandle
,
407 SECTION_ALL_ACCESS
, // DesiredAccess
408 NULL
, // ObjectAttributes
410 PAGE_READWRITE
, // SectionPageProtection
411 SEC_IMAGE
, // AllocationAttributes
413 ok(Status
== STATUS_SUCCESS
, "NtCreateSection failed, Status 0x%lx\n", Status
);
415 /* Map the image section again */
418 Status
= NtMapViewOfSection(ImageSectionHandle
,
429 ok(Status
== STATUS_SUCCESS
, "NtMapViewOfSection failed, Status 0x%lx\n", Status
);
431 ok(Status
== STATUS_IMAGE_MACHINE_TYPE_MISMATCH
, "NtMapViewOfSection failed, Status 0x%lx\n", Status
);
434 /* Check the .data section again */
435 ok(*(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000) == 0xf00dada,
436 "Data should be synced: 0x%lx!\n", *(ULONG
*)((PUCHAR
)ImageBase
+ 0x80000));
438 /* Restore the original data */
439 *(ULONG
*)((PUCHAR
)DataBase
+ 0x800) = 0x12345678;
441 /* Close the data mapping */
442 NtUnmapViewOfSection(NtCurrentProcess(), DataBase
);
444 NtClose(DataSectionHandle
);
446 /* Try to allocate memory inside the image mapping */
447 DataBase
= (PUCHAR
)ImageBase
+ 0x20000;
449 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &DataBase
, 0, &ViewSize
, MEM_RESERVE
, PAGE_NOACCESS
);
450 ok(Status
== STATUS_CONFLICTING_ADDRESSES
, "Wrong Status: 0x%lx\n", Status
);
454 NtClose(ImageSectionHandle
);
455 NtUnmapViewOfSection(NtCurrentProcess(), ImageBase
);
459 START_TEST(NtMapViewOfSection
)
461 Test_PageFileSection();