aya-ebpf: put mem{set,move,cpy} behind cfg(target_arch = "bpf")

Address some lints while I'm here.
pull/1251/head
Tamir Duberstein 6 days ago
parent eab5661a0e
commit 6004fcdb0f

@ -27,7 +27,7 @@ use core::{ffi::c_void, ptr::NonNull};
pub use aya_ebpf_cty as cty; pub use aya_ebpf_cty as cty;
pub use aya_ebpf_macros as macros; pub use aya_ebpf_macros as macros;
use cty::{c_int, c_long}; use cty::c_long;
use helpers::{ use helpers::{
bpf_get_current_comm, bpf_get_current_pid_tgid, bpf_get_current_uid_gid, bpf_map_delete_elem, bpf_get_current_comm, bpf_get_current_pid_tgid, bpf_get_current_uid_gid, bpf_map_delete_elem,
bpf_map_lookup_elem, bpf_map_update_elem, bpf_map_lookup_elem, bpf_map_update_elem,
@ -60,51 +60,54 @@ pub trait EbpfContext {
} }
} }
#[unsafe(no_mangle)] #[cfg_attr(target_arch = "bpf", expect(clippy::missing_safety_doc))]
#[expect(clippy::missing_safety_doc, unsafe_op_in_unsafe_fn)] mod intrinsics {
pub unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) { use super::cty::c_int;
#[expect(clippy::cast_sign_loss)]
let b = c as u8; #[unsafe(no_mangle)]
for i in 0..n { pub unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) {
*s.add(i) = b; #[expect(clippy::cast_sign_loss)]
let b = c as u8;
for i in 0..n {
unsafe { *s.add(i) = b }
}
} }
}
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
#[expect(clippy::missing_safety_doc, unsafe_op_in_unsafe_fn)] pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *mut u8, n: usize) {
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *mut u8, n: usize) { unsafe { copy_forward(dest, src, n) }
copy_forward(dest, src, n); }
}
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
#[expect(clippy::missing_safety_doc, unsafe_op_in_unsafe_fn)] pub unsafe extern "C" fn memmove(dest: *mut u8, src: *mut u8, n: usize) {
pub unsafe extern "C" fn memmove(dest: *mut u8, src: *mut u8, n: usize) { let delta = (dest as usize).wrapping_sub(src as usize);
let delta = (dest as usize).wrapping_sub(src as usize); if delta >= n {
if delta >= n { // We can copy forwards because either dest is far enough ahead of src,
// We can copy forwards because either dest is far enough ahead of src, // or src is ahead of dest (and delta overflowed).
// or src is ahead of dest (and delta overflowed). unsafe { copy_forward(dest, src, n) }
copy_forward(dest, src, n); } else {
} else { unsafe { copy_backward(dest, src, n) }
copy_backward(dest, src, n); }
} }
}
#[inline(always)] #[inline(always)]
#[expect(unsafe_op_in_unsafe_fn)] unsafe fn copy_forward(dest: *mut u8, src: *mut u8, n: usize) {
unsafe fn copy_forward(dest: *mut u8, src: *mut u8, n: usize) { for i in 0..n {
for i in 0..n { unsafe { *dest.add(i) = *src.add(i) }
*dest.add(i) = *src.add(i); }
} }
}
#[inline(always)] #[inline(always)]
#[expect(unsafe_op_in_unsafe_fn)] unsafe fn copy_backward(dest: *mut u8, src: *mut u8, n: usize) {
unsafe fn copy_backward(dest: *mut u8, src: *mut u8, n: usize) { for i in (0..n).rev() {
for i in (0..n).rev() { unsafe { *dest.add(i) = *src.add(i) }
*dest.add(i) = *src.add(i); }
} }
} }
#[cfg(target_arch = "bpf")]
pub use intrinsics::*;
/// Check if a value is within a range, using conditional forms compatible with /// Check if a value is within a range, using conditional forms compatible with
/// the verifier. /// the verifier.
#[inline(always)] #[inline(always)]

@ -2833,6 +2833,3 @@ pub fn aya_ebpf::programs::tracepoint::TracePointContext::as_ptr(&self) -> *mut
impl aya_ebpf::EbpfContext for aya_ebpf::programs::xdp::XdpContext impl aya_ebpf::EbpfContext for aya_ebpf::programs::xdp::XdpContext
pub fn aya_ebpf::programs::xdp::XdpContext::as_ptr(&self) -> *mut core::ffi::c_void pub fn aya_ebpf::programs::xdp::XdpContext::as_ptr(&self) -> *mut core::ffi::c_void
pub fn aya_ebpf::check_bounds_signed(value: i64, lower: i64, upper: i64) -> bool pub fn aya_ebpf::check_bounds_signed(value: i64, lower: i64, upper: i64) -> bool
#[no_mangle] pub unsafe c fn aya_ebpf::memcpy(dest: *mut u8, src: *mut u8, n: usize)
#[no_mangle] pub unsafe c fn aya_ebpf::memmove(dest: *mut u8, src: *mut u8, n: usize)
#[no_mangle] pub unsafe c fn aya_ebpf::memset(s: *mut u8, c: aya_ebpf_cty::ad::c_int, n: usize)

Loading…
Cancel
Save