Skip to content

Commit

Permalink
Enable clippy::absolute_paths lint
Browse files Browse the repository at this point in the history
In the past we had many submissions that, for one reason or another,
were using absolute paths to functions/constants in a very inconsistent
manner. It is not particularly great use of anybody's time pointing
these out and it's certainly a job that computers can do better. Clippy
folks seem to concur that this can be a useful lint [0] and we got
blessed with clippy::absolute_paths.
Enable it throughout libbpf-rs and libbpf-cargo and fix all violations.

[0] rust-lang/rust-clippy#10568

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o authored and danielocfb committed Dec 4, 2023
1 parent be286ef commit 4ef1505
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 45 deletions.
5 changes: 3 additions & 2 deletions libbpf-cargo/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashSet;
use std::env::consts::ARCH;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fs;
Expand Down Expand Up @@ -170,10 +171,10 @@ fn compile_one(debug: bool, source: &Path, out: &Path, clang: &Path, options: &s
}

if !options.contains("-D__TARGET_ARCH_") {
let arch = match std::env::consts::ARCH {
let arch = match ARCH {
"x86_64" => "x86",
"aarch64" => "arm64",
_ => std::env::consts::ARCH,
_ => ARCH,
};
cmd.arg(format!("-D__TARGET_ARCH_{arch}"));
}
Expand Down
3 changes: 2 additions & 1 deletion libbpf-cargo/src/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::fs::File;
use std::io::stdout;
use std::io::ErrorKind;
use std::io::Write;
use std::mem::size_of;
use std::os::raw::c_ulong;
use std::path::Path;
use std::path::PathBuf;
Expand Down Expand Up @@ -606,7 +607,7 @@ fn gen_skel_link_getter(skel: &mut String, object: &mut BpfObj, obj_name: &str)
fn open_bpf_object(name: &str, data: &[u8]) -> Result<BpfObj> {
let cname = CString::new(name)?;
let obj_opts = libbpf_sys::bpf_object_open_opts {
sz: std::mem::size_of::<libbpf_sys::bpf_object_open_opts>() as libbpf_sys::size_t,
sz: size_of::<libbpf_sys::bpf_object_open_opts>() as libbpf_sys::size_t,
object_name: cname.as_ptr(),
..Default::default()
};
Expand Down
1 change: 1 addition & 0 deletions libbpf-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#![warn(
elided_lifetimes_in_paths,
single_use_lifetimes,
clippy::absolute_paths,
clippy::wildcard_imports
)]
#![deny(unsafe_op_in_unsafe_fn)]
Expand Down
2 changes: 1 addition & 1 deletion libbpf-cargo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::let_unit_value)]
#![allow(clippy::absolute_paths, clippy::let_unit_value)]

use std::path::PathBuf;

Expand Down
8 changes: 5 additions & 3 deletions libbpf-rs/src/btf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ use std::marker::PhantomData;
use std::mem::size_of;
use std::num::NonZeroUsize;
use std::ops::Deref;
use std::os::raw::c_ulong;
use std::os::raw::c_void;
use std::os::unix::prelude::AsRawFd;
use std::os::unix::prelude::FromRawFd;
use std::os::unix::prelude::OsStrExt;
use std::os::unix::prelude::OwnedFd;
use std::path::Path;
use std::ptr;
use std::ptr::NonNull;

