Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnects Store state fields from Compiler #1761

Merged
merged 39 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
aef8458
mv SignatureRegistry
yurydelendik May 22, 2020
050aebe
mv code_memory
yurydelendik May 22, 2020
d7defe2
Move Arc to CompiledModule level; unsafe Sync+Send
yurydelendik May 26, 2020
5a3a142
Threads example
yurydelendik May 26, 2020
9b08221
rm interrupts from Compiler
yurydelendik May 26, 2020
45717c3
rm Sync+Send from Module
yurydelendik May 26, 2020
dc497a5
Merge remote-tracking branch 'upstream/master' into mv-sig-registry
yurydelendik May 26, 2020
d47371f
c api
yurydelendik May 26, 2020
d4e4ade
rn module_ref()
yurydelendik May 26, 2020
a2b432b
small fixes
yurydelendik May 26, 2020
ede93de
rm Send+Sync from CompiledModule
yurydelendik May 26, 2020
2e73c4a
rm RawCompiledModule
yurydelendik May 26, 2020
edcf487
rm jit_code_registration; win32 test; any
yurydelendik May 27, 2020
3f7c2a5
rm SignatureRegistry lock
yurydelendik May 27, 2020
1ccdee7
replace Deref with InstanceContext
yurydelendik May 27, 2020
810ec8a
fix test
yurydelendik May 27, 2020
3ab0e3c
Redesign API to remove Store from Module::new
yurydelendik May 27, 2020
51b901b
fix docs
yurydelendik May 27, 2020
02f79f8
fix fuzz
yurydelendik May 27, 2020
2009eb7
jit_int comments; fix 'let _ =' lifetime
yurydelendik Jun 1, 2020
02bd6f4
Merge remote-tracking branch 'upstream/master' into mv-sig-registry
yurydelendik Jun 1, 2020
2f23c7f
Fix linker tests
yurydelendik Jun 1, 2020
bde14b9
Assert Module and Compiler's Sync+Send
yurydelendik Jun 1, 2020
3f04e73
One Compiler instance per Engine
yurydelendik Jun 1, 2020
6fd50db
Add thread safety doc
yurydelendik Jun 1, 2020
acc6603
check same engine constraint
yurydelendik Jun 1, 2020
b3d3eda
Reduces amount of held Module data
yurydelendik Jun 1, 2020
6e1a590
Change signatures of wasmtime_module_new/wasmtime_instance_new
yurydelendik Jun 1, 2020
449cc52
mv alloc/drop outside of lock
yurydelendik Jun 1, 2020
4048b5a
mv build_compiler()
yurydelendik Jun 1, 2020
ce9bedf
register_jit_code comment
yurydelendik Jun 1, 2020
9cf7a61
Remove signatures() accessors where possible
yurydelendik Jun 1, 2020
88093a6
fix tests
yurydelendik Jun 1, 2020
fc9d289
revert some changes
yurydelendik Jun 1, 2020
4cefa4a
Fix intermitent failures in threads tests
yurydelendik Jun 1, 2020
5fc0a16
Merge remote-tracking branch 'upstream/master' into mv-sig-registry
yurydelendik Jun 2, 2020
65f2c00
mv unwrap(); reduce code churn
yurydelendik Jun 2, 2020
757ce85
Merge remote-tracking branch 'upstream/master' into mv-sig-registry
yurydelendik Jun 2, 2020
430a417
RELEASES.md
yurydelendik Jun 2, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

