diff --git a/lib/cli/src/commands/run/mod.rs b/lib/cli/src/commands/run/mod.rs index d91a3ed4296..4abed422fbd 100644 --- a/lib/cli/src/commands/run/mod.rs +++ b/lib/cli/src/commands/run/mod.rs @@ -423,7 +423,8 @@ impl Run { uses: Vec, runtime: Arc, ) -> Result<(), Error> { - let mut runner = self.build_wasi_runner(&runtime)?; + // Assume webcs are always WASIX + let mut runner = self.build_wasi_runner(&runtime, true)?; Runner::run_command(&mut runner, command_name, pkg, runtime) } @@ -506,7 +507,7 @@ impl Run { pkg: &BinaryPackage, runtime: Arc, ) -> Result<(), Error> { - let mut inner = self.build_wasi_runner(&runtime)?; + let mut inner = self.build_wasi_runner(&runtime, true)?; let mut runner = wasmer_wasix::runners::dproxy::DProxyRunner::new(inner, pkg); runner.run_command(command_name, pkg, runtime) } @@ -555,12 +556,13 @@ impl Run { fn build_wasi_runner( &self, runtime: &Arc, + is_wasix: bool, ) -> Result { let packages = self.load_injected_packages(runtime)?; let mut runner = WasiRunner::new(); - let (is_home_mapped, mapped_diretories) = self.wasi.build_mapped_directories()?; + let (is_home_mapped, mapped_diretories) = self.wasi.build_mapped_directories(is_wasix)?; runner .with_args(&self.args) @@ -625,7 +627,7 @@ impl Run { ) -> Result<(), Error> { let program_name = wasm_path.display().to_string(); - let runner = self.build_wasi_runner(&runtime)?; + let runner = self.build_wasi_runner(&runtime, wasmer_wasix::is_wasix_module(&module))?; runner.run_wasm( RuntimeOrEngine::Runtime(runtime), &program_name, diff --git a/lib/cli/src/commands/run/wasi.rs b/lib/cli/src/commands/run/wasi.rs index e324e668e26..33336136d38 100644 --- a/lib/cli/src/commands/run/wasi.rs +++ b/lib/cli/src/commands/run/wasi.rs @@ -495,7 +495,10 @@ impl Wasi { Ok(Vec::new()) } - pub fn build_mapped_directories(&self) -> Result<(bool, Vec), anyhow::Error> { + pub fn build_mapped_directories( + &self, + is_wasix: bool, + ) -> Result<(bool, Vec), anyhow::Error> { let mut mapped_dirs = Vec::new(); // Process the --dirs flag and merge it with --mapdir. @@ -514,9 +517,19 @@ impl Wasi { MappedDirectory { host: current_dir, - guest: MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string(), + guest: if is_wasix { + MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string() + } else { + "/".to_string() + }, } } else { + if dir == Path::new("/") && is_wasix { + tracing::warn!( + "Pre-opening the root directory with --dir=/ breaks WASIX modules' filesystems" + ); + } + let resolved = dir.canonicalize().with_context(|| { format!( "could not canonicalize path for argument '--dir {}'", @@ -551,11 +564,18 @@ impl Wasi { } for MappedDirectory { host, guest } in &self.mapped_dirs { + if guest == "/" && is_wasix { + // Note: it appears we canonicalize the path before this point and showing the value of + // `host` in the error message may throw users off, so we use a placeholder. + tracing::warn!( + "Mounting on the guest's virtual root with --mapdir /: breaks WASIX modules' filesystems" + ); + } let resolved_host = host.canonicalize().with_context(|| { format!( "could not canonicalize path for argument '--mapdir {}:{}'", - host.display(), guest, + host.display(), ) })?; @@ -569,7 +589,11 @@ impl Wasi { MappedDirectory { host: resolved_host, - guest: MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string(), + guest: if is_wasix { + MAPPED_CURRENT_DIR_DEFAULT_PATH.to_string() + } else { + "/".to_string() + }, } } else { MappedDirectory { diff --git a/lib/wasix/src/runtime/package_loader/load_package_tree.rs b/lib/wasix/src/runtime/package_loader/load_package_tree.rs index d8f5dd4ed62..d679a5ff38f 100644 --- a/lib/wasix/src/runtime/package_loader/load_package_tree.rs +++ b/lib/wasix/src/runtime/package_loader/load_package_tree.rs @@ -466,9 +466,6 @@ fn filesystem_v3( ) -> Result { let mut volumes: HashMap<&PackageId, BTreeMap> = HashMap::new(); - let mut mountings: Vec<_> = pkg.filesystem.iter().collect(); - mountings.sort_by_key(|m| std::cmp::Reverse(m.mount_path.as_path())); - let union_fs = UnionFileSystem::new(); for ResolvedFileSystemMapping { @@ -482,6 +479,12 @@ fn filesystem_v3( continue; } + if mount_path.as_path() == Path::new("/") { + tracing::warn!( + "The \"{package}\" package wants to mount a volume at \"/\", which breaks WASIX modules' filesystems", + ); + } + // Note: We want to reuse existing Volume instances if we can. That way // we can keep the memory usage down. A webc::compat::Volume is // reference-counted, anyway. @@ -538,9 +541,6 @@ fn filesystem_v2( ) -> Result { let mut volumes: HashMap<&PackageId, BTreeMap> = HashMap::new(); - let mut mountings: Vec<_> = pkg.filesystem.iter().collect(); - mountings.sort_by_key(|m| std::cmp::Reverse(m.mount_path.as_path())); - let union_fs = UnionFileSystem::new(); for ResolvedFileSystemMapping { @@ -554,6 +554,12 @@ fn filesystem_v2( continue; } + if mount_path.as_path() == Path::new("/") { + tracing::warn!( + "The \"{package}\" package wants to mount a volume at \"/\", which breaks WASIX modules' filesystems", + ); + } + // Note: We want to reuse existing Volume instances if we can. That way // we can keep the memory usage down. A webc::compat::Volume is // reference-counted, anyway.