opengl32: Avoid generating a wrapper for internal functions when we can call the...
[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), 4, 4,
175           1,
176           0,
177           ERROR_SUCCESS
178         },
179         { 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 1, 1,
180           1,
181           0,
182           ERROR_SUCCESS
183         },
184         { 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
185           0,
186           0,
187           ERROR_BAD_EXE_FORMAT /* image size == 0 -> failure */
188         }
189     };
190     static const char filler[0x1000];
191     int i;
192     DWORD dummy, file_size, file_align;
193     HANDLE hfile, hlib;
194     SYSTEM_INFO si;
195     char temp_path[MAX_PATH];
196     char dll_name[MAX_PATH];
197
198     GetSystemInfo(&si);
199     trace("system page size 0x%04x\n", si.dwPageSize);
200
201     /* prevent displaying of the "Unable to load this DLL" message box */
202     SetErrorMode(SEM_FAILCRITICALERRORS);
203
204     GetTempPath(MAX_PATH, temp_path);
205
206     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
207     {
208         GetTempFileName(temp_path, "ldr", 0, dll_name);
209
210         /*trace("creating %s\n", dll_name);*/
211         hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
212         if (hfile == INVALID_HANDLE_VALUE)
213         {
214             ok(0, "could not create %s\n", dll_name);
215             break;
216         }
217
218         SetLastError(0xdeadbeef);
219         ok(WriteFile(hfile, &dos_header, sizeof(dos_header), &dummy, NULL),
220            "WriteFile error %d\n", GetLastError());
221
222         nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
223         nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
224
225         nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
226         nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
227         nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
228         nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
229         SetLastError(0xdeadbeef);
230         ok(WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL),
231            "WriteFile error %d\n", GetLastError());
232
233         if (nt_header.FileHeader.SizeOfOptionalHeader)
234         {
235             assert(nt_header.FileHeader.SizeOfOptionalHeader <= sizeof(IMAGE_OPTIONAL_HEADER));
236
237             SetLastError(0xdeadbeef);
238             ok(WriteFile(hfile, &nt_header.OptionalHeader, nt_header.FileHeader.SizeOfOptionalHeader, &dummy, NULL),
239                "WriteFile error %d\n", GetLastError());
240         }
241
242         assert(nt_header.FileHeader.NumberOfSections <= 1);
243         if (nt_header.FileHeader.NumberOfSections)
244         {
245             if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize)
246             {
247                 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
248                 section.PointerToRawData = nt_header.OptionalHeader.FileAlignment;
249                 SetLastError(0xdeadbeef);
250                 ok(WriteFile(hfile, &section, sizeof(section), &dummy, NULL),
251                    "WriteFile error %d\n", GetLastError());
252
253                 file_size = GetFileSize(hfile, NULL);
254
255                 file_align = ALIGN_SIZE(file_size, nt_header.OptionalHeader.FileAlignment) - file_size;
256                 SetLastError(0xdeadbeef);
257                 ok(WriteFile(hfile, filler, file_align, &dummy, NULL),
258                    "WriteFile error %d\n", GetLastError());
259             }
260             else
261             {
262                 section.VirtualAddress = nt_header.OptionalHeader.SizeOfHeaders;
263                 section.PointerToRawData = nt_header.OptionalHeader.SizeOfHeaders;
264                 SetLastError(0xdeadbeef);
265                 ok(WriteFile(hfile, &section, sizeof(section), &dummy, NULL),
266                    "WriteFile error %d\n", GetLastError());
267             }
268
269             /* section data */
270             SetLastError(0xdeadbeef);
271             ok(WriteFile(hfile, filler, 0x10, &dummy, NULL),
272                "WriteFile error %d\n", GetLastError());
273         }
274
275         CloseHandle(hfile);
276
277         SetLastError(0xdeadbeef);
278         hlib = LoadLibrary(dll_name);
279         if (td[i].error == ERROR_SUCCESS)
280         {
281             MEMORY_BASIC_INFORMATION info;
282
283             ok(hlib != 0, "%d: LoadLibrary error %d\n", i, GetLastError());
284
285             SetLastError(0xdeadbeef);
286             ok(VirtualQuery(hlib, &info, sizeof(info)) == sizeof(info),
287                 "%d: VirtualQuery error %d\n", i, GetLastError());
288             ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
289             ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
290             ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
291             ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
292                i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
293             ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
294             if (nt_header.OptionalHeader.SectionAlignment != si.dwPageSize)
295                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
296             else
297                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
298             ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
299
300             SetLastError(0xdeadbeef);
301             ok(VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info)) == sizeof(info),
302                 "%d: VirtualQuery error %d\n", i, GetLastError());
303             if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize ||
304                 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
305             {
306                 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: %p != %p\n",
307                    i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
308                 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
309                 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
310                 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
311                 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
312                 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
313                 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
314             }
315             else
316             {
317                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
318                 ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
319                 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
320                 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
321                 ok(info.RegionSize == 0x2000, "%d: %lx != 0x2000\n", i, info.RegionSize);
322                 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
323                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
324                 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
325             }
326
327             SetLastError(0xdeadbeef);
328             ok(FreeLibrary(hlib), "FreeLibrary error %d\n", GetLastError());
329         }
330         else
331         {   /* LoadLibrary has failed */
332             ok(!hlib, "%d: LoadLibrary should fail\n", i);
333
334             if (GetLastError() == ERROR_GEN_FAILURE) /* Win9x, broken behaviour */
335             {
336                 trace("skipping the loader test on Win9x\n");
337                 DeleteFile(dll_name);
338                 return;
339             }
340
341             ok(td[i].error == GetLastError(), "%d: expected error %d, got %d\n",
342                i, td[i].error, GetLastError());
343         }
344
345         SetLastError(0xdeadbeef);
346         ok(DeleteFile(dll_name), "DeleteFile error %d\n", GetLastError());
347     }
348 }