1
0
mirror of https://github.com/containers/youki synced 2024-09-25 13:40:41 +02:00

Merge pull request #1320 from apepkuss/feat-wasmedge-executor

[libcontainer] Integrate WasmEdge Runtime
This commit is contained in:
Toru Komatsu 2022-11-30 15:00:08 +09:00 committed by GitHub
commit 997bc43839
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 280 additions and 6 deletions

View File

@ -67,7 +67,9 @@ jobs:
- run: sudo apt-get -y update
- run: sudo apt-get install -y pkg-config libsystemd-dev libdbus-glib-1-dev libelf-dev libseccomp-dev
- name: Run tests
run: cd ./crates && cargo test --all --all-features --no-fail-fast
run: |
export LD_LIBRARY_PATH=$HOME/.wasmedge/lib
cd ./crates && cargo test --all --all-features --no-fail-fast
coverage:
runs-on: ubuntu-latest
timeout-minutes: 15

155
Cargo.lock generated
View File

@ -96,6 +96,26 @@ version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cbbc9d0964165b47557570cce6c952866c2678457aca742aafc9fb771d30270"
[[package]]
name = "bindgen"
version = "0.61.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a022e58a142a46fea340d68012b9201c094e93ec3d033a944a24f8fd4a4f09a"
dependencies = [
"bitflags",
"cexpr",
"clang-sys",
"lazy_static",
"lazycell",
"peeking_take_while",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex",
"syn",
]
[[package]]
name = "bitflags"
version = "1.3.2"
@ -154,6 +174,15 @@ dependencies = [
"jobserver",
]
[[package]]
name = "cexpr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
dependencies = [
"nom",
]
[[package]]
name = "cfg-if"
version = "0.1.10"
@ -182,6 +211,17 @@ dependencies = [
"winapi",
]
[[package]]
name = "clang-sys"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa2e27ae6ab525c3d369ded447057bca5438d86dc3a68f6faafb8269ba82ebf3"
dependencies = [
"glob",
"libc",
"libloading",
]
[[package]]
name = "clap"
version = "3.2.23"
@ -243,6 +283,15 @@ dependencies = [
"os_str_bytes",
]
[[package]]
name = "cmake"
version = "0.1.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a"
dependencies = [
"cc",
]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
@ -905,6 +954,12 @@ dependencies = [
"url",
]
[[package]]
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]]
name = "hashbrown"
version = "0.11.2"
@ -1122,6 +1177,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "leb128"
version = "0.2.5"
@ -1196,6 +1257,7 @@ dependencies = [
"serde_json",
"serial_test",
"syscalls",
"wasmedge-sdk",
"wasmer",
"wasmer-wasi",
]
@ -1371,6 +1433,12 @@ dependencies = [
"autocfg",
]
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "miniz_oxide"
version = "0.5.4"
@ -1454,6 +1522,16 @@ version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43794a0ace135be66a25d3ae77d41b91615fb68ae937f904090203e81f755b65"
[[package]]
name = "nom"
version = "7.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "normalize-line-endings"
version = "0.3.0"
@ -1469,6 +1547,17 @@ dependencies = [
"winapi",
]
[[package]]
name = "num-derive"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "num-integer"
version = "0.1.45"
@ -1577,12 +1666,24 @@ dependencies = [
"windows-sys 0.42.0",
]
[[package]]
name = "paste"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1de2e551fb905ac83f73f7aedf2f0cb4a0da7e35efa24a202a936269f1f18e1"
[[package]]
name = "path-clean"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecba01bf2678719532c5e3059e0b5f0811273d94b397088b82e3bd0a78c78fdd"
[[package]]
name = "peeking_take_while"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]]
name = "pentacle"
version = "1.0.0"
@ -2192,6 +2293,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012"
[[package]]
name = "shlex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
[[package]]
name = "slab"
version = "0.4.7"
@ -2659,6 +2766,54 @@ dependencies = [
"leb128",
]
[[package]]
name = "wasmedge-macro"
version = "0.3.0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "wasmedge-sdk"
version = "0.7.1"
dependencies = [
"anyhow",
"num-derive",
"num-traits",
"thiserror",
"wasmedge-macro",
"wasmedge-sys",
"wasmedge-types",
"wat",
]
[[package]]
name = "wasmedge-sys"
version = "0.12.1"
dependencies = [
"bindgen",
"cmake",
"lazy_static",
"libc",
"parking_lot",
"paste",
"rand",
"thiserror",
"wasmedge-macro",
"wasmedge-types",
"wat",
]
[[package]]
name = "wasmedge-types"
version = "0.3.1"
dependencies = [
"thiserror",
"wat",
]
[[package]]
name = "wasmer"
version = "2.3.0"

View File

