Skip to content

Commit

Permalink
feat: mmap NonNull
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanWoollett-Light committed Oct 1, 2023
1 parent 173bde1 commit 099f78b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Added
- Added `Icmp` and `IcmpV6` to `SockProtocol`.
(#[2103](https://github.com/nix-rust/nix/pull/2103))
- `sys::mman::mmap` to return `NonNull<c_void>`.
([#2000](https://github.com/nix-rust/nix/pull/2000))

## [0.27.1] - 2023-08-28

Expand Down Expand Up @@ -123,6 +125,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
`sys::kevent::Kqueue::kevent`, and `sys::event::kqueue` is deprecated in
favor of `sys::kevent::Kqueue::new`.
([#1943](https://github.com/nix-rust/nix/pull/1943))

### Removed

- Removed deprecated IoVec API.
([#1855](https://github.com/nix-rust/nix/pull/1855))
- Removed deprecated net APIs.
Expand Down
15 changes: 6 additions & 9 deletions src/sys/mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::Result;
use crate::{fcntl::OFlag, sys::stat::Mode};
use libc::{self, c_int, c_void, off_t, size_t};
use std::{num::NonZeroUsize, os::unix::io::{AsRawFd, AsFd}};
use std::ptr::NonNull;

libc_bitflags! {
/// Desired memory protection of a memory mapping.
Expand Down Expand Up @@ -425,19 +426,15 @@ pub unsafe fn mmap<F: AsFd>(
flags: MapFlags,
f: Option<F>,
offset: off_t,
) -> Result<*mut c_void> {
) -> Result<NonNull<c_void>> {
let ptr =
addr.map_or(std::ptr::null_mut(), |a| usize::from(a) as *mut c_void);

let fd = f.map(|f| f.as_fd().as_raw_fd()).unwrap_or(-1);
let ret =
libc::mmap(ptr, length.into(), prot.bits(), flags.bits(), fd, offset);

if ret == libc::MAP_FAILED {
Err(Errno::last())
} else {
Ok(ret)
}

NonNull::new(ret).ok_or_else(Errno::last)
}

/// Expands (or shrinks) an existing memory mapping, potentially moving it at
Expand Down Expand Up @@ -527,8 +524,8 @@ pub unsafe fn madvise(
/// let mut slice: &mut [u8] = unsafe {
/// let mem = mmap::<BorrowedFd>(None, one_k_non_zero, ProtFlags::PROT_NONE,
/// MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE, None, 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)
/// mprotect(mem.as_ptr(), ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE).unwrap();
/// std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K)
/// };
/// assert_eq!(slice[0], 0x00);
/// slice[0] = 0xFF;
Expand Down
15 changes: 8 additions & 7 deletions test/sys/test_mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ use std::{num::NonZeroUsize, os::unix::io::BorrowedFd};
#[test]
fn test_mmap_anonymous() {
unsafe {
let ptr = mmap::<BorrowedFd>(
let mut ptr = mmap::<BorrowedFd>(
None,
NonZeroUsize::new(1).unwrap(),
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS,
None,
0,
)
.unwrap() as *mut u8;
assert_eq!(*ptr, 0x00u8);
*ptr = 0xffu8;
assert_eq!(*ptr, 0xffu8);
.unwrap()
.cast::<u8>();
assert_eq!(*ptr.as_ref(), 0x00u8);
*ptr.as_mut() = 0xffu8;
assert_eq!(*ptr.as_ref(), 0xffu8);
}
}

Expand All @@ -38,7 +39,7 @@ fn test_mremap_grow() {
0,
)
.unwrap();
std::slice::from_raw_parts_mut(mem as *mut u8, ONE_K)
std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K)
};
assert_eq!(slice[ONE_K - 1], 0x00);
slice[ONE_K - 1] = 0xFF;
Expand Down Expand Up @@ -96,7 +97,7 @@ fn test_mremap_shrink() {
0,
)
.unwrap();
std::slice::from_raw_parts_mut(mem as *mut u8, ONE_K)
std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K)
};
assert_eq!(slice[ONE_K - 1], 0x00);
slice[ONE_K - 1] = 0xFF;
Expand Down

0 comments on commit 099f78b

Please sign in to comment.