Skip to content

Commit 08bced8

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 1bc9a1a commit 08bced8

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
@@ -25,7 +25,7 @@ pub mod programs;
2525
pub use aya_bpf_cty as cty;
2626

2727
use core::ffi::c_void;
28-
use cty::{c_int, c_long};
28+
use cty::c_long;
2929
use helpers::{bpf_get_current_comm, bpf_get_current_pid_tgid, bpf_get_current_uid_gid};
3030

3131
pub use aya_bpf_macros as macros;
@@ -57,22 +57,6 @@ pub trait BpfContext {
5757
}
5858
}
5959

60-
#[no_mangle]
61-
pub unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) {
62-
#[allow(clippy::cast_sign_loss)]
63-
let b = c as u8;
64-
for i in 0..n {
65-
*s.add(i) = b;
66-
}
67-
}
68-
69-
#[no_mangle]
70-
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *mut u8, n: usize) {
71-
for i in 0..n {
72-
*dest.add(i) = *src.add(i);
73-
}
74-
}
75-
7660
/// Check if a value is within a range, using conditional forms compatible with
7761
/// the verifier.
7862
#[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
@@ -2285,5 +2285,3 @@ pub fn aya_bpf::programs::tracepoint::TracePointContext::as_ptr(&self) -> *mut c
22852285
impl aya_bpf::BpfContext for aya_bpf::programs::xdp::XdpContext
22862286
pub fn aya_bpf::programs::xdp::XdpContext::as_ptr(&self) -> *mut core::ffi::c_void
22872287
pub fn aya_bpf::check_bounds_signed(value: i64, lower: i64, upper: i64) -> bool
2288-
#[no_mangle] pub unsafe c fn aya_bpf::memcpy(dest: *mut u8, src: *mut u8, n: usize)
2289-
#[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)