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

[refactor] Improve the VmBuilder::with_plugin API #82

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ jobs:
working-directory: WasmEdge
run: |
apt update
apt install -y software-properties-common libboost-all-dev llvm-15-dev liblld-15-dev ninja-build
cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_PLUGIN_PROCESS=On .
apt install -y software-properties-common libboost-all-dev llvm-15-dev liblld-15-dev ninja-build libssl-dev
cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_PLUGIN_PROCESS=On -DWASMEDGE_PLUGIN_WASI_CRYPTO=On .
cmake --build build
cmake --install build
ldconfig
Expand Down Expand Up @@ -151,7 +151,7 @@ jobs:
export WASMEDGE_BUILD_DIR="$(pwd)/WasmEdge/build"
export WASMEDGE_PLUGIN_PATH="$(pwd)/WasmEdge/build/plugins/wasmedge_process"
export LD_LIBRARY_PATH="$(pwd)/WasmEdge/build/lib/api"
cargo test --workspace --locked --features aot,wasmedge_process,ffi -- --nocapture --test-threads=1
cargo test --workspace --locked --features aot,wasmedge_process,ffi -- --nocapture --test-threads=1 --skip test_vmbuilder
hydai marked this conversation as resolved.
Show resolved Hide resolved

- name: Test Rust SDK with async feature
run: |
Expand Down
Binary file added examples/wasmedge-sys/data/test_crypto.wasm
Binary file not shown.
156 changes: 118 additions & 38 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct VmBuilder {
config: Option<Config>,
stat: Option<Statistics>,
store: Option<Store>,
plugins: Vec<(String, String)>,
plugins: Vec<(String, Vec<String>)>,
#[cfg(all(feature = "async", target_os = "linux"))]
wasi_ctx: Option<WasiContext>,
}
Expand Down Expand Up @@ -61,31 +61,23 @@ impl VmBuilder {
}

