From b6e2ab34377f16a7bd02df30d64e558257bcba78 Mon Sep 17 00:00:00 2001 From: Arshia Ghafoori Date: Mon, 23 Sep 2024 19:49:54 +0400 Subject: [PATCH 1/2] Fix --entrypoint not working for WASI(X) modules --- lib/cli/src/commands/run/mod.rs | 4 ++++ lib/wasix/src/runners/wasi.rs | 14 ++++++++++++++ lib/wasix/src/runners/wasi_common.rs | 5 +++++ lib/wasix/src/state/builder.rs | 24 +++++++++++++++++++++++- 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/cli/src/commands/run/mod.rs b/lib/cli/src/commands/run/mod.rs index eefc629f72a..9a414c5a58d 100644 --- a/lib/cli/src/commands/run/mod.rs +++ b/lib/cli/src/commands/run/mod.rs @@ -425,6 +425,10 @@ impl Run { .with_forward_host_env(self.wasi.forward_host_env) .with_capabilities(self.wasi.capabilities()); + if let Some(ref entrypoint) = self.entrypoint { + runner.with_entrypoint(entrypoint); + } + #[cfg(feature = "journal")] { for trigger in self.wasi.snapshot_on.iter().cloned() { diff --git a/lib/wasix/src/runners/wasi.rs b/lib/wasix/src/runners/wasi.rs index 7713b80f6d2..23d3f2eb6b7 100644 --- a/lib/wasix/src/runners/wasi.rs +++ b/lib/wasix/src/runners/wasi.rs @@ -34,6 +34,20 @@ impl WasiRunner { WasiRunner::default() } + /// Returns the current entrypoint for this `WasiRunner` + pub fn entrypoint(&self) -> Option { + self.wasi.entrypoint.clone() + } + + /// Builder method to set the name of the entrypoint function for this `WasiRunner` + pub fn with_entrypoint(&mut self, entrypoint: S) -> &mut Self + where + S: Into, + { + self.wasi.entrypoint = Some(entrypoint.into()); + self + } + /// Returns the current arguments for this `WasiRunner` pub fn get_args(&self) -> Vec { self.wasi.args.clone() diff --git a/lib/wasix/src/runners/wasi_common.rs b/lib/wasix/src/runners/wasi_common.rs index 82b2135f98a..f265d275911 100644 --- a/lib/wasix/src/runners/wasi_common.rs +++ b/lib/wasix/src/runners/wasi_common.rs @@ -32,6 +32,7 @@ pub struct MappedCommand { #[derive(Derivative, Default, Clone)] #[derivative(Debug)] pub(crate) struct CommonWasiOptions { + pub(crate) entrypoint: Option, pub(crate) args: Vec, pub(crate) env: HashMap, pub(crate) forward_host_env: bool, @@ -57,6 +58,10 @@ impl CommonWasiOptions { wasi: &WasiAnnotation, root_fs: Option, ) -> Result<(), anyhow::Error> { + if let Some(ref entrypoint) = self.entrypoint { + builder.set_entrypoint(entrypoint); + } + let root_fs = root_fs.unwrap_or_else(|| { RootFileSystemBuilder::default() .with_tmp(!self.is_tmp_mapped) diff --git a/lib/wasix/src/state/builder.rs b/lib/wasix/src/state/builder.rs index 9d678205dd0..5a2f62091c3 100644 --- a/lib/wasix/src/state/builder.rs +++ b/lib/wasix/src/state/builder.rs @@ -49,6 +49,8 @@ use super::env::WasiEnvInit; /// ``` #[derive(Default)] pub struct WasiEnvBuilder { + /// Name of entrypoint function. Defaults to running `_start` if not specified. + pub(super) entrypoint: Option, /// Command line arguments. pub(super) args: Vec, /// Environment variables. @@ -97,6 +99,7 @@ impl std::fmt::Debug for WasiEnvBuilder { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // TODO: update this when stable f.debug_struct("WasiEnvBuilder") + .field("entrypoint", &self.entrypoint) .field("args", &self.args) .field("envs", &self.envs) .field("preopens", &self.preopens) @@ -240,6 +243,21 @@ impl WasiEnvBuilder { &mut self.envs } + pub fn entrypoint(mut self, entrypoint: S) -> Self + where + S: AsRef, + { + self.set_entrypoint(entrypoint); + self + } + + pub fn set_entrypoint(&mut self, entrypoint: S) + where + S: AsRef, + { + self.entrypoint = Some(entrypoint.as_ref().to_owned()); + } + /// Add an argument. /// /// Arguments must not contain the nul (0x0) byte @@ -1024,6 +1042,8 @@ impl WasiEnvBuilder { ); } + let entrypoint = self.entrypoint.clone(); + let (instance, env) = self.instantiate_ext(module, module_hash, store)?; // Bootstrap the process @@ -1036,7 +1056,9 @@ impl WasiEnvBuilder { .map_err(|exit| WasiRuntimeError::Wasi(WasiError::Exit(exit)))?; } - let start = instance.exports.get_function("_start")?; + let start = instance + .exports + .get_function(entrypoint.as_deref().unwrap_or("_start"))?; env.data(&store).thread.set_status_running(); let result = crate::run_wasi_func_start(start, store); From 21787bbe34e038e746951f6a756964e41f8f8822 Mon Sep 17 00:00:00 2001 From: Arshia Ghafoori Date: Tue, 24 Sep 2024 12:23:33 +0400 Subject: [PATCH 2/2] Remove 'invoke' alias from --entrypoint, add separate --invoke arg for single WASM modules (breaking change to Wasmer CLI args) --- lib/cli/src/commands/run/mod.rs | 20 ++++++++++++-------- lib/wasix/src/runners/wasi.rs | 12 ++++++------ lib/wasix/src/runners/wasi_common.rs | 6 +++--- lib/wasix/src/state/builder.rs | 18 +++++++++--------- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/lib/cli/src/commands/run/mod.rs b/lib/cli/src/commands/run/mod.rs index 9a414c5a58d..3966aaa6cc1 100644 --- a/lib/cli/src/commands/run/mod.rs +++ b/lib/cli/src/commands/run/mod.rs @@ -76,9 +76,12 @@ pub struct Run { /// Set the default stack size (default is 1048576) #[clap(long = "stack-size")] stack_size: Option, - /// The function or command to invoke. - #[clap(short, long, aliases = &["command", "invoke", "command-name"])] + /// The entrypoint module for webc packages. + #[clap(short, long, aliases = &["command", "command-name"])] entrypoint: Option, + /// The function to invoke. + #[clap(short, long)] + invoke: Option, /// Generate a coredump at this path if a WebAssembly trap occurs #[clap(name = "COREDUMP_PATH", long)] coredump_on_trap: Option, @@ -377,19 +380,19 @@ impl Run { let instance = Instance::new(store, module, &imports) .context("Unable to instantiate the WebAssembly module")?; - let entrypoint = match &self.entrypoint { + let entry_function = match &self.invoke { Some(entry) => { instance.exports .get_function(entry) - .with_context(|| format!("The module doesn't contain a \"{entry}\" function"))? + .with_context(|| format!("The module doesn't export a function named \"{entry}\""))? }, None => { instance.exports.get_function("_start") - .context("The module doesn't contain a \"_start\" function. Either implement it or specify an entrypoint function.")? + .context("The module doesn't export a \"_start\" function. Either implement it or specify an entry function with --invoke")? } }; - let return_values = invoke_function(&instance, store, entrypoint, &self.args)?; + let return_values = invoke_function(&instance, store, entry_function, &self.args)?; println!( "{}", @@ -425,8 +428,8 @@ impl Run { .with_forward_host_env(self.wasi.forward_host_env) .with_capabilities(self.wasi.capabilities()); - if let Some(ref entrypoint) = self.entrypoint { - runner.with_entrypoint(entrypoint); + if let Some(ref entry_function) = self.invoke { + runner.with_entry_function(entry_function); } #[cfg(feature = "journal")] @@ -523,6 +526,7 @@ impl Run { wcgi: WcgiOptions::default(), stack_size: None, entrypoint: Some(original_executable.to_string()), + invoke: None, coredump_on_trap: None, input: PackageSource::infer(executable)?, args: args.to_vec(), diff --git a/lib/wasix/src/runners/wasi.rs b/lib/wasix/src/runners/wasi.rs index 23d3f2eb6b7..530061df92c 100644 --- a/lib/wasix/src/runners/wasi.rs +++ b/lib/wasix/src/runners/wasi.rs @@ -34,17 +34,17 @@ impl WasiRunner { WasiRunner::default() } - /// Returns the current entrypoint for this `WasiRunner` - pub fn entrypoint(&self) -> Option { - self.wasi.entrypoint.clone() + /// Returns the current entry function for this `WasiRunner` + pub fn entry_function(&self) -> Option { + self.wasi.entry_function.clone() } - /// Builder method to set the name of the entrypoint function for this `WasiRunner` - pub fn with_entrypoint(&mut self, entrypoint: S) -> &mut Self + /// Builder method to set the name of the entry function for this `WasiRunner` + pub fn with_entry_function(&mut self, entry_function: S) -> &mut Self where S: Into, { - self.wasi.entrypoint = Some(entrypoint.into()); + self.wasi.entry_function = Some(entry_function.into()); self } diff --git a/lib/wasix/src/runners/wasi_common.rs b/lib/wasix/src/runners/wasi_common.rs index f265d275911..85ff26b548c 100644 --- a/lib/wasix/src/runners/wasi_common.rs +++ b/lib/wasix/src/runners/wasi_common.rs @@ -32,7 +32,7 @@ pub struct MappedCommand { #[derive(Derivative, Default, Clone)] #[derivative(Debug)] pub(crate) struct CommonWasiOptions { - pub(crate) entrypoint: Option, + pub(crate) entry_function: Option, pub(crate) args: Vec, pub(crate) env: HashMap, pub(crate) forward_host_env: bool, @@ -58,8 +58,8 @@ impl CommonWasiOptions { wasi: &WasiAnnotation, root_fs: Option, ) -> Result<(), anyhow::Error> { - if let Some(ref entrypoint) = self.entrypoint { - builder.set_entrypoint(entrypoint); + if let Some(ref entry_function) = self.entry_function { + builder.set_entry_function(entry_function); } let root_fs = root_fs.unwrap_or_else(|| { diff --git a/lib/wasix/src/state/builder.rs b/lib/wasix/src/state/builder.rs index 5a2f62091c3..6026452cf8b 100644 --- a/lib/wasix/src/state/builder.rs +++ b/lib/wasix/src/state/builder.rs @@ -49,8 +49,8 @@ use super::env::WasiEnvInit; /// ``` #[derive(Default)] pub struct WasiEnvBuilder { - /// Name of entrypoint function. Defaults to running `_start` if not specified. - pub(super) entrypoint: Option, + /// Name of entry function. Defaults to running `_start` if not specified. + pub(super) entry_function: Option, /// Command line arguments. pub(super) args: Vec, /// Environment variables. @@ -99,7 +99,7 @@ impl std::fmt::Debug for WasiEnvBuilder { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // TODO: update this when stable f.debug_struct("WasiEnvBuilder") - .field("entrypoint", &self.entrypoint) + .field("entry_function", &self.entry_function) .field("args", &self.args) .field("envs", &self.envs) .field("preopens", &self.preopens) @@ -243,19 +243,19 @@ impl WasiEnvBuilder { &mut self.envs } - pub fn entrypoint(mut self, entrypoint: S) -> Self + pub fn entry_function(mut self, entry_function: S) -> Self where S: AsRef, { - self.set_entrypoint(entrypoint); + self.set_entry_function(entry_function); self } - pub fn set_entrypoint(&mut self, entrypoint: S) + pub fn set_entry_function(&mut self, entry_function: S) where S: AsRef, { - self.entrypoint = Some(entrypoint.as_ref().to_owned()); + self.entry_function = Some(entry_function.as_ref().to_owned()); } /// Add an argument. @@ -1042,7 +1042,7 @@ impl WasiEnvBuilder { ); } - let entrypoint = self.entrypoint.clone(); + let entry_function = self.entry_function.clone(); let (instance, env) = self.instantiate_ext(module, module_hash, store)?; @@ -1058,7 +1058,7 @@ impl WasiEnvBuilder { let start = instance .exports - .get_function(entrypoint.as_deref().unwrap_or("_start"))?; + .get_function(entry_function.as_deref().unwrap_or("_start"))?; env.data(&store).thread.set_status_running(); let result = crate::run_wasi_func_start(start, store);