mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-12 08:31:21 +00:00
Hopefully finish the memory manager implementation for llvm RuntimeDyLd
This commit is contained in:
@ -49,6 +49,7 @@ lazy_static! {
|
||||
use std::io::Read;
|
||||
let mut s = String::new();
|
||||
file.read_to_string(&mut s).unwrap();
|
||||
s.truncate(s.len() - 4);
|
||||
Some(s)
|
||||
} else {
|
||||
None
|
||||
|
@ -1,19 +1,35 @@
|
||||
#include "object_loader.hh"
|
||||
#include <llvm/ExecutionEngine/RuntimeDyld.h>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
class MemoryManager : llvm::RuntimeDyld::MemoryManager {
|
||||
struct MemoryManager : llvm::RuntimeDyld::MemoryManager {
|
||||
public:
|
||||
MemoryManager() {}
|
||||
MemoryManager(callbacks_t *callbacks) : callbacks(callbacks) {}
|
||||
|
||||
virtual ~MemoryManager() {}
|
||||
|
||||
virtual uint8_t* allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, llvm::StringRef SectionName) override {
|
||||
return nullptr;
|
||||
virtual ~MemoryManager() {
|
||||
// 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);
|
||||
}
|
||||
|
||||
virtual uint8_t* allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, llvm::StringRef SectionName, bool isReadOnly) override {
|
||||
return nullptr;
|
||||
virtual uint8_t* allocateCodeSection(uintptr_t size, unsigned alignment, unsigned section_id, llvm::StringRef section_name) override {
|
||||
std::cout << "code section name: " << (std::string)section_name << std::endl;
|
||||
|
||||
return allocate_bump(code_section, code_bump_ptr, size, alignment);
|
||||
}
|
||||
|
||||
virtual uint8_t* allocateDataSection(uintptr_t size, unsigned alignment, unsigned section_id, llvm::StringRef section_name, bool read_only) override {
|
||||
std::cout << "data section name: " << (std::string)section_name << std::endl;
|
||||
|
||||
// Allocate from the read-only section or the read-write section, depending on if this allocation
|
||||
// should be read-only or not.
|
||||
if (read_only) {
|
||||
return allocate_bump(read_section, read_bump_ptr, size, alignment);
|
||||
} else {
|
||||
return allocate_bump(readwrite_section, readwrite_bump_ptr, size, alignment);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void reserveAllocationSpace(
|
||||
@ -24,7 +40,20 @@ public:
|
||||
uintptr_t read_write_data_size,
|
||||
uint32_t read_write_data_align
|
||||
) override {
|
||||
|
||||
uint8_t *ptr_out = nullptr;
|
||||
size_t size_out = 0;
|
||||
|
||||
auto code_result = callbacks->alloc_memory(code_size, PROTECT_READ_WRITE, &ptr_out, &size_out);
|
||||
code_section = Section { ptr_out, size_out };
|
||||
code_bump_ptr = (uintptr_t)ptr_out;
|
||||
|
||||
auto read_result = callbacks->alloc_memory(read_data_size, PROTECT_READ_WRITE, &ptr_out, &size_out);
|
||||
read_section = Section { ptr_out, size_out };
|
||||
read_bump_ptr = (uintptr_t)ptr_out;
|
||||
|
||||
auto readwrite_result = callbacks->alloc_memory(read_write_data_size, PROTECT_READ_WRITE, &ptr_out, &size_out);
|
||||
readwrite_section = Section { ptr_out, size_out };
|
||||
readwrite_bump_ptr = (uintptr_t)ptr_out;
|
||||
}
|
||||
|
||||
/* Turn on the `reserveAllocationSpace` callback. */
|
||||
@ -33,38 +62,59 @@ public:
|
||||
}
|
||||
|
||||
virtual void registerEHFrames(uint8_t* Addr, uint64_t LoadAddr, size_t Size) override {
|
||||
|
||||
std::cout << "should register eh frames" << std::endl;
|
||||
}
|
||||
|
||||
virtual void deregisterEHFrames() override {
|
||||
|
||||
std::cout << "should deregister eh frames" << std::endl;
|
||||
}
|
||||
|
||||
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.
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual void notifyObjectLoaded(llvm::RuntimeDyld &RTDyld, const llvm::object::ObjectFile &Obj) override {
|
||||
|
||||
}
|
||||
virtual void notifyObjectLoaded(llvm::RuntimeDyld &RTDyld, const llvm::object::ObjectFile &Obj) override {}
|
||||
private:
|
||||
struct Section {
|
||||
uint8_t* base;
|
||||
size_t num_pages;
|
||||
size_t num_commited_bytes;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
uint8_t *image_base;
|
||||
size_t num_allocated_pages;
|
||||
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;
|
||||
}
|
||||
|
||||
Section code_section, read_section, readwrite_section;
|
||||
uintptr_t code_bump_ptr, read_bump_ptr, readwrite_bump_ptr;
|
||||
|
||||
|
||||
callbacks_t *callbacks;
|
||||
};
|
||||
|
||||
class SymbolLookup : llvm::JITSymbolResolver {
|
||||
struct SymbolLookup : llvm::JITSymbolResolver {
|
||||
public:
|
||||
virtual llvm::Expected<LookupResult> lookup(const LookupSet& symbols) override {
|
||||
LookupResult result;
|
||||
@ -93,4 +143,34 @@ private:
|
||||
|
||||
return llvm::JITEvaluatedSymbol(addr, llvm::JITSymbolFlags::None);
|
||||
}
|
||||
};
|
||||
|
||||
class WasmModule {
|
||||
public:
|
||||
WasmModule(
|
||||
const uint8_t *object_start,
|
||||
size_t object_size,
|
||||
callbacks_t *callbacks
|
||||
|
||||
) : memory_manager(std::unique_ptr<MemoryManager>(new MemoryManager(callbacks))) {
|
||||
object_file = llvm::cantFail(llvm::object::ObjectFile::createObjectFile(llvm::MemoryBufferRef(
|
||||
llvm::StringRef((const char *)object_start, object_size), "object"
|
||||
)));
|
||||
|
||||
SymbolLookup symbol_resolver;
|
||||
llvm::RuntimeDyld loader(*memory_manager, symbol_resolver);
|
||||
|
||||
loader.setProcessAllSections(true);
|
||||
|
||||
auto loaded_object_info = loader.loadObject(*object_file);
|
||||
loader.finalizeWithMemoryManagerLocking();
|
||||
|
||||
assert(!loader.hasError());
|
||||
|
||||
|
||||
|
||||
}
|
||||
private:
|
||||
std::unique_ptr<MemoryManager> memory_manager;
|
||||
std::unique_ptr<llvm::object::ObjectFile> object_file;
|
||||
};
|
@ -616,7 +616,7 @@ fn parse_function(
|
||||
let struct_value = basic_value.into_struct_value();
|
||||
for i in 0..(count as u32) {
|
||||
let value =
|
||||
builder.build_extract_value(struct_value, i, &state.var_name());
|
||||
builder.build_extract_value(struct_value, i, &state.var_name()).unwrap();
|
||||
state.push1(value);
|
||||
}
|
||||
}
|
||||
|
8
lib/llvm-backend/src/platform/mod.rs
Normal file
8
lib/llvm-backend/src/platform/mod.rs
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
#[cfg(target_family = "unix")]
|
||||
mod unix;
|
||||
#[cfg(target_family = "unix")]
|
||||
pub use self::unix::*;
|
||||
|
||||
#[cfg(target_family = "windows")]
|
||||
compile_error!("windows not yet supported for the llvm-based compiler backend");
|
40
lib/llvm-backend/src/platform/unix/mod.rs
Normal file
40
lib/llvm-backend/src/platform/unix/mod.rs
Normal file
@ -0,0 +1,40 @@
|
||||
|
||||
extern "C" {
|
||||
fn __register_frame(frame: *const u8);
|
||||
fn __deregister_frame(frame: *const u8);
|
||||
}
|
||||
|
||||
pub unsafe fn register_eh_frames(eh_frames: *const u8, num_bytes: usize) {
|
||||
visit_frame_desc_entries(eh_frames, num_bytes, |frame| __register_frame(frame));
|
||||
}
|
||||
|
||||
unsafe fn visit_frame_desc_entries<F>(eh_frames: *const u8, num_bytes: usize, visitor: F)
|
||||
where
|
||||
F: Fn(*const u8),
|
||||
{
|
||||
let mut next = eh_frames;
|
||||
let mut end = eh_frames.add(num_bytes);
|
||||
|
||||
loop {
|
||||
if next >= end {
|
||||
break;
|
||||
}
|
||||
|
||||
let cfi = next;
|
||||
let mut cfi_num_bytes = (next as *const u32).read_unaligned() as u64;
|
||||
assert!(cfi_num_bytes != 0);
|
||||
|
||||
next = next.add(4);
|
||||
if num_bytes == 0xffffffff {
|
||||
let cfi_num_bytes64 = (next as *const u64).read_unaligned();
|
||||
cfi_num_bytes = cfi_num_bytes64;
|
||||
next = next.add(8);
|
||||
}
|
||||
|
||||
let cie_offset = (next as *const u32).read_unaligned();
|
||||
if cie_offset != 0 {
|
||||
visitor(cfi);
|
||||
}
|
||||
next = next.add(cfi_num_bytes as usize);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user