From 40516e1352fb8b94b35ada201aabdb07a6cea6e7 Mon Sep 17 00:00:00 2001 From: Dave Tucker Date: Thu, 27 Mar 2025 13:28:57 +0000 Subject: [PATCH] chore: Fix clippy panic_handler warnings Working with aya in vscode will currently show a number of warnings along the lines of: ``` found duplicate lang item `panic_impl` the lang item is first defined in crate `std` (which `aya` depends on) ... second definition in the local crate (`bpf_probe_read`) ``` This comes from feature unification. integration-test requires the integration-common user feature, which requires aya, which in turn brings in std. For this same reason we avoid running clippy across the whole workspace. We can avoid this issue by using the panic handler from the panic_halt crate, which implements the same loop {} panic handler we use today. However, it seems rustc is happy to conditionally link the panic handler from an external crate without issuing warnings. Signed-off-by: Dave Tucker --- Cargo.toml | 2 ++ clippy.sh | 8 +------- test/integration-ebpf/Cargo.toml | 3 ++- test/integration-ebpf/src/bpf_probe_read.rs | 8 ++------ test/integration-ebpf/src/log.rs | 8 ++------ test/integration-ebpf/src/map_test.rs | 8 ++------ test/integration-ebpf/src/memmove_test.rs | 8 ++------ test/integration-ebpf/src/name_test.rs | 8 ++------ test/integration-ebpf/src/pass.rs | 8 ++------ test/integration-ebpf/src/raw_tracepoint.rs | 8 ++------ test/integration-ebpf/src/redirect.rs | 8 ++------ test/integration-ebpf/src/relocations.rs | 8 ++------ test/integration-ebpf/src/ring_buf.rs | 8 ++------ test/integration-ebpf/src/simple_prog.rs | 8 ++------ test/integration-ebpf/src/strncmp.rs | 8 ++------ test/integration-ebpf/src/tcx.rs | 8 ++------ test/integration-ebpf/src/test.rs | 8 ++------ test/integration-ebpf/src/two_progs.rs | 8 ++------ test/integration-ebpf/src/uprobe_cookie.rs | 8 ++------ test/integration-ebpf/src/xdp_sec.rs | 8 ++------ 20 files changed, 39 insertions(+), 110 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b21d6f34..6796941c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -80,11 +80,13 @@ indoc = { version = "2.0", default-features = false } libc = { version = "0.2.105", default-features = false } log = { version = "0.4", default-features = false } netns-rs = { version = "0.1", default-features = false } +network-types = { version = "0.0.7", default-features = false } nix = { version = "0.29.0", default-features = false } num_enum = { version = "0.7", default-features = false } object = { version = "0.36", default-features = false } octorust = { version = "0.10", default-features = false } once_cell = { version = "1.20.1", default-features = false } +panic-halt = { version = "1", default-features = false } proc-macro2 = { version = "1", default-features = false } proc-macro2-diagnostics = { version = "0.10.1", default-features = false } public-api = { version = "0.44.0", default-features = false } diff --git a/clippy.sh b/clippy.sh index b690f988..62de2bb5 100755 --- a/clippy.sh +++ b/clippy.sh @@ -2,11 +2,6 @@ set -eux -# We cannot run clippy over the whole workspace at once due to feature unification. Since both -# integration-test and integration-ebpf depend on integration-common and integration-test activates -# integration-common's aya dependency, we end up trying to compile the panic handler twice: once -# from the bpf program, and again from std via aya. -# # `-C panic=abort` because "unwinding panics are not supported without std"; integration-ebpf # contains `#[no_std]` binaries. # @@ -16,5 +11,4 @@ set -eux # unwinding behavior. # # `+nightly` because "the option `Z` is only accepted on the nightly compiler". -cargo +nightly hack clippy "$@" --exclude integration-ebpf --all-targets --feature-powerset --workspace -- --deny warnings -cargo +nightly hack clippy "$@" --package integration-ebpf --all-targets --feature-powerset -- --deny warnings -C panic=abort -Zpanic_abort_tests +cargo +nightly hack clippy "$@" --all-targets --feature-powerset -- --deny warnings -C panic=abort -Zpanic_abort_tests diff --git a/test/integration-ebpf/Cargo.toml b/test/integration-ebpf/Cargo.toml index bb4c950f..5afe3f53 100644 --- a/test/integration-ebpf/Cargo.toml +++ b/test/integration-ebpf/Cargo.toml @@ -17,7 +17,8 @@ workspace = true aya-ebpf = { path = "../../ebpf/aya-ebpf" } aya-log-ebpf = { path = "../../ebpf/aya-log-ebpf" } integration-common = { path = "../integration-common" } -network-types = "0.0.7" +network-types = { workspace = true } +panic-halt = { workspace = true } [build-dependencies] which = { workspace = true } diff --git a/test/integration-ebpf/src/bpf_probe_read.rs b/test/integration-ebpf/src/bpf_probe_read.rs index a721163c..c580db98 100644 --- a/test/integration-ebpf/src/bpf_probe_read.rs +++ b/test/integration-ebpf/src/bpf_probe_read.rs @@ -8,6 +8,8 @@ use aya_ebpf::{ programs::ProbeContext, }; use integration_common::bpf_probe_read::{RESULT_BUF_LEN, TestResult}; +#[cfg(not(test))] +use panic_halt as _; fn read_str_bytes( fun: unsafe fn(*const u8, &mut [u8]) -> Result<&[u8], i64>, @@ -71,9 +73,3 @@ pub fn test_bpf_probe_read_kernel_str_bytes(ctx: ProbeContext) { ctx.arg::(0), ); } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/log.rs b/test/integration-ebpf/src/log.rs index b6471f3f..9daf95f6 100644 --- a/test/integration-ebpf/src/log.rs +++ b/test/integration-ebpf/src/log.rs @@ -5,6 +5,8 @@ use core::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use aya_ebpf::{macros::uprobe, programs::ProbeContext}; use aya_log_ebpf::{debug, error, info, trace, warn}; +#[cfg(not(test))] +use panic_halt as _; #[uprobe] pub fn test_log(ctx: ProbeContext) { @@ -81,9 +83,3 @@ pub fn test_log(ctx: ProbeContext) { debug!(&ctx, "{:x}", no_copy.consume()); } } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/map_test.rs b/test/integration-ebpf/src/map_test.rs index e53b2f85..ed46e09f 100644 --- a/test/integration-ebpf/src/map_test.rs +++ b/test/integration-ebpf/src/map_test.rs @@ -9,6 +9,8 @@ use aya_ebpf::{ maps::{Array, HashMap}, programs::SkBuffContext, }; +#[cfg(not(test))] +use panic_halt as _; // Introduced in kernel v3.19. #[map] @@ -35,9 +37,3 @@ pub fn simple_prog(_ctx: SkBuffContext) -> i64 { 0 } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/memmove_test.rs b/test/integration-ebpf/src/memmove_test.rs index f9002131..3415fc85 100644 --- a/test/integration-ebpf/src/memmove_test.rs +++ b/test/integration-ebpf/src/memmove_test.rs @@ -13,6 +13,8 @@ use network_types::{ eth::{EthHdr, EtherType}, ip::Ipv6Hdr, }; +#[cfg(not(test))] +use panic_halt as _; #[inline(always)] fn ptr_at(ctx: &XdpContext, offset: usize) -> Result<*const T, ()> { @@ -53,9 +55,3 @@ fn try_do_dnat(ctx: XdpContext) -> Result { } Ok(xdp_action::XDP_PASS) } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/name_test.rs b/test/integration-ebpf/src/name_test.rs index d1b48950..4bf138ad 100644 --- a/test/integration-ebpf/src/name_test.rs +++ b/test/integration-ebpf/src/name_test.rs @@ -2,6 +2,8 @@ #![no_main] use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext}; +#[cfg(not(test))] +use panic_halt as _; #[xdp] pub fn ihaveaverylongname(ctx: XdpContext) -> u32 { @@ -14,9 +16,3 @@ pub fn ihaveaverylongname(ctx: XdpContext) -> u32 { unsafe fn try_pass(_ctx: XdpContext) -> Result { Ok(xdp_action::XDP_PASS) } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/pass.rs b/test/integration-ebpf/src/pass.rs index 795d82b0..033d5ade 100644 --- a/test/integration-ebpf/src/pass.rs +++ b/test/integration-ebpf/src/pass.rs @@ -2,6 +2,8 @@ #![no_main] use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext}; +#[cfg(not(test))] +use panic_halt as _; // Note: the `frags` attribute causes this probe to be incompatible with kernel versions < 5.18.0. // See https://github.com/torvalds/linux/commit/c2f2cdb. @@ -16,9 +18,3 @@ pub fn pass(ctx: XdpContext) -> u32 { unsafe fn try_pass(_ctx: XdpContext) -> Result { Ok(xdp_action::XDP_PASS) } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/raw_tracepoint.rs b/test/integration-ebpf/src/raw_tracepoint.rs index 1db9a21b..545df6e9 100644 --- a/test/integration-ebpf/src/raw_tracepoint.rs +++ b/test/integration-ebpf/src/raw_tracepoint.rs @@ -7,6 +7,8 @@ use aya_ebpf::{ programs::RawTracePointContext, }; use integration_common::raw_tracepoint::SysEnterEvent; +#[cfg(not(test))] +use panic_halt as _; #[map] static RESULT: Array = Array::with_max_entries(1, 0); @@ -25,9 +27,3 @@ pub fn sys_enter(ctx: RawTracePointContext) -> i32 { 0 } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/redirect.rs b/test/integration-ebpf/src/redirect.rs index eb32f999..298083c4 100644 --- a/test/integration-ebpf/src/redirect.rs +++ b/test/integration-ebpf/src/redirect.rs @@ -7,6 +7,8 @@ use aya_ebpf::{ maps::{Array, CpuMap, DevMap, DevMapHash, XskMap}, programs::XdpContext, }; +#[cfg(not(test))] +use panic_halt as _; #[map] static SOCKS: XskMap = XskMap::with_max_entries(1, 0); @@ -74,9 +76,3 @@ fn inc_hit(index: u32) { unsafe { *hit += 1 }; } } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/relocations.rs b/test/integration-ebpf/src/relocations.rs index dc9e71ca..c4292a1e 100644 --- a/test/integration-ebpf/src/relocations.rs +++ b/test/integration-ebpf/src/relocations.rs @@ -8,6 +8,8 @@ use aya_ebpf::{ maps::Array, programs::ProbeContext, }; +#[cfg(not(test))] +use panic_halt as _; #[map] static RESULTS: Array = Array::with_max_entries(3, 0); @@ -38,9 +40,3 @@ fn set_result(index: u32, value: u64) { fn set_result_backward(index: u32, value: u64) { set_result(index, value); } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/ring_buf.rs b/test/integration-ebpf/src/ring_buf.rs index 6674f5e6..93eba8e5 100644 --- a/test/integration-ebpf/src/ring_buf.rs +++ b/test/integration-ebpf/src/ring_buf.rs @@ -7,6 +7,8 @@ use aya_ebpf::{ programs::ProbeContext, }; use integration_common::ring_buf::Registers; +#[cfg(not(test))] +use panic_halt as _; #[map] static RING_BUF: RingBuf = RingBuf::with_byte_size(0, 0); @@ -45,9 +47,3 @@ pub fn ring_buf_test(ctx: ProbeContext) { entry.discard(0); } } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/simple_prog.rs b/test/integration-ebpf/src/simple_prog.rs index 98725dce..725866b0 100644 --- a/test/integration-ebpf/src/simple_prog.rs +++ b/test/integration-ebpf/src/simple_prog.rs @@ -5,15 +5,11 @@ #![no_main] use aya_ebpf::{macros::socket_filter, programs::SkBuffContext}; +#[cfg(not(test))] +use panic_halt as _; // Introduced in kernel v3.19. #[socket_filter] pub fn simple_prog(_ctx: SkBuffContext) -> i64 { 0 } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/strncmp.rs b/test/integration-ebpf/src/strncmp.rs index a19b10d2..f329a8e1 100644 --- a/test/integration-ebpf/src/strncmp.rs +++ b/test/integration-ebpf/src/strncmp.rs @@ -9,6 +9,8 @@ use aya_ebpf::{ programs::ProbeContext, }; use integration_common::strncmp::TestResult; +#[cfg(not(test))] +use panic_halt as _; #[map] static RESULT: Array = Array::with_max_entries(1, 0); @@ -26,9 +28,3 @@ pub fn test_bpf_strncmp(ctx: ProbeContext) -> Result<(), c_long> { Ok(()) } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/tcx.rs b/test/integration-ebpf/src/tcx.rs index 5ed3c211..98a47d88 100644 --- a/test/integration-ebpf/src/tcx.rs +++ b/test/integration-ebpf/src/tcx.rs @@ -2,14 +2,10 @@ #![no_main] use aya_ebpf::{bindings::tcx_action_base::TCX_NEXT, macros::classifier, programs::TcContext}; +#[cfg(not(test))] +use panic_halt as _; #[classifier] pub fn tcx_next(_ctx: TcContext) -> i32 { TCX_NEXT } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/test.rs b/test/integration-ebpf/src/test.rs index 417da0de..ade5ae55 100644 --- a/test/integration-ebpf/src/test.rs +++ b/test/integration-ebpf/src/test.rs @@ -8,6 +8,8 @@ use aya_ebpf::{ FlowDissectorContext, ProbeContext, RetProbeContext, TracePointContext, XdpContext, }, }; +#[cfg(not(test))] +use panic_halt as _; #[xdp] pub fn pass(ctx: XdpContext) -> u32 { @@ -52,9 +54,3 @@ pub fn test_flow(_ctx: FlowDissectorContext) -> u32 { // Linux kernel for inspiration. bpf_ret_code::BPF_FLOW_DISSECTOR_CONTINUE } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/two_progs.rs b/test/integration-ebpf/src/two_progs.rs index 17d08168..2ad063a7 100644 --- a/test/integration-ebpf/src/two_progs.rs +++ b/test/integration-ebpf/src/two_progs.rs @@ -4,6 +4,8 @@ #![no_main] use aya_ebpf::{macros::tracepoint, programs::TracePointContext}; +#[cfg(not(test))] +use panic_halt as _; #[tracepoint] pub fn test_tracepoint_one(_ctx: TracePointContext) -> u32 { @@ -13,9 +15,3 @@ pub fn test_tracepoint_one(_ctx: TracePointContext) -> u32 { pub fn test_tracepoint_two(_ctx: TracePointContext) -> u32 { 0 } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/uprobe_cookie.rs b/test/integration-ebpf/src/uprobe_cookie.rs index 19713f85..f07b4cce 100644 --- a/test/integration-ebpf/src/uprobe_cookie.rs +++ b/test/integration-ebpf/src/uprobe_cookie.rs @@ -7,6 +7,8 @@ use aya_ebpf::{ maps::RingBuf, programs::ProbeContext, }; +#[cfg(not(test))] +use panic_halt as _; #[map] static RING_BUF: RingBuf = RingBuf::with_byte_size(0, 0); @@ -17,9 +19,3 @@ pub fn uprobe_cookie(ctx: ProbeContext) { let cookie_bytes = cookie.to_le_bytes(); let _res = RING_BUF.output(&cookie_bytes, 0); } - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} diff --git a/test/integration-ebpf/src/xdp_sec.rs b/test/integration-ebpf/src/xdp_sec.rs index c9eed920..ba394d41 100644 --- a/test/integration-ebpf/src/xdp_sec.rs +++ b/test/integration-ebpf/src/xdp_sec.rs @@ -2,6 +2,8 @@ #![no_main] use aya_ebpf::{bindings::xdp_action::XDP_PASS, macros::xdp, programs::XdpContext}; +#[cfg(not(test))] +use panic_halt as _; macro_rules! probe { ($name:ident, ($($arg:ident $(= $value:literal)?),*) ) => { @@ -18,9 +20,3 @@ probe!(xdp_cpumap, (map = "cpumap")); probe!(xdp_devmap, (map = "devmap")); probe!(xdp_frags_cpumap, (frags, map = "cpumap")); probe!(xdp_frags_devmap, (frags, map = "devmap")); - -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -}