ntdll: Fix the failing loader tests.
[wine] / dlls / kernel32 / tests / loader.c
1 /*
2  * Unit test suite for the PE loader.
3  *
4  * Copyright 2006 Dmitry Timoshkov
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <assert.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/test.h"
27
28 #define ALIGN_SIZE(size, alignment) (((size) + (alignment - 1)) & ~((alignment - 1)))
29
30 static const struct
31 {
32     WORD e_magic;      /* 00: MZ Header signature */
33     WORD unused[29];
34     DWORD e_lfanew;    /* 3c: Offset to extended header */
35 } dos_header =
36 {
37     IMAGE_DOS_SIGNATURE, { 0 }, sizeof(dos_header)
38 };
39
40 static IMAGE_NT_HEADERS nt_header =
41 {
42     IMAGE_NT_SIGNATURE, /* Signature */
43 #ifdef __i386__
44     { IMAGE_FILE_MACHINE_I386, /* Machine */
45 #else
46 # error You must specify the machine type
47 #endif
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 */
54     },
55     { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
56       1, /* MajorLinkerVersion */
57       0, /* MinorLinkerVersion */
58       0, /* SizeOfCode */
59       0, /* SizeOfInitializedData */
60       0, /* SizeOfUninitializedData */
61       0, /* AddressOfEntryPoint */
62       0, /* BaseOfCode */
63       0, /* BaseOfData */
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 */
76       0, /* CheckSum */
77       IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
78       0, /* DllCharacteristics */
79       0, /* SizeOfStackReserve */
80       0, /* SizeOfStackCommit */
81       0, /* SizeOfHeapReserve */
82       0, /* SizeOfHeapCommit */
83       0, /* LoaderFlags */
84       0, /* NumberOfRvaAndSizes */
85       { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
86     }
87 };
88
89 static IMAGE_SECTION_HEADER section =
90 {
91     ".rodata", /* Name */
92     { 0x10 }, /* Misc */
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 */
101 };
102
103 START_TEST(loader)
104 {
105     static const struct test_data
106     {
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 */
111     } td[] =
112     {
113         { 1, 0, 0, 0, 0, 0,
114           ERROR_BAD_EXE_FORMAT
115         },
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 */
120         },
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),
124           ERROR_SUCCESS
125         },
126         { 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
127           0x1f00,
128           0x1000,
129           ERROR_SUCCESS
130         },
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),
134           ERROR_SUCCESS
135         },
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 */
140         },
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),
144           ERROR_SUCCESS
145         },
146         { 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
147           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
148           0x200,
149           ERROR_SUCCESS
150         },
151         /* Mandatory are all fields up to SizeOfHeaders, everything else
152          * is really optional (at least that's true for XP).
153          */
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),
157           ERROR_SUCCESS
158         },
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 */
162           ERROR_SUCCESS
163         },
164         { 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
165           0x1000,
166           0,
167           ERROR_SUCCESS
168         },
169         { 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
170           1,
171           0,
172           ERROR_SUCCESS
173         },
174         { 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
175           0,
176           0,
177           ERROR_BAD_EXE_FORMAT /* image size == 0 -> failure */
178         }
179     };
180     static const char filler[0x1000];
181     int i;
182     DWORD dummy, file_size, file_align;
183     HANDLE hfile, hlib;
184     SYSTEM_INFO si;
185     char temp_path[MAX_PATH];
186     char dll_name[MAX_PATH];
187
188     GetSystemInfo(&si);
189     trace("system page size 0x%04x\n", si.dwPageSize);
190
191     /* prevent displaying of the "Unable to load this DLL" message box */
192     SetErrorMode(SEM_FAILCRITICALERRORS);
193
194     GetTempPath(MAX_PATH, temp_path);
195     GetTempFileName(temp_path, "ldr", 0, dll_name);
196
197     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
198     {
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)
202         {
203             ok(0, "could not create %s\n", dll_name);
204             break;
205         }
206
207         SetLastError(0xdeadbeef);
208         ok(WriteFile(hfile, &dos_header, sizeof(dos_header), &dummy, NULL),
209            "WriteFile error %d\n", GetLastError());
210
211         nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
212         nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
213
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());
221
222         if (nt_header.FileHeader.SizeOfOptionalHeader)
223         {
224             assert(nt_header.FileHeader.SizeOfOptionalHeader <= sizeof(IMAGE_OPTIONAL_HEADER));
225
226             SetLastError(0xdeadbeef);
227             ok(WriteFile(hfile, &nt_header.OptionalHeader, nt_header.FileHeader.SizeOfOptionalHeader, &dummy, NULL),
228                "WriteFile error %d\n", GetLastError());
229         }
230
231         assert(nt_header.FileHeader.NumberOfSections <= 1);
232         if (nt_header.FileHeader.NumberOfSections)
233         {
234             if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize)
235             {
236                 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
237                 section.PointerToRawData = nt_header.OptionalHeader.FileAlignment;
238                 SetLastError(0xdeadbeef);
239                 ok(WriteFile(hfile, &section, sizeof(section), &dummy, NULL),
240                    "WriteFile error %d\n", GetLastError());
241
242                 file_size = GetFileSize(hfile, NULL);
243
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());
248             }
249             else
250             {
251                 section.VirtualAddress = nt_header.OptionalHeader.SizeOfHeaders;
252                 section.PointerToRawData = nt_header.OptionalHeader.SizeOfHeaders;
253                 SetLastError(0xdeadbeef);
254                 ok(WriteFile(hfile, &section, sizeof(section), &dummy, NULL),
255                    "WriteFile error %d\n", GetLastError());
256             }
257
258             /* section data */
259             SetLastError(0xdeadbeef);
260             ok(WriteFile(hfile, filler, 0x10, &dummy, NULL),
261                "WriteFile error %d\n", GetLastError());
262         }
263
264         CloseHandle(hfile);
265
266         SetLastError(0xdeadbeef);
267         hlib = LoadLibrary(dll_name);
268         if (td[i].error == ERROR_SUCCESS)
269         {
270             MEMORY_BASIC_INFORMATION info;
271
272             ok(hlib != 0, "%d: LoadLibrary error %d\n", i, GetLastError());
273
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);
285             else
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);
288
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)
294             {
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);
303             }
304             else
305             {
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);
314             }
315
316             SetLastError(0xdeadbeef);
317             ok(FreeLibrary(hlib), "FreeLibrary error %d\n", GetLastError());
318         }
319         else
320         {   /* LoadLibrary has failed */
321             ok(!hlib, "%d: LoadLibrary should fail\n", i);
322
323             if (GetLastError() == ERROR_GEN_FAILURE) /* Win9x, broken behaviour */
324             {
325                 trace("skipping the loader test on Win9x\n");
326                 DeleteFile(dll_name);
327                 return;
328             }
329
330             ok(td[i].error == GetLastError(), "%d: expected error %d, got %d\n",
331                i, td[i].error, GetLastError());
332         }
333
334         SetLastError(0xdeadbeef);
335         ok(DeleteFile(dll_name), "DeleteFile error %d\n", GetLastError());
336     }
337 }