From ed18febfce93110360d5da06d8d3d665e36bff9a Mon Sep 17 00:00:00 2001 From: Julius Michaelis Date: Wed, 4 Aug 2021 08:49:18 +0900 Subject: [PATCH] binfmt_misc: register .wat --- lib/cli/src/commands/binfmt.rs | 61 +++++++++++++++++++++------------- tests/examples/memory.wat | 0 2 files changed, 38 insertions(+), 23 deletions(-) mode change 100644 => 100755 tests/examples/memory.wat diff --git a/lib/cli/src/commands/binfmt.rs b/lib/cli/src/commands/binfmt.rs index 8bfeb0bb7..7b49744ae 100644 --- a/lib/cli/src/commands/binfmt.rs +++ b/lib/cli/src/commands/binfmt.rs @@ -8,7 +8,7 @@ use std::os::unix::ffi::OsStrExt; use std::os::unix::fs::MetadataExt; use Action::*; -#[derive(StructOpt)] +#[derive(StructOpt,Clone,Copy)] enum Action { /// Register wasmer as binfmt interpreter Register, @@ -54,7 +54,7 @@ impl Binfmt { panic!("{} does not exist", self.binfmt_misc.to_string_lossy()); } let temp_dir; - let spec = match self.action { + let specs = match self.action { Register | Reregister => { temp_dir = tempfile::tempdir().context("Make temporary directory")?; seccheck(temp_dir.path())?; @@ -65,34 +65,49 @@ impl Binfmt { .context("Copy wasmer binary to temp folder")?; let bin_path = fs::canonicalize(&bin_path) .with_context(|| format!("Couldn't get absolute path for {}", bin_path.to_string_lossy()))?; - Some([b":wasm32:M::\\x00asm\\x01\\x00\\x00::".as_ref(), bin_path.as_os_str().as_bytes(), b":PFC"].concat()) + Some([ + [b":wasm32:M::\\x00asm\\x01\\x00\\x00::".as_ref(), bin_path.as_os_str().as_bytes(), b":PFC"].concat(), + [b":wasm32-wat:E::wat::".as_ref(), bin_path.as_os_str().as_bytes(), b":PFC"].concat(), + ]) }, _ => None }; - let registration = self.binfmt_misc.join("wasm32"); - let registration_exists = registration.exists(); + let wasm_registration = self.binfmt_misc.join("wasm32"); + let wat_registration = self.binfmt_misc.join("wasm32-wat"); match self.action { - Unregister if !registration_exists => { - bail!("Cannot unregister binfmt, not registered"); - }, - Reregister | Unregister if registration.exists() => { - let mut registration = fs::OpenOptions::new() - .write(true) - .open(registration).context("Open existing binfmt entry to remove")?; - registration.write_all(b"-1").context("Couldn't write binfmt unregister request")?; + Reregister | Unregister => { + let unregister = [wasm_registration, wat_registration].iter().map(|registration| { + if registration.exists() { + let mut registration = fs::OpenOptions::new() + .write(true) + .open(registration).context("Open existing binfmt entry to remove")?; + registration.write_all(b"-1").context("Couldn't write binfmt unregister request")?; + Ok(true) + } else { + eprintln!("Warning: {} does not exist, not unregistered.", registration.to_string_lossy()); + Ok(false) + } + }).collect::>().into_iter().collect::>>()?; + match (self.action, unregister.into_iter().any(|b| b)) { + (Unregister, false) => bail!("Nothing unregistered"), + _ => () + } }, _ => (), }; - if cfg!(target_env = "gnu") { - // Approximate. ELF parsing for a proper check feels like overkill here. - eprintln!("Warning: wasmer has been compiled for glibc, and is thus likely dynamically linked. Invoking wasm binaries in chroots or mount namespaces (lxc, docker, ...) may not work."); - } - if let Some(spec) = spec { - let register = self.binfmt_misc.join("register"); - let mut register = fs::OpenOptions::new() - .write(true) - .open(register).context("Open binfmt misc for registration")?; - register.write_all(&spec).context("Couldn't register binfmt")?; + if let Some(specs) = specs { + if cfg!(target_env = "gnu") { + // Approximate. ELF parsing for a proper check feels like overkill here. + eprintln!("Warning: wasmer has been compiled for glibc, and is thus likely dynamically linked. Invoking wasm binaries in chroots or mount namespaces (lxc, docker, ...) may not work."); + } + specs.iter().map(|spec| { + let register = self.binfmt_misc.join("register"); + let mut register = fs::OpenOptions::new() + .write(true) + .open(register).context("Open binfmt misc for registration")?; + register.write_all(&spec).context("Couldn't register binfmt")?; + Ok(()) + }).collect::>().into_iter().collect::>>()?; } Ok(()) } diff --git a/tests/examples/memory.wat b/tests/examples/memory.wat old mode 100644 new mode 100755