[IMAGEHLP_WINETEST] Un-mark a ROS-diff that's been committed upstream.
[reactos.git] / modules / 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 todo_wine_if (expected->todo)
190 ok(expected->cUpdates == got->cUpdates, "%s: expected %d updates, got %d\n",
191 header, expected->cUpdates, got->cUpdates);
192 for (i = 0; i < min(expected->cUpdates, got->cUpdates); i++)
193 {
194 ok(expected->updates[i].cb == got->updates[i].cb, "%s, update %d: expected %d bytes, got %d\n",
195 header, i, expected->updates[i].cb, got->updates[i].cb);
196 if (expected->updates[i].cb && expected->updates[i].cb == got->updates[i].cb)
197 ok(!memcmp(expected->updates[i].pb, got->updates[i].pb, got->updates[i].cb),
198 "%s, update %d: unexpected value\n", header, i);
199 }
200 }
201
202 /* Frees the updates stored in accum */
203 static void free_updates(struct update_accum *accum)
204 {
205 DWORD i;
206
207 for (i = 0; i < accum->cUpdates; i++)
208 HeapFree(GetProcessHeap(), 0, accum->updates[i].pb);
209 HeapFree(GetProcessHeap(), 0, accum->updates);
210 accum->updates = NULL;
211 accum->cUpdates = 0;
212 }
213
214 static const struct expected_blob b1[] = {
215 {FILE_PE_START, &bin},
216 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
217 {sizeof(bin.nt_headers), &bin.nt_headers},
218 {sizeof(bin.sections), &bin.sections},
219 {FILE_IDATA-FILE_TEXT, &bin.text_section},
220 {sizeof(bin.idata_section.descriptors[0].u.OriginalFirstThunk),
221 &bin.idata_section.descriptors[0].u.OriginalFirstThunk},
222 {FIELD_OFFSET(struct Imports, thunks)-
223 (FIELD_OFFSET(struct Imports, descriptors)+FIELD_OFFSET(IMAGE_IMPORT_DESCRIPTOR, Name)),
224 &bin.idata_section.descriptors[0].Name},
225 {FILE_TOTAL-FILE_IDATA-FIELD_OFFSET(struct Imports, ibn),
226 &bin.idata_section.ibn}
227 };
228 static const struct expected_update_accum a1 = { sizeof(b1) / sizeof(b1[0]), b1, TRUE };
229
230 static const struct expected_blob b2[] = {
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 {FILE_TOTAL-FILE_IDATA, &bin.idata_section}
237 };
238 static const struct expected_update_accum a2 = { sizeof(b2) / sizeof(b2[0]), b2, FALSE };
239
240 /* Creates a test file and returns a handle to it. The file's path is returned
241 * in temp_file, which must be at least MAX_PATH characters in length.
242 */
243 static HANDLE create_temp_file(char *temp_file)
244 {
245 HANDLE file = INVALID_HANDLE_VALUE;
246 char temp_path[MAX_PATH];
247
248 if (GetTempPathA(sizeof(temp_path), temp_path))
249 {
250 if (GetTempFileNameA(temp_path, "img", 0, temp_file))
251 file = CreateFileA(temp_file, GENERIC_READ | GENERIC_WRITE, 0, NULL,
252 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
253 }
254 return file;
255 }
256
257 static void update_checksum(void)
258 {
259 WORD const * ptr;
260 DWORD size;
261 DWORD sum = 0;
262
263 bin.nt_headers.OptionalHeader.CheckSum = 0;
264
265 for(ptr = (WORD const *)&bin, size = (sizeof(bin)+1)/sizeof(WORD); size > 0; ptr++, size--)
266 {
267 sum += *ptr;
268 if (HIWORD(sum) != 0)
269 {
270 sum = LOWORD(sum) + HIWORD(sum);
271 }
272 }
273 sum = (WORD)(LOWORD(sum) + HIWORD(sum));
274 sum += sizeof(bin);
275
276 bin.nt_headers.OptionalHeader.CheckSum = sum;
277 }
278
279 static BOOL CALLBACK testing_status_routine(IMAGEHLP_STATUS_REASON reason, const char *ImageName,
280 const char *DllName, ULONG_PTR Va, ULONG_PTR Parameter)
281 {
282 char kernel32_path[MAX_PATH];
283
284 if (0 <= (int)reason && reason <= BindSymbolsNotUpdated)
285 status_routine_called[reason]++;
286 else
287 ok(0, "expected reason between 0 and %d, got %d\n", BindSymbolsNotUpdated+1, reason);
288
289 switch(reason)
290 {
291 case BindImportModule:
292 ok(!strcmp(DllName, "KERNEL32.DLL"), "expected DllName to be KERNEL32.DLL, got %s\n",
293 DllName);
294 break;
295
296 case BindImportProcedure:
297 case BindForwarderNOT:
298 GetSystemDirectoryA(kernel32_path, MAX_PATH);
299 strcat(kernel32_path, "\\KERNEL32.DLL");
300 ok(!lstrcmpiA(DllName, kernel32_path), "expected DllName to be %s, got %s\n",
301 kernel32_path, DllName);
302 ok(!strcmp((char *)Parameter, "ExitProcess"),
303 "expected Parameter to be ExitProcess, got %s\n", (char *)Parameter);
304 break;
305
306 default:
307 ok(0, "got unexpected reason %d\n", reason);
308 break;
309 }
310 return TRUE;
311 }
312
313 static void test_get_digest_stream(void)
314 {
315 BOOL ret;
316 HANDLE file;
317 char temp_file[MAX_PATH];
318 DWORD count;
319 struct update_accum accum = { 0, NULL };
320
321 if (!pImageGetDigestStream)
322 {
323 win_skip("ImageGetDigestStream function is not available\n");
324 return;
325 }
326 SetLastError(0xdeadbeef);
327 ret = pImageGetDigestStream(NULL, 0, NULL, NULL);
328 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
329 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
330 file = create_temp_file(temp_file);
331 if (file == INVALID_HANDLE_VALUE)
332 {
333 skip("couldn't create temp file\n");
334 return;
335 }
336 SetLastError(0xdeadbeef);
337 ret = pImageGetDigestStream(file, 0, NULL, NULL);
338 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
339 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
340 SetLastError(0xdeadbeef);
341 ret = pImageGetDigestStream(NULL, 0, accumulating_stream_output, &accum);
342 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
343 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
344 /* Even with "valid" parameters, it fails with an empty file */
345 SetLastError(0xdeadbeef);
346 ret = pImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
347 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
348 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
349 /* Finally, with a valid executable in the file, it succeeds. Note that
350 * the file pointer need not be positioned at the beginning.
351 */
352 update_checksum();
353 WriteFile(file, &bin, sizeof(bin), &count, NULL);
354 FlushFileBuffers(file);
355
356 /* zero out some fields ImageGetDigestStream would zero out */
357 bin.nt_headers.OptionalHeader.CheckSum = 0;
358 bin.nt_headers.OptionalHeader.SizeOfInitializedData = 0;
359 bin.nt_headers.OptionalHeader.SizeOfImage = 0;
360
361 ret = pImageGetDigestStream(file, 0, accumulating_stream_output, &accum);
362 ok(ret, "ImageGetDigestStream failed: %d\n", GetLastError());
363 check_updates("flags = 0", &a1, &accum);
364 free_updates(&accum);
365 ret = pImageGetDigestStream(file, CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO,
366 accumulating_stream_output, &accum);
367 ok(ret, "ImageGetDigestStream failed: %d\n", GetLastError());
368 check_updates("flags = CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO", &a2, &accum);
369 free_updates(&accum);
370 CloseHandle(file);
371 DeleteFileA(temp_file);
372 }
373
374 static void test_bind_image_ex(void)
375 {
376 BOOL ret;
377 HANDLE file;
378 char temp_file[MAX_PATH];
379 DWORD count;
380
381 if (!pBindImageEx)
382 {
383 win_skip("BindImageEx function is not available\n");
384 return;
385 }
386
387 /* call with a non-existent file */
388 SetLastError(0xdeadbeef);
389 ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, "nonexistent.dll", 0, 0,
390 testing_status_routine);
391 todo_wine ok(!ret && ((GetLastError() == ERROR_FILE_NOT_FOUND) ||
392 (GetLastError() == ERROR_INVALID_PARAMETER)),
393 "expected ERROR_FILE_NOT_FOUND or ERROR_INVALID_PARAMETER, got %d\n",
394 GetLastError());
395
396 file = create_temp_file(temp_file);
397 if (file == INVALID_HANDLE_VALUE)
398 {
399 skip("couldn't create temp file\n");
400 return;
401 }
402
403 WriteFile(file, &bin, sizeof(bin), &count, NULL);
404 CloseHandle(file);
405
406 /* call with a proper PE file, but with StatusRoutine set to NULL */
407 ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, temp_file, 0, 0,
408 NULL);
409 ok(ret, "BindImageEx failed: %d\n", GetLastError());
410
411 /* call with a proper PE file and StatusRoutine */
412 ret = pBindImageEx(BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES, temp_file, 0, 0,
413 testing_status_routine);
414 ok(ret, "BindImageEx failed: %d\n", GetLastError());
415
416 todo_wine ok(status_routine_called[BindImportModule] == 1,
417 "StatusRoutine was called %d times\n", status_routine_called[BindImportModule]);
418
419 todo_wine ok((status_routine_called[BindImportProcedure] == 1)
420 #if defined(_WIN64)
421 || broken(status_routine_called[BindImportProcedure] == 0) /* < Win8 */
422 #endif
423 , "StatusRoutine was called %d times\n", status_routine_called[BindImportProcedure]);
424
425 DeleteFileA(temp_file);
426 }
427
428 START_TEST(image)
429 {
430 hImageHlp = LoadLibraryA("imagehlp.dll");
431
432 if (!hImageHlp)
433 {
434 win_skip("ImageHlp unavailable\n");
435 return;
436 }
437
438 pImageGetDigestStream = (void *) GetProcAddress(hImageHlp, "ImageGetDigestStream");
439 pBindImageEx = (void *) GetProcAddress(hImageHlp, "BindImageEx");
440
441 test_get_digest_stream();
442 test_bind_image_ex();
443
444 FreeLibrary(hImageHlp);
445 }