Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performant malloc implementation for executable code [closed]

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:

  • My current platform is Linux on x86_64, but Windows and ARM 32/64 support will be welcome (but not a must).
  • Memory never will be shared with different processes, but the allocator must be multi-threading aware and, if possible, scalable in this scenario.
like image 231
user3368561 Avatar asked Dec 12 '25 17:12

user3368561


1 Answers

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).

like image 199
John Zwinck Avatar answered Dec 15 '25 16:12

John Zwinck