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

Use I/O safety in sys::mman #1916

Merged
merged 1 commit into from
Dec 6, 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
21 changes: 14 additions & 7 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::{num::NonZeroUsize, os::unix::io::RawFd};
use std::{num::NonZeroUsize, os::unix::io::{AsRawFd, AsFd}};

libc_bitflags! {
/// Desired memory protection of a memory mapping.
Expand Down Expand Up @@ -416,17 +416,18 @@ pub fn munlockall() -> Result<()> {
/// See the [`mmap(2)`] man page for detailed requirements.
///
/// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html
pub unsafe fn mmap(
pub unsafe fn mmap<F: AsFd>(
addr: Option<NonZeroUsize>,
length: NonZeroUsize,
prot: ProtFlags,
flags: MapFlags,
fd: RawFd,
f: Option<&F>,
offset: off_t,
) -> Result<*mut 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);

Expand Down Expand Up @@ -518,11 +519,12 @@ pub unsafe fn madvise(
/// # use nix::libc::size_t;
/// # use nix::sys::mman::{mmap, mprotect, MapFlags, ProtFlags};
/// # use std::ptr;
/// # use std::os::unix::io::BorrowedFd;
/// const ONE_K: size_t = 1024;
/// let one_k_non_zero = std::num::NonZeroUsize::new(ONE_K).unwrap();
/// let mut slice: &mut [u8] = unsafe {
/// let mem = mmap(None, one_k_non_zero, ProtFlags::PROT_NONE,
/// MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE, -1, 0).unwrap();
/// 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)
/// };
Expand Down Expand Up @@ -566,9 +568,11 @@ pub fn shm_open<P>(
name: &P,
flag: OFlag,
mode: Mode
) -> Result<RawFd>
) -> Result<std::os::unix::io::OwnedFd>
where P: ?Sized + NixPath
{
use std::os::unix::io::{FromRawFd, OwnedFd};

let ret = name.with_nix_path(|cstr| {
#[cfg(any(target_os = "macos", target_os = "ios"))]
unsafe {
Expand All @@ -580,7 +584,10 @@ pub fn shm_open<P>(
}
})?;

Errno::result(ret)
match ret {
-1 => Err(Errno::last()),
fd => Ok(unsafe{ OwnedFd::from_raw_fd(fd) })
}
}
}

Expand Down
14 changes: 7 additions & 7 deletions test/sys/test_mman.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use nix::sys::mman::{mmap, MapFlags, ProtFlags};
use std::num::NonZeroUsize;
use std::{num::NonZeroUsize, os::unix::io::BorrowedFd};

#[test]
fn test_mmap_anonymous() {
unsafe {
let ptr = mmap(
let ptr = mmap::<BorrowedFd>(
None,
NonZeroUsize::new(1).unwrap(),
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS,
-1,
None,
0,
)
.unwrap() as *mut u8;
Expand All @@ -29,12 +29,12 @@ fn test_mremap_grow() {
let one_k_non_zero = NonZeroUsize::new(ONE_K).unwrap();

let slice: &mut [u8] = unsafe {
let mem = mmap(
let mem = mmap::<BorrowedFd>(
None,
one_k_non_zero,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE,
-1,
None,
0,
)
.unwrap();
Expand Down Expand Up @@ -87,12 +87,12 @@ fn test_mremap_shrink() {
const ONE_K: size_t = 1024;
let ten_one_k = NonZeroUsize::new(10 * ONE_K).unwrap();
let slice: &mut [u8] = unsafe {
let mem = mmap(
let mem = mmap::<BorrowedFd>(
None,
ten_one_k,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE,
-1,
None,
0,
)
.unwrap();
Expand Down