diff --git a/aya/src/programs/extension.rs b/aya/src/programs/extension.rs index 00f8b02a..3c389791 100644 --- a/aya/src/programs/extension.rs +++ b/aya/src/programs/extension.rs @@ -159,36 +159,23 @@ fn get_btf_info(prog_fd: i32, func_name: &str) -> Result<(RawFd, u32), ProgramEr } // the bpf fd of the BTF object - let btf_fd = sys::bpf_btf_get_fd_by_id(info.btf_id).map_err(|io_error| SyscallError { - call: "bpf_btf_get_fd_by_id", - io_error, - })?; + let btf_fd = sys::bpf_btf_get_fd_by_id(info.btf_id)?; // we need to read the btf bytes into a buffer but we don't know the size ahead of time. // assume 4kb. if this is too small we can resize based on the size obtained in the response. let mut buf = vec![0u8; 4096]; - let btf_info = match sys::btf_obj_get_info_by_fd(btf_fd, &buf) { - Ok(info) => { - if info.btf_size > buf.len() as u32 { - buf.resize(info.btf_size as usize, 0u8); - let btf_info = - sys::btf_obj_get_info_by_fd(btf_fd, &buf).map_err(|io_error| SyscallError { - call: "bpf_prog_get_info_by_fd", - io_error, - })?; - Ok(btf_info) - } else { - Ok(info) - } + loop { + let info = sys::btf_obj_get_info_by_fd(btf_fd, &mut buf)?; + let btf_size = info.btf_size as usize; + if btf_size > buf.len() { + buf.resize(btf_size, 0u8); + continue; } - Err(io_error) => Err(SyscallError { - call: "bpf_prog_get_info_by_fd", - io_error, - }), - }?; + buf.truncate(btf_size); + break; + } - let btf = Btf::parse(&buf[0..btf_info.btf_size as usize], Endianness::default()) - .map_err(ProgramError::Btf)?; + let btf = Btf::parse(&buf, Endianness::default()).map_err(ProgramError::Btf)?; let btf_id = btf .id_by_type_name_kind(func_name, BtfKind::Func) diff --git a/aya/src/sys/bpf.rs b/aya/src/sys/bpf.rs index db70fe5a..4e3c3602 100644 --- a/aya/src/sys/bpf.rs +++ b/aya/src/sys/bpf.rs @@ -539,22 +539,14 @@ pub(crate) fn bpf_link_get_info_by_fd(fd: BorrowedFd<'_>) -> Result Result { - let mut attr = unsafe { mem::zeroed::() }; - let mut info = unsafe { mem::zeroed::() }; - let buf_size = buf.len() as u32; - info.btf = buf.as_ptr() as u64; - info.btf_size = buf_size; - attr.info.bpf_fd = prog_fd as u32; - attr.info.info = &info as *const bpf_btf_info as u64; - attr.info.info_len = mem::size_of::() as u32; - - match sys_bpf(bpf_cmd::BPF_OBJ_GET_INFO_BY_FD, &mut attr) { - Ok(_) => Ok(info), - Err((_, err)) => Err(err), - } + fd: RawFd, + buf: &mut [u8], +) -> Result { + let fd = unsafe { BorrowedFd::borrow_raw(fd) }; + bpf_obj_get_info_by_fd(fd, |info: &mut bpf_btf_info| { + info.btf = buf.as_mut_ptr() as _; + info.btf_size = buf.len() as _; + }) } pub(crate) fn bpf_raw_tracepoint_open(name: Option<&CStr>, prog_fd: RawFd) -> SysResult {