Skip to content

Commit 9fc355f

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 ab38afe commit 9fc355f

23 files changed

+43
-65
lines changed

ebpf/aya-ebpf/src/lib.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -69,50 +69,6 @@ pub trait EbpfContext {
6969
}
7070
}
7171

72-
mod intrinsics {
73-
use super::cty::c_int;
74-
75-
#[unsafe(no_mangle)]
76-
unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) {
77-
#[expect(clippy::cast_sign_loss)]
78-
let b = c as u8;
79-
for i in 0..n {
80-
unsafe { *s.add(i) = b }
81-
}
82-
}
83-
84-
#[unsafe(no_mangle)]
85-
unsafe extern "C" fn memcpy(dest: *mut u8, src: *mut u8, n: usize) {
86-
unsafe { copy_forward(dest, src, n) }
87-
}
88-
89-
#[unsafe(no_mangle)]
90-
unsafe extern "C" fn memmove(dest: *mut u8, src: *mut u8, n: usize) {
91-
let delta = (dest as usize).wrapping_sub(src as usize);
92-
if delta >= n {
93-
// We can copy forwards because either dest is far enough ahead of src,
94-
// or src is ahead of dest (and delta overflowed).
95-
unsafe { copy_forward(dest, src, n) }
96-
} else {
97-
unsafe { copy_backward(dest, src, n) }
98-
}
99-
}
100-
101-
#[inline(always)]
102-
unsafe fn copy_forward(dest: *mut u8, src: *mut u8, n: usize) {
103-
for i in 0..n {
104-
unsafe { *dest.add(i) = *src.add(i) }
105-
}
106-
}
107-
108-
#[inline(always)]
109-
unsafe fn copy_backward(dest: *mut u8, src: *mut u8, n: usize) {
110-
for i in (0..n).rev() {
111-
unsafe { *dest.add(i) = *src.add(i) }
112-
}
113-
}
114-
}
115-
11672
/// Check if a value is within a range, using conditional forms compatible with
11773
/// the verifier.
11874
#[inline(always)]

test/integration-ebpf/src/array.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
#![expect(unused_crate_dependencies, reason = "used in other bins")]
45

56
#[cfg(not(test))]

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
#![expect(unused_crate_dependencies, reason = "used in other bins")]
45

56
use aya_ebpf::{

test/integration-ebpf/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![no_builtins]
12
#![no_std]
23
#![expect(unused_crate_dependencies, reason = "used in bins")]
34

test/integration-ebpf/src/linear_data_structures.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
#![expect(unused_crate_dependencies, reason = "used in other bins")]
45

56
#[cfg(not(test))]

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
#![expect(unused_crate_dependencies, reason = "used in other bins")]
45

56
use core::{

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
#![expect(unused_crate_dependencies, reason = "used in other bins")]
45

56
use aya_ebpf::{

test/integration-ebpf/src/memmove_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
#![expect(unused_crate_dependencies, reason = "used in other bins")]
45

56
use core::mem;

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
#![expect(unused_crate_dependencies, reason = "used in other bins")]
45

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

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
#![expect(unused_crate_dependencies, reason = "used in other bins")]
45

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

0 commit comments

Comments
 (0)