kernel32/tests: Pass test on error code returned by Windows 7.
[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 PVOID RVAToAddr(DWORD_PTR rva, HMODULE module)
31 {
32     if (rva == 0)
33         return NULL;
34     return ((char*) module) + rva;
35 }
36
37 static const struct
38 {
39     WORD e_magic;      /* 00: MZ Header signature */
40     WORD unused[29];
41     DWORD e_lfanew;    /* 3c: Offset to extended header */
42 } dos_header =
43 {
44     IMAGE_DOS_SIGNATURE, { 0 }, sizeof(dos_header)
45 };
46
47 static IMAGE_NT_HEADERS nt_header =
48 {
49     IMAGE_NT_SIGNATURE, /* Signature */
50     {
51 #if defined __i386__
52       IMAGE_FILE_MACHINE_I386, /* Machine */
53 #elif defined __x86_64__
54       IMAGE_FILE_MACHINE_AMD64, /* Machine */
55 #elif defined __powerpc__
56       IMAGE_FILE_MACHINE_POWERPC, /* Machine */
57 #else
58 # error You must specify the machine type
59 #endif
60       1, /* NumberOfSections */
61       0, /* TimeDateStamp */
62       0, /* PointerToSymbolTable */
63       0, /* NumberOfSymbols */
64       sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
65       IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
66     },
67     { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
68       1, /* MajorLinkerVersion */
69       0, /* MinorLinkerVersion */
70       0, /* SizeOfCode */
71       0, /* SizeOfInitializedData */
72       0, /* SizeOfUninitializedData */
73       0, /* AddressOfEntryPoint */
74       0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
75 #ifndef _WIN64
76       0, /* BaseOfData */
77 #endif
78       0x10000000, /* ImageBase */
79       0, /* SectionAlignment */
80       0, /* FileAlignment */
81       4, /* MajorOperatingSystemVersion */
82       0, /* MinorOperatingSystemVersion */
83       1, /* MajorImageVersion */
84       0, /* MinorImageVersion */
85       4, /* MajorSubsystemVersion */
86       0, /* MinorSubsystemVersion */
87       0, /* Win32VersionValue */
88       sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000, /* SizeOfImage */
89       sizeof(dos_header) + sizeof(nt_header), /* SizeOfHeaders */
90       0, /* CheckSum */
91       IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
92       0, /* DllCharacteristics */
93       0, /* SizeOfStackReserve */
94       0, /* SizeOfStackCommit */
95       0, /* SizeOfHeapReserve */
96       0, /* SizeOfHeapCommit */
97       0, /* LoaderFlags */
98       0, /* NumberOfRvaAndSizes */
99       { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
100     }
101 };
102
103 static IMAGE_SECTION_HEADER section =
104 {
105     ".rodata", /* Name */
106     { 0x10 }, /* Misc */
107     0, /* VirtualAddress */
108     0x0a, /* SizeOfRawData */
109     0, /* PointerToRawData */
110     0, /* PointerToRelocations */
111     0, /* PointerToLinenumbers */
112     0, /* NumberOfRelocations */
113     0, /* NumberOfLinenumbers */
114     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, /* Characteristics */
115 };
116
117 static void test_Loader(void)
118 {
119     static const struct test_data
120     {
121         const void *dos_header;
122         DWORD size_of_dos_header;
123         WORD number_of_sections, size_of_optional_header;
124         DWORD section_alignment, file_alignment;
125         DWORD size_of_image, size_of_headers;
126         DWORD error; /* 0 means LoadLibrary should succeed */
127         DWORD alt_error; /* alternate error */
128     } td[] =
129     {
130         { &dos_header, sizeof(dos_header),
131           1, 0, 0, 0, 0, 0,
132           ERROR_BAD_EXE_FORMAT
133         },
134         { &dos_header, sizeof(dos_header),
135           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
136           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0xe00,
137           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
138           ERROR_BAD_EXE_FORMAT /* XP doesn't like too small image size */
139         },
140         { &dos_header, sizeof(dos_header),
141           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
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         { &dos_header, sizeof(dos_header),
147           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
148           0x1f00,
149           0x1000,
150           ERROR_SUCCESS
151         },
152         { &dos_header, sizeof(dos_header),
153           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x200,
154           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x200,
155           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
156           ERROR_SUCCESS, ERROR_INVALID_ADDRESS /* vista is more strict */
157         },
158         { &dos_header, sizeof(dos_header),
159           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x1000,
160           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
161           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
162           ERROR_BAD_EXE_FORMAT /* XP doesn't like alignments */
163         },
164         { &dos_header, sizeof(dos_header),
165           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
166           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
167           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
168           ERROR_SUCCESS
169         },
170         { &dos_header, sizeof(dos_header),
171           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
172           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
173           0x200,
174           ERROR_SUCCESS
175         },
176         /* Mandatory are all fields up to SizeOfHeaders, everything else
177          * is really optional (at least that's true for XP).
178          */
179         { &dos_header, sizeof(dos_header),
180           1, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
181           sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER) + 0x10,
182           sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER),
183           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
184         },
185         { &dos_header, sizeof(dos_header),
186           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
187           0xd0, /* beyond of the end of file */
188           0xc0, /* beyond of the end of file */
189           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
190         },
191         { &dos_header, sizeof(dos_header),
192           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
193           0x1000,
194           0,
195           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
196         },
197         { &dos_header, sizeof(dos_header),
198           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
199           1,
200           0,
201           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
202         },
203 #if 0 /* not power of 2 alignments need more test cases */
204         { &dos_header, sizeof(dos_header),
205           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x300, 0x300,
206           1,
207           0,
208           ERROR_BAD_EXE_FORMAT /* alignment is not power of 2 */
209         },
210 #endif
211         { &dos_header, sizeof(dos_header),
212           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 4, 4,
213           1,
214           0,
215           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
216         },
217         { &dos_header, sizeof(dos_header),
218           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 1, 1,
219           1,
220           0,
221           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
222         },
223         { &dos_header, sizeof(dos_header),
224           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
225           0,
226           0,
227           ERROR_BAD_EXE_FORMAT /* image size == 0 -> failure */
228         },
229         /* the following data mimics the PE image which upack creates */
230         { &dos_header, 0x10,
231           1, 0x148, 0x1000, 0x200,
232           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
233           0x200,
234           ERROR_SUCCESS
235         },
236         /* Minimal PE image that XP is able to load: 92 bytes */
237         { &dos_header, 0x04,
238           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum),
239           0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
240           1,
241           0,
242           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
243         }
244     };
245     static const char filler[0x1000];
246     static const char section_data[0x10] = "section data";
247     int i;
248     DWORD dummy, file_size, file_align;
249     HANDLE hfile;
250     HMODULE hlib, hlib_as_data_file;
251     SYSTEM_INFO si;
252     char temp_path[MAX_PATH];
253     char dll_name[MAX_PATH];
254
255     GetSystemInfo(&si);
256     trace("system page size 0x%04x\n", si.dwPageSize);
257
258     /* prevent displaying of the "Unable to load this DLL" message box */
259     SetErrorMode(SEM_FAILCRITICALERRORS);
260
261     GetTempPath(MAX_PATH, temp_path);
262
263     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
264     {
265         GetTempFileName(temp_path, "ldr", 0, dll_name);
266
267         /*trace("creating %s\n", dll_name);*/
268         hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
269         if (hfile == INVALID_HANDLE_VALUE)
270         {
271             ok(0, "could not create %s\n", dll_name);
272             break;
273         }
274
275         SetLastError(0xdeadbeef);
276         ok(WriteFile(hfile, td[i].dos_header, td[i].size_of_dos_header, &dummy, NULL),
277            "WriteFile error %d\n", GetLastError());
278
279         nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
280         nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
281
282         nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
283         nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
284         nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
285         nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
286         SetLastError(0xdeadbeef);
287         ok(WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL),
288            "WriteFile error %d\n", GetLastError());
289
290         if (nt_header.FileHeader.SizeOfOptionalHeader)
291         {
292             SetLastError(0xdeadbeef);
293             ok(WriteFile(hfile, &nt_header.OptionalHeader,
294                          min(nt_header.FileHeader.SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER)),
295                          &dummy, NULL),
296                "WriteFile error %d\n", GetLastError());
297             if (nt_header.FileHeader.SizeOfOptionalHeader > sizeof(IMAGE_OPTIONAL_HEADER))
298             {
299                 file_align = nt_header.FileHeader.SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER);
300                 assert(file_align < sizeof(filler));
301                 SetLastError(0xdeadbeef);
302                 ok(WriteFile(hfile, filler, file_align, &dummy, NULL),
303                    "WriteFile error %d\n", GetLastError());
304             }
305         }
306
307         assert(nt_header.FileHeader.NumberOfSections <= 1);
308         if (nt_header.FileHeader.NumberOfSections)
309         {
310             if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
311             {
312                 section.PointerToRawData = td[i].size_of_dos_header;
313                 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
314                 section.Misc.VirtualSize = section.SizeOfRawData * 10;
315             }
316             else
317             {
318                 section.PointerToRawData = nt_header.OptionalHeader.SizeOfHeaders;
319                 section.VirtualAddress = nt_header.OptionalHeader.SizeOfHeaders;
320                 section.Misc.VirtualSize = 5;
321             }
322
323             SetLastError(0xdeadbeef);
324             ok(WriteFile(hfile, &section, sizeof(section), &dummy, NULL),
325                "WriteFile error %d\n", GetLastError());
326
327             /* section data */
328             SetLastError(0xdeadbeef);
329             ok(WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL),
330                "WriteFile error %d\n", GetLastError());
331         }
332
333         file_size = GetFileSize(hfile, NULL);
334         CloseHandle(hfile);
335
336         SetLastError(0xdeadbeef);
337         hlib = LoadLibrary(dll_name);
338         if (hlib)
339         {
340             MEMORY_BASIC_INFORMATION info;
341
342             ok( td[i].error == ERROR_SUCCESS, "%d: should have failed\n", i );
343
344             SetLastError(0xdeadbeef);
345             ok(VirtualQuery(hlib, &info, sizeof(info)) == sizeof(info),
346                 "%d: VirtualQuery error %d\n", i, GetLastError());
347             ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
348             ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
349             ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
350             ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
351                i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
352             ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
353             if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
354                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
355             else
356                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
357             ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
358
359             SetLastError(0xdeadbeef);
360             ok(VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info)) == sizeof(info),
361                 "%d: VirtualQuery error %d\n", i, GetLastError());
362             if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize ||
363                 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
364             {
365                 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %p != expected %p\n",
366                    i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
367                 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
368                 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
369                 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
370                 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
371                 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
372                 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
373             }
374             else
375             {
376                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
377                 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
378                 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
379                 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
380                 ok(info.RegionSize == ALIGN_SIZE(file_size, si.dwPageSize), "%d: got %lx != expected %x\n",
381                    i, info.RegionSize, ALIGN_SIZE(file_size, si.dwPageSize));
382                 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
383                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
384                 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
385             }
386
387             /* header: check the zeroing of alignment */
388             if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
389             {
390                 const char *start;
391                 int size;
392
393                 start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
394                 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
395                 ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
396             }
397
398             if (nt_header.FileHeader.NumberOfSections)
399             {
400                 SetLastError(0xdeadbeef);
401                 ok(VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info)) == sizeof(info),
402                     "%d: VirtualQuery error %d\n", i, GetLastError());
403                 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
404                 {
405                     ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
406                     ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
407                        i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
408                     ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
409                 }
410                 else
411                 {
412                     ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
413                     ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize), "%d: got %lx != expected %x\n",
414                        i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize));
415                     ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
416                 }
417                 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
418                 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
419                 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
420                 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
421
422                 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
423                     ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
424                 else
425                     ok(!memcmp((const char *)hlib + section.PointerToRawData, section_data, section.SizeOfRawData), "wrong section data\n");
426
427                 /* check the zeroing of alignment */
428                 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
429                 {
430                     const char *start;
431                     int size;
432
433                     start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
434                     size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
435                     ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
436                 }
437             }
438
439             SetLastError(0xdeadbeef);
440             hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
441             ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
442             ok(hlib_as_data_file == hlib, "hlib_as_file and hlib are different\n");
443
444             SetLastError(0xdeadbeef);
445             ok(FreeLibrary(hlib), "FreeLibrary error %d\n", GetLastError());
446
447             SetLastError(0xdeadbeef);
448             hlib = GetModuleHandle(dll_name);
449             ok(hlib != 0, "GetModuleHandle error %u\n", GetLastError());
450
451             SetLastError(0xdeadbeef);
452             ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
453
454             hlib = GetModuleHandle(dll_name);
455             ok(!hlib, "GetModuleHandle should fail\n");
456
457             SetLastError(0xdeadbeef);
458             hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
459             ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
460             ok((ULONG_PTR)hlib_as_data_file & 1, "hlib_as_data_file is even\n");
461
462             hlib = GetModuleHandle(dll_name);
463             ok(!hlib, "GetModuleHandle should fail\n");
464
465             SetLastError(0xdeadbeef);
466             ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
467         }
468         else
469         {
470             ok(td[i].error || td[i].alt_error, "%d: LoadLibrary should succeed\n", i);
471
472             if (GetLastError() == ERROR_GEN_FAILURE) /* Win9x, broken behaviour */
473             {
474                 trace("skipping the loader test on Win9x\n");
475                 DeleteFile(dll_name);
476                 return;
477             }
478
479             ok(td[i].error == GetLastError() || td[i].alt_error == GetLastError(),
480                "%d: expected error %d or %d, got %d\n",
481                i, td[i].error, td[i].alt_error, GetLastError());
482         }
483
484         SetLastError(0xdeadbeef);
485         ok(DeleteFile(dll_name), "DeleteFile error %d\n", GetLastError());
486     }
487 }
488
489 /* Verify linking style of import descriptors */
490 static void test_ImportDescriptors(void)
491 {
492     HMODULE kernel32_module = NULL;
493     PIMAGE_DOS_HEADER d_header;
494     PIMAGE_NT_HEADERS nt_headers;
495     DWORD import_dir_size;
496     DWORD_PTR dir_offset;
497     PIMAGE_IMPORT_DESCRIPTOR import_chunk;
498
499     /* Load kernel32 module */
500     kernel32_module = GetModuleHandleA("kernel32.dll");
501     assert( kernel32_module != NULL );
502
503     /* Get PE header info from module image */
504     d_header = (PIMAGE_DOS_HEADER) kernel32_module;
505     nt_headers = (PIMAGE_NT_HEADERS) (((char*) d_header) +
506             d_header->e_lfanew);
507
508     /* Get size of import entry directory */
509     import_dir_size = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
510     if (!import_dir_size)
511     {
512         skip("Unable to continue testing due to missing import directory.\n");
513         return;
514     }
515
516     /* Get address of first import chunk */
517     dir_offset = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
518     import_chunk = RVAToAddr(dir_offset, kernel32_module);
519     ok(import_chunk != 0, "Invalid import_chunk: %p\n", import_chunk);
520     if (!import_chunk) return;
521
522     /* Iterate through import descriptors and verify set name,
523      * OriginalFirstThunk, and FirstThunk.  Core Windows DLLs, such as
524      * kernel32.dll, don't use Borland-style linking, where the table of
525      * imported names is stored directly in FirstThunk and overwritten
526      * by the relocation, instead of being stored in OriginalFirstThunk.
527      * */
528     for (; import_chunk->FirstThunk; import_chunk++)
529     {
530         LPCSTR module_name = RVAToAddr(import_chunk->Name, kernel32_module);
531         PIMAGE_THUNK_DATA name_table = RVAToAddr(
532                 U(*import_chunk).OriginalFirstThunk, kernel32_module);
533         PIMAGE_THUNK_DATA iat = RVAToAddr(
534                 import_chunk->FirstThunk, kernel32_module);
535         ok(module_name != NULL, "Imported module name should not be NULL\n");
536         ok(name_table != NULL,
537                 "Name table for imported module %s should not be NULL\n",
538                 module_name);
539         ok(iat != NULL, "IAT for imported module %s should not be NULL\n",
540                 module_name);
541     }
542 }
543
544 START_TEST(loader)
545 {
546     test_Loader();
547     test_ImportDescriptors();
548 }