123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668 |
- #![allow(missing_docs)]
- use core::{fmt::Display, mem, ptr};
- use alloc::{string::ToString, vec, vec::Vec};
- use object::Endianness;
- use crate::btf::{Btf, BtfError, MAX_RESOLVE_DEPTH};
- #[derive(Clone, Debug)]
- pub enum BtfType {
- Unknown,
- Fwd(Fwd),
- Const(Const),
- Volatile(Volatile),
- Restrict(Restrict),
- Ptr(Ptr),
- Typedef(Typedef),
- Func(Func),
- Int(Int),
- Float(Float),
- Enum(Enum),
- Array(Array),
- Struct(Struct),
- Union(Union),
- FuncProto(FuncProto),
- Var(Var),
- DataSec(DataSec),
- DeclTag(DeclTag),
- TypeTag(TypeTag),
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Fwd {
- pub(crate) name_offset: u32,
- info: u32,
- _unused: u32,
- }
- impl Fwd {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- bytes_of::<Fwd>(self).to_vec()
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Fwd
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Const {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) btf_type: u32,
- }
- impl Const {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- bytes_of::<Const>(self).to_vec()
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Const
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- pub(crate) fn new(btf_type: u32) -> Self {
- let info = (BtfKind::Const as u32) << 24;
- Const {
- name_offset: 0,
- info,
- btf_type,
- }
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Volatile {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) btf_type: u32,
- }
- impl Volatile {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- bytes_of::<Volatile>(self).to_vec()
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Volatile
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- }
- #[derive(Clone, Debug)]
- pub struct Restrict {
- pub(crate) name_offset: u32,
- _info: u32,
- pub(crate) btf_type: u32,
- }
- impl Restrict {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- bytes_of::<Restrict>(self).to_vec()
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Restrict
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Ptr {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) btf_type: u32,
- }
- impl Ptr {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- bytes_of::<Self>(self).to_vec()
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Ptr
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- pub fn new(name_offset: u32, btf_type: u32) -> Self {
- let info = (BtfKind::Ptr as u32) << 24;
- Ptr {
- name_offset,
- info,
- btf_type,
- }
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Typedef {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) btf_type: u32,
- }
- impl Typedef {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- bytes_of::<Self>(self).to_vec()
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Typedef
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- pub(crate) fn new(name_offset: u32, btf_type: u32) -> Self {
- let info = (BtfKind::Typedef as u32) << 24;
- Typedef {
- name_offset,
- info,
- btf_type,
- }
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Float {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) size: u32,
- }
- impl Float {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- bytes_of::<Self>(self).to_vec()
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Float
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- pub fn new(name_offset: u32, size: u32) -> Self {
- let info = (BtfKind::Float as u32) << 24;
- Float {
- name_offset,
- info,
- size,
- }
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Func {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) btf_type: u32,
- }
- #[repr(u32)]
- #[derive(Clone, Debug, PartialEq, Eq)]
- pub enum FuncLinkage {
- Static = 0,
- Global = 1,
- Extern = 2,
- Unknown,
- }
- impl From<u32> for FuncLinkage {
- fn from(v: u32) -> Self {
- match v {
- 0 => FuncLinkage::Static,
- 1 => FuncLinkage::Global,
- 2 => FuncLinkage::Extern,
- _ => FuncLinkage::Unknown,
- }
- }
- }
- impl Func {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- bytes_of::<Self>(self).to_vec()
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Func
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- pub fn new(name_offset: u32, proto: u32, linkage: FuncLinkage) -> Self {
- let mut info = (BtfKind::Func as u32) << 24;
- info |= (linkage as u32) & 0xFFFF;
- Func {
- name_offset,
- info,
- btf_type: proto,
- }
- }
- pub(crate) fn linkage(&self) -> FuncLinkage {
- (self.info & 0xFFF).into()
- }
- pub(crate) fn set_linkage(&mut self, linkage: FuncLinkage) {
- self.info = (self.info & 0xFFFF0000) | (linkage as u32) & 0xFFFF;
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct TypeTag {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) btf_type: u32,
- }
- impl TypeTag {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- bytes_of::<Self>(self).to_vec()
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::TypeTag
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- pub fn new(name_offset: u32, btf_type: u32) -> Self {
- let info = (BtfKind::TypeTag as u32) << 24;
- TypeTag {
- name_offset,
- info,
- btf_type,
- }
- }
- }
- #[repr(u32)]
- #[derive(Clone, Debug, Eq, PartialEq)]
- pub enum IntEncoding {
- None,
- Signed = 1,
- Char = 2,
- Bool = 4,
- Unknown,
- }
- impl From<u32> for IntEncoding {
- fn from(v: u32) -> Self {
- match v {
- 0 => IntEncoding::None,
- 1 => IntEncoding::Signed,
- 2 => IntEncoding::Char,
- 4 => IntEncoding::Bool,
- _ => IntEncoding::Unknown,
- }
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Int {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) size: u32,
- pub(crate) data: u32,
- }
- impl Int {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- let mut buf = vec![];
- buf.extend(bytes_of::<u32>(&self.name_offset));
- buf.extend(bytes_of::<u32>(&self.info));
- buf.extend(bytes_of::<u32>(&self.size));
- buf.extend(bytes_of::<u32>(&self.data));
- buf
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Int
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- pub fn new(name_offset: u32, size: u32, encoding: IntEncoding, offset: u32) -> Self {
- let info = (BtfKind::Int as u32) << 24;
- let mut data = 0u32;
- data |= (encoding as u32 & 0x0f) << 24;
- data |= (offset & 0xff) << 16;
- data |= (size * 8) & 0xff;
- Int {
- name_offset,
- info,
- size,
- data,
- }
- }
- pub(crate) fn encoding(&self) -> IntEncoding {
- ((self.data & 0x0f000000) >> 24).into()
- }
- pub(crate) fn offset(&self) -> u32 {
- (self.data & 0x00ff0000) >> 16
- }
- // TODO: Remove directive this when this crate is pub
- #[cfg(test)]
- pub(crate) fn bits(&self) -> u32 {
- self.data & 0x000000ff
- }
- }
- #[repr(C)]
- #[derive(Debug, Clone)]
- pub(crate) struct BtfEnum {
- pub(crate) name_offset: u32,
- pub(crate) value: u32,
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Enum {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) size: u32,
- pub(crate) variants: Vec<BtfEnum>,
- }
- impl Enum {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- let mut buf = vec![];
- buf.extend(bytes_of::<u32>(&self.name_offset));
- buf.extend(bytes_of::<u32>(&self.info));
- buf.extend(bytes_of::<u32>(&self.size));
- for v in &self.variants {
- buf.extend(bytes_of::<u32>(&v.name_offset));
- buf.extend(bytes_of::<u32>(&v.value));
- }
- buf
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Enum
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Fwd>() + mem::size_of::<BtfEnum>() * self.variants.len()
- }
- pub(crate) fn new(name_offset: u32, variants: Vec<BtfEnum>) -> Self {
- let mut info = (BtfKind::Enum as u32) << 24;
- info |= (variants.len() as u32) & 0xFFFF;
- Enum {
- name_offset,
- info,
- size: 4,
- variants,
- }
- }
- pub(crate) fn is_signed(&self) -> bool {
- self.info >> 31 == 1
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub(crate) struct BtfMember {
- pub(crate) name_offset: u32,
- pub(crate) btf_type: u32,
- pub(crate) offset: u32,
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Struct {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) size: u32,
- pub(crate) members: Vec<BtfMember>,
- }
- impl Struct {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- let mut buf = vec![];
- buf.extend(bytes_of::<u32>(&self.name_offset));
- buf.extend(bytes_of::<u32>(&self.info));
- buf.extend(bytes_of::<u32>(&self.size));
- for v in &self.members {
- buf.extend(bytes_of::<u32>(&v.name_offset));
- buf.extend(bytes_of::<u32>(&v.btf_type));
- buf.extend(bytes_of::<u32>(&v.offset));
- }
- buf
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Struct
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Fwd>() + mem::size_of::<BtfMember>() * self.members.len()
- }
- pub(crate) fn new(name_offset: u32, members: Vec<BtfMember>, size: u32) -> Self {
- let mut info = (BtfKind::Struct as u32) << 24;
- info |= (members.len() as u32) & 0xFFFF;
- Struct {
- name_offset,
- info,
- size,
- members,
- }
- }
- pub(crate) fn member_bit_offset(&self, member: &BtfMember) -> usize {
- let k_flag = self.info >> 31 == 1;
- let bit_offset = if k_flag {
- member.offset & 0xFFFFFF
- } else {
- member.offset
- };
- bit_offset as usize
- }
- pub(crate) fn member_bit_field_size(&self, member: &BtfMember) -> usize {
- let k_flag = (self.info >> 31) == 1;
- let size = if k_flag { member.offset >> 24 } else { 0 };
- size as usize
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Union {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) size: u32,
- pub(crate) members: Vec<BtfMember>,
- }
- impl Union {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- let mut buf = vec![];
- buf.extend(bytes_of::<u32>(&self.name_offset));
- buf.extend(bytes_of::<u32>(&self.info));
- buf.extend(bytes_of::<u32>(&self.size));
- for v in &self.members {
- buf.extend(bytes_of::<u32>(&v.name_offset));
- buf.extend(bytes_of::<u32>(&v.btf_type));
- buf.extend(bytes_of::<u32>(&v.offset));
- }
- buf
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Union
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Fwd>() + mem::size_of::<BtfMember>() * self.members.len()
- }
- pub(crate) fn member_bit_offset(&self, member: &BtfMember) -> usize {
- let k_flag = self.info >> 31 == 1;
- let bit_offset = if k_flag {
- member.offset & 0xFFFFFF
- } else {
- member.offset
- };
- bit_offset as usize
- }
- pub(crate) fn member_bit_field_size(&self, member: &BtfMember) -> usize {
- let k_flag = (self.info >> 31) == 1;
- let size = if k_flag { member.offset >> 24 } else { 0 };
- size as usize
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub(crate) struct BtfArray {
- pub(crate) element_type: u32,
- pub(crate) index_type: u32,
- pub(crate) len: u32,
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Array {
- pub(crate) name_offset: u32,
- info: u32,
- _unused: u32,
- pub(crate) array: BtfArray,
- }
- impl Array {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- let mut buf = vec![];
- buf.extend(bytes_of::<u32>(&self.name_offset));
- buf.extend(bytes_of::<u32>(&self.info));
- buf.extend(bytes_of::<u32>(&self._unused));
- buf.extend(bytes_of::<BtfArray>(&self.array));
- buf
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Array
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- #[cfg(test)]
- pub(crate) fn new(name_offset: u32, element_type: u32, index_type: u32, len: u32) -> Self {
- let info = (BtfKind::Array as u32) << 24;
- Array {
- name_offset,
- info,
- _unused: 0,
- array: BtfArray {
- element_type,
- index_type,
- len,
- },
- }
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct BtfParam {
- pub name_offset: u32,
- pub btf_type: u32,
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct FuncProto {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) return_type: u32,
- pub(crate) params: Vec<BtfParam>,
- }
- impl FuncProto {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- let mut buf = vec![];
- buf.extend(bytes_of::<u32>(&self.name_offset));
- buf.extend(bytes_of::<u32>(&self.info));
- buf.extend(bytes_of::<u32>(&self.return_type));
- for p in &self.params {
- buf.extend(bytes_of::<u32>(&p.name_offset));
- buf.extend(bytes_of::<u32>(&p.btf_type));
- }
- buf
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::FuncProto
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Fwd>() + mem::size_of::<BtfParam>() * self.params.len()
- }
- pub fn new(params: Vec<BtfParam>, return_type: u32) -> Self {
- let mut info = (BtfKind::FuncProto as u32) << 24;
- info |= (params.len() as u32) & 0xFFFF;
- FuncProto {
- name_offset: 0,
- info,
- return_type,
- params,
- }
- }
- }
- #[repr(u32)]
- #[derive(Clone, Debug, PartialEq, Eq)]
- pub enum VarLinkage {
- Static,
- Global,
- Extern,
- Unknown,
- }
- impl From<u32> for VarLinkage {
- fn from(v: u32) -> Self {
- match v {
- 0 => VarLinkage::Static,
- 1 => VarLinkage::Global,
- 2 => VarLinkage::Extern,
- _ => VarLinkage::Unknown,
- }
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct Var {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) btf_type: u32,
- pub(crate) linkage: VarLinkage,
- }
- impl Var {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- let mut buf = vec![];
- buf.extend(bytes_of::<u32>(&self.name_offset));
- buf.extend(bytes_of::<u32>(&self.info));
- buf.extend(bytes_of::<u32>(&self.btf_type));
- buf.extend(bytes_of::<VarLinkage>(&self.linkage));
- buf
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::Var
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- pub fn new(name_offset: u32, btf_type: u32, linkage: VarLinkage) -> Self {
- let info = (BtfKind::Var as u32) << 24;
- Var {
- name_offset,
- info,
- btf_type,
- linkage,
- }
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct DataSecEntry {
- pub btf_type: u32,
- pub offset: u32,
- pub size: u32,
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct DataSec {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) size: u32,
- pub(crate) entries: Vec<DataSecEntry>,
- }
- impl DataSec {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- let mut buf = vec![];
- buf.extend(bytes_of::<u32>(&self.name_offset));
- buf.extend(bytes_of::<u32>(&self.info));
- buf.extend(bytes_of::<u32>(&self.size));
- for e in &self.entries {
- buf.extend(bytes_of::<u32>(&e.btf_type));
- buf.extend(bytes_of::<u32>(&e.offset));
- buf.extend(bytes_of::<u32>(&e.size));
- }
- buf
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::DataSec
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Fwd>() + mem::size_of::<DataSecEntry>() * self.entries.len()
- }
- pub fn new(name_offset: u32, entries: Vec<DataSecEntry>, size: u32) -> Self {
- let mut info = (BtfKind::DataSec as u32) << 24;
- info |= (entries.len() as u32) & 0xFFFF;
- DataSec {
- name_offset,
- info,
- size,
- entries,
- }
- }
- }
- #[repr(C)]
- #[derive(Clone, Debug)]
- pub struct DeclTag {
- pub(crate) name_offset: u32,
- info: u32,
- pub(crate) btf_type: u32,
- pub(crate) component_index: i32,
- }
- impl DeclTag {
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- let mut buf = vec![];
- buf.extend(bytes_of::<u32>(&self.name_offset));
- buf.extend(bytes_of::<u32>(&self.info));
- buf.extend(bytes_of::<u32>(&self.btf_type));
- buf.extend(bytes_of::<i32>(&self.component_index));
- buf
- }
- pub(crate) fn kind(&self) -> BtfKind {
- BtfKind::DeclTag
- }
- pub(crate) fn type_info_size(&self) -> usize {
- mem::size_of::<Self>()
- }
- pub fn new(name_offset: u32, btf_type: u32, component_index: i32) -> Self {
- let info = (BtfKind::DeclTag as u32) << 24;
- DeclTag {
- name_offset,
- info,
- btf_type,
- component_index,
- }
- }
- }
- #[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
- #[repr(u32)]
- pub enum BtfKind {
- #[default]
- Unknown = 0,
- Int = 1,
- Ptr = 2,
- Array = 3,
- Struct = 4,
- Union = 5,
- Enum = 6,
- Fwd = 7,
- Typedef = 8,
- Volatile = 9,
- Const = 10,
- Restrict = 11,
- Func = 12,
- FuncProto = 13,
- Var = 14,
- DataSec = 15,
- Float = 16,
- DeclTag = 17,
- TypeTag = 18,
- }
- impl TryFrom<u32> for BtfKind {
- type Error = BtfError;
- fn try_from(v: u32) -> Result<Self, Self::Error> {
- use BtfKind::*;
- Ok(match v {
- 0 => Unknown,
- 1 => Int,
- 2 => Ptr,
- 3 => Array,
- 4 => Struct,
- 5 => Union,
- 6 => Enum,
- 7 => Fwd,
- 8 => Typedef,
- 9 => Volatile,
- 10 => Const,
- 11 => Restrict,
- 12 => Func,
- 13 => FuncProto,
- 14 => Var,
- 15 => DataSec,
- 16 => Float,
- 17 => DeclTag,
- 18 => TypeTag,
- kind => return Err(BtfError::InvalidTypeKind { kind }),
- })
- }
- }
- impl Display for BtfKind {
- fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
- match self {
- BtfKind::Unknown => write!(f, "[UNKNOWN]"),
- BtfKind::Int => write!(f, "[INT]"),
- BtfKind::Float => write!(f, "[FLOAT]"),
- BtfKind::Ptr => write!(f, "[PTR]"),
- BtfKind::Array => write!(f, "[ARRAY]"),
- BtfKind::Struct => write!(f, "[STRUCT]"),
- BtfKind::Union => write!(f, "[UNION]"),
- BtfKind::Enum => write!(f, "[ENUM]"),
- BtfKind::Fwd => write!(f, "[FWD]"),
- BtfKind::Typedef => write!(f, "[TYPEDEF]"),
- BtfKind::Volatile => write!(f, "[VOLATILE]"),
- BtfKind::Const => write!(f, "[CONST]"),
- BtfKind::Restrict => write!(f, "[RESTRICT]"),
- BtfKind::Func => write!(f, "[FUNC]"),
- BtfKind::FuncProto => write!(f, "[FUNC_PROTO]"),
- BtfKind::Var => write!(f, "[VAR]"),
- BtfKind::DataSec => write!(f, "[DATASEC]"),
- BtfKind::DeclTag => write!(f, "[DECL_TAG]"),
- BtfKind::TypeTag => write!(f, "[TYPE_TAG]"),
- }
- }
- }
- unsafe fn read<T>(data: &[u8]) -> Result<T, BtfError> {
- if mem::size_of::<T>() > data.len() {
- return Err(BtfError::InvalidTypeInfo);
- }
- Ok(ptr::read_unaligned::<T>(data.as_ptr() as *const T))
- }
- unsafe fn read_array<T>(data: &[u8], len: usize) -> Result<Vec<T>, BtfError> {
- if mem::size_of::<T>() * len > data.len() {
- return Err(BtfError::InvalidTypeInfo);
- }
- let data = &data[0..mem::size_of::<T>() * len];
- let r = data
- .chunks(mem::size_of::<T>())
- .map(|chunk| ptr::read_unaligned(chunk.as_ptr() as *const T))
- .collect();
- Ok(r)
- }
- impl BtfType {
- #[allow(unused_unsafe)]
- pub(crate) unsafe fn read(data: &[u8], endianness: Endianness) -> Result<BtfType, BtfError> {
- let ty = unsafe { read_array::<u32>(data, 3)? };
- let data = &data[mem::size_of::<u32>() * 3..];
- let vlen = type_vlen(ty[1]);
- Ok(match type_kind(ty[1])? {
- BtfKind::Unknown => BtfType::Unknown,
- BtfKind::Fwd => BtfType::Fwd(Fwd {
- name_offset: ty[0],
- info: ty[1],
- _unused: 0,
- }),
- BtfKind::Const => BtfType::Const(Const {
- name_offset: ty[0],
- info: ty[1],
- btf_type: ty[2],
- }),
- BtfKind::Volatile => BtfType::Volatile(Volatile {
- name_offset: ty[0],
- info: ty[1],
- btf_type: ty[2],
- }),
- BtfKind::Restrict => BtfType::Restrict(Restrict {
- name_offset: ty[0],
- _info: ty[1],
- btf_type: ty[2],
- }),
- BtfKind::Ptr => BtfType::Ptr(Ptr {
- name_offset: ty[0],
- info: ty[1],
- btf_type: ty[2],
- }),
- BtfKind::Typedef => BtfType::Typedef(Typedef {
- name_offset: ty[0],
- info: ty[1],
- btf_type: ty[2],
- }),
- BtfKind::Func => BtfType::Func(Func {
- name_offset: ty[0],
- info: ty[1],
- btf_type: ty[2],
- }),
- BtfKind::Int => {
- if mem::size_of::<u32>() > data.len() {
- return Err(BtfError::InvalidTypeInfo);
- }
- let read_u32 = if endianness == Endianness::Little {
- u32::from_le_bytes
- } else {
- u32::from_be_bytes
- };
- BtfType::Int(Int {
- name_offset: ty[0],
- info: ty[1],
- size: ty[2],
- data: read_u32(data[..mem::size_of::<u32>()].try_into().unwrap()),
- })
- }
- BtfKind::Float => BtfType::Float(Float {
- name_offset: ty[0],
- info: ty[1],
- size: ty[2],
- }),
- BtfKind::Enum => BtfType::Enum(Enum {
- name_offset: ty[0],
- info: ty[1],
- size: ty[2],
- variants: unsafe { read_array::<BtfEnum>(data, vlen)? },
- }),
- BtfKind::Array => BtfType::Array(Array {
- name_offset: ty[0],
- info: ty[1],
- _unused: 0,
- array: unsafe { read(data)? },
- }),
- BtfKind::Struct => BtfType::Struct(Struct {
- name_offset: ty[0],
- info: ty[1],
- size: ty[2],
- members: unsafe { read_array::<BtfMember>(data, vlen)? },
- }),
- BtfKind::Union => BtfType::Union(Union {
- name_offset: ty[0],
- info: ty[1],
- size: ty[2],
- members: unsafe { read_array::<BtfMember>(data, vlen)? },
- }),
- BtfKind::FuncProto => BtfType::FuncProto(FuncProto {
- name_offset: ty[0],
- info: ty[1],
- return_type: ty[2],
- params: unsafe { read_array::<BtfParam>(data, vlen)? },
- }),
- BtfKind::Var => BtfType::Var(Var {
- name_offset: ty[0],
- info: ty[1],
- btf_type: ty[2],
- linkage: unsafe { read(data)? },
- }),
- BtfKind::DataSec => BtfType::DataSec(DataSec {
- name_offset: ty[0],
- info: ty[1],
- size: ty[2],
- entries: unsafe { read_array::<DataSecEntry>(data, vlen)? },
- }),
- BtfKind::DeclTag => BtfType::DeclTag(DeclTag {
- name_offset: ty[0],
- info: ty[1],
- btf_type: ty[2],
- component_index: unsafe { read(data)? },
- }),
- BtfKind::TypeTag => BtfType::TypeTag(TypeTag {
- name_offset: ty[0],
- info: ty[1],
- btf_type: ty[2],
- }),
- })
- }
- pub(crate) fn to_bytes(&self) -> Vec<u8> {
- match self {
- BtfType::Unknown => vec![],
- BtfType::Fwd(t) => t.to_bytes(),
- BtfType::Const(t) => t.to_bytes(),
- BtfType::Volatile(t) => t.to_bytes(),
- BtfType::Restrict(t) => t.to_bytes(),
- BtfType::Ptr(t) => t.to_bytes(),
- BtfType::Typedef(t) => t.to_bytes(),
- BtfType::Func(t) => t.to_bytes(),
- BtfType::Int(t) => t.to_bytes(),
- BtfType::Float(t) => t.to_bytes(),
- BtfType::Enum(t) => t.to_bytes(),
- BtfType::Array(t) => t.to_bytes(),
- BtfType::Struct(t) => t.to_bytes(),
- BtfType::Union(t) => t.to_bytes(),
- BtfType::FuncProto(t) => t.to_bytes(),
- BtfType::Var(t) => t.to_bytes(),
- BtfType::DataSec(t) => t.to_bytes(),
- BtfType::DeclTag(t) => t.to_bytes(),
- BtfType::TypeTag(t) => t.to_bytes(),
- }
- }
- pub(crate) fn size(&self) -> Option<u32> {
- match self {
- BtfType::Int(t) => Some(t.size),
- BtfType::Float(t) => Some(t.size),
- BtfType::Enum(t) => Some(t.size),
- BtfType::Struct(t) => Some(t.size),
- BtfType::Union(t) => Some(t.size),
- BtfType::DataSec(t) => Some(t.size),
- BtfType::Ptr(_) => Some(mem::size_of::<&()>() as u32),
- _ => None,
- }
- }
- pub(crate) fn btf_type(&self) -> Option<u32> {
- match self {
- BtfType::Const(t) => Some(t.btf_type),
- BtfType::Volatile(t) => Some(t.btf_type),
- BtfType::Restrict(t) => Some(t.btf_type),
- BtfType::Ptr(t) => Some(t.btf_type),
- BtfType::Typedef(t) => Some(t.btf_type),
- // FuncProto contains the return type here, and doesn't directly reference another type
- BtfType::FuncProto(t) => Some(t.return_type),
- BtfType::Var(t) => Some(t.btf_type),
- BtfType::DeclTag(t) => Some(t.btf_type),
- BtfType::TypeTag(t) => Some(t.btf_type),
- _ => None,
- }
- }
- pub(crate) fn type_info_size(&self) -> usize {
- match self {
- BtfType::Unknown => mem::size_of::<Fwd>(),
- BtfType::Fwd(t) => t.type_info_size(),
- BtfType::Const(t) => t.type_info_size(),
- BtfType::Volatile(t) => t.type_info_size(),
- BtfType::Restrict(t) => t.type_info_size(),
- BtfType::Ptr(t) => t.type_info_size(),
- BtfType::Typedef(t) => t.type_info_size(),
- BtfType::Func(t) => t.type_info_size(),
- BtfType::Int(t) => t.type_info_size(),
- BtfType::Float(t) => t.type_info_size(),
- BtfType::Enum(t) => t.type_info_size(),
- BtfType::Array(t) => t.type_info_size(),
- BtfType::Struct(t) => t.type_info_size(),
- BtfType::Union(t) => t.type_info_size(),
- BtfType::FuncProto(t) => t.type_info_size(),
- BtfType::Var(t) => t.type_info_size(),
- BtfType::DataSec(t) => t.type_info_size(),
- BtfType::DeclTag(t) => t.type_info_size(),
- BtfType::TypeTag(t) => t.type_info_size(),
- }
- }
- pub(crate) fn name_offset(&self) -> u32 {
- match self {
- BtfType::Unknown => 0,
- BtfType::Fwd(t) => t.name_offset,
- BtfType::Const(t) => t.name_offset,
- BtfType::Volatile(t) => t.name_offset,
- BtfType::Restrict(t) => t.name_offset,
- BtfType::Ptr(t) => t.name_offset,
- BtfType::Typedef(t) => t.name_offset,
- BtfType::Func(t) => t.name_offset,
- BtfType::Int(t) => t.name_offset,
- BtfType::Float(t) => t.name_offset,
- BtfType::Enum(t) => t.name_offset,
- BtfType::Array(t) => t.name_offset,
- BtfType::Struct(t) => t.name_offset,
- BtfType::Union(t) => t.name_offset,
- BtfType::FuncProto(t) => t.name_offset,
- BtfType::Var(t) => t.name_offset,
- BtfType::DataSec(t) => t.name_offset,
- BtfType::DeclTag(t) => t.name_offset,
- BtfType::TypeTag(t) => t.name_offset,
- }
- }
- pub(crate) fn kind(&self) -> BtfKind {
- match self {
- BtfType::Unknown => BtfKind::Unknown,
- BtfType::Fwd(t) => t.kind(),
- BtfType::Const(t) => t.kind(),
- BtfType::Volatile(t) => t.kind(),
- BtfType::Restrict(t) => t.kind(),
- BtfType::Ptr(t) => t.kind(),
- BtfType::Typedef(t) => t.kind(),
- BtfType::Func(t) => t.kind(),
- BtfType::Int(t) => t.kind(),
- BtfType::Float(t) => t.kind(),
- BtfType::Enum(t) => t.kind(),
- BtfType::Array(t) => t.kind(),
- BtfType::Struct(t) => t.kind(),
- BtfType::Union(t) => t.kind(),
- BtfType::FuncProto(t) => t.kind(),
- BtfType::Var(t) => t.kind(),
- BtfType::DataSec(t) => t.kind(),
- BtfType::DeclTag(t) => t.kind(),
- BtfType::TypeTag(t) => t.kind(),
- }
- }
- pub(crate) fn is_composite(&self) -> bool {
- matches!(self, BtfType::Struct(_) | BtfType::Union(_))
- }
- pub(crate) fn members(&self) -> Option<impl Iterator<Item = &BtfMember>> {
- match self {
- BtfType::Struct(t) => Some(t.members.iter()),
- BtfType::Union(t) => Some(t.members.iter()),
- _ => None,
- }
- }
- pub(crate) fn member_bit_field_size(&self, member: &BtfMember) -> Option<usize> {
- match self {
- BtfType::Struct(t) => Some(t.member_bit_field_size(member)),
- BtfType::Union(t) => Some(t.member_bit_field_size(member)),
- _ => None,
- }
- }
- pub(crate) fn member_bit_offset(&self, member: &BtfMember) -> Option<usize> {
- match self {
- BtfType::Struct(t) => Some(t.member_bit_offset(member)),
- BtfType::Union(t) => Some(t.member_bit_offset(member)),
- _ => None,
- }
- }
- }
- fn type_kind(info: u32) -> Result<BtfKind, BtfError> {
- ((info >> 24) & 0x1F).try_into()
- }
- fn type_vlen(info: u32) -> usize {
- (info & 0xFFFF) as usize
- }
- pub(crate) fn types_are_compatible(
- local_btf: &Btf,
- root_local_id: u32,
- target_btf: &Btf,
- root_target_id: u32,
- ) -> Result<bool, BtfError> {
- let mut local_id = root_local_id;
- let mut target_id = root_target_id;
- let local_ty = local_btf.type_by_id(local_id)?;
- let target_ty = target_btf.type_by_id(target_id)?;
- if local_ty.kind() != target_ty.kind() {
- return Ok(false);
- }
- for _ in 0..MAX_RESOLVE_DEPTH {
- local_id = local_btf.resolve_type(local_id)?;
- target_id = target_btf.resolve_type(target_id)?;
- let local_ty = local_btf.type_by_id(local_id)?;
- let target_ty = target_btf.type_by_id(target_id)?;
- if local_ty.kind() != target_ty.kind() {
- return Ok(false);
- }
- match local_ty {
- BtfType::Unknown
- | BtfType::Struct(_)
- | BtfType::Union(_)
- | BtfType::Enum(_)
- | BtfType::Fwd(_)
- | BtfType::Float(_) => return Ok(true),
- BtfType::Int(local) => {
- if let BtfType::Int(target) = target_ty {
- return Ok(local.offset() == 0 && target.offset() == 0);
- }
- }
- BtfType::Ptr(local) => {
- if let BtfType::Ptr(target) = target_ty {
- local_id = local.btf_type;
- target_id = target.btf_type;
- continue;
- }
- }
- BtfType::Array(Array { array: local, .. }) => {
- if let BtfType::Array(Array { array: target, .. }) = target_ty {
- local_id = local.element_type;
- target_id = target.element_type;
- continue;
- }
- }
- BtfType::FuncProto(local) => {
- if let BtfType::FuncProto(target) = target_ty {
- if local.params.len() != target.params.len() {
- return Ok(false);
- }
- for (l_param, t_param) in local.params.iter().zip(target.params.iter()) {
- let local_id = local_btf.resolve_type(l_param.btf_type)?;
- let target_id = target_btf.resolve_type(t_param.btf_type)?;
- if !types_are_compatible(local_btf, local_id, target_btf, target_id)? {
- return Ok(false);
- }
- }
- local_id = local.return_type;
- target_id = target.return_type;
- continue;
- }
- }
- _ => panic!("this shouldn't be reached"),
- }
- }
- Err(BtfError::MaximumTypeDepthReached { type_id: local_id })
- }
- pub(crate) fn fields_are_compatible(
- local_btf: &Btf,
- mut local_id: u32,
- target_btf: &Btf,
- mut target_id: u32,
- ) -> Result<bool, BtfError> {
- for _ in 0..MAX_RESOLVE_DEPTH {
- local_id = local_btf.resolve_type(local_id)?;
- target_id = target_btf.resolve_type(target_id)?;
- let local_ty = local_btf.type_by_id(local_id)?;
- let target_ty = target_btf.type_by_id(target_id)?;
- if local_ty.is_composite() && target_ty.is_composite() {
- return Ok(true);
- }
- if local_ty.kind() != target_ty.kind() {
- return Ok(false);
- }
- match local_ty {
- BtfType::Fwd(_) | BtfType::Enum(_) => {
- let flavorless_name =
- |name: &str| name.split_once("___").map_or(name, |x| x.0).to_string();
- let local_name = flavorless_name(&local_btf.type_name(local_ty)?);
- let target_name = flavorless_name(&target_btf.type_name(target_ty)?);
- return Ok(local_name == target_name);
- }
- BtfType::Int(local) => {
- if let BtfType::Int(target) = target_ty {
- return Ok(local.offset() == 0 && target.offset() == 0);
- }
- }
- BtfType::Float(_) => return Ok(true),
- BtfType::Ptr(_) => return Ok(true),
- BtfType::Array(Array { array: local, .. }) => {
- if let BtfType::Array(Array { array: target, .. }) = target_ty {
- local_id = local.element_type;
- target_id = target.element_type;
- continue;
- }
- }
- _ => panic!("this shouldn't be reached"),
- }
- }
- Err(BtfError::MaximumTypeDepthReached { type_id: local_id })
- }
- fn bytes_of<T>(val: &T) -> &[u8] {
- // Safety: all btf types are POD
- unsafe { crate::util::bytes_of(val) }
- }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test_read_btf_type_int() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x00, 0x40, 0x00,
- 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Int(new)) => {
- assert_eq!(new.name_offset, 1);
- assert_eq!(new.size, 8);
- assert_eq!(new.bits(), 64);
- let data2 = new.to_bytes();
- assert_eq!(data, data2);
- }
- Ok(t) => panic!("expected int type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- }
- #[test]
- fn test_write_btf_long_unsigned_int() {
- let data: &[u8] = &[
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x00, 0x40, 0x00,
- 0x00, 0x00,
- ];
- let int = Int::new(1, 8, IntEncoding::None, 0);
- assert_eq!(int.to_bytes(), data);
- }
- #[test]
- fn test_write_btf_uchar() {
- let data: &[u8] = &[
- 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00,
- 0x00, 0x00,
- ];
- let int = Int::new(0x13, 1, IntEncoding::None, 0);
- assert_eq!(int.to_bytes(), data);
- }
- #[test]
- fn test_write_btf_signed_short_int() {
- let data: &[u8] = &[
- 0x4a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x10, 0x00,
- 0x00, 0x01,
- ];
- let int = Int::new(0x4a, 2, IntEncoding::Signed, 0);
- assert_eq!(int.to_bytes(), data);
- }
- #[test]
- fn test_read_btf_type_ptr() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x06, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Ptr(_)) => {}
- Ok(t) => panic!("expected ptr type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_array() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
- 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Array(_)) => {}
- Ok(t) => panic!("expected array type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_struct() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x00, 0x47, 0x02,
- 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Struct(_)) => {}
- Ok(t) => panic!("expected struct type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_union() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x05, 0x04, 0x00, 0x00, 0x00, 0x0d, 0x04,
- 0x00, 0x00, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Union(_)) => {}
- Ok(t) => panic!("expected union type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_enum() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x06, 0x04, 0x00, 0x00, 0x00, 0xc9, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcf, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Enum(_)) => {}
- Ok(t) => panic!("expected enum type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_fwd() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x0b, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Fwd(_)) => {}
- Ok(t) => panic!("expected fwd type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_typedef() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x0b, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Typedef(_)) => {}
- Ok(t) => panic!("expected typedef type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_volatile() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x24, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Volatile(_)) => {}
- Ok(t) => panic!("expected volatile type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_const() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Const(_)) => {}
- Ok(t) => panic!("expected const type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_restrict() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x04, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Restrict(_)) => {}
- Ok(t) => panic!("expected restrict type gpt {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_func() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x17, 0x8b, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xf0, 0xe4, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Func(_)) => {}
- Ok(t) => panic!("expected func type gpt {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_func_proto() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x12, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::FuncProto(_)) => {}
- Ok(t) => panic!("expected func_proto type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_func_var() {
- let endianness = Endianness::default();
- // NOTE: There was no data in /sys/kernell/btf/vmlinux for this type
- let data: &[u8] = &[
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Var(_)) => {}
- Ok(t) => panic!("expected var type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- };
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_func_datasec() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0xd9, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match &got {
- Ok(BtfType::DataSec(ty)) => {
- assert_eq!(0, ty.size);
- assert_eq!(11, ty.entries[0].btf_type);
- assert_eq!(0, ty.entries[0].offset);
- assert_eq!(4, ty.entries[0].size);
- }
- Ok(t) => panic!("expected datasec type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_read_btf_type_float() {
- let endianness = Endianness::default();
- let data: &[u8] = &[
- 0x78, 0xfd, 0x02, 0x00, 0x00, 0x00, 0x00, 0x10, 0x08, 0x00, 0x00, 0x00,
- ];
- let got = unsafe { BtfType::read(data, endianness) };
- match got {
- Ok(BtfType::Float(_)) => {}
- Ok(t) => panic!("expected float type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- let data2 = got.unwrap().to_bytes();
- assert_eq!(data, data2)
- }
- #[test]
- fn test_write_btf_func_proto() {
- let params = vec![
- BtfParam {
- name_offset: 1,
- btf_type: 1,
- },
- BtfParam {
- name_offset: 3,
- btf_type: 1,
- },
- ];
- let func_proto = FuncProto::new(params, 2);
- let data = func_proto.to_bytes();
- let got = unsafe { BtfType::read(&data, Endianness::default()) };
- match got {
- Ok(BtfType::FuncProto(fp)) => {
- assert_eq!(fp.params.len(), 2);
- }
- Ok(t) => panic!("expected func proto type, got {t:#?}"),
- Err(_) => panic!("unexpected error"),
- }
- }
- #[test]
- fn test_types_are_compatible() {
- let mut btf = Btf::new();
- let name_offset = btf.add_string("u32".to_string());
- 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 u64t = btf.add_type(BtfType::Int(Int::new(name_offset, 8, IntEncoding::None, 0)));
- let name_offset = btf.add_string("widgets".to_string());
- let array_type = btf.add_type(BtfType::Array(Array::new(name_offset, u64t, u32t, 16)));
- assert!(types_are_compatible(&btf, u32t, &btf, u32t).unwrap());
- // int types are compatible if offsets match. size and encoding aren't compared
- assert!(types_are_compatible(&btf, u32t, &btf, u64t).unwrap());
- assert!(types_are_compatible(&btf, array_type, &btf, array_type).unwrap());
- }
- }
|