2 * Unit test suite for the PE loader.
4 * Copyright 2006 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
26 #include "wine/test.h"
28 #define ALIGN_SIZE(size, alignment) (((size) + (alignment - 1)) & ~((alignment - 1)))
32 WORD e_magic; /* 00: MZ Header signature */
34 DWORD e_lfanew; /* 3c: Offset to extended header */
37 IMAGE_DOS_SIGNATURE, { 0 }, sizeof(dos_header)
40 static IMAGE_NT_HEADERS nt_header =
42 IMAGE_NT_SIGNATURE, /* Signature */
44 { IMAGE_FILE_MACHINE_I386, /* Machine */
46 # error You must specify the machine type
48 1, /* NumberOfSections */
49 0, /* TimeDateStamp */
50 0, /* PointerToSymbolTable */
51 0, /* NumberOfSymbols */
52 sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
53 IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
55 { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
56 1, /* MajorLinkerVersion */
57 0, /* MinorLinkerVersion */
59 0, /* SizeOfInitializedData */
60 0, /* SizeOfUninitializedData */
61 0, /* AddressOfEntryPoint */
62 0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
64 0x10000000, /* ImageBase */
65 0, /* SectionAlignment */
66 0, /* FileAlignment */
67 4, /* MajorOperatingSystemVersion */
68 0, /* MinorOperatingSystemVersion */
69 1, /* MajorImageVersion */
70 0, /* MinorImageVersion */
71 4, /* MajorSubsystemVersion */
72 0, /* MinorSubsystemVersion */
73 0, /* Win32VersionValue */
74 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000, /* SizeOfImage */
75 sizeof(dos_header) + sizeof(nt_header), /* SizeOfHeaders */
77 IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
78 0, /* DllCharacteristics */
79 0, /* SizeOfStackReserve */
80 0, /* SizeOfStackCommit */
81 0, /* SizeOfHeapReserve */
82 0, /* SizeOfHeapCommit */
84 0, /* NumberOfRvaAndSizes */
85 { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
89 static IMAGE_SECTION_HEADER section =
93 0, /* VirtualAddress */
94 0x0a, /* SizeOfRawData */
95 0, /* PointerToRawData */
96 0, /* PointerToRelocations */
97 0, /* PointerToLinenumbers */
98 0, /* NumberOfRelocations */
99 0, /* NumberOfLinenumbers */
100 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, /* Characteristics */
105 static const struct test_data
107 const void *dos_header;
108 DWORD size_of_dos_header;
109 WORD number_of_sections, size_of_optional_header;
110 DWORD section_alignment, file_alignment;
111 DWORD size_of_image, size_of_headers;
112 DWORD error; /* 0 means LoadLibrary should succeed */
115 { &dos_header, sizeof(dos_header),
119 { &dos_header, sizeof(dos_header),
120 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
121 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0xe00,
122 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
123 ERROR_BAD_EXE_FORMAT /* XP doesn't like too small image size */
125 { &dos_header, sizeof(dos_header),
126 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
127 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
128 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
131 { &dos_header, sizeof(dos_header),
132 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
137 { &dos_header, sizeof(dos_header),
138 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x200,
139 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
140 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
143 { &dos_header, sizeof(dos_header),
144 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x1000,
145 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
146 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
147 ERROR_BAD_EXE_FORMAT /* XP doesn't like aligments */
149 { &dos_header, sizeof(dos_header),
150 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
151 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
152 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
155 { &dos_header, sizeof(dos_header),
156 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
157 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
161 /* Mandatory are all fields up to SizeOfHeaders, everything else
162 * is really optional (at least that's true for XP).
164 { &dos_header, sizeof(dos_header),
165 1, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
166 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER) + 0x10,
167 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER),
170 { &dos_header, sizeof(dos_header),
171 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
172 0xd0, /* beyond of the end of file */
173 0xc0, /* beyond of the end of file */
176 { &dos_header, sizeof(dos_header),
177 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
182 { &dos_header, sizeof(dos_header),
183 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
188 #if 0 /* not power of 2 alignments need more test cases */
189 { &dos_header, sizeof(dos_header),
190 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x300, 0x300,
193 ERROR_BAD_EXE_FORMAT /* alignment is not power of 2 */
196 { &dos_header, sizeof(dos_header),
197 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 4, 4,
202 { &dos_header, sizeof(dos_header),
203 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 1, 1,
208 { &dos_header, sizeof(dos_header),
209 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
212 ERROR_BAD_EXE_FORMAT /* image size == 0 -> failure */
214 /* the following data mimics the PE image which upack creates */
216 1, 0x148, 0x1000, 0x200,
217 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
221 /* Minimal PE image that XP is able to load: 92 bytes */
223 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum),
224 0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
230 static const char filler[0x1000];
231 static const char section_data[0x10] = "section data";
233 DWORD dummy, file_size, file_align;
235 HMODULE hlib, hlib_as_data_file;
237 char temp_path[MAX_PATH];
238 char dll_name[MAX_PATH];
241 trace("system page size 0x%04x\n", si.dwPageSize);
243 /* prevent displaying of the "Unable to load this DLL" message box */
244 SetErrorMode(SEM_FAILCRITICALERRORS);
246 GetTempPath(MAX_PATH, temp_path);
248 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
250 GetTempFileName(temp_path, "ldr", 0, dll_name);
252 /*trace("creating %s\n", dll_name);*/
253 hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
254 if (hfile == INVALID_HANDLE_VALUE)
256 ok(0, "could not create %s\n", dll_name);
260 SetLastError(0xdeadbeef);
261 ok(WriteFile(hfile, td[i].dos_header, td[i].size_of_dos_header, &dummy, NULL),
262 "WriteFile error %d\n", GetLastError());
264 nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
265 nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
267 nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
268 nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
269 nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
270 nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
271 SetLastError(0xdeadbeef);
272 ok(WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL),
273 "WriteFile error %d\n", GetLastError());
275 if (nt_header.FileHeader.SizeOfOptionalHeader)
277 SetLastError(0xdeadbeef);
278 ok(WriteFile(hfile, &nt_header.OptionalHeader,
279 min(nt_header.FileHeader.SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER)),
281 "WriteFile error %d\n", GetLastError());
282 if (nt_header.FileHeader.SizeOfOptionalHeader > sizeof(IMAGE_OPTIONAL_HEADER))
284 file_align = nt_header.FileHeader.SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER);
285 assert(file_align < sizeof(filler));
286 SetLastError(0xdeadbeef);
287 ok(WriteFile(hfile, filler, file_align, &dummy, NULL),
288 "WriteFile error %d\n", GetLastError());
292 assert(nt_header.FileHeader.NumberOfSections <= 1);
293 if (nt_header.FileHeader.NumberOfSections)
295 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
297 section.PointerToRawData = td[i].size_of_dos_header;
298 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
299 section.Misc.VirtualSize = section.SizeOfRawData * 10;
303 section.PointerToRawData = nt_header.OptionalHeader.SizeOfHeaders;
304 section.VirtualAddress = nt_header.OptionalHeader.SizeOfHeaders;
305 section.Misc.VirtualSize = 5;
308 SetLastError(0xdeadbeef);
309 ok(WriteFile(hfile, §ion, sizeof(section), &dummy, NULL),
310 "WriteFile error %d\n", GetLastError());
313 SetLastError(0xdeadbeef);
314 ok(WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL),
315 "WriteFile error %d\n", GetLastError());
318 file_size = GetFileSize(hfile, NULL);
321 SetLastError(0xdeadbeef);
322 hlib = LoadLibrary(dll_name);
323 if (td[i].error == ERROR_SUCCESS)
325 MEMORY_BASIC_INFORMATION info;
327 ok(hlib != 0, "%d: LoadLibrary error %d\n", i, GetLastError());
329 SetLastError(0xdeadbeef);
330 ok(VirtualQuery(hlib, &info, sizeof(info)) == sizeof(info),
331 "%d: VirtualQuery error %d\n", i, GetLastError());
332 ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
333 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
334 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
335 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
336 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
337 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
338 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
339 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
341 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
342 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
344 SetLastError(0xdeadbeef);
345 ok(VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info)) == sizeof(info),
346 "%d: VirtualQuery error %d\n", i, GetLastError());
347 if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize ||
348 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
350 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %p != expected %p\n",
351 i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
352 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
353 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
354 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
355 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
356 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
357 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
361 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
362 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
363 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
364 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
365 ok(info.RegionSize == ALIGN_SIZE(file_size, si.dwPageSize), "%d: got %lx != expected %x\n",
366 i, info.RegionSize, ALIGN_SIZE(file_size, si.dwPageSize));
367 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
368 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
369 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
372 /* header: check the zeroing of alignment */
373 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
378 start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
379 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
380 ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
383 if (nt_header.FileHeader.NumberOfSections)
385 SetLastError(0xdeadbeef);
386 ok(VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info)) == sizeof(info),
387 "%d: VirtualQuery error %d\n", i, GetLastError());
388 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
390 ok(info.BaseAddress == (char *)hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib);
391 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
392 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
393 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
397 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
398 ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize), "%d: got %lx != expected %x\n",
399 i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize));
400 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
402 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
403 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
404 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
405 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
407 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
408 ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
410 ok(!memcmp((const char *)hlib + section.PointerToRawData, section_data, section.SizeOfRawData), "wrong section data\n");
412 /* check the zeroing of alignment */
413 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
418 start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
419 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
420 ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
424 SetLastError(0xdeadbeef);
425 hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
426 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
427 ok(hlib_as_data_file == hlib, "hlib_as_file and hlib are different\n");
429 SetLastError(0xdeadbeef);
430 ok(FreeLibrary(hlib), "FreeLibrary error %d\n", GetLastError());
432 SetLastError(0xdeadbeef);
433 hlib = GetModuleHandle(dll_name);
434 ok(hlib != 0, "GetModuleHandle error %u\n", GetLastError());
436 SetLastError(0xdeadbeef);
437 ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
439 hlib = GetModuleHandle(dll_name);
440 ok(!hlib, "GetModuleHandle should fail\n");
442 SetLastError(0xdeadbeef);
443 hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
444 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
445 ok((ULONG_PTR)hlib_as_data_file & 1, "hlib_as_data_file is even\n");
447 hlib = GetModuleHandle(dll_name);
448 ok(!hlib, "GetModuleHandle should fail\n");
450 SetLastError(0xdeadbeef);
451 ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
454 { /* LoadLibrary is expected to fail */
455 ok(!hlib, "%d: LoadLibrary should fail\n", i);
457 if (GetLastError() == ERROR_GEN_FAILURE) /* Win9x, broken behaviour */
459 trace("skipping the loader test on Win9x\n");
460 DeleteFile(dll_name);
464 ok(td[i].error == GetLastError(), "%d: expected error %d, got %d\n",
465 i, td[i].error, GetLastError());
468 SetLastError(0xdeadbeef);
469 ok(DeleteFile(dll_name), "DeleteFile error %d\n", GetLastError());