Skip to content

Commit e9a099f

Browse files
committed
bpf: Remove builtin global functions
This commit removes memset and memcpy, relying instead on implementations provided by std/compiler-builtins. This commit adds `#![no_builtins]` to all the BPF programs written in Rust, and the same should be propagated to aya-template and all examples in the book and elsewhere before this commit is merged. It turns out that without the `#![no_builtins]` annotation rustc generates LLVM IR that calls LLVM intrinsics rather than libcalls. These may end up as libcalls after lowering, but not before emitting errors in BPF lowering[0]. This works thanks to rust-lang/rust#113716 which causes `#![no_builtins]` to behave similarly to `-fno-builtin` in clang, which was added in https://reviews.llvm.org/D68028 with similar motivation. This commit implies that we now require rustc nightly >= 2023-07-20. [0] https://github.com/llvm/llvm-project/blob/7b2745b/llvm/lib/Target/BPF/BPFISelLowering.cpp#L472-L474
1 parent e68fa14 commit e9a099f

File tree

9 files changed

+15
-26
lines changed

9 files changed

+15
-26
lines changed

bpf/aya-bpf/src/lib.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use core::ffi::c_void;
3131

3232
pub use aya_bpf_cty as cty;
3333
pub use aya_bpf_macros as macros;
34-
use cty::{c_int, c_long};
34+
use cty::c_long;
3535
use helpers::{bpf_get_current_comm, bpf_get_current_pid_tgid, bpf_get_current_uid_gid};
3636

3737
pub const TASK_COMM_LEN: usize = 16;
@@ -61,22 +61,6 @@ pub trait BpfContext {
6161
}
6262
}
6363

64-
#[no_mangle]
65-
pub unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) {
66-
#[allow(clippy::cast_sign_loss)]
67-
let b = c as u8;
68-
for i in 0..n {
69-
*s.add(i) = b;
70-
}
71-
}
72-
73-
#[no_mangle]
74-
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *mut u8, n: usize) {
75-
for i in 0..n {
76-
*dest.add(i) = *src.add(i);
77-
}
78-
}
79-
8064
/// Check if a value is within a range, using conditional forms compatible with
8165
/// the verifier.
8266
#[inline(always)]

test/integration-ebpf/src/bpf_probe_read.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
1+
#![no_builtins]
22
#![no_main]
3+
#![no_std]
34

45
use aya_bpf::{
56
helpers::{bpf_probe_read_kernel_str_bytes, bpf_probe_read_user_str_bytes},

test/integration-ebpf/src/log.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
1+
#![no_builtins]
22
#![no_main]
3+
#![no_std]
34

45
use aya_bpf::{macros::uprobe, programs::ProbeContext};
56
use aya_log_ebpf::{debug, error, info, trace, warn};

test/integration-ebpf/src/map_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
1+
#![no_builtins]
22
#![no_main]
3+
#![no_std]
34

45
use aya_bpf::{
56
bindings::xdp_action,

test/integration-ebpf/src/name_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
1+
#![no_builtins]
22
#![no_main]
3+
#![no_std]
34

45
use aya_bpf::{bindings::xdp_action, macros::xdp, programs::XdpContext};
56

test/integration-ebpf/src/pass.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
1+
#![no_builtins]
22
#![no_main]
3+
#![no_std]
34

45
use aya_bpf::{bindings::xdp_action, macros::xdp, programs::XdpContext};
56

test/integration-ebpf/src/relocations.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
1+
#![no_builtins]
22
#![no_main]
3+
#![no_std]
34

45
use core::hint;
56

test/integration-ebpf/src/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#![no_std]
1+
#![no_builtins]
22
#![no_main]
3+
#![no_std]
34

45
use aya_bpf::{
56
bindings::xdp_action,

xtask/public-api/aya-bpf.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,5 +2587,3 @@ pub fn aya_bpf::programs::tracepoint::TracePointContext::as_ptr(&self) -> *mut c
25872587
impl aya_bpf::BpfContext for aya_bpf::programs::xdp::XdpContext
25882588
pub fn aya_bpf::programs::xdp::XdpContext::as_ptr(&self) -> *mut core::ffi::c_void
25892589
pub fn aya_bpf::check_bounds_signed(value: i64, lower: i64, upper: i64) -> bool
2590-
#[no_mangle] pub unsafe c fn aya_bpf::memcpy(dest: *mut u8, src: *mut u8, n: usize)
2591-
#[no_mangle] pub unsafe c fn aya_bpf::memset(s: *mut u8, c: aya_bpf_cty::ad::c_int, n: usize)

0 commit comments

Comments
 (0)