mmap addr

This commit is contained in:
Jonathan 2022-11-20 17:16:21 +00:00
parent fbebb21dd8
commit d34696c84b
3 changed files with 14 additions and 7 deletions

View File

@ -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<NonZeroUsize>`.
([#1870](https://github.com/nix-rust/nix/pull/1870))
### Fixed

View File

@ -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<NonZeroUsize>,
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)

View File

@ -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,