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