Skip to content

Commit

Permalink
bpf: Remove builtin global functions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tamird committed Jul 27, 2023
1 parent 1bc9a1a commit 8234717
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 24 deletions.
18 changes: 1 addition & 17 deletions bpf/aya-bpf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub mod programs;
pub use aya_bpf_cty as cty;

use core::ffi::c_void;
use cty::{c_int, c_long};
use cty::c_long;
use helpers::{bpf_get_current_comm, bpf_get_current_pid_tgid, bpf_get_current_uid_gid};

pub use aya_bpf_macros as macros;
Expand Down Expand Up @@ -57,22 +57,6 @@ pub trait BpfContext {
}
}

#[no_mangle]
pub unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) {
#[allow(clippy::cast_sign_loss)]
let b = c as u8;
for i in 0..n {
*s.add(i) = b;
}
}

#[no_mangle]
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *mut u8, n: usize) {
for i in 0..n {
*dest.add(i) = *src.add(i);
}
}

/// Check if a value is within a range, using conditional forms compatible with
/// the verifier.
#[inline(always)]
Expand Down
3 changes: 2 additions & 1 deletion test/integration-ebpf/src/bpf_probe_read.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]
#![no_builtins]
#![no_main]
#![no_std]

use aya_bpf::{
helpers::{bpf_probe_read_kernel_str_bytes, bpf_probe_read_user_str_bytes},
Expand Down
3 changes: 2 additions & 1 deletion test/integration-ebpf/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]
#![no_builtins]
#![no_main]
#![no_std]

use aya_bpf::{macros::uprobe, programs::ProbeContext};
use aya_log_ebpf::{debug, error, info, trace, warn};
Expand Down
3 changes: 2 additions & 1 deletion test/integration-ebpf/src/map_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]
#![no_builtins]
#![no_main]
#![no_std]

use aya_bpf::{
bindings::xdp_action,
Expand Down
3 changes: 2 additions & 1 deletion test/integration-ebpf/src/name_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]
#![no_builtins]
#![no_main]
#![no_std]

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

Expand Down
3 changes: 2 additions & 1 deletion test/integration-ebpf/src/pass.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]
#![no_builtins]
#![no_main]
#![no_std]

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

Expand Down
3 changes: 2 additions & 1 deletion test/integration-ebpf/src/relocations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]
#![no_builtins]
#![no_main]
#![no_std]

use core::hint;

Expand Down
3 changes: 2 additions & 1 deletion test/integration-ebpf/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]
#![no_builtins]
#![no_main]
#![no_std]

use aya_bpf::{
bindings::xdp_action,
Expand Down

0 comments on commit 8234717

Please sign in to comment.