Avoid useless conversions

Use native C types to avoid platform-dependent conversions.
reviewable/pr1127/r1
Tamir Duberstein 3 weeks ago
parent 4257643354
commit 78ee9a4634

@ -78,7 +78,7 @@ impl<T: BorrowMut<MapData>, V: Pod> BloomFilter<T, V> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::io; use std::{ffi::c_long, io};
use assert_matches::assert_matches; use assert_matches::assert_matches;
use libc::{EFAULT, ENOENT}; use libc::{EFAULT, ENOENT};
@ -101,7 +101,7 @@ mod tests {
test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_BLOOM_FILTER) test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_BLOOM_FILTER)
} }
fn sys_error(value: i32) -> SysResult<i64> { fn sys_error(value: i32) -> SysResult<c_long> {
Err((-1, io::Error::from_raw_os_error(value))) Err((-1, io::Error::from_raw_os_error(value)))
} }

@ -103,7 +103,7 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<K, V> for HashMap<T, K, V>
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::io; use std::{ffi::c_long, io};
use assert_matches::assert_matches; use assert_matches::assert_matches;
use libc::{EFAULT, ENOENT}; use libc::{EFAULT, ENOENT};
@ -126,7 +126,7 @@ mod tests {
test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_HASH) test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_HASH)
} }
fn sys_error(value: i32) -> SysResult<i64> { fn sys_error(value: i32) -> SysResult<c_long> {
Err((-1, io::Error::from_raw_os_error(value))) Err((-1, io::Error::from_raw_os_error(value)))
} }
@ -332,7 +332,7 @@ mod tests {
assert_matches!(keys, Ok(ks) if ks.is_empty()) assert_matches!(keys, Ok(ks) if ks.is_empty())
} }
fn get_next_key(attr: &bpf_attr) -> SysResult<i64> { fn get_next_key(attr: &bpf_attr) -> SysResult<c_long> {
match bpf_key(attr) { match bpf_key(attr) {
None => set_next_key(attr, 10), None => set_next_key(attr, 10),
Some(10) => set_next_key(attr, 20), Some(10) => set_next_key(attr, 20),
@ -344,7 +344,7 @@ mod tests {
Ok(1) Ok(1)
} }
fn lookup_elem(attr: &bpf_attr) -> SysResult<i64> { fn lookup_elem(attr: &bpf_attr) -> SysResult<c_long> {
match bpf_key(attr) { match bpf_key(attr) {
Some(10) => set_ret(attr, 100), Some(10) => set_ret(attr, 100),
Some(20) => set_ret(attr, 200), Some(20) => set_ret(attr, 200),

@ -195,7 +195,7 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<Key<K>, V> for LpmTrie<T, K
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{io, net::Ipv4Addr}; use std::{ffi::c_long, io, net::Ipv4Addr};
use assert_matches::assert_matches; use assert_matches::assert_matches;
use libc::{EFAULT, ENOENT}; use libc::{EFAULT, ENOENT};
@ -218,7 +218,7 @@ mod tests {
test_utils::new_obj_map::<Key<u32>>(BPF_MAP_TYPE_LPM_TRIE) test_utils::new_obj_map::<Key<u32>>(BPF_MAP_TYPE_LPM_TRIE)
} }
fn sys_error(value: i32) -> SysResult<i64> { fn sys_error(value: i32) -> SysResult<c_long> {
Err((-1, io::Error::from_raw_os_error(value))) Err((-1, io::Error::from_raw_os_error(value)))
} }

@ -49,7 +49,7 @@
//! implement the [Pod] trait. //! implement the [Pod] trait.
use std::{ use std::{
borrow::Borrow, borrow::Borrow,
ffi::CString, ffi::{c_long, CString},
fmt, io, fmt, io,
marker::PhantomData, marker::PhantomData,
mem, mem,
@ -127,7 +127,7 @@ pub enum MapError {
/// Map name /// Map name
name: String, name: String,
/// Error code /// Error code
code: i64, code: c_long,
#[source] #[source]
/// Original io::Error /// Original io::Error
io_error: io::Error, io_error: io::Error,

@ -2,11 +2,11 @@
use std::{ use std::{
borrow::Cow, borrow::Cow,
error::Error, error::Error,
ffi::{CStr, OsStr, OsString}, ffi::{c_char, CStr, OsStr, OsString},
fs, fs,
io::{self, BufRead, Cursor, Read}, io::{self, BufRead, Cursor, Read},
mem, mem,
os::{fd::AsFd as _, raw::c_char, unix::ffi::OsStrExt}, os::{fd::AsFd as _, unix::ffi::OsStrExt},
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::LazyLock, sync::LazyLock,
}; };

@ -1,6 +1,6 @@
use std::{ use std::{
cmp, cmp,
ffi::{c_char, CStr, CString}, ffi::{c_char, c_long, CStr, CString},
io, iter, io, iter,
mem::{self, MaybeUninit}, mem::{self, MaybeUninit},
os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, FromRawFd as _, RawFd}, os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, FromRawFd as _, RawFd},
@ -108,7 +108,7 @@ pub(crate) fn bpf_create_map(
unsafe { fd_sys_bpf(bpf_cmd::BPF_MAP_CREATE, &mut attr) } unsafe { fd_sys_bpf(bpf_cmd::BPF_MAP_CREATE, &mut attr) }
} }
pub(crate) fn bpf_pin_object(fd: BorrowedFd<'_>, path: &CStr) -> SysResult<i64> { pub(crate) fn bpf_pin_object(fd: BorrowedFd<'_>, path: &CStr) -> SysResult<c_long> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_4 }; let u = unsafe { &mut attr.__bindgen_anon_4 };
u.bpf_fd = fd.as_raw_fd() as u32; u.bpf_fd = fd.as_raw_fd() as u32;
@ -289,7 +289,7 @@ pub(crate) fn bpf_map_update_elem<K: Pod, V: Pod>(
key: Option<&K>, key: Option<&K>,
value: &V, value: &V,
flags: u64, flags: u64,
) -> SysResult<i64> { ) -> SysResult<c_long> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_2 }; let u = unsafe { &mut attr.__bindgen_anon_2 };
@ -307,7 +307,7 @@ pub(crate) fn bpf_map_push_elem<V: Pod>(
fd: BorrowedFd<'_>, fd: BorrowedFd<'_>,
value: &V, value: &V,
flags: u64, flags: u64,
) -> SysResult<i64> { ) -> SysResult<c_long> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_2 }; let u = unsafe { &mut attr.__bindgen_anon_2 };
@ -323,7 +323,7 @@ pub(crate) fn bpf_map_update_elem_ptr<K, V>(
key: *const K, key: *const K,
value: *mut V, value: *mut V,
flags: u64, flags: u64,
) -> SysResult<i64> { ) -> SysResult<c_long> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_2 }; let u = unsafe { &mut attr.__bindgen_anon_2 };
@ -340,12 +340,12 @@ pub(crate) fn bpf_map_update_elem_per_cpu<K: Pod, V: Pod>(
key: &K, key: &K,
values: &PerCpuValues<V>, values: &PerCpuValues<V>,
flags: u64, flags: u64,
) -> SysResult<i64> { ) -> SysResult<c_long> {
let mut mem = values.build_kernel_mem().map_err(|e| (-1, e))?; let mut mem = values.build_kernel_mem().map_err(|e| (-1, e))?;
bpf_map_update_elem_ptr(fd, key, mem.as_mut_ptr(), flags) bpf_map_update_elem_ptr(fd, key, mem.as_mut_ptr(), flags)
} }
pub(crate) fn bpf_map_delete_elem<K: Pod>(fd: BorrowedFd<'_>, key: &K) -> SysResult<i64> { pub(crate) fn bpf_map_delete_elem<K: Pod>(fd: BorrowedFd<'_>, key: &K) -> SysResult<c_long> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_2 }; let u = unsafe { &mut attr.__bindgen_anon_2 };
@ -377,7 +377,7 @@ pub(crate) fn bpf_map_get_next_key<K: Pod>(
} }
// since kernel 5.2 // since kernel 5.2
pub(crate) fn bpf_map_freeze(fd: BorrowedFd<'_>) -> SysResult<i64> { pub(crate) fn bpf_map_freeze(fd: BorrowedFd<'_>) -> SysResult<c_long> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let u = unsafe { &mut attr.__bindgen_anon_2 }; let u = unsafe { &mut attr.__bindgen_anon_2 };
u.map_fd = fd.as_raw_fd() as u32; u.map_fd = fd.as_raw_fd() as u32;
@ -453,7 +453,7 @@ pub(crate) fn bpf_link_update(
new_prog_fd: BorrowedFd<'_>, new_prog_fd: BorrowedFd<'_>,
old_prog_fd: Option<RawFd>, old_prog_fd: Option<RawFd>,
flags: u32, flags: u32,
) -> SysResult<i64> { ) -> SysResult<c_long> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
attr.link_update.link_fd = link_fd.as_raw_fd() as u32; attr.link_update.link_fd = link_fd.as_raw_fd() as u32;
@ -528,7 +528,7 @@ pub(crate) fn bpf_prog_query(
prog_ids: &mut [u32], prog_ids: &mut [u32],
prog_cnt: &mut u32, prog_cnt: &mut u32,
revision: &mut u64, revision: &mut u64,
) -> SysResult<i64> { ) -> SysResult<c_long> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
match target { match target {
@ -1164,7 +1164,7 @@ fn bpf_prog_load(attr: &mut bpf_attr) -> SysResult<crate::MockableFd> {
unsafe { fd_sys_bpf(bpf_cmd::BPF_PROG_LOAD, attr) } unsafe { fd_sys_bpf(bpf_cmd::BPF_PROG_LOAD, attr) }
} }
fn sys_bpf(cmd: bpf_cmd, attr: &mut bpf_attr) -> SysResult<i64> { fn sys_bpf(cmd: bpf_cmd, attr: &mut bpf_attr) -> SysResult<c_long> {
syscall(Syscall::Ebpf { cmd, attr }) syscall(Syscall::Ebpf { cmd, attr })
} }

@ -1,8 +1,12 @@
use std::{cell::RefCell, ffi::c_void, io, ptr}; use std::{
cell::RefCell,
ffi::{c_long, c_void},
io, ptr,
};
use super::{SysResult, Syscall}; use super::{SysResult, Syscall};
type SyscallFn = unsafe fn(Syscall<'_>) -> SysResult<i64>; type SyscallFn = unsafe fn(Syscall<'_>) -> SysResult<c_long>;
#[cfg(test)] #[cfg(test)]
thread_local! { thread_local! {
@ -11,11 +15,11 @@ thread_local! {
} }
#[cfg(test)] #[cfg(test)]
unsafe fn test_syscall(_call: Syscall<'_>) -> SysResult<i64> { unsafe fn test_syscall(_call: Syscall<'_>) -> SysResult<c_long> {
Err((-1, io::Error::from_raw_os_error(libc::EINVAL))) Err((-1, io::Error::from_raw_os_error(libc::EINVAL)))
} }
#[cfg(test)] #[cfg(test)]
pub(crate) fn override_syscall(call: unsafe fn(Syscall<'_>) -> SysResult<i64>) { pub(crate) fn override_syscall(call: unsafe fn(Syscall<'_>) -> SysResult<c_long>) {
TEST_SYSCALL.with(|test_impl| *test_impl.borrow_mut() = call); TEST_SYSCALL.with(|test_impl| *test_impl.borrow_mut() = call);
} }

@ -8,7 +8,7 @@ mod perf_event;
mod fake; mod fake;
use std::{ use std::{
ffi::{c_int, c_void}, ffi::{c_int, c_long, c_void},
io, mem, io, mem,
os::fd::{AsRawFd as _, BorrowedFd, OwnedFd}, os::fd::{AsRawFd as _, BorrowedFd, OwnedFd},
}; };
@ -16,7 +16,7 @@ use std::{
pub(crate) use bpf::*; pub(crate) use bpf::*;
#[cfg(test)] #[cfg(test)]
pub(crate) use fake::*; pub(crate) use fake::*;
use libc::{pid_t, SYS_bpf, SYS_perf_event_open}; use libc::{pid_t, SYS_bpf, SYS_ioctl, SYS_perf_event_open};
#[doc(hidden)] #[doc(hidden)]
pub use netlink::netlink_set_link_up; pub use netlink::netlink_set_link_up;
pub(crate) use netlink::*; pub(crate) use netlink::*;
@ -25,7 +25,7 @@ use thiserror::Error;
use crate::generated::{bpf_attr, bpf_cmd, perf_event_attr}; use crate::generated::{bpf_attr, bpf_cmd, perf_event_attr};
pub(crate) type SysResult<T> = Result<T, (i64, io::Error)>; pub(crate) type SysResult<T> = Result<T, (c_long, io::Error)>;
pub(crate) enum Syscall<'a> { pub(crate) enum Syscall<'a> {
Ebpf { Ebpf {
@ -89,7 +89,7 @@ impl std::fmt::Debug for Syscall<'_> {
} }
} }
fn syscall(call: Syscall<'_>) -> SysResult<i64> { fn syscall(call: Syscall<'_>) -> SysResult<c_long> {
#[cfg(test)] #[cfg(test)]
return TEST_SYSCALL.with(|test_impl| unsafe { test_impl.borrow()(call) }); return TEST_SYSCALL.with(|test_impl| unsafe { test_impl.borrow()(call) });
@ -108,22 +108,13 @@ fn syscall(call: Syscall<'_>) -> SysResult<i64> {
flags, flags,
} => libc::syscall(SYS_perf_event_open, &attr, pid, cpu, group, flags), } => libc::syscall(SYS_perf_event_open, &attr, pid, cpu, group, flags),
Syscall::PerfEventIoctl { fd, request, arg } => { Syscall::PerfEventIoctl { fd, request, arg } => {
// The type of integer taken by `ioctl` is different in glibc (i64) and libc::syscall(SYS_ioctl, fd.as_raw_fd(), request, arg)
// musl (i32). musl builds would complain about useless conversion.
#[allow(clippy::useless_conversion)]
let request = request.try_into().unwrap();
let ret = libc::ioctl(fd.as_raw_fd(), request, 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. match ret {
#[allow(clippy::useless_conversion)] 0.. => Ok(ret),
match ret.into() {
ret @ 0.. => Ok(ret),
ret => Err((ret, io::Error::last_os_error())), ret => Err((ret, io::Error::last_os_error())),
} }
} }

@ -1,5 +1,5 @@
use std::{ use std::{
ffi::{c_int, CString, OsStr}, ffi::{c_int, c_long, CString, OsStr},
io, mem, io, mem,
os::fd::{BorrowedFd, FromRawFd as _}, os::fd::{BorrowedFd, FromRawFd as _},
}; };
@ -104,7 +104,11 @@ pub(crate) fn perf_event_open_trace_point(
perf_event_sys(attr, pid, cpu, PERF_FLAG_FD_CLOEXEC) 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<i64> { pub(crate) fn perf_event_ioctl(
fd: BorrowedFd<'_>,
request: c_int,
arg: c_int,
) -> SysResult<c_long> {
let call = Syscall::PerfEventIoctl { fd, request, arg }; let call = Syscall::PerfEventIoctl { fd, request, arg };
#[cfg(not(test))] #[cfg(not(test))]
return syscall(call); return syscall(call);

@ -1336,7 +1336,7 @@ impl<T> core::convert::From<T> for aya::maps::Map
pub fn aya::maps::Map::from(t: T) -> T pub fn aya::maps::Map::from(t: T) -> T
pub enum aya::maps::MapError pub enum aya::maps::MapError
pub aya::maps::MapError::CreateError pub aya::maps::MapError::CreateError
pub aya::maps::MapError::CreateError::code: i64 pub aya::maps::MapError::CreateError::code: core::ffi::c_long
pub aya::maps::MapError::CreateError::io_error: std::io::error::Error pub aya::maps::MapError::CreateError::io_error: std::io::error::Error
pub aya::maps::MapError::CreateError::name: alloc::string::String pub aya::maps::MapError::CreateError::name: alloc::string::String
pub aya::maps::MapError::ElementNotFound pub aya::maps::MapError::ElementNotFound

Loading…
Cancel
Save