[Gc] Allocating Executable Memory
aph at redhat.com
Mon Jul 26 06:29:13 PDT 2010
On 07/24/2010 11:04 AM, Ivan Maidanski wrote:
> Sat, 24 Jul 2010 09:50:38 +0100 Andrew Haley <aph at redhat.com>:
>> On 07/23/2010 07:39 PM, Ivan Maidanski wrote:
>>> Fri, 23 Jul 2010 13:28:17 -0400 Noah Lavine <noah.b.lavine at gmail.com>:
>>>> I am writing with a feature request for the next version of your library.
>>>> I am working on adding JIT compilation support to GNU Guile (a Scheme
>>>> implementation), which uses this library for garbage collection. In
>>>> order to make GC work, we'll need to allocate executable memory.
>>> I like the idea.
>>> I don't mind adding this feature in this release (probably Hans wouldn't neither since the added coded is small and easily verifiable).
>>> The API should be:
>>> - GC_set_pages_executable(int) // non-zero means executable
>>> - GC_get_pages_executable() // returns non-zero if it is allowed to execute code in allocated memory
>>> NO_EXECUTE_PERMISSION controls the initial value only.
>>> If you'd like to add it, I expect you'll provide the patch against the current CVS.
>> I think you'll find this problematic in practice, at least on
>> GNU/Linux. Some distributions forbid the allocation of memory that is
>> mapped writable and executable, and I think the number of such
>> distributions will increase over time. On such systems Java is marked
>> with a special file attribute, and that attribute is writeable only by
>> The way around this is supposed to be to map pages twice, with
>> writable and executable permissions.
> Okey. Another patch is needed which adjust the method how mmaping is done.
> If I understand correctly, we just need to duplicate all relevant
> mmap and munmap calls (RW + E), right?
> If yes, we also need some config option to enable this feature
> (otherwise retain old behavior).
Unfortunately, it's harder than that. On such systems, the only way
to solve the problem is to map each page twice, at different
addresses. As far as I know, the only way to do that is to use mmap()
on a named file on some device.
In practice the easiest way to do this is to map the pages
independently of the gc and use finalizers to recycle pages once
they're no longer reachable.
More information about the Gc