3 changes: 2 additions & 1 deletion 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 All @@ -254,7 +255,7 @@ WASM_API_EXTERN own wasmtime_error_t *wasmtime_instance_new(
// Similar to `wasm_module_new`, but an error is returned to return a
// descriptive error message in case compilation fails.
WASM_API_EXTERN own wasmtime_error_t *wasmtime_module_new(
wasm_store_t *store,
wasm_engine_t *engine,
const wasm_byte_vec_t *binary,
own wasm_module_t **ret
);
Expand Down
9 changes: 5 additions & 4 deletions crates/c-api/src/engine.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::wasm_config_t;
use wasmtime::{Engine, HostRef};
use std::sync::Arc;
use wasmtime::Engine;

#[repr(C)]
#[derive(Clone)]
pub struct wasm_engine_t {
pub(crate) engine: HostRef<Engine>,
pub(crate) engine: Arc<Engine>,
}

wasmtime_c_api_macros::declare_own!(wasm_engine_t);
Expand All @@ -22,14 +23,14 @@ pub extern "C" fn wasm_engine_new() -> Box<wasm_engine_t> {
drop(env_logger::try_init());

Box::new(wasm_engine_t {
engine: HostRef::new(Engine::default()),
engine: Arc::new(Engine::default()),
})
}

#[no_mangle]
pub extern "C" fn wasm_engine_new_with_config(c: Box<wasm_config_t>) -> Box<wasm_engine_t> {
let config = c.config;
Box::new(wasm_engine_t {
engine: HostRef::new(Engine::new(&config)),
engine: Arc::new(Engine::new(&config)),
})
}
23 changes: 11 additions & 12 deletions crates/c-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{wasm_store_t, wasmtime_error_t, ExternHost};
use anyhow::Result;
use std::cell::RefCell;
use std::ptr;
use wasmtime::{Extern, HostRef, Instance, Store, Trap};
use wasmtime::{Extern, HostRef, Instance, Trap};

#[repr(C)]
#[derive(Clone)]
Expand Down Expand Up @@ -34,19 +34,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.borrow();
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(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 Down Expand Up @@ -81,13 +72,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 @@ -96,11 +89,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.borrow();
let imports = imports
.iter()
.map(|import| match &import.which {
Expand All @@ -111,7 +106,11 @@ fn _wasmtime_instance_new(
})
.collect::<Vec<_>>();
let module = &module.module.borrow();
handle_instantiate(Instance::new(module, &imports), instance_ptr, trap_ptr)
handle_instantiate(
Instance::new(store, module, &imports),
instance_ptr,
trap_ptr,
)
}

pub fn handle_instantiate(
Expand Down
66 changes: 56 additions & 10 deletions crates/c-api/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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_engine_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::{HostRef, Module};
use std::sync::Arc;
use wasmtime::{Engine, HostRef, Module};

#[repr(C)]
#[derive(Clone)]
Expand All @@ -20,13 +22,25 @@ 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,
binary: &wasm_byte_vec_t,
) -> Option<Box<wasm_module_t>> {
let mut ret = ptr::null_mut();
match wasmtime_module_new(store, binary, &mut ret) {
let store = store.store.clone();
let engine = wasm_engine_t {
engine: Arc::new(store.borrow().engine().clone()),
};
match wasmtime_module_new(&engine, binary, &mut ret) {
Some(_err) => None,
None => {
assert!(!ret.is_null());
Expand All @@ -37,13 +51,12 @@ pub extern "C" fn wasm_module_new(

#[no_mangle]
pub extern "C" fn wasmtime_module_new(
store: &wasm_store_t,
engine: &wasm_engine_t,
binary: &wasm_byte_vec_t,
ret: &mut *mut wasm_module_t,
) -> Option<Box<wasmtime_error_t>> {
let binary = binary.as_slice();
let store = &store.store.borrow();
handle_result(Module::from_binary(store, binary), |module| {
handle_result(Module::from_binary(&engine.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 @@ -72,8 +85,10 @@ 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.borrow();
handle_result(Module::validate(store, binary), |()| {})
handle_result(
Module::validate(store.store.borrow().engine(), binary),
|()| {},
)
}

#[no_mangle]
Expand All @@ -95,3 +110,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.borrow().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(module),
imports,
exports,
}))
}
3 changes: 1 addition & 2 deletions crates/c-api/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ wasmtime_c_api_macros::declare_own!(wasm_store_t);

#[no_mangle]
pub extern "C" fn wasm_store_new(engine: &wasm_engine_t) -> Box<wasm_store_t> {
let engine = &engine.engine;
Box::new(wasm_store_t {
store: HostRef::new(Store::new(&engine.borrow())),
store: HostRef::new(Store::new(&engine.engine)),
})
}

Expand Down
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