From a11b61ebfde8713c35b6f2a760e470d3586803a7 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 2 May 2024 15:23:45 -0400 Subject: [PATCH] s/MiriSafeFd/MockableFd/ The need for this type isn't specific to Miri; it is necessary on toolchains containing https://github.com/rust-lang/rust/pull/124210 - it just so happens that today this is nightly only, and so is Miri. --- aya/src/lib.rs | 48 ++++++++++++---------- aya/src/maps/bloom_filter.rs | 4 +- aya/src/maps/hash_map/hash_map.rs | 8 ++-- aya/src/maps/lpm_trie.rs | 4 +- aya/src/maps/mod.rs | 66 +++++++++++++++++++------------ aya/src/maps/perf/perf_buffer.rs | 6 +-- aya/src/sys/bpf.rs | 44 ++++++++++----------- aya/src/sys/fake.rs | 12 ++---- aya/src/sys/mod.rs | 52 +++++++++++++----------- aya/src/sys/perf_event.rs | 8 +--- xtask/public-api/aya.txt | 2 +- 11 files changed, 137 insertions(+), 117 deletions(-) diff --git a/aya/src/lib.rs b/aya/src/lib.rs index 75451e25..eca3d072 100644 --- a/aya/src/lib.rs +++ b/aya/src/lib.rs @@ -97,37 +97,44 @@ pub use object::Endianness; pub use sys::netlink_set_link_up; // See https://github.com/rust-lang/rust/pull/124210; this structure exists to avoid crashing the -// process when we try to close a fake file descriptor in Miri. +// process when we try to close a fake file descriptor. #[derive(Debug)] -struct MiriSafeFd { - #[cfg(not(miri))] +struct MockableFd { + #[cfg(not(test))] fd: OwnedFd, - #[cfg(miri)] + #[cfg(test)] fd: Option, } -impl MiriSafeFd { - #[cfg(any(test, miri))] - const MOCK_FD: u16 = 1337; +impl MockableFd { + #[cfg(test)] + const fn mock_signed_fd() -> i32 { + 1337 + } + + #[cfg(test)] + const fn mock_unsigned_fd() -> u32 { + 1337 + } - #[cfg(not(miri))] + #[cfg(not(test))] fn from_fd(fd: OwnedFd) -> Self { Self { fd } } - #[cfg(miri)] + #[cfg(test)] fn from_fd(fd: OwnedFd) -> Self { Self { fd: Some(fd) } } - #[cfg(not(miri))] + #[cfg(not(test))] fn try_clone(&self) -> std::io::Result { let Self { fd } = self; let fd = fd.try_clone()?; Ok(Self { fd }) } - #[cfg(miri)] + #[cfg(test)] fn try_clone(&self) -> std::io::Result { let Self { fd } = self; let fd = fd.as_ref().map(OwnedFd::try_clone).transpose()?; @@ -135,33 +142,34 @@ impl MiriSafeFd { } } -impl AsFd for MiriSafeFd { - #[cfg(not(miri))] +impl AsFd for MockableFd { + #[cfg(not(test))] fn as_fd(&self) -> BorrowedFd<'_> { let Self { fd } = self; fd.as_fd() } - #[cfg(miri)] + #[cfg(test)] fn as_fd(&self) -> BorrowedFd<'_> { let Self { fd } = self; fd.as_ref().unwrap().as_fd() } } -impl Drop for MiriSafeFd { - #[cfg(not(miri))] +impl Drop for MockableFd { + #[cfg(not(test))] fn drop(&mut self) { // Intentional no-op. } - #[cfg(miri)] + #[cfg(test)] fn drop(&mut self) { use std::os::fd::AsRawFd as _; let Self { fd } = self; - let fd = fd.take().unwrap(); - assert_eq!(fd.as_raw_fd(), Self::MOCK_FD.into()); - std::mem::forget(fd) + if fd.as_ref().unwrap().as_raw_fd() >= Self::mock_signed_fd() { + let fd: OwnedFd = fd.take().unwrap(); + std::mem::forget(fd) + } } } diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index 14aef4d3..944f3f76 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -79,7 +79,7 @@ impl, V: Pod> BloomFilter { #[cfg(test)] mod tests { - use std::{ffi::c_long, io}; + use std::io; use assert_matches::assert_matches; use libc::{EFAULT, ENOENT}; @@ -102,7 +102,7 @@ mod tests { test_utils::new_obj_map::(BPF_MAP_TYPE_BLOOM_FILTER) } - fn sys_error(value: i32) -> SysResult { + fn sys_error(value: i32) -> SysResult { Err((-1, io::Error::from_raw_os_error(value))) } diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index 307d1a51..becac6c4 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -103,7 +103,7 @@ impl, K: Pod, V: Pod> IterableMap for HashMap #[cfg(test)] mod tests { - use std::{ffi::c_long, io}; + use std::io; use assert_matches::assert_matches; use libc::{EFAULT, ENOENT}; @@ -126,7 +126,7 @@ mod tests { test_utils::new_obj_map::(BPF_MAP_TYPE_HASH) } - fn sys_error(value: i32) -> SysResult { + fn sys_error(value: i32) -> SysResult { Err((-1, io::Error::from_raw_os_error(value))) } @@ -332,7 +332,7 @@ mod tests { assert_matches!(keys, Ok(ks) if ks.is_empty()) } - fn get_next_key(attr: &bpf_attr) -> SysResult { + fn get_next_key(attr: &bpf_attr) -> SysResult { match bpf_key(attr) { None => set_next_key(attr, 10), Some(10) => set_next_key(attr, 20), @@ -344,7 +344,7 @@ mod tests { Ok(1) } - fn lookup_elem(attr: &bpf_attr) -> SysResult { + fn lookup_elem(attr: &bpf_attr) -> SysResult { match bpf_key(attr) { Some(10) => set_ret(attr, 100), Some(20) => set_ret(attr, 200), diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index 8e25ddd5..e2271908 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -196,7 +196,7 @@ impl, K: Pod, V: Pod> IterableMap, V> for LpmTrie>(BPF_MAP_TYPE_LPM_TRIE) } - fn sys_error(value: i32) -> SysResult { + fn sys_error(value: i32) -> SysResult { Err((-1, io::Error::from_raw_os_error(value))) } diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 7abdabba..82467aee 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -49,7 +49,7 @@ //! implement the [Pod] trait. use std::{ borrow::Borrow, - ffi::{c_long, CString}, + ffi::CString, fmt, io, marker::PhantomData, mem, @@ -127,7 +127,7 @@ pub enum MapError { /// Map name name: String, /// Error code - code: c_long, + code: i64, #[source] /// Original io::Error io_error: io::Error, @@ -211,12 +211,12 @@ impl From for MapError { /// A map file descriptor. #[derive(Debug)] pub struct MapFd { - fd: crate::MiriSafeFd, + fd: crate::MockableFd, } impl MapFd { fn from_fd(fd: OwnedFd) -> Self { - let fd = crate::MiriSafeFd::from_fd(fd); + let fd = crate::MockableFd::from_fd(fd); Self { fd } } @@ -1052,7 +1052,7 @@ mod test_utils { Syscall::Ebpf { cmd: bpf_cmd::BPF_MAP_CREATE, .. - } => Ok(crate::MiriSafeFd::MOCK_FD.into()), + } => Ok(crate::MockableFd::mock_signed_fd().into()), call => panic!("unexpected syscall {:?}", call), }); MapData::create(obj, "foo", None).unwrap() @@ -1103,7 +1103,7 @@ mod tests { unsafe { attr.__bindgen_anon_6.__bindgen_anon_1.map_id }, 1234 ); - Ok(crate::MiriSafeFd::MOCK_FD.into()) + Ok(crate::MockableFd::mock_signed_fd().into()) } Syscall::Ebpf { cmd: bpf_cmd::BPF_OBJ_GET_INFO_BY_FD, @@ -1111,7 +1111,7 @@ mod tests { } => { assert_eq!( unsafe { attr.info.bpf_fd }, - crate::MiriSafeFd::MOCK_FD.into() + crate::MockableFd::mock_unsigned_fd(), ); Ok(0) } @@ -1123,7 +1123,7 @@ mod tests { Ok(MapData { obj: _, fd, - }) => assert_eq!(fd.as_fd().as_raw_fd(), crate::MiriSafeFd::MOCK_FD.into()) + }) => assert_eq!(fd.as_fd().as_raw_fd(), crate::MockableFd::mock_signed_fd()) ); } @@ -1133,7 +1133,7 @@ mod tests { Syscall::Ebpf { cmd: bpf_cmd::BPF_MAP_CREATE, .. - } => Ok(crate::MiriSafeFd::MOCK_FD.into()), + } => Ok(crate::MockableFd::mock_signed_fd().into()), _ => Err((-1, io::Error::from_raw_os_error(EFAULT))), }); @@ -1142,7 +1142,7 @@ mod tests { Ok(MapData { obj: _, fd, - }) => assert_eq!(fd.as_fd().as_raw_fd(), crate::MiriSafeFd::MOCK_FD.into()) + }) => assert_eq!(fd.as_fd().as_raw_fd(), crate::MockableFd::mock_signed_fd()) ); } @@ -1160,7 +1160,7 @@ mod tests { Syscall::Ebpf { cmd: bpf_cmd::BPF_MAP_CREATE, .. - } => Ok(42), + } => Ok(crate::MockableFd::mock_signed_fd().into()), Syscall::Ebpf { cmd: bpf_cmd::BPF_OBJ_GET_INFO_BY_FD, attr, @@ -1206,13 +1206,15 @@ mod tests { Syscall::Ebpf { cmd: bpf_cmd::BPF_MAP_GET_FD_BY_ID, attr, - } => Ok((1000 + unsafe { attr.__bindgen_anon_6.__bindgen_anon_1.map_id }) as c_long), + } => Ok((unsafe { attr.__bindgen_anon_6.__bindgen_anon_1.map_id } + + crate::MockableFd::mock_unsigned_fd()) + .into()), Syscall::Ebpf { cmd: bpf_cmd::BPF_OBJ_GET_INFO_BY_FD, attr, } => { let map_info = unsafe { &mut *(attr.info.info as *mut bpf_map_info) }; - map_info.id = unsafe { attr.info.bpf_fd } - 1000; + map_info.id = unsafe { attr.info.bpf_fd } - crate::MockableFd::mock_unsigned_fd(); map_info.key_size = 32; map_info.value_size = 64; map_info.map_flags = 1234; @@ -1222,19 +1224,31 @@ mod tests { _ => Err((-1, io::Error::from_raw_os_error(EFAULT))), }); - let loaded_maps: Vec<_> = loaded_maps().collect(); - assert_eq!(loaded_maps.len(), 5); - - for (i, map_info) in loaded_maps.into_iter().enumerate() { - let i = i + 1; - let map_info = map_info.unwrap(); - assert_eq!(map_info.id(), i as u32); - assert_eq!(map_info.key_size(), 32); - assert_eq!(map_info.value_size(), 64); - assert_eq!(map_info.map_flags(), 1234); - assert_eq!(map_info.max_entries(), 99); - assert_eq!(map_info.fd().unwrap().as_fd().as_raw_fd(), 1000 + i as i32); - } + assert_eq!( + loaded_maps() + .map(|map_info| { + let map_info = map_info.unwrap(); + ( + map_info.id(), + map_info.key_size(), + map_info.value_size(), + map_info.map_flags(), + map_info.max_entries(), + map_info.fd().unwrap().as_fd().as_raw_fd(), + ) + }) + .collect::>(), + (1..6) + .map(|i: u8| ( + i.into(), + 32, + 64, + 1234, + 99, + crate::MockableFd::mock_signed_fd() + i32::from(i) + )) + .collect::>(), + ); } #[test] diff --git a/aya/src/maps/perf/perf_buffer.rs b/aya/src/maps/perf/perf_buffer.rs index 26f09b20..89f2cf73 100644 --- a/aya/src/maps/perf/perf_buffer.rs +++ b/aya/src/maps/perf/perf_buffer.rs @@ -88,7 +88,7 @@ pub(crate) struct PerfBuffer { buf: AtomicPtr, size: usize, page_size: usize, - fd: crate::MiriSafeFd, + fd: crate::MockableFd, } impl PerfBuffer { @@ -120,7 +120,7 @@ impl PerfBuffer { }); } - let fd = crate::MiriSafeFd::from_fd(fd); + let fd = crate::MockableFd::from_fd(fd); let perf_buf = Self { buf: AtomicPtr::new(buf as *mut perf_event_mmap_page), size, @@ -303,7 +303,7 @@ mod tests { fn fake_mmap(buf: &MMappedBuf) { override_syscall(|call| match call { Syscall::PerfEventOpen { .. } | Syscall::PerfEventIoctl { .. } => { - Ok(crate::MiriSafeFd::MOCK_FD.into()) + Ok(crate::MockableFd::mock_signed_fd().into()) } call => panic!("unexpected syscall: {:?}", call), }); diff --git a/aya/src/sys/bpf.rs b/aya/src/sys/bpf.rs index add46b67..1ae1dc95 100644 --- a/aya/src/sys/bpf.rs +++ b/aya/src/sys/bpf.rs @@ -1,6 +1,6 @@ use std::{ cmp, - ffi::{c_char, c_long, CStr, CString}, + ffi::{c_char, CStr, CString}, io, iter, mem::{self, MaybeUninit}, os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, FromRawFd as _, OwnedFd, RawFd}, @@ -96,7 +96,7 @@ pub(crate) fn bpf_create_map( unsafe { fd_sys_bpf(bpf_cmd::BPF_MAP_CREATE, &mut attr) } } -pub(crate) fn bpf_pin_object(fd: BorrowedFd<'_>, path: &CStr) -> SysResult { +pub(crate) fn bpf_pin_object(fd: BorrowedFd<'_>, path: &CStr) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_4 }; u.bpf_fd = fd.as_raw_fd() as u32; @@ -200,7 +200,7 @@ fn lookup( key: Option<&K>, flags: u64, cmd: bpf_cmd, -) -> Result, (c_long, io::Error)> { +) -> SysResult> { let mut attr = unsafe { mem::zeroed::() }; let mut value = MaybeUninit::zeroed(); @@ -223,7 +223,7 @@ pub(crate) fn bpf_map_lookup_elem( fd: BorrowedFd<'_>, key: &K, flags: u64, -) -> Result, (c_long, io::Error)> { +) -> SysResult> { lookup(fd, Some(key), flags, bpf_cmd::BPF_MAP_LOOKUP_ELEM) } @@ -231,7 +231,7 @@ pub(crate) fn bpf_map_lookup_and_delete_elem( fd: BorrowedFd<'_>, key: Option<&K>, flags: u64, -) -> Result, (c_long, io::Error)> { +) -> SysResult> { lookup(fd, key, flags, bpf_cmd::BPF_MAP_LOOKUP_AND_DELETE_ELEM) } @@ -239,7 +239,7 @@ pub(crate) fn bpf_map_lookup_elem_per_cpu( fd: BorrowedFd<'_>, key: &K, flags: u64, -) -> Result>, (c_long, io::Error)> { +) -> SysResult>> { let mut mem = PerCpuValues::::alloc_kernel_mem().map_err(|io_error| (-1, io_error))?; match bpf_map_lookup_elem_ptr(fd, Some(key), mem.as_mut_ptr(), flags) { Ok(_) => Ok(Some(unsafe { PerCpuValues::from_kernel_mem(mem) })), @@ -253,7 +253,7 @@ pub(crate) fn bpf_map_lookup_elem_ptr( key: Option<&K>, value: *mut V, flags: u64, -) -> Result, (c_long, io::Error)> { +) -> SysResult> { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; @@ -276,7 +276,7 @@ pub(crate) fn bpf_map_update_elem( key: Option<&K>, value: &V, flags: u64, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; @@ -294,7 +294,7 @@ pub(crate) fn bpf_map_push_elem( fd: BorrowedFd<'_>, value: &V, flags: u64, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; @@ -310,7 +310,7 @@ pub(crate) fn bpf_map_update_elem_ptr( key: *const K, value: *mut V, flags: u64, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; @@ -327,12 +327,12 @@ pub(crate) fn bpf_map_update_elem_per_cpu( key: &K, values: &PerCpuValues, flags: u64, -) -> SysResult { +) -> SysResult { let mut mem = values.build_kernel_mem().map_err(|e| (-1, e))?; bpf_map_update_elem_ptr(fd, key, mem.as_mut_ptr(), flags) } -pub(crate) fn bpf_map_delete_elem(fd: BorrowedFd<'_>, key: &K) -> SysResult { +pub(crate) fn bpf_map_delete_elem(fd: BorrowedFd<'_>, key: &K) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; @@ -345,7 +345,7 @@ pub(crate) fn bpf_map_delete_elem(fd: BorrowedFd<'_>, key: &K) -> SysRes pub(crate) fn bpf_map_get_next_key( fd: BorrowedFd<'_>, key: Option<&K>, -) -> Result, (c_long, io::Error)> { +) -> SysResult> { let mut attr = unsafe { mem::zeroed::() }; let mut next_key = MaybeUninit::uninit(); @@ -364,7 +364,7 @@ pub(crate) fn bpf_map_get_next_key( } // since kernel 5.2 -pub(crate) fn bpf_map_freeze(fd: BorrowedFd<'_>) -> SysResult { +pub(crate) fn bpf_map_freeze(fd: BorrowedFd<'_>) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; u.map_fd = fd.as_raw_fd() as u32; @@ -412,7 +412,7 @@ pub(crate) fn bpf_link_update( new_prog_fd: BorrowedFd<'_>, old_prog_fd: Option, flags: u32, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; attr.link_update.link_fd = link_fd.as_raw_fd() as u32; @@ -478,7 +478,7 @@ pub(crate) fn bpf_prog_query( attach_flags: Option<&mut u32>, prog_ids: &mut [u32], prog_cnt: &mut u32, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; attr.query.__bindgen_anon_1.target_fd = target_fd as u32; @@ -730,7 +730,7 @@ 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::MiriSafeFd::from_fd(fd); + 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 +830,7 @@ 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::MiriSafeFd::from_fd); + let fd = fd.map(crate::MockableFd::from_fd); fd.is_ok() } @@ -995,7 +995,7 @@ fn bpf_prog_load(attr: &mut bpf_attr) -> SysResult { unsafe { fd_sys_bpf(bpf_cmd::BPF_PROG_LOAD, attr) } } -fn sys_bpf(cmd: bpf_cmd, attr: &mut bpf_attr) -> SysResult { +fn sys_bpf(cmd: bpf_cmd, attr: &mut bpf_attr) -> SysResult { syscall(Syscall::Ebpf { cmd, attr }) } @@ -1103,7 +1103,7 @@ mod tests { cmd: bpf_cmd::BPF_LINK_CREATE, .. } => Err((-1, io::Error::from_raw_os_error(EBADF))), - _ => Ok(crate::MiriSafeFd::MOCK_FD.into()), + _ => Ok(crate::MockableFd::mock_signed_fd().into()), }); let supported = is_perf_link_supported(); assert!(supported); @@ -1113,7 +1113,7 @@ mod tests { cmd: bpf_cmd::BPF_LINK_CREATE, .. } => Err((-1, io::Error::from_raw_os_error(EINVAL))), - _ => Ok(crate::MiriSafeFd::MOCK_FD.into()), + _ => Ok(crate::MockableFd::mock_signed_fd().into()), }); let supported = is_perf_link_supported(); assert!(!supported); @@ -1121,7 +1121,7 @@ mod tests { #[test] fn test_prog_id_supported() { - override_syscall(|_call| Ok(crate::MiriSafeFd::MOCK_FD.into())); + override_syscall(|_call| Ok(crate::MockableFd::mock_signed_fd().into())); // Ensure that the three map types we can check are accepted let supported = is_prog_id_supported(bpf_map_type::BPF_MAP_TYPE_CPUMAP); diff --git a/aya/src/sys/fake.rs b/aya/src/sys/fake.rs index 47019926..0e0cd3fe 100644 --- a/aya/src/sys/fake.rs +++ b/aya/src/sys/fake.rs @@ -1,12 +1,8 @@ -use std::{ - cell::RefCell, - ffi::{c_long, c_void}, - io, ptr, -}; +use std::{cell::RefCell, ffi::c_void, io, ptr}; use super::{SysResult, Syscall}; -type SyscallFn = unsafe fn(Syscall<'_>) -> SysResult; +type SyscallFn = unsafe fn(Syscall<'_>) -> SysResult; #[cfg(test)] thread_local! { @@ -15,11 +11,11 @@ thread_local! { } #[cfg(test)] -unsafe fn test_syscall(_call: Syscall<'_>) -> SysResult { +unsafe fn test_syscall(_call: Syscall<'_>) -> SysResult { Err((-1, io::Error::from_raw_os_error(libc::EINVAL))) } #[cfg(test)] -pub(crate) fn override_syscall(call: unsafe fn(Syscall<'_>) -> SysResult) { +pub(crate) fn override_syscall(call: unsafe fn(Syscall<'_>) -> SysResult) { TEST_SYSCALL.with(|test_impl| *test_impl.borrow_mut() = call); } diff --git a/aya/src/sys/mod.rs b/aya/src/sys/mod.rs index fddab712..1f13df5a 100644 --- a/aya/src/sys/mod.rs +++ b/aya/src/sys/mod.rs @@ -6,7 +6,7 @@ mod perf_event; mod fake; use std::{ - ffi::{c_int, c_long, c_void}, + ffi::{c_int, c_void}, io, mem, os::fd::{AsRawFd as _, BorrowedFd}, }; @@ -23,7 +23,7 @@ use thiserror::Error; use crate::generated::{bpf_attr, bpf_cmd, perf_event_attr}; -pub(crate) type SysResult = Result; +pub(crate) type SysResult = Result; pub(crate) enum Syscall<'a> { Ebpf { @@ -86,33 +86,39 @@ impl std::fmt::Debug for Syscall<'_> { } } -fn syscall(call: Syscall<'_>) -> SysResult { +fn syscall(call: Syscall<'_>) -> SysResult { #[cfg(test)] return TEST_SYSCALL.with(|test_impl| unsafe { test_impl.borrow()(call) }); #[cfg_attr(test, allow(unreachable_code))] - match unsafe { - match call { - Syscall::Ebpf { cmd, attr } => { - libc::syscall(SYS_bpf, cmd, attr, mem::size_of::()) - } - Syscall::PerfEventOpen { - attr, - pid, - cpu, - group, - flags, - } => libc::syscall(SYS_perf_event_open, &attr, pid, cpu, group, flags), - Syscall::PerfEventIoctl { fd, request, arg } => { - let int = libc::ioctl(fd.as_raw_fd(), request.try_into().unwrap(), arg); - #[allow(trivial_numeric_casts)] - let int = int as c_long; - int + { + let ret = unsafe { + match call { + Syscall::Ebpf { cmd, attr } => { + libc::syscall(SYS_bpf, cmd, attr, mem::size_of::()) + } + Syscall::PerfEventOpen { + attr, + pid, + cpu, + group, + flags, + } => libc::syscall(SYS_perf_event_open, &attr, pid, cpu, group, flags), + Syscall::PerfEventIoctl { fd, request, arg } => { + let ret = libc::ioctl(fd.as_raw_fd(), request.try_into().unwrap(), arg); + // `libc::ioctl` returns i32 on x86_64 while `libc::syscall` returns i64. + #[allow(clippy::useless_conversion)] + ret.into() + } } + }; + + // `libc::syscall` returns i32 on armv7. + #[allow(clippy::useless_conversion)] + match ret.into() { + ret @ 0.. => Ok(ret), + ret => Err((ret, io::Error::last_os_error())), } - } { - ret @ 0.. => Ok(ret), - ret => Err((ret, io::Error::last_os_error())), } } diff --git a/aya/src/sys/perf_event.rs b/aya/src/sys/perf_event.rs index 1d7af608..f5815226 100644 --- a/aya/src/sys/perf_event.rs +++ b/aya/src/sys/perf_event.rs @@ -1,5 +1,5 @@ use std::{ - ffi::{c_int, c_long, CString, OsStr}, + ffi::{c_int, CString, OsStr}, io, mem, os::fd::{BorrowedFd, FromRawFd as _, OwnedFd}, }; @@ -101,11 +101,7 @@ pub(crate) fn perf_event_open_trace_point(id: u32, pid: Option) -> SysRes perf_event_sys(attr, pid, cpu, PERF_FLAG_FD_CLOEXEC) } -pub(crate) fn perf_event_ioctl( - fd: BorrowedFd<'_>, - request: c_int, - arg: c_int, -) -> SysResult { +pub(crate) fn perf_event_ioctl(fd: BorrowedFd<'_>, request: c_int, arg: c_int) -> SysResult { let call = Syscall::PerfEventIoctl { fd, request, arg }; #[cfg(not(test))] return syscall(call); diff --git a/xtask/public-api/aya.txt b/xtask/public-api/aya.txt index 760c4e08..2496b411 100644 --- a/xtask/public-api/aya.txt +++ b/xtask/public-api/aya.txt @@ -1330,7 +1330,7 @@ impl core::convert::From for aya::maps::Map pub fn aya::maps::Map::from(t: T) -> T pub enum aya::maps::MapError pub aya::maps::MapError::CreateError -pub aya::maps::MapError::CreateError::code: core::ffi::c_long +pub aya::maps::MapError::CreateError::code: i64 pub aya::maps::MapError::CreateError::io_error: std::io::error::Error pub aya::maps::MapError::CreateError::name: alloc::string::String pub aya::maps::MapError::ElementNotFound