Skip to content

Commit c983f58

Browse files
d-e-s-odanielocfb
authored andcommitted
Stop using create_bpf_entity_checked() everywhere else
Get rid of all remaining call sites of create_bpf_entity_checked() in the crate. 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 2e08b36 commit c983f58

File tree

6 files changed

+46
-30
lines changed

6 files changed

+46
-30
lines changed

libbpf-rs/src/link.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use std::path::PathBuf;
66
use std::ptr::NonNull;
77

88
use crate::util;
9+
use crate::util::validate_bpf_ret;
910
use crate::AsRawLibbpf;
11+
use crate::ErrorExt as _;
1012
use crate::Program;
1113
use crate::Result;
1214

@@ -33,8 +35,10 @@ impl Link {
3335
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
3436
let path_c = util::path_to_cstring(path)?;
3537
let path_ptr = path_c.as_ptr();
36-
util::create_bpf_entity_checked(|| unsafe { libbpf_sys::bpf_link__open(path_ptr) })
37-
.map(|ptr| unsafe { Self::new(ptr) })
38+
let ptr = unsafe { libbpf_sys::bpf_link__open(path_ptr) };
39+
let ptr = validate_bpf_ret(ptr).context("failed to open link")?;
40+
let slf = unsafe { Self::new(ptr) };
41+
Ok(slf)
3842
}
3943

4044
/// Takes ownership from pointer.

libbpf-rs/src/linker.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::path::Path;
22
use std::ptr::null_mut;
33
use std::ptr::NonNull;
44

5-
use crate::util;
65
use crate::util::path_to_cstring;
6+
use crate::util::validate_bpf_ret;
77
use crate::AsRawLibbpf;
88
use crate::Error;
99
use crate::ErrorExt as _;
@@ -27,12 +27,12 @@ impl Linker {
2727
P: AsRef<Path>,
2828
{
2929
let output = path_to_cstring(output)?;
30-
util::create_bpf_entity_checked(|| {
31-
let opts = null_mut();
32-
// SAFETY: `output` is a valid pointer and `opts` is accepted as NULL.
33-
unsafe { libbpf_sys::bpf_linker__new(output.as_ptr(), opts) }
34-
})
35-
.map(|linker| Self { linker })
30+
let opts = null_mut();
31+
// SAFETY: `output` is a valid pointer and `opts` is accepted as NULL.
32+
let ptr = unsafe { libbpf_sys::bpf_linker__new(output.as_ptr(), opts) };
33+
let ptr = validate_bpf_ret(ptr).context("failed to attach iterator")?;
34+
let slf = Self { linker: ptr };
35+
Ok(slf)
3636
}
3737

3838
/// Add a file to the set of files to link.

libbpf-rs/src/map.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use libbpf_sys::bpf_obj_get_info_by_fd;
2929

3030
use crate::util;
3131
use crate::util::parse_ret_i32;
32+
use crate::util::validate_bpf_ret;
3233
use crate::AsRawLibbpf;
3334
use crate::Error;
3435
use crate::ErrorExt as _;
@@ -791,13 +792,11 @@ impl<'obj> MapMut<'obj> {
791792
)));
792793
}
793794

794-
util::create_bpf_entity_checked(|| unsafe {
795-
libbpf_sys::bpf_map__attach_struct_ops(self.ptr.as_ptr())
796-
})
797-
.map(|ptr| unsafe {
798-
// SAFETY: the pointer came from libbpf and has been checked for errors
799-
Link::new(ptr)
800-
})
795+
let ptr = unsafe { libbpf_sys::bpf_map__attach_struct_ops(self.ptr.as_ptr()) };
796+
let ptr = validate_bpf_ret(ptr).context("failed to attach struct_ops")?;
797+
// SAFETY: the pointer came from libbpf and has been checked for errors.
798+
let link = unsafe { Link::new(ptr) };
799+
Ok(link)
801800
}
802801
}
803802

