From e12fcf46cb1e0856a8105ed43fda184fa4648713 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Fri, 26 Jul 2024 16:08:51 -0700 Subject: [PATCH] Use MockableFd everywhere Rust 1.80 contains https://github.com/rust-lang/rust/pull/124210, causing tests which we skip under miri to segfault. --- aya/src/bpf.rs | 7 ++-- aya/src/lib.rs | 56 +++++++++++++++++++++---------- aya/src/maps/mod.rs | 22 +++++++----- aya/src/maps/perf/perf_buffer.rs | 1 - aya/src/programs/cgroup_device.rs | 1 + aya/src/programs/extension.rs | 7 ++-- aya/src/programs/links.rs | 12 ++++--- aya/src/programs/lirc_mode2.rs | 8 +++-- aya/src/programs/mod.rs | 12 +++---- aya/src/programs/perf_attach.rs | 10 +++--- aya/src/programs/probe.rs | 6 ++-- aya/src/sys/bpf.rs | 30 ++++++++--------- aya/src/sys/netlink.rs | 6 ++-- aya/src/sys/perf_event.rs | 22 ++++++++---- 14 files changed, 121 insertions(+), 79 deletions(-) diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index 42a9a4899..99d8e0d29 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -3,7 +3,7 @@ use std::{ collections::{HashMap, HashSet}, fs, io, os::{ - fd::{AsFd as _, AsRawFd as _, OwnedFd}, + fd::{AsFd as _, AsRawFd as _}, raw::c_int, }, path::{Path, PathBuf}, @@ -1123,7 +1123,10 @@ pub enum EbpfError { #[deprecated(since = "0.13.0", note = "use `EbpfError` instead")] pub type BpfError = EbpfError; -fn load_btf(raw_btf: Vec, verifier_log_level: VerifierLogLevel) -> Result { +fn load_btf( + raw_btf: Vec, + verifier_log_level: VerifierLogLevel, +) -> Result { let (ret, verifier_log) = retry_with_verifier_logs(10, |logger| { bpf_load_btf(raw_btf.as_slice(), logger, verifier_log_level) }); diff --git a/aya/src/lib.rs b/aya/src/lib.rs index ccceb9e09..361a5fe9e 100644 --- a/aya/src/lib.rs +++ b/aya/src/lib.rs @@ -87,7 +87,7 @@ pub use programs::loaded_programs; mod sys; pub mod util; -use std::os::fd::{AsFd, BorrowedFd, OwnedFd}; +use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd}; pub use bpf::*; pub use obj::btf::{Btf, BtfError}; @@ -123,35 +123,55 @@ impl MockableFd { #[cfg(test)] fn from_fd(fd: OwnedFd) -> Self { - Self { fd: Some(fd) } + let fd = Some(fd); + Self { fd } } #[cfg(not(test))] - fn try_clone(&self) -> std::io::Result { + fn inner(&self) -> &OwnedFd { let Self { fd } = self; - let fd = fd.try_clone()?; - Ok(Self { fd }) + fd } #[cfg(test)] - fn try_clone(&self) -> std::io::Result { + fn inner(&self) -> &OwnedFd { let Self { fd } = self; - let fd = fd.as_ref().map(OwnedFd::try_clone).transpose()?; - Ok(Self { fd }) + fd.as_ref().unwrap() + } + + fn try_clone(&self) -> std::io::Result { + let fd = self.inner(); + let fd = fd.try_clone()?; + Ok(Self::from_fd(fd)) + } +} + +impl From for MockableFd +where + OwnedFd: From, +{ + fn from(value: T) -> Self { + let fd = OwnedFd::from(value); + Self::from_fd(fd) } } impl AsFd for MockableFd { - #[cfg(not(test))] fn as_fd(&self) -> BorrowedFd<'_> { - let Self { fd } = self; - fd.as_fd() + self.inner().as_fd() } +} - #[cfg(test)] - fn as_fd(&self) -> BorrowedFd<'_> { - let Self { fd } = self; - fd.as_ref().unwrap().as_fd() +impl AsRawFd for MockableFd { + fn as_raw_fd(&self) -> RawFd { + self.inner().as_raw_fd() + } +} + +impl FromRawFd for MockableFd { + unsafe fn from_raw_fd(fd: RawFd) -> Self { + let fd = OwnedFd::from_raw_fd(fd); + Self::from_fd(fd) } } @@ -166,8 +186,10 @@ impl Drop for MockableFd { use std::os::fd::AsRawFd as _; let Self { fd } = self; - if fd.as_ref().unwrap().as_raw_fd() >= Self::mock_signed_fd() { - let fd: OwnedFd = fd.take().unwrap(); + let fd = fd.take().unwrap(); + if fd.as_raw_fd() < Self::mock_signed_fd() { + std::mem::drop(fd) + } else { std::mem::forget(fd) } } diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 82467aee8..356662d88 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -215,8 +215,7 @@ pub struct MapFd { } impl MapFd { - fn from_fd(fd: OwnedFd) -> Self { - let fd = crate::MockableFd::from_fd(fd); + fn from_fd(fd: crate::MockableFd) -> Self { Self { fd } } @@ -657,13 +656,21 @@ impl MapData { io_error, })?; - Self::from_fd(fd) + Self::from_fd_inner(fd) } /// Loads a map from a map id. pub fn from_id(id: u32) -> Result { let fd = bpf_map_get_fd_by_id(id)?; - Self::from_fd(fd) + Self::from_fd_inner(fd) + } + + fn from_fd_inner(fd: crate::MockableFd) -> Result { + let MapInfo(info) = MapInfo::new_from_fd(fd.as_fd())?; + Ok(Self { + obj: parse_map_info(info, PinningType::None), + fd: MapFd::from_fd(fd), + }) } /// Loads a map from a file descriptor. @@ -672,11 +679,8 @@ impl MapData { /// This API is intended for cases where you have received a valid BPF FD from some other means. /// For example, you received an FD over Unix Domain Socket. pub fn from_fd(fd: OwnedFd) -> Result { - let MapInfo(info) = MapInfo::new_from_fd(fd.as_fd())?; - Ok(Self { - obj: parse_map_info(info, PinningType::None), - fd: MapFd::from_fd(fd), - }) + let fd = crate::MockableFd::from_fd(fd); + Self::from_fd_inner(fd) } /// Allows the map to be pinned to the provided path. diff --git a/aya/src/maps/perf/perf_buffer.rs b/aya/src/maps/perf/perf_buffer.rs index 89f2cf733..564246378 100644 --- a/aya/src/maps/perf/perf_buffer.rs +++ b/aya/src/maps/perf/perf_buffer.rs @@ -120,7 +120,6 @@ impl PerfBuffer { }); } - let fd = crate::MockableFd::from_fd(fd); let perf_buf = Self { buf: AtomicPtr::new(buf as *mut perf_event_mmap_page), size, diff --git a/aya/src/programs/cgroup_device.rs b/aya/src/programs/cgroup_device.rs index 152637911..6484e87c6 100644 --- a/aya/src/programs/cgroup_device.rs +++ b/aya/src/programs/cgroup_device.rs @@ -122,6 +122,7 @@ impl CgroupDevice { .map(|prog_id| { let prog_fd = bpf_prog_get_fd_by_id(prog_id)?; let target_fd = target_fd.try_clone_to_owned()?; + let target_fd = crate::MockableFd::from_fd(target_fd); let prog_fd = ProgramFd(prog_fd); Ok(CgroupDeviceLink::new(CgroupDeviceLinkInner::ProgAttach( ProgAttachLink::new(prog_fd, target_fd, BPF_CGROUP_DEVICE), diff --git a/aya/src/programs/extension.rs b/aya/src/programs/extension.rs index 648bbfc4a..558bef054 100644 --- a/aya/src/programs/extension.rs +++ b/aya/src/programs/extension.rs @@ -1,6 +1,6 @@ //! Extension programs. -use std::os::fd::{AsFd as _, BorrowedFd, OwnedFd}; +use std::os::fd::{AsFd as _, BorrowedFd}; use object::Endianness; use thiserror::Error; @@ -169,7 +169,10 @@ impl Extension { /// Retrieves the FD of the BTF object for the provided `prog_fd` and the BTF ID of the function /// with the name `func_name` within that BTF object. -fn get_btf_info(prog_fd: BorrowedFd<'_>, func_name: &str) -> Result<(OwnedFd, u32), ProgramError> { +fn get_btf_info( + prog_fd: BorrowedFd<'_>, + func_name: &str, +) -> Result<(crate::MockableFd, u32), ProgramError> { // retrieve program information let info = sys::bpf_prog_get_info_by_fd(prog_fd, &mut [])?; diff --git a/aya/src/programs/links.rs b/aya/src/programs/links.rs index b809d1dce..740b4f787 100644 --- a/aya/src/programs/links.rs +++ b/aya/src/programs/links.rs @@ -3,7 +3,7 @@ use std::{ collections::{hash_map::Entry, HashMap}, ffi::CString, io, - os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, OwnedFd, RawFd}, + os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, RawFd}, path::{Path, PathBuf}, }; @@ -107,11 +107,11 @@ pub struct FdLinkId(pub(crate) RawFd); /// ``` #[derive(Debug)] pub struct FdLink { - pub(crate) fd: OwnedFd, + pub(crate) fd: crate::MockableFd, } impl FdLink { - pub(crate) fn new(fd: OwnedFd) -> Self { + pub(crate) fn new(fd: crate::MockableFd) -> Self { Self { fd } } @@ -231,14 +231,14 @@ pub struct ProgAttachLinkId(RawFd, RawFd, bpf_attach_type); #[derive(Debug)] pub struct ProgAttachLink { prog_fd: ProgramFd, - target_fd: OwnedFd, + target_fd: crate::MockableFd, attach_type: bpf_attach_type, } impl ProgAttachLink { pub(crate) fn new( prog_fd: ProgramFd, - target_fd: OwnedFd, + target_fd: crate::MockableFd, attach_type: bpf_attach_type, ) -> Self { Self { @@ -258,7 +258,9 @@ impl ProgAttachLink { // duplicate it prior to attaching it so the new file // descriptor is closed at drop in case it fails to attach. let prog_fd = prog_fd.try_clone_to_owned()?; + let prog_fd = crate::MockableFd::from_fd(prog_fd); let target_fd = target_fd.try_clone_to_owned()?; + let target_fd = crate::MockableFd::from_fd(target_fd); bpf_prog_attach(prog_fd.as_fd(), target_fd.as_fd(), attach_type)?; let prog_fd = ProgramFd(prog_fd); diff --git a/aya/src/programs/lirc_mode2.rs b/aya/src/programs/lirc_mode2.rs index cbd47d950..aa0a45ec2 100644 --- a/aya/src/programs/lirc_mode2.rs +++ b/aya/src/programs/lirc_mode2.rs @@ -1,5 +1,5 @@ //! Lirc programs. -use std::os::fd::{AsFd, AsRawFd as _, OwnedFd, RawFd}; +use std::os::fd::{AsFd, AsRawFd as _, RawFd}; use crate::{ generated::{bpf_attach_type::BPF_LIRC_MODE2, bpf_prog_type::BPF_PROG_TYPE_LIRC_MODE2}, @@ -67,6 +67,7 @@ impl LircMode2 { // descriptor is closed at drop in case it fails to attach. let prog_fd = prog_fd.try_clone()?; let lircdev_fd = lircdev.as_fd().try_clone_to_owned()?; + let lircdev_fd = crate::MockableFd::from_fd(lircdev_fd); bpf_prog_attach(prog_fd.as_fd(), lircdev_fd.as_fd(), BPF_LIRC_MODE2)?; @@ -98,6 +99,7 @@ impl LircMode2 { .map(|prog_id| { let prog_fd = bpf_prog_get_fd_by_id(prog_id)?; let target_fd = target_fd.try_clone_to_owned()?; + let target_fd = crate::MockableFd::from_fd(target_fd); let prog_fd = ProgramFd(prog_fd); Ok(LircLink::new(prog_fd, target_fd)) }) @@ -113,11 +115,11 @@ pub struct LircLinkId(RawFd, RawFd); /// An LircMode2 Link pub struct LircLink { prog_fd: ProgramFd, - target_fd: OwnedFd, + target_fd: crate::MockableFd, } impl LircLink { - pub(crate) fn new(prog_fd: ProgramFd, target_fd: OwnedFd) -> Self { + pub(crate) fn new(prog_fd: ProgramFd, target_fd: crate::MockableFd) -> Self { Self { prog_fd, target_fd } } diff --git a/aya/src/programs/mod.rs b/aya/src/programs/mod.rs index 675e36f0a..4b12c6bbe 100644 --- a/aya/src/programs/mod.rs +++ b/aya/src/programs/mod.rs @@ -72,7 +72,7 @@ use std::{ ffi::CString, io, num::NonZeroU32, - os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd}, + os::fd::{AsFd, AsRawFd, BorrowedFd}, path::{Path, PathBuf}, sync::Arc, time::{Duration, SystemTime}, @@ -224,7 +224,7 @@ pub enum ProgramError { /// A [`Program`] file descriptor. #[derive(Debug)] -pub struct ProgramFd(OwnedFd); +pub struct ProgramFd(crate::MockableFd); impl ProgramFd { /// Creates a new instance that shares the same underlying file description as [`self`]. @@ -460,10 +460,10 @@ pub(crate) struct ProgramData { pub(crate) fd: Option, pub(crate) links: LinkMap, pub(crate) expected_attach_type: Option, - pub(crate) attach_btf_obj_fd: Option, + pub(crate) attach_btf_obj_fd: Option, pub(crate) attach_btf_id: Option, pub(crate) attach_prog_fd: Option, - pub(crate) btf_fd: Option>, + pub(crate) btf_fd: Option>, pub(crate) verifier_log_level: VerifierLogLevel, pub(crate) path: Option, pub(crate) flags: u32, @@ -473,7 +473,7 @@ impl ProgramData { pub(crate) fn new( name: Option, obj: (obj::Program, obj::Function), - btf_fd: Option>, + btf_fd: Option>, verifier_log_level: VerifierLogLevel, ) -> Self { Self { @@ -494,7 +494,7 @@ impl ProgramData { pub(crate) fn from_bpf_prog_info( name: Option, - fd: OwnedFd, + fd: crate::MockableFd, path: &Path, info: bpf_prog_info, verifier_log_level: VerifierLogLevel, diff --git a/aya/src/programs/perf_attach.rs b/aya/src/programs/perf_attach.rs index 8cf04871f..f58189448 100644 --- a/aya/src/programs/perf_attach.rs +++ b/aya/src/programs/perf_attach.rs @@ -1,5 +1,5 @@ //! Perf attach links. -use std::os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, OwnedFd, RawFd}; +use std::os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, RawFd}; use crate::{ generated::bpf_attach_type::BPF_PERF_EVENT, @@ -48,7 +48,7 @@ pub struct PerfLinkId(RawFd); /// The attachment type of PerfEvent programs. #[derive(Debug)] pub struct PerfLink { - perf_fd: OwnedFd, + perf_fd: crate::MockableFd, event: Option, } @@ -72,7 +72,7 @@ impl Link for PerfLink { pub(crate) fn perf_attach( prog_fd: BorrowedFd<'_>, - fd: OwnedFd, + fd: crate::MockableFd, ) -> Result { if FEATURES.bpf_perf_link() { let link_fd = bpf_link_create(prog_fd, LinkTarget::Fd(fd.as_fd()), BPF_PERF_EVENT, None, 0) @@ -88,7 +88,7 @@ pub(crate) fn perf_attach( pub(crate) fn perf_attach_debugfs( prog_fd: BorrowedFd<'_>, - fd: OwnedFd, + fd: crate::MockableFd, event: ProbeEvent, ) -> Result { perf_attach_either(prog_fd, fd, Some(event)) @@ -96,7 +96,7 @@ pub(crate) fn perf_attach_debugfs( fn perf_attach_either( prog_fd: BorrowedFd<'_>, - fd: OwnedFd, + fd: crate::MockableFd, event: Option, ) -> Result { perf_event_ioctl(fd.as_fd(), PERF_EVENT_IOC_SET_BPF, prog_fd.as_raw_fd()).map_err( diff --git a/aya/src/programs/probe.rs b/aya/src/programs/probe.rs index 85be3b3c5..4d737e346 100644 --- a/aya/src/programs/probe.rs +++ b/aya/src/programs/probe.rs @@ -3,7 +3,7 @@ use std::{ fmt::Write as _, fs::{self, OpenOptions}, io::{self, Write}, - os::fd::{AsFd as _, OwnedFd}, + os::fd::AsFd as _, path::{Path, PathBuf}, process, sync::atomic::{AtomicUsize, Ordering}, @@ -150,7 +150,7 @@ fn create_as_probe( fn_name: &OsStr, offset: u64, pid: Option, -) -> Result { +) -> Result { use ProbeKind::*; let perf_ty = match kind { @@ -186,7 +186,7 @@ fn create_as_trace_point( name: &OsStr, offset: u64, pid: Option, -) -> Result<(OwnedFd, OsString), ProgramError> { +) -> Result<(crate::MockableFd, OsString), ProgramError> { use ProbeKind::*; let tracefs = find_tracefs_path()?; diff --git a/aya/src/sys/bpf.rs b/aya/src/sys/bpf.rs index 1ae1dc950..f327d2bbe 100644 --- a/aya/src/sys/bpf.rs +++ b/aya/src/sys/bpf.rs @@ -3,7 +3,7 @@ use std::{ ffi::{c_char, CStr, CString}, io, iter, mem::{self, MaybeUninit}, - os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, FromRawFd as _, OwnedFd, RawFd}, + os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, FromRawFd as _, RawFd}, slice, }; @@ -39,7 +39,7 @@ pub(crate) fn bpf_create_map( def: &obj::Map, btf_fd: Option>, kernel_version: KernelVersion, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_1 }; @@ -104,7 +104,7 @@ pub(crate) fn bpf_pin_object(fd: BorrowedFd<'_>, path: &CStr) -> SysResult sys_bpf(bpf_cmd::BPF_OBJ_PIN, &mut attr) } -pub(crate) fn bpf_get_object(path: &CStr) -> SysResult { +pub(crate) fn bpf_get_object(path: &CStr) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_4 }; u.pathname = path.as_ptr() as u64; @@ -134,7 +134,7 @@ pub(crate) fn bpf_load_program( aya_attr: &EbpfLoadProgramAttrs<'_>, log_buf: &mut [u8], verifier_log_level: VerifierLogLevel, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_3 }; @@ -383,7 +383,7 @@ pub(crate) fn bpf_link_create( attach_type: bpf_attach_type, btf_id: Option, flags: u32, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; attr.link_create.__bindgen_anon_1.prog_fd = prog_fd.as_raw_fd() as u32; @@ -498,7 +498,7 @@ pub(crate) fn bpf_prog_query( ret } -pub(crate) fn bpf_prog_get_fd_by_id(prog_id: u32) -> Result { +pub(crate) fn bpf_prog_get_fd_by_id(prog_id: u32) -> Result { let mut attr = unsafe { mem::zeroed::() }; attr.__bindgen_anon_6.__bindgen_anon_1.prog_id = prog_id; @@ -550,7 +550,7 @@ pub(crate) fn bpf_prog_get_info_by_fd( }) } -pub(crate) fn bpf_map_get_fd_by_id(map_id: u32) -> Result { +pub(crate) fn bpf_map_get_fd_by_id(map_id: u32) -> Result { let mut attr = unsafe { mem::zeroed::() }; attr.__bindgen_anon_6.__bindgen_anon_1.map_id = map_id; @@ -569,7 +569,7 @@ pub(crate) fn bpf_map_get_info_by_fd(fd: BorrowedFd<'_>) -> Result Result { +pub(crate) fn bpf_link_get_fd_by_id(link_id: u32) -> Result { let mut attr = unsafe { mem::zeroed::() }; attr.__bindgen_anon_6.__bindgen_anon_1.link_id = link_id; @@ -600,7 +600,7 @@ pub(crate) fn btf_obj_get_info_by_fd( pub(crate) fn bpf_raw_tracepoint_open( name: Option<&CStr>, prog_fd: BorrowedFd<'_>, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; attr.raw_tracepoint.name = match name { @@ -617,7 +617,7 @@ pub(crate) fn bpf_load_btf( raw_btf: &[u8], log_buf: &mut [u8], verifier_log_level: VerifierLogLevel, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_7 }; u.btf = raw_btf.as_ptr() as *const _ as u64; @@ -632,7 +632,7 @@ pub(crate) fn bpf_load_btf( } // SAFETY: only use for bpf_cmd that return a new file descriptor on success. -unsafe fn fd_sys_bpf(cmd: bpf_cmd, attr: &mut bpf_attr) -> SysResult { +unsafe fn fd_sys_bpf(cmd: bpf_cmd, attr: &mut bpf_attr) -> SysResult { let fd = sys_bpf(cmd, attr)?; let fd = fd.try_into().map_err(|_| { ( @@ -643,10 +643,10 @@ unsafe fn fd_sys_bpf(cmd: bpf_cmd, attr: &mut bpf_attr) -> SysResult { ), ) })?; - Ok(OwnedFd::from_raw_fd(fd)) + Ok(crate::MockableFd::from_raw_fd(fd)) } -pub(crate) fn bpf_btf_get_fd_by_id(id: u32) -> Result { +pub(crate) fn bpf_btf_get_fd_by_id(id: u32) -> Result { let mut attr = unsafe { mem::zeroed::() }; attr.__bindgen_anon_6.__bindgen_anon_1.btf_id = id; @@ -730,7 +730,6 @@ pub(crate) fn is_perf_link_supported() -> bool { u.prog_type = bpf_prog_type::BPF_PROG_TYPE_TRACEPOINT as u32; if let Ok(fd) = bpf_prog_load(&mut attr) { - let fd = crate::MockableFd::from_fd(fd); let fd = fd.as_fd(); matches!( // Uses an invalid target FD so we get EBADF if supported. @@ -830,7 +829,6 @@ pub(crate) fn is_prog_id_supported(map_type: bpf_map_type) -> bool { // SAFETY: BPF_MAP_CREATE returns a new file descriptor. let fd = unsafe { fd_sys_bpf(bpf_cmd::BPF_MAP_CREATE, &mut attr) }; - let fd = fd.map(crate::MockableFd::from_fd); fd.is_ok() } @@ -990,7 +988,7 @@ pub(crate) fn is_btf_type_tag_supported() -> bool { bpf_load_btf(btf_bytes.as_slice(), &mut [], Default::default()).is_ok() } -fn bpf_prog_load(attr: &mut bpf_attr) -> SysResult { +fn bpf_prog_load(attr: &mut bpf_attr) -> SysResult { // SAFETY: BPF_PROG_LOAD returns a new file descriptor. unsafe { fd_sys_bpf(bpf_cmd::BPF_PROG_LOAD, attr) } } diff --git a/aya/src/sys/netlink.rs b/aya/src/sys/netlink.rs index f10c2839b..522155552 100644 --- a/aya/src/sys/netlink.rs +++ b/aya/src/sys/netlink.rs @@ -2,7 +2,7 @@ use std::{ collections::HashMap, ffi::CStr, io, mem, - os::fd::{AsRawFd as _, BorrowedFd, FromRawFd as _, OwnedFd}, + os::fd::{AsRawFd as _, BorrowedFd, FromRawFd as _}, ptr, slice, }; @@ -307,7 +307,7 @@ struct TcRequest { } struct NetlinkSocket { - sock: OwnedFd, + sock: crate::MockableFd, _nl_pid: u32, } @@ -319,7 +319,7 @@ impl NetlinkSocket { return Err(io::Error::last_os_error()); } // SAFETY: `socket` returns a file descriptor. - let sock = unsafe { OwnedFd::from_raw_fd(sock) }; + let sock = unsafe { crate::MockableFd::from_raw_fd(sock) }; let enable = 1i32; // Safety: libc wrapper diff --git a/aya/src/sys/perf_event.rs b/aya/src/sys/perf_event.rs index f58152260..ec8b14207 100644 --- a/aya/src/sys/perf_event.rs +++ b/aya/src/sys/perf_event.rs @@ -1,7 +1,7 @@ use std::{ ffi::{c_int, CString, OsStr}, io, mem, - os::fd::{BorrowedFd, FromRawFd as _, OwnedFd}, + os::fd::{BorrowedFd, FromRawFd as _}, }; use libc::pid_t; @@ -26,7 +26,7 @@ pub(crate) fn perf_event_open( wakeup: bool, inherit: bool, flags: u32, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; attr.config = config; @@ -46,7 +46,7 @@ pub(crate) fn perf_event_open( perf_event_sys(attr, pid, cpu, flags) } -pub(crate) fn perf_event_open_bpf(cpu: c_int) -> SysResult { +pub(crate) fn perf_event_open_bpf(cpu: c_int) -> SysResult { perf_event_open( PERF_TYPE_SOFTWARE as u32, PERF_COUNT_SW_BPF_OUTPUT as u64, @@ -66,7 +66,7 @@ pub(crate) fn perf_event_open_probe( name: &OsStr, offset: u64, pid: Option, -) -> SysResult { +) -> SysResult { use std::os::unix::ffi::OsStrExt as _; let mut attr = unsafe { mem::zeroed::() }; @@ -88,7 +88,10 @@ pub(crate) fn perf_event_open_probe( perf_event_sys(attr, pid, cpu, PERF_FLAG_FD_CLOEXEC) } -pub(crate) fn perf_event_open_trace_point(id: u32, pid: Option) -> SysResult { +pub(crate) fn perf_event_open_trace_point( + id: u32, + pid: Option, +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; attr.size = mem::size_of::() as u32; @@ -110,7 +113,12 @@ pub(crate) fn perf_event_ioctl(fd: BorrowedFd<'_>, request: c_int, arg: c_int) - return crate::sys::TEST_SYSCALL.with(|test_impl| unsafe { test_impl.borrow()(call) }); } -fn perf_event_sys(attr: perf_event_attr, pid: pid_t, cpu: i32, flags: u32) -> SysResult { +fn perf_event_sys( + attr: perf_event_attr, + pid: pid_t, + cpu: i32, + flags: u32, +) -> SysResult { let fd = syscall(Syscall::PerfEventOpen { attr, pid, @@ -130,7 +138,7 @@ fn perf_event_sys(attr: perf_event_attr, pid: pid_t, cpu: i32, flags: u32) -> Sy })?; // SAFETY: perf_event_open returns a new file descriptor on success. - unsafe { Ok(OwnedFd::from_raw_fd(fd)) } + unsafe { Ok(crate::MockableFd::from_raw_fd(fd)) } } /*