Skip to content

sync main branch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
*.exe
tests/*.dll
tests/*.res

tests/SampleExports.cpp
tests/SampleExports.h
101 changes: 78 additions & 23 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,89 @@
sudo: false
sudo: true

env:
- PLATFORM=x86_64 UNICODE= CMAKE=
- PLATFORM=i686 UNICODE= CMAKE=
- PLATFORM=x86_64 UNICODE=1 CMAKE=
- PLATFORM=i686 UNICODE=1 CMAKE=
- PLATFORM=x86_64 UNICODE= CMAKE=1
- PLATFORM=i686 UNICODE= CMAKE=1
- PLATFORM=x86_64 UNICODE=1 CMAKE=1
- PLATFORM=i686 UNICODE=1 CMAKE=1
matrix:
include:
- env: PLATFORM=x86_64 UNICODE= CMAKE=
addons:
apt:
packages:
- binutils-mingw-w64-x86-64
- mingw-w64-x86-64-dev
- g++-mingw-w64-x86-64
- gcc-mingw-w64-x86-64
- wine:amd64
- env: PLATFORM=i686 UNICODE= CMAKE=
addons:
apt:
packages:
- binutils-mingw-w64-i686
- mingw-w64-i686-dev
- g++-mingw-w64-i686
- gcc-mingw-w64-i686
- wine:i386
- env: PLATFORM=x86_64 UNICODE=1 CMAKE=
addons:
apt:
packages:
- binutils-mingw-w64-x86-64
- mingw-w64-x86-64-dev
- g++-mingw-w64-x86-64
- gcc-mingw-w64-x86-64
- wine:amd64
- env: PLATFORM=i686 UNICODE=1 CMAKE=
addons:
apt:
packages:
- binutils-mingw-w64-i686
- mingw-w64-i686-dev
- g++-mingw-w64-i686
- gcc-mingw-w64-i686
- wine:i386
- env: PLATFORM=x86_64 UNICODE= CMAKE=1
addons:
apt:
packages:
- binutils-mingw-w64-x86-64
- mingw-w64-x86-64-dev
- g++-mingw-w64-x86-64
- gcc-mingw-w64-x86-64
- wine:amd64
- env: PLATFORM=i686 UNICODE= CMAKE=1
addons:
apt:
packages:
- binutils-mingw-w64-i686
- cmake
- mingw-w64-i686-dev
- g++-mingw-w64-i686
- gcc-mingw-w64-i686
- wine:i386
- env: PLATFORM=x86_64 UNICODE=1 CMAKE=1
addons:
apt:
packages:
- binutils-mingw-w64-x86-64
- cmake
- mingw-w64-x86-64-dev
- g++-mingw-w64-x86-64
- gcc-mingw-w64-x86-64
- wine:amd64
- env: PLATFORM=i686 UNICODE=1 CMAKE=1
addons:
apt:
packages:
- binutils-mingw-w64-i686
- cmake
- mingw-w64-i686-dev
- g++-mingw-w64-i686
- gcc-mingw-w64-i686
- wine:i386

language: cpp

cache:
- apt
- ccache

addons:
apt:
packages:
- binutils-mingw-w64-i686
- binutils-mingw-w64-x86-64
- cmake
- mingw-w64-dev
- g++-mingw-w64-i686
- g++-mingw-w64-x86-64
- gcc-mingw-w64-i686
- gcc-mingw-w64-x86-64
- wine

before_script:
- if [ ! -z "$CMAKE" ]; then cmake -DPLATFORM=$PLATFORM -DUNICODE=$UNICODE -DTESTSUITE=ON -H. -B.; fi

Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ else ()
endif ()

add_library (MemoryModule STATIC MemoryModule.c MemoryModule.h)
target_include_directories(MemoryModule PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
if (NOT MSVC)
set_target_properties ("MemoryModule" PROPERTIES PREFIX "")
endif ()
Expand Down
138 changes: 125 additions & 13 deletions MemoryModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
* Portions created by Joachim Bauch are Copyright (C) 2004-2015
* Joachim Bauch. All Rights Reserved.
*
*
* THeller: Added binary search in MemoryGetProcAddress function
* (#define USE_BINARY_SEARCH to enable it). This gives a very large
* speedup for libraries that exports lots of functions.
*
* These portions are Copyright (C) 2013 Thomas Heller.
*/

