From 0878c4505a95931c745b6b0bdbcb5413579acd85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alessandro=C2=A0Decina?= Date: Sun, 4 Jul 2021 00:48:07 +0000 Subject: [PATCH] aya: fix clippy warnings --- aya/src/bpf.rs | 2 +- aya/src/generated/mod.rs | 2 +- aya/src/lib.rs | 1 + aya/src/maps/array/array.rs | 4 +- aya/src/maps/array/mod.rs | 1 + aya/src/maps/array/per_cpu_array.rs | 6 +- aya/src/maps/array/program_array.rs | 2 +- aya/src/maps/hash_map/hash_map.rs | 54 +++++++-------- aya/src/maps/hash_map/mod.rs | 6 +- aya/src/maps/hash_map/per_cpu_hash_map.rs | 2 +- aya/src/maps/mod.rs | 22 +++---- aya/src/maps/perf/perf_buffer.rs | 2 +- aya/src/maps/perf/perf_event_array.rs | 2 +- aya/src/maps/queue.rs | 2 +- aya/src/maps/sock/sock_hash.rs | 2 +- aya/src/maps/sock/sock_map.rs | 2 +- aya/src/maps/stack.rs | 2 +- aya/src/maps/stack_trace.rs | 2 +- aya/src/obj/btf/btf.rs | 2 +- aya/src/obj/btf/mod.rs | 1 + aya/src/obj/btf/relocation.rs | 80 ++++++++++++++--------- aya/src/obj/btf/types.rs | 5 +- aya/src/obj/mod.rs | 27 ++++---- aya/src/obj/relocation.rs | 5 +- aya/src/programs/probe.rs | 2 +- aya/src/programs/socket_filter.rs | 2 +- aya/src/programs/tc.rs | 2 +- aya/src/programs/uprobe.rs | 9 ++- aya/src/programs/xdp.rs | 2 +- aya/src/sys/bpf.rs | 4 +- aya/src/sys/fake.rs | 2 +- aya/src/sys/netlink.rs | 14 ++-- aya/src/util.rs | 17 +++-- 33 files changed, 156 insertions(+), 134 deletions(-) diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index 00c08870..77863261 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -280,7 +280,7 @@ impl Bpf { /// } /// # Ok::<(), aya::BpfError>(()) /// ``` - pub fn maps<'a>(&'a self) -> impl Iterator)> + 'a { + pub fn maps(&self) -> impl Iterator)> { let ret = self.maps.iter().map(|(name, lock)| { ( name.as_str(), diff --git a/aya/src/generated/mod.rs b/aya/src/generated/mod.rs index 3c413f32..3c269ade 100644 --- a/aya/src/generated/mod.rs +++ b/aya/src/generated/mod.rs @@ -1,4 +1,4 @@ -#![allow(dead_code, non_camel_case_types, non_snake_case)] +#![allow(dead_code, non_camel_case_types, non_snake_case, clippy::all)] mod btf_internal_bindings; #[cfg(target_arch = "aarch64")] diff --git a/aya/src/lib.rs b/aya/src/lib.rs index 4483cc16..e4e2b906 100644 --- a/aya/src/lib.rs +++ b/aya/src/lib.rs @@ -30,6 +30,7 @@ //! [tokio]: https://docs.rs/tokio //! [async-std]: https://docs.rs/async-std #![deny(clippy::all)] +#![allow(clippy::missing_safety_doc, clippy::len_without_is_empty)] #[macro_use] extern crate lazy_static; diff --git a/aya/src/maps/array/array.rs b/aya/src/maps/array/array.rs index bc920573..8aa9abce 100644 --- a/aya/src/maps/array/array.rs +++ b/aya/src/maps/array/array.rs @@ -44,7 +44,7 @@ impl, V: Pod> Array { if map_type != BPF_MAP_TYPE_ARRAY as u32 { return Err(MapError::InvalidMapType { map_type: map_type as u32, - })?; + }); } let expected = mem::size_of::(); let size = map.obj.def.key_size as usize; @@ -94,7 +94,7 @@ impl, V: Pod> Array { /// An iterator over the elements of the array. The iterator item type is `Result`. - pub unsafe fn iter<'a>(&'a self) -> impl Iterator> + 'a { + pub unsafe fn iter(&self) -> impl Iterator> + '_ { (0..self.len()).map(move |i| self.get(&i, 0)) } diff --git a/aya/src/maps/array/mod.rs b/aya/src/maps/array/mod.rs index 8bc35e42..5b762e27 100644 --- a/aya/src/maps/array/mod.rs +++ b/aya/src/maps/array/mod.rs @@ -1,4 +1,5 @@ //! Array types. +#[allow(clippy::module_inception)] mod array; mod per_cpu_array; mod program_array; diff --git a/aya/src/maps/array/per_cpu_array.rs b/aya/src/maps/array/per_cpu_array.rs index e4116d64..400553b8 100644 --- a/aya/src/maps/array/per_cpu_array.rs +++ b/aya/src/maps/array/per_cpu_array.rs @@ -63,7 +63,7 @@ impl, V: Pod> PerCpuArray { if map_type != BPF_MAP_TYPE_PERCPU_ARRAY as u32 { return Err(MapError::InvalidMapType { map_type: map_type as u32, - })?; + }); } let expected = mem::size_of::(); let size = map.obj.def.key_size as usize; @@ -113,9 +113,7 @@ impl, V: Pod> PerCpuArray { /// An iterator over the elements of the array. The iterator item type is /// `Result, MapError>`. - pub unsafe fn iter<'a>( - &'a self, - ) -> impl Iterator, MapError>> + 'a { + pub unsafe fn iter(&self) -> impl Iterator, MapError>> + '_ { (0..self.len()).map(move |i| self.get(&i, 0)) } diff --git a/aya/src/maps/array/program_array.rs b/aya/src/maps/array/program_array.rs index a2134810..840033a2 100644 --- a/aya/src/maps/array/program_array.rs +++ b/aya/src/maps/array/program_array.rs @@ -59,7 +59,7 @@ impl> ProgramArray { if map_type != BPF_MAP_TYPE_PROG_ARRAY as u32 { return Err(MapError::InvalidMapType { map_type: map_type as u32, - })?; + }); } let expected = mem::size_of::(); let size = map.obj.def.key_size as usize; diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index 3ebdd1a7..f9d20c9d 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -48,7 +48,7 @@ impl, K: Pod, V: Pod> HashMap { if map_type != BPF_MAP_TYPE_HASH as u32 && map_type != BPF_MAP_TYPE_LRU_HASH as u32 { return Err(MapError::InvalidMapType { map_type: map_type as u32, - })?; + }); } hash_map::check_kv_size::(&map)?; let _ = map.fd_or_err()?; @@ -426,10 +426,10 @@ mod tests { } 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), - Some(20) => set_next_key(&attr, 30), + match bpf_key(attr) { + None => set_next_key(attr, 10), + Some(10) => set_next_key(attr, 20), + Some(20) => set_next_key(attr, 30), Some(30) => return sys_error(ENOENT), Some(_) => return sys_error(EFAULT), }; @@ -438,10 +438,10 @@ mod tests { } fn lookup_elem(attr: &bpf_attr) -> SysResult { - match bpf_key(&attr) { - Some(10) => set_ret(&attr, 100), - Some(20) => set_ret(&attr, 200), - Some(30) => set_ret(&attr, 300), + match bpf_key(attr) { + Some(10) => set_ret(attr, 100), + Some(20) => set_ret(attr, 200), + Some(30) => set_ret(attr, 300), Some(_) => return sys_error(ENOENT), None => return sys_error(EFAULT), }; @@ -455,7 +455,7 @@ mod tests { Syscall::Bpf { cmd: bpf_cmd::BPF_MAP_GET_NEXT_KEY, attr, - } => get_next_key(&attr), + } => get_next_key(attr), _ => sys_error(EFAULT), }); @@ -476,9 +476,9 @@ mod tests { cmd: bpf_cmd::BPF_MAP_GET_NEXT_KEY, attr, } => { - match bpf_key(&attr) { - None => set_next_key(&attr, 10), - Some(10) => set_next_key(&attr, 20), + match bpf_key(attr) { + None => set_next_key(attr, 10), + Some(10) => set_next_key(attr, 20), Some(_) => return sys_error(EFAULT), }; @@ -508,11 +508,11 @@ mod tests { Syscall::Bpf { cmd: bpf_cmd::BPF_MAP_GET_NEXT_KEY, attr, - } => get_next_key(&attr), + } => get_next_key(attr), Syscall::Bpf { cmd: bpf_cmd::BPF_MAP_LOOKUP_ELEM, attr, - } => lookup_elem(&attr), + } => lookup_elem(attr), _ => sys_error(EFAULT), }); let map = Map { @@ -530,15 +530,15 @@ mod tests { Syscall::Bpf { cmd: bpf_cmd::BPF_MAP_GET_NEXT_KEY, attr, - } => get_next_key(&attr), + } => get_next_key(attr), Syscall::Bpf { cmd: bpf_cmd::BPF_MAP_LOOKUP_ELEM, attr, } => { - match bpf_key(&attr) { - Some(10) => set_ret(&attr, 100), + match bpf_key(attr) { + Some(10) => set_ret(attr, 100), Some(20) => return sys_error(ENOENT), - Some(30) => set_ret(&attr, 300), + Some(30) => set_ret(attr, 300), Some(_) => return sys_error(ENOENT), None => return sys_error(EFAULT), }; @@ -564,9 +564,9 @@ mod tests { cmd: bpf_cmd::BPF_MAP_GET_NEXT_KEY, attr, } => { - match bpf_key(&attr) { - None => set_next_key(&attr, 10), - Some(10) => set_next_key(&attr, 20), + match bpf_key(attr) { + None => set_next_key(attr, 10), + Some(10) => set_next_key(attr, 20), Some(20) => return sys_error(EFAULT), Some(30) => return sys_error(ENOENT), Some(_) => panic!(), @@ -577,7 +577,7 @@ mod tests { Syscall::Bpf { cmd: bpf_cmd::BPF_MAP_LOOKUP_ELEM, attr, - } => lookup_elem(&attr), + } => lookup_elem(attr), _ => sys_error(EFAULT), }); let map = Map { @@ -602,15 +602,15 @@ mod tests { Syscall::Bpf { cmd: bpf_cmd::BPF_MAP_GET_NEXT_KEY, attr, - } => get_next_key(&attr), + } => get_next_key(attr), Syscall::Bpf { cmd: bpf_cmd::BPF_MAP_LOOKUP_ELEM, attr, } => { - match bpf_key(&attr) { - Some(10) => set_ret(&attr, 100), + match bpf_key(attr) { + Some(10) => set_ret(attr, 100), Some(20) => return sys_error(EFAULT), - Some(30) => set_ret(&attr, 300), + Some(30) => set_ret(attr, 300), Some(_) => return sys_error(ENOENT), None => return sys_error(EFAULT), }; diff --git a/aya/src/maps/hash_map/mod.rs b/aya/src/maps/hash_map/mod.rs index 531fbfdd..c6e99405 100644 --- a/aya/src/maps/hash_map/mod.rs +++ b/aya/src/maps/hash_map/mod.rs @@ -6,6 +6,7 @@ use crate::{ sys::{bpf_map_delete_elem, bpf_map_update_elem}, }; +#[allow(clippy::module_inception)] mod hash_map; mod per_cpu_hash_map; @@ -20,9 +21,10 @@ pub(crate) fn check_kv_size(map: &Map) -> Result<(), MapError> { } let size = mem::size_of::(); let expected = map.obj.def.value_size as usize; - Ok(if size != expected { + if size != expected { return Err(MapError::InvalidValueSize { size, expected }); - }) + }; + Ok(()) } pub(crate) fn insert(map: &mut Map, key: K, value: V, flags: u64) -> Result<(), MapError> { diff --git a/aya/src/maps/hash_map/per_cpu_hash_map.rs b/aya/src/maps/hash_map/per_cpu_hash_map.rs index fb23fb43..991282ed 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -60,7 +60,7 @@ impl, K: Pod, V: Pod> PerCpuHashMap { { return Err(MapError::InvalidMapType { map_type: map_type as u32, - })?; + }); } hash_map::check_kv_size::(&map)?; let _ = map.fd_or_err()?; diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index d5cf2fa3..bf7b1293 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -134,7 +134,7 @@ impl Map { pub fn create(&mut self) -> Result { let name = self.obj.name.clone(); if self.fd.is_some() { - return Err(MapError::AlreadyCreated { name: name.clone() }); + return Err(MapError::AlreadyCreated { name }); } let c_name = @@ -210,19 +210,19 @@ impl Iterator for MapKeys<'_, K> { match bpf_map_get_next_key(fd, self.key.as_ref()) { Ok(Some(key)) => { self.key = Some(key); - return Some(Ok(key)); + Some(Ok(key)) } Ok(None) => { self.key = None; - return None; + None } Err((code, io_error)) => { self.err = true; - return Some(Err(MapError::SyscallError { + Some(Err(MapError::SyscallError { call: "bpf_map_get_next_key".to_owned(), code, io_error, - })); + })) } } } @@ -366,7 +366,7 @@ impl TryFrom> for PerCpuValues { impl PerCpuValues { pub(crate) fn alloc_kernel_mem() -> Result { - let value_size = mem::size_of::() + 7 & !7; + let value_size = (mem::size_of::() + 7) & !7; Ok(PerCpuKernelMem { bytes: vec![0u8; nr_cpus()? * value_size], }) @@ -374,7 +374,7 @@ impl PerCpuValues { pub(crate) unsafe fn from_kernel_mem(mem: PerCpuKernelMem) -> PerCpuValues { let mem_ptr = mem.bytes.as_ptr() as usize; - let value_size = mem::size_of::() + 7 & !7; + let value_size = (mem::size_of::() + 7) & !7; let mut values = Vec::new(); let mut offset = 0; while offset < mem.bytes.len() { @@ -387,10 +387,10 @@ impl PerCpuValues { } } - pub(crate) fn into_kernel_mem(&self) -> Result { + pub(crate) fn build_kernel_mem(&self) -> Result { let mut mem = PerCpuValues::::alloc_kernel_mem()?; let mem_ptr = mem.as_mut_ptr() as usize; - let value_size = mem::size_of::() + 7 & !7; + let value_size = (mem::size_of::() + 7) & !7; for i in 0..self.values.len() { unsafe { ptr::write_unaligned((mem_ptr + i * value_size) as *mut _, self.values[i]) }; } @@ -459,9 +459,7 @@ mod tests { #[test] fn test_create_failed() { - override_syscall(|_| { - return Err((-42, io::Error::from_raw_os_error(EFAULT))); - }); + override_syscall(|_| Err((-42, io::Error::from_raw_os_error(EFAULT)))); let mut map = new_map("foo"); let ret = map.create(); diff --git a/aya/src/maps/perf/perf_buffer.rs b/aya/src/maps/perf/perf_buffer.rs index bdab9325..c288a2f3 100644 --- a/aya/src/maps/perf/perf_buffer.rs +++ b/aya/src/maps/perf/perf_buffer.rs @@ -250,7 +250,7 @@ impl PerfBuffer { atomic::fence(Ordering::SeqCst); unsafe { (*header).data_tail = tail as u64 }; - return Ok(events); + Ok(events) } } diff --git a/aya/src/maps/perf/perf_event_array.rs b/aya/src/maps/perf/perf_event_array.rs index 5a727eab..240cde18 100644 --- a/aya/src/maps/perf/perf_event_array.rs +++ b/aya/src/maps/perf/perf_event_array.rs @@ -171,7 +171,7 @@ impl> PerfEventArray { if map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY as u32 { return Err(MapError::InvalidMapType { map_type: map_type as u32, - })?; + }); } let _fd = map.fd_or_err()?; diff --git a/aya/src/maps/queue.rs b/aya/src/maps/queue.rs index d0b34a9c..f4b53e05 100644 --- a/aya/src/maps/queue.rs +++ b/aya/src/maps/queue.rs @@ -43,7 +43,7 @@ impl, V: Pod> Queue { if map_type != BPF_MAP_TYPE_QUEUE as u32 { return Err(MapError::InvalidMapType { map_type: map_type as u32, - })?; + }); } let expected = 0; let size = map.obj.def.key_size as usize; diff --git a/aya/src/maps/sock/sock_hash.rs b/aya/src/maps/sock/sock_hash.rs index ca5b5682..d3b02e00 100644 --- a/aya/src/maps/sock/sock_hash.rs +++ b/aya/src/maps/sock/sock_hash.rs @@ -75,7 +75,7 @@ impl, K: Pod> SockHash { if map_type != BPF_MAP_TYPE_SOCKHASH as u32 { return Err(MapError::InvalidMapType { map_type: map_type as u32, - })?; + }); } hash_map::check_kv_size::(&map)?; let _ = map.fd_or_err()?; diff --git a/aya/src/maps/sock/sock_map.rs b/aya/src/maps/sock/sock_map.rs index 47f6e95f..ae00769d 100644 --- a/aya/src/maps/sock/sock_map.rs +++ b/aya/src/maps/sock/sock_map.rs @@ -51,7 +51,7 @@ impl> SockMap { if map_type != BPF_MAP_TYPE_SOCKMAP as u32 { return Err(MapError::InvalidMapType { map_type: map_type as u32, - })?; + }); } let expected = mem::size_of::(); let size = map.obj.def.key_size as usize; diff --git a/aya/src/maps/stack.rs b/aya/src/maps/stack.rs index 304d4628..b86828ee 100644 --- a/aya/src/maps/stack.rs +++ b/aya/src/maps/stack.rs @@ -43,7 +43,7 @@ impl, V: Pod> Stack { if map_type != BPF_MAP_TYPE_STACK as u32 { return Err(MapError::InvalidMapType { map_type: map_type as u32, - })?; + }); } let expected = 0; let size = map.obj.def.key_size as usize; diff --git a/aya/src/maps/stack_trace.rs b/aya/src/maps/stack_trace.rs index ca20073d..d00189d9 100644 --- a/aya/src/maps/stack_trace.rs +++ b/aya/src/maps/stack_trace.rs @@ -77,7 +77,7 @@ impl> StackTraceMap { if map_type != BPF_MAP_TYPE_STACK_TRACE as u32 { return Err(MapError::InvalidMapType { map_type: map_type as u32, - })?; + }); } let expected = mem::size_of::(); let size = map.obj.def.key_size as usize; diff --git a/aya/src/obj/btf/btf.rs b/aya/src/obj/btf/btf.rs index 4e61d6eb..e3e98332 100644 --- a/aya/src/obj/btf/btf.rs +++ b/aya/src/obj/btf/btf.rs @@ -215,7 +215,7 @@ impl Btf { pub(crate) fn type_name(&self, ty: &BtfType) -> Result>, BtfError> { ty.name_offset() - .map(|off| Ok(self.string_at(off)?)) + .map(|off| self.string_at(off)) .transpose() } diff --git a/aya/src/obj/btf/mod.rs b/aya/src/obj/btf/mod.rs index 80b3efb3..ae117d69 100644 --- a/aya/src/obj/btf/mod.rs +++ b/aya/src/obj/btf/mod.rs @@ -1,3 +1,4 @@ +#[allow(clippy::module_inception)] mod btf; mod relocation; mod types; diff --git a/aya/src/obj/btf/relocation.rs b/aya/src/obj/btf/relocation.rs index b9bf1002..a21a2c96 100644 --- a/aya/src/obj/btf/relocation.rs +++ b/aya/src/obj/btf/relocation.rs @@ -162,7 +162,7 @@ impl Object { let section_name = local_btf.string_at(*sec_name_off)?; // FIXME - let parts = section_name.split("/").collect::>(); + let parts = section_name.split('/').collect::>(); if parts.len() < 2 { continue; } @@ -182,7 +182,7 @@ impl Object { &mut candidates_cache, ) { Ok(_) => {} - Err(ErrorWrapper::BtfError(e)) => return Err(e)?, + Err(ErrorWrapper::BtfError(e)) => return Err(e.into()), Err(ErrorWrapper::RelocationError(error)) => { return Err(BpfError::RelocationError { function: section_name.to_owned(), @@ -211,7 +211,8 @@ fn relocate_btf_program<'target>( index: ins_index, num_instructions: instructions.len(), relocation_number: rel.number, - })?; + } + .into()); } let local_ty = local_btf.type_by_id(rel.type_id)?; @@ -257,7 +258,7 @@ fn relocate_btf_program<'target>( if cand_spec.bit_offset != target_spec.bit_offset || cand_comp_rel.target.value != target_comp_rel.target.value { - Some(cand_name.clone()) + Some(cand_name) } else { None } @@ -267,7 +268,8 @@ fn relocate_btf_program<'target>( return Err(RelocationError::ConflictingCandidates { type_name: local_name.to_string(), candidates: conflicts, - })?; + } + .into()); } target_comp_rel } else { @@ -277,7 +279,7 @@ fn relocate_btf_program<'target>( ComputedRelocation::new(rel, &local_spec, None)? }; - comp_rel.apply(program, rel, local_btf, &target_btf)?; + comp_rel.apply(program, rel, local_btf, target_btf)?; } Ok(()) @@ -305,7 +307,7 @@ fn find_candidates<'target>( candidates.push(Candidate { name: name.to_owned(), - btf: &target_btf, + btf: target_btf, ty, type_id: type_id as u32, }); @@ -381,7 +383,7 @@ fn match_candidate<'target>( if accessor.name.is_some() { if let Some(next_id) = match_member( local_spec.btf, - &local_spec, + local_spec, accessor, candidate.btf, target_id, @@ -419,7 +421,8 @@ fn match_candidate<'target>( if target_spec.parts.len() == MAX_SPEC_LEN { return Err(RelocationError::MaximumNestingLevelReached { type_name: Some(candidate.name.clone()), - })?; + } + .into()); } target_spec.parts.push(accessor.index); @@ -470,13 +473,14 @@ fn match_member<'local, 'target>( let root_ty = target_spec.btf.type_by_id(target_spec.root_type_id)?; return Err(RelocationError::MaximumNestingLevelReached { type_name: target_spec.btf.err_type_name(root_ty), - })?; + } + .into()); } let bit_offset = member_bit_offset(target_ty.info().unwrap(), target_member); let target_name = &*target_btf.string_at(target_member.name_off)?; - if target_name == "" { + if target_name.is_empty() { let ret = match_member( local_btf, local_spec, @@ -532,7 +536,7 @@ impl<'a> AccessSpec<'a> { relocation: Relocation, ) -> Result, ErrorWrapper> { let parts = spec - .split(":") + .split(':') .map(|s| s.parse::()) .collect::, _>>() .map_err(|_| RelocationError::InvalidAccessString { @@ -550,7 +554,8 @@ impl<'a> AccessSpec<'a> { if parts != [0] { return Err(RelocationError::InvalidAccessString { access_str: spec.to_string(), - })?; + } + .into()); } AccessSpec { btf, @@ -566,17 +571,19 @@ impl<'a> AccessSpec<'a> { if parts.len() != 1 { return Err(RelocationError::InvalidAccessString { access_str: spec.to_string(), - })?; + } + .into()); } let index = parts[0]; if index >= members.len() { return Err(RelocationError::InvalidAccessIndex { type_name: btf.err_type_name(ty), spec: spec.to_string(), - index: index, + index, max_index: members.len(), error: "tried to access nonexistant enum variant".to_string(), - })?; + } + .into()); } let accessors = vec![Accessor { type_id, @@ -599,7 +606,8 @@ impl<'a> AccessSpec<'a> { relocation_kind: format!("{:?}", relocation.kind), type_kind: format!("{:?}", ty.kind()?.unwrap()), error: "enum relocation on non-enum type".to_string(), - })? + } + .into()) } }, @@ -626,10 +634,11 @@ impl<'a> AccessSpec<'a> { return Err(RelocationError::InvalidAccessIndex { type_name: btf.err_type_name(ty), spec: spec.to_string(), - index: index, + index, max_index: members.len(), error: "out of bounds struct or union access".to_string(), - })?; + } + .into()); } let member = members[index]; @@ -665,7 +674,8 @@ impl<'a> AccessSpec<'a> { index, max_index: array.nelems as usize, error: "array index out of bounds".to_string(), - })?; + } + .into()); } accessors.push(Accessor { type_id, @@ -682,7 +692,8 @@ impl<'a> AccessSpec<'a> { type_kind: format!("{:?}", ty.kind()), error: "field relocation on a type that doesn't have fields" .to_string(), - })?; + } + .into()); } }; } @@ -690,9 +701,9 @@ impl<'a> AccessSpec<'a> { AccessSpec { btf, root_type_id, - relocation, parts, accessors, + relocation, bit_offset, } } @@ -787,7 +798,8 @@ impl ComputedRelocation { relocation_number: rel.number, index: ins_index, error: format!("invalid src_reg={:x} expected {:x}", src_reg, BPF_K), - })?; + } + .into()); } ins.imm = target_value as i32; @@ -798,7 +810,8 @@ impl ComputedRelocation { relocation_number: rel.number, index: ins_index, error: format!("value `{}` overflows 16 bits offset field", target_value), - })?; + } + .into()); } ins.off = target_value as i16; @@ -823,7 +836,8 @@ impl ComputedRelocation { err_type_name(&target_btf.err_type_name(target_ty)), self.target.size, ), - })? + } + .into()) } } @@ -837,7 +851,8 @@ impl ComputedRelocation { relocation_number: rel.number, index: ins_index, error: format!("invalid target size {}", size), - })? + } + .into()) } } as u8; ins.code = ins.code & 0xE0 | size | ins.code & 0x07; @@ -860,7 +875,8 @@ impl ComputedRelocation { relocation_number: rel.number, index: ins_index, error: format!("invalid instruction class {:x}", class), - })? + } + .into()) } }; @@ -931,7 +947,8 @@ impl ComputedRelocation { relocation_kind: format!("{:?}", rel_kind), type_kind: format!("{:?}", ty.kind()), error: "invalid relocation kind for array type".to_string(), - })?; + } + .into()); } }; } @@ -947,7 +964,8 @@ impl ComputedRelocation { relocation_kind: format!("{:?}", rel.kind), type_kind: format!("{:?}", ty.kind()), error: "field relocation on a type that doesn't have fields".to_string(), - })?; + } + .into()); } }; @@ -967,7 +985,7 @@ impl ComputedRelocation { while bit_off + bit_size - byte_off * 8 > byte_size * 8 { if byte_size >= 8 { // the bitfield is larger than 8 bytes!? - return Err(BtfError::InvalidTypeInfo)?; + return Err(BtfError::InvalidTypeInfo.into()); } byte_size *= 2; byte_off = bit_off / 8 / byte_size * byte_size; @@ -983,6 +1001,8 @@ impl ComputedRelocation { size: 0, type_id: None, }; + + #[allow(clippy::wildcard_in_or_patterns)] match rel.kind { FieldByteOffset => { value.value = byte_off; diff --git a/aya/src/obj/btf/types.rs b/aya/src/obj/btf/types.rs index c2576b9d..fe9aa433 100644 --- a/aya/src/obj/btf/types.rs +++ b/aya/src/obj/btf/types.rs @@ -200,10 +200,7 @@ impl BtfType { } pub(crate) fn is_composite(&self) -> bool { - match self { - BtfType::Struct(_, _) | BtfType::Union(_, _) => true, - _ => false, - } + matches!(self, BtfType::Struct(_, _) | BtfType::Union(_, _)) } } diff --git a/aya/src/obj/mod.rs b/aya/src/obj/mod.rs index 3e169d2c..3d84c6d3 100644 --- a/aya/src/obj/mod.rs +++ b/aya/src/obj/mod.rs @@ -116,7 +116,7 @@ impl FromStr for ProgramSection { // parse the common case, eg "xdp/program_name" or // "sk_skb/stream_verdict/program_name" - let mut parts = section.rsplitn(2, "/").collect::>(); + let mut parts = section.rsplitn(2, '/').collect::>(); if parts.len() == 1 { parts.push(parts[0]); } @@ -132,7 +132,7 @@ impl FromStr for ProgramSection { _ if kind.starts_with("tracepoint") || kind.starts_with("tp") => { // tracepoint sections are named `tracepoint/category/event_name`, // and we want to parse the name as "category/event_name" - let name = section.splitn(2, "/").last().unwrap().to_owned(); + let name = section.splitn(2, '/').last().unwrap().to_owned(); TracePoint { name } } "socket_filter" => SocketFilter { name }, @@ -155,7 +155,7 @@ impl FromStr for ProgramSection { impl Object { pub(crate) fn parse(data: &[u8]) -> Result { - let obj = object::read::File::parse(data).map_err(|e| ParseError::ElfError(e))?; + let obj = object::read::File::parse(data).map_err(ParseError::ElfError)?; let endianness = obj.endianness(); let license = if let Some(section) = obj.section_by_name("license") { @@ -192,12 +192,12 @@ impl Object { bpf_obj.parse_section(Section::try_from(&s)?)?; } - return Ok(bpf_obj); + Ok(bpf_obj) } fn new(endianness: Endianness, license: CString, kernel_version: KernelVersion) -> Object { Object { - endianness: endianness.into(), + endianness, license, kernel_version, btf: None, @@ -300,17 +300,16 @@ impl Object { } fn parse_section(&mut self, mut section: Section) -> Result<(), BpfError> { - let mut parts = section.name.rsplitn(2, "/").collect::>(); + let mut parts = section.name.rsplitn(2, '/').collect::>(); parts.reverse(); - if parts.len() == 1 { - if parts[0] == "xdp" + if parts.len() == 1 + && (parts[0] == "xdp" || parts[0] == "sk_msg" || parts[0] == "sockops" - || parts[0] == "classifier" - { - parts.push(parts[0]); - } + || parts[0] == "classifier") + { + parts.push(parts[0]); } match section.name { @@ -322,7 +321,7 @@ impl Object { ".BTF" => self.parse_btf(§ion)?, ".BTF.ext" => self.parse_btf_ext(§ion)?, map if map.starts_with("maps/") => { - let name = map.splitn(2, "/").last().unwrap(); + let name = map.splitn(2, '/').last().unwrap(); self.maps .insert(name.to_string(), parse_map(§ion, name)?); } @@ -720,7 +719,7 @@ mod tests { pinning: 7, }; let mut buf = [0u8; 128]; - unsafe { ptr::write_unaligned(buf.as_mut_ptr() as *mut _, def.clone()) }; + unsafe { ptr::write_unaligned(buf.as_mut_ptr() as *mut _, def) }; assert_eq!(parse_map_def("foo", &buf).unwrap(), def); } diff --git a/aya/src/obj/relocation.rs b/aya/src/obj/relocation.rs index a18480d6..85a68cf1 100644 --- a/aya/src/obj/relocation.rs +++ b/aya/src/obj/relocation.rs @@ -216,9 +216,8 @@ impl<'a> FunctionLinker<'a> { program: &mut Function, fun: &Function, ) -> Result { - match self.linked_functions.get(&fun.address) { - Some(fun_ins_index) => return Ok(*fun_ins_index), // already linked - None => {} + if let Some(fun_ins_index) = self.linked_functions.get(&fun.address) { + return Ok(*fun_ins_index); }; // append fun.instructions to the program and record that `fun.address` has been inserted diff --git a/aya/src/programs/probe.rs b/aya/src/programs/probe.rs index ec260899..92d84ae6 100644 --- a/aya/src/programs/probe.rs +++ b/aya/src/programs/probe.rs @@ -75,7 +75,7 @@ fn read_sys_fs_perf_ret_probe(pmu: &str) -> Result { let data = fs::read_to_string(&file).map_err(|e| (file.clone(), e))?; - let mut parts = data.trim().splitn(2, ":").skip(1); + let mut parts = data.trim().splitn(2, ':').skip(1); let config = parts.next().ok_or_else(|| { ( file.clone(), diff --git a/aya/src/programs/socket_filter.rs b/aya/src/programs/socket_filter.rs index ac75f081..0b5b4518 100644 --- a/aya/src/programs/socket_filter.rs +++ b/aya/src/programs/socket_filter.rs @@ -87,7 +87,7 @@ impl SocketFilter { if ret < 0 { return Err(SocketFilterError::SoAttachBpfError { io_error: io::Error::last_os_error(), - })?; + }.into()); } Ok(self.data.link(SocketFilterLink { diff --git a/aya/src/programs/tc.rs b/aya/src/programs/tc.rs index 42547273..341a8f95 100644 --- a/aya/src/programs/tc.rs +++ b/aya/src/programs/tc.rs @@ -147,7 +147,7 @@ impl Drop for TcLink { impl Link for TcLink { fn detach(&mut self) -> Result<(), ProgramError> { - if let Some(_) = self.prog_fd.take() { + if self.prog_fd.take().is_some() { unsafe { netlink_qdisc_detach(self.if_index, &self.attach_type, self.priority) } .map_err(|io_error| TcError::NetlinkError { io_error })?; Ok(()) diff --git a/aya/src/programs/uprobe.rs b/aya/src/programs/uprobe.rs index 08405e78..8bedc22f 100644 --- a/aya/src/programs/uprobe.rs +++ b/aya/src/programs/uprobe.rs @@ -88,7 +88,7 @@ impl UProbe { let target_str = &*target.as_os_str().to_string_lossy(); let mut path = if let Some(pid) = pid { - find_lib_in_proc_maps(pid, &target_str).map_err(|io_error| UProbeError::FileError { + find_lib_in_proc_maps(pid, target_str).map_err(|io_error| UProbeError::FileError { filename: format!("/proc/{}/maps", pid), io_error, })? @@ -231,10 +231,9 @@ impl LdSoCache { let mut buf = [0u8; LD_SO_CACHE_HEADER.len()]; cursor.read_exact(&mut buf)?; - let header = std::str::from_utf8(&buf).or(Err(io::Error::new( - io::ErrorKind::InvalidData, - "invalid ld.so.cache header", - )))?; + let header = std::str::from_utf8(&buf).map_err(|_| { + io::Error::new(io::ErrorKind::InvalidData, "invalid ld.so.cache header") + })?; if header != LD_SO_CACHE_HEADER { return Err(io::Error::new( io::ErrorKind::InvalidData, diff --git a/aya/src/programs/xdp.rs b/aya/src/programs/xdp.rs index acf2c53f..31a595c1 100644 --- a/aya/src/programs/xdp.rs +++ b/aya/src/programs/xdp.rs @@ -99,7 +99,7 @@ impl Xdp { if if_index == 0 { return Err(ProgramError::UnknownInterface { name: interface.to_string(), - })?; + }); } let k_ver = kernel_version().unwrap(); diff --git a/aya/src/sys/bpf.rs b/aya/src/sys/bpf.rs index 28dd4626..5ef2f1db 100644 --- a/aya/src/sys/bpf.rs +++ b/aya/src/sys/bpf.rs @@ -184,7 +184,7 @@ pub(crate) fn bpf_map_update_elem_per_cpu( values: &PerCpuValues, flags: u64, ) -> SysResult { - let mut mem = values.into_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) } @@ -264,6 +264,6 @@ pub(crate) fn bpf_prog_detach( sys_bpf(bpf_cmd::BPF_PROG_DETACH, &attr) } -fn sys_bpf<'a>(cmd: bpf_cmd, attr: &'a bpf_attr) -> SysResult { +fn sys_bpf(cmd: bpf_cmd, attr: &bpf_attr) -> SysResult { syscall(Syscall::Bpf { cmd, attr }) } diff --git a/aya/src/sys/fake.rs b/aya/src/sys/fake.rs index a72027b9..2c2c337c 100644 --- a/aya/src/sys/fake.rs +++ b/aya/src/sys/fake.rs @@ -14,7 +14,7 @@ thread_local! { #[cfg(test)] unsafe fn test_syscall(_call: Syscall) -> SysResult { - return Err((-1, io::Error::from_raw_os_error(libc::EINVAL))); + Err((-1, io::Error::from_raw_os_error(libc::EINVAL))) } #[cfg(test)] diff --git a/aya/src/sys/netlink.rs b/aya/src/sys/netlink.rs index 23af030d..7a495b8c 100644 --- a/aya/src/sys/netlink.rs +++ b/aya/src/sys/netlink.rs @@ -76,7 +76,7 @@ pub(crate) unsafe fn netlink_set_xdp_fd( 0, ) < 0 { - return Err(io::Error::last_os_error())?; + return Err(io::Error::last_os_error()); } sock.recv()?; @@ -123,7 +123,7 @@ pub(crate) unsafe fn netlink_qdisc_add_clsact(if_index: i32) -> Result<(), io::E 0, ) < 0 { - return Err(io::Error::last_os_error())?; + return Err(io::Error::last_os_error()); } sock.recv()?; @@ -184,7 +184,7 @@ pub(crate) unsafe fn netlink_qdisc_attach( 0, ) < 0 { - return Err(io::Error::last_os_error())?; + return Err(io::Error::last_os_error()); } // find the RTM_NEWTFILTER reply and read the tcm_info field which we'll @@ -242,7 +242,7 @@ pub(crate) unsafe fn netlink_qdisc_detach( 0, ) < 0 { - return Err(io::Error::last_os_error())?; + return Err(io::Error::last_os_error()); } sock.recv()?; @@ -274,7 +274,7 @@ impl NetlinkSocket { // Safety: libc wrapper let sock = unsafe { socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE) }; if sock < 0 { - return Err(io::Error::last_os_error())?; + return Err(io::Error::last_os_error()); } let enable = 1i32; @@ -296,7 +296,7 @@ impl NetlinkSocket { // Safety: libc wrapper if unsafe { getsockname(sock, &mut addr as *mut _ as *mut _, &mut addr_len as *mut _) } < 0 { - return Err(io::Error::last_os_error())?; + return Err(io::Error::last_os_error()); } Ok(NetlinkSocket { @@ -314,7 +314,7 @@ impl NetlinkSocket { // Safety: libc wrapper let len = unsafe { recv(self.sock, buf.as_mut_ptr() as *mut _, buf.len(), 0) }; if len < 0 { - return Err(io::Error::last_os_error())?; + return Err(io::Error::last_os_error()); } if len == 0 { break; diff --git a/aya/src/util.rs b/aya/src/util.rs index 0e45913a..a3bb2aa2 100644 --- a/aya/src/util.rs +++ b/aya/src/util.rs @@ -108,8 +108,6 @@ pub(crate) fn tc_handler_make(major: u32, minor: u32) -> u32 { #[cfg(test)] mod tests { - use std::iter::FromIterator; - use super::*; #[test] @@ -117,9 +115,18 @@ mod tests { assert_eq!(parse_cpu_ranges("0").unwrap(), vec![0]); assert_eq!(parse_cpu_ranges("0,1").unwrap(), vec![0, 1]); assert_eq!(parse_cpu_ranges("0,1,2").unwrap(), vec![0, 1, 2]); - assert_eq!(parse_cpu_ranges("0-7").unwrap(), Vec::from_iter(0..=7)); - assert_eq!(parse_cpu_ranges("0-3,4-7").unwrap(), Vec::from_iter(0..=7)); - assert_eq!(parse_cpu_ranges("0-5,6,7").unwrap(), Vec::from_iter(0..=7)); + assert_eq!( + parse_cpu_ranges("0-7").unwrap(), + (0..=7).collect::>() + ); + assert_eq!( + parse_cpu_ranges("0-3,4-7").unwrap(), + (0..=7).collect::>() + ); + assert_eq!( + parse_cpu_ranges("0-5,6,7").unwrap(), + (0..=7).collect::>() + ); assert!(parse_cpu_ranges("").is_err()); assert!(parse_cpu_ranges("0-1,2-").is_err()); assert!(parse_cpu_ranges("foo").is_err());