From 0e061f3dc70a8ce3fd5811bff8e1ab9030a05fa3 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Fri, 20 Sep 2024 01:05:01 +0000 Subject: [PATCH 1/8] wasmtime: tidy up imports Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wasmtime/src/instance.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/containerd-shim-wasmtime/src/instance.rs b/crates/containerd-shim-wasmtime/src/instance.rs index cf4e89929..60473e03a 100644 --- a/crates/containerd-shim-wasmtime/src/instance.rs +++ b/crates/containerd-shim-wasmtime/src/instance.rs @@ -7,7 +7,7 @@ use containerd_shim_wasm::container::{ Engine, Entrypoint, Instance, RuntimeContext, Stdio, WasmBinaryType, }; use containerd_shim_wasm::sandbox::WasmLayer; -use wasmtime::component::{self as wasmtime_component, Component, ResourceTable}; +use wasmtime::component::{self, Component, ResourceTable}; use wasmtime::{Config, Module, Precompiled, Store}; use wasmtime_wasi::preview1::{self as wasi_preview1}; use wasmtime_wasi::{self as wasi_preview2}; @@ -186,7 +186,7 @@ impl WasmtimeEngine Result, anyhow::Error> { log::debug!("loading wasm component"); - let mut linker = wasmtime_component::Linker::new(&self.engine); + let mut linker = component::Linker::new(&self.engine); log::debug!("init linker"); wasi_preview2::add_to_linker_async(&mut linker)?; From 73d960016f8847abe32121de8dfc8b97a07fc3d4 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Fri, 20 Sep 2024 01:07:19 +0000 Subject: [PATCH 2/8] wasmtime: simplify trait bound for WasmtimeEngine Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wasmtime/src/instance.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/containerd-shim-wasmtime/src/instance.rs b/crates/containerd-shim-wasmtime/src/instance.rs index 60473e03a..6add5950f 100644 --- a/crates/containerd-shim-wasmtime/src/instance.rs +++ b/crates/containerd-shim-wasmtime/src/instance.rs @@ -133,7 +133,10 @@ impl Engine for WasmtimeEngine { } } -impl WasmtimeEngine { +impl WasmtimeEngine +where + T: std::clone::Clone + Sync + WasiConfig + Send + 'static, +{ /// Execute a wasm module. /// /// This function adds wasi_preview1 to the linker and can be utilized From 6a3df739fbe0708e74ed5e8bf225f05d49312384 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Fri, 20 Sep 2024 01:12:58 +0000 Subject: [PATCH 3/8] wasmtime: rename func to wasm_func_name Signed-off-by: jiaxiao zhou --- .../containerd-shim-wasmtime/src/instance.rs | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/crates/containerd-shim-wasmtime/src/instance.rs b/crates/containerd-shim-wasmtime/src/instance.rs index 6add5950f..635687e45 100644 --- a/crates/containerd-shim-wasmtime/src/instance.rs +++ b/crates/containerd-shim-wasmtime/src/instance.rs @@ -84,7 +84,7 @@ impl Engine for WasmtimeEngine { let Entrypoint { source, - func, + func: wasm_func_name, arg0: _, name: _, } = ctx.entrypoint(); @@ -95,7 +95,7 @@ impl Engine for WasmtimeEngine { let wasm_bytes = &source.as_bytes()?; - let status = self.execute(wasm_bytes, store, func, stdio)?; + let status = self.execute(wasm_bytes, store, wasm_func_name, stdio)?; let status = status.map(|_| 0).or_else(|err| { match err.downcast_ref::() { @@ -145,7 +145,7 @@ where &self, module: Module, mut store: Store, - func: &String, + wasm_func_name: &String, stdio: Stdio, ) -> Result, anyhow::Error> { log::debug!("execute module"); @@ -164,10 +164,10 @@ where log::info!("getting start function"); let start_func = instance - .get_func(&mut store, func) + .get_func(&mut store, wasm_func_name) .context("module does not have a WASI start function")?; - log::debug!("running start function {func:?}"); + log::debug!("running start function {wasm_func_name:?}"); stdio.redirect()?; @@ -184,7 +184,7 @@ where &self, component: Component, mut store: Store, - func: String, + wasm_func_name: String, stdio: Stdio, ) -> Result, anyhow::Error> { log::debug!("loading wasm component"); @@ -201,7 +201,7 @@ where // // TODO: think about a better way to do this. wasmtime_wasi::runtime::in_tokio(async move { - if func == "_start" { + if wasm_func_name == "_start" { let pre = linker.instantiate_pre(&component)?; let (command, _instance) = wasi_preview2::bindings::Command::instantiate_pre(&mut store, &pre).await?; @@ -224,12 +224,15 @@ where let instance = pre.instantiate_async(&mut store).await?; - log::info!("getting component exported function {func:?}"); - let start_func = instance.get_func(&mut store, &func).context(format!( - "component does not have exported function {func:?}" - ))?; + log::info!("getting component exported function {wasm_func_name:?}"); + let start_func = + instance + .get_func(&mut store, &wasm_func_name) + .context(format!( + "component does not have exported function {wasm_func_name:?}" + ))?; - log::debug!("running exported function {func:?} {start_func:?}"); + log::debug!("running exported function {wasm_func_name:?} {start_func:?}"); stdio.redirect()?; @@ -243,29 +246,29 @@ where &self, wasm_binary: &[u8], store: Store, - func: String, + wasm_func_name: String, stdio: Stdio, ) -> Result, anyhow::Error> { match WasmBinaryType::from_bytes(wasm_binary) { Some(WasmBinaryType::Module) => { log::debug!("loading wasm module"); let module = Module::from_binary(&self.engine, wasm_binary)?; - self.execute_module(module, store, &func, stdio) + self.execute_module(module, store, &wasm_func_name, stdio) } Some(WasmBinaryType::Component) => { let component = Component::from_binary(&self.engine, wasm_binary)?; - self.execute_component(component, store, func, stdio) + self.execute_component(component, store, wasm_func_name, stdio) } None => match &self.engine.detect_precompiled(wasm_binary) { Some(Precompiled::Module) => { log::info!("using precompiled module"); let module = unsafe { Module::deserialize(&self.engine, wasm_binary) }?; - self.execute_module(module, store, &func, stdio) + self.execute_module(module, store, &wasm_func_name, stdio) } Some(Precompiled::Component) => { log::info!("using precompiled component"); let component = unsafe { Component::deserialize(&self.engine, wasm_binary) }?; - self.execute_component(component, store, func, stdio) + self.execute_component(component, store, wasm_func_name, stdio) } None => { bail!("invalid precompiled module") From 9d0b724b031157849550bc40351f5703c01b59d5 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Fri, 20 Sep 2024 01:14:08 +0000 Subject: [PATCH 4/8] wasmtime: tidy up the linker name Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wasmtime/src/instance.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/containerd-shim-wasmtime/src/instance.rs b/crates/containerd-shim-wasmtime/src/instance.rs index 635687e45..f6fe9f945 100644 --- a/crates/containerd-shim-wasmtime/src/instance.rs +++ b/crates/containerd-shim-wasmtime/src/instance.rs @@ -153,8 +153,8 @@ where let mut module_linker = wasmtime::Linker::new(&self.engine); log::debug!("init linker"); - wasi_preview1::add_to_linker_async(&mut module_linker, |s: &mut WasiCtx| { - &mut s.wasi_preview1 + wasi_preview1::add_to_linker_async(&mut module_linker, |wasi_ctx: &mut WasiCtx| { + &mut wasi_ctx.wasi_preview1 })?; wasmtime_wasi::runtime::in_tokio(async move { @@ -193,7 +193,6 @@ where log::debug!("init linker"); wasi_preview2::add_to_linker_async(&mut linker)?; - log::debug!("done init linker"); log::info!("instantiating component"); From 35811bb9ef3c933a517757177c9ef1815539ee02 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Fri, 20 Sep 2024 01:20:16 +0000 Subject: [PATCH 5/8] wasmtime: de-duplicate builder in prepare_wasi_ctx() Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wasmtime/src/instance.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/containerd-shim-wasmtime/src/instance.rs b/crates/containerd-shim-wasmtime/src/instance.rs index f6fe9f945..f258cf876 100644 --- a/crates/containerd-shim-wasmtime/src/instance.rs +++ b/crates/containerd-shim-wasmtime/src/instance.rs @@ -282,11 +282,10 @@ fn prepare_wasi_ctx( ctx: &impl RuntimeContext, envs: &[(String, String)], ) -> Result { - let mut wasi_preview1_builder = wasi_builder(ctx, envs)?; - let wasi_preview1_ctx = wasi_preview1_builder.build_p1(); + let mut wasi_builder = wasi_builder(ctx, envs)?; + let wasi_preview1_ctx = wasi_builder.build_p1(); + let wasi_preview2_ctx = wasi_builder.build(); - let mut wasi_preview2_builder = wasi_builder(ctx, envs)?; - let wasi_preview2_ctx = wasi_preview2_builder.build(); let wasi_data = WasiCtx { wasi_preview1: wasi_preview1_ctx, wasi_preview2: wasi_preview2_ctx, From 518598897a5936d58a965d4080ee137bb46299f2 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Fri, 20 Sep 2024 01:30:05 +0000 Subject: [PATCH 6/8] wasmtime: extract linker instantiation out from the if statement Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wasmtime/src/instance.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/crates/containerd-shim-wasmtime/src/instance.rs b/crates/containerd-shim-wasmtime/src/instance.rs index f258cf876..065c2c0d7 100644 --- a/crates/containerd-shim-wasmtime/src/instance.rs +++ b/crates/containerd-shim-wasmtime/src/instance.rs @@ -200,8 +200,8 @@ where // // TODO: think about a better way to do this. wasmtime_wasi::runtime::in_tokio(async move { + let pre = linker.instantiate_pre(&component)?; if wasm_func_name == "_start" { - let pre = linker.instantiate_pre(&component)?; let (command, _instance) = wasi_preview2::bindings::Command::instantiate_pre(&mut store, &pre).await?; @@ -219,8 +219,6 @@ where Ok(status) } else { - let pre = linker.instantiate_pre(&component)?; - let instance = pre.instantiate_async(&mut store).await?; log::info!("getting component exported function {wasm_func_name:?}"); @@ -285,7 +283,6 @@ fn prepare_wasi_ctx( let mut wasi_builder = wasi_builder(ctx, envs)?; let wasi_preview1_ctx = wasi_builder.build_p1(); let wasi_preview2_ctx = wasi_builder.build(); - let wasi_data = WasiCtx { wasi_preview1: wasi_preview1_ctx, wasi_preview2: wasi_preview2_ctx, From d80c34dda48ee5a0d5b24df0a015b59f2538871a Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Sun, 29 Sep 2024 17:40:42 +0000 Subject: [PATCH 7/8] containerd-shim-wasmtime: revert the changes to prepare_wasi_ctx() due to the fact that wasip1 and p2 should have their isolated context. Signed-off-by: jiaxiao zhou --- crates/containerd-shim-wasmtime/src/instance.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/crates/containerd-shim-wasmtime/src/instance.rs b/crates/containerd-shim-wasmtime/src/instance.rs index 065c2c0d7..22d93966c 100644 --- a/crates/containerd-shim-wasmtime/src/instance.rs +++ b/crates/containerd-shim-wasmtime/src/instance.rs @@ -280,12 +280,9 @@ fn prepare_wasi_ctx( ctx: &impl RuntimeContext, envs: &[(String, String)], ) -> Result { - let mut wasi_builder = wasi_builder(ctx, envs)?; - let wasi_preview1_ctx = wasi_builder.build_p1(); - let wasi_preview2_ctx = wasi_builder.build(); let wasi_data = WasiCtx { - wasi_preview1: wasi_preview1_ctx, - wasi_preview2: wasi_preview2_ctx, + wasi_preview1: wasi_builder(ctx, envs)?.build_p1(), + wasi_preview2: wasi_builder(ctx, envs)?.build(), resource_table: ResourceTable::default(), }; Ok(wasi_data) From 00cfb4400915e45f5daef32ac55414e9cd555a91 Mon Sep 17 00:00:00 2001 From: jiaxiao zhou Date: Sun, 29 Sep 2024 17:42:58 +0000 Subject: [PATCH 8/8] containerd-shim-wasmtime: revert wasm_func_name to func Signed-off-by: jiaxiao zhou --- .../containerd-shim-wasmtime/src/instance.rs | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/crates/containerd-shim-wasmtime/src/instance.rs b/crates/containerd-shim-wasmtime/src/instance.rs index 22d93966c..62f99051e 100644 --- a/crates/containerd-shim-wasmtime/src/instance.rs +++ b/crates/containerd-shim-wasmtime/src/instance.rs @@ -84,7 +84,7 @@ impl Engine for WasmtimeEngine { let Entrypoint { source, - func: wasm_func_name, + func, arg0: _, name: _, } = ctx.entrypoint(); @@ -95,7 +95,7 @@ impl Engine for WasmtimeEngine { let wasm_bytes = &source.as_bytes()?; - let status = self.execute(wasm_bytes, store, wasm_func_name, stdio)?; + let status = self.execute(wasm_bytes, store, func, stdio)?; let status = status.map(|_| 0).or_else(|err| { match err.downcast_ref::() { @@ -145,7 +145,7 @@ where &self, module: Module, mut store: Store, - wasm_func_name: &String, + func: &String, stdio: Stdio, ) -> Result, anyhow::Error> { log::debug!("execute module"); @@ -164,10 +164,10 @@ where log::info!("getting start function"); let start_func = instance - .get_func(&mut store, wasm_func_name) + .get_func(&mut store, func) .context("module does not have a WASI start function")?; - log::debug!("running start function {wasm_func_name:?}"); + log::debug!("running start function {func:?}"); stdio.redirect()?; @@ -184,7 +184,7 @@ where &self, component: Component, mut store: Store, - wasm_func_name: String, + func: String, stdio: Stdio, ) -> Result, anyhow::Error> { log::debug!("loading wasm component"); @@ -201,7 +201,7 @@ where // TODO: think about a better way to do this. wasmtime_wasi::runtime::in_tokio(async move { let pre = linker.instantiate_pre(&component)?; - if wasm_func_name == "_start" { + if func == "_start" { let (command, _instance) = wasi_preview2::bindings::Command::instantiate_pre(&mut store, &pre).await?; @@ -221,15 +221,12 @@ where } else { let instance = pre.instantiate_async(&mut store).await?; - log::info!("getting component exported function {wasm_func_name:?}"); - let start_func = - instance - .get_func(&mut store, &wasm_func_name) - .context(format!( - "component does not have exported function {wasm_func_name:?}" - ))?; + log::info!("getting component exported function {func:?}"); + let start_func = instance.get_func(&mut store, &func).context(format!( + "component does not have exported function {func:?}" + ))?; - log::debug!("running exported function {wasm_func_name:?} {start_func:?}"); + log::debug!("running exported function {func:?} {start_func:?}"); stdio.redirect()?; @@ -243,29 +240,29 @@ where &self, wasm_binary: &[u8], store: Store, - wasm_func_name: String, + func: String, stdio: Stdio, ) -> Result, anyhow::Error> { match WasmBinaryType::from_bytes(wasm_binary) { Some(WasmBinaryType::Module) => { log::debug!("loading wasm module"); let module = Module::from_binary(&self.engine, wasm_binary)?; - self.execute_module(module, store, &wasm_func_name, stdio) + self.execute_module(module, store, &func, stdio) } Some(WasmBinaryType::Component) => { let component = Component::from_binary(&self.engine, wasm_binary)?; - self.execute_component(component, store, wasm_func_name, stdio) + self.execute_component(component, store, func, stdio) } None => match &self.engine.detect_precompiled(wasm_binary) { Some(Precompiled::Module) => { log::info!("using precompiled module"); let module = unsafe { Module::deserialize(&self.engine, wasm_binary) }?; - self.execute_module(module, store, &wasm_func_name, stdio) + self.execute_module(module, store, &func, stdio) } Some(Precompiled::Component) => { log::info!("using precompiled component"); let component = unsafe { Component::deserialize(&self.engine, wasm_binary) }?; - self.execute_component(component, store, wasm_func_name, stdio) + self.execute_component(component, store, func, stdio) } None => { bail!("invalid precompiled module")