forked from fancycode/MemoryModule
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDllLoader.cpp
More file actions
343 lines (274 loc) · 9.44 KB
/
DllLoader.cpp
File metadata and controls
343 lines (274 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#define WIN32_LEAN_AND_MEAN
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <assert.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <malloc.h>
#include "../../MemoryModule.h"
typedef int (*addNumberProc)(int, int);
#define DLL_FILE TEXT("..\\SampleDLL\\SampleDLL.dll")
void LoadFromFile(void)
{
addNumberProc addNumber;
HRSRC resourceInfo;
DWORD resourceSize;
LPVOID resourceData;
TCHAR buffer[100];
HINSTANCE handle = LoadLibrary(DLL_FILE);
if (handle == NULL)
return;
addNumber = (addNumberProc)GetProcAddress(handle, "addNumbers");
_tprintf(_T("From file: %d\n"), addNumber(1, 2));
resourceInfo = FindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
_tprintf(_T("FindResource returned 0x%p\n"), resourceInfo);
resourceSize = SizeofResource(handle, resourceInfo);
resourceData = LoadResource(handle, resourceInfo);
_tprintf(_T("Resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData);
LoadString(handle, 1, buffer, sizeof(buffer));
_tprintf(_T("String1: %s\n"), buffer);
LoadString(handle, 20, buffer, sizeof(buffer));
_tprintf(_T("String2: %s\n"), buffer);
FreeLibrary(handle);
}
void* ReadLibrary(size_t* pSize) {
size_t read;
void* result;
FILE* fp;
fp = _tfopen(DLL_FILE, _T("rb"));
if (fp == NULL)
{
_tprintf(_T("Can't open DLL file \"%s\"."), DLL_FILE);
return NULL;
}
fseek(fp, 0, SEEK_END);
*pSize = static_cast<size_t>(ftell(fp));
if (*pSize == 0)
{
fclose(fp);
return NULL;
}
result = (unsigned char *)malloc(*pSize);
if (result == NULL)
{
return NULL;
}
fseek(fp, 0, SEEK_SET);
read = fread(result, 1, *pSize, fp);
fclose(fp);
if (read != *pSize)
{
free(result);
return NULL;
}
return result;
}
void LoadFromMemory(void)
{
void *data;
size_t size;
HMEMORYMODULE handle;
addNumberProc addNumber;
HMEMORYRSRC resourceInfo;
DWORD resourceSize;
LPVOID resourceData;
TCHAR buffer[100];
data = ReadLibrary(&size);
if (data == NULL)
{
return;
}
handle = MemoryLoadLibrary(data, size);
if (handle == NULL)
{
_tprintf(_T("Can't load library from memory.\n"));
goto exit;
}
addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
_tprintf(_T("From memory: %d\n"), addNumber(1, 2));
resourceInfo = MemoryFindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
_tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo);
resourceSize = MemorySizeofResource(handle, resourceInfo);
resourceData = MemoryLoadResource(handle, resourceInfo);
_tprintf(_T("Memory resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData);
MemoryLoadString(handle, 1, buffer, sizeof(buffer));
_tprintf(_T("String1: %s\n"), buffer);
MemoryLoadString(handle, 20, buffer, sizeof(buffer));
_tprintf(_T("String2: %s\n"), buffer);
MemoryFreeLibrary(handle);
exit:
free(data);
}
#define MAX_CALLS 20
struct CallList {
int current_alloc_call, current_free_call;
CustomAllocFunc alloc_calls[MAX_CALLS];
CustomFreeFunc free_calls[MAX_CALLS];
};
LPVOID MemoryFailingAlloc(LPVOID address, SIZE_T size, DWORD allocationType, DWORD protect, void* userdata)
{
UNREFERENCED_PARAMETER(address);
UNREFERENCED_PARAMETER(size);
UNREFERENCED_PARAMETER(allocationType);
UNREFERENCED_PARAMETER(protect);
UNREFERENCED_PARAMETER(userdata);
return NULL;
}
LPVOID MemoryMockAlloc(LPVOID address, SIZE_T size, DWORD allocationType, DWORD protect, void* userdata)
{
CallList* calls = (CallList*)userdata;
CustomAllocFunc current_func = calls->alloc_calls[calls->current_alloc_call++];
assert(current_func != NULL);
return current_func(address, size, allocationType, protect, NULL);
}
BOOL MemoryMockFree(LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType, void* userdata)
{
CallList* calls = (CallList*)userdata;
CustomFreeFunc current_func = calls->free_calls[calls->current_free_call++];
assert(current_func != NULL);
return current_func(lpAddress, dwSize, dwFreeType, NULL);
}
void InitFuncs(void** funcs, va_list args) {
for (int i = 0; ; i++) {
assert(i < MAX_CALLS);
funcs[i] = va_arg(args, void*);
if (funcs[i] == NULL) break;
}
}
void InitAllocFuncs(CallList* calls, ...) {
va_list args;
va_start(args, calls);
InitFuncs((void**)calls->alloc_calls, args);
va_end(args);
calls->current_alloc_call = 0;
}
void InitFreeFuncs(CallList* calls, ...) {
va_list args;
va_start(args, calls);
InitFuncs((void**)calls->free_calls, args);
va_end(args);
calls->current_free_call = 0;
}
void InitFreeFunc(CallList* calls, CustomFreeFunc freeFunc) {
for (int i = 0; i < MAX_CALLS; i++) {
calls->free_calls[i] = freeFunc;
}
calls->current_free_call = 0;
}
void TestFailingAllocation(void *data, size_t size) {
CallList expected_calls;
HMEMORYMODULE handle;
InitAllocFuncs(&expected_calls, MemoryFailingAlloc, MemoryFailingAlloc, NULL);
InitFreeFuncs(&expected_calls, NULL);
handle = MemoryLoadLibraryEx(
data, size, MemoryMockAlloc, MemoryMockFree, MemoryDefaultLoadLibrary,
MemoryDefaultGetProcAddress, MemoryDefaultFreeLibrary, &expected_calls);
assert(handle == NULL);
assert(GetLastError() == ERROR_OUTOFMEMORY);
assert(expected_calls.current_free_call == 0);
MemoryFreeLibrary(handle);
assert(expected_calls.current_free_call == 0);
}
void TestCleanupAfterFailingAllocation(void *data, size_t size) {
CallList expected_calls;
HMEMORYMODULE handle;
int free_calls_after_loading;
InitAllocFuncs(&expected_calls,
MemoryDefaultAlloc,
MemoryDefaultAlloc,
MemoryDefaultAlloc,
MemoryDefaultAlloc,
MemoryFailingAlloc,
NULL);
InitFreeFuncs(&expected_calls, MemoryDefaultFree, NULL);
handle = MemoryLoadLibraryEx(
data, size, MemoryMockAlloc, MemoryMockFree, MemoryDefaultLoadLibrary,
MemoryDefaultGetProcAddress, MemoryDefaultFreeLibrary, &expected_calls);
free_calls_after_loading = expected_calls.current_free_call;
MemoryFreeLibrary(handle);
assert(expected_calls.current_free_call == free_calls_after_loading);
}
void TestFreeAfterDefaultAlloc(void *data, size_t size) {
CallList expected_calls;
HMEMORYMODULE handle;
int free_calls_after_loading;
// Note: free might get called internally multiple times
InitFreeFunc(&expected_calls, MemoryDefaultFree);
handle = MemoryLoadLibraryEx(
data, size, MemoryDefaultAlloc, MemoryMockFree, MemoryDefaultLoadLibrary,
MemoryDefaultGetProcAddress, MemoryDefaultFreeLibrary, &expected_calls);
assert(handle != NULL);
free_calls_after_loading = expected_calls.current_free_call;
MemoryFreeLibrary(handle);
assert(expected_calls.current_free_call == free_calls_after_loading + 1);
}
#ifdef _WIN64
LPVOID MemoryAllocHigh(LPVOID address, SIZE_T size, DWORD allocationType, DWORD protect, void* userdata)
{
int* counter = static_cast<int*>(userdata);
if (*counter == 0) {
// Make sure the image gets loaded to an address above 32bit.
uintptr_t offset = 0x10000000000;
address = (LPVOID) ((uintptr_t) address + offset);
}
(*counter)++;
return MemoryDefaultAlloc(address, size, allocationType, protect, NULL);
}
void TestAllocHighMemory(void *data, size_t size) {
HMEMORYMODULE handle;
int counter = 0;
addNumberProc addNumber;
HMEMORYRSRC resourceInfo;
DWORD resourceSize;
LPVOID resourceData;
TCHAR buffer[100];
handle = MemoryLoadLibraryEx(
data, size, MemoryAllocHigh, MemoryDefaultFree, MemoryDefaultLoadLibrary,
MemoryDefaultGetProcAddress, MemoryDefaultFreeLibrary, &counter);
assert(handle != NULL);
addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
_tprintf(_T("From memory: %d\n"), addNumber(1, 2));
resourceInfo = MemoryFindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
_tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo);
resourceSize = MemorySizeofResource(handle, resourceInfo);
resourceData = MemoryLoadResource(handle, resourceInfo);
_tprintf(_T("Memory resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData);
MemoryLoadString(handle, 1, buffer, sizeof(buffer));
_tprintf(_T("String1: %s\n"), buffer);
MemoryLoadString(handle, 20, buffer, sizeof(buffer));
_tprintf(_T("String2: %s\n"), buffer);
MemoryFreeLibrary(handle);
}
#endif // _WIN64
void TestCustomAllocAndFree(void)
{
void *data;
size_t size;
data = ReadLibrary(&size);
if (data == NULL)
{
return;
}
_tprintf(_T("Test MemoryLoadLibraryEx after initially failing allocation function\n"));
TestFailingAllocation(data, size);
_tprintf(_T("Test cleanup after MemoryLoadLibraryEx with failing allocation function\n"));
TestCleanupAfterFailingAllocation(data, size);
_tprintf(_T("Test custom free function after MemoryLoadLibraryEx\n"));
TestFreeAfterDefaultAlloc(data, size);
#ifdef _WIN64
_tprintf(_T("Test allocating in high memory\n"));
TestAllocHighMemory(data, size);
#endif
free(data);
}
int main()
{
LoadFromFile();
printf("\n\n");
LoadFromMemory();
printf("\n\n");
TestCustomAllocAndFree();
return 0;
}