Split compiled module code & data segments and use mprotect to make a module host-executable
This commit is contained in:
parent
da39ae1225
commit
98a5720141
4 changed files with 21 additions and 16 deletions
|
|
@ -7,7 +7,7 @@ namespace pslang::jit
|
||||||
{
|
{
|
||||||
itanium,
|
itanium,
|
||||||
msvc,
|
msvc,
|
||||||
arm,
|
armv8,
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,15 @@ namespace pslang::jit
|
||||||
|
|
||||||
struct compiled_module
|
struct compiled_module
|
||||||
{
|
{
|
||||||
blob memory;
|
struct segment
|
||||||
std::unordered_map<std::string, std::size_t> symbol_table;
|
{
|
||||||
std::size_t entry_point;
|
blob memory;
|
||||||
|
std::unordered_map<std::string, std::size_t> symbol_table;
|
||||||
|
};
|
||||||
|
|
||||||
|
segment data;
|
||||||
|
segment code;
|
||||||
|
std::size_t entry_point; // in code segment
|
||||||
jit::abi abi;
|
jit::abi abi;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,24 +6,23 @@
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace pslang::jit
|
namespace pslang::jit
|
||||||
{
|
{
|
||||||
|
|
||||||
compiled_module make_host_executable(compiled_module module)
|
compiled_module make_host_executable(compiled_module module)
|
||||||
{
|
{
|
||||||
#ifdef __linux__
|
#if defined(__linux__) || defined(__APPLE__)
|
||||||
if (module.abi != abi::itanium)
|
if (module.abi != abi::itanium && module.abi != abi::armv8)
|
||||||
throw std::runtime_error("Abi mismatch");
|
throw std::runtime_error("Abi mismatch");
|
||||||
|
|
||||||
auto ptr = (std::uint8_t *)mmap(nullptr, module.memory.size, PROT_READ | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
// Assume the module code memory was obtained via mmap
|
||||||
auto shared_ptr = std::shared_ptr<std::uint8_t>(ptr, [size = module.memory.size](std::uint8_t * ptr){ munmap(ptr, size); });
|
mprotect(module.code.memory.data.get(), module.code.memory.size, PROT_READ | PROT_EXEC);
|
||||||
|
|
||||||
return compiled_module {
|
return module;
|
||||||
.memory = blob(shared_ptr, module.memory.size),
|
|
||||||
.symbol_table = std::move(module.symbol_table),
|
|
||||||
.entry_point = module.entry_point,
|
|
||||||
.abi = module.abi,
|
|
||||||
};
|
|
||||||
#else
|
#else
|
||||||
throw std::runtime_error("Host-executable modules are not supported for this platform");
|
throw std::runtime_error("Host-executable modules are not supported for this platform");
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ namespace pslang::jit
|
||||||
compiled_module compile(ast::statement_list_ptr const & /* statements */, jit::abi abi)
|
compiled_module compile(ast::statement_list_ptr const & /* statements */, jit::abi abi)
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
.memory = {},
|
.data = {},
|
||||||
.symbol_table = {},
|
.code = {},
|
||||||
.entry_point = 0,
|
.entry_point = 0,
|
||||||
.abi = abi,
|
.abi = abi,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue