kernel32: Add tests for ImpersonateNamedPipeClient.
[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       0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
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     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 */
101 };
102
103 START_TEST(loader)
104 {
105     static const struct test_data
106     {
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 */
113     } td[] =
114     {
115         { &dos_header, sizeof(dos_header),
116           1, 0, 0, 0, 0, 0,
117           ERROR_BAD_EXE_FORMAT
118         },
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 */
124         },
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),
129           ERROR_SUCCESS
130         },
131         { &dos_header, sizeof(dos_header),
132           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
133           0x1f00,
134           0x1000,
135           ERROR_SUCCESS
136         },
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),
141           ERROR_SUCCESS
142         },
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 */
148         },
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),
153           ERROR_SUCCESS
154         },
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,
158           0x200,
159           ERROR_SUCCESS
160         },
161         /* Mandatory are all fields up to SizeOfHeaders, everything else
162          * is really optional (at least that's true for XP).
163          */
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),
168           ERROR_SUCCESS
169         },
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 */
174           ERROR_SUCCESS
175         },
176         { &dos_header, sizeof(dos_header),
177           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
178           0x1000,
179           0,
180           ERROR_SUCCESS
181         },
182         { &dos_header, sizeof(dos_header),
183           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
184           1,
185           0,
186           ERROR_SUCCESS
187         },
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,
191           1,
192           0,
193           ERROR_BAD_EXE_FORMAT /* alignment is not power of 2 */
194         },
195 #endif
196         { &dos_header, sizeof(dos_header),
197           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 4, 4,
198           1,
199           0,
200           ERROR_SUCCESS
201         },
202         { &dos_header, sizeof(dos_header),
203           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 1, 1,
204           1,
205           0,
206           ERROR_SUCCESS
207         },
208         { &dos_header, sizeof(dos_header),
209           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
210           0,
211           0,
212           ERROR_BAD_EXE_FORMAT /* image size == 0 -> failure */
213         },
214         /* the following data mimics the PE image which upack creates */
215         { &dos_header, 0x10,
216           1, 0x148, 0x1000, 0x200,
217           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
218           0x200,
219           ERROR_SUCCESS
220         },
221         /* Minimal PE image that XP is able to load: 92 bytes */
222         { &dos_header, 0x04,
223           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum),
224           0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
225           1,
226           0,
227           ERROR_SUCCESS
228         }
229     };
230     static const char filler[0x1000];
231     static const char section_data[0x10] = "section data";
232     int i;
233     DWORD dummy, file_size, file_align;
234     HANDLE hfile, hlib;
235     SYSTEM_INFO si;
236     char temp_path[MAX_PATH];
237     char dll_name[MAX_PATH];
238
239     GetSystemInfo(&si);
240     trace("system page size 0x%04x\n", si.dwPageSize);
241
242     /* prevent displaying of the "Unable to load this DLL" message box */
243     SetErrorMode(SEM_FAILCRITICALERRORS);
244
245     GetTempPath(MAX_PATH, temp_path);
246
247     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
248     {
249         GetTempFileName(temp_path, "ldr", 0, dll_name);
250
251         /*trace("creating %s\n", dll_name);*/
252         hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
253         if (hfile == INVALID_HANDLE_VALUE)
254         {
255             ok(0, "could not create %s\n", dll_name);
256             break;
257         }
258
259         SetLastError(0xdeadbeef);
260         ok(WriteFile(hfile, td[i].dos_header, td[i].size_of_dos_header, &dummy, NULL),
261            "WriteFile error %d\n", GetLastError());
262
263         nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
264         nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
265
266         nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
267         nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
268         nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
269         nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
270         SetLastError(0xdeadbeef);
271         ok(WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL),
272            "WriteFile error %d\n", GetLastError());
273
274         if (nt_header.FileHeader.SizeOfOptionalHeader)
275         {
276             SetLastError(0xdeadbeef);
277             ok(WriteFile(hfile, &nt_header.OptionalHeader,
278                          min(nt_header.FileHeader.SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER)),
279                          &dummy, NULL),
280                "WriteFile error %d\n", GetLastError());
281             if (nt_header.FileHeader.SizeOfOptionalHeader > sizeof(IMAGE_OPTIONAL_HEADER))
282             {
283                 file_align = nt_header.FileHeader.SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER);
284                 assert(file_align < sizeof(filler));
285                 SetLastError(0xdeadbeef);
286                 ok(WriteFile(hfile, filler, file_align, &dummy, NULL),
287                    "WriteFile error %d\n", GetLastError());
288             }
289         }
290
291         assert(nt_header.FileHeader.NumberOfSections <= 1);
292         if (nt_header.FileHeader.NumberOfSections)
293         {
294             if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
295             {
296                 section.PointerToRawData = td[i].size_of_dos_header;
297                 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
298                 section.Misc.VirtualSize = section.SizeOfRawData * 10;
299             }
300             else
301             {
302                 section.PointerToRawData = nt_header.OptionalHeader.SizeOfHeaders;
303                 section.VirtualAddress = nt_header.OptionalHeader.SizeOfHeaders;
304                 section.Misc.VirtualSize = 5;
305             }
306
307             SetLastError(0xdeadbeef);
308             ok(WriteFile(hfile, &section, sizeof(section), &dummy, NULL),
309                "WriteFile error %d\n", GetLastError());
310
311             /* section data */
312             SetLastError(0xdeadbeef);
313             ok(WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL),
314                "WriteFile error %d\n", GetLastError());
315         }
316
317         file_size = GetFileSize(hfile, NULL);
318         CloseHandle(hfile);
319
320         SetLastError(0xdeadbeef);
321         hlib = LoadLibrary(dll_name);
322         if (td[i].error == ERROR_SUCCESS)
323         {
324             MEMORY_BASIC_INFORMATION info;
325
326             ok(hlib != 0, "%d: LoadLibrary error %d\n", i, GetLastError());
327
328             SetLastError(0xdeadbeef);
329             ok(VirtualQuery(hlib, &info, sizeof(info)) == sizeof(info),
330                 "%d: VirtualQuery error %d\n", i, GetLastError());
331             ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
332             ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
333             ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
334             ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
335                i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
336             ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
337             if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
338                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
339             else
340                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
341             ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
342
343             SetLastError(0xdeadbeef);
344             ok(VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info)) == sizeof(info),
345                 "%d: VirtualQuery error %d\n", i, GetLastError());
346             if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize ||
347                 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
348             {
349                 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %p != expected %p\n",
350                    i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
351                 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
352                 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
353                 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
354                 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
355                 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
356                 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
357             }
358             else
359             {
360                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
361                 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
362                 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
363                 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
364                 ok(info.RegionSize == ALIGN_SIZE(file_size, si.dwPageSize), "%d: got %lx != expected %x\n",
365                    i, info.RegionSize, ALIGN_SIZE(file_size, si.dwPageSize));
366                 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
367                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
368                 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
369             }
370
371             /* header: check the zeroing of alignment */
372             if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
373             {
374                 const char *start;
375                 int size;
376
377                 start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
378                 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
379                 ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
380             }
381
382             if (nt_header.FileHeader.NumberOfSections)
383             {
384                 SetLastError(0xdeadbeef);
385                 ok(VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info)) == sizeof(info),
386                     "%d: VirtualQuery error %d\n", i, GetLastError());
387                 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
388                 {
389                     ok(info.BaseAddress == (char *)hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib);
390                     ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
391                        i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
392                     ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
393                 }
394                 else
395                 {
396                     ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
397                     ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize), "%d: got %lx != expected %x\n",
398                        i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize));
399                     ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
400                 }
401                 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
402                 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
403                 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
404                 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
405
406                 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
407                     ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
408                 else
409                     ok(!memcmp((const char *)hlib + section.PointerToRawData, section_data, section.SizeOfRawData), "wrong section data\n");
410
411                 /* check the zeroing of alignment */
412                 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
413                 {
414                     const char *start;
415                     int size;
416
417                     start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
418                     size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
419                     ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
420                 }
421             }
422
423             SetLastError(0xdeadbeef);
424             ok(FreeLibrary(hlib), "FreeLibrary error %d\n", GetLastError());
425         }
426         else
427         {   /* LoadLibrary is expected to fail */
428             ok(!hlib, "%d: LoadLibrary should fail\n", i);
429
430             if (GetLastError() == ERROR_GEN_FAILURE) /* Win9x, broken behaviour */
431             {
432                 trace("skipping the loader test on Win9x\n");
433                 DeleteFile(dll_name);
434                 return;
435             }
436
437             ok(td[i].error == GetLastError(), "%d: expected error %d, got %d\n",
438                i, td[i].error, GetLastError());
439         }
440
441         SetLastError(0xdeadbeef);
442         ok(DeleteFile(dll_name), "DeleteFile error %d\n", GetLastError());
443     }
444 }