kernel32: Fix the image size in a test to avoid a BSOD on Windows XP SP1.
[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     {
44 #if defined __i386__
45       IMAGE_FILE_MACHINE_I386, /* Machine */
46 #elif defined __x86_64__
47       IMAGE_FILE_MACHINE_AMD64, /* Machine */
48 #else
49 # error You must specify the machine type
50 #endif
51       1, /* NumberOfSections */
52       0, /* TimeDateStamp */
53       0, /* PointerToSymbolTable */
54       0, /* NumberOfSymbols */
55       sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
56       IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
57     },
58     { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
59       1, /* MajorLinkerVersion */
60       0, /* MinorLinkerVersion */
61       0, /* SizeOfCode */
62       0, /* SizeOfInitializedData */
63       0, /* SizeOfUninitializedData */
64       0, /* AddressOfEntryPoint */
65       0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
66       0, /* BaseOfData */
67       0x10000000, /* ImageBase */
68       0, /* SectionAlignment */
69       0, /* FileAlignment */
70       4, /* MajorOperatingSystemVersion */
71       0, /* MinorOperatingSystemVersion */
72       1, /* MajorImageVersion */
73       0, /* MinorImageVersion */
74       4, /* MajorSubsystemVersion */
75       0, /* MinorSubsystemVersion */
76       0, /* Win32VersionValue */
77       sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000, /* SizeOfImage */
78       sizeof(dos_header) + sizeof(nt_header), /* SizeOfHeaders */
79       0, /* CheckSum */
80       IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
81       0, /* DllCharacteristics */
82       0, /* SizeOfStackReserve */
83       0, /* SizeOfStackCommit */
84       0, /* SizeOfHeapReserve */
85       0, /* SizeOfHeapCommit */
86       0, /* LoaderFlags */
87       0, /* NumberOfRvaAndSizes */
88       { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
89     }
90 };
91
92 static IMAGE_SECTION_HEADER section =
93 {
94     ".rodata", /* Name */
95     { 0x10 }, /* Misc */
96     0, /* VirtualAddress */
97     0x0a, /* SizeOfRawData */
98     0, /* PointerToRawData */
99     0, /* PointerToRelocations */
100     0, /* PointerToLinenumbers */
101     0, /* NumberOfRelocations */
102     0, /* NumberOfLinenumbers */
103     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, /* Characteristics */
104 };
105
106 START_TEST(loader)
107 {
108     static const struct test_data
109     {
110         const void *dos_header;
111         DWORD size_of_dos_header;
112         WORD number_of_sections, size_of_optional_header;
113         DWORD section_alignment, file_alignment;
114         DWORD size_of_image, size_of_headers;
115         DWORD error; /* 0 means LoadLibrary should succeed */
116     } td[] =
117     {
118         { &dos_header, sizeof(dos_header),
119           1, 0, 0, 0, 0, 0,
120           ERROR_BAD_EXE_FORMAT
121         },
122         { &dos_header, sizeof(dos_header),
123           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
124           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0xe00,
125           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
126           ERROR_BAD_EXE_FORMAT /* XP doesn't like too small image size */
127         },
128         { &dos_header, sizeof(dos_header),
129           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
130           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
131           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
132           ERROR_SUCCESS
133         },
134         { &dos_header, sizeof(dos_header),
135           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
136           0x1f00,
137           0x1000,
138           ERROR_SUCCESS
139         },
140         { &dos_header, sizeof(dos_header),
141           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x200,
142           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x200,
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), 0x200, 0x1000,
148           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
149           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
150           ERROR_BAD_EXE_FORMAT /* XP doesn't like aligments */
151         },
152         { &dos_header, sizeof(dos_header),
153           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
154           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
155           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
156           ERROR_SUCCESS
157         },
158         { &dos_header, sizeof(dos_header),
159           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
160           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
161           0x200,
162           ERROR_SUCCESS
163         },
164         /* Mandatory are all fields up to SizeOfHeaders, everything else
165          * is really optional (at least that's true for XP).
166          */
167         { &dos_header, sizeof(dos_header),
168           1, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
169           sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER) + 0x10,
170           sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER),
171           ERROR_SUCCESS
172         },
173         { &dos_header, sizeof(dos_header),
174           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
175           0xd0, /* beyond of the end of file */
176           0xc0, /* beyond of the end of file */
177           ERROR_SUCCESS
178         },
179         { &dos_header, sizeof(dos_header),
180           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
181           0x1000,
182           0,
183           ERROR_SUCCESS
184         },
185         { &dos_header, sizeof(dos_header),
186           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
187           1,
188           0,
189           ERROR_SUCCESS
190         },
191 #if 0 /* not power of 2 alignments need more test cases */
192         { &dos_header, sizeof(dos_header),
193           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x300, 0x300,
194           1,
195           0,
196           ERROR_BAD_EXE_FORMAT /* alignment is not power of 2 */
197         },
198 #endif
199         { &dos_header, sizeof(dos_header),
200           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 4, 4,
201           1,
202           0,
203           ERROR_SUCCESS
204         },
205         { &dos_header, sizeof(dos_header),
206           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 1, 1,
207           1,
208           0,
209           ERROR_SUCCESS
210         },
211         { &dos_header, sizeof(dos_header),
212           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
213           0,
214           0,
215           ERROR_BAD_EXE_FORMAT /* image size == 0 -> failure */
216         },
217         /* the following data mimics the PE image which upack creates */
218         { &dos_header, 0x10,
219           1, 0x148, 0x1000, 0x200,
220           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
221           0x200,
222           ERROR_SUCCESS
223         },
224         /* Minimal PE image that XP is able to load: 92 bytes */
225         { &dos_header, 0x04,
226           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum),
227           0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
228           1,
229           0,
230           ERROR_SUCCESS
231         }
232     };
233     static const char filler[0x1000];
234     static const char section_data[0x10] = "section data";
235     int i;
236     DWORD dummy, file_size, file_align;
237     HANDLE hfile;
238     HMODULE hlib, hlib_as_data_file;
239     SYSTEM_INFO si;
240     char temp_path[MAX_PATH];
241     char dll_name[MAX_PATH];
242
243     GetSystemInfo(&si);
244     trace("system page size 0x%04x\n", si.dwPageSize);
245
246     /* prevent displaying of the "Unable to load this DLL" message box */
247     SetErrorMode(SEM_FAILCRITICALERRORS);
248
249     GetTempPath(MAX_PATH, temp_path);
250
251     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
252     {
253         GetTempFileName(temp_path, "ldr", 0, dll_name);
254
255         /*trace("creating %s\n", dll_name);*/
256         hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
257         if (hfile == INVALID_HANDLE_VALUE)
258         {
259             ok(0, "could not create %s\n", dll_name);
260             break;
261         }
262
263         SetLastError(0xdeadbeef);
264         ok(WriteFile(hfile, td[i].dos_header, td[i].size_of_dos_header, &dummy, NULL),
265            "WriteFile error %d\n", GetLastError());
266
267         nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
268         nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
269
270         nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
271         nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
272         nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
273         nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
274         SetLastError(0xdeadbeef);
275         ok(WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL),
276            "WriteFile error %d\n", GetLastError());
277
278         if (nt_header.FileHeader.SizeOfOptionalHeader)
279         {
280             SetLastError(0xdeadbeef);
281             ok(WriteFile(hfile, &nt_header.OptionalHeader,
282                          min(nt_header.FileHeader.SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER)),
283                          &dummy, NULL),
284                "WriteFile error %d\n", GetLastError());
285             if (nt_header.FileHeader.SizeOfOptionalHeader > sizeof(IMAGE_OPTIONAL_HEADER))
286             {
287                 file_align = nt_header.FileHeader.SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER);
288                 assert(file_align < sizeof(filler));
289                 SetLastError(0xdeadbeef);
290                 ok(WriteFile(hfile, filler, file_align, &dummy, NULL),
291                    "WriteFile error %d\n", GetLastError());
292             }
293         }
294
295         assert(nt_header.FileHeader.NumberOfSections <= 1);
296         if (nt_header.FileHeader.NumberOfSections)
297         {
298             if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
299             {
300                 section.PointerToRawData = td[i].size_of_dos_header;
301                 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
302                 section.Misc.VirtualSize = section.SizeOfRawData * 10;
303             }
304             else
305             {
306                 section.PointerToRawData = nt_header.OptionalHeader.SizeOfHeaders;
307                 section.VirtualAddress = nt_header.OptionalHeader.SizeOfHeaders;
308                 section.Misc.VirtualSize = 5;
309             }
310
311             SetLastError(0xdeadbeef);
312             ok(WriteFile(hfile, &section, sizeof(section), &dummy, NULL),
313                "WriteFile error %d\n", GetLastError());
314
315             /* section data */
316             SetLastError(0xdeadbeef);
317             ok(WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL),
318                "WriteFile error %d\n", GetLastError());
319         }
320
321         file_size = GetFileSize(hfile, NULL);
322         CloseHandle(hfile);
323
324         SetLastError(0xdeadbeef);
325         hlib = LoadLibrary(dll_name);
326         if (td[i].error == ERROR_SUCCESS)
327         {
328             MEMORY_BASIC_INFORMATION info;
329
330             ok(hlib != 0, "%d: LoadLibrary error %d\n", i, GetLastError());
331
332             /* No point in crashing. Test crashes on Vista with some of the given files */
333             if (hlib == 0)
334             {
335                 skip("Failed to load dll number %d\n", i);
336                 goto endloop;
337             }
338
339             SetLastError(0xdeadbeef);
340             ok(VirtualQuery(hlib, &info, sizeof(info)) == sizeof(info),
341                 "%d: VirtualQuery error %d\n", i, GetLastError());
342             ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
343             ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
344             ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
345             ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
346                i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
347             ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
348             if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
349                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
350             else
351                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
352             ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
353
354             SetLastError(0xdeadbeef);
355             ok(VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info)) == sizeof(info),
356                 "%d: VirtualQuery error %d\n", i, GetLastError());
357             if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize ||
358                 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
359             {
360                 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %p != expected %p\n",
361                    i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
362                 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
363                 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
364                 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
365                 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
366                 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
367                 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
368             }
369             else
370             {
371                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
372                 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
373                 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
374                 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
375                 ok(info.RegionSize == ALIGN_SIZE(file_size, si.dwPageSize), "%d: got %lx != expected %x\n",
376                    i, info.RegionSize, ALIGN_SIZE(file_size, si.dwPageSize));
377                 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
378                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
379                 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
380             }
381
382             /* header: check the zeroing of alignment */
383             if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
384             {
385                 const char *start;
386                 int size;
387
388                 start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
389                 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
390                 ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
391             }
392
393             if (nt_header.FileHeader.NumberOfSections)
394             {
395                 SetLastError(0xdeadbeef);
396                 ok(VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info)) == sizeof(info),
397                     "%d: VirtualQuery error %d\n", i, GetLastError());
398                 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
399                 {
400                     ok(info.BaseAddress == (char *)hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib);
401                     ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
402                        i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
403                     ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
404                 }
405                 else
406                 {
407                     ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
408                     ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize), "%d: got %lx != expected %x\n",
409                        i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize));
410                     ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
411                 }
412                 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
413                 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
414                 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
415                 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
416
417                 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
418                     ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
419                 else
420                     ok(!memcmp((const char *)hlib + section.PointerToRawData, section_data, section.SizeOfRawData), "wrong section data\n");
421
422                 /* check the zeroing of alignment */
423                 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
424                 {
425                     const char *start;
426                     int size;
427
428                     start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
429                     size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
430                     ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
431                 }
432             }
433
434             SetLastError(0xdeadbeef);
435             hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
436             ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
437             ok(hlib_as_data_file == hlib, "hlib_as_file and hlib are different\n");
438
439             SetLastError(0xdeadbeef);
440             ok(FreeLibrary(hlib), "FreeLibrary error %d\n", GetLastError());
441
442             SetLastError(0xdeadbeef);
443             hlib = GetModuleHandle(dll_name);
444             ok(hlib != 0, "GetModuleHandle error %u\n", GetLastError());
445
446             SetLastError(0xdeadbeef);
447             ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
448
449             hlib = GetModuleHandle(dll_name);
450             ok(!hlib, "GetModuleHandle should fail\n");
451
452             SetLastError(0xdeadbeef);
453             hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
454             ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
455             ok((ULONG_PTR)hlib_as_data_file & 1, "hlib_as_data_file is even\n");
456
457             hlib = GetModuleHandle(dll_name);
458             ok(!hlib, "GetModuleHandle should fail\n");
459
460             SetLastError(0xdeadbeef);
461             ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
462         }
463         else
464         {   /* LoadLibrary is expected to fail */
465             ok(!hlib, "%d: LoadLibrary should fail\n", i);
466
467             if (GetLastError() == ERROR_GEN_FAILURE) /* Win9x, broken behaviour */
468             {
469                 trace("skipping the loader test on Win9x\n");
470                 DeleteFile(dll_name);
471                 return;
472             }
473
474             ok(td[i].error == GetLastError(), "%d: expected error %d, got %d\n",
475                i, td[i].error, GetLastError());
476         }
477
478 endloop:
479         SetLastError(0xdeadbeef);
480         ok(DeleteFile(dll_name), "DeleteFile error %d\n", GetLastError());
481     }
482 }