GeezerVison is showing github.com — Pass alloc/free functions to MemoryLoadLibraryEx · InnoDevTM/MemoryModule@d46b74d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: MemoryModule.c +37-14Lines changed: 37 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,6 +56,8 @@ typedef struct { | 56 | 56 | BOOL initialized; | 57 | 57 | BOOL isDLL; | 58 | 58 | BOOL isRelocated; | | 59 | + CustomAllocFunc alloc; | | 60 | + CustomFreeFunc free; | 59 | 61 | CustomLoadLibraryFunc loadLibrary; | 60 | 62 | CustomGetProcAddressFunc getProcAddress; | 61 | 63 | CustomFreeLibraryFunc freeLibrary; | @@ -115,10 +117,11 @@ CopySections(const unsigned char *data, size_t size, PIMAGE_NT_HEADERS old_heade | 115 | 117 | // uninitialized data | 116 | 118 | section_size = old_headers->OptionalHeader.SectionAlignment; | 117 | 119 | if (section_size > 0) { | 118 | | - dest = (unsigned char *)VirtualAlloc(codeBase + section->VirtualAddress, | | 120 | + dest = (unsigned char *)module->alloc(codeBase + section->VirtualAddress, | 119 | 121 | section_size, | 120 | 122 | MEM_COMMIT, | 121 | | - PAGE_READWRITE); | | 123 | + PAGE_READWRITE, | | 124 | + module->userdata); | 122 | 125 | if (dest == NULL) { | 123 | 126 | return FALSE; | 124 | 127 | } | @@ -139,10 +142,11 @@ CopySections(const unsigned char *data, size_t size, PIMAGE_NT_HEADERS old_heade | 139 | 142 | } | 140 | 143 |
| 141 | 144 | // commit memory block and copy data from dll | 142 | | - dest = (unsigned char *)VirtualAlloc(codeBase + section->VirtualAddress, | | 145 | + dest = (unsigned char *)module->alloc(codeBase + section->VirtualAddress, | 143 | 146 | section->SizeOfRawData, | 144 | 147 | MEM_COMMIT, | 145 | | - PAGE_READWRITE); | | 148 | + PAGE_READWRITE, | | 149 | + module->userdata); | 146 | 150 | if (dest == NULL) { | 147 | 151 | return FALSE; | 148 | 152 | } | @@ -202,7 +206,7 @@ FinalizeSection(PMEMORYMODULE module, PSECTIONFINALIZEDATA sectionData) { | 202 | 206 | (sectionData->size % module->pageSize) == 0) | 203 | 207 | ) { | 204 | 208 | // Only allowed to decommit whole pages | 205 | | - VirtualFree(sectionData->address, sectionData->size, MEM_DECOMMIT); | | 209 | + module->free(sectionData->address, sectionData->size, MEM_DECOMMIT, module->userdata); | 206 | 210 | } | 207 | 211 | return TRUE; | 208 | 212 | } | @@ -429,6 +433,18 @@ BuildImportTable(PMEMORYMODULE module) | 429 | 433 | return result; | 430 | 434 | } | 431 | 435 |
| | 436 | +LPVOID MemoryDefaultAlloc(LPVOID address, SIZE_T size, DWORD allocationType, DWORD protect, void* userdata) | | 437 | +{ | | 438 | + UNREFERENCED_PARAMETER(userdata); | | 439 | + return VirtualAlloc(address, size, allocationType, protect); | | 440 | +} | | 441 | + | | 442 | +BOOL MemoryDefaultFree(LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType, void* userdata) | | 443 | +{ | | 444 | + UNREFERENCED_PARAMETER(userdata); | | 445 | + return VirtualFree(lpAddress, dwSize, dwFreeType); | | 446 | +} | | 447 | + | 432 | 448 | HCUSTOMMODULE MemoryDefaultLoadLibrary(LPCSTR filename, void *userdata) | 433 | 449 | { | 434 | 450 | HMODULE result; | @@ -455,10 +471,12 @@ void MemoryDefaultFreeLibrary(HCUSTOMMODULE module, void *userdata) | 455 | 471 |
| 456 | 472 | HMEMORYMODULE MemoryLoadLibrary(const void *data, size_t size) | 457 | 473 | { | 458 | | - return MemoryLoadLibraryEx(data, size, MemoryDefaultLoadLibrary, MemoryDefaultGetProcAddress, MemoryDefaultFreeLibrary, NULL); | | 474 | + return MemoryLoadLibraryEx(data, size, MemoryDefaultAlloc, MemoryDefaultFree, MemoryDefaultLoadLibrary, MemoryDefaultGetProcAddress, MemoryDefaultFreeLibrary, NULL); | 459 | 475 | } | 460 | 476 |
| 461 | 477 | HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size, | | 478 | + CustomAllocFunc allocMemory, | | 479 | + CustomFreeFunc freeMemory, | 462 | 480 | CustomLoadLibraryFunc loadLibrary, | 463 | 481 | CustomGetProcAddressFunc getProcAddress, | 464 | 482 | CustomFreeLibraryFunc freeLibrary, | @@ -535,17 +553,19 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size, | 535 | 553 | // reserve memory for image of library | 536 | 554 | // XXX: is it correct to commit the complete memory region at once? | 537 | 555 | // calling DllEntry raises an exception if we don't... | 538 | | - code = (unsigned char *)VirtualAlloc((LPVOID)(old_header->OptionalHeader.ImageBase), | | 556 | + code = (unsigned char *)allocMemory((LPVOID)(old_header->OptionalHeader.ImageBase), | 539 | 557 | alignedImageSize, | 540 | 558 | MEM_RESERVE | MEM_COMMIT, | 541 | | - PAGE_READWRITE); | | 559 | + PAGE_READWRITE, | | 560 | + userdata); | 542 | 561 |
| 543 | 562 | if (code == NULL) { | 544 | 563 | // try to allocate memory at arbitrary position | 545 | | - code = (unsigned char *)VirtualAlloc(NULL, | | 564 | + code = (unsigned char *)allocMemory(NULL, | 546 | 565 | alignedImageSize, | 547 | 566 | MEM_RESERVE | MEM_COMMIT, | 548 | | - PAGE_READWRITE); | | 567 | + PAGE_READWRITE, | | 568 | + userdata); | 549 | 569 | if (code == NULL) { | 550 | 570 | SetLastError(ERROR_OUTOFMEMORY); | 551 | 571 | return NULL; | @@ -554,13 +574,15 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size, | 554 | 574 |
| 555 | 575 | result = (PMEMORYMODULE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MEMORYMODULE)); | 556 | 576 | if (result == NULL) { | 557 | | - VirtualFree(code, 0, MEM_RELEASE); | | 577 | + freeMemory(code, 0, MEM_RELEASE, userdata); | 558 | 578 | SetLastError(ERROR_OUTOFMEMORY); | 559 | 579 | return NULL; | 560 | 580 | } | 561 | 581 |
| 562 | 582 | result->codeBase = code; | 563 | 583 | result->isDLL = (old_header->FileHeader.Characteristics & IMAGE_FILE_DLL) != 0; | | 584 | + result->alloc = allocMemory; | | 585 | + result->free = freeMemory; | 564 | 586 | result->loadLibrary = loadLibrary; | 565 | 587 | result->getProcAddress = getProcAddress; | 566 | 588 | result->freeLibrary = freeLibrary; | @@ -572,10 +594,11 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size, | 572 | 594 | } | 573 | 595 |
| 574 | 596 | // commit memory for headers | 575 | | - headers = (unsigned char *)VirtualAlloc(code, | | 597 | + headers = (unsigned char *)allocMemory(code, | 576 | 598 | old_header->OptionalHeader.SizeOfHeaders, | 577 | 599 | MEM_COMMIT, | 578 | | - PAGE_READWRITE); | | 600 | + PAGE_READWRITE, | | 601 | + userdata); | 579 | 602 |
| 580 | 603 | // copy PE header to code | 581 | 604 | memcpy(headers, dos_header, old_header->OptionalHeader.SizeOfHeaders); | @@ -724,7 +747,7 @@ void MemoryFreeLibrary(HMEMORYMODULE mod) | 724 | 747 |
| 725 | 748 | if (module->codeBase != NULL) { | 726 | 749 | // release memory of library | 727 | | - VirtualFree(module->codeBase, 0, MEM_RELEASE); | | 750 | + module->free(module->codeBase, 0, MEM_RELEASE, module->userdata); | 728 | 751 | } | 729 | 752 |
| 730 | 753 | HeapFree(GetProcessHeap(), 0, module); | |
Collapse file Copy file name to clipboardExpand all lines: MemoryModule.h +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,8 @@ typedef void *HCUSTOMMODULE; | 39 | 39 | extern "C" { | 40 | 40 | #endif | 41 | 41 |
| | 42 | +typedef LPVOID (*CustomAllocFunc)(LPVOID, SIZE_T, DWORD, DWORD, void*); | | 43 | +typedef BOOL (*CustomFreeFunc)(LPVOID, SIZE_T, DWORD, void*); | 42 | 44 | typedef HCUSTOMMODULE (*CustomLoadLibraryFunc)(LPCSTR, void *); | 43 | 45 | typedef FARPROC (*CustomGetProcAddressFunc)(HCUSTOMMODULE, LPCSTR, void *); | 44 | 46 | typedef void (*CustomFreeLibraryFunc)(HCUSTOMMODULE, void *); | @@ -58,6 +60,8 @@ HMEMORYMODULE MemoryLoadLibrary(const void *, size_t); | 58 | 60 | * Dependencies will be resolved using passed callback methods. | 59 | 61 | */ | 60 | 62 | HMEMORYMODULE MemoryLoadLibraryEx(const void *, size_t, | | 63 | + CustomAllocFunc, | | 64 | + CustomFreeFunc, | 61 | 65 | CustomLoadLibraryFunc, | 62 | 66 | CustomGetProcAddressFunc, | 63 | 67 | CustomFreeLibraryFunc, | @@ -117,6 +121,22 @@ int MemoryLoadString(HMEMORYMODULE, UINT, LPTSTR, int); | 117 | 121 | */ | 118 | 122 | int MemoryLoadStringEx(HMEMORYMODULE, UINT, LPTSTR, int, WORD); | 119 | 123 |
| | 124 | +/** | | 125 | +* Default implementation of CustomAllocFunc that calls VirtualAlloc | | 126 | +* internally to allocate memory for a library | | 127 | +* | | 128 | +* This is the default as used by MemoryLoadLibrary. | | 129 | +*/ | | 130 | +LPVOID MemoryDefaultAlloc(LPVOID, SIZE_T, DWORD, DWORD, void *); | | 131 | + | | 132 | +/** | | 133 | +* Default implementation of CustomFreeFunc that calls VirtualFree | | 134 | +* internally to free the memory used by a library | | 135 | +* | | 136 | +* This is the default as used by MemoryLoadLibrary. | | 137 | +*/ | | 138 | +BOOL MemoryDefaultFree(LPVOID, SIZE_T, DWORD, void *); | | 139 | + | 120 | 140 | /** | 121 | 141 | * Default implementation of CustomLoadLibraryFunc that calls LoadLibraryA | 122 | 142 | * internally to load an additional libary. | |
|