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
22 #define NONAMELESSUNION
29 #include "wine/test.h"
31 static HMODULE hImageHlp
;
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
);
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
50 IMAGE_IMPORT_DESCRIPTOR descriptors
[2];
51 IMAGE_THUNK_DATA32 original_thunks
[2];
52 IMAGE_THUNK_DATA32 thunks
[2];
53 struct __IMPORT_BY_NAME
{
59 #define EXIT_PROCESS (VA_START+RVA_IDATA+FIELD_OFFSET(struct Imports, thunks[0]))
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
)];
73 {IMAGE_DOS_SIGNATURE
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {}, 0, 0, {}, FILE_PE_START
},
74 /* alignment before PE header */
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
},
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,
88 {RVA_IDATA
, sizeof(struct Imports
)}
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
}
101 /* alignment before first section */
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 */
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
)
119 {{{RVA_IDATA
+FIELD_OFFSET(struct Imports
, ibn
)}}, {{0}}},
120 {{{RVA_IDATA
+FIELD_OFFSET(struct Imports
, ibn
)}}, {{0}}},
124 /* final alignment */
144 struct blob
*updates
;
147 struct expected_update_accum
150 const struct expected_blob
*updates
;
154 static int status_routine_called
[BindSymbolsNotUpdated
+1];
157 static BOOL WINAPI
accumulating_stream_output(DIGEST_HANDLE handle
, BYTE
*pb
,
160 struct update_accum
*accum
= (struct update_accum
*)handle
;
164 accum
->updates
= HeapReAlloc(GetProcessHeap(), 0, accum
->updates
,
165 (accum
->cUpdates
+ 1) * sizeof(struct blob
));
167 accum
->updates
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct blob
));
170 struct blob
*blob
= &accum
->updates
[accum
->cUpdates
];
172 blob
->pb
= HeapAlloc(GetProcessHeap(), 0, cb
);
175 memcpy(blob
->pb
, pb
, cb
);
184 static void check_updates(LPCSTR header
, const struct expected_update_accum
*expected
,
185 const struct update_accum
*got
)
190 todo_wine
ok(expected
->cUpdates
== got
->cUpdates
, "%s: expected %d updates, got %d\n",
191 header
, expected
->cUpdates
, got
->cUpdates
);
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
++)
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
);
205 /* Frees the updates stored in accum */
206 static void free_updates(struct update_accum
*accum
)
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
;
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
)-FIELD_OFFSET(struct Imports
, descriptors
[0].Name
),
226 &bin
.idata_section
.descriptors
[0].Name
},
227 {FILE_TOTAL
-FILE_IDATA
-FIELD_OFFSET(struct Imports
, ibn
),
228 &bin
.idata_section
.ibn
}
230 static const struct expected_update_accum a1
= { sizeof(b1
) / sizeof(b1
[0]), b1
, TRUE
};
232 static const struct expected_blob b2
[] = {
233 {FILE_PE_START
, &bin
},
234 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
235 {sizeof(bin
.nt_headers
), &bin
.nt_headers
},
236 {sizeof(bin
.sections
), &bin
.sections
},
237 {FILE_IDATA
-FILE_TEXT
, &bin
.text_section
},
238 {FILE_TOTAL
-FILE_IDATA
, &bin
.idata_section
}
240 static const struct expected_update_accum a2
= { sizeof(b2
) / sizeof(b2
[0]), b2
, FALSE
};
242 /* Creates a test file and returns a handle to it. The file's path is returned
243 * in temp_file, which must be at least MAX_PATH characters in length.
245 static HANDLE
create_temp_file(char *temp_file
)
247 HANDLE file
= INVALID_HANDLE_VALUE
;
248 char temp_path
[MAX_PATH
];
250 if (GetTempPathA(sizeof(temp_path
), temp_path
))
252 if (GetTempFileNameA(temp_path
, "img", 0, temp_file
))
253 file
= CreateFileA(temp_file
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
254 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
259 static void update_checksum(void)
265 bin
.nt_headers
.OptionalHeader
.CheckSum
= 0;
267 for(ptr
= (WORD
const *)&bin
, size
= (sizeof(bin
)+1)/sizeof(WORD
); size
> 0; ptr
++, size
--)
270 if (HIWORD(sum
) != 0)
272 sum
= LOWORD(sum
) + HIWORD(sum
);
275 sum
= (WORD
)(LOWORD(sum
) + HIWORD(sum
));
278 bin
.nt_headers
.OptionalHeader
.CheckSum
= sum
;
281 static BOOL CALLBACK
testing_status_routine(IMAGEHLP_STATUS_REASON reason
, const char *ImageName
,
282 const char *DllName
, ULONG_PTR Va
, ULONG_PTR Parameter
)
284 char kernel32_path
[MAX_PATH
];
286 if (0 <= (int)reason
&& reason
<= BindSymbolsNotUpdated
)
287 status_routine_called
[reason
]++;
289 ok(0, "expected reason between 0 and %d, got %d\n", BindSymbolsNotUpdated
+1, reason
);
293 case BindImportModule
:
294 ok(!strcmp(DllName
, "KERNEL32.DLL"), "expected DllName to be KERNEL32.DLL, got %s\n",
298 case BindImportProcedure
:
299 case BindForwarderNOT
:
300 GetSystemDirectoryA(kernel32_path
, MAX_PATH
);
301 strcat(kernel32_path
, "\\KERNEL32.DLL");
302 ok(!lstrcmpiA(DllName
, kernel32_path
), "expected DllName to be %s, got %s\n",
303 kernel32_path
, DllName
);
304 ok(!strcmp((char *)Parameter
, "ExitProcess"),
305 "expected Parameter to be ExitProcess, got %s\n", (char *)Parameter
);
309 ok(0, "got unexpected reason %d\n", reason
);
315 static void test_get_digest_stream(void)
319 char temp_file
[MAX_PATH
];
321 struct update_accum accum
= { 0, NULL
};
323 if (!pImageGetDigestStream
)
325 win_skip("ImageGetDigestStream function is not available\n");
328 SetLastError(0xdeadbeef);
329 ret
= pImageGetDigestStream(NULL
, 0, NULL
, NULL
);
330 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
331 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
332 file
= create_temp_file(temp_file
);
333 if (file
== INVALID_HANDLE_VALUE
)
335 skip("couldn't create temp file\n");
338 SetLastError(0xdeadbeef);
339 ret
= pImageGetDigestStream(file
, 0, NULL
, NULL
);
340 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
341 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
342 SetLastError(0xdeadbeef);
343 ret
= pImageGetDigestStream(NULL
, 0, accumulating_stream_output
, &accum
);
344 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
345 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
346 /* Even with "valid" parameters, it fails with an empty file */
347 SetLastError(0xdeadbeef);
348 ret
= pImageGetDigestStream(file
, 0, accumulating_stream_output
, &accum
);
349 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
350 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
351 /* Finally, with a valid executable in the file, it succeeds. Note that
352 * the file pointer need not be positioned at the beginning.
355 WriteFile(file
, &bin
, sizeof(bin
), &count
, NULL
);
356 FlushFileBuffers(file
);
358 /* zero out some fields ImageGetDigestStream would zero out */
359 bin
.nt_headers
.OptionalHeader
.CheckSum
= 0;
360 bin
.nt_headers
.OptionalHeader
.SizeOfInitializedData
= 0;
361 bin
.nt_headers
.OptionalHeader
.SizeOfImage
= 0;
363 ret
= pImageGetDigestStream(file
, 0, accumulating_stream_output
, &accum
);
364 ok(ret
, "ImageGetDigestStream failed: %d\n", GetLastError());
365 check_updates("flags = 0", &a1
, &accum
);
366 free_updates(&accum
);
367 ret
= pImageGetDigestStream(file
, CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO
,
368 accumulating_stream_output
, &accum
);
369 ok(ret
, "ImageGetDigestStream failed: %d\n", GetLastError());
370 check_updates("flags = CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO", &a2
, &accum
);
371 free_updates(&accum
);
373 DeleteFileA(temp_file
);
376 static void test_bind_image_ex(void)
380 char temp_file
[MAX_PATH
];
385 win_skip("BindImageEx function is not available\n");
389 /* call with a non-existent file */
390 SetLastError(0xdeadbeef);
391 ret
= pBindImageEx(BIND_NO_BOUND_IMPORTS
| BIND_NO_UPDATE
| BIND_ALL_IMAGES
, "nonexistent.dll", 0, 0,
392 testing_status_routine
);
393 todo_wine
ok(!ret
&& ((GetLastError() == ERROR_FILE_NOT_FOUND
) ||
394 (GetLastError() == ERROR_INVALID_PARAMETER
)),
395 "expected ERROR_FILE_NOT_FOUND or ERROR_INVALID_PARAMETER, got %d\n",
398 file
= create_temp_file(temp_file
);
399 if (file
== INVALID_HANDLE_VALUE
)
401 skip("couldn't create temp file\n");
405 WriteFile(file
, &bin
, sizeof(bin
), &count
, NULL
);
408 /* call with a proper PE file, but with StatusRoutine set to NULL */
409 ret
= pBindImageEx(BIND_NO_BOUND_IMPORTS
| BIND_NO_UPDATE
| BIND_ALL_IMAGES
, temp_file
, 0, 0,
411 ok(ret
, "BindImageEx failed: %d\n", GetLastError());
413 /* call with a proper PE file and StatusRoutine */
414 ret
= pBindImageEx(BIND_NO_BOUND_IMPORTS
| BIND_NO_UPDATE
| BIND_ALL_IMAGES
, temp_file
, 0, 0,
415 testing_status_routine
);
416 ok(ret
, "BindImageEx failed: %d\n", GetLastError());
418 todo_wine
ok(status_routine_called
[BindImportModule
] == 1,
419 "StatusRoutine was called %d times\n", status_routine_called
[BindImportModule
]);
421 todo_wine
ok((status_routine_called
[BindImportProcedure
] == 1)
423 || broken(status_routine_called
[BindImportProcedure
] == 0) /* < Win8 */
425 , "StatusRoutine was called %d times\n", status_routine_called
[BindImportProcedure
]);
427 DeleteFileA(temp_file
);
432 hImageHlp
= LoadLibraryA("imagehlp.dll");
436 win_skip("ImageHlp unavailable\n");
440 pImageGetDigestStream
= (void *) GetProcAddress(hImageHlp
, "ImageGetDigestStream");
441 pBindImageEx
= (void *) GetProcAddress(hImageHlp
, "BindImageEx");
443 test_get_digest_stream();
444 test_bind_image_ex();
446 FreeLibrary(hImageHlp
);