From d18262da1f51ca933c774809f1a78e1e75ee0ee1 Mon Sep 17 00:00:00 2001 From: Alexander Boettcher Date: Wed, 18 Oct 2017 14:20:49 +0200 Subject: [PATCH] libc: add mempool for rwx memory Issue #1723 --- .../libports/src/lib/libc/file_operations.cc | 12 +++++++--- repos/libports/src/lib/libc/libc_mem_alloc.cc | 23 +++++++++++-------- repos/libports/src/lib/libc/libc_mem_alloc.h | 2 +- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/repos/libports/src/lib/libc/file_operations.cc b/repos/libports/src/lib/libc/file_operations.cc index ffa7507775..a19818c814 100644 --- a/repos/libports/src/lib/libc/file_operations.cc +++ b/repos/libports/src/lib/libc/file_operations.cc @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -427,7 +428,8 @@ extern "C" void *mmap(void *addr, ::size_t length, int prot, int flags, /* handle requests for anonymous memory */ 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); return start; } @@ -464,8 +466,12 @@ extern "C" int munmap(void *start, ::size_t length) int ret = 0; if (plugin) ret = plugin->munmap(start, length); - else - Libc::mem_alloc()->free(start); + else { + 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); return ret; diff --git a/repos/libports/src/lib/libc/libc_mem_alloc.cc b/repos/libports/src/lib/libc/libc_mem_alloc.cc index 45c2d6c9e4..22cdb9144f 100644 --- a/repos/libports/src/lib/libc/libc_mem_alloc.cc +++ b/repos/libports/src/lib/libc/libc_mem_alloc.cc @@ -16,7 +16,6 @@ /* Genode includes */ #include #include -#include /* local includes */ #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 Libc::Mem_alloc_impl inst(rm, ram); - _libc_mem_alloc = &inst; + enum { MEMORY_EXECUTABLE = true }; + + 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'"); - _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; +} diff --git a/repos/libports/src/lib/libc/libc_mem_alloc.h b/repos/libports/src/lib/libc/libc_mem_alloc.h index 7ea6f0a0d2..4e4ee80e23 100644 --- a/repos/libports/src/lib/libc/libc_mem_alloc.h +++ b/repos/libports/src/lib/libc/libc_mem_alloc.h @@ -27,7 +27,7 @@ namespace Libc { /** * 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 {