Reactor support. (#1565)

* Reactor support.

This implements the new WASI ABI described here:

https://github.com/WebAssembly/WASI/blob/master/design/application-abi.md

It adds APIs to `Instance` and `Linker` with support for running
WASI programs, and also simplifies the process of instantiating
WASI API modules.

This currently only includes Rust API support.

* Add comments and fix a typo in a comment.

* Fix a rustdoc warning.

* Tidy an unneeded `mut`.

* Factor out instance initialization with `NewInstance`.

This also separates instantiation from initialization in a manner
similar to https://github.com/bytecodealliance/lucet/pull/506.

* Update fuzzing oracles for the API changes.

* Remove `wasi_linker` and clarify that Commands/Reactors aren't connected to WASI.

* Move Command/Reactor semantics into the Linker.

* C API support.

* Fix fuzzer build.

* Update usage syntax from "::" to "=".

* Remove `NewInstance` and `start()`.

* Elaborate on Commands and Reactors and add a spec link.

* Add more comments.

* Fix wat syntax.

* Fix wat.

* Use the `Debug` formatter to format an anyhow::Error.

* Fix wat.
This commit is contained in:
Dan Gohman
2020-05-26 08:39:40 -07:00
committed by GitHub
parent c619136752
commit 3715e19c67
19 changed files with 724 additions and 245 deletions

View File

@@ -72,51 +72,30 @@ int main() {
if (wasi == NULL)
exit_with_error("failed to instantiate WASI", NULL, trap);
// Create import list for our module using wasi
wasm_importtype_vec_t import_types;
wasm_module_imports(module, &import_types);
const wasm_extern_t **imports = calloc(import_types.size, sizeof(void*));
assert(imports);
for (int i = 0; i < import_types.size; i++) {
const wasm_extern_t *binding = wasi_instance_bind_import(wasi, import_types.data[i]);
if (binding != NULL) {
imports[i] = binding;
} else {
printf("> Failed to satisfy import\n");
exit(1);
}
}
wasmtime_linker_t *linker = wasmtime_linker_new(store);
error = wasmtime_linker_define_wasi(linker, wasi);
if (error != NULL)
exit_with_error("failed to link wasi", error, NULL);
// Instantiate the module
wasm_name_t empty;
wasm_name_new_from_string(&empty, "");
wasm_instance_t *instance = NULL;
error = wasmtime_instance_new(module, imports, import_types.size, &instance, &trap);
if (instance == NULL)
exit_with_error("failed to instantiate", error, trap);
free(imports);
wasm_importtype_vec_delete(&import_types);
error = wasmtime_linker_module(linker, &empty, module);
if (error != NULL)
exit_with_error("failed to instantiate module", error, NULL);
// Lookup our `_start` export function
wasm_extern_vec_t externs;
wasm_instance_exports(instance, &externs);
wasm_exporttype_vec_t exports;
wasm_module_exports(module, &exports);
wasm_extern_t *start_extern = NULL;
for (int i = 0; i < exports.size; i++) {
const wasm_name_t *name = wasm_exporttype_name(exports.data[i]);
if (strncmp(name->data, "_start", name->size) == 0)
start_extern = externs.data[i];
}
assert(start_extern);
wasm_func_t *start = wasm_extern_as_func(start_extern);
assert(start != NULL);
error = wasmtime_func_call(start, NULL, 0, NULL, 0, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call `_start`", error, trap);
// Run it.
wasm_func_t* func;
wasmtime_linker_get_default(linker, &empty, &func);
if (error != NULL)
exit_with_error("failed to locate default export for module", error, NULL);
error = wasmtime_func_call(func, NULL, 0, NULL, 0, &trap);
if (error != NULL)
exit_with_error("error calling default export", error, trap);
// Clean up after ourselves at this point
wasm_exporttype_vec_delete(&exports);
wasm_extern_vec_delete(&externs);
wasm_instance_delete(instance);
wasm_name_delete(&empty);
wasm_module_delete(module);
wasm_store_delete(store);
wasm_engine_delete(engine);

View File

@@ -9,32 +9,18 @@ use wasmtime_wasi::{Wasi, WasiCtx};
fn main() -> Result<()> {
let store = Store::default();
let module = Module::from_file(&store, "target/wasm32-wasi/debug/wasi.wasm")?;
let mut linker = Linker::new(&store);
// Create an instance of `Wasi` which contains a `WasiCtx`. Note that
// `WasiCtx` provides a number of ways to configure what the target program
// will have access to.
let wasi = Wasi::new(&store, WasiCtx::new(std::env::args())?);
let mut imports = Vec::new();
for import in module.imports() {
if import.module() == "wasi_snapshot_preview1" {
if let Some(export) = wasi.get_export(import.name()) {
imports.push(Extern::from(export.clone()));
continue;
}
}
panic!(
"couldn't find import for `{}::{}`",
import.module(),
import.name()
);
}
wasi.add_to_linker(&mut linker)?;
// Instantiate our module with the imports we've created, and run it.
let module = Module::from_file(&store, "target/wasm32-wasi/debug/wasi.wasm")?;
linker.module("", &module)?;
linker.get_default("")?.get0::<()>()?()?;
// Instance our module with the imports we've created, then we can run the
// standard wasi `_start` function.
let instance = Instance::new(&module, &imports)?;
let start = instance.get_func("_start").unwrap();
let start = start.get0::<()>()?;
start()?;
Ok(())
}