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

Begin porting yanix to WASI #2046

Closed
wants to merge 24 commits into from

Conversation

sunfishcode
Copy link
Member

This is a series of patches which begin porting yanix to WASI, and which contain various other fixes needed for using yanix outside of Wasmtime in general.

@sunfishcode sunfishcode requested a review from kubkon July 20, 2020 14:51
@github-actions github-actions bot added the wasi Issues pertaining to WASI label Jul 20, 2020
@github-actions
Copy link

Subscribe to Label Action

cc @kubkon

This issue or pull request has been labeled: "wasi"

Thus the following users have been cc'd because of the following labels:

  • kubkon: wasi

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

Copy link
Member

@kubkon kubkon left a comment

Choose a reason for hiding this comment

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

Looks good, just a couple of questions below:

@@ -25,12 +25,12 @@ impl Dir {
}

unsafe fn from_fd(fd: RawFd) -> Result<Self> {
let d = libc::fdopendir(fd);
let d = libc::fdopendir(fd as libc::c_int);
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, why is this cast suddenly needed here?

Copy link
Member Author

Choose a reason for hiding this comment

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

WASI's RawFd is u32. libc::fdopendir takes a c_int, which is signed i32.

if let Some(d) = ptr::NonNull::new(d) {
Ok(Self(d))
} else {
let e = io::Error::last_os_error();
libc::close(fd);
libc::close(fd as libc::c_int);
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.


impl AsRawFd for Dir {
fn as_raw_fd(&self) -> RawFd {
unsafe { libc::dirfd(self.0.as_ptr()) }
unsafe { libc::dirfd(self.0.as_ptr()) as RawFd }
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to as cast here at all?

Copy link
Member Author

Choose a reason for hiding this comment

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

As above, RawFd is a u32 on WASI.

use libc::{
fstat as libc_fstat, fstatat as libc_fstatat, lseek as libc_lseek, openat as libc_openat,
};
#[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "l4re"))]
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity, what is l4re?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a microkernel with L4 ancestry. It's just here because I looked that Rust's libc crate to see which platforms needed this, and it was one of them.

Comment on lines -27 to -31
// TODO This should be verified on different BSD-flavours.
//
// According to 4.3BSD/POSIX.1-2001 man pages, there was an error
// if the errno value has changed at some point during the sequence
// of readdir calls.
Copy link
Member

Choose a reason for hiding this comment

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

Have you verified this is the case on different BSD flavours perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, yes. FreeBSD's readdir says:

The function returns NULL upon
reaching the end of the directory or on error. In the event of an error,
errno may be set to any of the values documented for the getdirentries(2)
system call.

The Unix convention for situatuations like this is to set errno to 0 before the call, and check if it's non-zero after the call. POSIX's description of readdir makes this explicit:

Applications wishing to check for error situations should set errno to 0 before calling readdir(). If errno is set to non-zero on return, an error occurred.

Ok(link.into())
// Start with a buffer big enough for the vast majority of paths.
let mut buffer = Vec::with_capacity(256);
loop {
Copy link
Member

Choose a reason for hiding this comment

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

Is this loop necessary for actual OSes? I thought that libc::PATH_MAX existed for a reason and the OS would never return a path that is actually longer, or am I missing something here?

On WASI, it makes sense to loop since we AFAIK we don't have the concept of a PATH_MAX since it's so platform-dependent and could actually leak what host we're running on, but I'm not sure this loop is at all necessary on actual OSes. Plus, doesn't it lead to unnecessary allocs which could have been easily handled using static buffers instead like it was prior to this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

For WASI, I want to avoid PATH_MAX because the actual value of PATH_MAX depends on the underlying host OS, so it's better for WASI code to avoid making size assumptions. But you're right that other platforms don't need this. I've now made this code conditional to restore stack allocation.

@sunfishcode
Copy link
Member Author

Thanks for the review! I've now responded to all of the comments :-).

@sunfishcode
Copy link
Member Author

Closing, as this PR is out of date with my latest WASI porting work.

@sunfishcode sunfishcode deleted the yanix-wasi branch September 30, 2020 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasi Issues pertaining to WASI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants