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,179 +5,144 @@
extern "C" void __register_frame(uint8_t *);
extern "C" void __deregister_frame(uint8_t *);
struct MemoryManager : llvm::RuntimeDyld::MemoryManager {
public:
MemoryManager(callbacks_t callbacks) : callbacks(callbacks) {}
MemoryManager::~MemoryManager() {
deregisterEHFrames();
// Deallocate all of the allocated memory.
callbacks.dealloc_memory(code_section.base, code_section.size);
callbacks.dealloc_memory(read_section.base, read_section.size);
callbacks.dealloc_memory(readwrite_section.base, readwrite_section.size);
}
uint8_t *get_stack_map_ptr() const { return stack_map_ptr; }
uint8_t *MemoryManager::allocateCodeSection(uintptr_t size, unsigned alignment,
unsigned section_id,
llvm::StringRef section_name) {
return allocate_bump(code_section, code_bump_ptr, size, alignment);
}
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();
// Deallocate all of the allocated memory.
callbacks.dealloc_memory(code_section.base, code_section.size);
callbacks.dealloc_memory(read_section.base, read_section.size);
callbacks.dealloc_memory(readwrite_section.base, readwrite_section.size);
uint8_t *MemoryManager::allocateDataSection(uintptr_t size, unsigned alignment,
unsigned section_id,
llvm::StringRef section_name,
bool read_only) {
// Allocate from the read-only section or the read-write section, depending
// on if this allocation should be read-only or not.
uint8_t *ret;
if (read_only) {
ret = allocate_bump(read_section, read_bump_ptr, size, alignment);
} else {
ret =
allocate_bump(readwrite_section, readwrite_bump_ptr, size, alignment);
}
virtual uint8_t *allocateCodeSection(uintptr_t size, unsigned alignment,
unsigned section_id,
llvm::StringRef section_name) override {
return allocate_bump(code_section, code_bump_ptr, size, alignment);
if (section_name.equals(llvm::StringRef("__llvm_stackmaps")) ||
section_name.equals(llvm::StringRef(".llvm_stackmaps"))) {
stack_map_ptr = ret;
stack_map_size = size;
}
return ret;
}
virtual uint8_t *allocateDataSection(uintptr_t size, unsigned alignment,
unsigned section_id,
llvm::StringRef section_name,
bool read_only) override {
// Allocate from the read-only section or the read-write section, depending
// on if this allocation should be read-only or not.
uint8_t *ret;
if (read_only) {
ret = allocate_bump(read_section, read_bump_ptr, size, alignment);
} else {
ret =
allocate_bump(readwrite_section, readwrite_bump_ptr, size, alignment);
}
if (section_name.equals(llvm::StringRef("__llvm_stackmaps")) ||
section_name.equals(llvm::StringRef(".llvm_stackmaps"))) {
stack_map_ptr = ret;
stack_map_size = size;
}
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,
uint32_t read_data_align,
uintptr_t read_write_data_size,
uint32_t read_write_data_align) override {
auto aligner = [](uintptr_t ptr, size_t align) {
if (ptr == 0) {
return align;
}
return (ptr + align - 1) & ~(align - 1);
};
uint32_t read_write_data_align) {
auto aligner = [](uintptr_t ptr, size_t align) {
if (ptr == 0) {
return align;
}
return (ptr + align - 1) & ~(align - 1);
};
uint8_t *code_ptr_out = nullptr;
size_t code_size_out = 0;
auto code_result =
callbacks.alloc_memory(aligner(code_size, 4096), PROTECT_READ_WRITE,
&code_ptr_out, &code_size_out);
assert(code_result == RESULT_OK);
code_section = Section{code_ptr_out, code_size_out};
code_bump_ptr = (uintptr_t)code_ptr_out;
code_start_ptr = (uintptr_t)code_ptr_out;
this->code_size = code_size;
uint8_t *code_ptr_out = nullptr;
size_t code_size_out = 0;
auto code_result =
callbacks.alloc_memory(aligner(code_size, 4096), PROTECT_READ_WRITE,
&code_ptr_out, &code_size_out);
assert(code_result == RESULT_OK);
code_section = Section{code_ptr_out, code_size_out};
code_bump_ptr = (uintptr_t)code_ptr_out;
code_start_ptr = (uintptr_t)code_ptr_out;
this->code_size = code_size;
uint8_t *read_ptr_out = nullptr;
size_t read_size_out = 0;
auto read_result = callbacks.alloc_memory(aligner(read_data_size, 4096),
PROTECT_READ_WRITE, &read_ptr_out,
&read_size_out);
assert(read_result == RESULT_OK);
read_section = Section{read_ptr_out, read_size_out};
read_bump_ptr = (uintptr_t)read_ptr_out;
uint8_t *read_ptr_out = nullptr;
size_t read_size_out = 0;
auto read_result = callbacks.alloc_memory(aligner(read_data_size, 4096),
PROTECT_READ_WRITE, &read_ptr_out,
&read_size_out);
assert(read_result == RESULT_OK);
read_section = Section{read_ptr_out, read_size_out};
read_bump_ptr = (uintptr_t)read_ptr_out;
uint8_t *readwrite_ptr_out = nullptr;
size_t readwrite_size_out = 0;
auto readwrite_result = callbacks.alloc_memory(
aligner(read_write_data_size, 4096), PROTECT_READ_WRITE,
&readwrite_ptr_out, &readwrite_size_out);
assert(readwrite_result == RESULT_OK);
readwrite_section = Section{readwrite_ptr_out, readwrite_size_out};
readwrite_bump_ptr = (uintptr_t)readwrite_ptr_out;
}
uint8_t *readwrite_ptr_out = nullptr;
size_t readwrite_size_out = 0;
auto readwrite_result = callbacks.alloc_memory(
aligner(read_write_data_size, 4096), PROTECT_READ_WRITE,
&readwrite_ptr_out, &readwrite_size_out);
assert(readwrite_result == RESULT_OK);
readwrite_section = Section{readwrite_ptr_out, readwrite_size_out};
readwrite_bump_ptr = (uintptr_t)readwrite_ptr_out;
}
bool MemoryManager::needsToReserveAllocationSpace() { return true; }
/* Turn on the `reserveAllocationSpace` callback. */
virtual bool needsToReserveAllocationSpace() override { return true; }
virtual void registerEHFrames(uint8_t *addr, uint64_t LoadAddr,
size_t size) override {
void MemoryManager::registerEHFrames(uint8_t *addr, uint64_t LoadAddr,
size_t size) {
// 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
#ifndef _WIN32
eh_frame_ptr = addr;
eh_frame_size = size;
eh_frames_registered = true;
callbacks.visit_fde(addr, size, __register_frame);
eh_frame_ptr = addr;
eh_frame_size = size;
eh_frames_registered = true;
callbacks.visit_fde(addr, size, __register_frame);
#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
// so we can compile and pass spectests on unix systems
#ifndef _WIN32
if (eh_frames_registered) {
callbacks.visit_fde(eh_frame_ptr, eh_frame_size, __deregister_frame);
}
#endif
if (eh_frames_registered) {
callbacks.visit_fde(eh_frame_ptr, eh_frame_size, __deregister_frame);
}
#endif
}
virtual bool finalizeMemory(std::string *ErrMsg = nullptr) override {
auto code_result =
callbacks.protect_memory(code_section.base, code_section.size,
mem_protect_t::PROTECT_READ_EXECUTE);
if (code_result != RESULT_OK) {
return false;
}
auto read_result = callbacks.protect_memory(
read_section.base, read_section.size, mem_protect_t::PROTECT_READ);
if (read_result != RESULT_OK) {
return false;
}
// The readwrite section is already mapped as read-write.
bool MemoryManager::finalizeMemory(std::string *ErrMsg) {
auto code_result =
callbacks.protect_memory(code_section.base, code_section.size,
mem_protect_t::PROTECT_READ_EXECUTE);
if (code_result != RESULT_OK) {
return false;
}
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) {
auto aligner = [](uintptr_t &ptr, size_t align) {
ptr = (ptr + align - 1) & ~(align - 1);
};
// Align the bump pointer to the requires alignment.
aligner(bump_ptr, align);
auto ret_ptr = bump_ptr;
bump_ptr += size;
assert(bump_ptr <= (uintptr_t)section.base + section.size);
return (uint8_t *)ret_ptr;
auto read_result = callbacks.protect_memory(
read_section.base, read_section.size, mem_protect_t::PROTECT_READ);
if (read_result != RESULT_OK) {
return false;
}
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;
// The readwrite section is already mapped as read-write.
callbacks_t callbacks;
return false;
}
uint8_t *stack_map_ptr = nullptr;
size_t stack_map_size = 0;
};
void MemoryManager::notifyObjectLoaded(llvm::RuntimeDyld &RTDyld,
const llvm::object::ObjectFile &Obj) {}
uint8_t *MemoryManager::allocate_bump(Section &section, uintptr_t &bump_ptr, size_t size,
size_t align) {
auto aligner = [](uintptr_t &ptr, size_t align) {
ptr = (ptr + align - 1) & ~(align - 1);
};
// Align the bump pointer to the requires alignment.
aligner(bump_ptr, align);
auto ret_ptr = bump_ptr;
bump_ptr += size;
assert(bump_ptr <= (uintptr_t)section.base + section.size);
return (uint8_t *)ret_ptr;
}
struct SymbolLookup : llvm::JITSymbolResolver {
public:
@ -242,25 +207,17 @@ void *WasmModule::get_func(llvm::StringRef name) const {
}
uint8_t *WasmModule::get_stack_map_ptr() const {
llvm::RuntimeDyld::MemoryManager &mm = *memory_manager;
MemoryManager *local_mm = dynamic_cast<MemoryManager *>(&mm);
return local_mm->get_stack_map_ptr();
return memory_manager->get_stack_map_ptr();
}
size_t WasmModule::get_stack_map_size() const {
llvm::RuntimeDyld::MemoryManager &mm = *memory_manager;
MemoryManager *local_mm = dynamic_cast<MemoryManager *>(&mm);
return local_mm->get_stack_map_size();
return memory_manager->get_stack_map_size();
}
uint8_t *WasmModule::get_code_ptr() const {
llvm::RuntimeDyld::MemoryManager &mm = *memory_manager;
MemoryManager *local_mm = dynamic_cast<MemoryManager *>(&mm);
return local_mm->get_code_ptr();
return memory_manager->get_code_ptr();
}
size_t WasmModule::get_code_size() const {
llvm::RuntimeDyld::MemoryManager &mm = *memory_manager;
MemoryManager *local_mm = dynamic_cast<MemoryManager *>(&mm);
return local_mm->get_code_size();
return memory_manager->get_code_size();
}