Skip to content

Commit

Permalink
mmap non-zero length
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanWoollett-Light committed Nov 20, 2022
1 parent 9e75299 commit eb489a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
9 changes: 5 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 @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 12 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(
std::ptr::null_mut(),
1,
NonZeroUsize::new(1).unwrap(),
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(
std::ptr::null_mut(),
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,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,
Expand All @@ -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,
Expand Down

0 comments on commit eb489a9

Please sign in to comment.