-
Notifications
You must be signed in to change notification settings - Fork 95
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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" |
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, | ||||||
Check warning on line 13 in crates/containerd-shim-wamr/src/instance.rs GitHub Actions / wamr / build for linux-gnu
Check warning on line 13 in crates/containerd-shim-wamr/src/instance.rs GitHub Actions / wamr / build for linux-gnu
Check warning on line 13 in crates/containerd-shim-wamr/src/instance.rs GitHub Actions / wamr / build for linux-musl
|
||||||
} | ||||||
|
||||||
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(); | ||||||
Check warning on line 41 in crates/containerd-shim-wamr/src/instance.rs GitHub Actions / wamr / build for linux-gnu
Check warning on line 41 in crates/containerd-shim-wamr/src/instance.rs GitHub Actions / wamr / build for linux-gnu
Check warning on line 41 in crates/containerd-shim-wamr/src/instance.rs GitHub Actions / wamr / build for linux-musl
|
||||||
let envs: Vec<_> = std::env::vars().map(|(k, v)| format!("{k}={v}")).collect(); | ||||||
let Entrypoint { | ||||||
source, | ||||||
func, | ||||||
arg0: _, | ||||||
name, | ||||||
Check warning on line 47 in crates/containerd-shim-wamr/src/instance.rs GitHub Actions / wamr / build for linux-gnu
Check warning on line 47 in crates/containerd-shim-wamr/src/instance.rs GitHub Actions / wamr / build for linux-gnu
Check warning on line 47 in crates/containerd-shim-wamr/src/instance.rs GitHub Actions / wamr / build for linux-musl
|
||||||
} = 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("/")]); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still have problems when
|
||||||
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` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am thinking about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah i believe that when the variable There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. about that, bytecodealliance/wamr-rust-sdk#8 |
||||||
|
||||||
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) | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod instance; | ||
|
||
pub use instance::WamrInstance; |
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// TODO: add tests |
There was a problem hiding this comment.
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