server: Implement the various image flags in SECTION_IMAGE_INFORMATION.
[wine.git] / dlls / kernel32 / tests / loader.c
blob25d3bf51c58491f6c7d2ab9e44318165e2497f5d
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 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <assert.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winternl.h"
32 #include "wine/test.h"
33 #include "delayloadhandler.h"
35 /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
36 #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
38 #define ALIGN_SIZE(size, alignment) (((size) + (alignment - 1)) & ~((alignment - 1)))
40 struct PROCESS_BASIC_INFORMATION_PRIVATE
42 DWORD_PTR ExitStatus;
43 PPEB PebBaseAddress;
44 DWORD_PTR AffinityMask;
45 DWORD_PTR BasePriority;
46 ULONG_PTR UniqueProcessId;
47 ULONG_PTR InheritedFromUniqueProcessId;
50 static LONG *child_failures;
51 static WORD cb_count;
52 static DWORD page_size;
53 static BOOL is_win64 = sizeof(void *) > sizeof(int);
54 static BOOL is_wow64;
56 static NTSTATUS (WINAPI *pNtCreateSection)(HANDLE *, ACCESS_MASK, const OBJECT_ATTRIBUTES *,
57 const LARGE_INTEGER *, ULONG, ULONG, HANDLE );
58 static NTSTATUS (WINAPI *pNtQuerySection)(HANDLE, SECTION_INFORMATION_CLASS, void *, SIZE_T, SIZE_T *);
59 static NTSTATUS (WINAPI *pNtMapViewOfSection)(HANDLE, HANDLE, PVOID *, ULONG, SIZE_T, const LARGE_INTEGER *, SIZE_T *, ULONG, ULONG, ULONG);
60 static NTSTATUS (WINAPI *pNtUnmapViewOfSection)(HANDLE, PVOID);
61 static NTSTATUS (WINAPI *pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
62 static NTSTATUS (WINAPI *pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
63 static NTSTATUS (WINAPI *pNtTerminateProcess)(HANDLE, DWORD);
64 static void (WINAPI *pLdrShutdownProcess)(void);
65 static BOOLEAN (WINAPI *pRtlDllShutdownInProgress)(void);
66 static NTSTATUS (WINAPI *pNtAllocateVirtualMemory)(HANDLE, PVOID *, ULONG, SIZE_T *, ULONG, ULONG);
67 static NTSTATUS (WINAPI *pNtFreeVirtualMemory)(HANDLE, PVOID *, SIZE_T *, ULONG);
68 static NTSTATUS (WINAPI *pLdrLockLoaderLock)(ULONG, ULONG *, ULONG_PTR *);
69 static NTSTATUS (WINAPI *pLdrUnlockLoaderLock)(ULONG, ULONG_PTR);
70 static void (WINAPI *pRtlAcquirePebLock)(void);
71 static void (WINAPI *pRtlReleasePebLock)(void);
72 static PVOID (WINAPI *pResolveDelayLoadedAPI)(PVOID, PCIMAGE_DELAYLOAD_DESCRIPTOR,
73 PDELAYLOAD_FAILURE_DLL_CALLBACK, PVOID,
74 PIMAGE_THUNK_DATA ThunkAddress,ULONG);
75 static PVOID (WINAPI *pRtlImageDirectoryEntryToData)(HMODULE,BOOL,WORD,ULONG *);
76 static DWORD (WINAPI *pFlsAlloc)(PFLS_CALLBACK_FUNCTION);
77 static BOOL (WINAPI *pFlsSetValue)(DWORD, PVOID);
78 static PVOID (WINAPI *pFlsGetValue)(DWORD);
79 static BOOL (WINAPI *pFlsFree)(DWORD);
80 static BOOL (WINAPI *pIsWow64Process)(HANDLE,PBOOL);
82 static PVOID RVAToAddr(DWORD_PTR rva, HMODULE module)
84 if (rva == 0)
85 return NULL;
86 return ((char*) module) + rva;
89 static IMAGE_DOS_HEADER dos_header;
91 static const IMAGE_NT_HEADERS nt_header_template =
93 IMAGE_NT_SIGNATURE, /* Signature */
95 #if defined __i386__
96 IMAGE_FILE_MACHINE_I386, /* Machine */
97 #elif defined __x86_64__
98 IMAGE_FILE_MACHINE_AMD64, /* Machine */
99 #elif defined __powerpc__
100 IMAGE_FILE_MACHINE_POWERPC, /* Machine */
101 #elif defined __arm__
102 IMAGE_FILE_MACHINE_ARMNT, /* Machine */
103 #elif defined __aarch64__
104 IMAGE_FILE_MACHINE_ARM64, /* Machine */
105 #else
106 # error You must specify the machine type
107 #endif
108 1, /* NumberOfSections */
109 0, /* TimeDateStamp */
110 0, /* PointerToSymbolTable */
111 0, /* NumberOfSymbols */
112 sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
113 IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
115 { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
116 1, /* MajorLinkerVersion */
117 0, /* MinorLinkerVersion */
118 0, /* SizeOfCode */
119 0, /* SizeOfInitializedData */
120 0, /* SizeOfUninitializedData */
121 0, /* AddressOfEntryPoint */
122 0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
123 #ifndef _WIN64
124 0, /* BaseOfData */
125 #endif
126 0x10000000, /* ImageBase */
127 0, /* SectionAlignment */
128 0, /* FileAlignment */
129 4, /* MajorOperatingSystemVersion */
130 0, /* MinorOperatingSystemVersion */
131 1, /* MajorImageVersion */
132 0, /* MinorImageVersion */
133 4, /* MajorSubsystemVersion */
134 0, /* MinorSubsystemVersion */
135 0, /* Win32VersionValue */
136 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000, /* SizeOfImage */
137 sizeof(dos_header) + sizeof(nt_header_template), /* SizeOfHeaders */
138 0, /* CheckSum */
139 IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
140 0, /* DllCharacteristics */
141 0, /* SizeOfStackReserve */
142 0, /* SizeOfStackCommit */
143 0, /* SizeOfHeapReserve */
144 0, /* SizeOfHeapCommit */
145 0, /* LoaderFlags */
146 0, /* NumberOfRvaAndSizes */
147 { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
151 static IMAGE_SECTION_HEADER section =
153 ".rodata", /* Name */
154 { 0 }, /* Misc */
155 0, /* VirtualAddress */
156 0, /* SizeOfRawData */
157 0, /* PointerToRawData */
158 0, /* PointerToRelocations */
159 0, /* PointerToLinenumbers */
160 0, /* NumberOfRelocations */
161 0, /* NumberOfLinenumbers */
162 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, /* Characteristics */
166 static const char filler[0x1000];
167 static const char section_data[0x10] = "section data";
169 static DWORD create_test_dll( const IMAGE_DOS_HEADER *dos_header, UINT dos_size,
170 const IMAGE_NT_HEADERS *nt_header, char dll_name[MAX_PATH] )
172 char temp_path[MAX_PATH];
173 DWORD dummy, size, file_align;
174 HANDLE hfile;
175 BOOL ret;
177 GetTempPathA(MAX_PATH, temp_path);
178 GetTempFileNameA(temp_path, "ldr", 0, dll_name);
180 hfile = CreateFileA(dll_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0);
181 ok( hfile != INVALID_HANDLE_VALUE, "failed to create %s err %u\n", dll_name, GetLastError() );
182 if (hfile == INVALID_HANDLE_VALUE) return 0;
184 SetLastError(0xdeadbeef);
185 ret = WriteFile(hfile, dos_header, dos_size, &dummy, NULL);
186 ok(ret, "WriteFile error %d\n", GetLastError());
188 SetLastError(0xdeadbeef);
189 ret = WriteFile(hfile, nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
190 ok(ret, "WriteFile error %d\n", GetLastError());
192 if (nt_header->FileHeader.SizeOfOptionalHeader)
194 SetLastError(0xdeadbeef);
195 ret = WriteFile(hfile, &nt_header->OptionalHeader,
196 min(nt_header->FileHeader.SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER)),
197 &dummy, NULL);
198 ok(ret, "WriteFile error %d\n", GetLastError());
199 if (nt_header->FileHeader.SizeOfOptionalHeader > sizeof(IMAGE_OPTIONAL_HEADER))
201 file_align = nt_header->FileHeader.SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER);
202 assert(file_align < sizeof(filler));
203 SetLastError(0xdeadbeef);
204 ret = WriteFile(hfile, filler, file_align, &dummy, NULL);
205 ok(ret, "WriteFile error %d\n", GetLastError());
209 assert(nt_header->FileHeader.NumberOfSections <= 1);
210 if (nt_header->FileHeader.NumberOfSections)
212 section.SizeOfRawData = 10;
214 if (nt_header->OptionalHeader.SectionAlignment >= page_size)
216 section.PointerToRawData = dos_size;
217 section.VirtualAddress = nt_header->OptionalHeader.SectionAlignment;
218 section.Misc.VirtualSize = section.SizeOfRawData * 10;
220 else
222 section.PointerToRawData = nt_header->OptionalHeader.SizeOfHeaders;
223 section.VirtualAddress = nt_header->OptionalHeader.SizeOfHeaders;
224 section.Misc.VirtualSize = 5;
227 SetLastError(0xdeadbeef);
228 ret = WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
229 ok(ret, "WriteFile error %d\n", GetLastError());
231 /* section data */
232 SetLastError(0xdeadbeef);
233 ret = WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL);
234 ok(ret, "WriteFile error %d\n", GetLastError());
236 size = GetFileSize(hfile, NULL);
237 CloseHandle(hfile);
238 return size;
241 static DWORD create_test_dll_sections( const IMAGE_DOS_HEADER *dos_header, const IMAGE_NT_HEADERS *nt_header,
242 const IMAGE_SECTION_HEADER *sections, const void *section_data,
243 char dll_name[MAX_PATH] )
245 char temp_path[MAX_PATH];
246 DWORD dummy, i, size;
247 HANDLE hfile;
248 BOOL ret;
250 GetTempPathA(MAX_PATH, temp_path);
251 GetTempFileNameA(temp_path, "ldr", 0, dll_name);
253 hfile = CreateFileA(dll_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0);
254 ok( hfile != INVALID_HANDLE_VALUE, "failed to create %s err %u\n", dll_name, GetLastError() );
255 if (hfile == INVALID_HANDLE_VALUE) return 0;
257 SetLastError(0xdeadbeef);
258 ret = WriteFile(hfile, dos_header, sizeof(*dos_header), &dummy, NULL);
259 ok(ret, "WriteFile error %d\n", GetLastError());
261 SetLastError(0xdeadbeef);
262 ret = WriteFile(hfile, nt_header, offsetof(IMAGE_NT_HEADERS, OptionalHeader) + nt_header->FileHeader.SizeOfOptionalHeader, &dummy, NULL);
263 ok(ret, "WriteFile error %d\n", GetLastError());
265 SetLastError(0xdeadbeef);
266 ret = WriteFile(hfile, sections, sizeof(*sections) * nt_header->FileHeader.NumberOfSections,
267 &dummy, NULL);
268 ok(ret, "WriteFile error %d\n", GetLastError());
270 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
272 SetFilePointer(hfile, sections[i].PointerToRawData, NULL, FILE_BEGIN);
273 SetLastError(0xdeadbeef);
274 ret = WriteFile(hfile, section_data, sections[i].SizeOfRawData, &dummy, NULL);
275 ok(ret, "WriteFile error %d\n", GetLastError());
277 size = GetFileSize(hfile, NULL);
278 CloseHandle(hfile);
279 return size;
282 static BOOL query_image_section( int id, const char *dll_name, const IMAGE_NT_HEADERS *nt_header,
283 const void *section_data )
285 static BOOL is_winxp;
286 SECTION_BASIC_INFORMATION info;
287 SECTION_IMAGE_INFORMATION image;
288 const IMAGE_COR20_HEADER *cor_header = NULL;
289 SIZE_T info_size = (SIZE_T)0xdeadbeef << 16;
290 NTSTATUS status;
291 HANDLE file, mapping;
292 ULONG file_size;
293 LARGE_INTEGER map_size;
294 SIZE_T max_stack, commit_stack;
295 void *entry_point;
297 /* truncated header is not handled correctly in windows <= w2k3 */
298 BOOL truncated;
300 file = CreateFileA( dll_name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE,
301 NULL, OPEN_EXISTING, 0, 0 );
302 ok( file != INVALID_HANDLE_VALUE, "%u: CreateFile error %d\n", id, GetLastError() );
303 file_size = GetFileSize( file, NULL );
305 status = pNtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
306 NULL, NULL, PAGE_READONLY, SEC_IMAGE, file );
307 ok( !status, "%u: NtCreateSection failed err %x\n", id, status );
308 if (status)
310 CloseHandle( file );
311 return FALSE;
313 status = pNtQuerySection( mapping, SectionImageInformation, &image, sizeof(image), &info_size );
314 ok( !status, "%u: NtQuerySection failed err %x\n", id, status );
315 ok( info_size == sizeof(image), "%u: NtQuerySection wrong size %lu\n", id, info_size );
316 if (nt_header->OptionalHeader.Magic == (is_win64 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC
317 : IMAGE_NT_OPTIONAL_HDR32_MAGIC))
319 max_stack = nt_header->OptionalHeader.SizeOfStackReserve;
320 commit_stack = nt_header->OptionalHeader.SizeOfStackCommit;
321 entry_point = (char *)nt_header->OptionalHeader.ImageBase + nt_header->OptionalHeader.AddressOfEntryPoint;
322 truncated = nt_header->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER);
323 if (!truncated &&
324 nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress &&
325 nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size)
326 cor_header = section_data;
328 else if (nt_header->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
330 const IMAGE_NT_HEADERS64 *nt64 = (const IMAGE_NT_HEADERS64 *)nt_header;
331 max_stack = 0x100000;
332 commit_stack = 0x10000;
333 entry_point = (void *)0x81231234;
334 truncated = nt_header->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER64);
335 if (!truncated &&
336 nt64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress &&
337 nt64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size)
338 cor_header = section_data;
340 else
342 const IMAGE_NT_HEADERS32 *nt32 = (const IMAGE_NT_HEADERS32 *)nt_header;
343 max_stack = nt32->OptionalHeader.SizeOfStackReserve;
344 commit_stack = nt32->OptionalHeader.SizeOfStackCommit;
345 entry_point = (char *)(ULONG_PTR)nt32->OptionalHeader.ImageBase + nt32->OptionalHeader.AddressOfEntryPoint;
346 truncated = nt_header->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER32);
347 if (!truncated &&
348 nt32->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress &&
349 nt32->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size)
350 cor_header = section_data;
352 ok( (char *)image.TransferAddress == (char *)entry_point ||
353 (S(U(image)).ImageDynamicallyRelocated && LOWORD(image.TransferAddress) == LOWORD(entry_point)),
354 "%u: TransferAddress wrong %p / %p (%08x)\n", id,
355 image.TransferAddress, entry_point, nt_header->OptionalHeader.AddressOfEntryPoint );
356 ok( image.ZeroBits == 0, "%u: ZeroBits wrong %08x\n", id, image.ZeroBits );
357 ok( image.MaximumStackSize == max_stack || broken(truncated),
358 "%u: MaximumStackSize wrong %lx / %lx\n", id, image.MaximumStackSize, max_stack );
359 ok( image.CommittedStackSize == commit_stack || broken(truncated),
360 "%u: CommittedStackSize wrong %lx / %lx\n", id, image.CommittedStackSize, commit_stack );
361 if (truncated)
362 ok( !image.SubSystemType || broken(truncated),
363 "%u: SubSystemType wrong %08x / 00000000\n", id, image.SubSystemType );
364 else
365 ok( image.SubSystemType == nt_header->OptionalHeader.Subsystem,
366 "%u: SubSystemType wrong %08x / %08x\n", id,
367 image.SubSystemType, nt_header->OptionalHeader.Subsystem );
368 ok( image.SubsystemVersionLow == nt_header->OptionalHeader.MinorSubsystemVersion,
369 "%u: SubsystemVersionLow wrong %04x / %04x\n", id,
370 image.SubsystemVersionLow, nt_header->OptionalHeader.MinorSubsystemVersion );
371 ok( image.SubsystemVersionHigh == nt_header->OptionalHeader.MajorSubsystemVersion,
372 "%u: SubsystemVersionHigh wrong %04x / %04x\n", id,
373 image.SubsystemVersionHigh, nt_header->OptionalHeader.MajorSubsystemVersion );
374 ok( image.ImageCharacteristics == nt_header->FileHeader.Characteristics,
375 "%u: ImageCharacteristics wrong %04x / %04x\n", id,
376 image.ImageCharacteristics, nt_header->FileHeader.Characteristics );
377 ok( image.DllCharacteristics == nt_header->OptionalHeader.DllCharacteristics || broken(truncated),
378 "%u: DllCharacteristics wrong %04x / %04x\n", id,
379 image.DllCharacteristics, nt_header->OptionalHeader.DllCharacteristics );
380 ok( image.Machine == nt_header->FileHeader.Machine, "%u: Machine wrong %04x / %04x\n", id,
381 image.Machine, nt_header->FileHeader.Machine );
382 ok( image.LoaderFlags == (cor_header != NULL), "%u: LoaderFlags wrong %08x\n", id, image.LoaderFlags );
383 ok( image.ImageFileSize == file_size || broken(!image.ImageFileSize), /* winxpsp1 */
384 "%u: ImageFileSize wrong %08x / %08x\n", id, image.ImageFileSize, file_size );
385 ok( image.CheckSum == nt_header->OptionalHeader.CheckSum, "%u: CheckSum wrong %08x / %08x\n", id,
386 image.CheckSum, nt_header->OptionalHeader.CheckSum );
388 if (nt_header->OptionalHeader.SizeOfCode || nt_header->OptionalHeader.AddressOfEntryPoint)
389 ok( image.ImageContainsCode == TRUE, "%u: ImageContainsCode wrong %u\n", id,
390 image.ImageContainsCode );
391 else if ((nt_header->OptionalHeader.SectionAlignment % page_size) ||
392 (nt_header->FileHeader.NumberOfSections == 1 &&
393 (section.Characteristics & IMAGE_SCN_MEM_EXECUTE)))
394 ok( image.ImageContainsCode == TRUE || broken(!image.ImageContainsCode), /* <= win8 */
395 "%u: ImageContainsCode wrong %u\n", id, image.ImageContainsCode );
396 else
397 ok( !image.ImageContainsCode, "%u: ImageContainsCode wrong %u\n", id, image.ImageContainsCode );
399 if (cor_header &&
400 (cor_header->Flags & COMIMAGE_FLAGS_ILONLY) &&
401 (cor_header->MajorRuntimeVersion > 2 ||
402 (cor_header->MajorRuntimeVersion == 2 && cor_header->MinorRuntimeVersion >= 5)))
404 ok( S(U(image)).ComPlusILOnly || broken(is_winxp),
405 "%u: wrong ComPlusILOnly flags %02x\n", id, U(image).ImageFlags );
406 if (nt_header->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC &&
407 !(cor_header->Flags & COMIMAGE_FLAGS_32BITREQUIRED))
408 ok( S(U(image)).ComPlusNativeReady || broken(is_winxp),
409 "%u: wrong ComPlusNativeReady flags %02x\n", id, U(image).ImageFlags );
410 else
411 ok( !S(U(image)).ComPlusNativeReady,
412 "%u: wrong ComPlusNativeReady flags %02x\n", id, U(image).ImageFlags );
414 else
416 ok( !S(U(image)).ComPlusILOnly, "%u: wrong ComPlusILOnly flags %02x\n", id, U(image).ImageFlags );
417 ok( !S(U(image)).ComPlusNativeReady, "%u: wrong ComPlusNativeReady flags %02x\n", id, U(image).ImageFlags );
419 if (!(nt_header->OptionalHeader.SectionAlignment % page_size))
420 ok( !S(U(image)).ImageMappedFlat, "%u: wrong ImageMappedFlat flags %02x\n", id, U(image).ImageFlags );
421 else
423 /* winxp doesn't support any of the loader flags */
424 if (!S(U(image)).ImageMappedFlat) is_winxp = TRUE;
425 ok( S(U(image)).ImageMappedFlat || broken(is_winxp),
426 "%u: wrong ImageMappedFlat flags %02x\n", id, U(image).ImageFlags );
428 if (!(nt_header->OptionalHeader.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
429 ok( !S(U(image)).ImageDynamicallyRelocated || broken( S(U(image)).ComPlusILOnly ), /* <= win7 */
430 "%u: wrong ImageDynamicallyRelocated flags %02x\n", id, U(image).ImageFlags );
431 else if (image.ImageContainsCode && !cor_header)
432 ok( S(U(image)).ImageDynamicallyRelocated || broken(is_winxp),
433 "%u: wrong ImageDynamicallyRelocated flags %02x\n", id, U(image).ImageFlags );
434 else
435 ok( !S(U(image)).ImageDynamicallyRelocated || broken(TRUE), /* <= win8 */
436 "%u: wrong ImageDynamicallyRelocated flags %02x\n", id, U(image).ImageFlags );
437 ok( !S(U(image)).BaseBelow4gb, "%u: wrong BaseBelow4gb flags %02x\n", id, U(image).ImageFlags );
439 /* FIXME: needs more work: */
440 /* image.GpValue */
442 map_size.QuadPart = (nt_header->OptionalHeader.SizeOfImage + page_size - 1) & ~(page_size - 1);
443 status = pNtQuerySection( mapping, SectionBasicInformation, &info, sizeof(info), NULL );
444 ok( !status, "NtQuerySection failed err %x\n", status );
445 ok( info.Size.QuadPart == map_size.QuadPart, "NtQuerySection wrong size %x%08x / %x%08x\n",
446 info.Size.u.HighPart, info.Size.u.LowPart, map_size.u.HighPart, map_size.u.LowPart );
447 CloseHandle( mapping );
449 map_size.QuadPart = (nt_header->OptionalHeader.SizeOfImage + page_size - 1) & ~(page_size - 1);
450 status = pNtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
451 NULL, &map_size, PAGE_READONLY, SEC_IMAGE, file );
452 ok( !status, "%u: NtCreateSection failed err %x\n", id, status );
453 status = pNtQuerySection( mapping, SectionBasicInformation, &info, sizeof(info), NULL );
454 ok( !status, "NtQuerySection failed err %x\n", status );
455 ok( info.Size.QuadPart == map_size.QuadPart, "NtQuerySection wrong size %x%08x / %x%08x\n",
456 info.Size.u.HighPart, info.Size.u.LowPart, map_size.u.HighPart, map_size.u.LowPart );
457 CloseHandle( mapping );
459 map_size.QuadPart++;
460 status = pNtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
461 NULL, &map_size, PAGE_READONLY, SEC_IMAGE, file );
462 ok( status == STATUS_SECTION_TOO_BIG, "%u: NtCreateSection failed err %x\n", id, status );
464 SetFilePointerEx( file, map_size, NULL, FILE_BEGIN );
465 SetEndOfFile( file );
466 status = pNtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
467 NULL, &map_size, PAGE_READONLY, SEC_IMAGE, file );
468 ok( status == STATUS_SECTION_TOO_BIG, "%u: NtCreateSection failed err %x\n", id, status );
470 map_size.QuadPart = 1;
471 status = pNtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
472 NULL, &map_size, PAGE_READONLY, SEC_IMAGE, file );
473 ok( !status, "%u: NtCreateSection failed err %x\n", id, status );
474 status = pNtQuerySection( mapping, SectionBasicInformation, &info, sizeof(info), NULL );
475 ok( !status, "NtQuerySection failed err %x\n", status );
476 ok( info.Size.QuadPart == map_size.QuadPart, "NtQuerySection wrong size %x%08x / %x%08x\n",
477 info.Size.u.HighPart, info.Size.u.LowPart, map_size.u.HighPart, map_size.u.LowPart );
478 CloseHandle( mapping );
480 CloseHandle( file );
481 return image.ImageContainsCode && (!cor_header || !(cor_header->Flags & COMIMAGE_FLAGS_ILONLY));
484 /* helper to test image section mapping */
485 static NTSTATUS map_image_section( const IMAGE_NT_HEADERS *nt_header, const IMAGE_SECTION_HEADER *sections,
486 const void *section_data, int line )
488 char dll_name[MAX_PATH];
489 LARGE_INTEGER size;
490 HANDLE file, map;
491 NTSTATUS status;
492 ULONG file_size;
493 BOOL has_code;
494 HMODULE mod;
496 file_size = create_test_dll_sections( &dos_header, nt_header, sections, section_data, dll_name );
498 file = CreateFileA(dll_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
499 ok(file != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
501 size.QuadPart = file_size;
502 status = pNtCreateSection(&map, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
503 NULL, &size, PAGE_READONLY, SEC_IMAGE, file );
504 if (!status)
506 SECTION_BASIC_INFORMATION info;
507 SIZE_T info_size = 0xdeadbeef;
508 NTSTATUS ret = pNtQuerySection( map, SectionBasicInformation, &info, sizeof(info), &info_size );
509 ok( !ret, "NtQuerySection failed err %x\n", ret );
510 ok( info_size == sizeof(info), "NtQuerySection wrong size %lu\n", info_size );
511 ok( info.Attributes == (SEC_IMAGE | SEC_FILE), "NtQuerySection wrong attr %x\n", info.Attributes );
512 ok( info.BaseAddress == NULL, "NtQuerySection wrong base %p\n", info.BaseAddress );
513 ok( info.Size.QuadPart == file_size, "NtQuerySection wrong size %x%08x / %08x\n",
514 info.Size.u.HighPart, info.Size.u.LowPart, file_size );
515 has_code = query_image_section( line, dll_name, nt_header, section_data );
516 /* test loading dll of wrong 32/64 bitness */
517 if (nt_header->OptionalHeader.Magic == (is_win64 ? IMAGE_NT_OPTIONAL_HDR32_MAGIC
518 : IMAGE_NT_OPTIONAL_HDR64_MAGIC))
520 SetLastError( 0xdeadbeef );
521 mod = LoadLibraryExA( dll_name, 0, DONT_RESOLVE_DLL_REFERENCES );
522 if (!has_code && nt_header->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
524 BOOL il_only = FALSE;
525 if (((const IMAGE_NT_HEADERS32 *)nt_header)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress)
527 const IMAGE_COR20_HEADER *cor_header = section_data;
528 il_only = (cor_header->Flags & COMIMAGE_FLAGS_ILONLY) != 0;
530 ok( mod != NULL || broken(il_only), /* <= win7 */
531 "%u: loading failed err %u\n", line, GetLastError() );
533 else
535 ok( !mod, "%u: loading succeeded\n", line );
536 ok( GetLastError() == ERROR_BAD_EXE_FORMAT, "%u: wrong error %u\n", line, GetLastError() );
538 if (mod) FreeLibrary( mod );
541 if (map) CloseHandle( map );
542 CloseHandle( file );
543 DeleteFileA( dll_name );
544 return status;
548 static void test_Loader(void)
550 static const struct test_data
552 DWORD size_of_dos_header;
553 WORD number_of_sections, size_of_optional_header;
554 DWORD section_alignment, file_alignment;
555 DWORD size_of_image, size_of_headers;
556 DWORD errors[4]; /* 0 means LoadLibrary should succeed */
557 } td[] =
559 { sizeof(dos_header),
560 1, 0, 0, 0, 0, 0,
561 { ERROR_BAD_EXE_FORMAT }
563 { sizeof(dos_header),
564 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
565 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0xe00,
566 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER),
567 { ERROR_BAD_EXE_FORMAT } /* XP doesn't like too small image size */
569 { sizeof(dos_header),
570 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
571 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
572 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER),
573 { ERROR_SUCCESS }
575 { sizeof(dos_header),
576 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
577 0x1f00,
578 0x1000,
579 { ERROR_SUCCESS }
581 { sizeof(dos_header),
582 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x200,
583 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x200,
584 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER),
585 { ERROR_SUCCESS, ERROR_INVALID_ADDRESS } /* vista is more strict */
587 { sizeof(dos_header),
588 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x1000,
589 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
590 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER),
591 { ERROR_BAD_EXE_FORMAT } /* XP doesn't like alignments */
593 { sizeof(dos_header),
594 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
595 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
596 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER),
597 { ERROR_SUCCESS }
599 { sizeof(dos_header),
600 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
601 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
602 0x200,
603 { ERROR_SUCCESS }
605 /* Mandatory are all fields up to SizeOfHeaders, everything else
606 * is really optional (at least that's true for XP).
608 { sizeof(dos_header),
609 1, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
610 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER) + 0x10,
611 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER),
612 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT, ERROR_INVALID_ADDRESS,
613 ERROR_NOACCESS }
615 { sizeof(dos_header),
616 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
617 0xd0, /* beyond of the end of file */
618 0xc0, /* beyond of the end of file */
619 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
621 { sizeof(dos_header),
622 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
623 0x1000,
625 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
627 { sizeof(dos_header),
628 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
631 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
633 #if 0 /* not power of 2 alignments need more test cases */
634 { sizeof(dos_header),
635 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x300, 0x300,
638 { ERROR_BAD_EXE_FORMAT } /* alignment is not power of 2 */
640 #endif
641 { sizeof(dos_header),
642 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 4, 4,
645 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
647 { sizeof(dos_header),
648 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 1, 1,
651 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
653 { sizeof(dos_header),
654 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
657 { ERROR_BAD_EXE_FORMAT } /* image size == 0 -> failure */
659 /* the following data mimics the PE image which upack creates */
660 { 0x10,
661 1, 0x148, 0x1000, 0x200,
662 sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
663 0x200,
664 { ERROR_SUCCESS }
666 /* Minimal PE image that XP is able to load: 92 bytes */
667 { 0x04,
668 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum),
669 0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
672 { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
674 /* Minimal PE image that Windows7 is able to load: 268 bytes */
675 { 0x04,
676 0, 0xf0, /* optional header size just forces 0xf0 bytes to be written,
677 0 or another number don't change the behaviour, what really
678 matters is file size regardless of values in the headers */
679 0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
680 0x40, /* minimal image size that Windows7 accepts */
682 { ERROR_SUCCESS }
685 int i;
686 DWORD file_size;
687 HANDLE h;
688 HMODULE hlib, hlib_as_data_file;
689 char dll_name[MAX_PATH];
690 SIZE_T size;
691 BOOL ret;
692 NTSTATUS status;
693 WORD alt_machine, orig_machine = nt_header_template.FileHeader.Machine;
694 IMAGE_NT_HEADERS nt_header;
695 IMAGE_COR20_HEADER cor_header;
697 /* prevent displaying of the "Unable to load this DLL" message box */
698 SetErrorMode(SEM_FAILCRITICALERRORS);
700 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
702 nt_header = nt_header_template;
703 nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
704 nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
706 nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
707 nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
708 nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
709 nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
711 file_size = create_test_dll( &dos_header, td[i].size_of_dos_header, &nt_header, dll_name );
713 SetLastError(0xdeadbeef);
714 hlib = LoadLibraryA(dll_name);
715 if (hlib)
717 MEMORY_BASIC_INFORMATION info;
718 void *ptr;
720 ok( td[i].errors[0] == ERROR_SUCCESS, "%d: should have failed\n", i );
722 SetLastError(0xdeadbeef);
723 size = VirtualQuery(hlib, &info, sizeof(info));
724 ok(size == sizeof(info),
725 "%d: VirtualQuery error %d\n", i, GetLastError());
726 ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
727 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
728 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
729 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "%d: got %lx != expected %x\n",
730 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
731 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
732 if (nt_header.OptionalHeader.SectionAlignment < page_size)
733 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
734 else
735 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
736 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
738 SetLastError(0xdeadbeef);
739 ptr = VirtualAlloc(hlib, page_size, MEM_COMMIT, info.Protect);
740 ok(!ptr, "%d: VirtualAlloc should fail\n", i);
741 ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
743 SetLastError(0xdeadbeef);
744 size = VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info));
745 ok(size == sizeof(info),
746 "%d: VirtualQuery error %d\n", i, GetLastError());
747 if (nt_header.OptionalHeader.SectionAlignment == page_size ||
748 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
750 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "%d: got %p != expected %p\n",
751 i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
752 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
753 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
754 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
755 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
756 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
757 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
759 else
761 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
762 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
763 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
764 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
765 ok(info.RegionSize == ALIGN_SIZE(file_size, page_size), "%d: got %lx != expected %x\n",
766 i, info.RegionSize, ALIGN_SIZE(file_size, page_size));
767 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
768 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
769 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
772 /* header: check the zeroing of alignment */
773 if (nt_header.OptionalHeader.SectionAlignment >= page_size)
775 const char *start;
777 start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
778 size = ALIGN_SIZE((ULONG_PTR)start, page_size) - (ULONG_PTR)start;
779 ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
782 if (nt_header.FileHeader.NumberOfSections)
784 SetLastError(0xdeadbeef);
785 size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
786 ok(size == sizeof(info),
787 "%d: VirtualQuery error %d\n", i, GetLastError());
788 if (nt_header.OptionalHeader.SectionAlignment < page_size)
790 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
791 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "%d: got %lx != expected %x\n",
792 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
793 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
795 else
797 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
798 ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, page_size), "%d: got %lx != expected %x\n",
799 i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, page_size));
800 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
802 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
803 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
804 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
805 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
807 if (nt_header.OptionalHeader.SectionAlignment >= page_size)
808 ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
809 else
810 ok(!memcmp((const char *)hlib + section.PointerToRawData, section_data, section.SizeOfRawData), "wrong section data\n");
812 /* check the zeroing of alignment */
813 if (nt_header.OptionalHeader.SectionAlignment >= page_size)
815 const char *start;
817 start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
818 size = ALIGN_SIZE((ULONG_PTR)start, page_size) - (ULONG_PTR)start;
819 ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
822 SetLastError(0xdeadbeef);
823 ptr = VirtualAlloc((char *)hlib + section.VirtualAddress, page_size, MEM_COMMIT, info.Protect);
824 ok(!ptr, "%d: VirtualAlloc should fail\n", i);
825 ok(GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_ADDRESS,
826 "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
829 SetLastError(0xdeadbeef);
830 hlib_as_data_file = LoadLibraryExA(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
831 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
832 ok(hlib_as_data_file == hlib, "hlib_as_file and hlib are different\n");
834 SetLastError(0xdeadbeef);
835 ret = FreeLibrary(hlib);
836 ok(ret, "FreeLibrary error %d\n", GetLastError());
838 SetLastError(0xdeadbeef);
839 hlib = GetModuleHandleA(dll_name);
840 ok(hlib != 0, "GetModuleHandle error %u\n", GetLastError());
842 SetLastError(0xdeadbeef);
843 ret = FreeLibrary(hlib_as_data_file);
844 ok(ret, "FreeLibrary error %d\n", GetLastError());
846 hlib = GetModuleHandleA(dll_name);
847 ok(!hlib, "GetModuleHandle should fail\n");
849 SetLastError(0xdeadbeef);
850 hlib_as_data_file = LoadLibraryExA(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
851 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
852 ok((ULONG_PTR)hlib_as_data_file & 1, "hlib_as_data_file is even\n");
854 hlib = GetModuleHandleA(dll_name);
855 ok(!hlib, "GetModuleHandle should fail\n");
857 SetLastError(0xdeadbeef);
858 h = CreateFileA( dll_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
859 ok( h != INVALID_HANDLE_VALUE, "open failed err %u\n", GetLastError() );
860 CloseHandle( h );
862 SetLastError(0xdeadbeef);
863 ret = FreeLibrary(hlib_as_data_file);
864 ok(ret, "FreeLibrary error %d\n", GetLastError());
866 SetLastError(0xdeadbeef);
867 hlib_as_data_file = LoadLibraryExA(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE);
868 if (!((ULONG_PTR)hlib_as_data_file & 1) || /* winxp */
869 (!hlib_as_data_file && GetLastError() == ERROR_INVALID_PARAMETER)) /* w2k3 */
871 win_skip( "LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE not supported\n" );
872 FreeLibrary(hlib_as_data_file);
874 else
876 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
878 SetLastError(0xdeadbeef);
879 h = CreateFileA( dll_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
880 todo_wine ok( h == INVALID_HANDLE_VALUE, "open succeeded\n" );
881 todo_wine ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error %u\n", GetLastError() );
882 CloseHandle( h );
884 SetLastError(0xdeadbeef);
885 ret = FreeLibrary(hlib_as_data_file);
886 ok(ret, "FreeLibrary error %d\n", GetLastError());
889 query_image_section( i, dll_name, &nt_header, NULL );
891 else
893 BOOL error_match;
894 int error_index;
896 error_match = FALSE;
897 for (error_index = 0;
898 ! error_match && error_index < sizeof(td[i].errors) / sizeof(DWORD);
899 error_index++)
901 error_match = td[i].errors[error_index] == GetLastError();
903 ok(error_match, "%d: unexpected error %d\n", i, GetLastError());
906 SetLastError(0xdeadbeef);
907 ret = DeleteFileA(dll_name);
908 ok(ret, "DeleteFile error %d\n", GetLastError());
911 nt_header = nt_header_template;
912 nt_header.FileHeader.NumberOfSections = 1;
913 nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
915 nt_header.OptionalHeader.SectionAlignment = page_size;
916 nt_header.OptionalHeader.AddressOfEntryPoint = 0x1234;
917 nt_header.OptionalHeader.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
918 nt_header.OptionalHeader.FileAlignment = page_size;
919 nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER);
920 nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + page_size;
922 section.SizeOfRawData = sizeof(section_data);
923 section.PointerToRawData = page_size;
924 section.VirtualAddress = page_size;
925 section.Misc.VirtualSize = page_size;
927 status = map_image_section( &nt_header, &section, section_data, __LINE__ );
928 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
930 nt_header.OptionalHeader.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE;
931 status = map_image_section( &nt_header, &section, section_data, __LINE__ );
932 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
934 nt_header.OptionalHeader.SizeOfCode = 0x1000;
935 status = map_image_section( &nt_header, &section, section_data, __LINE__ );
936 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
937 nt_header.OptionalHeader.SizeOfCode = 0;
938 nt_header.OptionalHeader.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
940 dos_header.e_magic = 0;
941 status = map_image_section( &nt_header, &section, section_data, __LINE__ );
942 ok( status == STATUS_INVALID_IMAGE_NOT_MZ, "NtCreateSection error %08x\n", status );
944 dos_header.e_magic = IMAGE_DOS_SIGNATURE;
945 nt_header.Signature = IMAGE_OS2_SIGNATURE;
946 status = map_image_section( &nt_header, &section, section_data, __LINE__ );
947 ok( status == STATUS_INVALID_IMAGE_NE_FORMAT, "NtCreateSection error %08x\n", status );
949 nt_header.Signature = 0xdeadbeef;
950 status = map_image_section( &nt_header, &section, section_data, __LINE__ );
951 ok( status == STATUS_INVALID_IMAGE_PROTECT, "NtCreateSection error %08x\n", status );
953 nt_header.Signature = IMAGE_NT_SIGNATURE;
954 nt_header.OptionalHeader.Magic = 0xdead;
955 status = map_image_section( &nt_header, &section, section_data, __LINE__ );
956 ok( status == STATUS_INVALID_IMAGE_FORMAT, "NtCreateSection error %08x\n", status );
958 nt_header.OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
959 nt_header.FileHeader.Machine = 0xdead;
960 status = map_image_section( &nt_header, &section, section_data, __LINE__ );
961 ok( status == STATUS_INVALID_IMAGE_FORMAT || broken(status == STATUS_SUCCESS), /* win2k */
962 "NtCreateSection error %08x\n", status );
964 nt_header.FileHeader.Machine = IMAGE_FILE_MACHINE_UNKNOWN;
965 status = map_image_section( &nt_header, &section, section_data, __LINE__ );
966 ok( status == STATUS_INVALID_IMAGE_FORMAT || broken(status == STATUS_SUCCESS), /* win2k */
967 "NtCreateSection error %08x\n", status );
969 switch (orig_machine)
971 case IMAGE_FILE_MACHINE_I386: alt_machine = IMAGE_FILE_MACHINE_ARMNT; break;
972 case IMAGE_FILE_MACHINE_AMD64: alt_machine = IMAGE_FILE_MACHINE_ARM64; break;
973 case IMAGE_FILE_MACHINE_ARMNT: alt_machine = IMAGE_FILE_MACHINE_I386; break;
974 case IMAGE_FILE_MACHINE_ARM64: alt_machine = IMAGE_FILE_MACHINE_AMD64; break;
976 nt_header.FileHeader.Machine = alt_machine;
977 status = map_image_section( &nt_header, &section, section_data, __LINE__ );
978 ok( status == STATUS_INVALID_IMAGE_FORMAT || broken(status == STATUS_SUCCESS), /* win2k */
979 "NtCreateSection error %08x\n", status );
981 switch (orig_machine)
983 case IMAGE_FILE_MACHINE_I386: alt_machine = IMAGE_FILE_MACHINE_AMD64; break;
984 case IMAGE_FILE_MACHINE_AMD64: alt_machine = IMAGE_FILE_MACHINE_I386; break;
985 case IMAGE_FILE_MACHINE_ARMNT: alt_machine = IMAGE_FILE_MACHINE_ARM64; break;
986 case IMAGE_FILE_MACHINE_ARM64: alt_machine = IMAGE_FILE_MACHINE_ARMNT; break;
988 nt_header.FileHeader.Machine = alt_machine;
989 status = map_image_section( &nt_header, &section, section_data, __LINE__ );
990 ok( status == STATUS_INVALID_IMAGE_FORMAT || broken(status == STATUS_SUCCESS), /* win2k */
991 "NtCreateSection error %08x\n", status );
993 nt_header.FileHeader.Machine = orig_machine;
994 nt_header.OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
995 nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = page_size;
996 nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = sizeof(cor_header);
997 section.SizeOfRawData = sizeof(cor_header);
999 memset( &cor_header, 0, sizeof(cor_header) );
1000 cor_header.cb = sizeof(cor_header);
1001 cor_header.MajorRuntimeVersion = 2;
1002 cor_header.MinorRuntimeVersion = 4;
1003 cor_header.Flags = COMIMAGE_FLAGS_ILONLY;
1004 U(cor_header).EntryPointToken = 0xbeef;
1005 status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1006 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1008 cor_header.MinorRuntimeVersion = 5;
1009 status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1010 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1012 cor_header.MajorRuntimeVersion = 3;
1013 cor_header.MinorRuntimeVersion = 0;
1014 status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1015 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1017 cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITREQUIRED;
1018 status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1019 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1021 cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITPREFERRED;
1022 status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1023 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1025 cor_header.Flags = 0;
1026 status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1027 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1029 nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = 1;
1030 nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = 1;
1031 status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1032 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1034 if (nt_header.OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
1036 IMAGE_NT_HEADERS64 nt64;
1038 memset( &nt64, 0, sizeof(nt64) );
1039 nt64.Signature = IMAGE_NT_SIGNATURE;
1040 nt64.FileHeader.Machine = orig_machine;
1041 nt64.FileHeader.NumberOfSections = 1;
1042 nt64.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER64);
1043 nt64.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL;
1044 nt64.OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1045 nt64.OptionalHeader.MajorLinkerVersion = 1;
1046 nt64.OptionalHeader.SizeOfCode = 0x1000;
1047 nt64.OptionalHeader.AddressOfEntryPoint = 0x1000;
1048 nt64.OptionalHeader.ImageBase = 0x10000000;
1049 nt64.OptionalHeader.SectionAlignment = 0x1000;
1050 nt64.OptionalHeader.FileAlignment = 0x1000;
1051 nt64.OptionalHeader.MajorOperatingSystemVersion = 4;
1052 nt64.OptionalHeader.MajorImageVersion = 1;
1053 nt64.OptionalHeader.MajorSubsystemVersion = 4;
1054 nt64.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt64) + sizeof(IMAGE_SECTION_HEADER);
1055 nt64.OptionalHeader.SizeOfImage = nt64.OptionalHeader.SizeOfHeaders + 0x1000;
1056 nt64.OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
1057 nt64.OptionalHeader.SizeOfStackReserve = 0x321000;
1058 nt64.OptionalHeader.SizeOfStackCommit = 0x123000;
1059 section.Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
1061 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1062 ok( status == (is_wow64 ? STATUS_INVALID_IMAGE_FORMAT : STATUS_INVALID_IMAGE_WIN_64),
1063 "NtCreateSection error %08x\n", status );
1065 switch (orig_machine)
1067 case IMAGE_FILE_MACHINE_I386: nt64.FileHeader.Machine = IMAGE_FILE_MACHINE_ARM64; break;
1068 case IMAGE_FILE_MACHINE_ARMNT: nt64.FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64; break;
1070 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1071 ok( status == (is_wow64 ? STATUS_INVALID_IMAGE_FORMAT : STATUS_INVALID_IMAGE_WIN_64),
1072 "NtCreateSection error %08x\n", status );
1074 nt64.FileHeader.Machine = alt_machine;
1075 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1076 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1077 "NtCreateSection error %08x\n", status );
1079 nt64.OptionalHeader.SizeOfCode = 0;
1080 nt64.OptionalHeader.AddressOfEntryPoint = 0x1000;
1081 section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1082 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1083 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1084 "NtCreateSection error %08x\n", status );
1086 nt64.OptionalHeader.SizeOfCode = 0;
1087 nt64.OptionalHeader.AddressOfEntryPoint = 0;
1088 section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE;
1089 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1090 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1091 "NtCreateSection error %08x\n", status );
1093 nt64.OptionalHeader.SizeOfCode = 0x1000;
1094 nt64.OptionalHeader.AddressOfEntryPoint = 0;
1095 nt64.OptionalHeader.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE;
1096 section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1097 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1098 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1099 "NtCreateSection error %08x\n", status );
1101 nt64.OptionalHeader.SizeOfCode = 0;
1102 nt64.OptionalHeader.AddressOfEntryPoint = 0;
1103 section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1104 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1105 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1106 "NtCreateSection error %08x\n", status );
1108 nt64.OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1109 nt64.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = page_size;
1110 nt64.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = sizeof(cor_header);
1111 cor_header.MajorRuntimeVersion = 2;
1112 cor_header.MinorRuntimeVersion = 4;
1113 cor_header.Flags = COMIMAGE_FLAGS_ILONLY;
1114 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1115 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1116 "NtCreateSection error %08x\n", status );
1118 nt64.OptionalHeader.SizeOfCode = 0x1000;
1119 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1120 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1121 "NtCreateSection error %08x\n", status );
1123 cor_header.MinorRuntimeVersion = 5;
1124 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1125 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1126 "NtCreateSection error %08x\n", status );
1128 cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITREQUIRED;
1129 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1130 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1131 "NtCreateSection error %08x\n", status );
1133 cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITPREFERRED;
1134 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1135 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1136 "NtCreateSection error %08x\n", status );
1138 cor_header.Flags = 0;
1139 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1140 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1141 "NtCreateSection error %08x\n", status );
1143 nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = 1;
1144 nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = 1;
1145 status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1146 ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1147 "NtCreateSection error %08x\n", status );
1149 else
1151 IMAGE_NT_HEADERS32 nt32;
1153 memset( &nt32, 0, sizeof(nt32) );
1154 nt32.Signature = IMAGE_NT_SIGNATURE;
1155 nt32.FileHeader.Machine = orig_machine;
1156 nt32.FileHeader.NumberOfSections = 1;
1157 nt32.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER32);
1158 nt32.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL;
1159 nt32.OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR32_MAGIC;
1160 nt32.OptionalHeader.MajorLinkerVersion = 1;
1161 nt32.OptionalHeader.SizeOfCode = 0x1000;
1162 nt32.OptionalHeader.AddressOfEntryPoint = 0x1000;
1163 nt32.OptionalHeader.ImageBase = 0x10000000;
1164 nt32.OptionalHeader.SectionAlignment = 0x1000;
1165 nt32.OptionalHeader.FileAlignment = 0x1000;
1166 nt32.OptionalHeader.MajorOperatingSystemVersion = 4;
1167 nt32.OptionalHeader.MajorImageVersion = 1;
1168 nt32.OptionalHeader.MajorSubsystemVersion = 4;
1169 nt32.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt32) + sizeof(IMAGE_SECTION_HEADER);
1170 nt32.OptionalHeader.SizeOfImage = nt32.OptionalHeader.SizeOfHeaders + 0x1000;
1171 nt32.OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
1172 nt32.OptionalHeader.SizeOfStackReserve = 0x321000;
1173 nt32.OptionalHeader.SizeOfStackCommit = 0x123000;
1174 section.Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
1176 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1177 ok( status == STATUS_INVALID_IMAGE_FORMAT, "NtCreateSection error %08x\n", status );
1179 switch (orig_machine)
1181 case IMAGE_FILE_MACHINE_AMD64: nt32.FileHeader.Machine = IMAGE_FILE_MACHINE_ARMNT; break;
1182 case IMAGE_FILE_MACHINE_ARM64: nt32.FileHeader.Machine = IMAGE_FILE_MACHINE_I386; break;
1184 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1185 ok( status == STATUS_INVALID_IMAGE_FORMAT || broken(!status) /* win8 */,
1186 "NtCreateSection error %08x\n", status );
1188 nt32.FileHeader.Machine = alt_machine;
1189 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1190 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1192 nt32.OptionalHeader.SizeOfCode = 0;
1193 nt32.OptionalHeader.AddressOfEntryPoint = 0x1000;
1194 section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1195 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1196 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1198 nt32.OptionalHeader.SizeOfCode = 0;
1199 nt32.OptionalHeader.AddressOfEntryPoint = 0;
1200 section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE;
1201 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1202 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1204 nt32.OptionalHeader.SizeOfCode = 0x1000;
1205 nt32.OptionalHeader.AddressOfEntryPoint = 0;
1206 nt32.OptionalHeader.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE;
1207 section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1208 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1209 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1211 nt32.OptionalHeader.SizeOfCode = 0;
1212 nt32.OptionalHeader.AddressOfEntryPoint = 0;
1213 section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1214 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1215 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1217 nt32.OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1218 nt32.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = page_size;
1219 nt32.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = sizeof(cor_header);
1220 cor_header.MajorRuntimeVersion = 2;
1221 cor_header.MinorRuntimeVersion = 4;
1222 cor_header.Flags = COMIMAGE_FLAGS_ILONLY;
1223 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1224 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1226 nt32.OptionalHeader.SizeOfCode = 0x1000;
1227 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1228 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1230 cor_header.MinorRuntimeVersion = 5;
1231 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1232 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1234 cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITREQUIRED;
1235 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1236 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1238 cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITPREFERRED;
1239 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1240 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1242 cor_header.Flags = 0;
1243 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1244 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1246 nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = 1;
1247 nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = 1;
1248 status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1249 ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1252 section.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
1255 /* Verify linking style of import descriptors */
1256 static void test_ImportDescriptors(void)
1258 HMODULE kernel32_module = NULL;
1259 PIMAGE_DOS_HEADER d_header;
1260 PIMAGE_NT_HEADERS nt_headers;
1261 DWORD import_dir_size;
1262 DWORD_PTR dir_offset;
1263 PIMAGE_IMPORT_DESCRIPTOR import_chunk;
1265 /* Load kernel32 module */
1266 kernel32_module = GetModuleHandleA("kernel32.dll");
1267 assert( kernel32_module != NULL );
1269 /* Get PE header info from module image */
1270 d_header = (PIMAGE_DOS_HEADER) kernel32_module;
1271 nt_headers = (PIMAGE_NT_HEADERS) (((char*) d_header) +
1272 d_header->e_lfanew);
1274 /* Get size of import entry directory */
1275 import_dir_size = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
1276 if (!import_dir_size)
1278 skip("Unable to continue testing due to missing import directory.\n");
1279 return;
1282 /* Get address of first import chunk */
1283 dir_offset = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
1284 import_chunk = RVAToAddr(dir_offset, kernel32_module);
1285 ok(import_chunk != 0, "Invalid import_chunk: %p\n", import_chunk);
1286 if (!import_chunk) return;
1288 /* Iterate through import descriptors and verify set name,
1289 * OriginalFirstThunk, and FirstThunk. Core Windows DLLs, such as
1290 * kernel32.dll, don't use Borland-style linking, where the table of
1291 * imported names is stored directly in FirstThunk and overwritten
1292 * by the relocation, instead of being stored in OriginalFirstThunk.
1293 * */
1294 for (; import_chunk->FirstThunk; import_chunk++)
1296 LPCSTR module_name = RVAToAddr(import_chunk->Name, kernel32_module);
1297 PIMAGE_THUNK_DATA name_table = RVAToAddr(
1298 U(*import_chunk).OriginalFirstThunk, kernel32_module);
1299 PIMAGE_THUNK_DATA iat = RVAToAddr(
1300 import_chunk->FirstThunk, kernel32_module);
1301 ok(module_name != NULL, "Imported module name should not be NULL\n");
1302 ok(name_table != NULL,
1303 "Name table for imported module %s should not be NULL\n",
1304 module_name);
1305 ok(iat != NULL, "IAT for imported module %s should not be NULL\n",
1306 module_name);
1310 static void test_image_mapping(const char *dll_name, DWORD scn_page_access, BOOL is_dll)
1312 HANDLE hfile, hmap;
1313 NTSTATUS status;
1314 LARGE_INTEGER offset;
1315 SIZE_T size;
1316 void *addr1, *addr2;
1317 MEMORY_BASIC_INFORMATION info;
1319 if (!pNtMapViewOfSection) return;
1321 SetLastError(0xdeadbeef);
1322 hfile = CreateFileA(dll_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1323 ok(hfile != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
1325 SetLastError(0xdeadbeef);
1326 hmap = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, 0);
1327 ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
1329 offset.u.LowPart = 0;
1330 offset.u.HighPart = 0;
1332 addr1 = NULL;
1333 size = 0;
1334 status = pNtMapViewOfSection(hmap, GetCurrentProcess(), &addr1, 0, 0, &offset,
1335 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
1336 ok(status == STATUS_SUCCESS, "NtMapViewOfSection error %x\n", status);
1337 ok(addr1 != 0, "mapped address should be valid\n");
1339 SetLastError(0xdeadbeef);
1340 size = VirtualQuery((char *)addr1 + section.VirtualAddress, &info, sizeof(info));
1341 ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
1342 ok(info.BaseAddress == (char *)addr1 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr1 + section.VirtualAddress);
1343 ok(info.RegionSize == page_size, "got %#lx != expected %#x\n", info.RegionSize, page_size);
1344 ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
1345 ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
1346 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
1347 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
1348 ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
1350 addr2 = NULL;
1351 size = 0;
1352 status = pNtMapViewOfSection(hmap, GetCurrentProcess(), &addr2, 0, 0, &offset,
1353 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
1354 ok(status == STATUS_IMAGE_NOT_AT_BASE, "expected STATUS_IMAGE_NOT_AT_BASE, got %x\n", status);
1355 ok(addr2 != 0, "mapped address should be valid\n");
1356 ok(addr2 != addr1, "mapped addresses should be different\n");
1358 SetLastError(0xdeadbeef);
1359 size = VirtualQuery((char *)addr2 + section.VirtualAddress, &info, sizeof(info));
1360 ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
1361 ok(info.BaseAddress == (char *)addr2 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr2 + section.VirtualAddress);
1362 ok(info.RegionSize == page_size, "got %#lx != expected %#x\n", info.RegionSize, page_size);
1363 ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
1364 ok(info.AllocationBase == addr2, "%p != %p\n", info.AllocationBase, addr2);
1365 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
1366 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
1367 ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
1369 status = pNtUnmapViewOfSection(GetCurrentProcess(), addr2);
1370 ok(status == STATUS_SUCCESS, "NtUnmapViewOfSection error %x\n", status);
1372 addr2 = MapViewOfFile(hmap, 0, 0, 0, 0);
1373 ok(addr2 != 0, "mapped address should be valid\n");
1374 ok(addr2 != addr1, "mapped addresses should be different\n");
1376 SetLastError(0xdeadbeef);
1377 size = VirtualQuery((char *)addr2 + section.VirtualAddress, &info, sizeof(info));
1378 ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
1379 ok(info.BaseAddress == (char *)addr2 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr2 + section.VirtualAddress);
1380 ok(info.RegionSize == page_size, "got %#lx != expected %#x\n", info.RegionSize, page_size);
1381 ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
1382 ok(info.AllocationBase == addr2, "%p != %p\n", info.AllocationBase, addr2);
1383 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
1384 ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
1385 ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
1387 UnmapViewOfFile(addr2);
1389 SetLastError(0xdeadbeef);
1390 addr2 = LoadLibraryA(dll_name);
1391 if (is_dll)
1393 ok(!addr2, "LoadLibrary should fail, is_dll %d\n", is_dll);
1394 ok(GetLastError() == ERROR_INVALID_ADDRESS, "expected ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1396 else
1398 BOOL ret;
1399 ok(addr2 != 0, "LoadLibrary error %d, is_dll %d\n", GetLastError(), is_dll);
1400 ok(addr2 != addr1, "mapped addresses should be different\n");
1402 SetLastError(0xdeadbeef);
1403 ret = FreeLibrary(addr2);
1404 ok(ret, "FreeLibrary error %d\n", GetLastError());
1407 status = pNtUnmapViewOfSection(GetCurrentProcess(), addr1);
1408 ok(status == STATUS_SUCCESS, "NtUnmapViewOfSection error %x\n", status);
1410 CloseHandle(hmap);
1411 CloseHandle(hfile);
1414 static BOOL is_mem_writable(DWORD prot)
1416 switch (prot & 0xff)
1418 case PAGE_READWRITE:
1419 case PAGE_WRITECOPY:
1420 case PAGE_EXECUTE_READWRITE:
1421 case PAGE_EXECUTE_WRITECOPY:
1422 return TRUE;
1424 default:
1425 return FALSE;
1429 static void test_VirtualProtect(void *base, void *section)
1431 static const struct test_data
1433 DWORD prot_set, prot_get;
1434 } td[] =
1436 { 0, 0 }, /* 0x00 */
1437 { PAGE_NOACCESS, PAGE_NOACCESS }, /* 0x01 */
1438 { PAGE_READONLY, PAGE_READONLY }, /* 0x02 */
1439 { PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x03 */
1440 { PAGE_READWRITE, PAGE_WRITECOPY }, /* 0x04 */
1441 { PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x05 */
1442 { PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x06 */
1443 { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x07 */
1444 { PAGE_WRITECOPY, PAGE_WRITECOPY }, /* 0x08 */
1445 { PAGE_WRITECOPY | PAGE_NOACCESS, 0 }, /* 0x09 */
1446 { PAGE_WRITECOPY | PAGE_READONLY, 0 }, /* 0x0a */
1447 { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, 0 }, /* 0x0b */
1448 { PAGE_WRITECOPY | PAGE_READWRITE, 0 }, /* 0x0c */
1449 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x0d */
1450 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x0e */
1451 { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x0f */
1453 { PAGE_EXECUTE, PAGE_EXECUTE }, /* 0x10 */
1454 { PAGE_EXECUTE_READ, PAGE_EXECUTE_READ }, /* 0x20 */
1455 { PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x30 */
1456 { PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY }, /* 0x40 */
1457 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0x50 */
1458 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0x60 */
1459 { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x70 */
1460 { PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_WRITECOPY }, /* 0x80 */
1461 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, 0 }, /* 0x90 */
1462 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, 0 }, /* 0xa0 */
1463 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0xb0 */
1464 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, 0 }, /* 0xc0 */
1465 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0xd0 */
1466 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0xe0 */
1467 { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 } /* 0xf0 */
1469 DWORD ret, orig_prot, old_prot, rw_prot, exec_prot, i, j;
1470 MEMORY_BASIC_INFORMATION info;
1472 SetLastError(0xdeadbeef);
1473 ret = VirtualProtect(section, page_size, PAGE_NOACCESS, &old_prot);
1474 ok(ret, "VirtualProtect error %d\n", GetLastError());
1476 orig_prot = old_prot;
1478 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1480 SetLastError(0xdeadbeef);
1481 ret = VirtualQuery(section, &info, sizeof(info));
1482 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1483 ok(info.BaseAddress == section, "%d: got %p != expected %p\n", i, info.BaseAddress, section);
1484 ok(info.RegionSize == page_size, "%d: got %#lx != expected %#x\n", i, info.RegionSize, page_size);
1485 ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
1486 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1487 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
1488 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1489 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
1491 old_prot = 0xdeadbeef;
1492 SetLastError(0xdeadbeef);
1493 ret = VirtualProtect(section, page_size, td[i].prot_set, &old_prot);
1494 if (td[i].prot_get)
1496 ok(ret, "%d: VirtualProtect error %d, requested prot %#x\n", i, GetLastError(), td[i].prot_set);
1497 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1499 SetLastError(0xdeadbeef);
1500 ret = VirtualQuery(section, &info, sizeof(info));
1501 ok(ret, "VirtualQuery failed %d\n", GetLastError());
1502 ok(info.BaseAddress == section, "%d: got %p != expected %p\n", i, info.BaseAddress, section);
1503 ok(info.RegionSize == page_size, "%d: got %#lx != expected %#x\n", i, info.RegionSize, page_size);
1504 ok(info.Protect == td[i].prot_get, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_get);
1505 ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1506 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
1507 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1508 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
1510 else
1512 ok(!ret, "%d: VirtualProtect should fail\n", i);
1513 ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1516 old_prot = 0xdeadbeef;
1517 SetLastError(0xdeadbeef);
1518 ret = VirtualProtect(section, page_size, PAGE_NOACCESS, &old_prot);
1519 ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
1520 if (td[i].prot_get)
1521 ok(old_prot == td[i].prot_get, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_get);
1522 else
1523 ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1526 exec_prot = 0;
1528 for (i = 0; i <= 4; i++)
1530 rw_prot = 0;
1532 for (j = 0; j <= 4; j++)
1534 DWORD prot = exec_prot | rw_prot;
1536 SetLastError(0xdeadbeef);
1537 ret = VirtualProtect(section, page_size, prot, &old_prot);
1538 if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
1540 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
1541 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1543 else
1544 ok(ret, "VirtualProtect(%02x) error %d\n", prot, GetLastError());
1546 rw_prot = 1 << j;
1549 exec_prot = 1 << (i + 4);
1552 SetLastError(0xdeadbeef);
1553 ret = VirtualProtect(section, page_size, orig_prot, &old_prot);
1554 ok(ret, "VirtualProtect error %d\n", GetLastError());
1557 static void test_section_access(void)
1559 static const struct test_data
1561 DWORD scn_file_access, scn_page_access, scn_page_access_after_write;
1562 } td[] =
1564 { 0, PAGE_NOACCESS, 0 },
1565 { IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
1566 { IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1567 { IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
1568 { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1569 { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ },
1570 { IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
1571 { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
1573 { IMAGE_SCN_CNT_INITIALIZED_DATA, PAGE_NOACCESS, 0 },
1574 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
1575 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1576 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
1577 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1578 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ, 0 },
1579 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
1580 { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
1582 { IMAGE_SCN_CNT_UNINITIALIZED_DATA, PAGE_NOACCESS, 0 },
1583 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
1584 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1585 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
1586 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1587 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ, 0 },
1588 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
1589 { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE }
1591 char buf[256];
1592 int i;
1593 DWORD dummy, file_align;
1594 HANDLE hfile;
1595 HMODULE hlib;
1596 char temp_path[MAX_PATH];
1597 char dll_name[MAX_PATH];
1598 SIZE_T size;
1599 MEMORY_BASIC_INFORMATION info;
1600 STARTUPINFOA sti;
1601 PROCESS_INFORMATION pi;
1602 DWORD ret;
1604 /* prevent displaying of the "Unable to load this DLL" message box */
1605 SetErrorMode(SEM_FAILCRITICALERRORS);
1607 GetTempPathA(MAX_PATH, temp_path);
1609 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1611 IMAGE_NT_HEADERS nt_header;
1613 GetTempFileNameA(temp_path, "ldr", 0, dll_name);
1615 /*trace("creating %s\n", dll_name);*/
1616 hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
1617 if (hfile == INVALID_HANDLE_VALUE)
1619 ok(0, "could not create %s\n", dll_name);
1620 return;
1623 SetLastError(0xdeadbeef);
1624 ret = WriteFile(hfile, &dos_header, sizeof(dos_header), &dummy, NULL);
1625 ok(ret, "WriteFile error %d\n", GetLastError());
1627 nt_header = nt_header_template;
1628 nt_header.FileHeader.NumberOfSections = 1;
1629 nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
1630 nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL | IMAGE_FILE_RELOCS_STRIPPED;
1632 nt_header.OptionalHeader.SectionAlignment = page_size;
1633 nt_header.OptionalHeader.FileAlignment = 0x200;
1634 nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + page_size;
1635 nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER);
1636 SetLastError(0xdeadbeef);
1637 ret = WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
1638 ok(ret, "WriteFile error %d\n", GetLastError());
1639 SetLastError(0xdeadbeef);
1640 ret = WriteFile(hfile, &nt_header.OptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER), &dummy, NULL);
1641 ok(ret, "WriteFile error %d\n", GetLastError());
1643 section.SizeOfRawData = sizeof(section_data);
1644 section.PointerToRawData = nt_header.OptionalHeader.FileAlignment;
1645 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
1646 section.Misc.VirtualSize = section.SizeOfRawData;
1647 section.Characteristics = td[i].scn_file_access;
1648 SetLastError(0xdeadbeef);
1649 ret = WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
1650 ok(ret, "WriteFile error %d\n", GetLastError());
1652 file_align = nt_header.OptionalHeader.FileAlignment - nt_header.OptionalHeader.SizeOfHeaders;
1653 assert(file_align < sizeof(filler));
1654 SetLastError(0xdeadbeef);
1655 ret = WriteFile(hfile, filler, file_align, &dummy, NULL);
1656 ok(ret, "WriteFile error %d\n", GetLastError());
1658 /* section data */
1659 SetLastError(0xdeadbeef);
1660 ret = WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL);
1661 ok(ret, "WriteFile error %d\n", GetLastError());
1663 CloseHandle(hfile);
1665 SetLastError(0xdeadbeef);
1666 hlib = LoadLibraryA(dll_name);
1667 ok(hlib != 0, "LoadLibrary error %d\n", GetLastError());
1669 SetLastError(0xdeadbeef);
1670 size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
1671 ok(size == sizeof(info),
1672 "%d: VirtualQuery error %d\n", i, GetLastError());
1673 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
1674 ok(info.RegionSize == page_size, "%d: got %#lx != expected %#x\n", i, info.RegionSize, page_size);
1675 ok(info.Protect == td[i].scn_page_access, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].scn_page_access);
1676 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
1677 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
1678 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1679 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
1680 if (info.Protect != PAGE_NOACCESS)
1681 ok(!memcmp((const char *)info.BaseAddress, section_data, section.SizeOfRawData), "wrong section data\n");
1683 test_VirtualProtect(hlib, (char *)hlib + section.VirtualAddress);
1685 /* Windows changes the WRITECOPY to WRITE protection on an image section write (for a changed page only) */
1686 if (is_mem_writable(info.Protect))
1688 char *p = info.BaseAddress;
1689 *p = 0xfe;
1690 SetLastError(0xdeadbeef);
1691 size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
1692 ok(size == sizeof(info), "%d: VirtualQuery error %d\n", i, GetLastError());
1693 /* FIXME: remove the condition below once Wine is fixed */
1694 todo_wine_if (info.Protect == PAGE_WRITECOPY || info.Protect == PAGE_EXECUTE_WRITECOPY)
1695 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);
1698 SetLastError(0xdeadbeef);
1699 ret = FreeLibrary(hlib);
1700 ok(ret, "FreeLibrary error %d\n", GetLastError());
1702 test_image_mapping(dll_name, td[i].scn_page_access, TRUE);
1704 /* reset IMAGE_FILE_DLL otherwise CreateProcess fails */
1705 nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_RELOCS_STRIPPED;
1706 SetLastError(0xdeadbeef);
1707 hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1708 /* LoadLibrary called on an already memory-mapped file in
1709 * test_image_mapping() above leads to a file handle leak
1710 * under nt4, and inability to overwrite and delete the file
1711 * due to sharing violation error. Ignore it and skip the test,
1712 * but leave a not deletable temporary file.
1714 ok(hfile != INVALID_HANDLE_VALUE || broken(hfile == INVALID_HANDLE_VALUE) /* nt4 */,
1715 "CreateFile error %d\n", GetLastError());
1716 if (hfile == INVALID_HANDLE_VALUE) goto nt4_is_broken;
1717 SetFilePointer(hfile, sizeof(dos_header), NULL, FILE_BEGIN);
1718 SetLastError(0xdeadbeef);
1719 ret = WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
1720 ok(ret, "WriteFile error %d\n", GetLastError());
1721 CloseHandle(hfile);
1723 memset(&sti, 0, sizeof(sti));
1724 sti.cb = sizeof(sti);
1725 SetLastError(0xdeadbeef);
1726 ret = CreateProcessA(dll_name, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &sti, &pi);
1727 ok(ret, "CreateProcess() error %d\n", GetLastError());
1729 SetLastError(0xdeadbeef);
1730 size = VirtualQueryEx(pi.hProcess, (char *)hlib + section.VirtualAddress, &info, sizeof(info));
1731 ok(size == sizeof(info),
1732 "%d: VirtualQuery error %d\n", i, GetLastError());
1733 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
1734 ok(info.RegionSize == page_size, "%d: got %#lx != expected %#x\n", i, info.RegionSize, page_size);
1735 ok(info.Protect == td[i].scn_page_access, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].scn_page_access);
1736 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
1737 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
1738 ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1739 ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
1740 if (info.Protect != PAGE_NOACCESS)
1742 SetLastError(0xdeadbeef);
1743 ret = ReadProcessMemory(pi.hProcess, info.BaseAddress, buf, section.SizeOfRawData, NULL);
1744 ok(ret, "ReadProcessMemory() error %d\n", GetLastError());
1745 ok(!memcmp(buf, section_data, section.SizeOfRawData), "wrong section data\n");
1748 SetLastError(0xdeadbeef);
1749 ret = TerminateProcess(pi.hProcess, 0);
1750 ok(ret, "TerminateProcess() error %d\n", GetLastError());
1751 ret = WaitForSingleObject(pi.hProcess, 3000);
1752 ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
1754 CloseHandle(pi.hThread);
1755 CloseHandle(pi.hProcess);
1757 test_image_mapping(dll_name, td[i].scn_page_access, FALSE);
1759 nt4_is_broken:
1760 SetLastError(0xdeadbeef);
1761 ret = DeleteFileA(dll_name);
1762 ok(ret || broken(!ret) /* nt4 */, "DeleteFile error %d\n", GetLastError());
1766 static void test_import_resolution(void)
1768 char temp_path[MAX_PATH];
1769 char dll_name[MAX_PATH];
1770 DWORD dummy;
1771 void *expect;
1772 char *str;
1773 HANDLE hfile;
1774 HMODULE mod, mod2;
1775 struct imports
1777 IMAGE_IMPORT_DESCRIPTOR descr[2];
1778 IMAGE_THUNK_DATA original_thunks[2];
1779 IMAGE_THUNK_DATA thunks[2];
1780 char module[16];
1781 struct { WORD hint; char name[32]; } function;
1782 IMAGE_TLS_DIRECTORY tls;
1783 char tls_data[16];
1784 SHORT tls_index;
1785 } data, *ptr;
1786 IMAGE_NT_HEADERS nt;
1787 IMAGE_SECTION_HEADER section;
1788 int test;
1790 for (test = 0; test < 3; test++)
1792 #define DATA_RVA(ptr) (page_size + ((char *)(ptr) - (char *)&data))
1793 nt = nt_header_template;
1794 nt.FileHeader.NumberOfSections = 1;
1795 nt.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
1796 nt.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_32BIT_MACHINE | IMAGE_FILE_RELOCS_STRIPPED;
1797 if (test != 2) nt.FileHeader.Characteristics |= IMAGE_FILE_DLL;
1798 nt.OptionalHeader.SectionAlignment = page_size;
1799 nt.OptionalHeader.FileAlignment = 0x200;
1800 nt.OptionalHeader.ImageBase = 0x12340000;
1801 nt.OptionalHeader.SizeOfImage = 2 * page_size;
1802 nt.OptionalHeader.SizeOfHeaders = nt.OptionalHeader.FileAlignment;
1803 nt.OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1804 memset( nt.OptionalHeader.DataDirectory, 0, sizeof(nt.OptionalHeader.DataDirectory) );
1805 nt.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size = sizeof(data.descr);
1806 nt.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress = DATA_RVA(data.descr);
1807 nt.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].Size = sizeof(data.tls);
1808 nt.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress = DATA_RVA(&data.tls);
1810 memset( &data, 0, sizeof(data) );
1811 U(data.descr[0]).OriginalFirstThunk = DATA_RVA( data.original_thunks );
1812 data.descr[0].FirstThunk = DATA_RVA( data.thunks );
1813 data.descr[0].Name = DATA_RVA( data.module );
1814 strcpy( data.module, "kernel32.dll" );
1815 strcpy( data.function.name, "CreateEventA" );
1816 data.original_thunks[0].u1.AddressOfData = DATA_RVA( &data.function );
1817 data.thunks[0].u1.AddressOfData = 0xdeadbeef;
1819 data.tls.StartAddressOfRawData = nt.OptionalHeader.ImageBase + DATA_RVA( data.tls_data );
1820 data.tls.EndAddressOfRawData = data.tls.StartAddressOfRawData + sizeof(data.tls_data);
1821 data.tls.AddressOfIndex = nt.OptionalHeader.ImageBase + DATA_RVA( &data.tls_index );
1822 strcpy( data.tls_data, "hello world" );
1823 data.tls_index = 9999;
1825 GetTempPathA(MAX_PATH, temp_path);
1826 GetTempFileNameA(temp_path, "ldr", 0, dll_name);
1828 hfile = CreateFileA(dll_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0);
1829 ok( hfile != INVALID_HANDLE_VALUE, "creation failed\n" );
1831 memset( &section, 0, sizeof(section) );
1832 memcpy( section.Name, ".text", sizeof(".text") );
1833 section.PointerToRawData = nt.OptionalHeader.FileAlignment;
1834 section.VirtualAddress = nt.OptionalHeader.SectionAlignment;
1835 section.Misc.VirtualSize = sizeof(data);
1836 section.SizeOfRawData = sizeof(data);
1837 section.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
1839 WriteFile(hfile, &dos_header, sizeof(dos_header), &dummy, NULL);
1840 WriteFile(hfile, &nt, sizeof(nt), &dummy, NULL);
1841 WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
1843 SetFilePointer( hfile, section.PointerToRawData, NULL, SEEK_SET );
1844 WriteFile(hfile, &data, sizeof(data), &dummy, NULL);
1846 CloseHandle( hfile );
1848 switch (test)
1850 case 0: /* normal load */
1851 mod = LoadLibraryA( dll_name );
1852 ok( mod != NULL, "failed to load err %u\n", GetLastError() );
1853 if (!mod) break;
1854 ptr = (struct imports *)((char *)mod + page_size);
1855 expect = GetProcAddress( GetModuleHandleA( data.module ), data.function.name );
1856 ok( (void *)ptr->thunks[0].u1.Function == expect, "thunk %p instead of %p for %s.%s\n",
1857 (void *)ptr->thunks[0].u1.Function, expect, data.module, data.function.name );
1858 ok( ptr->tls_index < 32 || broken(ptr->tls_index == 9999), /* before vista */
1859 "wrong tls index %d\n", ptr->tls_index );
1860 if (ptr->tls_index != 9999)
1862 str = ((char **)NtCurrentTeb()->ThreadLocalStoragePointer)[ptr->tls_index];
1863 ok( !strcmp( str, "hello world" ), "wrong tls data '%s' at %p\n", str, str );
1865 FreeLibrary( mod );
1866 break;
1867 case 1: /* load with DONT_RESOLVE_DLL_REFERENCES doesn't resolve imports */
1868 mod = LoadLibraryExA( dll_name, 0, DONT_RESOLVE_DLL_REFERENCES );
1869 ok( mod != NULL, "failed to load err %u\n", GetLastError() );
1870 if (!mod) break;
1871 ptr = (struct imports *)((char *)mod + page_size);
1872 ok( ptr->thunks[0].u1.Function == 0xdeadbeef, "thunk resolved to %p for %s.%s\n",
1873 (void *)ptr->thunks[0].u1.Function, data.module, data.function.name );
1874 ok( ptr->tls_index == 9999, "wrong tls index %d\n", ptr->tls_index );
1876 mod2 = LoadLibraryA( dll_name );
1877 ok( mod2 == mod, "loaded twice %p / %p\n", mod, mod2 );
1878 ok( ptr->thunks[0].u1.Function == 0xdeadbeef, "thunk resolved to %p for %s.%s\n",
1879 (void *)ptr->thunks[0].u1.Function, data.module, data.function.name );
1880 ok( ptr->tls_index == 9999, "wrong tls index %d\n", ptr->tls_index );
1881 FreeLibrary( mod2 );
1882 FreeLibrary( mod );
1883 break;
1884 case 2: /* load without IMAGE_FILE_DLL doesn't resolve imports */
1885 mod = LoadLibraryA( dll_name );
1886 ok( mod != NULL, "failed to load err %u\n", GetLastError() );
1887 if (!mod) break;
1888 ptr = (struct imports *)((char *)mod + page_size);
1889 ok( ptr->thunks[0].u1.Function == 0xdeadbeef, "thunk resolved to %p for %s.%s\n",
1890 (void *)ptr->thunks[0].u1.Function, data.module, data.function.name );
1891 ok( ptr->tls_index == 9999, "wrong tls index %d\n", ptr->tls_index );
1892 FreeLibrary( mod );
1893 break;
1895 DeleteFileA( dll_name );
1896 #undef DATA_RVA
1900 #define MAX_COUNT 10
1901 static HANDLE attached_thread[MAX_COUNT];
1902 static DWORD attached_thread_count;
1903 HANDLE stop_event, event, mutex, semaphore, loader_lock_event, peb_lock_event, heap_lock_event, ack_event;
1904 static int test_dll_phase, inside_loader_lock, inside_peb_lock, inside_heap_lock;
1905 static LONG fls_callback_count;
1907 static DWORD WINAPI mutex_thread_proc(void *param)
1909 HANDLE wait_list[4];
1910 DWORD ret;
1912 ret = WaitForSingleObject(mutex, 0);
1913 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1915 SetEvent(param);
1917 wait_list[0] = stop_event;
1918 wait_list[1] = loader_lock_event;
1919 wait_list[2] = peb_lock_event;
1920 wait_list[3] = heap_lock_event;
1922 trace("%04x: mutex_thread_proc: starting\n", GetCurrentThreadId());
1923 while (1)
1925 ret = WaitForMultipleObjects(sizeof(wait_list)/sizeof(wait_list[0]), wait_list, FALSE, 50);
1926 if (ret == WAIT_OBJECT_0) break;
1927 else if (ret == WAIT_OBJECT_0 + 1)
1929 ULONG_PTR loader_lock_magic;
1930 trace("%04x: mutex_thread_proc: Entering loader lock\n", GetCurrentThreadId());
1931 ret = pLdrLockLoaderLock(0, NULL, &loader_lock_magic);
1932 ok(!ret, "LdrLockLoaderLock error %#x\n", ret);
1933 inside_loader_lock++;
1934 SetEvent(ack_event);
1936 else if (ret == WAIT_OBJECT_0 + 2)
1938 trace("%04x: mutex_thread_proc: Entering PEB lock\n", GetCurrentThreadId());
1939 pRtlAcquirePebLock();
1940 inside_peb_lock++;
1941 SetEvent(ack_event);
1943 else if (ret == WAIT_OBJECT_0 + 3)
1945 trace("%04x: mutex_thread_proc: Entering heap lock\n", GetCurrentThreadId());
1946 HeapLock(GetProcessHeap());
1947 inside_heap_lock++;
1948 SetEvent(ack_event);
1952 trace("%04x: mutex_thread_proc: exiting\n", GetCurrentThreadId());
1953 return 196;
1956 static DWORD WINAPI semaphore_thread_proc(void *param)
1958 DWORD ret;
1960 ret = WaitForSingleObject(semaphore, 0);
1961 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
1963 SetEvent(param);
1965 while (1)
1967 if (winetest_debug > 1)
1968 trace("%04x: semaphore_thread_proc: still alive\n", GetCurrentThreadId());
1969 if (WaitForSingleObject(stop_event, 50) != WAIT_TIMEOUT) break;
1972 trace("%04x: semaphore_thread_proc: exiting\n", GetCurrentThreadId());
1973 return 196;
1976 static DWORD WINAPI noop_thread_proc(void *param)
1978 if (param)
1980 LONG *noop_thread_started = param;
1981 InterlockedIncrement(noop_thread_started);
1984 trace("%04x: noop_thread_proc: exiting\n", GetCurrentThreadId());
1985 return 195;
1988 static VOID WINAPI fls_callback(PVOID lpFlsData)
1990 ok(lpFlsData == (void*) 0x31415, "lpFlsData is %p, expected %p\n", lpFlsData, (void*) 0x31415);
1991 InterlockedIncrement(&fls_callback_count);
1994 static BOOL WINAPI dll_entry_point(HINSTANCE hinst, DWORD reason, LPVOID param)
1996 static LONG noop_thread_started;
1997 static DWORD fls_index = FLS_OUT_OF_INDEXES;
1998 static int fls_count = 0;
1999 static int thread_detach_count = 0;
2000 DWORD ret;
2002 ok(!inside_loader_lock, "inside_loader_lock should not be set\n");
2003 ok(!inside_peb_lock, "inside_peb_lock should not be set\n");
2005 switch (reason)
2007 case DLL_PROCESS_ATTACH:
2008 trace("dll: %p, DLL_PROCESS_ATTACH, %p\n", hinst, param);
2010 ret = pRtlDllShutdownInProgress();
2011 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2013 /* Set up the FLS slot, if FLS is available */
2014 if (pFlsGetValue)
2016 void* value;
2017 BOOL bret;
2018 ret = pFlsAlloc(&fls_callback);
2019 ok(ret != FLS_OUT_OF_INDEXES, "FlsAlloc returned %d\n", ret);
2020 fls_index = ret;
2021 SetLastError(0xdeadbeef);
2022 value = pFlsGetValue(fls_index);
2023 ok(!value, "FlsGetValue returned %p, expected NULL\n", value);
2024 ok(GetLastError() == ERROR_SUCCESS, "FlsGetValue failed with error %u\n", GetLastError());
2025 bret = pFlsSetValue(fls_index, (void*) 0x31415);
2026 ok(bret, "FlsSetValue failed\n");
2027 fls_count++;
2030 break;
2031 case DLL_PROCESS_DETACH:
2033 DWORD code, expected_code, i;
2034 HANDLE handle, process;
2035 void *addr;
2036 SIZE_T size;
2037 LARGE_INTEGER offset;
2038 DEBUG_EVENT de;
2040 trace("dll: %p, DLL_PROCESS_DETACH, %p\n", hinst, param);
2042 if (test_dll_phase == 4 || test_dll_phase == 5)
2044 ok(0, "dll_entry_point(DLL_PROCESS_DETACH) should not be called\n");
2045 break;
2048 /* The process should already deadlock at this point */
2049 if (test_dll_phase == 6)
2051 /* In reality, code below never gets executed, probably some other
2052 * code tries to access process heap and deadlocks earlier, even XP
2053 * doesn't call the DLL entry point on process detach either.
2055 HeapLock(GetProcessHeap());
2056 ok(0, "dll_entry_point: process should already deadlock\n");
2057 break;
2060 if (test_dll_phase == 0 || test_dll_phase == 1 || test_dll_phase == 3)
2061 ok(param != NULL, "dll: param %p\n", param);
2062 else
2063 ok(!param, "dll: param %p\n", param);
2065 if (test_dll_phase == 0 || test_dll_phase == 1) expected_code = 195;
2066 else if (test_dll_phase == 3) expected_code = 196;
2067 else expected_code = STILL_ACTIVE;
2069 if (test_dll_phase == 3)
2071 ret = pRtlDllShutdownInProgress();
2072 ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
2074 else
2076 ret = pRtlDllShutdownInProgress();
2078 /* FIXME: remove once Wine is fixed */
2079 todo_wine_if (!(expected_code == STILL_ACTIVE || expected_code == 196))
2080 ok(!ret || broken(ret) /* before Vista */, "RtlDllShutdownInProgress returned %d\n", ret);
2083 /* In the case that the process is terminating, FLS slots should still be accessible, but
2084 * the callback should be already run for this thread and the contents already NULL.
2085 * Note that this is broken for Win2k3, which runs the callbacks *after* the DLL entry
2086 * point has already run.
2088 if (param && pFlsGetValue)
2090 void* value;
2091 SetLastError(0xdeadbeef);
2092 value = pFlsGetValue(fls_index);
2093 todo_wine
2095 ok(broken(value == (void*) 0x31415) || /* Win2k3 */
2096 value == NULL, "FlsGetValue returned %p, expected NULL\n", value);
2098 ok(GetLastError() == ERROR_SUCCESS, "FlsGetValue failed with error %u\n", GetLastError());
2099 todo_wine
2101 ok(broken(fls_callback_count == thread_detach_count) || /* Win2k3 */
2102 fls_callback_count == thread_detach_count + 1,
2103 "wrong FLS callback count %d, expected %d\n", fls_callback_count, thread_detach_count + 1);
2106 if (pFlsFree)
2108 BOOL ret;
2109 /* Call FlsFree now and run the remaining callbacks from uncleanly terminated threads */
2110 ret = pFlsFree(fls_index);
2111 ok(ret, "FlsFree failed with error %u\n", GetLastError());
2112 fls_index = FLS_OUT_OF_INDEXES;
2113 todo_wine
2115 ok(fls_callback_count == fls_count,
2116 "wrong FLS callback count %d, expected %d\n", fls_callback_count, fls_count);
2120 ok(attached_thread_count >= 2, "attached thread count should be >= 2\n");
2122 for (i = 0; i < attached_thread_count; i++)
2124 /* Calling GetExitCodeThread() without waiting for thread termination
2125 * leads to different results due to a race condition.
2127 if (expected_code != STILL_ACTIVE)
2129 ret = WaitForSingleObject(attached_thread[i], 1000);
2130 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2132 ret = GetExitCodeThread(attached_thread[i], &code);
2133 trace("dll: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
2134 ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
2135 ok(code == expected_code, "expected thread exit code %u, got %u\n", expected_code, code);
2138 ret = WaitForSingleObject(event, 0);
2139 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2141 ret = WaitForSingleObject(mutex, 0);
2142 if (expected_code == STILL_ACTIVE)
2143 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2144 else
2145 ok(ret == WAIT_ABANDONED, "expected WAIT_ABANDONED, got %#x\n", ret);
2147 /* semaphore is not abandoned on thread termination */
2148 ret = WaitForSingleObject(semaphore, 0);
2149 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2151 if (expected_code == STILL_ACTIVE)
2153 ret = WaitForSingleObject(attached_thread[0], 0);
2154 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2155 ret = WaitForSingleObject(attached_thread[1], 0);
2156 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2158 else
2160 ret = WaitForSingleObject(attached_thread[0], 0);
2161 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2162 ret = WaitForSingleObject(attached_thread[1], 0);
2163 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2166 /* win7 doesn't allow creating a thread during process shutdown but
2167 * earlier Windows versions allow it.
2169 noop_thread_started = 0;
2170 SetLastError(0xdeadbeef);
2171 handle = CreateThread(NULL, 0, noop_thread_proc, &noop_thread_started, 0, &ret);
2172 if (param)
2174 ok(!handle || broken(handle != 0) /* before win7 */, "CreateThread should fail\n");
2175 if (!handle)
2176 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2177 else
2179 ret = WaitForSingleObject(handle, 1000);
2180 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2181 CloseHandle(handle);
2184 else
2186 ok(handle != 0, "CreateThread error %d\n", GetLastError());
2187 ret = WaitForSingleObject(handle, 1000);
2188 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2189 ok(!noop_thread_started || broken(noop_thread_started) /* XP64 */, "thread shouldn't start yet\n");
2190 CloseHandle(handle);
2193 SetLastError(0xdeadbeef);
2194 process = OpenProcess(PROCESS_ALL_ACCESS_NT4, FALSE, GetCurrentProcessId());
2195 ok(process != NULL, "OpenProcess error %d\n", GetLastError());
2197 noop_thread_started = 0;
2198 SetLastError(0xdeadbeef);
2199 handle = CreateRemoteThread(process, NULL, 0, noop_thread_proc, &noop_thread_started, 0, &ret);
2200 if (param)
2202 ok(!handle || broken(handle != 0) /* before win7 */, "CreateRemoteThread should fail\n");
2203 if (!handle)
2204 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2205 else
2207 ret = WaitForSingleObject(handle, 1000);
2208 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2209 CloseHandle(handle);
2212 else
2214 ok(handle != 0, "CreateRemoteThread error %d\n", GetLastError());
2215 ret = WaitForSingleObject(handle, 1000);
2216 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2217 ok(!noop_thread_started || broken(noop_thread_started) /* XP64 */, "thread shouldn't start yet\n");
2218 CloseHandle(handle);
2221 SetLastError(0xdeadbeef);
2222 handle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, NULL);
2223 ok(handle != 0, "CreateFileMapping error %d\n", GetLastError());
2225 offset.u.LowPart = 0;
2226 offset.u.HighPart = 0;
2227 addr = NULL;
2228 size = 0;
2229 ret = pNtMapViewOfSection(handle, process, &addr, 0, 0, &offset,
2230 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
2231 ok(ret == STATUS_SUCCESS, "NtMapViewOfSection error %#x\n", ret);
2232 ret = pNtUnmapViewOfSection(process, addr);
2233 ok(ret == STATUS_SUCCESS, "NtUnmapViewOfSection error %#x\n", ret);
2235 CloseHandle(handle);
2236 CloseHandle(process);
2238 handle = GetModuleHandleA("winver.exe");
2239 ok(!handle, "winver.exe shouldn't be loaded yet\n");
2240 SetLastError(0xdeadbeef);
2241 handle = LoadLibraryA("winver.exe");
2242 ok(handle != 0, "LoadLibrary error %d\n", GetLastError());
2243 SetLastError(0xdeadbeef);
2244 ret = FreeLibrary(handle);
2245 ok(ret, "FreeLibrary error %d\n", GetLastError());
2246 handle = GetModuleHandleA("winver.exe");
2247 if (param)
2248 ok(handle != 0, "winver.exe should not be unloaded\n");
2249 else
2250 todo_wine
2251 ok(!handle || broken(handle != 0) /* before win7 */, "winver.exe should be unloaded\n");
2253 SetLastError(0xdeadbeef);
2254 ret = WaitForDebugEvent(&de, 0);
2255 ok(!ret, "WaitForDebugEvent should fail\n");
2256 todo_wine
2257 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
2259 SetLastError(0xdeadbeef);
2260 ret = DebugActiveProcess(GetCurrentProcessId());
2261 ok(!ret, "DebugActiveProcess should fail\n");
2262 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2264 SetLastError(0xdeadbeef);
2265 ret = WaitForDebugEvent(&de, 0);
2266 ok(!ret, "WaitForDebugEvent should fail\n");
2267 ok(GetLastError() == ERROR_SEM_TIMEOUT, "expected ERROR_SEM_TIMEOUT, got %d\n", GetLastError());
2269 if (test_dll_phase == 2)
2271 trace("dll: call ExitProcess()\n");
2272 *child_failures = winetest_get_failures();
2273 ExitProcess(197);
2275 trace("dll: %p, DLL_PROCESS_DETACH, %p => DONE\n", hinst, param);
2276 break;
2278 case DLL_THREAD_ATTACH:
2279 trace("dll: %p, DLL_THREAD_ATTACH, %p\n", hinst, param);
2281 ret = pRtlDllShutdownInProgress();
2282 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2284 if (attached_thread_count < MAX_COUNT)
2286 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &attached_thread[attached_thread_count],
2287 0, TRUE, DUPLICATE_SAME_ACCESS);
2288 attached_thread_count++;
2291 /* Make sure the FLS slot is empty, if FLS is available */
2292 if (pFlsGetValue)
2294 void* value;
2295 BOOL ret;
2296 SetLastError(0xdeadbeef);
2297 value = pFlsGetValue(fls_index);
2298 ok(!value, "FlsGetValue returned %p, expected NULL\n", value);
2299 todo_wine
2300 ok(GetLastError() == ERROR_SUCCESS, "FlsGetValue failed with error %u\n", GetLastError());
2301 ret = pFlsSetValue(fls_index, (void*) 0x31415);
2302 ok(ret, "FlsSetValue failed\n");
2303 fls_count++;
2306 break;
2307 case DLL_THREAD_DETACH:
2308 trace("dll: %p, DLL_THREAD_DETACH, %p\n", hinst, param);
2309 thread_detach_count++;
2311 ret = pRtlDllShutdownInProgress();
2312 /* win7 doesn't allow creating a thread during process shutdown but
2313 * earlier Windows versions allow it. In that case DLL_THREAD_DETACH is
2314 * sent on thread exit, but DLL_THREAD_ATTACH is never received.
2316 if (noop_thread_started)
2317 ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
2318 else
2319 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2321 /* FLS data should already be destroyed, if FLS is available.
2322 * Note that this is broken for Win2k3, which runs the callbacks *after* the DLL entry
2323 * point has already run.
2325 if (pFlsGetValue && fls_index != FLS_OUT_OF_INDEXES)
2327 void* value;
2328 SetLastError(0xdeadbeef);
2329 value = pFlsGetValue(fls_index);
2330 todo_wine
2332 ok(broken(value == (void*) 0x31415) || /* Win2k3 */
2333 !value, "FlsGetValue returned %p, expected NULL\n", value);
2335 ok(GetLastError() == ERROR_SUCCESS, "FlsGetValue failed with error %u\n", GetLastError());
2338 break;
2339 default:
2340 trace("dll: %p, %d, %p\n", hinst, reason, param);
2341 break;
2344 *child_failures = winetest_get_failures();
2346 return TRUE;
2349 static void child_process(const char *dll_name, DWORD target_offset)
2351 void *target;
2352 DWORD ret, dummy, i, code, expected_code;
2353 HANDLE file, thread, process;
2354 HMODULE hmod;
2355 struct PROCESS_BASIC_INFORMATION_PRIVATE pbi;
2356 DWORD_PTR affinity;
2358 trace("phase %d: writing %p at %#x\n", test_dll_phase, dll_entry_point, target_offset);
2360 SetLastError(0xdeadbeef);
2361 mutex = CreateMutexW(NULL, FALSE, NULL);
2362 ok(mutex != 0, "CreateMutex error %d\n", GetLastError());
2364 SetLastError(0xdeadbeef);
2365 semaphore = CreateSemaphoreW(NULL, 1, 1, NULL);
2366 ok(semaphore != 0, "CreateSemaphore error %d\n", GetLastError());
2368 SetLastError(0xdeadbeef);
2369 event = CreateEventW(NULL, TRUE, FALSE, NULL);
2370 ok(event != 0, "CreateEvent error %d\n", GetLastError());
2372 SetLastError(0xdeadbeef);
2373 loader_lock_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2374 ok(loader_lock_event != 0, "CreateEvent error %d\n", GetLastError());
2376 SetLastError(0xdeadbeef);
2377 peb_lock_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2378 ok(peb_lock_event != 0, "CreateEvent error %d\n", GetLastError());
2380 SetLastError(0xdeadbeef);
2381 heap_lock_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2382 ok(heap_lock_event != 0, "CreateEvent error %d\n", GetLastError());
2384 SetLastError(0xdeadbeef);
2385 ack_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2386 ok(ack_event != 0, "CreateEvent error %d\n", GetLastError());
2388 file = CreateFileA(dll_name, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
2389 if (file == INVALID_HANDLE_VALUE)
2391 ok(0, "could not open %s\n", dll_name);
2392 return;
2394 SetFilePointer(file, target_offset, NULL, FILE_BEGIN);
2395 SetLastError(0xdeadbeef);
2396 target = dll_entry_point;
2397 ret = WriteFile(file, &target, sizeof(target), &dummy, NULL);
2398 ok(ret, "WriteFile error %d\n", GetLastError());
2399 CloseHandle(file);
2401 SetLastError(0xdeadbeef);
2402 hmod = LoadLibraryA(dll_name);
2403 ok(hmod != 0, "LoadLibrary error %d\n", GetLastError());
2405 SetLastError(0xdeadbeef);
2406 stop_event = CreateEventW(NULL, TRUE, FALSE, NULL);
2407 ok(stop_event != 0, "CreateEvent error %d\n", GetLastError());
2409 SetLastError(0xdeadbeef);
2410 thread = CreateThread(NULL, 0, mutex_thread_proc, event, 0, &dummy);
2411 ok(thread != 0, "CreateThread error %d\n", GetLastError());
2412 WaitForSingleObject(event, 3000);
2413 CloseHandle(thread);
2415 ResetEvent(event);
2417 SetLastError(0xdeadbeef);
2418 thread = CreateThread(NULL, 0, semaphore_thread_proc, event, 0, &dummy);
2419 ok(thread != 0, "CreateThread error %d\n", GetLastError());
2420 WaitForSingleObject(event, 3000);
2421 CloseHandle(thread);
2423 ResetEvent(event);
2424 Sleep(100);
2426 ok(attached_thread_count == 2, "attached thread count should be 2\n");
2427 for (i = 0; i < attached_thread_count; i++)
2429 ret = GetExitCodeThread(attached_thread[i], &code);
2430 trace("child: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
2431 ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
2432 ok(code == STILL_ACTIVE, "expected thread exit code STILL_ACTIVE, got %u\n", code);
2435 ret = WaitForSingleObject(attached_thread[0], 0);
2436 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2437 ret = WaitForSingleObject(attached_thread[1], 0);
2438 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2440 ret = WaitForSingleObject(event, 0);
2441 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2442 ret = WaitForSingleObject(mutex, 0);
2443 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2444 ret = WaitForSingleObject(semaphore, 0);
2445 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2447 ret = pRtlDllShutdownInProgress();
2448 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2450 SetLastError(0xdeadbeef);
2451 process = OpenProcess(PROCESS_ALL_ACCESS_NT4, FALSE, GetCurrentProcessId());
2452 ok(process != NULL, "OpenProcess error %d\n", GetLastError());
2454 SetLastError(0xdeadbeef);
2455 ret = TerminateProcess(0, 195);
2456 ok(!ret, "TerminateProcess(0) should fail\n");
2457 ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
2459 Sleep(100);
2461 affinity = 1;
2462 ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
2463 ok(!ret, "NtSetInformationProcess error %#x\n", ret);
2465 switch (test_dll_phase)
2467 case 0:
2468 ret = pRtlDllShutdownInProgress();
2469 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2471 trace("call NtTerminateProcess(0, 195)\n");
2472 ret = pNtTerminateProcess(0, 195);
2473 ok(!ret, "NtTerminateProcess error %#x\n", ret);
2475 memset(&pbi, 0, sizeof(pbi));
2476 ret = pNtQueryInformationProcess(process, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
2477 ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
2478 ok(pbi.ExitStatus == STILL_ACTIVE || pbi.ExitStatus == 195,
2479 "expected STILL_ACTIVE, got %lu\n", pbi.ExitStatus);
2480 affinity = 1;
2481 ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
2482 ok(!ret, "NtSetInformationProcess error %#x\n", ret);
2484 ret = pRtlDllShutdownInProgress();
2485 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2487 hmod = GetModuleHandleA(dll_name);
2488 ok(hmod != 0, "DLL should not be unloaded\n");
2490 SetLastError(0xdeadbeef);
2491 thread = CreateThread(NULL, 0, noop_thread_proc, &dummy, 0, &ret);
2492 ok(!thread || broken(thread != 0) /* before win7 */, "CreateThread should fail\n");
2493 if (!thread)
2494 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2495 else
2497 ret = WaitForSingleObject(thread, 1000);
2498 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2499 CloseHandle(thread);
2502 trace("call LdrShutdownProcess()\n");
2503 pLdrShutdownProcess();
2505 ret = pRtlDllShutdownInProgress();
2506 ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
2508 hmod = GetModuleHandleA(dll_name);
2509 ok(hmod != 0, "DLL should not be unloaded\n");
2511 memset(&pbi, 0, sizeof(pbi));
2512 ret = pNtQueryInformationProcess(process, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
2513 ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
2514 ok(pbi.ExitStatus == STILL_ACTIVE || pbi.ExitStatus == 195,
2515 "expected STILL_ACTIVE, got %lu\n", pbi.ExitStatus);
2516 affinity = 1;
2517 ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
2518 ok(!ret, "NtSetInformationProcess error %#x\n", ret);
2519 break;
2521 case 1: /* normal ExitProcess */
2522 ret = pRtlDllShutdownInProgress();
2523 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2524 break;
2526 case 2: /* ExitProcess will be called by the PROCESS_DETACH handler */
2527 ret = pRtlDllShutdownInProgress();
2528 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2530 trace("call FreeLibrary(%p)\n", hmod);
2531 SetLastError(0xdeadbeef);
2532 ret = FreeLibrary(hmod);
2533 ok(ret, "FreeLibrary error %d\n", GetLastError());
2534 hmod = GetModuleHandleA(dll_name);
2535 ok(!hmod, "DLL should be unloaded\n");
2537 if (test_dll_phase == 2)
2538 ok(0, "FreeLibrary+ExitProcess should never return\n");
2540 ret = pRtlDllShutdownInProgress();
2541 ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2543 break;
2545 case 3:
2546 trace("signalling thread exit\n");
2547 SetEvent(stop_event);
2548 break;
2550 case 4:
2551 trace("setting loader_lock_event\n");
2552 SetEvent(loader_lock_event);
2553 WaitForSingleObject(ack_event, 1000);
2554 ok(inside_loader_lock != 0, "inside_loader_lock is not set\n");
2556 /* calling NtTerminateProcess should not cause a deadlock */
2557 trace("call NtTerminateProcess(0, 198)\n");
2558 ret = pNtTerminateProcess(0, 198);
2559 ok(!ret, "NtTerminateProcess error %#x\n", ret);
2561 *child_failures = winetest_get_failures();
2563 /* Windows fails to release loader lock acquired from another thread,
2564 * so the LdrUnlockLoaderLock call fails here and ExitProcess deadlocks
2565 * later on, so NtTerminateProcess is used instead.
2567 trace("call NtTerminateProcess(GetCurrentProcess(), 198)\n");
2568 pNtTerminateProcess(GetCurrentProcess(), 198);
2569 ok(0, "NtTerminateProcess should not return\n");
2570 break;
2572 case 5:
2573 trace("setting peb_lock_event\n");
2574 SetEvent(peb_lock_event);
2575 WaitForSingleObject(ack_event, 1000);
2576 ok(inside_peb_lock != 0, "inside_peb_lock is not set\n");
2578 *child_failures = winetest_get_failures();
2580 /* calling ExitProcess should cause a deadlock */
2581 trace("call ExitProcess(198)\n");
2582 ExitProcess(198);
2583 ok(0, "ExitProcess should not return\n");
2584 break;
2586 case 6:
2587 trace("setting heap_lock_event\n");
2588 SetEvent(heap_lock_event);
2589 WaitForSingleObject(ack_event, 1000);
2590 ok(inside_heap_lock != 0, "inside_heap_lock is not set\n");
2592 *child_failures = winetest_get_failures();
2594 /* calling ExitProcess should cause a deadlock */
2595 trace("call ExitProcess(1)\n");
2596 ExitProcess(1);
2597 ok(0, "ExitProcess should not return\n");
2598 break;
2600 default:
2601 assert(0);
2602 break;
2605 if (test_dll_phase == 0) expected_code = 195;
2606 else if (test_dll_phase == 3) expected_code = 196;
2607 else if (test_dll_phase == 4) expected_code = 198;
2608 else expected_code = STILL_ACTIVE;
2610 if (expected_code == STILL_ACTIVE)
2612 ret = WaitForSingleObject(attached_thread[0], 100);
2613 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2614 ret = WaitForSingleObject(attached_thread[1], 100);
2615 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2617 else
2619 ret = WaitForSingleObject(attached_thread[0], 2000);
2620 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2621 ret = WaitForSingleObject(attached_thread[1], 2000);
2622 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2625 for (i = 0; i < attached_thread_count; i++)
2627 ret = GetExitCodeThread(attached_thread[i], &code);
2628 trace("child: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
2629 ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
2630 ok(code == expected_code, "expected thread exit code %u, got %u\n", expected_code, code);
2633 *child_failures = winetest_get_failures();
2635 trace("call ExitProcess(195)\n");
2636 ExitProcess(195);
2639 static void test_ExitProcess(void)
2641 #include "pshpack1.h"
2642 #ifdef __x86_64__
2643 static struct section_data
2645 BYTE mov_rax[2];
2646 void *target;
2647 BYTE jmp_rax[2];
2648 } section_data = { { 0x48,0xb8 }, dll_entry_point, { 0xff,0xe0 } };
2649 #else
2650 static struct section_data
2652 BYTE mov_eax;
2653 void *target;
2654 BYTE jmp_eax[2];
2655 } section_data = { 0xb8, dll_entry_point, { 0xff,0xe0 } };
2656 #endif
2657 #include "poppack.h"
2658 DWORD dummy, file_align;
2659 HANDLE file, thread, process, hmap, hmap_dup;
2660 char temp_path[MAX_PATH], dll_name[MAX_PATH], cmdline[MAX_PATH * 2];
2661 DWORD ret, target_offset, old_prot;
2662 char **argv, buf[256];
2663 PROCESS_INFORMATION pi;
2664 STARTUPINFOA si = { sizeof(si) };
2665 CONTEXT ctx;
2666 struct PROCESS_BASIC_INFORMATION_PRIVATE pbi;
2667 MEMORY_BASIC_INFORMATION mbi;
2668 DWORD_PTR affinity;
2669 void *addr;
2670 LARGE_INTEGER offset;
2671 SIZE_T size;
2672 IMAGE_NT_HEADERS nt_header;
2674 #if !defined(__i386__) && !defined(__x86_64__)
2675 skip("x86 specific ExitProcess test\n");
2676 return;
2677 #endif
2679 if (!pRtlDllShutdownInProgress)
2681 win_skip("RtlDllShutdownInProgress is not available on this platform (XP+)\n");
2682 return;
2684 if (!pNtQueryInformationProcess || !pNtSetInformationProcess)
2686 win_skip("NtQueryInformationProcess/NtSetInformationProcess are not available on this platform\n");
2687 return;
2689 if (!pNtAllocateVirtualMemory || !pNtFreeVirtualMemory)
2691 win_skip("NtAllocateVirtualMemory/NtFreeVirtualMemory are not available on this platform\n");
2692 return;
2695 /* prevent displaying of the "Unable to load this DLL" message box */
2696 SetErrorMode(SEM_FAILCRITICALERRORS);
2698 GetTempPathA(MAX_PATH, temp_path);
2699 GetTempFileNameA(temp_path, "ldr", 0, dll_name);
2701 /*trace("creating %s\n", dll_name);*/
2702 file = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
2703 if (file == INVALID_HANDLE_VALUE)
2705 ok(0, "could not create %s\n", dll_name);
2706 return;
2709 SetLastError(0xdeadbeef);
2710 ret = WriteFile(file, &dos_header, sizeof(dos_header), &dummy, NULL);
2711 ok(ret, "WriteFile error %d\n", GetLastError());
2713 nt_header = nt_header_template;
2714 nt_header.FileHeader.NumberOfSections = 1;
2715 nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
2716 nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL | IMAGE_FILE_RELOCS_STRIPPED;
2718 nt_header.OptionalHeader.AddressOfEntryPoint = 0x1000;
2719 nt_header.OptionalHeader.SectionAlignment = 0x1000;
2720 nt_header.OptionalHeader.FileAlignment = 0x200;
2721 nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000;
2722 nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER);
2723 SetLastError(0xdeadbeef);
2724 ret = WriteFile(file, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
2725 ok(ret, "WriteFile error %d\n", GetLastError());
2726 SetLastError(0xdeadbeef);
2727 ret = WriteFile(file, &nt_header.OptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER), &dummy, NULL);
2728 ok(ret, "WriteFile error %d\n", GetLastError());
2730 section.SizeOfRawData = sizeof(section_data);
2731 section.PointerToRawData = nt_header.OptionalHeader.FileAlignment;
2732 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
2733 section.Misc.VirtualSize = sizeof(section_data);
2734 section.Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
2735 SetLastError(0xdeadbeef);
2736 ret = WriteFile(file, &section, sizeof(section), &dummy, NULL);
2737 ok(ret, "WriteFile error %d\n", GetLastError());
2739 file_align = nt_header.OptionalHeader.FileAlignment - nt_header.OptionalHeader.SizeOfHeaders;
2740 assert(file_align < sizeof(filler));
2741 SetLastError(0xdeadbeef);
2742 ret = WriteFile(file, filler, file_align, &dummy, NULL);
2743 ok(ret, "WriteFile error %d\n", GetLastError());
2745 target_offset = SetFilePointer(file, 0, NULL, FILE_CURRENT) + FIELD_OFFSET(struct section_data, target);
2747 /* section data */
2748 SetLastError(0xdeadbeef);
2749 ret = WriteFile(file, &section_data, sizeof(section_data), &dummy, NULL);
2750 ok(ret, "WriteFile error %d\n", GetLastError());
2752 CloseHandle(file);
2754 winetest_get_mainargs(&argv);
2756 /* phase 0 */
2757 *child_failures = -1;
2758 sprintf(cmdline, "\"%s\" loader %s %u 0", argv[0], dll_name, target_offset);
2759 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2760 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
2761 ret = WaitForSingleObject(pi.hProcess, 10000);
2762 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
2763 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
2764 GetExitCodeProcess(pi.hProcess, &ret);
2765 ok(ret == 195, "expected exit code 195, got %u\n", ret);
2766 if (*child_failures)
2768 trace("%d failures in child process\n", *child_failures);
2769 winetest_add_failures(*child_failures);
2771 CloseHandle(pi.hThread);
2772 CloseHandle(pi.hProcess);
2774 /* phase 1 */
2775 *child_failures = -1;
2776 sprintf(cmdline, "\"%s\" loader %s %u 1", argv[0], dll_name, target_offset);
2777 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2778 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
2779 ret = WaitForSingleObject(pi.hProcess, 10000);
2780 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
2781 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
2782 GetExitCodeProcess(pi.hProcess, &ret);
2783 ok(ret == 195, "expected exit code 195, got %u\n", ret);
2784 if (*child_failures)
2786 trace("%d failures in child process\n", *child_failures);
2787 winetest_add_failures(*child_failures);
2789 CloseHandle(pi.hThread);
2790 CloseHandle(pi.hProcess);
2792 /* phase 2 */
2793 *child_failures = -1;
2794 sprintf(cmdline, "\"%s\" loader %s %u 2", argv[0], dll_name, target_offset);
2795 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2796 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
2797 ret = WaitForSingleObject(pi.hProcess, 10000);
2798 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
2799 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
2800 GetExitCodeProcess(pi.hProcess, &ret);
2801 ok(ret == 197, "expected exit code 197, got %u\n", ret);
2802 if (*child_failures)
2804 trace("%d failures in child process\n", *child_failures);
2805 winetest_add_failures(*child_failures);
2807 CloseHandle(pi.hThread);
2808 CloseHandle(pi.hProcess);
2810 /* phase 3 */
2811 *child_failures = -1;
2812 sprintf(cmdline, "\"%s\" loader %s %u 3", argv[0], dll_name, target_offset);
2813 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2814 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
2815 ret = WaitForSingleObject(pi.hProcess, 10000);
2816 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
2817 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
2818 GetExitCodeProcess(pi.hProcess, &ret);
2819 ok(ret == 195, "expected exit code 195, got %u\n", ret);
2820 if (*child_failures)
2822 trace("%d failures in child process\n", *child_failures);
2823 winetest_add_failures(*child_failures);
2825 CloseHandle(pi.hThread);
2826 CloseHandle(pi.hProcess);
2828 /* phase 4 */
2829 if (pLdrLockLoaderLock && pLdrUnlockLoaderLock)
2831 *child_failures = -1;
2832 sprintf(cmdline, "\"%s\" loader %s %u 4", argv[0], dll_name, target_offset);
2833 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2834 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
2835 ret = WaitForSingleObject(pi.hProcess, 10000);
2836 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
2837 if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
2838 GetExitCodeProcess(pi.hProcess, &ret);
2839 ok(ret == 198, "expected exit code 198, got %u\n", ret);
2840 if (*child_failures)
2842 trace("%d failures in child process\n", *child_failures);
2843 winetest_add_failures(*child_failures);
2845 CloseHandle(pi.hThread);
2846 CloseHandle(pi.hProcess);
2848 else
2849 win_skip("LdrLockLoaderLock/LdrUnlockLoaderLock are not available on this platform\n");
2851 /* phase 5 */
2852 if (pRtlAcquirePebLock && pRtlReleasePebLock)
2854 *child_failures = -1;
2855 sprintf(cmdline, "\"%s\" loader %s %u 5", argv[0], dll_name, target_offset);
2856 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2857 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
2858 ret = WaitForSingleObject(pi.hProcess, 5000);
2859 ok(ret == WAIT_TIMEOUT, "child process should fail to terminate\n");
2860 if (ret != WAIT_OBJECT_0)
2862 trace("terminating child process\n");
2863 TerminateProcess(pi.hProcess, 199);
2865 ret = WaitForSingleObject(pi.hProcess, 1000);
2866 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
2867 GetExitCodeProcess(pi.hProcess, &ret);
2868 ok(ret == 199, "expected exit code 199, got %u\n", ret);
2869 if (*child_failures)
2871 trace("%d failures in child process\n", *child_failures);
2872 winetest_add_failures(*child_failures);
2874 CloseHandle(pi.hThread);
2875 CloseHandle(pi.hProcess);
2877 else
2878 win_skip("RtlAcquirePebLock/RtlReleasePebLock are not available on this platform\n");
2880 /* phase 6 */
2881 *child_failures = -1;
2882 sprintf(cmdline, "\"%s\" loader %s %u 6", argv[0], dll_name, target_offset);
2883 ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2884 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
2885 ret = WaitForSingleObject(pi.hProcess, 5000);
2886 ok(ret == WAIT_TIMEOUT || broken(ret == WAIT_OBJECT_0) /* XP */, "child process should fail to terminate\n");
2887 if (ret != WAIT_OBJECT_0)
2889 trace("terminating child process\n");
2890 TerminateProcess(pi.hProcess, 201);
2892 ret = WaitForSingleObject(pi.hProcess, 1000);
2893 ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
2894 GetExitCodeProcess(pi.hProcess, &ret);
2895 ok(ret == 201 || broken(ret == 1) /* XP */, "expected exit code 201, got %u\n", ret);
2896 if (*child_failures)
2898 trace("%d failures in child process\n", *child_failures);
2899 winetest_add_failures(*child_failures);
2901 CloseHandle(pi.hThread);
2902 CloseHandle(pi.hProcess);
2904 /* test remote process termination */
2905 SetLastError(0xdeadbeef);
2906 ret = CreateProcessA(argv[0], NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
2907 ok(ret, "CreateProcess(%s) error %d\n", argv[0], GetLastError());
2909 SetLastError(0xdeadbeef);
2910 addr = VirtualAllocEx(pi.hProcess, NULL, 4096, MEM_COMMIT, PAGE_READWRITE);
2911 ok(addr != NULL, "VirtualAllocEx error %d\n", GetLastError());
2912 SetLastError(0xdeadbeef);
2913 ret = VirtualProtectEx(pi.hProcess, addr, 4096, PAGE_READONLY, &old_prot);
2914 ok(ret, "VirtualProtectEx error %d\n", GetLastError());
2915 ok(old_prot == PAGE_READWRITE, "expected PAGE_READWRITE, got %#x\n", old_prot);
2916 SetLastError(0xdeadbeef);
2917 size = VirtualQueryEx(pi.hProcess, NULL, &mbi, sizeof(mbi));
2918 ok(size == sizeof(mbi), "VirtualQueryEx error %d\n", GetLastError());
2920 SetLastError(0xdeadbeef);
2921 ret = ReadProcessMemory(pi.hProcess, addr, buf, 4, &size);
2922 ok(ret, "ReadProcessMemory error %d\n", GetLastError());
2923 ok(size == 4, "expected 4, got %lu\n", size);
2925 SetLastError(0xdeadbeef);
2926 hmap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, NULL);
2927 ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
2929 SetLastError(0xdeadbeef);
2930 ret = DuplicateHandle(GetCurrentProcess(), hmap, pi.hProcess, &hmap_dup,
2931 0, FALSE, DUPLICATE_SAME_ACCESS);
2932 ok(ret, "DuplicateHandle error %d\n", GetLastError());
2934 offset.u.LowPart = 0;
2935 offset.u.HighPart = 0;
2936 addr = NULL;
2937 size = 0;
2938 ret = pNtMapViewOfSection(hmap, pi.hProcess, &addr, 0, 0, &offset,
2939 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
2940 ok(!ret, "NtMapViewOfSection error %#x\n", ret);
2941 ret = pNtUnmapViewOfSection(pi.hProcess, addr);
2942 ok(!ret, "NtUnmapViewOfSection error %#x\n", ret);
2944 SetLastError(0xdeadbeef);
2945 thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void *)0xdeadbeef, NULL, CREATE_SUSPENDED, &ret);
2946 ok(thread != 0, "CreateRemoteThread error %d\n", GetLastError());
2947 SetLastError(0xdeadbeef);
2948 ctx.ContextFlags = CONTEXT_INTEGER;
2949 ret = GetThreadContext(thread, &ctx);
2950 ok(ret, "GetThreadContext error %d\n", GetLastError());
2951 SetLastError(0xdeadbeef);
2952 ctx.ContextFlags = CONTEXT_INTEGER;
2953 ret = SetThreadContext(thread, &ctx);
2954 ok(ret, "SetThreadContext error %d\n", GetLastError());
2955 SetLastError(0xdeadbeef);
2956 ret = SetThreadPriority(thread, 0);
2957 ok(ret, "SetThreadPriority error %d\n", GetLastError());
2959 SetLastError(0xdeadbeef);
2960 ret = TerminateThread(thread, 199);
2961 ok(ret, "TerminateThread error %d\n", GetLastError());
2962 /* Calling GetExitCodeThread() without waiting for thread termination
2963 * leads to different results due to a race condition.
2965 ret = WaitForSingleObject(thread, 1000);
2966 ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
2967 GetExitCodeThread(thread, &ret);
2968 ok(ret == 199, "expected exit code 199, got %u\n", ret);
2970 SetLastError(0xdeadbeef);
2971 ret = TerminateProcess(pi.hProcess, 198);
2972 ok(ret, "TerminateProcess error %d\n", GetLastError());
2973 /* Checking process state without waiting for process termination
2974 * leads to different results due to a race condition.
2976 ret = WaitForSingleObject(pi.hProcess, 1000);
2977 ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
2979 SetLastError(0xdeadbeef);
2980 process = OpenProcess(PROCESS_ALL_ACCESS_NT4, FALSE, pi.dwProcessId);
2981 ok(process != NULL, "OpenProcess error %d\n", GetLastError());
2982 CloseHandle(process);
2984 memset(&pbi, 0, sizeof(pbi));
2985 ret = pNtQueryInformationProcess(pi.hProcess, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
2986 ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
2987 ok(pbi.ExitStatus == 198, "expected 198, got %lu\n", pbi.ExitStatus);
2988 affinity = 1;
2989 ret = pNtSetInformationProcess(pi.hProcess, ProcessAffinityMask, &affinity, sizeof(affinity));
2990 ok(ret == STATUS_PROCESS_IS_TERMINATING, "expected STATUS_PROCESS_IS_TERMINATING, got %#x\n", ret);
2992 SetLastError(0xdeadbeef);
2993 ctx.ContextFlags = CONTEXT_INTEGER;
2994 ret = GetThreadContext(thread, &ctx);
2995 ok(!ret || broken(ret) /* XP 64-bit */, "GetThreadContext should fail\n");
2996 if (!ret)
2997 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
2998 GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
2999 GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
3000 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
3001 SetLastError(0xdeadbeef);
3002 ctx.ContextFlags = CONTEXT_INTEGER;
3003 ret = SetThreadContext(thread, &ctx);
3004 ok(!ret || broken(ret) /* XP 64-bit */, "SetThreadContext should fail\n");
3005 if (!ret)
3006 ok(GetLastError() == ERROR_ACCESS_DENIED ||
3007 GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
3008 GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
3009 "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3010 SetLastError(0xdeadbeef);
3011 ret = SetThreadPriority(thread, 0);
3012 ok(ret, "SetThreadPriority error %d\n", GetLastError());
3013 CloseHandle(thread);
3015 SetLastError(0xdeadbeef);
3016 ctx.ContextFlags = CONTEXT_INTEGER;
3017 ret = GetThreadContext(pi.hThread, &ctx);
3018 ok(!ret || broken(ret) /* XP 64-bit */, "GetThreadContext should fail\n");
3019 if (!ret)
3020 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
3021 GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
3022 GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
3023 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
3024 SetLastError(0xdeadbeef);
3025 ctx.ContextFlags = CONTEXT_INTEGER;
3026 ret = SetThreadContext(pi.hThread, &ctx);
3027 ok(!ret || broken(ret) /* XP 64-bit */, "SetThreadContext should fail\n");
3028 if (!ret)
3029 ok(GetLastError() == ERROR_ACCESS_DENIED ||
3030 GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
3031 GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
3032 "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3033 SetLastError(0xdeadbeef);
3034 ret = VirtualProtectEx(pi.hProcess, addr, 4096, PAGE_READWRITE, &old_prot);
3035 ok(!ret, "VirtualProtectEx should fail\n");
3036 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3037 SetLastError(0xdeadbeef);
3038 size = 0;
3039 ret = ReadProcessMemory(pi.hProcess, addr, buf, 4, &size);
3040 ok(!ret, "ReadProcessMemory should fail\n");
3041 ok(GetLastError() == ERROR_PARTIAL_COPY || GetLastError() == ERROR_ACCESS_DENIED,
3042 "expected ERROR_PARTIAL_COPY, got %d\n", GetLastError());
3043 ok(!size, "expected 0, got %lu\n", size);
3044 SetLastError(0xdeadbeef);
3045 ret = VirtualFreeEx(pi.hProcess, addr, 0, MEM_RELEASE);
3046 ok(!ret, "VirtualFreeEx should fail\n");
3047 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3048 SetLastError(0xdeadbeef);
3049 addr = VirtualAllocEx(pi.hProcess, NULL, 4096, MEM_COMMIT, PAGE_READWRITE);
3050 ok(!addr, "VirtualAllocEx should fail\n");
3051 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3052 SetLastError(0xdeadbeef);
3053 size = VirtualQueryEx(pi.hProcess, NULL, &mbi, sizeof(mbi));
3054 ok(!size, "VirtualQueryEx should fail\n");
3055 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3057 /* CloseHandle() call below leads to premature process termination
3058 * under some Windows versions.
3060 if (0)
3062 SetLastError(0xdeadbeef);
3063 ret = CloseHandle(hmap_dup);
3064 ok(ret, "CloseHandle should not fail\n");
3067 SetLastError(0xdeadbeef);
3068 ret = DuplicateHandle(GetCurrentProcess(), hmap, pi.hProcess, &hmap_dup,
3069 0, FALSE, DUPLICATE_SAME_ACCESS);
3070 ok(!ret, "DuplicateHandle should fail\n");
3071 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3073 offset.u.LowPart = 0;
3074 offset.u.HighPart = 0;
3075 addr = NULL;
3076 size = 0;
3077 ret = pNtMapViewOfSection(hmap, pi.hProcess, &addr, 0, 0, &offset,
3078 &size, 1 /* ViewShare */, 0, PAGE_READONLY);
3079 ok(ret == STATUS_PROCESS_IS_TERMINATING, "expected STATUS_PROCESS_IS_TERMINATING, got %#x\n", ret);
3081 SetLastError(0xdeadbeef);
3082 thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void *)0xdeadbeef, NULL, CREATE_SUSPENDED, &ret);
3083 ok(!thread, "CreateRemoteThread should fail\n");
3084 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3086 SetLastError(0xdeadbeef);
3087 ret = DebugActiveProcess(pi.dwProcessId);
3088 ok(!ret, "DebugActiveProcess should fail\n");
3089 ok(GetLastError() == ERROR_ACCESS_DENIED /* 64-bit */ || GetLastError() == ERROR_NOT_SUPPORTED /* 32-bit */,
3090 "ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3092 GetExitCodeProcess(pi.hProcess, &ret);
3093 ok(ret == 198 || broken(ret != 198) /* some 32-bit XP version in a VM returns random exit code */,
3094 "expected exit code 198, got %u\n", ret);
3095 CloseHandle(pi.hThread);
3096 CloseHandle(pi.hProcess);
3098 ret = DeleteFileA(dll_name);
3099 ok(ret, "DeleteFile error %d\n", GetLastError());
3102 static PVOID WINAPI failuredllhook(ULONG ul, DELAYLOAD_INFO* pd)
3104 ok(ul == 4, "expected 4, got %u\n", ul);
3105 ok(!!pd, "no delayload info supplied\n");
3106 if (pd)
3108 ok(pd->Size == sizeof(*pd), "got %u\n", pd->Size);
3109 ok(!!pd->DelayloadDescriptor, "no DelayloadDescriptor supplied\n");
3110 if (pd->DelayloadDescriptor)
3112 ok(pd->DelayloadDescriptor->Attributes.AllAttributes == 1,
3113 "expected 1, got %u\n", pd->DelayloadDescriptor->Attributes.AllAttributes);
3114 ok(pd->DelayloadDescriptor->DllNameRVA == 0x2000,
3115 "expected 0x2000, got %x\n", pd->DelayloadDescriptor->DllNameRVA);
3116 ok(pd->DelayloadDescriptor->ModuleHandleRVA == 0x201a,
3117 "expected 0x201a, got %x\n", pd->DelayloadDescriptor->ModuleHandleRVA);
3118 ok(pd->DelayloadDescriptor->ImportAddressTableRVA > pd->DelayloadDescriptor->ModuleHandleRVA,
3119 "expected %x > %x\n", pd->DelayloadDescriptor->ImportAddressTableRVA,
3120 pd->DelayloadDescriptor->ModuleHandleRVA);
3121 ok(pd->DelayloadDescriptor->ImportNameTableRVA > pd->DelayloadDescriptor->ImportAddressTableRVA,
3122 "expected %x > %x\n", pd->DelayloadDescriptor->ImportNameTableRVA,
3123 pd->DelayloadDescriptor->ImportAddressTableRVA);
3124 ok(pd->DelayloadDescriptor->BoundImportAddressTableRVA == 0,
3125 "expected 0, got %x\n", pd->DelayloadDescriptor->BoundImportAddressTableRVA);
3126 ok(pd->DelayloadDescriptor->UnloadInformationTableRVA == 0,
3127 "expected 0, got %x\n", pd->DelayloadDescriptor->UnloadInformationTableRVA);
3128 ok(pd->DelayloadDescriptor->TimeDateStamp == 0,
3129 "expected 0, got %x\n", pd->DelayloadDescriptor->TimeDateStamp);
3132 ok(!!pd->ThunkAddress, "no ThunkAddress supplied\n");
3133 if (pd->ThunkAddress)
3134 ok(pd->ThunkAddress->u1.Ordinal, "no ThunkAddress value supplied\n");
3136 ok(!!pd->TargetDllName, "no TargetDllName supplied\n");
3137 if (pd->TargetDllName)
3138 ok(!strcmp(pd->TargetDllName, "secur32.dll"),
3139 "expected \"secur32.dll\", got \"%s\"\n", pd->TargetDllName);
3141 ok(pd->TargetApiDescriptor.ImportDescribedByName == 0,
3142 "expected 0, got %x\n", pd->TargetApiDescriptor.ImportDescribedByName);
3143 ok(pd->TargetApiDescriptor.Description.Ordinal == 0 ||
3144 pd->TargetApiDescriptor.Description.Ordinal == 999,
3145 "expected 0, got %x\n", pd->TargetApiDescriptor.Description.Ordinal);
3147 ok(!!pd->TargetModuleBase, "no TargetModuleBase supplied\n");
3148 ok(pd->Unused == NULL, "expected NULL, got %p\n", pd->Unused);
3149 ok(pd->LastError, "no LastError supplied\n");
3151 cb_count++;
3152 return (void*)0xdeadbeef;
3155 static void test_ResolveDelayLoadedAPI(void)
3157 static const char test_dll[] = "secur32.dll";
3158 static const char test_func[] = "SealMessage";
3159 char temp_path[MAX_PATH];
3160 char dll_name[MAX_PATH];
3161 IMAGE_DELAYLOAD_DESCRIPTOR idd, *delaydir;
3162 IMAGE_THUNK_DATA itd32;
3163 HANDLE hfile;
3164 HMODULE hlib;
3165 DWORD dummy, file_size, i;
3166 WORD hint = 0;
3167 BOOL ret;
3168 IMAGE_NT_HEADERS nt_header;
3170 static const struct test_data
3172 BOOL func;
3173 UINT_PTR ordinal;
3174 BOOL succeeds;
3175 } td[] =
3178 TRUE, 0, TRUE
3181 FALSE, IMAGE_ORDINAL_FLAG | 2, TRUE
3184 FALSE, IMAGE_ORDINAL_FLAG | 5, TRUE
3187 FALSE, IMAGE_ORDINAL_FLAG | 0, FALSE
3190 FALSE, IMAGE_ORDINAL_FLAG | 999, FALSE
3194 if (!pResolveDelayLoadedAPI)
3196 win_skip("ResolveDelayLoadedAPI is not available\n");
3197 return;
3200 if (0) /* crashes on native */
3202 SetLastError(0xdeadbeef);
3203 ok(!pResolveDelayLoadedAPI(NULL, NULL, NULL, NULL, NULL, 0),
3204 "ResolveDelayLoadedAPI succeeded\n");
3205 ok(GetLastError() == 0xdeadbeef, "GetLastError changed to %x\n", GetLastError());
3207 cb_count = 0;
3208 SetLastError(0xdeadbeef);
3209 ok(!pResolveDelayLoadedAPI(NULL, NULL, failuredllhook, NULL, NULL, 0),
3210 "ResolveDelayLoadedAPI succeeded\n");
3211 ok(GetLastError() == 0xdeadbeef, "GetLastError changed to %x\n", GetLastError());
3212 ok(cb_count == 1, "Wrong callback count: %d\n", cb_count);
3215 GetTempPathA(MAX_PATH, temp_path);
3216 GetTempFileNameA(temp_path, "ldr", 0, dll_name);
3217 trace("creating %s\n", dll_name);
3218 hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
3219 if (hfile == INVALID_HANDLE_VALUE)
3221 ok(0, "could not create %s\n", dll_name);
3222 return;
3225 SetLastError(0xdeadbeef);
3226 ret = WriteFile(hfile, &dos_header, sizeof(dos_header), &dummy, NULL);
3227 ok(ret, "WriteFile error %d\n", GetLastError());
3229 nt_header = nt_header_template;
3230 nt_header.FileHeader.NumberOfSections = 2;
3231 nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
3233 nt_header.OptionalHeader.SectionAlignment = 0x1000;
3234 nt_header.OptionalHeader.FileAlignment = 0x1000;
3235 nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x2200;
3236 nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + 2 * sizeof(IMAGE_SECTION_HEADER);
3237 nt_header.OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
3238 nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress = 0x1000;
3239 nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size = 0x100;
3241 SetLastError(0xdeadbeef);
3242 ret = WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
3243 ok(ret, "WriteFile error %d\n", GetLastError());
3245 SetLastError(0xdeadbeef);
3246 ret = WriteFile(hfile, &nt_header.OptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER), &dummy, NULL);
3247 ok(ret, "WriteFile error %d\n", GetLastError());
3249 /* sections */
3250 section.PointerToRawData = nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress;
3251 section.VirtualAddress = nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress;
3252 section.Misc.VirtualSize = 2 * sizeof(idd);
3253 section.SizeOfRawData = section.Misc.VirtualSize;
3254 section.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
3255 SetLastError(0xdeadbeef);
3256 ret = WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
3257 ok(ret, "WriteFile error %d\n", GetLastError());
3259 section.PointerToRawData = 0x2000;
3260 section.VirtualAddress = 0x2000;
3261 i = sizeof(td)/sizeof(td[0]);
3262 section.Misc.VirtualSize = sizeof(test_dll) + sizeof(hint) + sizeof(test_func) + sizeof(HMODULE) +
3263 2 * (i + 1) * sizeof(IMAGE_THUNK_DATA);
3264 ok(section.Misc.VirtualSize <= 0x1000, "Too much tests, add a new section!\n");
3265 section.SizeOfRawData = section.Misc.VirtualSize;
3266 section.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
3267 SetLastError(0xdeadbeef);
3268 ret = WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
3269 ok(ret, "WriteFile error %d\n", GetLastError());
3271 /* fill up to delay data */
3272 SetFilePointer( hfile, nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress, NULL, SEEK_SET );
3274 /* delay data */
3275 idd.Attributes.AllAttributes = 1;
3276 idd.DllNameRVA = 0x2000;
3277 idd.ModuleHandleRVA = idd.DllNameRVA + sizeof(test_dll) + sizeof(hint) + sizeof(test_func);
3278 idd.ImportAddressTableRVA = idd.ModuleHandleRVA + sizeof(HMODULE);
3279 idd.ImportNameTableRVA = idd.ImportAddressTableRVA + (i + 1) * sizeof(IMAGE_THUNK_DATA);
3280 idd.BoundImportAddressTableRVA = 0;
3281 idd.UnloadInformationTableRVA = 0;
3282 idd.TimeDateStamp = 0;
3284 SetLastError(0xdeadbeef);
3285 ret = WriteFile(hfile, &idd, sizeof(idd), &dummy, NULL);
3286 ok(ret, "WriteFile error %d\n", GetLastError());
3288 SetLastError(0xdeadbeef);
3289 ret = WriteFile(hfile, filler, sizeof(idd), &dummy, NULL);
3290 ok(ret, "WriteFile error %d\n", GetLastError());
3292 /* fill up to extended delay data */
3293 SetFilePointer( hfile, idd.DllNameRVA, NULL, SEEK_SET );
3295 /* extended delay data */
3296 SetLastError(0xdeadbeef);
3297 ret = WriteFile(hfile, test_dll, sizeof(test_dll), &dummy, NULL);
3298 ok(ret, "WriteFile error %d\n", GetLastError());
3300 SetLastError(0xdeadbeef);
3301 ret = WriteFile(hfile, &hint, sizeof(hint), &dummy, NULL);
3302 ok(ret, "WriteFile error %d\n", GetLastError());
3304 SetLastError(0xdeadbeef);
3305 ret = WriteFile(hfile, test_func, sizeof(test_func), &dummy, NULL);
3306 ok(ret, "WriteFile error %d\n", GetLastError());
3308 SetFilePointer( hfile, idd.ImportAddressTableRVA, NULL, SEEK_SET );
3310 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3312 /* 0x1a00 is an empty space between delay data and extended delay data, real thunks are not necessary */
3313 itd32.u1.Function = nt_header.OptionalHeader.ImageBase + 0x1a00 + i * 0x20;
3314 SetLastError(0xdeadbeef);
3315 ret = WriteFile(hfile, &itd32, sizeof(itd32), &dummy, NULL);
3316 ok(ret, "WriteFile error %d\n", GetLastError());
3319 itd32.u1.Function = 0;
3320 SetLastError(0xdeadbeef);
3321 ret = WriteFile(hfile, &itd32, sizeof(itd32), &dummy, NULL);
3322 ok(ret, "WriteFile error %d\n", GetLastError());
3324 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3326 if (td[i].func)
3327 itd32.u1.AddressOfData = idd.DllNameRVA + sizeof(test_dll);
3328 else
3329 itd32.u1.Ordinal = td[i].ordinal;
3330 SetLastError(0xdeadbeef);
3331 ret = WriteFile(hfile, &itd32, sizeof(itd32), &dummy, NULL);
3332 ok(ret, "WriteFile error %d\n", GetLastError());
3335 itd32.u1.Ordinal = 0;
3336 SetLastError(0xdeadbeef);
3337 ret = WriteFile(hfile, &itd32, sizeof(itd32), &dummy, NULL);
3338 ok(ret, "WriteFile error %d\n", GetLastError());
3340 /* fill up to eof */
3341 SetFilePointer( hfile, section.VirtualAddress + section.Misc.VirtualSize, NULL, SEEK_SET );
3342 SetEndOfFile( hfile );
3343 CloseHandle(hfile);
3345 SetLastError(0xdeadbeef);
3346 hlib = LoadLibraryA(dll_name);
3347 ok(hlib != NULL, "LoadLibrary error %u\n", GetLastError());
3348 if (!hlib)
3350 skip("couldn't load %s.\n", dll_name);
3351 DeleteFileA(dll_name);
3352 return;
3355 delaydir = pRtlImageDirectoryEntryToData(hlib, TRUE, IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &file_size);
3356 if (!delaydir)
3358 skip("haven't found section for delay import directory.\n");
3359 FreeLibrary(hlib);
3360 DeleteFileA(dll_name);
3361 return;
3364 for (;;)
3366 IMAGE_THUNK_DATA *itdn, *itda;
3367 HMODULE htarget;
3369 if (!delaydir->DllNameRVA ||
3370 !delaydir->ImportAddressTableRVA ||
3371 !delaydir->ImportNameTableRVA) break;
3373 itdn = RVAToAddr(delaydir->ImportNameTableRVA, hlib);
3374 itda = RVAToAddr(delaydir->ImportAddressTableRVA, hlib);
3375 htarget = LoadLibraryA(RVAToAddr(delaydir->DllNameRVA, hlib));
3377 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3379 void *ret, *load;
3381 if (IMAGE_SNAP_BY_ORDINAL(itdn[i].u1.Ordinal))
3382 load = (void *)GetProcAddress(htarget, (LPSTR)IMAGE_ORDINAL(itdn[i].u1.Ordinal));
3383 else
3385 const IMAGE_IMPORT_BY_NAME* iibn = RVAToAddr(itdn[i].u1.AddressOfData, hlib);
3386 load = (void *)GetProcAddress(htarget, (char*)iibn->Name);
3389 cb_count = 0;
3390 ret = pResolveDelayLoadedAPI(hlib, delaydir, failuredllhook, NULL, &itda[i], 0);
3391 if (td[i].succeeds)
3393 ok(ret != NULL, "Test %u: ResolveDelayLoadedAPI failed\n", i);
3394 ok(ret == load, "Test %u: expected %p, got %p\n", i, load, ret);
3395 ok(ret == (void*)itda[i].u1.AddressOfData, "Test %u: expected %p, got %p\n",
3396 i, ret, (void*)itda[i].u1.AddressOfData);
3397 ok(!cb_count, "Test %u: Wrong callback count: %d\n", i, cb_count);
3399 else
3401 ok(ret == (void*)0xdeadbeef, "Test %u: ResolveDelayLoadedAPI succeeded with %p\n", i, ret);
3402 ok(cb_count, "Test %u: Wrong callback count: %d\n", i, cb_count);
3405 delaydir++;
3408 FreeLibrary(hlib);
3409 trace("deleting %s\n", dll_name);
3410 DeleteFileA(dll_name);
3413 static void test_InMemoryOrderModuleList(void)
3415 PEB_LDR_DATA *ldr = NtCurrentTeb()->Peb->LdrData;
3416 LIST_ENTRY *entry1, *mark1 = &ldr->InLoadOrderModuleList;
3417 LIST_ENTRY *entry2, *mark2 = &ldr->InMemoryOrderModuleList;
3418 LDR_MODULE *module1, *module2;
3420 ok(ldr->Initialized == TRUE, "expected TRUE, got %u\n", ldr->Initialized);
3422 for (entry1 = mark1->Flink, entry2 = mark2->Flink;
3423 entry1 != mark1 && entry2 != mark2;
3424 entry1 = entry1->Flink, entry2 = entry2->Flink)
3426 module1 = CONTAINING_RECORD(entry1, LDR_MODULE, InLoadOrderModuleList);
3427 module2 = CONTAINING_RECORD(entry2, LDR_MODULE, InMemoryOrderModuleList);
3428 ok(module1 == module2, "expected module1 == module2, got %p and %p\n", module1, module2);
3430 ok(entry1 == mark1, "expected entry1 == mark1, got %p and %p\n", entry1, mark1);
3431 ok(entry2 == mark2, "expected entry2 == mark2, got %p and %p\n", entry2, mark2);
3434 START_TEST(loader)
3436 int argc;
3437 char **argv;
3438 HANDLE ntdll, mapping, kernel32;
3439 SYSTEM_INFO si;
3441 ntdll = GetModuleHandleA("ntdll.dll");
3442 kernel32 = GetModuleHandleA("kernel32.dll");
3443 pNtCreateSection = (void *)GetProcAddress(ntdll, "NtCreateSection");
3444 pNtQuerySection = (void *)GetProcAddress(ntdll, "NtQuerySection");
3445 pNtMapViewOfSection = (void *)GetProcAddress(ntdll, "NtMapViewOfSection");
3446 pNtUnmapViewOfSection = (void *)GetProcAddress(ntdll, "NtUnmapViewOfSection");
3447 pNtTerminateProcess = (void *)GetProcAddress(ntdll, "NtTerminateProcess");
3448 pNtQueryInformationProcess = (void *)GetProcAddress(ntdll, "NtQueryInformationProcess");
3449 pNtSetInformationProcess = (void *)GetProcAddress(ntdll, "NtSetInformationProcess");
3450 pLdrShutdownProcess = (void *)GetProcAddress(ntdll, "LdrShutdownProcess");
3451 pRtlDllShutdownInProgress = (void *)GetProcAddress(ntdll, "RtlDllShutdownInProgress");
3452 pNtAllocateVirtualMemory = (void *)GetProcAddress(ntdll, "NtAllocateVirtualMemory");
3453 pNtFreeVirtualMemory = (void *)GetProcAddress(ntdll, "NtFreeVirtualMemory");
3454 pLdrLockLoaderLock = (void *)GetProcAddress(ntdll, "LdrLockLoaderLock");
3455 pLdrUnlockLoaderLock = (void *)GetProcAddress(ntdll, "LdrUnlockLoaderLock");
3456 pRtlAcquirePebLock = (void *)GetProcAddress(ntdll, "RtlAcquirePebLock");
3457 pRtlReleasePebLock = (void *)GetProcAddress(ntdll, "RtlReleasePebLock");
3458 pRtlImageDirectoryEntryToData = (void *)GetProcAddress(ntdll, "RtlImageDirectoryEntryToData");
3459 pFlsAlloc = (void *)GetProcAddress(kernel32, "FlsAlloc");
3460 pFlsSetValue = (void *)GetProcAddress(kernel32, "FlsSetValue");
3461 pFlsGetValue = (void *)GetProcAddress(kernel32, "FlsGetValue");
3462 pFlsFree = (void *)GetProcAddress(kernel32, "FlsFree");
3463 pIsWow64Process = (void *)GetProcAddress(kernel32, "IsWow64Process");
3464 pResolveDelayLoadedAPI = (void *)GetProcAddress(kernel32, "ResolveDelayLoadedAPI");
3466 if (pIsWow64Process) pIsWow64Process( GetCurrentProcess(), &is_wow64 );
3467 GetSystemInfo( &si );
3468 page_size = si.dwPageSize;
3469 dos_header.e_magic = IMAGE_DOS_SIGNATURE;
3470 dos_header.e_lfanew = sizeof(dos_header);
3472 mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "winetest_loader");
3473 ok(mapping != 0, "CreateFileMapping failed\n");
3474 child_failures = MapViewOfFile(mapping, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 4096);
3475 if (*child_failures == -1)
3477 *child_failures = 0;
3479 else
3480 *child_failures = -1;
3482 argc = winetest_get_mainargs(&argv);
3483 if (argc > 4)
3485 test_dll_phase = atoi(argv[4]);
3486 child_process(argv[2], atol(argv[3]));
3487 return;
3490 test_Loader();
3491 test_ResolveDelayLoadedAPI();
3492 test_ImportDescriptors();
3493 test_section_access();
3494 test_import_resolution();
3495 test_ExitProcess();
3496 test_InMemoryOrderModuleList();