Feature-gate `no_std` and `no_main`

The `no_std` and `no_main` attributes are only needed when we compile
the eBPF kernels for the "bpf" architecture. By feature-gating them on
the target architecture, we ensure the kernels compile just fine on a
host-architecture like x64. They won't be able to do anything
meaningfully but it is useful within the context of the larger workspace
they are embedded in as it allows `cargo build --workspace` and `cargo
test --workspace` to just work.
reviewable/pr1229/r2
Thomas Eizinger 3 weeks ago
parent c005ad30ed
commit 267796f1f5
No known key found for this signature in database
GPG Key ID: 05633CD77196CAF3

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{ use aya_ebpf::{
helpers::{bpf_probe_read_kernel_str_bytes, bpf_probe_read_user_str_bytes}, helpers::{bpf_probe_read_kernel_str_bytes, bpf_probe_read_user_str_bytes},
@ -77,3 +77,8 @@ pub fn test_bpf_probe_read_kernel_str_bytes(ctx: ProbeContext) {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,3 +1,3 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
// This file exists to enable the library target. // This file exists to enable the library target.

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
@ -87,3 +87,8 @@ pub fn test_log(ctx: ProbeContext) {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,8 +1,8 @@
// Socket Filter program for testing with an arbitrary program with maps. // Socket Filter program for testing with an arbitrary program with maps.
// This is mainly used in tests with consideration for old kernels. // This is mainly used in tests with consideration for old kernels.
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{ use aya_ebpf::{
macros::{map, socket_filter}, macros::{map, socket_filter},
@ -41,3 +41,8 @@ pub fn simple_prog(_ctx: SkBuffContext) -> i64 {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use core::mem; use core::mem;
@ -59,3 +59,8 @@ fn try_do_dnat(ctx: XdpContext) -> Result<u32, ()> {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext}; use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext};
@ -20,3 +20,8 @@ unsafe fn try_pass(_ctx: XdpContext) -> Result<u32, u32> {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext}; use aya_ebpf::{bindings::xdp_action, macros::xdp, programs::XdpContext};
@ -22,3 +22,8 @@ unsafe fn try_pass(_ctx: XdpContext) -> Result<u32, u32> {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{ use aya_ebpf::{
macros::{map, raw_tracepoint}, macros::{map, raw_tracepoint},
@ -31,3 +31,8 @@ pub fn sys_enter(ctx: RawTracePointContext) -> i32 {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{ use aya_ebpf::{
bindings::xdp_action, bindings::xdp_action,
@ -80,3 +80,8 @@ fn inc_hit(index: u32) {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use core::hint; use core::hint;
@ -44,3 +44,8 @@ fn set_result_backward(index: u32, value: u64) {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{ use aya_ebpf::{
macros::{map, uprobe}, macros::{map, uprobe},
@ -51,3 +51,8 @@ pub fn ring_buf_test(ctx: ProbeContext) {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,8 +1,8 @@
// Socket Filter program for testing with an arbitrary program. // Socket Filter program for testing with an arbitrary program.
// This is mainly used in tests with consideration for old kernels. // This is mainly used in tests with consideration for old kernels.
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{macros::socket_filter, programs::SkBuffContext}; use aya_ebpf::{macros::socket_filter, programs::SkBuffContext};
@ -17,3 +17,8 @@ pub fn simple_prog(_ctx: SkBuffContext) -> i64 {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{ use aya_ebpf::{
cty::c_long, cty::c_long,
@ -32,3 +32,8 @@ pub fn test_bpf_strncmp(ctx: ProbeContext) -> Result<(), c_long> {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{bindings::tcx_action_base::TCX_NEXT, macros::classifier, programs::TcContext}; use aya_ebpf::{bindings::tcx_action_base::TCX_NEXT, macros::classifier, programs::TcContext};
@ -13,3 +13,8 @@ pub fn tcx_next(_ctx: TcContext) -> i32 {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{ use aya_ebpf::{
bindings::{bpf_ret_code, xdp_action}, bindings::{bpf_ret_code, xdp_action},
@ -58,3 +58,8 @@ pub fn test_flow(_ctx: FlowDissectorContext) -> u32 {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,7 +1,7 @@
// Two programs in the same ELF section // Two programs in the same ELF section
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{macros::tracepoint, programs::TracePointContext}; use aya_ebpf::{macros::tracepoint, programs::TracePointContext};
@ -19,3 +19,8 @@ pub fn test_tracepoint_two(_ctx: TracePointContext) -> u32 {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{ use aya_ebpf::{
EbpfContext as _, helpers, EbpfContext as _, helpers,
@ -23,3 +23,8 @@ pub fn uprobe_cookie(ctx: ProbeContext) {
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

@ -1,5 +1,5 @@
#![no_std] #![cfg_attr(target_arch = "bpf", no_std)]
#![no_main] #![cfg_attr(target_arch = "bpf", no_main)]
use aya_ebpf::{bindings::xdp_action::XDP_PASS, macros::xdp, programs::XdpContext}; use aya_ebpf::{bindings::xdp_action::XDP_PASS, macros::xdp, programs::XdpContext};
@ -24,3 +24,8 @@ probe!(xdp_frags_devmap, (frags, map = "devmap"));
fn panic(_info: &core::panic::PanicInfo) -> ! { fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[cfg(not(target_arch = "bpf"))]
fn main() {
panic!("This should only ever be called from its eBPF entrypoint")
}

Loading…
Cancel
Save