@ -18,6 +18,7 @@ systemd = ["libcgroups/systemd", "v2"]
v2 = ["libcgroups/v2"]
v1 = ["libcgroups/v1"]
cgroupsv2_devices = ["libcgroups/cgroupsv2_devices"]
wasm-wasmedge = ["wasmedge-sdk/standalone"]
[dependencies]
anyhow = "1.0"
@ -43,6 +44,7 @@ syscalls = "0.6.7"
rust-criu = "0.2.0"
wasmer = { version = "2.2.0", optional = true }
wasmer-wasi = { version = "2.3.0", optional = true }
wasmedge-sdk = { version = "0.7.1", optional = true }
[dev-dependencies]
oci-spec = { version = "0.5.8", features = ["proptests", "runtime"] }

View File

@ -2,10 +2,14 @@ use anyhow::{Context, Result};
use oci_spec::runtime::Spec;
use self::default::DefaultExecutor;
#[cfg(feature = "wasm-wasmedge")]
use self::wasmedge::WasmEdgeExecutor;
#[cfg(feature = "wasm-wasmer")]
use self::wasmer::WasmerExecutor;
pub mod default;
#[cfg(feature = "wasm-wasmedge")]
pub mod wasmedge;
#[cfg(feature = "wasm-wasmer")]
pub mod wasmer;
@ -28,6 +32,11 @@ impl ExecutorManager {
return WasmerExecutor::exec(spec).context("wasmer execution failed");
}
#[cfg(feature = "wasm-wasmedge")]
if WasmEdgeExecutor::can_handle(spec)? {
return WasmEdgeExecutor::exec(spec).context("wasmedge execution failed");
}
DefaultExecutor::exec(spec).context("default execution failed")
}
}

View File

@ -0,0 +1,90 @@
use anyhow::Result;
use oci_spec::runtime::Spec;
use wasmedge_sdk::{
config::{CommonConfigOptions, ConfigBuilder, HostRegistrationConfigOptions},
params, Vm,
};
use super::Executor;
const EXECUTOR_NAME: &str = "wasmedge";
pub struct WasmEdgeExecutor {}
impl Executor for WasmEdgeExecutor {
fn exec(spec: &Spec) -> Result<()> {
// parse wasi parameters
let args = get_args(spec);
let mut cmd = args[0].clone();
if let Some(stripped) = args[0].strip_prefix(std::path::MAIN_SEPARATOR) {
cmd = stripped.to_string();
}
let envs = env_to_wasi(spec);
// create configuration with `wasi` option enabled
let config = ConfigBuilder::new(CommonConfigOptions::default())
.with_host_registration_config(HostRegistrationConfigOptions::default().wasi(true))
.build()?;
// create a vm with the config settings
let mut vm = Vm::new(Some(config))?;
// initialize the wasi module with the parsed parameters
let mut wasi_instance = vm.wasi_module()?;
wasi_instance.initialize(
Some(args.iter().map(|s| s as &str).collect()),
Some(envs.iter().map(|s| s as &str).collect()),
None,
);
let mut vm = vm.register_module_from_file("main", &cmd)?;
let ins = vm.named_module("main")?;
ins.func("_start")
.expect("Not found '_start' func in the 'main' module instance")
.call(&mut vm, params!())?;
Ok(())
}
fn can_handle(spec: &Spec) -> Result<bool> {
if let Some(annotations) = spec.annotations() {
if let Some(handler) = annotations.get("run.oci.handler") {
return Ok(handler == "wasm");
}
if let Some(variant) = annotations.get("module.wasm.image/variant") {
return Ok(variant == "compat");
}
}
Ok(false)
}
fn name() -> &'static str {
EXECUTOR_NAME
}
}
fn get_args(spec: &Spec) -> &[String] {
let p = match spec.process() {
None => return &[],
Some(p) => p,
};
match p.args() {
None => &[],
Some(args) => args.as_slice(),
}
}
fn env_to_wasi(spec: &Spec) -> Vec<String> {
let default = vec![];
let env = spec
.process()
.as_ref()
.unwrap()
.env()
.as_ref()
.unwrap_or(&default);
env.to_vec()
}

View File

@ -6,13 +6,29 @@ There are 3 things you need to do to run a WebAssembly module with youki.
2. Build a container image with the WebAssembly module
3. Run the container with youki
## Build youki with wasm-wasmer feature flag enabled
## Build youki with `wasm-wasmedge` or `wasm-wasmer` feature flag enabled
Run `build.sh` with `-f wasm-wasmer` option.
- Run `build.sh` with `-f wasm-wasmedge` option.
```console
./script/build.sh -o -r . -f wasm-wasmer
```
```console
./script/build.sh -o -r . -f wasm-wasmedge
```
> The `wasm-wasmedge` feature will install WasmEdge Runtime library in the `$HOME/.wasmedge` directory.
> To make the library avaible in your system, run the following command:
> ```bash
> export LD_LIBRARY_PATH=$HOME/.wasmedge/lib
> ```
> or
> ```bash
> source $HOME/.wasmedge/env
> ```
>
- Run `build.sh` with `-f wasm-wasmer` option.
```console
./script/build.sh -o -r . -f wasm-wasmer
```
## Build a container image with the WebAssembly module