* Compute instance exports on demand.

Instead having instances eagerly compute a Vec of Externs, and bumping
the refcount for each Extern, compute Externs on demand.

This also enables `Instance::get_export` to avoid doing a linear search.

This also means that the closure returned by `get0` and friends now
holds an `InstanceHandle` to dynamically hold the instance live rather
than being scoped to a lifetime.

* Compute module imports and exports on demand too.

And compute Extern::ty on demand too.

* Add a utility function for computing an ExternType.

* Add a utility function for looking up a function's signature.

* Add a utility function for computing the ValType of a Global.

* Rename wasmtime_environ::Export to EntityIndex.

This helps differentiate it from other Export types in the tree, and
describes what it is.

* Fix a typo in a comment.

* Simplify module imports and exports.

* Make `Instance::exports` return the export names.

This significantly simplifies the public API, as it's relatively common
to need the names, and this avoids the need to do a zip with
`Module::exports`.

This also changes `ImportType` and `ExportType` to have public members
instead of private members and accessors, as I find that simplifies the
usage particularly in cases where there are temporary instances.

* Remove `Instance::module`.

This doesn't quite remove `Instance`'s `module` member, it gets a step
closer.

* Use a InstanceHandle utility function.

* Don't consume self in the `Func::get*` methods.

Instead, just create a closure containing the instance handle and the
export for them to call.

* Use `ExactSizeIterator` to avoid needing separate `num_*` methods.

* Rename `Extern::func()` etc. to `into_func()` etc.

* Revise examples to avoid using `nth`.

* Add convenience methods to instance for getting specific extern types.

* Use the convenience functions in more tests and examples.

* Avoid cloning strings for `ImportType` and `ExportType`.

* Remove more obviated clone() calls.

* Simplify `Func`'s closure state.

* Make wasmtime::Export's fields private.

This makes them more consistent with ExportType.

* Fix compilation error.

* Make a lifetime parameter explicit, and use better lifetime names.

Instead of 'me, use 'instance and 'module to make it clear what the
lifetime is.

* More lifetime cleanups.
This commit is contained in:
Dan Gohman
2020-04-20 13:55:33 -07:00
committed by GitHub
parent 967827f4b5
commit 9364eb1d98
57 changed files with 788 additions and 875 deletions

View File

