Skip to content

Commit ac78ea1

Browse files
committed
Add test that loads a dll with lots of exports.
1 parent 98b95ec commit ac78ea1

File tree

4 files changed

+135
-2
lines changed

4 files changed

+135
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
*.exe
44
tests/*.dll
55
tests/*.res
6+
7+
tests/SampleExports.cpp
8+
tests/SampleExports.h

tests/LoadDll.cpp

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "../MemoryModule.h"
1313

14+
typedef int (*addProc)(int);
1415
typedef int (*addNumberProc)(int, int);
1516

1617
// Thanks to Tim Cooper (from http://stackoverflow.com/a/8584708)
@@ -127,6 +128,11 @@ BOOL LoadFromMemory(char *filename)
127128
}
128129

129130
addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
131+
if (!addNumber) {
132+
_tprintf(_T("MemoryGetProcAddress(\"addNumber\") returned NULL\n"));
133+
result = FALSE;
134+
goto exit;
135+
}
130136
_tprintf(_T("From memory: %d\n"), addNumber(1, 2));
131137

132138
// the DLL only exports one function, try to load by ordinal value
@@ -218,15 +224,80 @@ BOOL LoadFromMemory(char *filename)
218224
return result;
219225
}
220226

227+
BOOL LoadExportsFromMemory(char *filename)
228+
{
229+
FILE *fp;
230+
unsigned char *data=NULL;
231+
long size;
232+
size_t read;
233+
HMEMORYMODULE handle = NULL;
234+
int i;
235+
BOOL result = TRUE;
236+
237+
fp = fopen(filename, "rb");
238+
if (fp == NULL)
239+
{
240+
printf("Can't open DLL file \"%s\".", filename);
241+
result = FALSE;
242+
goto exit;
243+
}
244+
245+
fseek(fp, 0, SEEK_END);
246+
size = ftell(fp);
247+
assert(size > 0);
248+
data = (unsigned char *)malloc(size);
249+
assert(data != NULL);
250+
fseek(fp, 0, SEEK_SET);
251+
read = fread(data, 1, size, fp);
252+
assert(read == static_cast<size_t>(size));
253+
fclose(fp);
254+
255+
handle = MemoryLoadLibrary(data, size);
256+
if (handle == NULL)
257+
{
258+
_tprintf(_T("Can't load library from memory.\n"));
259+
result = FALSE;
260+
goto exit;
261+
}
262+
263+
for (i = 1; i <= 100; i++) {
264+
char name[100];
265+
sprintf(name, "add%d", i);
266+
addProc addNumber = (addProc)MemoryGetProcAddress(handle, name);
267+
if (!addNumber) {
268+
_tprintf(_T("MemoryGetProcAddress(\"%s\") returned NULL\n"), name);
269+
result = FALSE;
270+
goto exit;
271+
}
272+
int result = addNumber(1);
273+
if (result != 1 + i) {
274+
_tprintf(_T("(\"%s\") returned %d, expected %d\n"), name, result, 1 + i);
275+
result = FALSE;
276+
goto exit;
277+
}
278+
_tprintf(_T("%s: %d\n"), name, result);
279+
}
280+
exit:
281+
MemoryFreeLibrary(handle);
282+
free(data);
283+
return result;
284+
}
285+
221286
int main(int argc, char* argv[])
222287
{
223288
if (argc < 2) {
224289
fprintf(stderr, "USAGE: %s <filename.dll>\n", argv[0]);
225290
return 1;
226291
}
227292

228-
if (!LoadFromMemory(argv[1])) {
229-
return 2;
293+
if (!strstr((const char *) argv[1], "exports")) {
294+
if (!LoadFromMemory(argv[1])) {
295+
return 2;
296+
}
297+
} else {
298+
if (!LoadExportsFromMemory(argv[1])) {
299+
return 2;
300+
}
230301
}
231302

232303
return 0;

tests/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ TEST_DLLS = \
4747
test-align-800.dll \
4848
test-align-900.dll \
4949
test-relocate.dll \
50+
test-exports.dll
5051

5152
LOADDLL_OBJ = LoadDll.o ../MemoryModule.o
5253
TESTSUITE_OBJ = TestSuite.o ../MemoryModule.o
@@ -72,6 +73,12 @@ test-align-%.dll: $(DLL_OBJ)
7273
test-relocate.dll: $(DLL_OBJ)
7374
$(CXX) $(LDFLAGS_DLL) $(LDFLAGS) -Wl,--image-base -Wl,0x20000000 -o $@ $(DLL_OBJ)
7475

76+
test-exports.dll: SampleExports.o
77+
$(CXX) $(LDFLAGS_DLL) $(LDFLAGS) -o $@ SampleExports.o
78+
79+
SampleExports.cpp: generate-exports.sh
80+
./generate-exports.sh
81+
7582
%.o: %.cpp
7683
$(CXX) $(CFLAGS) $(CFLAGS_DLL) -c $<
7784

tests/generate-exports.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
##
4+
## Generate header file.
5+
##
6+
7+
cat > SampleExports.h << EOF
8+
extern "C" {
9+
10+
#ifdef SAMPLEDLL_EXPORTS
11+
#define SAMPLEDLL_API __declspec(dllexport)
12+
#else
13+
#define SAMPLEDLL_API __declspec(dllimport)
14+
#endif
15+
16+
EOF
17+
18+
for i in `seq 1 100`;
19+
do
20+
cat >> SampleExports.h << EOF
21+
SAMPLEDLL_API int add$i(int a);
22+
EOF
23+
done
24+
25+
cat >> SampleExports.h << EOF
26+
}
27+
EOF
28+
29+
30+
##
31+
## Generate source file.
32+
##
33+
34+
cat > SampleExports.cpp << EOF
35+
#include "SampleExports.h"
36+
37+
extern "C" {
38+
EOF
39+
40+
for i in `seq 1 100 | sort -R`;
41+
do
42+
cat >> SampleExports.cpp << EOF
43+
SAMPLEDLL_API int add$i(int a)
44+
{
45+
return a + $i;
46+
}
47+
EOF
48+
done
49+
50+
cat >> SampleExports.cpp << EOF
51+
}
52+
EOF

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy