From d56f394f69bdda7f9ace677387c7bba63b2f8eab Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 24 Sep 2024 12:36:31 -0700 Subject: [PATCH] Various fixes. --- src/backend/linux_raw/c.rs | 1 + src/event/select.rs | 30 ++++++++++++++++++++++++------ src/lib.rs | 2 +- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/backend/linux_raw/c.rs b/src/backend/linux_raw/c.rs index 4035bf945..95f701b16 100644 --- a/src/backend/linux_raw/c.rs +++ b/src/backend/linux_raw/c.rs @@ -9,6 +9,7 @@ pub(crate) type size_t = usize; pub(crate) use linux_raw_sys::ctypes::*; pub(crate) use linux_raw_sys::errno::EINVAL; +pub(crate) use linux_raw_sys::general::{__kernel_fd_set as fd_set, __FD_SETSIZE as FD_SETSIZE}; pub(crate) use linux_raw_sys::ioctl::{FIONBIO, FIONREAD}; // Import the kernel's `uid_t` and `gid_t` if they're 32-bit. #[cfg(not(any(target_arch = "arm", target_arch = "sparc", target_arch = "x86")))] diff --git a/src/event/select.rs b/src/event/select.rs index 466cd72db..c8dc4fcc8 100644 --- a/src/event/select.rs +++ b/src/event/select.rs @@ -5,7 +5,7 @@ //! `select` is unsafe due to I/O safety. #![allow(unsafe_code)] -#[cfg(linux_like)] +#[cfg(any(linux_like, target_os = "wasi"))] use crate::backend::c; use crate::fd::RawFd; use crate::{backend, io}; @@ -23,7 +23,7 @@ struct FD_SET { /// The wasi-libc headers call this `__nfds`. fd_count: usize, /// The wasi-libc headers call this `__fds`. - fd_array: [i32; libc::FD_SETSIZE], + fd_array: [i32; c::FD_SETSIZE], } #[cfg(windows)] @@ -233,7 +233,10 @@ pub fn fd_set_num_elements(set_count: usize, nfds: RawFd) -> usize { pub(crate) fn fd_set_num_elements_for_fd_array(set_count: usize) -> usize { // Allocate space for an `fd_count` field, plus `set_count` elements // for the `fd_array` field. - 1 + set_count + div_ceil( + align_of::() + set_count * size_of::(), + size_of::(), + ) } /// `fd_set_num_elements` implementation on platforms with bitvector @@ -246,7 +249,6 @@ pub(crate) fn fd_set_num_elements_for_bitvector(nfds: RawFd) -> usize { div_ceil(nfds, BITS) } -#[cfg(not(any(windows, target_os = "wasi")))] fn div_ceil(lhs: usize, rhs: usize) -> usize { let d = lhs / rhs; let r = lhs % rhs; @@ -333,19 +335,35 @@ impl<'a> Iterator for FdSetIter<'a> { #[cfg(test)] mod test { use super::*; - use core::mem::align_of; + use core::mem::{align_of, size_of}; #[test] #[cfg(any(windows, target_os = "wasi"))] fn layouts() { // The `FdSetElement` array should be suitably aligned. assert_eq!(align_of::(), align_of::()); + + // The layout of `FD_SET` should match our layout of a set of the same + // size. + assert_eq!( + fd_set_num_elements_for_fd_array( + memoffset::span_of!(FD_SET, fd_array).len() / size_of::() + ) * size_of::(), + size_of::() + ); } #[test] #[cfg(any(bsd, linux_kernel))] fn layouts() { // The `FdSetElement` array should be suitably aligned. - assert_eq!(align_of::(), align_of::()); + assert_eq!(align_of::(), align_of::()); + + // The layout of `fd_set` should match our layout of a set of the same + // size. + assert_eq!( + fd_set_num_elements_for_bitvector(c::FD_SETSIZE as RawFd) * size_of::(), + size_of::() + ); } } diff --git a/src/lib.rs b/src/lib.rs index e92162556..e53e263ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -362,7 +362,7 @@ mod signal; feature = "runtime", feature = "thread", feature = "time", - all(feature = "event", any(bsd, linux_kernel, windows)), + all(feature = "event", any(bsd, linux_kernel, windows, target_os = "wasi")), all( linux_raw, not(feature = "use-libc-auxv"),