Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop returning NOTCAPABLE errors from WASI calls. #4666

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ unsafe fn try_read_file(dir_fd: wasi::Fd) {
wasi::fd_read(fd, &[iovec])
.expect_err("reading bytes from file should fail")
.raw_error(),
wasi::ERRNO_NOTCAPABLE
wasi::ERRNO_BADF
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ unsafe fn test_truncation_rights(dir_fd: wasi::Fd) {
wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_TRUNC, 0, 0, 0)
.expect_err("truncating a file without path_filestat_set_size right")
.raw_error(),
wasi::ERRNO_NOTCAPABLE
wasi::ERRNO_PERM
);
}

Expand Down
14 changes: 11 additions & 3 deletions crates/wasi-common/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,23 @@ impl DirEntry {
if self.caps.contains(caps) {
Ok(())
} else {
Err(Error::not_capable().context(format!("desired {:?}, has {:?}", caps, self.caps,)))
let missing = caps & !self.caps;
let err = if missing.intersects(DirCaps::READDIR) {
Error::not_dir()
} else {
Error::perm()
};
Err(err.context(format!("desired rights {:?}, has {:?}", caps, self.caps)))
}
}
pub fn capable_of_file(&self, caps: FileCaps) -> Result<(), Error> {
if self.file_caps.contains(caps) {
Ok(())
} else {
Err(Error::not_capable()
.context(format!("desired {:?}, has {:?}", caps, self.file_caps)))
Err(Error::perm().context(format!(
"desired rights {:?}, has {:?}",
caps, self.file_caps
)))
}
}
pub fn drop_caps_to(&mut self, caps: DirCaps, file_caps: FileCaps) -> Result<(), Error> {
Expand Down
9 changes: 6 additions & 3 deletions crates/wasi-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ pub enum ErrorKind {
/// Errno::Spipe: Invalid seek
#[error("Spipe: Invalid seek")]
Spipe,
/// Errno::Perm: Permission denied
#[error("Permission denied")]
Perm,
/// Errno::NotCapable: Not capable
#[error("Not capable")]
NotCapable,
Expand All @@ -92,7 +95,7 @@ pub trait ErrorExt {
fn overflow() -> Self;
fn range() -> Self;
fn seek_pipe() -> Self;
fn not_capable() -> Self;
fn perm() -> Self;
}

impl ErrorExt for Error {
Expand Down Expand Up @@ -138,7 +141,7 @@ impl ErrorExt for Error {
fn seek_pipe() -> Self {
ErrorKind::Spipe.into()
}
fn not_capable() -> Self {
ErrorKind::NotCapable.into()
fn perm() -> Self {
ErrorKind::Perm.into()
}
}
11 changes: 10 additions & 1 deletion crates/wasi-common/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,16 @@ impl FileEntry {
if self.caps.contains(caps) {
Ok(())
} else {
Err(Error::not_capable().context(format!("desired {:?}, has {:?}", caps, self.caps,)))
let missing = caps & !self.caps;
let err = if missing.intersects(FileCaps::READ | FileCaps::WRITE) {
// `EBADF` is a little surprising here because it's also used
// for unknown-file-descriptor errors, but it's what POSIX uses
// in this situation.
Error::badf()
} else {
Error::perm()
};
Err(err.context(format!("desired rights {:?}, has {:?}", caps, self.caps)))
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/wasi-common/src/snapshots/preview_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl From<ErrorKind> for types::Errno {
ErrorKind::Range => Errno::Range,
ErrorKind::Spipe => Errno::Spipe,
ErrorKind::NotCapable => Errno::Notcapable,
ErrorKind::Perm => Errno::Perm,
}
}
}
Expand Down