mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-24 22:21:32 +00:00
Merge remote-tracking branch 'origin/master' into feature/llvm-osr
This commit is contained in:
@ -1,30 +1,40 @@
|
||||
[package]
|
||||
name = "wasmer-llvm-backend"
|
||||
version = "0.5.6"
|
||||
authors = ["Lachlan Sneff <lachlan.sneff@gmail.com>"]
|
||||
version = "0.6.0"
|
||||
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
|
||||
edition = "2018"
|
||||
readme = "README.md"
|
||||
|
||||
[dependencies]
|
||||
wasmer-runtime-core = { path = "../runtime-core", version = "0.5.6" }
|
||||
inkwell = { git = "https://github.com/wasmerio/inkwell", branch = "llvm7-0" }
|
||||
wasmparser = "0.32.1"
|
||||
hashbrown = "0.1.8"
|
||||
smallvec = "0.6.8"
|
||||
goblin = "0.0.20"
|
||||
libc = "0.2.49"
|
||||
nix = "0.14.0"
|
||||
capstone = { version = "0.5.0", optional = true }
|
||||
wasmer-runtime-core = { path = "../runtime-core", version = "0.6.0" }
|
||||
wasmparser = "0.35.1"
|
||||
smallvec = "0.6.10"
|
||||
goblin = "0.0.24"
|
||||
libc = "0.2.60"
|
||||
capstone = { version = "0.6.0", optional = true }
|
||||
byteorder = "1"
|
||||
|
||||
[dependencies.inkwell]
|
||||
git = "https://github.com/wasmerio/inkwell"
|
||||
branch = "llvm8-0"
|
||||
default-features = false
|
||||
features = ["llvm8-0", "target-x86"]
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
nix = "0.14.1"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
winapi = { version = "0.3.7", features = ["memoryapi"] }
|
||||
|
||||
[build-dependencies]
|
||||
cc = "1.0"
|
||||
lazy_static = "1.2.0"
|
||||
regex = "1.1.0"
|
||||
lazy_static = "1.3.0"
|
||||
regex = "1.2.1"
|
||||
semver = "0.9"
|
||||
rustc_version = "0.2.3"
|
||||
|
||||
[dev-dependencies]
|
||||
wabt = "0.7.4"
|
||||
wabt = "0.9.1"
|
||||
|
||||
[features]
|
||||
debug = ["wasmer-runtime-core/debug"]
|
||||
|
@ -28,4 +28,26 @@ Wasmer is a standalone JIT WebAssembly runtime, aiming to be fully
|
||||
compatible with Emscripten, Rust and Go. [Learn
|
||||
more](https://github.com/wasmerio/wasmer).
|
||||
|
||||
This crate represents the LLVM backend.
|
||||
This crate represents the LLVM backend integration for Wasmer.
|
||||
|
||||
## Usage
|
||||
|
||||
### Usage in Wasmer Standalone
|
||||
|
||||
If you are using the `wasmer` CLI, you can specify the backend with:
|
||||
|
||||
```bash
|
||||
wasmer run program.wasm --backend=llvm
|
||||
```
|
||||
|
||||
### Usage in Wasmer Embedded
|
||||
|
||||
If you are using Wasmer Embedded, you can specify
|
||||
the LLVM backend to the [`compile_with` function](https://docs.rs/wasmer-runtime-core/*/wasmer_runtime_core/fn.compile_with.html):
|
||||
|
||||
```rust
|
||||
use wasmer_llvm_backend::LLVMCompiler;
|
||||
|
||||
// ...
|
||||
let module = wasmer_runtime_core::compile_with(&wasm_binary[..], &LLVMCompiler::new());
|
||||
```
|
||||
|
@ -1,5 +1,5 @@
|
||||
//! This file was mostly taken from the llvm-sys crate.
|
||||
//! (https://bitbucket.org/tari/llvm-sys.rs/src/21ab524ec4df1450035df895209c3f8fbeb8775f/build.rs?at=default&fileviewer=file-view-default)
|
||||
//! (https://bitbucket.org/tari/llvm-sys.rs/raw/94361c1083a88f439b9d24c59b2d2831517413d7/build.rs)
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
@ -10,23 +10,54 @@ use std::io::{self, ErrorKind};
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
// Version of the llvm-sys crate that we (through inkwell) depend on.
|
||||
const LLVM_SYS_MAJOR_VERSION: &str = "80";
|
||||
const LLVM_SYS_MINOR_VERSION: &str = "0";
|
||||
|
||||
// Environment variables that can guide compilation
|
||||
//
|
||||
// When adding new ones, they should also be added to main() to force a
|
||||
// rebuild if they are changed.
|
||||
lazy_static! {
|
||||
|
||||
/// A single path to search for LLVM in (containing bin/llvm-config)
|
||||
static ref ENV_LLVM_PREFIX: String =
|
||||
format!("LLVM_SYS_{}_PREFIX", LLVM_SYS_MAJOR_VERSION);
|
||||
|
||||
/// If exactly "YES", ignore the version blacklist
|
||||
static ref ENV_IGNORE_BLACKLIST: String =
|
||||
format!("LLVM_SYS_{}_IGNORE_BLACKLIST", LLVM_SYS_MAJOR_VERSION);
|
||||
|
||||
/// If set, enforce precise correspondence between crate and binary versions.
|
||||
static ref ENV_STRICT_VERSIONING: String =
|
||||
format!("LLVM_SYS_{}_STRICT_VERSIONING", LLVM_SYS_MAJOR_VERSION);
|
||||
|
||||
/// If set, do not attempt to strip irrelevant options for llvm-config --cflags
|
||||
static ref ENV_NO_CLEAN_CXXFLAGS: String =
|
||||
format!("LLVM_SYS_{}_NO_CLEAN_CXXFLAGS", LLVM_SYS_MAJOR_VERSION);
|
||||
|
||||
/// If set and targeting MSVC, force the debug runtime library
|
||||
static ref ENV_USE_DEBUG_MSVCRT: String =
|
||||
format!("LLVM_SYS_{}_USE_DEBUG_MSVCRT", LLVM_SYS_MAJOR_VERSION);
|
||||
|
||||
/// If set, always link against libffi
|
||||
static ref ENV_FORCE_FFI: String =
|
||||
format!("LLVM_SYS_{}_FFI_WORKAROUND", LLVM_SYS_MAJOR_VERSION);
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// LLVM version used by this version of the crate.
|
||||
static ref CRATE_VERSION: Version = {
|
||||
let crate_version = Version::parse(env!("CARGO_PKG_VERSION"))
|
||||
.expect("Crate version is somehow not valid semver");
|
||||
Version {
|
||||
major: crate_version.major / 10,
|
||||
minor: crate_version.major % 10,
|
||||
.. crate_version
|
||||
}
|
||||
Version::new(LLVM_SYS_MAJOR_VERSION.parse::<u64>().unwrap() / 10,
|
||||
LLVM_SYS_MINOR_VERSION.parse::<u64>().unwrap() % 10,
|
||||
0)
|
||||
};
|
||||
|
||||
static ref LLVM_CONFIG_BINARY_NAMES: Vec<String> = {
|
||||
vec![
|
||||
"llvm-config".into(),
|
||||
// format!("llvm-config-{}", CRATE_VERSION.major),
|
||||
// format!("llvm-config-{}.{}", CRATE_VERSION.major, CRATE_VERSION.minor),
|
||||
format!("llvm-config-{}", CRATE_VERSION.major),
|
||||
format!("llvm-config-{}.{}", CRATE_VERSION.major, CRATE_VERSION.minor),
|
||||
]
|
||||
};
|
||||
|
||||
@ -41,21 +72,7 @@ lazy_static! {
|
||||
|
||||
// Did the user give us a binary path to use? If yes, try
|
||||
// to use that and fail if it doesn't work.
|
||||
let binary_prefix_var = "LLVM_SYS_70_PREFIX";
|
||||
|
||||
let path = if let Some(path) = env::var_os(&binary_prefix_var) {
|
||||
Some(path.to_str().unwrap().to_owned())
|
||||
} else if let Ok(mut file) = std::fs::File::open(".llvmenv") {
|
||||
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
|
||||
};
|
||||
|
||||
if let Some(path) = path {
|
||||
if let Some(path) = env::var_os(&*ENV_LLVM_PREFIX) {
|
||||
for binary_name in LLVM_CONFIG_BINARY_NAMES.iter() {
|
||||
let mut pb: PathBuf = path.clone().into();
|
||||
pb.push("bin");
|
||||
@ -67,7 +84,7 @@ lazy_static! {
|
||||
return pb;
|
||||
} else {
|
||||
println!("LLVM binaries specified by {} are the wrong version.
|
||||
(Found {}, need {}.)", binary_prefix_var, ver, *CRATE_VERSION);
|
||||
(Found {}, need {}.)", *ENV_LLVM_PREFIX, ver, *CRATE_VERSION);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -79,7 +96,7 @@ lazy_static! {
|
||||
refer to the llvm-sys documentation for more information.
|
||||
|
||||
llvm-sys: https://crates.io/crates/llvm-sys
|
||||
llvmenv: https://crates.io/crates/llvmenv", binary_prefix_var);
|
||||
llvmenv: https://crates.io/crates/llvmenv", *ENV_LLVM_PREFIX);
|
||||
panic!("Could not find a compatible version of LLVM");
|
||||
};
|
||||
}
|
||||
@ -115,15 +132,55 @@ fn locate_system_llvm_config() -> Option<&'static str> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Check whether the given version of LLVM is blacklisted,
|
||||
/// returning `Some(reason)` if it is.
|
||||
fn is_blacklisted_llvm(llvm_version: &Version) -> Option<&'static str> {
|
||||
static BLACKLIST: &'static [(u64, u64, u64, &'static str)] = &[];
|
||||
|
||||
if let Some(x) = env::var_os(&*ENV_IGNORE_BLACKLIST) {
|
||||
if &x == "YES" {
|
||||
println!(
|
||||
"cargo:warning=Ignoring blacklist entry for LLVM {}",
|
||||
llvm_version
|
||||
);
|
||||
return None;
|
||||
} else {
|
||||
println!(
|
||||
"cargo:warning={} is set but not exactly \"YES\"; blacklist is still honored.",
|
||||
*ENV_IGNORE_BLACKLIST
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for &(major, minor, patch, reason) in BLACKLIST.iter() {
|
||||
let bad_version = Version {
|
||||
major: major,
|
||||
minor: minor,
|
||||
patch: patch,
|
||||
pre: vec![],
|
||||
build: vec![],
|
||||
};
|
||||
|
||||
if &bad_version == llvm_version {
|
||||
return Some(reason);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Check whether the given LLVM version is compatible with this version of
|
||||
/// the crate.
|
||||
fn is_compatible_llvm(llvm_version: &Version) -> bool {
|
||||
let strict = env::var_os(format!(
|
||||
"LLVM_SYS_{}_STRICT_VERSIONING",
|
||||
env!("CARGO_PKG_VERSION_MAJOR")
|
||||
))
|
||||
.is_some()
|
||||
|| cfg!(feature = "strict-versioning");
|
||||
if let Some(reason) = is_blacklisted_llvm(llvm_version) {
|
||||
println!(
|
||||
"Found LLVM {}, which is blacklisted: {}",
|
||||
llvm_version, reason
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
let strict =
|
||||
env::var_os(&*ENV_STRICT_VERSIONING).is_some() || cfg!(feature = "strict-versioning");
|
||||
if strict {
|
||||
llvm_version.major == CRATE_VERSION.major && llvm_version.minor == CRATE_VERSION.minor
|
||||
} else {
|
||||
@ -184,11 +241,7 @@ fn get_llvm_cxxflags() -> String {
|
||||
// include flags that aren't understood by the default compiler we're
|
||||
// using. Unless requested otherwise, clean CFLAGS of options that are
|
||||
// known to be possibly-harmful.
|
||||
let no_clean = env::var_os(format!(
|
||||
"LLVM_SYS_{}_NO_CLEAN_CFLAGS",
|
||||
env!("CARGO_PKG_VERSION_MAJOR")
|
||||
))
|
||||
.is_some();
|
||||
let no_clean = env::var_os(&*ENV_NO_CLEAN_CXXFLAGS).is_some();
|
||||
if no_clean || cfg!(target_env = "msvc") {
|
||||
// MSVC doesn't accept -W... options, so don't try to strip them and
|
||||
// possibly strip something that should be retained. Also do nothing if
|
||||
@ -204,20 +257,43 @@ fn get_llvm_cxxflags() -> String {
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
fn is_llvm_debug() -> bool {
|
||||
// Has to be either Debug or Release
|
||||
llvm_config("--build-mode").contains("Debug")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rustc-link-lib=static=llvm-backend");
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
println!("cargo:rerun-if-changed=cpp/object_loader.cpp");
|
||||
println!("cargo:rerun-if-changed=cpp/object_loader.hh");
|
||||
println!("cargo:rerun-if-env-changed={}", &*ENV_LLVM_PREFIX);
|
||||
println!("cargo:rerun-if-env-changed={}", &*ENV_IGNORE_BLACKLIST);
|
||||
println!("cargo:rerun-if-env-changed={}", &*ENV_STRICT_VERSIONING);
|
||||
println!("cargo:rerun-if-env-changed={}", &*ENV_NO_CLEAN_CXXFLAGS);
|
||||
println!("cargo:rerun-if-env-changed={}", &*ENV_USE_DEBUG_MSVCRT);
|
||||
println!("cargo:rerun-if-env-changed={}", &*ENV_FORCE_FFI);
|
||||
|
||||
std::env::set_var("CXXFLAGS", get_llvm_cxxflags());
|
||||
cc::Build::new()
|
||||
.cpp(true)
|
||||
.file("cpp/object_loader.cpp")
|
||||
.compile("llvm-backend");
|
||||
|
||||
println!("cargo:rustc-link-lib=static=llvm-backend");
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
println!("cargo:rerun-if-changed=cpp/object_loader.cpp");
|
||||
println!("cargo:rerun-if-changed=cpp/object_loader.hh");
|
||||
|
||||
// Enable "nightly" cfg if the current compiler is nightly.
|
||||
if rustc_version::version_meta().unwrap().channel == rustc_version::Channel::Nightly {
|
||||
println!("cargo:rustc-cfg=nightly");
|
||||
}
|
||||
|
||||
let use_debug_msvcrt = env::var_os(&*ENV_USE_DEBUG_MSVCRT).is_some();
|
||||
if cfg!(target_env = "msvc") && (use_debug_msvcrt || is_llvm_debug()) {
|
||||
println!("cargo:rustc-link-lib={}", "msvcrtd");
|
||||
}
|
||||
|
||||
// Link libffi if the user requested this workaround.
|
||||
// See https://bitbucket.org/tari/llvm-sys.rs/issues/12/
|
||||
let force_ffi = env::var_os(&*ENV_FORCE_FFI).is_some();
|
||||
if force_ffi {
|
||||
println!("cargo:rustc-link-lib=dylib={}", "ffi");
|
||||
}
|
||||
}
|
||||
|
@ -93,62 +93,76 @@ public:
|
||||
readwrite_bump_ptr = (uintptr_t)readwrite_ptr_out;
|
||||
}
|
||||
|
||||
/* Turn on the `reserveAllocationSpace` callback. */
|
||||
virtual bool needsToReserveAllocationSpace() override {
|
||||
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 {
|
||||
// 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);
|
||||
#endif
|
||||
}
|
||||
|
||||
virtual void deregisterEHFrames() override {
|
||||
// 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
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
virtual void registerEHFrames(uint8_t* addr, uint64_t LoadAddr, size_t size) override {
|
||||
eh_frame_ptr = addr;
|
||||
eh_frame_size = size;
|
||||
eh_frames_registered = true;
|
||||
callbacks.visit_fde(addr, size, __register_frame);
|
||||
auto read_result = callbacks.protect_memory(
|
||||
read_section.base, read_section.size, mem_protect_t::PROTECT_READ);
|
||||
if (read_result != RESULT_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual void deregisterEHFrames() override {
|
||||
if (eh_frames_registered) {
|
||||
callbacks.visit_fde(eh_frame_ptr, eh_frame_size, __deregister_frame);
|
||||
}
|
||||
}
|
||||
// The readwrite section is already mapped as read-write.
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
virtual void
|
||||
notifyObjectLoaded(llvm::RuntimeDyld &RTDyld,
|
||||
const llvm::object::ObjectFile &Obj) override {}
|
||||
|
||||
// The readwrite section is already mapped as read-write.
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual void notifyObjectLoaded(llvm::RuntimeDyld &RTDyld, const llvm::object::ObjectFile &Obj) override {}
|
||||
private:
|
||||
struct Section {
|
||||
uint8_t* base;
|
||||
size_t size;
|
||||
struct Section {
|
||||
uint8_t *base;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
uint8_t *allocate_bump(Section §ion, uintptr_t &bump_ptr, size_t size,
|
||||
size_t align) {
|
||||
auto aligner = [](uintptr_t &ptr, size_t align) {
|
||||
ptr = (ptr + align - 1) & ~(align - 1);
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
// Align the bump pointer to the requires alignment.
|
||||
aligner(bump_ptr, align);
|
||||
auto ret_ptr = bump_ptr;
|
||||
bump_ptr += size;
|
||||
|
||||
auto ret_ptr = bump_ptr;
|
||||
bump_ptr += size;
|
||||
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;
|
||||
@ -166,65 +180,59 @@ private:
|
||||
|
||||
struct SymbolLookup : llvm::JITSymbolResolver {
|
||||
public:
|
||||
SymbolLookup(callbacks_t callbacks) : callbacks(callbacks) {}
|
||||
SymbolLookup(callbacks_t callbacks) : callbacks(callbacks) {}
|
||||
|
||||
virtual llvm::Expected<LookupResult> lookup(const LookupSet& symbols) override {
|
||||
LookupResult result;
|
||||
void lookup(const LookupSet &symbols, OnResolvedFunction OnResolved) {
|
||||
LookupResult result;
|
||||
|
||||
for (auto symbol : symbols) {
|
||||
result.emplace(symbol, symbol_lookup(symbol));
|
||||
}
|
||||
|
||||
return result;
|
||||
for (auto symbol : symbols) {
|
||||
result.emplace(symbol, symbol_lookup(symbol));
|
||||
}
|
||||
|
||||
virtual llvm::Expected<LookupFlagsResult> lookupFlags(const LookupSet& symbols) override {
|
||||
LookupFlagsResult result;
|
||||
OnResolved(result);
|
||||
}
|
||||
|
||||
for (auto symbol : symbols) {
|
||||
result.emplace(symbol, symbol_lookup(symbol).getFlags());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
llvm::Expected<LookupSet> getResponsibilitySet(const LookupSet &Symbols) {
|
||||
const std::set<llvm::StringRef> empty;
|
||||
return empty;
|
||||
}
|
||||
|
||||
private:
|
||||
llvm::JITEvaluatedSymbol symbol_lookup(llvm::StringRef name) {
|
||||
uint64_t addr = callbacks.lookup_vm_symbol(name.data(), name.size());
|
||||
llvm::JITEvaluatedSymbol symbol_lookup(llvm::StringRef name) {
|
||||
uint64_t addr = callbacks.lookup_vm_symbol(name.data(), name.size());
|
||||
|
||||
return llvm::JITEvaluatedSymbol(addr, llvm::JITSymbolFlags::None);
|
||||
}
|
||||
return llvm::JITEvaluatedSymbol(addr, llvm::JITSymbolFlags::None);
|
||||
}
|
||||
|
||||
callbacks_t callbacks;
|
||||
callbacks_t callbacks;
|
||||
};
|
||||
|
||||
WasmModule::WasmModule(
|
||||
const uint8_t *object_start,
|
||||
size_t object_size,
|
||||
callbacks_t callbacks
|
||||
) : memory_manager(std::unique_ptr<MemoryManager>(new MemoryManager(callbacks)))
|
||||
{
|
||||
|
||||
WasmModule::WasmModule(const uint8_t *object_start, size_t object_size,
|
||||
callbacks_t callbacks)
|
||||
: memory_manager(
|
||||
std::unique_ptr<MemoryManager>(new MemoryManager(callbacks))) {
|
||||
|
||||
if (auto created_object_file = llvm::object::ObjectFile::createObjectFile(llvm::MemoryBufferRef(
|
||||
llvm::StringRef((const char *)object_start, object_size), "object"
|
||||
))) {
|
||||
object_file = cantFail(std::move(created_object_file));
|
||||
SymbolLookup symbol_resolver(callbacks);
|
||||
runtime_dyld = std::unique_ptr<llvm::RuntimeDyld>(new llvm::RuntimeDyld(*memory_manager, symbol_resolver));
|
||||
if (auto created_object_file =
|
||||
llvm::object::ObjectFile::createObjectFile(llvm::MemoryBufferRef(
|
||||
llvm::StringRef((const char *)object_start, object_size),
|
||||
"object"))) {
|
||||
object_file = cantFail(std::move(created_object_file));
|
||||
SymbolLookup symbol_resolver(callbacks);
|
||||
runtime_dyld = std::unique_ptr<llvm::RuntimeDyld>(
|
||||
new llvm::RuntimeDyld(*memory_manager, symbol_resolver));
|
||||
|
||||
runtime_dyld->setProcessAllSections(true);
|
||||
runtime_dyld->setProcessAllSections(true);
|
||||
|
||||
runtime_dyld->loadObject(*object_file);
|
||||
runtime_dyld->finalizeWithMemoryManagerLocking();
|
||||
runtime_dyld->loadObject(*object_file);
|
||||
runtime_dyld->finalizeWithMemoryManagerLocking();
|
||||
|
||||
if (runtime_dyld->hasError()) {
|
||||
_init_failed = true;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
_init_failed = true;
|
||||
if (runtime_dyld->hasError()) {
|
||||
_init_failed = true;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
_init_failed = true;
|
||||
}
|
||||
}
|
||||
|
||||
void* WasmModule::get_func(llvm::StringRef name) const {
|
||||
|
@ -1,150 +1,86 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <llvm/ExecutionEngine/RuntimeDyld.h>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <exception>
|
||||
|
||||
typedef enum
|
||||
{
|
||||
PROTECT_NONE,
|
||||
PROTECT_READ,
|
||||
PROTECT_READ_WRITE,
|
||||
PROTECT_READ_EXECUTE,
|
||||
#include <llvm/ExecutionEngine/RuntimeDyld.h>
|
||||
|
||||
typedef enum {
|
||||
PROTECT_NONE,
|
||||
PROTECT_READ,
|
||||
PROTECT_READ_WRITE,
|
||||
PROTECT_READ_EXECUTE,
|
||||
} mem_protect_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
RESULT_OK,
|
||||
RESULT_ALLOCATE_FAILURE,
|
||||
RESULT_PROTECT_FAILURE,
|
||||
RESULT_DEALLOC_FAILURE,
|
||||
RESULT_OBJECT_LOAD_FAILURE,
|
||||
typedef enum {
|
||||
RESULT_OK,
|
||||
RESULT_ALLOCATE_FAILURE,
|
||||
RESULT_PROTECT_FAILURE,
|
||||
RESULT_DEALLOC_FAILURE,
|
||||
RESULT_OBJECT_LOAD_FAILURE,
|
||||
} result_t;
|
||||
|
||||
typedef result_t (*alloc_memory_t)(size_t size, mem_protect_t protect, uint8_t **ptr_out, size_t *size_out);
|
||||
typedef result_t (*protect_memory_t)(uint8_t *ptr, size_t size, mem_protect_t protect);
|
||||
typedef result_t (*alloc_memory_t)(size_t size, mem_protect_t protect,
|
||||
uint8_t **ptr_out, size_t *size_out);
|
||||
typedef result_t (*protect_memory_t)(uint8_t *ptr, size_t size,
|
||||
mem_protect_t protect);
|
||||
typedef result_t (*dealloc_memory_t)(uint8_t *ptr, size_t size);
|
||||
typedef uintptr_t (*lookup_vm_symbol_t)(const char *name_ptr, size_t length);
|
||||
typedef void (*fde_visitor_t)(uint8_t *fde);
|
||||
typedef result_t (*visit_fde_t)(uint8_t *fde, size_t size, fde_visitor_t visitor);
|
||||
typedef result_t (*visit_fde_t)(uint8_t *fde, size_t size,
|
||||
fde_visitor_t visitor);
|
||||
|
||||
typedef void (*trampoline_t)(void *, void *, void *, void *);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
/* Memory management. */
|
||||
alloc_memory_t alloc_memory;
|
||||
protect_memory_t protect_memory;
|
||||
dealloc_memory_t dealloc_memory;
|
||||
typedef struct {
|
||||
/* Memory management. */
|
||||
alloc_memory_t alloc_memory;
|
||||
protect_memory_t protect_memory;
|
||||
dealloc_memory_t dealloc_memory;
|
||||
|
||||
lookup_vm_symbol_t lookup_vm_symbol;
|
||||
lookup_vm_symbol_t lookup_vm_symbol;
|
||||
|
||||
visit_fde_t visit_fde;
|
||||
visit_fde_t visit_fde;
|
||||
} callbacks_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
size_t data, vtable;
|
||||
typedef struct {
|
||||
size_t data, vtable;
|
||||
} box_any_t;
|
||||
|
||||
struct WasmException
|
||||
{
|
||||
public:
|
||||
virtual std::string description() const noexcept = 0;
|
||||
struct WasmException {
|
||||
public:
|
||||
virtual std::string description() const noexcept = 0;
|
||||
};
|
||||
|
||||
struct UncatchableException : WasmException
|
||||
{
|
||||
public:
|
||||
virtual std::string description() const noexcept override
|
||||
{
|
||||
return "Uncatchable exception";
|
||||
}
|
||||
struct UncatchableException : WasmException {
|
||||
public:
|
||||
virtual std::string description() const noexcept override {
|
||||
return "Uncatchable exception";
|
||||
}
|
||||
};
|
||||
|
||||
struct UserException : UncatchableException
|
||||
{
|
||||
public:
|
||||
UserException(size_t data, size_t vtable) : error_data({ data, vtable }) {}
|
||||
struct UserException : UncatchableException {
|
||||
public:
|
||||
UserException(size_t data, size_t vtable) : error_data({data, vtable}) {}
|
||||
|
||||
virtual std::string description() const noexcept override
|
||||
{
|
||||
return "user exception";
|
||||
}
|
||||
virtual std::string description() const noexcept override {
|
||||
return "user exception";
|
||||
}
|
||||
|
||||
// The parts of a `Box<dyn Any>`.
|
||||
box_any_t error_data;
|
||||
// The parts of a `Box<dyn Any>`.
|
||||
box_any_t error_data;
|
||||
};
|
||||
|
||||
struct WasmTrap : UncatchableException
|
||||
{
|
||||
public:
|
||||
enum Type
|
||||
{
|
||||
Unreachable = 0,
|
||||
IncorrectCallIndirectSignature = 1,
|
||||
MemoryOutOfBounds = 2,
|
||||
CallIndirectOOB = 3,
|
||||
IllegalArithmetic = 4,
|
||||
Unknown,
|
||||
};
|
||||
struct BreakpointException : UncatchableException {
|
||||
public:
|
||||
BreakpointException(uintptr_t callback) : callback(callback) {}
|
||||
|
||||
WasmTrap(Type type) : type(type) {}
|
||||
virtual std::string description() const noexcept override {
|
||||
return "breakpoint exception";
|
||||
}
|
||||
|
||||
virtual std::string description() const noexcept override
|
||||
{
|
||||
std::ostringstream ss;
|
||||
ss
|
||||
<< "WebAssembly trap:" << '\n'
|
||||
<< " - type: " << type << '\n';
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
Type type;
|
||||
|
||||
private:
|
||||
friend std::ostream &operator<<(std::ostream &out, const Type &ty)
|
||||
{
|
||||
switch (ty)
|
||||
{
|
||||
case Type::Unreachable:
|
||||
out << "unreachable";
|
||||
break;
|
||||
case Type::IncorrectCallIndirectSignature:
|
||||
out << "incorrect call_indirect signature";
|
||||
break;
|
||||
case Type::MemoryOutOfBounds:
|
||||
out << "memory access out-of-bounds";
|
||||
break;
|
||||
case Type::CallIndirectOOB:
|
||||
out << "call_indirect out-of-bounds";
|
||||
break;
|
||||
case Type::IllegalArithmetic:
|
||||
out << "illegal arithmetic operation";
|
||||
break;
|
||||
case Type::Unknown:
|
||||
default:
|
||||
out << "unknown";
|
||||
break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
struct CatchableException : WasmException
|
||||
{
|
||||
public:
|
||||
CatchableException(uint32_t type_id, uint32_t value_num) : type_id(type_id), value_num(value_num) {}
|
||||
|
||||
virtual std::string description() const noexcept override
|
||||
{
|
||||
return "catchable exception";
|
||||
}
|
||||
|
||||
uint32_t type_id, value_num;
|
||||
uint64_t values[1];
|
||||
uintptr_t callback;
|
||||
};
|
||||
|
||||
struct WasmModule
|
||||
@ -168,77 +104,127 @@ struct WasmModule
|
||||
std::unique_ptr<llvm::RuntimeDyld> runtime_dyld;
|
||||
};
|
||||
|
||||
extern "C"
|
||||
{
|
||||
result_t module_load(const uint8_t *mem_ptr, size_t mem_size, callbacks_t callbacks, WasmModule **module_out)
|
||||
{
|
||||
*module_out = new WasmModule(mem_ptr, mem_size, callbacks);
|
||||
struct WasmTrap : UncatchableException {
|
||||
public:
|
||||
enum Type {
|
||||
Unreachable = 0,
|
||||
IncorrectCallIndirectSignature = 1,
|
||||
MemoryOutOfBounds = 2,
|
||||
CallIndirectOOB = 3,
|
||||
IllegalArithmetic = 4,
|
||||
Unknown,
|
||||
};
|
||||
|
||||
if ((*module_out)->_init_failed) {
|
||||
return RESULT_OBJECT_LOAD_FAILURE;
|
||||
}
|
||||
WasmTrap(Type type) : type(type) {}
|
||||
|
||||
return RESULT_OK;
|
||||
virtual std::string description() const noexcept override {
|
||||
std::ostringstream ss;
|
||||
ss << "WebAssembly trap:" << '\n' << " - type: " << type << '\n';
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
Type type;
|
||||
|
||||
private:
|
||||
friend std::ostream &operator<<(std::ostream &out, const Type &ty) {
|
||||
switch (ty) {
|
||||
case Type::Unreachable:
|
||||
out << "unreachable";
|
||||
break;
|
||||
case Type::IncorrectCallIndirectSignature:
|
||||
out << "incorrect call_indirect signature";
|
||||
break;
|
||||
case Type::MemoryOutOfBounds:
|
||||
out << "memory access out-of-bounds";
|
||||
break;
|
||||
case Type::CallIndirectOOB:
|
||||
out << "call_indirect out-of-bounds";
|
||||
break;
|
||||
case Type::IllegalArithmetic:
|
||||
out << "illegal arithmetic operation";
|
||||
break;
|
||||
case Type::Unknown:
|
||||
default:
|
||||
out << "unknown";
|
||||
break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
[[noreturn]] void throw_trap(WasmTrap::Type ty) {
|
||||
throw WasmTrap(ty);
|
||||
}
|
||||
struct CatchableException : WasmException {
|
||||
public:
|
||||
CatchableException(uint32_t type_id, uint32_t value_num)
|
||||
: type_id(type_id), value_num(value_num) {}
|
||||
|
||||
void module_delete(WasmModule *module)
|
||||
{
|
||||
delete module;
|
||||
}
|
||||
virtual std::string description() const noexcept override {
|
||||
return "catchable exception";
|
||||
}
|
||||
|
||||
// Throw a fat pointer that's assumed to be `*mut dyn Any` on the rust
|
||||
// side.
|
||||
[[noreturn]] void throw_any(size_t data, size_t vtable) {
|
||||
throw UserException(data, vtable);
|
||||
}
|
||||
uint32_t type_id, value_num;
|
||||
uint64_t values[1];
|
||||
};
|
||||
|
||||
bool invoke_trampoline(
|
||||
trampoline_t trampoline,
|
||||
void *ctx,
|
||||
void *func,
|
||||
void *params,
|
||||
void *results,
|
||||
WasmTrap::Type *trap_out,
|
||||
box_any_t *user_error,
|
||||
void *invoke_env) throw()
|
||||
{
|
||||
try
|
||||
{
|
||||
trampoline(ctx, func, params, results);
|
||||
return true;
|
||||
}
|
||||
catch (const WasmTrap &e)
|
||||
{
|
||||
*trap_out = e.type;
|
||||
return false;
|
||||
}
|
||||
catch (const UserException &e)
|
||||
{
|
||||
*user_error = e.error_data;
|
||||
return false;
|
||||
}
|
||||
catch (const WasmException &e)
|
||||
{
|
||||
*trap_out = WasmTrap::Type::Unknown;
|
||||
return false;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
*trap_out = WasmTrap::Type::Unknown;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
extern "C" {
|
||||
void callback_trampoline(void *, void *);
|
||||
|
||||
void *get_func_symbol(WasmModule *module, const char *name)
|
||||
{
|
||||
return module->get_func(llvm::StringRef(name));
|
||||
}
|
||||
result_t module_load(const uint8_t *mem_ptr, size_t mem_size,
|
||||
callbacks_t callbacks, WasmModule **module_out) {
|
||||
*module_out = new WasmModule(mem_ptr, mem_size, callbacks);
|
||||
|
||||
const uint8_t *llvm_backend_get_stack_map_ptr(const WasmModule *module) {
|
||||
if ((*module_out)->_init_failed) {
|
||||
return RESULT_OBJECT_LOAD_FAILURE;
|
||||
}
|
||||
|
||||
return RESULT_OK;
|
||||
}
|
||||
|
||||
[[noreturn]] void throw_trap(WasmTrap::Type ty) { throw WasmTrap(ty); }
|
||||
|
||||
void module_delete(WasmModule *module) { delete module; }
|
||||
|
||||
// Throw a fat pointer that's assumed to be `*mut dyn Any` on the rust
|
||||
// side.
|
||||
[[noreturn]] void throw_any(size_t data, size_t vtable) {
|
||||
throw UserException(data, vtable);
|
||||
}
|
||||
|
||||
// Throw a pointer that's assumed to be codegen::BreakpointHandler on the
|
||||
// rust side.
|
||||
[[noreturn]] void throw_breakpoint(uintptr_t callback) {
|
||||
throw BreakpointException(callback);
|
||||
}
|
||||
|
||||
bool invoke_trampoline(trampoline_t trampoline, void *ctx, void *func,
|
||||
void *params, void *results, WasmTrap::Type *trap_out,
|
||||
box_any_t *user_error, void *invoke_env) noexcept {
|
||||
try {
|
||||
trampoline(ctx, func, params, results);
|
||||
return true;
|
||||
} catch (const WasmTrap &e) {
|
||||
*trap_out = e.type;
|
||||
return false;
|
||||
} catch (const UserException &e) {
|
||||
*user_error = e.error_data;
|
||||
return false;
|
||||
} catch (const BreakpointException &e) {
|
||||
callback_trampoline(user_error, (void *)e.callback);
|
||||
return false;
|
||||
} catch (const WasmException &e) {
|
||||
*trap_out = WasmTrap::Type::Unknown;
|
||||
return false;
|
||||
} catch (...) {
|
||||
*trap_out = WasmTrap::Type::Unknown;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void *get_func_symbol(WasmModule *module, const char *name) {
|
||||
return module->get_func(llvm::StringRef(name));
|
||||
}
|
||||
|
||||
const uint8_t *llvm_backend_get_stack_map_ptr(const WasmModule *module) {
|
||||
return module->get_stack_map_ptr();
|
||||
}
|
||||
|
||||
|
@ -1,19 +1,19 @@
|
||||
use super::stackmap::{self, StackmapRegistry, StkMapRecord, StkSizeRecord};
|
||||
use crate::intrinsics::Intrinsics;
|
||||
use crate::structs::{Callbacks, LLVMModule, LLVMResult, MemProtect};
|
||||
use inkwell::{
|
||||
memory_buffer::MemoryBuffer,
|
||||
module::Module,
|
||||
targets::{CodeModel, FileType, InitializationConfig, RelocMode, Target, TargetMachine},
|
||||
OptimizationLevel,
|
||||
};
|
||||
use libc::{
|
||||
c_char, mmap, mprotect, munmap, MAP_ANON, MAP_PRIVATE, PROT_EXEC, PROT_NONE, PROT_READ,
|
||||
PROT_WRITE,
|
||||
};
|
||||
use libc::c_char;
|
||||
use std::{
|
||||
any::Any,
|
||||
collections::BTreeMap,
|
||||
ffi::{c_void, CString},
|
||||
fs::File,
|
||||
io::Write,
|
||||
mem,
|
||||
ops::Deref,
|
||||
ptr::{self, NonNull},
|
||||
@ -34,42 +34,6 @@ use wasmer_runtime_core::{
|
||||
vm, vmcalls,
|
||||
};
|
||||
|
||||
#[repr(C)]
|
||||
struct LLVMModule {
|
||||
_private: [u8; 0],
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types, dead_code)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[repr(C)]
|
||||
enum MemProtect {
|
||||
NONE,
|
||||
READ,
|
||||
READ_WRITE,
|
||||
READ_EXECUTE,
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types, dead_code)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[repr(C)]
|
||||
enum LLVMResult {
|
||||
OK,
|
||||
ALLOCATE_FAILURE,
|
||||
PROTECT_FAILURE,
|
||||
DEALLOC_FAILURE,
|
||||
OBJECT_LOAD_FAILURE,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct Callbacks {
|
||||
alloc_memory: extern "C" fn(usize, MemProtect, &mut *mut u8, &mut usize) -> LLVMResult,
|
||||
protect_memory: extern "C" fn(*mut u8, usize, MemProtect) -> LLVMResult,
|
||||
dealloc_memory: extern "C" fn(*mut u8, usize) -> LLVMResult,
|
||||
|
||||
lookup_vm_symbol: extern "C" fn(*const c_char, usize) -> *const vm::Func,
|
||||
visit_fde: extern "C" fn(*mut u8, usize, extern "C" fn(*mut u8)),
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn module_load(
|
||||
mem_ptr: *const u8,
|
||||
@ -84,7 +48,8 @@ extern "C" {
|
||||
fn llvm_backend_get_code_ptr(module: *const LLVMModule) -> *const u8;
|
||||
fn llvm_backend_get_code_size(module: *const LLVMModule) -> usize;
|
||||
|
||||
fn throw_trap(ty: i32);
|
||||
fn throw_trap(ty: i32) -> !;
|
||||
fn throw_breakpoint(ty: i64) -> !;
|
||||
|
||||
/// This should be the same as spliting up the fat pointer into two arguments,
|
||||
/// but this is cleaner, I think?
|
||||
@ -106,69 +71,21 @@ extern "C" {
|
||||
}
|
||||
|
||||
fn get_callbacks() -> Callbacks {
|
||||
fn round_up_to_page_size(size: usize) -> usize {
|
||||
(size + (4096 - 1)) & !(4096 - 1)
|
||||
}
|
||||
|
||||
extern "C" fn alloc_memory(
|
||||
size: usize,
|
||||
protect: MemProtect,
|
||||
ptr_out: &mut *mut u8,
|
||||
size_out: &mut usize,
|
||||
) -> LLVMResult {
|
||||
let size = round_up_to_page_size(size);
|
||||
let ptr = unsafe {
|
||||
mmap(
|
||||
ptr::null_mut(),
|
||||
size,
|
||||
match protect {
|
||||
MemProtect::NONE => PROT_NONE,
|
||||
MemProtect::READ => PROT_READ,
|
||||
MemProtect::READ_WRITE => PROT_READ | PROT_WRITE,
|
||||
MemProtect::READ_EXECUTE => PROT_READ | PROT_EXEC,
|
||||
},
|
||||
MAP_PRIVATE | MAP_ANON,
|
||||
-1,
|
||||
0,
|
||||
)
|
||||
};
|
||||
if ptr as isize == -1 {
|
||||
return LLVMResult::ALLOCATE_FAILURE;
|
||||
}
|
||||
*ptr_out = ptr as _;
|
||||
*size_out = size;
|
||||
LLVMResult::OK
|
||||
unsafe { crate::platform::alloc_memory(size, protect, ptr_out, size_out) }
|
||||
}
|
||||
|
||||
extern "C" fn protect_memory(ptr: *mut u8, size: usize, protect: MemProtect) -> LLVMResult {
|
||||
let res = unsafe {
|
||||
mprotect(
|
||||
ptr as _,
|
||||
round_up_to_page_size(size),
|
||||
match protect {
|
||||
MemProtect::NONE => PROT_NONE,
|
||||
MemProtect::READ => PROT_READ,
|
||||
MemProtect::READ_WRITE => PROT_READ | PROT_WRITE,
|
||||
MemProtect::READ_EXECUTE => PROT_READ | PROT_EXEC,
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
if res == 0 {
|
||||
LLVMResult::OK
|
||||
} else {
|
||||
LLVMResult::PROTECT_FAILURE
|
||||
}
|
||||
unsafe { crate::platform::protect_memory(ptr, size, protect) }
|
||||
}
|
||||
|
||||
extern "C" fn dealloc_memory(ptr: *mut u8, size: usize) -> LLVMResult {
|
||||
let res = unsafe { munmap(ptr as _, round_up_to_page_size(size)) };
|
||||
|
||||
if res == 0 {
|
||||
LLVMResult::OK
|
||||
} else {
|
||||
LLVMResult::DEALLOC_FAILURE
|
||||
}
|
||||
unsafe { crate::platform::dealloc_memory(ptr, size) }
|
||||
}
|
||||
|
||||
extern "C" fn lookup_vm_symbol(name_ptr: *const c_char, length: usize) -> *const vm::Func {
|
||||
@ -195,7 +112,13 @@ fn get_callbacks() -> Callbacks {
|
||||
fn_name!("vm.memory.grow.static.local") => vmcalls::local_static_memory_grow as _,
|
||||
fn_name!("vm.memory.size.static.local") => vmcalls::local_static_memory_size as _,
|
||||
|
||||
fn_name!("vm.memory.grow.dynamic.import") => vmcalls::imported_dynamic_memory_grow as _,
|
||||
fn_name!("vm.memory.size.dynamic.import") => vmcalls::imported_dynamic_memory_size as _,
|
||||
fn_name!("vm.memory.grow.static.import") => vmcalls::imported_static_memory_grow as _,
|
||||
fn_name!("vm.memory.size.static.import") => vmcalls::imported_static_memory_size as _,
|
||||
|
||||
fn_name!("vm.exception.trap") => throw_trap as _,
|
||||
fn_name!("vm.breakpoint") => throw_breakpoint as _,
|
||||
|
||||
_ => ptr::null(),
|
||||
}
|
||||
@ -275,6 +198,14 @@ impl LLVMBackend {
|
||||
.unwrap();
|
||||
let mem_buf_slice = memory_buffer.as_slice();
|
||||
|
||||
if let Some(path) = unsafe { &crate::GLOBAL_OPTIONS.obj_file } {
|
||||
let mut file = File::create(path).unwrap();
|
||||
let mut pos = 0;
|
||||
while pos < mem_buf_slice.len() {
|
||||
pos += file.write(&mem_buf_slice[pos..]).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
let callbacks = get_callbacks();
|
||||
let mut module: *mut LLVMModule = ptr::null_mut();
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,12 +1,15 @@
|
||||
use hashbrown::HashMap;
|
||||
use inkwell::{
|
||||
attributes::{Attribute, AttributeLoc},
|
||||
builder::Builder,
|
||||
context::Context,
|
||||
module::Module,
|
||||
types::{BasicType, FloatType, FunctionType, IntType, PointerType, StructType, VoidType},
|
||||
types::{
|
||||
BasicType, FloatType, FunctionType, IntType, PointerType, StructType, VectorType, VoidType,
|
||||
},
|
||||
values::{BasicValue, BasicValueEnum, FloatValue, FunctionValue, IntValue, PointerValue},
|
||||
AddressSpace,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::marker::PhantomData;
|
||||
use wasmer_runtime_core::{
|
||||
memory::MemoryType,
|
||||
@ -16,7 +19,7 @@ use wasmer_runtime_core::{
|
||||
GlobalIndex, ImportedFuncIndex, LocalFuncIndex, LocalOrImport, MemoryIndex, SigIndex,
|
||||
TableIndex, Type,
|
||||
},
|
||||
vm::Ctx,
|
||||
vm::{Ctx, INTERNALS_SIZE},
|
||||
};
|
||||
|
||||
fn type_to_llvm_ptr(intrinsics: &Intrinsics, ty: Type) -> PointerType {
|
||||
@ -25,6 +28,7 @@ fn type_to_llvm_ptr(intrinsics: &Intrinsics, ty: Type) -> PointerType {
|
||||
Type::I64 => intrinsics.i64_ptr_ty,
|
||||
Type::F32 => intrinsics.f32_ptr_ty,
|
||||
Type::F64 => intrinsics.f64_ptr_ty,
|
||||
Type::V128 => intrinsics.i128_ptr_ty,
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,12 +44,18 @@ pub struct Intrinsics {
|
||||
|
||||
pub sqrt_f32: FunctionValue,
|
||||
pub sqrt_f64: FunctionValue,
|
||||
pub sqrt_f32x4: FunctionValue,
|
||||
pub sqrt_f64x2: FunctionValue,
|
||||
|
||||
pub minimum_f32: FunctionValue,
|
||||
pub minimum_f64: FunctionValue,
|
||||
pub minimum_f32x4: FunctionValue,
|
||||
pub minimum_f64x2: FunctionValue,
|
||||
|
||||
pub maximum_f32: FunctionValue,
|
||||
pub maximum_f64: FunctionValue,
|
||||
pub maximum_f32x4: FunctionValue,
|
||||
pub maximum_f64x2: FunctionValue,
|
||||
|
||||
pub ceil_f32: FunctionValue,
|
||||
pub ceil_f64: FunctionValue,
|
||||
@ -61,10 +71,22 @@ pub struct Intrinsics {
|
||||
|
||||
pub fabs_f32: FunctionValue,
|
||||
pub fabs_f64: FunctionValue,
|
||||
pub fabs_f32x4: FunctionValue,
|
||||
pub fabs_f64x2: FunctionValue,
|
||||
|
||||
pub copysign_f32: FunctionValue,
|
||||
pub copysign_f64: FunctionValue,
|
||||
|
||||
pub sadd_sat_i8x16: FunctionValue,
|
||||
pub sadd_sat_i16x8: FunctionValue,
|
||||
pub uadd_sat_i8x16: FunctionValue,
|
||||
pub uadd_sat_i16x8: FunctionValue,
|
||||
|
||||
pub ssub_sat_i8x16: FunctionValue,
|
||||
pub ssub_sat_i16x8: FunctionValue,
|
||||
pub usub_sat_i8x16: FunctionValue,
|
||||
pub usub_sat_i16x8: FunctionValue,
|
||||
|
||||
pub expect_i1: FunctionValue,
|
||||
pub trap: FunctionValue,
|
||||
|
||||
@ -74,21 +96,33 @@ pub struct Intrinsics {
|
||||
pub i16_ty: IntType,
|
||||
pub i32_ty: IntType,
|
||||
pub i64_ty: IntType,
|
||||
pub i128_ty: IntType,
|
||||
pub f32_ty: FloatType,
|
||||
pub f64_ty: FloatType,
|
||||
|
||||
pub i1x128_ty: VectorType,
|
||||
pub i8x16_ty: VectorType,
|
||||
pub i16x8_ty: VectorType,
|
||||
pub i32x4_ty: VectorType,
|
||||
pub i64x2_ty: VectorType,
|
||||
pub f32x4_ty: VectorType,
|
||||
pub f64x2_ty: VectorType,
|
||||
|
||||
pub i8_ptr_ty: PointerType,
|
||||
pub i16_ptr_ty: PointerType,
|
||||
pub i32_ptr_ty: PointerType,
|
||||
pub i64_ptr_ty: PointerType,
|
||||
pub i128_ptr_ty: PointerType,
|
||||
pub f32_ptr_ty: PointerType,
|
||||
pub f64_ptr_ty: PointerType,
|
||||
|
||||
pub anyfunc_ty: StructType,
|
||||
|
||||
pub i1_zero: IntValue,
|
||||
pub i8_zero: IntValue,
|
||||
pub i32_zero: IntValue,
|
||||
pub i64_zero: IntValue,
|
||||
pub i128_zero: IntValue,
|
||||
pub f32_zero: FloatValue,
|
||||
pub f64_zero: FloatValue,
|
||||
|
||||
@ -114,6 +148,7 @@ pub struct Intrinsics {
|
||||
pub memory_size_shared_import: FunctionValue,
|
||||
|
||||
pub throw_trap: FunctionValue,
|
||||
pub throw_breakpoint: FunctionValue,
|
||||
|
||||
pub experimental_stackmap: FunctionValue,
|
||||
|
||||
@ -128,19 +163,31 @@ impl Intrinsics {
|
||||
let i16_ty = context.i16_type();
|
||||
let i32_ty = context.i32_type();
|
||||
let i64_ty = context.i64_type();
|
||||
let i128_ty = context.i128_type();
|
||||
let f32_ty = context.f32_type();
|
||||
let f64_ty = context.f64_type();
|
||||
|
||||
let i1x128_ty = i1_ty.vec_type(128);
|
||||
let i8x16_ty = i8_ty.vec_type(16);
|
||||
let i16x8_ty = i16_ty.vec_type(8);
|
||||
let i32x4_ty = i32_ty.vec_type(4);
|
||||
let i64x2_ty = i64_ty.vec_type(2);
|
||||
let f32x4_ty = f32_ty.vec_type(4);
|
||||
let f64x2_ty = f64_ty.vec_type(2);
|
||||
|
||||
let i8_ptr_ty = i8_ty.ptr_type(AddressSpace::Generic);
|
||||
let i16_ptr_ty = i16_ty.ptr_type(AddressSpace::Generic);
|
||||
let i32_ptr_ty = i32_ty.ptr_type(AddressSpace::Generic);
|
||||
let i64_ptr_ty = i64_ty.ptr_type(AddressSpace::Generic);
|
||||
let i128_ptr_ty = i128_ty.ptr_type(AddressSpace::Generic);
|
||||
let f32_ptr_ty = f32_ty.ptr_type(AddressSpace::Generic);
|
||||
let f64_ptr_ty = f64_ty.ptr_type(AddressSpace::Generic);
|
||||
|
||||
let i1_zero = i1_ty.const_int(0, false);
|
||||
let i8_zero = i8_ty.const_int(0, false);
|
||||
let i32_zero = i32_ty.const_int(0, false);
|
||||
let i64_zero = i64_ty.const_int(0, false);
|
||||
let i128_zero = i128_ty.const_int(0, false);
|
||||
let f32_zero = f32_ty.const_float(0.0);
|
||||
let f64_zero = f64_ty.const_float(0.0);
|
||||
|
||||
@ -149,6 +196,10 @@ impl Intrinsics {
|
||||
let i64_ty_basic = i64_ty.as_basic_type_enum();
|
||||
let f32_ty_basic = f32_ty.as_basic_type_enum();
|
||||
let f64_ty_basic = f64_ty.as_basic_type_enum();
|
||||
let i8x16_ty_basic = i8x16_ty.as_basic_type_enum();
|
||||
let i16x8_ty_basic = i16x8_ty.as_basic_type_enum();
|
||||
let f32x4_ty_basic = f32x4_ty.as_basic_type_enum();
|
||||
let f64x2_ty_basic = f64x2_ty.as_basic_type_enum();
|
||||
let i8_ptr_ty_basic = i8_ptr_ty.as_basic_type_enum();
|
||||
|
||||
let ctx_ty = context.opaque_struct_type("ctx");
|
||||
@ -235,6 +286,9 @@ impl Intrinsics {
|
||||
false,
|
||||
);
|
||||
|
||||
let ret_i8x16_take_i8x16_i8x16 = i8x16_ty.fn_type(&[i8x16_ty_basic, i8x16_ty_basic], false);
|
||||
let ret_i16x8_take_i16x8_i16x8 = i16x8_ty.fn_type(&[i16x8_ty_basic, i16x8_ty_basic], false);
|
||||
|
||||
let ret_i32_take_i32_i1 = i32_ty.fn_type(&[i32_ty_basic, i1_ty_basic], false);
|
||||
let ret_i64_take_i64_i1 = i64_ty.fn_type(&[i64_ty_basic, i1_ty_basic], false);
|
||||
|
||||
@ -243,9 +297,13 @@ impl Intrinsics {
|
||||
|
||||
let ret_f32_take_f32 = f32_ty.fn_type(&[f32_ty_basic], false);
|
||||
let ret_f64_take_f64 = f64_ty.fn_type(&[f64_ty_basic], false);
|
||||
let ret_f32x4_take_f32x4 = f32x4_ty.fn_type(&[f32x4_ty_basic], false);
|
||||
let ret_f64x2_take_f64x2 = f64x2_ty.fn_type(&[f64x2_ty_basic], false);
|
||||
|
||||
let ret_f32_take_f32_f32 = f32_ty.fn_type(&[f32_ty_basic, f32_ty_basic], false);
|
||||
let ret_f64_take_f64_f64 = f64_ty.fn_type(&[f64_ty_basic, f64_ty_basic], false);
|
||||
let ret_f32x4_take_f32x4_f32x4 = f32x4_ty.fn_type(&[f32x4_ty_basic, f32x4_ty_basic], false);
|
||||
let ret_f64x2_take_f64x2_f64x2 = f64x2_ty.fn_type(&[f64x2_ty_basic, f64x2_ty_basic], false);
|
||||
|
||||
let ret_i32_take_ctx_i32_i32 = i32_ty.fn_type(
|
||||
&[ctx_ptr_ty.as_basic_type_enum(), i32_ty_basic, i32_ty_basic],
|
||||
@ -255,8 +313,7 @@ impl Intrinsics {
|
||||
i32_ty.fn_type(&[ctx_ptr_ty.as_basic_type_enum(), i32_ty_basic], false);
|
||||
|
||||
let ret_i1_take_i1_i1 = i1_ty.fn_type(&[i1_ty_basic, i1_ty_basic], false);
|
||||
|
||||
Self {
|
||||
let intrinsics = Self {
|
||||
ctlz_i32: module.add_function("llvm.ctlz.i32", ret_i32_take_i32_i1, None),
|
||||
ctlz_i64: module.add_function("llvm.ctlz.i64", ret_i64_take_i64_i1, None),
|
||||
|
||||
@ -268,12 +325,34 @@ impl Intrinsics {
|
||||
|
||||
sqrt_f32: module.add_function("llvm.sqrt.f32", ret_f32_take_f32, None),
|
||||
sqrt_f64: module.add_function("llvm.sqrt.f64", ret_f64_take_f64, None),
|
||||
sqrt_f32x4: module.add_function("llvm.sqrt.v4f32", ret_f32x4_take_f32x4, None),
|
||||
sqrt_f64x2: module.add_function("llvm.sqrt.v2f64", ret_f64x2_take_f64x2, None),
|
||||
|
||||
minimum_f32: module.add_function("llvm.minnum.f32", ret_f32_take_f32_f32, None),
|
||||
minimum_f64: module.add_function("llvm.minnum.f64", ret_f64_take_f64_f64, None),
|
||||
minimum_f32x4: module.add_function(
|
||||
"llvm.minnum.v4f32",
|
||||
ret_f32x4_take_f32x4_f32x4,
|
||||
None,
|
||||
),
|
||||
minimum_f64x2: module.add_function(
|
||||
"llvm.minnum.v2f64",
|
||||
ret_f64x2_take_f64x2_f64x2,
|
||||
None,
|
||||
),
|
||||
|
||||
maximum_f32: module.add_function("llvm.maxnum.f32", ret_f32_take_f32_f32, None),
|
||||
maximum_f64: module.add_function("llvm.maxnum.f64", ret_f64_take_f64_f64, None),
|
||||
maximum_f32x4: module.add_function(
|
||||
"llvm.maxnum.v4f32",
|
||||
ret_f32x4_take_f32x4_f32x4,
|
||||
None,
|
||||
),
|
||||
maximum_f64x2: module.add_function(
|
||||
"llvm.maxnum.v2f64",
|
||||
ret_f64x2_take_f64x2_f64x2,
|
||||
None,
|
||||
),
|
||||
|
||||
ceil_f32: module.add_function("llvm.ceil.f32", ret_f32_take_f32, None),
|
||||
ceil_f64: module.add_function("llvm.ceil.f64", ret_f64_take_f64, None),
|
||||
@ -289,10 +368,54 @@ impl Intrinsics {
|
||||
|
||||
fabs_f32: module.add_function("llvm.fabs.f32", ret_f32_take_f32, None),
|
||||
fabs_f64: module.add_function("llvm.fabs.f64", ret_f64_take_f64, None),
|
||||
fabs_f32x4: module.add_function("llvm.fabs.v4f32", ret_f32x4_take_f32x4, None),
|
||||
fabs_f64x2: module.add_function("llvm.fabs.v2f64", ret_f64x2_take_f64x2, None),
|
||||
|
||||
copysign_f32: module.add_function("llvm.copysign.f32", ret_f32_take_f32_f32, None),
|
||||
copysign_f64: module.add_function("llvm.copysign.f64", ret_f64_take_f64_f64, None),
|
||||
|
||||
sadd_sat_i8x16: module.add_function(
|
||||
"llvm.sadd.sat.v16i8",
|
||||
ret_i8x16_take_i8x16_i8x16,
|
||||
None,
|
||||
),
|
||||
sadd_sat_i16x8: module.add_function(
|
||||
"llvm.sadd.sat.v8i16",
|
||||
ret_i16x8_take_i16x8_i16x8,
|
||||
None,
|
||||
),
|
||||
uadd_sat_i8x16: module.add_function(
|
||||
"llvm.uadd.sat.v16i8",
|
||||
ret_i8x16_take_i8x16_i8x16,
|
||||
None,
|
||||
),
|
||||
uadd_sat_i16x8: module.add_function(
|
||||
"llvm.uadd.sat.v8i16",
|
||||
ret_i16x8_take_i16x8_i16x8,
|
||||
None,
|
||||
),
|
||||
|
||||
ssub_sat_i8x16: module.add_function(
|
||||
"llvm.ssub.sat.v16i8",
|
||||
ret_i8x16_take_i8x16_i8x16,
|
||||
None,
|
||||
),
|
||||
ssub_sat_i16x8: module.add_function(
|
||||
"llvm.ssub.sat.v8i16",
|
||||
ret_i16x8_take_i16x8_i16x8,
|
||||
None,
|
||||
),
|
||||
usub_sat_i8x16: module.add_function(
|
||||
"llvm.usub.sat.v16i8",
|
||||
ret_i8x16_take_i8x16_i8x16,
|
||||
None,
|
||||
),
|
||||
usub_sat_i16x8: module.add_function(
|
||||
"llvm.usub.sat.v8i16",
|
||||
ret_i16x8_take_i16x8_i16x8,
|
||||
None,
|
||||
),
|
||||
|
||||
expect_i1: module.add_function("llvm.expect.i1", ret_i1_take_i1_i1, None),
|
||||
trap: module.add_function("llvm.trap", void_ty.fn_type(&[], false), None),
|
||||
|
||||
@ -302,21 +425,33 @@ impl Intrinsics {
|
||||
i16_ty,
|
||||
i32_ty,
|
||||
i64_ty,
|
||||
i128_ty,
|
||||
f32_ty,
|
||||
f64_ty,
|
||||
|
||||
i1x128_ty,
|
||||
i8x16_ty,
|
||||
i16x8_ty,
|
||||
i32x4_ty,
|
||||
i64x2_ty,
|
||||
f32x4_ty,
|
||||
f64x2_ty,
|
||||
|
||||
i8_ptr_ty,
|
||||
i16_ptr_ty,
|
||||
i32_ptr_ty,
|
||||
i64_ptr_ty,
|
||||
i128_ptr_ty,
|
||||
f32_ptr_ty,
|
||||
f64_ptr_ty,
|
||||
|
||||
anyfunc_ty,
|
||||
|
||||
i1_zero,
|
||||
i8_zero,
|
||||
i32_zero,
|
||||
i64_zero,
|
||||
i128_zero,
|
||||
f32_zero,
|
||||
f64_zero,
|
||||
|
||||
@ -404,8 +539,45 @@ impl Intrinsics {
|
||||
),
|
||||
None,
|
||||
),
|
||||
throw_breakpoint: module.add_function(
|
||||
"vm.breakpoint",
|
||||
void_ty.fn_type(&[i64_ty_basic], false),
|
||||
None,
|
||||
),
|
||||
ctx_ptr_ty,
|
||||
}
|
||||
};
|
||||
|
||||
let readonly =
|
||||
context.create_enum_attribute(Attribute::get_named_enum_kind_id("readonly"), 0);
|
||||
intrinsics
|
||||
.memory_size_dynamic_local
|
||||
.add_attribute(AttributeLoc::Function, readonly);
|
||||
intrinsics
|
||||
.memory_size_static_local
|
||||
.add_attribute(AttributeLoc::Function, readonly);
|
||||
intrinsics
|
||||
.memory_size_shared_local
|
||||
.add_attribute(AttributeLoc::Function, readonly);
|
||||
intrinsics
|
||||
.memory_size_dynamic_import
|
||||
.add_attribute(AttributeLoc::Function, readonly);
|
||||
intrinsics
|
||||
.memory_size_static_import
|
||||
.add_attribute(AttributeLoc::Function, readonly);
|
||||
intrinsics
|
||||
.memory_size_shared_import
|
||||
.add_attribute(AttributeLoc::Function, readonly);
|
||||
|
||||
let noreturn =
|
||||
context.create_enum_attribute(Attribute::get_named_enum_kind_id("noreturn"), 0);
|
||||
intrinsics
|
||||
.throw_trap
|
||||
.add_attribute(AttributeLoc::Function, noreturn);
|
||||
intrinsics
|
||||
.throw_breakpoint
|
||||
.add_attribute(AttributeLoc::Function, noreturn);
|
||||
|
||||
intrinsics
|
||||
}
|
||||
}
|
||||
|
||||
@ -855,4 +1027,31 @@ impl<'a> CtxType<'a> {
|
||||
|
||||
(imported_func_cache.func_ptr, imported_func_cache.ctx_ptr)
|
||||
}
|
||||
|
||||
pub fn internal_field(
|
||||
&mut self,
|
||||
index: usize,
|
||||
intrinsics: &Intrinsics,
|
||||
builder: &Builder,
|
||||
) -> PointerValue {
|
||||
assert!(index < INTERNALS_SIZE);
|
||||
|
||||
let local_internals_ptr_ptr = unsafe {
|
||||
builder.build_struct_gep(
|
||||
self.ctx_ptr_value,
|
||||
offset_to_index(Ctx::offset_internals()),
|
||||
"local_internals_ptr_ptr",
|
||||
)
|
||||
};
|
||||
let local_internals_ptr = builder
|
||||
.build_load(local_internals_ptr_ptr, "local_internals_ptr")
|
||||
.into_pointer_value();
|
||||
unsafe {
|
||||
builder.build_in_bounds_gep(
|
||||
local_internals_ptr,
|
||||
&[intrinsics.i32_ty.const_int(index as u64, false)],
|
||||
"local_internal_field_ptr",
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,10 @@
|
||||
#![deny(unused_imports, unused_variables, unused_unsafe, unreachable_patterns)]
|
||||
#![deny(
|
||||
dead_code,
|
||||
unused_imports,
|
||||
unused_variables,
|
||||
unused_unsafe,
|
||||
unreachable_patterns
|
||||
)]
|
||||
#![cfg_attr(nightly, feature(unwind_attributes))]
|
||||
|
||||
mod backend;
|
||||
@ -8,8 +14,14 @@ mod platform;
|
||||
mod read_info;
|
||||
mod stackmap;
|
||||
mod state;
|
||||
mod structs;
|
||||
mod trampolines;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub use code::LLVMFunctionCodeGenerator as FunctionCodeGenerator;
|
||||
pub use code::LLVMModuleCodeGenerator as ModuleCodeGenerator;
|
||||
|
||||
use wasmer_runtime_core::codegen::SimpleStreamingCompilerGen;
|
||||
|
||||
pub type LLVMCompiler = SimpleStreamingCompilerGen<
|
||||
@ -18,3 +30,22 @@ pub type LLVMCompiler = SimpleStreamingCompilerGen<
|
||||
backend::LLVMBackend,
|
||||
code::CodegenError,
|
||||
>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// LLVM backend flags.
|
||||
pub struct LLVMOptions {
|
||||
/// Emit LLVM IR before optimization pipeline.
|
||||
pub pre_opt_ir: Option<PathBuf>,
|
||||
|
||||
/// Emit LLVM IR after optimization pipeline.
|
||||
pub post_opt_ir: Option<PathBuf>,
|
||||
|
||||
/// Emit LLVM generated native code object file.
|
||||
pub obj_file: Option<PathBuf>,
|
||||
}
|
||||
|
||||
pub static mut GLOBAL_OPTIONS: LLVMOptions = LLVMOptions {
|
||||
pre_opt_ir: None,
|
||||
post_opt_ir: None,
|
||||
obj_file: None,
|
||||
};
|
||||
|
3
lib/llvm-backend/src/platform/common.rs
Normal file
3
lib/llvm-backend/src/platform/common.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub fn round_up_to_page_size(size: usize) -> usize {
|
||||
(size + (4096 - 1)) & !(4096 - 1)
|
||||
}
|
@ -1,7 +1,14 @@
|
||||
mod common;
|
||||
|
||||
#[cfg(unix)]
|
||||
mod unix;
|
||||
#[cfg(unix)]
|
||||
pub use self::unix::*;
|
||||
|
||||
#[cfg(target_family = "windows")]
|
||||
compile_error!("windows not yet supported for the llvm-based compiler backend");
|
||||
mod win;
|
||||
#[cfg(target_family = "windows")]
|
||||
pub use self::win::*;
|
||||
|
||||
#[cfg(not(any(unix, target_family = "windows")))]
|
||||
compile_error!("Your system is not yet supported for the llvm-based compiler backend");
|
||||
|
@ -1,5 +1,11 @@
|
||||
use libc::{c_void, siginfo_t};
|
||||
use super::common::round_up_to_page_size;
|
||||
use crate::structs::{LLVMResult, MemProtect};
|
||||
use libc::{
|
||||
c_void, mmap, mprotect, munmap, siginfo_t, MAP_ANON, MAP_PRIVATE, PROT_EXEC, PROT_NONE,
|
||||
PROT_READ, PROT_WRITE,
|
||||
};
|
||||
use nix::sys::signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, SIGBUS, SIGSEGV};
|
||||
use std::ptr;
|
||||
|
||||
/// `__register_frame` and `__deregister_frame` on macos take a single fde as an
|
||||
/// argument, so we need to parse the fde table here.
|
||||
@ -68,3 +74,60 @@ extern "C" fn signal_trap_handler(
|
||||
throw_trap(2);
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn alloc_memory(
|
||||
size: usize,
|
||||
protect: MemProtect,
|
||||
ptr_out: &mut *mut u8,
|
||||
size_out: &mut usize,
|
||||
) -> LLVMResult {
|
||||
let size = round_up_to_page_size(size);
|
||||
let ptr = mmap(
|
||||
ptr::null_mut(),
|
||||
size,
|
||||
match protect {
|
||||
MemProtect::NONE => PROT_NONE,
|
||||
MemProtect::READ => PROT_READ,
|
||||
MemProtect::READ_WRITE => PROT_READ | PROT_WRITE,
|
||||
MemProtect::READ_EXECUTE => PROT_READ | PROT_EXEC,
|
||||
},
|
||||
MAP_PRIVATE | MAP_ANON,
|
||||
-1,
|
||||
0,
|
||||
);
|
||||
if ptr as isize == -1 {
|
||||
return LLVMResult::ALLOCATE_FAILURE;
|
||||
}
|
||||
*ptr_out = ptr as _;
|
||||
*size_out = size;
|
||||
LLVMResult::OK
|
||||
}
|
||||
|
||||
pub unsafe fn protect_memory(ptr: *mut u8, size: usize, protect: MemProtect) -> LLVMResult {
|
||||
let res = mprotect(
|
||||
ptr as _,
|
||||
round_up_to_page_size(size),
|
||||
match protect {
|
||||
MemProtect::NONE => PROT_NONE,
|
||||
MemProtect::READ => PROT_READ,
|
||||
MemProtect::READ_WRITE => PROT_READ | PROT_WRITE,
|
||||
MemProtect::READ_EXECUTE => PROT_READ | PROT_EXEC,
|
||||
},
|
||||
);
|
||||
|
||||
if res == 0 {
|
||||
LLVMResult::OK
|
||||
} else {
|
||||
LLVMResult::PROTECT_FAILURE
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn dealloc_memory(ptr: *mut u8, size: usize) -> LLVMResult {
|
||||
let res = munmap(ptr as _, round_up_to_page_size(size));
|
||||
|
||||
if res == 0 {
|
||||
LLVMResult::OK
|
||||
} else {
|
||||
LLVMResult::DEALLOC_FAILURE
|
||||
}
|
||||
}
|
||||
|
80
lib/llvm-backend/src/platform/win.rs
Normal file
80
lib/llvm-backend/src/platform/win.rs
Normal file
@ -0,0 +1,80 @@
|
||||
use super::common::round_up_to_page_size;
|
||||
use crate::structs::{LLVMResult, MemProtect};
|
||||
use std::ptr;
|
||||
|
||||
use winapi::um::memoryapi::{VirtualAlloc, VirtualFree};
|
||||
use winapi::um::winnt::{
|
||||
MEM_COMMIT, MEM_DECOMMIT, MEM_RESERVE, PAGE_EXECUTE_READ, PAGE_NOACCESS, PAGE_READONLY,
|
||||
PAGE_READWRITE,
|
||||
};
|
||||
|
||||
pub unsafe fn visit_fde(_addr: *mut u8, _size: usize, _visitor: extern "C" fn(*mut u8)) {
|
||||
// Do nothing on Windows
|
||||
}
|
||||
|
||||
pub unsafe fn install_signal_handler() {
|
||||
// Do nothing on Windows
|
||||
}
|
||||
|
||||
pub unsafe fn alloc_memory(
|
||||
size: usize,
|
||||
protect: MemProtect,
|
||||
ptr_out: &mut *mut u8,
|
||||
size_out: &mut usize,
|
||||
) -> LLVMResult {
|
||||
let size = round_up_to_page_size(size);
|
||||
let flags = if protect == MemProtect::NONE {
|
||||
MEM_RESERVE
|
||||
} else {
|
||||
MEM_RESERVE | MEM_COMMIT
|
||||
};
|
||||
let ptr = VirtualAlloc(
|
||||
ptr::null_mut(),
|
||||
size,
|
||||
flags,
|
||||
memprotect_to_protect_const(protect),
|
||||
);
|
||||
|
||||
if ptr.is_null() {
|
||||
return LLVMResult::ALLOCATE_FAILURE;
|
||||
}
|
||||
|
||||
*ptr_out = ptr as _;
|
||||
*size_out = size;
|
||||
LLVMResult::OK
|
||||
}
|
||||
|
||||
pub unsafe fn protect_memory(ptr: *mut u8, size: usize, protect: MemProtect) -> LLVMResult {
|
||||
let size = round_up_to_page_size(size);
|
||||
let ptr = VirtualAlloc(
|
||||
ptr as _,
|
||||
size,
|
||||
MEM_COMMIT,
|
||||
memprotect_to_protect_const(protect),
|
||||
);
|
||||
|
||||
if ptr.is_null() {
|
||||
LLVMResult::PROTECT_FAILURE
|
||||
} else {
|
||||
LLVMResult::OK
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn dealloc_memory(ptr: *mut u8, size: usize) -> LLVMResult {
|
||||
let success = VirtualFree(ptr as _, size, MEM_DECOMMIT);
|
||||
// If the function succeeds, the return value is nonzero.
|
||||
if success == 1 {
|
||||
LLVMResult::OK
|
||||
} else {
|
||||
LLVMResult::DEALLOC_FAILURE
|
||||
}
|
||||
}
|
||||
|
||||
fn memprotect_to_protect_const(protect: MemProtect) -> u32 {
|
||||
match protect {
|
||||
MemProtect::NONE => PAGE_NOACCESS,
|
||||
MemProtect::READ => PAGE_READONLY,
|
||||
MemProtect::READ_WRITE => PAGE_READWRITE,
|
||||
MemProtect::READ_EXECUTE => PAGE_EXECUTE_READ,
|
||||
}
|
||||
}
|
@ -7,12 +7,7 @@ pub fn type_to_type(ty: WpType) -> Result<Type, BinaryReaderError> {
|
||||
WpType::I64 => Type::I64,
|
||||
WpType::F32 => Type::F32,
|
||||
WpType::F64 => Type::F64,
|
||||
WpType::V128 => {
|
||||
return Err(BinaryReaderError {
|
||||
message: "the wasmer llvm backend does not yet support the simd extension",
|
||||
offset: -1isize as usize,
|
||||
});
|
||||
}
|
||||
WpType::V128 => Type::V128,
|
||||
_ => {
|
||||
return Err(BinaryReaderError {
|
||||
message: "that type is not supported as a wasmer type",
|
||||
|
@ -97,12 +97,6 @@ impl StackmapEntry {
|
||||
end: Option<(&StackmapEntry, &StkMapRecord)>,
|
||||
msm: &mut ModuleStateMap,
|
||||
) {
|
||||
#[derive(Clone, Debug)]
|
||||
enum RuntimeOrConstant {
|
||||
Runtime(MachineValue),
|
||||
Constant(u64),
|
||||
}
|
||||
|
||||
let func_base_addr = (size_record.function_address as usize)
|
||||
.checked_sub(code_addr)
|
||||
.unwrap();
|
||||
|
39
lib/llvm-backend/src/structs.rs
Normal file
39
lib/llvm-backend/src/structs.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use libc::c_char;
|
||||
|
||||
use wasmer_runtime_core::vm;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct LLVMModule {
|
||||
_private: [u8; 0],
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types, dead_code)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[repr(C)]
|
||||
pub enum MemProtect {
|
||||
NONE,
|
||||
READ,
|
||||
READ_WRITE,
|
||||
READ_EXECUTE,
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types, dead_code)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[repr(C)]
|
||||
pub enum LLVMResult {
|
||||
OK,
|
||||
ALLOCATE_FAILURE,
|
||||
PROTECT_FAILURE,
|
||||
DEALLOC_FAILURE,
|
||||
OBJECT_LOAD_FAILURE,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Callbacks {
|
||||
pub alloc_memory: extern "C" fn(usize, MemProtect, &mut *mut u8, &mut usize) -> LLVMResult,
|
||||
pub protect_memory: extern "C" fn(*mut u8, usize, MemProtect) -> LLVMResult,
|
||||
pub dealloc_memory: extern "C" fn(*mut u8, usize) -> LLVMResult,
|
||||
|
||||
pub lookup_vm_symbol: extern "C" fn(*const c_char, usize) -> *const vm::Func,
|
||||
pub visit_fde: extern "C" fn(*mut u8, usize, extern "C" fn(*mut u8)),
|
||||
}
|
@ -72,12 +72,14 @@ fn generate_trampoline(
|
||||
Type::I64 => intrinsics.i64_ptr_ty,
|
||||
Type::F32 => intrinsics.f32_ptr_ty,
|
||||
Type::F64 => intrinsics.f64_ptr_ty,
|
||||
Type::V128 => intrinsics.i128_ptr_ty,
|
||||
};
|
||||
|
||||
let mut args_vec = Vec::with_capacity(func_sig.params().len() + 1);
|
||||
args_vec.push(vmctx_ptr);
|
||||
|
||||
for (i, param_ty) in func_sig.params().iter().enumerate() {
|
||||
let mut i = 0;
|
||||
for param_ty in func_sig.params().iter() {
|
||||
let index = intrinsics.i32_ty.const_int(i as _, false);
|
||||
let item_pointer = unsafe { builder.build_in_bounds_gep(args_ptr, &[index], "arg_ptr") };
|
||||
|
||||
@ -88,6 +90,10 @@ fn generate_trampoline(
|
||||
|
||||
let arg = builder.build_load(typed_item_pointer, "arg");
|
||||
args_vec.push(arg);
|
||||
i = i + 1;
|
||||
if *param_ty == Type::V128 {
|
||||
i = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
let call_site = builder.build_call(func_ptr, &args_vec, "call");
|
||||
|
Reference in New Issue
Block a user