I am generating tons of small fragments of executable machine code dynamically, all of them with unpredictable sizes and lifetimes. Currently I am using a naive executable mmap preallocated region, but this is only provisional due to the eventual memory exhaustion. I did not implemented any memory reclamation mechanism and I do not want to: this is a hard problem (hard to debug bugs, fragmentation, multi-threading, etc.).
What I need is a library that implements the malloc/free protocol but with the executable bit enabled.
Is there any malloc implementation that offers this option (can be a compile-time flag)?
Further details to answer comments:
The easiest way is to use a pool (arena) allocator. For example this one: https://github.com/philip-wernersbach/memory-pool-allocator . The implementation calls malloc() once during initialization, but on most systems if you call malloc() with a large enough size, it will use mmap(). So you could modify this library to call mprotect() or similar to make the pages executable, and maybe even replace the malloc() call with mmap() directly (because the threshold at which malloc() calls mmap() is configurable, so you may not want to rely on it).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With