use num_enum::IntoPrimitive;
Expand Down Expand Up @@ -141,7 +143,7 @@ impl Btf<'static> {
Error::with_invalid_data(format!("invalid path {path:?}, has null bytes"))
})?;
let ptr = create_bpf_entity_checked(|| unsafe {
libbpf_sys::btf__parse(path.as_ptr(), std::ptr::null_mut())
libbpf_sys::btf__parse(path.as_ptr(), ptr::null_mut())
})?;
Ok(Btf {
ptr,
Expand Down Expand Up @@ -219,15 +221,15 @@ impl<'btf> Btf<'btf> {
.unwrap();

let obj_opts = libbpf_sys::bpf_object_open_opts {
sz: std::mem::size_of::<libbpf_sys::bpf_object_open_opts>() as libbpf_sys::size_t,
sz: size_of::<libbpf_sys::bpf_object_open_opts>() as libbpf_sys::size_t,
object_name: cname.as_ptr(),
..Default::default()
};

let mut bpf_obj = create_bpf_entity_checked(|| unsafe {
libbpf_sys::bpf_object__open_mem(
object_file.as_ptr() as *const c_void,
object_file.len() as std::os::raw::c_ulong,
object_file.len() as c_ulong,
&obj_opts,
)
})?;
Expand Down
2 changes: 1 addition & 1 deletion libbpf-rs/src/btf/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ pub enum IntEncoding {
impl<'btf> TryFrom<BtfType<'btf>> for Int<'btf> {
type Error = BtfType<'btf>;

fn try_from(t: BtfType<'btf>) -> std::result::Result<Self, Self::Error> {
fn try_from(t: BtfType<'btf>) -> Result<Self, Self::Error> {
if t.kind() == BtfKind::Int {
let int = {
let base_ptr = t.ty as *const libbpf_sys::btf_type;
Expand Down
1 change: 1 addition & 0 deletions libbpf-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
missing_debug_implementations,
missing_docs,
single_use_lifetimes,
clippy::absolute_paths,
clippy::wildcard_imports,
rustdoc::broken_intra_doc_links
)]
Expand Down
7 changes: 3 additions & 4 deletions libbpf-rs/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::os::unix::io::OwnedFd;
use std::os::unix::io::RawFd;
use std::path::Path;
use std::ptr;
use std::ptr::null;
use std::ptr::NonNull;
use std::slice::from_raw_parts;

Expand Down Expand Up @@ -83,7 +82,7 @@ impl OpenMap {
let ret = unsafe {
libbpf_sys::bpf_map__set_initial_value(
self.ptr.as_ptr(),
data.as_ptr() as *const std::ffi::c_void,
data.as_ptr() as *const c_void,
data.len() as libbpf_sys::size_t,
)
};
Expand Down Expand Up @@ -393,7 +392,7 @@ impl MapHandle {

let map_name_ptr = {
if map_name_str.as_bytes().is_empty() {
null()
ptr::null()
} else {
map_name_str.as_ptr()
}
Expand Down Expand Up @@ -934,7 +933,7 @@ impl MapType {
/// Make sure the process has required set of CAP_* permissions (or runs as
/// root) when performing feature checking.
pub fn is_supported(&self) -> Result<bool> {
let ret = unsafe { libbpf_sys::libbpf_probe_bpf_map_type(*self as u32, std::ptr::null()) };
let ret = unsafe { libbpf_sys::libbpf_probe_bpf_map_type(*self as u32, ptr::null()) };
match ret {
0 => Ok(false),
1 => Ok(true),
Expand Down
14 changes: 7 additions & 7 deletions libbpf-rs/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl OpenObject {
};

// Populate obj.maps
let mut map: *mut libbpf_sys::bpf_map = std::ptr::null_mut();
let mut map: *mut libbpf_sys::bpf_map = ptr::null_mut();
loop {
// Get the pointer to the next BPF map
let map_ptr = {
Expand All @@ -190,7 +190,7 @@ impl OpenObject {
}

// Populate obj.progs
let mut prog: *mut libbpf_sys::bpf_program = std::ptr::null_mut();
let mut prog: *mut libbpf_sys::bpf_program = ptr::null_mut();
loop {
// Get the pointer to the next BPF program
let prog_ptr = {
Expand Down Expand Up @@ -233,12 +233,12 @@ impl OpenObject {
// using destructuring we make sure we'll get a compiler error if anything in
// Self changes, which will alert us to change this function as well
let Self { ptr, maps, progs } = &mut self;
std::mem::take(maps);
std::mem::take(progs);
mem::take(maps);
mem::take(progs);
*ptr
};
// avoid double free of self.ptr
std::mem::forget(self);
mem::forget(self);
ptr
}

Expand Down Expand Up @@ -354,7 +354,7 @@ impl Object {
};

// Populate obj.maps
let mut map: *mut libbpf_sys::bpf_map = std::ptr::null_mut();
let mut map: *mut libbpf_sys::bpf_map = ptr::null_mut();
loop {
// Get the pointer to the next BPF map
let map_ptr = {
Expand All @@ -373,7 +373,7 @@ impl Object {
}

// Populate obj.progs
let mut prog: *mut libbpf_sys::bpf_program = std::ptr::null_mut();
let mut prog: *mut libbpf_sys::bpf_program = ptr::null_mut();
loop {
// Get the pointer to the next BPF program
let prog_ptr = {
Expand Down
3 changes: 2 additions & 1 deletion libbpf-rs/src/perf_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::os::unix::io::AsFd;
use std::os::unix::prelude::AsRawFd;
use std::ptr;
use std::ptr::NonNull;
use std::slice;
use std::time::Duration;
Expand Down Expand Up @@ -133,7 +134,7 @@ impl<'a, 'b> PerfBufferBuilder<'a, 'b> {
c_sample_cb,
c_lost_cb,
callback_struct_ptr as *mut _,
std::ptr::null(),
ptr::null(),
)
})
.map(|ptr| PerfBuffer {
Expand Down
3 changes: 2 additions & 1 deletion libbpf-rs/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::libbpf_sys;
use lazy_static::lazy_static;
use std::io;
use std::io::Write;
use std::mem;
use std::os::raw::c_char;
use std::sync::Mutex;

Expand Down Expand Up @@ -117,7 +118,7 @@ pub fn set_print(
mut callback: Option<(PrintLevel, PrintCallback)>,
) -> Option<(PrintLevel, PrintCallback)> {
let real_cb: libbpf_sys::libbpf_print_fn_t = callback.as_ref().and(Some(outer_print_cb));
std::mem::swap(&mut callback, &mut *PRINT_CB.lock().unwrap());
mem::swap(&mut callback, &mut *PRINT_CB.lock().unwrap());
unsafe { libbpf_sys::libbpf_set_print(real_cb) };
callback
}
Expand Down
26 changes: 13 additions & 13 deletions libbpf-rs/src/program.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::ffi::c_void;
use std::convert::TryFrom;
use std::ffi::CStr;
use std::mem;
use std::mem::size_of;
use std::os::unix::io::AsFd;
use std::os::unix::io::AsRawFd;
use std::os::unix::io::BorrowedFd;
Expand All @@ -10,6 +10,7 @@ use std::os::unix::io::OwnedFd;
use std::path::Path;
use std::ptr;
use std::ptr::NonNull;
use std::slice;

use libbpf_sys::bpf_func_id;
use num_enum::TryFromPrimitive;
Expand Down Expand Up @@ -60,7 +61,7 @@ impl From<UsdtOpts> for libbpf_sys::bpf_usdt_opts {
_non_exhaustive,
} = opts;
libbpf_sys::bpf_usdt_opts {
sz: mem::size_of::<Self>() as _,
sz: size_of::<Self>() as _,
usdt_cookie: cookie,
}
}
Expand All @@ -83,7 +84,7 @@ impl From<TracepointOpts> for libbpf_sys::bpf_tracepoint_opts {
} = opts;

libbpf_sys::bpf_tracepoint_opts {
sz: mem::size_of::<Self>() as _,
sz: size_of::<Self>() as _,
bpf_cookie: cookie,
}
}
Expand Down Expand Up @@ -226,7 +227,7 @@ impl OpenProgram {
pub fn insns(&self) -> &[libbpf_sys::bpf_insn] {
let count = self.insn_cnt();
let ptr = unsafe { libbpf_sys::bpf_program__insns(self.ptr.as_ptr()) };
unsafe { std::slice::from_raw_parts(ptr, count) }
unsafe { slice::from_raw_parts(ptr, count) }
}
}

Expand Down Expand Up @@ -288,7 +289,7 @@ impl ProgramType {
/// Make sure the process has required set of CAP_* permissions (or runs as
/// root) when performing feature checking.
pub fn is_supported(&self) -> Result<bool> {
let ret = unsafe { libbpf_sys::libbpf_probe_bpf_prog_type(*self as u32, std::ptr::null()) };
let ret = unsafe { libbpf_sys::libbpf_probe_bpf_prog_type(*self as u32, ptr::null()) };
match ret {
0 => Ok(false),
1 => Ok(true),
Expand All @@ -302,9 +303,8 @@ impl ProgramType {
/// Make sure the process has required set of CAP_* permissions (or runs as
/// root) when performing feature checking.
pub fn is_helper_supported(&self, helper_id: bpf_func_id) -> Result<bool> {
let ret = unsafe {
libbpf_sys::libbpf_probe_bpf_helper(*self as u32, helper_id, std::ptr::null())
};
let ret =
unsafe { libbpf_sys::libbpf_probe_bpf_helper(*self as u32, helper_id, ptr::null()) };
match ret {
0 => Ok(false),
1 => Ok(true),
Expand Down Expand Up @@ -438,7 +438,7 @@ impl Program {
pub fn get_id_by_fd(fd: BorrowedFd<'_>) -> Result<u32> {
let mut prog_info = libbpf_sys::bpf_prog_info::default();
let prog_info_ptr: *mut libbpf_sys::bpf_prog_info = &mut prog_info;
let mut len = mem::size_of::<libbpf_sys::bpf_prog_info>() as u32;
let mut len = size_of::<libbpf_sys::bpf_prog_info>() as u32;
let ret = unsafe {
libbpf_sys::bpf_obj_get_info_by_fd(
fd.as_raw_fd(),
Expand Down Expand Up @@ -577,7 +577,7 @@ impl Program {

let func_name = util::str_to_cstring(&func_name)?;
let opts = libbpf_sys::bpf_uprobe_opts {
sz: mem::size_of::<libbpf_sys::bpf_uprobe_opts>() as _,
sz: size_of::<libbpf_sys::bpf_uprobe_opts>() as _,
ref_ctr_offset: ref_ctr_offset as libbpf_sys::size_t,
bpf_cookie: cookie,
retprobe,
Expand Down Expand Up @@ -829,8 +829,8 @@ impl Program {
linkinfo.map.map_fd = map_fd.as_raw_fd() as _;
let attach_opt = libbpf_sys::bpf_iter_attach_opts {
link_info: &mut linkinfo as *mut libbpf_sys::bpf_iter_link_info,
link_info_len: std::mem::size_of::<libbpf_sys::bpf_iter_link_info>() as _,
sz: std::mem::size_of::<libbpf_sys::bpf_iter_attach_opts>() as _,
link_info_len: size_of::<libbpf_sys::bpf_iter_link_info>() as _,
sz: size_of::<libbpf_sys::bpf_iter_attach_opts>() as _,
..Default::default()
};

Expand Down Expand Up @@ -859,6 +859,6 @@ impl Program {
pub fn insns(&self) -> &[libbpf_sys::bpf_insn] {
let count = self.insn_cnt();
let ptr = unsafe { libbpf_sys::bpf_program__insns(self.ptr.as_ptr()) };
unsafe { std::slice::from_raw_parts(ptr, count) }
unsafe { slice::from_raw_parts(ptr, count) }
}
}
5 changes: 3 additions & 2 deletions libbpf-rs/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::ffi::c_void;
use std::ffi::CString;
use std::mem::size_of_val;
use std::os::raw::c_char;
use std::ptr;
use std::time::Duration;

use nix::errno;
Expand Down Expand Up @@ -337,7 +338,7 @@ impl ProgramInfo {
}

if opts.include_jited_line_info {
jited_line_info.resize(item.nr_jited_line_info as usize, std::ptr::null());
jited_line_info.resize(item.nr_jited_line_info as usize, ptr::null());
item.jited_line_info = jited_line_info.as_mut_ptr() as *mut c_void as u64;
} else {
item.nr_jited_line_info = 0;
Expand All @@ -358,7 +359,7 @@ impl ProgramInfo {
}

if opts.include_jited_ksyms {
jited_ksyms.resize(item.nr_jited_ksyms as usize, std::ptr::null());
jited_ksyms.resize(item.nr_jited_ksyms as usize, ptr::null());
item.jited_ksyms = jited_ksyms.as_mut_ptr() as *mut c_void as u64;
} else {
item.nr_jited_ksyms = 0;
Expand Down
3 changes: 2 additions & 1 deletion libbpf-rs/src/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::os::raw::c_ulong;
use std::os::unix::io::AsFd;
use std::os::unix::prelude::AsRawFd;
use std::os::unix::prelude::BorrowedFd;
use std::ptr::null_mut;
use std::ptr::NonNull;
use std::slice;
use std::time::Duration;
Expand Down Expand Up @@ -98,7 +99,7 @@ impl<'slf, 'cb: 'slf> RingBufferBuilder<'slf, 'cb> {
fd.as_raw_fd(),
c_sample_cb,
sample_cb_ptr as *mut _,
std::ptr::null_mut(),
null_mut(),
)
})?);
}
Expand Down
5 changes: 3 additions & 2 deletions libbpf-rs/src/tc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::mem::size_of;
use std::os::unix::io::AsRawFd;
use std::os::unix::io::BorrowedFd;

Expand Down Expand Up @@ -64,8 +65,8 @@ impl TcHook {
opts: libbpf_sys::bpf_tc_opts::default(),
};

tc_hook.hook.sz = std::mem::size_of::<libbpf_sys::bpf_tc_hook>() as libbpf_sys::size_t;
tc_hook.opts.sz = std::mem::size_of::<libbpf_sys::bpf_tc_opts>() as libbpf_sys::size_t;
tc_hook.hook.sz = size_of::<libbpf_sys::bpf_tc_hook>() as libbpf_sys::size_t;
tc_hook.opts.sz = size_of::<libbpf_sys::bpf_tc_opts>() as libbpf_sys::size_t;
tc_hook.opts.prog_fd = fd.as_raw_fd();

tc_hook
Expand Down
Loading

0 comments on commit 4ef1505

Please sign in to comment.