Cleanup LLVM object loader.

This commit is contained in:
losfair
2019-08-21 11:08:23 -07:00
parent 90dcdfec1c
commit bf471fbc24
2 changed files with 170 additions and 157 deletions

View File

@ -5,36 +5,24 @@
extern "C" void __register_frame(uint8_t *); extern "C" void __register_frame(uint8_t *);
extern "C" void __deregister_frame(uint8_t *); extern "C" void __deregister_frame(uint8_t *);
struct MemoryManager : llvm::RuntimeDyld::MemoryManager { MemoryManager::~MemoryManager() {
public:
MemoryManager(callbacks_t callbacks) : callbacks(callbacks) {}
uint8_t *get_stack_map_ptr() const { return stack_map_ptr; }
size_t get_stack_map_size() const { return stack_map_size; }
uint8_t *get_code_ptr() const { return (uint8_t *)code_start_ptr; }
size_t get_code_size() const { return code_size; }
virtual ~MemoryManager() override {
deregisterEHFrames(); deregisterEHFrames();
// Deallocate all of the allocated memory. // Deallocate all of the allocated memory.
callbacks.dealloc_memory(code_section.base, code_section.size); callbacks.dealloc_memory(code_section.base, code_section.size);
callbacks.dealloc_memory(read_section.base, read_section.size); callbacks.dealloc_memory(read_section.base, read_section.size);
callbacks.dealloc_memory(readwrite_section.base, readwrite_section.size); callbacks.dealloc_memory(readwrite_section.base, readwrite_section.size);
} }
virtual uint8_t *allocateCodeSection(uintptr_t size, unsigned alignment, uint8_t *MemoryManager::allocateCodeSection(uintptr_t size, unsigned alignment,
unsigned section_id, unsigned section_id,
llvm::StringRef section_name) override { llvm::StringRef section_name) {
return allocate_bump(code_section, code_bump_ptr, size, alignment); return allocate_bump(code_section, code_bump_ptr, size, alignment);
} }
virtual uint8_t *allocateDataSection(uintptr_t size, unsigned alignment, uint8_t *MemoryManager::allocateDataSection(uintptr_t size, unsigned alignment,
unsigned section_id, unsigned section_id,
llvm::StringRef section_name, llvm::StringRef section_name,
bool read_only) override { bool read_only) {
// Allocate from the read-only section or the read-write section, depending // Allocate from the read-only section or the read-write section, depending
// on if this allocation should be read-only or not. // on if this allocation should be read-only or not.
uint8_t *ret; uint8_t *ret;
@ -50,20 +38,19 @@ public:
stack_map_size = size; stack_map_size = size;
} }
return ret; return ret;
} }
virtual void reserveAllocationSpace(uintptr_t code_size, uint32_t code_align, void MemoryManager::reserveAllocationSpace(uintptr_t code_size, uint32_t code_align,
uintptr_t read_data_size, uintptr_t read_data_size,
uint32_t read_data_align, uint32_t read_data_align,
uintptr_t read_write_data_size, uintptr_t read_write_data_size,
uint32_t read_write_data_align) override { uint32_t read_write_data_align) {
auto aligner = [](uintptr_t ptr, size_t align) { auto aligner = [](uintptr_t ptr, size_t align) {
if (ptr == 0) { if (ptr == 0) {
return align; return align;
} }
return (ptr + align - 1) & ~(align - 1); return (ptr + align - 1) & ~(align - 1);
}; };
uint8_t *code_ptr_out = nullptr; uint8_t *code_ptr_out = nullptr;
size_t code_size_out = 0; size_t code_size_out = 0;
auto code_result = auto code_result =
@ -92,13 +79,12 @@ public:
assert(readwrite_result == RESULT_OK); assert(readwrite_result == RESULT_OK);
readwrite_section = Section{readwrite_ptr_out, readwrite_size_out}; readwrite_section = Section{readwrite_ptr_out, readwrite_size_out};
readwrite_bump_ptr = (uintptr_t)readwrite_ptr_out; readwrite_bump_ptr = (uintptr_t)readwrite_ptr_out;
} }
/* Turn on the `reserveAllocationSpace` callback. */ bool MemoryManager::needsToReserveAllocationSpace() { return true; }
virtual bool needsToReserveAllocationSpace() override { return true; }
virtual void registerEHFrames(uint8_t *addr, uint64_t LoadAddr, void MemoryManager::registerEHFrames(uint8_t *addr, uint64_t LoadAddr,
size_t size) override { size_t size) {
// We don't know yet how to do this on Windows, so we hide this on compilation // We don't know yet how to do this on Windows, so we hide this on compilation
// so we can compile and pass spectests on unix systems // so we can compile and pass spectests on unix systems
#ifndef _WIN32 #ifndef _WIN32
@ -107,9 +93,9 @@ public:
eh_frames_registered = true; eh_frames_registered = true;
callbacks.visit_fde(addr, size, __register_frame); callbacks.visit_fde(addr, size, __register_frame);
#endif #endif
} }
virtual void deregisterEHFrames() override { void MemoryManager::deregisterEHFrames() {
// We don't know yet how to do this on Windows, so we hide this on compilation // We don't know yet how to do this on Windows, so we hide this on compilation
// so we can compile and pass spectests on unix systems // so we can compile and pass spectests on unix systems
#ifndef _WIN32 #ifndef _WIN32
@ -117,9 +103,9 @@ public:
callbacks.visit_fde(eh_frame_ptr, eh_frame_size, __deregister_frame); callbacks.visit_fde(eh_frame_ptr, eh_frame_size, __deregister_frame);
} }
#endif #endif
} }
virtual bool finalizeMemory(std::string *ErrMsg = nullptr) override { bool MemoryManager::finalizeMemory(std::string *ErrMsg) {
auto code_result = auto code_result =
callbacks.protect_memory(code_section.base, code_section.size, callbacks.protect_memory(code_section.base, code_section.size,
mem_protect_t::PROTECT_READ_EXECUTE); mem_protect_t::PROTECT_READ_EXECUTE);
@ -136,19 +122,12 @@ public:
// The readwrite section is already mapped as read-write. // The readwrite section is already mapped as read-write.
return false; return false;
} }
virtual void void MemoryManager::notifyObjectLoaded(llvm::RuntimeDyld &RTDyld,
notifyObjectLoaded(llvm::RuntimeDyld &RTDyld, const llvm::object::ObjectFile &Obj) {}
const llvm::object::ObjectFile &Obj) override {}
private: uint8_t *MemoryManager::allocate_bump(Section &section, uintptr_t &bump_ptr, size_t size,
struct Section {
uint8_t *base;
size_t size;
};
uint8_t *allocate_bump(Section &section, uintptr_t &bump_ptr, size_t size,
size_t align) { size_t align) {
auto aligner = [](uintptr_t &ptr, size_t align) { auto aligner = [](uintptr_t &ptr, size_t align) {
ptr = (ptr + align - 1) & ~(align - 1); ptr = (ptr + align - 1) & ~(align - 1);
@ -163,21 +142,7 @@ private:
assert(bump_ptr <= (uintptr_t)section.base + section.size); assert(bump_ptr <= (uintptr_t)section.base + section.size);
return (uint8_t *)ret_ptr; return (uint8_t *)ret_ptr;
} }
Section code_section, read_section, readwrite_section;
uintptr_t code_start_ptr;
size_t code_size;
uintptr_t code_bump_ptr, read_bump_ptr, readwrite_bump_ptr;
uint8_t *eh_frame_ptr;
size_t eh_frame_size;
bool eh_frames_registered = false;
callbacks_t callbacks;
uint8_t *stack_map_ptr = nullptr;
size_t stack_map_size = 0;
};
struct SymbolLookup : llvm::JITSymbolResolver { struct SymbolLookup : llvm::JITSymbolResolver {
public: public:
@ -242,25 +207,17 @@ void *WasmModule::get_func(llvm::StringRef name) const {
} }
uint8_t *WasmModule::get_stack_map_ptr() const { uint8_t *WasmModule::get_stack_map_ptr() const {
llvm::RuntimeDyld::MemoryManager &mm = *memory_manager; return memory_manager->get_stack_map_ptr();
MemoryManager *local_mm = dynamic_cast<MemoryManager *>(&mm);
return local_mm->get_stack_map_ptr();
} }
size_t WasmModule::get_stack_map_size() const { size_t WasmModule::get_stack_map_size() const {
llvm::RuntimeDyld::MemoryManager &mm = *memory_manager; return memory_manager->get_stack_map_size();
MemoryManager *local_mm = dynamic_cast<MemoryManager *>(&mm);
return local_mm->get_stack_map_size();
} }
uint8_t *WasmModule::get_code_ptr() const { uint8_t *WasmModule::get_code_ptr() const {
llvm::RuntimeDyld::MemoryManager &mm = *memory_manager; return memory_manager->get_code_ptr();
MemoryManager *local_mm = dynamic_cast<MemoryManager *>(&mm);
return local_mm->get_code_ptr();
} }
size_t WasmModule::get_code_size() const { size_t WasmModule::get_code_size() const {
llvm::RuntimeDyld::MemoryManager &mm = *memory_manager; return memory_manager->get_code_size();
MemoryManager *local_mm = dynamic_cast<MemoryManager *>(&mm);
return local_mm->get_code_size();
} }

View File

@ -1,3 +1,5 @@
#pragma once
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <exception> #include <exception>
@ -48,6 +50,60 @@ typedef struct {
size_t data, vtable; size_t data, vtable;
} box_any_t; } box_any_t;
struct MemoryManager : llvm::RuntimeDyld::MemoryManager {
public:
MemoryManager(callbacks_t callbacks) : callbacks(callbacks) {}
virtual ~MemoryManager() override;
inline uint8_t *get_stack_map_ptr() const { return stack_map_ptr; }
inline size_t get_stack_map_size() const { return stack_map_size; }
inline uint8_t *get_code_ptr() const { return (uint8_t *)code_start_ptr; }
inline size_t get_code_size() const { return code_size; }
virtual uint8_t *allocateCodeSection(uintptr_t size, unsigned alignment,
unsigned section_id,
llvm::StringRef section_name) override;
virtual uint8_t *allocateDataSection(uintptr_t size, unsigned alignment,
unsigned section_id,
llvm::StringRef section_name,
bool read_only) override;
virtual void reserveAllocationSpace(uintptr_t code_size, uint32_t code_align,
uintptr_t read_data_size,
uint32_t read_data_align,
uintptr_t read_write_data_size,
uint32_t read_write_data_align) override;
/* Turn on the `reserveAllocationSpace` callback. */
virtual bool needsToReserveAllocationSpace() override;
virtual void registerEHFrames(uint8_t *addr, uint64_t LoadAddr,
size_t size) override;
virtual void deregisterEHFrames() override;
virtual bool finalizeMemory(std::string *ErrMsg = nullptr) override;
virtual void notifyObjectLoaded(llvm::RuntimeDyld &RTDyld,
const llvm::object::ObjectFile &Obj) override;
private:
struct Section {
uint8_t *base;
size_t size;
};
uint8_t *allocate_bump(Section &section, uintptr_t &bump_ptr, size_t size,
size_t align);
Section code_section, read_section, readwrite_section;
uintptr_t code_start_ptr;
size_t code_size;
uintptr_t code_bump_ptr, read_bump_ptr, readwrite_bump_ptr;
uint8_t *eh_frame_ptr;
size_t eh_frame_size;
bool eh_frames_registered = false;
callbacks_t callbacks;
uint8_t *stack_map_ptr = nullptr;
size_t stack_map_size = 0;
};
struct WasmException { struct WasmException {
public: public:
virtual std::string description() const noexcept = 0; virtual std::string description() const noexcept = 0;
@ -97,7 +153,7 @@ public:
bool _init_failed = false; bool _init_failed = false;
private: private:
std::unique_ptr<llvm::RuntimeDyld::MemoryManager> memory_manager; std::unique_ptr<MemoryManager> memory_manager;
std::unique_ptr<llvm::object::ObjectFile> object_file; std::unique_ptr<llvm::object::ObjectFile> object_file;
std::unique_ptr<llvm::RuntimeDyld> runtime_dyld; std::unique_ptr<llvm::RuntimeDyld> runtime_dyld;
}; };