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
);
36 static DWORD (WINAPI
* pGetImageUnusedHeaderBytes
)(PLOADED_IMAGE
, LPDWORD
);
37 static PLOADED_IMAGE (WINAPI
* pImageLoad
)(PCSTR
, PCSTR
);
38 static BOOL (WINAPI
* pImageUnload
)(PLOADED_IMAGE
);
41 /* minimal PE file image */
42 #define VA_START 0x400000
43 #define FILE_PE_START 0x50
44 #define NUM_SECTIONS 3
45 #define FILE_TEXT 0x200
46 #define RVA_TEXT 0x1000
47 #define RVA_BSS 0x2000
48 #define FILE_IDATA 0x400
49 #define RVA_IDATA 0x3000
50 #define FILE_TOTAL 0x600
51 #define RVA_TOTAL 0x4000
54 IMAGE_IMPORT_DESCRIPTOR descriptors
[2];
55 IMAGE_THUNK_DATA32 original_thunks
[2];
56 IMAGE_THUNK_DATA32 thunks
[2];
57 struct __IMPORT_BY_NAME
{
63 #define EXIT_PROCESS (VA_START+RVA_IDATA+FIELD_OFFSET(struct Imports, thunks))
65 static struct _PeImage
{
66 IMAGE_DOS_HEADER dos_header
;
67 char __alignment1
[FILE_PE_START
- sizeof(IMAGE_DOS_HEADER
)];
68 IMAGE_NT_HEADERS32 nt_headers
;
69 IMAGE_SECTION_HEADER sections
[NUM_SECTIONS
];
70 char __alignment2
[FILE_TEXT
- FILE_PE_START
- sizeof(IMAGE_NT_HEADERS32
) -
71 NUM_SECTIONS
* sizeof(IMAGE_SECTION_HEADER
)];
72 unsigned char text_section
[FILE_IDATA
-FILE_TEXT
];
73 struct Imports idata_section
;
74 char __alignment3
[FILE_TOTAL
-FILE_IDATA
-sizeof(struct Imports
)];
77 {IMAGE_DOS_SIGNATURE
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0}, 0, 0, {0}, FILE_PE_START
},
78 /* alignment before PE header */
82 /* basic headers - 3 sections, no symbols, EXE file */
83 {IMAGE_FILE_MACHINE_I386
, NUM_SECTIONS
, 0, 0, 0, sizeof(IMAGE_OPTIONAL_HEADER32
),
84 IMAGE_FILE_32BIT_MACHINE
| IMAGE_FILE_EXECUTABLE_IMAGE
},
86 {IMAGE_NT_OPTIONAL_HDR32_MAGIC
, 4, 0, FILE_IDATA
-FILE_TEXT
,
87 FILE_TOTAL
-FILE_IDATA
+ FILE_IDATA
-FILE_TEXT
, 0x400,
88 RVA_TEXT
, RVA_TEXT
, RVA_BSS
, VA_START
, 0x1000, 0x200, 4, 0, 1, 0, 4, 0, 0,
89 RVA_TOTAL
, FILE_TEXT
, 0, IMAGE_SUBSYSTEM_WINDOWS_GUI
, 0,
90 0x200000, 0x1000, 0x100000, 0x1000, 0, 0x10,
92 {RVA_IDATA
, sizeof(struct Imports
)}
98 {".text", {0x100}, RVA_TEXT
, FILE_IDATA
-FILE_TEXT
, FILE_TEXT
,
99 0, 0, 0, 0, IMAGE_SCN_CNT_CODE
| IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
},
100 {".bss", {0x400}, RVA_BSS
, 0, 0, 0, 0, 0, 0,
101 IMAGE_SCN_CNT_UNINITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
},
102 {".idata", {sizeof(struct Imports
)}, RVA_IDATA
, FILE_TOTAL
-FILE_IDATA
, FILE_IDATA
, 0,
103 0, 0, 0, IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_WRITE
}
105 /* alignment before first section */
109 0x31, 0xC0, /* xor eax, eax */
110 0xFF, 0x25, EXIT_PROCESS
&0xFF, (EXIT_PROCESS
>>8)&0xFF, (EXIT_PROCESS
>>16)&0xFF,
111 (EXIT_PROCESS
>>24)&0xFF, /* jmp ExitProcess */
117 {{RVA_IDATA
+ FIELD_OFFSET(struct Imports
, original_thunks
)}, 0, 0,
118 RVA_IDATA
+ FIELD_OFFSET(struct Imports
, dllname
),
119 RVA_IDATA
+ FIELD_OFFSET(struct Imports
, thunks
)
123 {{{RVA_IDATA
+FIELD_OFFSET(struct Imports
, ibn
)}}, {{0}}},
124 {{{RVA_IDATA
+FIELD_OFFSET(struct Imports
, ibn
)}}, {{0}}},
128 /* final alignment */
148 struct blob
*updates
;
151 struct expected_update_accum
154 const struct expected_blob
*updates
;
158 static int status_routine_called
[BindSymbolsNotUpdated
+1];
161 static BOOL WINAPI
accumulating_stream_output(DIGEST_HANDLE handle
, BYTE
*pb
,
164 struct update_accum
*accum
= (struct update_accum
*)handle
;
168 accum
->updates
= HeapReAlloc(GetProcessHeap(), 0, accum
->updates
,
169 (accum
->cUpdates
+ 1) * sizeof(struct blob
));
171 accum
->updates
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct blob
));
174 struct blob
*blob
= &accum
->updates
[accum
->cUpdates
];
176 blob
->pb
= HeapAlloc(GetProcessHeap(), 0, cb
);
179 memcpy(blob
->pb
, pb
, cb
);
188 static void check_updates(LPCSTR header
, const struct expected_update_accum
*expected
,
189 const struct update_accum
*got
)
193 todo_wine_if (expected
->todo
)
194 ok(expected
->cUpdates
== got
->cUpdates
, "%s: expected %d updates, got %d\n",
195 header
, expected
->cUpdates
, got
->cUpdates
);
196 for (i
= 0; i
< min(expected
->cUpdates
, got
->cUpdates
); i
++)
198 ok(expected
->updates
[i
].cb
== got
->updates
[i
].cb
, "%s, update %d: expected %d bytes, got %d\n",
199 header
, i
, expected
->updates
[i
].cb
, got
->updates
[i
].cb
);
200 if (expected
->updates
[i
].cb
&& expected
->updates
[i
].cb
== got
->updates
[i
].cb
)
201 ok(!memcmp(expected
->updates
[i
].pb
, got
->updates
[i
].pb
, got
->updates
[i
].cb
),
202 "%s, update %d: unexpected value\n", header
, i
);
206 /* Frees the updates stored in accum */
207 static void free_updates(struct update_accum
*accum
)
211 for (i
= 0; i
< accum
->cUpdates
; i
++)
212 HeapFree(GetProcessHeap(), 0, accum
->updates
[i
].pb
);
213 HeapFree(GetProcessHeap(), 0, accum
->updates
);
214 accum
->updates
= NULL
;
218 static const struct expected_blob b1
[] = {
219 {FILE_PE_START
, &bin
},
220 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
221 {sizeof(bin
.nt_headers
), &bin
.nt_headers
},
222 {sizeof(bin
.sections
), &bin
.sections
},
223 {FILE_IDATA
-FILE_TEXT
, &bin
.text_section
},
224 {sizeof(bin
.idata_section
.descriptors
[0].u
.OriginalFirstThunk
),
225 &bin
.idata_section
.descriptors
[0].u
.OriginalFirstThunk
},
226 {FIELD_OFFSET(struct Imports
, thunks
)-
227 (FIELD_OFFSET(struct Imports
, descriptors
)+FIELD_OFFSET(IMAGE_IMPORT_DESCRIPTOR
, Name
)),
228 &bin
.idata_section
.descriptors
[0].Name
},
229 {FILE_TOTAL
-FILE_IDATA
-FIELD_OFFSET(struct Imports
, ibn
),
230 &bin
.idata_section
.ibn
}
232 static const struct expected_update_accum a1
= { sizeof(b1
) / sizeof(b1
[0]), b1
, TRUE
};
234 static const struct expected_blob b2
[] = {
235 {FILE_PE_START
, &bin
},
236 /* with zeroed Checksum/SizeOfInitializedData/SizeOfImage fields */
237 {sizeof(bin
.nt_headers
), &bin
.nt_headers
},
238 {sizeof(bin
.sections
), &bin
.sections
},
239 {FILE_IDATA
-FILE_TEXT
, &bin
.text_section
},
240 {FILE_TOTAL
-FILE_IDATA
, &bin
.idata_section
}
242 static const struct expected_update_accum a2
= { sizeof(b2
) / sizeof(b2
[0]), b2
, FALSE
};
244 /* Creates a test file and returns a handle to it. The file's path is returned
245 * in temp_file, which must be at least MAX_PATH characters in length.
247 static HANDLE
create_temp_file(char *temp_file
)
249 HANDLE file
= INVALID_HANDLE_VALUE
;
250 char temp_path
[MAX_PATH
];
252 if (GetTempPathA(sizeof(temp_path
), temp_path
))
254 if (GetTempFileNameA(temp_path
, "img", 0, temp_file
))
255 file
= CreateFileA(temp_file
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
256 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
261 static void update_checksum(void)
267 bin
.nt_headers
.OptionalHeader
.CheckSum
= 0;
269 for(ptr
= (WORD
const *)&bin
, size
= (sizeof(bin
)+1)/sizeof(WORD
); size
> 0; ptr
++, size
--)
272 if (HIWORD(sum
) != 0)
274 sum
= LOWORD(sum
) + HIWORD(sum
);
277 sum
= (WORD
)(LOWORD(sum
) + HIWORD(sum
));
280 bin
.nt_headers
.OptionalHeader
.CheckSum
= sum
;
283 static BOOL CALLBACK
testing_status_routine(IMAGEHLP_STATUS_REASON reason
, const char *ImageName
,
284 const char *DllName
, ULONG_PTR Va
, ULONG_PTR Parameter
)
286 char kernel32_path
[MAX_PATH
];
288 if (0 <= (int)reason
&& reason
<= BindSymbolsNotUpdated
)
289 status_routine_called
[reason
]++;
291 ok(0, "expected reason between 0 and %d, got %d\n", BindSymbolsNotUpdated
+1, reason
);
295 case BindImportModule
:
296 ok(!strcmp(DllName
, "KERNEL32.DLL"), "expected DllName to be KERNEL32.DLL, got %s\n",
300 case BindImportProcedure
:
301 case BindForwarderNOT
:
302 GetSystemDirectoryA(kernel32_path
, MAX_PATH
);
303 strcat(kernel32_path
, "\\KERNEL32.DLL");
304 ok(!lstrcmpiA(DllName
, kernel32_path
), "expected DllName to be %s, got %s\n",
305 kernel32_path
, DllName
);
306 ok(!strcmp((char *)Parameter
, "ExitProcess"),
307 "expected Parameter to be ExitProcess, got %s\n", (char *)Parameter
);
311 ok(0, "got unexpected reason %d\n", reason
);
317 static void test_get_digest_stream(void)
321 char temp_file
[MAX_PATH
];
323 struct update_accum accum
= { 0, NULL
};
325 if (!pImageGetDigestStream
)
327 win_skip("ImageGetDigestStream function is not available\n");
330 SetLastError(0xdeadbeef);
331 ret
= pImageGetDigestStream(NULL
, 0, NULL
, NULL
);
332 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
333 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
334 file
= create_temp_file(temp_file
);
335 if (file
== INVALID_HANDLE_VALUE
)
337 skip("couldn't create temp file\n");
340 SetLastError(0xdeadbeef);
341 ret
= pImageGetDigestStream(file
, 0, NULL
, NULL
);
342 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
343 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
344 SetLastError(0xdeadbeef);
345 ret
= pImageGetDigestStream(NULL
, 0, accumulating_stream_output
, &accum
);
346 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
347 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
348 /* Even with "valid" parameters, it fails with an empty file */
349 SetLastError(0xdeadbeef);
350 ret
= pImageGetDigestStream(file
, 0, accumulating_stream_output
, &accum
);
351 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
352 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
353 /* Finally, with a valid executable in the file, it succeeds. Note that
354 * the file pointer need not be positioned at the beginning.
357 WriteFile(file
, &bin
, sizeof(bin
), &count
, NULL
);
358 FlushFileBuffers(file
);
360 /* zero out some fields ImageGetDigestStream would zero out */
361 bin
.nt_headers
.OptionalHeader
.CheckSum
= 0;
362 bin
.nt_headers
.OptionalHeader
.SizeOfInitializedData
= 0;
363 bin
.nt_headers
.OptionalHeader
.SizeOfImage
= 0;
365 ret
= pImageGetDigestStream(file
, 0, accumulating_stream_output
, &accum
);
366 ok(ret
, "ImageGetDigestStream failed: %d\n", GetLastError());
367 check_updates("flags = 0", &a1
, &accum
);
368 free_updates(&accum
);
369 ret
= pImageGetDigestStream(file
, CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO
,
370 accumulating_stream_output
, &accum
);
371 ok(ret
, "ImageGetDigestStream failed: %d\n", GetLastError());
372 check_updates("flags = CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO", &a2
, &accum
);
373 free_updates(&accum
);
375 DeleteFileA(temp_file
);
378 static void test_bind_image_ex(void)
382 char temp_file
[MAX_PATH
];
387 win_skip("BindImageEx function is not available\n");
391 /* call with a non-existent file */
392 SetLastError(0xdeadbeef);
393 ret
= pBindImageEx(BIND_NO_BOUND_IMPORTS
| BIND_NO_UPDATE
| BIND_ALL_IMAGES
, "nonexistent.dll", 0, 0,
394 testing_status_routine
);
395 ok(!ret
&& ((GetLastError() == ERROR_FILE_NOT_FOUND
) ||
396 (GetLastError() == ERROR_INVALID_PARAMETER
)),
397 "expected ERROR_FILE_NOT_FOUND or ERROR_INVALID_PARAMETER, got %d\n",
400 file
= create_temp_file(temp_file
);
401 if (file
== INVALID_HANDLE_VALUE
)
403 skip("couldn't create temp file\n");
407 WriteFile(file
, &bin
, sizeof(bin
), &count
, NULL
);
410 /* call with a proper PE file, but with StatusRoutine set to NULL */
411 ret
= pBindImageEx(BIND_NO_BOUND_IMPORTS
| BIND_NO_UPDATE
| BIND_ALL_IMAGES
, temp_file
, 0, 0,
413 ok(ret
, "BindImageEx failed: %d\n", GetLastError());
415 /* call with a proper PE file and StatusRoutine */
416 ret
= pBindImageEx(BIND_NO_BOUND_IMPORTS
| BIND_NO_UPDATE
| BIND_ALL_IMAGES
, temp_file
, 0, 0,
417 testing_status_routine
);
418 ok(ret
, "BindImageEx failed: %d\n", GetLastError());
420 ok(status_routine_called
[BindImportModule
] == 1,
421 "StatusRoutine was called %d times\n", status_routine_called
[BindImportModule
]);
423 ok((status_routine_called
[BindImportProcedure
] == 1)
425 || broken(status_routine_called
[BindImportProcedure
] == 0) /* < Win8 */
427 , "StatusRoutine was called %d times\n", status_routine_called
[BindImportProcedure
]);
429 DeleteFileA(temp_file
);
432 static void test_image_load(void)
434 char temp_file
[MAX_PATH
];
439 if (!pImageLoad
|| !pImageUnload
)
441 win_skip("ImageLoad or ImageUnload function is not available\n");
444 if (!pGetImageUnusedHeaderBytes
)
446 win_skip("GetImageUnusedHeaderBytes function is not available\n");
450 file
= create_temp_file(temp_file
);
451 if (file
== INVALID_HANDLE_VALUE
)
453 skip("couldn't create temp file\n");
457 WriteFile(file
, &bin
, sizeof(bin
), &count
, NULL
);
460 img
= pImageLoad(temp_file
, NULL
);
461 ok(img
!= NULL
, "ImageLoad unexpectedly failed\n");
466 ok(!strcmp(img
->ModuleName
, temp_file
),
467 "unexpected ModuleName, got %s instead of %s\n", img
->ModuleName
, temp_file
);
469 ok(img
->MappedAddress
!= NULL
, "MappedAddress != NULL\n");
470 if (img
->MappedAddress
)
472 ok(!memcmp(img
->MappedAddress
, &bin
.dos_header
, sizeof(bin
.dos_header
)),
473 "MappedAddress doesn't point to IMAGE_DOS_HEADER\n");
475 ok(img
->FileHeader
!= NULL
, "FileHeader != NULL\n");
479 ok(!memcmp(img
->FileHeader
, &bin
.nt_headers
, sizeof(bin
.nt_headers
)),
480 "FileHeader doesn't point to IMAGE_NT_HEADERS32\n");
483 ok(img
->NumberOfSections
== 3,
484 "unexpected NumberOfSections, got %d instead of 3\n", img
->NumberOfSections
);
485 if (img
->NumberOfSections
>= 3)
487 ok(!strcmp((const char *)img
->Sections
[0].Name
, ".text"),
488 "unexpected name for section 0, expected .text, got %s\n",
489 (const char *)img
->Sections
[0].Name
);
490 ok(!strcmp((const char *)img
->Sections
[1].Name
, ".bss"),
491 "unexpected name for section 1, expected .bss, got %s\n",
492 (const char *)img
->Sections
[1].Name
);
493 ok(!strcmp((const char *)img
->Sections
[2].Name
, ".idata"),
494 "unexpected name for section 2, expected .idata, got %s\n",
495 (const char *)img
->Sections
[2].Name
);
498 ok(img
->Characteristics
== 0x102,
499 "unexpected Characteristics, got 0x%x instead of 0x102\n", img
->Characteristics
);
500 ok(img
->fSystemImage
== 0,
501 "unexpected fSystemImage, got %d instead of 0\n", img
->fSystemImage
);
502 ok(img
->fDOSImage
== 0,
503 "unexpected fDOSImage, got %d instead of 0\n", img
->fDOSImage
);
505 ok(img
->fReadOnly
== 1 || broken(!img
->fReadOnly
) /* <= WinXP */,
506 "unexpected fReadOnly, got %d instead of 1\n", img
->fReadOnly
);
508 ok(img
->Version
== 1 || broken(!img
->Version
) /* <= WinXP */,
509 "unexpected Version, got %d instead of 1\n", img
->Version
);
511 ok(img
->SizeOfImage
== 0x600,
512 "unexpected SizeOfImage, got 0x%x instead of 0x600\n", img
->SizeOfImage
);
515 ret
= pGetImageUnusedHeaderBytes(img
, &count
);
517 ok(ret
== 448, "GetImageUnusedHeaderBytes returned %u instead of 448\n", ret
);
519 ok(count
== 64, "unexpected size for unused header bytes, got %u instead of 64\n", count
);
524 DeleteFileA(temp_file
);
529 hImageHlp
= LoadLibraryA("imagehlp.dll");
533 win_skip("ImageHlp unavailable\n");
537 pImageGetDigestStream
= (void *) GetProcAddress(hImageHlp
, "ImageGetDigestStream");
538 pBindImageEx
= (void *) GetProcAddress(hImageHlp
, "BindImageEx");
539 pGetImageUnusedHeaderBytes
= (void *) GetProcAddress(hImageHlp
, "GetImageUnusedHeaderBytes");
540 pImageLoad
= (void *) GetProcAddress(hImageHlp
, "ImageLoad");
541 pImageUnload
= (void *) GetProcAddress(hImageHlp
, "ImageUnload");
543 test_get_digest_stream();
544 test_bind_image_ex();
547 FreeLibrary(hImageHlp
);