Fix the return value from wasi-common's fd_readdir.

`fd_readdir` returns a "bufused" value, which indicates the number of
bytes read into the buffer. WASI libc expects this value to be equal
to the size of the buffer if the end of the directory has not yet
been scanned.

Previously, wasi-common's `fd_readdir` was writing as many complete
entries as it could fit and then stopping, but this meant it was
returning size less than the buffer size even when the directory had
more entries. This patch makes it continue writing up until the end
of the buffer, and return that number of bytes, to let WASI libc
know that there's more to be read.

Fixes #2493.
This commit is contained in:
Dan Gohman
2020-12-09 16:49:59 -08:00
parent 9ccef597c2
commit c3f0471ff2
2 changed files with 91 additions and 15 deletions

View File

@@ -26,15 +26,20 @@ impl<'a> Iterator for ReadDir<'a> {
fn next(&mut self) -> Option<DirEntry> { fn next(&mut self) -> Option<DirEntry> {
unsafe { unsafe {
if self.buf.is_empty() { if self.buf.len() < mem::size_of::<wasi::Dirent>() {
return None; return None;
} }
// Read the data // Read the data
let dirent_ptr = self.buf.as_ptr() as *const wasi::Dirent; let dirent_ptr = self.buf.as_ptr() as *const wasi::Dirent;
let dirent = dirent_ptr.read_unaligned(); let dirent = dirent_ptr.read_unaligned();
if self.buf.len() < mem::size_of::<wasi::Dirent>() + dirent.d_namlen as usize {
return None;
}
let name_ptr = dirent_ptr.offset(1) as *const u8; let name_ptr = dirent_ptr.offset(1) as *const u8;
// NOTE Linux syscall returns a NULL-terminated name, but WASI doesn't // NOTE Linux syscall returns a NUL-terminated name, but WASI doesn't
let namelen = dirent.d_namlen as usize; let namelen = dirent.d_namlen as usize;
let slice = slice::from_raw_parts(name_ptr, namelen); let slice = slice::from_raw_parts(name_ptr, namelen);
let name = str::from_utf8(slice).expect("invalid utf8").to_owned(); let name = str::from_utf8(slice).expect("invalid utf8").to_owned();
@@ -48,21 +53,24 @@ impl<'a> Iterator for ReadDir<'a> {
} }
} }
unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> Vec<DirEntry> { /// Return the entries plus a bool indicating EOF.
unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> (Vec<DirEntry>, bool) {
let mut buf: [u8; BUF_LEN] = [0; BUF_LEN]; let mut buf: [u8; BUF_LEN] = [0; BUF_LEN];
let bufused = let bufused =
wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir"); wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir");
let sl = slice::from_raw_parts(buf.as_ptr(), min(BUF_LEN, bufused)); let sl = slice::from_raw_parts(buf.as_ptr(), min(BUF_LEN, bufused));
let dirs: Vec<_> = ReadDir::from_slice(sl).collect(); let dirs: Vec<_> = ReadDir::from_slice(sl).collect();
dirs let eof = bufused < BUF_LEN;
(dirs, eof)
} }
unsafe fn test_fd_readdir(dir_fd: wasi::Fd) { unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
let stat = wasi::fd_filestat_get(dir_fd).expect("failed filestat"); let stat = wasi::fd_filestat_get(dir_fd).expect("failed filestat");
// Check the behavior in an empty directory // Check the behavior in an empty directory
let mut dirs = exec_fd_readdir(dir_fd, 0); let (mut dirs, eof) = exec_fd_readdir(dir_fd, 0);
assert!(eof, "expected to read the entire directory");
dirs.sort_by_key(|d| d.name.clone()); dirs.sort_by_key(|d| d.name.clone());
assert_eq!(dirs.len(), 2, "expected two entries in an empty directory"); assert_eq!(dirs.len(), 2, "expected two entries in an empty directory");
let mut dirs = dirs.into_iter(); let mut dirs = dirs.into_iter();
@@ -105,9 +113,11 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
); );
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat"); let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat");
wasi::fd_close(file_fd).expect("closing a file");
// Execute another readdir // Execute another readdir
let mut dirs = exec_fd_readdir(dir_fd, 0); let (mut dirs, eof) = exec_fd_readdir(dir_fd, 0);
assert!(eof, "expected to read the entire directory");
assert_eq!(dirs.len(), 3, "expected three entries"); assert_eq!(dirs.len(), 3, "expected three entries");
// Save the data about the last entry. We need to do it before sorting. // Save the data about the last entry. We need to do it before sorting.
let lastfile_cookie = dirs[1].dirent.d_next; let lastfile_cookie = dirs[1].dirent.d_next;
@@ -130,9 +140,56 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
assert_eq!(dir.dirent.d_ino, stat.ino); assert_eq!(dir.dirent.d_ino, stat.ino);
// check if cookie works as expected // check if cookie works as expected
let dirs = exec_fd_readdir(dir_fd, lastfile_cookie); let (dirs, eof) = exec_fd_readdir(dir_fd, lastfile_cookie);
assert!(eof, "expected to read the entire directory");
assert_eq!(dirs.len(), 1, "expected one entry"); assert_eq!(dirs.len(), 1, "expected one entry");
assert_eq!(dirs[0].name, lastfile_name, "name of the only entry"); assert_eq!(dirs[0].name, lastfile_name, "name of the only entry");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}
unsafe fn test_fd_readdir_lots(dir_fd: wasi::Fd) {
let stat = wasi::fd_filestat_get(dir_fd).expect("failed filestat");
// Add a file and check the behavior
for count in 0..1000 {
let file_fd = wasi::path_open(
dir_fd,
0,
&format!("file.{}", count),
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
| wasi::RIGHTS_FD_READDIR
| wasi::RIGHTS_FD_FILESTAT_GET,
0,
0,
)
.expect("failed to create file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
wasi::fd_close(file_fd).expect("closing a file");
}
// Count the entries to ensure that we see the correct number.
let mut total = 0;
let mut cookie = 0;
loop {
let (dirs, eof) = exec_fd_readdir(dir_fd, cookie);
total += dirs.len();
if eof {
break;
}
cookie = dirs[dirs.len()-1].dirent.d_next;
}
assert_eq!(total, 1002, "expected 1000 entries plus . and ..");
for count in 0..1000 {
wasi::path_unlink_file(dir_fd, &format!("file.{}", count)).expect("removing a file");
}
} }
fn main() { fn main() {
@@ -156,4 +213,5 @@ fn main() {
// Run the tests. // Run the tests.
unsafe { test_fd_readdir(dir_fd) } unsafe { test_fd_readdir(dir_fd) }
unsafe { test_fd_readdir_lots(dir_fd) }
} }

View File

@@ -7,6 +7,7 @@ use crate::{path, sched, Error, Result, WasiCtx};
use std::convert::TryInto; use std::convert::TryInto;
use std::io::{self, SeekFrom}; use std::io::{self, SeekFrom};
use std::ops::Deref; use std::ops::Deref;
use std::cmp::min;
use tracing::{debug, trace}; use tracing::{debug, trace};
use wiggle::{GuestPtr, GuestSliceMut}; use wiggle::{GuestPtr, GuestSliceMut};
@@ -304,15 +305,32 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let name_raw = name.as_bytes(); let name_raw = name.as_bytes();
let name_len = name_raw.len().try_into()?; let name_len = name_raw.len().try_into()?;
let offset = dirent_len.checked_add(name_len).ok_or(Error::Overflow)?; let offset = dirent_len.checked_add(name_len).ok_or(Error::Overflow)?;
if (buf_len - bufused) < offset {
break; // Copy as many bytes of the dirent as we can, up to the end of the buffer.
} else { let dirent_copy_len = min(dirent_len, buf_len - bufused);
buf.as_array(dirent_len).copy_from_slice(&dirent_raw)?; buf.as_array(dirent_copy_len).copy_from_slice(&dirent_raw[..dirent_copy_len as usize])?;
buf = buf.add(dirent_len)?;
buf.as_array(name_len).copy_from_slice(name_raw)?; // If the dirent struct wasn't copied entirely, return that we
buf = buf.add(name_len)?; // filled the buffer, which tells libc that we're not at EOF.
bufused += offset; if dirent_copy_len < dirent_len {
return Ok(buf_len);
} }
buf = buf.add(dirent_copy_len)?;
// Copy as many bytes of the name as we can, up to the end of the buffer.
let name_copy_len = min(name_len, buf_len - bufused);
buf.as_array(name_copy_len).copy_from_slice(&name_raw[..name_copy_len as usize])?;
// If the dirent struct wasn't copied entirely, return that we
// filled the buffer, which tells libc that we're not at EOF.
if name_copy_len < name_len {
return Ok(buf_len);
}
buf = buf.add(name_copy_len)?;
bufused += offset;
} }
Ok(bufused) Ok(bufused)