-
Notifications
You must be signed in to change notification settings - Fork 764
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
Pass alloc/free functions to MemoryLoadLibraryEx #33
Pass alloc/free functions to MemoryLoadLibraryEx #33
Conversation
Nice, thanks. You should also use the custom function to release the memory in |
Ah, yes. Sorry, I missed that one. I'll fix it later today and I'll update the pull request. By the way - would you like me to try and write some tests for that? |
Sure, tests for new features are always welcome to prevent regressions. Although this here doesn't really change DLL loading functionality, so I'll leave it up to you... |
I've fixed the memory releasing that I initially overlooked. As for the tests - things are becoming a bit ugly. I was thinking - would it be acceptable to make some of the custom callbak params optional. I was thinking something along these lines in if (!allocMemory) allocMemory = _VirtualAlloc;
if (!freeMemory) freeMemory = _VirtualFree;
if (!loadLibrary) loadLibrary = _LoadLibrary;
if (!getProcAddress) getProcAddress = _GetProcAddress;
if (!freeLibrary) freeLibrary = _FreeLibrary; So calling with a MemoryLoadLibraryEx(address, size, _MockAlloc, _MockFree, NULL, NULL, NULL, NULL); |
Hmm, I'm not sure about having that in You could add a |
But what if someone intentionally wants to use the default behavior for some of the parameter functions? That's not possible at the moment without copy pasting the implementations of Maybe those can be made non-static and included in the header? |
Ok, I see your point. I exposed the default functions in the referenced commit. You will need to rebase your merge request though... |
Great, thanks! I'll rebase and I'll let you know when I have the tests. |
Thanks for waiting for this. There is some difference in the behaviour between Visual Studio and gcc cross-compiled code. I'll resolve it and write again. |
The CI seems to be good now. About the tests - I went with black box testing. I tried to do something similar to mock objects. It's not perfect and I'm open to suggestions on how to improve it. I have some notes on the tests I did:
|
This is driven by a problem I solved recently. And MemoryModule helped me a lot. It works great, it's simple, it's well written and does what it should. So thanks for the great library!
The problem I was solving - I have some legacy x86 code that I want to call from an executable that I'm writing. When I say "legacy" - I mean I only have binaries. The challenge is that the code is not relocatable. That's a bit of a problem on a modern Windows. I came to the conclusion that it's virtually impossible to consistently get the base address that I want at runtime.
With some compiler/linker magic I was able to reserve a section in the image at the offset I want. However, this does not play well with the current interface of MemoryModule. The
MemoryLoadLibrary
function wants to allocate the memory itself and would not work with my memory chunk.What I did was pass
alloc
andfree
functions toMemoryLoadLibraryEx
and use those instead. The default implementations just callVirtualAlloc
/VirtualFree
. My implementation sets memory permissions on the pre-allocated memory. Here is a rough example of what I'm doing:I hope it's not an issue that this changes the interface of
MemoryLoadLibraryEx
. My rationale behind it is that this is not a public API. At least I didn't find any references to it in the documentation at http://www.joachim-bauch.de/tutorials/loading-a-dll-from-memory/TL;DR
This pull request allows for
MemoryLoadLibraryEx
to work with memory allocations different from calls toVirtualAlloc
.