aya: move Mmap struct to a utility module

reviewable/pr1261/r1
Omri Steiner 4 weeks ago
parent 583709f6a0
commit a599fb95a4

@ -60,9 +60,7 @@ use std::{
};
use aya_obj::{EbpfSectionKind, InvalidTypeBinding, generated::bpf_map_type, parse_map_info};
use libc::{
MAP_FAILED, RLIM_INFINITY, RLIMIT_MEMLOCK, c_int, c_void, getrlimit, off_t, rlim_t, rlimit,
};
use libc::{RLIM_INFINITY, RLIMIT_MEMLOCK, getrlimit, rlim_t, rlimit};
use log::warn;
use thiserror::Error;
@ -71,7 +69,7 @@ use crate::{
pin::PinError,
sys::{
SyscallError, bpf_create_map, bpf_get_object, bpf_map_freeze, bpf_map_get_fd_by_id,
bpf_map_get_next_key, bpf_map_update_elem_ptr, bpf_pin_object, mmap, munmap,
bpf_map_get_next_key, bpf_map_update_elem_ptr, bpf_pin_object,
},
util::{KernelVersion, nr_cpus},
};
@ -944,62 +942,6 @@ impl<T: Pod> Deref for PerCpuValues<T> {
}
}
// MMap corresponds to a memory-mapped region.
//
// The data is unmapped in Drop.
#[cfg_attr(test, derive(Debug))]
struct MMap {
ptr: ptr::NonNull<c_void>,
len: usize,
}
// Needed because NonNull<T> is !Send and !Sync out of caution that the data
// might be aliased unsafely.
unsafe impl Send for MMap {}
unsafe impl Sync for MMap {}
impl MMap {
fn new(
fd: BorrowedFd<'_>,
len: usize,
prot: c_int,
flags: c_int,
offset: off_t,
) -> Result<Self, SyscallError> {
match unsafe { mmap(ptr::null_mut(), len, prot, flags, fd, offset) } {
MAP_FAILED => Err(SyscallError {
call: "mmap",
io_error: io::Error::last_os_error(),
}),
ptr => {
let ptr = ptr::NonNull::new(ptr).ok_or(
// This should never happen, but to be paranoid, and so we never need to talk
// about a null pointer, we check it anyway.
SyscallError {
call: "mmap",
io_error: io::Error::other("mmap returned null pointer"),
},
)?;
Ok(Self { ptr, len })
}
}
}
}
impl AsRef<[u8]> for MMap {
fn as_ref(&self) -> &[u8] {
let Self { ptr, len } = self;
unsafe { std::slice::from_raw_parts(ptr.as_ptr().cast(), *len) }
}
}
impl Drop for MMap {
fn drop(&mut self) {
let Self { ptr, len } = *self;
unsafe { munmap(ptr.as_ptr(), len) };
}
}
#[cfg(test)]
mod test_utils {
use aya_obj::{

@ -14,8 +14,8 @@ use libc::{MAP_SHARED, PROT_READ, PROT_WRITE};
use thiserror::Error;
use crate::{
maps::MMap,
sys::{PerfEventIoctlRequest, SyscallError, perf_event_ioctl, perf_event_open_bpf},
util::mmap::MMap,
};
/// Perf buffer error.

@ -17,8 +17,8 @@ use aya_obj::generated::{BPF_RINGBUF_BUSY_BIT, BPF_RINGBUF_DISCARD_BIT, BPF_RING
use libc::{MAP_SHARED, PROT_READ, PROT_WRITE};
use crate::{
maps::{MMap, MapData, MapError},
util::page_size,
maps::{MapData, MapError},
util::{mmap::MMap, page_size},
};
/// A map that can be used to receive events from eBPF programs.

@ -18,6 +18,8 @@ use log::warn;
use crate::Pod;
pub mod mmap;
/// Represents a kernel version, in major.minor.release version.
// Adapted from https://docs.rs/procfs/latest/procfs/sys/kernel/struct.Version.html.
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd)]

@ -0,0 +1,62 @@
//! Safe wrapper around memory-mapped files.
use libc::{MAP_FAILED, c_int, c_void, off_t};
use std::{io, os::fd::BorrowedFd, ptr};
use crate::sys::{SyscallError, mmap, munmap};
// MMap corresponds to a memory-mapped region.
//
// The data is unmapped in Drop.
#[cfg_attr(test, derive(Debug))]
pub(crate) struct MMap {
pub(crate) ptr: ptr::NonNull<c_void>,
pub(crate) len: usize,
}
// Needed because NonNull<T> is !Send and !Sync out of caution that the data
// might be aliased unsafely.
unsafe impl Send for MMap {}
unsafe impl Sync for MMap {}
impl MMap {
pub(crate) fn new(
fd: BorrowedFd<'_>,
len: usize,
prot: c_int,
flags: c_int,
offset: off_t,
) -> Result<Self, SyscallError> {
match unsafe { mmap(ptr::null_mut(), len, prot, flags, fd, offset) } {
MAP_FAILED => Err(SyscallError {
call: "mmap",
io_error: io::Error::last_os_error(),
}),
ptr => {
let ptr = ptr::NonNull::new(ptr).ok_or(
// This should never happen, but to be paranoid, and so we never need to talk
// about a null pointer, we check it anyway.
SyscallError {
call: "mmap",
io_error: io::Error::other("mmap returned null pointer"),
},
)?;
Ok(Self { ptr, len })
}
}
}
}
impl AsRef<[u8]> for MMap {
fn as_ref(&self) -> &[u8] {
let Self { ptr, len } = self;
unsafe { std::slice::from_raw_parts(ptr.as_ptr().cast(), *len) }
}
}
impl Drop for MMap {
fn drop(&mut self) {
let Self { ptr, len } = *self;
unsafe { munmap(ptr.as_ptr(), len) };
}
}
Loading…
Cancel
Save