From d34696c84b0d3b49456fa6f0f12af91d94b11371 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Sun, 20 Nov 2022 17:16:21 +0000 Subject: [PATCH] mmap addr --- CHANGELOG.md | 2 ++ src/sys/mman.rs | 13 +++++++++---- test/sys/test_mman.rs | 6 +++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10a95ad934..065f98fc00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ This project adheres to [Semantic Versioning](https://semver.org/). - The MSRV is now 1.56.1 ([#1792](https://github.com/nix-rust/nix/pull/1792)) +- The `addr` argument of `sys::mman::mmap` is now of type `Option`. + ([#1870](https://github.com/nix-rust/nix/pull/1870)) ### Fixed diff --git a/src/sys/mman.rs b/src/sys/mman.rs index 869f44c4b7..dab8f445e1 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. @@ -417,14 +417,19 @@ pub fn munlockall() -> Result<()> { /// /// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html pub unsafe fn mmap( - addr: *mut c_void, + addr: Option, length: size_t, 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 ptr = addr.map_or( + std::ptr::null_mut(), + |a| usize::from(a) as *mut c_void + ); + + let ret = libc::mmap(ptr, length, prot.bits(), flags.bits(), fd, offset); if ret == libc::MAP_FAILED { Err(Errno::last()) @@ -516,7 +521,7 @@ pub unsafe fn madvise( /// # use std::ptr; /// const ONE_K: size_t = 1024; /// let mut slice: &mut [u8] = unsafe { -/// let mem = mmap(ptr::null_mut(), ONE_K, ProtFlags::PROT_NONE, +/// let mem = mmap(None, ONE_K, 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..a43991c248 100644 --- a/test/sys/test_mman.rs +++ b/test/sys/test_mman.rs @@ -4,7 +4,7 @@ use nix::sys::mman::{mmap, MapFlags, ProtFlags}; fn test_mmap_anonymous() { unsafe { let ptr = mmap( - std::ptr::null_mut(), + None, 1, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS, @@ -27,7 +27,7 @@ fn test_mremap_grow() { const ONE_K: size_t = 1024; let slice: &mut [u8] = unsafe { let mem = mmap( - std::ptr::null_mut(), + None, ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE, @@ -83,7 +83,7 @@ fn test_mremap_shrink() { const ONE_K: size_t = 1024; let slice: &mut [u8] = unsafe { let mem = mmap( - std::ptr::null_mut(), + None, 10 * ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE,