32 lines
838 B
C++
32 lines
838 B
C++
#include <memory>
|
|
#include <pslang/jit/executable.hpp>
|
|
|
|
#include <stdexcept>
|
|
#include <system_error>
|
|
|
|
#ifdef __linux__
|
|
#include <sys/mman.h>
|
|
#endif
|
|
|
|
#ifdef __APPLE__
|
|
#include <sys/mman.h>
|
|
#endif
|
|
|
|
namespace pslang::jit
|
|
{
|
|
|
|
blob make_host_executable(std::vector<std::uint8_t> const & code)
|
|
{
|
|
#if defined(__linux__) || defined(__APPLE__)
|
|
auto size = code.size();
|
|
auto ptr = (std::uint8_t *)mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
|
|
std::copy(code.begin(), code.end(), ptr);
|
|
if (mprotect(ptr, size, PROT_READ | PROT_EXEC) != 0)
|
|
throw std::system_error(errno, std::generic_category());
|
|
return blob(std::shared_ptr<std::uint8_t>(ptr, [size](void * ptr){ munmap(ptr, size); }), size);
|
|
#else
|
|
throw std::runtime_error("Host-executable modules are not supported for this platform");
|
|
#endif
|
|
}
|
|
|
|
}
|