Remove the C++ code.

This is replaced in reborn by the engines.
This commit is contained in:
Nick Lewycky
2020-05-20 15:41:11 -07:00
parent 2c9ba69767
commit dde6bc5269
4 changed files with 0 additions and 925 deletions

View File

@@ -1,300 +0,0 @@
//! This file was mostly taken from the llvm-sys crate.
//! (https://bitbucket.org/tari/llvm-sys.rs/raw/94361c1083a88f439b9d24c59b2d2831517413d7/build.rs)
use lazy_static::lazy_static;
use regex::Regex;
use semver::Version;
use std::env;
use std::ffi::OsStr;
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 = "100";
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 = {
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, CRATE_VERSION.minor),
]
};
/// Filesystem path to an llvm-config binary for the correct version.
static ref LLVM_CONFIG_PATH: PathBuf = {
// Try llvm-config via PATH first.
if let Some(name) = locate_system_llvm_config() {
return name.into();
} else {
println!("Didn't find usable system-wide LLVM.");
}
// Did the user give us a binary path to use? If yes, try
// to use that and fail if it doesn't work.
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");
pb.push(binary_name);
let ver = llvm_version(&pb)
.expect(&format!("Failed to execute {:?}", &pb));
if is_compatible_llvm(&ver) {
return pb;
} else {
println!("LLVM binaries specified by {} are the wrong version.
(Found {}, need {}.)", *ENV_LLVM_PREFIX, ver, *CRATE_VERSION);
}
}
}
println!("No suitable version of LLVM was found system-wide or pointed
to by {}.
Consider using `llvmenv` to compile an appropriate copy of LLVM, and
refer to the llvm-sys documentation for more information.
llvm-sys: https://crates.io/crates/llvm-sys
llvmenv: https://crates.io/crates/llvmenv", *ENV_LLVM_PREFIX);
panic!("Could not find a compatible version of LLVM");
};
}
/// Try to find a system-wide version of llvm-config that is compatible with
/// this crate.
///
/// Returns None on failure.
fn locate_system_llvm_config() -> Option<&'static str> {
for binary_name in LLVM_CONFIG_BINARY_NAMES.iter() {
match llvm_version(binary_name) {
Ok(ref version) if is_compatible_llvm(version) => {
// Compatible version found. Nice.
return Some(binary_name);
}
Ok(version) => {
// Version mismatch. Will try further searches, but warn that
// we're not using the system one.
println!(
"Found LLVM version {} on PATH, but need {}.",
version, *CRATE_VERSION
);
}
Err(ref e) if e.kind() == ErrorKind::NotFound => {
// Looks like we failed to execute any llvm-config. Keep
// searching.
}
// Some other error, probably a weird failure. Give up.
Err(e) => panic!("Failed to search PATH for llvm-config: {}", e),
}
}
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 {
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 {
llvm_version.major >= CRATE_VERSION.major
|| (llvm_version.major == CRATE_VERSION.major
&& llvm_version.minor >= CRATE_VERSION.minor)
}
}
/// Get the output from running `llvm-config` with the given argument.
///
/// Lazily searches for or compiles LLVM as configured by the environment
/// variables.
fn llvm_config(arg: &str) -> String {
llvm_config_ex(&*LLVM_CONFIG_PATH, arg).expect("Surprising failure from llvm-config")
}
/// Invoke the specified binary as llvm-config.
///
/// Explicit version of the `llvm_config` function that bubbles errors
/// up.
fn llvm_config_ex<S: AsRef<OsStr>>(binary: S, arg: &str) -> io::Result<String> {
Command::new(binary)
.arg(arg)
.arg("--link-static") // Don't use dylib for >= 3.9
.output()
.map(|output| {
String::from_utf8(output.stdout).expect("Output from llvm-config was not valid UTF-8")
})
}
/// Get the LLVM version using llvm-config.
fn llvm_version<S: AsRef<OsStr>>(binary: S) -> io::Result<Version> {
let version_str = llvm_config_ex(binary.as_ref(), "--version")?;
// LLVM isn't really semver and uses version suffixes to build
// version strings like '3.8.0svn', so limit what we try to parse
// to only the numeric bits.
let re = Regex::new(r"^(?P<major>\d+)\.(?P<minor>\d+)(?:\.(?P<patch>\d+))??").unwrap();
let c = re
.captures(&version_str)
.expect("Could not determine LLVM version from llvm-config.");
// some systems don't have a patch number but Version wants it so we just append .0 if it isn't
// there
let s = match c.name("patch") {
None => format!("{}.0", &c[0]),
Some(_) => c[0].to_string(),
};
Ok(Version::parse(&s).unwrap())
}
fn get_llvm_cxxflags() -> String {
let output = llvm_config("--cxxflags");
// llvm-config includes cflags from its own compilation with --cflags that
// may not be relevant to us. In particularly annoying cases, these might
// 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(&*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
// the user requests it.
return output;
}
output
.split(&[' ', '\n'][..])
.filter(|word| !word.starts_with("-W"))
.filter(|word| word != &"-fno-exceptions")
.collect::<Vec<_>>()
.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");
// 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");
}
}

