SQUASHME: use Iterator::try_fold, remove pointless `resolve_type`

reviewable/pr1340/r19
Tamir Duberstein 2 months ago
parent 07d639d1a5
commit 4623a86ae2
No known key found for this signature in database

@ -159,9 +159,9 @@ pub enum BtfError {
#[error("Unable to get symbol name")] #[error("Unable to get symbol name")]
InvalidSymbolName, InvalidSymbolName,
/// BTF map wrapper's layout is invalid /// BTF map wrapper's layout is unexpected
#[error("BTF map wrapper's layout is invalid")] #[error("BTF map wrapper's layout is unexpected")]
BtfMapWrapperInvalidLayout, UnexpectedBtfMapWrapperLayout,
} }
/// Available BTF features /// Available BTF features

@ -1164,7 +1164,6 @@ fn get_map_field(btf: &Btf, type_id: u32) -> Result<u32, BtfError> {
}); });
} }
}; };
// Safety: union
let arr = match &btf.type_by_id(pty.btf_type)? { let arr = match &btf.type_by_id(pty.btf_type)? {
BtfType::Array(Array { array, .. }) => array, BtfType::Array(Array { array, .. }) => array,
other => { other => {
@ -1250,9 +1249,10 @@ fn parse_btf_map_def(btf: &Btf, info: &DataSecEntry) -> Result<(String, BtfMapDe
} }
}; };
let map_name = btf.string_at(ty.name_offset)?; let map_name = btf.string_at(ty.name_offset)?;
let mut map_def = BtfMapDef::default();
let root_type = btf.resolve_type(ty.btf_type)?; let root_type = btf.resolve_type(ty.btf_type)?;
let mut s = match btf.type_by_id(root_type)? { let s = match btf.type_by_id(root_type)? {
BtfType::Struct(s) => s, BtfType::Struct(s) => s,
other => { other => {
return Err(BtfError::UnexpectedBtfType { return Err(BtfError::UnexpectedBtfType {
@ -1261,8 +1261,6 @@ fn parse_btf_map_def(btf: &Btf, info: &DataSecEntry) -> Result<(String, BtfMapDe
} }
}; };
let mut map_def = BtfMapDef::default();
// In aya-ebpf, the BTF map definition types are not used directly in the // In aya-ebpf, the BTF map definition types are not used directly in the
// map variables. Instead, they are wrapped in two nested types: // map variables. Instead, they are wrapped in two nested types:
// //
@ -1278,36 +1276,32 @@ fn parse_btf_map_def(btf: &Btf, info: &DataSecEntry) -> Result<(String, BtfMapDe
// Therefore, the traversal to the actual map definition looks like: // Therefore, the traversal to the actual map definition looks like:
// //
// HashMap -> __0 -> value // HashMap -> __0 -> value
match &s.members[..] { let mut s = s;
[wrapper_field] if btf.string_at(wrapper_field.name_offset)?.as_ref() == "__0" => { for (index, expected_field_name) in ["__0", "value"].into_iter().enumerate() {
let unsafe_cell_type = btf.resolve_type(wrapper_field.btf_type)?; match s.members.as_slice() {
let BtfType::Struct(unsafe_cell) = btf.type_by_id(unsafe_cell_type)? else { [m] => {
return Err(BtfError::UnexpectedBtfType { let field_name = btf.string_at(m.name_offset)?;
type_id: unsafe_cell_type, if field_name.as_ref() != expected_field_name {
}); return Err(BtfError::UnexpectedBtfMapWrapperLayout);
}; }
s = match btf.type_by_id(m.btf_type)? {
match &unsafe_cell.members[..] { BtfType::Struct(s) => s,
[unsafe_cell_field] => { _ => {
if btf.string_at(unsafe_cell_field.name_offset)?.as_ref() == "value" { return Err(BtfError::UnexpectedBtfType {
let map_def_type = btf.resolve_type(unsafe_cell_field.btf_type)?; type_id: m.btf_type,
let BtfType::Struct(map_def) = });
btf.type_by_id(unsafe_cell_field.btf_type)?
else {
return Err(BtfError::UnexpectedBtfType {
type_id: map_def_type,
});
};
// Assign `map_def` to `s` and let the loop below process it.
s = map_def;
} else {
return Err(BtfError::BtfMapWrapperInvalidLayout);
} }
};
}
// If the first wrapper level is missing, use the original struct.
_ => {
if index == 0 {
break;
} else {
return Err(BtfError::UnexpectedBtfMapWrapperLayout);
} }
_ => return Err(BtfError::BtfMapWrapperInvalidLayout),
} }
} }
_ => {}
} }
for m in &s.members { for m in &s.members {
@ -1317,7 +1311,6 @@ fn parse_btf_map_def(btf: &Btf, info: &DataSecEntry) -> Result<(String, BtfMapDe
} }
"key" => { "key" => {
if let BtfType::Ptr(pty) = btf.type_by_id(m.btf_type)? { if let BtfType::Ptr(pty) = btf.type_by_id(m.btf_type)? {
// Safety: union
let t = pty.btf_type; let t = pty.btf_type;
map_def.key_size = btf.type_size(t)? as u32; map_def.key_size = btf.type_size(t)? as u32;
map_def.btf_key_type_id = t; map_def.btf_key_type_id = t;

@ -1,7 +1,6 @@
pub mod aya_obj pub mod aya_obj
pub mod aya_obj::btf pub mod aya_obj::btf
pub enum aya_obj::btf::BtfError pub enum aya_obj::btf::BtfError
pub aya_obj::btf::BtfError::BtfMapWrapperInvalidLayout
pub aya_obj::btf::BtfError::FileError pub aya_obj::btf::BtfError::FileError
pub aya_obj::btf::BtfError::FileError::error: std::io::error::Error pub aya_obj::btf::BtfError::FileError::error: std::io::error::Error
pub aya_obj::btf::BtfError::FileError::path: std::path::PathBuf pub aya_obj::btf::BtfError::FileError::path: std::path::PathBuf
@ -31,6 +30,7 @@ pub aya_obj::btf::BtfError::MaximumTypeDepthReached
pub aya_obj::btf::BtfError::MaximumTypeDepthReached::type_id: u32 pub aya_obj::btf::BtfError::MaximumTypeDepthReached::type_id: u32
pub aya_obj::btf::BtfError::SymbolOffsetNotFound pub aya_obj::btf::BtfError::SymbolOffsetNotFound
pub aya_obj::btf::BtfError::SymbolOffsetNotFound::symbol_name: alloc::string::String pub aya_obj::btf::BtfError::SymbolOffsetNotFound::symbol_name: alloc::string::String
pub aya_obj::btf::BtfError::UnexpectedBtfMapWrapperLayout
pub aya_obj::btf::BtfError::UnexpectedBtfType pub aya_obj::btf::BtfError::UnexpectedBtfType
pub aya_obj::btf::BtfError::UnexpectedBtfType::type_id: u32 pub aya_obj::btf::BtfError::UnexpectedBtfType::type_id: u32
pub aya_obj::btf::BtfError::UnknownBtfType pub aya_obj::btf::BtfError::UnknownBtfType

Loading…
Cancel
Save