From f5af33148ae3ccc539ac5ea810f4f44eb19e40a5 Mon Sep 17 00:00:00 2001 From: Adam Preuss Date: Tue, 31 Oct 2023 21:05:34 +0000 Subject: [PATCH] aya: extracting program and map names with the same function --- aya/src/maps/mod.rs | 14 ++------------ aya/src/programs/mod.rs | 14 ++------------ aya/src/util.rs | 12 ++++++++++++ 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 577e41038..8ebe43e6e 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -73,7 +73,7 @@ use crate::{ bpf_map_get_info_by_fd, bpf_map_get_next_key, bpf_map_update_elem_ptr, bpf_pin_object, iter_map_ids, SyscallError, }, - util::{nr_cpus, KernelVersion}, + util::{bytes_of_bpf_name, nr_cpus, KernelVersion}, PinningType, Pod, }; @@ -927,17 +927,7 @@ impl MapInfo { /// The name of the map, limited to 16 bytes. pub fn name(&self) -> &[u8] { - let length = self - .0 - .name - .iter() - .rposition(|ch| *ch != 0) - .map(|pos| pos + 1) - .unwrap_or(0); - - // The name field is defined as [std::os::raw::c_char; 16]. c_char may be signed or - // unsigned depending on the platform; that's why we're using from_raw_parts here. - unsafe { std::slice::from_raw_parts(self.0.name.as_ptr() as *const _, length) } + bytes_of_bpf_name(&self.0.name) } /// The name of the map as a &str. If the name is not valid unicode, None is returned. diff --git a/aya/src/programs/mod.rs b/aya/src/programs/mod.rs index 64da0fdff..fcbbce867 100644 --- a/aya/src/programs/mod.rs +++ b/aya/src/programs/mod.rs @@ -117,7 +117,7 @@ use crate::{ bpf_prog_query, iter_link_ids, iter_prog_ids, retry_with_verifier_logs, BpfLoadProgramAttrs, SyscallError, }, - util::KernelVersion, + util::{bytes_of_bpf_name, KernelVersion}, VerifierLogLevel, }; @@ -1002,17 +1002,7 @@ impl ProgramInfo { /// The name of the program as was provided when it was load. This is limited to 16 bytes pub fn name(&self) -> &[u8] { - let length = self - .0 - .name - .iter() - .rposition(|ch| *ch != 0) - .map(|pos| pos + 1) - .unwrap_or(0); - - // The name field is defined as [std::os::raw::c_char; 16]. c_char may be signed or - // unsigned depending on the platform; that's why we're using from_raw_parts here - unsafe { std::slice::from_raw_parts(self.0.name.as_ptr() as *const _, length) } + bytes_of_bpf_name(&self.0.name) } /// The name of the program as a &str. If the name was not valid unicode, None is returned. diff --git a/aya/src/util.rs b/aya/src/util.rs index 3184dae4f..f673b4718 100644 --- a/aya/src/util.rs +++ b/aya/src/util.rs @@ -364,6 +364,18 @@ pub(crate) fn bytes_of_slice(val: &[T]) -> &[u8] { unsafe { slice::from_raw_parts(val.as_ptr().cast(), size) } } +pub(crate) fn bytes_of_bpf_name(bpf_name: &[::core::ffi::c_char]) -> &[u8] { + let length = bpf_name + .iter() + .rposition(|ch| *ch != 0) + .map(|pos| pos + 1) + .unwrap_or(0); + + // The name field is defined as [std::os::raw::c_char; 16]. c_char may be signed or + // unsigned depending on the platform; that's why we're using from_raw_parts here + unsafe { std::slice::from_raw_parts(bpf_name.as_ptr() as *const _, length) } +} + #[cfg(test)] mod tests { use assert_matches::assert_matches;