libbpf-rs/src/object.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use std::ptr::NonNull;
1212
use crate::map::map_fd;
1313
use crate::set_print;
1414
use crate::util;
15+
use crate::util::validate_bpf_ret;
1516
use crate::Btf;
17+
use crate::ErrorExt as _;
1618
use crate::Map;
1719
use crate::MapMut;
1820
use crate::OpenMap;
@@ -171,27 +173,30 @@ impl ObjectBuilder {
171173

172174
/// Open an object using the provided path on the file system.
173175
pub fn open_file<P: AsRef<Path>>(&mut self, path: P) -> Result<OpenObject> {
176+
let path = path.as_ref();
174177
let path_c = util::path_to_cstring(path)?;
175178
let path_ptr = path_c.as_ptr();
176179
let opts_ptr = self.as_libbpf_object().as_ptr();
177180

178-
let ptr = util::create_bpf_entity_checked(|| unsafe {
179-
libbpf_sys::bpf_object__open_file(path_ptr, opts_ptr)
180-
})?;
181+
let ptr = unsafe { libbpf_sys::bpf_object__open_file(path_ptr, opts_ptr) };
182+
let ptr = validate_bpf_ret(ptr)
183+
.with_context(|| format!("failed to open object from `{}`", path.display()))?;
184+
181185
let obj = unsafe { OpenObject::from_ptr(ptr) };
182186
Ok(obj)
183187
}
184188

185189
/// Open an object from memory.
186190
pub fn open_memory(&mut self, mem: &[u8]) -> Result<OpenObject> {
187191
let opts_ptr = self.as_libbpf_object().as_ptr();
188-
let ptr = util::create_bpf_entity_checked(|| unsafe {
192+
let ptr = unsafe {
189193
libbpf_sys::bpf_object__open_mem(
190194
mem.as_ptr() as *const c_void,
191195
mem.len() as libbpf_sys::size_t,
192196
opts_ptr,
193197
)
194-
})?;
198+
};
199+
let ptr = validate_bpf_ret(ptr).context("failed to open object from memory")?;
195200
let obj = unsafe { OpenObject::from_ptr(ptr) };
196201
Ok(obj)
197202
}

libbpf-rs/src/perf_buffer.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ use std::slice;
1010
use std::time::Duration;
1111

1212
use crate::util;
13+
use crate::util::validate_bpf_ret;
1314
use crate::AsRawLibbpf;
1415
use crate::Error;
16+
use crate::ErrorExt as _;
1517
use crate::Map;
1618
use crate::MapCore as _;
1719
use crate::MapType;
@@ -126,7 +128,7 @@ impl<'a, 'b> PerfBufferBuilder<'a, 'b> {
126128
lost_cb: self.lost_cb,
127129
}));
128130

129-
util::create_bpf_entity_checked(|| unsafe {
131+
let ptr = unsafe {
130132
libbpf_sys::perf_buffer__new(
131133
self.map.as_fd().as_raw_fd(),
132134
self.pages as libbpf_sys::size_t,
@@ -135,11 +137,13 @@ impl<'a, 'b> PerfBufferBuilder<'a, 'b> {
135137
callback_struct_ptr as *mut _,
136138
ptr::null(),
137139
)
138-
})
139-
.map(|ptr| PerfBuffer {
140+
};
141+
let ptr = validate_bpf_ret(ptr).context("failed to create perf buffer")?;
142+
let pb = PerfBuffer {
140143
ptr,
141144
_cb_struct: unsafe { Box::from_raw(callback_struct_ptr) },
142-
})
145+
};
146+
Ok(pb)
143147
}
144148

145149
unsafe extern "C" fn call_sample_cb(ctx: *mut c_void, cpu: i32, data: *mut c_void, size: u32) {

libbpf-rs/src/ringbuf.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ use std::slice;
1212
use std::time::Duration;
1313

1414
use crate::util;
15+
use crate::util::validate_bpf_ret;
1516
use crate::AsRawLibbpf;
1617
use crate::Error;
18+
use crate::ErrorExt as _;
1719
use crate::MapCore;
1820
use crate::MapType;
1921
use crate::Result;
@@ -83,22 +85,24 @@ impl<'slf, 'cb: 'slf> RingBufferBuilder<'slf, 'cb> {
8385
/// Build a new [`RingBuffer`]. Must have added at least one ringbuf.
8486
pub fn build(self) -> Result<RingBuffer<'cb>> {
8587
let mut cbs = vec![];
86-
let mut ptr: Option<NonNull<libbpf_sys::ring_buffer>> = None;
88+
let mut rb_ptr: Option<NonNull<libbpf_sys::ring_buffer>> = None;
8789
let c_sample_cb: libbpf_sys::ring_buffer_sample_fn = Some(Self::call_sample_cb);
8890

8991
for (fd, callback) in self.fd_callbacks {
9092
let sample_cb_ptr = Box::into_raw(Box::new(callback));
91-
match ptr {
93+
match rb_ptr {
9294
None => {
9395
// Allocate a new ringbuf manager and add a ringbuf to it
94-
ptr = Some(util::create_bpf_entity_checked(|| unsafe {
96+
let ptr = unsafe {
9597
libbpf_sys::ring_buffer__new(
9698
fd.as_raw_fd(),
9799
c_sample_cb,
98100
sample_cb_ptr as *mut _,
99101
null_mut(),
100102
)
101-
})?);
103+
};
104+
let ptr = validate_bpf_ret(ptr).context("failed to create new ring buffer")?;
105+
rb_ptr = Some(ptr)
102106
}
103107
Some(ptr) => {
104108
// Add a ringbuf to the existing ringbuf manager
@@ -121,7 +125,7 @@ impl<'slf, 'cb: 'slf> RingBufferBuilder<'slf, 'cb> {
121125
unsafe { cbs.push(Box::from_raw(sample_cb_ptr)) };
122126
}
123127

124-
match ptr {
128+
match rb_ptr {
125129
Some(ptr) => Ok(RingBuffer { ptr, _cbs: cbs }),
126130
None => Err(Error::with_invalid_data(
127131
"You must add at least one ring buffer map and callback before building",

0 commit comments

Comments
 (0)