mirror of
https://github.com/fancycode/MemoryModule
synced 2026-06-06 15:44:28 +00:00
Add test that loads a dll with lots of exports.
This commit is contained in:
@@ -3,3 +3,6 @@
|
|||||||
*.exe
|
*.exe
|
||||||
tests/*.dll
|
tests/*.dll
|
||||||
tests/*.res
|
tests/*.res
|
||||||
|
|
||||||
|
tests/SampleExports.cpp
|
||||||
|
tests/SampleExports.h
|
||||||
|
|||||||
+73
-2
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "../MemoryModule.h"
|
#include "../MemoryModule.h"
|
||||||
|
|
||||||
|
typedef int (*addProc)(int);
|
||||||
typedef int (*addNumberProc)(int, int);
|
typedef int (*addNumberProc)(int, int);
|
||||||
|
|
||||||
// Thanks to Tim Cooper (from http://stackoverflow.com/a/8584708)
|
// Thanks to Tim Cooper (from http://stackoverflow.com/a/8584708)
|
||||||
@@ -127,6 +128,11 @@ BOOL LoadFromMemory(char *filename)
|
|||||||
}
|
}
|
||||||
|
|
||||||
addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
|
addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
|
||||||
|
if (!addNumber) {
|
||||||
|
_tprintf(_T("MemoryGetProcAddress(\"addNumber\") returned NULL\n"));
|
||||||
|
result = FALSE;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
_tprintf(_T("From memory: %d\n"), addNumber(1, 2));
|
_tprintf(_T("From memory: %d\n"), addNumber(1, 2));
|
||||||
|
|
||||||
// the DLL only exports one function, try to load by ordinal value
|
// the DLL only exports one function, try to load by ordinal value
|
||||||
@@ -218,6 +224,65 @@ exit:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOL LoadExportsFromMemory(char *filename)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
unsigned char *data=NULL;
|
||||||
|
long size;
|
||||||
|
size_t read;
|
||||||
|
HMEMORYMODULE handle = NULL;
|
||||||
|
int i;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 1; i <= 100; i++) {
|
||||||
|
char name[100];
|
||||||
|
sprintf(name, "add%d", i);
|
||||||
|
addProc addNumber = (addProc)MemoryGetProcAddress(handle, name);
|
||||||
|
if (!addNumber) {
|
||||||
|
_tprintf(_T("MemoryGetProcAddress(\"%s\") returned NULL\n"), name);
|
||||||
|
result = FALSE;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
int result = addNumber(1);
|
||||||
|
if (result != 1 + i) {
|
||||||
|
_tprintf(_T("(\"%s\") returned %d, expected %d\n"), name, result, 1 + i);
|
||||||
|
result = FALSE;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
_tprintf(_T("%s: %d\n"), name, result);
|
||||||
|
}
|
||||||
|
exit:
|
||||||
|
MemoryFreeLibrary(handle);
|
||||||
|
free(data);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
@@ -225,8 +290,14 @@ int main(int argc, char* argv[])
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!LoadFromMemory(argv[1])) {
|
if (!strstr((const char *) argv[1], "exports")) {
|
||||||
return 2;
|
if (!LoadFromMemory(argv[1])) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!LoadExportsFromMemory(argv[1])) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ TEST_DLLS = \
|
|||||||
test-align-800.dll \
|
test-align-800.dll \
|
||||||
test-align-900.dll \
|
test-align-900.dll \
|
||||||
test-relocate.dll \
|
test-relocate.dll \
|
||||||
|
test-exports.dll
|
||||||
|
|
||||||
LOADDLL_OBJ = LoadDll.o ../MemoryModule.o
|
LOADDLL_OBJ = LoadDll.o ../MemoryModule.o
|
||||||
TESTSUITE_OBJ = TestSuite.o ../MemoryModule.o
|
TESTSUITE_OBJ = TestSuite.o ../MemoryModule.o
|
||||||
@@ -72,6 +73,12 @@ test-align-%.dll: $(DLL_OBJ)
|
|||||||
test-relocate.dll: $(DLL_OBJ)
|
test-relocate.dll: $(DLL_OBJ)
|
||||||
$(CXX) $(LDFLAGS_DLL) $(LDFLAGS) -Wl,--image-base -Wl,0x20000000 -o $@ $(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
|
||||||
|
|
||||||
|
SampleExports.cpp: generate-exports.sh
|
||||||
|
./generate-exports.sh
|
||||||
|
|
||||||
%.o: %.cpp
|
%.o: %.cpp
|
||||||
$(CXX) $(CFLAGS) $(CFLAGS_DLL) -c $<
|
$(CXX) $(CFLAGS) $(CFLAGS_DLL) -c $<
|
||||||
|
|
||||||
|
|||||||
Executable
+52
@@ -0,0 +1,52 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
##
|
||||||
|
## Generate header file.
|
||||||
|
##
|
||||||
|
|
||||||
|
cat > SampleExports.h << EOF
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
#ifdef SAMPLEDLL_EXPORTS
|
||||||
|
#define SAMPLEDLL_API __declspec(dllexport)
|
||||||
|
#else
|
||||||
|
#define SAMPLEDLL_API __declspec(dllimport)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
EOF
|
||||||
|
|
||||||
|
for i in `seq 1 100`;
|
||||||
|
do
|
||||||
|
cat >> SampleExports.h << EOF
|
||||||
|
SAMPLEDLL_API int add$i(int a);
|
||||||
|
EOF
|
||||||
|
done
|
||||||
|
|
||||||
|
cat >> SampleExports.h << EOF
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
|
||||||
|
|
||||||
|
##
|
||||||
|
## Generate source file.
|
||||||
|
##
|
||||||
|
|
||||||
|
cat > SampleExports.cpp << EOF
|
||||||
|
#include "SampleExports.h"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
EOF
|
||||||
|
|
||||||
|
for i in `seq 1 100 | sort -R`;
|
||||||
|
do
|
||||||
|
cat >> SampleExports.cpp << EOF
|
||||||
|
SAMPLEDLL_API int add$i(int a)
|
||||||
|
{
|
||||||
|
return a + $i;
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
done
|
||||||
|
|
||||||
|
cat >> SampleExports.cpp << EOF
|
||||||
|
}
|
||||||
|
EOF
|
||||||
Reference in New Issue
Block a user