aya-ebpf: introduce `panic_handler` macro

All eBPF crates need to define a panic-handler.
To deduplicate that code, we introduce a macro for it.
reviewable/pr1229/r12
Thomas Eizinger 3 weeks ago
parent 9eecbe9d0e
commit 5757a96353
No known key found for this signature in database
GPG Key ID: 05633CD77196CAF3

@ -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] #[inline]
fn insert<K, V>( fn insert<K, V>(
def: *mut bindings::bpf_map_def, def: *mut bindings::bpf_map_def,

@ -72,8 +72,4 @@ pub fn test_bpf_probe_read_kernel_str_bytes(ctx: ProbeContext) {
); );
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -82,8 +82,4 @@ pub fn test_log(ctx: ProbeContext) {
} }
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -36,8 +36,4 @@ pub fn simple_prog(_ctx: SkBuffContext) -> i64 {
0 0
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -54,8 +54,4 @@ fn try_do_dnat(ctx: XdpContext) -> Result<u32, ()> {
Ok(xdp_action::XDP_PASS) Ok(xdp_action::XDP_PASS)
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -15,8 +15,4 @@ unsafe fn try_pass(_ctx: XdpContext) -> Result<u32, u32> {
Ok(xdp_action::XDP_PASS) Ok(xdp_action::XDP_PASS)
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -17,8 +17,4 @@ unsafe fn try_pass(_ctx: XdpContext) -> Result<u32, u32> {
Ok(xdp_action::XDP_PASS) Ok(xdp_action::XDP_PASS)
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -26,8 +26,4 @@ pub fn sys_enter(ctx: RawTracePointContext) -> i32 {
0 0
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -75,8 +75,4 @@ fn inc_hit(index: u32) {
} }
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -39,8 +39,4 @@ fn set_result_backward(index: u32, value: u64) {
set_result(index, value); set_result(index, value);
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -46,8 +46,4 @@ pub fn ring_buf_test(ctx: ProbeContext) {
} }
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -12,8 +12,4 @@ pub fn simple_prog(_ctx: SkBuffContext) -> i64 {
0 0
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -27,8 +27,4 @@ pub fn test_bpf_strncmp(ctx: ProbeContext) -> Result<(), c_long> {
Ok(()) Ok(())
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -8,8 +8,4 @@ pub fn tcx_next(_ctx: TcContext) -> i32 {
TCX_NEXT TCX_NEXT
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -53,8 +53,4 @@ pub fn test_flow(_ctx: FlowDissectorContext) -> u32 {
bpf_ret_code::BPF_FLOW_DISSECTOR_CONTINUE bpf_ret_code::BPF_FLOW_DISSECTOR_CONTINUE
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -14,8 +14,4 @@ pub fn test_tracepoint_two(_ctx: TracePointContext) -> u32 {
0 0
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -18,8 +18,4 @@ pub fn uprobe_cookie(ctx: ProbeContext) {
let _res = RING_BUF.output(&cookie_bytes, 0); let _res = RING_BUF.output(&cookie_bytes, 0);
} }
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -19,8 +19,4 @@ probe!(xdp_devmap, (map = "devmap"));
probe!(xdp_frags_cpumap, (frags, map = "cpumap")); probe!(xdp_frags_cpumap, (frags, map = "cpumap"));
probe!(xdp_frags_devmap, (frags, map = "devmap")); probe!(xdp_frags_devmap, (frags, map = "devmap"));
#[cfg(not(test))] aya_ebpf::panic_handler!();
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}

@ -2728,6 +2728,7 @@ pub fn aya_ebpf::programs::xdp::XdpContext::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya_ebpf::programs::xdp::XdpContext impl<T> core::convert::From<T> for aya_ebpf::programs::xdp::XdpContext
pub fn aya_ebpf::programs::xdp::XdpContext::from(t: T) -> T pub fn aya_ebpf::programs::xdp::XdpContext::from(t: T) -> T
pub macro aya_ebpf::bpf_printk! pub macro aya_ebpf::bpf_printk!
pub macro aya_ebpf::panic_handler!
pub struct aya_ebpf::PtRegs pub struct aya_ebpf::PtRegs
impl aya_ebpf::PtRegs impl aya_ebpf::PtRegs
pub fn aya_ebpf::PtRegs::arg<T: aya_ebpf::args::FromPtRegs>(&self, n: usize) -> core::option::Option<T> pub fn aya_ebpf::PtRegs::arg<T: aya_ebpf::args::FromPtRegs>(&self, n: usize) -> core::option::Option<T>

Loading…
Cancel
Save