Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/cli/src/commands/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ impl Run {
uses: Vec<BinaryPackage>,
runtime: Arc<dyn Runtime + Send + Sync>,
) -> 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)
}

Expand Down Expand Up @@ -506,7 +507,7 @@ impl Run {
pkg: &BinaryPackage,
runtime: Arc<dyn Runtime + Send + Sync>,
) -> 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)
}
Expand Down Expand Up @@ -555,12 +556,13 @@ impl Run {
fn build_wasi_runner(
&self,
runtime: &Arc<dyn Runtime + Send + Sync>,
is_wasix: bool,
) -> Result<WasiRunner, anyhow::Error> {
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)
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 26 additions & 4 deletions lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,10 @@ impl Wasi {
Ok(Vec::new())
}

pub fn build_mapped_directories(&self) -> Result<(bool, Vec<MappedDirectory>), anyhow::Error> {
pub fn build_mapped_directories(
&self,
is_wasix: bool,
) -> Result<(bool, Vec<MappedDirectory>), anyhow::Error> {
let mut mapped_dirs = Vec::new();

// Process the --dirs flag and merge it with --mapdir.
Expand All @@ -514,8 +517,16 @@ 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 {
bail!(
"Cannot pre-open the root directory with --dir=/ as mounting on the guest's virtual root is not allowed"
);
} else {
let resolved = dir.canonicalize().with_context(|| {
format!(
Expand Down Expand Up @@ -551,11 +562,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.
bail!(
"Mounting on the guest's virtual root with --mapdir /:<HOST_PATH> is not allowed"
);
}
let resolved_host = host.canonicalize().with_context(|| {
format!(
"could not canonicalize path for argument '--mapdir {}:{}'",
host.display(),
guest,
host.display(),
)
})?;

Expand All @@ -569,7 +587,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()
},
Comment on lines -572 to +596
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only thing that really changed. I don't understand why this change is made and if it is an improvement.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arshia001 If you are 100% certain this is a good idea, then go ahead and merge

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zebreus this is kind of the main issue! With WASI modules, nothing is mounted in the module's FS by default, so it's OK to mount wherever. With WASIX, we mount a lot of stuff in by default, so you don't want a mount on the root.

Now, with WASI, since there is no host-side CWD and you're at / by default, you just have to mount directly in / to make the files available to the module with relative paths. With WASIX, we mount into /home and cd into it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I don't understand is that when MAPPED_CURRENT_DIR_DEFAULT_PATH is already defined to /home and that seems to work just fine.

If I understand you correctly the reason for the change is to make sure that the mountpoint is always the CWD, which it can't be for WASI. But we also can't always mount to / because that won't work for WASIX...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in the past, the dir was just mounted to /home for WASI and now it isn't anymore. While this could be breaking, it's probably an improvement. However I don't like that it's one more inconsistency.

}
} else {
MappedDirectory {
Expand Down
9 changes: 9 additions & 0 deletions lib/package/src/package/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ pub enum WasmerPackageError {
/// A manifest validation error.
#[error("The manifest is invalid")]
Validation(#[from] wasmer_config::package::ValidationError),
/// Volumes can't be mounted on the guest's root
#[error(
"Mounting on the guest's root (e.g. \"/\" = \"<HOST_PATH>\" in [fs] section of wasmer.toml) is not allowed"
)]
MountOnRoot,
/// A path in the fs mapping does not exist
#[error("Path: \"{}\" does not exist", path.display())]
PathNotExists {
Expand Down Expand Up @@ -335,6 +340,10 @@ impl Package {
.expect("Canonicalizing should always result in a file with a parent directory")
.to_path_buf();

if wasmer_toml.fs.keys().any(|k| k == "/") {
return Err(WasmerPackageError::MountOnRoot);
}

for path in wasmer_toml.fs.values() {
if !base_dir.join(path).exists() {
return Err(WasmerPackageError::PathNotExists { path: path.clone() });
Expand Down
20 changes: 13 additions & 7 deletions lib/wasix/src/runtime/package_loader/load_package_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
sync::Arc,
};

use anyhow::{Context, Error};
use anyhow::{Context, Error, bail};
use futures::{StreamExt, TryStreamExt, future::BoxFuture};
use once_cell::sync::OnceCell;
use petgraph::visit::EdgeRef;
Expand Down Expand Up @@ -466,9 +466,6 @@ fn filesystem_v3(
) -> Result<UnionFileSystem, Error> {
let mut volumes: HashMap<&PackageId, BTreeMap<String, Volume>> = 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 {
Expand All @@ -482,6 +479,12 @@ fn filesystem_v3(
continue;
}

if mount_path.as_path() == Path::new("/") {
bail!(
"The \"{package}\" package wants to mount a volume at \"/\", but that's not allowed",
);
}

// 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.
Expand Down Expand Up @@ -538,9 +541,6 @@ fn filesystem_v2(
) -> Result<UnionFileSystem, Error> {
let mut volumes: HashMap<&PackageId, BTreeMap<String, Volume>> = 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 {
Expand All @@ -554,6 +554,12 @@ fn filesystem_v2(
continue;
}

if mount_path.as_path() == Path::new("/") {
bail!(
"The \"{package}\" package wants to mount a volume at \"/\", but that's not allowed",
);
}

// 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.
Expand Down
Loading