From 3847281b15dbd61a9f8e137f4db10471682822ca Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Wed, 16 Jul 2025 10:41:38 -0400 Subject: [PATCH] aya-log: add DST test --- test/integration-common/src/lib.rs | 14 ++++++++++++++ test/integration-ebpf/src/log.rs | 25 ++++++++++++++++++++++++- test/integration-test/src/tests/log.rs | 18 +++++++++++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/test/integration-common/src/lib.rs b/test/integration-common/src/lib.rs index f75a3939..4d108003 100644 --- a/test/integration-common/src/lib.rs +++ b/test/integration-common/src/lib.rs @@ -14,6 +14,20 @@ pub mod bpf_probe_read { unsafe impl aya::Pod for TestResult {} } +pub mod log { + pub const BUF_LEN: usize = 1024; + + #[derive(Copy, Clone)] + #[repr(C)] + pub struct Buffer { + pub buf: [u8; BUF_LEN], // 64 KiB, one more than LogValueLength::MAX. + pub len: usize, + } + + #[cfg(feature = "user")] + unsafe impl aya::Pod for Buffer {} +} + pub mod raw_tracepoint { #[repr(C)] #[derive(Clone, Copy)] diff --git a/test/integration-ebpf/src/log.rs b/test/integration-ebpf/src/log.rs index 5972af22..3835cf17 100644 --- a/test/integration-ebpf/src/log.rs +++ b/test/integration-ebpf/src/log.rs @@ -3,11 +3,19 @@ use core::net::{IpAddr, Ipv4Addr, Ipv6Addr}; -use aya_ebpf::{macros::uprobe, programs::ProbeContext}; +use aya_ebpf::{ + macros::{map, uprobe}, + maps::Array, + programs::ProbeContext, +}; use aya_log_ebpf::{debug, error, info, trace, warn}; +use integration_common::log::Buffer; #[cfg(not(test))] extern crate ebpf_panic; +#[map] +static BUFFER: Array = Array::with_max_entries(1, 0); + #[uprobe] pub fn test_log(ctx: ProbeContext) { debug!(&ctx, "Hello from eBPF!"); @@ -88,4 +96,19 @@ pub fn test_log(ctx: ProbeContext) { debug!(&ctx, "{:x}", no_copy.consume()); } + + let Some(ptr) = BUFFER.get_ptr_mut(0) else { + return; + }; + let src = unsafe { ptr.as_ref() }; + let Some(Buffer { buf, len }) = src else { + return; + }; + let len = *len; + let buf = if len < buf.len() { + &buf[..len] + } else { + &buf[..] + }; + info!(&ctx, "variable length buffer: {:x}", buf); } diff --git a/test/integration-test/src/tests/log.rs b/test/integration-test/src/tests/log.rs index 987ec23b..a20ab1db 100644 --- a/test/integration-test/src/tests/log.rs +++ b/test/integration-test/src/tests/log.rs @@ -1,7 +1,8 @@ use std::{borrow::Cow, sync::Mutex}; -use aya::{Ebpf, programs::UProbe}; +use aya::{Ebpf, maps::Array, programs::UProbe}; use aya_log::EbpfLogger; +use integration_common::log::{BUF_LEN, Buffer}; use log::{Level, Log, Record}; #[unsafe(no_mangle)] @@ -39,6 +40,21 @@ struct CapturedLog<'a> { fn log() { let mut bpf = Ebpf::load(crate::LOG).unwrap(); + { + let buffer = bpf.map_mut("BUFFER").unwrap(); + let mut buffer: Array<_, Buffer> = Array::try_from(buffer).unwrap(); + buffer + .set( + 0, + Buffer { + buf: [0xff; BUF_LEN], + len: 64, + }, + 0, + ) + .unwrap(); + } + let mut captured_logs = Vec::new(); let logger = TestingLogger { log: Mutex::new(|record: &Record| {