-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #716 from Mossaka/wamr-2
- Loading branch information
Showing
12 changed files
with
321 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[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-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] | ||
containerd-shim-wasm = { workspace = true, features = ["testing"] } | ||
serial_test = { workspace = true } | ||
|
||
[[bin]] | ||
name = "containerd-shim-wamr-v1" | ||
path = "src/main.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
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 WamrInst; | ||
use wamr_rust_sdk::module::Module; | ||
use wamr_rust_sdk::runtime::Runtime; | ||
use wamr_rust_sdk::wasi_context::WasiCtxBuilder; | ||
|
||
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 = ctx.envs(); | ||
let Entrypoint { | ||
source, func, name, .. | ||
} = ctx.entrypoint(); | ||
|
||
let wasm_bytes = source | ||
.as_bytes() | ||
.context("Failed to get bytes from source")?; | ||
|
||
log::info!("Create a WAMR module"); | ||
|
||
// TODO: error handling isn't ideal | ||
|
||
let mod_name = name.unwrap_or_else(|| "main".to_string()); | ||
|
||
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"); | ||
|
||
let wasi_ctx = WasiCtxBuilder::new() | ||
.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 register a named module with bytes? | ||
|
||
log::info!("Create a WAMR instance"); | ||
|
||
let instance = WamrInst::new(&self.runtime, &module, 1024 * 64) | ||
.context("Failed to create instance")?; | ||
|
||
log::info!("redirect stdio"); | ||
stdio.redirect()?; | ||
|
||
log::info!("Running {func:?}"); | ||
let function = | ||
Function::find_export_func(&instance, &func).context("Failed to find function")?; | ||
let status = function | ||
.call(&instance, &vec![]) | ||
.map(|_| 0) | ||
.map_err(|err| { | ||
log::error!("Error: {:?}", err); | ||
err | ||
}) | ||
.context("Failed to call function")?; | ||
|
||
Ok(status) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#[cfg(unix)] | ||
pub mod instance; | ||
|
||
#[cfg(unix)] | ||
pub use instance::WamrInstance; | ||
|
||
#[cfg(unix)] | ||
#[cfg(test)] | ||
#[path = "tests.rs"] | ||
mod wamr_tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#[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() { | ||
panic!("WAMR shim is not supported on Windows"); | ||
} | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
fn main() { | ||
shim_main::<WamrInstance>("wamr", version!(), revision!(), "v1", None); | ||
} |
Oops, something went wrong.