8000 Added testcase to load image above 32bit. · fancycode/MemoryModule@6c4ab35 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6c4ab35

Browse files
committed
Added testcase to load image above 32bit.
1 parent 8d609dc commit 6c4ab35

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

example/DllLoader/DllLoader.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,55 @@ void TestFreeAfterDefaultAlloc(void *data, size_t size) {
257257
assert(expected_calls.current_free_call == free_calls_after_loading + 1);
258258
}
259259

260+
#ifdef _WIN64
261+
262+
LPVOID MemoryAllocHigh(LPVOID address, SIZE_T size, DWORD allocationType, DWORD protect, void* userdata)
263+
{
264+
int* counter = static_cast<int*>(userdata);
265+
if (*counter == 0) {
266+
// Make sure the image gets loaded to an address above 32bit.
267+
uintptr_t offset = 0x10000000000;
268+
address = (LPVOID) ((uintptr_t) address + offset);
269+
}
270+
(*counter)++;
271+
return MemoryDefaultAlloc(address, size, allocationType, protect, NULL);
272+
}
273+
274+
void TestAllocHighMemory(void *data, size_t size) {
275+
HMEMORYMODULE handle;
276+
int counter = 0;
277+
addNumberProc addNumber;
278+
HMEMORYRSRC resourceInfo;
279+
DWORD resourceSize;
280+
LPVOID resourceData;
281+
TCHAR buffer[100];
282+
283+
handle = MemoryLoadLibraryEx(
284+
data, size, MemoryAllocHigh, MemoryDefaultFree, MemoryDefaultLoadLibrary,
285+
MemoryDefaultGetProcAddress, MemoryDefaultFreeLibrary, &counter);
286+
287+
assert(handle != NULL);
288+
289+
addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
290+
_tprintf(_T("From memory: %d\n"), addNumber(1, 2));
291+
292+
resourceInfo = MemoryFindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
293+
_tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo);
294+
295+
resourceSize = MemorySizeofResource(handle, resourceInfo);
296+
resourceData = MemoryLoadResource(handle, resourceInfo);
297+
_tprintf(_T("Memory resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData);
298+
299+
MemoryLoadString(handle, 1, buffer, sizeof(buffer));
300+
_tprintf(_T("String1: %s\n"), buffer);
301+
302+
MemoryLoadString(handle, 20, buffer, sizeof(buffer));
303+
_tprintf(_T("String2: %s\n"), buffer);
304+
305+
MemoryFreeLibrary(handle);
306+
}
307+
#endif // _WIN64
308+
260309
void TestCustomAllocAndFree(void)
261310
{
262311
void *data;
@@ -274,6 +323,10 @@ void TestCustomAllocAndFree(void)
274323
TestCleanupAfterFailingAllocation(data, size);
275324
_tprintf(_T("Test custom free function after MemoryLoadLibraryEx\n"));
276325
TestFreeAfterDefaultAlloc(data, size);
326+
#ifdef _WIN64
327+
_tprintf(_T("Test allocating in high memory\n"));
328+
TestAllocHighMemory(data, size);
329+
#endif
277330

278331
free(data);
279332
}

0 commit comments

Comments
 (0)
0