Skip to content

Commit bf23ebf

Browse files
committed
Add testcase for exception handling code.
1 parent f898376 commit bf23ebf

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ set (sources_testsuite
55
if (NOT MSVC)
66
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-static")
77
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "-static")
8+
else ()
9+
set (CMAKE_CXX_FLAGS "/EHsc")
810
endif ()
911

1012
add_executable (TestSuite ${sources_testsuite})

tests/LoadDll.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
typedef int (*addProc)(int);
1515
typedef int (*addNumberProc)(int, int);
16+
#ifdef _WIN64
17+
typedef void (*throwExceptionProc)(void);
18+
#endif
1619

1720
// Thanks to Tim Cooper (from http://stackoverflow.com/a/8584708)
1821
const char *sstrstr(const char *haystack, const char *needle, size_t length) {
@@ -283,6 +286,70 @@ BOOL LoadExportsFromMemory(char *filename)
283286
return result;
284287
}
285288

289+
#ifdef _WIN64
290+
BOOL LoadExceptionsFromMemory(char *filename)
291+
{
292+
FILE *fp;
293+
unsigned char *data=NULL;
294+
long size;
295+
size_t read;
296+
HMEMORYMODULE handle = NULL;
297+
throwExceptionProc throwException;
298+
BOOL result = TRUE;
299+
300+
fp = fopen(filename, "rb");
301+
if (fp == NULL)
302+
{
303+
printf("Can't open DLL file \"%s\".", filename);
304+
result = FALSE;
305+
goto exit;
306+
}
307+
308+
fseek(fp, 0, SEEK_END);
309+
size = ftell(fp);
310+
assert(size > 0);
311+
data = (unsigned char *)malloc(size);
312+
assert(data != NULL);
313+
fseek(fp, 0, SEEK_SET);
314+
read = fread(data, 1, size, fp);
315+
assert(read == static_cast<size_t>(size));
316+
fclose(fp);
317+
318+
handle = MemoryLoadLibrary(data, size);
319+
if (handle == NULL)
320+
{
321+
_tprintf(_T("Can't load library from memory.\n"));
322+
result = FALSE;
323+
goto exit;
324+
}
325+
326+
throwException = (throwExceptionProc)MemoryGetProcAddress(handle, "throwException");
327+
if (!throwException) {
328+
_tprintf(_T("MemoryGetProcAddress(\"throwException\") returned NULL\n"));
329+
result = FALSE;
330+
goto exit;
331+
}
332+
333+
try {
334+
throwException();
335+
_tprintf(_T("Should have caught exception.\n"));
336+
result = FALSE;
337+
goto exit;
338+
} catch (int e) {
339+
if (e != 42) {
340+
_tprintf(_T("Should have caught exception 42, got %d instead\n"), e);
341+
result = FALSE;
342+
goto exit;
343+
}
344+
}
345+
346+
exit:
347+
MemoryFreeLibrary(handle);
348+
free(data);
349+
return result;
350+
}
351+
#endif
352+
286353
int main(int argc, char* argv[])
287354
{
288355
if (argc < 2) {
@@ -298,6 +365,11 @@ int main(int argc, char* argv[])
298365
if (!LoadExportsFromMemory(argv[1])) {
299366
return 2;
300367
}
368+
#ifdef _WIN64
369+
if (!LoadExceptionsFromMemory(argv[1])) {
370+
return 2;
371+
}
372+
#endif
301373
}
302374

303375
return 0;

tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ test-relocate.dll: $(DLL_OBJ)
7474
$(CXX) $(LDFLAGS_DLL) $(LDFLAGS) -Wl,--image-base -Wl,0x20000000 -o $@ $(DLL_OBJ)
7575

7676
test-exports.dll: SampleExports.o
77-
$(CXX) $(LDFLAGS_DLL) $(LDFLAGS) -o $@ SampleExports.o
77+
$(CXX) $(LDFLAGS_DLL) $(LDFLAGS) -static -lstdc++ -dynamic -o $@ SampleExports.o
7878

7979
SampleExports.cpp: generate-exports.sh
8080
./generate-exports.sh

tests/generate-exports.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@ EOF
4848
done
4949

5050
cat >> SampleExports.cpp << EOF
51+
#ifdef _WIN64
52+
SAMPLEDLL_API void throwException(void)
53+
{
54+
throw 42;
55+
}
56+
#endif
5157
}
5258
EOF

0 commit comments

Comments
 (0)