test: adjust test to not use byte arrays

Where possible, replace the hardcoded byte arrays in the tests with the
structs they represent, then convert the structs to byte arrays.

Signed-off-by: Billy McFall <22157057+Billy99@users.noreply.github.com>
pull/974/head
Billy McFall 2 months ago
parent cd1db86fd4
commit 4dc4b5ccd4

@ -1110,17 +1110,18 @@ mod tests {
#[test] #[test]
fn test_parse_header() { fn test_parse_header() {
#[cfg(target_endian = "little")] let header = btf_header {
let data: &[u8] = &[ magic: 0xeb9f,
0x9f, 0xeb, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x54, version: 0x01,
0x2a, 0x00, 0x64, 0x54, 0x2a, 0x00, 0x10, 0x64, 0x1c, 0x00, flags: 0x00,
]; hdr_len: 0x18,
#[cfg(target_endian = "big")] type_off: 0x00,
let data: &[u8] = &[ type_len: 0x2a5464,
0xeb, 0x9f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, str_off: 0x2a5464,
0x54, 0x64, 0x00, 0x2a, 0x54, 0x64, 0x00, 0x1c, 0x64, 0x10, str_len: 0x1c6410,
]; };
let header = unsafe { read_btf_header(data) }; let data = unsafe { bytes_of::<btf_header>(&header).to_vec() };
let header = unsafe { read_btf_header(&data) };
assert_eq!(header.magic, 0xeb9f); assert_eq!(header.magic, 0xeb9f);
assert_eq!(header.version, 0x01); assert_eq!(header.version, 0x01);
assert_eq!(header.flags, 0x00); assert_eq!(header.flags, 0x00);
@ -1221,51 +1222,88 @@ mod tests {
assert_eq!(data2.len(), 517); assert_eq!(data2.len(), 517);
assert_eq!(data, data2); assert_eq!(data, data2);
#[cfg(target_endian = "little")] const FUNC_LEN: u32 = 0x14;
let ext_data: &[u8] = &[ const LINE_INFO_LEN: u32 = 0x1c;
0x9f, 0xeb, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, const CORE_RELO_LEN: u32 = 0;
0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, const DATA_LEN: u32 = (FUNC_LEN + LINE_INFO_LEN + CORE_RELO_LEN) / 4;
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x01, 0x00, struct TestStruct {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, _header: btf_ext_header,
0x72, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0x00, _data: [u32; DATA_LEN as usize],
0x00, 0x00, 0xa2, 0x00, 0x00, 0x00, 0x05, 0x2c, 0x00, 0x00, }
]; let test_data = TestStruct {
#[cfg(target_endian = "big")] _header: btf_ext_header {
let ext_data: &[u8] = &[ magic: 0xeb9f,
0xeb, 0x9f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, version: 1,
0x00, 0x14, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x30, flags: 0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, hdr_len: 0x20,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, func_info_off: 0,
0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, func_info_len: FUNC_LEN,
0x00, 0x7b, 0x00, 0x00, 0x00, 0xa2, 0x00, 0x00, 0x2c, 0x05, line_info_off: FUNC_LEN,
]; line_info_len: LINE_INFO_LEN,
core_relo_off: FUNC_LEN + LINE_INFO_LEN,
core_relo_len: CORE_RELO_LEN,
},
_data: [
0x00000008u32,
0x00000072u32,
0x00000001u32,
0x00000000u32,
0x00000007u32,
0x00000010u32,
0x00000072u32,
0x00000001u32,
0x00000000u32,
0x0000007bu32,
0x000000a2u32,
0x00002c05u32,
],
};
let ext_data = unsafe { bytes_of::<TestStruct>(&test_data).to_vec() };
assert_eq!(ext_data.len(), 80); assert_eq!(ext_data.len(), 80);
let _: BtfExt = BtfExt::parse(ext_data, Endianness::default(), &btf) let _: BtfExt = BtfExt::parse(&ext_data, Endianness::default(), &btf)
.unwrap_or_else(|e| panic!("{}", e)); .unwrap_or_else(|e| panic!("{}", e));
} }
#[test] #[test]
fn parsing_older_ext_data() { fn parsing_older_ext_data() {
#[cfg(target_endian = "little")] const TYPE_LEN: u32 = 0;
let btf_data = [ const STR_LEN: u32 = 1;
159, 235, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, struct BtfTestStruct {
]; _header: btf_header,
#[cfg(target_endian = "big")] _data: [u8; (TYPE_LEN + STR_LEN) as usize],
let btf_data = [ }
235, 159, 1, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, let btf_test_data = BtfTestStruct {
]; _header: btf_header {
magic: 0xeb9f,
version: 0x01,
flags: 0x00,
hdr_len: 24,
type_off: 0,
type_len: TYPE_LEN,
str_off: TYPE_LEN,
str_len: TYPE_LEN + STR_LEN,
},
_data: [0x00u8],
};
let btf_data = unsafe { bytes_of::<BtfTestStruct>(&btf_test_data).to_vec() };
#[cfg(target_endian = "little")] const FUNC_INFO_LEN: u32 = 4;
let btf_ext_data = [ const LINE_INFO_LEN: u32 = 4;
159, 235, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 8, 0, 0, const CORE_RELO_LEN: u32 = 16;
0, 16, 0, 0, 0, let ext_header = btf_ext_header {
]; magic: 0xeb9f,
#[cfg(target_endian = "big")] version: 1,
let btf_ext_data = [ flags: 0,
235, 159, 1, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, hdr_len: 24,
8, 0, 0, 0, 16, func_info_off: 0,
]; func_info_len: FUNC_INFO_LEN,
line_info_off: FUNC_INFO_LEN,
line_info_len: LINE_INFO_LEN,
core_relo_off: FUNC_INFO_LEN + LINE_INFO_LEN,
core_relo_len: CORE_RELO_LEN,
};
let btf_ext_data = unsafe { bytes_of::<btf_ext_header>(&ext_header).to_vec() };
let btf = Btf::parse(&btf_data, Endianness::default()).unwrap(); let btf = Btf::parse(&btf_data, Endianness::default()).unwrap();
let btf_ext = BtfExt::parse(&btf_ext_data, Endianness::default(), &btf).unwrap(); let btf_ext = BtfExt::parse(&btf_ext_data, Endianness::default(), &btf).unwrap();
@ -1771,6 +1809,7 @@ mod tests {
Btf::parse(&raw, Endianness::default()).unwrap(); Btf::parse(&raw, Endianness::default()).unwrap();
} }
// Not possible to emulate file system file "/sys/kernel/btf/vmlinux" as big endian, so skip
#[test] #[test]
#[cfg(feature = "std")] #[cfg(feature = "std")]
#[cfg_attr(miri, ignore = "`open` not available when isolation is enabled")] #[cfg_attr(miri, ignore = "`open` not available when isolation is enabled")]

@ -662,7 +662,8 @@ pub struct Union {
impl Union { impl Union {
pub(crate) fn new(name_offset: u32, size: u32, members: Vec<BtfMember>) -> Self { pub(crate) fn new(name_offset: u32, size: u32, members: Vec<BtfMember>) -> Self {
let info = (BtfKind::Union as u32) << 24; let mut info = (BtfKind::Union as u32) << 24;
info |= (members.len() as u32) & 0xFFFF;
Self { Self {
name_offset, name_offset,
info, info,
@ -1577,16 +1578,8 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_int() { fn test_read_btf_type_int() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let bpf_type = BtfType::Int(Int::new(1, 8, IntEncoding::None, 0));
let data: &[u8] = &[ let data: &[u8] = &bpf_type.to_bytes();
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x00, 0x40, 0x00,
0x00, 0x00,
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
0x00, 0x40,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Int(new @ Int { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Int(new @ Int {
name_offset, name_offset,
info: _, info: _,
@ -1600,65 +1593,11 @@ mod tests {
}); });
} }
#[test]
fn test_write_btf_long_unsigned_int() {
#[cfg(target_endian = "little")]
let data: &[u8] = &[
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x00, 0x40, 0x00,
0x00, 0x00,
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
0x00, 0x40,
];
let int = Int::new(1, 8, IntEncoding::None, 0);
assert_eq!(int.to_bytes(), data);
}
#[test]
fn test_write_btf_uchar() {
#[cfg(target_endian = "little")]
let data: &[u8] = &[
0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x00,
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x13, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x08,
];
let int = Int::new(0x13, 1, IntEncoding::None, 0);
assert_eq!(int.to_bytes(), data);
}
#[test]
fn test_write_btf_signed_short_int() {
#[cfg(target_endian = "little")]
let data: &[u8] = &[
0x4a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x10, 0x00,
0x00, 0x01,
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x4a, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x00,
0x00, 0x10,
];
let int = Int::new(0x4a, 2, IntEncoding::Signed, 0);
assert_eq!(int.to_bytes(), data);
}
#[test] #[test]
fn test_read_btf_type_ptr() { fn test_read_btf_type_ptr() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let bpf_type = BtfType::Ptr(Ptr::new(0, 0x06));
let data: &[u8] = &[ let data: &[u8] = &bpf_type.to_bytes();
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x06, 0x00, 0x00, 0x00,
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Ptr(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Ptr(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1667,16 +1606,8 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_array() { fn test_read_btf_type_array() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let bpf_type = BtfType::Array(Array::new(0, 1, 0x12, 2));
let data: &[u8] = &[ let data: &[u8] = &bpf_type.to_bytes();
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x02,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Array(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Array(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1685,16 +1616,13 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_struct() { fn test_read_btf_type_struct() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let members = vec![BtfMember {
let data: &[u8] = &[ name_offset: 0x0247,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x00, 0x47, 0x02, btf_type: 0x12,
0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, offset: 0,
]; }];
#[cfg(target_endian = "big")] let bpf_type = BtfType::Struct(Struct::new(0, members, 4));
let data: &[u8] = &[ let data: &[u8] = &bpf_type.to_bytes();
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
0x02, 0x47, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Struct(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Struct(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1703,16 +1631,13 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_union() { fn test_read_btf_type_union() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let members = vec![BtfMember {
let data: &[u8] = &[ name_offset: 0x040d,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x05, 0x04, 0x00, 0x00, 0x00, 0x0d, 0x04, btf_type: 0x68,
0x00, 0x00, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, offset: 0,
]; }];
#[cfg(target_endian = "big")] let bpf_type = BtfType::Union(Union::new(0, 4, members));
let data: &[u8] = &[ let data: &[u8] = &bpf_type.to_bytes();
0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
0x04, 0x0d, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00, 0x00,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Union(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Union(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1721,16 +1646,11 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_enum() { fn test_read_btf_type_enum() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let enum1 = BtfEnum::new(0xc9, 0);
let data: &[u8] = &[ let enum2 = BtfEnum::new(0xcf, 1);
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x06, 0x04, 0x00, 0x00, 0x00, 0xc9, 0x00, let variants = vec![enum1, enum2];
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, let bpf_type = BtfType::Enum(Enum::new(0, false, variants));
]; let data: &[u8] = &bpf_type.to_bytes();
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
0x00, 0xc9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcf, 0x00, 0x00, 0x00, 0x01,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Enum(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Enum(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1739,14 +1659,13 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_fwd() { fn test_read_btf_type_fwd() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let info = (BtfKind::Fwd as u32) << 24;
let data: &[u8] = &[ let bpf_type = BtfType::Fwd(Fwd {
0x0b, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, name_offset: 0x550b,
]; info,
#[cfg(target_endian = "big")] _unused: 0,
let data: &[u8] = &[ });
0x00, 0x00, 0x55, 0x0b, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, let data: &[u8] = &bpf_type.to_bytes();
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Fwd(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Fwd(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1755,14 +1674,8 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_typedef() { fn test_read_btf_type_typedef() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let bpf_type = BtfType::Typedef(Typedef::new(0x31, 0x0b));
let data: &[u8] = &[ let data: &[u8] = &bpf_type.to_bytes();
0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x0b, 0x00, 0x00, 0x00,
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x31, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Typedef(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Typedef(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1771,14 +1684,13 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_volatile() { fn test_read_btf_type_volatile() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let info = (BtfKind::Volatile as u32) << 24;
let data: &[u8] = &[ let bpf_type = BtfType::Volatile(Volatile {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x24, 0x00, 0x00, 0x00, name_offset: 0,
]; info,
#[cfg(target_endian = "big")] btf_type: 0x24,
let data: &[u8] = &[ });
0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, let data: &[u8] = &bpf_type.to_bytes();
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Volatile(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Volatile(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1787,14 +1699,8 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_const() { fn test_read_btf_type_const() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let bpf_type = BtfType::Const(Const::new(1));
let data: &[u8] = &[ let data: &[u8] = &bpf_type.to_bytes();
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x00,
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Const(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Const(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1803,14 +1709,13 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_restrict() { fn test_read_btf_type_restrict() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let info = (BtfKind::Restrict as u32) << 24;
let data: &[u8] = &[ let bpf_type = BtfType::Restrict(Restrict {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x04, 0x00, 0x00, 0x00, name_offset: 0,
]; _info: info,
#[cfg(target_endian = "big")] btf_type: 4,
let data: &[u8] = &[ });
0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, let data: &[u8] = &bpf_type.to_bytes();
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Restrict(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Restrict(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1819,14 +1724,8 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_func() { fn test_read_btf_type_func() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let bpf_type = BtfType::Func(Func::new(0x000f8b17, 0xe4f0, FuncLinkage::Global));
let data: &[u8] = &[ let data: &[u8] = &bpf_type.to_bytes();
0x17, 0x8b, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xf0, 0xe4, 0x00, 0x00,
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x0f, 0x8b, 0x17, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe4, 0xf0,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Func(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Func(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1835,16 +1734,12 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_func_proto() { fn test_read_btf_type_func_proto() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let params = vec![BtfParam {
let data: &[u8] = &[ name_offset: 0,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, btf_type: 0x12,
0x00, 0x00, 0x12, 0x00, 0x00, 0x00, }];
]; let bpf_type = BtfType::FuncProto(FuncProto::new(params, 0));
#[cfg(target_endian = "big")] let data: &[u8] = &bpf_type.to_bytes();
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x12,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::FuncProto(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::FuncProto(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1854,16 +1749,8 @@ mod tests {
fn test_read_btf_type_func_var() { fn test_read_btf_type_func_var() {
let endianness = Endianness::default(); let endianness = Endianness::default();
// NOTE: There was no data in /sys/kernell/btf/vmlinux for this type // NOTE: There was no data in /sys/kernell/btf/vmlinux for this type
#[cfg(target_endian = "little")] let bpf_type = BtfType::Var(Var::new(0, 0xf0, VarLinkage::Static));
let data: &[u8] = &[ let data: &[u8] = &bpf_type.to_bytes();
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00,
0x00, 0x00,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Var(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Var(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1872,16 +1759,13 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_func_datasec() { fn test_read_btf_type_func_datasec() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let entries = vec![DataSecEntry {
let data: &[u8] = &[ btf_type: 11,
0xd9, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, offset: 0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, size: 4,
]; }];
#[cfg(target_endian = "big")] let bpf_type = BtfType::DataSec(DataSec::new(0xd9, entries, 0));
let data: &[u8] = &[ let data: &[u8] = &bpf_type.to_bytes();
0x00, 0x00, 0x00, 0xd9, 0x0f, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::DataSec(DataSec { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::DataSec(DataSec {
name_offset: _, name_offset: _,
info: _, info: _,
@ -1903,14 +1787,8 @@ mod tests {
#[test] #[test]
fn test_read_btf_type_float() { fn test_read_btf_type_float() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let bpf_type = BtfType::Float(Float::new(0x02fd, 8));
let data: &[u8] = &[ let data: &[u8] = &bpf_type.to_bytes();
0x78, 0xfd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x10, 0x08, 0x00, 0x00, 0x00,
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x02, 0xfd, 0x78, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Float(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Float(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });
@ -1962,25 +1840,9 @@ mod tests {
#[test] #[test]
pub fn test_read_btf_type_enum64() { pub fn test_read_btf_type_enum64() {
let endianness = Endianness::default(); let endianness = Endianness::default();
#[cfg(target_endian = "little")] let variants = vec![BtfEnum64::new(0, 0xbbbbbbbbaaaaaaaau64)];
let data: &[u8] = &[ let bpf_type = BtfType::Enum64(Enum64::new(0, false, variants));
0x00, 0x00, 0x00, 0x00, // name offset let data: &[u8] = &bpf_type.to_bytes();
0x01, 0x00, 0x00, 0x13, // info: vlen, type_kind
0x08, 0x00, 0x00, 0x00, // size
0xd7, 0x06, 0x00, 0x00, // enum variant name offset
0xbb, 0xbb, 0xbb, 0xbb, // enum variant low
0xaa, 0xaa, 0xaa, 0xaa, // enum variant high
];
#[cfg(target_endian = "big")]
let data: &[u8] = &[
0x00, 0x00, 0x00, 0x00, // name offset
0x13, 0x00, 0x00, 0x01, // info: vlen, type_kind
0x00, 0x00, 0x00, 0x08, // size
0x00, 0x00, 0x06, 0xd7, // enum variant name offset
0xbb, 0xbb, 0xbb, 0xbb, // enum variant low
0xaa, 0xaa, 0xaa, 0xaa, // enum variant high
];
assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Enum64(got) => { assert_matches!(unsafe { BtfType::read(data, endianness) }.unwrap(), BtfType::Enum64(got) => {
assert_eq!(got.to_bytes(), data); assert_eq!(got.to_bytes(), data);
}); });

@ -1396,6 +1396,7 @@ mod tests {
use assert_matches::assert_matches; use assert_matches::assert_matches;
use super::*; use super::*;
use crate::generated::btf_ext_header;
const FAKE_INS_LEN: u64 = 8; const FAKE_INS_LEN: u64 = 8;
@ -1594,46 +1595,32 @@ mod tests {
#[test] #[test]
fn sanitizes_empty_btf_files_to_none() { fn sanitizes_empty_btf_files_to_none() {
let mut obj = fake_obj(); let mut obj = fake_obj();
#[cfg(target_endian = "little")]
obj.parse_section(fake_section(
EbpfSectionKind::Btf,
".BTF",
&[
159, 235, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
],
None,
))
.unwrap();
#[cfg(target_endian = "big")]
obj.parse_section(fake_section(
EbpfSectionKind::Btf,
".BTF",
&[
235, 159, 1, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
],
None,
))
.unwrap();
#[cfg(target_endian = "little")] let btf = Btf::new();
obj.parse_section(fake_section( let btf_bytes = btf.to_bytes();
EbpfSectionKind::BtfExt, obj.parse_section(fake_section(EbpfSectionKind::Btf, ".BTF", &btf_bytes, None))
".BTF.ext",
&[
159, 235, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 8, 0,
0, 0, 16, 0, 0, 0,
],
None,
))
.unwrap(); .unwrap();
#[cfg(target_endian = "big")]
const FUNC_INFO_LEN: u32 = 4;
const LINE_INFO_LEN: u32 = 4;
const CORE_RELO_LEN: u32 = 16;
let ext_header = btf_ext_header {
magic: 0xeb9f,
version: 1,
flags: 0,
hdr_len: 24,
func_info_off: 0,
func_info_len: FUNC_INFO_LEN,
line_info_off: FUNC_INFO_LEN,
line_info_len: LINE_INFO_LEN,
core_relo_off: FUNC_INFO_LEN + LINE_INFO_LEN,
core_relo_len: CORE_RELO_LEN,
};
let btf_ext_bytes = bytes_of::<btf_ext_header>(&ext_header).to_vec();
obj.parse_section(fake_section( obj.parse_section(fake_section(
EbpfSectionKind::BtfExt, EbpfSectionKind::BtfExt,
".BTF.ext", ".BTF.ext",
&[ &btf_ext_bytes,
235, 129, 1, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0,
0, 8, 0, 0, 0, 16,
],
None, None,
)) ))
.unwrap(); .unwrap();

Loading…
Cancel
Save