#include <windows.h>
Expand Down Expand Up @@ -56,9 +62,21 @@

#include "MemoryModule.h"

struct ExportNameEntry {
LPCSTR name;
WORD idx;
};

typedef BOOL (WINAPI *DllEntryProc)(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved);
typedef int (WINAPI *ExeEntryProc)(void);

#ifdef _WIN64
typedef struct POINTER_LIST {
struct POINTER_LIST *next;
void *address;
} POINTER_LIST;
#endif

typedef struct {
PIMAGE_NT_HEADERS headers;
unsigned char *codeBase;
Expand All @@ -72,9 +90,13 @@ typedef struct {
CustomLoadLibraryFunc loadLibrary;
CustomGetProcAddressFunc getProcAddress;
CustomFreeLibraryFunc freeLibrary;
struct ExportNameEntry *nameExportsTable;
void *userdata;
ExeEntryProc exeEntry;
DWORD pageSize;
#ifdef _WIN64
POINTER_LIST *blockedMemory;
#endif
} MEMORYMODULE, *PMEMORYMODULE;

typedef struct {
Expand Down Expand Up @@ -125,6 +147,21 @@ OutputLastError(const char *msg)
#endif
}

#ifdef _WIN64
static void
FreePointerList(POINTER_LIST *head, CustomFreeFunc freeMemory, void *userdata)
{
POINTER_LIST *node = head;
while (node) {
POINTER_LIST *next;
freeMemory(node->address, 0, MEM_RELEASE, userdata);
next = node->next;
free(node);
node = next;
}
}
#endif

static BOOL
CheckSize(size_t size, size_t expected) {
if (size < expected) {
Expand Down Expand Up @@ -523,6 +560,9 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size,
size_t optionalSectionSize;
size_t lastSectionEnd = 0;
size_t alignedImageSize;
#ifdef _WIN64
POINTER_LIST *blockedMemory = NULL;
#endif

if (!CheckSize(size, sizeof(IMAGE_DOS_HEADER))) {
return NULL;
Expand Down Expand Up @@ -598,9 +638,40 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size,
}
}

#ifdef _WIN64
// Memory block may not span 4 GB boundaries.
while ((((uintptr_t) code) >> 32) < (((uintptr_t) (code + alignedImageSize)) >> 32)) {
POINTER_LIST *node = (POINTER_LIST*) malloc(sizeof(POINTER_LIST));
if (!node) {
freeMemory(code, 0, MEM_RELEASE, userdata);
FreePointerList(blockedMemory, freeMemory, userdata);
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}

node->next = blockedMemory;
node->address = code;
blockedMemory = node;

code = (unsigned char *)allocMemory(NULL,
alignedImageSize,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE,
userdata);
if (code == NULL) {
FreePointerList(blockedMemory, freeMemory, userdata);
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
}
#endif

result = (PMEMORYMODULE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MEMORYMODULE));
if (result == NULL) {
freeMemory(code, 0, MEM_RELEASE, userdata);
#ifdef _WIN64
FreePointerList(blockedMemory, freeMemory, userdata);
#endif
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
Expand All @@ -614,6 +685,9 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size,
result->freeLibrary = freeLibrary;
result->userdata = userdata;
result->pageSize = sysInfo.dwPageSize;
#ifdef _WIN64
result->blockedMemory = blockedMemory;
#endif

if (!CheckSize(size, old_header->OptionalHeader.SizeOfHeaders)) {
goto error;
Expand Down Expand Up @@ -688,12 +762,27 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size,
return NULL;
}

FARPROC MemoryGetProcAddress(HMEMORYMODULE module, LPCSTR name)
static int _compare(const void *a, const void *b)
{
const struct ExportNameEntry *p1 = (const struct ExportNameEntry*) a;
const struct ExportNameEntry *p2 = (const struct ExportNameEntry*) b;
return strcmp(p1->name, p2->name);
}

static int _find(const void *a, const void *b)
{
LPCSTR *name = (LPCSTR *) a;
const struct ExportNameEntry *p = (const struct ExportNameEntry*) b;
return strcmp(*name, p->name);
}

FARPROC MemoryGetProcAddress(HMEMORYMODULE mod, LPCSTR name)
{
unsigned char *codeBase = ((PMEMORYMODULE)module)->codeBase;
PMEMORYMODULE module = (PMEMORYMODULE)mod;
unsigned char *codeBase = module->codeBase;
DWORD idx = 0;
PIMAGE_EXPORT_DIRECTORY exports;
PIMAGE_DATA_DIRECTORY directory = GET_HEADER_DICTIONARY((PMEMORYMODULE)module, IMAGE_DIRECTORY_ENTRY_EXPORT);
PIMAGE_DATA_DIRECTORY directory = GET_HEADER_DICTIONARY(module, IMAGE_DIRECTORY_ENTRY_EXPORT);
if (directory->Size == 0) {
// no export table found
SetLastError(ERROR_PROC_NOT_FOUND);
Expand All @@ -715,25 +804,44 @@ FARPROC MemoryGetProcAddress(HMEMORYMODULE module, LPCSTR name)
}

idx = LOWORD(name) - exports->Base;
} else if (!exports->NumberOfNames) {
SetLastError(ERROR_PROC_NOT_FOUND);
return NULL;
} else {
// search function name in list of exported names
DWORD i;
DWORD *nameRef = (DWORD *) (codeBase + exports->AddressOfNames);
WORD *ordinal = (WORD *) (codeBase + exports->AddressOfNameOrdinals);
BOOL found = FALSE;
for (i=0; i<exports->NumberOfNames; i++, nameRef++, ordinal++) {
if (_stricmp(name, (const char *) (codeBase + (*nameRef))) == 0) {
idx = *ordinal;
found = TRUE;
break;
const struct ExportNameEntry *found;

// Lazily build name table and sort it by names
if (!module->nameExportsTable) {
DWORD i;
DWORD *nameRef = (DWORD *) (codeBase + exports->AddressOfNames);
WORD *ordinal = (WORD *) (codeBase + exports->AddressOfNameOrdinals);
struct ExportNameEntry *entry = (struct ExportNameEntry*) malloc(exports->NumberOfNames * sizeof(struct ExportNameEntry));
module->nameExportsTable = entry;
if (!entry) {
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
for (i=0; i<exports->NumberOfNames; i++, nameRef++, ordinal++, entry++) {
entry->name = (const char *) (codeBase + (*nameRef));
entry->idx = *ordinal;
}
qsort(module->nameExportsTable,
exports->NumberOfNames,
sizeof(struct ExportNameEntry), _compare);
}

// search function name in list of exported names with binary search
found = (const struct ExportNameEntry*) bsearch(&name,
module->nameExportsTable,
exports->NumberOfNames,
sizeof(struct ExportNameEntry), _find);
if (!found) {
// exported symbol not found
SetLastError(ERROR_PROC_NOT_FOUND);
return NULL;
}

idx = found->idx;
}

if (idx > exports->NumberOfFunctions) {
Expand All @@ -759,6 +867,7 @@ void MemoryFreeLibrary(HMEMORYMODULE mod)
(*DllEntry)((HINSTANCE)module->codeBase, DLL_PROCESS_DETACH, 0);
}

free(module->nameExportsTable);
if (module->modules != NULL) {
// free previously opened libraries
int i;
Expand All @@ -776,6 +885,9 @@ void MemoryFreeLibrary(HMEMORYMODULE mod)
module->free(module->codeBase, 0, MEM_RELEASE, module->userdata);
}

#ifdef _WIN64
FreePointerList(module->blockedMemory, module->free, module->userdata);
#endif
HeapFree(GetProcessHeap(), 0, module);
}

Expand Down
Loading