From eb489a9d511dfc581da178257f861cb7e6ee59f5 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Sat, 19 Nov 2022 23:53:46 +0000 Subject: [PATCH] mmap non-zero length --- src/sys/mman.rs | 9 +++++---- test/sys/test_mman.rs | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/sys/mman.rs b/src/sys/mman.rs index 869f44c4b7..e997f0ca28 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -8,7 +8,7 @@ use crate::Result; #[cfg(feature = "fs")] use crate::{fcntl::OFlag, sys::stat::Mode}; use libc::{self, c_int, c_void, off_t, size_t}; -use std::os::unix::io::RawFd; +use std::{os::unix::io::RawFd, num::NonZeroUsize}; libc_bitflags! { /// Desired memory protection of a memory mapping. @@ -418,13 +418,13 @@ pub fn munlockall() -> Result<()> { /// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html pub unsafe fn mmap( addr: *mut c_void, - length: size_t, + length: NonZeroUsize, prot: ProtFlags, flags: MapFlags, fd: RawFd, offset: off_t, ) -> Result<*mut c_void> { - let ret = libc::mmap(addr, length, prot.bits(), flags.bits(), fd, offset); + let ret = libc::mmap(addr, length.into(), prot.bits(), flags.bits(), fd, offset); if ret == libc::MAP_FAILED { Err(Errno::last()) @@ -515,8 +515,9 @@ pub unsafe fn madvise( /// # use nix::sys::mman::{mmap, mprotect, MapFlags, ProtFlags}; /// # use std::ptr; /// const ONE_K: size_t = 1024; +/// let one_k_non_zero = std::num::NonZeroUsize::new(1024).unwrap(); /// let mut slice: &mut [u8] = unsafe { -/// let mem = mmap(ptr::null_mut(), ONE_K, ProtFlags::PROT_NONE, +/// let mem = mmap(ptr::null_mut(), 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) diff --git a/test/sys/test_mman.rs b/test/sys/test_mman.rs index 75cbf6ce8c..e76aba0ab1 100644 --- a/test/sys/test_mman.rs +++ b/test/sys/test_mman.rs @@ -1,11 +1,12 @@ use nix::sys::mman::{mmap, MapFlags, ProtFlags}; +use std::num::NonZeroUsize; #[test] fn test_mmap_anonymous() { unsafe { let ptr = mmap( std::ptr::null_mut(), - 1, + NonZeroUsize::new(1).unwrap(), ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS, -1, @@ -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( std::ptr::null_mut(), - ONE_K, + one_k_non_zero, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE, -1, @@ -79,12 +82,17 @@ 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 one_k_non_zero = NonZeroUsize::new(ONE_K).unwrap(); + let ten_one_k = one_k_non_zero + .checked_mul(NonZeroUsize::new(10).unwrap()) + .unwrap(); let slice: &mut [u8] = unsafe { let mem = mmap( std::ptr::null_mut(), - 10 * ONE_K, + ten_one_k, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE, -1, @@ -100,7 +108,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,