1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Unit test suite for the PE loader.
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 2006,2011 Dmitry Timoshkov
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck  * License as published by the Free Software Foundation; either
9c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c2c66affSColin Finck  * Lesser General Public License for more details.
15c2c66affSColin Finck  *
16c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19c2c66affSColin Finck  */
20c2c66affSColin Finck 
21*ed41a4deSAmine Khaldi #define NONAMELESSUNION
22*ed41a4deSAmine Khaldi #define NONAMELESSSTRUCT
23*ed41a4deSAmine Khaldi #include <stdarg.h>
24*ed41a4deSAmine Khaldi #include <stdio.h>
25*ed41a4deSAmine Khaldi #include <assert.h>
26c2c66affSColin Finck 
27*ed41a4deSAmine Khaldi #include "ntstatus.h"
28*ed41a4deSAmine Khaldi #define WIN32_NO_STATUS
29*ed41a4deSAmine Khaldi #include "windef.h"
30*ed41a4deSAmine Khaldi #include "winbase.h"
31*ed41a4deSAmine Khaldi #include "winternl.h"
32*ed41a4deSAmine Khaldi #include "winuser.h"
33*ed41a4deSAmine Khaldi #include "wine/test.h"
34*ed41a4deSAmine Khaldi #include "delayloadhandler.h"
35c2c66affSColin Finck 
36c2c66affSColin Finck /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
37c2c66affSColin Finck #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
38c2c66affSColin Finck 
39c2c66affSColin Finck #define ALIGN_SIZE(size, alignment) (((size) + (alignment - 1)) & ~((alignment - 1)))
40c2c66affSColin Finck 
41c2c66affSColin Finck struct PROCESS_BASIC_INFORMATION_PRIVATE
42c2c66affSColin Finck {
43c2c66affSColin Finck     DWORD_PTR ExitStatus;
44c2c66affSColin Finck     PPEB      PebBaseAddress;
45c2c66affSColin Finck     DWORD_PTR AffinityMask;
46c2c66affSColin Finck     DWORD_PTR BasePriority;
47c2c66affSColin Finck     ULONG_PTR UniqueProcessId;
48c2c66affSColin Finck     ULONG_PTR InheritedFromUniqueProcessId;
49c2c66affSColin Finck };
50c2c66affSColin Finck 
51c2c66affSColin Finck static LONG *child_failures;
52c2c66affSColin Finck static WORD cb_count;
53c2c66affSColin Finck static DWORD page_size;
54*ed41a4deSAmine Khaldi static BOOL is_win64 = sizeof(void *) > sizeof(int);
55*ed41a4deSAmine Khaldi static BOOL is_wow64;
56c2c66affSColin Finck 
57c2c66affSColin Finck static NTSTATUS (WINAPI *pNtCreateSection)(HANDLE *, ACCESS_MASK, const OBJECT_ATTRIBUTES *,
58c2c66affSColin Finck                                            const LARGE_INTEGER *, ULONG, ULONG, HANDLE );
59*ed41a4deSAmine Khaldi static NTSTATUS (WINAPI *pNtQuerySection)(HANDLE, SECTION_INFORMATION_CLASS, void *, SIZE_T, SIZE_T *);
60c2c66affSColin Finck static NTSTATUS (WINAPI *pNtMapViewOfSection)(HANDLE, HANDLE, PVOID *, ULONG, SIZE_T, const LARGE_INTEGER *, SIZE_T *, ULONG, ULONG, ULONG);
61c2c66affSColin Finck static NTSTATUS (WINAPI *pNtUnmapViewOfSection)(HANDLE, PVOID);
62c2c66affSColin Finck static NTSTATUS (WINAPI *pNtQueryInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
63c2c66affSColin Finck static NTSTATUS (WINAPI *pNtSetInformationProcess)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG);
64c2c66affSColin Finck static NTSTATUS (WINAPI *pNtTerminateProcess)(HANDLE, DWORD);
65c2c66affSColin Finck static void (WINAPI *pLdrShutdownProcess)(void);
66c2c66affSColin Finck static BOOLEAN (WINAPI *pRtlDllShutdownInProgress)(void);
67c2c66affSColin Finck static NTSTATUS (WINAPI *pNtAllocateVirtualMemory)(HANDLE, PVOID *, ULONG, SIZE_T *, ULONG, ULONG);
68c2c66affSColin Finck static NTSTATUS (WINAPI *pNtFreeVirtualMemory)(HANDLE, PVOID *, SIZE_T *, ULONG);
69c2c66affSColin Finck static NTSTATUS (WINAPI *pLdrLockLoaderLock)(ULONG, ULONG *, ULONG_PTR *);
70c2c66affSColin Finck static NTSTATUS (WINAPI *pLdrUnlockLoaderLock)(ULONG, ULONG_PTR);
71c2c66affSColin Finck static void (WINAPI *pRtlAcquirePebLock)(void);
72c2c66affSColin Finck static void (WINAPI *pRtlReleasePebLock)(void);
73c2c66affSColin Finck static PVOID    (WINAPI *pResolveDelayLoadedAPI)(PVOID, PCIMAGE_DELAYLOAD_DESCRIPTOR,
74c2c66affSColin Finck                                                  PDELAYLOAD_FAILURE_DLL_CALLBACK, PVOID,
75c2c66affSColin Finck                                                  PIMAGE_THUNK_DATA ThunkAddress,ULONG);
76c2c66affSColin Finck static PVOID (WINAPI *pRtlImageDirectoryEntryToData)(HMODULE,BOOL,WORD,ULONG *);
77c2c66affSColin Finck static DWORD (WINAPI *pFlsAlloc)(PFLS_CALLBACK_FUNCTION);
78c2c66affSColin Finck static BOOL (WINAPI *pFlsSetValue)(DWORD, PVOID);
79c2c66affSColin Finck static PVOID (WINAPI *pFlsGetValue)(DWORD);
80c2c66affSColin Finck static BOOL (WINAPI *pFlsFree)(DWORD);
81*ed41a4deSAmine Khaldi static BOOL (WINAPI *pIsWow64Process)(HANDLE,PBOOL);
82c2c66affSColin Finck 
RVAToAddr(DWORD_PTR rva,HMODULE module)83c2c66affSColin Finck static PVOID RVAToAddr(DWORD_PTR rva, HMODULE module)
84c2c66affSColin Finck {
85c2c66affSColin Finck     if (rva == 0)
86c2c66affSColin Finck         return NULL;
87c2c66affSColin Finck     return ((char*) module) + rva;
88c2c66affSColin Finck }
89c2c66affSColin Finck 
90c2c66affSColin Finck static IMAGE_DOS_HEADER dos_header;
91c2c66affSColin Finck 
92c2c66affSColin Finck static const IMAGE_NT_HEADERS nt_header_template =
93c2c66affSColin Finck {
94c2c66affSColin Finck     IMAGE_NT_SIGNATURE, /* Signature */
95c2c66affSColin Finck     {
96c2c66affSColin Finck #if defined __i386__
97c2c66affSColin Finck       IMAGE_FILE_MACHINE_I386, /* Machine */
98c2c66affSColin Finck #elif defined __x86_64__
99c2c66affSColin Finck       IMAGE_FILE_MACHINE_AMD64, /* Machine */
100c2c66affSColin Finck #elif defined __powerpc__
101c2c66affSColin Finck       IMAGE_FILE_MACHINE_POWERPC, /* Machine */
102c2c66affSColin Finck #elif defined __arm__
103c2c66affSColin Finck       IMAGE_FILE_MACHINE_ARMNT, /* Machine */
104c2c66affSColin Finck #elif defined __aarch64__
105c2c66affSColin Finck       IMAGE_FILE_MACHINE_ARM64, /* Machine */
106c2c66affSColin Finck #else
107c2c66affSColin Finck # error You must specify the machine type
108c2c66affSColin Finck #endif
109c2c66affSColin Finck       1, /* NumberOfSections */
110c2c66affSColin Finck       0, /* TimeDateStamp */
111c2c66affSColin Finck       0, /* PointerToSymbolTable */
112c2c66affSColin Finck       0, /* NumberOfSymbols */
113c2c66affSColin Finck       sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
114c2c66affSColin Finck       IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
115c2c66affSColin Finck     },
116c2c66affSColin Finck     { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
117c2c66affSColin Finck       1, /* MajorLinkerVersion */
118c2c66affSColin Finck       0, /* MinorLinkerVersion */
119c2c66affSColin Finck       0, /* SizeOfCode */
120c2c66affSColin Finck       0, /* SizeOfInitializedData */
121c2c66affSColin Finck       0, /* SizeOfUninitializedData */
122c2c66affSColin Finck       0, /* AddressOfEntryPoint */
123c2c66affSColin Finck       0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
124c2c66affSColin Finck #ifndef _WIN64
125c2c66affSColin Finck       0, /* BaseOfData */
126c2c66affSColin Finck #endif
127c2c66affSColin Finck       0x10000000, /* ImageBase */
128c2c66affSColin Finck       0, /* SectionAlignment */
129c2c66affSColin Finck       0, /* FileAlignment */
130c2c66affSColin Finck       4, /* MajorOperatingSystemVersion */
131c2c66affSColin Finck       0, /* MinorOperatingSystemVersion */
132c2c66affSColin Finck       1, /* MajorImageVersion */
133c2c66affSColin Finck       0, /* MinorImageVersion */
134c2c66affSColin Finck       4, /* MajorSubsystemVersion */
135c2c66affSColin Finck       0, /* MinorSubsystemVersion */
136c2c66affSColin Finck       0, /* Win32VersionValue */
137c2c66affSColin Finck       sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000, /* SizeOfImage */
138c2c66affSColin Finck       sizeof(dos_header) + sizeof(nt_header_template), /* SizeOfHeaders */
139c2c66affSColin Finck       0, /* CheckSum */
140c2c66affSColin Finck       IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
141c2c66affSColin Finck       0, /* DllCharacteristics */
142c2c66affSColin Finck       0, /* SizeOfStackReserve */
143c2c66affSColin Finck       0, /* SizeOfStackCommit */
144c2c66affSColin Finck       0, /* SizeOfHeapReserve */
145c2c66affSColin Finck       0, /* SizeOfHeapCommit */
146c2c66affSColin Finck       0, /* LoaderFlags */
147c2c66affSColin Finck       0, /* NumberOfRvaAndSizes */
148c2c66affSColin Finck       { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
149c2c66affSColin Finck     }
150c2c66affSColin Finck };
151c2c66affSColin Finck 
152c2c66affSColin Finck static IMAGE_SECTION_HEADER section =
153c2c66affSColin Finck {
154c2c66affSColin Finck     ".rodata", /* Name */
155c2c66affSColin Finck     { 0 }, /* Misc */
156c2c66affSColin Finck     0, /* VirtualAddress */
157c2c66affSColin Finck     0, /* SizeOfRawData */
158c2c66affSColin Finck     0, /* PointerToRawData */
159c2c66affSColin Finck     0, /* PointerToRelocations */
160c2c66affSColin Finck     0, /* PointerToLinenumbers */
161c2c66affSColin Finck     0, /* NumberOfRelocations */
162c2c66affSColin Finck     0, /* NumberOfLinenumbers */
163c2c66affSColin Finck     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, /* Characteristics */
164c2c66affSColin Finck };
165c2c66affSColin Finck 
166c2c66affSColin Finck 
167c2c66affSColin Finck static const char filler[0x1000];
168c2c66affSColin Finck static const char section_data[0x10] = "section data";
169c2c66affSColin Finck 
create_test_dll(const IMAGE_DOS_HEADER * dos_header,UINT dos_size,const IMAGE_NT_HEADERS * nt_header,char dll_name[MAX_PATH])170c2c66affSColin Finck static DWORD create_test_dll( const IMAGE_DOS_HEADER *dos_header, UINT dos_size,
171*ed41a4deSAmine Khaldi                               const IMAGE_NT_HEADERS *nt_header, char dll_name[MAX_PATH] )
172c2c66affSColin Finck {
173*ed41a4deSAmine Khaldi     char temp_path[MAX_PATH];
174c2c66affSColin Finck     DWORD dummy, size, file_align;
175c2c66affSColin Finck     HANDLE hfile;
176c2c66affSColin Finck     BOOL ret;
177c2c66affSColin Finck 
178*ed41a4deSAmine Khaldi     GetTempPathA(MAX_PATH, temp_path);
179*ed41a4deSAmine Khaldi     GetTempFileNameA(temp_path, "ldr", 0, dll_name);
180*ed41a4deSAmine Khaldi 
181c2c66affSColin Finck     hfile = CreateFileA(dll_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0);
182*ed41a4deSAmine Khaldi     ok( hfile != INVALID_HANDLE_VALUE, "failed to create %s err %u\n", dll_name, GetLastError() );
183c2c66affSColin Finck     if (hfile == INVALID_HANDLE_VALUE) return 0;
184c2c66affSColin Finck 
185c2c66affSColin Finck     SetLastError(0xdeadbeef);
186c2c66affSColin Finck     ret = WriteFile(hfile, dos_header, dos_size, &dummy, NULL);
187c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
188c2c66affSColin Finck 
189c2c66affSColin Finck     SetLastError(0xdeadbeef);
190c2c66affSColin Finck     ret = WriteFile(hfile, nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
191c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
192c2c66affSColin Finck 
193c2c66affSColin Finck     if (nt_header->FileHeader.SizeOfOptionalHeader)
194c2c66affSColin Finck     {
195c2c66affSColin Finck         SetLastError(0xdeadbeef);
196c2c66affSColin Finck         ret = WriteFile(hfile, &nt_header->OptionalHeader,
197c2c66affSColin Finck                         sizeof(IMAGE_OPTIONAL_HEADER),
198c2c66affSColin Finck                         &dummy, NULL);
199c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
200c2c66affSColin Finck         if (nt_header->FileHeader.SizeOfOptionalHeader > sizeof(IMAGE_OPTIONAL_HEADER))
201c2c66affSColin Finck         {
202c2c66affSColin Finck             file_align = nt_header->FileHeader.SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER);
203c2c66affSColin Finck             assert(file_align < sizeof(filler));
204c2c66affSColin Finck             SetLastError(0xdeadbeef);
205c2c66affSColin Finck             ret = WriteFile(hfile, filler, file_align, &dummy, NULL);
206c2c66affSColin Finck             ok(ret, "WriteFile error %d\n", GetLastError());
207c2c66affSColin Finck         }
208c2c66affSColin Finck     }
209c2c66affSColin Finck 
210c2c66affSColin Finck     assert(nt_header->FileHeader.NumberOfSections <= 1);
211c2c66affSColin Finck     if (nt_header->FileHeader.NumberOfSections)
212c2c66affSColin Finck     {
213c2c66affSColin Finck         SetFilePointer(hfile, dos_size + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + nt_header->FileHeader.SizeOfOptionalHeader, NULL, FILE_BEGIN);
214c2c66affSColin Finck 
215c2c66affSColin Finck         section.SizeOfRawData = 10;
216c2c66affSColin Finck 
217c2c66affSColin Finck         if (nt_header->OptionalHeader.SectionAlignment >= page_size)
218c2c66affSColin Finck         {
219c2c66affSColin Finck             section.PointerToRawData = dos_size;
220c2c66affSColin Finck             section.VirtualAddress = nt_header->OptionalHeader.SectionAlignment;
221c2c66affSColin Finck             section.Misc.VirtualSize = section.SizeOfRawData * 10;
222c2c66affSColin Finck         }
223c2c66affSColin Finck         else
224c2c66affSColin Finck         {
225c2c66affSColin Finck             section.PointerToRawData = nt_header->OptionalHeader.SizeOfHeaders;
226c2c66affSColin Finck             section.VirtualAddress = nt_header->OptionalHeader.SizeOfHeaders;
227c2c66affSColin Finck             section.Misc.VirtualSize = 5;
228c2c66affSColin Finck         }
229c2c66affSColin Finck 
230c2c66affSColin Finck         SetLastError(0xdeadbeef);
231c2c66affSColin Finck         ret = WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
232c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
233c2c66affSColin Finck 
234c2c66affSColin Finck         /* section data */
235c2c66affSColin Finck         SetLastError(0xdeadbeef);
236c2c66affSColin Finck         ret = WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL);
237c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
238c2c66affSColin Finck     }
239c2c66affSColin Finck 
240c2c66affSColin Finck     /* Minimal PE image that Windows7+ is able to load: 268 bytes */
241c2c66affSColin Finck     size = GetFileSize(hfile, NULL);
242c2c66affSColin Finck     if (size < 268)
243c2c66affSColin Finck     {
244c2c66affSColin Finck         file_align = 268 - size;
245c2c66affSColin Finck         SetLastError(0xdeadbeef);
246c2c66affSColin Finck         ret = WriteFile(hfile, filler, file_align, &dummy, NULL);
247c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
248c2c66affSColin Finck     }
249c2c66affSColin Finck 
250c2c66affSColin Finck     size = GetFileSize(hfile, NULL);
251c2c66affSColin Finck     CloseHandle(hfile);
252c2c66affSColin Finck     return size;
253c2c66affSColin Finck }
254c2c66affSColin Finck 
create_test_dll_sections(const IMAGE_DOS_HEADER * dos_header,const IMAGE_NT_HEADERS * nt_header,const IMAGE_SECTION_HEADER * sections,const void * section_data,char dll_name[MAX_PATH])255*ed41a4deSAmine Khaldi static DWORD create_test_dll_sections( const IMAGE_DOS_HEADER *dos_header, const IMAGE_NT_HEADERS *nt_header,
256*ed41a4deSAmine Khaldi                                        const IMAGE_SECTION_HEADER *sections, const void *section_data,
257*ed41a4deSAmine Khaldi                                        char dll_name[MAX_PATH] )
258c2c66affSColin Finck {
259*ed41a4deSAmine Khaldi     char temp_path[MAX_PATH];
260*ed41a4deSAmine Khaldi     DWORD dummy, i, size;
261*ed41a4deSAmine Khaldi     HANDLE hfile;
262*ed41a4deSAmine Khaldi     BOOL ret;
263*ed41a4deSAmine Khaldi 
264*ed41a4deSAmine Khaldi     GetTempPathA(MAX_PATH, temp_path);
265*ed41a4deSAmine Khaldi     GetTempFileNameA(temp_path, "ldr", 0, dll_name);
266*ed41a4deSAmine Khaldi 
267*ed41a4deSAmine Khaldi     hfile = CreateFileA(dll_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0);
268*ed41a4deSAmine Khaldi     ok( hfile != INVALID_HANDLE_VALUE, "failed to create %s err %u\n", dll_name, GetLastError() );
269*ed41a4deSAmine Khaldi     if (hfile == INVALID_HANDLE_VALUE) return 0;
270*ed41a4deSAmine Khaldi 
271*ed41a4deSAmine Khaldi     SetLastError(0xdeadbeef);
272*ed41a4deSAmine Khaldi     ret = WriteFile(hfile, dos_header, sizeof(*dos_header), &dummy, NULL);
273*ed41a4deSAmine Khaldi     ok(ret, "WriteFile error %d\n", GetLastError());
274*ed41a4deSAmine Khaldi 
275*ed41a4deSAmine Khaldi     SetLastError(0xdeadbeef);
276*ed41a4deSAmine Khaldi     ret = WriteFile(hfile, nt_header, offsetof(IMAGE_NT_HEADERS, OptionalHeader) + nt_header->FileHeader.SizeOfOptionalHeader, &dummy, NULL);
277*ed41a4deSAmine Khaldi     ok(ret, "WriteFile error %d\n", GetLastError());
278*ed41a4deSAmine Khaldi 
279*ed41a4deSAmine Khaldi     SetLastError(0xdeadbeef);
280*ed41a4deSAmine Khaldi     ret = WriteFile(hfile, sections, sizeof(*sections) * nt_header->FileHeader.NumberOfSections,
281*ed41a4deSAmine Khaldi                     &dummy, NULL);
282*ed41a4deSAmine Khaldi     ok(ret, "WriteFile error %d\n", GetLastError());
283*ed41a4deSAmine Khaldi 
284*ed41a4deSAmine Khaldi     for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
285*ed41a4deSAmine Khaldi     {
286*ed41a4deSAmine Khaldi         SetFilePointer(hfile, sections[i].PointerToRawData, NULL, FILE_BEGIN);
287*ed41a4deSAmine Khaldi         SetLastError(0xdeadbeef);
288*ed41a4deSAmine Khaldi         ret = WriteFile(hfile, section_data, sections[i].SizeOfRawData, &dummy, NULL);
289*ed41a4deSAmine Khaldi         ok(ret, "WriteFile error %d\n", GetLastError());
290*ed41a4deSAmine Khaldi     }
291*ed41a4deSAmine Khaldi     size = GetFileSize(hfile, NULL);
292*ed41a4deSAmine Khaldi     CloseHandle(hfile);
293*ed41a4deSAmine Khaldi     return size;
294*ed41a4deSAmine Khaldi }
295*ed41a4deSAmine Khaldi 
query_image_section(int id,const char * dll_name,const IMAGE_NT_HEADERS * nt_header,const void * section_data)296*ed41a4deSAmine Khaldi static BOOL query_image_section( int id, const char *dll_name, const IMAGE_NT_HEADERS *nt_header,
297*ed41a4deSAmine Khaldi                                  const void *section_data )
298*ed41a4deSAmine Khaldi {
299*ed41a4deSAmine Khaldi     static BOOL is_winxp;
300c2c66affSColin Finck     SECTION_BASIC_INFORMATION info;
301c2c66affSColin Finck     SECTION_IMAGE_INFORMATION image;
302*ed41a4deSAmine Khaldi     const IMAGE_COR20_HEADER *cor_header = NULL;
303*ed41a4deSAmine Khaldi     SIZE_T info_size = (SIZE_T)0xdeadbeef << 16;
304c2c66affSColin Finck     NTSTATUS status;
305c2c66affSColin Finck     HANDLE file, mapping;
306c2c66affSColin Finck     ULONG file_size;
307c2c66affSColin Finck     LARGE_INTEGER map_size;
308*ed41a4deSAmine Khaldi     SIZE_T max_stack, commit_stack;
309*ed41a4deSAmine Khaldi     void *entry_point;
310*ed41a4deSAmine Khaldi 
311c2c66affSColin Finck     /* truncated header is not handled correctly in windows <= w2k3 */
312*ed41a4deSAmine Khaldi     BOOL truncated;
313c2c66affSColin Finck 
314c2c66affSColin Finck     file = CreateFileA( dll_name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE,
315c2c66affSColin Finck                         NULL, OPEN_EXISTING, 0, 0 );
316c2c66affSColin Finck     ok( file != INVALID_HANDLE_VALUE, "%u: CreateFile error %d\n", id, GetLastError() );
317c2c66affSColin Finck     file_size = GetFileSize( file, NULL );
318c2c66affSColin Finck 
319c2c66affSColin Finck     status = pNtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
320c2c66affSColin Finck                                NULL, NULL, PAGE_READONLY, SEC_IMAGE, file );
321c2c66affSColin Finck     ok( !status, "%u: NtCreateSection failed err %x\n", id, status );
322c2c66affSColin Finck     if (status)
323c2c66affSColin Finck     {
324c2c66affSColin Finck         CloseHandle( file );
325*ed41a4deSAmine Khaldi         return FALSE;
326c2c66affSColin Finck     }
327c2c66affSColin Finck     status = pNtQuerySection( mapping, SectionImageInformation, &image, sizeof(image), &info_size );
328c2c66affSColin Finck     ok( !status, "%u: NtQuerySection failed err %x\n", id, status );
329*ed41a4deSAmine Khaldi     ok( info_size == sizeof(image), "%u: NtQuerySection wrong size %lu\n", id, info_size );
330*ed41a4deSAmine Khaldi     if (nt_header->OptionalHeader.Magic == (is_win64 ? IMAGE_NT_OPTIONAL_HDR64_MAGIC
331*ed41a4deSAmine Khaldi                                                      : IMAGE_NT_OPTIONAL_HDR32_MAGIC))
332*ed41a4deSAmine Khaldi     {
333*ed41a4deSAmine Khaldi         max_stack = nt_header->OptionalHeader.SizeOfStackReserve;
334*ed41a4deSAmine Khaldi         commit_stack = nt_header->OptionalHeader.SizeOfStackCommit;
335*ed41a4deSAmine Khaldi         entry_point = (char *)nt_header->OptionalHeader.ImageBase + nt_header->OptionalHeader.AddressOfEntryPoint;
336*ed41a4deSAmine Khaldi         truncated = nt_header->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER);
337*ed41a4deSAmine Khaldi         if (!truncated &&
338*ed41a4deSAmine Khaldi             nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress &&
339*ed41a4deSAmine Khaldi             nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size)
340*ed41a4deSAmine Khaldi             cor_header = section_data;
341*ed41a4deSAmine Khaldi     }
342*ed41a4deSAmine Khaldi     else if (nt_header->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
343*ed41a4deSAmine Khaldi     {
344*ed41a4deSAmine Khaldi         const IMAGE_NT_HEADERS64 *nt64 = (const IMAGE_NT_HEADERS64 *)nt_header;
345*ed41a4deSAmine Khaldi         max_stack = 0x100000;
346*ed41a4deSAmine Khaldi         commit_stack = 0x10000;
347*ed41a4deSAmine Khaldi         entry_point = (void *)0x81231234;
348*ed41a4deSAmine Khaldi         truncated = nt_header->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER64);
349*ed41a4deSAmine Khaldi         if (!truncated &&
350*ed41a4deSAmine Khaldi             nt64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress &&
351*ed41a4deSAmine Khaldi             nt64->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size)
352*ed41a4deSAmine Khaldi             cor_header = section_data;
353*ed41a4deSAmine Khaldi     }
354*ed41a4deSAmine Khaldi     else
355*ed41a4deSAmine Khaldi     {
356*ed41a4deSAmine Khaldi         const IMAGE_NT_HEADERS32 *nt32 = (const IMAGE_NT_HEADERS32 *)nt_header;
357*ed41a4deSAmine Khaldi         max_stack = nt32->OptionalHeader.SizeOfStackReserve;
358*ed41a4deSAmine Khaldi         commit_stack = nt32->OptionalHeader.SizeOfStackCommit;
359*ed41a4deSAmine Khaldi         entry_point = (char *)(ULONG_PTR)nt32->OptionalHeader.ImageBase + nt32->OptionalHeader.AddressOfEntryPoint;
360*ed41a4deSAmine Khaldi         truncated = nt_header->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER32);
361*ed41a4deSAmine Khaldi         if (!truncated &&
362*ed41a4deSAmine Khaldi             nt32->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress &&
363*ed41a4deSAmine Khaldi             nt32->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size)
364*ed41a4deSAmine Khaldi             cor_header = section_data;
365*ed41a4deSAmine Khaldi     }
366*ed41a4deSAmine Khaldi     ok( (char *)image.TransferAddress == (char *)entry_point ||
367*ed41a4deSAmine Khaldi         (S(U(image)).ImageDynamicallyRelocated && LOWORD(image.TransferAddress) == LOWORD(entry_point)),
368*ed41a4deSAmine Khaldi         "%u: TransferAddress wrong %p / %p (%08x)\n", id,
369*ed41a4deSAmine Khaldi         image.TransferAddress, entry_point, nt_header->OptionalHeader.AddressOfEntryPoint );
370c2c66affSColin Finck     ok( image.ZeroBits == 0, "%u: ZeroBits wrong %08x\n", id, image.ZeroBits );
371*ed41a4deSAmine Khaldi     ok( image.MaximumStackSize == max_stack || broken(truncated),
372*ed41a4deSAmine Khaldi         "%u: MaximumStackSize wrong %lx / %lx\n", id, image.MaximumStackSize, max_stack );
373*ed41a4deSAmine Khaldi     ok( image.CommittedStackSize == commit_stack || broken(truncated),
374*ed41a4deSAmine Khaldi         "%u: CommittedStackSize wrong %lx / %lx\n", id, image.CommittedStackSize, commit_stack );
375c2c66affSColin Finck     if (truncated)
376c2c66affSColin Finck         ok( !image.SubSystemType || broken(truncated),
377c2c66affSColin Finck             "%u: SubSystemType wrong %08x / 00000000\n", id, image.SubSystemType );
378c2c66affSColin Finck     else
379c2c66affSColin Finck         ok( image.SubSystemType == nt_header->OptionalHeader.Subsystem,
380c2c66affSColin Finck             "%u: SubSystemType wrong %08x / %08x\n", id,
381c2c66affSColin Finck             image.SubSystemType, nt_header->OptionalHeader.Subsystem );
382c2c66affSColin Finck     ok( image.SubsystemVersionLow == nt_header->OptionalHeader.MinorSubsystemVersion,
383c2c66affSColin Finck         "%u: SubsystemVersionLow wrong %04x / %04x\n", id,
384c2c66affSColin Finck         image.SubsystemVersionLow, nt_header->OptionalHeader.MinorSubsystemVersion );
385c2c66affSColin Finck     ok( image.SubsystemVersionHigh == nt_header->OptionalHeader.MajorSubsystemVersion,
386c2c66affSColin Finck         "%u: SubsystemVersionHigh wrong %04x / %04x\n", id,
387c2c66affSColin Finck         image.SubsystemVersionHigh, nt_header->OptionalHeader.MajorSubsystemVersion );
388c2c66affSColin Finck     ok( image.ImageCharacteristics == nt_header->FileHeader.Characteristics,
389c2c66affSColin Finck         "%u: ImageCharacteristics wrong %04x / %04x\n", id,
390c2c66affSColin Finck         image.ImageCharacteristics, nt_header->FileHeader.Characteristics );
391c2c66affSColin Finck     ok( image.DllCharacteristics == nt_header->OptionalHeader.DllCharacteristics || broken(truncated),
392c2c66affSColin Finck         "%u: DllCharacteristics wrong %04x / %04x\n", id,
393c2c66affSColin Finck         image.DllCharacteristics, nt_header->OptionalHeader.DllCharacteristics );
394c2c66affSColin Finck     ok( image.Machine == nt_header->FileHeader.Machine, "%u: Machine wrong %04x / %04x\n", id,
395c2c66affSColin Finck         image.Machine, nt_header->FileHeader.Machine );
396*ed41a4deSAmine Khaldi     ok( image.LoaderFlags == (cor_header != NULL), "%u: LoaderFlags wrong %08x\n", id, image.LoaderFlags );
397c2c66affSColin Finck     ok( image.ImageFileSize == file_size || broken(!image.ImageFileSize), /* winxpsp1 */
398c2c66affSColin Finck         "%u: ImageFileSize wrong %08x / %08x\n", id, image.ImageFileSize, file_size );
399c2c66affSColin Finck     ok( image.CheckSum == nt_header->OptionalHeader.CheckSum || broken(truncated),
400c2c66affSColin Finck         "%u: CheckSum wrong %08x / %08x\n", id,
401c2c66affSColin Finck         image.CheckSum, nt_header->OptionalHeader.CheckSum );
402*ed41a4deSAmine Khaldi 
403*ed41a4deSAmine Khaldi     if (nt_header->OptionalHeader.SizeOfCode || nt_header->OptionalHeader.AddressOfEntryPoint)
404*ed41a4deSAmine Khaldi         ok( image.ImageContainsCode == TRUE, "%u: ImageContainsCode wrong %u\n", id,
405*ed41a4deSAmine Khaldi             image.ImageContainsCode );
406*ed41a4deSAmine Khaldi     else if ((nt_header->OptionalHeader.SectionAlignment % page_size) ||
407*ed41a4deSAmine Khaldi              (nt_header->FileHeader.NumberOfSections == 1 &&
408*ed41a4deSAmine Khaldi               (section.Characteristics & IMAGE_SCN_MEM_EXECUTE)))
409*ed41a4deSAmine Khaldi         ok( image.ImageContainsCode == TRUE || broken(!image.ImageContainsCode), /* <= win8 */
410*ed41a4deSAmine Khaldi             "%u: ImageContainsCode wrong %u\n", id, image.ImageContainsCode );
411*ed41a4deSAmine Khaldi     else
412*ed41a4deSAmine Khaldi         ok( !image.ImageContainsCode, "%u: ImageContainsCode wrong %u\n", id, image.ImageContainsCode );
413*ed41a4deSAmine Khaldi 
414*ed41a4deSAmine Khaldi     if (cor_header &&
415*ed41a4deSAmine Khaldi         (cor_header->Flags & COMIMAGE_FLAGS_ILONLY) &&
416*ed41a4deSAmine Khaldi         (cor_header->MajorRuntimeVersion > 2 ||
417*ed41a4deSAmine Khaldi          (cor_header->MajorRuntimeVersion == 2 && cor_header->MinorRuntimeVersion >= 5)))
418*ed41a4deSAmine Khaldi     {
419*ed41a4deSAmine Khaldi         ok( S(U(image)).ComPlusILOnly || broken(is_winxp),
420*ed41a4deSAmine Khaldi             "%u: wrong ComPlusILOnly flags %02x\n", id, U(image).ImageFlags );
421*ed41a4deSAmine Khaldi         if (nt_header->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC &&
422*ed41a4deSAmine Khaldi             !(cor_header->Flags & COMIMAGE_FLAGS_32BITREQUIRED))
423*ed41a4deSAmine Khaldi             ok( S(U(image)).ComPlusNativeReady || broken(is_winxp),
424*ed41a4deSAmine Khaldi                 "%u: wrong ComPlusNativeReady flags %02x\n", id, U(image).ImageFlags );
425*ed41a4deSAmine Khaldi         else
426*ed41a4deSAmine Khaldi             ok( !S(U(image)).ComPlusNativeReady,
427*ed41a4deSAmine Khaldi                 "%u: wrong ComPlusNativeReady flags %02x\n", id, U(image).ImageFlags );
428*ed41a4deSAmine Khaldi     }
429*ed41a4deSAmine Khaldi     else
430*ed41a4deSAmine Khaldi     {
431*ed41a4deSAmine Khaldi         ok( !S(U(image)).ComPlusILOnly, "%u: wrong ComPlusILOnly flags %02x\n", id, U(image).ImageFlags );
432*ed41a4deSAmine Khaldi         ok( !S(U(image)).ComPlusNativeReady, "%u: wrong ComPlusNativeReady flags %02x\n", id, U(image).ImageFlags );
433*ed41a4deSAmine Khaldi     }
434*ed41a4deSAmine Khaldi     if (!(nt_header->OptionalHeader.SectionAlignment % page_size))
435*ed41a4deSAmine Khaldi         ok( !S(U(image)).ImageMappedFlat, "%u: wrong ImageMappedFlat flags %02x\n", id, U(image).ImageFlags );
436*ed41a4deSAmine Khaldi     else
437*ed41a4deSAmine Khaldi     {
438*ed41a4deSAmine Khaldi         /* winxp doesn't support any of the loader flags */
439*ed41a4deSAmine Khaldi         if (!S(U(image)).ImageMappedFlat) is_winxp = TRUE;
440*ed41a4deSAmine Khaldi         ok( S(U(image)).ImageMappedFlat || broken(is_winxp),
441*ed41a4deSAmine Khaldi         "%u: wrong ImageMappedFlat flags %02x\n", id, U(image).ImageFlags );
442*ed41a4deSAmine Khaldi     }
443*ed41a4deSAmine Khaldi     if (!(nt_header->OptionalHeader.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
444*ed41a4deSAmine Khaldi         ok( !S(U(image)).ImageDynamicallyRelocated || broken( S(U(image)).ComPlusILOnly ), /* <= win7 */
445*ed41a4deSAmine Khaldi             "%u: wrong ImageDynamicallyRelocated flags %02x\n", id, U(image).ImageFlags );
446*ed41a4deSAmine Khaldi     else if (image.ImageContainsCode && !cor_header)
447*ed41a4deSAmine Khaldi         ok( S(U(image)).ImageDynamicallyRelocated || broken(is_winxp),
448*ed41a4deSAmine Khaldi             "%u: wrong ImageDynamicallyRelocated flags %02x\n", id, U(image).ImageFlags );
449*ed41a4deSAmine Khaldi     else
450*ed41a4deSAmine Khaldi         ok( !S(U(image)).ImageDynamicallyRelocated || broken(TRUE), /* <= win8 */
451*ed41a4deSAmine Khaldi             "%u: wrong ImageDynamicallyRelocated flags %02x\n", id, U(image).ImageFlags );
452*ed41a4deSAmine Khaldi     ok( !S(U(image)).BaseBelow4gb, "%u: wrong BaseBelow4gb flags %02x\n", id, U(image).ImageFlags );
453*ed41a4deSAmine Khaldi 
454c2c66affSColin Finck     /* FIXME: needs more work: */
455c2c66affSColin Finck     /* image.GpValue */
456c2c66affSColin Finck 
457c2c66affSColin Finck     map_size.QuadPart = (nt_header->OptionalHeader.SizeOfImage + page_size - 1) & ~(page_size - 1);
458c2c66affSColin Finck     status = pNtQuerySection( mapping, SectionBasicInformation, &info, sizeof(info), NULL );
459c2c66affSColin Finck     ok( !status, "NtQuerySection failed err %x\n", status );
460c2c66affSColin Finck     ok( info.Size.QuadPart == map_size.QuadPart, "NtQuerySection wrong size %x%08x / %x%08x\n",
461c2c66affSColin Finck         info.Size.u.HighPart, info.Size.u.LowPart, map_size.u.HighPart, map_size.u.LowPart );
462c2c66affSColin Finck     CloseHandle( mapping );
463c2c66affSColin Finck 
464c2c66affSColin Finck     map_size.QuadPart = (nt_header->OptionalHeader.SizeOfImage + page_size - 1) & ~(page_size - 1);
465c2c66affSColin Finck     status = pNtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
466c2c66affSColin Finck                                NULL, &map_size, PAGE_READONLY, SEC_IMAGE, file );
467c2c66affSColin Finck     ok( !status, "%u: NtCreateSection failed err %x\n", id, status );
468c2c66affSColin Finck     status = pNtQuerySection( mapping, SectionBasicInformation, &info, sizeof(info), NULL );
469c2c66affSColin Finck     ok( !status, "NtQuerySection failed err %x\n", status );
470c2c66affSColin Finck     ok( info.Size.QuadPart == map_size.QuadPart, "NtQuerySection wrong size %x%08x / %x%08x\n",
471c2c66affSColin Finck         info.Size.u.HighPart, info.Size.u.LowPart, map_size.u.HighPart, map_size.u.LowPart );
472c2c66affSColin Finck     CloseHandle( mapping );
473c2c66affSColin Finck 
474c2c66affSColin Finck     map_size.QuadPart++;
475c2c66affSColin Finck     status = pNtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
476c2c66affSColin Finck                                NULL, &map_size, PAGE_READONLY, SEC_IMAGE, file );
477c2c66affSColin Finck     ok( status == STATUS_SECTION_TOO_BIG, "%u: NtCreateSection failed err %x\n", id, status );
478c2c66affSColin Finck 
479c2c66affSColin Finck     SetFilePointerEx( file, map_size, NULL, FILE_BEGIN );
480c2c66affSColin Finck     SetEndOfFile( file );
481c2c66affSColin Finck     status = pNtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
482c2c66affSColin Finck                                NULL, &map_size, PAGE_READONLY, SEC_IMAGE, file );
483c2c66affSColin Finck     ok( status == STATUS_SECTION_TOO_BIG, "%u: NtCreateSection failed err %x\n", id, status );
484c2c66affSColin Finck 
485c2c66affSColin Finck     map_size.QuadPart = 1;
486c2c66affSColin Finck     status = pNtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
487c2c66affSColin Finck                                NULL, &map_size, PAGE_READONLY, SEC_IMAGE, file );
488c2c66affSColin Finck     ok( !status, "%u: NtCreateSection failed err %x\n", id, status );
489c2c66affSColin Finck     status = pNtQuerySection( mapping, SectionBasicInformation, &info, sizeof(info), NULL );
490c2c66affSColin Finck     ok( !status, "NtQuerySection failed err %x\n", status );
491c2c66affSColin Finck     ok( info.Size.QuadPart == map_size.QuadPart, "NtQuerySection wrong size %x%08x / %x%08x\n",
492c2c66affSColin Finck         info.Size.u.HighPart, info.Size.u.LowPart, map_size.u.HighPart, map_size.u.LowPart );
493c2c66affSColin Finck     CloseHandle( mapping );
494c2c66affSColin Finck 
495c2c66affSColin Finck     CloseHandle( file );
496*ed41a4deSAmine Khaldi     return image.ImageContainsCode && (!cor_header || !(cor_header->Flags & COMIMAGE_FLAGS_ILONLY));
497c2c66affSColin Finck }
498c2c66affSColin Finck 
499c2c66affSColin Finck /* helper to test image section mapping */
map_image_section(const IMAGE_NT_HEADERS * nt_header,const IMAGE_SECTION_HEADER * sections,const void * section_data,int line)500*ed41a4deSAmine Khaldi static NTSTATUS map_image_section( const IMAGE_NT_HEADERS *nt_header, const IMAGE_SECTION_HEADER *sections,
501*ed41a4deSAmine Khaldi                                    const void *section_data, int line )
502c2c66affSColin Finck {
503c2c66affSColin Finck     char dll_name[MAX_PATH];
504c2c66affSColin Finck     LARGE_INTEGER size;
505c2c66affSColin Finck     HANDLE file, map;
506c2c66affSColin Finck     NTSTATUS status;
507c2c66affSColin Finck     ULONG file_size;
508*ed41a4deSAmine Khaldi     BOOL has_code;
509*ed41a4deSAmine Khaldi     HMODULE mod;
510c2c66affSColin Finck 
511*ed41a4deSAmine Khaldi     file_size = create_test_dll_sections( &dos_header, nt_header, sections, section_data, dll_name );
512c2c66affSColin Finck 
513c2c66affSColin Finck     file = CreateFileA(dll_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
514c2c66affSColin Finck     ok(file != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
515c2c66affSColin Finck 
516c2c66affSColin Finck     size.QuadPart = file_size;
517c2c66affSColin Finck     status = pNtCreateSection(&map, STANDARD_RIGHTS_REQUIRED | SECTION_MAP_READ | SECTION_QUERY,
518c2c66affSColin Finck                               NULL, &size, PAGE_READONLY, SEC_IMAGE, file );
519c2c66affSColin Finck     if (!status)
520c2c66affSColin Finck     {
521c2c66affSColin Finck         SECTION_BASIC_INFORMATION info;
522*ed41a4deSAmine Khaldi         SIZE_T info_size = 0xdeadbeef;
523c2c66affSColin Finck         NTSTATUS ret = pNtQuerySection( map, SectionBasicInformation, &info, sizeof(info), &info_size );
524c2c66affSColin Finck         ok( !ret, "NtQuerySection failed err %x\n", ret );
525*ed41a4deSAmine Khaldi         ok( info_size == sizeof(info), "NtQuerySection wrong size %lu\n", info_size );
526c2c66affSColin Finck         ok( info.Attributes == (SEC_IMAGE | SEC_FILE), "NtQuerySection wrong attr %x\n", info.Attributes );
527c2c66affSColin Finck         ok( info.BaseAddress == NULL, "NtQuerySection wrong base %p\n", info.BaseAddress );
528c2c66affSColin Finck         ok( info.Size.QuadPart == file_size, "NtQuerySection wrong size %x%08x / %08x\n",
529c2c66affSColin Finck             info.Size.u.HighPart, info.Size.u.LowPart, file_size );
530*ed41a4deSAmine Khaldi         has_code = query_image_section( line, dll_name, nt_header, section_data );
531*ed41a4deSAmine Khaldi         /* test loading dll of wrong 32/64 bitness */
532*ed41a4deSAmine Khaldi         if (nt_header->OptionalHeader.Magic == (is_win64 ? IMAGE_NT_OPTIONAL_HDR32_MAGIC
533*ed41a4deSAmine Khaldi                                                          : IMAGE_NT_OPTIONAL_HDR64_MAGIC))
534*ed41a4deSAmine Khaldi         {
535*ed41a4deSAmine Khaldi             SetLastError( 0xdeadbeef );
536*ed41a4deSAmine Khaldi             mod = LoadLibraryExA( dll_name, 0, DONT_RESOLVE_DLL_REFERENCES );
537*ed41a4deSAmine Khaldi             if (!has_code && nt_header->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
538*ed41a4deSAmine Khaldi             {
539*ed41a4deSAmine Khaldi                 BOOL il_only = FALSE;
540*ed41a4deSAmine Khaldi                 if (((const IMAGE_NT_HEADERS32 *)nt_header)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress)
541*ed41a4deSAmine Khaldi                 {
542*ed41a4deSAmine Khaldi                     const IMAGE_COR20_HEADER *cor_header = section_data;
543*ed41a4deSAmine Khaldi                     il_only = (cor_header->Flags & COMIMAGE_FLAGS_ILONLY) != 0;
544*ed41a4deSAmine Khaldi                 }
545*ed41a4deSAmine Khaldi                 ok( mod != NULL || broken(il_only), /* <= win7 */
546*ed41a4deSAmine Khaldi                     "%u: loading failed err %u\n", line, GetLastError() );
547*ed41a4deSAmine Khaldi             }
548*ed41a4deSAmine Khaldi             else
549*ed41a4deSAmine Khaldi             {
550*ed41a4deSAmine Khaldi                 ok( !mod, "%u: loading succeeded\n", line );
551*ed41a4deSAmine Khaldi                 ok( GetLastError() == ERROR_BAD_EXE_FORMAT, "%u: wrong error %u\n", line, GetLastError() );
552*ed41a4deSAmine Khaldi             }
553*ed41a4deSAmine Khaldi             if (mod) FreeLibrary( mod );
554*ed41a4deSAmine Khaldi         }
555c2c66affSColin Finck     }
556c2c66affSColin Finck     if (map) CloseHandle( map );
557c2c66affSColin Finck     CloseHandle( file );
558c2c66affSColin Finck     DeleteFileA( dll_name );
559c2c66affSColin Finck     return status;
560c2c66affSColin Finck }
561c2c66affSColin Finck 
562c2c66affSColin Finck 
test_Loader(void)563c2c66affSColin Finck static void test_Loader(void)
564c2c66affSColin Finck {
565c2c66affSColin Finck     static const struct test_data
566c2c66affSColin Finck     {
567c2c66affSColin Finck         DWORD size_of_dos_header;
568c2c66affSColin Finck         WORD number_of_sections, size_of_optional_header;
569c2c66affSColin Finck         DWORD section_alignment, file_alignment;
570c2c66affSColin Finck         DWORD size_of_image, size_of_headers;
571c2c66affSColin Finck         DWORD errors[4]; /* 0 means LoadLibrary should succeed */
572c2c66affSColin Finck     } td[] =
573c2c66affSColin Finck     {
574c2c66affSColin Finck         { sizeof(dos_header),
575c2c66affSColin Finck           1, 0, 0, 0, 0, 0,
576c2c66affSColin Finck           { ERROR_BAD_EXE_FORMAT }
577c2c66affSColin Finck         },
578c2c66affSColin Finck         { sizeof(dos_header),
579c2c66affSColin Finck           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
580c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0xe00,
581c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER),
582c2c66affSColin Finck           { ERROR_BAD_EXE_FORMAT } /* XP doesn't like too small image size */
583c2c66affSColin Finck         },
584c2c66affSColin Finck         { sizeof(dos_header),
585c2c66affSColin Finck           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
586c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
587c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER),
588c2c66affSColin Finck           { ERROR_SUCCESS }
589c2c66affSColin Finck         },
590c2c66affSColin Finck         { sizeof(dos_header),
591c2c66affSColin Finck           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
592c2c66affSColin Finck           0x1f00,
593c2c66affSColin Finck           0x1000,
594c2c66affSColin Finck           { ERROR_SUCCESS }
595c2c66affSColin Finck         },
596c2c66affSColin Finck         { sizeof(dos_header),
597c2c66affSColin Finck           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x200,
598c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x200,
599c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER),
600c2c66affSColin Finck           { ERROR_SUCCESS, ERROR_INVALID_ADDRESS } /* vista is more strict */
601c2c66affSColin Finck         },
602c2c66affSColin Finck         { sizeof(dos_header),
603c2c66affSColin Finck           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x1000,
604c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
605c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER),
606c2c66affSColin Finck           { ERROR_BAD_EXE_FORMAT } /* XP doesn't like alignments */
607c2c66affSColin Finck         },
608c2c66affSColin Finck         { sizeof(dos_header),
609c2c66affSColin Finck           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
610c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
611c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER),
612c2c66affSColin Finck           { ERROR_SUCCESS }
613c2c66affSColin Finck         },
614c2c66affSColin Finck         { sizeof(dos_header),
615c2c66affSColin Finck           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
616c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
617c2c66affSColin Finck           0x200,
618c2c66affSColin Finck           { ERROR_SUCCESS }
619c2c66affSColin Finck         },
620c2c66affSColin Finck         /* Mandatory are all fields up to SizeOfHeaders, everything else
621c2c66affSColin Finck          * is really optional (at least that's true for XP).
622c2c66affSColin Finck          */
623c2c66affSColin Finck #if 0 /* 32-bit Windows 8 crashes inside of LoadLibrary */
624c2c66affSColin Finck         { sizeof(dos_header),
625c2c66affSColin Finck           1, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
626c2c66affSColin Finck           sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER) + 0x10,
627c2c66affSColin Finck           sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER),
628c2c66affSColin Finck           { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT, ERROR_INVALID_ADDRESS,
629c2c66affSColin Finck             ERROR_NOACCESS }
630c2c66affSColin Finck         },
631c2c66affSColin Finck #endif
632c2c66affSColin Finck         { sizeof(dos_header),
633c2c66affSColin Finck           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
634c2c66affSColin Finck           0xd0, /* beyond of the end of file */
635c2c66affSColin Finck           0xc0, /* beyond of the end of file */
636c2c66affSColin Finck           { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
637c2c66affSColin Finck         },
638c2c66affSColin Finck         { sizeof(dos_header),
639c2c66affSColin Finck           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
640c2c66affSColin Finck           0x1000,
641c2c66affSColin Finck           0,
642c2c66affSColin Finck           { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
643c2c66affSColin Finck         },
644c2c66affSColin Finck         { sizeof(dos_header),
645c2c66affSColin Finck           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
646c2c66affSColin Finck           1,
647c2c66affSColin Finck           0,
648c2c66affSColin Finck           { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
649c2c66affSColin Finck         },
650c2c66affSColin Finck #if 0 /* not power of 2 alignments need more test cases */
651c2c66affSColin Finck         { sizeof(dos_header),
652c2c66affSColin Finck           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x300, 0x300,
653c2c66affSColin Finck           1,
654c2c66affSColin Finck           0,
655c2c66affSColin Finck           { ERROR_BAD_EXE_FORMAT } /* alignment is not power of 2 */
656c2c66affSColin Finck         },
657c2c66affSColin Finck #endif
658c2c66affSColin Finck         { sizeof(dos_header),
659c2c66affSColin Finck           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 4, 4,
660c2c66affSColin Finck           1,
661c2c66affSColin Finck           0,
662c2c66affSColin Finck           { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
663c2c66affSColin Finck         },
664c2c66affSColin Finck         { sizeof(dos_header),
665c2c66affSColin Finck           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 1, 1,
666c2c66affSColin Finck           1,
667c2c66affSColin Finck           0,
668c2c66affSColin Finck           { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
669c2c66affSColin Finck         },
670c2c66affSColin Finck         { sizeof(dos_header),
671c2c66affSColin Finck           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
672c2c66affSColin Finck           0,
673c2c66affSColin Finck           0,
674c2c66affSColin Finck           { ERROR_BAD_EXE_FORMAT } /* image size == 0 -> failure */
675c2c66affSColin Finck         },
676c2c66affSColin Finck         /* the following data mimics the PE image which upack creates */
677c2c66affSColin Finck         { 0x10,
678c2c66affSColin Finck           1, 0x148, 0x1000, 0x200,
679c2c66affSColin Finck           sizeof(dos_header) + sizeof(nt_header_template) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
680c2c66affSColin Finck           0x200,
681c2c66affSColin Finck           { ERROR_SUCCESS }
682c2c66affSColin Finck         },
683c2c66affSColin Finck         /* Minimal PE image that XP is able to load: 92 bytes */
684c2c66affSColin Finck         { 0x04,
685c2c66affSColin Finck           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum),
686c2c66affSColin Finck           0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
687c2c66affSColin Finck           1,
688c2c66affSColin Finck           0,
689c2c66affSColin Finck           { ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT } /* vista is more strict */
690c2c66affSColin Finck         },
691c2c66affSColin Finck         /* Minimal PE image that Windows7 is able to load: 268 bytes */
692c2c66affSColin Finck         { 0x04,
693c2c66affSColin Finck           0, 0xf0, /* optional header size just forces 0xf0 bytes to be written,
694c2c66affSColin Finck                       0 or another number don't change the behaviour, what really
695c2c66affSColin Finck                       matters is file size regardless of values in the headers */
696c2c66affSColin Finck           0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
697c2c66affSColin Finck           0x40, /* minimal image size that Windows7 accepts */
698c2c66affSColin Finck           0,
699c2c66affSColin Finck           { ERROR_SUCCESS }
700c2c66affSColin Finck         },
701c2c66affSColin Finck         /* the following data mimics the PE image which 8k demos have */
702c2c66affSColin Finck         { 0x04,
703c2c66affSColin Finck           0, 0x08,
704c2c66affSColin Finck           0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
705c2c66affSColin Finck           0x200000,
706c2c66affSColin Finck           0x40,
707c2c66affSColin Finck           { ERROR_SUCCESS }
708c2c66affSColin Finck         }
709c2c66affSColin Finck     };
710c2c66affSColin Finck     int i;
711c2c66affSColin Finck     DWORD file_size;
7129a436e2dSAmine Khaldi     HANDLE h;
713c2c66affSColin Finck     HMODULE hlib, hlib_as_data_file;
714c2c66affSColin Finck     char dll_name[MAX_PATH];
715c2c66affSColin Finck     SIZE_T size;
716c2c66affSColin Finck     BOOL ret;
717c2c66affSColin Finck     NTSTATUS status;
718*ed41a4deSAmine Khaldi     WORD alt_machine, orig_machine = nt_header_template.FileHeader.Machine;
719c2c66affSColin Finck     IMAGE_NT_HEADERS nt_header;
720*ed41a4deSAmine Khaldi     IMAGE_COR20_HEADER cor_header;
721c2c66affSColin Finck 
722c2c66affSColin Finck     /* prevent displaying of the "Unable to load this DLL" message box */
723c2c66affSColin Finck     SetErrorMode(SEM_FAILCRITICALERRORS);
724c2c66affSColin Finck 
725c2c66affSColin Finck     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
726c2c66affSColin Finck     {
727c2c66affSColin Finck         nt_header = nt_header_template;
728c2c66affSColin Finck         nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
729c2c66affSColin Finck         nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
730c2c66affSColin Finck 
731c2c66affSColin Finck         nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
732c2c66affSColin Finck         nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
733c2c66affSColin Finck         nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
734c2c66affSColin Finck         nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
735c2c66affSColin Finck 
736c2c66affSColin Finck         file_size = create_test_dll( &dos_header, td[i].size_of_dos_header, &nt_header, dll_name );
737c2c66affSColin Finck 
738c2c66affSColin Finck         SetLastError(0xdeadbeef);
739c2c66affSColin Finck         hlib = LoadLibraryA(dll_name);
740c2c66affSColin Finck         if (hlib)
741c2c66affSColin Finck         {
742c2c66affSColin Finck             MEMORY_BASIC_INFORMATION info;
743c2c66affSColin Finck             void *ptr;
744c2c66affSColin Finck 
745c2c66affSColin Finck             ok( td[i].errors[0] == ERROR_SUCCESS, "%d: should have failed\n", i );
746c2c66affSColin Finck 
747c2c66affSColin Finck             SetLastError(0xdeadbeef);
748c2c66affSColin Finck             size = VirtualQuery(hlib, &info, sizeof(info));
749c2c66affSColin Finck             ok(size == sizeof(info),
750c2c66affSColin Finck                 "%d: VirtualQuery error %d\n", i, GetLastError());
751c2c66affSColin Finck             ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
752c2c66affSColin Finck             ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
753c2c66affSColin Finck             ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
754c2c66affSColin Finck             ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "%d: got %lx != expected %x\n",
755c2c66affSColin Finck                i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
756c2c66affSColin Finck             ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
757c2c66affSColin Finck             if (nt_header.OptionalHeader.SectionAlignment < page_size)
758c2c66affSColin Finck                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
759c2c66affSColin Finck             else
760c2c66affSColin Finck                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
761c2c66affSColin Finck             ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
762c2c66affSColin Finck 
763c2c66affSColin Finck             SetLastError(0xdeadbeef);
764c2c66affSColin Finck             ptr = VirtualAlloc(hlib, page_size, MEM_COMMIT, info.Protect);
765c2c66affSColin Finck             ok(!ptr, "%d: VirtualAlloc should fail\n", i);
766c2c66affSColin Finck             ok(GetLastError() == ERROR_ACCESS_DENIED, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
767c2c66affSColin Finck 
768c2c66affSColin Finck             SetLastError(0xdeadbeef);
769c2c66affSColin Finck             size = VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info));
770c2c66affSColin Finck             ok(size == sizeof(info),
771c2c66affSColin Finck                 "%d: VirtualQuery error %d\n", i, GetLastError());
772c2c66affSColin Finck             if (nt_header.OptionalHeader.SectionAlignment == page_size ||
773c2c66affSColin Finck                 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
774c2c66affSColin Finck             {
775c2c66affSColin Finck                 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "%d: got %p != expected %p\n",
776c2c66affSColin Finck                    i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
777c2c66affSColin Finck                 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
778c2c66affSColin Finck                 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
779c2c66affSColin Finck                 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
780c2c66affSColin Finck                 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
781c2c66affSColin Finck                 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
782c2c66affSColin Finck                 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
783c2c66affSColin Finck             }
784c2c66affSColin Finck             else
785c2c66affSColin Finck             {
786c2c66affSColin Finck                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
787c2c66affSColin Finck                 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
788c2c66affSColin Finck                 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
789c2c66affSColin Finck                 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
790c2c66affSColin Finck                 ok(info.RegionSize == ALIGN_SIZE(file_size, page_size), "%d: got %lx != expected %x\n",
791c2c66affSColin Finck                    i, info.RegionSize, ALIGN_SIZE(file_size, page_size));
792c2c66affSColin Finck                 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
793c2c66affSColin Finck                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
794c2c66affSColin Finck                 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
795c2c66affSColin Finck             }
796c2c66affSColin Finck 
797c2c66affSColin Finck             /* header: check the zeroing of alignment */
798c2c66affSColin Finck             if (nt_header.OptionalHeader.SectionAlignment >= page_size)
799c2c66affSColin Finck             {
800c2c66affSColin Finck                 const char *start;
801c2c66affSColin Finck 
802c2c66affSColin Finck                 start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
803c2c66affSColin Finck                 size = ALIGN_SIZE((ULONG_PTR)start, page_size) - (ULONG_PTR)start;
804c2c66affSColin Finck                 ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
805c2c66affSColin Finck             }
806c2c66affSColin Finck 
807c2c66affSColin Finck             if (nt_header.FileHeader.NumberOfSections)
808c2c66affSColin Finck             {
809c2c66affSColin Finck                 SetLastError(0xdeadbeef);
810c2c66affSColin Finck                 size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
811c2c66affSColin Finck                 ok(size == sizeof(info),
812c2c66affSColin Finck                     "%d: VirtualQuery error %d\n", i, GetLastError());
813c2c66affSColin Finck                 if (nt_header.OptionalHeader.SectionAlignment < page_size)
814c2c66affSColin Finck                 {
815c2c66affSColin Finck                     ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
816c2c66affSColin Finck                     ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size), "%d: got %lx != expected %x\n",
817c2c66affSColin Finck                        i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, page_size));
818c2c66affSColin Finck                     ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
819c2c66affSColin Finck                 }
820c2c66affSColin Finck                 else
821c2c66affSColin Finck                 {
822c2c66affSColin Finck                     ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
823c2c66affSColin Finck                     ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, page_size), "%d: got %lx != expected %x\n",
824c2c66affSColin Finck                        i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, page_size));
825c2c66affSColin Finck                     ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
826c2c66affSColin Finck                 }
827c2c66affSColin Finck                 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
828c2c66affSColin Finck                 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
829c2c66affSColin Finck                 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
830c2c66affSColin Finck                 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
831c2c66affSColin Finck 
832c2c66affSColin Finck                 if (nt_header.OptionalHeader.SectionAlignment >= page_size)
833c2c66affSColin Finck                     ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
834c2c66affSColin Finck                 else
835c2c66affSColin Finck                     ok(!memcmp((const char *)hlib + section.PointerToRawData, section_data, section.SizeOfRawData), "wrong section data\n");
836c2c66affSColin Finck 
837c2c66affSColin Finck                 /* check the zeroing of alignment */
838c2c66affSColin Finck                 if (nt_header.OptionalHeader.SectionAlignment >= page_size)
839c2c66affSColin Finck                 {
840c2c66affSColin Finck                     const char *start;
841c2c66affSColin Finck 
842c2c66affSColin Finck                     start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
843c2c66affSColin Finck                     size = ALIGN_SIZE((ULONG_PTR)start, page_size) - (ULONG_PTR)start;
844c2c66affSColin Finck                     ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
845c2c66affSColin Finck                 }
846c2c66affSColin Finck 
847c2c66affSColin Finck                 SetLastError(0xdeadbeef);
848c2c66affSColin Finck                 ptr = VirtualAlloc((char *)hlib + section.VirtualAddress, page_size, MEM_COMMIT, info.Protect);
849c2c66affSColin Finck                 ok(!ptr, "%d: VirtualAlloc should fail\n", i);
850c2c66affSColin Finck                 ok(GetLastError() == ERROR_ACCESS_DENIED || GetLastError() == ERROR_INVALID_ADDRESS,
851c2c66affSColin Finck                    "%d: expected ERROR_ACCESS_DENIED, got %d\n", i, GetLastError());
852c2c66affSColin Finck             }
853c2c66affSColin Finck 
854c2c66affSColin Finck             SetLastError(0xdeadbeef);
855c2c66affSColin Finck             hlib_as_data_file = LoadLibraryExA(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
856c2c66affSColin Finck             ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
857c2c66affSColin Finck             ok(hlib_as_data_file == hlib, "hlib_as_file and hlib are different\n");
858c2c66affSColin Finck 
859c2c66affSColin Finck             SetLastError(0xdeadbeef);
860c2c66affSColin Finck             ret = FreeLibrary(hlib);
861c2c66affSColin Finck             ok(ret, "FreeLibrary error %d\n", GetLastError());
862c2c66affSColin Finck 
863c2c66affSColin Finck             SetLastError(0xdeadbeef);
864c2c66affSColin Finck             hlib = GetModuleHandleA(dll_name);
865c2c66affSColin Finck             ok(hlib != 0, "GetModuleHandle error %u\n", GetLastError());
866c2c66affSColin Finck 
867c2c66affSColin Finck             SetLastError(0xdeadbeef);
868c2c66affSColin Finck             ret = FreeLibrary(hlib_as_data_file);
869c2c66affSColin Finck             ok(ret, "FreeLibrary error %d\n", GetLastError());
870c2c66affSColin Finck 
871c2c66affSColin Finck             hlib = GetModuleHandleA(dll_name);
872c2c66affSColin Finck             ok(!hlib, "GetModuleHandle should fail\n");
873c2c66affSColin Finck 
874c2c66affSColin Finck             SetLastError(0xdeadbeef);
875c2c66affSColin Finck             hlib_as_data_file = LoadLibraryExA(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
876c2c66affSColin Finck             ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
877*ed41a4deSAmine Khaldi             ok(((ULONG_PTR)hlib_as_data_file & 3) == 1, "hlib_as_data_file got %p\n", hlib_as_data_file);
878c2c66affSColin Finck 
879c2c66affSColin Finck             hlib = GetModuleHandleA(dll_name);
880c2c66affSColin Finck             ok(!hlib, "GetModuleHandle should fail\n");
881c2c66affSColin Finck 
882c2c66affSColin Finck             SetLastError(0xdeadbeef);
8839a436e2dSAmine Khaldi             h = CreateFileA( dll_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
8849a436e2dSAmine Khaldi             ok( h != INVALID_HANDLE_VALUE, "open failed err %u\n", GetLastError() );
8859a436e2dSAmine Khaldi             CloseHandle( h );
8869a436e2dSAmine Khaldi 
8879a436e2dSAmine Khaldi             SetLastError(0xdeadbeef);
888c2c66affSColin Finck             ret = FreeLibrary(hlib_as_data_file);
889c2c66affSColin Finck             ok(ret, "FreeLibrary error %d\n", GetLastError());
890c2c66affSColin Finck 
891c2c66affSColin Finck             SetLastError(0xdeadbeef);
8929a436e2dSAmine Khaldi             hlib_as_data_file = LoadLibraryExA(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE);
893*ed41a4deSAmine Khaldi             if (!((ULONG_PTR)hlib_as_data_file & 3) ||  /* winxp */
8949a436e2dSAmine Khaldi                 (!hlib_as_data_file && GetLastError() == ERROR_INVALID_PARAMETER))  /* w2k3 */
8959a436e2dSAmine Khaldi             {
8969a436e2dSAmine Khaldi                 win_skip( "LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE not supported\n" );
8979a436e2dSAmine Khaldi                 FreeLibrary(hlib_as_data_file);
8989a436e2dSAmine Khaldi             }
8999a436e2dSAmine Khaldi             else
9009a436e2dSAmine Khaldi             {
9019a436e2dSAmine Khaldi                 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
9029a436e2dSAmine Khaldi 
9039a436e2dSAmine Khaldi                 SetLastError(0xdeadbeef);
9049a436e2dSAmine Khaldi                 h = CreateFileA( dll_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
905*ed41a4deSAmine Khaldi                 ok( h == INVALID_HANDLE_VALUE, "open succeeded\n" );
906*ed41a4deSAmine Khaldi                 ok( GetLastError() == ERROR_SHARING_VIOLATION, "wrong error %u\n", GetLastError() );
9079a436e2dSAmine Khaldi                 CloseHandle( h );
9089a436e2dSAmine Khaldi 
9099a436e2dSAmine Khaldi                 SetLastError(0xdeadbeef);
910*ed41a4deSAmine Khaldi                 h = CreateFileA( dll_name, GENERIC_READ | DELETE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
911*ed41a4deSAmine Khaldi                 ok( h != INVALID_HANDLE_VALUE, "open failed err %u\n", GetLastError() );
912*ed41a4deSAmine Khaldi                 CloseHandle( h );
913*ed41a4deSAmine Khaldi 
914*ed41a4deSAmine Khaldi                 SetLastError(0xdeadbeef);
915*ed41a4deSAmine Khaldi                 ret = FreeLibrary(hlib_as_data_file);
916*ed41a4deSAmine Khaldi                 ok(ret, "FreeLibrary error %d\n", GetLastError());
917*ed41a4deSAmine Khaldi             }
918*ed41a4deSAmine Khaldi 
919*ed41a4deSAmine Khaldi             SetLastError(0xdeadbeef);
920*ed41a4deSAmine Khaldi             hlib_as_data_file = LoadLibraryExA(dll_name, 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE);
921*ed41a4deSAmine Khaldi             if (!((ULONG_PTR)hlib_as_data_file & 3) ||  /* winxp */
922*ed41a4deSAmine Khaldi                 (!hlib_as_data_file && GetLastError() == ERROR_INVALID_PARAMETER))  /* w2k3 */
923*ed41a4deSAmine Khaldi             {
924*ed41a4deSAmine Khaldi                 win_skip( "LOAD_LIBRARY_AS_IMAGE_RESOURCE not supported\n" );
925*ed41a4deSAmine Khaldi                 FreeLibrary(hlib_as_data_file);
926*ed41a4deSAmine Khaldi             }
927*ed41a4deSAmine Khaldi             else
928*ed41a4deSAmine Khaldi             {
929*ed41a4deSAmine Khaldi                 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
930*ed41a4deSAmine Khaldi                 ok(((ULONG_PTR)hlib_as_data_file & 3) == 2, "hlib_as_data_file got %p\n",
931*ed41a4deSAmine Khaldi                    hlib_as_data_file);
932*ed41a4deSAmine Khaldi 
933*ed41a4deSAmine Khaldi                 hlib = GetModuleHandleA(dll_name);
934*ed41a4deSAmine Khaldi                 ok(!hlib, "GetModuleHandle should fail\n");
935*ed41a4deSAmine Khaldi 
936*ed41a4deSAmine Khaldi                 SetLastError(0xdeadbeef);
9379a436e2dSAmine Khaldi                 ret = FreeLibrary(hlib_as_data_file);
9389a436e2dSAmine Khaldi                 ok(ret, "FreeLibrary error %d\n", GetLastError());
9399a436e2dSAmine Khaldi             }
9409a436e2dSAmine Khaldi 
9419a436e2dSAmine Khaldi             SetLastError(0xdeadbeef);
942c2c66affSColin Finck             ret = DeleteFileA(dll_name);
943c2c66affSColin Finck             ok(ret, "DeleteFile error %d\n", GetLastError());
944c2c66affSColin Finck 
945c2c66affSColin Finck             nt_header.OptionalHeader.AddressOfEntryPoint = 0x12345678;
946c2c66affSColin Finck             file_size = create_test_dll( &dos_header, td[i].size_of_dos_header, &nt_header, dll_name );
947c2c66affSColin Finck             if (!file_size)
948c2c66affSColin Finck             {
949c2c66affSColin Finck                 ok(0, "could not create %s\n", dll_name);
950c2c66affSColin Finck                 break;
951c2c66affSColin Finck             }
952*ed41a4deSAmine Khaldi 
953*ed41a4deSAmine Khaldi             query_image_section( i, dll_name, &nt_header, NULL );
954c2c66affSColin Finck         }
955c2c66affSColin Finck         else
956c2c66affSColin Finck         {
957c2c66affSColin Finck             BOOL error_match;
958c2c66affSColin Finck             int error_index;
959c2c66affSColin Finck 
960c2c66affSColin Finck             error_match = FALSE;
961c2c66affSColin Finck             for (error_index = 0;
962c2c66affSColin Finck                  ! error_match && error_index < sizeof(td[i].errors) / sizeof(DWORD);
963c2c66affSColin Finck                  error_index++)
964c2c66affSColin Finck             {
965c2c66affSColin Finck                 error_match = td[i].errors[error_index] == GetLastError();
966c2c66affSColin Finck             }
967c2c66affSColin Finck             ok(error_match, "%d: unexpected error %d\n", i, GetLastError());
968c2c66affSColin Finck         }
969c2c66affSColin Finck 
970c2c66affSColin Finck         SetLastError(0xdeadbeef);
971c2c66affSColin Finck         ret = DeleteFileA(dll_name);
972c2c66affSColin Finck         ok(ret, "DeleteFile error %d\n", GetLastError());
973c2c66affSColin Finck     }
974c2c66affSColin Finck 
975c2c66affSColin Finck     nt_header = nt_header_template;
976c2c66affSColin Finck     nt_header.FileHeader.NumberOfSections = 1;
977c2c66affSColin Finck     nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
978c2c66affSColin Finck 
979c2c66affSColin Finck     nt_header.OptionalHeader.SectionAlignment = page_size;
980c2c66affSColin Finck     nt_header.OptionalHeader.AddressOfEntryPoint = 0x1234;
981c2c66affSColin Finck     nt_header.OptionalHeader.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
982c2c66affSColin Finck     nt_header.OptionalHeader.FileAlignment = page_size;
983c2c66affSColin Finck     nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER);
984c2c66affSColin Finck     nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + page_size;
985c2c66affSColin Finck 
986*ed41a4deSAmine Khaldi     section.SizeOfRawData = sizeof(section_data);
987*ed41a4deSAmine Khaldi     section.PointerToRawData = page_size;
988*ed41a4deSAmine Khaldi     section.VirtualAddress = page_size;
989*ed41a4deSAmine Khaldi     section.Misc.VirtualSize = page_size;
990*ed41a4deSAmine Khaldi 
991*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, section_data, __LINE__ );
992c2c66affSColin Finck     ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
993c2c66affSColin Finck 
994*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE;
995*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, section_data, __LINE__ );
996*ed41a4deSAmine Khaldi     ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
997*ed41a4deSAmine Khaldi 
998*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.SizeOfCode = 0x1000;
999*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, section_data, __LINE__ );
1000*ed41a4deSAmine Khaldi     ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1001*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.SizeOfCode = 0;
1002*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
1003*ed41a4deSAmine Khaldi 
1004c2c66affSColin Finck     dos_header.e_magic = 0;
1005*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, section_data, __LINE__ );
1006c2c66affSColin Finck     ok( status == STATUS_INVALID_IMAGE_NOT_MZ, "NtCreateSection error %08x\n", status );
1007c2c66affSColin Finck 
1008c2c66affSColin Finck     dos_header.e_magic = IMAGE_DOS_SIGNATURE;
1009c2c66affSColin Finck     nt_header.Signature = IMAGE_OS2_SIGNATURE;
1010*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, section_data, __LINE__ );
1011c2c66affSColin Finck     ok( status == STATUS_INVALID_IMAGE_NE_FORMAT, "NtCreateSection error %08x\n", status );
1012c2c66affSColin Finck 
1013c2c66affSColin Finck     nt_header.Signature = 0xdeadbeef;
1014*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, section_data, __LINE__ );
1015c2c66affSColin Finck     ok( status == STATUS_INVALID_IMAGE_PROTECT, "NtCreateSection error %08x\n", status );
1016c2c66affSColin Finck 
1017c2c66affSColin Finck     nt_header.Signature = IMAGE_NT_SIGNATURE;
1018c2c66affSColin Finck     nt_header.OptionalHeader.Magic = 0xdead;
1019*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, section_data, __LINE__ );
1020c2c66affSColin Finck     ok( status == STATUS_INVALID_IMAGE_FORMAT, "NtCreateSection error %08x\n", status );
1021c2c66affSColin Finck 
1022c2c66affSColin Finck     nt_header.OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
1023c2c66affSColin Finck     nt_header.FileHeader.Machine = 0xdead;
1024*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, section_data, __LINE__ );
1025c2c66affSColin Finck     ok( status == STATUS_INVALID_IMAGE_FORMAT || broken(status == STATUS_SUCCESS), /* win2k */
1026c2c66affSColin Finck         "NtCreateSection error %08x\n", status );
1027c2c66affSColin Finck 
1028c2c66affSColin Finck     nt_header.FileHeader.Machine = IMAGE_FILE_MACHINE_UNKNOWN;
1029*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, section_data, __LINE__ );
1030c2c66affSColin Finck     ok( status == STATUS_INVALID_IMAGE_FORMAT || broken(status == STATUS_SUCCESS), /* win2k */
1031c2c66affSColin Finck         "NtCreateSection error %08x\n", status );
1032c2c66affSColin Finck 
1033c2c66affSColin Finck     switch (orig_machine)
1034c2c66affSColin Finck     {
1035*ed41a4deSAmine Khaldi     case IMAGE_FILE_MACHINE_I386:  alt_machine = IMAGE_FILE_MACHINE_ARMNT; break;
1036*ed41a4deSAmine Khaldi     case IMAGE_FILE_MACHINE_AMD64: alt_machine = IMAGE_FILE_MACHINE_ARM64; break;
1037*ed41a4deSAmine Khaldi     case IMAGE_FILE_MACHINE_ARMNT: alt_machine = IMAGE_FILE_MACHINE_I386; break;
1038*ed41a4deSAmine Khaldi     case IMAGE_FILE_MACHINE_ARM64: alt_machine = IMAGE_FILE_MACHINE_AMD64; break;
1039c2c66affSColin Finck     }
1040*ed41a4deSAmine Khaldi     nt_header.FileHeader.Machine = alt_machine;
1041*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, section_data, __LINE__ );
1042c2c66affSColin Finck     ok( status == STATUS_INVALID_IMAGE_FORMAT || broken(status == STATUS_SUCCESS), /* win2k */
1043c2c66affSColin Finck         "NtCreateSection error %08x\n", status );
1044c2c66affSColin Finck 
1045*ed41a4deSAmine Khaldi     switch (orig_machine)
1046*ed41a4deSAmine Khaldi     {
1047*ed41a4deSAmine Khaldi     case IMAGE_FILE_MACHINE_I386:  alt_machine = IMAGE_FILE_MACHINE_AMD64; break;
1048*ed41a4deSAmine Khaldi     case IMAGE_FILE_MACHINE_AMD64: alt_machine = IMAGE_FILE_MACHINE_I386; break;
1049*ed41a4deSAmine Khaldi     case IMAGE_FILE_MACHINE_ARMNT: alt_machine = IMAGE_FILE_MACHINE_ARM64; break;
1050*ed41a4deSAmine Khaldi     case IMAGE_FILE_MACHINE_ARM64: alt_machine = IMAGE_FILE_MACHINE_ARMNT; break;
1051*ed41a4deSAmine Khaldi     }
1052*ed41a4deSAmine Khaldi     nt_header.FileHeader.Machine = alt_machine;
1053*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, section_data, __LINE__ );
1054*ed41a4deSAmine Khaldi     ok( status == STATUS_INVALID_IMAGE_FORMAT || broken(status == STATUS_SUCCESS), /* win2k */
1055*ed41a4deSAmine Khaldi                   "NtCreateSection error %08x\n", status );
1056*ed41a4deSAmine Khaldi 
1057*ed41a4deSAmine Khaldi     nt_header.FileHeader.Machine = orig_machine;
1058*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1059*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = page_size;
1060*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = sizeof(cor_header);
1061*ed41a4deSAmine Khaldi     section.SizeOfRawData = sizeof(cor_header);
1062*ed41a4deSAmine Khaldi 
1063*ed41a4deSAmine Khaldi     memset( &cor_header, 0, sizeof(cor_header) );
1064*ed41a4deSAmine Khaldi     cor_header.cb = sizeof(cor_header);
1065*ed41a4deSAmine Khaldi     cor_header.MajorRuntimeVersion = 2;
1066*ed41a4deSAmine Khaldi     cor_header.MinorRuntimeVersion = 4;
1067*ed41a4deSAmine Khaldi     cor_header.Flags = COMIMAGE_FLAGS_ILONLY;
1068*ed41a4deSAmine Khaldi     U(cor_header).EntryPointToken = 0xbeef;
1069*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1070*ed41a4deSAmine Khaldi     ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1071*ed41a4deSAmine Khaldi 
1072*ed41a4deSAmine Khaldi     cor_header.MinorRuntimeVersion = 5;
1073*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1074*ed41a4deSAmine Khaldi     ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1075*ed41a4deSAmine Khaldi 
1076*ed41a4deSAmine Khaldi     cor_header.MajorRuntimeVersion = 3;
1077*ed41a4deSAmine Khaldi     cor_header.MinorRuntimeVersion = 0;
1078*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1079*ed41a4deSAmine Khaldi     ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1080*ed41a4deSAmine Khaldi 
1081*ed41a4deSAmine Khaldi     cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITREQUIRED;
1082*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1083*ed41a4deSAmine Khaldi     ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1084*ed41a4deSAmine Khaldi 
1085*ed41a4deSAmine Khaldi     cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITPREFERRED;
1086*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1087*ed41a4deSAmine Khaldi     ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1088*ed41a4deSAmine Khaldi 
1089*ed41a4deSAmine Khaldi     cor_header.Flags = 0;
1090*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1091*ed41a4deSAmine Khaldi     ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1092*ed41a4deSAmine Khaldi 
1093*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = 1;
1094*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = 1;
1095*ed41a4deSAmine Khaldi     status = map_image_section( &nt_header, &section, &cor_header, __LINE__ );
1096*ed41a4deSAmine Khaldi     ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1097*ed41a4deSAmine Khaldi 
1098c2c66affSColin Finck     if (nt_header.OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
1099c2c66affSColin Finck     {
1100c2c66affSColin Finck         IMAGE_NT_HEADERS64 nt64;
1101c2c66affSColin Finck 
1102c2c66affSColin Finck         memset( &nt64, 0, sizeof(nt64) );
1103c2c66affSColin Finck         nt64.Signature = IMAGE_NT_SIGNATURE;
1104c2c66affSColin Finck         nt64.FileHeader.Machine = orig_machine;
1105c2c66affSColin Finck         nt64.FileHeader.NumberOfSections = 1;
1106c2c66affSColin Finck         nt64.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER64);
1107*ed41a4deSAmine Khaldi         nt64.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL;
1108c2c66affSColin Finck         nt64.OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1109c2c66affSColin Finck         nt64.OptionalHeader.MajorLinkerVersion = 1;
1110*ed41a4deSAmine Khaldi         nt64.OptionalHeader.SizeOfCode = 0x1000;
1111*ed41a4deSAmine Khaldi         nt64.OptionalHeader.AddressOfEntryPoint = 0x1000;
1112c2c66affSColin Finck         nt64.OptionalHeader.ImageBase = 0x10000000;
1113*ed41a4deSAmine Khaldi         nt64.OptionalHeader.SectionAlignment = 0x1000;
1114*ed41a4deSAmine Khaldi         nt64.OptionalHeader.FileAlignment = 0x1000;
1115c2c66affSColin Finck         nt64.OptionalHeader.MajorOperatingSystemVersion = 4;
1116c2c66affSColin Finck         nt64.OptionalHeader.MajorImageVersion = 1;
1117c2c66affSColin Finck         nt64.OptionalHeader.MajorSubsystemVersion = 4;
1118c2c66affSColin Finck         nt64.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt64) + sizeof(IMAGE_SECTION_HEADER);
1119c2c66affSColin Finck         nt64.OptionalHeader.SizeOfImage = nt64.OptionalHeader.SizeOfHeaders + 0x1000;
1120c2c66affSColin Finck         nt64.OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
1121*ed41a4deSAmine Khaldi         nt64.OptionalHeader.SizeOfStackReserve = 0x321000;
1122*ed41a4deSAmine Khaldi         nt64.OptionalHeader.SizeOfStackCommit = 0x123000;
1123*ed41a4deSAmine Khaldi         section.Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
1124*ed41a4deSAmine Khaldi 
1125*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1126*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_INVALID_IMAGE_FORMAT : STATUS_INVALID_IMAGE_WIN_64),
1127*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1128*ed41a4deSAmine Khaldi 
1129*ed41a4deSAmine Khaldi         switch (orig_machine)
1130*ed41a4deSAmine Khaldi         {
1131*ed41a4deSAmine Khaldi         case IMAGE_FILE_MACHINE_I386: nt64.FileHeader.Machine = IMAGE_FILE_MACHINE_ARM64; break;
1132*ed41a4deSAmine Khaldi         case IMAGE_FILE_MACHINE_ARMNT: nt64.FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64; break;
1133*ed41a4deSAmine Khaldi         }
1134*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1135*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_INVALID_IMAGE_FORMAT : STATUS_INVALID_IMAGE_WIN_64),
1136*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1137*ed41a4deSAmine Khaldi 
1138*ed41a4deSAmine Khaldi         nt64.FileHeader.Machine = alt_machine;
1139*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1140*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1141*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1142*ed41a4deSAmine Khaldi 
1143*ed41a4deSAmine Khaldi         nt64.OptionalHeader.SizeOfCode = 0;
1144*ed41a4deSAmine Khaldi         nt64.OptionalHeader.AddressOfEntryPoint = 0x1000;
1145*ed41a4deSAmine Khaldi         section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1146*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1147*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1148*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1149*ed41a4deSAmine Khaldi 
1150*ed41a4deSAmine Khaldi         nt64.OptionalHeader.SizeOfCode = 0;
1151*ed41a4deSAmine Khaldi         nt64.OptionalHeader.AddressOfEntryPoint = 0;
1152*ed41a4deSAmine Khaldi         section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE;
1153*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1154*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1155*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1156*ed41a4deSAmine Khaldi 
1157*ed41a4deSAmine Khaldi         nt64.OptionalHeader.SizeOfCode = 0x1000;
1158*ed41a4deSAmine Khaldi         nt64.OptionalHeader.AddressOfEntryPoint = 0;
1159*ed41a4deSAmine Khaldi         nt64.OptionalHeader.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE;
1160*ed41a4deSAmine Khaldi         section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1161*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1162*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1163*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1164*ed41a4deSAmine Khaldi 
1165*ed41a4deSAmine Khaldi         nt64.OptionalHeader.SizeOfCode = 0;
1166*ed41a4deSAmine Khaldi         nt64.OptionalHeader.AddressOfEntryPoint = 0;
1167*ed41a4deSAmine Khaldi         section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1168*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, section_data, __LINE__ );
1169*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1170*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1171*ed41a4deSAmine Khaldi 
1172*ed41a4deSAmine Khaldi         nt64.OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1173*ed41a4deSAmine Khaldi         nt64.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = page_size;
1174*ed41a4deSAmine Khaldi         nt64.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = sizeof(cor_header);
1175*ed41a4deSAmine Khaldi         cor_header.MajorRuntimeVersion = 2;
1176*ed41a4deSAmine Khaldi         cor_header.MinorRuntimeVersion = 4;
1177*ed41a4deSAmine Khaldi         cor_header.Flags = COMIMAGE_FLAGS_ILONLY;
1178*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1179*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1180*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1181*ed41a4deSAmine Khaldi 
1182*ed41a4deSAmine Khaldi         nt64.OptionalHeader.SizeOfCode = 0x1000;
1183*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1184*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1185*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1186*ed41a4deSAmine Khaldi 
1187*ed41a4deSAmine Khaldi         cor_header.MinorRuntimeVersion = 5;
1188*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1189*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1190*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1191*ed41a4deSAmine Khaldi 
1192*ed41a4deSAmine Khaldi         cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITREQUIRED;
1193*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1194*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1195*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1196*ed41a4deSAmine Khaldi 
1197*ed41a4deSAmine Khaldi         cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITPREFERRED;
1198*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1199*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1200*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1201*ed41a4deSAmine Khaldi 
1202*ed41a4deSAmine Khaldi         cor_header.Flags = 0;
1203*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1204*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1205*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1206*ed41a4deSAmine Khaldi 
1207*ed41a4deSAmine Khaldi         nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = 1;
1208*ed41a4deSAmine Khaldi         nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = 1;
1209*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt64, &section, &cor_header, __LINE__ );
1210*ed41a4deSAmine Khaldi         ok( status == (is_wow64 ? STATUS_SUCCESS : STATUS_INVALID_IMAGE_WIN_64),
1211*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1212c2c66affSColin Finck     }
1213c2c66affSColin Finck     else
1214c2c66affSColin Finck     {
1215c2c66affSColin Finck         IMAGE_NT_HEADERS32 nt32;
1216c2c66affSColin Finck 
1217c2c66affSColin Finck         memset( &nt32, 0, sizeof(nt32) );
1218c2c66affSColin Finck         nt32.Signature = IMAGE_NT_SIGNATURE;
1219c2c66affSColin Finck         nt32.FileHeader.Machine = orig_machine;
1220c2c66affSColin Finck         nt32.FileHeader.NumberOfSections = 1;
1221c2c66affSColin Finck         nt32.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER32);
1222*ed41a4deSAmine Khaldi         nt32.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL;
1223c2c66affSColin Finck         nt32.OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR32_MAGIC;
1224c2c66affSColin Finck         nt32.OptionalHeader.MajorLinkerVersion = 1;
1225*ed41a4deSAmine Khaldi         nt32.OptionalHeader.SizeOfCode = 0x1000;
1226*ed41a4deSAmine Khaldi         nt32.OptionalHeader.AddressOfEntryPoint = 0x1000;
1227c2c66affSColin Finck         nt32.OptionalHeader.ImageBase = 0x10000000;
1228*ed41a4deSAmine Khaldi         nt32.OptionalHeader.SectionAlignment = 0x1000;
1229*ed41a4deSAmine Khaldi         nt32.OptionalHeader.FileAlignment = 0x1000;
1230c2c66affSColin Finck         nt32.OptionalHeader.MajorOperatingSystemVersion = 4;
1231c2c66affSColin Finck         nt32.OptionalHeader.MajorImageVersion = 1;
1232c2c66affSColin Finck         nt32.OptionalHeader.MajorSubsystemVersion = 4;
1233c2c66affSColin Finck         nt32.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt32) + sizeof(IMAGE_SECTION_HEADER);
1234c2c66affSColin Finck         nt32.OptionalHeader.SizeOfImage = nt32.OptionalHeader.SizeOfHeaders + 0x1000;
1235c2c66affSColin Finck         nt32.OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
1236*ed41a4deSAmine Khaldi         nt32.OptionalHeader.SizeOfStackReserve = 0x321000;
1237*ed41a4deSAmine Khaldi         nt32.OptionalHeader.SizeOfStackCommit = 0x123000;
1238*ed41a4deSAmine Khaldi         section.Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
1239*ed41a4deSAmine Khaldi 
1240*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1241c2c66affSColin Finck         ok( status == STATUS_INVALID_IMAGE_FORMAT, "NtCreateSection error %08x\n", status );
1242*ed41a4deSAmine Khaldi 
1243*ed41a4deSAmine Khaldi         switch (orig_machine)
1244*ed41a4deSAmine Khaldi         {
1245*ed41a4deSAmine Khaldi         case IMAGE_FILE_MACHINE_AMD64: nt32.FileHeader.Machine = IMAGE_FILE_MACHINE_ARMNT; break;
1246*ed41a4deSAmine Khaldi         case IMAGE_FILE_MACHINE_ARM64: nt32.FileHeader.Machine = IMAGE_FILE_MACHINE_I386; break;
1247*ed41a4deSAmine Khaldi         }
1248*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1249*ed41a4deSAmine Khaldi         ok( status == STATUS_INVALID_IMAGE_FORMAT || broken(!status) /* win8 */,
1250*ed41a4deSAmine Khaldi             "NtCreateSection error %08x\n", status );
1251*ed41a4deSAmine Khaldi 
1252*ed41a4deSAmine Khaldi         nt32.FileHeader.Machine = alt_machine;
1253*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1254*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1255*ed41a4deSAmine Khaldi 
1256*ed41a4deSAmine Khaldi         nt32.OptionalHeader.SizeOfCode = 0;
1257*ed41a4deSAmine Khaldi         nt32.OptionalHeader.AddressOfEntryPoint = 0x1000;
1258*ed41a4deSAmine Khaldi         section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1259*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1260*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1261*ed41a4deSAmine Khaldi 
1262*ed41a4deSAmine Khaldi         nt32.OptionalHeader.SizeOfCode = 0;
1263*ed41a4deSAmine Khaldi         nt32.OptionalHeader.AddressOfEntryPoint = 0;
1264*ed41a4deSAmine Khaldi         section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE;
1265*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1266*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1267*ed41a4deSAmine Khaldi 
1268*ed41a4deSAmine Khaldi         nt32.OptionalHeader.SizeOfCode = 0x1000;
1269*ed41a4deSAmine Khaldi         nt32.OptionalHeader.AddressOfEntryPoint = 0;
1270*ed41a4deSAmine Khaldi         nt32.OptionalHeader.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE;
1271*ed41a4deSAmine Khaldi         section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1272*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1273*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1274*ed41a4deSAmine Khaldi 
1275*ed41a4deSAmine Khaldi         nt32.OptionalHeader.SizeOfCode = 0;
1276*ed41a4deSAmine Khaldi         nt32.OptionalHeader.AddressOfEntryPoint = 0;
1277*ed41a4deSAmine Khaldi         section.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;
1278*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, section_data, __LINE__ );
1279*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1280*ed41a4deSAmine Khaldi 
1281*ed41a4deSAmine Khaldi         nt32.OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
1282*ed41a4deSAmine Khaldi         nt32.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = page_size;
1283*ed41a4deSAmine Khaldi         nt32.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = sizeof(cor_header);
1284*ed41a4deSAmine Khaldi         cor_header.MajorRuntimeVersion = 2;
1285*ed41a4deSAmine Khaldi         cor_header.MinorRuntimeVersion = 4;
1286*ed41a4deSAmine Khaldi         cor_header.Flags = COMIMAGE_FLAGS_ILONLY;
1287*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1288*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1289*ed41a4deSAmine Khaldi 
1290*ed41a4deSAmine Khaldi         nt32.OptionalHeader.SizeOfCode = 0x1000;
1291*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1292*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1293*ed41a4deSAmine Khaldi 
1294*ed41a4deSAmine Khaldi         cor_header.MinorRuntimeVersion = 5;
1295*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1296*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1297*ed41a4deSAmine Khaldi 
1298*ed41a4deSAmine Khaldi         cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITREQUIRED;
1299*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1300*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1301*ed41a4deSAmine Khaldi 
1302*ed41a4deSAmine Khaldi         cor_header.Flags = COMIMAGE_FLAGS_ILONLY | COMIMAGE_FLAGS_32BITPREFERRED;
1303*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1304*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1305*ed41a4deSAmine Khaldi 
1306*ed41a4deSAmine Khaldi         cor_header.Flags = 0;
1307*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1308*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1309*ed41a4deSAmine Khaldi 
1310*ed41a4deSAmine Khaldi         nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = 1;
1311*ed41a4deSAmine Khaldi         nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = 1;
1312*ed41a4deSAmine Khaldi         status = map_image_section( (IMAGE_NT_HEADERS *)&nt32, &section, &cor_header, __LINE__ );
1313*ed41a4deSAmine Khaldi         ok( status == STATUS_SUCCESS, "NtCreateSection error %08x\n", status );
1314c2c66affSColin Finck     }
1315c2c66affSColin Finck 
1316*ed41a4deSAmine Khaldi     section.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
1317*ed41a4deSAmine Khaldi }
1318*ed41a4deSAmine Khaldi 
test_filenames(void)1319*ed41a4deSAmine Khaldi static void test_filenames(void)
1320*ed41a4deSAmine Khaldi {
1321*ed41a4deSAmine Khaldi     IMAGE_NT_HEADERS nt_header = nt_header_template;
1322*ed41a4deSAmine Khaldi     char dll_name[MAX_PATH], long_path[MAX_PATH], short_path[MAX_PATH], buffer[MAX_PATH];
1323*ed41a4deSAmine Khaldi     HMODULE mod, mod2;
1324*ed41a4deSAmine Khaldi     BOOL ret;
1325*ed41a4deSAmine Khaldi 
1326*ed41a4deSAmine Khaldi     nt_header.FileHeader.NumberOfSections = 1;
1327*ed41a4deSAmine Khaldi     nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
1328*ed41a4deSAmine Khaldi 
1329*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.SectionAlignment = page_size;
1330*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
1331*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.FileAlignment = page_size;
1332*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER);
1333*ed41a4deSAmine Khaldi     nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + page_size;
1334*ed41a4deSAmine Khaldi 
1335*ed41a4deSAmine Khaldi     create_test_dll( &dos_header, sizeof(dos_header), &nt_header, dll_name );
1336*ed41a4deSAmine Khaldi     strcpy( long_path, dll_name );
1337*ed41a4deSAmine Khaldi     strcpy( strrchr( long_path, '\\' ), "\\this-is-a-long-name.dll" );
1338*ed41a4deSAmine Khaldi     ret = MoveFileA( dll_name, long_path );
1339*ed41a4deSAmine Khaldi     ok( ret, "MoveFileA failed err %u\n", GetLastError() );
1340*ed41a4deSAmine Khaldi     GetShortPathNameA( long_path, short_path, MAX_PATH );
1341*ed41a4deSAmine Khaldi 
1342*ed41a4deSAmine Khaldi     mod = LoadLibraryA( short_path );
1343*ed41a4deSAmine Khaldi     ok( mod != NULL, "loading failed err %u\n", GetLastError() );
1344*ed41a4deSAmine Khaldi     GetModuleFileNameA( mod, buffer, MAX_PATH );
1345*ed41a4deSAmine Khaldi     ok( !lstrcmpiA( buffer, short_path ), "got wrong path %s / %s\n", buffer, short_path );
1346*ed41a4deSAmine Khaldi     mod2 = GetModuleHandleA( short_path );
1347*ed41a4deSAmine Khaldi     ok( mod == mod2, "wrong module %p for %s\n", mod2, short_path );
1348*ed41a4deSAmine Khaldi     mod2 = GetModuleHandleA( long_path );
1349*ed41a4deSAmine Khaldi     ok( mod == mod2, "wrong module %p for %s\n", mod2, long_path );
1350*ed41a4deSAmine Khaldi     mod2 = LoadLibraryA( long_path );
1351*ed41a4deSAmine Khaldi     ok( mod2 != NULL, "loading failed err %u\n", GetLastError() );
1352*ed41a4deSAmine Khaldi     ok( mod == mod2, "library loaded twice\n" );
1353*ed41a4deSAmine Khaldi     GetModuleFileNameA( mod2, buffer, MAX_PATH );
1354*ed41a4deSAmine Khaldi     ok( !lstrcmpiA( buffer, short_path ), "got wrong path %s / %s\n", buffer, short_path );
1355*ed41a4deSAmine Khaldi     FreeLibrary( mod2 );
1356*ed41a4deSAmine Khaldi     FreeLibrary( mod );
1357*ed41a4deSAmine Khaldi 
1358*ed41a4deSAmine Khaldi     mod = LoadLibraryA( long_path );
1359*ed41a4deSAmine Khaldi     ok( mod != NULL, "loading failed err %u\n", GetLastError() );
1360*ed41a4deSAmine Khaldi     GetModuleFileNameA( mod, buffer, MAX_PATH );
1361*ed41a4deSAmine Khaldi     ok( !lstrcmpiA( buffer, long_path ), "got wrong path %s / %s\n", buffer, long_path );
1362*ed41a4deSAmine Khaldi     mod2 = GetModuleHandleA( short_path );
1363*ed41a4deSAmine Khaldi     ok( mod == mod2, "wrong module %p for %s\n", mod2, short_path );
1364*ed41a4deSAmine Khaldi     mod2 = GetModuleHandleA( long_path );
1365*ed41a4deSAmine Khaldi     ok( mod == mod2, "wrong module %p for %s\n", mod2, long_path );
1366*ed41a4deSAmine Khaldi     mod2 = LoadLibraryA( short_path );
1367*ed41a4deSAmine Khaldi     ok( mod2 != NULL, "loading failed err %u\n", GetLastError() );
1368*ed41a4deSAmine Khaldi     ok( mod == mod2, "library loaded twice\n" );
1369*ed41a4deSAmine Khaldi     GetModuleFileNameA( mod2, buffer, MAX_PATH );
1370*ed41a4deSAmine Khaldi     ok( !lstrcmpiA( buffer, long_path ), "got wrong path %s / %s\n", buffer, long_path );
1371*ed41a4deSAmine Khaldi     FreeLibrary( mod2 );
1372*ed41a4deSAmine Khaldi     FreeLibrary( mod );
1373*ed41a4deSAmine Khaldi 
1374*ed41a4deSAmine Khaldi     strcpy( dll_name, long_path );
1375*ed41a4deSAmine Khaldi     strcpy( strrchr( dll_name, '\\' ), "\\this-is-another-name.dll" );
1376*ed41a4deSAmine Khaldi     ret = CreateHardLinkA( dll_name, long_path, NULL );
1377*ed41a4deSAmine Khaldi     ok( ret, "CreateHardLinkA failed err %u\n", GetLastError() );
1378*ed41a4deSAmine Khaldi     if (ret)
1379*ed41a4deSAmine Khaldi     {
1380*ed41a4deSAmine Khaldi         mod = LoadLibraryA( dll_name );
1381*ed41a4deSAmine Khaldi         ok( mod != NULL, "loading failed err %u\n", GetLastError() );
1382*ed41a4deSAmine Khaldi         GetModuleFileNameA( mod, buffer, MAX_PATH );
1383*ed41a4deSAmine Khaldi         ok( !lstrcmpiA( buffer, dll_name ), "got wrong path %s / %s\n", buffer, dll_name );
1384*ed41a4deSAmine Khaldi         mod2 = GetModuleHandleA( long_path );
1385*ed41a4deSAmine Khaldi         ok( mod == mod2, "wrong module %p for %s\n", mod2, long_path );
1386*ed41a4deSAmine Khaldi         mod2 = LoadLibraryA( long_path );
1387*ed41a4deSAmine Khaldi         ok( mod2 != NULL, "loading failed err %u\n", GetLastError() );
1388*ed41a4deSAmine Khaldi         ok( mod == mod2, "library loaded twice\n" );
1389*ed41a4deSAmine Khaldi         GetModuleFileNameA( mod2, buffer, MAX_PATH );
1390*ed41a4deSAmine Khaldi         ok( !lstrcmpiA( buffer, dll_name ), "got wrong path %s / %s\n", buffer, short_path );
1391*ed41a4deSAmine Khaldi         FreeLibrary( mod2 );
1392*ed41a4deSAmine Khaldi         FreeLibrary( mod );
1393*ed41a4deSAmine Khaldi         DeleteFileA( dll_name );
1394*ed41a4deSAmine Khaldi     }
1395*ed41a4deSAmine Khaldi     DeleteFileA( long_path );
1396c2c66affSColin Finck }
1397c2c66affSColin Finck 
test_FakeDLL(void)1398c2c66affSColin Finck static void test_FakeDLL(void)
1399c2c66affSColin Finck {
14009a436e2dSAmine Khaldi #if defined(__i386__) || defined(__x86_64__)
1401c2c66affSColin Finck     NTSTATUS (WINAPI *pNtSetEvent)(HANDLE, ULONG *) = NULL;
1402c2c66affSColin Finck     IMAGE_EXPORT_DIRECTORY *dir;
1403c2c66affSColin Finck     HMODULE module = GetModuleHandleA("ntdll.dll");
1404c2c66affSColin Finck     HANDLE file, map, event;
1405c2c66affSColin Finck     WCHAR path[MAX_PATH];
1406c2c66affSColin Finck     DWORD *names, *funcs;
1407c2c66affSColin Finck     WORD *ordinals;
1408c2c66affSColin Finck     ULONG size;
1409c2c66affSColin Finck     void *ptr;
1410c2c66affSColin Finck     int i;
1411c2c66affSColin Finck 
1412c2c66affSColin Finck     GetModuleFileNameW(module, path, MAX_PATH);
1413c2c66affSColin Finck 
1414c2c66affSColin Finck     file = CreateFileW(path, GENERIC_READ | GENERIC_EXECUTE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1415c2c66affSColin Finck     ok(file != INVALID_HANDLE_VALUE, "Failed to open %s (error %u)\n", wine_dbgstr_w(path), GetLastError());
1416c2c66affSColin Finck 
1417c2c66affSColin Finck     map = CreateFileMappingW(file, NULL, PAGE_EXECUTE_READ | SEC_IMAGE, 0, 0, NULL);
1418c2c66affSColin Finck     ok(map != NULL, "CreateFileMapping failed with error %u\n", GetLastError());
1419c2c66affSColin Finck     ptr = MapViewOfFile(map, FILE_MAP_READ | FILE_MAP_EXECUTE, 0, 0, 0);
1420c2c66affSColin Finck     ok(ptr != NULL, "MapViewOfFile failed with error %u\n", GetLastError());
1421c2c66affSColin Finck 
1422c2c66affSColin Finck     dir = RtlImageDirectoryEntryToData(ptr, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &size);
1423c2c66affSColin Finck     ok(dir != NULL, "RtlImageDirectoryEntryToData failed\n");
1424c2c66affSColin Finck 
1425c2c66affSColin Finck     names    = RVAToAddr(dir->AddressOfNames, ptr);
1426c2c66affSColin Finck     ordinals = RVAToAddr(dir->AddressOfNameOrdinals, ptr);
1427c2c66affSColin Finck     funcs    = RVAToAddr(dir->AddressOfFunctions, ptr);
1428c2c66affSColin Finck     ok(dir->NumberOfNames > 0, "Could not find any exported functions\n");
1429c2c66affSColin Finck 
1430c2c66affSColin Finck     for (i = 0; i < dir->NumberOfNames; i++)
1431c2c66affSColin Finck     {
1432c2c66affSColin Finck         DWORD map_rva, dll_rva, map_offset, dll_offset;
1433c2c66affSColin Finck         char *func_name = RVAToAddr(names[i], ptr);
1434c2c66affSColin Finck         BYTE *dll_func, *map_func;
1435c2c66affSColin Finck 
1436c2c66affSColin Finck         /* check only Nt functions for now */
1437c2c66affSColin Finck         if (strncmp(func_name, "Zw", 2) && strncmp(func_name, "Nt", 2))
1438c2c66affSColin Finck             continue;
1439c2c66affSColin Finck 
1440c2c66affSColin Finck         dll_func = (BYTE *)GetProcAddress(module, func_name);
1441c2c66affSColin Finck         ok(dll_func != NULL, "%s: GetProcAddress returned NULL\n", func_name);
14429a436e2dSAmine Khaldi #if defined(__i386__)
1443c2c66affSColin Finck         if (dll_func[0] == 0x90 && dll_func[1] == 0x90 &&
1444c2c66affSColin Finck             dll_func[2] == 0x90 && dll_func[3] == 0x90)
14459a436e2dSAmine Khaldi #elif defined(__x86_64__)
14469a436e2dSAmine Khaldi         if (dll_func[0] == 0x48 && dll_func[1] == 0x83 &&
14479a436e2dSAmine Khaldi             dll_func[2] == 0xec && dll_func[3] == 0x08)
14489a436e2dSAmine Khaldi #endif
1449c2c66affSColin Finck         {
1450c2c66affSColin Finck             todo_wine ok(0, "%s: Export is a stub-function, skipping\n", func_name);
1451c2c66affSColin Finck             continue;
1452c2c66affSColin Finck         }
1453c2c66affSColin Finck 
1454c2c66affSColin Finck         /* check position in memory */
1455c2c66affSColin Finck         dll_rva = (DWORD_PTR)dll_func - (DWORD_PTR)module;
1456c2c66affSColin Finck         map_rva = funcs[ordinals[i]];
1457c2c66affSColin Finck         ok(map_rva == dll_rva, "%s: Rva of mapped function (0x%x) does not match dll (0x%x)\n",
1458c2c66affSColin Finck            func_name, dll_rva, map_rva);
1459c2c66affSColin Finck 
1460c2c66affSColin Finck         /* check position in file */
1461c2c66affSColin Finck         map_offset = (DWORD_PTR)RtlImageRvaToVa(RtlImageNtHeader(ptr),    ptr,    map_rva, NULL) - (DWORD_PTR)ptr;
1462c2c66affSColin Finck         dll_offset = (DWORD_PTR)RtlImageRvaToVa(RtlImageNtHeader(module), module, dll_rva, NULL) - (DWORD_PTR)module;
1463c2c66affSColin Finck         ok(map_offset == dll_offset, "%s: File offset of mapped function (0x%x) does not match dll (0x%x)\n",
1464c2c66affSColin Finck            func_name, map_offset, dll_offset);
1465c2c66affSColin Finck 
1466c2c66affSColin Finck         /* check function content */
1467c2c66affSColin Finck         map_func = RVAToAddr(map_rva, ptr);
1468c2c66affSColin Finck         ok(!memcmp(map_func, dll_func, 0x20), "%s: Function content does not match!\n", func_name);
1469c2c66affSColin Finck 
1470c2c66affSColin Finck         if (!strcmp(func_name, "NtSetEvent"))
1471c2c66affSColin Finck             pNtSetEvent = (void *)map_func;
1472c2c66affSColin Finck     }
1473c2c66affSColin Finck 
1474c2c66affSColin Finck     ok(pNtSetEvent != NULL, "Could not find NtSetEvent export\n");
1475c2c66affSColin Finck     if (pNtSetEvent)
1476c2c66affSColin Finck     {
1477c2c66affSColin Finck         event = CreateEventA(NULL, TRUE, FALSE, NULL);
1478c2c66affSColin Finck         ok(event != NULL, "CreateEvent failed with error %u\n", GetLastError());
1479c2c66affSColin Finck         pNtSetEvent(event, 0);
1480c2c66affSColin Finck         ok(WaitForSingleObject(event, 0) == WAIT_OBJECT_0, "Event was not signaled\n");
1481c2c66affSColin Finck         pNtSetEvent(event, 0);
1482c2c66affSColin Finck         ok(WaitForSingleObject(event, 0) == WAIT_OBJECT_0, "Event was not signaled\n");
1483c2c66affSColin Finck         CloseHandle(event);
1484c2c66affSColin Finck     }
1485c2c66affSColin Finck 
1486c2c66affSColin Finck     UnmapViewOfFile(ptr);
1487c2c66affSColin Finck     CloseHandle(map);
1488c2c66affSColin Finck     CloseHandle(file);
1489c2c66affSColin Finck #endif
1490c2c66affSColin Finck }
1491c2c66affSColin Finck 
1492c2c66affSColin Finck /* Verify linking style of import descriptors */
test_ImportDescriptors(void)1493c2c66affSColin Finck static void test_ImportDescriptors(void)
1494c2c66affSColin Finck {
1495c2c66affSColin Finck     HMODULE kernel32_module = NULL;
1496c2c66affSColin Finck     PIMAGE_DOS_HEADER d_header;
1497c2c66affSColin Finck     PIMAGE_NT_HEADERS nt_headers;
1498c2c66affSColin Finck     DWORD import_dir_size;
1499c2c66affSColin Finck     DWORD_PTR dir_offset;
1500c2c66affSColin Finck     PIMAGE_IMPORT_DESCRIPTOR import_chunk;
1501c2c66affSColin Finck 
1502c2c66affSColin Finck     /* Load kernel32 module */
1503c2c66affSColin Finck     kernel32_module = GetModuleHandleA("kernel32.dll");
1504c2c66affSColin Finck     assert( kernel32_module != NULL );
1505c2c66affSColin Finck 
1506c2c66affSColin Finck     /* Get PE header info from module image */
1507c2c66affSColin Finck     d_header = (PIMAGE_DOS_HEADER) kernel32_module;
1508c2c66affSColin Finck     nt_headers = (PIMAGE_NT_HEADERS) (((char*) d_header) +
1509c2c66affSColin Finck             d_header->e_lfanew);
1510c2c66affSColin Finck 
1511c2c66affSColin Finck     /* Get size of import entry directory */
1512c2c66affSColin Finck     import_dir_size = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
1513c2c66affSColin Finck     if (!import_dir_size)
1514c2c66affSColin Finck     {
1515c2c66affSColin Finck         skip("Unable to continue testing due to missing import directory.\n");
1516c2c66affSColin Finck         return;
1517c2c66affSColin Finck     }
1518c2c66affSColin Finck 
1519c2c66affSColin Finck     /* Get address of first import chunk */
1520c2c66affSColin Finck     dir_offset = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
1521c2c66affSColin Finck     import_chunk = RVAToAddr(dir_offset, kernel32_module);
1522c2c66affSColin Finck     ok(import_chunk != 0, "Invalid import_chunk: %p\n", import_chunk);
1523c2c66affSColin Finck     if (!import_chunk) return;
1524c2c66affSColin Finck 
1525c2c66affSColin Finck     /* Iterate through import descriptors and verify set name,
1526c2c66affSColin Finck      * OriginalFirstThunk, and FirstThunk.  Core Windows DLLs, such as
1527c2c66affSColin Finck      * kernel32.dll, don't use Borland-style linking, where the table of
1528c2c66affSColin Finck      * imported names is stored directly in FirstThunk and overwritten
1529c2c66affSColin Finck      * by the relocation, instead of being stored in OriginalFirstThunk.
1530c2c66affSColin Finck      * */
1531c2c66affSColin Finck     for (; import_chunk->FirstThunk; import_chunk++)
1532c2c66affSColin Finck     {
1533c2c66affSColin Finck         LPCSTR module_name = RVAToAddr(import_chunk->Name, kernel32_module);
1534c2c66affSColin Finck         PIMAGE_THUNK_DATA name_table = RVAToAddr(
1535c2c66affSColin Finck                 U(*import_chunk).OriginalFirstThunk, kernel32_module);
1536c2c66affSColin Finck         PIMAGE_THUNK_DATA iat = RVAToAddr(
1537c2c66affSColin Finck                 import_chunk->FirstThunk, kernel32_module);
1538c2c66affSColin Finck         ok(module_name != NULL, "Imported module name should not be NULL\n");
1539c2c66affSColin Finck         ok(name_table != NULL,
1540c2c66affSColin Finck                 "Name table for imported module %s should not be NULL\n",
1541c2c66affSColin Finck                 module_name);
1542c2c66affSColin Finck         ok(iat != NULL, "IAT for imported module %s should not be NULL\n",
1543c2c66affSColin Finck                 module_name);
1544c2c66affSColin Finck     }
1545c2c66affSColin Finck }
1546c2c66affSColin Finck 
test_image_mapping(const char * dll_name,DWORD scn_page_access,BOOL is_dll)1547c2c66affSColin Finck static void test_image_mapping(const char *dll_name, DWORD scn_page_access, BOOL is_dll)
1548c2c66affSColin Finck {
1549c2c66affSColin Finck     HANDLE hfile, hmap;
1550c2c66affSColin Finck     NTSTATUS status;
1551c2c66affSColin Finck     LARGE_INTEGER offset;
1552c2c66affSColin Finck     SIZE_T size;
1553c2c66affSColin Finck     void *addr1, *addr2;
1554c2c66affSColin Finck     MEMORY_BASIC_INFORMATION info;
1555c2c66affSColin Finck 
1556c2c66affSColin Finck     if (!pNtMapViewOfSection) return;
1557c2c66affSColin Finck 
1558c2c66affSColin Finck     SetLastError(0xdeadbeef);
1559c2c66affSColin Finck     hfile = CreateFileA(dll_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
1560c2c66affSColin Finck     ok(hfile != INVALID_HANDLE_VALUE, "CreateFile error %d\n", GetLastError());
1561c2c66affSColin Finck 
1562c2c66affSColin Finck     SetLastError(0xdeadbeef);
1563c2c66affSColin Finck     hmap = CreateFileMappingW(hfile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, 0);
1564c2c66affSColin Finck     ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
1565c2c66affSColin Finck 
1566c2c66affSColin Finck     offset.u.LowPart  = 0;
1567c2c66affSColin Finck     offset.u.HighPart = 0;
1568c2c66affSColin Finck 
1569c2c66affSColin Finck     addr1 = NULL;
1570c2c66affSColin Finck     size = 0;
1571c2c66affSColin Finck     status = pNtMapViewOfSection(hmap, GetCurrentProcess(), &addr1, 0, 0, &offset,
1572c2c66affSColin Finck                                  &size, 1 /* ViewShare */, 0, PAGE_READONLY);
1573c2c66affSColin Finck     ok(status == STATUS_SUCCESS, "NtMapViewOfSection error %x\n", status);
1574c2c66affSColin Finck     ok(addr1 != 0, "mapped address should be valid\n");
1575c2c66affSColin Finck 
1576c2c66affSColin Finck     SetLastError(0xdeadbeef);
1577c2c66affSColin Finck     size = VirtualQuery((char *)addr1 + section.VirtualAddress, &info, sizeof(info));
1578c2c66affSColin Finck     ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
1579c2c66affSColin Finck     ok(info.BaseAddress == (char *)addr1 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr1 + section.VirtualAddress);
1580c2c66affSColin Finck     ok(info.RegionSize == page_size, "got %#lx != expected %#x\n", info.RegionSize, page_size);
1581c2c66affSColin Finck     ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
1582c2c66affSColin Finck     ok(info.AllocationBase == addr1, "%p != %p\n", info.AllocationBase, addr1);
1583c2c66affSColin Finck     ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
1584c2c66affSColin Finck     ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
1585c2c66affSColin Finck     ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
1586c2c66affSColin Finck 
1587c2c66affSColin Finck     addr2 = NULL;
1588c2c66affSColin Finck     size = 0;
1589c2c66affSColin Finck     status = pNtMapViewOfSection(hmap, GetCurrentProcess(), &addr2, 0, 0, &offset,
1590c2c66affSColin Finck                                  &size, 1 /* ViewShare */, 0, PAGE_READONLY);
1591c2c66affSColin Finck     ok(status == STATUS_IMAGE_NOT_AT_BASE, "expected STATUS_IMAGE_NOT_AT_BASE, got %x\n", status);
1592c2c66affSColin Finck     ok(addr2 != 0, "mapped address should be valid\n");
1593c2c66affSColin Finck     ok(addr2 != addr1, "mapped addresses should be different\n");
1594c2c66affSColin Finck 
1595c2c66affSColin Finck     SetLastError(0xdeadbeef);
1596c2c66affSColin Finck     size = VirtualQuery((char *)addr2 + section.VirtualAddress, &info, sizeof(info));
1597c2c66affSColin Finck     ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
1598c2c66affSColin Finck     ok(info.BaseAddress == (char *)addr2 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr2 + section.VirtualAddress);
1599c2c66affSColin Finck     ok(info.RegionSize == page_size, "got %#lx != expected %#x\n", info.RegionSize, page_size);
1600c2c66affSColin Finck     ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
1601c2c66affSColin Finck     ok(info.AllocationBase == addr2, "%p != %p\n", info.AllocationBase, addr2);
1602c2c66affSColin Finck     ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
1603c2c66affSColin Finck     ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
1604c2c66affSColin Finck     ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
1605c2c66affSColin Finck 
1606c2c66affSColin Finck     status = pNtUnmapViewOfSection(GetCurrentProcess(), addr2);
1607c2c66affSColin Finck     ok(status == STATUS_SUCCESS, "NtUnmapViewOfSection error %x\n", status);
1608c2c66affSColin Finck 
1609c2c66affSColin Finck     addr2 = MapViewOfFile(hmap, 0, 0, 0, 0);
1610c2c66affSColin Finck     ok(addr2 != 0, "mapped address should be valid\n");
1611c2c66affSColin Finck     ok(addr2 != addr1, "mapped addresses should be different\n");
1612c2c66affSColin Finck 
1613c2c66affSColin Finck     SetLastError(0xdeadbeef);
1614c2c66affSColin Finck     size = VirtualQuery((char *)addr2 + section.VirtualAddress, &info, sizeof(info));
1615c2c66affSColin Finck     ok(size == sizeof(info), "VirtualQuery error %d\n", GetLastError());
1616c2c66affSColin Finck     ok(info.BaseAddress == (char *)addr2 + section.VirtualAddress, "got %p != expected %p\n", info.BaseAddress, (char *)addr2 + section.VirtualAddress);
1617c2c66affSColin Finck     ok(info.RegionSize == page_size, "got %#lx != expected %#x\n", info.RegionSize, page_size);
1618c2c66affSColin Finck     ok(info.Protect == scn_page_access, "got %#x != expected %#x\n", info.Protect, scn_page_access);
1619c2c66affSColin Finck     ok(info.AllocationBase == addr2, "%p != %p\n", info.AllocationBase, addr2);
1620c2c66affSColin Finck     ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%#x != PAGE_EXECUTE_WRITECOPY\n", info.AllocationProtect);
1621c2c66affSColin Finck     ok(info.State == MEM_COMMIT, "%#x != MEM_COMMIT\n", info.State);
1622c2c66affSColin Finck     ok(info.Type == SEC_IMAGE, "%#x != SEC_IMAGE\n", info.Type);
1623c2c66affSColin Finck 
1624c2c66affSColin Finck     UnmapViewOfFile(addr2);
1625c2c66affSColin Finck 
1626c2c66affSColin Finck     SetLastError(0xdeadbeef);
1627c2c66affSColin Finck     addr2 = LoadLibraryA(dll_name);
1628c2c66affSColin Finck     if (is_dll)
1629c2c66affSColin Finck     {
1630c2c66affSColin Finck         ok(!addr2, "LoadLibrary should fail, is_dll %d\n", is_dll);
1631c2c66affSColin Finck         ok(GetLastError() == ERROR_INVALID_ADDRESS, "expected ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
1632c2c66affSColin Finck     }
1633c2c66affSColin Finck     else
1634c2c66affSColin Finck     {
1635c2c66affSColin Finck         BOOL ret;
1636c2c66affSColin Finck         ok(addr2 != 0, "LoadLibrary error %d, is_dll %d\n", GetLastError(), is_dll);
1637c2c66affSColin Finck         ok(addr2 != addr1, "mapped addresses should be different\n");
1638c2c66affSColin Finck 
1639c2c66affSColin Finck         SetLastError(0xdeadbeef);
1640c2c66affSColin Finck         ret = FreeLibrary(addr2);
1641c2c66affSColin Finck         ok(ret, "FreeLibrary error %d\n", GetLastError());
1642c2c66affSColin Finck     }
1643c2c66affSColin Finck 
1644c2c66affSColin Finck     status = pNtUnmapViewOfSection(GetCurrentProcess(), addr1);
1645c2c66affSColin Finck     ok(status == STATUS_SUCCESS, "NtUnmapViewOfSection error %x\n", status);
1646c2c66affSColin Finck 
1647c2c66affSColin Finck     CloseHandle(hmap);
1648c2c66affSColin Finck     CloseHandle(hfile);
1649c2c66affSColin Finck }
1650c2c66affSColin Finck 
is_mem_writable(DWORD prot)1651c2c66affSColin Finck static BOOL is_mem_writable(DWORD prot)
1652c2c66affSColin Finck {
1653c2c66affSColin Finck     switch (prot & 0xff)
1654c2c66affSColin Finck     {
1655c2c66affSColin Finck         case PAGE_READWRITE:
1656c2c66affSColin Finck         case PAGE_WRITECOPY:
1657c2c66affSColin Finck         case PAGE_EXECUTE_READWRITE:
1658c2c66affSColin Finck         case PAGE_EXECUTE_WRITECOPY:
1659c2c66affSColin Finck             return TRUE;
1660c2c66affSColin Finck 
1661c2c66affSColin Finck         default:
1662c2c66affSColin Finck             return FALSE;
1663c2c66affSColin Finck     }
1664c2c66affSColin Finck }
1665c2c66affSColin Finck 
test_VirtualProtect(void * base,void * section)1666c2c66affSColin Finck static void test_VirtualProtect(void *base, void *section)
1667c2c66affSColin Finck {
1668c2c66affSColin Finck     static const struct test_data
1669c2c66affSColin Finck     {
1670c2c66affSColin Finck         DWORD prot_set, prot_get;
1671c2c66affSColin Finck     } td[] =
1672c2c66affSColin Finck     {
1673c2c66affSColin Finck         { 0, 0 }, /* 0x00 */
1674c2c66affSColin Finck         { PAGE_NOACCESS, PAGE_NOACCESS }, /* 0x01 */
1675c2c66affSColin Finck         { PAGE_READONLY, PAGE_READONLY }, /* 0x02 */
1676c2c66affSColin Finck         { PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x03 */
1677c2c66affSColin Finck         { PAGE_READWRITE, PAGE_WRITECOPY }, /* 0x04 */
1678c2c66affSColin Finck         { PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x05 */
1679c2c66affSColin Finck         { PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x06 */
1680c2c66affSColin Finck         { PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x07 */
1681c2c66affSColin Finck         { PAGE_WRITECOPY, PAGE_WRITECOPY }, /* 0x08 */
1682c2c66affSColin Finck         { PAGE_WRITECOPY | PAGE_NOACCESS, 0 }, /* 0x09 */
1683c2c66affSColin Finck         { PAGE_WRITECOPY | PAGE_READONLY, 0 }, /* 0x0a */
1684c2c66affSColin Finck         { PAGE_WRITECOPY | PAGE_NOACCESS | PAGE_READONLY, 0 }, /* 0x0b */
1685c2c66affSColin Finck         { PAGE_WRITECOPY | PAGE_READWRITE, 0 }, /* 0x0c */
1686c2c66affSColin Finck         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_NOACCESS, 0 }, /* 0x0d */
1687c2c66affSColin Finck         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY, 0 }, /* 0x0e */
1688c2c66affSColin Finck         { PAGE_WRITECOPY | PAGE_READWRITE | PAGE_READONLY | PAGE_NOACCESS, 0 }, /* 0x0f */
1689c2c66affSColin Finck 
1690c2c66affSColin Finck         { PAGE_EXECUTE, PAGE_EXECUTE }, /* 0x10 */
1691c2c66affSColin Finck         { PAGE_EXECUTE_READ, PAGE_EXECUTE_READ }, /* 0x20 */
1692c2c66affSColin Finck         { PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x30 */
1693c2c66affSColin Finck         { PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY }, /* 0x40 */
1694c2c66affSColin Finck         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0x50 */
1695c2c66affSColin Finck         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0x60 */
1696c2c66affSColin Finck         { PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0x70 */
1697c2c66affSColin Finck         { PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_WRITECOPY }, /* 0x80 */
1698c2c66affSColin Finck         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE, 0 }, /* 0x90 */
1699c2c66affSColin Finck         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ, 0 }, /* 0xa0 */
1700c2c66affSColin Finck         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 }, /* 0xb0 */
1701c2c66affSColin Finck         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE, 0 }, /* 0xc0 */
1702c2c66affSColin Finck         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE, 0 }, /* 0xd0 */
1703c2c66affSColin Finck         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ, 0 }, /* 0xe0 */
1704c2c66affSColin Finck         { PAGE_EXECUTE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ | PAGE_EXECUTE, 0 } /* 0xf0 */
1705c2c66affSColin Finck     };
1706c2c66affSColin Finck     DWORD ret, orig_prot, old_prot, rw_prot, exec_prot, i, j;
1707c2c66affSColin Finck     MEMORY_BASIC_INFORMATION info;
1708c2c66affSColin Finck 
1709c2c66affSColin Finck     SetLastError(0xdeadbeef);
1710c2c66affSColin Finck     ret = VirtualProtect(section, page_size, PAGE_NOACCESS, &old_prot);
1711c2c66affSColin Finck     ok(ret, "VirtualProtect error %d\n", GetLastError());
1712c2c66affSColin Finck 
1713c2c66affSColin Finck     orig_prot = old_prot;
1714c2c66affSColin Finck 
1715c2c66affSColin Finck     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1716c2c66affSColin Finck     {
1717c2c66affSColin Finck         SetLastError(0xdeadbeef);
1718c2c66affSColin Finck         ret = VirtualQuery(section, &info, sizeof(info));
1719c2c66affSColin Finck         ok(ret, "VirtualQuery failed %d\n", GetLastError());
1720c2c66affSColin Finck         ok(info.BaseAddress == section, "%d: got %p != expected %p\n", i, info.BaseAddress, section);
1721c2c66affSColin Finck         ok(info.RegionSize == page_size, "%d: got %#lx != expected %#x\n", i, info.RegionSize, page_size);
1722c2c66affSColin Finck         ok(info.Protect == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, info.Protect);
1723c2c66affSColin Finck         ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1724c2c66affSColin Finck         ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
1725c2c66affSColin Finck         ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1726c2c66affSColin Finck         ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
1727c2c66affSColin Finck 
1728c2c66affSColin Finck         old_prot = 0xdeadbeef;
1729c2c66affSColin Finck         SetLastError(0xdeadbeef);
1730c2c66affSColin Finck         ret = VirtualProtect(section, page_size, td[i].prot_set, &old_prot);
1731c2c66affSColin Finck         if (td[i].prot_get)
1732c2c66affSColin Finck         {
1733c2c66affSColin Finck             ok(ret, "%d: VirtualProtect error %d, requested prot %#x\n", i, GetLastError(), td[i].prot_set);
1734c2c66affSColin Finck             ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1735c2c66affSColin Finck 
1736c2c66affSColin Finck             SetLastError(0xdeadbeef);
1737c2c66affSColin Finck             ret = VirtualQuery(section, &info, sizeof(info));
1738c2c66affSColin Finck             ok(ret, "VirtualQuery failed %d\n", GetLastError());
1739c2c66affSColin Finck             ok(info.BaseAddress == section, "%d: got %p != expected %p\n", i, info.BaseAddress, section);
1740c2c66affSColin Finck             ok(info.RegionSize == page_size, "%d: got %#lx != expected %#x\n", i, info.RegionSize, page_size);
1741c2c66affSColin Finck             ok(info.Protect == td[i].prot_get, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].prot_get);
1742c2c66affSColin Finck             ok(info.AllocationBase == base, "%d: %p != %p\n", i, info.AllocationBase, base);
1743c2c66affSColin Finck             ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
1744c2c66affSColin Finck             ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1745c2c66affSColin Finck             ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
1746c2c66affSColin Finck         }
1747c2c66affSColin Finck         else
1748c2c66affSColin Finck         {
1749c2c66affSColin Finck             ok(!ret, "%d: VirtualProtect should fail\n", i);
1750c2c66affSColin Finck             ok(GetLastError() == ERROR_INVALID_PARAMETER, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i, GetLastError());
1751c2c66affSColin Finck         }
1752c2c66affSColin Finck 
1753c2c66affSColin Finck         old_prot = 0xdeadbeef;
1754c2c66affSColin Finck         SetLastError(0xdeadbeef);
1755c2c66affSColin Finck         ret = VirtualProtect(section, page_size, PAGE_NOACCESS, &old_prot);
1756c2c66affSColin Finck         ok(ret, "%d: VirtualProtect error %d\n", i, GetLastError());
1757c2c66affSColin Finck         if (td[i].prot_get)
1758c2c66affSColin Finck             ok(old_prot == td[i].prot_get, "%d: got %#x != expected %#x\n", i, old_prot, td[i].prot_get);
1759c2c66affSColin Finck         else
1760c2c66affSColin Finck             ok(old_prot == PAGE_NOACCESS, "%d: got %#x != expected PAGE_NOACCESS\n", i, old_prot);
1761c2c66affSColin Finck     }
1762c2c66affSColin Finck 
1763c2c66affSColin Finck     exec_prot = 0;
1764c2c66affSColin Finck 
1765c2c66affSColin Finck     for (i = 0; i <= 4; i++)
1766c2c66affSColin Finck     {
1767c2c66affSColin Finck         rw_prot = 0;
1768c2c66affSColin Finck 
1769c2c66affSColin Finck         for (j = 0; j <= 4; j++)
1770c2c66affSColin Finck         {
1771c2c66affSColin Finck             DWORD prot = exec_prot | rw_prot;
1772c2c66affSColin Finck 
1773c2c66affSColin Finck             SetLastError(0xdeadbeef);
1774c2c66affSColin Finck             ret = VirtualProtect(section, page_size, prot, &old_prot);
1775c2c66affSColin Finck             if ((rw_prot && exec_prot) || (!rw_prot && !exec_prot))
1776c2c66affSColin Finck             {
1777c2c66affSColin Finck                 ok(!ret, "VirtualProtect(%02x) should fail\n", prot);
1778c2c66affSColin Finck                 ok(GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1779c2c66affSColin Finck             }
1780c2c66affSColin Finck             else
1781c2c66affSColin Finck                 ok(ret, "VirtualProtect(%02x) error %d\n", prot, GetLastError());
1782c2c66affSColin Finck 
1783c2c66affSColin Finck             rw_prot = 1 << j;
1784c2c66affSColin Finck         }
1785c2c66affSColin Finck 
1786c2c66affSColin Finck         exec_prot = 1 << (i + 4);
1787c2c66affSColin Finck     }
1788c2c66affSColin Finck 
1789c2c66affSColin Finck     SetLastError(0xdeadbeef);
1790c2c66affSColin Finck     ret = VirtualProtect(section, page_size, orig_prot, &old_prot);
1791c2c66affSColin Finck     ok(ret, "VirtualProtect error %d\n", GetLastError());
1792c2c66affSColin Finck }
1793c2c66affSColin Finck 
test_section_access(void)1794c2c66affSColin Finck static void test_section_access(void)
1795c2c66affSColin Finck {
1796c2c66affSColin Finck     static const struct test_data
1797c2c66affSColin Finck     {
1798c2c66affSColin Finck         DWORD scn_file_access, scn_page_access, scn_page_access_after_write;
1799c2c66affSColin Finck     } td[] =
1800c2c66affSColin Finck     {
1801c2c66affSColin Finck         { 0, PAGE_NOACCESS, 0 },
1802c2c66affSColin Finck         { IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
1803c2c66affSColin Finck         { IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1804c2c66affSColin Finck         { IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
1805c2c66affSColin Finck         { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1806c2c66affSColin Finck         { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ },
1807c2c66affSColin Finck         { IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
1808c2c66affSColin Finck         { IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
1809c2c66affSColin Finck 
1810c2c66affSColin Finck         { IMAGE_SCN_CNT_INITIALIZED_DATA, PAGE_NOACCESS, 0 },
1811c2c66affSColin Finck         { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
1812c2c66affSColin Finck         { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1813c2c66affSColin Finck         { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
1814c2c66affSColin Finck         { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1815c2c66affSColin Finck         { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ, 0 },
1816c2c66affSColin Finck         { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
1817c2c66affSColin Finck         { IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
1818c2c66affSColin Finck 
1819c2c66affSColin Finck         { IMAGE_SCN_CNT_UNINITIALIZED_DATA, PAGE_NOACCESS, 0 },
1820c2c66affSColin Finck         { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ, PAGE_READONLY, 0 },
1821c2c66affSColin Finck         { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1822c2c66affSColin Finck         { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE, 0 },
1823c2c66affSColin Finck         { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE, PAGE_WRITECOPY, PAGE_READWRITE },
1824c2c66affSColin Finck         { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_READ, 0 },
1825c2c66affSColin Finck         { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE },
1826c2c66affSColin Finck         { IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE, PAGE_EXECUTE_WRITECOPY, PAGE_EXECUTE_READWRITE }
1827c2c66affSColin Finck     };
1828c2c66affSColin Finck     char buf[256];
1829c2c66affSColin Finck     int i;
1830c2c66affSColin Finck     DWORD dummy, file_align;
1831c2c66affSColin Finck     HANDLE hfile;
1832c2c66affSColin Finck     HMODULE hlib;
1833c2c66affSColin Finck     char temp_path[MAX_PATH];
1834c2c66affSColin Finck     char dll_name[MAX_PATH];
1835c2c66affSColin Finck     SIZE_T size;
1836c2c66affSColin Finck     MEMORY_BASIC_INFORMATION info;
1837c2c66affSColin Finck     STARTUPINFOA sti;
1838c2c66affSColin Finck     PROCESS_INFORMATION pi;
1839c2c66affSColin Finck     DWORD ret;
1840c2c66affSColin Finck 
1841c2c66affSColin Finck     /* prevent displaying of the "Unable to load this DLL" message box */
1842c2c66affSColin Finck     SetErrorMode(SEM_FAILCRITICALERRORS);
1843c2c66affSColin Finck 
1844c2c66affSColin Finck     GetTempPathA(MAX_PATH, temp_path);
1845c2c66affSColin Finck 
1846c2c66affSColin Finck     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
1847c2c66affSColin Finck     {
1848c2c66affSColin Finck         IMAGE_NT_HEADERS nt_header;
1849c2c66affSColin Finck 
1850c2c66affSColin Finck         GetTempFileNameA(temp_path, "ldr", 0, dll_name);
1851c2c66affSColin Finck 
1852c2c66affSColin Finck         /*trace("creating %s\n", dll_name);*/
1853c2c66affSColin Finck         hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
1854c2c66affSColin Finck         if (hfile == INVALID_HANDLE_VALUE)
1855c2c66affSColin Finck         {
1856c2c66affSColin Finck             ok(0, "could not create %s\n", dll_name);
1857c2c66affSColin Finck             return;
1858c2c66affSColin Finck         }
1859c2c66affSColin Finck 
1860c2c66affSColin Finck         SetLastError(0xdeadbeef);
1861c2c66affSColin Finck         ret = WriteFile(hfile, &dos_header, sizeof(dos_header), &dummy, NULL);
1862c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
1863c2c66affSColin Finck 
1864c2c66affSColin Finck         nt_header = nt_header_template;
1865c2c66affSColin Finck         nt_header.FileHeader.NumberOfSections = 1;
1866c2c66affSColin Finck         nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
1867c2c66affSColin Finck         nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL | IMAGE_FILE_RELOCS_STRIPPED;
1868c2c66affSColin Finck 
1869c2c66affSColin Finck         nt_header.OptionalHeader.SectionAlignment = page_size;
1870c2c66affSColin Finck         nt_header.OptionalHeader.FileAlignment = 0x200;
1871c2c66affSColin Finck         nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + page_size;
1872c2c66affSColin Finck         nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER);
1873c2c66affSColin Finck         SetLastError(0xdeadbeef);
1874c2c66affSColin Finck         ret = WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
1875c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
1876c2c66affSColin Finck         SetLastError(0xdeadbeef);
1877c2c66affSColin Finck         ret = WriteFile(hfile, &nt_header.OptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER), &dummy, NULL);
1878c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
1879c2c66affSColin Finck 
1880c2c66affSColin Finck         section.SizeOfRawData = sizeof(section_data);
1881c2c66affSColin Finck         section.PointerToRawData = nt_header.OptionalHeader.FileAlignment;
1882c2c66affSColin Finck         section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
1883c2c66affSColin Finck         section.Misc.VirtualSize = section.SizeOfRawData;
1884c2c66affSColin Finck         section.Characteristics = td[i].scn_file_access;
1885c2c66affSColin Finck         SetLastError(0xdeadbeef);
1886c2c66affSColin Finck         ret = WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
1887c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
1888c2c66affSColin Finck 
1889c2c66affSColin Finck         file_align = nt_header.OptionalHeader.FileAlignment - nt_header.OptionalHeader.SizeOfHeaders;
1890c2c66affSColin Finck         assert(file_align < sizeof(filler));
1891c2c66affSColin Finck         SetLastError(0xdeadbeef);
1892c2c66affSColin Finck         ret = WriteFile(hfile, filler, file_align, &dummy, NULL);
1893c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
1894c2c66affSColin Finck 
1895c2c66affSColin Finck         /* section data */
1896c2c66affSColin Finck         SetLastError(0xdeadbeef);
1897c2c66affSColin Finck         ret = WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL);
1898c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
1899c2c66affSColin Finck 
1900c2c66affSColin Finck         CloseHandle(hfile);
1901c2c66affSColin Finck 
1902c2c66affSColin Finck         SetLastError(0xdeadbeef);
1903c2c66affSColin Finck         hlib = LoadLibraryA(dll_name);
1904c2c66affSColin Finck         ok(hlib != 0, "LoadLibrary error %d\n", GetLastError());
1905c2c66affSColin Finck 
1906c2c66affSColin Finck         SetLastError(0xdeadbeef);
1907c2c66affSColin Finck         size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
1908c2c66affSColin Finck         ok(size == sizeof(info),
1909c2c66affSColin Finck             "%d: VirtualQuery error %d\n", i, GetLastError());
1910c2c66affSColin Finck         ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
1911c2c66affSColin Finck         ok(info.RegionSize == page_size, "%d: got %#lx != expected %#x\n", i, info.RegionSize, page_size);
1912c2c66affSColin Finck         ok(info.Protect == td[i].scn_page_access, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].scn_page_access);
1913c2c66affSColin Finck         ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
1914c2c66affSColin Finck         ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
1915c2c66affSColin Finck         ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1916c2c66affSColin Finck         ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
1917c2c66affSColin Finck         if (info.Protect != PAGE_NOACCESS)
1918c2c66affSColin Finck             ok(!memcmp((const char *)info.BaseAddress, section_data, section.SizeOfRawData), "wrong section data\n");
1919c2c66affSColin Finck 
1920c2c66affSColin Finck         test_VirtualProtect(hlib, (char *)hlib + section.VirtualAddress);
1921c2c66affSColin Finck 
1922c2c66affSColin Finck         /* Windows changes the WRITECOPY to WRITE protection on an image section write (for a changed page only) */
1923c2c66affSColin Finck         if (is_mem_writable(info.Protect))
1924c2c66affSColin Finck         {
1925c2c66affSColin Finck             char *p = info.BaseAddress;
1926c2c66affSColin Finck             *p = 0xfe;
1927c2c66affSColin Finck             SetLastError(0xdeadbeef);
1928c2c66affSColin Finck             size = VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info));
1929c2c66affSColin Finck             ok(size == sizeof(info), "%d: VirtualQuery error %d\n", i, GetLastError());
1930c2c66affSColin Finck             /* FIXME: remove the condition below once Wine is fixed */
1931c2c66affSColin Finck             todo_wine_if (info.Protect == PAGE_WRITECOPY || info.Protect == PAGE_EXECUTE_WRITECOPY)
1932c2c66affSColin Finck                 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);
1933c2c66affSColin Finck         }
1934c2c66affSColin Finck 
1935c2c66affSColin Finck         SetLastError(0xdeadbeef);
1936c2c66affSColin Finck         ret = FreeLibrary(hlib);
1937c2c66affSColin Finck         ok(ret, "FreeLibrary error %d\n", GetLastError());
1938c2c66affSColin Finck 
1939c2c66affSColin Finck         test_image_mapping(dll_name, td[i].scn_page_access, TRUE);
1940c2c66affSColin Finck 
1941c2c66affSColin Finck         /* reset IMAGE_FILE_DLL otherwise CreateProcess fails */
1942c2c66affSColin Finck         nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_RELOCS_STRIPPED;
1943c2c66affSColin Finck         SetLastError(0xdeadbeef);
1944c2c66affSColin Finck         hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1945c2c66affSColin Finck         /* LoadLibrary called on an already memory-mapped file in
1946c2c66affSColin Finck          * test_image_mapping() above leads to a file handle leak
1947c2c66affSColin Finck          * under nt4, and inability to overwrite and delete the file
1948c2c66affSColin Finck          * due to sharing violation error. Ignore it and skip the test,
1949c2c66affSColin Finck          * but leave a not deletable temporary file.
1950c2c66affSColin Finck          */
1951c2c66affSColin Finck         ok(hfile != INVALID_HANDLE_VALUE || broken(hfile == INVALID_HANDLE_VALUE) /* nt4 */,
1952c2c66affSColin Finck             "CreateFile error %d\n", GetLastError());
1953c2c66affSColin Finck         if (hfile == INVALID_HANDLE_VALUE) goto nt4_is_broken;
1954c2c66affSColin Finck         SetFilePointer(hfile, sizeof(dos_header), NULL, FILE_BEGIN);
1955c2c66affSColin Finck         SetLastError(0xdeadbeef);
1956c2c66affSColin Finck         ret = WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
1957c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
1958c2c66affSColin Finck         CloseHandle(hfile);
1959c2c66affSColin Finck 
1960c2c66affSColin Finck         memset(&sti, 0, sizeof(sti));
1961c2c66affSColin Finck         sti.cb = sizeof(sti);
1962c2c66affSColin Finck         SetLastError(0xdeadbeef);
1963c2c66affSColin Finck         ret = CreateProcessA(dll_name, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &sti, &pi);
1964c2c66affSColin Finck         ok(ret, "CreateProcess() error %d\n", GetLastError());
1965c2c66affSColin Finck 
1966c2c66affSColin Finck         SetLastError(0xdeadbeef);
1967c2c66affSColin Finck         size = VirtualQueryEx(pi.hProcess, (char *)hlib + section.VirtualAddress, &info, sizeof(info));
1968c2c66affSColin Finck         ok(size == sizeof(info),
1969c2c66affSColin Finck             "%d: VirtualQuery error %d\n", i, GetLastError());
1970c2c66affSColin Finck         ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
1971c2c66affSColin Finck         ok(info.RegionSize == page_size, "%d: got %#lx != expected %#x\n", i, info.RegionSize, page_size);
1972c2c66affSColin Finck         ok(info.Protect == td[i].scn_page_access, "%d: got %#x != expected %#x\n", i, info.Protect, td[i].scn_page_access);
1973c2c66affSColin Finck         ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
1974c2c66affSColin Finck         ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
1975c2c66affSColin Finck         ok(info.State == MEM_COMMIT, "%d: %#x != MEM_COMMIT\n", i, info.State);
1976c2c66affSColin Finck         ok(info.Type == SEC_IMAGE, "%d: %#x != SEC_IMAGE\n", i, info.Type);
1977c2c66affSColin Finck         if (info.Protect != PAGE_NOACCESS)
1978c2c66affSColin Finck         {
1979c2c66affSColin Finck             SetLastError(0xdeadbeef);
1980c2c66affSColin Finck             ret = ReadProcessMemory(pi.hProcess, info.BaseAddress, buf, section.SizeOfRawData, NULL);
1981c2c66affSColin Finck             ok(ret, "ReadProcessMemory() error %d\n", GetLastError());
1982c2c66affSColin Finck             ok(!memcmp(buf, section_data, section.SizeOfRawData), "wrong section data\n");
1983c2c66affSColin Finck         }
1984c2c66affSColin Finck 
1985c2c66affSColin Finck         SetLastError(0xdeadbeef);
1986c2c66affSColin Finck         ret = TerminateProcess(pi.hProcess, 0);
1987c2c66affSColin Finck         ok(ret, "TerminateProcess() error %d\n", GetLastError());
1988c2c66affSColin Finck         ret = WaitForSingleObject(pi.hProcess, 3000);
1989c2c66affSColin Finck         ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
1990c2c66affSColin Finck 
1991c2c66affSColin Finck         CloseHandle(pi.hThread);
1992c2c66affSColin Finck         CloseHandle(pi.hProcess);
1993c2c66affSColin Finck 
1994c2c66affSColin Finck         test_image_mapping(dll_name, td[i].scn_page_access, FALSE);
1995c2c66affSColin Finck 
1996c2c66affSColin Finck nt4_is_broken:
1997c2c66affSColin Finck         SetLastError(0xdeadbeef);
1998c2c66affSColin Finck         ret = DeleteFileA(dll_name);
1999c2c66affSColin Finck         ok(ret || broken(!ret) /* nt4 */, "DeleteFile error %d\n", GetLastError());
2000c2c66affSColin Finck     }
2001c2c66affSColin Finck }
2002c2c66affSColin Finck 
test_import_resolution(void)2003c2c66affSColin Finck static void test_import_resolution(void)
2004c2c66affSColin Finck {
2005c2c66affSColin Finck     char temp_path[MAX_PATH];
2006c2c66affSColin Finck     char dll_name[MAX_PATH];
2007c2c66affSColin Finck     DWORD dummy;
2008c2c66affSColin Finck     void *expect;
2009c2c66affSColin Finck     char *str;
2010c2c66affSColin Finck     HANDLE hfile;
2011c2c66affSColin Finck     HMODULE mod, mod2;
2012c2c66affSColin Finck     struct imports
2013c2c66affSColin Finck     {
2014c2c66affSColin Finck         IMAGE_IMPORT_DESCRIPTOR descr[2];
2015c2c66affSColin Finck         IMAGE_THUNK_DATA original_thunks[2];
2016c2c66affSColin Finck         IMAGE_THUNK_DATA thunks[2];
2017c2c66affSColin Finck         char module[16];
2018c2c66affSColin Finck         struct { WORD hint; char name[32]; } function;
2019c2c66affSColin Finck         IMAGE_TLS_DIRECTORY tls;
2020c2c66affSColin Finck         char tls_data[16];
2021c2c66affSColin Finck         SHORT tls_index;
2022c2c66affSColin Finck     } data, *ptr;
2023c2c66affSColin Finck     IMAGE_NT_HEADERS nt;
2024c2c66affSColin Finck     IMAGE_SECTION_HEADER section;
2025c2c66affSColin Finck     int test;
2026c2c66affSColin Finck 
2027c2c66affSColin Finck     for (test = 0; test < 3; test++)
2028c2c66affSColin Finck     {
2029c2c66affSColin Finck #define DATA_RVA(ptr) (page_size + ((char *)(ptr) - (char *)&data))
2030c2c66affSColin Finck         nt = nt_header_template;
2031c2c66affSColin Finck         nt.FileHeader.NumberOfSections = 1;
2032c2c66affSColin Finck         nt.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
2033c2c66affSColin Finck         nt.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_32BIT_MACHINE | IMAGE_FILE_RELOCS_STRIPPED;
2034c2c66affSColin Finck         if (test != 2) nt.FileHeader.Characteristics |= IMAGE_FILE_DLL;
2035c2c66affSColin Finck         nt.OptionalHeader.SectionAlignment = page_size;
2036c2c66affSColin Finck         nt.OptionalHeader.FileAlignment = 0x200;
2037c2c66affSColin Finck         nt.OptionalHeader.ImageBase = 0x12340000;
2038c2c66affSColin Finck         nt.OptionalHeader.SizeOfImage = 2 * page_size;
2039c2c66affSColin Finck         nt.OptionalHeader.SizeOfHeaders = nt.OptionalHeader.FileAlignment;
2040c2c66affSColin Finck         nt.OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
2041c2c66affSColin Finck         memset( nt.OptionalHeader.DataDirectory, 0, sizeof(nt.OptionalHeader.DataDirectory) );
2042c2c66affSColin Finck         nt.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size = sizeof(data.descr);
2043c2c66affSColin Finck         nt.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress = DATA_RVA(data.descr);
2044c2c66affSColin Finck         nt.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].Size = sizeof(data.tls);
2045c2c66affSColin Finck         nt.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress = DATA_RVA(&data.tls);
2046c2c66affSColin Finck 
2047c2c66affSColin Finck         memset( &data, 0, sizeof(data) );
2048*ed41a4deSAmine Khaldi         U(data.descr[0]).OriginalFirstThunk = DATA_RVA( data.original_thunks );
2049c2c66affSColin Finck         data.descr[0].FirstThunk = DATA_RVA( data.thunks );
2050c2c66affSColin Finck         data.descr[0].Name = DATA_RVA( data.module );
2051c2c66affSColin Finck         strcpy( data.module, "kernel32.dll" );
2052c2c66affSColin Finck         strcpy( data.function.name, "CreateEventA" );
2053c2c66affSColin Finck         data.original_thunks[0].u1.AddressOfData = DATA_RVA( &data.function );
2054c2c66affSColin Finck         data.thunks[0].u1.AddressOfData = 0xdeadbeef;
2055c2c66affSColin Finck 
2056c2c66affSColin Finck         data.tls.StartAddressOfRawData = nt.OptionalHeader.ImageBase + DATA_RVA( data.tls_data );
2057c2c66affSColin Finck         data.tls.EndAddressOfRawData = data.tls.StartAddressOfRawData + sizeof(data.tls_data);
2058c2c66affSColin Finck         data.tls.AddressOfIndex = nt.OptionalHeader.ImageBase + DATA_RVA( &data.tls_index );
2059c2c66affSColin Finck         strcpy( data.tls_data, "hello world" );
2060c2c66affSColin Finck         data.tls_index = 9999;
2061c2c66affSColin Finck 
2062c2c66affSColin Finck         GetTempPathA(MAX_PATH, temp_path);
2063c2c66affSColin Finck         GetTempFileNameA(temp_path, "ldr", 0, dll_name);
2064c2c66affSColin Finck 
2065c2c66affSColin Finck         hfile = CreateFileA(dll_name, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, 0);
2066c2c66affSColin Finck         ok( hfile != INVALID_HANDLE_VALUE, "creation failed\n" );
2067c2c66affSColin Finck 
2068c2c66affSColin Finck         memset( &section, 0, sizeof(section) );
2069c2c66affSColin Finck         memcpy( section.Name, ".text", sizeof(".text") );
2070c2c66affSColin Finck         section.PointerToRawData = nt.OptionalHeader.FileAlignment;
2071c2c66affSColin Finck         section.VirtualAddress = nt.OptionalHeader.SectionAlignment;
2072c2c66affSColin Finck         section.Misc.VirtualSize = sizeof(data);
2073c2c66affSColin Finck         section.SizeOfRawData = sizeof(data);
2074c2c66affSColin Finck         section.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
2075c2c66affSColin Finck 
2076c2c66affSColin Finck         WriteFile(hfile, &dos_header, sizeof(dos_header), &dummy, NULL);
2077c2c66affSColin Finck         WriteFile(hfile, &nt, sizeof(nt), &dummy, NULL);
2078c2c66affSColin Finck         WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
2079c2c66affSColin Finck 
2080c2c66affSColin Finck         SetFilePointer( hfile, section.PointerToRawData, NULL, SEEK_SET );
2081c2c66affSColin Finck         WriteFile(hfile, &data, sizeof(data), &dummy, NULL);
2082c2c66affSColin Finck 
2083c2c66affSColin Finck         CloseHandle( hfile );
2084c2c66affSColin Finck 
2085c2c66affSColin Finck         switch (test)
2086c2c66affSColin Finck         {
2087c2c66affSColin Finck         case 0:  /* normal load */
2088c2c66affSColin Finck             mod = LoadLibraryA( dll_name );
2089c2c66affSColin Finck             ok( mod != NULL, "failed to load err %u\n", GetLastError() );
2090c2c66affSColin Finck             if (!mod) break;
2091c2c66affSColin Finck             ptr = (struct imports *)((char *)mod + page_size);
2092c2c66affSColin Finck             expect = GetProcAddress( GetModuleHandleA( data.module ), data.function.name );
2093c2c66affSColin Finck             ok( (void *)ptr->thunks[0].u1.Function == expect, "thunk %p instead of %p for %s.%s\n",
2094c2c66affSColin Finck                 (void *)ptr->thunks[0].u1.Function, expect, data.module, data.function.name );
2095c2c66affSColin Finck             ok( ptr->tls_index < 32 || broken(ptr->tls_index == 9999), /* before vista */
2096c2c66affSColin Finck                 "wrong tls index %d\n", ptr->tls_index );
2097c2c66affSColin Finck             if (ptr->tls_index != 9999)
2098c2c66affSColin Finck             {
2099c2c66affSColin Finck                 str = ((char **)NtCurrentTeb()->ThreadLocalStoragePointer)[ptr->tls_index];
2100c2c66affSColin Finck                 ok( !strcmp( str, "hello world" ), "wrong tls data '%s' at %p\n", str, str );
2101c2c66affSColin Finck             }
2102c2c66affSColin Finck             FreeLibrary( mod );
2103c2c66affSColin Finck             break;
2104c2c66affSColin Finck         case 1:  /* load with DONT_RESOLVE_DLL_REFERENCES doesn't resolve imports */
2105c2c66affSColin Finck             mod = LoadLibraryExA( dll_name, 0, DONT_RESOLVE_DLL_REFERENCES );
2106c2c66affSColin Finck             ok( mod != NULL, "failed to load err %u\n", GetLastError() );
2107c2c66affSColin Finck             if (!mod) break;
2108c2c66affSColin Finck             ptr = (struct imports *)((char *)mod + page_size);
2109c2c66affSColin Finck             ok( ptr->thunks[0].u1.Function == 0xdeadbeef, "thunk resolved to %p for %s.%s\n",
2110c2c66affSColin Finck                 (void *)ptr->thunks[0].u1.Function, data.module, data.function.name );
2111c2c66affSColin Finck             ok( ptr->tls_index == 9999, "wrong tls index %d\n", ptr->tls_index );
2112c2c66affSColin Finck 
2113c2c66affSColin Finck             mod2 = LoadLibraryA( dll_name );
2114c2c66affSColin Finck             ok( mod2 == mod, "loaded twice %p / %p\n", mod, mod2 );
2115c2c66affSColin Finck             ok( ptr->thunks[0].u1.Function == 0xdeadbeef, "thunk resolved to %p for %s.%s\n",
2116c2c66affSColin Finck                 (void *)ptr->thunks[0].u1.Function, data.module, data.function.name );
2117c2c66affSColin Finck             ok( ptr->tls_index == 9999, "wrong tls index %d\n", ptr->tls_index );
2118c2c66affSColin Finck             FreeLibrary( mod2 );
2119c2c66affSColin Finck             FreeLibrary( mod );
2120c2c66affSColin Finck             break;
2121c2c66affSColin Finck         case 2:  /* load without IMAGE_FILE_DLL doesn't resolve imports */
2122c2c66affSColin Finck             mod = LoadLibraryA( dll_name );
2123c2c66affSColin Finck             ok( mod != NULL, "failed to load err %u\n", GetLastError() );
2124c2c66affSColin Finck             if (!mod) break;
2125c2c66affSColin Finck             ptr = (struct imports *)((char *)mod + page_size);
2126c2c66affSColin Finck             ok( ptr->thunks[0].u1.Function == 0xdeadbeef, "thunk resolved to %p for %s.%s\n",
2127c2c66affSColin Finck                 (void *)ptr->thunks[0].u1.Function, data.module, data.function.name );
2128c2c66affSColin Finck             ok( ptr->tls_index == 9999, "wrong tls index %d\n", ptr->tls_index );
2129c2c66affSColin Finck             FreeLibrary( mod );
2130c2c66affSColin Finck             break;
2131c2c66affSColin Finck         }
2132c2c66affSColin Finck         DeleteFileA( dll_name );
2133c2c66affSColin Finck #undef DATA_RVA
2134c2c66affSColin Finck     }
2135c2c66affSColin Finck }
2136c2c66affSColin Finck 
2137c2c66affSColin Finck #define MAX_COUNT 10
2138c2c66affSColin Finck static HANDLE attached_thread[MAX_COUNT];
2139c2c66affSColin Finck static DWORD attached_thread_count;
2140c2c66affSColin Finck HANDLE stop_event, event, mutex, semaphore, loader_lock_event, peb_lock_event, heap_lock_event, ack_event;
2141c2c66affSColin Finck static int test_dll_phase, inside_loader_lock, inside_peb_lock, inside_heap_lock;
2142c2c66affSColin Finck static LONG fls_callback_count;
2143c2c66affSColin Finck 
mutex_thread_proc(void * param)2144c2c66affSColin Finck static DWORD WINAPI mutex_thread_proc(void *param)
2145c2c66affSColin Finck {
2146c2c66affSColin Finck     HANDLE wait_list[4];
2147c2c66affSColin Finck     DWORD ret;
2148c2c66affSColin Finck 
2149c2c66affSColin Finck     ret = WaitForSingleObject(mutex, 0);
2150c2c66affSColin Finck     ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2151c2c66affSColin Finck 
2152c2c66affSColin Finck     SetEvent(param);
2153c2c66affSColin Finck 
2154c2c66affSColin Finck     wait_list[0] = stop_event;
2155c2c66affSColin Finck     wait_list[1] = loader_lock_event;
2156c2c66affSColin Finck     wait_list[2] = peb_lock_event;
2157c2c66affSColin Finck     wait_list[3] = heap_lock_event;
2158c2c66affSColin Finck 
2159*ed41a4deSAmine Khaldi     trace("%04x: mutex_thread_proc: starting\n", GetCurrentThreadId());
2160c2c66affSColin Finck     while (1)
2161c2c66affSColin Finck     {
2162c2c66affSColin Finck         ret = WaitForMultipleObjects(sizeof(wait_list)/sizeof(wait_list[0]), wait_list, FALSE, 50);
2163c2c66affSColin Finck         if (ret == WAIT_OBJECT_0) break;
2164c2c66affSColin Finck         else if (ret == WAIT_OBJECT_0 + 1)
2165c2c66affSColin Finck         {
2166c2c66affSColin Finck             ULONG_PTR loader_lock_magic;
2167*ed41a4deSAmine Khaldi             trace("%04x: mutex_thread_proc: Entering loader lock\n", GetCurrentThreadId());
2168c2c66affSColin Finck             ret = pLdrLockLoaderLock(0, NULL, &loader_lock_magic);
2169c2c66affSColin Finck             ok(!ret, "LdrLockLoaderLock error %#x\n", ret);
2170c2c66affSColin Finck             inside_loader_lock++;
2171c2c66affSColin Finck             SetEvent(ack_event);
2172c2c66affSColin Finck         }
2173c2c66affSColin Finck         else if (ret == WAIT_OBJECT_0 + 2)
2174c2c66affSColin Finck         {
2175*ed41a4deSAmine Khaldi             trace("%04x: mutex_thread_proc: Entering PEB lock\n", GetCurrentThreadId());
2176c2c66affSColin Finck             pRtlAcquirePebLock();
2177c2c66affSColin Finck             inside_peb_lock++;
2178c2c66affSColin Finck             SetEvent(ack_event);
2179c2c66affSColin Finck         }
2180c2c66affSColin Finck         else if (ret == WAIT_OBJECT_0 + 3)
2181c2c66affSColin Finck         {
2182*ed41a4deSAmine Khaldi             trace("%04x: mutex_thread_proc: Entering heap lock\n", GetCurrentThreadId());
2183c2c66affSColin Finck             HeapLock(GetProcessHeap());
2184c2c66affSColin Finck             inside_heap_lock++;
2185c2c66affSColin Finck             SetEvent(ack_event);
2186c2c66affSColin Finck         }
2187c2c66affSColin Finck     }
2188c2c66affSColin Finck 
2189*ed41a4deSAmine Khaldi     trace("%04x: mutex_thread_proc: exiting\n", GetCurrentThreadId());
2190c2c66affSColin Finck     return 196;
2191c2c66affSColin Finck }
2192c2c66affSColin Finck 
semaphore_thread_proc(void * param)2193c2c66affSColin Finck static DWORD WINAPI semaphore_thread_proc(void *param)
2194c2c66affSColin Finck {
2195c2c66affSColin Finck     DWORD ret;
2196c2c66affSColin Finck 
2197c2c66affSColin Finck     ret = WaitForSingleObject(semaphore, 0);
2198c2c66affSColin Finck     ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2199c2c66affSColin Finck 
2200c2c66affSColin Finck     SetEvent(param);
2201c2c66affSColin Finck 
2202c2c66affSColin Finck     while (1)
2203c2c66affSColin Finck     {
2204c2c66affSColin Finck         if (winetest_debug > 1)
2205*ed41a4deSAmine Khaldi             trace("%04x: semaphore_thread_proc: still alive\n", GetCurrentThreadId());
2206c2c66affSColin Finck         if (WaitForSingleObject(stop_event, 50) != WAIT_TIMEOUT) break;
2207c2c66affSColin Finck     }
2208c2c66affSColin Finck 
2209*ed41a4deSAmine Khaldi     trace("%04x: semaphore_thread_proc: exiting\n", GetCurrentThreadId());
2210c2c66affSColin Finck     return 196;
2211c2c66affSColin Finck }
2212c2c66affSColin Finck 
noop_thread_proc(void * param)2213c2c66affSColin Finck static DWORD WINAPI noop_thread_proc(void *param)
2214c2c66affSColin Finck {
2215c2c66affSColin Finck     if (param)
2216c2c66affSColin Finck     {
2217c2c66affSColin Finck         LONG *noop_thread_started = param;
2218c2c66affSColin Finck         InterlockedIncrement(noop_thread_started);
2219c2c66affSColin Finck     }
2220c2c66affSColin Finck 
2221*ed41a4deSAmine Khaldi     trace("%04x: noop_thread_proc: exiting\n", GetCurrentThreadId());
2222c2c66affSColin Finck     return 195;
2223c2c66affSColin Finck }
2224c2c66affSColin Finck 
fls_callback(PVOID lpFlsData)2225c2c66affSColin Finck static VOID WINAPI fls_callback(PVOID lpFlsData)
2226c2c66affSColin Finck {
2227c2c66affSColin Finck     ok(lpFlsData == (void*) 0x31415, "lpFlsData is %p, expected %p\n", lpFlsData, (void*) 0x31415);
2228c2c66affSColin Finck     InterlockedIncrement(&fls_callback_count);
2229c2c66affSColin Finck }
2230c2c66affSColin Finck 
dll_entry_point(HINSTANCE hinst,DWORD reason,LPVOID param)2231c2c66affSColin Finck static BOOL WINAPI dll_entry_point(HINSTANCE hinst, DWORD reason, LPVOID param)
2232c2c66affSColin Finck {
2233c2c66affSColin Finck     static LONG noop_thread_started;
2234c2c66affSColin Finck     static DWORD fls_index = FLS_OUT_OF_INDEXES;
2235c2c66affSColin Finck     static int fls_count = 0;
2236c2c66affSColin Finck     static int thread_detach_count = 0;
2237c2c66affSColin Finck     DWORD ret;
2238c2c66affSColin Finck 
2239c2c66affSColin Finck     ok(!inside_loader_lock, "inside_loader_lock should not be set\n");
2240c2c66affSColin Finck     ok(!inside_peb_lock, "inside_peb_lock should not be set\n");
2241c2c66affSColin Finck 
2242c2c66affSColin Finck     switch (reason)
2243c2c66affSColin Finck     {
2244c2c66affSColin Finck     case DLL_PROCESS_ATTACH:
2245c2c66affSColin Finck         trace("dll: %p, DLL_PROCESS_ATTACH, %p\n", hinst, param);
2246c2c66affSColin Finck 
2247c2c66affSColin Finck         ret = pRtlDllShutdownInProgress();
2248c2c66affSColin Finck         ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2249c2c66affSColin Finck 
2250c2c66affSColin Finck         /* Set up the FLS slot, if FLS is available */
2251c2c66affSColin Finck         if (pFlsGetValue)
2252c2c66affSColin Finck         {
2253c2c66affSColin Finck             void* value;
2254c2c66affSColin Finck             BOOL bret;
2255c2c66affSColin Finck             ret = pFlsAlloc(&fls_callback);
2256c2c66affSColin Finck             ok(ret != FLS_OUT_OF_INDEXES, "FlsAlloc returned %d\n", ret);
2257c2c66affSColin Finck             fls_index = ret;
2258c2c66affSColin Finck             SetLastError(0xdeadbeef);
2259c2c66affSColin Finck             value = pFlsGetValue(fls_index);
2260c2c66affSColin Finck             ok(!value, "FlsGetValue returned %p, expected NULL\n", value);
2261c2c66affSColin Finck             ok(GetLastError() == ERROR_SUCCESS, "FlsGetValue failed with error %u\n", GetLastError());
2262c2c66affSColin Finck             bret = pFlsSetValue(fls_index, (void*) 0x31415);
2263c2c66affSColin Finck             ok(bret, "FlsSetValue failed\n");
2264c2c66affSColin Finck             fls_count++;
2265c2c66affSColin Finck         }
2266c2c66affSColin Finck 
2267c2c66affSColin Finck         break;
2268c2c66affSColin Finck     case DLL_PROCESS_DETACH:
2269c2c66affSColin Finck     {
2270c2c66affSColin Finck         DWORD code, expected_code, i;
2271c2c66affSColin Finck         HANDLE handle, process;
2272c2c66affSColin Finck         void *addr;
2273c2c66affSColin Finck         SIZE_T size;
2274c2c66affSColin Finck         LARGE_INTEGER offset;
2275c2c66affSColin Finck         DEBUG_EVENT de;
2276c2c66affSColin Finck 
2277c2c66affSColin Finck         trace("dll: %p, DLL_PROCESS_DETACH, %p\n", hinst, param);
2278c2c66affSColin Finck 
2279c2c66affSColin Finck         if (test_dll_phase == 4 || test_dll_phase == 5)
2280c2c66affSColin Finck         {
2281c2c66affSColin Finck             ok(0, "dll_entry_point(DLL_PROCESS_DETACH) should not be called\n");
2282c2c66affSColin Finck             break;
2283c2c66affSColin Finck         }
2284c2c66affSColin Finck 
2285c2c66affSColin Finck         /* The process should already deadlock at this point */
2286c2c66affSColin Finck         if (test_dll_phase == 6)
2287c2c66affSColin Finck         {
2288c2c66affSColin Finck             /* In reality, code below never gets executed, probably some other
2289c2c66affSColin Finck              * code tries to access process heap and deadlocks earlier, even XP
2290c2c66affSColin Finck              * doesn't call the DLL entry point on process detach either.
2291c2c66affSColin Finck              */
2292c2c66affSColin Finck             HeapLock(GetProcessHeap());
2293c2c66affSColin Finck             ok(0, "dll_entry_point: process should already deadlock\n");
2294c2c66affSColin Finck             break;
2295c2c66affSColin Finck         }
2296c2c66affSColin Finck 
2297c2c66affSColin Finck         if (test_dll_phase == 0 || test_dll_phase == 1 || test_dll_phase == 3)
2298c2c66affSColin Finck             ok(param != NULL, "dll: param %p\n", param);
2299c2c66affSColin Finck         else
2300c2c66affSColin Finck             ok(!param, "dll: param %p\n", param);
2301c2c66affSColin Finck 
2302c2c66affSColin Finck         if (test_dll_phase == 0 || test_dll_phase == 1) expected_code = 195;
2303c2c66affSColin Finck         else if (test_dll_phase == 3) expected_code = 196;
2304c2c66affSColin Finck         else expected_code = STILL_ACTIVE;
2305c2c66affSColin Finck 
2306c2c66affSColin Finck         if (test_dll_phase == 3)
2307c2c66affSColin Finck         {
2308c2c66affSColin Finck             ret = pRtlDllShutdownInProgress();
2309c2c66affSColin Finck             ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
2310c2c66affSColin Finck         }
2311c2c66affSColin Finck         else
2312c2c66affSColin Finck         {
2313c2c66affSColin Finck             ret = pRtlDllShutdownInProgress();
2314c2c66affSColin Finck 
2315c2c66affSColin Finck             /* FIXME: remove once Wine is fixed */
2316c2c66affSColin Finck             todo_wine_if (!(expected_code == STILL_ACTIVE || expected_code == 196))
2317c2c66affSColin Finck                 ok(!ret || broken(ret) /* before Vista */, "RtlDllShutdownInProgress returned %d\n", ret);
2318c2c66affSColin Finck         }
2319c2c66affSColin Finck 
2320c2c66affSColin Finck         /* In the case that the process is terminating, FLS slots should still be accessible, but
2321c2c66affSColin Finck          * the callback should be already run for this thread and the contents already NULL.
2322c2c66affSColin Finck          * Note that this is broken for Win2k3, which runs the callbacks *after* the DLL entry
2323c2c66affSColin Finck          * point has already run.
2324c2c66affSColin Finck          */
2325c2c66affSColin Finck         if (param && pFlsGetValue)
2326c2c66affSColin Finck         {
2327c2c66affSColin Finck             void* value;
2328c2c66affSColin Finck             SetLastError(0xdeadbeef);
2329c2c66affSColin Finck             value = pFlsGetValue(fls_index);
2330c2c66affSColin Finck             todo_wine
2331c2c66affSColin Finck             {
2332c2c66affSColin Finck                 ok(broken(value == (void*) 0x31415) || /* Win2k3 */
2333c2c66affSColin Finck                    value == NULL, "FlsGetValue returned %p, expected NULL\n", value);
2334c2c66affSColin Finck             }
2335c2c66affSColin Finck             ok(GetLastError() == ERROR_SUCCESS, "FlsGetValue failed with error %u\n", GetLastError());
2336c2c66affSColin Finck             todo_wine
2337c2c66affSColin Finck             {
2338c2c66affSColin Finck                 ok(broken(fls_callback_count == thread_detach_count) || /* Win2k3 */
2339c2c66affSColin Finck                    fls_callback_count == thread_detach_count + 1,
2340c2c66affSColin Finck                    "wrong FLS callback count %d, expected %d\n", fls_callback_count, thread_detach_count + 1);
2341c2c66affSColin Finck             }
2342c2c66affSColin Finck         }
2343c2c66affSColin Finck         if (pFlsFree)
2344c2c66affSColin Finck         {
2345c2c66affSColin Finck             BOOL ret;
2346c2c66affSColin Finck             /* Call FlsFree now and run the remaining callbacks from uncleanly terminated threads */
2347c2c66affSColin Finck             ret = pFlsFree(fls_index);
2348c2c66affSColin Finck             ok(ret, "FlsFree failed with error %u\n", GetLastError());
2349c2c66affSColin Finck             fls_index = FLS_OUT_OF_INDEXES;
2350c2c66affSColin Finck             todo_wine
2351c2c66affSColin Finck             {
2352c2c66affSColin Finck                 ok(fls_callback_count == fls_count,
2353c2c66affSColin Finck                    "wrong FLS callback count %d, expected %d\n", fls_callback_count, fls_count);
2354c2c66affSColin Finck             }
2355c2c66affSColin Finck         }
2356c2c66affSColin Finck 
2357c2c66affSColin Finck         ok(attached_thread_count >= 2, "attached thread count should be >= 2\n");
2358c2c66affSColin Finck 
2359c2c66affSColin Finck         for (i = 0; i < attached_thread_count; i++)
2360c2c66affSColin Finck         {
2361c2c66affSColin Finck             /* Calling GetExitCodeThread() without waiting for thread termination
2362c2c66affSColin Finck              * leads to different results due to a race condition.
2363c2c66affSColin Finck              */
2364c2c66affSColin Finck             if (expected_code != STILL_ACTIVE)
2365c2c66affSColin Finck             {
2366c2c66affSColin Finck                 ret = WaitForSingleObject(attached_thread[i], 1000);
2367c2c66affSColin Finck                 ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2368c2c66affSColin Finck             }
2369c2c66affSColin Finck             ret = GetExitCodeThread(attached_thread[i], &code);
2370c2c66affSColin Finck             trace("dll: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
2371c2c66affSColin Finck             ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
2372c2c66affSColin Finck             ok(code == expected_code, "expected thread exit code %u, got %u\n", expected_code, code);
2373c2c66affSColin Finck         }
2374c2c66affSColin Finck 
2375c2c66affSColin Finck         ret = WaitForSingleObject(event, 0);
2376c2c66affSColin Finck         ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2377c2c66affSColin Finck 
2378c2c66affSColin Finck         ret = WaitForSingleObject(mutex, 0);
2379c2c66affSColin Finck         if (expected_code == STILL_ACTIVE)
2380c2c66affSColin Finck             ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2381c2c66affSColin Finck         else
2382c2c66affSColin Finck             ok(ret == WAIT_ABANDONED, "expected WAIT_ABANDONED, got %#x\n", ret);
2383c2c66affSColin Finck 
2384c2c66affSColin Finck         /* semaphore is not abandoned on thread termination */
2385c2c66affSColin Finck         ret = WaitForSingleObject(semaphore, 0);
2386c2c66affSColin Finck         ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2387c2c66affSColin Finck 
2388c2c66affSColin Finck         if (expected_code == STILL_ACTIVE)
2389c2c66affSColin Finck         {
2390c2c66affSColin Finck             ret = WaitForSingleObject(attached_thread[0], 0);
2391c2c66affSColin Finck             ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2392c2c66affSColin Finck             ret = WaitForSingleObject(attached_thread[1], 0);
2393c2c66affSColin Finck             ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2394c2c66affSColin Finck         }
2395c2c66affSColin Finck         else
2396c2c66affSColin Finck         {
2397c2c66affSColin Finck             ret = WaitForSingleObject(attached_thread[0], 0);
2398c2c66affSColin Finck             ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2399c2c66affSColin Finck             ret = WaitForSingleObject(attached_thread[1], 0);
2400c2c66affSColin Finck             ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2401c2c66affSColin Finck         }
2402c2c66affSColin Finck 
2403c2c66affSColin Finck         /* win7 doesn't allow creating a thread during process shutdown but
2404c2c66affSColin Finck          * earlier Windows versions allow it.
2405c2c66affSColin Finck          */
2406c2c66affSColin Finck         noop_thread_started = 0;
2407c2c66affSColin Finck         SetLastError(0xdeadbeef);
2408c2c66affSColin Finck         handle = CreateThread(NULL, 0, noop_thread_proc, &noop_thread_started, 0, &ret);
2409c2c66affSColin Finck         if (param)
2410c2c66affSColin Finck         {
2411c2c66affSColin Finck             ok(!handle || broken(handle != 0) /* before win7 */, "CreateThread should fail\n");
2412c2c66affSColin Finck             if (!handle)
2413c2c66affSColin Finck                 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2414c2c66affSColin Finck             else
2415c2c66affSColin Finck             {
2416c2c66affSColin Finck                 ret = WaitForSingleObject(handle, 1000);
2417c2c66affSColin Finck                 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2418c2c66affSColin Finck                 CloseHandle(handle);
2419c2c66affSColin Finck             }
2420c2c66affSColin Finck         }
2421c2c66affSColin Finck         else
2422c2c66affSColin Finck         {
2423c2c66affSColin Finck             ok(handle != 0, "CreateThread error %d\n", GetLastError());
2424c2c66affSColin Finck             ret = WaitForSingleObject(handle, 1000);
2425c2c66affSColin Finck             ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2426c2c66affSColin Finck             ok(!noop_thread_started || broken(noop_thread_started) /* XP64 */, "thread shouldn't start yet\n");
2427c2c66affSColin Finck             CloseHandle(handle);
2428c2c66affSColin Finck         }
2429c2c66affSColin Finck 
2430c2c66affSColin Finck         SetLastError(0xdeadbeef);
2431c2c66affSColin Finck         process = OpenProcess(PROCESS_ALL_ACCESS_NT4, FALSE, GetCurrentProcessId());
2432c2c66affSColin Finck         ok(process != NULL, "OpenProcess error %d\n", GetLastError());
2433c2c66affSColin Finck 
2434c2c66affSColin Finck         noop_thread_started = 0;
2435c2c66affSColin Finck         SetLastError(0xdeadbeef);
2436c2c66affSColin Finck         handle = CreateRemoteThread(process, NULL, 0, noop_thread_proc, &noop_thread_started, 0, &ret);
2437c2c66affSColin Finck         if (param)
2438c2c66affSColin Finck         {
2439c2c66affSColin Finck             ok(!handle || broken(handle != 0) /* before win7 */, "CreateRemoteThread should fail\n");
2440c2c66affSColin Finck             if (!handle)
2441c2c66affSColin Finck                 ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2442c2c66affSColin Finck             else
2443c2c66affSColin Finck             {
2444c2c66affSColin Finck                 ret = WaitForSingleObject(handle, 1000);
2445c2c66affSColin Finck                 ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2446c2c66affSColin Finck                 CloseHandle(handle);
2447c2c66affSColin Finck             }
2448c2c66affSColin Finck         }
2449c2c66affSColin Finck         else
2450c2c66affSColin Finck         {
2451c2c66affSColin Finck             ok(handle != 0, "CreateRemoteThread error %d\n", GetLastError());
2452c2c66affSColin Finck             ret = WaitForSingleObject(handle, 1000);
2453c2c66affSColin Finck             ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2454c2c66affSColin Finck             ok(!noop_thread_started || broken(noop_thread_started) /* XP64 */, "thread shouldn't start yet\n");
2455c2c66affSColin Finck             CloseHandle(handle);
2456c2c66affSColin Finck         }
2457c2c66affSColin Finck 
2458c2c66affSColin Finck         SetLastError(0xdeadbeef);
2459c2c66affSColin Finck         handle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, NULL);
2460c2c66affSColin Finck         ok(handle != 0, "CreateFileMapping error %d\n", GetLastError());
2461c2c66affSColin Finck 
2462c2c66affSColin Finck         offset.u.LowPart = 0;
2463c2c66affSColin Finck         offset.u.HighPart = 0;
2464c2c66affSColin Finck         addr = NULL;
2465c2c66affSColin Finck         size = 0;
2466c2c66affSColin Finck         ret = pNtMapViewOfSection(handle, process, &addr, 0, 0, &offset,
2467c2c66affSColin Finck                                   &size, 1 /* ViewShare */, 0, PAGE_READONLY);
2468c2c66affSColin Finck         ok(ret == STATUS_SUCCESS, "NtMapViewOfSection error %#x\n", ret);
2469c2c66affSColin Finck         ret = pNtUnmapViewOfSection(process, addr);
2470c2c66affSColin Finck         ok(ret == STATUS_SUCCESS, "NtUnmapViewOfSection error %#x\n", ret);
2471c2c66affSColin Finck 
2472c2c66affSColin Finck         CloseHandle(handle);
2473c2c66affSColin Finck         CloseHandle(process);
2474c2c66affSColin Finck 
2475c2c66affSColin Finck         handle = GetModuleHandleA("winver.exe");
2476c2c66affSColin Finck         ok(!handle, "winver.exe shouldn't be loaded yet\n");
2477c2c66affSColin Finck         SetLastError(0xdeadbeef);
2478c2c66affSColin Finck         handle = LoadLibraryA("winver.exe");
2479c2c66affSColin Finck         ok(handle != 0, "LoadLibrary error %d\n", GetLastError());
2480c2c66affSColin Finck         SetLastError(0xdeadbeef);
2481c2c66affSColin Finck         ret = FreeLibrary(handle);
2482c2c66affSColin Finck         ok(ret, "FreeLibrary error %d\n", GetLastError());
2483c2c66affSColin Finck         handle = GetModuleHandleA("winver.exe");
2484c2c66affSColin Finck         if (param)
2485c2c66affSColin Finck             ok(handle != 0, "winver.exe should not be unloaded\n");
2486c2c66affSColin Finck         else
2487c2c66affSColin Finck         todo_wine
2488c2c66affSColin Finck             ok(!handle || broken(handle != 0) /* before win7 */, "winver.exe should be unloaded\n");
2489c2c66affSColin Finck 
2490c2c66affSColin Finck         SetLastError(0xdeadbeef);
2491c2c66affSColin Finck         ret = WaitForDebugEvent(&de, 0);
2492c2c66affSColin Finck         ok(!ret, "WaitForDebugEvent should fail\n");
2493c2c66affSColin Finck todo_wine
2494c2c66affSColin Finck         ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
2495c2c66affSColin Finck 
2496c2c66affSColin Finck         SetLastError(0xdeadbeef);
2497c2c66affSColin Finck         ret = DebugActiveProcess(GetCurrentProcessId());
2498c2c66affSColin Finck         ok(!ret, "DebugActiveProcess should fail\n");
2499c2c66affSColin Finck         ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2500c2c66affSColin Finck 
2501c2c66affSColin Finck         SetLastError(0xdeadbeef);
2502c2c66affSColin Finck         ret = WaitForDebugEvent(&de, 0);
2503c2c66affSColin Finck         ok(!ret, "WaitForDebugEvent should fail\n");
2504c2c66affSColin Finck         ok(GetLastError() == ERROR_SEM_TIMEOUT, "expected ERROR_SEM_TIMEOUT, got %d\n", GetLastError());
2505c2c66affSColin Finck 
2506c2c66affSColin Finck         if (test_dll_phase == 2)
2507c2c66affSColin Finck         {
2508c2c66affSColin Finck             trace("dll: call ExitProcess()\n");
2509c2c66affSColin Finck             *child_failures = winetest_get_failures();
2510c2c66affSColin Finck             ExitProcess(197);
2511c2c66affSColin Finck         }
2512c2c66affSColin Finck         trace("dll: %p, DLL_PROCESS_DETACH, %p => DONE\n", hinst, param);
2513c2c66affSColin Finck         break;
2514c2c66affSColin Finck     }
2515c2c66affSColin Finck     case DLL_THREAD_ATTACH:
2516c2c66affSColin Finck         trace("dll: %p, DLL_THREAD_ATTACH, %p\n", hinst, param);
2517c2c66affSColin Finck 
2518c2c66affSColin Finck         ret = pRtlDllShutdownInProgress();
2519c2c66affSColin Finck         ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2520c2c66affSColin Finck 
2521c2c66affSColin Finck         if (attached_thread_count < MAX_COUNT)
2522c2c66affSColin Finck         {
2523c2c66affSColin Finck             DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &attached_thread[attached_thread_count],
2524c2c66affSColin Finck                             0, TRUE, DUPLICATE_SAME_ACCESS);
2525c2c66affSColin Finck             attached_thread_count++;
2526c2c66affSColin Finck         }
2527c2c66affSColin Finck 
2528c2c66affSColin Finck         /* Make sure the FLS slot is empty, if FLS is available */
2529c2c66affSColin Finck         if (pFlsGetValue)
2530c2c66affSColin Finck         {
2531c2c66affSColin Finck             void* value;
2532c2c66affSColin Finck             BOOL ret;
2533c2c66affSColin Finck             SetLastError(0xdeadbeef);
2534c2c66affSColin Finck             value = pFlsGetValue(fls_index);
2535c2c66affSColin Finck             ok(!value, "FlsGetValue returned %p, expected NULL\n", value);
2536c2c66affSColin Finck             todo_wine
2537c2c66affSColin Finck                 ok(GetLastError() == ERROR_SUCCESS, "FlsGetValue failed with error %u\n", GetLastError());
2538c2c66affSColin Finck             ret = pFlsSetValue(fls_index, (void*) 0x31415);
2539c2c66affSColin Finck             ok(ret, "FlsSetValue failed\n");
2540c2c66affSColin Finck             fls_count++;
2541c2c66affSColin Finck         }
2542c2c66affSColin Finck 
2543c2c66affSColin Finck         break;
2544c2c66affSColin Finck     case DLL_THREAD_DETACH:
2545c2c66affSColin Finck         trace("dll: %p, DLL_THREAD_DETACH, %p\n", hinst, param);
2546c2c66affSColin Finck         thread_detach_count++;
2547c2c66affSColin Finck 
2548c2c66affSColin Finck         ret = pRtlDllShutdownInProgress();
2549c2c66affSColin Finck         /* win7 doesn't allow creating a thread during process shutdown but
2550c2c66affSColin Finck          * earlier Windows versions allow it. In that case DLL_THREAD_DETACH is
2551c2c66affSColin Finck          * sent on thread exit, but DLL_THREAD_ATTACH is never received.
2552c2c66affSColin Finck          */
2553c2c66affSColin Finck         if (noop_thread_started)
2554c2c66affSColin Finck             ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
2555c2c66affSColin Finck         else
2556c2c66affSColin Finck             ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2557c2c66affSColin Finck 
2558c2c66affSColin Finck         /* FLS data should already be destroyed, if FLS is available.
2559c2c66affSColin Finck          * Note that this is broken for Win2k3, which runs the callbacks *after* the DLL entry
2560c2c66affSColin Finck          * point has already run.
2561c2c66affSColin Finck          */
2562c2c66affSColin Finck         if (pFlsGetValue && fls_index != FLS_OUT_OF_INDEXES)
2563c2c66affSColin Finck         {
2564c2c66affSColin Finck             void* value;
2565c2c66affSColin Finck             SetLastError(0xdeadbeef);
2566c2c66affSColin Finck             value = pFlsGetValue(fls_index);
2567c2c66affSColin Finck             todo_wine
2568c2c66affSColin Finck             {
2569c2c66affSColin Finck                 ok(broken(value == (void*) 0x31415) || /* Win2k3 */
2570c2c66affSColin Finck                    !value, "FlsGetValue returned %p, expected NULL\n", value);
2571c2c66affSColin Finck             }
2572c2c66affSColin Finck             ok(GetLastError() == ERROR_SUCCESS, "FlsGetValue failed with error %u\n", GetLastError());
2573c2c66affSColin Finck         }
2574c2c66affSColin Finck 
2575c2c66affSColin Finck         break;
2576c2c66affSColin Finck     default:
2577c2c66affSColin Finck         trace("dll: %p, %d, %p\n", hinst, reason, param);
2578c2c66affSColin Finck         break;
2579c2c66affSColin Finck     }
2580c2c66affSColin Finck 
2581c2c66affSColin Finck     *child_failures = winetest_get_failures();
2582c2c66affSColin Finck 
2583c2c66affSColin Finck     return TRUE;
2584c2c66affSColin Finck }
2585c2c66affSColin Finck 
child_process(const char * dll_name,DWORD target_offset)2586c2c66affSColin Finck static void child_process(const char *dll_name, DWORD target_offset)
2587c2c66affSColin Finck {
2588c2c66affSColin Finck     void *target;
2589c2c66affSColin Finck     DWORD ret, dummy, i, code, expected_code;
2590c2c66affSColin Finck     HANDLE file, thread, process;
2591c2c66affSColin Finck     HMODULE hmod;
2592c2c66affSColin Finck     struct PROCESS_BASIC_INFORMATION_PRIVATE pbi;
2593c2c66affSColin Finck     DWORD_PTR affinity;
2594c2c66affSColin Finck 
2595c2c66affSColin Finck     trace("phase %d: writing %p at %#x\n", test_dll_phase, dll_entry_point, target_offset);
2596c2c66affSColin Finck 
2597c2c66affSColin Finck     SetLastError(0xdeadbeef);
2598c2c66affSColin Finck     mutex = CreateMutexW(NULL, FALSE, NULL);
2599c2c66affSColin Finck     ok(mutex != 0, "CreateMutex error %d\n", GetLastError());
2600c2c66affSColin Finck 
2601c2c66affSColin Finck     SetLastError(0xdeadbeef);
2602c2c66affSColin Finck     semaphore = CreateSemaphoreW(NULL, 1, 1, NULL);
2603c2c66affSColin Finck     ok(semaphore != 0, "CreateSemaphore error %d\n", GetLastError());
2604c2c66affSColin Finck 
2605c2c66affSColin Finck     SetLastError(0xdeadbeef);
2606c2c66affSColin Finck     event = CreateEventW(NULL, TRUE, FALSE, NULL);
2607c2c66affSColin Finck     ok(event != 0, "CreateEvent error %d\n", GetLastError());
2608c2c66affSColin Finck 
2609c2c66affSColin Finck     SetLastError(0xdeadbeef);
2610c2c66affSColin Finck     loader_lock_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2611c2c66affSColin Finck     ok(loader_lock_event != 0, "CreateEvent error %d\n", GetLastError());
2612c2c66affSColin Finck 
2613c2c66affSColin Finck     SetLastError(0xdeadbeef);
2614c2c66affSColin Finck     peb_lock_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2615c2c66affSColin Finck     ok(peb_lock_event != 0, "CreateEvent error %d\n", GetLastError());
2616c2c66affSColin Finck 
2617c2c66affSColin Finck     SetLastError(0xdeadbeef);
2618c2c66affSColin Finck     heap_lock_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2619c2c66affSColin Finck     ok(heap_lock_event != 0, "CreateEvent error %d\n", GetLastError());
2620c2c66affSColin Finck 
2621c2c66affSColin Finck     SetLastError(0xdeadbeef);
2622c2c66affSColin Finck     ack_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2623c2c66affSColin Finck     ok(ack_event != 0, "CreateEvent error %d\n", GetLastError());
2624c2c66affSColin Finck 
2625c2c66affSColin Finck     file = CreateFileA(dll_name, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
2626c2c66affSColin Finck     if (file == INVALID_HANDLE_VALUE)
2627c2c66affSColin Finck     {
2628c2c66affSColin Finck         ok(0, "could not open %s\n", dll_name);
2629c2c66affSColin Finck         return;
2630c2c66affSColin Finck     }
2631c2c66affSColin Finck     SetFilePointer(file, target_offset, NULL, FILE_BEGIN);
2632c2c66affSColin Finck     SetLastError(0xdeadbeef);
2633c2c66affSColin Finck     target = dll_entry_point;
2634c2c66affSColin Finck     ret = WriteFile(file, &target, sizeof(target), &dummy, NULL);
2635c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
2636c2c66affSColin Finck     CloseHandle(file);
2637c2c66affSColin Finck 
2638c2c66affSColin Finck     SetLastError(0xdeadbeef);
2639c2c66affSColin Finck     hmod = LoadLibraryA(dll_name);
2640c2c66affSColin Finck     ok(hmod != 0, "LoadLibrary error %d\n", GetLastError());
2641c2c66affSColin Finck 
2642c2c66affSColin Finck     SetLastError(0xdeadbeef);
2643c2c66affSColin Finck     stop_event = CreateEventW(NULL, TRUE, FALSE, NULL);
2644c2c66affSColin Finck     ok(stop_event != 0, "CreateEvent error %d\n", GetLastError());
2645c2c66affSColin Finck 
2646c2c66affSColin Finck     SetLastError(0xdeadbeef);
2647c2c66affSColin Finck     thread = CreateThread(NULL, 0, mutex_thread_proc, event, 0, &dummy);
2648c2c66affSColin Finck     ok(thread != 0, "CreateThread error %d\n", GetLastError());
2649c2c66affSColin Finck     WaitForSingleObject(event, 3000);
2650c2c66affSColin Finck     CloseHandle(thread);
2651c2c66affSColin Finck 
2652c2c66affSColin Finck     ResetEvent(event);
2653c2c66affSColin Finck 
2654c2c66affSColin Finck     SetLastError(0xdeadbeef);
2655c2c66affSColin Finck     thread = CreateThread(NULL, 0, semaphore_thread_proc, event, 0, &dummy);
2656c2c66affSColin Finck     ok(thread != 0, "CreateThread error %d\n", GetLastError());
2657c2c66affSColin Finck     WaitForSingleObject(event, 3000);
2658c2c66affSColin Finck     CloseHandle(thread);
2659c2c66affSColin Finck 
2660c2c66affSColin Finck     ResetEvent(event);
2661c2c66affSColin Finck     Sleep(100);
2662c2c66affSColin Finck 
2663c2c66affSColin Finck     ok(attached_thread_count == 2, "attached thread count should be 2\n");
2664c2c66affSColin Finck     for (i = 0; i < attached_thread_count; i++)
2665c2c66affSColin Finck     {
2666c2c66affSColin Finck         ret = GetExitCodeThread(attached_thread[i], &code);
2667c2c66affSColin Finck         trace("child: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
2668c2c66affSColin Finck         ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
2669c2c66affSColin Finck         ok(code == STILL_ACTIVE, "expected thread exit code STILL_ACTIVE, got %u\n", code);
2670c2c66affSColin Finck     }
2671c2c66affSColin Finck 
2672c2c66affSColin Finck     ret = WaitForSingleObject(attached_thread[0], 0);
2673c2c66affSColin Finck     ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2674c2c66affSColin Finck     ret = WaitForSingleObject(attached_thread[1], 0);
2675c2c66affSColin Finck     ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2676c2c66affSColin Finck 
2677c2c66affSColin Finck     ret = WaitForSingleObject(event, 0);
2678c2c66affSColin Finck     ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2679c2c66affSColin Finck     ret = WaitForSingleObject(mutex, 0);
2680c2c66affSColin Finck     ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2681c2c66affSColin Finck     ret = WaitForSingleObject(semaphore, 0);
2682c2c66affSColin Finck     ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2683c2c66affSColin Finck 
2684c2c66affSColin Finck     ret = pRtlDllShutdownInProgress();
2685c2c66affSColin Finck     ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2686c2c66affSColin Finck 
2687c2c66affSColin Finck     SetLastError(0xdeadbeef);
2688c2c66affSColin Finck     process = OpenProcess(PROCESS_ALL_ACCESS_NT4, FALSE, GetCurrentProcessId());
2689c2c66affSColin Finck     ok(process != NULL, "OpenProcess error %d\n", GetLastError());
2690c2c66affSColin Finck 
2691c2c66affSColin Finck     SetLastError(0xdeadbeef);
2692c2c66affSColin Finck     ret = TerminateProcess(0, 195);
2693c2c66affSColin Finck     ok(!ret, "TerminateProcess(0) should fail\n");
2694c2c66affSColin Finck     ok(GetLastError() == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
2695c2c66affSColin Finck 
2696c2c66affSColin Finck     Sleep(100);
2697c2c66affSColin Finck 
2698c2c66affSColin Finck     affinity = 1;
2699c2c66affSColin Finck     ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
2700c2c66affSColin Finck     ok(!ret, "NtSetInformationProcess error %#x\n", ret);
2701c2c66affSColin Finck 
2702c2c66affSColin Finck     switch (test_dll_phase)
2703c2c66affSColin Finck     {
2704c2c66affSColin Finck     case 0:
2705c2c66affSColin Finck         ret = pRtlDllShutdownInProgress();
2706c2c66affSColin Finck         ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2707c2c66affSColin Finck 
2708c2c66affSColin Finck         trace("call NtTerminateProcess(0, 195)\n");
2709c2c66affSColin Finck         ret = pNtTerminateProcess(0, 195);
2710c2c66affSColin Finck         ok(!ret, "NtTerminateProcess error %#x\n", ret);
2711c2c66affSColin Finck 
2712c2c66affSColin Finck         memset(&pbi, 0, sizeof(pbi));
2713c2c66affSColin Finck         ret = pNtQueryInformationProcess(process, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
2714c2c66affSColin Finck         ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
2715c2c66affSColin Finck         ok(pbi.ExitStatus == STILL_ACTIVE || pbi.ExitStatus == 195,
2716c2c66affSColin Finck            "expected STILL_ACTIVE, got %lu\n", pbi.ExitStatus);
2717c2c66affSColin Finck         affinity = 1;
2718c2c66affSColin Finck         ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
2719c2c66affSColin Finck         ok(!ret, "NtSetInformationProcess error %#x\n", ret);
2720c2c66affSColin Finck 
2721c2c66affSColin Finck         ret = pRtlDllShutdownInProgress();
2722c2c66affSColin Finck         ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2723c2c66affSColin Finck 
2724c2c66affSColin Finck         hmod = GetModuleHandleA(dll_name);
2725c2c66affSColin Finck         ok(hmod != 0, "DLL should not be unloaded\n");
2726c2c66affSColin Finck 
2727c2c66affSColin Finck         SetLastError(0xdeadbeef);
2728c2c66affSColin Finck         thread = CreateThread(NULL, 0, noop_thread_proc, &dummy, 0, &ret);
2729c2c66affSColin Finck         ok(!thread || broken(thread != 0) /* before win7 */, "CreateThread should fail\n");
2730c2c66affSColin Finck         if (!thread)
2731c2c66affSColin Finck             ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2732c2c66affSColin Finck         else
2733c2c66affSColin Finck         {
2734c2c66affSColin Finck             ret = WaitForSingleObject(thread, 1000);
2735c2c66affSColin Finck             ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2736c2c66affSColin Finck             CloseHandle(thread);
2737c2c66affSColin Finck         }
2738c2c66affSColin Finck 
2739c2c66affSColin Finck         trace("call LdrShutdownProcess()\n");
2740c2c66affSColin Finck         pLdrShutdownProcess();
2741c2c66affSColin Finck 
2742c2c66affSColin Finck         ret = pRtlDllShutdownInProgress();
2743c2c66affSColin Finck         ok(ret, "RtlDllShutdownInProgress returned %d\n", ret);
2744c2c66affSColin Finck 
2745c2c66affSColin Finck         hmod = GetModuleHandleA(dll_name);
2746c2c66affSColin Finck         ok(hmod != 0, "DLL should not be unloaded\n");
2747c2c66affSColin Finck 
2748c2c66affSColin Finck         memset(&pbi, 0, sizeof(pbi));
2749c2c66affSColin Finck         ret = pNtQueryInformationProcess(process, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
2750c2c66affSColin Finck         ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
2751c2c66affSColin Finck         ok(pbi.ExitStatus == STILL_ACTIVE || pbi.ExitStatus == 195,
2752c2c66affSColin Finck            "expected STILL_ACTIVE, got %lu\n", pbi.ExitStatus);
2753c2c66affSColin Finck         affinity = 1;
2754c2c66affSColin Finck         ret = pNtSetInformationProcess(process, ProcessAffinityMask, &affinity, sizeof(affinity));
2755c2c66affSColin Finck         ok(!ret, "NtSetInformationProcess error %#x\n", ret);
2756c2c66affSColin Finck         break;
2757c2c66affSColin Finck 
2758c2c66affSColin Finck     case 1: /* normal ExitProcess */
2759c2c66affSColin Finck         ret = pRtlDllShutdownInProgress();
2760c2c66affSColin Finck         ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2761c2c66affSColin Finck         break;
2762c2c66affSColin Finck 
2763c2c66affSColin Finck     case 2: /* ExitProcess will be called by the PROCESS_DETACH handler */
2764c2c66affSColin Finck         ret = pRtlDllShutdownInProgress();
2765c2c66affSColin Finck         ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2766c2c66affSColin Finck 
2767c2c66affSColin Finck         trace("call FreeLibrary(%p)\n", hmod);
2768c2c66affSColin Finck         SetLastError(0xdeadbeef);
2769c2c66affSColin Finck         ret = FreeLibrary(hmod);
2770c2c66affSColin Finck         ok(ret, "FreeLibrary error %d\n", GetLastError());
2771c2c66affSColin Finck         hmod = GetModuleHandleA(dll_name);
2772c2c66affSColin Finck         ok(!hmod, "DLL should be unloaded\n");
2773c2c66affSColin Finck 
2774c2c66affSColin Finck         if (test_dll_phase == 2)
2775c2c66affSColin Finck             ok(0, "FreeLibrary+ExitProcess should never return\n");
2776c2c66affSColin Finck 
2777c2c66affSColin Finck         ret = pRtlDllShutdownInProgress();
2778c2c66affSColin Finck         ok(!ret, "RtlDllShutdownInProgress returned %d\n", ret);
2779c2c66affSColin Finck 
2780c2c66affSColin Finck         break;
2781c2c66affSColin Finck 
2782c2c66affSColin Finck     case 3:
2783c2c66affSColin Finck         trace("signalling thread exit\n");
2784c2c66affSColin Finck         SetEvent(stop_event);
2785c2c66affSColin Finck         break;
2786c2c66affSColin Finck 
2787c2c66affSColin Finck     case 4:
2788c2c66affSColin Finck         trace("setting loader_lock_event\n");
2789c2c66affSColin Finck         SetEvent(loader_lock_event);
2790c2c66affSColin Finck         WaitForSingleObject(ack_event, 1000);
2791c2c66affSColin Finck         ok(inside_loader_lock != 0, "inside_loader_lock is not set\n");
2792c2c66affSColin Finck 
2793c2c66affSColin Finck         /* calling NtTerminateProcess should not cause a deadlock */
2794c2c66affSColin Finck         trace("call NtTerminateProcess(0, 198)\n");
2795c2c66affSColin Finck         ret = pNtTerminateProcess(0, 198);
2796c2c66affSColin Finck         ok(!ret, "NtTerminateProcess error %#x\n", ret);
2797c2c66affSColin Finck 
2798c2c66affSColin Finck         *child_failures = winetest_get_failures();
2799c2c66affSColin Finck 
2800c2c66affSColin Finck         /* Windows fails to release loader lock acquired from another thread,
2801c2c66affSColin Finck          * so the LdrUnlockLoaderLock call fails here and ExitProcess deadlocks
2802c2c66affSColin Finck          * later on, so NtTerminateProcess is used instead.
2803c2c66affSColin Finck          */
2804c2c66affSColin Finck         trace("call NtTerminateProcess(GetCurrentProcess(), 198)\n");
2805c2c66affSColin Finck         pNtTerminateProcess(GetCurrentProcess(), 198);
2806c2c66affSColin Finck         ok(0, "NtTerminateProcess should not return\n");
2807c2c66affSColin Finck         break;
2808c2c66affSColin Finck 
2809c2c66affSColin Finck     case 5:
2810c2c66affSColin Finck         trace("setting peb_lock_event\n");
2811c2c66affSColin Finck         SetEvent(peb_lock_event);
2812c2c66affSColin Finck         WaitForSingleObject(ack_event, 1000);
2813c2c66affSColin Finck         ok(inside_peb_lock != 0, "inside_peb_lock is not set\n");
2814c2c66affSColin Finck 
2815c2c66affSColin Finck         *child_failures = winetest_get_failures();
2816c2c66affSColin Finck 
2817c2c66affSColin Finck         /* calling ExitProcess should cause a deadlock */
2818c2c66affSColin Finck         trace("call ExitProcess(198)\n");
2819c2c66affSColin Finck         ExitProcess(198);
2820c2c66affSColin Finck         ok(0, "ExitProcess should not return\n");
2821c2c66affSColin Finck         break;
2822c2c66affSColin Finck 
2823c2c66affSColin Finck     case 6:
2824c2c66affSColin Finck         trace("setting heap_lock_event\n");
2825c2c66affSColin Finck         SetEvent(heap_lock_event);
2826c2c66affSColin Finck         WaitForSingleObject(ack_event, 1000);
2827c2c66affSColin Finck         ok(inside_heap_lock != 0, "inside_heap_lock is not set\n");
2828c2c66affSColin Finck 
2829c2c66affSColin Finck         *child_failures = winetest_get_failures();
2830c2c66affSColin Finck 
2831c2c66affSColin Finck         /* calling ExitProcess should cause a deadlock */
2832c2c66affSColin Finck         trace("call ExitProcess(1)\n");
2833c2c66affSColin Finck         ExitProcess(1);
2834c2c66affSColin Finck         ok(0, "ExitProcess should not return\n");
2835c2c66affSColin Finck         break;
2836c2c66affSColin Finck 
2837c2c66affSColin Finck     default:
2838c2c66affSColin Finck         assert(0);
2839c2c66affSColin Finck         break;
2840c2c66affSColin Finck     }
2841c2c66affSColin Finck 
2842c2c66affSColin Finck     if (test_dll_phase == 0) expected_code = 195;
2843c2c66affSColin Finck     else if (test_dll_phase == 3) expected_code = 196;
2844c2c66affSColin Finck     else if (test_dll_phase == 4) expected_code = 198;
2845c2c66affSColin Finck     else expected_code = STILL_ACTIVE;
2846c2c66affSColin Finck 
2847c2c66affSColin Finck     if (expected_code == STILL_ACTIVE)
2848c2c66affSColin Finck     {
2849c2c66affSColin Finck         ret = WaitForSingleObject(attached_thread[0], 100);
2850c2c66affSColin Finck         ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2851c2c66affSColin Finck         ret = WaitForSingleObject(attached_thread[1], 100);
2852c2c66affSColin Finck         ok(ret == WAIT_TIMEOUT, "expected WAIT_TIMEOUT, got %#x\n", ret);
2853c2c66affSColin Finck     }
2854c2c66affSColin Finck     else
2855c2c66affSColin Finck     {
28569a436e2dSAmine Khaldi         ret = WaitForSingleObject(attached_thread[0], 2000);
2857c2c66affSColin Finck         ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
28589a436e2dSAmine Khaldi         ret = WaitForSingleObject(attached_thread[1], 2000);
2859c2c66affSColin Finck         ok(ret == WAIT_OBJECT_0, "expected WAIT_OBJECT_0, got %#x\n", ret);
2860c2c66affSColin Finck     }
2861c2c66affSColin Finck 
2862c2c66affSColin Finck     for (i = 0; i < attached_thread_count; i++)
2863c2c66affSColin Finck     {
2864c2c66affSColin Finck         ret = GetExitCodeThread(attached_thread[i], &code);
2865c2c66affSColin Finck         trace("child: GetExitCodeThread(%u) => %d,%u\n", i, ret, code);
2866c2c66affSColin Finck         ok(ret == 1, "GetExitCodeThread returned %d, expected 1\n", ret);
2867c2c66affSColin Finck         ok(code == expected_code, "expected thread exit code %u, got %u\n", expected_code, code);
2868c2c66affSColin Finck     }
2869c2c66affSColin Finck 
2870c2c66affSColin Finck     *child_failures = winetest_get_failures();
2871c2c66affSColin Finck 
2872c2c66affSColin Finck     trace("call ExitProcess(195)\n");
2873c2c66affSColin Finck     ExitProcess(195);
2874c2c66affSColin Finck }
2875c2c66affSColin Finck 
test_ExitProcess(void)2876c2c66affSColin Finck static void test_ExitProcess(void)
2877c2c66affSColin Finck {
2878c2c66affSColin Finck #include "pshpack1.h"
2879c2c66affSColin Finck #ifdef __x86_64__
2880c2c66affSColin Finck     static struct section_data
2881c2c66affSColin Finck     {
2882c2c66affSColin Finck         BYTE mov_rax[2];
2883c2c66affSColin Finck         void *target;
2884c2c66affSColin Finck         BYTE jmp_rax[2];
2885c2c66affSColin Finck     } section_data = { { 0x48,0xb8 }, dll_entry_point, { 0xff,0xe0 } };
2886c2c66affSColin Finck #else
2887c2c66affSColin Finck     static struct section_data
2888c2c66affSColin Finck     {
2889c2c66affSColin Finck         BYTE mov_eax;
2890c2c66affSColin Finck         void *target;
2891c2c66affSColin Finck         BYTE jmp_eax[2];
2892c2c66affSColin Finck     } section_data = { 0xb8, dll_entry_point, { 0xff,0xe0 } };
2893c2c66affSColin Finck #endif
2894c2c66affSColin Finck #include "poppack.h"
2895c2c66affSColin Finck     DWORD dummy, file_align;
2896c2c66affSColin Finck     HANDLE file, thread, process, hmap, hmap_dup;
2897c2c66affSColin Finck     char temp_path[MAX_PATH], dll_name[MAX_PATH], cmdline[MAX_PATH * 2];
2898c2c66affSColin Finck     DWORD ret, target_offset, old_prot;
2899c2c66affSColin Finck     char **argv, buf[256];
2900c2c66affSColin Finck     PROCESS_INFORMATION pi;
2901c2c66affSColin Finck     STARTUPINFOA si = { sizeof(si) };
2902c2c66affSColin Finck     CONTEXT ctx;
2903c2c66affSColin Finck     struct PROCESS_BASIC_INFORMATION_PRIVATE pbi;
2904c2c66affSColin Finck     MEMORY_BASIC_INFORMATION mbi;
2905c2c66affSColin Finck     DWORD_PTR affinity;
2906c2c66affSColin Finck     void *addr;
2907c2c66affSColin Finck     LARGE_INTEGER offset;
2908c2c66affSColin Finck     SIZE_T size;
2909c2c66affSColin Finck     IMAGE_NT_HEADERS nt_header;
2910c2c66affSColin Finck 
2911c2c66affSColin Finck #if !defined(__i386__) && !defined(__x86_64__)
2912c2c66affSColin Finck     skip("x86 specific ExitProcess test\n");
2913c2c66affSColin Finck     return;
2914c2c66affSColin Finck #endif
2915c2c66affSColin Finck 
2916c2c66affSColin Finck     if (!pRtlDllShutdownInProgress)
2917c2c66affSColin Finck     {
2918c2c66affSColin Finck         win_skip("RtlDllShutdownInProgress is not available on this platform (XP+)\n");
2919c2c66affSColin Finck         return;
2920c2c66affSColin Finck     }
2921c2c66affSColin Finck     if (!pNtQueryInformationProcess || !pNtSetInformationProcess)
2922c2c66affSColin Finck     {
2923c2c66affSColin Finck         win_skip("NtQueryInformationProcess/NtSetInformationProcess are not available on this platform\n");
2924c2c66affSColin Finck         return;
2925c2c66affSColin Finck     }
2926c2c66affSColin Finck     if (!pNtAllocateVirtualMemory || !pNtFreeVirtualMemory)
2927c2c66affSColin Finck     {
2928c2c66affSColin Finck         win_skip("NtAllocateVirtualMemory/NtFreeVirtualMemory are not available on this platform\n");
2929c2c66affSColin Finck         return;
2930c2c66affSColin Finck     }
2931c2c66affSColin Finck 
2932c2c66affSColin Finck     /* prevent displaying of the "Unable to load this DLL" message box */
2933c2c66affSColin Finck     SetErrorMode(SEM_FAILCRITICALERRORS);
2934c2c66affSColin Finck 
2935c2c66affSColin Finck     GetTempPathA(MAX_PATH, temp_path);
2936c2c66affSColin Finck     GetTempFileNameA(temp_path, "ldr", 0, dll_name);
2937c2c66affSColin Finck 
2938c2c66affSColin Finck     /*trace("creating %s\n", dll_name);*/
2939c2c66affSColin Finck     file = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
2940c2c66affSColin Finck     if (file == INVALID_HANDLE_VALUE)
2941c2c66affSColin Finck     {
2942c2c66affSColin Finck         ok(0, "could not create %s\n", dll_name);
2943c2c66affSColin Finck         return;
2944c2c66affSColin Finck     }
2945c2c66affSColin Finck 
2946c2c66affSColin Finck     SetLastError(0xdeadbeef);
2947c2c66affSColin Finck     ret = WriteFile(file, &dos_header, sizeof(dos_header), &dummy, NULL);
2948c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
2949c2c66affSColin Finck 
2950c2c66affSColin Finck     nt_header = nt_header_template;
2951c2c66affSColin Finck     nt_header.FileHeader.NumberOfSections = 1;
2952c2c66affSColin Finck     nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
2953c2c66affSColin Finck     nt_header.FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL | IMAGE_FILE_RELOCS_STRIPPED;
2954c2c66affSColin Finck 
2955c2c66affSColin Finck     nt_header.OptionalHeader.AddressOfEntryPoint = 0x1000;
2956c2c66affSColin Finck     nt_header.OptionalHeader.SectionAlignment = 0x1000;
2957c2c66affSColin Finck     nt_header.OptionalHeader.FileAlignment = 0x200;
2958c2c66affSColin Finck     nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000;
2959c2c66affSColin Finck     nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER);
2960c2c66affSColin Finck     SetLastError(0xdeadbeef);
2961c2c66affSColin Finck     ret = WriteFile(file, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
2962c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
2963c2c66affSColin Finck     SetLastError(0xdeadbeef);
2964c2c66affSColin Finck     ret = WriteFile(file, &nt_header.OptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER), &dummy, NULL);
2965c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
2966c2c66affSColin Finck 
2967c2c66affSColin Finck     section.SizeOfRawData = sizeof(section_data);
2968c2c66affSColin Finck     section.PointerToRawData = nt_header.OptionalHeader.FileAlignment;
2969c2c66affSColin Finck     section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
2970c2c66affSColin Finck     section.Misc.VirtualSize = sizeof(section_data);
2971c2c66affSColin Finck     section.Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
2972c2c66affSColin Finck     SetLastError(0xdeadbeef);
2973c2c66affSColin Finck     ret = WriteFile(file, &section, sizeof(section), &dummy, NULL);
2974c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
2975c2c66affSColin Finck 
2976c2c66affSColin Finck     file_align = nt_header.OptionalHeader.FileAlignment - nt_header.OptionalHeader.SizeOfHeaders;
2977c2c66affSColin Finck     assert(file_align < sizeof(filler));
2978c2c66affSColin Finck     SetLastError(0xdeadbeef);
2979c2c66affSColin Finck     ret = WriteFile(file, filler, file_align, &dummy, NULL);
2980c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
2981c2c66affSColin Finck 
2982c2c66affSColin Finck     target_offset = SetFilePointer(file, 0, NULL, FILE_CURRENT) + FIELD_OFFSET(struct section_data, target);
2983c2c66affSColin Finck 
2984c2c66affSColin Finck     /* section data */
2985c2c66affSColin Finck     SetLastError(0xdeadbeef);
2986c2c66affSColin Finck     ret = WriteFile(file, &section_data, sizeof(section_data), &dummy, NULL);
2987c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
2988c2c66affSColin Finck 
2989c2c66affSColin Finck     CloseHandle(file);
2990c2c66affSColin Finck 
2991c2c66affSColin Finck     winetest_get_mainargs(&argv);
2992c2c66affSColin Finck 
2993c2c66affSColin Finck     /* phase 0 */
2994c2c66affSColin Finck     *child_failures = -1;
2995c2c66affSColin Finck     sprintf(cmdline, "\"%s\" loader %s %u 0", argv[0], dll_name, target_offset);
2996c2c66affSColin Finck     ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
2997c2c66affSColin Finck     ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
2998c2c66affSColin Finck     ret = WaitForSingleObject(pi.hProcess, 10000);
2999c2c66affSColin Finck     ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
3000c2c66affSColin Finck     if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
3001c2c66affSColin Finck     GetExitCodeProcess(pi.hProcess, &ret);
3002c2c66affSColin Finck     ok(ret == 195, "expected exit code 195, got %u\n", ret);
3003c2c66affSColin Finck     if (*child_failures)
3004c2c66affSColin Finck     {
3005c2c66affSColin Finck         trace("%d failures in child process\n", *child_failures);
3006c2c66affSColin Finck         winetest_add_failures(*child_failures);
3007c2c66affSColin Finck     }
3008c2c66affSColin Finck     CloseHandle(pi.hThread);
3009c2c66affSColin Finck     CloseHandle(pi.hProcess);
3010c2c66affSColin Finck 
3011c2c66affSColin Finck     /* phase 1 */
3012c2c66affSColin Finck     *child_failures = -1;
3013c2c66affSColin Finck     sprintf(cmdline, "\"%s\" loader %s %u 1", argv[0], dll_name, target_offset);
3014c2c66affSColin Finck     ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
3015c2c66affSColin Finck     ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
3016c2c66affSColin Finck     ret = WaitForSingleObject(pi.hProcess, 10000);
3017c2c66affSColin Finck     ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
3018c2c66affSColin Finck     if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
3019c2c66affSColin Finck     GetExitCodeProcess(pi.hProcess, &ret);
3020c2c66affSColin Finck     ok(ret == 195, "expected exit code 195, got %u\n", ret);
3021c2c66affSColin Finck     if (*child_failures)
3022c2c66affSColin Finck     {
3023c2c66affSColin Finck         trace("%d failures in child process\n", *child_failures);
3024c2c66affSColin Finck         winetest_add_failures(*child_failures);
3025c2c66affSColin Finck     }
3026c2c66affSColin Finck     CloseHandle(pi.hThread);
3027c2c66affSColin Finck     CloseHandle(pi.hProcess);
3028c2c66affSColin Finck 
3029c2c66affSColin Finck     /* phase 2 */
3030c2c66affSColin Finck     *child_failures = -1;
3031c2c66affSColin Finck     sprintf(cmdline, "\"%s\" loader %s %u 2", argv[0], dll_name, target_offset);
3032c2c66affSColin Finck     ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
3033c2c66affSColin Finck     ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
3034c2c66affSColin Finck     ret = WaitForSingleObject(pi.hProcess, 10000);
3035c2c66affSColin Finck     ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
3036c2c66affSColin Finck     if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
3037c2c66affSColin Finck     GetExitCodeProcess(pi.hProcess, &ret);
3038c2c66affSColin Finck     ok(ret == 197, "expected exit code 197, got %u\n", ret);
3039c2c66affSColin Finck     if (*child_failures)
3040c2c66affSColin Finck     {
3041c2c66affSColin Finck         trace("%d failures in child process\n", *child_failures);
3042c2c66affSColin Finck         winetest_add_failures(*child_failures);
3043c2c66affSColin Finck     }
3044c2c66affSColin Finck     CloseHandle(pi.hThread);
3045c2c66affSColin Finck     CloseHandle(pi.hProcess);
3046c2c66affSColin Finck 
3047c2c66affSColin Finck     /* phase 3 */
3048c2c66affSColin Finck     *child_failures = -1;
3049c2c66affSColin Finck     sprintf(cmdline, "\"%s\" loader %s %u 3", argv[0], dll_name, target_offset);
3050c2c66affSColin Finck     ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
3051c2c66affSColin Finck     ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
3052c2c66affSColin Finck     ret = WaitForSingleObject(pi.hProcess, 10000);
3053c2c66affSColin Finck     ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
3054c2c66affSColin Finck     if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
3055c2c66affSColin Finck     GetExitCodeProcess(pi.hProcess, &ret);
3056c2c66affSColin Finck     ok(ret == 195, "expected exit code 195, got %u\n", ret);
3057c2c66affSColin Finck     if (*child_failures)
3058c2c66affSColin Finck     {
3059c2c66affSColin Finck         trace("%d failures in child process\n", *child_failures);
3060c2c66affSColin Finck         winetest_add_failures(*child_failures);
3061c2c66affSColin Finck     }
3062c2c66affSColin Finck     CloseHandle(pi.hThread);
3063c2c66affSColin Finck     CloseHandle(pi.hProcess);
3064c2c66affSColin Finck 
3065c2c66affSColin Finck     /* phase 4 */
3066c2c66affSColin Finck     if (pLdrLockLoaderLock && pLdrUnlockLoaderLock)
3067c2c66affSColin Finck     {
3068c2c66affSColin Finck         *child_failures = -1;
3069c2c66affSColin Finck         sprintf(cmdline, "\"%s\" loader %s %u 4", argv[0], dll_name, target_offset);
3070c2c66affSColin Finck         ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
3071c2c66affSColin Finck         ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
3072c2c66affSColin Finck         ret = WaitForSingleObject(pi.hProcess, 10000);
3073c2c66affSColin Finck         ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
3074c2c66affSColin Finck         if (ret != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 0);
3075c2c66affSColin Finck         GetExitCodeProcess(pi.hProcess, &ret);
3076c2c66affSColin Finck         ok(ret == 198, "expected exit code 198, got %u\n", ret);
3077c2c66affSColin Finck         if (*child_failures)
3078c2c66affSColin Finck         {
3079c2c66affSColin Finck             trace("%d failures in child process\n", *child_failures);
3080c2c66affSColin Finck             winetest_add_failures(*child_failures);
3081c2c66affSColin Finck         }
3082c2c66affSColin Finck         CloseHandle(pi.hThread);
3083c2c66affSColin Finck         CloseHandle(pi.hProcess);
3084c2c66affSColin Finck     }
3085c2c66affSColin Finck     else
3086c2c66affSColin Finck         win_skip("LdrLockLoaderLock/LdrUnlockLoaderLock are not available on this platform\n");
3087c2c66affSColin Finck 
3088c2c66affSColin Finck     /* phase 5 */
3089c2c66affSColin Finck     if (pRtlAcquirePebLock && pRtlReleasePebLock)
3090c2c66affSColin Finck     {
3091c2c66affSColin Finck         *child_failures = -1;
3092c2c66affSColin Finck         sprintf(cmdline, "\"%s\" loader %s %u 5", argv[0], dll_name, target_offset);
3093c2c66affSColin Finck         ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
3094c2c66affSColin Finck         ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
3095c2c66affSColin Finck         ret = WaitForSingleObject(pi.hProcess, 5000);
3096c2c66affSColin Finck         ok(ret == WAIT_TIMEOUT, "child process should fail to terminate\n");
3097c2c66affSColin Finck         if (ret != WAIT_OBJECT_0)
3098c2c66affSColin Finck         {
3099c2c66affSColin Finck             trace("terminating child process\n");
3100c2c66affSColin Finck             TerminateProcess(pi.hProcess, 199);
3101c2c66affSColin Finck         }
3102c2c66affSColin Finck         ret = WaitForSingleObject(pi.hProcess, 1000);
3103c2c66affSColin Finck         ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
3104c2c66affSColin Finck         GetExitCodeProcess(pi.hProcess, &ret);
3105c2c66affSColin Finck         ok(ret == 199, "expected exit code 199, got %u\n", ret);
3106c2c66affSColin Finck         if (*child_failures)
3107c2c66affSColin Finck         {
3108c2c66affSColin Finck             trace("%d failures in child process\n", *child_failures);
3109c2c66affSColin Finck             winetest_add_failures(*child_failures);
3110c2c66affSColin Finck         }
3111c2c66affSColin Finck         CloseHandle(pi.hThread);
3112c2c66affSColin Finck         CloseHandle(pi.hProcess);
3113c2c66affSColin Finck     }
3114c2c66affSColin Finck     else
3115c2c66affSColin Finck         win_skip("RtlAcquirePebLock/RtlReleasePebLock are not available on this platform\n");
3116c2c66affSColin Finck 
3117c2c66affSColin Finck     /* phase 6 */
3118c2c66affSColin Finck     *child_failures = -1;
3119c2c66affSColin Finck     sprintf(cmdline, "\"%s\" loader %s %u 6", argv[0], dll_name, target_offset);
3120c2c66affSColin Finck     ret = CreateProcessA(argv[0], cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
3121c2c66affSColin Finck     ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
3122c2c66affSColin Finck     ret = WaitForSingleObject(pi.hProcess, 5000);
3123c2c66affSColin Finck     ok(ret == WAIT_TIMEOUT || broken(ret == WAIT_OBJECT_0) /* XP */, "child process should fail to terminate\n");
3124c2c66affSColin Finck     if (ret != WAIT_OBJECT_0)
3125c2c66affSColin Finck     {
3126c2c66affSColin Finck         trace("terminating child process\n");
3127c2c66affSColin Finck         TerminateProcess(pi.hProcess, 201);
3128c2c66affSColin Finck     }
3129c2c66affSColin Finck     ret = WaitForSingleObject(pi.hProcess, 1000);
3130c2c66affSColin Finck     ok(ret == WAIT_OBJECT_0, "child process failed to terminate\n");
3131c2c66affSColin Finck     GetExitCodeProcess(pi.hProcess, &ret);
3132c2c66affSColin Finck     ok(ret == 201 || broken(ret == 1) /* XP */, "expected exit code 201, got %u\n", ret);
3133c2c66affSColin Finck     if (*child_failures)
3134c2c66affSColin Finck     {
3135c2c66affSColin Finck         trace("%d failures in child process\n", *child_failures);
3136c2c66affSColin Finck         winetest_add_failures(*child_failures);
3137c2c66affSColin Finck     }
3138c2c66affSColin Finck     CloseHandle(pi.hThread);
3139c2c66affSColin Finck     CloseHandle(pi.hProcess);
3140c2c66affSColin Finck 
3141c2c66affSColin Finck     /* test remote process termination */
3142c2c66affSColin Finck     SetLastError(0xdeadbeef);
3143c2c66affSColin Finck     ret = CreateProcessA(argv[0], NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
3144c2c66affSColin Finck     ok(ret, "CreateProcess(%s) error %d\n", argv[0], GetLastError());
3145c2c66affSColin Finck 
3146c2c66affSColin Finck     SetLastError(0xdeadbeef);
3147c2c66affSColin Finck     addr = VirtualAllocEx(pi.hProcess, NULL, 4096, MEM_COMMIT, PAGE_READWRITE);
3148c2c66affSColin Finck     ok(addr != NULL, "VirtualAllocEx error %d\n", GetLastError());
3149c2c66affSColin Finck     SetLastError(0xdeadbeef);
3150c2c66affSColin Finck     ret = VirtualProtectEx(pi.hProcess, addr, 4096, PAGE_READONLY, &old_prot);
3151c2c66affSColin Finck     ok(ret, "VirtualProtectEx error %d\n", GetLastError());
3152c2c66affSColin Finck     ok(old_prot == PAGE_READWRITE, "expected PAGE_READWRITE, got %#x\n", old_prot);
3153c2c66affSColin Finck     SetLastError(0xdeadbeef);
3154c2c66affSColin Finck     size = VirtualQueryEx(pi.hProcess, NULL, &mbi, sizeof(mbi));
3155c2c66affSColin Finck     ok(size == sizeof(mbi), "VirtualQueryEx error %d\n", GetLastError());
3156c2c66affSColin Finck 
3157c2c66affSColin Finck     SetLastError(0xdeadbeef);
3158c2c66affSColin Finck     ret = ReadProcessMemory(pi.hProcess, addr, buf, 4, &size);
3159c2c66affSColin Finck     ok(ret, "ReadProcessMemory error %d\n", GetLastError());
3160c2c66affSColin Finck     ok(size == 4, "expected 4, got %lu\n", size);
3161c2c66affSColin Finck 
3162c2c66affSColin Finck     SetLastError(0xdeadbeef);
3163c2c66affSColin Finck     hmap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, NULL);
3164c2c66affSColin Finck     ok(hmap != 0, "CreateFileMapping error %d\n", GetLastError());
3165c2c66affSColin Finck 
3166c2c66affSColin Finck     SetLastError(0xdeadbeef);
3167c2c66affSColin Finck     ret = DuplicateHandle(GetCurrentProcess(), hmap, pi.hProcess, &hmap_dup,
3168c2c66affSColin Finck                           0, FALSE, DUPLICATE_SAME_ACCESS);
3169c2c66affSColin Finck     ok(ret, "DuplicateHandle error %d\n", GetLastError());
3170c2c66affSColin Finck 
3171c2c66affSColin Finck     offset.u.LowPart = 0;
3172c2c66affSColin Finck     offset.u.HighPart = 0;
3173c2c66affSColin Finck     addr = NULL;
3174c2c66affSColin Finck     size = 0;
3175c2c66affSColin Finck     ret = pNtMapViewOfSection(hmap, pi.hProcess, &addr, 0, 0, &offset,
3176c2c66affSColin Finck                               &size, 1 /* ViewShare */, 0, PAGE_READONLY);
3177c2c66affSColin Finck     ok(!ret, "NtMapViewOfSection error %#x\n", ret);
3178c2c66affSColin Finck     ret = pNtUnmapViewOfSection(pi.hProcess, addr);
3179c2c66affSColin Finck     ok(!ret, "NtUnmapViewOfSection error %#x\n", ret);
3180c2c66affSColin Finck 
3181c2c66affSColin Finck     SetLastError(0xdeadbeef);
3182c2c66affSColin Finck     thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void *)0xdeadbeef, NULL, CREATE_SUSPENDED, &ret);
3183c2c66affSColin Finck     ok(thread != 0, "CreateRemoteThread error %d\n", GetLastError());
3184c2c66affSColin Finck     SetLastError(0xdeadbeef);
3185c2c66affSColin Finck     ctx.ContextFlags = CONTEXT_INTEGER;
3186c2c66affSColin Finck     ret = GetThreadContext(thread, &ctx);
3187c2c66affSColin Finck     ok(ret, "GetThreadContext error %d\n", GetLastError());
3188c2c66affSColin Finck     SetLastError(0xdeadbeef);
3189c2c66affSColin Finck     ctx.ContextFlags = CONTEXT_INTEGER;
3190c2c66affSColin Finck     ret = SetThreadContext(thread, &ctx);
3191c2c66affSColin Finck     ok(ret, "SetThreadContext error %d\n", GetLastError());
3192c2c66affSColin Finck     SetLastError(0xdeadbeef);
3193c2c66affSColin Finck     ret = SetThreadPriority(thread, 0);
3194c2c66affSColin Finck     ok(ret, "SetThreadPriority error %d\n", GetLastError());
3195c2c66affSColin Finck 
3196c2c66affSColin Finck     SetLastError(0xdeadbeef);
3197c2c66affSColin Finck     ret = TerminateThread(thread, 199);
3198c2c66affSColin Finck     ok(ret, "TerminateThread error %d\n", GetLastError());
3199c2c66affSColin Finck     /* Calling GetExitCodeThread() without waiting for thread termination
3200c2c66affSColin Finck      * leads to different results due to a race condition.
3201c2c66affSColin Finck      */
3202c2c66affSColin Finck     ret = WaitForSingleObject(thread, 1000);
3203c2c66affSColin Finck     ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
3204c2c66affSColin Finck     GetExitCodeThread(thread, &ret);
3205c2c66affSColin Finck     ok(ret == 199, "expected exit code 199, got %u\n", ret);
3206c2c66affSColin Finck 
3207c2c66affSColin Finck     SetLastError(0xdeadbeef);
3208c2c66affSColin Finck     ret = TerminateProcess(pi.hProcess, 198);
3209c2c66affSColin Finck     ok(ret, "TerminateProcess error %d\n", GetLastError());
3210c2c66affSColin Finck     /* Checking process state without waiting for process termination
3211c2c66affSColin Finck      * leads to different results due to a race condition.
3212c2c66affSColin Finck      */
3213c2c66affSColin Finck     ret = WaitForSingleObject(pi.hProcess, 1000);
3214c2c66affSColin Finck     ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed: %x\n", ret);
3215c2c66affSColin Finck 
3216c2c66affSColin Finck     SetLastError(0xdeadbeef);
3217c2c66affSColin Finck     process = OpenProcess(PROCESS_ALL_ACCESS_NT4, FALSE, pi.dwProcessId);
3218c2c66affSColin Finck     ok(process != NULL, "OpenProcess error %d\n", GetLastError());
3219c2c66affSColin Finck     CloseHandle(process);
3220c2c66affSColin Finck 
3221c2c66affSColin Finck     memset(&pbi, 0, sizeof(pbi));
3222c2c66affSColin Finck     ret = pNtQueryInformationProcess(pi.hProcess, ProcessBasicInformation, &pbi, sizeof(pbi), NULL);
3223c2c66affSColin Finck     ok(!ret, "NtQueryInformationProcess error %#x\n", ret);
3224c2c66affSColin Finck     ok(pbi.ExitStatus == 198, "expected 198, got %lu\n", pbi.ExitStatus);
3225c2c66affSColin Finck     affinity = 1;
3226c2c66affSColin Finck     ret = pNtSetInformationProcess(pi.hProcess, ProcessAffinityMask, &affinity, sizeof(affinity));
3227c2c66affSColin Finck     ok(ret == STATUS_PROCESS_IS_TERMINATING, "expected STATUS_PROCESS_IS_TERMINATING, got %#x\n", ret);
3228c2c66affSColin Finck 
3229c2c66affSColin Finck     SetLastError(0xdeadbeef);
3230c2c66affSColin Finck     ctx.ContextFlags = CONTEXT_INTEGER;
3231c2c66affSColin Finck     ret = GetThreadContext(thread, &ctx);
3232c2c66affSColin Finck     ok(!ret || broken(ret) /* XP 64-bit */, "GetThreadContext should fail\n");
3233c2c66affSColin Finck     if (!ret)
3234c2c66affSColin Finck         ok(GetLastError() == ERROR_INVALID_PARAMETER ||
3235c2c66affSColin Finck            GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
3236c2c66affSColin Finck            GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
3237c2c66affSColin Finck            "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
3238c2c66affSColin Finck     SetLastError(0xdeadbeef);
3239c2c66affSColin Finck     ctx.ContextFlags = CONTEXT_INTEGER;
3240c2c66affSColin Finck     ret = SetThreadContext(thread, &ctx);
3241c2c66affSColin Finck     ok(!ret || broken(ret) /* XP 64-bit */, "SetThreadContext should fail\n");
3242c2c66affSColin Finck     if (!ret)
3243c2c66affSColin Finck         ok(GetLastError() == ERROR_ACCESS_DENIED ||
3244c2c66affSColin Finck            GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
3245c2c66affSColin Finck            GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
3246c2c66affSColin Finck            "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3247c2c66affSColin Finck     SetLastError(0xdeadbeef);
3248c2c66affSColin Finck     ret = SetThreadPriority(thread, 0);
3249c2c66affSColin Finck     ok(ret, "SetThreadPriority error %d\n", GetLastError());
3250c2c66affSColin Finck     CloseHandle(thread);
3251c2c66affSColin Finck 
3252c2c66affSColin Finck     SetLastError(0xdeadbeef);
3253c2c66affSColin Finck     ctx.ContextFlags = CONTEXT_INTEGER;
3254c2c66affSColin Finck     ret = GetThreadContext(pi.hThread, &ctx);
3255c2c66affSColin Finck     ok(!ret || broken(ret) /* XP 64-bit */, "GetThreadContext should fail\n");
3256c2c66affSColin Finck     if (!ret)
3257c2c66affSColin Finck         ok(GetLastError() == ERROR_INVALID_PARAMETER ||
3258c2c66affSColin Finck            GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
3259c2c66affSColin Finck            GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
3260c2c66affSColin Finck            "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
3261c2c66affSColin Finck     SetLastError(0xdeadbeef);
3262c2c66affSColin Finck     ctx.ContextFlags = CONTEXT_INTEGER;
3263c2c66affSColin Finck     ret = SetThreadContext(pi.hThread, &ctx);
3264c2c66affSColin Finck     ok(!ret || broken(ret) /* XP 64-bit */, "SetThreadContext should fail\n");
3265c2c66affSColin Finck     if (!ret)
3266c2c66affSColin Finck         ok(GetLastError() == ERROR_ACCESS_DENIED ||
3267c2c66affSColin Finck            GetLastError() == ERROR_GEN_FAILURE /* win7 64-bit */ ||
3268c2c66affSColin Finck            GetLastError() == ERROR_INVALID_FUNCTION /* vista 64-bit */,
3269c2c66affSColin Finck            "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3270c2c66affSColin Finck     SetLastError(0xdeadbeef);
3271c2c66affSColin Finck     ret = VirtualProtectEx(pi.hProcess, addr, 4096, PAGE_READWRITE, &old_prot);
3272c2c66affSColin Finck     ok(!ret, "VirtualProtectEx should fail\n");
3273c2c66affSColin Finck     ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3274c2c66affSColin Finck     SetLastError(0xdeadbeef);
3275c2c66affSColin Finck     size = 0;
3276c2c66affSColin Finck     ret = ReadProcessMemory(pi.hProcess, addr, buf, 4, &size);
3277c2c66affSColin Finck     ok(!ret, "ReadProcessMemory should fail\n");
3278c2c66affSColin Finck     ok(GetLastError() == ERROR_PARTIAL_COPY || GetLastError() == ERROR_ACCESS_DENIED,
3279c2c66affSColin Finck        "expected ERROR_PARTIAL_COPY, got %d\n", GetLastError());
3280c2c66affSColin Finck     ok(!size, "expected 0, got %lu\n", size);
3281c2c66affSColin Finck     SetLastError(0xdeadbeef);
3282c2c66affSColin Finck     ret = VirtualFreeEx(pi.hProcess, addr, 0, MEM_RELEASE);
3283c2c66affSColin Finck     ok(!ret, "VirtualFreeEx should fail\n");
3284c2c66affSColin Finck     ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3285c2c66affSColin Finck     SetLastError(0xdeadbeef);
3286c2c66affSColin Finck     addr = VirtualAllocEx(pi.hProcess, NULL, 4096, MEM_COMMIT, PAGE_READWRITE);
3287c2c66affSColin Finck     ok(!addr, "VirtualAllocEx should fail\n");
3288c2c66affSColin Finck     ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3289c2c66affSColin Finck     SetLastError(0xdeadbeef);
3290c2c66affSColin Finck     size = VirtualQueryEx(pi.hProcess, NULL, &mbi, sizeof(mbi));
3291c2c66affSColin Finck     ok(!size, "VirtualQueryEx should fail\n");
3292c2c66affSColin Finck     ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3293c2c66affSColin Finck 
3294c2c66affSColin Finck     /* CloseHandle() call below leads to premature process termination
3295c2c66affSColin Finck      * under some Windows versions.
3296c2c66affSColin Finck      */
3297c2c66affSColin Finck if (0)
3298c2c66affSColin Finck {
3299c2c66affSColin Finck     SetLastError(0xdeadbeef);
3300c2c66affSColin Finck     ret = CloseHandle(hmap_dup);
3301c2c66affSColin Finck     ok(ret, "CloseHandle should not fail\n");
3302c2c66affSColin Finck }
3303c2c66affSColin Finck 
3304c2c66affSColin Finck     SetLastError(0xdeadbeef);
3305c2c66affSColin Finck     ret = DuplicateHandle(GetCurrentProcess(), hmap, pi.hProcess, &hmap_dup,
3306c2c66affSColin Finck                           0, FALSE, DUPLICATE_SAME_ACCESS);
3307c2c66affSColin Finck     ok(!ret, "DuplicateHandle should fail\n");
3308c2c66affSColin Finck     ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3309c2c66affSColin Finck 
3310c2c66affSColin Finck     offset.u.LowPart = 0;
3311c2c66affSColin Finck     offset.u.HighPart = 0;
3312c2c66affSColin Finck     addr = NULL;
3313c2c66affSColin Finck     size = 0;
3314c2c66affSColin Finck     ret = pNtMapViewOfSection(hmap, pi.hProcess, &addr, 0, 0, &offset,
3315c2c66affSColin Finck                               &size, 1 /* ViewShare */, 0, PAGE_READONLY);
3316c2c66affSColin Finck     ok(ret == STATUS_PROCESS_IS_TERMINATING, "expected STATUS_PROCESS_IS_TERMINATING, got %#x\n", ret);
3317c2c66affSColin Finck 
3318c2c66affSColin Finck     SetLastError(0xdeadbeef);
3319c2c66affSColin Finck     thread = CreateRemoteThread(pi.hProcess, NULL, 0, (void *)0xdeadbeef, NULL, CREATE_SUSPENDED, &ret);
3320c2c66affSColin Finck     ok(!thread, "CreateRemoteThread should fail\n");
3321c2c66affSColin Finck     ok(GetLastError() == ERROR_ACCESS_DENIED, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3322c2c66affSColin Finck 
3323c2c66affSColin Finck     SetLastError(0xdeadbeef);
3324c2c66affSColin Finck     ret = DebugActiveProcess(pi.dwProcessId);
3325c2c66affSColin Finck     ok(!ret, "DebugActiveProcess should fail\n");
3326c2c66affSColin Finck     ok(GetLastError() == ERROR_ACCESS_DENIED /* 64-bit */ || GetLastError() == ERROR_NOT_SUPPORTED /* 32-bit */,
3327c2c66affSColin Finck       "ERROR_ACCESS_DENIED, got %d\n", GetLastError());
3328c2c66affSColin Finck 
3329c2c66affSColin Finck     GetExitCodeProcess(pi.hProcess, &ret);
3330c2c66affSColin Finck     ok(ret == 198 || broken(ret != 198) /* some 32-bit XP version in a VM returns random exit code */,
3331c2c66affSColin Finck        "expected exit code 198, got %u\n", ret);
3332c2c66affSColin Finck     CloseHandle(pi.hThread);
3333c2c66affSColin Finck     CloseHandle(pi.hProcess);
3334c2c66affSColin Finck 
3335c2c66affSColin Finck     ret = DeleteFileA(dll_name);
3336c2c66affSColin Finck     ok(ret, "DeleteFile error %d\n", GetLastError());
3337c2c66affSColin Finck }
3338c2c66affSColin Finck 
failuredllhook(ULONG ul,DELAYLOAD_INFO * pd)3339c2c66affSColin Finck static PVOID WINAPI failuredllhook(ULONG ul, DELAYLOAD_INFO* pd)
3340c2c66affSColin Finck {
3341c2c66affSColin Finck     ok(ul == 4, "expected 4, got %u\n", ul);
3342c2c66affSColin Finck     ok(!!pd, "no delayload info supplied\n");
3343c2c66affSColin Finck     if (pd)
3344c2c66affSColin Finck     {
3345c2c66affSColin Finck         ok(pd->Size == sizeof(*pd), "got %u\n", pd->Size);
3346c2c66affSColin Finck         ok(!!pd->DelayloadDescriptor, "no DelayloadDescriptor supplied\n");
3347c2c66affSColin Finck         if (pd->DelayloadDescriptor)
3348c2c66affSColin Finck         {
3349c2c66affSColin Finck             ok(pd->DelayloadDescriptor->Attributes.AllAttributes == 1,
3350c2c66affSColin Finck                "expected 1, got %u\n", pd->DelayloadDescriptor->Attributes.AllAttributes);
3351c2c66affSColin Finck             ok(pd->DelayloadDescriptor->DllNameRVA == 0x2000,
3352c2c66affSColin Finck                "expected 0x2000, got %x\n", pd->DelayloadDescriptor->DllNameRVA);
3353c2c66affSColin Finck             ok(pd->DelayloadDescriptor->ModuleHandleRVA == 0x201a,
3354c2c66affSColin Finck                "expected 0x201a, got %x\n", pd->DelayloadDescriptor->ModuleHandleRVA);
3355c2c66affSColin Finck             ok(pd->DelayloadDescriptor->ImportAddressTableRVA > pd->DelayloadDescriptor->ModuleHandleRVA,
3356c2c66affSColin Finck                "expected %x > %x\n", pd->DelayloadDescriptor->ImportAddressTableRVA,
3357c2c66affSColin Finck                pd->DelayloadDescriptor->ModuleHandleRVA);
3358c2c66affSColin Finck             ok(pd->DelayloadDescriptor->ImportNameTableRVA > pd->DelayloadDescriptor->ImportAddressTableRVA,
3359c2c66affSColin Finck                "expected %x > %x\n", pd->DelayloadDescriptor->ImportNameTableRVA,
3360c2c66affSColin Finck                pd->DelayloadDescriptor->ImportAddressTableRVA);
3361c2c66affSColin Finck             ok(pd->DelayloadDescriptor->BoundImportAddressTableRVA == 0,
3362c2c66affSColin Finck                "expected 0, got %x\n", pd->DelayloadDescriptor->BoundImportAddressTableRVA);
3363c2c66affSColin Finck             ok(pd->DelayloadDescriptor->UnloadInformationTableRVA == 0,
3364c2c66affSColin Finck                "expected 0, got %x\n", pd->DelayloadDescriptor->UnloadInformationTableRVA);
3365c2c66affSColin Finck             ok(pd->DelayloadDescriptor->TimeDateStamp == 0,
3366c2c66affSColin Finck                "expected 0, got %x\n", pd->DelayloadDescriptor->TimeDateStamp);
3367c2c66affSColin Finck         }
3368c2c66affSColin Finck 
3369c2c66affSColin Finck         ok(!!pd->ThunkAddress, "no ThunkAddress supplied\n");
3370c2c66affSColin Finck         if (pd->ThunkAddress)
3371c2c66affSColin Finck             ok(pd->ThunkAddress->u1.Ordinal, "no ThunkAddress value supplied\n");
3372c2c66affSColin Finck 
3373c2c66affSColin Finck         ok(!!pd->TargetDllName, "no TargetDllName supplied\n");
3374c2c66affSColin Finck         if (pd->TargetDllName)
3375c2c66affSColin Finck             ok(!strcmp(pd->TargetDllName, "secur32.dll"),
3376c2c66affSColin Finck                "expected \"secur32.dll\", got \"%s\"\n", pd->TargetDllName);
3377c2c66affSColin Finck 
3378c2c66affSColin Finck         ok(pd->TargetApiDescriptor.ImportDescribedByName == 0,
3379c2c66affSColin Finck            "expected 0, got %x\n", pd->TargetApiDescriptor.ImportDescribedByName);
3380c2c66affSColin Finck         ok(pd->TargetApiDescriptor.Description.Ordinal == 0 ||
3381c2c66affSColin Finck            pd->TargetApiDescriptor.Description.Ordinal == 999,
3382c2c66affSColin Finck            "expected 0, got %x\n", pd->TargetApiDescriptor.Description.Ordinal);
3383c2c66affSColin Finck 
3384c2c66affSColin Finck         ok(!!pd->TargetModuleBase, "no TargetModuleBase supplied\n");
3385c2c66affSColin Finck         ok(pd->Unused == NULL, "expected NULL, got %p\n", pd->Unused);
3386c2c66affSColin Finck         ok(pd->LastError, "no LastError supplied\n");
3387c2c66affSColin Finck     }
3388c2c66affSColin Finck     cb_count++;
3389c2c66affSColin Finck     return (void*)0xdeadbeef;
3390c2c66affSColin Finck }
3391c2c66affSColin Finck 
test_ResolveDelayLoadedAPI(void)3392c2c66affSColin Finck static void test_ResolveDelayLoadedAPI(void)
3393c2c66affSColin Finck {
3394c2c66affSColin Finck     static const char test_dll[] = "secur32.dll";
3395c2c66affSColin Finck     static const char test_func[] = "SealMessage";
3396c2c66affSColin Finck     char temp_path[MAX_PATH];
3397c2c66affSColin Finck     char dll_name[MAX_PATH];
3398c2c66affSColin Finck     IMAGE_DELAYLOAD_DESCRIPTOR idd, *delaydir;
3399c2c66affSColin Finck     IMAGE_THUNK_DATA itd32;
3400c2c66affSColin Finck     HANDLE hfile;
3401c2c66affSColin Finck     HMODULE hlib;
3402c2c66affSColin Finck     DWORD dummy, file_size, i;
3403c2c66affSColin Finck     WORD hint = 0;
3404c2c66affSColin Finck     BOOL ret;
3405c2c66affSColin Finck     IMAGE_NT_HEADERS nt_header;
3406c2c66affSColin Finck 
3407c2c66affSColin Finck     static const struct test_data
3408c2c66affSColin Finck     {
3409c2c66affSColin Finck         BOOL func;
3410c2c66affSColin Finck         UINT_PTR ordinal;
3411c2c66affSColin Finck         BOOL succeeds;
3412c2c66affSColin Finck     } td[] =
3413c2c66affSColin Finck     {
3414c2c66affSColin Finck         {
3415c2c66affSColin Finck             TRUE, 0, TRUE
3416c2c66affSColin Finck         },
3417c2c66affSColin Finck         {
3418c2c66affSColin Finck             FALSE, IMAGE_ORDINAL_FLAG | 2, TRUE
3419c2c66affSColin Finck         },
3420c2c66affSColin Finck         {
3421c2c66affSColin Finck             FALSE, IMAGE_ORDINAL_FLAG | 5, TRUE
3422c2c66affSColin Finck         },
3423c2c66affSColin Finck         {
3424c2c66affSColin Finck             FALSE, IMAGE_ORDINAL_FLAG | 0, FALSE
3425c2c66affSColin Finck         },
3426c2c66affSColin Finck         {
3427c2c66affSColin Finck             FALSE, IMAGE_ORDINAL_FLAG | 999, FALSE
3428c2c66affSColin Finck         },
3429c2c66affSColin Finck     };
3430c2c66affSColin Finck 
3431c2c66affSColin Finck     if (!pResolveDelayLoadedAPI)
3432c2c66affSColin Finck     {
3433c2c66affSColin Finck         win_skip("ResolveDelayLoadedAPI is not available\n");
3434c2c66affSColin Finck         return;
3435c2c66affSColin Finck     }
3436c2c66affSColin Finck 
3437c2c66affSColin Finck     if (0) /* crashes on native */
3438c2c66affSColin Finck     {
3439c2c66affSColin Finck         SetLastError(0xdeadbeef);
3440c2c66affSColin Finck         ok(!pResolveDelayLoadedAPI(NULL, NULL, NULL, NULL, NULL, 0),
3441c2c66affSColin Finck            "ResolveDelayLoadedAPI succeeded\n");
3442c2c66affSColin Finck         ok(GetLastError() == 0xdeadbeef, "GetLastError changed to %x\n", GetLastError());
3443c2c66affSColin Finck 
3444c2c66affSColin Finck         cb_count = 0;
3445c2c66affSColin Finck         SetLastError(0xdeadbeef);
3446c2c66affSColin Finck         ok(!pResolveDelayLoadedAPI(NULL, NULL, failuredllhook, NULL, NULL, 0),
3447c2c66affSColin Finck            "ResolveDelayLoadedAPI succeeded\n");
3448c2c66affSColin Finck         ok(GetLastError() == 0xdeadbeef, "GetLastError changed to %x\n", GetLastError());
3449c2c66affSColin Finck         ok(cb_count == 1, "Wrong callback count: %d\n", cb_count);
3450c2c66affSColin Finck     }
3451c2c66affSColin Finck 
3452c2c66affSColin Finck     GetTempPathA(MAX_PATH, temp_path);
3453c2c66affSColin Finck     GetTempFileNameA(temp_path, "ldr", 0, dll_name);
3454c2c66affSColin Finck     trace("creating %s\n", dll_name);
3455c2c66affSColin Finck     hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
3456c2c66affSColin Finck     if (hfile == INVALID_HANDLE_VALUE)
3457c2c66affSColin Finck     {
3458c2c66affSColin Finck         ok(0, "could not create %s\n", dll_name);
3459c2c66affSColin Finck         return;
3460c2c66affSColin Finck     }
3461c2c66affSColin Finck 
3462c2c66affSColin Finck     SetLastError(0xdeadbeef);
3463c2c66affSColin Finck     ret = WriteFile(hfile, &dos_header, sizeof(dos_header), &dummy, NULL);
3464c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3465c2c66affSColin Finck 
3466c2c66affSColin Finck     nt_header = nt_header_template;
3467c2c66affSColin Finck     nt_header.FileHeader.NumberOfSections = 2;
3468c2c66affSColin Finck     nt_header.FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
3469c2c66affSColin Finck 
3470c2c66affSColin Finck     nt_header.OptionalHeader.SectionAlignment = 0x1000;
3471c2c66affSColin Finck     nt_header.OptionalHeader.FileAlignment = 0x1000;
3472c2c66affSColin Finck     nt_header.OptionalHeader.SizeOfImage = sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x2200;
3473c2c66affSColin Finck     nt_header.OptionalHeader.SizeOfHeaders = sizeof(dos_header) + sizeof(nt_header) + 2 * sizeof(IMAGE_SECTION_HEADER);
3474c2c66affSColin Finck     nt_header.OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
3475c2c66affSColin Finck     nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress = 0x1000;
3476c2c66affSColin Finck     nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size = 0x100;
3477c2c66affSColin Finck 
3478c2c66affSColin Finck     SetLastError(0xdeadbeef);
3479c2c66affSColin Finck     ret = WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL);
3480c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3481c2c66affSColin Finck 
3482c2c66affSColin Finck     SetLastError(0xdeadbeef);
3483c2c66affSColin Finck     ret = WriteFile(hfile, &nt_header.OptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER), &dummy, NULL);
3484c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3485c2c66affSColin Finck 
3486c2c66affSColin Finck     /* sections */
3487c2c66affSColin Finck     section.PointerToRawData = nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress;
3488c2c66affSColin Finck     section.VirtualAddress = nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress;
3489c2c66affSColin Finck     section.Misc.VirtualSize = 2 * sizeof(idd);
3490c2c66affSColin Finck     section.SizeOfRawData = section.Misc.VirtualSize;
3491c2c66affSColin Finck     section.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;
3492c2c66affSColin Finck     SetLastError(0xdeadbeef);
3493c2c66affSColin Finck     ret = WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
3494c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3495c2c66affSColin Finck 
3496c2c66affSColin Finck     section.PointerToRawData = 0x2000;
3497c2c66affSColin Finck     section.VirtualAddress = 0x2000;
3498c2c66affSColin Finck     i = sizeof(td)/sizeof(td[0]);
3499c2c66affSColin Finck     section.Misc.VirtualSize = sizeof(test_dll) + sizeof(hint) + sizeof(test_func) + sizeof(HMODULE) +
3500c2c66affSColin Finck                                2 * (i + 1) * sizeof(IMAGE_THUNK_DATA);
3501c2c66affSColin Finck     ok(section.Misc.VirtualSize <= 0x1000, "Too much tests, add a new section!\n");
3502c2c66affSColin Finck     section.SizeOfRawData = section.Misc.VirtualSize;
3503c2c66affSColin Finck     section.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
3504c2c66affSColin Finck     SetLastError(0xdeadbeef);
3505c2c66affSColin Finck     ret = WriteFile(hfile, &section, sizeof(section), &dummy, NULL);
3506c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3507c2c66affSColin Finck 
3508c2c66affSColin Finck     /* fill up to delay data */
3509c2c66affSColin Finck     SetFilePointer( hfile, nt_header.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress, NULL, SEEK_SET );
3510c2c66affSColin Finck 
3511c2c66affSColin Finck     /* delay data */
3512c2c66affSColin Finck     idd.Attributes.AllAttributes = 1;
3513c2c66affSColin Finck     idd.DllNameRVA = 0x2000;
3514c2c66affSColin Finck     idd.ModuleHandleRVA = idd.DllNameRVA + sizeof(test_dll) + sizeof(hint) + sizeof(test_func);
3515c2c66affSColin Finck     idd.ImportAddressTableRVA = idd.ModuleHandleRVA + sizeof(HMODULE);
3516c2c66affSColin Finck     idd.ImportNameTableRVA = idd.ImportAddressTableRVA + (i + 1) * sizeof(IMAGE_THUNK_DATA);
3517c2c66affSColin Finck     idd.BoundImportAddressTableRVA = 0;
3518c2c66affSColin Finck     idd.UnloadInformationTableRVA = 0;
3519c2c66affSColin Finck     idd.TimeDateStamp = 0;
3520c2c66affSColin Finck 
3521c2c66affSColin Finck     SetLastError(0xdeadbeef);
3522c2c66affSColin Finck     ret = WriteFile(hfile, &idd, sizeof(idd), &dummy, NULL);
3523c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3524c2c66affSColin Finck 
3525c2c66affSColin Finck     SetLastError(0xdeadbeef);
3526c2c66affSColin Finck     ret = WriteFile(hfile, filler, sizeof(idd), &dummy, NULL);
3527c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3528c2c66affSColin Finck 
3529c2c66affSColin Finck     /* fill up to extended delay data */
3530c2c66affSColin Finck     SetFilePointer( hfile, idd.DllNameRVA, NULL, SEEK_SET );
3531c2c66affSColin Finck 
3532c2c66affSColin Finck     /* extended delay data */
3533c2c66affSColin Finck     SetLastError(0xdeadbeef);
3534c2c66affSColin Finck     ret = WriteFile(hfile, test_dll, sizeof(test_dll), &dummy, NULL);
3535c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3536c2c66affSColin Finck 
3537c2c66affSColin Finck     SetLastError(0xdeadbeef);
3538c2c66affSColin Finck     ret = WriteFile(hfile, &hint, sizeof(hint), &dummy, NULL);
3539c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3540c2c66affSColin Finck 
3541c2c66affSColin Finck     SetLastError(0xdeadbeef);
3542c2c66affSColin Finck     ret = WriteFile(hfile, test_func, sizeof(test_func), &dummy, NULL);
3543c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3544c2c66affSColin Finck 
3545c2c66affSColin Finck     SetFilePointer( hfile, idd.ImportAddressTableRVA, NULL, SEEK_SET );
3546c2c66affSColin Finck 
3547c2c66affSColin Finck     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3548c2c66affSColin Finck     {
3549c2c66affSColin Finck         /* 0x1a00 is an empty space between delay data and extended delay data, real thunks are not necessary */
3550c2c66affSColin Finck         itd32.u1.Function = nt_header.OptionalHeader.ImageBase + 0x1a00 + i * 0x20;
3551c2c66affSColin Finck         SetLastError(0xdeadbeef);
3552c2c66affSColin Finck         ret = WriteFile(hfile, &itd32, sizeof(itd32), &dummy, NULL);
3553c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
3554c2c66affSColin Finck     }
3555c2c66affSColin Finck 
3556c2c66affSColin Finck     itd32.u1.Function = 0;
3557c2c66affSColin Finck     SetLastError(0xdeadbeef);
3558c2c66affSColin Finck     ret = WriteFile(hfile, &itd32, sizeof(itd32), &dummy, NULL);
3559c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3560c2c66affSColin Finck 
3561c2c66affSColin Finck     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3562c2c66affSColin Finck     {
3563c2c66affSColin Finck         if (td[i].func)
3564c2c66affSColin Finck             itd32.u1.AddressOfData = idd.DllNameRVA + sizeof(test_dll);
3565c2c66affSColin Finck         else
3566c2c66affSColin Finck             itd32.u1.Ordinal = td[i].ordinal;
3567c2c66affSColin Finck         SetLastError(0xdeadbeef);
3568c2c66affSColin Finck         ret = WriteFile(hfile, &itd32, sizeof(itd32), &dummy, NULL);
3569c2c66affSColin Finck         ok(ret, "WriteFile error %d\n", GetLastError());
3570c2c66affSColin Finck     }
3571c2c66affSColin Finck 
3572c2c66affSColin Finck     itd32.u1.Ordinal = 0;
3573c2c66affSColin Finck     SetLastError(0xdeadbeef);
3574c2c66affSColin Finck     ret = WriteFile(hfile, &itd32, sizeof(itd32), &dummy, NULL);
3575c2c66affSColin Finck     ok(ret, "WriteFile error %d\n", GetLastError());
3576c2c66affSColin Finck 
3577c2c66affSColin Finck     /* fill up to eof */
3578c2c66affSColin Finck     SetFilePointer( hfile, section.VirtualAddress + section.Misc.VirtualSize, NULL, SEEK_SET );
3579c2c66affSColin Finck     SetEndOfFile( hfile );
3580c2c66affSColin Finck     CloseHandle(hfile);
3581c2c66affSColin Finck 
3582c2c66affSColin Finck     SetLastError(0xdeadbeef);
3583c2c66affSColin Finck     hlib = LoadLibraryA(dll_name);
3584c2c66affSColin Finck     ok(hlib != NULL, "LoadLibrary error %u\n", GetLastError());
3585c2c66affSColin Finck     if (!hlib)
3586c2c66affSColin Finck     {
3587c2c66affSColin Finck         skip("couldn't load %s.\n", dll_name);
3588c2c66affSColin Finck         DeleteFileA(dll_name);
3589c2c66affSColin Finck         return;
3590c2c66affSColin Finck     }
3591c2c66affSColin Finck 
3592c2c66affSColin Finck     delaydir = pRtlImageDirectoryEntryToData(hlib, TRUE, IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &file_size);
3593c2c66affSColin Finck     if (!delaydir)
3594c2c66affSColin Finck     {
3595c2c66affSColin Finck         skip("haven't found section for delay import directory.\n");
3596c2c66affSColin Finck         FreeLibrary(hlib);
3597c2c66affSColin Finck         DeleteFileA(dll_name);
3598c2c66affSColin Finck         return;
3599c2c66affSColin Finck     }
3600c2c66affSColin Finck 
3601c2c66affSColin Finck     for (;;)
3602c2c66affSColin Finck     {
3603c2c66affSColin Finck         IMAGE_THUNK_DATA *itdn, *itda;
3604c2c66affSColin Finck         HMODULE htarget;
3605c2c66affSColin Finck 
3606c2c66affSColin Finck         if (!delaydir->DllNameRVA ||
3607c2c66affSColin Finck             !delaydir->ImportAddressTableRVA ||
3608c2c66affSColin Finck             !delaydir->ImportNameTableRVA) break;
3609c2c66affSColin Finck 
3610c2c66affSColin Finck         itdn = RVAToAddr(delaydir->ImportNameTableRVA, hlib);
3611c2c66affSColin Finck         itda = RVAToAddr(delaydir->ImportAddressTableRVA, hlib);
3612c2c66affSColin Finck         htarget = LoadLibraryA(RVAToAddr(delaydir->DllNameRVA, hlib));
3613c2c66affSColin Finck 
3614c2c66affSColin Finck         for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
3615c2c66affSColin Finck         {
3616c2c66affSColin Finck             void *ret, *load;
3617c2c66affSColin Finck 
3618c2c66affSColin Finck             if (IMAGE_SNAP_BY_ORDINAL(itdn[i].u1.Ordinal))
3619c2c66affSColin Finck                 load = (void *)GetProcAddress(htarget, (LPSTR)IMAGE_ORDINAL(itdn[i].u1.Ordinal));
3620c2c66affSColin Finck             else
3621c2c66affSColin Finck             {
3622c2c66affSColin Finck                 const IMAGE_IMPORT_BY_NAME* iibn = RVAToAddr(itdn[i].u1.AddressOfData, hlib);
3623c2c66affSColin Finck                 load = (void *)GetProcAddress(htarget, (char*)iibn->Name);
3624c2c66affSColin Finck             }
3625c2c66affSColin Finck 
3626c2c66affSColin Finck             cb_count = 0;
3627c2c66affSColin Finck             ret = pResolveDelayLoadedAPI(hlib, delaydir, failuredllhook, NULL, &itda[i], 0);
3628c2c66affSColin Finck             if (td[i].succeeds)
3629c2c66affSColin Finck             {
3630c2c66affSColin Finck                 ok(ret != NULL, "Test %u: ResolveDelayLoadedAPI failed\n", i);
3631c2c66affSColin Finck                 ok(ret == load, "Test %u: expected %p, got %p\n", i, load, ret);
3632c2c66affSColin Finck                 ok(ret == (void*)itda[i].u1.AddressOfData, "Test %u: expected %p, got %p\n",
3633c2c66affSColin Finck                    i, ret, (void*)itda[i].u1.AddressOfData);
3634c2c66affSColin Finck                 ok(!cb_count, "Test %u: Wrong callback count: %d\n", i, cb_count);
3635c2c66affSColin Finck             }
3636c2c66affSColin Finck             else
3637c2c66affSColin Finck             {
3638c2c66affSColin Finck                 ok(ret == (void*)0xdeadbeef, "Test %u: ResolveDelayLoadedAPI succeeded with %p\n", i, ret);
3639c2c66affSColin Finck                 ok(cb_count, "Test %u: Wrong callback count: %d\n", i, cb_count);
3640c2c66affSColin Finck             }
3641c2c66affSColin Finck         }
3642c2c66affSColin Finck         delaydir++;
3643c2c66affSColin Finck     }
3644c2c66affSColin Finck 
3645c2c66affSColin Finck     FreeLibrary(hlib);
3646c2c66affSColin Finck     trace("deleting %s\n", dll_name);
3647c2c66affSColin Finck     DeleteFileA(dll_name);
3648c2c66affSColin Finck }
3649c2c66affSColin Finck 
test_InMemoryOrderModuleList(void)3650c2c66affSColin Finck static void test_InMemoryOrderModuleList(void)
3651c2c66affSColin Finck {
3652c2c66affSColin Finck     PEB_LDR_DATA *ldr = NtCurrentTeb()->Peb->LdrData;
3653c2c66affSColin Finck     LIST_ENTRY *entry1, *mark1 = &ldr->InLoadOrderModuleList;
3654c2c66affSColin Finck     LIST_ENTRY *entry2, *mark2 = &ldr->InMemoryOrderModuleList;
3655c2c66affSColin Finck     LDR_MODULE *module1, *module2;
3656c2c66affSColin Finck 
3657c2c66affSColin Finck     ok(ldr->Initialized == TRUE, "expected TRUE, got %u\n", ldr->Initialized);
3658c2c66affSColin Finck 
3659c2c66affSColin Finck     for (entry1 = mark1->Flink, entry2 = mark2->Flink;
3660c2c66affSColin Finck          entry1 != mark1 && entry2 != mark2;
3661c2c66affSColin Finck          entry1 = entry1->Flink, entry2 = entry2->Flink)
3662c2c66affSColin Finck     {
3663c2c66affSColin Finck         module1 = CONTAINING_RECORD(entry1, LDR_MODULE, InLoadOrderModuleList);
3664c2c66affSColin Finck         module2 = CONTAINING_RECORD(entry2, LDR_MODULE, InMemoryOrderModuleList);
3665c2c66affSColin Finck         ok(module1 == module2, "expected module1 == module2, got %p and %p\n", module1, module2);
3666c2c66affSColin Finck     }
3667c2c66affSColin Finck     ok(entry1 == mark1, "expected entry1 == mark1, got %p and %p\n", entry1, mark1);
3668c2c66affSColin Finck     ok(entry2 == mark2, "expected entry2 == mark2, got %p and %p\n", entry2, mark2);
3669c2c66affSColin Finck }
3670c2c66affSColin Finck 
toupperW(WCHAR c)3671c2c66affSColin Finck static inline WCHAR toupperW(WCHAR c)
3672c2c66affSColin Finck {
3673c2c66affSColin Finck     WCHAR tmp = c;
3674c2c66affSColin Finck     CharUpperBuffW(&tmp, 1);
3675c2c66affSColin Finck     return tmp;
3676c2c66affSColin Finck }
3677c2c66affSColin Finck 
hash_basename(const WCHAR * basename)3678c2c66affSColin Finck static ULONG hash_basename(const WCHAR *basename)
3679c2c66affSColin Finck {
3680c2c66affSColin Finck     WORD version = MAKEWORD(NtCurrentTeb()->Peb->OSMinorVersion,
3681c2c66affSColin Finck                             NtCurrentTeb()->Peb->OSMajorVersion);
3682c2c66affSColin Finck     ULONG hash = 0;
3683c2c66affSColin Finck 
3684c2c66affSColin Finck     if (version >= 0x0602)
3685c2c66affSColin Finck     {
3686c2c66affSColin Finck         for (; *basename; basename++)
3687c2c66affSColin Finck             hash = hash * 65599 + toupperW(*basename);
3688c2c66affSColin Finck     }
3689c2c66affSColin Finck     else if (version == 0x0601)
3690c2c66affSColin Finck     {
3691c2c66affSColin Finck         for (; *basename; basename++)
3692c2c66affSColin Finck             hash = hash + 65599 * toupperW(*basename);
3693c2c66affSColin Finck     }
3694c2c66affSColin Finck     else
3695c2c66affSColin Finck         hash = toupperW(basename[0]) - 'A';
3696c2c66affSColin Finck 
3697c2c66affSColin Finck     return hash & 31;
3698c2c66affSColin Finck }
3699c2c66affSColin Finck 
test_HashLinks(void)3700c2c66affSColin Finck static void test_HashLinks(void)
3701c2c66affSColin Finck {
3702c2c66affSColin Finck     static WCHAR ntdllW[] = {'n','t','d','l','l','.','d','l','l',0};
3703c2c66affSColin Finck     static WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
3704c2c66affSColin Finck 
3705c2c66affSColin Finck     LIST_ENTRY *hash_map, *entry, *mark;
3706c2c66affSColin Finck     LDR_MODULE *module;
3707c2c66affSColin Finck     BOOL found;
3708c2c66affSColin Finck 
3709c2c66affSColin Finck     entry = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
3710c2c66affSColin Finck     entry = entry->Flink;
3711c2c66affSColin Finck 
3712c2c66affSColin Finck     module = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
3713c2c66affSColin Finck     entry = module->HashLinks.Blink;
3714c2c66affSColin Finck 
3715c2c66affSColin Finck     hash_map = entry - hash_basename(module->BaseDllName.Buffer);
3716c2c66affSColin Finck 
3717c2c66affSColin Finck     mark = &hash_map[hash_basename(ntdllW)];
3718c2c66affSColin Finck     found = FALSE;
3719c2c66affSColin Finck     for (entry = mark->Flink; entry != mark; entry = entry->Flink)
3720c2c66affSColin Finck     {
3721c2c66affSColin Finck         module = CONTAINING_RECORD(entry, LDR_MODULE, HashLinks);
3722c2c66affSColin Finck         if (!lstrcmpiW(module->BaseDllName.Buffer, ntdllW))
3723c2c66affSColin Finck         {
3724c2c66affSColin Finck             found = TRUE;
3725c2c66affSColin Finck             break;
3726c2c66affSColin Finck         }
3727c2c66affSColin Finck     }
3728c2c66affSColin Finck     ok(found, "Could not find ntdll\n");
3729c2c66affSColin Finck 
3730c2c66affSColin Finck     mark = &hash_map[hash_basename(kernel32W)];
3731c2c66affSColin Finck     found = FALSE;
3732c2c66affSColin Finck     for (entry = mark->Flink; entry != mark; entry = entry->Flink)
3733c2c66affSColin Finck     {
3734c2c66affSColin Finck         module = CONTAINING_RECORD(entry, LDR_MODULE, HashLinks);
3735c2c66affSColin Finck         if (!lstrcmpiW(module->BaseDllName.Buffer, kernel32W))
3736c2c66affSColin Finck         {
3737c2c66affSColin Finck             found = TRUE;
3738c2c66affSColin Finck             break;
3739c2c66affSColin Finck         }
3740c2c66affSColin Finck     }
3741c2c66affSColin Finck     ok(found, "Could not find kernel32\n");
3742c2c66affSColin Finck }
3743c2c66affSColin Finck 
START_TEST(loader)3744c2c66affSColin Finck START_TEST(loader)
3745c2c66affSColin Finck {
3746c2c66affSColin Finck     int argc;
3747c2c66affSColin Finck     char **argv;
3748c2c66affSColin Finck     HANDLE ntdll, mapping, kernel32;
3749c2c66affSColin Finck     SYSTEM_INFO si;
3750c2c66affSColin Finck 
3751c2c66affSColin Finck     ntdll = GetModuleHandleA("ntdll.dll");
3752c2c66affSColin Finck     kernel32 = GetModuleHandleA("kernel32.dll");
3753c2c66affSColin Finck     pNtCreateSection = (void *)GetProcAddress(ntdll, "NtCreateSection");
3754c2c66affSColin Finck     pNtQuerySection = (void *)GetProcAddress(ntdll, "NtQuerySection");
3755c2c66affSColin Finck     pNtMapViewOfSection = (void *)GetProcAddress(ntdll, "NtMapViewOfSection");
3756c2c66affSColin Finck     pNtUnmapViewOfSection = (void *)GetProcAddress(ntdll, "NtUnmapViewOfSection");
3757c2c66affSColin Finck     pNtTerminateProcess = (void *)GetProcAddress(ntdll, "NtTerminateProcess");
3758c2c66affSColin Finck     pNtQueryInformationProcess = (void *)GetProcAddress(ntdll, "NtQueryInformationProcess");
3759c2c66affSColin Finck     pNtSetInformationProcess = (void *)GetProcAddress(ntdll, "NtSetInformationProcess");
3760c2c66affSColin Finck     pLdrShutdownProcess = (void *)GetProcAddress(ntdll, "LdrShutdownProcess");
3761c2c66affSColin Finck     pRtlDllShutdownInProgress = (void *)GetProcAddress(ntdll, "RtlDllShutdownInProgress");
3762c2c66affSColin Finck     pNtAllocateVirtualMemory = (void *)GetProcAddress(ntdll, "NtAllocateVirtualMemory");
3763c2c66affSColin Finck     pNtFreeVirtualMemory = (void *)GetProcAddress(ntdll, "NtFreeVirtualMemory");
3764c2c66affSColin Finck     pLdrLockLoaderLock = (void *)GetProcAddress(ntdll, "LdrLockLoaderLock");
3765c2c66affSColin Finck     pLdrUnlockLoaderLock = (void *)GetProcAddress(ntdll, "LdrUnlockLoaderLock");
3766c2c66affSColin Finck     pRtlAcquirePebLock = (void *)GetProcAddress(ntdll, "RtlAcquirePebLock");
3767c2c66affSColin Finck     pRtlReleasePebLock = (void *)GetProcAddress(ntdll, "RtlReleasePebLock");
3768c2c66affSColin Finck     pRtlImageDirectoryEntryToData = (void *)GetProcAddress(ntdll, "RtlImageDirectoryEntryToData");
3769c2c66affSColin Finck     pFlsAlloc = (void *)GetProcAddress(kernel32, "FlsAlloc");
3770c2c66affSColin Finck     pFlsSetValue = (void *)GetProcAddress(kernel32, "FlsSetValue");
3771c2c66affSColin Finck     pFlsGetValue = (void *)GetProcAddress(kernel32, "FlsGetValue");
3772c2c66affSColin Finck     pFlsFree = (void *)GetProcAddress(kernel32, "FlsFree");
3773*ed41a4deSAmine Khaldi     pIsWow64Process = (void *)GetProcAddress(kernel32, "IsWow64Process");
3774c2c66affSColin Finck     pResolveDelayLoadedAPI = (void *)GetProcAddress(kernel32, "ResolveDelayLoadedAPI");
3775c2c66affSColin Finck 
3776*ed41a4deSAmine Khaldi     if (pIsWow64Process) pIsWow64Process( GetCurrentProcess(), &is_wow64 );
3777c2c66affSColin Finck     GetSystemInfo( &si );
3778c2c66affSColin Finck     page_size = si.dwPageSize;
3779c2c66affSColin Finck     dos_header.e_magic = IMAGE_DOS_SIGNATURE;
3780c2c66affSColin Finck     dos_header.e_lfanew = sizeof(dos_header);
3781c2c66affSColin Finck 
3782c2c66affSColin Finck     mapping = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "winetest_loader");
3783c2c66affSColin Finck     ok(mapping != 0, "CreateFileMapping failed\n");
3784c2c66affSColin Finck     child_failures = MapViewOfFile(mapping, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 4096);
3785c2c66affSColin Finck     if (*child_failures == -1)
3786c2c66affSColin Finck     {
3787c2c66affSColin Finck         *child_failures = 0;
3788c2c66affSColin Finck     }
3789c2c66affSColin Finck     else
3790c2c66affSColin Finck         *child_failures = -1;
3791c2c66affSColin Finck 
3792c2c66affSColin Finck     argc = winetest_get_mainargs(&argv);
3793c2c66affSColin Finck     if (argc > 4)
3794c2c66affSColin Finck     {
3795c2c66affSColin Finck         test_dll_phase = atoi(argv[4]);
3796c2c66affSColin Finck         child_process(argv[2], atol(argv[3]));
3797c2c66affSColin Finck         return;
3798c2c66affSColin Finck     }
3799c2c66affSColin Finck 
3800c2c66affSColin Finck     test_Loader();
3801c2c66affSColin Finck     test_FakeDLL();
3802*ed41a4deSAmine Khaldi     test_filenames();
3803c2c66affSColin Finck     test_ResolveDelayLoadedAPI();
3804c2c66affSColin Finck     test_ImportDescriptors();
3805c2c66affSColin Finck     test_section_access();
3806c2c66affSColin Finck     test_import_resolution();
3807c2c66affSColin Finck     test_ExitProcess();
3808c2c66affSColin Finck     test_InMemoryOrderModuleList();
3809c2c66affSColin Finck     test_HashLinks();
3810c2c66affSColin Finck }
3811