libc: add mempool for rwx memory

Issue #1723
This commit is contained in:
Alexander Boettcher 2017-10-18 14:20:49 +02:00 committed by Christian Helmuth
parent e87b322616
commit d18262da1f
3 changed files with 23 additions and 14 deletions

View File

@ -29,6 +29,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
@ -427,7 +428,8 @@ extern "C" void *mmap(void *addr, ::size_t length, int prot, int flags,
/* handle requests for anonymous memory */ /* handle requests for anonymous memory */
if (!addr && libc_fd == -1) { if (!addr && libc_fd == -1) {
void *start = Libc::mem_alloc()->alloc(length, PAGE_SHIFT); bool const executable = prot & PROT_EXEC;
void *start = Libc::mem_alloc(executable)->alloc(length, PAGE_SHIFT);
mmap_registry()->insert(start, length, 0); mmap_registry()->insert(start, length, 0);
return start; return start;
} }
@ -464,8 +466,12 @@ extern "C" int munmap(void *start, ::size_t length)
int ret = 0; int ret = 0;
if (plugin) if (plugin)
ret = plugin->munmap(start, length); ret = plugin->munmap(start, length);
else else {
Libc::mem_alloc()->free(start); bool const executable = true;
/* XXX another metadata handling required to track anonymous memory */
Libc::mem_alloc(!executable)->free(start);
Libc::mem_alloc(executable)->free(start);
}
mmap_registry()->remove(start); mmap_registry()->remove(start);
return ret; return ret;

View File

@ -16,7 +16,6 @@
/* Genode includes */ /* Genode includes */
#include <base/env.h> #include <base/env.h>
#include <base/allocator_avl.h> #include <base/allocator_avl.h>
#include <base/sleep.h>
/* local includes */ /* local includes */
#include "libc_mem_alloc.h" #include "libc_mem_alloc.h"
@ -143,13 +142,19 @@ Genode::size_t Libc::Mem_alloc_impl::size_at(void const *addr) const
} }
static Libc::Mem_alloc *_libc_mem_alloc; static Libc::Mem_alloc *_libc_mem_alloc_rw = nullptr;
static Libc::Mem_alloc *_libc_mem_alloc_rwx = nullptr;
static void _init_mem_alloc(Genode::Region_map &rm, Genode::Ram_session &ram) static void _init_mem_alloc(Genode::Region_map &rm, Genode::Ram_session &ram)
{ {
static Libc::Mem_alloc_impl inst(rm, ram); enum { MEMORY_EXECUTABLE = true };
_libc_mem_alloc = &inst;
static Libc::Mem_alloc_impl inst_rw(rm, ram, !MEMORY_EXECUTABLE);
static Libc::Mem_alloc_impl inst_rwx(rm, ram, MEMORY_EXECUTABLE);
_libc_mem_alloc_rw = &inst_rw;
_libc_mem_alloc_rwx = &inst_rwx;
} }
@ -162,12 +167,10 @@ namespace Libc {
} }
Libc::Mem_alloc *Libc::mem_alloc() Libc::Mem_alloc *Libc::mem_alloc(bool executable)
{ {
if (!_libc_mem_alloc) { if (!_libc_mem_alloc_rw || !_libc_mem_alloc_rwx)
error("attempt to use 'Libc::mem_alloc' before call of 'init_mem_alloc'"); error("attempt to use 'Libc::mem_alloc' before call of 'init_mem_alloc'");
_init_mem_alloc(*env_deprecated()->rm_session(), *env_deprecated()->ram_session());
}
return _libc_mem_alloc;
}
return executable ? _libc_mem_alloc_rwx : _libc_mem_alloc_rw;
}

View File

@ -27,7 +27,7 @@ namespace Libc {
/** /**
* Return singleton instance of the memory allocator * Return singleton instance of the memory allocator
*/ */
Mem_alloc *mem_alloc(); Mem_alloc *mem_alloc(bool executable = false);
class Mem_alloc_impl : public Mem_alloc class Mem_alloc_impl : public Mem_alloc
{ {