Skip to content

Commit 52f44ed

Browse files
d-e-s-odanielocfb
authored andcommitted
Rid btf/mod.rs of create_bpf_entity_checked()
Get rid of any traces of create_bpf_entity_checked() usage in btf/mod.rs. Favor more explicit error checking via validate_bpf_ret() (as introduced earlier) and make sure to add proper context. Signed-off-by: Daniel Müller <[email protected]>
1 parent ed1e496 commit 52f44ed

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

libbpf-rs/src/btf/mod.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use std::path::Path;
3737
use std::ptr;
3838
use std::ptr::NonNull;
3939

40-
use crate::util::create_bpf_entity_checked;
4140
use crate::util::parse_ret_i32;
4241
use crate::util::validate_bpf_ret;
4342
use crate::AsRawLibbpf;
@@ -173,9 +172,8 @@ impl Btf<'static> {
173172
let path = CString::new(path.as_os_str().as_bytes()).map_err(|_| {
174173
Error::with_invalid_data(format!("invalid path {path:?}, has null bytes"))
175174
})?;
176-
let ptr = create_bpf_entity_checked(|| unsafe {
177-
libbpf_sys::btf__parse(path.as_ptr(), ptr::null_mut())
178-
})?;
175+
let ptr = unsafe { libbpf_sys::btf__parse(path.as_ptr(), ptr::null_mut()) };
176+
let ptr = validate_bpf_ret(ptr).context("failed to parse BTF information")?;
179177
Ok(Btf {
180178
ptr,
181179
drop_policy: DropPolicy::SelfPtrOnly,
@@ -187,7 +185,8 @@ impl Btf<'static> {
187185

188186
/// Load the vmlinux btf information from few well-known locations.
189187
pub fn from_vmlinux() -> Result<Self> {
190-
let ptr = create_bpf_entity_checked(|| unsafe { libbpf_sys::btf__load_vmlinux_btf() })?;
188+
let ptr = unsafe { libbpf_sys::btf__load_vmlinux_btf() };
189+
let ptr = validate_bpf_ret(ptr).context("failed to load BTF from vmlinux")?;
191190

192191
Ok(Btf {
193192
ptr,
@@ -212,9 +211,8 @@ impl Btf<'static> {
212211
)
213212
})?;
214213

215-
let ptr = create_bpf_entity_checked(|| unsafe {
216-
libbpf_sys::btf__load_from_kernel_by_id(info.btf_id)
217-
})?;
214+
let ptr = unsafe { libbpf_sys::btf__load_from_kernel_by_id(info.btf_id) };
215+
let ptr = validate_bpf_ret(ptr).context("failed to load BTF from kernel")?;
218216

219217
Ok(Self {
220218
ptr,
@@ -261,27 +259,29 @@ impl<'btf> Btf<'btf> {
261259
..Default::default()
262260
};
263261

264-
let mut bpf_obj = create_bpf_entity_checked(|| unsafe {
262+
let ptr = unsafe {
265263
libbpf_sys::bpf_object__open_mem(
266264
object_file.as_ptr() as *const c_void,
267265
object_file.len() as c_ulong,
268266
&obj_opts,
269267
)
270-
})?;
268+
};
271269

270+
let mut bpf_obj = validate_bpf_ret(ptr).context("failed to open BPF object from memory")?;
271+
// SAFETY: The pointer has been validated.
272272
let bpf_obj = unsafe { bpf_obj.as_mut() };
273273
match Self::from_bpf_object_raw(bpf_obj) {
274274
Ok(Some(this)) => Ok(Some(Self {
275275
drop_policy: DropPolicy::ObjPtr(bpf_obj),
276276
..this
277277
})),
278278
x => {
279+
// SAFETY: The obj pointer is valid because we checked
280+
// its validity.
279281
unsafe {
280-
// SAFETY:
281-
// The obj pointer is valid, create_bpf_entity_checked has checked it.
282-
//
283-
// We free it here, otherwise it will be a memory leak as this codepath
284-
// (Ok(None) | Err(e)) does not reference it anymore and as such it can be
282+
// We free it here, otherwise it will be a memory
283+
// leak as this codepath (Ok(None) | Err(e)) does
284+
// not reference it anymore and as such it can be
285285
// dropped.
286286
libbpf_sys::bpf_object__close(bpf_obj)
287287
};

0 commit comments

Comments
 (0)