Skip to content
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

mmap addr #1870

Merged
merged 1 commit into from
Nov 20, 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 @@ -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

Expand Down
13 changes: 9 additions & 4 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/sys/test_mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down