d3d10/effect: Add support for 'imul' instruction.
[wine.git] / dlls / imagehlp / tests / image.c
bloba2fc306428f204b1a43ec78838f4caed0a49dd60
1 /*
2 * Copyright 2008 Juan Lang
3 * Copyright 2010 Andrey Turkin
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdio.h>
20 #include <stdarg.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include <winver.h>
25 #include <winnt.h>
26 #include <winuser.h>
27 #include <imagehlp.h>
29 #include "wine/test.h"
31 static char *load_resource(const char *name)
33 static char path[MAX_PATH];
34 DWORD written;
35 HANDLE file;
36 HRSRC res;
37 void *ptr;
39 GetTempPathA(ARRAY_SIZE(path), path);
40 strcat(path, name);
42 file = CreateFileA(path, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
43 ok(file != INVALID_HANDLE_VALUE, "Failed to create file %s, error %lu.\n",
44 debugstr_a(path), GetLastError());
46 res = FindResourceA(NULL, name, "TESTDLL");
47 ok(!!res, "Failed to load resource, error %lu.\n", GetLastError());
48 ptr = LockResource(LoadResource(GetModuleHandleA(NULL), res));
49 WriteFile(file, ptr, SizeofResource( GetModuleHandleA(NULL), res), &written, NULL);
50 ok(written == SizeofResource(GetModuleHandleA(NULL), res), "Failed to write resource.\n");
51 CloseHandle(file);
53 return path;
56 /* minimal PE file image */
57 #define VA_START 0x400000
58 #define FILE_PE_START 0x50
59 #define NUM_SECTIONS 3
60 #define FILE_TEXT 0x200
61 #define RVA_TEXT 0x1000
62 #define RVA_BSS 0x2000
63 #define FILE_IDATA 0x400
64 #define RVA_IDATA 0x3000
65 #define FILE_TOTAL 0x600
66 #define RVA_TOTAL 0x4000
67 #include <pshpack1.h>
68 struct Imports {
69 IMAGE_IMPORT_DESCRIPTOR descriptors[2];
70 IMAGE_THUNK_DATA32 original_thunks[2];
71 IMAGE_THUNK_DATA32 thunks[2];
72 struct __IMPORT_BY_NAME {
73 WORD hint;
74 char funcname[0x20];
75 } ibn;
76 char dllname[0x10];
78 #define EXIT_PROCESS (VA_START+RVA_IDATA+FIELD_OFFSET(struct Imports, thunks))
80 static struct _PeImage {
81 IMAGE_DOS_HEADER dos_header;
82 char __alignment1[FILE_PE_START - sizeof(IMAGE_DOS_HEADER)];
83 IMAGE_NT_HEADERS32 nt_headers;
84 IMAGE_SECTION_HEADER sections[NUM_SECTIONS];
85 char __alignment2[FILE_TEXT - FILE_PE_START - sizeof(IMAGE_NT_HEADERS32) -
86 NUM_SECTIONS * sizeof(IMAGE_SECTION_HEADER)];
87 unsigned char text_section[FILE_IDATA-FILE_TEXT];
88 struct Imports idata_section;
89 char __alignment3[FILE_TOTAL-FILE_IDATA-sizeof(struct Imports)];
90 } bin = {
91 /* dos header */
92 {IMAGE_DOS_SIGNATURE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0}, 0, 0, {0}, FILE_PE_START},
93 /* alignment before PE header */
94 {0},
95 /* nt headers */
96 {IMAGE_NT_SIGNATURE,
97 /* basic headers - 3 sections, no symbols, EXE file */
98 {IMAGE_FILE_MACHINE_I386, NUM_SECTIONS, 0, 0, 0, sizeof(IMAGE_OPTIONAL_HEADER32),
99 IMAGE_FILE_32BIT_MACHINE | IMAGE_FILE_EXECUTABLE_IMAGE},
100 /* optional header */
101 {IMAGE_NT_OPTIONAL_HDR32_MAGIC, 4, 0, FILE_IDATA-FILE_TEXT,
102 FILE_TOTAL-FILE_IDATA + FILE_IDATA-FILE_TEXT, 0x400,
103 RVA_TEXT, RVA_TEXT, RVA_BSS, VA_START, 0x1000, 0x200, 4, 0, 1, 0, 4, 0, 0,
104 RVA_TOTAL, FILE_TEXT, 0, IMAGE_SUBSYSTEM_WINDOWS_GUI, 0,
105 0x200000, 0x1000, 0x100000, 0x1000, 0, 0x10,
106 {{0, 0},
107 {RVA_IDATA, sizeof(struct Imports)}
111 /* sections */
113 {".text", {0x100}, RVA_TEXT, FILE_IDATA-FILE_TEXT, FILE_TEXT,
114 0, 0, 0, 0, IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ},
115 {".bss", {0x400}, RVA_BSS, 0, 0, 0, 0, 0, 0,
116 IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE},
117 {".idata", {sizeof(struct Imports)}, RVA_IDATA, FILE_TOTAL-FILE_IDATA, FILE_IDATA, 0,
118 0, 0, 0, IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE}
120 /* alignment before first section */
121 {0},
122 /* .text section */
124 0x31, 0xC0, /* xor eax, eax */
125 0xFF, 0x25, EXIT_PROCESS&0xFF, (EXIT_PROCESS>>8)&0xFF, (EXIT_PROCESS>>16)&0xFF,
126 (EXIT_PROCESS>>24)&0xFF, /* jmp ExitProcess */
129 /* .idata section */
132 {{RVA_IDATA + FIELD_OFFSET(struct Imports, original_thunks)}, 0, 0,
133 RVA_IDATA + FIELD_OFFSET(struct Imports, dllname),
134 RVA_IDATA + FIELD_OFFSET(struct Imports, thunks)
136 {{0}, 0, 0, 0, 0}
138 {{{RVA_IDATA+FIELD_OFFSET(struct Imports, ibn)}}, {{0}}},
139 {{{RVA_IDATA+FIELD_OFFSET(struct Imports, ibn)}}, {{0}}},
140 {0,"ExitProcess"},
141 "KERNEL32.DLL"
143 /* final alignment */
146 #include <poppack.h>
148 struct blob
150 DWORD cb;
151 BYTE *pb;
154 struct expected_blob
156 DWORD cb;
157 const void *pb;
160 struct update_accum
162 DWORD cUpdates;
163 struct blob *updates;
166 struct expected_update_accum
168 DWORD cUpdates;
169 const struct expected_blob *updates;
170 BOOL todo;
173 static BOOL WINAPI accumulating_stream_output(DIGEST_HANDLE handle, BYTE *pb,
174 DWORD cb)
176 struct update_accum *accum = (struct update_accum *)handle;
177 BOOL ret = FALSE;
179 if (accum->cUpdates)
180 accum->updates = HeapReAlloc(GetProcessHeap(), 0, accum->updates,
181 (accum->cUpdates + 1) * sizeof(struct blob));
182 else
183 accum->updates = HeapAlloc(GetProcessHeap(), 0, sizeof(struct blob));
184 if (accum->updates)
186 struct blob *blob = &accum->updates[accum->cUpdates];
188 blob->pb = HeapAlloc(GetProcessHeap(), 0, cb);
189 if (blob->pb)
191 memcpy(blob->pb, pb, cb);
192 blob->cb = cb;
193 ret = TRUE;
195 accum->cUpdates++;
197 return ret;
200 static void check_updates(LPCSTR header, const struct expected_update_accum *expected,
201 const struct update_accum *got)
203 DWORD i;
205 todo_wine_if (expected->todo)
206 ok(expected->cUpdates == got->cUpdates, "%s: expected %ld updates, got %ld\n",
207 header, expected->cUpdates, got->cUpdates);
208 for (i = 0; i < min(expected->cUpdates, got->cUpdates); i++)
210 ok(expected->updates[i].cb == got->updates[i].cb, "%s, update %ld: expected %ld bytes, got %ld\n",
211 header, i, expected->updates[i].cb, got->updates[i].cb);
212 if (expected->updates[i].cb && expected->updates[i].cb == got->updates[i].cb)
213 ok(!memcmp(expected->updates[i].pb, got->updates[i].pb, got->updates[i].cb),
214 "%s, update %ld: unexpected value\n", header, i);
218 /* Frees the updates stored in accum */
219 static void free_updates(struct update_accum *accum)
221 DWORD i;
223 for (i = 0; i < accum->cUpdates; i++)
224 HeapFree(GetProcessHeap(), 0, accum->updates[i].pb);
225 HeapFree(GetProcessHeap(), 0, accum->updates);
226 accum->updates = NULL;
227 accum->cUpdates = 0;
230 static const struct expected_blob b1[] = {
231 {FILE_PE_START, &bin},
232 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
233 {sizeof(bin.nt_headers), &bin.nt_headers},
234 {sizeof(bin.sections), &bin.sections},
235 {FILE_IDATA-FILE_TEXT, &bin.text_section},
236 {sizeof(bin.idata_section.descriptors[0].OriginalFirstThunk),
237 &bin.idata_section.descriptors[0].OriginalFirstThunk},
238 {FIELD_OFFSET(struct Imports, thunks)-
239 (FIELD_OFFSET(struct Imports, descriptors)+FIELD_OFFSET(IMAGE_IMPORT_DESCRIPTOR, Name)),
240 &bin.idata_section.descriptors[0].Name},
241 {FILE_TOTAL-FILE_IDATA-FIELD_OFFSET(struct Imports, ibn),
242 &bin.idata_section.ibn}
244 static const struct expected_update_accum a1 = { ARRAY_SIZE(b1), b1, TRUE };
246 static const struct expected_blob b2[] = {
247 {FILE_PE_START, &bin},
248 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
249 {sizeof(bin.nt_headers), &bin.nt_headers},
250 {sizeof(bin.sections), &bin.sections},
251 {FILE_IDATA-FILE_TEXT, &bin.text_section},
252 {FILE_TOTAL-FILE_IDATA, &bin.idata_section}
254 static const struct expected_update_accum a2 = { ARRAY_SIZE(b2), b2, FALSE };
256 /* Creates a test file and returns a handle to it. The file's path is returned
257 * in temp_file, which must be at least MAX_PATH characters in length.
259 static HANDLE create_temp_file(char *temp_file)
261 HANDLE file = INVALID_HANDLE_VALUE;
262 char temp_path[MAX_PATH];
264 if (GetTempPathA(sizeof(temp_path), temp_path))
266 if (GetTempFileNameA(temp_path, "img", 0, temp_file))
267 file = CreateFileA(temp_file, GENERIC_READ | GENERIC_WRITE, 0, NULL,
268 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
270 return file;
273 static void update_checksum(void)
275 WORD const * ptr;
276 DWORD size;
277 DWORD sum = 0;
279 bin.nt_headers.OptionalHeader.CheckSum = 0;
281 for(ptr = (WORD const *)&bin, size = (sizeof(bin)+1)/sizeof(WORD); size > 0; ptr++, size--)
283 sum += *ptr;
284 if (HIWORD(sum) != 0)
286 sum = LOWORD(sum) + HIWORD(sum);
289 sum = (WORD)(LOWORD(sum) + HIWORD(sum));
290 sum += sizeof(bin);
292 bin.nt_headers.OptionalHeader.CheckSum = sum;
295 static void test_get_digest_stream(void)
297 BOOL ret;
298 HANDLE file;
299 char temp_file[MAX_PATH];
300 DWORD count;
301 struct update_accum accum = { 0, NULL };
303 SetLastError(0xdeadbeef);
304 ret = ImageGetDigestStream(NULL, 0, NULL, NULL);
305 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
306 "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
307 file = create_temp_file(temp_file);
308 if (file == INVALID_HANDLE_VALUE)
310 skip("couldn't create temp file\n");
311 return;
313 SetLastError(0xdeadbeef);
314 ret = ImageGetDigestStream(file, 0, NULL, NULL);
315 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
316 "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
317 SetLastError(0xdeadbeef);
318 ret = ImageGetDigestStream(NULL, 0, accumulating_stream_output, &accum);
319 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
320 "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
321 /* Even with "valid" parameters, it fails with an empty file */
322 SetLastError(0xdeadbeef);
323 ret = ImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
324 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
325 "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
326 /* Finally, with a valid executable in the file, it succeeds. Note that
327 * the file pointer need not be positioned at the beginning.
329 update_checksum();
330 WriteFile(file, &bin, sizeof(bin), &count, NULL);
331 FlushFileBuffers(file);
333 /* zero out some fields ImageGetDigestStream would zero out */
334 bin.nt_headers.OptionalHeader.CheckSum = 0;
335 bin.nt_headers.OptionalHeader.SizeOfInitializedData = 0;
336 bin.nt_headers.OptionalHeader.SizeOfImage = 0;
338 ret = ImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
339 ok(ret, "ImageGetDigestStream failed: %ld\n", GetLastError());
340 check_updates("flags = 0", &a1, &accum);
341 free_updates(&accum);
342 ret = ImageGetDigestStream(file, CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO,
343 accumulating_stream_output, &accum);
344 ok(ret, "ImageGetDigestStream failed: %ld\n", GetLastError());
345 check_updates("flags = CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO", &a2, &accum);
346 free_updates(&accum);
347 CloseHandle(file);
348 DeleteFileA(temp_file);
351 static unsigned int got_SysAllocString, got_GetOpenFileNameA, got_SHRegGetIntW;
353 static BOOL WINAPI bind_image_cb(IMAGEHLP_STATUS_REASON reason, const char *file,
354 const char *module, ULONG_PTR va, ULONG_PTR param)
356 static char last_module[MAX_PATH];
358 if (winetest_debug > 1)
359 trace("reason %u, file %s, module %s, va %#Ix, param %#Ix\n",
360 reason, debugstr_a(file), debugstr_a(module), va, param);
362 if (reason == BindImportModule)
364 ok(!strchr(module, '\\'), "got module name %s\n", debugstr_a(module));
365 strcpy(last_module, module);
366 ok(!va, "got VA %#Ix\n", va);
367 ok(!param, "got param %#Ix\n", param);
369 else if (reason == BindImportProcedure)
371 char full_path[MAX_PATH];
372 BOOL ret;
374 todo_wine ok(!!va, "expected nonzero VA\n");
375 ret = SearchPathA(NULL, last_module, ".dll", sizeof(full_path), full_path, NULL);
376 ok(ret, "got error %lu\n", GetLastError());
377 ok(!strcmp(module, full_path), "expected %s, got %s\n", debugstr_a(full_path), debugstr_a(module));
379 if (!strcmp((const char *)param, "SysAllocString"))
381 ok(!strcmp(last_module, "oleaut32.dll"), "got wrong module %s\n", debugstr_a(module));
382 ++got_SysAllocString;
384 else if (!strcmp((const char *)param, "GetOpenFileNameA"))
386 ok(!strcmp(last_module, "comdlg32.dll"), "got wrong module %s\n", debugstr_a(module));
387 ++got_GetOpenFileNameA;
389 else if (!strcmp((const char *)param, "Ordinal117"))
391 ok(!strcmp(last_module, "shlwapi.dll"), "got wrong module %s\n", debugstr_a(module));
392 ++got_SHRegGetIntW;
395 else
397 ok(0, "got unexpected reason %#x\n", reason);
399 return TRUE;
402 static void test_bind_image_ex(void)
404 const char *filename = load_resource("testdll.dll");
405 BOOL ret;
407 SetLastError(0xdeadbeef);
408 ret = BindImageEx(BIND_ALL_IMAGES | BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE,
409 "nonexistent.dll", 0, 0, bind_image_cb);
410 ok(!ret, "expected failure\n");
411 ok(GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_INVALID_PARAMETER,
412 "got error %lu\n", GetLastError());
414 ret = BindImageEx(BIND_ALL_IMAGES | BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE,
415 filename, NULL, NULL, bind_image_cb);
416 ok(ret, "got error %lu\n", GetLastError());
417 ok(got_SysAllocString == 1, "got %u imports of SysAllocString\n", got_SysAllocString);
418 ok(got_GetOpenFileNameA == 1, "got %u imports of GetOpenFileNameA\n", got_GetOpenFileNameA);
419 todo_wine ok(got_SHRegGetIntW == 1, "got %u imports of SHRegGetIntW\n", got_SHRegGetIntW);
421 ret = DeleteFileA(filename);
422 ok(ret, "got error %lu\n", GetLastError());
425 static void test_image_load(void)
427 char temp_file[MAX_PATH];
428 PLOADED_IMAGE img;
429 DWORD ret, count;
430 HANDLE file;
432 file = create_temp_file(temp_file);
433 if (file == INVALID_HANDLE_VALUE)
435 skip("couldn't create temp file\n");
436 return;
439 WriteFile(file, &bin, sizeof(bin), &count, NULL);
440 CloseHandle(file);
442 img = ImageLoad(temp_file, NULL);
443 ok(img != NULL, "ImageLoad unexpectedly failed\n");
445 if (img)
447 ok(!strcmp(img->ModuleName, temp_file),
448 "unexpected ModuleName, got %s instead of %s\n", img->ModuleName, temp_file);
449 ok(img->MappedAddress != NULL, "MappedAddress != NULL\n");
450 if (img->MappedAddress)
452 ok(!memcmp(img->MappedAddress, &bin.dos_header, sizeof(bin.dos_header)),
453 "MappedAddress doesn't point to IMAGE_DOS_HEADER\n");
455 ok(img->FileHeader != NULL, "FileHeader != NULL\n");
456 if (img->FileHeader)
458 ok(!memcmp(img->FileHeader, &bin.nt_headers, sizeof(bin.nt_headers)),
459 "FileHeader doesn't point to IMAGE_NT_HEADERS32\n");
461 ok(img->NumberOfSections == 3,
462 "unexpected NumberOfSections, got %ld instead of 3\n", img->NumberOfSections);
463 if (img->NumberOfSections >= 3)
465 ok(!strcmp((const char *)img->Sections[0].Name, ".text"),
466 "unexpected name for section 0, expected .text, got %s\n",
467 (const char *)img->Sections[0].Name);
468 ok(!strcmp((const char *)img->Sections[1].Name, ".bss"),
469 "unexpected name for section 1, expected .bss, got %s\n",
470 (const char *)img->Sections[1].Name);
471 ok(!strcmp((const char *)img->Sections[2].Name, ".idata"),
472 "unexpected name for section 2, expected .idata, got %s\n",
473 (const char *)img->Sections[2].Name);
475 ok(img->Characteristics == 0x102,
476 "unexpected Characteristics, got 0x%lx instead of 0x102\n", img->Characteristics);
477 ok(img->fSystemImage == 0,
478 "unexpected fSystemImage, got %d instead of 0\n", img->fSystemImage);
479 ok(img->fDOSImage == 0,
480 "unexpected fDOSImage, got %d instead of 0\n", img->fDOSImage);
481 todo_wine ok(img->fReadOnly == 1 || broken(!img->fReadOnly) /* <= WinXP */,
482 "unexpected fReadOnly, got %d instead of 1\n", img->fReadOnly);
483 todo_wine ok(img->Version == 1 || broken(!img->Version) /* <= WinXP */,
484 "unexpected Version, got %d instead of 1\n", img->Version);
485 ok(img->SizeOfImage == 0x600,
486 "unexpected SizeOfImage, got 0x%lx instead of 0x600\n", img->SizeOfImage);
488 count = 0xdeadbeef;
489 ret = GetImageUnusedHeaderBytes(img, &count);
490 todo_wine
491 ok(ret == 448, "GetImageUnusedHeaderBytes returned %lu instead of 448\n", ret);
492 todo_wine
493 ok(count == 64, "unexpected size for unused header bytes, got %lu instead of 64\n", count);
495 ImageUnload(img);
498 DeleteFileA(temp_file);
501 START_TEST(image)
503 test_get_digest_stream();
504 test_bind_image_ex();
505 test_image_load();