mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-13 00:51:20 +00:00
Actually unmap the code after it's done being used
This commit is contained in:
@ -4,24 +4,20 @@
|
||||
|
||||
struct MemoryManager : llvm::RuntimeDyld::MemoryManager {
|
||||
public:
|
||||
MemoryManager(callbacks_t *callbacks) : callbacks(callbacks) {}
|
||||
MemoryManager(callbacks_t callbacks) : callbacks(callbacks) {}
|
||||
|
||||
virtual ~MemoryManager() {
|
||||
virtual ~MemoryManager() override {
|
||||
// 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);
|
||||
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* 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) {
|
||||
@ -41,21 +37,21 @@ public:
|
||||
) override {
|
||||
uint8_t *code_ptr_out = nullptr;
|
||||
size_t code_size_out = 0;
|
||||
auto code_result = callbacks->alloc_memory(code_size, PROTECT_READ_WRITE, &code_ptr_out, &code_size_out);
|
||||
auto code_result = callbacks.alloc_memory(code_size, 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;
|
||||
|
||||
uint8_t *read_ptr_out = nullptr;
|
||||
size_t read_size_out = 0;
|
||||
auto read_result = callbacks->alloc_memory(read_data_size, PROTECT_READ_WRITE, &read_ptr_out, &read_size_out);
|
||||
auto read_result = callbacks.alloc_memory(read_data_size, 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(read_write_data_size, PROTECT_READ_WRITE, &readwrite_ptr_out, &readwrite_size_out);
|
||||
auto readwrite_result = callbacks.alloc_memory(read_write_data_size, 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;
|
||||
@ -75,12 +71,12 @@ public:
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
auto read_result = callbacks.protect_memory(read_section.base, read_section.size, mem_protect_t::PROTECT_READ);
|
||||
if (read_result != RESULT_OK) {
|
||||
return false;
|
||||
}
|
||||
@ -116,7 +112,7 @@ private:
|
||||
Section code_section, read_section, readwrite_section;
|
||||
uintptr_t code_bump_ptr, read_bump_ptr, readwrite_bump_ptr;
|
||||
|
||||
callbacks_t *callbacks;
|
||||
callbacks_t callbacks;
|
||||
};
|
||||
|
||||
struct SymbolLookup : llvm::JITSymbolResolver {
|
||||
@ -153,34 +149,28 @@ private:
|
||||
WasmModule::WasmModule(
|
||||
const uint8_t *object_start,
|
||||
size_t object_size,
|
||||
callbacks_t *callbacks
|
||||
) : memory_manager(std::unique_ptr<MemoryManager>(new MemoryManager(callbacks)))
|
||||
callbacks_t callbacks
|
||||
) : memory_manager(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);
|
||||
runtime_dyld = std::unique_ptr<llvm::RuntimeDyld>(new llvm::RuntimeDyld(*memory_manager, symbol_resolver));
|
||||
|
||||
loader.setProcessAllSections(true);
|
||||
runtime_dyld->setProcessAllSections(true);
|
||||
|
||||
auto loaded_object_info = loader.loadObject(*object_file);
|
||||
loader.finalizeWithMemoryManagerLocking();
|
||||
runtime_dyld->loadObject(*object_file);
|
||||
runtime_dyld->finalizeWithMemoryManagerLocking();
|
||||
|
||||
assert(!loader.hasError());
|
||||
|
||||
symbol_table = loader.getSymbolTable();
|
||||
|
||||
for (auto const& pair : symbol_table) {
|
||||
std::cout << "symbol: (" << (std::string)pair.first << ") => " << (void*)pair.second.getAddress() << std::endl;
|
||||
if (runtime_dyld->hasError()) {
|
||||
std::cout << "RuntimeDyld error: " << (std::string)runtime_dyld->getErrorString() << std::endl;
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
void* WasmModule::get_func(llvm::StringRef name) const {
|
||||
try {
|
||||
return (void*)symbol_table.at(name).getAddress();
|
||||
} catch (const std::out_of_range& e) {
|
||||
return nullptr;
|
||||
}
|
||||
auto symbol = runtime_dyld->getSymbol(name);
|
||||
return (void*)symbol.getAddress();
|
||||
}
|
Reference in New Issue
Block a user