2 * Unit test suite for the PE loader.
4 * Copyright 2006,2011 Dmitry Timoshkov
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define NONAMELESSUNION
27 #define WIN32_NO_STATUS
30 #include "wine/winternl.h"
31 #include "wine/test.h"
32 #include "delayloadhandler.h"
34 /* PROCESS_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
35 #define PROCESS_ALL_ACCESS_NT4 (PROCESS_ALL_ACCESS & ~0xf000)
37 #define ALIGN_SIZE(size, alignment) (((size) + (alignment - 1)) & ~((alignment - 1)))
39 struct PROCESS_BASIC_INFORMATION_PRIVATE
43 DWORD_PTR AffinityMask
;
44 DWORD_PTR BasePriority
;
45 ULONG_PTR UniqueProcessId
;
46 ULONG_PTR InheritedFromUniqueProcessId
;
49 static LONG
*child_failures
;
51 static DWORD page_size
;
53 static NTSTATUS (WINAPI
*pNtCreateSection
)(HANDLE
*, ACCESS_MASK
, const OBJECT_ATTRIBUTES
*,
54 const LARGE_INTEGER
*, ULONG
, ULONG
, HANDLE
);
55 static NTSTATUS (WINAPI
*pNtQuerySection
)(HANDLE
, SECTION_INFORMATION_CLASS
, void *, ULONG
, ULONG
*);
56 static NTSTATUS (WINAPI
*pNtMapViewOfSection
)(HANDLE
, HANDLE
, PVOID
*, ULONG
, SIZE_T
, const LARGE_INTEGER
*, SIZE_T
*, ULONG
, ULONG
, ULONG
);
57 static NTSTATUS (WINAPI
*pNtUnmapViewOfSection
)(HANDLE
, PVOID
);
58 static NTSTATUS (WINAPI
*pNtQueryInformationProcess
)(HANDLE
, PROCESSINFOCLASS
, PVOID
, ULONG
, PULONG
);
59 static NTSTATUS (WINAPI
*pNtSetInformationProcess
)(HANDLE
, PROCESSINFOCLASS
, PVOID
, ULONG
);
60 static NTSTATUS (WINAPI
*pNtTerminateProcess
)(HANDLE
, DWORD
);
61 static void (WINAPI
*pLdrShutdownProcess
)(void);
62 static BOOLEAN (WINAPI
*pRtlDllShutdownInProgress
)(void);
63 static NTSTATUS (WINAPI
*pNtAllocateVirtualMemory
)(HANDLE
, PVOID
*, ULONG
, SIZE_T
*, ULONG
, ULONG
);
64 static NTSTATUS (WINAPI
*pNtFreeVirtualMemory
)(HANDLE
, PVOID
*, SIZE_T
*, ULONG
);
65 static NTSTATUS (WINAPI
*pLdrLockLoaderLock
)(ULONG
, ULONG
*, ULONG_PTR
*);
66 static NTSTATUS (WINAPI
*pLdrUnlockLoaderLock
)(ULONG
, ULONG_PTR
);
67 static void (WINAPI
*pRtlAcquirePebLock
)(void);
68 static void (WINAPI
*pRtlReleasePebLock
)(void);
69 static PVOID (WINAPI
*pResolveDelayLoadedAPI
)(PVOID
, PCIMAGE_DELAYLOAD_DESCRIPTOR
,
70 PDELAYLOAD_FAILURE_DLL_CALLBACK
, PVOID
,
71 PIMAGE_THUNK_DATA ThunkAddress
,ULONG
);
72 static PVOID (WINAPI
*pRtlImageDirectoryEntryToData
)(HMODULE
,BOOL
,WORD
,ULONG
*);
73 static DWORD (WINAPI
*pFlsAlloc
)(PFLS_CALLBACK_FUNCTION
);
74 static BOOL (WINAPI
*pFlsSetValue
)(DWORD
, PVOID
);
75 static PVOID (WINAPI
*pFlsGetValue
)(DWORD
);
76 static BOOL (WINAPI
*pFlsFree
)(DWORD
);
78 static PVOID
RVAToAddr(DWORD_PTR rva
, HMODULE module
)
82 return ((char*) module
) + rva
;
85 static IMAGE_DOS_HEADER dos_header
;
87 static const IMAGE_NT_HEADERS nt_header_template
=
89 IMAGE_NT_SIGNATURE
, /* Signature */
92 IMAGE_FILE_MACHINE_I386
, /* Machine */
93 #elif defined __x86_64__
94 IMAGE_FILE_MACHINE_AMD64
, /* Machine */
95 #elif defined __powerpc__
96 IMAGE_FILE_MACHINE_POWERPC
, /* Machine */
98 IMAGE_FILE_MACHINE_ARMNT
, /* Machine */
99 #elif defined __aarch64__
100 IMAGE_FILE_MACHINE_ARM64
, /* Machine */
102 # error You must specify the machine type
104 1, /* NumberOfSections */
105 0, /* TimeDateStamp */
106 0, /* PointerToSymbolTable */
107 0, /* NumberOfSymbols */
108 sizeof(IMAGE_OPTIONAL_HEADER
), /* SizeOfOptionalHeader */
109 IMAGE_FILE_EXECUTABLE_IMAGE
| IMAGE_FILE_DLL
/* Characteristics */
111 { IMAGE_NT_OPTIONAL_HDR_MAGIC
, /* Magic */
112 1, /* MajorLinkerVersion */
113 0, /* MinorLinkerVersion */
115 0, /* SizeOfInitializedData */
116 0, /* SizeOfUninitializedData */
117 0, /* AddressOfEntryPoint */
118 0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
122 0x10000000, /* ImageBase */
123 0, /* SectionAlignment */
124 0, /* FileAlignment */
125 4, /* MajorOperatingSystemVersion */
126 0, /* MinorOperatingSystemVersion */
127 1, /* MajorImageVersion */
128 0, /* MinorImageVersion */
129 4, /* MajorSubsystemVersion */
130 0, /* MinorSubsystemVersion */
131 0, /* Win32VersionValue */
132 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
) + 0x1000, /* SizeOfImage */
133 sizeof(dos_header
) + sizeof(nt_header_template
), /* SizeOfHeaders */
135 IMAGE_SUBSYSTEM_WINDOWS_CUI
, /* Subsystem */
136 0, /* DllCharacteristics */
137 0, /* SizeOfStackReserve */
138 0, /* SizeOfStackCommit */
139 0, /* SizeOfHeapReserve */
140 0, /* SizeOfHeapCommit */
142 0, /* NumberOfRvaAndSizes */
143 { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
147 static IMAGE_SECTION_HEADER section
=
149 ".rodata", /* Name */
151 0, /* VirtualAddress */
152 0, /* SizeOfRawData */
153 0, /* PointerToRawData */
154 0, /* PointerToRelocations */
155 0, /* PointerToLinenumbers */
156 0, /* NumberOfRelocations */
157 0, /* NumberOfLinenumbers */
158 IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
, /* Characteristics */
162 static const char filler
[0x1000];
163 static const char section_data
[0x10] = "section data";
165 static DWORD
create_test_dll( const IMAGE_DOS_HEADER
*dos_header
, UINT dos_size
,
166 const IMAGE_NT_HEADERS
*nt_header
, const char *dll_name
)
168 DWORD dummy
, size
, file_align
;
172 hfile
= CreateFileA(dll_name
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, 0, 0);
173 if (hfile
== INVALID_HANDLE_VALUE
) return 0;
175 SetLastError(0xdeadbeef);
176 ret
= WriteFile(hfile
, dos_header
, dos_size
, &dummy
, NULL
);
177 ok(ret
, "WriteFile error %d\n", GetLastError());
179 SetLastError(0xdeadbeef);
180 ret
= WriteFile(hfile
, nt_header
, sizeof(DWORD
) + sizeof(IMAGE_FILE_HEADER
), &dummy
, NULL
);
181 ok(ret
, "WriteFile error %d\n", GetLastError());
183 if (nt_header
->FileHeader
.SizeOfOptionalHeader
)
185 SetLastError(0xdeadbeef);
186 ret
= WriteFile(hfile
, &nt_header
->OptionalHeader
,
187 min(nt_header
->FileHeader
.SizeOfOptionalHeader
, sizeof(IMAGE_OPTIONAL_HEADER
)),
189 ok(ret
, "WriteFile error %d\n", GetLastError());
190 if (nt_header
->FileHeader
.SizeOfOptionalHeader
> sizeof(IMAGE_OPTIONAL_HEADER
))
192 file_align
= nt_header
->FileHeader
.SizeOfOptionalHeader
- sizeof(IMAGE_OPTIONAL_HEADER
);
193 assert(file_align
< sizeof(filler
));
194 SetLastError(0xdeadbeef);
195 ret
= WriteFile(hfile
, filler
, file_align
, &dummy
, NULL
);
196 ok(ret
, "WriteFile error %d\n", GetLastError());
200 assert(nt_header
->FileHeader
.NumberOfSections
<= 1);
201 if (nt_header
->FileHeader
.NumberOfSections
)
203 section
.SizeOfRawData
= 10;
205 if (nt_header
->OptionalHeader
.SectionAlignment
>= page_size
)
207 section
.PointerToRawData
= dos_size
;
208 section
.VirtualAddress
= nt_header
->OptionalHeader
.SectionAlignment
;
209 section
.Misc
.VirtualSize
= section
.SizeOfRawData
* 10;
213 section
.PointerToRawData
= nt_header
->OptionalHeader
.SizeOfHeaders
;
214 section
.VirtualAddress
= nt_header
->OptionalHeader
.SizeOfHeaders
;
215 section
.Misc
.VirtualSize
= 5;
218 SetLastError(0xdeadbeef);
219 ret
= WriteFile(hfile
, §ion
, sizeof(section
), &dummy
, NULL
);
220 ok(ret
, "WriteFile error %d\n", GetLastError());
223 SetLastError(0xdeadbeef);
224 ret
= WriteFile(hfile
, section_data
, sizeof(section_data
), &dummy
, NULL
);
225 ok(ret
, "WriteFile error %d\n", GetLastError());
227 size
= GetFileSize(hfile
, NULL
);
232 static void query_image_section( int id
, const char *dll_name
, const IMAGE_NT_HEADERS
*nt_header
)
234 SECTION_BASIC_INFORMATION info
;
235 SECTION_IMAGE_INFORMATION image
;
236 ULONG info_size
= 0xdeadbeef;
238 HANDLE file
, mapping
;
240 LARGE_INTEGER map_size
;
241 /* truncated header is not handled correctly in windows <= w2k3 */
242 BOOL truncated
= nt_header
->FileHeader
.SizeOfOptionalHeader
< sizeof(nt_header
->OptionalHeader
);
244 file
= CreateFileA( dll_name
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_DELETE
,
245 NULL
, OPEN_EXISTING
, 0, 0 );
246 ok( file
!= INVALID_HANDLE_VALUE
, "%u: CreateFile error %d\n", id
, GetLastError() );
247 file_size
= GetFileSize( file
, NULL
);
249 status
= pNtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_MAP_READ
| SECTION_QUERY
,
250 NULL
, NULL
, PAGE_READONLY
, SEC_IMAGE
, file
);
251 ok( !status
, "%u: NtCreateSection failed err %x\n", id
, status
);
257 status
= pNtQuerySection( mapping
, SectionImageInformation
, &image
, sizeof(image
), &info_size
);
258 ok( !status
, "%u: NtQuerySection failed err %x\n", id
, status
);
259 ok( info_size
== sizeof(image
), "%u: NtQuerySection wrong size %u\n", id
, info_size
);
260 ok( (char *)image
.TransferAddress
== (char *)nt_header
->OptionalHeader
.ImageBase
+ nt_header
->OptionalHeader
.AddressOfEntryPoint
,
261 "%u: TransferAddress wrong %p / %p+%08x\n", id
,
262 image
.TransferAddress
, (char *)nt_header
->OptionalHeader
.ImageBase
,
263 nt_header
->OptionalHeader
.AddressOfEntryPoint
);
264 ok( image
.ZeroBits
== 0, "%u: ZeroBits wrong %08x\n", id
, image
.ZeroBits
);
265 ok( image
.MaximumStackSize
== nt_header
->OptionalHeader
.SizeOfStackReserve
|| broken(truncated
),
266 "%u: MaximumStackSize wrong %lx / %lx\n", id
,
267 image
.MaximumStackSize
, (SIZE_T
)nt_header
->OptionalHeader
.SizeOfStackReserve
);
268 ok( image
.CommittedStackSize
== nt_header
->OptionalHeader
.SizeOfStackCommit
|| broken(truncated
),
269 "%u: CommittedStackSize wrong %lx / %lx\n", id
,
270 image
.CommittedStackSize
, (SIZE_T
)nt_header
->OptionalHeader
.SizeOfStackCommit
);
272 ok( !image
.SubSystemType
|| broken(truncated
),
273 "%u: SubSystemType wrong %08x / 00000000\n", id
, image
.SubSystemType
);
275 ok( image
.SubSystemType
== nt_header
->OptionalHeader
.Subsystem
,
276 "%u: SubSystemType wrong %08x / %08x\n", id
,
277 image
.SubSystemType
, nt_header
->OptionalHeader
.Subsystem
);
278 ok( image
.SubsystemVersionLow
== nt_header
->OptionalHeader
.MinorSubsystemVersion
,
279 "%u: SubsystemVersionLow wrong %04x / %04x\n", id
,
280 image
.SubsystemVersionLow
, nt_header
->OptionalHeader
.MinorSubsystemVersion
);
281 ok( image
.SubsystemVersionHigh
== nt_header
->OptionalHeader
.MajorSubsystemVersion
,
282 "%u: SubsystemVersionHigh wrong %04x / %04x\n", id
,
283 image
.SubsystemVersionHigh
, nt_header
->OptionalHeader
.MajorSubsystemVersion
);
284 ok( image
.ImageCharacteristics
== nt_header
->FileHeader
.Characteristics
,
285 "%u: ImageCharacteristics wrong %04x / %04x\n", id
,
286 image
.ImageCharacteristics
, nt_header
->FileHeader
.Characteristics
);
287 ok( image
.DllCharacteristics
== nt_header
->OptionalHeader
.DllCharacteristics
|| broken(truncated
),
288 "%u: DllCharacteristics wrong %04x / %04x\n", id
,
289 image
.DllCharacteristics
, nt_header
->OptionalHeader
.DllCharacteristics
);
290 ok( image
.Machine
== nt_header
->FileHeader
.Machine
, "%u: Machine wrong %04x / %04x\n", id
,
291 image
.Machine
, nt_header
->FileHeader
.Machine
);
292 ok( image
.LoaderFlags
== nt_header
->OptionalHeader
.LoaderFlags
,
293 "%u: LoaderFlags wrong %08x / %08x\n", id
,
294 image
.LoaderFlags
, nt_header
->OptionalHeader
.LoaderFlags
);
295 ok( image
.ImageFileSize
== file_size
|| broken(!image
.ImageFileSize
), /* winxpsp1 */
296 "%u: ImageFileSize wrong %08x / %08x\n", id
, image
.ImageFileSize
, file_size
);
297 ok( image
.CheckSum
== nt_header
->OptionalHeader
.CheckSum
, "%u: CheckSum wrong %08x / %08x\n", id
,
298 image
.CheckSum
, nt_header
->OptionalHeader
.CheckSum
);
299 /* FIXME: needs more work: */
301 /* image.ImageFlags */
302 /* image.ImageContainsCode */
304 map_size
.QuadPart
= (nt_header
->OptionalHeader
.SizeOfImage
+ page_size
- 1) & ~(page_size
- 1);
305 status
= pNtQuerySection( mapping
, SectionBasicInformation
, &info
, sizeof(info
), NULL
);
306 ok( !status
, "NtQuerySection failed err %x\n", status
);
307 ok( info
.Size
.QuadPart
== map_size
.QuadPart
, "NtQuerySection wrong size %x%08x / %x%08x\n",
308 info
.Size
.u
.HighPart
, info
.Size
.u
.LowPart
, map_size
.u
.HighPart
, map_size
.u
.LowPart
);
309 CloseHandle( mapping
);
311 map_size
.QuadPart
= (nt_header
->OptionalHeader
.SizeOfImage
+ page_size
- 1) & ~(page_size
- 1);
312 status
= pNtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_MAP_READ
| SECTION_QUERY
,
313 NULL
, &map_size
, PAGE_READONLY
, SEC_IMAGE
, file
);
314 ok( !status
, "%u: NtCreateSection failed err %x\n", id
, status
);
315 status
= pNtQuerySection( mapping
, SectionBasicInformation
, &info
, sizeof(info
), NULL
);
316 ok( !status
, "NtQuerySection failed err %x\n", status
);
317 ok( info
.Size
.QuadPart
== map_size
.QuadPart
, "NtQuerySection wrong size %x%08x / %x%08x\n",
318 info
.Size
.u
.HighPart
, info
.Size
.u
.LowPart
, map_size
.u
.HighPart
, map_size
.u
.LowPart
);
319 CloseHandle( mapping
);
322 status
= pNtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_MAP_READ
| SECTION_QUERY
,
323 NULL
, &map_size
, PAGE_READONLY
, SEC_IMAGE
, file
);
324 ok( status
== STATUS_SECTION_TOO_BIG
, "%u: NtCreateSection failed err %x\n", id
, status
);
326 SetFilePointerEx( file
, map_size
, NULL
, FILE_BEGIN
);
327 SetEndOfFile( file
);
328 status
= pNtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_MAP_READ
| SECTION_QUERY
,
329 NULL
, &map_size
, PAGE_READONLY
, SEC_IMAGE
, file
);
330 ok( status
== STATUS_SECTION_TOO_BIG
, "%u: NtCreateSection failed err %x\n", id
, status
);
332 map_size
.QuadPart
= 1;
333 status
= pNtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_MAP_READ
| SECTION_QUERY
,
334 NULL
, &map_size
, PAGE_READONLY
, SEC_IMAGE
, file
);
335 ok( !status
, "%u: NtCreateSection failed err %x\n", id
, status
);
336 status
= pNtQuerySection( mapping
, SectionBasicInformation
, &info
, sizeof(info
), NULL
);
337 ok( !status
, "NtQuerySection failed err %x\n", status
);
338 ok( info
.Size
.QuadPart
== map_size
.QuadPart
, "NtQuerySection wrong size %x%08x / %x%08x\n",
339 info
.Size
.u
.HighPart
, info
.Size
.u
.LowPart
, map_size
.u
.HighPart
, map_size
.u
.LowPart
);
340 CloseHandle( mapping
);
345 /* helper to test image section mapping */
346 static NTSTATUS
map_image_section( const IMAGE_NT_HEADERS
*nt_header
)
348 char temp_path
[MAX_PATH
];
349 char dll_name
[MAX_PATH
];
355 GetTempPathA(MAX_PATH
, temp_path
);
356 GetTempFileNameA(temp_path
, "ldr", 0, dll_name
);
358 file_size
= create_test_dll( &dos_header
, sizeof(dos_header
), nt_header
, dll_name
);
359 ok( file_size
, "could not create %s\n", dll_name
);
361 file
= CreateFileA(dll_name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
362 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
364 size
.QuadPart
= file_size
;
365 status
= pNtCreateSection(&map
, STANDARD_RIGHTS_REQUIRED
| SECTION_MAP_READ
| SECTION_QUERY
,
366 NULL
, &size
, PAGE_READONLY
, SEC_IMAGE
, file
);
369 SECTION_BASIC_INFORMATION info
;
370 ULONG info_size
= 0xdeadbeef;
371 NTSTATUS ret
= pNtQuerySection( map
, SectionBasicInformation
, &info
, sizeof(info
), &info_size
);
372 ok( !ret
, "NtQuerySection failed err %x\n", ret
);
373 ok( info_size
== sizeof(info
), "NtQuerySection wrong size %u\n", info_size
);
374 ok( info
.Attributes
== (SEC_IMAGE
| SEC_FILE
), "NtQuerySection wrong attr %x\n", info
.Attributes
);
375 ok( info
.BaseAddress
== NULL
, "NtQuerySection wrong base %p\n", info
.BaseAddress
);
376 ok( info
.Size
.QuadPart
== file_size
, "NtQuerySection wrong size %x%08x / %08x\n",
377 info
.Size
.u
.HighPart
, info
.Size
.u
.LowPart
, file_size
);
378 query_image_section( 1000, dll_name
, nt_header
);
380 if (map
) CloseHandle( map
);
382 DeleteFileA( dll_name
);
387 static void test_Loader(void)
389 static const struct test_data
391 DWORD size_of_dos_header
;
392 WORD number_of_sections
, size_of_optional_header
;
393 DWORD section_alignment
, file_alignment
;
394 DWORD size_of_image
, size_of_headers
;
395 DWORD errors
[4]; /* 0 means LoadLibrary should succeed */
398 { sizeof(dos_header
),
400 { ERROR_BAD_EXE_FORMAT
}
402 { sizeof(dos_header
),
403 1, sizeof(IMAGE_OPTIONAL_HEADER
), 0x1000, 0x1000,
404 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
) + 0xe00,
405 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
),
406 { ERROR_BAD_EXE_FORMAT
} /* XP doesn't like too small image size */
408 { sizeof(dos_header
),
409 1, sizeof(IMAGE_OPTIONAL_HEADER
), 0x1000, 0x1000,
410 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
) + 0x1000,
411 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
),
414 { sizeof(dos_header
),
415 1, sizeof(IMAGE_OPTIONAL_HEADER
), 0x1000, 0x1000,
420 { sizeof(dos_header
),
421 1, sizeof(IMAGE_OPTIONAL_HEADER
), 0x200, 0x200,
422 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
) + 0x200,
423 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
),
424 { ERROR_SUCCESS
, ERROR_INVALID_ADDRESS
} /* vista is more strict */
426 { sizeof(dos_header
),
427 1, sizeof(IMAGE_OPTIONAL_HEADER
), 0x200, 0x1000,
428 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
) + 0x1000,
429 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
),
430 { ERROR_BAD_EXE_FORMAT
} /* XP doesn't like alignments */
432 { sizeof(dos_header
),
433 1, sizeof(IMAGE_OPTIONAL_HEADER
), 0x1000, 0x200,
434 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
) + 0x1000,
435 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
),
438 { sizeof(dos_header
),
439 1, sizeof(IMAGE_OPTIONAL_HEADER
), 0x1000, 0x200,
440 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
) + 0x1000,
444 /* Mandatory are all fields up to SizeOfHeaders, everything else
445 * is really optional (at least that's true for XP).
447 { sizeof(dos_header
),
448 1, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER
, CheckSum
), 0x200, 0x200,
449 sizeof(dos_header
) + sizeof(DWORD
) + sizeof(IMAGE_FILE_HEADER
) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER
, CheckSum
) + sizeof(IMAGE_SECTION_HEADER
) + 0x10,
450 sizeof(dos_header
) + sizeof(DWORD
) + sizeof(IMAGE_FILE_HEADER
) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER
, CheckSum
) + sizeof(IMAGE_SECTION_HEADER
),
451 { ERROR_SUCCESS
, ERROR_BAD_EXE_FORMAT
, ERROR_INVALID_ADDRESS
,
454 { sizeof(dos_header
),
455 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER
, CheckSum
), 0x200, 0x200,
456 0xd0, /* beyond of the end of file */
457 0xc0, /* beyond of the end of file */
458 { ERROR_SUCCESS
, ERROR_BAD_EXE_FORMAT
} /* vista is more strict */
460 { sizeof(dos_header
),
461 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER
, CheckSum
), 0x200, 0x200,
464 { ERROR_SUCCESS
, ERROR_BAD_EXE_FORMAT
} /* vista is more strict */
466 { sizeof(dos_header
),
467 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER
, CheckSum
), 0x200, 0x200,
470 { ERROR_SUCCESS
, ERROR_BAD_EXE_FORMAT
} /* vista is more strict */
472 #if 0 /* not power of 2 alignments need more test cases */
473 { sizeof(dos_header
),
474 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER
, CheckSum
), 0x300, 0x300,
477 { ERROR_BAD_EXE_FORMAT
} /* alignment is not power of 2 */
480 { sizeof(dos_header
),
481 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER
, CheckSum
), 4, 4,
484 { ERROR_SUCCESS
, ERROR_BAD_EXE_FORMAT
} /* vista is more strict */
486 { sizeof(dos_header
),
487 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER
, CheckSum
), 1, 1,
490 { ERROR_SUCCESS
, ERROR_BAD_EXE_FORMAT
} /* vista is more strict */
492 { sizeof(dos_header
),
493 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER
, CheckSum
), 0x200, 0x200,
496 { ERROR_BAD_EXE_FORMAT
} /* image size == 0 -> failure */
498 /* the following data mimics the PE image which upack creates */
500 1, 0x148, 0x1000, 0x200,
501 sizeof(dos_header
) + sizeof(nt_header_template
) + sizeof(IMAGE_SECTION_HEADER
) + 0x1000,
505 /* Minimal PE image that XP is able to load: 92 bytes */
507 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER
, CheckSum
),
508 0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
511 { ERROR_SUCCESS
, ERROR_BAD_EXE_FORMAT
} /* vista is more strict */
513 /* Minimal PE image that Windows7 is able to load: 268 bytes */
515 0, 0xf0, /* optional header size just forces 0xf0 bytes to be written,
516 0 or another number don't change the behaviour, what really
517 matters is file size regardless of values in the headers */
518 0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
519 0x40, /* minimal image size that Windows7 accepts */
526 HMODULE hlib
, hlib_as_data_file
;
527 char temp_path
[MAX_PATH
];
528 char dll_name
[MAX_PATH
];
532 WORD orig_machine
= nt_header_template
.FileHeader
.Machine
;
533 IMAGE_NT_HEADERS nt_header
;
535 /* prevent displaying of the "Unable to load this DLL" message box */
536 SetErrorMode(SEM_FAILCRITICALERRORS
);
538 GetTempPathA(MAX_PATH
, temp_path
);
540 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
542 GetTempFileNameA(temp_path
, "ldr", 0, dll_name
);
544 nt_header
= nt_header_template
;
545 nt_header
.FileHeader
.NumberOfSections
= td
[i
].number_of_sections
;
546 nt_header
.FileHeader
.SizeOfOptionalHeader
= td
[i
].size_of_optional_header
;
548 nt_header
.OptionalHeader
.SectionAlignment
= td
[i
].section_alignment
;
549 nt_header
.OptionalHeader
.FileAlignment
= td
[i
].file_alignment
;
550 nt_header
.OptionalHeader
.SizeOfImage
= td
[i
].size_of_image
;
551 nt_header
.OptionalHeader
.SizeOfHeaders
= td
[i
].size_of_headers
;
553 file_size
= create_test_dll( &dos_header
, td
[i
].size_of_dos_header
, &nt_header
, dll_name
);
556 ok(0, "could not create %s\n", dll_name
);
560 SetLastError(0xdeadbeef);
561 hlib
= LoadLibraryA(dll_name
);
564 MEMORY_BASIC_INFORMATION info
;
567 ok( td
[i
].errors
[0] == ERROR_SUCCESS
, "%d: should have failed\n", i
);
569 SetLastError(0xdeadbeef);
570 size
= VirtualQuery(hlib
, &info
, sizeof(info
));
571 ok(size
== sizeof(info
),
572 "%d: VirtualQuery error %d\n", i
, GetLastError());
573 ok(info
.BaseAddress
== hlib
, "%d: %p != %p\n", i
, info
.BaseAddress
, hlib
);
574 ok(info
.AllocationBase
== hlib
, "%d: %p != %p\n", i
, info
.AllocationBase
, hlib
);
575 ok(info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i
, info
.AllocationProtect
);
576 ok(info
.RegionSize
== ALIGN_SIZE(nt_header
.OptionalHeader
.SizeOfImage
, page_size
), "%d: got %lx != expected %x\n",
577 i
, info
.RegionSize
, ALIGN_SIZE(nt_header
.OptionalHeader
.SizeOfImage
, page_size
));
578 ok(info
.State
== MEM_COMMIT
, "%d: %x != MEM_COMMIT\n", i
, info
.State
);
579 if (nt_header
.OptionalHeader
.SectionAlignment
< page_size
)
580 ok(info
.Protect
== PAGE_EXECUTE_WRITECOPY
, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i
, info
.Protect
);
582 ok(info
.Protect
== PAGE_READONLY
, "%d: %x != PAGE_READONLY\n", i
, info
.Protect
);
583 ok(info
.Type
== SEC_IMAGE
, "%d: %x != SEC_IMAGE\n", i
, info
.Type
);
585 SetLastError(0xdeadbeef);
586 ptr
= VirtualAlloc(hlib
, page_size
, MEM_COMMIT
, info
.Protect
);
587 ok(!ptr
, "%d: VirtualAlloc should fail\n", i
);
588 /* FIXME: Remove once Wine is fixed */
589 todo_wine_if (info
.Protect
== PAGE_WRITECOPY
|| info
.Protect
== PAGE_EXECUTE_WRITECOPY
)
590 ok(GetLastError() == ERROR_ACCESS_DENIED
, "%d: expected ERROR_ACCESS_DENIED, got %d\n", i
, GetLastError());
592 SetLastError(0xdeadbeef);
593 size
= VirtualQuery((char *)hlib
+ info
.RegionSize
, &info
, sizeof(info
));
594 ok(size
== sizeof(info
),
595 "%d: VirtualQuery error %d\n", i
, GetLastError());
596 if (nt_header
.OptionalHeader
.SectionAlignment
== page_size
||
597 nt_header
.OptionalHeader
.SectionAlignment
== nt_header
.OptionalHeader
.FileAlignment
)
599 ok(info
.BaseAddress
== (char *)hlib
+ ALIGN_SIZE(nt_header
.OptionalHeader
.SizeOfImage
, page_size
), "%d: got %p != expected %p\n",
600 i
, info
.BaseAddress
, (char *)hlib
+ ALIGN_SIZE(nt_header
.OptionalHeader
.SizeOfImage
, page_size
));
601 ok(info
.AllocationBase
== 0, "%d: %p != 0\n", i
, info
.AllocationBase
);
602 ok(info
.AllocationProtect
== 0, "%d: %x != 0\n", i
, info
.AllocationProtect
);
603 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
604 ok(info
.State
== MEM_FREE
, "%d: %x != MEM_FREE\n", i
, info
.State
);
605 ok(info
.Type
== 0, "%d: %x != 0\n", i
, info
.Type
);
606 ok(info
.Protect
== PAGE_NOACCESS
, "%d: %x != PAGE_NOACCESS\n", i
, info
.Protect
);
610 ok(info
.Protect
== PAGE_EXECUTE_WRITECOPY
, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i
, info
.Protect
);
611 ok(info
.BaseAddress
== hlib
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, hlib
);
612 ok(info
.AllocationBase
== hlib
, "%d: %p != %p\n", i
, info
.AllocationBase
, hlib
);
613 ok(info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i
, info
.AllocationProtect
);
614 ok(info
.RegionSize
== ALIGN_SIZE(file_size
, page_size
), "%d: got %lx != expected %x\n",
615 i
, info
.RegionSize
, ALIGN_SIZE(file_size
, page_size
));
616 ok(info
.State
== MEM_COMMIT
, "%d: %x != MEM_COMMIT\n", i
, info
.State
);
617 ok(info
.Protect
== PAGE_READONLY
, "%d: %x != PAGE_READONLY\n", i
, info
.Protect
);
618 ok(info
.Type
== SEC_IMAGE
, "%d: %x != SEC_IMAGE\n", i
, info
.Type
);
621 /* header: check the zeroing of alignment */
622 if (nt_header
.OptionalHeader
.SectionAlignment
>= page_size
)
626 start
= (const char *)hlib
+ nt_header
.OptionalHeader
.SizeOfHeaders
;
627 size
= ALIGN_SIZE((ULONG_PTR
)start
, page_size
) - (ULONG_PTR
)start
;
628 ok(!memcmp(start
, filler
, size
), "%d: header alignment is not cleared\n", i
);
631 if (nt_header
.FileHeader
.NumberOfSections
)
633 SetLastError(0xdeadbeef);
634 size
= VirtualQuery((char *)hlib
+ section
.VirtualAddress
, &info
, sizeof(info
));
635 ok(size
== sizeof(info
),
636 "%d: VirtualQuery error %d\n", i
, GetLastError());
637 if (nt_header
.OptionalHeader
.SectionAlignment
< page_size
)
639 ok(info
.BaseAddress
== hlib
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, hlib
);
640 ok(info
.RegionSize
== ALIGN_SIZE(nt_header
.OptionalHeader
.SizeOfImage
, page_size
), "%d: got %lx != expected %x\n",
641 i
, info
.RegionSize
, ALIGN_SIZE(nt_header
.OptionalHeader
.SizeOfImage
, page_size
));
642 ok(info
.Protect
== PAGE_EXECUTE_WRITECOPY
, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i
, info
.Protect
);
646 ok(info
.BaseAddress
== (char *)hlib
+ section
.VirtualAddress
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, (char *)hlib
+ section
.VirtualAddress
);
647 ok(info
.RegionSize
== ALIGN_SIZE(section
.Misc
.VirtualSize
, page_size
), "%d: got %lx != expected %x\n",
648 i
, info
.RegionSize
, ALIGN_SIZE(section
.Misc
.VirtualSize
, page_size
));
649 ok(info
.Protect
== PAGE_READONLY
, "%d: %x != PAGE_READONLY\n", i
, info
.Protect
);
651 ok(info
.AllocationBase
== hlib
, "%d: %p != %p\n", i
, info
.AllocationBase
, hlib
);
652 ok(info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i
, info
.AllocationProtect
);
653 ok(info
.State
== MEM_COMMIT
, "%d: %x != MEM_COMMIT\n", i
, info
.State
);
654 ok(info
.Type
== SEC_IMAGE
, "%d: %x != SEC_IMAGE\n", i
, info
.Type
);
656 if (nt_header
.OptionalHeader
.SectionAlignment
>= page_size
)
657 ok(!memcmp((const char *)hlib
+ section
.VirtualAddress
+ section
.PointerToRawData
, &nt_header
, section
.SizeOfRawData
), "wrong section data\n");
659 ok(!memcmp((const char *)hlib
+ section
.PointerToRawData
, section_data
, section
.SizeOfRawData
), "wrong section data\n");
661 /* check the zeroing of alignment */
662 if (nt_header
.OptionalHeader
.SectionAlignment
>= page_size
)
666 start
= (const char *)hlib
+ section
.VirtualAddress
+ section
.PointerToRawData
+ section
.SizeOfRawData
;
667 size
= ALIGN_SIZE((ULONG_PTR
)start
, page_size
) - (ULONG_PTR
)start
;
668 ok(memcmp(start
, filler
, size
), "%d: alignment should not be cleared\n", i
);
671 SetLastError(0xdeadbeef);
672 ptr
= VirtualAlloc((char *)hlib
+ section
.VirtualAddress
, page_size
, MEM_COMMIT
, info
.Protect
);
673 ok(!ptr
, "%d: VirtualAlloc should fail\n", i
);
674 /* FIXME: Remove once Wine is fixed */
675 todo_wine_if (info
.Protect
== PAGE_WRITECOPY
|| info
.Protect
== PAGE_EXECUTE_WRITECOPY
)
676 ok(GetLastError() == ERROR_ACCESS_DENIED
|| GetLastError() == ERROR_INVALID_ADDRESS
,
677 "%d: expected ERROR_ACCESS_DENIED, got %d\n", i
, GetLastError());
680 SetLastError(0xdeadbeef);
681 hlib_as_data_file
= LoadLibraryExA(dll_name
, 0, LOAD_LIBRARY_AS_DATAFILE
);
682 ok(hlib_as_data_file
!= 0, "LoadLibraryEx error %u\n", GetLastError());
683 ok(hlib_as_data_file
== hlib
, "hlib_as_file and hlib are different\n");
685 SetLastError(0xdeadbeef);
686 ret
= FreeLibrary(hlib
);
687 ok(ret
, "FreeLibrary error %d\n", GetLastError());
689 SetLastError(0xdeadbeef);
690 hlib
= GetModuleHandleA(dll_name
);
691 ok(hlib
!= 0, "GetModuleHandle error %u\n", GetLastError());
693 SetLastError(0xdeadbeef);
694 ret
= FreeLibrary(hlib_as_data_file
);
695 ok(ret
, "FreeLibrary error %d\n", GetLastError());
697 hlib
= GetModuleHandleA(dll_name
);
698 ok(!hlib
, "GetModuleHandle should fail\n");
700 SetLastError(0xdeadbeef);
701 hlib_as_data_file
= LoadLibraryExA(dll_name
, 0, LOAD_LIBRARY_AS_DATAFILE
);
702 ok(hlib_as_data_file
!= 0, "LoadLibraryEx error %u\n", GetLastError());
703 ok((ULONG_PTR
)hlib_as_data_file
& 1, "hlib_as_data_file is even\n");
705 hlib
= GetModuleHandleA(dll_name
);
706 ok(!hlib
, "GetModuleHandle should fail\n");
708 SetLastError(0xdeadbeef);
709 ret
= FreeLibrary(hlib_as_data_file
);
710 ok(ret
, "FreeLibrary error %d\n", GetLastError());
712 query_image_section( i
, dll_name
, &nt_header
);
720 for (error_index
= 0;
721 ! error_match
&& error_index
< sizeof(td
[i
].errors
) / sizeof(DWORD
);
724 error_match
= td
[i
].errors
[error_index
] == GetLastError();
726 ok(error_match
, "%d: unexpected error %d\n", i
, GetLastError());
729 SetLastError(0xdeadbeef);
730 ret
= DeleteFileA(dll_name
);
731 ok(ret
, "DeleteFile error %d\n", GetLastError());
734 nt_header
= nt_header_template
;
735 nt_header
.FileHeader
.NumberOfSections
= 1;
736 nt_header
.FileHeader
.SizeOfOptionalHeader
= sizeof(IMAGE_OPTIONAL_HEADER
);
738 nt_header
.OptionalHeader
.SectionAlignment
= page_size
;
739 nt_header
.OptionalHeader
.AddressOfEntryPoint
= 0x1234;
740 nt_header
.OptionalHeader
.DllCharacteristics
= IMAGE_DLLCHARACTERISTICS_NX_COMPAT
;
741 nt_header
.OptionalHeader
.FileAlignment
= page_size
;
742 nt_header
.OptionalHeader
.SizeOfHeaders
= sizeof(dos_header
) + sizeof(nt_header
) + sizeof(IMAGE_SECTION_HEADER
);
743 nt_header
.OptionalHeader
.SizeOfImage
= sizeof(dos_header
) + sizeof(nt_header
) + sizeof(IMAGE_SECTION_HEADER
) + page_size
;
745 status
= map_image_section( &nt_header
);
746 ok( status
== STATUS_SUCCESS
, "NtCreateSection error %08x\n", status
);
748 dos_header
.e_magic
= 0;
749 status
= map_image_section( &nt_header
);
750 ok( status
== STATUS_INVALID_IMAGE_NOT_MZ
, "NtCreateSection error %08x\n", status
);
752 dos_header
.e_magic
= IMAGE_DOS_SIGNATURE
;
753 nt_header
.Signature
= IMAGE_OS2_SIGNATURE
;
754 status
= map_image_section( &nt_header
);
755 ok( status
== STATUS_INVALID_IMAGE_NE_FORMAT
, "NtCreateSection error %08x\n", status
);
757 nt_header
.Signature
= 0xdeadbeef;
758 status
= map_image_section( &nt_header
);
759 ok( status
== STATUS_INVALID_IMAGE_PROTECT
, "NtCreateSection error %08x\n", status
);
761 nt_header
.Signature
= IMAGE_NT_SIGNATURE
;
762 nt_header
.OptionalHeader
.Magic
= 0xdead;
763 status
= map_image_section( &nt_header
);
764 ok( status
== STATUS_INVALID_IMAGE_FORMAT
, "NtCreateSection error %08x\n", status
);
766 nt_header
.OptionalHeader
.Magic
= IMAGE_NT_OPTIONAL_HDR_MAGIC
;
767 nt_header
.FileHeader
.Machine
= 0xdead;
768 status
= map_image_section( &nt_header
);
769 ok( status
== STATUS_INVALID_IMAGE_FORMAT
|| broken(status
== STATUS_SUCCESS
), /* win2k */
770 "NtCreateSection error %08x\n", status
);
772 nt_header
.FileHeader
.Machine
= IMAGE_FILE_MACHINE_UNKNOWN
;
773 status
= map_image_section( &nt_header
);
774 ok( status
== STATUS_INVALID_IMAGE_FORMAT
|| broken(status
== STATUS_SUCCESS
), /* win2k */
775 "NtCreateSection error %08x\n", status
);
777 switch (orig_machine
)
779 case IMAGE_FILE_MACHINE_I386
: nt_header
.FileHeader
.Machine
= IMAGE_FILE_MACHINE_AMD64
; break;
780 case IMAGE_FILE_MACHINE_AMD64
: nt_header
.FileHeader
.Machine
= IMAGE_FILE_MACHINE_I386
; break;
781 case IMAGE_FILE_MACHINE_ARMNT
: nt_header
.FileHeader
.Machine
= IMAGE_FILE_MACHINE_ARM64
; break;
782 case IMAGE_FILE_MACHINE_ARM64
: nt_header
.FileHeader
.Machine
= IMAGE_FILE_MACHINE_ARMNT
; break;
784 status
= map_image_section( &nt_header
);
785 ok( status
== STATUS_INVALID_IMAGE_FORMAT
|| broken(status
== STATUS_SUCCESS
), /* win2k */
786 "NtCreateSection error %08x\n", status
);
788 if (nt_header
.OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR32_MAGIC
)
790 IMAGE_NT_HEADERS64 nt64
;
792 memset( &nt64
, 0, sizeof(nt64
) );
793 nt64
.Signature
= IMAGE_NT_SIGNATURE
;
794 nt64
.FileHeader
.Machine
= orig_machine
;
795 nt64
.FileHeader
.NumberOfSections
= 1;
796 nt64
.FileHeader
.SizeOfOptionalHeader
= sizeof(IMAGE_OPTIONAL_HEADER64
);
797 nt64
.OptionalHeader
.Magic
= IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
798 nt64
.OptionalHeader
.MajorLinkerVersion
= 1;
799 nt64
.OptionalHeader
.ImageBase
= 0x10000000;
800 nt64
.OptionalHeader
.MajorOperatingSystemVersion
= 4;
801 nt64
.OptionalHeader
.MajorImageVersion
= 1;
802 nt64
.OptionalHeader
.MajorSubsystemVersion
= 4;
803 nt64
.OptionalHeader
.SizeOfHeaders
= sizeof(dos_header
) + sizeof(nt64
) + sizeof(IMAGE_SECTION_HEADER
);
804 nt64
.OptionalHeader
.SizeOfImage
= nt64
.OptionalHeader
.SizeOfHeaders
+ 0x1000;
805 nt64
.OptionalHeader
.Subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
806 status
= map_image_section( (IMAGE_NT_HEADERS
*)&nt64
);
807 ok( status
== STATUS_INVALID_IMAGE_FORMAT
, "NtCreateSection error %08x\n", status
);
811 IMAGE_NT_HEADERS32 nt32
;
813 memset( &nt32
, 0, sizeof(nt32
) );
814 nt32
.Signature
= IMAGE_NT_SIGNATURE
;
815 nt32
.FileHeader
.Machine
= orig_machine
;
816 nt32
.FileHeader
.NumberOfSections
= 1;
817 nt32
.FileHeader
.SizeOfOptionalHeader
= sizeof(IMAGE_OPTIONAL_HEADER32
);
818 nt32
.OptionalHeader
.Magic
= IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
819 nt32
.OptionalHeader
.MajorLinkerVersion
= 1;
820 nt32
.OptionalHeader
.ImageBase
= 0x10000000;
821 nt32
.OptionalHeader
.MajorOperatingSystemVersion
= 4;
822 nt32
.OptionalHeader
.MajorImageVersion
= 1;
823 nt32
.OptionalHeader
.MajorSubsystemVersion
= 4;
824 nt32
.OptionalHeader
.SizeOfHeaders
= sizeof(dos_header
) + sizeof(nt32
) + sizeof(IMAGE_SECTION_HEADER
);
825 nt32
.OptionalHeader
.SizeOfImage
= nt32
.OptionalHeader
.SizeOfHeaders
+ 0x1000;
826 nt32
.OptionalHeader
.Subsystem
= IMAGE_SUBSYSTEM_WINDOWS_CUI
;
827 status
= map_image_section( (IMAGE_NT_HEADERS
*)&nt32
);
828 ok( status
== STATUS_INVALID_IMAGE_FORMAT
, "NtCreateSection error %08x\n", status
);
831 nt_header
.FileHeader
.Machine
= orig_machine
; /* restore it for the next tests */
834 /* Verify linking style of import descriptors */
835 static void test_ImportDescriptors(void)
837 HMODULE kernel32_module
= NULL
;
838 PIMAGE_DOS_HEADER d_header
;
839 PIMAGE_NT_HEADERS nt_headers
;
840 DWORD import_dir_size
;
841 DWORD_PTR dir_offset
;
842 PIMAGE_IMPORT_DESCRIPTOR import_chunk
;
844 /* Load kernel32 module */
845 kernel32_module
= GetModuleHandleA("kernel32.dll");
846 assert( kernel32_module
!= NULL
);
848 /* Get PE header info from module image */
849 d_header
= (PIMAGE_DOS_HEADER
) kernel32_module
;
850 nt_headers
= (PIMAGE_NT_HEADERS
) (((char*) d_header
) +
853 /* Get size of import entry directory */
854 import_dir_size
= nt_headers
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
].Size
;
855 if (!import_dir_size
)
857 skip("Unable to continue testing due to missing import directory.\n");
861 /* Get address of first import chunk */
862 dir_offset
= nt_headers
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
].VirtualAddress
;
863 import_chunk
= RVAToAddr(dir_offset
, kernel32_module
);
864 ok(import_chunk
!= 0, "Invalid import_chunk: %p\n", import_chunk
);
865 if (!import_chunk
) return;
867 /* Iterate through import descriptors and verify set name,
868 * OriginalFirstThunk, and FirstThunk. Core Windows DLLs, such as
869 * kernel32.dll, don't use Borland-style linking, where the table of
870 * imported names is stored directly in FirstThunk and overwritten
871 * by the relocation, instead of being stored in OriginalFirstThunk.
873 for (; import_chunk
->FirstThunk
; import_chunk
++)
875 LPCSTR module_name
= RVAToAddr(import_chunk
->Name
, kernel32_module
);
876 PIMAGE_THUNK_DATA name_table
= RVAToAddr(
877 U(*import_chunk
).OriginalFirstThunk
, kernel32_module
);
878 PIMAGE_THUNK_DATA iat
= RVAToAddr(
879 import_chunk
->FirstThunk
, kernel32_module
);
880 ok(module_name
!= NULL
, "Imported module name should not be NULL\n");
881 ok(name_table
!= NULL
,
882 "Name table for imported module %s should not be NULL\n",
884 ok(iat
!= NULL
, "IAT for imported module %s should not be NULL\n",
889 static void test_image_mapping(const char *dll_name
, DWORD scn_page_access
, BOOL is_dll
)
893 LARGE_INTEGER offset
;
896 MEMORY_BASIC_INFORMATION info
;
898 if (!pNtMapViewOfSection
) return;
900 SetLastError(0xdeadbeef);
901 hfile
= CreateFileA(dll_name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
902 ok(hfile
!= INVALID_HANDLE_VALUE
, "CreateFile error %d\n", GetLastError());
904 SetLastError(0xdeadbeef);
905 hmap
= CreateFileMappingW(hfile
, NULL
, PAGE_READONLY
| SEC_IMAGE
, 0, 0, 0);
906 ok(hmap
!= 0, "CreateFileMapping error %d\n", GetLastError());
908 offset
.u
.LowPart
= 0;
909 offset
.u
.HighPart
= 0;
913 status
= pNtMapViewOfSection(hmap
, GetCurrentProcess(), &addr1
, 0, 0, &offset
,
914 &size
, 1 /* ViewShare */, 0, PAGE_READONLY
);
915 ok(status
== STATUS_SUCCESS
, "NtMapViewOfSection error %x\n", status
);
916 ok(addr1
!= 0, "mapped address should be valid\n");
918 SetLastError(0xdeadbeef);
919 size
= VirtualQuery((char *)addr1
+ section
.VirtualAddress
, &info
, sizeof(info
));
920 ok(size
== sizeof(info
), "VirtualQuery error %d\n", GetLastError());
921 ok(info
.BaseAddress
== (char *)addr1
+ section
.VirtualAddress
, "got %p != expected %p\n", info
.BaseAddress
, (char *)addr1
+ section
.VirtualAddress
);
922 ok(info
.RegionSize
== page_size
, "got %#lx != expected %#x\n", info
.RegionSize
, page_size
);
923 ok(info
.Protect
== scn_page_access
, "got %#x != expected %#x\n", info
.Protect
, scn_page_access
);
924 ok(info
.AllocationBase
== addr1
, "%p != %p\n", info
.AllocationBase
, addr1
);
925 ok(info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
, "%#x != PAGE_EXECUTE_WRITECOPY\n", info
.AllocationProtect
);
926 ok(info
.State
== MEM_COMMIT
, "%#x != MEM_COMMIT\n", info
.State
);
927 ok(info
.Type
== SEC_IMAGE
, "%#x != SEC_IMAGE\n", info
.Type
);
931 status
= pNtMapViewOfSection(hmap
, GetCurrentProcess(), &addr2
, 0, 0, &offset
,
932 &size
, 1 /* ViewShare */, 0, PAGE_READONLY
);
933 ok(status
== STATUS_IMAGE_NOT_AT_BASE
, "expected STATUS_IMAGE_NOT_AT_BASE, got %x\n", status
);
934 ok(addr2
!= 0, "mapped address should be valid\n");
935 ok(addr2
!= addr1
, "mapped addresses should be different\n");
937 SetLastError(0xdeadbeef);
938 size
= VirtualQuery((char *)addr2
+ section
.VirtualAddress
, &info
, sizeof(info
));
939 ok(size
== sizeof(info
), "VirtualQuery error %d\n", GetLastError());
940 ok(info
.BaseAddress
== (char *)addr2
+ section
.VirtualAddress
, "got %p != expected %p\n", info
.BaseAddress
, (char *)addr2
+ section
.VirtualAddress
);
941 ok(info
.RegionSize
== page_size
, "got %#lx != expected %#x\n", info
.RegionSize
, page_size
);
942 ok(info
.Protect
== scn_page_access
, "got %#x != expected %#x\n", info
.Protect
, scn_page_access
);
943 ok(info
.AllocationBase
== addr2
, "%p != %p\n", info
.AllocationBase
, addr2
);
944 ok(info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
, "%#x != PAGE_EXECUTE_WRITECOPY\n", info
.AllocationProtect
);
945 ok(info
.State
== MEM_COMMIT
, "%#x != MEM_COMMIT\n", info
.State
);
946 ok(info
.Type
== SEC_IMAGE
, "%#x != SEC_IMAGE\n", info
.Type
);
948 status
= pNtUnmapViewOfSection(GetCurrentProcess(), addr2
);
949 ok(status
== STATUS_SUCCESS
, "NtUnmapViewOfSection error %x\n", status
);
951 addr2
= MapViewOfFile(hmap
, 0, 0, 0, 0);
952 ok(addr2
!= 0, "mapped address should be valid\n");
953 ok(addr2
!= addr1
, "mapped addresses should be different\n");
955 SetLastError(0xdeadbeef);
956 size
= VirtualQuery((char *)addr2
+ section
.VirtualAddress
, &info
, sizeof(info
));
957 ok(size
== sizeof(info
), "VirtualQuery error %d\n", GetLastError());
958 ok(info
.BaseAddress
== (char *)addr2
+ section
.VirtualAddress
, "got %p != expected %p\n", info
.BaseAddress
, (char *)addr2
+ section
.VirtualAddress
);
959 ok(info
.RegionSize
== page_size
, "got %#lx != expected %#x\n", info
.RegionSize
, page_size
);
960 ok(info
.Protect
== scn_page_access
, "got %#x != expected %#x\n", info
.Protect
, scn_page_access
);
961 ok(info
.AllocationBase
== addr2
, "%p != %p\n", info
.AllocationBase
, addr2
);
962 ok(info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
, "%#x != PAGE_EXECUTE_WRITECOPY\n", info
.AllocationProtect
);
963 ok(info
.State
== MEM_COMMIT
, "%#x != MEM_COMMIT\n", info
.State
);
964 ok(info
.Type
== SEC_IMAGE
, "%#x != SEC_IMAGE\n", info
.Type
);
966 UnmapViewOfFile(addr2
);
968 SetLastError(0xdeadbeef);
969 addr2
= LoadLibraryA(dll_name
);
972 ok(!addr2
, "LoadLibrary should fail, is_dll %d\n", is_dll
);
973 ok(GetLastError() == ERROR_INVALID_ADDRESS
, "expected ERROR_INVALID_ADDRESS, got %d\n", GetLastError());
978 ok(addr2
!= 0, "LoadLibrary error %d, is_dll %d\n", GetLastError(), is_dll
);
979 ok(addr2
!= addr1
, "mapped addresses should be different\n");
981 SetLastError(0xdeadbeef);
982 ret
= FreeLibrary(addr2
);
983 ok(ret
, "FreeLibrary error %d\n", GetLastError());
986 status
= pNtUnmapViewOfSection(GetCurrentProcess(), addr1
);
987 ok(status
== STATUS_SUCCESS
, "NtUnmapViewOfSection error %x\n", status
);
993 static BOOL
is_mem_writable(DWORD prot
)
999 case PAGE_EXECUTE_READWRITE
:
1000 case PAGE_EXECUTE_WRITECOPY
:
1008 static void test_VirtualProtect(void *base
, void *section
)
1010 static const struct test_data
1012 DWORD prot_set
, prot_get
;
1015 { 0, 0 }, /* 0x00 */
1016 { PAGE_NOACCESS
, PAGE_NOACCESS
}, /* 0x01 */
1017 { PAGE_READONLY
, PAGE_READONLY
}, /* 0x02 */
1018 { PAGE_READONLY
| PAGE_NOACCESS
, 0 }, /* 0x03 */
1019 { PAGE_READWRITE
, PAGE_WRITECOPY
}, /* 0x04 */
1020 { PAGE_READWRITE
| PAGE_NOACCESS
, 0 }, /* 0x05 */
1021 { PAGE_READWRITE
| PAGE_READONLY
, 0 }, /* 0x06 */
1022 { PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, 0 }, /* 0x07 */
1023 { PAGE_WRITECOPY
, PAGE_WRITECOPY
}, /* 0x08 */
1024 { PAGE_WRITECOPY
| PAGE_NOACCESS
, 0 }, /* 0x09 */
1025 { PAGE_WRITECOPY
| PAGE_READONLY
, 0 }, /* 0x0a */
1026 { PAGE_WRITECOPY
| PAGE_NOACCESS
| PAGE_READONLY
, 0 }, /* 0x0b */
1027 { PAGE_WRITECOPY
| PAGE_READWRITE
, 0 }, /* 0x0c */
1028 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_NOACCESS
, 0 }, /* 0x0d */
1029 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
, 0 }, /* 0x0e */
1030 { PAGE_WRITECOPY
| PAGE_READWRITE
| PAGE_READONLY
| PAGE_NOACCESS
, 0 }, /* 0x0f */
1032 { PAGE_EXECUTE
, PAGE_EXECUTE
}, /* 0x10 */
1033 { PAGE_EXECUTE_READ
, PAGE_EXECUTE_READ
}, /* 0x20 */
1034 { PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 }, /* 0x30 */
1035 { PAGE_EXECUTE_READWRITE
, PAGE_EXECUTE_WRITECOPY
}, /* 0x40 */
1036 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, 0 }, /* 0x50 */
1037 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, 0 }, /* 0x60 */
1038 { PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 }, /* 0x70 */
1039 { PAGE_EXECUTE_WRITECOPY
, PAGE_EXECUTE_WRITECOPY
}, /* 0x80 */
1040 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE
, 0 }, /* 0x90 */
1041 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
, 0 }, /* 0xa0 */
1042 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 }, /* 0xb0 */
1043 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
, 0 }, /* 0xc0 */
1044 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE
, 0 }, /* 0xd0 */
1045 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
, 0 }, /* 0xe0 */
1046 { PAGE_EXECUTE_WRITECOPY
| PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_READ
| PAGE_EXECUTE
, 0 } /* 0xf0 */
1048 DWORD ret
, orig_prot
, old_prot
, rw_prot
, exec_prot
, i
, j
;
1049 MEMORY_BASIC_INFORMATION info
;
1051 SetLastError(0xdeadbeef);
1052 ret
= VirtualProtect(section
, page_size
, PAGE_NOACCESS
, &old_prot
);
1053 ok(ret
, "VirtualProtect error %d\n", GetLastError());
1055 orig_prot
= old_prot
;
1057 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
1059 SetLastError(0xdeadbeef);
1060 ret
= VirtualQuery(section
, &info
, sizeof(info
));
1061 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
1062 ok(info
.BaseAddress
== section
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, section
);
1063 ok(info
.RegionSize
== page_size
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, page_size
);
1064 ok(info
.Protect
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, info
.Protect
);
1065 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
1066 ok(info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i
, info
.AllocationProtect
);
1067 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
1068 ok(info
.Type
== SEC_IMAGE
, "%d: %#x != SEC_IMAGE\n", i
, info
.Type
);
1070 old_prot
= 0xdeadbeef;
1071 SetLastError(0xdeadbeef);
1072 ret
= VirtualProtect(section
, page_size
, td
[i
].prot_set
, &old_prot
);
1075 ok(ret
, "%d: VirtualProtect error %d, requested prot %#x\n", i
, GetLastError(), td
[i
].prot_set
);
1076 ok(old_prot
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, old_prot
);
1078 SetLastError(0xdeadbeef);
1079 ret
= VirtualQuery(section
, &info
, sizeof(info
));
1080 ok(ret
, "VirtualQuery failed %d\n", GetLastError());
1081 ok(info
.BaseAddress
== section
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, section
);
1082 ok(info
.RegionSize
== page_size
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, page_size
);
1083 ok(info
.Protect
== td
[i
].prot_get
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].prot_get
);
1084 ok(info
.AllocationBase
== base
, "%d: %p != %p\n", i
, info
.AllocationBase
, base
);
1085 ok(info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i
, info
.AllocationProtect
);
1086 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
1087 ok(info
.Type
== SEC_IMAGE
, "%d: %#x != SEC_IMAGE\n", i
, info
.Type
);
1091 ok(!ret
, "%d: VirtualProtect should fail\n", i
);
1092 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "%d: expected ERROR_INVALID_PARAMETER, got %d\n", i
, GetLastError());
1095 old_prot
= 0xdeadbeef;
1096 SetLastError(0xdeadbeef);
1097 ret
= VirtualProtect(section
, page_size
, PAGE_NOACCESS
, &old_prot
);
1098 ok(ret
, "%d: VirtualProtect error %d\n", i
, GetLastError());
1100 ok(old_prot
== td
[i
].prot_get
, "%d: got %#x != expected %#x\n", i
, old_prot
, td
[i
].prot_get
);
1102 ok(old_prot
== PAGE_NOACCESS
, "%d: got %#x != expected PAGE_NOACCESS\n", i
, old_prot
);
1107 for (i
= 0; i
<= 4; i
++)
1111 for (j
= 0; j
<= 4; j
++)
1113 DWORD prot
= exec_prot
| rw_prot
;
1115 SetLastError(0xdeadbeef);
1116 ret
= VirtualProtect(section
, page_size
, prot
, &old_prot
);
1117 if ((rw_prot
&& exec_prot
) || (!rw_prot
&& !exec_prot
))
1119 ok(!ret
, "VirtualProtect(%02x) should fail\n", prot
);
1120 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
1123 ok(ret
, "VirtualProtect(%02x) error %d\n", prot
, GetLastError());
1128 exec_prot
= 1 << (i
+ 4);
1131 SetLastError(0xdeadbeef);
1132 ret
= VirtualProtect(section
, page_size
, orig_prot
, &old_prot
);
1133 ok(ret
, "VirtualProtect error %d\n", GetLastError());
1136 static void test_section_access(void)
1138 static const struct test_data
1140 DWORD scn_file_access
, scn_page_access
, scn_page_access_after_write
;
1143 { 0, PAGE_NOACCESS
, 0 },
1144 { IMAGE_SCN_MEM_READ
, PAGE_READONLY
, 0 },
1145 { IMAGE_SCN_MEM_WRITE
, PAGE_WRITECOPY
, PAGE_READWRITE
},
1146 { IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE
, 0 },
1147 { IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
, PAGE_WRITECOPY
, PAGE_READWRITE
},
1148 { IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE_READ
},
1149 { IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE_WRITECOPY
, PAGE_EXECUTE_READWRITE
},
1150 { IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE_WRITECOPY
, PAGE_EXECUTE_READWRITE
},
1152 { IMAGE_SCN_CNT_INITIALIZED_DATA
, PAGE_NOACCESS
, 0 },
1153 { IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
, PAGE_READONLY
, 0 },
1154 { IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_WRITE
, PAGE_WRITECOPY
, PAGE_READWRITE
},
1155 { IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE
, 0 },
1156 { IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
, PAGE_WRITECOPY
, PAGE_READWRITE
},
1157 { IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE_READ
, 0 },
1158 { IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE_WRITECOPY
, PAGE_EXECUTE_READWRITE
},
1159 { IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE_WRITECOPY
, PAGE_EXECUTE_READWRITE
},
1161 { IMAGE_SCN_CNT_UNINITIALIZED_DATA
, PAGE_NOACCESS
, 0 },
1162 { IMAGE_SCN_CNT_UNINITIALIZED_DATA
| IMAGE_SCN_MEM_READ
, PAGE_READONLY
, 0 },
1163 { IMAGE_SCN_CNT_UNINITIALIZED_DATA
| IMAGE_SCN_MEM_WRITE
, PAGE_WRITECOPY
, PAGE_READWRITE
},
1164 { IMAGE_SCN_CNT_UNINITIALIZED_DATA
| IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE
, 0 },
1165 { IMAGE_SCN_CNT_UNINITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
, PAGE_WRITECOPY
, PAGE_READWRITE
},
1166 { IMAGE_SCN_CNT_UNINITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE_READ
, 0 },
1167 { IMAGE_SCN_CNT_UNINITIALIZED_DATA
| IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE_WRITECOPY
, PAGE_EXECUTE_READWRITE
},
1168 { IMAGE_SCN_CNT_UNINITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_EXECUTE
, PAGE_EXECUTE_WRITECOPY
, PAGE_EXECUTE_READWRITE
}
1172 DWORD dummy
, file_align
;
1175 char temp_path
[MAX_PATH
];
1176 char dll_name
[MAX_PATH
];
1178 MEMORY_BASIC_INFORMATION info
;
1180 PROCESS_INFORMATION pi
;
1183 /* prevent displaying of the "Unable to load this DLL" message box */
1184 SetErrorMode(SEM_FAILCRITICALERRORS
);
1186 GetTempPathA(MAX_PATH
, temp_path
);
1188 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
1190 IMAGE_NT_HEADERS nt_header
;
1192 GetTempFileNameA(temp_path
, "ldr", 0, dll_name
);
1194 /*trace("creating %s\n", dll_name);*/
1195 hfile
= CreateFileA(dll_name
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
1196 if (hfile
== INVALID_HANDLE_VALUE
)
1198 ok(0, "could not create %s\n", dll_name
);
1202 SetLastError(0xdeadbeef);
1203 ret
= WriteFile(hfile
, &dos_header
, sizeof(dos_header
), &dummy
, NULL
);
1204 ok(ret
, "WriteFile error %d\n", GetLastError());
1206 nt_header
= nt_header_template
;
1207 nt_header
.FileHeader
.NumberOfSections
= 1;
1208 nt_header
.FileHeader
.SizeOfOptionalHeader
= sizeof(IMAGE_OPTIONAL_HEADER
);
1209 nt_header
.FileHeader
.Characteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
| IMAGE_FILE_DLL
| IMAGE_FILE_RELOCS_STRIPPED
;
1211 nt_header
.OptionalHeader
.SectionAlignment
= page_size
;
1212 nt_header
.OptionalHeader
.FileAlignment
= 0x200;
1213 nt_header
.OptionalHeader
.SizeOfImage
= sizeof(dos_header
) + sizeof(nt_header
) + sizeof(IMAGE_SECTION_HEADER
) + page_size
;
1214 nt_header
.OptionalHeader
.SizeOfHeaders
= sizeof(dos_header
) + sizeof(nt_header
) + sizeof(IMAGE_SECTION_HEADER
);
1215 SetLastError(0xdeadbeef);
1216 ret
= WriteFile(hfile
, &nt_header
, sizeof(DWORD
) + sizeof(IMAGE_FILE_HEADER
), &dummy
, NULL
);
1217 ok(ret
, "WriteFile error %d\n", GetLastError());
1218 SetLastError(0xdeadbeef);
1219 ret
= WriteFile(hfile
, &nt_header
.OptionalHeader
, sizeof(IMAGE_OPTIONAL_HEADER
), &dummy
, NULL
);
1220 ok(ret
, "WriteFile error %d\n", GetLastError());
1222 section
.SizeOfRawData
= sizeof(section_data
);
1223 section
.PointerToRawData
= nt_header
.OptionalHeader
.FileAlignment
;
1224 section
.VirtualAddress
= nt_header
.OptionalHeader
.SectionAlignment
;
1225 section
.Misc
.VirtualSize
= section
.SizeOfRawData
;
1226 section
.Characteristics
= td
[i
].scn_file_access
;
1227 SetLastError(0xdeadbeef);
1228 ret
= WriteFile(hfile
, §ion
, sizeof(section
), &dummy
, NULL
);
1229 ok(ret
, "WriteFile error %d\n", GetLastError());
1231 file_align
= nt_header
.OptionalHeader
.FileAlignment
- nt_header
.OptionalHeader
.SizeOfHeaders
;
1232 assert(file_align
< sizeof(filler
));
1233 SetLastError(0xdeadbeef);
1234 ret
= WriteFile(hfile
, filler
, file_align
, &dummy
, NULL
);
1235 ok(ret
, "WriteFile error %d\n", GetLastError());
1238 SetLastError(0xdeadbeef);
1239 ret
= WriteFile(hfile
, section_data
, sizeof(section_data
), &dummy
, NULL
);
1240 ok(ret
, "WriteFile error %d\n", GetLastError());
1244 SetLastError(0xdeadbeef);
1245 hlib
= LoadLibraryA(dll_name
);
1246 ok(hlib
!= 0, "LoadLibrary error %d\n", GetLastError());
1248 SetLastError(0xdeadbeef);
1249 size
= VirtualQuery((char *)hlib
+ section
.VirtualAddress
, &info
, sizeof(info
));
1250 ok(size
== sizeof(info
),
1251 "%d: VirtualQuery error %d\n", i
, GetLastError());
1252 ok(info
.BaseAddress
== (char *)hlib
+ section
.VirtualAddress
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, (char *)hlib
+ section
.VirtualAddress
);
1253 ok(info
.RegionSize
== page_size
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, page_size
);
1254 ok(info
.Protect
== td
[i
].scn_page_access
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].scn_page_access
);
1255 ok(info
.AllocationBase
== hlib
, "%d: %p != %p\n", i
, info
.AllocationBase
, hlib
);
1256 ok(info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i
, info
.AllocationProtect
);
1257 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
1258 ok(info
.Type
== SEC_IMAGE
, "%d: %#x != SEC_IMAGE\n", i
, info
.Type
);
1259 if (info
.Protect
!= PAGE_NOACCESS
)
1260 ok(!memcmp((const char *)info
.BaseAddress
, section_data
, section
.SizeOfRawData
), "wrong section data\n");
1262 test_VirtualProtect(hlib
, (char *)hlib
+ section
.VirtualAddress
);
1264 /* Windows changes the WRITECOPY to WRITE protection on an image section write (for a changed page only) */
1265 if (is_mem_writable(info
.Protect
))
1267 char *p
= info
.BaseAddress
;
1269 SetLastError(0xdeadbeef);
1270 size
= VirtualQuery((char *)hlib
+ section
.VirtualAddress
, &info
, sizeof(info
));
1271 ok(size
== sizeof(info
), "%d: VirtualQuery error %d\n", i
, GetLastError());
1272 /* FIXME: remove the condition below once Wine is fixed */
1273 todo_wine_if (info
.Protect
== PAGE_WRITECOPY
|| info
.Protect
== PAGE_EXECUTE_WRITECOPY
)
1274 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
);
1277 SetLastError(0xdeadbeef);
1278 ret
= FreeLibrary(hlib
);
1279 ok(ret
, "FreeLibrary error %d\n", GetLastError());
1281 test_image_mapping(dll_name
, td
[i
].scn_page_access
, TRUE
);
1283 /* reset IMAGE_FILE_DLL otherwise CreateProcess fails */
1284 nt_header
.FileHeader
.Characteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
| IMAGE_FILE_RELOCS_STRIPPED
;
1285 SetLastError(0xdeadbeef);
1286 hfile
= CreateFileA(dll_name
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1287 /* LoadLibrary called on an already memory-mapped file in
1288 * test_image_mapping() above leads to a file handle leak
1289 * under nt4, and inability to overwrite and delete the file
1290 * due to sharing violation error. Ignore it and skip the test,
1291 * but leave a not deletable temporary file.
1293 ok(hfile
!= INVALID_HANDLE_VALUE
|| broken(hfile
== INVALID_HANDLE_VALUE
) /* nt4 */,
1294 "CreateFile error %d\n", GetLastError());
1295 if (hfile
== INVALID_HANDLE_VALUE
) goto nt4_is_broken
;
1296 SetFilePointer(hfile
, sizeof(dos_header
), NULL
, FILE_BEGIN
);
1297 SetLastError(0xdeadbeef);
1298 ret
= WriteFile(hfile
, &nt_header
, sizeof(DWORD
) + sizeof(IMAGE_FILE_HEADER
), &dummy
, NULL
);
1299 ok(ret
, "WriteFile error %d\n", GetLastError());
1302 memset(&sti
, 0, sizeof(sti
));
1303 sti
.cb
= sizeof(sti
);
1304 SetLastError(0xdeadbeef);
1305 ret
= CreateProcessA(dll_name
, NULL
, NULL
, NULL
, FALSE
, CREATE_SUSPENDED
, NULL
, NULL
, &sti
, &pi
);
1306 ok(ret
, "CreateProcess() error %d\n", GetLastError());
1308 SetLastError(0xdeadbeef);
1309 size
= VirtualQueryEx(pi
.hProcess
, (char *)hlib
+ section
.VirtualAddress
, &info
, sizeof(info
));
1310 ok(size
== sizeof(info
),
1311 "%d: VirtualQuery error %d\n", i
, GetLastError());
1312 ok(info
.BaseAddress
== (char *)hlib
+ section
.VirtualAddress
, "%d: got %p != expected %p\n", i
, info
.BaseAddress
, (char *)hlib
+ section
.VirtualAddress
);
1313 ok(info
.RegionSize
== page_size
, "%d: got %#lx != expected %#x\n", i
, info
.RegionSize
, page_size
);
1314 ok(info
.Protect
== td
[i
].scn_page_access
, "%d: got %#x != expected %#x\n", i
, info
.Protect
, td
[i
].scn_page_access
);
1315 ok(info
.AllocationBase
== hlib
, "%d: %p != %p\n", i
, info
.AllocationBase
, hlib
);
1316 ok(info
.AllocationProtect
== PAGE_EXECUTE_WRITECOPY
, "%d: %#x != PAGE_EXECUTE_WRITECOPY\n", i
, info
.AllocationProtect
);
1317 ok(info
.State
== MEM_COMMIT
, "%d: %#x != MEM_COMMIT\n", i
, info
.State
);
1318 ok(info
.Type
== SEC_IMAGE
, "%d: %#x != SEC_IMAGE\n", i
, info
.Type
);
1319 if (info
.Protect
!= PAGE_NOACCESS
)
1321 SetLastError(0xdeadbeef);
1322 ret
= ReadProcessMemory(pi
.hProcess
, info
.BaseAddress
, buf
, section
.SizeOfRawData
, NULL
);
1323 ok(ret
, "ReadProcessMemory() error %d\n", GetLastError());
1324 ok(!memcmp(buf
, section_data
, section
.SizeOfRawData
), "wrong section data\n");
1327 SetLastError(0xdeadbeef);
1328 ret
= TerminateProcess(pi
.hProcess
, 0);
1329 ok(ret
, "TerminateProcess() error %d\n", GetLastError());
1330 ret
= WaitForSingleObject(pi
.hProcess
, 3000);
1331 ok(ret
== WAIT_OBJECT_0
, "WaitForSingleObject failed: %x\n", ret
);
1333 CloseHandle(pi
.hThread
);
1334 CloseHandle(pi
.hProcess
);
1336 test_image_mapping(dll_name
, td
[i
].scn_page_access
, FALSE
);
1339 SetLastError(0xdeadbeef);
1340 ret
= DeleteFileA(dll_name
);
1341 ok(ret
|| broken(!ret
) /* nt4 */, "DeleteFile error %d\n", GetLastError());
1345 static void test_import_resolution(void)
1347 char temp_path
[MAX_PATH
];
1348 char dll_name
[MAX_PATH
];
1356 IMAGE_IMPORT_DESCRIPTOR descr
[2];
1357 IMAGE_THUNK_DATA original_thunks
[2];
1358 IMAGE_THUNK_DATA thunks
[2];
1360 struct { WORD hint
; char name
[32]; } function
;
1361 IMAGE_TLS_DIRECTORY tls
;
1365 IMAGE_NT_HEADERS nt
;
1366 IMAGE_SECTION_HEADER section
;
1369 for (test
= 0; test
< 3; test
++)
1371 #define DATA_RVA(ptr) (page_size + ((char *)(ptr) - (char *)&data))
1372 nt
= nt_header_template
;
1373 nt
.FileHeader
.NumberOfSections
= 1;
1374 nt
.FileHeader
.SizeOfOptionalHeader
= sizeof(IMAGE_OPTIONAL_HEADER
);
1375 nt
.FileHeader
.Characteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
| IMAGE_FILE_32BIT_MACHINE
| IMAGE_FILE_RELOCS_STRIPPED
;
1376 if (test
!= 2) nt
.FileHeader
.Characteristics
|= IMAGE_FILE_DLL
;
1377 nt
.OptionalHeader
.SectionAlignment
= page_size
;
1378 nt
.OptionalHeader
.FileAlignment
= 0x200;
1379 nt
.OptionalHeader
.ImageBase
= 0x12340000;
1380 nt
.OptionalHeader
.SizeOfImage
= 2 * page_size
;
1381 nt
.OptionalHeader
.SizeOfHeaders
= nt
.OptionalHeader
.FileAlignment
;
1382 nt
.OptionalHeader
.NumberOfRvaAndSizes
= IMAGE_NUMBEROF_DIRECTORY_ENTRIES
;
1383 memset( nt
.OptionalHeader
.DataDirectory
, 0, sizeof(nt
.OptionalHeader
.DataDirectory
) );
1384 nt
.OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
].Size
= sizeof(data
.descr
);
1385 nt
.OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
].VirtualAddress
= DATA_RVA(data
.descr
);
1386 nt
.OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_TLS
].Size
= sizeof(data
.tls
);
1387 nt
.OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_TLS
].VirtualAddress
= DATA_RVA(&data
.tls
);
1389 memset( &data
, 0, sizeof(data
) );
1390 data
.descr
[0].u
.OriginalFirstThunk
= DATA_RVA( data
.original_thunks
);
1391 data
.descr
[0].FirstThunk
= DATA_RVA( data
.thunks
);
1392 data
.descr
[0].Name
= DATA_RVA( data
.module
);
1393 strcpy( data
.module
, "kernel32.dll" );
1394 strcpy( data
.function
.name
, "CreateEventA" );
1395 data
.original_thunks
[0].u1
.AddressOfData
= DATA_RVA( &data
.function
);
1396 data
.thunks
[0].u1
.AddressOfData
= 0xdeadbeef;
1398 data
.tls
.StartAddressOfRawData
= nt
.OptionalHeader
.ImageBase
+ DATA_RVA( data
.tls_data
);
1399 data
.tls
.EndAddressOfRawData
= data
.tls
.StartAddressOfRawData
+ sizeof(data
.tls_data
);
1400 data
.tls
.AddressOfIndex
= nt
.OptionalHeader
.ImageBase
+ DATA_RVA( &data
.tls_index
);
1401 strcpy( data
.tls_data
, "hello world" );
1402 data
.tls_index
= 9999;
1404 GetTempPathA(MAX_PATH
, temp_path
);
1405 GetTempFileNameA(temp_path
, "ldr", 0, dll_name
);
1407 hfile
= CreateFileA(dll_name
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
, CREATE_ALWAYS
, 0, 0);
1408 ok( hfile
!= INVALID_HANDLE_VALUE
, "creation failed\n" );
1410 memset( §ion
, 0, sizeof(section
) );
1411 memcpy( section
.Name
, ".text", sizeof(".text") );
1412 section
.PointerToRawData
= nt
.OptionalHeader
.FileAlignment
;
1413 section
.VirtualAddress
= nt
.OptionalHeader
.SectionAlignment
;
1414 section
.Misc
.VirtualSize
= sizeof(data
);
1415 section
.SizeOfRawData
= sizeof(data
);
1416 section
.Characteristics
= IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
;
1418 WriteFile(hfile
, &dos_header
, sizeof(dos_header
), &dummy
, NULL
);
1419 WriteFile(hfile
, &nt
, sizeof(nt
), &dummy
, NULL
);
1420 WriteFile(hfile
, §ion
, sizeof(section
), &dummy
, NULL
);
1422 SetFilePointer( hfile
, section
.PointerToRawData
, NULL
, SEEK_SET
);
1423 WriteFile(hfile
, &data
, sizeof(data
), &dummy
, NULL
);
1425 CloseHandle( hfile
);
1429 case 0: /* normal load */
1430 mod
= LoadLibraryA( dll_name
);
1431 ok( mod
!= NULL
, "failed to load err %u\n", GetLastError() );
1433 ptr
= (struct imports
*)((char *)mod
+ page_size
);
1434 expect
= GetProcAddress( GetModuleHandleA( data
.module
), data
.function
.name
);
1435 ok( (void *)ptr
->thunks
[0].u1
.Function
== expect
, "thunk %p instead of %p for %s.%s\n",
1436 (void *)ptr
->thunks
[0].u1
.Function
, expect
, data
.module
, data
.function
.name
);
1437 ok( ptr
->tls_index
< 32 || broken(ptr
->tls_index
== 9999), /* before vista */
1438 "wrong tls index %d\n", ptr
->tls_index
);
1439 if (ptr
->tls_index
!= 9999)
1441 str
= ((char **)NtCurrentTeb()->ThreadLocalStoragePointer
)[ptr
->tls_index
];
1442 ok( !strcmp( str
, "hello world" ), "wrong tls data '%s' at %p\n", str
, str
);
1446 case 1: /* load with DONT_RESOLVE_DLL_REFERENCES doesn't resolve imports */
1447 mod
= LoadLibraryExA( dll_name
, 0, DONT_RESOLVE_DLL_REFERENCES
);
1448 ok( mod
!= NULL
, "failed to load err %u\n", GetLastError() );
1450 ptr
= (struct imports
*)((char *)mod
+ page_size
);
1451 ok( ptr
->thunks
[0].u1
.Function
== 0xdeadbeef, "thunk resolved to %p for %s.%s\n",
1452 (void *)ptr
->thunks
[0].u1
.Function
, data
.module
, data
.function
.name
);
1453 ok( ptr
->tls_index
== 9999, "wrong tls index %d\n", ptr
->tls_index
);
1455 mod2
= LoadLibraryA( dll_name
);
1456 ok( mod2
== mod
, "loaded twice %p / %p\n", mod
, mod2
);
1457 ok( ptr
->thunks
[0].u1
.Function
== 0xdeadbeef, "thunk resolved to %p for %s.%s\n",
1458 (void *)ptr
->thunks
[0].u1
.Function
, data
.module
, data
.function
.name
);
1459 ok( ptr
->tls_index
== 9999, "wrong tls index %d\n", ptr
->tls_index
);
1462 case 2: /* load without IMAGE_FILE_DLL doesn't resolve imports */
1463 mod
= LoadLibraryA( dll_name
);
1464 ok( mod
!= NULL
, "failed to load err %u\n", GetLastError() );
1466 ptr
= (struct imports
*)((char *)mod
+ page_size
);
1467 ok( ptr
->thunks
[0].u1
.Function
== 0xdeadbeef, "thunk resolved to %p for %s.%s\n",
1468 (void *)ptr
->thunks
[0].u1
.Function
, data
.module
, data
.function
.name
);
1469 ok( ptr
->tls_index
== 9999, "wrong tls index %d\n", ptr
->tls_index
);
1473 DeleteFileA( dll_name
);
1478 #define MAX_COUNT 10
1479 static HANDLE attached_thread
[MAX_COUNT
];
1480 static DWORD attached_thread_count
;
1481 HANDLE stop_event
, event
, mutex
, semaphore
, loader_lock_event
, peb_lock_event
, heap_lock_event
, ack_event
;
1482 static int test_dll_phase
, inside_loader_lock
, inside_peb_lock
, inside_heap_lock
;
1483 static LONG fls_callback_count
;
1485 static DWORD WINAPI
mutex_thread_proc(void *param
)
1487 HANDLE wait_list
[4];
1490 ret
= WaitForSingleObject(mutex
, 0);
1491 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %#x\n", ret
);
1495 wait_list
[0] = stop_event
;
1496 wait_list
[1] = loader_lock_event
;
1497 wait_list
[2] = peb_lock_event
;
1498 wait_list
[3] = heap_lock_event
;
1500 trace("%04u: mutex_thread_proc: starting\n", GetCurrentThreadId());
1503 ret
= WaitForMultipleObjects(sizeof(wait_list
)/sizeof(wait_list
[0]), wait_list
, FALSE
, 50);
1504 if (ret
== WAIT_OBJECT_0
) break;
1505 else if (ret
== WAIT_OBJECT_0
+ 1)
1507 ULONG_PTR loader_lock_magic
;
1508 trace("%04u: mutex_thread_proc: Entering loader lock\n", GetCurrentThreadId());
1509 ret
= pLdrLockLoaderLock(0, NULL
, &loader_lock_magic
);
1510 ok(!ret
, "LdrLockLoaderLock error %#x\n", ret
);
1511 inside_loader_lock
++;
1512 SetEvent(ack_event
);
1514 else if (ret
== WAIT_OBJECT_0
+ 2)
1516 trace("%04u: mutex_thread_proc: Entering PEB lock\n", GetCurrentThreadId());
1517 pRtlAcquirePebLock();
1519 SetEvent(ack_event
);
1521 else if (ret
== WAIT_OBJECT_0
+ 3)
1523 trace("%04u: mutex_thread_proc: Entering heap lock\n", GetCurrentThreadId());
1524 HeapLock(GetProcessHeap());
1526 SetEvent(ack_event
);
1530 trace("%04u: mutex_thread_proc: exiting\n", GetCurrentThreadId());
1534 static DWORD WINAPI
semaphore_thread_proc(void *param
)
1538 ret
= WaitForSingleObject(semaphore
, 0);
1539 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %#x\n", ret
);
1545 if (winetest_debug
> 1)
1546 trace("%04u: semaphore_thread_proc: still alive\n", GetCurrentThreadId());
1547 if (WaitForSingleObject(stop_event
, 50) != WAIT_TIMEOUT
) break;
1550 trace("%04u: semaphore_thread_proc: exiting\n", GetCurrentThreadId());
1554 static DWORD WINAPI
noop_thread_proc(void *param
)
1558 LONG
*noop_thread_started
= param
;
1559 InterlockedIncrement(noop_thread_started
);
1562 trace("%04u: noop_thread_proc: exiting\n", GetCurrentThreadId());
1566 static VOID WINAPI
fls_callback(PVOID lpFlsData
)
1568 ok(lpFlsData
== (void*) 0x31415, "lpFlsData is %p, expected %p\n", lpFlsData
, (void*) 0x31415);
1569 InterlockedIncrement(&fls_callback_count
);
1572 static BOOL WINAPI
dll_entry_point(HINSTANCE hinst
, DWORD reason
, LPVOID param
)
1574 static LONG noop_thread_started
;
1575 static DWORD fls_index
= FLS_OUT_OF_INDEXES
;
1576 static int fls_count
= 0;
1577 static int thread_detach_count
= 0;
1580 ok(!inside_loader_lock
, "inside_loader_lock should not be set\n");
1581 ok(!inside_peb_lock
, "inside_peb_lock should not be set\n");
1585 case DLL_PROCESS_ATTACH
:
1586 trace("dll: %p, DLL_PROCESS_ATTACH, %p\n", hinst
, param
);
1588 ret
= pRtlDllShutdownInProgress();
1589 ok(!ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
1591 /* Set up the FLS slot, if FLS is available */
1596 ret
= pFlsAlloc(&fls_callback
);
1597 ok(ret
!= FLS_OUT_OF_INDEXES
, "FlsAlloc returned %d\n", ret
);
1599 SetLastError(0xdeadbeef);
1600 value
= pFlsGetValue(fls_index
);
1601 ok(!value
, "FlsGetValue returned %p, expected NULL\n", value
);
1602 ok(GetLastError() == ERROR_SUCCESS
, "FlsGetValue failed with error %u\n", GetLastError());
1603 bret
= pFlsSetValue(fls_index
, (void*) 0x31415);
1604 ok(bret
, "FlsSetValue failed\n");
1609 case DLL_PROCESS_DETACH
:
1611 DWORD code
, expected_code
, i
;
1612 HANDLE handle
, process
;
1615 LARGE_INTEGER offset
;
1618 trace("dll: %p, DLL_PROCESS_DETACH, %p\n", hinst
, param
);
1620 if (test_dll_phase
== 4 || test_dll_phase
== 5)
1622 ok(0, "dll_entry_point(DLL_PROCESS_DETACH) should not be called\n");
1626 /* The process should already deadlock at this point */
1627 if (test_dll_phase
== 6)
1629 /* In reality, code below never gets executed, probably some other
1630 * code tries to access process heap and deadlocks earlier, even XP
1631 * doesn't call the DLL entry point on process detach either.
1633 HeapLock(GetProcessHeap());
1634 ok(0, "dll_entry_point: process should already deadlock\n");
1638 if (test_dll_phase
== 0 || test_dll_phase
== 1 || test_dll_phase
== 3)
1639 ok(param
!= NULL
, "dll: param %p\n", param
);
1641 ok(!param
, "dll: param %p\n", param
);
1643 if (test_dll_phase
== 0 || test_dll_phase
== 1) expected_code
= 195;
1644 else if (test_dll_phase
== 3) expected_code
= 196;
1645 else expected_code
= STILL_ACTIVE
;
1647 if (test_dll_phase
== 3)
1649 ret
= pRtlDllShutdownInProgress();
1650 ok(ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
1654 ret
= pRtlDllShutdownInProgress();
1656 /* FIXME: remove once Wine is fixed */
1657 todo_wine_if (!(expected_code
== STILL_ACTIVE
|| expected_code
== 196))
1658 ok(!ret
|| broken(ret
) /* before Vista */, "RtlDllShutdownInProgress returned %d\n", ret
);
1661 /* In the case that the process is terminating, FLS slots should still be accessible, but
1662 * the callback should be already run for this thread and the contents already NULL.
1663 * Note that this is broken for Win2k3, which runs the callbacks *after* the DLL entry
1664 * point has already run.
1666 if (param
&& pFlsGetValue
)
1669 SetLastError(0xdeadbeef);
1670 value
= pFlsGetValue(fls_index
);
1673 ok(broken(value
== (void*) 0x31415) || /* Win2k3 */
1674 value
== NULL
, "FlsGetValue returned %p, expected NULL\n", value
);
1676 ok(GetLastError() == ERROR_SUCCESS
, "FlsGetValue failed with error %u\n", GetLastError());
1679 ok(broken(fls_callback_count
== thread_detach_count
) || /* Win2k3 */
1680 fls_callback_count
== thread_detach_count
+ 1,
1681 "wrong FLS callback count %d, expected %d\n", fls_callback_count
, thread_detach_count
+ 1);
1687 /* Call FlsFree now and run the remaining callbacks from uncleanly terminated threads */
1688 ret
= pFlsFree(fls_index
);
1689 ok(ret
, "FlsFree failed with error %u\n", GetLastError());
1690 fls_index
= FLS_OUT_OF_INDEXES
;
1693 ok(fls_callback_count
== fls_count
,
1694 "wrong FLS callback count %d, expected %d\n", fls_callback_count
, fls_count
);
1698 ok(attached_thread_count
>= 2, "attached thread count should be >= 2\n");
1700 for (i
= 0; i
< attached_thread_count
; i
++)
1702 /* Calling GetExitCodeThread() without waiting for thread termination
1703 * leads to different results due to a race condition.
1705 if (expected_code
!= STILL_ACTIVE
)
1707 ret
= WaitForSingleObject(attached_thread
[i
], 1000);
1708 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %#x\n", ret
);
1710 ret
= GetExitCodeThread(attached_thread
[i
], &code
);
1711 trace("dll: GetExitCodeThread(%u) => %d,%u\n", i
, ret
, code
);
1712 ok(ret
== 1, "GetExitCodeThread returned %d, expected 1\n", ret
);
1713 ok(code
== expected_code
, "expected thread exit code %u, got %u\n", expected_code
, code
);
1716 ret
= WaitForSingleObject(event
, 0);
1717 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
1719 ret
= WaitForSingleObject(mutex
, 0);
1720 if (expected_code
== STILL_ACTIVE
)
1721 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
1723 ok(ret
== WAIT_ABANDONED
, "expected WAIT_ABANDONED, got %#x\n", ret
);
1725 /* semaphore is not abandoned on thread termination */
1726 ret
= WaitForSingleObject(semaphore
, 0);
1727 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
1729 if (expected_code
== STILL_ACTIVE
)
1731 ret
= WaitForSingleObject(attached_thread
[0], 0);
1732 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
1733 ret
= WaitForSingleObject(attached_thread
[1], 0);
1734 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
1738 ret
= WaitForSingleObject(attached_thread
[0], 0);
1739 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %#x\n", ret
);
1740 ret
= WaitForSingleObject(attached_thread
[1], 0);
1741 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %#x\n", ret
);
1744 /* win7 doesn't allow creating a thread during process shutdown but
1745 * earlier Windows versions allow it.
1747 noop_thread_started
= 0;
1748 SetLastError(0xdeadbeef);
1749 handle
= CreateThread(NULL
, 0, noop_thread_proc
, &noop_thread_started
, 0, &ret
);
1752 ok(!handle
|| broken(handle
!= 0) /* before win7 */, "CreateThread should fail\n");
1754 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1757 ret
= WaitForSingleObject(handle
, 1000);
1758 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
1759 CloseHandle(handle
);
1764 ok(handle
!= 0, "CreateThread error %d\n", GetLastError());
1765 ret
= WaitForSingleObject(handle
, 1000);
1766 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
1767 ok(!noop_thread_started
|| broken(noop_thread_started
) /* XP64 */, "thread shouldn't start yet\n");
1768 CloseHandle(handle
);
1771 SetLastError(0xdeadbeef);
1772 process
= OpenProcess(PROCESS_ALL_ACCESS_NT4
, FALSE
, GetCurrentProcessId());
1773 ok(process
!= NULL
, "OpenProcess error %d\n", GetLastError());
1775 noop_thread_started
= 0;
1776 SetLastError(0xdeadbeef);
1777 handle
= CreateRemoteThread(process
, NULL
, 0, noop_thread_proc
, &noop_thread_started
, 0, &ret
);
1780 ok(!handle
|| broken(handle
!= 0) /* before win7 */, "CreateRemoteThread should fail\n");
1782 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1785 ret
= WaitForSingleObject(handle
, 1000);
1786 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
1787 CloseHandle(handle
);
1792 ok(handle
!= 0, "CreateRemoteThread error %d\n", GetLastError());
1793 ret
= WaitForSingleObject(handle
, 1000);
1794 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
1795 ok(!noop_thread_started
|| broken(noop_thread_started
) /* XP64 */, "thread shouldn't start yet\n");
1796 CloseHandle(handle
);
1799 SetLastError(0xdeadbeef);
1800 handle
= CreateFileMappingW(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
1801 ok(handle
!= 0, "CreateFileMapping error %d\n", GetLastError());
1803 offset
.u
.LowPart
= 0;
1804 offset
.u
.HighPart
= 0;
1807 ret
= pNtMapViewOfSection(handle
, process
, &addr
, 0, 0, &offset
,
1808 &size
, 1 /* ViewShare */, 0, PAGE_READONLY
);
1809 ok(ret
== STATUS_SUCCESS
, "NtMapViewOfSection error %#x\n", ret
);
1810 ret
= pNtUnmapViewOfSection(process
, addr
);
1811 ok(ret
== STATUS_SUCCESS
, "NtUnmapViewOfSection error %#x\n", ret
);
1813 CloseHandle(handle
);
1814 CloseHandle(process
);
1816 handle
= GetModuleHandleA("winver.exe");
1817 ok(!handle
, "winver.exe shouldn't be loaded yet\n");
1818 SetLastError(0xdeadbeef);
1819 handle
= LoadLibraryA("winver.exe");
1820 ok(handle
!= 0, "LoadLibrary error %d\n", GetLastError());
1821 SetLastError(0xdeadbeef);
1822 ret
= FreeLibrary(handle
);
1823 ok(ret
, "FreeLibrary error %d\n", GetLastError());
1824 handle
= GetModuleHandleA("winver.exe");
1826 ok(handle
!= 0, "winver.exe should not be unloaded\n");
1829 ok(!handle
|| broken(handle
!= 0) /* before win7 */, "winver.exe should be unloaded\n");
1831 SetLastError(0xdeadbeef);
1832 ret
= WaitForDebugEvent(&de
, 0);
1833 ok(!ret
, "WaitForDebugEvent should fail\n");
1835 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1837 SetLastError(0xdeadbeef);
1838 ret
= DebugActiveProcess(GetCurrentProcessId());
1839 ok(!ret
, "DebugActiveProcess should fail\n");
1840 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1842 SetLastError(0xdeadbeef);
1843 ret
= WaitForDebugEvent(&de
, 0);
1844 ok(!ret
, "WaitForDebugEvent should fail\n");
1845 ok(GetLastError() == ERROR_SEM_TIMEOUT
, "expected ERROR_SEM_TIMEOUT, got %d\n", GetLastError());
1847 if (test_dll_phase
== 2)
1849 trace("dll: call ExitProcess()\n");
1850 *child_failures
= winetest_get_failures();
1853 trace("dll: %p, DLL_PROCESS_DETACH, %p => DONE\n", hinst
, param
);
1856 case DLL_THREAD_ATTACH
:
1857 trace("dll: %p, DLL_THREAD_ATTACH, %p\n", hinst
, param
);
1859 ret
= pRtlDllShutdownInProgress();
1860 ok(!ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
1862 if (attached_thread_count
< MAX_COUNT
)
1864 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &attached_thread
[attached_thread_count
],
1865 0, TRUE
, DUPLICATE_SAME_ACCESS
);
1866 attached_thread_count
++;
1869 /* Make sure the FLS slot is empty, if FLS is available */
1874 SetLastError(0xdeadbeef);
1875 value
= pFlsGetValue(fls_index
);
1876 ok(!value
, "FlsGetValue returned %p, expected NULL\n", value
);
1878 ok(GetLastError() == ERROR_SUCCESS
, "FlsGetValue failed with error %u\n", GetLastError());
1879 ret
= pFlsSetValue(fls_index
, (void*) 0x31415);
1880 ok(ret
, "FlsSetValue failed\n");
1885 case DLL_THREAD_DETACH
:
1886 trace("dll: %p, DLL_THREAD_DETACH, %p\n", hinst
, param
);
1887 thread_detach_count
++;
1889 ret
= pRtlDllShutdownInProgress();
1890 /* win7 doesn't allow creating a thread during process shutdown but
1891 * earlier Windows versions allow it. In that case DLL_THREAD_DETACH is
1892 * sent on thread exit, but DLL_THREAD_ATTACH is never received.
1894 if (noop_thread_started
)
1895 ok(ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
1897 ok(!ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
1899 /* FLS data should already be destroyed, if FLS is available.
1900 * Note that this is broken for Win2k3, which runs the callbacks *after* the DLL entry
1901 * point has already run.
1903 if (pFlsGetValue
&& fls_index
!= FLS_OUT_OF_INDEXES
)
1906 SetLastError(0xdeadbeef);
1907 value
= pFlsGetValue(fls_index
);
1910 ok(broken(value
== (void*) 0x31415) || /* Win2k3 */
1911 !value
, "FlsGetValue returned %p, expected NULL\n", value
);
1913 ok(GetLastError() == ERROR_SUCCESS
, "FlsGetValue failed with error %u\n", GetLastError());
1918 trace("dll: %p, %d, %p\n", hinst
, reason
, param
);
1922 *child_failures
= winetest_get_failures();
1927 static void child_process(const char *dll_name
, DWORD target_offset
)
1930 DWORD ret
, dummy
, i
, code
, expected_code
;
1931 HANDLE file
, thread
, process
;
1933 struct PROCESS_BASIC_INFORMATION_PRIVATE pbi
;
1936 trace("phase %d: writing %p at %#x\n", test_dll_phase
, dll_entry_point
, target_offset
);
1938 SetLastError(0xdeadbeef);
1939 mutex
= CreateMutexW(NULL
, FALSE
, NULL
);
1940 ok(mutex
!= 0, "CreateMutex error %d\n", GetLastError());
1942 SetLastError(0xdeadbeef);
1943 semaphore
= CreateSemaphoreW(NULL
, 1, 1, NULL
);
1944 ok(semaphore
!= 0, "CreateSemaphore error %d\n", GetLastError());
1946 SetLastError(0xdeadbeef);
1947 event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1948 ok(event
!= 0, "CreateEvent error %d\n", GetLastError());
1950 SetLastError(0xdeadbeef);
1951 loader_lock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1952 ok(loader_lock_event
!= 0, "CreateEvent error %d\n", GetLastError());
1954 SetLastError(0xdeadbeef);
1955 peb_lock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1956 ok(peb_lock_event
!= 0, "CreateEvent error %d\n", GetLastError());
1958 SetLastError(0xdeadbeef);
1959 heap_lock_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1960 ok(heap_lock_event
!= 0, "CreateEvent error %d\n", GetLastError());
1962 SetLastError(0xdeadbeef);
1963 ack_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1964 ok(ack_event
!= 0, "CreateEvent error %d\n", GetLastError());
1966 file
= CreateFileA(dll_name
, GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
1967 if (file
== INVALID_HANDLE_VALUE
)
1969 ok(0, "could not open %s\n", dll_name
);
1972 SetFilePointer(file
, target_offset
, NULL
, FILE_BEGIN
);
1973 SetLastError(0xdeadbeef);
1974 target
= dll_entry_point
;
1975 ret
= WriteFile(file
, &target
, sizeof(target
), &dummy
, NULL
);
1976 ok(ret
, "WriteFile error %d\n", GetLastError());
1979 SetLastError(0xdeadbeef);
1980 hmod
= LoadLibraryA(dll_name
);
1981 ok(hmod
!= 0, "LoadLibrary error %d\n", GetLastError());
1983 SetLastError(0xdeadbeef);
1984 stop_event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1985 ok(stop_event
!= 0, "CreateEvent error %d\n", GetLastError());
1987 SetLastError(0xdeadbeef);
1988 thread
= CreateThread(NULL
, 0, mutex_thread_proc
, event
, 0, &dummy
);
1989 ok(thread
!= 0, "CreateThread error %d\n", GetLastError());
1990 WaitForSingleObject(event
, 3000);
1991 CloseHandle(thread
);
1995 SetLastError(0xdeadbeef);
1996 thread
= CreateThread(NULL
, 0, semaphore_thread_proc
, event
, 0, &dummy
);
1997 ok(thread
!= 0, "CreateThread error %d\n", GetLastError());
1998 WaitForSingleObject(event
, 3000);
1999 CloseHandle(thread
);
2004 ok(attached_thread_count
== 2, "attached thread count should be 2\n");
2005 for (i
= 0; i
< attached_thread_count
; i
++)
2007 ret
= GetExitCodeThread(attached_thread
[i
], &code
);
2008 trace("child: GetExitCodeThread(%u) => %d,%u\n", i
, ret
, code
);
2009 ok(ret
== 1, "GetExitCodeThread returned %d, expected 1\n", ret
);
2010 ok(code
== STILL_ACTIVE
, "expected thread exit code STILL_ACTIVE, got %u\n", code
);
2013 ret
= WaitForSingleObject(attached_thread
[0], 0);
2014 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
2015 ret
= WaitForSingleObject(attached_thread
[1], 0);
2016 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
2018 ret
= WaitForSingleObject(event
, 0);
2019 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
2020 ret
= WaitForSingleObject(mutex
, 0);
2021 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
2022 ret
= WaitForSingleObject(semaphore
, 0);
2023 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
2025 ret
= pRtlDllShutdownInProgress();
2026 ok(!ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
2028 SetLastError(0xdeadbeef);
2029 process
= OpenProcess(PROCESS_ALL_ACCESS_NT4
, FALSE
, GetCurrentProcessId());
2030 ok(process
!= NULL
, "OpenProcess error %d\n", GetLastError());
2032 SetLastError(0xdeadbeef);
2033 ret
= TerminateProcess(0, 195);
2034 ok(!ret
, "TerminateProcess(0) should fail\n");
2035 ok(GetLastError() == ERROR_INVALID_HANDLE
, "expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
2040 ret
= pNtSetInformationProcess(process
, ProcessAffinityMask
, &affinity
, sizeof(affinity
));
2041 ok(!ret
, "NtSetInformationProcess error %#x\n", ret
);
2043 switch (test_dll_phase
)
2046 ret
= pRtlDllShutdownInProgress();
2047 ok(!ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
2049 trace("call NtTerminateProcess(0, 195)\n");
2050 ret
= pNtTerminateProcess(0, 195);
2051 ok(!ret
, "NtTerminateProcess error %#x\n", ret
);
2053 memset(&pbi
, 0, sizeof(pbi
));
2054 ret
= pNtQueryInformationProcess(process
, ProcessBasicInformation
, &pbi
, sizeof(pbi
), NULL
);
2055 ok(!ret
, "NtQueryInformationProcess error %#x\n", ret
);
2056 ok(pbi
.ExitStatus
== STILL_ACTIVE
|| pbi
.ExitStatus
== 195,
2057 "expected STILL_ACTIVE, got %lu\n", pbi
.ExitStatus
);
2059 ret
= pNtSetInformationProcess(process
, ProcessAffinityMask
, &affinity
, sizeof(affinity
));
2060 ok(!ret
, "NtSetInformationProcess error %#x\n", ret
);
2062 ret
= pRtlDllShutdownInProgress();
2063 ok(!ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
2065 hmod
= GetModuleHandleA(dll_name
);
2066 ok(hmod
!= 0, "DLL should not be unloaded\n");
2068 SetLastError(0xdeadbeef);
2069 thread
= CreateThread(NULL
, 0, noop_thread_proc
, &dummy
, 0, &ret
);
2070 ok(!thread
|| broken(thread
!= 0) /* before win7 */, "CreateThread should fail\n");
2072 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2075 ret
= WaitForSingleObject(thread
, 1000);
2076 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %#x\n", ret
);
2077 CloseHandle(thread
);
2080 trace("call LdrShutdownProcess()\n");
2081 pLdrShutdownProcess();
2083 ret
= pRtlDllShutdownInProgress();
2084 ok(ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
2086 hmod
= GetModuleHandleA(dll_name
);
2087 ok(hmod
!= 0, "DLL should not be unloaded\n");
2089 memset(&pbi
, 0, sizeof(pbi
));
2090 ret
= pNtQueryInformationProcess(process
, ProcessBasicInformation
, &pbi
, sizeof(pbi
), NULL
);
2091 ok(!ret
, "NtQueryInformationProcess error %#x\n", ret
);
2092 ok(pbi
.ExitStatus
== STILL_ACTIVE
|| pbi
.ExitStatus
== 195,
2093 "expected STILL_ACTIVE, got %lu\n", pbi
.ExitStatus
);
2095 ret
= pNtSetInformationProcess(process
, ProcessAffinityMask
, &affinity
, sizeof(affinity
));
2096 ok(!ret
, "NtSetInformationProcess error %#x\n", ret
);
2099 case 1: /* normal ExitProcess */
2100 ret
= pRtlDllShutdownInProgress();
2101 ok(!ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
2104 case 2: /* ExitProcess will be called by the PROCESS_DETACH handler */
2105 ret
= pRtlDllShutdownInProgress();
2106 ok(!ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
2108 trace("call FreeLibrary(%p)\n", hmod
);
2109 SetLastError(0xdeadbeef);
2110 ret
= FreeLibrary(hmod
);
2111 ok(ret
, "FreeLibrary error %d\n", GetLastError());
2112 hmod
= GetModuleHandleA(dll_name
);
2113 ok(!hmod
, "DLL should be unloaded\n");
2115 if (test_dll_phase
== 2)
2116 ok(0, "FreeLibrary+ExitProcess should never return\n");
2118 ret
= pRtlDllShutdownInProgress();
2119 ok(!ret
, "RtlDllShutdownInProgress returned %d\n", ret
);
2124 trace("signalling thread exit\n");
2125 SetEvent(stop_event
);
2126 CloseHandle(stop_event
);
2130 trace("setting loader_lock_event\n");
2131 SetEvent(loader_lock_event
);
2132 WaitForSingleObject(ack_event
, 1000);
2133 ok(inside_loader_lock
!= 0, "inside_loader_lock is not set\n");
2135 /* calling NtTerminateProcess should not cause a deadlock */
2136 trace("call NtTerminateProcess(0, 198)\n");
2137 ret
= pNtTerminateProcess(0, 198);
2138 ok(!ret
, "NtTerminateProcess error %#x\n", ret
);
2140 *child_failures
= winetest_get_failures();
2142 /* Windows fails to release loader lock acquired from another thread,
2143 * so the LdrUnlockLoaderLock call fails here and ExitProcess deadlocks
2144 * later on, so NtTerminateProcess is used instead.
2146 trace("call NtTerminateProcess(GetCurrentProcess(), 198)\n");
2147 pNtTerminateProcess(GetCurrentProcess(), 198);
2148 ok(0, "NtTerminateProcess should not return\n");
2152 trace("setting peb_lock_event\n");
2153 SetEvent(peb_lock_event
);
2154 WaitForSingleObject(ack_event
, 1000);
2155 ok(inside_peb_lock
!= 0, "inside_peb_lock is not set\n");
2157 *child_failures
= winetest_get_failures();
2159 /* calling ExitProcess should cause a deadlock */
2160 trace("call ExitProcess(198)\n");
2162 ok(0, "ExitProcess should not return\n");
2166 trace("setting heap_lock_event\n");
2167 SetEvent(heap_lock_event
);
2168 WaitForSingleObject(ack_event
, 1000);
2169 ok(inside_heap_lock
!= 0, "inside_heap_lock is not set\n");
2171 *child_failures
= winetest_get_failures();
2173 /* calling ExitProcess should cause a deadlock */
2174 trace("call ExitProcess(1)\n");
2176 ok(0, "ExitProcess should not return\n");
2184 if (test_dll_phase
== 0) expected_code
= 195;
2185 else if (test_dll_phase
== 3) expected_code
= 196;
2186 else if (test_dll_phase
== 4) expected_code
= 198;
2187 else expected_code
= STILL_ACTIVE
;
2189 if (expected_code
== STILL_ACTIVE
)
2191 ret
= WaitForSingleObject(attached_thread
[0], 100);
2192 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
2193 ret
= WaitForSingleObject(attached_thread
[1], 100);
2194 ok(ret
== WAIT_TIMEOUT
, "expected WAIT_TIMEOUT, got %#x\n", ret
);
2198 ret
= WaitForSingleObject(attached_thread
[0], 1000);
2199 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %#x\n", ret
);
2200 ret
= WaitForSingleObject(attached_thread
[1], 1000);
2201 ok(ret
== WAIT_OBJECT_0
, "expected WAIT_OBJECT_0, got %#x\n", ret
);
2204 for (i
= 0; i
< attached_thread_count
; i
++)
2206 ret
= GetExitCodeThread(attached_thread
[i
], &code
);
2207 trace("child: GetExitCodeThread(%u) => %d,%u\n", i
, ret
, code
);
2208 ok(ret
== 1, "GetExitCodeThread returned %d, expected 1\n", ret
);
2209 ok(code
== expected_code
, "expected thread exit code %u, got %u\n", expected_code
, code
);
2212 *child_failures
= winetest_get_failures();
2214 trace("call ExitProcess(195)\n");
2218 static void test_ExitProcess(void)
2220 #include "pshpack1.h"
2222 static struct section_data
2227 } section_data
= { { 0x48,0xb8 }, dll_entry_point
, { 0xff,0xe0 } };
2229 static struct section_data
2234 } section_data
= { 0xb8, dll_entry_point
, { 0xff,0xe0 } };
2236 #include "poppack.h"
2237 DWORD dummy
, file_align
;
2238 HANDLE file
, thread
, process
, hmap
, hmap_dup
;
2239 char temp_path
[MAX_PATH
], dll_name
[MAX_PATH
], cmdline
[MAX_PATH
* 2];
2240 DWORD ret
, target_offset
, old_prot
;
2241 char **argv
, buf
[256];
2242 PROCESS_INFORMATION pi
;
2243 STARTUPINFOA si
= { sizeof(si
) };
2245 struct PROCESS_BASIC_INFORMATION_PRIVATE pbi
;
2246 MEMORY_BASIC_INFORMATION mbi
;
2249 LARGE_INTEGER offset
;
2251 IMAGE_NT_HEADERS nt_header
;
2253 #if !defined(__i386__) && !defined(__x86_64__)
2254 skip("x86 specific ExitProcess test\n");
2258 if (!pRtlDllShutdownInProgress
)
2260 win_skip("RtlDllShutdownInProgress is not available on this platform (XP+)\n");
2263 if (!pNtQueryInformationProcess
|| !pNtSetInformationProcess
)
2265 win_skip("NtQueryInformationProcess/NtSetInformationProcess are not available on this platform\n");
2268 if (!pNtAllocateVirtualMemory
|| !pNtFreeVirtualMemory
)
2270 win_skip("NtAllocateVirtualMemory/NtFreeVirtualMemory are not available on this platform\n");
2274 /* prevent displaying of the "Unable to load this DLL" message box */
2275 SetErrorMode(SEM_FAILCRITICALERRORS
);
2277 GetTempPathA(MAX_PATH
, temp_path
);
2278 GetTempFileNameA(temp_path
, "ldr", 0, dll_name
);
2280 /*trace("creating %s\n", dll_name);*/
2281 file
= CreateFileA(dll_name
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
2282 if (file
== INVALID_HANDLE_VALUE
)
2284 ok(0, "could not create %s\n", dll_name
);
2288 SetLastError(0xdeadbeef);
2289 ret
= WriteFile(file
, &dos_header
, sizeof(dos_header
), &dummy
, NULL
);
2290 ok(ret
, "WriteFile error %d\n", GetLastError());
2292 nt_header
= nt_header_template
;
2293 nt_header
.FileHeader
.NumberOfSections
= 1;
2294 nt_header
.FileHeader
.SizeOfOptionalHeader
= sizeof(IMAGE_OPTIONAL_HEADER
);
2295 nt_header
.FileHeader
.Characteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
| IMAGE_FILE_DLL
| IMAGE_FILE_RELOCS_STRIPPED
;
2297 nt_header
.OptionalHeader
.AddressOfEntryPoint
= 0x1000;
2298 nt_header
.OptionalHeader
.SectionAlignment
= 0x1000;
2299 nt_header
.OptionalHeader
.FileAlignment
= 0x200;
2300 nt_header
.OptionalHeader
.SizeOfImage
= sizeof(dos_header
) + sizeof(nt_header
) + sizeof(IMAGE_SECTION_HEADER
) + 0x1000;
2301 nt_header
.OptionalHeader
.SizeOfHeaders
= sizeof(dos_header
) + sizeof(nt_header
) + sizeof(IMAGE_SECTION_HEADER
);
2302 SetLastError(0xdeadbeef);
2303 ret
= WriteFile(file
, &nt_header
, sizeof(DWORD
) + sizeof(IMAGE_FILE_HEADER
), &dummy
, NULL
);
2304 ok(ret
, "WriteFile error %d\n", GetLastError());
2305 SetLastError(0xdeadbeef);
2306 ret
= WriteFile(file
, &nt_header
.OptionalHeader
, sizeof(IMAGE_OPTIONAL_HEADER
), &dummy
, NULL
);
2307 ok(ret
, "WriteFile error %d\n", GetLastError());
2309 section
.SizeOfRawData
= sizeof(section_data
);
2310 section
.PointerToRawData
= nt_header
.OptionalHeader
.FileAlignment
;
2311 section
.VirtualAddress
= nt_header
.OptionalHeader
.SectionAlignment
;
2312 section
.Misc
.VirtualSize
= sizeof(section_data
);
2313 section
.Characteristics
= IMAGE_SCN_CNT_CODE
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_EXECUTE
;
2314 SetLastError(0xdeadbeef);
2315 ret
= WriteFile(file
, §ion
, sizeof(section
), &dummy
, NULL
);
2316 ok(ret
, "WriteFile error %d\n", GetLastError());
2318 file_align
= nt_header
.OptionalHeader
.FileAlignment
- nt_header
.OptionalHeader
.SizeOfHeaders
;
2319 assert(file_align
< sizeof(filler
));
2320 SetLastError(0xdeadbeef);
2321 ret
= WriteFile(file
, filler
, file_align
, &dummy
, NULL
);
2322 ok(ret
, "WriteFile error %d\n", GetLastError());
2324 target_offset
= SetFilePointer(file
, 0, NULL
, FILE_CURRENT
) + FIELD_OFFSET(struct section_data
, target
);
2327 SetLastError(0xdeadbeef);
2328 ret
= WriteFile(file
, §ion_data
, sizeof(section_data
), &dummy
, NULL
);
2329 ok(ret
, "WriteFile error %d\n", GetLastError());
2333 winetest_get_mainargs(&argv
);
2336 *child_failures
= -1;
2337 sprintf(cmdline
, "\"%s\" loader %s %u 0", argv
[0], dll_name
, target_offset
);
2338 ret
= CreateProcessA(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
2339 ok(ret
, "CreateProcess(%s) error %d\n", cmdline
, GetLastError());
2340 ret
= WaitForSingleObject(pi
.hProcess
, 10000);
2341 ok(ret
== WAIT_OBJECT_0
, "child process failed to terminate\n");
2342 if (ret
!= WAIT_OBJECT_0
) TerminateProcess(pi
.hProcess
, 0);
2343 GetExitCodeProcess(pi
.hProcess
, &ret
);
2344 ok(ret
== 195, "expected exit code 195, got %u\n", ret
);
2345 if (*child_failures
)
2347 trace("%d failures in child process\n", *child_failures
);
2348 winetest_add_failures(*child_failures
);
2350 CloseHandle(pi
.hThread
);
2351 CloseHandle(pi
.hProcess
);
2354 *child_failures
= -1;
2355 sprintf(cmdline
, "\"%s\" loader %s %u 1", argv
[0], dll_name
, target_offset
);
2356 ret
= CreateProcessA(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
2357 ok(ret
, "CreateProcess(%s) error %d\n", cmdline
, GetLastError());
2358 ret
= WaitForSingleObject(pi
.hProcess
, 10000);
2359 ok(ret
== WAIT_OBJECT_0
, "child process failed to terminate\n");
2360 if (ret
!= WAIT_OBJECT_0
) TerminateProcess(pi
.hProcess
, 0);
2361 GetExitCodeProcess(pi
.hProcess
, &ret
);
2362 ok(ret
== 195, "expected exit code 195, got %u\n", ret
);
2363 if (*child_failures
)
2365 trace("%d failures in child process\n", *child_failures
);
2366 winetest_add_failures(*child_failures
);
2368 CloseHandle(pi
.hThread
);
2369 CloseHandle(pi
.hProcess
);
2372 *child_failures
= -1;
2373 sprintf(cmdline
, "\"%s\" loader %s %u 2", argv
[0], dll_name
, target_offset
);
2374 ret
= CreateProcessA(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
2375 ok(ret
, "CreateProcess(%s) error %d\n", cmdline
, GetLastError());
2376 ret
= WaitForSingleObject(pi
.hProcess
, 10000);
2377 ok(ret
== WAIT_OBJECT_0
, "child process failed to terminate\n");
2378 if (ret
!= WAIT_OBJECT_0
) TerminateProcess(pi
.hProcess
, 0);
2379 GetExitCodeProcess(pi
.hProcess
, &ret
);
2380 ok(ret
== 197, "expected exit code 197, got %u\n", ret
);
2381 if (*child_failures
)
2383 trace("%d failures in child process\n", *child_failures
);
2384 winetest_add_failures(*child_failures
);
2386 CloseHandle(pi
.hThread
);
2387 CloseHandle(pi
.hProcess
);
2390 *child_failures
= -1;
2391 sprintf(cmdline
, "\"%s\" loader %s %u 3", argv
[0], dll_name
, target_offset
);
2392 ret
= CreateProcessA(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
2393 ok(ret
, "CreateProcess(%s) error %d\n", cmdline
, GetLastError());
2394 ret
= WaitForSingleObject(pi
.hProcess
, 10000);
2395 ok(ret
== WAIT_OBJECT_0
, "child process failed to terminate\n");
2396 if (ret
!= WAIT_OBJECT_0
) TerminateProcess(pi
.hProcess
, 0);
2397 GetExitCodeProcess(pi
.hProcess
, &ret
);
2398 ok(ret
== 195, "expected exit code 195, got %u\n", ret
);
2399 if (*child_failures
)
2401 trace("%d failures in child process\n", *child_failures
);
2402 winetest_add_failures(*child_failures
);
2404 CloseHandle(pi
.hThread
);
2405 CloseHandle(pi
.hProcess
);
2408 if (pLdrLockLoaderLock
&& pLdrUnlockLoaderLock
)
2410 *child_failures
= -1;
2411 sprintf(cmdline
, "\"%s\" loader %s %u 4", argv
[0], dll_name
, target_offset
);
2412 ret
= CreateProcessA(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
2413 ok(ret
, "CreateProcess(%s) error %d\n", cmdline
, GetLastError());
2414 ret
= WaitForSingleObject(pi
.hProcess
, 10000);
2415 ok(ret
== WAIT_OBJECT_0
, "child process failed to terminate\n");
2416 if (ret
!= WAIT_OBJECT_0
) TerminateProcess(pi
.hProcess
, 0);
2417 GetExitCodeProcess(pi
.hProcess
, &ret
);
2418 ok(ret
== 198, "expected exit code 198, got %u\n", ret
);
2419 if (*child_failures
)
2421 trace("%d failures in child process\n", *child_failures
);
2422 winetest_add_failures(*child_failures
);
2424 CloseHandle(pi
.hThread
);
2425 CloseHandle(pi
.hProcess
);
2428 win_skip("LdrLockLoaderLock/LdrUnlockLoaderLock are not available on this platform\n");
2431 if (pRtlAcquirePebLock
&& pRtlReleasePebLock
)
2433 *child_failures
= -1;
2434 sprintf(cmdline
, "\"%s\" loader %s %u 5", argv
[0], dll_name
, target_offset
);
2435 ret
= CreateProcessA(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
2436 ok(ret
, "CreateProcess(%s) error %d\n", cmdline
, GetLastError());
2437 ret
= WaitForSingleObject(pi
.hProcess
, 5000);
2438 ok(ret
== WAIT_TIMEOUT
, "child process should fail to terminate\n");
2439 if (ret
!= WAIT_OBJECT_0
)
2441 trace("terminating child process\n");
2442 TerminateProcess(pi
.hProcess
, 199);
2444 ret
= WaitForSingleObject(pi
.hProcess
, 1000);
2445 ok(ret
== WAIT_OBJECT_0
, "child process failed to terminate\n");
2446 GetExitCodeProcess(pi
.hProcess
, &ret
);
2447 ok(ret
== 199, "expected exit code 199, got %u\n", ret
);
2448 if (*child_failures
)
2450 trace("%d failures in child process\n", *child_failures
);
2451 winetest_add_failures(*child_failures
);
2453 CloseHandle(pi
.hThread
);
2454 CloseHandle(pi
.hProcess
);
2457 win_skip("RtlAcquirePebLock/RtlReleasePebLock are not available on this platform\n");
2460 *child_failures
= -1;
2461 sprintf(cmdline
, "\"%s\" loader %s %u 6", argv
[0], dll_name
, target_offset
);
2462 ret
= CreateProcessA(argv
[0], cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
2463 ok(ret
, "CreateProcess(%s) error %d\n", cmdline
, GetLastError());
2464 ret
= WaitForSingleObject(pi
.hProcess
, 5000);
2465 ok(ret
== WAIT_TIMEOUT
|| broken(ret
== WAIT_OBJECT_0
) /* XP */, "child process should fail to terminate\n");
2466 if (ret
!= WAIT_OBJECT_0
)
2468 trace("terminating child process\n");
2469 TerminateProcess(pi
.hProcess
, 201);
2471 ret
= WaitForSingleObject(pi
.hProcess
, 1000);
2472 ok(ret
== WAIT_OBJECT_0
, "child process failed to terminate\n");
2473 GetExitCodeProcess(pi
.hProcess
, &ret
);
2474 ok(ret
== 201 || broken(ret
== 1) /* XP */, "expected exit code 201, got %u\n", ret
);
2475 if (*child_failures
)
2477 trace("%d failures in child process\n", *child_failures
);
2478 winetest_add_failures(*child_failures
);
2480 CloseHandle(pi
.hThread
);
2481 CloseHandle(pi
.hProcess
);
2483 /* test remote process termination */
2484 SetLastError(0xdeadbeef);
2485 ret
= CreateProcessA(argv
[0], NULL
, NULL
, NULL
, FALSE
, CREATE_SUSPENDED
, NULL
, NULL
, &si
, &pi
);
2486 ok(ret
, "CreateProcess(%s) error %d\n", argv
[0], GetLastError());
2488 SetLastError(0xdeadbeef);
2489 addr
= VirtualAllocEx(pi
.hProcess
, NULL
, 4096, MEM_COMMIT
, PAGE_READWRITE
);
2490 ok(addr
!= NULL
, "VirtualAllocEx error %d\n", GetLastError());
2491 SetLastError(0xdeadbeef);
2492 ret
= VirtualProtectEx(pi
.hProcess
, addr
, 4096, PAGE_READONLY
, &old_prot
);
2493 ok(ret
, "VirtualProtectEx error %d\n", GetLastError());
2494 ok(old_prot
== PAGE_READWRITE
, "expected PAGE_READWRITE, got %#x\n", old_prot
);
2495 SetLastError(0xdeadbeef);
2496 size
= VirtualQueryEx(pi
.hProcess
, NULL
, &mbi
, sizeof(mbi
));
2497 ok(size
== sizeof(mbi
), "VirtualQueryEx error %d\n", GetLastError());
2499 SetLastError(0xdeadbeef);
2500 ret
= ReadProcessMemory(pi
.hProcess
, addr
, buf
, 4, &size
);
2501 ok(ret
, "ReadProcessMemory error %d\n", GetLastError());
2502 ok(size
== 4, "expected 4, got %lu\n", size
);
2504 SetLastError(0xdeadbeef);
2505 hmap
= CreateFileMappingW(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, NULL
);
2506 ok(hmap
!= 0, "CreateFileMapping error %d\n", GetLastError());
2508 SetLastError(0xdeadbeef);
2509 ret
= DuplicateHandle(GetCurrentProcess(), hmap
, pi
.hProcess
, &hmap_dup
,
2510 0, FALSE
, DUPLICATE_SAME_ACCESS
);
2511 ok(ret
, "DuplicateHandle error %d\n", GetLastError());
2513 offset
.u
.LowPart
= 0;
2514 offset
.u
.HighPart
= 0;
2517 ret
= pNtMapViewOfSection(hmap
, pi
.hProcess
, &addr
, 0, 0, &offset
,
2518 &size
, 1 /* ViewShare */, 0, PAGE_READONLY
);
2519 ok(!ret
, "NtMapViewOfSection error %#x\n", ret
);
2520 ret
= pNtUnmapViewOfSection(pi
.hProcess
, addr
);
2521 ok(!ret
, "NtUnmapViewOfSection error %#x\n", ret
);
2523 SetLastError(0xdeadbeef);
2524 thread
= CreateRemoteThread(pi
.hProcess
, NULL
, 0, (void *)0xdeadbeef, NULL
, CREATE_SUSPENDED
, &ret
);
2525 ok(thread
!= 0, "CreateRemoteThread error %d\n", GetLastError());
2526 SetLastError(0xdeadbeef);
2527 ctx
.ContextFlags
= CONTEXT_INTEGER
;
2528 ret
= GetThreadContext(thread
, &ctx
);
2529 ok(ret
, "GetThreadContext error %d\n", GetLastError());
2530 SetLastError(0xdeadbeef);
2531 ctx
.ContextFlags
= CONTEXT_INTEGER
;
2532 ret
= SetThreadContext(thread
, &ctx
);
2533 ok(ret
, "SetThreadContext error %d\n", GetLastError());
2534 SetLastError(0xdeadbeef);
2535 ret
= SetThreadPriority(thread
, 0);
2536 ok(ret
, "SetThreadPriority error %d\n", GetLastError());
2538 SetLastError(0xdeadbeef);
2539 ret
= TerminateThread(thread
, 199);
2540 ok(ret
, "TerminateThread error %d\n", GetLastError());
2541 /* Calling GetExitCodeThread() without waiting for thread termination
2542 * leads to different results due to a race condition.
2544 ret
= WaitForSingleObject(thread
, 1000);
2545 ok(ret
== WAIT_OBJECT_0
, "WaitForSingleObject failed: %x\n", ret
);
2546 GetExitCodeThread(thread
, &ret
);
2547 ok(ret
== 199, "expected exit code 199, got %u\n", ret
);
2549 SetLastError(0xdeadbeef);
2550 ret
= TerminateProcess(pi
.hProcess
, 198);
2551 ok(ret
, "TerminateProcess error %d\n", GetLastError());
2552 /* Checking process state without waiting for process termination
2553 * leads to different results due to a race condition.
2555 ret
= WaitForSingleObject(pi
.hProcess
, 1000);
2556 ok(ret
== WAIT_OBJECT_0
, "WaitForSingleObject failed: %x\n", ret
);
2558 SetLastError(0xdeadbeef);
2559 process
= OpenProcess(PROCESS_ALL_ACCESS_NT4
, FALSE
, pi
.dwProcessId
);
2560 ok(process
!= NULL
, "OpenProcess error %d\n", GetLastError());
2561 CloseHandle(process
);
2563 memset(&pbi
, 0, sizeof(pbi
));
2564 ret
= pNtQueryInformationProcess(pi
.hProcess
, ProcessBasicInformation
, &pbi
, sizeof(pbi
), NULL
);
2565 ok(!ret
, "NtQueryInformationProcess error %#x\n", ret
);
2566 ok(pbi
.ExitStatus
== 198, "expected 198, got %lu\n", pbi
.ExitStatus
);
2568 ret
= pNtSetInformationProcess(pi
.hProcess
, ProcessAffinityMask
, &affinity
, sizeof(affinity
));
2569 ok(ret
== STATUS_PROCESS_IS_TERMINATING
, "expected STATUS_PROCESS_IS_TERMINATING, got %#x\n", ret
);
2571 SetLastError(0xdeadbeef);
2572 ctx
.ContextFlags
= CONTEXT_INTEGER
;
2573 ret
= GetThreadContext(thread
, &ctx
);
2574 ok(!ret
|| broken(ret
) /* XP 64-bit */, "GetThreadContext should fail\n");
2576 ok(GetLastError() == ERROR_INVALID_PARAMETER
||
2577 GetLastError() == ERROR_GEN_FAILURE
/* win7 64-bit */ ||
2578 GetLastError() == ERROR_INVALID_FUNCTION
/* vista 64-bit */,
2579 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2580 SetLastError(0xdeadbeef);
2581 ctx
.ContextFlags
= CONTEXT_INTEGER
;
2582 ret
= SetThreadContext(thread
, &ctx
);
2583 ok(!ret
|| broken(ret
) /* XP 64-bit */, "SetThreadContext should fail\n");
2585 ok(GetLastError() == ERROR_ACCESS_DENIED
||
2586 GetLastError() == ERROR_GEN_FAILURE
/* win7 64-bit */ ||
2587 GetLastError() == ERROR_INVALID_FUNCTION
/* vista 64-bit */,
2588 "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2589 SetLastError(0xdeadbeef);
2590 ret
= SetThreadPriority(thread
, 0);
2591 ok(ret
, "SetThreadPriority error %d\n", GetLastError());
2592 CloseHandle(thread
);
2594 SetLastError(0xdeadbeef);
2595 ctx
.ContextFlags
= CONTEXT_INTEGER
;
2596 ret
= GetThreadContext(pi
.hThread
, &ctx
);
2597 ok(!ret
|| broken(ret
) /* XP 64-bit */, "GetThreadContext should fail\n");
2599 ok(GetLastError() == ERROR_INVALID_PARAMETER
||
2600 GetLastError() == ERROR_GEN_FAILURE
/* win7 64-bit */ ||
2601 GetLastError() == ERROR_INVALID_FUNCTION
/* vista 64-bit */,
2602 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
2603 SetLastError(0xdeadbeef);
2604 ctx
.ContextFlags
= CONTEXT_INTEGER
;
2605 ret
= SetThreadContext(pi
.hThread
, &ctx
);
2606 ok(!ret
|| broken(ret
) /* XP 64-bit */, "SetThreadContext should fail\n");
2608 ok(GetLastError() == ERROR_ACCESS_DENIED
||
2609 GetLastError() == ERROR_GEN_FAILURE
/* win7 64-bit */ ||
2610 GetLastError() == ERROR_INVALID_FUNCTION
/* vista 64-bit */,
2611 "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2612 SetLastError(0xdeadbeef);
2613 ret
= VirtualProtectEx(pi
.hProcess
, addr
, 4096, PAGE_READWRITE
, &old_prot
);
2614 ok(!ret
, "VirtualProtectEx should fail\n");
2615 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2616 SetLastError(0xdeadbeef);
2618 ret
= ReadProcessMemory(pi
.hProcess
, addr
, buf
, 4, &size
);
2619 ok(!ret
, "ReadProcessMemory should fail\n");
2620 ok(GetLastError() == ERROR_PARTIAL_COPY
|| GetLastError() == ERROR_ACCESS_DENIED
,
2621 "expected ERROR_PARTIAL_COPY, got %d\n", GetLastError());
2622 ok(!size
, "expected 0, got %lu\n", size
);
2623 SetLastError(0xdeadbeef);
2624 ret
= VirtualFreeEx(pi
.hProcess
, addr
, 0, MEM_RELEASE
);
2625 ok(!ret
, "VirtualFreeEx should fail\n");
2626 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2627 SetLastError(0xdeadbeef);
2628 addr
= VirtualAllocEx(pi
.hProcess
, NULL
, 4096, MEM_COMMIT
, PAGE_READWRITE
);
2629 ok(!addr
, "VirtualAllocEx should fail\n");
2630 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2631 SetLastError(0xdeadbeef);
2632 size
= VirtualQueryEx(pi
.hProcess
, NULL
, &mbi
, sizeof(mbi
));
2633 ok(!size
, "VirtualQueryEx should fail\n");
2634 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2636 /* CloseHandle() call below leads to premature process termination
2637 * under some Windows versions.
2641 SetLastError(0xdeadbeef);
2642 ret
= CloseHandle(hmap_dup
);
2643 ok(ret
, "CloseHandle should not fail\n");
2646 SetLastError(0xdeadbeef);
2647 ret
= DuplicateHandle(GetCurrentProcess(), hmap
, pi
.hProcess
, &hmap_dup
,
2648 0, FALSE
, DUPLICATE_SAME_ACCESS
);
2649 ok(!ret
, "DuplicateHandle should fail\n");
2650 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2652 offset
.u
.LowPart
= 0;
2653 offset
.u
.HighPart
= 0;
2656 ret
= pNtMapViewOfSection(hmap
, pi
.hProcess
, &addr
, 0, 0, &offset
,
2657 &size
, 1 /* ViewShare */, 0, PAGE_READONLY
);
2658 ok(ret
== STATUS_PROCESS_IS_TERMINATING
, "expected STATUS_PROCESS_IS_TERMINATING, got %#x\n", ret
);
2660 SetLastError(0xdeadbeef);
2661 thread
= CreateRemoteThread(pi
.hProcess
, NULL
, 0, (void *)0xdeadbeef, NULL
, CREATE_SUSPENDED
, &ret
);
2662 ok(!thread
, "CreateRemoteThread should fail\n");
2663 ok(GetLastError() == ERROR_ACCESS_DENIED
, "expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2665 SetLastError(0xdeadbeef);
2666 ret
= DebugActiveProcess(pi
.dwProcessId
);
2667 ok(!ret
, "DebugActiveProcess should fail\n");
2668 ok(GetLastError() == ERROR_ACCESS_DENIED
/* 64-bit */ || GetLastError() == ERROR_NOT_SUPPORTED
/* 32-bit */,
2669 "ERROR_ACCESS_DENIED, got %d\n", GetLastError());
2671 GetExitCodeProcess(pi
.hProcess
, &ret
);
2672 ok(ret
== 198 || broken(ret
!= 198) /* some 32-bit XP version in a VM returns random exit code */,
2673 "expected exit code 198, got %u\n", ret
);
2674 CloseHandle(pi
.hThread
);
2675 CloseHandle(pi
.hProcess
);
2677 ret
= DeleteFileA(dll_name
);
2678 ok(ret
, "DeleteFile error %d\n", GetLastError());
2681 static PVOID WINAPI
failuredllhook(ULONG ul
, DELAYLOAD_INFO
* pd
)
2683 ok(ul
== 4, "expected 4, got %u\n", ul
);
2684 ok(!!pd
, "no delayload info supplied\n");
2687 ok(pd
->Size
== sizeof(*pd
), "got %u\n", pd
->Size
);
2688 ok(!!pd
->DelayloadDescriptor
, "no DelayloadDescriptor supplied\n");
2689 if (pd
->DelayloadDescriptor
)
2691 ok(pd
->DelayloadDescriptor
->Attributes
.AllAttributes
== 1,
2692 "expected 1, got %u\n", pd
->DelayloadDescriptor
->Attributes
.AllAttributes
);
2693 ok(pd
->DelayloadDescriptor
->DllNameRVA
== 0x2000,
2694 "expected 0x2000, got %x\n", pd
->DelayloadDescriptor
->DllNameRVA
);
2695 ok(pd
->DelayloadDescriptor
->ModuleHandleRVA
== 0x201a,
2696 "expected 0x201a, got %x\n", pd
->DelayloadDescriptor
->ModuleHandleRVA
);
2697 ok(pd
->DelayloadDescriptor
->ImportAddressTableRVA
> pd
->DelayloadDescriptor
->ModuleHandleRVA
,
2698 "expected %x > %x\n", pd
->DelayloadDescriptor
->ImportAddressTableRVA
,
2699 pd
->DelayloadDescriptor
->ModuleHandleRVA
);
2700 ok(pd
->DelayloadDescriptor
->ImportNameTableRVA
> pd
->DelayloadDescriptor
->ImportAddressTableRVA
,
2701 "expected %x > %x\n", pd
->DelayloadDescriptor
->ImportNameTableRVA
,
2702 pd
->DelayloadDescriptor
->ImportAddressTableRVA
);
2703 ok(pd
->DelayloadDescriptor
->BoundImportAddressTableRVA
== 0,
2704 "expected 0, got %x\n", pd
->DelayloadDescriptor
->BoundImportAddressTableRVA
);
2705 ok(pd
->DelayloadDescriptor
->UnloadInformationTableRVA
== 0,
2706 "expected 0, got %x\n", pd
->DelayloadDescriptor
->UnloadInformationTableRVA
);
2707 ok(pd
->DelayloadDescriptor
->TimeDateStamp
== 0,
2708 "expected 0, got %x\n", pd
->DelayloadDescriptor
->TimeDateStamp
);
2711 ok(!!pd
->ThunkAddress
, "no ThunkAddress supplied\n");
2712 if (pd
->ThunkAddress
)
2713 ok(pd
->ThunkAddress
->u1
.Ordinal
, "no ThunkAddress value supplied\n");
2715 ok(!!pd
->TargetDllName
, "no TargetDllName supplied\n");
2716 if (pd
->TargetDllName
)
2717 ok(!strcmp(pd
->TargetDllName
, "secur32.dll"),
2718 "expected \"secur32.dll\", got \"%s\"\n", pd
->TargetDllName
);
2720 ok(pd
->TargetApiDescriptor
.ImportDescribedByName
== 0,
2721 "expected 0, got %x\n", pd
->TargetApiDescriptor
.ImportDescribedByName
);
2722 ok(pd
->TargetApiDescriptor
.Description
.Ordinal
== 0 ||
2723 pd
->TargetApiDescriptor
.Description
.Ordinal
== 999,
2724 "expected 0, got %x\n", pd
->TargetApiDescriptor
.Description
.Ordinal
);
2726 ok(!!pd
->TargetModuleBase
, "no TargetModuleBase supplied\n");
2727 ok(pd
->Unused
== NULL
, "expected NULL, got %p\n", pd
->Unused
);
2728 ok(pd
->LastError
, "no LastError supplied\n");
2731 return (void*)0xdeadbeef;
2734 static void test_ResolveDelayLoadedAPI(void)
2736 static const char test_dll
[] = "secur32.dll";
2737 static const char test_func
[] = "SealMessage";
2738 char temp_path
[MAX_PATH
];
2739 char dll_name
[MAX_PATH
];
2740 IMAGE_DELAYLOAD_DESCRIPTOR idd
, *delaydir
;
2741 IMAGE_THUNK_DATA itd32
;
2744 DWORD dummy
, file_size
, i
;
2747 IMAGE_NT_HEADERS nt_header
;
2749 static const struct test_data
2760 FALSE
, IMAGE_ORDINAL_FLAG
| 2, TRUE
2763 FALSE
, IMAGE_ORDINAL_FLAG
| 5, TRUE
2766 FALSE
, IMAGE_ORDINAL_FLAG
| 0, FALSE
2769 FALSE
, IMAGE_ORDINAL_FLAG
| 999, FALSE
2773 if (!pResolveDelayLoadedAPI
)
2775 win_skip("ResolveDelayLoadedAPI is not available\n");
2779 if (0) /* crashes on native */
2781 SetLastError(0xdeadbeef);
2782 ok(!pResolveDelayLoadedAPI(NULL
, NULL
, NULL
, NULL
, NULL
, 0),
2783 "ResolveDelayLoadedAPI succeeded\n");
2784 ok(GetLastError() == 0xdeadbeef, "GetLastError changed to %x\n", GetLastError());
2787 SetLastError(0xdeadbeef);
2788 ok(!pResolveDelayLoadedAPI(NULL
, NULL
, failuredllhook
, NULL
, NULL
, 0),
2789 "ResolveDelayLoadedAPI succeeded\n");
2790 ok(GetLastError() == 0xdeadbeef, "GetLastError changed to %x\n", GetLastError());
2791 ok(cb_count
== 1, "Wrong callback count: %d\n", cb_count
);
2794 GetTempPathA(MAX_PATH
, temp_path
);
2795 GetTempFileNameA(temp_path
, "ldr", 0, dll_name
);
2796 trace("creating %s\n", dll_name
);
2797 hfile
= CreateFileA(dll_name
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
2798 if (hfile
== INVALID_HANDLE_VALUE
)
2800 ok(0, "could not create %s\n", dll_name
);
2804 SetLastError(0xdeadbeef);
2805 ret
= WriteFile(hfile
, &dos_header
, sizeof(dos_header
), &dummy
, NULL
);
2806 ok(ret
, "WriteFile error %d\n", GetLastError());
2808 nt_header
= nt_header_template
;
2809 nt_header
.FileHeader
.NumberOfSections
= 2;
2810 nt_header
.FileHeader
.SizeOfOptionalHeader
= sizeof(IMAGE_OPTIONAL_HEADER
);
2812 nt_header
.OptionalHeader
.SectionAlignment
= 0x1000;
2813 nt_header
.OptionalHeader
.FileAlignment
= 0x1000;
2814 nt_header
.OptionalHeader
.SizeOfImage
= sizeof(dos_header
) + sizeof(nt_header
) + sizeof(IMAGE_SECTION_HEADER
) + 0x2200;
2815 nt_header
.OptionalHeader
.SizeOfHeaders
= sizeof(dos_header
) + sizeof(nt_header
) + 2 * sizeof(IMAGE_SECTION_HEADER
);
2816 nt_header
.OptionalHeader
.NumberOfRvaAndSizes
= IMAGE_NUMBEROF_DIRECTORY_ENTRIES
;
2817 nt_header
.OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
].VirtualAddress
= 0x1000;
2818 nt_header
.OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
].Size
= 0x100;
2820 SetLastError(0xdeadbeef);
2821 ret
= WriteFile(hfile
, &nt_header
, sizeof(DWORD
) + sizeof(IMAGE_FILE_HEADER
), &dummy
, NULL
);
2822 ok(ret
, "WriteFile error %d\n", GetLastError());
2824 SetLastError(0xdeadbeef);
2825 ret
= WriteFile(hfile
, &nt_header
.OptionalHeader
, sizeof(IMAGE_OPTIONAL_HEADER
), &dummy
, NULL
);
2826 ok(ret
, "WriteFile error %d\n", GetLastError());
2829 section
.PointerToRawData
= nt_header
.OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
].VirtualAddress
;
2830 section
.VirtualAddress
= nt_header
.OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
].VirtualAddress
;
2831 section
.Misc
.VirtualSize
= 2 * sizeof(idd
);
2832 section
.SizeOfRawData
= section
.Misc
.VirtualSize
;
2833 section
.Characteristics
= IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
;
2834 SetLastError(0xdeadbeef);
2835 ret
= WriteFile(hfile
, §ion
, sizeof(section
), &dummy
, NULL
);
2836 ok(ret
, "WriteFile error %d\n", GetLastError());
2838 section
.PointerToRawData
= 0x2000;
2839 section
.VirtualAddress
= 0x2000;
2840 i
= sizeof(td
)/sizeof(td
[0]);
2841 section
.Misc
.VirtualSize
= sizeof(test_dll
) + sizeof(hint
) + sizeof(test_func
) + sizeof(HMODULE
) +
2842 2 * (i
+ 1) * sizeof(IMAGE_THUNK_DATA
);
2843 ok(section
.Misc
.VirtualSize
<= 0x1000, "Too much tests, add a new section!\n");
2844 section
.SizeOfRawData
= section
.Misc
.VirtualSize
;
2845 section
.Characteristics
= IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
;
2846 SetLastError(0xdeadbeef);
2847 ret
= WriteFile(hfile
, §ion
, sizeof(section
), &dummy
, NULL
);
2848 ok(ret
, "WriteFile error %d\n", GetLastError());
2850 /* fill up to delay data */
2851 SetFilePointer( hfile
, nt_header
.OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
].VirtualAddress
, NULL
, SEEK_SET
);
2854 idd
.Attributes
.AllAttributes
= 1;
2855 idd
.DllNameRVA
= 0x2000;
2856 idd
.ModuleHandleRVA
= idd
.DllNameRVA
+ sizeof(test_dll
) + sizeof(hint
) + sizeof(test_func
);
2857 idd
.ImportAddressTableRVA
= idd
.ModuleHandleRVA
+ sizeof(HMODULE
);
2858 idd
.ImportNameTableRVA
= idd
.ImportAddressTableRVA
+ (i
+ 1) * sizeof(IMAGE_THUNK_DATA
);
2859 idd
.BoundImportAddressTableRVA
= 0;
2860 idd
.UnloadInformationTableRVA
= 0;
2861 idd
.TimeDateStamp
= 0;
2863 SetLastError(0xdeadbeef);
2864 ret
= WriteFile(hfile
, &idd
, sizeof(idd
), &dummy
, NULL
);
2865 ok(ret
, "WriteFile error %d\n", GetLastError());
2867 SetLastError(0xdeadbeef);
2868 ret
= WriteFile(hfile
, filler
, sizeof(idd
), &dummy
, NULL
);
2869 ok(ret
, "WriteFile error %d\n", GetLastError());
2871 /* fill up to extended delay data */
2872 SetFilePointer( hfile
, idd
.DllNameRVA
, NULL
, SEEK_SET
);
2874 /* extended delay data */
2875 SetLastError(0xdeadbeef);
2876 ret
= WriteFile(hfile
, test_dll
, sizeof(test_dll
), &dummy
, NULL
);
2877 ok(ret
, "WriteFile error %d\n", GetLastError());
2879 SetLastError(0xdeadbeef);
2880 ret
= WriteFile(hfile
, &hint
, sizeof(hint
), &dummy
, NULL
);
2881 ok(ret
, "WriteFile error %d\n", GetLastError());
2883 SetLastError(0xdeadbeef);
2884 ret
= WriteFile(hfile
, test_func
, sizeof(test_func
), &dummy
, NULL
);
2885 ok(ret
, "WriteFile error %d\n", GetLastError());
2887 SetFilePointer( hfile
, idd
.ImportAddressTableRVA
, NULL
, SEEK_SET
);
2889 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
2891 /* 0x1a00 is an empty space between delay data and extended delay data, real thunks are not necessary */
2892 itd32
.u1
.Function
= nt_header
.OptionalHeader
.ImageBase
+ 0x1a00 + i
* 0x20;
2893 SetLastError(0xdeadbeef);
2894 ret
= WriteFile(hfile
, &itd32
, sizeof(itd32
), &dummy
, NULL
);
2895 ok(ret
, "WriteFile error %d\n", GetLastError());
2898 itd32
.u1
.Function
= 0;
2899 SetLastError(0xdeadbeef);
2900 ret
= WriteFile(hfile
, &itd32
, sizeof(itd32
), &dummy
, NULL
);
2901 ok(ret
, "WriteFile error %d\n", GetLastError());
2903 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
2906 itd32
.u1
.AddressOfData
= idd
.DllNameRVA
+ sizeof(test_dll
);
2908 itd32
.u1
.Ordinal
= td
[i
].ordinal
;
2909 SetLastError(0xdeadbeef);
2910 ret
= WriteFile(hfile
, &itd32
, sizeof(itd32
), &dummy
, NULL
);
2911 ok(ret
, "WriteFile error %d\n", GetLastError());
2914 itd32
.u1
.Ordinal
= 0;
2915 SetLastError(0xdeadbeef);
2916 ret
= WriteFile(hfile
, &itd32
, sizeof(itd32
), &dummy
, NULL
);
2917 ok(ret
, "WriteFile error %d\n", GetLastError());
2919 /* fill up to eof */
2920 SetFilePointer( hfile
, section
.VirtualAddress
+ section
.Misc
.VirtualSize
, NULL
, SEEK_SET
);
2921 SetEndOfFile( hfile
);
2924 SetLastError(0xdeadbeef);
2925 hlib
= LoadLibraryA(dll_name
);
2926 ok(hlib
!= NULL
, "LoadLibrary error %u\n", GetLastError());
2929 skip("couldn't load %s.\n", dll_name
);
2930 DeleteFileA(dll_name
);
2934 delaydir
= pRtlImageDirectoryEntryToData(hlib
, TRUE
, IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
, &file_size
);
2937 skip("haven't found section for delay import directory.\n");
2939 DeleteFileA(dll_name
);
2945 IMAGE_THUNK_DATA
*itdn
, *itda
;
2948 if (!delaydir
->DllNameRVA
||
2949 !delaydir
->ImportAddressTableRVA
||
2950 !delaydir
->ImportNameTableRVA
) break;
2952 itdn
= RVAToAddr(delaydir
->ImportNameTableRVA
, hlib
);
2953 itda
= RVAToAddr(delaydir
->ImportAddressTableRVA
, hlib
);
2954 htarget
= LoadLibraryA(RVAToAddr(delaydir
->DllNameRVA
, hlib
));
2956 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++)
2960 if (IMAGE_SNAP_BY_ORDINAL(itdn
[i
].u1
.Ordinal
))
2961 load
= (void *)GetProcAddress(htarget
, (LPSTR
)IMAGE_ORDINAL(itdn
[i
].u1
.Ordinal
));
2964 const IMAGE_IMPORT_BY_NAME
* iibn
= RVAToAddr(itdn
[i
].u1
.AddressOfData
, hlib
);
2965 load
= (void *)GetProcAddress(htarget
, (char*)iibn
->Name
);
2969 ret
= pResolveDelayLoadedAPI(hlib
, delaydir
, failuredllhook
, NULL
, &itda
[i
], 0);
2972 ok(ret
!= NULL
, "Test %u: ResolveDelayLoadedAPI failed\n", i
);
2973 ok(ret
== load
, "Test %u: expected %p, got %p\n", i
, load
, ret
);
2974 ok(ret
== (void*)itda
[i
].u1
.AddressOfData
, "Test %u: expected %p, got %p\n",
2975 i
, ret
, (void*)itda
[i
].u1
.AddressOfData
);
2976 ok(!cb_count
, "Test %u: Wrong callback count: %d\n", i
, cb_count
);
2980 ok(ret
== (void*)0xdeadbeef, "Test %u: ResolveDelayLoadedAPI succeeded with %p\n", i
, ret
);
2981 ok(cb_count
, "Test %u: Wrong callback count: %d\n", i
, cb_count
);
2988 trace("deleting %s\n", dll_name
);
2989 DeleteFileA(dll_name
);
2992 static void test_InMemoryOrderModuleList(void)
2994 LIST_ENTRY
*entry1
, *mark1
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2995 LIST_ENTRY
*entry2
, *mark2
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
2996 LDR_MODULE
*module1
, *module2
;
2998 for (entry1
= mark1
->Flink
, entry2
= mark2
->Flink
;
2999 entry1
!= mark1
&& entry2
!= mark2
;
3000 entry1
= entry1
->Flink
, entry2
= entry2
->Flink
)
3002 module1
= CONTAINING_RECORD(entry1
, LDR_MODULE
, InLoadOrderModuleList
);
3003 module2
= CONTAINING_RECORD(entry2
, LDR_MODULE
, InMemoryOrderModuleList
);
3004 ok(module1
== module2
, "expected module1 == module2, got %p and %p\n", module1
, module2
);
3006 ok(entry1
== mark1
, "expected entry1 == mark1, got %p and %p\n", entry1
, mark1
);
3007 ok(entry2
== mark2
, "expected entry2 == mark2, got %p and %p\n", entry2
, mark2
);
3014 HANDLE ntdll
, mapping
, kernel32
;
3017 ntdll
= GetModuleHandleA("ntdll.dll");
3018 kernel32
= GetModuleHandleA("kernel32.dll");
3019 pNtCreateSection
= (void *)GetProcAddress(ntdll
, "NtCreateSection");
3020 pNtQuerySection
= (void *)GetProcAddress(ntdll
, "NtQuerySection");
3021 pNtMapViewOfSection
= (void *)GetProcAddress(ntdll
, "NtMapViewOfSection");
3022 pNtUnmapViewOfSection
= (void *)GetProcAddress(ntdll
, "NtUnmapViewOfSection");
3023 pNtTerminateProcess
= (void *)GetProcAddress(ntdll
, "NtTerminateProcess");
3024 pNtQueryInformationProcess
= (void *)GetProcAddress(ntdll
, "NtQueryInformationProcess");
3025 pNtSetInformationProcess
= (void *)GetProcAddress(ntdll
, "NtSetInformationProcess");
3026 pLdrShutdownProcess
= (void *)GetProcAddress(ntdll
, "LdrShutdownProcess");
3027 pRtlDllShutdownInProgress
= (void *)GetProcAddress(ntdll
, "RtlDllShutdownInProgress");
3028 pNtAllocateVirtualMemory
= (void *)GetProcAddress(ntdll
, "NtAllocateVirtualMemory");
3029 pNtFreeVirtualMemory
= (void *)GetProcAddress(ntdll
, "NtFreeVirtualMemory");
3030 pLdrLockLoaderLock
= (void *)GetProcAddress(ntdll
, "LdrLockLoaderLock");
3031 pLdrUnlockLoaderLock
= (void *)GetProcAddress(ntdll
, "LdrUnlockLoaderLock");
3032 pRtlAcquirePebLock
= (void *)GetProcAddress(ntdll
, "RtlAcquirePebLock");
3033 pRtlReleasePebLock
= (void *)GetProcAddress(ntdll
, "RtlReleasePebLock");
3034 pRtlImageDirectoryEntryToData
= (void *)GetProcAddress(ntdll
, "RtlImageDirectoryEntryToData");
3035 pFlsAlloc
= (void *)GetProcAddress(kernel32
, "FlsAlloc");
3036 pFlsSetValue
= (void *)GetProcAddress(kernel32
, "FlsSetValue");
3037 pFlsGetValue
= (void *)GetProcAddress(kernel32
, "FlsGetValue");
3038 pFlsFree
= (void *)GetProcAddress(kernel32
, "FlsFree");
3039 pResolveDelayLoadedAPI
= (void *)GetProcAddress(kernel32
, "ResolveDelayLoadedAPI");
3041 GetSystemInfo( &si
);
3042 page_size
= si
.dwPageSize
;
3043 dos_header
.e_magic
= IMAGE_DOS_SIGNATURE
;
3044 dos_header
.e_lfanew
= sizeof(dos_header
);
3046 mapping
= CreateFileMappingA(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
, 0, 4096, "winetest_loader");
3047 ok(mapping
!= 0, "CreateFileMapping failed\n");
3048 child_failures
= MapViewOfFile(mapping
, FILE_MAP_READ
|FILE_MAP_WRITE
, 0, 0, 4096);
3049 if (*child_failures
== -1)
3051 *child_failures
= 0;
3054 *child_failures
= -1;
3056 argc
= winetest_get_mainargs(&argv
);
3059 test_dll_phase
= atoi(argv
[4]);
3060 child_process(argv
[2], atol(argv
[3]));
3065 test_ResolveDelayLoadedAPI();
3066 test_ImportDescriptors();
3067 test_section_access();
3068 test_import_resolution();
3070 test_InMemoryOrderModuleList();