Skip to content

Commit

Permalink
Fix some test failures on illumos. (#740)
Browse files Browse the repository at this point in the history
`mlock` fails if the memory is not aligned. `tcgetattr` doesn't work on
pseudoterminals. A few errno values are different. And disable some tests
that are disabled on other platforms too.
  • Loading branch information
sunfishcode authored Jul 15, 2023
1 parent 05681c8 commit 7914500
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/mm/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ pub unsafe fn mprotect(ptr: *mut c_void, len: usize, flags: MprotectFlags) -> io
///
/// Some implementations implicitly round the memory region out to the nearest
/// page boundaries, so this function may lock more memory than explicitly
/// requested if the memory isn't page-aligned.
/// requested if the memory isn't page-aligned. Other implementations fail if
/// the memory isn't page-aligned.
///
/// # References
/// - [POSIX]
Expand Down
9 changes: 7 additions & 2 deletions tests/fs/mknodat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ fn test_mknodat() {
let tmp = tempfile::tempdir().unwrap();
let dir = openat(CWD, tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap();

// Create a regular file. Not supported on FreeBSD or OpenBSD.
#[cfg(not(any(target_os = "freebsd", target_os = "openbsd", target_os = "solaris")))]
// Create a regular file. Not supported on FreeBSD, OpenBSD, or illumos.
#[cfg(not(any(
target_os = "freebsd",
target_os = "illumos",
target_os = "openbsd",
target_os = "solaris"
)))]
{
mknodat(&dir, "foo", FileType::RegularFile, Mode::empty(), 0).unwrap();
let stat = statat(&dir, "foo", AtFlags::empty()).unwrap();
Expand Down
10 changes: 8 additions & 2 deletions tests/mm/mlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ use std::ffi::c_void;
#[test]
fn test_mlock() {
let mut buf = vec![0_u8; 4096];
let ptr = buf.as_mut_ptr();

// On Linux, `mlock` automatically rounds the address down to the nearest
// page size. On other platforms, we need to do it manually.
#[cfg(not(linux_kernel))]
let ptr = ((ptr as usize) & (-4096_isize) as usize) as *mut u8;

unsafe {
match rustix::mm::mlock(buf.as_mut_ptr().cast::<c_void>(), buf.len()) {
Ok(()) => rustix::mm::munlock(buf.as_mut_ptr().cast::<c_void>(), buf.len()).unwrap(),
match rustix::mm::mlock(ptr.cast::<c_void>(), buf.len()) {
Ok(()) => rustix::mm::munlock(ptr.cast::<c_void>(), buf.len()).unwrap(),
// Tests won't always have enough memory or permissions, and that's ok.
Err(rustix::io::Errno::PERM | rustix::io::Errno::NOMEM) => {}
// But they shouldn't fail otherwise.
Expand Down
9 changes: 7 additions & 2 deletions tests/mm/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ fn test_mlock() {
unsafe {
let addr = mmap_anonymous(null_mut(), 8192, ProtFlags::READ, MapFlags::PRIVATE).unwrap();

mlock(addr, 8192).unwrap();
munlock(addr, 8192).unwrap();
match mlock(addr, 8192) {
Ok(()) => munlock(addr, 8192).unwrap(),
// Tests won't always have enough memory or permissions, and that's ok.
Err(rustix::io::Errno::PERM | rustix::io::Errno::NOMEM) => (),
// But they shouldn't fail otherwise.
Err(other) => Err(other).unwrap(),
}

#[cfg(linux_kernel)]
{
Expand Down
10 changes: 5 additions & 5 deletions tests/net/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ fn test_sockopts_ipv4() {
assert!(!rustix::net::sockopt::get_socket_passcred(&s).unwrap());
assert_ne!(rustix::net::sockopt::get_ip_ttl(&s).unwrap(), 0);
assert_ne!(rustix::net::sockopt::get_ip_ttl(&s).unwrap(), 77);
#[cfg(not(any(bsd, windows)))]
#[cfg(not(any(bsd, windows, target_os = "illumos")))]
assert!(rustix::net::sockopt::get_ip_multicast_loop(&s).unwrap());
#[cfg(not(any(bsd, windows)))]
#[cfg(not(any(bsd, windows, target_os = "illumos")))]
assert_eq!(rustix::net::sockopt::get_ip_multicast_ttl(&s).unwrap(), 1);
assert!(!rustix::net::sockopt::get_tcp_nodelay(&s).unwrap());
// On a new socket we shouldn't have an error yet.
Expand Down Expand Up @@ -111,7 +111,7 @@ fn test_sockopts_ipv4() {
// Check the ip ttl.
assert_eq!(rustix::net::sockopt::get_ip_ttl(&s).unwrap(), 77);

#[cfg(not(any(bsd, windows)))]
#[cfg(not(any(bsd, windows, target_os = "illumos")))]
{
// Set the multicast loop flag;
rustix::net::sockopt::set_ip_multicast_loop(&s, false).unwrap();
Expand Down Expand Up @@ -152,6 +152,7 @@ fn test_sockopts_ipv6() {
Ok(multicast_loop) => assert!(multicast_loop),
Err(rustix::io::Errno::OPNOTSUPP) => (),
Err(rustix::io::Errno::INVAL) => (),
Err(rustix::io::Errno::NOPROTOOPT) => (),
Err(err) => Err(err).unwrap(),
}
assert_ne!(rustix::net::sockopt::get_ipv6_unicast_hops(&s).unwrap(), 0);
Expand All @@ -175,13 +176,12 @@ fn test_sockopts_ipv6() {
// Check that the IPV6 multicast loop value is set.
match rustix::net::sockopt::get_ipv6_multicast_loop(&s) {
Ok(multicast_loop) => assert!(!multicast_loop),
Err(rustix::io::Errno::OPNOTSUPP) => (),
Err(rustix::io::Errno::INVAL) => (),
Err(err) => Err(err).unwrap(),
}
}
Err(rustix::io::Errno::OPNOTSUPP) => (),
Err(rustix::io::Errno::INVAL) => (),
Err(rustix::io::Errno::NOPROTOOPT) => (),
Err(err) => Err(err).unwrap(),
}

Expand Down
3 changes: 2 additions & 1 deletion tests/net/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ fn do_test_unix_msg(addr: SocketAddrUnix) {
);
// Don't ask me why, but this was seen to fail on FreeBSD.
// `SocketAddrUnix::path()` returned `None` for some reason.
#[cfg(not(target_os = "freebsd"))]
// illumos too.
#[cfg(not(any(target_os = "freebsd", target_os = "illumos")))]
assert_eq!(
Some(rustix::net::SocketAddrAny::Unix(addr.clone())),
result.address
Expand Down
3 changes: 3 additions & 0 deletions tests/termios/termios.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Disable on illumos where `tcgetattr` doesn't appear to support
// pseudoterminals.
#[cfg(not(target_os = "illumos"))]
#[test]
fn test_termios_speeds() {
use rustix::pty::*;
Expand Down
20 changes: 10 additions & 10 deletions tests/time/settime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use rustix::time::{clock_settime, ClockId, Timespec};
#[test]
fn test_settime() {
// Monotonic clocks are never settable.
assert_eq!(
clock_settime(
ClockId::Monotonic,
Timespec {
tv_sec: 0,
tv_nsec: 0
}
),
Err(io::Errno::INVAL)
);
match clock_settime(
ClockId::Monotonic,
Timespec {
tv_sec: 0,
tv_nsec: 0,
},
) {
Err(io::Errno::INVAL | io::Errno::PERM) => (),
_otherwise => panic!(),
}
}

0 comments on commit 7914500

Please sign in to comment.