Skip to content

Commit

Permalink
Merge pull request #2570 from bytecodealliance/pch/wiggle_flags_bitflags
Browse files Browse the repository at this point in the history
wiggle: generate flags using `bitflags`
  • Loading branch information
Pat Hickey authored Jan 12, 2021
2 parents 7ed7c08 + 32f162a commit 47ff726
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 168 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/wasi-common/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Entry {
/// `HandleRights` structure is a subset of rights attached to this `Entry`. The check is
/// performed using `Entry::validate_rights` method. If the check fails, `Error::Notcapable`
/// is returned.
pub(crate) fn as_handle(&self, rights: &HandleRights) -> Result<EntryHandle> {
pub(crate) fn as_handle(&self, rights: HandleRights) -> Result<EntryHandle> {
self.validate_rights(rights)?;
Ok(self.handle.get())
}
Expand All @@ -87,7 +87,7 @@ impl Entry {
/// rights attached to this `Entry` object are a superset.
///
/// Upon unsuccessful check, `Error::Notcapable` is returned.
pub(crate) fn validate_rights(&self, rights: &HandleRights) -> Result<()> {
pub(crate) fn validate_rights(&self, rights: HandleRights) -> Result<()> {
let this_rights = self.handle.get_rights();
if this_rights.contains(rights) {
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi-common/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ impl HandleRights {
}

/// Checks if `other` is a subset of those rights.
pub fn contains(&self, other: &Self) -> bool {
self.base.contains(&other.base) && self.inheriting.contains(&other.inheriting)
pub fn contains(&self, other: Self) -> bool {
self.base.contains(other.base) && self.inheriting.contains(other.inheriting)
}

/// Returns base rights.
Expand Down Expand Up @@ -100,7 +100,7 @@ pub trait Handle {
let file_type = self.get_file_type();
let rights = self.get_rights();
let required_rights = HandleRights::from_base(Rights::FD_SEEK | Rights::FD_TELL);
file_type == Filetype::CharacterDevice && rights.contains(&required_rights)
file_type == Filetype::CharacterDevice && rights.contains(required_rights)
}
// TODO perhaps should be a separate trait?
// FdOps
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-common/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) use crate::sys::path::{from_host, open_rights};
/// This is a workaround for not having Capsicum support in the OS.
pub(crate) fn get(
entry: &Entry,
required_rights: &HandleRights,
required_rights: HandleRights,
dirflags: Lookupflags,
path: &str,
needs_final_component: bool,
Expand Down Expand Up @@ -140,7 +140,7 @@ pub(crate) fn get(
}

continue;
} else if ends_with_slash || dirflags.contains(&Lookupflags::SYMLINK_FOLLOW)
} else if ends_with_slash || dirflags.contains(Lookupflags::SYMLINK_FOLLOW)
{
// if there's a trailing slash, or if `LOOKUP_SYMLINK_FOLLOW` is set, attempt
// symlink expansion
Expand Down
68 changes: 34 additions & 34 deletions crates/wasi-common/src/snapshots/wasi_snapshot_preview1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let required_rights = HandleRights::from_base(types::Rights::FD_ADVISE);
let entry = self.get_entry(fd)?;
entry
.as_handle(&required_rights)?
.as_handle(required_rights)?
.advise(advice, offset, len)
}

Expand All @@ -72,7 +72,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
) -> Result<()> {
let required_rights = HandleRights::from_base(types::Rights::FD_ALLOCATE);
let entry = self.get_entry(fd)?;
entry.as_handle(&required_rights)?.allocate(offset, len)
entry.as_handle(required_rights)?.allocate(offset, len)
}

fn fd_close(&self, fd: types::Fd) -> Result<()> {
Expand All @@ -89,12 +89,12 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
fn fd_datasync(&self, fd: types::Fd) -> Result<()> {
let required_rights = HandleRights::from_base(types::Rights::FD_DATASYNC);
let entry = self.get_entry(fd)?;
entry.as_handle(&required_rights)?.datasync()
entry.as_handle(required_rights)?.datasync()
}

fn fd_fdstat_get(&self, fd: types::Fd) -> Result<types::Fdstat> {
let entry = self.get_entry(fd)?;
let file = entry.as_handle(&HandleRights::empty())?;
let file = entry.as_handle(HandleRights::empty())?;
let fs_flags = file.fdstat_get()?;
let rights = entry.get_rights();
let fdstat = types::Fdstat {
Expand All @@ -109,7 +109,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
fn fd_fdstat_set_flags(&self, fd: types::Fd, flags: types::Fdflags) -> Result<()> {
let required_rights = HandleRights::from_base(types::Rights::FD_FDSTAT_SET_FLAGS);
let entry = self.get_entry(fd)?;
entry.as_handle(&required_rights)?.fdstat_set_flags(flags)
entry.as_handle(required_rights)?.fdstat_set_flags(flags)
}

fn fd_fdstat_set_rights(
Expand All @@ -120,7 +120,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
) -> Result<()> {
let rights = HandleRights::new(fs_rights_base, fs_rights_inheriting);
let entry = self.get_entry(fd)?;
if !entry.get_rights().contains(&rights) {
if !entry.get_rights().contains(rights) {
return Err(Error::Notcapable);
}
entry.set_rights(rights);
Expand All @@ -130,14 +130,14 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
fn fd_filestat_get(&self, fd: types::Fd) -> Result<types::Filestat> {
let required_rights = HandleRights::from_base(types::Rights::FD_FILESTAT_GET);
let entry = self.get_entry(fd)?;
let host_filestat = entry.as_handle(&required_rights)?.filestat_get()?;
let host_filestat = entry.as_handle(required_rights)?.filestat_get()?;
Ok(host_filestat)
}

fn fd_filestat_set_size(&self, fd: types::Fd, size: types::Filesize) -> Result<()> {
let required_rights = HandleRights::from_base(types::Rights::FD_FILESTAT_SET_SIZE);
let entry = self.get_entry(fd)?;
entry.as_handle(&required_rights)?.filestat_set_size(size)
entry.as_handle(required_rights)?.filestat_set_size(size)
}

fn fd_filestat_set_times(
Expand All @@ -150,7 +150,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let required_rights = HandleRights::from_base(types::Rights::FD_FILESTAT_SET_TIMES);
let entry = self.get_entry(fd)?;
entry
.as_handle(&required_rights)?
.as_handle(required_rights)?
.filestat_set_times(atim, mtim, fst_flags)
}

Expand Down Expand Up @@ -180,7 +180,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
.map(|s| io::IoSliceMut::new(&mut *s))
.collect::<Vec<io::IoSliceMut<'_>>>();
entry
.as_handle(&required_rights)?
.as_handle(required_rights)?
.preadv(&mut buf, offset)?
.try_into()?
};
Expand Down Expand Up @@ -255,7 +255,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let buf: Vec<io::IoSlice> =
guest_slices.iter().map(|s| io::IoSlice::new(&*s)).collect();
entry
.as_handle(&required_rights)?
.as_handle(required_rights)?
.pwritev(&buf, offset)?
.try_into()?
};
Expand All @@ -278,7 +278,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
.map(|s| io::IoSliceMut::new(&mut *s))
.collect();
entry
.as_handle(&required_rights)?
.as_handle(required_rights)?
.read_vectored(&mut slices)?
.try_into()?
};
Expand All @@ -298,7 +298,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {

let mut bufused = 0;
let mut buf = buf.clone();
for pair in entry.as_handle(&required_rights)?.readdir(cookie)? {
for pair in entry.as_handle(required_rights)?.readdir(cookie)? {
let (dirent, name) = pair?;
let dirent_raw = dirent.as_bytes()?;
let dirent_len: types::Size = dirent_raw.len().try_into()?;
Expand Down Expand Up @@ -379,21 +379,21 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
types::Whence::End => SeekFrom::End(offset),
types::Whence::Set => SeekFrom::Start(offset as u64),
};
let host_newoffset = entry.as_handle(&required_rights)?.seek(pos)?;
let host_newoffset = entry.as_handle(required_rights)?.seek(pos)?;
Ok(host_newoffset)
}

fn fd_sync(&self, fd: types::Fd) -> Result<()> {
let required_rights = HandleRights::from_base(types::Rights::FD_SYNC);
let entry = self.get_entry(fd)?;
entry.as_handle(&required_rights)?.sync()
entry.as_handle(required_rights)?.sync()
}

fn fd_tell(&self, fd: types::Fd) -> Result<types::Filesize> {
let required_rights = HandleRights::from_base(types::Rights::FD_TELL);
let entry = self.get_entry(fd)?;
let host_offset = entry
.as_handle(&required_rights)?
.as_handle(required_rights)?
.seek(SeekFrom::Current(0))?;
Ok(host_offset)
}
Expand All @@ -411,7 +411,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let slices: Vec<io::IoSlice> =
guest_slices.iter().map(|s| io::IoSlice::new(&*s)).collect();
entry
.as_handle(&required_rights)?
.as_handle(required_rights)?
.write_vectored(&slices)?
.try_into()?
};
Expand All @@ -426,7 +426,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let path = path.as_str()?;
let (dirfd, path) = path::get(
&entry,
&required_rights,
required_rights,
types::Lookupflags::empty(),
path.deref(),
false,
Expand All @@ -443,9 +443,9 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let required_rights = HandleRights::from_base(types::Rights::PATH_FILESTAT_GET);
let entry = self.get_entry(dirfd)?;
let path = path.as_str()?;
let (dirfd, path) = path::get(&entry, &required_rights, flags, path.deref(), false)?;
let (dirfd, path) = path::get(&entry, required_rights, flags, path.deref(), false)?;
let host_filestat =
dirfd.filestat_get_at(&path, flags.contains(&types::Lookupflags::SYMLINK_FOLLOW))?;
dirfd.filestat_get_at(&path, flags.contains(types::Lookupflags::SYMLINK_FOLLOW))?;
Ok(host_filestat)
}

Expand All @@ -461,13 +461,13 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let required_rights = HandleRights::from_base(types::Rights::PATH_FILESTAT_SET_TIMES);
let entry = self.get_entry(dirfd)?;
let path = path.as_str()?;
let (dirfd, path) = path::get(&entry, &required_rights, flags, path.deref(), false)?;
let (dirfd, path) = path::get(&entry, required_rights, flags, path.deref(), false)?;
dirfd.filestat_set_times_at(
&path,
atim,
mtim,
fst_flags,
flags.contains(&types::Lookupflags::SYMLINK_FOLLOW),
flags.contains(types::Lookupflags::SYMLINK_FOLLOW),
)?;
Ok(())
}
Expand All @@ -487,7 +487,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let old_path = old_path.as_str()?;
path::get(
&old_entry,
&required_rights,
required_rights,
types::Lookupflags::empty(),
old_path.deref(),
false,
Expand All @@ -500,7 +500,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let new_path = new_path.as_str()?;
path::get(
&new_entry,
&required_rights,
required_rights,
types::Lookupflags::empty(),
new_path.deref(),
false,
Expand All @@ -510,7 +510,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
&old_path,
new_dirfd,
&new_path,
old_flags.contains(&types::Lookupflags::SYMLINK_FOLLOW),
old_flags.contains(types::Lookupflags::SYMLINK_FOLLOW),
)
}

Expand All @@ -535,7 +535,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let path = path.as_str()?;
path::get(
&entry,
&needed_rights,
needed_rights,
dirflags,
path.deref(),
oflags & types::Oflags::CREAT != types::Oflags::empty(),
Expand Down Expand Up @@ -581,7 +581,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let path = path.as_str()?;
path::get(
&entry,
&required_rights,
required_rights,
types::Lookupflags::empty(),
path.deref(),
false,
Expand All @@ -599,7 +599,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let path = path.as_str()?;
path::get(
&entry,
&required_rights,
required_rights,
types::Lookupflags::empty(),
path.deref(),
true,
Expand All @@ -621,7 +621,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let old_path = old_path.as_str()?;
path::get(
&entry,
&required_rights,
required_rights,
types::Lookupflags::empty(),
old_path.deref(),
true,
Expand All @@ -633,7 +633,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let new_path = new_path.as_str()?;
path::get(
&entry,
&required_rights,
required_rights,
types::Lookupflags::empty(),
new_path.deref(),
true,
Expand All @@ -654,7 +654,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let new_path = new_path.as_str()?;
path::get(
&entry,
&required_rights,
required_rights,
types::Lookupflags::empty(),
new_path.deref(),
true,
Expand All @@ -672,7 +672,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let path = path.as_str()?;
path::get(
&entry,
&required_rights,
required_rights,
types::Lookupflags::empty(),
path.deref(),
false,
Expand Down Expand Up @@ -815,7 +815,7 @@ impl WasiCtx {
}
};
fd_events.push(sched::FdEventData {
handle: entry.as_handle(&required_rights)?,
handle: entry.as_handle(required_rights)?,
r#type: types::Eventtype::FdRead,
userdata: subscription.userdata,
});
Expand All @@ -841,7 +841,7 @@ impl WasiCtx {
}
};
fd_events.push(sched::FdEventData {
handle: entry.as_handle(&required_rights)?,
handle: entry.as_handle(required_rights)?,
r#type: types::Eventtype::FdWrite,
userdata: subscription.userdata,
});
Expand Down
8 changes: 4 additions & 4 deletions crates/wasi-common/src/sys/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ pub(crate) fn filestat_set_times(
st_mtim: Timestamp,
fst_flags: Fstflags,
) -> Result<()> {
let set_atim = fst_flags.contains(&Fstflags::ATIM);
let set_atim_now = fst_flags.contains(&Fstflags::ATIM_NOW);
let set_mtim = fst_flags.contains(&Fstflags::MTIM);
let set_mtim_now = fst_flags.contains(&Fstflags::MTIM_NOW);
let set_atim = fst_flags.contains(Fstflags::ATIM);
let set_atim_now = fst_flags.contains(Fstflags::ATIM_NOW);
let set_mtim = fst_flags.contains(Fstflags::MTIM);
let set_mtim_now = fst_flags.contains(Fstflags::MTIM_NOW);

if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
return Err(Error::Inval);
Expand Down
Loading

0 comments on commit 47ff726

Please sign in to comment.