@@ -38,18 +38,13 @@ mod tests {
"#;
fn invoke_export(instance: &Instance, func_name: &str) -> Result<Box<[Val]>> {
let ret = instance
.get_export(func_name)
.unwrap()
.func()
.unwrap()
.call(&[])?;
let ret = instance.get_func(func_name).unwrap().call(&[])?;
Ok(ret)
}
// Locate "memory" export, get base address and size and set memory protection to PROT_NONE
fn set_up_memory(instance: &Instance) -> (*mut u8, usize) {
let mem_export = instance.get_export("memory").unwrap().memory().unwrap();
let mem_export = instance.get_memory("memory").unwrap();
let base = mem_export.data_ptr();
let length = mem_export.data_size();
@@ -105,9 +100,6 @@ mod tests {
});
}
let exports = instance.exports();
assert!(!exports.is_empty());
// these invoke wasmtime_call_trampoline from action.rs
{
println!("calling read...");
@@ -130,8 +122,8 @@ mod tests {
// these invoke wasmtime_call_trampoline from callable.rs
{
let read_func = exports[0]
.func()
let read_func = instance
.get_func("read")
.expect("expected a 'read' func in the module");
println!("calling read...");
let result = read_func.call(&[]).expect("expected function not to trap");
@@ -139,8 +131,8 @@ mod tests {
}
{
let read_out_of_bounds_func = exports[1]
.func()
let read_out_of_bounds_func = instance
.get_func("read_out_of_bounds")
.expect("expected a 'read_out_of_bounds' func in the module");
println!("calling read_out_of_bounds...");
let trap = read_out_of_bounds_func
@@ -216,8 +208,8 @@ mod tests {
// First instance1
{
let exports1 = instance1.exports();
assert!(!exports1.is_empty());
let mut exports1 = instance1.exports();
assert!(exports1.next().is_some());
println!("calling instance1.read...");
let result = invoke_export(&instance1, "read").expect("read succeeded");
@@ -231,8 +223,8 @@ mod tests {
// And then instance2
{
let exports2 = instance2.exports();
assert!(!exports2.is_empty());
let mut exports2 = instance2.exports();
assert!(exports2.next().is_some());
println!("calling instance2.read...");
let result = invoke_export(&instance2, "read").expect("read succeeded");
@@ -262,13 +254,12 @@ mod tests {
});
}
let instance1_exports = instance1.exports();
assert!(!instance1_exports.is_empty());
let instance1_read = instance1_exports[0].clone();
let mut instance1_exports = instance1.exports();
let instance1_read = instance1_exports.next().unwrap();
// instance2 wich calls 'instance1.read'
// instance2 which calls 'instance1.read'
let module2 = Module::new(&store, WAT2)?;
let instance2 = Instance::new(&module2, &[instance1_read])?;
let instance2 = Instance::new(&module2, &[instance1_read.into_extern()])?;
// since 'instance2.run' calls 'instance1.read' we need to set up the signal handler to handle
// SIGSEGV originating from within the memory of instance1
unsafe {

View File

@@ -269,14 +269,14 @@ fn get_from_module() -> anyhow::Result<()> {
"#,
)?;
let instance = Instance::new(&module, &[])?;
let f0 = instance.get_export("f0").unwrap().func().unwrap();
let f0 = instance.get_func("f0").unwrap();
assert!(f0.get0::<()>().is_ok());
assert!(f0.get0::<i32>().is_err());
let f1 = instance.get_export("f1").unwrap().func().unwrap();
let f1 = instance.get_func("f1").unwrap();
assert!(f1.get0::<()>().is_err());
assert!(f1.get1::<i32, ()>().is_ok());
assert!(f1.get1::<i32, f32>().is_err());
let f2 = instance.get_export("f2").unwrap().func().unwrap();
let f2 = instance.get_func("f2").unwrap();
assert!(f2.get0::<()>().is_err());
assert!(f2.get0::<i32>().is_ok());
assert!(f2.get1::<i32, ()>().is_err());

View File

@@ -70,7 +70,7 @@ fn use_after_drop() -> anyhow::Result<()> {
"#,
)?;
let instance = Instance::new(&module, &[])?;
let g = instance.exports()[0].global().unwrap().clone();
let g = instance.get_global("foo").unwrap();
assert_eq!(g.get().i32(), Some(100));
g.set(101.into())?;
drop(instance);

View File

@@ -40,18 +40,14 @@ fn test_import_calling_export() {
let instance =
Instance::new(&module, imports.as_slice()).expect("failed to instantiate module");
let exports = instance.exports();
assert!(!exports.is_empty());
let run_func = exports[0]
.func()
let run_func = instance
.get_func("run")
.expect("expected a run func in the module");
*other.borrow_mut() = Some(
exports[1]
.func()
.expect("expected an other func in the module")
.clone(),
instance
.get_func("other")
.expect("expected an other func in the module"),
);
run_func.call(&[]).expect("expected function not to trap");
@@ -84,11 +80,8 @@ fn test_returns_incorrect_type() -> Result<()> {
let imports = vec![callback_func.into()];
let instance = Instance::new(&module, imports.as_slice())?;
let exports = instance.exports();
assert!(!exports.is_empty());
let run_func = exports[0]
.func()
let run_func = instance
.get_func("run")
.expect("expected a run func in the module");
let trap = run_func

View File

@@ -43,7 +43,7 @@ fn same_import_names_still_distinct() -> anyhow::Result<()> {
];
let instance = Instance::new(&module, &imports)?;
let func = instance.get_export("foo").unwrap().func().unwrap();
let func = instance.get_func("foo").unwrap();
let results = func.call(&[])?;
assert_eq!(results.len(), 1);
match results[0] {

View File

@@ -17,9 +17,7 @@ fn test_invoke_func_via_table() -> Result<()> {
let instance = Instance::new(&module, &[]).context("> Error instantiating module!")?;
let f = instance
.get_export("table")
.unwrap()
.table()
.get_table("table")
.unwrap()
.get(0)
.unwrap()

View File

@@ -90,7 +90,7 @@ fn interposition() -> Result<()> {
)?;
}
let instance = linker.instantiate(&module)?;
let func = instance.get_export("export").unwrap().func().unwrap();
let func = instance.get_func("export").unwrap();
let func = func.get0::<i32>()?;
assert_eq!(func()?, 112);
Ok(())

View File

@@ -169,15 +169,7 @@ mod not_for_windows {
assert_eq!(*mem_creator.num_created_memories.lock().unwrap(), 2);
assert_eq!(
instance2
.get_export("memory")
.unwrap()
.memory()
.unwrap()
.size(),
2
);
assert_eq!(instance2.get_memory("memory").unwrap().size(), 2);
// we take the lock outside the assert, so it won't get poisoned on assert failure
let tot_pages = *mem_creator.num_total_pages.lock().unwrap();

View File

@@ -17,9 +17,7 @@ fn test_trap_return() -> Result<()> {
let hello_func = Func::new(&store, hello_type, |_, _, _| Err(Trap::new("test 123")));
let instance = Instance::new(&module, &[hello_func.into()])?;
let run_func = instance.exports()[0]
.func()
.expect("expected function export");
let run_func = instance.get_func("run").expect("expected function export");
let e = run_func
.call(&[])
@@ -44,9 +42,7 @@ fn test_trap_trace() -> Result<()> {
let module = Module::new(&store, wat)?;
let instance = Instance::new(&module, &[])?;
let run_func = instance.exports()[0]
.func()
.expect("expected function export");
let run_func = instance.get_func("run").expect("expected function export");
let e = run_func
.call(&[])
@@ -91,9 +87,7 @@ fn test_trap_trace_cb() -> Result<()> {
let module = Module::new(&store, wat)?;
let instance = Instance::new(&module, &[fn_func.into()])?;
let run_func = instance.exports()[0]
.func()
.expect("expected function export");
let run_func = instance.get_func("run").expect("expected function export");
let e = run_func
.call(&[])
@@ -123,9 +117,7 @@ fn test_trap_stack_overflow() -> Result<()> {
let module = Module::new(&store, wat)?;
let instance = Instance::new(&module, &[])?;
let run_func = instance.exports()[0]
.func()
.expect("expected function export");
let run_func = instance.get_func("run").expect("expected function export");
let e = run_func
.call(&[])
@@ -159,9 +151,7 @@ fn trap_display_pretty() -> Result<()> {
let module = Module::new(&store, wat)?;
let instance = Instance::new(&module, &[])?;
let run_func = instance.exports()[0]
.func()
.expect("expected function export");
let run_func = instance.get_func("bar").expect("expected function export");
let e = run_func.call(&[]).err().expect("error calling function");
assert_eq!(
@@ -192,7 +182,7 @@ fn trap_display_multi_module() -> Result<()> {
let module = Module::new(&store, wat)?;
let instance = Instance::new(&module, &[])?;
let bar = instance.exports()[0].clone();
let bar = instance.get_export("bar").unwrap();
let wat = r#"
(module $b
@@ -203,9 +193,7 @@ fn trap_display_multi_module() -> Result<()> {
"#;
let module = Module::new(&store, wat)?;
let instance = Instance::new(&module, &[bar])?;
let bar2 = instance.exports()[0]
.func()
.expect("expected function export");
let bar2 = instance.get_func("bar2").expect("expected function export");
let e = bar2.call(&[]).err().expect("error calling function");
assert_eq!(
@@ -268,14 +256,14 @@ fn rust_panic_import() -> Result<()> {
Func::wrap(&store, || panic!("this is another panic")).into(),
],
)?;
let func = instance.exports()[0].func().unwrap().clone();
let func = instance.get_func("foo").unwrap();
let err = panic::catch_unwind(AssertUnwindSafe(|| {
drop(func.call(&[]));
}))
.unwrap_err();
assert_eq!(err.downcast_ref::<&'static str>(), Some(&"this is a panic"));
let func = instance.exports()[1].func().unwrap().clone();
let func = instance.get_func("bar").unwrap();
let err = panic::catch_unwind(AssertUnwindSafe(|| {
drop(func.call(&[]));
}))
@@ -333,7 +321,7 @@ fn mismatched_arguments() -> Result<()> {
let module = Module::new(&store, &binary)?;
let instance = Instance::new(&module, &[])?;
let func = instance.exports()[0].func().unwrap().clone();
let func = instance.get_func("foo").unwrap();
assert_eq!(
func.call(&[]).unwrap_err().to_string(),
"expected 1 arguments, got 0"
@@ -417,7 +405,7 @@ fn present_after_module_drop() -> Result<()> {
let store = Store::default();
let module = Module::new(&store, r#"(func (export "foo") unreachable)"#)?;
let instance = Instance::new(&module, &[])?;
let func = instance.exports()[0].func().unwrap().clone();
let func = instance.get_func("foo").unwrap();
println!("asserting before we drop modules");
assert_trap(func.call(&[]).unwrap_err().downcast()?);

View File

@@ -1 +1 @@
(assert_invalid (module (memory 1 1 shared)) "not supported")
(assert_invalid (module (memory 1 1 shared)) "Unsupported feature: shared memories")