View File

@@ -1,271 +0,0 @@
#include "object_loader.hh"
#include <iostream>
#include <memory>
#include <setjmp.h>
extern "C" void __register_frame(uint8_t *);
extern "C" void __deregister_frame(uint8_t *);
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);
}
void unwinding_setjmp(jmp_buf stack_out, void (*func)(void *), void *userdata) {
if (setjmp(stack_out)) {
} else {
func(userdata);
}
}
[[noreturn]] void unwinding_longjmp(jmp_buf stack_in) { longjmp(stack_in, 42); }
struct UnwindPoint {
UnwindPoint *prev;
jmp_buf stack;
std::function<void()> *f;
std::unique_ptr<WasmException> exception;
};
static thread_local UnwindPoint *unwind_state = nullptr;
static void unwind_payload(void *_point) {
UnwindPoint *point = (UnwindPoint *)_point;
(*point->f)();
}
void catch_unwind(std::function<void()> &&f) {
UnwindPoint current;
current.prev = unwind_state;
current.f = &f;
unwind_state = &current;
unwinding_setjmp(current.stack, unwind_payload, (void *)&current);
unwind_state = current.prev;
if (current.exception) {
throw std::move(current.exception);
}
}
void unsafe_unwind(WasmException *exception) {
UnwindPoint *state = unwind_state;
if (state) {
state->exception.reset(exception);
unwinding_longjmp(state->stack);
} else {
abort();
}
}
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);
}
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);
}
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;
}
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) {
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 *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;
}
bool MemoryManager::needsToReserveAllocationSpace() { return true; }
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);
#endif
}
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
}
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;
}
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;
}
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:
SymbolLookup(callbacks_t callbacks) : callbacks(callbacks) {}
void lookup(const LookupSet &symbols, OnResolvedFunction OnResolved) {
LookupResult result;
for (auto symbol : symbols) {
result.emplace(symbol, symbol_lookup(symbol));
}
OnResolved(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());
return llvm::JITEvaluatedSymbol(addr, llvm::JITSymbolFlags::None);
}
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))) {
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->loadObject(*object_file);
runtime_dyld->finalizeWithMemoryManagerLocking();
if (runtime_dyld->hasError()) {
_init_failed = true;
return;
}
} else {
_init_failed = true;
}
}
void *WasmModule::get_func(llvm::StringRef name) const {
auto symbol = runtime_dyld->getSymbol(name);
return (void *)symbol.getAddress();
}
uint8_t *WasmModule::get_stack_map_ptr() const {
return memory_manager->get_stack_map_ptr();
}
size_t WasmModule::get_stack_map_size() const {
return memory_manager->get_stack_map_size();
}
uint8_t *WasmModule::get_code_ptr() const {
return memory_manager->get_code_ptr();
}
size_t WasmModule::get_code_size() const {
return memory_manager->get_code_size();
}

View File

