[IMAGEHLP_WINETEST] Add to and fix MSVC build. By Mark Jansen.
[reactos.git] / rostests / winetests / imagehlp / image.c
1 /*
2 * Copyright 2008 Juan Lang
3 * Copyright 2010 Andrey Turkin
4 *
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.
9 *
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.
14 *
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
18 */
19 #include <stdio.h>
20 #include <stdarg.h>
21
22 #define NONAMELESSUNION
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winver.h>
26 #include <winnt.h>
27 #include <imagehlp.h>
28
29 #include "wine/test.h"
30
31 static HMODULE hImageHlp;
32
33 static BOOL (WINAPI *pImageGetDigestStream)(HANDLE, DWORD, DIGEST_FUNCTION, DIGEST_HANDLE);
34 static BOOL (WINAPI *pBindImageEx)(DWORD Flags, const char *ImageName, const char *DllPath,
35 const char *SymbolPath, PIMAGEHLP_STATUS_ROUTINE StatusRoutine);
36
37 /* minimal PE file image */
38 #define VA_START 0x400000
39 #define FILE_PE_START 0x50
40 #define NUM_SECTIONS 3
41 #define FILE_TEXT 0x200
42 #define RVA_TEXT 0x1000
43 #define RVA_BSS 0x2000
44 #define FILE_IDATA 0x400
45 #define RVA_IDATA 0x3000
46 #define FILE_TOTAL 0x600
47 #define RVA_TOTAL 0x4000
48 #include <pshpack1.h>
49 struct Imports {
50 IMAGE_IMPORT_DESCRIPTOR descriptors[2];
51 IMAGE_THUNK_DATA32 original_thunks[2];
52 IMAGE_THUNK_DATA32 thunks[2];
53 struct __IMPORT_BY_NAME {
54 WORD hint;
55 char funcname[0x20];
56 } ibn;
57 char dllname[0x10];
58 };
59 #define EXIT_PROCESS (VA_START+RVA_IDATA+FIELD_OFFSET(struct Imports, thunks))
60
61 static struct _PeImage {
62 IMAGE_DOS_HEADER dos_header;
63 char __alignment1[FILE_PE_START - sizeof(IMAGE_DOS_HEADER)];
64 IMAGE_NT_HEADERS32 nt_headers;
65 IMAGE_SECTION_HEADER sections[NUM_SECTIONS];
66 char __alignment2[FILE_TEXT - FILE_PE_START - sizeof(IMAGE_NT_HEADERS32) -
67 NUM_SECTIONS * sizeof(IMAGE_SECTION_HEADER)];
68 unsigned char text_section[FILE_IDATA-FILE_TEXT];
69 struct Imports idata_section;
70 char __alignment3[FILE_TOTAL-FILE_IDATA-sizeof(struct Imports)];
71 } bin = {
72 /* dos header */
73 {IMAGE_DOS_SIGNATURE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0}, 0, 0, {0}, FILE_PE_START},
74 /* alignment before PE header */
75 {0},
76 /* nt headers */
77 {IMAGE_NT_SIGNATURE,
78 /* basic headers - 3 sections, no symbols, EXE file */
79 {IMAGE_FILE_MACHINE_I386, NUM_SECTIONS, 0, 0, 0, sizeof(IMAGE_OPTIONAL_HEADER32),
80 IMAGE_FILE_32BIT_MACHINE | IMAGE_FILE_EXECUTABLE_IMAGE},
81 /* optional header */
82 {IMAGE_NT_OPTIONAL_HDR32_MAGIC, 4, 0, FILE_IDATA-FILE_TEXT,
83 FILE_TOTAL-FILE_IDATA + FILE_IDATA-FILE_TEXT, 0x400,
84 RVA_TEXT, RVA_TEXT, RVA_BSS, VA_START, 0x1000, 0x200, 4, 0, 1, 0, 4, 0, 0,
85 RVA_TOTAL, FILE_TEXT, 0, IMAGE_SUBSYSTEM_WINDOWS_GUI, 0,
86 0x200000, 0x1000, 0x100000, 0x1000, 0, 0x10,
87 {{0, 0},
88 {RVA_IDATA, sizeof(struct Imports)}
89 }
90 }
91 },
92 /* sections */
93 {
94 {".text", {0x100}, RVA_TEXT, FILE_IDATA-FILE_TEXT, FILE_TEXT,
95 0, 0, 0, 0, IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ},
96 {".bss", {0x400}, RVA_BSS, 0, 0, 0, 0, 0, 0,
97 IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE},
98 {".idata", {sizeof(struct Imports)}, RVA_IDATA, FILE_TOTAL-FILE_IDATA, FILE_IDATA, 0,
99 0, 0, 0, IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE}
100 },
101 /* alignment before first section */
102 {0},
103 /* .text section */
104 {
105 0x31, 0xC0, /* xor eax, eax */
106 0xFF, 0x25, EXIT_PROCESS&0xFF, (EXIT_PROCESS>>8)&0xFF, (EXIT_PROCESS>>16)&0xFF,
107 (EXIT_PROCESS>>24)&0xFF, /* jmp ExitProcess */
108 0
109 },
110 /* .idata section */
111 {
112 {
113 {{RVA_IDATA + FIELD_OFFSET(struct Imports, original_thunks)}, 0, 0,
114 RVA_IDATA + FIELD_OFFSET(struct Imports, dllname),
115 RVA_IDATA + FIELD_OFFSET(struct Imports, thunks)
116 },
117 {{0}, 0, 0, 0, 0}
118 },
119 {{{RVA_IDATA+FIELD_OFFSET(struct Imports, ibn)}}, {{0}}},
120 {{{RVA_IDATA+FIELD_OFFSET(struct Imports, ibn)}}, {{0}}},
121 {0,"ExitProcess"},
122 "KERNEL32.DLL"
123 },
124 /* final alignment */
125 {0}
126 };
127 #include <poppack.h>
128
129 struct blob
130 {
131 DWORD cb;
132 BYTE *pb;
133 };
134
135 struct expected_blob
136 {
137 DWORD cb;
138 const void *pb;
139 };
140
141 struct update_accum
142 {
143 DWORD cUpdates;
144 struct blob *updates;
145 };
146
147 struct expected_update_accum
148 {
149 DWORD cUpdates;
150 const struct expected_blob *updates;
151 BOOL todo;
152 };
153
154 static int status_routine_called[BindSymbolsNotUpdated+1];
155
156
157 static BOOL WINAPI accumulating_stream_output(DIGEST_HANDLE handle, BYTE *pb,
158 DWORD cb)
159 {
160 struct update_accum *accum = (struct update_accum *)handle;
161 BOOL ret = FALSE;
162
163 if (accum->cUpdates)
164 accum->updates = HeapReAlloc(GetProcessHeap(), 0, accum->updates,
165 (accum->cUpdates + 1) * sizeof(struct blob));
166 else
167 accum->updates = HeapAlloc(GetProcessHeap(), 0, sizeof(struct blob));
168 if (accum->updates)
169 {
170 struct blob *blob = &accum->updates[accum->cUpdates];
171
172 blob->pb = HeapAlloc(GetProcessHeap(), 0, cb);
173 if (blob->pb)
174 {
175 memcpy(blob->pb, pb, cb);
176 blob->cb = cb;
177 ret = TRUE;
178 }
179 accum->cUpdates++;
180 }
181 return ret;
182 }
183
184 static void check_updates(LPCSTR header, const struct expected_update_accum *expected,
185 const struct update_accum *got)
186 {
187 DWORD i;
188
189 if (expected->todo)
190 todo_wine ok(expected->cUpdates == got->cUpdates, "%s: expected %d updates, got %d\n",
191 header, expected->cUpdates, got->cUpdates);
192 else
193 ok(expected->cUpdates == got->cUpdates, "%s: expected %d updates, got %d\n",
194 header, expected->cUpdates, got->cUpdates);
195 for (i = 0; i < min(expected->cUpdates, got->cUpdates); i++)
196 {
197 ok(expected->updates[i].cb == got->updates[i].cb, "%s, update %d: expected %d bytes, got %d\n",
198 header, i, expected->updates[i].cb, got->updates[i].cb);
199 if (expected->updates[i].cb && expected->updates[i].cb == got->updates[i].cb)
200 ok(!memcmp(expected->updates[i].pb, got->updates[i].pb, got->updates[i].cb),
201 "%s, update %d: unexpected value\n", header, i);
202 }
203 }
204
205 /* Frees the updates stored in accum */
206 static void free_updates(struct update_accum *accum)
207 {
208 DWORD i;
209
210 for (i = 0; i < accum->cUpdates; i++)
211 HeapFree(GetProcessHeap(), 0, accum->updates[i].pb);
212 HeapFree(GetProcessHeap(), 0, accum->updates);
213 accum->updates = NULL;
214 accum->cUpdates = 0;
215 }
216
217 static const struct expected_blob b1[] = {
218 {FILE_PE_START, &bin},
219 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
220 {sizeof(bin.nt_headers), &bin.nt_headers},
221 {sizeof(bin.sections), &bin.sections},
222 {FILE_IDATA-FILE_TEXT, &bin.text_section},
223 {sizeof(bin.idata_section.descriptors[0].u.OriginalFirstThunk),
224 &bin.idata_section.descriptors[0].u.OriginalFirstThunk},
225 {FIELD_OFFSET(struct Imports, thunks)-
226 (FIELD_OFFSET(struct Imports, descriptors)+FIELD_OFFSET(IMAGE_IMPORT_DESCRIPTOR, Name)),
227 &bin.idata_section.descriptors[0].Name},
228 {FILE_TOTAL-FILE_IDATA-FIELD_OFFSET(struct Imports, ibn),
229 &bin.idata_section.ibn}
230 };
231 static const struct expected_update_accum a1 = { sizeof(b1) / sizeof(b1[0]), b1, TRUE };
232
233 static const struct expected_blob b2[] = {
234 {FILE_PE_START, &bin},
235 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
236 {sizeof(bin.nt_headers), &bin.nt_headers},
237 {sizeof(bin.sections), &bin.sections},
238 {FILE_IDATA-FILE_TEXT, &bin.text_section},
239 {FILE_TOTAL-FILE_IDATA, &bin.idata_section}
240 };
241 static const struct expected_update_accum a2 = { sizeof(b2) / sizeof(b2[0]), b2, FALSE };
242
243 /* Creates a test file and returns a handle to it. The file's path is returned
244 * in temp_file, which must be at least MAX_PATH characters in length.
245 */
246 static HANDLE create_temp_file(char *temp_file)
247 {
248 HANDLE file = INVALID_HANDLE_VALUE;
249 char temp_path[MAX_PATH];
250
251 if (GetTempPathA(sizeof(temp_path), temp_path))
252 {
253 if (GetTempFileNameA(temp_path, "img", 0, temp_file))
254 file = CreateFileA(temp_file, GENERIC_READ | GENERIC_WRITE, 0, NULL,
255 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
256 }
257 return file;
258 }
259
260 static void update_checksum(void)
261 {
262 WORD const * ptr;
263 DWORD size;
264 DWORD sum = 0;
265
266 bin.nt_headers.OptionalHeader.CheckSum = 0;
267
268 for(ptr = (WORD const *)&bin, size = (sizeof(bin)+1)/sizeof(WORD); size > 0; ptr++, size--)
269 {
270 sum += *ptr;
271 if (HIWORD(sum) != 0)
272 {
273 sum = LOWORD(sum) + HIWORD(sum);
274 }
275 }
276 sum = (WORD)(LOWORD(sum) + HIWORD(sum));
277 sum += sizeof(bin);
278
279 bin.nt_headers.OptionalHeader.CheckSum = sum;
280 }
281
282 static BOOL CALLBACK testing_status_routine(IMAGEHLP_STATUS_REASON reason, const char *ImageName,
283 const char *DllName, ULONG_PTR Va, ULONG_PTR Parameter)
284 {
285 char kernel32_path[MAX_PATH];
286
287 if (0 <= (int)reason && reason <= BindSymbolsNotUpdated)
288 status_routine_called[reason]++;
289 else
290 ok(0, "expected reason between 0 and %d, got %d\n", BindSymbolsNotUpdated+1, reason);
291
292 switch(reason)
293 {
294 case BindImportModule:
295 ok(!strcmp(DllName, "KERNEL32.DLL"), "expected DllName to be KERNEL32.DLL, got %s\n",
296 DllName);
297 break;
298
299 case BindImportProcedure:
300 case BindForwarderNOT:
301 GetSystemDirectoryA(kernel32_path, MAX_PATH);
302 strcat(kernel32_path, "\\KERNEL32.DLL");
303 ok(!lstrcmpiA(DllName, kernel32_path), "expected DllName to be %s, got %s\n",
304 kernel32_path, DllName);
305 ok(!strcmp((char *)Parameter, "ExitProcess"),
306 "expected Parameter to be ExitProcess, got %s\n", (char *)Parameter);
307 break;
308
309 default:
310 ok(0, "got unexpected reason %d\n", reason);
311 break;
312 }
313 return TRUE;
314 }
315
316 static void test_get_digest_stream(void)
317 {
318 BOOL ret;
319 HANDLE file;
320 char temp_file[MAX_PATH];
321 DWORD count;
322 struct update_accum accum = { 0, NULL };
323
324 if (!pImageGetDigestStream)
325 {
326 win_skip("ImageGetDigestStream function is not available\n");
327 return;
328 }
329 SetLastError(0xdeadbeef);
330 ret = pImageGetDigestStream(NULL, 0, NULL, NULL);
331 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
332 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
333 file = create_temp_file(temp_file);
334 if (file == INVALID_HANDLE_VALUE)
335 {
336 skip("couldn't create temp file\n");
337 return;
338 }
339 SetLastError(0xdeadbeef);
340 ret = pImageGetDigestStream(file, 0, NULL, NULL);
341 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
342 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
343 SetLastError(0xdeadbeef);
344 ret = pImageGetDigestStream(NULL, 0, accumulating_stream_output, &accum);
345 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
346 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
347 /* Even with "valid" parameters, it fails with an empty file */
348 SetLastError(0xdeadbeef);
349 ret = pImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
350 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
351 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
352 /* Finally, with a valid executable in the file, it succeeds. Note that
353 * the file pointer need not be positioned at the beginning.
354 */
355 update_checksum();
356 WriteFile(file, &bin, sizeof(bin), &count, NULL);
357 FlushFileBuffers(file);
358
359 /* zero out some fields ImageGetDigestStream would zero out */
360 bin.nt_headers.OptionalHeader.CheckSum = 0;
361 bin.nt_headers.OptionalHeader.SizeOfInitializedData = 0;
362 bin.nt_headers.OptionalHeader.SizeOfImage = 0;
363
364 ret = pImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
365 ok(ret, "ImageGetDigestStream failed: %d\n", GetLastError());
366 check_updates("flags = 0", &a1, &accum);
367 free_updates(&accum);
368 ret = pImageGetDigestStream(file, CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO,
369 accumulating_stream_output, &accum);
370 ok(ret, "ImageGetDigestStream failed: %d\n", GetLastError());
371 check_updates("flags = CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO", &a2, &accum);
372 free_updates(&accum);
373 CloseHandle(file);
374 DeleteFileA(temp_file);
375 }
376
377 static void test_bind_image_ex(void)
378 {
379 BOOL ret;
380 HANDLE file;
381 char temp_file[MAX_PATH];
382 DWORD count;
383
384 if (!pBindImageEx)
385 {
386 win_skip("BindImageEx function is not available\n");
387 return;
388 }
389
390 /* call with a non-existent file */
391 SetLastError(0xdeadbeef);
392 ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, "nonexistent.dll", 0, 0,
393 testing_status_routine);
394 ok(!ret && ((GetLastError() == ERROR_FILE_NOT_FOUND) ||
395 (GetLastError() == ERROR_INVALID_PARAMETER)),
396 "expected ERROR_FILE_NOT_FOUND or ERROR_INVALID_PARAMETER, got %d\n",
397 GetLastError());
398
399 file = create_temp_file(temp_file);
400 if (file == INVALID_HANDLE_VALUE)
401 {
402 skip("couldn't create temp file\n");
403 return;
404 }
405
406 WriteFile(file, &bin, sizeof(bin), &count, NULL);
407 CloseHandle(file);
408
409 /* call with a proper PE file, but with StatusRoutine set to NULL */
410 ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, temp_file, 0, 0,
411 NULL);
412 ok(ret, "BindImageEx failed: %d\n", GetLastError());
413
414 /* call with a proper PE file and StatusRoutine */
415 ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, temp_file, 0, 0,
416 testing_status_routine);
417 ok(ret, "BindImageEx failed: %d\n", GetLastError());
418
419 ok(status_routine_called[BindImportModule] == 1,
420 "StatusRoutine was called %d times\n", status_routine_called[BindImportModule]);
421
422 ok((status_routine_called[BindImportProcedure] == 1)
423 #if defined(_WIN64)
424 || broken(status_routine_called[BindImportProcedure] == 0) /* < Win8 */
425 #endif
426 , "StatusRoutine was called %d times\n", status_routine_called[BindImportProcedure]);
427
428 DeleteFileA(temp_file);
429 }
430
431 START_TEST(image)
432 {
433 hImageHlp = LoadLibraryA("imagehlp.dll");
434
435 if (!hImageHlp)
436 {
437 win_skip("ImageHlp unavailable\n");
438 return;
439 }
440
441 pImageGetDigestStream = (void *) GetProcAddress(hImageHlp, "ImageGetDigestStream");
442 pBindImageEx = (void *) GetProcAddress(hImageHlp, "BindImageEx");
443
444 test_get_digest_stream();
445 test_bind_image_ex();
446
447 FreeLibrary(hImageHlp);
448 }