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 */
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 0x10, /* 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 WORD number_of_sections, size_of_optional_header;
108 DWORD section_alignment, file_alignment;
109 DWORD size_of_image, size_of_headers;
110 DWORD error; /* 0 means LoadLibrary should succeed */
116 { 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
117 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0xe00,
118 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
119 ERROR_BAD_EXE_FORMAT /* XP doesn't like too small image size */
121 { 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
122 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
123 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
126 { 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
131 { 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x200,
132 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
133 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
136 { 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x1000,
137 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
138 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
139 ERROR_BAD_EXE_FORMAT /* XP doesn't like aligments */
141 { 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
142 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
143 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
146 { 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
147 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
151 /* Mandatory are all fields up to SizeOfHeaders, everything else
152 * is really optional (at least that's true for XP).
154 { 1, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
155 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER) + 0x10,
156 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER),
159 { 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
160 0xd0, /* beyond of the end of file */
161 0xc0, /* beyond of the end of file */
164 { 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
169 { 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
174 { 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
177 ERROR_BAD_EXE_FORMAT /* image size == 0 -> failure */
180 static const char filler[0x1000];
182 DWORD dummy, file_size, file_align;
185 char temp_path[MAX_PATH];
186 char dll_name[MAX_PATH];
189 trace("system page size 0x%04x\n", si.dwPageSize);
191 /* prevent displaying of the "Unable to load this DLL" message box */
192 SetErrorMode(SEM_FAILCRITICALERRORS);
194 GetTempPath(MAX_PATH, temp_path);
195 GetTempFileName(temp_path, "ldr", 0, dll_name);
197 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
199 /*trace("creating %s\n", dll_name);*/
200 hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
201 if (hfile == INVALID_HANDLE_VALUE)
203 ok(0, "could not create %s\n", dll_name);
207 SetLastError(0xdeadbeef);
208 ok(WriteFile(hfile, &dos_header, sizeof(dos_header), &dummy, NULL),
209 "WriteFile error %d\n", GetLastError());
211 nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
212 nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
214 nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
215 nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
216 nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
217 nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
218 SetLastError(0xdeadbeef);
219 ok(WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL),
220 "WriteFile error %d\n", GetLastError());
222 if (nt_header.FileHeader.SizeOfOptionalHeader)
224 assert(nt_header.FileHeader.SizeOfOptionalHeader <= sizeof(IMAGE_OPTIONAL_HEADER));
226 SetLastError(0xdeadbeef);
227 ok(WriteFile(hfile, &nt_header.OptionalHeader, nt_header.FileHeader.SizeOfOptionalHeader, &dummy, NULL),
228 "WriteFile error %d\n", GetLastError());
231 assert(nt_header.FileHeader.NumberOfSections <= 1);
232 if (nt_header.FileHeader.NumberOfSections)
234 if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize)
236 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
237 section.PointerToRawData = nt_header.OptionalHeader.FileAlignment;
238 SetLastError(0xdeadbeef);
239 ok(WriteFile(hfile, §ion, sizeof(section), &dummy, NULL),
240 "WriteFile error %d\n", GetLastError());
242 file_size = GetFileSize(hfile, NULL);
244 file_align = ALIGN_SIZE(file_size, nt_header.OptionalHeader.FileAlignment) - file_size;
245 SetLastError(0xdeadbeef);
246 ok(WriteFile(hfile, filler, file_align, &dummy, NULL),
247 "WriteFile error %d\n", GetLastError());
251 section.VirtualAddress = nt_header.OptionalHeader.SizeOfHeaders;
252 section.PointerToRawData = nt_header.OptionalHeader.SizeOfHeaders;
253 SetLastError(0xdeadbeef);
254 ok(WriteFile(hfile, §ion, sizeof(section), &dummy, NULL),
255 "WriteFile error %d\n", GetLastError());
259 SetLastError(0xdeadbeef);
260 ok(WriteFile(hfile, filler, 0x10, &dummy, NULL),
261 "WriteFile error %d\n", GetLastError());
266 SetLastError(0xdeadbeef);
267 hlib = LoadLibrary(dll_name);
268 if (td[i].error == ERROR_SUCCESS)
270 MEMORY_BASIC_INFORMATION info;
272 ok(hlib != 0, "%d: LoadLibrary error %d\n", i, GetLastError());
274 SetLastError(0xdeadbeef);
275 ok(VirtualQuery(hlib, &info, sizeof(info)) == sizeof(info),
276 "%d: VirtualQuery error %d\n", i, GetLastError());
277 ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
278 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
279 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
280 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
281 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
282 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
283 if (nt_header.OptionalHeader.SectionAlignment != si.dwPageSize)
284 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
286 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
287 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
289 SetLastError(0xdeadbeef);
290 ok(VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info)) == sizeof(info),
291 "%d: VirtualQuery error %d\n", i, GetLastError());
292 if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize ||
293 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
295 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: %p != %p\n",
296 i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
297 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
298 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
299 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
300 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
301 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
302 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
306 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
307 ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
308 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
309 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
310 ok(info.RegionSize == 0x2000, "%d: %lx != 0x2000\n", i, info.RegionSize);
311 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
312 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
313 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
316 SetLastError(0xdeadbeef);
317 ok(FreeLibrary(hlib), "FreeLibrary error %d\n", GetLastError());
320 { /* LoadLibrary has failed */
321 ok(!hlib, "%d: LoadLibrary should fail\n", i);
323 if (GetLastError() == ERROR_GEN_FAILURE) /* Win9x, broken behaviour */
325 trace("skipping the loader test on Win9x\n");
326 DeleteFile(dll_name);
330 ok(td[i].error == GetLastError(), "%d: expected error %d, got %d\n",
331 i, td[i].error, GetLastError());
334 SetLastError(0xdeadbeef);
335 ok(DeleteFile(dll_name), "DeleteFile error %d\n", GetLastError());