kernel32/tests: Increase some wait timeouts.
[wine.git] / dlls / kernel32 / tests / loader.c
blob3b72cc8cad096817dbd9abaf5a489be3fe534c27
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;
377 void *ptr;
379 ok( td[i].errors[0] == ERROR_SUCCESS, "%d: should have failed\n", i );
381 SetLastError(0xdeadbeef);
382 size = VirtualQuery(hlib, &info, sizeof(info));
383 ok(size == sizeof(info),
384 "%d: VirtualQuery error %d\n", i, GetLastError());
385 ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
386 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
387 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
388 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
389 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
390 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
391 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
392 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
393 else
394 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
395 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
397 SetLastError(0xdeadbeef);
398 ptr = VirtualAlloc(hlib, si.dwPageSize, MEM_COMMIT, info.Protect);
399 ok(!ptr, "%d: VirtualAlloc should fail\n", i);
400 /* FIXME: Remove once Wine is fixed */
401 if (info.Protect == PAGE_WRITECOPY || info.Protect == PAGE_EXECUTE_WRITECOPY)
402 todo_wine
403 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
404 else
405 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
407 SetLastError(0xdeadbeef);
408 size = VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info));
409 ok(size == sizeof(info),
410 "%d: VirtualQuery error %d\n", i, GetLastError());
411 if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize ||
412 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
414 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %p != expected %p\n",
415 i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
416 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
417 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
418 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
419 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
420 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
421 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
423 else
425 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
426 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
427 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
428 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
429 ok(info.RegionSize == ALIGN_SIZE(file_size, si.dwPageSize), "%d: got %lx != expected %x\n",
430 i, info.RegionSize, ALIGN_SIZE(file_size, si.dwPageSize));
431 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
432 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
433 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
436 /* header: check the zeroing of alignment */
437 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
439 const char *start;
441 start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
442 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
443 ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
446 if (nt_header.FileHeader.NumberOfSections)
448 SetLastError(0xdeadbeef);
449 size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
450 ok(size == sizeof(info),
451 "%d: VirtualQuery error %d\n", i, GetLastError());
452 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
454 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
455 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
456 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
457 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
459 else
461 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
462 ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize), "%d: got %lx != expected %x\n",
463 i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize));
464 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
466 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
467 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
468 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
469 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
471 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
472 ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
473 else
474 ok(!memcmp((const char *)hlib + section.PointerToRawData, section_data, section.SizeOfRawData), "wrong section data\n");
476 /* check the zeroing of alignment */
477 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
479 const char *start;
481 start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
482 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
483 ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
486 SetLastError(0xdeadbeef);
487 ptr = VirtualAlloc((char *)hlib + section.VirtualAddress, si.dwPageSize, MEM_COMMIT, info.Protect);
488 ok(!ptr, "%d: VirtualAlloc should fail\n", i);
489 /* FIXME: Remove once Wine is fixed */
490 if (info.Protect == PAGE_WRITECOPY || info.Protect == PAGE_EXECUTE_WRITECOPY)
491 todo_wine
492 ok(GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_ADDRESS,
493 "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
494 else
495 ok(GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_ADDRESS,
496 "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
499 SetLastError(0xdeadbeef);
500 hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
501 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
502 ok(hlib_as_data_file == hlib, "hlib_as_file and hlib are different\n");
504 SetLastError(0xdeadbeef);
505 ret = FreeLibrary(hlib);
506 ok(ret, "FreeLibrary error %d\n", GetLastError());
508 SetLastError(0xdeadbeef);
509 hlib = GetModuleHandle(dll_name);
510 ok(hlib != 0, "GetModuleHandle error %u\n", GetLastError());
512 SetLastError(0xdeadbeef);
513 ret = FreeLibrary(hlib_as_data_file);
514 ok(ret, "FreeLibrary error %d\n", GetLastError());
516 hlib = GetModuleHandle(dll_name);
517 ok(!hlib, "GetModuleHandle should fail\n");
519 SetLastError(0xdeadbeef);
520 hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
521 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
522 ok((ULONG_PTR)hlib_as_data_file & 1, "hlib_as_data_file is even\n");
524 hlib = GetModuleHandle(dll_name);
525 ok(!hlib, "GetModuleHandle should fail\n");
527 SetLastError(0xdeadbeef);
528 ret = FreeLibrary(hlib_as_data_file);
529 ok(ret, "FreeLibrary error %d\n", GetLastError());
531 else
533 BOOL error_match;
534 int error_index;
536 error_match = FALSE;
537 for (error_index = 0;
538 ! error_match && error_index < sizeof(td[i].errors) / sizeof(DWORD);
539 error_index++)
541 error_match = td[i].errors[error_index] == GetLastError();
543 ok(error_match, "%d: unexpected error %d\n", i, GetLastError());
546 SetLastError(0xdeadbeef);
547 ret = DeleteFile(dll_name);
548 ok(ret, "DeleteFile error %d\n", GetLastError());
552 /* Verify linking style of import descriptors */
553 static void test_ImportDescriptors(void)
555 HMODULE kernel32_module = NULL;
556 PIMAGE_DOS_HEADER d_header;
557 PIMAGE_NT_HEADERS nt_headers;
558 DWORD import_dir_size;
559 DWORD_PTR dir_offset;
560 PIMAGE_IMPORT_DESCRIPTOR import_chunk;
562 /* Load kernel32 module */
563 kernel32_module = GetModuleHandleA("kernel32.dll");
564 assert( kernel32_module != NULL );
566 /* Get PE header info from module image */
567 d_header = (PIMAGE_DOS_HEADER) kernel32_module;
568 nt_headers = (PIMAGE_NT_HEADERS) (((char*) d_header) +
569 d_header->e_lfanew);
571 /* Get size of import entry directory */
572 import_dir_size = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
573 if (!import_dir_size)
575 skip("Unable to continue testing due to missing import directory.\n");
576 return;
579 /* Get address of first import chunk */
580 dir_offset = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
581 import_chunk = RVAToAddr(dir_offset, kernel32_module);
582 ok(import_chunk != 0, "Invalid import_chunk: %p\n", import_chunk);
583 if (!import_chunk) return;
585 /* Iterate through import descriptors and verify set name,
586 * OriginalFirstThunk, and FirstThunk. Core Windows DLLs, such as
587 * kernel32.dll, don't use Borland-style linking, where the table of
588 * imported names is stored directly in FirstThunk and overwritten
589 * by the relocation, instead of being stored in OriginalFirstThunk.
590 * */
591 for (; import_chunk->FirstThunk; import_chunk++)
593 LPCSTR module_name = RVAToAddr(import_chunk->Name, kernel32_module);
594 PIMAGE_THUNK_DATA name_table = RVAToAddr(
595 U(*import_chunk).OriginalFirstThunk, kernel32_module);
596 PIMAGE_THUNK_DATA iat = RVAToAddr(
597 import_chunk->FirstThunk, kernel32_module);
598 ok(module_name != NULL, "Imported module name should not be NULL\n");
599 ok(name_table != NULL,
600 "Name table for imported module %s should not be NULL\n",
601 module_name);
602 ok(iat != NULL, "IAT for imported module %s should not be NULL\n",
603 module_name);
607 static void test_image_mapping(const char *dll_name, DWORD scn_page_access, BOOL is_dll)
609 HANDLE hfile, hmap;
610 NTSTATUS status;
611 LARGE_INTEGER offset;
612 SIZE_T size;
613 void *addr1, *addr2;
614 MEMORY_BASIC_INFORMATION info;
615 SYSTEM_INFO si;
617 if (!pNtMapViewOfSection) return;
619 GetSystemInfo(&si);
621 SetLastError(0xdeadbeef);
622 hfile = CreateFile(dll_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
623 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
625 SetLastError(0xdeadbeef);
626 hmap = CreateFileMapping(hfile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, 0);
627 ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
629 offset.u.LowPart = 0;
630 offset.u.HighPart = 0;
632 addr1 = NULL;
633 size = 0;
634 status = pNtMapViewOfSection(hmap, GetCurrentProcess(), &addr1, 0, 0, &offset,
635 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
636 ok(status == STATUS_SUCCESS, "NtMapViewOfSection error %x\n", status);
637 ok(addr1 != 0, "mapped address should be valid\n");
639 SetLastError(0xdeadbeef);
640 size = VirtualQuery((char *)addr1 + section.VirtualAddress, &info, sizeof(info));
641 ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
642 ok(info.BaseAddress == (char *)addr1 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr1 + section.VirtualAddress);
643 ok(info.RegionSize == si.dwPageSize, "got %#lx != expected %#x\n", info.RegionSize, si.dwPageSize);
644 ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
645 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
646 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
647 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
648 ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
650 addr2 = NULL;
651 size = 0;
652 status = pNtMapViewOfSection(hmap, GetCurrentProcess(), &addr2, 0, 0, &offset,
653 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
654 /* FIXME: remove once Wine is fixed */
655 if (status != STATUS_IMAGE_NOT_AT_BASE)
657 todo_wine {
658 ok(status == STATUS_IMAGE_NOT_AT_BASE, "expected STATUS_IMAGE_NOT_AT_BASE, got %x\n", status);
659 ok(addr2 != 0, "mapped address should be valid\n");
661 goto wine_is_broken;
663 ok(status == STATUS_IMAGE_NOT_AT_BASE, "expected STATUS_IMAGE_NOT_AT_BASE, got %x\n", status);
664 ok(addr2 != 0, "mapped address should be valid\n");
665 ok(addr2 != addr1, "mapped addresses should be different\n");
667 SetLastError(0xdeadbeef);
668 size = VirtualQuery((char *)addr2 + section.VirtualAddress, &info, sizeof(info));
669 ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
670 ok(info.BaseAddress == (char *)addr2 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr2 + section.VirtualAddress);
671 ok(info.RegionSize == si.dwPageSize, "got %#lx != expected %#x\n", info.RegionSize, si.dwPageSize);
672 ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
673 ok(info.AllocationBase == addr2, "%p != %p\n", info.AllocationBase, addr2);
674 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
675 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
676 ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
678 status = pNtUnmapViewOfSection(GetCurrentProcess(), addr2);
679 ok(status == STATUS_SUCCESS, "NtUnmapViewOfSection error %x\n", status);
681 addr2 = MapViewOfFile(hmap, 0, 0, 0, 0);
682 ok(addr2 != 0, "mapped address should be valid\n");
683 ok(addr2 != addr1, "mapped addresses should be different\n");
685 SetLastError(0xdeadbeef);
686 size = VirtualQuery((char *)addr2 + section.VirtualAddress, &info, sizeof(info));
687 ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
688 ok(info.BaseAddress == (char *)addr2 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr2 + section.VirtualAddress);
689 ok(info.RegionSize == si.dwPageSize, "got %#lx != expected %#x\n", info.RegionSize, si.dwPageSize);
690 ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
691 ok(info.AllocationBase == addr2, "%p != %p\n", info.AllocationBase, addr2);
692 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
693 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
694 ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
696 UnmapViewOfFile(addr2);
698 SetLastError(0xdeadbeef);
699 addr2 = LoadLibrary(dll_name);
700 if (is_dll)
702 ok(!addr2, "LoadLibrary should fail, is_dll %d\n", is_dll);
703 ok(GetLastError() == ERROR_INVALID_ADDRESS, "expected ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
705 else
707 BOOL ret;
708 ok(addr2 != 0, "LoadLibrary error %d, is_dll %d\n", GetLastError(), is_dll);
709 ok(addr2 != addr1, "mapped addresses should be different\n");
711 SetLastError(0xdeadbeef);
712 ret = FreeLibrary(addr2);
713 ok(ret, "FreeLibrary error %d\n", GetLastError());
716 wine_is_broken:
717 status = pNtUnmapViewOfSection(GetCurrentProcess(), addr1);
718 ok(status == STATUS_SUCCESS, "NtUnmapViewOfSection error %x\n", status);
720 CloseHandle(hmap);
721 CloseHandle(hfile);
724 static BOOL is_mem_writable(DWORD prot)
726 switch (prot & 0xff)
728 case PAGE_READWRITE:
729 case PAGE_WRITECOPY:
730 case PAGE_EXECUTE_READWRITE:
731 case PAGE_EXECUTE_WRITECOPY:
732 return TRUE;
734 default:
735 return FALSE;
739 static void test_VirtualProtect(void *base, void *section)
741 static const struct test_data
743 DWORD prot_set, prot_get;
744 } td[] =
746 { 0, 0 }, /* 0x00 */
747 { PAGE_NOACCESS, PAGE_NOACCESS }, /* 0x01 */
748 { PAGE_READONLY, PAGE_READONLY }, /* 0x02 */
749 { PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x03 */
750 { PAGE_READWRITE, PAGE_WRITECOPY }, /* 0x04 */
751 { PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x05 */
752 { PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x06 */
753 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x07 */
754 { PAGE_WRITECOPY, PAGE_WRITECOPY }, /* 0x08 */
755 { PAGE_WRITECOPY | PAGE_NOACCESS, 0 }, /* 0x09 */
756 { PAGE_WRITECOPY | PAGE_READONLY, 0 }, /* 0x0a */
757 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, 0 }, /* 0x0b */
758 { PAGE_WRITECOPY | PAGE_READWRITE, 0 }, /* 0x0c */
759 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x0d */
760 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x0e */
761 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x0f */
763 { PAGE_EXECUTE, PAGE_EXECUTE }, /* 0x10 */
764 { PAGE_EXECUTE_READ, PAGE_EXECUTE_READ }, /* 0x20 */
765 { PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x30 */
766 { PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY }, /* 0x40 */
767 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0x50 */
768 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0x60 */
769 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x70 */
770 { PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_WRITECOPY }, /* 0x80 */
771 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, 0 }, /* 0x90 */
772 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, 0 }, /* 0xa0 */
773 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0xb0 */
774 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, 0 }, /* 0xc0 */
775 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0xd0 */
776 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0xe0 */
777 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 } /* 0xf0 */
779 DWORD ret, orig_prot, old_prot, rw_prot, exec_prot, i, j;
780 MEMORY_BASIC_INFORMATION info;
781 SYSTEM_INFO si;
783 GetSystemInfo(&si);
785 SetLastError(0xdeadbeef);
786 ret = VirtualProtect(section, si.dwPageSize, PAGE_NOACCESS, &old_prot);
787 ok(ret, "VirtualProtect error %d\n", GetLastError());
789 orig_prot = old_prot;
791 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
793 SetLastError(0xdeadbeef);
794 ret = VirtualQuery(section, &info, sizeof(info));
795 ok(ret, "VirtualQuery failed %d\n", GetLastError());
796 ok(info.BaseAddress == section, "%d: got %p != expected %p\n", i, info.BaseAddress, section);
797 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
798 ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
799 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
800 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
801 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
802 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
804 old_prot = 0xdeadbeef;
805 SetLastError(0xdeadbeef);
806 ret = VirtualProtect(section, si.dwPageSize, td[i].prot_set, &old_prot);
807 if (td[i].prot_get)
809 ok(ret, "%d: VirtualProtect error %d, requested prot %#x\n", i, GetLastError(), td[i].prot_set);
810 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
812 SetLastError(0xdeadbeef);
813 ret = VirtualQuery(section, &info, sizeof(info));
814 ok(ret, "VirtualQuery failed %d\n", GetLastError());
815 ok(info.BaseAddress == section, "%d: got %p != expected %p\n", i, info.BaseAddress, section);
816 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
817 ok(info.Protect == td[i].prot_get, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_get);
818 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
819 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
820 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
821 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
823 else
825 ok(!ret, "%d: VirtualProtect should fail\n", i);
826 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
829 old_prot = 0xdeadbeef;
830 SetLastError(0xdeadbeef);
831 ret = VirtualProtect(section, si.dwPageSize, PAGE_NOACCESS, &old_prot);
832 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
833 if (td[i].prot_get)
834 ok(old_prot == td[i].prot_get, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_get);
835 else
836 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
839 exec_prot = 0;
841 for (i = 0; i <= 4; i++)
843 rw_prot = 0;
845 for (j = 0; j <= 4; j++)
847 DWORD prot = exec_prot | rw_prot;
849 SetLastError(0xdeadbeef);
850 ret = VirtualProtect(section, si.dwPageSize, prot, &old_prot);
851 if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
853 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
854 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
856 else
857 ok(ret, "VirtualProtect(%02x) error %d\n", prot, GetLastError());
859 rw_prot = 1 << j;
862 exec_prot = 1 << (i + 4);
865 SetLastError(0xdeadbeef);
866 ret = VirtualProtect(section, si.dwPageSize, orig_prot, &old_prot);
867 ok(ret, "VirtualProtect error %d\n", GetLastError());
870 static void test_section_access(void)
872 static const struct test_data
874 DWORD scn_file_access, scn_page_access, scn_page_access_after_write;
875 } td[] =
877 { 0, PAGE_NOACCESS, 0 },
878 { IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
879 { IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
880 { IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
881 { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
882 { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ },
883 { IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
884 { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
886 { IMAGE_SCN_CNT_INITIALIZED_DATA, PAGE_NOACCESS, 0 },
887 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
888 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
889 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
890 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
891 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ, 0 },
892 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
893 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
895 { IMAGE_SCN_CNT_UNINITIALIZED_DATA, PAGE_NOACCESS, 0 },
896 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
897 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
898 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
899 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
900 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ, 0 },
901 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
902 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE }
904 static const char filler[0x1000];
905 static const char section_data[0x10] = "section data";
906 char buf[256];
907 int i;
908 DWORD dummy, file_align;
909 HANDLE hfile;
910 HMODULE hlib;
911 SYSTEM_INFO si;
912 char temp_path[MAX_PATH];
913 char dll_name[MAX_PATH];
914 SIZE_T size;
915 MEMORY_BASIC_INFORMATION info;
916 STARTUPINFO sti;
917 PROCESS_INFORMATION pi;
918 DWORD ret;
920 GetSystemInfo(&si);
922 /* prevent displaying of the "Unable to load this DLL" message box */
923 SetErrorMode(SEM_FAILCRITICALERRORS);
925 GetTempPath(MAX_PATH, temp_path);
927 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
929 GetTempFileName(temp_path, "ldr", 0, dll_name);
931 /*trace("creating %s\n", dll_name);*/
932 hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
933 if (hfile == INVALID_HANDLE_VALUE)
935 ok(0, "could not create %s\n", dll_name);
936 return;
939 SetLastError(0xdeadbeef);
940 ret = WriteFile(hfile, &dos_header, sizeof(dos_header), &dummy, NULL);
941 ok(ret, "WriteFile error %d\n", GetLastError());
943 nt_header.FileHeader.NumberOfSections = 1;
944 nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
945 nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL | IMAGE_FILE_RELOCS_STRIPPED;
947 nt_header.OptionalHeader.SectionAlignment = si.dwPageSize;
948 nt_header.OptionalHeader.FileAlignment = 0x200;
949 nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + si.dwPageSize;
950 nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER);
951 SetLastError(0xdeadbeef);
952 ret = WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
953 ok(ret, "WriteFile error %d\n", GetLastError());
954 SetLastError(0xdeadbeef);
955 ret = WriteFile(hfile, &nt_header.OptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER), &dummy, NULL);
956 ok(ret, "WriteFile error %d\n", GetLastError());
958 section.SizeOfRawData = sizeof(section_data);
959 section.PointerToRawData = nt_header.OptionalHeader.FileAlignment;
960 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
961 section.Misc.VirtualSize = section.SizeOfRawData;
962 section.Characteristics = td[i].scn_file_access;
963 SetLastError(0xdeadbeef);
964 ret = WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
965 ok(ret, "WriteFile error %d\n", GetLastError());
967 file_align = nt_header.OptionalHeader.FileAlignment - nt_header.OptionalHeader.SizeOfHeaders;
968 assert(file_align < sizeof(filler));
969 SetLastError(0xdeadbeef);
970 ret = WriteFile(hfile, filler, file_align, &dummy, NULL);
971 ok(ret, "WriteFile error %d\n", GetLastError());
973 /* section data */
974 SetLastError(0xdeadbeef);
975 ret = WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL);
976 ok(ret, "WriteFile error %d\n", GetLastError());
978 CloseHandle(hfile);
980 SetLastError(0xdeadbeef);
981 hlib = LoadLibrary(dll_name);
982 ok(hlib != 0, "LoadLibrary error %d\n", GetLastError());
984 SetLastError(0xdeadbeef);
985 size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
986 ok(size == sizeof(info),
987 "%d: VirtualQuery error %d\n", i, GetLastError());
988 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
989 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
990 ok(info.Protect == td[i].scn_page_access, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].scn_page_access);
991 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
992 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
993 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
994 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
995 if (info.Protect != PAGE_NOACCESS)
996 ok(!memcmp((const char *)info.BaseAddress, section_data, section.SizeOfRawData), "wrong section data\n");
998 test_VirtualProtect(hlib, (char *)hlib + section.VirtualAddress);
1000 /* Windows changes the WRITECOPY to WRITE protection on an image section write (for a changed page only) */
1001 if (is_mem_writable(info.Protect))
1003 char *p = info.BaseAddress;
1004 *p = 0xfe;
1005 SetLastError(0xdeadbeef);
1006 size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
1007 ok(size == sizeof(info), "%d: VirtualQuery error %d\n", i, GetLastError());
1008 /* FIXME: remove the condition below once Wine is fixed */
1009 if (info.Protect == PAGE_WRITECOPY || info.Protect == PAGE_EXECUTE_WRITECOPY)
1010 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);
1011 else
1012 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);
1015 SetLastError(0xdeadbeef);
1016 ret = FreeLibrary(hlib);
1017 ok(ret, "FreeLibrary error %d\n", GetLastError());
1019 test_image_mapping(dll_name, td[i].scn_page_access, TRUE);
1021 /* reset IMAGE_FILE_DLL otherwise CreateProcess fails */
1022 nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_RELOCS_STRIPPED;
1023 SetLastError(0xdeadbeef);
1024 hfile = CreateFile(dll_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1025 /* LoadLibrary called on an already memory-mapped file in
1026 * test_image_mapping() above leads to a file handle leak
1027 * under nt4, and inability to overwrite and delete the file
1028 * due to sharing violation error. Ignore it and skip the test,
1029 * but leave a not deletable temporary file.
1031 ok(hfile != INVALID_HANDLE_VALUE || broken(hfile == INVALID_HANDLE_VALUE) /* nt4 */,
1032 "CreateFile error %d\n", GetLastError());
1033 if (hfile == INVALID_HANDLE_VALUE) goto nt4_is_broken;
1034 SetFilePointer(hfile, sizeof(dos_header), NULL, FILE_BEGIN);
1035 SetLastError(0xdeadbeef);
1036 ret = WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
1037 ok(ret, "WriteFile error %d\n", GetLastError());
1038 CloseHandle(hfile);
1040 memset(&sti, 0, sizeof(sti));
1041 sti.cb = sizeof(sti);
1042 SetLastError(0xdeadbeef);
1043 ret = CreateProcess(dll_name, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &sti, &pi);
1044 ok(ret, "CreateProcess() error %d\n", GetLastError());
1046 SetLastError(0xdeadbeef);
1047 size = VirtualQueryEx(pi.hProcess, (char *)hlib + section.VirtualAddress, &info, sizeof(info));
1048 ok(size == sizeof(info),
1049 "%d: VirtualQuery error %d\n", i, GetLastError());
1050 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
1051 ok(info.RegionSize == si.dwPageSize, "%d: got %#lx != expected %#x\n", i, info.RegionSize, si.dwPageSize);
1052 ok(info.Protect == td[i].scn_page_access, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].scn_page_access);
1053 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
1054 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
1055 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1056 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
1057 if (info.Protect != PAGE_NOACCESS)
1059 SetLastError(0xdeadbeef);
1060 ret = ReadProcessMemory(pi.hProcess, info.BaseAddress, buf, section.SizeOfRawData, NULL);
1061 ok(ret, "ReadProcessMemory() error %d\n", GetLastError());
1062 ok(!memcmp(buf, section_data, section.SizeOfRawData), "wrong section data\n");
1065 SetLastError(0xdeadbeef);
1066 ret = TerminateProcess(pi.hProcess, 0);
1067 ok(ret, "TerminateProcess() error %d\n", GetLastError());
1068 ret = WaitForSingleObject(pi.hProcess, 3000);
1069 ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
1071 CloseHandle(pi.hThread);
1072 CloseHandle(pi.hProcess);
1074 test_image_mapping(dll_name, td[i].scn_page_access, FALSE);
1076 nt4_is_broken:
1077 SetLastError(0xdeadbeef);
1078 ret = DeleteFile(dll_name);
1079 ok(ret || broken(!ret) /* nt4 */, "DeleteFile error %d\n", GetLastError());
1083 #define MAX_COUNT 10
1084 static HANDLE attached_thread[MAX_COUNT];
1085 static DWORD attached_thread_count;
1086 HANDLE stop_event, event, mutex, semaphore, loader_lock_event, peb_lock_event, heap_lock_event, ack_event;
1087 static int test_dll_phase, inside_loader_lock, inside_peb_lock, inside_heap_lock;
1089 static DWORD WINAPI mutex_thread_proc(void *param)
1091 HANDLE wait_list[4];
1092 DWORD ret;
1094 ret = WaitForSingleObject(mutex, 0);
1095 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1097 SetEvent(param);
1099 wait_list[0] = stop_event;
1100 wait_list[1] = loader_lock_event;
1101 wait_list[2] = peb_lock_event;
1102 wait_list[3] = heap_lock_event;
1104 trace("%04u: mutex_thread_proc: starting\n", GetCurrentThreadId());
1105 while (1)
1107 ret = WaitForMultipleObjects(sizeof(wait_list)/sizeof(wait_list[0]), wait_list, FALSE, 50);
1108 if (ret == WAIT_OBJECT_0) break;
1109 else if (ret == WAIT_OBJECT_0 + 1)
1111 ULONG loader_lock_magic;
1112 trace("%04u: mutex_thread_proc: Entering loader lock\n", GetCurrentThreadId());
1113 ret = pLdrLockLoaderLock(0, NULL, &loader_lock_magic);
1114 ok(!ret, "LdrLockLoaderLock error %#x\n", ret);
1115 inside_loader_lock++;
1116 SetEvent(ack_event);
1118 else if (ret == WAIT_OBJECT_0 + 2)
1120 trace("%04u: mutex_thread_proc: Entering PEB lock\n", GetCurrentThreadId());
1121 pRtlAcquirePebLock();
1122 inside_peb_lock++;
1123 SetEvent(ack_event);
1125 else if (ret == WAIT_OBJECT_0 + 3)
1127 trace("%04u: mutex_thread_proc: Entering heap lock\n", GetCurrentThreadId());
1128 HeapLock(GetProcessHeap());
1129 inside_heap_lock++;
1130 SetEvent(ack_event);
1134 trace("%04u: mutex_thread_proc: exiting\n", GetCurrentThreadId());
1135 return 196;
1138 static DWORD WINAPI semaphore_thread_proc(void *param)
1140 DWORD ret;
1142 ret = WaitForSingleObject(semaphore, 0);
1143 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1145 SetEvent(param);
1147 while (1)
1149 trace("%04u: semaphore_thread_proc: still alive\n", GetCurrentThreadId());
1150 if (WaitForSingleObject(stop_event, 50) != WAIT_TIMEOUT) break;
1153 trace("%04u: semaphore_thread_proc: exiting\n", GetCurrentThreadId());
1154 return 196;
1157 static DWORD WINAPI noop_thread_proc(void *param)
1159 if (param)
1161 LONG *noop_thread_started = param;
1162 InterlockedIncrement(noop_thread_started);
1165 trace("%04u: noop_thread_proc: exiting\n", GetCurrentThreadId());
1166 return 195;
1169 static BOOL WINAPI dll_entry_point(HINSTANCE hinst, DWORD reason, LPVOID param)
1171 static LONG noop_thread_started;
1172 DWORD ret;
1174 ok(!inside_loader_lock, "inside_loader_lock should not be set\n");
1175 ok(!inside_peb_lock, "inside_peb_lock should not be set\n");
1177 switch (reason)
1179 case DLL_PROCESS_ATTACH:
1180 trace("dll: %p, DLL_PROCESS_ATTACH, %p\n", hinst, param);
1182 ret = pRtlDllShutdownInProgress();
1183 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1185 break;
1186 case DLL_PROCESS_DETACH:
1188 DWORD code, expected_code, i;
1189 HANDLE handle, process;
1190 void *addr;
1191 SIZE_T size;
1192 LARGE_INTEGER offset;
1193 DEBUG_EVENT de;
1195 trace("dll: %p, DLL_PROCESS_DETACH, %p\n", hinst, param);
1197 if (test_dll_phase == 4 || test_dll_phase == 5)
1199 ok(0, "dll_entry_point(DLL_PROCESS_DETACH) should not be called\n");
1200 break;
1203 /* The process should already deadlock at this point */
1204 if (test_dll_phase == 6)
1206 /* In reality, code below never gets executed, probably some other
1207 * code tries to access process heap and deadlocks earlier, even XP
1208 * doesn't call the DLL entry point on process detach either.
1210 HeapLock(GetProcessHeap());
1211 ok(0, "dll_entry_point: process should already deadlock\n");
1212 break;
1215 if (test_dll_phase == 0 || test_dll_phase == 1 || test_dll_phase == 3)
1216 ok(param != NULL, "dll: param %p\n", param);
1217 else
1218 ok(!param, "dll: param %p\n", param);
1220 if (test_dll_phase == 0 || test_dll_phase == 1) expected_code = 195;
1221 else if (test_dll_phase == 3) expected_code = 196;
1222 else expected_code = STILL_ACTIVE;
1224 if (test_dll_phase == 3)
1226 ret = pRtlDllShutdownInProgress();
1227 ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
1229 else
1231 ret = pRtlDllShutdownInProgress();
1233 /* FIXME: remove once Wine is fixed */
1234 if (expected_code == STILL_ACTIVE || expected_code == 196)
1235 ok(!ret || broken(ret) /* before Vista */, "RtlDllShutdownInProgress returned %d\n", ret);
1236 else
1237 todo_wine
1238 ok(!ret || broken(ret) /* before Vista */, "RtlDllShutdownInProgress returned %d\n", ret);
1241 ok(attached_thread_count >= 2, "attached thread count should be >= 2\n");
1243 for (i = 0; i < attached_thread_count; i++)
1245 /* Calling GetExitCodeThread() without waiting for thread termination
1246 * leads to different results due to a race condition.
1248 if (expected_code != STILL_ACTIVE)
1250 ret = WaitForSingleObject(attached_thread[i], 1000);
1251 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1253 ret = GetExitCodeThread(attached_thread[i], &code);
1254 trace("dll: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
1255 ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
1256 ok(code == expected_code, "expected thread exit code %u, got %u\n", expected_code, code);
1259 ret = WaitForSingleObject(event, 0);
1260 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1262 ret = WaitForSingleObject(mutex, 0);
1263 if (expected_code == STILL_ACTIVE)
1264 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1265 else
1266 ok(ret == WAIT_ABANDONED, "expected WAIT_ABANDONED, got %#x\n", ret);
1268 /* semaphore is not abandoned on thread termination */
1269 ret = WaitForSingleObject(semaphore, 0);
1270 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1272 if (expected_code == STILL_ACTIVE)
1274 ret = WaitForSingleObject(attached_thread[0], 0);
1275 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1276 ret = WaitForSingleObject(attached_thread[1], 0);
1277 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1279 else
1281 ret = WaitForSingleObject(attached_thread[0], 0);
1282 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1283 ret = WaitForSingleObject(attached_thread[1], 0);
1284 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1287 /* win7 doesn't allow to create a thread during process shutdown,
1288 * earlier Windows versions allow it.
1290 noop_thread_started = 0;
1291 SetLastError(0xdeadbeef);
1292 handle = CreateThread(NULL, 0, noop_thread_proc, &noop_thread_started, 0, &ret);
1293 if (param)
1295 ok(!handle || broken(handle != 0) /* before win7 */, "CreateThread should fail\n");
1296 if (!handle)
1297 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1298 else
1300 ret = WaitForSingleObject(handle, 1000);
1301 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1302 CloseHandle(handle);
1305 else
1307 ok(handle != 0, "CreateThread error %d\n", GetLastError());
1308 ret = WaitForSingleObject(handle, 1000);
1309 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1310 ok(!noop_thread_started || broken(noop_thread_started) /* XP64 */, "thread shouldn't start yet\n");
1311 CloseHandle(handle);
1314 SetLastError(0xdeadbeef);
1315 process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
1316 ok(process != NULL, "OpenProcess error %d\n", GetLastError());
1318 noop_thread_started = 0;
1319 SetLastError(0xdeadbeef);
1320 handle = CreateRemoteThread(process, NULL, 0, noop_thread_proc, &noop_thread_started, 0, &ret);
1321 if (param)
1323 ok(!handle || broken(handle != 0) /* before win7 */, "CreateRemoteThread should fail\n");
1324 if (!handle)
1325 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1326 else
1328 ret = WaitForSingleObject(handle, 1000);
1329 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1330 CloseHandle(handle);
1333 else
1335 ok(handle != 0, "CreateRemoteThread error %d\n", GetLastError());
1336 ret = WaitForSingleObject(handle, 1000);
1337 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1338 ok(!noop_thread_started || broken(noop_thread_started) /* XP64 */, "thread shouldn't start yet\n");
1339 CloseHandle(handle);
1342 SetLastError(0xdeadbeef);
1343 handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, NULL);
1344 ok(handle != 0, "CreateFileMapping error %d\n", GetLastError());
1346 offset.u.LowPart = 0;
1347 offset.u.HighPart = 0;
1348 addr = NULL;
1349 size = 0;
1350 ret = pNtMapViewOfSection(handle, process, &addr, 0, 0, &offset,
1351 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
1352 ok(ret == STATUS_SUCCESS, "NtMapViewOfSection error %#x\n", ret);
1353 ret = pNtUnmapViewOfSection(process, addr);
1354 ok(ret == STATUS_SUCCESS, "NtUnmapViewOfSection error %#x\n", ret);
1356 CloseHandle(handle);
1357 CloseHandle(process);
1359 handle = GetModuleHandle("winver.exe");
1360 ok(!handle, "winver.exe shouldn't be loaded yet\n");
1361 SetLastError(0xdeadbeef);
1362 handle = LoadLibrary("winver.exe");
1363 ok(handle != 0, "LoadLibrary error %d\n", GetLastError());
1364 SetLastError(0xdeadbeef);
1365 ret = FreeLibrary(handle);
1366 ok(ret, "FreeLibrary error %d\n", GetLastError());
1367 handle = GetModuleHandle("winver.exe");
1368 if (param)
1369 ok(handle != 0, "winver.exe should not be unloaded\n");
1370 else
1371 todo_wine
1372 ok(!handle || broken(handle != 0) /* before win7 */, "winver.exe should be unloaded\n");
1374 SetLastError(0xdeadbeef);
1375 ret = WaitForDebugEvent(&de, 0);
1376 ok(!ret, "WaitForDebugEvent should fail\n");
1377 todo_wine
1378 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1380 SetLastError(0xdeadbeef);
1381 ret = DebugActiveProcess(GetCurrentProcessId());
1382 ok(!ret, "DebugActiveProcess should fail\n");
1383 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1385 SetLastError(0xdeadbeef);
1386 ret = WaitForDebugEvent(&de, 0);
1387 ok(!ret, "WaitForDebugEvent should fail\n");
1388 ok(GetLastError() == ERROR_SEM_TIMEOUT, "expected ERROR_SEM_TIMEOUT, got %d\n", GetLastError());
1390 if (test_dll_phase == 2)
1392 trace("dll: call ExitProcess()\n");
1393 *child_failures = winetest_get_failures();
1394 ExitProcess(197);
1396 trace("dll: %p, DLL_PROCESS_DETACH, %p => DONE\n", hinst, param);
1397 break;
1399 case DLL_THREAD_ATTACH:
1400 trace("dll: %p, DLL_THREAD_ATTACH, %p\n", hinst, param);
1402 ret = pRtlDllShutdownInProgress();
1403 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1405 if (attached_thread_count < MAX_COUNT)
1407 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &attached_thread[attached_thread_count],
1408 0, TRUE, DUPLICATE_SAME_ACCESS);
1409 attached_thread_count++;
1411 break;
1412 case DLL_THREAD_DETACH:
1413 trace("dll: %p, DLL_THREAD_DETACH, %p\n", hinst, param);
1415 ret = pRtlDllShutdownInProgress();
1416 /* win7 doesn't allow to create a thread during process shutdown,
1417 * earlier Windows versions allow it, and DLL_THREAD_DETACH is
1418 * sent on thread exit, but DLL_THREAD_ATTACH is never received.
1420 if (noop_thread_started)
1421 ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
1422 else
1423 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1425 break;
1426 default:
1427 trace("dll: %p, %d, %p\n", hinst, reason, param);
1428 break;
1431 *child_failures = winetest_get_failures();
1433 return TRUE;
1436 static void child_process(const char *dll_name, DWORD target_offset)
1438 void *target;
1439 DWORD ret, dummy, i, code, expected_code;
1440 HANDLE file, thread, process;
1441 HMODULE hmod;
1442 struct PROCESS_BASIC_INFORMATION_PRIVATE pbi;
1443 DWORD_PTR affinity;
1445 trace("phase %d: writing %p at %#x\n", test_dll_phase, dll_entry_point, target_offset);
1447 SetLastError(0xdeadbeef);
1448 mutex = CreateMutex(NULL, FALSE, NULL);
1449 ok(mutex != 0, "CreateMutex error %d\n", GetLastError());
1451 SetLastError(0xdeadbeef);
1452 semaphore = CreateSemaphore(NULL, 1, 1, NULL);
1453 ok(semaphore != 0, "CreateSemaphore error %d\n", GetLastError());
1455 SetLastError(0xdeadbeef);
1456 event = CreateEvent(NULL, TRUE, FALSE, NULL);
1457 ok(event != 0, "CreateEvent error %d\n", GetLastError());
1459 SetLastError(0xdeadbeef);
1460 loader_lock_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1461 ok(loader_lock_event != 0, "CreateEvent error %d\n", GetLastError());
1463 SetLastError(0xdeadbeef);
1464 peb_lock_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1465 ok(peb_lock_event != 0, "CreateEvent error %d\n", GetLastError());
1467 SetLastError(0xdeadbeef);
1468 heap_lock_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1469 ok(heap_lock_event != 0, "CreateEvent error %d\n", GetLastError());
1471 SetLastError(0xdeadbeef);
1472 ack_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1473 ok(ack_event != 0, "CreateEvent error %d\n", GetLastError());
1475 file = CreateFile(dll_name, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1476 if (file == INVALID_HANDLE_VALUE)
1478 ok(0, "could not open %s\n", dll_name);
1479 return;
1481 SetFilePointer(file, target_offset, NULL, FILE_BEGIN);
1482 SetLastError(0xdeadbeef);
1483 target = dll_entry_point;
1484 ret = WriteFile(file, &target, sizeof(target), &dummy, NULL);
1485 ok(ret, "WriteFile error %d\n", GetLastError());
1486 CloseHandle(file);
1488 SetLastError(0xdeadbeef);
1489 hmod = LoadLibrary(dll_name);
1490 ok(hmod != 0, "LoadLibrary error %d\n", GetLastError());
1492 SetLastError(0xdeadbeef);
1493 stop_event = CreateEvent(NULL, TRUE, FALSE, NULL);
1494 ok(stop_event != 0, "CreateEvent error %d\n", GetLastError());
1496 SetLastError(0xdeadbeef);
1497 thread = CreateThread(NULL, 0, mutex_thread_proc, event, 0, &dummy);
1498 ok(thread != 0, "CreateThread error %d\n", GetLastError());
1499 WaitForSingleObject(event, 3000);
1500 CloseHandle(thread);
1502 ResetEvent(event);
1504 SetLastError(0xdeadbeef);
1505 thread = CreateThread(NULL, 0, semaphore_thread_proc, event, 0, &dummy);
1506 ok(thread != 0, "CreateThread error %d\n", GetLastError());
1507 WaitForSingleObject(event, 3000);
1508 CloseHandle(thread);
1510 ResetEvent(event);
1511 Sleep(100);
1513 ok(attached_thread_count == 2, "attached thread count should be 2\n");
1514 for (i = 0; i < attached_thread_count; i++)
1516 ret = GetExitCodeThread(attached_thread[i], &code);
1517 trace("child: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
1518 ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
1519 ok(code == STILL_ACTIVE, "expected thread exit code STILL_ACTIVE, got %u\n", code);
1522 ret = WaitForSingleObject(attached_thread[0], 0);
1523 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1524 ret = WaitForSingleObject(attached_thread[1], 0);
1525 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1527 ret = WaitForSingleObject(event, 0);
1528 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1529 ret = WaitForSingleObject(mutex, 0);
1530 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1531 ret = WaitForSingleObject(semaphore, 0);
1532 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1534 ret = pRtlDllShutdownInProgress();
1535 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1537 SetLastError(0xdeadbeef);
1538 process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
1539 ok(process != NULL, "OpenProcess error %d\n", GetLastError());
1541 SetLastError(0xdeadbeef);
1542 ret = TerminateProcess(0, 195);
1543 ok(!ret, "TerminateProcess(0) should fail\n");
1544 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1546 Sleep(100);
1548 affinity = 1;
1549 ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
1550 ok(!ret, "NtSetInformationProcess error %#x\n", ret);
1552 switch (test_dll_phase)
1554 case 0:
1555 ret = pRtlDllShutdownInProgress();
1556 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1558 trace("call NtTerminateProcess(0, 195)\n");
1559 ret = pNtTerminateProcess(0, 195);
1560 ok(!ret, "NtTerminateProcess error %#x\n", ret);
1562 memset(&pbi, 0, sizeof(pbi));
1563 ret = pNtQueryInformationProcess(process, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
1564 ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
1565 ok(pbi.ExitStatus == STILL_ACTIVE, "expected STILL_ACTIVE, got %lu\n", pbi.ExitStatus);
1566 affinity = 1;
1567 ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
1568 ok(!ret, "NtSetInformationProcess error %#x\n", ret);
1570 ret = pRtlDllShutdownInProgress();
1571 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1573 hmod = GetModuleHandle(dll_name);
1574 ok(hmod != 0, "DLL should not be unloaded\n");
1576 SetLastError(0xdeadbeef);
1577 thread = CreateThread(NULL, 0, noop_thread_proc, &dummy, 0, &ret);
1578 ok(!thread || broken(thread != 0) /* before win7 */, "CreateThread should fail\n");
1579 if (!thread)
1580 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1581 else
1583 ret = WaitForSingleObject(thread, 1000);
1584 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1585 CloseHandle(thread);
1588 trace("call LdrShutdownProcess()\n");
1589 pLdrShutdownProcess();
1591 ret = pRtlDllShutdownInProgress();
1592 ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
1594 hmod = GetModuleHandle(dll_name);
1595 ok(hmod != 0, "DLL should not be unloaded\n");
1597 memset(&pbi, 0, sizeof(pbi));
1598 ret = pNtQueryInformationProcess(process, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
1599 ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
1600 ok(pbi.ExitStatus == STILL_ACTIVE, "expected STILL_ACTIVE, got %lu\n", pbi.ExitStatus);
1601 affinity = 1;
1602 ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
1603 ok(!ret, "NtSetInformationProcess error %#x\n", ret);
1604 break;
1606 case 1: /* normal ExitProcess */
1607 ret = pRtlDllShutdownInProgress();
1608 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1609 break;
1611 case 2: /* ExitProcess will be called by the PROCESS_DETACH handler */
1612 ret = pRtlDllShutdownInProgress();
1613 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1615 trace("call FreeLibrary(%p)\n", hmod);
1616 SetLastError(0xdeadbeef);
1617 ret = FreeLibrary(hmod);
1618 ok(ret, "FreeLibrary error %d\n", GetLastError());
1619 hmod = GetModuleHandle(dll_name);
1620 ok(!hmod, "DLL should be unloaded\n");
1622 if (test_dll_phase == 2)
1623 ok(0, "FreeLibrary+ExitProcess should never return\n");
1625 ret = pRtlDllShutdownInProgress();
1626 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
1628 break;
1630 case 3:
1631 trace("signalling thread exit\n");
1632 SetEvent(stop_event);
1633 CloseHandle(stop_event);
1634 break;
1636 case 4:
1637 trace("setting loader_lock_event\n");
1638 SetEvent(loader_lock_event);
1639 WaitForSingleObject(ack_event, 1000);
1640 ok(inside_loader_lock != 0, "inside_loader_lock is not set\n");
1642 /* calling NtTerminateProcess should not cause a deadlock */
1643 trace("call NtTerminateProcess(0, 198)\n");
1644 ret = pNtTerminateProcess(0, 198);
1645 ok(!ret, "NtTerminateProcess error %#x\n", ret);
1647 *child_failures = winetest_get_failures();
1649 /* Windows fails to release loader lock acquired from another thread,
1650 * so the LdrUnlockLoaderLock call fails here and ExitProcess deadlocks
1651 * later on, so NtTerminateProcess is used instead.
1653 trace("call NtTerminateProcess(GetCurrentProcess(), 198)\n");
1654 pNtTerminateProcess(GetCurrentProcess(), 198);
1655 ok(0, "NtTerminateProcess should not return\n");
1656 break;
1658 case 5:
1659 trace("setting peb_lock_event\n");
1660 SetEvent(peb_lock_event);
1661 WaitForSingleObject(ack_event, 1000);
1662 ok(inside_peb_lock != 0, "inside_peb_lock is not set\n");
1664 *child_failures = winetest_get_failures();
1666 /* calling ExitProcess should cause a deadlock */
1667 trace("call ExitProcess(198)\n");
1668 ExitProcess(198);
1669 ok(0, "ExitProcess should not return\n");
1670 break;
1672 case 6:
1673 trace("setting heap_lock_event\n");
1674 SetEvent(heap_lock_event);
1675 WaitForSingleObject(ack_event, 1000);
1676 ok(inside_heap_lock != 0, "inside_heap_lock is not set\n");
1678 *child_failures = winetest_get_failures();
1680 /* calling ExitProcess should cause a deadlock */
1681 trace("call ExitProcess(1)\n");
1682 ExitProcess(1);
1683 ok(0, "ExitProcess should not return\n");
1684 break;
1686 default:
1687 assert(0);
1688 break;
1691 if (test_dll_phase == 0) expected_code = 195;
1692 else if (test_dll_phase == 3) expected_code = 196;
1693 else if (test_dll_phase == 4) expected_code = 198;
1694 else expected_code = STILL_ACTIVE;
1696 if (expected_code == STILL_ACTIVE)
1698 ret = WaitForSingleObject(attached_thread[0], 100);
1699 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1700 ret = WaitForSingleObject(attached_thread[1], 100);
1701 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
1703 else
1705 ret = WaitForSingleObject(attached_thread[0], 1000);
1706 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1707 ret = WaitForSingleObject(attached_thread[1], 1000);
1708 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1711 for (i = 0; i < attached_thread_count; i++)
1713 ret = GetExitCodeThread(attached_thread[i], &code);
1714 trace("child: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
1715 ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
1716 ok(code == expected_code, "expected thread exit code %u, got %u\n", expected_code, code);
1719 *child_failures = winetest_get_failures();
1721 trace("call ExitProcess(195)\n");
1722 ExitProcess(195);
1725 static void test_ExitProcess(void)
1727 #include "pshpack1.h"
1728 #ifdef __x86_64__
1729 static struct section_data
1731 BYTE mov_rax[2];
1732 void *target;
1733 BYTE jmp_rax[2];
1734 } section_data = { { 0x48,0xb8 }, dll_entry_point, { 0xff,0xe0 } };
1735 #else
1736 static struct section_data
1738 BYTE mov_eax;
1739 void *target;
1740 BYTE jmp_eax[2];
1741 } section_data = { 0xb8, dll_entry_point, { 0xff,0xe0 } };
1742 #endif
1743 #include "poppack.h"
1744 static const char filler[0x1000];
1745 DWORD dummy, file_align;
1746 HANDLE file, thread, process, hmap, hmap_dup;
1747 char temp_path[MAX_PATH], dll_name[MAX_PATH], cmdline[MAX_PATH * 2];
1748 DWORD ret, target_offset, old_prot;
1749 char **argv, buf[256];
1750 PROCESS_INFORMATION pi;
1751 STARTUPINFO si = { sizeof(si) };
1752 CONTEXT ctx;
1753 struct PROCESS_BASIC_INFORMATION_PRIVATE pbi;
1754 MEMORY_BASIC_INFORMATION mbi;
1755 DWORD_PTR affinity;
1756 void *addr;
1757 LARGE_INTEGER offset;
1758 SIZE_T size;
1760 #if !defined(__i386__) && !defined(__x86_64__)
1761 skip("x86 specific ExitProcess test\n");
1762 return;
1763 #endif
1765 if (!pRtlDllShutdownInProgress)
1767 win_skip("RtlDllShutdownInProgress is not available on this platform (XP+)\n");
1768 return;
1770 if (!pNtQueryInformationProcess || !pNtSetInformationProcess)
1772 win_skip("NtQueryInformationProcess/NtSetInformationProcess are not available on this platform\n");
1773 return;
1775 if (!pNtAllocateVirtualMemory || !pNtFreeVirtualMemory)
1777 win_skip("NtAllocateVirtualMemory/NtFreeVirtualMemory are not available on this platform\n");
1778 return;
1781 /* prevent displaying of the "Unable to load this DLL" message box */
1782 SetErrorMode(SEM_FAILCRITICALERRORS);
1784 GetTempPath(MAX_PATH, temp_path);
1785 GetTempFileName(temp_path, "ldr", 0, dll_name);
1787 /*trace("creating %s\n", dll_name);*/
1788 file = CreateFile(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
1789 if (file == INVALID_HANDLE_VALUE)
1791 ok(0, "could not create %s\n", dll_name);
1792 return;
1795 SetLastError(0xdeadbeef);
1796 ret = WriteFile(file, &dos_header, sizeof(dos_header), &dummy, NULL);
1797 ok(ret, "WriteFile error %d\n", GetLastError());
1799 nt_header.FileHeader.NumberOfSections = 1;
1800 nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
1801 nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL | IMAGE_FILE_RELOCS_STRIPPED;
1803 nt_header.OptionalHeader.AddressOfEntryPoint = 0x1000;
1804 nt_header.OptionalHeader.SectionAlignment = 0x1000;
1805 nt_header.OptionalHeader.FileAlignment = 0x200;
1806 nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000;
1807 nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER);
1808 SetLastError(0xdeadbeef);
1809 ret = WriteFile(file, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
1810 ok(ret, "WriteFile error %d\n", GetLastError());
1811 SetLastError(0xdeadbeef);
1812 ret = WriteFile(file, &nt_header.OptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER), &dummy, NULL);
1813 ok(ret, "WriteFile error %d\n", GetLastError());
1815 section.SizeOfRawData = sizeof(section_data);
1816 section.PointerToRawData = nt_header.OptionalHeader.FileAlignment;
1817 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
1818 section.Misc.VirtualSize = sizeof(section_data);
1819 section.Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
1820 SetLastError(0xdeadbeef);
1821 ret = WriteFile(file, &section, sizeof(section), &dummy, NULL);
1822 ok(ret, "WriteFile error %d\n", GetLastError());
1824 file_align = nt_header.OptionalHeader.FileAlignment - nt_header.OptionalHeader.SizeOfHeaders;
1825 assert(file_align < sizeof(filler));
1826 SetLastError(0xdeadbeef);
1827 ret = WriteFile(file, filler, file_align, &dummy, NULL);
1828 ok(ret, "WriteFile error %d\n", GetLastError());
1830 target_offset = SetFilePointer(file, 0, NULL, FILE_CURRENT) + FIELD_OFFSET(struct section_data, target);
1832 /* section data */
1833 SetLastError(0xdeadbeef);
1834 ret = WriteFile(file, &section_data, sizeof(section_data), &dummy, NULL);
1835 ok(ret, "WriteFile error %d\n", GetLastError());
1837 CloseHandle(file);
1839 winetest_get_mainargs(&argv);
1841 /* phase 0 */
1842 *child_failures = -1;
1843 sprintf(cmdline, "\"%s\" loader %s %u 0", argv[0], dll_name, target_offset);
1844 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1845 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1846 ret = WaitForSingleObject(pi.hProcess, 10000);
1847 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1848 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
1849 GetExitCodeProcess(pi.hProcess, &ret);
1850 ok(ret == 195, "expected exit code 195, got %u\n", ret);
1851 if (*child_failures)
1853 trace("%d failures in child process\n", *child_failures);
1854 winetest_add_failures(*child_failures);
1856 CloseHandle(pi.hThread);
1857 CloseHandle(pi.hProcess);
1859 /* phase 1 */
1860 *child_failures = -1;
1861 sprintf(cmdline, "\"%s\" loader %s %u 1", argv[0], dll_name, target_offset);
1862 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1863 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1864 ret = WaitForSingleObject(pi.hProcess, 10000);
1865 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1866 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
1867 GetExitCodeProcess(pi.hProcess, &ret);
1868 ok(ret == 195, "expected exit code 195, got %u\n", ret);
1869 if (*child_failures)
1871 trace("%d failures in child process\n", *child_failures);
1872 winetest_add_failures(*child_failures);
1874 CloseHandle(pi.hThread);
1875 CloseHandle(pi.hProcess);
1877 /* phase 2 */
1878 *child_failures = -1;
1879 sprintf(cmdline, "\"%s\" loader %s %u 2", argv[0], dll_name, target_offset);
1880 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1881 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1882 ret = WaitForSingleObject(pi.hProcess, 10000);
1883 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1884 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
1885 GetExitCodeProcess(pi.hProcess, &ret);
1886 ok(ret == 197, "expected exit code 197, got %u\n", ret);
1887 if (*child_failures)
1889 trace("%d failures in child process\n", *child_failures);
1890 winetest_add_failures(*child_failures);
1892 CloseHandle(pi.hThread);
1893 CloseHandle(pi.hProcess);
1895 /* phase 3 */
1896 *child_failures = -1;
1897 sprintf(cmdline, "\"%s\" loader %s %u 3", argv[0], dll_name, target_offset);
1898 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1899 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1900 ret = WaitForSingleObject(pi.hProcess, 10000);
1901 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1902 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
1903 GetExitCodeProcess(pi.hProcess, &ret);
1904 ok(ret == 195, "expected exit code 195, got %u\n", ret);
1905 if (*child_failures)
1907 trace("%d failures in child process\n", *child_failures);
1908 winetest_add_failures(*child_failures);
1910 CloseHandle(pi.hThread);
1911 CloseHandle(pi.hProcess);
1913 /* phase 4 */
1914 if (pLdrLockLoaderLock && pLdrUnlockLoaderLock)
1916 *child_failures = -1;
1917 sprintf(cmdline, "\"%s\" loader %s %u 4", 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, 10000);
1921 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1922 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
1923 GetExitCodeProcess(pi.hProcess, &ret);
1924 ok(ret == 198, "expected exit code 198, got %u\n", ret);
1925 if (*child_failures)
1927 trace("%d failures in child process\n", *child_failures);
1928 winetest_add_failures(*child_failures);
1930 CloseHandle(pi.hThread);
1931 CloseHandle(pi.hProcess);
1933 else
1934 win_skip("LdrLockLoaderLock/LdrUnlockLoaderLock are not available on this platform\n");
1936 /* phase 5 */
1937 if (pRtlAcquirePebLock && pRtlReleasePebLock)
1939 *child_failures = -1;
1940 sprintf(cmdline, "\"%s\" loader %s %u 5", argv[0], dll_name, target_offset);
1941 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1942 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1943 ret = WaitForSingleObject(pi.hProcess, 5000);
1944 ok(ret == WAIT_TIMEOUT, "child process should fail to terminate\n");
1945 if (ret != WAIT_OBJECT_0)
1947 trace("terminating child process\n");
1948 TerminateProcess(pi.hProcess, 199);
1950 ret = WaitForSingleObject(pi.hProcess, 1000);
1951 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1952 GetExitCodeProcess(pi.hProcess, &ret);
1953 ok(ret == 199, "expected exit code 199, got %u\n", ret);
1954 if (*child_failures)
1956 trace("%d failures in child process\n", *child_failures);
1957 winetest_add_failures(*child_failures);
1959 CloseHandle(pi.hThread);
1960 CloseHandle(pi.hProcess);
1962 else
1963 win_skip("RtlAcquirePebLock/RtlReleasePebLock are not available on this platform\n");
1965 /* phase 6 */
1966 *child_failures = -1;
1967 sprintf(cmdline, "\"%s\" loader %s %u 6", argv[0], dll_name, target_offset);
1968 ret = CreateProcess(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
1969 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
1970 ret = WaitForSingleObject(pi.hProcess, 5000);
1971 ok(ret == WAIT_TIMEOUT || broken(ret == WAIT_OBJECT_0) /* XP */, "child process should fail to terminate\n");
1972 if (ret != WAIT_OBJECT_0)
1974 trace("terminating child process\n");
1975 TerminateProcess(pi.hProcess, 201);
1977 ret = WaitForSingleObject(pi.hProcess, 1000);
1978 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
1979 GetExitCodeProcess(pi.hProcess, &ret);
1980 ok(ret == 201 || broken(ret == 1) /* XP */, "expected exit code 201, got %u\n", ret);
1981 if (*child_failures)
1983 trace("%d failures in child process\n", *child_failures);
1984 winetest_add_failures(*child_failures);
1986 CloseHandle(pi.hThread);
1987 CloseHandle(pi.hProcess);
1989 /* test remote process termination */
1990 SetLastError(0xdeadbeef);
1991 ret = CreateProcess(argv[0], NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
1992 ok(ret, "CreateProcess(%s) error %d\n", argv[0], GetLastError());
1994 SetLastError(0xdeadbeef);
1995 addr = VirtualAllocEx(pi.hProcess, NULL, 4096, MEM_COMMIT, PAGE_READWRITE);
1996 ok(addr != NULL, "VirtualAllocEx error %d\n", GetLastError());
1997 SetLastError(0xdeadbeef);
1998 ret = VirtualProtectEx(pi.hProcess, addr, 4096, PAGE_READONLY, &old_prot);
1999 ok(ret, "VirtualProtectEx error %d\n", GetLastError());
2000 ok(old_prot == PAGE_READWRITE, "expected PAGE_READWRITE, got %#x\n", old_prot);
2001 SetLastError(0xdeadbeef);
2002 size = VirtualQueryEx(pi.hProcess, NULL, &mbi, sizeof(mbi));
2003 ok(size == sizeof(mbi), "VirtualQueryEx error %d\n", GetLastError());
2005 SetLastError(0xdeadbeef);
2006 ret = ReadProcessMemory(pi.hProcess, addr, buf, 4, &size);
2007 ok(ret, "ReadProcessMemory error %d\n", GetLastError());
2008 ok(size == 4, "expected 4, got %lu\n", size);
2010 SetLastError(0xdeadbeef);
2011 hmap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, NULL);
2012 ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
2014 SetLastError(0xdeadbeef);
2015 ret = DuplicateHandle(GetCurrentProcess(), hmap, pi.hProcess, &hmap_dup,
2016 0, FALSE, DUPLICATE_SAME_ACCESS);
2017 ok(ret, "DuplicateHandle error %d\n", GetLastError());
2019 offset.u.LowPart = 0;
2020 offset.u.HighPart = 0;
2021 addr = NULL;
2022 size = 0;
2023 ret = pNtMapViewOfSection(hmap, pi.hProcess, &addr, 0, 0, &offset,
2024 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
2025 ok(!ret, "NtMapViewOfSection error %#x\n", ret);
2026 ret = pNtUnmapViewOfSection(pi.hProcess, addr);
2027 ok(!ret, "NtUnmapViewOfSection error %#x\n", ret);
2029 SetLastError(0xdeadbeef);
2030 thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void *)0xdeadbeef, NULL, CREATE_SUSPENDED, &ret);
2031 ok(thread != 0, "CreateRemoteThread error %d\n", GetLastError());
2032 SetLastError(0xdeadbeef);
2033 ctx.ContextFlags = CONTEXT_INTEGER;
2034 ret = GetThreadContext(thread, &ctx);
2035 ok(ret, "GetThreadContext error %d\n", GetLastError());
2036 SetLastError(0xdeadbeef);
2037 ctx.ContextFlags = CONTEXT_INTEGER;
2038 ret = SetThreadContext(thread, &ctx);
2039 ok(ret, "SetThreadContext error %d\n", GetLastError());
2040 SetLastError(0xdeadbeef);
2041 ret = SetThreadPriority(thread, 0);
2042 ok(ret, "SetThreadPriority error %d\n", GetLastError());
2044 SetLastError(0xdeadbeef);
2045 ret = TerminateThread(thread, 199);
2046 ok(ret, "TerminateThread error %d\n", GetLastError());
2047 /* Calling GetExitCodeThread() without waiting for thread termination
2048 * leads to different results due to a race condition.
2050 ret = WaitForSingleObject(thread, 1000);
2051 ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
2052 GetExitCodeThread(thread, &ret);
2053 ok(ret == 199, "expected exit code 199, got %u\n", ret);
2055 SetLastError(0xdeadbeef);
2056 ret = TerminateProcess(pi.hProcess, 198);
2057 ok(ret, "TerminateProcess error %d\n", GetLastError());
2058 /* Checking process state without waiting for process termination
2059 * leads to different results due to a race condition.
2061 ret = WaitForSingleObject(pi.hProcess, 1000);
2062 ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
2064 SetLastError(0xdeadbeef);
2065 process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pi.dwProcessId);
2066 ok(process != NULL, "OpenProcess error %d\n", GetLastError());
2067 CloseHandle(process);
2069 memset(&pbi, 0, sizeof(pbi));
2070 ret = pNtQueryInformationProcess(pi.hProcess, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
2071 ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
2072 ok(pbi.ExitStatus == 198, "expected 198, got %lu\n", pbi.ExitStatus);
2073 affinity = 1;
2074 ret = pNtSetInformationProcess(pi.hProcess, ProcessAffinityMask, &affinity, sizeof(affinity));
2075 ok(ret == STATUS_PROCESS_IS_TERMINATING, "expected STATUS_PROCESS_IS_TERMINATING, got %#x\n", ret);
2077 SetLastError(0xdeadbeef);
2078 ctx.ContextFlags = CONTEXT_INTEGER;
2079 ret = GetThreadContext(thread, &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(thread, &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 = SetThreadPriority(thread, 0);
2097 ok(ret, "SetThreadPriority error %d\n", GetLastError());
2098 CloseHandle(thread);
2100 SetLastError(0xdeadbeef);
2101 ctx.ContextFlags = CONTEXT_INTEGER;
2102 ret = GetThreadContext(pi.hThread, &ctx);
2103 ok(!ret || broken(ret) /* XP 64-bit */, "GetThreadContext should fail\n");
2104 if (!ret)
2105 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
2106 GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
2107 GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
2108 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2109 SetLastError(0xdeadbeef);
2110 ctx.ContextFlags = CONTEXT_INTEGER;
2111 ret = SetThreadContext(pi.hThread, &ctx);
2112 ok(!ret || broken(ret) /* XP 64-bit */, "SetThreadContext should fail\n");
2113 if (!ret)
2114 ok(GetLastError() == ERROR_ACCESS_DENIED ||
2115 GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
2116 GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
2117 "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2118 SetLastError(0xdeadbeef);
2119 ret = VirtualProtectEx(pi.hProcess, addr, 4096, PAGE_READWRITE, &old_prot);
2120 ok(!ret, "VirtualProtectEx should fail\n");
2121 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2122 SetLastError(0xdeadbeef);
2123 size = 0;
2124 ret = ReadProcessMemory(pi.hProcess, addr, buf, 4, &size);
2125 ok(!ret, "ReadProcessMemory should fail\n");
2126 ok(GetLastError() == ERROR_PARTIAL_COPY || GetLastError() == ERROR_ACCESS_DENIED,
2127 "expected ERROR_PARTIAL_COPY, got %d\n", GetLastError());
2128 ok(!size, "expected 0, got %lu\n", size);
2129 SetLastError(0xdeadbeef);
2130 ret = VirtualFreeEx(pi.hProcess, addr, 0, MEM_RELEASE);
2131 ok(!ret, "VirtualFreeEx should fail\n");
2132 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2133 SetLastError(0xdeadbeef);
2134 addr = VirtualAllocEx(pi.hProcess, NULL, 4096, MEM_COMMIT, PAGE_READWRITE);
2135 ok(!addr, "VirtualAllocEx should fail\n");
2136 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2137 SetLastError(0xdeadbeef);
2138 size = VirtualQueryEx(pi.hProcess, NULL, &mbi, sizeof(mbi));
2139 ok(!size, "VirtualQueryEx should fail\n");
2140 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2142 /* CloseHandle() call below leads to premature process termination
2143 * under some Windows versions.
2145 if (0)
2147 SetLastError(0xdeadbeef);
2148 ret = CloseHandle(hmap_dup);
2149 ok(ret, "CloseHandle should not fail\n");
2152 SetLastError(0xdeadbeef);
2153 ret = DuplicateHandle(GetCurrentProcess(), hmap, pi.hProcess, &hmap_dup,
2154 0, FALSE, DUPLICATE_SAME_ACCESS);
2155 ok(!ret, "DuplicateHandle should fail\n");
2156 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2158 offset.u.LowPart = 0;
2159 offset.u.HighPart = 0;
2160 addr = NULL;
2161 size = 0;
2162 ret = pNtMapViewOfSection(hmap, pi.hProcess, &addr, 0, 0, &offset,
2163 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
2164 ok(ret == STATUS_PROCESS_IS_TERMINATING, "expected STATUS_PROCESS_IS_TERMINATING, got %#x\n", ret);
2166 SetLastError(0xdeadbeef);
2167 thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void *)0xdeadbeef, NULL, CREATE_SUSPENDED, &ret);
2168 ok(!thread, "CreateRemoteThread should fail\n");
2169 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2171 SetLastError(0xdeadbeef);
2172 ret = DebugActiveProcess(pi.dwProcessId);
2173 ok(!ret, "DebugActiveProcess should fail\n");
2174 ok(GetLastError() == ERROR_ACCESS_DENIED /* 64-bit */ || GetLastError() == ERROR_NOT_SUPPORTED /* 32-bit */,
2175 "ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2177 GetExitCodeProcess(pi.hProcess, &ret);
2178 ok(ret == 198 || broken(ret != 198) /* some 32-bit XP version in a VM returns random exit code */,
2179 "expected exit code 198, got %u\n", ret);
2180 CloseHandle(pi.hThread);
2181 CloseHandle(pi.hProcess);
2183 ret = DeleteFile(dll_name);
2184 ok(ret, "DeleteFile error %d\n", GetLastError());
2187 START_TEST(loader)
2189 int argc;
2190 char **argv;
2191 HANDLE mapping;
2193 pNtMapViewOfSection = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtMapViewOfSection");
2194 pNtUnmapViewOfSection = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtUnmapViewOfSection");
2195 pNtTerminateProcess = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtTerminateProcess");
2196 pNtQueryInformationProcess = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryInformationProcess");
2197 pNtSetInformationProcess = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtSetInformationProcess");
2198 pLdrShutdownProcess = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "LdrShutdownProcess");
2199 pRtlDllShutdownInProgress = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlDllShutdownInProgress");
2200 pNtAllocateVirtualMemory = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtAllocateVirtualMemory");
2201 pNtFreeVirtualMemory = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtFreeVirtualMemory");
2202 pLdrLockLoaderLock = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "LdrLockLoaderLock");
2203 pLdrUnlockLoaderLock = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "LdrUnlockLoaderLock");
2204 pRtlAcquirePebLock = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlAcquirePebLock");
2205 pRtlReleasePebLock = (void *)GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlReleasePebLock");
2207 mapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "winetest_loader");
2208 ok(mapping != 0, "CreateFileMapping failed\n");
2209 child_failures = MapViewOfFile(mapping, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 4096);
2210 if (*child_failures == -1)
2212 is_child = 1;
2213 *child_failures = 0;
2215 else
2216 *child_failures = -1;
2218 argc = winetest_get_mainargs(&argv);
2219 if (argc > 4)
2221 test_dll_phase = atoi(argv[4]);
2222 child_process(argv[2], atol(argv[3]));
2223 return;
2226 test_Loader();
2227 test_ImportDescriptors();
2228 test_section_access();
2229 test_ExitProcess();