diff --git a/ebpf/aya-ebpf/src/lib.rs b/ebpf/aya-ebpf/src/lib.rs index 4ff3c40b..3a84a325 100644 --- a/ebpf/aya-ebpf/src/lib.rs +++ b/ebpf/aya-ebpf/src/lib.rs @@ -133,6 +133,22 @@ pub fn check_bounds_signed(value: i64, lower: i64, upper: i64) -> bool { } } +#[macro_export] +macro_rules! prelude { + () => { + #[cfg(target_arch = "bpf")] + #[panic_handler] + fn panic(_info: &core::panic::PanicInfo) -> ! { + loop {} + } + + #[cfg(not(target_arch = "bpf"))] + fn main() { + panic!("This should only ever be called from its eBPF entrypoint") + } + }; +} + #[inline] fn insert( def: *mut bindings::bpf_map_def, diff --git a/test/integration-ebpf/src/bpf_probe_read.rs b/test/integration-ebpf/src/bpf_probe_read.rs index 12313974..e01021ed 100644 --- a/test/integration-ebpf/src/bpf_probe_read.rs +++ b/test/integration-ebpf/src/bpf_probe_read.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{ helpers::{bpf_probe_read_kernel_str_bytes, bpf_probe_read_user_str_bytes}, @@ -71,14 +72,3 @@ pub fn test_bpf_probe_read_kernel_str_bytes(ctx: ProbeContext) { ctx.arg::(0), ); } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/log.rs b/test/integration-ebpf/src/log.rs index 45364c79..7c4daf61 100644 --- a/test/integration-ebpf/src/log.rs +++ b/test/integration-ebpf/src/log.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use core::net::{IpAddr, Ipv4Addr, Ipv6Addr}; @@ -81,14 +82,3 @@ pub fn test_log(ctx: ProbeContext) { debug!(&ctx, "{:x}", no_copy.consume()); } } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/map_test.rs b/test/integration-ebpf/src/map_test.rs index 4a2e9be5..54db9f98 100644 --- a/test/integration-ebpf/src/map_test.rs +++ b/test/integration-ebpf/src/map_test.rs @@ -3,6 +3,7 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{ macros::{map, socket_filter}, @@ -35,14 +36,3 @@ pub fn simple_prog(_ctx: SkBuffContext) -> i64 { 0 } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/memmove_test.rs b/test/integration-ebpf/src/memmove_test.rs index 5d185fee..9fb65d84 100644 --- a/test/integration-ebpf/src/memmove_test.rs +++ b/test/integration-ebpf/src/memmove_test.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use core::mem; @@ -53,14 +54,3 @@ fn try_do_dnat(ctx: XdpContext) -> Result { } Ok(xdp_action::XDP_PASS) } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/name_test.rs b/test/integration-ebpf/src/name_test.rs index 25a8f906..dc5fa3ba 100644 --- a/test/integration-ebpf/src/name_test.rs +++ b/test/integration-ebpf/src/name_test.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext}; @@ -14,14 +15,3 @@ pub fn ihaveaverylongname(ctx: XdpContext) -> u32 { unsafe fn try_pass(_ctx: XdpContext) -> Result { Ok(xdp_action::XDP_PASS) } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/pass.rs b/test/integration-ebpf/src/pass.rs index a2d922b4..91d627d9 100644 --- a/test/integration-ebpf/src/pass.rs +++ b/test/integration-ebpf/src/pass.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext}; @@ -16,14 +17,3 @@ pub fn pass(ctx: XdpContext) -> u32 { unsafe fn try_pass(_ctx: XdpContext) -> Result { Ok(xdp_action::XDP_PASS) } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/raw_tracepoint.rs b/test/integration-ebpf/src/raw_tracepoint.rs index 3b790c80..24e6563d 100644 --- a/test/integration-ebpf/src/raw_tracepoint.rs +++ b/test/integration-ebpf/src/raw_tracepoint.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{ macros::{map, raw_tracepoint}, @@ -25,14 +26,3 @@ pub fn sys_enter(ctx: RawTracePointContext) -> i32 { 0 } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/redirect.rs b/test/integration-ebpf/src/redirect.rs index 941eb24f..2e16bc7b 100644 --- a/test/integration-ebpf/src/redirect.rs +++ b/test/integration-ebpf/src/redirect.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{ bindings::xdp_action, @@ -74,14 +75,3 @@ fn inc_hit(index: u32) { unsafe { *hit += 1 }; } } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/relocations.rs b/test/integration-ebpf/src/relocations.rs index b15324d5..37d03c98 100644 --- a/test/integration-ebpf/src/relocations.rs +++ b/test/integration-ebpf/src/relocations.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use core::hint; @@ -38,14 +39,3 @@ fn set_result(index: u32, value: u64) { fn set_result_backward(index: u32, value: u64) { set_result(index, value); } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/ring_buf.rs b/test/integration-ebpf/src/ring_buf.rs index 4cf721fe..609784db 100644 --- a/test/integration-ebpf/src/ring_buf.rs +++ b/test/integration-ebpf/src/ring_buf.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{ macros::{map, uprobe}, @@ -45,14 +46,3 @@ pub fn ring_buf_test(ctx: ProbeContext) { entry.discard(0); } } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/simple_prog.rs b/test/integration-ebpf/src/simple_prog.rs index 1a97033c..79c968dc 100644 --- a/test/integration-ebpf/src/simple_prog.rs +++ b/test/integration-ebpf/src/simple_prog.rs @@ -3,6 +3,7 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{macros::socket_filter, programs::SkBuffContext}; @@ -11,14 +12,3 @@ use aya_ebpf::{macros::socket_filter, programs::SkBuffContext}; pub fn simple_prog(_ctx: SkBuffContext) -> i64 { 0 } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/strncmp.rs b/test/integration-ebpf/src/strncmp.rs index fe3b1009..157e31fc 100644 --- a/test/integration-ebpf/src/strncmp.rs +++ b/test/integration-ebpf/src/strncmp.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{ cty::c_long, @@ -26,14 +27,3 @@ pub fn test_bpf_strncmp(ctx: ProbeContext) -> Result<(), c_long> { Ok(()) } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/tcx.rs b/test/integration-ebpf/src/tcx.rs index 55a7971c..6df69dee 100644 --- a/test/integration-ebpf/src/tcx.rs +++ b/test/integration-ebpf/src/tcx.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{bindings::tcx_action_base::TCX_NEXT, macros::classifier, programs::TcContext}; @@ -7,14 +8,3 @@ use aya_ebpf::{bindings::tcx_action_base::TCX_NEXT, macros::classifier, programs pub fn tcx_next(_ctx: TcContext) -> i32 { TCX_NEXT } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/test.rs b/test/integration-ebpf/src/test.rs index b21a6ff9..70b0a585 100644 --- a/test/integration-ebpf/src/test.rs +++ b/test/integration-ebpf/src/test.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{ bindings::{bpf_ret_code, xdp_action}, @@ -52,14 +53,3 @@ pub fn test_flow(_ctx: FlowDissectorContext) -> u32 { // Linux kernel for inspiration. bpf_ret_code::BPF_FLOW_DISSECTOR_CONTINUE } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/two_progs.rs b/test/integration-ebpf/src/two_progs.rs index 039f8a91..18fecf85 100644 --- a/test/integration-ebpf/src/two_progs.rs +++ b/test/integration-ebpf/src/two_progs.rs @@ -2,6 +2,7 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{macros::tracepoint, programs::TracePointContext}; @@ -13,14 +14,3 @@ pub fn test_tracepoint_one(_ctx: TracePointContext) -> u32 { pub fn test_tracepoint_two(_ctx: TracePointContext) -> u32 { 0 } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/uprobe_cookie.rs b/test/integration-ebpf/src/uprobe_cookie.rs index a01a90bf..127c40cc 100644 --- a/test/integration-ebpf/src/uprobe_cookie.rs +++ b/test/integration-ebpf/src/uprobe_cookie.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{ EbpfContext as _, helpers, @@ -17,14 +18,3 @@ pub fn uprobe_cookie(ctx: ProbeContext) { let cookie_bytes = cookie.to_le_bytes(); let _res = RING_BUF.output(&cookie_bytes, 0); } - -#[cfg(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -} diff --git a/test/integration-ebpf/src/xdp_sec.rs b/test/integration-ebpf/src/xdp_sec.rs index 98fb1f57..efb7dc45 100644 --- a/test/integration-ebpf/src/xdp_sec.rs +++ b/test/integration-ebpf/src/xdp_sec.rs @@ -1,5 +1,6 @@ #![cfg_attr(target_arch = "bpf", no_std)] #![cfg_attr(target_arch = "bpf", no_main)] +aya_ebpf::prelude!(); use aya_ebpf::{bindings::xdp_action::XDP_PASS, macros::xdp, programs::XdpContext}; @@ -18,14 +19,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(target_arch = "bpf")] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -#[cfg(not(target_arch = "bpf"))] -fn main() { - panic!("This should only ever be called from its eBPF entrypoint") -}