Jump to content
  • Advertisement
Sign in to follow this  

Memory Management: Hooking into allocations

This topic is 4079 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey All, I’m trying to integrate a custom memory manager on multiple platforms. All allocations that are used in our program should be redirected to our memory manager. This includes 3rd party libraries that malloc or new from within. Unfortunately, because of certain libraries we are using we are forced to link to the CRT. The CRT (libcmtd.lib) overrides malloc, free, new, and delete. This causes multiple defined symbol errors when trying to write our own version of these functions. The CRT does have hooks to allow you to set your own allocation routine but it seems to be only for debug and not final. I’m almost tempted to just scan the built executable find the symbols malloc and replace the address with my own allocation function. Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by LessBread
How about scanning the code and replacing the calls to malloc etc. with calls to your replacement functions?


When you say scan the code, do you mean source code such as find and replace? If that is what you are referring to then its not possible. Our code base does not make any calls to malloc. It is static libraries that we link to that make the calls to malloc internally.

I mentioned earlier I might be able to scan the built executable and replace them but now that I think about it I don't even know if that is possible. Some platforms don't allow modification to the code after the executable has been signed.

The only option may to find out which function calls make allocation calls and try to avoid using those functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by David Neubelt
Quote:
Original post by LessBread
How about scanning the code and replacing the calls to malloc etc. with calls to your replacement functions?


When you say scan the code, do you mean source code such as find and replace? If that is what you are referring to then its not possible. Our code base does not make any calls to malloc. It is static libraries that we link to that make the calls to malloc internally.

I mentioned earlier I might be able to scan the built executable and replace them but now that I think about it I don't even know if that is possible. Some platforms don't allow modification to the code after the executable has been signed.

The only option may to find out which function calls make allocation calls and try to avoid using those functions.


Yes, I meant scan the source code using find and replace. I see now that's not possible due to static libraries. Depending on how the compiler your using imports functions from dlls, it might be as simple as hooking the import address table in the pe file header. See iat hooking for references to implementations of that method. Calls to functions imported from dlls are typically routed through the import address table in the pe file header. Change the function address stored in the iat to point to your replacement function and you won't have to scan the code to change addresses there. I don't recall off the top of my head whether the section that stores the iat is flagged as code or data, I'm leaning towards data. Hooking the IAT won't catch dynamically loaded dlls (eg situations where LoadLibrary/GetProcAddress are used), but I doubt that would be the case with malloc etc.

Share this post


Link to post
Share on other sites
I'm not sure exactly how this is done at our company, but I know it is:

Often the linkage on new/delete and malloc/free are whats called 'weak linkage'. Create a library that defines new/delete and malloc/free with the same linkage as the standard ones. Add it to your project. You should get multiple link errors. Make sure you link your library before the standard C one and it will use yours instead. A good implementation will not cause an error if your library is linked to first. Hopefully the libraries will now use your calls instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!