Replace the global-exports mechanism with a caller-vmctx mechanism. (#789)
* Replace the global-exports mechanism with a caller-vmctx mechanism. This eliminates the global exports mechanism, and instead adds a caller-vmctx argument to wasm functions so that WASI can obtain the memory and other things from the caller rather than looking them up in a global registry. This replaces #390. * Fixup some merge conflicts * Rustfmt * Ensure VMContext is aligned to 16 bytes With the removal of `global_exports` it "just so happens" that this isn't happening naturally any more. * Fixup some bugs with double vmctx in wasmtime crate * Trampoline stub needed adjusting * Use pointer type instead of always using I64 for caller vmctx * Don't store `ir::Signature` in `Func` since we don't know the pointer size at creation time. * Skip the first 2 arguments in IR signatures since that's the two vmctx parameters. * Update cranelift to 0.56.0 * Handle more merge conflicts * Rustfmt Co-authored-by: Alex Crichton <alex@alexcrichton.com>
This commit is contained in:
@@ -18,9 +18,8 @@ use crate::vmcontext::{
|
||||
use memoffset::offset_of;
|
||||
use more_asserts::assert_lt;
|
||||
use std::any::Any;
|
||||
use std::borrow::Borrow;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::collections::HashSet;
|
||||
use std::convert::TryFrom;
|
||||
use std::ptr::NonNull;
|
||||
use std::rc::Rc;
|
||||
@@ -294,11 +293,6 @@ pub(crate) struct Instance {
|
||||
/// Offsets in the `vmctx` region.
|
||||
offsets: VMOffsets,
|
||||
|
||||
/// A global namespace of exports. This is a temporary mechanism to avoid
|
||||
/// cyclic dependencies when one module wants to import from another and
|
||||
/// make its memory available too, that will be obviated by host-bindings.
|
||||
global_exports: Rc<RefCell<HashMap<String, Option<Export>>>>,
|
||||
|
||||
/// WebAssembly linear memory data.
|
||||
memories: BoxedSlice<DefinedMemoryIndex, LinearMemory>,
|
||||
|
||||
@@ -567,7 +561,7 @@ impl Instance {
|
||||
};
|
||||
|
||||
// Make the call.
|
||||
unsafe { wasmtime_call(callee_vmctx, callee_address) }
|
||||
unsafe { wasmtime_call(callee_vmctx, self.vmctx_mut_ptr(), callee_address) }
|
||||
.map_err(InstantiationError::StartTrap)
|
||||
}
|
||||
|
||||
@@ -689,15 +683,6 @@ impl Instance {
|
||||
foreign_instance.memory_size(foreign_index)
|
||||
}
|
||||
|
||||
pub(crate) fn lookup_global_export(&self, field: &str) -> Option<Export> {
|
||||
let cell: &RefCell<HashMap<String, Option<Export>>> = self.global_exports.borrow();
|
||||
let map: &mut HashMap<String, Option<Export>> = &mut cell.borrow_mut();
|
||||
if let Some(Some(export)) = map.get(field) {
|
||||
return Some(export.clone());
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Grow table by the specified amount of elements.
|
||||
///
|
||||
/// Returns `None` if table can't be grown by the specified amount
|
||||
@@ -772,7 +757,6 @@ impl InstanceHandle {
|
||||
/// Create a new `InstanceHandle` pointing at a new `Instance`.
|
||||
pub fn new(
|
||||
module: Rc<Module>,
|
||||
global_exports: Rc<RefCell<HashMap<String, Option<Export>>>>,
|
||||
finished_functions: BoxedSlice<DefinedFuncIndex, *const VMFunctionBody>,
|
||||
imports: Imports,
|
||||
data_initializers: &[DataInitializer<'_>],
|
||||
@@ -814,7 +798,6 @@ impl InstanceHandle {
|
||||
dependencies: imports.dependencies,
|
||||
mmap: instance_mmap,
|
||||
module,
|
||||
global_exports,
|
||||
offsets,
|
||||
memories,
|
||||
tables,
|
||||
@@ -886,25 +869,6 @@ impl InstanceHandle {
|
||||
initialize_memories(instance, data_initializers)?;
|
||||
initialize_globals(instance);
|
||||
|
||||
// Collect the exports for the global export map.
|
||||
for (field, decl) in &instance.module.exports {
|
||||
use std::collections::hash_map::Entry::*;
|
||||
let cell: &RefCell<HashMap<String, Option<Export>>> = instance.global_exports.borrow();
|
||||
let map: &mut HashMap<String, Option<Export>> = &mut cell.borrow_mut();
|
||||
match map.entry(field.to_string()) {
|
||||
Vacant(entry) => {
|
||||
entry.insert(Some(lookup_by_declaration(
|
||||
&instance.module,
|
||||
&mut instance.vmctx,
|
||||
&instance.offsets,
|
||||
&instance.finished_functions,
|
||||
&decl,
|
||||
)));
|
||||
}
|
||||
Occupied(ref mut entry) => *entry.get_mut() = None,
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure that our signal handlers are ready for action.
|
||||
// TODO: Move these calls out of `InstanceHandle`.
|
||||
wasmtime_init_eager();
|
||||
|
||||
Reference in New Issue
Block a user