Skip to content

Commit

Permalink
Fix preopened directory mapping to virtual root
Browse files Browse the repository at this point in the history
This commit fixes the bug where preopening `.` causes the WASI runner to
map it to the virtual root, causing `path_open` to fail.

fixes wasmerio#4361
  • Loading branch information
yagehu committed Dec 18, 2023
1 parent 1abcd2c commit 16655e6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/wasix/src/os/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
bin_factory::{spawn_exec, BinFactory, BinaryPackage},
capabilities::Capabilities,
os::task::{control_plane::WasiControlPlane, process::WasiProcess},
runners::MappedDirectory,
runtime::{resolver::PackageSpecifier, task_manager::InlineWaker},
Runtime, SpawnError, WasiEnv, WasiEnvBuilder, WasiRuntimeError,
};
Expand Down Expand Up @@ -225,6 +226,10 @@ impl Console {
.with_stdin(Box::new(self.stdin.clone()))
.with_stdout(Box::new(self.stdout.clone()))
.with_stderr(Box::new(self.stderr.clone()))
.with_mapped_directories(vec![MappedDirectory {
host: ".".into(),
guest: "/mnt/host".to_owned(),
}])
.prepare_webc_env(
prog,
&wasi_opts,
Expand Down
4 changes: 2 additions & 2 deletions lib/wasix/src/runners/wasi_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl CommonWasiOptions {
builder.add_preopen_dir("/")?;

if self.mapped_dirs.iter().all(|m| m.guest != ".") {
// The user hasn't mounted "." to anything, so let's map it to "/"
builder.add_map_dir(".", "/")?;
// The user hasn't mounted "." to anything, so let's map it to "/mnt/host"
builder.add_map_dir(".", "/mnt/host")?;
}

builder.set_fs(Box::new(fs));
Expand Down
14 changes: 13 additions & 1 deletion lib/wasix/tests/runners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ use webc::Container;

mod wasi {
use virtual_fs::{AsyncReadExt, AsyncSeekExt};
use wasmer_wasix::{bin_factory::BinaryPackage, runners::wasi::WasiRunner, WasiError};
use wasmer_wasix::{
bin_factory::BinaryPackage,
runners::{wasi::WasiRunner, MappedDirectory},
WasiError,
};

use super::*;

Expand Down Expand Up @@ -54,6 +58,10 @@ mod wasi {
.with_stdin(Box::new(virtual_fs::NullFile::default()))
.with_stdout(Box::new(stdout_2) as Box<_>)
.with_stderr(Box::new(virtual_fs::NullFile::default()))
.with_mapped_directories(vec![MappedDirectory {
host: ".".into(),
guest: "/mnt/host".to_owned(),
}])
.run_command("wat2wasm", &pkg, Arc::new(rt))
});

Expand Down Expand Up @@ -82,6 +90,10 @@ mod wasi {
.with_stdin(Box::new(virtual_fs::NullFile::default()))
.with_stdout(Box::new(virtual_fs::NullFile::default()))
.with_stderr(Box::new(virtual_fs::NullFile::default()))
.with_mapped_directories(vec![MappedDirectory {
host: ".".into(),
guest: "/mnt/host".to_owned(),
}])
.run_command("python", &pkg, Arc::new(rt))
});

Expand Down

0 comments on commit 16655e6

Please sign in to comment.