@@ -1,328 +0,0 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <exception>
#include <functional>
#include <iostream>
#include <memory>
#include <setjmp.h>
#include <sstream>
#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,
} 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 (*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 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;
lookup_vm_symbol_t lookup_vm_symbol;
visit_fde_t visit_fde;
} callbacks_t;
typedef struct {
size_t data, vtable;
} box_any_t;
enum WasmTrapType {
Unreachable = 0,
IncorrectCallIndirectSignature = 1,
MemoryOutOfBounds = 2,
CallIndirectOOB = 3,
IllegalArithmetic = 4,
MisalignedAtomicAccess = 5,
Unknown,
};
extern "C" void callback_trampoline(void *, void *);
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 WasmErrorSink {
WasmTrapType *trap_out;
box_any_t *user_error;
};
struct WasmException : std::exception {
public:
virtual std::string description() const noexcept { return "unknown"; }
virtual const char *what() const noexcept override {
return "wasm exception";
}
virtual void write_error(WasmErrorSink &out) const noexcept {
*out.trap_out = WasmTrapType::Unknown;
}
};
void catch_unwind(std::function<void()> &&f);
[[noreturn]] void unsafe_unwind(WasmException *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}) {}
virtual std::string description() const noexcept override {
return "user exception";
}
// The parts of a `Box<dyn Any>`.
box_any_t error_data;
virtual void write_error(WasmErrorSink &out) const noexcept override {
*out.user_error = error_data;
}
};
struct BreakpointException : UncatchableException {
public:
BreakpointException(uintptr_t callback) : callback(callback) {}
virtual std::string description() const noexcept override {
return "breakpoint exception";
}
uintptr_t callback;
virtual void write_error(WasmErrorSink &out) const noexcept override {
puts("CB TRAMPOLINE");
callback_trampoline(out.user_error, (void *)callback);
}
};
struct WasmModule {
public:
WasmModule(const uint8_t *object_start, size_t object_size,
callbacks_t callbacks);
void *get_func(llvm::StringRef name) const;
uint8_t *get_stack_map_ptr() const;
size_t get_stack_map_size() const;
uint8_t *get_code_ptr() const;
size_t get_code_size() const;
bool _init_failed = false;
private:
std::unique_ptr<MemoryManager> memory_manager;
std::unique_ptr<llvm::object::ObjectFile> object_file;
std::unique_ptr<llvm::RuntimeDyld> runtime_dyld;
};
struct WasmTrap : UncatchableException {
public:
WasmTrap(WasmTrapType type) : type(type) {}
virtual std::string description() const noexcept override {
std::ostringstream ss;
ss << "WebAssembly trap:" << '\n' << " - type: " << type << '\n';
return ss.str();
}
WasmTrapType type;
virtual void write_error(WasmErrorSink &out) const noexcept override {
*out.trap_out = type;
}
private:
friend std::ostream &operator<<(std::ostream &out, const WasmTrapType &ty) {
switch (ty) {
case WasmTrapType::Unreachable:
out << "unreachable";
break;
case WasmTrapType::IncorrectCallIndirectSignature:
out << "incorrect call_indirect signature";
break;
case WasmTrapType::MemoryOutOfBounds:
out << "memory access out-of-bounds";
break;
case WasmTrapType::CallIndirectOOB:
out << "call_indirect out-of-bounds";
break;
case WasmTrapType::IllegalArithmetic:
out << "illegal arithmetic operation";
break;
case WasmTrapType::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];
};
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);
if ((*module_out)->_init_failed) {
return RESULT_OBJECT_LOAD_FAILURE;
}
return RESULT_OK;
}
[[noreturn]] void throw_trap(WasmTrapType ty) {
unsafe_unwind(new 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) {
unsafe_unwind(new UserException(data, vtable));
}
// Throw a pointer that's assumed to be codegen::BreakpointHandler on the
// rust side.
[[noreturn]] void throw_breakpoint(uintptr_t callback) {
unsafe_unwind(new BreakpointException(callback));
}
bool cxx_invoke_trampoline(trampoline_t trampoline, void *ctx, void *func,
void *params, void *results, WasmTrapType *trap_out,
box_any_t *user_error, void *invoke_env) noexcept {
try {
catch_unwind([trampoline, ctx, func, params, results]() {
trampoline(ctx, func, params, results);
});
return true;
} catch (std::unique_ptr<WasmException> &e) {
WasmErrorSink sink;
sink.trap_out = trap_out;
sink.user_error = user_error;
e->write_error(sink);
return false;
} catch (...) {
*trap_out = WasmTrapType::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();
}
size_t llvm_backend_get_stack_map_size(const WasmModule *module) {
return module->get_stack_map_size();
}
const uint8_t *llvm_backend_get_code_ptr(const WasmModule *module) {
return module->get_code_ptr();
}
size_t llvm_backend_get_code_size(const WasmModule *module) {
return module->get_code_size();
}
}

View File

@@ -1421,32 +1421,6 @@ fn finalize_opcode_stack_map<'ctx>(
}
*/
// TODO: breakpoints are gone, remove this.
pub type BreakpointHandler =
Box<dyn Fn(BreakpointInfo) -> Result<(), Box<dyn Any + Send>> + Send + Sync + 'static>;
/// Information for a breakpoint
pub struct BreakpointInfo {
/// Fault.
pub fault: Option<()>,
}
// This is only called by C++ code, the 'pub' + '#[no_mangle]' combination
// prevents unused function elimination.
#[no_mangle]
pub unsafe extern "C" fn callback_trampoline(
b: *mut Option<Box<dyn std::any::Any>>,
callback: *mut BreakpointHandler,
) {
let callback = Box::from_raw(callback);
let result: Result<(), Box<dyn std::any::Any + Send>> =
callback(BreakpointInfo { fault: None });
match result {
Ok(()) => *b = None,
Err(e) => *b = Some(e),
}
}
pub struct LLVMFunctionCodeGenerator<'ctx, 'a> {
context: &'ctx Context,
builder: Builder<'ctx>,