Release 1.6-rc2.
[wine/testsucceed.git] / dlls / kernel32 / tests / loader.c
blobbd805089a3691c5673b41f45f1f535156975ae19
1 /*
2 * Unit test suite for the PE loader.
4 * Copyright 2006,2011 Dmitry Timoshkov
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <assert.h>
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winternl.h"
30 #include "wine/test.h"
32 #define ALIGN_SIZE(size, alignment) (((size) + (alignment - 1)) & ~((alignment - 1)))
34 struct PROCESS_BASIC_INFORMATION_PRIVATE
36 DWORD_PTR ExitStatus;
37 PPEB PebBaseAddress;
38 DWORD_PTR AffinityMask;
39 DWORD_PTR BasePriority;
40 ULONG_PTR UniqueProcessId;
41 ULONG_PTR InheritedFromUniqueProcessId;
44 static BOOL is_child;
45 static LONG *child_failures;
47 static NTSTATUS (WINAPI *pNtMapViewOfSection)(HANDLE, HANDLE, PVOID *, ULONG, SIZE_T, const LARGE_INTEGER *, SIZE_T *, ULONG, ULONG, ULONG);
48 static NTSTATUS (WINAPI *pNtUnmapViewOfSection)(HANDLE, PVOID);
49 static NTSTATUS (WINAPI *pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
50 static NTSTATUS (WINAPI *pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
51 static NTSTATUS (WINAPI *pNtTerminateProcess)(HANDLE, DWORD);
52 static void (WINAPI *pLdrShutdownProcess)(void);
53 static BOOLEAN (WINAPI *pRtlDllShutdownInProgress)(void);
54 static NTSTATUS (WINAPI *pNtAllocateVirtualMemory)(HANDLE, PVOID *, ULONG, SIZE_T *, ULONG, ULONG);
55 static NTSTATUS (WINAPI *pNtFreeVirtualMemory)(HANDLE, PVOID *, SIZE_T *, ULONG);
56 static NTSTATUS (WINAPI *pLdrLockLoaderLock)(ULONG, ULONG *, ULONG *);
57 static NTSTATUS (WINAPI *pLdrUnlockLoaderLock)(ULONG, ULONG);
58 static void (WINAPI *pRtlAcquirePebLock)(void);
59 static void (WINAPI *pRtlReleasePebLock)(void);
61 static PVOID RVAToAddr(DWORD_PTR rva, HMODULE module)
63 if (rva == 0)
64 return NULL;
65 return ((char*) module) + rva;
68 static const struct
70 WORD e_magic; /* 00: MZ Header signature */
71 WORD unused[29];
72 DWORD e_lfanew; /* 3c: Offset to extended header */
73 } dos_header =
75 IMAGE_DOS_SIGNATURE, { 0 }, sizeof(dos_header)
78 static IMAGE_NT_HEADERS nt_header =
80 IMAGE_NT_SIGNATURE, /* Signature */
82 #if defined __i386__
83 IMAGE_FILE_MACHINE_I386, /* Machine */
84 #elif defined __x86_64__
85 IMAGE_FILE_MACHINE_AMD64, /* Machine */
86 #elif defined __powerpc__
87 IMAGE_FILE_MACHINE_POWERPC, /* Machine */
88 #elif defined __arm__
89 IMAGE_FILE_MACHINE_ARMNT, /* Machine */
90 #elif defined __aarch64__
91 IMAGE_FILE_MACHINE_ARM64, /* Machine */
92 #else
93 # error You must specify the machine type
94 #endif
95 1, /* NumberOfSections */
96 0, /* TimeDateStamp */
97 0, /* PointerToSymbolTable */
98 0, /* NumberOfSymbols */
99 sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
100 IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
102 { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
103 1, /* MajorLinkerVersion */
104 0, /* MinorLinkerVersion */
105 0, /* SizeOfCode */
106 0, /* SizeOfInitializedData */
107 0, /* SizeOfUninitializedData */
108 0, /* AddressOfEntryPoint */
109 0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
110 #ifndef _WIN64
111 0, /* BaseOfData */
112 #endif
113 0x10000000, /* ImageBase */
114 0, /* SectionAlignment */
115 0, /* FileAlignment */
116 4, /* MajorOperatingSystemVersion */
117 0, /* MinorOperatingSystemVersion */
118 1, /* MajorImageVersion */
119 0, /* MinorImageVersion */
120 4, /* MajorSubsystemVersion */
121 0, /* MinorSubsystemVersion */
122 0, /* Win32VersionValue */
123 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000, /* SizeOfImage */
124 sizeof(dos_header) + sizeof(nt_header), /* SizeOfHeaders */
125 0, /* CheckSum */
126 IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
127 0, /* DllCharacteristics */
128 0, /* SizeOfStackReserve */
129 0, /* SizeOfStackCommit */
130 0, /* SizeOfHeapReserve */
131 0, /* SizeOfHeapCommit */
132 0, /* LoaderFlags */
133 0, /* NumberOfRvaAndSizes */
134 { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
138 static IMAGE_SECTION_HEADER section =
140 ".rodata", /* Name */
141 { 0x10 }, /* Misc */
142 0, /* VirtualAddress */
143 0x0a, /* SizeOfRawData */
144 0, /* PointerToRawData */
145 0, /* PointerToRelocations */
146 0, /* PointerToLinenumbers */
147 0, /* NumberOfRelocations */
148 0, /* NumberOfLinenumbers */
149 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, /* Characteristics */
152 static void test_Loader(void)
154 static const struct test_data
156 const void *dos_header;
157 DWORD size_of_dos_header;
158 WORD number_of_sections, size_of_optional_header;
159 DWORD section_alignment, file_alignment;
160 DWORD size_of_image, size_of_headers;
161 DWORD errors[4]; /* 0 means LoadLibrary should succeed */
162 } td[] =
164 { &dos_header, sizeof(dos_header),
165 1, 0, 0, 0, 0, 0,
166 { ERROR_BAD_EXE_FORMAT }
168 { &dos_header, sizeof(dos_header),
169 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
170 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0xe00,
171 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
172 { ERROR_BAD_EXE_FORMAT } /* XP doesn't like too small image size */
174 { &dos_header, sizeof(dos_header),
175 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
176 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
177 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
178 { ERROR_SUCCESS }
180 { &dos_header, sizeof(dos_header),
181 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
182 0x1f00,
183 0x1000,
184 { ERROR_SUCCESS }
186 { &dos_header, sizeof(dos_header),
187 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x200,
188 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x200,
189 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
190 { ERROR_SUCCESS, ERROR_INVALID_ADDRESS } /* vista is more strict */
192 { &dos_header, sizeof(dos_header),
193 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x1000,
194 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
195 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
196 { ERROR_BAD_EXE_FORMAT } /* XP doesn't like alignments */
198 { &dos_header, sizeof(dos_header),
199 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
200 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
201 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
202 { ERROR_SUCCESS }
204 { &dos_header, sizeof(dos_header),
205 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
206 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
207 0x200,
208 { ERROR_SUCCESS }
210 /* Mandatory are all fields up to SizeOfHeaders, everything else
211 * is really optional (at least that's true for XP).
213 { &dos_header, sizeof(dos_header),
214 1, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
215 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER) + 0x10,
216 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER),
217 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT, ERROR_INVALID_ADDRESS,
218 ERROR_NOACCESS }
220 { &dos_header, sizeof(dos_header),
221 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
222 0xd0, /* beyond of the end of file */
223 0xc0, /* beyond of the end of file */
224 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
226 { &dos_header, sizeof(dos_header),
227 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
228 0x1000,
230 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
232 { &dos_header, sizeof(dos_header),
233 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
236 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
238 #if 0 /* not power of 2 alignments need more test cases */
239 { &dos_header, sizeof(dos_header),
240 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x300, 0x300,
243 { ERROR_BAD_EXE_FORMAT } /* alignment is not power of 2 */
245 #endif
246 { &dos_header, sizeof(dos_header),
247 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 4, 4,
250 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
252 { &dos_header, sizeof(dos_header),
253 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 1, 1,
256 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
258 { &dos_header, sizeof(dos_header),
259 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
262 { ERROR_BAD_EXE_FORMAT } /* image size == 0 -> failure */
264 /* the following data mimics the PE image which upack creates */
265 { &dos_header, 0x10,
266 1, 0x148, 0x1000, 0x200,
267 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
268 0x200,
269 { ERROR_SUCCESS }
271 /* Minimal PE image that XP is able to load: 92 bytes */
272 { &dos_header, 0x04,
273 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum),
274 0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
277 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
280 static const char filler[0x1000];
281 static const char section_data[0x10] = "section data";
282 int i;
283 DWORD dummy, file_size, file_align;
284 HANDLE hfile;
285 HMODULE hlib, hlib_as_data_file;
286 SYSTEM_INFO si;
287 char temp_path[MAX_PATH];
288 char dll_name[MAX_PATH];
289 SIZE_T size;
290 BOOL ret;
292 GetSystemInfo(&si);
294 /* prevent displaying of the "Unable to load this DLL" message box */
295 SetErrorMode(SEM_FAILCRITICALERRORS);
297 GetTempPath(MAX_PATH, temp_path);
299 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
301 GetTempFileName(temp_path, "ldr", 0, dll_name);
303 /*trace("creating %s\n", dll_name);*/
304 hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
305 if (hfile == INVALID_HANDLE_VALUE)
307 ok(0, "could not create %s\n", dll_name);
308 break;
311 SetLastError(0xdeadbeef);
312 ret = WriteFile(hfile, td[i].dos_header, td[i].size_of_dos_header, &dummy, NULL);
313 ok(ret, "WriteFile error %d\n", GetLastError());
315 nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
316 nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
318 nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
319 nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
320 nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
321 nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
322 SetLastError(0xdeadbeef);
323 ret = WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
324 ok(ret, "WriteFile error %d\n", GetLastError());
326 if (nt_header.FileHeader.SizeOfOptionalHeader)
328 SetLastError(0xdeadbeef);
329 ret = WriteFile(hfile, &nt_header.OptionalHeader,
330 min(nt_header.FileHeader.SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER)),
331 &dummy, NULL);
332 ok(ret, "WriteFile error %d\n", GetLastError());
333 if (nt_header.FileHeader.SizeOfOptionalHeader > sizeof(IMAGE_OPTIONAL_HEADER))
335 file_align = nt_header.FileHeader.SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER);
336 assert(file_align < sizeof(filler));
337 SetLastError(0xdeadbeef);
338 ret = WriteFile(hfile, filler, file_align, &dummy, NULL);
339 ok(ret, "WriteFile error %d\n", GetLastError());
343 assert(nt_header.FileHeader.NumberOfSections <= 1);
344 if (nt_header.FileHeader.NumberOfSections)
346 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
348 section.PointerToRawData = td[i].size_of_dos_header;
349 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
350 section.Misc.VirtualSize = section.SizeOfRawData * 10;
352 else
354 section.PointerToRawData = nt_header.OptionalHeader.SizeOfHeaders;
355 section.VirtualAddress = nt_header.OptionalHeader.SizeOfHeaders;
356 section.Misc.VirtualSize = 5;
359 SetLastError(0xdeadbeef);
360 ret = WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
361 ok(ret, "WriteFile error %d\n", GetLastError());
363 /* section data */
364 SetLastError(0xdeadbeef);
365 ret = WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL);
366 ok(ret, "WriteFile error %d\n", GetLastError());
369 file_size = GetFileSize(hfile, NULL);
370 CloseHandle(hfile);
372 SetLastError(0xdeadbeef);
373 hlib = LoadLibrary(dll_name);
374 if (hlib)
376 MEMORY_BASIC_INFORMATION info;
378 ok( td[i].errors[0] == ERROR_SUCCESS, "%d: should have failed\n", i );
380 SetLastError(0xdeadbeef);
381 size = VirtualQuery(hlib, &info, sizeof(info));
382 ok(size == sizeof(info),
383 "%d: VirtualQuery error %d\n", i, GetLastError());
384 ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
385 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
386 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
387 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
388 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
389 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
390 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
391 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
392 else
393 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
394 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
396 SetLastError(0xdeadbeef);
397 size = VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info));
398 ok(size == sizeof(info),
399 "%d: VirtualQuery error %d\n", i, GetLastError());
400 if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize ||
401 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
403 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %p != expected %p\n",
404 i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
405 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
406 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
407 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
408 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
409 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
410 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
412 else
414 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
415 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
416 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
417 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
418 ok(info.RegionSize == ALIGN_SIZE(file_size, si.dwPageSize), "%d: got %lx != expected %x\n",
419 i, info.RegionSize, ALIGN_SIZE(file_size, si.dwPageSize));
420 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
421 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
422 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
425 /* header: check the zeroing of alignment */
426 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
428 const char *start;
430 start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
431 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
432 ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
435 if (nt_header.FileHeader.NumberOfSections)
437 SetLastError(0xdeadbeef);
438 size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
439 ok(size == sizeof(info),
440 "%d: VirtualQuery error %d\n", i, GetLastError());
441 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
443 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
444 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
445 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
446 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
448 else
450 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
451 ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize), "%d: got %lx != expected %x\n",
452 i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize));
453 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
455 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
456 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
457 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
458 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
460 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
461 ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
462 else
463 ok(!memcmp((const char *)hlib + section.PointerToRawData, section_data, section.SizeOfRawData), "wrong section data\n");
465 /* check the zeroing of alignment */
466 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
468 const char *start;
470 start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
471 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
472 ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
476 SetLastError(0xdeadbeef);
477 hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
478 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
479 ok(hlib_as_data_file == hlib, "hlib_as_file and hlib are different\n");
481 SetLastError(0xdeadbeef);
482 ret = FreeLibrary(hlib);
483 ok(ret, "FreeLibrary error %d\n", GetLastError());
485 SetLastError(0xdeadbeef);
486 hlib = GetModuleHandle(dll_name);
487 ok(hlib != 0, "GetModuleHandle error %u\n", GetLastError());
489 SetLastError(0xdeadbeef);
490 ret = FreeLibrary(hlib_as_data_file);
491 ok(ret, "FreeLibrary error %d\n", GetLastError());
493 hlib = GetModuleHandle(dll_name);
494 ok(!hlib, "GetModuleHandle should fail\n");
496 SetLastError(0xdeadbeef);
497 hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
498 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
499 ok((ULONG_PTR)hlib_as_data_file & 1, "hlib_as_data_file is even\n");
501 hlib = GetModuleHandle(dll_name);
502 ok(!hlib, "GetModuleHandle should fail\n");
504 SetLastError(0xdeadbeef);
505 ret = FreeLibrary(hlib_as_data_file);
506 ok(ret, "FreeLibrary error %d\n", GetLastError());
508 else
510 BOOL error_match;
511 int error_index;
513 error_match = FALSE;
514 for (error_index = 0;
515 ! error_match && error_index < sizeof(td[i].errors) / sizeof(DWORD);
516 error_index++)
518 error_match = td[i].errors[error_index] == GetLastError();
520 ok(error_match, "%d: unexpected error %d\n", i, GetLastError());
523 SetLastError(0xdeadbeef);
524 ret = DeleteFile(dll_name);
525 ok(ret, "DeleteFile error %d\n", GetLastError());
529 /* Verify linking style of import descriptors */
530 static void test_ImportDescriptors(void)
532 HMODULE kernel32_module = NULL;
533 PIMAGE_DOS_HEADER d_header;
534 PIMAGE_NT_HEADERS nt_headers;
535 DWORD import_dir_size;
536 DWORD_PTR dir_offset;
537 PIMAGE_IMPORT_DESCRIPTOR import_chunk;
539 /* Load kernel32 module */
540 kernel32_module = GetModuleHandleA("kernel32.dll");
541 assert( kernel32_module != NULL );
543 /* Get PE header info from module image */
544 d_header = (PIMAGE_DOS_HEADER) kernel32_module;
545 nt_headers = (PIMAGE_NT_HEADERS) (((char*) d_header) +
546 d_header->e_lfanew);
548 /* Get size of import entry directory */
549 import_dir_size = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
550 if (!import_dir_size)
552 skip("Unable to continue testing due to missing import directory.\n");
553 return;
556 /* Get address of first import chunk */
557 dir_offset = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
558 import_chunk = RVAToAddr(dir_offset, kernel32_module);
559 ok(import_chunk != 0, "Invalid import_chunk: %p\n", import_chunk);
560 if (!import_chunk) return;
562 /* Iterate through import descriptors and verify set name,
563 * OriginalFirstThunk, and FirstThunk. Core Windows DLLs, such as
564 * kernel32.dll, don't use Borland-style linking, where the table of
565 * imported names is stored directly in FirstThunk and overwritten
566 * by the relocation, instead of being stored in OriginalFirstThunk.
567 * */
568 for (; import_chunk->FirstThunk; import_chunk++)
570 LPCSTR module_name = RVAToAddr(import_chunk->Name, kernel32_module);
571 PIMAGE_THUNK_DATA name_table = RVAToAddr(
572 U(*import_chunk).OriginalFirstThunk, kernel32_module);
573 PIMAGE_THUNK_DATA iat = RVAToAddr(
574 import_chunk->FirstThunk, kernel32_module);
575 ok(module_name != NULL, "Imported module name should not be NULL\n");
576 ok(name_table != NULL,
577 "Name table for imported module %s should not be NULL\n",
578 module_name);
579 ok(iat != NULL, "IAT for imported module %s should not be NULL\n",
580 module_name);
584 static void test_image_mapping(const char *dll_name, DWORD scn_page_access, BOOL is_dll)
586 HANDLE hfile, hmap;
587 NTSTATUS status;
588 LARGE_INTEGER offset;
589 SIZE_T size;
590 void *addr1, *addr2;
591 MEMORY_BASIC_INFORMATION info;
592 SYSTEM_INFO si;
594 if (!pNtMapViewOfSection) return;
596 GetSystemInfo(&si);
598 SetLastError(0xdeadbeef);
599 hfile = CreateFile(dll_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
600 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
602 SetLastError(0xdeadbeef);
603 hmap = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, 0);
604 ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
606 offset.u.LowPart = 0;
607 offset.u.HighPart = 0;
609 addr1 = NULL;
610 size = 0;
611 status = pNtMapViewOfSection(hmap, GetCurrentProcess(), &addr1, 0, 0, &offset,
612 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
613 ok(status == STATUS_SUCCESS, "NtMapViewOfSection error %x\n", status);
614 ok(addr1 != 0, "mapped address should be valid\n");
616 SetLastError(0xdeadbeef);
617 size = VirtualQuery((char *)addr1 + section.VirtualAddress, &info, sizeof(info));
618 ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
619 ok(info.BaseAddress == (char *)addr1 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr1 + section.VirtualAddress);
620 ok(info.RegionSize == si.dwPageSize, "got %#lx != expected %#x\n", info.RegionSize, si.dwPageSize);
621 ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
622 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
623 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
624 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
625 ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
627 addr2 = NULL;
628 size = 0;
629 status = pNtMapViewOfSection(hmap, GetCurrentProcess(), &addr2, 0, 0, &offset,
630 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
631 /* FIXME: remove once Wine is fixed */
632 if (status != STATUS_IMAGE_NOT_AT_BASE)
634 todo_wine {
635 ok(status == STATUS_IMAGE_NOT_AT_BASE, "expected STATUS_IMAGE_NOT_AT_BASE, got %x\n", status);
636 ok(addr2 != 0, "mapped address should be valid\n");
638 goto wine_is_broken;
640 ok(status == STATUS_IMAGE_NOT_AT_BASE, "expected STATUS_IMAGE_NOT_AT_BASE, got %x\n", status);
641 ok(addr2 != 0, "mapped address should be valid\n");
642 ok(addr2 != addr1, "mapped addresses should be different\n");
644 SetLastError(0xdeadbeef);
645 size = VirtualQuery((char *)addr2 + section.VirtualAddress, &info, sizeof(info));
646 ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
647 ok(info.BaseAddress == (char *)addr2 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr2 + section.VirtualAddress);
648 ok(info.RegionSize == si.dwPageSize, "got %#lx != expected %#x\n", info.RegionSize, si.dwPageSize);
649 ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
650 ok(info.AllocationBase == addr2, "%p != %p\n", info.AllocationBase, addr2);
651 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
652 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
653 ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
655 status = pNtUnmapViewOfSection(GetCurrentProcess(), addr2);
656 ok(status == STATUS_SUCCESS, "NtUnmapViewOfSection error %x\n", status);
658 addr2 = MapViewOfFile(hmap, 0, 0, 0, 0);
659 ok(addr2 != 0, "mapped address should be valid\n");
660 ok(addr2 != addr1, "mapped addresses should be different\n");
662 SetLastError(0xdeadbeef);
663 size = VirtualQuery((char *)addr2 + section.VirtualAddress, &info, sizeof(info));
664 ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
665 ok(info.BaseAddress == (char *)addr2 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr2 + section.VirtualAddress);
666 ok(info.RegionSize == si.dwPageSize, "got %#lx != expected %#x\n", info.RegionSize, si.dwPageSize);
667 ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
668 ok(info.AllocationBase == addr2, "%p != %p\n", info.AllocationBase, addr2);
669 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
670 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
671 ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
673 UnmapViewOfFile(addr2);
675 SetLastError(0xdeadbeef);
676 addr2 = LoadLibrary(dll_name);
677 if (is_dll)
679 ok(!addr2, "LoadLibrary should fail, is_dll %d\n", is_dll);
680 ok(GetLastError() == ERROR_INVALID_ADDRESS, "expected ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
682 else
684 BOOL ret;
685 ok(addr2 != 0, "LoadLibrary error %d, is_dll %d\n", GetLastError(), is_dll);
686 ok(addr2 != addr1, "mapped addresses should be different\n");
688 SetLastError(0xdeadbeef);
689 ret = FreeLibrary(addr2);
690 ok(ret, "FreeLibrary error %d\n", GetLastError());
693 wine_is_broken:
694 status = pNtUnmapViewOfSection(GetCurrentProcess(), addr1);
695 ok(status == STATUS_SUCCESS, "NtUnmapViewOfSection error %x\n", status);
697 CloseHandle(hmap);
698 CloseHandle(hfile);
701 static BOOL is_mem_writable(DWORD prot)
703 switch (prot & 0xff)
705 case PAGE_READWRITE:
706 case PAGE_WRITECOPY:
707 case PAGE_EXECUTE_READWRITE:
708 case PAGE_EXECUTE_WRITECOPY:
709 return TRUE;
711 default:
712 return FALSE;
716 static void test_VirtualProtect(void *base, void *section)
718 static const struct test_data
720 DWORD prot_set, prot_get;
721 } td[] =
723 { 0, 0 }, /* 0x00 */
724 { PAGE_NOACCESS, PAGE_NOACCESS }, /* 0x01 */
725 { PAGE_READONLY, PAGE_READONLY }, /* 0x02 */
726 { PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x03 */
727 { PAGE_READWRITE, PAGE_WRITECOPY }, /* 0x04 */
728 { PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x05 */
729 { PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x06 */
730 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x07 */
731 { PAGE_WRITECOPY, PAGE_WRITECOPY }, /* 0x08 */
732 { PAGE_WRITECOPY | PAGE_NOACCESS, 0 }, /* 0x09 */
733 { PAGE_WRITECOPY | PAGE_READONLY, 0 }, /* 0x0a */
734 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, 0 }, /* 0x0b */
735 { PAGE_WRITECOPY | PAGE_READWRITE, 0 }, /* 0x0c */
736 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x0d */
737 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x0e */
738 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x0f */
740 { PAGE_EXECUTE, PAGE_EXECUTE }, /* 0x10 */
741 { PAGE_EXECUTE_READ, PAGE_EXECUTE_READ }, /* 0x20 */
742 { PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x30 */
743 { PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY }, /* 0x40 */
744 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0x50 */
745 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0x60 */
746 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x70 */
747 { PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_WRITECOPY }, /* 0x80 */
748 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, 0 }, /* 0x90 */
749 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, 0 }, /* 0xa0 */
750 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0xb0 */
751 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, 0 }, /* 0xc0 */
752 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0xd0 */
753 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0xe0 */
754 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 } /* 0xf0 */
756 DWORD ret, orig_prot, old_prot, rw_prot, exec_prot, i, j;
757 MEMORY_BASIC_INFORMATION info;
758 SYSTEM_INFO si;
760 GetSystemInfo(&si);
762 SetLastError(0xdeadbeef);
763 ret = VirtualProtect(section, si.dwPageSize, PAGE_NOACCESS, &old_prot);
764 ok(ret, "VirtualProtect error %d\n", GetLastError());
766 orig_prot = old_prot;
768 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
770 SetLastError(0xdeadbeef);
771 ret = VirtualQuery(section, &info, sizeof(info));
772 ok(ret, "VirtualQuery failed %d\n", GetLastError());
773 ok(info.BaseAddress == section, "%d: got %p != expected %p\n", i, info.BaseAddress, section);
774 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
775 ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
776 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
777 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
778 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
779 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
781 old_prot = 0xdeadbeef;
782 SetLastError(0xdeadbeef);
783 ret = VirtualProtect(section, si.dwPageSize, td[i].prot_set, &old_prot);
784 if (td[i].prot_get)
786 ok(ret, "%d: VirtualProtect error %d, requested prot %#x\n", i, GetLastError(), td[i].prot_set);
787 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
789 SetLastError(0xdeadbeef);
790 ret = VirtualQuery(section, &info, sizeof(info));
791 ok(ret, "VirtualQuery failed %d\n", GetLastError());
792 ok(info.BaseAddress == section, "%d: got %p != expected %p\n", i, info.BaseAddress, section);
793 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
794 ok(info.Protect == td[i].prot_get, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_get);
795 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
796 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
797 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
798 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
800 else
802 ok(!ret, "%d: VirtualProtect should fail\n", i);
803 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
806 old_prot = 0xdeadbeef;
807 SetLastError(0xdeadbeef);
808 ret = VirtualProtect(section, si.dwPageSize, PAGE_NOACCESS, &old_prot);
809 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
810 if (td[i].prot_get)
811 ok(old_prot == td[i].prot_get, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_get);
812 else
813 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
816 exec_prot = 0;
818 for (i = 0; i <= 4; i++)
820 rw_prot = 0;
822 for (j = 0; j <= 4; j++)
824 DWORD prot = exec_prot | rw_prot;
826 SetLastError(0xdeadbeef);
827 ret = VirtualProtect(section, si.dwPageSize, prot, &old_prot);
828 if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
830 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
831 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
833 else
834 ok(ret, "VirtualProtect(%02x) error %d\n", prot, GetLastError());
836 rw_prot = 1 << j;
839 exec_prot = 1 << (i + 4);
842 SetLastError(0xdeadbeef);
843 ret = VirtualProtect(section, si.dwPageSize, orig_prot, &old_prot);
844 ok(ret, "VirtualProtect error %d\n", GetLastError());
847 static void test_section_access(void)
849 static const struct test_data
851 DWORD scn_file_access, scn_page_access, scn_page_access_after_write;
852 } td[] =
854 { 0, PAGE_NOACCESS, 0 },
855 { IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
856 { IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
857 { IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
858 { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
859 { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ },
860 { IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
861 { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
863 { IMAGE_SCN_CNT_INITIALIZED_DATA, PAGE_NOACCESS, 0 },
864 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
865 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
866 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
867 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
868 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ, 0 },
869 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
870 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
872 { IMAGE_SCN_CNT_UNINITIALIZED_DATA, PAGE_NOACCESS, 0 },
873 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
874 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
875 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
876 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
877 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ, 0 },
878 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
879 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE }
881 static const char filler[0x1000];
882 static const char section_data[0x10] = "section data";
883 char buf[256];
884 int i;
885 DWORD dummy, file_align;
886 HANDLE hfile;
887 HMODULE hlib;
888 SYSTEM_INFO si;
889 char temp_path[MAX_PATH];
890 char dll_name[MAX_PATH];
891 SIZE_T size;
892 MEMORY_BASIC_INFORMATION info;
893 STARTUPINFO sti;
894 PROCESS_INFORMATION pi;
895 DWORD ret;
897 GetSystemInfo(&si);
899 /* prevent displaying of the "Unable to load this DLL" message box */
900 SetErrorMode(SEM_FAILCRITICALERRORS);
902 GetTempPath(MAX_PATH, temp_path);
904 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
906 GetTempFileName(temp_path, "ldr", 0, dll_name);
908 /*trace("creating %s\n", dll_name);*/
909 hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
910 if (hfile == INVALID_HANDLE_VALUE)
912 ok(0, "could not create %s\n", dll_name);
913 return;
916 SetLastError(0xdeadbeef);
917 ret = WriteFile(hfile, &dos_header, sizeof(dos_header), &dummy, NULL);
918 ok(ret, "WriteFile error %d\n", GetLastError());
920 nt_header.FileHeader.NumberOfSections = 1;
921 nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
922 nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL | IMAGE_FILE_RELOCS_STRIPPED;
924 nt_header.OptionalHeader.SectionAlignment = si.dwPageSize;
925 nt_header.OptionalHeader.FileAlignment = 0x200;
926 nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + si.dwPageSize;
927 nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER);
928 SetLastError(0xdeadbeef);
929 ret = WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
930 ok(ret, "WriteFile error %d\n", GetLastError());
931 SetLastError(0xdeadbeef);
932 ret = WriteFile(hfile, &nt_header.OptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER), &dummy, NULL);
933 ok(ret, "WriteFile error %d\n", GetLastError());
935 section.SizeOfRawData = sizeof(section_data);
936 section.PointerToRawData = nt_header.OptionalHeader.FileAlignment;
937 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
938 section.Misc.VirtualSize = section.SizeOfRawData;
939 section.Characteristics = td[i].scn_file_access;
940 SetLastError(0xdeadbeef);
941 ret = WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
942 ok(ret, "WriteFile error %d\n", GetLastError());
944 file_align = nt_header.OptionalHeader.FileAlignment - nt_header.OptionalHeader.SizeOfHeaders;
945 assert(file_align < sizeof(filler));
946 SetLastError(0xdeadbeef);
947 ret = WriteFile(hfile, filler, file_align, &dummy, NULL);
948 ok(ret, "WriteFile error %d\n", GetLastError());
950 /* section data */
951 SetLastError(0xdeadbeef);
952 ret = WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL);
953 ok(ret, "WriteFile error %d\n", GetLastError());
955 CloseHandle(hfile);
957 SetLastError(0xdeadbeef);
958 hlib = LoadLibrary(dll_name);
959 ok(hlib != 0, "LoadLibrary error %d\n", GetLastError());
961 SetLastError(0xdeadbeef);
962 size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
963 ok(size == sizeof(info),
964 "%d: VirtualQuery error %d\n", i, GetLastError());
965 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
966 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
967 ok(info.Protect == td[i].scn_page_access, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].scn_page_access);
968 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
969 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
970 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
971 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
972 if (info.Protect != PAGE_NOACCESS)
973 ok(!memcmp((const char *)info.BaseAddress, section_data, section.SizeOfRawData), "wrong section data\n");
975 test_VirtualProtect(hlib, (char *)hlib + section.VirtualAddress);
977 /* Windows changes the WRITECOPY to WRITE protection on an image section write (for a changed page only) */
978 if (is_mem_writable(info.Protect))
980 char *p = info.BaseAddress;
981 *p = 0xfe;
982 SetLastError(0xdeadbeef);
983 size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
984 ok(size == sizeof(info), "%d: VirtualQuery error %d\n", i, GetLastError());
985 /* FIXME: remove the condition below once Wine is fixed */
986 if (info.Protect == PAGE_WRITECOPY || info.Protect == PAGE_EXECUTE_WRITECOPY)
987 todo_wine ok(info.Protect == td[i].scn_page_access_after_write, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].scn_page_access_after_write);
988 else
989 ok(info.Protect == td[i].scn_page_access_after_write, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].scn_page_access_after_write);
992 SetLastError(0xdeadbeef);
993 ret = FreeLibrary(hlib);
994 ok(ret, "FreeLibrary error %d\n", GetLastError());
996 test_image_mapping(dll_name, td[i].scn_page_access, TRUE);
998 /* reset IMAGE_FILE_DLL otherwise CreateProcess fails */
999 nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_RELOCS_STRIPPED;
1000 SetLastError(0xdeadbeef);
1001 hfile = CreateFile(dll_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1002 /* LoadLibrary called on an already memory-mapped file in
1003 * test_image_mapping() above leads to a file handle leak
1004 * under nt4, and inability to overwrite and delete the file
1005 * due to sharing violation error. Ignore it and skip the test,
1006 * but leave a not deletable temporary file.
1008 ok(hfile != INVALID_HANDLE_VALUE || broken(hfile == INVALID_HANDLE_VALUE) /* nt4 */,
1009 "CreateFile error %d\n", GetLastError());
1010 if (hfile == INVALID_HANDLE_VALUE) goto nt4_is_broken;
1011 SetFilePointer(hfile, sizeof(dos_header), NULL, FILE_BEGIN);
1012 SetLastError(0xdeadbeef);
1013 ret = WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
1014 ok(ret, "WriteFile error %d\n", GetLastError());
1015 CloseHandle(hfile);
1017 memset(&sti, 0, sizeof(sti));
1018 sti.cb = sizeof(sti);
1019 SetLastError(0xdeadbeef);
1020 ret = CreateProcess(dll_name, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &sti, &pi);
1021 ok(ret, "CreateProcess() error %d\n", GetLastError());
1023 SetLastError(0xdeadbeef);
1024 size = VirtualQueryEx(pi.hProcess, (char *)hlib + section.VirtualAddress, &info, sizeof(info));
1025 ok(size == sizeof(info),
1026 "%d: VirtualQuery error %d\n", i, GetLastError());
1027 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
1028 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1029 ok(info.Protect == td[i].scn_page_access, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].scn_page_access);
1030 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
1031 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
1032 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1033 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
1034 if (info.Protect != PAGE_NOACCESS)
1036 SetLastError(0xdeadbeef);
1037 ret = ReadProcessMemory(pi.hProcess, info.BaseAddress, buf, section.SizeOfRawData, NULL);
1038 ok(ret, "ReadProcessMemory() error %d\n", GetLastError());
1039 ok(!memcmp(buf, section_data, section.SizeOfRawData), "wrong section data\n");
1042 SetLastError(0xdeadbeef);
1043 ret = TerminateProcess(pi.hProcess, 0);
1044 ok(ret, "TerminateProcess() error %d\n", GetLastError());
1045 ret = WaitForSingleObject(pi.hProcess, 3000);
1046 ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
1048 CloseHandle(pi.hThread);
1049 CloseHandle(pi.hProcess);
1051 test_image_mapping(dll_name, td[i].scn_page_access, FALSE);
1053 nt4_is_broken:
1054 SetLastError(0xdeadbeef);
1055 ret = DeleteFile(dll_name);
1056 ok(ret || broken(!ret) /* nt4 */, "DeleteFile error %d\n", GetLastError());
1060 #define MAX_COUNT 10
1061 static HANDLE attached_thread[MAX_COUNT];
1062 static DWORD attached_thread_count;
1063 HANDLE stop_event, event, mutex, semaphore, loader_lock_event, peb_lock_event, heap_lock_event, ack_event;
1064 static int test_dll_phase, inside_loader_lock, inside_peb_lock, inside_heap_lock;
1066 static DWORD WINAPI mutex_thread_proc(void *param)
1068 HANDLE wait_list[4];
1069 DWORD ret;
1071 ret = WaitForSingleObject(mutex, 0);
1072 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1074 SetEvent(param);
1076 wait_list[0] = stop_event;
1077 wait_list[1] = loader_lock_event;
1078 wait_list[2] = peb_lock_event;
1079 wait_list[3] = heap_lock_event;
1081 while (1)
1083 trace("%04u: mutex_thread_proc: still alive\n", GetCurrentThreadId());
1084 ret = WaitForMultipleObjects(sizeof(wait_list)/sizeof(wait_list[0]), wait_list, FALSE, 50);
1085 if (ret == WAIT_OBJECT_0) break;
1086 else if (ret == WAIT_OBJECT_0 + 1)
1088 ULONG loader_lock_magic;
1089 trace("%04u: mutex_thread_proc: Entering loader lock\n", GetCurrentThreadId());
1090 ret = pLdrLockLoaderLock(0, NULL, &loader_lock_magic);
1091 ok(!ret, "LdrLockLoaderLock error %#x\n", ret);
1092 inside_loader_lock++;
1093 SetEvent(ack_event);
1095 else if (ret == WAIT_OBJECT_0 + 2)
1097 trace("%04u: mutex_thread_proc: Entering PEB lock\n", GetCurrentThreadId());
1098 pRtlAcquirePebLock();
1099 inside_peb_lock++;
1100 SetEvent(ack_event);
1102 else if (ret == WAIT_OBJECT_0 + 3)
1104 trace("%04u: mutex_thread_proc: Entering heap lock\n", GetCurrentThreadId());
1105 HeapLock(GetProcessHeap());
1106 inside_heap_lock++;
1107 SetEvent(ack_event);
1111 trace("%04u: mutex_thread_proc: exiting\n", GetCurrentThreadId());
1112 return 196;
1115 static DWORD WINAPI semaphore_thread_proc(void *param)
1117 DWORD ret;
1119 ret = WaitForSingleObject(semaphore, 0);
1120 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1122 SetEvent(param);
1124 while (1)
1126 trace("%04u: semaphore_thread_proc: still alive\n", GetCurrentThreadId());
1127 if (WaitForSingleObject(stop_event, 50) != WAIT_TIMEOUT) break;
1130 trace("%04u: semaphore_thread_proc: exiting\n", GetCurrentThreadId());
1131 return 196;
1134 static DWORD WINAPI noop_thread_proc(void *param)
1136 if (param)
1138 LONG *noop_thread_started = param;
1139 InterlockedIncrement(noop_thread_started);
1142 trace("%04u: noop_thread_proc: exiting\n", GetCurrentThreadId());
1143 return 195;
1146 static BOOL WINAPI dll_entry_point(HINSTANCE hinst, DWORD reason, LPVOID param)
1148 static LONG noop_thread_started;
1149 DWORD ret;
1151 ok(!inside_loader_lock, "inside_loader_lock should not be set\n");
1152 ok(!inside_peb_lock, "inside_peb_lock should not be set\n");
1154 switch (reason)
1156 case DLL_PROCESS_ATTACH:
1157 trace("dll: %p, DLL_PROCESS_ATTACH, %p\n", hinst, param);
1159 ret = pRtlDllShutdownInProgress();
1160 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1162 break;
1163 case DLL_PROCESS_DETACH:
1165 DWORD code, expected_code, i;
1166 HANDLE handle, process;
1167 void *addr;
1168 SIZE_T size;
1169 LARGE_INTEGER offset;
1170 DEBUG_EVENT de;
1172 trace("dll: %p, DLL_PROCESS_DETACH, %p\n", hinst, param);
1174 if (test_dll_phase == 4 || test_dll_phase == 5)
1176 ok(0, "dll_entry_point(DLL_PROCESS_DETACH) should not be called\n");
1177 break;
1180 /* The process should already deadlock at this point */
1181 if (test_dll_phase == 6)
1183 /* In reality, code below never gets executed, probably some other
1184 * code tries to access process heap and deadlocks earlier, even XP
1185 * doesn't call the DLL entry point on process detach either.
1187 HeapLock(GetProcessHeap());
1188 ok(0, "dll_entry_point: process should already deadlock\n");
1189 break;
1192 if (test_dll_phase == 0 || test_dll_phase == 1 || test_dll_phase == 3)
1193 ok(param != NULL, "dll: param %p\n", param);
1194 else
1195 ok(!param, "dll: param %p\n", param);
1197 if (test_dll_phase == 0 || test_dll_phase == 1) expected_code = 195;
1198 else if (test_dll_phase == 3) expected_code = 196;
1199 else expected_code = STILL_ACTIVE;
1201 if (test_dll_phase == 3)
1203 ret = pRtlDllShutdownInProgress();
1204 ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
1206 else
1208 ret = pRtlDllShutdownInProgress();
1210 /* FIXME: remove once Wine is fixed */
1211 if (expected_code == STILL_ACTIVE || expected_code == 196)
1212 ok(!ret || broken(ret) /* before Vista */, "RtlDllShutdownInProgress returned %d\n", ret);
1213 else
1214 todo_wine
1215 ok(!ret || broken(ret) /* before Vista */, "RtlDllShutdownInProgress returned %d\n", ret);
1218 ok(attached_thread_count >= 2, "attached thread count should be >= 2\n");
1220 for (i = 0; i < attached_thread_count; i++)
1222 /* Calling GetExitCodeThread() without waiting for thread termination
1223 * leads to different results due to a race condition.
1225 if (expected_code != STILL_ACTIVE)
1227 ret = WaitForSingleObject(attached_thread[i], 1000);
1228 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1230 ret = GetExitCodeThread(attached_thread[i], &code);
1231 trace("dll: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
1232 ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
1233 ok(code == expected_code, "expected thread exit code %u, got %u\n", expected_code, code);
1236 ret = WaitForSingleObject(event, 0);
1237 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1239 ret = WaitForSingleObject(mutex, 0);
1240 if (expected_code == STILL_ACTIVE)
1241 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1242 else
1243 ok(ret == WAIT_ABANDONED, "expected WAIT_ABANDONED, got %#x\n", ret);
1245 /* semaphore is not abandoned on thread termination */
1246 ret = WaitForSingleObject(semaphore, 0);
1247 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1249 if (expected_code == STILL_ACTIVE)
1251 ret = WaitForSingleObject(attached_thread[0], 0);
1252 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1253 ret = WaitForSingleObject(attached_thread[1], 0);
1254 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1256 else
1258 ret = WaitForSingleObject(attached_thread[0], 0);
1259 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1260 ret = WaitForSingleObject(attached_thread[1], 0);
1261 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1264 /* win7 doesn't allow to create a thread during process shutdown,
1265 * earlier Windows versions allow it.
1267 noop_thread_started = 0;
1268 SetLastError(0xdeadbeef);
1269 handle = CreateThread(NULL, 0, noop_thread_proc, &noop_thread_started, 0, &ret);
1270 if (param)
1272 ok(!handle || broken(handle != 0) /* before win7 */, "CreateThread should fail\n");
1273 if (!handle)
1274 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1275 else
1277 ret = WaitForSingleObject(handle, 1000);
1278 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1279 CloseHandle(handle);
1282 else
1284 ok(handle != 0, "CreateThread error %d\n", GetLastError());
1285 ret = WaitForSingleObject(handle, 1000);
1286 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1287 ok(!noop_thread_started || broken(noop_thread_started) /* XP64 */, "thread shouldn't start yet\n");
1288 CloseHandle(handle);
1291 SetLastError(0xdeadbeef);
1292 process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
1293 ok(process != NULL, "OpenProcess error %d\n", GetLastError());
1295 noop_thread_started = 0;
1296 SetLastError(0xdeadbeef);
1297 handle = CreateRemoteThread(process, NULL, 0, noop_thread_proc, &noop_thread_started, 0, &ret);
1298 if (param)
1300 ok(!handle || broken(handle != 0) /* before win7 */, "CreateRemoteThread should fail\n");
1301 if (!handle)
1302 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1303 else
1305 ret = WaitForSingleObject(handle, 1000);
1306 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1307 CloseHandle(handle);
1310 else
1312 ok(handle != 0, "CreateRemoteThread error %d\n", GetLastError());
1313 ret = WaitForSingleObject(handle, 1000);
1314 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1315 ok(!noop_thread_started || broken(noop_thread_started) /* XP64 */, "thread shouldn't start yet\n");
1316 CloseHandle(handle);
1319 SetLastError(0xdeadbeef);
1320 handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, NULL);
1321 ok(handle != 0, "CreateFileMapping error %d\n", GetLastError());
1323 offset.u.LowPart = 0;
1324 offset.u.HighPart = 0;
1325 addr = NULL;
1326 size = 0;
1327 ret = pNtMapViewOfSection(handle, process, &addr, 0, 0, &offset,
1328 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
1329 ok(ret == STATUS_SUCCESS, "NtMapViewOfSection error %#x\n", ret);
1330 ret = pNtUnmapViewOfSection(process, addr);
1331 ok(ret == STATUS_SUCCESS, "NtUnmapViewOfSection error %#x\n", ret);
1333 CloseHandle(handle);
1334 CloseHandle(process);
1336 handle = GetModuleHandle("winver.exe");
1337 ok(!handle, "winver.exe shouldn't be loaded yet\n");
1338 SetLastError(0xdeadbeef);
1339 handle = LoadLibrary("winver.exe");
1340 ok(handle != 0, "LoadLibrary error %d\n", GetLastError());
1341 SetLastError(0xdeadbeef);
1342 ret = FreeLibrary(handle);
1343 ok(ret, "FreeLibrary error %d\n", GetLastError());
1344 handle = GetModuleHandle("winver.exe");
1345 if (param)
1346 ok(handle != 0, "winver.exe should not be unloaded\n");
1347 else
1348 todo_wine
1349 ok(!handle || broken(handle != 0) /* before win7 */, "winver.exe should be unloaded\n");
1351 SetLastError(0xdeadbeef);
1352 ret = WaitForDebugEvent(&de, 0);
1353 ok(!ret, "WaitForDebugEvent should fail\n");
1354 todo_wine
1355 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1357 SetLastError(0xdeadbeef);
1358 ret = DebugActiveProcess(GetCurrentProcessId());
1359 ok(!ret, "DebugActiveProcess should fail\n");
1360 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1362 SetLastError(0xdeadbeef);
1363 ret = WaitForDebugEvent(&de, 0);
1364 ok(!ret, "WaitForDebugEvent should fail\n");
1365 ok(GetLastError() == ERROR_SEM_TIMEOUT, "expected ERROR_SEM_TIMEOUT, got %d\n", GetLastError());
1367 if (test_dll_phase == 2)
1369 trace("dll: call ExitProcess()\n");
1370 *child_failures = winetest_get_failures();
1371 ExitProcess(197);
1373 trace("dll: %p, DLL_PROCESS_DETACH, %p => DONE\n", hinst, param);
1374 break;
1376 case DLL_THREAD_ATTACH:
1377 trace("dll: %p, DLL_THREAD_ATTACH, %p\n", hinst, param);
1379 ret = pRtlDllShutdownInProgress();
1380 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1382 if (attached_thread_count < MAX_COUNT)
1384 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &attached_thread[attached_thread_count],
1385 0, TRUE, DUPLICATE_SAME_ACCESS);
1386 attached_thread_count++;
1388 break;
1389 case DLL_THREAD_DETACH:
1390 trace("dll: %p, DLL_THREAD_DETACH, %p\n", hinst, param);
1392 ret = pRtlDllShutdownInProgress();
1393 /* win7 doesn't allow to create a thread during process shutdown,
1394 * earlier Windows versions allow it, and DLL_THREAD_DETACH is
1395 * sent on thread exit, but DLL_THREAD_ATTACH is never received.
1397 if (noop_thread_started)
1398 ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
1399 else
1400 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1402 break;
1403 default:
1404 trace("dll: %p, %d, %p\n", hinst, reason, param);
1405 break;
1408 *child_failures = winetest_get_failures();
1410 return TRUE;
1413 static void child_process(const char *dll_name, DWORD target_offset)
1415 void *target;
1416 DWORD ret, dummy, i, code, expected_code;
1417 HANDLE file, thread, process;
1418 HMODULE hmod;
1419 struct PROCESS_BASIC_INFORMATION_PRIVATE pbi;
1420 DWORD_PTR affinity;
1422 trace("phase %d: writing %p at %#x\n", test_dll_phase, dll_entry_point, target_offset);
1424 SetLastError(0xdeadbeef);
1425 mutex = CreateMutex(NULL, FALSE, NULL);
1426 ok(mutex != 0, "CreateMutex error %d\n", GetLastError());
1428 SetLastError(0xdeadbeef);
1429 semaphore = CreateSemaphore(NULL, 1, 1, NULL);
1430 ok(semaphore != 0, "CreateSemaphore error %d\n", GetLastError());
1432 SetLastError(0xdeadbeef);
1433 event = CreateEvent(NULL, TRUE, FALSE, NULL);
1434 ok(event != 0, "CreateEvent error %d\n", GetLastError());
1436 SetLastError(0xdeadbeef);
1437 loader_lock_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1438 ok(loader_lock_event != 0, "CreateEvent error %d\n", GetLastError());
1440 SetLastError(0xdeadbeef);
1441 peb_lock_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1442 ok(peb_lock_event != 0, "CreateEvent error %d\n", GetLastError());
1444 SetLastError(0xdeadbeef);
1445 heap_lock_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1446 ok(heap_lock_event != 0, "CreateEvent error %d\n", GetLastError());
1448 SetLastError(0xdeadbeef);
1449 ack_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1450 ok(ack_event != 0, "CreateEvent error %d\n", GetLastError());
1452 file = CreateFile(dll_name, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1453 if (file == INVALID_HANDLE_VALUE)
1455 ok(0, "could not open %s\n", dll_name);
1456 return;
1458 SetFilePointer(file, target_offset, NULL, FILE_BEGIN);
1459 SetLastError(0xdeadbeef);
1460 target = dll_entry_point;
1461 ret = WriteFile(file, &target, sizeof(target), &dummy, NULL);
1462 ok(ret, "WriteFile error %d\n", GetLastError());
1463 CloseHandle(file);
1465 SetLastError(0xdeadbeef);
1466 hmod = LoadLibrary(dll_name);
1467 ok(hmod != 0, "LoadLibrary error %d\n", GetLastError());
1469 SetLastError(0xdeadbeef);
1470 stop_event = CreateEvent(NULL, TRUE, FALSE, NULL);
1471 ok(stop_event != 0, "CreateEvent error %d\n", GetLastError());
1473 SetLastError(0xdeadbeef);
1474 thread = CreateThread(NULL, 0, mutex_thread_proc, event, 0, &dummy);
1475 ok(thread != 0, "CreateThread error %d\n", GetLastError());
1476 WaitForSingleObject(event, 3000);
1477 CloseHandle(thread);
1479 ResetEvent(event);
1481 SetLastError(0xdeadbeef);
1482 thread = CreateThread(NULL, 0, semaphore_thread_proc, event, 0, &dummy);
1483 ok(thread != 0, "CreateThread error %d\n", GetLastError());
1484 WaitForSingleObject(event, 3000);
1485 CloseHandle(thread);
1487 ResetEvent(event);
1488 Sleep(100);
1490 ok(attached_thread_count == 2, "attached thread count should be 2\n");
1491 for (i = 0; i < attached_thread_count; i++)
1493 ret = GetExitCodeThread(attached_thread[i], &code);
1494 trace("child: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
1495 ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
1496 ok(code == STILL_ACTIVE, "expected thread exit code STILL_ACTIVE, got %u\n", code);
1499 ret = WaitForSingleObject(attached_thread[0], 0);
1500 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1501 ret = WaitForSingleObject(attached_thread[1], 0);
1502 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1504 ret = WaitForSingleObject(event, 0);
1505 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1506 ret = WaitForSingleObject(mutex, 0);
1507 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1508 ret = WaitForSingleObject(semaphore, 0);
1509 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1511 ret = pRtlDllShutdownInProgress();
1512 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1514 SetLastError(0xdeadbeef);
1515 process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
1516 ok(process != NULL, "OpenProcess error %d\n", GetLastError());
1518 SetLastError(0xdeadbeef);
1519 ret = TerminateProcess(0, 195);
1520 ok(!ret, "TerminateProcess(0) should fail\n");
1521 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1523 Sleep(100);
1525 affinity = 1;
1526 ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
1527 ok(!ret, "NtSetInformationProcess error %#x\n", ret);
1529 switch (test_dll_phase)
1531 case 0:
1532 ret = pRtlDllShutdownInProgress();
1533 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1535 trace("call NtTerminateProcess(0, 195)\n");
1536 ret = pNtTerminateProcess(0, 195);
1537 ok(!ret, "NtTerminateProcess error %#x\n", ret);
1539 memset(&pbi, 0, sizeof(pbi));
1540 ret = pNtQueryInformationProcess(process, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
1541 ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
1542 ok(pbi.ExitStatus == STILL_ACTIVE, "expected STILL_ACTIVE, got %lu\n", pbi.ExitStatus);
1543 affinity = 1;
1544 ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
1545 ok(!ret, "NtSetInformationProcess error %#x\n", ret);
1547 ret = pRtlDllShutdownInProgress();
1548 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1550 hmod = GetModuleHandle(dll_name);
1551 ok(hmod != 0, "DLL should not be unloaded\n");
1553 SetLastError(0xdeadbeef);
1554 thread = CreateThread(NULL, 0, noop_thread_proc, &dummy, 0, &ret);
1555 ok(!thread || broken(thread != 0) /* before win7 */, "CreateThread should fail\n");
1556 if (!thread)
1557 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1558 else
1560 ret = WaitForSingleObject(thread, 1000);
1561 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1562 CloseHandle(thread);
1565 trace("call LdrShutdownProcess()\n");
1566 pLdrShutdownProcess();
1568 ret = pRtlDllShutdownInProgress();
1569 ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
1571 hmod = GetModuleHandle(dll_name);
1572 ok(hmod != 0, "DLL should not be unloaded\n");
1574 memset(&pbi, 0, sizeof(pbi));
1575 ret = pNtQueryInformationProcess(process, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
1576 ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
1577 ok(pbi.ExitStatus == STILL_ACTIVE, "expected STILL_ACTIVE, got %lu\n", pbi.ExitStatus);
1578 affinity = 1;
1579 ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
1580 ok(!ret, "NtSetInformationProcess error %#x\n", ret);
1581 break;
1583 case 1: /* normal ExitProcess */
1584 ret = pRtlDllShutdownInProgress();
1585 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1586 break;
1588 case 2: /* ExitProcess will be called by the PROCESS_DETACH handler */
1589 ret = pRtlDllShutdownInProgress();
1590 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1592 trace("call FreeLibrary(%p)\n", hmod);
1593 SetLastError(0xdeadbeef);
1594 ret = FreeLibrary(hmod);
1595 ok(ret, "FreeLibrary error %d\n", GetLastError());
1596 hmod = GetModuleHandle(dll_name);
1597 ok(!hmod, "DLL should be unloaded\n");
1599 if (test_dll_phase == 2)
1600 ok(0, "FreeLibrary+ExitProcess should never return\n");
1602 ret = pRtlDllShutdownInProgress();
1603 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1605 break;
1607 case 3:
1608 trace("signalling thread exit\n");
1609 SetEvent(stop_event);
1610 CloseHandle(stop_event);
1611 break;
1613 case 4:
1614 trace("setting loader_lock_event\n");
1615 SetEvent(loader_lock_event);
1616 WaitForSingleObject(ack_event, 1000);
1617 ok(inside_loader_lock != 0, "inside_loader_lock is not set\n");
1619 /* calling NtTerminateProcess should not cause a deadlock */
1620 trace("call NtTerminateProcess(0, 198)\n");
1621 ret = pNtTerminateProcess(0, 198);
1622 ok(!ret, "NtTerminateProcess error %#x\n", ret);
1624 *child_failures = winetest_get_failures();
1626 /* Windows fails to release loader lock acquired from another thread,
1627 * so the LdrUnlockLoaderLock call fails here and ExitProcess deadlocks
1628 * later on, so NtTerminateProcess is used instead.
1630 trace("call NtTerminateProcess(GetCurrentProcess(), 198)\n");
1631 pNtTerminateProcess(GetCurrentProcess(), 198);
1632 ok(0, "NtTerminateProcess should not return\n");
1633 break;
1635 case 5:
1636 trace("setting peb_lock_event\n");
1637 SetEvent(peb_lock_event);
1638 WaitForSingleObject(ack_event, 1000);
1639 ok(inside_peb_lock != 0, "inside_peb_lock is not set\n");
1641 *child_failures = winetest_get_failures();
1643 /* calling ExitProcess should cause a deadlock */
1644 trace("call ExitProcess(198)\n");
1645 ExitProcess(198);
1646 ok(0, "ExitProcess should not return\n");
1647 break;
1649 case 6:
1650 trace("setting heap_lock_event\n");
1651 SetEvent(heap_lock_event);
1652 WaitForSingleObject(ack_event, 1000);
1653 ok(inside_heap_lock != 0, "inside_heap_lock is not set\n");
1655 *child_failures = winetest_get_failures();
1657 /* calling ExitProcess should cause a deadlock */
1658 trace("call ExitProcess(1)\n");
1659 ExitProcess(1);
1660 ok(0, "ExitProcess should not return\n");
1661 break;
1663 default:
1664 assert(0);
1665 break;
1668 if (test_dll_phase == 0) expected_code = 195;
1669 else if (test_dll_phase == 3) expected_code = 196;
1670 else if (test_dll_phase == 4) expected_code = 198;
1671 else expected_code = STILL_ACTIVE;
1673 if (expected_code == STILL_ACTIVE)
1675 ret = WaitForSingleObject(attached_thread[0], 100);
1676 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1677 ret = WaitForSingleObject(attached_thread[1], 100);
1678 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1680 else
1682 ret = WaitForSingleObject(attached_thread[0], 100);
1683 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1684 ret = WaitForSingleObject(attached_thread[1], 100);
1685 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1688 for (i = 0; i < attached_thread_count; i++)
1690 ret = GetExitCodeThread(attached_thread[i], &code);
1691 trace("child: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
1692 ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
1693 ok(code == expected_code, "expected thread exit code %u, got %u\n", expected_code, code);
1696 *child_failures = winetest_get_failures();
1698 trace("call ExitProcess(195)\n");
1699 ExitProcess(195);
1702 static void test_ExitProcess(void)
1704 #include "pshpack1.h"
1705 #ifdef __x86_64__
1706 static struct section_data
1708 BYTE mov_rax[2];
1709 void *target;
1710 BYTE jmp_rax[2];
1711 } section_data = { { 0x48,0xb8 }, dll_entry_point, { 0xff,0xe0 } };
1712 #else
1713 static struct section_data
1715 BYTE mov_eax;
1716 void *target;
1717 BYTE jmp_eax[2];
1718 } section_data = { 0xb8, dll_entry_point, { 0xff,0xe0 } };
1719 #endif
1720 #include "poppack.h"
1721 static const char filler[0x1000];
1722 DWORD dummy, file_align;
1723 HANDLE file, thread, process, hmap, hmap_dup;
1724 char temp_path[MAX_PATH], dll_name[MAX_PATH], cmdline[MAX_PATH * 2];
1725 DWORD ret, target_offset, old_prot;
1726 char **argv, buf[256];
1727 PROCESS_INFORMATION pi;
1728 STARTUPINFO si = { sizeof(si) };
1729 CONTEXT ctx;
1730 struct PROCESS_BASIC_INFORMATION_PRIVATE pbi;
1731 MEMORY_BASIC_INFORMATION mbi;
1732 DWORD_PTR affinity;
1733 void *addr;
1734 LARGE_INTEGER offset;
1735 SIZE_T size;
1737 #if !defined(__i386__) && !defined(__x86_64__)
1738 skip("x86 specific ExitProcess test\n");
1739 return;
1740 #endif
1742 if (!pRtlDllShutdownInProgress)
1744 win_skip("RtlDllShutdownInProgress is not available on this platform (XP+)\n");
1745 return;
1747 if (!pNtQueryInformationProcess || !pNtSetInformationProcess)
1749 win_skip("NtQueryInformationProcess/NtSetInformationProcess are not available on this platform\n");
1750 return;
1752 if (!pNtAllocateVirtualMemory || !pNtFreeVirtualMemory)
1754 win_skip("NtAllocateVirtualMemory/NtFreeVirtualMemory are not available on this platform\n");
1755 return;
1758 /* prevent displaying of the "Unable to load this DLL" message box */
1759 SetErrorMode(SEM_FAILCRITICALERRORS);
1761 GetTempPath(MAX_PATH, temp_path);
1762 GetTempFileName(temp_path, "ldr", 0, dll_name);
1764 /*trace("creating %s\n", dll_name);*/
1765 file = CreateFile(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
1766 if (file == INVALID_HANDLE_VALUE)
1768 ok(0, "could not create %s\n", dll_name);
1769 return;
1772 SetLastError(0xdeadbeef);
1773 ret = WriteFile(file, &dos_header, sizeof(dos_header), &dummy, NULL);
1774 ok(ret, "WriteFile error %d\n", GetLastError());
1776 nt_header.FileHeader.NumberOfSections = 1;
1777 nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
1778 nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL | IMAGE_FILE_RELOCS_STRIPPED;
1780 nt_header.OptionalHeader.AddressOfEntryPoint = 0x1000;
1781 nt_header.OptionalHeader.SectionAlignment = 0x1000;
1782 nt_header.OptionalHeader.FileAlignment = 0x200;
1783 nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000;
1784 nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER);
1785 SetLastError(0xdeadbeef);
1786 ret = WriteFile(file, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
1787 ok(ret, "WriteFile error %d\n", GetLastError());
1788 SetLastError(0xdeadbeef);
1789 ret = WriteFile(file, &nt_header.OptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER), &dummy, NULL);
1790 ok(ret, "WriteFile error %d\n", GetLastError());
1792 section.SizeOfRawData = sizeof(section_data);
1793 section.PointerToRawData = nt_header.OptionalHeader.FileAlignment;
1794 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
1795 section.Misc.VirtualSize = sizeof(section_data);
1796 section.Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
1797 SetLastError(0xdeadbeef);
1798 ret = WriteFile(file, &section, sizeof(section), &dummy, NULL);
1799 ok(ret, "WriteFile error %d\n", GetLastError());
1801 file_align = nt_header.OptionalHeader.FileAlignment - nt_header.OptionalHeader.SizeOfHeaders;
1802 assert(file_align < sizeof(filler));
1803 SetLastError(0xdeadbeef);
1804 ret = WriteFile(file, filler, file_align, &dummy, NULL);
1805 ok(ret, "WriteFile error %d\n", GetLastError());
1807 target_offset = SetFilePointer(file, 0, NULL, FILE_CURRENT) + FIELD_OFFSET(struct section_data, target);
1809 /* section data */
1810 SetLastError(0xdeadbeef);
1811 ret = WriteFile(file, &section_data, sizeof(section_data), &dummy, NULL);
1812 ok(ret, "WriteFile error %d\n", GetLastError());
1814 CloseHandle(file);
1816 winetest_get_mainargs(&argv);
1818 /* phase 0 */
1819 *child_failures = -1;
1820 sprintf(cmdline, "\"%s\" loader %s %u 0", argv[0], dll_name, target_offset);
1821 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1822 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1823 ret = WaitForSingleObject(pi.hProcess, 10000);
1824 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1825 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
1826 GetExitCodeProcess(pi.hProcess, &ret);
1827 ok(ret == 195, "expected exit code 195, got %u\n", ret);
1828 if (*child_failures)
1830 trace("%d failures in child process\n", *child_failures);
1831 winetest_add_failures(*child_failures);
1833 CloseHandle(pi.hThread);
1834 CloseHandle(pi.hProcess);
1836 /* phase 1 */
1837 *child_failures = -1;
1838 sprintf(cmdline, "\"%s\" loader %s %u 1", argv[0], dll_name, target_offset);
1839 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1840 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1841 ret = WaitForSingleObject(pi.hProcess, 10000);
1842 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1843 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
1844 GetExitCodeProcess(pi.hProcess, &ret);
1845 ok(ret == 195, "expected exit code 195, got %u\n", ret);
1846 if (*child_failures)
1848 trace("%d failures in child process\n", *child_failures);
1849 winetest_add_failures(*child_failures);
1851 CloseHandle(pi.hThread);
1852 CloseHandle(pi.hProcess);
1854 /* phase 2 */
1855 *child_failures = -1;
1856 sprintf(cmdline, "\"%s\" loader %s %u 2", argv[0], dll_name, target_offset);
1857 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1858 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1859 ret = WaitForSingleObject(pi.hProcess, 10000);
1860 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1861 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
1862 GetExitCodeProcess(pi.hProcess, &ret);
1863 ok(ret == 197, "expected exit code 197, got %u\n", ret);
1864 if (*child_failures)
1866 trace("%d failures in child process\n", *child_failures);
1867 winetest_add_failures(*child_failures);
1869 CloseHandle(pi.hThread);
1870 CloseHandle(pi.hProcess);
1872 /* phase 3 */
1873 *child_failures = -1;
1874 sprintf(cmdline, "\"%s\" loader %s %u 3", argv[0], dll_name, target_offset);
1875 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1876 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1877 ret = WaitForSingleObject(pi.hProcess, 10000);
1878 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1879 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
1880 GetExitCodeProcess(pi.hProcess, &ret);
1881 ok(ret == 195, "expected exit code 195, got %u\n", ret);
1882 if (*child_failures)
1884 trace("%d failures in child process\n", *child_failures);
1885 winetest_add_failures(*child_failures);
1887 CloseHandle(pi.hThread);
1888 CloseHandle(pi.hProcess);
1890 /* phase 4 */
1891 if (pLdrLockLoaderLock && pLdrUnlockLoaderLock)
1893 *child_failures = -1;
1894 sprintf(cmdline, "\"%s\" loader %s %u 4", argv[0], dll_name, target_offset);
1895 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1896 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1897 ret = WaitForSingleObject(pi.hProcess, 10000);
1898 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1899 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
1900 GetExitCodeProcess(pi.hProcess, &ret);
1901 ok(ret == 198, "expected exit code 198, got %u\n", ret);
1902 if (*child_failures)
1904 trace("%d failures in child process\n", *child_failures);
1905 winetest_add_failures(*child_failures);
1907 CloseHandle(pi.hThread);
1908 CloseHandle(pi.hProcess);
1910 else
1911 win_skip("LdrLockLoaderLock/LdrUnlockLoaderLock are not available on this platform\n");
1913 /* phase 5 */
1914 if (pRtlAcquirePebLock && pRtlReleasePebLock)
1916 *child_failures = -1;
1917 sprintf(cmdline, "\"%s\" loader %s %u 5", argv[0], dll_name, target_offset);
1918 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1919 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1920 ret = WaitForSingleObject(pi.hProcess, 5000);
1921 ok(ret == WAIT_TIMEOUT, "child process should fail to terminate\n");
1922 if (ret != WAIT_OBJECT_0)
1924 trace("terminating child process\n");
1925 TerminateProcess(pi.hProcess, 199);
1927 ret = WaitForSingleObject(pi.hProcess, 1000);
1928 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1929 GetExitCodeProcess(pi.hProcess, &ret);
1930 ok(ret == 199, "expected exit code 199, got %u\n", ret);
1931 if (*child_failures)
1933 trace("%d failures in child process\n", *child_failures);
1934 winetest_add_failures(*child_failures);
1936 CloseHandle(pi.hThread);
1937 CloseHandle(pi.hProcess);
1939 else
1940 win_skip("RtlAcquirePebLock/RtlReleasePebLock are not available on this platform\n");
1942 /* phase 6 */
1943 *child_failures = -1;
1944 sprintf(cmdline, "\"%s\" loader %s %u 6", argv[0], dll_name, target_offset);
1945 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1946 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1947 ret = WaitForSingleObject(pi.hProcess, 5000);
1948 ok(ret == WAIT_TIMEOUT || broken(ret == WAIT_OBJECT_0) /* XP */, "child process should fail to terminate\n");
1949 if (ret != WAIT_OBJECT_0)
1951 trace("terminating child process\n");
1952 TerminateProcess(pi.hProcess, 201);
1954 ret = WaitForSingleObject(pi.hProcess, 1000);
1955 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1956 GetExitCodeProcess(pi.hProcess, &ret);
1957 ok(ret == 201 || broken(ret == 1) /* XP */, "expected exit code 201, got %u\n", ret);
1958 if (*child_failures)
1960 trace("%d failures in child process\n", *child_failures);
1961 winetest_add_failures(*child_failures);
1963 CloseHandle(pi.hThread);
1964 CloseHandle(pi.hProcess);
1966 /* test remote process termination */
1967 SetLastError(0xdeadbeef);
1968 ret = CreateProcess(argv[0], NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
1969 ok(ret, "CreateProcess(%s) error %d\n", argv[0], GetLastError());
1971 SetLastError(0xdeadbeef);
1972 addr = VirtualAllocEx(pi.hProcess, NULL, 4096, MEM_COMMIT, PAGE_READWRITE);
1973 ok(addr != NULL, "VirtualAllocEx error %d\n", GetLastError());
1974 SetLastError(0xdeadbeef);
1975 ret = VirtualProtectEx(pi.hProcess, addr, 4096, PAGE_READONLY, &old_prot);
1976 ok(ret, "VirtualProtectEx error %d\n", GetLastError());
1977 ok(old_prot == PAGE_READWRITE, "expected PAGE_READWRITE, got %#x\n", old_prot);
1978 SetLastError(0xdeadbeef);
1979 size = VirtualQueryEx(pi.hProcess, NULL, &mbi, sizeof(mbi));
1980 ok(size == sizeof(mbi), "VirtualQueryEx error %d\n", GetLastError());
1982 SetLastError(0xdeadbeef);
1983 ret = ReadProcessMemory(pi.hProcess, addr, buf, 4, &size);
1984 ok(ret, "ReadProcessMemory error %d\n", GetLastError());
1985 ok(size == 4, "expected 4, got %lu\n", size);
1987 SetLastError(0xdeadbeef);
1988 hmap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, NULL);
1989 ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
1991 SetLastError(0xdeadbeef);
1992 ret = DuplicateHandle(GetCurrentProcess(), hmap, pi.hProcess, &hmap_dup,
1993 0, FALSE, DUPLICATE_SAME_ACCESS);
1994 ok(ret, "DuplicateHandle error %d\n", GetLastError());
1996 offset.u.LowPart = 0;
1997 offset.u.HighPart = 0;
1998 addr = NULL;
1999 size = 0;
2000 ret = pNtMapViewOfSection(hmap, pi.hProcess, &addr, 0, 0, &offset,
2001 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
2002 ok(!ret, "NtMapViewOfSection error %#x\n", ret);
2003 ret = pNtUnmapViewOfSection(pi.hProcess, addr);
2004 ok(!ret, "NtUnmapViewOfSection error %#x\n", ret);
2006 SetLastError(0xdeadbeef);
2007 thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void *)0xdeadbeef, NULL, CREATE_SUSPENDED, &ret);
2008 ok(thread != 0, "CreateRemoteThread error %d\n", GetLastError());
2009 SetLastError(0xdeadbeef);
2010 ctx.ContextFlags = CONTEXT_INTEGER;
2011 ret = GetThreadContext(thread, &ctx);
2012 ok(ret, "GetThreadContext error %d\n", GetLastError());
2013 SetLastError(0xdeadbeef);
2014 ctx.ContextFlags = CONTEXT_INTEGER;
2015 ret = SetThreadContext(thread, &ctx);
2016 ok(ret, "SetThreadContext error %d\n", GetLastError());
2017 SetLastError(0xdeadbeef);
2018 ret = SetThreadPriority(thread, 0);
2019 ok(ret, "SetThreadPriority error %d\n", GetLastError());
2021 SetLastError(0xdeadbeef);
2022 ret = TerminateThread(thread, 199);
2023 ok(ret, "TerminateThread error %d\n", GetLastError());
2024 /* Calling GetExitCodeThread() without waiting for thread termination
2025 * leads to different results due to a race condition.
2027 ret = WaitForSingleObject(thread, 1000);
2028 ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
2029 GetExitCodeThread(thread, &ret);
2030 ok(ret == 199, "expected exit code 199, got %u\n", ret);
2032 SetLastError(0xdeadbeef);
2033 ret = TerminateProcess(pi.hProcess, 198);
2034 ok(ret, "TerminateProcess error %d\n", GetLastError());
2035 /* Checking process state without waiting for process termination
2036 * leads to different results due to a race condition.
2038 ret = WaitForSingleObject(pi.hProcess, 1000);
2039 ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
2041 SetLastError(0xdeadbeef);
2042 process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pi.dwProcessId);
2043 ok(process != NULL, "OpenProcess error %d\n", GetLastError());
2044 CloseHandle(process);
2046 memset(&pbi, 0, sizeof(pbi));
2047 ret = pNtQueryInformationProcess(pi.hProcess, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
2048 ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
2049 ok(pbi.ExitStatus == 198, "expected 198, got %lu\n", pbi.ExitStatus);
2050 affinity = 1;
2051 ret = pNtSetInformationProcess(pi.hProcess, ProcessAffinityMask, &affinity, sizeof(affinity));
2052 ok(ret == STATUS_PROCESS_IS_TERMINATING, "expected STATUS_PROCESS_IS_TERMINATING, got %#x\n", ret);
2054 SetLastError(0xdeadbeef);
2055 ctx.ContextFlags = CONTEXT_INTEGER;
2056 ret = GetThreadContext(thread, &ctx);
2057 ok(!ret || broken(ret) /* XP 64-bit */, "GetThreadContext should fail\n");
2058 if (!ret)
2059 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
2060 GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
2061 GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
2062 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2063 SetLastError(0xdeadbeef);
2064 ctx.ContextFlags = CONTEXT_INTEGER;
2065 ret = SetThreadContext(thread, &ctx);
2066 ok(!ret || broken(ret) /* XP 64-bit */, "SetThreadContext should fail\n");
2067 if (!ret)
2068 ok(GetLastError() == ERROR_ACCESS_DENIED ||
2069 GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
2070 GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
2071 "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2072 SetLastError(0xdeadbeef);
2073 ret = SetThreadPriority(thread, 0);
2074 ok(ret, "SetThreadPriority error %d\n", GetLastError());
2075 CloseHandle(thread);
2077 SetLastError(0xdeadbeef);
2078 ctx.ContextFlags = CONTEXT_INTEGER;
2079 ret = GetThreadContext(pi.hThread, &ctx);
2080 ok(!ret || broken(ret) /* XP 64-bit */, "GetThreadContext should fail\n");
2081 if (!ret)
2082 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
2083 GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
2084 GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
2085 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2086 SetLastError(0xdeadbeef);
2087 ctx.ContextFlags = CONTEXT_INTEGER;
2088 ret = SetThreadContext(pi.hThread, &ctx);
2089 ok(!ret || broken(ret) /* XP 64-bit */, "SetThreadContext should fail\n");
2090 if (!ret)
2091 ok(GetLastError() == ERROR_ACCESS_DENIED ||
2092 GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
2093 GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
2094 "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2095 SetLastError(0xdeadbeef);
2096 ret = VirtualProtectEx(pi.hProcess, addr, 4096, PAGE_READWRITE, &old_prot);
2097 ok(!ret, "VirtualProtectEx should fail\n");
2098 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2099 SetLastError(0xdeadbeef);
2100 size = 0;
2101 ret = ReadProcessMemory(pi.hProcess, addr, buf, 4, &size);
2102 ok(!ret, "ReadProcessMemory should fail\n");
2103 ok(GetLastError() == ERROR_PARTIAL_COPY || GetLastError() == ERROR_ACCESS_DENIED,
2104 "expected ERROR_PARTIAL_COPY, got %d\n", GetLastError());
2105 ok(!size, "expected 0, got %lu\n", size);
2106 SetLastError(0xdeadbeef);
2107 ret = VirtualFreeEx(pi.hProcess, addr, 0, MEM_RELEASE);
2108 ok(!ret, "VirtualFreeEx should fail\n");
2109 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2110 SetLastError(0xdeadbeef);
2111 addr = VirtualAllocEx(pi.hProcess, NULL, 4096, MEM_COMMIT, PAGE_READWRITE);
2112 ok(!addr, "VirtualAllocEx should fail\n");
2113 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2114 SetLastError(0xdeadbeef);
2115 size = VirtualQueryEx(pi.hProcess, NULL, &mbi, sizeof(mbi));
2116 ok(!size, "VirtualQueryEx should fail\n");
2117 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2119 /* CloseHandle() call below leads to premature process termination
2120 * under some Windows versions.
2122 if (0)
2124 SetLastError(0xdeadbeef);
2125 ret = CloseHandle(hmap_dup);
2126 ok(ret, "CloseHandle should not fail\n");
2129 SetLastError(0xdeadbeef);
2130 ret = DuplicateHandle(GetCurrentProcess(), hmap, pi.hProcess, &hmap_dup,
2131 0, FALSE, DUPLICATE_SAME_ACCESS);
2132 ok(!ret, "DuplicateHandle should fail\n");
2133 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2135 offset.u.LowPart = 0;
2136 offset.u.HighPart = 0;
2137 addr = NULL;
2138 size = 0;
2139 ret = pNtMapViewOfSection(hmap, pi.hProcess, &addr, 0, 0, &offset,
2140 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
2141 ok(ret == STATUS_PROCESS_IS_TERMINATING, "expected STATUS_PROCESS_IS_TERMINATING, got %#x\n", ret);
2143 SetLastError(0xdeadbeef);
2144 thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void *)0xdeadbeef, NULL, CREATE_SUSPENDED, &ret);
2145 ok(!thread, "CreateRemoteThread should fail\n");
2146 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2148 SetLastError(0xdeadbeef);
2149 ret = DebugActiveProcess(pi.dwProcessId);
2150 ok(!ret, "DebugActiveProcess should fail\n");
2151 ok(GetLastError() == ERROR_ACCESS_DENIED /* 64-bit */ || GetLastError() == ERROR_NOT_SUPPORTED /* 32-bit */,
2152 "ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2154 GetExitCodeProcess(pi.hProcess, &ret);
2155 ok(ret == 198 || broken(ret != 198) /* some 32-bit XP version in a VM returns random exit code */,
2156 "expected exit code 198, got %u\n", ret);
2157 CloseHandle(pi.hThread);
2158 CloseHandle(pi.hProcess);
2160 ret = DeleteFile(dll_name);
2161 ok(ret, "DeleteFile error %d\n", GetLastError());
2164 START_TEST(loader)
2166 int argc;
2167 char **argv;
2168 HANDLE mapping;
2170 pNtMapViewOfSection = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtMapViewOfSection");
2171 pNtUnmapViewOfSection = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection");
2172 pNtTerminateProcess = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtTerminateProcess");
2173 pNtQueryInformationProcess = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryInformationProcess");
2174 pNtSetInformationProcess = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtSetInformationProcess");
2175 pLdrShutdownProcess = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "LdrShutdownProcess");
2176 pRtlDllShutdownInProgress = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlDllShutdownInProgress");
2177 pNtAllocateVirtualMemory = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtAllocateVirtualMemory");
2178 pNtFreeVirtualMemory = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtFreeVirtualMemory");
2179 pLdrLockLoaderLock = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "LdrLockLoaderLock");
2180 pLdrUnlockLoaderLock = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "LdrUnlockLoaderLock");
2181 pRtlAcquirePebLock = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlAcquirePebLock");
2182 pRtlReleasePebLock = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlReleasePebLock");
2184 mapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "winetest_loader");
2185 ok(mapping != 0, "CreateFileMapping failed\n");
2186 child_failures = MapViewOfFile(mapping, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 4096);
2187 if (*child_failures == -1)
2189 is_child = 1;
2190 *child_failures = 0;
2192 else
2193 *child_failures = -1;
2195 argc = winetest_get_mainargs(&argv);
2196 if (argc > 4)
2198 test_dll_phase = atoi(argv[4]);
2199 child_process(argv[2], atol(argv[3]));
2200 return;
2203 test_Loader();
2204 test_ImportDescriptors();
2205 test_section_access();
2206 test_ExitProcess();