/// Sets the `wasi_nn` plugin for the [Vm] to build. The `wasi_nn` plugin should be deployed with WasmEdge library.
pub fn with_plugin_wasi_nn(mut self) -> Self {
self.plugins.push(("wasi_nn".into(), "wasi_nn".into()));
self
pub fn with_plugin_wasi_nn(self) -> Self {
self.with_plugin("wasi_nn", None)
}

/// Sets the `wasi_crypto` plugin for the [Vm] to build. The `wasi_crypto` plugin should be deployed with WasmEdge library.
pub fn with_plugin_wasi_crypto(mut self) -> Self {
self.plugins
.push(("wasi_crypto".into(), "wasi_crypto_common".into()));
self.plugins
.push(("wasi_crypto".into(), "wasi_crypto_asymmetric_common".into()));
self.plugins
.push(("wasi_crypto".into(), "wasi_crypto_kx".into()));
self.plugins
.push(("wasi_crypto".into(), "wasi_crypto_signatures".into()));
self.plugins
.push(("wasi_crypto".into(), "wasi_crypto_symmetric".into()));
self
pub fn with_plugin_wasi_crypto(self) -> Self {
self.with_plugin("wasi_crypto", None)
}

/// Sets the `wasmedge_process` plugin for the [Vm] to build. The `wasmedge_process` plugin should be deployed with WasmEdge library.
pub fn with_plugin_wasmedge_process(mut self) -> Self {
self.plugins
.push(("wasmedge_process".into(), "wasmedge_process".into()));
self
pub fn with_plugin_wasmedge_process(self) -> Self {
self.with_plugin("wasmedge_process", None)
}

/// Sets the `rustls` plugin for the [Vm] to build. The `rustls` plugin should be deployed with WasmEdge library.
pub fn with_plugin_rustls(self) -> Self {
self.with_plugin("rustls", None)
}

/// Set the third-party plugin for the [Vm] to build.
Expand All @@ -94,10 +86,16 @@ impl VmBuilder {
///
/// * `pname` - The name of the plugin.
///
/// * `mname` - The name of the plugin module.
pub fn with_plugin(mut self, pname: impl AsRef<str>, mname: impl AsRef<str>) -> Self {
self.plugins
.push((pname.as_ref().into(), mname.as_ref().into()));
/// * `mnames` - The names of the plugin modules to be registered. If `None`, then all modules in the plugin are registered.
pub fn with_plugin(mut self, pname: impl AsRef<str>, mnames: Option<Vec<&str>>) -> Self {
match mnames {
Some(mod_names) => self.plugins.push((
pname.as_ref().into(),
mod_names.into_iter().map(|s| s.into()).collect(),
)),
None => self.plugins.push((pname.as_ref().into(), Vec::new())),
}

self
}

Expand Down Expand Up @@ -161,13 +159,30 @@ impl VmBuilder {
}

// * load and register plugin instances
for (pname, mname) in self.plugins.iter() {
let plugin_instance = Self::create_plugin_instance(pname, mname)?;
vm.plugin_host_instances.push(plugin_instance);
vm.store.register_plugin_module(
&mut vm.executor,
vm.plugin_host_instances.last().unwrap(),
)?;
for (pname, mnames) in self.plugins.iter() {
match mnames.is_empty() {
true => {
let plugin = PluginManager::find(pname)?;
for mname in plugin.mod_names().iter() {
let plugin_instance = plugin.mod_instance(mname)?;
vm.plugin_host_instances.push(plugin_instance);
vm.store.register_plugin_module(
&mut vm.executor,
vm.plugin_host_instances.last().unwrap(),
)?;
}
}
false => {
for mname in mnames {
let plugin_instance = Self::create_plugin_instance(pname, mname)?;
vm.plugin_host_instances.push(plugin_instance);
vm.store.register_plugin_module(
&mut vm.executor,
vm.plugin_host_instances.last().unwrap(),
)?;
}
}
}
}

Ok(vm)
Expand Down Expand Up @@ -226,13 +241,30 @@ impl VmBuilder {
}

// * load and register plugin instances
for (pname, mname) in self.plugins.iter() {
let plugin_instance = Self::create_plugin_instance(pname, mname)?;
vm.plugin_host_instances.push(plugin_instance);
vm.store.register_plugin_module(
&mut vm.executor,
vm.plugin_host_instances.last().unwrap(),
)?;
for (pname, mnames) in self.plugins.iter() {
match mnames.is_empty() {
true => {
let plugin = PluginManager::find(pname)?;
for mname in plugin.mod_names().iter() {
let plugin_instance = plugin.mod_instance(mname)?;
vm.plugin_host_instances.push(plugin_instance);
vm.store.register_plugin_module(
&mut vm.executor,
vm.plugin_host_instances.last().unwrap(),
)?;
}
}
false => {
for mname in mnames {
let plugin_instance = Self::create_plugin_instance(pname, mname)?;
vm.plugin_host_instances.push(plugin_instance);
vm.store.register_plugin_module(
&mut vm.executor,
vm.plugin_host_instances.last().unwrap(),
)?;
}
}
}
}

Ok(vm)
Expand Down Expand Up @@ -971,6 +1003,54 @@ mod tests {
Mutability, NeverType, RefType, Table, TableType, ValType, WasmValue,
};

#[cfg(target_os = "linux")]
#[test]
fn test_vmbuilder() -> Result<(), Box<dyn std::error::Error>> {
use crate::{
config::{CommonConfigOptions, ConfigBuilder, HostRegistrationConfigOptions},
params,
plugin::PluginManager,
VmBuilder,
};

// load plugins from the default plugin path
PluginManager::load(None)?;

PluginManager::names().iter().for_each(|name| {
println!("plugin name: {}", name);
});

let wasm_app_file = "examples/wasmedge-sys/data/test_crypto.wasm";

let config = ConfigBuilder::new(CommonConfigOptions::default())
.with_host_registration_config(HostRegistrationConfigOptions::default().wasi(true))
.build()?;
assert!(config.wasi_enabled());

let mut vm = VmBuilder::new()
.with_config(config)
// .with_plugin_wasi_crypto()
.with_plugin(
"wasi_crypto",
Some(vec![
"wasi_crypto_asymmetric_common",
"wasi_crypto_signatures",
"wasi_crypto_symmetric",
]),
)
// .with_plugin("wasi_crypto", None)
.build()?;

vm.wasi_module_mut()
.expect("Not found wasi module")
.initialize(None, None, None);

vm.register_module_from_file("wasm-app", &wasm_app_file)?
.run_func(Some("wasm-app"), "_start", params!())?;

Ok(())
}

#[test]
fn test_vm_run_func_from_file() {
// create a Vm context
Expand Down