From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/rustix/tests/mm/mlock.rs | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 vendor/rustix/tests/mm/mlock.rs (limited to 'vendor/rustix/tests/mm/mlock.rs') diff --git a/vendor/rustix/tests/mm/mlock.rs b/vendor/rustix/tests/mm/mlock.rs new file mode 100644 index 000000000..c4a1a83b5 --- /dev/null +++ b/vendor/rustix/tests/mm/mlock.rs @@ -0,0 +1,79 @@ +//! Tests for `mlock`. +//! +//! We can't easily test that it actually locks memory, but we can test that we +//! can call it and either get success or a reasonable error message. + +use std::ffi::c_void; + +#[test] +fn test_mlock() { + let mut buf = vec![0_u8; 4096]; + + unsafe { + match rustix::mm::mlock(buf.as_mut_ptr().cast::(), buf.len()) { + Ok(()) => rustix::mm::munlock(buf.as_mut_ptr().cast::(), buf.len()).unwrap(), + // Tests won't always have enough memory or permissions, and that's ok. + Err(rustix::io::Errno::PERM) | Err(rustix::io::Errno::NOMEM) => {} + // But they shouldn't fail otherwise. + Err(other) => Err(other).unwrap(), + } + } +} + +#[cfg(any(target_os = "android", target_os = "linux"))] +#[test] +fn test_mlock_with() { + let mut buf = vec![0_u8; 4096]; + + unsafe { + match rustix::mm::mlock_with( + buf.as_mut_ptr().cast::(), + buf.len(), + rustix::mm::MlockFlags::empty(), + ) { + Ok(()) => rustix::mm::munlock(buf.as_mut_ptr().cast::(), buf.len()).unwrap(), + // Tests won't always have enough memory or permissions, and that's ok. + Err(rustix::io::Errno::PERM) + | Err(rustix::io::Errno::NOMEM) + | Err(rustix::io::Errno::NOSYS) => {} + // But they shouldn't fail otherwise. + Err(other) => Err(other).unwrap(), + } + } +} + +#[cfg(any(target_os = "android", target_os = "linux"))] +#[test] +fn test_mlock_with_onfault() { + // With glibc, `mlock2` with `MLOCK_ONFAULT` returns `EINVAL` if the + // `mlock2` system call returns `ENOSYS`. That's not what we want + // here though, because `ENOSYS` just means the OS doesn't have + // `mlock2`, while `EINVAL` may indicate a bug in rustix. + // + // To work around this, we use `libc::syscall` to make a `mlock2` + // syscall directly to test for `ENOSYS`, before running the main + // test below. + unsafe { + if libc::syscall(libc::SYS_mlock2, 0, 0) == -1 && libc_errno::errno().0 == libc::ENOSYS { + return; + } + } + + let mut buf = vec![0_u8; 4096]; + + unsafe { + match rustix::mm::mlock_with( + buf.as_mut_ptr().cast::(), + buf.len(), + rustix::mm::MlockFlags::ONFAULT, + ) { + Ok(()) => rustix::mm::munlock(buf.as_mut_ptr().cast::(), buf.len()).unwrap(), + // Tests won't always have enough memory or permissions, and that's ok. + Err(rustix::io::Errno::PERM) + | Err(rustix::io::Errno::NOMEM) + | Err(rustix::io::Errno::NOSYS) => {} + // But they shouldn't fail otherwise. + Err(other) => Err(other).unwrap(), + } + } +} -- cgit v1.2.3