From bf23ebf03ccd0affa1cdc772d4d98ea5c71ccdb9 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Sun, 24 Feb 2019 14:42:48 +0100 Subject: [PATCH] Add testcase for exception handling code. --- tests/CMakeLists.txt | 2 ++ tests/LoadDll.cpp | 72 +++++++++++++++++++++++++++++++++++++++ tests/Makefile | 2 +- tests/generate-exports.sh | 6 ++++ 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8b1007f..e968369 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,6 +5,8 @@ set (sources_testsuite if (NOT MSVC) set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-static") set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "-static") +else () + set (CMAKE_CXX_FLAGS "/EHsc") endif () add_executable (TestSuite ${sources_testsuite}) diff --git a/tests/LoadDll.cpp b/tests/LoadDll.cpp index 8cb215b..15e9f93 100644 --- a/tests/LoadDll.cpp +++ b/tests/LoadDll.cpp @@ -13,6 +13,9 @@ typedef int (*addProc)(int); typedef int (*addNumberProc)(int, int); +#ifdef _WIN64 +typedef void (*throwExceptionProc)(void); +#endif // Thanks to Tim Cooper (from http://stackoverflow.com/a/8584708) const char *sstrstr(const char *haystack, const char *needle, size_t length) { @@ -283,6 +286,70 @@ exit: return result; } +#ifdef _WIN64 +BOOL LoadExceptionsFromMemory(char *filename) +{ + FILE *fp; + unsigned char *data=NULL; + long size; + size_t read; + HMEMORYMODULE handle = NULL; + throwExceptionProc throwException; + BOOL result = TRUE; + + fp = fopen(filename, "rb"); + if (fp == NULL) + { + printf("Can't open DLL file \"%s\".", filename); + result = FALSE; + goto exit; + } + + fseek(fp, 0, SEEK_END); + size = ftell(fp); + assert(size > 0); + data = (unsigned char *)malloc(size); + assert(data != NULL); + fseek(fp, 0, SEEK_SET); + read = fread(data, 1, size, fp); + assert(read == static_cast(size)); + fclose(fp); + + handle = MemoryLoadLibrary(data, size); + if (handle == NULL) + { + _tprintf(_T("Can't load library from memory.\n")); + result = FALSE; + goto exit; + } + + throwException = (throwExceptionProc)MemoryGetProcAddress(handle, "throwException"); + if (!throwException) { + _tprintf(_T("MemoryGetProcAddress(\"throwException\") returned NULL\n")); + result = FALSE; + goto exit; + } + + try { + throwException(); + _tprintf(_T("Should have caught exception.\n")); + result = FALSE; + goto exit; + } catch (int e) { + if (e != 42) { + _tprintf(_T("Should have caught exception 42, got %d instead\n"), e); + result = FALSE; + goto exit; + } + } + +exit: + MemoryFreeLibrary(handle); + free(data); + return result; +} +#endif + int main(int argc, char* argv[]) { if (argc < 2) { @@ -298,6 +365,11 @@ int main(int argc, char* argv[]) if (!LoadExportsFromMemory(argv[1])) { return 2; } +#ifdef _WIN64 + if (!LoadExceptionsFromMemory(argv[1])) { + return 2; + } +#endif } return 0; diff --git a/tests/Makefile b/tests/Makefile index 028bcad..532a1b7 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -74,7 +74,7 @@ test-relocate.dll: $(DLL_OBJ) $(CXX) $(LDFLAGS_DLL) $(LDFLAGS) -Wl,--image-base -Wl,0x20000000 -o $@ $(DLL_OBJ) test-exports.dll: SampleExports.o - $(CXX) $(LDFLAGS_DLL) $(LDFLAGS) -o $@ SampleExports.o + $(CXX) $(LDFLAGS_DLL) $(LDFLAGS) -static -lstdc++ -dynamic -o $@ SampleExports.o SampleExports.cpp: generate-exports.sh ./generate-exports.sh diff --git a/tests/generate-exports.sh b/tests/generate-exports.sh index 974b2a6..ece6d45 100755 --- a/tests/generate-exports.sh +++ b/tests/generate-exports.sh @@ -48,5 +48,11 @@ EOF done cat >> SampleExports.cpp << EOF +#ifdef _WIN64 +SAMPLEDLL_API void throwException(void) +{ + throw 42; +} +#endif } EOF