Add testcase for exception handling code.

This commit is contained in:
Joachim Bauch
2019-02-24 14:42:48 +01:00
parent f89837636a
commit bf23ebf03c
4 changed files with 81 additions and 1 deletions
+2
View File
@@ -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})
+72
View File
@@ -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_t>(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;
+1 -1
View File
@@ -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
+6
View File
@@ -48,5 +48,11 @@ EOF
done
cat >> SampleExports.cpp << EOF
#ifdef _WIN64
SAMPLEDLL_API void throwException(void)
{
throw 42;
}
#endif
}
EOF