btf.rs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. use core::{ffi::CStr, mem, ptr};
  2. use alloc::{
  3. borrow::Cow,
  4. ffi::CString,
  5. format,
  6. string::{String, ToString},
  7. vec,
  8. vec::Vec,
  9. };
  10. use bytes::BufMut;
  11. use log::debug;
  12. use object::Endianness;
  13. use crate::{
  14. btf::{
  15. info::{FuncSecInfo, LineSecInfo},
  16. relocation::Relocation,
  17. Array, BtfEnum, BtfKind, BtfMember, BtfType, Const, Enum, FuncInfo, FuncLinkage, Int,
  18. IntEncoding, LineInfo, Struct, Typedef, VarLinkage,
  19. },
  20. generated::{btf_ext_header, btf_header},
  21. thiserror::{self, Error},
  22. util::{bytes_of, HashMap},
  23. Object,
  24. };
  25. pub(crate) const MAX_RESOLVE_DEPTH: u8 = 32;
  26. pub(crate) const MAX_SPEC_LEN: usize = 64;
  27. /// The error type returned when `BTF` operations fail.
  28. #[derive(Error, Debug)]
  29. pub enum BtfError {
  30. #[cfg(not(feature = "no_std"))]
  31. /// Error parsing file
  32. #[error("error parsing {path}")]
  33. FileError {
  34. /// file path
  35. path: std::path::PathBuf,
  36. /// source of the error
  37. #[source]
  38. error: std::io::Error,
  39. },
  40. /// Error parsing BTF header
  41. #[error("error parsing BTF header")]
  42. InvalidHeader,
  43. /// invalid BTF type info segment
  44. #[error("invalid BTF type info segment")]
  45. InvalidTypeInfo,
  46. /// invalid BTF relocation info segment
  47. #[error("invalid BTF relocation info segment")]
  48. InvalidRelocationInfo,
  49. /// invalid BTF type kind
  50. #[error("invalid BTF type kind `{kind}`")]
  51. InvalidTypeKind {
  52. /// type kind
  53. kind: u32,
  54. },
  55. /// invalid BTF relocation kind
  56. #[error("invalid BTF relocation kind `{kind}`")]
  57. InvalidRelocationKind {
  58. /// type kind
  59. kind: u32,
  60. },
  61. /// invalid BTF string offset
  62. #[error("invalid BTF string offset: {offset}")]
  63. InvalidStringOffset {
  64. /// offset
  65. offset: usize,
  66. },
  67. /// invalid BTF info
  68. #[error("invalid BTF info, offset: {offset} len: {len} section_len: {section_len}")]
  69. InvalidInfo {
  70. /// offset
  71. offset: usize,
  72. /// length
  73. len: usize,
  74. /// section length
  75. section_len: usize,
  76. },
  77. /// invalid BTF line infos
  78. #[error("invalid BTF line info, offset: {offset} len: {len} section_len: {section_len}")]
  79. InvalidLineInfo {
  80. /// offset
  81. offset: usize,
  82. /// length
  83. len: usize,
  84. /// section length
  85. section_len: usize,
  86. },
  87. /// unknown BTF type id
  88. #[error("Unknown BTF type id `{type_id}`")]
  89. UnknownBtfType {
  90. /// type id
  91. type_id: u32,
  92. },
  93. /// unexpected btf type id
  94. #[error("Unexpected BTF type id `{type_id}`")]
  95. UnexpectedBtfType {
  96. /// type id
  97. type_id: u32,
  98. },
  99. /// unknown BTF type
  100. #[error("Unknown BTF type `{type_name}`")]
  101. UnknownBtfTypeName {
  102. /// type name
  103. type_name: String,
  104. },
  105. /// maximum depth reached resolving BTF type
  106. #[error("maximum depth reached resolving BTF type")]
  107. MaximumTypeDepthReached {
  108. /// type id
  109. type_id: u32,
  110. },
  111. #[cfg(not(feature = "no_std"))]
  112. /// Loading the btf failed
  113. #[error("the BPF_BTF_LOAD syscall failed. Verifier output: {verifier_log}")]
  114. LoadError {
  115. /// The [`std::io::Error`] returned by the `BPF_BTF_LOAD` syscall.
  116. #[source]
  117. io_error: std::io::Error,
  118. /// The error log produced by the kernel verifier.
  119. verifier_log: String,
  120. },
  121. /// offset not found for symbol
  122. #[error("Offset not found for symbol `{symbol_name}`")]
  123. SymbolOffsetNotFound {
  124. /// name of the symbol
  125. symbol_name: String,
  126. },
  127. /// btf type that is not VAR found in DATASEC
  128. #[error("BTF type that is not VAR was found in DATASEC")]
  129. InvalidDatasec,
  130. /// unable to determine the size of section
  131. #[error("Unable to determine the size of section `{section_name}`")]
  132. UnknownSectionSize {
  133. /// name of the section
  134. section_name: String,
  135. },
  136. /// unable to get symbol name
  137. #[error("Unable to get symbol name")]
  138. InvalidSymbolName,
  139. }
  140. /// Available BTF features
  141. #[derive(Default, Debug)]
  142. #[allow(missing_docs)]
  143. pub struct BtfFeatures {
  144. pub btf_func: bool,
  145. pub btf_func_global: bool,
  146. pub btf_datasec: bool,
  147. pub btf_float: bool,
  148. pub btf_decl_tag: bool,
  149. pub btf_type_tag: bool,
  150. }
  151. impl std::fmt::Display for BtfFeatures {
  152. fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
  153. f.write_fmt(format_args!(
  154. "[FEAT PROBE] BTF func support: {}\n\
  155. [FEAT PROBE] BTF global func support: {}\n\
  156. [FEAT PROBE] BTF var and datasec support: {}\n\
  157. [FEAT PROBE] BTF float support: {}\n\
  158. [FEAT PROBE] BTF decl_tag support: {}\n\
  159. [FEAT PROBE] BTF type_tag support: {}",
  160. self.btf_func,
  161. self.btf_func_global,
  162. self.btf_datasec,
  163. self.btf_float,
  164. self.btf_decl_tag,
  165. self.btf_type_tag,
  166. ))
  167. }
  168. }
  169. /// Bpf Type Format metadata.
  170. ///
  171. /// BTF is a kind of debug metadata that allows eBPF programs compiled against one kernel version
  172. /// to be loaded into different kernel versions.
  173. ///
  174. /// Aya automatically loads BTF metadata if you use `Bpf::load_file`. You
  175. /// only need to explicitly use this type if you want to load BTF from a non-standard
  176. /// location or if you are using `Bpf::load`.
  177. #[derive(Clone, Debug)]
  178. pub struct Btf {
  179. header: btf_header,
  180. strings: Vec<u8>,
  181. types: BtfTypes,
  182. _endianness: Endianness,
  183. }
  184. impl Btf {
  185. /// Creates a new empty instance with its header initialized
  186. pub fn new() -> Btf {
  187. Btf {
  188. header: btf_header {
  189. magic: 0xeb9f,
  190. version: 0x01,
  191. flags: 0x00,
  192. hdr_len: 0x18,
  193. type_off: 0x00,
  194. type_len: 0x00,
  195. str_off: 0x00,
  196. str_len: 0x00,
  197. },
  198. strings: vec![0],
  199. types: BtfTypes::default(),
  200. _endianness: Endianness::default(),
  201. }
  202. }
  203. pub(crate) fn types(&self) -> impl Iterator<Item = &BtfType> {
  204. self.types.types.iter()
  205. }
  206. /// Adds a string to BTF metadata, returning an offset
  207. pub fn add_string(&mut self, name: String) -> u32 {
  208. let str = CString::new(name).unwrap();
  209. let name_offset = self.strings.len();
  210. self.strings.extend(str.as_c_str().to_bytes_with_nul());
  211. self.header.str_len = self.strings.len() as u32;
  212. name_offset as u32
  213. }
  214. /// Adds a type to BTF metadata, returning a type id
  215. pub fn add_type(&mut self, btf_type: BtfType) -> u32 {
  216. let size = btf_type.type_info_size() as u32;
  217. let type_id = self.types.len();
  218. self.types.push(btf_type);
  219. self.header.type_len += size;
  220. self.header.str_off += size;
  221. type_id as u32
  222. }
  223. /// Loads BTF metadata from `/sys/kernel/btf/vmlinux`.
  224. #[cfg(not(feature = "no_std"))]
  225. pub fn from_sys_fs() -> Result<Btf, BtfError> {
  226. Btf::parse_file("/sys/kernel/btf/vmlinux", Endianness::default())
  227. }
  228. /// Loads BTF metadata from the given `path`.
  229. #[cfg(not(feature = "no_std"))]
  230. pub fn parse_file<P: AsRef<std::path::Path>>(
  231. path: P,
  232. endianness: Endianness,
  233. ) -> Result<Btf, BtfError> {
  234. use std::{borrow::ToOwned, fs};
  235. let path = path.as_ref();
  236. Btf::parse(
  237. &fs::read(path).map_err(|error| BtfError::FileError {
  238. path: path.to_owned(),
  239. error,
  240. })?,
  241. endianness,
  242. )
  243. }
  244. /// Parses BTF from binary data of the given endianness
  245. pub fn parse(data: &[u8], endianness: Endianness) -> Result<Btf, BtfError> {
  246. if data.len() < mem::size_of::<btf_header>() {
  247. return Err(BtfError::InvalidHeader);
  248. }
  249. // safety: btf_header is POD so read_unaligned is safe
  250. let header = unsafe { read_btf_header(data) };
  251. let str_off = header.hdr_len as usize + header.str_off as usize;
  252. let str_len = header.str_len as usize;
  253. if str_off + str_len > data.len() {
  254. return Err(BtfError::InvalidHeader);
  255. }
  256. let strings = data[str_off..str_off + str_len].to_vec();
  257. let types = Btf::read_type_info(&header, data, endianness)?;
  258. Ok(Btf {
  259. header,
  260. strings,
  261. types,
  262. _endianness: endianness,
  263. })
  264. }
  265. fn read_type_info(
  266. header: &btf_header,
  267. data: &[u8],
  268. endianness: Endianness,
  269. ) -> Result<BtfTypes, BtfError> {
  270. let hdr_len = header.hdr_len as usize;
  271. let type_off = header.type_off as usize;
  272. let type_len = header.type_len as usize;
  273. let base = hdr_len + type_off;
  274. if base + type_len > data.len() {
  275. return Err(BtfError::InvalidTypeInfo);
  276. }
  277. let mut data = &data[base..base + type_len];
  278. let mut types = BtfTypes::default();
  279. while !data.is_empty() {
  280. // Safety:
  281. // read() reads POD values from ELF, which is sound, but the values can still contain
  282. // internally inconsistent values (like out of bound offsets and such).
  283. let ty = unsafe { BtfType::read(data, endianness)? };
  284. data = &data[ty.type_info_size()..];
  285. types.push(ty);
  286. }
  287. Ok(types)
  288. }
  289. pub(crate) fn string_at(&self, offset: u32) -> Result<Cow<'_, str>, BtfError> {
  290. let btf_header {
  291. hdr_len,
  292. mut str_off,
  293. str_len,
  294. ..
  295. } = self.header;
  296. str_off += hdr_len;
  297. if offset >= str_off + str_len {
  298. return Err(BtfError::InvalidStringOffset {
  299. offset: offset as usize,
  300. });
  301. }
  302. let offset = offset as usize;
  303. let nul = self.strings[offset..]
  304. .iter()
  305. .position(|c| *c == 0u8)
  306. .ok_or(BtfError::InvalidStringOffset { offset })?;
  307. let s = CStr::from_bytes_with_nul(&self.strings[offset..=offset + nul])
  308. .map_err(|_| BtfError::InvalidStringOffset { offset })?;
  309. Ok(s.to_string_lossy())
  310. }
  311. pub(crate) fn type_by_id(&self, type_id: u32) -> Result<&BtfType, BtfError> {
  312. self.types.type_by_id(type_id)
  313. }
  314. pub(crate) fn resolve_type(&self, root_type_id: u32) -> Result<u32, BtfError> {
  315. self.types.resolve_type(root_type_id)
  316. }
  317. pub(crate) fn type_name(&self, ty: &BtfType) -> Result<Cow<'_, str>, BtfError> {
  318. self.string_at(ty.name_offset())
  319. }
  320. pub(crate) fn err_type_name(&self, ty: &BtfType) -> Option<String> {
  321. self.string_at(ty.name_offset()).ok().map(String::from)
  322. }
  323. /// Returns a type id matching the type name and [BtfKind]
  324. pub fn id_by_type_name_kind(&self, name: &str, kind: BtfKind) -> Result<u32, BtfError> {
  325. for (type_id, ty) in self.types().enumerate() {
  326. if ty.kind() != kind {
  327. continue;
  328. }
  329. if self.type_name(ty)? == name {
  330. return Ok(type_id as u32);
  331. }
  332. continue;
  333. }
  334. Err(BtfError::UnknownBtfTypeName {
  335. type_name: name.to_string(),
  336. })
  337. }
  338. pub(crate) fn type_size(&self, root_type_id: u32) -> Result<usize, BtfError> {
  339. let mut type_id = root_type_id;
  340. let mut n_elems = 1;
  341. for _ in 0..MAX_RESOLVE_DEPTH {
  342. let ty = self.types.type_by_id(type_id)?;
  343. let size = match ty {
  344. BtfType::Array(Array { array, .. }) => {
  345. n_elems = array.len;
  346. type_id = array.element_type;
  347. continue;
  348. }
  349. other => {
  350. if let Some(size) = other.size() {
  351. size
  352. } else if let Some(next) = other.btf_type() {
  353. type_id = next;
  354. continue;
  355. } else {
  356. return Err(BtfError::UnexpectedBtfType { type_id });
  357. }
  358. }
  359. };
  360. return Ok((size * n_elems) as usize);
  361. }
  362. Err(BtfError::MaximumTypeDepthReached {
  363. type_id: root_type_id,
  364. })
  365. }
  366. /// Encodes the metadata as BTF format
  367. pub fn to_bytes(&self) -> Vec<u8> {
  368. // Safety: btf_header is POD
  369. let mut buf = unsafe { bytes_of::<btf_header>(&self.header).to_vec() };
  370. // Skip the first type since it's always BtfType::Unknown for type_by_id to work
  371. buf.extend(self.types.to_bytes());
  372. buf.put(self.strings.as_slice());
  373. buf
  374. }
  375. pub(crate) fn fixup_and_sanitize(
  376. &mut self,
  377. section_sizes: &HashMap<String, u64>,
  378. symbol_offsets: &HashMap<String, u64>,
  379. features: &BtfFeatures,
  380. ) -> Result<(), BtfError> {
  381. let mut types = mem::take(&mut self.types);
  382. for i in 0..types.types.len() {
  383. let t = &types.types[i];
  384. let kind = t.kind();
  385. match t {
  386. // Fixup PTR for Rust
  387. // LLVM emits names for Rust pointer types, which the kernel doesn't like
  388. // While I figure out if this needs fixing in the Kernel or LLVM, we'll
  389. // do a fixup here
  390. BtfType::Ptr(ptr) => {
  391. let mut fixed_ty = ptr.clone();
  392. fixed_ty.name_offset = 0;
  393. types.types[i] = BtfType::Ptr(fixed_ty)
  394. }
  395. // Sanitize VAR if they are not supported
  396. BtfType::Var(v) if !features.btf_datasec => {
  397. types.types[i] = BtfType::Int(Int::new(v.name_offset, 1, IntEncoding::None, 0));
  398. }
  399. // Sanitize DATASEC if they are not supported
  400. BtfType::DataSec(d) if !features.btf_datasec => {
  401. debug!("{}: not supported. replacing with STRUCT", kind);
  402. let mut members = vec![];
  403. for member in d.entries.iter() {
  404. let mt = types.type_by_id(member.btf_type).unwrap();
  405. members.push(BtfMember {
  406. name_offset: mt.name_offset(),
  407. btf_type: member.btf_type,
  408. offset: member.offset * 8,
  409. })
  410. }
  411. types.types[i] = BtfType::Struct(Struct::new(t.name_offset(), members, 0));
  412. }
  413. // Fixup DATASEC
  414. // DATASEC sizes aren't always set by LLVM
  415. // we need to fix them here before loading the btf to the kernel
  416. BtfType::DataSec(d) if features.btf_datasec => {
  417. // Start DataSec Fixups
  418. let sec_name = self.string_at(d.name_offset)?;
  419. let name = sec_name.to_string();
  420. let mut fixed_ty = d.clone();
  421. // Handle any "/" characters in section names
  422. // Example: "maps/hashmap"
  423. let fixed_name = name.replace('/', ".");
  424. if fixed_name != name {
  425. fixed_ty.name_offset = self.add_string(fixed_name);
  426. }
  427. // There are some cases when the compiler does indeed populate the
  428. // size
  429. if t.size().unwrap() > 0 {
  430. debug!("{} {}: size fixup not required", kind, name);
  431. } else {
  432. // We need to get the size of the section from the ELF file
  433. // Fortunately, we cached these when parsing it initially
  434. // and we can this up by name in section_sizes
  435. let size = section_sizes.get(&name).ok_or_else(|| {
  436. BtfError::UnknownSectionSize {
  437. section_name: name.clone(),
  438. }
  439. })?;
  440. debug!("{} {}: fixup size to {}", kind, name, size);
  441. fixed_ty.size = *size as u32;
  442. // The Vec<btf_var_secinfo> contains BTF_KIND_VAR sections
  443. // that need to have their offsets adjusted. To do this,
  444. // we need to get the offset from the ELF file.
  445. // This was also cached during initial parsing and
  446. // we can query by name in symbol_offsets
  447. for d in &mut fixed_ty.entries.iter_mut() {
  448. let var_type = types.type_by_id(d.btf_type)?;
  449. let var_kind = var_type.kind();
  450. if let BtfType::Var(var) = var_type {
  451. let var_name = self.string_at(var.name_offset)?.to_string();
  452. if var.linkage == VarLinkage::Static {
  453. debug!(
  454. "{} {}: {} {}: fixup not required",
  455. kind, name, var_kind, var_name
  456. );
  457. continue;
  458. }
  459. let offset = symbol_offsets.get(&var_name).ok_or(
  460. BtfError::SymbolOffsetNotFound {
  461. symbol_name: var_name.clone(),
  462. },
  463. )?;
  464. d.offset = *offset as u32;
  465. debug!(
  466. "{} {}: {} {}: fixup offset {}",
  467. kind, name, var_kind, var_name, offset
  468. );
  469. } else {
  470. return Err(BtfError::InvalidDatasec);
  471. }
  472. }
  473. }
  474. types.types[i] = BtfType::DataSec(fixed_ty);
  475. }
  476. // Fixup FUNC_PROTO
  477. BtfType::FuncProto(ty) if features.btf_func => {
  478. let mut ty = ty.clone();
  479. for (i, mut param) in ty.params.iter_mut().enumerate() {
  480. if param.name_offset == 0 && param.btf_type != 0 {
  481. param.name_offset = self.add_string(format!("param{i}"));
  482. }
  483. }
  484. types.types[i] = BtfType::FuncProto(ty);
  485. }
  486. // Sanitize FUNC_PROTO
  487. BtfType::FuncProto(ty) if !features.btf_func => {
  488. debug!("{}: not supported. replacing with ENUM", kind);
  489. let members: Vec<BtfEnum> = ty
  490. .params
  491. .iter()
  492. .map(|p| BtfEnum {
  493. name_offset: p.name_offset,
  494. value: p.btf_type,
  495. })
  496. .collect();
  497. let enum_type = BtfType::Enum(Enum::new(ty.name_offset, members));
  498. types.types[i] = enum_type;
  499. }
  500. // Sanitize FUNC
  501. BtfType::Func(ty) if !features.btf_func => {
  502. debug!("{}: not supported. replacing with TYPEDEF", kind);
  503. let typedef_type = BtfType::Typedef(Typedef::new(ty.name_offset, ty.btf_type));
  504. types.types[i] = typedef_type;
  505. }
  506. // Sanitize BTF_FUNC_GLOBAL
  507. BtfType::Func(ty) if !features.btf_func_global => {
  508. let mut fixed_ty = ty.clone();
  509. if ty.linkage() == FuncLinkage::Global {
  510. debug!(
  511. "{}: BTF_FUNC_GLOBAL not supported. replacing with BTF_FUNC_STATIC",
  512. kind
  513. );
  514. fixed_ty.set_linkage(FuncLinkage::Static);
  515. }
  516. types.types[i] = BtfType::Func(fixed_ty);
  517. }
  518. // Sanitize FLOAT
  519. BtfType::Float(ty) if !features.btf_float => {
  520. debug!("{}: not supported. replacing with STRUCT", kind);
  521. let struct_ty = BtfType::Struct(Struct::new(0, vec![], ty.size));
  522. types.types[i] = struct_ty;
  523. }
  524. // Sanitize DECL_TAG
  525. BtfType::DeclTag(ty) if !features.btf_decl_tag => {
  526. debug!("{}: not supported. replacing with INT", kind);
  527. let int_type = BtfType::Int(Int::new(ty.name_offset, 1, IntEncoding::None, 0));
  528. types.types[i] = int_type;
  529. }
  530. // Sanitize TYPE_TAG
  531. BtfType::TypeTag(ty) if !features.btf_type_tag => {
  532. debug!("{}: not supported. replacing with CONST", kind);
  533. let const_type = BtfType::Const(Const::new(ty.btf_type));
  534. types.types[i] = const_type;
  535. }
  536. // The type does not need fixing up or sanitization
  537. _ => {}
  538. }
  539. }
  540. self.types = types;
  541. Ok(())
  542. }
  543. }
  544. impl Default for Btf {
  545. fn default() -> Self {
  546. Self::new()
  547. }
  548. }
  549. impl Object {
  550. /// Fixes up and sanitizes BTF data.
  551. ///
  552. /// Mostly, it removes unsupported types and works around LLVM behaviours.
  553. pub fn fixup_and_sanitize_btf(
  554. &mut self,
  555. features: &BtfFeatures,
  556. ) -> Result<Option<&Btf>, BtfError> {
  557. if let Some(ref mut obj_btf) = self.btf {
  558. // fixup btf
  559. obj_btf.fixup_and_sanitize(
  560. &self.section_sizes,
  561. &self.symbol_offset_by_name,
  562. features,
  563. )?;
  564. Ok(Some(obj_btf))
  565. } else {
  566. Ok(None)
  567. }
  568. }
  569. }
  570. unsafe fn read_btf_header(data: &[u8]) -> btf_header {
  571. // safety: btf_header is POD so read_unaligned is safe
  572. ptr::read_unaligned(data.as_ptr() as *const btf_header)
  573. }
  574. /// Data in the `.BTF.ext` section
  575. #[derive(Debug, Clone)]
  576. pub struct BtfExt {
  577. data: Vec<u8>,
  578. _endianness: Endianness,
  579. relocations: Vec<(u32, Vec<Relocation>)>,
  580. header: btf_ext_header,
  581. func_info_rec_size: usize,
  582. pub(crate) func_info: FuncInfo,
  583. line_info_rec_size: usize,
  584. pub(crate) line_info: LineInfo,
  585. core_relo_rec_size: usize,
  586. }
  587. impl BtfExt {
  588. pub(crate) fn parse(
  589. data: &[u8],
  590. endianness: Endianness,
  591. btf: &Btf,
  592. ) -> Result<BtfExt, BtfError> {
  593. // Safety: btf_ext_header is POD so read_unaligned is safe
  594. let header = unsafe {
  595. ptr::read_unaligned::<btf_ext_header>(data.as_ptr() as *const btf_ext_header)
  596. };
  597. let rec_size = |offset, len| {
  598. let offset = mem::size_of::<btf_ext_header>() + offset as usize;
  599. let len = len as usize;
  600. // check that there's at least enough space for the `rec_size` field
  601. if (len > 0 && len < 4) || offset + len > data.len() {
  602. return Err(BtfError::InvalidInfo {
  603. offset,
  604. len,
  605. section_len: data.len(),
  606. });
  607. }
  608. let read_u32 = if endianness == Endianness::Little {
  609. u32::from_le_bytes
  610. } else {
  611. u32::from_be_bytes
  612. };
  613. Ok(if len > 0 {
  614. read_u32(data[offset..offset + 4].try_into().unwrap()) as usize
  615. } else {
  616. 0
  617. })
  618. };
  619. let btf_ext_header {
  620. func_info_off,
  621. func_info_len,
  622. line_info_off,
  623. line_info_len,
  624. core_relo_off,
  625. core_relo_len,
  626. ..
  627. } = header;
  628. let mut ext = BtfExt {
  629. header,
  630. relocations: Vec::new(),
  631. func_info: FuncInfo::new(),
  632. line_info: LineInfo::new(),
  633. func_info_rec_size: rec_size(func_info_off, func_info_len)?,
  634. line_info_rec_size: rec_size(line_info_off, line_info_len)?,
  635. core_relo_rec_size: rec_size(core_relo_off, core_relo_len)?,
  636. data: data.to_vec(),
  637. _endianness: endianness,
  638. };
  639. let func_info_rec_size = ext.func_info_rec_size;
  640. ext.func_info.data.extend(
  641. SecInfoIter::new(ext.func_info_data(), ext.func_info_rec_size, endianness)
  642. .map(move |sec| {
  643. let name = btf
  644. .string_at(sec.name_offset)
  645. .ok()
  646. .map(String::from)
  647. .unwrap();
  648. let info = FuncSecInfo::parse(
  649. sec.name_offset,
  650. sec.num_info,
  651. func_info_rec_size,
  652. sec.data,
  653. endianness,
  654. );
  655. Ok((name, info))
  656. })
  657. .collect::<Result<HashMap<_, _>, _>>()?,
  658. );
  659. let line_info_rec_size = ext.line_info_rec_size;
  660. ext.line_info.data.extend(
  661. SecInfoIter::new(ext.line_info_data(), ext.line_info_rec_size, endianness)
  662. .map(move |sec| {
  663. let name = btf
  664. .string_at(sec.name_offset)
  665. .ok()
  666. .map(String::from)
  667. .unwrap();
  668. let info = LineSecInfo::parse(
  669. sec.name_offset,
  670. sec.num_info,
  671. line_info_rec_size,
  672. sec.data,
  673. endianness,
  674. );
  675. Ok((name, info))
  676. })
  677. .collect::<Result<HashMap<_, _>, _>>()?,
  678. );
  679. let rec_size = ext.core_relo_rec_size;
  680. ext.relocations.extend(
  681. SecInfoIter::new(ext.core_relo_data(), ext.core_relo_rec_size, endianness)
  682. .map(move |sec| {
  683. let relos = sec
  684. .data
  685. .chunks(rec_size)
  686. .enumerate()
  687. .map(|(n, rec)| unsafe { Relocation::parse(rec, n) })
  688. .collect::<Result<Vec<_>, _>>()?;
  689. Ok((sec.name_offset, relos))
  690. })
  691. .collect::<Result<Vec<_>, _>>()?,
  692. );
  693. Ok(ext)
  694. }
  695. fn info_data(&self, offset: u32, len: u32) -> &[u8] {
  696. let offset = (self.header.hdr_len + offset) as usize;
  697. let data = &self.data[offset..offset + len as usize];
  698. if len > 0 {
  699. // skip `rec_size`
  700. &data[4..]
  701. } else {
  702. data
  703. }
  704. }
  705. fn core_relo_data(&self) -> &[u8] {
  706. self.info_data(self.header.core_relo_off, self.header.core_relo_len)
  707. }
  708. fn func_info_data(&self) -> &[u8] {
  709. self.info_data(self.header.func_info_off, self.header.func_info_len)
  710. }
  711. fn line_info_data(&self) -> &[u8] {
  712. self.info_data(self.header.line_info_off, self.header.line_info_len)
  713. }
  714. pub(crate) fn relocations(&self) -> impl Iterator<Item = &(u32, Vec<Relocation>)> {
  715. self.relocations.iter()
  716. }
  717. pub(crate) fn func_info_rec_size(&self) -> usize {
  718. self.func_info_rec_size
  719. }
  720. pub(crate) fn line_info_rec_size(&self) -> usize {
  721. self.line_info_rec_size
  722. }
  723. }
  724. pub(crate) struct SecInfoIter<'a> {
  725. data: &'a [u8],
  726. offset: usize,
  727. rec_size: usize,
  728. endianness: Endianness,
  729. }
  730. impl<'a> SecInfoIter<'a> {
  731. fn new(data: &'a [u8], rec_size: usize, endianness: Endianness) -> Self {
  732. Self {
  733. data,
  734. rec_size,
  735. offset: 0,
  736. endianness,
  737. }
  738. }
  739. }
  740. impl<'a> Iterator for SecInfoIter<'a> {
  741. type Item = SecInfo<'a>;
  742. fn next(&mut self) -> Option<Self::Item> {
  743. let data = self.data;
  744. if self.offset + 8 >= data.len() {
  745. return None;
  746. }
  747. let read_u32 = if self.endianness == Endianness::Little {
  748. u32::from_le_bytes
  749. } else {
  750. u32::from_be_bytes
  751. };
  752. let name_offset = read_u32(data[self.offset..self.offset + 4].try_into().unwrap());
  753. self.offset += 4;
  754. let num_info = u32::from_ne_bytes(data[self.offset..self.offset + 4].try_into().unwrap());
  755. self.offset += 4;
  756. let data = &data[self.offset..self.offset + (self.rec_size * num_info as usize)];
  757. self.offset += self.rec_size * num_info as usize;
  758. Some(SecInfo {
  759. name_offset,
  760. num_info,
  761. data,
  762. })
  763. }
  764. }
  765. /// BtfTypes allows for access and manipulation of a
  766. /// collection of BtfType objects
  767. #[derive(Debug, Clone)]
  768. pub(crate) struct BtfTypes {
  769. pub(crate) types: Vec<BtfType>,
  770. }
  771. impl Default for BtfTypes {
  772. fn default() -> Self {
  773. Self {
  774. types: vec![BtfType::Unknown],
  775. }
  776. }
  777. }
  778. impl BtfTypes {
  779. pub(crate) fn to_bytes(&self) -> Vec<u8> {
  780. let mut buf = vec![];
  781. for t in self.types.iter().skip(1) {
  782. let b = t.to_bytes();
  783. buf.extend(b)
  784. }
  785. buf
  786. }
  787. pub(crate) fn len(&self) -> usize {
  788. self.types.len()
  789. }
  790. pub(crate) fn push(&mut self, value: BtfType) {
  791. self.types.push(value)
  792. }
  793. pub(crate) fn type_by_id(&self, type_id: u32) -> Result<&BtfType, BtfError> {
  794. self.types
  795. .get(type_id as usize)
  796. .ok_or(BtfError::UnknownBtfType { type_id })
  797. }
  798. pub(crate) fn resolve_type(&self, root_type_id: u32) -> Result<u32, BtfError> {
  799. let mut type_id = root_type_id;
  800. for _ in 0..MAX_RESOLVE_DEPTH {
  801. let ty = self.type_by_id(type_id)?;
  802. use BtfType::*;
  803. match ty {
  804. Volatile(ty) => {
  805. type_id = ty.btf_type;
  806. continue;
  807. }
  808. Const(ty) => {
  809. type_id = ty.btf_type;
  810. continue;
  811. }
  812. Restrict(ty) => {
  813. type_id = ty.btf_type;
  814. continue;
  815. }
  816. Typedef(ty) => {
  817. type_id = ty.btf_type;
  818. continue;
  819. }
  820. TypeTag(ty) => {
  821. type_id = ty.btf_type;
  822. continue;
  823. }
  824. _ => return Ok(type_id),
  825. }
  826. }
  827. Err(BtfError::MaximumTypeDepthReached {
  828. type_id: root_type_id,
  829. })
  830. }
  831. }
  832. #[derive(Debug)]
  833. pub(crate) struct SecInfo<'a> {
  834. name_offset: u32,
  835. num_info: u32,
  836. data: &'a [u8],
  837. }
  838. #[cfg(test)]
  839. mod tests {
  840. use crate::btf::{
  841. BtfParam, DataSec, DataSecEntry, DeclTag, Float, Func, FuncProto, Ptr, TypeTag, Var,
  842. };
  843. use super::*;
  844. #[test]
  845. fn test_parse_header() {
  846. let data: &[u8] = &[
  847. 0x9f, 0xeb, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x54,
  848. 0x2a, 0x00, 0x64, 0x54, 0x2a, 0x00, 0x10, 0x64, 0x1c, 0x00,
  849. ];
  850. let header = unsafe { read_btf_header(data) };
  851. assert_eq!(header.magic, 0xeb9f);
  852. assert_eq!(header.version, 0x01);
  853. assert_eq!(header.flags, 0x00);
  854. assert_eq!(header.hdr_len, 0x18);
  855. assert_eq!(header.type_off, 0x00);
  856. assert_eq!(header.type_len, 0x2a5464);
  857. assert_eq!(header.str_off, 0x2a5464);
  858. assert_eq!(header.str_len, 0x1c6410);
  859. }
  860. #[test]
  861. fn test_parse_btf() {
  862. // this generated BTF data is from an XDP program that simply returns XDP_PASS
  863. // compiled using clang
  864. let data: &[u8] = &[
  865. 0x9f, 0xeb, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x01,
  866. 0x00, 0x00, 0x0c, 0x01, 0x00, 0x00, 0xe1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  867. 0x00, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x00,
  868. 0x00, 0x04, 0x18, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  869. 0x00, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x20, 0x00,
  870. 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  871. 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x30, 0x00,
  872. 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00,
  873. 0x03, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00,
  874. 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  875. 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  876. 0x00, 0x0d, 0x06, 0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  877. 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00,
  878. 0x00, 0x01, 0x69, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0c, 0x05, 0x00, 0x00, 0x00,
  879. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00,
  880. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
  881. 0x08, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xbc, 0x00,
  882. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  883. 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x09, 0x00, 0x00, 0x00, 0x01, 0x00,
  884. 0x00, 0x00, 0xd9, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00,
  885. 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x78,
  886. 0x64, 0x70, 0x5f, 0x6d, 0x64, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x64, 0x61, 0x74,
  887. 0x61, 0x5f, 0x65, 0x6e, 0x64, 0x00, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x6d, 0x65, 0x74,
  888. 0x61, 0x00, 0x69, 0x6e, 0x67, 0x72, 0x65, 0x73, 0x73, 0x5f, 0x69, 0x66, 0x69, 0x6e,
  889. 0x64, 0x65, 0x78, 0x00, 0x72, 0x78, 0x5f, 0x71, 0x75, 0x65, 0x75, 0x65, 0x5f, 0x69,
  890. 0x6e, 0x64, 0x65, 0x78, 0x00, 0x65, 0x67, 0x72, 0x65, 0x73, 0x73, 0x5f, 0x69, 0x66,
  891. 0x69, 0x6e, 0x64, 0x65, 0x78, 0x00, 0x5f, 0x5f, 0x75, 0x33, 0x32, 0x00, 0x75, 0x6e,
  892. 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x20, 0x69, 0x6e, 0x74, 0x00, 0x63, 0x74, 0x78,
  893. 0x00, 0x69, 0x6e, 0x74, 0x00, 0x78, 0x64, 0x70, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x00,
  894. 0x78, 0x64, 0x70, 0x2f, 0x70, 0x61, 0x73, 0x73, 0x00, 0x2f, 0x68, 0x6f, 0x6d, 0x65,
  895. 0x2f, 0x64, 0x61, 0x76, 0x65, 0x2f, 0x64, 0x65, 0x76, 0x2f, 0x62, 0x70, 0x66, 0x64,
  896. 0x2f, 0x62, 0x70, 0x66, 0x2f, 0x78, 0x64, 0x70, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x2e,
  897. 0x62, 0x70, 0x66, 0x2e, 0x63, 0x00, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75,
  898. 0x72, 0x6e, 0x20, 0x58, 0x44, 0x50, 0x5f, 0x50, 0x41, 0x53, 0x53, 0x3b, 0x00, 0x63,
  899. 0x68, 0x61, 0x72, 0x00, 0x5f, 0x5f, 0x41, 0x52, 0x52, 0x41, 0x59, 0x5f, 0x53, 0x49,
  900. 0x5a, 0x45, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x5f, 0x00, 0x5f, 0x6c, 0x69, 0x63,
  901. 0x65, 0x6e, 0x73, 0x65, 0x00, 0x6c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x00,
  902. ];
  903. assert_eq!(data.len(), 517);
  904. let got = Btf::parse(data, Endianness::default());
  905. match got {
  906. Ok(_) => {}
  907. Err(e) => panic!("{}", e),
  908. }
  909. let btf = got.unwrap();
  910. let data2 = btf.to_bytes();
  911. assert_eq!(data2.len(), 517);
  912. assert_eq!(data, data2);
  913. let ext_data: &[u8] = &[
  914. 0x9f, 0xeb, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00,
  915. 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
  916. 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x01, 0x00,
  917. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  918. 0x72, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0x00,
  919. 0x00, 0x00, 0xa2, 0x00, 0x00, 0x00, 0x05, 0x2c, 0x00, 0x00,
  920. ];
  921. assert_eq!(ext_data.len(), 80);
  922. let got = BtfExt::parse(ext_data, Endianness::default(), &btf);
  923. if let Err(e) = got {
  924. panic!("{}", e)
  925. }
  926. }
  927. #[test]
  928. fn test_write_btf() {
  929. let mut btf = Btf::new();
  930. let name_offset = btf.add_string("int".to_string());
  931. let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0));
  932. btf.add_type(int_type);
  933. let name_offset = btf.add_string("widget".to_string());
  934. let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0));
  935. btf.add_type(int_type);
  936. let btf_bytes = btf.to_bytes();
  937. let raw_btf = btf_bytes.as_slice();
  938. let parsed = Btf::parse(raw_btf, Endianness::default());
  939. match parsed {
  940. Ok(btf) => {
  941. assert_eq!(btf.string_at(1).unwrap(), "int");
  942. assert_eq!(btf.string_at(5).unwrap(), "widget");
  943. }
  944. Err(e) => {
  945. panic!("{}", e)
  946. }
  947. }
  948. }
  949. #[test]
  950. fn test_fixup_ptr() {
  951. let mut btf = Btf::new();
  952. let name_offset = btf.add_string("int".to_string());
  953. let int_type_id = btf.add_type(BtfType::Int(Int::new(
  954. name_offset,
  955. 4,
  956. IntEncoding::Signed,
  957. 0,
  958. )));
  959. let name_offset = btf.add_string("&mut int".to_string());
  960. let ptr_type_id = btf.add_type(BtfType::Ptr(Ptr::new(name_offset, int_type_id)));
  961. let features = BtfFeatures {
  962. ..Default::default()
  963. };
  964. btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features)
  965. .unwrap();
  966. if let BtfType::Ptr(fixed) = btf.type_by_id(ptr_type_id).unwrap() {
  967. assert!(
  968. fixed.name_offset == 0,
  969. "expected offset 0, got {}",
  970. fixed.name_offset
  971. )
  972. } else {
  973. panic!("not a ptr")
  974. }
  975. // Ensure we can convert to bytes and back again
  976. let raw = btf.to_bytes();
  977. Btf::parse(&raw, Endianness::default()).unwrap();
  978. }
  979. #[test]
  980. fn test_sanitize_var() {
  981. let mut btf = Btf::new();
  982. let name_offset = btf.add_string("int".to_string());
  983. let int_type_id = btf.add_type(BtfType::Int(Int::new(
  984. name_offset,
  985. 4,
  986. IntEncoding::Signed,
  987. 0,
  988. )));
  989. let name_offset = btf.add_string("&mut int".to_string());
  990. let var_type_id = btf.add_type(BtfType::Var(Var::new(
  991. name_offset,
  992. int_type_id,
  993. VarLinkage::Static,
  994. )));
  995. let features = BtfFeatures {
  996. btf_datasec: false,
  997. ..Default::default()
  998. };
  999. btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features)
  1000. .unwrap();
  1001. if let BtfType::Int(fixed) = btf.type_by_id(var_type_id).unwrap() {
  1002. assert!(fixed.name_offset == name_offset)
  1003. } else {
  1004. panic!("not an int")
  1005. }
  1006. // Ensure we can convert to bytes and back again
  1007. let raw = btf.to_bytes();
  1008. Btf::parse(&raw, Endianness::default()).unwrap();
  1009. }
  1010. #[test]
  1011. fn test_sanitize_datasec() {
  1012. let mut btf = Btf::new();
  1013. let name_offset = btf.add_string("int".to_string());
  1014. let int_type_id = btf.add_type(BtfType::Int(Int::new(
  1015. name_offset,
  1016. 4,
  1017. IntEncoding::Signed,
  1018. 0,
  1019. )));
  1020. let name_offset = btf.add_string("foo".to_string());
  1021. let var_type_id = btf.add_type(BtfType::Var(Var::new(
  1022. name_offset,
  1023. int_type_id,
  1024. VarLinkage::Static,
  1025. )));
  1026. let name_offset = btf.add_string(".data".to_string());
  1027. let variables = vec![DataSecEntry {
  1028. btf_type: var_type_id,
  1029. offset: 0,
  1030. size: 4,
  1031. }];
  1032. let datasec_type_id =
  1033. btf.add_type(BtfType::DataSec(DataSec::new(name_offset, variables, 0)));
  1034. let features = BtfFeatures {
  1035. btf_datasec: false,
  1036. ..Default::default()
  1037. };
  1038. btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features)
  1039. .unwrap();
  1040. if let BtfType::Struct(fixed) = btf.type_by_id(datasec_type_id).unwrap() {
  1041. assert!(fixed.name_offset == name_offset);
  1042. assert!(fixed.members.len() == 1);
  1043. assert!(fixed.members[0].btf_type == var_type_id);
  1044. assert!(fixed.members[0].offset == 0)
  1045. } else {
  1046. panic!("not a struct")
  1047. }
  1048. // Ensure we can convert to bytes and back again
  1049. let raw = btf.to_bytes();
  1050. Btf::parse(&raw, Endianness::default()).unwrap();
  1051. }
  1052. #[test]
  1053. fn test_fixup_datasec() {
  1054. let mut btf = Btf::new();
  1055. let name_offset = btf.add_string("int".to_string());
  1056. let int_type_id = btf.add_type(BtfType::Int(Int::new(
  1057. name_offset,
  1058. 4,
  1059. IntEncoding::Signed,
  1060. 0,
  1061. )));
  1062. let name_offset = btf.add_string("foo".to_string());
  1063. let var_type_id = btf.add_type(BtfType::Var(Var::new(
  1064. name_offset,
  1065. int_type_id,
  1066. VarLinkage::Global,
  1067. )));
  1068. let name_offset = btf.add_string(".data/foo".to_string());
  1069. let variables = vec![DataSecEntry {
  1070. btf_type: var_type_id,
  1071. offset: 0,
  1072. size: 4,
  1073. }];
  1074. let datasec_type_id =
  1075. btf.add_type(BtfType::DataSec(DataSec::new(name_offset, variables, 0)));
  1076. let features = BtfFeatures {
  1077. btf_datasec: true,
  1078. ..Default::default()
  1079. };
  1080. btf.fixup_and_sanitize(
  1081. &HashMap::from([(".data/foo".to_string(), 32u64)]),
  1082. &HashMap::from([("foo".to_string(), 64u64)]),
  1083. &features,
  1084. )
  1085. .unwrap();
  1086. if let BtfType::DataSec(fixed) = btf.type_by_id(datasec_type_id).unwrap() {
  1087. assert!(fixed.name_offset != name_offset);
  1088. assert!(fixed.size == 32);
  1089. assert!(fixed.entries.len() == 1);
  1090. assert!(fixed.entries[0].btf_type == var_type_id);
  1091. assert!(
  1092. fixed.entries[0].offset == 64,
  1093. "expected 64, got {}",
  1094. fixed.entries[0].offset
  1095. );
  1096. assert!(btf.string_at(fixed.name_offset).unwrap() == ".data.foo")
  1097. } else {
  1098. panic!("not a datasec")
  1099. }
  1100. // Ensure we can convert to bytes and back again
  1101. let raw = btf.to_bytes();
  1102. Btf::parse(&raw, Endianness::default()).unwrap();
  1103. }
  1104. #[test]
  1105. fn test_sanitize_func_and_proto() {
  1106. let mut btf = Btf::new();
  1107. let name_offset = btf.add_string("int".to_string());
  1108. let int_type_id = btf.add_type(BtfType::Int(Int::new(
  1109. name_offset,
  1110. 4,
  1111. IntEncoding::Signed,
  1112. 0,
  1113. )));
  1114. let params = vec![
  1115. BtfParam {
  1116. name_offset: btf.add_string("a".to_string()),
  1117. btf_type: int_type_id,
  1118. },
  1119. BtfParam {
  1120. name_offset: btf.add_string("b".to_string()),
  1121. btf_type: int_type_id,
  1122. },
  1123. ];
  1124. let func_proto_type_id =
  1125. btf.add_type(BtfType::FuncProto(FuncProto::new(params, int_type_id)));
  1126. let inc = btf.add_string("inc".to_string());
  1127. let func_type_id = btf.add_type(BtfType::Func(Func::new(
  1128. inc,
  1129. func_proto_type_id,
  1130. FuncLinkage::Static,
  1131. )));
  1132. let features = BtfFeatures {
  1133. btf_func: false,
  1134. ..Default::default()
  1135. };
  1136. btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features)
  1137. .unwrap();
  1138. if let BtfType::Enum(fixed) = btf.type_by_id(func_proto_type_id).unwrap() {
  1139. assert!(fixed.name_offset == 0);
  1140. assert!(fixed.variants.len() == 2);
  1141. assert!(btf.string_at(fixed.variants[0].name_offset).unwrap() == "a");
  1142. assert!(fixed.variants[0].value == int_type_id);
  1143. assert!(btf.string_at(fixed.variants[1].name_offset).unwrap() == "b");
  1144. assert!(fixed.variants[1].value == int_type_id);
  1145. } else {
  1146. panic!("not an emum")
  1147. }
  1148. if let BtfType::Typedef(fixed) = btf.type_by_id(func_type_id).unwrap() {
  1149. assert!(fixed.name_offset == inc);
  1150. assert!(fixed.btf_type == func_proto_type_id);
  1151. } else {
  1152. panic!("not a typedef")
  1153. }
  1154. // Ensure we can convert to bytes and back again
  1155. let raw = btf.to_bytes();
  1156. Btf::parse(&raw, Endianness::default()).unwrap();
  1157. }
  1158. #[test]
  1159. fn test_fixup_func_proto() {
  1160. let mut btf = Btf::new();
  1161. let name_offset = btf.add_string("int".to_string());
  1162. let int_type = BtfType::Int(Int::new(name_offset, 4, IntEncoding::Signed, 0));
  1163. let int_type_id = btf.add_type(int_type);
  1164. let params = vec![
  1165. BtfParam {
  1166. name_offset: 0,
  1167. btf_type: int_type_id,
  1168. },
  1169. BtfParam {
  1170. name_offset: 0,
  1171. btf_type: int_type_id,
  1172. },
  1173. ];
  1174. let func_proto = BtfType::FuncProto(FuncProto::new(params, int_type_id));
  1175. let func_proto_type_id = btf.add_type(func_proto);
  1176. let features = BtfFeatures {
  1177. btf_func: true,
  1178. ..Default::default()
  1179. };
  1180. btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features)
  1181. .unwrap();
  1182. if let BtfType::FuncProto(fixed) = btf.type_by_id(func_proto_type_id).unwrap() {
  1183. assert!(btf.string_at(fixed.params[0].name_offset).unwrap() == "param0");
  1184. assert!(btf.string_at(fixed.params[1].name_offset).unwrap() == "param1");
  1185. } else {
  1186. panic!("not a func_proto")
  1187. }
  1188. // Ensure we can convert to bytes and back again
  1189. let raw = btf.to_bytes();
  1190. Btf::parse(&raw, Endianness::default()).unwrap();
  1191. }
  1192. #[test]
  1193. fn test_sanitize_func_global() {
  1194. let mut btf = Btf::new();
  1195. let name_offset = btf.add_string("int".to_string());
  1196. let int_type_id = btf.add_type(BtfType::Int(Int::new(
  1197. name_offset,
  1198. 4,
  1199. IntEncoding::Signed,
  1200. 0,
  1201. )));
  1202. let params = vec![
  1203. BtfParam {
  1204. name_offset: btf.add_string("a".to_string()),
  1205. btf_type: int_type_id,
  1206. },
  1207. BtfParam {
  1208. name_offset: btf.add_string("b".to_string()),
  1209. btf_type: int_type_id,
  1210. },
  1211. ];
  1212. let func_proto_type_id =
  1213. btf.add_type(BtfType::FuncProto(FuncProto::new(params, int_type_id)));
  1214. let inc = btf.add_string("inc".to_string());
  1215. let func_type_id = btf.add_type(BtfType::Func(Func::new(
  1216. inc,
  1217. func_proto_type_id,
  1218. FuncLinkage::Global,
  1219. )));
  1220. let features = BtfFeatures {
  1221. btf_func: true,
  1222. btf_func_global: false,
  1223. ..Default::default()
  1224. };
  1225. btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features)
  1226. .unwrap();
  1227. if let BtfType::Func(fixed) = btf.type_by_id(func_type_id).unwrap() {
  1228. assert!(fixed.linkage() == FuncLinkage::Static);
  1229. } else {
  1230. panic!("not a func")
  1231. }
  1232. // Ensure we can convert to bytes and back again
  1233. let raw = btf.to_bytes();
  1234. Btf::parse(&raw, Endianness::default()).unwrap();
  1235. }
  1236. #[test]
  1237. fn test_sanitize_float() {
  1238. let mut btf = Btf::new();
  1239. let name_offset = btf.add_string("float".to_string());
  1240. let float_type_id = btf.add_type(BtfType::Float(Float::new(name_offset, 16)));
  1241. let features = BtfFeatures {
  1242. btf_float: false,
  1243. ..Default::default()
  1244. };
  1245. btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features)
  1246. .unwrap();
  1247. if let BtfType::Struct(fixed) = btf.type_by_id(float_type_id).unwrap() {
  1248. assert!(fixed.name_offset == 0);
  1249. assert!(fixed.size == 16);
  1250. } else {
  1251. panic!("not a struct")
  1252. }
  1253. // Ensure we can convert to bytes and back again
  1254. let raw = btf.to_bytes();
  1255. Btf::parse(&raw, Endianness::default()).unwrap();
  1256. }
  1257. #[test]
  1258. fn test_sanitize_decl_tag() {
  1259. let mut btf = Btf::new();
  1260. let name_offset = btf.add_string("int".to_string());
  1261. let int_type_id = btf.add_type(BtfType::Int(Int::new(
  1262. name_offset,
  1263. 4,
  1264. IntEncoding::Signed,
  1265. 0,
  1266. )));
  1267. let name_offset = btf.add_string("foo".to_string());
  1268. let var_type_id = btf.add_type(BtfType::Var(Var::new(
  1269. name_offset,
  1270. int_type_id,
  1271. VarLinkage::Static,
  1272. )));
  1273. let name_offset = btf.add_string("decl_tag".to_string());
  1274. let decl_tag_type_id =
  1275. btf.add_type(BtfType::DeclTag(DeclTag::new(name_offset, var_type_id, -1)));
  1276. let features = BtfFeatures {
  1277. btf_decl_tag: false,
  1278. ..Default::default()
  1279. };
  1280. btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features)
  1281. .unwrap();
  1282. if let BtfType::Int(fixed) = btf.type_by_id(decl_tag_type_id).unwrap() {
  1283. assert!(fixed.name_offset == name_offset);
  1284. assert!(fixed.size == 1);
  1285. } else {
  1286. panic!("not an int")
  1287. }
  1288. // Ensure we can convert to bytes and back again
  1289. let raw = btf.to_bytes();
  1290. Btf::parse(&raw, Endianness::default()).unwrap();
  1291. }
  1292. #[test]
  1293. fn test_sanitize_type_tag() {
  1294. let mut btf = Btf::new();
  1295. let int_type_id = btf.add_type(BtfType::Int(Int::new(0, 4, IntEncoding::Signed, 0)));
  1296. let name_offset = btf.add_string("int".to_string());
  1297. let type_tag_type = btf.add_type(BtfType::TypeTag(TypeTag::new(name_offset, int_type_id)));
  1298. btf.add_type(BtfType::Ptr(Ptr::new(0, type_tag_type)));
  1299. let features = BtfFeatures {
  1300. btf_type_tag: false,
  1301. ..Default::default()
  1302. };
  1303. btf.fixup_and_sanitize(&HashMap::new(), &HashMap::new(), &features)
  1304. .unwrap();
  1305. if let BtfType::Const(fixed) = btf.type_by_id(type_tag_type).unwrap() {
  1306. assert!(fixed.btf_type == int_type_id);
  1307. } else {
  1308. panic!("not a const")
  1309. }
  1310. // Ensure we can convert to bytes and back again
  1311. let raw = btf.to_bytes();
  1312. Btf::parse(&raw, Endianness::default()).unwrap();
  1313. }
  1314. #[test]
  1315. #[cfg(not(feature = "no_std"))]
  1316. #[cfg_attr(miri, ignore)]
  1317. fn test_read_btf_from_sys_fs() {
  1318. let btf = Btf::parse_file("/sys/kernel/btf/vmlinux", Endianness::default()).unwrap();
  1319. let task_struct_id = btf
  1320. .id_by_type_name_kind("task_struct", BtfKind::Struct)
  1321. .unwrap();
  1322. // we can't assert on exact ID since this may change across kernel versions
  1323. assert!(task_struct_id != 0);
  1324. let netif_id = btf
  1325. .id_by_type_name_kind("netif_receive_skb", BtfKind::Func)
  1326. .unwrap();
  1327. assert!(netif_id != 0);
  1328. let u32_def = btf.id_by_type_name_kind("__u32", BtfKind::Typedef).unwrap();
  1329. assert!(u32_def != 0);
  1330. let u32_base = btf.resolve_type(u32_def).unwrap();
  1331. assert!(u32_base != 0);
  1332. let u32_ty = btf.type_by_id(u32_base).unwrap();
  1333. assert_eq!(u32_ty.kind(), BtfKind::Int);
  1334. }
  1335. }