From 4ef150551dbb25b18db7368cec79ca7ef9adfe0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Wed, 26 Jul 2023 10:12:56 -0700 Subject: [PATCH] Enable clippy::absolute_paths lint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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] https://github.com/rust-lang/rust-clippy/issues/10568 Signed-off-by: Daniel Müller --- libbpf-cargo/src/build.rs | 5 +++-- libbpf-cargo/src/gen/mod.rs | 3 ++- libbpf-cargo/src/lib.rs | 1 + libbpf-cargo/src/main.rs | 2 +- libbpf-rs/src/btf/mod.rs | 8 +++++--- libbpf-rs/src/btf/types.rs | 2 +- libbpf-rs/src/lib.rs | 1 + libbpf-rs/src/map.rs | 7 +++---- libbpf-rs/src/object.rs | 14 +++++++------- libbpf-rs/src/perf_buffer.rs | 3 ++- libbpf-rs/src/print.rs | 3 ++- libbpf-rs/src/program.rs | 26 +++++++++++++------------- libbpf-rs/src/query.rs | 5 +++-- libbpf-rs/src/ringbuf.rs | 3 ++- libbpf-rs/src/tc.rs | 5 +++-- libbpf-rs/src/util.rs | 13 +++++++------ 16 files changed, 56 insertions(+), 45 deletions(-) diff --git a/libbpf-cargo/src/build.rs b/libbpf-cargo/src/build.rs index 14bf656f..d58b8b7e 100644 --- a/libbpf-cargo/src/build.rs +++ b/libbpf-cargo/src/build.rs @@ -1,4 +1,5 @@ use std::collections::HashSet; +use std::env::consts::ARCH; use std::ffi::OsStr; use std::ffi::OsString; use std::fs; @@ -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}")); } diff --git a/libbpf-cargo/src/gen/mod.rs b/libbpf-cargo/src/gen/mod.rs index d41fd3eb..430d6a67 100644 --- a/libbpf-cargo/src/gen/mod.rs +++ b/libbpf-cargo/src/gen/mod.rs @@ -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; @@ -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 { let cname = CString::new(name)?; let obj_opts = libbpf_sys::bpf_object_open_opts { - sz: std::mem::size_of::() as libbpf_sys::size_t, + sz: size_of::() as libbpf_sys::size_t, object_name: cname.as_ptr(), ..Default::default() }; diff --git a/libbpf-cargo/src/lib.rs b/libbpf-cargo/src/lib.rs index 48aea59a..363af903 100644 --- a/libbpf-cargo/src/lib.rs +++ b/libbpf-cargo/src/lib.rs @@ -59,6 +59,7 @@ #![warn( elided_lifetimes_in_paths, single_use_lifetimes, + clippy::absolute_paths, clippy::wildcard_imports )] #![deny(unsafe_op_in_unsafe_fn)] diff --git a/libbpf-cargo/src/main.rs b/libbpf-cargo/src/main.rs index d9c90604..f4b51834 100644 --- a/libbpf-cargo/src/main.rs +++ b/libbpf-cargo/src/main.rs @@ -1,4 +1,4 @@ -#![allow(clippy::let_unit_value)] +#![allow(clippy::absolute_paths, clippy::let_unit_value)] use std::path::PathBuf; diff --git a/libbpf-rs/src/btf/mod.rs b/libbpf-rs/src/btf/mod.rs index 01b9f440..080f4fe9 100644 --- a/libbpf-rs/src/btf/mod.rs +++ b/libbpf-rs/src/btf/mod.rs @@ -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; @@ -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, @@ -219,7 +221,7 @@ impl<'btf> Btf<'btf> { .unwrap(); let obj_opts = libbpf_sys::bpf_object_open_opts { - sz: std::mem::size_of::() as libbpf_sys::size_t, + sz: size_of::() as libbpf_sys::size_t, object_name: cname.as_ptr(), ..Default::default() }; @@ -227,7 +229,7 @@ impl<'btf> Btf<'btf> { 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, ) })?; diff --git a/libbpf-rs/src/btf/types.rs b/libbpf-rs/src/btf/types.rs index 6921b6b8..d3206692 100644 --- a/libbpf-rs/src/btf/types.rs +++ b/libbpf-rs/src/btf/types.rs @@ -339,7 +339,7 @@ pub enum IntEncoding { impl<'btf> TryFrom> for Int<'btf> { type Error = BtfType<'btf>; - fn try_from(t: BtfType<'btf>) -> std::result::Result { + fn try_from(t: BtfType<'btf>) -> Result { if t.kind() == BtfKind::Int { let int = { let base_ptr = t.ty as *const libbpf_sys::btf_type; diff --git a/libbpf-rs/src/lib.rs b/libbpf-rs/src/lib.rs index e5f7571c..3d96043e 100644 --- a/libbpf-rs/src/lib.rs +++ b/libbpf-rs/src/lib.rs @@ -72,6 +72,7 @@ missing_debug_implementations, missing_docs, single_use_lifetimes, + clippy::absolute_paths, clippy::wildcard_imports, rustdoc::broken_intra_doc_links )] diff --git a/libbpf-rs/src/map.rs b/libbpf-rs/src/map.rs index fa0abae7..81c8761c 100644 --- a/libbpf-rs/src/map.rs +++ b/libbpf-rs/src/map.rs @@ -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; @@ -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, ) }; @@ -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() } @@ -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 { - 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), diff --git a/libbpf-rs/src/object.rs b/libbpf-rs/src/object.rs index 46b1c568..2ceef71a 100644 --- a/libbpf-rs/src/object.rs +++ b/libbpf-rs/src/object.rs @@ -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 = { @@ -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 = { @@ -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 } @@ -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 = { @@ -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 = { diff --git a/libbpf-rs/src/perf_buffer.rs b/libbpf-rs/src/perf_buffer.rs index f667d834..48c66636 100644 --- a/libbpf-rs/src/perf_buffer.rs +++ b/libbpf-rs/src/perf_buffer.rs @@ -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; @@ -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 { diff --git a/libbpf-rs/src/print.rs b/libbpf-rs/src/print.rs index 81f4c305..dd656f66 100644 --- a/libbpf-rs/src/print.rs +++ b/libbpf-rs/src/print.rs @@ -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; @@ -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 } diff --git a/libbpf-rs/src/program.rs b/libbpf-rs/src/program.rs index 1909d29e..55c07513 100644 --- a/libbpf-rs/src/program.rs +++ b/libbpf-rs/src/program.rs @@ -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; @@ -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; @@ -60,7 +61,7 @@ impl From for libbpf_sys::bpf_usdt_opts { _non_exhaustive, } = opts; libbpf_sys::bpf_usdt_opts { - sz: mem::size_of::() as _, + sz: size_of::() as _, usdt_cookie: cookie, } } @@ -83,7 +84,7 @@ impl From for libbpf_sys::bpf_tracepoint_opts { } = opts; libbpf_sys::bpf_tracepoint_opts { - sz: mem::size_of::() as _, + sz: size_of::() as _, bpf_cookie: cookie, } } @@ -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) } } } @@ -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 { - 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), @@ -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 { - 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), @@ -438,7 +438,7 @@ impl Program { pub fn get_id_by_fd(fd: BorrowedFd<'_>) -> Result { 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::() as u32; + let mut len = size_of::() as u32; let ret = unsafe { libbpf_sys::bpf_obj_get_info_by_fd( fd.as_raw_fd(), @@ -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::() as _, + sz: size_of::() as _, ref_ctr_offset: ref_ctr_offset as libbpf_sys::size_t, bpf_cookie: cookie, retprobe, @@ -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::() as _, - sz: std::mem::size_of::() as _, + link_info_len: size_of::() as _, + sz: size_of::() as _, ..Default::default() }; @@ -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) } } } diff --git a/libbpf-rs/src/query.rs b/libbpf-rs/src/query.rs index 88194272..45c0f7ca 100644 --- a/libbpf-rs/src/query.rs +++ b/libbpf-rs/src/query.rs @@ -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; @@ -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; @@ -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; diff --git a/libbpf-rs/src/ringbuf.rs b/libbpf-rs/src/ringbuf.rs index 095636bd..5d803464 100644 --- a/libbpf-rs/src/ringbuf.rs +++ b/libbpf-rs/src/ringbuf.rs @@ -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; @@ -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(), ) })?); } diff --git a/libbpf-rs/src/tc.rs b/libbpf-rs/src/tc.rs index 8786ccc4..85e22447 100644 --- a/libbpf-rs/src/tc.rs +++ b/libbpf-rs/src/tc.rs @@ -1,3 +1,4 @@ +use std::mem::size_of; use std::os::unix::io::AsRawFd; use std::os::unix::io::BorrowedFd; @@ -64,8 +65,8 @@ impl TcHook { opts: libbpf_sys::bpf_tc_opts::default(), }; - tc_hook.hook.sz = std::mem::size_of::() as libbpf_sys::size_t; - tc_hook.opts.sz = std::mem::size_of::() as libbpf_sys::size_t; + tc_hook.hook.sz = size_of::() as libbpf_sys::size_t; + tc_hook.opts.sz = size_of::() as libbpf_sys::size_t; tc_hook.opts.prog_fd = fd.as_raw_fd(); tc_hook diff --git a/libbpf-rs/src/util.rs b/libbpf-rs/src/util.rs index 0d5332ec..059a3676 100644 --- a/libbpf-rs/src/util.rs +++ b/libbpf-rs/src/util.rs @@ -1,3 +1,4 @@ +use std::any::type_name; use std::ffi::CStr; use std::ffi::CString; use std::io; @@ -95,12 +96,12 @@ pub fn create_bpf_entity_checked *mut B>(f: F) -> Res io::ErrorKind::Other, format!( "bpf call {:?} returned NULL", - std::any::type_name::() // this is usually a library bug, hopefully this will - // help diagnose the bug. - // - // One way to fix the bug might be to change to calling - // create_bpf_entity_checked_opt and handling Ok(None) - // as a meaningful value. + type_name::() // this is usually a library bug, hopefully this will + // help diagnose the bug. + // + // One way to fix the bug might be to change to calling + // create_bpf_entity_checked_opt and handling Ok(None) + // as a meaningful value. ), ) })