Skip to content

mmap non-zero length #1873

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

Merged
merged 1 commit into from
Nov 21, 2022
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
([#1792](https://github.com/nix-rust/nix/pull/1792))
- The `addr` argument of `sys::mman::mmap` is now of type `Option<NonZeroUsize>`.
([#1870](https://github.com/nix-rust/nix/pull/1870))
- The `length` argument of `sys::mman::mmap` is now of type `NonZeroUsize`.
([#1873](https://github.com/nix-rust/nix/pull/1873))

### Fixed

Expand Down
9 changes: 5 additions & 4 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ pub fn munlockall() -> Result<()> {
/// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html
pub unsafe fn mmap(
addr: Option<NonZeroUsize>,
length: size_t,
length: NonZeroUsize,
prot: ProtFlags,
flags: MapFlags,
fd: RawFd,
Expand All @@ -428,8 +428,8 @@ pub unsafe fn mmap(
std::ptr::null_mut(),
|a| usize::from(a) as *mut c_void
);

let ret = libc::mmap(ptr, length, prot.bits(), flags.bits(), fd, offset);
let ret = libc::mmap(ptr, length.into(), prot.bits(), flags.bits(), fd, offset);

if ret == libc::MAP_FAILED {
Err(Errno::last())
Expand Down Expand Up @@ -520,8 +520,9 @@ pub unsafe fn madvise(
/// # use nix::sys::mman::{mmap, mprotect, MapFlags, ProtFlags};
/// # use std::ptr;
/// const ONE_K: size_t = 1024;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ONE_K is no longer used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to fix this.

/// let one_k_non_zero = std::num::NonZeroUsize::new(ONE_K).unwrap();
/// let mut slice: &mut [u8] = unsafe {
/// let mem = mmap(None, ONE_K, ProtFlags::PROT_NONE,
/// let mem = mmap(None, one_k_non_zero, ProtFlags::PROT_NONE,
/// MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE, -1, 0).unwrap();
/// mprotect(mem, ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE).unwrap();
/// std::slice::from_raw_parts_mut(mem as *mut u8, ONE_K)
Expand Down
13 changes: 9 additions & 4 deletions test/sys/test_mman.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use nix::sys::mman::{mmap, MapFlags, ProtFlags};
use std::num::NonZeroUsize;

#[test]
fn test_mmap_anonymous() {
unsafe {
let ptr = mmap(
None,
1,
NonZeroUsize::new(1).unwrap(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would be worthwhile to bring in the nonzero_ext crate just for these tests?

Suggested change
NonZeroUsize::new(1).unwrap(),
nonzero_ext::nonzero!(1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point no, but if NonZeroUsize sees wider usage it may become worthwhile.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I took a look through my other crates and found exactly zero places where nonzero_ext would help. It turns out that Option<NonZeroXXX> is a lot more common than NonZeroXXX.

ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS,
-1,
Expand All @@ -25,10 +26,12 @@ fn test_mremap_grow() {
use nix::sys::mman::{mremap, MRemapFlags};

const ONE_K: size_t = 1024;
let one_k_non_zero = NonZeroUsize::new(ONE_K).unwrap();

let slice: &mut [u8] = unsafe {
let mem = mmap(
None,
ONE_K,
one_k_non_zero,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE,
-1,
Expand Down Expand Up @@ -79,12 +82,14 @@ fn test_mremap_grow() {
fn test_mremap_shrink() {
use nix::libc::{c_void, size_t};
use nix::sys::mman::{mremap, MRemapFlags};
use std::num::NonZeroUsize;

const ONE_K: size_t = 1024;
let ten_one_k = NonZeroUsize::new(10 * ONE_K).unwrap();
let slice: &mut [u8] = unsafe {
let mem = mmap(
None,
10 * ONE_K,
ten_one_k,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE,
-1,
Expand All @@ -100,7 +105,7 @@ fn test_mremap_shrink() {
let slice: &mut [u8] = unsafe {
let mem = mremap(
slice.as_mut_ptr() as *mut c_void,
10 * ONE_K,
ten_one_k.into(),
ONE_K,
MRemapFlags::empty(),
None,
Expand Down