Skip to content

Commit

Permalink
Disconnects Store state fields from Compiler (#1761)
Browse files Browse the repository at this point in the history
*  Moves CodeMemory, VMInterrupts and SignatureRegistry from Compiler
*  CompiledModule holds CodeMemory and GdbJitImageRegistration
*  Store keeps track of its JIT code
*  Makes "jit_int.rs" stuff Send+Sync
*  Adds the threads example.
  • Loading branch information
yurydelendik authored Jun 2, 2020
1 parent b413303 commit 15c68f2
Show file tree
Hide file tree
Showing 61 changed files with 982 additions and 663 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ is now enabled by default.

[#1667](https://github.com/bytecodealliance/wasmtime/pull/1667)

The Rust API does not require a store provided during `Module::new` operation. The `Module` can be send accross threads and instantiate for a specific store. The `Instance::new` now requires the store.

[#1761](https://github.com/bytecodealliance/wasmtime/pull/1761)

--------------------------------------------------------------------------------

## 0.16.0
Expand Down
1 change: 1 addition & 0 deletions crates/c-api/include/wasmtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ WASM_API_EXTERN own wasmtime_error_t *wasmtime_global_set(
// instance is returned), or an instance can be returned (meaning no error or
// trap is returned).
WASM_API_EXTERN own wasmtime_error_t *wasmtime_instance_new(
wasm_store_t *store,
const wasm_module_t *module,
const wasm_extern_t* const imports[],
size_t num_imports,
Expand Down
21 changes: 8 additions & 13 deletions crates/c-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,10 @@ pub unsafe extern "C" fn wasm_instance_new(
imports: *const Box<wasm_extern_t>,
result: Option<&mut *mut wasm_trap_t>,
) -> Option<Box<wasm_instance_t>> {
let store = &store.store;
let module = &wasm_module.module.borrow();
if !Store::same(&store, module.store()) {
if let Some(result) = result {
let trap = Trap::new("wasm_store_t must match store in wasm_module_t");
let trap = Box::new(wasm_trap_t::new(store, trap));
*result = Box::into_raw(trap);
}
return None;
}
let mut instance = ptr::null_mut();
let mut trap = ptr::null_mut();
let err = wasmtime_instance_new(
store,
wasm_module,
imports,
wasm_module.imports.len(),
Expand All @@ -60,7 +51,7 @@ pub unsafe extern "C" fn wasm_instance_new(
assert!(trap.is_null());
assert!(instance.is_null());
if let Some(result) = result {
*result = Box::into_raw(err.to_trap(store));
*result = Box::into_raw(err.to_trap(&store.store));
}
None
}
Expand All @@ -83,13 +74,15 @@ pub unsafe extern "C" fn wasm_instance_new(

#[no_mangle]
pub unsafe extern "C" fn wasmtime_instance_new(
store: &wasm_store_t,
module: &wasm_module_t,
imports: *const Box<wasm_extern_t>,
num_imports: usize,
instance_ptr: &mut *mut wasm_instance_t,
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
_wasmtime_instance_new(
store,
module,
std::slice::from_raw_parts(imports, num_imports),
instance_ptr,
Expand All @@ -98,11 +91,13 @@ pub unsafe extern "C" fn wasmtime_instance_new(
}

fn _wasmtime_instance_new(
store: &wasm_store_t,
module: &wasm_module_t,
imports: &[Box<wasm_extern_t>],
instance_ptr: &mut *mut wasm_instance_t,
trap_ptr: &mut *mut wasm_trap_t,
) -> Option<Box<wasmtime_error_t>> {
let store = &store.store;
let imports = imports
.iter()
.map(|import| match &import.which {
Expand All @@ -114,8 +109,8 @@ fn _wasmtime_instance_new(
.collect::<Vec<_>>();
let module = &module.module.borrow();
handle_instantiate(
module.store(),
Instance::new(module, &imports),
store,
Instance::new(store, module, &imports),
instance_ptr,
trap_ptr,
)
Expand Down
53 changes: 46 additions & 7 deletions crates/c-api/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::host_ref::HostRef;
use crate::{handle_result, wasmtime_error_t};
use crate::{wasm_byte_vec_t, wasm_exporttype_vec_t, wasm_importtype_vec_t};
use crate::{wasm_exporttype_t, wasm_importtype_t, wasm_store_t};
use crate::{
handle_result, wasm_byte_vec_t, wasm_exporttype_t, wasm_exporttype_vec_t, wasm_importtype_t,
wasm_importtype_vec_t, wasm_store_t, wasmtime_error_t,
};
use std::ptr;
use wasmtime::Module;
use wasmtime::{Engine, Module};

#[repr(C)]
#[derive(Clone)]
Expand All @@ -21,6 +22,14 @@ impl wasm_module_t {
}
}

#[repr(C)]
#[derive(Clone)]
pub struct wasm_shared_module_t {
module: Module,
}

wasmtime_c_api_macros::declare_own!(wasm_shared_module_t);

#[no_mangle]
pub extern "C" fn wasm_module_new(
store: &wasm_store_t,
Expand All @@ -44,7 +53,7 @@ pub extern "C" fn wasmtime_module_new(
) -> Option<Box<wasmtime_error_t>> {
let binary = binary.as_slice();
let store = &store.store;
handle_result(Module::from_binary(store, binary), |module| {
handle_result(Module::from_binary(store.engine(), binary), |module| {
let imports = module
.imports()
.map(|i| wasm_importtype_t::new(i.module().to_owned(), i.name().to_owned(), i.ty()))
Expand Down Expand Up @@ -73,8 +82,7 @@ pub extern "C" fn wasmtime_module_validate(
binary: &wasm_byte_vec_t,
) -> Option<Box<wasmtime_error_t>> {
let binary = binary.as_slice();
let store = &store.store;
handle_result(Module::validate(store, binary), |()| {})
handle_result(Module::validate(store.store.engine(), binary), |()| {})
}

#[no_mangle]
Expand All @@ -96,3 +104,34 @@ pub extern "C" fn wasm_module_imports(module: &wasm_module_t, out: &mut wasm_imp
.collect::<Vec<_>>();
out.set_buffer(buffer);
}

#[no_mangle]
pub extern "C" fn wasm_module_share(module: &wasm_module_t) -> Box<wasm_shared_module_t> {
Box::new(wasm_shared_module_t {
module: module.module.borrow().clone(),
})
}

#[no_mangle]
pub extern "C" fn wasm_module_obtain(
store: &wasm_store_t,
shared_module: &wasm_shared_module_t,
) -> Option<Box<wasm_module_t>> {
let module = shared_module.module.clone();
if !Engine::same(store.store.engine(), module.engine()) {
return None;
}
let imports = module
.imports()
.map(|i| wasm_importtype_t::new(i.module().to_owned(), i.name().to_owned(), i.ty()))
.collect::<Vec<_>>();
let exports = module
.exports()
.map(|e| wasm_exporttype_t::new(e.name().to_owned(), e.ty()))
.collect::<Vec<_>>();
Some(Box::new(wasm_module_t {
module: HostRef::new(&store.store, module),
imports,
exports,
}))
}
19 changes: 10 additions & 9 deletions crates/fuzzing/src/oracles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn instantiate_with_config(wasm: &[u8], config: Config) {
let store = Store::new(&engine);

log_wasm(wasm);
let module = match Module::new(&store, wasm) {
let module = match Module::new(&engine, wasm) {
Ok(module) => module,
Err(_) => return,
};
Expand All @@ -75,7 +75,7 @@ pub fn instantiate_with_config(wasm: &[u8], config: Config) {
// aren't caught during validation or compilation. For example, an imported
// table might not have room for an element segment that we want to
// initialize into it.
let _result = Instance::new(&module, &imports);
let _result = Instance::new(&store, &module, &imports);
}

/// Compile the Wasm buffer, and implicitly fail if we have an unexpected
Expand All @@ -88,9 +88,8 @@ pub fn compile(wasm: &[u8], strategy: Strategy) {
crate::init_fuzzing();

let engine = Engine::new(&crate::fuzz_default_config(strategy).unwrap());
let store = Store::new(&engine);
log_wasm(wasm);
let _ = Module::new(&store, wasm);
let _ = Module::new(&engine, wasm);
}

/// Instantiate the given Wasm module with each `Config` and call all of its
Expand Down Expand Up @@ -128,7 +127,7 @@ pub fn differential_execution(
let engine = Engine::new(config);
let store = Store::new(&engine);

let module = match Module::new(&store, &ttf.wasm) {
let module = match Module::new(&engine, &ttf.wasm) {
Ok(module) => module,
// The module might rely on some feature that our config didn't
// enable or something like that.
Expand Down Expand Up @@ -158,7 +157,7 @@ pub fn differential_execution(
// aren't caught during validation or compilation. For example, an imported
// table might not have room for an element segment that we want to
// initialize into it.
let instance = match Instance::new(&module, &imports) {
let instance = match Instance::new(&store, &module, &imports) {
Ok(instance) => instance,
Err(e) => {
eprintln!(
Expand Down Expand Up @@ -304,7 +303,7 @@ pub fn make_api_calls(api: crate::generators::api::ApiCalls) {
ApiCall::ModuleNew { id, wasm } => {
log::debug!("creating module: {}", id);
log_wasm(&wasm.wasm);
let module = match Module::new(store.as_ref().unwrap(), &wasm.wasm) {
let module = match Module::new(engine.as_ref().unwrap(), &wasm.wasm) {
Ok(m) => m,
Err(_) => continue,
};
Expand All @@ -324,7 +323,9 @@ pub fn make_api_calls(api: crate::generators::api::ApiCalls) {
None => continue,
};

let imports = match dummy_imports(store.as_ref().unwrap(), module.imports()) {
let store = store.as_ref().unwrap();

let imports = match dummy_imports(store, module.imports()) {
Ok(imps) => imps,
Err(_) => {
// There are some value types that we can't synthesize a
Expand All @@ -338,7 +339,7 @@ pub fn make_api_calls(api: crate::generators::api::ApiCalls) {
// aren't caught during validation or compilation. For example, an imported
// table might not have room for an element segment that we want to
// initialize into it.
if let Ok(instance) = Instance::new(&module, &imports) {
if let Ok(instance) = Instance::new(store, &module, &imports) {
instances.insert(id, instance);
}
}
Expand Down
11 changes: 5 additions & 6 deletions crates/jit/src/code_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ impl CodeMemoryEntry {
Ok(Self { mmap, registry })
}

fn contains(&self, addr: usize) -> bool {
fn range(&self) -> (usize, usize) {
let start = self.mmap.as_ptr() as usize;
let end = start + self.mmap.len();
start <= addr && addr < end
(start, end)
}
}

Expand Down Expand Up @@ -243,11 +243,10 @@ impl CodeMemory {
Ok(())
}

/// Returns whether any published segment of this code memory contains
/// `addr`.
pub fn published_contains(&self, addr: usize) -> bool {
/// Returns all published segment ranges.
pub fn published_ranges<'a>(&'a self) -> impl Iterator<Item = (usize, usize)> + 'a {
self.entries[..self.published]
.iter()
.any(|entry| entry.contains(addr))
.map(|entry| entry.range())
}
}
Loading

0 comments on commit 15c68f2

Please sign in to comment.