Skip to content

mman: Fix mman tests to be runnable #1535

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Fixed potential undefined behavior in `Signal::try_from` on some platforms.
(#[1484](https://github.com/nix-rust/nix/pull/1484))

- Fixed mman tests to be runnable.
(#[1535](https://github.com/nix-rust/nix/pull/1535))

### Removed

- Removed a couple of termios constants on redox that were never actually
Expand Down
3 changes: 3 additions & 0 deletions test/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ mod test_pthread;
mod test_ptrace;
#[cfg(any(target_os = "android", target_os = "linux"))]
mod test_timerfd;
#[cfg(any(target_os = "linux",
target_os = "netbsd"))]
mod test_mman;
25 changes: 15 additions & 10 deletions test/sys/test_mman.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use nix::Error;
use nix::libc::{c_void, size_t};
use nix::sys::mman::{mmap, MapFlags, ProtFlags};

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
use nix::sys::mman::{mremap, MRemapFlags};

#[test]
fn test_mmap_anonymous() {
let ref mut byte = unsafe {
let ptr = mmap(std::ptr::null_mut(), 1,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS, -1, 0)
MapFlags::MAP_PRIVATE | MapFlags::MAP_ANON, -1, 0)
.unwrap();
*(ptr as * mut u8)
};
Expand All @@ -26,7 +25,7 @@ fn test_mremap_grow() {
let slice : &mut[u8] = unsafe {
let mem = mmap(std::ptr::null_mut(), ONE_K,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE, -1, 0)
MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE, -1, 0)
.unwrap();
std::slice::from_raw_parts_mut(mem as * mut u8, ONE_K)
};
Expand Down Expand Up @@ -56,13 +55,19 @@ fn test_mremap_grow() {
}

#[test]
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
// calling mremap to shrink could cause segfaults on some 32bit architectures likely due to qemu bug
// refs: https://bugs.launchpad.net/qemu/+bug/1876373
#[cfg(all(any(target_os = "linux", target_os = "netbsd"),
not(any(target_arch = "mips",
target_arch = "mipsel",
target_arch = "arm",
target_arch = "armv7"))))]
fn test_mremap_shrink() {
const ONE_K : size_t = 1024;
let slice : &mut[u8] = unsafe {
let mem = mmap(std::ptr::null_mut(), 10 * ONE_K,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE, -1, 0)
MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE, -1, 0)
.unwrap();
std::slice::from_raw_parts_mut(mem as * mut u8, ONE_K)
};
Expand All @@ -75,12 +80,12 @@ fn test_mremap_shrink() {
let mem = mremap(slice.as_mut_ptr() as * mut c_void, 10 * ONE_K, ONE_K,
MRemapFlags::empty(), None)
.unwrap();
// Since we didn't supply MREMAP_MAYMOVE, the address should be the
// same.
#[cfg(target_os = "linux")]
#[cfg(target_os = "netbsd")]
let mem = mremap(slice.as_mut_ptr() as * mut c_void, 10 * ONE_K, ONE_K,
MRemapFlags::MAP_FIXED), None)
MRemapFlags::MAP_FIXED, None)
.unwrap();
// Since we didn't supply MREMAP_MAYMOVE, the address should be the
// same.
assert_eq !(mem, slice.as_mut_ptr() as * mut c_void);
std::slice::from_raw_parts_mut(mem as * mut u8, ONE_K)
};
Expand Down