From 27120b328aac5f992eed98b03216a9880a381749 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 10 Jul 2023 11:52:37 -0400 Subject: [PATCH] aya: don't allocate static strings --- aya-bpf-macros/src/expand.rs | 42 ++++---- aya-obj/src/btf/btf.rs | 114 +++++++++++----------- aya-obj/src/btf/relocation.rs | 39 ++++---- aya-obj/src/btf/types.rs | 6 +- aya-obj/src/obj.rs | 4 +- aya-tool/src/generate.rs | 16 +-- aya/src/bpf.rs | 4 +- aya/src/maps/array/array.rs | 4 +- aya/src/maps/array/per_cpu_array.rs | 4 +- aya/src/maps/array/program_array.rs | 4 +- aya/src/maps/bloom_filter.rs | 4 +- aya/src/maps/hash_map/hash_map.rs | 2 +- aya/src/maps/hash_map/mod.rs | 4 +- aya/src/maps/hash_map/per_cpu_hash_map.rs | 4 +- aya/src/maps/lpm_trie.rs | 6 +- aya/src/maps/mod.rs | 14 +-- aya/src/maps/queue.rs | 4 +- aya/src/maps/sock/sock_hash.rs | 2 +- aya/src/maps/sock/sock_map.rs | 4 +- aya/src/maps/stack.rs | 4 +- aya/src/maps/stack_trace.rs | 8 +- aya/src/pin.rs | 2 +- aya/src/programs/cgroup_device.rs | 4 +- aya/src/programs/cgroup_skb.rs | 4 +- aya/src/programs/cgroup_sock.rs | 4 +- aya/src/programs/cgroup_sock_addr.rs | 4 +- aya/src/programs/cgroup_sockopt.rs | 4 +- aya/src/programs/cgroup_sysctl.rs | 4 +- aya/src/programs/extension.rs | 12 +-- aya/src/programs/links.rs | 6 +- aya/src/programs/lirc_mode2.rs | 6 +- aya/src/programs/mod.rs | 28 +++--- aya/src/programs/perf_attach.rs | 6 +- aya/src/programs/perf_event.rs | 2 +- aya/src/programs/probe.rs | 6 +- aya/src/programs/sk_lookup.rs | 2 +- aya/src/programs/sk_msg.rs | 2 +- aya/src/programs/sk_skb.rs | 2 +- aya/src/programs/sock_ops.rs | 2 +- aya/src/programs/tc.rs | 6 +- aya/src/programs/trace_point.rs | 2 +- aya/src/programs/utils.rs | 2 +- aya/src/programs/xdp.rs | 6 +- aya/src/sys/bpf.rs | 34 +++---- xtask/src/build_ebpf.rs | 4 +- xtask/src/codegen/mod.rs | 4 +- 46 files changed, 227 insertions(+), 224 deletions(-) diff --git a/aya-bpf-macros/src/expand.rs b/aya-bpf-macros/src/expand.rs index b96db0fe..c419172f 100644 --- a/aya-bpf-macros/src/expand.rs +++ b/aya-bpf-macros/src/expand.rs @@ -93,7 +93,7 @@ impl Map { } pub fn expand(&self) -> Result { - let section_name = "maps".to_string(); + let section_name = "maps"; let name = &self.name; let item = &self.item; Ok(quote! { @@ -148,10 +148,10 @@ impl SockOps { } pub fn expand(&self) -> Result { - let section_name = if let Some(name) = &self.name { - format!("sockops/{name}") + let section_name: std::borrow::Cow<'_, _> = if let Some(name) = &self.name { + format!("sockops/{name}").into() } else { - "sockops".to_owned() + "sockops".into() }; let fn_vis = &self.item.vis; let fn_name = &self.item.sig.ident; @@ -256,10 +256,10 @@ impl SchedClassifier { } pub fn expand(&self) -> Result { - let section_name = if let Some(name) = &self.name { - format!("classifier/{name}") + let section_name: std::borrow::Cow<'_, _> = if let Some(name) = &self.name { + format!("classifier/{name}").into() } else { - "classifier".to_owned() + "classifier".into() }; let fn_vis = &self.item.vis; let fn_name = &self.item.sig.ident; @@ -452,16 +452,16 @@ impl CgroupSock { } pub fn expand(&self) -> Result { - let section_name = if let Some(name) = &self.name { + let section_name: std::borrow::Cow<'_, _> = if let Some(name) = &self.name { if let Some(attach_type) = &self.attach_type { - format!("cgroup/{attach_type}/{name}") + format!("cgroup/{attach_type}/{name}").into() } else { - format!("cgroup/sock/{name}") + format!("cgroup/sock/{name}").into() } } else if let Some(attach_type) = &self.attach_type { - format!("cgroup/{attach_type}") + format!("cgroup/{attach_type}").into() } else { - "cgroup/sock".to_string() + "cgroup/sock".into() }; let fn_vis = &self.item.vis; let fn_name = &self.item.sig.ident; @@ -761,10 +761,10 @@ impl SocketFilter { } pub fn expand(&self) -> Result { - let section_name = if let Some(name) = &self.name { - format!("socket/{name}") + let section_name: std::borrow::Cow<'_, _> = if let Some(name) = &self.name { + format!("socket/{name}").into() } else { - "socket".to_owned() + "socket".into() }; let fn_vis = &self.item.vis; let fn_name = &self.item.sig.ident; @@ -854,10 +854,10 @@ impl SkLookup { } pub fn expand(&self) -> Result { - let section_name = if let Some(name) = &self.name { - format!("sk_lookup/{name}") + let section_name: std::borrow::Cow<'_, _> = if let Some(name) = &self.name { + format!("sk_lookup/{name}").into() } else { - "sk_lookup".to_owned() + "sk_lookup".into() }; let fn_vis = &self.item.vis; let fn_name = &self.item.sig.ident; @@ -887,10 +887,10 @@ impl CgroupDevice { } pub fn expand(&self) -> Result { - let section_name = if let Some(name) = &self.name { - format!("cgroup/dev/{name}") + let section_name: std::borrow::Cow<'_, _> = if let Some(name) = &self.name { + format!("cgroup/dev/{name}").into() } else { - ("cgroup/dev").to_owned() + "cgroup/dev".into() }; let fn_vis = &self.item.vis; let fn_name = &self.item.sig.ident; diff --git a/aya-obj/src/btf/btf.rs b/aya-obj/src/btf/btf.rs index cf68e403..a9049b1e 100644 --- a/aya-obj/src/btf/btf.rs +++ b/aya-obj/src/btf/btf.rs @@ -1,10 +1,9 @@ use core::{ffi::CStr, mem, ptr}; use alloc::{ - borrow::Cow, - ffi::CString, + borrow::{Cow, ToOwned as _}, format, - string::{String, ToString}, + string::String, vec, vec::Vec, }; @@ -276,10 +275,10 @@ impl Btf { } /// Adds a string to BTF metadata, returning an offset - pub fn add_string(&mut self, name: String) -> u32 { - let str = CString::new(name).unwrap(); + pub fn add_string(&mut self, name: &str) -> u32 { + let str = name.bytes().chain(std::iter::once(0)); let name_offset = self.strings.len(); - self.strings.extend(str.as_c_str().to_bytes_with_nul()); + self.strings.extend(str); self.header.str_len = self.strings.len() as u32; name_offset as u32 } @@ -424,7 +423,7 @@ impl Btf { } Err(BtfError::UnknownBtfTypeName { - type_name: name.to_string(), + type_name: name.to_owned(), }) } @@ -498,14 +497,13 @@ impl Btf { // STRUCT aren't allowed to have "." in their name, fixup this if needed. let mut name_offset = t.name_offset(); - let sec_name = self.string_at(name_offset)?; - let name = sec_name.to_string(); + let name = self.string_at(name_offset)?; // Handle any "." characters in struct names // Example: ".maps" let fixed_name = name.replace('.', "_"); if fixed_name != name { - name_offset = self.add_string(fixed_name); + name_offset = self.add_string(&fixed_name); } let mut members = vec![]; @@ -526,8 +524,8 @@ impl Btf { // we need to fix them here before loading the btf to the kernel BtfType::DataSec(d) if features.btf_datasec => { // Start DataSec Fixups - let sec_name = self.string_at(d.name_offset)?; - let name = sec_name.to_string(); + let name = self.string_at(d.name_offset)?; + let name = name.into_owned(); let mut fixed_ty = d.clone(); @@ -535,7 +533,7 @@ impl Btf { // Example: "maps/hashmap" let fixed_name = name.replace('/', "."); if fixed_name != name { - fixed_ty.name_offset = self.add_string(fixed_name); + fixed_ty.name_offset = self.add_string(&fixed_name); } // There are some cases when the compiler does indeed populate the @@ -546,11 +544,12 @@ impl Btf { // We need to get the size of the section from the ELF file // Fortunately, we cached these when parsing it initially // and we can this up by name in section_infos - let (_, size) = section_infos.get(&name).ok_or_else(|| { - BtfError::UnknownSectionSize { - section_name: name.clone(), + let size = match section_infos.get(&name) { + Some((_, size)) => size, + None => { + return Err(BtfError::UnknownSectionSize { section_name: name }); } - })?; + }; debug!("{} {}: fixup size to {}", kind, name, size); fixed_ty.size = *size as u32; @@ -563,7 +562,7 @@ impl Btf { let var_type = types.type_by_id(d.btf_type)?; let var_kind = var_type.kind(); if let BtfType::Var(var) = var_type { - let var_name = self.string_at(var.name_offset)?.to_string(); + let var_name = self.string_at(var.name_offset)?; if var.linkage == VarLinkage::Static { debug!( "{} {}: {} {}: fixup not required", @@ -572,11 +571,14 @@ impl Btf { continue; } - let offset = symbol_offsets.get(&var_name).ok_or( - BtfError::SymbolOffsetNotFound { - symbol_name: var_name.clone(), - }, - )?; + let offset = match symbol_offsets.get(var_name.as_ref()) { + Some(offset) => offset, + None => { + return Err(BtfError::SymbolOffsetNotFound { + symbol_name: var_name.into_owned(), + }); + } + }; d.offset = *offset as u32; debug!( "{} {}: {} {}: fixup offset {}", @@ -594,7 +596,7 @@ impl Btf { let mut ty = ty.clone(); for (i, param) in ty.params.iter_mut().enumerate() { if param.name_offset == 0 && param.btf_type != 0 { - param.name_offset = self.add_string(format!("param{i}")); + param.name_offset = self.add_string(&format!("param{i}")); } } types.types[i] = BtfType::FuncProto(ty); @@ -1163,11 +1165,11 @@ mod tests { #[test] fn test_write_btf() { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0)); btf.add_type(int_type); - let name_offset = btf.add_string("widget".to_string()); + let name_offset = btf.add_string("widget"); let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0)); btf.add_type(int_type); @@ -1189,7 +1191,7 @@ mod tests { #[test] fn test_fixup_ptr() { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type_id = btf.add_type(BtfType::Int(Int::new( name_offset, 4, @@ -1197,7 +1199,7 @@ mod tests { 0, ))); - let name_offset = btf.add_string("&mut int".to_string()); + let name_offset = btf.add_string("&mut int"); let ptr_type_id = btf.add_type(BtfType::Ptr(Ptr::new(name_offset, int_type_id))); let features = Default::default(); @@ -1221,7 +1223,7 @@ mod tests { #[test] fn test_sanitize_var() { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type_id = btf.add_type(BtfType::Int(Int::new( name_offset, 4, @@ -1229,7 +1231,7 @@ mod tests { 0, ))); - let name_offset = btf.add_string("&mut int".to_string()); + let name_offset = btf.add_string("&mut int"); let var_type_id = btf.add_type(BtfType::Var(Var::new( name_offset, int_type_id, @@ -1256,7 +1258,7 @@ mod tests { #[test] fn test_sanitize_datasec() { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type_id = btf.add_type(BtfType::Int(Int::new( name_offset, 4, @@ -1264,14 +1266,14 @@ mod tests { 0, ))); - let name_offset = btf.add_string("foo".to_string()); + let name_offset = btf.add_string("foo"); let var_type_id = btf.add_type(BtfType::Var(Var::new( name_offset, int_type_id, VarLinkage::Static, ))); - let name_offset = btf.add_string("data".to_string()); + let name_offset = btf.add_string("data"); let variables = vec![DataSecEntry { btf_type: var_type_id, offset: 0, @@ -1303,7 +1305,7 @@ mod tests { #[test] fn test_fixup_datasec() { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type_id = btf.add_type(BtfType::Int(Int::new( name_offset, 4, @@ -1311,14 +1313,14 @@ mod tests { 0, ))); - let name_offset = btf.add_string("foo".to_string()); + let name_offset = btf.add_string("foo"); let var_type_id = btf.add_type(BtfType::Var(Var::new( name_offset, int_type_id, VarLinkage::Global, ))); - let name_offset = btf.add_string(".data/foo".to_string()); + let name_offset = btf.add_string(".data/foo"); let variables = vec![DataSecEntry { btf_type: var_type_id, offset: 0, @@ -1333,8 +1335,8 @@ mod tests { }; btf.fixup_and_sanitize( - &HashMap::from([(".data/foo".to_string(), (SectionIndex(0), 32u64))]), - &HashMap::from([("foo".to_string(), 64u64)]), + &HashMap::from([(".data/foo".to_owned(), (SectionIndex(0), 32u64))]), + &HashMap::from([("foo".to_owned(), 64u64)]), &features, ) .unwrap(); @@ -1361,7 +1363,7 @@ mod tests { #[test] fn test_sanitize_func_and_proto() { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type_id = btf.add_type(BtfType::Int(Int::new( name_offset, 4, @@ -1371,17 +1373,17 @@ mod tests { let params = vec![ BtfParam { - name_offset: btf.add_string("a".to_string()), + name_offset: btf.add_string("a"), btf_type: int_type_id, }, BtfParam { - name_offset: btf.add_string("b".to_string()), + name_offset: btf.add_string("b"), btf_type: int_type_id, }, ]; let func_proto_type_id = btf.add_type(BtfType::FuncProto(FuncProto::new(params, int_type_id))); - let inc = btf.add_string("inc".to_string()); + let inc = btf.add_string("inc"); let func_type_id = btf.add_type(BtfType::Func(Func::new( inc, func_proto_type_id, @@ -1420,7 +1422,7 @@ mod tests { #[test] fn test_fixup_func_proto() { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0)); let int_type_id = btf.add_type(int_type); @@ -1459,7 +1461,7 @@ mod tests { #[test] fn test_sanitize_func_global() { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type_id = btf.add_type(BtfType::Int(Int::new( name_offset, 4, @@ -1469,17 +1471,17 @@ mod tests { let params = vec![ BtfParam { - name_offset: btf.add_string("a".to_string()), + name_offset: btf.add_string("a"), btf_type: int_type_id, }, BtfParam { - name_offset: btf.add_string("b".to_string()), + name_offset: btf.add_string("b"), btf_type: int_type_id, }, ]; let func_proto_type_id = btf.add_type(BtfType::FuncProto(FuncProto::new(params, int_type_id))); - let inc = btf.add_string("inc".to_string()); + let inc = btf.add_string("inc"); let func_type_id = btf.add_type(BtfType::Func(Func::new( inc, func_proto_type_id, @@ -1508,7 +1510,7 @@ mod tests { #[test] fn test_sanitize_mem_builtins() { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type_id = btf.add_type(BtfType::Int(Int::new( name_offset, 4, @@ -1518,11 +1520,11 @@ mod tests { let params = vec![ BtfParam { - name_offset: btf.add_string("a".to_string()), + name_offset: btf.add_string("a"), btf_type: int_type_id, }, BtfParam { - name_offset: btf.add_string("b".to_string()), + name_offset: btf.add_string("b"), btf_type: int_type_id, }, ]; @@ -1531,7 +1533,7 @@ mod tests { let builtins = ["memset", "memcpy", "memcmp", "memmove"]; for fname in builtins { - let func_name_offset = btf.add_string(fname.to_string()); + let func_name_offset = btf.add_string(fname); let func_type_id = btf.add_type(BtfType::Func(Func::new( func_name_offset, func_proto_type_id, @@ -1562,7 +1564,7 @@ mod tests { #[test] fn test_sanitize_float() { let mut btf = Btf::new(); - let name_offset = btf.add_string("float".to_string()); + let name_offset = btf.add_string("float"); let float_type_id = btf.add_type(BtfType::Float(Float::new(name_offset, 16))); let features = BtfFeatures { @@ -1586,7 +1588,7 @@ mod tests { #[test] fn test_sanitize_decl_tag() { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type_id = btf.add_type(BtfType::Int(Int::new( name_offset, 4, @@ -1594,14 +1596,14 @@ mod tests { 0, ))); - let name_offset = btf.add_string("foo".to_string()); + let name_offset = btf.add_string("foo"); let var_type_id = btf.add_type(BtfType::Var(Var::new( name_offset, int_type_id, VarLinkage::Static, ))); - let name_offset = btf.add_string("decl_tag".to_string()); + let name_offset = btf.add_string("decl_tag"); let decl_tag_type_id = btf.add_type(BtfType::DeclTag(DeclTag::new(name_offset, var_type_id, -1))); @@ -1629,7 +1631,7 @@ mod tests { let int_type_id = btf.add_type(BtfType::Int(Int::new(0, 4, IntEncoding::Signed, 0))); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let type_tag_type = btf.add_type(BtfType::TypeTag(TypeTag::new(name_offset, int_type_id))); btf.add_type(BtfType::Ptr(Ptr::new(0, type_tag_type))); diff --git a/aya-obj/src/btf/relocation.rs b/aya-obj/src/btf/relocation.rs index 06e1810d..c495a19e 100644 --- a/aya-obj/src/btf/relocation.rs +++ b/aya-obj/src/btf/relocation.rs @@ -1,7 +1,7 @@ use core::{mem, ops::Bound::Included, ptr}; use alloc::{ - borrow::ToOwned, + borrow::{Cow, ToOwned as _}, collections::BTreeMap, format, string::{String, ToString}, @@ -99,7 +99,7 @@ enum RelocationError { /// The max index max_index: usize, /// The error message - error: String, + error: &'static str, }, /// Relocation not valid for type @@ -114,7 +114,7 @@ enum RelocationError { /// The type kind type_kind: String, /// The error message - error: String, + error: &'static str, }, /// Invalid instruction referenced by relocation @@ -127,7 +127,7 @@ enum RelocationError { /// The instruction index index: usize, /// The error message - error: String, + error: Cow<'static, str>, }, #[error("applying relocation `{kind:?}` missing target BTF info for type `{type_id}` at instruction #{ins_index}")] @@ -142,8 +142,8 @@ enum RelocationError { BtfError(#[from] BtfError), } -fn err_type_name(name: &Option) -> String { - name.clone().unwrap_or_else(|| "[unknown name]".to_string()) +fn err_type_name(name: &Option) -> &str { + name.as_deref().unwrap_or("[unknown name]") } #[derive(Copy, Clone, Debug)] @@ -717,7 +717,7 @@ impl<'a> AccessSpec<'a> { spec: spec.to_string(), index, max_index: n_variants, - error: "tried to access nonexistant enum variant".to_string(), + error: "tried to access nonexistant enum variant", }); } let accessors = vec![Accessor { @@ -740,7 +740,7 @@ impl<'a> AccessSpec<'a> { relocation_number: relocation.number, relocation_kind: format!("{:?}", relocation.kind), type_kind: format!("{:?}", ty.kind()), - error: "enum relocation on non-enum type".to_string(), + error: "enum relocation on non-enum type", }) } }, @@ -770,7 +770,7 @@ impl<'a> AccessSpec<'a> { spec: spec.to_string(), index, max_index: members.len(), - error: "out of bounds struct or union access".to_string(), + error: "out of bounds struct or union access", }); } @@ -806,7 +806,7 @@ impl<'a> AccessSpec<'a> { spec: spec.to_string(), index, max_index: array.len as usize, - error: "array index out of bounds".to_string(), + error: "array index out of bounds", }); } accessors.push(Accessor { @@ -822,8 +822,7 @@ impl<'a> AccessSpec<'a> { relocation_number: relocation.number, relocation_kind: format!("{rel_kind:?}"), type_kind: format!("{:?}", ty.kind()), - error: "field relocation on a type that doesn't have fields" - .to_string(), + error: "field relocation on a type that doesn't have fields", }); } }; @@ -958,7 +957,7 @@ impl ComputedRelocation { return Err(RelocationError::InvalidInstruction { relocation_number: rel.number, index: ins_index, - error: format!("invalid src_reg={src_reg:x} expected {BPF_K:x}"), + error: format!("invalid src_reg={src_reg:x} expected {BPF_K:x}").into(), }); } @@ -969,7 +968,8 @@ impl ComputedRelocation { return Err(RelocationError::InvalidInstruction { relocation_number: rel.number, index: ins_index, - error: format!("value `{target_value}` overflows 16 bits offset field"), + error: format!("value `{target_value}` overflows 16 bits offset field") + .into(), }); } @@ -994,7 +994,8 @@ impl ComputedRelocation { self.local.size, err_type_name(&target_btf.err_type_name(target_ty)), target.size, - ), + ) + .into(), }) } } @@ -1008,7 +1009,7 @@ impl ComputedRelocation { return Err(RelocationError::InvalidInstruction { relocation_number: rel.number, index: ins_index, - error: format!("invalid target size {size}"), + error: format!("invalid target size {size}").into(), }) } } as u8; @@ -1031,7 +1032,7 @@ impl ComputedRelocation { return Err(RelocationError::InvalidInstruction { relocation_number: rel.number, index: ins_index, - error: format!("invalid instruction class {class:x}"), + error: format!("invalid instruction class {class:x}").into(), }) } }; @@ -1128,7 +1129,7 @@ impl ComputedRelocation { relocation_number: rel.number, relocation_kind: format!("{rel_kind:?}"), type_kind: format!("{:?}", ty.kind()), - error: "invalid relocation kind for array type".to_string(), + error: "invalid relocation kind for array type", }); } }; @@ -1143,7 +1144,7 @@ impl ComputedRelocation { relocation_number: rel.number, relocation_kind: format!("{:?}", rel.kind), type_kind: format!("{:?}", ty.kind()), - error: "field relocation on a type that doesn't have fields".to_string(), + error: "field relocation on a type that doesn't have fields", }); } }; diff --git a/aya-obj/src/btf/types.rs b/aya-obj/src/btf/types.rs index 5577044c..01e1168c 100644 --- a/aya-obj/src/btf/types.rs +++ b/aya-obj/src/btf/types.rs @@ -1724,11 +1724,11 @@ mod tests { #[test] fn test_types_are_compatible() { let mut btf = Btf::new(); - let name_offset = btf.add_string("u32".to_string()); + let name_offset = btf.add_string("u32"); let u32t = btf.add_type(BtfType::Int(Int::new(name_offset, 4, IntEncoding::None, 0))); - let name_offset = btf.add_string("u64".to_string()); + let name_offset = btf.add_string("u64"); let u64t = btf.add_type(BtfType::Int(Int::new(name_offset, 8, IntEncoding::None, 0))); - let name_offset = btf.add_string("widgets".to_string()); + let name_offset = btf.add_string("widgets"); let array_type = btf.add_type(BtfType::Array(Array::new(name_offset, u64t, u32t, 16))); assert!(types_are_compatible(&btf, u32t, &btf, u32t).unwrap()); diff --git a/aya-obj/src/obj.rs b/aya-obj/src/obj.rs index f066d01e..e9093e62 100644 --- a/aya-obj/src/obj.rs +++ b/aya-obj/src/obj.rs @@ -2378,7 +2378,7 @@ mod tests { fn test_patch_map_data() { let mut obj = fake_obj(); obj.maps.insert( - ".rodata".to_string(), + ".rodata".to_owned(), Map::Legacy(LegacyMap { def: bpf_map_def { map_type: BPF_MAP_TYPE_ARRAY as u32, @@ -2400,7 +2400,7 @@ mod tests { Symbol { index: 1, section_index: Some(1), - name: Some("my_config".to_string()), + name: Some("my_config".to_owned()), address: 0, size: 3, is_definition: true, diff --git a/aya-tool/src/generate.rs b/aya-tool/src/generate.rs index d8d724d7..c8b97f6a 100644 --- a/aya-tool/src/generate.rs +++ b/aya-tool/src/generate.rs @@ -157,7 +157,7 @@ mod test { fn test_extract_ctypes_prefix() { let (flags, prefix) = extract_ctypes_prefix(&to_vec("foo --ctypes-prefix bar baz")); assert_eq!(flags, to_vec("foo baz")); - assert_eq!(prefix, Some("bar".to_string())); + assert_eq!(prefix.as_deref(), Some("bar")); let (flags, prefix) = extract_ctypes_prefix(&to_vec("foo --ctypes-prefi bar baz")); assert_eq!(flags, to_vec("foo --ctypes-prefi bar baz")); @@ -170,7 +170,7 @@ mod test { let (flags, prefix) = extract_ctypes_prefix(&to_vec("--ctypes-prefix foo")); let empty: Vec = Vec::new(); assert_eq!(flags, empty); - assert_eq!(prefix, Some("foo".to_string())); + assert_eq!(prefix.as_deref(), Some("foo")); let (flags, prefix) = extract_ctypes_prefix(&to_vec("--ctypes-prefix")); assert_eq!(flags, to_vec("--ctypes-prefix")); @@ -181,32 +181,32 @@ mod test { fn test_combine_flags() { assert_eq!( combine_flags(&to_vec("a b"), &to_vec("c d"),).join(" "), - "a b c d".to_string(), + "a b c d", ); assert_eq!( combine_flags(&to_vec("a -- b"), &to_vec("a b"),).join(" "), - "a a b -- b".to_string(), + "a a b -- b", ); assert_eq!( combine_flags(&to_vec("a -- b"), &to_vec("c d"),).join(" "), - "a c d -- b".to_string(), + "a c d -- b", ); assert_eq!( combine_flags(&to_vec("a b"), &to_vec("c -- d"),).join(" "), - "a b c -- d".to_string(), + "a b c -- d", ); assert_eq!( combine_flags(&to_vec("a -- b"), &to_vec("c -- d"),).join(" "), - "a c -- b d".to_string(), + "a c -- b d", ); assert_eq!( combine_flags(&to_vec("a -- b"), &to_vec("-- c d"),).join(" "), - "a -- b c d".to_string(), + "a -- b c d", ); } } diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index 3c2aa495..74eb5317 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -425,13 +425,13 @@ impl<'a> BpfLoader<'a> { if !map.obj.data().is_empty() && map.obj.section_kind() != BpfSectionKind::Bss { bpf_map_update_elem_ptr(fd, &0 as *const _, map.obj.data_mut().as_mut_ptr(), 0) .map_err(|(_, io_error)| MapError::SyscallError { - call: "bpf_map_update_elem".to_owned(), + call: "bpf_map_update_elem", io_error, })?; } if map.obj.section_kind() == BpfSectionKind::Rodata { bpf_map_freeze(fd).map_err(|(_, io_error)| MapError::SyscallError { - call: "bpf_map_freeze".to_owned(), + call: "bpf_map_freeze", io_error, })?; } diff --git a/aya/src/maps/array/array.rs b/aya/src/maps/array/array.rs index 71ac1223..e219ced6 100644 --- a/aya/src/maps/array/array.rs +++ b/aya/src/maps/array/array.rs @@ -67,7 +67,7 @@ impl, V: Pod> Array { let value = bpf_map_lookup_elem(fd, index, flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_lookup_elem".to_owned(), + call: "bpf_map_lookup_elem", io_error, } })?; @@ -94,7 +94,7 @@ impl, V: Pod> Array { let fd = data.fd_or_err()?; bpf_map_update_elem(fd, Some(&index), value.borrow(), flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_update_elem".to_owned(), + call: "bpf_map_update_elem", io_error, } })?; diff --git a/aya/src/maps/array/per_cpu_array.rs b/aya/src/maps/array/per_cpu_array.rs index e83f59e5..a7cde3ea 100644 --- a/aya/src/maps/array/per_cpu_array.rs +++ b/aya/src/maps/array/per_cpu_array.rs @@ -86,7 +86,7 @@ impl, V: Pod> PerCpuArray { let value = bpf_map_lookup_elem_per_cpu(fd, index, flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_lookup_elem".to_owned(), + call: "bpf_map_lookup_elem", io_error, } })?; @@ -114,7 +114,7 @@ impl, V: Pod> PerCpuArray { bpf_map_update_elem_per_cpu(fd, &index, &values, flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_update_elem".to_owned(), + call: "bpf_map_update_elem", io_error, } })?; diff --git a/aya/src/maps/array/program_array.rs b/aya/src/maps/array/program_array.rs index 8418b1cd..613d7b17 100644 --- a/aya/src/maps/array/program_array.rs +++ b/aya/src/maps/array/program_array.rs @@ -81,7 +81,7 @@ impl> ProgramArray { bpf_map_update_elem(fd, Some(&index), &prog_fd, flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_update_elem".to_owned(), + call: "bpf_map_update_elem", io_error, } })?; @@ -100,7 +100,7 @@ impl> ProgramArray { bpf_map_delete_elem(fd, index) .map(|_| ()) .map_err(|(_, io_error)| MapError::SyscallError { - call: "bpf_map_delete_elem".to_owned(), + call: "bpf_map_delete_elem", io_error, }) } diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index f7f4be02..bdf81eae 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -54,7 +54,7 @@ impl, V: Pod> BloomFilter { bpf_map_lookup_elem_ptr::(fd, None, &mut value, flags) .map_err(|(_, io_error)| MapError::SyscallError { - call: "bpf_map_lookup_elem".to_owned(), + call: "bpf_map_lookup_elem", io_error, })? .ok_or(MapError::ElementNotFound)?; @@ -66,7 +66,7 @@ impl, V: Pod> BloomFilter { let fd = self.inner.borrow().fd_or_err()?; bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_push_elem".to_owned(), + call: "bpf_map_push_elem", io_error, } })?; diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index 26a4af0f..493a944b 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -56,7 +56,7 @@ impl, K: Pod, V: Pod> HashMap { let fd = self.inner.borrow().fd_or_err()?; let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_lookup_elem".to_owned(), + call: "bpf_map_lookup_elem", io_error, } })?; diff --git a/aya/src/maps/hash_map/mod.rs b/aya/src/maps/hash_map/mod.rs index f1e1837f..4ae4d712 100644 --- a/aya/src/maps/hash_map/mod.rs +++ b/aya/src/maps/hash_map/mod.rs @@ -23,7 +23,7 @@ pub(crate) fn insert( let fd = map.fd_or_err()?; bpf_map_update_elem(fd, Some(key), value, flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_update_elem".to_owned(), + call: "bpf_map_update_elem", io_error, } })?; @@ -36,7 +36,7 @@ pub(crate) fn remove(map: &mut MapData, key: &K) -> Result<(), MapError> bpf_map_delete_elem(fd, key) .map(|_| ()) .map_err(|(_, io_error)| MapError::SyscallError { - call: "bpf_map_delete_elem".to_owned(), + call: "bpf_map_delete_elem", io_error, }) } 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 aaa48838..91657f13 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -66,7 +66,7 @@ impl, K: Pod, V: Pod> PerCpuHashMap { let fd = self.inner.borrow().fd_or_err()?; let values = bpf_map_lookup_elem_per_cpu(fd, key, flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_lookup_elem".to_owned(), + call: "bpf_map_lookup_elem", io_error, } })?; @@ -124,7 +124,7 @@ impl, K: Pod, V: Pod> PerCpuHashMap { let fd = self.inner.borrow_mut().fd_or_err()?; bpf_map_update_elem_per_cpu(fd, key.borrow(), &values, flags).map_err( |(_, io_error)| MapError::SyscallError { - call: "bpf_map_update_elem".to_owned(), + call: "bpf_map_update_elem", io_error, }, )?; diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index 34b86ef0..0d8b9d09 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -130,7 +130,7 @@ impl, K: Pod, V: Pod> LpmTrie { let fd = self.inner.borrow().fd_or_err()?; let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_lookup_elem".to_owned(), + call: "bpf_map_lookup_elem", io_error, } })?; @@ -161,7 +161,7 @@ impl, K: Pod, V: Pod> LpmTrie { let fd = self.inner.borrow().fd_or_err()?; bpf_map_update_elem(fd, Some(key), value.borrow(), flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_update_elem".to_owned(), + call: "bpf_map_update_elem", io_error, } })?; @@ -177,7 +177,7 @@ impl, K: Pod, V: Pod> LpmTrie { bpf_map_delete_elem(fd, key) .map(|_| ()) .map_err(|(_, io_error)| MapError::SyscallError { - call: "bpf_map_delete_elem".to_owned(), + call: "bpf_map_delete_elem", io_error, }) } diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 3f20c503..f2b7a6a5 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -168,7 +168,7 @@ pub enum MapError { #[error("the `{call}` syscall failed")] SyscallError { /// Syscall Name - call: String, + call: &'static str, /// Original io::Error io_error: io::Error, }, @@ -518,7 +518,7 @@ impl MapData { let map_path = path.as_ref().join(name); let path_string = CString::new(map_path.to_str().unwrap()).unwrap(); let fd = bpf_get_object(&path_string).map_err(|(_, io_error)| MapError::SyscallError { - call: "BPF_OBJ_GET".to_string(), + call: "BPF_OBJ_GET", io_error, })? as RawFd; @@ -540,12 +540,12 @@ impl MapData { })?; let fd = bpf_get_object(&path_string).map_err(|(_, io_error)| MapError::SyscallError { - call: "BPF_OBJ_GET".to_owned(), + call: "BPF_OBJ_GET", io_error, })? as RawFd; let info = bpf_map_get_info_by_fd(fd).map_err(|io_error| MapError::SyscallError { - call: "BPF_MAP_GET_INFO_BY_FD".to_owned(), + call: "BPF_MAP_GET_INFO_BY_FD", io_error, })?; @@ -564,7 +564,7 @@ impl MapData { /// For example, you received an FD over Unix Domain Socket. pub fn from_fd(fd: RawFd) -> Result { let info = bpf_map_get_info_by_fd(fd).map_err(|io_error| MapError::SyscallError { - call: "BPF_OBJ_GET".to_owned(), + call: "BPF_OBJ_GET", io_error, })?; @@ -594,7 +594,7 @@ impl MapData { } })?; bpf_pin_object(fd, &path_string).map_err(|(_, io_error)| PinError::SyscallError { - name: "BPF_OBJ_PIN".to_string(), + name: "BPF_OBJ_PIN", io_error, })?; self.pinned = true; @@ -683,7 +683,7 @@ impl Iterator for MapKeys<'_, K> { Err((_, io_error)) => { self.err = true; Some(Err(MapError::SyscallError { - call: "bpf_map_get_next_key".to_owned(), + call: "bpf_map_get_next_key", io_error, })) } diff --git a/aya/src/maps/queue.rs b/aya/src/maps/queue.rs index c6810546..fc32c8b7 100644 --- a/aya/src/maps/queue.rs +++ b/aya/src/maps/queue.rs @@ -66,7 +66,7 @@ impl, V: Pod> Queue { let value = bpf_map_lookup_and_delete_elem::(fd, None, flags).map_err( |(_, io_error)| MapError::SyscallError { - call: "bpf_map_lookup_and_delete_elem".to_owned(), + call: "bpf_map_lookup_and_delete_elem", io_error, }, )?; @@ -82,7 +82,7 @@ impl, V: Pod> Queue { let fd = self.inner.borrow().fd_or_err()?; bpf_map_push_elem(fd, value.borrow(), flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_push_elem".to_owned(), + call: "bpf_map_push_elem", io_error, } })?; diff --git a/aya/src/maps/sock/sock_hash.rs b/aya/src/maps/sock/sock_hash.rs index 244dcd1c..fba613b0 100644 --- a/aya/src/maps/sock/sock_hash.rs +++ b/aya/src/maps/sock/sock_hash.rs @@ -85,7 +85,7 @@ impl, K: Pod> SockHash { let fd = self.inner.borrow().fd_or_err()?; let value = bpf_map_lookup_elem(fd, key, flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_lookup_elem".to_owned(), + call: "bpf_map_lookup_elem", io_error, } })?; diff --git a/aya/src/maps/sock/sock_map.rs b/aya/src/maps/sock/sock_map.rs index 732917de..d34f658b 100644 --- a/aya/src/maps/sock/sock_map.rs +++ b/aya/src/maps/sock/sock_map.rs @@ -77,7 +77,7 @@ impl> SockMap { check_bounds(data, index)?; bpf_map_update_elem(fd, Some(&index), &socket.as_raw_fd(), flags).map_err( |(_, io_error)| MapError::SyscallError { - call: "bpf_map_update_elem".to_owned(), + call: "bpf_map_update_elem", io_error, }, )?; @@ -92,7 +92,7 @@ impl> SockMap { bpf_map_delete_elem(fd, index) .map(|_| ()) .map_err(|(_, io_error)| MapError::SyscallError { - call: "bpf_map_delete_elem".to_owned(), + call: "bpf_map_delete_elem", io_error, }) } diff --git a/aya/src/maps/stack.rs b/aya/src/maps/stack.rs index db428ddf..0b75ae2c 100644 --- a/aya/src/maps/stack.rs +++ b/aya/src/maps/stack.rs @@ -66,7 +66,7 @@ impl, V: Pod> Stack { let value = bpf_map_lookup_and_delete_elem::(fd, None, flags).map_err( |(_, io_error)| MapError::SyscallError { - call: "bpf_map_lookup_and_delete_elem".to_owned(), + call: "bpf_map_lookup_and_delete_elem", io_error, }, )?; @@ -82,7 +82,7 @@ impl, V: Pod> Stack { let fd = self.inner.borrow().fd_or_err()?; bpf_map_update_elem(fd, None::<&u32>, value.borrow(), flags).map_err(|(_, io_error)| { MapError::SyscallError { - call: "bpf_map_update_elem".to_owned(), + call: "bpf_map_update_elem", io_error, } })?; diff --git a/aya/src/maps/stack_trace.rs b/aya/src/maps/stack_trace.rs index a8cce414..eff8df47 100644 --- a/aya/src/maps/stack_trace.rs +++ b/aya/src/maps/stack_trace.rs @@ -52,8 +52,8 @@ use crate::{ /// frame.ip, /// frame /// .symbol_name -/// .as_ref() -/// .unwrap_or(&"[unknown symbol name]".to_owned()) +/// .as_deref() +/// .unwrap_or("[unknown symbol name]") /// ); /// } /// @@ -79,7 +79,7 @@ impl> StackTraceMap { let max_stack_depth = sysctl::("kernel/perf_event_max_stack").map_err(|io_error| { MapError::SyscallError { - call: "sysctl".to_owned(), + call: "sysctl", io_error, } })?; @@ -107,7 +107,7 @@ impl> StackTraceMap { let mut frames = vec![0; self.max_stack_depth]; bpf_map_lookup_elem_ptr(fd, Some(stack_id), frames.as_mut_ptr(), flags) .map_err(|(_, io_error)| MapError::SyscallError { - call: "bpf_map_lookup_elem".to_owned(), + call: "bpf_map_lookup_elem", io_error, })? .ok_or(MapError::KeyNotFound)?; diff --git a/aya/src/pin.rs b/aya/src/pin.rs index 6bb9d5a9..f47d8750 100644 --- a/aya/src/pin.rs +++ b/aya/src/pin.rs @@ -27,7 +27,7 @@ pub enum PinError { #[error("{name} failed")] SyscallError { /// The syscall name. - name: String, + name: &'static str, /// The [`io::Error`] returned by the syscall. #[source] io_error: io::Error, diff --git a/aya/src/programs/cgroup_device.rs b/aya/src/programs/cgroup_device.rs index 9e1d94a9..ad211b83 100644 --- a/aya/src/programs/cgroup_device.rs +++ b/aya/src/programs/cgroup_device.rs @@ -66,7 +66,7 @@ impl CgroupDevice { if k_ver >= (5, 7, 0) { let link_fd = bpf_link_create(prog_fd, cgroup_fd, BPF_CGROUP_DEVICE, None, 0).map_err( |(_, io_error)| ProgramError::SyscallError { - call: "bpf_link_create".to_owned(), + call: "bpf_link_create", io_error, }, )? as RawFd; @@ -78,7 +78,7 @@ impl CgroupDevice { } else { bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_DEVICE).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "bpf_prog_attach".to_owned(), + call: "bpf_prog_attach", io_error, } })?; diff --git a/aya/src/programs/cgroup_skb.rs b/aya/src/programs/cgroup_skb.rs index 558afadf..ed92fd2c 100644 --- a/aya/src/programs/cgroup_skb.rs +++ b/aya/src/programs/cgroup_skb.rs @@ -100,7 +100,7 @@ impl CgroupSkb { if k_ver >= (5, 7, 0) { let link_fd = bpf_link_create(prog_fd, cgroup_fd, attach_type, None, 0).map_err( |(_, io_error)| ProgramError::SyscallError { - call: "bpf_link_create".to_owned(), + call: "bpf_link_create", io_error, }, )? as RawFd; @@ -112,7 +112,7 @@ impl CgroupSkb { } else { bpf_prog_attach(prog_fd, cgroup_fd, attach_type).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "bpf_prog_attach".to_owned(), + call: "bpf_prog_attach", io_error, } })?; diff --git a/aya/src/programs/cgroup_sock.rs b/aya/src/programs/cgroup_sock.rs index f4a0b59b..a1c32179 100644 --- a/aya/src/programs/cgroup_sock.rs +++ b/aya/src/programs/cgroup_sock.rs @@ -75,7 +75,7 @@ impl CgroupSock { if k_ver >= (5, 7, 0) { let link_fd = bpf_link_create(prog_fd, cgroup_fd, attach_type, None, 0).map_err( |(_, io_error)| ProgramError::SyscallError { - call: "bpf_link_create".to_owned(), + call: "bpf_link_create", io_error, }, )? as RawFd; @@ -87,7 +87,7 @@ impl CgroupSock { } else { bpf_prog_attach(prog_fd, cgroup_fd, attach_type).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "bpf_prog_attach".to_owned(), + call: "bpf_prog_attach", io_error, } })?; diff --git a/aya/src/programs/cgroup_sock_addr.rs b/aya/src/programs/cgroup_sock_addr.rs index cc2b3106..de9a48a2 100644 --- a/aya/src/programs/cgroup_sock_addr.rs +++ b/aya/src/programs/cgroup_sock_addr.rs @@ -76,7 +76,7 @@ impl CgroupSockAddr { if k_ver >= (5, 7, 0) { let link_fd = bpf_link_create(prog_fd, cgroup_fd, attach_type, None, 0).map_err( |(_, io_error)| ProgramError::SyscallError { - call: "bpf_link_create".to_owned(), + call: "bpf_link_create", io_error, }, )? as RawFd; @@ -88,7 +88,7 @@ impl CgroupSockAddr { } else { bpf_prog_attach(prog_fd, cgroup_fd, attach_type).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "bpf_prog_attach".to_owned(), + call: "bpf_prog_attach", io_error, } })?; diff --git a/aya/src/programs/cgroup_sockopt.rs b/aya/src/programs/cgroup_sockopt.rs index eff006e9..021ba38a 100644 --- a/aya/src/programs/cgroup_sockopt.rs +++ b/aya/src/programs/cgroup_sockopt.rs @@ -73,7 +73,7 @@ impl CgroupSockopt { if k_ver >= (5, 7, 0) { let link_fd = bpf_link_create(prog_fd, cgroup_fd, attach_type, None, 0).map_err( |(_, io_error)| ProgramError::SyscallError { - call: "bpf_link_create".to_owned(), + call: "bpf_link_create", io_error, }, )? as RawFd; @@ -85,7 +85,7 @@ impl CgroupSockopt { } else { bpf_prog_attach(prog_fd, cgroup_fd, attach_type).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "bpf_prog_attach".to_owned(), + call: "bpf_prog_attach", io_error, } })?; diff --git a/aya/src/programs/cgroup_sysctl.rs b/aya/src/programs/cgroup_sysctl.rs index 81fd7a6b..f2bfd56b 100644 --- a/aya/src/programs/cgroup_sysctl.rs +++ b/aya/src/programs/cgroup_sysctl.rs @@ -68,7 +68,7 @@ impl CgroupSysctl { if k_ver >= (5, 7, 0) { let link_fd = bpf_link_create(prog_fd, cgroup_fd, BPF_CGROUP_SYSCTL, None, 0).map_err( |(_, io_error)| ProgramError::SyscallError { - call: "bpf_link_create".to_owned(), + call: "bpf_link_create", io_error, }, )? as RawFd; @@ -80,7 +80,7 @@ impl CgroupSysctl { } else { bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SYSCTL).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "bpf_prog_attach".to_owned(), + call: "bpf_prog_attach", io_error, } })?; diff --git a/aya/src/programs/extension.rs b/aya/src/programs/extension.rs index aaff4b61..1826a101 100644 --- a/aya/src/programs/extension.rs +++ b/aya/src/programs/extension.rs @@ -92,7 +92,7 @@ impl Extension { // the attach type must be set as 0, which is bpf_attach_type::BPF_CGROUP_INET_INGRESS let link_fd = bpf_link_create(prog_fd, target_fd, BPF_CGROUP_INET_INGRESS, Some(btf_id), 0) .map_err(|(_, io_error)| ProgramError::SyscallError { - call: "bpf_link_create".to_owned(), + call: "bpf_link_create", io_error, })? as RawFd; self.data @@ -122,7 +122,7 @@ impl Extension { // the attach type must be set as 0, which is bpf_attach_type::BPF_CGROUP_INET_INGRESS let link_fd = bpf_link_create(prog_fd, target_fd, BPF_CGROUP_INET_INGRESS, Some(btf_id), 0) .map_err(|(_, io_error)| ProgramError::SyscallError { - call: "bpf_link_create".to_owned(), + call: "bpf_link_create", io_error, })? as RawFd; self.data @@ -153,7 +153,7 @@ fn get_btf_info(prog_fd: i32, func_name: &str) -> Result<(RawFd, u32), ProgramEr // retrieve program information let info = sys::bpf_prog_get_info_by_fd(prog_fd).map_err(|io_error| ProgramError::SyscallError { - call: "bpf_prog_get_info_by_fd".to_owned(), + call: "bpf_prog_get_info_by_fd", io_error, })?; @@ -165,7 +165,7 @@ fn get_btf_info(prog_fd: i32, func_name: &str) -> Result<(RawFd, u32), ProgramEr // the bpf fd of the BTF object let btf_fd = sys::bpf_btf_get_fd_by_id(info.btf_id).map_err(|io_error| ProgramError::SyscallError { - call: "bpf_btf_get_fd_by_id".to_owned(), + call: "bpf_btf_get_fd_by_id", io_error, })?; @@ -179,7 +179,7 @@ fn get_btf_info(prog_fd: i32, func_name: &str) -> Result<(RawFd, u32), ProgramEr let btf_info = sys::btf_obj_get_info_by_fd(btf_fd, &mut buf).map_err(|io_error| { ProgramError::SyscallError { - call: "bpf_prog_get_info_by_fd".to_owned(), + call: "bpf_prog_get_info_by_fd", io_error, } })?; @@ -189,7 +189,7 @@ fn get_btf_info(prog_fd: i32, func_name: &str) -> Result<(RawFd, u32), ProgramEr } } Err(io_error) => Err(ProgramError::SyscallError { - call: "bpf_prog_get_info_by_fd".to_owned(), + call: "bpf_prog_get_info_by_fd", io_error, }), }?; diff --git a/aya/src/programs/links.rs b/aya/src/programs/links.rs index 86017b15..7e66af46 100644 --- a/aya/src/programs/links.rs +++ b/aya/src/programs/links.rs @@ -129,7 +129,7 @@ impl FdLink { } })?; bpf_pin_object(self.fd, &path_string).map_err(|(_, io_error)| PinError::SyscallError { - name: "BPF_OBJ_PIN".to_string(), + name: "BPF_OBJ_PIN", io_error, })?; Ok(PinnedLink::new(PathBuf::from(path.as_ref()), self)) @@ -186,7 +186,7 @@ impl PinnedLink { let path_string = CString::new(path.as_ref().to_string_lossy().to_string()).unwrap(); let fd = bpf_get_object(&path_string).map_err(|(code, io_error)| LinkError::SyscallError { - call: "BPF_OBJ_GET".to_string(), + call: "BPF_OBJ_GET", code, io_error, })? as RawFd; @@ -318,7 +318,7 @@ pub enum LinkError { #[error("the `{call}` syscall failed with code {code}")] SyscallError { /// Syscall Name. - call: String, + call: &'static str, /// Error code. code: libc::c_long, #[source] diff --git a/aya/src/programs/lirc_mode2.rs b/aya/src/programs/lirc_mode2.rs index 98f348b3..1d445eb7 100644 --- a/aya/src/programs/lirc_mode2.rs +++ b/aya/src/programs/lirc_mode2.rs @@ -66,7 +66,7 @@ impl LircMode2 { bpf_prog_attach(prog_fd, lircdev_fd, BPF_LIRC_MODE2).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "bpf_prog_attach".to_owned(), + call: "bpf_prog_attach", io_error, } })?; @@ -97,7 +97,7 @@ impl LircMode2 { for id in prog_ids { let fd = bpf_prog_get_fd_by_id(id).map_err(|io_error| ProgramError::SyscallError { - call: "bpf_prog_get_fd_by_id".to_owned(), + call: "bpf_prog_get_fd_by_id", io_error, })?; @@ -135,7 +135,7 @@ impl LircLink { match bpf_prog_get_info_by_fd(self.prog_fd) { Ok(info) => Ok(ProgramInfo(info)), Err(io_error) => Err(ProgramError::SyscallError { - call: "bpf_prog_get_info_by_fd".to_owned(), + call: "bpf_prog_get_info_by_fd", io_error, }), } diff --git a/aya/src/programs/mod.rs b/aya/src/programs/mod.rs index 561b3a1a..b47687b2 100644 --- a/aya/src/programs/mod.rs +++ b/aya/src/programs/mod.rs @@ -148,7 +148,7 @@ pub enum ProgramError { #[error("`{call}` failed")] SyscallError { /// The name of the syscall which failed. - call: String, + call: &'static str, /// The [`io::Error`] returned by the syscall. #[source] io_error: io::Error, @@ -455,7 +455,7 @@ impl ProgramData { let attach_btf_obj_fd = if info.attach_btf_obj_id > 0 { let fd = bpf_btf_get_fd_by_id(info.attach_btf_obj_id).map_err(|io_error| { ProgramError::SyscallError { - call: "bpf_btf_get_fd_by_id".to_string(), + call: "bpf_btf_get_fd_by_id", io_error, } })?; @@ -487,12 +487,12 @@ impl ProgramData { CString::new(path.as_ref().as_os_str().to_string_lossy().as_bytes()).unwrap(); let fd = bpf_get_object(&path_string).map_err(|(_, io_error)| ProgramError::SyscallError { - call: "bpf_obj_get".to_owned(), + call: "bpf_obj_get", io_error, })? as RawFd; let info = bpf_prog_get_info_by_fd(fd).map_err(|io_error| ProgramError::SyscallError { - call: "bpf_prog_get_info_by_fd".to_owned(), + call: "bpf_prog_get_info_by_fd", io_error, })?; @@ -528,8 +528,8 @@ fn pin_program>( let fd = data.fd.ok_or(PinError::NoFd { name: data .name - .as_ref() - .unwrap_or(&"".to_string()) + .as_deref() + .unwrap_or("") .to_string(), })?; let path_string = CString::new(path.as_ref().to_string_lossy().into_owned()).map_err(|e| { @@ -538,7 +538,7 @@ fn pin_program>( } })?; bpf_pin_object(fd, &path_string).map_err(|(_, io_error)| PinError::SyscallError { - name: "BPF_OBJ_PIN".to_string(), + name: "BPF_OBJ_PIN", io_error, })?; Ok(()) @@ -666,7 +666,7 @@ pub(crate) fn query( } Err((_, io_error)) => { return Err(ProgramError::SyscallError { - call: "bpf_prog_query".to_owned(), + call: "bpf_prog_query", io_error, }); } @@ -948,7 +948,7 @@ impl ProgramInfo { pub fn fd(&self) -> Result { let fd = bpf_prog_get_fd_by_id(self.0.id).map_err(|io_error| ProgramError::SyscallError { - call: "bpf_prog_get_fd_by_id".to_owned(), + call: "bpf_prog_get_fd_by_id", io_error, })?; Ok(fd as RawFd) @@ -959,12 +959,12 @@ impl ProgramInfo { let path_string = CString::new(path.as_ref().to_str().unwrap()).unwrap(); let fd = bpf_get_object(&path_string).map_err(|(_, io_error)| ProgramError::SyscallError { - call: "BPF_OBJ_GET".to_owned(), + call: "BPF_OBJ_GET", io_error, })? as RawFd; let info = bpf_prog_get_info_by_fd(fd).map_err(|io_error| ProgramError::SyscallError { - call: "bpf_prog_get_info_by_fd".to_owned(), + call: "bpf_prog_get_info_by_fd", io_error, })?; unsafe { @@ -995,13 +995,13 @@ impl Iterator for ProgramsIter { Some( bpf_prog_get_fd_by_id(next) .map_err(|io_error| ProgramError::SyscallError { - call: "bpf_prog_get_fd_by_id".to_owned(), + call: "bpf_prog_get_fd_by_id", io_error, }) .and_then(|fd| { let info = bpf_prog_get_info_by_fd(fd) .map_err(|io_error| ProgramError::SyscallError { - call: "bpf_prog_get_info_by_fd".to_owned(), + call: "bpf_prog_get_info_by_fd", io_error, }) .map(ProgramInfo); @@ -1016,7 +1016,7 @@ impl Iterator for ProgramsIter { // iteration to avoid an infinite loop. self.error = true; Some(Err(ProgramError::SyscallError { - call: "bpf_prog_get_fd_by_id".to_owned(), + call: "bpf_prog_get_fd_by_id", io_error, })) } diff --git a/aya/src/programs/perf_attach.rs b/aya/src/programs/perf_attach.rs index fe4fafe3..7bba6238 100644 --- a/aya/src/programs/perf_attach.rs +++ b/aya/src/programs/perf_attach.rs @@ -77,7 +77,7 @@ pub(crate) fn perf_attach(prog_fd: RawFd, fd: RawFd) -> Result Result { perf_event_ioctl(fd, PERF_EVENT_IOC_SET_BPF, prog_fd).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "PERF_EVENT_IOC_SET_BPF".to_owned(), + call: "PERF_EVENT_IOC_SET_BPF", io_error, } })?; perf_event_ioctl(fd, PERF_EVENT_IOC_ENABLE, 0).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "PERF_EVENT_IOC_ENABLE".to_owned(), + call: "PERF_EVENT_IOC_ENABLE", io_error, } })?; diff --git a/aya/src/programs/perf_event.rs b/aya/src/programs/perf_event.rs index 177b3544..3c245573 100644 --- a/aya/src/programs/perf_event.rs +++ b/aya/src/programs/perf_event.rs @@ -165,7 +165,7 @@ impl PerfEvent { 0, ) .map_err(|(_code, io_error)| ProgramError::SyscallError { - call: "perf_event_open".to_owned(), + call: "perf_event_open", io_error, })? as i32; diff --git a/aya/src/programs/probe.rs b/aya/src/programs/probe.rs index 4ecf80a5..f9d17bc0 100644 --- a/aya/src/programs/probe.rs +++ b/aya/src/programs/probe.rs @@ -110,7 +110,7 @@ fn create_as_probe( let fd = perf_event_open_probe(perf_ty, ret_bit, fn_name, offset, pid).map_err( |(_code, io_error)| ProgramError::SyscallError { - call: "perf_event_open".to_owned(), + call: "perf_event_open", io_error, }, )? as i32; @@ -139,7 +139,7 @@ fn create_as_trace_point( let tpid = read_sys_fs_trace_point_id(tracefs, &category, &event_alias)?; let fd = perf_event_open_trace_point(tpid, pid).map_err(|(_code, io_error)| { ProgramError::SyscallError { - call: "perf_event_open".to_owned(), + call: "perf_event_open", io_error, } })? as i32; @@ -174,7 +174,7 @@ fn create_probe_event( let offset_suffix = match kind { KProbe => format!("+{offset}"), UProbe | URetProbe => format!(":{offset:#x}"), - _ => "".to_string(), + _ => String::new(), }; let probe = format!( "{}:{}s/{} {}{}\n", diff --git a/aya/src/programs/sk_lookup.rs b/aya/src/programs/sk_lookup.rs index 8230a81e..27d60b8f 100644 --- a/aya/src/programs/sk_lookup.rs +++ b/aya/src/programs/sk_lookup.rs @@ -66,7 +66,7 @@ impl SkLookup { let link_fd = bpf_link_create(prog_fd, netns_fd, BPF_SK_LOOKUP, None, 0).map_err( |(_, io_error)| ProgramError::SyscallError { - call: "bpf_link_create".to_owned(), + call: "bpf_link_create", io_error, }, )? as RawFd; diff --git a/aya/src/programs/sk_msg.rs b/aya/src/programs/sk_msg.rs index a60346d8..079816cd 100644 --- a/aya/src/programs/sk_msg.rs +++ b/aya/src/programs/sk_msg.rs @@ -84,7 +84,7 @@ impl SkMsg { bpf_prog_attach(prog_fd, map_fd, BPF_SK_MSG_VERDICT).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "bpf_prog_attach".to_owned(), + call: "bpf_prog_attach", io_error, } })?; diff --git a/aya/src/programs/sk_skb.rs b/aya/src/programs/sk_skb.rs index 34aade6d..df7729e8 100644 --- a/aya/src/programs/sk_skb.rs +++ b/aya/src/programs/sk_skb.rs @@ -80,7 +80,7 @@ impl SkSkb { }; bpf_prog_attach(prog_fd, map_fd, attach_type).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "bpf_prog_attach".to_owned(), + call: "bpf_prog_attach", io_error, } })?; diff --git a/aya/src/programs/sock_ops.rs b/aya/src/programs/sock_ops.rs index 9021b312..e57900a3 100644 --- a/aya/src/programs/sock_ops.rs +++ b/aya/src/programs/sock_ops.rs @@ -64,7 +64,7 @@ impl SockOps { bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "bpf_prog_attach".to_owned(), + call: "bpf_prog_attach", io_error, } })?; diff --git a/aya/src/programs/tc.rs b/aya/src/programs/tc.rs index ad86b65c..da20c635 100644 --- a/aya/src/programs/tc.rs +++ b/aya/src/programs/tc.rs @@ -265,13 +265,13 @@ impl SchedClassifierLink { /// # #[error(transparent)] /// # IO(#[from] std::io::Error), /// # } - /// # fn read_persisted_link_details() -> (String, TcAttachType, u16, u32) { - /// # ("eth0".to_string(), TcAttachType::Ingress, 50, 1) + /// # fn read_persisted_link_details() -> (&'static str, TcAttachType, u16, u32) { + /// # ("eth0", TcAttachType::Ingress, 50, 1) /// # } /// // Get the link parameters from some external source. Where and how the parameters are /// // persisted is up to your application. /// let (if_name, attach_type, priority, handle) = read_persisted_link_details(); - /// let new_tc_link = SchedClassifierLink::attached(&if_name, attach_type, priority, handle)?; + /// let new_tc_link = SchedClassifierLink::attached(if_name, attach_type, priority, handle)?; /// /// # Ok::<(), Error>(()) /// ``` diff --git a/aya/src/programs/trace_point.rs b/aya/src/programs/trace_point.rs index f43cd77e..d67c6a41 100644 --- a/aya/src/programs/trace_point.rs +++ b/aya/src/programs/trace_point.rs @@ -82,7 +82,7 @@ impl TracePoint { let id = read_sys_fs_trace_point_id(tracefs, category, name)?; let fd = perf_event_open_trace_point(id, None).map_err(|(_code, io_error)| { ProgramError::SyscallError { - call: "perf_event_open".to_owned(), + call: "perf_event_open", io_error, } })? as i32; diff --git a/aya/src/programs/utils.rs b/aya/src/programs/utils.rs index 4a12d0a5..33b84095 100644 --- a/aya/src/programs/utils.rs +++ b/aya/src/programs/utils.rs @@ -15,7 +15,7 @@ pub(crate) fn attach_raw_tracepoint>( let pfd = bpf_raw_tracepoint_open(tp_name, prog_fd).map_err(|(_code, io_error)| { ProgramError::SyscallError { - call: "bpf_raw_tracepoint_open".to_owned(), + call: "bpf_raw_tracepoint_open", io_error, } })? as RawFd; diff --git a/aya/src/programs/xdp.rs b/aya/src/programs/xdp.rs index 1fad9da1..4940fc7e 100644 --- a/aya/src/programs/xdp.rs +++ b/aya/src/programs/xdp.rs @@ -130,7 +130,7 @@ impl Xdp { if k_ver >= (5, 9, 0) { let link_fd = bpf_link_create(prog_fd, if_index, BPF_XDP, None, flags.bits()).map_err( |(_, io_error)| ProgramError::SyscallError { - call: "bpf_link_create".to_owned(), + call: "bpf_link_create", io_error, }, )? as RawFd; @@ -176,7 +176,7 @@ impl Xdp { let link_fd = fd_link.fd; bpf_link_update(link_fd, prog_fd, None, 0).map_err(|(_, io_error)| { ProgramError::SyscallError { - call: "bpf_link_update".to_string(), + call: "bpf_link_update", io_error, } })?; @@ -284,7 +284,7 @@ impl TryFrom for XdpLink { // unwrap of fd_link.fd will not panic since it's only None when being dropped. let info = bpf_link_get_info_by_fd(fd_link.fd).map_err(|io_error| LinkError::SyscallError { - call: "BPF_OBJ_GET_INFO_BY_FD".to_string(), + call: "BPF_OBJ_GET_INFO_BY_FD", code: 0, io_error, })?; diff --git a/aya/src/sys/bpf.rs b/aya/src/sys/bpf.rs index 0bbdf943..e0aa1669 100644 --- a/aya/src/sys/bpf.rs +++ b/aya/src/sys/bpf.rs @@ -748,7 +748,7 @@ pub(crate) fn is_bpf_cookie_supported() -> bool { pub(crate) fn is_btf_supported() -> bool { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0)); btf.add_type(int_type); let btf_bytes = btf.to_bytes(); @@ -770,12 +770,12 @@ pub(crate) fn is_btf_supported() -> bool { pub(crate) fn is_btf_func_supported() -> bool { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0)); let int_type_id = btf.add_type(int_type); - let a_name = btf.add_string("a".to_string()); - let b_name = btf.add_string("b".to_string()); + let a_name = btf.add_string("a"); + let b_name = btf.add_string("b"); let params = vec![ BtfParam { name_offset: a_name, @@ -789,7 +789,7 @@ pub(crate) fn is_btf_func_supported() -> bool { let func_proto = BtfType::FuncProto(FuncProto::new(params, int_type_id)); let func_proto_type_id = btf.add_type(func_proto); - let add = btf.add_string("inc".to_string()); + let add = btf.add_string("inc"); let func = BtfType::Func(Func::new(add, func_proto_type_id, FuncLinkage::Static)); btf.add_type(func); @@ -812,12 +812,12 @@ pub(crate) fn is_btf_func_supported() -> bool { pub(crate) fn is_btf_func_global_supported() -> bool { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0)); let int_type_id = btf.add_type(int_type); - let a_name = btf.add_string("a".to_string()); - let b_name = btf.add_string("b".to_string()); + let a_name = btf.add_string("a"); + let b_name = btf.add_string("b"); let params = vec![ BtfParam { name_offset: a_name, @@ -831,7 +831,7 @@ pub(crate) fn is_btf_func_global_supported() -> bool { let func_proto = BtfType::FuncProto(FuncProto::new(params, int_type_id)); let func_proto_type_id = btf.add_type(func_proto); - let add = btf.add_string("inc".to_string()); + let add = btf.add_string("inc"); let func = BtfType::Func(Func::new(add, func_proto_type_id, FuncLinkage::Global)); btf.add_type(func); @@ -854,15 +854,15 @@ pub(crate) fn is_btf_func_global_supported() -> bool { pub(crate) fn is_btf_datasec_supported() -> bool { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0)); let int_type_id = btf.add_type(int_type); - let name_offset = btf.add_string("foo".to_string()); + let name_offset = btf.add_string("foo"); let var_type = BtfType::Var(Var::new(name_offset, int_type_id, VarLinkage::Static)); let var_type_id = btf.add_type(var_type); - let name_offset = btf.add_string(".data".to_string()); + let name_offset = btf.add_string(".data"); let variables = vec![DataSecEntry { btf_type: var_type_id, offset: 0, @@ -890,7 +890,7 @@ pub(crate) fn is_btf_datasec_supported() -> bool { pub(crate) fn is_btf_float_supported() -> bool { let mut btf = Btf::new(); - let name_offset = btf.add_string("float".to_string()); + let name_offset = btf.add_string("float"); let float_type = BtfType::Float(Float::new(name_offset, 16)); btf.add_type(float_type); @@ -913,15 +913,15 @@ pub(crate) fn is_btf_float_supported() -> bool { pub(crate) fn is_btf_decl_tag_supported() -> bool { let mut btf = Btf::new(); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0)); let int_type_id = btf.add_type(int_type); - let name_offset = btf.add_string("foo".to_string()); + let name_offset = btf.add_string("foo"); let var_type = BtfType::Var(Var::new(name_offset, int_type_id, VarLinkage::Static)); let var_type_id = btf.add_type(var_type); - let name_offset = btf.add_string("decl_tag".to_string()); + let name_offset = btf.add_string("decl_tag"); let decl_tag = BtfType::DeclTag(DeclTag::new(name_offset, var_type_id, -1)); btf.add_type(decl_tag); @@ -948,7 +948,7 @@ pub(crate) fn is_btf_type_tag_supported() -> bool { let int_type = BtfType::Int(Int::new(0, 4, IntEncoding::Signed, 0)); let int_type_id = btf.add_type(int_type); - let name_offset = btf.add_string("int".to_string()); + let name_offset = btf.add_string("int"); let type_tag = BtfType::TypeTag(TypeTag::new(name_offset, int_type_id)); let type_tag_type = btf.add_type(type_tag); diff --git a/xtask/src/build_ebpf.rs b/xtask/src/build_ebpf.rs index 91e57e72..5a9889c3 100644 --- a/xtask/src/build_ebpf.rs +++ b/xtask/src/build_ebpf.rs @@ -19,13 +19,13 @@ pub enum Architecture { } impl std::str::FromStr for Architecture { - type Err = String; + type Err = &'static str; fn from_str(s: &str) -> Result { Ok(match s { "bpfel-unknown-none" => Architecture::BpfEl, "bpfeb-unknown-none" => Architecture::BpfEb, - _ => return Err("invalid target".to_owned()), + _ => return Err("invalid target"), }) } } diff --git a/xtask/src/codegen/mod.rs b/xtask/src/codegen/mod.rs index ab3fa96f..e2321142 100644 --- a/xtask/src/codegen/mod.rs +++ b/xtask/src/codegen/mod.rs @@ -28,7 +28,7 @@ impl Architecture { } impl std::str::FromStr for Architecture { - type Err = String; + type Err = &'static str; fn from_str(s: &str) -> Result { Ok(match s { @@ -36,7 +36,7 @@ impl std::str::FromStr for Architecture { "armv7" => Architecture::ARMv7, "aarch64" => Architecture::AArch64, "riscv64" => Architecture::RISCV64, - _ => return Err("invalid architecture".to_owned()), + _ => return Err("invalid architecture"), }) } }