diff --git a/ebpf/aya-ebpf/src/lib.rs b/ebpf/aya-ebpf/src/lib.rs index 4ff3c40b..f6d3a766 100644 --- a/ebpf/aya-ebpf/src/lib.rs +++ b/ebpf/aya-ebpf/src/lib.rs @@ -133,6 +133,24 @@ pub fn check_bounds_signed(value: i64, lower: i64, upper: i64) -> bool { } } +#[macro_export] +macro_rules! panic_handler { + () => { + /// Defines the panic handler when compiling for eBPF. + /// + /// eBPF programs don't support the concept of a panic and therefore, compiling a program that includes panics should fail at link time. + /// + /// Yet, as part of compiling for `no_std`, we need to define a panic handler. + /// The eBPF verifier enforces that programs terminate and will thus reject unbounded loops. + /// By using an endless loop within our panic handler, we can ensure that if this panic handler were somehow linked into the program, the eBPF verifier would reject it. + #[cfg(not(test))] + #[panic_handler] + fn panic(_info: &core::panic::PanicInfo) -> ! { + loop {} + } + }; +} + #[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 a721163c..53908d00 100644 --- a/test/integration-ebpf/src/bpf_probe_read.rs +++ b/test/integration-ebpf/src/bpf_probe_read.rs @@ -72,8 +72,4 @@ pub fn test_bpf_probe_read_kernel_str_bytes(ctx: ProbeContext) { ); } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/log.rs b/test/integration-ebpf/src/log.rs index b6471f3f..55d51677 100644 --- a/test/integration-ebpf/src/log.rs +++ b/test/integration-ebpf/src/log.rs @@ -82,8 +82,4 @@ pub fn test_log(ctx: ProbeContext) { } } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/map_test.rs b/test/integration-ebpf/src/map_test.rs index e53b2f85..0d10b5ae 100644 --- a/test/integration-ebpf/src/map_test.rs +++ b/test/integration-ebpf/src/map_test.rs @@ -36,8 +36,4 @@ pub fn simple_prog(_ctx: SkBuffContext) -> i64 { 0 } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/memmove_test.rs b/test/integration-ebpf/src/memmove_test.rs index f9002131..0a2748bb 100644 --- a/test/integration-ebpf/src/memmove_test.rs +++ b/test/integration-ebpf/src/memmove_test.rs @@ -54,8 +54,4 @@ fn try_do_dnat(ctx: XdpContext) -> Result { Ok(xdp_action::XDP_PASS) } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/name_test.rs b/test/integration-ebpf/src/name_test.rs index d1b48950..0e5a8d90 100644 --- a/test/integration-ebpf/src/name_test.rs +++ b/test/integration-ebpf/src/name_test.rs @@ -15,8 +15,4 @@ unsafe fn try_pass(_ctx: XdpContext) -> Result { Ok(xdp_action::XDP_PASS) } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/pass.rs b/test/integration-ebpf/src/pass.rs index 795d82b0..0b0371b4 100644 --- a/test/integration-ebpf/src/pass.rs +++ b/test/integration-ebpf/src/pass.rs @@ -17,8 +17,4 @@ unsafe fn try_pass(_ctx: XdpContext) -> Result { Ok(xdp_action::XDP_PASS) } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/raw_tracepoint.rs b/test/integration-ebpf/src/raw_tracepoint.rs index 1db9a21b..4c1917f0 100644 --- a/test/integration-ebpf/src/raw_tracepoint.rs +++ b/test/integration-ebpf/src/raw_tracepoint.rs @@ -26,8 +26,4 @@ pub fn sys_enter(ctx: RawTracePointContext) -> i32 { 0 } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/redirect.rs b/test/integration-ebpf/src/redirect.rs index eb32f999..88e67f76 100644 --- a/test/integration-ebpf/src/redirect.rs +++ b/test/integration-ebpf/src/redirect.rs @@ -75,8 +75,4 @@ fn inc_hit(index: u32) { } } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/relocations.rs b/test/integration-ebpf/src/relocations.rs index dc9e71ca..d2f4fcdf 100644 --- a/test/integration-ebpf/src/relocations.rs +++ b/test/integration-ebpf/src/relocations.rs @@ -39,8 +39,4 @@ fn set_result_backward(index: u32, value: u64) { set_result(index, value); } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/ring_buf.rs b/test/integration-ebpf/src/ring_buf.rs index 6674f5e6..5a508046 100644 --- a/test/integration-ebpf/src/ring_buf.rs +++ b/test/integration-ebpf/src/ring_buf.rs @@ -46,8 +46,4 @@ pub fn ring_buf_test(ctx: ProbeContext) { } } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/simple_prog.rs b/test/integration-ebpf/src/simple_prog.rs index 98725dce..df21a7ff 100644 --- a/test/integration-ebpf/src/simple_prog.rs +++ b/test/integration-ebpf/src/simple_prog.rs @@ -12,8 +12,4 @@ pub fn simple_prog(_ctx: SkBuffContext) -> i64 { 0 } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/strncmp.rs b/test/integration-ebpf/src/strncmp.rs index a19b10d2..39157d46 100644 --- a/test/integration-ebpf/src/strncmp.rs +++ b/test/integration-ebpf/src/strncmp.rs @@ -27,8 +27,4 @@ pub fn test_bpf_strncmp(ctx: ProbeContext) -> Result<(), c_long> { Ok(()) } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/tcx.rs b/test/integration-ebpf/src/tcx.rs index 5ed3c211..3b9953ad 100644 --- a/test/integration-ebpf/src/tcx.rs +++ b/test/integration-ebpf/src/tcx.rs @@ -8,8 +8,4 @@ pub fn tcx_next(_ctx: TcContext) -> i32 { TCX_NEXT } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/test.rs b/test/integration-ebpf/src/test.rs index 417da0de..8a944309 100644 --- a/test/integration-ebpf/src/test.rs +++ b/test/integration-ebpf/src/test.rs @@ -53,8 +53,4 @@ pub fn test_flow(_ctx: FlowDissectorContext) -> u32 { bpf_ret_code::BPF_FLOW_DISSECTOR_CONTINUE } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/two_progs.rs b/test/integration-ebpf/src/two_progs.rs index 17d08168..d727c809 100644 --- a/test/integration-ebpf/src/two_progs.rs +++ b/test/integration-ebpf/src/two_progs.rs @@ -14,8 +14,4 @@ pub fn test_tracepoint_two(_ctx: TracePointContext) -> u32 { 0 } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/uprobe_cookie.rs b/test/integration-ebpf/src/uprobe_cookie.rs index 19713f85..39f47b5d 100644 --- a/test/integration-ebpf/src/uprobe_cookie.rs +++ b/test/integration-ebpf/src/uprobe_cookie.rs @@ -18,8 +18,4 @@ pub fn uprobe_cookie(ctx: ProbeContext) { let _res = RING_BUF.output(&cookie_bytes, 0); } -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} +aya_ebpf::panic_handler!(); diff --git a/test/integration-ebpf/src/xdp_sec.rs b/test/integration-ebpf/src/xdp_sec.rs index c9eed920..9d442342 100644 --- a/test/integration-ebpf/src/xdp_sec.rs +++ b/test/integration-ebpf/src/xdp_sec.rs @@ -19,8 +19,4 @@ 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 {} -} +aya_ebpf::panic_handler!(); diff --git a/xtask/public-api/aya-ebpf.txt b/xtask/public-api/aya-ebpf.txt index aa92abd4..90255811 100644 --- a/xtask/public-api/aya-ebpf.txt +++ b/xtask/public-api/aya-ebpf.txt @@ -2728,6 +2728,7 @@ pub fn aya_ebpf::programs::xdp::XdpContext::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya_ebpf::programs::xdp::XdpContext pub fn aya_ebpf::programs::xdp::XdpContext::from(t: T) -> T pub macro aya_ebpf::bpf_printk! +pub macro aya_ebpf::panic_handler! pub struct aya_ebpf::PtRegs impl aya_ebpf::PtRegs pub fn aya_ebpf::PtRegs::arg(&self, n: usize) -> core::option::Option