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

feat(wamr): add wasm-mico-runtime shim implementation #508

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 48 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
[workspace]
members = [
"crates/containerd-shim-wasm",
"crates/containerd-shim-wasm-test-modules",
"crates/wasi-demo-app",
"crates/oci-tar-builder",
"crates/containerd-shim-wasmedge",
"crates/containerd-shim-wasmtime",
"crates/containerd-shim-wasmer"
"crates/*",
]
resolver = "2"

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add wamr to CI

CONTAINERD_NAMESPACE ?= default

# We have a bit of fancy logic here to determine the target
Expand Down
24 changes: 24 additions & 0 deletions crates/containerd-shim-wamr/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[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"] }
ttrpc = { workspace = true }
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"
90 changes: 90 additions & 0 deletions crates/containerd-shim-wamr/src/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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;

pub type WamrInstance = Instance<WamrEngine>;

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 {
let runtime = Runtime::new().unwrap();
Self { runtime }
}
}

impl Clone for WamrEngine {
fn clone(&self) -> Self {
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<i32> {
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::info!("Create a WamrInstance");

// TODO: error handling isn't ideal

let mut module = Module::from_buf(&wasm_bytes).map_err(|e| {
anyhow::Error::msg(format!("Failed to create module from bytes: {:?}", e))
})?;

module.set_wasi_arg_pre_open_path(vec![String::from("/")], vec![String::from("/")]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module.set_wasi_arg_pre_open_path(vec![String::from("/")], vec![String::from("/")]);
module.set_wasi_arg_pre_open_path(vec![String::from("/")], vec![]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still have problems when function.call(). need further investigation. But blocked by above setup issues. I was hoping to check

  • if module is unloaded or released?
  • if there anything unexpected unloaded or released?

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?

let instance = WamrInstnace::new(&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`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking about Self { runtime } may has been destroyed. It happened several times in my test cases when implementing rust-sdk. Is there a quick way we can confirm that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i believe that when the variable let runtime = Runtime::new() goes out of scope, the Drop() will be invoked to destruct it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's probably why I favor the design of a binding the runtime to the variable and pass that to instances like wasmtime does.

    let engine = Engine::default();
    let module = Module::new(&engine, wat)?;
    let mut store = Store::new(&engine, 4);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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)))?;
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)
}
}
3 changes: 3 additions & 0 deletions crates/containerd-shim-wamr/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod instance;

pub use instance::WamrInstance;
6 changes: 6 additions & 0 deletions crates/containerd-shim-wamr/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use containerd_shim_wamr::WamrInstance;
use containerd_shim_wasm::sandbox::cli::{revision, shim_main, version};

fn main() {
shim_main::<WamrInstance>("wamr", version!(), revision!(), "v1", None);
}
1 change: 1 addition & 0 deletions crates/containerd-shim-wamr/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO: add tests
Loading